blob: a990afb11d148b6657401923f19d49b991729bad [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 }
357 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
358 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");
drh5b6afba2008-01-05 16:29:28 +0000489}
490#endif
491
492#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000493# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000494#else
495# define REGISTER_TRACE(R,M)
496#endif
497
danielk197784ac9d02004-05-18 09:58:06 +0000498
drh7b396862003-01-01 23:06:20 +0000499#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000500
501/*
502** hwtime.h contains inline assembler code for implementing
503** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000504*/
shane9bcbdad2008-05-29 20:22:37 +0000505#include "hwtime.h"
506
drh7b396862003-01-01 23:06:20 +0000507#endif
508
danielk1977fd7f0452008-12-17 17:30:26 +0000509#ifndef NDEBUG
510/*
511** This function is only called from within an assert() expression. It
512** checks that the sqlite3.nTransaction variable is correctly set to
513** the number of non-transaction savepoints currently in the
514** linked list starting at sqlite3.pSavepoint.
515**
516** Usage:
517**
518** assert( checkSavepointCount(db) );
519*/
520static int checkSavepointCount(sqlite3 *db){
521 int n = 0;
522 Savepoint *p;
523 for(p=db->pSavepoint; p; p=p->pNext) n++;
524 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
525 return 1;
526}
527#endif
528
drh27a348c2015-04-13 19:14:06 +0000529/*
530** Return the register of pOp->p2 after first preparing it to be
531** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000532*/
533static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
534 sqlite3VdbeMemSetNull(pOut);
535 pOut->flags = MEM_Int;
536 return pOut;
537}
drh27a348c2015-04-13 19:14:06 +0000538static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
539 Mem *pOut;
540 assert( pOp->p2>0 );
drh9f6168b2016-03-19 23:32:58 +0000541 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
drh27a348c2015-04-13 19:14:06 +0000542 pOut = &p->aMem[pOp->p2];
543 memAboutToChange(p, pOut);
drha3fa1402016-04-29 02:55:05 +0000544 if( VdbeMemDynamic(pOut) ){ /*OPTIMIZATION-IF-FALSE*/
drh9eef8c62015-10-15 17:31:41 +0000545 return out2PrereleaseWithClear(pOut);
546 }else{
547 pOut->flags = MEM_Int;
548 return pOut;
549 }
drh27a348c2015-04-13 19:14:06 +0000550}
551
drhb9755982010-07-24 16:34:37 +0000552
553/*
drh0fd61352014-02-07 02:29:45 +0000554** Execute as much of a VDBE program as we can.
555** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000556*/
danielk19774adee202004-05-08 08:23:19 +0000557int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000558 Vdbe *p /* The VDBE */
559){
drhbbe879d2009-11-14 18:04:35 +0000560 Op *aOp = p->aOp; /* Copy of p->aOp */
mistachkin5f7b95f2017-02-01 23:03:54 +0000561 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000562#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
563 Op *pOrigOp; /* Value of pOp at the top of the loop */
564#endif
drhb89aeb62016-01-27 15:49:32 +0000565#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000566 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000567#endif
drhb86ccfb2003-01-28 23:13:10 +0000568 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000569 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000570 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000571 u8 encoding = ENC(db); /* The database encoding */
drh0f825a72016-08-13 14:17:02 +0000572 int iCompare = 0; /* Result of last comparison */
drhbf159fa2013-06-25 22:01:22 +0000573 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000574#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000575 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000576#endif
drha6c2ed92009-11-14 23:22:23 +0000577 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000578 Mem *pIn1 = 0; /* 1st input operand */
579 Mem *pIn2 = 0; /* 2nd input operand */
580 Mem *pIn3 = 0; /* 3rd input operand */
581 Mem *pOut = 0; /* Output operand */
drhb86ccfb2003-01-28 23:13:10 +0000582#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000583 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000584#endif
drh856c1032009-06-02 15:21:42 +0000585 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000586
drhca48c902008-01-18 14:08:24 +0000587 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000588 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000589 if( p->rc==SQLITE_NOMEM ){
590 /* This happens if a malloc() inside a call to sqlite3_column_text() or
591 ** sqlite3_column_text16() failed. */
592 goto no_mem;
593 }
drhcbd8db32015-08-20 17:18:32 +0000594 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000595 assert( p->bIsReader || p->readOnly!=0 );
drh95a7b3e2013-09-16 12:57:19 +0000596 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000597 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000598 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000599 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000600 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000601 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000602#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
603 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000604 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000605 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000606 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh0d1961e2013-07-25 16:27:51 +0000607 }
608#endif
drh3c23a882007-01-09 14:01:13 +0000609#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000610 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000611 if( p->pc==0
612 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
613 ){
drh3c23a882007-01-09 14:01:13 +0000614 int i;
drh84e55a82013-11-13 17:58:23 +0000615 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000616 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000617 if( p->db->flags & SQLITE_VdbeListing ){
618 printf("VDBE Program Listing:\n");
619 for(i=0; i<p->nOp; i++){
620 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
621 }
drh3c23a882007-01-09 14:01:13 +0000622 }
drh84e55a82013-11-13 17:58:23 +0000623 if( p->db->flags & SQLITE_VdbeEQP ){
624 for(i=0; i<p->nOp; i++){
625 if( aOp[i].opcode==OP_Explain ){
626 if( once ) printf("VDBE Query Plan:\n");
627 printf("%s\n", aOp[i].p4.z);
628 once = 0;
629 }
630 }
631 }
632 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000633 }
danielk19772d1d86f2008-06-20 14:59:51 +0000634 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000635#endif
drh9467abf2016-02-17 18:44:11 +0000636 for(pOp=&aOp[p->pc]; 1; pOp++){
637 /* Errors are detected by individual opcodes, with an immediate
638 ** jumps to abort_due_to_error. */
639 assert( rc==SQLITE_OK );
640
drhf56fa462015-04-13 21:39:54 +0000641 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000642#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000643 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000644#endif
drhbf159fa2013-06-25 22:01:22 +0000645 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000646#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000647 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000648#endif
drh6e142f52000-06-08 13:36:40 +0000649
danielk19778b60e0f2005-01-12 09:10:39 +0000650 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000651 */
danielk19778b60e0f2005-01-12 09:10:39 +0000652#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000653 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000654 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000655 }
drh3f7d4e42004-07-24 14:35:58 +0000656#endif
657
drh6e142f52000-06-08 13:36:40 +0000658
drhf6038712004-02-08 18:07:34 +0000659 /* Check to see if we need to simulate an interrupt. This only happens
660 ** if we have a special test build.
661 */
662#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000663 if( sqlite3_interrupt_count>0 ){
664 sqlite3_interrupt_count--;
665 if( sqlite3_interrupt_count==0 ){
666 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000667 }
668 }
669#endif
670
drh3c657212009-11-17 23:59:58 +0000671 /* Sanity checking on other operands */
672#ifdef SQLITE_DEBUG
drh7cc84c22016-04-11 13:36:42 +0000673 {
674 u8 opProperty = sqlite3OpcodeProperty[pOp->opcode];
675 if( (opProperty & OPFLG_IN1)!=0 ){
676 assert( pOp->p1>0 );
677 assert( pOp->p1<=(p->nMem+1 - p->nCursor) );
678 assert( memIsValid(&aMem[pOp->p1]) );
679 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
680 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
681 }
682 if( (opProperty & OPFLG_IN2)!=0 ){
683 assert( pOp->p2>0 );
684 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
685 assert( memIsValid(&aMem[pOp->p2]) );
686 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
687 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
688 }
689 if( (opProperty & OPFLG_IN3)!=0 ){
690 assert( pOp->p3>0 );
691 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
692 assert( memIsValid(&aMem[pOp->p3]) );
693 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
694 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
695 }
696 if( (opProperty & OPFLG_OUT2)!=0 ){
697 assert( pOp->p2>0 );
698 assert( pOp->p2<=(p->nMem+1 - p->nCursor) );
699 memAboutToChange(p, &aMem[pOp->p2]);
700 }
701 if( (opProperty & OPFLG_OUT3)!=0 ){
702 assert( pOp->p3>0 );
703 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
704 memAboutToChange(p, &aMem[pOp->p3]);
705 }
drh3c657212009-11-17 23:59:58 +0000706 }
707#endif
drh6dc41482015-04-16 17:31:02 +0000708#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
709 pOrigOp = pOp;
710#endif
drh93952eb2009-11-13 19:43:43 +0000711
drh75897232000-05-29 14:26:00 +0000712 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000713
drh5e00f6c2001-09-13 13:46:56 +0000714/*****************************************************************************
715** What follows is a massive switch statement where each case implements a
716** separate instruction in the virtual machine. If we follow the usual
717** indentation conventions, each case should be indented by 6 spaces. But
718** that is a lot of wasted space on the left margin. So the code within
719** the switch statement will break with convention and be flush-left. Another
720** big comment (similar to this one) will mark the point in the code where
721** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000722**
723** The formatting of each case is important. The makefile for SQLite
724** generates two C files "opcodes.h" and "opcodes.c" by scanning this
725** file looking for lines that begin with "case OP_". The opcodes.h files
726** will be filled with #defines that give unique integer values to each
727** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000728** each string is the symbolic name for the corresponding opcode. If the
729** case statement is followed by a comment of the form "/# same as ... #/"
730** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000731**
drh9cbf3422008-01-17 16:22:13 +0000732** Other keywords in the comment that follows each case are used to
733** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000734** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000735** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000736**
drhac82fcf2002-09-08 17:23:41 +0000737** Documentation about VDBE opcodes is generated by scanning this file
738** for lines of that contain "Opcode:". That line and all subsequent
739** comment lines are used in the generation of the opcode.html documentation
740** file.
741**
742** SUMMARY:
743**
744** Formatting is important to scripts that scan this file.
745** Do not deviate from the formatting style currently in use.
746**
drh5e00f6c2001-09-13 13:46:56 +0000747*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000748
drh9cbf3422008-01-17 16:22:13 +0000749/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000750**
751** An unconditional jump to address P2.
752** The next instruction executed will be
753** the one at index P2 from the beginning of
754** the program.
drhfe705102014-03-06 13:38:37 +0000755**
756** The P1 parameter is not actually used by this opcode. However, it
757** is sometimes set to 1 instead of 0 as a hint to the command-line shell
758** that this Goto is the bottom of a loop and that the lines from P2 down
759** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000760*/
drh9cbf3422008-01-17 16:22:13 +0000761case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000762jump_to_p2_and_check_for_interrupt:
763 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000764
765 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
766 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
767 ** completion. Check to see if sqlite3_interrupt() has been called
768 ** or if the progress callback needs to be invoked.
769 **
770 ** This code uses unstructured "goto" statements and does not look clean.
771 ** But that is not due to sloppy coding habits. The code is written this
772 ** way for performance, to avoid having to run the interrupt and progress
773 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
774 ** faster according to "valgrind --tool=cachegrind" */
775check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000776 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000777#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
778 /* Call the progress callback if it is configured and the required number
779 ** of VDBE ops have been executed (either since this invocation of
780 ** sqlite3VdbeExec() or since last time the progress callback was called).
781 ** If the progress callback returns non-zero, exit the virtual machine with
782 ** a return code SQLITE_ABORT.
783 */
drh0d1961e2013-07-25 16:27:51 +0000784 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000785 assert( db->nProgressOps!=0 );
786 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
787 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000788 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000789 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000790 }
drh49afe3a2013-07-10 03:05:14 +0000791 }
792#endif
793
drh5e00f6c2001-09-13 13:46:56 +0000794 break;
795}
drh75897232000-05-29 14:26:00 +0000796
drh2eb95372008-06-06 15:04:36 +0000797/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000798**
drh2eb95372008-06-06 15:04:36 +0000799** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000800** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000801*/
drhb8475df2011-12-09 16:21:19 +0000802case OP_Gosub: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000803 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000804 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000805 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000806 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000807 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000808 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000809 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000810
811 /* Most jump operations do a goto to this spot in order to update
812 ** the pOp pointer. */
813jump_to_p2:
814 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000815 break;
816}
817
drh2eb95372008-06-06 15:04:36 +0000818/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000819**
drh81cf13e2014-02-07 18:27:53 +0000820** Jump to the next instruction after the address in register P1. After
821** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000822*/
drh2eb95372008-06-06 15:04:36 +0000823case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000824 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000825 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000826 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000827 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000828 break;
829}
830
drhed71a832014-02-07 19:18:10 +0000831/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000832**
drh5dad9a32014-07-25 18:37:42 +0000833** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000834** located at address P3.
835**
drh5dad9a32014-07-25 18:37:42 +0000836** If P2!=0 then the coroutine implementation immediately follows
837** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000838** address P2.
drh5dad9a32014-07-25 18:37:42 +0000839**
840** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000841*/
842case OP_InitCoroutine: { /* jump */
drh9f6168b2016-03-19 23:32:58 +0000843 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drhed71a832014-02-07 19:18:10 +0000844 assert( pOp->p2>=0 && pOp->p2<p->nOp );
845 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000846 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000847 assert( !VdbeMemDynamic(pOut) );
848 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000849 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000850 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000851 break;
852}
853
854/* Opcode: EndCoroutine P1 * * * *
855**
drhbc5cf382014-08-06 01:08:07 +0000856** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000857** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000858** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000859**
860** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000861*/
862case OP_EndCoroutine: { /* in1 */
863 VdbeOp *pCaller;
864 pIn1 = &aMem[pOp->p1];
865 assert( pIn1->flags==MEM_Int );
866 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
867 pCaller = &aOp[pIn1->u.i];
868 assert( pCaller->opcode==OP_Yield );
869 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000870 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000871 pIn1->flags = MEM_Undefined;
872 break;
873}
874
875/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000876**
drh5dad9a32014-07-25 18:37:42 +0000877** Swap the program counter with the value in register P1. This
878** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000879**
drh5dad9a32014-07-25 18:37:42 +0000880** If the coroutine that is launched by this instruction ends with
881** Yield or Return then continue to the next instruction. But if
882** the coroutine launched by this instruction ends with
883** EndCoroutine, then jump to P2 rather than continuing with the
884** next instruction.
885**
886** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000887*/
drh81cf13e2014-02-07 18:27:53 +0000888case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000889 int pcDest;
drh3c657212009-11-17 23:59:58 +0000890 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000891 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000892 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000893 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000894 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000895 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000896 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000897 break;
898}
899
drhf9c8ce32013-11-05 13:33:55 +0000900/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +0000901** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000902**
drhef8662b2011-06-20 21:47:58 +0000903** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000904** parameter P1, P2, and P4 as if this were a Halt instruction. If the
905** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000906** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000907*/
908case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000909 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000910 if( (pIn3->flags & MEM_Null)==0 ) break;
911 /* Fall through into OP_Halt */
912}
drhe00ee6e2008-06-20 15:24:01 +0000913
drhf9c8ce32013-11-05 13:33:55 +0000914/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000915**
drh3d4501e2008-12-04 20:40:10 +0000916** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000917** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000918**
drh92f02c32004-09-02 14:57:08 +0000919** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
920** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
921** For errors, it can be some other value. If P1!=0 then P2 will determine
922** whether or not to rollback the current transaction. Do not rollback
923** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
924** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000925** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000926**
drh66a51672008-01-03 00:01:23 +0000927** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000928**
drhf9c8ce32013-11-05 13:33:55 +0000929** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
930**
931** 0: (no change)
932** 1: NOT NULL contraint failed: P4
933** 2: UNIQUE constraint failed: P4
934** 3: CHECK constraint failed: P4
935** 4: FOREIGN KEY constraint failed: P4
936**
937** If P5 is not zero and P4 is NULL, then everything after the ":" is
938** omitted.
939**
drh9cfcf5d2002-01-29 18:41:24 +0000940** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000941** every program. So a jump past the last instruction of the program
942** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000943*/
drh9cbf3422008-01-17 16:22:13 +0000944case OP_Halt: {
drhf56fa462015-04-13 21:39:54 +0000945 VdbeFrame *pFrame;
946 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000947
drhf56fa462015-04-13 21:39:54 +0000948 pcx = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +0000949 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000950 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000951 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000952 p->pFrame = pFrame->pParent;
953 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000954 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000955 pcx = sqlite3VdbeFrameRestore(pFrame);
dan165921a2009-08-28 18:53:45 +0000956 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000957 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000958 ** currently being halted. If the p2 instruction of this OP_Halt
959 ** instruction is set to OE_Ignore, then the sub-program is throwing
960 ** an IGNORE exception. In this case jump to the address specified
961 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +0000962 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +0000963 }
drhbbe879d2009-11-14 18:04:35 +0000964 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000965 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +0000966 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +0000967 break;
968 }
drh92f02c32004-09-02 14:57:08 +0000969 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000970 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +0000971 p->pc = pcx;
drhfb4e3a32016-12-30 00:09:14 +0000972 assert( pOp->p5<=4 );
drhf9c8ce32013-11-05 13:33:55 +0000973 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000974 if( pOp->p5 ){
975 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
976 "FOREIGN KEY" };
drhd9b7ec92013-11-06 14:05:21 +0000977 testcase( pOp->p5==1 );
978 testcase( pOp->p5==2 );
979 testcase( pOp->p5==3 );
980 testcase( pOp->p5==4 );
drh99f5de72016-04-30 02:59:15 +0000981 sqlite3VdbeError(p, "%s constraint failed", azType[pOp->p5-1]);
982 if( pOp->p4.z ){
983 p->zErrMsg = sqlite3MPrintf(db, "%z: %s", p->zErrMsg, pOp->p4.z);
984 }
drhd9b7ec92013-11-06 14:05:21 +0000985 }else{
drh22c17b82015-05-15 04:13:15 +0000986 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000987 }
drh99f5de72016-04-30 02:59:15 +0000988 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000989 }
drh92f02c32004-09-02 14:57:08 +0000990 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000991 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000992 if( rc==SQLITE_BUSY ){
drh99f5de72016-04-30 02:59:15 +0000993 p->rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +0000994 }else{
drhd91c1a12013-02-09 13:58:25 +0000995 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
dancb3e4b72013-07-03 19:53:05 +0000996 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000997 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000998 }
drh900b31e2007-08-28 02:27:51 +0000999 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001000}
drhc61053b2000-06-04 12:58:36 +00001001
drh4c583122008-01-04 22:01:03 +00001002/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001003** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001004**
drh9cbf3422008-01-17 16:22:13 +00001005** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001006*/
drh27a348c2015-04-13 19:14:06 +00001007case OP_Integer: { /* out2 */
1008 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001009 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001010 break;
1011}
1012
drh4c583122008-01-04 22:01:03 +00001013/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001014** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001015**
drh66a51672008-01-03 00:01:23 +00001016** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001017** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001018*/
drh27a348c2015-04-13 19:14:06 +00001019case OP_Int64: { /* out2 */
1020 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001021 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001022 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001023 break;
1024}
drh4f26d6c2004-05-26 23:25:30 +00001025
drh13573c72010-01-12 17:04:07 +00001026#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001027/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001028** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001029**
drh4c583122008-01-04 22:01:03 +00001030** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001031** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001032*/
drh27a348c2015-04-13 19:14:06 +00001033case OP_Real: { /* same as TK_FLOAT, out2 */
1034 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001035 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001036 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001037 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001038 break;
1039}
drh13573c72010-01-12 17:04:07 +00001040#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001041
drh3c84ddf2008-01-09 02:15:38 +00001042/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001043** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001044**
drh66a51672008-01-03 00:01:23 +00001045** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001046** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001047** this transformation, the length of string P4 is computed and stored
1048** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001049*/
drh27a348c2015-04-13 19:14:06 +00001050case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001051 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001052 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001053 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001054 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001055
1056#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001057 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001058 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh2f555112016-04-30 18:10:34 +00001059 assert( rc==SQLITE_OK || rc==SQLITE_TOOBIG );
drh4c583122008-01-04 22:01:03 +00001060 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001061 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001062 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001063 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001064 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001065 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001066 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001067 }
drh66a51672008-01-03 00:01:23 +00001068 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001069 pOp->p4.z = pOut->z;
1070 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001071 }
drh2f555112016-04-30 18:10:34 +00001072 testcase( rc==SQLITE_TOOBIG );
danielk197793758c82005-01-21 08:13:14 +00001073#endif
drhbb4957f2008-03-20 14:03:29 +00001074 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001075 goto too_big;
1076 }
drh2f555112016-04-30 18:10:34 +00001077 assert( rc==SQLITE_OK );
drhcbd2da92007-12-17 16:20:06 +00001078 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001079}
drhf4479502004-05-27 03:12:53 +00001080
drhf07cf6e2015-03-06 16:45:16 +00001081/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001082** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001083**
drh9cbf3422008-01-17 16:22:13 +00001084** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001085**
drh44aebff2016-05-02 10:25:42 +00001086** If P3 is not zero and the content of register P3 is equal to P5, then
drha9c18a92015-03-06 20:49:52 +00001087** the datatype of the register P2 is converted to BLOB. The content is
1088** the same sequence of bytes, it is merely interpreted as a BLOB instead
drh44aebff2016-05-02 10:25:42 +00001089** of a string, as if it had been CAST. In other words:
1090**
1091** if( P3!=0 and reg[P3]==P5 ) reg[P2] := CAST(reg[P2] as BLOB)
drhf4479502004-05-27 03:12:53 +00001092*/
drh27a348c2015-04-13 19:14:06 +00001093case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001094 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001095 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001096 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1097 pOut->z = pOp->p4.z;
1098 pOut->n = pOp->p1;
1099 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001100 UPDATE_MAX_BLOBSIZE(pOut);
drh41d2e662015-12-01 21:23:07 +00001101#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +00001102 if( pOp->p3>0 ){
drh9f6168b2016-03-19 23:32:58 +00001103 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhf07cf6e2015-03-06 16:45:16 +00001104 pIn3 = &aMem[pOp->p3];
1105 assert( pIn3->flags & MEM_Int );
drh44aebff2016-05-02 10:25:42 +00001106 if( pIn3->u.i==pOp->p5 ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
drhf07cf6e2015-03-06 16:45:16 +00001107 }
drh41d2e662015-12-01 21:23:07 +00001108#endif
danielk1977c572ef72004-05-27 09:28:41 +00001109 break;
1110}
1111
drh053a1282012-09-19 21:15:46 +00001112/* Opcode: Null P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001113** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001114**
drhb8475df2011-12-09 16:21:19 +00001115** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001116** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001117** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001118** set to NULL.
1119**
1120** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1121** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1122** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001123*/
drh27a348c2015-04-13 19:14:06 +00001124case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001125 int cnt;
drh053a1282012-09-19 21:15:46 +00001126 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001127 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001128 cnt = pOp->p3-pOp->p2;
drh9f6168b2016-03-19 23:32:58 +00001129 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001130 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drh2a1df932016-09-30 17:46:44 +00001131 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001132 while( cnt>0 ){
1133 pOut++;
1134 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001135 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001136 pOut->flags = nullFlag;
drh2a1df932016-09-30 17:46:44 +00001137 pOut->n = 0;
drhb8475df2011-12-09 16:21:19 +00001138 cnt--;
1139 }
drhf0863fe2005-06-12 21:35:51 +00001140 break;
1141}
1142
drh05a86c52014-02-16 01:55:49 +00001143/* Opcode: SoftNull P1 * * * *
drh72e26de2016-08-24 21:24:04 +00001144** Synopsis: r[P1]=NULL
drh05a86c52014-02-16 01:55:49 +00001145**
1146** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1147** instruction, but do not free any string or blob memory associated with
1148** the register, so that if the value was a string or blob that was
1149** previously copied using OP_SCopy, the copies will continue to be valid.
1150*/
1151case OP_SoftNull: {
drh9f6168b2016-03-19 23:32:58 +00001152 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drh05a86c52014-02-16 01:55:49 +00001153 pOut = &aMem[pOp->p1];
1154 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1155 break;
1156}
drhf0863fe2005-06-12 21:35:51 +00001157
drha5750cf2014-02-07 13:20:31 +00001158/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001159** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001160**
drh9de221d2008-01-05 06:51:30 +00001161** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001162** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001163*/
drh27a348c2015-04-13 19:14:06 +00001164case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001165 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001166 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001167 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001168 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001169 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001170 break;
1171}
1172
drheaf52d82010-05-12 13:50:23 +00001173/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001174** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001175**
drheaf52d82010-05-12 13:50:23 +00001176** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001177**
drh0fd61352014-02-07 02:29:45 +00001178** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001179** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001180*/
drh27a348c2015-04-13 19:14:06 +00001181case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001182 Mem *pVar; /* Value being transferred */
1183
drheaf52d82010-05-12 13:50:23 +00001184 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh9bf755c2016-12-23 03:59:31 +00001185 assert( pOp->p4.z==0 || pOp->p4.z==sqlite3VListNumToName(p->pVList,pOp->p1) );
drheaf52d82010-05-12 13:50:23 +00001186 pVar = &p->aVar[pOp->p1 - 1];
1187 if( sqlite3VdbeMemTooBig(pVar) ){
1188 goto too_big;
drh023ae032007-05-08 12:12:16 +00001189 }
drh7441df72017-01-09 19:27:04 +00001190 pOut = &aMem[pOp->p2];
drheaf52d82010-05-12 13:50:23 +00001191 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1192 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001193 break;
1194}
danielk1977295ba552004-05-19 10:34:51 +00001195
drhb21e7c72008-06-22 12:37:57 +00001196/* Opcode: Move P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001197** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001198**
drh079a3072014-03-19 14:10:55 +00001199** Move the P3 values in register P1..P1+P3-1 over into
1200** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001201** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001202** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1203** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001204*/
drhe1349cb2008-04-01 00:36:10 +00001205case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001206 int n; /* Number of registers left to copy */
1207 int p1; /* Register to copy from */
1208 int p2; /* Register to copy to */
1209
drhe09f43f2013-11-21 04:18:31 +00001210 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001211 p1 = pOp->p1;
1212 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001213 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001214 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001215
drha6c2ed92009-11-14 23:22:23 +00001216 pIn1 = &aMem[p1];
1217 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001218 do{
drh9f6168b2016-03-19 23:32:58 +00001219 assert( pOut<=&aMem[(p->nMem+1 - p->nCursor)] );
1220 assert( pIn1<=&aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001221 assert( memIsValid(pIn1) );
1222 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001223 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001224#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001225 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001226 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001227 }
1228#endif
drhbd6789e2015-04-28 14:00:02 +00001229 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001230 REGISTER_TRACE(p2++, pOut);
1231 pIn1++;
1232 pOut++;
drh079a3072014-03-19 14:10:55 +00001233 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001234 break;
1235}
1236
drhe8e4af72012-09-21 00:04:28 +00001237/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001238** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001239**
drhe8e4af72012-09-21 00:04:28 +00001240** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001241**
1242** This instruction makes a deep copy of the value. A duplicate
1243** is made of any string or blob constant. See also OP_SCopy.
1244*/
drhe8e4af72012-09-21 00:04:28 +00001245case OP_Copy: {
1246 int n;
1247
1248 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001249 pIn1 = &aMem[pOp->p1];
1250 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001251 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001252 while( 1 ){
1253 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1254 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001255#ifdef SQLITE_DEBUG
1256 pOut->pScopyFrom = 0;
1257#endif
drhe8e4af72012-09-21 00:04:28 +00001258 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1259 if( (n--)==0 ) break;
1260 pOut++;
1261 pIn1++;
1262 }
drhe1349cb2008-04-01 00:36:10 +00001263 break;
1264}
1265
drhb1fdb2a2008-01-05 04:06:03 +00001266/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001267** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001268**
drh9cbf3422008-01-17 16:22:13 +00001269** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001270**
1271** This instruction makes a shallow copy of the value. If the value
1272** is a string or blob, then the copy is only a pointer to the
1273** original and hence if the original changes so will the copy.
1274** Worse, if the original is deallocated, the copy becomes invalid.
1275** Thus the program must guarantee that the original will not change
1276** during the lifetime of the copy. Use OP_Copy to make a complete
1277** copy.
1278*/
drh26198bb2013-10-31 11:15:09 +00001279case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001280 pIn1 = &aMem[pOp->p1];
1281 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001282 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001283 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001284#ifdef SQLITE_DEBUG
1285 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1286#endif
drh5e00f6c2001-09-13 13:46:56 +00001287 break;
1288}
drh75897232000-05-29 14:26:00 +00001289
drhfed7ac62015-10-15 18:04:59 +00001290/* Opcode: IntCopy P1 P2 * * *
1291** Synopsis: r[P2]=r[P1]
1292**
1293** Transfer the integer value held in register P1 into register P2.
1294**
1295** This is an optimized version of SCopy that works only for integer
1296** values.
1297*/
1298case OP_IntCopy: { /* out2 */
1299 pIn1 = &aMem[pOp->p1];
1300 assert( (pIn1->flags & MEM_Int)!=0 );
1301 pOut = &aMem[pOp->p2];
1302 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1303 break;
1304}
1305
drh9cbf3422008-01-17 16:22:13 +00001306/* Opcode: ResultRow P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001307** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001308**
shane21e7feb2008-05-30 15:59:49 +00001309** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001310** results. This opcode causes the sqlite3_step() call to terminate
1311** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001312** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001313** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001314*/
drh9cbf3422008-01-17 16:22:13 +00001315case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001316 Mem *pMem;
1317 int i;
1318 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001319 assert( pOp->p1>0 );
drh9f6168b2016-03-19 23:32:58 +00001320 assert( pOp->p1+pOp->p2<=(p->nMem+1 - p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001321
drhe6400b92013-11-13 23:48:46 +00001322#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1323 /* Run the progress counter just before returning.
1324 */
1325 if( db->xProgress!=0
1326 && nVmStep>=nProgressLimit
1327 && db->xProgress(db->pProgressArg)!=0
1328 ){
1329 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001330 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001331 }
1332#endif
1333
dan32b09f22009-09-23 17:29:59 +00001334 /* If this statement has violated immediate foreign key constraints, do
1335 ** not return the number of rows modified. And do not RELEASE the statement
1336 ** transaction. It needs to be rolled back. */
1337 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1338 assert( db->flags&SQLITE_CountRows );
1339 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001340 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001341 }
1342
danielk1977bd434552009-03-18 10:33:00 +00001343 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1344 ** DML statements invoke this opcode to return the number of rows
1345 ** modified to the user. This is the only way that a VM that
1346 ** opens a statement transaction may invoke this opcode.
1347 **
1348 ** In case this is such a statement, close any statement transaction
1349 ** opened by this VM before returning control to the user. This is to
1350 ** ensure that statement-transactions are always nested, not overlapping.
1351 ** If the open statement-transaction is not closed here, then the user
1352 ** may step another VM that opens its own statement transaction. This
1353 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001354 **
1355 ** The statement transaction is never a top-level transaction. Hence
1356 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001357 */
1358 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001359 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001360 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001361
drhd4e70eb2008-01-02 00:34:36 +00001362 /* Invalidate all ephemeral cursor row caches */
1363 p->cacheCtr = (p->cacheCtr + 2)|1;
1364
1365 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001366 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001367 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001368 */
drha6c2ed92009-11-14 23:22:23 +00001369 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001370 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001371 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001372 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001373 assert( (pMem[i].flags & MEM_Ephem)==0
1374 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001375 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001376 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001377 }
drh28039692008-03-17 16:54:01 +00001378 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001379
drh3d2a5292016-07-13 22:55:01 +00001380 if( db->mTrace & SQLITE_TRACE_ROW ){
1381 db->xTrace(SQLITE_TRACE_ROW, db->pTraceArg, p, 0);
1382 }
1383
drhd4e70eb2008-01-02 00:34:36 +00001384 /* Return SQLITE_ROW
1385 */
drhf56fa462015-04-13 21:39:54 +00001386 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001387 rc = SQLITE_ROW;
1388 goto vdbe_return;
1389}
1390
drh5b6afba2008-01-05 16:29:28 +00001391/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001392** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001393**
drh5b6afba2008-01-05 16:29:28 +00001394** Add the text in register P1 onto the end of the text in
1395** register P2 and store the result in register P3.
1396** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001397**
1398** P3 = P2 || P1
1399**
1400** It is illegal for P1 and P3 to be the same register. Sometimes,
1401** if P3 is the same register as P2, the implementation is able
1402** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001403*/
drh5b6afba2008-01-05 16:29:28 +00001404case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001405 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001406
drh3c657212009-11-17 23:59:58 +00001407 pIn1 = &aMem[pOp->p1];
1408 pIn2 = &aMem[pOp->p2];
1409 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001410 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001411 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001412 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001413 break;
drh5e00f6c2001-09-13 13:46:56 +00001414 }
drha0c06522009-06-17 22:50:41 +00001415 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001416 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001417 Stringify(pIn2, encoding);
1418 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001419 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001420 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001421 }
drh9c1905f2008-12-10 22:32:56 +00001422 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001423 goto no_mem;
1424 }
drhc91b2fd2014-03-01 18:13:23 +00001425 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001426 if( pOut!=pIn2 ){
1427 memcpy(pOut->z, pIn2->z, pIn2->n);
1428 }
1429 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001430 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001431 pOut->z[nByte+1] = 0;
1432 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001433 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001434 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001435 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001436 break;
1437}
drh75897232000-05-29 14:26:00 +00001438
drh3c84ddf2008-01-09 02:15:38 +00001439/* Opcode: Add P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001440** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001441**
drh60a713c2008-01-21 16:22:45 +00001442** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001443** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001444** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001445*/
drh3c84ddf2008-01-09 02:15:38 +00001446/* Opcode: Multiply P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001447** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001448**
drh3c84ddf2008-01-09 02:15:38 +00001449**
shane21e7feb2008-05-30 15:59:49 +00001450** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001451** and store the result in register P3.
1452** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001453*/
drh3c84ddf2008-01-09 02:15:38 +00001454/* Opcode: Subtract P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001455** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001456**
drh60a713c2008-01-21 16:22:45 +00001457** Subtract the value in register P1 from the value in register P2
1458** and store the result in register P3.
1459** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001460*/
drh9cbf3422008-01-17 16:22:13 +00001461/* Opcode: Divide P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001462** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001463**
drh60a713c2008-01-21 16:22:45 +00001464** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001465** and store the result in register P3 (P3=P2/P1). If the value in
1466** register P1 is zero, then the result is NULL. If either input is
1467** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001468*/
drh9cbf3422008-01-17 16:22:13 +00001469/* Opcode: Remainder P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001470** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001471**
drh40864a12013-11-15 18:58:37 +00001472** Compute the remainder after integer register P2 is divided by
1473** register P1 and store the result in register P3.
1474** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001475** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001476*/
drh5b6afba2008-01-05 16:29:28 +00001477case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1478case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1479case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1480case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1481case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001482 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001483 u16 flags; /* Combined MEM_* flags from both inputs */
1484 u16 type1; /* Numeric type of left operand */
1485 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001486 i64 iA; /* Integer value of left operand */
1487 i64 iB; /* Integer value of right operand */
1488 double rA; /* Real value of left operand */
1489 double rB; /* Real value of right operand */
1490
drh3c657212009-11-17 23:59:58 +00001491 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001492 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001493 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001494 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001495 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001496 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001497 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
drh3d1d90a2014-03-24 15:00:15 +00001498 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001499 iA = pIn1->u.i;
1500 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001501 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001502 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001503 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1504 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1505 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001506 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001507 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001508 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001509 iB /= iA;
drh75897232000-05-29 14:26:00 +00001510 break;
1511 }
drhbf4133c2001-10-13 02:59:08 +00001512 default: {
drh856c1032009-06-02 15:21:42 +00001513 if( iA==0 ) goto arithmetic_result_is_null;
1514 if( iA==-1 ) iA = 1;
1515 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001516 break;
1517 }
drh75897232000-05-29 14:26:00 +00001518 }
drh856c1032009-06-02 15:21:42 +00001519 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001520 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001521 }else{
drhbe707b32012-12-10 22:19:14 +00001522 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001523fp_math:
drh856c1032009-06-02 15:21:42 +00001524 rA = sqlite3VdbeRealValue(pIn1);
1525 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001526 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001527 case OP_Add: rB += rA; break;
1528 case OP_Subtract: rB -= rA; break;
1529 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001530 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001531 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001532 if( rA==(double)0 ) goto arithmetic_result_is_null;
1533 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001534 break;
1535 }
drhbf4133c2001-10-13 02:59:08 +00001536 default: {
shane75ac1de2009-06-09 18:58:52 +00001537 iA = (i64)rA;
1538 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001539 if( iA==0 ) goto arithmetic_result_is_null;
1540 if( iA==-1 ) iA = 1;
1541 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001542 break;
1543 }
drh5e00f6c2001-09-13 13:46:56 +00001544 }
drhc5a7b512010-01-13 16:25:42 +00001545#ifdef SQLITE_OMIT_FLOATING_POINT
1546 pOut->u.i = rB;
1547 MemSetTypeFlag(pOut, MEM_Int);
1548#else
drh856c1032009-06-02 15:21:42 +00001549 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001550 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001551 }
drh74eaba42014-09-18 17:52:15 +00001552 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001553 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001554 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001555 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001556 }
drhc5a7b512010-01-13 16:25:42 +00001557#endif
drh5e00f6c2001-09-13 13:46:56 +00001558 }
1559 break;
1560
drha05a7222008-01-19 03:35:58 +00001561arithmetic_result_is_null:
1562 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001563 break;
1564}
1565
drh7a957892012-02-02 17:35:43 +00001566/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001567**
drh66a51672008-01-03 00:01:23 +00001568** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001569** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1570** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001571** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001572**
drh7a957892012-02-02 17:35:43 +00001573** If P1 is not zero, then it is a register that a subsequent min() or
1574** max() aggregate will set to 1 if the current row is not the minimum or
1575** maximum. The P1 register is initialized to 0 by this instruction.
1576**
danielk1977dc1bdc42004-06-11 10:51:27 +00001577** The interface used by the implementation of the aforementioned functions
1578** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001579** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001580*/
drh9cbf3422008-01-17 16:22:13 +00001581case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001582 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001583 if( pOp->p1 ){
1584 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1585 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001586 break;
1587}
1588
drh9c7c9132015-06-26 18:16:52 +00001589/* Opcode: Function0 P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001590** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001591**
drhe2d9e7c2015-06-26 18:47:53 +00001592** Invoke a user function (P4 is a pointer to a FuncDef object that
drh98757152008-01-09 23:04:12 +00001593** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001594** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001595** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001596**
drh13449892005-09-07 21:22:45 +00001597** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001598** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001599** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001600** whether meta data associated with a user function argument using the
1601** sqlite3_set_auxdata() API may be safely retained until the next
1602** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001603**
drh9c7c9132015-06-26 18:16:52 +00001604** See also: Function, AggStep, AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001605*/
drh9c7c9132015-06-26 18:16:52 +00001606/* Opcode: Function P1 P2 P3 P4 P5
1607** Synopsis: r[P3]=func(r[P2@P5])
1608**
1609** Invoke a user function (P4 is a pointer to an sqlite3_context object that
1610** contains a pointer to the function to be run) with P5 arguments taken
1611** from register P2 and successors. The result of the function is stored
1612** in register P3. Register P3 must not be one of the function inputs.
1613**
1614** P1 is a 32-bit bitmask indicating whether or not each argument to the
1615** function was determined to be constant at compile time. If the first
1616** argument was constant then bit 0 of P1 is set. This is used to determine
1617** whether meta data associated with a user function argument using the
1618** sqlite3_set_auxdata() API may be safely retained until the next
1619** invocation of this opcode.
1620**
1621** SQL functions are initially coded as OP_Function0 with P4 pointing
drhe2d9e7c2015-06-26 18:47:53 +00001622** to a FuncDef object. But on first evaluation, the P4 operand is
drh9c7c9132015-06-26 18:16:52 +00001623** automatically converted into an sqlite3_context object and the operation
1624** changed to this OP_Function opcode. In this way, the initialization of
1625** the sqlite3_context object occurs only once, rather than once for each
1626** evaluation of the function.
1627**
1628** See also: Function0, AggStep, AggFinal
1629*/
1630case OP_Function0: {
drh856c1032009-06-02 15:21:42 +00001631 int n;
drh9c7c9132015-06-26 18:16:52 +00001632 sqlite3_context *pCtx;
danielk197751ad0ec2004-05-24 12:39:02 +00001633
dan0c547792013-07-18 17:12:08 +00001634 assert( pOp->p4type==P4_FUNCDEF );
drh9c7c9132015-06-26 18:16:52 +00001635 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00001636 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
1637 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00001638 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh575fad62016-02-05 13:38:36 +00001639 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
drh9c7c9132015-06-26 18:16:52 +00001640 if( pCtx==0 ) goto no_mem;
1641 pCtx->pOut = 0;
1642 pCtx->pFunc = pOp->p4.pFunc;
1643 pCtx->iOp = (int)(pOp - aOp);
1644 pCtx->pVdbe = p;
1645 pCtx->argc = n;
1646 pOp->p4type = P4_FUNCCTX;
1647 pOp->p4.pCtx = pCtx;
1648 pOp->opcode = OP_Function;
1649 /* Fall through into OP_Function */
1650}
1651case OP_Function: {
1652 int i;
1653 sqlite3_context *pCtx;
1654
1655 assert( pOp->p4type==P4_FUNCCTX );
1656 pCtx = pOp->p4.pCtx;
1657
1658 /* If this function is inside of a trigger, the register array in aMem[]
1659 ** might change from one evaluation to the next. The next block of code
1660 ** checks to see if the register array has changed, and if so it
1661 ** reinitializes the relavant parts of the sqlite3_context object */
drhe2d9e7c2015-06-26 18:47:53 +00001662 pOut = &aMem[pOp->p3];
1663 if( pCtx->pOut != pOut ){
1664 pCtx->pOut = pOut;
drh9c7c9132015-06-26 18:16:52 +00001665 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
1666 }
1667
drhe3247822017-02-21 15:27:22 +00001668 memAboutToChange(p, pOut);
drh9c7c9132015-06-26 18:16:52 +00001669#ifdef SQLITE_DEBUG
1670 for(i=0; i<pCtx->argc; i++){
1671 assert( memIsValid(pCtx->argv[i]) );
1672 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
1673 }
1674#endif
drhe3247822017-02-21 15:27:22 +00001675 MemSetTypeFlag(pOut, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00001676 pCtx->fErrorOrAux = 0;
drh2d801512016-01-14 22:19:58 +00001677 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
danielk19777e18c252004-05-25 11:47:24 +00001678
drh90669c12006-01-20 15:45:36 +00001679 /* If the function returned an error, throw an exception */
drh9c7c9132015-06-26 18:16:52 +00001680 if( pCtx->fErrorOrAux ){
1681 if( pCtx->isError ){
drhe3247822017-02-21 15:27:22 +00001682 sqlite3VdbeError(p, "%s", sqlite3_value_text(pOut));
drh9c7c9132015-06-26 18:16:52 +00001683 rc = pCtx->isError;
drh9b47ee32013-08-20 03:13:51 +00001684 }
drhb9626cf2016-02-22 16:04:31 +00001685 sqlite3VdbeDeleteAuxData(db, &p->pAuxData, pCtx->iOp, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00001686 if( rc ) goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00001687 }
1688
drh9cbf3422008-01-17 16:22:13 +00001689 /* Copy the result of the function into register P3 */
drhe2d9e7c2015-06-26 18:47:53 +00001690 if( pOut->flags & (MEM_Str|MEM_Blob) ){
drhe3247822017-02-21 15:27:22 +00001691 sqlite3VdbeChangeEncoding(pOut, encoding);
1692 if( sqlite3VdbeMemTooBig(pOut) ) goto too_big;
drh023ae032007-05-08 12:12:16 +00001693 }
drh7b94e7f2011-04-04 12:29:20 +00001694
drhe3247822017-02-21 15:27:22 +00001695 REGISTER_TRACE(pOp->p3, pOut);
1696 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001697 break;
1698}
1699
drh98757152008-01-09 23:04:12 +00001700/* Opcode: BitAnd P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001701** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001702**
drh98757152008-01-09 23:04:12 +00001703** Take the bit-wise AND of the values in register P1 and P2 and
1704** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001705** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001706*/
drh98757152008-01-09 23:04:12 +00001707/* Opcode: BitOr P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001708** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001709**
drh98757152008-01-09 23:04:12 +00001710** Take the bit-wise OR of the values in register P1 and P2 and
1711** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001712** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001713*/
drh98757152008-01-09 23:04:12 +00001714/* Opcode: ShiftLeft P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001715** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001716**
drh98757152008-01-09 23:04:12 +00001717** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001718** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001719** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001720** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001721*/
drh98757152008-01-09 23:04:12 +00001722/* Opcode: ShiftRight P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00001723** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001724**
drh98757152008-01-09 23:04:12 +00001725** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001726** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001727** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001728** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001729*/
drh5b6afba2008-01-05 16:29:28 +00001730case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1731case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1732case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1733case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001734 i64 iA;
1735 u64 uA;
1736 i64 iB;
1737 u8 op;
drh6810ce62004-01-31 19:22:56 +00001738
drh3c657212009-11-17 23:59:58 +00001739 pIn1 = &aMem[pOp->p1];
1740 pIn2 = &aMem[pOp->p2];
1741 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001742 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001743 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001744 break;
1745 }
drh158b9cb2011-03-05 20:59:46 +00001746 iA = sqlite3VdbeIntValue(pIn2);
1747 iB = sqlite3VdbeIntValue(pIn1);
1748 op = pOp->opcode;
1749 if( op==OP_BitAnd ){
1750 iA &= iB;
1751 }else if( op==OP_BitOr ){
1752 iA |= iB;
1753 }else if( iB!=0 ){
1754 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1755
1756 /* If shifting by a negative amount, shift in the other direction */
1757 if( iB<0 ){
1758 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1759 op = 2*OP_ShiftLeft + 1 - op;
1760 iB = iB>(-64) ? -iB : 64;
1761 }
1762
1763 if( iB>=64 ){
1764 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1765 }else{
1766 memcpy(&uA, &iA, sizeof(uA));
1767 if( op==OP_ShiftLeft ){
1768 uA <<= iB;
1769 }else{
1770 uA >>= iB;
1771 /* Sign-extend on a right shift of a negative number */
1772 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1773 }
1774 memcpy(&iA, &uA, sizeof(iA));
1775 }
drhbf4133c2001-10-13 02:59:08 +00001776 }
drh158b9cb2011-03-05 20:59:46 +00001777 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001778 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001779 break;
1780}
1781
drh8558cde2008-01-05 05:20:10 +00001782/* Opcode: AddImm P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00001783** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001784**
danielk19770cdc0222008-06-26 18:04:03 +00001785** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001786** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001787**
drh8558cde2008-01-05 05:20:10 +00001788** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001789*/
drh9cbf3422008-01-17 16:22:13 +00001790case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001791 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001792 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001793 sqlite3VdbeMemIntegerify(pIn1);
1794 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001795 break;
1796}
1797
drh9cbf3422008-01-17 16:22:13 +00001798/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001799**
drh9cbf3422008-01-17 16:22:13 +00001800** Force the value in register P1 to be an integer. If the value
1801** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001802** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001803** raise an SQLITE_MISMATCH exception.
1804*/
drh9cbf3422008-01-17 16:22:13 +00001805case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001806 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001807 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001808 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001809 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001810 if( (pIn1->flags & MEM_Int)==0 ){
1811 if( pOp->p2==0 ){
1812 rc = SQLITE_MISMATCH;
1813 goto abort_due_to_error;
1814 }else{
drhf56fa462015-04-13 21:39:54 +00001815 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001816 }
drh8aff1012001-12-22 14:49:24 +00001817 }
drh8aff1012001-12-22 14:49:24 +00001818 }
drh83b301b2013-11-20 00:59:02 +00001819 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001820 break;
1821}
1822
drh13573c72010-01-12 17:04:07 +00001823#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001824/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001825**
drh2133d822008-01-03 18:44:59 +00001826** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001827**
drh8a512562005-11-14 22:29:05 +00001828** This opcode is used when extracting information from a column that
1829** has REAL affinity. Such column values may still be stored as
1830** integers, for space efficiency, but after extraction we want them
1831** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001832*/
drh9cbf3422008-01-17 16:22:13 +00001833case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001834 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001835 if( pIn1->flags & MEM_Int ){
1836 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001837 }
drh487e2622005-06-25 18:42:14 +00001838 break;
1839}
drh13573c72010-01-12 17:04:07 +00001840#endif
drh487e2622005-06-25 18:42:14 +00001841
drh8df447f2005-11-01 15:48:24 +00001842#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001843/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001844** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001845**
drh4169e432014-08-25 20:11:52 +00001846** Force the value in register P1 to be the type defined by P2.
1847**
1848** <ul>
1849** <li value="97"> TEXT
1850** <li value="98"> BLOB
1851** <li value="99"> NUMERIC
1852** <li value="100"> INTEGER
1853** <li value="101"> REAL
1854** </ul>
drh487e2622005-06-25 18:42:14 +00001855**
1856** A NULL value is not changed by this routine. It remains NULL.
1857*/
drh4169e432014-08-25 20:11:52 +00001858case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001859 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001860 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001861 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001862 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1863 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1864 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001865 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001866 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001867 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001868 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001869 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001870 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001871 break;
1872}
drh8a512562005-11-14 22:29:05 +00001873#endif /* SQLITE_OMIT_CAST */
1874
drh79752b62016-08-13 10:02:17 +00001875/* Opcode: Eq P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001876** Synopsis: IF r[P3]==r[P1]
drh79752b62016-08-13 10:02:17 +00001877**
1878** Compare the values in register P1 and P3. If reg(P3)==reg(P1) then
1879** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5, then
1880** store the result of comparison in register P2.
1881**
1882** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
1883** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1884** to coerce both inputs according to this affinity before the
1885** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
1886** affinity is used. Note that the affinity conversions are stored
1887** back into the input registers P1 and P3. So this opcode can cause
1888** persistent changes to registers P1 and P3.
1889**
1890** Once any conversions have taken place, and neither value is NULL,
1891** the values are compared. If both values are blobs then memcmp() is
1892** used to determine the results of the comparison. If both values
1893** are text, then the appropriate collating function specified in
1894** P4 is used to do the comparison. If P4 is not specified then
1895** memcmp() is used to compare text string. If both values are
1896** numeric, then a numeric comparison is used. If the two values
1897** are of different types, then numbers are considered less than
1898** strings and strings are considered less than blobs.
1899**
1900** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1901** true or false and is never NULL. If both operands are NULL then the result
1902** of comparison is true. If either operand is NULL then the result is false.
1903** If neither operand is NULL the result is the same as it would be if
1904** the SQLITE_NULLEQ flag were omitted from P5.
1905**
1906** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001907** content of r[P2] is only changed if the new value is NULL or 0 (false).
1908** In other words, a prior r[P2] value will not be overwritten by 1 (true).
drh79752b62016-08-13 10:02:17 +00001909*/
1910/* Opcode: Ne P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001911** Synopsis: IF r[P3]!=r[P1]
drh79752b62016-08-13 10:02:17 +00001912**
1913** This works just like the Eq opcode except that the jump is taken if
1914** the operands in registers P1 and P3 are not equal. See the Eq opcode for
1915** additional information.
1916**
1917** If both SQLITE_STOREP2 and SQLITE_KEEPNULL flags are set then the
drh3fffbf92016-09-05 15:02:41 +00001918** content of r[P2] is only changed if the new value is NULL or 1 (true).
1919** In other words, a prior r[P2] value will not be overwritten by 0 (false).
drh79752b62016-08-13 10:02:17 +00001920*/
drh35573352008-01-08 23:54:25 +00001921/* Opcode: Lt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001922** Synopsis: IF r[P3]<r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001923**
drh35573352008-01-08 23:54:25 +00001924** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
drh79752b62016-08-13 10:02:17 +00001925** jump to address P2. Or if the SQLITE_STOREP2 flag is set in P5 store
1926** the result of comparison (0 or 1 or NULL) into register P2.
drhf5905aa2002-05-26 20:54:33 +00001927**
drh35573352008-01-08 23:54:25 +00001928** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
drh79752b62016-08-13 10:02:17 +00001929** reg(P3) is NULL then the take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001930** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001931**
drh35573352008-01-08 23:54:25 +00001932** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001933** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001934** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001935** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001936** affinity is used. Note that the affinity conversions are stored
1937** back into the input registers P1 and P3. So this opcode can cause
1938** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001939**
1940** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001941** the values are compared. If both values are blobs then memcmp() is
1942** used to determine the results of the comparison. If both values
1943** are text, then the appropriate collating function specified in
1944** P4 is used to do the comparison. If P4 is not specified then
1945** memcmp() is used to compare text string. If both values are
1946** numeric, then a numeric comparison is used. If the two values
1947** are of different types, then numbers are considered less than
1948** strings and strings are considered less than blobs.
drh5e00f6c2001-09-13 13:46:56 +00001949*/
drh9cbf3422008-01-17 16:22:13 +00001950/* Opcode: Le P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001951** Synopsis: IF r[P3]<=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001952**
drh35573352008-01-08 23:54:25 +00001953** This works just like the Lt opcode except that the jump is taken if
1954** the content of register P3 is less than or equal to the content of
1955** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001956*/
drh9cbf3422008-01-17 16:22:13 +00001957/* Opcode: Gt P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001958** Synopsis: IF r[P3]>r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001959**
drh35573352008-01-08 23:54:25 +00001960** This works just like the Lt opcode except that the jump is taken if
1961** the content of register P3 is greater than the content of
1962** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001963*/
drh9cbf3422008-01-17 16:22:13 +00001964/* Opcode: Ge P1 P2 P3 P4 P5
drh88e665f2016-08-27 01:41:53 +00001965** Synopsis: IF r[P3]>=r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001966**
drh35573352008-01-08 23:54:25 +00001967** This works just like the Lt opcode except that the jump is taken if
1968** the content of register P3 is greater than or equal to the content of
1969** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001970*/
drh9cbf3422008-01-17 16:22:13 +00001971case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1972case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1973case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1974case OP_Le: /* same as TK_LE, jump, in1, in3 */
1975case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1976case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh4910a762016-09-03 01:46:15 +00001977 int res, res2; /* Result of the comparison of pIn1 against pIn3 */
drh6a2fe092009-09-23 02:29:36 +00001978 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001979 u16 flags1; /* Copy of initial value of pIn1->flags */
1980 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001981
drh3c657212009-11-17 23:59:58 +00001982 pIn1 = &aMem[pOp->p1];
1983 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001984 flags1 = pIn1->flags;
1985 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001986 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001987 /* One or both operands are NULL */
1988 if( pOp->p5 & SQLITE_NULLEQ ){
1989 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1990 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1991 ** or not both operands are null.
1992 */
1993 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001994 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001995 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drhc3191d22016-10-18 16:36:15 +00001996 if( (flags1&flags3&MEM_Null)!=0
drh053a1282012-09-19 21:15:46 +00001997 && (flags3&MEM_Cleared)==0
1998 ){
drh4910a762016-09-03 01:46:15 +00001999 res = 0; /* Operands are equal */
drh053a1282012-09-19 21:15:46 +00002000 }else{
drh4910a762016-09-03 01:46:15 +00002001 res = 1; /* Operands are not equal */
drh053a1282012-09-19 21:15:46 +00002002 }
drh6a2fe092009-09-23 02:29:36 +00002003 }else{
2004 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
2005 ** then the result is always NULL.
2006 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
2007 */
drh688852a2014-02-17 22:40:43 +00002008 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002009 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002010 iCompare = 1; /* Operands are not equal */
danb1d6b532015-12-14 19:42:19 +00002011 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00002012 MemSetTypeFlag(pOut, MEM_Null);
2013 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002014 }else{
drhf4345e42014-02-18 11:31:59 +00002015 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00002016 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00002017 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002018 }
drh6a2fe092009-09-23 02:29:36 +00002019 }
2020 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002021 }
drh6a2fe092009-09-23 02:29:36 +00002022 }else{
2023 /* Neither operand is NULL. Do a comparison. */
2024 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002025 if( affinity>=SQLITE_AFF_NUMERIC ){
drh5fd0c122016-04-04 13:46:24 +00002026 if( (flags1 | flags3)&MEM_Str ){
2027 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2028 applyNumericAffinity(pIn1,0);
drh64caee42016-09-09 19:33:00 +00002029 testcase( flags3!=pIn3->flags ); /* Possible if pIn1==pIn3 */
drh4b37cd42016-06-25 11:43:47 +00002030 flags3 = pIn3->flags;
drh5fd0c122016-04-04 13:46:24 +00002031 }
2032 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
2033 applyNumericAffinity(pIn3,0);
2034 }
drh24a09622014-09-18 16:28:59 +00002035 }
drh64caee42016-09-09 19:33:00 +00002036 /* Handle the common case of integer comparison here, as an
2037 ** optimization, to avoid a call to sqlite3MemCompare() */
2038 if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
2039 if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
2040 if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
2041 res = 0;
2042 goto compare_op;
2043 }
drh24a09622014-09-18 16:28:59 +00002044 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00002045 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002046 testcase( pIn1->flags & MEM_Int );
2047 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002048 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002049 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2050 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh21e19b42016-09-15 14:54:51 +00002051 assert( pIn1!=pIn3 );
drh24a09622014-09-18 16:28:59 +00002052 }
drhe5520e22015-12-31 04:34:26 +00002053 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002054 testcase( pIn3->flags & MEM_Int );
2055 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002056 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002057 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2058 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002059 }
drh6a2fe092009-09-23 02:29:36 +00002060 }
drh6a2fe092009-09-23 02:29:36 +00002061 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh4910a762016-09-03 01:46:15 +00002062 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002063 }
drh64caee42016-09-09 19:33:00 +00002064compare_op:
danielk1977a37cdde2004-05-16 11:15:36 +00002065 switch( pOp->opcode ){
drh4910a762016-09-03 01:46:15 +00002066 case OP_Eq: res2 = res==0; break;
2067 case OP_Ne: res2 = res; break;
2068 case OP_Lt: res2 = res<0; break;
2069 case OP_Le: res2 = res<=0; break;
2070 case OP_Gt: res2 = res>0; break;
2071 default: res2 = res>=0; break;
danielk1977a37cdde2004-05-16 11:15:36 +00002072 }
2073
drhf56fa462015-04-13 21:39:54 +00002074 /* Undo any changes made by applyAffinity() to the input registers. */
2075 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2076 pIn1->flags = flags1;
2077 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2078 pIn3->flags = flags3;
2079
drh35573352008-01-08 23:54:25 +00002080 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002081 pOut = &aMem[pOp->p2];
drh4910a762016-09-03 01:46:15 +00002082 iCompare = res;
2083 res2 = res2!=0; /* For this path res2 must be exactly 0 or 1 */
drh3fffbf92016-09-05 15:02:41 +00002084 if( (pOp->p5 & SQLITE_KEEPNULL)!=0 ){
drh79752b62016-08-13 10:02:17 +00002085 /* The KEEPNULL flag prevents OP_Eq from overwriting a NULL with 1
drh3fffbf92016-09-05 15:02:41 +00002086 ** and prevents OP_Ne from overwriting NULL with 0. This flag
2087 ** is only used in contexts where either:
2088 ** (1) op==OP_Eq && (r[P2]==NULL || r[P2]==0)
2089 ** (2) op==OP_Ne && (r[P2]==NULL || r[P2]==1)
2090 ** Therefore it is not necessary to check the content of r[P2] for
2091 ** NULL. */
drh79752b62016-08-13 10:02:17 +00002092 assert( pOp->opcode==OP_Ne || pOp->opcode==OP_Eq );
drh4910a762016-09-03 01:46:15 +00002093 assert( res2==0 || res2==1 );
drh3fffbf92016-09-05 15:02:41 +00002094 testcase( res2==0 && pOp->opcode==OP_Eq );
2095 testcase( res2==1 && pOp->opcode==OP_Eq );
2096 testcase( res2==0 && pOp->opcode==OP_Ne );
2097 testcase( res2==1 && pOp->opcode==OP_Ne );
drh4910a762016-09-03 01:46:15 +00002098 if( (pOp->opcode==OP_Eq)==res2 ) break;
drh79752b62016-08-13 10:02:17 +00002099 }
drh2b4ded92010-09-27 21:09:31 +00002100 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002101 MemSetTypeFlag(pOut, MEM_Int);
drh4910a762016-09-03 01:46:15 +00002102 pOut->u.i = res2;
drh35573352008-01-08 23:54:25 +00002103 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002104 }else{
drhf4345e42014-02-18 11:31:59 +00002105 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh4910a762016-09-03 01:46:15 +00002106 if( res2 ){
drhf56fa462015-04-13 21:39:54 +00002107 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002108 }
danielk1977a37cdde2004-05-16 11:15:36 +00002109 }
2110 break;
2111}
drhc9b84a12002-06-20 11:36:48 +00002112
drh79752b62016-08-13 10:02:17 +00002113/* Opcode: ElseNotEq * P2 * * *
2114**
drhfd7459e2016-09-17 17:39:01 +00002115** This opcode must immediately follow an OP_Lt or OP_Gt comparison operator.
2116** If result of an OP_Eq comparison on the same two operands
2117** would have be NULL or false (0), then then jump to P2.
2118** If the result of an OP_Eq comparison on the two previous operands
2119** would have been true (1), then fall through.
drh79752b62016-08-13 10:02:17 +00002120*/
2121case OP_ElseNotEq: { /* same as TK_ESCAPE, jump */
2122 assert( pOp>aOp );
2123 assert( pOp[-1].opcode==OP_Lt || pOp[-1].opcode==OP_Gt );
drh4910a762016-09-03 01:46:15 +00002124 assert( pOp[-1].p5 & SQLITE_STOREP2 );
drh0f825a72016-08-13 14:17:02 +00002125 VdbeBranchTaken(iCompare!=0, 2);
2126 if( iCompare!=0 ) goto jump_to_p2;
drh79752b62016-08-13 10:02:17 +00002127 break;
2128}
2129
2130
drh0acb7e42008-06-25 00:12:41 +00002131/* Opcode: Permutation * * * P4 *
2132**
drhb7dab702017-01-26 18:00:00 +00002133** Set the permutation used by the OP_Compare operator in the next
2134** instruction. The permutation is stored in the P4 operand.
drh0acb7e42008-06-25 00:12:41 +00002135**
drh953f7612012-12-07 22:18:54 +00002136** The permutation is only valid until the next OP_Compare that has
2137** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2138** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002139**
2140** The first integer in the P4 integer array is the length of the array
2141** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002142*/
2143case OP_Permutation: {
2144 assert( pOp->p4type==P4_INTARRAY );
2145 assert( pOp->p4.ai );
drhb7dab702017-01-26 18:00:00 +00002146 assert( pOp[1].opcode==OP_Compare );
2147 assert( pOp[1].p5 & OPFLAG_PERMUTE );
drh0acb7e42008-06-25 00:12:41 +00002148 break;
2149}
2150
drh953f7612012-12-07 22:18:54 +00002151/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002152** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002153**
drh710c4842010-08-30 01:17:20 +00002154** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2155** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002156** the comparison for use by the next OP_Jump instruct.
2157**
drh0ca10df2012-12-08 13:26:23 +00002158** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2159** determined by the most recent OP_Permutation operator. If the
2160** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2161** order.
2162**
drh0acb7e42008-06-25 00:12:41 +00002163** P4 is a KeyInfo structure that defines collating sequences and sort
2164** orders for the comparison. The permutation applies to registers
2165** only. The KeyInfo elements are used sequentially.
2166**
2167** The comparison is a sort comparison, so NULLs compare equal,
2168** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002169** and strings are less than blobs.
2170*/
2171case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002172 int n;
2173 int i;
2174 int p1;
2175 int p2;
2176 const KeyInfo *pKeyInfo;
2177 int idx;
2178 CollSeq *pColl; /* Collating sequence to use on this term */
2179 int bRev; /* True for DESCENDING sort order */
drhb7dab702017-01-26 18:00:00 +00002180 int *aPermute; /* The permutation */
drh856c1032009-06-02 15:21:42 +00002181
drhb7dab702017-01-26 18:00:00 +00002182 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ){
2183 aPermute = 0;
2184 }else{
2185 assert( pOp>aOp );
2186 assert( pOp[-1].opcode==OP_Permutation );
2187 assert( pOp[-1].p4type==P4_INTARRAY );
2188 aPermute = pOp[-1].p4.ai + 1;
2189 assert( aPermute!=0 );
2190 }
drh856c1032009-06-02 15:21:42 +00002191 n = pOp->p3;
2192 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002193 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002194 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002195 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002196 p2 = pOp->p2;
drhd879e3e2017-02-13 13:35:55 +00002197#ifdef SQLITE_DEBUG
drh6a2fe092009-09-23 02:29:36 +00002198 if( aPermute ){
2199 int k, mx = 0;
2200 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
drh9f6168b2016-03-19 23:32:58 +00002201 assert( p1>0 && p1+mx<=(p->nMem+1 - p->nCursor)+1 );
2202 assert( p2>0 && p2+mx<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002203 }else{
drh9f6168b2016-03-19 23:32:58 +00002204 assert( p1>0 && p1+n<=(p->nMem+1 - p->nCursor)+1 );
2205 assert( p2>0 && p2+n<=(p->nMem+1 - p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002206 }
2207#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002208 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002209 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002210 assert( memIsValid(&aMem[p1+idx]) );
2211 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002212 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2213 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002214 assert( i<pKeyInfo->nField );
2215 pColl = pKeyInfo->aColl[i];
2216 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002217 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002218 if( iCompare ){
2219 if( bRev ) iCompare = -iCompare;
2220 break;
2221 }
drh16ee60f2008-06-20 18:13:25 +00002222 }
2223 break;
2224}
2225
2226/* Opcode: Jump P1 P2 P3 * *
2227**
2228** Jump to the instruction at address P1, P2, or P3 depending on whether
2229** in the most recent OP_Compare instruction the P1 vector was less than
2230** equal to, or greater than the P2 vector, respectively.
2231*/
drh0acb7e42008-06-25 00:12:41 +00002232case OP_Jump: { /* jump */
2233 if( iCompare<0 ){
drhf56fa462015-04-13 21:39:54 +00002234 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002235 }else if( iCompare==0 ){
drhf56fa462015-04-13 21:39:54 +00002236 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002237 }else{
drhf56fa462015-04-13 21:39:54 +00002238 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002239 }
2240 break;
2241}
2242
drh5b6afba2008-01-05 16:29:28 +00002243/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002244** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002245**
drh5b6afba2008-01-05 16:29:28 +00002246** Take the logical AND of the values in registers P1 and P2 and
2247** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002248**
drh5b6afba2008-01-05 16:29:28 +00002249** If either P1 or P2 is 0 (false) then the result is 0 even if
2250** the other input is NULL. A NULL and true or two NULLs give
2251** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002252*/
drh5b6afba2008-01-05 16:29:28 +00002253/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002254** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002255**
2256** Take the logical OR of the values in register P1 and P2 and
2257** store the answer in register P3.
2258**
2259** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2260** even if the other input is NULL. A NULL and false or two NULLs
2261** give a NULL output.
2262*/
2263case OP_And: /* same as TK_AND, in1, in2, out3 */
2264case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002265 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2266 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002267
drh3c657212009-11-17 23:59:58 +00002268 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002269 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002270 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002271 }else{
drh5b6afba2008-01-05 16:29:28 +00002272 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002273 }
drh3c657212009-11-17 23:59:58 +00002274 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002275 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002276 v2 = 2;
2277 }else{
drh5b6afba2008-01-05 16:29:28 +00002278 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002279 }
2280 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002281 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002282 v1 = and_logic[v1*3+v2];
2283 }else{
drh5b6afba2008-01-05 16:29:28 +00002284 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002285 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002286 }
drh3c657212009-11-17 23:59:58 +00002287 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002288 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002289 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002290 }else{
drh5b6afba2008-01-05 16:29:28 +00002291 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002292 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002293 }
drh5e00f6c2001-09-13 13:46:56 +00002294 break;
2295}
2296
drhe99fa2a2008-12-15 15:27:51 +00002297/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002298** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002299**
drhe99fa2a2008-12-15 15:27:51 +00002300** Interpret the value in register P1 as a boolean value. Store the
2301** boolean complement in register P2. If the value in register P1 is
2302** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002303*/
drh93952eb2009-11-13 19:43:43 +00002304case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002305 pIn1 = &aMem[pOp->p1];
2306 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002307 sqlite3VdbeMemSetNull(pOut);
2308 if( (pIn1->flags & MEM_Null)==0 ){
2309 pOut->flags = MEM_Int;
2310 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002311 }
drh5e00f6c2001-09-13 13:46:56 +00002312 break;
2313}
2314
drhe99fa2a2008-12-15 15:27:51 +00002315/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002316** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002317**
drhe99fa2a2008-12-15 15:27:51 +00002318** Interpret the content of register P1 as an integer. Store the
2319** ones-complement of the P1 value into register P2. If P1 holds
2320** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002321*/
drh93952eb2009-11-13 19:43:43 +00002322case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002323 pIn1 = &aMem[pOp->p1];
2324 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002325 sqlite3VdbeMemSetNull(pOut);
2326 if( (pIn1->flags & MEM_Null)==0 ){
2327 pOut->flags = MEM_Int;
2328 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002329 }
drhbf4133c2001-10-13 02:59:08 +00002330 break;
2331}
2332
drh48f2d3b2011-09-16 01:34:43 +00002333/* Opcode: Once P1 P2 * * *
2334**
drhab087d42017-03-24 17:59:56 +00002335** Fall through to the next instruction the first time this opcode is
2336** encountered on each invocation of the byte-code program. Jump to P2
2337** on the second and all subsequent encounters during the same invocation.
2338**
2339** Top-level programs determine first invocation by comparing the P1
2340** operand against the P1 operand on the OP_Init opcode at the beginning
2341** of the program. If the P1 values differ, then fall through and make
2342** the P1 of this opcode equal to the P1 of OP_Init. If P1 values are
2343** the same then take the jump.
2344**
2345** For subprograms, there is a bitmask in the VdbeFrame that determines
2346** whether or not the jump should be taken. The bitmask is necessary
2347** because the self-altering code trick does not work for recursive
2348** triggers.
drh48f2d3b2011-09-16 01:34:43 +00002349*/
dan1d8cb212011-12-09 13:24:16 +00002350case OP_Once: { /* jump */
drhab087d42017-03-24 17:59:56 +00002351 u32 iAddr; /* Address of this instruction */
drh9e5eb9c2016-09-18 16:08:10 +00002352 assert( p->aOp[0].opcode==OP_Init );
drhab087d42017-03-24 17:59:56 +00002353 if( p->pFrame ){
2354 iAddr = (int)(pOp - p->aOp);
2355 if( (p->pFrame->aOnce[iAddr/8] & (1<<(iAddr & 7)))!=0 ){
2356 VdbeBranchTaken(1, 2);
drhab087d42017-03-24 17:59:56 +00002357 goto jump_to_p2;
2358 }
drh18333ef2017-03-24 18:38:41 +00002359 p->pFrame->aOnce[iAddr/8] |= 1<<(iAddr & 7);
dan1d8cb212011-12-09 13:24:16 +00002360 }else{
drhab087d42017-03-24 17:59:56 +00002361 if( p->aOp[0].p1==pOp->p1 ){
2362 VdbeBranchTaken(1, 2);
2363 goto jump_to_p2;
2364 }
dan1d8cb212011-12-09 13:24:16 +00002365 }
drhab087d42017-03-24 17:59:56 +00002366 VdbeBranchTaken(0, 2);
2367 pOp->p1 = p->aOp[0].p1;
dan1d8cb212011-12-09 13:24:16 +00002368 break;
2369}
2370
drh3c84ddf2008-01-09 02:15:38 +00002371/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002372**
drhef8662b2011-06-20 21:47:58 +00002373** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002374** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002375** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002376*/
drh3c84ddf2008-01-09 02:15:38 +00002377/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002378**
drhef8662b2011-06-20 21:47:58 +00002379** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002380** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002381** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002382*/
drh9cbf3422008-01-17 16:22:13 +00002383case OP_If: /* jump, in1 */
2384case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002385 int c;
drh3c657212009-11-17 23:59:58 +00002386 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002387 if( pIn1->flags & MEM_Null ){
2388 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002389 }else{
drhba0232a2005-06-06 17:27:19 +00002390#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002391 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002392#else
drh3c84ddf2008-01-09 02:15:38 +00002393 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002394#endif
drhf5905aa2002-05-26 20:54:33 +00002395 if( pOp->opcode==OP_IfNot ) c = !c;
2396 }
drh688852a2014-02-17 22:40:43 +00002397 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002398 if( c ){
drhf56fa462015-04-13 21:39:54 +00002399 goto jump_to_p2;
drh3c84ddf2008-01-09 02:15:38 +00002400 }
drh5e00f6c2001-09-13 13:46:56 +00002401 break;
2402}
2403
drh830ecf92009-06-18 00:41:55 +00002404/* Opcode: IsNull P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00002405** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002406**
drh830ecf92009-06-18 00:41:55 +00002407** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002408*/
drh9cbf3422008-01-17 16:22:13 +00002409case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002410 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002411 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002412 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002413 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002414 }
drh477df4b2008-01-05 18:48:24 +00002415 break;
2416}
2417
drh98757152008-01-09 23:04:12 +00002418/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002419** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002420**
drh6a288a32008-01-07 19:20:24 +00002421** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002422*/
drh9cbf3422008-01-17 16:22:13 +00002423case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002424 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002425 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002426 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002427 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002428 }
drh5e00f6c2001-09-13 13:46:56 +00002429 break;
2430}
2431
drh3e9ca092009-09-08 01:14:48 +00002432/* Opcode: Column P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00002433** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002434**
danielk1977cfcdaef2004-05-12 07:33:33 +00002435** Interpret the data that cursor P1 points to as a structure built using
2436** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002437** information about the format of the data.) Extract the P2-th column
2438** from this record. If there are less that (P2+1)
2439** values in the record, extract a NULL.
2440**
drh9cbf3422008-01-17 16:22:13 +00002441** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002442**
drh1cc3a362017-04-03 13:17:31 +00002443** If the record contains fewer than P2 fields, then extract a NULL. Or,
danielk19771f4aa332008-01-03 09:51:55 +00002444** if the P4 argument is a P4_MEM use the value of the P4 argument as
2445** the result.
drh3e9ca092009-09-08 01:14:48 +00002446**
2447** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2448** then the cache of the cursor is reset prior to extracting the column.
2449** The first OP_Column against a pseudo-table after the value of the content
2450** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002451**
drh1cc3a362017-04-03 13:17:31 +00002452** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 then
drhdda5c082012-03-28 13:41:10 +00002453** the result is guaranteed to only be used as the argument of a length()
2454** or typeof() function, respectively. The loading of large blobs can be
2455** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002456*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002457case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002458 int p2; /* column number to retrieve */
2459 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002460 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002461 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002462 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002463 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002464 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002465 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002466 const u8 *zData; /* Part of the record being decoded */
2467 const u8 *zHdr; /* Next unparsed byte of the header */
2468 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002469 u32 offset; /* Offset into the data */
drhc6ce38832015-10-15 21:30:24 +00002470 u64 offset64; /* 64-bit offset */
drh501932c2013-11-21 21:59:53 +00002471 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002472 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002473 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002474
dande892d92016-01-29 19:29:45 +00002475 pC = p->apCsr[pOp->p1];
drh856c1032009-06-02 15:21:42 +00002476 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002477
2478 /* If the cursor cache is stale, bring it up-to-date */
2479 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
drh4ca239f2016-05-19 11:12:43 +00002480 if( rc ) goto abort_due_to_error;
dande892d92016-01-29 19:29:45 +00002481
drh9f6168b2016-03-19 23:32:58 +00002482 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002483 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002484 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002485 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
danielk19776c924092007-11-12 08:09:34 +00002486 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002487 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002488 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002489 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002490 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2491 assert( pC->eCurType!=CURTYPE_SORTER );
drh399af1d2013-11-20 17:25:55 +00002492
drha43a02e2016-05-19 17:51:19 +00002493 if( pC->cacheStatus!=p->cacheCtr ){ /*OPTIMIZATION-IF-FALSE*/
danielk1977192ac1d2004-05-10 07:17:30 +00002494 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002495 if( pC->eCurType==CURTYPE_PSEUDO ){
2496 assert( pC->uc.pseudoTableReg>0 );
2497 pReg = &aMem[pC->uc.pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002498 assert( pReg->flags & MEM_Blob );
2499 assert( memIsValid(pReg) );
2500 pC->payloadSize = pC->szRow = avail = pReg->n;
2501 pC->aRow = (u8*)pReg->z;
2502 }else{
drh6b5631e2014-11-05 15:57:39 +00002503 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002504 goto op_column_out;
2505 }
danielk1977192ac1d2004-05-10 07:17:30 +00002506 }else{
drh06a09a82016-11-25 17:03:03 +00002507 pCrsr = pC->uc.pCursor;
drhc960dcb2015-11-20 19:22:01 +00002508 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002509 assert( pCrsr );
drha7c90c42016-06-04 20:37:10 +00002510 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2511 pC->payloadSize = sqlite3BtreePayloadSize(pCrsr);
2512 pC->aRow = sqlite3BtreePayloadFetch(pCrsr, &avail);
drh399af1d2013-11-20 17:25:55 +00002513 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2514 if( pC->payloadSize <= (u32)avail ){
2515 pC->szRow = pC->payloadSize;
drh5f7dacb2015-11-20 13:33:56 +00002516 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2517 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002518 }else{
drh399af1d2013-11-20 17:25:55 +00002519 pC->szRow = avail;
2520 }
danielk1977192ac1d2004-05-10 07:17:30 +00002521 }
drhd3194f52004-05-27 19:59:32 +00002522 pC->cacheStatus = p->cacheCtr;
drh399af1d2013-11-20 17:25:55 +00002523 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2524 pC->nHdrParsed = 0;
2525 aOffset[0] = offset;
drh35cd6432009-06-05 14:17:21 +00002526
drhc81aa2e2014-10-11 23:31:52 +00002527
drha43a02e2016-05-19 17:51:19 +00002528 if( avail<offset ){ /*OPTIMIZATION-IF-FALSE*/
drhc81aa2e2014-10-11 23:31:52 +00002529 /* pC->aRow does not have to hold the entire row, but it does at least
2530 ** need to cover the header of the record. If pC->aRow does not contain
2531 ** the complete header, then set it to zero, forcing the header to be
2532 ** dynamically allocated. */
2533 pC->aRow = 0;
2534 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002535
2536 /* Make sure a corrupt database has not given us an oversize header.
2537 ** Do this now to avoid an oversize memory allocation.
2538 **
2539 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2540 ** types use so much data space that there can only be 4096 and 32 of
2541 ** them, respectively. So the maximum header length results from a
2542 ** 3-byte type for each of the maximum of 32768 columns plus three
2543 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2544 */
2545 if( offset > 98307 || offset > pC->payloadSize ){
2546 rc = SQLITE_CORRUPT_BKPT;
drh9467abf2016-02-17 18:44:11 +00002547 goto abort_due_to_error;
drh848a3322015-10-16 12:53:47 +00002548 }
drh0eda6cd2016-05-19 16:58:42 +00002549 }else if( offset>0 ){ /*OPTIMIZATION-IF-TRUE*/
2550 /* The following goto is an optimization. It can be omitted and
2551 ** everything will still work. But OP_Column is measurably faster
2552 ** by skipping the subsequent conditional, which is always true.
2553 */
2554 zData = pC->aRow;
2555 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2556 goto op_column_read_header;
drhc81aa2e2014-10-11 23:31:52 +00002557 }
drh399af1d2013-11-20 17:25:55 +00002558 }
drh35cd6432009-06-05 14:17:21 +00002559
drh399af1d2013-11-20 17:25:55 +00002560 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002561 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002562 */
drhc8606e42013-11-20 19:28:03 +00002563 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002564 /* If there is more header available for parsing in the record, try
2565 ** to extract additional fields up through the p2+1-th field
drh35cd6432009-06-05 14:17:21 +00002566 */
drhc8606e42013-11-20 19:28:03 +00002567 if( pC->iHdrOffset<aOffset[0] ){
2568 /* Make sure zData points to enough of the record to cover the header. */
2569 if( pC->aRow==0 ){
2570 memset(&sMem, 0, sizeof(sMem));
drhcb3cabd2016-11-25 19:18:28 +00002571 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, 0, aOffset[0], &sMem);
drh9467abf2016-02-17 18:44:11 +00002572 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002573 zData = (u8*)sMem.z;
2574 }else{
2575 zData = pC->aRow;
drh9188b382004-05-14 21:12:22 +00002576 }
drhc8606e42013-11-20 19:28:03 +00002577
drh0c8f7602014-09-19 16:56:45 +00002578 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drh0eda6cd2016-05-19 16:58:42 +00002579 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002580 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002581 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002582 zHdr = zData + pC->iHdrOffset;
2583 zEndHdr = zData + aOffset[0];
drhc8606e42013-11-20 19:28:03 +00002584 do{
drh95fa6062015-10-16 13:50:08 +00002585 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002586 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002587 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002588 }else{
drhc8606e42013-11-20 19:28:03 +00002589 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002590 offset64 += sqlite3VdbeSerialTypeLen(t);
drh5a077b72011-08-29 02:16:18 +00002591 }
drhfaf37272015-10-16 14:23:42 +00002592 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002593 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002594 }while( i<=p2 && zHdr<zEndHdr );
drh170c2762016-05-20 21:40:11 +00002595
drh8dd83622014-10-13 23:39:02 +00002596 /* The record is corrupt if any of the following are true:
2597 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002598 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002599 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002600 */
drhc6ce38832015-10-15 21:30:24 +00002601 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2602 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002603 ){
drhddb2b4a2016-03-25 12:10:32 +00002604 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drhc8606e42013-11-20 19:28:03 +00002605 rc = SQLITE_CORRUPT_BKPT;
drh9467abf2016-02-17 18:44:11 +00002606 goto abort_due_to_error;
danielk1977dedf45b2006-01-13 17:12:01 +00002607 }
drhddb2b4a2016-03-25 12:10:32 +00002608
drh170c2762016-05-20 21:40:11 +00002609 pC->nHdrParsed = i;
2610 pC->iHdrOffset = (u32)(zHdr - zData);
2611 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
mistachkin8c7cd6a2015-12-16 21:09:53 +00002612 }else{
drh9fbc8852016-01-04 03:48:46 +00002613 t = 0;
drh9188b382004-05-14 21:12:22 +00002614 }
drhd3194f52004-05-27 19:59:32 +00002615
drhf2db3382015-04-30 20:33:25 +00002616 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002617 ** still not up to p2, that means that the record has fewer than p2
2618 ** columns. So the result will be either the default value or a NULL.
drhd3194f52004-05-27 19:59:32 +00002619 */
drhc8606e42013-11-20 19:28:03 +00002620 if( pC->nHdrParsed<=p2 ){
2621 if( pOp->p4type==P4_MEM ){
2622 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2623 }else{
drh22e8d832014-10-29 00:58:38 +00002624 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002625 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002626 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002627 }
drh95fa6062015-10-16 13:50:08 +00002628 }else{
2629 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002630 }
danielk1977192ac1d2004-05-10 07:17:30 +00002631
drh380d6852013-11-20 20:58:00 +00002632 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002633 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002634 ** all valid.
drh9188b382004-05-14 21:12:22 +00002635 */
drhc8606e42013-11-20 19:28:03 +00002636 assert( p2<pC->nHdrParsed );
2637 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002638 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drha1851ef2016-05-20 19:51:28 +00002639 if( VdbeMemDynamic(pDest) ){
2640 sqlite3VdbeMemSetNull(pDest);
2641 }
drh95fa6062015-10-16 13:50:08 +00002642 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002643 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002644 /* This is the common case where the desired content fits on the original
2645 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002646 zData = pC->aRow + aOffset[p2];
2647 if( t<12 ){
2648 sqlite3VdbeSerialGet(zData, t, pDest);
2649 }else{
2650 /* If the column value is a string, we need a persistent value, not
2651 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2652 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2653 */
2654 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2655 pDest->n = len = (t-12)/2;
drha1851ef2016-05-20 19:51:28 +00002656 pDest->enc = encoding;
drh69f6e252016-01-11 18:05:00 +00002657 if( pDest->szMalloc < len+2 ){
2658 pDest->flags = MEM_Null;
2659 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2660 }else{
2661 pDest->z = pDest->zMalloc;
2662 }
2663 memcpy(pDest->z, zData, len);
2664 pDest->z[len] = 0;
2665 pDest->z[len+1] = 0;
2666 pDest->flags = aFlag[t&1];
2667 }
danielk197736963fd2005-02-19 08:18:05 +00002668 }else{
drha1851ef2016-05-20 19:51:28 +00002669 pDest->enc = encoding;
drh58c96082013-12-23 11:33:32 +00002670 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002671 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2672 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2673 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002674 ){
drh2a2a6962014-09-16 18:22:44 +00002675 /* Content is irrelevant for
2676 ** 1. the typeof() function,
2677 ** 2. the length(X) function if X is a blob, and
2678 ** 3. if the content length is zero.
2679 ** So we might as well use bogus content rather than reading
dan1f9144e2017-03-17 13:59:06 +00002680 ** content from disk.
2681 **
2682 ** Although sqlite3VdbeSerialGet() may read at most 8 bytes from the
2683 ** buffer passed to it, debugging function VdbeMemPrettyPrint() may
2684 ** read up to 16. So 16 bytes of bogus content is supplied.
2685 */
2686 static u8 aZero[16]; /* This is the bogus content */
drh69f6e252016-01-11 18:05:00 +00002687 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002688 }else{
drhcb3cabd2016-11-25 19:18:28 +00002689 rc = sqlite3VdbeMemFromBtree(pC->uc.pCursor, aOffset[p2], len, pDest);
drh9467abf2016-02-17 18:44:11 +00002690 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2691 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2692 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002693 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002694 }
drhd3194f52004-05-27 19:59:32 +00002695
danielk19773c9cc8d2005-01-17 03:40:08 +00002696op_column_out:
drhb7654112008-01-12 12:48:07 +00002697 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002698 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002699 break;
2700}
2701
danielk1977751de562008-04-18 09:01:15 +00002702/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002703** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002704**
2705** Apply affinities to a range of P2 registers starting with P1.
2706**
2707** P4 is a string that is P2 characters long. The nth character of the
2708** string indicates the column affinity that should be used for the nth
2709** memory cell in the range.
2710*/
2711case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002712 const char *zAffinity; /* The affinity to be applied */
danielk1977751de562008-04-18 09:01:15 +00002713
drh856c1032009-06-02 15:21:42 +00002714 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002715 assert( zAffinity!=0 );
drh662c50e2017-04-01 20:14:01 +00002716 assert( pOp->p2>0 );
drh039fc322009-11-17 18:31:47 +00002717 assert( zAffinity[pOp->p2]==0 );
2718 pIn1 = &aMem[pOp->p1];
drh662c50e2017-04-01 20:14:01 +00002719 do{
drh9f6168b2016-03-19 23:32:58 +00002720 assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002721 assert( memIsValid(pIn1) );
drh662c50e2017-04-01 20:14:01 +00002722 applyAffinity(pIn1, *(zAffinity++), encoding);
drh039fc322009-11-17 18:31:47 +00002723 pIn1++;
drh662c50e2017-04-01 20:14:01 +00002724 }while( zAffinity[0] );
danielk1977751de562008-04-18 09:01:15 +00002725 break;
2726}
2727
drh1db639c2008-01-17 02:36:28 +00002728/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002729** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002730**
drh710c4842010-08-30 01:17:20 +00002731** Convert P2 registers beginning with P1 into the [record format]
2732** use as a data record in a database table or as a key
2733** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002734**
danielk1977751de562008-04-18 09:01:15 +00002735** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002736** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002737** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002738**
drh8a512562005-11-14 22:29:05 +00002739** The mapping from character to affinity is given by the SQLITE_AFF_
2740** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002741**
drh05883a32015-06-02 15:32:08 +00002742** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002743*/
drh1db639c2008-01-17 02:36:28 +00002744case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002745 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2746 Mem *pRec; /* The new record */
2747 u64 nData; /* Number of bytes of data space */
2748 int nHdr; /* Number of bytes of header space */
2749 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002750 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002751 int nVarint; /* Number of bytes in a varint */
2752 u32 serial_type; /* Type field */
2753 Mem *pData0; /* First field to be combined into the record */
2754 Mem *pLast; /* Last field of the record */
2755 int nField; /* Number of fields in the record */
2756 char *zAffinity; /* The affinity string for the record */
2757 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002758 int i; /* Space used in zNewRecord[] header */
2759 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002760 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002761
drhf3218fe2004-05-28 08:21:02 +00002762 /* Assuming the record contains N fields, the record format looks
2763 ** like this:
2764 **
drh7a224de2004-06-02 01:22:02 +00002765 ** ------------------------------------------------------------------------
2766 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2767 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002768 **
drh9cbf3422008-01-17 16:22:13 +00002769 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002770 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002771 **
2772 ** Each type field is a varint representing the serial type of the
2773 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002774 ** hdr-size field is also a varint which is the offset from the beginning
2775 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002776 */
drh856c1032009-06-02 15:21:42 +00002777 nData = 0; /* Number of bytes of data space */
2778 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002779 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002780 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002781 zAffinity = pOp->p4.z;
drh9f6168b2016-03-19 23:32:58 +00002782 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem+1 - p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002783 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002784 nField = pOp->p2;
2785 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002786 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002787
drh2b4ded92010-09-27 21:09:31 +00002788 /* Identify the output register */
2789 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2790 pOut = &aMem[pOp->p3];
2791 memAboutToChange(p, pOut);
2792
drh3e6c0602013-12-10 20:53:01 +00002793 /* Apply the requested affinity to all inputs
2794 */
2795 assert( pData0<=pLast );
2796 if( zAffinity ){
2797 pRec = pData0;
2798 do{
drh57bf4a82014-02-17 14:59:22 +00002799 applyAffinity(pRec++, *(zAffinity++), encoding);
2800 assert( zAffinity[0]==0 || pRec<=pLast );
2801 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002802 }
2803
drhd447dce2017-01-25 20:55:11 +00002804#ifdef SQLITE_ENABLE_NULL_TRIM
drh585ce192017-01-25 14:58:27 +00002805 /* NULLs can be safely trimmed from the end of the record, as long as
2806 ** as the schema format is 2 or more and none of the omitted columns
2807 ** have a non-NULL default value. Also, the record must be left with
2808 ** at least one field. If P5>0 then it will be one more than the
2809 ** index of the right-most column with a non-NULL default value */
2810 if( pOp->p5 ){
2811 while( (pLast->flags & MEM_Null)!=0 && nField>pOp->p5 ){
2812 pLast--;
2813 nField--;
2814 }
2815 }
drhd447dce2017-01-25 20:55:11 +00002816#endif
drh585ce192017-01-25 14:58:27 +00002817
drhf3218fe2004-05-28 08:21:02 +00002818 /* Loop through the elements that will make up the record to figure
2819 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002820 */
drh038b7bc2013-12-09 23:17:22 +00002821 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002822 do{
drh2b4ded92010-09-27 21:09:31 +00002823 assert( memIsValid(pRec) );
drhbe37c122015-10-16 14:54:17 +00002824 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002825 if( pRec->flags & MEM_Zero ){
drh038b7bc2013-12-09 23:17:22 +00002826 if( nData ){
drh53e66c32015-07-24 15:49:23 +00002827 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002828 }else{
2829 nZero += pRec->u.nZero;
2830 len -= pRec->u.nZero;
2831 }
drhfdf972a2007-05-02 13:30:27 +00002832 }
drh8079a0d2006-01-12 17:20:50 +00002833 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002834 testcase( serial_type==127 );
2835 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002836 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh45c3c662016-04-07 14:16:16 +00002837 if( pRec==pData0 ) break;
2838 pRec--;
2839 }while(1);
danielk19773d1bfea2004-05-14 11:00:53 +00002840
drh654858d2014-11-20 02:18:14 +00002841 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2842 ** which determines the total number of bytes in the header. The varint
2843 ** value is the size of the header in bytes including the size varint
2844 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002845 testcase( nHdr==126 );
2846 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002847 if( nHdr<=126 ){
2848 /* The common case */
2849 nHdr += 1;
2850 }else{
2851 /* Rare case of a really large header */
2852 nVarint = sqlite3VarintLen(nHdr);
2853 nHdr += nVarint;
2854 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002855 }
drh038b7bc2013-12-09 23:17:22 +00002856 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002857 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002858 goto too_big;
2859 }
drhf3218fe2004-05-28 08:21:02 +00002860
danielk1977a7a8e142008-02-13 18:25:27 +00002861 /* Make sure the output register has a buffer large enough to store
2862 ** the new record. The output register (pOp->p3) is not allowed to
2863 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002864 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002865 */
drh322f2852014-09-19 00:43:39 +00002866 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002867 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002868 }
danielk1977a7a8e142008-02-13 18:25:27 +00002869 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002870
2871 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002872 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002873 j = nHdr;
2874 assert( pData0<=pLast );
2875 pRec = pData0;
2876 do{
drhfacf47a2014-10-13 20:12:47 +00002877 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002878 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2879 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002880 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002881 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2882 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002883 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002884 }while( (++pRec)<=pLast );
2885 assert( i==nHdr );
2886 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002887
drh9f6168b2016-03-19 23:32:58 +00002888 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002889 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002890 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002891 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002892 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002893 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002894 }
drh1013c932008-01-06 00:25:21 +00002895 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002896 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002897 break;
2898}
2899
danielk1977a5533162009-02-24 10:01:51 +00002900/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002901** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002902**
2903** Store the number of entries (an integer value) in the table or index
2904** opened by cursor P1 in register P2
2905*/
2906#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002907case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002908 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002909 BtCursor *pCrsr;
2910
drhc960dcb2015-11-20 19:22:01 +00002911 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2912 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002913 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002914 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002915 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002916 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002917 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002918 pOut->u.i = nEntry;
2919 break;
2920}
2921#endif
2922
danielk1977fd7f0452008-12-17 17:30:26 +00002923/* Opcode: Savepoint P1 * * P4 *
2924**
2925** Open, release or rollback the savepoint named by parameter P4, depending
2926** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2927** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2928*/
2929case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002930 int p1; /* Value of P1 operand */
2931 char *zName; /* Name of savepoint */
2932 int nName;
2933 Savepoint *pNew;
2934 Savepoint *pSavepoint;
2935 Savepoint *pTmp;
2936 int iSavepoint;
2937 int ii;
2938
2939 p1 = pOp->p1;
2940 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002941
2942 /* Assert that the p1 parameter is valid. Also that if there is no open
2943 ** transaction, then there cannot be any savepoints.
2944 */
2945 assert( db->pSavepoint==0 || db->autoCommit==0 );
2946 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2947 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2948 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002949 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002950
2951 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002952 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002953 /* A new savepoint cannot be created if there are active write
2954 ** statements (i.e. open read/write incremental blob handles).
2955 */
drh22c17b82015-05-15 04:13:15 +00002956 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002957 rc = SQLITE_BUSY;
2958 }else{
drh856c1032009-06-02 15:21:42 +00002959 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002960
drhbe07ec52011-06-03 12:15:26 +00002961#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002962 /* This call is Ok even if this savepoint is actually a transaction
2963 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2964 ** If this is a transaction savepoint being opened, it is guaranteed
2965 ** that the db->aVTrans[] array is empty. */
2966 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002967 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2968 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002969 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002970#endif
dand9495cd2011-04-27 12:08:04 +00002971
danielk1977fd7f0452008-12-17 17:30:26 +00002972 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00002973 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00002974 if( pNew ){
2975 pNew->zName = (char *)&pNew[1];
2976 memcpy(pNew->zName, zName, nName+1);
2977
2978 /* If there is no open transaction, then mark this as a special
2979 ** "transaction savepoint". */
2980 if( db->autoCommit ){
2981 db->autoCommit = 0;
2982 db->isTransactionSavepoint = 1;
2983 }else{
2984 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002985 }
dan21e8d012011-03-03 20:05:59 +00002986
danielk1977fd7f0452008-12-17 17:30:26 +00002987 /* Link the new savepoint into the database handle's list. */
2988 pNew->pNext = db->pSavepoint;
2989 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002990 pNew->nDeferredCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00002991 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002992 }
2993 }
2994 }else{
drh856c1032009-06-02 15:21:42 +00002995 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002996
2997 /* Find the named savepoint. If there is no such savepoint, then an
2998 ** an error is returned to the user. */
2999 for(
drh856c1032009-06-02 15:21:42 +00003000 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003001 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00003002 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00003003 ){
3004 iSavepoint++;
3005 }
3006 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00003007 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00003008 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00003009 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00003010 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00003011 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00003012 */
drh22c17b82015-05-15 04:13:15 +00003013 sqlite3VdbeError(p, "cannot release savepoint - "
3014 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00003015 rc = SQLITE_BUSY;
3016 }else{
3017
3018 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00003019 ** and this is a RELEASE command, then the current transaction
3020 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00003021 */
3022 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
3023 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00003024 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003025 goto vdbe_return;
3026 }
danielk1977fd7f0452008-12-17 17:30:26 +00003027 db->autoCommit = 1;
3028 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003029 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00003030 db->autoCommit = 0;
3031 p->rc = rc = SQLITE_BUSY;
3032 goto vdbe_return;
3033 }
danielk197734cf35d2008-12-18 18:31:38 +00003034 db->isTransactionSavepoint = 0;
3035 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00003036 }else{
drh47b7fc72014-11-11 01:33:57 +00003037 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00003038 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00003039 if( p1==SAVEPOINT_ROLLBACK ){
drh47b7fc72014-11-11 01:33:57 +00003040 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
drh31f10052012-03-31 17:17:26 +00003041 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00003042 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
3043 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00003044 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00003045 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00003046 }
drh47b7fc72014-11-11 01:33:57 +00003047 }else{
3048 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00003049 }
3050 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00003051 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
3052 if( rc!=SQLITE_OK ){
3053 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00003054 }
danielk1977fd7f0452008-12-17 17:30:26 +00003055 }
drh47b7fc72014-11-11 01:33:57 +00003056 if( isSchemaChange ){
danielk1977fd7f0452008-12-17 17:30:26 +00003057 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00003058 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00003059 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00003060 }
3061 }
3062
3063 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
3064 ** savepoints nested inside of the savepoint being operated on. */
3065 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00003066 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00003067 db->pSavepoint = pTmp->pNext;
3068 sqlite3DbFree(db, pTmp);
3069 db->nSavepoint--;
3070 }
3071
dan1da40a32009-09-19 17:00:31 +00003072 /* If it is a RELEASE, then destroy the savepoint being operated on
3073 ** too. If it is a ROLLBACK TO, then set the number of deferred
3074 ** constraint violations present in the database to the value stored
3075 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00003076 if( p1==SAVEPOINT_RELEASE ){
3077 assert( pSavepoint==db->pSavepoint );
3078 db->pSavepoint = pSavepoint->pNext;
3079 sqlite3DbFree(db, pSavepoint);
3080 if( !isTransaction ){
3081 db->nSavepoint--;
3082 }
dan1da40a32009-09-19 17:00:31 +00003083 }else{
3084 db->nDeferredCons = pSavepoint->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003085 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00003086 }
dand9495cd2011-04-27 12:08:04 +00003087
danea8562e2015-04-18 16:25:54 +00003088 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00003089 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
3090 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3091 }
danielk1977fd7f0452008-12-17 17:30:26 +00003092 }
3093 }
drh9467abf2016-02-17 18:44:11 +00003094 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003095
3096 break;
3097}
3098
drh98757152008-01-09 23:04:12 +00003099/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003100**
3101** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003102** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003103** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3104** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003105**
3106** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003107*/
drh9cbf3422008-01-17 16:22:13 +00003108case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003109 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003110 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003111
drh856c1032009-06-02 15:21:42 +00003112 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003113 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003114 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003115 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003116 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003117 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003118
drhb0c88652016-02-01 13:21:13 +00003119 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003120 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003121 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003122 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003123 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003124 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3125 /* If this instruction implements a COMMIT and other VMs are writing
3126 ** return an error indicating that the other VMs must complete first.
3127 */
3128 sqlite3VdbeError(p, "cannot commit transaction - "
3129 "SQL statements in progress");
3130 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003131 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003132 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003133 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003134 }else{
shane7d3846a2008-12-11 02:58:26 +00003135 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003136 }
3137 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3138 p->pc = (int)(pOp - aOp);
3139 db->autoCommit = (u8)(1-desiredAutoCommit);
3140 p->rc = rc = SQLITE_BUSY;
3141 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003142 }
danielk1977bd434552009-03-18 10:33:00 +00003143 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003144 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003145 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003146 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003147 }else{
drh900b31e2007-08-28 02:27:51 +00003148 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003149 }
drh900b31e2007-08-28 02:27:51 +00003150 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003151 }else{
drh22c17b82015-05-15 04:13:15 +00003152 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003153 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003154 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003155 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003156
3157 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003158 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003159 }
3160 break;
3161}
3162
drhb22f7c82014-02-06 23:56:27 +00003163/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003164**
drh05a86c52014-02-16 01:55:49 +00003165** Begin a transaction on database P1 if a transaction is not already
3166** active.
3167** If P2 is non-zero, then a write-transaction is started, or if a
3168** read-transaction is already active, it is upgraded to a write-transaction.
3169** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003170**
drh001bbcb2003-03-19 03:14:00 +00003171** P1 is the index of the database file on which the transaction is
3172** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003173** file used for temporary tables. Indices of 2 or more are used for
3174** attached databases.
drhcabb0812002-09-14 13:47:32 +00003175**
dane0af83a2009-09-08 19:15:01 +00003176** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3177** true (this flag is set if the Vdbe may modify more than one row and may
3178** throw an ABORT exception), a statement transaction may also be opened.
3179** More specifically, a statement transaction is opened iff the database
3180** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003181** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003182** VDBE to be rolled back after an error without having to roll back the
3183** entire transaction. If no error is encountered, the statement transaction
3184** will automatically commit when the VDBE halts.
3185**
drhb22f7c82014-02-06 23:56:27 +00003186** If P5!=0 then this opcode also checks the schema cookie against P3
3187** and the schema generation counter against P4.
3188** The cookie changes its value whenever the database schema changes.
3189** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003190** and that the current process needs to reread the schema. If the schema
3191** cookie in P3 differs from the schema cookie in the database header or
3192** if the schema generation counter in P4 differs from the current
3193** generation counter, then an SQLITE_SCHEMA error is raised and execution
3194** halts. The sqlite3_step() wrapper function might then reprepare the
3195** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003196*/
drh9cbf3422008-01-17 16:22:13 +00003197case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003198 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00003199 int iMeta;
3200 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00003201
drh1713afb2013-06-28 01:24:57 +00003202 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003203 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003204 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003205 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003206 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3207 rc = SQLITE_READONLY;
3208 goto abort_due_to_error;
3209 }
drh653b82a2009-06-22 11:10:47 +00003210 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003211
danielk197724162fe2004-06-04 06:22:00 +00003212 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003213 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
drhcbd8db32015-08-20 17:18:32 +00003214 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3215 testcase( rc==SQLITE_BUSY_RECOVERY );
drh9e9f1bd2009-10-13 15:36:51 +00003216 if( rc!=SQLITE_OK ){
drhfadd2b12016-09-19 23:39:34 +00003217 if( (rc&0xff)==SQLITE_BUSY ){
3218 p->pc = (int)(pOp - aOp);
3219 p->rc = rc;
3220 goto vdbe_return;
3221 }
danielk197724162fe2004-06-04 06:22:00 +00003222 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003223 }
dane0af83a2009-09-08 19:15:01 +00003224
3225 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003226 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003227 ){
3228 assert( sqlite3BtreeIsInTrans(pBt) );
3229 if( p->iStatement==0 ){
3230 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3231 db->nStatement++;
3232 p->iStatement = db->nSavepoint + db->nStatement;
3233 }
dana311b802011-04-26 19:21:34 +00003234
drh346506f2011-05-25 01:16:42 +00003235 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003236 if( rc==SQLITE_OK ){
3237 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3238 }
dan1da40a32009-09-19 17:00:31 +00003239
3240 /* Store the current value of the database handles deferred constraint
3241 ** counter. If the statement transaction needs to be rolled back,
3242 ** the value of this counter needs to be restored too. */
3243 p->nStmtDefCons = db->nDeferredCons;
dancb3e4b72013-07-03 19:53:05 +00003244 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003245 }
drhb22f7c82014-02-06 23:56:27 +00003246
drh51a74d42015-02-28 01:04:27 +00003247 /* Gather the schema version number for checking:
drh96fdcb42016-09-27 00:09:33 +00003248 ** IMPLEMENTATION-OF: R-03189-51135 As each SQL statement runs, the schema
3249 ** version is checked to ensure that the schema has not changed since the
3250 ** SQL statement was prepared.
drh51a74d42015-02-28 01:04:27 +00003251 */
drhb22f7c82014-02-06 23:56:27 +00003252 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3253 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3254 }else{
3255 iGen = iMeta = 0;
3256 }
3257 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3258 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3259 sqlite3DbFree(db, p->zErrMsg);
3260 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3261 /* If the schema-cookie from the database file matches the cookie
3262 ** stored with the in-memory representation of the schema, do
3263 ** not reload the schema from the database file.
3264 **
3265 ** If virtual-tables are in use, this is not just an optimization.
3266 ** Often, v-tables store their data in other SQLite tables, which
3267 ** are queried from within xNext() and other v-table methods using
3268 ** prepared queries. If such a query is out-of-date, we do not want to
3269 ** discard the database schema, as the user code implementing the
3270 ** v-table would have to be ready for the sqlite3_vtab structure itself
3271 ** to be invalidated whenever sqlite3_step() is called from within
3272 ** a v-table method.
3273 */
3274 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3275 sqlite3ResetOneSchema(db, pOp->p1);
3276 }
3277 p->expired = 1;
3278 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003279 }
drh9467abf2016-02-17 18:44:11 +00003280 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003281 break;
3282}
3283
drhb1fdb2a2008-01-05 04:06:03 +00003284/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003285**
drh9cbf3422008-01-17 16:22:13 +00003286** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003287** P3==1 is the schema version. P3==2 is the database format.
3288** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003289** the main database file and P1==1 is the database file used to store
3290** temporary tables.
drh4a324312001-12-21 14:30:42 +00003291**
drh50e5dad2001-09-15 00:57:28 +00003292** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003293** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003294** executing this instruction.
3295*/
drh27a348c2015-04-13 19:14:06 +00003296case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003297 int iMeta;
drh856c1032009-06-02 15:21:42 +00003298 int iDb;
3299 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003300
drh1713afb2013-06-28 01:24:57 +00003301 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003302 iDb = pOp->p1;
3303 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003304 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003305 assert( iDb>=0 && iDb<db->nDb );
3306 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003307 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003308
danielk1977602b4662009-07-02 07:47:33 +00003309 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003310 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003311 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003312 break;
3313}
3314
drh98757152008-01-09 23:04:12 +00003315/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003316**
drh1861afc2016-02-01 21:48:34 +00003317** Write the integer value P3 into cookie number P2 of database P1.
3318** P2==1 is the schema version. P2==2 is the database format.
3319** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003320** size, and so forth. P1==0 is the main database file and P1==1 is the
3321** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003322**
3323** A transaction must be started before executing this opcode.
3324*/
drh1861afc2016-02-01 21:48:34 +00003325case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003326 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003327 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003328 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003329 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003330 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003331 pDb = &db->aDb[pOp->p1];
3332 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003333 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003334 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003335 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003336 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003337 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003338 pDb->pSchema->schema_cookie = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003339 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003340 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003341 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003342 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003343 }
drhfd426c62006-01-30 15:34:22 +00003344 if( pOp->p1==1 ){
3345 /* Invalidate all prepared statements whenever the TEMP database
3346 ** schema is changed. Ticket #1644 */
3347 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003348 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003349 }
drh9467abf2016-02-17 18:44:11 +00003350 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003351 break;
3352}
3353
drh98757152008-01-09 23:04:12 +00003354/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003355** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003356**
drhecdc7532001-09-23 02:35:53 +00003357** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003358** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003359** P3==0 means the main database, P3==1 means the database used for
3360** temporary tables, and P3>1 means used the corresponding attached
3361** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003362** values need not be contiguous but all P1 values should be small integers.
3363** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003364**
drh98757152008-01-09 23:04:12 +00003365** If P5!=0 then use the content of register P2 as the root page, not
3366** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003367**
drhb19a2bc2001-09-16 00:13:26 +00003368** There will be a read lock on the database whenever there is an
3369** open cursor. If the database was unlocked prior to this instruction
3370** then a read lock is acquired as part of this instruction. A read
3371** lock allows other processes to read the database but prohibits
3372** any other process from modifying the database. The read lock is
3373** released when all cursors are closed. If this instruction attempts
3374** to get a read lock but fails, the script terminates with an
3375** SQLITE_BUSY error code.
3376**
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
3381** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003382**
drh35263192014-07-22 20:02:19 +00003383** See also: OpenWrite, ReopenIdx
3384*/
3385/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3386** Synopsis: root=P2 iDb=P3
3387**
3388** The ReopenIdx opcode works exactly like ReadOpen except that it first
3389** checks to see if the cursor on P1 is already open with a root page
3390** number of P2 and if it is this opcode becomes a no-op. In other words,
3391** if the cursor is already open, do not reopen it.
3392**
3393** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3394** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3395** every other ReopenIdx or OpenRead for the same cursor number.
3396**
3397** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003398*/
drh98757152008-01-09 23:04:12 +00003399/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003400** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003401**
3402** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003403** page is P2. Or if P5!=0 use the content of register P2 to find the
3404** root page.
drhecdc7532001-09-23 02:35:53 +00003405**
danielk1977d336e222009-02-20 10:58:41 +00003406** The P4 value may be either an integer (P4_INT32) or a pointer to
3407** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3408** structure, then said structure defines the content and collating
3409** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003410** value, it is set to the number of columns in the table, or to the
3411** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003412**
drh001bbcb2003-03-19 03:14:00 +00003413** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003414** in read/write mode. For a given table, there can be one or more read-only
3415** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003416**
drh001bbcb2003-03-19 03:14:00 +00003417** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003418*/
drh35263192014-07-22 20:02:19 +00003419case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003420 int nField;
3421 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003422 int p2;
3423 int iDb;
drhf57b3392001-10-08 13:22:32 +00003424 int wrFlag;
3425 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003426 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003427 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003428
drhe0997b32015-03-20 14:57:50 +00003429 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003430 assert( pOp->p4type==P4_KEYINFO );
3431 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003432 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003433 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003434 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003435 }
3436 /* If the cursor is not currently open or is open on a different
3437 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003438case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003439case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003440
drhe0997b32015-03-20 14:57:50 +00003441 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003442 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003443 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3444 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003445
danfa401de2009-10-16 14:55:03 +00003446 if( p->expired ){
drh47b7fc72014-11-11 01:33:57 +00003447 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003448 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003449 }
3450
drh856c1032009-06-02 15:21:42 +00003451 nField = 0;
3452 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003453 p2 = pOp->p2;
3454 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003455 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003456 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003457 pDb = &db->aDb[iDb];
3458 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003459 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003460 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003461 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3462 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003463 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003464 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3465 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003466 }
3467 }else{
3468 wrFlag = 0;
3469 }
dan428c2182012-08-06 18:50:11 +00003470 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003471 assert( p2>0 );
drh9f6168b2016-03-19 23:32:58 +00003472 assert( p2<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003473 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003474 assert( memIsValid(pIn2) );
3475 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003476 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003477 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003478 /* The p2 value always comes from a prior OP_CreateTable opcode and
3479 ** that opcode will always set the p2 value to 2 or more or else fail.
3480 ** If there were a failure, the prepared statement would have halted
3481 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003482 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003483 }
danielk1977d336e222009-02-20 10:58:41 +00003484 if( pOp->p4type==P4_KEYINFO ){
3485 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003486 assert( pKeyInfo->enc==ENC(db) );
3487 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003488 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003489 }else if( pOp->p4type==P4_INT32 ){
3490 nField = pOp->p4.i;
3491 }
drh653b82a2009-06-22 11:10:47 +00003492 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003493 assert( nField>=0 );
3494 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003495 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003496 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003497 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003498 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003499 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003500#ifdef SQLITE_DEBUG
3501 pCur->wrFlag = wrFlag;
3502#endif
drhc960dcb2015-11-20 19:22:01 +00003503 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003504 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003505 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003506 ** SQLite used to check if the root-page flags were sane at this point
3507 ** and report database corruption if they were not, but this check has
3508 ** since moved into the btree layer. */
3509 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003510
3511open_cursor_set_hints:
3512 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3513 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003514 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003515#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003516 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3517#endif
drhc960dcb2015-11-20 19:22:01 +00003518 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003519 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003520 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003521 break;
3522}
3523
drh2a5d9902011-08-26 00:34:45 +00003524/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003525** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003526**
drhb9bb7c12006-06-11 23:41:55 +00003527** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003528** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003529** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003530** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003531**
drh25d3adb2010-04-05 15:11:08 +00003532** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003533** The cursor points to a BTree table if P4==0 and to a BTree index
3534** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003535** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003536**
drh2a5d9902011-08-26 00:34:45 +00003537** The P5 parameter can be a mask of the BTREE_* flags defined
3538** in btree.h. These flags control aspects of the operation of
3539** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3540** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003541*/
drha21a64d2010-04-06 22:33:55 +00003542/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003543** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003544**
3545** This opcode works the same as OP_OpenEphemeral. It has a
3546** different name to distinguish its use. Tables created using
3547** by this opcode will be used for automatically created transient
3548** indices in joins.
3549*/
3550case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003551case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003552 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003553 KeyInfo *pKeyInfo;
3554
drhd4187c72010-08-30 22:15:45 +00003555 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003556 SQLITE_OPEN_READWRITE |
3557 SQLITE_OPEN_CREATE |
3558 SQLITE_OPEN_EXCLUSIVE |
3559 SQLITE_OPEN_DELETEONCLOSE |
3560 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003561 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003562 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003563 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003564 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003565 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003566 pCx->isEphemeral = 1;
drhfbd8cbd2016-12-10 12:58:15 +00003567 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx,
drhd4187c72010-08-30 22:15:45 +00003568 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003569 if( rc==SQLITE_OK ){
drhfbd8cbd2016-12-10 12:58:15 +00003570 rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
drh5e00f6c2001-09-13 13:46:56 +00003571 }
3572 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003573 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003574 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003575 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003576 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003577 */
drhfbd8cbd2016-12-10 12:58:15 +00003578 if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003579 int pgno;
drh66a51672008-01-03 00:01:23 +00003580 assert( pOp->p4type==P4_KEYINFO );
drhfbd8cbd2016-12-10 12:58:15 +00003581 rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003582 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003583 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003584 assert( pKeyInfo->db==db );
3585 assert( pKeyInfo->enc==ENC(db) );
drhfbd8cbd2016-12-10 12:58:15 +00003586 rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003587 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003588 }
drhf0863fe2005-06-12 21:35:51 +00003589 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003590 }else{
drhfbd8cbd2016-12-10 12:58:15 +00003591 rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
drh62aaa6c2015-11-21 17:27:42 +00003592 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003593 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003594 }
drh5e00f6c2001-09-13 13:46:56 +00003595 }
drh9467abf2016-02-17 18:44:11 +00003596 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003597 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003598 break;
3599}
3600
danfad9f9a2014-04-01 18:41:51 +00003601/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003602**
3603** This opcode works like OP_OpenEphemeral except that it opens
3604** a transient index that is specifically designed to sort large
3605** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003606**
3607** If argument P3 is non-zero, then it indicates that the sorter may
3608** assume that a stable sort considering the first P3 fields of each
3609** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003610*/
drhca892a72011-09-03 00:17:51 +00003611case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003612 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003613
drh399af1d2013-11-20 17:25:55 +00003614 assert( pOp->p1>=0 );
3615 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003616 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003617 if( pCx==0 ) goto no_mem;
3618 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003619 assert( pCx->pKeyInfo->db==db );
3620 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003621 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003622 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003623 break;
3624}
3625
dan78d58432014-03-25 15:04:07 +00003626/* Opcode: SequenceTest P1 P2 * * *
3627** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3628**
3629** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3630** to P2. Regardless of whether or not the jump is taken, increment the
3631** the sequence value.
3632*/
3633case OP_SequenceTest: {
3634 VdbeCursor *pC;
3635 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3636 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003637 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003638 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003639 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003640 }
drh5e00f6c2001-09-13 13:46:56 +00003641 break;
3642}
3643
drh5f612292014-02-08 23:20:32 +00003644/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003645** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003646**
3647** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003648** row of data. The content of that one row is the content of memory
3649** register P2. In other words, cursor P1 becomes an alias for the
3650** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003651**
drh2d8d7ce2010-02-15 15:17:05 +00003652** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003653** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003654** individual columns using the OP_Column opcode. The OP_Column opcode
3655** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003656**
3657** P3 is the number of fields in the records that will be stored by
3658** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003659*/
drh9cbf3422008-01-17 16:22:13 +00003660case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003661 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003662
drh653b82a2009-06-22 11:10:47 +00003663 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003664 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003665 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003666 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003667 pCx->nullRow = 1;
drhc960dcb2015-11-20 19:22:01 +00003668 pCx->uc.pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003669 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003670 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003671 break;
3672}
3673
drh98757152008-01-09 23:04:12 +00003674/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003675**
3676** Close a cursor previously opened as P1. If P1 is not
3677** currently open, this instruction is a no-op.
3678*/
drh9cbf3422008-01-17 16:22:13 +00003679case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003680 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3681 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3682 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003683 break;
3684}
3685
drh97bae792015-06-05 15:59:57 +00003686#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3687/* Opcode: ColumnsUsed P1 * * P4 *
3688**
3689** This opcode (which only exists if SQLite was compiled with
3690** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3691** table or index for cursor P1 are used. P4 is a 64-bit integer
3692** (P4_INT64) in which the first 63 bits are one for each of the
3693** first 63 columns of the table or index that are actually used
3694** by the cursor. The high-order bit is set if any column after
3695** the 64th is used.
3696*/
3697case OP_ColumnsUsed: {
3698 VdbeCursor *pC;
3699 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003700 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003701 pC->maskUsed = *(u64*)pOp->p4.pI64;
3702 break;
3703}
3704#endif
3705
drh8af3f772014-07-25 18:01:06 +00003706/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003707** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003708**
danielk1977b790c6c2008-04-18 10:25:24 +00003709** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003710** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003711** to an SQL index, then P3 is the first in an array of P4 registers
3712** that are used as an unpacked index key.
3713**
3714** Reposition cursor P1 so that it points to the smallest entry that
3715** is greater than or equal to the key value. If there are no records
3716** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003717**
drhb1d607d2015-11-05 22:30:54 +00003718** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3719** opcode will always land on a record that equally equals the key, or
3720** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3721** opcode must be followed by an IdxLE opcode with the same arguments.
3722** The IdxLE opcode will be skipped if this opcode succeeds, but the
3723** IdxLE opcode will be used on subsequent loop iterations.
3724**
drh8af3f772014-07-25 18:01:06 +00003725** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003726** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003727** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003728**
drh935850e2014-05-24 17:15:15 +00003729** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003730*/
drh8af3f772014-07-25 18:01:06 +00003731/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003732** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003733**
danielk1977b790c6c2008-04-18 10:25:24 +00003734** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003735** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003736** to an SQL index, then P3 is the first in an array of P4 registers
3737** that are used as an unpacked index key.
3738**
3739** Reposition cursor P1 so that it points to the smallest entry that
3740** is greater than the key value. If there are no records greater than
3741** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003742**
drh8af3f772014-07-25 18:01:06 +00003743** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003744** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003745** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003746**
drh935850e2014-05-24 17:15:15 +00003747** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003748*/
drh8af3f772014-07-25 18:01:06 +00003749/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003750** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003751**
danielk1977b790c6c2008-04-18 10:25:24 +00003752** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003753** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003754** to an SQL index, then P3 is the first in an array of P4 registers
3755** that are used as an unpacked index key.
3756**
3757** Reposition cursor P1 so that it points to the largest entry that
3758** is less than the key value. If there are no records less than
3759** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003760**
drh8af3f772014-07-25 18:01:06 +00003761** This opcode leaves the cursor configured to move in reverse order,
3762** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003763** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003764**
drh935850e2014-05-24 17:15:15 +00003765** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003766*/
drh8af3f772014-07-25 18:01:06 +00003767/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003768** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003769**
danielk1977b790c6c2008-04-18 10:25:24 +00003770** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003771** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003772** to an SQL index, then P3 is the first in an array of P4 registers
3773** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003774**
danielk1977b790c6c2008-04-18 10:25:24 +00003775** Reposition cursor P1 so that it points to the largest entry that
3776** is less than or equal to the key value. If there are no records
3777** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003778**
drh8af3f772014-07-25 18:01:06 +00003779** This opcode leaves the cursor configured to move in reverse order,
3780** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003781** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003782**
drhb1d607d2015-11-05 22:30:54 +00003783** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3784** opcode will always land on a record that equally equals the key, or
3785** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3786** opcode must be followed by an IdxGE opcode with the same arguments.
3787** The IdxGE opcode will be skipped if this opcode succeeds, but the
3788** IdxGE opcode will be used on subsequent loop iterations.
3789**
drh935850e2014-05-24 17:15:15 +00003790** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003791*/
drh4a1d3652014-02-14 15:13:36 +00003792case OP_SeekLT: /* jump, in3 */
3793case OP_SeekLE: /* jump, in3 */
3794case OP_SeekGE: /* jump, in3 */
3795case OP_SeekGT: { /* jump, in3 */
drhb1d607d2015-11-05 22:30:54 +00003796 int res; /* Comparison result */
3797 int oc; /* Opcode */
3798 VdbeCursor *pC; /* The cursor to seek */
3799 UnpackedRecord r; /* The key to seek for */
3800 int nField; /* Number of columns or fields in the key */
3801 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003802 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003803
drh653b82a2009-06-22 11:10:47 +00003804 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003805 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003806 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003807 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003808 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003809 assert( OP_SeekLE == OP_SeekLT+1 );
3810 assert( OP_SeekGE == OP_SeekLT+2 );
3811 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003812 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003813 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003814 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003815 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003816 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003817#ifdef SQLITE_DEBUG
3818 pC->seekOp = pOp->opcode;
3819#endif
drhe0997b32015-03-20 14:57:50 +00003820
drh3da046d2013-11-11 03:24:11 +00003821 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003822 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drh218c66e2016-12-27 12:35:36 +00003823 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
3824 || CORRUPT_DB );
drhd6b79462015-11-07 01:19:00 +00003825
drh3da046d2013-11-11 03:24:11 +00003826 /* The input value in P3 might be of any type: integer, real, string,
3827 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003828 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003829 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003830 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003831 applyNumericAffinity(pIn3, 0);
3832 }
drh3da046d2013-11-11 03:24:11 +00003833 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003834
drh3da046d2013-11-11 03:24:11 +00003835 /* If the P3 value could not be converted into an integer without
3836 ** loss of information, then special processing is required... */
3837 if( (pIn3->flags & MEM_Int)==0 ){
3838 if( (pIn3->flags & MEM_Real)==0 ){
3839 /* If the P3 value cannot be converted into any kind of a number,
3840 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003841 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003842 break;
3843 }
drh959403f2008-12-12 17:56:16 +00003844
danaa1776f2013-11-26 18:22:59 +00003845 /* If the approximation iKey is larger than the actual real search
3846 ** term, substitute >= for > and < for <=. e.g. if the search term
3847 ** is 4.9 and the integer approximation 5:
3848 **
3849 ** (x > 4.9) -> (x >= 5)
3850 ** (x <= 4.9) -> (x < 5)
3851 */
drh74eaba42014-09-18 17:52:15 +00003852 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003853 assert( OP_SeekGE==(OP_SeekGT-1) );
3854 assert( OP_SeekLT==(OP_SeekLE-1) );
3855 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3856 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003857 }
3858
3859 /* If the approximation iKey is smaller than the actual real search
3860 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003861 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003862 assert( OP_SeekLE==(OP_SeekLT+1) );
3863 assert( OP_SeekGT==(OP_SeekGE+1) );
3864 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3865 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003866 }
drh3da046d2013-11-11 03:24:11 +00003867 }
drhc960dcb2015-11-20 19:22:01 +00003868 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003869 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003870 if( rc!=SQLITE_OK ){
3871 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003872 }
drhaa736092009-06-22 00:55:30 +00003873 }else{
drhd6b79462015-11-07 01:19:00 +00003874 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3875 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3876 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3877 */
drhc960dcb2015-11-20 19:22:01 +00003878 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003879 eqOnly = 1;
3880 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3881 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3882 assert( pOp[1].p1==pOp[0].p1 );
3883 assert( pOp[1].p2==pOp[0].p2 );
3884 assert( pOp[1].p3==pOp[0].p3 );
3885 assert( pOp[1].p4.i==pOp[0].p4.i );
3886 }
3887
drh3da046d2013-11-11 03:24:11 +00003888 nField = pOp->p4.i;
3889 assert( pOp->p4type==P4_INT32 );
3890 assert( nField>0 );
3891 r.pKeyInfo = pC->pKeyInfo;
3892 r.nField = (u16)nField;
3893
3894 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003895 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003896 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003897 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003898 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003899 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003900 */
dan1fed5da2014-02-25 21:01:25 +00003901 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3902 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3903 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3904 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3905 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003906
3907 r.aMem = &aMem[pOp->p3];
3908#ifdef SQLITE_DEBUG
3909 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3910#endif
drh70528d72015-11-05 20:25:09 +00003911 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00003912 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00003913 if( rc!=SQLITE_OK ){
3914 goto abort_due_to_error;
3915 }
drhb1d607d2015-11-05 22:30:54 +00003916 if( eqOnly && r.eqSeen==0 ){
3917 assert( res!=0 );
3918 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00003919 }
drh3da046d2013-11-11 03:24:11 +00003920 }
3921 pC->deferredMoveto = 0;
3922 pC->cacheStatus = CACHE_STALE;
3923#ifdef SQLITE_TEST
3924 sqlite3_search_count++;
3925#endif
drh4a1d3652014-02-14 15:13:36 +00003926 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3927 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003928 res = 0;
drhc960dcb2015-11-20 19:22:01 +00003929 rc = sqlite3BtreeNext(pC->uc.pCursor, &res);
drh3da046d2013-11-11 03:24:11 +00003930 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003931 }else{
3932 res = 0;
3933 }
3934 }else{
drh4a1d3652014-02-14 15:13:36 +00003935 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3936 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003937 res = 0;
drhc960dcb2015-11-20 19:22:01 +00003938 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res);
drh3da046d2013-11-11 03:24:11 +00003939 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003940 }else{
3941 /* res might be negative because the table is empty. Check to
3942 ** see if this is the case.
3943 */
drhc960dcb2015-11-20 19:22:01 +00003944 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00003945 }
3946 }
drhb1d607d2015-11-05 22:30:54 +00003947seek_not_found:
drh3da046d2013-11-11 03:24:11 +00003948 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003949 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003950 if( res ){
drhf56fa462015-04-13 21:39:54 +00003951 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00003952 }else if( eqOnly ){
3953 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3954 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00003955 }
drh5e00f6c2001-09-13 13:46:56 +00003956 break;
3957}
dan71c57db2016-07-09 20:23:55 +00003958
drh8cff69d2009-11-12 19:59:44 +00003959/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003960** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003961**
drh8cff69d2009-11-12 19:59:44 +00003962** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3963** P4>0 then register P3 is the first of P4 registers that form an unpacked
3964** record.
3965**
3966** Cursor P1 is on an index btree. If the record identified by P3 and P4
3967** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003968** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003969**
drhcefc87f2014-08-01 01:40:33 +00003970** This operation leaves the cursor in a state where it can be
3971** advanced in the forward direction. The Next instruction will work,
3972** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003973**
drh6f225d02013-10-26 13:36:51 +00003974** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003975*/
drh8cff69d2009-11-12 19:59:44 +00003976/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003977** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003978**
drh8cff69d2009-11-12 19:59:44 +00003979** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3980** P4>0 then register P3 is the first of P4 registers that form an unpacked
3981** record.
3982**
3983** Cursor P1 is on an index btree. If the record identified by P3 and P4
3984** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3985** does contain an entry whose prefix matches the P3/P4 record then control
3986** falls through to the next instruction and P1 is left pointing at the
3987** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003988**
drh8af3f772014-07-25 18:01:06 +00003989** This operation leaves the cursor in a state where it cannot be
3990** advanced in either direction. In other words, the Next and Prev
3991** opcodes do not work after this operation.
3992**
drh6f225d02013-10-26 13:36:51 +00003993** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003994*/
drh6f225d02013-10-26 13:36:51 +00003995/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003996** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003997**
3998** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3999** P4>0 then register P3 is the first of P4 registers that form an unpacked
4000** record.
4001**
4002** Cursor P1 is on an index btree. If the record identified by P3 and P4
4003** contains any NULL value, jump immediately to P2. If all terms of the
4004** record are not-NULL then a check is done to determine if any row in the
4005** P1 index btree has a matching key prefix. If there are no matches, jump
4006** immediately to P2. If there is a match, fall through and leave the P1
4007** cursor pointing to the matching row.
4008**
4009** This opcode is similar to OP_NotFound with the exceptions that the
4010** branch is always taken if any part of the search key input is NULL.
4011**
drh8af3f772014-07-25 18:01:06 +00004012** This operation leaves the cursor in a state where it cannot be
4013** advanced in either direction. In other words, the Next and Prev
4014** opcodes do not work after this operation.
4015**
drh6f225d02013-10-26 13:36:51 +00004016** See also: NotFound, Found, NotExists
4017*/
4018case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004019case OP_NotFound: /* jump, in3 */
4020case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00004021 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00004022 int takeJump;
drh6f225d02013-10-26 13:36:51 +00004023 int ii;
drhdfe88ec2008-11-03 20:55:06 +00004024 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004025 int res;
drha582b012016-12-21 19:45:54 +00004026 UnpackedRecord *pFree;
drh856c1032009-06-02 15:21:42 +00004027 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00004028 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004029
dan0ff297e2009-09-25 17:03:14 +00004030#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00004031 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00004032#endif
4033
drhaa736092009-06-22 00:55:30 +00004034 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00004035 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00004036 pC = p->apCsr[pOp->p1];
4037 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004038#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00004039 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00004040#endif
drh3c657212009-11-17 23:59:58 +00004041 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00004042 assert( pC->eCurType==CURTYPE_BTREE );
4043 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004044 assert( pC->isTable==0 );
4045 if( pOp->p4.i>0 ){
4046 r.pKeyInfo = pC->pKeyInfo;
4047 r.nField = (u16)pOp->p4.i;
4048 r.aMem = pIn3;
drh8aaf7bc2016-09-20 01:19:18 +00004049#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00004050 for(ii=0; ii<r.nField; ii++){
4051 assert( memIsValid(&r.aMem[ii]) );
drh8aaf7bc2016-09-20 01:19:18 +00004052 assert( (r.aMem[ii].flags & MEM_Zero)==0 || r.aMem[ii].n==0 );
drh826af372014-02-08 19:12:21 +00004053 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh826af372014-02-08 19:12:21 +00004054 }
drh8aaf7bc2016-09-20 01:19:18 +00004055#endif
drh3da046d2013-11-11 03:24:11 +00004056 pIdxKey = &r;
drha582b012016-12-21 19:45:54 +00004057 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00004058 }else{
drha582b012016-12-21 19:45:54 +00004059 pFree = pIdxKey = sqlite3VdbeAllocUnpackedRecord(pC->pKeyInfo);
drh3da046d2013-11-11 03:24:11 +00004060 if( pIdxKey==0 ) goto no_mem;
4061 assert( pIn3->flags & MEM_Blob );
drh2eb22af2016-09-10 19:51:40 +00004062 (void)ExpandBlob(pIn3);
drh3da046d2013-11-11 03:24:11 +00004063 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh5e00f6c2001-09-13 13:46:56 +00004064 }
dan1fed5da2014-02-25 21:01:25 +00004065 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00004066 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00004067 if( pOp->opcode==OP_NoConflict ){
4068 /* For the OP_NoConflict opcode, take the jump if any of the
4069 ** input fields are NULL, since any key with a NULL will not
4070 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00004071 for(ii=0; ii<pIdxKey->nField; ii++){
4072 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00004073 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00004074 break;
drh6f225d02013-10-26 13:36:51 +00004075 }
4076 }
drh5e00f6c2001-09-13 13:46:56 +00004077 }
drhc960dcb2015-11-20 19:22:01 +00004078 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhdbd6a7d2017-04-05 12:39:49 +00004079 if( pFree ) sqlite3DbFreeNN(db, pFree);
drh3da046d2013-11-11 03:24:11 +00004080 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00004081 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00004082 }
4083 pC->seekResult = res;
4084 alreadyExists = (res==0);
4085 pC->nullRow = 1-alreadyExists;
4086 pC->deferredMoveto = 0;
4087 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004088 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004089 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004090 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004091 }else{
drhf56fa462015-04-13 21:39:54 +00004092 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4093 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004094 }
drh5e00f6c2001-09-13 13:46:56 +00004095 break;
4096}
4097
drheeb95652016-05-26 20:56:38 +00004098/* Opcode: SeekRowid P1 P2 P3 * *
4099** Synopsis: intkey=r[P3]
4100**
4101** P1 is the index of a cursor open on an SQL table btree (with integer
4102** keys). If register P3 does not contain an integer or if P1 does not
4103** contain a record with rowid P3 then jump immediately to P2.
4104** Or, if P2 is 0, raise an SQLITE_CORRUPT error. If P1 does contain
4105** a record with rowid P3 then
4106** leave the cursor pointing at that record and fall through to the next
4107** instruction.
4108**
4109** The OP_NotExists opcode performs the same operation, but with OP_NotExists
4110** the P3 register must be guaranteed to contain an integer value. With this
4111** opcode, register P3 might not contain an integer.
4112**
4113** The OP_NotFound opcode performs the same operation on index btrees
4114** (with arbitrary multi-value keys).
4115**
4116** This opcode leaves the cursor in a state where it cannot be advanced
4117** in either direction. In other words, the Next and Prev opcodes will
4118** not work following this opcode.
4119**
4120** See also: Found, NotFound, NoConflict, SeekRowid
4121*/
drh9cbf3422008-01-17 16:22:13 +00004122/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004123** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004124**
drh261c02d2013-10-25 14:46:15 +00004125** P1 is the index of a cursor open on an SQL table btree (with integer
4126** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004127** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4128** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4129** leave the cursor pointing at that record and fall through to the next
4130** instruction.
drh6b125452002-01-28 15:53:03 +00004131**
drheeb95652016-05-26 20:56:38 +00004132** The OP_SeekRowid opcode performs the same operation but also allows the
4133** P3 register to contain a non-integer value, in which case the jump is
4134** always taken. This opcode requires that P3 always contain an integer.
4135**
drh261c02d2013-10-25 14:46:15 +00004136** The OP_NotFound opcode performs the same operation on index btrees
4137** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004138**
drh8af3f772014-07-25 18:01:06 +00004139** 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**
drheeb95652016-05-26 20:56:38 +00004143** See also: Found, NotFound, NoConflict, SeekRowid
drh6b125452002-01-28 15:53:03 +00004144*/
drheeb95652016-05-26 20:56:38 +00004145case OP_SeekRowid: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004146 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004147 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004148 int res;
4149 u64 iKey;
4150
drh3c657212009-11-17 23:59:58 +00004151 pIn3 = &aMem[pOp->p3];
drheeb95652016-05-26 20:56:38 +00004152 if( (pIn3->flags & MEM_Int)==0 ){
4153 applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);
4154 if( (pIn3->flags & MEM_Int)==0 ) goto jump_to_p2;
4155 }
4156 /* Fall through into OP_NotExists */
4157case OP_NotExists: /* jump, in3 */
4158 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004159 assert( pIn3->flags & MEM_Int );
4160 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4161 pC = p->apCsr[pOp->p1];
4162 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004163#ifdef SQLITE_DEBUG
4164 pC->seekOp = 0;
4165#endif
drhaa736092009-06-22 00:55:30 +00004166 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004167 assert( pC->eCurType==CURTYPE_BTREE );
4168 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004169 assert( pCrsr!=0 );
4170 res = 0;
4171 iKey = pIn3->u.i;
4172 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004173 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004174 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004175 pC->nullRow = 0;
4176 pC->cacheStatus = CACHE_STALE;
4177 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004178 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00004179 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004180 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004181 assert( rc==SQLITE_OK );
4182 if( pOp->p2==0 ){
4183 rc = SQLITE_CORRUPT_BKPT;
4184 }else{
4185 goto jump_to_p2;
4186 }
danc6157e12015-09-14 09:23:47 +00004187 }
drh9467abf2016-02-17 18:44:11 +00004188 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004189 break;
4190}
4191
drh4c583122008-01-04 22:01:03 +00004192/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004193** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004194**
drh4c583122008-01-04 22:01:03 +00004195** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004196** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004197** The sequence number on the cursor is incremented after this
4198** instruction.
drh4db38a72005-09-01 12:16:28 +00004199*/
drh27a348c2015-04-13 19:14:06 +00004200case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004201 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4202 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004203 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004204 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004205 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004206 break;
4207}
4208
4209
drh98757152008-01-09 23:04:12 +00004210/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004211** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004212**
drhf0863fe2005-06-12 21:35:51 +00004213** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004214** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004215** table that cursor P1 points to. The new record number is written
4216** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004217**
dan76d462e2009-08-30 11:42:51 +00004218** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4219** the largest previously generated record number. No new record numbers are
4220** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004221** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004222** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004223** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004224*/
drh27a348c2015-04-13 19:14:06 +00004225case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004226 i64 v; /* The new rowid */
4227 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4228 int res; /* Result of an sqlite3BtreeLast() */
4229 int cnt; /* Counter to limit the number of searches */
4230 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004231 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004232
drh856c1032009-06-02 15:21:42 +00004233 v = 0;
4234 res = 0;
drh27a348c2015-04-13 19:14:06 +00004235 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004236 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4237 pC = p->apCsr[pOp->p1];
4238 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004239 assert( pC->eCurType==CURTYPE_BTREE );
4240 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004241 {
drh5cf8e8c2002-02-19 22:42:05 +00004242 /* The next rowid or record number (different terms for the same
4243 ** thing) is obtained in a two-step algorithm.
4244 **
4245 ** First we attempt to find the largest existing rowid and add one
4246 ** to that. But if the largest existing rowid is already the maximum
4247 ** positive integer, we have to fall through to the second
4248 ** probabilistic algorithm
4249 **
4250 ** The second algorithm is to select a rowid at random and see if
4251 ** it already exists in the table. If it does not exist, we have
4252 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004253 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004254 */
drhaa736092009-06-22 00:55:30 +00004255 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004256
drh75f86a42005-02-17 00:03:06 +00004257#ifdef SQLITE_32BIT_ROWID
4258# define MAX_ROWID 0x7fffffff
4259#else
drhfe2093d2005-01-20 22:48:47 +00004260 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4261 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4262 ** to provide the constant while making all compilers happy.
4263 */
danielk197764202cf2008-11-17 15:31:47 +00004264# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004265#endif
drhfe2093d2005-01-20 22:48:47 +00004266
drh5cf8e8c2002-02-19 22:42:05 +00004267 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004268 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004269 if( rc!=SQLITE_OK ){
4270 goto abort_due_to_error;
4271 }
4272 if( res ){
4273 v = 1; /* IMP: R-61914-48074 */
4274 }else{
drhc960dcb2015-11-20 19:22:01 +00004275 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
drha7c90c42016-06-04 20:37:10 +00004276 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drhe0670b62014-02-12 21:31:12 +00004277 if( v>=MAX_ROWID ){
4278 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004279 }else{
drhe0670b62014-02-12 21:31:12 +00004280 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004281 }
drh3fc190c2001-09-14 03:24:23 +00004282 }
drhe0670b62014-02-12 21:31:12 +00004283 }
drh205f48e2004-11-05 00:43:11 +00004284
4285#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004286 if( pOp->p3 ){
4287 /* Assert that P3 is a valid memory cell. */
4288 assert( pOp->p3>0 );
4289 if( p->pFrame ){
4290 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004291 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004292 assert( pOp->p3<=pFrame->nMem );
4293 pMem = &pFrame->aMem[pOp->p3];
4294 }else{
4295 /* Assert that P3 is a valid memory cell. */
drh9f6168b2016-03-19 23:32:58 +00004296 assert( pOp->p3<=(p->nMem+1 - p->nCursor) );
drhe0670b62014-02-12 21:31:12 +00004297 pMem = &aMem[pOp->p3];
4298 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004299 }
drhe0670b62014-02-12 21:31:12 +00004300 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004301
drhe0670b62014-02-12 21:31:12 +00004302 REGISTER_TRACE(pOp->p3, pMem);
4303 sqlite3VdbeMemIntegerify(pMem);
4304 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4305 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhe77caa12016-11-02 13:18:46 +00004306 rc = SQLITE_FULL; /* IMP: R-17817-00630 */
drhe0670b62014-02-12 21:31:12 +00004307 goto abort_due_to_error;
4308 }
4309 if( v<pMem->u.i+1 ){
4310 v = pMem->u.i + 1;
4311 }
4312 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004313 }
drhe0670b62014-02-12 21:31:12 +00004314#endif
drh5cf8e8c2002-02-19 22:42:05 +00004315 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004316 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004317 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004318 ** engine starts picking positive candidate ROWIDs at random until
4319 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004320 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4321 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004322 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004323 do{
4324 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004325 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004326 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004327 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004328 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004329 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004330 if( rc ) goto abort_due_to_error;
4331 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004332 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004333 goto abort_due_to_error;
4334 }
drh748a52c2010-09-01 11:50:08 +00004335 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004336 }
drha11846b2004-01-07 18:52:56 +00004337 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004338 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004339 }
drh4c583122008-01-04 22:01:03 +00004340 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004341 break;
4342}
4343
danielk19771f4aa332008-01-03 09:51:55 +00004344/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004345** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004346**
jplyon5a564222003-06-02 06:15:58 +00004347** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004348** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004349** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004350** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004351** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004352**
danielk19771f4aa332008-01-03 09:51:55 +00004353** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4354** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004355** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004356** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004357**
drheaf6ae22016-11-09 20:14:34 +00004358** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
4359** run faster by avoiding an unnecessary seek on cursor P1. However,
4360** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
4361** seeks on the cursor or if the most recent seek used a key equal to P3.
drh3e9ca092009-09-08 01:14:48 +00004362**
4363** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4364** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4365** is part of an INSERT operation. The difference is only important to
4366** the update hook.
4367**
dan319eeb72011-03-19 08:38:50 +00004368** Parameter P4 may point to a Table structure, or may be NULL. If it is
4369** not NULL, then the update-hook (sqlite3.xUpdateCallback) is invoked
4370** following a successful insert.
danielk19771f6eec52006-06-16 06:17:47 +00004371**
drh93aed5a2008-01-16 17:46:38 +00004372** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4373** allocated, then ownership of P2 is transferred to the pseudo-cursor
4374** and register P2 becomes ephemeral. If the cursor is changed, the
4375** value of register P2 will then change. Make sure this does not
4376** cause any problems.)
4377**
drhf0863fe2005-06-12 21:35:51 +00004378** This instruction only works on tables. The equivalent instruction
4379** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004380*/
drhe05c9292009-10-29 13:48:10 +00004381/* Opcode: InsertInt P1 P2 P3 P4 P5
drh72e26de2016-08-24 21:24:04 +00004382** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004383**
4384** This works exactly like OP_Insert except that the key is the
4385** integer value P3, not the value of the integer stored in register P3.
4386*/
4387case OP_Insert:
4388case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004389 Mem *pData; /* MEM cell holding data for the record to be inserted */
4390 Mem *pKey; /* MEM cell holding key for the record */
drh3e9ca092009-09-08 01:14:48 +00004391 VdbeCursor *pC; /* Cursor to table into which insert is written */
drh3e9ca092009-09-08 01:14:48 +00004392 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4393 const char *zDb; /* database name - used by the update hook */
dan319eeb72011-03-19 08:38:50 +00004394 Table *pTab; /* Table structure - used by update and pre-update hooks */
drh74c33022016-03-30 12:56:55 +00004395 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh8eeb4462016-05-21 20:03:42 +00004396 BtreePayload x; /* Payload to be inserted */
drh856c1032009-06-02 15:21:42 +00004397
drh74c33022016-03-30 12:56:55 +00004398 op = 0;
drha6c2ed92009-11-14 23:22:23 +00004399 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004400 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004401 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004402 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004403 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004404 assert( pC->eCurType==CURTYPE_BTREE );
4405 assert( pC->uc.pCursor!=0 );
dancb9a3642017-01-30 19:44:53 +00004406 assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
drhcbf1b8e2013-11-11 22:55:26 +00004407 assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
drh5b6afba2008-01-05 16:29:28 +00004408 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004409
drhe05c9292009-10-29 13:48:10 +00004410 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004411 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004412 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004413 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004414 REGISTER_TRACE(pOp->p3, pKey);
drh8eeb4462016-05-21 20:03:42 +00004415 x.nKey = pKey->u.i;
drhe05c9292009-10-29 13:48:10 +00004416 }else{
4417 assert( pOp->opcode==OP_InsertInt );
drh8eeb4462016-05-21 20:03:42 +00004418 x.nKey = pOp->p3;
drhe05c9292009-10-29 13:48:10 +00004419 }
4420
drh9b1c62d2011-03-30 21:04:43 +00004421 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004422 assert( pC->iDb>=0 );
drh69c33822016-08-18 14:33:11 +00004423 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004424 pTab = pOp->p4.pTab;
dancb9a3642017-01-30 19:44:53 +00004425 assert( (pOp->p5 & OPFLAG_ISNOOP) || HasRowid(pTab) );
dan46c47d42011-03-01 18:42:07 +00004426 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drh74c33022016-03-30 12:56:55 +00004427 }else{
drh13795212017-01-31 15:27:04 +00004428 pTab = 0; /* Not needed. Silence a compiler warning. */
drh74c33022016-03-30 12:56:55 +00004429 zDb = 0; /* Not needed. Silence a compiler warning. */
dan46c47d42011-03-01 18:42:07 +00004430 }
4431
drh9b1c62d2011-03-30 21:04:43 +00004432#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004433 /* Invoke the pre-update hook, if any */
4434 if( db->xPreUpdateCallback
dan319eeb72011-03-19 08:38:50 +00004435 && pOp->p4type==P4_TABLE
drh92fe38e2014-10-14 13:41:32 +00004436 && !(pOp->p5 & OPFLAG_ISUPDATE)
dan46c47d42011-03-01 18:42:07 +00004437 ){
drh8eeb4462016-05-21 20:03:42 +00004438 sqlite3VdbePreUpdateHook(p, pC, SQLITE_INSERT, zDb, pTab, x.nKey, pOp->p2);
dan46c47d42011-03-01 18:42:07 +00004439 }
dancb9a3642017-01-30 19:44:53 +00004440 if( pOp->p5 & OPFLAG_ISNOOP ) break;
drh9b1c62d2011-03-30 21:04:43 +00004441#endif
dan46c47d42011-03-01 18:42:07 +00004442
drha05a7222008-01-19 03:35:58 +00004443 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhfae58d52017-01-26 17:26:44 +00004444 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = x.nKey;
drha05a7222008-01-19 03:35:58 +00004445 if( pData->flags & MEM_Null ){
drh8eeb4462016-05-21 20:03:42 +00004446 x.pData = 0;
4447 x.nData = 0;
drha05a7222008-01-19 03:35:58 +00004448 }else{
4449 assert( pData->flags & (MEM_Blob|MEM_Str) );
drh8eeb4462016-05-21 20:03:42 +00004450 x.pData = pData->z;
4451 x.nData = pData->n;
drha05a7222008-01-19 03:35:58 +00004452 }
drh3e9ca092009-09-08 01:14:48 +00004453 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4454 if( pData->flags & MEM_Zero ){
drh8eeb4462016-05-21 20:03:42 +00004455 x.nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004456 }else{
drh8eeb4462016-05-21 20:03:42 +00004457 x.nZero = 0;
drha05a7222008-01-19 03:35:58 +00004458 }
drh8eeb4462016-05-21 20:03:42 +00004459 x.pKey = 0;
4460 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00004461 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)), seekResult
drh3e9ca092009-09-08 01:14:48 +00004462 );
drha05a7222008-01-19 03:35:58 +00004463 pC->deferredMoveto = 0;
4464 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004465
drha05a7222008-01-19 03:35:58 +00004466 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004467 if( rc ) goto abort_due_to_error;
drhc556f3c2016-03-30 15:30:07 +00004468 if( db->xUpdateCallback && op ){
drh8eeb4462016-05-21 20:03:42 +00004469 db->xUpdateCallback(db->pUpdateArg, op, zDb, pTab->zName, x.nKey);
drha05a7222008-01-19 03:35:58 +00004470 }
drh5e00f6c2001-09-13 13:46:56 +00004471 break;
4472}
4473
dan438b8812015-09-15 15:55:15 +00004474/* Opcode: Delete P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004475**
drh5edc3122001-09-13 21:53:09 +00004476** Delete the record at which the P1 cursor is currently pointing.
4477**
drhe807bdb2016-01-21 17:06:33 +00004478** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4479** the cursor will be left pointing at either the next or the previous
4480** record in the table. If it is left pointing at the next record, then
4481** the next Next instruction will be a no-op. As a result, in this case
4482** it is ok to delete a record from within a Next loop. If
4483** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4484** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004485**
drhdef19e32016-01-27 16:26:25 +00004486** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4487** delete one of several associated with deleting a table row and all its
4488** associated index entries. Exactly one of those deletes is the "primary"
4489** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4490** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004491**
4492** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4493** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004494**
drh91fd4d42008-01-19 20:11:25 +00004495** P1 must not be pseudo-table. It has to be a real table with
4496** multiple rows.
4497**
drh5e769a52016-09-28 16:05:53 +00004498** If P4 is not NULL then it points to a Table object. In this case either
dan319eeb72011-03-19 08:38:50 +00004499** the update or pre-update hook, or both, may be invoked. The P1 cursor must
4500** have been positioned using OP_NotFound prior to invoking this opcode in
4501** this case. Specifically, if one is configured, the pre-update hook is
4502** invoked if P4 is not NULL. The update-hook is invoked if one is configured,
4503** P4 is not NULL, and the OPFLAG_NCHANGE flag is set in P2.
dan46c47d42011-03-01 18:42:07 +00004504**
4505** If the OPFLAG_ISUPDATE flag is set in P2, then P3 contains the address
4506** of the memory cell that contains the value that the rowid of the row will
4507** be set to by the update.
drh5e00f6c2001-09-13 13:46:56 +00004508*/
drh9cbf3422008-01-17 16:22:13 +00004509case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004510 VdbeCursor *pC;
dan46c47d42011-03-01 18:42:07 +00004511 const char *zDb;
dan319eeb72011-03-19 08:38:50 +00004512 Table *pTab;
dan46c47d42011-03-01 18:42:07 +00004513 int opflags;
drh91fd4d42008-01-19 20:11:25 +00004514
dan46c47d42011-03-01 18:42:07 +00004515 opflags = pOp->p2;
drh653b82a2009-06-22 11:10:47 +00004516 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4517 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004518 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004519 assert( pC->eCurType==CURTYPE_BTREE );
4520 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004521 assert( pC->deferredMoveto==0 );
drh9a65f2c2009-06-22 19:05:40 +00004522
drhb53a5a92014-10-12 22:37:22 +00004523#ifdef SQLITE_DEBUG
dan438b8812015-09-15 15:55:15 +00004524 if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
4525 /* If p5 is zero, the seek operation that positioned the cursor prior to
4526 ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
4527 ** the row that is being deleted */
drha7c90c42016-06-04 20:37:10 +00004528 i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh92fe38e2014-10-14 13:41:32 +00004529 assert( pC->movetoTarget==iKey );
drhb53a5a92014-10-12 22:37:22 +00004530 }
4531#endif
drh91fd4d42008-01-19 20:11:25 +00004532
dan438b8812015-09-15 15:55:15 +00004533 /* If the update-hook or pre-update-hook will be invoked, set zDb to
4534 ** the name of the db to pass as to it. Also set local pTab to a copy
4535 ** of p4.pTab. Finally, if p5 is true, indicating that this cursor was
4536 ** last moved with OP_Next or OP_Prev, not Seek or NotFound, set
4537 ** VdbeCursor.movetoTarget to the current rowid. */
drhc556f3c2016-03-30 15:30:07 +00004538 if( pOp->p4type==P4_TABLE && HAS_UPDATE_HOOK(db) ){
dan46c47d42011-03-01 18:42:07 +00004539 assert( pC->iDb>=0 );
drhc556f3c2016-03-30 15:30:07 +00004540 assert( pOp->p4.pTab!=0 );
drh69c33822016-08-18 14:33:11 +00004541 zDb = db->aDb[pC->iDb].zDbSName;
dan319eeb72011-03-19 08:38:50 +00004542 pTab = pOp->p4.pTab;
drhc556f3c2016-03-30 15:30:07 +00004543 if( (pOp->p5 & OPFLAG_SAVEPOSITION)!=0 && pC->isTable ){
drha7c90c42016-06-04 20:37:10 +00004544 pC->movetoTarget = sqlite3BtreeIntegerKey(pC->uc.pCursor);
dan438b8812015-09-15 15:55:15 +00004545 }
drh74c33022016-03-30 12:56:55 +00004546 }else{
4547 zDb = 0; /* Not needed. Silence a compiler warning. */
4548 pTab = 0; /* Not needed. Silence a compiler warning. */
drh92fe38e2014-10-14 13:41:32 +00004549 }
dan46c47d42011-03-01 18:42:07 +00004550
drh9b1c62d2011-03-30 21:04:43 +00004551#ifdef SQLITE_ENABLE_PREUPDATE_HOOK
dan46c47d42011-03-01 18:42:07 +00004552 /* Invoke the pre-update-hook if required. */
dancb9a3642017-01-30 19:44:53 +00004553 if( db->xPreUpdateCallback && pOp->p4.pTab ){
4554 assert( !(opflags & OPFLAG_ISUPDATE)
4555 || HasRowid(pTab)==0
4556 || (aMem[pOp->p3].flags & MEM_Int)
4557 );
dan46c47d42011-03-01 18:42:07 +00004558 sqlite3VdbePreUpdateHook(p, pC,
4559 (opflags & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_DELETE,
drh92fe38e2014-10-14 13:41:32 +00004560 zDb, pTab, pC->movetoTarget,
dan37db03b2011-03-16 19:59:18 +00004561 pOp->p3
dan46c47d42011-03-01 18:42:07 +00004562 );
4563 }
dan46c47d42011-03-01 18:42:07 +00004564 if( opflags & OPFLAG_ISNOOP ) break;
drhc556f3c2016-03-30 15:30:07 +00004565#endif
drhb53a5a92014-10-12 22:37:22 +00004566
drhdef19e32016-01-27 16:26:25 +00004567 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4568 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004569 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004570 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004571
4572#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004573 if( p->pFrame==0 ){
4574 if( pC->isEphemeral==0
4575 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4576 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4577 ){
4578 nExtraDelete++;
4579 }
4580 if( pOp->p2 & OPFLAG_NCHANGE ){
4581 nExtraDelete--;
4582 }
drhb89aeb62016-01-27 15:49:32 +00004583 }
4584#endif
4585
drhc960dcb2015-11-20 19:22:01 +00004586 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004587 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00004588 pC->seekResult = 0;
drhd3e1af42016-02-25 18:54:30 +00004589 if( rc ) goto abort_due_to_error;
danielk197794eb6a12005-12-15 15:22:08 +00004590
drh91fd4d42008-01-19 20:11:25 +00004591 /* Invoke the update-hook if required. */
dan46c47d42011-03-01 18:42:07 +00004592 if( opflags & OPFLAG_NCHANGE ){
4593 p->nChange++;
drhc556f3c2016-03-30 15:30:07 +00004594 if( db->xUpdateCallback && HasRowid(pTab) ){
drh92fe38e2014-10-14 13:41:32 +00004595 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, pTab->zName,
dan438b8812015-09-15 15:55:15 +00004596 pC->movetoTarget);
4597 assert( pC->iDb>=0 );
dan46c47d42011-03-01 18:42:07 +00004598 }
drh5e00f6c2001-09-13 13:46:56 +00004599 }
dan438b8812015-09-15 15:55:15 +00004600
rdcb0c374f2004-02-20 22:53:38 +00004601 break;
4602}
drhb7f1d9a2009-09-08 02:27:58 +00004603/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004604**
drhb7f1d9a2009-09-08 02:27:58 +00004605** The value of the change counter is copied to the database handle
4606** change counter (returned by subsequent calls to sqlite3_changes()).
4607** Then the VMs internal change counter resets to 0.
4608** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004609*/
drh9cbf3422008-01-17 16:22:13 +00004610case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004611 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004612 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004613 break;
4614}
4615
drh1153c7b2013-11-01 22:02:56 +00004616/* Opcode: SorterCompare P1 P2 P3 P4
drh72e26de2016-08-24 21:24:04 +00004617** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004618**
drh1153c7b2013-11-01 22:02:56 +00004619** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004620** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004621** the sorter cursor currently points to. Only the first P4 fields
4622** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004623**
4624** If either P3 or the sorter contains a NULL in one of their significant
4625** fields (not counting the P4 fields at the end which are ignored) then
4626** the comparison is assumed to be equal.
4627**
4628** Fall through to next instruction if the two records compare equal to
4629** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004630*/
4631case OP_SorterCompare: {
4632 VdbeCursor *pC;
4633 int res;
drhac502322014-07-30 13:56:48 +00004634 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004635
4636 pC = p->apCsr[pOp->p1];
4637 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004638 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004639 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004640 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004641 res = 0;
drhac502322014-07-30 13:56:48 +00004642 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004643 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004644 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004645 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004646 break;
4647};
4648
drh6cf4a7d2014-10-13 13:00:58 +00004649/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004650** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004651**
4652** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004653** Then clear the column header cache on cursor P3.
4654**
4655** This opcode is normally use to move a record out of the sorter and into
4656** a register that is the source for a pseudo-table cursor created using
4657** OpenPseudo. That pseudo-table cursor is the one that is identified by
4658** parameter P3. Clearing the P3 column cache as part of this opcode saves
4659** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004660*/
4661case OP_SorterData: {
4662 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004663
dan5134d132011-09-02 10:31:11 +00004664 pOut = &aMem[pOp->p2];
4665 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004666 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004667 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004668 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004669 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004670 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004671 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004672 break;
4673}
4674
drhe7b554d2017-01-09 15:44:25 +00004675/* Opcode: RowData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004676** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004677**
drh9057fc72016-11-25 19:32:32 +00004678** Write into register P2 the complete row content for the row at
4679** which cursor P1 is currently pointing.
drh98757152008-01-09 23:04:12 +00004680** There is no interpretation of the data.
4681** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004682** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004683**
drh9057fc72016-11-25 19:32:32 +00004684** If cursor P1 is an index, then the content is the key of the row.
4685** If cursor P2 is a table, then the content extracted is the data.
drh143f3c42004-01-07 20:37:52 +00004686**
drhde4fcfd2008-01-19 23:50:26 +00004687** If the P1 cursor must be pointing to a valid row (not a NULL row)
4688** of a real table, not a pseudo-table.
drhe7b554d2017-01-09 15:44:25 +00004689**
4690** If P3!=0 then this opcode is allowed to make an ephermeral pointer
4691** into the database page. That means that the content of the output
4692** register will be invalidated as soon as the cursor moves - including
4693** moves caused by other cursors that "save" the the current cursors
4694** position in order that they can write to the same table. If P3==0
4695** then a copy of the data is made into memory. P3!=0 is faster, but
4696** P3==0 is safer.
4697**
4698** If P3!=0 then the content of the P2 register is unsuitable for use
4699** in OP_Result and any OP_Result will invalidate the P2 register content.
mistachkinab61cf72017-01-09 18:22:54 +00004700** The P2 register content is invalidated by opcodes like OP_Function or
drhe7b554d2017-01-09 15:44:25 +00004701** by any use of another cursor pointing to the same table.
drh143f3c42004-01-07 20:37:52 +00004702*/
danielk1977a7a8e142008-02-13 18:25:27 +00004703case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004704 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004705 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004706 u32 n;
drh70ce3f02003-04-15 19:22:22 +00004707
drhe7b554d2017-01-09 15:44:25 +00004708 pOut = out2Prerelease(p, pOp);
danielk1977a7a8e142008-02-13 18:25:27 +00004709
drh653b82a2009-06-22 11:10:47 +00004710 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4711 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004712 assert( pC!=0 );
4713 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004714 assert( isSorter(pC)==0 );
drhde4fcfd2008-01-19 23:50:26 +00004715 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004716 assert( pC->uc.pCursor!=0 );
4717 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004718
drh9057fc72016-11-25 19:32:32 +00004719 /* The OP_RowData opcodes always follow OP_NotExists or
drheeb95652016-05-26 20:56:38 +00004720 ** OP_SeekRowid or OP_Rewind/Op_Next with no intervening instructions
4721 ** that might invalidate the cursor.
4722 ** If this where not the case, on of the following assert()s
drhc22284f2014-10-13 16:02:20 +00004723 ** would fail. Should this ever change (because of changes in the code
4724 ** generator) then the fix would be to insert a call to
4725 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004726 */
4727 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004728 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4729#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004730 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004731 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4732#endif
drh9a65f2c2009-06-22 19:05:40 +00004733
drha7c90c42016-06-04 20:37:10 +00004734 n = sqlite3BtreePayloadSize(pCrsr);
drhd66c4f82016-06-04 20:58:35 +00004735 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drha7c90c42016-06-04 20:37:10 +00004736 goto too_big;
drhde4fcfd2008-01-19 23:50:26 +00004737 }
drh722246e2014-10-07 23:02:24 +00004738 testcase( n==0 );
drhe7b554d2017-01-09 15:44:25 +00004739 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, n, pOut);
drh9467abf2016-02-17 18:44:11 +00004740 if( rc ) goto abort_due_to_error;
drhe7b554d2017-01-09 15:44:25 +00004741 if( !pOp->p3 ) Deephemeralize(pOut);
drhb7654112008-01-12 12:48:07 +00004742 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004743 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004744 break;
4745}
4746
drh2133d822008-01-03 18:44:59 +00004747/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004748** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004749**
drh2133d822008-01-03 18:44:59 +00004750** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004751** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004752**
4753** P1 can be either an ordinary table or a virtual table. There used to
4754** be a separate OP_VRowid opcode for use with virtual tables, but this
4755** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004756*/
drh27a348c2015-04-13 19:14:06 +00004757case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004758 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004759 i64 v;
drh856c1032009-06-02 15:21:42 +00004760 sqlite3_vtab *pVtab;
4761 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004762
drh27a348c2015-04-13 19:14:06 +00004763 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004764 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4765 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004766 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004767 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004768 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004769 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004770 break;
4771 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004772 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004773#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004774 }else if( pC->eCurType==CURTYPE_VTAB ){
4775 assert( pC->uc.pVCur!=0 );
4776 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004777 pModule = pVtab->pModule;
4778 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004779 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004780 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004781 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004782#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004783 }else{
drhc960dcb2015-11-20 19:22:01 +00004784 assert( pC->eCurType==CURTYPE_BTREE );
4785 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004786 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004787 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004788 if( pC->nullRow ){
4789 pOut->flags = MEM_Null;
4790 break;
4791 }
drha7c90c42016-06-04 20:37:10 +00004792 v = sqlite3BtreeIntegerKey(pC->uc.pCursor);
drh5e00f6c2001-09-13 13:46:56 +00004793 }
drh4c583122008-01-04 22:01:03 +00004794 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004795 break;
4796}
4797
drh9cbf3422008-01-17 16:22:13 +00004798/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004799**
4800** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004801** that occur while the cursor is on the null row will always
4802** write a NULL.
drh17f71932002-02-21 12:01:27 +00004803*/
drh9cbf3422008-01-17 16:22:13 +00004804case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004805 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004806
drh653b82a2009-06-22 11:10:47 +00004807 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4808 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004809 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004810 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004811 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00004812 if( pC->eCurType==CURTYPE_BTREE ){
4813 assert( pC->uc.pCursor!=0 );
4814 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00004815 }
drh17f71932002-02-21 12:01:27 +00004816 break;
4817}
4818
danb18e60b2015-04-01 16:18:00 +00004819/* Opcode: Last P1 P2 P3 * *
drh9562b552002-02-19 15:00:07 +00004820**
drh8af3f772014-07-25 18:01:06 +00004821** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004822** will refer to the last entry in the database table or index.
4823** If the table or index is empty and P2>0, then jump immediately to P2.
4824** If P2 is 0 or if the table or index is not empty, fall through
4825** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004826**
4827** This opcode leaves the cursor configured to move in reverse order,
4828** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004829** configured to use Prev, not Next.
drhd6ef5af2016-11-15 04:00:24 +00004830**
4831** If P3 is -1, then the cursor is positioned at the end of the btree
4832** for the purpose of appending a new entry onto the btree. In that
4833** case P2 must be 0. It is assumed that the cursor is used only for
4834** appending and so if the cursor is valid, then the cursor must already
4835** be pointing at the end of the btree and so no changes are made to
4836** the cursor.
drh9562b552002-02-19 15:00:07 +00004837*/
drh9cbf3422008-01-17 16:22:13 +00004838case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004839 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004840 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004841 int res;
drh9562b552002-02-19 15:00:07 +00004842
drh653b82a2009-06-22 11:10:47 +00004843 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4844 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004845 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004846 assert( pC->eCurType==CURTYPE_BTREE );
4847 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00004848 res = 0;
drh3da046d2013-11-11 03:24:11 +00004849 assert( pCrsr!=0 );
danb18e60b2015-04-01 16:18:00 +00004850 pC->seekResult = pOp->p3;
drh8af3f772014-07-25 18:01:06 +00004851#ifdef SQLITE_DEBUG
4852 pC->seekOp = OP_Last;
4853#endif
drhd6ef5af2016-11-15 04:00:24 +00004854 if( pOp->p3==0 || !sqlite3BtreeCursorIsValidNN(pCrsr) ){
4855 rc = sqlite3BtreeLast(pCrsr, &res);
4856 pC->nullRow = (u8)res;
4857 pC->deferredMoveto = 0;
4858 pC->cacheStatus = CACHE_STALE;
4859 if( rc ) goto abort_due_to_error;
4860 if( pOp->p2>0 ){
4861 VdbeBranchTaken(res!=0,2);
4862 if( res ) goto jump_to_p2;
4863 }
4864 }else{
4865 assert( pOp->p2==0 );
drh9562b552002-02-19 15:00:07 +00004866 }
4867 break;
4868}
4869
drh5e98e832017-02-17 19:24:06 +00004870/* Opcode: IfSmaller P1 P2 P3 * *
4871**
4872** Estimate the number of rows in the table P1. Jump to P2 if that
4873** estimate is less than approximately 2**(0.1*P3).
4874*/
4875case OP_IfSmaller: { /* jump */
4876 VdbeCursor *pC;
4877 BtCursor *pCrsr;
4878 int res;
4879 i64 sz;
4880
4881 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4882 pC = p->apCsr[pOp->p1];
4883 assert( pC!=0 );
4884 pCrsr = pC->uc.pCursor;
4885 assert( pCrsr );
4886 rc = sqlite3BtreeFirst(pCrsr, &res);
4887 if( rc ) goto abort_due_to_error;
4888 if( res==0 ){
4889 sz = sqlite3BtreeRowCountEst(pCrsr);
4890 if( ALWAYS(sz>=0) && sqlite3LogEst((u64)sz)<pOp->p3 ) res = 1;
4891 }
4892 VdbeBranchTaken(res!=0,2);
4893 if( res ) goto jump_to_p2;
4894 break;
4895}
4896
drh0342b1f2005-09-01 03:07:44 +00004897
drh6bd4dc62016-12-23 16:05:22 +00004898/* Opcode: SorterSort P1 P2 * * *
4899**
4900** After all records have been inserted into the Sorter object
4901** identified by P1, invoke this opcode to actually do the sorting.
4902** Jump to P2 if there are no records to be sorted.
4903**
4904** This opcode is an alias for OP_Sort and OP_Rewind that is used
4905** for Sorter objects.
4906*/
drh9cbf3422008-01-17 16:22:13 +00004907/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004908**
4909** This opcode does exactly the same thing as OP_Rewind except that
4910** it increments an undocumented global variable used for testing.
4911**
4912** Sorting is accomplished by writing records into a sorting index,
4913** then rewinding that index and playing it back from beginning to
4914** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4915** rewinding so that the global variable will be incremented and
4916** regression tests can determine whether or not the optimizer is
4917** correctly optimizing out sorts.
4918*/
drhc6aff302011-09-01 15:32:47 +00004919case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004920case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004921#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004922 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004923 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004924#endif
drh9b47ee32013-08-20 03:13:51 +00004925 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004926 /* Fall through into OP_Rewind */
4927}
drh9cbf3422008-01-17 16:22:13 +00004928/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004929**
drhf0863fe2005-06-12 21:35:51 +00004930** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004931** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00004932** If the table or index is empty, jump immediately to P2.
4933** If the table or index is not empty, fall through to the following
4934** instruction.
drh8af3f772014-07-25 18:01:06 +00004935**
4936** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004937** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004938** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004939*/
drh9cbf3422008-01-17 16:22:13 +00004940case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004941 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004942 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004943 int res;
drh5e00f6c2001-09-13 13:46:56 +00004944
drh653b82a2009-06-22 11:10:47 +00004945 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4946 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004947 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004948 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004949 res = 1;
drh8af3f772014-07-25 18:01:06 +00004950#ifdef SQLITE_DEBUG
4951 pC->seekOp = OP_Rewind;
4952#endif
dan689ab892011-08-12 15:02:00 +00004953 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004954 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004955 }else{
drhc960dcb2015-11-20 19:22:01 +00004956 assert( pC->eCurType==CURTYPE_BTREE );
4957 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00004958 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004959 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004960 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004961 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00004962 }
drh9467abf2016-02-17 18:44:11 +00004963 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00004964 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004965 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004966 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004967 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004968 break;
4969}
4970
drh0fd61352014-02-07 02:29:45 +00004971/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004972**
4973** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004974** table or index. If there are no more key/value pairs then fall through
4975** to the following instruction. But if the cursor advance was successful,
4976** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004977**
drh5dad9a32014-07-25 18:37:42 +00004978** The Next opcode is only valid following an SeekGT, SeekGE, or
4979** OP_Rewind opcode used to position the cursor. Next is not allowed
4980** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00004981**
drhf93cd942013-11-21 03:12:25 +00004982** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4983** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004984**
drhe39a7322014-02-03 14:04:11 +00004985** The P3 value is a hint to the btree implementation. If P3==1, that
4986** means P1 is an SQL index and that this instruction could have been
4987** omitted if that index had been unique. P3 is usually 0. P3 is
4988** always either 0 or 1.
4989**
dana205a482011-08-27 18:48:57 +00004990** P4 is always of type P4_ADVANCE. The function pointer points to
4991** sqlite3BtreeNext().
4992**
drhafc266a2010-03-31 17:47:44 +00004993** If P5 is positive and the jump is taken, then event counter
4994** number P5-1 in the prepared statement is incremented.
4995**
drhf93cd942013-11-21 03:12:25 +00004996** See also: Prev, NextIfOpen
4997*/
drh0fd61352014-02-07 02:29:45 +00004998/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004999**
drh5dad9a32014-07-25 18:37:42 +00005000** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00005001** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00005002*/
drh0fd61352014-02-07 02:29:45 +00005003/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00005004**
5005** Back up cursor P1 so that it points to the previous key/data pair in its
5006** table or index. If there is no previous key/value pairs then fall through
5007** to the following instruction. But if the cursor backup was successful,
5008** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00005009**
drh8af3f772014-07-25 18:01:06 +00005010**
drh5dad9a32014-07-25 18:37:42 +00005011** The Prev opcode is only valid following an SeekLT, SeekLE, or
5012** OP_Last opcode used to position the cursor. Prev is not allowed
5013** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00005014**
drhf93cd942013-11-21 03:12:25 +00005015** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
5016** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00005017**
drhe39a7322014-02-03 14:04:11 +00005018** The P3 value is a hint to the btree implementation. If P3==1, that
5019** means P1 is an SQL index and that this instruction could have been
5020** omitted if that index had been unique. P3 is usually 0. P3 is
5021** always either 0 or 1.
5022**
dana205a482011-08-27 18:48:57 +00005023** P4 is always of type P4_ADVANCE. The function pointer points to
5024** sqlite3BtreePrevious().
5025**
drhafc266a2010-03-31 17:47:44 +00005026** If P5 is positive and the jump is taken, then event counter
5027** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00005028*/
drh0fd61352014-02-07 02:29:45 +00005029/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00005030**
drh5dad9a32014-07-25 18:37:42 +00005031** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00005032** open it behaves a no-op.
5033*/
drh6bd4dc62016-12-23 16:05:22 +00005034/* Opcode: SorterNext P1 P2 * * P5
5035**
5036** This opcode works just like OP_Next except that P1 must be a
5037** sorter object for which the OP_SorterSort opcode has been
5038** invoked. This opcode advances the cursor to the next sorted
5039** record, or jumps to P2 if there are no more sorted records.
5040*/
drhf93cd942013-11-21 03:12:25 +00005041case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005042 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00005043 int res;
drh8721ce42001-11-07 14:22:00 +00005044
drhf93cd942013-11-21 03:12:25 +00005045 pC = p->apCsr[pOp->p1];
5046 assert( isSorter(pC) );
drh323913c2014-03-23 16:29:23 +00005047 res = 0;
drhf93cd942013-11-21 03:12:25 +00005048 rc = sqlite3VdbeSorterNext(db, pC, &res);
5049 goto next_tail;
5050case OP_PrevIfOpen: /* jump */
5051case OP_NextIfOpen: /* jump */
5052 if( p->apCsr[pOp->p1]==0 ) break;
5053 /* Fall through */
5054case OP_Prev: /* jump */
5055case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00005056 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00005057 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00005058 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00005059 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00005060 assert( pC!=0 );
5061 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00005062 assert( pC->eCurType==CURTYPE_BTREE );
drhe39a7322014-02-03 14:04:11 +00005063 assert( res==0 || (res==1 && pC->isTable==0) );
5064 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00005065 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
5066 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
5067 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
5068 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00005069
5070 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
5071 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
5072 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
5073 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00005074 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00005075 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
5076 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
5077 || pC->seekOp==OP_Last );
5078
drhc960dcb2015-11-20 19:22:01 +00005079 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
drhf93cd942013-11-21 03:12:25 +00005080next_tail:
drha3460582008-07-11 21:02:53 +00005081 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00005082 VdbeBranchTaken(res==0,2);
drh9467abf2016-02-17 18:44:11 +00005083 if( rc ) goto abort_due_to_error;
drha3460582008-07-11 21:02:53 +00005084 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00005085 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00005086 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00005087#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00005088 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00005089#endif
drhf56fa462015-04-13 21:39:54 +00005090 goto jump_to_p2_and_check_for_interrupt;
drhf93cd942013-11-21 03:12:25 +00005091 }else{
5092 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00005093 }
drh49afe3a2013-07-10 03:05:14 +00005094 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00005095}
5096
drh9b4eaeb2016-11-09 00:10:33 +00005097/* Opcode: IdxInsert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00005098** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005099**
drhef8662b2011-06-20 21:47:58 +00005100** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00005101** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00005102** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00005103**
drhfb8c56f2016-11-09 01:19:25 +00005104** If P4 is not zero, then it is the number of values in the unpacked
drh9b4eaeb2016-11-09 00:10:33 +00005105** key of reg(P2). In that case, P3 is the index of the first register
5106** for the unpacked key. The availability of the unpacked key can sometimes
5107** be an optimization.
5108**
5109** If P5 has the OPFLAG_APPEND bit set, that is a hint to the b-tree layer
5110** that this insert is likely to be an append.
drhe4d90812007-03-29 05:51:49 +00005111**
mistachkin21a919f2014-02-07 03:28:02 +00005112** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
5113** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
5114** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00005115**
drheaf6ae22016-11-09 20:14:34 +00005116** If the OPFLAG_USESEEKRESULT flag of P5 is set, the implementation might
5117** run faster by avoiding an unnecessary seek on cursor P1. However,
5118** the OPFLAG_USESEEKRESULT flag must only be set if there have been no prior
5119** seeks on the cursor or if the most recent seek used a key equivalent
5120** to P2.
drh0fd61352014-02-07 02:29:45 +00005121**
drhf0863fe2005-06-12 21:35:51 +00005122** This instruction only works for indices. The equivalent instruction
5123** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00005124*/
drhf013e202016-10-15 18:37:05 +00005125/* Opcode: SorterInsert P1 P2 * * *
5126** Synopsis: key=r[P2]
5127**
5128** Register P2 holds an SQL index key made using the
5129** MakeRecord instructions. This opcode writes that key
5130** into the sorter P1. Data for the entry is nil.
5131*/
drhca892a72011-09-03 00:17:51 +00005132case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00005133case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00005134 VdbeCursor *pC;
drh8eeb4462016-05-21 20:03:42 +00005135 BtreePayload x;
drh856c1032009-06-02 15:21:42 +00005136
drh653b82a2009-06-22 11:10:47 +00005137 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5138 pC = p->apCsr[pOp->p1];
5139 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00005140 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00005141 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00005142 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00005143 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00005144 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00005145 assert( pC->isTable==0 );
5146 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00005147 if( rc ) goto abort_due_to_error;
5148 if( pOp->opcode==OP_SorterInsert ){
5149 rc = sqlite3VdbeSorterWrite(pC, pIn2);
5150 }else{
drh8eeb4462016-05-21 20:03:42 +00005151 x.nKey = pIn2->n;
5152 x.pKey = pIn2->z;
drh9b4eaeb2016-11-09 00:10:33 +00005153 x.aMem = aMem + pOp->p3;
5154 x.nMem = (u16)pOp->p4.i;
5155 rc = sqlite3BtreeInsert(pC->uc.pCursor, &x,
danf91c1312017-01-10 20:04:38 +00005156 (pOp->p5 & (OPFLAG_APPEND|OPFLAG_SAVEPOSITION)),
drh9467abf2016-02-17 18:44:11 +00005157 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
5158 );
5159 assert( pC->deferredMoveto==0 );
5160 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00005161 }
drh9467abf2016-02-17 18:44:11 +00005162 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005163 break;
5164}
5165
drhd1d38482008-10-07 23:46:38 +00005166/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00005167** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00005168**
drhe14006d2008-03-25 17:23:32 +00005169** The content of P3 registers starting at register P2 form
5170** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00005171** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00005172*/
drhe14006d2008-03-25 17:23:32 +00005173case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00005174 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00005175 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00005176 int res;
5177 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00005178
drhe14006d2008-03-25 17:23:32 +00005179 assert( pOp->p3>0 );
drh9f6168b2016-03-19 23:32:58 +00005180 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00005181 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5182 pC = p->apCsr[pOp->p1];
5183 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005184 assert( pC->eCurType==CURTYPE_BTREE );
5185 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00005186 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00005187 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00005188 r.pKeyInfo = pC->pKeyInfo;
5189 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00005190 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005191 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00005192 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00005193 if( rc ) goto abort_due_to_error;
5194 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00005195 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00005196 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00005197 }
drh3da046d2013-11-11 03:24:11 +00005198 assert( pC->deferredMoveto==0 );
5199 pC->cacheStatus = CACHE_STALE;
dan3b908d42016-11-08 19:22:32 +00005200 pC->seekResult = 0;
drh5e00f6c2001-09-13 13:46:56 +00005201 break;
5202}
5203
drh784c1b92016-01-30 16:59:56 +00005204/* Opcode: Seek P1 * P3 P4 *
drh72e26de2016-08-24 21:24:04 +00005205** Synopsis: Move P3 to P1.rowid
drh784c1b92016-01-30 16:59:56 +00005206**
5207** P1 is an open index cursor and P3 is a cursor on the corresponding
5208** table. This opcode does a deferred seek of the P3 table cursor
5209** to the row that corresponds to the current row of P1.
5210**
5211** This is a deferred seek. Nothing actually happens until
5212** the cursor is used to read a record. That way, if no reads
5213** occur, no unnecessary I/O happens.
5214**
5215** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00005216** one entry for each column in the P3 table. If array entry a(i)
5217** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00005218** equivalent to performing the deferred seek and then reading column i
5219** from P1. This information is stored in P3 and used to redirect
5220** reads against P3 over to P1, thus possibly avoiding the need to
5221** seek and read cursor P3.
5222*/
drh2133d822008-01-03 18:44:59 +00005223/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005224** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00005225**
drh2133d822008-01-03 18:44:59 +00005226** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00005227** the end of the index key pointed to by cursor P1. This integer should be
5228** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00005229**
drh9437bd22009-02-01 00:29:56 +00005230** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005231*/
drh784c1b92016-01-30 16:59:56 +00005232case OP_Seek:
drh27a348c2015-04-13 19:14:06 +00005233case OP_IdxRowid: { /* out2 */
drh784c1b92016-01-30 16:59:56 +00005234 VdbeCursor *pC; /* The P1 index cursor */
5235 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
5236 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005237
drh653b82a2009-06-22 11:10:47 +00005238 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5239 pC = p->apCsr[pOp->p1];
5240 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005241 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005242 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005243 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005244 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005245 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5246
5247 /* The IdxRowid and Seek opcodes are combined because of the commonality
5248 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5249 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005250
5251 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005252 ** out from under the cursor. That will never happens for an IdxRowid
5253 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005254 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5255
drh3da046d2013-11-11 03:24:11 +00005256 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005257 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005258 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005259 if( rc!=SQLITE_OK ){
5260 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005261 }
drh784c1b92016-01-30 16:59:56 +00005262 if( pOp->opcode==OP_Seek ){
5263 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5264 pTabCur = p->apCsr[pOp->p3];
5265 assert( pTabCur!=0 );
5266 assert( pTabCur->eCurType==CURTYPE_BTREE );
5267 assert( pTabCur->uc.pCursor!=0 );
5268 assert( pTabCur->isTable );
5269 pTabCur->nullRow = 0;
5270 pTabCur->movetoTarget = rowid;
5271 pTabCur->deferredMoveto = 1;
5272 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5273 pTabCur->aAltMap = pOp->p4.ai;
5274 pTabCur->pAltCursor = pC;
5275 }else{
5276 pOut = out2Prerelease(p, pOp);
5277 pOut->u.i = rowid;
drh784c1b92016-01-30 16:59:56 +00005278 }
5279 }else{
5280 assert( pOp->opcode==OP_IdxRowid );
5281 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005282 }
5283 break;
5284}
5285
danielk197761dd5832008-04-18 11:31:12 +00005286/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005287** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005288**
danielk197761dd5832008-04-18 11:31:12 +00005289** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005290** key that omits the PRIMARY KEY. Compare this key value against the index
5291** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5292** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005293**
danielk197761dd5832008-04-18 11:31:12 +00005294** If the P1 index entry is greater than or equal to the key value
5295** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005296*/
5297/* Opcode: IdxGT P1 P2 P3 P4 P5
5298** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005299**
drh4a1d3652014-02-14 15:13:36 +00005300** The P4 register values beginning with P3 form an unpacked index
5301** key that omits the PRIMARY KEY. Compare this key value against the index
5302** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5303** fields at the end.
5304**
5305** If the P1 index entry is greater than the key value
5306** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005307*/
drh3bb9b932010-08-06 02:10:00 +00005308/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005309** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005310**
danielk197761dd5832008-04-18 11:31:12 +00005311** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005312** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5313** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5314** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005315**
danielk197761dd5832008-04-18 11:31:12 +00005316** If the P1 index entry is less than the key value then jump to P2.
5317** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005318*/
drh4a1d3652014-02-14 15:13:36 +00005319/* Opcode: IdxLE P1 P2 P3 P4 P5
5320** Synopsis: key=r[P3@P4]
5321**
5322** The P4 register values beginning with P3 form an unpacked index
5323** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5324** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5325** ROWID on the P1 index.
5326**
5327** If the P1 index entry is less than or equal to the key value then jump
5328** to P2. Otherwise fall through to the next instruction.
5329*/
5330case OP_IdxLE: /* jump */
5331case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005332case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005333case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005334 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005335 int res;
5336 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005337
drh653b82a2009-06-22 11:10:47 +00005338 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5339 pC = p->apCsr[pOp->p1];
5340 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005341 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005342 assert( pC->eCurType==CURTYPE_BTREE );
5343 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005344 assert( pC->deferredMoveto==0 );
5345 assert( pOp->p5==0 || pOp->p5==1 );
5346 assert( pOp->p4type==P4_INT32 );
5347 r.pKeyInfo = pC->pKeyInfo;
5348 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005349 if( pOp->opcode<OP_IdxLT ){
5350 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005351 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005352 }else{
drh4a1d3652014-02-14 15:13:36 +00005353 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005354 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005355 }
5356 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005357#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00005358 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005359#endif
drh2dc06482013-12-11 00:59:10 +00005360 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005361 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005362 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5363 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5364 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005365 res = -res;
5366 }else{
drh4a1d3652014-02-14 15:13:36 +00005367 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005368 res++;
5369 }
drh688852a2014-02-17 22:40:43 +00005370 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005371 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005372 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005373 break;
5374}
5375
drh98757152008-01-09 23:04:12 +00005376/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005377**
5378** Delete an entire database table or index whose root page in the database
5379** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005380**
drh98757152008-01-09 23:04:12 +00005381** The table being destroyed is in the main database file if P3==0. If
5382** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005383** that is used to store tables create using CREATE TEMPORARY TABLE.
5384**
drh205f48e2004-11-05 00:43:11 +00005385** If AUTOVACUUM is enabled then it is possible that another root page
5386** might be moved into the newly deleted root page in order to keep all
5387** root pages contiguous at the beginning of the database. The former
5388** value of the root page that moved - its value before the move occurred -
dana34adaf2017-04-08 14:11:47 +00005389** is stored in register P2. If no page movement was required (because the
5390** table being dropped was already the last one in the database) then a
5391** zero is stored in register P2. If AUTOVACUUM is disabled then a zero
5392** is stored in register P2.
5393**
5394** This opcode throws an error if there are any active reader VMs when
5395** it is invoked. This is done to avoid the difficulty associated with
5396** updating existing cursors when a root page is moved in an AUTOVACUUM
5397** database. This error is thrown even if the database is not an AUTOVACUUM
5398** db in order to avoid introducing an incompatibility between autovacuum
5399** and non-autovacuum modes.
drh205f48e2004-11-05 00:43:11 +00005400**
drhb19a2bc2001-09-16 00:13:26 +00005401** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005402*/
drh27a348c2015-04-13 19:14:06 +00005403case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005404 int iMoved;
drh856c1032009-06-02 15:21:42 +00005405 int iDb;
drh3a949872012-09-18 13:20:13 +00005406
drh9e92a472013-06-27 17:40:30 +00005407 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005408 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005409 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005410 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005411 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005412 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005413 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005414 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005415 }else{
drh856c1032009-06-02 15:21:42 +00005416 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005417 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005418 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005419 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005420 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005421 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005422 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005423#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005424 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005425 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5426 /* All OP_Destroy operations occur on the same btree */
5427 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5428 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005429 }
drh3765df42006-06-28 18:18:09 +00005430#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005431 }
drh5e00f6c2001-09-13 13:46:56 +00005432 break;
5433}
5434
danielk1977c7af4842008-10-27 13:59:33 +00005435/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005436**
5437** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005438** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005439** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005440**
drhf57b3392001-10-08 13:22:32 +00005441** The table being clear is in the main database file if P2==0. If
5442** P2==1 then the table to be clear is in the auxiliary database file
5443** that is used to store tables create using CREATE TEMPORARY TABLE.
5444**
shanebe217792009-03-05 04:20:31 +00005445** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005446** intkey table (an SQL table, not an index). In this case the row change
5447** count is incremented by the number of rows in the table being cleared.
5448** If P3 is greater than zero, then the value stored in register P3 is
5449** also incremented by the number of rows in the table being cleared.
5450**
drhb19a2bc2001-09-16 00:13:26 +00005451** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005452*/
drh9cbf3422008-01-17 16:22:13 +00005453case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005454 int nChange;
5455
5456 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005457 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005458 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005459 rc = sqlite3BtreeClearTable(
5460 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5461 );
5462 if( pOp->p3 ){
5463 p->nChange += nChange;
5464 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005465 assert( memIsValid(&aMem[pOp->p3]) );
5466 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005467 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005468 }
5469 }
drh9467abf2016-02-17 18:44:11 +00005470 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005471 break;
5472}
5473
drh65ea12c2014-03-19 17:41:36 +00005474/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005475**
drh65ea12c2014-03-19 17:41:36 +00005476** Delete all contents from the ephemeral table or sorter
5477** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005478**
drh65ea12c2014-03-19 17:41:36 +00005479** This opcode only works for cursors used for sorting and
5480** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005481*/
drh65ea12c2014-03-19 17:41:36 +00005482case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005483 VdbeCursor *pC;
5484
5485 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5486 pC = p->apCsr[pOp->p1];
5487 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005488 if( isSorter(pC) ){
5489 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005490 }else{
drhc960dcb2015-11-20 19:22:01 +00005491 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005492 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005493 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005494 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005495 }
drh079a3072014-03-19 14:10:55 +00005496 break;
5497}
5498
drh4c583122008-01-04 22:01:03 +00005499/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005500** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00005501**
drh4c583122008-01-04 22:01:03 +00005502** Allocate a new table in the main database file if P1==0 or in the
5503** auxiliary database file if P1==1 or in an attached database if
5504** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005505** register P2
drh5b2fd562001-09-13 15:21:31 +00005506**
drhc6b52df2002-01-04 03:09:29 +00005507** The difference between a table and an index is this: A table must
5508** have a 4-byte integer key and can have arbitrary data. An index
5509** has an arbitrary key but no data.
5510**
drhb19a2bc2001-09-16 00:13:26 +00005511** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00005512*/
drh4c583122008-01-04 22:01:03 +00005513/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005514** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00005515**
drh4c583122008-01-04 22:01:03 +00005516** Allocate a new index in the main database file if P1==0 or in the
5517** auxiliary database file if P1==1 or in an attached database if
5518** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005519** register P2.
drhf57b3392001-10-08 13:22:32 +00005520**
drhc6b52df2002-01-04 03:09:29 +00005521** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00005522*/
drh27a348c2015-04-13 19:14:06 +00005523case OP_CreateIndex: /* out2 */
5524case OP_CreateTable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005525 int pgno;
drhf328bc82004-05-10 23:29:49 +00005526 int flags;
drh234c39d2004-07-24 03:30:47 +00005527 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005528
drh27a348c2015-04-13 19:14:06 +00005529 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005530 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00005531 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005532 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005533 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005534 pDb = &db->aDb[pOp->p1];
5535 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00005536 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00005537 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00005538 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00005539 }else{
drhd4187c72010-08-30 22:15:45 +00005540 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00005541 }
drh234c39d2004-07-24 03:30:47 +00005542 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh9467abf2016-02-17 18:44:11 +00005543 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005544 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005545 break;
5546}
5547
drh4a54bb52017-02-18 15:58:52 +00005548/* Opcode: SqlExec * * * P4 *
5549**
5550** Run the SQL statement or statements specified in the P4 string.
5551*/
5552case OP_SqlExec: {
drhbce04142017-02-23 00:58:36 +00005553 db->nSqlExec++;
drh4a54bb52017-02-18 15:58:52 +00005554 rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
drhbce04142017-02-23 00:58:36 +00005555 db->nSqlExec--;
drh4a54bb52017-02-18 15:58:52 +00005556 if( rc ) goto abort_due_to_error;
5557 break;
5558}
5559
drh22645842011-03-24 01:34:03 +00005560/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005561**
5562** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005563** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005564**
5565** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005566** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005567*/
drh9cbf3422008-01-17 16:22:13 +00005568case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005569 int iDb;
5570 const char *zMaster;
5571 char *zSql;
5572 InitData initData;
5573
drhbdaec522011-04-04 00:14:43 +00005574 /* Any prepared statement that invokes this opcode will hold mutexes
5575 ** on every btree. This is a prerequisite for invoking
5576 ** sqlite3InitCallback().
5577 */
5578#ifdef SQLITE_DEBUG
5579 for(iDb=0; iDb<db->nDb; iDb++){
5580 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5581 }
5582#endif
drhbdaec522011-04-04 00:14:43 +00005583
drh856c1032009-06-02 15:21:42 +00005584 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005585 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005586 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005587 /* Used to be a conditional */ {
drhe0a04a32016-12-16 01:00:21 +00005588 zMaster = MASTER_NAME;
danielk1977a8bbef82009-03-23 17:11:26 +00005589 initData.db = db;
5590 initData.iDb = pOp->p1;
5591 initData.pzErrMsg = &p->zErrMsg;
5592 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005593 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
drh69c33822016-08-18 14:33:11 +00005594 db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
danielk1977a8bbef82009-03-23 17:11:26 +00005595 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005596 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005597 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005598 assert( db->init.busy==0 );
5599 db->init.busy = 1;
5600 initData.rc = SQLITE_OK;
5601 assert( !db->mallocFailed );
5602 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5603 if( rc==SQLITE_OK ) rc = initData.rc;
drhdbd6a7d2017-04-05 12:39:49 +00005604 sqlite3DbFreeNN(db, zSql);
danielk1977a8bbef82009-03-23 17:11:26 +00005605 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005606 }
drh3c23a882007-01-09 14:01:13 +00005607 }
drh9467abf2016-02-17 18:44:11 +00005608 if( rc ){
5609 sqlite3ResetAllSchemasOfConnection(db);
5610 if( rc==SQLITE_NOMEM ){
5611 goto no_mem;
5612 }
5613 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005614 }
drh234c39d2004-07-24 03:30:47 +00005615 break;
5616}
5617
drh8bfdf722009-06-19 14:06:03 +00005618#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005619/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005620**
5621** Read the sqlite_stat1 table for database P1 and load the content
5622** of that table into the internal index hash table. This will cause
5623** the analysis to be used when preparing all subsequent queries.
5624*/
drh9cbf3422008-01-17 16:22:13 +00005625case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005626 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5627 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005628 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005629 break;
5630}
drh8bfdf722009-06-19 14:06:03 +00005631#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005632
drh98757152008-01-09 23:04:12 +00005633/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005634**
5635** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005636** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005637** is dropped from disk (using the Destroy opcode) in order to keep
5638** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005639** schema consistent with what is on disk.
5640*/
drh9cbf3422008-01-17 16:22:13 +00005641case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005642 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005643 break;
5644}
5645
drh98757152008-01-09 23:04:12 +00005646/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005647**
5648** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005649** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005650** is dropped from disk (using the Destroy opcode)
5651** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005652** schema consistent with what is on disk.
5653*/
drh9cbf3422008-01-17 16:22:13 +00005654case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005655 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005656 break;
5657}
5658
drh98757152008-01-09 23:04:12 +00005659/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005660**
5661** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005662** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005663** is dropped from disk (using the Destroy opcode) in order to keep
5664** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005665** schema consistent with what is on disk.
5666*/
drh9cbf3422008-01-17 16:22:13 +00005667case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005668 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005669 break;
5670}
5671
drh234c39d2004-07-24 03:30:47 +00005672
drhb7f91642004-10-31 02:22:47 +00005673#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98968b22016-03-15 22:00:39 +00005674/* Opcode: IntegrityCk P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00005675**
drh98757152008-01-09 23:04:12 +00005676** Do an analysis of the currently open database. Store in
5677** register P1 the text of an error message describing any problems.
5678** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005679**
drh66accfc2017-02-22 18:04:42 +00005680** The register P3 contains one less than the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005681** At most reg(P3) errors will be reported.
5682** In other words, the analysis stops as soon as reg(P1) errors are
5683** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005684**
drh98968b22016-03-15 22:00:39 +00005685** The root page numbers of all tables in the database are integers
5686** stored in P4_INTARRAY argument.
drh21504322002-06-25 13:16:02 +00005687**
drh98757152008-01-09 23:04:12 +00005688** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005689** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005690**
drh1dcdbc02007-01-27 02:24:54 +00005691** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005692*/
drhaaab5722002-02-19 13:39:21 +00005693case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005694 int nRoot; /* Number of tables to check. (Number of root pages.) */
5695 int *aRoot; /* Array of rootpage numbers for tables to be checked */
drh98757152008-01-09 23:04:12 +00005696 int nErr; /* Number of errors reported */
5697 char *z; /* Text of the error report */
5698 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005699
drh1713afb2013-06-28 01:24:57 +00005700 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005701 nRoot = pOp->p2;
drh98968b22016-03-15 22:00:39 +00005702 aRoot = pOp->p4.ai;
drh79069752004-05-22 21:30:40 +00005703 assert( nRoot>0 );
drh98968b22016-03-15 22:00:39 +00005704 assert( aRoot[nRoot]==0 );
drh9f6168b2016-03-19 23:32:58 +00005705 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005706 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005707 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005708 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005709 pIn1 = &aMem[pOp->p1];
drh98757152008-01-09 23:04:12 +00005710 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005711 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh98757152008-01-09 23:04:12 +00005712 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh66accfc2017-02-22 18:04:42 +00005713 (int)pnErr->u.i+1, &nErr);
drha05a7222008-01-19 03:35:58 +00005714 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005715 if( nErr==0 ){
5716 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005717 }else if( z==0 ){
5718 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005719 }else{
drh66accfc2017-02-22 18:04:42 +00005720 pnErr->u.i -= nErr-1;
danielk1977a7a8e142008-02-13 18:25:27 +00005721 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005722 }
drhb7654112008-01-12 12:48:07 +00005723 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005724 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005725 break;
5726}
drhb7f91642004-10-31 02:22:47 +00005727#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005728
drh3d4501e2008-12-04 20:40:10 +00005729/* Opcode: RowSetAdd P1 P2 * * *
drh72e26de2016-08-24 21:24:04 +00005730** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005731**
drh3d4501e2008-12-04 20:40:10 +00005732** Insert the integer value held by register P2 into a boolean index
5733** held in register P1.
5734**
5735** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005736*/
drh93952eb2009-11-13 19:43:43 +00005737case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005738 pIn1 = &aMem[pOp->p1];
5739 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005740 assert( (pIn2->flags & MEM_Int)!=0 );
5741 if( (pIn1->flags & MEM_RowSet)==0 ){
5742 sqlite3VdbeMemSetRowSet(pIn1);
5743 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005744 }
drh93952eb2009-11-13 19:43:43 +00005745 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005746 break;
5747}
5748
5749/* Opcode: RowSetRead P1 P2 P3 * *
drh72e26de2016-08-24 21:24:04 +00005750** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005751**
5752** Extract the smallest value from boolean index P1 and put that value into
5753** register P3. Or, if boolean index P1 is initially empty, leave P3
5754** unchanged and jump to instruction P2.
5755*/
drh93952eb2009-11-13 19:43:43 +00005756case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005757 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005758
drh3c657212009-11-17 23:59:58 +00005759 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005760 if( (pIn1->flags & MEM_RowSet)==0
5761 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005762 ){
5763 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005764 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005765 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005766 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005767 }else{
5768 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005769 VdbeBranchTaken(0,2);
drhf56fa462015-04-13 21:39:54 +00005770 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005771 }
drh49afe3a2013-07-10 03:05:14 +00005772 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005773}
5774
drh1b26c7c2009-04-22 02:15:47 +00005775/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005776** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005777**
drhade97602009-04-21 15:05:18 +00005778** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005779** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005780** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005781** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005782** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005783**
drh1b26c7c2009-04-22 02:15:47 +00005784** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005785** of integers, where each set contains no duplicates. Each set
5786** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005787** must have P4==0, the final set P4=-1. P4 must be either -1 or
5788** non-negative. For non-negative values of P4 only the lower 4
5789** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005790**
5791** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005792** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005793** (b) when P4==-1 there is no need to insert the value, as it will
5794** never be tested for, and (c) when a value that is part of set X is
5795** inserted, there is no need to search to see if the same value was
5796** previously inserted as part of set X (only if it was previously
5797** inserted as part of some other set).
5798*/
drh1b26c7c2009-04-22 02:15:47 +00005799case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005800 int iSet;
5801 int exists;
5802
drh3c657212009-11-17 23:59:58 +00005803 pIn1 = &aMem[pOp->p1];
5804 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005805 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005806 assert( pIn3->flags&MEM_Int );
5807
drh1b26c7c2009-04-22 02:15:47 +00005808 /* If there is anything other than a rowset object in memory cell P1,
5809 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005810 */
drh733bf1b2009-04-22 00:47:00 +00005811 if( (pIn1->flags & MEM_RowSet)==0 ){
5812 sqlite3VdbeMemSetRowSet(pIn1);
5813 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005814 }
5815
5816 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005817 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005818 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005819 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005820 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005821 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005822 }
5823 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005824 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005825 }
5826 break;
5827}
5828
drh5e00f6c2001-09-13 13:46:56 +00005829
danielk197793758c82005-01-21 08:13:14 +00005830#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005831
drh0fd61352014-02-07 02:29:45 +00005832/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005833**
dan76d462e2009-08-30 11:42:51 +00005834** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005835**
dan76d462e2009-08-30 11:42:51 +00005836** P1 contains the address of the memory cell that contains the first memory
5837** cell in an array of values used as arguments to the sub-program. P2
5838** contains the address to jump to if the sub-program throws an IGNORE
5839** exception using the RAISE() function. Register P3 contains the address
5840** of a memory cell in this (the parent) VM that is used to allocate the
5841** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005842**
5843** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005844**
5845** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005846*/
dan76d462e2009-08-30 11:42:51 +00005847case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005848 int nMem; /* Number of memory registers for sub-program */
5849 int nByte; /* Bytes of runtime space required for sub-program */
5850 Mem *pRt; /* Register to allocate runtime space */
5851 Mem *pMem; /* Used to iterate through memory cells */
5852 Mem *pEnd; /* Last memory cell in new array */
5853 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5854 SubProgram *pProgram; /* Sub-program to execute */
5855 void *t; /* Token identifying trigger */
5856
5857 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005858 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005859 assert( pProgram->nOp>0 );
5860
dan1da40a32009-09-19 17:00:31 +00005861 /* If the p5 flag is clear, then recursive invocation of triggers is
5862 ** disabled for backwards compatibility (p5 is set if this sub-program
5863 ** is really a trigger, not a foreign key action, and the flag set
5864 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005865 **
5866 ** It is recursive invocation of triggers, at the SQL level, that is
5867 ** disabled. In some cases a single trigger may generate more than one
5868 ** SubProgram (if the trigger may be executed with more than one different
5869 ** ON CONFLICT algorithm). SubProgram structures associated with a
5870 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005871 ** variable. */
5872 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005873 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005874 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5875 if( pFrame ) break;
5876 }
5877
danf5894502009-10-07 18:41:19 +00005878 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005879 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00005880 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00005881 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00005882 }
5883
5884 /* Register pRt is used to store the memory required to save the state
5885 ** of the current program, and the memory required at runtime to execute
5886 ** the trigger program. If this trigger has been fired before, then pRt
5887 ** is already allocated. Otherwise, it must be initialized. */
5888 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005889 /* SubProgram.nMem is set to the number of memory cells used by the
5890 ** program stored in SubProgram.aOp. As well as these, one memory
5891 ** cell is required for each cursor used by the program. Set local
5892 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5893 */
dan65a7cd12009-09-01 12:16:01 +00005894 nMem = pProgram->nMem + pProgram->nCsr;
drh3cdce922016-03-21 00:30:40 +00005895 assert( nMem>0 );
5896 if( pProgram->nCsr==0 ) nMem++;
dan65a7cd12009-09-01 12:16:01 +00005897 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005898 + nMem * sizeof(Mem)
drhab087d42017-03-24 17:59:56 +00005899 + pProgram->nCsr * sizeof(VdbeCursor*)
5900 + (pProgram->nOp + 7)/8;
dan165921a2009-08-28 18:53:45 +00005901 pFrame = sqlite3DbMallocZero(db, nByte);
5902 if( !pFrame ){
5903 goto no_mem;
5904 }
5905 sqlite3VdbeMemRelease(pRt);
5906 pRt->flags = MEM_Frame;
5907 pRt->u.pFrame = pFrame;
5908
5909 pFrame->v = p;
5910 pFrame->nChildMem = nMem;
5911 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00005912 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00005913 pFrame->aMem = p->aMem;
5914 pFrame->nMem = p->nMem;
5915 pFrame->apCsr = p->apCsr;
5916 pFrame->nCursor = p->nCursor;
5917 pFrame->aOp = p->aOp;
5918 pFrame->nOp = p->nOp;
5919 pFrame->token = pProgram->token;
dane2f771b2014-11-03 15:33:17 +00005920#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005921 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00005922#endif
dan165921a2009-08-28 18:53:45 +00005923
5924 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5925 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005926 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005927 pMem->db = db;
5928 }
5929 }else{
5930 pFrame = pRt->u.pFrame;
drh9f6168b2016-03-19 23:32:58 +00005931 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem
5932 || (pProgram->nCsr==0 && pProgram->nMem+1==pFrame->nChildMem) );
dan165921a2009-08-28 18:53:45 +00005933 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00005934 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00005935 }
5936
5937 p->nFrame++;
5938 pFrame->pParent = p->pFrame;
drhfae58d52017-01-26 17:26:44 +00005939 pFrame->lastRowid = db->lastRowid;
dan76d462e2009-08-30 11:42:51 +00005940 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00005941 pFrame->nDbChange = p->db->nChange;
dan32001322016-02-19 18:54:29 +00005942 assert( pFrame->pAuxData==0 );
5943 pFrame->pAuxData = p->pAuxData;
5944 p->pAuxData = 0;
dan2832ad42009-08-31 15:27:27 +00005945 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005946 p->pFrame = pFrame;
drh9f6168b2016-03-19 23:32:58 +00005947 p->aMem = aMem = VdbeFrameMem(pFrame);
dan165921a2009-08-28 18:53:45 +00005948 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005949 p->nCursor = (u16)pFrame->nChildCsr;
drh9f6168b2016-03-19 23:32:58 +00005950 p->apCsr = (VdbeCursor **)&aMem[p->nMem];
drhab087d42017-03-24 17:59:56 +00005951 pFrame->aOnce = (u8*)&p->apCsr[pProgram->nCsr];
drh18333ef2017-03-24 18:38:41 +00005952 memset(pFrame->aOnce, 0, (pProgram->nOp + 7)/8);
drhbbe879d2009-11-14 18:04:35 +00005953 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005954 p->nOp = pProgram->nOp;
dane2f771b2014-11-03 15:33:17 +00005955#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005956 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00005957#endif
drhf56fa462015-04-13 21:39:54 +00005958 pOp = &aOp[-1];
dan165921a2009-08-28 18:53:45 +00005959
5960 break;
5961}
5962
dan76d462e2009-08-30 11:42:51 +00005963/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005964**
dan76d462e2009-08-30 11:42:51 +00005965** This opcode is only ever present in sub-programs called via the
5966** OP_Program instruction. Copy a value currently stored in a memory
5967** cell of the calling (parent) frame to cell P2 in the current frames
5968** address space. This is used by trigger programs to access the new.*
5969** and old.* values.
dan165921a2009-08-28 18:53:45 +00005970**
dan76d462e2009-08-30 11:42:51 +00005971** The address of the cell in the parent frame is determined by adding
5972** the value of the P1 argument to the value of the P1 argument to the
5973** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005974*/
drh27a348c2015-04-13 19:14:06 +00005975case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00005976 VdbeFrame *pFrame;
5977 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00005978 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00005979 pFrame = p->pFrame;
5980 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005981 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5982 break;
5983}
5984
danielk197793758c82005-01-21 08:13:14 +00005985#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005986
dan1da40a32009-09-19 17:00:31 +00005987#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005988/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005989** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005990**
dan0ff297e2009-09-25 17:03:14 +00005991** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5992** If P1 is non-zero, the database constraint counter is incremented
5993** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005994** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005995*/
dan32b09f22009-09-23 17:29:59 +00005996case OP_FkCounter: {
drh963c74d2013-07-11 12:19:12 +00005997 if( db->flags & SQLITE_DeferFKs ){
dancb3e4b72013-07-03 19:53:05 +00005998 db->nDeferredImmCons += pOp->p2;
5999 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00006000 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00006001 }else{
dan0ff297e2009-09-25 17:03:14 +00006002 p->nFkConstraint += pOp->p2;
6003 }
6004 break;
6005}
6006
6007/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006008** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00006009**
6010** This opcode tests if a foreign key constraint-counter is currently zero.
6011** If so, jump to instruction P2. Otherwise, fall through to the next
6012** instruction.
6013**
6014** If P1 is non-zero, then the jump is taken if the database constraint-counter
6015** is zero (the one that counts deferred constraint violations). If P1 is
6016** zero, the jump is taken if the statement constraint-counter is zero
6017** (immediate foreign key constraint violations).
6018*/
6019case OP_FkIfZero: { /* jump */
6020 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00006021 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006022 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00006023 }else{
drh688852a2014-02-17 22:40:43 +00006024 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00006025 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00006026 }
dan1da40a32009-09-19 17:00:31 +00006027 break;
6028}
6029#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
6030
drh205f48e2004-11-05 00:43:11 +00006031#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00006032/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00006033** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00006034**
dan76d462e2009-08-30 11:42:51 +00006035** P1 is a register in the root frame of this VM (the root frame is
6036** different from the current frame if this instruction is being executed
6037** within a sub-program). Set the value of register P1 to the maximum of
6038** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00006039**
6040** This instruction throws an error if the memory cell is not initially
6041** an integer.
6042*/
dan76d462e2009-08-30 11:42:51 +00006043case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00006044 VdbeFrame *pFrame;
6045 if( p->pFrame ){
6046 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
6047 pIn1 = &pFrame->aMem[pOp->p1];
6048 }else{
drha6c2ed92009-11-14 23:22:23 +00006049 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00006050 }
drh2b4ded92010-09-27 21:09:31 +00006051 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00006052 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00006053 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00006054 sqlite3VdbeMemIntegerify(pIn2);
6055 if( pIn1->u.i<pIn2->u.i){
6056 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00006057 }
6058 break;
6059}
6060#endif /* SQLITE_OMIT_AUTOINCREMENT */
6061
drh8b0cf382015-10-06 21:07:06 +00006062/* Opcode: IfPos P1 P2 P3 * *
6063** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00006064**
drh16897072015-03-07 00:57:37 +00006065** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00006066** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00006067** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00006068**
drh16897072015-03-07 00:57:37 +00006069** If the initial value of register P1 is less than 1, then the
6070** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00006071*/
drh9cbf3422008-01-17 16:22:13 +00006072case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006073 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006074 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00006075 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00006076 if( pIn1->u.i>0 ){
6077 pIn1->u.i -= pOp->p3;
6078 goto jump_to_p2;
6079 }
drhec7429a2005-10-06 16:53:14 +00006080 break;
6081}
6082
drhcc2fa4c2016-01-25 15:57:29 +00006083/* Opcode: OffsetLimit P1 P2 P3 * *
6084** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00006085**
drhcc2fa4c2016-01-25 15:57:29 +00006086** This opcode performs a commonly used computation associated with
6087** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
6088** holds the offset counter. The opcode computes the combined value
6089** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
6090** value computed is the total number of rows that will need to be
6091** visited in order to complete the query.
6092**
6093** If r[P3] is zero or negative, that means there is no OFFSET
6094** and r[P2] is set to be the value of the LIMIT, r[P1].
6095**
6096** if r[P1] is zero or negative, that means there is no LIMIT
6097** and r[P2] is set to -1.
6098**
6099** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00006100*/
drhcc2fa4c2016-01-25 15:57:29 +00006101case OP_OffsetLimit: { /* in1, out2, in3 */
drh719da302016-12-10 04:06:49 +00006102 i64 x;
drh3c657212009-11-17 23:59:58 +00006103 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00006104 pIn3 = &aMem[pOp->p3];
6105 pOut = out2Prerelease(p, pOp);
6106 assert( pIn1->flags & MEM_Int );
6107 assert( pIn3->flags & MEM_Int );
drh719da302016-12-10 04:06:49 +00006108 x = pIn1->u.i;
6109 if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
6110 /* If the LIMIT is less than or equal to zero, loop forever. This
6111 ** is documented. But also, if the LIMIT+OFFSET exceeds 2^63 then
6112 ** also loop forever. This is undocumented. In fact, one could argue
6113 ** that the loop should terminate. But assuming 1 billion iterations
6114 ** per second (far exceeding the capabilities of any current hardware)
6115 ** it would take nearly 300 years to actually reach the limit. So
6116 ** looping forever is a reasonable approximation. */
6117 pOut->u.i = -1;
6118 }else{
6119 pOut->u.i = x;
6120 }
drh15007a92006-01-08 18:10:17 +00006121 break;
6122}
6123
drhf99dd352016-12-18 17:42:00 +00006124/* Opcode: IfNotZero P1 P2 * * *
6125** Synopsis: if r[P1]!=0 then r[P1]--, goto P2
drhec7429a2005-10-06 16:53:14 +00006126**
drh16897072015-03-07 00:57:37 +00006127** Register P1 must contain an integer. If the content of register P1 is
drhf99dd352016-12-18 17:42:00 +00006128** initially greater than zero, then decrement the value in register P1.
6129** If it is non-zero (negative or positive) and then also jump to P2.
6130** If register P1 is initially zero, leave it unchanged and fall through.
drhec7429a2005-10-06 16:53:14 +00006131*/
drh16897072015-03-07 00:57:37 +00006132case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00006133 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00006134 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00006135 VdbeBranchTaken(pIn1->u.i<0, 2);
6136 if( pIn1->u.i ){
drhf99dd352016-12-18 17:42:00 +00006137 if( pIn1->u.i>0 ) pIn1->u.i--;
drhf56fa462015-04-13 21:39:54 +00006138 goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00006139 }
6140 break;
6141}
6142
6143/* Opcode: DecrJumpZero P1 P2 * * *
6144** Synopsis: if (--r[P1])==0 goto P2
6145**
drhab5be2e2016-11-30 05:08:59 +00006146** Register P1 must hold an integer. Decrement the value in P1
6147** and jump to P2 if the new value is exactly zero.
drh16897072015-03-07 00:57:37 +00006148*/
6149case OP_DecrJumpZero: { /* jump, in1 */
6150 pIn1 = &aMem[pOp->p1];
6151 assert( pIn1->flags&MEM_Int );
drhab5be2e2016-11-30 05:08:59 +00006152 if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
6153 VdbeBranchTaken(pIn1->u.i==0, 2);
6154 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00006155 break;
6156}
6157
drh16897072015-03-07 00:57:37 +00006158
drhe2d9e7c2015-06-26 18:47:53 +00006159/* Opcode: AggStep0 * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006160** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00006161**
drh0bce8352002-02-28 00:41:10 +00006162** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00006163** function has P5 arguments. P4 is a pointer to the FuncDef
drhe2d9e7c2015-06-26 18:47:53 +00006164** structure that specifies the function. Register P3 is the
6165** accumulator.
drhe5095352002-02-24 03:25:14 +00006166**
drh98757152008-01-09 23:04:12 +00006167** The P5 arguments are taken from register P2 and its
6168** successors.
drhe5095352002-02-24 03:25:14 +00006169*/
drhe2d9e7c2015-06-26 18:47:53 +00006170/* Opcode: AggStep * P2 P3 P4 P5
6171** Synopsis: accum=r[P3] step(r[P2@P5])
6172**
6173** Execute the step function for an aggregate. The
6174** function has P5 arguments. P4 is a pointer to an sqlite3_context
6175** object that is used to run the function. Register P3 is
6176** as the accumulator.
6177**
6178** The P5 arguments are taken from register P2 and its
6179** successors.
6180**
6181** This opcode is initially coded as OP_AggStep0. On first evaluation,
6182** the FuncDef stored in P4 is converted into an sqlite3_context and
6183** the opcode is changed. In this way, the initialization of the
6184** sqlite3_context only happens once, instead of on each call to the
6185** step function.
6186*/
drh9c7c9132015-06-26 18:16:52 +00006187case OP_AggStep0: {
drh856c1032009-06-02 15:21:42 +00006188 int n;
drh9c7c9132015-06-26 18:16:52 +00006189 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00006190
drh9c7c9132015-06-26 18:16:52 +00006191 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00006192 n = pOp->p5;
drh9f6168b2016-03-19 23:32:58 +00006193 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
6194 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem+1 - p->nCursor)+1) );
drh9c7c9132015-06-26 18:16:52 +00006195 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh575fad62016-02-05 13:38:36 +00006196 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
drh9c7c9132015-06-26 18:16:52 +00006197 if( pCtx==0 ) goto no_mem;
6198 pCtx->pMem = 0;
6199 pCtx->pFunc = pOp->p4.pFunc;
6200 pCtx->iOp = (int)(pOp - aOp);
6201 pCtx->pVdbe = p;
6202 pCtx->argc = n;
6203 pOp->p4type = P4_FUNCCTX;
6204 pOp->p4.pCtx = pCtx;
6205 pOp->opcode = OP_AggStep;
6206 /* Fall through into OP_AggStep */
6207}
6208case OP_AggStep: {
6209 int i;
6210 sqlite3_context *pCtx;
6211 Mem *pMem;
6212 Mem t;
6213
6214 assert( pOp->p4type==P4_FUNCCTX );
6215 pCtx = pOp->p4.pCtx;
6216 pMem = &aMem[pOp->p3];
6217
6218 /* If this function is inside of a trigger, the register array in aMem[]
6219 ** might change from one evaluation to the next. The next block of code
6220 ** checks to see if the register array has changed, and if so it
6221 ** reinitializes the relavant parts of the sqlite3_context object */
6222 if( pCtx->pMem != pMem ){
6223 pCtx->pMem = pMem;
6224 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
6225 }
6226
6227#ifdef SQLITE_DEBUG
6228 for(i=0; i<pCtx->argc; i++){
6229 assert( memIsValid(pCtx->argv[i]) );
6230 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
6231 }
6232#endif
6233
drhabfcea22005-09-06 20:36:48 +00006234 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00006235 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00006236 pCtx->pOut = &t;
6237 pCtx->fErrorOrAux = 0;
6238 pCtx->skipFlag = 0;
drh2d801512016-01-14 22:19:58 +00006239 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
drh9c7c9132015-06-26 18:16:52 +00006240 if( pCtx->fErrorOrAux ){
6241 if( pCtx->isError ){
6242 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
6243 rc = pCtx->isError;
6244 }
6245 sqlite3VdbeMemRelease(&t);
drh9467abf2016-02-17 18:44:11 +00006246 if( rc ) goto abort_due_to_error;
drh9c7c9132015-06-26 18:16:52 +00006247 }else{
6248 assert( t.flags==MEM_Null );
drh1350b032002-02-27 19:00:20 +00006249 }
drh9c7c9132015-06-26 18:16:52 +00006250 if( pCtx->skipFlag ){
drh7a957892012-02-02 17:35:43 +00006251 assert( pOp[-1].opcode==OP_CollSeq );
6252 i = pOp[-1].p1;
6253 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6254 }
drh5e00f6c2001-09-13 13:46:56 +00006255 break;
6256}
6257
drh98757152008-01-09 23:04:12 +00006258/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006259** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006260**
drh13449892005-09-07 21:22:45 +00006261** Execute the finalizer function for an aggregate. P1 is
6262** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00006263**
6264** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006265** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006266** argument is not used by this opcode. It is only there to disambiguate
6267** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00006268** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00006269** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006270*/
drh9cbf3422008-01-17 16:22:13 +00006271case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006272 Mem *pMem;
drh9f6168b2016-03-19 23:32:58 +00006273 assert( pOp->p1>0 && pOp->p1<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006274 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006275 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00006276 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00006277 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006278 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006279 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006280 }
drh2dca8682008-03-21 17:13:13 +00006281 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006282 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006283 if( sqlite3VdbeMemTooBig(pMem) ){
6284 goto too_big;
6285 }
drh5e00f6c2001-09-13 13:46:56 +00006286 break;
6287}
6288
dan5cf53532010-05-01 16:40:20 +00006289#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006290/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006291**
6292** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006293** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6294** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006295** SQLITE_BUSY or not, respectively. Write the number of pages in the
6296** WAL after the checkpoint into mem[P3+1] and the number of pages
6297** in the WAL that have been checkpointed after the checkpoint
6298** completes into mem[P3+2]. However on an error, mem[P3+1] and
6299** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006300*/
6301case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006302 int i; /* Loop counter */
6303 int aRes[3]; /* Results */
6304 Mem *pMem; /* Write results here */
6305
drh9e92a472013-06-27 17:40:30 +00006306 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006307 aRes[0] = 0;
6308 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006309 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6310 || pOp->p2==SQLITE_CHECKPOINT_FULL
6311 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006312 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006313 );
drh30aa3b92011-02-07 23:56:01 +00006314 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006315 if( rc ){
6316 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006317 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006318 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006319 }
drh30aa3b92011-02-07 23:56:01 +00006320 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6321 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6322 }
dan7c246102010-04-12 19:00:29 +00006323 break;
6324};
dan5cf53532010-05-01 16:40:20 +00006325#endif
drh5e00f6c2001-09-13 13:46:56 +00006326
drhcac29a62010-07-02 19:36:52 +00006327#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006328/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006329**
6330** Change the journal mode of database P1 to P3. P3 must be one of the
6331** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6332** modes (delete, truncate, persist, off and memory), this is a simple
6333** operation. No IO is required.
6334**
6335** If changing into or out of WAL mode the procedure is more complicated.
6336**
6337** Write a string containing the final journal-mode to register P2.
6338*/
drh27a348c2015-04-13 19:14:06 +00006339case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006340 Btree *pBt; /* Btree to change journal mode of */
6341 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006342 int eNew; /* New journal mode */
6343 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006344#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006345 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006346#endif
dane04dc882010-04-20 18:53:15 +00006347
drh27a348c2015-04-13 19:14:06 +00006348 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006349 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006350 assert( eNew==PAGER_JOURNALMODE_DELETE
6351 || eNew==PAGER_JOURNALMODE_TRUNCATE
6352 || eNew==PAGER_JOURNALMODE_PERSIST
6353 || eNew==PAGER_JOURNALMODE_OFF
6354 || eNew==PAGER_JOURNALMODE_MEMORY
6355 || eNew==PAGER_JOURNALMODE_WAL
6356 || eNew==PAGER_JOURNALMODE_QUERY
6357 );
6358 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006359 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006360
dane04dc882010-04-20 18:53:15 +00006361 pBt = db->aDb[pOp->p1].pBt;
6362 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006363 eOld = sqlite3PagerGetJournalMode(pPager);
6364 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6365 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006366
6367#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006368 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006369
drhd80b2332010-05-01 00:59:37 +00006370 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006371 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006372 */
6373 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006374 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006375 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006376 ){
drh0b9b4302010-06-11 17:01:24 +00006377 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006378 }
6379
drh0b9b4302010-06-11 17:01:24 +00006380 if( (eNew!=eOld)
6381 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6382 ){
danc0537fe2013-06-28 19:41:43 +00006383 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006384 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006385 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006386 "cannot change %s wal mode from within a transaction",
6387 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6388 );
drh9467abf2016-02-17 18:44:11 +00006389 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006390 }else{
6391
6392 if( eOld==PAGER_JOURNALMODE_WAL ){
6393 /* If leaving WAL mode, close the log file. If successful, the call
6394 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6395 ** file. An EXCLUSIVE lock may still be held on the database file
6396 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006397 */
dan7fb89902016-08-12 16:21:15 +00006398 rc = sqlite3PagerCloseWal(pPager, db);
drhab9b7442010-05-10 11:20:05 +00006399 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006400 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006401 }
drh242c4f72010-06-22 14:49:39 +00006402 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6403 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6404 ** as an intermediate */
6405 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006406 }
6407
6408 /* Open a transaction on the database file. Regardless of the journal
6409 ** mode, this transaction always uses a rollback journal.
6410 */
6411 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6412 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006413 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006414 }
6415 }
6416 }
dan5cf53532010-05-01 16:40:20 +00006417#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006418
drh9467abf2016-02-17 18:44:11 +00006419 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006420 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006421
dane04dc882010-04-20 18:53:15 +00006422 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006423 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006424 pOut->n = sqlite3Strlen30(pOut->z);
6425 pOut->enc = SQLITE_UTF8;
6426 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006427 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006428 break;
drhcac29a62010-07-02 19:36:52 +00006429};
6430#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006431
drhfdbcdee2007-03-27 14:44:50 +00006432#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh9ef5e772016-08-19 14:20:56 +00006433/* Opcode: Vacuum P1 * * * *
drh6f8c91c2003-12-07 00:24:35 +00006434**
drh9ef5e772016-08-19 14:20:56 +00006435** Vacuum the entire database P1. P1 is 0 for "main", and 2 or more
6436** for an attached database. The "temp" database may not be vacuumed.
drh6f8c91c2003-12-07 00:24:35 +00006437*/
drh9cbf3422008-01-17 16:22:13 +00006438case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006439 assert( p->readOnly==0 );
drh9ef5e772016-08-19 14:20:56 +00006440 rc = sqlite3RunVacuum(&p->zErrMsg, db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00006441 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006442 break;
6443}
drh154d4b22006-09-21 11:02:16 +00006444#endif
drh6f8c91c2003-12-07 00:24:35 +00006445
danielk1977dddbcdc2007-04-26 14:42:34 +00006446#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006447/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006448**
6449** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006450** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006451** P2. Otherwise, fall through to the next instruction.
6452*/
drh9cbf3422008-01-17 16:22:13 +00006453case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006454 Btree *pBt;
6455
6456 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006457 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006458 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006459 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006460 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006461 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006462 if( rc ){
6463 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006464 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006465 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006466 }
6467 break;
6468}
6469#endif
6470
drh98757152008-01-09 23:04:12 +00006471/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006472**
drh25df48d2014-07-22 14:58:12 +00006473** Cause precompiled statements to expire. When an expired statement
6474** is executed using sqlite3_step() it will either automatically
6475** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6476** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006477**
6478** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006479** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00006480*/
drh9cbf3422008-01-17 16:22:13 +00006481case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00006482 if( !pOp->p1 ){
6483 sqlite3ExpirePreparedStatements(db);
6484 }else{
6485 p->expired = 1;
6486 }
6487 break;
6488}
6489
danielk1977c00da102006-01-07 13:21:04 +00006490#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006491/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006492** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006493**
6494** Obtain a lock on a particular table. This instruction is only used when
6495** the shared-cache feature is enabled.
6496**
danielk197796d48e92009-06-29 06:00:37 +00006497** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006498** on which the lock is acquired. A readlock is obtained if P3==0 or
6499** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006500**
6501** P2 contains the root-page of the table to lock.
6502**
drh66a51672008-01-03 00:01:23 +00006503** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006504** used to generate an error message if the lock cannot be obtained.
6505*/
drh9cbf3422008-01-17 16:22:13 +00006506case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006507 u8 isWriteLock = (u8)pOp->p3;
6508 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6509 int p1 = pOp->p1;
6510 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006511 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006512 assert( isWriteLock==0 || isWriteLock==1 );
6513 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006514 if( rc ){
6515 if( (rc&0xFF)==SQLITE_LOCKED ){
6516 const char *z = pOp->p4.z;
6517 sqlite3VdbeError(p, "database table is locked: %s", z);
6518 }
6519 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006520 }
danielk1977c00da102006-01-07 13:21:04 +00006521 }
6522 break;
6523}
drhb9bb7c12006-06-11 23:41:55 +00006524#endif /* SQLITE_OMIT_SHARED_CACHE */
6525
6526#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006527/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006528**
danielk19773e3a84d2008-08-01 17:37:40 +00006529** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6530** xBegin method for that table.
6531**
6532** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006533** within a callback to a virtual table xSync() method. If it is, the error
6534** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006535*/
drh9cbf3422008-01-17 16:22:13 +00006536case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006537 VTable *pVTab;
6538 pVTab = pOp->p4.pVtab;
6539 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006540 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006541 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006542 break;
6543}
6544#endif /* SQLITE_OMIT_VIRTUALTABLE */
6545
6546#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006547/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006548**
dan73779452015-03-19 18:56:17 +00006549** P2 is a register that holds the name of a virtual table in database
6550** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006551*/
drh9cbf3422008-01-17 16:22:13 +00006552case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006553 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006554 const char *zTab; /* Name of the virtual table */
6555
dan73779452015-03-19 18:56:17 +00006556 memset(&sMem, 0, sizeof(sMem));
6557 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006558 /* Because P2 is always a static string, it is impossible for the
6559 ** sqlite3VdbeMemCopy() to fail */
6560 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6561 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006562 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006563 assert( rc==SQLITE_OK );
6564 zTab = (const char*)sqlite3_value_text(&sMem);
6565 assert( zTab || db->mallocFailed );
6566 if( zTab ){
6567 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006568 }
6569 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006570 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006571 break;
6572}
6573#endif /* SQLITE_OMIT_VIRTUALTABLE */
6574
6575#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006576/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006577**
drh66a51672008-01-03 00:01:23 +00006578** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006579** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006580*/
drh9cbf3422008-01-17 16:22:13 +00006581case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006582 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006583 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006584 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006585 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006586 break;
6587}
6588#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006589
drh9eff6162006-06-12 21:59:13 +00006590#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006591/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006592**
drh66a51672008-01-03 00:01:23 +00006593** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006594** P1 is a cursor number. This opcode opens a cursor to the virtual
6595** table and stores that cursor in P1.
6596*/
drh9cbf3422008-01-17 16:22:13 +00006597case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006598 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006599 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006600 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006601 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006602
drh1713afb2013-06-28 01:24:57 +00006603 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006604 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006605 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006606 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006607 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6608 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006609 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006610 }
6611 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006612 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006613 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006614 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006615
drh9467abf2016-02-17 18:44:11 +00006616 /* Initialize sqlite3_vtab_cursor base class */
6617 pVCur->pVtab = pVtab;
6618
6619 /* Initialize vdbe cursor object */
6620 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6621 if( pCur ){
6622 pCur->uc.pVCur = pVCur;
6623 pVtab->nRef++;
6624 }else{
6625 assert( db->mallocFailed );
6626 pModule->xClose(pVCur);
6627 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006628 }
drh9eff6162006-06-12 21:59:13 +00006629 break;
6630}
6631#endif /* SQLITE_OMIT_VIRTUALTABLE */
6632
6633#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006634/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006635** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006636**
6637** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6638** the filtered result set is empty.
6639**
drh66a51672008-01-03 00:01:23 +00006640** P4 is either NULL or a string that was generated by the xBestIndex
6641** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006642** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006643**
drh9eff6162006-06-12 21:59:13 +00006644** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006645** by P1. The integer query plan parameter to xFilter is stored in register
6646** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006647** xFilter method. Registers P3+2..P3+1+argc are the argc
6648** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006649** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006650**
danielk19776dbee812008-01-03 18:39:41 +00006651** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006652*/
drh9cbf3422008-01-17 16:22:13 +00006653case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006654 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006655 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006656 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006657 Mem *pQuery;
6658 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006659 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006660 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006661 VdbeCursor *pCur;
6662 int res;
6663 int i;
6664 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006665
drha6c2ed92009-11-14 23:22:23 +00006666 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006667 pArgc = &pQuery[1];
6668 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006669 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006670 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006671 assert( pCur->eCurType==CURTYPE_VTAB );
6672 pVCur = pCur->uc.pVCur;
6673 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006674 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006675
drh9cbf3422008-01-17 16:22:13 +00006676 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006677 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006678 nArg = (int)pArgc->u.i;
6679 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006680
drh644a5292006-12-20 14:53:38 +00006681 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006682 res = 0;
6683 apArg = p->apArg;
6684 for(i = 0; i<nArg; i++){
6685 apArg[i] = &pArgc[i+1];
6686 }
drhc960dcb2015-11-20 19:22:01 +00006687 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006688 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006689 if( rc ) goto abort_due_to_error;
6690 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006691 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006692 VdbeBranchTaken(res!=0,2);
6693 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006694 break;
6695}
6696#endif /* SQLITE_OMIT_VIRTUALTABLE */
6697
6698#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006699/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006700** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006701**
drh2133d822008-01-03 18:44:59 +00006702** Store the value of the P2-th column of
6703** the row of the virtual-table that the
6704** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006705*/
6706case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006707 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006708 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006709 Mem *pDest;
6710 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006711
drhdfe88ec2008-11-03 20:55:06 +00006712 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006713 assert( pCur->eCurType==CURTYPE_VTAB );
drh9f6168b2016-03-19 23:32:58 +00006714 assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006715 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006716 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006717 if( pCur->nullRow ){
6718 sqlite3VdbeMemSetNull(pDest);
6719 break;
6720 }
drhc960dcb2015-11-20 19:22:01 +00006721 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006722 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006723 assert( pModule->xColumn );
6724 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006725 sContext.pOut = pDest;
6726 MemSetTypeFlag(pDest, MEM_Null);
drhc960dcb2015-11-20 19:22:01 +00006727 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006728 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006729 if( sContext.isError ){
6730 rc = sContext.isError;
6731 }
drh9bd038f2014-08-27 14:14:06 +00006732 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006733 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006734 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006735
drhde4fcfd2008-01-19 23:50:26 +00006736 if( sqlite3VdbeMemTooBig(pDest) ){
6737 goto too_big;
6738 }
drh9467abf2016-02-17 18:44:11 +00006739 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006740 break;
6741}
6742#endif /* SQLITE_OMIT_VIRTUALTABLE */
6743
6744#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006745/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006746**
6747** Advance virtual table P1 to the next row in its result set and
6748** jump to instruction P2. Or, if the virtual table has reached
6749** the end of its result set, then fall through to the next instruction.
6750*/
drh9cbf3422008-01-17 16:22:13 +00006751case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006752 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006753 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006754 int res;
drh856c1032009-06-02 15:21:42 +00006755 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006756
drhc54a6172009-06-02 16:06:03 +00006757 res = 0;
drh856c1032009-06-02 15:21:42 +00006758 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006759 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00006760 if( pCur->nullRow ){
6761 break;
6762 }
drhc960dcb2015-11-20 19:22:01 +00006763 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006764 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006765 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006766
drhde4fcfd2008-01-19 23:50:26 +00006767 /* Invoke the xNext() method of the module. There is no way for the
6768 ** underlying implementation to return an error if one occurs during
6769 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6770 ** data is available) and the error code returned when xColumn or
6771 ** some other method is next invoked on the save virtual table cursor.
6772 */
drhc960dcb2015-11-20 19:22:01 +00006773 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00006774 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006775 if( rc ) goto abort_due_to_error;
6776 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00006777 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006778 if( !res ){
6779 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00006780 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00006781 }
drh49afe3a2013-07-10 03:05:14 +00006782 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006783}
6784#endif /* SQLITE_OMIT_VIRTUALTABLE */
6785
danielk1977182c4ba2007-06-27 15:53:34 +00006786#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006787/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006788**
drh66a51672008-01-03 00:01:23 +00006789** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006790** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006791** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006792*/
drh9cbf3422008-01-17 16:22:13 +00006793case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006794 sqlite3_vtab *pVtab;
6795 Mem *pName;
6796
danielk1977595a5232009-07-24 17:58:53 +00006797 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006798 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006799 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006800 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006801 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006802 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006803 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006804 testcase( pName->enc==SQLITE_UTF8 );
6805 testcase( pName->enc==SQLITE_UTF16BE );
6806 testcase( pName->enc==SQLITE_UTF16LE );
6807 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00006808 if( rc ) goto abort_due_to_error;
6809 rc = pVtab->pModule->xRename(pVtab, pName->z);
6810 sqlite3VtabImportErrmsg(p, pVtab);
6811 p->expired = 0;
6812 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00006813 break;
6814}
6815#endif
drh4cbdda92006-06-14 19:00:20 +00006816
6817#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006818/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006819** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006820**
drh66a51672008-01-03 00:01:23 +00006821** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006822** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006823** are contiguous memory cells starting at P3 to pass to the xUpdate
6824** invocation. The value in register (P3+P2-1) corresponds to the
6825** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006826**
6827** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006828** The argv[0] element (which corresponds to memory cell P3)
6829** is the rowid of a row to delete. If argv[0] is NULL then no
6830** deletion occurs. The argv[1] element is the rowid of the new
6831** row. This can be NULL to have the virtual table select the new
6832** rowid for itself. The subsequent elements in the array are
6833** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006834**
6835** If P2==1 then no insert is performed. argv[0] is the rowid of
6836** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006837**
6838** P1 is a boolean flag. If it is set to true and the xUpdate call
6839** is successful, then the value returned by sqlite3_last_insert_rowid()
6840** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006841**
6842** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6843** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006844*/
drh9cbf3422008-01-17 16:22:13 +00006845case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006846 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006847 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006848 int nArg;
6849 int i;
6850 sqlite_int64 rowid;
6851 Mem **apArg;
6852 Mem *pX;
6853
danb061d052011-04-25 18:49:57 +00006854 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6855 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6856 );
drh9e92a472013-06-27 17:40:30 +00006857 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006858 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006859 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6860 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006861 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006862 }
6863 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00006864 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006865 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006866 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006867 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006868 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006869 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006870 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006871 assert( memIsValid(pX) );
6872 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006873 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006874 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006875 }
danb061d052011-04-25 18:49:57 +00006876 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006877 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006878 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006879 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006880 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006881 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drhfae58d52017-01-26 17:26:44 +00006882 db->lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006883 }
drhd91c1a12013-02-09 13:58:25 +00006884 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006885 if( pOp->p5==OE_Ignore ){
6886 rc = SQLITE_OK;
6887 }else{
6888 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6889 }
6890 }else{
6891 p->nChange++;
6892 }
drh9467abf2016-02-17 18:44:11 +00006893 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00006894 }
drh4cbdda92006-06-14 19:00:20 +00006895 break;
danielk1977399918f2006-06-14 13:03:23 +00006896}
6897#endif /* SQLITE_OMIT_VIRTUALTABLE */
6898
danielk197759a93792008-05-15 17:48:20 +00006899#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6900/* Opcode: Pagecount P1 P2 * * *
6901**
6902** Write the current number of pages in database P1 to memory cell P2.
6903*/
drh27a348c2015-04-13 19:14:06 +00006904case OP_Pagecount: { /* out2 */
6905 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00006906 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006907 break;
6908}
6909#endif
6910
drh60ac3f42010-11-23 18:59:27 +00006911
6912#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6913/* Opcode: MaxPgcnt P1 P2 P3 * *
6914**
6915** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006916** Do not let the maximum page count fall below the current page count and
6917** do not change the maximum page count value if P3==0.
6918**
drh60ac3f42010-11-23 18:59:27 +00006919** Store the maximum page count after the change in register P2.
6920*/
drh27a348c2015-04-13 19:14:06 +00006921case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00006922 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006923 Btree *pBt;
6924
drh27a348c2015-04-13 19:14:06 +00006925 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00006926 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006927 newMax = 0;
6928 if( pOp->p3 ){
6929 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006930 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006931 }
6932 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006933 break;
6934}
6935#endif
6936
6937
drh9e5eb9c2016-09-18 16:08:10 +00006938/* Opcode: Init P1 P2 * P4 *
drh72e26de2016-08-24 21:24:04 +00006939** Synopsis: Start at P2
drhaceb31b2014-02-08 01:40:27 +00006940**
6941** Programs contain a single instance of this opcode as the very first
6942** opcode.
drh949f9cd2008-01-12 21:35:57 +00006943**
6944** If tracing is enabled (by the sqlite3_trace()) interface, then
6945** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006946** Or if P4 is blank, use the string returned by sqlite3_sql().
6947**
6948** If P2 is not zero, jump to instruction P2.
drh9e5eb9c2016-09-18 16:08:10 +00006949**
6950** Increment the value of P1 so that OP_Once opcodes will jump the
6951** first time they are evaluated for this run.
drh949f9cd2008-01-12 21:35:57 +00006952*/
drhaceb31b2014-02-08 01:40:27 +00006953case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006954 char *zTrace;
drh9e5eb9c2016-09-18 16:08:10 +00006955 int i;
drh5fe63bf2016-07-25 02:42:22 +00006956
6957 /* If the P4 argument is not NULL, then it must be an SQL comment string.
6958 ** The "--" string is broken up to prevent false-positives with srcck1.c.
6959 **
6960 ** This assert() provides evidence for:
6961 ** EVIDENCE-OF: R-50676-09860 The callback can compute the same text that
6962 ** would have been returned by the legacy sqlite3_trace() interface by
6963 ** using the X argument when X begins with "--" and invoking
6964 ** sqlite3_expanded_sql(P) otherwise.
6965 */
6966 assert( pOp->p4.z==0 || strncmp(pOp->p4.z, "-" "- ", 3)==0 );
drh9e5eb9c2016-09-18 16:08:10 +00006967 assert( pOp==p->aOp ); /* Always instruction 0 */
drh856c1032009-06-02 15:21:42 +00006968
drhaceb31b2014-02-08 01:40:27 +00006969#ifndef SQLITE_OMIT_TRACE
drhfca760c2016-07-14 01:09:08 +00006970 if( (db->mTrace & (SQLITE_TRACE_STMT|SQLITE_TRACE_LEGACY))!=0
drh37f58e92012-09-04 21:34:26 +00006971 && !p->doingRerun
6972 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6973 ){
drh3d2a5292016-07-13 22:55:01 +00006974#ifndef SQLITE_OMIT_DEPRECATED
drhfca760c2016-07-14 01:09:08 +00006975 if( db->mTrace & SQLITE_TRACE_LEGACY ){
6976 void (*x)(void*,const char*) = (void(*)(void*,const char*))db->xTrace;
drh5fe63bf2016-07-25 02:42:22 +00006977 char *z = sqlite3VdbeExpandSql(p, zTrace);
drhfca760c2016-07-14 01:09:08 +00006978 x(db->pTraceArg, z);
drhbd441f72016-07-25 02:31:48 +00006979 sqlite3_free(z);
drhfca760c2016-07-14 01:09:08 +00006980 }else
drh3d2a5292016-07-13 22:55:01 +00006981#endif
drh7adbcff2017-03-20 15:29:28 +00006982 if( db->nVdbeExec>1 ){
6983 char *z = sqlite3MPrintf(db, "-- %s", zTrace);
6984 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, z);
6985 sqlite3DbFree(db, z);
6986 }else{
drhbd441f72016-07-25 02:31:48 +00006987 (void)db->xTrace(SQLITE_TRACE_STMT, db->pTraceArg, p, zTrace);
drh3d2a5292016-07-13 22:55:01 +00006988 }
drh949f9cd2008-01-12 21:35:57 +00006989 }
drh8f8b2312013-10-18 20:03:43 +00006990#ifdef SQLITE_USE_FCNTL_TRACE
6991 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6992 if( zTrace ){
mistachkind8992ce2016-09-20 17:49:01 +00006993 int j;
6994 for(j=0; j<db->nDb; j++){
6995 if( DbMaskTest(p->btreeMask, j)==0 ) continue;
6996 sqlite3_file_control(db, db->aDb[j].zDbSName, SQLITE_FCNTL_TRACE, zTrace);
drh8f8b2312013-10-18 20:03:43 +00006997 }
6998 }
6999#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00007000#ifdef SQLITE_DEBUG
7001 if( (db->flags & SQLITE_SqlTrace)!=0
7002 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
7003 ){
7004 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
7005 }
7006#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00007007#endif /* SQLITE_OMIT_TRACE */
drh4910a762016-09-03 01:46:15 +00007008 assert( pOp->p2>0 );
drh9e5eb9c2016-09-18 16:08:10 +00007009 if( pOp->p1>=sqlite3GlobalConfig.iOnceResetThreshold ){
7010 for(i=1; i<p->nOp; i++){
7011 if( p->aOp[i].opcode==OP_Once ) p->aOp[i].p1 = 0;
7012 }
7013 pOp->p1 = 0;
7014 }
7015 pOp->p1++;
drh4910a762016-09-03 01:46:15 +00007016 goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00007017}
drh949f9cd2008-01-12 21:35:57 +00007018
drh28935362013-12-07 20:39:19 +00007019#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00007020/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00007021**
7022** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00007023** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
7024** to values currently held in registers. TK_COLUMN terms in the P4
7025** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00007026*/
7027case OP_CursorHint: {
7028 VdbeCursor *pC;
7029
7030 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7031 assert( pOp->p4type==P4_EXPR );
7032 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00007033 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00007034 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00007035 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
7036 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00007037 }
drh28935362013-12-07 20:39:19 +00007038 break;
7039}
7040#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00007041
7042/* Opcode: Noop * * * * *
7043**
7044** Do nothing. This instruction is often useful as a jump
7045** destination.
drh5e00f6c2001-09-13 13:46:56 +00007046*/
drh91fd4d42008-01-19 20:11:25 +00007047/*
7048** The magic Explain opcode are only inserted when explain==2 (which
7049** is to say when the EXPLAIN QUERY PLAN syntax is used.)
7050** This opcode records information from the optimizer. It is the
7051** the same as a no-op. This opcodesnever appears in a real VM program.
7052*/
7053default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00007054 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00007055 break;
7056}
7057
7058/*****************************************************************************
7059** The cases of the switch statement above this line should all be indented
7060** by 6 spaces. But the left-most 6 spaces have been removed to improve the
7061** readability. From this point on down, the normal indentation rules are
7062** restored.
7063*****************************************************************************/
7064 }
drh6e142f52000-06-08 13:36:40 +00007065
drh7b396862003-01-01 23:06:20 +00007066#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00007067 {
drha01c7c72014-04-25 12:35:31 +00007068 u64 endTime = sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00007069 if( endTime>start ) pOrigOp->cycles += endTime - start;
7070 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00007071 }
drh7b396862003-01-01 23:06:20 +00007072#endif
7073
drh6e142f52000-06-08 13:36:40 +00007074 /* The following code adds nothing to the actual functionality
7075 ** of the program. It is only here for testing and debugging.
7076 ** On the other hand, it does burn CPU cycles every time through
7077 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
7078 */
7079#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00007080 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00007081
drhcf1023c2007-05-08 20:59:49 +00007082#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00007083 if( db->flags & SQLITE_VdbeTrace ){
drh7cc84c22016-04-11 13:36:42 +00007084 u8 opProperty = sqlite3OpcodeProperty[pOrigOp->opcode];
drh84e55a82013-11-13 17:58:23 +00007085 if( rc!=0 ) printf("rc=%d\n",rc);
drh7cc84c22016-04-11 13:36:42 +00007086 if( opProperty & (OPFLG_OUT2) ){
drh6dc41482015-04-16 17:31:02 +00007087 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00007088 }
drh7cc84c22016-04-11 13:36:42 +00007089 if( opProperty & OPFLG_OUT3 ){
drh6dc41482015-04-16 17:31:02 +00007090 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00007091 }
drh75897232000-05-29 14:26:00 +00007092 }
danielk1977b5402fb2005-01-12 07:15:04 +00007093#endif /* SQLITE_DEBUG */
7094#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00007095 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00007096
drha05a7222008-01-19 03:35:58 +00007097 /* If we reach this point, it means that execution is finished with
7098 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00007099 */
drh9467abf2016-02-17 18:44:11 +00007100abort_due_to_error:
7101 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00007102 assert( rc );
drh9467abf2016-02-17 18:44:11 +00007103 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
7104 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
7105 }
drha05a7222008-01-19 03:35:58 +00007106 p->rc = rc;
drhf68521c2016-03-21 12:28:02 +00007107 sqlite3SystemError(db, rc);
drha64fa912010-03-04 00:53:32 +00007108 testcase( sqlite3GlobalConfig.xLog!=0 );
7109 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00007110 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00007111 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00007112 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00007113 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00007114 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00007115 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00007116 }
drh900b31e2007-08-28 02:27:51 +00007117
7118 /* This is the only way out of this procedure. We have to
7119 ** release the mutexes on btrees that were acquired at the
7120 ** top. */
7121vdbe_return:
drh77dfd5b2013-08-19 11:15:48 +00007122 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00007123 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00007124 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00007125 assert( rc!=SQLITE_OK || nExtraDelete==0
7126 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
7127 );
drhb86ccfb2003-01-28 23:13:10 +00007128 return rc;
7129
drh023ae032007-05-08 12:12:16 +00007130 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7131 ** is encountered.
7132 */
7133too_big:
drh22c17b82015-05-15 04:13:15 +00007134 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00007135 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00007136 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00007137
drh98640a32007-06-07 19:08:32 +00007138 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00007139 */
7140no_mem:
drh4a642b62016-02-05 01:55:27 +00007141 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00007142 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00007143 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00007144 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007145
danielk19776f8a5032004-05-10 10:34:51 +00007146 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00007147 ** flag.
7148 */
7149abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00007150 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00007151 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00007152 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00007153 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00007154 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00007155}