blob: 22e9539437d8f1e570aa345030db13cbb5aba8f6 [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;
drhb53a5a92014-10-12 22:37:22 +0000217 pCx->aOffset = &pCx->aType[nField];
danielk1977cd3e8f72008-03-25 09:47:35 +0000218 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000219 pCx->pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000220 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhf25a5072009-11-18 23:01:25 +0000221 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000222 }
danielk197794eb6a12005-12-15 15:22:08 +0000223 }
drh4774b132004-06-12 20:12:51 +0000224 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000225}
226
danielk19773d1bfea2004-05-14 11:00:53 +0000227/*
drh29d72102006-02-09 22:13:41 +0000228** Try to convert a value into a numeric representation if we can
229** do so without loss of information. In other words, if the string
230** looks like a number, convert it into a number. If it does not
231** look like a number, leave it alone.
drhbd9507c2014-08-23 17:21:37 +0000232**
233** If the bTryForInt flag is true, then extra effort is made to give
234** an integer representation. Strings that look like floating point
235** values but which have no fractional component (example: '48.00')
236** will have a MEM_Int representation when bTryForInt is true.
237**
238** If bTryForInt is false, then if the input string contains a decimal
239** point or exponential notation, the result is only MEM_Real, even
240** if there is an exact integer representation of the quantity.
drh29d72102006-02-09 22:13:41 +0000241*/
drhbd9507c2014-08-23 17:21:37 +0000242static void applyNumericAffinity(Mem *pRec, int bTryForInt){
drh975b4c62014-07-26 16:47:23 +0000243 double rValue;
244 i64 iValue;
245 u8 enc = pRec->enc;
drh11a6eee2014-09-19 22:01:54 +0000246 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
drh975b4c62014-07-26 16:47:23 +0000247 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
248 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
249 pRec->u.i = iValue;
250 pRec->flags |= MEM_Int;
251 }else{
drh74eaba42014-09-18 17:52:15 +0000252 pRec->u.r = rValue;
drh975b4c62014-07-26 16:47:23 +0000253 pRec->flags |= MEM_Real;
drhbd9507c2014-08-23 17:21:37 +0000254 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000255 }
256}
257
258/*
drh8a512562005-11-14 22:29:05 +0000259** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000260**
drh8a512562005-11-14 22:29:05 +0000261** SQLITE_AFF_INTEGER:
262** SQLITE_AFF_REAL:
263** SQLITE_AFF_NUMERIC:
264** Try to convert pRec to an integer representation or a
265** floating-point representation if an integer representation
266** is not possible. Note that the integer representation is
267** always preferred, even if the affinity is REAL, because
268** an integer representation is more space efficient on disk.
269**
270** SQLITE_AFF_TEXT:
271** Convert pRec to a text representation.
272**
273** SQLITE_AFF_NONE:
274** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000275*/
drh17435752007-08-16 04:30:38 +0000276static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000277 Mem *pRec, /* The value to apply affinity to */
278 char affinity, /* The affinity to be applied */
279 u8 enc /* Use this text encoding */
280){
drh7ea31cc2014-09-18 14:36:00 +0000281 if( affinity>=SQLITE_AFF_NUMERIC ){
drh8a512562005-11-14 22:29:05 +0000282 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
283 || affinity==SQLITE_AFF_NUMERIC );
drhbd9507c2014-08-23 17:21:37 +0000284 if( (pRec->flags & MEM_Int)==0 ){
285 if( (pRec->flags & MEM_Real)==0 ){
drh11a6eee2014-09-19 22:01:54 +0000286 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1);
drhbd9507c2014-08-23 17:21:37 +0000287 }else{
288 sqlite3VdbeIntegerAffinity(pRec);
289 }
drh17c40292004-07-21 02:53:29 +0000290 }
drh7ea31cc2014-09-18 14:36:00 +0000291 }else if( affinity==SQLITE_AFF_TEXT ){
292 /* Only attempt the conversion to TEXT if there is an integer or real
293 ** representation (blob and NULL do not get converted) but no string
294 ** representation.
295 */
296 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
297 sqlite3VdbeMemStringify(pRec, enc, 1);
298 }
danielk19773d1bfea2004-05-14 11:00:53 +0000299 }
300}
301
danielk1977aee18ef2005-03-09 12:26:50 +0000302/*
drh29d72102006-02-09 22:13:41 +0000303** Try to convert the type of a function argument or a result column
304** into a numeric representation. Use either INTEGER or REAL whichever
305** is appropriate. But only do the conversion if it is possible without
306** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000307*/
308int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000309 int eType = sqlite3_value_type(pVal);
310 if( eType==SQLITE_TEXT ){
311 Mem *pMem = (Mem*)pVal;
drhbd9507c2014-08-23 17:21:37 +0000312 applyNumericAffinity(pMem, 0);
drh1b27b8c2014-02-10 03:21:57 +0000313 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000314 }
drh1b27b8c2014-02-10 03:21:57 +0000315 return eType;
drh29d72102006-02-09 22:13:41 +0000316}
317
318/*
danielk1977aee18ef2005-03-09 12:26:50 +0000319** Exported version of applyAffinity(). This one works on sqlite3_value*,
320** not the internal Mem* type.
321*/
danielk19771e536952007-08-16 10:09:01 +0000322void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000323 sqlite3_value *pVal,
324 u8 affinity,
325 u8 enc
326){
drhb21c8cd2007-08-21 19:33:56 +0000327 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000328}
329
drh3d1d90a2014-03-24 15:00:15 +0000330/*
drhf1a89ed2014-08-23 17:41:15 +0000331** pMem currently only holds a string type (or maybe a BLOB that we can
332** interpret as a string if we want to). Compute its corresponding
drh74eaba42014-09-18 17:52:15 +0000333** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields
drhf1a89ed2014-08-23 17:41:15 +0000334** accordingly.
335*/
336static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
337 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
338 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
drh74eaba42014-09-18 17:52:15 +0000339 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
drhf1a89ed2014-08-23 17:41:15 +0000340 return 0;
341 }
342 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){
343 return MEM_Int;
344 }
345 return MEM_Real;
346}
347
348/*
drh3d1d90a2014-03-24 15:00:15 +0000349** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
350** none.
351**
352** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
drh74eaba42014-09-18 17:52:15 +0000353** But it does set pMem->u.r and pMem->u.i appropriately.
drh3d1d90a2014-03-24 15:00:15 +0000354*/
355static u16 numericType(Mem *pMem){
356 if( pMem->flags & (MEM_Int|MEM_Real) ){
357 return pMem->flags & (MEM_Int|MEM_Real);
358 }
359 if( pMem->flags & (MEM_Str|MEM_Blob) ){
drhf1a89ed2014-08-23 17:41:15 +0000360 return computeNumericType(pMem);
drh3d1d90a2014-03-24 15:00:15 +0000361 }
362 return 0;
363}
364
danielk1977b5402fb2005-01-12 07:15:04 +0000365#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000366/*
danielk1977ca6b2912004-05-21 10:49:47 +0000367** Write a nice string representation of the contents of cell pMem
368** into buffer zBuf, length nBuf.
369*/
drh74161702006-02-24 02:53:49 +0000370void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000371 char *zCsr = zBuf;
372 int f = pMem->flags;
373
drh57196282004-10-06 15:41:16 +0000374 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000375
danielk1977ca6b2912004-05-21 10:49:47 +0000376 if( f&MEM_Blob ){
377 int i;
378 char c;
379 if( f & MEM_Dyn ){
380 c = 'z';
381 assert( (f & (MEM_Static|MEM_Ephem))==0 );
382 }else if( f & MEM_Static ){
383 c = 't';
384 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
385 }else if( f & MEM_Ephem ){
386 c = 'e';
387 assert( (f & (MEM_Static|MEM_Dyn))==0 );
388 }else{
389 c = 's';
390 }
391
drh5bb3eb92007-05-04 13:15:55 +0000392 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000393 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000394 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000395 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000396 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000397 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000398 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000399 }
400 for(i=0; i<16 && i<pMem->n; i++){
401 char z = pMem->z[i];
402 if( z<32 || z>126 ) *zCsr++ = '.';
403 else *zCsr++ = z;
404 }
405
drhe718efe2007-05-10 21:14:03 +0000406 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000407 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000408 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000409 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000410 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000411 }
danielk1977b1bc9532004-05-22 03:05:33 +0000412 *zCsr = '\0';
413 }else if( f & MEM_Str ){
414 int j, k;
415 zBuf[0] = ' ';
416 if( f & MEM_Dyn ){
417 zBuf[1] = 'z';
418 assert( (f & (MEM_Static|MEM_Ephem))==0 );
419 }else if( f & MEM_Static ){
420 zBuf[1] = 't';
421 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
422 }else if( f & MEM_Ephem ){
423 zBuf[1] = 'e';
424 assert( (f & (MEM_Static|MEM_Dyn))==0 );
425 }else{
426 zBuf[1] = 's';
427 }
428 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000429 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000430 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000431 zBuf[k++] = '[';
432 for(j=0; j<15 && j<pMem->n; j++){
433 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000434 if( c>=0x20 && c<0x7f ){
435 zBuf[k++] = c;
436 }else{
437 zBuf[k++] = '.';
438 }
439 }
440 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000441 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000442 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000443 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000444 }
danielk1977ca6b2912004-05-21 10:49:47 +0000445}
446#endif
447
drh5b6afba2008-01-05 16:29:28 +0000448#ifdef SQLITE_DEBUG
449/*
450** Print the value of a register for tracing purposes:
451*/
drh84e55a82013-11-13 17:58:23 +0000452static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000453 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000454 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000455 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000456 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000457 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000458 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000459 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000460 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000461#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000462 }else if( p->flags & MEM_Real ){
drh74eaba42014-09-18 17:52:15 +0000463 printf(" r:%g", p->u.r);
drh0b3bf922009-06-15 20:45:34 +0000464#endif
drh733bf1b2009-04-22 00:47:00 +0000465 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000466 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000467 }else{
468 char zBuf[200];
469 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000470 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000471 }
472}
drh84e55a82013-11-13 17:58:23 +0000473static void registerTrace(int iReg, Mem *p){
474 printf("REG[%d] = ", iReg);
475 memTracePrint(p);
476 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000477}
478#endif
479
480#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000481# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000482#else
483# define REGISTER_TRACE(R,M)
484#endif
485
danielk197784ac9d02004-05-18 09:58:06 +0000486
drh7b396862003-01-01 23:06:20 +0000487#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000488
489/*
490** hwtime.h contains inline assembler code for implementing
491** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000492*/
shane9bcbdad2008-05-29 20:22:37 +0000493#include "hwtime.h"
494
drh7b396862003-01-01 23:06:20 +0000495#endif
496
danielk1977fd7f0452008-12-17 17:30:26 +0000497#ifndef NDEBUG
498/*
499** This function is only called from within an assert() expression. It
500** checks that the sqlite3.nTransaction variable is correctly set to
501** the number of non-transaction savepoints currently in the
502** linked list starting at sqlite3.pSavepoint.
503**
504** Usage:
505**
506** assert( checkSavepointCount(db) );
507*/
508static int checkSavepointCount(sqlite3 *db){
509 int n = 0;
510 Savepoint *p;
511 for(p=db->pSavepoint; p; p=p->pNext) n++;
512 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
513 return 1;
514}
515#endif
516
drhb9755982010-07-24 16:34:37 +0000517
518/*
drh0fd61352014-02-07 02:29:45 +0000519** Execute as much of a VDBE program as we can.
520** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000521*/
danielk19774adee202004-05-08 08:23:19 +0000522int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000523 Vdbe *p /* The VDBE */
524){
shaneh84f4b2f2010-02-26 01:46:54 +0000525 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000526 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000527 Op *pOp; /* Current operation */
528 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000529 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000530 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000531 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000532 int iCompare = 0; /* Result of last OP_Compare operation */
533 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000534#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000535 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000536#endif
drha6c2ed92009-11-14 23:22:23 +0000537 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000538 Mem *pIn1 = 0; /* 1st input operand */
539 Mem *pIn2 = 0; /* 2nd input operand */
540 Mem *pIn3 = 0; /* 3rd input operand */
541 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000542 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000543 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000544#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000545 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000546#endif
drh856c1032009-06-02 15:21:42 +0000547 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000548
drhca48c902008-01-18 14:08:24 +0000549 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000550 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000551 if( p->rc==SQLITE_NOMEM ){
552 /* This happens if a malloc() inside a call to sqlite3_column_text() or
553 ** sqlite3_column_text16() failed. */
554 goto no_mem;
555 }
drh3a840692003-01-29 22:58:26 +0000556 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000557 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000558 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000559 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000560 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000561 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000562 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000563 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000564 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000565#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
566 if( db->xProgress ){
567 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000568 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000569 if( nProgressLimit==0 ){
570 nProgressLimit = db->nProgressOps;
571 }else{
572 nProgressLimit %= (unsigned)db->nProgressOps;
573 }
574 }
575#endif
drh3c23a882007-01-09 14:01:13 +0000576#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000577 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000578 if( p->pc==0
579 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
580 ){
drh3c23a882007-01-09 14:01:13 +0000581 int i;
drh84e55a82013-11-13 17:58:23 +0000582 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000583 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000584 if( p->db->flags & SQLITE_VdbeListing ){
585 printf("VDBE Program Listing:\n");
586 for(i=0; i<p->nOp; i++){
587 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
588 }
drh3c23a882007-01-09 14:01:13 +0000589 }
drh84e55a82013-11-13 17:58:23 +0000590 if( p->db->flags & SQLITE_VdbeEQP ){
591 for(i=0; i<p->nOp; i++){
592 if( aOp[i].opcode==OP_Explain ){
593 if( once ) printf("VDBE Query Plan:\n");
594 printf("%s\n", aOp[i].p4.z);
595 once = 0;
596 }
597 }
598 }
599 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000600 }
danielk19772d1d86f2008-06-20 14:59:51 +0000601 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000602#endif
drhb86ccfb2003-01-28 23:13:10 +0000603 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000604 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000605 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000606#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000607 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000608#endif
danf533dbe2014-10-23 17:26:22 +0000609#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_LOOPCOUNTERS)
610 if( p->pFrame==0 ) p->anExec[pc]++;
611#endif
drhbf159fa2013-06-25 22:01:22 +0000612 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000613 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000614
danielk19778b60e0f2005-01-12 09:10:39 +0000615 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000616 */
danielk19778b60e0f2005-01-12 09:10:39 +0000617#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000618 if( db->flags & SQLITE_VdbeTrace ){
619 sqlite3VdbePrintOp(stdout, pc, pOp);
drh75897232000-05-29 14:26:00 +0000620 }
drh3f7d4e42004-07-24 14:35:58 +0000621#endif
622
drh6e142f52000-06-08 13:36:40 +0000623
drhf6038712004-02-08 18:07:34 +0000624 /* Check to see if we need to simulate an interrupt. This only happens
625 ** if we have a special test build.
626 */
627#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000628 if( sqlite3_interrupt_count>0 ){
629 sqlite3_interrupt_count--;
630 if( sqlite3_interrupt_count==0 ){
631 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000632 }
633 }
634#endif
635
drhb5b407e2012-08-29 10:28:43 +0000636 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000637 ** external allocations out of mem[p2] and set mem[p2] to be
638 ** an undefined integer. Opcodes will either fill in the integer
639 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000640 */
drha6c2ed92009-11-14 23:22:23 +0000641 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000642 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
643 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000644 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000645 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000646 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +0000647 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut);
drh3c657212009-11-17 23:59:58 +0000648 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000649 }
drh3c657212009-11-17 23:59:58 +0000650
651 /* Sanity checking on other operands */
652#ifdef SQLITE_DEBUG
653 if( (pOp->opflags & OPFLG_IN1)!=0 ){
654 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000655 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000656 assert( memIsValid(&aMem[pOp->p1]) );
drh75fd0542014-03-01 16:24:44 +0000657 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000658 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
659 }
660 if( (pOp->opflags & OPFLG_IN2)!=0 ){
661 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000662 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000663 assert( memIsValid(&aMem[pOp->p2]) );
drh75fd0542014-03-01 16:24:44 +0000664 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000665 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
666 }
667 if( (pOp->opflags & OPFLG_IN3)!=0 ){
668 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000669 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000670 assert( memIsValid(&aMem[pOp->p3]) );
drh75fd0542014-03-01 16:24:44 +0000671 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000672 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
673 }
674 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
675 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000676 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000677 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000678 }
679 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
680 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000681 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000682 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000683 }
684#endif
drh93952eb2009-11-13 19:43:43 +0000685
drh75897232000-05-29 14:26:00 +0000686 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000687
drh5e00f6c2001-09-13 13:46:56 +0000688/*****************************************************************************
689** What follows is a massive switch statement where each case implements a
690** separate instruction in the virtual machine. If we follow the usual
691** indentation conventions, each case should be indented by 6 spaces. But
692** that is a lot of wasted space on the left margin. So the code within
693** the switch statement will break with convention and be flush-left. Another
694** big comment (similar to this one) will mark the point in the code where
695** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000696**
697** The formatting of each case is important. The makefile for SQLite
698** generates two C files "opcodes.h" and "opcodes.c" by scanning this
699** file looking for lines that begin with "case OP_". The opcodes.h files
700** will be filled with #defines that give unique integer values to each
701** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000702** each string is the symbolic name for the corresponding opcode. If the
703** case statement is followed by a comment of the form "/# same as ... #/"
704** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000705**
drh9cbf3422008-01-17 16:22:13 +0000706** Other keywords in the comment that follows each case are used to
707** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
708** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
709** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000710**
drhac82fcf2002-09-08 17:23:41 +0000711** Documentation about VDBE opcodes is generated by scanning this file
712** for lines of that contain "Opcode:". That line and all subsequent
713** comment lines are used in the generation of the opcode.html documentation
714** file.
715**
716** SUMMARY:
717**
718** Formatting is important to scripts that scan this file.
719** Do not deviate from the formatting style currently in use.
720**
drh5e00f6c2001-09-13 13:46:56 +0000721*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000722
drh9cbf3422008-01-17 16:22:13 +0000723/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000724**
725** An unconditional jump to address P2.
726** The next instruction executed will be
727** the one at index P2 from the beginning of
728** the program.
drhfe705102014-03-06 13:38:37 +0000729**
730** The P1 parameter is not actually used by this opcode. However, it
731** is sometimes set to 1 instead of 0 as a hint to the command-line shell
732** that this Goto is the bottom of a loop and that the lines from P2 down
733** to the current line should be indented for EXPLAIN output.
drh5e00f6c2001-09-13 13:46:56 +0000734*/
drh9cbf3422008-01-17 16:22:13 +0000735case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000736 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000737
738 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
739 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
740 ** completion. Check to see if sqlite3_interrupt() has been called
741 ** or if the progress callback needs to be invoked.
742 **
743 ** This code uses unstructured "goto" statements and does not look clean.
744 ** But that is not due to sloppy coding habits. The code is written this
745 ** way for performance, to avoid having to run the interrupt and progress
746 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
747 ** faster according to "valgrind --tool=cachegrind" */
748check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000749 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000750#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
751 /* Call the progress callback if it is configured and the required number
752 ** of VDBE ops have been executed (either since this invocation of
753 ** sqlite3VdbeExec() or since last time the progress callback was called).
754 ** If the progress callback returns non-zero, exit the virtual machine with
755 ** a return code SQLITE_ABORT.
756 */
drh0d1961e2013-07-25 16:27:51 +0000757 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000758 assert( db->nProgressOps!=0 );
759 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
760 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000761 rc = SQLITE_INTERRUPT;
762 goto vdbe_error_halt;
763 }
drh49afe3a2013-07-10 03:05:14 +0000764 }
765#endif
766
drh5e00f6c2001-09-13 13:46:56 +0000767 break;
768}
drh75897232000-05-29 14:26:00 +0000769
drh2eb95372008-06-06 15:04:36 +0000770/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000771**
drh2eb95372008-06-06 15:04:36 +0000772** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000773** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000774*/
drhb8475df2011-12-09 16:21:19 +0000775case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000776 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000777 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000778 assert( VdbeMemDynamic(pIn1)==0 );
drh2b4ded92010-09-27 21:09:31 +0000779 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000780 pIn1->flags = MEM_Int;
781 pIn1->u.i = pc;
782 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000783 pc = pOp->p2 - 1;
784 break;
785}
786
drh2eb95372008-06-06 15:04:36 +0000787/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000788**
drh81cf13e2014-02-07 18:27:53 +0000789** Jump to the next instruction after the address in register P1. After
790** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000791*/
drh2eb95372008-06-06 15:04:36 +0000792case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000793 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000794 assert( pIn1->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000795 pc = (int)pIn1->u.i;
drh81cf13e2014-02-07 18:27:53 +0000796 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000797 break;
798}
799
drhed71a832014-02-07 19:18:10 +0000800/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000801**
drh5dad9a32014-07-25 18:37:42 +0000802** Set up register P1 so that it will Yield to the coroutine
drhed71a832014-02-07 19:18:10 +0000803** located at address P3.
804**
drh5dad9a32014-07-25 18:37:42 +0000805** If P2!=0 then the coroutine implementation immediately follows
806** this opcode. So jump over the coroutine implementation to
drhed71a832014-02-07 19:18:10 +0000807** address P2.
drh5dad9a32014-07-25 18:37:42 +0000808**
809** See also: EndCoroutine
drh81cf13e2014-02-07 18:27:53 +0000810*/
811case OP_InitCoroutine: { /* jump */
drhed71a832014-02-07 19:18:10 +0000812 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
813 assert( pOp->p2>=0 && pOp->p2<p->nOp );
814 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000815 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000816 assert( !VdbeMemDynamic(pOut) );
817 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000818 pOut->flags = MEM_Int;
drhed71a832014-02-07 19:18:10 +0000819 if( pOp->p2 ) pc = pOp->p2 - 1;
drh81cf13e2014-02-07 18:27:53 +0000820 break;
821}
822
823/* Opcode: EndCoroutine P1 * * * *
824**
drhbc5cf382014-08-06 01:08:07 +0000825** The instruction at the address in register P1 is a Yield.
drh5dad9a32014-07-25 18:37:42 +0000826** Jump to the P2 parameter of that Yield.
drh81cf13e2014-02-07 18:27:53 +0000827** After the jump, register P1 becomes undefined.
drh5dad9a32014-07-25 18:37:42 +0000828**
829** See also: InitCoroutine
drh81cf13e2014-02-07 18:27:53 +0000830*/
831case OP_EndCoroutine: { /* in1 */
832 VdbeOp *pCaller;
833 pIn1 = &aMem[pOp->p1];
834 assert( pIn1->flags==MEM_Int );
835 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
836 pCaller = &aOp[pIn1->u.i];
837 assert( pCaller->opcode==OP_Yield );
838 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
839 pc = pCaller->p2 - 1;
840 pIn1->flags = MEM_Undefined;
841 break;
842}
843
844/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000845**
drh5dad9a32014-07-25 18:37:42 +0000846** Swap the program counter with the value in register P1. This
847** has the effect of yielding to a coroutine.
drh81cf13e2014-02-07 18:27:53 +0000848**
drh5dad9a32014-07-25 18:37:42 +0000849** If the coroutine that is launched by this instruction ends with
850** Yield or Return then continue to the next instruction. But if
851** the coroutine launched by this instruction ends with
852** EndCoroutine, then jump to P2 rather than continuing with the
853** next instruction.
854**
855** See also: InitCoroutine
drhe00ee6e2008-06-20 15:24:01 +0000856*/
drh81cf13e2014-02-07 18:27:53 +0000857case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000858 int pcDest;
drh3c657212009-11-17 23:59:58 +0000859 pIn1 = &aMem[pOp->p1];
drhc91b2fd2014-03-01 18:13:23 +0000860 assert( VdbeMemDynamic(pIn1)==0 );
drhe00ee6e2008-06-20 15:24:01 +0000861 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000862 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000863 pIn1->u.i = pc;
864 REGISTER_TRACE(pOp->p1, pIn1);
865 pc = pcDest;
866 break;
867}
868
drhf9c8ce32013-11-05 13:33:55 +0000869/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh0fd61352014-02-07 02:29:45 +0000870** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000871**
drhef8662b2011-06-20 21:47:58 +0000872** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000873** parameter P1, P2, and P4 as if this were a Halt instruction. If the
874** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000875** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000876*/
877case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000878 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000879 if( (pIn3->flags & MEM_Null)==0 ) break;
880 /* Fall through into OP_Halt */
881}
drhe00ee6e2008-06-20 15:24:01 +0000882
drhf9c8ce32013-11-05 13:33:55 +0000883/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000884**
drh3d4501e2008-12-04 20:40:10 +0000885** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000886** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000887**
drh92f02c32004-09-02 14:57:08 +0000888** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
889** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
890** For errors, it can be some other value. If P1!=0 then P2 will determine
891** whether or not to rollback the current transaction. Do not rollback
892** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
893** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000894** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000895**
drh66a51672008-01-03 00:01:23 +0000896** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000897**
drhf9c8ce32013-11-05 13:33:55 +0000898** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
899**
900** 0: (no change)
901** 1: NOT NULL contraint failed: P4
902** 2: UNIQUE constraint failed: P4
903** 3: CHECK constraint failed: P4
904** 4: FOREIGN KEY constraint failed: P4
905**
906** If P5 is not zero and P4 is NULL, then everything after the ":" is
907** omitted.
908**
drh9cfcf5d2002-01-29 18:41:24 +0000909** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000910** every program. So a jump past the last instruction of the program
911** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000912*/
drh9cbf3422008-01-17 16:22:13 +0000913case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000914 const char *zType;
915 const char *zLogFmt;
916
dan165921a2009-08-28 18:53:45 +0000917 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000918 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000919 VdbeFrame *pFrame = p->pFrame;
920 p->pFrame = pFrame->pParent;
921 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000922 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000923 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000924 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000925 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000926 /* Instruction pc is the OP_Program that invoked the sub-program
927 ** currently being halted. If the p2 instruction of this OP_Halt
928 ** instruction is set to OE_Ignore, then the sub-program is throwing
929 ** an IGNORE exception. In this case jump to the address specified
930 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000931 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000932 }
drhbbe879d2009-11-14 18:04:35 +0000933 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000934 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000935 break;
936 }
drh92f02c32004-09-02 14:57:08 +0000937 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000938 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000939 p->pc = pc;
drhf9c8ce32013-11-05 13:33:55 +0000940 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000941 if( pOp->p5 ){
942 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
943 "FOREIGN KEY" };
944 assert( pOp->p5>=1 && pOp->p5<=4 );
945 testcase( pOp->p5==1 );
946 testcase( pOp->p5==2 );
947 testcase( pOp->p5==3 );
948 testcase( pOp->p5==4 );
949 zType = azType[pOp->p5-1];
950 }else{
951 zType = 0;
952 }
drh4308e342013-11-11 16:55:52 +0000953 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000954 zLogFmt = "abort at %d in [%s]: %s";
955 if( zType && pOp->p4.z ){
956 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
957 zType, pOp->p4.z);
958 }else if( pOp->p4.z ){
959 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000960 }else{
drh4308e342013-11-11 16:55:52 +0000961 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000962 }
963 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000964 }
drh92f02c32004-09-02 14:57:08 +0000965 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000966 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000967 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000968 p->rc = rc = SQLITE_BUSY;
969 }else{
drhd91c1a12013-02-09 13:58:25 +0000970 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000971 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000972 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000973 }
drh900b31e2007-08-28 02:27:51 +0000974 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000975}
drhc61053b2000-06-04 12:58:36 +0000976
drh4c583122008-01-04 22:01:03 +0000977/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000978** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000979**
drh9cbf3422008-01-17 16:22:13 +0000980** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000981*/
drh4c583122008-01-04 22:01:03 +0000982case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000983 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000984 break;
985}
986
drh4c583122008-01-04 22:01:03 +0000987/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000988** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +0000989**
drh66a51672008-01-03 00:01:23 +0000990** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000991** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000992*/
drh4c583122008-01-04 22:01:03 +0000993case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000994 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000995 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000996 break;
997}
drh4f26d6c2004-05-26 23:25:30 +0000998
drh13573c72010-01-12 17:04:07 +0000999#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +00001000/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001001** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +00001002**
drh4c583122008-01-04 22:01:03 +00001003** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +00001004** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +00001005*/
drh4c583122008-01-04 22:01:03 +00001006case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
1007 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +00001008 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh74eaba42014-09-18 17:52:15 +00001009 pOut->u.r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +00001010 break;
1011}
drh13573c72010-01-12 17:04:07 +00001012#endif
danielk1977cbb18d22004-05-28 11:37:27 +00001013
drh3c84ddf2008-01-09 02:15:38 +00001014/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001015** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +00001016**
drh66a51672008-01-03 00:01:23 +00001017** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drhbc5cf382014-08-06 01:08:07 +00001018** into a String before it is executed for the first time. During
drh0fd61352014-02-07 02:29:45 +00001019** this transformation, the length of string P4 is computed and stored
1020** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +00001021*/
drh4c583122008-01-04 22:01:03 +00001022case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +00001023 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +00001024 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +00001025 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +00001026
1027#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +00001028 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +00001029 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
1030 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +00001031 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh17bcb102014-09-18 21:25:33 +00001032 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z );
drhc91b2fd2014-03-01 18:13:23 +00001033 assert( VdbeMemDynamic(pOut)==0 );
drh17bcb102014-09-18 21:25:33 +00001034 pOut->szMalloc = 0;
drh4c583122008-01-04 22:01:03 +00001035 pOut->flags |= MEM_Static;
drh66a51672008-01-03 00:01:23 +00001036 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +00001037 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +00001038 }
drh66a51672008-01-03 00:01:23 +00001039 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +00001040 pOp->p4.z = pOut->z;
1041 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +00001042 }
danielk197793758c82005-01-21 08:13:14 +00001043#endif
drhbb4957f2008-03-20 14:03:29 +00001044 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +00001045 goto too_big;
1046 }
1047 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +00001048}
drhf4479502004-05-27 03:12:53 +00001049
drh4c583122008-01-04 22:01:03 +00001050/* Opcode: String P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001051** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +00001052**
drh9cbf3422008-01-17 16:22:13 +00001053** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +00001054*/
drh4c583122008-01-04 22:01:03 +00001055case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +00001056 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +00001057 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
1058 pOut->z = pOp->p4.z;
1059 pOut->n = pOp->p1;
1060 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001061 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +00001062 break;
1063}
1064
drh053a1282012-09-19 21:15:46 +00001065/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001066** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001067**
drhb8475df2011-12-09 16:21:19 +00001068** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001069** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001070** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001071** set to NULL.
1072**
1073** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1074** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1075** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001076*/
drh4c583122008-01-04 22:01:03 +00001077case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +00001078 int cnt;
drh053a1282012-09-19 21:15:46 +00001079 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +00001080 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001081 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001082 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +00001083 while( cnt>0 ){
1084 pOut++;
1085 memAboutToChange(p, pOut);
drh0725cab2014-09-17 14:52:46 +00001086 sqlite3VdbeMemSetNull(pOut);
drh053a1282012-09-19 21:15:46 +00001087 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001088 cnt--;
1089 }
drhf0863fe2005-06-12 21:35:51 +00001090 break;
1091}
1092
drh05a86c52014-02-16 01:55:49 +00001093/* Opcode: SoftNull P1 * * * *
1094** Synopsis: r[P1]=NULL
1095**
1096** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1097** instruction, but do not free any string or blob memory associated with
1098** the register, so that if the value was a string or blob that was
1099** previously copied using OP_SCopy, the copies will continue to be valid.
1100*/
1101case OP_SoftNull: {
1102 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
1103 pOut = &aMem[pOp->p1];
1104 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1105 break;
1106}
drhf0863fe2005-06-12 21:35:51 +00001107
drha5750cf2014-02-07 13:20:31 +00001108/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001109** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001110**
drh9de221d2008-01-05 06:51:30 +00001111** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001112** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001113*/
drh4c583122008-01-04 22:01:03 +00001114case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001115 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001116 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001117 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001118 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001119 break;
1120}
1121
drheaf52d82010-05-12 13:50:23 +00001122/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001123** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001124**
drheaf52d82010-05-12 13:50:23 +00001125** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001126**
drh0fd61352014-02-07 02:29:45 +00001127** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001128** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001129*/
drheaf52d82010-05-12 13:50:23 +00001130case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001131 Mem *pVar; /* Value being transferred */
1132
drheaf52d82010-05-12 13:50:23 +00001133 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001134 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001135 pVar = &p->aVar[pOp->p1 - 1];
1136 if( sqlite3VdbeMemTooBig(pVar) ){
1137 goto too_big;
drh023ae032007-05-08 12:12:16 +00001138 }
drheaf52d82010-05-12 13:50:23 +00001139 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1140 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001141 break;
1142}
danielk1977295ba552004-05-19 10:34:51 +00001143
drhb21e7c72008-06-22 12:37:57 +00001144/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001145** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001146**
drh079a3072014-03-19 14:10:55 +00001147** Move the P3 values in register P1..P1+P3-1 over into
1148** registers P2..P2+P3-1. Registers P1..P1+P3-1 are
drhb21e7c72008-06-22 12:37:57 +00001149** left holding a NULL. It is an error for register ranges
drh079a3072014-03-19 14:10:55 +00001150** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error
1151** for P3 to be less than 1.
drh5e00f6c2001-09-13 13:46:56 +00001152*/
drhe1349cb2008-04-01 00:36:10 +00001153case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001154 int n; /* Number of registers left to copy */
1155 int p1; /* Register to copy from */
1156 int p2; /* Register to copy to */
1157
drhe09f43f2013-11-21 04:18:31 +00001158 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001159 p1 = pOp->p1;
1160 p2 = pOp->p2;
drh079a3072014-03-19 14:10:55 +00001161 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001162 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001163
drha6c2ed92009-11-14 23:22:23 +00001164 pIn1 = &aMem[p1];
1165 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001166 do{
dan3bc9f742013-08-15 16:18:39 +00001167 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1168 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001169 assert( memIsValid(pIn1) );
1170 memAboutToChange(p, pOut);
drh17bcb102014-09-18 21:25:33 +00001171 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001172#ifdef SQLITE_DEBUG
1173 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1174 pOut->pScopyFrom += p1 - pOp->p2;
1175 }
1176#endif
drhb21e7c72008-06-22 12:37:57 +00001177 REGISTER_TRACE(p2++, pOut);
1178 pIn1++;
1179 pOut++;
drh079a3072014-03-19 14:10:55 +00001180 }while( --n );
drhe1349cb2008-04-01 00:36:10 +00001181 break;
1182}
1183
drhe8e4af72012-09-21 00:04:28 +00001184/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001185** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001186**
drhe8e4af72012-09-21 00:04:28 +00001187** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001188**
1189** This instruction makes a deep copy of the value. A duplicate
1190** is made of any string or blob constant. See also OP_SCopy.
1191*/
drhe8e4af72012-09-21 00:04:28 +00001192case OP_Copy: {
1193 int n;
1194
1195 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001196 pIn1 = &aMem[pOp->p1];
1197 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001198 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001199 while( 1 ){
1200 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1201 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001202#ifdef SQLITE_DEBUG
1203 pOut->pScopyFrom = 0;
1204#endif
drhe8e4af72012-09-21 00:04:28 +00001205 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1206 if( (n--)==0 ) break;
1207 pOut++;
1208 pIn1++;
1209 }
drhe1349cb2008-04-01 00:36:10 +00001210 break;
1211}
1212
drhb1fdb2a2008-01-05 04:06:03 +00001213/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001214** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001215**
drh9cbf3422008-01-17 16:22:13 +00001216** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001217**
1218** This instruction makes a shallow copy of the value. If the value
1219** is a string or blob, then the copy is only a pointer to the
1220** original and hence if the original changes so will the copy.
1221** Worse, if the original is deallocated, the copy becomes invalid.
1222** Thus the program must guarantee that the original will not change
1223** during the lifetime of the copy. Use OP_Copy to make a complete
1224** copy.
1225*/
drh26198bb2013-10-31 11:15:09 +00001226case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001227 pIn1 = &aMem[pOp->p1];
1228 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001229 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001230 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001231#ifdef SQLITE_DEBUG
1232 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1233#endif
drh5e00f6c2001-09-13 13:46:56 +00001234 break;
1235}
drh75897232000-05-29 14:26:00 +00001236
drh9cbf3422008-01-17 16:22:13 +00001237/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001238** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001239**
shane21e7feb2008-05-30 15:59:49 +00001240** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001241** results. This opcode causes the sqlite3_step() call to terminate
1242** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh4d87aae2014-02-20 19:42:00 +00001243** structure to provide access to the r(P1)..r(P1+P2-1) values as
drh0fd61352014-02-07 02:29:45 +00001244** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001245*/
drh9cbf3422008-01-17 16:22:13 +00001246case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001247 Mem *pMem;
1248 int i;
1249 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001250 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001251 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001252
drhe6400b92013-11-13 23:48:46 +00001253#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1254 /* Run the progress counter just before returning.
1255 */
1256 if( db->xProgress!=0
1257 && nVmStep>=nProgressLimit
1258 && db->xProgress(db->pProgressArg)!=0
1259 ){
1260 rc = SQLITE_INTERRUPT;
1261 goto vdbe_error_halt;
1262 }
1263#endif
1264
dan32b09f22009-09-23 17:29:59 +00001265 /* If this statement has violated immediate foreign key constraints, do
1266 ** not return the number of rows modified. And do not RELEASE the statement
1267 ** transaction. It needs to be rolled back. */
1268 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1269 assert( db->flags&SQLITE_CountRows );
1270 assert( p->usesStmtJournal );
1271 break;
1272 }
1273
danielk1977bd434552009-03-18 10:33:00 +00001274 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1275 ** DML statements invoke this opcode to return the number of rows
1276 ** modified to the user. This is the only way that a VM that
1277 ** opens a statement transaction may invoke this opcode.
1278 **
1279 ** In case this is such a statement, close any statement transaction
1280 ** opened by this VM before returning control to the user. This is to
1281 ** ensure that statement-transactions are always nested, not overlapping.
1282 ** If the open statement-transaction is not closed here, then the user
1283 ** may step another VM that opens its own statement transaction. This
1284 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001285 **
1286 ** The statement transaction is never a top-level transaction. Hence
1287 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001288 */
1289 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001290 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1291 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001292 break;
1293 }
1294
drhd4e70eb2008-01-02 00:34:36 +00001295 /* Invalidate all ephemeral cursor row caches */
1296 p->cacheCtr = (p->cacheCtr + 2)|1;
1297
1298 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001299 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001300 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001301 */
drha6c2ed92009-11-14 23:22:23 +00001302 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001303 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001304 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001305 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001306 assert( (pMem[i].flags & MEM_Ephem)==0
1307 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001308 sqlite3VdbeMemNulTerminate(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001309 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001310 }
drh28039692008-03-17 16:54:01 +00001311 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001312
1313 /* Return SQLITE_ROW
1314 */
drhd4e70eb2008-01-02 00:34:36 +00001315 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001316 rc = SQLITE_ROW;
1317 goto vdbe_return;
1318}
1319
drh5b6afba2008-01-05 16:29:28 +00001320/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001321** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001322**
drh5b6afba2008-01-05 16:29:28 +00001323** Add the text in register P1 onto the end of the text in
1324** register P2 and store the result in register P3.
1325** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001326**
1327** P3 = P2 || P1
1328**
1329** It is illegal for P1 and P3 to be the same register. Sometimes,
1330** if P3 is the same register as P2, the implementation is able
1331** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001332*/
drh5b6afba2008-01-05 16:29:28 +00001333case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001334 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001335
drh3c657212009-11-17 23:59:58 +00001336 pIn1 = &aMem[pOp->p1];
1337 pIn2 = &aMem[pOp->p2];
1338 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001339 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001340 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001341 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001342 break;
drh5e00f6c2001-09-13 13:46:56 +00001343 }
drha0c06522009-06-17 22:50:41 +00001344 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001345 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001346 Stringify(pIn2, encoding);
1347 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001348 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001349 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001350 }
drh9c1905f2008-12-10 22:32:56 +00001351 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001352 goto no_mem;
1353 }
drhc91b2fd2014-03-01 18:13:23 +00001354 MemSetTypeFlag(pOut, MEM_Str);
danielk1977a7a8e142008-02-13 18:25:27 +00001355 if( pOut!=pIn2 ){
1356 memcpy(pOut->z, pIn2->z, pIn2->n);
1357 }
1358 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001359 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001360 pOut->z[nByte+1] = 0;
1361 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001362 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001363 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001364 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001365 break;
1366}
drh75897232000-05-29 14:26:00 +00001367
drh3c84ddf2008-01-09 02:15:38 +00001368/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001369** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001370**
drh60a713c2008-01-21 16:22:45 +00001371** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001372** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001373** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001374*/
drh3c84ddf2008-01-09 02:15:38 +00001375/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001376** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001377**
drh3c84ddf2008-01-09 02:15:38 +00001378**
shane21e7feb2008-05-30 15:59:49 +00001379** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001380** and store the result in register P3.
1381** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001382*/
drh3c84ddf2008-01-09 02:15:38 +00001383/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001384** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001385**
drh60a713c2008-01-21 16:22:45 +00001386** Subtract the value in register P1 from the value in register P2
1387** and store the result in register P3.
1388** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001389*/
drh9cbf3422008-01-17 16:22:13 +00001390/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001391** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001392**
drh60a713c2008-01-21 16:22:45 +00001393** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001394** and store the result in register P3 (P3=P2/P1). If the value in
1395** register P1 is zero, then the result is NULL. If either input is
1396** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001397*/
drh9cbf3422008-01-17 16:22:13 +00001398/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001399** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001400**
drh40864a12013-11-15 18:58:37 +00001401** Compute the remainder after integer register P2 is divided by
1402** register P1 and store the result in register P3.
1403** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001404** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001405*/
drh5b6afba2008-01-05 16:29:28 +00001406case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1407case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1408case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1409case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1410case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001411 char bIntint; /* Started out as two integer operands */
drh3d1d90a2014-03-24 15:00:15 +00001412 u16 flags; /* Combined MEM_* flags from both inputs */
1413 u16 type1; /* Numeric type of left operand */
1414 u16 type2; /* Numeric type of right operand */
drh856c1032009-06-02 15:21:42 +00001415 i64 iA; /* Integer value of left operand */
1416 i64 iB; /* Integer value of right operand */
1417 double rA; /* Real value of left operand */
1418 double rB; /* Real value of right operand */
1419
drh3c657212009-11-17 23:59:58 +00001420 pIn1 = &aMem[pOp->p1];
drh3d1d90a2014-03-24 15:00:15 +00001421 type1 = numericType(pIn1);
drh3c657212009-11-17 23:59:58 +00001422 pIn2 = &aMem[pOp->p2];
drh3d1d90a2014-03-24 15:00:15 +00001423 type2 = numericType(pIn2);
drh3c657212009-11-17 23:59:58 +00001424 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001425 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001426 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
drh3d1d90a2014-03-24 15:00:15 +00001427 if( (type1 & type2 & MEM_Int)!=0 ){
drh856c1032009-06-02 15:21:42 +00001428 iA = pIn1->u.i;
1429 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001430 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001431 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001432 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1433 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1434 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001435 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001436 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001437 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001438 iB /= iA;
drh75897232000-05-29 14:26:00 +00001439 break;
1440 }
drhbf4133c2001-10-13 02:59:08 +00001441 default: {
drh856c1032009-06-02 15:21:42 +00001442 if( iA==0 ) goto arithmetic_result_is_null;
1443 if( iA==-1 ) iA = 1;
1444 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001445 break;
1446 }
drh75897232000-05-29 14:26:00 +00001447 }
drh856c1032009-06-02 15:21:42 +00001448 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001449 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001450 }else{
drhbe707b32012-12-10 22:19:14 +00001451 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001452fp_math:
drh856c1032009-06-02 15:21:42 +00001453 rA = sqlite3VdbeRealValue(pIn1);
1454 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001455 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001456 case OP_Add: rB += rA; break;
1457 case OP_Subtract: rB -= rA; break;
1458 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001459 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001460 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001461 if( rA==(double)0 ) goto arithmetic_result_is_null;
1462 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001463 break;
1464 }
drhbf4133c2001-10-13 02:59:08 +00001465 default: {
shane75ac1de2009-06-09 18:58:52 +00001466 iA = (i64)rA;
1467 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001468 if( iA==0 ) goto arithmetic_result_is_null;
1469 if( iA==-1 ) iA = 1;
1470 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001471 break;
1472 }
drh5e00f6c2001-09-13 13:46:56 +00001473 }
drhc5a7b512010-01-13 16:25:42 +00001474#ifdef SQLITE_OMIT_FLOATING_POINT
1475 pOut->u.i = rB;
1476 MemSetTypeFlag(pOut, MEM_Int);
1477#else
drh856c1032009-06-02 15:21:42 +00001478 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001479 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001480 }
drh74eaba42014-09-18 17:52:15 +00001481 pOut->u.r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001482 MemSetTypeFlag(pOut, MEM_Real);
drh3d1d90a2014-03-24 15:00:15 +00001483 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001484 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001485 }
drhc5a7b512010-01-13 16:25:42 +00001486#endif
drh5e00f6c2001-09-13 13:46:56 +00001487 }
1488 break;
1489
drha05a7222008-01-19 03:35:58 +00001490arithmetic_result_is_null:
1491 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001492 break;
1493}
1494
drh7a957892012-02-02 17:35:43 +00001495/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001496**
drh66a51672008-01-03 00:01:23 +00001497** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001498** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1499** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001500** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001501**
drh7a957892012-02-02 17:35:43 +00001502** If P1 is not zero, then it is a register that a subsequent min() or
1503** max() aggregate will set to 1 if the current row is not the minimum or
1504** maximum. The P1 register is initialized to 0 by this instruction.
1505**
danielk1977dc1bdc42004-06-11 10:51:27 +00001506** The interface used by the implementation of the aforementioned functions
1507** to retrieve the collation sequence set by this opcode is not available
1508** publicly, only to user functions defined in func.c.
1509*/
drh9cbf3422008-01-17 16:22:13 +00001510case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001511 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001512 if( pOp->p1 ){
1513 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1514 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001515 break;
1516}
1517
drh98757152008-01-09 23:04:12 +00001518/* Opcode: Function P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001519** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001520**
drh66a51672008-01-03 00:01:23 +00001521** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001522** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001523** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001524** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001525**
drh13449892005-09-07 21:22:45 +00001526** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001527** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001528** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001529** whether meta data associated with a user function argument using the
1530** sqlite3_set_auxdata() API may be safely retained until the next
1531** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001532**
drh13449892005-09-07 21:22:45 +00001533** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001534*/
drh0bce8352002-02-28 00:41:10 +00001535case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001536 int i;
drh6810ce62004-01-31 19:22:56 +00001537 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001538 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001539 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001540 int n;
drh1350b032002-02-27 19:00:20 +00001541
drh856c1032009-06-02 15:21:42 +00001542 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001543 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001544 assert( apVal || n==0 );
dan3bc9f742013-08-15 16:18:39 +00001545 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9bd038f2014-08-27 14:14:06 +00001546 ctx.pOut = &aMem[pOp->p3];
1547 memAboutToChange(p, ctx.pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001548
dan3bc9f742013-08-15 16:18:39 +00001549 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001550 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001551 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001552 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001553 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001554 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001555 Deephemeralize(pArg);
drhab5cd702010-04-07 14:32:11 +00001556 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001557 }
danielk197751ad0ec2004-05-24 12:39:02 +00001558
dan0c547792013-07-18 17:12:08 +00001559 assert( pOp->p4type==P4_FUNCDEF );
1560 ctx.pFunc = pOp->p4.pFunc;
dan0c547792013-07-18 17:12:08 +00001561 ctx.iOp = pc;
1562 ctx.pVdbe = p;
drh9bd038f2014-08-27 14:14:06 +00001563 MemSetTypeFlag(ctx.pOut, MEM_Null);
drh9b47ee32013-08-20 03:13:51 +00001564 ctx.fErrorOrAux = 0;
drhf6aff802014-10-08 14:28:31 +00001565 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001566 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh3b130be2014-09-26 01:10:02 +00001567 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */
danielk19777e18c252004-05-25 11:47:24 +00001568
drh90669c12006-01-20 15:45:36 +00001569 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001570 if( ctx.fErrorOrAux ){
1571 if( ctx.isError ){
drh9bd038f2014-08-27 14:14:06 +00001572 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
drh9b47ee32013-08-20 03:13:51 +00001573 rc = ctx.isError;
1574 }
1575 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001576 }
1577
drh9cbf3422008-01-17 16:22:13 +00001578 /* Copy the result of the function into register P3 */
drh9bd038f2014-08-27 14:14:06 +00001579 sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
1580 if( sqlite3VdbeMemTooBig(ctx.pOut) ){
drh023ae032007-05-08 12:12:16 +00001581 goto too_big;
1582 }
drh7b94e7f2011-04-04 12:29:20 +00001583
drh9bd038f2014-08-27 14:14:06 +00001584 REGISTER_TRACE(pOp->p3, ctx.pOut);
1585 UPDATE_MAX_BLOBSIZE(ctx.pOut);
drh8e0a2f92002-02-23 23:45:45 +00001586 break;
1587}
1588
drh98757152008-01-09 23:04:12 +00001589/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001590** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001591**
drh98757152008-01-09 23:04:12 +00001592** Take the bit-wise AND of the values in register P1 and P2 and
1593** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001594** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001595*/
drh98757152008-01-09 23:04:12 +00001596/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001597** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001598**
drh98757152008-01-09 23:04:12 +00001599** Take the bit-wise OR of the values in register P1 and P2 and
1600** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001601** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001602*/
drh98757152008-01-09 23:04:12 +00001603/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001604** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001605**
drh98757152008-01-09 23:04:12 +00001606** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001607** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001608** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001609** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001610*/
drh98757152008-01-09 23:04:12 +00001611/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001612** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001613**
drh98757152008-01-09 23:04:12 +00001614** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001615** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001616** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001617** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001618*/
drh5b6afba2008-01-05 16:29:28 +00001619case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1620case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1621case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1622case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001623 i64 iA;
1624 u64 uA;
1625 i64 iB;
1626 u8 op;
drh6810ce62004-01-31 19:22:56 +00001627
drh3c657212009-11-17 23:59:58 +00001628 pIn1 = &aMem[pOp->p1];
1629 pIn2 = &aMem[pOp->p2];
1630 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001631 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001632 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001633 break;
1634 }
drh158b9cb2011-03-05 20:59:46 +00001635 iA = sqlite3VdbeIntValue(pIn2);
1636 iB = sqlite3VdbeIntValue(pIn1);
1637 op = pOp->opcode;
1638 if( op==OP_BitAnd ){
1639 iA &= iB;
1640 }else if( op==OP_BitOr ){
1641 iA |= iB;
1642 }else if( iB!=0 ){
1643 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1644
1645 /* If shifting by a negative amount, shift in the other direction */
1646 if( iB<0 ){
1647 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1648 op = 2*OP_ShiftLeft + 1 - op;
1649 iB = iB>(-64) ? -iB : 64;
1650 }
1651
1652 if( iB>=64 ){
1653 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1654 }else{
1655 memcpy(&uA, &iA, sizeof(uA));
1656 if( op==OP_ShiftLeft ){
1657 uA <<= iB;
1658 }else{
1659 uA >>= iB;
1660 /* Sign-extend on a right shift of a negative number */
1661 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1662 }
1663 memcpy(&iA, &uA, sizeof(iA));
1664 }
drhbf4133c2001-10-13 02:59:08 +00001665 }
drh158b9cb2011-03-05 20:59:46 +00001666 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001667 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001668 break;
1669}
1670
drh8558cde2008-01-05 05:20:10 +00001671/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001672** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001673**
danielk19770cdc0222008-06-26 18:04:03 +00001674** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001675** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001676**
drh8558cde2008-01-05 05:20:10 +00001677** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001678*/
drh9cbf3422008-01-17 16:22:13 +00001679case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001680 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001681 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001682 sqlite3VdbeMemIntegerify(pIn1);
1683 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001684 break;
1685}
1686
drh9cbf3422008-01-17 16:22:13 +00001687/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001688**
drh9cbf3422008-01-17 16:22:13 +00001689** Force the value in register P1 to be an integer. If the value
1690** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001691** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001692** raise an SQLITE_MISMATCH exception.
1693*/
drh9cbf3422008-01-17 16:22:13 +00001694case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001695 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001696 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001697 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001698 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001699 if( (pIn1->flags & MEM_Int)==0 ){
1700 if( pOp->p2==0 ){
1701 rc = SQLITE_MISMATCH;
1702 goto abort_due_to_error;
1703 }else{
1704 pc = pOp->p2 - 1;
1705 break;
1706 }
drh8aff1012001-12-22 14:49:24 +00001707 }
drh8aff1012001-12-22 14:49:24 +00001708 }
drh83b301b2013-11-20 00:59:02 +00001709 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001710 break;
1711}
1712
drh13573c72010-01-12 17:04:07 +00001713#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001714/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001715**
drh2133d822008-01-03 18:44:59 +00001716** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001717**
drh8a512562005-11-14 22:29:05 +00001718** This opcode is used when extracting information from a column that
1719** has REAL affinity. Such column values may still be stored as
1720** integers, for space efficiency, but after extraction we want them
1721** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001722*/
drh9cbf3422008-01-17 16:22:13 +00001723case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001724 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001725 if( pIn1->flags & MEM_Int ){
1726 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001727 }
drh487e2622005-06-25 18:42:14 +00001728 break;
1729}
drh13573c72010-01-12 17:04:07 +00001730#endif
drh487e2622005-06-25 18:42:14 +00001731
drh8df447f2005-11-01 15:48:24 +00001732#ifndef SQLITE_OMIT_CAST
drh4169e432014-08-25 20:11:52 +00001733/* Opcode: Cast P1 P2 * * *
mistachkina1dc42a2014-08-27 17:53:40 +00001734** Synopsis: affinity(r[P1])
drh487e2622005-06-25 18:42:14 +00001735**
drh4169e432014-08-25 20:11:52 +00001736** Force the value in register P1 to be the type defined by P2.
1737**
1738** <ul>
1739** <li value="97"> TEXT
1740** <li value="98"> BLOB
1741** <li value="99"> NUMERIC
1742** <li value="100"> INTEGER
1743** <li value="101"> REAL
1744** </ul>
drh487e2622005-06-25 18:42:14 +00001745**
1746** A NULL value is not changed by this routine. It remains NULL.
1747*/
drh4169e432014-08-25 20:11:52 +00001748case OP_Cast: { /* in1 */
drh7ea31cc2014-09-18 14:36:00 +00001749 assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
drh05bbb2e2014-08-25 22:37:19 +00001750 testcase( pOp->p2==SQLITE_AFF_TEXT );
1751 testcase( pOp->p2==SQLITE_AFF_NONE );
1752 testcase( pOp->p2==SQLITE_AFF_NUMERIC );
1753 testcase( pOp->p2==SQLITE_AFF_INTEGER );
1754 testcase( pOp->p2==SQLITE_AFF_REAL );
drh3c657212009-11-17 23:59:58 +00001755 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001756 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001757 rc = ExpandBlob(pIn1);
drh4169e432014-08-25 20:11:52 +00001758 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
drhb7654112008-01-12 12:48:07 +00001759 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001760 break;
1761}
drh8a512562005-11-14 22:29:05 +00001762#endif /* SQLITE_OMIT_CAST */
1763
drh35573352008-01-08 23:54:25 +00001764/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001765** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001766**
drh35573352008-01-08 23:54:25 +00001767** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1768** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001769**
drh35573352008-01-08 23:54:25 +00001770** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1771** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001772** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001773**
drh35573352008-01-08 23:54:25 +00001774** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001775** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001776** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001777** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001778** affinity is used. Note that the affinity conversions are stored
1779** back into the input registers P1 and P3. So this opcode can cause
1780** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001781**
1782** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001783** the values are compared. If both values are blobs then memcmp() is
1784** used to determine the results of the comparison. If both values
1785** are text, then the appropriate collating function specified in
1786** P4 is used to do the comparison. If P4 is not specified then
1787** memcmp() is used to compare text string. If both values are
1788** numeric, then a numeric comparison is used. If the two values
1789** are of different types, then numbers are considered less than
1790** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001791**
drh35573352008-01-08 23:54:25 +00001792** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1793** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001794**
1795** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1796** equal to one another, provided that they do not have their MEM_Cleared
1797** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001798*/
drh9cbf3422008-01-17 16:22:13 +00001799/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001800** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001801**
drh35573352008-01-08 23:54:25 +00001802** This works just like the Lt opcode except that the jump is taken if
1803** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001804** additional information.
drh6a2fe092009-09-23 02:29:36 +00001805**
1806** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1807** true or false and is never NULL. If both operands are NULL then the result
1808** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001809** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001810** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001811*/
drh9cbf3422008-01-17 16:22:13 +00001812/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001813** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001814**
drh35573352008-01-08 23:54:25 +00001815** This works just like the Lt opcode except that the jump is taken if
1816** the operands in registers P1 and P3 are equal.
1817** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001818**
1819** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1820** true or false and is never NULL. If both operands are NULL then the result
1821** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001822** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001823** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001824*/
drh9cbf3422008-01-17 16:22:13 +00001825/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001826** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001827**
drh35573352008-01-08 23:54:25 +00001828** This works just like the Lt opcode except that the jump is taken if
1829** the content of register P3 is less than or equal to the content of
1830** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001831*/
drh9cbf3422008-01-17 16:22:13 +00001832/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001833** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001834**
drh35573352008-01-08 23:54:25 +00001835** This works just like the Lt opcode except that the jump is taken if
1836** the content of register P3 is greater than the content of
1837** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001838*/
drh9cbf3422008-01-17 16:22:13 +00001839/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001840** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001841**
drh35573352008-01-08 23:54:25 +00001842** This works just like the Lt opcode except that the jump is taken if
1843** the content of register P3 is greater than or equal to the content of
1844** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001845*/
drh9cbf3422008-01-17 16:22:13 +00001846case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1847case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1848case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1849case OP_Le: /* same as TK_LE, jump, in1, in3 */
1850case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1851case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001852 int res; /* Result of the comparison of pIn1 against pIn3 */
1853 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001854 u16 flags1; /* Copy of initial value of pIn1->flags */
1855 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001856
drh3c657212009-11-17 23:59:58 +00001857 pIn1 = &aMem[pOp->p1];
1858 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001859 flags1 = pIn1->flags;
1860 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001861 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001862 /* One or both operands are NULL */
1863 if( pOp->p5 & SQLITE_NULLEQ ){
1864 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1865 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1866 ** or not both operands are null.
1867 */
1868 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001869 assert( (flags1 & MEM_Cleared)==0 );
drh3d77dee2014-02-19 14:20:49 +00001870 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
drh053a1282012-09-19 21:15:46 +00001871 if( (flags1&MEM_Null)!=0
1872 && (flags3&MEM_Null)!=0
1873 && (flags3&MEM_Cleared)==0
1874 ){
1875 res = 0; /* Results are equal */
1876 }else{
1877 res = 1; /* Results are not equal */
1878 }
drh6a2fe092009-09-23 02:29:36 +00001879 }else{
1880 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1881 ** then the result is always NULL.
1882 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1883 */
drh688852a2014-02-17 22:40:43 +00001884 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001885 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001886 MemSetTypeFlag(pOut, MEM_Null);
1887 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001888 }else{
drhf4345e42014-02-18 11:31:59 +00001889 VdbeBranchTaken(2,3);
drh688852a2014-02-17 22:40:43 +00001890 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1891 pc = pOp->p2-1;
1892 }
drh6a2fe092009-09-23 02:29:36 +00001893 }
1894 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001895 }
drh6a2fe092009-09-23 02:29:36 +00001896 }else{
1897 /* Neither operand is NULL. Do a comparison. */
1898 affinity = pOp->p5 & SQLITE_AFF_MASK;
drh24a09622014-09-18 16:28:59 +00001899 if( affinity>=SQLITE_AFF_NUMERIC ){
drhe7a34662014-09-19 22:44:20 +00001900 if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001901 applyNumericAffinity(pIn1,0);
1902 }
drhe7a34662014-09-19 22:44:20 +00001903 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drh24a09622014-09-18 16:28:59 +00001904 applyNumericAffinity(pIn3,0);
1905 }
1906 }else if( affinity==SQLITE_AFF_TEXT ){
1907 if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001908 testcase( pIn1->flags & MEM_Int );
1909 testcase( pIn1->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001910 sqlite3VdbeMemStringify(pIn1, encoding, 1);
1911 }
1912 if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){
drhe7a34662014-09-19 22:44:20 +00001913 testcase( pIn3->flags & MEM_Int );
1914 testcase( pIn3->flags & MEM_Real );
drh24a09622014-09-18 16:28:59 +00001915 sqlite3VdbeMemStringify(pIn3, encoding, 1);
1916 }
drh6a2fe092009-09-23 02:29:36 +00001917 }
drh6a2fe092009-09-23 02:29:36 +00001918 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drhca5506b2014-09-17 23:37:38 +00001919 if( pIn1->flags & MEM_Zero ){
1920 sqlite3VdbeMemExpandBlob(pIn1);
1921 flags1 &= ~MEM_Zero;
1922 }
1923 if( pIn3->flags & MEM_Zero ){
1924 sqlite3VdbeMemExpandBlob(pIn3);
1925 flags3 &= ~MEM_Zero;
1926 }
drh24a09622014-09-18 16:28:59 +00001927 if( db->mallocFailed ) goto no_mem;
drh6a2fe092009-09-23 02:29:36 +00001928 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001929 }
danielk1977a37cdde2004-05-16 11:15:36 +00001930 switch( pOp->opcode ){
1931 case OP_Eq: res = res==0; break;
1932 case OP_Ne: res = res!=0; break;
1933 case OP_Lt: res = res<0; break;
1934 case OP_Le: res = res<=0; break;
1935 case OP_Gt: res = res>0; break;
1936 default: res = res>=0; break;
1937 }
1938
drh35573352008-01-08 23:54:25 +00001939 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001940 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001941 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001942 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001943 pOut->u.i = res;
1944 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001945 }else{
drhf4345e42014-02-18 11:31:59 +00001946 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
drh688852a2014-02-17 22:40:43 +00001947 if( res ){
1948 pc = pOp->p2-1;
1949 }
danielk1977a37cdde2004-05-16 11:15:36 +00001950 }
danb7dca7d2010-03-05 16:32:12 +00001951 /* Undo any changes made by applyAffinity() to the input registers. */
drhca5506b2014-09-17 23:37:38 +00001952 pIn1->flags = flags1;
1953 pIn3->flags = flags3;
danielk1977a37cdde2004-05-16 11:15:36 +00001954 break;
1955}
drhc9b84a12002-06-20 11:36:48 +00001956
drh0acb7e42008-06-25 00:12:41 +00001957/* Opcode: Permutation * * * P4 *
1958**
shanebe217792009-03-05 04:20:31 +00001959** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001960** of integers in P4.
1961**
drh953f7612012-12-07 22:18:54 +00001962** The permutation is only valid until the next OP_Compare that has
1963** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1964** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001965*/
1966case OP_Permutation: {
1967 assert( pOp->p4type==P4_INTARRAY );
1968 assert( pOp->p4.ai );
1969 aPermute = pOp->p4.ai;
1970 break;
1971}
1972
drh953f7612012-12-07 22:18:54 +00001973/* Opcode: Compare P1 P2 P3 P4 P5
drh079a3072014-03-19 14:10:55 +00001974** Synopsis: r[P1@P3] <-> r[P2@P3]
drh16ee60f2008-06-20 18:13:25 +00001975**
drh710c4842010-08-30 01:17:20 +00001976** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1977** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001978** the comparison for use by the next OP_Jump instruct.
1979**
drh0ca10df2012-12-08 13:26:23 +00001980** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1981** determined by the most recent OP_Permutation operator. If the
1982** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1983** order.
1984**
drh0acb7e42008-06-25 00:12:41 +00001985** P4 is a KeyInfo structure that defines collating sequences and sort
1986** orders for the comparison. The permutation applies to registers
1987** only. The KeyInfo elements are used sequentially.
1988**
1989** The comparison is a sort comparison, so NULLs compare equal,
1990** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001991** and strings are less than blobs.
1992*/
1993case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001994 int n;
1995 int i;
1996 int p1;
1997 int p2;
1998 const KeyInfo *pKeyInfo;
1999 int idx;
2000 CollSeq *pColl; /* Collating sequence to use on this term */
2001 int bRev; /* True for DESCENDING sort order */
2002
drh953f7612012-12-07 22:18:54 +00002003 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002004 n = pOp->p3;
2005 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002006 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002007 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002008 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002009 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002010#if SQLITE_DEBUG
2011 if( aPermute ){
2012 int k, mx = 0;
2013 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002014 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2015 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002016 }else{
dan3bc9f742013-08-15 16:18:39 +00002017 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2018 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002019 }
2020#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002021 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002022 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002023 assert( memIsValid(&aMem[p1+idx]) );
2024 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002025 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2026 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002027 assert( i<pKeyInfo->nField );
2028 pColl = pKeyInfo->aColl[i];
2029 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002030 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002031 if( iCompare ){
2032 if( bRev ) iCompare = -iCompare;
2033 break;
2034 }
drh16ee60f2008-06-20 18:13:25 +00002035 }
drh0acb7e42008-06-25 00:12:41 +00002036 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002037 break;
2038}
2039
2040/* Opcode: Jump P1 P2 P3 * *
2041**
2042** Jump to the instruction at address P1, P2, or P3 depending on whether
2043** in the most recent OP_Compare instruction the P1 vector was less than
2044** equal to, or greater than the P2 vector, respectively.
2045*/
drh0acb7e42008-06-25 00:12:41 +00002046case OP_Jump: { /* jump */
2047 if( iCompare<0 ){
drh688852a2014-02-17 22:40:43 +00002048 pc = pOp->p1 - 1; VdbeBranchTaken(0,3);
drh0acb7e42008-06-25 00:12:41 +00002049 }else if( iCompare==0 ){
drh688852a2014-02-17 22:40:43 +00002050 pc = pOp->p2 - 1; VdbeBranchTaken(1,3);
drh16ee60f2008-06-20 18:13:25 +00002051 }else{
drh688852a2014-02-17 22:40:43 +00002052 pc = pOp->p3 - 1; VdbeBranchTaken(2,3);
drh16ee60f2008-06-20 18:13:25 +00002053 }
2054 break;
2055}
2056
drh5b6afba2008-01-05 16:29:28 +00002057/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002058** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002059**
drh5b6afba2008-01-05 16:29:28 +00002060** Take the logical AND of the values in registers P1 and P2 and
2061** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002062**
drh5b6afba2008-01-05 16:29:28 +00002063** If either P1 or P2 is 0 (false) then the result is 0 even if
2064** the other input is NULL. A NULL and true or two NULLs give
2065** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002066*/
drh5b6afba2008-01-05 16:29:28 +00002067/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002068** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002069**
2070** Take the logical OR of the values in register P1 and P2 and
2071** store the answer in register P3.
2072**
2073** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2074** even if the other input is NULL. A NULL and false or two NULLs
2075** give a NULL output.
2076*/
2077case OP_And: /* same as TK_AND, in1, in2, out3 */
2078case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002079 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2080 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002081
drh3c657212009-11-17 23:59:58 +00002082 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002083 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002084 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002085 }else{
drh5b6afba2008-01-05 16:29:28 +00002086 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002087 }
drh3c657212009-11-17 23:59:58 +00002088 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002089 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002090 v2 = 2;
2091 }else{
drh5b6afba2008-01-05 16:29:28 +00002092 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002093 }
2094 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002095 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002096 v1 = and_logic[v1*3+v2];
2097 }else{
drh5b6afba2008-01-05 16:29:28 +00002098 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002099 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002100 }
drh3c657212009-11-17 23:59:58 +00002101 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002102 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002103 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002104 }else{
drh5b6afba2008-01-05 16:29:28 +00002105 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002106 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002107 }
drh5e00f6c2001-09-13 13:46:56 +00002108 break;
2109}
2110
drhe99fa2a2008-12-15 15:27:51 +00002111/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002112** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002113**
drhe99fa2a2008-12-15 15:27:51 +00002114** Interpret the value in register P1 as a boolean value. Store the
2115** boolean complement in register P2. If the value in register P1 is
2116** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002117*/
drh93952eb2009-11-13 19:43:43 +00002118case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002119 pIn1 = &aMem[pOp->p1];
2120 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002121 sqlite3VdbeMemSetNull(pOut);
2122 if( (pIn1->flags & MEM_Null)==0 ){
2123 pOut->flags = MEM_Int;
2124 pOut->u.i = !sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002125 }
drh5e00f6c2001-09-13 13:46:56 +00002126 break;
2127}
2128
drhe99fa2a2008-12-15 15:27:51 +00002129/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002130** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002131**
drhe99fa2a2008-12-15 15:27:51 +00002132** Interpret the content of register P1 as an integer. Store the
2133** ones-complement of the P1 value into register P2. If P1 holds
2134** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002135*/
drh93952eb2009-11-13 19:43:43 +00002136case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002137 pIn1 = &aMem[pOp->p1];
2138 pOut = &aMem[pOp->p2];
drh0725cab2014-09-17 14:52:46 +00002139 sqlite3VdbeMemSetNull(pOut);
2140 if( (pIn1->flags & MEM_Null)==0 ){
2141 pOut->flags = MEM_Int;
2142 pOut->u.i = ~sqlite3VdbeIntValue(pIn1);
drhe99fa2a2008-12-15 15:27:51 +00002143 }
drhbf4133c2001-10-13 02:59:08 +00002144 break;
2145}
2146
drh48f2d3b2011-09-16 01:34:43 +00002147/* Opcode: Once P1 P2 * * *
2148**
drh5dad9a32014-07-25 18:37:42 +00002149** Check the "once" flag number P1. If it is set, jump to instruction P2.
2150** Otherwise, set the flag and fall through to the next instruction.
2151** In other words, this opcode causes all following opcodes up through P2
2152** (but not including P2) to run just once and to be skipped on subsequent
2153** times through the loop.
2154**
2155** All "once" flags are initially cleared whenever a prepared statement
2156** first begins to run.
drh48f2d3b2011-09-16 01:34:43 +00002157*/
dan1d8cb212011-12-09 13:24:16 +00002158case OP_Once: { /* jump */
2159 assert( pOp->p1<p->nOnceFlag );
drh688852a2014-02-17 22:40:43 +00002160 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
dan1d8cb212011-12-09 13:24:16 +00002161 if( p->aOnceFlag[pOp->p1] ){
2162 pc = pOp->p2-1;
2163 }else{
2164 p->aOnceFlag[pOp->p1] = 1;
2165 }
2166 break;
2167}
2168
drh3c84ddf2008-01-09 02:15:38 +00002169/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002170**
drhef8662b2011-06-20 21:47:58 +00002171** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002172** is considered true if it is numeric and non-zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002173** in P1 is NULL then take the jump if and only if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002174*/
drh3c84ddf2008-01-09 02:15:38 +00002175/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002176**
drhef8662b2011-06-20 21:47:58 +00002177** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002178** is considered false if it has a numeric value of zero. If the value
drhe21a6e12014-08-01 18:00:24 +00002179** in P1 is NULL then take the jump if and only if P3 is non-zero.
drhf5905aa2002-05-26 20:54:33 +00002180*/
drh9cbf3422008-01-17 16:22:13 +00002181case OP_If: /* jump, in1 */
2182case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002183 int c;
drh3c657212009-11-17 23:59:58 +00002184 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002185 if( pIn1->flags & MEM_Null ){
2186 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002187 }else{
drhba0232a2005-06-06 17:27:19 +00002188#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002189 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002190#else
drh3c84ddf2008-01-09 02:15:38 +00002191 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002192#endif
drhf5905aa2002-05-26 20:54:33 +00002193 if( pOp->opcode==OP_IfNot ) c = !c;
2194 }
drh688852a2014-02-17 22:40:43 +00002195 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002196 if( c ){
2197 pc = pOp->p2-1;
2198 }
drh5e00f6c2001-09-13 13:46:56 +00002199 break;
2200}
2201
drh830ecf92009-06-18 00:41:55 +00002202/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002203** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002204**
drh830ecf92009-06-18 00:41:55 +00002205** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002206*/
drh9cbf3422008-01-17 16:22:13 +00002207case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002208 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002209 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002210 if( (pIn1->flags & MEM_Null)!=0 ){
2211 pc = pOp->p2 - 1;
2212 }
drh477df4b2008-01-05 18:48:24 +00002213 break;
2214}
2215
drh98757152008-01-09 23:04:12 +00002216/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002217** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002218**
drh6a288a32008-01-07 19:20:24 +00002219** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002220*/
drh9cbf3422008-01-17 16:22:13 +00002221case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002222 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002223 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002224 if( (pIn1->flags & MEM_Null)==0 ){
2225 pc = pOp->p2 - 1;
2226 }
drh5e00f6c2001-09-13 13:46:56 +00002227 break;
2228}
2229
drh3e9ca092009-09-08 01:14:48 +00002230/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002231** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002232**
danielk1977cfcdaef2004-05-12 07:33:33 +00002233** Interpret the data that cursor P1 points to as a structure built using
2234** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002235** information about the format of the data.) Extract the P2-th column
2236** from this record. If there are less that (P2+1)
2237** values in the record, extract a NULL.
2238**
drh9cbf3422008-01-17 16:22:13 +00002239** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002240**
danielk19771f4aa332008-01-03 09:51:55 +00002241** If the column contains fewer than P2 fields, then extract a NULL. Or,
2242** if the P4 argument is a P4_MEM use the value of the P4 argument as
2243** the result.
drh3e9ca092009-09-08 01:14:48 +00002244**
2245** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2246** then the cache of the cursor is reset prior to extracting the column.
2247** The first OP_Column against a pseudo-table after the value of the content
2248** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002249**
drhdda5c082012-03-28 13:41:10 +00002250** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2251** the result is guaranteed to only be used as the argument of a length()
2252** or typeof() function, respectively. The loading of large blobs can be
2253** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002254*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002255case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002256 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002257 int p2; /* column number to retrieve */
2258 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002259 BtCursor *pCrsr; /* The BTree cursor */
drhd3194f52004-05-27 19:59:32 +00002260 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002261 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002262 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002263 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002264 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002265 const u8 *zData; /* Part of the record being decoded */
2266 const u8 *zHdr; /* Next unparsed byte of the header */
2267 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002268 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002269 u32 szField; /* Number of bytes in the content of a field */
drh501932c2013-11-21 21:59:53 +00002270 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002271 u32 t; /* A type code from the record header */
drh0c8f7602014-09-19 16:56:45 +00002272 u16 fx; /* pDest->flags value */
drh3e9ca092009-09-08 01:14:48 +00002273 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002274
drh399af1d2013-11-20 17:25:55 +00002275 p2 = pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00002276 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002277 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002278 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002279 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2280 pC = p->apCsr[pOp->p1];
drha5759672012-10-30 14:39:12 +00002281 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002282 assert( p2<pC->nField );
drhb53a5a92014-10-12 22:37:22 +00002283 aOffset = pC->aOffset;
danielk19770817d0d2007-02-14 09:19:36 +00002284#ifndef SQLITE_OMIT_VIRTUALTABLE
drh380d6852013-11-20 20:58:00 +00002285 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
danielk19770817d0d2007-02-14 09:19:36 +00002286#endif
shane36840fd2009-06-26 16:32:13 +00002287 pCrsr = pC->pCursor;
drh380d6852013-11-20 20:58:00 +00002288 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
2289 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */
drh399af1d2013-11-20 17:25:55 +00002290
2291 /* If the cursor cache is stale, bring it up-to-date */
2292 rc = sqlite3VdbeCursorMoveto(pC);
2293 if( rc ) goto abort_due_to_error;
drh6cf4a7d2014-10-13 13:00:58 +00002294 if( pC->cacheStatus!=p->cacheCtr ){
drhc8606e42013-11-20 19:28:03 +00002295 if( pC->nullRow ){
2296 if( pCrsr==0 ){
2297 assert( pC->pseudoTableReg>0 );
2298 pReg = &aMem[pC->pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002299 assert( pReg->flags & MEM_Blob );
2300 assert( memIsValid(pReg) );
2301 pC->payloadSize = pC->szRow = avail = pReg->n;
2302 pC->aRow = (u8*)pReg->z;
2303 }else{
2304 MemSetTypeFlag(pDest, MEM_Null);
drh399af1d2013-11-20 17:25:55 +00002305 goto op_column_out;
2306 }
danielk197784ac9d02004-05-18 09:58:06 +00002307 }else{
drhc8606e42013-11-20 19:28:03 +00002308 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002309 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002310 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2311 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2312 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2313 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2314 ** payload size, so it is impossible for payloadSize64 to be
2315 ** larger than 32 bits. */
2316 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2317 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2318 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002319 }else{
drh399af1d2013-11-20 17:25:55 +00002320 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2321 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2322 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2323 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002324 }
drh399af1d2013-11-20 17:25:55 +00002325 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2326 if( pC->payloadSize <= (u32)avail ){
2327 pC->szRow = pC->payloadSize;
drhe61cffc2004-06-12 18:12:15 +00002328 }else{
drh399af1d2013-11-20 17:25:55 +00002329 pC->szRow = avail;
2330 }
2331 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2332 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002333 }
drhd3194f52004-05-27 19:59:32 +00002334 }
drh399af1d2013-11-20 17:25:55 +00002335 pC->cacheStatus = p->cacheCtr;
2336 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2337 pC->nHdrParsed = 0;
2338 aOffset[0] = offset;
drh35cd6432009-06-05 14:17:21 +00002339
2340 /* Make sure a corrupt database has not given us an oversize header.
2341 ** Do this now to avoid an oversize memory allocation.
2342 **
2343 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2344 ** types use so much data space that there can only be 4096 and 32 of
2345 ** them, respectively. So the maximum header length results from a
2346 ** 3-byte type for each of the maximum of 32768 columns plus three
2347 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2348 */
drh399af1d2013-11-20 17:25:55 +00002349 if( offset > 98307 || offset > pC->payloadSize ){
drh35cd6432009-06-05 14:17:21 +00002350 rc = SQLITE_CORRUPT_BKPT;
drhc8606e42013-11-20 19:28:03 +00002351 goto op_column_error;
drh35cd6432009-06-05 14:17:21 +00002352 }
drhc81aa2e2014-10-11 23:31:52 +00002353
2354 if( avail<offset ){
2355 /* pC->aRow does not have to hold the entire row, but it does at least
2356 ** need to cover the header of the record. If pC->aRow does not contain
2357 ** the complete header, then set it to zero, forcing the header to be
2358 ** dynamically allocated. */
2359 pC->aRow = 0;
2360 pC->szRow = 0;
2361 }
2362
2363 /* The following goto is an optimization. It can be omitted and
2364 ** everything will still work. But OP_Column is measurably faster
2365 ** by skipping the subsequent conditional, which is always true.
2366 */
2367 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */
2368 goto op_column_read_header;
drh399af1d2013-11-20 17:25:55 +00002369 }
drh35cd6432009-06-05 14:17:21 +00002370
drh399af1d2013-11-20 17:25:55 +00002371 /* Make sure at least the first p2+1 entries of the header have been
drh0c8f7602014-09-19 16:56:45 +00002372 ** parsed and valid information is in aOffset[] and pC->aType[].
drh399af1d2013-11-20 17:25:55 +00002373 */
drhc8606e42013-11-20 19:28:03 +00002374 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002375 /* If there is more header available for parsing in the record, try
2376 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002377 */
drhc81aa2e2014-10-11 23:31:52 +00002378 op_column_read_header:
drhc8606e42013-11-20 19:28:03 +00002379 if( pC->iHdrOffset<aOffset[0] ){
2380 /* Make sure zData points to enough of the record to cover the header. */
2381 if( pC->aRow==0 ){
2382 memset(&sMem, 0, sizeof(sMem));
drh14da87f2013-11-20 21:51:33 +00002383 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
2384 !pC->isTable, &sMem);
drhc8606e42013-11-20 19:28:03 +00002385 if( rc!=SQLITE_OK ){
2386 goto op_column_error;
2387 }
2388 zData = (u8*)sMem.z;
2389 }else{
2390 zData = pC->aRow;
2391 }
2392
drh0c8f7602014-09-19 16:56:45 +00002393 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */
drhc8606e42013-11-20 19:28:03 +00002394 i = pC->nHdrParsed;
2395 offset = aOffset[i];
2396 zHdr = zData + pC->iHdrOffset;
2397 zEndHdr = zData + aOffset[0];
2398 assert( i<=p2 && zHdr<zEndHdr );
2399 do{
2400 if( zHdr[0]<0x80 ){
2401 t = zHdr[0];
2402 zHdr++;
2403 }else{
2404 zHdr += sqlite3GetVarint32(zHdr, &t);
2405 }
drh0c8f7602014-09-19 16:56:45 +00002406 pC->aType[i] = t;
drhc8606e42013-11-20 19:28:03 +00002407 szField = sqlite3VdbeSerialTypeLen(t);
2408 offset += szField;
2409 if( offset<szField ){ /* True if offset overflows */
2410 zHdr = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2411 break;
2412 }
2413 i++;
2414 aOffset[i] = offset;
2415 }while( i<=p2 && zHdr<zEndHdr );
2416 pC->nHdrParsed = i;
2417 pC->iHdrOffset = (u32)(zHdr - zData);
2418 if( pC->aRow==0 ){
2419 sqlite3VdbeMemRelease(&sMem);
2420 sMem.flags = MEM_Null;
2421 }
2422
drh8dd83622014-10-13 23:39:02 +00002423 /* The record is corrupt if any of the following are true:
2424 ** (1) the bytes of the header extend past the declared header size
2425 ** (zHdr>zEndHdr)
2426 ** (2) the entire header was used but not all data was used
2427 ** (zHdr==zEndHdr && offset!=pC->payloadSize)
2428 ** (3) the end of the data extends beyond the end of the record.
2429 ** (offset > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002430 */
drh8dd83622014-10-13 23:39:02 +00002431 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize))
drhc8606e42013-11-20 19:28:03 +00002432 || (offset > pC->payloadSize)
drhc8606e42013-11-20 19:28:03 +00002433 ){
2434 rc = SQLITE_CORRUPT_BKPT;
2435 goto op_column_error;
2436 }
2437 }
2438
drh380d6852013-11-20 20:58:00 +00002439 /* If after trying to extra new entries from the header, nHdrParsed is
2440 ** still not up to p2, that means that the record has fewer than p2
2441 ** columns. So the result will be either the default value or a NULL.
2442 */
drhc8606e42013-11-20 19:28:03 +00002443 if( pC->nHdrParsed<=p2 ){
2444 if( pOp->p4type==P4_MEM ){
2445 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2446 }else{
2447 MemSetTypeFlag(pDest, MEM_Null);
2448 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002449 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002450 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002451 }
danielk1977192ac1d2004-05-10 07:17:30 +00002452
drh380d6852013-11-20 20:58:00 +00002453 /* Extract the content for the p2+1-th column. Control can only
drh0c8f7602014-09-19 16:56:45 +00002454 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are
drh380d6852013-11-20 20:58:00 +00002455 ** all valid.
drh9188b382004-05-14 21:12:22 +00002456 */
drhc8606e42013-11-20 19:28:03 +00002457 assert( p2<pC->nHdrParsed );
2458 assert( rc==SQLITE_OK );
drh75fd0542014-03-01 16:24:44 +00002459 assert( sqlite3VdbeCheckMemInvariants(pDest) );
drh0725cab2014-09-17 14:52:46 +00002460 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest);
drh0c8f7602014-09-19 16:56:45 +00002461 t = pC->aType[p2];
drhc8606e42013-11-20 19:28:03 +00002462 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002463 /* This is the common case where the desired content fits on the original
2464 ** page - where the content is not on an overflow page */
drh0c8f7602014-09-19 16:56:45 +00002465 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest);
danielk197736963fd2005-02-19 08:18:05 +00002466 }else{
drh58c96082013-12-23 11:33:32 +00002467 /* This branch happens only when content is on overflow pages */
drh380d6852013-11-20 20:58:00 +00002468 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2469 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2470 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002471 ){
drh2a2a6962014-09-16 18:22:44 +00002472 /* Content is irrelevant for
2473 ** 1. the typeof() function,
2474 ** 2. the length(X) function if X is a blob, and
2475 ** 3. if the content length is zero.
2476 ** So we might as well use bogus content rather than reading
2477 ** content from disk. NULL will work for the value for strings
2478 ** and blobs and whatever is in the payloadSize64 variable
2479 ** will work for everything else. */
2480 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest);
danielk1977aee18ef2005-03-09 12:26:50 +00002481 }else{
drh14da87f2013-11-20 21:51:33 +00002482 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drh2a2a6962014-09-16 18:22:44 +00002483 pDest);
drhc8606e42013-11-20 19:28:03 +00002484 if( rc!=SQLITE_OK ){
2485 goto op_column_error;
2486 }
drh2a2a6962014-09-16 18:22:44 +00002487 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest);
2488 pDest->flags &= ~MEM_Ephem;
danielk1977aee18ef2005-03-09 12:26:50 +00002489 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002490 }
drhc8606e42013-11-20 19:28:03 +00002491 pDest->enc = encoding;
drhd3194f52004-05-27 19:59:32 +00002492
danielk19773c9cc8d2005-01-17 03:40:08 +00002493op_column_out:
drh7b5ebca2014-09-19 15:28:33 +00002494 /* If the column value is an ephemeral string, go ahead and persist
2495 ** that string in case the cursor moves before the column value is
2496 ** used. The following code does the equivalent of Deephemeralize()
2497 ** but does it faster. */
2498 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){
drh0c8f7602014-09-19 16:56:45 +00002499 fx = pDest->flags & (MEM_Str|MEM_Blob);
2500 assert( fx!=0 );
drh7b5ebca2014-09-19 15:28:33 +00002501 zData = (const u8*)pDest->z;
2502 len = pDest->n;
2503 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem;
2504 memcpy(pDest->z, zData, len);
2505 pDest->z[len] = 0;
2506 pDest->z[len+1] = 0;
drh0c8f7602014-09-19 16:56:45 +00002507 pDest->flags = fx|MEM_Term;
drh7b5ebca2014-09-19 15:28:33 +00002508 }
drhc8606e42013-11-20 19:28:03 +00002509op_column_error:
drhb7654112008-01-12 12:48:07 +00002510 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002511 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002512 break;
2513}
2514
danielk1977751de562008-04-18 09:01:15 +00002515/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002516** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002517**
2518** Apply affinities to a range of P2 registers starting with P1.
2519**
2520** P4 is a string that is P2 characters long. The nth character of the
2521** string indicates the column affinity that should be used for the nth
2522** memory cell in the range.
2523*/
2524case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002525 const char *zAffinity; /* The affinity to be applied */
2526 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002527
drh856c1032009-06-02 15:21:42 +00002528 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002529 assert( zAffinity!=0 );
2530 assert( zAffinity[pOp->p2]==0 );
2531 pIn1 = &aMem[pOp->p1];
2532 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002533 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002534 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002535 applyAffinity(pIn1, cAff, encoding);
2536 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002537 }
2538 break;
2539}
2540
drh1db639c2008-01-17 02:36:28 +00002541/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002542** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002543**
drh710c4842010-08-30 01:17:20 +00002544** Convert P2 registers beginning with P1 into the [record format]
2545** use as a data record in a database table or as a key
2546** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002547**
danielk1977751de562008-04-18 09:01:15 +00002548** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002549** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002550** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002551**
drh8a512562005-11-14 22:29:05 +00002552** The mapping from character to affinity is given by the SQLITE_AFF_
2553** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002554**
drh66a51672008-01-03 00:01:23 +00002555** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002556*/
drh1db639c2008-01-17 02:36:28 +00002557case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002558 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2559 Mem *pRec; /* The new record */
2560 u64 nData; /* Number of bytes of data space */
2561 int nHdr; /* Number of bytes of header space */
2562 i64 nByte; /* Data space required for this record */
2563 int nZero; /* Number of zero bytes at the end of the record */
2564 int nVarint; /* Number of bytes in a varint */
2565 u32 serial_type; /* Type field */
2566 Mem *pData0; /* First field to be combined into the record */
2567 Mem *pLast; /* Last field of the record */
2568 int nField; /* Number of fields in the record */
2569 char *zAffinity; /* The affinity string for the record */
2570 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002571 int i; /* Space used in zNewRecord[] header */
2572 int j; /* Space used in zNewRecord[] content */
drh856c1032009-06-02 15:21:42 +00002573 int len; /* Length of a field */
2574
drhf3218fe2004-05-28 08:21:02 +00002575 /* Assuming the record contains N fields, the record format looks
2576 ** like this:
2577 **
drh7a224de2004-06-02 01:22:02 +00002578 ** ------------------------------------------------------------------------
2579 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2580 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002581 **
drh9cbf3422008-01-17 16:22:13 +00002582 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
peter.d.reid60ec9142014-09-06 16:39:46 +00002583 ** and so forth.
drhf3218fe2004-05-28 08:21:02 +00002584 **
2585 ** Each type field is a varint representing the serial type of the
2586 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002587 ** hdr-size field is also a varint which is the offset from the beginning
2588 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002589 */
drh856c1032009-06-02 15:21:42 +00002590 nData = 0; /* Number of bytes of data space */
2591 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002592 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002593 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002594 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002595 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002596 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002597 nField = pOp->p2;
2598 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002599 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002600
drh2b4ded92010-09-27 21:09:31 +00002601 /* Identify the output register */
2602 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2603 pOut = &aMem[pOp->p3];
2604 memAboutToChange(p, pOut);
2605
drh3e6c0602013-12-10 20:53:01 +00002606 /* Apply the requested affinity to all inputs
2607 */
2608 assert( pData0<=pLast );
2609 if( zAffinity ){
2610 pRec = pData0;
2611 do{
drh57bf4a82014-02-17 14:59:22 +00002612 applyAffinity(pRec++, *(zAffinity++), encoding);
2613 assert( zAffinity[0]==0 || pRec<=pLast );
2614 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002615 }
2616
drhf3218fe2004-05-28 08:21:02 +00002617 /* Loop through the elements that will make up the record to figure
2618 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002619 */
drh038b7bc2013-12-09 23:17:22 +00002620 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002621 do{
drh2b4ded92010-09-27 21:09:31 +00002622 assert( memIsValid(pRec) );
drhfacf47a2014-10-13 20:12:47 +00002623 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002624 len = sqlite3VdbeSerialTypeLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002625 if( pRec->flags & MEM_Zero ){
2626 if( nData ){
2627 sqlite3VdbeMemExpandBlob(pRec);
2628 }else{
2629 nZero += pRec->u.nZero;
2630 len -= pRec->u.nZero;
2631 }
2632 }
drhae7e1512007-05-02 16:51:59 +00002633 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002634 testcase( serial_type==127 );
2635 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002636 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002637 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002638
drhf3218fe2004-05-28 08:21:02 +00002639 /* Add the initial header varint and total the size */
drh59bf00c2013-12-08 23:33:28 +00002640 testcase( nHdr==126 );
2641 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002642 if( nHdr<=126 ){
2643 /* The common case */
2644 nHdr += 1;
2645 }else{
2646 /* Rare case of a really large header */
2647 nVarint = sqlite3VarintLen(nHdr);
2648 nHdr += nVarint;
2649 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002650 }
drh038b7bc2013-12-09 23:17:22 +00002651 nByte = nHdr+nData;
drhbb4957f2008-03-20 14:03:29 +00002652 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002653 goto too_big;
2654 }
drhf3218fe2004-05-28 08:21:02 +00002655
danielk1977a7a8e142008-02-13 18:25:27 +00002656 /* Make sure the output register has a buffer large enough to store
2657 ** the new record. The output register (pOp->p3) is not allowed to
2658 ** be one of the input registers (because the following call to
drh322f2852014-09-19 00:43:39 +00002659 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used).
danielk1977a7a8e142008-02-13 18:25:27 +00002660 */
drh322f2852014-09-19 00:43:39 +00002661 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002662 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002663 }
danielk1977a7a8e142008-02-13 18:25:27 +00002664 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002665
2666 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002667 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002668 j = nHdr;
2669 assert( pData0<=pLast );
2670 pRec = pData0;
2671 do{
drhfacf47a2014-10-13 20:12:47 +00002672 serial_type = pRec->uTemp;
drh038b7bc2013-12-09 23:17:22 +00002673 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drha9ab4812013-12-11 11:00:44 +00002674 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002675 }while( (++pRec)<=pLast );
2676 assert( i==nHdr );
2677 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002678
dan3bc9f742013-08-15 16:18:39 +00002679 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002680 pOut->n = (int)nByte;
drhc91b2fd2014-03-01 18:13:23 +00002681 pOut->flags = MEM_Blob;
drhfdf972a2007-05-02 13:30:27 +00002682 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002683 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002684 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002685 }
drh477df4b2008-01-05 18:48:24 +00002686 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002687 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002688 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002689 break;
2690}
2691
danielk1977a5533162009-02-24 10:01:51 +00002692/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002693** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002694**
2695** Store the number of entries (an integer value) in the table or index
2696** opened by cursor P1 in register P2
2697*/
2698#ifndef SQLITE_OMIT_BTREECOUNT
2699case OP_Count: { /* out2-prerelease */
2700 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002701 BtCursor *pCrsr;
2702
2703 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh3da046d2013-11-11 03:24:11 +00002704 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002705 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002706 rc = sqlite3BtreeCount(pCrsr, &nEntry);
danielk1977a5533162009-02-24 10:01:51 +00002707 pOut->u.i = nEntry;
2708 break;
2709}
2710#endif
2711
danielk1977fd7f0452008-12-17 17:30:26 +00002712/* Opcode: Savepoint P1 * * P4 *
2713**
2714** Open, release or rollback the savepoint named by parameter P4, depending
2715** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2716** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2717*/
2718case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002719 int p1; /* Value of P1 operand */
2720 char *zName; /* Name of savepoint */
2721 int nName;
2722 Savepoint *pNew;
2723 Savepoint *pSavepoint;
2724 Savepoint *pTmp;
2725 int iSavepoint;
2726 int ii;
2727
2728 p1 = pOp->p1;
2729 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002730
2731 /* Assert that the p1 parameter is valid. Also that if there is no open
2732 ** transaction, then there cannot be any savepoints.
2733 */
2734 assert( db->pSavepoint==0 || db->autoCommit==0 );
2735 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2736 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2737 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002738 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002739
2740 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002741 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002742 /* A new savepoint cannot be created if there are active write
2743 ** statements (i.e. open read/write incremental blob handles).
2744 */
2745 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2746 "SQL statements in progress");
2747 rc = SQLITE_BUSY;
2748 }else{
drh856c1032009-06-02 15:21:42 +00002749 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002750
drhbe07ec52011-06-03 12:15:26 +00002751#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002752 /* This call is Ok even if this savepoint is actually a transaction
2753 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2754 ** If this is a transaction savepoint being opened, it is guaranteed
2755 ** that the db->aVTrans[] array is empty. */
2756 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002757 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2758 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002759 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002760#endif
dand9495cd2011-04-27 12:08:04 +00002761
danielk1977fd7f0452008-12-17 17:30:26 +00002762 /* Create a new savepoint structure. */
2763 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2764 if( pNew ){
2765 pNew->zName = (char *)&pNew[1];
2766 memcpy(pNew->zName, zName, nName+1);
2767
2768 /* If there is no open transaction, then mark this as a special
2769 ** "transaction savepoint". */
2770 if( db->autoCommit ){
2771 db->autoCommit = 0;
2772 db->isTransactionSavepoint = 1;
2773 }else{
2774 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002775 }
danielk1977fd7f0452008-12-17 17:30:26 +00002776
2777 /* Link the new savepoint into the database handle's list. */
2778 pNew->pNext = db->pSavepoint;
2779 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002780 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002781 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002782 }
2783 }
2784 }else{
drh856c1032009-06-02 15:21:42 +00002785 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002786
2787 /* Find the named savepoint. If there is no such savepoint, then an
2788 ** an error is returned to the user. */
2789 for(
drh856c1032009-06-02 15:21:42 +00002790 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002791 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002792 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002793 ){
2794 iSavepoint++;
2795 }
2796 if( !pSavepoint ){
2797 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2798 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002799 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002800 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002801 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002802 */
2803 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002804 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002805 );
2806 rc = SQLITE_BUSY;
2807 }else{
2808
2809 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002810 ** and this is a RELEASE command, then the current transaction
2811 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002812 */
2813 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2814 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002815 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002816 goto vdbe_return;
2817 }
danielk1977fd7f0452008-12-17 17:30:26 +00002818 db->autoCommit = 1;
2819 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2820 p->pc = pc;
2821 db->autoCommit = 0;
2822 p->rc = rc = SQLITE_BUSY;
2823 goto vdbe_return;
2824 }
danielk197734cf35d2008-12-18 18:31:38 +00002825 db->isTransactionSavepoint = 0;
2826 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002827 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002828 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002829 if( p1==SAVEPOINT_ROLLBACK ){
2830 for(ii=0; ii<db->nDb; ii++){
2831 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2832 }
drh0f198a72012-02-13 16:43:16 +00002833 }
2834 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002835 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2836 if( rc!=SQLITE_OK ){
2837 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002838 }
danielk1977fd7f0452008-12-17 17:30:26 +00002839 }
drh9f0bbf92009-01-02 21:08:09 +00002840 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002841 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002842 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002843 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002844 }
2845 }
2846
2847 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2848 ** savepoints nested inside of the savepoint being operated on. */
2849 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002850 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002851 db->pSavepoint = pTmp->pNext;
2852 sqlite3DbFree(db, pTmp);
2853 db->nSavepoint--;
2854 }
2855
dan1da40a32009-09-19 17:00:31 +00002856 /* If it is a RELEASE, then destroy the savepoint being operated on
2857 ** too. If it is a ROLLBACK TO, then set the number of deferred
2858 ** constraint violations present in the database to the value stored
2859 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002860 if( p1==SAVEPOINT_RELEASE ){
2861 assert( pSavepoint==db->pSavepoint );
2862 db->pSavepoint = pSavepoint->pNext;
2863 sqlite3DbFree(db, pSavepoint);
2864 if( !isTransaction ){
2865 db->nSavepoint--;
2866 }
dan1da40a32009-09-19 17:00:31 +00002867 }else{
2868 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002869 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002870 }
dand9495cd2011-04-27 12:08:04 +00002871
2872 if( !isTransaction ){
2873 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2874 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2875 }
danielk1977fd7f0452008-12-17 17:30:26 +00002876 }
2877 }
2878
2879 break;
2880}
2881
drh98757152008-01-09 23:04:12 +00002882/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002883**
2884** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002885** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002886** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2887** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002888**
2889** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002890*/
drh9cbf3422008-01-17 16:22:13 +00002891case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002892 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002893 int iRollback;
drh856c1032009-06-02 15:21:42 +00002894 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002895
drh856c1032009-06-02 15:21:42 +00002896 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002897 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002898 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002899 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002900 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002901 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002902 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002903
drh0f198a72012-02-13 16:43:16 +00002904#if 0
drh4f7d3a52013-06-27 23:54:02 +00002905 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002906 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002907 ** still running, and a transaction is active, return an error indicating
2908 ** that the other VMs must complete first.
2909 */
drhad4a4b82008-11-05 16:37:34 +00002910 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2911 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002912 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002913 }else
2914#endif
drh4f7d3a52013-06-27 23:54:02 +00002915 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002916 /* If this instruction implements a COMMIT and other VMs are writing
2917 ** return an error indicating that the other VMs must complete first.
2918 */
2919 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2920 "SQL statements in progress");
2921 rc = SQLITE_BUSY;
2922 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002923 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002924 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002925 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002926 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002927 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002928 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002929 }else{
shane7d3846a2008-12-11 02:58:26 +00002930 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002931 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002932 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002933 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002934 p->rc = rc = SQLITE_BUSY;
2935 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002936 }
danielk19771d850a72004-05-31 08:26:49 +00002937 }
danielk1977bd434552009-03-18 10:33:00 +00002938 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002939 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002940 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002941 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002942 }else{
drh900b31e2007-08-28 02:27:51 +00002943 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002944 }
drh900b31e2007-08-28 02:27:51 +00002945 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002946 }else{
drhf089aa42008-07-08 19:34:06 +00002947 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002948 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002949 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002950 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002951
2952 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002953 }
2954 break;
2955}
2956
drhb22f7c82014-02-06 23:56:27 +00002957/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002958**
drh05a86c52014-02-16 01:55:49 +00002959** Begin a transaction on database P1 if a transaction is not already
2960** active.
2961** If P2 is non-zero, then a write-transaction is started, or if a
2962** read-transaction is already active, it is upgraded to a write-transaction.
2963** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00002964**
drh001bbcb2003-03-19 03:14:00 +00002965** P1 is the index of the database file on which the transaction is
2966** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002967** file used for temporary tables. Indices of 2 or more are used for
2968** attached databases.
drhcabb0812002-09-14 13:47:32 +00002969**
dane0af83a2009-09-08 19:15:01 +00002970** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2971** true (this flag is set if the Vdbe may modify more than one row and may
2972** throw an ABORT exception), a statement transaction may also be opened.
2973** More specifically, a statement transaction is opened iff the database
2974** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002975** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002976** VDBE to be rolled back after an error without having to roll back the
2977** entire transaction. If no error is encountered, the statement transaction
2978** will automatically commit when the VDBE halts.
2979**
drhb22f7c82014-02-06 23:56:27 +00002980** If P5!=0 then this opcode also checks the schema cookie against P3
2981** and the schema generation counter against P4.
2982** The cookie changes its value whenever the database schema changes.
2983** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00002984** and that the current process needs to reread the schema. If the schema
2985** cookie in P3 differs from the schema cookie in the database header or
2986** if the schema generation counter in P4 differs from the current
2987** generation counter, then an SQLITE_SCHEMA error is raised and execution
2988** halts. The sqlite3_step() wrapper function might then reprepare the
2989** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00002990*/
drh9cbf3422008-01-17 16:22:13 +00002991case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002992 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00002993 int iMeta;
2994 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00002995
drh1713afb2013-06-28 01:24:57 +00002996 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00002997 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00002998 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00002999 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh13447bf2013-07-10 13:33:49 +00003000 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3001 rc = SQLITE_READONLY;
3002 goto abort_due_to_error;
3003 }
drh653b82a2009-06-22 11:10:47 +00003004 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003005
danielk197724162fe2004-06-04 06:22:00 +00003006 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003007 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00003008 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00003009 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00003010 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00003011 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003012 }
drh9e9f1bd2009-10-13 15:36:51 +00003013 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003014 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003015 }
dane0af83a2009-09-08 19:15:01 +00003016
3017 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003018 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003019 ){
3020 assert( sqlite3BtreeIsInTrans(pBt) );
3021 if( p->iStatement==0 ){
3022 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3023 db->nStatement++;
3024 p->iStatement = db->nSavepoint + db->nStatement;
3025 }
dana311b802011-04-26 19:21:34 +00003026
drh346506f2011-05-25 01:16:42 +00003027 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003028 if( rc==SQLITE_OK ){
3029 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3030 }
dan1da40a32009-09-19 17:00:31 +00003031
3032 /* Store the current value of the database handles deferred constraint
3033 ** counter. If the statement transaction needs to be rolled back,
3034 ** the value of this counter needs to be restored too. */
3035 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003036 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003037 }
drhb22f7c82014-02-06 23:56:27 +00003038
3039 /* Gather the schema version number for checking */
3040 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3041 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3042 }else{
3043 iGen = iMeta = 0;
3044 }
3045 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3046 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3047 sqlite3DbFree(db, p->zErrMsg);
3048 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3049 /* If the schema-cookie from the database file matches the cookie
3050 ** stored with the in-memory representation of the schema, do
3051 ** not reload the schema from the database file.
3052 **
3053 ** If virtual-tables are in use, this is not just an optimization.
3054 ** Often, v-tables store their data in other SQLite tables, which
3055 ** are queried from within xNext() and other v-table methods using
3056 ** prepared queries. If such a query is out-of-date, we do not want to
3057 ** discard the database schema, as the user code implementing the
3058 ** v-table would have to be ready for the sqlite3_vtab structure itself
3059 ** to be invalidated whenever sqlite3_step() is called from within
3060 ** a v-table method.
3061 */
3062 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3063 sqlite3ResetOneSchema(db, pOp->p1);
3064 }
3065 p->expired = 1;
3066 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003067 }
drh5e00f6c2001-09-13 13:46:56 +00003068 break;
3069}
3070
drhb1fdb2a2008-01-05 04:06:03 +00003071/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003072**
drh9cbf3422008-01-17 16:22:13 +00003073** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003074** P3==1 is the schema version. P3==2 is the database format.
3075** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003076** the main database file and P1==1 is the database file used to store
3077** temporary tables.
drh4a324312001-12-21 14:30:42 +00003078**
drh50e5dad2001-09-15 00:57:28 +00003079** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003080** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003081** executing this instruction.
3082*/
drh4c583122008-01-04 22:01:03 +00003083case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003084 int iMeta;
drh856c1032009-06-02 15:21:42 +00003085 int iDb;
3086 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003087
drh1713afb2013-06-28 01:24:57 +00003088 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003089 iDb = pOp->p1;
3090 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003091 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003092 assert( iDb>=0 && iDb<db->nDb );
3093 assert( db->aDb[iDb].pBt!=0 );
drha7ab6d82014-07-21 15:44:39 +00003094 assert( DbMaskTest(p->btreeMask, iDb) );
danielk19770d19f7a2009-06-03 11:25:07 +00003095
danielk1977602b4662009-07-02 07:47:33 +00003096 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003097 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003098 break;
3099}
3100
drh98757152008-01-09 23:04:12 +00003101/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003102**
drh98757152008-01-09 23:04:12 +00003103** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003104** into cookie number P2 of database P1. P2==1 is the schema version.
3105** P2==2 is the database format. P2==3 is the recommended pager cache
3106** size, and so forth. P1==0 is the main database file and P1==1 is the
3107** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003108**
3109** A transaction must be started before executing this opcode.
3110*/
drh9cbf3422008-01-17 16:22:13 +00003111case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003112 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003113 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003114 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003115 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00003116 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003117 pDb = &db->aDb[pOp->p1];
3118 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003119 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003120 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003121 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003122 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003123 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3124 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003125 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003126 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003127 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003128 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003129 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003130 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003131 }
drhfd426c62006-01-30 15:34:22 +00003132 if( pOp->p1==1 ){
3133 /* Invalidate all prepared statements whenever the TEMP database
3134 ** schema is changed. Ticket #1644 */
3135 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003136 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003137 }
drh50e5dad2001-09-15 00:57:28 +00003138 break;
3139}
3140
drh98757152008-01-09 23:04:12 +00003141/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003142** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003143**
drhecdc7532001-09-23 02:35:53 +00003144** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003145** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003146** P3==0 means the main database, P3==1 means the database used for
3147** temporary tables, and P3>1 means used the corresponding attached
3148** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003149** values need not be contiguous but all P1 values should be small integers.
3150** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003151**
drh98757152008-01-09 23:04:12 +00003152** If P5!=0 then use the content of register P2 as the root page, not
3153** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003154**
drhb19a2bc2001-09-16 00:13:26 +00003155** There will be a read lock on the database whenever there is an
3156** open cursor. If the database was unlocked prior to this instruction
3157** then a read lock is acquired as part of this instruction. A read
3158** lock allows other processes to read the database but prohibits
3159** any other process from modifying the database. The read lock is
3160** released when all cursors are closed. If this instruction attempts
3161** to get a read lock but fails, the script terminates with an
3162** SQLITE_BUSY error code.
3163**
danielk1977d336e222009-02-20 10:58:41 +00003164** The P4 value may be either an integer (P4_INT32) or a pointer to
3165** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3166** structure, then said structure defines the content and collating
3167** sequence of the index being opened. Otherwise, if P4 is an integer
3168** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003169**
drh35263192014-07-22 20:02:19 +00003170** See also: OpenWrite, ReopenIdx
3171*/
3172/* Opcode: ReopenIdx P1 P2 P3 P4 P5
3173** Synopsis: root=P2 iDb=P3
3174**
3175** The ReopenIdx opcode works exactly like ReadOpen except that it first
3176** checks to see if the cursor on P1 is already open with a root page
3177** number of P2 and if it is this opcode becomes a no-op. In other words,
3178** if the cursor is already open, do not reopen it.
3179**
3180** The ReopenIdx opcode may only be used with P5==0 and with P4 being
3181** a P4_KEYINFO object. Furthermore, the P3 value must be the same as
3182** every other ReopenIdx or OpenRead for the same cursor number.
3183**
3184** See the OpenRead opcode documentation for additional information.
drh5e00f6c2001-09-13 13:46:56 +00003185*/
drh98757152008-01-09 23:04:12 +00003186/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003187** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003188**
3189** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003190** page is P2. Or if P5!=0 use the content of register P2 to find the
3191** root page.
drhecdc7532001-09-23 02:35:53 +00003192**
danielk1977d336e222009-02-20 10:58:41 +00003193** The P4 value may be either an integer (P4_INT32) or a pointer to
3194** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3195** structure, then said structure defines the content and collating
3196** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003197** value, it is set to the number of columns in the table, or to the
3198** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003199**
drh001bbcb2003-03-19 03:14:00 +00003200** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003201** in read/write mode. For a given table, there can be one or more read-only
3202** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003203**
drh001bbcb2003-03-19 03:14:00 +00003204** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003205*/
drh35263192014-07-22 20:02:19 +00003206case OP_ReopenIdx: {
3207 VdbeCursor *pCur;
3208
3209 assert( pOp->p5==0 );
3210 assert( pOp->p4type==P4_KEYINFO );
3211 pCur = p->apCsr[pOp->p1];
drhe8f2c9d2014-08-06 17:49:13 +00003212 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){
drh35263192014-07-22 20:02:19 +00003213 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */
3214 break;
3215 }
3216 /* If the cursor is not currently open or is open on a different
3217 ** index, then fall through into OP_OpenRead to force a reopen */
3218}
drh9cbf3422008-01-17 16:22:13 +00003219case OP_OpenRead:
3220case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003221 int nField;
3222 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003223 int p2;
3224 int iDb;
drhf57b3392001-10-08 13:22:32 +00003225 int wrFlag;
3226 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003227 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003228 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003229
dan428c2182012-08-06 18:50:11 +00003230 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3231 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003232 assert( p->bIsReader );
drh35263192014-07-22 20:02:19 +00003233 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx
3234 || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003235
danfa401de2009-10-16 14:55:03 +00003236 if( p->expired ){
3237 rc = SQLITE_ABORT;
3238 break;
3239 }
3240
drh856c1032009-06-02 15:21:42 +00003241 nField = 0;
3242 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003243 p2 = pOp->p2;
3244 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003245 assert( iDb>=0 && iDb<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00003246 assert( DbMaskTest(p->btreeMask, iDb) );
drhd946db02005-12-29 19:23:06 +00003247 pDb = &db->aDb[iDb];
3248 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003249 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003250 if( pOp->opcode==OP_OpenWrite ){
3251 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003252 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003253 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3254 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003255 }
3256 }else{
3257 wrFlag = 0;
3258 }
dan428c2182012-08-06 18:50:11 +00003259 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003260 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003261 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003262 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003263 assert( memIsValid(pIn2) );
3264 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003265 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003266 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003267 /* The p2 value always comes from a prior OP_CreateTable opcode and
3268 ** that opcode will always set the p2 value to 2 or more or else fail.
3269 ** If there were a failure, the prepared statement would have halted
3270 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003271 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003272 rc = SQLITE_CORRUPT_BKPT;
3273 goto abort_due_to_error;
3274 }
drh5edc3122001-09-13 21:53:09 +00003275 }
danielk1977d336e222009-02-20 10:58:41 +00003276 if( pOp->p4type==P4_KEYINFO ){
3277 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003278 assert( pKeyInfo->enc==ENC(db) );
3279 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003280 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003281 }else if( pOp->p4type==P4_INT32 ){
3282 nField = pOp->p4.i;
3283 }
drh653b82a2009-06-22 11:10:47 +00003284 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003285 assert( nField>=0 );
3286 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drh653b82a2009-06-22 11:10:47 +00003287 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003288 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003289 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003290 pCur->isOrdered = 1;
drh35263192014-07-22 20:02:19 +00003291 pCur->pgnoRoot = p2;
danielk1977d336e222009-02-20 10:58:41 +00003292 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3293 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003294 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3295 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003296
drh14da87f2013-11-20 21:51:33 +00003297 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003298 ** SQLite used to check if the root-page flags were sane at this point
3299 ** and report database corruption if they were not, but this check has
3300 ** since moved into the btree layer. */
3301 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drh5e00f6c2001-09-13 13:46:56 +00003302 break;
3303}
3304
drh2a5d9902011-08-26 00:34:45 +00003305/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003306** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003307**
drhb9bb7c12006-06-11 23:41:55 +00003308** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003309** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003310** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003311** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003312**
drh25d3adb2010-04-05 15:11:08 +00003313** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003314** The cursor points to a BTree table if P4==0 and to a BTree index
3315** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003316** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003317**
drh2a5d9902011-08-26 00:34:45 +00003318** The P5 parameter can be a mask of the BTREE_* flags defined
3319** in btree.h. These flags control aspects of the operation of
3320** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3321** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003322*/
drha21a64d2010-04-06 22:33:55 +00003323/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003324** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003325**
3326** This opcode works the same as OP_OpenEphemeral. It has a
3327** different name to distinguish its use. Tables created using
3328** by this opcode will be used for automatically created transient
3329** indices in joins.
3330*/
3331case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003332case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003333 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003334 KeyInfo *pKeyInfo;
3335
drhd4187c72010-08-30 22:15:45 +00003336 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003337 SQLITE_OPEN_READWRITE |
3338 SQLITE_OPEN_CREATE |
3339 SQLITE_OPEN_EXCLUSIVE |
3340 SQLITE_OPEN_DELETEONCLOSE |
3341 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003342 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003343 assert( pOp->p2>=0 );
drh653b82a2009-06-22 11:10:47 +00003344 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003345 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003346 pCx->nullRow = 1;
drh079a3072014-03-19 14:10:55 +00003347 pCx->isEphemeral = 1;
dan689ab892011-08-12 15:02:00 +00003348 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3349 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003350 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003351 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003352 }
3353 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003354 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003355 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003356 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003357 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003358 */
drh41e13e12013-11-07 14:09:39 +00003359 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003360 int pgno;
drh66a51672008-01-03 00:01:23 +00003361 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003362 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003363 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003364 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003365 assert( pKeyInfo->db==db );
3366 assert( pKeyInfo->enc==ENC(db) );
3367 pCx->pKeyInfo = pKeyInfo;
3368 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00003369 }
drhf0863fe2005-06-12 21:35:51 +00003370 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003371 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003372 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003373 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003374 }
drh5e00f6c2001-09-13 13:46:56 +00003375 }
drhd4187c72010-08-30 22:15:45 +00003376 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003377 break;
3378}
3379
danfad9f9a2014-04-01 18:41:51 +00003380/* Opcode: SorterOpen P1 P2 P3 P4 *
dan5134d132011-09-02 10:31:11 +00003381**
3382** This opcode works like OP_OpenEphemeral except that it opens
3383** a transient index that is specifically designed to sort large
3384** tables using an external merge-sort algorithm.
danfad9f9a2014-04-01 18:41:51 +00003385**
3386** If argument P3 is non-zero, then it indicates that the sorter may
3387** assume that a stable sort considering the first P3 fields of each
3388** key is sufficient to produce the required results.
dan5134d132011-09-02 10:31:11 +00003389*/
drhca892a72011-09-03 00:17:51 +00003390case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003391 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003392
drh399af1d2013-11-20 17:25:55 +00003393 assert( pOp->p1>=0 );
3394 assert( pOp->p2>=0 );
dan5134d132011-09-02 10:31:11 +00003395 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3396 if( pCx==0 ) goto no_mem;
3397 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003398 assert( pCx->pKeyInfo->db==db );
3399 assert( pCx->pKeyInfo->enc==ENC(db) );
danfad9f9a2014-04-01 18:41:51 +00003400 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003401 break;
3402}
3403
dan78d58432014-03-25 15:04:07 +00003404/* Opcode: SequenceTest P1 P2 * * *
3405** Synopsis: if( cursor[P1].ctr++ ) pc = P2
3406**
3407** P1 is a sorter cursor. If the sequence counter is currently zero, jump
3408** to P2. Regardless of whether or not the jump is taken, increment the
3409** the sequence value.
3410*/
3411case OP_SequenceTest: {
3412 VdbeCursor *pC;
3413 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3414 pC = p->apCsr[pOp->p1];
3415 assert( pC->pSorter );
3416 if( (pC->seqCount++)==0 ){
3417 pc = pOp->p2 - 1;
3418 }
3419 break;
3420}
3421
drh5f612292014-02-08 23:20:32 +00003422/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003423** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003424**
3425** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003426** row of data. The content of that one row is the content of memory
3427** register P2. In other words, cursor P1 becomes an alias for the
3428** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003429**
drh2d8d7ce2010-02-15 15:17:05 +00003430** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003431** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003432** individual columns using the OP_Column opcode. The OP_Column opcode
3433** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003434**
3435** P3 is the number of fields in the records that will be stored by
3436** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003437*/
drh9cbf3422008-01-17 16:22:13 +00003438case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003439 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003440
drh653b82a2009-06-22 11:10:47 +00003441 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003442 assert( pOp->p3>=0 );
drh653b82a2009-06-22 11:10:47 +00003443 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003444 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003445 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003446 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003447 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003448 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003449 break;
3450}
3451
drh98757152008-01-09 23:04:12 +00003452/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003453**
3454** Close a cursor previously opened as P1. If P1 is not
3455** currently open, this instruction is a no-op.
3456*/
drh9cbf3422008-01-17 16:22:13 +00003457case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003458 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3459 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3460 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003461 break;
3462}
3463
drh8af3f772014-07-25 18:01:06 +00003464/* Opcode: SeekGE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003465** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003466**
danielk1977b790c6c2008-04-18 10:25:24 +00003467** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003468** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003469** to an SQL index, then P3 is the first in an array of P4 registers
3470** that are used as an unpacked index key.
3471**
3472** Reposition cursor P1 so that it points to the smallest entry that
3473** is greater than or equal to the key value. If there are no records
3474** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003475**
drh8af3f772014-07-25 18:01:06 +00003476** This opcode leaves the cursor configured to move in forward order,
drhbc5cf382014-08-06 01:08:07 +00003477** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003478** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003479**
drh935850e2014-05-24 17:15:15 +00003480** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003481*/
drh8af3f772014-07-25 18:01:06 +00003482/* Opcode: SeekGT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003483** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003484**
danielk1977b790c6c2008-04-18 10:25:24 +00003485** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003486** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003487** to an SQL index, then P3 is the first in an array of P4 registers
3488** that are used as an unpacked index key.
3489**
3490** Reposition cursor P1 so that it points to the smallest entry that
3491** is greater than the key value. If there are no records greater than
3492** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003493**
drh8af3f772014-07-25 18:01:06 +00003494** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00003495** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003496** configured to use Next, not Prev.
drh8af3f772014-07-25 18:01:06 +00003497**
drh935850e2014-05-24 17:15:15 +00003498** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003499*/
drh8af3f772014-07-25 18:01:06 +00003500/* Opcode: SeekLT P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003501** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003502**
danielk1977b790c6c2008-04-18 10:25:24 +00003503** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003504** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003505** to an SQL index, then P3 is the first in an array of P4 registers
3506** that are used as an unpacked index key.
3507**
3508** Reposition cursor P1 so that it points to the largest entry that
3509** is less than the key value. If there are no records less than
3510** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003511**
drh8af3f772014-07-25 18:01:06 +00003512** This opcode leaves the cursor configured to move in reverse order,
3513** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003514** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003515**
drh935850e2014-05-24 17:15:15 +00003516** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003517*/
drh8af3f772014-07-25 18:01:06 +00003518/* Opcode: SeekLE P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003519** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003520**
danielk1977b790c6c2008-04-18 10:25:24 +00003521** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003522** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003523** to an SQL index, then P3 is the first in an array of P4 registers
3524** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003525**
danielk1977b790c6c2008-04-18 10:25:24 +00003526** Reposition cursor P1 so that it points to the largest entry that
3527** is less than or equal to the key value. If there are no records
3528** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003529**
drh8af3f772014-07-25 18:01:06 +00003530** This opcode leaves the cursor configured to move in reverse order,
3531** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00003532** configured to use Prev, not Next.
drh8af3f772014-07-25 18:01:06 +00003533**
drh935850e2014-05-24 17:15:15 +00003534** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003535*/
drh4a1d3652014-02-14 15:13:36 +00003536case OP_SeekLT: /* jump, in3 */
3537case OP_SeekLE: /* jump, in3 */
3538case OP_SeekGE: /* jump, in3 */
3539case OP_SeekGT: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003540 int res;
3541 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003542 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003543 UnpackedRecord r;
3544 int nField;
3545 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003546
drh653b82a2009-06-22 11:10:47 +00003547 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003548 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003549 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003550 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003551 assert( pC->pseudoTableReg==0 );
drh4a1d3652014-02-14 15:13:36 +00003552 assert( OP_SeekLE == OP_SeekLT+1 );
3553 assert( OP_SeekGE == OP_SeekLT+2 );
3554 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003555 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00003556 assert( pC->pCursor!=0 );
3557 oc = pOp->opcode;
3558 pC->nullRow = 0;
drh8af3f772014-07-25 18:01:06 +00003559#ifdef SQLITE_DEBUG
3560 pC->seekOp = pOp->opcode;
3561#endif
drh3da046d2013-11-11 03:24:11 +00003562 if( pC->isTable ){
3563 /* The input value in P3 might be of any type: integer, real, string,
3564 ** blob, or NULL. But it needs to be an integer before we can do
peter.d.reid60ec9142014-09-06 16:39:46 +00003565 ** the seek, so convert it. */
drh3da046d2013-11-11 03:24:11 +00003566 pIn3 = &aMem[pOp->p3];
drh11a6eee2014-09-19 22:01:54 +00003567 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
drhbd9507c2014-08-23 17:21:37 +00003568 applyNumericAffinity(pIn3, 0);
3569 }
drh3da046d2013-11-11 03:24:11 +00003570 iKey = sqlite3VdbeIntValue(pIn3);
drh959403f2008-12-12 17:56:16 +00003571
drh3da046d2013-11-11 03:24:11 +00003572 /* If the P3 value could not be converted into an integer without
3573 ** loss of information, then special processing is required... */
3574 if( (pIn3->flags & MEM_Int)==0 ){
3575 if( (pIn3->flags & MEM_Real)==0 ){
3576 /* If the P3 value cannot be converted into any kind of a number,
3577 ** then the seek is not possible, so jump to P2 */
drh688852a2014-02-17 22:40:43 +00003578 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003579 break;
3580 }
drh959403f2008-12-12 17:56:16 +00003581
danaa1776f2013-11-26 18:22:59 +00003582 /* If the approximation iKey is larger than the actual real search
3583 ** term, substitute >= for > and < for <=. e.g. if the search term
3584 ** is 4.9 and the integer approximation 5:
3585 **
3586 ** (x > 4.9) -> (x >= 5)
3587 ** (x <= 4.9) -> (x < 5)
3588 */
drh74eaba42014-09-18 17:52:15 +00003589 if( pIn3->u.r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003590 assert( OP_SeekGE==(OP_SeekGT-1) );
3591 assert( OP_SeekLT==(OP_SeekLE-1) );
3592 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3593 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003594 }
3595
3596 /* If the approximation iKey is smaller than the actual real search
3597 ** term, substitute <= for < and > for >=. */
drh74eaba42014-09-18 17:52:15 +00003598 else if( pIn3->u.r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003599 assert( OP_SeekLE==(OP_SeekLT+1) );
3600 assert( OP_SeekGT==(OP_SeekGE+1) );
3601 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3602 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003603 }
drh3da046d2013-11-11 03:24:11 +00003604 }
3605 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003606 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003607 if( rc!=SQLITE_OK ){
3608 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003609 }
drhaa736092009-06-22 00:55:30 +00003610 }else{
drh3da046d2013-11-11 03:24:11 +00003611 nField = pOp->p4.i;
3612 assert( pOp->p4type==P4_INT32 );
3613 assert( nField>0 );
3614 r.pKeyInfo = pC->pKeyInfo;
3615 r.nField = (u16)nField;
3616
3617 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003618 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
dan1fed5da2014-02-25 21:01:25 +00003619 ** r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00003620 ** }else{
dan1fed5da2014-02-25 21:01:25 +00003621 ** r.default_rc = +1;
drh3da046d2013-11-11 03:24:11 +00003622 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003623 */
dan1fed5da2014-02-25 21:01:25 +00003624 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
3625 assert( oc!=OP_SeekGT || r.default_rc==-1 );
3626 assert( oc!=OP_SeekLE || r.default_rc==-1 );
3627 assert( oc!=OP_SeekGE || r.default_rc==+1 );
3628 assert( oc!=OP_SeekLT || r.default_rc==+1 );
drh3da046d2013-11-11 03:24:11 +00003629
3630 r.aMem = &aMem[pOp->p3];
3631#ifdef SQLITE_DEBUG
3632 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3633#endif
3634 ExpandBlob(r.aMem);
3635 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
3636 if( rc!=SQLITE_OK ){
3637 goto abort_due_to_error;
3638 }
drh3da046d2013-11-11 03:24:11 +00003639 }
3640 pC->deferredMoveto = 0;
3641 pC->cacheStatus = CACHE_STALE;
3642#ifdef SQLITE_TEST
3643 sqlite3_search_count++;
3644#endif
drh4a1d3652014-02-14 15:13:36 +00003645 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3646 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003647 res = 0;
drh3da046d2013-11-11 03:24:11 +00003648 rc = sqlite3BtreeNext(pC->pCursor, &res);
3649 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003650 }else{
3651 res = 0;
3652 }
3653 }else{
drh4a1d3652014-02-14 15:13:36 +00003654 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3655 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003656 res = 0;
drh3da046d2013-11-11 03:24:11 +00003657 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3658 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh3da046d2013-11-11 03:24:11 +00003659 }else{
3660 /* res might be negative because the table is empty. Check to
3661 ** see if this is the case.
3662 */
3663 res = sqlite3BtreeEof(pC->pCursor);
3664 }
3665 }
3666 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003667 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003668 if( res ){
danielk1977f7b9d662008-06-23 18:49:43 +00003669 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003670 }
drh5e00f6c2001-09-13 13:46:56 +00003671 break;
3672}
3673
drh959403f2008-12-12 17:56:16 +00003674/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003675** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003676**
3677** P1 is an open table cursor and P2 is a rowid integer. Arrange
3678** for P1 to move so that it points to the rowid given by P2.
3679**
3680** This is actually a deferred seek. Nothing actually happens until
3681** the cursor is used to read a record. That way, if no reads
3682** occur, no unnecessary I/O happens.
3683*/
3684case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003685 VdbeCursor *pC;
3686
drh653b82a2009-06-22 11:10:47 +00003687 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3688 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003689 assert( pC!=0 );
drh3da046d2013-11-11 03:24:11 +00003690 assert( pC->pCursor!=0 );
3691 assert( pC->isTable );
3692 pC->nullRow = 0;
3693 pIn2 = &aMem[pOp->p2];
3694 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
drh3da046d2013-11-11 03:24:11 +00003695 pC->deferredMoveto = 1;
drh959403f2008-12-12 17:56:16 +00003696 break;
3697}
3698
3699
drh8cff69d2009-11-12 19:59:44 +00003700/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003701** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003702**
drh8cff69d2009-11-12 19:59:44 +00003703** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3704** P4>0 then register P3 is the first of P4 registers that form an unpacked
3705** record.
3706**
3707** Cursor P1 is on an index btree. If the record identified by P3 and P4
3708** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003709** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003710**
drhcefc87f2014-08-01 01:40:33 +00003711** This operation leaves the cursor in a state where it can be
3712** advanced in the forward direction. The Next instruction will work,
3713** but not the Prev instruction.
drh8af3f772014-07-25 18:01:06 +00003714**
drh6f225d02013-10-26 13:36:51 +00003715** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003716*/
drh8cff69d2009-11-12 19:59:44 +00003717/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003718** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003719**
drh8cff69d2009-11-12 19:59:44 +00003720** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3721** P4>0 then register P3 is the first of P4 registers that form an unpacked
3722** record.
3723**
3724** Cursor P1 is on an index btree. If the record identified by P3 and P4
3725** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3726** does contain an entry whose prefix matches the P3/P4 record then control
3727** falls through to the next instruction and P1 is left pointing at the
3728** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003729**
drh8af3f772014-07-25 18:01:06 +00003730** This operation leaves the cursor in a state where it cannot be
3731** advanced in either direction. In other words, the Next and Prev
3732** opcodes do not work after this operation.
3733**
drh6f225d02013-10-26 13:36:51 +00003734** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003735*/
drh6f225d02013-10-26 13:36:51 +00003736/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003737** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003738**
3739** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3740** P4>0 then register P3 is the first of P4 registers that form an unpacked
3741** record.
3742**
3743** Cursor P1 is on an index btree. If the record identified by P3 and P4
3744** contains any NULL value, jump immediately to P2. If all terms of the
3745** record are not-NULL then a check is done to determine if any row in the
3746** P1 index btree has a matching key prefix. If there are no matches, jump
3747** immediately to P2. If there is a match, fall through and leave the P1
3748** cursor pointing to the matching row.
3749**
3750** This opcode is similar to OP_NotFound with the exceptions that the
3751** branch is always taken if any part of the search key input is NULL.
3752**
drh8af3f772014-07-25 18:01:06 +00003753** This operation leaves the cursor in a state where it cannot be
3754** advanced in either direction. In other words, the Next and Prev
3755** opcodes do not work after this operation.
3756**
drh6f225d02013-10-26 13:36:51 +00003757** See also: NotFound, Found, NotExists
3758*/
3759case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003760case OP_NotFound: /* jump, in3 */
3761case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003762 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00003763 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003764 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003765 int res;
dan03e9cfc2011-09-05 14:20:27 +00003766 char *pFree;
drh856c1032009-06-02 15:21:42 +00003767 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003768 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003769 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003770
dan0ff297e2009-09-25 17:03:14 +00003771#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003772 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003773#endif
3774
drhaa736092009-06-22 00:55:30 +00003775 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003776 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003777 pC = p->apCsr[pOp->p1];
3778 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003779#ifdef SQLITE_DEBUG
drhcefc87f2014-08-01 01:40:33 +00003780 pC->seekOp = pOp->opcode;
drh8af3f772014-07-25 18:01:06 +00003781#endif
drh3c657212009-11-17 23:59:58 +00003782 pIn3 = &aMem[pOp->p3];
drh3da046d2013-11-11 03:24:11 +00003783 assert( pC->pCursor!=0 );
3784 assert( pC->isTable==0 );
drha9ab4812013-12-11 11:00:44 +00003785 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
drh3da046d2013-11-11 03:24:11 +00003786 if( pOp->p4.i>0 ){
3787 r.pKeyInfo = pC->pKeyInfo;
3788 r.nField = (u16)pOp->p4.i;
3789 r.aMem = pIn3;
drh826af372014-02-08 19:12:21 +00003790 for(ii=0; ii<r.nField; ii++){
3791 assert( memIsValid(&r.aMem[ii]) );
3792 ExpandBlob(&r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003793#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00003794 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003795#endif
drh826af372014-02-08 19:12:21 +00003796 }
drh3da046d2013-11-11 03:24:11 +00003797 pIdxKey = &r;
3798 }else{
3799 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3800 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3801 );
3802 if( pIdxKey==0 ) goto no_mem;
3803 assert( pIn3->flags & MEM_Blob );
3804 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
3805 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh3da046d2013-11-11 03:24:11 +00003806 }
dan1fed5da2014-02-25 21:01:25 +00003807 pIdxKey->default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00003808 if( pOp->opcode==OP_NoConflict ){
3809 /* For the OP_NoConflict opcode, take the jump if any of the
3810 ** input fields are NULL, since any key with a NULL will not
3811 ** conflict */
3812 for(ii=0; ii<r.nField; ii++){
3813 if( r.aMem[ii].flags & MEM_Null ){
drh688852a2014-02-17 22:40:43 +00003814 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003815 break;
drh6f225d02013-10-26 13:36:51 +00003816 }
3817 }
drh5e00f6c2001-09-13 13:46:56 +00003818 }
drh3da046d2013-11-11 03:24:11 +00003819 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3820 if( pOp->p4.i==0 ){
3821 sqlite3DbFree(db, pFree);
3822 }
3823 if( rc!=SQLITE_OK ){
3824 break;
3825 }
drh1fd522f2013-11-21 00:10:35 +00003826 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003827 alreadyExists = (res==0);
3828 pC->nullRow = 1-alreadyExists;
3829 pC->deferredMoveto = 0;
3830 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003831 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00003832 VdbeBranchTaken(alreadyExists!=0,2);
drh5e00f6c2001-09-13 13:46:56 +00003833 if( alreadyExists ) pc = pOp->p2 - 1;
3834 }else{
drh688852a2014-02-17 22:40:43 +00003835 VdbeBranchTaken(alreadyExists==0,2);
drh5e00f6c2001-09-13 13:46:56 +00003836 if( !alreadyExists ) pc = pOp->p2 - 1;
3837 }
drh5e00f6c2001-09-13 13:46:56 +00003838 break;
3839}
3840
drh9cbf3422008-01-17 16:22:13 +00003841/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003842** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00003843**
drh261c02d2013-10-25 14:46:15 +00003844** P1 is the index of a cursor open on an SQL table btree (with integer
3845** keys). P3 is an integer rowid. If P1 does not contain a record with
3846** rowid P3 then jump immediately to P2. If P1 does contain a record
3847** with rowid P3 then leave the cursor pointing at that record and fall
3848** through to the next instruction.
drh6b125452002-01-28 15:53:03 +00003849**
drh261c02d2013-10-25 14:46:15 +00003850** The OP_NotFound opcode performs the same operation on index btrees
3851** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00003852**
drh8af3f772014-07-25 18:01:06 +00003853** This opcode leaves the cursor in a state where it cannot be advanced
3854** in either direction. In other words, the Next and Prev opcodes will
3855** not work following this opcode.
3856**
drh11e85272013-10-26 15:40:48 +00003857** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00003858*/
drh9cbf3422008-01-17 16:22:13 +00003859case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003860 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003861 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003862 int res;
3863 u64 iKey;
3864
drh3c657212009-11-17 23:59:58 +00003865 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003866 assert( pIn3->flags & MEM_Int );
3867 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3868 pC = p->apCsr[pOp->p1];
3869 assert( pC!=0 );
drh8af3f772014-07-25 18:01:06 +00003870#ifdef SQLITE_DEBUG
3871 pC->seekOp = 0;
3872#endif
drhaa736092009-06-22 00:55:30 +00003873 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003874 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003875 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00003876 assert( pCrsr!=0 );
3877 res = 0;
3878 iKey = pIn3->u.i;
3879 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drhb53a5a92014-10-12 22:37:22 +00003880 pC->movetoTarget = iKey; /* Used by OP_Delete */
drh3da046d2013-11-11 03:24:11 +00003881 pC->nullRow = 0;
3882 pC->cacheStatus = CACHE_STALE;
3883 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00003884 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003885 if( res!=0 ){
danielk1977f7b9d662008-06-23 18:49:43 +00003886 pc = pOp->p2 - 1;
drh6b125452002-01-28 15:53:03 +00003887 }
drh1fd522f2013-11-21 00:10:35 +00003888 pC->seekResult = res;
drh6b125452002-01-28 15:53:03 +00003889 break;
3890}
3891
drh4c583122008-01-04 22:01:03 +00003892/* Opcode: Sequence P1 P2 * * *
drh079a3072014-03-19 14:10:55 +00003893** Synopsis: r[P2]=cursor[P1].ctr++
drh4db38a72005-09-01 12:16:28 +00003894**
drh4c583122008-01-04 22:01:03 +00003895** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003896** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003897** The sequence number on the cursor is incremented after this
3898** instruction.
drh4db38a72005-09-01 12:16:28 +00003899*/
drh4c583122008-01-04 22:01:03 +00003900case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003901 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3902 assert( p->apCsr[pOp->p1]!=0 );
3903 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003904 break;
3905}
3906
3907
drh98757152008-01-09 23:04:12 +00003908/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003909** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00003910**
drhf0863fe2005-06-12 21:35:51 +00003911** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003912** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003913** table that cursor P1 points to. The new record number is written
3914** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003915**
dan76d462e2009-08-30 11:42:51 +00003916** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3917** the largest previously generated record number. No new record numbers are
3918** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003919** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003920** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003921** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003922*/
drh4c583122008-01-04 22:01:03 +00003923case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003924 i64 v; /* The new rowid */
3925 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3926 int res; /* Result of an sqlite3BtreeLast() */
3927 int cnt; /* Counter to limit the number of searches */
3928 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003929 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003930
drh856c1032009-06-02 15:21:42 +00003931 v = 0;
3932 res = 0;
drhaa736092009-06-22 00:55:30 +00003933 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3934 pC = p->apCsr[pOp->p1];
3935 assert( pC!=0 );
3936 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003937 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003938 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003939 /* The next rowid or record number (different terms for the same
3940 ** thing) is obtained in a two-step algorithm.
3941 **
3942 ** First we attempt to find the largest existing rowid and add one
3943 ** to that. But if the largest existing rowid is already the maximum
3944 ** positive integer, we have to fall through to the second
3945 ** probabilistic algorithm
3946 **
3947 ** The second algorithm is to select a rowid at random and see if
3948 ** it already exists in the table. If it does not exist, we have
3949 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003950 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003951 */
drhaa736092009-06-22 00:55:30 +00003952 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003953
drh75f86a42005-02-17 00:03:06 +00003954#ifdef SQLITE_32BIT_ROWID
3955# define MAX_ROWID 0x7fffffff
3956#else
drhfe2093d2005-01-20 22:48:47 +00003957 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3958 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3959 ** to provide the constant while making all compilers happy.
3960 */
danielk197764202cf2008-11-17 15:31:47 +00003961# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003962#endif
drhfe2093d2005-01-20 22:48:47 +00003963
drh5cf8e8c2002-02-19 22:42:05 +00003964 if( !pC->useRandomRowid ){
drhe0670b62014-02-12 21:31:12 +00003965 rc = sqlite3BtreeLast(pC->pCursor, &res);
3966 if( rc!=SQLITE_OK ){
3967 goto abort_due_to_error;
3968 }
3969 if( res ){
3970 v = 1; /* IMP: R-61914-48074 */
3971 }else{
3972 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
3973 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3974 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
3975 if( v>=MAX_ROWID ){
3976 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003977 }else{
drhe0670b62014-02-12 21:31:12 +00003978 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00003979 }
drh3fc190c2001-09-14 03:24:23 +00003980 }
drhe0670b62014-02-12 21:31:12 +00003981 }
drh205f48e2004-11-05 00:43:11 +00003982
3983#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00003984 if( pOp->p3 ){
3985 /* Assert that P3 is a valid memory cell. */
3986 assert( pOp->p3>0 );
3987 if( p->pFrame ){
3988 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003989 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00003990 assert( pOp->p3<=pFrame->nMem );
3991 pMem = &pFrame->aMem[pOp->p3];
3992 }else{
3993 /* Assert that P3 is a valid memory cell. */
3994 assert( pOp->p3<=(p->nMem-p->nCursor) );
3995 pMem = &aMem[pOp->p3];
3996 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00003997 }
drhe0670b62014-02-12 21:31:12 +00003998 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00003999
drhe0670b62014-02-12 21:31:12 +00004000 REGISTER_TRACE(pOp->p3, pMem);
4001 sqlite3VdbeMemIntegerify(pMem);
4002 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
4003 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
4004 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
4005 goto abort_due_to_error;
4006 }
4007 if( v<pMem->u.i+1 ){
4008 v = pMem->u.i + 1;
4009 }
4010 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00004011 }
drhe0670b62014-02-12 21:31:12 +00004012#endif
drh5cf8e8c2002-02-19 22:42:05 +00004013 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00004014 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00004015 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00004016 ** engine starts picking positive candidate ROWIDs at random until
4017 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004018 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4019 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00004020 cnt = 0;
drh2c4dc632014-09-25 12:31:28 +00004021 do{
4022 sqlite3_randomness(sizeof(v), &v);
drhd8633462014-09-25 17:42:41 +00004023 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */
drh2c4dc632014-09-25 12:31:28 +00004024 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
drh748a52c2010-09-01 11:50:08 +00004025 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004026 && (res==0)
drh2c4dc632014-09-25 12:31:28 +00004027 && (++cnt<100));
drhaa736092009-06-22 00:55:30 +00004028 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004029 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004030 goto abort_due_to_error;
4031 }
drh748a52c2010-09-01 11:50:08 +00004032 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004033 }
drha11846b2004-01-07 18:52:56 +00004034 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004035 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004036 }
drh4c583122008-01-04 22:01:03 +00004037 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004038 break;
4039}
4040
danielk19771f4aa332008-01-03 09:51:55 +00004041/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004042** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004043**
jplyon5a564222003-06-02 06:15:58 +00004044** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004045** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004046** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004047** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004048** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004049**
danielk19771f4aa332008-01-03 09:51:55 +00004050** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4051** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004052** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004053** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004054**
drh3e9ca092009-09-08 01:14:48 +00004055** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4056** the last seek operation (OP_NotExists) was a success, then this
4057** operation will not attempt to find the appropriate row before doing
4058** the insert but will instead overwrite the row that the cursor is
4059** currently pointing to. Presumably, the prior OP_NotExists opcode
4060** has already positioned the cursor correctly. This is an optimization
4061** that boosts performance by avoiding redundant seeks.
4062**
4063** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4064** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4065** is part of an INSERT operation. The difference is only important to
4066** the update hook.
4067**
drh66a51672008-01-03 00:01:23 +00004068** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004069** may be NULL. If it is not NULL, then the update-hook
4070** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4071**
drh93aed5a2008-01-16 17:46:38 +00004072** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4073** allocated, then ownership of P2 is transferred to the pseudo-cursor
4074** and register P2 becomes ephemeral. If the cursor is changed, the
4075** value of register P2 will then change. Make sure this does not
4076** cause any problems.)
4077**
drhf0863fe2005-06-12 21:35:51 +00004078** This instruction only works on tables. The equivalent instruction
4079** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004080*/
drhe05c9292009-10-29 13:48:10 +00004081/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004082** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004083**
4084** This works exactly like OP_Insert except that the key is the
4085** integer value P3, not the value of the integer stored in register P3.
4086*/
4087case OP_Insert:
4088case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004089 Mem *pData; /* MEM cell holding data for the record to be inserted */
4090 Mem *pKey; /* MEM cell holding key for the record */
4091 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4092 VdbeCursor *pC; /* Cursor to table into which insert is written */
4093 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004094 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004095 const char *zDb; /* database name - used by the update hook */
4096 const char *zTbl; /* Table name - used by the opdate hook */
4097 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004098
drha6c2ed92009-11-14 23:22:23 +00004099 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004100 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004101 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004102 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004103 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004104 assert( pC->pCursor!=0 );
4105 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004106 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004107 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004108
drhe05c9292009-10-29 13:48:10 +00004109 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004110 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004111 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004112 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004113 REGISTER_TRACE(pOp->p3, pKey);
4114 iKey = pKey->u.i;
4115 }else{
4116 assert( pOp->opcode==OP_InsertInt );
4117 iKey = pOp->p3;
4118 }
4119
drha05a7222008-01-19 03:35:58 +00004120 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004121 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004122 if( pData->flags & MEM_Null ){
4123 pData->z = 0;
4124 pData->n = 0;
4125 }else{
4126 assert( pData->flags & (MEM_Blob|MEM_Str) );
4127 }
drh3e9ca092009-09-08 01:14:48 +00004128 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4129 if( pData->flags & MEM_Zero ){
4130 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004131 }else{
drh3e9ca092009-09-08 01:14:48 +00004132 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004133 }
drh3e9ca092009-09-08 01:14:48 +00004134 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4135 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004136 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004137 );
drha05a7222008-01-19 03:35:58 +00004138 pC->deferredMoveto = 0;
4139 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004140
drha05a7222008-01-19 03:35:58 +00004141 /* Invoke the update-hook if required. */
4142 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004143 zDb = db->aDb[pC->iDb].zName;
4144 zTbl = pOp->p4.z;
4145 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004146 assert( pC->isTable );
4147 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4148 assert( pC->iDb>=0 );
4149 }
drh5e00f6c2001-09-13 13:46:56 +00004150 break;
4151}
4152
drh98757152008-01-09 23:04:12 +00004153/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004154**
drh5edc3122001-09-13 21:53:09 +00004155** Delete the record at which the P1 cursor is currently pointing.
4156**
4157** The cursor will be left pointing at either the next or the previous
4158** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004159** the next Next instruction will be a no-op. Hence it is OK to delete
drhbc5cf382014-08-06 01:08:07 +00004160** a record from within a Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004161**
rdcb0c374f2004-02-20 22:53:38 +00004162** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004163** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004164**
drh91fd4d42008-01-19 20:11:25 +00004165** P1 must not be pseudo-table. It has to be a real table with
4166** multiple rows.
4167**
4168** If P4 is not NULL, then it is the name of the table that P1 is
4169** pointing to. The update hook will be invoked, if it exists.
4170** If P4 is not NULL then the P1 cursor must have been positioned
4171** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004172*/
drh9cbf3422008-01-17 16:22:13 +00004173case OP_Delete: {
drhdfe88ec2008-11-03 20:55:06 +00004174 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004175
drh653b82a2009-06-22 11:10:47 +00004176 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4177 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004178 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004179 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
drh9a65f2c2009-06-22 19:05:40 +00004180 assert( pC->deferredMoveto==0 );
drh9a65f2c2009-06-22 19:05:40 +00004181
drhb53a5a92014-10-12 22:37:22 +00004182#ifdef SQLITE_DEBUG
4183 /* The seek operation that positioned the cursor prior to OP_Delete will
4184 ** have also set the pC->movetoTarget field to the rowid of the row that
4185 ** is being deleted */
4186 if( pOp->p4.z && pC->isTable ){
4187 i64 iKey = 0;
4188 sqlite3BtreeKeySize(pC->pCursor, &iKey);
4189 assert( pC->movetoTarget==iKey );
4190 }
4191#endif
4192
drh91fd4d42008-01-19 20:11:25 +00004193 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004194 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004195
drh91fd4d42008-01-19 20:11:25 +00004196 /* Invoke the update-hook if required. */
drhbbbb0e82013-11-26 23:27:07 +00004197 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
drh2c77be02013-11-27 21:07:03 +00004198 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
drhb53a5a92014-10-12 22:37:22 +00004199 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget);
drh91fd4d42008-01-19 20:11:25 +00004200 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004201 }
danielk1977b28af712004-06-21 06:50:26 +00004202 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004203 break;
4204}
drhb7f1d9a2009-09-08 02:27:58 +00004205/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004206**
drhb7f1d9a2009-09-08 02:27:58 +00004207** The value of the change counter is copied to the database handle
4208** change counter (returned by subsequent calls to sqlite3_changes()).
4209** Then the VMs internal change counter resets to 0.
4210** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004211*/
drh9cbf3422008-01-17 16:22:13 +00004212case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004213 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004214 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004215 break;
4216}
4217
drh1153c7b2013-11-01 22:02:56 +00004218/* Opcode: SorterCompare P1 P2 P3 P4
drhac502322014-07-30 13:56:48 +00004219** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004220**
drh1153c7b2013-11-01 22:02:56 +00004221** P1 is a sorter cursor. This instruction compares a prefix of the
drhbc5cf382014-08-06 01:08:07 +00004222** record blob in register P3 against a prefix of the entry that
drhac502322014-07-30 13:56:48 +00004223** the sorter cursor currently points to. Only the first P4 fields
4224** of r[P3] and the sorter record are compared.
drh1153c7b2013-11-01 22:02:56 +00004225**
4226** If either P3 or the sorter contains a NULL in one of their significant
4227** fields (not counting the P4 fields at the end which are ignored) then
4228** the comparison is assumed to be equal.
4229**
4230** Fall through to next instruction if the two records compare equal to
4231** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004232*/
4233case OP_SorterCompare: {
4234 VdbeCursor *pC;
4235 int res;
drhac502322014-07-30 13:56:48 +00004236 int nKeyCol;
dan5134d132011-09-02 10:31:11 +00004237
4238 pC = p->apCsr[pOp->p1];
4239 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004240 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004241 pIn3 = &aMem[pOp->p3];
drhac502322014-07-30 13:56:48 +00004242 nKeyCol = pOp->p4.i;
drh958d2612014-04-18 13:40:07 +00004243 res = 0;
drhac502322014-07-30 13:56:48 +00004244 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res);
drh688852a2014-02-17 22:40:43 +00004245 VdbeBranchTaken(res!=0,2);
dan5134d132011-09-02 10:31:11 +00004246 if( res ){
4247 pc = pOp->p2-1;
4248 }
4249 break;
4250};
4251
drh6cf4a7d2014-10-13 13:00:58 +00004252/* Opcode: SorterData P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00004253** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004254**
4255** Write into register P2 the current sorter data for sorter cursor P1.
drh6cf4a7d2014-10-13 13:00:58 +00004256** Then clear the column header cache on cursor P3.
4257**
4258** This opcode is normally use to move a record out of the sorter and into
4259** a register that is the source for a pseudo-table cursor created using
4260** OpenPseudo. That pseudo-table cursor is the one that is identified by
4261** parameter P3. Clearing the P3 column cache as part of this opcode saves
4262** us from having to issue a separate NullRow instruction to clear that cache.
dan5134d132011-09-02 10:31:11 +00004263*/
4264case OP_SorterData: {
4265 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004266
dan5134d132011-09-02 10:31:11 +00004267 pOut = &aMem[pOp->p2];
4268 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004269 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004270 rc = sqlite3VdbeSorterRowkey(pC, pOut);
dan38524132014-05-01 20:26:48 +00004271 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
drh6cf4a7d2014-10-13 13:00:58 +00004272 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4273 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE;
dan5134d132011-09-02 10:31:11 +00004274 break;
4275}
4276
drh98757152008-01-09 23:04:12 +00004277/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004278** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004279**
drh98757152008-01-09 23:04:12 +00004280** Write into register P2 the complete row data for cursor P1.
4281** There is no interpretation of the data.
4282** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004283** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004284**
drhde4fcfd2008-01-19 23:50:26 +00004285** If the P1 cursor must be pointing to a valid row (not a NULL row)
4286** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004287*/
drh98757152008-01-09 23:04:12 +00004288/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004289** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004290**
drh98757152008-01-09 23:04:12 +00004291** Write into register P2 the complete row key for cursor P1.
4292** There is no interpretation of the data.
drh0fd61352014-02-07 02:29:45 +00004293** The key is copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004294** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004295**
drhde4fcfd2008-01-19 23:50:26 +00004296** If the P1 cursor must be pointing to a valid row (not a NULL row)
4297** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004298*/
danielk1977a7a8e142008-02-13 18:25:27 +00004299case OP_RowKey:
4300case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004301 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004302 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004303 u32 n;
drh856c1032009-06-02 15:21:42 +00004304 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004305
drha6c2ed92009-11-14 23:22:23 +00004306 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004307 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004308
drhf0863fe2005-06-12 21:35:51 +00004309 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004310 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4311 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004312 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004313 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004314 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004315 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004316 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004317 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004318 assert( pC->pCursor!=0 );
4319 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004320
4321 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4322 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
drhc22284f2014-10-13 16:02:20 +00004323 ** the cursor. If this where not the case, on of the following assert()s
4324 ** would fail. Should this ever change (because of changes in the code
4325 ** generator) then the fix would be to insert a call to
4326 ** sqlite3VdbeCursorMoveto().
drh9a65f2c2009-06-22 19:05:40 +00004327 */
4328 assert( pC->deferredMoveto==0 );
drhc22284f2014-10-13 16:02:20 +00004329 assert( sqlite3BtreeCursorIsValid(pCrsr) );
4330#if 0 /* Not required due to the previous to assert() statements */
drhde4fcfd2008-01-19 23:50:26 +00004331 rc = sqlite3VdbeCursorMoveto(pC);
drhc22284f2014-10-13 16:02:20 +00004332 if( rc!=SQLITE_OK ) goto abort_due_to_error;
4333#endif
drh9a65f2c2009-06-22 19:05:40 +00004334
drh14da87f2013-11-20 21:51:33 +00004335 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004336 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004337 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004338 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004339 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004340 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004341 }
drhbfb19dc2009-06-05 16:46:53 +00004342 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004343 }else{
drhb07028f2011-10-14 21:49:18 +00004344 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004345 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004346 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004347 goto too_big;
4348 }
drhde4fcfd2008-01-19 23:50:26 +00004349 }
drh722246e2014-10-07 23:02:24 +00004350 testcase( n==0 );
4351 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
danielk1977a7a8e142008-02-13 18:25:27 +00004352 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004353 }
danielk1977a7a8e142008-02-13 18:25:27 +00004354 pOut->n = n;
4355 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004356 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004357 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4358 }else{
4359 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004360 }
danielk197796cb76f2008-01-04 13:24:28 +00004361 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004362 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004363 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004364 break;
4365}
4366
drh2133d822008-01-03 18:44:59 +00004367/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004368** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004369**
drh2133d822008-01-03 18:44:59 +00004370** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004371** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004372**
4373** P1 can be either an ordinary table or a virtual table. There used to
4374** be a separate OP_VRowid opcode for use with virtual tables, but this
4375** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004376*/
drh4c583122008-01-04 22:01:03 +00004377case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004378 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004379 i64 v;
drh856c1032009-06-02 15:21:42 +00004380 sqlite3_vtab *pVtab;
4381 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004382
drh653b82a2009-06-22 11:10:47 +00004383 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4384 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004385 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004386 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004387 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004388 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004389 break;
4390 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004391 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004392#ifndef SQLITE_OMIT_VIRTUALTABLE
4393 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004394 pVtab = pC->pVtabCursor->pVtab;
4395 pModule = pVtab->pModule;
4396 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004397 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004398 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004399#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004400 }else{
drh6be240e2009-07-14 02:33:02 +00004401 assert( pC->pCursor!=0 );
drhc22284f2014-10-13 16:02:20 +00004402 rc = sqlite3VdbeCursorRestore(pC);
drh61495262009-04-22 15:32:59 +00004403 if( rc ) goto abort_due_to_error;
drhb53a5a92014-10-12 22:37:22 +00004404 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
drhc22284f2014-10-13 16:02:20 +00004405 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */
drh5e00f6c2001-09-13 13:46:56 +00004406 }
drh4c583122008-01-04 22:01:03 +00004407 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004408 break;
4409}
4410
drh9cbf3422008-01-17 16:22:13 +00004411/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004412**
4413** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004414** that occur while the cursor is on the null row will always
4415** write a NULL.
drh17f71932002-02-21 12:01:27 +00004416*/
drh9cbf3422008-01-17 16:22:13 +00004417case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004418 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004419
drh653b82a2009-06-22 11:10:47 +00004420 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4421 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004422 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004423 pC->nullRow = 1;
drh399af1d2013-11-20 17:25:55 +00004424 pC->cacheStatus = CACHE_STALE;
danielk1977be51a652008-10-08 17:58:48 +00004425 if( pC->pCursor ){
4426 sqlite3BtreeClearCursor(pC->pCursor);
4427 }
drh17f71932002-02-21 12:01:27 +00004428 break;
4429}
4430
drh9cbf3422008-01-17 16:22:13 +00004431/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004432**
drh8af3f772014-07-25 18:01:06 +00004433** The next use of the Rowid or Column or Prev instruction for P1
drh9562b552002-02-19 15:00:07 +00004434** will refer to the last entry in the database table or index.
4435** If the table or index is empty and P2>0, then jump immediately to P2.
4436** If P2 is 0 or if the table or index is not empty, fall through
4437** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004438**
4439** This opcode leaves the cursor configured to move in reverse order,
4440** from the end toward the beginning. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004441** configured to use Prev, not Next.
drh9562b552002-02-19 15:00:07 +00004442*/
drh9cbf3422008-01-17 16:22:13 +00004443case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004444 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004445 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004446 int res;
drh9562b552002-02-19 15:00:07 +00004447
drh653b82a2009-06-22 11:10:47 +00004448 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4449 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004450 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004451 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004452 res = 0;
drh3da046d2013-11-11 03:24:11 +00004453 assert( pCrsr!=0 );
4454 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004455 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004456 pC->deferredMoveto = 0;
4457 pC->cacheStatus = CACHE_STALE;
drh8af3f772014-07-25 18:01:06 +00004458#ifdef SQLITE_DEBUG
4459 pC->seekOp = OP_Last;
4460#endif
drh688852a2014-02-17 22:40:43 +00004461 if( pOp->p2>0 ){
4462 VdbeBranchTaken(res!=0,2);
4463 if( res ) pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004464 }
4465 break;
4466}
4467
drh0342b1f2005-09-01 03:07:44 +00004468
drh9cbf3422008-01-17 16:22:13 +00004469/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004470**
4471** This opcode does exactly the same thing as OP_Rewind except that
4472** it increments an undocumented global variable used for testing.
4473**
4474** Sorting is accomplished by writing records into a sorting index,
4475** then rewinding that index and playing it back from beginning to
4476** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4477** rewinding so that the global variable will be incremented and
4478** regression tests can determine whether or not the optimizer is
4479** correctly optimizing out sorts.
4480*/
drhc6aff302011-09-01 15:32:47 +00004481case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004482case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004483#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004484 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004485 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004486#endif
drh9b47ee32013-08-20 03:13:51 +00004487 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004488 /* Fall through into OP_Rewind */
4489}
drh9cbf3422008-01-17 16:22:13 +00004490/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004491**
drhf0863fe2005-06-12 21:35:51 +00004492** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004493** will refer to the first entry in the database table or index.
4494** If the table or index is empty and P2>0, then jump immediately to P2.
4495** If P2 is 0 or if the table or index is not empty, fall through
4496** to the following instruction.
drh8af3f772014-07-25 18:01:06 +00004497**
4498** This opcode leaves the cursor configured to move in forward order,
drh4ed2fb92014-08-14 13:06:25 +00004499** from the beginning toward the end. In other words, the cursor is
drh5dad9a32014-07-25 18:37:42 +00004500** configured to use Next, not Prev.
drh5e00f6c2001-09-13 13:46:56 +00004501*/
drh9cbf3422008-01-17 16:22:13 +00004502case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004503 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004504 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004505 int res;
drh5e00f6c2001-09-13 13:46:56 +00004506
drh653b82a2009-06-22 11:10:47 +00004507 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4508 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004509 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004510 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004511 res = 1;
drh8af3f772014-07-25 18:01:06 +00004512#ifdef SQLITE_DEBUG
4513 pC->seekOp = OP_Rewind;
4514#endif
dan689ab892011-08-12 15:02:00 +00004515 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004516 rc = sqlite3VdbeSorterRewind(pC, &res);
dana205a482011-08-27 18:48:57 +00004517 }else{
4518 pCrsr = pC->pCursor;
4519 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004520 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004521 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004522 pC->cacheStatus = CACHE_STALE;
drhf4dada72004-05-11 09:57:35 +00004523 }
drh9c1905f2008-12-10 22:32:56 +00004524 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004525 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004526 VdbeBranchTaken(res!=0,2);
drha05a7222008-01-19 03:35:58 +00004527 if( res ){
drhf4dada72004-05-11 09:57:35 +00004528 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004529 }
4530 break;
4531}
4532
drh0fd61352014-02-07 02:29:45 +00004533/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004534**
4535** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004536** table or index. If there are no more key/value pairs then fall through
4537** to the following instruction. But if the cursor advance was successful,
4538** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004539**
drh5dad9a32014-07-25 18:37:42 +00004540** The Next opcode is only valid following an SeekGT, SeekGE, or
4541** OP_Rewind opcode used to position the cursor. Next is not allowed
4542** to follow SeekLT, SeekLE, or OP_Last.
drh8af3f772014-07-25 18:01:06 +00004543**
drhf93cd942013-11-21 03:12:25 +00004544** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4545** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004546**
drhe39a7322014-02-03 14:04:11 +00004547** The P3 value is a hint to the btree implementation. If P3==1, that
4548** means P1 is an SQL index and that this instruction could have been
4549** omitted if that index had been unique. P3 is usually 0. P3 is
4550** always either 0 or 1.
4551**
dana205a482011-08-27 18:48:57 +00004552** P4 is always of type P4_ADVANCE. The function pointer points to
4553** sqlite3BtreeNext().
4554**
drhafc266a2010-03-31 17:47:44 +00004555** If P5 is positive and the jump is taken, then event counter
4556** number P5-1 in the prepared statement is incremented.
4557**
drhf93cd942013-11-21 03:12:25 +00004558** See also: Prev, NextIfOpen
4559*/
drh0fd61352014-02-07 02:29:45 +00004560/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004561**
drh5dad9a32014-07-25 18:37:42 +00004562** This opcode works just like Next except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004563** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004564*/
drh0fd61352014-02-07 02:29:45 +00004565/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004566**
4567** Back up cursor P1 so that it points to the previous key/data pair in its
4568** table or index. If there is no previous key/value pairs then fall through
4569** to the following instruction. But if the cursor backup was successful,
4570** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004571**
drh8af3f772014-07-25 18:01:06 +00004572**
drh5dad9a32014-07-25 18:37:42 +00004573** The Prev opcode is only valid following an SeekLT, SeekLE, or
4574** OP_Last opcode used to position the cursor. Prev is not allowed
4575** to follow SeekGT, SeekGE, or OP_Rewind.
drh8af3f772014-07-25 18:01:06 +00004576**
drhf93cd942013-11-21 03:12:25 +00004577** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4578** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004579**
drhe39a7322014-02-03 14:04:11 +00004580** The P3 value is a hint to the btree implementation. If P3==1, that
4581** means P1 is an SQL index and that this instruction could have been
4582** omitted if that index had been unique. P3 is usually 0. P3 is
4583** always either 0 or 1.
4584**
dana205a482011-08-27 18:48:57 +00004585** P4 is always of type P4_ADVANCE. The function pointer points to
4586** sqlite3BtreePrevious().
4587**
drhafc266a2010-03-31 17:47:44 +00004588** If P5 is positive and the jump is taken, then event counter
4589** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004590*/
drh0fd61352014-02-07 02:29:45 +00004591/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004592**
drh5dad9a32014-07-25 18:37:42 +00004593** This opcode works just like Prev except that if cursor P1 is not
drhf93cd942013-11-21 03:12:25 +00004594** open it behaves a no-op.
4595*/
4596case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004597 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004598 int res;
drh8721ce42001-11-07 14:22:00 +00004599
drhf93cd942013-11-21 03:12:25 +00004600 pC = p->apCsr[pOp->p1];
4601 assert( isSorter(pC) );
drh323913c2014-03-23 16:29:23 +00004602 res = 0;
drhf93cd942013-11-21 03:12:25 +00004603 rc = sqlite3VdbeSorterNext(db, pC, &res);
4604 goto next_tail;
4605case OP_PrevIfOpen: /* jump */
4606case OP_NextIfOpen: /* jump */
4607 if( p->apCsr[pOp->p1]==0 ) break;
4608 /* Fall through */
4609case OP_Prev: /* jump */
4610case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004611 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004612 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004613 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00004614 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00004615 assert( pC!=0 );
4616 assert( pC->deferredMoveto==0 );
4617 assert( pC->pCursor );
drhe39a7322014-02-03 14:04:11 +00004618 assert( res==0 || (res==1 && pC->isTable==0) );
4619 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00004620 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4621 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4622 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4623 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
drh8af3f772014-07-25 18:01:06 +00004624
4625 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
4626 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
4627 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
4628 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
drhcefc87f2014-08-01 01:40:33 +00004629 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
drh8af3f772014-07-25 18:01:06 +00004630 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
4631 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
4632 || pC->seekOp==OP_Last );
4633
drhf93cd942013-11-21 03:12:25 +00004634 rc = pOp->p4.xAdvance(pC->pCursor, &res);
4635next_tail:
drha3460582008-07-11 21:02:53 +00004636 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004637 VdbeBranchTaken(res==0,2);
drha3460582008-07-11 21:02:53 +00004638 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004639 pC->nullRow = 0;
drha3460582008-07-11 21:02:53 +00004640 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004641 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004642#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004643 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004644#endif
drhf93cd942013-11-21 03:12:25 +00004645 }else{
4646 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004647 }
drh49afe3a2013-07-10 03:05:14 +00004648 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004649}
4650
danielk1977de630352009-05-04 11:42:29 +00004651/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004652** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004653**
drhef8662b2011-06-20 21:47:58 +00004654** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004655** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004656** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004657**
drhaa9b8962008-01-08 02:57:55 +00004658** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004659** insert is likely to be an append.
4660**
mistachkin21a919f2014-02-07 03:28:02 +00004661** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4662** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4663** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00004664**
mistachkin21a919f2014-02-07 03:28:02 +00004665** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
4666** just done a seek to the spot where the new entry is to be inserted.
4667** This flag avoids doing an extra seek.
drh0fd61352014-02-07 02:29:45 +00004668**
drhf0863fe2005-06-12 21:35:51 +00004669** This instruction only works for indices. The equivalent instruction
4670** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004671*/
drhca892a72011-09-03 00:17:51 +00004672case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004673case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004674 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004675 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004676 int nKey;
4677 const char *zKey;
4678
drh653b82a2009-06-22 11:10:47 +00004679 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4680 pC = p->apCsr[pOp->p1];
4681 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004682 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004683 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004684 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004685 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004686 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh3da046d2013-11-11 03:24:11 +00004687 assert( pCrsr!=0 );
4688 assert( pC->isTable==0 );
4689 rc = ExpandBlob(pIn2);
4690 if( rc==SQLITE_OK ){
4691 if( isSorter(pC) ){
drh958d2612014-04-18 13:40:07 +00004692 rc = sqlite3VdbeSorterWrite(pC, pIn2);
drh3da046d2013-11-11 03:24:11 +00004693 }else{
4694 nKey = pIn2->n;
4695 zKey = pIn2->z;
4696 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4697 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4698 );
4699 assert( pC->deferredMoveto==0 );
4700 pC->cacheStatus = CACHE_STALE;
danielk1977d908f5a2007-05-11 07:08:28 +00004701 }
drh5e00f6c2001-09-13 13:46:56 +00004702 }
drh5e00f6c2001-09-13 13:46:56 +00004703 break;
4704}
4705
drh4308e342013-11-11 16:55:52 +00004706/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004707** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004708**
drhe14006d2008-03-25 17:23:32 +00004709** The content of P3 registers starting at register P2 form
4710** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004711** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004712*/
drhe14006d2008-03-25 17:23:32 +00004713case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004714 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004715 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004716 int res;
4717 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004718
drhe14006d2008-03-25 17:23:32 +00004719 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004720 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004721 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4722 pC = p->apCsr[pOp->p1];
4723 assert( pC!=0 );
4724 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004725 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004726 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004727 r.pKeyInfo = pC->pKeyInfo;
4728 r.nField = (u16)pOp->p3;
dan1fed5da2014-02-25 21:01:25 +00004729 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004730 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004731#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004732 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004733#endif
drh3da046d2013-11-11 03:24:11 +00004734 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4735 if( rc==SQLITE_OK && res==0 ){
4736 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004737 }
drh3da046d2013-11-11 03:24:11 +00004738 assert( pC->deferredMoveto==0 );
4739 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004740 break;
4741}
4742
drh2133d822008-01-03 18:44:59 +00004743/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004744** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004745**
drh2133d822008-01-03 18:44:59 +00004746** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004747** the end of the index key pointed to by cursor P1. This integer should be
4748** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004749**
drh9437bd22009-02-01 00:29:56 +00004750** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004751*/
drh4c583122008-01-04 22:01:03 +00004752case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004753 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004754 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004755 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004756
drh653b82a2009-06-22 11:10:47 +00004757 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4758 pC = p->apCsr[pOp->p1];
4759 assert( pC!=0 );
4760 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004761 assert( pCrsr!=0 );
drh3c657212009-11-17 23:59:58 +00004762 pOut->flags = MEM_Null;
drh3da046d2013-11-11 03:24:11 +00004763 assert( pC->isTable==0 );
drhc22284f2014-10-13 16:02:20 +00004764 assert( pC->deferredMoveto==0 );
4765
4766 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
4767 ** out from under the cursor. That will never happend for an IdxRowid
4768 ** opcode, hence the NEVER() arround the check of the return value.
4769 */
4770 rc = sqlite3VdbeCursorRestore(pC);
4771 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4772
drh3da046d2013-11-11 03:24:11 +00004773 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00004774 rowid = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00004775 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
drh3da046d2013-11-11 03:24:11 +00004776 if( rc!=SQLITE_OK ){
4777 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00004778 }
drh3da046d2013-11-11 03:24:11 +00004779 pOut->u.i = rowid;
4780 pOut->flags = MEM_Int;
drh8721ce42001-11-07 14:22:00 +00004781 }
4782 break;
4783}
4784
danielk197761dd5832008-04-18 11:31:12 +00004785/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004786** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004787**
danielk197761dd5832008-04-18 11:31:12 +00004788** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004789** key that omits the PRIMARY KEY. Compare this key value against the index
4790** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4791** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00004792**
danielk197761dd5832008-04-18 11:31:12 +00004793** If the P1 index entry is greater than or equal to the key value
4794** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00004795*/
4796/* Opcode: IdxGT P1 P2 P3 P4 P5
4797** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00004798**
drh4a1d3652014-02-14 15:13:36 +00004799** The P4 register values beginning with P3 form an unpacked index
4800** key that omits the PRIMARY KEY. Compare this key value against the index
4801** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4802** fields at the end.
4803**
4804** If the P1 index entry is greater than the key value
4805** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00004806*/
drh3bb9b932010-08-06 02:10:00 +00004807/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004808** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004809**
danielk197761dd5832008-04-18 11:31:12 +00004810** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004811** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4812** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4813** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004814**
danielk197761dd5832008-04-18 11:31:12 +00004815** If the P1 index entry is less than the key value then jump to P2.
4816** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00004817*/
drh4a1d3652014-02-14 15:13:36 +00004818/* Opcode: IdxLE P1 P2 P3 P4 P5
4819** Synopsis: key=r[P3@P4]
4820**
4821** The P4 register values beginning with P3 form an unpacked index
4822** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4823** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4824** ROWID on the P1 index.
4825**
4826** If the P1 index entry is less than or equal to the key value then jump
4827** to P2. Otherwise fall through to the next instruction.
4828*/
4829case OP_IdxLE: /* jump */
4830case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00004831case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00004832case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004833 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004834 int res;
4835 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004836
drh653b82a2009-06-22 11:10:47 +00004837 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4838 pC = p->apCsr[pOp->p1];
4839 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004840 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00004841 assert( pC->pCursor!=0);
4842 assert( pC->deferredMoveto==0 );
4843 assert( pOp->p5==0 || pOp->p5==1 );
4844 assert( pOp->p4type==P4_INT32 );
4845 r.pKeyInfo = pC->pKeyInfo;
4846 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00004847 if( pOp->opcode<OP_IdxLT ){
4848 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
dan1fed5da2014-02-25 21:01:25 +00004849 r.default_rc = -1;
drh3da046d2013-11-11 03:24:11 +00004850 }else{
drh4a1d3652014-02-14 15:13:36 +00004851 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
dan1fed5da2014-02-25 21:01:25 +00004852 r.default_rc = 0;
drh3da046d2013-11-11 03:24:11 +00004853 }
4854 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004855#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004856 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004857#endif
drh2dc06482013-12-11 00:59:10 +00004858 res = 0; /* Not needed. Only used to silence a warning. */
drhd3b74202014-09-17 16:41:15 +00004859 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00004860 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
4861 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
4862 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00004863 res = -res;
4864 }else{
drh4a1d3652014-02-14 15:13:36 +00004865 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00004866 res++;
4867 }
drh688852a2014-02-17 22:40:43 +00004868 VdbeBranchTaken(res>0,2);
drh3da046d2013-11-11 03:24:11 +00004869 if( res>0 ){
4870 pc = pOp->p2 - 1 ;
drh8721ce42001-11-07 14:22:00 +00004871 }
4872 break;
4873}
4874
drh98757152008-01-09 23:04:12 +00004875/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004876**
4877** Delete an entire database table or index whose root page in the database
4878** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004879**
drh98757152008-01-09 23:04:12 +00004880** The table being destroyed is in the main database file if P3==0. If
4881** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004882** that is used to store tables create using CREATE TEMPORARY TABLE.
4883**
drh205f48e2004-11-05 00:43:11 +00004884** If AUTOVACUUM is enabled then it is possible that another root page
4885** might be moved into the newly deleted root page in order to keep all
4886** root pages contiguous at the beginning of the database. The former
4887** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004888** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004889** movement was required (because the table being dropped was already
4890** the last one in the database) then a zero is stored in register P2.
4891** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004892**
drhb19a2bc2001-09-16 00:13:26 +00004893** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004894*/
drh98757152008-01-09 23:04:12 +00004895case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004896 int iMoved;
drh3765df42006-06-28 18:18:09 +00004897 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004898 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004899 int iDb;
drh3a949872012-09-18 13:20:13 +00004900
drh9e92a472013-06-27 17:40:30 +00004901 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004902#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004903 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004904 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004905 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4906 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4907 ){
danielk1977212b2182006-06-23 14:32:08 +00004908 iCnt++;
4909 }
4910 }
drh3765df42006-06-28 18:18:09 +00004911#else
danc0537fe2013-06-28 19:41:43 +00004912 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004913#endif
drh3c657212009-11-17 23:59:58 +00004914 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004915 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004916 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004917 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004918 }else{
drh856c1032009-06-02 15:21:42 +00004919 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004920 assert( iCnt==1 );
drha7ab6d82014-07-21 15:44:39 +00004921 assert( DbMaskTest(p->btreeMask, iDb) );
drh2dc06482013-12-11 00:59:10 +00004922 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00004923 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004924 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004925 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004926#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004927 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004928 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4929 /* All OP_Destroy operations occur on the same btree */
4930 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4931 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004932 }
drh3765df42006-06-28 18:18:09 +00004933#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004934 }
drh5e00f6c2001-09-13 13:46:56 +00004935 break;
4936}
4937
danielk1977c7af4842008-10-27 13:59:33 +00004938/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004939**
4940** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004941** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004942** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004943**
drhf57b3392001-10-08 13:22:32 +00004944** The table being clear is in the main database file if P2==0. If
4945** P2==1 then the table to be clear is in the auxiliary database file
4946** that is used to store tables create using CREATE TEMPORARY TABLE.
4947**
shanebe217792009-03-05 04:20:31 +00004948** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004949** intkey table (an SQL table, not an index). In this case the row change
4950** count is incremented by the number of rows in the table being cleared.
4951** If P3 is greater than zero, then the value stored in register P3 is
4952** also incremented by the number of rows in the table being cleared.
4953**
drhb19a2bc2001-09-16 00:13:26 +00004954** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004955*/
drh9cbf3422008-01-17 16:22:13 +00004956case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004957 int nChange;
4958
4959 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004960 assert( p->readOnly==0 );
drha7ab6d82014-07-21 15:44:39 +00004961 assert( DbMaskTest(p->btreeMask, pOp->p2) );
danielk1977c7af4842008-10-27 13:59:33 +00004962 rc = sqlite3BtreeClearTable(
4963 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4964 );
4965 if( pOp->p3 ){
4966 p->nChange += nChange;
4967 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004968 assert( memIsValid(&aMem[pOp->p3]) );
4969 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004970 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004971 }
4972 }
drh5edc3122001-09-13 21:53:09 +00004973 break;
4974}
4975
drh65ea12c2014-03-19 17:41:36 +00004976/* Opcode: ResetSorter P1 * * * *
drh079a3072014-03-19 14:10:55 +00004977**
drh65ea12c2014-03-19 17:41:36 +00004978** Delete all contents from the ephemeral table or sorter
4979** that is open on cursor P1.
drh079a3072014-03-19 14:10:55 +00004980**
drh65ea12c2014-03-19 17:41:36 +00004981** This opcode only works for cursors used for sorting and
4982** opened with OP_OpenEphemeral or OP_SorterOpen.
drh079a3072014-03-19 14:10:55 +00004983*/
drh65ea12c2014-03-19 17:41:36 +00004984case OP_ResetSorter: {
drh079a3072014-03-19 14:10:55 +00004985 VdbeCursor *pC;
4986
4987 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4988 pC = p->apCsr[pOp->p1];
4989 assert( pC!=0 );
drh65ea12c2014-03-19 17:41:36 +00004990 if( pC->pSorter ){
4991 sqlite3VdbeSorterReset(db, pC->pSorter);
4992 }else{
4993 assert( pC->isEphemeral );
4994 rc = sqlite3BtreeClearTableOfCursor(pC->pCursor);
4995 }
drh079a3072014-03-19 14:10:55 +00004996 break;
4997}
4998
drh4c583122008-01-04 22:01:03 +00004999/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005000** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00005001**
drh4c583122008-01-04 22:01:03 +00005002** Allocate a new table in the main database file if P1==0 or in the
5003** auxiliary database file if P1==1 or in an attached database if
5004** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005005** register P2
drh5b2fd562001-09-13 15:21:31 +00005006**
drhc6b52df2002-01-04 03:09:29 +00005007** The difference between a table and an index is this: A table must
5008** have a 4-byte integer key and can have arbitrary data. An index
5009** has an arbitrary key but no data.
5010**
drhb19a2bc2001-09-16 00:13:26 +00005011** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00005012*/
drh4c583122008-01-04 22:01:03 +00005013/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005014** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00005015**
drh4c583122008-01-04 22:01:03 +00005016** Allocate a new index in the main database file if P1==0 or in the
5017** auxiliary database file if P1==1 or in an attached database if
5018** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00005019** register P2.
drhf57b3392001-10-08 13:22:32 +00005020**
drhc6b52df2002-01-04 03:09:29 +00005021** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00005022*/
drh4c583122008-01-04 22:01:03 +00005023case OP_CreateIndex: /* out2-prerelease */
5024case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00005025 int pgno;
drhf328bc82004-05-10 23:29:49 +00005026 int flags;
drh234c39d2004-07-24 03:30:47 +00005027 Db *pDb;
drh856c1032009-06-02 15:21:42 +00005028
5029 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00005030 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005031 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005032 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00005033 pDb = &db->aDb[pOp->p1];
5034 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00005035 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00005036 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00005037 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00005038 }else{
drhd4187c72010-08-30 22:15:45 +00005039 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00005040 }
drh234c39d2004-07-24 03:30:47 +00005041 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00005042 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00005043 break;
5044}
5045
drh22645842011-03-24 01:34:03 +00005046/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00005047**
5048** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00005049** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00005050**
5051** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00005052** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00005053*/
drh9cbf3422008-01-17 16:22:13 +00005054case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00005055 int iDb;
5056 const char *zMaster;
5057 char *zSql;
5058 InitData initData;
5059
drhbdaec522011-04-04 00:14:43 +00005060 /* Any prepared statement that invokes this opcode will hold mutexes
5061 ** on every btree. This is a prerequisite for invoking
5062 ** sqlite3InitCallback().
5063 */
5064#ifdef SQLITE_DEBUG
5065 for(iDb=0; iDb<db->nDb; iDb++){
5066 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
5067 }
5068#endif
drhbdaec522011-04-04 00:14:43 +00005069
drh856c1032009-06-02 15:21:42 +00005070 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00005071 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00005072 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00005073 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00005074 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00005075 initData.db = db;
5076 initData.iDb = pOp->p1;
5077 initData.pzErrMsg = &p->zErrMsg;
5078 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00005079 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00005080 db->aDb[iDb].zName, zMaster, pOp->p4.z);
5081 if( zSql==0 ){
5082 rc = SQLITE_NOMEM;
5083 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00005084 assert( db->init.busy==0 );
5085 db->init.busy = 1;
5086 initData.rc = SQLITE_OK;
5087 assert( !db->mallocFailed );
5088 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
5089 if( rc==SQLITE_OK ) rc = initData.rc;
5090 sqlite3DbFree(db, zSql);
5091 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00005092 }
drh3c23a882007-01-09 14:01:13 +00005093 }
drh81028a42012-05-15 18:28:27 +00005094 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00005095 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00005096 goto no_mem;
5097 }
drh234c39d2004-07-24 03:30:47 +00005098 break;
5099}
5100
drh8bfdf722009-06-19 14:06:03 +00005101#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00005102/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00005103**
5104** Read the sqlite_stat1 table for database P1 and load the content
5105** of that table into the internal index hash table. This will cause
5106** the analysis to be used when preparing all subsequent queries.
5107*/
drh9cbf3422008-01-17 16:22:13 +00005108case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00005109 assert( pOp->p1>=0 && pOp->p1<db->nDb );
5110 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00005111 break;
5112}
drh8bfdf722009-06-19 14:06:03 +00005113#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00005114
drh98757152008-01-09 23:04:12 +00005115/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005116**
5117** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005118** the table named P4 in database P1. This is called after a table
drh5dad9a32014-07-25 18:37:42 +00005119** is dropped from disk (using the Destroy opcode) in order to keep
5120** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005121** schema consistent with what is on disk.
5122*/
drh9cbf3422008-01-17 16:22:13 +00005123case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005124 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005125 break;
5126}
5127
drh98757152008-01-09 23:04:12 +00005128/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005129**
5130** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005131** the index named P4 in database P1. This is called after an index
drh5dad9a32014-07-25 18:37:42 +00005132** is dropped from disk (using the Destroy opcode)
5133** in order to keep the internal representation of the
drh956bc922004-07-24 17:38:29 +00005134** schema consistent with what is on disk.
5135*/
drh9cbf3422008-01-17 16:22:13 +00005136case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005137 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005138 break;
5139}
5140
drh98757152008-01-09 23:04:12 +00005141/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005142**
5143** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005144** the trigger named P4 in database P1. This is called after a trigger
drh5dad9a32014-07-25 18:37:42 +00005145** is dropped from disk (using the Destroy opcode) in order to keep
5146** the internal representation of the
drh956bc922004-07-24 17:38:29 +00005147** schema consistent with what is on disk.
5148*/
drh9cbf3422008-01-17 16:22:13 +00005149case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005150 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005151 break;
5152}
5153
drh234c39d2004-07-24 03:30:47 +00005154
drhb7f91642004-10-31 02:22:47 +00005155#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005156/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005157**
drh98757152008-01-09 23:04:12 +00005158** Do an analysis of the currently open database. Store in
5159** register P1 the text of an error message describing any problems.
5160** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005161**
drh98757152008-01-09 23:04:12 +00005162** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005163** At most reg(P3) errors will be reported.
5164** In other words, the analysis stops as soon as reg(P1) errors are
5165** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005166**
drh79069752004-05-22 21:30:40 +00005167** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005168** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005169** total.
drh21504322002-06-25 13:16:02 +00005170**
drh98757152008-01-09 23:04:12 +00005171** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005172** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005173**
drh1dcdbc02007-01-27 02:24:54 +00005174** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005175*/
drhaaab5722002-02-19 13:39:21 +00005176case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005177 int nRoot; /* Number of tables to check. (Number of root pages.) */
5178 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5179 int j; /* Loop counter */
5180 int nErr; /* Number of errors reported */
5181 char *z; /* Text of the error report */
5182 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005183
drh1713afb2013-06-28 01:24:57 +00005184 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005185 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005186 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005187 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005188 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005189 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005190 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005191 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005192 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005193 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005194 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005195 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005196 }
5197 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005198 assert( pOp->p5<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005199 assert( DbMaskTest(p->btreeMask, pOp->p5) );
drh98757152008-01-09 23:04:12 +00005200 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005201 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005202 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005203 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005204 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005205 if( nErr==0 ){
5206 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005207 }else if( z==0 ){
5208 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005209 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005210 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005211 }
drhb7654112008-01-12 12:48:07 +00005212 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005213 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005214 break;
5215}
drhb7f91642004-10-31 02:22:47 +00005216#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005217
drh3d4501e2008-12-04 20:40:10 +00005218/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005219** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005220**
drh3d4501e2008-12-04 20:40:10 +00005221** Insert the integer value held by register P2 into a boolean index
5222** held in register P1.
5223**
5224** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005225*/
drh93952eb2009-11-13 19:43:43 +00005226case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005227 pIn1 = &aMem[pOp->p1];
5228 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005229 assert( (pIn2->flags & MEM_Int)!=0 );
5230 if( (pIn1->flags & MEM_RowSet)==0 ){
5231 sqlite3VdbeMemSetRowSet(pIn1);
5232 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005233 }
drh93952eb2009-11-13 19:43:43 +00005234 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005235 break;
5236}
5237
5238/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005239** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005240**
5241** Extract the smallest value from boolean index P1 and put that value into
5242** register P3. Or, if boolean index P1 is initially empty, leave P3
5243** unchanged and jump to instruction P2.
5244*/
drh93952eb2009-11-13 19:43:43 +00005245case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005246 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005247
drh3c657212009-11-17 23:59:58 +00005248 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005249 if( (pIn1->flags & MEM_RowSet)==0
5250 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005251 ){
5252 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005253 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005254 pc = pOp->p2 - 1;
drh688852a2014-02-17 22:40:43 +00005255 VdbeBranchTaken(1,2);
drh3d4501e2008-12-04 20:40:10 +00005256 }else{
5257 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005258 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh688852a2014-02-17 22:40:43 +00005259 VdbeBranchTaken(0,2);
drh17435752007-08-16 04:30:38 +00005260 }
drh49afe3a2013-07-10 03:05:14 +00005261 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005262}
5263
drh1b26c7c2009-04-22 02:15:47 +00005264/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005265** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005266**
drhade97602009-04-21 15:05:18 +00005267** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005268** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005269** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005270** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005271** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005272**
drh1b26c7c2009-04-22 02:15:47 +00005273** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005274** of integers, where each set contains no duplicates. Each set
5275** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005276** must have P4==0, the final set P4=-1. P4 must be either -1 or
5277** non-negative. For non-negative values of P4 only the lower 4
5278** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005279**
5280** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005281** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005282** (b) when P4==-1 there is no need to insert the value, as it will
5283** never be tested for, and (c) when a value that is part of set X is
5284** inserted, there is no need to search to see if the same value was
5285** previously inserted as part of set X (only if it was previously
5286** inserted as part of some other set).
5287*/
drh1b26c7c2009-04-22 02:15:47 +00005288case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005289 int iSet;
5290 int exists;
5291
drh3c657212009-11-17 23:59:58 +00005292 pIn1 = &aMem[pOp->p1];
5293 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005294 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005295 assert( pIn3->flags&MEM_Int );
5296
drh1b26c7c2009-04-22 02:15:47 +00005297 /* If there is anything other than a rowset object in memory cell P1,
5298 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005299 */
drh733bf1b2009-04-22 00:47:00 +00005300 if( (pIn1->flags & MEM_RowSet)==0 ){
5301 sqlite3VdbeMemSetRowSet(pIn1);
5302 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005303 }
5304
5305 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005306 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005307 if( iSet ){
drhd83cad22014-04-10 02:24:48 +00005308 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005309 VdbeBranchTaken(exists!=0,2);
danielk19771d461462009-04-21 09:02:45 +00005310 if( exists ){
5311 pc = pOp->p2 - 1;
5312 break;
5313 }
5314 }
5315 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005316 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005317 }
5318 break;
5319}
5320
drh5e00f6c2001-09-13 13:46:56 +00005321
danielk197793758c82005-01-21 08:13:14 +00005322#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005323
drh0fd61352014-02-07 02:29:45 +00005324/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005325**
dan76d462e2009-08-30 11:42:51 +00005326** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005327**
dan76d462e2009-08-30 11:42:51 +00005328** P1 contains the address of the memory cell that contains the first memory
5329** cell in an array of values used as arguments to the sub-program. P2
5330** contains the address to jump to if the sub-program throws an IGNORE
5331** exception using the RAISE() function. Register P3 contains the address
5332** of a memory cell in this (the parent) VM that is used to allocate the
5333** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005334**
5335** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005336**
5337** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005338*/
dan76d462e2009-08-30 11:42:51 +00005339case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005340 int nMem; /* Number of memory registers for sub-program */
5341 int nByte; /* Bytes of runtime space required for sub-program */
5342 Mem *pRt; /* Register to allocate runtime space */
5343 Mem *pMem; /* Used to iterate through memory cells */
5344 Mem *pEnd; /* Last memory cell in new array */
5345 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5346 SubProgram *pProgram; /* Sub-program to execute */
5347 void *t; /* Token identifying trigger */
5348
5349 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005350 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005351 assert( pProgram->nOp>0 );
5352
dan1da40a32009-09-19 17:00:31 +00005353 /* If the p5 flag is clear, then recursive invocation of triggers is
5354 ** disabled for backwards compatibility (p5 is set if this sub-program
5355 ** is really a trigger, not a foreign key action, and the flag set
5356 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005357 **
5358 ** It is recursive invocation of triggers, at the SQL level, that is
5359 ** disabled. In some cases a single trigger may generate more than one
5360 ** SubProgram (if the trigger may be executed with more than one different
5361 ** ON CONFLICT algorithm). SubProgram structures associated with a
5362 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005363 ** variable. */
5364 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005365 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005366 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5367 if( pFrame ) break;
5368 }
5369
danf5894502009-10-07 18:41:19 +00005370 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005371 rc = SQLITE_ERROR;
5372 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5373 break;
5374 }
5375
5376 /* Register pRt is used to store the memory required to save the state
5377 ** of the current program, and the memory required at runtime to execute
5378 ** the trigger program. If this trigger has been fired before, then pRt
5379 ** is already allocated. Otherwise, it must be initialized. */
5380 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005381 /* SubProgram.nMem is set to the number of memory cells used by the
5382 ** program stored in SubProgram.aOp. As well as these, one memory
5383 ** cell is required for each cursor used by the program. Set local
5384 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5385 */
dan65a7cd12009-09-01 12:16:01 +00005386 nMem = pProgram->nMem + pProgram->nCsr;
5387 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005388 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005389 + pProgram->nCsr * sizeof(VdbeCursor *)
5390 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005391 pFrame = sqlite3DbMallocZero(db, nByte);
5392 if( !pFrame ){
5393 goto no_mem;
5394 }
5395 sqlite3VdbeMemRelease(pRt);
5396 pRt->flags = MEM_Frame;
5397 pRt->u.pFrame = pFrame;
5398
5399 pFrame->v = p;
5400 pFrame->nChildMem = nMem;
5401 pFrame->nChildCsr = pProgram->nCsr;
5402 pFrame->pc = pc;
5403 pFrame->aMem = p->aMem;
5404 pFrame->nMem = p->nMem;
5405 pFrame->apCsr = p->apCsr;
5406 pFrame->nCursor = p->nCursor;
5407 pFrame->aOp = p->aOp;
5408 pFrame->nOp = p->nOp;
5409 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005410 pFrame->aOnceFlag = p->aOnceFlag;
5411 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005412
5413 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5414 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005415 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005416 pMem->db = db;
5417 }
5418 }else{
5419 pFrame = pRt->u.pFrame;
5420 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5421 assert( pProgram->nCsr==pFrame->nChildCsr );
5422 assert( pc==pFrame->pc );
5423 }
5424
5425 p->nFrame++;
5426 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005427 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005428 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005429 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005430 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005431 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005432 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005433 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005434 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005435 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005436 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005437 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5438 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005439 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005440 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005441
5442 break;
5443}
5444
dan76d462e2009-08-30 11:42:51 +00005445/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005446**
dan76d462e2009-08-30 11:42:51 +00005447** This opcode is only ever present in sub-programs called via the
5448** OP_Program instruction. Copy a value currently stored in a memory
5449** cell of the calling (parent) frame to cell P2 in the current frames
5450** address space. This is used by trigger programs to access the new.*
5451** and old.* values.
dan165921a2009-08-28 18:53:45 +00005452**
dan76d462e2009-08-30 11:42:51 +00005453** The address of the cell in the parent frame is determined by adding
5454** the value of the P1 argument to the value of the P1 argument to the
5455** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005456*/
dan76d462e2009-08-30 11:42:51 +00005457case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005458 VdbeFrame *pFrame;
5459 Mem *pIn;
5460 pFrame = p->pFrame;
5461 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005462 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5463 break;
5464}
5465
danielk197793758c82005-01-21 08:13:14 +00005466#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005467
dan1da40a32009-09-19 17:00:31 +00005468#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005469/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005470** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005471**
dan0ff297e2009-09-25 17:03:14 +00005472** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5473** If P1 is non-zero, the database constraint counter is incremented
5474** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005475** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005476*/
dan32b09f22009-09-23 17:29:59 +00005477case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005478 if( db->flags & SQLITE_DeferFKs ){
5479 db->nDeferredImmCons += pOp->p2;
5480 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005481 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005482 }else{
dan0ff297e2009-09-25 17:03:14 +00005483 p->nFkConstraint += pOp->p2;
5484 }
5485 break;
5486}
5487
5488/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005489** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005490**
5491** This opcode tests if a foreign key constraint-counter is currently zero.
5492** If so, jump to instruction P2. Otherwise, fall through to the next
5493** instruction.
5494**
5495** If P1 is non-zero, then the jump is taken if the database constraint-counter
5496** is zero (the one that counts deferred constraint violations). If P1 is
5497** zero, the jump is taken if the statement constraint-counter is zero
5498** (immediate foreign key constraint violations).
5499*/
5500case OP_FkIfZero: { /* jump */
5501 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00005502 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005503 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005504 }else{
drh688852a2014-02-17 22:40:43 +00005505 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005506 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005507 }
dan1da40a32009-09-19 17:00:31 +00005508 break;
5509}
5510#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5511
drh205f48e2004-11-05 00:43:11 +00005512#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005513/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005514** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005515**
dan76d462e2009-08-30 11:42:51 +00005516** P1 is a register in the root frame of this VM (the root frame is
5517** different from the current frame if this instruction is being executed
5518** within a sub-program). Set the value of register P1 to the maximum of
5519** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005520**
5521** This instruction throws an error if the memory cell is not initially
5522** an integer.
5523*/
dan76d462e2009-08-30 11:42:51 +00005524case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005525 VdbeFrame *pFrame;
5526 if( p->pFrame ){
5527 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5528 pIn1 = &pFrame->aMem[pOp->p1];
5529 }else{
drha6c2ed92009-11-14 23:22:23 +00005530 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005531 }
drhec86c722011-12-09 17:27:51 +00005532 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005533 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005534 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005535 sqlite3VdbeMemIntegerify(pIn2);
5536 if( pIn1->u.i<pIn2->u.i){
5537 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005538 }
5539 break;
5540}
5541#endif /* SQLITE_OMIT_AUTOINCREMENT */
5542
drh98757152008-01-09 23:04:12 +00005543/* Opcode: IfPos P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005544** Synopsis: if r[P1]>0 goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005545**
drh98757152008-01-09 23:04:12 +00005546** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005547**
drh98757152008-01-09 23:04:12 +00005548** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005549** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005550*/
drh9cbf3422008-01-17 16:22:13 +00005551case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005552 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005553 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005554 VdbeBranchTaken( pIn1->u.i>0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005555 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005556 pc = pOp->p2 - 1;
5557 }
5558 break;
5559}
5560
drh4336b0e2014-08-05 00:53:51 +00005561/* Opcode: IfNeg P1 P2 P3 * *
5562** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
drh15007a92006-01-08 18:10:17 +00005563**
drhbc5cf382014-08-06 01:08:07 +00005564** Register P1 must contain an integer. Add literal P3 to the value in
drh4336b0e2014-08-05 00:53:51 +00005565** register P1 then if the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005566*/
drh9cbf3422008-01-17 16:22:13 +00005567case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005568 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005569 assert( pIn1->flags&MEM_Int );
drh4336b0e2014-08-05 00:53:51 +00005570 pIn1->u.i += pOp->p3;
drh688852a2014-02-17 22:40:43 +00005571 VdbeBranchTaken(pIn1->u.i<0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005572 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005573 pc = pOp->p2 - 1;
5574 }
5575 break;
5576}
5577
drh9b918ed2009-11-12 03:13:26 +00005578/* Opcode: IfZero P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005579** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
drhec7429a2005-10-06 16:53:14 +00005580**
drh9b918ed2009-11-12 03:13:26 +00005581** The register P1 must contain an integer. Add literal P3 to the
5582** value in register P1. If the result is exactly 0, jump to P2.
drhec7429a2005-10-06 16:53:14 +00005583*/
drh9cbf3422008-01-17 16:22:13 +00005584case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005585 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005586 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005587 pIn1->u.i += pOp->p3;
drh688852a2014-02-17 22:40:43 +00005588 VdbeBranchTaken(pIn1->u.i==0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005589 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005590 pc = pOp->p2 - 1;
5591 }
5592 break;
5593}
5594
drh98757152008-01-09 23:04:12 +00005595/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005596** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005597**
drh0bce8352002-02-28 00:41:10 +00005598** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005599** function has P5 arguments. P4 is a pointer to the FuncDef
5600** structure that specifies the function. Use register
5601** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005602**
drh98757152008-01-09 23:04:12 +00005603** The P5 arguments are taken from register P2 and its
5604** successors.
drhe5095352002-02-24 03:25:14 +00005605*/
drh9cbf3422008-01-17 16:22:13 +00005606case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005607 int n;
drhe5095352002-02-24 03:25:14 +00005608 int i;
drhc54a6172009-06-02 16:06:03 +00005609 Mem *pMem;
5610 Mem *pRec;
drh9bd038f2014-08-27 14:14:06 +00005611 Mem t;
danielk197722322fd2004-05-25 23:35:17 +00005612 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005613 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005614
drh856c1032009-06-02 15:21:42 +00005615 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005616 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005617 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005618 apVal = p->apArg;
5619 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005620 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005621 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005622 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005623 memAboutToChange(p, pRec);
drhe5095352002-02-24 03:25:14 +00005624 }
danielk19772dca4ac2008-01-03 11:50:29 +00005625 ctx.pFunc = pOp->p4.pFunc;
dan3bc9f742013-08-15 16:18:39 +00005626 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005627 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005628 pMem->n++;
drhd3b74202014-09-17 16:41:15 +00005629 sqlite3VdbeMemInit(&t, db, MEM_Null);
drh9bd038f2014-08-27 14:14:06 +00005630 ctx.pOut = &t;
drh1350b032002-02-27 19:00:20 +00005631 ctx.isError = 0;
drha15cc472014-09-25 13:17:30 +00005632 ctx.pVdbe = p;
5633 ctx.iOp = pc;
drh7a957892012-02-02 17:35:43 +00005634 ctx.skipFlag = 0;
drhee9ff672010-09-03 18:50:48 +00005635 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005636 if( ctx.isError ){
drh9bd038f2014-08-27 14:14:06 +00005637 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
drh69544ec2008-02-06 14:11:34 +00005638 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005639 }
drh7a957892012-02-02 17:35:43 +00005640 if( ctx.skipFlag ){
5641 assert( pOp[-1].opcode==OP_CollSeq );
5642 i = pOp[-1].p1;
5643 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5644 }
drh9bd038f2014-08-27 14:14:06 +00005645 sqlite3VdbeMemRelease(&t);
drh5e00f6c2001-09-13 13:46:56 +00005646 break;
5647}
5648
drh98757152008-01-09 23:04:12 +00005649/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005650** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005651**
drh13449892005-09-07 21:22:45 +00005652** Execute the finalizer function for an aggregate. P1 is
5653** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005654**
5655** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005656** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005657** argument is not used by this opcode. It is only there to disambiguate
5658** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005659** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005660** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005661*/
drh9cbf3422008-01-17 16:22:13 +00005662case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005663 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005664 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005665 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005666 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005667 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005668 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005669 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005670 }
drh2dca8682008-03-21 17:13:13 +00005671 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005672 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005673 if( sqlite3VdbeMemTooBig(pMem) ){
5674 goto too_big;
5675 }
drh5e00f6c2001-09-13 13:46:56 +00005676 break;
5677}
5678
dan5cf53532010-05-01 16:40:20 +00005679#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005680/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005681**
5682** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005683** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005684** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5685** SQLITE_BUSY or not, respectively. Write the number of pages in the
5686** WAL after the checkpoint into mem[P3+1] and the number of pages
5687** in the WAL that have been checkpointed after the checkpoint
5688** completes into mem[P3+2]. However on an error, mem[P3+1] and
5689** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005690*/
5691case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005692 int i; /* Loop counter */
5693 int aRes[3]; /* Results */
5694 Mem *pMem; /* Write results here */
5695
drh9e92a472013-06-27 17:40:30 +00005696 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005697 aRes[0] = 0;
5698 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005699 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5700 || pOp->p2==SQLITE_CHECKPOINT_FULL
5701 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5702 );
drh30aa3b92011-02-07 23:56:01 +00005703 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005704 if( rc==SQLITE_BUSY ){
5705 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005706 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005707 }
drh30aa3b92011-02-07 23:56:01 +00005708 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5709 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5710 }
dan7c246102010-04-12 19:00:29 +00005711 break;
5712};
dan5cf53532010-05-01 16:40:20 +00005713#endif
drh5e00f6c2001-09-13 13:46:56 +00005714
drhcac29a62010-07-02 19:36:52 +00005715#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00005716/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005717**
5718** Change the journal mode of database P1 to P3. P3 must be one of the
5719** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5720** modes (delete, truncate, persist, off and memory), this is a simple
5721** operation. No IO is required.
5722**
5723** If changing into or out of WAL mode the procedure is more complicated.
5724**
5725** Write a string containing the final journal-mode to register P2.
5726*/
drhd80b2332010-05-01 00:59:37 +00005727case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005728 Btree *pBt; /* Btree to change journal mode of */
5729 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005730 int eNew; /* New journal mode */
5731 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005732#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005733 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005734#endif
dane04dc882010-04-20 18:53:15 +00005735
drhd80b2332010-05-01 00:59:37 +00005736 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005737 assert( eNew==PAGER_JOURNALMODE_DELETE
5738 || eNew==PAGER_JOURNALMODE_TRUNCATE
5739 || eNew==PAGER_JOURNALMODE_PERSIST
5740 || eNew==PAGER_JOURNALMODE_OFF
5741 || eNew==PAGER_JOURNALMODE_MEMORY
5742 || eNew==PAGER_JOURNALMODE_WAL
5743 || eNew==PAGER_JOURNALMODE_QUERY
5744 );
5745 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005746 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005747
dane04dc882010-04-20 18:53:15 +00005748 pBt = db->aDb[pOp->p1].pBt;
5749 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005750 eOld = sqlite3PagerGetJournalMode(pPager);
5751 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5752 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005753
5754#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005755 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005756
drhd80b2332010-05-01 00:59:37 +00005757 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005758 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005759 */
5760 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005761 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005762 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005763 ){
drh0b9b4302010-06-11 17:01:24 +00005764 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005765 }
5766
drh0b9b4302010-06-11 17:01:24 +00005767 if( (eNew!=eOld)
5768 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5769 ){
danc0537fe2013-06-28 19:41:43 +00005770 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005771 rc = SQLITE_ERROR;
5772 sqlite3SetString(&p->zErrMsg, db,
5773 "cannot change %s wal mode from within a transaction",
5774 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5775 );
5776 break;
5777 }else{
5778
5779 if( eOld==PAGER_JOURNALMODE_WAL ){
5780 /* If leaving WAL mode, close the log file. If successful, the call
5781 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5782 ** file. An EXCLUSIVE lock may still be held on the database file
5783 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005784 */
drh0b9b4302010-06-11 17:01:24 +00005785 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005786 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005787 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005788 }
drh242c4f72010-06-22 14:49:39 +00005789 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5790 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5791 ** as an intermediate */
5792 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005793 }
5794
5795 /* Open a transaction on the database file. Regardless of the journal
5796 ** mode, this transaction always uses a rollback journal.
5797 */
5798 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5799 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005800 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005801 }
5802 }
5803 }
dan5cf53532010-05-01 16:40:20 +00005804#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005805
dand956efe2010-06-18 16:13:45 +00005806 if( rc ){
dand956efe2010-06-18 16:13:45 +00005807 eNew = eOld;
5808 }
drh0b9b4302010-06-11 17:01:24 +00005809 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005810
dane04dc882010-04-20 18:53:15 +00005811 pOut = &aMem[pOp->p2];
5812 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005813 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005814 pOut->n = sqlite3Strlen30(pOut->z);
5815 pOut->enc = SQLITE_UTF8;
5816 sqlite3VdbeChangeEncoding(pOut, encoding);
5817 break;
drhcac29a62010-07-02 19:36:52 +00005818};
5819#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005820
drhfdbcdee2007-03-27 14:44:50 +00005821#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005822/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005823**
5824** Vacuum the entire database. This opcode will cause other virtual
5825** machines to be created and run. It may not be called from within
5826** a transaction.
5827*/
drh9cbf3422008-01-17 16:22:13 +00005828case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005829 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005830 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005831 break;
5832}
drh154d4b22006-09-21 11:02:16 +00005833#endif
drh6f8c91c2003-12-07 00:24:35 +00005834
danielk1977dddbcdc2007-04-26 14:42:34 +00005835#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005836/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005837**
5838** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005839** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005840** P2. Otherwise, fall through to the next instruction.
5841*/
drh9cbf3422008-01-17 16:22:13 +00005842case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005843 Btree *pBt;
5844
5845 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005846 assert( DbMaskTest(p->btreeMask, pOp->p1) );
drh9e92a472013-06-27 17:40:30 +00005847 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005848 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005849 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00005850 VdbeBranchTaken(rc==SQLITE_DONE,2);
danielk1977dddbcdc2007-04-26 14:42:34 +00005851 if( rc==SQLITE_DONE ){
5852 pc = pOp->p2 - 1;
5853 rc = SQLITE_OK;
5854 }
5855 break;
5856}
5857#endif
5858
drh98757152008-01-09 23:04:12 +00005859/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005860**
drh25df48d2014-07-22 14:58:12 +00005861** Cause precompiled statements to expire. When an expired statement
5862** is executed using sqlite3_step() it will either automatically
5863** reprepare itself (if it was originally created using sqlite3_prepare_v2())
5864** or it will fail with SQLITE_SCHEMA.
danielk1977a21c6b62005-01-24 10:25:59 +00005865**
5866** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
drh25df48d2014-07-22 14:58:12 +00005867** then only the currently executing statement is expired.
danielk1977a21c6b62005-01-24 10:25:59 +00005868*/
drh9cbf3422008-01-17 16:22:13 +00005869case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005870 if( !pOp->p1 ){
5871 sqlite3ExpirePreparedStatements(db);
5872 }else{
5873 p->expired = 1;
5874 }
5875 break;
5876}
5877
danielk1977c00da102006-01-07 13:21:04 +00005878#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005879/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005880** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00005881**
5882** Obtain a lock on a particular table. This instruction is only used when
5883** the shared-cache feature is enabled.
5884**
danielk197796d48e92009-06-29 06:00:37 +00005885** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005886** on which the lock is acquired. A readlock is obtained if P3==0 or
5887** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005888**
5889** P2 contains the root-page of the table to lock.
5890**
drh66a51672008-01-03 00:01:23 +00005891** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005892** used to generate an error message if the lock cannot be obtained.
5893*/
drh9cbf3422008-01-17 16:22:13 +00005894case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005895 u8 isWriteLock = (u8)pOp->p3;
5896 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5897 int p1 = pOp->p1;
5898 assert( p1>=0 && p1<db->nDb );
drha7ab6d82014-07-21 15:44:39 +00005899 assert( DbMaskTest(p->btreeMask, p1) );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005900 assert( isWriteLock==0 || isWriteLock==1 );
5901 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5902 if( (rc&0xFF)==SQLITE_LOCKED ){
5903 const char *z = pOp->p4.z;
5904 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5905 }
danielk1977c00da102006-01-07 13:21:04 +00005906 }
5907 break;
5908}
drhb9bb7c12006-06-11 23:41:55 +00005909#endif /* SQLITE_OMIT_SHARED_CACHE */
5910
5911#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005912/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005913**
danielk19773e3a84d2008-08-01 17:37:40 +00005914** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5915** xBegin method for that table.
5916**
5917** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005918** within a callback to a virtual table xSync() method. If it is, the error
5919** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005920*/
drh9cbf3422008-01-17 16:22:13 +00005921case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005922 VTable *pVTab;
5923 pVTab = pOp->p4.pVtab;
5924 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00005925 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005926 break;
5927}
5928#endif /* SQLITE_OMIT_VIRTUALTABLE */
5929
5930#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005931/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005932**
drh66a51672008-01-03 00:01:23 +00005933** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005934** for that table.
5935*/
drh9cbf3422008-01-17 16:22:13 +00005936case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005937 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005938 break;
5939}
5940#endif /* SQLITE_OMIT_VIRTUALTABLE */
5941
5942#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005943/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005944**
drh66a51672008-01-03 00:01:23 +00005945** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005946** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005947*/
drh9cbf3422008-01-17 16:22:13 +00005948case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005949 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005950 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005951 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005952 break;
5953}
5954#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005955
drh9eff6162006-06-12 21:59:13 +00005956#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005957/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005958**
drh66a51672008-01-03 00:01:23 +00005959** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005960** P1 is a cursor number. This opcode opens a cursor to the virtual
5961** table and stores that cursor in P1.
5962*/
drh9cbf3422008-01-17 16:22:13 +00005963case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005964 VdbeCursor *pCur;
5965 sqlite3_vtab_cursor *pVtabCursor;
5966 sqlite3_vtab *pVtab;
5967 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005968
drh1713afb2013-06-28 01:24:57 +00005969 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005970 pCur = 0;
5971 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005972 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005973 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005974 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005975 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00005976 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005977 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005978 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005979 pVtabCursor->pVtab = pVtab;
5980
mistachkin48864df2013-03-21 21:20:32 +00005981 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005982 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005983 if( pCur ){
5984 pCur->pVtabCursor = pVtabCursor;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005985 }else{
drh17435752007-08-16 04:30:38 +00005986 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005987 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005988 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005989 }
drh9eff6162006-06-12 21:59:13 +00005990 break;
5991}
5992#endif /* SQLITE_OMIT_VIRTUALTABLE */
5993
5994#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005995/* Opcode: VFilter P1 P2 P3 P4 *
drh831116d2014-04-03 14:31:00 +00005996** Synopsis: iplan=r[P3] zplan='P4'
drh9eff6162006-06-12 21:59:13 +00005997**
5998** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5999** the filtered result set is empty.
6000**
drh66a51672008-01-03 00:01:23 +00006001** P4 is either NULL or a string that was generated by the xBestIndex
6002** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00006003** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00006004**
drh9eff6162006-06-12 21:59:13 +00006005** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00006006** by P1. The integer query plan parameter to xFilter is stored in register
6007** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00006008** xFilter method. Registers P3+2..P3+1+argc are the argc
6009** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00006010** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00006011**
danielk19776dbee812008-01-03 18:39:41 +00006012** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00006013*/
drh9cbf3422008-01-17 16:22:13 +00006014case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00006015 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00006016 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006017 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00006018 Mem *pQuery;
6019 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00006020 sqlite3_vtab_cursor *pVtabCursor;
6021 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00006022 VdbeCursor *pCur;
6023 int res;
6024 int i;
6025 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006026
drha6c2ed92009-11-14 23:22:23 +00006027 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00006028 pArgc = &pQuery[1];
6029 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00006030 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00006031 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006032 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00006033 pVtabCursor = pCur->pVtabCursor;
6034 pVtab = pVtabCursor->pVtab;
6035 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006036
drh9cbf3422008-01-17 16:22:13 +00006037 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00006038 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00006039 nArg = (int)pArgc->u.i;
6040 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006041
drh644a5292006-12-20 14:53:38 +00006042 /* Invoke the xFilter method */
6043 {
drh856c1032009-06-02 15:21:42 +00006044 res = 0;
6045 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00006046 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00006047 apArg[i] = &pArgc[i+1];
danielk19775fac9f82006-06-13 14:16:58 +00006048 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00006049
danielk1977be718892006-06-23 08:05:19 +00006050 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00006051 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00006052 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006053 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00006054 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00006055 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00006056 }
drh688852a2014-02-17 22:40:43 +00006057 VdbeBranchTaken(res!=0,2);
danielk1977a298e902006-06-22 09:53:48 +00006058 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00006059 pc = pOp->p2 - 1;
6060 }
6061 }
drh1d454a32008-01-31 19:34:51 +00006062 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006063
drh9eff6162006-06-12 21:59:13 +00006064 break;
6065}
6066#endif /* SQLITE_OMIT_VIRTUALTABLE */
6067
6068#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006069/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00006070** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00006071**
drh2133d822008-01-03 18:44:59 +00006072** Store the value of the P2-th column of
6073** the row of the virtual-table that the
6074** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00006075*/
6076case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00006077 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006078 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00006079 Mem *pDest;
6080 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006081
drhdfe88ec2008-11-03 20:55:06 +00006082 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006083 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00006084 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00006085 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00006086 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00006087 if( pCur->nullRow ){
6088 sqlite3VdbeMemSetNull(pDest);
6089 break;
6090 }
danielk19773e3a84d2008-08-01 17:37:40 +00006091 pVtab = pCur->pVtabCursor->pVtab;
6092 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006093 assert( pModule->xColumn );
6094 memset(&sContext, 0, sizeof(sContext));
drh9bd038f2014-08-27 14:14:06 +00006095 sContext.pOut = pDest;
6096 MemSetTypeFlag(pDest, MEM_Null);
drhde4fcfd2008-01-19 23:50:26 +00006097 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00006098 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00006099 if( sContext.isError ){
6100 rc = sContext.isError;
6101 }
drh9bd038f2014-08-27 14:14:06 +00006102 sqlite3VdbeChangeEncoding(pDest, encoding);
drh5ff44372009-11-24 16:26:17 +00006103 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00006104 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006105
drhde4fcfd2008-01-19 23:50:26 +00006106 if( sqlite3VdbeMemTooBig(pDest) ){
6107 goto too_big;
6108 }
drh9eff6162006-06-12 21:59:13 +00006109 break;
6110}
6111#endif /* SQLITE_OMIT_VIRTUALTABLE */
6112
6113#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006114/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006115**
6116** Advance virtual table P1 to the next row in its result set and
6117** jump to instruction P2. Or, if the virtual table has reached
6118** the end of its result set, then fall through to the next instruction.
6119*/
drh9cbf3422008-01-17 16:22:13 +00006120case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006121 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006122 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006123 int res;
drh856c1032009-06-02 15:21:42 +00006124 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006125
drhc54a6172009-06-02 16:06:03 +00006126 res = 0;
drh856c1032009-06-02 15:21:42 +00006127 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006128 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006129 if( pCur->nullRow ){
6130 break;
6131 }
danielk19773e3a84d2008-08-01 17:37:40 +00006132 pVtab = pCur->pVtabCursor->pVtab;
6133 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006134 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006135
drhde4fcfd2008-01-19 23:50:26 +00006136 /* Invoke the xNext() method of the module. There is no way for the
6137 ** underlying implementation to return an error if one occurs during
6138 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6139 ** data is available) and the error code returned when xColumn or
6140 ** some other method is next invoked on the save virtual table cursor.
6141 */
drhde4fcfd2008-01-19 23:50:26 +00006142 p->inVtabMethod = 1;
6143 rc = pModule->xNext(pCur->pVtabCursor);
6144 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006145 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006146 if( rc==SQLITE_OK ){
6147 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006148 }
drh688852a2014-02-17 22:40:43 +00006149 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006150 if( !res ){
6151 /* If there is data, jump to P2 */
6152 pc = pOp->p2 - 1;
6153 }
drh49afe3a2013-07-10 03:05:14 +00006154 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006155}
6156#endif /* SQLITE_OMIT_VIRTUALTABLE */
6157
danielk1977182c4ba2007-06-27 15:53:34 +00006158#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006159/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006160**
drh66a51672008-01-03 00:01:23 +00006161** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006162** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006163** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006164*/
drh9cbf3422008-01-17 16:22:13 +00006165case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006166 sqlite3_vtab *pVtab;
6167 Mem *pName;
6168
danielk1977595a5232009-07-24 17:58:53 +00006169 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006170 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006171 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006172 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006173 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006174 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006175 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006176 testcase( pName->enc==SQLITE_UTF8 );
6177 testcase( pName->enc==SQLITE_UTF16BE );
6178 testcase( pName->enc==SQLITE_UTF16LE );
6179 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6180 if( rc==SQLITE_OK ){
6181 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006182 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006183 p->expired = 0;
6184 }
danielk1977182c4ba2007-06-27 15:53:34 +00006185 break;
6186}
6187#endif
drh4cbdda92006-06-14 19:00:20 +00006188
6189#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006190/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006191** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006192**
drh66a51672008-01-03 00:01:23 +00006193** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006194** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006195** are contiguous memory cells starting at P3 to pass to the xUpdate
6196** invocation. The value in register (P3+P2-1) corresponds to the
6197** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006198**
6199** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006200** The argv[0] element (which corresponds to memory cell P3)
6201** is the rowid of a row to delete. If argv[0] is NULL then no
6202** deletion occurs. The argv[1] element is the rowid of the new
6203** row. This can be NULL to have the virtual table select the new
6204** rowid for itself. The subsequent elements in the array are
6205** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006206**
6207** If P2==1 then no insert is performed. argv[0] is the rowid of
6208** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006209**
6210** P1 is a boolean flag. If it is set to true and the xUpdate call
6211** is successful, then the value returned by sqlite3_last_insert_rowid()
6212** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006213**
6214** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6215** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006216*/
drh9cbf3422008-01-17 16:22:13 +00006217case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006218 sqlite3_vtab *pVtab;
6219 sqlite3_module *pModule;
6220 int nArg;
6221 int i;
6222 sqlite_int64 rowid;
6223 Mem **apArg;
6224 Mem *pX;
6225
danb061d052011-04-25 18:49:57 +00006226 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6227 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6228 );
drh9e92a472013-06-27 17:40:30 +00006229 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006230 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006231 pModule = (sqlite3_module *)pVtab->pModule;
6232 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006233 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006234 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006235 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006236 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006237 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006238 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006239 assert( memIsValid(pX) );
6240 memAboutToChange(p, pX);
drh9c419382006-06-16 21:13:21 +00006241 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006242 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006243 }
danb061d052011-04-25 18:49:57 +00006244 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006245 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006246 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006247 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006248 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006249 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006250 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006251 }
drhd91c1a12013-02-09 13:58:25 +00006252 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006253 if( pOp->p5==OE_Ignore ){
6254 rc = SQLITE_OK;
6255 }else{
6256 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6257 }
6258 }else{
6259 p->nChange++;
6260 }
danielk1977399918f2006-06-14 13:03:23 +00006261 }
drh4cbdda92006-06-14 19:00:20 +00006262 break;
danielk1977399918f2006-06-14 13:03:23 +00006263}
6264#endif /* SQLITE_OMIT_VIRTUALTABLE */
6265
danielk197759a93792008-05-15 17:48:20 +00006266#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6267/* Opcode: Pagecount P1 P2 * * *
6268**
6269** Write the current number of pages in database P1 to memory cell P2.
6270*/
6271case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006272 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006273 break;
6274}
6275#endif
6276
drh60ac3f42010-11-23 18:59:27 +00006277
6278#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6279/* Opcode: MaxPgcnt P1 P2 P3 * *
6280**
6281** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006282** Do not let the maximum page count fall below the current page count and
6283** do not change the maximum page count value if P3==0.
6284**
drh60ac3f42010-11-23 18:59:27 +00006285** Store the maximum page count after the change in register P2.
6286*/
6287case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006288 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006289 Btree *pBt;
6290
6291 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006292 newMax = 0;
6293 if( pOp->p3 ){
6294 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006295 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006296 }
6297 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006298 break;
6299}
6300#endif
6301
6302
drhaceb31b2014-02-08 01:40:27 +00006303/* Opcode: Init * P2 * P4 *
6304** Synopsis: Start at P2
6305**
6306** Programs contain a single instance of this opcode as the very first
6307** opcode.
drh949f9cd2008-01-12 21:35:57 +00006308**
6309** If tracing is enabled (by the sqlite3_trace()) interface, then
6310** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006311** Or if P4 is blank, use the string returned by sqlite3_sql().
6312**
6313** If P2 is not zero, jump to instruction P2.
drh949f9cd2008-01-12 21:35:57 +00006314*/
drhaceb31b2014-02-08 01:40:27 +00006315case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006316 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006317 char *z;
drh856c1032009-06-02 15:21:42 +00006318
drhaceb31b2014-02-08 01:40:27 +00006319 if( pOp->p2 ){
6320 pc = pOp->p2 - 1;
6321 }
6322#ifndef SQLITE_OMIT_TRACE
drh37f58e92012-09-04 21:34:26 +00006323 if( db->xTrace
6324 && !p->doingRerun
6325 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6326 ){
drhc3f1d5f2011-05-30 23:42:16 +00006327 z = sqlite3VdbeExpandSql(p, zTrace);
6328 db->xTrace(db->pTraceArg, z);
6329 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006330 }
drh8f8b2312013-10-18 20:03:43 +00006331#ifdef SQLITE_USE_FCNTL_TRACE
6332 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6333 if( zTrace ){
6334 int i;
6335 for(i=0; i<db->nDb; i++){
drha7ab6d82014-07-21 15:44:39 +00006336 if( DbMaskTest(p->btreeMask, i)==0 ) continue;
drh8f8b2312013-10-18 20:03:43 +00006337 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6338 }
6339 }
6340#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006341#ifdef SQLITE_DEBUG
6342 if( (db->flags & SQLITE_SqlTrace)!=0
6343 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6344 ){
6345 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6346 }
6347#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00006348#endif /* SQLITE_OMIT_TRACE */
drh949f9cd2008-01-12 21:35:57 +00006349 break;
6350}
drh949f9cd2008-01-12 21:35:57 +00006351
drh91fd4d42008-01-19 20:11:25 +00006352
6353/* Opcode: Noop * * * * *
6354**
6355** Do nothing. This instruction is often useful as a jump
6356** destination.
drh5e00f6c2001-09-13 13:46:56 +00006357*/
drh91fd4d42008-01-19 20:11:25 +00006358/*
6359** The magic Explain opcode are only inserted when explain==2 (which
6360** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6361** This opcode records information from the optimizer. It is the
6362** the same as a no-op. This opcodesnever appears in a real VM program.
6363*/
6364default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006365 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006366 break;
6367}
6368
6369/*****************************************************************************
6370** The cases of the switch statement above this line should all be indented
6371** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6372** readability. From this point on down, the normal indentation rules are
6373** restored.
6374*****************************************************************************/
6375 }
drh6e142f52000-06-08 13:36:40 +00006376
drh7b396862003-01-01 23:06:20 +00006377#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006378 {
drha01c7c72014-04-25 12:35:31 +00006379 u64 endTime = sqlite3Hwtime();
6380 if( endTime>start ) pOp->cycles += endTime - start;
drh8178a752003-01-05 21:41:40 +00006381 pOp->cnt++;
drh8178a752003-01-05 21:41:40 +00006382 }
drh7b396862003-01-01 23:06:20 +00006383#endif
6384
drh6e142f52000-06-08 13:36:40 +00006385 /* The following code adds nothing to the actual functionality
6386 ** of the program. It is only here for testing and debugging.
6387 ** On the other hand, it does burn CPU cycles every time through
6388 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6389 */
6390#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006391 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006392
drhcf1023c2007-05-08 20:59:49 +00006393#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006394 if( db->flags & SQLITE_VdbeTrace ){
6395 if( rc!=0 ) printf("rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006396 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
drh84e55a82013-11-13 17:58:23 +00006397 registerTrace(pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006398 }
drh3c657212009-11-17 23:59:58 +00006399 if( pOp->opflags & OPFLG_OUT3 ){
drh84e55a82013-11-13 17:58:23 +00006400 registerTrace(pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006401 }
drh75897232000-05-29 14:26:00 +00006402 }
danielk1977b5402fb2005-01-12 07:15:04 +00006403#endif /* SQLITE_DEBUG */
6404#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006405 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006406
drha05a7222008-01-19 03:35:58 +00006407 /* If we reach this point, it means that execution is finished with
6408 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006409 */
drha05a7222008-01-19 03:35:58 +00006410vdbe_error_halt:
6411 assert( rc );
6412 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006413 testcase( sqlite3GlobalConfig.xLog!=0 );
6414 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6415 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006416 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006417 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6418 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006419 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006420 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006421 }
drh900b31e2007-08-28 02:27:51 +00006422
6423 /* This is the only way out of this procedure. We have to
6424 ** release the mutexes on btrees that were acquired at the
6425 ** top. */
6426vdbe_return:
drh99a66922011-05-13 18:51:42 +00006427 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006428 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006429 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006430 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006431 return rc;
6432
drh023ae032007-05-08 12:12:16 +00006433 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6434 ** is encountered.
6435 */
6436too_big:
drhf089aa42008-07-08 19:34:06 +00006437 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006438 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006439 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006440
drh98640a32007-06-07 19:08:32 +00006441 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006442 */
6443no_mem:
drh17435752007-08-16 04:30:38 +00006444 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006445 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006446 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006447 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006448
drhb86ccfb2003-01-28 23:13:10 +00006449 /* Jump to here for any other kind of fatal error. The "rc" variable
6450 ** should hold the error number.
6451 */
6452abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006453 assert( p->zErrMsg==0 );
6454 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006455 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006456 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006457 }
drha05a7222008-01-19 03:35:58 +00006458 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006459
danielk19776f8a5032004-05-10 10:34:51 +00006460 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006461 ** flag.
6462 */
6463abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006464 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006465 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006466 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006467 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006468 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006469}