blob: d13655d9c1b9627e6425848a37f603cb575ed059 [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. */
drh0fd61352014-02-07 02:29:45 +0000168#define isSorter(x) ((x)->pSorter!=0)
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 */
drh3e9ca092009-09-08 01:14:48 +0000179 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
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 +
205 (isBtreeCursor?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));
danielk197794eb6a12005-12-15 15:22:08 +0000215 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000216 pCx->nField = nField;
drhb53a5a92014-10-12 22:37:22 +0000217 pCx->aOffset = &pCx->aType[nField];
danielk1977cd3e8f72008-03-25 09:47:35 +0000218 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000219 pCx->pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000220 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhf25a5072009-11-18 23:01:25 +0000221 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000222 }
danielk197794eb6a12005-12-15 15:22:08 +0000223 }
drh4774b132004-06-12 20:12:51 +0000224 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000225}
226
danielk19773d1bfea2004-05-14 11:00:53 +0000227/*
drh29d72102006-02-09 22:13:41 +0000228** Try to convert a value into a numeric representation if we can
229** do so without loss of information. In other words, if the string
230** looks like a number, convert it into a number. If it does not
231** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000232**
233** If the bTryForInt flag is true, then extra effort is made to give
234** an integer representation. Strings that look like floating point
235** values but which have no fractional component (example: '48.00')
236** will have a MEM_Int representation when bTryForInt is true.
237**
238** If bTryForInt is false, then if the input string contains a decimal
239** point or exponential notation, the result is only MEM_Real, even
240** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000241*/
drhbd9507c2014-08-23 17:21:37 +0000242static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000243 double rValue;
244 i64 iValue;
245 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000246 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000247 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
248 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
249 pRec->u.i = iValue;
250 pRec->flags |= MEM_Int;
251 }else{
drh74eaba42014-09-18 17:52:15 +0000252 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000253 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000254 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000255 }
256}
257
258/*
drh8a512562005-11-14 22:29:05 +0000259** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000260**
drh8a512562005-11-14 22:29:05 +0000261** SQLITE_AFF_INTEGER:
262** SQLITE_AFF_REAL:
263** SQLITE_AFF_NUMERIC:
264** Try to convert pRec to an integer representation or a
265** floating-point representation if an integer representation
266** is not possible. Note that the integer representation is
267** always preferred, even if the affinity is REAL, because
268** an integer representation is more space efficient on disk.
269**
270** SQLITE_AFF_TEXT:
271** Convert pRec to a text representation.
272**
drh05883a32015-06-02 15:32:08 +0000273** SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000274** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000275*/
drh17435752007-08-16 04:30:38 +0000276static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000277 Mem *pRec, /* The value to apply affinity to */
278 char affinity, /* The affinity to be applied */
279 u8 enc /* Use this text encoding */
280){
drh7ea31cc2014-09-18 14:36:00 +0000281 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000282 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
283 || affinity==SQLITE_AFF_NUMERIC );
drhbd9507c2014-08-23 17:21:37 +0000284 if( (pRec->flags & MEM_Int)==0 ){
285 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000286 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000287 }else{
288 sqlite3VdbeIntegerAffinity(pRec);
289 }
drh17c40292004-07-21 02:53:29 +0000290 }
drh7ea31cc2014-09-18 14:36:00 +0000291 }else if( affinity==SQLITE_AFF_TEXT ){
danielk19773d1bfea2004-05-14 11:00:53 +0000292 /* Only attempt the conversion to TEXT if there is an integer or real
drhf4479502004-05-27 03:12:53 +0000293 ** representation (blob and NULL do not get converted) but no string
danielk19773d1bfea2004-05-14 11:00:53 +0000294 ** representation.
295 */
296 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drh7ea31cc2014-09-18 14:36:00 +0000297 sqlite3VdbeMemStringify(pRec, enc, 1);
danielk19773d1bfea2004-05-14 11:00:53 +0000298 }
299 pRec->flags &= ~(MEM_Real|MEM_Int);
danielk19773d1bfea2004-05-14 11:00:53 +0000300 }
301}
302
danielk1977aee18ef2005-03-09 12:26:50 +0000303/*
drh29d72102006-02-09 22:13:41 +0000304** Try to convert the type of a function argument or a result column
305** into a numeric representation. Use either INTEGER or REAL whichever
306** is appropriate. But only do the conversion if it is possible without
307** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000308*/
309int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000310 int eType = sqlite3_value_type(pVal);
311 if( eType==SQLITE_TEXT ){
312 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000313 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000314 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000315 }
drh1b27b8c2014-02-10 03:21:57 +0000316 return eType;
drh29d72102006-02-09 22:13:41 +0000317}
318
319/*
danielk1977aee18ef2005-03-09 12:26:50 +0000320** Exported version of applyAffinity(). This one works on sqlite3_value*,
321** not the internal Mem* type.
322*/
danielk19771e536952007-08-16 10:09:01 +0000323void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000324 sqlite3_value *pVal,
325 u8 affinity,
326 u8 enc
327){
drhb21c8cd2007-08-21 19:33:56 +0000328 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000329}
330
drh3d1d90a2014-03-24 15:00:15 +0000331/*
drhf1a89ed2014-08-23 17:41:15 +0000332** pMem currently only holds a string type (or maybe a BLOB that we can
333** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000334** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000335** accordingly.
336*/
337static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
338 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
339 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000340 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000341 return 0;
342 }
343 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
344 return MEM_Int;
345 }
346 return MEM_Real;
347}
348
349/*
drh3d1d90a2014-03-24 15:00:15 +0000350** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
351** none.
352**
353** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000354** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000355*/
356static u16 numericType(Mem *pMem){
357 if( pMem->flags & (MEM_Int|MEM_Real) ){
358 return pMem->flags & (MEM_Int|MEM_Real);
359 }
360 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000361 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000362 }
363 return 0;
364}
365
danielk1977b5402fb2005-01-12 07:15:04 +0000366#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000367/*
danielk1977ca6b2912004-05-21 10:49:47 +0000368** Write a nice string representation of the contents of cell pMem
369** into buffer zBuf, length nBuf.
370*/
drh74161702006-02-24 02:53:49 +0000371void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000372 char *zCsr = zBuf;
373 int f = pMem->flags;
374
drh57196282004-10-06 15:41:16 +0000375 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000376
danielk1977ca6b2912004-05-21 10:49:47 +0000377 if( f&MEM_Blob ){
378 int i;
379 char c;
380 if( f & MEM_Dyn ){
381 c = 'z';
382 assert( (f & (MEM_Static|MEM_Ephem))==0 );
383 }else if( f & MEM_Static ){
384 c = 't';
385 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
386 }else if( f & MEM_Ephem ){
387 c = 'e';
388 assert( (f & (MEM_Static|MEM_Dyn))==0 );
389 }else{
390 c = 's';
391 }
392
drh5bb3eb92007-05-04 13:15:55 +0000393 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000394 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000395 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000396 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000397 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000398 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000399 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000400 }
401 for(i=0; i<16 && i<pMem->n; i++){
402 char z = pMem->z[i];
403 if( z<32 || z>126 ) *zCsr++ = '.';
404 else *zCsr++ = z;
405 }
406
drhe718efe2007-05-10 21:14:03 +0000407 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000408 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000409 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000410 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000411 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000412 }
danielk1977b1bc9532004-05-22 03:05:33 +0000413 *zCsr = '\0';
414 }else if( f & MEM_Str ){
415 int j, k;
416 zBuf[0] = ' ';
417 if( f & MEM_Dyn ){
418 zBuf[1] = 'z';
419 assert( (f & (MEM_Static|MEM_Ephem))==0 );
420 }else if( f & MEM_Static ){
421 zBuf[1] = 't';
422 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
423 }else if( f & MEM_Ephem ){
424 zBuf[1] = 'e';
425 assert( (f & (MEM_Static|MEM_Dyn))==0 );
426 }else{
427 zBuf[1] = 's';
428 }
429 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000430 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000431 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000432 zBuf[k++] = '[';
433 for(j=0; j<15 && j<pMem->n; j++){
434 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000435 if( c>=0x20 && c<0x7f ){
436 zBuf[k++] = c;
437 }else{
438 zBuf[k++] = '.';
439 }
440 }
441 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000442 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000443 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000444 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000445 }
danielk1977ca6b2912004-05-21 10:49:47 +0000446}
447#endif
448
drh5b6afba2008-01-05 16:29:28 +0000449#ifdef SQLITE_DEBUG
450/*
451** Print the value of a register for tracing purposes:
452*/
drh84e55a82013-11-13 17:58:23 +0000453static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000454 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000455 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000456 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000457 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000458 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000459 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000460 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000461 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000462#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000463 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000464 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000465#endif
drh733bf1b2009-04-22 00:47:00 +0000466 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000467 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000468 }else{
469 char zBuf[200];
470 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000471 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000472 }
473}
drh84e55a82013-11-13 17:58:23 +0000474static void registerTrace(int iReg, Mem *p){
475 printf("REG[%d] = ", iReg);
476 memTracePrint(p);
477 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000478}
479#endif
480
481#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000482# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000483#else
484# define REGISTER_TRACE(R,M)
485#endif
486
danielk197784ac9d02004-05-18 09:58:06 +0000487
drh7b396862003-01-01 23:06:20 +0000488#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000489
490/*
491** hwtime.h contains inline assembler code for implementing
492** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000493*/
shane9bcbdad2008-05-29 20:22:37 +0000494#include "hwtime.h"
495
drh7b396862003-01-01 23:06:20 +0000496#endif
497
danielk1977fd7f0452008-12-17 17:30:26 +0000498#ifndef NDEBUG
499/*
500** This function is only called from within an assert() expression. It
501** checks that the sqlite3.nTransaction variable is correctly set to
502** the number of non-transaction savepoints currently in the
503** linked list starting at sqlite3.pSavepoint.
504**
505** Usage:
506**
507** assert( checkSavepointCount(db) );
508*/
509static int checkSavepointCount(sqlite3 *db){
510 int n = 0;
511 Savepoint *p;
512 for(p=db->pSavepoint; p; p=p->pNext) n++;
513 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
514 return 1;
515}
516#endif
517
drh27a348c2015-04-13 19:14:06 +0000518/*
519** Return the register of pOp->p2 after first preparing it to be
520** overwritten with an integer value.
drh9eef8c62015-10-15 17:31:41 +0000521*/
522static SQLITE_NOINLINE Mem *out2PrereleaseWithClear(Mem *pOut){
523 sqlite3VdbeMemSetNull(pOut);
524 pOut->flags = MEM_Int;
525 return pOut;
526}
drh27a348c2015-04-13 19:14:06 +0000527static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){
528 Mem *pOut;
529 assert( pOp->p2>0 );
530 assert( pOp->p2<=(p->nMem-p->nCursor) );
531 pOut = &p->aMem[pOp->p2];
532 memAboutToChange(p, pOut);
drh9eef8c62015-10-15 17:31:41 +0000533 if( VdbeMemDynamic(pOut) ){
534 return out2PrereleaseWithClear(pOut);
535 }else{
536 pOut->flags = MEM_Int;
537 return pOut;
538 }
drh27a348c2015-04-13 19:14:06 +0000539}
540
drhb9755982010-07-24 16:34:37 +0000541
542/*
drh0fd61352014-02-07 02:29:45 +0000543** Execute as much of a VDBE program as we can.
544** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000545*/
danielk19774adee202004-05-08 08:23:19 +0000546int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000547 Vdbe *p /* The VDBE */
548){
drhbbe879d2009-11-14 18:04:35 +0000549 Op *aOp = p->aOp; /* Copy of p->aOp */
drhf56fa462015-04-13 21:39:54 +0000550 Op *pOp = aOp; /* Current operation */
drh6dc41482015-04-16 17:31:02 +0000551#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
552 Op *pOrigOp; /* Value of pOp at the top of the loop */
553#endif
drhb86ccfb2003-01-28 23:13:10 +0000554 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000555 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000556 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000557 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000558 int iCompare = 0; /* Result of last OP_Compare operation */
559 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000560#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000561 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000562#endif
drha6c2ed92009-11-14 23:22:23 +0000563 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000564 Mem *pIn1 = 0; /* 1st input operand */
565 Mem *pIn2 = 0; /* 2nd input operand */
566 Mem *pIn3 = 0; /* 3rd input operand */
567 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000568 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000569 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000570#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000571 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000572#endif
drh856c1032009-06-02 15:21:42 +0000573 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000574
drhca48c902008-01-18 14:08:24 +0000575 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000576 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000577 if( p->rc==SQLITE_NOMEM ){
578 /* This happens if a malloc() inside a call to sqlite3_column_text() or
579 ** sqlite3_column_text16() failed. */
580 goto no_mem;
581 }
drhcbd8db32015-08-20 17:18:32 +0000582 assert( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000583 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000584 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000585 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000586 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000587 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000588 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000589 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000590 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000591#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
592 if( db->xProgress ){
drh6cbbdb02015-06-24 14:36:27 +0000593 u32 iPrior = p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000594 assert( 0 < db->nProgressOps );
drh6cbbdb02015-06-24 14:36:27 +0000595 nProgressLimit = db->nProgressOps - (iPrior % db->nProgressOps);
drh0d1961e2013-07-25 16:27:51 +0000596 }
597#endif
drh3c23a882007-01-09 14:01:13 +0000598#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000599 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000600 if( p->pc==0
601 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
602 ){
drh3c23a882007-01-09 14:01:13 +0000603 int i;
drh84e55a82013-11-13 17:58:23 +0000604 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000605 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000606 if( p->db->flags & SQLITE_VdbeListing ){
607 printf("VDBE Program Listing:\n");
608 for(i=0; i<p->nOp; i++){
609 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
610 }
drh3c23a882007-01-09 14:01:13 +0000611 }
drh84e55a82013-11-13 17:58:23 +0000612 if( p->db->flags & SQLITE_VdbeEQP ){
613 for(i=0; i<p->nOp; i++){
614 if( aOp[i].opcode==OP_Explain ){
615 if( once ) printf("VDBE Query Plan:\n");
616 printf("%s\n", aOp[i].p4.z);
617 once = 0;
618 }
619 }
620 }
621 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000622 }
danielk19772d1d86f2008-06-20 14:59:51 +0000623 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000624#endif
drhf56fa462015-04-13 21:39:54 +0000625 for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
626 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
drh17435752007-08-16 04:30:38 +0000627 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000628#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000629 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000630#endif
drhbf159fa2013-06-25 22:01:22 +0000631 nVmStep++;
dan6f9702e2014-11-01 20:38:06 +0000632#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
drhf56fa462015-04-13 21:39:54 +0000633 if( p->anExec ) p->anExec[(int)(pOp-aOp)]++;
dan6f9702e2014-11-01 20:38:06 +0000634#endif
drh6e142f52000-06-08 13:36:40 +0000635
danielk19778b60e0f2005-01-12 09:10:39 +0000636 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000637 */
danielk19778b60e0f2005-01-12 09:10:39 +0000638#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000639 if( db->flags & SQLITE_VdbeTrace ){
drhf56fa462015-04-13 21:39:54 +0000640 sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp);
drh75897232000-05-29 14:26:00 +0000641 }
drh3f7d4e42004-07-24 14:35:58 +0000642#endif
643
drh6e142f52000-06-08 13:36:40 +0000644
drhf6038712004-02-08 18:07:34 +0000645 /* Check to see if we need to simulate an interrupt. This only happens
646 ** if we have a special test build.
647 */
648#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000649 if( sqlite3_interrupt_count>0 ){
650 sqlite3_interrupt_count--;
651 if( sqlite3_interrupt_count==0 ){
652 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000653 }
654 }
655#endif
656
drh3c657212009-11-17 23:59:58 +0000657 /* Sanity checking on other operands */
658#ifdef SQLITE_DEBUG
drh27a348c2015-04-13 19:14:06 +0000659 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000660 if( (pOp->opflags & OPFLG_IN1)!=0 ){
661 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000662 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000663 assert( memIsValid(&aMem[pOp->p1]) );
drh75fd0542014-03-01 16:24:44 +0000664 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000665 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
666 }
667 if( (pOp->opflags & OPFLG_IN2)!=0 ){
668 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000669 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000670 assert( memIsValid(&aMem[pOp->p2]) );
drh75fd0542014-03-01 16:24:44 +0000671 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000672 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
673 }
674 if( (pOp->opflags & OPFLG_IN3)!=0 ){
675 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000676 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000677 assert( memIsValid(&aMem[pOp->p3]) );
drh75fd0542014-03-01 16:24:44 +0000678 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000679 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
680 }
681 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
682 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000683 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000684 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000685 }
686 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
687 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000688 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000689 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000690 }
691#endif
drh6dc41482015-04-16 17:31:02 +0000692#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
693 pOrigOp = pOp;
694#endif
drh93952eb2009-11-13 19:43:43 +0000695
drh75897232000-05-29 14:26:00 +0000696 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000697
drh5e00f6c2001-09-13 13:46:56 +0000698/*****************************************************************************
699** What follows is a massive switch statement where each case implements a
700** separate instruction in the virtual machine. If we follow the usual
701** indentation conventions, each case should be indented by 6 spaces. But
702** that is a lot of wasted space on the left margin. So the code within
703** the switch statement will break with convention and be flush-left. Another
704** big comment (similar to this one) will mark the point in the code where
705** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000706**
707** The formatting of each case is important. The makefile for SQLite
708** generates two C files "opcodes.h" and "opcodes.c" by scanning this
709** file looking for lines that begin with "case OP_". The opcodes.h files
710** will be filled with #defines that give unique integer values to each
711** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000712** each string is the symbolic name for the corresponding opcode. If the
713** case statement is followed by a comment of the form "/# same as ... #/"
714** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000715**
drh9cbf3422008-01-17 16:22:13 +0000716** Other keywords in the comment that follows each case are used to
717** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
drh27a348c2015-04-13 19:14:06 +0000718** Keywords include: in1, in2, in3, out2, out3. See
drh9cbf3422008-01-17 16:22:13 +0000719** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000720**
drhac82fcf2002-09-08 17:23:41 +0000721** Documentation about VDBE opcodes is generated by scanning this file
722** for lines of that contain "Opcode:". That line and all subsequent
723** comment lines are used in the generation of the opcode.html documentation
724** file.
725**
726** SUMMARY:
727**
728** Formatting is important to scripts that scan this file.
729** Do not deviate from the formatting style currently in use.
730**
drh5e00f6c2001-09-13 13:46:56 +0000731*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000732
drh9cbf3422008-01-17 16:22:13 +0000733/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000734**
735** An unconditional jump to address P2.
736** The next instruction executed will be
737** the one at index P2 from the beginning of
738** the program.
drhfe705102014-03-06 13:38:37 +0000739**
740** The P1 parameter is not actually used by this opcode. However, it
741** is sometimes set to 1 instead of 0 as a hint to the command-line shell
742** that this Goto is the bottom of a loop and that the lines from P2 down
743** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000744*/
drh9cbf3422008-01-17 16:22:13 +0000745case OP_Goto: { /* jump */
drhf56fa462015-04-13 21:39:54 +0000746jump_to_p2_and_check_for_interrupt:
747 pOp = &aOp[pOp->p2 - 1];
drh49afe3a2013-07-10 03:05:14 +0000748
749 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
750 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
751 ** completion. Check to see if sqlite3_interrupt() has been called
752 ** or if the progress callback needs to be invoked.
753 **
754 ** This code uses unstructured "goto" statements and does not look clean.
755 ** But that is not due to sloppy coding habits. The code is written this
756 ** way for performance, to avoid having to run the interrupt and progress
757 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
758 ** faster according to "valgrind --tool=cachegrind" */
759check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000760 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000761#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
762 /* Call the progress callback if it is configured and the required number
763 ** of VDBE ops have been executed (either since this invocation of
764 ** sqlite3VdbeExec() or since last time the progress callback was called).
765 ** If the progress callback returns non-zero, exit the virtual machine with
766 ** a return code SQLITE_ABORT.
767 */
drh0d1961e2013-07-25 16:27:51 +0000768 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000769 assert( db->nProgressOps!=0 );
770 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
771 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000772 rc = SQLITE_INTERRUPT;
773 goto vdbe_error_halt;
774 }
drh49afe3a2013-07-10 03:05:14 +0000775 }
776#endif
777
drh5e00f6c2001-09-13 13:46:56 +0000778 break;
779}
drh75897232000-05-29 14:26:00 +0000780
drh2eb95372008-06-06 15:04:36 +0000781/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000782**
drh2eb95372008-06-06 15:04:36 +0000783** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000784** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000785*/
drhb8475df2011-12-09 16:21:19 +0000786case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000787 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000788 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000789 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000790 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000791 pIn1->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000792 pIn1->u.i = (int)(pOp-aOp);
drh2eb95372008-06-06 15:04:36 +0000793 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000794
795 /* Most jump operations do a goto to this spot in order to update
796 ** the pOp pointer. */
797jump_to_p2:
798 pOp = &aOp[pOp->p2 - 1];
drh8c74a8c2002-08-25 19:20:40 +0000799 break;
800}
801
drh2eb95372008-06-06 15:04:36 +0000802/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000803**
drh81cf13e2014-02-07 18:27:53 +0000804** Jump to the next instruction after the address in register P1. After
805** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000806*/
drh2eb95372008-06-06 15:04:36 +0000807case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000808 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000809 assert( pIn1->flags==MEM_Int );
drhf56fa462015-04-13 21:39:54 +0000810 pOp = &aOp[pIn1->u.i];
drh81cf13e2014-02-07 18:27:53 +0000811 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000812 break;
813}
814
drhed71a832014-02-07 19:18:10 +0000815/* Opcode: InitCoroutine P1 P2 P3 * *
drhe00ee6e2008-06-20 15:24:01 +0000816**
drh5dad9a32014-07-25 18:37:42 +0000817** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000818** located at address P3.
819**
drh5dad9a32014-07-25 18:37:42 +0000820** If P2!=0 then the coroutine implementation immediately follows
821** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000822** address P2.
drh5dad9a32014-07-25 18:37:42 +0000823**
824** See also: EndCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000825*/
drh81cf13e2014-02-07 18:27:53 +0000826case OP_InitCoroutine: { /* jump */
drhed71a832014-02-07 19:18:10 +0000827 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
828 assert( pOp->p2>=0 && pOp->p2<p->nOp );
829 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000830 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000831 assert( !VdbeMemDynamic(pOut) );
832 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000833 pOut->flags = MEM_Int;
drhf56fa462015-04-13 21:39:54 +0000834 if( pOp->p2 ) goto jump_to_p2;
drh81cf13e2014-02-07 18:27:53 +0000835 break;
836}
837
838/* Opcode: EndCoroutine P1 * * * *
839**
drhbc5cf382014-08-06 01:08:07 +0000840** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000841** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000842** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000843**
844** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000845*/
846case OP_EndCoroutine: { /* in1 */
847 VdbeOp *pCaller;
848 pIn1 = &aMem[pOp->p1];
849 assert( pIn1->flags==MEM_Int );
850 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
851 pCaller = &aOp[pIn1->u.i];
852 assert( pCaller->opcode==OP_Yield );
853 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
drhf56fa462015-04-13 21:39:54 +0000854 pOp = &aOp[pCaller->p2 - 1];
drh81cf13e2014-02-07 18:27:53 +0000855 pIn1->flags = MEM_Undefined;
856 break;
857}
858
859/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000860**
drh5dad9a32014-07-25 18:37:42 +0000861** Swap the program counter with the value in register P1. This
862** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000863**
drh5dad9a32014-07-25 18:37:42 +0000864** If the coroutine that is launched by this instruction ends with
865** Yield or Return then continue to the next instruction. But if
866** the coroutine launched by this instruction ends with
867** EndCoroutine, then jump to P2 rather than continuing with the
868** next instruction.
869**
870** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000871*/
drh81cf13e2014-02-07 18:27:53 +0000872case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000873 int pcDest;
drh3c657212009-11-17 23:59:58 +0000874 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000875 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000876 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000877 pcDest = (int)pIn1->u.i;
drhf56fa462015-04-13 21:39:54 +0000878 pIn1->u.i = (int)(pOp - aOp);
drhe00ee6e2008-06-20 15:24:01 +0000879 REGISTER_TRACE(pOp->p1, pIn1);
drhf56fa462015-04-13 21:39:54 +0000880 pOp = &aOp[pcDest];
drhe00ee6e2008-06-20 15:24:01 +0000881 break;
882}
883
drhf9c8ce32013-11-05 13:33:55 +0000884/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh0fd61352014-02-07 02:29:45 +0000885** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000886**
drhef8662b2011-06-20 21:47:58 +0000887** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000888** parameter P1, P2, and P4 as if this were a Halt instruction. If the
889** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000890** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000891*/
892case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000893 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000894 if( (pIn3->flags & MEM_Null)==0 ) break;
895 /* Fall through into OP_Halt */
896}
drhe00ee6e2008-06-20 15:24:01 +0000897
drhf9c8ce32013-11-05 13:33:55 +0000898/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000899**
drh3d4501e2008-12-04 20:40:10 +0000900** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000901** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000902**
drh92f02c32004-09-02 14:57:08 +0000903** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
904** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
905** For errors, it can be some other value. If P1!=0 then P2 will determine
906** whether or not to rollback the current transaction. Do not rollback
907** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
908** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000909** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000910**
drh66a51672008-01-03 00:01:23 +0000911** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000912**
drhf9c8ce32013-11-05 13:33:55 +0000913** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
914**
915** 0: (no change)
916** 1: NOT NULL contraint failed: P4
917** 2: UNIQUE constraint failed: P4
918** 3: CHECK constraint failed: P4
919** 4: FOREIGN KEY constraint failed: P4
920**
921** If P5 is not zero and P4 is NULL, then everything after the ":" is
922** omitted.
923**
drh9cfcf5d2002-01-29 18:41:24 +0000924** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000925** every program. So a jump past the last instruction of the program
926** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000927*/
drh9cbf3422008-01-17 16:22:13 +0000928case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000929 const char *zType;
930 const char *zLogFmt;
drhf56fa462015-04-13 21:39:54 +0000931 VdbeFrame *pFrame;
932 int pcx;
drhf9c8ce32013-11-05 13:33:55 +0000933
drhf56fa462015-04-13 21:39:54 +0000934 pcx = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +0000935 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000936 /* Halt the sub-program. Return control to the parent frame. */
drhf56fa462015-04-13 21:39:54 +0000937 pFrame = p->pFrame;
dan165921a2009-08-28 18:53:45 +0000938 p->pFrame = pFrame->pParent;
939 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000940 sqlite3VdbeSetChanges(db, p->nChange);
drhf56fa462015-04-13 21:39:54 +0000941 pcx = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000942 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000943 if( pOp->p2==OE_Ignore ){
drhf56fa462015-04-13 21:39:54 +0000944 /* Instruction pcx is the OP_Program that invoked the sub-program
dan2832ad42009-08-31 15:27:27 +0000945 ** currently being halted. If the p2 instruction of this OP_Halt
946 ** instruction is set to OE_Ignore, then the sub-program is throwing
947 ** an IGNORE exception. In this case jump to the address specified
948 ** as the p2 of the calling OP_Program. */
drhf56fa462015-04-13 21:39:54 +0000949 pcx = p->aOp[pcx].p2-1;
dan165921a2009-08-28 18:53:45 +0000950 }
drhbbe879d2009-11-14 18:04:35 +0000951 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000952 aMem = p->aMem;
drhf56fa462015-04-13 21:39:54 +0000953 pOp = &aOp[pcx];
dan165921a2009-08-28 18:53:45 +0000954 break;
955 }
drh92f02c32004-09-02 14:57:08 +0000956 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000957 p->errorAction = (u8)pOp->p2;
drhf56fa462015-04-13 21:39:54 +0000958 p->pc = pcx;
drhf9c8ce32013-11-05 13:33:55 +0000959 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000960 if( pOp->p5 ){
961 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
962 "FOREIGN KEY" };
963 assert( pOp->p5>=1 && pOp->p5<=4 );
964 testcase( pOp->p5==1 );
965 testcase( pOp->p5==2 );
966 testcase( pOp->p5==3 );
967 testcase( pOp->p5==4 );
968 zType = azType[pOp->p5-1];
969 }else{
970 zType = 0;
971 }
drh4308e342013-11-11 16:55:52 +0000972 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000973 zLogFmt = "abort at %d in [%s]: %s";
974 if( zType && pOp->p4.z ){
drh22c17b82015-05-15 04:13:15 +0000975 sqlite3VdbeError(p, "%s constraint failed: %s", zType, pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000976 }else if( pOp->p4.z ){
drh22c17b82015-05-15 04:13:15 +0000977 sqlite3VdbeError(p, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000978 }else{
drh22c17b82015-05-15 04:13:15 +0000979 sqlite3VdbeError(p, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000980 }
drhf56fa462015-04-13 21:39:54 +0000981 sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000982 }
drh92f02c32004-09-02 14:57:08 +0000983 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000984 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000985 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000986 p->rc = rc = SQLITE_BUSY;
987 }else{
drhd91c1a12013-02-09 13:58:25 +0000988 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000989 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000990 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000991 }
drh900b31e2007-08-28 02:27:51 +0000992 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000993}
drhc61053b2000-06-04 12:58:36 +0000994
drh4c583122008-01-04 22:01:03 +0000995/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000996** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000997**
drh9cbf3422008-01-17 16:22:13 +0000998** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000999*/
drh27a348c2015-04-13 19:14:06 +00001000case OP_Integer: { /* out2 */
1001 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001002 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +00001003 break;
1004}
1005
drh4c583122008-01-04 22:01:03 +00001006/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001007** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +00001008**
drh66a51672008-01-03 00:01:23 +00001009** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +00001010** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +00001011*/
drh27a348c2015-04-13 19:14:06 +00001012case OP_Int64: { /* out2 */
1013 pOut = out2Prerelease(p, pOp);
danielk19772dca4ac2008-01-03 11:50:29 +00001014 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +00001015 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +00001016 break;
1017}
drh4f26d6c2004-05-26 23:25:30 +00001018
drh13573c72010-01-12 17:04:07 +00001019#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001020/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001021** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001022**
drh4c583122008-01-04 22:01:03 +00001023** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001024** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001025*/
drh27a348c2015-04-13 19:14:06 +00001026case OP_Real: { /* same as TK_FLOAT, out2 */
1027 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001028 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001029 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001030 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001031 break;
1032}
drh13573c72010-01-12 17:04:07 +00001033#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001034
drh3c84ddf2008-01-09 02:15:38 +00001035/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001036** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001037**
drh66a51672008-01-03 00:01:23 +00001038** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhf07cf6e2015-03-06 16:45:16 +00001039** into a String opcode before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001040** this transformation, the length of string P4 is computed and stored
1041** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001042*/
drh27a348c2015-04-13 19:14:06 +00001043case OP_String8: { /* same as TK_STRING, out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001044 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001045 pOut = out2Prerelease(p, pOp);
drhed2df7f2005-11-16 04:34:32 +00001046 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001047 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001048
1049#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001050 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001051 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1052 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001053 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001054 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001055 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001056 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001057 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001058 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001059 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001060 }
drh66a51672008-01-03 00:01:23 +00001061 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001062 pOp->p4.z = pOut->z;
1063 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001064 }
danielk197793758c82005-01-21 08:13:14 +00001065#endif
drhbb4957f2008-03-20 14:03:29 +00001066 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001067 goto too_big;
1068 }
1069 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001070}
drhf4479502004-05-27 03:12:53 +00001071
drhf07cf6e2015-03-06 16:45:16 +00001072/* Opcode: String P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00001073** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001074**
drh9cbf3422008-01-17 16:22:13 +00001075** The string value P4 of length P1 (bytes) is stored in register P2.
drhf07cf6e2015-03-06 16:45:16 +00001076**
1077** If P5!=0 and the content of register P3 is greater than zero, then
drha9c18a92015-03-06 20:49:52 +00001078** the datatype of the register P2 is converted to BLOB. The content is
1079** the same sequence of bytes, it is merely interpreted as a BLOB instead
1080** of a string, as if it had been CAST.
drhf4479502004-05-27 03:12:53 +00001081*/
drh27a348c2015-04-13 19:14:06 +00001082case OP_String: { /* out2 */
danielk19772dca4ac2008-01-03 11:50:29 +00001083 assert( pOp->p4.z!=0 );
drh27a348c2015-04-13 19:14:06 +00001084 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001085 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1086 pOut->z = pOp->p4.z;
1087 pOut->n = pOp->p1;
1088 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001089 UPDATE_MAX_BLOBSIZE(pOut);
drhf07cf6e2015-03-06 16:45:16 +00001090 if( pOp->p5 ){
1091 assert( pOp->p3>0 );
1092 assert( pOp->p3<=(p->nMem-p->nCursor) );
1093 pIn3 = &aMem[pOp->p3];
1094 assert( pIn3->flags & MEM_Int );
1095 if( pIn3->u.i ) pOut->flags = MEM_Blob|MEM_Static|MEM_Term;
1096 }
danielk1977c572ef72004-05-27 09:28:41 +00001097 break;
1098}
1099
drh053a1282012-09-19 21:15:46 +00001100/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001101** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001102**
drhb8475df2011-12-09 16:21:19 +00001103** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001104** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001105** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001106** set to NULL.
1107**
1108** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1109** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1110** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001111*/
drh27a348c2015-04-13 19:14:06 +00001112case OP_Null: { /* out2 */
drhb8475df2011-12-09 16:21:19 +00001113 int cnt;
drh053a1282012-09-19 21:15:46 +00001114 u16 nullFlag;
drh27a348c2015-04-13 19:14:06 +00001115 pOut = out2Prerelease(p, pOp);
drhb8475df2011-12-09 16:21:19 +00001116 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001117 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001118 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +00001119 while( cnt>0 ){
1120 pOut++;
1121 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001122 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001123 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001124 cnt--;
1125 }
drhf0863fe2005-06-12 21:35:51 +00001126 break;
1127}
1128
drh05a86c52014-02-16 01:55:49 +00001129/* Opcode: SoftNull P1 * * * *
1130** Synopsis: r[P1]=NULL
1131**
1132** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1133** instruction, but do not free any string or blob memory associated with
1134** the register, so that if the value was a string or blob that was
1135** previously copied using OP_SCopy, the copies will continue to be valid.
1136*/
1137case OP_SoftNull: {
1138 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
1139 pOut = &aMem[pOp->p1];
1140 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1141 break;
1142}
drhf0863fe2005-06-12 21:35:51 +00001143
drha5750cf2014-02-07 13:20:31 +00001144/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001145** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001146**
drh9de221d2008-01-05 06:51:30 +00001147** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001148** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001149*/
drh27a348c2015-04-13 19:14:06 +00001150case OP_Blob: { /* out2 */
drhcbd2da92007-12-17 16:20:06 +00001151 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh27a348c2015-04-13 19:14:06 +00001152 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00001153 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001154 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001155 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001156 break;
1157}
1158
drheaf52d82010-05-12 13:50:23 +00001159/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001160** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001161**
drheaf52d82010-05-12 13:50:23 +00001162** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001163**
drh0fd61352014-02-07 02:29:45 +00001164** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001165** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001166*/
drh27a348c2015-04-13 19:14:06 +00001167case OP_Variable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00001168 Mem *pVar; /* Value being transferred */
1169
drheaf52d82010-05-12 13:50:23 +00001170 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001171 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001172 pVar = &p->aVar[pOp->p1 - 1];
1173 if( sqlite3VdbeMemTooBig(pVar) ){
1174 goto too_big;
drh023ae032007-05-08 12:12:16 +00001175 }
drh27a348c2015-04-13 19:14:06 +00001176 pOut = out2Prerelease(p, pOp);
drheaf52d82010-05-12 13:50:23 +00001177 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1178 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001179 break;
1180}
danielk1977295ba552004-05-19 10:34:51 +00001181
drhb21e7c72008-06-22 12:37:57 +00001182/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001183** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001184**
drh079a3072014-03-19 14:10:55 +00001185** Move the P3 values in register P1..P1+P3-1 over into
1186** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001187** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001188** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1189** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001190*/
drhe1349cb2008-04-01 00:36:10 +00001191case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001192 int n; /* Number of registers left to copy */
1193 int p1; /* Register to copy from */
1194 int p2; /* Register to copy to */
1195
drhe09f43f2013-11-21 04:18:31 +00001196 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001197 p1 = pOp->p1;
1198 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001199 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001200 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001201
drha6c2ed92009-11-14 23:22:23 +00001202 pIn1 = &aMem[p1];
1203 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001204 do{
dan3bc9f742013-08-15 16:18:39 +00001205 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1206 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001207 assert( memIsValid(pIn1) );
1208 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001209 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001210#ifdef SQLITE_DEBUG
drhbd6789e2015-04-28 14:00:02 +00001211 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){
drh5fb71252015-04-28 12:44:55 +00001212 pOut->pScopyFrom += pOp->p2 - p1;
drh52043d72011-08-03 16:40:15 +00001213 }
1214#endif
drhbd6789e2015-04-28 14:00:02 +00001215 Deephemeralize(pOut);
drhb21e7c72008-06-22 12:37:57 +00001216 REGISTER_TRACE(p2++, pOut);
1217 pIn1++;
1218 pOut++;
drh079a3072014-03-19 14:10:55 +00001219 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001220 break;
1221}
1222
drhe8e4af72012-09-21 00:04:28 +00001223/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001224** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001225**
drhe8e4af72012-09-21 00:04:28 +00001226** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001227**
1228** This instruction makes a deep copy of the value. A duplicate
1229** is made of any string or blob constant. See also OP_SCopy.
1230*/
drhe8e4af72012-09-21 00:04:28 +00001231case OP_Copy: {
1232 int n;
1233
1234 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001235 pIn1 = &aMem[pOp->p1];
1236 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001237 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001238 while( 1 ){
1239 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1240 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001241#ifdef SQLITE_DEBUG
1242 pOut->pScopyFrom = 0;
1243#endif
drhe8e4af72012-09-21 00:04:28 +00001244 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1245 if( (n--)==0 ) break;
1246 pOut++;
1247 pIn1++;
1248 }
drhe1349cb2008-04-01 00:36:10 +00001249 break;
1250}
1251
drhb1fdb2a2008-01-05 04:06:03 +00001252/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001253** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001254**
drh9cbf3422008-01-17 16:22:13 +00001255** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001256**
1257** This instruction makes a shallow copy of the value. If the value
1258** is a string or blob, then the copy is only a pointer to the
1259** original and hence if the original changes so will the copy.
1260** Worse, if the original is deallocated, the copy becomes invalid.
1261** Thus the program must guarantee that the original will not change
1262** during the lifetime of the copy. Use OP_Copy to make a complete
1263** copy.
1264*/
drh26198bb2013-10-31 11:15:09 +00001265case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001266 pIn1 = &aMem[pOp->p1];
1267 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001268 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001269 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001270#ifdef SQLITE_DEBUG
1271 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1272#endif
drh5e00f6c2001-09-13 13:46:56 +00001273 break;
1274}
drh75897232000-05-29 14:26:00 +00001275
drhfed7ac62015-10-15 18:04:59 +00001276/* Opcode: IntCopy P1 P2 * * *
1277** Synopsis: r[P2]=r[P1]
1278**
1279** Transfer the integer value held in register P1 into register P2.
1280**
1281** This is an optimized version of SCopy that works only for integer
1282** values.
1283*/
1284case OP_IntCopy: { /* out2 */
1285 pIn1 = &aMem[pOp->p1];
1286 assert( (pIn1->flags & MEM_Int)!=0 );
1287 pOut = &aMem[pOp->p2];
1288 sqlite3VdbeMemSetInt64(pOut, pIn1->u.i);
1289 break;
1290}
1291
drh9cbf3422008-01-17 16:22:13 +00001292/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001293** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001294**
shane21e7feb2008-05-30 15:59:49 +00001295** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001296** results. This opcode causes the sqlite3_step() call to terminate
1297** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001298** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001299** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001300*/
drh9cbf3422008-01-17 16:22:13 +00001301case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001302 Mem *pMem;
1303 int i;
1304 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001305 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001306 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001307
drhe6400b92013-11-13 23:48:46 +00001308#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1309 /* Run the progress counter just before returning.
1310 */
1311 if( db->xProgress!=0
1312 && nVmStep>=nProgressLimit
1313 && db->xProgress(db->pProgressArg)!=0
1314 ){
1315 rc = SQLITE_INTERRUPT;
1316 goto vdbe_error_halt;
1317 }
1318#endif
1319
dan32b09f22009-09-23 17:29:59 +00001320 /* If this statement has violated immediate foreign key constraints, do
1321 ** not return the number of rows modified. And do not RELEASE the statement
1322 ** transaction. It needs to be rolled back. */
1323 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1324 assert( db->flags&SQLITE_CountRows );
1325 assert( p->usesStmtJournal );
1326 break;
1327 }
1328
danielk1977bd434552009-03-18 10:33:00 +00001329 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1330 ** DML statements invoke this opcode to return the number of rows
1331 ** modified to the user. This is the only way that a VM that
1332 ** opens a statement transaction may invoke this opcode.
1333 **
1334 ** In case this is such a statement, close any statement transaction
1335 ** opened by this VM before returning control to the user. This is to
1336 ** ensure that statement-transactions are always nested, not overlapping.
1337 ** If the open statement-transaction is not closed here, then the user
1338 ** may step another VM that opens its own statement transaction. This
1339 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001340 **
1341 ** The statement transaction is never a top-level transaction. Hence
1342 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001343 */
1344 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001345 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1346 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001347 break;
1348 }
1349
drhd4e70eb2008-01-02 00:34:36 +00001350 /* Invalidate all ephemeral cursor row caches */
1351 p->cacheCtr = (p->cacheCtr + 2)|1;
1352
1353 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001354 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001355 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001356 */
drha6c2ed92009-11-14 23:22:23 +00001357 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001358 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001359 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001360 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001361 assert( (pMem[i].flags & MEM_Ephem)==0
1362 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001363 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001364 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001365 }
drh28039692008-03-17 16:54:01 +00001366 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001367
1368 /* Return SQLITE_ROW
1369 */
drhf56fa462015-04-13 21:39:54 +00001370 p->pc = (int)(pOp - aOp) + 1;
drhd4e70eb2008-01-02 00:34:36 +00001371 rc = SQLITE_ROW;
1372 goto vdbe_return;
1373}
1374
drh5b6afba2008-01-05 16:29:28 +00001375/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001376** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001377**
drh5b6afba2008-01-05 16:29:28 +00001378** Add the text in register P1 onto the end of the text in
1379** register P2 and store the result in register P3.
1380** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001381**
1382** P3 = P2 || P1
1383**
1384** It is illegal for P1 and P3 to be the same register. Sometimes,
1385** if P3 is the same register as P2, the implementation is able
1386** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001387*/
drh5b6afba2008-01-05 16:29:28 +00001388case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001389 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001390
drh3c657212009-11-17 23:59:58 +00001391 pIn1 = &aMem[pOp->p1];
1392 pIn2 = &aMem[pOp->p2];
1393 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001394 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001395 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001396 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001397 break;
drh5e00f6c2001-09-13 13:46:56 +00001398 }
drha0c06522009-06-17 22:50:41 +00001399 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001400 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001401 Stringify(pIn2, encoding);
1402 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001403 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001404 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001405 }
drh9c1905f2008-12-10 22:32:56 +00001406 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001407 goto no_mem;
1408 }
drhc91b2fd2014-03-01 18:13:23 +00001409 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001410 if( pOut!=pIn2 ){
1411 memcpy(pOut->z, pIn2->z, pIn2->n);
1412 }
1413 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001414 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001415 pOut->z[nByte+1] = 0;
1416 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001417 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001418 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001419 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001420 break;
1421}
drh75897232000-05-29 14:26:00 +00001422
drh3c84ddf2008-01-09 02:15:38 +00001423/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001424** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001425**
drh60a713c2008-01-21 16:22:45 +00001426** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001427** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001428** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001429*/
drh3c84ddf2008-01-09 02:15:38 +00001430/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001431** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001432**
drh3c84ddf2008-01-09 02:15:38 +00001433**
shane21e7feb2008-05-30 15:59:49 +00001434** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001435** and store the result in register P3.
1436** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001437*/
drh3c84ddf2008-01-09 02:15:38 +00001438/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001439** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001440**
drh60a713c2008-01-21 16:22:45 +00001441** Subtract the value in register P1 from the value in register P2
1442** and store the result in register P3.
1443** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001444*/
drh9cbf3422008-01-17 16:22:13 +00001445/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001446** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001447**
drh60a713c2008-01-21 16:22:45 +00001448** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001449** and store the result in register P3 (P3=P2/P1). If the value in
1450** register P1 is zero, then the result is NULL. If either input is
1451** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001452*/
drh9cbf3422008-01-17 16:22:13 +00001453/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001454** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001455**
drh40864a12013-11-15 18:58:37 +00001456** Compute the remainder after integer register P2 is divided by
1457** register P1 and store the result in register P3.
1458** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001459** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001460*/
drh5b6afba2008-01-05 16:29:28 +00001461case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1462case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1463case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1464case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1465case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001466 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001467 u16 flags; /* Combined MEM_* flags from both inputs */
1468 u16 type1; /* Numeric type of left operand */
1469 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001470 i64 iA; /* Integer value of left operand */
1471 i64 iB; /* Integer value of right operand */
1472 double rA; /* Real value of left operand */
1473 double rB; /* Real value of right operand */
1474
drh3c657212009-11-17 23:59:58 +00001475 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001476 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001477 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001478 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001479 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001480 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001481 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
drh3d1d90a2014-03-24 15:00:15 +00001482 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001483 iA = pIn1->u.i;
1484 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001485 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001486 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001487 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1488 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1489 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001490 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001491 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001492 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001493 iB /= iA;
drh75897232000-05-29 14:26:00 +00001494 break;
1495 }
drhbf4133c2001-10-13 02:59:08 +00001496 default: {
drh856c1032009-06-02 15:21:42 +00001497 if( iA==0 ) goto arithmetic_result_is_null;
1498 if( iA==-1 ) iA = 1;
1499 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001500 break;
1501 }
drh75897232000-05-29 14:26:00 +00001502 }
drh856c1032009-06-02 15:21:42 +00001503 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001504 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001505 }else{
drhbe707b32012-12-10 22:19:14 +00001506 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001507fp_math:
drh856c1032009-06-02 15:21:42 +00001508 rA = sqlite3VdbeRealValue(pIn1);
1509 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001510 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001511 case OP_Add: rB += rA; break;
1512 case OP_Subtract: rB -= rA; break;
1513 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001514 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001515 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001516 if( rA==(double)0 ) goto arithmetic_result_is_null;
1517 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001518 break;
1519 }
drhbf4133c2001-10-13 02:59:08 +00001520 default: {
shane75ac1de2009-06-09 18:58:52 +00001521 iA = (i64)rA;
1522 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001523 if( iA==0 ) goto arithmetic_result_is_null;
1524 if( iA==-1 ) iA = 1;
1525 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001526 break;
1527 }
drh5e00f6c2001-09-13 13:46:56 +00001528 }
drhc5a7b512010-01-13 16:25:42 +00001529#ifdef SQLITE_OMIT_FLOATING_POINT
1530 pOut->u.i = rB;
1531 MemSetTypeFlag(pOut, MEM_Int);
1532#else
drh856c1032009-06-02 15:21:42 +00001533 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001534 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001535 }
drh74eaba42014-09-18 17:52:15 +00001536 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001537 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001538 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001539 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001540 }
drhc5a7b512010-01-13 16:25:42 +00001541#endif
drh5e00f6c2001-09-13 13:46:56 +00001542 }
1543 break;
1544
drha05a7222008-01-19 03:35:58 +00001545arithmetic_result_is_null:
1546 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001547 break;
1548}
1549
drh7a957892012-02-02 17:35:43 +00001550/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001551**
drh66a51672008-01-03 00:01:23 +00001552** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001553** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1554** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001555** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001556**
drh7a957892012-02-02 17:35:43 +00001557** If P1 is not zero, then it is a register that a subsequent min() or
1558** max() aggregate will set to 1 if the current row is not the minimum or
1559** maximum. The P1 register is initialized to 0 by this instruction.
1560**
danielk1977dc1bdc42004-06-11 10:51:27 +00001561** The interface used by the implementation of the aforementioned functions
1562** to retrieve the collation sequence set by this opcode is not available
drh0a0d0562015-03-12 05:08:34 +00001563** publicly. Only built-in functions have access to this feature.
danielk1977dc1bdc42004-06-11 10:51:27 +00001564*/
drh9cbf3422008-01-17 16:22:13 +00001565case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001566 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001567 if( pOp->p1 ){
1568 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1569 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001570 break;
1571}
1572
drh9c7c9132015-06-26 18:16:52 +00001573/* Opcode: Function0 P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001574** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001575**
drhe2d9e7c2015-06-26 18:47:53 +00001576** Invoke a user function (P4 is a pointer to a FuncDef object that
drh98757152008-01-09 23:04:12 +00001577** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001578** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001579** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001580**
drh13449892005-09-07 21:22:45 +00001581** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001582** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001583** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001584** whether meta data associated with a user function argument using the
1585** sqlite3_set_auxdata() API may be safely retained until the next
1586** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001587**
drh9c7c9132015-06-26 18:16:52 +00001588** See also: Function, AggStep, AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001589*/
drh9c7c9132015-06-26 18:16:52 +00001590/* Opcode: Function P1 P2 P3 P4 P5
1591** Synopsis: r[P3]=func(r[P2@P5])
1592**
1593** Invoke a user function (P4 is a pointer to an sqlite3_context object that
1594** contains a pointer to the function to be run) with P5 arguments taken
1595** from register P2 and successors. The result of the function is stored
1596** in register P3. Register P3 must not be one of the function inputs.
1597**
1598** P1 is a 32-bit bitmask indicating whether or not each argument to the
1599** function was determined to be constant at compile time. If the first
1600** argument was constant then bit 0 of P1 is set. This is used to determine
1601** whether meta data associated with a user function argument using the
1602** sqlite3_set_auxdata() API may be safely retained until the next
1603** invocation of this opcode.
1604**
1605** SQL functions are initially coded as OP_Function0 with P4 pointing
drhe2d9e7c2015-06-26 18:47:53 +00001606** to a FuncDef object. But on first evaluation, the P4 operand is
drh9c7c9132015-06-26 18:16:52 +00001607** automatically converted into an sqlite3_context object and the operation
1608** changed to this OP_Function opcode. In this way, the initialization of
1609** the sqlite3_context object occurs only once, rather than once for each
1610** evaluation of the function.
1611**
1612** See also: Function0, AggStep, AggFinal
1613*/
1614case OP_Function0: {
drh856c1032009-06-02 15:21:42 +00001615 int n;
drh9c7c9132015-06-26 18:16:52 +00001616 sqlite3_context *pCtx;
danielk197751ad0ec2004-05-24 12:39:02 +00001617
dan0c547792013-07-18 17:12:08 +00001618 assert( pOp->p4type==P4_FUNCDEF );
drh9c7c9132015-06-26 18:16:52 +00001619 n = pOp->p5;
1620 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
1621 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
1622 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
1623 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
1624 if( pCtx==0 ) goto no_mem;
1625 pCtx->pOut = 0;
1626 pCtx->pFunc = pOp->p4.pFunc;
1627 pCtx->iOp = (int)(pOp - aOp);
1628 pCtx->pVdbe = p;
1629 pCtx->argc = n;
1630 pOp->p4type = P4_FUNCCTX;
1631 pOp->p4.pCtx = pCtx;
1632 pOp->opcode = OP_Function;
1633 /* Fall through into OP_Function */
1634}
1635case OP_Function: {
1636 int i;
1637 sqlite3_context *pCtx;
danielk1977a7a8e142008-02-13 18:25:27 +00001638
drh9c7c9132015-06-26 18:16:52 +00001639 assert( pOp->p4type==P4_FUNCCTX );
1640 pCtx = pOp->p4.pCtx;
danielk1977a7a8e142008-02-13 18:25:27 +00001641
drh9c7c9132015-06-26 18:16:52 +00001642 /* If this function is inside of a trigger, the register array in aMem[]
1643 ** might change from one evaluation to the next. The next block of code
1644 ** checks to see if the register array has changed, and if so it
1645 ** reinitializes the relavant parts of the sqlite3_context object */
drhe2d9e7c2015-06-26 18:47:53 +00001646 pOut = &aMem[pOp->p3];
1647 if( pCtx->pOut != pOut ){
1648 pCtx->pOut = pOut;
drh9c7c9132015-06-26 18:16:52 +00001649 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
danielk1977dc1bdc42004-06-11 10:51:27 +00001650 }
drh9c7c9132015-06-26 18:16:52 +00001651
1652 memAboutToChange(p, pCtx->pOut);
1653#ifdef SQLITE_DEBUG
1654 for(i=0; i<pCtx->argc; i++){
1655 assert( memIsValid(pCtx->argv[i]) );
1656 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
1657 }
1658#endif
1659 MemSetTypeFlag(pCtx->pOut, MEM_Null);
1660 pCtx->fErrorOrAux = 0;
drh99a66922011-05-13 18:51:42 +00001661 db->lastRowid = lastRowid;
drh9c7c9132015-06-26 18:16:52 +00001662 (*pCtx->pFunc->xFunc)(pCtx, pCtx->argc, pCtx->argv); /* IMP: R-24505-23230 */
drh3b130be2014-09-26 01:10:02 +00001663 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
dan5f84e142011-06-14 14:18:45 +00001664
drh90669c12006-01-20 15:45:36 +00001665 /* If the function returned an error, throw an exception */
drh9c7c9132015-06-26 18:16:52 +00001666 if( pCtx->fErrorOrAux ){
1667 if( pCtx->isError ){
1668 sqlite3VdbeError(p, "%s", sqlite3_value_text(pCtx->pOut));
1669 rc = pCtx->isError;
drh9b47ee32013-08-20 03:13:51 +00001670 }
drhe2d9e7c2015-06-26 18:47:53 +00001671 sqlite3VdbeDeleteAuxData(p, pCtx->iOp, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001672 }
1673
drh9cbf3422008-01-17 16:22:13 +00001674 /* Copy the result of the function into register P3 */
drhe2d9e7c2015-06-26 18:47:53 +00001675 if( pOut->flags & (MEM_Str|MEM_Blob) ){
1676 sqlite3VdbeChangeEncoding(pCtx->pOut, encoding);
1677 if( sqlite3VdbeMemTooBig(pCtx->pOut) ) goto too_big;
drh023ae032007-05-08 12:12:16 +00001678 }
drh7b94e7f2011-04-04 12:29:20 +00001679
drh9c7c9132015-06-26 18:16:52 +00001680 REGISTER_TRACE(pOp->p3, pCtx->pOut);
1681 UPDATE_MAX_BLOBSIZE(pCtx->pOut);
drh8e0a2f92002-02-23 23:45:45 +00001682 break;
1683}
1684
drh98757152008-01-09 23:04:12 +00001685/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001686** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001687**
drh98757152008-01-09 23:04:12 +00001688** Take the bit-wise AND of the values in register P1 and P2 and
1689** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001690** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001691*/
drh98757152008-01-09 23:04:12 +00001692/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001693** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001694**
drh98757152008-01-09 23:04:12 +00001695** Take the bit-wise OR of the values in register P1 and P2 and
1696** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001697** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001698*/
drh98757152008-01-09 23:04:12 +00001699/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001700** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001701**
drh98757152008-01-09 23:04:12 +00001702** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001703** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001704** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001705** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001706*/
drh98757152008-01-09 23:04:12 +00001707/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001708** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001709**
drh98757152008-01-09 23:04:12 +00001710** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001711** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001712** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001713** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001714*/
drh5b6afba2008-01-05 16:29:28 +00001715case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1716case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1717case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1718case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001719 i64 iA;
1720 u64 uA;
1721 i64 iB;
1722 u8 op;
drh6810ce62004-01-31 19:22:56 +00001723
drh3c657212009-11-17 23:59:58 +00001724 pIn1 = &aMem[pOp->p1];
1725 pIn2 = &aMem[pOp->p2];
1726 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001727 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001728 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001729 break;
1730 }
drh158b9cb2011-03-05 20:59:46 +00001731 iA = sqlite3VdbeIntValue(pIn2);
1732 iB = sqlite3VdbeIntValue(pIn1);
1733 op = pOp->opcode;
1734 if( op==OP_BitAnd ){
1735 iA &= iB;
1736 }else if( op==OP_BitOr ){
1737 iA |= iB;
1738 }else if( iB!=0 ){
1739 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1740
1741 /* If shifting by a negative amount, shift in the other direction */
1742 if( iB<0 ){
1743 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1744 op = 2*OP_ShiftLeft + 1 - op;
1745 iB = iB>(-64) ? -iB : 64;
1746 }
1747
1748 if( iB>=64 ){
1749 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1750 }else{
1751 memcpy(&uA, &iA, sizeof(uA));
1752 if( op==OP_ShiftLeft ){
1753 uA <<= iB;
1754 }else{
1755 uA >>= iB;
1756 /* Sign-extend on a right shift of a negative number */
1757 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1758 }
1759 memcpy(&iA, &uA, sizeof(iA));
1760 }
drhbf4133c2001-10-13 02:59:08 +00001761 }
drh158b9cb2011-03-05 20:59:46 +00001762 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001763 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001764 break;
1765}
1766
drh8558cde2008-01-05 05:20:10 +00001767/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001768** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001769**
danielk19770cdc0222008-06-26 18:04:03 +00001770** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001771** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001772**
drh8558cde2008-01-05 05:20:10 +00001773** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001774*/
drh9cbf3422008-01-17 16:22:13 +00001775case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001776 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001777 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001778 sqlite3VdbeMemIntegerify(pIn1);
1779 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001780 break;
1781}
1782
drh9cbf3422008-01-17 16:22:13 +00001783/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001784**
drh9cbf3422008-01-17 16:22:13 +00001785** Force the value in register P1 to be an integer. If the value
1786** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001787** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001788** raise an SQLITE_MISMATCH exception.
1789*/
drh9cbf3422008-01-17 16:22:13 +00001790case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001791 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001792 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001793 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001794 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001795 if( (pIn1->flags & MEM_Int)==0 ){
1796 if( pOp->p2==0 ){
1797 rc = SQLITE_MISMATCH;
1798 goto abort_due_to_error;
1799 }else{
drhf56fa462015-04-13 21:39:54 +00001800 goto jump_to_p2;
drh83b301b2013-11-20 00:59:02 +00001801 }
drh8aff1012001-12-22 14:49:24 +00001802 }
drh8aff1012001-12-22 14:49:24 +00001803 }
drh83b301b2013-11-20 00:59:02 +00001804 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001805 break;
1806}
1807
drh13573c72010-01-12 17:04:07 +00001808#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001809/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001810**
drh2133d822008-01-03 18:44:59 +00001811** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001812**
drh8a512562005-11-14 22:29:05 +00001813** This opcode is used when extracting information from a column that
1814** has REAL affinity. Such column values may still be stored as
1815** integers, for space efficiency, but after extraction we want them
1816** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001817*/
drh9cbf3422008-01-17 16:22:13 +00001818case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001819 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001820 if( pIn1->flags & MEM_Int ){
1821 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001822 }
drh487e2622005-06-25 18:42:14 +00001823 break;
1824}
drh13573c72010-01-12 17:04:07 +00001825#endif
drh487e2622005-06-25 18:42:14 +00001826
drh8df447f2005-11-01 15:48:24 +00001827#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001828/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001829** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001830**
drh4169e432014-08-25 20:11:52 +00001831** Force the value in register P1 to be the type defined by P2.
1832**
1833** <ul>
1834** <li value="97"> TEXT
1835** <li value="98"> BLOB
1836** <li value="99"> NUMERIC
1837** <li value="100"> INTEGER
1838** <li value="101"> REAL
1839** </ul>
drh487e2622005-06-25 18:42:14 +00001840**
1841** A NULL value is not changed by this routine. It remains NULL.
1842*/
drh4169e432014-08-25 20:11:52 +00001843case OP_Cast: { /* in1 */
drh05883a32015-06-02 15:32:08 +00001844 assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001845 testcase( pOp->p2==SQLITE_AFF_TEXT );
drh05883a32015-06-02 15:32:08 +00001846 testcase( pOp->p2==SQLITE_AFF_BLOB );
drh05bbb2e2014-08-25 22:37:19 +00001847 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1848 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1849 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001850 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001851 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001852 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001853 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001854 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001855 break;
1856}
drh8a512562005-11-14 22:29:05 +00001857#endif /* SQLITE_OMIT_CAST */
1858
drh35573352008-01-08 23:54:25 +00001859/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001860** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001861**
drh35573352008-01-08 23:54:25 +00001862** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1863** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001864**
drh35573352008-01-08 23:54:25 +00001865** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1866** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001867** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001868**
drh35573352008-01-08 23:54:25 +00001869** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001870** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001871** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001872** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001873** affinity is used. Note that the affinity conversions are stored
1874** back into the input registers P1 and P3. So this opcode can cause
1875** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001876**
1877** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001878** the values are compared. If both values are blobs then memcmp() is
1879** used to determine the results of the comparison. If both values
1880** are text, then the appropriate collating function specified in
1881** P4 is used to do the comparison. If P4 is not specified then
1882** memcmp() is used to compare text string. If both values are
1883** numeric, then a numeric comparison is used. If the two values
1884** are of different types, then numbers are considered less than
1885** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001886**
drh35573352008-01-08 23:54:25 +00001887** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1888** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001889**
1890** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1891** equal to one another, provided that they do not have their MEM_Cleared
1892** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001893*/
drh9cbf3422008-01-17 16:22:13 +00001894/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001895** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001896**
drh35573352008-01-08 23:54:25 +00001897** This works just like the Lt opcode except that the jump is taken if
1898** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001899** additional information.
drh6a2fe092009-09-23 02:29:36 +00001900**
1901** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1902** true or false and is never NULL. If both operands are NULL then the result
1903** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001904** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001905** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001906*/
drh9cbf3422008-01-17 16:22:13 +00001907/* Opcode: Eq 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 equal.
1912** See the Lt opcode for 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 true. If either operand is NULL then the result is false.
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: Le 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 content of register P3 is less than or equal to the content of
1925** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001926*/
drh9cbf3422008-01-17 16:22:13 +00001927/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001928** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001929**
drh35573352008-01-08 23:54:25 +00001930** This works just like the Lt opcode except that the jump is taken if
1931** the content of register P3 is greater than the content of
1932** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001933*/
drh9cbf3422008-01-17 16:22:13 +00001934/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001935** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001936**
drh35573352008-01-08 23:54:25 +00001937** This works just like the Lt opcode except that the jump is taken if
1938** the content of register P3 is greater than or equal to the content of
1939** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001940*/
drh9cbf3422008-01-17 16:22:13 +00001941case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1942case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1943case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1944case OP_Le: /* same as TK_LE, jump, in1, in3 */
1945case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1946case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001947 int res; /* Result of the comparison of pIn1 against pIn3 */
1948 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001949 u16 flags1; /* Copy of initial value of pIn1->flags */
1950 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001951
drh3c657212009-11-17 23:59:58 +00001952 pIn1 = &aMem[pOp->p1];
1953 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001954 flags1 = pIn1->flags;
1955 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001956 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001957 /* One or both operands are NULL */
1958 if( pOp->p5 & SQLITE_NULLEQ ){
1959 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1960 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1961 ** or not both operands are null.
1962 */
1963 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001964 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001965 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drh053a1282012-09-19 21:15:46 +00001966 if( (flags1&MEM_Null)!=0
1967 && (flags3&MEM_Null)!=0
1968 && (flags3&MEM_Cleared)==0
1969 ){
1970 res = 0; /* Results are equal */
1971 }else{
1972 res = 1; /* Results are not equal */
1973 }
drh6a2fe092009-09-23 02:29:36 +00001974 }else{
1975 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1976 ** then the result is always NULL.
1977 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1978 */
drh688852a2014-02-17 22:40:43 +00001979 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001980 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001981 MemSetTypeFlag(pOut, MEM_Null);
1982 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001983 }else{
drhf4345e42014-02-18 11:31:59 +00001984 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001985 if( pOp->p5 & SQLITE_JUMPIFNULL ){
drhf56fa462015-04-13 21:39:54 +00001986 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00001987 }
drh6a2fe092009-09-23 02:29:36 +00001988 }
1989 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001990 }
drh6a2fe092009-09-23 02:29:36 +00001991 }else{
1992 /* Neither operand is NULL. Do a comparison. */
1993 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001994 if( affinity>=SQLITE_AFF_NUMERIC ){
drhe7a34662014-09-19 22:44:20 +00001995 if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001996 applyNumericAffinity(pIn1,0);
1997 }
drhe7a34662014-09-19 22:44:20 +00001998 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001999 applyNumericAffinity(pIn3,0);
2000 }
2001 }else if( affinity==SQLITE_AFF_TEXT ){
2002 if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002003 testcase( pIn1->flags & MEM_Int );
2004 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002005 sqlite3VdbeMemStringify(pIn1, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002006 testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
2007 flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002008 }
2009 if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00002010 testcase( pIn3->flags & MEM_Int );
2011 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00002012 sqlite3VdbeMemStringify(pIn3, encoding, 1);
drhbc8a6b32015-03-31 11:42:23 +00002013 testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
2014 flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
drh24a09622014-09-18 16:28:59 +00002015 }
drh6a2fe092009-09-23 02:29:36 +00002016 }
drh6a2fe092009-09-23 02:29:36 +00002017 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drhca5506b2014-09-17 23:37:38 +00002018 if( pIn1->flags & MEM_Zero ){
2019 sqlite3VdbeMemExpandBlob(pIn1);
2020 flags1 &= ~MEM_Zero;
2021 }
2022 if( pIn3->flags & MEM_Zero ){
2023 sqlite3VdbeMemExpandBlob(pIn3);
2024 flags3 &= ~MEM_Zero;
2025 }
drh24a09622014-09-18 16:28:59 +00002026 if( db->mallocFailed ) goto no_mem;
drh6a2fe092009-09-23 02:29:36 +00002027 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00002028 }
danielk1977a37cdde2004-05-16 11:15:36 +00002029 switch( pOp->opcode ){
2030 case OP_Eq: res = res==0; break;
2031 case OP_Ne: res = res!=0; break;
2032 case OP_Lt: res = res<0; break;
2033 case OP_Le: res = res<=0; break;
2034 case OP_Gt: res = res>0; break;
2035 default: res = res>=0; break;
2036 }
2037
drhf56fa462015-04-13 21:39:54 +00002038 /* Undo any changes made by applyAffinity() to the input registers. */
2039 assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
2040 pIn1->flags = flags1;
2041 assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) );
2042 pIn3->flags = flags3;
2043
drh35573352008-01-08 23:54:25 +00002044 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00002045 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00002046 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00002047 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00002048 pOut->u.i = res;
2049 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00002050 }else{
drhf4345e42014-02-18 11:31:59 +00002051 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh688852a2014-02-17 22:40:43 +00002052 if( res ){
drhf56fa462015-04-13 21:39:54 +00002053 goto jump_to_p2;
drh688852a2014-02-17 22:40:43 +00002054 }
danielk1977a37cdde2004-05-16 11:15:36 +00002055 }
2056 break;
2057}
drhc9b84a12002-06-20 11:36:48 +00002058
drh0acb7e42008-06-25 00:12:41 +00002059/* Opcode: Permutation * * * P4 *
2060**
shanebe217792009-03-05 04:20:31 +00002061** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00002062** of integers in P4.
2063**
drh953f7612012-12-07 22:18:54 +00002064** The permutation is only valid until the next OP_Compare that has
2065** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
2066** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00002067*/
2068case OP_Permutation: {
2069 assert( pOp->p4type==P4_INTARRAY );
2070 assert( pOp->p4.ai );
2071 aPermute = pOp->p4.ai;
2072 break;
2073}
2074
drh953f7612012-12-07 22:18:54 +00002075/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00002076** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00002077**
drh710c4842010-08-30 01:17:20 +00002078** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
2079** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00002080** the comparison for use by the next OP_Jump instruct.
2081**
drh0ca10df2012-12-08 13:26:23 +00002082** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
2083** determined by the most recent OP_Permutation operator. If the
2084** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
2085** order.
2086**
drh0acb7e42008-06-25 00:12:41 +00002087** P4 is a KeyInfo structure that defines collating sequences and sort
2088** orders for the comparison. The permutation applies to registers
2089** only. The KeyInfo elements are used sequentially.
2090**
2091** The comparison is a sort comparison, so NULLs compare equal,
2092** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00002093** and strings are less than blobs.
2094*/
2095case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002096 int n;
2097 int i;
2098 int p1;
2099 int p2;
2100 const KeyInfo *pKeyInfo;
2101 int idx;
2102 CollSeq *pColl; /* Collating sequence to use on this term */
2103 int bRev; /* True for DESCENDING sort order */
2104
drh953f7612012-12-07 22:18:54 +00002105 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002106 n = pOp->p3;
2107 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002108 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002109 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002110 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002111 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002112#if SQLITE_DEBUG
2113 if( aPermute ){
2114 int k, mx = 0;
2115 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002116 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2117 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002118 }else{
dan3bc9f742013-08-15 16:18:39 +00002119 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2120 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002121 }
2122#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002123 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002124 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002125 assert( memIsValid(&aMem[p1+idx]) );
2126 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002127 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2128 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002129 assert( i<pKeyInfo->nField );
2130 pColl = pKeyInfo->aColl[i];
2131 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002132 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002133 if( iCompare ){
2134 if( bRev ) iCompare = -iCompare;
2135 break;
2136 }
drh16ee60f2008-06-20 18:13:25 +00002137 }
drh0acb7e42008-06-25 00:12:41 +00002138 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002139 break;
2140}
2141
2142/* Opcode: Jump P1 P2 P3 * *
2143**
2144** Jump to the instruction at address P1, P2, or P3 depending on whether
2145** in the most recent OP_Compare instruction the P1 vector was less than
2146** equal to, or greater than the P2 vector, respectively.
2147*/
drh0acb7e42008-06-25 00:12:41 +00002148case OP_Jump: { /* jump */
2149 if( iCompare<0 ){
drhf56fa462015-04-13 21:39:54 +00002150 VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1];
drh0acb7e42008-06-25 00:12:41 +00002151 }else if( iCompare==0 ){
drhf56fa462015-04-13 21:39:54 +00002152 VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1];
drh16ee60f2008-06-20 18:13:25 +00002153 }else{
drhf56fa462015-04-13 21:39:54 +00002154 VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1];
drh16ee60f2008-06-20 18:13:25 +00002155 }
2156 break;
2157}
2158
drh5b6afba2008-01-05 16:29:28 +00002159/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002160** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002161**
drh5b6afba2008-01-05 16:29:28 +00002162** Take the logical AND of the values in registers P1 and P2 and
2163** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002164**
drh5b6afba2008-01-05 16:29:28 +00002165** If either P1 or P2 is 0 (false) then the result is 0 even if
2166** the other input is NULL. A NULL and true or two NULLs give
2167** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002168*/
drh5b6afba2008-01-05 16:29:28 +00002169/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002170** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002171**
2172** Take the logical OR of the values in register P1 and P2 and
2173** store the answer in register P3.
2174**
2175** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2176** even if the other input is NULL. A NULL and false or two NULLs
2177** give a NULL output.
2178*/
2179case OP_And: /* same as TK_AND, in1, in2, out3 */
2180case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002181 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2182 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002183
drh3c657212009-11-17 23:59:58 +00002184 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002185 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002186 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002187 }else{
drh5b6afba2008-01-05 16:29:28 +00002188 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002189 }
drh3c657212009-11-17 23:59:58 +00002190 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002191 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002192 v2 = 2;
2193 }else{
drh5b6afba2008-01-05 16:29:28 +00002194 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002195 }
2196 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002197 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002198 v1 = and_logic[v1*3+v2];
2199 }else{
drh5b6afba2008-01-05 16:29:28 +00002200 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002201 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002202 }
drh3c657212009-11-17 23:59:58 +00002203 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002204 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002205 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002206 }else{
drh5b6afba2008-01-05 16:29:28 +00002207 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002208 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002209 }
drh5e00f6c2001-09-13 13:46:56 +00002210 break;
2211}
2212
drhe99fa2a2008-12-15 15:27:51 +00002213/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002214** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002215**
drhe99fa2a2008-12-15 15:27:51 +00002216** Interpret the value in register P1 as a boolean value. Store the
2217** boolean complement in register P2. If the value in register P1 is
2218** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002219*/
drh93952eb2009-11-13 19:43:43 +00002220case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002221 pIn1 = &aMem[pOp->p1];
2222 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002223 sqlite3VdbeMemSetNull(pOut);
2224 if( (pIn1->flags & MEM_Null)==0 ){
2225 pOut->flags = MEM_Int;
2226 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002227 }
drh5e00f6c2001-09-13 13:46:56 +00002228 break;
2229}
2230
drhe99fa2a2008-12-15 15:27:51 +00002231/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002232** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002233**
drhe99fa2a2008-12-15 15:27:51 +00002234** Interpret the content of register P1 as an integer. Store the
2235** ones-complement of the P1 value into register P2. If P1 holds
2236** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002237*/
drh93952eb2009-11-13 19:43:43 +00002238case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002239 pIn1 = &aMem[pOp->p1];
2240 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002241 sqlite3VdbeMemSetNull(pOut);
2242 if( (pIn1->flags & MEM_Null)==0 ){
2243 pOut->flags = MEM_Int;
2244 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002245 }
drhbf4133c2001-10-13 02:59:08 +00002246 break;
2247}
2248
drh48f2d3b2011-09-16 01:34:43 +00002249/* Opcode: Once P1 P2 * * *
2250**
drh5dad9a32014-07-25 18:37:42 +00002251** Check the "once" flag number P1. If it is set, jump to instruction P2.
2252** Otherwise, set the flag and fall through to the next instruction.
2253** In other words, this opcode causes all following opcodes up through P2
2254** (but not including P2) to run just once and to be skipped on subsequent
2255** times through the loop.
2256**
2257** All "once" flags are initially cleared whenever a prepared statement
2258** first begins to run.
drh48f2d3b2011-09-16 01:34:43 +00002259*/
dan1d8cb212011-12-09 13:24:16 +00002260case OP_Once: { /* jump */
2261 assert( pOp->p1<p->nOnceFlag );
drh688852a2014-02-17 22:40:43 +00002262 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
dan1d8cb212011-12-09 13:24:16 +00002263 if( p->aOnceFlag[pOp->p1] ){
drhf56fa462015-04-13 21:39:54 +00002264 goto jump_to_p2;
dan1d8cb212011-12-09 13:24:16 +00002265 }else{
2266 p->aOnceFlag[pOp->p1] = 1;
2267 }
2268 break;
2269}
2270
drh3c84ddf2008-01-09 02:15:38 +00002271/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002272**
drhef8662b2011-06-20 21:47:58 +00002273** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002274** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002275** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002276*/
drh3c84ddf2008-01-09 02:15:38 +00002277/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002278**
drhef8662b2011-06-20 21:47:58 +00002279** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002280** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002281** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002282*/
drh9cbf3422008-01-17 16:22:13 +00002283case OP_If: /* jump, in1 */
2284case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002285 int c;
drh3c657212009-11-17 23:59:58 +00002286 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002287 if( pIn1->flags & MEM_Null ){
2288 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002289 }else{
drhba0232a2005-06-06 17:27:19 +00002290#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002291 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002292#else
drh3c84ddf2008-01-09 02:15:38 +00002293 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002294#endif
drhf5905aa2002-05-26 20:54:33 +00002295 if( pOp->opcode==OP_IfNot ) c = !c;
2296 }
drh688852a2014-02-17 22:40:43 +00002297 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002298 if( c ){
drhf56fa462015-04-13 21:39:54 +00002299 goto jump_to_p2;
drh3c84ddf2008-01-09 02:15:38 +00002300 }
drh5e00f6c2001-09-13 13:46:56 +00002301 break;
2302}
2303
drh830ecf92009-06-18 00:41:55 +00002304/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002305** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002306**
drh830ecf92009-06-18 00:41:55 +00002307** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002308*/
drh9cbf3422008-01-17 16:22:13 +00002309case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002310 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002311 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002312 if( (pIn1->flags & MEM_Null)!=0 ){
drhf56fa462015-04-13 21:39:54 +00002313 goto jump_to_p2;
drh830ecf92009-06-18 00:41:55 +00002314 }
drh477df4b2008-01-05 18:48:24 +00002315 break;
2316}
2317
drh98757152008-01-09 23:04:12 +00002318/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002319** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002320**
drh6a288a32008-01-07 19:20:24 +00002321** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002322*/
drh9cbf3422008-01-17 16:22:13 +00002323case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002324 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002325 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002326 if( (pIn1->flags & MEM_Null)==0 ){
drhf56fa462015-04-13 21:39:54 +00002327 goto jump_to_p2;
drh6a288a32008-01-07 19:20:24 +00002328 }
drh5e00f6c2001-09-13 13:46:56 +00002329 break;
2330}
2331
drh3e9ca092009-09-08 01:14:48 +00002332/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002333** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002334**
danielk1977cfcdaef2004-05-12 07:33:33 +00002335** Interpret the data that cursor P1 points to as a structure built using
2336** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002337** information about the format of the data.) Extract the P2-th column
2338** from this record. If there are less that (P2+1)
2339** values in the record, extract a NULL.
2340**
drh9cbf3422008-01-17 16:22:13 +00002341** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002342**
danielk19771f4aa332008-01-03 09:51:55 +00002343** If the column contains fewer than P2 fields, then extract a NULL. Or,
2344** if the P4 argument is a P4_MEM use the value of the P4 argument as
2345** the result.
drh3e9ca092009-09-08 01:14:48 +00002346**
2347** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2348** then the cache of the cursor is reset prior to extracting the column.
2349** The first OP_Column against a pseudo-table after the value of the content
2350** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002351**
drhdda5c082012-03-28 13:41:10 +00002352** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2353** the result is guaranteed to only be used as the argument of a length()
2354** or typeof() function, respectively. The loading of large blobs can be
2355** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002356*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002357case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002358 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002359 int p2; /* column number to retrieve */
2360 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002361 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002362 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002363 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002364 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002365 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002366 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002367 const u8 *zData; /* Part of the record being decoded */
2368 const u8 *zHdr; /* Next unparsed byte of the header */
2369 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002370 u32 offset; /* Offset into the data */
drhc6ce38832015-10-15 21:30:24 +00002371 u64 offset64; /* 64-bit offset */
drh501932c2013-11-21 21:59:53 +00002372 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002373 u32 t; /* A type code from the record header */
drh0c8f7602014-09-19 16:56:45 +00002374 u16 fx; /* pDest->flags value */
drh3e9ca092009-09-08 01:14:48 +00002375 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002376
drh399af1d2013-11-20 17:25:55 +00002377 p2 = pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00002378 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002379 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002380 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002381 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2382 pC = p->apCsr[pOp->p1];
drha5759672012-10-30 14:39:12 +00002383 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002384 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002385 aOffset = pC->aOffset;
danielk19770817d0d2007-02-14 09:19:36 +00002386#ifndef SQLITE_OMIT_VIRTUALTABLE
drh380d6852013-11-20 20:58:00 +00002387 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
danielk19770817d0d2007-02-14 09:19:36 +00002388#endif
shane36840fd2009-06-26 16:32:13 +00002389 pCrsr = pC->pCursor;
drh380d6852013-11-20 20:58:00 +00002390 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
2391 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */
drh399af1d2013-11-20 17:25:55 +00002392
2393 /* If the cursor cache is stale, bring it up-to-date */
2394 rc = sqlite3VdbeCursorMoveto(pC);
2395 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00002396 if( pC->cacheStatus!=p->cacheCtr ){
drhc8606e42013-11-20 19:28:03 +00002397 if( pC->nullRow ){
2398 if( pCrsr==0 ){
2399 assert( pC->pseudoTableReg>0 );
2400 pReg = &aMem[pC->pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002401 assert( pReg->flags & MEM_Blob );
2402 assert( memIsValid(pReg) );
2403 pC->payloadSize = pC->szRow = avail = pReg->n;
2404 pC->aRow = (u8*)pReg->z;
2405 }else{
drh6b5631e2014-11-05 15:57:39 +00002406 sqlite3VdbeMemSetNull(pDest);
drh399af1d2013-11-20 17:25:55 +00002407 goto op_column_out;
2408 }
danielk197784ac9d02004-05-18 09:58:06 +00002409 }else{
drhc8606e42013-11-20 19:28:03 +00002410 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002411 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002412 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2413 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2414 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2415 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2416 ** payload size, so it is impossible for payloadSize64 to be
2417 ** larger than 32 bits. */
2418 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2419 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2420 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002421 }else{
drh399af1d2013-11-20 17:25:55 +00002422 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2423 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2424 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2425 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002426 }
drh399af1d2013-11-20 17:25:55 +00002427 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2428 if( pC->payloadSize <= (u32)avail ){
2429 pC->szRow = pC->payloadSize;
drhe61cffc2004-06-12 18:12:15 +00002430 }else{
drh399af1d2013-11-20 17:25:55 +00002431 pC->szRow = avail;
2432 }
2433 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2434 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002435 }
drhd3194f52004-05-27 19:59:32 +00002436 }
drh399af1d2013-11-20 17:25:55 +00002437 pC->cacheStatus = p->cacheCtr;
2438 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2439 pC->nHdrParsed = 0;
2440 aOffset[0] = offset;
drh35cd6432009-06-05 14:17:21 +00002441
drhc81aa2e2014-10-11 23:31:52 +00002442
2443 if( avail<offset ){
2444 /* pC->aRow does not have to hold the entire row, but it does at least
2445 ** need to cover the header of the record. If pC->aRow does not contain
2446 ** the complete header, then set it to zero, forcing the header to be
2447 ** dynamically allocated. */
2448 pC->aRow = 0;
2449 pC->szRow = 0;
drh848a3322015-10-16 12:53:47 +00002450
2451 /* Make sure a corrupt database has not given us an oversize header.
2452 ** Do this now to avoid an oversize memory allocation.
2453 **
2454 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2455 ** types use so much data space that there can only be 4096 and 32 of
2456 ** them, respectively. So the maximum header length results from a
2457 ** 3-byte type for each of the maximum of 32768 columns plus three
2458 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2459 */
2460 if( offset > 98307 || offset > pC->payloadSize ){
2461 rc = SQLITE_CORRUPT_BKPT;
2462 goto op_column_error;
2463 }
drhc81aa2e2014-10-11 23:31:52 +00002464 }
2465
2466 /* The following goto is an optimization. It can be omitted and
2467 ** everything will still work. But OP_Column is measurably faster
2468 ** by skipping the subsequent conditional, which is always true.
2469 */
2470 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2471 goto op_column_read_header;
drh399af1d2013-11-20 17:25:55 +00002472 }
drh35cd6432009-06-05 14:17:21 +00002473
drh399af1d2013-11-20 17:25:55 +00002474 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002475 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002476 */
drhc8606e42013-11-20 19:28:03 +00002477 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002478 /* If there is more header available for parsing in the record, try
2479 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002480 */
drhc81aa2e2014-10-11 23:31:52 +00002481 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002482 if( pC->iHdrOffset<aOffset[0] ){
2483 /* Make sure zData points to enough of the record to cover the header. */
2484 if( pC->aRow==0 ){
2485 memset(&sMem, 0, sizeof(sMem));
drh95fa6062015-10-16 13:50:08 +00002486 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], !pC->isTable, &sMem);
2487 if( rc!=SQLITE_OK ) goto op_column_error;
drhc8606e42013-11-20 19:28:03 +00002488 zData = (u8*)sMem.z;
2489 }else{
2490 zData = pC->aRow;
2491 }
2492
drh0c8f7602014-09-19 16:56:45 +00002493 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drhc8606e42013-11-20 19:28:03 +00002494 i = pC->nHdrParsed;
drhc6ce38832015-10-15 21:30:24 +00002495 offset64 = aOffset[i];
drhc8606e42013-11-20 19:28:03 +00002496 zHdr = zData + pC->iHdrOffset;
2497 zEndHdr = zData + aOffset[0];
2498 assert( i<=p2 && zHdr<zEndHdr );
2499 do{
drh95fa6062015-10-16 13:50:08 +00002500 if( (t = zHdr[0])<0x80 ){
drhc8606e42013-11-20 19:28:03 +00002501 zHdr++;
drhfaf37272015-10-16 14:23:42 +00002502 offset64 += sqlite3VdbeOneByteSerialTypeLen(t);
drhc8606e42013-11-20 19:28:03 +00002503 }else{
2504 zHdr += sqlite3GetVarint32(zHdr, &t);
drhfaf37272015-10-16 14:23:42 +00002505 offset64 += sqlite3VdbeSerialTypeLen(t);
drhc8606e42013-11-20 19:28:03 +00002506 }
drhfaf37272015-10-16 14:23:42 +00002507 pC->aType[i++] = t;
drhc6ce38832015-10-15 21:30:24 +00002508 aOffset[i] = (u32)(offset64 & 0xffffffff);
drhc8606e42013-11-20 19:28:03 +00002509 }while( i<=p2 && zHdr<zEndHdr );
2510 pC->nHdrParsed = i;
2511 pC->iHdrOffset = (u32)(zHdr - zData);
drh95fa6062015-10-16 13:50:08 +00002512 if( pC->aRow==0 ) sqlite3VdbeMemRelease(&sMem);
drhc8606e42013-11-20 19:28:03 +00002513
drh8dd83622014-10-13 23:39:02 +00002514 /* The record is corrupt if any of the following are true:
2515 ** (1) the bytes of the header extend past the declared header size
drh8dd83622014-10-13 23:39:02 +00002516 ** (2) the entire header was used but not all data was used
drh8dd83622014-10-13 23:39:02 +00002517 ** (3) the end of the data extends beyond the end of the record.
drhc8606e42013-11-20 19:28:03 +00002518 */
drhc6ce38832015-10-15 21:30:24 +00002519 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset64!=pC->payloadSize))
2520 || (offset64 > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002521 ){
2522 rc = SQLITE_CORRUPT_BKPT;
2523 goto op_column_error;
2524 }
2525 }
2526
drhf2db3382015-04-30 20:33:25 +00002527 /* If after trying to extract new entries from the header, nHdrParsed is
drh380d6852013-11-20 20:58:00 +00002528 ** still not up to p2, that means that the record has fewer than p2
2529 ** columns. So the result will be either the default value or a NULL.
2530 */
drhc8606e42013-11-20 19:28:03 +00002531 if( pC->nHdrParsed<=p2 ){
2532 if( pOp->p4type==P4_MEM ){
2533 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2534 }else{
drh22e8d832014-10-29 00:58:38 +00002535 sqlite3VdbeMemSetNull(pDest);
drhc8606e42013-11-20 19:28:03 +00002536 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002537 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002538 }
drh95fa6062015-10-16 13:50:08 +00002539 }else{
2540 t = pC->aType[p2];
danielk1977cfcdaef2004-05-12 07:33:33 +00002541 }
danielk1977192ac1d2004-05-10 07:17:30 +00002542
drh380d6852013-11-20 20:58:00 +00002543 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002544 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002545 ** all valid.
drh9188b382004-05-14 21:12:22 +00002546 */
drhc8606e42013-11-20 19:28:03 +00002547 assert( p2<pC->nHdrParsed );
2548 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002549 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drh0725cab2014-09-17 14:52:46 +00002550 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
drh95fa6062015-10-16 13:50:08 +00002551 assert( t==pC->aType[p2] );
drhc8606e42013-11-20 19:28:03 +00002552 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002553 /* This is the common case where the desired content fits on the original
2554 ** page - where the content is not on an overflow page */
drh0c8f7602014-09-19 16:56:45 +00002555 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
danielk197736963fd2005-02-19 08:18:05 +00002556 }else{
drh58c96082013-12-23 11:33:32 +00002557 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002558 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2559 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2560 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002561 ){
drh2a2a6962014-09-16 18:22:44 +00002562 /* Content is irrelevant for
2563 ** 1. the typeof() function,
2564 ** 2. the length(X) function if X is a blob, and
2565 ** 3. if the content length is zero.
2566 ** So we might as well use bogus content rather than reading
2567 ** content from disk. NULL will work for the value for strings
2568 ** and blobs and whatever is in the payloadSize64 variable
2569 ** will work for everything else. */
2570 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002571 }else{
drh14da87f2013-11-20 21:51:33 +00002572 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drh2a2a6962014-09-16 18:22:44 +00002573 pDest);
drhc8606e42013-11-20 19:28:03 +00002574 if( rc!=SQLITE_OK ){
2575 goto op_column_error;
2576 }
drh2a2a6962014-09-16 18:22:44 +00002577 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2578 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002579 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002580 }
drhc8606e42013-11-20 19:28:03 +00002581 pDest->enc = encoding;
drhd3194f52004-05-27 19:59:32 +00002582
danielk19773c9cc8d2005-01-17 03:40:08 +00002583op_column_out:
drh7b5ebca2014-09-19 15:28:33 +00002584 /* If the column value is an ephemeral string, go ahead and persist
2585 ** that string in case the cursor moves before the column value is
2586 ** used. The following code does the equivalent of Deephemeralize()
2587 ** but does it faster. */
2588 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
drh0c8f7602014-09-19 16:56:45 +00002589 fx = pDest->flags & (MEM_Str|MEM_Blob);
2590 assert( fx!=0 );
drh7b5ebca2014-09-19 15:28:33 +00002591 zData = (const u8*)pDest->z;
2592 len = pDest->n;
2593 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
2594 memcpy(pDest->z, zData, len);
2595 pDest->z[len] = 0;
2596 pDest->z[len+1] = 0;
drh0c8f7602014-09-19 16:56:45 +00002597 pDest->flags = fx|MEM_Term;
drh7b5ebca2014-09-19 15:28:33 +00002598 }
drhc8606e42013-11-20 19:28:03 +00002599op_column_error:
drhb7654112008-01-12 12:48:07 +00002600 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002601 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002602 break;
2603}
2604
danielk1977751de562008-04-18 09:01:15 +00002605/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002606** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002607**
2608** Apply affinities to a range of P2 registers starting with P1.
2609**
2610** P4 is a string that is P2 characters long. The nth character of the
2611** string indicates the column affinity that should be used for the nth
2612** memory cell in the range.
2613*/
2614case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002615 const char *zAffinity; /* The affinity to be applied */
2616 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002617
drh856c1032009-06-02 15:21:42 +00002618 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002619 assert( zAffinity!=0 );
2620 assert( zAffinity[pOp->p2]==0 );
2621 pIn1 = &aMem[pOp->p1];
2622 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002623 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002624 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002625 applyAffinity(pIn1, cAff, encoding);
2626 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002627 }
2628 break;
2629}
2630
drh1db639c2008-01-17 02:36:28 +00002631/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002632** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002633**
drh710c4842010-08-30 01:17:20 +00002634** Convert P2 registers beginning with P1 into the [record format]
2635** use as a data record in a database table or as a key
2636** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002637**
danielk1977751de562008-04-18 09:01:15 +00002638** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002639** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002640** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002641**
drh8a512562005-11-14 22:29:05 +00002642** The mapping from character to affinity is given by the SQLITE_AFF_
2643** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002644**
drh05883a32015-06-02 15:32:08 +00002645** If P4 is NULL then all index fields have the affinity BLOB.
drh7f057c92005-06-24 03:53:06 +00002646*/
drh1db639c2008-01-17 02:36:28 +00002647case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002648 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2649 Mem *pRec; /* The new record */
2650 u64 nData; /* Number of bytes of data space */
2651 int nHdr; /* Number of bytes of header space */
2652 i64 nByte; /* Data space required for this record */
drh4a335072015-04-11 02:08:48 +00002653 i64 nZero; /* Number of zero bytes at the end of the record */
drh856c1032009-06-02 15:21:42 +00002654 int nVarint; /* Number of bytes in a varint */
2655 u32 serial_type; /* Type field */
2656 Mem *pData0; /* First field to be combined into the record */
2657 Mem *pLast; /* Last field of the record */
2658 int nField; /* Number of fields in the record */
2659 char *zAffinity; /* The affinity string for the record */
2660 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002661 int i; /* Space used in zNewRecord[] header */
2662 int j; /* Space used in zNewRecord[] content */
drhbe37c122015-10-16 14:54:17 +00002663 u32 len; /* Length of a field */
drh856c1032009-06-02 15:21:42 +00002664
drhf3218fe2004-05-28 08:21:02 +00002665 /* Assuming the record contains N fields, the record format looks
2666 ** like this:
2667 **
drh7a224de2004-06-02 01:22:02 +00002668 ** ------------------------------------------------------------------------
2669 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2670 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002671 **
drh9cbf3422008-01-17 16:22:13 +00002672 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002673 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002674 **
2675 ** Each type field is a varint representing the serial type of the
2676 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002677 ** hdr-size field is also a varint which is the offset from the beginning
2678 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002679 */
drh856c1032009-06-02 15:21:42 +00002680 nData = 0; /* Number of bytes of data space */
2681 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002682 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002683 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002684 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002685 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002686 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002687 nField = pOp->p2;
2688 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002689 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002690
drh2b4ded92010-09-27 21:09:31 +00002691 /* Identify the output register */
2692 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2693 pOut = &aMem[pOp->p3];
2694 memAboutToChange(p, pOut);
2695
drh3e6c0602013-12-10 20:53:01 +00002696 /* Apply the requested affinity to all inputs
2697 */
2698 assert( pData0<=pLast );
2699 if( zAffinity ){
2700 pRec = pData0;
2701 do{
drh57bf4a82014-02-17 14:59:22 +00002702 applyAffinity(pRec++, *(zAffinity++), encoding);
2703 assert( zAffinity[0]==0 || pRec<=pLast );
2704 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002705 }
2706
drhf3218fe2004-05-28 08:21:02 +00002707 /* Loop through the elements that will make up the record to figure
2708 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002709 */
drh038b7bc2013-12-09 23:17:22 +00002710 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002711 do{
drh2b4ded92010-09-27 21:09:31 +00002712 assert( memIsValid(pRec) );
drhbe37c122015-10-16 14:54:17 +00002713 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
drhfdf972a2007-05-02 13:30:27 +00002714 if( pRec->flags & MEM_Zero ){
drh038b7bc2013-12-09 23:17:22 +00002715 if( nData ){
drh53e66c32015-07-24 15:49:23 +00002716 if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
drh038b7bc2013-12-09 23:17:22 +00002717 }else{
2718 nZero += pRec->u.nZero;
2719 len -= pRec->u.nZero;
2720 }
drhfdf972a2007-05-02 13:30:27 +00002721 }
drh8079a0d2006-01-12 17:20:50 +00002722 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002723 testcase( serial_type==127 );
2724 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002725 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002726 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002727
drh654858d2014-11-20 02:18:14 +00002728 /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
2729 ** which determines the total number of bytes in the header. The varint
2730 ** value is the size of the header in bytes including the size varint
2731 ** itself. */
drh59bf00c2013-12-08 23:33:28 +00002732 testcase( nHdr==126 );
2733 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002734 if( nHdr<=126 ){
2735 /* The common case */
2736 nHdr += 1;
2737 }else{
2738 /* Rare case of a really large header */
2739 nVarint = sqlite3VarintLen(nHdr);
2740 nHdr += nVarint;
2741 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002742 }
drh038b7bc2013-12-09 23:17:22 +00002743 nByte = nHdr+nData;
drh4a335072015-04-11 02:08:48 +00002744 if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002745 goto too_big;
2746 }
drhf3218fe2004-05-28 08:21:02 +00002747
danielk1977a7a8e142008-02-13 18:25:27 +00002748 /* Make sure the output register has a buffer large enough to store
2749 ** the new record. The output register (pOp->p3) is not allowed to
2750 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002751 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002752 */
drh322f2852014-09-19 00:43:39 +00002753 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002754 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002755 }
danielk1977a7a8e142008-02-13 18:25:27 +00002756 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002757
2758 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002759 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002760 j = nHdr;
2761 assert( pData0<=pLast );
2762 pRec = pData0;
2763 do{
drhfacf47a2014-10-13 20:12:47 +00002764 serial_type = pRec->uTemp;
drh654858d2014-11-20 02:18:14 +00002765 /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
2766 ** additional varints, one per column. */
drh038b7bc2013-12-09 23:17:22 +00002767 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drh654858d2014-11-20 02:18:14 +00002768 /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
2769 ** immediately follow the header. */
drha9ab4812013-12-11 11:00:44 +00002770 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002771 }while( (++pRec)<=pLast );
2772 assert( i==nHdr );
2773 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002774
dan3bc9f742013-08-15 16:18:39 +00002775 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002776 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002777 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002778 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002779 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002780 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002781 }
drh477df4b2008-01-05 18:48:24 +00002782 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002783 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002784 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002785 break;
2786}
2787
danielk1977a5533162009-02-24 10:01:51 +00002788/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002789** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002790**
2791** Store the number of entries (an integer value) in the table or index
2792** opened by cursor P1 in register P2
2793*/
2794#ifndef SQLITE_OMIT_BTREECOUNT
drh27a348c2015-04-13 19:14:06 +00002795case OP_Count: { /* out2 */
danielk1977a5533162009-02-24 10:01:51 +00002796 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002797 BtCursor *pCrsr;
2798
2799 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh3da046d2013-11-11 03:24:11 +00002800 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002801 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002802 rc = sqlite3BtreeCount(pCrsr, &nEntry);
drh27a348c2015-04-13 19:14:06 +00002803 pOut = out2Prerelease(p, pOp);
danielk1977a5533162009-02-24 10:01:51 +00002804 pOut->u.i = nEntry;
2805 break;
2806}
2807#endif
2808
danielk1977fd7f0452008-12-17 17:30:26 +00002809/* Opcode: Savepoint P1 * * P4 *
2810**
2811** Open, release or rollback the savepoint named by parameter P4, depending
2812** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2813** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2814*/
2815case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002816 int p1; /* Value of P1 operand */
2817 char *zName; /* Name of savepoint */
2818 int nName;
2819 Savepoint *pNew;
2820 Savepoint *pSavepoint;
2821 Savepoint *pTmp;
2822 int iSavepoint;
2823 int ii;
2824
2825 p1 = pOp->p1;
2826 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002827
2828 /* Assert that the p1 parameter is valid. Also that if there is no open
2829 ** transaction, then there cannot be any savepoints.
2830 */
2831 assert( db->pSavepoint==0 || db->autoCommit==0 );
2832 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2833 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2834 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002835 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002836
2837 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002838 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002839 /* A new savepoint cannot be created if there are active write
2840 ** statements (i.e. open read/write incremental blob handles).
2841 */
drh22c17b82015-05-15 04:13:15 +00002842 sqlite3VdbeError(p, "cannot open savepoint - SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002843 rc = SQLITE_BUSY;
2844 }else{
drh856c1032009-06-02 15:21:42 +00002845 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002846
drhbe07ec52011-06-03 12:15:26 +00002847#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002848 /* This call is Ok even if this savepoint is actually a transaction
2849 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2850 ** If this is a transaction savepoint being opened, it is guaranteed
2851 ** that the db->aVTrans[] array is empty. */
2852 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002853 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2854 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002855 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002856#endif
dand9495cd2011-04-27 12:08:04 +00002857
danielk1977fd7f0452008-12-17 17:30:26 +00002858 /* Create a new savepoint structure. */
2859 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2860 if( pNew ){
2861 pNew->zName = (char *)&pNew[1];
2862 memcpy(pNew->zName, zName, nName+1);
2863
2864 /* If there is no open transaction, then mark this as a special
2865 ** "transaction savepoint". */
2866 if( db->autoCommit ){
2867 db->autoCommit = 0;
2868 db->isTransactionSavepoint = 1;
2869 }else{
2870 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002871 }
danielk1977fd7f0452008-12-17 17:30:26 +00002872
2873 /* Link the new savepoint into the database handle's list. */
2874 pNew->pNext = db->pSavepoint;
2875 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002876 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002877 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002878 }
2879 }
2880 }else{
drh856c1032009-06-02 15:21:42 +00002881 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002882
2883 /* Find the named savepoint. If there is no such savepoint, then an
2884 ** an error is returned to the user. */
2885 for(
drh856c1032009-06-02 15:21:42 +00002886 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002887 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002888 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002889 ){
2890 iSavepoint++;
2891 }
2892 if( !pSavepoint ){
drh22c17b82015-05-15 04:13:15 +00002893 sqlite3VdbeError(p, "no such savepoint: %s", zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002894 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002895 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002896 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002897 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002898 */
drh22c17b82015-05-15 04:13:15 +00002899 sqlite3VdbeError(p, "cannot release savepoint - "
2900 "SQL statements in progress");
danielk1977fd7f0452008-12-17 17:30:26 +00002901 rc = SQLITE_BUSY;
2902 }else{
2903
2904 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002905 ** and this is a RELEASE command, then the current transaction
2906 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002907 */
2908 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2909 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002910 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002911 goto vdbe_return;
2912 }
danielk1977fd7f0452008-12-17 17:30:26 +00002913 db->autoCommit = 1;
2914 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00002915 p->pc = (int)(pOp - aOp);
danielk1977fd7f0452008-12-17 17:30:26 +00002916 db->autoCommit = 0;
2917 p->rc = rc = SQLITE_BUSY;
2918 goto vdbe_return;
2919 }
danielk197734cf35d2008-12-18 18:31:38 +00002920 db->isTransactionSavepoint = 0;
2921 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002922 }else{
drh47b7fc72014-11-11 01:33:57 +00002923 int isSchemaChange;
danielk1977fd7f0452008-12-17 17:30:26 +00002924 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002925 if( p1==SAVEPOINT_ROLLBACK ){
drh47b7fc72014-11-11 01:33:57 +00002926 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0;
drh31f10052012-03-31 17:17:26 +00002927 for(ii=0; ii<db->nDb; ii++){
drh77b1dee2014-11-17 17:13:06 +00002928 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
2929 SQLITE_ABORT_ROLLBACK,
drh47b7fc72014-11-11 01:33:57 +00002930 isSchemaChange==0);
dan80231042014-11-12 14:56:02 +00002931 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh31f10052012-03-31 17:17:26 +00002932 }
drh47b7fc72014-11-11 01:33:57 +00002933 }else{
2934 isSchemaChange = 0;
drh0f198a72012-02-13 16:43:16 +00002935 }
2936 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002937 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2938 if( rc!=SQLITE_OK ){
2939 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002940 }
danielk1977fd7f0452008-12-17 17:30:26 +00002941 }
drh47b7fc72014-11-11 01:33:57 +00002942 if( isSchemaChange ){
danielk1977fd7f0452008-12-17 17:30:26 +00002943 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002944 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002945 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002946 }
2947 }
2948
2949 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2950 ** savepoints nested inside of the savepoint being operated on. */
2951 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002952 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002953 db->pSavepoint = pTmp->pNext;
2954 sqlite3DbFree(db, pTmp);
2955 db->nSavepoint--;
2956 }
2957
dan1da40a32009-09-19 17:00:31 +00002958 /* If it is a RELEASE, then destroy the savepoint being operated on
2959 ** too. If it is a ROLLBACK TO, then set the number of deferred
2960 ** constraint violations present in the database to the value stored
2961 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002962 if( p1==SAVEPOINT_RELEASE ){
2963 assert( pSavepoint==db->pSavepoint );
2964 db->pSavepoint = pSavepoint->pNext;
2965 sqlite3DbFree(db, pSavepoint);
2966 if( !isTransaction ){
2967 db->nSavepoint--;
2968 }
dan1da40a32009-09-19 17:00:31 +00002969 }else{
2970 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002971 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002972 }
dand9495cd2011-04-27 12:08:04 +00002973
danea8562e2015-04-18 16:25:54 +00002974 if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
dand9495cd2011-04-27 12:08:04 +00002975 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2976 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2977 }
danielk1977fd7f0452008-12-17 17:30:26 +00002978 }
2979 }
2980
2981 break;
2982}
2983
drh98757152008-01-09 23:04:12 +00002984/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002985**
2986** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002987** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002988** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2989** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002990**
2991** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002992*/
drh9cbf3422008-01-17 16:22:13 +00002993case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002994 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002995 int iRollback;
drh856c1032009-06-02 15:21:42 +00002996 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002997
drh856c1032009-06-02 15:21:42 +00002998 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002999 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00003000 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00003001 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00003002 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00003003 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00003004 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00003005
drh4f7d3a52013-06-27 23:54:02 +00003006 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00003007 /* If this instruction implements a COMMIT and other VMs are writing
3008 ** return an error indicating that the other VMs must complete first.
3009 */
drh22c17b82015-05-15 04:13:15 +00003010 sqlite3VdbeError(p, "cannot commit transaction - "
3011 "SQL statements in progress");
drhad4a4b82008-11-05 16:37:34 +00003012 rc = SQLITE_BUSY;
3013 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00003014 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00003015 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00003016 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00003017 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00003018 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00003019 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00003020 }else{
shane7d3846a2008-12-11 02:58:26 +00003021 db->autoCommit = (u8)desiredAutoCommit;
drh8ff25872015-07-31 18:59:56 +00003022 }
3023 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
3024 p->pc = (int)(pOp - aOp);
3025 db->autoCommit = (u8)(1-desiredAutoCommit);
3026 p->rc = rc = SQLITE_BUSY;
3027 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003028 }
danielk1977bd434552009-03-18 10:33:00 +00003029 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00003030 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00003031 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00003032 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00003033 }else{
drh900b31e2007-08-28 02:27:51 +00003034 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00003035 }
drh900b31e2007-08-28 02:27:51 +00003036 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00003037 }else{
drh22c17b82015-05-15 04:13:15 +00003038 sqlite3VdbeError(p,
drhad4a4b82008-11-05 16:37:34 +00003039 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00003040 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00003041 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00003042
3043 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00003044 }
3045 break;
3046}
3047
drhb22f7c82014-02-06 23:56:27 +00003048/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003049**
drh05a86c52014-02-16 01:55:49 +00003050** Begin a transaction on database P1 if a transaction is not already
3051** active.
3052** If P2 is non-zero, then a write-transaction is started, or if a
3053** read-transaction is already active, it is upgraded to a write-transaction.
3054** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00003055**
drh001bbcb2003-03-19 03:14:00 +00003056** P1 is the index of the database file on which the transaction is
3057** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00003058** file used for temporary tables. Indices of 2 or more are used for
3059** attached databases.
drhcabb0812002-09-14 13:47:32 +00003060**
dane0af83a2009-09-08 19:15:01 +00003061** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
3062** true (this flag is set if the Vdbe may modify more than one row and may
3063** throw an ABORT exception), a statement transaction may also be opened.
3064** More specifically, a statement transaction is opened iff the database
3065** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00003066** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00003067** VDBE to be rolled back after an error without having to roll back the
3068** entire transaction. If no error is encountered, the statement transaction
3069** will automatically commit when the VDBE halts.
3070**
drhb22f7c82014-02-06 23:56:27 +00003071** If P5!=0 then this opcode also checks the schema cookie against P3
3072** and the schema generation counter against P4.
3073** The cookie changes its value whenever the database schema changes.
3074** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00003075** and that the current process needs to reread the schema. If the schema
3076** cookie in P3 differs from the schema cookie in the database header or
3077** if the schema generation counter in P4 differs from the current
3078** generation counter, then an SQLITE_SCHEMA error is raised and execution
3079** halts. The sqlite3_step() wrapper function might then reprepare the
3080** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003081*/
drh9cbf3422008-01-17 16:22:13 +00003082case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003083 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00003084 int iMeta;
3085 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00003086
drh1713afb2013-06-28 01:24:57 +00003087 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003088 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003089 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003090 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003091 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3092 rc = SQLITE_READONLY;
3093 goto abort_due_to_error;
3094 }
drh653b82a2009-06-22 11:10:47 +00003095 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003096
danielk197724162fe2004-06-04 06:22:00 +00003097 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003098 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
drhcbd8db32015-08-20 17:18:32 +00003099 testcase( rc==SQLITE_BUSY_SNAPSHOT );
3100 testcase( rc==SQLITE_BUSY_RECOVERY );
3101 if( (rc&0xff)==SQLITE_BUSY ){
drhf56fa462015-04-13 21:39:54 +00003102 p->pc = (int)(pOp - aOp);
drhcbd8db32015-08-20 17:18:32 +00003103 p->rc = rc;
drh900b31e2007-08-28 02:27:51 +00003104 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003105 }
drh9e9f1bd2009-10-13 15:36:51 +00003106 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003107 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003108 }
dane0af83a2009-09-08 19:15:01 +00003109
3110 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003111 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003112 ){
3113 assert( sqlite3BtreeIsInTrans(pBt) );
3114 if( p->iStatement==0 ){
3115 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3116 db->nStatement++;
3117 p->iStatement = db->nSavepoint + db->nStatement;
3118 }
dana311b802011-04-26 19:21:34 +00003119
drh346506f2011-05-25 01:16:42 +00003120 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003121 if( rc==SQLITE_OK ){
3122 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3123 }
dan1da40a32009-09-19 17:00:31 +00003124
3125 /* Store the current value of the database handles deferred constraint
3126 ** counter. If the statement transaction needs to be rolled back,
3127 ** the value of this counter needs to be restored too. */
3128 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003129 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003130 }
drhb22f7c82014-02-06 23:56:27 +00003131
drh51a74d42015-02-28 01:04:27 +00003132 /* Gather the schema version number for checking:
3133 ** IMPLEMENTATION-OF: R-32195-19465 The schema version is used by SQLite
3134 ** each time a query is executed to ensure that the internal cache of the
3135 ** schema used when compiling the SQL query matches the schema of the
3136 ** database against which the compiled query is actually executed.
3137 */
drhb22f7c82014-02-06 23:56:27 +00003138 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3139 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3140 }else{
3141 iGen = iMeta = 0;
3142 }
3143 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3144 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3145 sqlite3DbFree(db, p->zErrMsg);
3146 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3147 /* If the schema-cookie from the database file matches the cookie
3148 ** stored with the in-memory representation of the schema, do
3149 ** not reload the schema from the database file.
3150 **
3151 ** If virtual-tables are in use, this is not just an optimization.
3152 ** Often, v-tables store their data in other SQLite tables, which
3153 ** are queried from within xNext() and other v-table methods using
3154 ** prepared queries. If such a query is out-of-date, we do not want to
3155 ** discard the database schema, as the user code implementing the
3156 ** v-table would have to be ready for the sqlite3_vtab structure itself
3157 ** to be invalidated whenever sqlite3_step() is called from within
3158 ** a v-table method.
3159 */
3160 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3161 sqlite3ResetOneSchema(db, pOp->p1);
3162 }
3163 p->expired = 1;
3164 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003165 }
drh5e00f6c2001-09-13 13:46:56 +00003166 break;
3167}
3168
drhb1fdb2a2008-01-05 04:06:03 +00003169/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003170**
drh9cbf3422008-01-17 16:22:13 +00003171** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003172** P3==1 is the schema version. P3==2 is the database format.
3173** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003174** the main database file and P1==1 is the database file used to store
3175** temporary tables.
drh4a324312001-12-21 14:30:42 +00003176**
drh50e5dad2001-09-15 00:57:28 +00003177** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003178** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003179** executing this instruction.
3180*/
drh27a348c2015-04-13 19:14:06 +00003181case OP_ReadCookie: { /* out2 */
drhf328bc82004-05-10 23:29:49 +00003182 int iMeta;
drh856c1032009-06-02 15:21:42 +00003183 int iDb;
3184 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003185
drh1713afb2013-06-28 01:24:57 +00003186 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003187 iDb = pOp->p1;
3188 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003189 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003190 assert( iDb>=0 && iDb<db->nDb );
3191 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003192 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003193
danielk1977602b4662009-07-02 07:47:33 +00003194 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh27a348c2015-04-13 19:14:06 +00003195 pOut = out2Prerelease(p, pOp);
drh4c583122008-01-04 22:01:03 +00003196 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003197 break;
3198}
3199
drh98757152008-01-09 23:04:12 +00003200/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003201**
drh98757152008-01-09 23:04:12 +00003202** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003203** into cookie number P2 of database P1. P2==1 is the schema version.
3204** P2==2 is the database format. P2==3 is the recommended pager cache
3205** size, and so forth. P1==0 is the main database file and P1==1 is the
3206** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003207**
3208** A transaction must be started before executing this opcode.
3209*/
drh9cbf3422008-01-17 16:22:13 +00003210case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003211 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003212 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003213 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003214 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003215 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003216 pDb = &db->aDb[pOp->p1];
3217 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003218 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003219 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003220 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003221 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003222 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3223 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003224 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003225 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003226 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003227 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003228 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003229 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003230 }
drhfd426c62006-01-30 15:34:22 +00003231 if( pOp->p1==1 ){
3232 /* Invalidate all prepared statements whenever the TEMP database
3233 ** schema is changed. Ticket #1644 */
3234 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003235 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003236 }
drh50e5dad2001-09-15 00:57:28 +00003237 break;
3238}
3239
drh98757152008-01-09 23:04:12 +00003240/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003241** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003242**
drhecdc7532001-09-23 02:35:53 +00003243** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003244** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003245** P3==0 means the main database, P3==1 means the database used for
3246** temporary tables, and P3>1 means used the corresponding attached
3247** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003248** values need not be contiguous but all P1 values should be small integers.
3249** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003250**
drh98757152008-01-09 23:04:12 +00003251** If P5!=0 then use the content of register P2 as the root page, not
3252** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003253**
drhb19a2bc2001-09-16 00:13:26 +00003254** There will be a read lock on the database whenever there is an
3255** open cursor. If the database was unlocked prior to this instruction
3256** then a read lock is acquired as part of this instruction. A read
3257** lock allows other processes to read the database but prohibits
3258** any other process from modifying the database. The read lock is
3259** released when all cursors are closed. If this instruction attempts
3260** to get a read lock but fails, the script terminates with an
3261** SQLITE_BUSY error code.
3262**
danielk1977d336e222009-02-20 10:58:41 +00003263** The P4 value may be either an integer (P4_INT32) or a pointer to
3264** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3265** structure, then said structure defines the content and collating
3266** sequence of the index being opened. Otherwise, if P4 is an integer
3267** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003268**
drh35263192014-07-22 20:02:19 +00003269** See also: OpenWrite, ReopenIdx
3270*/
3271/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3272** Synopsis: root=P2 iDb=P3
3273**
3274** The ReopenIdx opcode works exactly like ReadOpen except that it first
3275** checks to see if the cursor on P1 is already open with a root page
3276** number of P2 and if it is this opcode becomes a no-op. In other words,
3277** if the cursor is already open, do not reopen it.
3278**
3279** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3280** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3281** every other ReopenIdx or OpenRead for the same cursor number.
3282**
3283** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003284*/
drh98757152008-01-09 23:04:12 +00003285/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003286** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003287**
3288** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003289** page is P2. Or if P5!=0 use the content of register P2 to find the
3290** root page.
drhecdc7532001-09-23 02:35:53 +00003291**
danielk1977d336e222009-02-20 10:58:41 +00003292** The P4 value may be either an integer (P4_INT32) or a pointer to
3293** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3294** structure, then said structure defines the content and collating
3295** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003296** value, it is set to the number of columns in the table, or to the
3297** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003298**
drh001bbcb2003-03-19 03:14:00 +00003299** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003300** in read/write mode. For a given table, there can be one or more read-only
3301** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003302**
drh001bbcb2003-03-19 03:14:00 +00003303** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003304*/
drh35263192014-07-22 20:02:19 +00003305case OP_ReopenIdx: {
drh856c1032009-06-02 15:21:42 +00003306 int nField;
3307 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003308 int p2;
3309 int iDb;
drhf57b3392001-10-08 13:22:32 +00003310 int wrFlag;
3311 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003312 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003313 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003314
drhe0997b32015-03-20 14:57:50 +00003315 assert( pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh35263192014-07-22 20:02:19 +00003316 assert( pOp->p4type==P4_KEYINFO );
3317 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003318 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003319 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
drhe0997b32015-03-20 14:57:50 +00003320 goto open_cursor_set_hints;
drh35263192014-07-22 20:02:19 +00003321 }
3322 /* If the cursor is not currently open or is open on a different
3323 ** index, then fall through into OP_OpenRead to force a reopen */
drh5e00f6c2001-09-13 13:46:56 +00003324case OP_OpenRead:
drh1fa509a2015-03-20 16:34:49 +00003325case OP_OpenWrite:
drh856c1032009-06-02 15:21:42 +00003326
drhe0997b32015-03-20 14:57:50 +00003327 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 || pOp->p5==OPFLAG_SEEKEQ );
drh1713afb2013-06-28 01:24:57 +00003328 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003329 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3330 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003331
danfa401de2009-10-16 14:55:03 +00003332 if( p->expired ){
drh47b7fc72014-11-11 01:33:57 +00003333 rc = SQLITE_ABORT_ROLLBACK;
danfa401de2009-10-16 14:55:03 +00003334 break;
3335 }
3336
drh856c1032009-06-02 15:21:42 +00003337 nField = 0;
3338 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003339 p2 = pOp->p2;
3340 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003341 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003342 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003343 pDb = &db->aDb[iDb];
3344 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003345 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003346 if( pOp->opcode==OP_OpenWrite ){
danfd261ec2015-10-22 20:54:33 +00003347 assert( OPFLAG_FORDELETE==BTREE_FORDELETE );
3348 wrFlag = BTREE_WRCSR | (pOp->p5 & OPFLAG_FORDELETE);
drh21206082011-04-04 18:22:02 +00003349 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003350 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3351 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003352 }
3353 }else{
3354 wrFlag = 0;
3355 }
dan428c2182012-08-06 18:50:11 +00003356 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003357 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003358 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003359 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003360 assert( memIsValid(pIn2) );
3361 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003362 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003363 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003364 /* The p2 value always comes from a prior OP_CreateTable opcode and
3365 ** that opcode will always set the p2 value to 2 or more or else fail.
3366 ** If there were a failure, the prepared statement would have halted
3367 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003368 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003369 rc = SQLITE_CORRUPT_BKPT;
3370 goto abort_due_to_error;
3371 }
drh5edc3122001-09-13 21:53:09 +00003372 }
danielk1977d336e222009-02-20 10:58:41 +00003373 if( pOp->p4type==P4_KEYINFO ){
3374 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003375 assert( pKeyInfo->enc==ENC(db) );
3376 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003377 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003378 }else if( pOp->p4type==P4_INT32 ){
3379 nField = pOp->p4.i;
3380 }
drh653b82a2009-06-22 11:10:47 +00003381 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003382 assert( nField>=0 );
3383 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drh653b82a2009-06-22 11:10:47 +00003384 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003385 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003386 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003387 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003388 pCur->pgnoRoot = p2;
danielk1977d336e222009-02-20 10:58:41 +00003389 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3390 pCur->pKeyInfo = pKeyInfo;
drh14da87f2013-11-20 21:51:33 +00003391 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003392 ** SQLite used to check if the root-page flags were sane at this point
3393 ** and report database corruption if they were not, but this check has
3394 ** since moved into the btree layer. */
3395 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhe0997b32015-03-20 14:57:50 +00003396
3397open_cursor_set_hints:
3398 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3399 assert( OPFLAG_SEEKEQ==BTREE_SEEK_EQ );
drh0403cb32015-08-14 23:57:04 +00003400 testcase( pOp->p5 & OPFLAG_BULKCSR );
3401#ifdef SQLITE_ENABLE_CURSOR_HINT
3402 testcase( pOp->p2 & OPFLAG_SEEKEQ );
3403#endif
drhf7854c72015-10-27 13:24:37 +00003404 sqlite3BtreeCursorHintFlags(pCur->pCursor,
3405 (pOp->p5 & (OPFLAG_BULKCSR|OPFLAG_SEEKEQ)));
drh5e00f6c2001-09-13 13:46:56 +00003406 break;
3407}
3408
drh2a5d9902011-08-26 00:34:45 +00003409/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003410** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003411**
drhb9bb7c12006-06-11 23:41:55 +00003412** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003413** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003414** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003415** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003416**
drh25d3adb2010-04-05 15:11:08 +00003417** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003418** The cursor points to a BTree table if P4==0 and to a BTree index
3419** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003420** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003421**
drh2a5d9902011-08-26 00:34:45 +00003422** The P5 parameter can be a mask of the BTREE_* flags defined
3423** in btree.h. These flags control aspects of the operation of
3424** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3425** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003426*/
drha21a64d2010-04-06 22:33:55 +00003427/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003428** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003429**
3430** This opcode works the same as OP_OpenEphemeral. It has a
3431** different name to distinguish its use. Tables created using
3432** by this opcode will be used for automatically created transient
3433** indices in joins.
3434*/
3435case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003436case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003437 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003438 KeyInfo *pKeyInfo;
3439
drhd4187c72010-08-30 22:15:45 +00003440 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003441 SQLITE_OPEN_READWRITE |
3442 SQLITE_OPEN_CREATE |
3443 SQLITE_OPEN_EXCLUSIVE |
3444 SQLITE_OPEN_DELETEONCLOSE |
3445 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003446 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003447 assert( pOp->p2>=0 );
drh653b82a2009-06-22 11:10:47 +00003448 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003449 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003450 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003451 pCx->isEphemeral = 1;
dan689ab892011-08-12 15:02:00 +00003452 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3453 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003454 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003455 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003456 }
3457 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003458 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003459 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003460 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003461 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003462 */
drh41e13e12013-11-07 14:09:39 +00003463 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003464 int pgno;
drh66a51672008-01-03 00:01:23 +00003465 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003466 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003467 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003468 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003469 assert( pKeyInfo->db==db );
3470 assert( pKeyInfo->enc==ENC(db) );
3471 pCx->pKeyInfo = pKeyInfo;
danfd261ec2015-10-22 20:54:33 +00003472 rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR, pKeyInfo, pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00003473 }
drhf0863fe2005-06-12 21:35:51 +00003474 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003475 }else{
danfd261ec2015-10-22 20:54:33 +00003476 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003477 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003478 }
drh5e00f6c2001-09-13 13:46:56 +00003479 }
drhd4187c72010-08-30 22:15:45 +00003480 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003481 break;
3482}
3483
danfad9f9a2014-04-01 18:41:51 +00003484/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003485**
3486** This opcode works like OP_OpenEphemeral except that it opens
3487** a transient index that is specifically designed to sort large
3488** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003489**
3490** If argument P3 is non-zero, then it indicates that the sorter may
3491** assume that a stable sort considering the first P3 fields of each
3492** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003493*/
drhca892a72011-09-03 00:17:51 +00003494case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003495 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003496
drh399af1d2013-11-20 17:25:55 +00003497 assert( pOp->p1>=0 );
3498 assert( pOp->p2>=0 );
dan5134d132011-09-02 10:31:11 +00003499 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3500 if( pCx==0 ) goto no_mem;
3501 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003502 assert( pCx->pKeyInfo->db==db );
3503 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003504 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003505 break;
3506}
3507
dan78d58432014-03-25 15:04:07 +00003508/* Opcode: SequenceTest P1 P2 * * *
3509** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3510**
3511** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3512** to P2. Regardless of whether or not the jump is taken, increment the
3513** the sequence value.
3514*/
3515case OP_SequenceTest: {
3516 VdbeCursor *pC;
3517 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3518 pC = p->apCsr[pOp->p1];
3519 assert( pC->pSorter );
3520 if( (pC->seqCount++)==0 ){
drhf56fa462015-04-13 21:39:54 +00003521 goto jump_to_p2;
dan78d58432014-03-25 15:04:07 +00003522 }
3523 break;
3524}
3525
drh5f612292014-02-08 23:20:32 +00003526/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003527** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003528**
3529** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003530** row of data. The content of that one row is the content of memory
3531** register P2. In other words, cursor P1 becomes an alias for the
3532** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003533**
drh2d8d7ce2010-02-15 15:17:05 +00003534** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003535** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003536** individual columns using the OP_Column opcode. The OP_Column opcode
3537** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003538**
3539** P3 is the number of fields in the records that will be stored by
3540** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003541*/
drh9cbf3422008-01-17 16:22:13 +00003542case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003543 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003544
drh653b82a2009-06-22 11:10:47 +00003545 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003546 assert( pOp->p3>=0 );
drh653b82a2009-06-22 11:10:47 +00003547 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003548 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003549 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003550 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003551 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003552 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003553 break;
3554}
3555
drh98757152008-01-09 23:04:12 +00003556/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003557**
3558** Close a cursor previously opened as P1. If P1 is not
3559** currently open, this instruction is a no-op.
3560*/
drh9cbf3422008-01-17 16:22:13 +00003561case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003562 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3563 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3564 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003565 break;
3566}
3567
drh97bae792015-06-05 15:59:57 +00003568#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
3569/* Opcode: ColumnsUsed P1 * * P4 *
3570**
3571** This opcode (which only exists if SQLite was compiled with
3572** SQLITE_ENABLE_COLUMN_USED_MASK) identifies which columns of the
3573** table or index for cursor P1 are used. P4 is a 64-bit integer
3574** (P4_INT64) in which the first 63 bits are one for each of the
3575** first 63 columns of the table or index that are actually used
3576** by the cursor. The high-order bit is set if any column after
3577** the 64th is used.
3578*/
3579case OP_ColumnsUsed: {
3580 VdbeCursor *pC;
3581 pC = p->apCsr[pOp->p1];
3582 assert( pC->pCursor );
3583 pC->maskUsed = *(u64*)pOp->p4.pI64;
3584 break;
3585}
3586#endif
3587
drh8af3f772014-07-25 18:01:06 +00003588/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003589** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003590**
danielk1977b790c6c2008-04-18 10:25:24 +00003591** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003592** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003593** to an SQL index, then P3 is the first in an array of P4 registers
3594** that are used as an unpacked index key.
3595**
3596** Reposition cursor P1 so that it points to the smallest entry that
3597** is greater than or equal to the key value. If there are no records
3598** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003599**
drhb1d607d2015-11-05 22:30:54 +00003600** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3601** opcode will always land on a record that equally equals the key, or
3602** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3603** opcode must be followed by an IdxLE opcode with the same arguments.
3604** The IdxLE opcode will be skipped if this opcode succeeds, but the
3605** IdxLE opcode will be used on subsequent loop iterations.
3606**
drh8af3f772014-07-25 18:01:06 +00003607** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003608** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003609** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003610**
drh935850e2014-05-24 17:15:15 +00003611** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003612*/
drh8af3f772014-07-25 18:01:06 +00003613/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003614** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003615**
danielk1977b790c6c2008-04-18 10:25:24 +00003616** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003617** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003618** to an SQL index, then P3 is the first in an array of P4 registers
3619** that are used as an unpacked index key.
3620**
3621** Reposition cursor P1 so that it points to the smallest entry that
3622** is greater than the key value. If there are no records greater than
3623** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003624**
drh8af3f772014-07-25 18:01:06 +00003625** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003626** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003627** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003628**
drh935850e2014-05-24 17:15:15 +00003629** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003630*/
drh8af3f772014-07-25 18:01:06 +00003631/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003632** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003633**
danielk1977b790c6c2008-04-18 10:25:24 +00003634** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003635** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003636** to an SQL index, then P3 is the first in an array of P4 registers
3637** that are used as an unpacked index key.
3638**
3639** Reposition cursor P1 so that it points to the largest entry that
3640** is less than the key value. If there are no records less than
3641** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003642**
drh8af3f772014-07-25 18:01:06 +00003643** This opcode leaves the cursor configured to move in reverse order,
3644** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003645** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003646**
drh935850e2014-05-24 17:15:15 +00003647** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003648*/
drh8af3f772014-07-25 18:01:06 +00003649/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003650** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003651**
danielk1977b790c6c2008-04-18 10:25:24 +00003652** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003653** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003654** to an SQL index, then P3 is the first in an array of P4 registers
3655** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003656**
danielk1977b790c6c2008-04-18 10:25:24 +00003657** Reposition cursor P1 so that it points to the largest entry that
3658** is less than or equal to the key value. If there are no records
3659** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003660**
drh8af3f772014-07-25 18:01:06 +00003661** This opcode leaves the cursor configured to move in reverse order,
3662** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003663** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003664**
drhb1d607d2015-11-05 22:30:54 +00003665** If the cursor P1 was opened using the OPFLAG_SEEKEQ flag, then this
3666** opcode will always land on a record that equally equals the key, or
3667** else jump immediately to P2. When the cursor is OPFLAG_SEEKEQ, this
3668** opcode must be followed by an IdxGE opcode with the same arguments.
3669** The IdxGE opcode will be skipped if this opcode succeeds, but the
3670** IdxGE opcode will be used on subsequent loop iterations.
3671**
drh935850e2014-05-24 17:15:15 +00003672** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003673*/
drh4a1d3652014-02-14 15:13:36 +00003674case OP_SeekLT: /* jump, in3 */
3675case OP_SeekLE: /* jump, in3 */
3676case OP_SeekGE: /* jump, in3 */
3677case OP_SeekGT: { /* jump, in3 */
drhb1d607d2015-11-05 22:30:54 +00003678 int res; /* Comparison result */
3679 int oc; /* Opcode */
3680 VdbeCursor *pC; /* The cursor to seek */
3681 UnpackedRecord r; /* The key to seek for */
3682 int nField; /* Number of columns or fields in the key */
3683 i64 iKey; /* The rowid we are to seek to */
drhd6b79462015-11-07 01:19:00 +00003684 int eqOnly; /* Only interested in == results */
drh80ff32f2001-11-04 18:32:46 +00003685
drh653b82a2009-06-22 11:10:47 +00003686 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003687 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003688 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003689 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003690 assert( pC->pseudoTableReg==0 );
drh4a1d3652014-02-14 15:13:36 +00003691 assert( OP_SeekLE == OP_SeekLT+1 );
3692 assert( OP_SeekGE == OP_SeekLT+2 );
3693 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003694 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00003695 assert( pC->pCursor!=0 );
3696 oc = pOp->opcode;
drhd6b79462015-11-07 01:19:00 +00003697 eqOnly = 0;
drh3da046d2013-11-11 03:24:11 +00003698 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003699#ifdef SQLITE_DEBUG
3700 pC->seekOp = pOp->opcode;
3701#endif
drhe0997b32015-03-20 14:57:50 +00003702
drh3da046d2013-11-11 03:24:11 +00003703 if( pC->isTable ){
drhd6b79462015-11-07 01:19:00 +00003704 /* The BTREE_SEEK_EQ flag is only set on index cursors */
3705 assert( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ)==0 );
3706
drh3da046d2013-11-11 03:24:11 +00003707 /* The input value in P3 might be of any type: integer, real, string,
3708 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003709 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003710 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003711 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003712 applyNumericAffinity(pIn3, 0);
3713 }
drh3da046d2013-11-11 03:24:11 +00003714 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003715
drh3da046d2013-11-11 03:24:11 +00003716 /* If the P3 value could not be converted into an integer without
3717 ** loss of information, then special processing is required... */
3718 if( (pIn3->flags & MEM_Int)==0 ){
3719 if( (pIn3->flags & MEM_Real)==0 ){
3720 /* If the P3 value cannot be converted into any kind of a number,
3721 ** then the seek is not possible, so jump to P2 */
drhf56fa462015-04-13 21:39:54 +00003722 VdbeBranchTaken(1,2); goto jump_to_p2;
drh3da046d2013-11-11 03:24:11 +00003723 break;
3724 }
drh959403f2008-12-12 17:56:16 +00003725
danaa1776f2013-11-26 18:22:59 +00003726 /* If the approximation iKey is larger than the actual real search
3727 ** term, substitute >= for > and < for <=. e.g. if the search term
3728 ** is 4.9 and the integer approximation 5:
3729 **
3730 ** (x > 4.9) -> (x >= 5)
3731 ** (x <= 4.9) -> (x < 5)
3732 */
drh74eaba42014-09-18 17:52:15 +00003733 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003734 assert( OP_SeekGE==(OP_SeekGT-1) );
3735 assert( OP_SeekLT==(OP_SeekLE-1) );
3736 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3737 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003738 }
3739
3740 /* If the approximation iKey is smaller than the actual real search
3741 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003742 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003743 assert( OP_SeekLE==(OP_SeekLT+1) );
3744 assert( OP_SeekGT==(OP_SeekGE+1) );
3745 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3746 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003747 }
drh3da046d2013-11-11 03:24:11 +00003748 }
3749 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003750 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003751 if( rc!=SQLITE_OK ){
3752 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003753 }
drhaa736092009-06-22 00:55:30 +00003754 }else{
drhd6b79462015-11-07 01:19:00 +00003755 /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
3756 ** OP_SeekLE opcodes are allowed, and these must be immediately followed
3757 ** by an OP_IdxGT or OP_IdxLT opcode, respectively, with the same key.
3758 */
3759 if( sqlite3BtreeCursorHasHint(pC->pCursor, BTREE_SEEK_EQ) ){
3760 eqOnly = 1;
3761 assert( pOp->opcode==OP_SeekGE || pOp->opcode==OP_SeekLE );
3762 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3763 assert( pOp[1].p1==pOp[0].p1 );
3764 assert( pOp[1].p2==pOp[0].p2 );
3765 assert( pOp[1].p3==pOp[0].p3 );
3766 assert( pOp[1].p4.i==pOp[0].p4.i );
3767 }
3768
drh3da046d2013-11-11 03:24:11 +00003769 nField = pOp->p4.i;
3770 assert( pOp->p4type==P4_INT32 );
3771 assert( nField>0 );
3772 r.pKeyInfo = pC->pKeyInfo;
3773 r.nField = (u16)nField;
3774
3775 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003776 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003777 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003778 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003779 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003780 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003781 */
dan1fed5da2014-02-25 21:01:25 +00003782 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3783 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3784 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3785 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3786 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003787
3788 r.aMem = &aMem[pOp->p3];
3789#ifdef SQLITE_DEBUG
3790 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3791#endif
3792 ExpandBlob(r.aMem);
drh70528d72015-11-05 20:25:09 +00003793 r.eqSeen = 0;
drh3da046d2013-11-11 03:24:11 +00003794 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
3795 if( rc!=SQLITE_OK ){
3796 goto abort_due_to_error;
3797 }
drhb1d607d2015-11-05 22:30:54 +00003798 if( eqOnly && r.eqSeen==0 ){
3799 assert( res!=0 );
3800 goto seek_not_found;
drh70528d72015-11-05 20:25:09 +00003801 }
drh3da046d2013-11-11 03:24:11 +00003802 }
3803 pC->deferredMoveto = 0;
3804 pC->cacheStatus = CACHE_STALE;
3805#ifdef SQLITE_TEST
3806 sqlite3_search_count++;
3807#endif
drh4a1d3652014-02-14 15:13:36 +00003808 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3809 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003810 res = 0;
drh3da046d2013-11-11 03:24:11 +00003811 rc = sqlite3BtreeNext(pC->pCursor, &res);
3812 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003813 }else{
3814 res = 0;
3815 }
3816 }else{
drh4a1d3652014-02-14 15:13:36 +00003817 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3818 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003819 res = 0;
drh3da046d2013-11-11 03:24:11 +00003820 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3821 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003822 }else{
3823 /* res might be negative because the table is empty. Check to
3824 ** see if this is the case.
3825 */
3826 res = sqlite3BtreeEof(pC->pCursor);
3827 }
3828 }
drhb1d607d2015-11-05 22:30:54 +00003829seek_not_found:
drh3da046d2013-11-11 03:24:11 +00003830 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003831 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003832 if( res ){
drhf56fa462015-04-13 21:39:54 +00003833 goto jump_to_p2;
drhb1d607d2015-11-05 22:30:54 +00003834 }else if( eqOnly ){
3835 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
3836 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
drh5e00f6c2001-09-13 13:46:56 +00003837 }
drh5e00f6c2001-09-13 13:46:56 +00003838 break;
3839}
3840
drh959403f2008-12-12 17:56:16 +00003841/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003842** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003843**
3844** P1 is an open table cursor and P2 is a rowid integer. Arrange
3845** for P1 to move so that it points to the rowid given by P2.
3846**
3847** This is actually a deferred seek. Nothing actually happens until
3848** the cursor is used to read a record. That way, if no reads
3849** occur, no unnecessary I/O happens.
3850*/
3851case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003852 VdbeCursor *pC;
3853
drh653b82a2009-06-22 11:10:47 +00003854 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3855 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003856 assert( pC!=0 );
drh3da046d2013-11-11 03:24:11 +00003857 assert( pC->pCursor!=0 );
3858 assert( pC->isTable );
3859 pC->nullRow = 0;
3860 pIn2 = &aMem[pOp->p2];
3861 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
drh3da046d2013-11-11 03:24:11 +00003862 pC->deferredMoveto = 1;
drh959403f2008-12-12 17:56:16 +00003863 break;
3864}
3865
3866
drh8cff69d2009-11-12 19:59:44 +00003867/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003868** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003869**
drh8cff69d2009-11-12 19:59:44 +00003870** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3871** P4>0 then register P3 is the first of P4 registers that form an unpacked
3872** record.
3873**
3874** Cursor P1 is on an index btree. If the record identified by P3 and P4
3875** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003876** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003877**
drhcefc87f2014-08-01 01:40:33 +00003878** This operation leaves the cursor in a state where it can be
3879** advanced in the forward direction. The Next instruction will work,
3880** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003881**
drh6f225d02013-10-26 13:36:51 +00003882** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003883*/
drh8cff69d2009-11-12 19:59:44 +00003884/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003885** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003886**
drh8cff69d2009-11-12 19:59:44 +00003887** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3888** P4>0 then register P3 is the first of P4 registers that form an unpacked
3889** record.
3890**
3891** Cursor P1 is on an index btree. If the record identified by P3 and P4
3892** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3893** does contain an entry whose prefix matches the P3/P4 record then control
3894** falls through to the next instruction and P1 is left pointing at the
3895** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003896**
drh8af3f772014-07-25 18:01:06 +00003897** This operation leaves the cursor in a state where it cannot be
3898** advanced in either direction. In other words, the Next and Prev
3899** opcodes do not work after this operation.
3900**
drh6f225d02013-10-26 13:36:51 +00003901** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003902*/
drh6f225d02013-10-26 13:36:51 +00003903/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003904** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003905**
3906** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3907** P4>0 then register P3 is the first of P4 registers that form an unpacked
3908** record.
3909**
3910** Cursor P1 is on an index btree. If the record identified by P3 and P4
3911** contains any NULL value, jump immediately to P2. If all terms of the
3912** record are not-NULL then a check is done to determine if any row in the
3913** P1 index btree has a matching key prefix. If there are no matches, jump
3914** immediately to P2. If there is a match, fall through and leave the P1
3915** cursor pointing to the matching row.
3916**
3917** This opcode is similar to OP_NotFound with the exceptions that the
3918** branch is always taken if any part of the search key input is NULL.
3919**
drh8af3f772014-07-25 18:01:06 +00003920** This operation leaves the cursor in a state where it cannot be
3921** advanced in either direction. In other words, the Next and Prev
3922** opcodes do not work after this operation.
3923**
drh6f225d02013-10-26 13:36:51 +00003924** See also: NotFound, Found, NotExists
3925*/
3926case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003927case OP_NotFound: /* jump, in3 */
3928case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003929 int alreadyExists;
drhf56fa462015-04-13 21:39:54 +00003930 int takeJump;
drh6f225d02013-10-26 13:36:51 +00003931 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003932 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003933 int res;
dan03e9cfc2011-09-05 14:20:27 +00003934 char *pFree;
drh856c1032009-06-02 15:21:42 +00003935 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003936 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003937 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003938
dan0ff297e2009-09-25 17:03:14 +00003939#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003940 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003941#endif
3942
drhaa736092009-06-22 00:55:30 +00003943 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003944 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003945 pC = p->apCsr[pOp->p1];
3946 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003947#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00003948 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00003949#endif
drh3c657212009-11-17 23:59:58 +00003950 pIn3 = &aMem[pOp->p3];
drh3da046d2013-11-11 03:24:11 +00003951 assert( pC->pCursor!=0 );
3952 assert( pC->isTable==0 );
drhf56fa462015-04-13 21:39:54 +00003953 pFree = 0;
drh3da046d2013-11-11 03:24:11 +00003954 if( pOp->p4.i>0 ){
3955 r.pKeyInfo = pC->pKeyInfo;
3956 r.nField = (u16)pOp->p4.i;
3957 r.aMem = pIn3;
drh826af372014-02-08 19:12:21 +00003958 for(ii=0; ii<r.nField; ii++){
3959 assert( memIsValid(&r.aMem[ii]) );
3960 ExpandBlob(&r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003961#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00003962 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003963#endif
drh826af372014-02-08 19:12:21 +00003964 }
drh3da046d2013-11-11 03:24:11 +00003965 pIdxKey = &r;
3966 }else{
3967 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3968 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
danb391b942014-11-07 14:41:11 +00003969 );
drh3da046d2013-11-11 03:24:11 +00003970 if( pIdxKey==0 ) goto no_mem;
3971 assert( pIn3->flags & MEM_Blob );
danb391b942014-11-07 14:41:11 +00003972 ExpandBlob(pIn3);
drh3da046d2013-11-11 03:24:11 +00003973 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh3da046d2013-11-11 03:24:11 +00003974 }
dan1fed5da2014-02-25 21:01:25 +00003975 pIdxKey->default_rc = 0;
drhf56fa462015-04-13 21:39:54 +00003976 takeJump = 0;
drh3da046d2013-11-11 03:24:11 +00003977 if( pOp->opcode==OP_NoConflict ){
3978 /* For the OP_NoConflict opcode, take the jump if any of the
3979 ** input fields are NULL, since any key with a NULL will not
3980 ** conflict */
mistachkin7bb6e8e2015-01-12 18:52:41 +00003981 for(ii=0; ii<pIdxKey->nField; ii++){
3982 if( pIdxKey->aMem[ii].flags & MEM_Null ){
drhf56fa462015-04-13 21:39:54 +00003983 takeJump = 1;
drh3da046d2013-11-11 03:24:11 +00003984 break;
drh6f225d02013-10-26 13:36:51 +00003985 }
3986 }
drh5e00f6c2001-09-13 13:46:56 +00003987 }
drh3da046d2013-11-11 03:24:11 +00003988 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drhf56fa462015-04-13 21:39:54 +00003989 sqlite3DbFree(db, pFree);
drh3da046d2013-11-11 03:24:11 +00003990 if( rc!=SQLITE_OK ){
3991 break;
3992 }
drh1fd522f2013-11-21 00:10:35 +00003993 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003994 alreadyExists = (res==0);
3995 pC->nullRow = 1-alreadyExists;
3996 pC->deferredMoveto = 0;
3997 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003998 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00003999 VdbeBranchTaken(alreadyExists!=0,2);
drhf56fa462015-04-13 21:39:54 +00004000 if( alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004001 }else{
drhf56fa462015-04-13 21:39:54 +00004002 VdbeBranchTaken(takeJump||alreadyExists==0,2);
4003 if( takeJump || !alreadyExists ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004004 }
drh5e00f6c2001-09-13 13:46:56 +00004005 break;
4006}
4007
drh9cbf3422008-01-17 16:22:13 +00004008/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004009** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00004010**
drh261c02d2013-10-25 14:46:15 +00004011** P1 is the index of a cursor open on an SQL table btree (with integer
4012** keys). P3 is an integer rowid. If P1 does not contain a record with
danc6157e12015-09-14 09:23:47 +00004013** rowid P3 then jump immediately to P2. Or, if P2 is 0, raise an
4014** SQLITE_CORRUPT error. If P1 does contain a record with rowid P3 then
4015** leave the cursor pointing at that record and fall through to the next
4016** instruction.
drh6b125452002-01-28 15:53:03 +00004017**
drh261c02d2013-10-25 14:46:15 +00004018** The OP_NotFound opcode performs the same operation on index btrees
4019** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00004020**
drh8af3f772014-07-25 18:01:06 +00004021** This opcode leaves the cursor in a state where it cannot be advanced
4022** in either direction. In other words, the Next and Prev opcodes will
4023** not work following this opcode.
4024**
drh11e85272013-10-26 15:40:48 +00004025** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00004026*/
drh9cbf3422008-01-17 16:22:13 +00004027case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004028 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00004029 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004030 int res;
4031 u64 iKey;
4032
drh3c657212009-11-17 23:59:58 +00004033 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00004034 assert( pIn3->flags & MEM_Int );
4035 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4036 pC = p->apCsr[pOp->p1];
4037 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00004038#ifdef SQLITE_DEBUG
4039 pC->seekOp = 0;
4040#endif
drhaa736092009-06-22 00:55:30 +00004041 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00004042 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00004043 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004044 assert( pCrsr!=0 );
4045 res = 0;
4046 iKey = pIn3->u.i;
4047 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb79d5522015-09-14 19:26:37 +00004048 assert( rc==SQLITE_OK || res==0 );
drhb53a5a92014-10-12 22:37:22 +00004049 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00004050 pC->nullRow = 0;
4051 pC->cacheStatus = CACHE_STALE;
4052 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00004053 VdbeBranchTaken(res!=0,2);
drh1fd522f2013-11-21 00:10:35 +00004054 pC->seekResult = res;
danc6157e12015-09-14 09:23:47 +00004055 if( res!=0 ){
drhb79d5522015-09-14 19:26:37 +00004056 assert( rc==SQLITE_OK );
4057 if( pOp->p2==0 ){
4058 rc = SQLITE_CORRUPT_BKPT;
4059 }else{
4060 goto jump_to_p2;
4061 }
danc6157e12015-09-14 09:23:47 +00004062 }
drh6b125452002-01-28 15:53:03 +00004063 break;
4064}
4065
drh4c583122008-01-04 22:01:03 +00004066/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00004067** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00004068**
drh4c583122008-01-04 22:01:03 +00004069** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00004070** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00004071** The sequence number on the cursor is incremented after this
4072** instruction.
drh4db38a72005-09-01 12:16:28 +00004073*/
drh27a348c2015-04-13 19:14:06 +00004074case OP_Sequence: { /* out2 */
drh653b82a2009-06-22 11:10:47 +00004075 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4076 assert( p->apCsr[pOp->p1]!=0 );
drh27a348c2015-04-13 19:14:06 +00004077 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004078 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00004079 break;
4080}
4081
4082
drh98757152008-01-09 23:04:12 +00004083/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004084** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004085**
drhf0863fe2005-06-12 21:35:51 +00004086** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00004087** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00004088** table that cursor P1 points to. The new record number is written
4089** written to register P2.
drh205f48e2004-11-05 00:43:11 +00004090**
dan76d462e2009-08-30 11:42:51 +00004091** If P3>0 then P3 is a register in the root frame of this VDBE that holds
4092** the largest previously generated record number. No new record numbers are
4093** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00004094** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00004095** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00004096** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00004097*/
drh27a348c2015-04-13 19:14:06 +00004098case OP_NewRowid: { /* out2 */
drhaa736092009-06-22 00:55:30 +00004099 i64 v; /* The new rowid */
4100 VdbeCursor *pC; /* Cursor of table to get the new rowid */
4101 int res; /* Result of an sqlite3BtreeLast() */
4102 int cnt; /* Counter to limit the number of searches */
4103 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00004104 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00004105
drh856c1032009-06-02 15:21:42 +00004106 v = 0;
4107 res = 0;
drh27a348c2015-04-13 19:14:06 +00004108 pOut = out2Prerelease(p, pOp);
drhaa736092009-06-22 00:55:30 +00004109 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4110 pC = p->apCsr[pOp->p1];
4111 assert( pC!=0 );
drh98ef0f62015-06-30 01:25:52 +00004112 assert( pC->pCursor!=0 );
4113 {
drh5cf8e8c2002-02-19 22:42:05 +00004114 /* The next rowid or record number (different terms for the same
4115 ** thing) is obtained in a two-step algorithm.
4116 **
4117 ** First we attempt to find the largest existing rowid and add one
4118 ** to that. But if the largest existing rowid is already the maximum
4119 ** positive integer, we have to fall through to the second
4120 ** probabilistic algorithm
4121 **
4122 ** The second algorithm is to select a rowid at random and see if
4123 ** it already exists in the table. If it does not exist, we have
4124 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00004125 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00004126 */
drhaa736092009-06-22 00:55:30 +00004127 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00004128
drh75f86a42005-02-17 00:03:06 +00004129#ifdef SQLITE_32BIT_ROWID
4130# define MAX_ROWID 0x7fffffff
4131#else
drhfe2093d2005-01-20 22:48:47 +00004132 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
4133 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
4134 ** to provide the constant while making all compilers happy.
4135 */
danielk197764202cf2008-11-17 15:31:47 +00004136# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00004137#endif
drhfe2093d2005-01-20 22:48:47 +00004138
drh5cf8e8c2002-02-19 22:42:05 +00004139 if( !pC->useRandomRowid ){
drhe0670b62014-02-12 21:31:12 +00004140 rc = sqlite3BtreeLast(pC->pCursor, &res);
4141 if( rc!=SQLITE_OK ){
4142 goto abort_due_to_error;
4143 }
4144 if( res ){
4145 v = 1; /* IMP: R-61914-48074 */
4146 }else{
4147 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
4148 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4149 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
4150 if( v>=MAX_ROWID ){
4151 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00004152 }else{
drhe0670b62014-02-12 21:31:12 +00004153 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00004154 }
drh3fc190c2001-09-14 03:24:23 +00004155 }
drhe0670b62014-02-12 21:31:12 +00004156 }
drh205f48e2004-11-05 00:43:11 +00004157
4158#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00004159 if( pOp->p3 ){
4160 /* Assert that P3 is a valid memory cell. */
4161 assert( pOp->p3>0 );
4162 if( p->pFrame ){
4163 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00004164 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00004165 assert( pOp->p3<=pFrame->nMem );
4166 pMem = &pFrame->aMem[pOp->p3];
4167 }else{
4168 /* Assert that P3 is a valid memory cell. */
4169 assert( pOp->p3<=(p->nMem-p->nCursor) );
4170 pMem = &aMem[pOp->p3];
4171 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004172 }
drhe0670b62014-02-12 21:31:12 +00004173 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004174
drhe0670b62014-02-12 21:31:12 +00004175 REGISTER_TRACE(pOp->p3, pMem);
4176 sqlite3VdbeMemIntegerify(pMem);
4177 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4178 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4179 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
4180 goto abort_due_to_error;
4181 }
4182 if( v<pMem->u.i+1 ){
4183 v = pMem->u.i + 1;
4184 }
4185 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004186 }
drhe0670b62014-02-12 21:31:12 +00004187#endif
drh5cf8e8c2002-02-19 22:42:05 +00004188 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004189 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004190 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004191 ** engine starts picking positive candidate ROWIDs at random until
4192 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004193 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4194 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004195 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004196 do{
4197 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004198 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drh2c4dc632014-09-25 12:31:28 +00004199 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004200 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004201 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004202 && (++cnt<100));
drhaa736092009-06-22 00:55:30 +00004203 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004204 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004205 goto abort_due_to_error;
4206 }
drh748a52c2010-09-01 11:50:08 +00004207 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004208 }
drha11846b2004-01-07 18:52:56 +00004209 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004210 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004211 }
drh4c583122008-01-04 22:01:03 +00004212 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004213 break;
4214}
4215
danielk19771f4aa332008-01-03 09:51:55 +00004216/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004217** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004218**
jplyon5a564222003-06-02 06:15:58 +00004219** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004220** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004221** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004222** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004223** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004224**
danielk19771f4aa332008-01-03 09:51:55 +00004225** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4226** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004227** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004228** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004229**
drh3e9ca092009-09-08 01:14:48 +00004230** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4231** the last seek operation (OP_NotExists) was a success, then this
4232** operation will not attempt to find the appropriate row before doing
4233** the insert but will instead overwrite the row that the cursor is
4234** currently pointing to. Presumably, the prior OP_NotExists opcode
4235** has already positioned the cursor correctly. This is an optimization
4236** that boosts performance by avoiding redundant seeks.
4237**
4238** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4239** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4240** is part of an INSERT operation. The difference is only important to
4241** the update hook.
4242**
drh66a51672008-01-03 00:01:23 +00004243** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004244** may be NULL. If it is not NULL, then the update-hook
4245** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4246**
drh93aed5a2008-01-16 17:46:38 +00004247** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4248** allocated, then ownership of P2 is transferred to the pseudo-cursor
4249** and register P2 becomes ephemeral. If the cursor is changed, the
4250** value of register P2 will then change. Make sure this does not
4251** cause any problems.)
4252**
drhf0863fe2005-06-12 21:35:51 +00004253** This instruction only works on tables. The equivalent instruction
4254** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004255*/
drhe05c9292009-10-29 13:48:10 +00004256/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004257** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004258**
4259** This works exactly like OP_Insert except that the key is the
4260** integer value P3, not the value of the integer stored in register P3.
4261*/
4262case OP_Insert:
4263case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004264 Mem *pData; /* MEM cell holding data for the record to be inserted */
4265 Mem *pKey; /* MEM cell holding key for the record */
4266 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4267 VdbeCursor *pC; /* Cursor to table into which insert is written */
4268 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004269 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004270 const char *zDb; /* database name - used by the update hook */
4271 const char *zTbl; /* Table name - used by the opdate hook */
4272 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004273
drha6c2ed92009-11-14 23:22:23 +00004274 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004275 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004276 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004277 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004278 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004279 assert( pC->pCursor!=0 );
4280 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004281 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004282 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004283
drhe05c9292009-10-29 13:48:10 +00004284 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004285 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004286 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004287 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004288 REGISTER_TRACE(pOp->p3, pKey);
4289 iKey = pKey->u.i;
4290 }else{
4291 assert( pOp->opcode==OP_InsertInt );
4292 iKey = pOp->p3;
4293 }
4294
drha05a7222008-01-19 03:35:58 +00004295 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004296 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004297 if( pData->flags & MEM_Null ){
4298 pData->z = 0;
4299 pData->n = 0;
4300 }else{
4301 assert( pData->flags & (MEM_Blob|MEM_Str) );
4302 }
drh3e9ca092009-09-08 01:14:48 +00004303 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4304 if( pData->flags & MEM_Zero ){
4305 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004306 }else{
drh3e9ca092009-09-08 01:14:48 +00004307 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004308 }
drh3e9ca092009-09-08 01:14:48 +00004309 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4310 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004311 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004312 );
drha05a7222008-01-19 03:35:58 +00004313 pC->deferredMoveto = 0;
4314 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004315
drha05a7222008-01-19 03:35:58 +00004316 /* Invoke the update-hook if required. */
4317 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004318 zDb = db->aDb[pC->iDb].zName;
4319 zTbl = pOp->p4.z;
4320 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004321 assert( pC->isTable );
4322 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4323 assert( pC->iDb>=0 );
4324 }
drh5e00f6c2001-09-13 13:46:56 +00004325 break;
4326}
4327
danf0ee1d32015-09-12 19:26:11 +00004328/* Opcode: Delete P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004329**
drh5edc3122001-09-13 21:53:09 +00004330** Delete the record at which the P1 cursor is currently pointing.
4331**
danf0ee1d32015-09-12 19:26:11 +00004332** If the P5 parameter is non-zero, the cursor will be left pointing at
4333** either the next or the previous record in the table. If it is left
4334** pointing at the next record, then the next Next instruction will be a
4335** no-op. As a result, in this case it is OK to delete a record from within a
4336** Next loop. If P5 is zero, then the cursor is left in an undefined state.
drhc8d30ac2002-04-12 10:08:59 +00004337**
rdcb0c374f2004-02-20 22:53:38 +00004338** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004339** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004340**
drh91fd4d42008-01-19 20:11:25 +00004341** P1 must not be pseudo-table. It has to be a real table with
4342** multiple rows.
4343**
4344** If P4 is not NULL, then it is the name of the table that P1 is
4345** pointing to. The update hook will be invoked, if it exists.
4346** If P4 is not NULL then the P1 cursor must have been positioned
4347** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004348*/
drh9cbf3422008-01-17 16:22:13 +00004349case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004350 VdbeCursor *pC;
drhb79d5522015-09-14 19:26:37 +00004351 u8 hasUpdateCallback;
drh91fd4d42008-01-19 20:11:25 +00004352
drh653b82a2009-06-22 11:10:47 +00004353 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4354 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004355 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004356 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
drh9a65f2c2009-06-22 19:05:40 +00004357 assert( pC->deferredMoveto==0 );
drh9a65f2c2009-06-22 19:05:40 +00004358
drhb79d5522015-09-14 19:26:37 +00004359 hasUpdateCallback = db->xUpdateCallback && pOp->p4.z && pC->isTable;
4360 if( pOp->p5 && hasUpdateCallback ){
danf0ee1d32015-09-12 19:26:11 +00004361 sqlite3BtreeKeySize(pC->pCursor, &pC->movetoTarget);
4362 }
drh91fd4d42008-01-19 20:11:25 +00004363
drhb53a5a92014-10-12 22:37:22 +00004364#ifdef SQLITE_DEBUG
4365 /* The seek operation that positioned the cursor prior to OP_Delete will
4366 ** have also set the pC->movetoTarget field to the rowid of the row that
4367 ** is being deleted */
danf0ee1d32015-09-12 19:26:11 +00004368 if( pOp->p4.z && pC->isTable && pOp->p5==0 ){
drhb53a5a92014-10-12 22:37:22 +00004369 i64 iKey = 0;
4370 sqlite3BtreeKeySize(pC->pCursor, &iKey);
4371 assert( pC->movetoTarget==iKey );
4372 }
4373#endif
4374
danf0ee1d32015-09-12 19:26:11 +00004375 rc = sqlite3BtreeDelete(pC->pCursor, pOp->p5);
drh91fd4d42008-01-19 20:11:25 +00004376 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004377
drh91fd4d42008-01-19 20:11:25 +00004378 /* Invoke the update-hook if required. */
drhb79d5522015-09-14 19:26:37 +00004379 if( rc==SQLITE_OK && hasUpdateCallback ){
drh2c77be02013-11-27 21:07:03 +00004380 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
drhb53a5a92014-10-12 22:37:22 +00004381 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
drh91fd4d42008-01-19 20:11:25 +00004382 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004383 }
danielk1977b28af712004-06-21 06:50:26 +00004384 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004385 break;
4386}
drhb7f1d9a2009-09-08 02:27:58 +00004387/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004388**
drhb7f1d9a2009-09-08 02:27:58 +00004389** The value of the change counter is copied to the database handle
4390** change counter (returned by subsequent calls to sqlite3_changes()).
4391** Then the VMs internal change counter resets to 0.
4392** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004393*/
drh9cbf3422008-01-17 16:22:13 +00004394case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004395 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004396 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004397 break;
4398}
4399
drh1153c7b2013-11-01 22:02:56 +00004400/* Opcode: SorterCompare P1 P2 P3 P4
drhac502322014-07-30 13:56:48 +00004401** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004402**
drh1153c7b2013-11-01 22:02:56 +00004403** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004404** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004405** the sorter cursor currently points to. Only the first P4 fields
4406** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004407**
4408** If either P3 or the sorter contains a NULL in one of their significant
4409** fields (not counting the P4 fields at the end which are ignored) then
4410** the comparison is assumed to be equal.
4411**
4412** Fall through to next instruction if the two records compare equal to
4413** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004414*/
4415case OP_SorterCompare: {
4416 VdbeCursor *pC;
4417 int res;
drhac502322014-07-30 13:56:48 +00004418 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004419
4420 pC = p->apCsr[pOp->p1];
4421 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004422 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004423 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004424 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004425 res = 0;
drhac502322014-07-30 13:56:48 +00004426 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004427 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004428 if( res ) goto jump_to_p2;
dan5134d132011-09-02 10:31:11 +00004429 break;
4430};
4431
drh6cf4a7d2014-10-13 13:00:58 +00004432/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004433** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004434**
4435** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004436** Then clear the column header cache on cursor P3.
4437**
4438** This opcode is normally use to move a record out of the sorter and into
4439** a register that is the source for a pseudo-table cursor created using
4440** OpenPseudo. That pseudo-table cursor is the one that is identified by
4441** parameter P3. Clearing the P3 column cache as part of this opcode saves
4442** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004443*/
4444case OP_SorterData: {
4445 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004446
dan5134d132011-09-02 10:31:11 +00004447 pOut = &aMem[pOp->p2];
4448 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004449 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004450 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004451 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004452 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4453 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004454 break;
4455}
4456
drh98757152008-01-09 23:04:12 +00004457/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004458** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004459**
drh98757152008-01-09 23:04:12 +00004460** Write into register P2 the complete row data for cursor P1.
4461** There is no interpretation of the data.
4462** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004463** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004464**
drhde4fcfd2008-01-19 23:50:26 +00004465** If the P1 cursor must be pointing to a valid row (not a NULL row)
4466** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004467*/
drh98757152008-01-09 23:04:12 +00004468/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004469** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004470**
drh98757152008-01-09 23:04:12 +00004471** Write into register P2 the complete row key for cursor P1.
4472** There is no interpretation of the data.
drh0fd61352014-02-07 02:29:45 +00004473** The key is copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004474** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004475**
drhde4fcfd2008-01-19 23:50:26 +00004476** If the P1 cursor must be pointing to a valid row (not a NULL row)
4477** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004478*/
danielk1977a7a8e142008-02-13 18:25:27 +00004479case OP_RowKey:
4480case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004481 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004482 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004483 u32 n;
drh856c1032009-06-02 15:21:42 +00004484 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004485
drha6c2ed92009-11-14 23:22:23 +00004486 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004487 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004488
drhf0863fe2005-06-12 21:35:51 +00004489 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004490 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4491 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004492 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004493 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004494 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004495 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004496 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004497 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004498 assert( pC->pCursor!=0 );
4499 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004500
4501 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4502 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
drhc22284f2014-10-13 16:02:20 +00004503 ** the cursor. If this where not the case, on of the following assert()s
4504 ** would fail. Should this ever change (because of changes in the code
4505 ** generator) then the fix would be to insert a call to
4506 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004507 */
4508 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004509 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4510#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004511 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004512 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4513#endif
drh9a65f2c2009-06-22 19:05:40 +00004514
drh14da87f2013-11-20 21:51:33 +00004515 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004516 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004517 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004518 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004519 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004520 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004521 }
drhbfb19dc2009-06-05 16:46:53 +00004522 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004523 }else{
drhb07028f2011-10-14 21:49:18 +00004524 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004525 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004526 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004527 goto too_big;
4528 }
drhde4fcfd2008-01-19 23:50:26 +00004529 }
drh722246e2014-10-07 23:02:24 +00004530 testcase( n==0 );
4531 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
danielk1977a7a8e142008-02-13 18:25:27 +00004532 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004533 }
danielk1977a7a8e142008-02-13 18:25:27 +00004534 pOut->n = n;
4535 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004536 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004537 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4538 }else{
4539 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004540 }
danielk197796cb76f2008-01-04 13:24:28 +00004541 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004542 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004543 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004544 break;
4545}
4546
drh2133d822008-01-03 18:44:59 +00004547/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004548** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004549**
drh2133d822008-01-03 18:44:59 +00004550** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004551** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004552**
4553** P1 can be either an ordinary table or a virtual table. There used to
4554** be a separate OP_VRowid opcode for use with virtual tables, but this
4555** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004556*/
drh27a348c2015-04-13 19:14:06 +00004557case OP_Rowid: { /* out2 */
drhdfe88ec2008-11-03 20:55:06 +00004558 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004559 i64 v;
drh856c1032009-06-02 15:21:42 +00004560 sqlite3_vtab *pVtab;
4561 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004562
drh27a348c2015-04-13 19:14:06 +00004563 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004564 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4565 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004566 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004567 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004568 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004569 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004570 break;
4571 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004572 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004573#ifndef SQLITE_OMIT_VIRTUALTABLE
4574 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004575 pVtab = pC->pVtabCursor->pVtab;
4576 pModule = pVtab->pModule;
4577 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004578 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004579 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004580#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004581 }else{
drh6be240e2009-07-14 02:33:02 +00004582 assert( pC->pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004583 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004584 if( rc ) goto abort_due_to_error;
dan2b8669a2014-11-17 19:42:48 +00004585 if( pC->nullRow ){
4586 pOut->flags = MEM_Null;
4587 break;
drh61495262009-04-22 15:32:59 +00004588 }
drhb53a5a92014-10-12 22:37:22 +00004589 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
drhc22284f2014-10-13 16:02:20 +00004590 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */
drh5e00f6c2001-09-13 13:46:56 +00004591 }
drh4c583122008-01-04 22:01:03 +00004592 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004593 break;
4594}
4595
drh9cbf3422008-01-17 16:22:13 +00004596/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004597**
4598** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004599** that occur while the cursor is on the null row will always
4600** write a NULL.
drh17f71932002-02-21 12:01:27 +00004601*/
drh9cbf3422008-01-17 16:22:13 +00004602case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004603 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004604
drh653b82a2009-06-22 11:10:47 +00004605 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4606 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004607 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004608 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004609 pC->cacheStatus = CACHE_STALE;
danielk1977be51a652008-10-08 17:58:48 +00004610 if( pC->pCursor ){
4611 sqlite3BtreeClearCursor(pC->pCursor);
4612 }
drh17f71932002-02-21 12:01:27 +00004613 break;
4614}
4615
danb18e60b2015-04-01 16:18:00 +00004616/* Opcode: Last P1 P2 P3 * *
drh9562b552002-02-19 15:00:07 +00004617**
drh8af3f772014-07-25 18:01:06 +00004618** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004619** will refer to the last entry in the database table or index.
4620** If the table or index is empty and P2>0, then jump immediately to P2.
4621** If P2 is 0 or if the table or index is not empty, fall through
4622** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004623**
4624** This opcode leaves the cursor configured to move in reverse order,
4625** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004626** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004627*/
drh9cbf3422008-01-17 16:22:13 +00004628case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004629 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004630 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004631 int res;
drh9562b552002-02-19 15:00:07 +00004632
drh653b82a2009-06-22 11:10:47 +00004633 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4634 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004635 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004636 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004637 res = 0;
drh3da046d2013-11-11 03:24:11 +00004638 assert( pCrsr!=0 );
4639 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004640 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004641 pC->deferredMoveto = 0;
4642 pC->cacheStatus = CACHE_STALE;
danb18e60b2015-04-01 16:18:00 +00004643 pC->seekResult = pOp->p3;
drh8af3f772014-07-25 18:01:06 +00004644#ifdef SQLITE_DEBUG
4645 pC->seekOp = OP_Last;
4646#endif
drh688852a2014-02-17 22:40:43 +00004647 if( pOp->p2>0 ){
4648 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004649 if( res ) goto jump_to_p2;
drh9562b552002-02-19 15:00:07 +00004650 }
4651 break;
4652}
4653
drh0342b1f2005-09-01 03:07:44 +00004654
drh9cbf3422008-01-17 16:22:13 +00004655/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004656**
4657** This opcode does exactly the same thing as OP_Rewind except that
4658** it increments an undocumented global variable used for testing.
4659**
4660** Sorting is accomplished by writing records into a sorting index,
4661** then rewinding that index and playing it back from beginning to
4662** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4663** rewinding so that the global variable will be incremented and
4664** regression tests can determine whether or not the optimizer is
4665** correctly optimizing out sorts.
4666*/
drhc6aff302011-09-01 15:32:47 +00004667case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004668case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004669#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004670 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004671 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004672#endif
drh9b47ee32013-08-20 03:13:51 +00004673 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004674 /* Fall through into OP_Rewind */
4675}
drh9cbf3422008-01-17 16:22:13 +00004676/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004677**
drhf0863fe2005-06-12 21:35:51 +00004678** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004679** will refer to the first entry in the database table or index.
dan04489b62014-10-31 20:11:32 +00004680** If the table or index is empty, jump immediately to P2.
4681** If the table or index is not empty, fall through to the following
4682** instruction.
drh8af3f772014-07-25 18:01:06 +00004683**
4684** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004685** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004686** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004687*/
drh9cbf3422008-01-17 16:22:13 +00004688case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004689 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004690 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004691 int res;
drh5e00f6c2001-09-13 13:46:56 +00004692
drh653b82a2009-06-22 11:10:47 +00004693 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4694 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004695 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004696 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004697 res = 1;
drh8af3f772014-07-25 18:01:06 +00004698#ifdef SQLITE_DEBUG
4699 pC->seekOp = OP_Rewind;
4700#endif
dan689ab892011-08-12 15:02:00 +00004701 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004702 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004703 }else{
4704 pCrsr = pC->pCursor;
4705 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004706 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004707 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004708 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00004709 }
drh9c1905f2008-12-10 22:32:56 +00004710 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004711 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004712 VdbeBranchTaken(res!=0,2);
drhf56fa462015-04-13 21:39:54 +00004713 if( res ) goto jump_to_p2;
drh5e00f6c2001-09-13 13:46:56 +00004714 break;
4715}
4716
drh0fd61352014-02-07 02:29:45 +00004717/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004718**
4719** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004720** table or index. If there are no more key/value pairs then fall through
4721** to the following instruction. But if the cursor advance was successful,
4722** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004723**
drh5dad9a32014-07-25 18:37:42 +00004724** The Next opcode is only valid following an SeekGT, SeekGE, or
4725** OP_Rewind opcode used to position the cursor. Next is not allowed
4726** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00004727**
drhf93cd942013-11-21 03:12:25 +00004728** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4729** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004730**
drhe39a7322014-02-03 14:04:11 +00004731** The P3 value is a hint to the btree implementation. If P3==1, that
4732** means P1 is an SQL index and that this instruction could have been
4733** omitted if that index had been unique. P3 is usually 0. P3 is
4734** always either 0 or 1.
4735**
dana205a482011-08-27 18:48:57 +00004736** P4 is always of type P4_ADVANCE. The function pointer points to
4737** sqlite3BtreeNext().
4738**
drhafc266a2010-03-31 17:47:44 +00004739** If P5 is positive and the jump is taken, then event counter
4740** number P5-1 in the prepared statement is incremented.
4741**
drhf93cd942013-11-21 03:12:25 +00004742** See also: Prev, NextIfOpen
4743*/
drh0fd61352014-02-07 02:29:45 +00004744/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004745**
drh5dad9a32014-07-25 18:37:42 +00004746** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004747** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004748*/
drh0fd61352014-02-07 02:29:45 +00004749/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004750**
4751** Back up cursor P1 so that it points to the previous key/data pair in its
4752** table or index. If there is no previous key/value pairs then fall through
4753** to the following instruction. But if the cursor backup was successful,
4754** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004755**
drh8af3f772014-07-25 18:01:06 +00004756**
drh5dad9a32014-07-25 18:37:42 +00004757** The Prev opcode is only valid following an SeekLT, SeekLE, or
4758** OP_Last opcode used to position the cursor. Prev is not allowed
4759** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00004760**
drhf93cd942013-11-21 03:12:25 +00004761** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4762** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004763**
drhe39a7322014-02-03 14:04:11 +00004764** The P3 value is a hint to the btree implementation. If P3==1, that
4765** means P1 is an SQL index and that this instruction could have been
4766** omitted if that index had been unique. P3 is usually 0. P3 is
4767** always either 0 or 1.
4768**
dana205a482011-08-27 18:48:57 +00004769** P4 is always of type P4_ADVANCE. The function pointer points to
4770** sqlite3BtreePrevious().
4771**
drhafc266a2010-03-31 17:47:44 +00004772** If P5 is positive and the jump is taken, then event counter
4773** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004774*/
drh0fd61352014-02-07 02:29:45 +00004775/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004776**
drh5dad9a32014-07-25 18:37:42 +00004777** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004778** open it behaves a no-op.
4779*/
4780case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004781 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004782 int res;
drh8721ce42001-11-07 14:22:00 +00004783
drhf93cd942013-11-21 03:12:25 +00004784 pC = p->apCsr[pOp->p1];
4785 assert( isSorter(pC) );
drh323913c2014-03-23 16:29:23 +00004786 res = 0;
drhf93cd942013-11-21 03:12:25 +00004787 rc = sqlite3VdbeSorterNext(db, pC, &res);
4788 goto next_tail;
4789case OP_PrevIfOpen: /* jump */
4790case OP_NextIfOpen: /* jump */
4791 if( p->apCsr[pOp->p1]==0 ) break;
4792 /* Fall through */
4793case OP_Prev: /* jump */
4794case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004795 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004796 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004797 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00004798 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00004799 assert( pC!=0 );
4800 assert( pC->deferredMoveto==0 );
4801 assert( pC->pCursor );
drhe39a7322014-02-03 14:04:11 +00004802 assert( res==0 || (res==1 && pC->isTable==0) );
4803 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00004804 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4805 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4806 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4807 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00004808
4809 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
4810 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
4811 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
4812 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00004813 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00004814 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
4815 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
4816 || pC->seekOp==OP_Last );
4817
drhf93cd942013-11-21 03:12:25 +00004818 rc = pOp->p4.xAdvance(pC->pCursor, &res);
4819next_tail:
drha3460582008-07-11 21:02:53 +00004820 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004821 VdbeBranchTaken(res==0,2);
drha3460582008-07-11 21:02:53 +00004822 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004823 pC->nullRow = 0;
drh9b47ee32013-08-20 03:13:51 +00004824 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004825#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004826 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004827#endif
drhf56fa462015-04-13 21:39:54 +00004828 goto jump_to_p2_and_check_for_interrupt;
drhf93cd942013-11-21 03:12:25 +00004829 }else{
4830 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004831 }
drh49afe3a2013-07-10 03:05:14 +00004832 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004833}
4834
danielk1977de630352009-05-04 11:42:29 +00004835/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004836** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004837**
drhef8662b2011-06-20 21:47:58 +00004838** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004839** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004840** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004841**
drhaa9b8962008-01-08 02:57:55 +00004842** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004843** insert is likely to be an append.
4844**
mistachkin21a919f2014-02-07 03:28:02 +00004845** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4846** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4847** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00004848**
mistachkin21a919f2014-02-07 03:28:02 +00004849** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
4850** just done a seek to the spot where the new entry is to be inserted.
4851** This flag avoids doing an extra seek.
drh0fd61352014-02-07 02:29:45 +00004852**
drhf0863fe2005-06-12 21:35:51 +00004853** This instruction only works for indices. The equivalent instruction
4854** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004855*/
drhca892a72011-09-03 00:17:51 +00004856case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004857case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004858 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004859 int nKey;
4860 const char *zKey;
4861
drh653b82a2009-06-22 11:10:47 +00004862 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4863 pC = p->apCsr[pOp->p1];
4864 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004865 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004866 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004867 assert( pIn2->flags & MEM_Blob );
drh6546af12013-11-04 15:23:25 +00004868 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhabb78fb2015-06-26 19:43:55 +00004869 assert( pC->pCursor!=0 );
drh3da046d2013-11-11 03:24:11 +00004870 assert( pC->isTable==0 );
4871 rc = ExpandBlob(pIn2);
4872 if( rc==SQLITE_OK ){
drhabb78fb2015-06-26 19:43:55 +00004873 if( pOp->opcode==OP_SorterInsert ){
drh958d2612014-04-18 13:40:07 +00004874 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh3da046d2013-11-11 03:24:11 +00004875 }else{
4876 nKey = pIn2->n;
4877 zKey = pIn2->z;
drhabb78fb2015-06-26 19:43:55 +00004878 rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, "", 0, 0, pOp->p3,
drh3da046d2013-11-11 03:24:11 +00004879 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4880 );
4881 assert( pC->deferredMoveto==0 );
4882 pC->cacheStatus = CACHE_STALE;
danielk1977d908f5a2007-05-11 07:08:28 +00004883 }
drh5e00f6c2001-09-13 13:46:56 +00004884 }
drh5e00f6c2001-09-13 13:46:56 +00004885 break;
4886}
4887
drh4308e342013-11-11 16:55:52 +00004888/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004889** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004890**
drhe14006d2008-03-25 17:23:32 +00004891** The content of P3 registers starting at register P2 form
4892** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004893** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004894*/
drhe14006d2008-03-25 17:23:32 +00004895case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004896 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004897 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004898 int res;
4899 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004900
drhe14006d2008-03-25 17:23:32 +00004901 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004902 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004903 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4904 pC = p->apCsr[pOp->p1];
4905 assert( pC!=0 );
4906 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004907 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004908 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004909 r.pKeyInfo = pC->pKeyInfo;
4910 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00004911 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004912 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004913#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004914 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004915#endif
drh3da046d2013-11-11 03:24:11 +00004916 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4917 if( rc==SQLITE_OK && res==0 ){
danf0ee1d32015-09-12 19:26:11 +00004918 rc = sqlite3BtreeDelete(pCrsr, 0);
drh5e00f6c2001-09-13 13:46:56 +00004919 }
drh3da046d2013-11-11 03:24:11 +00004920 assert( pC->deferredMoveto==0 );
4921 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004922 break;
4923}
4924
drh2133d822008-01-03 18:44:59 +00004925/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004926** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004927**
drh2133d822008-01-03 18:44:59 +00004928** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004929** the end of the index key pointed to by cursor P1. This integer should be
4930** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004931**
drh9437bd22009-02-01 00:29:56 +00004932** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004933*/
drh27a348c2015-04-13 19:14:06 +00004934case OP_IdxRowid: { /* out2 */
drh8721ce42001-11-07 14:22:00 +00004935 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004936 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004937 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004938
drh27a348c2015-04-13 19:14:06 +00004939 pOut = out2Prerelease(p, pOp);
drh653b82a2009-06-22 11:10:47 +00004940 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4941 pC = p->apCsr[pOp->p1];
4942 assert( pC!=0 );
4943 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004944 assert( pCrsr!=0 );
drh3c657212009-11-17 23:59:58 +00004945 pOut->flags = MEM_Null;
drh3da046d2013-11-11 03:24:11 +00004946 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00004947 assert( pC->deferredMoveto==0 );
4948
4949 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
4950 ** out from under the cursor. That will never happend for an IdxRowid
4951 ** opcode, hence the NEVER() arround the check of the return value.
4952 */
4953 rc = sqlite3VdbeCursorRestore(pC);
4954 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4955
drh3da046d2013-11-11 03:24:11 +00004956 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00004957 rowid = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00004958 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
4959 if( rc!=SQLITE_OK ){
4960 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00004961 }
drh3da046d2013-11-11 03:24:11 +00004962 pOut->u.i = rowid;
4963 pOut->flags = MEM_Int;
drh8721ce42001-11-07 14:22:00 +00004964 }
4965 break;
4966}
4967
danielk197761dd5832008-04-18 11:31:12 +00004968/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004969** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004970**
danielk197761dd5832008-04-18 11:31:12 +00004971** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004972** key that omits the PRIMARY KEY. Compare this key value against the index
4973** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4974** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00004975**
danielk197761dd5832008-04-18 11:31:12 +00004976** If the P1 index entry is greater than or equal to the key value
4977** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00004978*/
4979/* Opcode: IdxGT P1 P2 P3 P4 P5
4980** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00004981**
drh4a1d3652014-02-14 15:13:36 +00004982** The P4 register values beginning with P3 form an unpacked index
4983** key that omits the PRIMARY KEY. Compare this key value against the index
4984** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4985** fields at the end.
4986**
4987** If the P1 index entry is greater than the key value
4988** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00004989*/
drh3bb9b932010-08-06 02:10:00 +00004990/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004991** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004992**
danielk197761dd5832008-04-18 11:31:12 +00004993** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004994** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4995** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4996** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004997**
danielk197761dd5832008-04-18 11:31:12 +00004998** If the P1 index entry is less than the key value then jump to P2.
4999** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00005000*/
drh4a1d3652014-02-14 15:13:36 +00005001/* Opcode: IdxLE P1 P2 P3 P4 P5
5002** Synopsis: key=r[P3@P4]
5003**
5004** The P4 register values beginning with P3 form an unpacked index
5005** key that omits the PRIMARY KEY or ROWID. Compare this key value against
5006** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
5007** ROWID on the P1 index.
5008**
5009** If the P1 index entry is less than or equal to the key value then jump
5010** to P2. Otherwise fall through to the next instruction.
5011*/
5012case OP_IdxLE: /* jump */
5013case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00005014case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00005015case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00005016 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00005017 int res;
5018 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00005019
drh653b82a2009-06-22 11:10:47 +00005020 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5021 pC = p->apCsr[pOp->p1];
5022 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00005023 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00005024 assert( pC->pCursor!=0);
5025 assert( pC->deferredMoveto==0 );
5026 assert( pOp->p5==0 || pOp->p5==1 );
5027 assert( pOp->p4type==P4_INT32 );
5028 r.pKeyInfo = pC->pKeyInfo;
5029 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00005030 if( pOp->opcode<OP_IdxLT ){
5031 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00005032 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00005033 }else{
drh4a1d3652014-02-14 15:13:36 +00005034 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00005035 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00005036 }
5037 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005038#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00005039 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00005040#endif
drh2dc06482013-12-11 00:59:10 +00005041 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00005042 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00005043 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
5044 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
5045 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00005046 res = -res;
5047 }else{
drh4a1d3652014-02-14 15:13:36 +00005048 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00005049 res++;
5050 }
drh688852a2014-02-17 22:40:43 +00005051 VdbeBranchTaken(res>0,2);
drhf56fa462015-04-13 21:39:54 +00005052 if( res>0 ) goto jump_to_p2;
drh8721ce42001-11-07 14:22:00 +00005053 break;
5054}
5055
drh98757152008-01-09 23:04:12 +00005056/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00005057**
5058** Delete an entire database table or index whose root page in the database
5059** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00005060**
drh98757152008-01-09 23:04:12 +00005061** The table being destroyed is in the main database file if P3==0. If
5062** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00005063** that is used to store tables create using CREATE TEMPORARY TABLE.
5064**
drh205f48e2004-11-05 00:43:11 +00005065** If AUTOVACUUM is enabled then it is possible that another root page
5066** might be moved into the newly deleted root page in order to keep all
5067** root pages contiguous at the beginning of the database. The former
5068** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00005069** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00005070** movement was required (because the table being dropped was already
5071** the last one in the database) then a zero is stored in register P2.
5072** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00005073**
drhb19a2bc2001-09-16 00:13:26 +00005074** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00005075*/
drh27a348c2015-04-13 19:14:06 +00005076case OP_Destroy: { /* out2 */
danielk1977a0bf2652004-11-04 14:30:04 +00005077 int iMoved;
drh856c1032009-06-02 15:21:42 +00005078 int iDb;
drh3a949872012-09-18 13:20:13 +00005079
drh9e92a472013-06-27 17:40:30 +00005080 assert( p->readOnly==0 );
drh27a348c2015-04-13 19:14:06 +00005081 pOut = out2Prerelease(p, pOp);
drh3c657212009-11-17 23:59:58 +00005082 pOut->flags = MEM_Null;
drh086723a2015-03-24 12:51:52 +00005083 if( db->nVdbeRead > db->nVDestroy+1 ){
danielk1977e6efa742004-11-10 11:55:10 +00005084 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00005085 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00005086 }else{
drh856c1032009-06-02 15:21:42 +00005087 iDb = pOp->p3;
drha7ab6d82014-07-21 15:44:39 +00005088 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00005089 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00005090 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00005091 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00005092 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00005093#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00005094 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00005095 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
5096 /* All OP_Destroy operations occur on the same btree */
5097 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
5098 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00005099 }
drh3765df42006-06-28 18:18:09 +00005100#endif
danielk1977a0bf2652004-11-04 14:30:04 +00005101 }
drh5e00f6c2001-09-13 13:46:56 +00005102 break;
5103}
5104
danielk1977c7af4842008-10-27 13:59:33 +00005105/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00005106**
5107** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00005108** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00005109** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00005110**
drhf57b3392001-10-08 13:22:32 +00005111** The table being clear is in the main database file if P2==0. If
5112** P2==1 then the table to be clear is in the auxiliary database file
5113** that is used to store tables create using CREATE TEMPORARY TABLE.
5114**
shanebe217792009-03-05 04:20:31 +00005115** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00005116** intkey table (an SQL table, not an index). In this case the row change
5117** count is incremented by the number of rows in the table being cleared.
5118** If P3 is greater than zero, then the value stored in register P3 is
5119** also incremented by the number of rows in the table being cleared.
5120**
drhb19a2bc2001-09-16 00:13:26 +00005121** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00005122*/
drh9cbf3422008-01-17 16:22:13 +00005123case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00005124 int nChange;
5125
5126 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00005127 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00005128 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00005129 rc = sqlite3BtreeClearTable(
5130 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
5131 );
5132 if( pOp->p3 ){
5133 p->nChange += nChange;
5134 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00005135 assert( memIsValid(&aMem[pOp->p3]) );
5136 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00005137 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00005138 }
5139 }
drh5edc3122001-09-13 21:53:09 +00005140 break;
5141}
5142
drh65ea12c2014-03-19 17:41:36 +00005143/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00005144**
drh65ea12c2014-03-19 17:41:36 +00005145** Delete all contents from the ephemeral table or sorter
5146** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00005147**
drh65ea12c2014-03-19 17:41:36 +00005148** This opcode only works for cursors used for sorting and
5149** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00005150*/
drh65ea12c2014-03-19 17:41:36 +00005151case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00005152 VdbeCursor *pC;
5153
5154 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
5155 pC = p->apCsr[pOp->p1];
5156 assert( pC!=0 );
drh65ea12c2014-03-19 17:41:36 +00005157 if( pC->pSorter ){
5158 sqlite3VdbeSorterReset(db, pC->pSorter);
5159 }else{
5160 assert( pC->isEphemeral );
5161 rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
5162 }
drh079a3072014-03-19 14:10:55 +00005163 break;
5164}
5165
drh4c583122008-01-04 22:01:03 +00005166/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005167** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00005168**
drh4c583122008-01-04 22:01:03 +00005169** Allocate a new table in the main database file if P1==0 or in the
5170** auxiliary database file if P1==1 or in an attached database if
5171** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005172** register P2
drh5b2fd562001-09-13 15:21:31 +00005173**
drhc6b52df2002-01-04 03:09:29 +00005174** The difference between a table and an index is this: A table must
5175** have a 4-byte integer key and can have arbitrary data. An index
5176** has an arbitrary key but no data.
5177**
drhb19a2bc2001-09-16 00:13:26 +00005178** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00005179*/
drh4c583122008-01-04 22:01:03 +00005180/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005181** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00005182**
drh4c583122008-01-04 22:01:03 +00005183** Allocate a new index in the main database file if P1==0 or in the
5184** auxiliary database file if P1==1 or in an attached database if
5185** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005186** register P2.
drhf57b3392001-10-08 13:22:32 +00005187**
drhc6b52df2002-01-04 03:09:29 +00005188** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00005189*/
drh27a348c2015-04-13 19:14:06 +00005190case OP_CreateIndex: /* out2 */
5191case OP_CreateTable: { /* out2 */
drh856c1032009-06-02 15:21:42 +00005192 int pgno;
drhf328bc82004-05-10 23:29:49 +00005193 int flags;
drh234c39d2004-07-24 03:30:47 +00005194 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005195
drh27a348c2015-04-13 19:14:06 +00005196 pOut = out2Prerelease(p, pOp);
drh856c1032009-06-02 15:21:42 +00005197 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00005198 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005199 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005200 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005201 pDb = &db->aDb[pOp->p1];
5202 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00005203 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00005204 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00005205 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00005206 }else{
drhd4187c72010-08-30 22:15:45 +00005207 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00005208 }
drh234c39d2004-07-24 03:30:47 +00005209 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00005210 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005211 break;
5212}
5213
drh22645842011-03-24 01:34:03 +00005214/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005215**
5216** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005217** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005218**
5219** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005220** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005221*/
drh9cbf3422008-01-17 16:22:13 +00005222case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005223 int iDb;
5224 const char *zMaster;
5225 char *zSql;
5226 InitData initData;
5227
drhbdaec522011-04-04 00:14:43 +00005228 /* Any prepared statement that invokes this opcode will hold mutexes
5229 ** on every btree. This is a prerequisite for invoking
5230 ** sqlite3InitCallback().
5231 */
5232#ifdef SQLITE_DEBUG
5233 for(iDb=0; iDb<db->nDb; iDb++){
5234 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5235 }
5236#endif
drhbdaec522011-04-04 00:14:43 +00005237
drh856c1032009-06-02 15:21:42 +00005238 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005239 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005240 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005241 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00005242 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00005243 initData.db = db;
5244 initData.iDb = pOp->p1;
5245 initData.pzErrMsg = &p->zErrMsg;
5246 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005247 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00005248 db->aDb[iDb].zName, zMaster, pOp->p4.z);
5249 if( zSql==0 ){
5250 rc = SQLITE_NOMEM;
5251 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005252 assert( db->init.busy==0 );
5253 db->init.busy = 1;
5254 initData.rc = SQLITE_OK;
5255 assert( !db->mallocFailed );
5256 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5257 if( rc==SQLITE_OK ) rc = initData.rc;
5258 sqlite3DbFree(db, zSql);
5259 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005260 }
drh3c23a882007-01-09 14:01:13 +00005261 }
drh81028a42012-05-15 18:28:27 +00005262 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00005263 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00005264 goto no_mem;
5265 }
drh234c39d2004-07-24 03:30:47 +00005266 break;
5267}
5268
drh8bfdf722009-06-19 14:06:03 +00005269#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005270/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005271**
5272** Read the sqlite_stat1 table for database P1 and load the content
5273** of that table into the internal index hash table. This will cause
5274** the analysis to be used when preparing all subsequent queries.
5275*/
drh9cbf3422008-01-17 16:22:13 +00005276case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005277 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5278 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00005279 break;
5280}
drh8bfdf722009-06-19 14:06:03 +00005281#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005282
drh98757152008-01-09 23:04:12 +00005283/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005284**
5285** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005286** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005287** is dropped from disk (using the Destroy opcode) in order to keep
5288** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005289** schema consistent with what is on disk.
5290*/
drh9cbf3422008-01-17 16:22:13 +00005291case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005292 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005293 break;
5294}
5295
drh98757152008-01-09 23:04:12 +00005296/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005297**
5298** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005299** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005300** is dropped from disk (using the Destroy opcode)
5301** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005302** schema consistent with what is on disk.
5303*/
drh9cbf3422008-01-17 16:22:13 +00005304case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005305 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005306 break;
5307}
5308
drh98757152008-01-09 23:04:12 +00005309/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005310**
5311** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005312** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005313** is dropped from disk (using the Destroy opcode) in order to keep
5314** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005315** schema consistent with what is on disk.
5316*/
drh9cbf3422008-01-17 16:22:13 +00005317case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005318 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005319 break;
5320}
5321
drh234c39d2004-07-24 03:30:47 +00005322
drhb7f91642004-10-31 02:22:47 +00005323#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005324/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005325**
drh98757152008-01-09 23:04:12 +00005326** Do an analysis of the currently open database. Store in
5327** register P1 the text of an error message describing any problems.
5328** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005329**
drh98757152008-01-09 23:04:12 +00005330** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005331** At most reg(P3) errors will be reported.
5332** In other words, the analysis stops as soon as reg(P1) errors are
5333** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005334**
drh79069752004-05-22 21:30:40 +00005335** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005336** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005337** total.
drh21504322002-06-25 13:16:02 +00005338**
drh98757152008-01-09 23:04:12 +00005339** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005340** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005341**
drh1dcdbc02007-01-27 02:24:54 +00005342** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005343*/
drhaaab5722002-02-19 13:39:21 +00005344case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005345 int nRoot; /* Number of tables to check. (Number of root pages.) */
5346 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5347 int j; /* Loop counter */
5348 int nErr; /* Number of errors reported */
5349 char *z; /* Text of the error report */
5350 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005351
drh1713afb2013-06-28 01:24:57 +00005352 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005353 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005354 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005355 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005356 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005357 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005358 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005359 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005360 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005361 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005362 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005363 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005364 }
5365 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005366 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005367 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh98757152008-01-09 23:04:12 +00005368 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005369 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005370 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005371 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005372 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005373 if( nErr==0 ){
5374 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005375 }else if( z==0 ){
5376 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005377 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005378 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005379 }
drhb7654112008-01-12 12:48:07 +00005380 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005381 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005382 break;
5383}
drhb7f91642004-10-31 02:22:47 +00005384#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005385
drh3d4501e2008-12-04 20:40:10 +00005386/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005387** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005388**
drh3d4501e2008-12-04 20:40:10 +00005389** Insert the integer value held by register P2 into a boolean index
5390** held in register P1.
5391**
5392** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005393*/
drh93952eb2009-11-13 19:43:43 +00005394case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005395 pIn1 = &aMem[pOp->p1];
5396 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005397 assert( (pIn2->flags & MEM_Int)!=0 );
5398 if( (pIn1->flags & MEM_RowSet)==0 ){
5399 sqlite3VdbeMemSetRowSet(pIn1);
5400 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005401 }
drh93952eb2009-11-13 19:43:43 +00005402 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005403 break;
5404}
5405
5406/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005407** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005408**
5409** Extract the smallest value from boolean index P1 and put that value into
5410** register P3. Or, if boolean index P1 is initially empty, leave P3
5411** unchanged and jump to instruction P2.
5412*/
drh93952eb2009-11-13 19:43:43 +00005413case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005414 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005415
drh3c657212009-11-17 23:59:58 +00005416 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005417 if( (pIn1->flags & MEM_RowSet)==0
5418 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005419 ){
5420 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005421 sqlite3VdbeMemSetNull(pIn1);
drh688852a2014-02-17 22:40:43 +00005422 VdbeBranchTaken(1,2);
drhf56fa462015-04-13 21:39:54 +00005423 goto jump_to_p2_and_check_for_interrupt;
drh3d4501e2008-12-04 20:40:10 +00005424 }else{
5425 /* A value was pulled from the index */
drh688852a2014-02-17 22:40:43 +00005426 VdbeBranchTaken(0,2);
drh3c657212009-11-17 23:59:58 +00005427 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005428 }
drh49afe3a2013-07-10 03:05:14 +00005429 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005430}
5431
drh1b26c7c2009-04-22 02:15:47 +00005432/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005433** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005434**
drhade97602009-04-21 15:05:18 +00005435** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005436** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005437** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005438** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005439** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005440**
drh1b26c7c2009-04-22 02:15:47 +00005441** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005442** of integers, where each set contains no duplicates. Each set
5443** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005444** must have P4==0, the final set P4=-1. P4 must be either -1 or
5445** non-negative. For non-negative values of P4 only the lower 4
5446** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005447**
5448** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005449** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005450** (b) when P4==-1 there is no need to insert the value, as it will
5451** never be tested for, and (c) when a value that is part of set X is
5452** inserted, there is no need to search to see if the same value was
5453** previously inserted as part of set X (only if it was previously
5454** inserted as part of some other set).
5455*/
drh1b26c7c2009-04-22 02:15:47 +00005456case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005457 int iSet;
5458 int exists;
5459
drh3c657212009-11-17 23:59:58 +00005460 pIn1 = &aMem[pOp->p1];
5461 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005462 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005463 assert( pIn3->flags&MEM_Int );
5464
drh1b26c7c2009-04-22 02:15:47 +00005465 /* If there is anything other than a rowset object in memory cell P1,
5466 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005467 */
drh733bf1b2009-04-22 00:47:00 +00005468 if( (pIn1->flags & MEM_RowSet)==0 ){
5469 sqlite3VdbeMemSetRowSet(pIn1);
5470 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005471 }
5472
5473 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005474 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005475 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005476 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005477 VdbeBranchTaken(exists!=0,2);
drhf56fa462015-04-13 21:39:54 +00005478 if( exists ) goto jump_to_p2;
danielk19771d461462009-04-21 09:02:45 +00005479 }
5480 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005481 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005482 }
5483 break;
5484}
5485
drh5e00f6c2001-09-13 13:46:56 +00005486
danielk197793758c82005-01-21 08:13:14 +00005487#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005488
drh0fd61352014-02-07 02:29:45 +00005489/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005490**
dan76d462e2009-08-30 11:42:51 +00005491** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005492**
dan76d462e2009-08-30 11:42:51 +00005493** P1 contains the address of the memory cell that contains the first memory
5494** cell in an array of values used as arguments to the sub-program. P2
5495** contains the address to jump to if the sub-program throws an IGNORE
5496** exception using the RAISE() function. Register P3 contains the address
5497** of a memory cell in this (the parent) VM that is used to allocate the
5498** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005499**
5500** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005501**
5502** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005503*/
dan76d462e2009-08-30 11:42:51 +00005504case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005505 int nMem; /* Number of memory registers for sub-program */
5506 int nByte; /* Bytes of runtime space required for sub-program */
5507 Mem *pRt; /* Register to allocate runtime space */
5508 Mem *pMem; /* Used to iterate through memory cells */
5509 Mem *pEnd; /* Last memory cell in new array */
5510 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5511 SubProgram *pProgram; /* Sub-program to execute */
5512 void *t; /* Token identifying trigger */
5513
5514 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005515 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005516 assert( pProgram->nOp>0 );
5517
dan1da40a32009-09-19 17:00:31 +00005518 /* If the p5 flag is clear, then recursive invocation of triggers is
5519 ** disabled for backwards compatibility (p5 is set if this sub-program
5520 ** is really a trigger, not a foreign key action, and the flag set
5521 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005522 **
5523 ** It is recursive invocation of triggers, at the SQL level, that is
5524 ** disabled. In some cases a single trigger may generate more than one
5525 ** SubProgram (if the trigger may be executed with more than one different
5526 ** ON CONFLICT algorithm). SubProgram structures associated with a
5527 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005528 ** variable. */
5529 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005530 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005531 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5532 if( pFrame ) break;
5533 }
5534
danf5894502009-10-07 18:41:19 +00005535 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005536 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00005537 sqlite3VdbeError(p, "too many levels of trigger recursion");
dan165921a2009-08-28 18:53:45 +00005538 break;
5539 }
5540
5541 /* Register pRt is used to store the memory required to save the state
5542 ** of the current program, and the memory required at runtime to execute
5543 ** the trigger program. If this trigger has been fired before, then pRt
5544 ** is already allocated. Otherwise, it must be initialized. */
5545 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005546 /* SubProgram.nMem is set to the number of memory cells used by the
5547 ** program stored in SubProgram.aOp. As well as these, one memory
5548 ** cell is required for each cursor used by the program. Set local
5549 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5550 */
dan65a7cd12009-09-01 12:16:01 +00005551 nMem = pProgram->nMem + pProgram->nCsr;
5552 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005553 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005554 + pProgram->nCsr * sizeof(VdbeCursor *)
5555 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005556 pFrame = sqlite3DbMallocZero(db, nByte);
5557 if( !pFrame ){
5558 goto no_mem;
5559 }
5560 sqlite3VdbeMemRelease(pRt);
5561 pRt->flags = MEM_Frame;
5562 pRt->u.pFrame = pFrame;
5563
5564 pFrame->v = p;
5565 pFrame->nChildMem = nMem;
5566 pFrame->nChildCsr = pProgram->nCsr;
drhf56fa462015-04-13 21:39:54 +00005567 pFrame->pc = (int)(pOp - aOp);
dan165921a2009-08-28 18:53:45 +00005568 pFrame->aMem = p->aMem;
5569 pFrame->nMem = p->nMem;
5570 pFrame->apCsr = p->apCsr;
5571 pFrame->nCursor = p->nCursor;
5572 pFrame->aOp = p->aOp;
5573 pFrame->nOp = p->nOp;
5574 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005575 pFrame->aOnceFlag = p->aOnceFlag;
5576 pFrame->nOnceFlag = p->nOnceFlag;
dane2f771b2014-11-03 15:33:17 +00005577#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005578 pFrame->anExec = p->anExec;
dane2f771b2014-11-03 15:33:17 +00005579#endif
dan165921a2009-08-28 18:53:45 +00005580
5581 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5582 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005583 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005584 pMem->db = db;
5585 }
5586 }else{
5587 pFrame = pRt->u.pFrame;
5588 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5589 assert( pProgram->nCsr==pFrame->nChildCsr );
drhf56fa462015-04-13 21:39:54 +00005590 assert( (int)(pOp - aOp)==pFrame->pc );
dan165921a2009-08-28 18:53:45 +00005591 }
5592
5593 p->nFrame++;
5594 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005595 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005596 pFrame->nChange = p->nChange;
danc3da6672014-10-28 18:24:16 +00005597 pFrame->nDbChange = p->db->nChange;
dan2832ad42009-08-31 15:27:27 +00005598 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005599 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005600 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005601 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005602 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005603 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005604 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005605 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005606 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5607 p->nOnceFlag = pProgram->nOnce;
dane2f771b2014-11-03 15:33:17 +00005608#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
dan43764a82014-11-01 21:00:04 +00005609 p->anExec = 0;
dane2f771b2014-11-03 15:33:17 +00005610#endif
drhf56fa462015-04-13 21:39:54 +00005611 pOp = &aOp[-1];
dan1d8cb212011-12-09 13:24:16 +00005612 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005613
5614 break;
5615}
5616
dan76d462e2009-08-30 11:42:51 +00005617/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005618**
dan76d462e2009-08-30 11:42:51 +00005619** This opcode is only ever present in sub-programs called via the
5620** OP_Program instruction. Copy a value currently stored in a memory
5621** cell of the calling (parent) frame to cell P2 in the current frames
5622** address space. This is used by trigger programs to access the new.*
5623** and old.* values.
dan165921a2009-08-28 18:53:45 +00005624**
dan76d462e2009-08-30 11:42:51 +00005625** The address of the cell in the parent frame is determined by adding
5626** the value of the P1 argument to the value of the P1 argument to the
5627** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005628*/
drh27a348c2015-04-13 19:14:06 +00005629case OP_Param: { /* out2 */
dan65a7cd12009-09-01 12:16:01 +00005630 VdbeFrame *pFrame;
5631 Mem *pIn;
drh27a348c2015-04-13 19:14:06 +00005632 pOut = out2Prerelease(p, pOp);
dan65a7cd12009-09-01 12:16:01 +00005633 pFrame = p->pFrame;
5634 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005635 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5636 break;
5637}
5638
danielk197793758c82005-01-21 08:13:14 +00005639#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005640
dan1da40a32009-09-19 17:00:31 +00005641#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005642/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005643** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005644**
dan0ff297e2009-09-25 17:03:14 +00005645** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5646** If P1 is non-zero, the database constraint counter is incremented
5647** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005648** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005649*/
dan32b09f22009-09-23 17:29:59 +00005650case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005651 if( db->flags & SQLITE_DeferFKs ){
5652 db->nDeferredImmCons += pOp->p2;
5653 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005654 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005655 }else{
dan0ff297e2009-09-25 17:03:14 +00005656 p->nFkConstraint += pOp->p2;
5657 }
5658 break;
5659}
5660
5661/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005662** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005663**
5664** This opcode tests if a foreign key constraint-counter is currently zero.
5665** If so, jump to instruction P2. Otherwise, fall through to the next
5666** instruction.
5667**
5668** If P1 is non-zero, then the jump is taken if the database constraint-counter
5669** is zero (the one that counts deferred constraint violations). If P1 is
5670** zero, the jump is taken if the statement constraint-counter is zero
5671** (immediate foreign key constraint violations).
5672*/
5673case OP_FkIfZero: { /* jump */
5674 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00005675 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00005676 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan0ff297e2009-09-25 17:03:14 +00005677 }else{
drh688852a2014-02-17 22:40:43 +00005678 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drhf56fa462015-04-13 21:39:54 +00005679 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2;
dan32b09f22009-09-23 17:29:59 +00005680 }
dan1da40a32009-09-19 17:00:31 +00005681 break;
5682}
5683#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5684
drh205f48e2004-11-05 00:43:11 +00005685#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005686/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005687** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005688**
dan76d462e2009-08-30 11:42:51 +00005689** P1 is a register in the root frame of this VM (the root frame is
5690** different from the current frame if this instruction is being executed
5691** within a sub-program). Set the value of register P1 to the maximum of
5692** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005693**
5694** This instruction throws an error if the memory cell is not initially
5695** an integer.
5696*/
dan76d462e2009-08-30 11:42:51 +00005697case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005698 VdbeFrame *pFrame;
5699 if( p->pFrame ){
5700 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5701 pIn1 = &pFrame->aMem[pOp->p1];
5702 }else{
drha6c2ed92009-11-14 23:22:23 +00005703 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005704 }
drhec86c722011-12-09 17:27:51 +00005705 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005706 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005707 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005708 sqlite3VdbeMemIntegerify(pIn2);
5709 if( pIn1->u.i<pIn2->u.i){
5710 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005711 }
5712 break;
5713}
5714#endif /* SQLITE_OMIT_AUTOINCREMENT */
5715
drh8b0cf382015-10-06 21:07:06 +00005716/* Opcode: IfPos P1 P2 P3 * *
5717** Synopsis: if r[P1]>0 then r[P1]-=P3, goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005718**
drh16897072015-03-07 00:57:37 +00005719** Register P1 must contain an integer.
mistachkin91a3ecb2015-10-06 21:49:55 +00005720** If the value of register P1 is 1 or greater, subtract P3 from the
drh8b0cf382015-10-06 21:07:06 +00005721** value in P1 and jump to P2.
drh6f58f702006-01-08 05:26:41 +00005722**
drh16897072015-03-07 00:57:37 +00005723** If the initial value of register P1 is less than 1, then the
5724** value is unchanged and control passes through to the next instruction.
danielk1977a2dc3b12005-02-05 12:48:48 +00005725*/
drh9cbf3422008-01-17 16:22:13 +00005726case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005727 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005728 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005729 VdbeBranchTaken( pIn1->u.i>0, 2);
drh8b0cf382015-10-06 21:07:06 +00005730 if( pIn1->u.i>0 ){
5731 pIn1->u.i -= pOp->p3;
5732 goto jump_to_p2;
5733 }
drhec7429a2005-10-06 16:53:14 +00005734 break;
5735}
5736
drh8b0cf382015-10-06 21:07:06 +00005737/* Opcode: SetIfNotPos P1 P2 P3 * *
5738** Synopsis: if r[P1]<=0 then r[P2]=P3
drh15007a92006-01-08 18:10:17 +00005739**
drh8b0cf382015-10-06 21:07:06 +00005740** Register P1 must contain an integer.
5741** If the value of register P1 is not positive (if it is less than 1) then
5742** set the value of register P2 to be the integer P3.
drh15007a92006-01-08 18:10:17 +00005743*/
drh8b0cf382015-10-06 21:07:06 +00005744case OP_SetIfNotPos: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005745 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005746 assert( pIn1->flags&MEM_Int );
drh8b0cf382015-10-06 21:07:06 +00005747 if( pIn1->u.i<=0 ){
5748 pOut = out2Prerelease(p, pOp);
5749 pOut->u.i = pOp->p3;
5750 }
drh15007a92006-01-08 18:10:17 +00005751 break;
5752}
5753
drh16897072015-03-07 00:57:37 +00005754/* Opcode: IfNotZero P1 P2 P3 * *
drh8b0cf382015-10-06 21:07:06 +00005755** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
drhec7429a2005-10-06 16:53:14 +00005756**
drh16897072015-03-07 00:57:37 +00005757** Register P1 must contain an integer. If the content of register P1 is
mistachkin91a3ecb2015-10-06 21:49:55 +00005758** initially nonzero, then subtract P3 from the value in register P1 and
drh8b0cf382015-10-06 21:07:06 +00005759** jump to P2. If register P1 is initially zero, leave it unchanged
5760** and fall through.
drhec7429a2005-10-06 16:53:14 +00005761*/
drh16897072015-03-07 00:57:37 +00005762case OP_IfNotZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005763 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005764 assert( pIn1->flags&MEM_Int );
drh16897072015-03-07 00:57:37 +00005765 VdbeBranchTaken(pIn1->u.i<0, 2);
5766 if( pIn1->u.i ){
drh8b0cf382015-10-06 21:07:06 +00005767 pIn1->u.i -= pOp->p3;
drhf56fa462015-04-13 21:39:54 +00005768 goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00005769 }
5770 break;
5771}
5772
drh16897072015-03-07 00:57:37 +00005773/* Opcode: DecrJumpZero P1 P2 * * *
5774** Synopsis: if (--r[P1])==0 goto P2
5775**
5776** Register P1 must hold an integer. Decrement the value in register P1
5777** then jump to P2 if the new value is exactly zero.
5778*/
5779case OP_DecrJumpZero: { /* jump, in1 */
5780 pIn1 = &aMem[pOp->p1];
5781 assert( pIn1->flags&MEM_Int );
5782 pIn1->u.i--;
drh688852a2014-02-17 22:40:43 +00005783 VdbeBranchTaken(pIn1->u.i==0, 2);
drhf56fa462015-04-13 21:39:54 +00005784 if( pIn1->u.i==0 ) goto jump_to_p2;
drha2a49dc2008-01-02 14:28:13 +00005785 break;
5786}
5787
drh16897072015-03-07 00:57:37 +00005788
5789/* Opcode: JumpZeroIncr P1 P2 * * *
5790** Synopsis: if (r[P1]++)==0 ) goto P2
5791**
5792** The register P1 must contain an integer. If register P1 is initially
5793** zero, then jump to P2. Increment register P1 regardless of whether or
5794** not the jump is taken.
5795*/
5796case OP_JumpZeroIncr: { /* jump, in1 */
5797 pIn1 = &aMem[pOp->p1];
5798 assert( pIn1->flags&MEM_Int );
5799 VdbeBranchTaken(pIn1->u.i==0, 2);
drhf56fa462015-04-13 21:39:54 +00005800 if( (pIn1->u.i++)==0 ) goto jump_to_p2;
drh16897072015-03-07 00:57:37 +00005801 break;
5802}
5803
drhe2d9e7c2015-06-26 18:47:53 +00005804/* Opcode: AggStep0 * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005805** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005806**
drh0bce8352002-02-28 00:41:10 +00005807** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005808** function has P5 arguments. P4 is a pointer to the FuncDef
drhe2d9e7c2015-06-26 18:47:53 +00005809** structure that specifies the function. Register P3 is the
5810** accumulator.
drhe5095352002-02-24 03:25:14 +00005811**
drh98757152008-01-09 23:04:12 +00005812** The P5 arguments are taken from register P2 and its
5813** successors.
drhe5095352002-02-24 03:25:14 +00005814*/
drhe2d9e7c2015-06-26 18:47:53 +00005815/* Opcode: AggStep * P2 P3 P4 P5
5816** Synopsis: accum=r[P3] step(r[P2@P5])
5817**
5818** Execute the step function for an aggregate. The
5819** function has P5 arguments. P4 is a pointer to an sqlite3_context
5820** object that is used to run the function. Register P3 is
5821** as the accumulator.
5822**
5823** The P5 arguments are taken from register P2 and its
5824** successors.
5825**
5826** This opcode is initially coded as OP_AggStep0. On first evaluation,
5827** the FuncDef stored in P4 is converted into an sqlite3_context and
5828** the opcode is changed. In this way, the initialization of the
5829** sqlite3_context only happens once, instead of on each call to the
5830** step function.
5831*/
drh9c7c9132015-06-26 18:16:52 +00005832case OP_AggStep0: {
drh856c1032009-06-02 15:21:42 +00005833 int n;
drh9c7c9132015-06-26 18:16:52 +00005834 sqlite3_context *pCtx;
drhe5095352002-02-24 03:25:14 +00005835
drh9c7c9132015-06-26 18:16:52 +00005836 assert( pOp->p4type==P4_FUNCDEF );
drh856c1032009-06-02 15:21:42 +00005837 n = pOp->p5;
dan3bc9f742013-08-15 16:18:39 +00005838 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c7c9132015-06-26 18:16:52 +00005839 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
5840 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
5841 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
5842 if( pCtx==0 ) goto no_mem;
5843 pCtx->pMem = 0;
5844 pCtx->pFunc = pOp->p4.pFunc;
5845 pCtx->iOp = (int)(pOp - aOp);
5846 pCtx->pVdbe = p;
5847 pCtx->argc = n;
5848 pOp->p4type = P4_FUNCCTX;
5849 pOp->p4.pCtx = pCtx;
5850 pOp->opcode = OP_AggStep;
5851 /* Fall through into OP_AggStep */
5852}
5853case OP_AggStep: {
5854 int i;
5855 sqlite3_context *pCtx;
5856 Mem *pMem;
5857 Mem t;
5858
5859 assert( pOp->p4type==P4_FUNCCTX );
5860 pCtx = pOp->p4.pCtx;
5861 pMem = &aMem[pOp->p3];
5862
5863 /* If this function is inside of a trigger, the register array in aMem[]
5864 ** might change from one evaluation to the next. The next block of code
5865 ** checks to see if the register array has changed, and if so it
5866 ** reinitializes the relavant parts of the sqlite3_context object */
5867 if( pCtx->pMem != pMem ){
5868 pCtx->pMem = pMem;
5869 for(i=pCtx->argc-1; i>=0; i--) pCtx->argv[i] = &aMem[pOp->p2+i];
5870 }
5871
5872#ifdef SQLITE_DEBUG
5873 for(i=0; i<pCtx->argc; i++){
5874 assert( memIsValid(pCtx->argv[i]) );
5875 REGISTER_TRACE(pOp->p2+i, pCtx->argv[i]);
5876 }
5877#endif
5878
drhabfcea22005-09-06 20:36:48 +00005879 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00005880 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9c7c9132015-06-26 18:16:52 +00005881 pCtx->pOut = &t;
5882 pCtx->fErrorOrAux = 0;
5883 pCtx->skipFlag = 0;
5884 (pCtx->pFunc->xStep)(pCtx,pCtx->argc,pCtx->argv); /* IMP: R-24505-23230 */
5885 if( pCtx->fErrorOrAux ){
5886 if( pCtx->isError ){
5887 sqlite3VdbeError(p, "%s", sqlite3_value_text(&t));
5888 rc = pCtx->isError;
5889 }
5890 sqlite3VdbeMemRelease(&t);
5891 }else{
5892 assert( t.flags==MEM_Null );
danielk1977dc1bdc42004-06-11 10:51:27 +00005893 }
drh9c7c9132015-06-26 18:16:52 +00005894 if( pCtx->skipFlag ){
drh7a957892012-02-02 17:35:43 +00005895 assert( pOp[-1].opcode==OP_CollSeq );
5896 i = pOp[-1].p1;
5897 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5898 }
drh5e00f6c2001-09-13 13:46:56 +00005899 break;
5900}
5901
drh98757152008-01-09 23:04:12 +00005902/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005903** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005904**
drh13449892005-09-07 21:22:45 +00005905** Execute the finalizer function for an aggregate. P1 is
5906** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005907**
5908** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005909** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005910** argument is not used by this opcode. It is only there to disambiguate
5911** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005912** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005913** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005914*/
drh9cbf3422008-01-17 16:22:13 +00005915case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005916 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005917 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005918 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005919 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005920 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005921 if( rc ){
drh22c17b82015-05-15 04:13:15 +00005922 sqlite3VdbeError(p, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005923 }
drh2dca8682008-03-21 17:13:13 +00005924 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005925 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005926 if( sqlite3VdbeMemTooBig(pMem) ){
5927 goto too_big;
5928 }
drh5e00f6c2001-09-13 13:46:56 +00005929 break;
5930}
5931
dan5cf53532010-05-01 16:40:20 +00005932#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005933/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005934**
5935** Checkpoint database P1. This is a no-op if P1 is not currently in
drha25165f2014-12-04 04:50:59 +00005936** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL,
5937** RESTART, or TRUNCATE. Write 1 or 0 into mem[P3] if the checkpoint returns
drh30aa3b92011-02-07 23:56:01 +00005938** SQLITE_BUSY or not, respectively. Write the number of pages in the
5939** WAL after the checkpoint into mem[P3+1] and the number of pages
5940** in the WAL that have been checkpointed after the checkpoint
5941** completes into mem[P3+2]. However on an error, mem[P3+1] and
5942** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005943*/
5944case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005945 int i; /* Loop counter */
5946 int aRes[3]; /* Results */
5947 Mem *pMem; /* Write results here */
5948
drh9e92a472013-06-27 17:40:30 +00005949 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005950 aRes[0] = 0;
5951 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005952 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5953 || pOp->p2==SQLITE_CHECKPOINT_FULL
5954 || pOp->p2==SQLITE_CHECKPOINT_RESTART
danf26a1542014-12-02 19:04:54 +00005955 || pOp->p2==SQLITE_CHECKPOINT_TRUNCATE
dancdc1f042010-11-18 12:11:05 +00005956 );
drh30aa3b92011-02-07 23:56:01 +00005957 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005958 if( rc==SQLITE_BUSY ){
5959 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005960 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005961 }
drh30aa3b92011-02-07 23:56:01 +00005962 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5963 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5964 }
dan7c246102010-04-12 19:00:29 +00005965 break;
5966};
dan5cf53532010-05-01 16:40:20 +00005967#endif
drh5e00f6c2001-09-13 13:46:56 +00005968
drhcac29a62010-07-02 19:36:52 +00005969#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00005970/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005971**
5972** Change the journal mode of database P1 to P3. P3 must be one of the
5973** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5974** modes (delete, truncate, persist, off and memory), this is a simple
5975** operation. No IO is required.
5976**
5977** If changing into or out of WAL mode the procedure is more complicated.
5978**
5979** Write a string containing the final journal-mode to register P2.
5980*/
drh27a348c2015-04-13 19:14:06 +00005981case OP_JournalMode: { /* out2 */
dane04dc882010-04-20 18:53:15 +00005982 Btree *pBt; /* Btree to change journal mode of */
5983 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005984 int eNew; /* New journal mode */
5985 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005986#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005987 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005988#endif
dane04dc882010-04-20 18:53:15 +00005989
drh27a348c2015-04-13 19:14:06 +00005990 pOut = out2Prerelease(p, pOp);
drhd80b2332010-05-01 00:59:37 +00005991 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005992 assert( eNew==PAGER_JOURNALMODE_DELETE
5993 || eNew==PAGER_JOURNALMODE_TRUNCATE
5994 || eNew==PAGER_JOURNALMODE_PERSIST
5995 || eNew==PAGER_JOURNALMODE_OFF
5996 || eNew==PAGER_JOURNALMODE_MEMORY
5997 || eNew==PAGER_JOURNALMODE_WAL
5998 || eNew==PAGER_JOURNALMODE_QUERY
5999 );
6000 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00006001 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00006002
dane04dc882010-04-20 18:53:15 +00006003 pBt = db->aDb[pOp->p1].pBt;
6004 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00006005 eOld = sqlite3PagerGetJournalMode(pPager);
6006 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
6007 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00006008
6009#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00006010 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00006011
drhd80b2332010-05-01 00:59:37 +00006012 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00006013 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00006014 */
6015 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00006016 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00006017 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00006018 ){
drh0b9b4302010-06-11 17:01:24 +00006019 eNew = eOld;
dane180c292010-04-26 17:42:56 +00006020 }
6021
drh0b9b4302010-06-11 17:01:24 +00006022 if( (eNew!=eOld)
6023 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
6024 ){
danc0537fe2013-06-28 19:41:43 +00006025 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00006026 rc = SQLITE_ERROR;
drh22c17b82015-05-15 04:13:15 +00006027 sqlite3VdbeError(p,
drh0b9b4302010-06-11 17:01:24 +00006028 "cannot change %s wal mode from within a transaction",
6029 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6030 );
6031 break;
6032 }else{
6033
6034 if( eOld==PAGER_JOURNALMODE_WAL ){
6035 /* If leaving WAL mode, close the log file. If successful, the call
6036 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6037 ** file. An EXCLUSIVE lock may still be held on the database file
6038 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00006039 */
drh0b9b4302010-06-11 17:01:24 +00006040 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00006041 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00006042 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00006043 }
drh242c4f72010-06-22 14:49:39 +00006044 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
6045 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
6046 ** as an intermediate */
6047 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00006048 }
6049
6050 /* Open a transaction on the database file. Regardless of the journal
6051 ** mode, this transaction always uses a rollback journal.
6052 */
6053 assert( sqlite3BtreeIsInTrans(pBt)==0 );
6054 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00006055 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00006056 }
6057 }
6058 }
dan5cf53532010-05-01 16:40:20 +00006059#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00006060
dand956efe2010-06-18 16:13:45 +00006061 if( rc ){
dand956efe2010-06-18 16:13:45 +00006062 eNew = eOld;
6063 }
drh0b9b4302010-06-11 17:01:24 +00006064 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00006065
dane04dc882010-04-20 18:53:15 +00006066 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00006067 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00006068 pOut->n = sqlite3Strlen30(pOut->z);
6069 pOut->enc = SQLITE_UTF8;
6070 sqlite3VdbeChangeEncoding(pOut, encoding);
6071 break;
drhcac29a62010-07-02 19:36:52 +00006072};
6073#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00006074
drhfdbcdee2007-03-27 14:44:50 +00006075#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00006076/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00006077**
6078** Vacuum the entire database. This opcode will cause other virtual
6079** machines to be created and run. It may not be called from within
6080** a transaction.
6081*/
drh9cbf3422008-01-17 16:22:13 +00006082case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00006083 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00006084 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00006085 break;
6086}
drh154d4b22006-09-21 11:02:16 +00006087#endif
drh6f8c91c2003-12-07 00:24:35 +00006088
danielk1977dddbcdc2007-04-26 14:42:34 +00006089#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00006090/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00006091**
6092** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00006093** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00006094** P2. Otherwise, fall through to the next instruction.
6095*/
drh9cbf3422008-01-17 16:22:13 +00006096case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00006097 Btree *pBt;
6098
6099 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006100 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00006101 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00006102 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00006103 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00006104 VdbeBranchTaken(rc==SQLITE_DONE,2);
danielk1977dddbcdc2007-04-26 14:42:34 +00006105 if( rc==SQLITE_DONE ){
danielk1977dddbcdc2007-04-26 14:42:34 +00006106 rc = SQLITE_OK;
drhf56fa462015-04-13 21:39:54 +00006107 goto jump_to_p2;
danielk1977dddbcdc2007-04-26 14:42:34 +00006108 }
6109 break;
6110}
6111#endif
6112
drh98757152008-01-09 23:04:12 +00006113/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00006114**
drh25df48d2014-07-22 14:58:12 +00006115** Cause precompiled statements to expire. When an expired statement
6116** is executed using sqlite3_step() it will either automatically
6117** reprepare itself (if it was originally created using sqlite3_prepare_v2())
6118** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00006119**
6120** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00006121** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00006122*/
drh9cbf3422008-01-17 16:22:13 +00006123case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00006124 if( !pOp->p1 ){
6125 sqlite3ExpirePreparedStatements(db);
6126 }else{
6127 p->expired = 1;
6128 }
6129 break;
6130}
6131
danielk1977c00da102006-01-07 13:21:04 +00006132#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00006133/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00006134** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00006135**
6136** Obtain a lock on a particular table. This instruction is only used when
6137** the shared-cache feature is enabled.
6138**
danielk197796d48e92009-06-29 06:00:37 +00006139** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00006140** on which the lock is acquired. A readlock is obtained if P3==0 or
6141** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00006142**
6143** P2 contains the root-page of the table to lock.
6144**
drh66a51672008-01-03 00:01:23 +00006145** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00006146** used to generate an error message if the lock cannot be obtained.
6147*/
drh9cbf3422008-01-17 16:22:13 +00006148case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00006149 u8 isWriteLock = (u8)pOp->p3;
6150 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
6151 int p1 = pOp->p1;
6152 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00006153 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00006154 assert( isWriteLock==0 || isWriteLock==1 );
6155 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
6156 if( (rc&0xFF)==SQLITE_LOCKED ){
6157 const char *z = pOp->p4.z;
drh22c17b82015-05-15 04:13:15 +00006158 sqlite3VdbeError(p, "database table is locked: %s", z);
danielk1977e0d9e6f2009-07-03 16:25:06 +00006159 }
danielk1977c00da102006-01-07 13:21:04 +00006160 }
6161 break;
6162}
drhb9bb7c12006-06-11 23:41:55 +00006163#endif /* SQLITE_OMIT_SHARED_CACHE */
6164
6165#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006166/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006167**
danielk19773e3a84d2008-08-01 17:37:40 +00006168** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
6169** xBegin method for that table.
6170**
6171** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00006172** within a callback to a virtual table xSync() method. If it is, the error
6173** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00006174*/
drh9cbf3422008-01-17 16:22:13 +00006175case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00006176 VTable *pVTab;
6177 pVTab = pOp->p4.pVtab;
6178 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00006179 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00006180 break;
6181}
6182#endif /* SQLITE_OMIT_VIRTUALTABLE */
6183
6184#ifndef SQLITE_OMIT_VIRTUALTABLE
dan73779452015-03-19 18:56:17 +00006185/* Opcode: VCreate P1 P2 * * *
danielk1977f9e7dda2006-06-16 16:08:53 +00006186**
dan73779452015-03-19 18:56:17 +00006187** P2 is a register that holds the name of a virtual table in database
6188** P1. Call the xCreate method for that table.
danielk1977f9e7dda2006-06-16 16:08:53 +00006189*/
drh9cbf3422008-01-17 16:22:13 +00006190case OP_VCreate: {
dan73779452015-03-19 18:56:17 +00006191 Mem sMem; /* For storing the record being decoded */
drh47464062015-03-21 12:22:16 +00006192 const char *zTab; /* Name of the virtual table */
6193
dan73779452015-03-19 18:56:17 +00006194 memset(&sMem, 0, sizeof(sMem));
6195 sMem.db = db;
drh47464062015-03-21 12:22:16 +00006196 /* Because P2 is always a static string, it is impossible for the
6197 ** sqlite3VdbeMemCopy() to fail */
6198 assert( (aMem[pOp->p2].flags & MEM_Str)!=0 );
6199 assert( (aMem[pOp->p2].flags & MEM_Static)!=0 );
dan73779452015-03-19 18:56:17 +00006200 rc = sqlite3VdbeMemCopy(&sMem, &aMem[pOp->p2]);
drh47464062015-03-21 12:22:16 +00006201 assert( rc==SQLITE_OK );
6202 zTab = (const char*)sqlite3_value_text(&sMem);
6203 assert( zTab || db->mallocFailed );
6204 if( zTab ){
6205 rc = sqlite3VtabCallCreate(db, pOp->p1, zTab, &p->zErrMsg);
dan73779452015-03-19 18:56:17 +00006206 }
6207 sqlite3VdbeMemRelease(&sMem);
drhb9bb7c12006-06-11 23:41:55 +00006208 break;
6209}
6210#endif /* SQLITE_OMIT_VIRTUALTABLE */
6211
6212#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006213/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00006214**
drh66a51672008-01-03 00:01:23 +00006215** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00006216** of that table.
drhb9bb7c12006-06-11 23:41:55 +00006217*/
drh9cbf3422008-01-17 16:22:13 +00006218case OP_VDestroy: {
drh086723a2015-03-24 12:51:52 +00006219 db->nVDestroy++;
danielk19772dca4ac2008-01-03 11:50:29 +00006220 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
drh086723a2015-03-24 12:51:52 +00006221 db->nVDestroy--;
drhb9bb7c12006-06-11 23:41:55 +00006222 break;
6223}
6224#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00006225
drh9eff6162006-06-12 21:59:13 +00006226#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006227/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00006228**
drh66a51672008-01-03 00:01:23 +00006229** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00006230** P1 is a cursor number. This opcode opens a cursor to the virtual
6231** table and stores that cursor in P1.
6232*/
drh9cbf3422008-01-17 16:22:13 +00006233case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00006234 VdbeCursor *pCur;
6235 sqlite3_vtab_cursor *pVtabCursor;
6236 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006237 const sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006238
drh1713afb2013-06-28 01:24:57 +00006239 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00006240 pCur = 0;
6241 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00006242 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006243 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6244 rc = SQLITE_LOCKED;
6245 break;
6246 }
6247 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006248 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00006249 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006250 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00006251 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006252 pVtabCursor->pVtab = pVtab;
6253
mistachkin48864df2013-03-21 21:20:32 +00006254 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00006255 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00006256 if( pCur ){
6257 pCur->pVtabCursor = pVtabCursor;
drha68d6282015-03-24 13:32:53 +00006258 pVtab->nRef++;
danielk1977b7a2f2e2006-06-23 11:34:54 +00006259 }else{
dan995f8b92015-04-27 19:53:55 +00006260 assert( db->mallocFailed );
danielk1977b7a2f2e2006-06-23 11:34:54 +00006261 pModule->xClose(pVtabCursor);
dan995f8b92015-04-27 19:53:55 +00006262 goto no_mem;
danielk1977be718892006-06-23 08:05:19 +00006263 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00006264 }
drh9eff6162006-06-12 21:59:13 +00006265 break;
6266}
6267#endif /* SQLITE_OMIT_VIRTUALTABLE */
6268
6269#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006270/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006271** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006272**
6273** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6274** the filtered result set is empty.
6275**
drh66a51672008-01-03 00:01:23 +00006276** P4 is either NULL or a string that was generated by the xBestIndex
6277** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006278** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006279**
drh9eff6162006-06-12 21:59:13 +00006280** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006281** by P1. The integer query plan parameter to xFilter is stored in register
6282** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006283** xFilter method. Registers P3+2..P3+1+argc are the argc
6284** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006285** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006286**
danielk19776dbee812008-01-03 18:39:41 +00006287** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006288*/
drh9cbf3422008-01-17 16:22:13 +00006289case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006290 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006291 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006292 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006293 Mem *pQuery;
6294 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00006295 sqlite3_vtab_cursor *pVtabCursor;
6296 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006297 VdbeCursor *pCur;
6298 int res;
6299 int i;
6300 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006301
drha6c2ed92009-11-14 23:22:23 +00006302 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006303 pArgc = &pQuery[1];
6304 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006305 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006306 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006307 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00006308 pVtabCursor = pCur->pVtabCursor;
6309 pVtab = pVtabCursor->pVtab;
6310 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006311
drh9cbf3422008-01-17 16:22:13 +00006312 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006313 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006314 nArg = (int)pArgc->u.i;
6315 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006316
drh644a5292006-12-20 14:53:38 +00006317 /* Invoke the xFilter method */
drhf56fa462015-04-13 21:39:54 +00006318 res = 0;
6319 apArg = p->apArg;
6320 for(i = 0; i<nArg; i++){
6321 apArg[i] = &pArgc[i+1];
6322 }
6323 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
6324 sqlite3VtabImportErrmsg(p, pVtab);
6325 if( rc==SQLITE_OK ){
6326 res = pModule->xEof(pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006327 }
drh1d454a32008-01-31 19:34:51 +00006328 pCur->nullRow = 0;
drhf56fa462015-04-13 21:39:54 +00006329 VdbeBranchTaken(res!=0,2);
6330 if( res ) goto jump_to_p2;
drh9eff6162006-06-12 21:59:13 +00006331 break;
6332}
6333#endif /* SQLITE_OMIT_VIRTUALTABLE */
6334
6335#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006336/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006337** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006338**
drh2133d822008-01-03 18:44:59 +00006339** Store the value of the P2-th column of
6340** the row of the virtual-table that the
6341** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006342*/
6343case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006344 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006345 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006346 Mem *pDest;
6347 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006348
drhdfe88ec2008-11-03 20:55:06 +00006349 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006350 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00006351 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006352 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006353 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006354 if( pCur->nullRow ){
6355 sqlite3VdbeMemSetNull(pDest);
6356 break;
6357 }
danielk19773e3a84d2008-08-01 17:37:40 +00006358 pVtab = pCur->pVtabCursor->pVtab;
6359 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006360 assert( pModule->xColumn );
6361 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006362 sContext.pOut = pDest;
6363 MemSetTypeFlag(pDest, MEM_Null);
drhde4fcfd2008-01-19 23:50:26 +00006364 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006365 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006366 if( sContext.isError ){
6367 rc = sContext.isError;
6368 }
drh9bd038f2014-08-27 14:14:06 +00006369 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006370 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006371 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006372
drhde4fcfd2008-01-19 23:50:26 +00006373 if( sqlite3VdbeMemTooBig(pDest) ){
6374 goto too_big;
6375 }
drh9eff6162006-06-12 21:59:13 +00006376 break;
6377}
6378#endif /* SQLITE_OMIT_VIRTUALTABLE */
6379
6380#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006381/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006382**
6383** Advance virtual table P1 to the next row in its result set and
6384** jump to instruction P2. Or, if the virtual table has reached
6385** the end of its result set, then fall through to the next instruction.
6386*/
drh9cbf3422008-01-17 16:22:13 +00006387case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006388 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006389 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006390 int res;
drh856c1032009-06-02 15:21:42 +00006391 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006392
drhc54a6172009-06-02 16:06:03 +00006393 res = 0;
drh856c1032009-06-02 15:21:42 +00006394 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006395 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006396 if( pCur->nullRow ){
6397 break;
6398 }
danielk19773e3a84d2008-08-01 17:37:40 +00006399 pVtab = pCur->pVtabCursor->pVtab;
6400 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006401 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006402
drhde4fcfd2008-01-19 23:50:26 +00006403 /* Invoke the xNext() method of the module. There is no way for the
6404 ** underlying implementation to return an error if one occurs during
6405 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6406 ** data is available) and the error code returned when xColumn or
6407 ** some other method is next invoked on the save virtual table cursor.
6408 */
drhde4fcfd2008-01-19 23:50:26 +00006409 rc = pModule->xNext(pCur->pVtabCursor);
dan016f7812013-08-21 17:35:48 +00006410 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006411 if( rc==SQLITE_OK ){
6412 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006413 }
drh688852a2014-02-17 22:40:43 +00006414 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006415 if( !res ){
6416 /* If there is data, jump to P2 */
drhf56fa462015-04-13 21:39:54 +00006417 goto jump_to_p2_and_check_for_interrupt;
drhde4fcfd2008-01-19 23:50:26 +00006418 }
drh49afe3a2013-07-10 03:05:14 +00006419 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006420}
6421#endif /* SQLITE_OMIT_VIRTUALTABLE */
6422
danielk1977182c4ba2007-06-27 15:53:34 +00006423#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006424/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006425**
drh66a51672008-01-03 00:01:23 +00006426** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006427** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006428** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006429*/
drh9cbf3422008-01-17 16:22:13 +00006430case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006431 sqlite3_vtab *pVtab;
6432 Mem *pName;
6433
danielk1977595a5232009-07-24 17:58:53 +00006434 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006435 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006436 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006437 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006438 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006439 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006440 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006441 testcase( pName->enc==SQLITE_UTF8 );
6442 testcase( pName->enc==SQLITE_UTF16BE );
6443 testcase( pName->enc==SQLITE_UTF16LE );
6444 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6445 if( rc==SQLITE_OK ){
6446 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006447 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006448 p->expired = 0;
6449 }
danielk1977182c4ba2007-06-27 15:53:34 +00006450 break;
6451}
6452#endif
drh4cbdda92006-06-14 19:00:20 +00006453
6454#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006455/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006456** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006457**
drh66a51672008-01-03 00:01:23 +00006458** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006459** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006460** are contiguous memory cells starting at P3 to pass to the xUpdate
6461** invocation. The value in register (P3+P2-1) corresponds to the
6462** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006463**
6464** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006465** The argv[0] element (which corresponds to memory cell P3)
6466** is the rowid of a row to delete. If argv[0] is NULL then no
6467** deletion occurs. The argv[1] element is the rowid of the new
6468** row. This can be NULL to have the virtual table select the new
6469** rowid for itself. The subsequent elements in the array are
6470** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006471**
6472** If P2==1 then no insert is performed. argv[0] is the rowid of
6473** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006474**
6475** P1 is a boolean flag. If it is set to true and the xUpdate call
6476** is successful, then the value returned by sqlite3_last_insert_rowid()
6477** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006478**
6479** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6480** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006481*/
drh9cbf3422008-01-17 16:22:13 +00006482case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006483 sqlite3_vtab *pVtab;
drhf496a7d2015-03-24 14:05:50 +00006484 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006485 int nArg;
6486 int i;
6487 sqlite_int64 rowid;
6488 Mem **apArg;
6489 Mem *pX;
6490
danb061d052011-04-25 18:49:57 +00006491 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6492 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6493 );
drh9e92a472013-06-27 17:40:30 +00006494 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006495 pVtab = pOp->p4.pVtab->pVtab;
drhf496a7d2015-03-24 14:05:50 +00006496 if( pVtab==0 || NEVER(pVtab->pModule==0) ){
6497 rc = SQLITE_LOCKED;
6498 break;
6499 }
6500 pModule = pVtab->pModule;
drh856c1032009-06-02 15:21:42 +00006501 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006502 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006503 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006504 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006505 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006506 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006507 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006508 assert( memIsValid(pX) );
6509 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006510 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006511 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006512 }
danb061d052011-04-25 18:49:57 +00006513 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006514 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006515 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006516 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006517 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006518 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006519 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006520 }
drhd91c1a12013-02-09 13:58:25 +00006521 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006522 if( pOp->p5==OE_Ignore ){
6523 rc = SQLITE_OK;
6524 }else{
6525 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6526 }
6527 }else{
6528 p->nChange++;
6529 }
danielk1977399918f2006-06-14 13:03:23 +00006530 }
drh4cbdda92006-06-14 19:00:20 +00006531 break;
danielk1977399918f2006-06-14 13:03:23 +00006532}
6533#endif /* SQLITE_OMIT_VIRTUALTABLE */
6534
danielk197759a93792008-05-15 17:48:20 +00006535#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6536/* Opcode: Pagecount P1 P2 * * *
6537**
6538** Write the current number of pages in database P1 to memory cell P2.
6539*/
drh27a348c2015-04-13 19:14:06 +00006540case OP_Pagecount: { /* out2 */
6541 pOut = out2Prerelease(p, pOp);
drhb1299152010-03-30 22:58:33 +00006542 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006543 break;
6544}
6545#endif
6546
drh60ac3f42010-11-23 18:59:27 +00006547
6548#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6549/* Opcode: MaxPgcnt P1 P2 P3 * *
6550**
6551** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006552** Do not let the maximum page count fall below the current page count and
6553** do not change the maximum page count value if P3==0.
6554**
drh60ac3f42010-11-23 18:59:27 +00006555** Store the maximum page count after the change in register P2.
6556*/
drh27a348c2015-04-13 19:14:06 +00006557case OP_MaxPgcnt: { /* out2 */
drhc84e0332010-11-23 20:25:08 +00006558 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006559 Btree *pBt;
6560
drh27a348c2015-04-13 19:14:06 +00006561 pOut = out2Prerelease(p, pOp);
drh60ac3f42010-11-23 18:59:27 +00006562 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006563 newMax = 0;
6564 if( pOp->p3 ){
6565 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006566 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006567 }
6568 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006569 break;
6570}
6571#endif
6572
6573
drhaceb31b2014-02-08 01:40:27 +00006574/* Opcode: Init * P2 * P4 *
6575** Synopsis: Start at P2
6576**
6577** Programs contain a single instance of this opcode as the very first
6578** opcode.
drh949f9cd2008-01-12 21:35:57 +00006579**
6580** If tracing is enabled (by the sqlite3_trace()) interface, then
6581** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006582** Or if P4 is blank, use the string returned by sqlite3_sql().
6583**
6584** If P2 is not zero, jump to instruction P2.
drh949f9cd2008-01-12 21:35:57 +00006585*/
drhaceb31b2014-02-08 01:40:27 +00006586case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006587 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006588 char *z;
drh856c1032009-06-02 15:21:42 +00006589
drhaceb31b2014-02-08 01:40:27 +00006590#ifndef SQLITE_OMIT_TRACE
drh37f58e92012-09-04 21:34:26 +00006591 if( db->xTrace
6592 && !p->doingRerun
6593 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6594 ){
drhc3f1d5f2011-05-30 23:42:16 +00006595 z = sqlite3VdbeExpandSql(p, zTrace);
6596 db->xTrace(db->pTraceArg, z);
6597 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006598 }
drh8f8b2312013-10-18 20:03:43 +00006599#ifdef SQLITE_USE_FCNTL_TRACE
6600 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6601 if( zTrace ){
6602 int i;
6603 for(i=0; i<db->nDb; i++){
drha7ab6d82014-07-21 15:44:39 +00006604 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
drh8f8b2312013-10-18 20:03:43 +00006605 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6606 }
6607 }
6608#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006609#ifdef SQLITE_DEBUG
6610 if( (db->flags & SQLITE_SqlTrace)!=0
6611 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6612 ){
6613 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6614 }
6615#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00006616#endif /* SQLITE_OMIT_TRACE */
drhf56fa462015-04-13 21:39:54 +00006617 if( pOp->p2 ) goto jump_to_p2;
drh949f9cd2008-01-12 21:35:57 +00006618 break;
6619}
drh949f9cd2008-01-12 21:35:57 +00006620
drh28935362013-12-07 20:39:19 +00006621#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0df57012015-08-14 15:05:55 +00006622/* Opcode: CursorHint P1 * * P4 *
drh28935362013-12-07 20:39:19 +00006623**
6624** Provide a hint to cursor P1 that it only needs to return rows that
drh0df57012015-08-14 15:05:55 +00006625** satisfy the Expr in P4. TK_REGISTER terms in the P4 expression refer
6626** to values currently held in registers. TK_COLUMN terms in the P4
6627** expression refer to columns in the b-tree to which cursor P1 is pointing.
drh28935362013-12-07 20:39:19 +00006628*/
6629case OP_CursorHint: {
6630 VdbeCursor *pC;
6631
6632 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6633 assert( pOp->p4type==P4_EXPR );
6634 pC = p->apCsr[pOp->p1];
dan91d3a612014-07-15 11:59:44 +00006635 if( pC ){
drh0df57012015-08-14 15:05:55 +00006636 sqlite3BtreeCursorHint(pC->pCursor, BTREE_HINT_RANGE, pOp->p4.pExpr, aMem);
dan91d3a612014-07-15 11:59:44 +00006637 }
drh28935362013-12-07 20:39:19 +00006638 break;
6639}
6640#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh91fd4d42008-01-19 20:11:25 +00006641
6642/* Opcode: Noop * * * * *
6643**
6644** Do nothing. This instruction is often useful as a jump
6645** destination.
drh5e00f6c2001-09-13 13:46:56 +00006646*/
drh91fd4d42008-01-19 20:11:25 +00006647/*
6648** The magic Explain opcode are only inserted when explain==2 (which
6649** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6650** This opcode records information from the optimizer. It is the
6651** the same as a no-op. This opcodesnever appears in a real VM program.
6652*/
6653default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006654 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006655 break;
6656}
6657
6658/*****************************************************************************
6659** The cases of the switch statement above this line should all be indented
6660** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6661** readability. From this point on down, the normal indentation rules are
6662** restored.
6663*****************************************************************************/
6664 }
drh6e142f52000-06-08 13:36:40 +00006665
drh7b396862003-01-01 23:06:20 +00006666#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006667 {
drha01c7c72014-04-25 12:35:31 +00006668 u64 endTime = sqlite3Hwtime();
drh6dc41482015-04-16 17:31:02 +00006669 if( endTime>start ) pOrigOp->cycles += endTime - start;
6670 pOrigOp->cnt++;
drh8178a752003-01-05 21:41:40 +00006671 }
drh7b396862003-01-01 23:06:20 +00006672#endif
6673
drh6e142f52000-06-08 13:36:40 +00006674 /* The following code adds nothing to the actual functionality
6675 ** of the program. It is only here for testing and debugging.
6676 ** On the other hand, it does burn CPU cycles every time through
6677 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6678 */
6679#ifndef NDEBUG
drh6dc41482015-04-16 17:31:02 +00006680 assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] );
drhae7e1512007-05-02 16:51:59 +00006681
drhcf1023c2007-05-08 20:59:49 +00006682#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006683 if( db->flags & SQLITE_VdbeTrace ){
6684 if( rc!=0 ) printf("rc=%d\n",rc);
drh6dc41482015-04-16 17:31:02 +00006685 if( pOrigOp->opflags & (OPFLG_OUT2) ){
6686 registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]);
drh75897232000-05-29 14:26:00 +00006687 }
drh6dc41482015-04-16 17:31:02 +00006688 if( pOrigOp->opflags & OPFLG_OUT3 ){
6689 registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006690 }
drh75897232000-05-29 14:26:00 +00006691 }
danielk1977b5402fb2005-01-12 07:15:04 +00006692#endif /* SQLITE_DEBUG */
6693#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006694 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006695
drha05a7222008-01-19 03:35:58 +00006696 /* If we reach this point, it means that execution is finished with
6697 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006698 */
drha05a7222008-01-19 03:35:58 +00006699vdbe_error_halt:
6700 assert( rc );
6701 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006702 testcase( sqlite3GlobalConfig.xLog!=0 );
6703 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
drhf56fa462015-04-13 21:39:54 +00006704 (int)(pOp - aOp), p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006705 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006706 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6707 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006708 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006709 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006710 }
drh900b31e2007-08-28 02:27:51 +00006711
6712 /* This is the only way out of this procedure. We have to
6713 ** release the mutexes on btrees that were acquired at the
6714 ** top. */
6715vdbe_return:
drh99a66922011-05-13 18:51:42 +00006716 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006717 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006718 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006719 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006720 return rc;
6721
drh023ae032007-05-08 12:12:16 +00006722 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6723 ** is encountered.
6724 */
6725too_big:
drh22c17b82015-05-15 04:13:15 +00006726 sqlite3VdbeError(p, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006727 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006728 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006729
drh98640a32007-06-07 19:08:32 +00006730 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006731 */
6732no_mem:
drh17435752007-08-16 04:30:38 +00006733 db->mallocFailed = 1;
drh22c17b82015-05-15 04:13:15 +00006734 sqlite3VdbeError(p, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006735 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006736 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006737
drhb86ccfb2003-01-28 23:13:10 +00006738 /* Jump to here for any other kind of fatal error. The "rc" variable
6739 ** should hold the error number.
6740 */
6741abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006742 assert( p->zErrMsg==0 );
6743 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006744 if( rc!=SQLITE_IOERR_NOMEM ){
drh22c17b82015-05-15 04:13:15 +00006745 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006746 }
drha05a7222008-01-19 03:35:58 +00006747 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006748
danielk19776f8a5032004-05-10 10:34:51 +00006749 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006750 ** flag.
6751 */
6752abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006753 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006754 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006755 p->rc = rc;
drh22c17b82015-05-15 04:13:15 +00006756 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006757 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006758}