blob: 26ca72b9f4efdf465a2ea58fe0b85e8e7325ba01 [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)
dan689ab892011-08-12 15:02:00 +0000169
danielk19771cc5ed82007-05-16 17:28:43 +0000170/*
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;
danielk1977cd3e8f72008-03-25 09:47:35 +0000217 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000218 pCx->pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000219 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhf25a5072009-11-18 23:01:25 +0000220 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000221 }
danielk197794eb6a12005-12-15 15:22:08 +0000222 }
drh4774b132004-06-12 20:12:51 +0000223 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000224}
225
danielk19773d1bfea2004-05-14 11:00:53 +0000226/*
drh29d72102006-02-09 22:13:41 +0000227** Try to convert a value into a numeric representation if we can
228** do so without loss of information. In other words, if the string
229** looks like a number, convert it into a number. If it does not
230** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000231**
232** If the bTryForInt flag is true, then extra effort is made to give
233** an integer representation. Strings that look like floating point
234** values but which have no fractional component (example: '48.00')
235** will have a MEM_Int representation when bTryForInt is true.
236**
237** If bTryForInt is false, then if the input string contains a decimal
238** point or exponential notation, the result is only MEM_Real, even
239** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000240*/
drhbd9507c2014-08-23 17:21:37 +0000241static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000242 double rValue;
243 i64 iValue;
244 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000245 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000246 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
247 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
248 pRec->u.i = iValue;
249 pRec->flags |= MEM_Int;
250 }else{
drh74eaba42014-09-18 17:52:15 +0000251 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000252 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000253 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000254 }
255}
256
257/*
drh8a512562005-11-14 22:29:05 +0000258** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000259**
drh8a512562005-11-14 22:29:05 +0000260** SQLITE_AFF_INTEGER:
261** SQLITE_AFF_REAL:
262** SQLITE_AFF_NUMERIC:
263** Try to convert pRec to an integer representation or a
264** floating-point representation if an integer representation
265** is not possible. Note that the integer representation is
266** always preferred, even if the affinity is REAL, because
267** an integer representation is more space efficient on disk.
268**
269** SQLITE_AFF_TEXT:
270** Convert pRec to a text representation.
271**
272** SQLITE_AFF_NONE:
273** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000274*/
drh17435752007-08-16 04:30:38 +0000275static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000276 Mem *pRec, /* The value to apply affinity to */
277 char affinity, /* The affinity to be applied */
278 u8 enc /* Use this text encoding */
279){
drh7ea31cc2014-09-18 14:36:00 +0000280 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000281 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
282 || affinity==SQLITE_AFF_NUMERIC );
drhbd9507c2014-08-23 17:21:37 +0000283 if( (pRec->flags & MEM_Int)==0 ){
284 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000285 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000286 }else{
287 sqlite3VdbeIntegerAffinity(pRec);
288 }
drh17c40292004-07-21 02:53:29 +0000289 }
drh7ea31cc2014-09-18 14:36:00 +0000290 }else if( affinity==SQLITE_AFF_TEXT ){
291 /* Only attempt the conversion to TEXT if there is an integer or real
292 ** representation (blob and NULL do not get converted) but no string
293 ** representation.
294 */
295 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
296 sqlite3VdbeMemStringify(pRec, enc, 1);
297 }
danielk19773d1bfea2004-05-14 11:00:53 +0000298 }
299}
300
danielk1977aee18ef2005-03-09 12:26:50 +0000301/*
drh29d72102006-02-09 22:13:41 +0000302** Try to convert the type of a function argument or a result column
303** into a numeric representation. Use either INTEGER or REAL whichever
304** is appropriate. But only do the conversion if it is possible without
305** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000306*/
307int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000308 int eType = sqlite3_value_type(pVal);
309 if( eType==SQLITE_TEXT ){
310 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000311 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000312 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000313 }
drh1b27b8c2014-02-10 03:21:57 +0000314 return eType;
drh29d72102006-02-09 22:13:41 +0000315}
316
317/*
danielk1977aee18ef2005-03-09 12:26:50 +0000318** Exported version of applyAffinity(). This one works on sqlite3_value*,
319** not the internal Mem* type.
320*/
danielk19771e536952007-08-16 10:09:01 +0000321void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000322 sqlite3_value *pVal,
323 u8 affinity,
324 u8 enc
325){
drhb21c8cd2007-08-21 19:33:56 +0000326 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000327}
328
drh3d1d90a2014-03-24 15:00:15 +0000329/*
drhf1a89ed2014-08-23 17:41:15 +0000330** pMem currently only holds a string type (or maybe a BLOB that we can
331** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000332** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000333** accordingly.
334*/
335static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
336 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
337 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000338 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000339 return 0;
340 }
341 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
342 return MEM_Int;
343 }
344 return MEM_Real;
345}
346
347/*
drh3d1d90a2014-03-24 15:00:15 +0000348** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
349** none.
350**
351** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000352** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000353*/
354static u16 numericType(Mem *pMem){
355 if( pMem->flags & (MEM_Int|MEM_Real) ){
356 return pMem->flags & (MEM_Int|MEM_Real);
357 }
358 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000359 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000360 }
361 return 0;
362}
363
danielk1977b5402fb2005-01-12 07:15:04 +0000364#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000365/*
danielk1977ca6b2912004-05-21 10:49:47 +0000366** Write a nice string representation of the contents of cell pMem
367** into buffer zBuf, length nBuf.
368*/
drh74161702006-02-24 02:53:49 +0000369void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000370 char *zCsr = zBuf;
371 int f = pMem->flags;
372
drh57196282004-10-06 15:41:16 +0000373 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000374
danielk1977ca6b2912004-05-21 10:49:47 +0000375 if( f&MEM_Blob ){
376 int i;
377 char c;
378 if( f & MEM_Dyn ){
379 c = 'z';
380 assert( (f & (MEM_Static|MEM_Ephem))==0 );
381 }else if( f & MEM_Static ){
382 c = 't';
383 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
384 }else if( f & MEM_Ephem ){
385 c = 'e';
386 assert( (f & (MEM_Static|MEM_Dyn))==0 );
387 }else{
388 c = 's';
389 }
390
drh5bb3eb92007-05-04 13:15:55 +0000391 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000392 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000393 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000394 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000395 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000396 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000397 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000398 }
399 for(i=0; i<16 && i<pMem->n; i++){
400 char z = pMem->z[i];
401 if( z<32 || z>126 ) *zCsr++ = '.';
402 else *zCsr++ = z;
403 }
404
drhe718efe2007-05-10 21:14:03 +0000405 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000406 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000407 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000408 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000409 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000410 }
danielk1977b1bc9532004-05-22 03:05:33 +0000411 *zCsr = '\0';
412 }else if( f & MEM_Str ){
413 int j, k;
414 zBuf[0] = ' ';
415 if( f & MEM_Dyn ){
416 zBuf[1] = 'z';
417 assert( (f & (MEM_Static|MEM_Ephem))==0 );
418 }else if( f & MEM_Static ){
419 zBuf[1] = 't';
420 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
421 }else if( f & MEM_Ephem ){
422 zBuf[1] = 'e';
423 assert( (f & (MEM_Static|MEM_Dyn))==0 );
424 }else{
425 zBuf[1] = 's';
426 }
427 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000428 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000429 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000430 zBuf[k++] = '[';
431 for(j=0; j<15 && j<pMem->n; j++){
432 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000433 if( c>=0x20 && c<0x7f ){
434 zBuf[k++] = c;
435 }else{
436 zBuf[k++] = '.';
437 }
438 }
439 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000440 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000441 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000442 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000443 }
danielk1977ca6b2912004-05-21 10:49:47 +0000444}
445#endif
446
drh5b6afba2008-01-05 16:29:28 +0000447#ifdef SQLITE_DEBUG
448/*
449** Print the value of a register for tracing purposes:
450*/
drh84e55a82013-11-13 17:58:23 +0000451static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000452 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000453 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000454 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000455 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000456 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000457 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000458 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000459 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000460#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000461 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000462 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000463#endif
drh733bf1b2009-04-22 00:47:00 +0000464 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000465 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000466 }else{
467 char zBuf[200];
468 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000469 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000470 }
471}
drh84e55a82013-11-13 17:58:23 +0000472static void registerTrace(int iReg, Mem *p){
473 printf("REG[%d] = ", iReg);
474 memTracePrint(p);
475 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000476}
477#endif
478
479#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000480# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000481#else
482# define REGISTER_TRACE(R,M)
483#endif
484
danielk197784ac9d02004-05-18 09:58:06 +0000485
drh7b396862003-01-01 23:06:20 +0000486#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000487
488/*
489** hwtime.h contains inline assembler code for implementing
490** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000491*/
shane9bcbdad2008-05-29 20:22:37 +0000492#include "hwtime.h"
493
drh7b396862003-01-01 23:06:20 +0000494#endif
495
danielk1977fd7f0452008-12-17 17:30:26 +0000496#ifndef NDEBUG
497/*
498** This function is only called from within an assert() expression. It
499** checks that the sqlite3.nTransaction variable is correctly set to
500** the number of non-transaction savepoints currently in the
501** linked list starting at sqlite3.pSavepoint.
502**
503** Usage:
504**
505** assert( checkSavepointCount(db) );
506*/
507static int checkSavepointCount(sqlite3 *db){
508 int n = 0;
509 Savepoint *p;
510 for(p=db->pSavepoint; p; p=p->pNext) n++;
511 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
512 return 1;
513}
514#endif
515
drhb9755982010-07-24 16:34:37 +0000516
517/*
drh0fd61352014-02-07 02:29:45 +0000518** Execute as much of a VDBE program as we can.
519** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000520*/
danielk19774adee202004-05-08 08:23:19 +0000521int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000522 Vdbe *p /* The VDBE */
523){
shaneh84f4b2f2010-02-26 01:46:54 +0000524 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000525 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000526 Op *pOp; /* Current operation */
527 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000528 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000529 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000530 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000531 int iCompare = 0; /* Result of last OP_Compare operation */
532 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000533#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000534 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000535#endif
drha6c2ed92009-11-14 23:22:23 +0000536 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000537 Mem *pIn1 = 0; /* 1st input operand */
538 Mem *pIn2 = 0; /* 2nd input operand */
539 Mem *pIn3 = 0; /* 3rd input operand */
540 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000541 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000542 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000543#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000544 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000545#endif
drh856c1032009-06-02 15:21:42 +0000546 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000547
drhca48c902008-01-18 14:08:24 +0000548 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000549 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000550 if( p->rc==SQLITE_NOMEM ){
551 /* This happens if a malloc() inside a call to sqlite3_column_text() or
552 ** sqlite3_column_text16() failed. */
553 goto no_mem;
554 }
drh3a840692003-01-29 22:58:26 +0000555 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000556 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000557 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000558 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000559 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000560 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000561 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000562 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000563 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000564#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
565 if( db->xProgress ){
566 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000567 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000568 if( nProgressLimit==0 ){
569 nProgressLimit = db->nProgressOps;
570 }else{
571 nProgressLimit %= (unsigned)db->nProgressOps;
572 }
573 }
574#endif
drh3c23a882007-01-09 14:01:13 +0000575#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000576 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000577 if( p->pc==0
578 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
579 ){
drh3c23a882007-01-09 14:01:13 +0000580 int i;
drh84e55a82013-11-13 17:58:23 +0000581 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000582 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000583 if( p->db->flags & SQLITE_VdbeListing ){
584 printf("VDBE Program Listing:\n");
585 for(i=0; i<p->nOp; i++){
586 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
587 }
drh3c23a882007-01-09 14:01:13 +0000588 }
drh84e55a82013-11-13 17:58:23 +0000589 if( p->db->flags & SQLITE_VdbeEQP ){
590 for(i=0; i<p->nOp; i++){
591 if( aOp[i].opcode==OP_Explain ){
592 if( once ) printf("VDBE Query Plan:\n");
593 printf("%s\n", aOp[i].p4.z);
594 once = 0;
595 }
596 }
597 }
598 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000599 }
danielk19772d1d86f2008-06-20 14:59:51 +0000600 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000601#endif
drhb86ccfb2003-01-28 23:13:10 +0000602 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000603 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000604 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000605#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000606 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000607#endif
drhbf159fa2013-06-25 22:01:22 +0000608 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000609 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000610
danielk19778b60e0f2005-01-12 09:10:39 +0000611 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000612 */
danielk19778b60e0f2005-01-12 09:10:39 +0000613#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000614 if( db->flags & SQLITE_VdbeTrace ){
615 sqlite3VdbePrintOp(stdout, pc, pOp);
drh75897232000-05-29 14:26:00 +0000616 }
drh3f7d4e42004-07-24 14:35:58 +0000617#endif
618
drh6e142f52000-06-08 13:36:40 +0000619
drhf6038712004-02-08 18:07:34 +0000620 /* Check to see if we need to simulate an interrupt. This only happens
621 ** if we have a special test build.
622 */
623#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000624 if( sqlite3_interrupt_count>0 ){
625 sqlite3_interrupt_count--;
626 if( sqlite3_interrupt_count==0 ){
627 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000628 }
629 }
630#endif
631
drhb5b407e2012-08-29 10:28:43 +0000632 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000633 ** external allocations out of mem[p2] and set mem[p2] to be
634 ** an undefined integer. Opcodes will either fill in the integer
635 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000636 */
drha6c2ed92009-11-14 23:22:23 +0000637 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000638 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
639 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000640 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000641 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000642 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +0000643 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
drh3c657212009-11-17 23:59:58 +0000644 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000645 }
drh3c657212009-11-17 23:59:58 +0000646
647 /* Sanity checking on other operands */
648#ifdef SQLITE_DEBUG
649 if( (pOp->opflags & OPFLG_IN1)!=0 ){
650 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000651 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000652 assert( memIsValid(&aMem[pOp->p1]) );
drh75fd0542014-03-01 16:24:44 +0000653 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000654 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
655 }
656 if( (pOp->opflags & OPFLG_IN2)!=0 ){
657 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000658 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000659 assert( memIsValid(&aMem[pOp->p2]) );
drh75fd0542014-03-01 16:24:44 +0000660 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000661 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
662 }
663 if( (pOp->opflags & OPFLG_IN3)!=0 ){
664 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000665 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000666 assert( memIsValid(&aMem[pOp->p3]) );
drh75fd0542014-03-01 16:24:44 +0000667 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000668 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
669 }
670 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
671 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000672 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000673 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000674 }
675 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
676 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000677 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000678 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000679 }
680#endif
drh93952eb2009-11-13 19:43:43 +0000681
drh75897232000-05-29 14:26:00 +0000682 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000683
drh5e00f6c2001-09-13 13:46:56 +0000684/*****************************************************************************
685** What follows is a massive switch statement where each case implements a
686** separate instruction in the virtual machine. If we follow the usual
687** indentation conventions, each case should be indented by 6 spaces. But
688** that is a lot of wasted space on the left margin. So the code within
689** the switch statement will break with convention and be flush-left. Another
690** big comment (similar to this one) will mark the point in the code where
691** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000692**
693** The formatting of each case is important. The makefile for SQLite
694** generates two C files "opcodes.h" and "opcodes.c" by scanning this
695** file looking for lines that begin with "case OP_". The opcodes.h files
696** will be filled with #defines that give unique integer values to each
697** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000698** each string is the symbolic name for the corresponding opcode. If the
699** case statement is followed by a comment of the form "/# same as ... #/"
700** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000701**
drh9cbf3422008-01-17 16:22:13 +0000702** Other keywords in the comment that follows each case are used to
703** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
704** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
705** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000706**
drhac82fcf2002-09-08 17:23:41 +0000707** Documentation about VDBE opcodes is generated by scanning this file
708** for lines of that contain "Opcode:". That line and all subsequent
709** comment lines are used in the generation of the opcode.html documentation
710** file.
711**
712** SUMMARY:
713**
714** Formatting is important to scripts that scan this file.
715** Do not deviate from the formatting style currently in use.
716**
drh5e00f6c2001-09-13 13:46:56 +0000717*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000718
drh9cbf3422008-01-17 16:22:13 +0000719/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000720**
721** An unconditional jump to address P2.
722** The next instruction executed will be
723** the one at index P2 from the beginning of
724** the program.
drhfe705102014-03-06 13:38:37 +0000725**
726** The P1 parameter is not actually used by this opcode. However, it
727** is sometimes set to 1 instead of 0 as a hint to the command-line shell
728** that this Goto is the bottom of a loop and that the lines from P2 down
729** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000730*/
drh9cbf3422008-01-17 16:22:13 +0000731case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000732 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000733
734 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
735 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
736 ** completion. Check to see if sqlite3_interrupt() has been called
737 ** or if the progress callback needs to be invoked.
738 **
739 ** This code uses unstructured "goto" statements and does not look clean.
740 ** But that is not due to sloppy coding habits. The code is written this
741 ** way for performance, to avoid having to run the interrupt and progress
742 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
743 ** faster according to "valgrind --tool=cachegrind" */
744check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000745 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000746#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
747 /* Call the progress callback if it is configured and the required number
748 ** of VDBE ops have been executed (either since this invocation of
749 ** sqlite3VdbeExec() or since last time the progress callback was called).
750 ** If the progress callback returns non-zero, exit the virtual machine with
751 ** a return code SQLITE_ABORT.
752 */
drh0d1961e2013-07-25 16:27:51 +0000753 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000754 assert( db->nProgressOps!=0 );
755 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
756 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000757 rc = SQLITE_INTERRUPT;
758 goto vdbe_error_halt;
759 }
drh49afe3a2013-07-10 03:05:14 +0000760 }
761#endif
762
drh5e00f6c2001-09-13 13:46:56 +0000763 break;
764}
drh75897232000-05-29 14:26:00 +0000765
drh2eb95372008-06-06 15:04:36 +0000766/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000767**
drh2eb95372008-06-06 15:04:36 +0000768** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000769** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000770*/
drhb8475df2011-12-09 16:21:19 +0000771case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000772 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000773 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000774 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000775 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000776 pIn1->flags = MEM_Int;
777 pIn1->u.i = pc;
778 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000779 pc = pOp->p2 - 1;
780 break;
781}
782
drh2eb95372008-06-06 15:04:36 +0000783/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000784**
drh81cf13e2014-02-07 18:27:53 +0000785** Jump to the next instruction after the address in register P1. After
786** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000787*/
drh2eb95372008-06-06 15:04:36 +0000788case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000789 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000790 assert( pIn1->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000791 pc = (int)pIn1->u.i;
drh81cf13e2014-02-07 18:27:53 +0000792 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000793 break;
794}
795
drhed71a832014-02-07 19:18:10 +0000796/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000797**
drh5dad9a32014-07-25 18:37:42 +0000798** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000799** located at address P3.
800**
drh5dad9a32014-07-25 18:37:42 +0000801** If P2!=0 then the coroutine implementation immediately follows
802** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000803** address P2.
drh5dad9a32014-07-25 18:37:42 +0000804**
805** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000806*/
807case OP_InitCoroutine: { /* jump */
drhed71a832014-02-07 19:18:10 +0000808 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
809 assert( pOp->p2>=0 && pOp->p2<p->nOp );
810 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000811 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000812 assert( !VdbeMemDynamic(pOut) );
813 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000814 pOut->flags = MEM_Int;
drhed71a832014-02-07 19:18:10 +0000815 if( pOp->p2 ) pc = pOp->p2 - 1;
drh81cf13e2014-02-07 18:27:53 +0000816 break;
817}
818
819/* Opcode: EndCoroutine P1 * * * *
820**
drhbc5cf382014-08-06 01:08:07 +0000821** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000822** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000823** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000824**
825** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000826*/
827case OP_EndCoroutine: { /* in1 */
828 VdbeOp *pCaller;
829 pIn1 = &aMem[pOp->p1];
830 assert( pIn1->flags==MEM_Int );
831 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
832 pCaller = &aOp[pIn1->u.i];
833 assert( pCaller->opcode==OP_Yield );
834 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
835 pc = pCaller->p2 - 1;
836 pIn1->flags = MEM_Undefined;
837 break;
838}
839
840/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000841**
drh5dad9a32014-07-25 18:37:42 +0000842** Swap the program counter with the value in register P1. This
843** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000844**
drh5dad9a32014-07-25 18:37:42 +0000845** If the coroutine that is launched by this instruction ends with
846** Yield or Return then continue to the next instruction. But if
847** the coroutine launched by this instruction ends with
848** EndCoroutine, then jump to P2 rather than continuing with the
849** next instruction.
850**
851** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000852*/
drh81cf13e2014-02-07 18:27:53 +0000853case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000854 int pcDest;
drh3c657212009-11-17 23:59:58 +0000855 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000856 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000857 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000858 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000859 pIn1->u.i = pc;
860 REGISTER_TRACE(pOp->p1, pIn1);
861 pc = pcDest;
862 break;
863}
864
drhf9c8ce32013-11-05 13:33:55 +0000865/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh0fd61352014-02-07 02:29:45 +0000866** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000867**
drhef8662b2011-06-20 21:47:58 +0000868** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000869** parameter P1, P2, and P4 as if this were a Halt instruction. If the
870** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000871** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000872*/
873case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000874 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000875 if( (pIn3->flags & MEM_Null)==0 ) break;
876 /* Fall through into OP_Halt */
877}
drhe00ee6e2008-06-20 15:24:01 +0000878
drhf9c8ce32013-11-05 13:33:55 +0000879/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000880**
drh3d4501e2008-12-04 20:40:10 +0000881** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000882** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000883**
drh92f02c32004-09-02 14:57:08 +0000884** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
885** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
886** For errors, it can be some other value. If P1!=0 then P2 will determine
887** whether or not to rollback the current transaction. Do not rollback
888** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
889** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000890** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000891**
drh66a51672008-01-03 00:01:23 +0000892** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000893**
drhf9c8ce32013-11-05 13:33:55 +0000894** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
895**
896** 0: (no change)
897** 1: NOT NULL contraint failed: P4
898** 2: UNIQUE constraint failed: P4
899** 3: CHECK constraint failed: P4
900** 4: FOREIGN KEY constraint failed: P4
901**
902** If P5 is not zero and P4 is NULL, then everything after the ":" is
903** omitted.
904**
drh9cfcf5d2002-01-29 18:41:24 +0000905** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000906** every program. So a jump past the last instruction of the program
907** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000908*/
drh9cbf3422008-01-17 16:22:13 +0000909case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000910 const char *zType;
911 const char *zLogFmt;
912
dan165921a2009-08-28 18:53:45 +0000913 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000914 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000915 VdbeFrame *pFrame = p->pFrame;
916 p->pFrame = pFrame->pParent;
917 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000918 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000919 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000920 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000921 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000922 /* Instruction pc is the OP_Program that invoked the sub-program
923 ** currently being halted. If the p2 instruction of this OP_Halt
924 ** instruction is set to OE_Ignore, then the sub-program is throwing
925 ** an IGNORE exception. In this case jump to the address specified
926 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000927 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000928 }
drhbbe879d2009-11-14 18:04:35 +0000929 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000930 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000931 break;
932 }
drh92f02c32004-09-02 14:57:08 +0000933 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000934 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000935 p->pc = pc;
drhf9c8ce32013-11-05 13:33:55 +0000936 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000937 if( pOp->p5 ){
938 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
939 "FOREIGN KEY" };
940 assert( pOp->p5>=1 && pOp->p5<=4 );
941 testcase( pOp->p5==1 );
942 testcase( pOp->p5==2 );
943 testcase( pOp->p5==3 );
944 testcase( pOp->p5==4 );
945 zType = azType[pOp->p5-1];
946 }else{
947 zType = 0;
948 }
drh4308e342013-11-11 16:55:52 +0000949 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000950 zLogFmt = "abort at %d in [%s]: %s";
951 if( zType && pOp->p4.z ){
952 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
953 zType, pOp->p4.z);
954 }else if( pOp->p4.z ){
955 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000956 }else{
drh4308e342013-11-11 16:55:52 +0000957 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000958 }
959 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000960 }
drh92f02c32004-09-02 14:57:08 +0000961 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000962 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000963 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000964 p->rc = rc = SQLITE_BUSY;
965 }else{
drhd91c1a12013-02-09 13:58:25 +0000966 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000967 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000968 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000969 }
drh900b31e2007-08-28 02:27:51 +0000970 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000971}
drhc61053b2000-06-04 12:58:36 +0000972
drh4c583122008-01-04 22:01:03 +0000973/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000974** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000975**
drh9cbf3422008-01-17 16:22:13 +0000976** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000977*/
drh4c583122008-01-04 22:01:03 +0000978case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000979 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000980 break;
981}
982
drh4c583122008-01-04 22:01:03 +0000983/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000984** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +0000985**
drh66a51672008-01-03 00:01:23 +0000986** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000987** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000988*/
drh4c583122008-01-04 22:01:03 +0000989case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000990 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000991 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000992 break;
993}
drh4f26d6c2004-05-26 23:25:30 +0000994
drh13573c72010-01-12 17:04:07 +0000995#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000996/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000997** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +0000998**
drh4c583122008-01-04 22:01:03 +0000999** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001000** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001001*/
drh4c583122008-01-04 22:01:03 +00001002case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
1003 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001004 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001005 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001006 break;
1007}
drh13573c72010-01-12 17:04:07 +00001008#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001009
drh3c84ddf2008-01-09 02:15:38 +00001010/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001011** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001012**
drh66a51672008-01-03 00:01:23 +00001013** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhbc5cf382014-08-06 01:08:07 +00001014** into a String before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001015** this transformation, the length of string P4 is computed and stored
1016** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001017*/
drh4c583122008-01-04 22:01:03 +00001018case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +00001019 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +00001020 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001021 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001022
1023#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001024 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001025 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1026 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001027 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001028 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001029 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001030 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001031 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001032 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001033 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001034 }
drh66a51672008-01-03 00:01:23 +00001035 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001036 pOp->p4.z = pOut->z;
1037 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001038 }
danielk197793758c82005-01-21 08:13:14 +00001039#endif
drhbb4957f2008-03-20 14:03:29 +00001040 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001041 goto too_big;
1042 }
1043 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001044}
drhf4479502004-05-27 03:12:53 +00001045
drh4c583122008-01-04 22:01:03 +00001046/* Opcode: String P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001047** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001048**
drh9cbf3422008-01-17 16:22:13 +00001049** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +00001050*/
drh4c583122008-01-04 22:01:03 +00001051case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +00001052 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +00001053 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1054 pOut->z = pOp->p4.z;
1055 pOut->n = pOp->p1;
1056 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001057 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +00001058 break;
1059}
1060
drh053a1282012-09-19 21:15:46 +00001061/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001062** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001063**
drhb8475df2011-12-09 16:21:19 +00001064** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001065** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001066** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001067** set to NULL.
1068**
1069** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1070** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1071** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001072*/
drh4c583122008-01-04 22:01:03 +00001073case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +00001074 int cnt;
drh053a1282012-09-19 21:15:46 +00001075 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +00001076 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001077 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001078 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +00001079 while( cnt>0 ){
1080 pOut++;
1081 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001082 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001083 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001084 cnt--;
1085 }
drhf0863fe2005-06-12 21:35:51 +00001086 break;
1087}
1088
drh05a86c52014-02-16 01:55:49 +00001089/* Opcode: SoftNull P1 * * * *
1090** Synopsis: r[P1]=NULL
1091**
1092** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1093** instruction, but do not free any string or blob memory associated with
1094** the register, so that if the value was a string or blob that was
1095** previously copied using OP_SCopy, the copies will continue to be valid.
1096*/
1097case OP_SoftNull: {
1098 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
1099 pOut = &aMem[pOp->p1];
1100 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1101 break;
1102}
drhf0863fe2005-06-12 21:35:51 +00001103
drha5750cf2014-02-07 13:20:31 +00001104/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001105** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001106**
drh9de221d2008-01-05 06:51:30 +00001107** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001108** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001109*/
drh4c583122008-01-04 22:01:03 +00001110case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001111 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001112 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001113 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001114 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001115 break;
1116}
1117
drheaf52d82010-05-12 13:50:23 +00001118/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001119** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001120**
drheaf52d82010-05-12 13:50:23 +00001121** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001122**
drh0fd61352014-02-07 02:29:45 +00001123** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001124** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001125*/
drheaf52d82010-05-12 13:50:23 +00001126case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001127 Mem *pVar; /* Value being transferred */
1128
drheaf52d82010-05-12 13:50:23 +00001129 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001130 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001131 pVar = &p->aVar[pOp->p1 - 1];
1132 if( sqlite3VdbeMemTooBig(pVar) ){
1133 goto too_big;
drh023ae032007-05-08 12:12:16 +00001134 }
drheaf52d82010-05-12 13:50:23 +00001135 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1136 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001137 break;
1138}
danielk1977295ba552004-05-19 10:34:51 +00001139
drhb21e7c72008-06-22 12:37:57 +00001140/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001141** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001142**
drh079a3072014-03-19 14:10:55 +00001143** Move the P3 values in register P1..P1+P3-1 over into
1144** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001145** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001146** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1147** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001148*/
drhe1349cb2008-04-01 00:36:10 +00001149case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001150 int n; /* Number of registers left to copy */
1151 int p1; /* Register to copy from */
1152 int p2; /* Register to copy to */
1153
drhe09f43f2013-11-21 04:18:31 +00001154 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001155 p1 = pOp->p1;
1156 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001157 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001158 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001159
drha6c2ed92009-11-14 23:22:23 +00001160 pIn1 = &aMem[p1];
1161 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001162 do{
dan3bc9f742013-08-15 16:18:39 +00001163 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1164 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001165 assert( memIsValid(pIn1) );
1166 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001167 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001168#ifdef SQLITE_DEBUG
1169 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1170 pOut->pScopyFrom += p1 - pOp->p2;
1171 }
1172#endif
drhb21e7c72008-06-22 12:37:57 +00001173 REGISTER_TRACE(p2++, pOut);
1174 pIn1++;
1175 pOut++;
drh079a3072014-03-19 14:10:55 +00001176 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001177 break;
1178}
1179
drhe8e4af72012-09-21 00:04:28 +00001180/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001181** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001182**
drhe8e4af72012-09-21 00:04:28 +00001183** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001184**
1185** This instruction makes a deep copy of the value. A duplicate
1186** is made of any string or blob constant. See also OP_SCopy.
1187*/
drhe8e4af72012-09-21 00:04:28 +00001188case OP_Copy: {
1189 int n;
1190
1191 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001192 pIn1 = &aMem[pOp->p1];
1193 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001194 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001195 while( 1 ){
1196 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1197 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001198#ifdef SQLITE_DEBUG
1199 pOut->pScopyFrom = 0;
1200#endif
drhe8e4af72012-09-21 00:04:28 +00001201 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1202 if( (n--)==0 ) break;
1203 pOut++;
1204 pIn1++;
1205 }
drhe1349cb2008-04-01 00:36:10 +00001206 break;
1207}
1208
drhb1fdb2a2008-01-05 04:06:03 +00001209/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001210** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001211**
drh9cbf3422008-01-17 16:22:13 +00001212** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001213**
1214** This instruction makes a shallow copy of the value. If the value
1215** is a string or blob, then the copy is only a pointer to the
1216** original and hence if the original changes so will the copy.
1217** Worse, if the original is deallocated, the copy becomes invalid.
1218** Thus the program must guarantee that the original will not change
1219** during the lifetime of the copy. Use OP_Copy to make a complete
1220** copy.
1221*/
drh26198bb2013-10-31 11:15:09 +00001222case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001223 pIn1 = &aMem[pOp->p1];
1224 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001225 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001226 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001227#ifdef SQLITE_DEBUG
1228 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1229#endif
drh5e00f6c2001-09-13 13:46:56 +00001230 break;
1231}
drh75897232000-05-29 14:26:00 +00001232
drh9cbf3422008-01-17 16:22:13 +00001233/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001234** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001235**
shane21e7feb2008-05-30 15:59:49 +00001236** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001237** results. This opcode causes the sqlite3_step() call to terminate
1238** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001239** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001240** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001241*/
drh9cbf3422008-01-17 16:22:13 +00001242case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001243 Mem *pMem;
1244 int i;
1245 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001246 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001247 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001248
drhe6400b92013-11-13 23:48:46 +00001249#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1250 /* Run the progress counter just before returning.
1251 */
1252 if( db->xProgress!=0
1253 && nVmStep>=nProgressLimit
1254 && db->xProgress(db->pProgressArg)!=0
1255 ){
1256 rc = SQLITE_INTERRUPT;
1257 goto vdbe_error_halt;
1258 }
1259#endif
1260
dan32b09f22009-09-23 17:29:59 +00001261 /* If this statement has violated immediate foreign key constraints, do
1262 ** not return the number of rows modified. And do not RELEASE the statement
1263 ** transaction. It needs to be rolled back. */
1264 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1265 assert( db->flags&SQLITE_CountRows );
1266 assert( p->usesStmtJournal );
1267 break;
1268 }
1269
danielk1977bd434552009-03-18 10:33:00 +00001270 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1271 ** DML statements invoke this opcode to return the number of rows
1272 ** modified to the user. This is the only way that a VM that
1273 ** opens a statement transaction may invoke this opcode.
1274 **
1275 ** In case this is such a statement, close any statement transaction
1276 ** opened by this VM before returning control to the user. This is to
1277 ** ensure that statement-transactions are always nested, not overlapping.
1278 ** If the open statement-transaction is not closed here, then the user
1279 ** may step another VM that opens its own statement transaction. This
1280 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001281 **
1282 ** The statement transaction is never a top-level transaction. Hence
1283 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001284 */
1285 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001286 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1287 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001288 break;
1289 }
1290
drhd4e70eb2008-01-02 00:34:36 +00001291 /* Invalidate all ephemeral cursor row caches */
1292 p->cacheCtr = (p->cacheCtr + 2)|1;
1293
1294 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001295 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001296 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001297 */
drha6c2ed92009-11-14 23:22:23 +00001298 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001299 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001300 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001301 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001302 assert( (pMem[i].flags & MEM_Ephem)==0
1303 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001304 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001305 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001306 }
drh28039692008-03-17 16:54:01 +00001307 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001308
1309 /* Return SQLITE_ROW
1310 */
drhd4e70eb2008-01-02 00:34:36 +00001311 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001312 rc = SQLITE_ROW;
1313 goto vdbe_return;
1314}
1315
drh5b6afba2008-01-05 16:29:28 +00001316/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001317** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001318**
drh5b6afba2008-01-05 16:29:28 +00001319** Add the text in register P1 onto the end of the text in
1320** register P2 and store the result in register P3.
1321** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001322**
1323** P3 = P2 || P1
1324**
1325** It is illegal for P1 and P3 to be the same register. Sometimes,
1326** if P3 is the same register as P2, the implementation is able
1327** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001328*/
drh5b6afba2008-01-05 16:29:28 +00001329case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001330 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001331
drh3c657212009-11-17 23:59:58 +00001332 pIn1 = &aMem[pOp->p1];
1333 pIn2 = &aMem[pOp->p2];
1334 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001335 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001336 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001337 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001338 break;
drh5e00f6c2001-09-13 13:46:56 +00001339 }
drha0c06522009-06-17 22:50:41 +00001340 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001341 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001342 Stringify(pIn2, encoding);
1343 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001344 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001345 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001346 }
drh9c1905f2008-12-10 22:32:56 +00001347 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001348 goto no_mem;
1349 }
drhc91b2fd2014-03-01 18:13:23 +00001350 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001351 if( pOut!=pIn2 ){
1352 memcpy(pOut->z, pIn2->z, pIn2->n);
1353 }
1354 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001355 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001356 pOut->z[nByte+1] = 0;
1357 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001358 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001359 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001360 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001361 break;
1362}
drh75897232000-05-29 14:26:00 +00001363
drh3c84ddf2008-01-09 02:15:38 +00001364/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001365** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001366**
drh60a713c2008-01-21 16:22:45 +00001367** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001368** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001369** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001370*/
drh3c84ddf2008-01-09 02:15:38 +00001371/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001372** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001373**
drh3c84ddf2008-01-09 02:15:38 +00001374**
shane21e7feb2008-05-30 15:59:49 +00001375** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001376** and store the result in register P3.
1377** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001378*/
drh3c84ddf2008-01-09 02:15:38 +00001379/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001380** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001381**
drh60a713c2008-01-21 16:22:45 +00001382** Subtract the value in register P1 from the value in register P2
1383** and store the result in register P3.
1384** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001385*/
drh9cbf3422008-01-17 16:22:13 +00001386/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001387** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001388**
drh60a713c2008-01-21 16:22:45 +00001389** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001390** and store the result in register P3 (P3=P2/P1). If the value in
1391** register P1 is zero, then the result is NULL. If either input is
1392** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001393*/
drh9cbf3422008-01-17 16:22:13 +00001394/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001395** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001396**
drh40864a12013-11-15 18:58:37 +00001397** Compute the remainder after integer register P2 is divided by
1398** register P1 and store the result in register P3.
1399** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001400** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001401*/
drh5b6afba2008-01-05 16:29:28 +00001402case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1403case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1404case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1405case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1406case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001407 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001408 u16 flags; /* Combined MEM_* flags from both inputs */
1409 u16 type1; /* Numeric type of left operand */
1410 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001411 i64 iA; /* Integer value of left operand */
1412 i64 iB; /* Integer value of right operand */
1413 double rA; /* Real value of left operand */
1414 double rB; /* Real value of right operand */
1415
drh3c657212009-11-17 23:59:58 +00001416 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001417 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001418 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001419 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001420 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001421 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001422 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
drh3d1d90a2014-03-24 15:00:15 +00001423 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001424 iA = pIn1->u.i;
1425 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001426 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001427 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001428 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1429 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1430 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001431 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001432 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001433 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001434 iB /= iA;
drh75897232000-05-29 14:26:00 +00001435 break;
1436 }
drhbf4133c2001-10-13 02:59:08 +00001437 default: {
drh856c1032009-06-02 15:21:42 +00001438 if( iA==0 ) goto arithmetic_result_is_null;
1439 if( iA==-1 ) iA = 1;
1440 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001441 break;
1442 }
drh75897232000-05-29 14:26:00 +00001443 }
drh856c1032009-06-02 15:21:42 +00001444 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001445 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001446 }else{
drhbe707b32012-12-10 22:19:14 +00001447 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001448fp_math:
drh856c1032009-06-02 15:21:42 +00001449 rA = sqlite3VdbeRealValue(pIn1);
1450 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001451 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001452 case OP_Add: rB += rA; break;
1453 case OP_Subtract: rB -= rA; break;
1454 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001455 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001456 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001457 if( rA==(double)0 ) goto arithmetic_result_is_null;
1458 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001459 break;
1460 }
drhbf4133c2001-10-13 02:59:08 +00001461 default: {
shane75ac1de2009-06-09 18:58:52 +00001462 iA = (i64)rA;
1463 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001464 if( iA==0 ) goto arithmetic_result_is_null;
1465 if( iA==-1 ) iA = 1;
1466 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001467 break;
1468 }
drh5e00f6c2001-09-13 13:46:56 +00001469 }
drhc5a7b512010-01-13 16:25:42 +00001470#ifdef SQLITE_OMIT_FLOATING_POINT
1471 pOut->u.i = rB;
1472 MemSetTypeFlag(pOut, MEM_Int);
1473#else
drh856c1032009-06-02 15:21:42 +00001474 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001475 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001476 }
drh74eaba42014-09-18 17:52:15 +00001477 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001478 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001479 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001480 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001481 }
drhc5a7b512010-01-13 16:25:42 +00001482#endif
drh5e00f6c2001-09-13 13:46:56 +00001483 }
1484 break;
1485
drha05a7222008-01-19 03:35:58 +00001486arithmetic_result_is_null:
1487 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001488 break;
1489}
1490
drh7a957892012-02-02 17:35:43 +00001491/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001492**
drh66a51672008-01-03 00:01:23 +00001493** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001494** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1495** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001496** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001497**
drh7a957892012-02-02 17:35:43 +00001498** If P1 is not zero, then it is a register that a subsequent min() or
1499** max() aggregate will set to 1 if the current row is not the minimum or
1500** maximum. The P1 register is initialized to 0 by this instruction.
1501**
danielk1977dc1bdc42004-06-11 10:51:27 +00001502** The interface used by the implementation of the aforementioned functions
1503** to retrieve the collation sequence set by this opcode is not available
1504** publicly, only to user functions defined in func.c.
1505*/
drh9cbf3422008-01-17 16:22:13 +00001506case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001507 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001508 if( pOp->p1 ){
1509 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1510 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001511 break;
1512}
1513
drh98757152008-01-09 23:04:12 +00001514/* Opcode: Function P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001515** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001516**
drh66a51672008-01-03 00:01:23 +00001517** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001518** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001519** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001520** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001521**
drh13449892005-09-07 21:22:45 +00001522** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001523** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001524** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001525** whether meta data associated with a user function argument using the
1526** sqlite3_set_auxdata() API may be safely retained until the next
1527** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001528**
drh13449892005-09-07 21:22:45 +00001529** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001530*/
drh0bce8352002-02-28 00:41:10 +00001531case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001532 int i;
drh6810ce62004-01-31 19:22:56 +00001533 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001534 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001535 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001536 int n;
drh1350b032002-02-27 19:00:20 +00001537
drh856c1032009-06-02 15:21:42 +00001538 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001539 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001540 assert( apVal || n==0 );
dan3bc9f742013-08-15 16:18:39 +00001541 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9bd038f2014-08-27 14:14:06 +00001542 ctx.pOut = &aMem[pOp->p3];
1543 memAboutToChange(p, ctx.pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001544
dan3bc9f742013-08-15 16:18:39 +00001545 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001546 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001547 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001548 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001549 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001550 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001551 Deephemeralize(pArg);
drhab5cd702010-04-07 14:32:11 +00001552 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001553 }
danielk197751ad0ec2004-05-24 12:39:02 +00001554
dan0c547792013-07-18 17:12:08 +00001555 assert( pOp->p4type==P4_FUNCDEF );
1556 ctx.pFunc = pOp->p4.pFunc;
dan0c547792013-07-18 17:12:08 +00001557 ctx.iOp = pc;
1558 ctx.pVdbe = p;
drh9bd038f2014-08-27 14:14:06 +00001559 MemSetTypeFlag(ctx.pOut, MEM_Null);
danielk1977a7a8e142008-02-13 18:25:27 +00001560
drh9b47ee32013-08-20 03:13:51 +00001561 ctx.fErrorOrAux = 0;
drhd36e1042013-09-06 13:10:12 +00001562 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001563 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001564 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001565 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001566 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001567 }
drh99a66922011-05-13 18:51:42 +00001568 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001569 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001570 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001571
drh90669c12006-01-20 15:45:36 +00001572 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001573 if( ctx.fErrorOrAux ){
1574 if( ctx.isError ){
drh9bd038f2014-08-27 14:14:06 +00001575 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
drh9b47ee32013-08-20 03:13:51 +00001576 rc = ctx.isError;
1577 }
1578 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001579 }
1580
drh9cbf3422008-01-17 16:22:13 +00001581 /* Copy the result of the function into register P3 */
drh9bd038f2014-08-27 14:14:06 +00001582 sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
1583 if( sqlite3VdbeMemTooBig(ctx.pOut) ){
drh023ae032007-05-08 12:12:16 +00001584 goto too_big;
1585 }
drh7b94e7f2011-04-04 12:29:20 +00001586
drh9bd038f2014-08-27 14:14:06 +00001587 REGISTER_TRACE(pOp->p3, ctx.pOut);
1588 UPDATE_MAX_BLOBSIZE(ctx.pOut);
drh8e0a2f92002-02-23 23:45:45 +00001589 break;
1590}
1591
drh98757152008-01-09 23:04:12 +00001592/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001593** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001594**
drh98757152008-01-09 23:04:12 +00001595** Take the bit-wise AND of the values in register P1 and P2 and
1596** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001597** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001598*/
drh98757152008-01-09 23:04:12 +00001599/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001600** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001601**
drh98757152008-01-09 23:04:12 +00001602** Take the bit-wise OR of the values in register P1 and P2 and
1603** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001604** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001605*/
drh98757152008-01-09 23:04:12 +00001606/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001607** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001608**
drh98757152008-01-09 23:04:12 +00001609** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001610** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001611** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001612** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001613*/
drh98757152008-01-09 23:04:12 +00001614/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001615** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001616**
drh98757152008-01-09 23:04:12 +00001617** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001618** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001619** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001620** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001621*/
drh5b6afba2008-01-05 16:29:28 +00001622case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1623case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1624case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1625case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001626 i64 iA;
1627 u64 uA;
1628 i64 iB;
1629 u8 op;
drh6810ce62004-01-31 19:22:56 +00001630
drh3c657212009-11-17 23:59:58 +00001631 pIn1 = &aMem[pOp->p1];
1632 pIn2 = &aMem[pOp->p2];
1633 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001634 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001635 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001636 break;
1637 }
drh158b9cb2011-03-05 20:59:46 +00001638 iA = sqlite3VdbeIntValue(pIn2);
1639 iB = sqlite3VdbeIntValue(pIn1);
1640 op = pOp->opcode;
1641 if( op==OP_BitAnd ){
1642 iA &= iB;
1643 }else if( op==OP_BitOr ){
1644 iA |= iB;
1645 }else if( iB!=0 ){
1646 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1647
1648 /* If shifting by a negative amount, shift in the other direction */
1649 if( iB<0 ){
1650 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1651 op = 2*OP_ShiftLeft + 1 - op;
1652 iB = iB>(-64) ? -iB : 64;
1653 }
1654
1655 if( iB>=64 ){
1656 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1657 }else{
1658 memcpy(&uA, &iA, sizeof(uA));
1659 if( op==OP_ShiftLeft ){
1660 uA <<= iB;
1661 }else{
1662 uA >>= iB;
1663 /* Sign-extend on a right shift of a negative number */
1664 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1665 }
1666 memcpy(&iA, &uA, sizeof(iA));
1667 }
drhbf4133c2001-10-13 02:59:08 +00001668 }
drh158b9cb2011-03-05 20:59:46 +00001669 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001670 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001671 break;
1672}
1673
drh8558cde2008-01-05 05:20:10 +00001674/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001675** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001676**
danielk19770cdc0222008-06-26 18:04:03 +00001677** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001678** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001679**
drh8558cde2008-01-05 05:20:10 +00001680** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001681*/
drh9cbf3422008-01-17 16:22:13 +00001682case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001683 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001684 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001685 sqlite3VdbeMemIntegerify(pIn1);
1686 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001687 break;
1688}
1689
drh9cbf3422008-01-17 16:22:13 +00001690/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001691**
drh9cbf3422008-01-17 16:22:13 +00001692** Force the value in register P1 to be an integer. If the value
1693** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001694** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001695** raise an SQLITE_MISMATCH exception.
1696*/
drh9cbf3422008-01-17 16:22:13 +00001697case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001698 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001699 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001700 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001701 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001702 if( (pIn1->flags & MEM_Int)==0 ){
1703 if( pOp->p2==0 ){
1704 rc = SQLITE_MISMATCH;
1705 goto abort_due_to_error;
1706 }else{
1707 pc = pOp->p2 - 1;
1708 break;
1709 }
drh8aff1012001-12-22 14:49:24 +00001710 }
drh8aff1012001-12-22 14:49:24 +00001711 }
drh83b301b2013-11-20 00:59:02 +00001712 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001713 break;
1714}
1715
drh13573c72010-01-12 17:04:07 +00001716#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001717/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001718**
drh2133d822008-01-03 18:44:59 +00001719** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001720**
drh8a512562005-11-14 22:29:05 +00001721** This opcode is used when extracting information from a column that
1722** has REAL affinity. Such column values may still be stored as
1723** integers, for space efficiency, but after extraction we want them
1724** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001725*/
drh9cbf3422008-01-17 16:22:13 +00001726case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001727 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001728 if( pIn1->flags & MEM_Int ){
1729 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001730 }
drh487e2622005-06-25 18:42:14 +00001731 break;
1732}
drh13573c72010-01-12 17:04:07 +00001733#endif
drh487e2622005-06-25 18:42:14 +00001734
drh8df447f2005-11-01 15:48:24 +00001735#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001736/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001737** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001738**
drh4169e432014-08-25 20:11:52 +00001739** Force the value in register P1 to be the type defined by P2.
1740**
1741** <ul>
1742** <li value="97"> TEXT
1743** <li value="98"> BLOB
1744** <li value="99"> NUMERIC
1745** <li value="100"> INTEGER
1746** <li value="101"> REAL
1747** </ul>
drh487e2622005-06-25 18:42:14 +00001748**
1749** A NULL value is not changed by this routine. It remains NULL.
1750*/
drh4169e432014-08-25 20:11:52 +00001751case OP_Cast: { /* in1 */
drh7ea31cc2014-09-18 14:36:00 +00001752 assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001753 testcase( pOp->p2==SQLITE_AFF_TEXT );
1754 testcase( pOp->p2==SQLITE_AFF_NONE );
1755 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1756 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1757 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001758 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001759 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001760 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001761 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001762 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001763 break;
1764}
drh8a512562005-11-14 22:29:05 +00001765#endif /* SQLITE_OMIT_CAST */
1766
drh35573352008-01-08 23:54:25 +00001767/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001768** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001769**
drh35573352008-01-08 23:54:25 +00001770** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1771** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001772**
drh35573352008-01-08 23:54:25 +00001773** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1774** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001775** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001776**
drh35573352008-01-08 23:54:25 +00001777** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001778** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001779** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001780** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001781** affinity is used. Note that the affinity conversions are stored
1782** back into the input registers P1 and P3. So this opcode can cause
1783** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001784**
1785** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001786** the values are compared. If both values are blobs then memcmp() is
1787** used to determine the results of the comparison. If both values
1788** are text, then the appropriate collating function specified in
1789** P4 is used to do the comparison. If P4 is not specified then
1790** memcmp() is used to compare text string. If both values are
1791** numeric, then a numeric comparison is used. If the two values
1792** are of different types, then numbers are considered less than
1793** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001794**
drh35573352008-01-08 23:54:25 +00001795** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1796** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001797**
1798** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1799** equal to one another, provided that they do not have their MEM_Cleared
1800** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001801*/
drh9cbf3422008-01-17 16:22:13 +00001802/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001803** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001804**
drh35573352008-01-08 23:54:25 +00001805** This works just like the Lt opcode except that the jump is taken if
1806** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001807** additional information.
drh6a2fe092009-09-23 02:29:36 +00001808**
1809** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1810** true or false and is never NULL. If both operands are NULL then the result
1811** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001812** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001813** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001814*/
drh9cbf3422008-01-17 16:22:13 +00001815/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001816** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001817**
drh35573352008-01-08 23:54:25 +00001818** This works just like the Lt opcode except that the jump is taken if
1819** the operands in registers P1 and P3 are equal.
1820** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001821**
1822** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1823** true or false and is never NULL. If both operands are NULL then the result
1824** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001825** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001826** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001827*/
drh9cbf3422008-01-17 16:22:13 +00001828/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001829** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001830**
drh35573352008-01-08 23:54:25 +00001831** This works just like the Lt opcode except that the jump is taken if
1832** the content of register P3 is less than or equal to the content of
1833** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001834*/
drh9cbf3422008-01-17 16:22:13 +00001835/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001836** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001837**
drh35573352008-01-08 23:54:25 +00001838** This works just like the Lt opcode except that the jump is taken if
1839** the content of register P3 is greater than the content of
1840** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001841*/
drh9cbf3422008-01-17 16:22:13 +00001842/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001843** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001844**
drh35573352008-01-08 23:54:25 +00001845** This works just like the Lt opcode except that the jump is taken if
1846** the content of register P3 is greater than or equal to the content of
1847** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001848*/
drh9cbf3422008-01-17 16:22:13 +00001849case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1850case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1851case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1852case OP_Le: /* same as TK_LE, jump, in1, in3 */
1853case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1854case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001855 int res; /* Result of the comparison of pIn1 against pIn3 */
1856 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001857 u16 flags1; /* Copy of initial value of pIn1->flags */
1858 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001859
drh3c657212009-11-17 23:59:58 +00001860 pIn1 = &aMem[pOp->p1];
1861 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001862 flags1 = pIn1->flags;
1863 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001864 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001865 /* One or both operands are NULL */
1866 if( pOp->p5 & SQLITE_NULLEQ ){
1867 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1868 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1869 ** or not both operands are null.
1870 */
1871 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001872 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001873 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drh053a1282012-09-19 21:15:46 +00001874 if( (flags1&MEM_Null)!=0
1875 && (flags3&MEM_Null)!=0
1876 && (flags3&MEM_Cleared)==0
1877 ){
1878 res = 0; /* Results are equal */
1879 }else{
1880 res = 1; /* Results are not equal */
1881 }
drh6a2fe092009-09-23 02:29:36 +00001882 }else{
1883 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1884 ** then the result is always NULL.
1885 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1886 */
drh688852a2014-02-17 22:40:43 +00001887 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001888 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001889 MemSetTypeFlag(pOut, MEM_Null);
1890 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001891 }else{
drhf4345e42014-02-18 11:31:59 +00001892 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001893 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1894 pc = pOp->p2-1;
1895 }
drh6a2fe092009-09-23 02:29:36 +00001896 }
1897 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001898 }
drh6a2fe092009-09-23 02:29:36 +00001899 }else{
1900 /* Neither operand is NULL. Do a comparison. */
1901 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001902 if( affinity>=SQLITE_AFF_NUMERIC ){
drhe7a34662014-09-19 22:44:20 +00001903 if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001904 applyNumericAffinity(pIn1,0);
1905 }
drhe7a34662014-09-19 22:44:20 +00001906 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001907 applyNumericAffinity(pIn3,0);
1908 }
1909 }else if( affinity==SQLITE_AFF_TEXT ){
1910 if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001911 testcase( pIn1->flags & MEM_Int );
1912 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001913 sqlite3VdbeMemStringify(pIn1, encoding, 1);
1914 }
1915 if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001916 testcase( pIn3->flags & MEM_Int );
1917 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001918 sqlite3VdbeMemStringify(pIn3, encoding, 1);
1919 }
drh6a2fe092009-09-23 02:29:36 +00001920 }
drh6a2fe092009-09-23 02:29:36 +00001921 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drhca5506b2014-09-17 23:37:38 +00001922 if( pIn1->flags & MEM_Zero ){
1923 sqlite3VdbeMemExpandBlob(pIn1);
1924 flags1 &= ~MEM_Zero;
1925 }
1926 if( pIn3->flags & MEM_Zero ){
1927 sqlite3VdbeMemExpandBlob(pIn3);
1928 flags3 &= ~MEM_Zero;
1929 }
drh24a09622014-09-18 16:28:59 +00001930 if( db->mallocFailed ) goto no_mem;
drh6a2fe092009-09-23 02:29:36 +00001931 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001932 }
danielk1977a37cdde2004-05-16 11:15:36 +00001933 switch( pOp->opcode ){
1934 case OP_Eq: res = res==0; break;
1935 case OP_Ne: res = res!=0; break;
1936 case OP_Lt: res = res<0; break;
1937 case OP_Le: res = res<=0; break;
1938 case OP_Gt: res = res>0; break;
1939 default: res = res>=0; break;
1940 }
1941
drh35573352008-01-08 23:54:25 +00001942 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001943 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001944 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001945 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001946 pOut->u.i = res;
1947 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001948 }else{
drhf4345e42014-02-18 11:31:59 +00001949 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh688852a2014-02-17 22:40:43 +00001950 if( res ){
1951 pc = pOp->p2-1;
1952 }
danielk1977a37cdde2004-05-16 11:15:36 +00001953 }
danb7dca7d2010-03-05 16:32:12 +00001954 /* Undo any changes made by applyAffinity() to the input registers. */
drhca5506b2014-09-17 23:37:38 +00001955 pIn1->flags = flags1;
1956 pIn3->flags = flags3;
danielk1977a37cdde2004-05-16 11:15:36 +00001957 break;
1958}
drhc9b84a12002-06-20 11:36:48 +00001959
drh0acb7e42008-06-25 00:12:41 +00001960/* Opcode: Permutation * * * P4 *
1961**
shanebe217792009-03-05 04:20:31 +00001962** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001963** of integers in P4.
1964**
drh953f7612012-12-07 22:18:54 +00001965** The permutation is only valid until the next OP_Compare that has
1966** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1967** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001968*/
1969case OP_Permutation: {
1970 assert( pOp->p4type==P4_INTARRAY );
1971 assert( pOp->p4.ai );
1972 aPermute = pOp->p4.ai;
1973 break;
1974}
1975
drh953f7612012-12-07 22:18:54 +00001976/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00001977** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00001978**
drh710c4842010-08-30 01:17:20 +00001979** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1980** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001981** the comparison for use by the next OP_Jump instruct.
1982**
drh0ca10df2012-12-08 13:26:23 +00001983** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1984** determined by the most recent OP_Permutation operator. If the
1985** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1986** order.
1987**
drh0acb7e42008-06-25 00:12:41 +00001988** P4 is a KeyInfo structure that defines collating sequences and sort
1989** orders for the comparison. The permutation applies to registers
1990** only. The KeyInfo elements are used sequentially.
1991**
1992** The comparison is a sort comparison, so NULLs compare equal,
1993** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001994** and strings are less than blobs.
1995*/
1996case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001997 int n;
1998 int i;
1999 int p1;
2000 int p2;
2001 const KeyInfo *pKeyInfo;
2002 int idx;
2003 CollSeq *pColl; /* Collating sequence to use on this term */
2004 int bRev; /* True for DESCENDING sort order */
2005
drh953f7612012-12-07 22:18:54 +00002006 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002007 n = pOp->p3;
2008 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002009 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002010 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002011 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002012 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002013#if SQLITE_DEBUG
2014 if( aPermute ){
2015 int k, mx = 0;
2016 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002017 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2018 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002019 }else{
dan3bc9f742013-08-15 16:18:39 +00002020 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2021 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002022 }
2023#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002024 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002025 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002026 assert( memIsValid(&aMem[p1+idx]) );
2027 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002028 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2029 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002030 assert( i<pKeyInfo->nField );
2031 pColl = pKeyInfo->aColl[i];
2032 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002033 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002034 if( iCompare ){
2035 if( bRev ) iCompare = -iCompare;
2036 break;
2037 }
drh16ee60f2008-06-20 18:13:25 +00002038 }
drh0acb7e42008-06-25 00:12:41 +00002039 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002040 break;
2041}
2042
2043/* Opcode: Jump P1 P2 P3 * *
2044**
2045** Jump to the instruction at address P1, P2, or P3 depending on whether
2046** in the most recent OP_Compare instruction the P1 vector was less than
2047** equal to, or greater than the P2 vector, respectively.
2048*/
drh0acb7e42008-06-25 00:12:41 +00002049case OP_Jump: { /* jump */
2050 if( iCompare<0 ){
drh688852a2014-02-17 22:40:43 +00002051 pc = pOp->p1 - 1; VdbeBranchTaken(0,3);
drh0acb7e42008-06-25 00:12:41 +00002052 }else if( iCompare==0 ){
drh688852a2014-02-17 22:40:43 +00002053 pc = pOp->p2 - 1; VdbeBranchTaken(1,3);
drh16ee60f2008-06-20 18:13:25 +00002054 }else{
drh688852a2014-02-17 22:40:43 +00002055 pc = pOp->p3 - 1; VdbeBranchTaken(2,3);
drh16ee60f2008-06-20 18:13:25 +00002056 }
2057 break;
2058}
2059
drh5b6afba2008-01-05 16:29:28 +00002060/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002061** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002062**
drh5b6afba2008-01-05 16:29:28 +00002063** Take the logical AND of the values in registers P1 and P2 and
2064** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002065**
drh5b6afba2008-01-05 16:29:28 +00002066** If either P1 or P2 is 0 (false) then the result is 0 even if
2067** the other input is NULL. A NULL and true or two NULLs give
2068** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002069*/
drh5b6afba2008-01-05 16:29:28 +00002070/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002071** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002072**
2073** Take the logical OR of the values in register P1 and P2 and
2074** store the answer in register P3.
2075**
2076** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2077** even if the other input is NULL. A NULL and false or two NULLs
2078** give a NULL output.
2079*/
2080case OP_And: /* same as TK_AND, in1, in2, out3 */
2081case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002082 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2083 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002084
drh3c657212009-11-17 23:59:58 +00002085 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002086 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002087 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002088 }else{
drh5b6afba2008-01-05 16:29:28 +00002089 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002090 }
drh3c657212009-11-17 23:59:58 +00002091 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002092 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002093 v2 = 2;
2094 }else{
drh5b6afba2008-01-05 16:29:28 +00002095 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002096 }
2097 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002098 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002099 v1 = and_logic[v1*3+v2];
2100 }else{
drh5b6afba2008-01-05 16:29:28 +00002101 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002102 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002103 }
drh3c657212009-11-17 23:59:58 +00002104 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002105 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002106 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002107 }else{
drh5b6afba2008-01-05 16:29:28 +00002108 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002109 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002110 }
drh5e00f6c2001-09-13 13:46:56 +00002111 break;
2112}
2113
drhe99fa2a2008-12-15 15:27:51 +00002114/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002115** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002116**
drhe99fa2a2008-12-15 15:27:51 +00002117** Interpret the value in register P1 as a boolean value. Store the
2118** boolean complement in register P2. If the value in register P1 is
2119** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002120*/
drh93952eb2009-11-13 19:43:43 +00002121case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002122 pIn1 = &aMem[pOp->p1];
2123 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002124 sqlite3VdbeMemSetNull(pOut);
2125 if( (pIn1->flags & MEM_Null)==0 ){
2126 pOut->flags = MEM_Int;
2127 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002128 }
drh5e00f6c2001-09-13 13:46:56 +00002129 break;
2130}
2131
drhe99fa2a2008-12-15 15:27:51 +00002132/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002133** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002134**
drhe99fa2a2008-12-15 15:27:51 +00002135** Interpret the content of register P1 as an integer. Store the
2136** ones-complement of the P1 value into register P2. If P1 holds
2137** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002138*/
drh93952eb2009-11-13 19:43:43 +00002139case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002140 pIn1 = &aMem[pOp->p1];
2141 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002142 sqlite3VdbeMemSetNull(pOut);
2143 if( (pIn1->flags & MEM_Null)==0 ){
2144 pOut->flags = MEM_Int;
2145 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002146 }
drhbf4133c2001-10-13 02:59:08 +00002147 break;
2148}
2149
drh48f2d3b2011-09-16 01:34:43 +00002150/* Opcode: Once P1 P2 * * *
2151**
drh5dad9a32014-07-25 18:37:42 +00002152** Check the "once" flag number P1. If it is set, jump to instruction P2.
2153** Otherwise, set the flag and fall through to the next instruction.
2154** In other words, this opcode causes all following opcodes up through P2
2155** (but not including P2) to run just once and to be skipped on subsequent
2156** times through the loop.
2157**
2158** All "once" flags are initially cleared whenever a prepared statement
2159** first begins to run.
drh48f2d3b2011-09-16 01:34:43 +00002160*/
dan1d8cb212011-12-09 13:24:16 +00002161case OP_Once: { /* jump */
2162 assert( pOp->p1<p->nOnceFlag );
drh688852a2014-02-17 22:40:43 +00002163 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
dan1d8cb212011-12-09 13:24:16 +00002164 if( p->aOnceFlag[pOp->p1] ){
2165 pc = pOp->p2-1;
2166 }else{
2167 p->aOnceFlag[pOp->p1] = 1;
2168 }
2169 break;
2170}
2171
drh3c84ddf2008-01-09 02:15:38 +00002172/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002173**
drhef8662b2011-06-20 21:47:58 +00002174** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002175** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002176** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002177*/
drh3c84ddf2008-01-09 02:15:38 +00002178/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002179**
drhef8662b2011-06-20 21:47:58 +00002180** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002181** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002182** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002183*/
drh9cbf3422008-01-17 16:22:13 +00002184case OP_If: /* jump, in1 */
2185case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002186 int c;
drh3c657212009-11-17 23:59:58 +00002187 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002188 if( pIn1->flags & MEM_Null ){
2189 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002190 }else{
drhba0232a2005-06-06 17:27:19 +00002191#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002192 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002193#else
drh3c84ddf2008-01-09 02:15:38 +00002194 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002195#endif
drhf5905aa2002-05-26 20:54:33 +00002196 if( pOp->opcode==OP_IfNot ) c = !c;
2197 }
drh688852a2014-02-17 22:40:43 +00002198 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002199 if( c ){
2200 pc = pOp->p2-1;
2201 }
drh5e00f6c2001-09-13 13:46:56 +00002202 break;
2203}
2204
drh830ecf92009-06-18 00:41:55 +00002205/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002206** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002207**
drh830ecf92009-06-18 00:41:55 +00002208** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002209*/
drh9cbf3422008-01-17 16:22:13 +00002210case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002211 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002212 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002213 if( (pIn1->flags & MEM_Null)!=0 ){
2214 pc = pOp->p2 - 1;
2215 }
drh477df4b2008-01-05 18:48:24 +00002216 break;
2217}
2218
drh98757152008-01-09 23:04:12 +00002219/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002220** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002221**
drh6a288a32008-01-07 19:20:24 +00002222** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002223*/
drh9cbf3422008-01-17 16:22:13 +00002224case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002225 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002226 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002227 if( (pIn1->flags & MEM_Null)==0 ){
2228 pc = pOp->p2 - 1;
2229 }
drh5e00f6c2001-09-13 13:46:56 +00002230 break;
2231}
2232
drh3e9ca092009-09-08 01:14:48 +00002233/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002234** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002235**
danielk1977cfcdaef2004-05-12 07:33:33 +00002236** Interpret the data that cursor P1 points to as a structure built using
2237** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002238** information about the format of the data.) Extract the P2-th column
2239** from this record. If there are less that (P2+1)
2240** values in the record, extract a NULL.
2241**
drh9cbf3422008-01-17 16:22:13 +00002242** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002243**
danielk19771f4aa332008-01-03 09:51:55 +00002244** If the column contains fewer than P2 fields, then extract a NULL. Or,
2245** if the P4 argument is a P4_MEM use the value of the P4 argument as
2246** the result.
drh3e9ca092009-09-08 01:14:48 +00002247**
2248** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2249** then the cache of the cursor is reset prior to extracting the column.
2250** The first OP_Column against a pseudo-table after the value of the content
2251** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002252**
drhdda5c082012-03-28 13:41:10 +00002253** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2254** the result is guaranteed to only be used as the argument of a length()
2255** or typeof() function, respectively. The loading of large blobs can be
2256** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002257*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002258case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002259 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002260 int p2; /* column number to retrieve */
2261 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002262 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002263 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002264 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002265 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002266 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002267 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002268 const u8 *zData; /* Part of the record being decoded */
2269 const u8 *zHdr; /* Next unparsed byte of the header */
2270 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002271 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002272 u32 szField; /* Number of bytes in the content of a field */
drh501932c2013-11-21 21:59:53 +00002273 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002274 u32 t; /* A type code from the record header */
drh0c8f7602014-09-19 16:56:45 +00002275 u16 fx; /* pDest->flags value */
drh3e9ca092009-09-08 01:14:48 +00002276 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002277
drh399af1d2013-11-20 17:25:55 +00002278 p2 = pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00002279 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002280 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002281 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002282 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2283 pC = p->apCsr[pOp->p1];
drha5759672012-10-30 14:39:12 +00002284 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002285 assert( p2<pC->nField );
drh0c8f7602014-09-19 16:56:45 +00002286 aOffset = pC->aType + pC->nField;
danielk19770817d0d2007-02-14 09:19:36 +00002287#ifndef SQLITE_OMIT_VIRTUALTABLE
drh380d6852013-11-20 20:58:00 +00002288 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
danielk19770817d0d2007-02-14 09:19:36 +00002289#endif
shane36840fd2009-06-26 16:32:13 +00002290 pCrsr = pC->pCursor;
drh380d6852013-11-20 20:58:00 +00002291 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
2292 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */
drh399af1d2013-11-20 17:25:55 +00002293
2294 /* If the cursor cache is stale, bring it up-to-date */
2295 rc = sqlite3VdbeCursorMoveto(pC);
2296 if( rc ) goto abort_due_to_error;
2297 if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
drhc8606e42013-11-20 19:28:03 +00002298 if( pC->nullRow ){
2299 if( pCrsr==0 ){
2300 assert( pC->pseudoTableReg>0 );
2301 pReg = &aMem[pC->pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002302 assert( pReg->flags & MEM_Blob );
2303 assert( memIsValid(pReg) );
2304 pC->payloadSize = pC->szRow = avail = pReg->n;
2305 pC->aRow = (u8*)pReg->z;
2306 }else{
2307 MemSetTypeFlag(pDest, MEM_Null);
drh399af1d2013-11-20 17:25:55 +00002308 goto op_column_out;
2309 }
danielk197784ac9d02004-05-18 09:58:06 +00002310 }else{
drhc8606e42013-11-20 19:28:03 +00002311 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002312 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002313 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2314 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2315 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2316 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2317 ** payload size, so it is impossible for payloadSize64 to be
2318 ** larger than 32 bits. */
2319 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2320 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2321 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002322 }else{
drh399af1d2013-11-20 17:25:55 +00002323 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2324 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2325 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2326 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002327 }
drh399af1d2013-11-20 17:25:55 +00002328 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2329 if( pC->payloadSize <= (u32)avail ){
2330 pC->szRow = pC->payloadSize;
drhe61cffc2004-06-12 18:12:15 +00002331 }else{
drh399af1d2013-11-20 17:25:55 +00002332 pC->szRow = avail;
2333 }
2334 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2335 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002336 }
drhd3194f52004-05-27 19:59:32 +00002337 }
drh399af1d2013-11-20 17:25:55 +00002338 pC->cacheStatus = p->cacheCtr;
2339 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2340 pC->nHdrParsed = 0;
2341 aOffset[0] = offset;
2342 if( avail<offset ){
drh380d6852013-11-20 20:58:00 +00002343 /* pC->aRow does not have to hold the entire row, but it does at least
2344 ** need to cover the header of the record. If pC->aRow does not contain
2345 ** the complete header, then set it to zero, forcing the header to be
2346 ** dynamically allocated. */
drh399af1d2013-11-20 17:25:55 +00002347 pC->aRow = 0;
2348 pC->szRow = 0;
2349 }
drh35cd6432009-06-05 14:17:21 +00002350
2351 /* Make sure a corrupt database has not given us an oversize header.
2352 ** Do this now to avoid an oversize memory allocation.
2353 **
2354 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2355 ** types use so much data space that there can only be 4096 and 32 of
2356 ** them, respectively. So the maximum header length results from a
2357 ** 3-byte type for each of the maximum of 32768 columns plus three
2358 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2359 */
drh399af1d2013-11-20 17:25:55 +00002360 if( offset > 98307 || offset > pC->payloadSize ){
drh35cd6432009-06-05 14:17:21 +00002361 rc = SQLITE_CORRUPT_BKPT;
drhc8606e42013-11-20 19:28:03 +00002362 goto op_column_error;
drh35cd6432009-06-05 14:17:21 +00002363 }
drh399af1d2013-11-20 17:25:55 +00002364 }
drh35cd6432009-06-05 14:17:21 +00002365
drh399af1d2013-11-20 17:25:55 +00002366 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002367 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002368 */
drhc8606e42013-11-20 19:28:03 +00002369 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002370 /* If there is more header available for parsing in the record, try
2371 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002372 */
drhc8606e42013-11-20 19:28:03 +00002373 if( pC->iHdrOffset<aOffset[0] ){
2374 /* Make sure zData points to enough of the record to cover the header. */
2375 if( pC->aRow==0 ){
2376 memset(&sMem, 0, sizeof(sMem));
drh14da87f2013-11-20 21:51:33 +00002377 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
2378 !pC->isTable, &sMem);
drhc8606e42013-11-20 19:28:03 +00002379 if( rc!=SQLITE_OK ){
2380 goto op_column_error;
2381 }
2382 zData = (u8*)sMem.z;
2383 }else{
2384 zData = pC->aRow;
2385 }
2386
drh0c8f7602014-09-19 16:56:45 +00002387 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drhc8606e42013-11-20 19:28:03 +00002388 i = pC->nHdrParsed;
2389 offset = aOffset[i];
2390 zHdr = zData + pC->iHdrOffset;
2391 zEndHdr = zData + aOffset[0];
2392 assert( i<=p2 && zHdr<zEndHdr );
2393 do{
2394 if( zHdr[0]<0x80 ){
2395 t = zHdr[0];
2396 zHdr++;
2397 }else{
2398 zHdr += sqlite3GetVarint32(zHdr, &t);
2399 }
drh0c8f7602014-09-19 16:56:45 +00002400 pC->aType[i] = t;
drhc8606e42013-11-20 19:28:03 +00002401 szField = sqlite3VdbeSerialTypeLen(t);
2402 offset += szField;
2403 if( offset<szField ){ /* True if offset overflows */
2404 zHdr = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2405 break;
2406 }
2407 i++;
2408 aOffset[i] = offset;
2409 }while( i<=p2 && zHdr<zEndHdr );
2410 pC->nHdrParsed = i;
2411 pC->iHdrOffset = (u32)(zHdr - zData);
2412 if( pC->aRow==0 ){
2413 sqlite3VdbeMemRelease(&sMem);
2414 sMem.flags = MEM_Null;
2415 }
2416
2417 /* If we have read more header data than was contained in the header,
2418 ** or if the end of the last field appears to be past the end of the
2419 ** record, or if the end of the last field appears to be before the end
2420 ** of the record (when all fields present), then we must be dealing
2421 ** with a corrupt database.
2422 */
2423 if( (zHdr > zEndHdr)
2424 || (offset > pC->payloadSize)
2425 || (zHdr==zEndHdr && offset!=pC->payloadSize)
2426 ){
2427 rc = SQLITE_CORRUPT_BKPT;
2428 goto op_column_error;
2429 }
2430 }
2431
drh380d6852013-11-20 20:58:00 +00002432 /* If after trying to extra new entries from the header, nHdrParsed is
2433 ** still not up to p2, that means that the record has fewer than p2
2434 ** columns. So the result will be either the default value or a NULL.
2435 */
drhc8606e42013-11-20 19:28:03 +00002436 if( pC->nHdrParsed<=p2 ){
2437 if( pOp->p4type==P4_MEM ){
2438 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2439 }else{
2440 MemSetTypeFlag(pDest, MEM_Null);
2441 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002442 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002443 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002444 }
danielk1977192ac1d2004-05-10 07:17:30 +00002445
drh380d6852013-11-20 20:58:00 +00002446 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002447 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002448 ** all valid.
drh9188b382004-05-14 21:12:22 +00002449 */
drhc8606e42013-11-20 19:28:03 +00002450 assert( p2<pC->nHdrParsed );
2451 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002452 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drh0725cab2014-09-17 14:52:46 +00002453 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
drh0c8f7602014-09-19 16:56:45 +00002454 t = pC->aType[p2];
drhc8606e42013-11-20 19:28:03 +00002455 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002456 /* This is the common case where the desired content fits on the original
2457 ** page - where the content is not on an overflow page */
drh0c8f7602014-09-19 16:56:45 +00002458 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
danielk197736963fd2005-02-19 08:18:05 +00002459 }else{
drh58c96082013-12-23 11:33:32 +00002460 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002461 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2462 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2463 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002464 ){
drh2a2a6962014-09-16 18:22:44 +00002465 /* Content is irrelevant for
2466 ** 1. the typeof() function,
2467 ** 2. the length(X) function if X is a blob, and
2468 ** 3. if the content length is zero.
2469 ** So we might as well use bogus content rather than reading
2470 ** content from disk. NULL will work for the value for strings
2471 ** and blobs and whatever is in the payloadSize64 variable
2472 ** will work for everything else. */
2473 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002474 }else{
drh14da87f2013-11-20 21:51:33 +00002475 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drh2a2a6962014-09-16 18:22:44 +00002476 pDest);
drhc8606e42013-11-20 19:28:03 +00002477 if( rc!=SQLITE_OK ){
2478 goto op_column_error;
2479 }
drh2a2a6962014-09-16 18:22:44 +00002480 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2481 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002482 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002483 }
drhc8606e42013-11-20 19:28:03 +00002484 pDest->enc = encoding;
drhd3194f52004-05-27 19:59:32 +00002485
danielk19773c9cc8d2005-01-17 03:40:08 +00002486op_column_out:
drh7b5ebca2014-09-19 15:28:33 +00002487 /* If the column value is an ephemeral string, go ahead and persist
2488 ** that string in case the cursor moves before the column value is
2489 ** used. The following code does the equivalent of Deephemeralize()
2490 ** but does it faster. */
2491 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
drh0c8f7602014-09-19 16:56:45 +00002492 fx = pDest->flags & (MEM_Str|MEM_Blob);
2493 assert( fx!=0 );
drh7b5ebca2014-09-19 15:28:33 +00002494 zData = (const u8*)pDest->z;
2495 len = pDest->n;
2496 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
2497 memcpy(pDest->z, zData, len);
2498 pDest->z[len] = 0;
2499 pDest->z[len+1] = 0;
drh0c8f7602014-09-19 16:56:45 +00002500 pDest->flags = fx|MEM_Term;
drh7b5ebca2014-09-19 15:28:33 +00002501 }
drhc8606e42013-11-20 19:28:03 +00002502op_column_error:
drhb7654112008-01-12 12:48:07 +00002503 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002504 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002505 break;
2506}
2507
danielk1977751de562008-04-18 09:01:15 +00002508/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002509** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002510**
2511** Apply affinities to a range of P2 registers starting with P1.
2512**
2513** P4 is a string that is P2 characters long. The nth character of the
2514** string indicates the column affinity that should be used for the nth
2515** memory cell in the range.
2516*/
2517case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002518 const char *zAffinity; /* The affinity to be applied */
2519 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002520
drh856c1032009-06-02 15:21:42 +00002521 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002522 assert( zAffinity!=0 );
2523 assert( zAffinity[pOp->p2]==0 );
2524 pIn1 = &aMem[pOp->p1];
2525 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002526 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002527 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002528 applyAffinity(pIn1, cAff, encoding);
2529 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002530 }
2531 break;
2532}
2533
drh1db639c2008-01-17 02:36:28 +00002534/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002535** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002536**
drh710c4842010-08-30 01:17:20 +00002537** Convert P2 registers beginning with P1 into the [record format]
2538** use as a data record in a database table or as a key
2539** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002540**
danielk1977751de562008-04-18 09:01:15 +00002541** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002542** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002543** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002544**
drh8a512562005-11-14 22:29:05 +00002545** The mapping from character to affinity is given by the SQLITE_AFF_
2546** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002547**
drh66a51672008-01-03 00:01:23 +00002548** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002549*/
drh1db639c2008-01-17 02:36:28 +00002550case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002551 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2552 Mem *pRec; /* The new record */
2553 u64 nData; /* Number of bytes of data space */
2554 int nHdr; /* Number of bytes of header space */
2555 i64 nByte; /* Data space required for this record */
2556 int nZero; /* Number of zero bytes at the end of the record */
2557 int nVarint; /* Number of bytes in a varint */
2558 u32 serial_type; /* Type field */
2559 Mem *pData0; /* First field to be combined into the record */
2560 Mem *pLast; /* Last field of the record */
2561 int nField; /* Number of fields in the record */
2562 char *zAffinity; /* The affinity string for the record */
2563 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002564 int i; /* Space used in zNewRecord[] header */
2565 int j; /* Space used in zNewRecord[] content */
drh856c1032009-06-02 15:21:42 +00002566 int len; /* Length of a field */
2567
drhf3218fe2004-05-28 08:21:02 +00002568 /* Assuming the record contains N fields, the record format looks
2569 ** like this:
2570 **
drh7a224de2004-06-02 01:22:02 +00002571 ** ------------------------------------------------------------------------
2572 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2573 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002574 **
drh9cbf3422008-01-17 16:22:13 +00002575 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002576 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002577 **
2578 ** Each type field is a varint representing the serial type of the
2579 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002580 ** hdr-size field is also a varint which is the offset from the beginning
2581 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002582 */
drh856c1032009-06-02 15:21:42 +00002583 nData = 0; /* Number of bytes of data space */
2584 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002585 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002586 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002587 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002588 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002589 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002590 nField = pOp->p2;
2591 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002592 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002593
drh2b4ded92010-09-27 21:09:31 +00002594 /* Identify the output register */
2595 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2596 pOut = &aMem[pOp->p3];
2597 memAboutToChange(p, pOut);
2598
drh3e6c0602013-12-10 20:53:01 +00002599 /* Apply the requested affinity to all inputs
2600 */
2601 assert( pData0<=pLast );
2602 if( zAffinity ){
2603 pRec = pData0;
2604 do{
drh57bf4a82014-02-17 14:59:22 +00002605 applyAffinity(pRec++, *(zAffinity++), encoding);
2606 assert( zAffinity[0]==0 || pRec<=pLast );
2607 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002608 }
2609
drhf3218fe2004-05-28 08:21:02 +00002610 /* Loop through the elements that will make up the record to figure
2611 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002612 */
drh038b7bc2013-12-09 23:17:22 +00002613 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002614 do{
drh2b4ded92010-09-27 21:09:31 +00002615 assert( memIsValid(pRec) );
drhd946db02005-12-29 19:23:06 +00002616 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002617 len = sqlite3VdbeSerialTypeLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002618 if( pRec->flags & MEM_Zero ){
2619 if( nData ){
2620 sqlite3VdbeMemExpandBlob(pRec);
2621 }else{
2622 nZero += pRec->u.nZero;
2623 len -= pRec->u.nZero;
2624 }
2625 }
drhae7e1512007-05-02 16:51:59 +00002626 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002627 testcase( serial_type==127 );
2628 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002629 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002630 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002631
drhf3218fe2004-05-28 08:21:02 +00002632 /* Add the initial header varint and total the size */
drh59bf00c2013-12-08 23:33:28 +00002633 testcase( nHdr==126 );
2634 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002635 if( nHdr<=126 ){
2636 /* The common case */
2637 nHdr += 1;
2638 }else{
2639 /* Rare case of a really large header */
2640 nVarint = sqlite3VarintLen(nHdr);
2641 nHdr += nVarint;
2642 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002643 }
drh038b7bc2013-12-09 23:17:22 +00002644 nByte = nHdr+nData;
drhbb4957f2008-03-20 14:03:29 +00002645 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002646 goto too_big;
2647 }
drhf3218fe2004-05-28 08:21:02 +00002648
danielk1977a7a8e142008-02-13 18:25:27 +00002649 /* Make sure the output register has a buffer large enough to store
2650 ** the new record. The output register (pOp->p3) is not allowed to
2651 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002652 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002653 */
drh322f2852014-09-19 00:43:39 +00002654 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002655 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002656 }
danielk1977a7a8e142008-02-13 18:25:27 +00002657 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002658
2659 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002660 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002661 j = nHdr;
2662 assert( pData0<=pLast );
2663 pRec = pData0;
2664 do{
drhd946db02005-12-29 19:23:06 +00002665 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drh038b7bc2013-12-09 23:17:22 +00002666 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drha9ab4812013-12-11 11:00:44 +00002667 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002668 }while( (++pRec)<=pLast );
2669 assert( i==nHdr );
2670 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002671
dan3bc9f742013-08-15 16:18:39 +00002672 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002673 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002674 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002675 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002676 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002677 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002678 }
drh477df4b2008-01-05 18:48:24 +00002679 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002680 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002681 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002682 break;
2683}
2684
danielk1977a5533162009-02-24 10:01:51 +00002685/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002686** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002687**
2688** Store the number of entries (an integer value) in the table or index
2689** opened by cursor P1 in register P2
2690*/
2691#ifndef SQLITE_OMIT_BTREECOUNT
2692case OP_Count: { /* out2-prerelease */
2693 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002694 BtCursor *pCrsr;
2695
2696 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh3da046d2013-11-11 03:24:11 +00002697 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002698 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002699 rc = sqlite3BtreeCount(pCrsr, &nEntry);
danielk1977a5533162009-02-24 10:01:51 +00002700 pOut->u.i = nEntry;
2701 break;
2702}
2703#endif
2704
danielk1977fd7f0452008-12-17 17:30:26 +00002705/* Opcode: Savepoint P1 * * P4 *
2706**
2707** Open, release or rollback the savepoint named by parameter P4, depending
2708** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2709** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2710*/
2711case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002712 int p1; /* Value of P1 operand */
2713 char *zName; /* Name of savepoint */
2714 int nName;
2715 Savepoint *pNew;
2716 Savepoint *pSavepoint;
2717 Savepoint *pTmp;
2718 int iSavepoint;
2719 int ii;
2720
2721 p1 = pOp->p1;
2722 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002723
2724 /* Assert that the p1 parameter is valid. Also that if there is no open
2725 ** transaction, then there cannot be any savepoints.
2726 */
2727 assert( db->pSavepoint==0 || db->autoCommit==0 );
2728 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2729 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2730 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002731 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002732
2733 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002734 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002735 /* A new savepoint cannot be created if there are active write
2736 ** statements (i.e. open read/write incremental blob handles).
2737 */
2738 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2739 "SQL statements in progress");
2740 rc = SQLITE_BUSY;
2741 }else{
drh856c1032009-06-02 15:21:42 +00002742 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002743
drhbe07ec52011-06-03 12:15:26 +00002744#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002745 /* This call is Ok even if this savepoint is actually a transaction
2746 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2747 ** If this is a transaction savepoint being opened, it is guaranteed
2748 ** that the db->aVTrans[] array is empty. */
2749 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002750 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2751 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002752 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002753#endif
dand9495cd2011-04-27 12:08:04 +00002754
danielk1977fd7f0452008-12-17 17:30:26 +00002755 /* Create a new savepoint structure. */
2756 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2757 if( pNew ){
2758 pNew->zName = (char *)&pNew[1];
2759 memcpy(pNew->zName, zName, nName+1);
2760
2761 /* If there is no open transaction, then mark this as a special
2762 ** "transaction savepoint". */
2763 if( db->autoCommit ){
2764 db->autoCommit = 0;
2765 db->isTransactionSavepoint = 1;
2766 }else{
2767 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002768 }
danielk1977fd7f0452008-12-17 17:30:26 +00002769
2770 /* Link the new savepoint into the database handle's list. */
2771 pNew->pNext = db->pSavepoint;
2772 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002773 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002774 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002775 }
2776 }
2777 }else{
drh856c1032009-06-02 15:21:42 +00002778 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002779
2780 /* Find the named savepoint. If there is no such savepoint, then an
2781 ** an error is returned to the user. */
2782 for(
drh856c1032009-06-02 15:21:42 +00002783 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002784 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002785 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002786 ){
2787 iSavepoint++;
2788 }
2789 if( !pSavepoint ){
2790 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2791 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002792 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002793 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002794 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002795 */
2796 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002797 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002798 );
2799 rc = SQLITE_BUSY;
2800 }else{
2801
2802 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002803 ** and this is a RELEASE command, then the current transaction
2804 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002805 */
2806 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2807 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002808 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002809 goto vdbe_return;
2810 }
danielk1977fd7f0452008-12-17 17:30:26 +00002811 db->autoCommit = 1;
2812 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2813 p->pc = pc;
2814 db->autoCommit = 0;
2815 p->rc = rc = SQLITE_BUSY;
2816 goto vdbe_return;
2817 }
danielk197734cf35d2008-12-18 18:31:38 +00002818 db->isTransactionSavepoint = 0;
2819 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002820 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002821 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002822 if( p1==SAVEPOINT_ROLLBACK ){
2823 for(ii=0; ii<db->nDb; ii++){
2824 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2825 }
drh0f198a72012-02-13 16:43:16 +00002826 }
2827 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002828 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2829 if( rc!=SQLITE_OK ){
2830 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002831 }
danielk1977fd7f0452008-12-17 17:30:26 +00002832 }
drh9f0bbf92009-01-02 21:08:09 +00002833 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002834 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002835 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002836 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002837 }
2838 }
2839
2840 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2841 ** savepoints nested inside of the savepoint being operated on. */
2842 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002843 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002844 db->pSavepoint = pTmp->pNext;
2845 sqlite3DbFree(db, pTmp);
2846 db->nSavepoint--;
2847 }
2848
dan1da40a32009-09-19 17:00:31 +00002849 /* If it is a RELEASE, then destroy the savepoint being operated on
2850 ** too. If it is a ROLLBACK TO, then set the number of deferred
2851 ** constraint violations present in the database to the value stored
2852 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002853 if( p1==SAVEPOINT_RELEASE ){
2854 assert( pSavepoint==db->pSavepoint );
2855 db->pSavepoint = pSavepoint->pNext;
2856 sqlite3DbFree(db, pSavepoint);
2857 if( !isTransaction ){
2858 db->nSavepoint--;
2859 }
dan1da40a32009-09-19 17:00:31 +00002860 }else{
2861 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002862 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002863 }
dand9495cd2011-04-27 12:08:04 +00002864
2865 if( !isTransaction ){
2866 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2867 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2868 }
danielk1977fd7f0452008-12-17 17:30:26 +00002869 }
2870 }
2871
2872 break;
2873}
2874
drh98757152008-01-09 23:04:12 +00002875/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002876**
2877** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002878** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002879** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2880** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002881**
2882** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002883*/
drh9cbf3422008-01-17 16:22:13 +00002884case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002885 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002886 int iRollback;
drh856c1032009-06-02 15:21:42 +00002887 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002888
drh856c1032009-06-02 15:21:42 +00002889 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002890 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002891 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002892 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002893 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002894 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002895 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002896
drh0f198a72012-02-13 16:43:16 +00002897#if 0
drh4f7d3a52013-06-27 23:54:02 +00002898 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002899 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002900 ** still running, and a transaction is active, return an error indicating
2901 ** that the other VMs must complete first.
2902 */
drhad4a4b82008-11-05 16:37:34 +00002903 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2904 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002905 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002906 }else
2907#endif
drh4f7d3a52013-06-27 23:54:02 +00002908 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002909 /* If this instruction implements a COMMIT and other VMs are writing
2910 ** return an error indicating that the other VMs must complete first.
2911 */
2912 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2913 "SQL statements in progress");
2914 rc = SQLITE_BUSY;
2915 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002916 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002917 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002918 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002919 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002920 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002921 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002922 }else{
shane7d3846a2008-12-11 02:58:26 +00002923 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002924 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002925 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002926 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002927 p->rc = rc = SQLITE_BUSY;
2928 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002929 }
danielk19771d850a72004-05-31 08:26:49 +00002930 }
danielk1977bd434552009-03-18 10:33:00 +00002931 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002932 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002933 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002934 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002935 }else{
drh900b31e2007-08-28 02:27:51 +00002936 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002937 }
drh900b31e2007-08-28 02:27:51 +00002938 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002939 }else{
drhf089aa42008-07-08 19:34:06 +00002940 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002941 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002942 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002943 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002944
2945 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002946 }
2947 break;
2948}
2949
drhb22f7c82014-02-06 23:56:27 +00002950/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002951**
drh05a86c52014-02-16 01:55:49 +00002952** Begin a transaction on database P1 if a transaction is not already
2953** active.
2954** If P2 is non-zero, then a write-transaction is started, or if a
2955** read-transaction is already active, it is upgraded to a write-transaction.
2956** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00002957**
drh001bbcb2003-03-19 03:14:00 +00002958** P1 is the index of the database file on which the transaction is
2959** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002960** file used for temporary tables. Indices of 2 or more are used for
2961** attached databases.
drhcabb0812002-09-14 13:47:32 +00002962**
dane0af83a2009-09-08 19:15:01 +00002963** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2964** true (this flag is set if the Vdbe may modify more than one row and may
2965** throw an ABORT exception), a statement transaction may also be opened.
2966** More specifically, a statement transaction is opened iff the database
2967** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002968** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002969** VDBE to be rolled back after an error without having to roll back the
2970** entire transaction. If no error is encountered, the statement transaction
2971** will automatically commit when the VDBE halts.
2972**
drhb22f7c82014-02-06 23:56:27 +00002973** If P5!=0 then this opcode also checks the schema cookie against P3
2974** and the schema generation counter against P4.
2975** The cookie changes its value whenever the database schema changes.
2976** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00002977** and that the current process needs to reread the schema. If the schema
2978** cookie in P3 differs from the schema cookie in the database header or
2979** if the schema generation counter in P4 differs from the current
2980** generation counter, then an SQLITE_SCHEMA error is raised and execution
2981** halts. The sqlite3_step() wrapper function might then reprepare the
2982** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00002983*/
drh9cbf3422008-01-17 16:22:13 +00002984case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002985 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00002986 int iMeta;
2987 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00002988
drh1713afb2013-06-28 01:24:57 +00002989 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00002990 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00002991 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00002992 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00002993 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
2994 rc = SQLITE_READONLY;
2995 goto abort_due_to_error;
2996 }
drh653b82a2009-06-22 11:10:47 +00002997 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002998
danielk197724162fe2004-06-04 06:22:00 +00002999 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003000 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00003001 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00003002 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00003003 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00003004 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003005 }
drh9e9f1bd2009-10-13 15:36:51 +00003006 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003007 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003008 }
dane0af83a2009-09-08 19:15:01 +00003009
3010 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003011 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003012 ){
3013 assert( sqlite3BtreeIsInTrans(pBt) );
3014 if( p->iStatement==0 ){
3015 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3016 db->nStatement++;
3017 p->iStatement = db->nSavepoint + db->nStatement;
3018 }
dana311b802011-04-26 19:21:34 +00003019
drh346506f2011-05-25 01:16:42 +00003020 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003021 if( rc==SQLITE_OK ){
3022 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3023 }
dan1da40a32009-09-19 17:00:31 +00003024
3025 /* Store the current value of the database handles deferred constraint
3026 ** counter. If the statement transaction needs to be rolled back,
3027 ** the value of this counter needs to be restored too. */
3028 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003029 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003030 }
drhb22f7c82014-02-06 23:56:27 +00003031
3032 /* Gather the schema version number for checking */
3033 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3034 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3035 }else{
3036 iGen = iMeta = 0;
3037 }
3038 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3039 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3040 sqlite3DbFree(db, p->zErrMsg);
3041 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3042 /* If the schema-cookie from the database file matches the cookie
3043 ** stored with the in-memory representation of the schema, do
3044 ** not reload the schema from the database file.
3045 **
3046 ** If virtual-tables are in use, this is not just an optimization.
3047 ** Often, v-tables store their data in other SQLite tables, which
3048 ** are queried from within xNext() and other v-table methods using
3049 ** prepared queries. If such a query is out-of-date, we do not want to
3050 ** discard the database schema, as the user code implementing the
3051 ** v-table would have to be ready for the sqlite3_vtab structure itself
3052 ** to be invalidated whenever sqlite3_step() is called from within
3053 ** a v-table method.
3054 */
3055 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3056 sqlite3ResetOneSchema(db, pOp->p1);
3057 }
3058 p->expired = 1;
3059 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003060 }
drh5e00f6c2001-09-13 13:46:56 +00003061 break;
3062}
3063
drhb1fdb2a2008-01-05 04:06:03 +00003064/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003065**
drh9cbf3422008-01-17 16:22:13 +00003066** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003067** P3==1 is the schema version. P3==2 is the database format.
3068** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003069** the main database file and P1==1 is the database file used to store
3070** temporary tables.
drh4a324312001-12-21 14:30:42 +00003071**
drh50e5dad2001-09-15 00:57:28 +00003072** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003073** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003074** executing this instruction.
3075*/
drh4c583122008-01-04 22:01:03 +00003076case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003077 int iMeta;
drh856c1032009-06-02 15:21:42 +00003078 int iDb;
3079 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003080
drh1713afb2013-06-28 01:24:57 +00003081 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003082 iDb = pOp->p1;
3083 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003084 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003085 assert( iDb>=0 && iDb<db->nDb );
3086 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003087 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003088
danielk1977602b4662009-07-02 07:47:33 +00003089 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003090 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003091 break;
3092}
3093
drh98757152008-01-09 23:04:12 +00003094/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003095**
drh98757152008-01-09 23:04:12 +00003096** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003097** into cookie number P2 of database P1. P2==1 is the schema version.
3098** P2==2 is the database format. P2==3 is the recommended pager cache
3099** size, and so forth. P1==0 is the main database file and P1==1 is the
3100** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003101**
3102** A transaction must be started before executing this opcode.
3103*/
drh9cbf3422008-01-17 16:22:13 +00003104case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003105 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003106 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003107 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003108 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003109 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003110 pDb = &db->aDb[pOp->p1];
3111 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003112 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003113 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003114 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003115 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003116 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3117 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003118 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003119 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003120 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003121 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003122 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003123 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003124 }
drhfd426c62006-01-30 15:34:22 +00003125 if( pOp->p1==1 ){
3126 /* Invalidate all prepared statements whenever the TEMP database
3127 ** schema is changed. Ticket #1644 */
3128 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003129 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003130 }
drh50e5dad2001-09-15 00:57:28 +00003131 break;
3132}
3133
drh98757152008-01-09 23:04:12 +00003134/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003135** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003136**
drhecdc7532001-09-23 02:35:53 +00003137** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003138** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003139** P3==0 means the main database, P3==1 means the database used for
3140** temporary tables, and P3>1 means used the corresponding attached
3141** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003142** values need not be contiguous but all P1 values should be small integers.
3143** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003144**
drh98757152008-01-09 23:04:12 +00003145** If P5!=0 then use the content of register P2 as the root page, not
3146** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003147**
drhb19a2bc2001-09-16 00:13:26 +00003148** There will be a read lock on the database whenever there is an
3149** open cursor. If the database was unlocked prior to this instruction
3150** then a read lock is acquired as part of this instruction. A read
3151** lock allows other processes to read the database but prohibits
3152** any other process from modifying the database. The read lock is
3153** released when all cursors are closed. If this instruction attempts
3154** to get a read lock but fails, the script terminates with an
3155** SQLITE_BUSY error code.
3156**
danielk1977d336e222009-02-20 10:58:41 +00003157** The P4 value may be either an integer (P4_INT32) or a pointer to
3158** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3159** structure, then said structure defines the content and collating
3160** sequence of the index being opened. Otherwise, if P4 is an integer
3161** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003162**
drh35263192014-07-22 20:02:19 +00003163** See also: OpenWrite, ReopenIdx
3164*/
3165/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3166** Synopsis: root=P2 iDb=P3
3167**
3168** The ReopenIdx opcode works exactly like ReadOpen except that it first
3169** checks to see if the cursor on P1 is already open with a root page
3170** number of P2 and if it is this opcode becomes a no-op. In other words,
3171** if the cursor is already open, do not reopen it.
3172**
3173** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3174** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3175** every other ReopenIdx or OpenRead for the same cursor number.
3176**
3177** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003178*/
drh98757152008-01-09 23:04:12 +00003179/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003180** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003181**
3182** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003183** page is P2. Or if P5!=0 use the content of register P2 to find the
3184** root page.
drhecdc7532001-09-23 02:35:53 +00003185**
danielk1977d336e222009-02-20 10:58:41 +00003186** The P4 value may be either an integer (P4_INT32) or a pointer to
3187** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3188** structure, then said structure defines the content and collating
3189** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003190** value, it is set to the number of columns in the table, or to the
3191** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003192**
drh001bbcb2003-03-19 03:14:00 +00003193** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003194** in read/write mode. For a given table, there can be one or more read-only
3195** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003196**
drh001bbcb2003-03-19 03:14:00 +00003197** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003198*/
drh35263192014-07-22 20:02:19 +00003199case OP_ReopenIdx: {
3200 VdbeCursor *pCur;
3201
3202 assert( pOp->p5==0 );
3203 assert( pOp->p4type==P4_KEYINFO );
3204 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003205 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003206 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
3207 break;
3208 }
3209 /* If the cursor is not currently open or is open on a different
3210 ** index, then fall through into OP_OpenRead to force a reopen */
3211}
drh9cbf3422008-01-17 16:22:13 +00003212case OP_OpenRead:
3213case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003214 int nField;
3215 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003216 int p2;
3217 int iDb;
drhf57b3392001-10-08 13:22:32 +00003218 int wrFlag;
3219 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003220 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003221 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003222
dan428c2182012-08-06 18:50:11 +00003223 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3224 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003225 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003226 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3227 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003228
danfa401de2009-10-16 14:55:03 +00003229 if( p->expired ){
3230 rc = SQLITE_ABORT;
3231 break;
3232 }
3233
drh856c1032009-06-02 15:21:42 +00003234 nField = 0;
3235 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003236 p2 = pOp->p2;
3237 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003238 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003239 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003240 pDb = &db->aDb[iDb];
3241 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003242 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003243 if( pOp->opcode==OP_OpenWrite ){
3244 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003245 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003246 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3247 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003248 }
3249 }else{
3250 wrFlag = 0;
3251 }
dan428c2182012-08-06 18:50:11 +00003252 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003253 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003254 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003255 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003256 assert( memIsValid(pIn2) );
3257 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003258 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003259 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003260 /* The p2 value always comes from a prior OP_CreateTable opcode and
3261 ** that opcode will always set the p2 value to 2 or more or else fail.
3262 ** If there were a failure, the prepared statement would have halted
3263 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003264 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003265 rc = SQLITE_CORRUPT_BKPT;
3266 goto abort_due_to_error;
3267 }
drh5edc3122001-09-13 21:53:09 +00003268 }
danielk1977d336e222009-02-20 10:58:41 +00003269 if( pOp->p4type==P4_KEYINFO ){
3270 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003271 assert( pKeyInfo->enc==ENC(db) );
3272 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003273 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003274 }else if( pOp->p4type==P4_INT32 ){
3275 nField = pOp->p4.i;
3276 }
drh653b82a2009-06-22 11:10:47 +00003277 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003278 assert( nField>=0 );
3279 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drh653b82a2009-06-22 11:10:47 +00003280 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003281 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003282 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003283 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003284 pCur->pgnoRoot = p2;
danielk1977d336e222009-02-20 10:58:41 +00003285 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3286 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003287 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3288 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003289
dana205a482011-08-27 18:48:57 +00003290 /* Since it performs no memory allocation or IO, the only value that
3291 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3292 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003293
drh14da87f2013-11-20 21:51:33 +00003294 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003295 ** SQLite used to check if the root-page flags were sane at this point
3296 ** and report database corruption if they were not, but this check has
3297 ** since moved into the btree layer. */
3298 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drh5e00f6c2001-09-13 13:46:56 +00003299 break;
3300}
3301
drh2a5d9902011-08-26 00:34:45 +00003302/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003303** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003304**
drhb9bb7c12006-06-11 23:41:55 +00003305** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003306** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003307** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003308** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003309**
drh25d3adb2010-04-05 15:11:08 +00003310** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003311** The cursor points to a BTree table if P4==0 and to a BTree index
3312** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003313** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003314**
drh2a5d9902011-08-26 00:34:45 +00003315** The P5 parameter can be a mask of the BTREE_* flags defined
3316** in btree.h. These flags control aspects of the operation of
3317** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3318** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003319*/
drha21a64d2010-04-06 22:33:55 +00003320/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003321** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003322**
3323** This opcode works the same as OP_OpenEphemeral. It has a
3324** different name to distinguish its use. Tables created using
3325** by this opcode will be used for automatically created transient
3326** indices in joins.
3327*/
3328case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003329case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003330 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003331 KeyInfo *pKeyInfo;
3332
drhd4187c72010-08-30 22:15:45 +00003333 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003334 SQLITE_OPEN_READWRITE |
3335 SQLITE_OPEN_CREATE |
3336 SQLITE_OPEN_EXCLUSIVE |
3337 SQLITE_OPEN_DELETEONCLOSE |
3338 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003339 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003340 assert( pOp->p2>=0 );
drh653b82a2009-06-22 11:10:47 +00003341 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003342 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003343 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003344 pCx->isEphemeral = 1;
dan689ab892011-08-12 15:02:00 +00003345 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3346 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003347 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003348 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003349 }
3350 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003351 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003352 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003353 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003354 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003355 */
drh41e13e12013-11-07 14:09:39 +00003356 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003357 int pgno;
drh66a51672008-01-03 00:01:23 +00003358 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003359 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003360 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003361 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003362 assert( pKeyInfo->db==db );
3363 assert( pKeyInfo->enc==ENC(db) );
3364 pCx->pKeyInfo = pKeyInfo;
3365 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00003366 }
drhf0863fe2005-06-12 21:35:51 +00003367 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003368 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003369 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003370 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003371 }
drh5e00f6c2001-09-13 13:46:56 +00003372 }
drhd4187c72010-08-30 22:15:45 +00003373 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003374 break;
3375}
3376
danfad9f9a2014-04-01 18:41:51 +00003377/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003378**
3379** This opcode works like OP_OpenEphemeral except that it opens
3380** a transient index that is specifically designed to sort large
3381** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003382**
3383** If argument P3 is non-zero, then it indicates that the sorter may
3384** assume that a stable sort considering the first P3 fields of each
3385** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003386*/
drhca892a72011-09-03 00:17:51 +00003387case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003388 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003389
drh399af1d2013-11-20 17:25:55 +00003390 assert( pOp->p1>=0 );
3391 assert( pOp->p2>=0 );
dan5134d132011-09-02 10:31:11 +00003392 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3393 if( pCx==0 ) goto no_mem;
3394 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003395 assert( pCx->pKeyInfo->db==db );
3396 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003397 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003398 break;
3399}
3400
dan78d58432014-03-25 15:04:07 +00003401/* Opcode: SequenceTest P1 P2 * * *
3402** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3403**
3404** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3405** to P2. Regardless of whether or not the jump is taken, increment the
3406** the sequence value.
3407*/
3408case OP_SequenceTest: {
3409 VdbeCursor *pC;
3410 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3411 pC = p->apCsr[pOp->p1];
3412 assert( pC->pSorter );
3413 if( (pC->seqCount++)==0 ){
3414 pc = pOp->p2 - 1;
3415 }
3416 break;
3417}
3418
drh5f612292014-02-08 23:20:32 +00003419/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003420** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003421**
3422** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003423** row of data. The content of that one row is the content of memory
3424** register P2. In other words, cursor P1 becomes an alias for the
3425** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003426**
drh2d8d7ce2010-02-15 15:17:05 +00003427** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003428** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003429** individual columns using the OP_Column opcode. The OP_Column opcode
3430** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003431**
3432** P3 is the number of fields in the records that will be stored by
3433** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003434*/
drh9cbf3422008-01-17 16:22:13 +00003435case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003436 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003437
drh653b82a2009-06-22 11:10:47 +00003438 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003439 assert( pOp->p3>=0 );
drh653b82a2009-06-22 11:10:47 +00003440 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003441 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003442 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003443 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003444 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003445 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003446 break;
3447}
3448
drh98757152008-01-09 23:04:12 +00003449/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003450**
3451** Close a cursor previously opened as P1. If P1 is not
3452** currently open, this instruction is a no-op.
3453*/
drh9cbf3422008-01-17 16:22:13 +00003454case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003455 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3456 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3457 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003458 break;
3459}
3460
drh8af3f772014-07-25 18:01:06 +00003461/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003462** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003463**
danielk1977b790c6c2008-04-18 10:25:24 +00003464** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003465** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003466** to an SQL index, then P3 is the first in an array of P4 registers
3467** that are used as an unpacked index key.
3468**
3469** Reposition cursor P1 so that it points to the smallest entry that
3470** is greater than or equal to the key value. If there are no records
3471** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003472**
drh8af3f772014-07-25 18:01:06 +00003473** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003474** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003475** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003476**
drh935850e2014-05-24 17:15:15 +00003477** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003478*/
drh8af3f772014-07-25 18:01:06 +00003479/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003480** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003481**
danielk1977b790c6c2008-04-18 10:25:24 +00003482** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003483** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003484** to an SQL index, then P3 is the first in an array of P4 registers
3485** that are used as an unpacked index key.
3486**
3487** Reposition cursor P1 so that it points to the smallest entry that
3488** is greater than the key value. If there are no records greater than
3489** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003490**
drh8af3f772014-07-25 18:01:06 +00003491** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003492** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003493** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003494**
drh935850e2014-05-24 17:15:15 +00003495** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003496*/
drh8af3f772014-07-25 18:01:06 +00003497/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003498** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003499**
danielk1977b790c6c2008-04-18 10:25:24 +00003500** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003501** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003502** to an SQL index, then P3 is the first in an array of P4 registers
3503** that are used as an unpacked index key.
3504**
3505** Reposition cursor P1 so that it points to the largest entry that
3506** is less than the key value. If there are no records less than
3507** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003508**
drh8af3f772014-07-25 18:01:06 +00003509** This opcode leaves the cursor configured to move in reverse order,
3510** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003511** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003512**
drh935850e2014-05-24 17:15:15 +00003513** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003514*/
drh8af3f772014-07-25 18:01:06 +00003515/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003516** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003517**
danielk1977b790c6c2008-04-18 10:25:24 +00003518** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003519** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003520** to an SQL index, then P3 is the first in an array of P4 registers
3521** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003522**
danielk1977b790c6c2008-04-18 10:25:24 +00003523** Reposition cursor P1 so that it points to the largest entry that
3524** is less than or equal to the key value. If there are no records
3525** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003526**
drh8af3f772014-07-25 18:01:06 +00003527** This opcode leaves the cursor configured to move in reverse order,
3528** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003529** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003530**
drh935850e2014-05-24 17:15:15 +00003531** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003532*/
drh4a1d3652014-02-14 15:13:36 +00003533case OP_SeekLT: /* jump, in3 */
3534case OP_SeekLE: /* jump, in3 */
3535case OP_SeekGE: /* jump, in3 */
3536case OP_SeekGT: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003537 int res;
3538 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003539 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003540 UnpackedRecord r;
3541 int nField;
3542 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003543
drh653b82a2009-06-22 11:10:47 +00003544 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003545 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003546 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003547 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003548 assert( pC->pseudoTableReg==0 );
drh4a1d3652014-02-14 15:13:36 +00003549 assert( OP_SeekLE == OP_SeekLT+1 );
3550 assert( OP_SeekGE == OP_SeekLT+2 );
3551 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003552 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00003553 assert( pC->pCursor!=0 );
3554 oc = pOp->opcode;
3555 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003556#ifdef SQLITE_DEBUG
3557 pC->seekOp = pOp->opcode;
3558#endif
drh3da046d2013-11-11 03:24:11 +00003559 if( pC->isTable ){
3560 /* The input value in P3 might be of any type: integer, real, string,
3561 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003562 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003563 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003564 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003565 applyNumericAffinity(pIn3, 0);
3566 }
drh3da046d2013-11-11 03:24:11 +00003567 iKey = sqlite3VdbeIntValue(pIn3);
3568 pC->rowidIsValid = 0;
drh959403f2008-12-12 17:56:16 +00003569
drh3da046d2013-11-11 03:24:11 +00003570 /* If the P3 value could not be converted into an integer without
3571 ** loss of information, then special processing is required... */
3572 if( (pIn3->flags & MEM_Int)==0 ){
3573 if( (pIn3->flags & MEM_Real)==0 ){
3574 /* If the P3 value cannot be converted into any kind of a number,
3575 ** then the seek is not possible, so jump to P2 */
drh688852a2014-02-17 22:40:43 +00003576 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003577 break;
3578 }
drh959403f2008-12-12 17:56:16 +00003579
danaa1776f2013-11-26 18:22:59 +00003580 /* If the approximation iKey is larger than the actual real search
3581 ** term, substitute >= for > and < for <=. e.g. if the search term
3582 ** is 4.9 and the integer approximation 5:
3583 **
3584 ** (x > 4.9) -> (x >= 5)
3585 ** (x <= 4.9) -> (x < 5)
3586 */
drh74eaba42014-09-18 17:52:15 +00003587 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003588 assert( OP_SeekGE==(OP_SeekGT-1) );
3589 assert( OP_SeekLT==(OP_SeekLE-1) );
3590 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3591 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003592 }
3593
3594 /* If the approximation iKey is smaller than the actual real search
3595 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003596 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003597 assert( OP_SeekLE==(OP_SeekLT+1) );
3598 assert( OP_SeekGT==(OP_SeekGE+1) );
3599 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3600 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003601 }
drh3da046d2013-11-11 03:24:11 +00003602 }
3603 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
3604 if( rc!=SQLITE_OK ){
3605 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003606 }
drh3da046d2013-11-11 03:24:11 +00003607 if( res==0 ){
3608 pC->rowidIsValid = 1;
3609 pC->lastRowid = iKey;
drh8721ce42001-11-07 14:22:00 +00003610 }
drhaa736092009-06-22 00:55:30 +00003611 }else{
drh3da046d2013-11-11 03:24:11 +00003612 nField = pOp->p4.i;
3613 assert( pOp->p4type==P4_INT32 );
3614 assert( nField>0 );
3615 r.pKeyInfo = pC->pKeyInfo;
3616 r.nField = (u16)nField;
3617
3618 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003619 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003620 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003621 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003622 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003623 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003624 */
dan1fed5da2014-02-25 21:01:25 +00003625 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3626 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3627 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3628 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3629 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003630
3631 r.aMem = &aMem[pOp->p3];
3632#ifdef SQLITE_DEBUG
3633 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3634#endif
3635 ExpandBlob(r.aMem);
3636 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
3637 if( rc!=SQLITE_OK ){
3638 goto abort_due_to_error;
3639 }
3640 pC->rowidIsValid = 0;
3641 }
3642 pC->deferredMoveto = 0;
3643 pC->cacheStatus = CACHE_STALE;
3644#ifdef SQLITE_TEST
3645 sqlite3_search_count++;
3646#endif
drh4a1d3652014-02-14 15:13:36 +00003647 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3648 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003649 res = 0;
drh3da046d2013-11-11 03:24:11 +00003650 rc = sqlite3BtreeNext(pC->pCursor, &res);
3651 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3652 pC->rowidIsValid = 0;
3653 }else{
3654 res = 0;
3655 }
3656 }else{
drh4a1d3652014-02-14 15:13:36 +00003657 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3658 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003659 res = 0;
drh3da046d2013-11-11 03:24:11 +00003660 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3661 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3662 pC->rowidIsValid = 0;
3663 }else{
3664 /* res might be negative because the table is empty. Check to
3665 ** see if this is the case.
3666 */
3667 res = sqlite3BtreeEof(pC->pCursor);
3668 }
3669 }
3670 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003671 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003672 if( res ){
danielk1977f7b9d662008-06-23 18:49:43 +00003673 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003674 }
drh5e00f6c2001-09-13 13:46:56 +00003675 break;
3676}
3677
drh959403f2008-12-12 17:56:16 +00003678/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003679** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003680**
3681** P1 is an open table cursor and P2 is a rowid integer. Arrange
3682** for P1 to move so that it points to the rowid given by P2.
3683**
3684** This is actually a deferred seek. Nothing actually happens until
3685** the cursor is used to read a record. That way, if no reads
3686** occur, no unnecessary I/O happens.
3687*/
3688case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003689 VdbeCursor *pC;
3690
drh653b82a2009-06-22 11:10:47 +00003691 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3692 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003693 assert( pC!=0 );
drh3da046d2013-11-11 03:24:11 +00003694 assert( pC->pCursor!=0 );
3695 assert( pC->isTable );
3696 pC->nullRow = 0;
3697 pIn2 = &aMem[pOp->p2];
3698 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3699 pC->rowidIsValid = 0;
3700 pC->deferredMoveto = 1;
drh959403f2008-12-12 17:56:16 +00003701 break;
3702}
3703
3704
drh8cff69d2009-11-12 19:59:44 +00003705/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003706** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003707**
drh8cff69d2009-11-12 19:59:44 +00003708** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3709** P4>0 then register P3 is the first of P4 registers that form an unpacked
3710** record.
3711**
3712** Cursor P1 is on an index btree. If the record identified by P3 and P4
3713** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003714** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003715**
drhcefc87f2014-08-01 01:40:33 +00003716** This operation leaves the cursor in a state where it can be
3717** advanced in the forward direction. The Next instruction will work,
3718** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003719**
drh6f225d02013-10-26 13:36:51 +00003720** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003721*/
drh8cff69d2009-11-12 19:59:44 +00003722/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003723** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003724**
drh8cff69d2009-11-12 19:59:44 +00003725** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3726** P4>0 then register P3 is the first of P4 registers that form an unpacked
3727** record.
3728**
3729** Cursor P1 is on an index btree. If the record identified by P3 and P4
3730** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3731** does contain an entry whose prefix matches the P3/P4 record then control
3732** falls through to the next instruction and P1 is left pointing at the
3733** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003734**
drh8af3f772014-07-25 18:01:06 +00003735** This operation leaves the cursor in a state where it cannot be
3736** advanced in either direction. In other words, the Next and Prev
3737** opcodes do not work after this operation.
3738**
drh6f225d02013-10-26 13:36:51 +00003739** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003740*/
drh6f225d02013-10-26 13:36:51 +00003741/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003742** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003743**
3744** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3745** P4>0 then register P3 is the first of P4 registers that form an unpacked
3746** record.
3747**
3748** Cursor P1 is on an index btree. If the record identified by P3 and P4
3749** contains any NULL value, jump immediately to P2. If all terms of the
3750** record are not-NULL then a check is done to determine if any row in the
3751** P1 index btree has a matching key prefix. If there are no matches, jump
3752** immediately to P2. If there is a match, fall through and leave the P1
3753** cursor pointing to the matching row.
3754**
3755** This opcode is similar to OP_NotFound with the exceptions that the
3756** branch is always taken if any part of the search key input is NULL.
3757**
drh8af3f772014-07-25 18:01:06 +00003758** This operation leaves the cursor in a state where it cannot be
3759** advanced in either direction. In other words, the Next and Prev
3760** opcodes do not work after this operation.
3761**
drh6f225d02013-10-26 13:36:51 +00003762** See also: NotFound, Found, NotExists
3763*/
3764case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003765case OP_NotFound: /* jump, in3 */
3766case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003767 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00003768 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003769 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003770 int res;
dan03e9cfc2011-09-05 14:20:27 +00003771 char *pFree;
drh856c1032009-06-02 15:21:42 +00003772 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003773 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003774 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003775
dan0ff297e2009-09-25 17:03:14 +00003776#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003777 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003778#endif
3779
drhaa736092009-06-22 00:55:30 +00003780 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003781 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003782 pC = p->apCsr[pOp->p1];
3783 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003784#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00003785 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00003786#endif
drh3c657212009-11-17 23:59:58 +00003787 pIn3 = &aMem[pOp->p3];
drh3da046d2013-11-11 03:24:11 +00003788 assert( pC->pCursor!=0 );
3789 assert( pC->isTable==0 );
drha9ab4812013-12-11 11:00:44 +00003790 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
drh3da046d2013-11-11 03:24:11 +00003791 if( pOp->p4.i>0 ){
3792 r.pKeyInfo = pC->pKeyInfo;
3793 r.nField = (u16)pOp->p4.i;
3794 r.aMem = pIn3;
drh826af372014-02-08 19:12:21 +00003795 for(ii=0; ii<r.nField; ii++){
3796 assert( memIsValid(&r.aMem[ii]) );
3797 ExpandBlob(&r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003798#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00003799 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003800#endif
drh826af372014-02-08 19:12:21 +00003801 }
drh3da046d2013-11-11 03:24:11 +00003802 pIdxKey = &r;
3803 }else{
3804 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3805 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3806 );
3807 if( pIdxKey==0 ) goto no_mem;
3808 assert( pIn3->flags & MEM_Blob );
3809 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
3810 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh3da046d2013-11-11 03:24:11 +00003811 }
dan1fed5da2014-02-25 21:01:25 +00003812 pIdxKey->default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00003813 if( pOp->opcode==OP_NoConflict ){
3814 /* For the OP_NoConflict opcode, take the jump if any of the
3815 ** input fields are NULL, since any key with a NULL will not
3816 ** conflict */
3817 for(ii=0; ii<r.nField; ii++){
3818 if( r.aMem[ii].flags & MEM_Null ){
drh688852a2014-02-17 22:40:43 +00003819 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003820 break;
drh6f225d02013-10-26 13:36:51 +00003821 }
3822 }
drh5e00f6c2001-09-13 13:46:56 +00003823 }
drh3da046d2013-11-11 03:24:11 +00003824 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3825 if( pOp->p4.i==0 ){
3826 sqlite3DbFree(db, pFree);
3827 }
3828 if( rc!=SQLITE_OK ){
3829 break;
3830 }
drh1fd522f2013-11-21 00:10:35 +00003831 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003832 alreadyExists = (res==0);
3833 pC->nullRow = 1-alreadyExists;
3834 pC->deferredMoveto = 0;
3835 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003836 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00003837 VdbeBranchTaken(alreadyExists!=0,2);
drh5e00f6c2001-09-13 13:46:56 +00003838 if( alreadyExists ) pc = pOp->p2 - 1;
3839 }else{
drh688852a2014-02-17 22:40:43 +00003840 VdbeBranchTaken(alreadyExists==0,2);
drh5e00f6c2001-09-13 13:46:56 +00003841 if( !alreadyExists ) pc = pOp->p2 - 1;
3842 }
drh5e00f6c2001-09-13 13:46:56 +00003843 break;
3844}
3845
drh9cbf3422008-01-17 16:22:13 +00003846/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003847** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00003848**
drh261c02d2013-10-25 14:46:15 +00003849** P1 is the index of a cursor open on an SQL table btree (with integer
3850** keys). P3 is an integer rowid. If P1 does not contain a record with
3851** rowid P3 then jump immediately to P2. If P1 does contain a record
3852** with rowid P3 then leave the cursor pointing at that record and fall
3853** through to the next instruction.
drh6b125452002-01-28 15:53:03 +00003854**
drh261c02d2013-10-25 14:46:15 +00003855** The OP_NotFound opcode performs the same operation on index btrees
3856** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00003857**
drh8af3f772014-07-25 18:01:06 +00003858** This opcode leaves the cursor in a state where it cannot be advanced
3859** in either direction. In other words, the Next and Prev opcodes will
3860** not work following this opcode.
3861**
drh11e85272013-10-26 15:40:48 +00003862** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00003863*/
drh9cbf3422008-01-17 16:22:13 +00003864case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003865 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003866 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003867 int res;
3868 u64 iKey;
3869
drh3c657212009-11-17 23:59:58 +00003870 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003871 assert( pIn3->flags & MEM_Int );
3872 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3873 pC = p->apCsr[pOp->p1];
3874 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003875#ifdef SQLITE_DEBUG
3876 pC->seekOp = 0;
3877#endif
drhaa736092009-06-22 00:55:30 +00003878 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003879 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003880 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00003881 assert( pCrsr!=0 );
3882 res = 0;
3883 iKey = pIn3->u.i;
3884 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
3885 pC->lastRowid = pIn3->u.i;
3886 pC->rowidIsValid = res==0 ?1:0;
3887 pC->nullRow = 0;
3888 pC->cacheStatus = CACHE_STALE;
3889 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00003890 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003891 if( res!=0 ){
danielk1977f7b9d662008-06-23 18:49:43 +00003892 pc = pOp->p2 - 1;
3893 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003894 }
drh1fd522f2013-11-21 00:10:35 +00003895 pC->seekResult = res;
drh6b125452002-01-28 15:53:03 +00003896 break;
3897}
3898
drh4c583122008-01-04 22:01:03 +00003899/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00003900** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00003901**
drh4c583122008-01-04 22:01:03 +00003902** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003903** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003904** The sequence number on the cursor is incremented after this
3905** instruction.
drh4db38a72005-09-01 12:16:28 +00003906*/
drh4c583122008-01-04 22:01:03 +00003907case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003908 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3909 assert( p->apCsr[pOp->p1]!=0 );
3910 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003911 break;
3912}
3913
3914
drh98757152008-01-09 23:04:12 +00003915/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003916** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00003917**
drhf0863fe2005-06-12 21:35:51 +00003918** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003919** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003920** table that cursor P1 points to. The new record number is written
3921** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003922**
dan76d462e2009-08-30 11:42:51 +00003923** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3924** the largest previously generated record number. No new record numbers are
3925** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003926** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003927** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003928** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003929*/
drh4c583122008-01-04 22:01:03 +00003930case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003931 i64 v; /* The new rowid */
3932 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3933 int res; /* Result of an sqlite3BtreeLast() */
3934 int cnt; /* Counter to limit the number of searches */
3935 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003936 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003937
drh856c1032009-06-02 15:21:42 +00003938 v = 0;
3939 res = 0;
drhaa736092009-06-22 00:55:30 +00003940 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3941 pC = p->apCsr[pOp->p1];
3942 assert( pC!=0 );
3943 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003944 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003945 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003946 /* The next rowid or record number (different terms for the same
3947 ** thing) is obtained in a two-step algorithm.
3948 **
3949 ** First we attempt to find the largest existing rowid and add one
3950 ** to that. But if the largest existing rowid is already the maximum
3951 ** positive integer, we have to fall through to the second
3952 ** probabilistic algorithm
3953 **
3954 ** The second algorithm is to select a rowid at random and see if
3955 ** it already exists in the table. If it does not exist, we have
3956 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003957 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003958 */
drhaa736092009-06-22 00:55:30 +00003959 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003960
drh75f86a42005-02-17 00:03:06 +00003961#ifdef SQLITE_32BIT_ROWID
3962# define MAX_ROWID 0x7fffffff
3963#else
drhfe2093d2005-01-20 22:48:47 +00003964 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3965 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3966 ** to provide the constant while making all compilers happy.
3967 */
danielk197764202cf2008-11-17 15:31:47 +00003968# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003969#endif
drhfe2093d2005-01-20 22:48:47 +00003970
drh5cf8e8c2002-02-19 22:42:05 +00003971 if( !pC->useRandomRowid ){
drhe0670b62014-02-12 21:31:12 +00003972 rc = sqlite3BtreeLast(pC->pCursor, &res);
3973 if( rc!=SQLITE_OK ){
3974 goto abort_due_to_error;
3975 }
3976 if( res ){
3977 v = 1; /* IMP: R-61914-48074 */
3978 }else{
3979 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
3980 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3981 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
3982 if( v>=MAX_ROWID ){
3983 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003984 }else{
drhe0670b62014-02-12 21:31:12 +00003985 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00003986 }
drh3fc190c2001-09-14 03:24:23 +00003987 }
drhe0670b62014-02-12 21:31:12 +00003988 }
drh205f48e2004-11-05 00:43:11 +00003989
3990#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00003991 if( pOp->p3 ){
3992 /* Assert that P3 is a valid memory cell. */
3993 assert( pOp->p3>0 );
3994 if( p->pFrame ){
3995 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003996 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00003997 assert( pOp->p3<=pFrame->nMem );
3998 pMem = &pFrame->aMem[pOp->p3];
3999 }else{
4000 /* Assert that P3 is a valid memory cell. */
4001 assert( pOp->p3<=(p->nMem-p->nCursor) );
4002 pMem = &aMem[pOp->p3];
4003 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00004004 }
drhe0670b62014-02-12 21:31:12 +00004005 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00004006
drhe0670b62014-02-12 21:31:12 +00004007 REGISTER_TRACE(pOp->p3, pMem);
4008 sqlite3VdbeMemIntegerify(pMem);
4009 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4010 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4011 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
4012 goto abort_due_to_error;
4013 }
4014 if( v<pMem->u.i+1 ){
4015 v = pMem->u.i + 1;
4016 }
4017 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004018 }
drhe0670b62014-02-12 21:31:12 +00004019#endif
drh5cf8e8c2002-02-19 22:42:05 +00004020 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004021 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004022 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004023 ** engine starts picking positive candidate ROWIDs at random until
4024 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004025 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4026 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00004027 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00004028 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00004029 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
4030 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00004031 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00004032 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
4033 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004034 && (res==0)
4035 && (++cnt<100)){
4036 /* collision - try another random rowid */
4037 sqlite3_randomness(sizeof(v), &v);
4038 if( cnt<5 ){
4039 /* try "small" random rowids for the initial attempts */
4040 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00004041 }else{
shanehc4d340a2010-09-01 02:37:56 +00004042 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00004043 }
shanehc4d340a2010-09-01 02:37:56 +00004044 v++; /* ensure non-zero */
4045 }
drhaa736092009-06-22 00:55:30 +00004046 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004047 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004048 goto abort_due_to_error;
4049 }
drh748a52c2010-09-01 11:50:08 +00004050 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004051 }
drhf0863fe2005-06-12 21:35:51 +00004052 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00004053 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004054 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004055 }
drh4c583122008-01-04 22:01:03 +00004056 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004057 break;
4058}
4059
danielk19771f4aa332008-01-03 09:51:55 +00004060/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004061** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004062**
jplyon5a564222003-06-02 06:15:58 +00004063** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004064** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004065** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004066** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004067** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004068**
danielk19771f4aa332008-01-03 09:51:55 +00004069** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4070** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004071** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004072** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004073**
drh3e9ca092009-09-08 01:14:48 +00004074** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4075** the last seek operation (OP_NotExists) was a success, then this
4076** operation will not attempt to find the appropriate row before doing
4077** the insert but will instead overwrite the row that the cursor is
4078** currently pointing to. Presumably, the prior OP_NotExists opcode
4079** has already positioned the cursor correctly. This is an optimization
4080** that boosts performance by avoiding redundant seeks.
4081**
4082** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4083** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4084** is part of an INSERT operation. The difference is only important to
4085** the update hook.
4086**
drh66a51672008-01-03 00:01:23 +00004087** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004088** may be NULL. If it is not NULL, then the update-hook
4089** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4090**
drh93aed5a2008-01-16 17:46:38 +00004091** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4092** allocated, then ownership of P2 is transferred to the pseudo-cursor
4093** and register P2 becomes ephemeral. If the cursor is changed, the
4094** value of register P2 will then change. Make sure this does not
4095** cause any problems.)
4096**
drhf0863fe2005-06-12 21:35:51 +00004097** This instruction only works on tables. The equivalent instruction
4098** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004099*/
drhe05c9292009-10-29 13:48:10 +00004100/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004101** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004102**
4103** This works exactly like OP_Insert except that the key is the
4104** integer value P3, not the value of the integer stored in register P3.
4105*/
4106case OP_Insert:
4107case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004108 Mem *pData; /* MEM cell holding data for the record to be inserted */
4109 Mem *pKey; /* MEM cell holding key for the record */
4110 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4111 VdbeCursor *pC; /* Cursor to table into which insert is written */
4112 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004113 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004114 const char *zDb; /* database name - used by the update hook */
4115 const char *zTbl; /* Table name - used by the opdate hook */
4116 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004117
drha6c2ed92009-11-14 23:22:23 +00004118 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004119 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004120 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004121 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004122 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004123 assert( pC->pCursor!=0 );
4124 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004125 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004126 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004127
drhe05c9292009-10-29 13:48:10 +00004128 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004129 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004130 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004131 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004132 REGISTER_TRACE(pOp->p3, pKey);
4133 iKey = pKey->u.i;
4134 }else{
4135 assert( pOp->opcode==OP_InsertInt );
4136 iKey = pOp->p3;
4137 }
4138
drha05a7222008-01-19 03:35:58 +00004139 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004140 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004141 if( pData->flags & MEM_Null ){
4142 pData->z = 0;
4143 pData->n = 0;
4144 }else{
4145 assert( pData->flags & (MEM_Blob|MEM_Str) );
4146 }
drh3e9ca092009-09-08 01:14:48 +00004147 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4148 if( pData->flags & MEM_Zero ){
4149 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004150 }else{
drh3e9ca092009-09-08 01:14:48 +00004151 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004152 }
drh3e9ca092009-09-08 01:14:48 +00004153 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4154 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004155 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004156 );
drha05a7222008-01-19 03:35:58 +00004157 pC->rowidIsValid = 0;
4158 pC->deferredMoveto = 0;
4159 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004160
drha05a7222008-01-19 03:35:58 +00004161 /* Invoke the update-hook if required. */
4162 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004163 zDb = db->aDb[pC->iDb].zName;
4164 zTbl = pOp->p4.z;
4165 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004166 assert( pC->isTable );
4167 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4168 assert( pC->iDb>=0 );
4169 }
drh5e00f6c2001-09-13 13:46:56 +00004170 break;
4171}
4172
drh98757152008-01-09 23:04:12 +00004173/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004174**
drh5edc3122001-09-13 21:53:09 +00004175** Delete the record at which the P1 cursor is currently pointing.
4176**
4177** The cursor will be left pointing at either the next or the previous
4178** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004179** the next Next instruction will be a no-op. Hence it is OK to delete
drhbc5cf382014-08-06 01:08:07 +00004180** a record from within a Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004181**
rdcb0c374f2004-02-20 22:53:38 +00004182** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004183** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004184**
drh91fd4d42008-01-19 20:11:25 +00004185** P1 must not be pseudo-table. It has to be a real table with
4186** multiple rows.
4187**
4188** If P4 is not NULL, then it is the name of the table that P1 is
4189** pointing to. The update hook will be invoked, if it exists.
4190** If P4 is not NULL then the P1 cursor must have been positioned
4191** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004192*/
drh9cbf3422008-01-17 16:22:13 +00004193case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004194 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004195 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004196
drh653b82a2009-06-22 11:10:47 +00004197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4198 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004199 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004200 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
drhbbbb0e82013-11-26 23:27:07 +00004201 iKey = pC->lastRowid; /* Only used for the update hook */
danielk197794eb6a12005-12-15 15:22:08 +00004202
drh9a65f2c2009-06-22 19:05:40 +00004203 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4204 ** OP_Column on the same table without any intervening operations that
4205 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4206 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4207 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4208 ** to guard against future changes to the code generator.
4209 **/
4210 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004211 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004212 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4213
drh91fd4d42008-01-19 20:11:25 +00004214 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004215 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004216
drh91fd4d42008-01-19 20:11:25 +00004217 /* Invoke the update-hook if required. */
drhbbbb0e82013-11-26 23:27:07 +00004218 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
drh2c77be02013-11-27 21:07:03 +00004219 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
4220 db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
drh91fd4d42008-01-19 20:11:25 +00004221 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004222 }
danielk1977b28af712004-06-21 06:50:26 +00004223 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004224 break;
4225}
drhb7f1d9a2009-09-08 02:27:58 +00004226/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004227**
drhb7f1d9a2009-09-08 02:27:58 +00004228** The value of the change counter is copied to the database handle
4229** change counter (returned by subsequent calls to sqlite3_changes()).
4230** Then the VMs internal change counter resets to 0.
4231** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004232*/
drh9cbf3422008-01-17 16:22:13 +00004233case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004234 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004235 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004236 break;
4237}
4238
drh1153c7b2013-11-01 22:02:56 +00004239/* Opcode: SorterCompare P1 P2 P3 P4
drhac502322014-07-30 13:56:48 +00004240** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004241**
drh1153c7b2013-11-01 22:02:56 +00004242** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004243** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004244** the sorter cursor currently points to. Only the first P4 fields
4245** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004246**
4247** If either P3 or the sorter contains a NULL in one of their significant
4248** fields (not counting the P4 fields at the end which are ignored) then
4249** the comparison is assumed to be equal.
4250**
4251** Fall through to next instruction if the two records compare equal to
4252** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004253*/
4254case OP_SorterCompare: {
4255 VdbeCursor *pC;
4256 int res;
drhac502322014-07-30 13:56:48 +00004257 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004258
4259 pC = p->apCsr[pOp->p1];
4260 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004261 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004262 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004263 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004264 res = 0;
drhac502322014-07-30 13:56:48 +00004265 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004266 VdbeBranchTaken(res!=0,2);
dan5134d132011-09-02 10:31:11 +00004267 if( res ){
4268 pc = pOp->p2-1;
4269 }
4270 break;
4271};
4272
4273/* Opcode: SorterData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004274** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004275**
4276** Write into register P2 the current sorter data for sorter cursor P1.
4277*/
4278case OP_SorterData: {
4279 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004280
dan5134d132011-09-02 10:31:11 +00004281 pOut = &aMem[pOp->p2];
4282 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004283 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004284 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004285 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
dan5134d132011-09-02 10:31:11 +00004286 break;
4287}
4288
drh98757152008-01-09 23:04:12 +00004289/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004290** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004291**
drh98757152008-01-09 23:04:12 +00004292** Write into register P2 the complete row data for cursor P1.
4293** There is no interpretation of the data.
4294** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004295** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004296**
drhde4fcfd2008-01-19 23:50:26 +00004297** If the P1 cursor must be pointing to a valid row (not a NULL row)
4298** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004299*/
drh98757152008-01-09 23:04:12 +00004300/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004301** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004302**
drh98757152008-01-09 23:04:12 +00004303** Write into register P2 the complete row key for cursor P1.
4304** There is no interpretation of the data.
drh0fd61352014-02-07 02:29:45 +00004305** The key is copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004306** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004307**
drhde4fcfd2008-01-19 23:50:26 +00004308** If the P1 cursor must be pointing to a valid row (not a NULL row)
4309** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004310*/
danielk1977a7a8e142008-02-13 18:25:27 +00004311case OP_RowKey:
4312case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004313 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004314 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004315 u32 n;
drh856c1032009-06-02 15:21:42 +00004316 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004317
drha6c2ed92009-11-14 23:22:23 +00004318 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004319 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004320
drhf0863fe2005-06-12 21:35:51 +00004321 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004322 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4323 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004324 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004325 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004326 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004327 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004328 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004329 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004330 assert( pC->pCursor!=0 );
4331 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004332 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004333
4334 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4335 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4336 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4337 ** a no-op and can never fail. But we leave it in place as a safety.
4338 */
4339 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004340 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004341 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4342
drh14da87f2013-11-20 21:51:33 +00004343 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004344 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004345 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004346 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004347 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004348 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004349 }
drhbfb19dc2009-06-05 16:46:53 +00004350 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004351 }else{
drhb07028f2011-10-14 21:49:18 +00004352 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004353 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004354 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004355 goto too_big;
4356 }
drhde4fcfd2008-01-19 23:50:26 +00004357 }
drh322f2852014-09-19 00:43:39 +00004358 if( sqlite3VdbeMemClearAndResize(pOut, n) ){
danielk1977a7a8e142008-02-13 18:25:27 +00004359 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004360 }
danielk1977a7a8e142008-02-13 18:25:27 +00004361 pOut->n = n;
4362 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004363 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004364 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4365 }else{
4366 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004367 }
danielk197796cb76f2008-01-04 13:24:28 +00004368 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004369 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004370 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004371 break;
4372}
4373
drh2133d822008-01-03 18:44:59 +00004374/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004375** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004376**
drh2133d822008-01-03 18:44:59 +00004377** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004378** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004379**
4380** P1 can be either an ordinary table or a virtual table. There used to
4381** be a separate OP_VRowid opcode for use with virtual tables, but this
4382** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004383*/
drh4c583122008-01-04 22:01:03 +00004384case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004385 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004386 i64 v;
drh856c1032009-06-02 15:21:42 +00004387 sqlite3_vtab *pVtab;
4388 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004389
drh653b82a2009-06-22 11:10:47 +00004390 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4391 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004392 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004393 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004394 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004395 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004396 break;
4397 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004398 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004399#ifndef SQLITE_OMIT_VIRTUALTABLE
4400 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004401 pVtab = pC->pVtabCursor->pVtab;
4402 pModule = pVtab->pModule;
4403 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004404 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004405 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004406#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004407 }else{
drh6be240e2009-07-14 02:33:02 +00004408 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004409 rc = sqlite3VdbeCursorMoveto(pC);
4410 if( rc ) goto abort_due_to_error;
4411 if( pC->rowidIsValid ){
4412 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004413 }else{
drhc27ae612009-07-14 18:35:44 +00004414 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4415 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004416 }
drh5e00f6c2001-09-13 13:46:56 +00004417 }
drh4c583122008-01-04 22:01:03 +00004418 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004419 break;
4420}
4421
drh9cbf3422008-01-17 16:22:13 +00004422/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004423**
4424** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004425** that occur while the cursor is on the null row will always
4426** write a NULL.
drh17f71932002-02-21 12:01:27 +00004427*/
drh9cbf3422008-01-17 16:22:13 +00004428case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004429 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004430
drh653b82a2009-06-22 11:10:47 +00004431 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4432 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004433 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004434 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004435 pC->rowidIsValid = 0;
drh399af1d2013-11-20 17:25:55 +00004436 pC->cacheStatus = CACHE_STALE;
danielk1977be51a652008-10-08 17:58:48 +00004437 if( pC->pCursor ){
4438 sqlite3BtreeClearCursor(pC->pCursor);
4439 }
drh17f71932002-02-21 12:01:27 +00004440 break;
4441}
4442
drh9cbf3422008-01-17 16:22:13 +00004443/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004444**
drh8af3f772014-07-25 18:01:06 +00004445** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004446** will refer to the last entry in the database table or index.
4447** If the table or index is empty and P2>0, then jump immediately to P2.
4448** If P2 is 0 or if the table or index is not empty, fall through
4449** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004450**
4451** This opcode leaves the cursor configured to move in reverse order,
4452** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004453** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004454*/
drh9cbf3422008-01-17 16:22:13 +00004455case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004456 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004457 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004458 int res;
drh9562b552002-02-19 15:00:07 +00004459
drh653b82a2009-06-22 11:10:47 +00004460 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4461 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004462 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004463 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004464 res = 0;
drh3da046d2013-11-11 03:24:11 +00004465 assert( pCrsr!=0 );
4466 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004467 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004468 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004469 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004470 pC->cacheStatus = CACHE_STALE;
drh8af3f772014-07-25 18:01:06 +00004471#ifdef SQLITE_DEBUG
4472 pC->seekOp = OP_Last;
4473#endif
drh688852a2014-02-17 22:40:43 +00004474 if( pOp->p2>0 ){
4475 VdbeBranchTaken(res!=0,2);
4476 if( res ) pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004477 }
4478 break;
4479}
4480
drh0342b1f2005-09-01 03:07:44 +00004481
drh9cbf3422008-01-17 16:22:13 +00004482/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004483**
4484** This opcode does exactly the same thing as OP_Rewind except that
4485** it increments an undocumented global variable used for testing.
4486**
4487** Sorting is accomplished by writing records into a sorting index,
4488** then rewinding that index and playing it back from beginning to
4489** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4490** rewinding so that the global variable will be incremented and
4491** regression tests can determine whether or not the optimizer is
4492** correctly optimizing out sorts.
4493*/
drhc6aff302011-09-01 15:32:47 +00004494case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004495case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004496#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004497 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004498 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004499#endif
drh9b47ee32013-08-20 03:13:51 +00004500 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004501 /* Fall through into OP_Rewind */
4502}
drh9cbf3422008-01-17 16:22:13 +00004503/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004504**
drhf0863fe2005-06-12 21:35:51 +00004505** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004506** will refer to the first entry in the database table or index.
4507** If the table or index is empty and P2>0, then jump immediately to P2.
4508** If P2 is 0 or if the table or index is not empty, fall through
4509** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004510**
4511** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004512** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004513** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004514*/
drh9cbf3422008-01-17 16:22:13 +00004515case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004516 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004517 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004518 int res;
drh5e00f6c2001-09-13 13:46:56 +00004519
drh653b82a2009-06-22 11:10:47 +00004520 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4521 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004522 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004523 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004524 res = 1;
drh8af3f772014-07-25 18:01:06 +00004525#ifdef SQLITE_DEBUG
4526 pC->seekOp = OP_Rewind;
4527#endif
dan689ab892011-08-12 15:02:00 +00004528 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004529 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004530 }else{
4531 pCrsr = pC->pCursor;
4532 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004533 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004534 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004535 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004536 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004537 }
drh9c1905f2008-12-10 22:32:56 +00004538 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004539 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004540 VdbeBranchTaken(res!=0,2);
drha05a7222008-01-19 03:35:58 +00004541 if( res ){
drhf4dada72004-05-11 09:57:35 +00004542 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004543 }
4544 break;
4545}
4546
drh0fd61352014-02-07 02:29:45 +00004547/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004548**
4549** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004550** table or index. If there are no more key/value pairs then fall through
4551** to the following instruction. But if the cursor advance was successful,
4552** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004553**
drh5dad9a32014-07-25 18:37:42 +00004554** The Next opcode is only valid following an SeekGT, SeekGE, or
4555** OP_Rewind opcode used to position the cursor. Next is not allowed
4556** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00004557**
drhf93cd942013-11-21 03:12:25 +00004558** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4559** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004560**
drhe39a7322014-02-03 14:04:11 +00004561** The P3 value is a hint to the btree implementation. If P3==1, that
4562** means P1 is an SQL index and that this instruction could have been
4563** omitted if that index had been unique. P3 is usually 0. P3 is
4564** always either 0 or 1.
4565**
dana205a482011-08-27 18:48:57 +00004566** P4 is always of type P4_ADVANCE. The function pointer points to
4567** sqlite3BtreeNext().
4568**
drhafc266a2010-03-31 17:47:44 +00004569** If P5 is positive and the jump is taken, then event counter
4570** number P5-1 in the prepared statement is incremented.
4571**
drhf93cd942013-11-21 03:12:25 +00004572** See also: Prev, NextIfOpen
4573*/
drh0fd61352014-02-07 02:29:45 +00004574/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004575**
drh5dad9a32014-07-25 18:37:42 +00004576** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004577** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004578*/
drh0fd61352014-02-07 02:29:45 +00004579/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004580**
4581** Back up cursor P1 so that it points to the previous key/data pair in its
4582** table or index. If there is no previous key/value pairs then fall through
4583** to the following instruction. But if the cursor backup was successful,
4584** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004585**
drh8af3f772014-07-25 18:01:06 +00004586**
drh5dad9a32014-07-25 18:37:42 +00004587** The Prev opcode is only valid following an SeekLT, SeekLE, or
4588** OP_Last opcode used to position the cursor. Prev is not allowed
4589** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00004590**
drhf93cd942013-11-21 03:12:25 +00004591** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4592** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004593**
drhe39a7322014-02-03 14:04:11 +00004594** The P3 value is a hint to the btree implementation. If P3==1, that
4595** means P1 is an SQL index and that this instruction could have been
4596** omitted if that index had been unique. P3 is usually 0. P3 is
4597** always either 0 or 1.
4598**
dana205a482011-08-27 18:48:57 +00004599** P4 is always of type P4_ADVANCE. The function pointer points to
4600** sqlite3BtreePrevious().
4601**
drhafc266a2010-03-31 17:47:44 +00004602** If P5 is positive and the jump is taken, then event counter
4603** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004604*/
drh0fd61352014-02-07 02:29:45 +00004605/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004606**
drh5dad9a32014-07-25 18:37:42 +00004607** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004608** open it behaves a no-op.
4609*/
4610case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004611 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004612 int res;
drh8721ce42001-11-07 14:22:00 +00004613
drhf93cd942013-11-21 03:12:25 +00004614 pC = p->apCsr[pOp->p1];
4615 assert( isSorter(pC) );
drh323913c2014-03-23 16:29:23 +00004616 res = 0;
drhf93cd942013-11-21 03:12:25 +00004617 rc = sqlite3VdbeSorterNext(db, pC, &res);
4618 goto next_tail;
4619case OP_PrevIfOpen: /* jump */
4620case OP_NextIfOpen: /* jump */
4621 if( p->apCsr[pOp->p1]==0 ) break;
4622 /* Fall through */
4623case OP_Prev: /* jump */
4624case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004625 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004626 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004627 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00004628 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00004629 assert( pC!=0 );
4630 assert( pC->deferredMoveto==0 );
4631 assert( pC->pCursor );
drhe39a7322014-02-03 14:04:11 +00004632 assert( res==0 || (res==1 && pC->isTable==0) );
4633 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00004634 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4635 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4636 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4637 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00004638
4639 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
4640 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
4641 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
4642 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00004643 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00004644 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
4645 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
4646 || pC->seekOp==OP_Last );
4647
drhf93cd942013-11-21 03:12:25 +00004648 rc = pOp->p4.xAdvance(pC->pCursor, &res);
4649next_tail:
drha3460582008-07-11 21:02:53 +00004650 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004651 VdbeBranchTaken(res==0,2);
drha3460582008-07-11 21:02:53 +00004652 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004653 pC->nullRow = 0;
drha3460582008-07-11 21:02:53 +00004654 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004655 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004656#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004657 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004658#endif
drhf93cd942013-11-21 03:12:25 +00004659 }else{
4660 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004661 }
drhf0863fe2005-06-12 21:35:51 +00004662 pC->rowidIsValid = 0;
drh49afe3a2013-07-10 03:05:14 +00004663 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004664}
4665
danielk1977de630352009-05-04 11:42:29 +00004666/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004667** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004668**
drhef8662b2011-06-20 21:47:58 +00004669** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004670** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004671** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004672**
drhaa9b8962008-01-08 02:57:55 +00004673** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004674** insert is likely to be an append.
4675**
mistachkin21a919f2014-02-07 03:28:02 +00004676** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4677** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4678** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00004679**
mistachkin21a919f2014-02-07 03:28:02 +00004680** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
4681** just done a seek to the spot where the new entry is to be inserted.
4682** This flag avoids doing an extra seek.
drh0fd61352014-02-07 02:29:45 +00004683**
drhf0863fe2005-06-12 21:35:51 +00004684** This instruction only works for indices. The equivalent instruction
4685** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004686*/
drhca892a72011-09-03 00:17:51 +00004687case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004688case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004689 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004690 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004691 int nKey;
4692 const char *zKey;
4693
drh653b82a2009-06-22 11:10:47 +00004694 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4695 pC = p->apCsr[pOp->p1];
4696 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004697 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004698 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004699 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004700 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004701 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh3da046d2013-11-11 03:24:11 +00004702 assert( pCrsr!=0 );
4703 assert( pC->isTable==0 );
4704 rc = ExpandBlob(pIn2);
4705 if( rc==SQLITE_OK ){
4706 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004707 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh3da046d2013-11-11 03:24:11 +00004708 }else{
4709 nKey = pIn2->n;
4710 zKey = pIn2->z;
4711 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4712 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4713 );
4714 assert( pC->deferredMoveto==0 );
4715 pC->cacheStatus = CACHE_STALE;
danielk1977d908f5a2007-05-11 07:08:28 +00004716 }
drh5e00f6c2001-09-13 13:46:56 +00004717 }
drh5e00f6c2001-09-13 13:46:56 +00004718 break;
4719}
4720
drh4308e342013-11-11 16:55:52 +00004721/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004722** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004723**
drhe14006d2008-03-25 17:23:32 +00004724** The content of P3 registers starting at register P2 form
4725** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004726** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004727*/
drhe14006d2008-03-25 17:23:32 +00004728case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004729 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004730 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004731 int res;
4732 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004733
drhe14006d2008-03-25 17:23:32 +00004734 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004735 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004736 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4737 pC = p->apCsr[pOp->p1];
4738 assert( pC!=0 );
4739 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004740 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004741 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004742 r.pKeyInfo = pC->pKeyInfo;
4743 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00004744 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004745 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004746#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004747 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004748#endif
drh3da046d2013-11-11 03:24:11 +00004749 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4750 if( rc==SQLITE_OK && res==0 ){
4751 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004752 }
drh3da046d2013-11-11 03:24:11 +00004753 assert( pC->deferredMoveto==0 );
4754 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004755 break;
4756}
4757
drh2133d822008-01-03 18:44:59 +00004758/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004759** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004760**
drh2133d822008-01-03 18:44:59 +00004761** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004762** the end of the index key pointed to by cursor P1. This integer should be
4763** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004764**
drh9437bd22009-02-01 00:29:56 +00004765** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004766*/
drh4c583122008-01-04 22:01:03 +00004767case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004768 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004769 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004770 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004771
drh653b82a2009-06-22 11:10:47 +00004772 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4773 pC = p->apCsr[pOp->p1];
4774 assert( pC!=0 );
4775 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004776 assert( pCrsr!=0 );
drh3c657212009-11-17 23:59:58 +00004777 pOut->flags = MEM_Null;
drh3da046d2013-11-11 03:24:11 +00004778 rc = sqlite3VdbeCursorMoveto(pC);
4779 if( NEVER(rc) ) goto abort_due_to_error;
4780 assert( pC->deferredMoveto==0 );
4781 assert( pC->isTable==0 );
4782 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00004783 rowid = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00004784 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
drh3da046d2013-11-11 03:24:11 +00004785 if( rc!=SQLITE_OK ){
4786 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00004787 }
drh3da046d2013-11-11 03:24:11 +00004788 pOut->u.i = rowid;
4789 pOut->flags = MEM_Int;
drh8721ce42001-11-07 14:22:00 +00004790 }
4791 break;
4792}
4793
danielk197761dd5832008-04-18 11:31:12 +00004794/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004795** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004796**
danielk197761dd5832008-04-18 11:31:12 +00004797** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004798** key that omits the PRIMARY KEY. Compare this key value against the index
4799** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4800** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00004801**
danielk197761dd5832008-04-18 11:31:12 +00004802** If the P1 index entry is greater than or equal to the key value
4803** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00004804*/
4805/* Opcode: IdxGT P1 P2 P3 P4 P5
4806** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00004807**
drh4a1d3652014-02-14 15:13:36 +00004808** The P4 register values beginning with P3 form an unpacked index
4809** key that omits the PRIMARY KEY. Compare this key value against the index
4810** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4811** fields at the end.
4812**
4813** If the P1 index entry is greater than the key value
4814** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00004815*/
drh3bb9b932010-08-06 02:10:00 +00004816/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004817** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004818**
danielk197761dd5832008-04-18 11:31:12 +00004819** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004820** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4821** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4822** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004823**
danielk197761dd5832008-04-18 11:31:12 +00004824** If the P1 index entry is less than the key value then jump to P2.
4825** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00004826*/
drh4a1d3652014-02-14 15:13:36 +00004827/* Opcode: IdxLE P1 P2 P3 P4 P5
4828** Synopsis: key=r[P3@P4]
4829**
4830** The P4 register values beginning with P3 form an unpacked index
4831** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4832** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4833** ROWID on the P1 index.
4834**
4835** If the P1 index entry is less than or equal to the key value then jump
4836** to P2. Otherwise fall through to the next instruction.
4837*/
4838case OP_IdxLE: /* jump */
4839case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00004840case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00004841case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004842 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004843 int res;
4844 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004845
drh653b82a2009-06-22 11:10:47 +00004846 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4847 pC = p->apCsr[pOp->p1];
4848 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004849 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00004850 assert( pC->pCursor!=0);
4851 assert( pC->deferredMoveto==0 );
4852 assert( pOp->p5==0 || pOp->p5==1 );
4853 assert( pOp->p4type==P4_INT32 );
4854 r.pKeyInfo = pC->pKeyInfo;
4855 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00004856 if( pOp->opcode<OP_IdxLT ){
4857 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00004858 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004859 }else{
drh4a1d3652014-02-14 15:13:36 +00004860 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00004861 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004862 }
4863 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004864#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004865 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004866#endif
drh2dc06482013-12-11 00:59:10 +00004867 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00004868 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00004869 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
4870 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
4871 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00004872 res = -res;
4873 }else{
drh4a1d3652014-02-14 15:13:36 +00004874 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00004875 res++;
4876 }
drh688852a2014-02-17 22:40:43 +00004877 VdbeBranchTaken(res>0,2);
drh3da046d2013-11-11 03:24:11 +00004878 if( res>0 ){
4879 pc = pOp->p2 - 1 ;
drh8721ce42001-11-07 14:22:00 +00004880 }
4881 break;
4882}
4883
drh98757152008-01-09 23:04:12 +00004884/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004885**
4886** Delete an entire database table or index whose root page in the database
4887** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004888**
drh98757152008-01-09 23:04:12 +00004889** The table being destroyed is in the main database file if P3==0. If
4890** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004891** that is used to store tables create using CREATE TEMPORARY TABLE.
4892**
drh205f48e2004-11-05 00:43:11 +00004893** If AUTOVACUUM is enabled then it is possible that another root page
4894** might be moved into the newly deleted root page in order to keep all
4895** root pages contiguous at the beginning of the database. The former
4896** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004897** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004898** movement was required (because the table being dropped was already
4899** the last one in the database) then a zero is stored in register P2.
4900** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004901**
drhb19a2bc2001-09-16 00:13:26 +00004902** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004903*/
drh98757152008-01-09 23:04:12 +00004904case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004905 int iMoved;
drh3765df42006-06-28 18:18:09 +00004906 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004907 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004908 int iDb;
drh3a949872012-09-18 13:20:13 +00004909
drh9e92a472013-06-27 17:40:30 +00004910 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004911#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004912 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004913 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004914 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4915 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4916 ){
danielk1977212b2182006-06-23 14:32:08 +00004917 iCnt++;
4918 }
4919 }
drh3765df42006-06-28 18:18:09 +00004920#else
danc0537fe2013-06-28 19:41:43 +00004921 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004922#endif
drh3c657212009-11-17 23:59:58 +00004923 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004924 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004925 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004926 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004927 }else{
drh856c1032009-06-02 15:21:42 +00004928 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004929 assert( iCnt==1 );
drha7ab6d82014-07-21 15:44:39 +00004930 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00004931 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00004932 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004933 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004934 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004935#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004936 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004937 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4938 /* All OP_Destroy operations occur on the same btree */
4939 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4940 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004941 }
drh3765df42006-06-28 18:18:09 +00004942#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004943 }
drh5e00f6c2001-09-13 13:46:56 +00004944 break;
4945}
4946
danielk1977c7af4842008-10-27 13:59:33 +00004947/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004948**
4949** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004950** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004951** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004952**
drhf57b3392001-10-08 13:22:32 +00004953** The table being clear is in the main database file if P2==0. If
4954** P2==1 then the table to be clear is in the auxiliary database file
4955** that is used to store tables create using CREATE TEMPORARY TABLE.
4956**
shanebe217792009-03-05 04:20:31 +00004957** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004958** intkey table (an SQL table, not an index). In this case the row change
4959** count is incremented by the number of rows in the table being cleared.
4960** If P3 is greater than zero, then the value stored in register P3 is
4961** also incremented by the number of rows in the table being cleared.
4962**
drhb19a2bc2001-09-16 00:13:26 +00004963** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004964*/
drh9cbf3422008-01-17 16:22:13 +00004965case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004966 int nChange;
4967
4968 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004969 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00004970 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00004971 rc = sqlite3BtreeClearTable(
4972 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4973 );
4974 if( pOp->p3 ){
4975 p->nChange += nChange;
4976 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004977 assert( memIsValid(&aMem[pOp->p3]) );
4978 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004979 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004980 }
4981 }
drh5edc3122001-09-13 21:53:09 +00004982 break;
4983}
4984
drh65ea12c2014-03-19 17:41:36 +00004985/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00004986**
drh65ea12c2014-03-19 17:41:36 +00004987** Delete all contents from the ephemeral table or sorter
4988** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00004989**
drh65ea12c2014-03-19 17:41:36 +00004990** This opcode only works for cursors used for sorting and
4991** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00004992*/
drh65ea12c2014-03-19 17:41:36 +00004993case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00004994 VdbeCursor *pC;
4995
4996 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4997 pC = p->apCsr[pOp->p1];
4998 assert( pC!=0 );
drh65ea12c2014-03-19 17:41:36 +00004999 if( pC->pSorter ){
5000 sqlite3VdbeSorterReset(db, pC->pSorter);
5001 }else{
5002 assert( pC->isEphemeral );
5003 rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
5004 }
drh079a3072014-03-19 14:10:55 +00005005 break;
5006}
5007
drh4c583122008-01-04 22:01:03 +00005008/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005009** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00005010**
drh4c583122008-01-04 22:01:03 +00005011** Allocate a new table in the main database file if P1==0 or in the
5012** auxiliary database file if P1==1 or in an attached database if
5013** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005014** register P2
drh5b2fd562001-09-13 15:21:31 +00005015**
drhc6b52df2002-01-04 03:09:29 +00005016** The difference between a table and an index is this: A table must
5017** have a 4-byte integer key and can have arbitrary data. An index
5018** has an arbitrary key but no data.
5019**
drhb19a2bc2001-09-16 00:13:26 +00005020** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00005021*/
drh4c583122008-01-04 22:01:03 +00005022/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005023** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00005024**
drh4c583122008-01-04 22:01:03 +00005025** Allocate a new index in the main database file if P1==0 or in the
5026** auxiliary database file if P1==1 or in an attached database if
5027** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005028** register P2.
drhf57b3392001-10-08 13:22:32 +00005029**
drhc6b52df2002-01-04 03:09:29 +00005030** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00005031*/
drh4c583122008-01-04 22:01:03 +00005032case OP_CreateIndex: /* out2-prerelease */
5033case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00005034 int pgno;
drhf328bc82004-05-10 23:29:49 +00005035 int flags;
drh234c39d2004-07-24 03:30:47 +00005036 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005037
5038 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00005039 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005040 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005041 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005042 pDb = &db->aDb[pOp->p1];
5043 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00005044 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00005045 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00005046 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00005047 }else{
drhd4187c72010-08-30 22:15:45 +00005048 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00005049 }
drh234c39d2004-07-24 03:30:47 +00005050 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00005051 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005052 break;
5053}
5054
drh22645842011-03-24 01:34:03 +00005055/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005056**
5057** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005058** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005059**
5060** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005061** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005062*/
drh9cbf3422008-01-17 16:22:13 +00005063case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005064 int iDb;
5065 const char *zMaster;
5066 char *zSql;
5067 InitData initData;
5068
drhbdaec522011-04-04 00:14:43 +00005069 /* Any prepared statement that invokes this opcode will hold mutexes
5070 ** on every btree. This is a prerequisite for invoking
5071 ** sqlite3InitCallback().
5072 */
5073#ifdef SQLITE_DEBUG
5074 for(iDb=0; iDb<db->nDb; iDb++){
5075 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5076 }
5077#endif
drhbdaec522011-04-04 00:14:43 +00005078
drh856c1032009-06-02 15:21:42 +00005079 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005080 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005081 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005082 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00005083 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00005084 initData.db = db;
5085 initData.iDb = pOp->p1;
5086 initData.pzErrMsg = &p->zErrMsg;
5087 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005088 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00005089 db->aDb[iDb].zName, zMaster, pOp->p4.z);
5090 if( zSql==0 ){
5091 rc = SQLITE_NOMEM;
5092 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005093 assert( db->init.busy==0 );
5094 db->init.busy = 1;
5095 initData.rc = SQLITE_OK;
5096 assert( !db->mallocFailed );
5097 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5098 if( rc==SQLITE_OK ) rc = initData.rc;
5099 sqlite3DbFree(db, zSql);
5100 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005101 }
drh3c23a882007-01-09 14:01:13 +00005102 }
drh81028a42012-05-15 18:28:27 +00005103 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00005104 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00005105 goto no_mem;
5106 }
drh234c39d2004-07-24 03:30:47 +00005107 break;
5108}
5109
drh8bfdf722009-06-19 14:06:03 +00005110#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005111/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005112**
5113** Read the sqlite_stat1 table for database P1 and load the content
5114** of that table into the internal index hash table. This will cause
5115** the analysis to be used when preparing all subsequent queries.
5116*/
drh9cbf3422008-01-17 16:22:13 +00005117case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005118 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5119 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00005120 break;
5121}
drh8bfdf722009-06-19 14:06:03 +00005122#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005123
drh98757152008-01-09 23:04:12 +00005124/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005125**
5126** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005127** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005128** is dropped from disk (using the Destroy opcode) in order to keep
5129** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005130** schema consistent with what is on disk.
5131*/
drh9cbf3422008-01-17 16:22:13 +00005132case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005133 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005134 break;
5135}
5136
drh98757152008-01-09 23:04:12 +00005137/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005138**
5139** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005140** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005141** is dropped from disk (using the Destroy opcode)
5142** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005143** schema consistent with what is on disk.
5144*/
drh9cbf3422008-01-17 16:22:13 +00005145case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005146 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005147 break;
5148}
5149
drh98757152008-01-09 23:04:12 +00005150/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005151**
5152** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005153** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005154** is dropped from disk (using the Destroy opcode) in order to keep
5155** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005156** schema consistent with what is on disk.
5157*/
drh9cbf3422008-01-17 16:22:13 +00005158case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005159 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005160 break;
5161}
5162
drh234c39d2004-07-24 03:30:47 +00005163
drhb7f91642004-10-31 02:22:47 +00005164#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005165/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005166**
drh98757152008-01-09 23:04:12 +00005167** Do an analysis of the currently open database. Store in
5168** register P1 the text of an error message describing any problems.
5169** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005170**
drh98757152008-01-09 23:04:12 +00005171** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005172** At most reg(P3) errors will be reported.
5173** In other words, the analysis stops as soon as reg(P1) errors are
5174** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005175**
drh79069752004-05-22 21:30:40 +00005176** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005177** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005178** total.
drh21504322002-06-25 13:16:02 +00005179**
drh98757152008-01-09 23:04:12 +00005180** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005181** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005182**
drh1dcdbc02007-01-27 02:24:54 +00005183** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005184*/
drhaaab5722002-02-19 13:39:21 +00005185case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005186 int nRoot; /* Number of tables to check. (Number of root pages.) */
5187 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5188 int j; /* Loop counter */
5189 int nErr; /* Number of errors reported */
5190 char *z; /* Text of the error report */
5191 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005192
drh1713afb2013-06-28 01:24:57 +00005193 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005194 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005195 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005196 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005197 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005198 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005199 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005200 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005201 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005202 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005203 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005204 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005205 }
5206 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005207 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005208 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh98757152008-01-09 23:04:12 +00005209 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005210 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005211 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005212 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005213 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005214 if( nErr==0 ){
5215 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005216 }else if( z==0 ){
5217 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005218 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005219 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005220 }
drhb7654112008-01-12 12:48:07 +00005221 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005222 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005223 break;
5224}
drhb7f91642004-10-31 02:22:47 +00005225#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005226
drh3d4501e2008-12-04 20:40:10 +00005227/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005228** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005229**
drh3d4501e2008-12-04 20:40:10 +00005230** Insert the integer value held by register P2 into a boolean index
5231** held in register P1.
5232**
5233** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005234*/
drh93952eb2009-11-13 19:43:43 +00005235case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005236 pIn1 = &aMem[pOp->p1];
5237 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005238 assert( (pIn2->flags & MEM_Int)!=0 );
5239 if( (pIn1->flags & MEM_RowSet)==0 ){
5240 sqlite3VdbeMemSetRowSet(pIn1);
5241 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005242 }
drh93952eb2009-11-13 19:43:43 +00005243 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005244 break;
5245}
5246
5247/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005248** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005249**
5250** Extract the smallest value from boolean index P1 and put that value into
5251** register P3. Or, if boolean index P1 is initially empty, leave P3
5252** unchanged and jump to instruction P2.
5253*/
drh93952eb2009-11-13 19:43:43 +00005254case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005255 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005256
drh3c657212009-11-17 23:59:58 +00005257 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005258 if( (pIn1->flags & MEM_RowSet)==0
5259 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005260 ){
5261 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005262 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005263 pc = pOp->p2 - 1;
drh688852a2014-02-17 22:40:43 +00005264 VdbeBranchTaken(1,2);
drh3d4501e2008-12-04 20:40:10 +00005265 }else{
5266 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005267 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh688852a2014-02-17 22:40:43 +00005268 VdbeBranchTaken(0,2);
drh17435752007-08-16 04:30:38 +00005269 }
drh49afe3a2013-07-10 03:05:14 +00005270 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005271}
5272
drh1b26c7c2009-04-22 02:15:47 +00005273/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005274** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005275**
drhade97602009-04-21 15:05:18 +00005276** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005277** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005278** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005279** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005280** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005281**
drh1b26c7c2009-04-22 02:15:47 +00005282** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005283** of integers, where each set contains no duplicates. Each set
5284** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005285** must have P4==0, the final set P4=-1. P4 must be either -1 or
5286** non-negative. For non-negative values of P4 only the lower 4
5287** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005288**
5289** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005290** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005291** (b) when P4==-1 there is no need to insert the value, as it will
5292** never be tested for, and (c) when a value that is part of set X is
5293** inserted, there is no need to search to see if the same value was
5294** previously inserted as part of set X (only if it was previously
5295** inserted as part of some other set).
5296*/
drh1b26c7c2009-04-22 02:15:47 +00005297case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005298 int iSet;
5299 int exists;
5300
drh3c657212009-11-17 23:59:58 +00005301 pIn1 = &aMem[pOp->p1];
5302 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005303 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005304 assert( pIn3->flags&MEM_Int );
5305
drh1b26c7c2009-04-22 02:15:47 +00005306 /* If there is anything other than a rowset object in memory cell P1,
5307 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005308 */
drh733bf1b2009-04-22 00:47:00 +00005309 if( (pIn1->flags & MEM_RowSet)==0 ){
5310 sqlite3VdbeMemSetRowSet(pIn1);
5311 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005312 }
5313
5314 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005315 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005316 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005317 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005318 VdbeBranchTaken(exists!=0,2);
danielk19771d461462009-04-21 09:02:45 +00005319 if( exists ){
5320 pc = pOp->p2 - 1;
5321 break;
5322 }
5323 }
5324 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005325 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005326 }
5327 break;
5328}
5329
drh5e00f6c2001-09-13 13:46:56 +00005330
danielk197793758c82005-01-21 08:13:14 +00005331#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005332
drh0fd61352014-02-07 02:29:45 +00005333/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005334**
dan76d462e2009-08-30 11:42:51 +00005335** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005336**
dan76d462e2009-08-30 11:42:51 +00005337** P1 contains the address of the memory cell that contains the first memory
5338** cell in an array of values used as arguments to the sub-program. P2
5339** contains the address to jump to if the sub-program throws an IGNORE
5340** exception using the RAISE() function. Register P3 contains the address
5341** of a memory cell in this (the parent) VM that is used to allocate the
5342** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005343**
5344** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005345**
5346** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005347*/
dan76d462e2009-08-30 11:42:51 +00005348case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005349 int nMem; /* Number of memory registers for sub-program */
5350 int nByte; /* Bytes of runtime space required for sub-program */
5351 Mem *pRt; /* Register to allocate runtime space */
5352 Mem *pMem; /* Used to iterate through memory cells */
5353 Mem *pEnd; /* Last memory cell in new array */
5354 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5355 SubProgram *pProgram; /* Sub-program to execute */
5356 void *t; /* Token identifying trigger */
5357
5358 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005359 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005360 assert( pProgram->nOp>0 );
5361
dan1da40a32009-09-19 17:00:31 +00005362 /* If the p5 flag is clear, then recursive invocation of triggers is
5363 ** disabled for backwards compatibility (p5 is set if this sub-program
5364 ** is really a trigger, not a foreign key action, and the flag set
5365 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005366 **
5367 ** It is recursive invocation of triggers, at the SQL level, that is
5368 ** disabled. In some cases a single trigger may generate more than one
5369 ** SubProgram (if the trigger may be executed with more than one different
5370 ** ON CONFLICT algorithm). SubProgram structures associated with a
5371 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005372 ** variable. */
5373 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005374 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005375 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5376 if( pFrame ) break;
5377 }
5378
danf5894502009-10-07 18:41:19 +00005379 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005380 rc = SQLITE_ERROR;
5381 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5382 break;
5383 }
5384
5385 /* Register pRt is used to store the memory required to save the state
5386 ** of the current program, and the memory required at runtime to execute
5387 ** the trigger program. If this trigger has been fired before, then pRt
5388 ** is already allocated. Otherwise, it must be initialized. */
5389 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005390 /* SubProgram.nMem is set to the number of memory cells used by the
5391 ** program stored in SubProgram.aOp. As well as these, one memory
5392 ** cell is required for each cursor used by the program. Set local
5393 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5394 */
dan65a7cd12009-09-01 12:16:01 +00005395 nMem = pProgram->nMem + pProgram->nCsr;
5396 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005397 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005398 + pProgram->nCsr * sizeof(VdbeCursor *)
5399 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005400 pFrame = sqlite3DbMallocZero(db, nByte);
5401 if( !pFrame ){
5402 goto no_mem;
5403 }
5404 sqlite3VdbeMemRelease(pRt);
5405 pRt->flags = MEM_Frame;
5406 pRt->u.pFrame = pFrame;
5407
5408 pFrame->v = p;
5409 pFrame->nChildMem = nMem;
5410 pFrame->nChildCsr = pProgram->nCsr;
5411 pFrame->pc = pc;
5412 pFrame->aMem = p->aMem;
5413 pFrame->nMem = p->nMem;
5414 pFrame->apCsr = p->apCsr;
5415 pFrame->nCursor = p->nCursor;
5416 pFrame->aOp = p->aOp;
5417 pFrame->nOp = p->nOp;
5418 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005419 pFrame->aOnceFlag = p->aOnceFlag;
5420 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005421
5422 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5423 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005424 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005425 pMem->db = db;
5426 }
5427 }else{
5428 pFrame = pRt->u.pFrame;
5429 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5430 assert( pProgram->nCsr==pFrame->nChildCsr );
5431 assert( pc==pFrame->pc );
5432 }
5433
5434 p->nFrame++;
5435 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005436 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005437 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005438 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005439 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005440 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005441 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005442 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005443 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005444 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005445 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005446 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5447 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005448 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005449 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005450
5451 break;
5452}
5453
dan76d462e2009-08-30 11:42:51 +00005454/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005455**
dan76d462e2009-08-30 11:42:51 +00005456** This opcode is only ever present in sub-programs called via the
5457** OP_Program instruction. Copy a value currently stored in a memory
5458** cell of the calling (parent) frame to cell P2 in the current frames
5459** address space. This is used by trigger programs to access the new.*
5460** and old.* values.
dan165921a2009-08-28 18:53:45 +00005461**
dan76d462e2009-08-30 11:42:51 +00005462** The address of the cell in the parent frame is determined by adding
5463** the value of the P1 argument to the value of the P1 argument to the
5464** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005465*/
dan76d462e2009-08-30 11:42:51 +00005466case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005467 VdbeFrame *pFrame;
5468 Mem *pIn;
5469 pFrame = p->pFrame;
5470 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005471 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5472 break;
5473}
5474
danielk197793758c82005-01-21 08:13:14 +00005475#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005476
dan1da40a32009-09-19 17:00:31 +00005477#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005478/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005479** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005480**
dan0ff297e2009-09-25 17:03:14 +00005481** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5482** If P1 is non-zero, the database constraint counter is incremented
5483** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005484** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005485*/
dan32b09f22009-09-23 17:29:59 +00005486case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005487 if( db->flags & SQLITE_DeferFKs ){
5488 db->nDeferredImmCons += pOp->p2;
5489 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005490 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005491 }else{
dan0ff297e2009-09-25 17:03:14 +00005492 p->nFkConstraint += pOp->p2;
5493 }
5494 break;
5495}
5496
5497/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005498** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005499**
5500** This opcode tests if a foreign key constraint-counter is currently zero.
5501** If so, jump to instruction P2. Otherwise, fall through to the next
5502** instruction.
5503**
5504** If P1 is non-zero, then the jump is taken if the database constraint-counter
5505** is zero (the one that counts deferred constraint violations). If P1 is
5506** zero, the jump is taken if the statement constraint-counter is zero
5507** (immediate foreign key constraint violations).
5508*/
5509case OP_FkIfZero: { /* jump */
5510 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00005511 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005512 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005513 }else{
drh688852a2014-02-17 22:40:43 +00005514 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005515 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005516 }
dan1da40a32009-09-19 17:00:31 +00005517 break;
5518}
5519#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5520
drh205f48e2004-11-05 00:43:11 +00005521#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005522/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005523** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005524**
dan76d462e2009-08-30 11:42:51 +00005525** P1 is a register in the root frame of this VM (the root frame is
5526** different from the current frame if this instruction is being executed
5527** within a sub-program). Set the value of register P1 to the maximum of
5528** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005529**
5530** This instruction throws an error if the memory cell is not initially
5531** an integer.
5532*/
dan76d462e2009-08-30 11:42:51 +00005533case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005534 VdbeFrame *pFrame;
5535 if( p->pFrame ){
5536 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5537 pIn1 = &pFrame->aMem[pOp->p1];
5538 }else{
drha6c2ed92009-11-14 23:22:23 +00005539 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005540 }
drhec86c722011-12-09 17:27:51 +00005541 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005542 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005543 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005544 sqlite3VdbeMemIntegerify(pIn2);
5545 if( pIn1->u.i<pIn2->u.i){
5546 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005547 }
5548 break;
5549}
5550#endif /* SQLITE_OMIT_AUTOINCREMENT */
5551
drh98757152008-01-09 23:04:12 +00005552/* Opcode: IfPos P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005553** Synopsis: if r[P1]>0 goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005554**
drh98757152008-01-09 23:04:12 +00005555** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005556**
drh98757152008-01-09 23:04:12 +00005557** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005558** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005559*/
drh9cbf3422008-01-17 16:22:13 +00005560case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005561 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005562 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005563 VdbeBranchTaken( pIn1->u.i>0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005564 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005565 pc = pOp->p2 - 1;
5566 }
5567 break;
5568}
5569
drh4336b0e2014-08-05 00:53:51 +00005570/* Opcode: IfNeg P1 P2 P3 * *
5571** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
drh15007a92006-01-08 18:10:17 +00005572**
drhbc5cf382014-08-06 01:08:07 +00005573** Register P1 must contain an integer. Add literal P3 to the value in
drh4336b0e2014-08-05 00:53:51 +00005574** register P1 then if the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005575*/
drh9cbf3422008-01-17 16:22:13 +00005576case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005577 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005578 assert( pIn1->flags&MEM_Int );
drh4336b0e2014-08-05 00:53:51 +00005579 pIn1->u.i += pOp->p3;
drh688852a2014-02-17 22:40:43 +00005580 VdbeBranchTaken(pIn1->u.i<0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005581 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005582 pc = pOp->p2 - 1;
5583 }
5584 break;
5585}
5586
drh9b918ed2009-11-12 03:13:26 +00005587/* Opcode: IfZero P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005588** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
drhec7429a2005-10-06 16:53:14 +00005589**
drh9b918ed2009-11-12 03:13:26 +00005590** The register P1 must contain an integer. Add literal P3 to the
5591** value in register P1. If the result is exactly 0, jump to P2.
drhec7429a2005-10-06 16:53:14 +00005592*/
drh9cbf3422008-01-17 16:22:13 +00005593case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005594 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005595 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005596 pIn1->u.i += pOp->p3;
drh688852a2014-02-17 22:40:43 +00005597 VdbeBranchTaken(pIn1->u.i==0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005598 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005599 pc = pOp->p2 - 1;
5600 }
5601 break;
5602}
5603
drh98757152008-01-09 23:04:12 +00005604/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005605** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005606**
drh0bce8352002-02-28 00:41:10 +00005607** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005608** function has P5 arguments. P4 is a pointer to the FuncDef
5609** structure that specifies the function. Use register
5610** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005611**
drh98757152008-01-09 23:04:12 +00005612** The P5 arguments are taken from register P2 and its
5613** successors.
drhe5095352002-02-24 03:25:14 +00005614*/
drh9cbf3422008-01-17 16:22:13 +00005615case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005616 int n;
drhe5095352002-02-24 03:25:14 +00005617 int i;
drhc54a6172009-06-02 16:06:03 +00005618 Mem *pMem;
5619 Mem *pRec;
drh9bd038f2014-08-27 14:14:06 +00005620 Mem t;
danielk197722322fd2004-05-25 23:35:17 +00005621 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005622 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005623
drh856c1032009-06-02 15:21:42 +00005624 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005625 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005626 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005627 apVal = p->apArg;
5628 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005629 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005630 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005631 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005632 memAboutToChange(p, pRec);
drhe5095352002-02-24 03:25:14 +00005633 }
danielk19772dca4ac2008-01-03 11:50:29 +00005634 ctx.pFunc = pOp->p4.pFunc;
dan3bc9f742013-08-15 16:18:39 +00005635 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005636 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005637 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00005638 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9bd038f2014-08-27 14:14:06 +00005639 ctx.pOut = &t;
drh1350b032002-02-27 19:00:20 +00005640 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005641 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005642 ctx.skipFlag = 0;
drhd36e1042013-09-06 13:10:12 +00005643 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005644 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005645 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005646 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005647 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005648 }
drhee9ff672010-09-03 18:50:48 +00005649 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005650 if( ctx.isError ){
drh9bd038f2014-08-27 14:14:06 +00005651 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
drh69544ec2008-02-06 14:11:34 +00005652 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005653 }
drh7a957892012-02-02 17:35:43 +00005654 if( ctx.skipFlag ){
5655 assert( pOp[-1].opcode==OP_CollSeq );
5656 i = pOp[-1].p1;
5657 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5658 }
drh9bd038f2014-08-27 14:14:06 +00005659 sqlite3VdbeMemRelease(&t);
drh5e00f6c2001-09-13 13:46:56 +00005660 break;
5661}
5662
drh98757152008-01-09 23:04:12 +00005663/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005664** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005665**
drh13449892005-09-07 21:22:45 +00005666** Execute the finalizer function for an aggregate. P1 is
5667** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005668**
5669** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005670** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005671** argument is not used by this opcode. It is only there to disambiguate
5672** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005673** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005674** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005675*/
drh9cbf3422008-01-17 16:22:13 +00005676case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005677 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005678 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005679 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005680 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005681 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005682 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005683 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005684 }
drh2dca8682008-03-21 17:13:13 +00005685 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005686 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005687 if( sqlite3VdbeMemTooBig(pMem) ){
5688 goto too_big;
5689 }
drh5e00f6c2001-09-13 13:46:56 +00005690 break;
5691}
5692
dan5cf53532010-05-01 16:40:20 +00005693#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005694/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005695**
5696** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005697** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005698** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5699** SQLITE_BUSY or not, respectively. Write the number of pages in the
5700** WAL after the checkpoint into mem[P3+1] and the number of pages
5701** in the WAL that have been checkpointed after the checkpoint
5702** completes into mem[P3+2]. However on an error, mem[P3+1] and
5703** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005704*/
5705case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005706 int i; /* Loop counter */
5707 int aRes[3]; /* Results */
5708 Mem *pMem; /* Write results here */
5709
drh9e92a472013-06-27 17:40:30 +00005710 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005711 aRes[0] = 0;
5712 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005713 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5714 || pOp->p2==SQLITE_CHECKPOINT_FULL
5715 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5716 );
drh30aa3b92011-02-07 23:56:01 +00005717 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005718 if( rc==SQLITE_BUSY ){
5719 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005720 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005721 }
drh30aa3b92011-02-07 23:56:01 +00005722 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5723 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5724 }
dan7c246102010-04-12 19:00:29 +00005725 break;
5726};
dan5cf53532010-05-01 16:40:20 +00005727#endif
drh5e00f6c2001-09-13 13:46:56 +00005728
drhcac29a62010-07-02 19:36:52 +00005729#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00005730/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005731**
5732** Change the journal mode of database P1 to P3. P3 must be one of the
5733** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5734** modes (delete, truncate, persist, off and memory), this is a simple
5735** operation. No IO is required.
5736**
5737** If changing into or out of WAL mode the procedure is more complicated.
5738**
5739** Write a string containing the final journal-mode to register P2.
5740*/
drhd80b2332010-05-01 00:59:37 +00005741case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005742 Btree *pBt; /* Btree to change journal mode of */
5743 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005744 int eNew; /* New journal mode */
5745 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005746#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005747 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005748#endif
dane04dc882010-04-20 18:53:15 +00005749
drhd80b2332010-05-01 00:59:37 +00005750 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005751 assert( eNew==PAGER_JOURNALMODE_DELETE
5752 || eNew==PAGER_JOURNALMODE_TRUNCATE
5753 || eNew==PAGER_JOURNALMODE_PERSIST
5754 || eNew==PAGER_JOURNALMODE_OFF
5755 || eNew==PAGER_JOURNALMODE_MEMORY
5756 || eNew==PAGER_JOURNALMODE_WAL
5757 || eNew==PAGER_JOURNALMODE_QUERY
5758 );
5759 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005760 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005761
dane04dc882010-04-20 18:53:15 +00005762 pBt = db->aDb[pOp->p1].pBt;
5763 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005764 eOld = sqlite3PagerGetJournalMode(pPager);
5765 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5766 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005767
5768#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005769 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005770
drhd80b2332010-05-01 00:59:37 +00005771 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005772 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005773 */
5774 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005775 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005776 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005777 ){
drh0b9b4302010-06-11 17:01:24 +00005778 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005779 }
5780
drh0b9b4302010-06-11 17:01:24 +00005781 if( (eNew!=eOld)
5782 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5783 ){
danc0537fe2013-06-28 19:41:43 +00005784 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005785 rc = SQLITE_ERROR;
5786 sqlite3SetString(&p->zErrMsg, db,
5787 "cannot change %s wal mode from within a transaction",
5788 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5789 );
5790 break;
5791 }else{
5792
5793 if( eOld==PAGER_JOURNALMODE_WAL ){
5794 /* If leaving WAL mode, close the log file. If successful, the call
5795 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5796 ** file. An EXCLUSIVE lock may still be held on the database file
5797 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005798 */
drh0b9b4302010-06-11 17:01:24 +00005799 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005800 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005801 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005802 }
drh242c4f72010-06-22 14:49:39 +00005803 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5804 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5805 ** as an intermediate */
5806 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005807 }
5808
5809 /* Open a transaction on the database file. Regardless of the journal
5810 ** mode, this transaction always uses a rollback journal.
5811 */
5812 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5813 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005814 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005815 }
5816 }
5817 }
dan5cf53532010-05-01 16:40:20 +00005818#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005819
dand956efe2010-06-18 16:13:45 +00005820 if( rc ){
dand956efe2010-06-18 16:13:45 +00005821 eNew = eOld;
5822 }
drh0b9b4302010-06-11 17:01:24 +00005823 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005824
dane04dc882010-04-20 18:53:15 +00005825 pOut = &aMem[pOp->p2];
5826 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005827 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005828 pOut->n = sqlite3Strlen30(pOut->z);
5829 pOut->enc = SQLITE_UTF8;
5830 sqlite3VdbeChangeEncoding(pOut, encoding);
5831 break;
drhcac29a62010-07-02 19:36:52 +00005832};
5833#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005834
drhfdbcdee2007-03-27 14:44:50 +00005835#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005836/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005837**
5838** Vacuum the entire database. This opcode will cause other virtual
5839** machines to be created and run. It may not be called from within
5840** a transaction.
5841*/
drh9cbf3422008-01-17 16:22:13 +00005842case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005843 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005844 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005845 break;
5846}
drh154d4b22006-09-21 11:02:16 +00005847#endif
drh6f8c91c2003-12-07 00:24:35 +00005848
danielk1977dddbcdc2007-04-26 14:42:34 +00005849#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005850/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005851**
5852** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005853** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005854** P2. Otherwise, fall through to the next instruction.
5855*/
drh9cbf3422008-01-17 16:22:13 +00005856case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005857 Btree *pBt;
5858
5859 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005860 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005861 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005862 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005863 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00005864 VdbeBranchTaken(rc==SQLITE_DONE,2);
danielk1977dddbcdc2007-04-26 14:42:34 +00005865 if( rc==SQLITE_DONE ){
5866 pc = pOp->p2 - 1;
5867 rc = SQLITE_OK;
5868 }
5869 break;
5870}
5871#endif
5872
drh98757152008-01-09 23:04:12 +00005873/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005874**
drh25df48d2014-07-22 14:58:12 +00005875** Cause precompiled statements to expire. When an expired statement
5876** is executed using sqlite3_step() it will either automatically
5877** reprepare itself (if it was originally created using sqlite3_prepare_v2())
5878** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00005879**
5880** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00005881** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00005882*/
drh9cbf3422008-01-17 16:22:13 +00005883case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005884 if( !pOp->p1 ){
5885 sqlite3ExpirePreparedStatements(db);
5886 }else{
5887 p->expired = 1;
5888 }
5889 break;
5890}
5891
danielk1977c00da102006-01-07 13:21:04 +00005892#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005893/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005894** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00005895**
5896** Obtain a lock on a particular table. This instruction is only used when
5897** the shared-cache feature is enabled.
5898**
danielk197796d48e92009-06-29 06:00:37 +00005899** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005900** on which the lock is acquired. A readlock is obtained if P3==0 or
5901** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005902**
5903** P2 contains the root-page of the table to lock.
5904**
drh66a51672008-01-03 00:01:23 +00005905** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005906** used to generate an error message if the lock cannot be obtained.
5907*/
drh9cbf3422008-01-17 16:22:13 +00005908case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005909 u8 isWriteLock = (u8)pOp->p3;
5910 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5911 int p1 = pOp->p1;
5912 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005913 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005914 assert( isWriteLock==0 || isWriteLock==1 );
5915 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5916 if( (rc&0xFF)==SQLITE_LOCKED ){
5917 const char *z = pOp->p4.z;
5918 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5919 }
danielk1977c00da102006-01-07 13:21:04 +00005920 }
5921 break;
5922}
drhb9bb7c12006-06-11 23:41:55 +00005923#endif /* SQLITE_OMIT_SHARED_CACHE */
5924
5925#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005926/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005927**
danielk19773e3a84d2008-08-01 17:37:40 +00005928** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5929** xBegin method for that table.
5930**
5931** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005932** within a callback to a virtual table xSync() method. If it is, the error
5933** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005934*/
drh9cbf3422008-01-17 16:22:13 +00005935case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005936 VTable *pVTab;
5937 pVTab = pOp->p4.pVtab;
5938 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00005939 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005940 break;
5941}
5942#endif /* SQLITE_OMIT_VIRTUALTABLE */
5943
5944#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005945/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005946**
drh66a51672008-01-03 00:01:23 +00005947** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005948** for that table.
5949*/
drh9cbf3422008-01-17 16:22:13 +00005950case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005951 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005952 break;
5953}
5954#endif /* SQLITE_OMIT_VIRTUALTABLE */
5955
5956#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005957/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005958**
drh66a51672008-01-03 00:01:23 +00005959** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005960** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005961*/
drh9cbf3422008-01-17 16:22:13 +00005962case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005963 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005964 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005965 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005966 break;
5967}
5968#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005969
drh9eff6162006-06-12 21:59:13 +00005970#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005971/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005972**
drh66a51672008-01-03 00:01:23 +00005973** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005974** P1 is a cursor number. This opcode opens a cursor to the virtual
5975** table and stores that cursor in P1.
5976*/
drh9cbf3422008-01-17 16:22:13 +00005977case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005978 VdbeCursor *pCur;
5979 sqlite3_vtab_cursor *pVtabCursor;
5980 sqlite3_vtab *pVtab;
5981 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005982
drh1713afb2013-06-28 01:24:57 +00005983 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005984 pCur = 0;
5985 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005986 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005987 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005988 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005989 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00005990 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005991 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005992 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005993 pVtabCursor->pVtab = pVtab;
5994
mistachkin48864df2013-03-21 21:20:32 +00005995 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005996 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005997 if( pCur ){
5998 pCur->pVtabCursor = pVtabCursor;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005999 }else{
drh17435752007-08-16 04:30:38 +00006000 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00006001 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00006002 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00006003 }
drh9eff6162006-06-12 21:59:13 +00006004 break;
6005}
6006#endif /* SQLITE_OMIT_VIRTUALTABLE */
6007
6008#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00006009/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00006010** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00006011**
6012** P1 is a cursor opened using VOpen. P2 is an address to jump to if
6013** the filtered result set is empty.
6014**
drh66a51672008-01-03 00:01:23 +00006015** P4 is either NULL or a string that was generated by the xBestIndex
6016** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006017** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006018**
drh9eff6162006-06-12 21:59:13 +00006019** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006020** by P1. The integer query plan parameter to xFilter is stored in register
6021** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006022** xFilter method. Registers P3+2..P3+1+argc are the argc
6023** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006024** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006025**
danielk19776dbee812008-01-03 18:39:41 +00006026** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006027*/
drh9cbf3422008-01-17 16:22:13 +00006028case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006029 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006030 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006031 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006032 Mem *pQuery;
6033 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00006034 sqlite3_vtab_cursor *pVtabCursor;
6035 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006036 VdbeCursor *pCur;
6037 int res;
6038 int i;
6039 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006040
drha6c2ed92009-11-14 23:22:23 +00006041 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006042 pArgc = &pQuery[1];
6043 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006044 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006045 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006046 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00006047 pVtabCursor = pCur->pVtabCursor;
6048 pVtab = pVtabCursor->pVtab;
6049 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006050
drh9cbf3422008-01-17 16:22:13 +00006051 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006052 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006053 nArg = (int)pArgc->u.i;
6054 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006055
drh644a5292006-12-20 14:53:38 +00006056 /* Invoke the xFilter method */
6057 {
drh856c1032009-06-02 15:21:42 +00006058 res = 0;
6059 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00006060 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00006061 apArg[i] = &pArgc[i+1];
danielk19775fac9f82006-06-13 14:16:58 +00006062 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00006063
danielk1977be718892006-06-23 08:05:19 +00006064 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00006065 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00006066 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006067 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00006068 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00006069 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00006070 }
drh688852a2014-02-17 22:40:43 +00006071 VdbeBranchTaken(res!=0,2);
danielk1977a298e902006-06-22 09:53:48 +00006072 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00006073 pc = pOp->p2 - 1;
6074 }
6075 }
drh1d454a32008-01-31 19:34:51 +00006076 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006077
drh9eff6162006-06-12 21:59:13 +00006078 break;
6079}
6080#endif /* SQLITE_OMIT_VIRTUALTABLE */
6081
6082#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006083/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006084** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006085**
drh2133d822008-01-03 18:44:59 +00006086** Store the value of the P2-th column of
6087** the row of the virtual-table that the
6088** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006089*/
6090case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006091 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006092 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006093 Mem *pDest;
6094 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006095
drhdfe88ec2008-11-03 20:55:06 +00006096 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006097 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00006098 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006099 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006100 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006101 if( pCur->nullRow ){
6102 sqlite3VdbeMemSetNull(pDest);
6103 break;
6104 }
danielk19773e3a84d2008-08-01 17:37:40 +00006105 pVtab = pCur->pVtabCursor->pVtab;
6106 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006107 assert( pModule->xColumn );
6108 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006109 sContext.pOut = pDest;
6110 MemSetTypeFlag(pDest, MEM_Null);
drhde4fcfd2008-01-19 23:50:26 +00006111 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006112 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006113 if( sContext.isError ){
6114 rc = sContext.isError;
6115 }
drh9bd038f2014-08-27 14:14:06 +00006116 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006117 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006118 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006119
drhde4fcfd2008-01-19 23:50:26 +00006120 if( sqlite3VdbeMemTooBig(pDest) ){
6121 goto too_big;
6122 }
drh9eff6162006-06-12 21:59:13 +00006123 break;
6124}
6125#endif /* SQLITE_OMIT_VIRTUALTABLE */
6126
6127#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006128/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006129**
6130** Advance virtual table P1 to the next row in its result set and
6131** jump to instruction P2. Or, if the virtual table has reached
6132** the end of its result set, then fall through to the next instruction.
6133*/
drh9cbf3422008-01-17 16:22:13 +00006134case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006135 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006136 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006137 int res;
drh856c1032009-06-02 15:21:42 +00006138 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006139
drhc54a6172009-06-02 16:06:03 +00006140 res = 0;
drh856c1032009-06-02 15:21:42 +00006141 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006142 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006143 if( pCur->nullRow ){
6144 break;
6145 }
danielk19773e3a84d2008-08-01 17:37:40 +00006146 pVtab = pCur->pVtabCursor->pVtab;
6147 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006148 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006149
drhde4fcfd2008-01-19 23:50:26 +00006150 /* Invoke the xNext() method of the module. There is no way for the
6151 ** underlying implementation to return an error if one occurs during
6152 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6153 ** data is available) and the error code returned when xColumn or
6154 ** some other method is next invoked on the save virtual table cursor.
6155 */
drhde4fcfd2008-01-19 23:50:26 +00006156 p->inVtabMethod = 1;
6157 rc = pModule->xNext(pCur->pVtabCursor);
6158 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006159 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006160 if( rc==SQLITE_OK ){
6161 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006162 }
drh688852a2014-02-17 22:40:43 +00006163 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006164 if( !res ){
6165 /* If there is data, jump to P2 */
6166 pc = pOp->p2 - 1;
6167 }
drh49afe3a2013-07-10 03:05:14 +00006168 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006169}
6170#endif /* SQLITE_OMIT_VIRTUALTABLE */
6171
danielk1977182c4ba2007-06-27 15:53:34 +00006172#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006173/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006174**
drh66a51672008-01-03 00:01:23 +00006175** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006176** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006177** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006178*/
drh9cbf3422008-01-17 16:22:13 +00006179case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006180 sqlite3_vtab *pVtab;
6181 Mem *pName;
6182
danielk1977595a5232009-07-24 17:58:53 +00006183 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006184 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006185 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006186 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006187 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006188 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006189 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006190 testcase( pName->enc==SQLITE_UTF8 );
6191 testcase( pName->enc==SQLITE_UTF16BE );
6192 testcase( pName->enc==SQLITE_UTF16LE );
6193 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6194 if( rc==SQLITE_OK ){
6195 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006196 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006197 p->expired = 0;
6198 }
danielk1977182c4ba2007-06-27 15:53:34 +00006199 break;
6200}
6201#endif
drh4cbdda92006-06-14 19:00:20 +00006202
6203#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006204/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006205** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006206**
drh66a51672008-01-03 00:01:23 +00006207** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006208** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006209** are contiguous memory cells starting at P3 to pass to the xUpdate
6210** invocation. The value in register (P3+P2-1) corresponds to the
6211** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006212**
6213** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006214** The argv[0] element (which corresponds to memory cell P3)
6215** is the rowid of a row to delete. If argv[0] is NULL then no
6216** deletion occurs. The argv[1] element is the rowid of the new
6217** row. This can be NULL to have the virtual table select the new
6218** rowid for itself. The subsequent elements in the array are
6219** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006220**
6221** If P2==1 then no insert is performed. argv[0] is the rowid of
6222** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006223**
6224** P1 is a boolean flag. If it is set to true and the xUpdate call
6225** is successful, then the value returned by sqlite3_last_insert_rowid()
6226** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006227**
6228** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6229** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006230*/
drh9cbf3422008-01-17 16:22:13 +00006231case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006232 sqlite3_vtab *pVtab;
6233 sqlite3_module *pModule;
6234 int nArg;
6235 int i;
6236 sqlite_int64 rowid;
6237 Mem **apArg;
6238 Mem *pX;
6239
danb061d052011-04-25 18:49:57 +00006240 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6241 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6242 );
drh9e92a472013-06-27 17:40:30 +00006243 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006244 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006245 pModule = (sqlite3_module *)pVtab->pModule;
6246 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006247 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006248 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006249 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006250 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006251 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006252 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006253 assert( memIsValid(pX) );
6254 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006255 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006256 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006257 }
danb061d052011-04-25 18:49:57 +00006258 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006259 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006260 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006261 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006262 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006263 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006264 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006265 }
drhd91c1a12013-02-09 13:58:25 +00006266 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006267 if( pOp->p5==OE_Ignore ){
6268 rc = SQLITE_OK;
6269 }else{
6270 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6271 }
6272 }else{
6273 p->nChange++;
6274 }
danielk1977399918f2006-06-14 13:03:23 +00006275 }
drh4cbdda92006-06-14 19:00:20 +00006276 break;
danielk1977399918f2006-06-14 13:03:23 +00006277}
6278#endif /* SQLITE_OMIT_VIRTUALTABLE */
6279
danielk197759a93792008-05-15 17:48:20 +00006280#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6281/* Opcode: Pagecount P1 P2 * * *
6282**
6283** Write the current number of pages in database P1 to memory cell P2.
6284*/
6285case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006286 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006287 break;
6288}
6289#endif
6290
drh60ac3f42010-11-23 18:59:27 +00006291
6292#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6293/* Opcode: MaxPgcnt P1 P2 P3 * *
6294**
6295** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006296** Do not let the maximum page count fall below the current page count and
6297** do not change the maximum page count value if P3==0.
6298**
drh60ac3f42010-11-23 18:59:27 +00006299** Store the maximum page count after the change in register P2.
6300*/
6301case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006302 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006303 Btree *pBt;
6304
6305 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006306 newMax = 0;
6307 if( pOp->p3 ){
6308 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006309 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006310 }
6311 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006312 break;
6313}
6314#endif
6315
6316
drhaceb31b2014-02-08 01:40:27 +00006317/* Opcode: Init * P2 * P4 *
6318** Synopsis: Start at P2
6319**
6320** Programs contain a single instance of this opcode as the very first
6321** opcode.
drh949f9cd2008-01-12 21:35:57 +00006322**
6323** If tracing is enabled (by the sqlite3_trace()) interface, then
6324** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006325** Or if P4 is blank, use the string returned by sqlite3_sql().
6326**
6327** If P2 is not zero, jump to instruction P2.
drh949f9cd2008-01-12 21:35:57 +00006328*/
drhaceb31b2014-02-08 01:40:27 +00006329case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006330 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006331 char *z;
drh856c1032009-06-02 15:21:42 +00006332
drhaceb31b2014-02-08 01:40:27 +00006333 if( pOp->p2 ){
6334 pc = pOp->p2 - 1;
6335 }
6336#ifndef SQLITE_OMIT_TRACE
drh37f58e92012-09-04 21:34:26 +00006337 if( db->xTrace
6338 && !p->doingRerun
6339 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6340 ){
drhc3f1d5f2011-05-30 23:42:16 +00006341 z = sqlite3VdbeExpandSql(p, zTrace);
6342 db->xTrace(db->pTraceArg, z);
6343 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006344 }
drh8f8b2312013-10-18 20:03:43 +00006345#ifdef SQLITE_USE_FCNTL_TRACE
6346 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6347 if( zTrace ){
6348 int i;
6349 for(i=0; i<db->nDb; i++){
drha7ab6d82014-07-21 15:44:39 +00006350 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
drh8f8b2312013-10-18 20:03:43 +00006351 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6352 }
6353 }
6354#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006355#ifdef SQLITE_DEBUG
6356 if( (db->flags & SQLITE_SqlTrace)!=0
6357 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6358 ){
6359 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6360 }
6361#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00006362#endif /* SQLITE_OMIT_TRACE */
drh949f9cd2008-01-12 21:35:57 +00006363 break;
6364}
drh949f9cd2008-01-12 21:35:57 +00006365
drh91fd4d42008-01-19 20:11:25 +00006366
6367/* Opcode: Noop * * * * *
6368**
6369** Do nothing. This instruction is often useful as a jump
6370** destination.
drh5e00f6c2001-09-13 13:46:56 +00006371*/
drh91fd4d42008-01-19 20:11:25 +00006372/*
6373** The magic Explain opcode are only inserted when explain==2 (which
6374** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6375** This opcode records information from the optimizer. It is the
6376** the same as a no-op. This opcodesnever appears in a real VM program.
6377*/
6378default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006379 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006380 break;
6381}
6382
6383/*****************************************************************************
6384** The cases of the switch statement above this line should all be indented
6385** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6386** readability. From this point on down, the normal indentation rules are
6387** restored.
6388*****************************************************************************/
6389 }
drh6e142f52000-06-08 13:36:40 +00006390
drh7b396862003-01-01 23:06:20 +00006391#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006392 {
drha01c7c72014-04-25 12:35:31 +00006393 u64 endTime = sqlite3Hwtime();
6394 if( endTime>start ) pOp->cycles += endTime - start;
drh8178a752003-01-05 21:41:40 +00006395 pOp->cnt++;
drh8178a752003-01-05 21:41:40 +00006396 }
drh7b396862003-01-01 23:06:20 +00006397#endif
6398
drh6e142f52000-06-08 13:36:40 +00006399 /* The following code adds nothing to the actual functionality
6400 ** of the program. It is only here for testing and debugging.
6401 ** On the other hand, it does burn CPU cycles every time through
6402 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6403 */
6404#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006405 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006406
drhcf1023c2007-05-08 20:59:49 +00006407#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006408 if( db->flags & SQLITE_VdbeTrace ){
6409 if( rc!=0 ) printf("rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006410 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
drh84e55a82013-11-13 17:58:23 +00006411 registerTrace(pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006412 }
drh3c657212009-11-17 23:59:58 +00006413 if( pOp->opflags & OPFLG_OUT3 ){
drh84e55a82013-11-13 17:58:23 +00006414 registerTrace(pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006415 }
drh75897232000-05-29 14:26:00 +00006416 }
danielk1977b5402fb2005-01-12 07:15:04 +00006417#endif /* SQLITE_DEBUG */
6418#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006419 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006420
drha05a7222008-01-19 03:35:58 +00006421 /* If we reach this point, it means that execution is finished with
6422 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006423 */
drha05a7222008-01-19 03:35:58 +00006424vdbe_error_halt:
6425 assert( rc );
6426 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006427 testcase( sqlite3GlobalConfig.xLog!=0 );
6428 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6429 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006430 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006431 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6432 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006433 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006434 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006435 }
drh900b31e2007-08-28 02:27:51 +00006436
6437 /* This is the only way out of this procedure. We have to
6438 ** release the mutexes on btrees that were acquired at the
6439 ** top. */
6440vdbe_return:
drh99a66922011-05-13 18:51:42 +00006441 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006442 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006443 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006444 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006445 return rc;
6446
drh023ae032007-05-08 12:12:16 +00006447 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6448 ** is encountered.
6449 */
6450too_big:
drhf089aa42008-07-08 19:34:06 +00006451 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006452 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006453 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006454
drh98640a32007-06-07 19:08:32 +00006455 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006456 */
6457no_mem:
drh17435752007-08-16 04:30:38 +00006458 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006459 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006460 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006461 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006462
drhb86ccfb2003-01-28 23:13:10 +00006463 /* Jump to here for any other kind of fatal error. The "rc" variable
6464 ** should hold the error number.
6465 */
6466abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006467 assert( p->zErrMsg==0 );
6468 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006469 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006470 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006471 }
drha05a7222008-01-19 03:35:58 +00006472 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006473
danielk19776f8a5032004-05-10 10:34:51 +00006474 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006475 ** flag.
6476 */
6477abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006478 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006479 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006480 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006481 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006482 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006483}