blob: 0f8d75d302c1965efa45c50b3fae5abb6328b853 [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/*
drh0fd61352014-02-07 02:29:45 +000090** The next global variable is incremented each time the OP_Found opcode
dan0ff297e2009-09-25 17:03:14 +000091** is executed. This is used to test whether or not the foreign key
92** operation implemented using OP_FkIsZero is working. This variable
93** has no function other than to help verify the correct operation of the
94** library.
95*/
96#ifdef SQLITE_TEST
97int sqlite3_found_count = 0;
98#endif
99
100/*
drhb7654112008-01-12 12:48:07 +0000101** Test a register to see if it exceeds the current maximum blob size.
102** If it does, record the new maximum blob size.
103*/
drh678ccce2008-03-31 18:19:54 +0000104#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000105# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000106#else
107# define UPDATE_MAX_BLOBSIZE(P)
108#endif
109
110/*
drh5655c542014-02-19 19:14:34 +0000111** Invoke the VDBE coverage callback, if that callback is defined. This
112** feature is used for test suite validation only and does not appear an
113** production builds.
114**
115** M is an integer, 2 or 3, that indices how many different ways the
116** branch can go. It is usually 2. "I" is the direction the branch
117** goes. 0 means falls through. 1 means branch is taken. 2 means the
118** second alternative branch is taken.
drh4336b0e2014-08-05 00:53:51 +0000119**
120** iSrcLine is the source code line (from the __LINE__ macro) that
121** generated the VDBE instruction. This instrumentation assumes that all
122** source code is in a single file (the amalgamation). Special values 1
123** and 2 for the iSrcLine parameter mean that this particular branch is
124** always taken or never taken, respectively.
drh688852a2014-02-17 22:40:43 +0000125*/
126#if !defined(SQLITE_VDBE_COVERAGE)
127# define VdbeBranchTaken(I,M)
128#else
drh5655c542014-02-19 19:14:34 +0000129# define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
130 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
131 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
132 M = iSrcLine;
133 /* Assert the truth of VdbeCoverageAlwaysTaken() and
134 ** VdbeCoverageNeverTaken() */
135 assert( (M & I)==I );
136 }else{
137 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/
138 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
139 iSrcLine,I,M);
140 }
141 }
drh688852a2014-02-17 22:40:43 +0000142#endif
143
144/*
drh9cbf3422008-01-17 16:22:13 +0000145** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000146** already. Return non-zero if a malloc() fails.
147*/
drhb21c8cd2007-08-21 19:33:56 +0000148#define Stringify(P, enc) \
drhbd9507c2014-08-23 17:21:37 +0000149 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
drhf4479502004-05-27 03:12:53 +0000150 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000151
152/*
danielk1977bd7e4602004-05-24 07:34:48 +0000153** An ephemeral string value (signified by the MEM_Ephem flag) contains
154** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000155** is responsible for deallocating that string. Because the register
156** does not control the string, it might be deleted without the register
157** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000158**
159** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000160** string that the register itself controls. In other words, it
drhc91b2fd2014-03-01 18:13:23 +0000161** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
danielk1977bd7e4602004-05-24 07:34:48 +0000162*/
drhb21c8cd2007-08-21 19:33:56 +0000163#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000164 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000165 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000166
dan689ab892011-08-12 15:02:00 +0000167/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drhc960dcb2015-11-20 19:22:01 +0000168#define isSorter(x) ((x)->eCurType==CURTYPE_SORTER)
danielk19778a6b5412004-05-24 07:04:25 +0000169
170/*
drhdfe88ec2008-11-03 20:55:06 +0000171** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000172** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000173*/
drhdfe88ec2008-11-03 20:55:06 +0000174static VdbeCursor *allocateCursor(
175 Vdbe *p, /* The virtual machine */
176 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000177 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000178 int iDb, /* Database the cursor belongs to, or -1 */
drhc960dcb2015-11-20 19:22:01 +0000179 u8 eCurType /* Type of the new cursor */
danielk1977cd3e8f72008-03-25 09:47:35 +0000180){
181 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000182 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000183 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000184 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000185 **
186 ** * Sometimes cursor numbers are used for a couple of different
187 ** purposes in a vdbe program. The different uses might require
188 ** different sized allocations. Memory cells provide growable
189 ** allocations.
190 **
191 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
192 ** be freed lazily via the sqlite3_release_memory() API. This
193 ** minimizes the number of malloc calls made by the system.
194 **
195 ** Memory cells for cursors are allocated at the top of the address
196 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
197 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
198 */
199 Mem *pMem = &p->aMem[p->nMem-iCur];
200
danielk19775f096132008-03-28 15:44:09 +0000201 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000202 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000203 nByte =
drh5cc10232013-11-21 01:04:02 +0000204 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
drhc960dcb2015-11-20 19:22:01 +0000205 (eCurType==CURTYPE_BTREE?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000206
drh290c1942004-08-21 17:54:45 +0000207 assert( iCur<p->nCursor );
208 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000209 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000210 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000211 }
drh322f2852014-09-19 00:43:39 +0000212 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
drhdfe88ec2008-11-03 20:55:06 +0000213 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000214 memset(pCx, 0, sizeof(VdbeCursor));
drhc960dcb2015-11-20 19:22:01 +0000215 pCx->eCurType = eCurType;
danielk197794eb6a12005-12-15 15:22:08 +0000216 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000217 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000218 pCx->aOffset = &pCx->aType[nField];
drhc960dcb2015-11-20 19:22:01 +0000219 if( eCurType==CURTYPE_BTREE ){
220 pCx->uc.pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000221 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhc960dcb2015-11-20 19:22:01 +0000222 sqlite3BtreeCursorZero(pCx->uc.pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000223 }
danielk197794eb6a12005-12-15 15:22:08 +0000224 }
drh4774b132004-06-12 20:12:51 +0000225 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000226}
227
danielk19773d1bfea2004-05-14 11:00:53 +0000228/*
drh29d72102006-02-09 22:13:41 +0000229** Try to convert a value into a numeric representation if we can
230** do so without loss of information. In other words, if the string
231** looks like a number, convert it into a number. If it does not
232** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000233**
234** If the bTryForInt flag is true, then extra effort is made to give
235** an integer representation. Strings that look like floating point
236** values but which have no fractional component (example: '48.00')
237** will have a MEM_Int representation when bTryForInt is true.
238**
239** If bTryForInt is false, then if the input string contains a decimal
240** point or exponential notation, the result is only MEM_Real, even
241** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000242*/
drhbd9507c2014-08-23 17:21:37 +0000243static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000244 double rValue;
245 i64 iValue;
246 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000247 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000248 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
249 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
250 pRec->u.i = iValue;
251 pRec->flags |= MEM_Int;
252 }else{
drh74eaba42014-09-18 17:52:15 +0000253 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000254 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000255 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000256 }
257}
258
259/*
drh8a512562005-11-14 22:29:05 +0000260** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000261**
drh8a512562005-11-14 22:29:05 +0000262** SQLITE_AFF_INTEGER:
263** SQLITE_AFF_REAL:
264** SQLITE_AFF_NUMERIC:
265** Try to convert pRec to an integer representation or a
266** floating-point representation if an integer representation
267** is not possible. Note that the integer representation is
268** always preferred, even if the affinity is REAL, because
269** an integer representation is more space efficient on disk.
270**
271** SQLITE_AFF_TEXT:
272** Convert pRec to a text representation.
273**
drh05883a32015-06-02 15:32:08 +0000274** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000275** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000276*/
drh17435752007-08-16 04:30:38 +0000277static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000278 Mem *pRec, /* The value to apply affinity to */
279 char affinity, /* The affinity to be applied */
280 u8 enc /* Use this text encoding */
281){
drh7ea31cc2014-09-18 14:36:00 +0000282 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000283 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
284 || affinity==SQLITE_AFF_NUMERIC );
drhbd9507c2014-08-23 17:21:37 +0000285 if( (pRec->flags & MEM_Int)==0 ){
286 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000287 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000288 }else{
289 sqlite3VdbeIntegerAffinity(pRec);
290 }
drh17c40292004-07-21 02:53:29 +0000291 }
drh7ea31cc2014-09-18 14:36:00 +0000292 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000293 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000294 ** representation (blob and NULL do not get converted) but no string
danielk19773d1bfea2004-05-14 11:00:53 +0000295 ** representation.
296 */
297 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drh7ea31cc2014-09-18 14:36:00 +0000298 sqlite3VdbeMemStringify(pRec, enc, 1);
danielk19773d1bfea2004-05-14 11:00:53 +0000299 }
300 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000301 }
302}
303
danielk1977aee18ef2005-03-09 12:26:50 +0000304/*
drh29d72102006-02-09 22:13:41 +0000305** Try to convert the type of a function argument or a result column
306** into a numeric representation. Use either INTEGER or REAL whichever
307** is appropriate. But only do the conversion if it is possible without
308** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000309*/
310int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000311 int eType = sqlite3_value_type(pVal);
312 if( eType==SQLITE_TEXT ){
313 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000314 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000315 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000316 }
drh1b27b8c2014-02-10 03:21:57 +0000317 return eType;
drh29d72102006-02-09 22:13:41 +0000318}
319
320/*
danielk1977aee18ef2005-03-09 12:26:50 +0000321** Exported version of applyAffinity(). This one works on sqlite3_value*,
322** not the internal Mem* type.
323*/
danielk19771e536952007-08-16 10:09:01 +0000324void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000325 sqlite3_value *pVal,
326 u8 affinity,
327 u8 enc
328){
drhb21c8cd2007-08-21 19:33:56 +0000329 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000330}
331
drh3d1d90a2014-03-24 15:00:15 +0000332/*
drhf1a89ed2014-08-23 17:41:15 +0000333** pMem currently only holds a string type (or maybe a BLOB that we can
334** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000335** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000336** accordingly.
337*/
338static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
339 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
340 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000341 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000342 return 0;
343 }
344 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
345 return MEM_Int;
346 }
347 return MEM_Real;
348}
349
350/*
drh3d1d90a2014-03-24 15:00:15 +0000351** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
352** none.
353**
354** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000355** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000356*/
357static u16 numericType(Mem *pMem){
358 if( pMem->flags & (MEM_Int|MEM_Real) ){
359 return pMem->flags & (MEM_Int|MEM_Real);
360 }
361 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000362 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000363 }
364 return 0;
365}
366
danielk1977b5402fb2005-01-12 07:15:04 +0000367#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000368/*
danielk1977ca6b2912004-05-21 10:49:47 +0000369** Write a nice string representation of the contents of cell pMem
370** into buffer zBuf, length nBuf.
371*/
drh74161702006-02-24 02:53:49 +0000372void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000373 char *zCsr = zBuf;
374 int f = pMem->flags;
375
drh57196282004-10-06 15:41:16 +0000376 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000377
danielk1977ca6b2912004-05-21 10:49:47 +0000378 if( f&MEM_Blob ){
379 int i;
380 char c;
381 if( f & MEM_Dyn ){
382 c = 'z';
383 assert( (f & (MEM_Static|MEM_Ephem))==0 );
384 }else if( f & MEM_Static ){
385 c = 't';
386 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
387 }else if( f & MEM_Ephem ){
388 c = 'e';
389 assert( (f & (MEM_Static|MEM_Dyn))==0 );
390 }else{
391 c = 's';
392 }
393
drh5bb3eb92007-05-04 13:15:55 +0000394 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000395 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000396 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000397 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000398 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000399 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000400 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000401 }
402 for(i=0; i<16 && i<pMem->n; i++){
403 char z = pMem->z[i];
404 if( z<32 || z>126 ) *zCsr++ = '.';
405 else *zCsr++ = z;
406 }
407
drhe718efe2007-05-10 21:14:03 +0000408 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000409 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000410 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000411 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000412 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000413 }
danielk1977b1bc9532004-05-22 03:05:33 +0000414 *zCsr = '\0';
415 }else if( f & MEM_Str ){
416 int j, k;
417 zBuf[0] = ' ';
418 if( f & MEM_Dyn ){
419 zBuf[1] = 'z';
420 assert( (f & (MEM_Static|MEM_Ephem))==0 );
421 }else if( f & MEM_Static ){
422 zBuf[1] = 't';
423 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
424 }else if( f & MEM_Ephem ){
425 zBuf[1] = 'e';
426 assert( (f & (MEM_Static|MEM_Dyn))==0 );
427 }else{
428 zBuf[1] = 's';
429 }
430 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000431 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000432 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000433 zBuf[k++] = '[';
434 for(j=0; j<15 && j<pMem->n; j++){
435 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000436 if( c>=0x20 && c<0x7f ){
437 zBuf[k++] = c;
438 }else{
439 zBuf[k++] = '.';
440 }
441 }
442 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000443 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000444 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000445 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000446 }
danielk1977ca6b2912004-05-21 10:49:47 +0000447}
448#endif
449
drh5b6afba2008-01-05 16:29:28 +0000450#ifdef SQLITE_DEBUG
451/*
452** Print the value of a register for tracing purposes:
453*/
drh84e55a82013-11-13 17:58:23 +0000454static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000455 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000456 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000457 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000458 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000459 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000460 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000461 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000462 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000463#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000464 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000465 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000466#endif
drh733bf1b2009-04-22 00:47:00 +0000467 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000468 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000469 }else{
470 char zBuf[200];
471 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000472 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000473 }
dan5b6c8e42016-01-30 15:46:03 +0000474 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
drh5b6afba2008-01-05 16:29:28 +0000475}
drh84e55a82013-11-13 17:58:23 +0000476static void registerTrace(int iReg, Mem *p){
477 printf("REG[%d] = ", iReg);
478 memTracePrint(p);
479 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000480}
481#endif
482
483#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000484# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000485#else
486# define REGISTER_TRACE(R,M)
487#endif
488
danielk197784ac9d02004-05-18 09:58:06 +0000489
drh7b396862003-01-01 23:06:20 +0000490#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000491
492/*
493** hwtime.h contains inline assembler code for implementing
494** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000495*/
shane9bcbdad2008-05-29 20:22:37 +0000496#include "hwtime.h"
497
drh7b396862003-01-01 23:06:20 +0000498#endif
499
danielk1977fd7f0452008-12-17 17:30:26 +0000500#ifndef NDEBUG
501/*
502** This function is only called from within an assert() expression. It
503** checks that the sqlite3.nTransaction variable is correctly set to
504** the number of non-transaction savepoints currently in the
505** linked list starting at sqlite3.pSavepoint.
506**
507** Usage:
508**
509** assert( checkSavepointCount(db) );
510*/
511static int checkSavepointCount(sqlite3 *db){
512 int n = 0;
513 Savepoint *p;
514 for(p=db->pSavepoint; p; p=p->pNext) n++;
515 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
516 return 1;
517}
518#endif
519
drh27a348c2015-04-13 19:14:06 +0000520/*
521** Return the register of pOp->p2 after first preparing it to be
522** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000523*/
524static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
525 sqlite3VdbeMemSetNull(pOut);
526 pOut->flags = MEM_Int;
527 return pOut;
528}
drh27a348c2015-04-13 19:14:06 +0000529static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
530 Mem *pOut;
531 assert( pOp->p2>0 );
532 assert( pOp->p2<=(p->nMem-p->nCursor) );
533 pOut = &p->aMem[pOp->p2];
534 memAboutToChange(p, pOut);
drh9eef8c62015-10-15 17:31:41 +0000535 if( VdbeMemDynamic(pOut) ){
536 return out2PrereleaseWithClear(pOut);
537 }else{
538 pOut->flags = MEM_Int;
539 return pOut;
540 }
drh27a348c2015-04-13 19:14:06 +0000541}
542
drhb9755982010-07-24 16:34:37 +0000543
544/*
drh0fd61352014-02-07 02:29:45 +0000545** Execute as much of a VDBE program as we can.
546** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000547*/
danielk19774adee202004-05-08 08:23:19 +0000548int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000549 Vdbe *p /* The VDBE */
550){
drhbbe879d2009-11-14 18:04:35 +0000551 Op *aOp = p->aOp; /* Copy of p->aOp */
drhf56fa462015-04-13 21:39:54 +0000552 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000553#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
554 Op *pOrigOp; /* Value of pOp at the top of the loop */
555#endif
drhb89aeb62016-01-27 15:49:32 +0000556#ifdef SQLITE_DEBUG
drhdef19e32016-01-27 16:26:25 +0000557 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
drhb89aeb62016-01-27 15:49:32 +0000558#endif
drhb86ccfb2003-01-28 23:13:10 +0000559 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000560 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000561 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000562 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000563 int iCompare = 0; /* Result of last OP_Compare operation */
564 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000565#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000566 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000567#endif
drha6c2ed92009-11-14 23:22:23 +0000568 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000569 Mem *pIn1 = 0; /* 1st input operand */
570 Mem *pIn2 = 0; /* 2nd input operand */
571 Mem *pIn3 = 0; /* 3rd input operand */
572 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000573 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000574 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000575#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000576 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000577#endif
drh856c1032009-06-02 15:21:42 +0000578 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000579
drhca48c902008-01-18 14:08:24 +0000580 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000581 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000582 if( p->rc==SQLITE_NOMEM ){
583 /* This happens if a malloc() inside a call to sqlite3_column_text() or
584 ** sqlite3_column_text16() failed. */
585 goto no_mem;
586 }
drhcbd8db32015-08-20 17:18:32 +0000587 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000588 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000589 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000590 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000591 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000592 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000593 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000594 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000595 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000596#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
597 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000598 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000599 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000600 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh0d1961e2013-07-25 16:27:51 +0000601 }
602#endif
drh3c23a882007-01-09 14:01:13 +0000603#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000604 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000605 if( p->pc==0
606 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
607 ){
drh3c23a882007-01-09 14:01:13 +0000608 int i;
drh84e55a82013-11-13 17:58:23 +0000609 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000610 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000611 if( p->db->flags & SQLITE_VdbeListing ){
612 printf("VDBE Program Listing:\n");
613 for(i=0; i<p->nOp; i++){
614 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
615 }
drh3c23a882007-01-09 14:01:13 +0000616 }
drh84e55a82013-11-13 17:58:23 +0000617 if( p->db->flags & SQLITE_VdbeEQP ){
618 for(i=0; i<p->nOp; i++){
619 if( aOp[i].opcode==OP_Explain ){
620 if( once ) printf("VDBE Query Plan:\n");
621 printf("%s\n", aOp[i].p4.z);
622 once = 0;
623 }
624 }
625 }
626 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000627 }
danielk19772d1d86f2008-06-20 14:59:51 +0000628 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000629#endif
drh9467abf2016-02-17 18:44:11 +0000630 for(pOp=&aOp[p->pc]; 1; pOp++){
631 /* Errors are detected by individual opcodes, with an immediate
632 ** jumps to abort_due_to_error. */
633 assert( rc==SQLITE_OK );
634
drhf56fa462015-04-13 21:39:54 +0000635 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh7b396862003-01-01 23:06:20 +0000636#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000637 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000638#endif
drhbf159fa2013-06-25 22:01:22 +0000639 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000640#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000641 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000642#endif
drh6e142f52000-06-08 13:36:40 +0000643
danielk19778b60e0f2005-01-12 09:10:39 +0000644 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000645 */
danielk19778b60e0f2005-01-12 09:10:39 +0000646#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000647 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000648 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000649 }
drh3f7d4e42004-07-24 14:35:58 +0000650#endif
651
drh6e142f52000-06-08 13:36:40 +0000652
drhf6038712004-02-08 18:07:34 +0000653 /* Check to see if we need to simulate an interrupt. This only happens
654 ** if we have a special test build.
655 */
656#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000657 if( sqlite3_interrupt_count>0 ){
658 sqlite3_interrupt_count--;
659 if( sqlite3_interrupt_count==0 ){
660 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000661 }
662 }
663#endif
664
drh3c657212009-11-17 23:59:58 +0000665 /* Sanity checking on other operands */
666#ifdef SQLITE_DEBUG
drh27a348c2015-04-13 19:14:06 +0000667 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000668 if( (pOp->opflags & OPFLG_IN1)!=0 ){
669 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000670 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000671 assert( memIsValid(&aMem[pOp->p1]) );
drh75fd0542014-03-01 16:24:44 +0000672 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000673 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
674 }
675 if( (pOp->opflags & OPFLG_IN2)!=0 ){
676 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000677 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000678 assert( memIsValid(&aMem[pOp->p2]) );
drh75fd0542014-03-01 16:24:44 +0000679 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000680 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
681 }
682 if( (pOp->opflags & OPFLG_IN3)!=0 ){
683 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000684 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000685 assert( memIsValid(&aMem[pOp->p3]) );
drh75fd0542014-03-01 16:24:44 +0000686 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000687 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
688 }
689 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
690 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000691 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000692 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000693 }
694 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
695 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000696 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000697 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000698 }
699#endif
drh6dc41482015-04-16 17:31:02 +0000700#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
701 pOrigOp = pOp;
702#endif
drh93952eb2009-11-13 19:43:43 +0000703
drh75897232000-05-29 14:26:00 +0000704 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000705
drh5e00f6c2001-09-13 13:46:56 +0000706/*****************************************************************************
707** What follows is a massive switch statement where each case implements a
708** separate instruction in the virtual machine. If we follow the usual
709** indentation conventions, each case should be indented by 6 spaces. But
710** that is a lot of wasted space on the left margin. So the code within
711** the switch statement will break with convention and be flush-left. Another
712** big comment (similar to this one) will mark the point in the code where
713** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000714**
715** The formatting of each case is important. The makefile for SQLite
716** generates two C files "opcodes.h" and "opcodes.c" by scanning this
717** file looking for lines that begin with "case OP_". The opcodes.h files
718** will be filled with #defines that give unique integer values to each
719** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000720** each string is the symbolic name for the corresponding opcode. If the
721** case statement is followed by a comment of the form "/# same as ... #/"
722** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000723**
drh9cbf3422008-01-17 16:22:13 +0000724** Other keywords in the comment that follows each case are used to
725** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000726** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000727** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000728**
drhac82fcf2002-09-08 17:23:41 +0000729** Documentation about VDBE opcodes is generated by scanning this file
730** for lines of that contain "Opcode:". That line and all subsequent
731** comment lines are used in the generation of the opcode.html documentation
732** file.
733**
734** SUMMARY:
735**
736** Formatting is important to scripts that scan this file.
737** Do not deviate from the formatting style currently in use.
738**
drh5e00f6c2001-09-13 13:46:56 +0000739*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000740
drh9cbf3422008-01-17 16:22:13 +0000741/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000742**
743** An unconditional jump to address P2.
744** The next instruction executed will be
745** the one at index P2 from the beginning of
746** the program.
drhfe705102014-03-06 13:38:37 +0000747**
748** The P1 parameter is not actually used by this opcode. However, it
749** is sometimes set to 1 instead of 0 as a hint to the command-line shell
750** that this Goto is the bottom of a loop and that the lines from P2 down
751** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000752*/
drh9cbf3422008-01-17 16:22:13 +0000753case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000754jump_to_p2_and_check_for_interrupt:
755 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000756
757 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
758 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
759 ** completion. Check to see if sqlite3_interrupt() has been called
drhe514f652016-02-04 19:50:33 +0000760 ** or if the progress callback needs to be invoked.
drh49afe3a2013-07-10 03:05:14 +0000761 **
762 ** This code uses unstructured "goto" statements and does not look clean.
763 ** But that is not due to sloppy coding habits. The code is written this
764 ** way for performance, to avoid having to run the interrupt and progress
drhe514f652016-02-04 19:50:33 +0000765 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
drh49afe3a2013-07-10 03:05:14 +0000766 ** faster according to "valgrind --tool=cachegrind" */
767check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000768 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000769#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
770 /* Call the progress callback if it is configured and the required number
771 ** of VDBE ops have been executed (either since this invocation of
772 ** sqlite3VdbeExec() or since last time the progress callback was called).
773 ** If the progress callback returns non-zero, exit the virtual machine with
774 ** a return code SQLITE_ABORT.
775 */
drh0d1961e2013-07-25 16:27:51 +0000776 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000777 assert( db->nProgressOps!=0 );
778 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
779 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000780 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +0000781 goto abort_due_to_error;
drh49afe3a2013-07-10 03:05:14 +0000782 }
drh49afe3a2013-07-10 03:05:14 +0000783 }
784#endif
785
drh5e00f6c2001-09-13 13:46:56 +0000786 break;
787}
drh75897232000-05-29 14:26:00 +0000788
drh2eb95372008-06-06 15:04:36 +0000789/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000790**
drh2eb95372008-06-06 15:04:36 +0000791** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000792** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000793*/
drhb8475df2011-12-09 16:21:19 +0000794case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000795 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000796 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000797 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000798 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000799 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000800 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000801 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000802
803 /* Most jump operations do a goto to this spot in order to update
804 ** the pOp pointer. */
805jump_to_p2:
806 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000807 break;
808}
809
drh2eb95372008-06-06 15:04:36 +0000810/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000811**
drh81cf13e2014-02-07 18:27:53 +0000812** Jump to the next instruction after the address in register P1. After
813** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000814*/
drh2eb95372008-06-06 15:04:36 +0000815case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000816 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000817 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000818 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000819 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000820 break;
821}
822
drhed71a832014-02-07 19:18:10 +0000823/* Opcode: InitCoroutine P1 P2 P3 * *
drhe00ee6e2008-06-20 15:24:01 +0000824**
drh5dad9a32014-07-25 18:37:42 +0000825** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000826** located at address P3.
827**
drh5dad9a32014-07-25 18:37:42 +0000828** If P2!=0 then the coroutine implementation immediately follows
829** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000830** address P2.
drh5dad9a32014-07-25 18:37:42 +0000831**
832** See also: EndCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000833*/
drh81cf13e2014-02-07 18:27:53 +0000834case OP_InitCoroutine: { /* jump */
drhed71a832014-02-07 19:18:10 +0000835 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
836 assert( pOp->p2>=0 && pOp->p2<p->nOp );
837 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000838 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000839 assert( !VdbeMemDynamic(pOut) );
840 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000841 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000842 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000843 break;
844}
845
846/* Opcode: EndCoroutine P1 * * * *
847**
drhbc5cf382014-08-06 01:08:07 +0000848** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000849** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000850** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000851**
852** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000853*/
854case OP_EndCoroutine: { /* in1 */
855 VdbeOp *pCaller;
856 pIn1 = &aMem[pOp->p1];
857 assert( pIn1->flags==MEM_Int );
858 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
859 pCaller = &aOp[pIn1->u.i];
860 assert( pCaller->opcode==OP_Yield );
861 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000862 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000863 pIn1->flags = MEM_Undefined;
864 break;
865}
866
867/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000868**
drh5dad9a32014-07-25 18:37:42 +0000869** Swap the program counter with the value in register P1. This
870** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000871**
drh5dad9a32014-07-25 18:37:42 +0000872** If the coroutine that is launched by this instruction ends with
873** Yield or Return then continue to the next instruction. But if
874** the coroutine launched by this instruction ends with
875** EndCoroutine, then jump to P2 rather than continuing with the
876** next instruction.
877**
878** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000879*/
drh81cf13e2014-02-07 18:27:53 +0000880case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000881 int pcDest;
drh3c657212009-11-17 23:59:58 +0000882 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000883 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000884 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000885 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000886 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000887 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000888 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000889 break;
890}
891
drhf9c8ce32013-11-05 13:33:55 +0000892/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh0fd61352014-02-07 02:29:45 +0000893** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000894**
drhef8662b2011-06-20 21:47:58 +0000895** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000896** parameter P1, P2, and P4 as if this were a Halt instruction. If the
897** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000898** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000899*/
900case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000901 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000902 if( (pIn3->flags & MEM_Null)==0 ) break;
903 /* Fall through into OP_Halt */
904}
drhe00ee6e2008-06-20 15:24:01 +0000905
drhf9c8ce32013-11-05 13:33:55 +0000906/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000907**
drh3d4501e2008-12-04 20:40:10 +0000908** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000909** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000910**
drh92f02c32004-09-02 14:57:08 +0000911** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
912** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
913** For errors, it can be some other value. If P1!=0 then P2 will determine
914** whether or not to rollback the current transaction. Do not rollback
915** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
916** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000917** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000918**
drh66a51672008-01-03 00:01:23 +0000919** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000920**
drhf9c8ce32013-11-05 13:33:55 +0000921** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
922**
923** 0: (no change)
924** 1: NOT NULL contraint failed: P4
925** 2: UNIQUE constraint failed: P4
926** 3: CHECK constraint failed: P4
927** 4: FOREIGN KEY constraint failed: P4
928**
929** If P5 is not zero and P4 is NULL, then everything after the ":" is
930** omitted.
931**
drh9cfcf5d2002-01-29 18:41:24 +0000932** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000933** every program. So a jump past the last instruction of the program
934** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000935*/
drh9cbf3422008-01-17 16:22:13 +0000936case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000937 const char *zType;
938 const char *zLogFmt;
drhf56fa462015-04-13 21:39:54 +0000939 VdbeFrame *pFrame;
940 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000941
drhf56fa462015-04-13 21:39:54 +0000942 pcx = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +0000943 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000944 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000945 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000946 p->pFrame = pFrame->pParent;
947 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000948 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000949 pcx = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000950 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000951 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000952 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000953 ** currently being halted. If the p2 instruction of this OP_Halt
954 ** instruction is set to OE_Ignore, then the sub-program is throwing
955 ** an IGNORE exception. In this case jump to the address specified
956 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +0000957 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +0000958 }
drhbbe879d2009-11-14 18:04:35 +0000959 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000960 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +0000961 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +0000962 break;
963 }
drh92f02c32004-09-02 14:57:08 +0000964 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000965 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +0000966 p->pc = pcx;
drhf9c8ce32013-11-05 13:33:55 +0000967 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000968 if( pOp->p5 ){
969 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
970 "FOREIGN KEY" };
971 assert( pOp->p5>=1 && pOp->p5<=4 );
972 testcase( pOp->p5==1 );
973 testcase( pOp->p5==2 );
974 testcase( pOp->p5==3 );
975 testcase( pOp->p5==4 );
976 zType = azType[pOp->p5-1];
977 }else{
978 zType = 0;
979 }
drh4308e342013-11-11 16:55:52 +0000980 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000981 zLogFmt = "abort at %d in [%s]: %s";
982 if( zType && pOp->p4.z ){
drh22c17b82015-05-15 04:13:15 +0000983 sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000984 }else if( pOp->p4.z ){
drh22c17b82015-05-15 04:13:15 +0000985 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000986 }else{
drh22c17b82015-05-15 04:13:15 +0000987 sqlite3VdbeError(p, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000988 }
drhf56fa462015-04-13 21:39:54 +0000989 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000990 }
drh92f02c32004-09-02 14:57:08 +0000991 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000992 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000993 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000994 p->rc = rc = SQLITE_BUSY;
995 }else{
drhd91c1a12013-02-09 13:58:25 +0000996 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000997 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000998 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000999 }
drh900b31e2007-08-28 02:27:51 +00001000 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +00001001}
drhc61053b2000-06-04 12:58:36 +00001002
drh4c583122008-01-04 22:01:03 +00001003/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001004** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +00001005**
drh9cbf3422008-01-17 16:22:13 +00001006** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +00001007*/
drh27a348c2015-04-13 19:14:06 +00001008case OP_Integer: { /* out2 */
1009 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001010 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001011 break;
1012}
1013
drh4c583122008-01-04 22:01:03 +00001014/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001015** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001016**
drh66a51672008-01-03 00:01:23 +00001017** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001018** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001019*/
drh27a348c2015-04-13 19:14:06 +00001020case OP_Int64: { /* out2 */
1021 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001022 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001023 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001024 break;
1025}
drh4f26d6c2004-05-26 23:25:30 +00001026
drh13573c72010-01-12 17:04:07 +00001027#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001028/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001029** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001030**
drh4c583122008-01-04 22:01:03 +00001031** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001032** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001033*/
drh27a348c2015-04-13 19:14:06 +00001034case OP_Real: { /* same as TK_FLOAT, out2 */
1035 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001036 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001037 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001038 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001039 break;
1040}
drh13573c72010-01-12 17:04:07 +00001041#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001042
drh3c84ddf2008-01-09 02:15:38 +00001043/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001044** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001045**
drh66a51672008-01-03 00:01:23 +00001046** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001047** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001048** this transformation, the length of string P4 is computed and stored
1049** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001050*/
drh27a348c2015-04-13 19:14:06 +00001051case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001052 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001053 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001054 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001055 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001056
1057#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001058 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001059 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
drh9467abf2016-02-17 18:44:11 +00001060 if( rc ){
1061 assert( rc==SQLITE_TOOBIG ); /* This is the only possible error here */
1062 goto too_big;
1063 }
drh4c583122008-01-04 22:01:03 +00001064 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001065 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001066 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001067 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001068 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001069 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001070 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001071 }
drh66a51672008-01-03 00:01:23 +00001072 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001073 pOp->p4.z = pOut->z;
1074 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001075 }
danielk197793758c82005-01-21 08:13:14 +00001076#endif
drhbb4957f2008-03-20 14:03:29 +00001077 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001078 goto too_big;
1079 }
1080 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001081}
drhf4479502004-05-27 03:12:53 +00001082
drhf07cf6e2015-03-06 16:45:16 +00001083/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001084** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001085**
drh9cbf3422008-01-17 16:22:13 +00001086** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001087**
1088** If P5!=0 and the content of register P3 is greater than zero, then
drha9c18a92015-03-06 20:49:52 +00001089** the datatype of the register P2 is converted to BLOB. The content is
1090** the same sequence of bytes, it is merely interpreted as a BLOB instead
1091** of a string, as if it had been CAST.
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
drhf07cf6e2015-03-06 16:45:16 +00001102 if( pOp->p5 ){
1103 assert( pOp->p3>0 );
1104 assert( pOp->p3<=(p->nMem-p->nCursor) );
1105 pIn3 = &aMem[pOp->p3];
1106 assert( pIn3->flags & MEM_Int );
1107 if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
1108 }
drh41d2e662015-12-01 21:23:07 +00001109#endif
danielk1977c572ef72004-05-27 09:28:41 +00001110 break;
1111}
1112
drh053a1282012-09-19 21:15:46 +00001113/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001114** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001115**
drhb8475df2011-12-09 16:21:19 +00001116** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001117** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001118** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001119** set to NULL.
1120**
1121** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1122** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1123** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001124*/
drh27a348c2015-04-13 19:14:06 +00001125case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001126 int cnt;
drh053a1282012-09-19 21:15:46 +00001127 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001128 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001129 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001130 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001131 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
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;
drhb8475df2011-12-09 16:21:19 +00001137 cnt--;
1138 }
drhf0863fe2005-06-12 21:35:51 +00001139 break;
1140}
1141
drh05a86c52014-02-16 01:55:49 +00001142/* Opcode: SoftNull P1 * * * *
1143** Synopsis: r[P1]=NULL
1144**
1145** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1146** instruction, but do not free any string or blob memory associated with
1147** the register, so that if the value was a string or blob that was
1148** previously copied using OP_SCopy, the copies will continue to be valid.
1149*/
1150case OP_SoftNull: {
1151 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
1152 pOut = &aMem[pOp->p1];
1153 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1154 break;
1155}
drhf0863fe2005-06-12 21:35:51 +00001156
drha5750cf2014-02-07 13:20:31 +00001157/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001158** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001159**
drh9de221d2008-01-05 06:51:30 +00001160** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001161** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001162*/
drh27a348c2015-04-13 19:14:06 +00001163case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001164 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001165 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001166 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001167 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001168 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001169 break;
1170}
1171
drheaf52d82010-05-12 13:50:23 +00001172/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001173** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001174**
drheaf52d82010-05-12 13:50:23 +00001175** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001176**
drh0fd61352014-02-07 02:29:45 +00001177** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001178** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001179*/
drh27a348c2015-04-13 19:14:06 +00001180case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001181 Mem *pVar; /* Value being transferred */
1182
drheaf52d82010-05-12 13:50:23 +00001183 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001184 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001185 pVar = &p->aVar[pOp->p1 - 1];
1186 if( sqlite3VdbeMemTooBig(pVar) ){
1187 goto too_big;
drh023ae032007-05-08 12:12:16 +00001188 }
drh27a348c2015-04-13 19:14:06 +00001189 pOut = out2Prerelease(p, pOp);
drheaf52d82010-05-12 13:50:23 +00001190 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1191 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001192 break;
1193}
danielk1977295ba552004-05-19 10:34:51 +00001194
drhb21e7c72008-06-22 12:37:57 +00001195/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001196** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001197**
drh079a3072014-03-19 14:10:55 +00001198** Move the P3 values in register P1..P1+P3-1 over into
1199** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001200** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001201** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1202** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001203*/
drhe1349cb2008-04-01 00:36:10 +00001204case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001205 int n; /* Number of registers left to copy */
1206 int p1; /* Register to copy from */
1207 int p2; /* Register to copy to */
1208
drhe09f43f2013-11-21 04:18:31 +00001209 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001210 p1 = pOp->p1;
1211 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001212 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001213 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001214
drha6c2ed92009-11-14 23:22:23 +00001215 pIn1 = &aMem[p1];
1216 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001217 do{
dan3bc9f742013-08-15 16:18:39 +00001218 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1219 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001220 assert( memIsValid(pIn1) );
1221 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001222 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001223#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001224 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001225 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001226 }
1227#endif
drhbd6789e2015-04-28 14:00:02 +00001228 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001229 REGISTER_TRACE(p2++, pOut);
1230 pIn1++;
1231 pOut++;
drh079a3072014-03-19 14:10:55 +00001232 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001233 break;
1234}
1235
drhe8e4af72012-09-21 00:04:28 +00001236/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001237** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001238**
drhe8e4af72012-09-21 00:04:28 +00001239** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001240**
1241** This instruction makes a deep copy of the value. A duplicate
1242** is made of any string or blob constant. See also OP_SCopy.
1243*/
drhe8e4af72012-09-21 00:04:28 +00001244case OP_Copy: {
1245 int n;
1246
1247 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001248 pIn1 = &aMem[pOp->p1];
1249 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001250 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001251 while( 1 ){
1252 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1253 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001254#ifdef SQLITE_DEBUG
1255 pOut->pScopyFrom = 0;
1256#endif
drhe8e4af72012-09-21 00:04:28 +00001257 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1258 if( (n--)==0 ) break;
1259 pOut++;
1260 pIn1++;
1261 }
drhe1349cb2008-04-01 00:36:10 +00001262 break;
1263}
1264
drhb1fdb2a2008-01-05 04:06:03 +00001265/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001266** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001267**
drh9cbf3422008-01-17 16:22:13 +00001268** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001269**
1270** This instruction makes a shallow copy of the value. If the value
1271** is a string or blob, then the copy is only a pointer to the
1272** original and hence if the original changes so will the copy.
1273** Worse, if the original is deallocated, the copy becomes invalid.
1274** Thus the program must guarantee that the original will not change
1275** during the lifetime of the copy. Use OP_Copy to make a complete
1276** copy.
1277*/
drh26198bb2013-10-31 11:15:09 +00001278case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001279 pIn1 = &aMem[pOp->p1];
1280 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001281 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001282 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001283#ifdef SQLITE_DEBUG
1284 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1285#endif
drh5e00f6c2001-09-13 13:46:56 +00001286 break;
1287}
drh75897232000-05-29 14:26:00 +00001288
drhfed7ac62015-10-15 18:04:59 +00001289/* Opcode: IntCopy P1 P2 * * *
1290** Synopsis: r[P2]=r[P1]
1291**
1292** Transfer the integer value held in register P1 into register P2.
1293**
1294** This is an optimized version of SCopy that works only for integer
1295** values.
1296*/
1297case OP_IntCopy: { /* out2 */
1298 pIn1 = &aMem[pOp->p1];
1299 assert( (pIn1->flags & MEM_Int)!=0 );
1300 pOut = &aMem[pOp->p2];
1301 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1302 break;
1303}
1304
drh9cbf3422008-01-17 16:22:13 +00001305/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001306** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001307**
shane21e7feb2008-05-30 15:59:49 +00001308** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001309** results. This opcode causes the sqlite3_step() call to terminate
1310** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001311** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001312** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001313*/
drh9cbf3422008-01-17 16:22:13 +00001314case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001315 Mem *pMem;
1316 int i;
1317 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001318 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001319 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001320
drhe6400b92013-11-13 23:48:46 +00001321#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1322 /* Run the progress counter just before returning.
1323 */
1324 if( db->xProgress!=0
1325 && nVmStep>=nProgressLimit
1326 && db->xProgress(db->pProgressArg)!=0
1327 ){
1328 rc = SQLITE_INTERRUPT;
drh9467abf2016-02-17 18:44:11 +00001329 goto abort_due_to_error;
drhe6400b92013-11-13 23:48:46 +00001330 }
1331#endif
1332
dan32b09f22009-09-23 17:29:59 +00001333 /* If this statement has violated immediate foreign key constraints, do
1334 ** not return the number of rows modified. And do not RELEASE the statement
1335 ** transaction. It needs to be rolled back. */
1336 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1337 assert( db->flags&SQLITE_CountRows );
1338 assert( p->usesStmtJournal );
drh9467abf2016-02-17 18:44:11 +00001339 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00001340 }
1341
danielk1977bd434552009-03-18 10:33:00 +00001342 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1343 ** DML statements invoke this opcode to return the number of rows
1344 ** modified to the user. This is the only way that a VM that
1345 ** opens a statement transaction may invoke this opcode.
1346 **
1347 ** In case this is such a statement, close any statement transaction
1348 ** opened by this VM before returning control to the user. This is to
1349 ** ensure that statement-transactions are always nested, not overlapping.
1350 ** If the open statement-transaction is not closed here, then the user
1351 ** may step another VM that opens its own statement transaction. This
1352 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001353 **
1354 ** The statement transaction is never a top-level transaction. Hence
1355 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001356 */
1357 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001358 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
drh9467abf2016-02-17 18:44:11 +00001359 assert( rc==SQLITE_OK );
danielk1977bd434552009-03-18 10:33:00 +00001360
drhd4e70eb2008-01-02 00:34:36 +00001361 /* Invalidate all ephemeral cursor row caches */
1362 p->cacheCtr = (p->cacheCtr + 2)|1;
1363
1364 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001365 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001366 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001367 */
drha6c2ed92009-11-14 23:22:23 +00001368 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001369 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001370 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001371 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001372 assert( (pMem[i].flags & MEM_Ephem)==0
1373 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001374 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001375 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001376 }
drh28039692008-03-17 16:54:01 +00001377 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001378
1379 /* Return SQLITE_ROW
1380 */
drhf56fa462015-04-13 21:39:54 +00001381 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001382 rc = SQLITE_ROW;
1383 goto vdbe_return;
1384}
1385
drh5b6afba2008-01-05 16:29:28 +00001386/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001387** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001388**
drh5b6afba2008-01-05 16:29:28 +00001389** Add the text in register P1 onto the end of the text in
1390** register P2 and store the result in register P3.
1391** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001392**
1393** P3 = P2 || P1
1394**
1395** It is illegal for P1 and P3 to be the same register. Sometimes,
1396** if P3 is the same register as P2, the implementation is able
1397** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001398*/
drh5b6afba2008-01-05 16:29:28 +00001399case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001400 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001401
drh3c657212009-11-17 23:59:58 +00001402 pIn1 = &aMem[pOp->p1];
1403 pIn2 = &aMem[pOp->p2];
1404 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001405 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001406 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001407 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001408 break;
drh5e00f6c2001-09-13 13:46:56 +00001409 }
drha0c06522009-06-17 22:50:41 +00001410 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001411 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001412 Stringify(pIn2, encoding);
1413 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001414 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001415 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001416 }
drh9c1905f2008-12-10 22:32:56 +00001417 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001418 goto no_mem;
1419 }
drhc91b2fd2014-03-01 18:13:23 +00001420 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001421 if( pOut!=pIn2 ){
1422 memcpy(pOut->z, pIn2->z, pIn2->n);
1423 }
1424 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001425 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001426 pOut->z[nByte+1] = 0;
1427 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001428 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001429 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001430 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001431 break;
1432}
drh75897232000-05-29 14:26:00 +00001433
drh3c84ddf2008-01-09 02:15:38 +00001434/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001435** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001436**
drh60a713c2008-01-21 16:22:45 +00001437** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001438** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001439** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001440*/
drh3c84ddf2008-01-09 02:15:38 +00001441/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001442** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001443**
drh3c84ddf2008-01-09 02:15:38 +00001444**
shane21e7feb2008-05-30 15:59:49 +00001445** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001446** and store the result in register P3.
1447** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001448*/
drh3c84ddf2008-01-09 02:15:38 +00001449/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001450** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001451**
drh60a713c2008-01-21 16:22:45 +00001452** Subtract the value in register P1 from the value in register P2
1453** and store the result in register P3.
1454** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001455*/
drh9cbf3422008-01-17 16:22:13 +00001456/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001457** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001458**
drh60a713c2008-01-21 16:22:45 +00001459** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001460** and store the result in register P3 (P3=P2/P1). If the value in
1461** register P1 is zero, then the result is NULL. If either input is
1462** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001463*/
drh9cbf3422008-01-17 16:22:13 +00001464/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001465** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001466**
drh40864a12013-11-15 18:58:37 +00001467** Compute the remainder after integer register P2 is divided by
1468** register P1 and store the result in register P3.
1469** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001470** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001471*/
drh5b6afba2008-01-05 16:29:28 +00001472case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1473case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1474case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1475case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1476case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001477 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001478 u16 flags; /* Combined MEM_* flags from both inputs */
1479 u16 type1; /* Numeric type of left operand */
1480 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001481 i64 iA; /* Integer value of left operand */
1482 i64 iB; /* Integer value of right operand */
1483 double rA; /* Real value of left operand */
1484 double rB; /* Real value of right operand */
1485
drh3c657212009-11-17 23:59:58 +00001486 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001487 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001488 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001489 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001490 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001491 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001492 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
drh3d1d90a2014-03-24 15:00:15 +00001493 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001494 iA = pIn1->u.i;
1495 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001496 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001497 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001498 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1499 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1500 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001501 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001502 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001503 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001504 iB /= iA;
drh75897232000-05-29 14:26:00 +00001505 break;
1506 }
drhbf4133c2001-10-13 02:59:08 +00001507 default: {
drh856c1032009-06-02 15:21:42 +00001508 if( iA==0 ) goto arithmetic_result_is_null;
1509 if( iA==-1 ) iA = 1;
1510 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001511 break;
1512 }
drh75897232000-05-29 14:26:00 +00001513 }
drh856c1032009-06-02 15:21:42 +00001514 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001515 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001516 }else{
drhbe707b32012-12-10 22:19:14 +00001517 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001518fp_math:
drh856c1032009-06-02 15:21:42 +00001519 rA = sqlite3VdbeRealValue(pIn1);
1520 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001521 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001522 case OP_Add: rB += rA; break;
1523 case OP_Subtract: rB -= rA; break;
1524 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001525 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001526 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001527 if( rA==(double)0 ) goto arithmetic_result_is_null;
1528 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001529 break;
1530 }
drhbf4133c2001-10-13 02:59:08 +00001531 default: {
shane75ac1de2009-06-09 18:58:52 +00001532 iA = (i64)rA;
1533 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001534 if( iA==0 ) goto arithmetic_result_is_null;
1535 if( iA==-1 ) iA = 1;
1536 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001537 break;
1538 }
drh5e00f6c2001-09-13 13:46:56 +00001539 }
drhc5a7b512010-01-13 16:25:42 +00001540#ifdef SQLITE_OMIT_FLOATING_POINT
1541 pOut->u.i = rB;
1542 MemSetTypeFlag(pOut, MEM_Int);
1543#else
drh856c1032009-06-02 15:21:42 +00001544 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001545 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001546 }
drh74eaba42014-09-18 17:52:15 +00001547 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001548 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001549 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001550 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001551 }
drhc5a7b512010-01-13 16:25:42 +00001552#endif
drh5e00f6c2001-09-13 13:46:56 +00001553 }
1554 break;
1555
drha05a7222008-01-19 03:35:58 +00001556arithmetic_result_is_null:
1557 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001558 break;
1559}
1560
drh7a957892012-02-02 17:35:43 +00001561/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001562**
drh66a51672008-01-03 00:01:23 +00001563** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001564** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1565** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001566** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001567**
drh7a957892012-02-02 17:35:43 +00001568** If P1 is not zero, then it is a register that a subsequent min() or
1569** max() aggregate will set to 1 if the current row is not the minimum or
1570** maximum. The P1 register is initialized to 0 by this instruction.
1571**
danielk1977dc1bdc42004-06-11 10:51:27 +00001572** The interface used by the implementation of the aforementioned functions
1573** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001574** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001575*/
drh9cbf3422008-01-17 16:22:13 +00001576case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001577 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001578 if( pOp->p1 ){
1579 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1580 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001581 break;
1582}
1583
drh9c7c9132015-06-26 18:16:52 +00001584/* Opcode: Function0 P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001585** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001586**
drhe2d9e7c2015-06-26 18:47:53 +00001587** Invoke a user function (P4 is a pointer to a FuncDef object that
drh98757152008-01-09 23:04:12 +00001588** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001589** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001590** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001591**
drh13449892005-09-07 21:22:45 +00001592** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001593** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001594** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001595** whether meta data associated with a user function argument using the
1596** sqlite3_set_auxdata() API may be safely retained until the next
1597** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001598**
drh9c7c9132015-06-26 18:16:52 +00001599** See also: Function, AggStep, AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001600*/
drh9c7c9132015-06-26 18:16:52 +00001601/* Opcode: Function P1 P2 P3 P4 P5
1602** Synopsis: r[P3]=func(r[P2@P5])
1603**
1604** Invoke a user function (P4 is a pointer to an sqlite3_context object that
1605** contains a pointer to the function to be run) with P5 arguments taken
1606** from register P2 and successors. The result of the function is stored
1607** in register P3. Register P3 must not be one of the function inputs.
1608**
1609** P1 is a 32-bit bitmask indicating whether or not each argument to the
1610** function was determined to be constant at compile time. If the first
1611** argument was constant then bit 0 of P1 is set. This is used to determine
1612** whether meta data associated with a user function argument using the
1613** sqlite3_set_auxdata() API may be safely retained until the next
1614** invocation of this opcode.
1615**
1616** SQL functions are initially coded as OP_Function0 with P4 pointing
drhe2d9e7c2015-06-26 18:47:53 +00001617** to a FuncDef object. But on first evaluation, the P4 operand is
drh9c7c9132015-06-26 18:16:52 +00001618** automatically converted into an sqlite3_context object and the operation
1619** changed to this OP_Function opcode. In this way, the initialization of
1620** the sqlite3_context object occurs only once, rather than once for each
1621** evaluation of the function.
1622**
1623** See also: Function0, AggStep, AggFinal
1624*/
1625case OP_Function0: {
drh856c1032009-06-02 15:21:42 +00001626 int n;
drh9c7c9132015-06-26 18:16:52 +00001627 sqlite3_context *pCtx;
danielk197751ad0ec2004-05-24 12:39:02 +00001628
dan0c547792013-07-18 17:12:08 +00001629 assert( pOp->p4type==P4_FUNCDEF );
drh9c7c9132015-06-26 18:16:52 +00001630 n = pOp->p5;
1631 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
1632 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
1633 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh575fad62016-02-05 13:38:36 +00001634 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
drh9c7c9132015-06-26 18:16:52 +00001635 if( pCtx==0 ) goto no_mem;
1636 pCtx->pOut = 0;
1637 pCtx->pFunc = pOp->p4.pFunc;
1638 pCtx->iOp = (int)(pOp - aOp);
1639 pCtx->pVdbe = p;
1640 pCtx->argc = n;
1641 pOp->p4type = P4_FUNCCTX;
1642 pOp->p4.pCtx = pCtx;
1643 pOp->opcode = OP_Function;
1644 /* Fall through into OP_Function */
1645}
1646case OP_Function: {
1647 int i;
1648 sqlite3_context *pCtx;
danielk1977a7a8e142008-02-13 18:25:27 +00001649
drh9c7c9132015-06-26 18:16:52 +00001650 assert( pOp->p4type==P4_FUNCCTX );
1651 pCtx = pOp->p4.pCtx;
danielk1977a7a8e142008-02-13 18:25:27 +00001652
drh9c7c9132015-06-26 18:16:52 +00001653 /* If this function is inside of a trigger, the register array in aMem[]
1654 ** might change from one evaluation to the next. The next block of code
1655 ** checks to see if the register array has changed, and if so it
1656 ** reinitializes the relavant parts of the sqlite3_context object */
drhe2d9e7c2015-06-26 18:47:53 +00001657 pOut = &aMem[pOp->p3];
1658 if( pCtx->pOut != pOut ){
1659 pCtx->pOut = pOut;
drh9c7c9132015-06-26 18:16:52 +00001660 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
danielk1977dc1bdc42004-06-11 10:51:27 +00001661 }
drh9c7c9132015-06-26 18:16:52 +00001662
1663 memAboutToChange(p, pCtx->pOut);
1664#ifdef SQLITE_DEBUG
1665 for(i=0; i<pCtx->argc; i++){
1666 assert( memIsValid(pCtx->argv[i]) );
1667 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
1668 }
1669#endif
1670 MemSetTypeFlag(pCtx->pOut, MEM_Null);
1671 pCtx->fErrorOrAux = 0;
drh99a66922011-05-13 18:51:42 +00001672 db->lastRowid = lastRowid;
drh2d801512016-01-14 22:19:58 +00001673 (*pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/* IMP: R-24505-23230 */
1674 lastRowid = db->lastRowid; /* Remember rowid changes made by xSFunc */
dan5f84e142011-06-14 14:18:45 +00001675
drh90669c12006-01-20 15:45:36 +00001676 /* If the function returned an error, throw an exception */
drh9c7c9132015-06-26 18:16:52 +00001677 if( pCtx->fErrorOrAux ){
1678 if( pCtx->isError ){
1679 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
1680 rc = pCtx->isError;
drh9b47ee32013-08-20 03:13:51 +00001681 }
drhe2d9e7c2015-06-26 18:47:53 +00001682 sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00001683 if( rc ) goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00001684 }
1685
drh9cbf3422008-01-17 16:22:13 +00001686 /* Copy the result of the function into register P3 */
drhe2d9e7c2015-06-26 18:47:53 +00001687 if( pOut->flags & (MEM_Str|MEM_Blob) ){
1688 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
1689 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
drh023ae032007-05-08 12:12:16 +00001690 }
drh7b94e7f2011-04-04 12:29:20 +00001691
drh9c7c9132015-06-26 18:16:52 +00001692 REGISTER_TRACE(pOp->p3, pCtx->pOut);
1693 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
drh8e0a2f92002-02-23 23:45:45 +00001694 break;
1695}
1696
drh98757152008-01-09 23:04:12 +00001697/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001698** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001699**
drh98757152008-01-09 23:04:12 +00001700** Take the bit-wise AND of the values in register P1 and P2 and
1701** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001702** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001703*/
drh98757152008-01-09 23:04:12 +00001704/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001705** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001706**
drh98757152008-01-09 23:04:12 +00001707** Take the bit-wise OR of the values in register P1 and P2 and
1708** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001709** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001710*/
drh98757152008-01-09 23:04:12 +00001711/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001712** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001713**
drh98757152008-01-09 23:04:12 +00001714** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001715** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001716** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001717** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001718*/
drh98757152008-01-09 23:04:12 +00001719/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001720** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001721**
drh98757152008-01-09 23:04:12 +00001722** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001723** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001724** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001725** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001726*/
drh5b6afba2008-01-05 16:29:28 +00001727case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1728case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1729case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1730case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001731 i64 iA;
1732 u64 uA;
1733 i64 iB;
1734 u8 op;
drh6810ce62004-01-31 19:22:56 +00001735
drh3c657212009-11-17 23:59:58 +00001736 pIn1 = &aMem[pOp->p1];
1737 pIn2 = &aMem[pOp->p2];
1738 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001739 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001740 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001741 break;
1742 }
drh158b9cb2011-03-05 20:59:46 +00001743 iA = sqlite3VdbeIntValue(pIn2);
1744 iB = sqlite3VdbeIntValue(pIn1);
1745 op = pOp->opcode;
1746 if( op==OP_BitAnd ){
1747 iA &= iB;
1748 }else if( op==OP_BitOr ){
1749 iA |= iB;
1750 }else if( iB!=0 ){
1751 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1752
1753 /* If shifting by a negative amount, shift in the other direction */
1754 if( iB<0 ){
1755 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1756 op = 2*OP_ShiftLeft + 1 - op;
1757 iB = iB>(-64) ? -iB : 64;
1758 }
1759
1760 if( iB>=64 ){
1761 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1762 }else{
1763 memcpy(&uA, &iA, sizeof(uA));
1764 if( op==OP_ShiftLeft ){
1765 uA <<= iB;
1766 }else{
1767 uA >>= iB;
1768 /* Sign-extend on a right shift of a negative number */
1769 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1770 }
1771 memcpy(&iA, &uA, sizeof(iA));
1772 }
drhbf4133c2001-10-13 02:59:08 +00001773 }
drh158b9cb2011-03-05 20:59:46 +00001774 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001775 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001776 break;
1777}
1778
drh8558cde2008-01-05 05:20:10 +00001779/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001780** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001781**
danielk19770cdc0222008-06-26 18:04:03 +00001782** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001783** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001784**
drh8558cde2008-01-05 05:20:10 +00001785** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001786*/
drh9cbf3422008-01-17 16:22:13 +00001787case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001788 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001789 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001790 sqlite3VdbeMemIntegerify(pIn1);
1791 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001792 break;
1793}
1794
drh9cbf3422008-01-17 16:22:13 +00001795/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001796**
drh9cbf3422008-01-17 16:22:13 +00001797** Force the value in register P1 to be an integer. If the value
1798** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001799** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001800** raise an SQLITE_MISMATCH exception.
1801*/
drh9cbf3422008-01-17 16:22:13 +00001802case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001803 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001804 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001805 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001806 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001807 if( (pIn1->flags & MEM_Int)==0 ){
1808 if( pOp->p2==0 ){
1809 rc = SQLITE_MISMATCH;
1810 goto abort_due_to_error;
1811 }else{
drhf56fa462015-04-13 21:39:54 +00001812 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001813 }
drh8aff1012001-12-22 14:49:24 +00001814 }
drh8aff1012001-12-22 14:49:24 +00001815 }
drh83b301b2013-11-20 00:59:02 +00001816 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001817 break;
1818}
1819
drh13573c72010-01-12 17:04:07 +00001820#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001821/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001822**
drh2133d822008-01-03 18:44:59 +00001823** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001824**
drh8a512562005-11-14 22:29:05 +00001825** This opcode is used when extracting information from a column that
1826** has REAL affinity. Such column values may still be stored as
1827** integers, for space efficiency, but after extraction we want them
1828** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001829*/
drh9cbf3422008-01-17 16:22:13 +00001830case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001831 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001832 if( pIn1->flags & MEM_Int ){
1833 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001834 }
drh487e2622005-06-25 18:42:14 +00001835 break;
1836}
drh13573c72010-01-12 17:04:07 +00001837#endif
drh487e2622005-06-25 18:42:14 +00001838
drh8df447f2005-11-01 15:48:24 +00001839#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001840/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001841** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001842**
drh4169e432014-08-25 20:11:52 +00001843** Force the value in register P1 to be the type defined by P2.
1844**
1845** <ul>
1846** <li value="97"> TEXT
1847** <li value="98"> BLOB
1848** <li value="99"> NUMERIC
1849** <li value="100"> INTEGER
1850** <li value="101"> REAL
1851** </ul>
drh487e2622005-06-25 18:42:14 +00001852**
1853** A NULL value is not changed by this routine. It remains NULL.
1854*/
drh4169e432014-08-25 20:11:52 +00001855case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001856 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001857 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001858 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001859 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1860 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1861 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001862 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001863 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001864 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001865 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001866 UPDATE_MAX_BLOBSIZE(pIn1);
drh9467abf2016-02-17 18:44:11 +00001867 if( rc ) goto abort_due_to_error;
drh487e2622005-06-25 18:42:14 +00001868 break;
1869}
drh8a512562005-11-14 22:29:05 +00001870#endif /* SQLITE_OMIT_CAST */
1871
drh35573352008-01-08 23:54:25 +00001872/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001873** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001874**
drh35573352008-01-08 23:54:25 +00001875** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1876** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001877**
drh35573352008-01-08 23:54:25 +00001878** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1879** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001880** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001881**
drh35573352008-01-08 23:54:25 +00001882** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001883** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001884** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001885** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001886** 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.
danielk1977a37cdde2004-05-16 11:15:36 +00001889**
1890** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001891** 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.
drhc9b84a12002-06-20 11:36:48 +00001899**
drh35573352008-01-08 23:54:25 +00001900** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1901** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001902**
1903** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1904** equal to one another, provided that they do not have their MEM_Cleared
1905** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001906*/
drh9cbf3422008-01-17 16:22:13 +00001907/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001908** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001909**
drh35573352008-01-08 23:54:25 +00001910** This works just like the Lt opcode except that the jump is taken if
1911** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001912** additional information.
drh6a2fe092009-09-23 02:29:36 +00001913**
1914** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1915** true or false and is never NULL. If both operands are NULL then the result
1916** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001917** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001918** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001919*/
drh9cbf3422008-01-17 16:22:13 +00001920/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001921** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001922**
drh35573352008-01-08 23:54:25 +00001923** This works just like the Lt opcode except that the jump is taken if
1924** the operands in registers P1 and P3 are equal.
1925** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001926**
1927** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1928** true or false and is never NULL. If both operands are NULL then the result
1929** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001930** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001931** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001932*/
drh9cbf3422008-01-17 16:22:13 +00001933/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001934** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001935**
drh35573352008-01-08 23:54:25 +00001936** This works just like the Lt opcode except that the jump is taken if
1937** the content of register P3 is less than or equal to the content of
1938** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001939*/
drh9cbf3422008-01-17 16:22:13 +00001940/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001941** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001942**
drh35573352008-01-08 23:54:25 +00001943** This works just like the Lt opcode except that the jump is taken if
1944** the content of register P3 is greater than the content of
1945** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001946*/
drh9cbf3422008-01-17 16:22:13 +00001947/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001948** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001949**
drh35573352008-01-08 23:54:25 +00001950** This works just like the Lt opcode except that the jump is taken if
1951** the content of register P3 is greater than or equal to the content of
1952** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001953*/
drh9cbf3422008-01-17 16:22:13 +00001954case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1955case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1956case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1957case OP_Le: /* same as TK_LE, jump, in1, in3 */
1958case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1959case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001960 int res; /* Result of the comparison of pIn1 against pIn3 */
1961 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001962 u16 flags1; /* Copy of initial value of pIn1->flags */
1963 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001964
drh3c657212009-11-17 23:59:58 +00001965 pIn1 = &aMem[pOp->p1];
1966 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001967 flags1 = pIn1->flags;
1968 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001969 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001970 /* One or both operands are NULL */
1971 if( pOp->p5 & SQLITE_NULLEQ ){
1972 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1973 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1974 ** or not both operands are null.
1975 */
1976 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001977 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001978 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drh053a1282012-09-19 21:15:46 +00001979 if( (flags1&MEM_Null)!=0
1980 && (flags3&MEM_Null)!=0
1981 && (flags3&MEM_Cleared)==0
1982 ){
1983 res = 0; /* Results are equal */
1984 }else{
1985 res = 1; /* Results are not equal */
1986 }
drh6a2fe092009-09-23 02:29:36 +00001987 }else{
1988 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1989 ** then the result is always NULL.
1990 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1991 */
drh688852a2014-02-17 22:40:43 +00001992 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001993 pOut = &aMem[pOp->p2];
danb1d6b532015-12-14 19:42:19 +00001994 memAboutToChange(p, pOut);
drh6a2fe092009-09-23 02:29:36 +00001995 MemSetTypeFlag(pOut, MEM_Null);
1996 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001997 }else{
drhf4345e42014-02-18 11:31:59 +00001998 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001999 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00002000 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002001 }
drh6a2fe092009-09-23 02:29:36 +00002002 }
2003 break;
danielk1977a37cdde2004-05-16 11:15:36 +00002004 }
drh6a2fe092009-09-23 02:29:36 +00002005 }else{
2006 /* Neither operand is NULL. Do a comparison. */
2007 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00002008 if( affinity>=SQLITE_AFF_NUMERIC ){
drhe5520e22015-12-31 04:34:26 +00002009 if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00002010 applyNumericAffinity(pIn1,0);
2011 }
drhe5520e22015-12-31 04:34:26 +00002012 if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00002013 applyNumericAffinity(pIn3,0);
2014 }
2015 }else if( affinity==SQLITE_AFF_TEXT ){
drhe5520e22015-12-31 04:34:26 +00002016 if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002017 testcase( pIn1->flags & MEM_Int );
2018 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002019 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002020 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2021 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002022 }
drhe5520e22015-12-31 04:34:26 +00002023 if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002024 testcase( pIn3->flags & MEM_Int );
2025 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002026 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002027 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2028 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002029 }
drh6a2fe092009-09-23 02:29:36 +00002030 }
drh6a2fe092009-09-23 02:29:36 +00002031 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drhe5520e22015-12-31 04:34:26 +00002032 if( flags1 & MEM_Zero ){
drhca5506b2014-09-17 23:37:38 +00002033 sqlite3VdbeMemExpandBlob(pIn1);
2034 flags1 &= ~MEM_Zero;
2035 }
drhe5520e22015-12-31 04:34:26 +00002036 if( flags3 & MEM_Zero ){
drhca5506b2014-09-17 23:37:38 +00002037 sqlite3VdbeMemExpandBlob(pIn3);
2038 flags3 &= ~MEM_Zero;
2039 }
drh6a2fe092009-09-23 02:29:36 +00002040 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002041 }
danielk1977a37cdde2004-05-16 11:15:36 +00002042 switch( pOp->opcode ){
2043 case OP_Eq: res = res==0; break;
2044 case OP_Ne: res = res!=0; break;
2045 case OP_Lt: res = res<0; break;
2046 case OP_Le: res = res<=0; break;
2047 case OP_Gt: res = res>0; break;
2048 default: res = res>=0; break;
2049 }
2050
drhf56fa462015-04-13 21:39:54 +00002051 /* Undo any changes made by applyAffinity() to the input registers. */
2052 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2053 pIn1->flags = flags1;
2054 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2055 pIn3->flags = flags3;
2056
drh35573352008-01-08 23:54:25 +00002057 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002058 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00002059 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002060 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00002061 pOut->u.i = res;
2062 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002063 }else{
drhf4345e42014-02-18 11:31:59 +00002064 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh688852a2014-02-17 22:40:43 +00002065 if( res ){
drhf56fa462015-04-13 21:39:54 +00002066 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002067 }
danielk1977a37cdde2004-05-16 11:15:36 +00002068 }
2069 break;
2070}
drhc9b84a12002-06-20 11:36:48 +00002071
drh0acb7e42008-06-25 00:12:41 +00002072/* Opcode: Permutation * * * P4 *
2073**
shanebe217792009-03-05 04:20:31 +00002074** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00002075** of integers in P4.
2076**
drh953f7612012-12-07 22:18:54 +00002077** The permutation is only valid until the next OP_Compare that has
2078** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2079** occur immediately prior to the OP_Compare.
drhb1702022016-01-30 00:45:18 +00002080**
2081** The first integer in the P4 integer array is the length of the array
2082** and does not become part of the permutation.
drh0acb7e42008-06-25 00:12:41 +00002083*/
2084case OP_Permutation: {
2085 assert( pOp->p4type==P4_INTARRAY );
2086 assert( pOp->p4.ai );
drhb1702022016-01-30 00:45:18 +00002087 aPermute = pOp->p4.ai + 1;
drh0acb7e42008-06-25 00:12:41 +00002088 break;
2089}
2090
drh953f7612012-12-07 22:18:54 +00002091/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002092** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002093**
drh710c4842010-08-30 01:17:20 +00002094** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2095** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002096** the comparison for use by the next OP_Jump instruct.
2097**
drh0ca10df2012-12-08 13:26:23 +00002098** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2099** determined by the most recent OP_Permutation operator. If the
2100** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2101** order.
2102**
drh0acb7e42008-06-25 00:12:41 +00002103** P4 is a KeyInfo structure that defines collating sequences and sort
2104** orders for the comparison. The permutation applies to registers
2105** only. The KeyInfo elements are used sequentially.
2106**
2107** The comparison is a sort comparison, so NULLs compare equal,
2108** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002109** and strings are less than blobs.
2110*/
2111case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002112 int n;
2113 int i;
2114 int p1;
2115 int p2;
2116 const KeyInfo *pKeyInfo;
2117 int idx;
2118 CollSeq *pColl; /* Collating sequence to use on this term */
2119 int bRev; /* True for DESCENDING sort order */
2120
drh953f7612012-12-07 22:18:54 +00002121 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002122 n = pOp->p3;
2123 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002124 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002125 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002126 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002127 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002128#if SQLITE_DEBUG
2129 if( aPermute ){
2130 int k, mx = 0;
2131 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002132 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2133 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002134 }else{
dan3bc9f742013-08-15 16:18:39 +00002135 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2136 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002137 }
2138#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002139 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002140 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002141 assert( memIsValid(&aMem[p1+idx]) );
2142 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002143 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2144 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002145 assert( i<pKeyInfo->nField );
2146 pColl = pKeyInfo->aColl[i];
2147 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002148 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002149 if( iCompare ){
2150 if( bRev ) iCompare = -iCompare;
2151 break;
2152 }
drh16ee60f2008-06-20 18:13:25 +00002153 }
drh0acb7e42008-06-25 00:12:41 +00002154 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002155 break;
2156}
2157
2158/* Opcode: Jump P1 P2 P3 * *
2159**
2160** Jump to the instruction at address P1, P2, or P3 depending on whether
2161** in the most recent OP_Compare instruction the P1 vector was less than
2162** equal to, or greater than the P2 vector, respectively.
2163*/
drh0acb7e42008-06-25 00:12:41 +00002164case OP_Jump: { /* jump */
2165 if( iCompare<0 ){
drhf56fa462015-04-13 21:39:54 +00002166 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002167 }else if( iCompare==0 ){
drhf56fa462015-04-13 21:39:54 +00002168 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002169 }else{
drhf56fa462015-04-13 21:39:54 +00002170 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002171 }
2172 break;
2173}
2174
drh5b6afba2008-01-05 16:29:28 +00002175/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002176** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002177**
drh5b6afba2008-01-05 16:29:28 +00002178** Take the logical AND of the values in registers P1 and P2 and
2179** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002180**
drh5b6afba2008-01-05 16:29:28 +00002181** If either P1 or P2 is 0 (false) then the result is 0 even if
2182** the other input is NULL. A NULL and true or two NULLs give
2183** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002184*/
drh5b6afba2008-01-05 16:29:28 +00002185/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002186** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002187**
2188** Take the logical OR of the values in register P1 and P2 and
2189** store the answer in register P3.
2190**
2191** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2192** even if the other input is NULL. A NULL and false or two NULLs
2193** give a NULL output.
2194*/
2195case OP_And: /* same as TK_AND, in1, in2, out3 */
2196case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002197 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2198 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002199
drh3c657212009-11-17 23:59:58 +00002200 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002201 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002202 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002203 }else{
drh5b6afba2008-01-05 16:29:28 +00002204 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002205 }
drh3c657212009-11-17 23:59:58 +00002206 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002207 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002208 v2 = 2;
2209 }else{
drh5b6afba2008-01-05 16:29:28 +00002210 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002211 }
2212 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002213 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002214 v1 = and_logic[v1*3+v2];
2215 }else{
drh5b6afba2008-01-05 16:29:28 +00002216 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002217 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002218 }
drh3c657212009-11-17 23:59:58 +00002219 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002220 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002221 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002222 }else{
drh5b6afba2008-01-05 16:29:28 +00002223 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002224 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002225 }
drh5e00f6c2001-09-13 13:46:56 +00002226 break;
2227}
2228
drhe99fa2a2008-12-15 15:27:51 +00002229/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002230** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002231**
drhe99fa2a2008-12-15 15:27:51 +00002232** Interpret the value in register P1 as a boolean value. Store the
2233** boolean complement in register P2. If the value in register P1 is
2234** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002235*/
drh93952eb2009-11-13 19:43:43 +00002236case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002237 pIn1 = &aMem[pOp->p1];
2238 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002239 sqlite3VdbeMemSetNull(pOut);
2240 if( (pIn1->flags & MEM_Null)==0 ){
2241 pOut->flags = MEM_Int;
2242 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002243 }
drh5e00f6c2001-09-13 13:46:56 +00002244 break;
2245}
2246
drhe99fa2a2008-12-15 15:27:51 +00002247/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002248** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002249**
drhe99fa2a2008-12-15 15:27:51 +00002250** Interpret the content of register P1 as an integer. Store the
2251** ones-complement of the P1 value into register P2. If P1 holds
2252** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002253*/
drh93952eb2009-11-13 19:43:43 +00002254case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002255 pIn1 = &aMem[pOp->p1];
2256 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002257 sqlite3VdbeMemSetNull(pOut);
2258 if( (pIn1->flags & MEM_Null)==0 ){
2259 pOut->flags = MEM_Int;
2260 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002261 }
drhbf4133c2001-10-13 02:59:08 +00002262 break;
2263}
2264
drh48f2d3b2011-09-16 01:34:43 +00002265/* Opcode: Once P1 P2 * * *
2266**
drh5dad9a32014-07-25 18:37:42 +00002267** Check the "once" flag number P1. If it is set, jump to instruction P2.
2268** Otherwise, set the flag and fall through to the next instruction.
2269** In other words, this opcode causes all following opcodes up through P2
2270** (but not including P2) to run just once and to be skipped on subsequent
2271** times through the loop.
2272**
2273** All "once" flags are initially cleared whenever a prepared statement
2274** first begins to run.
drh48f2d3b2011-09-16 01:34:43 +00002275*/
dan1d8cb212011-12-09 13:24:16 +00002276case OP_Once: { /* jump */
2277 assert( pOp->p1<p->nOnceFlag );
drh688852a2014-02-17 22:40:43 +00002278 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
dan1d8cb212011-12-09 13:24:16 +00002279 if( p->aOnceFlag[pOp->p1] ){
drhf56fa462015-04-13 21:39:54 +00002280 goto jump_to_p2;
dan1d8cb212011-12-09 13:24:16 +00002281 }else{
2282 p->aOnceFlag[pOp->p1] = 1;
2283 }
2284 break;
2285}
2286
drh3c84ddf2008-01-09 02:15:38 +00002287/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002288**
drhef8662b2011-06-20 21:47:58 +00002289** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002290** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002291** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002292*/
drh3c84ddf2008-01-09 02:15:38 +00002293/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002294**
drhef8662b2011-06-20 21:47:58 +00002295** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002296** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002297** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002298*/
drh9cbf3422008-01-17 16:22:13 +00002299case OP_If: /* jump, in1 */
2300case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002301 int c;
drh3c657212009-11-17 23:59:58 +00002302 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002303 if( pIn1->flags & MEM_Null ){
2304 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002305 }else{
drhba0232a2005-06-06 17:27:19 +00002306#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002307 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002308#else
drh3c84ddf2008-01-09 02:15:38 +00002309 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002310#endif
drhf5905aa2002-05-26 20:54:33 +00002311 if( pOp->opcode==OP_IfNot ) c = !c;
2312 }
drh688852a2014-02-17 22:40:43 +00002313 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002314 if( c ){
drhf56fa462015-04-13 21:39:54 +00002315 goto jump_to_p2;
drh3c84ddf2008-01-09 02:15:38 +00002316 }
drh5e00f6c2001-09-13 13:46:56 +00002317 break;
2318}
2319
drh830ecf92009-06-18 00:41:55 +00002320/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002321** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002322**
drh830ecf92009-06-18 00:41:55 +00002323** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002324*/
drh9cbf3422008-01-17 16:22:13 +00002325case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002326 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002327 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002328 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002329 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002330 }
drh477df4b2008-01-05 18:48:24 +00002331 break;
2332}
2333
drh98757152008-01-09 23:04:12 +00002334/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002335** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002336**
drh6a288a32008-01-07 19:20:24 +00002337** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002338*/
drh9cbf3422008-01-17 16:22:13 +00002339case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002340 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002341 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002342 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002343 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002344 }
drh5e00f6c2001-09-13 13:46:56 +00002345 break;
2346}
2347
drh3e9ca092009-09-08 01:14:48 +00002348/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002349** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002350**
danielk1977cfcdaef2004-05-12 07:33:33 +00002351** Interpret the data that cursor P1 points to as a structure built using
2352** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002353** information about the format of the data.) Extract the P2-th column
2354** from this record. If there are less that (P2+1)
2355** values in the record, extract a NULL.
2356**
drh9cbf3422008-01-17 16:22:13 +00002357** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002358**
danielk19771f4aa332008-01-03 09:51:55 +00002359** If the column contains fewer than P2 fields, then extract a NULL. Or,
2360** if the P4 argument is a P4_MEM use the value of the P4 argument as
2361** the result.
drh3e9ca092009-09-08 01:14:48 +00002362**
2363** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2364** then the cache of the cursor is reset prior to extracting the column.
2365** The first OP_Column against a pseudo-table after the value of the content
2366** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002367**
drhdda5c082012-03-28 13:41:10 +00002368** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2369** the result is guaranteed to only be used as the argument of a length()
2370** or typeof() function, respectively. The loading of large blobs can be
2371** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002372*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002373case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002374 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002375 int p2; /* column number to retrieve */
2376 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002377 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002378 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002379 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002380 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002381 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002382 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002383 const u8 *zData; /* Part of the record being decoded */
2384 const u8 *zHdr; /* Next unparsed byte of the header */
2385 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002386 u32 offset; /* Offset into the data */
drhc6ce38832015-10-15 21:30:24 +00002387 u64 offset64; /* 64-bit offset */
drh501932c2013-11-21 21:59:53 +00002388 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002389 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002390 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002391
dande892d92016-01-29 19:29:45 +00002392 pC = p->apCsr[pOp->p1];
drh399af1d2013-11-20 17:25:55 +00002393 p2 = pOp->p2;
dande892d92016-01-29 19:29:45 +00002394
2395 /* If the cursor cache is stale, bring it up-to-date */
2396 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
2397
dan3bc9f742013-08-15 16:18:39 +00002398 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002399 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002400 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002401 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drha5759672012-10-30 14:39:12 +00002402 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002403 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002404 aOffset = pC->aOffset;
drh62aaa6c2015-11-21 17:27:42 +00002405 assert( pC->eCurType!=CURTYPE_VTAB );
drhc960dcb2015-11-20 19:22:01 +00002406 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
2407 assert( pC->eCurType!=CURTYPE_SORTER );
2408 pCrsr = pC->uc.pCursor;
drh399af1d2013-11-20 17:25:55 +00002409
drh399af1d2013-11-20 17:25:55 +00002410 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00002411 if( pC->cacheStatus!=p->cacheCtr ){
drhc8606e42013-11-20 19:28:03 +00002412 if( pC->nullRow ){
drhc960dcb2015-11-20 19:22:01 +00002413 if( pC->eCurType==CURTYPE_PSEUDO ){
2414 assert( pC->uc.pseudoTableReg>0 );
2415 pReg = &aMem[pC->uc.pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002416 assert( pReg->flags & MEM_Blob );
2417 assert( memIsValid(pReg) );
2418 pC->payloadSize = pC->szRow = avail = pReg->n;
2419 pC->aRow = (u8*)pReg->z;
2420 }else{
drh6b5631e2014-11-05 15:57:39 +00002421 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002422 goto op_column_out;
2423 }
danielk197784ac9d02004-05-18 09:58:06 +00002424 }else{
drhc960dcb2015-11-20 19:22:01 +00002425 assert( pC->eCurType==CURTYPE_BTREE );
drhc8606e42013-11-20 19:28:03 +00002426 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002427 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002428 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2429 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2430 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2431 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2432 ** payload size, so it is impossible for payloadSize64 to be
2433 ** larger than 32 bits. */
2434 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2435 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2436 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002437 }else{
drh399af1d2013-11-20 17:25:55 +00002438 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2439 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2440 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2441 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002442 }
drh399af1d2013-11-20 17:25:55 +00002443 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2444 if( pC->payloadSize <= (u32)avail ){
2445 pC->szRow = pC->payloadSize;
drh5f7dacb2015-11-20 13:33:56 +00002446 }else if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2447 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002448 }else{
drh399af1d2013-11-20 17:25:55 +00002449 pC->szRow = avail;
2450 }
drhd3194f52004-05-27 19:59:32 +00002451 }
drh399af1d2013-11-20 17:25:55 +00002452 pC->cacheStatus = p->cacheCtr;
2453 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2454 pC->nHdrParsed = 0;
2455 aOffset[0] = offset;
drh35cd6432009-06-05 14:17:21 +00002456
drhc81aa2e2014-10-11 23:31:52 +00002457
2458 if( avail<offset ){
2459 /* pC->aRow does not have to hold the entire row, but it does at least
2460 ** need to cover the header of the record. If pC->aRow does not contain
2461 ** the complete header, then set it to zero, forcing the header to be
2462 ** dynamically allocated. */
2463 pC->aRow = 0;
2464 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002465
2466 /* Make sure a corrupt database has not given us an oversize header.
2467 ** Do this now to avoid an oversize memory allocation.
2468 **
2469 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2470 ** types use so much data space that there can only be 4096 and 32 of
2471 ** them, respectively. So the maximum header length results from a
2472 ** 3-byte type for each of the maximum of 32768 columns plus three
2473 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2474 */
2475 if( offset > 98307 || offset > pC->payloadSize ){
2476 rc = SQLITE_CORRUPT_BKPT;
drh9467abf2016-02-17 18:44:11 +00002477 goto abort_due_to_error;
drh848a3322015-10-16 12:53:47 +00002478 }
drhc81aa2e2014-10-11 23:31:52 +00002479 }
2480
2481 /* The following goto is an optimization. It can be omitted and
2482 ** everything will still work. But OP_Column is measurably faster
2483 ** by skipping the subsequent conditional, which is always true.
2484 */
2485 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2486 goto op_column_read_header;
drh399af1d2013-11-20 17:25:55 +00002487 }
drh35cd6432009-06-05 14:17:21 +00002488
drh399af1d2013-11-20 17:25:55 +00002489 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002490 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002491 */
drhc8606e42013-11-20 19:28:03 +00002492 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002493 /* If there is more header available for parsing in the record, try
2494 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002495 */
drhc81aa2e2014-10-11 23:31:52 +00002496 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002497 if( pC->iHdrOffset<aOffset[0] ){
2498 /* Make sure zData points to enough of the record to cover the header. */
2499 if( pC->aRow==0 ){
2500 memset(&sMem, 0, sizeof(sMem));
drh95fa6062015-10-16 13:50:08 +00002501 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem);
drh9467abf2016-02-17 18:44:11 +00002502 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002503 zData = (u8*)sMem.z;
2504 }else{
2505 zData = pC->aRow;
2506 }
2507
drh0c8f7602014-09-19 16:56:45 +00002508 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drhc8606e42013-11-20 19:28:03 +00002509 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002510 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002511 zHdr = zData + pC->iHdrOffset;
2512 zEndHdr = zData + aOffset[0];
2513 assert( i<=p2 && zHdr<zEndHdr );
2514 do{
drh95fa6062015-10-16 13:50:08 +00002515 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002516 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002517 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drhc8606e42013-11-20 19:28:03 +00002518 }else{
2519 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002520 offset64 += sqlite3VdbeSerialTypeLen(t);
drhc8606e42013-11-20 19:28:03 +00002521 }
drhfaf37272015-10-16 14:23:42 +00002522 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002523 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002524 }while( i<=p2 && zHdr<zEndHdr );
2525 pC->nHdrParsed = i;
2526 pC->iHdrOffset = (u32)(zHdr - zData);
drh95fa6062015-10-16 13:50:08 +00002527 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drhc8606e42013-11-20 19:28:03 +00002528
drh8dd83622014-10-13 23:39:02 +00002529 /* The record is corrupt if any of the following are true:
2530 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002531 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002532 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002533 */
drhc6ce38832015-10-15 21:30:24 +00002534 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2535 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002536 ){
2537 rc = SQLITE_CORRUPT_BKPT;
drh9467abf2016-02-17 18:44:11 +00002538 goto abort_due_to_error;
drhc8606e42013-11-20 19:28:03 +00002539 }
mistachkin8c7cd6a2015-12-16 21:09:53 +00002540 }else{
drh9fbc8852016-01-04 03:48:46 +00002541 t = 0;
drhc8606e42013-11-20 19:28:03 +00002542 }
2543
drhf2db3382015-04-30 20:33:25 +00002544 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002545 ** still not up to p2, that means that the record has fewer than p2
2546 ** columns. So the result will be either the default value or a NULL.
2547 */
drhc8606e42013-11-20 19:28:03 +00002548 if( pC->nHdrParsed<=p2 ){
2549 if( pOp->p4type==P4_MEM ){
2550 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2551 }else{
drh22e8d832014-10-29 00:58:38 +00002552 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002553 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002554 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002555 }
drh95fa6062015-10-16 13:50:08 +00002556 }else{
2557 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002558 }
danielk1977192ac1d2004-05-10 07:17:30 +00002559
drh380d6852013-11-20 20:58:00 +00002560 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002561 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002562 ** all valid.
drh9188b382004-05-14 21:12:22 +00002563 */
drhc8606e42013-11-20 19:28:03 +00002564 assert( p2<pC->nHdrParsed );
2565 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002566 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drh0725cab2014-09-17 14:52:46 +00002567 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
drh95fa6062015-10-16 13:50:08 +00002568 assert( t==pC->aType[p2] );
drh69f6e252016-01-11 18:05:00 +00002569 pDest->enc = encoding;
drhc8606e42013-11-20 19:28:03 +00002570 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002571 /* This is the common case where the desired content fits on the original
2572 ** page - where the content is not on an overflow page */
drh69f6e252016-01-11 18:05:00 +00002573 zData = pC->aRow + aOffset[p2];
2574 if( t<12 ){
2575 sqlite3VdbeSerialGet(zData, t, pDest);
2576 }else{
2577 /* If the column value is a string, we need a persistent value, not
2578 ** a MEM_Ephem value. This branch is a fast short-cut that is equivalent
2579 ** to calling sqlite3VdbeSerialGet() and sqlite3VdbeDeephemeralize().
2580 */
2581 static const u16 aFlag[] = { MEM_Blob, MEM_Str|MEM_Term };
2582 pDest->n = len = (t-12)/2;
2583 if( pDest->szMalloc < len+2 ){
2584 pDest->flags = MEM_Null;
2585 if( sqlite3VdbeMemGrow(pDest, len+2, 0) ) goto no_mem;
2586 }else{
2587 pDest->z = pDest->zMalloc;
2588 }
2589 memcpy(pDest->z, zData, len);
2590 pDest->z[len] = 0;
2591 pDest->z[len+1] = 0;
2592 pDest->flags = aFlag[t&1];
2593 }
danielk197736963fd2005-02-19 08:18:05 +00002594 }else{
drh58c96082013-12-23 11:33:32 +00002595 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002596 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2597 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2598 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002599 ){
drh2a2a6962014-09-16 18:22:44 +00002600 /* Content is irrelevant for
2601 ** 1. the typeof() function,
2602 ** 2. the length(X) function if X is a blob, and
2603 ** 3. if the content length is zero.
2604 ** So we might as well use bogus content rather than reading
drh69f6e252016-01-11 18:05:00 +00002605 ** content from disk. */
2606 static u8 aZero[8]; /* This is the bogus content */
2607 sqlite3VdbeSerialGet(aZero, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002608 }else{
drh14da87f2013-11-20 21:51:33 +00002609 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drh2a2a6962014-09-16 18:22:44 +00002610 pDest);
drh9467abf2016-02-17 18:44:11 +00002611 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2612 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2613 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002614 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002615 }
drhd3194f52004-05-27 19:59:32 +00002616
danielk19773c9cc8d2005-01-17 03:40:08 +00002617op_column_out:
drhb7654112008-01-12 12:48:07 +00002618 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002619 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002620 break;
2621}
2622
danielk1977751de562008-04-18 09:01:15 +00002623/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002624** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002625**
2626** Apply affinities to a range of P2 registers starting with P1.
2627**
2628** P4 is a string that is P2 characters long. The nth character of the
2629** string indicates the column affinity that should be used for the nth
2630** memory cell in the range.
2631*/
2632case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002633 const char *zAffinity; /* The affinity to be applied */
2634 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002635
drh856c1032009-06-02 15:21:42 +00002636 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002637 assert( zAffinity!=0 );
2638 assert( zAffinity[pOp->p2]==0 );
2639 pIn1 = &aMem[pOp->p1];
2640 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002641 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002642 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002643 applyAffinity(pIn1, cAff, encoding);
2644 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002645 }
2646 break;
2647}
2648
drh1db639c2008-01-17 02:36:28 +00002649/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002650** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002651**
drh710c4842010-08-30 01:17:20 +00002652** Convert P2 registers beginning with P1 into the [record format]
2653** use as a data record in a database table or as a key
2654** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002655**
danielk1977751de562008-04-18 09:01:15 +00002656** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002657** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002658** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002659**
drh8a512562005-11-14 22:29:05 +00002660** The mapping from character to affinity is given by the SQLITE_AFF_
2661** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002662**
drh05883a32015-06-02 15:32:08 +00002663** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002664*/
drh1db639c2008-01-17 02:36:28 +00002665case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002666 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2667 Mem *pRec; /* The new record */
2668 u64 nData; /* Number of bytes of data space */
2669 int nHdr; /* Number of bytes of header space */
2670 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002671 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002672 int nVarint; /* Number of bytes in a varint */
2673 u32 serial_type; /* Type field */
2674 Mem *pData0; /* First field to be combined into the record */
2675 Mem *pLast; /* Last field of the record */
2676 int nField; /* Number of fields in the record */
2677 char *zAffinity; /* The affinity string for the record */
2678 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002679 int i; /* Space used in zNewRecord[] header */
2680 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002681 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002682
drhf3218fe2004-05-28 08:21:02 +00002683 /* Assuming the record contains N fields, the record format looks
2684 ** like this:
2685 **
drh7a224de2004-06-02 01:22:02 +00002686 ** ------------------------------------------------------------------------
2687 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2688 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002689 **
drh9cbf3422008-01-17 16:22:13 +00002690 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002691 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002692 **
2693 ** Each type field is a varint representing the serial type of the
2694 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002695 ** hdr-size field is also a varint which is the offset from the beginning
2696 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002697 */
drh856c1032009-06-02 15:21:42 +00002698 nData = 0; /* Number of bytes of data space */
2699 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002700 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002701 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002702 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002703 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002704 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002705 nField = pOp->p2;
2706 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002707 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002708
drh2b4ded92010-09-27 21:09:31 +00002709 /* Identify the output register */
2710 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2711 pOut = &aMem[pOp->p3];
2712 memAboutToChange(p, pOut);
2713
drh3e6c0602013-12-10 20:53:01 +00002714 /* Apply the requested affinity to all inputs
2715 */
2716 assert( pData0<=pLast );
2717 if( zAffinity ){
2718 pRec = pData0;
2719 do{
drh57bf4a82014-02-17 14:59:22 +00002720 applyAffinity(pRec++, *(zAffinity++), encoding);
2721 assert( zAffinity[0]==0 || pRec<=pLast );
2722 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002723 }
2724
drhf3218fe2004-05-28 08:21:02 +00002725 /* Loop through the elements that will make up the record to figure
2726 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002727 */
drh038b7bc2013-12-09 23:17:22 +00002728 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002729 do{
drh2b4ded92010-09-27 21:09:31 +00002730 assert( memIsValid(pRec) );
drhbe37c122015-10-16 14:54:17 +00002731 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002732 if( pRec->flags & MEM_Zero ){
drh038b7bc2013-12-09 23:17:22 +00002733 if( nData ){
drh53e66c32015-07-24 15:49:23 +00002734 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002735 }else{
2736 nZero += pRec->u.nZero;
2737 len -= pRec->u.nZero;
2738 }
drhfdf972a2007-05-02 13:30:27 +00002739 }
drh8079a0d2006-01-12 17:20:50 +00002740 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002741 testcase( serial_type==127 );
2742 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002743 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002744 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002745
drh654858d2014-11-20 02:18:14 +00002746 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2747 ** which determines the total number of bytes in the header. The varint
2748 ** value is the size of the header in bytes including the size varint
2749 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002750 testcase( nHdr==126 );
2751 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002752 if( nHdr<=126 ){
2753 /* The common case */
2754 nHdr += 1;
2755 }else{
2756 /* Rare case of a really large header */
2757 nVarint = sqlite3VarintLen(nHdr);
2758 nHdr += nVarint;
2759 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002760 }
drh038b7bc2013-12-09 23:17:22 +00002761 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002762 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002763 goto too_big;
2764 }
drhf3218fe2004-05-28 08:21:02 +00002765
danielk1977a7a8e142008-02-13 18:25:27 +00002766 /* Make sure the output register has a buffer large enough to store
2767 ** the new record. The output register (pOp->p3) is not allowed to
2768 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002769 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002770 */
drh322f2852014-09-19 00:43:39 +00002771 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002772 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002773 }
danielk1977a7a8e142008-02-13 18:25:27 +00002774 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002775
2776 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002777 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002778 j = nHdr;
2779 assert( pData0<=pLast );
2780 pRec = pData0;
2781 do{
drhfacf47a2014-10-13 20:12:47 +00002782 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002783 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2784 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002785 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002786 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2787 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002788 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002789 }while( (++pRec)<=pLast );
2790 assert( i==nHdr );
2791 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002792
dan3bc9f742013-08-15 16:18:39 +00002793 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002794 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002795 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002796 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002797 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002798 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002799 }
drh477df4b2008-01-05 18:48:24 +00002800 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002801 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002802 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002803 break;
2804}
2805
danielk1977a5533162009-02-24 10:01:51 +00002806/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002807** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002808**
2809** Store the number of entries (an integer value) in the table or index
2810** opened by cursor P1 in register P2
2811*/
2812#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002813case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002814 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002815 BtCursor *pCrsr;
2816
drhc960dcb2015-11-20 19:22:01 +00002817 assert( p->apCsr[pOp->p1]->eCurType==CURTYPE_BTREE );
2818 pCrsr = p->apCsr[pOp->p1]->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00002819 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002820 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002821 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh9467abf2016-02-17 18:44:11 +00002822 if( rc ) goto abort_due_to_error;
drh27a348c2015-04-13 19:14:06 +00002823 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002824 pOut->u.i = nEntry;
2825 break;
2826}
2827#endif
2828
danielk1977fd7f0452008-12-17 17:30:26 +00002829/* Opcode: Savepoint P1 * * P4 *
2830**
2831** Open, release or rollback the savepoint named by parameter P4, depending
2832** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2833** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2834*/
2835case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002836 int p1; /* Value of P1 operand */
2837 char *zName; /* Name of savepoint */
2838 int nName;
2839 Savepoint *pNew;
2840 Savepoint *pSavepoint;
2841 Savepoint *pTmp;
2842 int iSavepoint;
2843 int ii;
2844
2845 p1 = pOp->p1;
2846 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002847
2848 /* Assert that the p1 parameter is valid. Also that if there is no open
2849 ** transaction, then there cannot be any savepoints.
2850 */
2851 assert( db->pSavepoint==0 || db->autoCommit==0 );
2852 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2853 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2854 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002855 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002856
2857 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002858 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002859 /* A new savepoint cannot be created if there are active write
2860 ** statements (i.e. open read/write incremental blob handles).
2861 */
drh22c17b82015-05-15 04:13:15 +00002862 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002863 rc = SQLITE_BUSY;
2864 }else{
drh856c1032009-06-02 15:21:42 +00002865 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002866
drhbe07ec52011-06-03 12:15:26 +00002867#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002868 /* This call is Ok even if this savepoint is actually a transaction
2869 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2870 ** If this is a transaction savepoint being opened, it is guaranteed
2871 ** that the db->aVTrans[] array is empty. */
2872 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002873 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2874 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002875 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002876#endif
dand9495cd2011-04-27 12:08:04 +00002877
danielk1977fd7f0452008-12-17 17:30:26 +00002878 /* Create a new savepoint structure. */
drh575fad62016-02-05 13:38:36 +00002879 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
danielk1977fd7f0452008-12-17 17:30:26 +00002880 if( pNew ){
2881 pNew->zName = (char *)&pNew[1];
2882 memcpy(pNew->zName, zName, nName+1);
2883
2884 /* If there is no open transaction, then mark this as a special
2885 ** "transaction savepoint". */
2886 if( db->autoCommit ){
2887 db->autoCommit = 0;
2888 db->isTransactionSavepoint = 1;
2889 }else{
2890 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002891 }
danielk1977fd7f0452008-12-17 17:30:26 +00002892
2893 /* Link the new savepoint into the database handle's list. */
2894 pNew->pNext = db->pSavepoint;
2895 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002896 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002897 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002898 }
2899 }
2900 }else{
drh856c1032009-06-02 15:21:42 +00002901 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002902
2903 /* Find the named savepoint. If there is no such savepoint, then an
2904 ** an error is returned to the user. */
2905 for(
drh856c1032009-06-02 15:21:42 +00002906 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002907 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002908 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002909 ){
2910 iSavepoint++;
2911 }
2912 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00002913 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002914 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002915 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002916 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002917 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002918 */
drh22c17b82015-05-15 04:13:15 +00002919 sqlite3VdbeError(p, "cannot release savepoint - "
2920 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002921 rc = SQLITE_BUSY;
2922 }else{
2923
2924 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002925 ** and this is a RELEASE command, then the current transaction
2926 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002927 */
2928 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2929 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002930 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002931 goto vdbe_return;
2932 }
danielk1977fd7f0452008-12-17 17:30:26 +00002933 db->autoCommit = 1;
2934 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00002935 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00002936 db->autoCommit = 0;
2937 p->rc = rc = SQLITE_BUSY;
2938 goto vdbe_return;
2939 }
danielk197734cf35d2008-12-18 18:31:38 +00002940 db->isTransactionSavepoint = 0;
2941 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002942 }else{
drh47b7fc72014-11-11 01:33:57 +00002943 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00002944 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002945 if( p1==SAVEPOINT_ROLLBACK ){
drh47b7fc72014-11-11 01:33:57 +00002946 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
drh31f10052012-03-31 17:17:26 +00002947 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00002948 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
2949 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00002950 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00002951 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00002952 }
drh47b7fc72014-11-11 01:33:57 +00002953 }else{
2954 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00002955 }
2956 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002957 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2958 if( rc!=SQLITE_OK ){
2959 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002960 }
danielk1977fd7f0452008-12-17 17:30:26 +00002961 }
drh47b7fc72014-11-11 01:33:57 +00002962 if( isSchemaChange ){
danielk1977fd7f0452008-12-17 17:30:26 +00002963 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002964 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002965 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002966 }
2967 }
2968
2969 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2970 ** savepoints nested inside of the savepoint being operated on. */
2971 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002972 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002973 db->pSavepoint = pTmp->pNext;
2974 sqlite3DbFree(db, pTmp);
2975 db->nSavepoint--;
2976 }
2977
dan1da40a32009-09-19 17:00:31 +00002978 /* If it is a RELEASE, then destroy the savepoint being operated on
2979 ** too. If it is a ROLLBACK TO, then set the number of deferred
2980 ** constraint violations present in the database to the value stored
2981 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002982 if( p1==SAVEPOINT_RELEASE ){
2983 assert( pSavepoint==db->pSavepoint );
2984 db->pSavepoint = pSavepoint->pNext;
2985 sqlite3DbFree(db, pSavepoint);
2986 if( !isTransaction ){
2987 db->nSavepoint--;
2988 }
dan1da40a32009-09-19 17:00:31 +00002989 }else{
2990 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002991 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002992 }
dand9495cd2011-04-27 12:08:04 +00002993
danea8562e2015-04-18 16:25:54 +00002994 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00002995 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2996 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2997 }
danielk1977fd7f0452008-12-17 17:30:26 +00002998 }
2999 }
drh9467abf2016-02-17 18:44:11 +00003000 if( rc ) goto abort_due_to_error;
danielk1977fd7f0452008-12-17 17:30:26 +00003001
3002 break;
3003}
3004
drh98757152008-01-09 23:04:12 +00003005/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00003006**
3007** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00003008** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00003009** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
3010** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00003011**
3012** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00003013*/
drh9cbf3422008-01-17 16:22:13 +00003014case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00003015 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00003016 int iRollback;
danielk19771d850a72004-05-31 08:26:49 +00003017
drh856c1032009-06-02 15:21:42 +00003018 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00003019 iRollback = pOp->p2;
drhad4a4b82008-11-05 16:37:34 +00003020 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003021 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003022 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003023 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003024
drhb0c88652016-02-01 13:21:13 +00003025 if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003026 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003027 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003028 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003029 db->autoCommit = 1;
drhb0c88652016-02-01 13:21:13 +00003030 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
3031 /* If this instruction implements a COMMIT and other VMs are writing
3032 ** return an error indicating that the other VMs must complete first.
3033 */
3034 sqlite3VdbeError(p, "cannot commit transaction - "
3035 "SQL statements in progress");
3036 rc = SQLITE_BUSY;
drh9467abf2016-02-17 18:44:11 +00003037 goto abort_due_to_error;
dan32b09f22009-09-23 17:29:59 +00003038 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003039 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003040 }else{
shane7d3846a2008-12-11 02:58:26 +00003041 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003042 }
3043 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3044 p->pc = (int)(pOp - aOp);
3045 db->autoCommit = (u8)(1-desiredAutoCommit);
3046 p->rc = rc = SQLITE_BUSY;
3047 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003048 }
danielk1977bd434552009-03-18 10:33:00 +00003049 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003050 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003051 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003052 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003053 }else{
drh900b31e2007-08-28 02:27:51 +00003054 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003055 }
drh900b31e2007-08-28 02:27:51 +00003056 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003057 }else{
drh22c17b82015-05-15 04:13:15 +00003058 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003059 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003060 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003061 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003062
3063 rc = SQLITE_ERROR;
drh9467abf2016-02-17 18:44:11 +00003064 goto abort_due_to_error;
drh663fc632002-02-02 18:49:19 +00003065 }
3066 break;
3067}
3068
drhb22f7c82014-02-06 23:56:27 +00003069/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003070**
drh05a86c52014-02-16 01:55:49 +00003071** Begin a transaction on database P1 if a transaction is not already
3072** active.
3073** If P2 is non-zero, then a write-transaction is started, or if a
3074** read-transaction is already active, it is upgraded to a write-transaction.
3075** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003076**
drh001bbcb2003-03-19 03:14:00 +00003077** P1 is the index of the database file on which the transaction is
3078** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003079** file used for temporary tables. Indices of 2 or more are used for
3080** attached databases.
drhcabb0812002-09-14 13:47:32 +00003081**
dane0af83a2009-09-08 19:15:01 +00003082** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3083** true (this flag is set if the Vdbe may modify more than one row and may
3084** throw an ABORT exception), a statement transaction may also be opened.
3085** More specifically, a statement transaction is opened iff the database
3086** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003087** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003088** VDBE to be rolled back after an error without having to roll back the
3089** entire transaction. If no error is encountered, the statement transaction
3090** will automatically commit when the VDBE halts.
3091**
drhb22f7c82014-02-06 23:56:27 +00003092** If P5!=0 then this opcode also checks the schema cookie against P3
3093** and the schema generation counter against P4.
3094** The cookie changes its value whenever the database schema changes.
3095** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003096** and that the current process needs to reread the schema. If the schema
3097** cookie in P3 differs from the schema cookie in the database header or
3098** if the schema generation counter in P4 differs from the current
3099** generation counter, then an SQLITE_SCHEMA error is raised and execution
3100** halts. The sqlite3_step() wrapper function might then reprepare the
3101** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003102*/
drh9cbf3422008-01-17 16:22:13 +00003103case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003104 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00003105 int iMeta;
3106 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00003107
drh1713afb2013-06-28 01:24:57 +00003108 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003109 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003110 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003111 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003112 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3113 rc = SQLITE_READONLY;
3114 goto abort_due_to_error;
3115 }
drh653b82a2009-06-22 11:10:47 +00003116 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003117
danielk197724162fe2004-06-04 06:22:00 +00003118 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003119 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
drhcbd8db32015-08-20 17:18:32 +00003120 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3121 testcase( rc==SQLITE_BUSY_RECOVERY );
3122 if( (rc&0xff)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003123 p->pc = (int)(pOp - aOp);
drhcbd8db32015-08-20 17:18:32 +00003124 p->rc = rc;
drh900b31e2007-08-28 02:27:51 +00003125 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003126 }
drh9e9f1bd2009-10-13 15:36:51 +00003127 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003128 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003129 }
dane0af83a2009-09-08 19:15:01 +00003130
3131 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003132 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003133 ){
3134 assert( sqlite3BtreeIsInTrans(pBt) );
3135 if( p->iStatement==0 ){
3136 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3137 db->nStatement++;
3138 p->iStatement = db->nSavepoint + db->nStatement;
3139 }
dana311b802011-04-26 19:21:34 +00003140
drh346506f2011-05-25 01:16:42 +00003141 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003142 if( rc==SQLITE_OK ){
3143 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3144 }
dan1da40a32009-09-19 17:00:31 +00003145
3146 /* Store the current value of the database handles deferred constraint
3147 ** counter. If the statement transaction needs to be rolled back,
3148 ** the value of this counter needs to be restored too. */
3149 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003150 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003151 }
drhb22f7c82014-02-06 23:56:27 +00003152
drh51a74d42015-02-28 01:04:27 +00003153 /* Gather the schema version number for checking:
3154 ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
3155 ** each time a query is executed to ensure that the internal cache of the
3156 ** schema used when compiling the SQL query matches the schema of the
3157 ** database against which the compiled query is actually executed.
3158 */
drhb22f7c82014-02-06 23:56:27 +00003159 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3160 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3161 }else{
3162 iGen = iMeta = 0;
3163 }
3164 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3165 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3166 sqlite3DbFree(db, p->zErrMsg);
3167 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3168 /* If the schema-cookie from the database file matches the cookie
3169 ** stored with the in-memory representation of the schema, do
3170 ** not reload the schema from the database file.
3171 **
3172 ** If virtual-tables are in use, this is not just an optimization.
3173 ** Often, v-tables store their data in other SQLite tables, which
3174 ** are queried from within xNext() and other v-table methods using
3175 ** prepared queries. If such a query is out-of-date, we do not want to
3176 ** discard the database schema, as the user code implementing the
3177 ** v-table would have to be ready for the sqlite3_vtab structure itself
3178 ** to be invalidated whenever sqlite3_step() is called from within
3179 ** a v-table method.
3180 */
3181 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3182 sqlite3ResetOneSchema(db, pOp->p1);
3183 }
3184 p->expired = 1;
3185 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003186 }
drh9467abf2016-02-17 18:44:11 +00003187 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003188 break;
3189}
3190
drhb1fdb2a2008-01-05 04:06:03 +00003191/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003192**
drh9cbf3422008-01-17 16:22:13 +00003193** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003194** P3==1 is the schema version. P3==2 is the database format.
3195** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003196** the main database file and P1==1 is the database file used to store
3197** temporary tables.
drh4a324312001-12-21 14:30:42 +00003198**
drh50e5dad2001-09-15 00:57:28 +00003199** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003200** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003201** executing this instruction.
3202*/
drh27a348c2015-04-13 19:14:06 +00003203case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003204 int iMeta;
drh856c1032009-06-02 15:21:42 +00003205 int iDb;
3206 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003207
drh1713afb2013-06-28 01:24:57 +00003208 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003209 iDb = pOp->p1;
3210 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003211 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003212 assert( iDb>=0 && iDb<db->nDb );
3213 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003214 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003215
danielk1977602b4662009-07-02 07:47:33 +00003216 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003217 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003218 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003219 break;
3220}
3221
drh98757152008-01-09 23:04:12 +00003222/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003223**
drh1861afc2016-02-01 21:48:34 +00003224** Write the integer value P3 into cookie number P2 of database P1.
3225** P2==1 is the schema version. P2==2 is the database format.
3226** P2==3 is the recommended pager cache
danielk19770d19f7a2009-06-03 11:25:07 +00003227** size, and so forth. P1==0 is the main database file and P1==1 is the
3228** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003229**
3230** A transaction must be started before executing this opcode.
3231*/
drh1861afc2016-02-01 21:48:34 +00003232case OP_SetCookie: {
drh3f7d4e42004-07-24 14:35:58 +00003233 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003234 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003235 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003236 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003237 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003238 pDb = &db->aDb[pOp->p1];
3239 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003240 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drha3b321d2004-05-11 09:31:31 +00003241 /* See note about index shifting on OP_ReadCookie */
drh1861afc2016-02-01 21:48:34 +00003242 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
danielk19770d19f7a2009-06-03 11:25:07 +00003243 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003244 /* When the schema cookie changes, record the new cookie internally */
drh1861afc2016-02-01 21:48:34 +00003245 pDb->pSchema->schema_cookie = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003246 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003247 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003248 /* Record changes in the file format */
drh1861afc2016-02-01 21:48:34 +00003249 pDb->pSchema->file_format = pOp->p3;
drh3f7d4e42004-07-24 14:35:58 +00003250 }
drhfd426c62006-01-30 15:34:22 +00003251 if( pOp->p1==1 ){
3252 /* Invalidate all prepared statements whenever the TEMP database
3253 ** schema is changed. Ticket #1644 */
3254 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003255 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003256 }
drh9467abf2016-02-17 18:44:11 +00003257 if( rc ) goto abort_due_to_error;
drh50e5dad2001-09-15 00:57:28 +00003258 break;
3259}
3260
drh98757152008-01-09 23:04:12 +00003261/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003262** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003263**
drhecdc7532001-09-23 02:35:53 +00003264** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003265** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003266** P3==0 means the main database, P3==1 means the database used for
3267** temporary tables, and P3>1 means used the corresponding attached
3268** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003269** values need not be contiguous but all P1 values should be small integers.
3270** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003271**
drh98757152008-01-09 23:04:12 +00003272** If P5!=0 then use the content of register P2 as the root page, not
3273** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003274**
drhb19a2bc2001-09-16 00:13:26 +00003275** There will be a read lock on the database whenever there is an
3276** open cursor. If the database was unlocked prior to this instruction
3277** then a read lock is acquired as part of this instruction. A read
3278** lock allows other processes to read the database but prohibits
3279** any other process from modifying the database. The read lock is
3280** released when all cursors are closed. If this instruction attempts
3281** to get a read lock but fails, the script terminates with an
3282** SQLITE_BUSY error code.
3283**
danielk1977d336e222009-02-20 10:58:41 +00003284** The P4 value may be either an integer (P4_INT32) or a pointer to
3285** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3286** structure, then said structure defines the content and collating
3287** sequence of the index being opened. Otherwise, if P4 is an integer
3288** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003289**
drh35263192014-07-22 20:02:19 +00003290** See also: OpenWrite, ReopenIdx
3291*/
3292/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3293** Synopsis: root=P2 iDb=P3
3294**
3295** The ReopenIdx opcode works exactly like ReadOpen except that it first
3296** checks to see if the cursor on P1 is already open with a root page
3297** number of P2 and if it is this opcode becomes a no-op. In other words,
3298** if the cursor is already open, do not reopen it.
3299**
3300** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3301** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3302** every other ReopenIdx or OpenRead for the same cursor number.
3303**
3304** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003305*/
drh98757152008-01-09 23:04:12 +00003306/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003307** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003308**
3309** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003310** page is P2. Or if P5!=0 use the content of register P2 to find the
3311** root page.
drhecdc7532001-09-23 02:35:53 +00003312**
danielk1977d336e222009-02-20 10:58:41 +00003313** The P4 value may be either an integer (P4_INT32) or a pointer to
3314** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3315** structure, then said structure defines the content and collating
3316** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003317** value, it is set to the number of columns in the table, or to the
3318** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003319**
drh001bbcb2003-03-19 03:14:00 +00003320** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003321** in read/write mode. For a given table, there can be one or more read-only
3322** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003323**
drh001bbcb2003-03-19 03:14:00 +00003324** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003325*/
drh35263192014-07-22 20:02:19 +00003326case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003327 int nField;
3328 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003329 int p2;
3330 int iDb;
drhf57b3392001-10-08 13:22:32 +00003331 int wrFlag;
3332 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003333 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003334 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003335
drhe0997b32015-03-20 14:57:50 +00003336 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003337 assert( pOp->p4type==P4_KEYINFO );
3338 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003339 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003340 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003341 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003342 }
3343 /* If the cursor is not currently open or is open on a different
3344 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003345case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003346case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003347
drhe0997b32015-03-20 14:57:50 +00003348 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003349 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003350 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3351 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003352
danfa401de2009-10-16 14:55:03 +00003353 if( p->expired ){
drh47b7fc72014-11-11 01:33:57 +00003354 rc = SQLITE_ABORT_ROLLBACK;
drh9467abf2016-02-17 18:44:11 +00003355 goto abort_due_to_error;
danfa401de2009-10-16 14:55:03 +00003356 }
3357
drh856c1032009-06-02 15:21:42 +00003358 nField = 0;
3359 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003360 p2 = pOp->p2;
3361 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003362 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003363 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003364 pDb = &db->aDb[iDb];
3365 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003366 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003367 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003368 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3369 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003370 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003371 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3372 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003373 }
3374 }else{
3375 wrFlag = 0;
3376 }
dan428c2182012-08-06 18:50:11 +00003377 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003378 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003379 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003380 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003381 assert( memIsValid(pIn2) );
3382 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003383 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003384 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003385 /* The p2 value always comes from a prior OP_CreateTable opcode and
3386 ** that opcode will always set the p2 value to 2 or more or else fail.
3387 ** If there were a failure, the prepared statement would have halted
3388 ** before reaching this instruction. */
drh9467abf2016-02-17 18:44:11 +00003389 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00003390 }
danielk1977d336e222009-02-20 10:58:41 +00003391 if( pOp->p4type==P4_KEYINFO ){
3392 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003393 assert( pKeyInfo->enc==ENC(db) );
3394 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003395 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003396 }else if( pOp->p4type==P4_INT32 ){
3397 nField = pOp->p4.i;
3398 }
drh653b82a2009-06-22 11:10:47 +00003399 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003400 assert( nField>=0 );
3401 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drhc960dcb2015-11-20 19:22:01 +00003402 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003403 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003404 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003405 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003406 pCur->pgnoRoot = p2;
drhb89aeb62016-01-27 15:49:32 +00003407#ifdef SQLITE_DEBUG
3408 pCur->wrFlag = wrFlag;
3409#endif
drhc960dcb2015-11-20 19:22:01 +00003410 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
danielk1977d336e222009-02-20 10:58:41 +00003411 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003412 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003413 ** SQLite used to check if the root-page flags were sane at this point
3414 ** and report database corruption if they were not, but this check has
3415 ** since moved into the btree layer. */
3416 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003417
3418open_cursor_set_hints:
3419 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3420 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003421 testcase( pOp->p5 & OPFLAG_BULKCSR );
drh9abe8412016-01-02 05:00:31 +00003422#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +00003423 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3424#endif
drhc960dcb2015-11-20 19:22:01 +00003425 sqlite3BtreeCursorHintFlags(pCur->uc.pCursor,
drhf7854c72015-10-27 13:24:37 +00003426 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh9467abf2016-02-17 18:44:11 +00003427 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003428 break;
3429}
3430
drh2a5d9902011-08-26 00:34:45 +00003431/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003432** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003433**
drhb9bb7c12006-06-11 23:41:55 +00003434** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003435** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003436** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003437** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003438**
drh25d3adb2010-04-05 15:11:08 +00003439** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003440** The cursor points to a BTree table if P4==0 and to a BTree index
3441** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003442** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003443**
drh2a5d9902011-08-26 00:34:45 +00003444** The P5 parameter can be a mask of the BTREE_* flags defined
3445** in btree.h. These flags control aspects of the operation of
3446** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3447** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003448*/
drha21a64d2010-04-06 22:33:55 +00003449/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003450** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003451**
3452** This opcode works the same as OP_OpenEphemeral. It has a
3453** different name to distinguish its use. Tables created using
3454** by this opcode will be used for automatically created transient
3455** indices in joins.
3456*/
3457case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003458case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003459 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003460 KeyInfo *pKeyInfo;
3461
drhd4187c72010-08-30 22:15:45 +00003462 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003463 SQLITE_OPEN_READWRITE |
3464 SQLITE_OPEN_CREATE |
3465 SQLITE_OPEN_EXCLUSIVE |
3466 SQLITE_OPEN_DELETEONCLOSE |
3467 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003468 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003469 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003470 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
drh4774b132004-06-12 20:12:51 +00003471 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003472 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003473 pCx->isEphemeral = 1;
dan689ab892011-08-12 15:02:00 +00003474 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3475 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003476 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003477 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003478 }
3479 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003480 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003481 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003482 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003483 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003484 */
drh41e13e12013-11-07 14:09:39 +00003485 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003486 int pgno;
drh66a51672008-01-03 00:01:23 +00003487 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003488 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003489 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003490 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003491 assert( pKeyInfo->db==db );
3492 assert( pKeyInfo->enc==ENC(db) );
3493 pCx->pKeyInfo = pKeyInfo;
drh62aaa6c2015-11-21 17:27:42 +00003494 rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR,
3495 pKeyInfo, pCx->uc.pCursor);
drhc6b52df2002-01-04 03:09:29 +00003496 }
drhf0863fe2005-06-12 21:35:51 +00003497 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003498 }else{
drh62aaa6c2015-11-21 17:27:42 +00003499 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR,
3500 0, pCx->uc.pCursor);
drhf0863fe2005-06-12 21:35:51 +00003501 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003502 }
drh5e00f6c2001-09-13 13:46:56 +00003503 }
drh9467abf2016-02-17 18:44:11 +00003504 if( rc ) goto abort_due_to_error;
drhd4187c72010-08-30 22:15:45 +00003505 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003506 break;
3507}
3508
danfad9f9a2014-04-01 18:41:51 +00003509/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003510**
3511** This opcode works like OP_OpenEphemeral except that it opens
3512** a transient index that is specifically designed to sort large
3513** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003514**
3515** If argument P3 is non-zero, then it indicates that the sorter may
3516** assume that a stable sort considering the first P3 fields of each
3517** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003518*/
drhca892a72011-09-03 00:17:51 +00003519case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003520 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003521
drh399af1d2013-11-20 17:25:55 +00003522 assert( pOp->p1>=0 );
3523 assert( pOp->p2>=0 );
drhc960dcb2015-11-20 19:22:01 +00003524 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_SORTER);
dan5134d132011-09-02 10:31:11 +00003525 if( pCx==0 ) goto no_mem;
3526 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003527 assert( pCx->pKeyInfo->db==db );
3528 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003529 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh9467abf2016-02-17 18:44:11 +00003530 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00003531 break;
3532}
3533
dan78d58432014-03-25 15:04:07 +00003534/* Opcode: SequenceTest P1 P2 * * *
3535** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3536**
3537** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3538** to P2. Regardless of whether or not the jump is taken, increment the
3539** the sequence value.
3540*/
3541case OP_SequenceTest: {
3542 VdbeCursor *pC;
3543 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3544 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003545 assert( isSorter(pC) );
dan78d58432014-03-25 15:04:07 +00003546 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003547 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003548 }
3549 break;
3550}
3551
drh5f612292014-02-08 23:20:32 +00003552/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003553** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003554**
3555** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003556** row of data. The content of that one row is the content of memory
3557** register P2. In other words, cursor P1 becomes an alias for the
3558** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003559**
drh2d8d7ce2010-02-15 15:17:05 +00003560** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003561** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003562** individual columns using the OP_Column opcode. The OP_Column opcode
3563** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003564**
3565** P3 is the number of fields in the records that will be stored by
3566** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003567*/
drh9cbf3422008-01-17 16:22:13 +00003568case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003569 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003570
drh653b82a2009-06-22 11:10:47 +00003571 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003572 assert( pOp->p3>=0 );
drhc960dcb2015-11-20 19:22:01 +00003573 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, CURTYPE_PSEUDO);
drh4774b132004-06-12 20:12:51 +00003574 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003575 pCx->nullRow = 1;
drhc960dcb2015-11-20 19:22:01 +00003576 pCx->uc.pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003577 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003578 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003579 break;
3580}
3581
drh98757152008-01-09 23:04:12 +00003582/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003583**
3584** Close a cursor previously opened as P1. If P1 is not
3585** currently open, this instruction is a no-op.
3586*/
drh9cbf3422008-01-17 16:22:13 +00003587case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003588 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3589 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3590 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003591 break;
3592}
3593
drh97bae792015-06-05 15:59:57 +00003594#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3595/* Opcode: ColumnsUsed P1 * * P4 *
3596**
3597** This opcode (which only exists if SQLite was compiled with
3598** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3599** table or index for cursor P1 are used. P4 is a 64-bit integer
3600** (P4_INT64) in which the first 63 bits are one for each of the
3601** first 63 columns of the table or index that are actually used
3602** by the cursor. The high-order bit is set if any column after
3603** the 64th is used.
3604*/
3605case OP_ColumnsUsed: {
3606 VdbeCursor *pC;
3607 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00003608 assert( pC->eCurType==CURTYPE_BTREE );
drh97bae792015-06-05 15:59:57 +00003609 pC->maskUsed = *(u64*)pOp->p4.pI64;
3610 break;
3611}
3612#endif
3613
drh8af3f772014-07-25 18:01:06 +00003614/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003615** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003616**
danielk1977b790c6c2008-04-18 10:25:24 +00003617** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003618** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003619** to an SQL index, then P3 is the first in an array of P4 registers
3620** that are used as an unpacked index key.
3621**
3622** Reposition cursor P1 so that it points to the smallest entry that
3623** is greater than or equal to the key value. If there are no records
3624** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003625**
drhb1d607d2015-11-05 22:30:54 +00003626** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3627** opcode will always land on a record that equally equals the key, or
3628** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3629** opcode must be followed by an IdxLE opcode with the same arguments.
3630** The IdxLE opcode will be skipped if this opcode succeeds, but the
3631** IdxLE opcode will be used on subsequent loop iterations.
3632**
drh8af3f772014-07-25 18:01:06 +00003633** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003634** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003635** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003636**
drh935850e2014-05-24 17:15:15 +00003637** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003638*/
drh8af3f772014-07-25 18:01:06 +00003639/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003640** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003641**
danielk1977b790c6c2008-04-18 10:25:24 +00003642** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003643** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003644** to an SQL index, then P3 is the first in an array of P4 registers
3645** that are used as an unpacked index key.
3646**
3647** Reposition cursor P1 so that it points to the smallest entry that
3648** is greater than the key value. If there are no records greater than
3649** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003650**
drh8af3f772014-07-25 18:01:06 +00003651** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003652** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003653** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003654**
drh935850e2014-05-24 17:15:15 +00003655** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003656*/
drh8af3f772014-07-25 18:01:06 +00003657/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003658** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003659**
danielk1977b790c6c2008-04-18 10:25:24 +00003660** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003661** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003662** to an SQL index, then P3 is the first in an array of P4 registers
3663** that are used as an unpacked index key.
3664**
3665** Reposition cursor P1 so that it points to the largest entry that
3666** is less than the key value. If there are no records less than
3667** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003668**
drh8af3f772014-07-25 18:01:06 +00003669** This opcode leaves the cursor configured to move in reverse order,
3670** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003671** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003672**
drh935850e2014-05-24 17:15:15 +00003673** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003674*/
drh8af3f772014-07-25 18:01:06 +00003675/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003676** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003677**
danielk1977b790c6c2008-04-18 10:25:24 +00003678** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003679** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003680** to an SQL index, then P3 is the first in an array of P4 registers
3681** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003682**
danielk1977b790c6c2008-04-18 10:25:24 +00003683** Reposition cursor P1 so that it points to the largest entry that
3684** is less than or equal to the key value. If there are no records
3685** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003686**
drh8af3f772014-07-25 18:01:06 +00003687** This opcode leaves the cursor configured to move in reverse order,
3688** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003689** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003690**
drhb1d607d2015-11-05 22:30:54 +00003691** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3692** opcode will always land on a record that equally equals the key, or
3693** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3694** opcode must be followed by an IdxGE opcode with the same arguments.
3695** The IdxGE opcode will be skipped if this opcode succeeds, but the
3696** IdxGE opcode will be used on subsequent loop iterations.
3697**
drh935850e2014-05-24 17:15:15 +00003698** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003699*/
drh4a1d3652014-02-14 15:13:36 +00003700case OP_SeekLT: /* jump, in3 */
3701case OP_SeekLE: /* jump, in3 */
3702case OP_SeekGE: /* jump, in3 */
3703case OP_SeekGT: { /* jump, in3 */
drhb1d607d2015-11-05 22:30:54 +00003704 int res; /* Comparison result */
3705 int oc; /* Opcode */
3706 VdbeCursor *pC; /* The cursor to seek */
3707 UnpackedRecord r; /* The key to seek for */
3708 int nField; /* Number of columns or fields in the key */
3709 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003710 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003711
drh653b82a2009-06-22 11:10:47 +00003712 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003713 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003714 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003715 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00003716 assert( pC->eCurType==CURTYPE_BTREE );
drh4a1d3652014-02-14 15:13:36 +00003717 assert( OP_SeekLE == OP_SeekLT+1 );
3718 assert( OP_SeekGE == OP_SeekLT+2 );
3719 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003720 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00003721 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003722 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003723 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003724 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003725#ifdef SQLITE_DEBUG
3726 pC->seekOp = pOp->opcode;
3727#endif
drhe0997b32015-03-20 14:57:50 +00003728
drh3da046d2013-11-11 03:24:11 +00003729 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003730 /* The BTREE_SEEK_EQ flag is only set on index cursors */
drhc960dcb2015-11-20 19:22:01 +00003731 assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0 );
drhd6b79462015-11-07 01:19:00 +00003732
drh3da046d2013-11-11 03:24:11 +00003733 /* The input value in P3 might be of any type: integer, real, string,
3734 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003735 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003736 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003737 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003738 applyNumericAffinity(pIn3, 0);
3739 }
drh3da046d2013-11-11 03:24:11 +00003740 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003741
drh3da046d2013-11-11 03:24:11 +00003742 /* If the P3 value could not be converted into an integer without
3743 ** loss of information, then special processing is required... */
3744 if( (pIn3->flags & MEM_Int)==0 ){
3745 if( (pIn3->flags & MEM_Real)==0 ){
3746 /* If the P3 value cannot be converted into any kind of a number,
3747 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003748 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003749 break;
3750 }
drh959403f2008-12-12 17:56:16 +00003751
danaa1776f2013-11-26 18:22:59 +00003752 /* If the approximation iKey is larger than the actual real search
3753 ** term, substitute >= for > and < for <=. e.g. if the search term
3754 ** is 4.9 and the integer approximation 5:
3755 **
3756 ** (x > 4.9) -> (x >= 5)
3757 ** (x <= 4.9) -> (x < 5)
3758 */
drh74eaba42014-09-18 17:52:15 +00003759 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003760 assert( OP_SeekGE==(OP_SeekGT-1) );
3761 assert( OP_SeekLT==(OP_SeekLE-1) );
3762 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3763 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003764 }
3765
3766 /* If the approximation iKey is smaller than the actual real search
3767 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003768 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003769 assert( OP_SeekLE==(OP_SeekLT+1) );
3770 assert( OP_SeekGT==(OP_SeekGE+1) );
3771 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3772 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003773 }
drh3da046d2013-11-11 03:24:11 +00003774 }
drhc960dcb2015-11-20 19:22:01 +00003775 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003776 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003777 if( rc!=SQLITE_OK ){
3778 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003779 }
drhaa736092009-06-22 00:55:30 +00003780 }else{
drhd6b79462015-11-07 01:19:00 +00003781 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3782 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3783 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3784 */
drhc960dcb2015-11-20 19:22:01 +00003785 if( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ) ){
drhd6b79462015-11-07 01:19:00 +00003786 eqOnly = 1;
3787 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3788 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3789 assert( pOp[1].p1==pOp[0].p1 );
3790 assert( pOp[1].p2==pOp[0].p2 );
3791 assert( pOp[1].p3==pOp[0].p3 );
3792 assert( pOp[1].p4.i==pOp[0].p4.i );
3793 }
3794
drh3da046d2013-11-11 03:24:11 +00003795 nField = pOp->p4.i;
3796 assert( pOp->p4type==P4_INT32 );
3797 assert( nField>0 );
3798 r.pKeyInfo = pC->pKeyInfo;
3799 r.nField = (u16)nField;
3800
3801 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003802 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003803 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003804 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003805 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003806 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003807 */
dan1fed5da2014-02-25 21:01:25 +00003808 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3809 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3810 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3811 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3812 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003813
3814 r.aMem = &aMem[pOp->p3];
3815#ifdef SQLITE_DEBUG
3816 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3817#endif
3818 ExpandBlob(r.aMem);
drh70528d72015-11-05 20:25:09 +00003819 r.eqSeen = 0;
drhc960dcb2015-11-20 19:22:01 +00003820 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, &r, 0, 0, &res);
drh3da046d2013-11-11 03:24:11 +00003821 if( rc!=SQLITE_OK ){
3822 goto abort_due_to_error;
3823 }
drhb1d607d2015-11-05 22:30:54 +00003824 if( eqOnly && r.eqSeen==0 ){
3825 assert( res!=0 );
3826 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00003827 }
drh3da046d2013-11-11 03:24:11 +00003828 }
3829 pC->deferredMoveto = 0;
3830 pC->cacheStatus = CACHE_STALE;
3831#ifdef SQLITE_TEST
3832 sqlite3_search_count++;
3833#endif
drh4a1d3652014-02-14 15:13:36 +00003834 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3835 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003836 res = 0;
drhc960dcb2015-11-20 19:22:01 +00003837 rc = sqlite3BtreeNext(pC->uc.pCursor, &res);
drh3da046d2013-11-11 03:24:11 +00003838 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003839 }else{
3840 res = 0;
3841 }
3842 }else{
drh4a1d3652014-02-14 15:13:36 +00003843 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3844 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003845 res = 0;
drhc960dcb2015-11-20 19:22:01 +00003846 rc = sqlite3BtreePrevious(pC->uc.pCursor, &res);
drh3da046d2013-11-11 03:24:11 +00003847 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003848 }else{
3849 /* res might be negative because the table is empty. Check to
3850 ** see if this is the case.
3851 */
drhc960dcb2015-11-20 19:22:01 +00003852 res = sqlite3BtreeEof(pC->uc.pCursor);
drh3da046d2013-11-11 03:24:11 +00003853 }
3854 }
drhb1d607d2015-11-05 22:30:54 +00003855seek_not_found:
drh3da046d2013-11-11 03:24:11 +00003856 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003857 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003858 if( res ){
drhf56fa462015-04-13 21:39:54 +00003859 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00003860 }else if( eqOnly ){
3861 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3862 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00003863 }
drh5e00f6c2001-09-13 13:46:56 +00003864 break;
3865}
drh959403f2008-12-12 17:56:16 +00003866
3867
drh8cff69d2009-11-12 19:59:44 +00003868/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003869** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003870**
drh8cff69d2009-11-12 19:59:44 +00003871** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3872** P4>0 then register P3 is the first of P4 registers that form an unpacked
3873** record.
3874**
3875** Cursor P1 is on an index btree. If the record identified by P3 and P4
3876** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003877** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003878**
drhcefc87f2014-08-01 01:40:33 +00003879** This operation leaves the cursor in a state where it can be
3880** advanced in the forward direction. The Next instruction will work,
3881** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003882**
drh6f225d02013-10-26 13:36:51 +00003883** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003884*/
drh8cff69d2009-11-12 19:59:44 +00003885/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003886** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003887**
drh8cff69d2009-11-12 19:59:44 +00003888** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3889** P4>0 then register P3 is the first of P4 registers that form an unpacked
3890** record.
3891**
3892** Cursor P1 is on an index btree. If the record identified by P3 and P4
3893** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3894** does contain an entry whose prefix matches the P3/P4 record then control
3895** falls through to the next instruction and P1 is left pointing at the
3896** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003897**
drh8af3f772014-07-25 18:01:06 +00003898** This operation leaves the cursor in a state where it cannot be
3899** advanced in either direction. In other words, the Next and Prev
3900** opcodes do not work after this operation.
3901**
drh6f225d02013-10-26 13:36:51 +00003902** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003903*/
drh6f225d02013-10-26 13:36:51 +00003904/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003905** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003906**
3907** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3908** P4>0 then register P3 is the first of P4 registers that form an unpacked
3909** record.
3910**
3911** Cursor P1 is on an index btree. If the record identified by P3 and P4
3912** contains any NULL value, jump immediately to P2. If all terms of the
3913** record are not-NULL then a check is done to determine if any row in the
3914** P1 index btree has a matching key prefix. If there are no matches, jump
3915** immediately to P2. If there is a match, fall through and leave the P1
3916** cursor pointing to the matching row.
3917**
3918** This opcode is similar to OP_NotFound with the exceptions that the
3919** branch is always taken if any part of the search key input is NULL.
3920**
drh8af3f772014-07-25 18:01:06 +00003921** This operation leaves the cursor in a state where it cannot be
3922** advanced in either direction. In other words, the Next and Prev
3923** opcodes do not work after this operation.
3924**
drh6f225d02013-10-26 13:36:51 +00003925** See also: NotFound, Found, NotExists
3926*/
3927case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003928case OP_NotFound: /* jump, in3 */
3929case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003930 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00003931 int takeJump;
drh6f225d02013-10-26 13:36:51 +00003932 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003933 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003934 int res;
dan03e9cfc2011-09-05 14:20:27 +00003935 char *pFree;
drh856c1032009-06-02 15:21:42 +00003936 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003937 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003938 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003939
dan0ff297e2009-09-25 17:03:14 +00003940#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003941 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003942#endif
3943
drhaa736092009-06-22 00:55:30 +00003944 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003945 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003946 pC = p->apCsr[pOp->p1];
3947 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003948#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00003949 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00003950#endif
drh3c657212009-11-17 23:59:58 +00003951 pIn3 = &aMem[pOp->p3];
drhc960dcb2015-11-20 19:22:01 +00003952 assert( pC->eCurType==CURTYPE_BTREE );
3953 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00003954 assert( pC->isTable==0 );
drhf56fa462015-04-13 21:39:54 +00003955 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00003956 if( pOp->p4.i>0 ){
3957 r.pKeyInfo = pC->pKeyInfo;
3958 r.nField = (u16)pOp->p4.i;
3959 r.aMem = pIn3;
drh826af372014-02-08 19:12:21 +00003960 for(ii=0; ii<r.nField; ii++){
3961 assert( memIsValid(&r.aMem[ii]) );
3962 ExpandBlob(&r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003963#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00003964 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003965#endif
drh826af372014-02-08 19:12:21 +00003966 }
drh3da046d2013-11-11 03:24:11 +00003967 pIdxKey = &r;
3968 }else{
3969 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3970 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
danb391b942014-11-07 14:41:11 +00003971 );
drh3da046d2013-11-11 03:24:11 +00003972 if( pIdxKey==0 ) goto no_mem;
3973 assert( pIn3->flags & MEM_Blob );
danb391b942014-11-07 14:41:11 +00003974 ExpandBlob(pIn3);
drh3da046d2013-11-11 03:24:11 +00003975 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh3da046d2013-11-11 03:24:11 +00003976 }
dan1fed5da2014-02-25 21:01:25 +00003977 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00003978 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00003979 if( pOp->opcode==OP_NoConflict ){
3980 /* For the OP_NoConflict opcode, take the jump if any of the
3981 ** input fields are NULL, since any key with a NULL will not
3982 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00003983 for(ii=0; ii<pIdxKey->nField; ii++){
3984 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00003985 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00003986 break;
drh6f225d02013-10-26 13:36:51 +00003987 }
3988 }
drh5e00f6c2001-09-13 13:46:56 +00003989 }
drhc960dcb2015-11-20 19:22:01 +00003990 rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, pIdxKey, 0, 0, &res);
drhf56fa462015-04-13 21:39:54 +00003991 sqlite3DbFree(db, pFree);
drh3da046d2013-11-11 03:24:11 +00003992 if( rc!=SQLITE_OK ){
drh9467abf2016-02-17 18:44:11 +00003993 goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003994 }
drh1fd522f2013-11-21 00:10:35 +00003995 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003996 alreadyExists = (res==0);
3997 pC->nullRow = 1-alreadyExists;
3998 pC->deferredMoveto = 0;
3999 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004000 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00004001 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004002 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004003 }else{
drhf56fa462015-04-13 21:39:54 +00004004 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4005 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004006 }
drh5e00f6c2001-09-13 13:46:56 +00004007 break;
4008}
4009
drh9cbf3422008-01-17 16:22:13 +00004010/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004011** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004012**
drh261c02d2013-10-25 14:46:15 +00004013** P1 is the index of a cursor open on an SQL table btree (with integer
4014** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004015** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4016** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4017** leave the cursor pointing at that record and fall through to the next
4018** instruction.
drh6b125452002-01-28 15:53:03 +00004019**
drh261c02d2013-10-25 14:46:15 +00004020** The OP_NotFound opcode performs the same operation on index btrees
4021** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004022**
drh8af3f772014-07-25 18:01:06 +00004023** This opcode leaves the cursor in a state where it cannot be advanced
4024** in either direction. In other words, the Next and Prev opcodes will
4025** not work following this opcode.
4026**
drh11e85272013-10-26 15:40:48 +00004027** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00004028*/
drh9cbf3422008-01-17 16:22:13 +00004029case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004030 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004031 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004032 int res;
4033 u64 iKey;
4034
drh3c657212009-11-17 23:59:58 +00004035 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004036 assert( pIn3->flags & MEM_Int );
4037 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4038 pC = p->apCsr[pOp->p1];
4039 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004040#ifdef SQLITE_DEBUG
4041 pC->seekOp = 0;
4042#endif
drhaa736092009-06-22 00:55:30 +00004043 assert( pC->isTable );
drhc960dcb2015-11-20 19:22:01 +00004044 assert( pC->eCurType==CURTYPE_BTREE );
4045 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004046 assert( pCrsr!=0 );
4047 res = 0;
4048 iKey = pIn3->u.i;
4049 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004050 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004051 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004052 pC->nullRow = 0;
4053 pC->cacheStatus = CACHE_STALE;
4054 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004055 VdbeBranchTaken(res!=0,2);
drh1fd522f2013-11-21 00:10:35 +00004056 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004057 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004058 assert( rc==SQLITE_OK );
4059 if( pOp->p2==0 ){
4060 rc = SQLITE_CORRUPT_BKPT;
4061 }else{
4062 goto jump_to_p2;
4063 }
danc6157e12015-09-14 09:23:47 +00004064 }
drh9467abf2016-02-17 18:44:11 +00004065 if( rc ) goto abort_due_to_error;
drh6b125452002-01-28 15:53:03 +00004066 break;
4067}
4068
drh4c583122008-01-04 22:01:03 +00004069/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004070** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004071**
drh4c583122008-01-04 22:01:03 +00004072** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004073** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004074** The sequence number on the cursor is incremented after this
4075** instruction.
drh4db38a72005-09-01 12:16:28 +00004076*/
drh27a348c2015-04-13 19:14:06 +00004077case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004078 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4079 assert( p->apCsr[pOp->p1]!=0 );
drhc960dcb2015-11-20 19:22:01 +00004080 assert( p->apCsr[pOp->p1]->eCurType!=CURTYPE_VTAB );
drh27a348c2015-04-13 19:14:06 +00004081 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004082 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004083 break;
4084}
4085
4086
drh98757152008-01-09 23:04:12 +00004087/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004088** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004089**
drhf0863fe2005-06-12 21:35:51 +00004090** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004091** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004092** table that cursor P1 points to. The new record number is written
4093** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004094**
dan76d462e2009-08-30 11:42:51 +00004095** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4096** the largest previously generated record number. No new record numbers are
4097** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004098** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004099** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004100** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004101*/
drh27a348c2015-04-13 19:14:06 +00004102case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004103 i64 v; /* The new rowid */
4104 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4105 int res; /* Result of an sqlite3BtreeLast() */
4106 int cnt; /* Counter to limit the number of searches */
4107 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004108 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004109
drh856c1032009-06-02 15:21:42 +00004110 v = 0;
4111 res = 0;
drh27a348c2015-04-13 19:14:06 +00004112 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004113 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4114 pC = p->apCsr[pOp->p1];
4115 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004116 assert( pC->eCurType==CURTYPE_BTREE );
4117 assert( pC->uc.pCursor!=0 );
drh98ef0f62015-06-30 01:25:52 +00004118 {
drh5cf8e8c2002-02-19 22:42:05 +00004119 /* The next rowid or record number (different terms for the same
4120 ** thing) is obtained in a two-step algorithm.
4121 **
4122 ** First we attempt to find the largest existing rowid and add one
4123 ** to that. But if the largest existing rowid is already the maximum
4124 ** positive integer, we have to fall through to the second
4125 ** probabilistic algorithm
4126 **
4127 ** The second algorithm is to select a rowid at random and see if
4128 ** it already exists in the table. If it does not exist, we have
4129 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004130 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004131 */
drhaa736092009-06-22 00:55:30 +00004132 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004133
drh75f86a42005-02-17 00:03:06 +00004134#ifdef SQLITE_32BIT_ROWID
4135# define MAX_ROWID 0x7fffffff
4136#else
drhfe2093d2005-01-20 22:48:47 +00004137 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4138 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4139 ** to provide the constant while making all compilers happy.
4140 */
danielk197764202cf2008-11-17 15:31:47 +00004141# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004142#endif
drhfe2093d2005-01-20 22:48:47 +00004143
drh5cf8e8c2002-02-19 22:42:05 +00004144 if( !pC->useRandomRowid ){
drhc960dcb2015-11-20 19:22:01 +00004145 rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
drhe0670b62014-02-12 21:31:12 +00004146 if( rc!=SQLITE_OK ){
4147 goto abort_due_to_error;
4148 }
4149 if( res ){
4150 v = 1; /* IMP: R-61914-48074 */
4151 }else{
drhc960dcb2015-11-20 19:22:01 +00004152 assert( sqlite3BtreeCursorIsValid(pC->uc.pCursor) );
4153 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
drhe0670b62014-02-12 21:31:12 +00004154 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
4155 if( v>=MAX_ROWID ){
4156 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004157 }else{
drhe0670b62014-02-12 21:31:12 +00004158 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004159 }
drh3fc190c2001-09-14 03:24:23 +00004160 }
drhe0670b62014-02-12 21:31:12 +00004161 }
drh205f48e2004-11-05 00:43:11 +00004162
4163#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004164 if( pOp->p3 ){
4165 /* Assert that P3 is a valid memory cell. */
4166 assert( pOp->p3>0 );
4167 if( p->pFrame ){
4168 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004169 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004170 assert( pOp->p3<=pFrame->nMem );
4171 pMem = &pFrame->aMem[pOp->p3];
4172 }else{
4173 /* Assert that P3 is a valid memory cell. */
4174 assert( pOp->p3<=(p->nMem-p->nCursor) );
4175 pMem = &aMem[pOp->p3];
4176 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004177 }
drhe0670b62014-02-12 21:31:12 +00004178 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004179
drhe0670b62014-02-12 21:31:12 +00004180 REGISTER_TRACE(pOp->p3, pMem);
4181 sqlite3VdbeMemIntegerify(pMem);
4182 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4183 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4184 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
4185 goto abort_due_to_error;
4186 }
4187 if( v<pMem->u.i+1 ){
4188 v = pMem->u.i + 1;
4189 }
4190 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004191 }
drhe0670b62014-02-12 21:31:12 +00004192#endif
drh5cf8e8c2002-02-19 22:42:05 +00004193 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004194 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004195 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004196 ** engine starts picking positive candidate ROWIDs at random until
4197 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004198 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4199 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004200 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004201 do{
4202 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004203 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drhc960dcb2015-11-20 19:22:01 +00004204 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004205 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004206 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004207 && (++cnt<100));
drh9467abf2016-02-17 18:44:11 +00004208 if( rc ) goto abort_due_to_error;
4209 if( res==0 ){
drhc79c7612010-01-01 18:57:48 +00004210 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004211 goto abort_due_to_error;
4212 }
drh748a52c2010-09-01 11:50:08 +00004213 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004214 }
drha11846b2004-01-07 18:52:56 +00004215 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004216 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004217 }
drh4c583122008-01-04 22:01:03 +00004218 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004219 break;
4220}
4221
danielk19771f4aa332008-01-03 09:51:55 +00004222/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004223** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004224**
jplyon5a564222003-06-02 06:15:58 +00004225** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004226** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004227** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004228** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004229** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004230**
danielk19771f4aa332008-01-03 09:51:55 +00004231** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4232** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004233** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004234** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004235**
drh3e9ca092009-09-08 01:14:48 +00004236** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4237** the last seek operation (OP_NotExists) was a success, then this
4238** operation will not attempt to find the appropriate row before doing
4239** the insert but will instead overwrite the row that the cursor is
4240** currently pointing to. Presumably, the prior OP_NotExists opcode
4241** has already positioned the cursor correctly. This is an optimization
4242** that boosts performance by avoiding redundant seeks.
4243**
4244** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4245** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4246** is part of an INSERT operation. The difference is only important to
4247** the update hook.
4248**
drh66a51672008-01-03 00:01:23 +00004249** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004250** may be NULL. If it is not NULL, then the update-hook
4251** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4252**
drh93aed5a2008-01-16 17:46:38 +00004253** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4254** allocated, then ownership of P2 is transferred to the pseudo-cursor
4255** and register P2 becomes ephemeral. If the cursor is changed, the
4256** value of register P2 will then change. Make sure this does not
4257** cause any problems.)
4258**
drhf0863fe2005-06-12 21:35:51 +00004259** This instruction only works on tables. The equivalent instruction
4260** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004261*/
drhe05c9292009-10-29 13:48:10 +00004262/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004263** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004264**
4265** This works exactly like OP_Insert except that the key is the
4266** integer value P3, not the value of the integer stored in register P3.
4267*/
4268case OP_Insert:
4269case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004270 Mem *pData; /* MEM cell holding data for the record to be inserted */
4271 Mem *pKey; /* MEM cell holding key for the record */
4272 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4273 VdbeCursor *pC; /* Cursor to table into which insert is written */
4274 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004275 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004276 const char *zDb; /* database name - used by the update hook */
4277 const char *zTbl; /* Table name - used by the opdate hook */
4278 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004279
drha6c2ed92009-11-14 23:22:23 +00004280 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004281 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004282 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004283 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004284 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004285 assert( pC->eCurType==CURTYPE_BTREE );
4286 assert( pC->uc.pCursor!=0 );
drha05a7222008-01-19 03:35:58 +00004287 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004288 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004289
drhe05c9292009-10-29 13:48:10 +00004290 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004291 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004292 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004293 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004294 REGISTER_TRACE(pOp->p3, pKey);
4295 iKey = pKey->u.i;
4296 }else{
4297 assert( pOp->opcode==OP_InsertInt );
4298 iKey = pOp->p3;
4299 }
4300
drha05a7222008-01-19 03:35:58 +00004301 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004302 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004303 if( pData->flags & MEM_Null ){
4304 pData->z = 0;
4305 pData->n = 0;
4306 }else{
4307 assert( pData->flags & (MEM_Blob|MEM_Str) );
4308 }
drh3e9ca092009-09-08 01:14:48 +00004309 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4310 if( pData->flags & MEM_Zero ){
4311 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004312 }else{
drh3e9ca092009-09-08 01:14:48 +00004313 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004314 }
drhc960dcb2015-11-20 19:22:01 +00004315 rc = sqlite3BtreeInsert(pC->uc.pCursor, 0, iKey,
drh3e9ca092009-09-08 01:14:48 +00004316 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004317 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004318 );
drha05a7222008-01-19 03:35:58 +00004319 pC->deferredMoveto = 0;
4320 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004321
drha05a7222008-01-19 03:35:58 +00004322 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004323 if( rc ) goto abort_due_to_error;
4324 if( db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004325 zDb = db->aDb[pC->iDb].zName;
4326 zTbl = pOp->p4.z;
4327 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004328 assert( pC->isTable );
4329 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4330 assert( pC->iDb>=0 );
4331 }
drh5e00f6c2001-09-13 13:46:56 +00004332 break;
4333}
4334
danf0ee1d32015-09-12 19:26:11 +00004335/* Opcode: Delete P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004336**
drh5edc3122001-09-13 21:53:09 +00004337** Delete the record at which the P1 cursor is currently pointing.
4338**
drhe807bdb2016-01-21 17:06:33 +00004339** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
4340** the cursor will be left pointing at either the next or the previous
4341** record in the table. If it is left pointing at the next record, then
4342** the next Next instruction will be a no-op. As a result, in this case
4343** it is ok to delete a record from within a Next loop. If
4344** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
4345** left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004346**
drhdef19e32016-01-27 16:26:25 +00004347** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
4348** delete one of several associated with deleting a table row and all its
4349** associated index entries. Exactly one of those deletes is the "primary"
4350** delete. The others are all on OPFLAG_FORDELETE cursors or else are
4351** marked with the AUXDELETE flag.
drhe807bdb2016-01-21 17:06:33 +00004352**
4353** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
4354** change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004355**
drh91fd4d42008-01-19 20:11:25 +00004356** P1 must not be pseudo-table. It has to be a real table with
4357** multiple rows.
4358**
4359** If P4 is not NULL, then it is the name of the table that P1 is
4360** pointing to. The update hook will be invoked, if it exists.
4361** If P4 is not NULL then the P1 cursor must have been positioned
4362** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004363*/
drh9cbf3422008-01-17 16:22:13 +00004364case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004365 VdbeCursor *pC;
drhb79d5522015-09-14 19:26:37 +00004366 u8 hasUpdateCallback;
drh91fd4d42008-01-19 20:11:25 +00004367
drh653b82a2009-06-22 11:10:47 +00004368 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4369 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004370 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004371 assert( pC->eCurType==CURTYPE_BTREE );
4372 assert( pC->uc.pCursor!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004373 assert( pC->deferredMoveto==0 );
drh9a65f2c2009-06-22 19:05:40 +00004374
drhb79d5522015-09-14 19:26:37 +00004375 hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable;
4376 if( pOp->p5 && hasUpdateCallback ){
drhc960dcb2015-11-20 19:22:01 +00004377 sqlite3BtreeKeySize(pC->uc.pCursor, &pC->movetoTarget);
danf0ee1d32015-09-12 19:26:11 +00004378 }
drh91fd4d42008-01-19 20:11:25 +00004379
drhb53a5a92014-10-12 22:37:22 +00004380#ifdef SQLITE_DEBUG
4381 /* The seek operation that positioned the cursor prior to OP_Delete will
4382 ** have also set the pC->movetoTarget field to the rowid of the row that
4383 ** is being deleted */
danf0ee1d32015-09-12 19:26:11 +00004384 if( pOp->p4.z && pC->isTable && pOp->p5==0 ){
drhb53a5a92014-10-12 22:37:22 +00004385 i64 iKey = 0;
drhc960dcb2015-11-20 19:22:01 +00004386 sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
drhb53a5a92014-10-12 22:37:22 +00004387 assert( pC->movetoTarget==iKey );
4388 }
4389#endif
drhb89aeb62016-01-27 15:49:32 +00004390
drhdef19e32016-01-27 16:26:25 +00004391 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
4392 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
drhe807bdb2016-01-21 17:06:33 +00004393 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
drhdef19e32016-01-27 16:26:25 +00004394 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
drhb89aeb62016-01-27 15:49:32 +00004395
4396#ifdef SQLITE_DEBUG
dane61bbf42016-01-28 17:06:17 +00004397 if( p->pFrame==0 ){
4398 if( pC->isEphemeral==0
4399 && (pOp->p5 & OPFLAG_AUXDELETE)==0
4400 && (pC->wrFlag & OPFLAG_FORDELETE)==0
4401 ){
4402 nExtraDelete++;
4403 }
4404 if( pOp->p2 & OPFLAG_NCHANGE ){
4405 nExtraDelete--;
4406 }
drhb89aeb62016-01-27 15:49:32 +00004407 }
4408#endif
4409
drhc960dcb2015-11-20 19:22:01 +00004410 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004411 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004412
drh91fd4d42008-01-19 20:11:25 +00004413 /* Invoke the update-hook if required. */
drh9467abf2016-02-17 18:44:11 +00004414 if( rc ) goto abort_due_to_error;
4415 if( hasUpdateCallback ){
drh2c77be02013-11-27 21:07:03 +00004416 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
drhb53a5a92014-10-12 22:37:22 +00004417 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
drh91fd4d42008-01-19 20:11:25 +00004418 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004419 }
danielk1977b28af712004-06-21 06:50:26 +00004420 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004421 break;
4422}
drhb7f1d9a2009-09-08 02:27:58 +00004423/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004424**
drhb7f1d9a2009-09-08 02:27:58 +00004425** The value of the change counter is copied to the database handle
4426** change counter (returned by subsequent calls to sqlite3_changes()).
4427** Then the VMs internal change counter resets to 0.
4428** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004429*/
drh9cbf3422008-01-17 16:22:13 +00004430case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004431 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004432 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004433 break;
4434}
4435
drh1153c7b2013-11-01 22:02:56 +00004436/* Opcode: SorterCompare P1 P2 P3 P4
drhac502322014-07-30 13:56:48 +00004437** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004438**
drh1153c7b2013-11-01 22:02:56 +00004439** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004440** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004441** the sorter cursor currently points to. Only the first P4 fields
4442** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004443**
4444** If either P3 or the sorter contains a NULL in one of their significant
4445** fields (not counting the P4 fields at the end which are ignored) then
4446** the comparison is assumed to be equal.
4447**
4448** Fall through to next instruction if the two records compare equal to
4449** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004450*/
4451case OP_SorterCompare: {
4452 VdbeCursor *pC;
4453 int res;
drhac502322014-07-30 13:56:48 +00004454 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004455
4456 pC = p->apCsr[pOp->p1];
4457 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004458 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004459 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004460 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004461 res = 0;
drhac502322014-07-30 13:56:48 +00004462 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004463 VdbeBranchTaken(res!=0,2);
drh9467abf2016-02-17 18:44:11 +00004464 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00004465 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004466 break;
4467};
4468
drh6cf4a7d2014-10-13 13:00:58 +00004469/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004470** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004471**
4472** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004473** Then clear the column header cache on cursor P3.
4474**
4475** This opcode is normally use to move a record out of the sorter and into
4476** a register that is the source for a pseudo-table cursor created using
4477** OpenPseudo. That pseudo-table cursor is the one that is identified by
4478** parameter P3. Clearing the P3 column cache as part of this opcode saves
4479** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004480*/
4481case OP_SorterData: {
4482 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004483
dan5134d132011-09-02 10:31:11 +00004484 pOut = &aMem[pOp->p2];
4485 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004486 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004487 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004488 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004489 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9467abf2016-02-17 18:44:11 +00004490 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00004491 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004492 break;
4493}
4494
drh98757152008-01-09 23:04:12 +00004495/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004496** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004497**
drh98757152008-01-09 23:04:12 +00004498** Write into register P2 the complete row data for cursor P1.
4499** There is no interpretation of the data.
4500** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004501** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004502**
drhde4fcfd2008-01-19 23:50:26 +00004503** If the P1 cursor must be pointing to a valid row (not a NULL row)
4504** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004505*/
drh98757152008-01-09 23:04:12 +00004506/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004507** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004508**
drh98757152008-01-09 23:04:12 +00004509** Write into register P2 the complete row key for cursor P1.
4510** There is no interpretation of the data.
drh0fd61352014-02-07 02:29:45 +00004511** The key is copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004512** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004513**
drhde4fcfd2008-01-19 23:50:26 +00004514** If the P1 cursor must be pointing to a valid row (not a NULL row)
4515** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004516*/
danielk1977a7a8e142008-02-13 18:25:27 +00004517case OP_RowKey:
4518case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004519 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004520 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004521 u32 n;
drh856c1032009-06-02 15:21:42 +00004522 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004523
drha6c2ed92009-11-14 23:22:23 +00004524 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004525 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004526
drhf0863fe2005-06-12 21:35:51 +00004527 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004528 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4529 pC = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00004530 assert( pC!=0 );
4531 assert( pC->eCurType==CURTYPE_BTREE );
drh14da87f2013-11-20 21:51:33 +00004532 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004533 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004534 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drhde4fcfd2008-01-19 23:50:26 +00004535 assert( pC->nullRow==0 );
drhc960dcb2015-11-20 19:22:01 +00004536 assert( pC->uc.pCursor!=0 );
4537 pCrsr = pC->uc.pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004538
4539 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4540 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
drhc22284f2014-10-13 16:02:20 +00004541 ** the cursor. If this where not the case, on of the following assert()s
4542 ** would fail. Should this ever change (because of changes in the code
4543 ** generator) then the fix would be to insert a call to
4544 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004545 */
4546 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004547 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4548#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004549 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004550 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4551#endif
drh9a65f2c2009-06-22 19:05:40 +00004552
drh14da87f2013-11-20 21:51:33 +00004553 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004554 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004555 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004556 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004557 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004558 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004559 }
drhbfb19dc2009-06-05 16:46:53 +00004560 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004561 }else{
drhb07028f2011-10-14 21:49:18 +00004562 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004563 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004564 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004565 goto too_big;
4566 }
drhde4fcfd2008-01-19 23:50:26 +00004567 }
drh722246e2014-10-07 23:02:24 +00004568 testcase( n==0 );
4569 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
danielk1977a7a8e142008-02-13 18:25:27 +00004570 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004571 }
danielk1977a7a8e142008-02-13 18:25:27 +00004572 pOut->n = n;
4573 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004574 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004575 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4576 }else{
4577 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004578 }
drh9467abf2016-02-17 18:44:11 +00004579 if( rc ) goto abort_due_to_error;
danielk197796cb76f2008-01-04 13:24:28 +00004580 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004581 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004582 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004583 break;
4584}
4585
drh2133d822008-01-03 18:44:59 +00004586/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004587** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004588**
drh2133d822008-01-03 18:44:59 +00004589** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004590** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004591**
4592** P1 can be either an ordinary table or a virtual table. There used to
4593** be a separate OP_VRowid opcode for use with virtual tables, but this
4594** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004595*/
drh27a348c2015-04-13 19:14:06 +00004596case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004597 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004598 i64 v;
drh856c1032009-06-02 15:21:42 +00004599 sqlite3_vtab *pVtab;
4600 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004601
drh27a348c2015-04-13 19:14:06 +00004602 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004603 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4604 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004605 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004606 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004607 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004608 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004609 break;
4610 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004611 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004612#ifndef SQLITE_OMIT_VIRTUALTABLE
drhc960dcb2015-11-20 19:22:01 +00004613 }else if( pC->eCurType==CURTYPE_VTAB ){
4614 assert( pC->uc.pVCur!=0 );
4615 pVtab = pC->uc.pVCur->pVtab;
drh044925b2009-04-22 17:15:02 +00004616 pModule = pVtab->pModule;
4617 assert( pModule->xRowid );
drhc960dcb2015-11-20 19:22:01 +00004618 rc = pModule->xRowid(pC->uc.pVCur, &v);
dan016f7812013-08-21 17:35:48 +00004619 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00004620 if( rc ) goto abort_due_to_error;
drh044925b2009-04-22 17:15:02 +00004621#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004622 }else{
drhc960dcb2015-11-20 19:22:01 +00004623 assert( pC->eCurType==CURTYPE_BTREE );
4624 assert( pC->uc.pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004625 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004626 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004627 if( pC->nullRow ){
4628 pOut->flags = MEM_Null;
4629 break;
drh61495262009-04-22 15:32:59 +00004630 }
drhc960dcb2015-11-20 19:22:01 +00004631 rc = sqlite3BtreeKeySize(pC->uc.pCursor, &v);
drhc22284f2014-10-13 16:02:20 +00004632 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */
drh5e00f6c2001-09-13 13:46:56 +00004633 }
drh4c583122008-01-04 22:01:03 +00004634 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004635 break;
4636}
4637
drh9cbf3422008-01-17 16:22:13 +00004638/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004639**
4640** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004641** that occur while the cursor is on the null row will always
4642** write a NULL.
drh17f71932002-02-21 12:01:27 +00004643*/
drh9cbf3422008-01-17 16:22:13 +00004644case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004645 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004646
drh653b82a2009-06-22 11:10:47 +00004647 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4648 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004649 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004650 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004651 pC->cacheStatus = CACHE_STALE;
drhc960dcb2015-11-20 19:22:01 +00004652 if( pC->eCurType==CURTYPE_BTREE ){
4653 assert( pC->uc.pCursor!=0 );
4654 sqlite3BtreeClearCursor(pC->uc.pCursor);
danielk1977be51a652008-10-08 17:58:48 +00004655 }
drh17f71932002-02-21 12:01:27 +00004656 break;
4657}
4658
danb18e60b2015-04-01 16:18:00 +00004659/* Opcode: Last P1 P2 P3 * *
drh9562b552002-02-19 15:00:07 +00004660**
drh8af3f772014-07-25 18:01:06 +00004661** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004662** will refer to the last entry in the database table or index.
4663** If the table or index is empty and P2>0, then jump immediately to P2.
4664** If P2 is 0 or if the table or index is not empty, fall through
4665** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004666**
4667** This opcode leaves the cursor configured to move in reverse order,
4668** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004669** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004670*/
drh9cbf3422008-01-17 16:22:13 +00004671case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004672 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004673 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004674 int res;
drh9562b552002-02-19 15:00:07 +00004675
drh653b82a2009-06-22 11:10:47 +00004676 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4677 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004678 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004679 assert( pC->eCurType==CURTYPE_BTREE );
4680 pCrsr = pC->uc.pCursor;
drh7abc5402011-10-22 21:00:46 +00004681 res = 0;
drh3da046d2013-11-11 03:24:11 +00004682 assert( pCrsr!=0 );
4683 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004684 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004685 pC->deferredMoveto = 0;
4686 pC->cacheStatus = CACHE_STALE;
danb18e60b2015-04-01 16:18:00 +00004687 pC->seekResult = pOp->p3;
drh8af3f772014-07-25 18:01:06 +00004688#ifdef SQLITE_DEBUG
4689 pC->seekOp = OP_Last;
4690#endif
drh9467abf2016-02-17 18:44:11 +00004691 if( rc ) goto abort_due_to_error;
drh688852a2014-02-17 22:40:43 +00004692 if( pOp->p2>0 ){
4693 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004694 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00004695 }
4696 break;
4697}
4698
drh0342b1f2005-09-01 03:07:44 +00004699
drh9cbf3422008-01-17 16:22:13 +00004700/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004701**
4702** This opcode does exactly the same thing as OP_Rewind except that
4703** it increments an undocumented global variable used for testing.
4704**
4705** Sorting is accomplished by writing records into a sorting index,
4706** then rewinding that index and playing it back from beginning to
4707** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4708** rewinding so that the global variable will be incremented and
4709** regression tests can determine whether or not the optimizer is
4710** correctly optimizing out sorts.
4711*/
drhc6aff302011-09-01 15:32:47 +00004712case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004713case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004714#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004715 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004716 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004717#endif
drh9b47ee32013-08-20 03:13:51 +00004718 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004719 /* Fall through into OP_Rewind */
4720}
drh9cbf3422008-01-17 16:22:13 +00004721/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004722**
drhf0863fe2005-06-12 21:35:51 +00004723** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004724** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00004725** If the table or index is empty, jump immediately to P2.
4726** If the table or index is not empty, fall through to the following
4727** instruction.
drh8af3f772014-07-25 18:01:06 +00004728**
4729** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004730** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004731** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004732*/
drh9cbf3422008-01-17 16:22:13 +00004733case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004734 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004735 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004736 int res;
drh5e00f6c2001-09-13 13:46:56 +00004737
drh653b82a2009-06-22 11:10:47 +00004738 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4739 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004740 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004741 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004742 res = 1;
drh8af3f772014-07-25 18:01:06 +00004743#ifdef SQLITE_DEBUG
4744 pC->seekOp = OP_Rewind;
4745#endif
dan689ab892011-08-12 15:02:00 +00004746 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004747 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004748 }else{
drhc960dcb2015-11-20 19:22:01 +00004749 assert( pC->eCurType==CURTYPE_BTREE );
4750 pCrsr = pC->uc.pCursor;
dana205a482011-08-27 18:48:57 +00004751 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004752 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004753 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004754 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00004755 }
drh9467abf2016-02-17 18:44:11 +00004756 if( rc ) goto abort_due_to_error;
drh9c1905f2008-12-10 22:32:56 +00004757 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004758 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004759 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004760 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004761 break;
4762}
4763
drh0fd61352014-02-07 02:29:45 +00004764/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004765**
4766** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004767** table or index. If there are no more key/value pairs then fall through
4768** to the following instruction. But if the cursor advance was successful,
4769** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004770**
drh5dad9a32014-07-25 18:37:42 +00004771** The Next opcode is only valid following an SeekGT, SeekGE, or
4772** OP_Rewind opcode used to position the cursor. Next is not allowed
4773** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00004774**
drhf93cd942013-11-21 03:12:25 +00004775** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4776** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004777**
drhe39a7322014-02-03 14:04:11 +00004778** The P3 value is a hint to the btree implementation. If P3==1, that
4779** means P1 is an SQL index and that this instruction could have been
4780** omitted if that index had been unique. P3 is usually 0. P3 is
4781** always either 0 or 1.
4782**
dana205a482011-08-27 18:48:57 +00004783** P4 is always of type P4_ADVANCE. The function pointer points to
4784** sqlite3BtreeNext().
4785**
drhafc266a2010-03-31 17:47:44 +00004786** If P5 is positive and the jump is taken, then event counter
4787** number P5-1 in the prepared statement is incremented.
4788**
drhf93cd942013-11-21 03:12:25 +00004789** See also: Prev, NextIfOpen
4790*/
drh0fd61352014-02-07 02:29:45 +00004791/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004792**
drh5dad9a32014-07-25 18:37:42 +00004793** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004794** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004795*/
drh0fd61352014-02-07 02:29:45 +00004796/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004797**
4798** Back up cursor P1 so that it points to the previous key/data pair in its
4799** table or index. If there is no previous key/value pairs then fall through
4800** to the following instruction. But if the cursor backup was successful,
4801** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004802**
drh8af3f772014-07-25 18:01:06 +00004803**
drh5dad9a32014-07-25 18:37:42 +00004804** The Prev opcode is only valid following an SeekLT, SeekLE, or
4805** OP_Last opcode used to position the cursor. Prev is not allowed
4806** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00004807**
drhf93cd942013-11-21 03:12:25 +00004808** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4809** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004810**
drhe39a7322014-02-03 14:04:11 +00004811** The P3 value is a hint to the btree implementation. If P3==1, that
4812** means P1 is an SQL index and that this instruction could have been
4813** omitted if that index had been unique. P3 is usually 0. P3 is
4814** always either 0 or 1.
4815**
dana205a482011-08-27 18:48:57 +00004816** P4 is always of type P4_ADVANCE. The function pointer points to
4817** sqlite3BtreePrevious().
4818**
drhafc266a2010-03-31 17:47:44 +00004819** If P5 is positive and the jump is taken, then event counter
4820** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004821*/
drh0fd61352014-02-07 02:29:45 +00004822/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004823**
drh5dad9a32014-07-25 18:37:42 +00004824** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004825** open it behaves a no-op.
4826*/
4827case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004828 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004829 int res;
drh8721ce42001-11-07 14:22:00 +00004830
drhf93cd942013-11-21 03:12:25 +00004831 pC = p->apCsr[pOp->p1];
4832 assert( isSorter(pC) );
drh323913c2014-03-23 16:29:23 +00004833 res = 0;
drhf93cd942013-11-21 03:12:25 +00004834 rc = sqlite3VdbeSorterNext(db, pC, &res);
4835 goto next_tail;
4836case OP_PrevIfOpen: /* jump */
4837case OP_NextIfOpen: /* jump */
4838 if( p->apCsr[pOp->p1]==0 ) break;
4839 /* Fall through */
4840case OP_Prev: /* jump */
4841case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004842 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004843 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004844 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00004845 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00004846 assert( pC!=0 );
4847 assert( pC->deferredMoveto==0 );
drhc960dcb2015-11-20 19:22:01 +00004848 assert( pC->eCurType==CURTYPE_BTREE );
drhe39a7322014-02-03 14:04:11 +00004849 assert( res==0 || (res==1 && pC->isTable==0) );
4850 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00004851 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4852 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4853 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4854 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00004855
4856 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
4857 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
4858 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
4859 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00004860 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00004861 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
4862 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
4863 || pC->seekOp==OP_Last );
4864
drhc960dcb2015-11-20 19:22:01 +00004865 rc = pOp->p4.xAdvance(pC->uc.pCursor, &res);
drhf93cd942013-11-21 03:12:25 +00004866next_tail:
drha3460582008-07-11 21:02:53 +00004867 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004868 VdbeBranchTaken(res==0,2);
drh9467abf2016-02-17 18:44:11 +00004869 if( rc ) goto abort_due_to_error;
drha3460582008-07-11 21:02:53 +00004870 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004871 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00004872 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004873#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004874 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004875#endif
drhf56fa462015-04-13 21:39:54 +00004876 goto jump_to_p2_and_check_for_interrupt;
drhf93cd942013-11-21 03:12:25 +00004877 }else{
4878 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004879 }
drh49afe3a2013-07-10 03:05:14 +00004880 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004881}
4882
danielk1977de630352009-05-04 11:42:29 +00004883/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004884** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004885**
drhef8662b2011-06-20 21:47:58 +00004886** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004887** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004888** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004889**
drhaa9b8962008-01-08 02:57:55 +00004890** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004891** insert is likely to be an append.
4892**
mistachkin21a919f2014-02-07 03:28:02 +00004893** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4894** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4895** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00004896**
mistachkin21a919f2014-02-07 03:28:02 +00004897** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
4898** just done a seek to the spot where the new entry is to be inserted.
4899** This flag avoids doing an extra seek.
drh0fd61352014-02-07 02:29:45 +00004900**
drhf0863fe2005-06-12 21:35:51 +00004901** This instruction only works for indices. The equivalent instruction
4902** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004903*/
drhca892a72011-09-03 00:17:51 +00004904case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004905case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004906 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004907 int nKey;
4908 const char *zKey;
4909
drh653b82a2009-06-22 11:10:47 +00004910 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4911 pC = p->apCsr[pOp->p1];
4912 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004913 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004914 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004915 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00004916 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhc960dcb2015-11-20 19:22:01 +00004917 assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
drh3da046d2013-11-11 03:24:11 +00004918 assert( pC->isTable==0 );
4919 rc = ExpandBlob(pIn2);
drh9467abf2016-02-17 18:44:11 +00004920 if( rc ) goto abort_due_to_error;
4921 if( pOp->opcode==OP_SorterInsert ){
4922 rc = sqlite3VdbeSorterWrite(pC, pIn2);
4923 }else{
4924 nKey = pIn2->n;
4925 zKey = pIn2->z;
4926 rc = sqlite3BtreeInsert(pC->uc.pCursor, zKey, nKey, "", 0, 0, pOp->p3,
4927 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4928 );
4929 assert( pC->deferredMoveto==0 );
4930 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004931 }
drh9467abf2016-02-17 18:44:11 +00004932 if( rc) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004933 break;
4934}
4935
drh4308e342013-11-11 16:55:52 +00004936/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004937** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004938**
drhe14006d2008-03-25 17:23:32 +00004939** The content of P3 registers starting at register P2 form
4940** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004941** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004942*/
drhe14006d2008-03-25 17:23:32 +00004943case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004944 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004945 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004946 int res;
4947 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004948
drhe14006d2008-03-25 17:23:32 +00004949 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004950 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004951 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4952 pC = p->apCsr[pOp->p1];
4953 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00004954 assert( pC->eCurType==CURTYPE_BTREE );
4955 pCrsr = pC->uc.pCursor;
drh3da046d2013-11-11 03:24:11 +00004956 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004957 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004958 r.pKeyInfo = pC->pKeyInfo;
4959 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00004960 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004961 r.aMem = &aMem[pOp->p2];
drh3da046d2013-11-11 03:24:11 +00004962 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
drh9467abf2016-02-17 18:44:11 +00004963 if( rc ) goto abort_due_to_error;
4964 if( res==0 ){
dane61bbf42016-01-28 17:06:17 +00004965 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
drh9467abf2016-02-17 18:44:11 +00004966 if( rc ) goto abort_due_to_error;
drh5e00f6c2001-09-13 13:46:56 +00004967 }
drh3da046d2013-11-11 03:24:11 +00004968 assert( pC->deferredMoveto==0 );
4969 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004970 break;
4971}
4972
drh784c1b92016-01-30 16:59:56 +00004973/* Opcode: Seek P1 * P3 P4 *
4974** Synopsis: Move P3 to P1.rowid
4975**
4976** P1 is an open index cursor and P3 is a cursor on the corresponding
4977** table. This opcode does a deferred seek of the P3 table cursor
4978** to the row that corresponds to the current row of P1.
4979**
4980** This is a deferred seek. Nothing actually happens until
4981** the cursor is used to read a record. That way, if no reads
4982** occur, no unnecessary I/O happens.
4983**
4984** P4 may be an array of integers (type P4_INTARRAY) containing
drh19d720d2016-02-03 19:52:06 +00004985** one entry for each column in the P3 table. If array entry a(i)
4986** is non-zero, then reading column a(i)-1 from cursor P3 is
drh784c1b92016-01-30 16:59:56 +00004987** equivalent to performing the deferred seek and then reading column i
4988** from P1. This information is stored in P3 and used to redirect
4989** reads against P3 over to P1, thus possibly avoiding the need to
4990** seek and read cursor P3.
4991*/
drh2133d822008-01-03 18:44:59 +00004992/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004993** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004994**
drh2133d822008-01-03 18:44:59 +00004995** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004996** the end of the index key pointed to by cursor P1. This integer should be
4997** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004998**
drh9437bd22009-02-01 00:29:56 +00004999** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00005000*/
drh784c1b92016-01-30 16:59:56 +00005001case OP_Seek:
drh27a348c2015-04-13 19:14:06 +00005002case OP_IdxRowid: { /* out2 */
drh784c1b92016-01-30 16:59:56 +00005003 VdbeCursor *pC; /* The P1 index cursor */
5004 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
5005 i64 rowid; /* Rowid that P1 current points to */
drh8721ce42001-11-07 14:22:00 +00005006
drh653b82a2009-06-22 11:10:47 +00005007 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5008 pC = p->apCsr[pOp->p1];
5009 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005010 assert( pC->eCurType==CURTYPE_BTREE );
drh784c1b92016-01-30 16:59:56 +00005011 assert( pC->uc.pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00005012 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00005013 assert( pC->deferredMoveto==0 );
drh784c1b92016-01-30 16:59:56 +00005014 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
5015
5016 /* The IdxRowid and Seek opcodes are combined because of the commonality
5017 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
5018 rc = sqlite3VdbeCursorRestore(pC);
drhc22284f2014-10-13 16:02:20 +00005019
5020 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
drh784c1b92016-01-30 16:59:56 +00005021 ** out from under the cursor. That will never happens for an IdxRowid
5022 ** or Seek opcode */
drhc22284f2014-10-13 16:02:20 +00005023 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
5024
drh3da046d2013-11-11 03:24:11 +00005025 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00005026 rowid = 0; /* Not needed. Only used to silence a warning. */
drh784c1b92016-01-30 16:59:56 +00005027 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
drh3da046d2013-11-11 03:24:11 +00005028 if( rc!=SQLITE_OK ){
5029 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00005030 }
drh784c1b92016-01-30 16:59:56 +00005031 if( pOp->opcode==OP_Seek ){
5032 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
5033 pTabCur = p->apCsr[pOp->p3];
5034 assert( pTabCur!=0 );
5035 assert( pTabCur->eCurType==CURTYPE_BTREE );
5036 assert( pTabCur->uc.pCursor!=0 );
5037 assert( pTabCur->isTable );
5038 pTabCur->nullRow = 0;
5039 pTabCur->movetoTarget = rowid;
5040 pTabCur->deferredMoveto = 1;
5041 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
5042 pTabCur->aAltMap = pOp->p4.ai;
5043 pTabCur->pAltCursor = pC;
5044 }else{
5045 pOut = out2Prerelease(p, pOp);
5046 pOut->u.i = rowid;
5047 pOut->flags = MEM_Int;
5048 }
5049 }else{
5050 assert( pOp->opcode==OP_IdxRowid );
5051 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
drh8721ce42001-11-07 14:22:00 +00005052 }
5053 break;
5054}
5055
danielk197761dd5832008-04-18 11:31:12 +00005056/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005057** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00005058**
danielk197761dd5832008-04-18 11:31:12 +00005059** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005060** key that omits the PRIMARY KEY. Compare this key value against the index
5061** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5062** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00005063**
danielk197761dd5832008-04-18 11:31:12 +00005064** If the P1 index entry is greater than or equal to the key value
5065** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00005066*/
5067/* Opcode: IdxGT P1 P2 P3 P4 P5
5068** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00005069**
drh4a1d3652014-02-14 15:13:36 +00005070** The P4 register values beginning with P3 form an unpacked index
5071** key that omits the PRIMARY KEY. Compare this key value against the index
5072** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
5073** fields at the end.
5074**
5075** If the P1 index entry is greater than the key value
5076** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00005077*/
drh3bb9b932010-08-06 02:10:00 +00005078/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005079** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00005080**
danielk197761dd5832008-04-18 11:31:12 +00005081** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00005082** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5083** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5084** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00005085**
danielk197761dd5832008-04-18 11:31:12 +00005086** If the P1 index entry is less than the key value then jump to P2.
5087** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005088*/
drh4a1d3652014-02-14 15:13:36 +00005089/* Opcode: IdxLE P1 P2 P3 P4 P5
5090** Synopsis: key=r[P3@P4]
5091**
5092** The P4 register values beginning with P3 form an unpacked index
5093** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5094** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5095** ROWID on the P1 index.
5096**
5097** If the P1 index entry is less than or equal to the key value then jump
5098** to P2. Otherwise fall through to the next instruction.
5099*/
5100case OP_IdxLE: /* jump */
5101case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005102case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005103case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005104 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005105 int res;
5106 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005107
drh653b82a2009-06-22 11:10:47 +00005108 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5109 pC = p->apCsr[pOp->p1];
5110 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005111 assert( pC->isOrdered );
drhc960dcb2015-11-20 19:22:01 +00005112 assert( pC->eCurType==CURTYPE_BTREE );
5113 assert( pC->uc.pCursor!=0);
drh3da046d2013-11-11 03:24:11 +00005114 assert( pC->deferredMoveto==0 );
5115 assert( pOp->p5==0 || pOp->p5==1 );
5116 assert( pOp->p4type==P4_INT32 );
5117 r.pKeyInfo = pC->pKeyInfo;
5118 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005119 if( pOp->opcode<OP_IdxLT ){
5120 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005121 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005122 }else{
drh4a1d3652014-02-14 15:13:36 +00005123 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005124 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005125 }
5126 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005127#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00005128 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005129#endif
drh2dc06482013-12-11 00:59:10 +00005130 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005131 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005132 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5133 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5134 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005135 res = -res;
5136 }else{
drh4a1d3652014-02-14 15:13:36 +00005137 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005138 res++;
5139 }
drh688852a2014-02-17 22:40:43 +00005140 VdbeBranchTaken(res>0,2);
drh9467abf2016-02-17 18:44:11 +00005141 if( rc ) goto abort_due_to_error;
drhf56fa462015-04-13 21:39:54 +00005142 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005143 break;
5144}
5145
drh98757152008-01-09 23:04:12 +00005146/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005147**
5148** Delete an entire database table or index whose root page in the database
5149** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005150**
drh98757152008-01-09 23:04:12 +00005151** The table being destroyed is in the main database file if P3==0. If
5152** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005153** that is used to store tables create using CREATE TEMPORARY TABLE.
5154**
drh205f48e2004-11-05 00:43:11 +00005155** If AUTOVACUUM is enabled then it is possible that another root page
5156** might be moved into the newly deleted root page in order to keep all
5157** root pages contiguous at the beginning of the database. The former
5158** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00005159** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00005160** movement was required (because the table being dropped was already
5161** the last one in the database) then a zero is stored in register P2.
5162** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00005163**
drhb19a2bc2001-09-16 00:13:26 +00005164** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005165*/
drh27a348c2015-04-13 19:14:06 +00005166case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005167 int iMoved;
drh856c1032009-06-02 15:21:42 +00005168 int iDb;
drh3a949872012-09-18 13:20:13 +00005169
drh9e92a472013-06-27 17:40:30 +00005170 assert( p->readOnly==0 );
drh055f2982016-01-15 15:06:41 +00005171 assert( pOp->p1>1 );
drh27a348c2015-04-13 19:14:06 +00005172 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005173 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005174 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005175 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005176 p->errorAction = OE_Abort;
drh9467abf2016-02-17 18:44:11 +00005177 goto abort_due_to_error;
danielk1977e6efa742004-11-10 11:55:10 +00005178 }else{
drh856c1032009-06-02 15:21:42 +00005179 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005180 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005181 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005182 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005183 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005184 pOut->u.i = iMoved;
drh9467abf2016-02-17 18:44:11 +00005185 if( rc ) goto abort_due_to_error;
drh3765df42006-06-28 18:18:09 +00005186#ifndef SQLITE_OMIT_AUTOVACUUM
drh9467abf2016-02-17 18:44:11 +00005187 if( iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005188 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5189 /* All OP_Destroy operations occur on the same btree */
5190 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5191 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005192 }
drh3765df42006-06-28 18:18:09 +00005193#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005194 }
drh5e00f6c2001-09-13 13:46:56 +00005195 break;
5196}
5197
danielk1977c7af4842008-10-27 13:59:33 +00005198/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005199**
5200** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005201** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005202** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005203**
drhf57b3392001-10-08 13:22:32 +00005204** The table being clear is in the main database file if P2==0. If
5205** P2==1 then the table to be clear is in the auxiliary database file
5206** that is used to store tables create using CREATE TEMPORARY TABLE.
5207**
shanebe217792009-03-05 04:20:31 +00005208** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005209** intkey table (an SQL table, not an index). In this case the row change
5210** count is incremented by the number of rows in the table being cleared.
5211** If P3 is greater than zero, then the value stored in register P3 is
5212** also incremented by the number of rows in the table being cleared.
5213**
drhb19a2bc2001-09-16 00:13:26 +00005214** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005215*/
drh9cbf3422008-01-17 16:22:13 +00005216case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005217 int nChange;
5218
5219 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005220 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005221 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005222 rc = sqlite3BtreeClearTable(
5223 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5224 );
5225 if( pOp->p3 ){
5226 p->nChange += nChange;
5227 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005228 assert( memIsValid(&aMem[pOp->p3]) );
5229 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005230 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005231 }
5232 }
drh9467abf2016-02-17 18:44:11 +00005233 if( rc ) goto abort_due_to_error;
drh5edc3122001-09-13 21:53:09 +00005234 break;
5235}
5236
drh65ea12c2014-03-19 17:41:36 +00005237/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005238**
drh65ea12c2014-03-19 17:41:36 +00005239** Delete all contents from the ephemeral table or sorter
5240** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005241**
drh65ea12c2014-03-19 17:41:36 +00005242** This opcode only works for cursors used for sorting and
5243** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005244*/
drh65ea12c2014-03-19 17:41:36 +00005245case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005246 VdbeCursor *pC;
5247
5248 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5249 pC = p->apCsr[pOp->p1];
5250 assert( pC!=0 );
drhc960dcb2015-11-20 19:22:01 +00005251 if( isSorter(pC) ){
5252 sqlite3VdbeSorterReset(db, pC->uc.pSorter);
drh65ea12c2014-03-19 17:41:36 +00005253 }else{
drhc960dcb2015-11-20 19:22:01 +00005254 assert( pC->eCurType==CURTYPE_BTREE );
drh65ea12c2014-03-19 17:41:36 +00005255 assert( pC->isEphemeral );
drhc960dcb2015-11-20 19:22:01 +00005256 rc = sqlite3BtreeClearTableOfCursor(pC->uc.pCursor);
drh9467abf2016-02-17 18:44:11 +00005257 if( rc ) goto abort_due_to_error;
drh65ea12c2014-03-19 17:41:36 +00005258 }
drh079a3072014-03-19 14:10:55 +00005259 break;
5260}
5261
drh4c583122008-01-04 22:01:03 +00005262/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005263** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00005264**
drh4c583122008-01-04 22:01:03 +00005265** Allocate a new table in the main database file if P1==0 or in the
5266** auxiliary database file if P1==1 or in an attached database if
5267** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005268** register P2
drh5b2fd562001-09-13 15:21:31 +00005269**
drhc6b52df2002-01-04 03:09:29 +00005270** The difference between a table and an index is this: A table must
5271** have a 4-byte integer key and can have arbitrary data. An index
5272** has an arbitrary key but no data.
5273**
drhb19a2bc2001-09-16 00:13:26 +00005274** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00005275*/
drh4c583122008-01-04 22:01:03 +00005276/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005277** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00005278**
drh4c583122008-01-04 22:01:03 +00005279** Allocate a new index in the main database file if P1==0 or in the
5280** auxiliary database file if P1==1 or in an attached database if
5281** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005282** register P2.
drhf57b3392001-10-08 13:22:32 +00005283**
drhc6b52df2002-01-04 03:09:29 +00005284** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00005285*/
drh27a348c2015-04-13 19:14:06 +00005286case OP_CreateIndex: /* out2 */
5287case OP_CreateTable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005288 int pgno;
drhf328bc82004-05-10 23:29:49 +00005289 int flags;
drh234c39d2004-07-24 03:30:47 +00005290 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005291
drh27a348c2015-04-13 19:14:06 +00005292 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005293 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00005294 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005295 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005296 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005297 pDb = &db->aDb[pOp->p1];
5298 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00005299 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00005300 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00005301 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00005302 }else{
drhd4187c72010-08-30 22:15:45 +00005303 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00005304 }
drh234c39d2004-07-24 03:30:47 +00005305 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh9467abf2016-02-17 18:44:11 +00005306 if( rc ) goto abort_due_to_error;
drh88a003e2008-12-11 16:17:03 +00005307 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005308 break;
5309}
5310
drh22645842011-03-24 01:34:03 +00005311/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005312**
5313** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005314** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005315**
5316** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005317** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005318*/
drh9cbf3422008-01-17 16:22:13 +00005319case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005320 int iDb;
5321 const char *zMaster;
5322 char *zSql;
5323 InitData initData;
5324
drhbdaec522011-04-04 00:14:43 +00005325 /* Any prepared statement that invokes this opcode will hold mutexes
5326 ** on every btree. This is a prerequisite for invoking
5327 ** sqlite3InitCallback().
5328 */
5329#ifdef SQLITE_DEBUG
5330 for(iDb=0; iDb<db->nDb; iDb++){
5331 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5332 }
5333#endif
drhbdaec522011-04-04 00:14:43 +00005334
drh856c1032009-06-02 15:21:42 +00005335 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005336 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005337 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005338 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00005339 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00005340 initData.db = db;
5341 initData.iDb = pOp->p1;
5342 initData.pzErrMsg = &p->zErrMsg;
5343 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005344 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00005345 db->aDb[iDb].zName, zMaster, pOp->p4.z);
5346 if( zSql==0 ){
mistachkinfad30392016-02-13 23:43:46 +00005347 rc = SQLITE_NOMEM_BKPT;
danielk1977a8bbef82009-03-23 17:11:26 +00005348 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005349 assert( db->init.busy==0 );
5350 db->init.busy = 1;
5351 initData.rc = SQLITE_OK;
5352 assert( !db->mallocFailed );
5353 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5354 if( rc==SQLITE_OK ) rc = initData.rc;
5355 sqlite3DbFree(db, zSql);
5356 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005357 }
drh3c23a882007-01-09 14:01:13 +00005358 }
drh9467abf2016-02-17 18:44:11 +00005359 if( rc ){
5360 sqlite3ResetAllSchemasOfConnection(db);
5361 if( rc==SQLITE_NOMEM ){
5362 goto no_mem;
5363 }
5364 goto abort_due_to_error;
danielk1977261919c2005-12-06 12:52:59 +00005365 }
drh234c39d2004-07-24 03:30:47 +00005366 break;
5367}
5368
drh8bfdf722009-06-19 14:06:03 +00005369#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005370/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005371**
5372** Read the sqlite_stat1 table for database P1 and load the content
5373** of that table into the internal index hash table. This will cause
5374** the analysis to be used when preparing all subsequent queries.
5375*/
drh9cbf3422008-01-17 16:22:13 +00005376case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005377 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5378 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh9467abf2016-02-17 18:44:11 +00005379 if( rc ) goto abort_due_to_error;
drh497e4462005-07-23 03:18:40 +00005380 break;
5381}
drh8bfdf722009-06-19 14:06:03 +00005382#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005383
drh98757152008-01-09 23:04:12 +00005384/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005385**
5386** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005387** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005388** is dropped from disk (using the Destroy opcode) in order to keep
5389** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005390** schema consistent with what is on disk.
5391*/
drh9cbf3422008-01-17 16:22:13 +00005392case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005393 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005394 break;
5395}
5396
drh98757152008-01-09 23:04:12 +00005397/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005398**
5399** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005400** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005401** is dropped from disk (using the Destroy opcode)
5402** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005403** schema consistent with what is on disk.
5404*/
drh9cbf3422008-01-17 16:22:13 +00005405case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005406 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005407 break;
5408}
5409
drh98757152008-01-09 23:04:12 +00005410/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005411**
5412** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005413** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005414** is dropped from disk (using the Destroy opcode) in order to keep
5415** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005416** schema consistent with what is on disk.
5417*/
drh9cbf3422008-01-17 16:22:13 +00005418case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005419 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005420 break;
5421}
5422
drh234c39d2004-07-24 03:30:47 +00005423
drhb7f91642004-10-31 02:22:47 +00005424#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005425/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005426**
drh98757152008-01-09 23:04:12 +00005427** Do an analysis of the currently open database. Store in
5428** register P1 the text of an error message describing any problems.
5429** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005430**
drh98757152008-01-09 23:04:12 +00005431** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005432** At most reg(P3) errors will be reported.
5433** In other words, the analysis stops as soon as reg(P1) errors are
5434** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005435**
drh79069752004-05-22 21:30:40 +00005436** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005437** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005438** total.
drh21504322002-06-25 13:16:02 +00005439**
drh98757152008-01-09 23:04:12 +00005440** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005441** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005442**
drh1dcdbc02007-01-27 02:24:54 +00005443** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005444*/
drhaaab5722002-02-19 13:39:21 +00005445case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005446 int nRoot; /* Number of tables to check. (Number of root pages.) */
5447 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5448 int j; /* Loop counter */
5449 int nErr; /* Number of errors reported */
5450 char *z; /* Text of the error report */
5451 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005452
drh1713afb2013-06-28 01:24:57 +00005453 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005454 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005455 assert( nRoot>0 );
drh575fad62016-02-05 13:38:36 +00005456 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005457 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005458 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005459 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005460 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005461 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005462 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005463 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005464 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005465 }
5466 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005467 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005468 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh98757152008-01-09 23:04:12 +00005469 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005470 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005471 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005472 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005473 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005474 if( nErr==0 ){
5475 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005476 }else if( z==0 ){
5477 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005478 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005479 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005480 }
drhb7654112008-01-12 12:48:07 +00005481 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005482 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005483 break;
5484}
drhb7f91642004-10-31 02:22:47 +00005485#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005486
drh3d4501e2008-12-04 20:40:10 +00005487/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005488** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005489**
drh3d4501e2008-12-04 20:40:10 +00005490** Insert the integer value held by register P2 into a boolean index
5491** held in register P1.
5492**
5493** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005494*/
drh93952eb2009-11-13 19:43:43 +00005495case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005496 pIn1 = &aMem[pOp->p1];
5497 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005498 assert( (pIn2->flags & MEM_Int)!=0 );
5499 if( (pIn1->flags & MEM_RowSet)==0 ){
5500 sqlite3VdbeMemSetRowSet(pIn1);
5501 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005502 }
drh93952eb2009-11-13 19:43:43 +00005503 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005504 break;
5505}
5506
5507/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005508** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005509**
5510** Extract the smallest value from boolean index P1 and put that value into
5511** register P3. Or, if boolean index P1 is initially empty, leave P3
5512** unchanged and jump to instruction P2.
5513*/
drh93952eb2009-11-13 19:43:43 +00005514case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005515 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005516
drh3c657212009-11-17 23:59:58 +00005517 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005518 if( (pIn1->flags & MEM_RowSet)==0
5519 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005520 ){
5521 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005522 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005523 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005524 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005525 }else{
5526 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005527 VdbeBranchTaken(0,2);
drh3c657212009-11-17 23:59:58 +00005528 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005529 }
drh49afe3a2013-07-10 03:05:14 +00005530 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005531}
5532
drh1b26c7c2009-04-22 02:15:47 +00005533/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005534** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005535**
drhade97602009-04-21 15:05:18 +00005536** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005537** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005538** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005539** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005540** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005541**
drh1b26c7c2009-04-22 02:15:47 +00005542** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005543** of integers, where each set contains no duplicates. Each set
5544** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005545** must have P4==0, the final set P4=-1. P4 must be either -1 or
5546** non-negative. For non-negative values of P4 only the lower 4
5547** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005548**
5549** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005550** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005551** (b) when P4==-1 there is no need to insert the value, as it will
5552** never be tested for, and (c) when a value that is part of set X is
5553** inserted, there is no need to search to see if the same value was
5554** previously inserted as part of set X (only if it was previously
5555** inserted as part of some other set).
5556*/
drh1b26c7c2009-04-22 02:15:47 +00005557case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005558 int iSet;
5559 int exists;
5560
drh3c657212009-11-17 23:59:58 +00005561 pIn1 = &aMem[pOp->p1];
5562 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005563 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005564 assert( pIn3->flags&MEM_Int );
5565
drh1b26c7c2009-04-22 02:15:47 +00005566 /* If there is anything other than a rowset object in memory cell P1,
5567 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005568 */
drh733bf1b2009-04-22 00:47:00 +00005569 if( (pIn1->flags & MEM_RowSet)==0 ){
5570 sqlite3VdbeMemSetRowSet(pIn1);
5571 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005572 }
5573
5574 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005575 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005576 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005577 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005578 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005579 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005580 }
5581 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005582 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005583 }
5584 break;
5585}
5586
drh5e00f6c2001-09-13 13:46:56 +00005587
danielk197793758c82005-01-21 08:13:14 +00005588#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005589
drh0fd61352014-02-07 02:29:45 +00005590/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005591**
dan76d462e2009-08-30 11:42:51 +00005592** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005593**
dan76d462e2009-08-30 11:42:51 +00005594** P1 contains the address of the memory cell that contains the first memory
5595** cell in an array of values used as arguments to the sub-program. P2
5596** contains the address to jump to if the sub-program throws an IGNORE
5597** exception using the RAISE() function. Register P3 contains the address
5598** of a memory cell in this (the parent) VM that is used to allocate the
5599** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005600**
5601** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005602**
5603** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005604*/
dan76d462e2009-08-30 11:42:51 +00005605case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005606 int nMem; /* Number of memory registers for sub-program */
5607 int nByte; /* Bytes of runtime space required for sub-program */
5608 Mem *pRt; /* Register to allocate runtime space */
5609 Mem *pMem; /* Used to iterate through memory cells */
5610 Mem *pEnd; /* Last memory cell in new array */
5611 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5612 SubProgram *pProgram; /* Sub-program to execute */
5613 void *t; /* Token identifying trigger */
5614
5615 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005616 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005617 assert( pProgram->nOp>0 );
5618
dan1da40a32009-09-19 17:00:31 +00005619 /* If the p5 flag is clear, then recursive invocation of triggers is
5620 ** disabled for backwards compatibility (p5 is set if this sub-program
5621 ** is really a trigger, not a foreign key action, and the flag set
5622 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005623 **
5624 ** It is recursive invocation of triggers, at the SQL level, that is
5625 ** disabled. In some cases a single trigger may generate more than one
5626 ** SubProgram (if the trigger may be executed with more than one different
5627 ** ON CONFLICT algorithm). SubProgram structures associated with a
5628 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005629 ** variable. */
5630 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005631 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005632 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5633 if( pFrame ) break;
5634 }
5635
danf5894502009-10-07 18:41:19 +00005636 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005637 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00005638 sqlite3VdbeError(p, "too many levels of trigger recursion");
drh9467abf2016-02-17 18:44:11 +00005639 goto abort_due_to_error;
dan165921a2009-08-28 18:53:45 +00005640 }
5641
5642 /* Register pRt is used to store the memory required to save the state
5643 ** of the current program, and the memory required at runtime to execute
5644 ** the trigger program. If this trigger has been fired before, then pRt
5645 ** is already allocated. Otherwise, it must be initialized. */
5646 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005647 /* SubProgram.nMem is set to the number of memory cells used by the
5648 ** program stored in SubProgram.aOp. As well as these, one memory
5649 ** cell is required for each cursor used by the program. Set local
5650 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5651 */
dan65a7cd12009-09-01 12:16:01 +00005652 nMem = pProgram->nMem + pProgram->nCsr;
5653 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005654 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005655 + pProgram->nCsr * sizeof(VdbeCursor *)
5656 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005657 pFrame = sqlite3DbMallocZero(db, nByte);
5658 if( !pFrame ){
5659 goto no_mem;
5660 }
5661 sqlite3VdbeMemRelease(pRt);
5662 pRt->flags = MEM_Frame;
5663 pRt->u.pFrame = pFrame;
5664
5665 pFrame->v = p;
5666 pFrame->nChildMem = nMem;
5667 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00005668 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00005669 pFrame->aMem = p->aMem;
5670 pFrame->nMem = p->nMem;
5671 pFrame->apCsr = p->apCsr;
5672 pFrame->nCursor = p->nCursor;
5673 pFrame->aOp = p->aOp;
5674 pFrame->nOp = p->nOp;
5675 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005676 pFrame->aOnceFlag = p->aOnceFlag;
5677 pFrame->nOnceFlag = p->nOnceFlag;
dane2f771b2014-11-03 15:33:17 +00005678#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005679 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00005680#endif
dan165921a2009-08-28 18:53:45 +00005681
5682 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5683 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005684 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005685 pMem->db = db;
5686 }
5687 }else{
5688 pFrame = pRt->u.pFrame;
5689 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5690 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00005691 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00005692 }
5693
5694 p->nFrame++;
5695 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005696 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005697 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00005698 pFrame->nDbChange = p->db->nChange;
dan2832ad42009-08-31 15:27:27 +00005699 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005700 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005701 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005702 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005703 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005704 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005705 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005706 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005707 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5708 p->nOnceFlag = pProgram->nOnce;
dane2f771b2014-11-03 15:33:17 +00005709#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005710 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00005711#endif
drhf56fa462015-04-13 21:39:54 +00005712 pOp = &aOp[-1];
dan1d8cb212011-12-09 13:24:16 +00005713 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005714
5715 break;
5716}
5717
dan76d462e2009-08-30 11:42:51 +00005718/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005719**
dan76d462e2009-08-30 11:42:51 +00005720** This opcode is only ever present in sub-programs called via the
5721** OP_Program instruction. Copy a value currently stored in a memory
5722** cell of the calling (parent) frame to cell P2 in the current frames
5723** address space. This is used by trigger programs to access the new.*
5724** and old.* values.
dan165921a2009-08-28 18:53:45 +00005725**
dan76d462e2009-08-30 11:42:51 +00005726** The address of the cell in the parent frame is determined by adding
5727** the value of the P1 argument to the value of the P1 argument to the
5728** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005729*/
drh27a348c2015-04-13 19:14:06 +00005730case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00005731 VdbeFrame *pFrame;
5732 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00005733 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00005734 pFrame = p->pFrame;
5735 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005736 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5737 break;
5738}
5739
danielk197793758c82005-01-21 08:13:14 +00005740#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005741
dan1da40a32009-09-19 17:00:31 +00005742#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005743/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005744** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005745**
dan0ff297e2009-09-25 17:03:14 +00005746** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5747** If P1 is non-zero, the database constraint counter is incremented
5748** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005749** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005750*/
dan32b09f22009-09-23 17:29:59 +00005751case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005752 if( db->flags & SQLITE_DeferFKs ){
5753 db->nDeferredImmCons += pOp->p2;
5754 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005755 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005756 }else{
dan0ff297e2009-09-25 17:03:14 +00005757 p->nFkConstraint += pOp->p2;
5758 }
5759 break;
5760}
5761
5762/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005763** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005764**
5765** This opcode tests if a foreign key constraint-counter is currently zero.
5766** If so, jump to instruction P2. Otherwise, fall through to the next
5767** instruction.
5768**
5769** If P1 is non-zero, then the jump is taken if the database constraint-counter
5770** is zero (the one that counts deferred constraint violations). If P1 is
5771** zero, the jump is taken if the statement constraint-counter is zero
5772** (immediate foreign key constraint violations).
5773*/
5774case OP_FkIfZero: { /* jump */
5775 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00005776 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00005777 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00005778 }else{
drh688852a2014-02-17 22:40:43 +00005779 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00005780 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00005781 }
dan1da40a32009-09-19 17:00:31 +00005782 break;
5783}
5784#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5785
drh205f48e2004-11-05 00:43:11 +00005786#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005787/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005788** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005789**
dan76d462e2009-08-30 11:42:51 +00005790** P1 is a register in the root frame of this VM (the root frame is
5791** different from the current frame if this instruction is being executed
5792** within a sub-program). Set the value of register P1 to the maximum of
5793** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005794**
5795** This instruction throws an error if the memory cell is not initially
5796** an integer.
5797*/
dan76d462e2009-08-30 11:42:51 +00005798case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005799 VdbeFrame *pFrame;
5800 if( p->pFrame ){
5801 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5802 pIn1 = &pFrame->aMem[pOp->p1];
5803 }else{
drha6c2ed92009-11-14 23:22:23 +00005804 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005805 }
drhec86c722011-12-09 17:27:51 +00005806 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005807 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005808 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005809 sqlite3VdbeMemIntegerify(pIn2);
5810 if( pIn1->u.i<pIn2->u.i){
5811 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005812 }
5813 break;
5814}
5815#endif /* SQLITE_OMIT_AUTOINCREMENT */
5816
drh8b0cf382015-10-06 21:07:06 +00005817/* Opcode: IfPos P1 P2 P3 * *
5818** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005819**
drh16897072015-03-07 00:57:37 +00005820** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00005821** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00005822** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00005823**
drh16897072015-03-07 00:57:37 +00005824** If the initial value of register P1 is less than 1, then the
5825** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00005826*/
drh9cbf3422008-01-17 16:22:13 +00005827case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005828 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005829 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005830 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00005831 if( pIn1->u.i>0 ){
5832 pIn1->u.i -= pOp->p3;
5833 goto jump_to_p2;
5834 }
drhec7429a2005-10-06 16:53:14 +00005835 break;
5836}
5837
drhcc2fa4c2016-01-25 15:57:29 +00005838/* Opcode: OffsetLimit P1 P2 P3 * *
5839** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
drh15007a92006-01-08 18:10:17 +00005840**
drhcc2fa4c2016-01-25 15:57:29 +00005841** This opcode performs a commonly used computation associated with
5842** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
5843** holds the offset counter. The opcode computes the combined value
5844** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
5845** value computed is the total number of rows that will need to be
5846** visited in order to complete the query.
5847**
5848** If r[P3] is zero or negative, that means there is no OFFSET
5849** and r[P2] is set to be the value of the LIMIT, r[P1].
5850**
5851** if r[P1] is zero or negative, that means there is no LIMIT
5852** and r[P2] is set to -1.
5853**
5854** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
drh15007a92006-01-08 18:10:17 +00005855*/
drhcc2fa4c2016-01-25 15:57:29 +00005856case OP_OffsetLimit: { /* in1, out2, in3 */
drh3c657212009-11-17 23:59:58 +00005857 pIn1 = &aMem[pOp->p1];
drhcc2fa4c2016-01-25 15:57:29 +00005858 pIn3 = &aMem[pOp->p3];
5859 pOut = out2Prerelease(p, pOp);
5860 assert( pIn1->flags & MEM_Int );
5861 assert( pIn3->flags & MEM_Int );
5862 pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0);
drh15007a92006-01-08 18:10:17 +00005863 break;
5864}
5865
drh16897072015-03-07 00:57:37 +00005866/* Opcode: IfNotZero P1 P2 P3 * *
drh8b0cf382015-10-06 21:07:06 +00005867** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
drhec7429a2005-10-06 16:53:14 +00005868**
drh16897072015-03-07 00:57:37 +00005869** Register P1 must contain an integer. If the content of register P1 is
mistachkin91a3ecb2015-10-06 21:49:55 +00005870** initially nonzero, then subtract P3 from the value in register P1 and
drh8b0cf382015-10-06 21:07:06 +00005871** jump to P2. If register P1 is initially zero, leave it unchanged
5872** and fall through.
drhec7429a2005-10-06 16:53:14 +00005873*/
drh16897072015-03-07 00:57:37 +00005874case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005875 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005876 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00005877 VdbeBranchTaken(pIn1->u.i<0, 2);
5878 if( pIn1->u.i ){
drh8b0cf382015-10-06 21:07:06 +00005879 pIn1->u.i -= pOp->p3;
drhf56fa462015-04-13 21:39:54 +00005880 goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00005881 }
5882 break;
5883}
5884
drh16897072015-03-07 00:57:37 +00005885/* Opcode: DecrJumpZero P1 P2 * * *
5886** Synopsis: if (--r[P1])==0 goto P2
5887**
5888** Register P1 must hold an integer. Decrement the value in register P1
5889** then jump to P2 if the new value is exactly zero.
5890*/
5891case OP_DecrJumpZero: { /* jump, in1 */
5892 pIn1 = &aMem[pOp->p1];
5893 assert( pIn1->flags&MEM_Int );
5894 pIn1->u.i--;
drh688852a2014-02-17 22:40:43 +00005895 VdbeBranchTaken(pIn1->u.i==0, 2);
drhf56fa462015-04-13 21:39:54 +00005896 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00005897 break;
5898}
5899
drh16897072015-03-07 00:57:37 +00005900
5901/* Opcode: JumpZeroIncr P1 P2 * * *
5902** Synopsis: if (r[P1]++)==0 ) goto P2
5903**
5904** The register P1 must contain an integer. If register P1 is initially
5905** zero, then jump to P2. Increment register P1 regardless of whether or
5906** not the jump is taken.
5907*/
5908case OP_JumpZeroIncr: { /* jump, in1 */
5909 pIn1 = &aMem[pOp->p1];
5910 assert( pIn1->flags&MEM_Int );
5911 VdbeBranchTaken(pIn1->u.i==0, 2);
drhf56fa462015-04-13 21:39:54 +00005912 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00005913 break;
5914}
5915
drhe2d9e7c2015-06-26 18:47:53 +00005916/* Opcode: AggStep0 * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005917** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005918**
drh0bce8352002-02-28 00:41:10 +00005919** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005920** function has P5 arguments. P4 is a pointer to the FuncDef
drhe2d9e7c2015-06-26 18:47:53 +00005921** structure that specifies the function. Register P3 is the
5922** accumulator.
drhe5095352002-02-24 03:25:14 +00005923**
drh98757152008-01-09 23:04:12 +00005924** The P5 arguments are taken from register P2 and its
5925** successors.
drhe5095352002-02-24 03:25:14 +00005926*/
drhe2d9e7c2015-06-26 18:47:53 +00005927/* Opcode: AggStep * P2 P3 P4 P5
5928** Synopsis: accum=r[P3] step(r[P2@P5])
5929**
5930** Execute the step function for an aggregate. The
5931** function has P5 arguments. P4 is a pointer to an sqlite3_context
5932** object that is used to run the function. Register P3 is
5933** as the accumulator.
5934**
5935** The P5 arguments are taken from register P2 and its
5936** successors.
5937**
5938** This opcode is initially coded as OP_AggStep0. On first evaluation,
5939** the FuncDef stored in P4 is converted into an sqlite3_context and
5940** the opcode is changed. In this way, the initialization of the
5941** sqlite3_context only happens once, instead of on each call to the
5942** step function.
5943*/
drh9c7c9132015-06-26 18:16:52 +00005944case OP_AggStep0: {
drh856c1032009-06-02 15:21:42 +00005945 int n;
drh9c7c9132015-06-26 18:16:52 +00005946 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00005947
drh9c7c9132015-06-26 18:16:52 +00005948 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00005949 n = pOp->p5;
dan3bc9f742013-08-15 16:18:39 +00005950 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c7c9132015-06-26 18:16:52 +00005951 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
5952 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh575fad62016-02-05 13:38:36 +00005953 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
drh9c7c9132015-06-26 18:16:52 +00005954 if( pCtx==0 ) goto no_mem;
5955 pCtx->pMem = 0;
5956 pCtx->pFunc = pOp->p4.pFunc;
5957 pCtx->iOp = (int)(pOp - aOp);
5958 pCtx->pVdbe = p;
5959 pCtx->argc = n;
5960 pOp->p4type = P4_FUNCCTX;
5961 pOp->p4.pCtx = pCtx;
5962 pOp->opcode = OP_AggStep;
5963 /* Fall through into OP_AggStep */
5964}
5965case OP_AggStep: {
5966 int i;
5967 sqlite3_context *pCtx;
5968 Mem *pMem;
5969 Mem t;
5970
5971 assert( pOp->p4type==P4_FUNCCTX );
5972 pCtx = pOp->p4.pCtx;
5973 pMem = &aMem[pOp->p3];
5974
5975 /* If this function is inside of a trigger, the register array in aMem[]
5976 ** might change from one evaluation to the next. The next block of code
5977 ** checks to see if the register array has changed, and if so it
5978 ** reinitializes the relavant parts of the sqlite3_context object */
5979 if( pCtx->pMem != pMem ){
5980 pCtx->pMem = pMem;
5981 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
5982 }
5983
5984#ifdef SQLITE_DEBUG
5985 for(i=0; i<pCtx->argc; i++){
5986 assert( memIsValid(pCtx->argv[i]) );
5987 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
5988 }
5989#endif
5990
drhabfcea22005-09-06 20:36:48 +00005991 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00005992 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00005993 pCtx->pOut = &t;
5994 pCtx->fErrorOrAux = 0;
5995 pCtx->skipFlag = 0;
drh2d801512016-01-14 22:19:58 +00005996 (pCtx->pFunc->xSFunc)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
drh9c7c9132015-06-26 18:16:52 +00005997 if( pCtx->fErrorOrAux ){
5998 if( pCtx->isError ){
5999 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
6000 rc = pCtx->isError;
6001 }
6002 sqlite3VdbeMemRelease(&t);
drh9467abf2016-02-17 18:44:11 +00006003 if( rc ) goto abort_due_to_error;
drh9c7c9132015-06-26 18:16:52 +00006004 }else{
6005 assert( t.flags==MEM_Null );
danielk1977dc1bdc42004-06-11 10:51:27 +00006006 }
drh9c7c9132015-06-26 18:16:52 +00006007 if( pCtx->skipFlag ){
drh7a957892012-02-02 17:35:43 +00006008 assert( pOp[-1].opcode==OP_CollSeq );
6009 i = pOp[-1].p1;
6010 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
6011 }
drh5e00f6c2001-09-13 13:46:56 +00006012 break;
6013}
6014
drh98757152008-01-09 23:04:12 +00006015/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00006016** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00006017**
drh13449892005-09-07 21:22:45 +00006018** Execute the finalizer function for an aggregate. P1 is
6019** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00006020**
6021** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00006022** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00006023** argument is not used by this opcode. It is only there to disambiguate
6024** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00006025** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00006026** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00006027*/
drh9cbf3422008-01-17 16:22:13 +00006028case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00006029 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00006030 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006031 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00006032 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00006033 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00006034 if( rc ){
drh22c17b82015-05-15 04:13:15 +00006035 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh9467abf2016-02-17 18:44:11 +00006036 goto abort_due_to_error;
drh90669c12006-01-20 15:45:36 +00006037 }
drh2dca8682008-03-21 17:13:13 +00006038 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00006039 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00006040 if( sqlite3VdbeMemTooBig(pMem) ){
6041 goto too_big;
6042 }
drh5e00f6c2001-09-13 13:46:56 +00006043 break;
6044}
6045
dan5cf53532010-05-01 16:40:20 +00006046#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00006047/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006048**
6049** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00006050** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
6051** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00006052** SQLITE_BUSY or not, respectively. Write the number of pages in the
6053** WAL after the checkpoint into mem[P3+1] and the number of pages
6054** in the WAL that have been checkpointed after the checkpoint
6055** completes into mem[P3+2]. However on an error, mem[P3+1] and
6056** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00006057*/
6058case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00006059 int i; /* Loop counter */
6060 int aRes[3]; /* Results */
6061 Mem *pMem; /* Write results here */
6062
drh9e92a472013-06-27 17:40:30 +00006063 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00006064 aRes[0] = 0;
6065 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00006066 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6067 || pOp->p2==SQLITE_CHECKPOINT_FULL
6068 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00006069 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00006070 );
drh30aa3b92011-02-07 23:56:01 +00006071 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
drh9467abf2016-02-17 18:44:11 +00006072 if( rc ){
6073 if( rc!=SQLITE_BUSY ) goto abort_due_to_error;
dancdc1f042010-11-18 12:11:05 +00006074 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00006075 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00006076 }
drh30aa3b92011-02-07 23:56:01 +00006077 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
6078 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
6079 }
dan7c246102010-04-12 19:00:29 +00006080 break;
6081};
dan5cf53532010-05-01 16:40:20 +00006082#endif
drh5e00f6c2001-09-13 13:46:56 +00006083
drhcac29a62010-07-02 19:36:52 +00006084#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00006085/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00006086**
6087** Change the journal mode of database P1 to P3. P3 must be one of the
6088** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
6089** modes (delete, truncate, persist, off and memory), this is a simple
6090** operation. No IO is required.
6091**
6092** If changing into or out of WAL mode the procedure is more complicated.
6093**
6094** Write a string containing the final journal-mode to register P2.
6095*/
drh27a348c2015-04-13 19:14:06 +00006096case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00006097 Btree *pBt; /* Btree to change journal mode of */
6098 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00006099 int eNew; /* New journal mode */
6100 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00006101#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00006102 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00006103#endif
dane04dc882010-04-20 18:53:15 +00006104
drh27a348c2015-04-13 19:14:06 +00006105 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00006106 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00006107 assert( eNew==PAGER_JOURNALMODE_DELETE
6108 || eNew==PAGER_JOURNALMODE_TRUNCATE
6109 || eNew==PAGER_JOURNALMODE_PERSIST
6110 || eNew==PAGER_JOURNALMODE_OFF
6111 || eNew==PAGER_JOURNALMODE_MEMORY
6112 || eNew==PAGER_JOURNALMODE_WAL
6113 || eNew==PAGER_JOURNALMODE_QUERY
6114 );
6115 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006116 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006117
dane04dc882010-04-20 18:53:15 +00006118 pBt = db->aDb[pOp->p1].pBt;
6119 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006120 eOld = sqlite3PagerGetJournalMode(pPager);
6121 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6122 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006123
6124#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006125 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006126
drhd80b2332010-05-01 00:59:37 +00006127 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006128 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006129 */
6130 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006131 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006132 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006133 ){
drh0b9b4302010-06-11 17:01:24 +00006134 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006135 }
6136
drh0b9b4302010-06-11 17:01:24 +00006137 if( (eNew!=eOld)
6138 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6139 ){
danc0537fe2013-06-28 19:41:43 +00006140 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006141 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006142 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006143 "cannot change %s wal mode from within a transaction",
6144 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6145 );
drh9467abf2016-02-17 18:44:11 +00006146 goto abort_due_to_error;
drh0b9b4302010-06-11 17:01:24 +00006147 }else{
6148
6149 if( eOld==PAGER_JOURNALMODE_WAL ){
6150 /* If leaving WAL mode, close the log file. If successful, the call
6151 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6152 ** file. An EXCLUSIVE lock may still be held on the database file
6153 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006154 */
drh0b9b4302010-06-11 17:01:24 +00006155 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00006156 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006157 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006158 }
drh242c4f72010-06-22 14:49:39 +00006159 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6160 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6161 ** as an intermediate */
6162 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006163 }
6164
6165 /* Open a transaction on the database file. Regardless of the journal
6166 ** mode, this transaction always uses a rollback journal.
6167 */
6168 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6169 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006170 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006171 }
6172 }
6173 }
dan5cf53532010-05-01 16:40:20 +00006174#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006175
drh9467abf2016-02-17 18:44:11 +00006176 if( rc ) eNew = eOld;
drh0b9b4302010-06-11 17:01:24 +00006177 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006178
dane04dc882010-04-20 18:53:15 +00006179 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006180 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006181 pOut->n = sqlite3Strlen30(pOut->z);
6182 pOut->enc = SQLITE_UTF8;
6183 sqlite3VdbeChangeEncoding(pOut, encoding);
drh9467abf2016-02-17 18:44:11 +00006184 if( rc ) goto abort_due_to_error;
dane04dc882010-04-20 18:53:15 +00006185 break;
drhcac29a62010-07-02 19:36:52 +00006186};
6187#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006188
drhfdbcdee2007-03-27 14:44:50 +00006189#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00006190/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00006191**
6192** Vacuum the entire database. This opcode will cause other virtual
6193** machines to be created and run. It may not be called from within
6194** a transaction.
6195*/
drh9cbf3422008-01-17 16:22:13 +00006196case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006197 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00006198 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh9467abf2016-02-17 18:44:11 +00006199 if( rc ) goto abort_due_to_error;
drh6f8c91c2003-12-07 00:24:35 +00006200 break;
6201}
drh154d4b22006-09-21 11:02:16 +00006202#endif
drh6f8c91c2003-12-07 00:24:35 +00006203
danielk1977dddbcdc2007-04-26 14:42:34 +00006204#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006205/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006206**
6207** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006208** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006209** P2. Otherwise, fall through to the next instruction.
6210*/
drh9cbf3422008-01-17 16:22:13 +00006211case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006212 Btree *pBt;
6213
6214 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006215 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006216 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006217 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006218 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006219 VdbeBranchTaken(rc==SQLITE_DONE,2);
drh9467abf2016-02-17 18:44:11 +00006220 if( rc ){
6221 if( rc!=SQLITE_DONE ) goto abort_due_to_error;
danielk1977dddbcdc2007-04-26 14:42:34 +00006222 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006223 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006224 }
6225 break;
6226}
6227#endif
6228
drh98757152008-01-09 23:04:12 +00006229/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006230**
drh25df48d2014-07-22 14:58:12 +00006231** Cause precompiled statements to expire. When an expired statement
6232** is executed using sqlite3_step() it will either automatically
6233** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6234** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006235**
6236** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006237** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00006238*/
drh9cbf3422008-01-17 16:22:13 +00006239case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00006240 if( !pOp->p1 ){
6241 sqlite3ExpirePreparedStatements(db);
6242 }else{
6243 p->expired = 1;
6244 }
6245 break;
6246}
6247
danielk1977c00da102006-01-07 13:21:04 +00006248#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006249/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006250** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006251**
6252** Obtain a lock on a particular table. This instruction is only used when
6253** the shared-cache feature is enabled.
6254**
danielk197796d48e92009-06-29 06:00:37 +00006255** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006256** on which the lock is acquired. A readlock is obtained if P3==0 or
6257** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006258**
6259** P2 contains the root-page of the table to lock.
6260**
drh66a51672008-01-03 00:01:23 +00006261** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006262** used to generate an error message if the lock cannot be obtained.
6263*/
drh9cbf3422008-01-17 16:22:13 +00006264case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006265 u8 isWriteLock = (u8)pOp->p3;
6266 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6267 int p1 = pOp->p1;
6268 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006269 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006270 assert( isWriteLock==0 || isWriteLock==1 );
6271 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
drh9467abf2016-02-17 18:44:11 +00006272 if( rc ){
6273 if( (rc&0xFF)==SQLITE_LOCKED ){
6274 const char *z = pOp->p4.z;
6275 sqlite3VdbeError(p, "database table is locked: %s", z);
6276 }
6277 goto abort_due_to_error;
danielk1977e0d9e6f2009-07-03 16:25:06 +00006278 }
danielk1977c00da102006-01-07 13:21:04 +00006279 }
6280 break;
6281}
drhb9bb7c12006-06-11 23:41:55 +00006282#endif /* SQLITE_OMIT_SHARED_CACHE */
6283
6284#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006285/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006286**
danielk19773e3a84d2008-08-01 17:37:40 +00006287** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6288** xBegin method for that table.
6289**
6290** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006291** within a callback to a virtual table xSync() method. If it is, the error
6292** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006293*/
drh9cbf3422008-01-17 16:22:13 +00006294case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006295 VTable *pVTab;
6296 pVTab = pOp->p4.pVtab;
6297 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006298 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
drh9467abf2016-02-17 18:44:11 +00006299 if( rc ) goto abort_due_to_error;
danielk1977f9e7dda2006-06-16 16:08:53 +00006300 break;
6301}
6302#endif /* SQLITE_OMIT_VIRTUALTABLE */
6303
6304#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006305/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006306**
dan73779452015-03-19 18:56:17 +00006307** P2 is a register that holds the name of a virtual table in database
6308** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006309*/
drh9cbf3422008-01-17 16:22:13 +00006310case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006311 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006312 const char *zTab; /* Name of the virtual table */
6313
dan73779452015-03-19 18:56:17 +00006314 memset(&sMem, 0, sizeof(sMem));
6315 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006316 /* Because P2 is always a static string, it is impossible for the
6317 ** sqlite3VdbeMemCopy() to fail */
6318 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6319 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006320 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006321 assert( rc==SQLITE_OK );
6322 zTab = (const char*)sqlite3_value_text(&sMem);
6323 assert( zTab || db->mallocFailed );
6324 if( zTab ){
6325 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006326 }
6327 sqlite3VdbeMemRelease(&sMem);
drh9467abf2016-02-17 18:44:11 +00006328 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006329 break;
6330}
6331#endif /* SQLITE_OMIT_VIRTUALTABLE */
6332
6333#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006334/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006335**
drh66a51672008-01-03 00:01:23 +00006336** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006337** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006338*/
drh9cbf3422008-01-17 16:22:13 +00006339case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006340 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006341 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006342 db->nVDestroy--;
drh9467abf2016-02-17 18:44:11 +00006343 if( rc ) goto abort_due_to_error;
drhb9bb7c12006-06-11 23:41:55 +00006344 break;
6345}
6346#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006347
drh9eff6162006-06-12 21:59:13 +00006348#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006349/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006350**
drh66a51672008-01-03 00:01:23 +00006351** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006352** P1 is a cursor number. This opcode opens a cursor to the virtual
6353** table and stores that cursor in P1.
6354*/
drh9cbf3422008-01-17 16:22:13 +00006355case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006356 VdbeCursor *pCur;
drhc960dcb2015-11-20 19:22:01 +00006357 sqlite3_vtab_cursor *pVCur;
drh856c1032009-06-02 15:21:42 +00006358 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006359 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006360
drh1713afb2013-06-28 01:24:57 +00006361 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006362 pCur = 0;
drhc960dcb2015-11-20 19:22:01 +00006363 pVCur = 0;
danielk1977595a5232009-07-24 17:58:53 +00006364 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006365 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6366 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006367 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006368 }
6369 pModule = pVtab->pModule;
drhc960dcb2015-11-20 19:22:01 +00006370 rc = pModule->xOpen(pVtab, &pVCur);
dan016f7812013-08-21 17:35:48 +00006371 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006372 if( rc ) goto abort_due_to_error;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006373
drh9467abf2016-02-17 18:44:11 +00006374 /* Initialize sqlite3_vtab_cursor base class */
6375 pVCur->pVtab = pVtab;
6376
6377 /* Initialize vdbe cursor object */
6378 pCur = allocateCursor(p, pOp->p1, 0, -1, CURTYPE_VTAB);
6379 if( pCur ){
6380 pCur->uc.pVCur = pVCur;
6381 pVtab->nRef++;
6382 }else{
6383 assert( db->mallocFailed );
6384 pModule->xClose(pVCur);
6385 goto no_mem;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006386 }
drh9eff6162006-06-12 21:59:13 +00006387 break;
6388}
6389#endif /* SQLITE_OMIT_VIRTUALTABLE */
6390
6391#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006392/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006393** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006394**
6395** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6396** the filtered result set is empty.
6397**
drh66a51672008-01-03 00:01:23 +00006398** P4 is either NULL or a string that was generated by the xBestIndex
6399** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006400** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006401**
drh9eff6162006-06-12 21:59:13 +00006402** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006403** by P1. The integer query plan parameter to xFilter is stored in register
6404** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006405** xFilter method. Registers P3+2..P3+1+argc are the argc
6406** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006407** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006408**
danielk19776dbee812008-01-03 18:39:41 +00006409** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006410*/
drh9cbf3422008-01-17 16:22:13 +00006411case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006412 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006413 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006414 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006415 Mem *pQuery;
6416 Mem *pArgc;
drhc960dcb2015-11-20 19:22:01 +00006417 sqlite3_vtab_cursor *pVCur;
drh4dc754d2008-07-23 18:17:32 +00006418 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006419 VdbeCursor *pCur;
6420 int res;
6421 int i;
6422 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006423
drha6c2ed92009-11-14 23:22:23 +00006424 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006425 pArgc = &pQuery[1];
6426 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006427 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006428 REGISTER_TRACE(pOp->p3, pQuery);
drhc960dcb2015-11-20 19:22:01 +00006429 assert( pCur->eCurType==CURTYPE_VTAB );
6430 pVCur = pCur->uc.pVCur;
6431 pVtab = pVCur->pVtab;
drh4dc754d2008-07-23 18:17:32 +00006432 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006433
drh9cbf3422008-01-17 16:22:13 +00006434 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006435 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006436 nArg = (int)pArgc->u.i;
6437 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006438
drh644a5292006-12-20 14:53:38 +00006439 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006440 res = 0;
6441 apArg = p->apArg;
6442 for(i = 0; i<nArg; i++){
6443 apArg[i] = &pArgc[i+1];
6444 }
drhc960dcb2015-11-20 19:22:01 +00006445 rc = pModule->xFilter(pVCur, iQuery, pOp->p4.z, nArg, apArg);
drhf56fa462015-04-13 21:39:54 +00006446 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006447 if( rc ) goto abort_due_to_error;
6448 res = pModule->xEof(pVCur);
drh1d454a32008-01-31 19:34:51 +00006449 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006450 VdbeBranchTaken(res!=0,2);
6451 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006452 break;
6453}
6454#endif /* SQLITE_OMIT_VIRTUALTABLE */
6455
6456#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006457/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006458** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006459**
drh2133d822008-01-03 18:44:59 +00006460** Store the value of the P2-th column of
6461** the row of the virtual-table that the
6462** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006463*/
6464case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006465 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006466 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006467 Mem *pDest;
6468 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006469
drhdfe88ec2008-11-03 20:55:06 +00006470 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006471 assert( pCur->eCurType==CURTYPE_VTAB );
dan3bc9f742013-08-15 16:18:39 +00006472 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006473 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006474 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006475 if( pCur->nullRow ){
6476 sqlite3VdbeMemSetNull(pDest);
6477 break;
6478 }
drhc960dcb2015-11-20 19:22:01 +00006479 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006480 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006481 assert( pModule->xColumn );
6482 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006483 sContext.pOut = pDest;
6484 MemSetTypeFlag(pDest, MEM_Null);
drhc960dcb2015-11-20 19:22:01 +00006485 rc = pModule->xColumn(pCur->uc.pVCur, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006486 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006487 if( sContext.isError ){
6488 rc = sContext.isError;
6489 }
drh9bd038f2014-08-27 14:14:06 +00006490 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006491 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006492 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006493
drhde4fcfd2008-01-19 23:50:26 +00006494 if( sqlite3VdbeMemTooBig(pDest) ){
6495 goto too_big;
6496 }
drh9467abf2016-02-17 18:44:11 +00006497 if( rc ) goto abort_due_to_error;
drh9eff6162006-06-12 21:59:13 +00006498 break;
6499}
6500#endif /* SQLITE_OMIT_VIRTUALTABLE */
6501
6502#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006503/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006504**
6505** Advance virtual table P1 to the next row in its result set and
6506** jump to instruction P2. Or, if the virtual table has reached
6507** the end of its result set, then fall through to the next instruction.
6508*/
drh9cbf3422008-01-17 16:22:13 +00006509case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006510 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006511 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006512 int res;
drh856c1032009-06-02 15:21:42 +00006513 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006514
drhc54a6172009-06-02 16:06:03 +00006515 res = 0;
drh856c1032009-06-02 15:21:42 +00006516 pCur = p->apCsr[pOp->p1];
drhc960dcb2015-11-20 19:22:01 +00006517 assert( pCur->eCurType==CURTYPE_VTAB );
drh2945b4a2008-01-31 15:53:45 +00006518 if( pCur->nullRow ){
6519 break;
6520 }
drhc960dcb2015-11-20 19:22:01 +00006521 pVtab = pCur->uc.pVCur->pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00006522 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006523 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006524
drhde4fcfd2008-01-19 23:50:26 +00006525 /* Invoke the xNext() method of the module. There is no way for the
6526 ** underlying implementation to return an error if one occurs during
6527 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6528 ** data is available) and the error code returned when xColumn or
6529 ** some other method is next invoked on the save virtual table cursor.
6530 */
drhc960dcb2015-11-20 19:22:01 +00006531 rc = pModule->xNext(pCur->uc.pVCur);
dan016f7812013-08-21 17:35:48 +00006532 sqlite3VtabImportErrmsg(p, pVtab);
drh9467abf2016-02-17 18:44:11 +00006533 if( rc ) goto abort_due_to_error;
6534 res = pModule->xEof(pCur->uc.pVCur);
drh688852a2014-02-17 22:40:43 +00006535 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006536 if( !res ){
6537 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00006538 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00006539 }
drh49afe3a2013-07-10 03:05:14 +00006540 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006541}
6542#endif /* SQLITE_OMIT_VIRTUALTABLE */
6543
danielk1977182c4ba2007-06-27 15:53:34 +00006544#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006545/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006546**
drh66a51672008-01-03 00:01:23 +00006547** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006548** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006549** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006550*/
drh9cbf3422008-01-17 16:22:13 +00006551case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006552 sqlite3_vtab *pVtab;
6553 Mem *pName;
6554
danielk1977595a5232009-07-24 17:58:53 +00006555 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006556 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006557 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006558 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006559 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006560 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006561 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006562 testcase( pName->enc==SQLITE_UTF8 );
6563 testcase( pName->enc==SQLITE_UTF16BE );
6564 testcase( pName->enc==SQLITE_UTF16LE );
6565 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
drh9467abf2016-02-17 18:44:11 +00006566 if( rc ) goto abort_due_to_error;
6567 rc = pVtab->pModule->xRename(pVtab, pName->z);
6568 sqlite3VtabImportErrmsg(p, pVtab);
6569 p->expired = 0;
6570 if( rc ) goto abort_due_to_error;
danielk1977182c4ba2007-06-27 15:53:34 +00006571 break;
6572}
6573#endif
drh4cbdda92006-06-14 19:00:20 +00006574
6575#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006576/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006577** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006578**
drh66a51672008-01-03 00:01:23 +00006579** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006580** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006581** are contiguous memory cells starting at P3 to pass to the xUpdate
6582** invocation. The value in register (P3+P2-1) corresponds to the
6583** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006584**
6585** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006586** The argv[0] element (which corresponds to memory cell P3)
6587** is the rowid of a row to delete. If argv[0] is NULL then no
6588** deletion occurs. The argv[1] element is the rowid of the new
6589** row. This can be NULL to have the virtual table select the new
6590** rowid for itself. The subsequent elements in the array are
6591** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006592**
6593** If P2==1 then no insert is performed. argv[0] is the rowid of
6594** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006595**
6596** P1 is a boolean flag. If it is set to true and the xUpdate call
6597** is successful, then the value returned by sqlite3_last_insert_rowid()
6598** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006599**
6600** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6601** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006602*/
drh9cbf3422008-01-17 16:22:13 +00006603case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006604 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006605 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006606 int nArg;
6607 int i;
6608 sqlite_int64 rowid;
6609 Mem **apArg;
6610 Mem *pX;
6611
danb061d052011-04-25 18:49:57 +00006612 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6613 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6614 );
drh9e92a472013-06-27 17:40:30 +00006615 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006616 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006617 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6618 rc = SQLITE_LOCKED;
drh9467abf2016-02-17 18:44:11 +00006619 goto abort_due_to_error;
drhf496a7d2015-03-24 14:05:50 +00006620 }
6621 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00006622 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006623 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006624 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006625 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006626 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006627 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006628 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006629 assert( memIsValid(pX) );
6630 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006631 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006632 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006633 }
danb061d052011-04-25 18:49:57 +00006634 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006635 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006636 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006637 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006638 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006639 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006640 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006641 }
drhd91c1a12013-02-09 13:58:25 +00006642 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006643 if( pOp->p5==OE_Ignore ){
6644 rc = SQLITE_OK;
6645 }else{
6646 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6647 }
6648 }else{
6649 p->nChange++;
6650 }
drh9467abf2016-02-17 18:44:11 +00006651 if( rc ) goto abort_due_to_error;
danielk1977399918f2006-06-14 13:03:23 +00006652 }
drh4cbdda92006-06-14 19:00:20 +00006653 break;
danielk1977399918f2006-06-14 13:03:23 +00006654}
6655#endif /* SQLITE_OMIT_VIRTUALTABLE */
6656
danielk197759a93792008-05-15 17:48:20 +00006657#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6658/* Opcode: Pagecount P1 P2 * * *
6659**
6660** Write the current number of pages in database P1 to memory cell P2.
6661*/
drh27a348c2015-04-13 19:14:06 +00006662case OP_Pagecount: { /* out2 */
6663 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00006664 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006665 break;
6666}
6667#endif
6668
drh60ac3f42010-11-23 18:59:27 +00006669
6670#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6671/* Opcode: MaxPgcnt P1 P2 P3 * *
6672**
6673** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006674** Do not let the maximum page count fall below the current page count and
6675** do not change the maximum page count value if P3==0.
6676**
drh60ac3f42010-11-23 18:59:27 +00006677** Store the maximum page count after the change in register P2.
6678*/
drh27a348c2015-04-13 19:14:06 +00006679case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00006680 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006681 Btree *pBt;
6682
drh27a348c2015-04-13 19:14:06 +00006683 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00006684 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006685 newMax = 0;
6686 if( pOp->p3 ){
6687 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006688 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006689 }
6690 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006691 break;
6692}
6693#endif
6694
6695
drhaceb31b2014-02-08 01:40:27 +00006696/* Opcode: Init * P2 * P4 *
6697** Synopsis: Start at P2
6698**
6699** Programs contain a single instance of this opcode as the very first
6700** opcode.
drh949f9cd2008-01-12 21:35:57 +00006701**
6702** If tracing is enabled (by the sqlite3_trace()) interface, then
6703** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006704** Or if P4 is blank, use the string returned by sqlite3_sql().
6705**
6706** If P2 is not zero, jump to instruction P2.
drh949f9cd2008-01-12 21:35:57 +00006707*/
drhaceb31b2014-02-08 01:40:27 +00006708case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006709 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006710 char *z;
drh856c1032009-06-02 15:21:42 +00006711
drhaceb31b2014-02-08 01:40:27 +00006712#ifndef SQLITE_OMIT_TRACE
drh37f58e92012-09-04 21:34:26 +00006713 if( db->xTrace
6714 && !p->doingRerun
6715 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6716 ){
drhc3f1d5f2011-05-30 23:42:16 +00006717 z = sqlite3VdbeExpandSql(p, zTrace);
6718 db->xTrace(db->pTraceArg, z);
6719 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006720 }
drh8f8b2312013-10-18 20:03:43 +00006721#ifdef SQLITE_USE_FCNTL_TRACE
6722 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6723 if( zTrace ){
6724 int i;
6725 for(i=0; i<db->nDb; i++){
drha7ab6d82014-07-21 15:44:39 +00006726 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
drh8f8b2312013-10-18 20:03:43 +00006727 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6728 }
6729 }
6730#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006731#ifdef SQLITE_DEBUG
6732 if( (db->flags & SQLITE_SqlTrace)!=0
6733 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6734 ){
6735 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6736 }
6737#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00006738#endif /* SQLITE_OMIT_TRACE */
drhf56fa462015-04-13 21:39:54 +00006739 if( pOp->p2 ) goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00006740 break;
6741}
drh949f9cd2008-01-12 21:35:57 +00006742
drh28935362013-12-07 20:39:19 +00006743#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00006744/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00006745**
6746** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00006747** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
6748** to values currently held in registers. TK_COLUMN terms in the P4
6749** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00006750*/
6751case OP_CursorHint: {
6752 VdbeCursor *pC;
6753
6754 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6755 assert( pOp->p4type==P4_EXPR );
6756 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00006757 if( pC ){
drhc960dcb2015-11-20 19:22:01 +00006758 assert( pC->eCurType==CURTYPE_BTREE );
drh62aaa6c2015-11-21 17:27:42 +00006759 sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
6760 pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00006761 }
drh28935362013-12-07 20:39:19 +00006762 break;
6763}
6764#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00006765
6766/* Opcode: Noop * * * * *
6767**
6768** Do nothing. This instruction is often useful as a jump
6769** destination.
drh5e00f6c2001-09-13 13:46:56 +00006770*/
drh91fd4d42008-01-19 20:11:25 +00006771/*
6772** The magic Explain opcode are only inserted when explain==2 (which
6773** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6774** This opcode records information from the optimizer. It is the
6775** the same as a no-op. This opcodesnever appears in a real VM program.
6776*/
6777default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006778 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006779 break;
6780}
6781
6782/*****************************************************************************
6783** The cases of the switch statement above this line should all be indented
6784** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6785** readability. From this point on down, the normal indentation rules are
6786** restored.
6787*****************************************************************************/
6788 }
drh6e142f52000-06-08 13:36:40 +00006789
drh7b396862003-01-01 23:06:20 +00006790#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006791 {
drha01c7c72014-04-25 12:35:31 +00006792 u64 endTime = sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00006793 if( endTime>start ) pOrigOp->cycles += endTime - start;
6794 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00006795 }
drh7b396862003-01-01 23:06:20 +00006796#endif
6797
drh6e142f52000-06-08 13:36:40 +00006798 /* The following code adds nothing to the actual functionality
6799 ** of the program. It is only here for testing and debugging.
6800 ** On the other hand, it does burn CPU cycles every time through
6801 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6802 */
6803#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00006804 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00006805
drhcf1023c2007-05-08 20:59:49 +00006806#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006807 if( db->flags & SQLITE_VdbeTrace ){
6808 if( rc!=0 ) printf("rc=%d\n",rc);
drh6dc41482015-04-16 17:31:02 +00006809 if( pOrigOp->opflags & (OPFLG_OUT2) ){
6810 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00006811 }
drh6dc41482015-04-16 17:31:02 +00006812 if( pOrigOp->opflags & OPFLG_OUT3 ){
6813 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006814 }
drh75897232000-05-29 14:26:00 +00006815 }
danielk1977b5402fb2005-01-12 07:15:04 +00006816#endif /* SQLITE_DEBUG */
6817#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006818 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006819
drha05a7222008-01-19 03:35:58 +00006820 /* If we reach this point, it means that execution is finished with
6821 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006822 */
drh9467abf2016-02-17 18:44:11 +00006823abort_due_to_error:
6824 if( db->mallocFailed ) rc = SQLITE_NOMEM_BKPT;
drha05a7222008-01-19 03:35:58 +00006825 assert( rc );
drh9467abf2016-02-17 18:44:11 +00006826 if( p->zErrMsg==0 && rc!=SQLITE_IOERR_NOMEM ){
6827 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
6828 }
drha05a7222008-01-19 03:35:58 +00006829 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006830 testcase( sqlite3GlobalConfig.xLog!=0 );
6831 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00006832 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006833 sqlite3VdbeHalt(p);
drh4a642b62016-02-05 01:55:27 +00006834 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
danielk19777eaabcd2008-07-07 14:56:56 +00006835 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006836 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006837 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006838 }
drh900b31e2007-08-28 02:27:51 +00006839
6840 /* This is the only way out of this procedure. We have to
6841 ** release the mutexes on btrees that were acquired at the
6842 ** top. */
6843vdbe_return:
drh99a66922011-05-13 18:51:42 +00006844 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006845 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006846 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006847 sqlite3VdbeLeave(p);
dan83f0ab82016-01-29 18:04:31 +00006848 assert( rc!=SQLITE_OK || nExtraDelete==0
6849 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
6850 );
drhb86ccfb2003-01-28 23:13:10 +00006851 return rc;
6852
drh023ae032007-05-08 12:12:16 +00006853 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6854 ** is encountered.
6855 */
6856too_big:
drh22c17b82015-05-15 04:13:15 +00006857 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006858 rc = SQLITE_TOOBIG;
drh9467abf2016-02-17 18:44:11 +00006859 goto abort_due_to_error;
drh023ae032007-05-08 12:12:16 +00006860
drh98640a32007-06-07 19:08:32 +00006861 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006862 */
6863no_mem:
drh4a642b62016-02-05 01:55:27 +00006864 sqlite3OomFault(db);
drh22c17b82015-05-15 04:13:15 +00006865 sqlite3VdbeError(p, "out of memory");
mistachkinfad30392016-02-13 23:43:46 +00006866 rc = SQLITE_NOMEM_BKPT;
drh9467abf2016-02-17 18:44:11 +00006867 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00006868
danielk19776f8a5032004-05-10 10:34:51 +00006869 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006870 ** flag.
6871 */
6872abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006873 assert( db->u1.isInterrupted );
mistachkinfad30392016-02-13 23:43:46 +00006874 rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006875 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00006876 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drh9467abf2016-02-17 18:44:11 +00006877 goto abort_due_to_error;
drhb86ccfb2003-01-28 23:13:10 +00006878}