blob: 002309bc3b45a3b8779a48eaa862acd90cd24e67 [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
31** like that every 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/*
drh688852a2014-02-17 22:40:43 +0000111** Invoke the VDBE coverage callback, if defined
112*/
113#if !defined(SQLITE_VDBE_COVERAGE)
114# define VdbeBranchTaken(I,M)
115#else
116# define VdbeBranchTaken(I,M) \
117 if( sqlite3GlobalConfig.xVdbeBranch!=0 ){ \
118 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, \
119 pOp->iSrcLine,(I),(M)); }
120#endif
121
122/*
drh9cbf3422008-01-17 16:22:13 +0000123** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000124** already. Return non-zero if a malloc() fails.
125*/
drhb21c8cd2007-08-21 19:33:56 +0000126#define Stringify(P, enc) \
127 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000128 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000129
130/*
danielk1977bd7e4602004-05-24 07:34:48 +0000131** An ephemeral string value (signified by the MEM_Ephem flag) contains
132** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000133** is responsible for deallocating that string. Because the register
134** does not control the string, it might be deleted without the register
135** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000136**
137** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000138** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000139** converts an MEM_Ephem string into an MEM_Dyn string.
140*/
drhb21c8cd2007-08-21 19:33:56 +0000141#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000142 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000143 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000144
dan689ab892011-08-12 15:02:00 +0000145/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
drh0fd61352014-02-07 02:29:45 +0000146#define isSorter(x) ((x)->pSorter!=0)
dan689ab892011-08-12 15:02:00 +0000147
danielk19771cc5ed82007-05-16 17:28:43 +0000148/*
drhdfe88ec2008-11-03 20:55:06 +0000149** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000150** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000151*/
drhdfe88ec2008-11-03 20:55:06 +0000152static VdbeCursor *allocateCursor(
153 Vdbe *p, /* The virtual machine */
154 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000155 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000156 int iDb, /* Database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000157 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000158){
159 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000160 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000161 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000162 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000163 **
164 ** * Sometimes cursor numbers are used for a couple of different
165 ** purposes in a vdbe program. The different uses might require
166 ** different sized allocations. Memory cells provide growable
167 ** allocations.
168 **
169 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
170 ** be freed lazily via the sqlite3_release_memory() API. This
171 ** minimizes the number of malloc calls made by the system.
172 **
173 ** Memory cells for cursors are allocated at the top of the address
174 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
175 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
176 */
177 Mem *pMem = &p->aMem[p->nMem-iCur];
178
danielk19775f096132008-03-28 15:44:09 +0000179 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000180 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000181 nByte =
drh5cc10232013-11-21 01:04:02 +0000182 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
183 (isBtreeCursor?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000184
drh290c1942004-08-21 17:54:45 +0000185 assert( iCur<p->nCursor );
186 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000187 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000188 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000189 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000190 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000191 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000192 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000193 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000194 pCx->nField = nField;
danielk1977cd3e8f72008-03-25 09:47:35 +0000195 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000196 pCx->pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000197 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhf25a5072009-11-18 23:01:25 +0000198 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000199 }
danielk197794eb6a12005-12-15 15:22:08 +0000200 }
drh4774b132004-06-12 20:12:51 +0000201 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000202}
203
danielk19773d1bfea2004-05-14 11:00:53 +0000204/*
drh29d72102006-02-09 22:13:41 +0000205** Try to convert a value into a numeric representation if we can
206** do so without loss of information. In other words, if the string
207** looks like a number, convert it into a number. If it does not
208** look like a number, leave it alone.
209*/
drhb21c8cd2007-08-21 19:33:56 +0000210static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000211 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000212 double rValue;
213 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000214 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000215 if( (pRec->flags&MEM_Str)==0 ) return;
216 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000217 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000218 pRec->u.i = iValue;
219 pRec->flags |= MEM_Int;
220 }else{
221 pRec->r = rValue;
222 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000223 }
224 }
225}
226
227/*
drh8a512562005-11-14 22:29:05 +0000228** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000229**
drh8a512562005-11-14 22:29:05 +0000230** SQLITE_AFF_INTEGER:
231** SQLITE_AFF_REAL:
232** SQLITE_AFF_NUMERIC:
233** Try to convert pRec to an integer representation or a
234** floating-point representation if an integer representation
235** is not possible. Note that the integer representation is
236** always preferred, even if the affinity is REAL, because
237** an integer representation is more space efficient on disk.
238**
239** SQLITE_AFF_TEXT:
240** Convert pRec to a text representation.
241**
242** SQLITE_AFF_NONE:
243** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000244*/
drh17435752007-08-16 04:30:38 +0000245static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000246 Mem *pRec, /* The value to apply affinity to */
247 char affinity, /* The affinity to be applied */
248 u8 enc /* Use this text encoding */
249){
drh8a512562005-11-14 22:29:05 +0000250 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000251 /* Only attempt the conversion to TEXT if there is an integer or real
252 ** representation (blob and NULL do not get converted) but no string
253 ** representation.
254 */
255 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000256 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000257 }
258 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000259 }else if( affinity!=SQLITE_AFF_NONE ){
260 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
261 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000262 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000263 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000264 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000265 }
danielk19773d1bfea2004-05-14 11:00:53 +0000266 }
267}
268
danielk1977aee18ef2005-03-09 12:26:50 +0000269/*
drh29d72102006-02-09 22:13:41 +0000270** Try to convert the type of a function argument or a result column
271** into a numeric representation. Use either INTEGER or REAL whichever
272** is appropriate. But only do the conversion if it is possible without
273** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000274*/
275int sqlite3_value_numeric_type(sqlite3_value *pVal){
drh1b27b8c2014-02-10 03:21:57 +0000276 int eType = sqlite3_value_type(pVal);
277 if( eType==SQLITE_TEXT ){
278 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000279 applyNumericAffinity(pMem);
280 sqlite3VdbeMemStoreType(pMem);
drh1b27b8c2014-02-10 03:21:57 +0000281 eType = sqlite3_value_type(pVal);
drhe5a8a1d2010-11-18 12:31:24 +0000282 }
drh1b27b8c2014-02-10 03:21:57 +0000283 return eType;
drh29d72102006-02-09 22:13:41 +0000284}
285
286/*
danielk1977aee18ef2005-03-09 12:26:50 +0000287** Exported version of applyAffinity(). This one works on sqlite3_value*,
288** not the internal Mem* type.
289*/
danielk19771e536952007-08-16 10:09:01 +0000290void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000291 sqlite3_value *pVal,
292 u8 affinity,
293 u8 enc
294){
drhb21c8cd2007-08-21 19:33:56 +0000295 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000296}
297
danielk1977b5402fb2005-01-12 07:15:04 +0000298#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000299/*
danielk1977ca6b2912004-05-21 10:49:47 +0000300** Write a nice string representation of the contents of cell pMem
301** into buffer zBuf, length nBuf.
302*/
drh74161702006-02-24 02:53:49 +0000303void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000304 char *zCsr = zBuf;
305 int f = pMem->flags;
306
drh57196282004-10-06 15:41:16 +0000307 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000308
danielk1977ca6b2912004-05-21 10:49:47 +0000309 if( f&MEM_Blob ){
310 int i;
311 char c;
312 if( f & MEM_Dyn ){
313 c = 'z';
314 assert( (f & (MEM_Static|MEM_Ephem))==0 );
315 }else if( f & MEM_Static ){
316 c = 't';
317 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
318 }else if( f & MEM_Ephem ){
319 c = 'e';
320 assert( (f & (MEM_Static|MEM_Dyn))==0 );
321 }else{
322 c = 's';
323 }
324
drh5bb3eb92007-05-04 13:15:55 +0000325 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000326 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000327 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000328 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000329 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000330 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000331 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000332 }
333 for(i=0; i<16 && i<pMem->n; i++){
334 char z = pMem->z[i];
335 if( z<32 || z>126 ) *zCsr++ = '.';
336 else *zCsr++ = z;
337 }
338
drhe718efe2007-05-10 21:14:03 +0000339 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000340 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000341 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000342 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000343 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000344 }
danielk1977b1bc9532004-05-22 03:05:33 +0000345 *zCsr = '\0';
346 }else if( f & MEM_Str ){
347 int j, k;
348 zBuf[0] = ' ';
349 if( f & MEM_Dyn ){
350 zBuf[1] = 'z';
351 assert( (f & (MEM_Static|MEM_Ephem))==0 );
352 }else if( f & MEM_Static ){
353 zBuf[1] = 't';
354 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
355 }else if( f & MEM_Ephem ){
356 zBuf[1] = 'e';
357 assert( (f & (MEM_Static|MEM_Dyn))==0 );
358 }else{
359 zBuf[1] = 's';
360 }
361 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000362 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000363 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000364 zBuf[k++] = '[';
365 for(j=0; j<15 && j<pMem->n; j++){
366 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000367 if( c>=0x20 && c<0x7f ){
368 zBuf[k++] = c;
369 }else{
370 zBuf[k++] = '.';
371 }
372 }
373 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000374 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000375 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000376 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000377 }
danielk1977ca6b2912004-05-21 10:49:47 +0000378}
379#endif
380
drh5b6afba2008-01-05 16:29:28 +0000381#ifdef SQLITE_DEBUG
382/*
383** Print the value of a register for tracing purposes:
384*/
drh84e55a82013-11-13 17:58:23 +0000385static void memTracePrint(Mem *p){
drha5750cf2014-02-07 13:20:31 +0000386 if( p->flags & MEM_Undefined ){
drh84e55a82013-11-13 17:58:23 +0000387 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000388 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000389 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000390 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000391 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000392 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000393 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000394#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000395 }else if( p->flags & MEM_Real ){
drh84e55a82013-11-13 17:58:23 +0000396 printf(" r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000397#endif
drh733bf1b2009-04-22 00:47:00 +0000398 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000399 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000400 }else{
401 char zBuf[200];
402 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000403 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000404 }
405}
drh84e55a82013-11-13 17:58:23 +0000406static void registerTrace(int iReg, Mem *p){
407 printf("REG[%d] = ", iReg);
408 memTracePrint(p);
409 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000410}
411#endif
412
413#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000414# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000415#else
416# define REGISTER_TRACE(R,M)
417#endif
418
danielk197784ac9d02004-05-18 09:58:06 +0000419
drh7b396862003-01-01 23:06:20 +0000420#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000421
422/*
423** hwtime.h contains inline assembler code for implementing
424** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000425*/
shane9bcbdad2008-05-29 20:22:37 +0000426#include "hwtime.h"
427
drh7b396862003-01-01 23:06:20 +0000428#endif
429
danielk1977fd7f0452008-12-17 17:30:26 +0000430#ifndef NDEBUG
431/*
432** This function is only called from within an assert() expression. It
433** checks that the sqlite3.nTransaction variable is correctly set to
434** the number of non-transaction savepoints currently in the
435** linked list starting at sqlite3.pSavepoint.
436**
437** Usage:
438**
439** assert( checkSavepointCount(db) );
440*/
441static int checkSavepointCount(sqlite3 *db){
442 int n = 0;
443 Savepoint *p;
444 for(p=db->pSavepoint; p; p=p->pNext) n++;
445 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
446 return 1;
447}
448#endif
449
drhb9755982010-07-24 16:34:37 +0000450
451/*
drh0fd61352014-02-07 02:29:45 +0000452** Execute as much of a VDBE program as we can.
453** This is the core of sqlite3_step().
drhb86ccfb2003-01-28 23:13:10 +0000454*/
danielk19774adee202004-05-08 08:23:19 +0000455int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000456 Vdbe *p /* The VDBE */
457){
shaneh84f4b2f2010-02-26 01:46:54 +0000458 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000459 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000460 Op *pOp; /* Current operation */
461 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000462 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000463 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000464 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000465 int iCompare = 0; /* Result of last OP_Compare operation */
466 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000467#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000468 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000469#endif
drha6c2ed92009-11-14 23:22:23 +0000470 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000471 Mem *pIn1 = 0; /* 1st input operand */
472 Mem *pIn2 = 0; /* 2nd input operand */
473 Mem *pIn3 = 0; /* 3rd input operand */
474 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000475 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000476 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000477#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000478 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000479 int origPc; /* Program counter at start of opcode */
480#endif
drh856c1032009-06-02 15:21:42 +0000481 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000482
drhca48c902008-01-18 14:08:24 +0000483 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000484 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000485 if( p->rc==SQLITE_NOMEM ){
486 /* This happens if a malloc() inside a call to sqlite3_column_text() or
487 ** sqlite3_column_text16() failed. */
488 goto no_mem;
489 }
drh3a840692003-01-29 22:58:26 +0000490 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000491 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000492 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000493 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000494 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000495 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000496 db->busyHandler.nBusy = 0;
drh0fd61352014-02-07 02:29:45 +0000497 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh602c2372007-03-01 00:29:13 +0000498 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000499#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
500 if( db->xProgress ){
501 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000502 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000503 if( nProgressLimit==0 ){
504 nProgressLimit = db->nProgressOps;
505 }else{
506 nProgressLimit %= (unsigned)db->nProgressOps;
507 }
508 }
509#endif
drh3c23a882007-01-09 14:01:13 +0000510#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000511 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000512 if( p->pc==0
513 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
514 ){
drh3c23a882007-01-09 14:01:13 +0000515 int i;
drh84e55a82013-11-13 17:58:23 +0000516 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000517 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000518 if( p->db->flags & SQLITE_VdbeListing ){
519 printf("VDBE Program Listing:\n");
520 for(i=0; i<p->nOp; i++){
521 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
522 }
drh3c23a882007-01-09 14:01:13 +0000523 }
drh84e55a82013-11-13 17:58:23 +0000524 if( p->db->flags & SQLITE_VdbeEQP ){
525 for(i=0; i<p->nOp; i++){
526 if( aOp[i].opcode==OP_Explain ){
527 if( once ) printf("VDBE Query Plan:\n");
528 printf("%s\n", aOp[i].p4.z);
529 once = 0;
530 }
531 }
532 }
533 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000534 }
danielk19772d1d86f2008-06-20 14:59:51 +0000535 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000536#endif
drhb86ccfb2003-01-28 23:13:10 +0000537 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000538 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000539 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000540#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000541 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000542 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000543#endif
drhbf159fa2013-06-25 22:01:22 +0000544 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000545 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000546
danielk19778b60e0f2005-01-12 09:10:39 +0000547 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000548 */
danielk19778b60e0f2005-01-12 09:10:39 +0000549#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000550 if( db->flags & SQLITE_VdbeTrace ){
551 sqlite3VdbePrintOp(stdout, pc, pOp);
drh75897232000-05-29 14:26:00 +0000552 }
drh3f7d4e42004-07-24 14:35:58 +0000553#endif
554
drh6e142f52000-06-08 13:36:40 +0000555
drhf6038712004-02-08 18:07:34 +0000556 /* Check to see if we need to simulate an interrupt. This only happens
557 ** if we have a special test build.
558 */
559#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000560 if( sqlite3_interrupt_count>0 ){
561 sqlite3_interrupt_count--;
562 if( sqlite3_interrupt_count==0 ){
563 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000564 }
565 }
566#endif
567
drhb5b407e2012-08-29 10:28:43 +0000568 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000569 ** external allocations out of mem[p2] and set mem[p2] to be
570 ** an undefined integer. Opcodes will either fill in the integer
571 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000572 */
drha6c2ed92009-11-14 23:22:23 +0000573 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000574 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
575 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000576 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000577 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000578 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000579 VdbeMemRelease(pOut);
drh3c657212009-11-17 23:59:58 +0000580 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000581 }
drh3c657212009-11-17 23:59:58 +0000582
583 /* Sanity checking on other operands */
584#ifdef SQLITE_DEBUG
585 if( (pOp->opflags & OPFLG_IN1)!=0 ){
586 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000587 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000588 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000589 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
590 }
591 if( (pOp->opflags & OPFLG_IN2)!=0 ){
592 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000593 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000594 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000595 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
596 }
597 if( (pOp->opflags & OPFLG_IN3)!=0 ){
598 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000599 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000600 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000601 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
602 }
603 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
604 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000605 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000606 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000607 }
608 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
609 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000610 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000611 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000612 }
613#endif
drh93952eb2009-11-13 19:43:43 +0000614
drh75897232000-05-29 14:26:00 +0000615 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000616
drh5e00f6c2001-09-13 13:46:56 +0000617/*****************************************************************************
618** What follows is a massive switch statement where each case implements a
619** separate instruction in the virtual machine. If we follow the usual
620** indentation conventions, each case should be indented by 6 spaces. But
621** that is a lot of wasted space on the left margin. So the code within
622** the switch statement will break with convention and be flush-left. Another
623** big comment (similar to this one) will mark the point in the code where
624** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000625**
626** The formatting of each case is important. The makefile for SQLite
627** generates two C files "opcodes.h" and "opcodes.c" by scanning this
628** file looking for lines that begin with "case OP_". The opcodes.h files
629** will be filled with #defines that give unique integer values to each
630** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000631** each string is the symbolic name for the corresponding opcode. If the
632** case statement is followed by a comment of the form "/# same as ... #/"
633** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000634**
drh9cbf3422008-01-17 16:22:13 +0000635** Other keywords in the comment that follows each case are used to
636** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
637** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
638** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000639**
drhac82fcf2002-09-08 17:23:41 +0000640** Documentation about VDBE opcodes is generated by scanning this file
641** for lines of that contain "Opcode:". That line and all subsequent
642** comment lines are used in the generation of the opcode.html documentation
643** file.
644**
645** SUMMARY:
646**
647** Formatting is important to scripts that scan this file.
648** Do not deviate from the formatting style currently in use.
649**
drh5e00f6c2001-09-13 13:46:56 +0000650*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000651
drh9cbf3422008-01-17 16:22:13 +0000652/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000653**
654** An unconditional jump to address P2.
655** The next instruction executed will be
656** the one at index P2 from the beginning of
657** the program.
658*/
drh9cbf3422008-01-17 16:22:13 +0000659case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000660 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000661
662 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
663 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
664 ** completion. Check to see if sqlite3_interrupt() has been called
665 ** or if the progress callback needs to be invoked.
666 **
667 ** This code uses unstructured "goto" statements and does not look clean.
668 ** But that is not due to sloppy coding habits. The code is written this
669 ** way for performance, to avoid having to run the interrupt and progress
670 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
671 ** faster according to "valgrind --tool=cachegrind" */
672check_for_interrupt:
drh0fd61352014-02-07 02:29:45 +0000673 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drh49afe3a2013-07-10 03:05:14 +0000674#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
675 /* Call the progress callback if it is configured and the required number
676 ** of VDBE ops have been executed (either since this invocation of
677 ** sqlite3VdbeExec() or since last time the progress callback was called).
678 ** If the progress callback returns non-zero, exit the virtual machine with
679 ** a return code SQLITE_ABORT.
680 */
drh0d1961e2013-07-25 16:27:51 +0000681 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000682 assert( db->nProgressOps!=0 );
683 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
684 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000685 rc = SQLITE_INTERRUPT;
686 goto vdbe_error_halt;
687 }
drh49afe3a2013-07-10 03:05:14 +0000688 }
689#endif
690
drh5e00f6c2001-09-13 13:46:56 +0000691 break;
692}
drh75897232000-05-29 14:26:00 +0000693
drh2eb95372008-06-06 15:04:36 +0000694/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000695**
drh2eb95372008-06-06 15:04:36 +0000696** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000697** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000698*/
drhb8475df2011-12-09 16:21:19 +0000699case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000700 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000701 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000702 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000703 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000704 pIn1->flags = MEM_Int;
705 pIn1->u.i = pc;
706 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000707 pc = pOp->p2 - 1;
708 break;
709}
710
drh2eb95372008-06-06 15:04:36 +0000711/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000712**
drh81cf13e2014-02-07 18:27:53 +0000713** Jump to the next instruction after the address in register P1. After
714** the jump, register P1 becomes undefined.
drh8c74a8c2002-08-25 19:20:40 +0000715*/
drh2eb95372008-06-06 15:04:36 +0000716case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000717 pIn1 = &aMem[pOp->p1];
drh81cf13e2014-02-07 18:27:53 +0000718 assert( pIn1->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000719 pc = (int)pIn1->u.i;
drh81cf13e2014-02-07 18:27:53 +0000720 pIn1->flags = MEM_Undefined;
drh8c74a8c2002-08-25 19:20:40 +0000721 break;
722}
723
drhed71a832014-02-07 19:18:10 +0000724/* Opcode: InitCoroutine P1 P2 P3 * *
drh81cf13e2014-02-07 18:27:53 +0000725**
drhed71a832014-02-07 19:18:10 +0000726** Set up register P1 so that it will OP_Yield to the co-routine
727** located at address P3.
728**
729** If P2!=0 then the co-routine implementation immediately follows
730** this opcode. So jump over the co-routine implementation to
731** address P2.
drh81cf13e2014-02-07 18:27:53 +0000732*/
733case OP_InitCoroutine: { /* jump */
drhed71a832014-02-07 19:18:10 +0000734 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
735 assert( pOp->p2>=0 && pOp->p2<p->nOp );
736 assert( pOp->p3>=0 && pOp->p3<p->nOp );
drh81cf13e2014-02-07 18:27:53 +0000737 pOut = &aMem[pOp->p1];
drhed71a832014-02-07 19:18:10 +0000738 assert( !VdbeMemDynamic(pOut) );
739 pOut->u.i = pOp->p3 - 1;
drh81cf13e2014-02-07 18:27:53 +0000740 pOut->flags = MEM_Int;
drhed71a832014-02-07 19:18:10 +0000741 if( pOp->p2 ) pc = pOp->p2 - 1;
drh81cf13e2014-02-07 18:27:53 +0000742 break;
743}
744
745/* Opcode: EndCoroutine P1 * * * *
746**
747** The instruction at the address in register P1 is an OP_Yield.
748** Jump to the P2 parameter of that OP_Yield.
749** After the jump, register P1 becomes undefined.
750*/
751case OP_EndCoroutine: { /* in1 */
752 VdbeOp *pCaller;
753 pIn1 = &aMem[pOp->p1];
754 assert( pIn1->flags==MEM_Int );
755 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
756 pCaller = &aOp[pIn1->u.i];
757 assert( pCaller->opcode==OP_Yield );
758 assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
759 pc = pCaller->p2 - 1;
760 pIn1->flags = MEM_Undefined;
761 break;
762}
763
764/* Opcode: Yield P1 P2 * * *
drhe00ee6e2008-06-20 15:24:01 +0000765**
766** Swap the program counter with the value in register P1.
drh81cf13e2014-02-07 18:27:53 +0000767**
768** If the co-routine ends with OP_Yield or OP_Return then continue
769** to the next instruction. But if the co-routine ends with
770** OP_EndCoroutine, jump immediately to P2.
drhe00ee6e2008-06-20 15:24:01 +0000771*/
drh81cf13e2014-02-07 18:27:53 +0000772case OP_Yield: { /* in1, jump */
drhe00ee6e2008-06-20 15:24:01 +0000773 int pcDest;
drh3c657212009-11-17 23:59:58 +0000774 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000775 assert( (pIn1->flags & MEM_Dyn)==0 );
776 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000777 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000778 pIn1->u.i = pc;
779 REGISTER_TRACE(pOp->p1, pIn1);
780 pc = pcDest;
781 break;
782}
783
drhf9c8ce32013-11-05 13:33:55 +0000784/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh0fd61352014-02-07 02:29:45 +0000785** Synopsis: if r[P3]=null halt
drh5053a792009-02-20 03:02:23 +0000786**
drhef8662b2011-06-20 21:47:58 +0000787** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000788** parameter P1, P2, and P4 as if this were a Halt instruction. If the
789** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000790** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000791*/
792case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000793 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000794 if( (pIn3->flags & MEM_Null)==0 ) break;
795 /* Fall through into OP_Halt */
796}
drhe00ee6e2008-06-20 15:24:01 +0000797
drhf9c8ce32013-11-05 13:33:55 +0000798/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000799**
drh3d4501e2008-12-04 20:40:10 +0000800** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000801** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000802**
drh92f02c32004-09-02 14:57:08 +0000803** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
804** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
805** For errors, it can be some other value. If P1!=0 then P2 will determine
806** whether or not to rollback the current transaction. Do not rollback
807** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
808** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000809** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000810**
drh66a51672008-01-03 00:01:23 +0000811** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000812**
drhf9c8ce32013-11-05 13:33:55 +0000813** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
814**
815** 0: (no change)
816** 1: NOT NULL contraint failed: P4
817** 2: UNIQUE constraint failed: P4
818** 3: CHECK constraint failed: P4
819** 4: FOREIGN KEY constraint failed: P4
820**
821** If P5 is not zero and P4 is NULL, then everything after the ":" is
822** omitted.
823**
drh9cfcf5d2002-01-29 18:41:24 +0000824** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000825** every program. So a jump past the last instruction of the program
826** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000827*/
drh9cbf3422008-01-17 16:22:13 +0000828case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000829 const char *zType;
830 const char *zLogFmt;
831
dan165921a2009-08-28 18:53:45 +0000832 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000833 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000834 VdbeFrame *pFrame = p->pFrame;
835 p->pFrame = pFrame->pParent;
836 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000837 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000838 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000839 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000840 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000841 /* Instruction pc is the OP_Program that invoked the sub-program
842 ** currently being halted. If the p2 instruction of this OP_Halt
843 ** instruction is set to OE_Ignore, then the sub-program is throwing
844 ** an IGNORE exception. In this case jump to the address specified
845 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000846 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000847 }
drhbbe879d2009-11-14 18:04:35 +0000848 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000849 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000850 break;
851 }
drh92f02c32004-09-02 14:57:08 +0000852 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000853 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000854 p->pc = pc;
drhf9c8ce32013-11-05 13:33:55 +0000855 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000856 if( pOp->p5 ){
857 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
858 "FOREIGN KEY" };
859 assert( pOp->p5>=1 && pOp->p5<=4 );
860 testcase( pOp->p5==1 );
861 testcase( pOp->p5==2 );
862 testcase( pOp->p5==3 );
863 testcase( pOp->p5==4 );
864 zType = azType[pOp->p5-1];
865 }else{
866 zType = 0;
867 }
drh4308e342013-11-11 16:55:52 +0000868 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000869 zLogFmt = "abort at %d in [%s]: %s";
870 if( zType && pOp->p4.z ){
871 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
872 zType, pOp->p4.z);
873 }else if( pOp->p4.z ){
874 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000875 }else{
drh4308e342013-11-11 16:55:52 +0000876 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000877 }
878 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000879 }
drh92f02c32004-09-02 14:57:08 +0000880 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000881 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000882 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000883 p->rc = rc = SQLITE_BUSY;
884 }else{
drhd91c1a12013-02-09 13:58:25 +0000885 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000886 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000887 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000888 }
drh900b31e2007-08-28 02:27:51 +0000889 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000890}
drhc61053b2000-06-04 12:58:36 +0000891
drh4c583122008-01-04 22:01:03 +0000892/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000893** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000894**
drh9cbf3422008-01-17 16:22:13 +0000895** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000896*/
drh4c583122008-01-04 22:01:03 +0000897case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000898 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000899 break;
900}
901
drh4c583122008-01-04 22:01:03 +0000902/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000903** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +0000904**
drh66a51672008-01-03 00:01:23 +0000905** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000906** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000907*/
drh4c583122008-01-04 22:01:03 +0000908case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000909 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000910 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000911 break;
912}
drh4f26d6c2004-05-26 23:25:30 +0000913
drh13573c72010-01-12 17:04:07 +0000914#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000915/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000916** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +0000917**
drh4c583122008-01-04 22:01:03 +0000918** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000919** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000920*/
drh4c583122008-01-04 22:01:03 +0000921case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
922 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000923 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000924 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000925 break;
926}
drh13573c72010-01-12 17:04:07 +0000927#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000928
drh3c84ddf2008-01-09 02:15:38 +0000929/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000930** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +0000931**
drh66a51672008-01-03 00:01:23 +0000932** P4 points to a nul terminated UTF-8 string. This opcode is transformed
drh0fd61352014-02-07 02:29:45 +0000933** into an OP_String before it is executed for the first time. During
934** this transformation, the length of string P4 is computed and stored
935** as the P1 parameter.
danielk1977cbb18d22004-05-28 11:37:27 +0000936*/
drh4c583122008-01-04 22:01:03 +0000937case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000938 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000939 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000940 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000941
942#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000943 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000944 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
945 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000946 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000947 assert( pOut->zMalloc==pOut->z );
948 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000949 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000950 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000951 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000952 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000953 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000954 }
drh66a51672008-01-03 00:01:23 +0000955 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000956 pOp->p4.z = pOut->z;
957 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000958 }
danielk197793758c82005-01-21 08:13:14 +0000959#endif
drhbb4957f2008-03-20 14:03:29 +0000960 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000961 goto too_big;
962 }
963 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000964}
drhf4479502004-05-27 03:12:53 +0000965
drh4c583122008-01-04 22:01:03 +0000966/* Opcode: String P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000967** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +0000968**
drh9cbf3422008-01-17 16:22:13 +0000969** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000970*/
drh4c583122008-01-04 22:01:03 +0000971case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000972 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000973 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
974 pOut->z = pOp->p4.z;
975 pOut->n = pOp->p1;
976 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000977 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000978 break;
979}
980
drh053a1282012-09-19 21:15:46 +0000981/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +0000982** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +0000983**
drhb8475df2011-12-09 16:21:19 +0000984** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +0000985** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +0000986** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +0000987** set to NULL.
988**
989** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
990** NULL values will not compare equal even if SQLITE_NULLEQ is set on
991** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +0000992*/
drh4c583122008-01-04 22:01:03 +0000993case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +0000994 int cnt;
drh053a1282012-09-19 21:15:46 +0000995 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +0000996 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +0000997 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +0000998 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +0000999 while( cnt>0 ){
1000 pOut++;
1001 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +00001002 VdbeMemRelease(pOut);
drh053a1282012-09-19 21:15:46 +00001003 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001004 cnt--;
1005 }
drhf0863fe2005-06-12 21:35:51 +00001006 break;
1007}
1008
drh05a86c52014-02-16 01:55:49 +00001009/* Opcode: SoftNull P1 * * * *
1010** Synopsis: r[P1]=NULL
1011**
1012** Set register P1 to have the value NULL as seen by the OP_MakeRecord
1013** instruction, but do not free any string or blob memory associated with
1014** the register, so that if the value was a string or blob that was
1015** previously copied using OP_SCopy, the copies will continue to be valid.
1016*/
1017case OP_SoftNull: {
1018 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
1019 pOut = &aMem[pOp->p1];
1020 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
1021 break;
1022}
drhf0863fe2005-06-12 21:35:51 +00001023
drha5750cf2014-02-07 13:20:31 +00001024/* Opcode: Blob P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001025** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001026**
drh9de221d2008-01-05 06:51:30 +00001027** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001028** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001029*/
drh4c583122008-01-04 22:01:03 +00001030case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001031 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001032 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001033 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001034 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001035 break;
1036}
1037
drheaf52d82010-05-12 13:50:23 +00001038/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001039** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001040**
drheaf52d82010-05-12 13:50:23 +00001041** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001042**
drh0fd61352014-02-07 02:29:45 +00001043** If the parameter is named, then its name appears in P4.
drh08de1492009-02-20 03:55:05 +00001044** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001045*/
drheaf52d82010-05-12 13:50:23 +00001046case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001047 Mem *pVar; /* Value being transferred */
1048
drheaf52d82010-05-12 13:50:23 +00001049 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001050 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001051 pVar = &p->aVar[pOp->p1 - 1];
1052 if( sqlite3VdbeMemTooBig(pVar) ){
1053 goto too_big;
drh023ae032007-05-08 12:12:16 +00001054 }
drheaf52d82010-05-12 13:50:23 +00001055 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1056 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001057 break;
1058}
danielk1977295ba552004-05-19 10:34:51 +00001059
drhb21e7c72008-06-22 12:37:57 +00001060/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001061** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001062**
drhe8e4af72012-09-21 00:04:28 +00001063** Move the values in register P1..P1+P3 over into
1064** registers P2..P2+P3. Registers P1..P1+P3 are
drhb21e7c72008-06-22 12:37:57 +00001065** left holding a NULL. It is an error for register ranges
drhe8e4af72012-09-21 00:04:28 +00001066** P1..P1+P3 and P2..P2+P3 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001067*/
drhe1349cb2008-04-01 00:36:10 +00001068case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001069 char *zMalloc; /* Holding variable for allocated memory */
1070 int n; /* Number of registers left to copy */
1071 int p1; /* Register to copy from */
1072 int p2; /* Register to copy to */
1073
drhe09f43f2013-11-21 04:18:31 +00001074 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001075 p1 = pOp->p1;
1076 p2 = pOp->p2;
drhe09f43f2013-11-21 04:18:31 +00001077 assert( n>=0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001078 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001079
drha6c2ed92009-11-14 23:22:23 +00001080 pIn1 = &aMem[p1];
1081 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001082 do{
dan3bc9f742013-08-15 16:18:39 +00001083 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1084 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001085 assert( memIsValid(pIn1) );
1086 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001087 zMalloc = pOut->zMalloc;
1088 pOut->zMalloc = 0;
1089 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001090#ifdef SQLITE_DEBUG
1091 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1092 pOut->pScopyFrom += p1 - pOp->p2;
1093 }
1094#endif
drhb21e7c72008-06-22 12:37:57 +00001095 pIn1->zMalloc = zMalloc;
1096 REGISTER_TRACE(p2++, pOut);
1097 pIn1++;
1098 pOut++;
drhe09f43f2013-11-21 04:18:31 +00001099 }while( n-- );
drhe1349cb2008-04-01 00:36:10 +00001100 break;
1101}
1102
drhe8e4af72012-09-21 00:04:28 +00001103/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001104** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001105**
drhe8e4af72012-09-21 00:04:28 +00001106** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001107**
1108** This instruction makes a deep copy of the value. A duplicate
1109** is made of any string or blob constant. See also OP_SCopy.
1110*/
drhe8e4af72012-09-21 00:04:28 +00001111case OP_Copy: {
1112 int n;
1113
1114 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001115 pIn1 = &aMem[pOp->p1];
1116 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001117 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001118 while( 1 ){
1119 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1120 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001121#ifdef SQLITE_DEBUG
1122 pOut->pScopyFrom = 0;
1123#endif
drhe8e4af72012-09-21 00:04:28 +00001124 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1125 if( (n--)==0 ) break;
1126 pOut++;
1127 pIn1++;
1128 }
drhe1349cb2008-04-01 00:36:10 +00001129 break;
1130}
1131
drhb1fdb2a2008-01-05 04:06:03 +00001132/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001133** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001134**
drh9cbf3422008-01-17 16:22:13 +00001135** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001136**
1137** This instruction makes a shallow copy of the value. If the value
1138** is a string or blob, then the copy is only a pointer to the
1139** original and hence if the original changes so will the copy.
1140** Worse, if the original is deallocated, the copy becomes invalid.
1141** Thus the program must guarantee that the original will not change
1142** during the lifetime of the copy. Use OP_Copy to make a complete
1143** copy.
1144*/
drh26198bb2013-10-31 11:15:09 +00001145case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001146 pIn1 = &aMem[pOp->p1];
1147 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001148 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001149 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001150#ifdef SQLITE_DEBUG
1151 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1152#endif
drh5e00f6c2001-09-13 13:46:56 +00001153 break;
1154}
drh75897232000-05-29 14:26:00 +00001155
drh9cbf3422008-01-17 16:22:13 +00001156/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001157** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001158**
shane21e7feb2008-05-30 15:59:49 +00001159** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001160** results. This opcode causes the sqlite3_step() call to terminate
1161** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
drh0fd61352014-02-07 02:29:45 +00001162** structure to provide access to the r[P1]..r[P1+P2-1] values as
1163** the result row.
drhd4e70eb2008-01-02 00:34:36 +00001164*/
drh9cbf3422008-01-17 16:22:13 +00001165case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001166 Mem *pMem;
1167 int i;
1168 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001169 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001170 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001171
drhe6400b92013-11-13 23:48:46 +00001172#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1173 /* Run the progress counter just before returning.
1174 */
1175 if( db->xProgress!=0
1176 && nVmStep>=nProgressLimit
1177 && db->xProgress(db->pProgressArg)!=0
1178 ){
1179 rc = SQLITE_INTERRUPT;
1180 goto vdbe_error_halt;
1181 }
1182#endif
1183
dan32b09f22009-09-23 17:29:59 +00001184 /* If this statement has violated immediate foreign key constraints, do
1185 ** not return the number of rows modified. And do not RELEASE the statement
1186 ** transaction. It needs to be rolled back. */
1187 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1188 assert( db->flags&SQLITE_CountRows );
1189 assert( p->usesStmtJournal );
1190 break;
1191 }
1192
danielk1977bd434552009-03-18 10:33:00 +00001193 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1194 ** DML statements invoke this opcode to return the number of rows
1195 ** modified to the user. This is the only way that a VM that
1196 ** opens a statement transaction may invoke this opcode.
1197 **
1198 ** In case this is such a statement, close any statement transaction
1199 ** opened by this VM before returning control to the user. This is to
1200 ** ensure that statement-transactions are always nested, not overlapping.
1201 ** If the open statement-transaction is not closed here, then the user
1202 ** may step another VM that opens its own statement transaction. This
1203 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001204 **
1205 ** The statement transaction is never a top-level transaction. Hence
1206 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001207 */
1208 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001209 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1210 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001211 break;
1212 }
1213
drhd4e70eb2008-01-02 00:34:36 +00001214 /* Invalidate all ephemeral cursor row caches */
1215 p->cacheCtr = (p->cacheCtr + 2)|1;
1216
1217 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001218 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001219 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001220 */
drha6c2ed92009-11-14 23:22:23 +00001221 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001222 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001223 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001224 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001225 assert( (pMem[i].flags & MEM_Ephem)==0
1226 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001227 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001228 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001229 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001230 }
drh28039692008-03-17 16:54:01 +00001231 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001232
1233 /* Return SQLITE_ROW
1234 */
drhd4e70eb2008-01-02 00:34:36 +00001235 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001236 rc = SQLITE_ROW;
1237 goto vdbe_return;
1238}
1239
drh5b6afba2008-01-05 16:29:28 +00001240/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001241** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001242**
drh5b6afba2008-01-05 16:29:28 +00001243** Add the text in register P1 onto the end of the text in
1244** register P2 and store the result in register P3.
1245** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001246**
1247** P3 = P2 || P1
1248**
1249** It is illegal for P1 and P3 to be the same register. Sometimes,
1250** if P3 is the same register as P2, the implementation is able
1251** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001252*/
drh5b6afba2008-01-05 16:29:28 +00001253case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001254 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001255
drh3c657212009-11-17 23:59:58 +00001256 pIn1 = &aMem[pOp->p1];
1257 pIn2 = &aMem[pOp->p2];
1258 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001259 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001260 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001261 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001262 break;
drh5e00f6c2001-09-13 13:46:56 +00001263 }
drha0c06522009-06-17 22:50:41 +00001264 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001265 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001266 Stringify(pIn2, encoding);
1267 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001268 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001269 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001270 }
danielk1977a7a8e142008-02-13 18:25:27 +00001271 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001272 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001273 goto no_mem;
1274 }
danielk1977a7a8e142008-02-13 18:25:27 +00001275 if( pOut!=pIn2 ){
1276 memcpy(pOut->z, pIn2->z, pIn2->n);
1277 }
1278 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001279 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001280 pOut->z[nByte+1] = 0;
1281 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001282 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001283 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001284 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001285 break;
1286}
drh75897232000-05-29 14:26:00 +00001287
drh3c84ddf2008-01-09 02:15:38 +00001288/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001289** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001290**
drh60a713c2008-01-21 16:22:45 +00001291** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001292** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001293** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001294*/
drh3c84ddf2008-01-09 02:15:38 +00001295/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001296** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001297**
drh3c84ddf2008-01-09 02:15:38 +00001298**
shane21e7feb2008-05-30 15:59:49 +00001299** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001300** and store the result in register P3.
1301** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001302*/
drh3c84ddf2008-01-09 02:15:38 +00001303/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001304** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001305**
drh60a713c2008-01-21 16:22:45 +00001306** Subtract the value in register P1 from the value in register P2
1307** and store the result in register P3.
1308** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001309*/
drh9cbf3422008-01-17 16:22:13 +00001310/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001311** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001312**
drh60a713c2008-01-21 16:22:45 +00001313** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001314** and store the result in register P3 (P3=P2/P1). If the value in
1315** register P1 is zero, then the result is NULL. If either input is
1316** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001317*/
drh9cbf3422008-01-17 16:22:13 +00001318/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001319** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001320**
drh40864a12013-11-15 18:58:37 +00001321** Compute the remainder after integer register P2 is divided by
1322** register P1 and store the result in register P3.
1323** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001324** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001325*/
drh5b6afba2008-01-05 16:29:28 +00001326case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1327case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1328case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1329case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1330case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001331 char bIntint; /* Started out as two integer operands */
drh856c1032009-06-02 15:21:42 +00001332 int flags; /* Combined MEM_* flags from both inputs */
1333 i64 iA; /* Integer value of left operand */
1334 i64 iB; /* Integer value of right operand */
1335 double rA; /* Real value of left operand */
1336 double rB; /* Real value of right operand */
1337
drh3c657212009-11-17 23:59:58 +00001338 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001339 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001340 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001341 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001342 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001343 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001344 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1345 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001346 iA = pIn1->u.i;
1347 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001348 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001349 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001350 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1351 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1352 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001353 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001354 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001355 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001356 iB /= iA;
drh75897232000-05-29 14:26:00 +00001357 break;
1358 }
drhbf4133c2001-10-13 02:59:08 +00001359 default: {
drh856c1032009-06-02 15:21:42 +00001360 if( iA==0 ) goto arithmetic_result_is_null;
1361 if( iA==-1 ) iA = 1;
1362 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001363 break;
1364 }
drh75897232000-05-29 14:26:00 +00001365 }
drh856c1032009-06-02 15:21:42 +00001366 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001367 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001368 }else{
drhbe707b32012-12-10 22:19:14 +00001369 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001370fp_math:
drh856c1032009-06-02 15:21:42 +00001371 rA = sqlite3VdbeRealValue(pIn1);
1372 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001373 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001374 case OP_Add: rB += rA; break;
1375 case OP_Subtract: rB -= rA; break;
1376 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001377 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001378 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001379 if( rA==(double)0 ) goto arithmetic_result_is_null;
1380 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001381 break;
1382 }
drhbf4133c2001-10-13 02:59:08 +00001383 default: {
shane75ac1de2009-06-09 18:58:52 +00001384 iA = (i64)rA;
1385 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001386 if( iA==0 ) goto arithmetic_result_is_null;
1387 if( iA==-1 ) iA = 1;
1388 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001389 break;
1390 }
drh5e00f6c2001-09-13 13:46:56 +00001391 }
drhc5a7b512010-01-13 16:25:42 +00001392#ifdef SQLITE_OMIT_FLOATING_POINT
1393 pOut->u.i = rB;
1394 MemSetTypeFlag(pOut, MEM_Int);
1395#else
drh856c1032009-06-02 15:21:42 +00001396 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001397 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001398 }
drh856c1032009-06-02 15:21:42 +00001399 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001400 MemSetTypeFlag(pOut, MEM_Real);
drhbe707b32012-12-10 22:19:14 +00001401 if( (flags & MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001402 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001403 }
drhc5a7b512010-01-13 16:25:42 +00001404#endif
drh5e00f6c2001-09-13 13:46:56 +00001405 }
1406 break;
1407
drha05a7222008-01-19 03:35:58 +00001408arithmetic_result_is_null:
1409 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001410 break;
1411}
1412
drh7a957892012-02-02 17:35:43 +00001413/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001414**
drh66a51672008-01-03 00:01:23 +00001415** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001416** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1417** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001418** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001419**
drh7a957892012-02-02 17:35:43 +00001420** If P1 is not zero, then it is a register that a subsequent min() or
1421** max() aggregate will set to 1 if the current row is not the minimum or
1422** maximum. The P1 register is initialized to 0 by this instruction.
1423**
danielk1977dc1bdc42004-06-11 10:51:27 +00001424** The interface used by the implementation of the aforementioned functions
1425** to retrieve the collation sequence set by this opcode is not available
1426** publicly, only to user functions defined in func.c.
1427*/
drh9cbf3422008-01-17 16:22:13 +00001428case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001429 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001430 if( pOp->p1 ){
1431 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1432 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001433 break;
1434}
1435
drh98757152008-01-09 23:04:12 +00001436/* Opcode: Function P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001437** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001438**
drh66a51672008-01-03 00:01:23 +00001439** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001440** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001441** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001442** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001443**
drh13449892005-09-07 21:22:45 +00001444** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001445** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001446** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001447** whether meta data associated with a user function argument using the
1448** sqlite3_set_auxdata() API may be safely retained until the next
1449** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001450**
drh13449892005-09-07 21:22:45 +00001451** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001452*/
drh0bce8352002-02-28 00:41:10 +00001453case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001454 int i;
drh6810ce62004-01-31 19:22:56 +00001455 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001456 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001457 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001458 int n;
drh1350b032002-02-27 19:00:20 +00001459
drh856c1032009-06-02 15:21:42 +00001460 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001461 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001462 assert( apVal || n==0 );
dan3bc9f742013-08-15 16:18:39 +00001463 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drhebc16712010-09-28 00:25:58 +00001464 pOut = &aMem[pOp->p3];
1465 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001466
dan3bc9f742013-08-15 16:18:39 +00001467 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001468 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001469 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001470 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001471 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001472 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001473 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001474 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001475 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001476 }
danielk197751ad0ec2004-05-24 12:39:02 +00001477
dan0c547792013-07-18 17:12:08 +00001478 assert( pOp->p4type==P4_FUNCDEF );
1479 ctx.pFunc = pOp->p4.pFunc;
dan0c547792013-07-18 17:12:08 +00001480 ctx.iOp = pc;
1481 ctx.pVdbe = p;
danielk1977a7a8e142008-02-13 18:25:27 +00001482
1483 /* The output cell may already have a buffer allocated. Move
1484 ** the pointer to ctx.s so in case the user-function can use
1485 ** the already allocated buffer instead of allocating a new one.
1486 */
drh76694c32013-11-21 03:43:12 +00001487 memcpy(&ctx.s, pOut, sizeof(Mem));
1488 pOut->flags = MEM_Null;
1489 pOut->xDel = 0;
1490 pOut->zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001491 MemSetTypeFlag(&ctx.s, MEM_Null);
1492
drh9b47ee32013-08-20 03:13:51 +00001493 ctx.fErrorOrAux = 0;
drhd36e1042013-09-06 13:10:12 +00001494 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001495 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001496 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001497 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001498 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001499 }
drh99a66922011-05-13 18:51:42 +00001500 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001501 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001502 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001503
dan5f84e142011-06-14 14:18:45 +00001504 if( db->mallocFailed ){
1505 /* Even though a malloc() has failed, the implementation of the
1506 ** user function may have called an sqlite3_result_XXX() function
1507 ** to return a value. The following call releases any resources
1508 ** associated with such a value.
1509 */
1510 sqlite3VdbeMemRelease(&ctx.s);
1511 goto no_mem;
1512 }
1513
drh90669c12006-01-20 15:45:36 +00001514 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001515 if( ctx.fErrorOrAux ){
1516 if( ctx.isError ){
1517 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1518 rc = ctx.isError;
1519 }
1520 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001521 }
1522
drh9cbf3422008-01-17 16:22:13 +00001523 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001524 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drhe09f43f2013-11-21 04:18:31 +00001525 assert( pOut->flags==MEM_Null );
1526 memcpy(pOut, &ctx.s, sizeof(Mem));
drh98757152008-01-09 23:04:12 +00001527 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001528 goto too_big;
1529 }
drh7b94e7f2011-04-04 12:29:20 +00001530
1531#if 0
1532 /* The app-defined function has done something that as caused this
1533 ** statement to expire. (Perhaps the function called sqlite3_exec()
1534 ** with a CREATE TABLE statement.)
1535 */
1536 if( p->expired ) rc = SQLITE_ABORT;
1537#endif
1538
drh2dcef112008-01-12 19:03:48 +00001539 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001540 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001541 break;
1542}
1543
drh98757152008-01-09 23:04:12 +00001544/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001545** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001546**
drh98757152008-01-09 23:04:12 +00001547** Take the bit-wise AND of the values in register P1 and P2 and
1548** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001549** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001550*/
drh98757152008-01-09 23:04:12 +00001551/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001552** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001553**
drh98757152008-01-09 23:04:12 +00001554** Take the bit-wise OR of the values in register P1 and P2 and
1555** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001556** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001557*/
drh98757152008-01-09 23:04:12 +00001558/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001559** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001560**
drh98757152008-01-09 23:04:12 +00001561** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001562** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001563** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001564** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001565*/
drh98757152008-01-09 23:04:12 +00001566/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001567** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001568**
drh98757152008-01-09 23:04:12 +00001569** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001570** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001571** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001572** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001573*/
drh5b6afba2008-01-05 16:29:28 +00001574case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1575case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1576case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1577case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001578 i64 iA;
1579 u64 uA;
1580 i64 iB;
1581 u8 op;
drh6810ce62004-01-31 19:22:56 +00001582
drh3c657212009-11-17 23:59:58 +00001583 pIn1 = &aMem[pOp->p1];
1584 pIn2 = &aMem[pOp->p2];
1585 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001586 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001587 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001588 break;
1589 }
drh158b9cb2011-03-05 20:59:46 +00001590 iA = sqlite3VdbeIntValue(pIn2);
1591 iB = sqlite3VdbeIntValue(pIn1);
1592 op = pOp->opcode;
1593 if( op==OP_BitAnd ){
1594 iA &= iB;
1595 }else if( op==OP_BitOr ){
1596 iA |= iB;
1597 }else if( iB!=0 ){
1598 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1599
1600 /* If shifting by a negative amount, shift in the other direction */
1601 if( iB<0 ){
1602 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1603 op = 2*OP_ShiftLeft + 1 - op;
1604 iB = iB>(-64) ? -iB : 64;
1605 }
1606
1607 if( iB>=64 ){
1608 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1609 }else{
1610 memcpy(&uA, &iA, sizeof(uA));
1611 if( op==OP_ShiftLeft ){
1612 uA <<= iB;
1613 }else{
1614 uA >>= iB;
1615 /* Sign-extend on a right shift of a negative number */
1616 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1617 }
1618 memcpy(&iA, &uA, sizeof(iA));
1619 }
drhbf4133c2001-10-13 02:59:08 +00001620 }
drh158b9cb2011-03-05 20:59:46 +00001621 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001622 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001623 break;
1624}
1625
drh8558cde2008-01-05 05:20:10 +00001626/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001627** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001628**
danielk19770cdc0222008-06-26 18:04:03 +00001629** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001630** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001631**
drh8558cde2008-01-05 05:20:10 +00001632** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001633*/
drh9cbf3422008-01-17 16:22:13 +00001634case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001635 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001636 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001637 sqlite3VdbeMemIntegerify(pIn1);
1638 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001639 break;
1640}
1641
drh9cbf3422008-01-17 16:22:13 +00001642/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001643**
drh9cbf3422008-01-17 16:22:13 +00001644** Force the value in register P1 to be an integer. If the value
1645** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001646** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001647** raise an SQLITE_MISMATCH exception.
1648*/
drh9cbf3422008-01-17 16:22:13 +00001649case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001650 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001651 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001652 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
drh688852a2014-02-17 22:40:43 +00001653 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
drh83b301b2013-11-20 00:59:02 +00001654 if( (pIn1->flags & MEM_Int)==0 ){
1655 if( pOp->p2==0 ){
1656 rc = SQLITE_MISMATCH;
1657 goto abort_due_to_error;
1658 }else{
1659 pc = pOp->p2 - 1;
1660 break;
1661 }
drh8aff1012001-12-22 14:49:24 +00001662 }
drh8aff1012001-12-22 14:49:24 +00001663 }
drh83b301b2013-11-20 00:59:02 +00001664 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001665 break;
1666}
1667
drh13573c72010-01-12 17:04:07 +00001668#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001669/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001670**
drh2133d822008-01-03 18:44:59 +00001671** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001672**
drh8a512562005-11-14 22:29:05 +00001673** This opcode is used when extracting information from a column that
1674** has REAL affinity. Such column values may still be stored as
1675** integers, for space efficiency, but after extraction we want them
1676** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001677*/
drh9cbf3422008-01-17 16:22:13 +00001678case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001679 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001680 if( pIn1->flags & MEM_Int ){
1681 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001682 }
drh487e2622005-06-25 18:42:14 +00001683 break;
1684}
drh13573c72010-01-12 17:04:07 +00001685#endif
drh487e2622005-06-25 18:42:14 +00001686
drh8df447f2005-11-01 15:48:24 +00001687#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001688/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001689**
drh8558cde2008-01-05 05:20:10 +00001690** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001691** If the value is numeric, convert it to a string using the
drh0fd61352014-02-07 02:29:45 +00001692** equivalent of sprintf(). Blob values are unchanged and
drh487e2622005-06-25 18:42:14 +00001693** are afterwards simply interpreted as text.
1694**
1695** A NULL value is not changed by this routine. It remains NULL.
1696*/
drh9cbf3422008-01-17 16:22:13 +00001697case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001698 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001699 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001700 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001701 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001702 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1703 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1704 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001705 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001706 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001707 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001708 break;
1709}
1710
drh8558cde2008-01-05 05:20:10 +00001711/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001712**
drh8558cde2008-01-05 05:20:10 +00001713** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001714** If the value is numeric, convert it to a string first.
1715** Strings are simply reinterpreted as blobs with no change
1716** to the underlying data.
1717**
1718** A NULL value is not changed by this routine. It remains NULL.
1719*/
drh9cbf3422008-01-17 16:22:13 +00001720case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001721 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001722 if( pIn1->flags & MEM_Null ) break;
1723 if( (pIn1->flags & MEM_Blob)==0 ){
1724 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001725 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001726 MemSetTypeFlag(pIn1, MEM_Blob);
1727 }else{
1728 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001729 }
drhb7654112008-01-12 12:48:07 +00001730 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001731 break;
1732}
drh8a512562005-11-14 22:29:05 +00001733
drh8558cde2008-01-05 05:20:10 +00001734/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001735**
drh8558cde2008-01-05 05:20:10 +00001736** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001737** integer or a floating-point number.)
1738** If the value is text or blob, try to convert it to an using the
1739** equivalent of atoi() or atof() and store 0 if no such conversion
1740** is possible.
1741**
1742** A NULL value is not changed by this routine. It remains NULL.
1743*/
drh9cbf3422008-01-17 16:22:13 +00001744case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001745 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001746 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001747 break;
1748}
1749#endif /* SQLITE_OMIT_CAST */
1750
drh8558cde2008-01-05 05:20:10 +00001751/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001752**
drh710c4842010-08-30 01:17:20 +00001753** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001754** The value is currently a real number, drop its fractional part.
1755** If the value is text or blob, try to convert it to an integer using the
1756** equivalent of atoi() and store 0 if no such conversion is possible.
1757**
1758** A NULL value is not changed by this routine. It remains NULL.
1759*/
drh9cbf3422008-01-17 16:22:13 +00001760case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001761 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001762 if( (pIn1->flags & MEM_Null)==0 ){
1763 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001764 }
1765 break;
1766}
1767
drh13573c72010-01-12 17:04:07 +00001768#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001769/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001770**
drh8558cde2008-01-05 05:20:10 +00001771** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001772** If The value is currently an integer, convert it.
1773** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001774** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001775**
1776** A NULL value is not changed by this routine. It remains NULL.
1777*/
drh9cbf3422008-01-17 16:22:13 +00001778case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001779 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001780 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001781 if( (pIn1->flags & MEM_Null)==0 ){
1782 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001783 }
1784 break;
1785}
drh13573c72010-01-12 17:04:07 +00001786#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001787
drh35573352008-01-08 23:54:25 +00001788/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001789** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001790**
drh35573352008-01-08 23:54:25 +00001791** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1792** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001793**
drh35573352008-01-08 23:54:25 +00001794** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1795** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001796** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001797**
drh35573352008-01-08 23:54:25 +00001798** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001799** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001800** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001801** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001802** affinity is used. Note that the affinity conversions are stored
1803** back into the input registers P1 and P3. So this opcode can cause
1804** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001805**
1806** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001807** the values are compared. If both values are blobs then memcmp() is
1808** used to determine the results of the comparison. If both values
1809** are text, then the appropriate collating function specified in
1810** P4 is used to do the comparison. If P4 is not specified then
1811** memcmp() is used to compare text string. If both values are
1812** numeric, then a numeric comparison is used. If the two values
1813** are of different types, then numbers are considered less than
1814** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001815**
drh35573352008-01-08 23:54:25 +00001816** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1817** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001818**
1819** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1820** equal to one another, provided that they do not have their MEM_Cleared
1821** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001822*/
drh9cbf3422008-01-17 16:22:13 +00001823/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001824** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001825**
drh35573352008-01-08 23:54:25 +00001826** This works just like the Lt opcode except that the jump is taken if
1827** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001828** additional information.
drh6a2fe092009-09-23 02:29:36 +00001829**
1830** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1831** true or false and is never NULL. If both operands are NULL then the result
1832** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001833** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001834** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001835*/
drh9cbf3422008-01-17 16:22:13 +00001836/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001837** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001838**
drh35573352008-01-08 23:54:25 +00001839** This works just like the Lt opcode except that the jump is taken if
1840** the operands in registers P1 and P3 are equal.
1841** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001842**
1843** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1844** true or false and is never NULL. If both operands are NULL then the result
1845** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001846** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001847** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001848*/
drh9cbf3422008-01-17 16:22:13 +00001849/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001850** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001851**
drh35573352008-01-08 23:54:25 +00001852** This works just like the Lt opcode except that the jump is taken if
1853** the content of register P3 is less than or equal to the content of
1854** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001855*/
drh9cbf3422008-01-17 16:22:13 +00001856/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001857** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001858**
drh35573352008-01-08 23:54:25 +00001859** This works just like the Lt opcode except that the jump is taken if
1860** the content of register P3 is greater than the content of
1861** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001862*/
drh9cbf3422008-01-17 16:22:13 +00001863/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001864** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001865**
drh35573352008-01-08 23:54:25 +00001866** This works just like the Lt opcode except that the jump is taken if
1867** the content of register P3 is greater than or equal to the content of
1868** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001869*/
drh9cbf3422008-01-17 16:22:13 +00001870case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1871case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1872case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1873case OP_Le: /* same as TK_LE, jump, in1, in3 */
1874case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1875case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001876 int res; /* Result of the comparison of pIn1 against pIn3 */
1877 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001878 u16 flags1; /* Copy of initial value of pIn1->flags */
1879 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001880
drh3c657212009-11-17 23:59:58 +00001881 pIn1 = &aMem[pOp->p1];
1882 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001883 flags1 = pIn1->flags;
1884 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001885 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001886 /* One or both operands are NULL */
1887 if( pOp->p5 & SQLITE_NULLEQ ){
1888 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1889 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1890 ** or not both operands are null.
1891 */
1892 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001893 assert( (flags1 & MEM_Cleared)==0 );
1894 if( (flags1&MEM_Null)!=0
1895 && (flags3&MEM_Null)!=0
1896 && (flags3&MEM_Cleared)==0
1897 ){
1898 res = 0; /* Results are equal */
1899 }else{
1900 res = 1; /* Results are not equal */
1901 }
drh6a2fe092009-09-23 02:29:36 +00001902 }else{
1903 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1904 ** then the result is always NULL.
1905 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1906 */
drh688852a2014-02-17 22:40:43 +00001907 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001908 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001909 MemSetTypeFlag(pOut, MEM_Null);
1910 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001911 }else{
1912 VdbeBranchTaken((pOp->p5 & SQLITE_JUMPIFNULL)?2:3,4);
1913 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1914 pc = pOp->p2-1;
1915 }
drh6a2fe092009-09-23 02:29:36 +00001916 }
1917 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001918 }
drh6a2fe092009-09-23 02:29:36 +00001919 }else{
1920 /* Neither operand is NULL. Do a comparison. */
1921 affinity = pOp->p5 & SQLITE_AFF_MASK;
1922 if( affinity ){
1923 applyAffinity(pIn1, affinity, encoding);
1924 applyAffinity(pIn3, affinity, encoding);
1925 if( db->mallocFailed ) goto no_mem;
1926 }
danielk1977a37cdde2004-05-16 11:15:36 +00001927
drh6a2fe092009-09-23 02:29:36 +00001928 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1929 ExpandBlob(pIn1);
1930 ExpandBlob(pIn3);
1931 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001932 }
danielk1977a37cdde2004-05-16 11:15:36 +00001933 switch( pOp->opcode ){
1934 case OP_Eq: res = res==0; break;
1935 case OP_Ne: res = res!=0; break;
1936 case OP_Lt: res = res<0; break;
1937 case OP_Le: res = res<=0; break;
1938 case OP_Gt: res = res>0; break;
1939 default: res = res>=0; break;
1940 }
1941
drh35573352008-01-08 23:54:25 +00001942 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001943 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001944 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001945 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001946 pOut->u.i = res;
1947 REGISTER_TRACE(pOp->p2, pOut);
drh688852a2014-02-17 22:40:43 +00001948 }else{
1949 VdbeBranchTaken(res!=0, 4);
1950 if( res ){
1951 pc = pOp->p2-1;
1952 }
danielk1977a37cdde2004-05-16 11:15:36 +00001953 }
danb7dca7d2010-03-05 16:32:12 +00001954 /* Undo any changes made by applyAffinity() to the input registers. */
1955 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1956 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001957 break;
1958}
drhc9b84a12002-06-20 11:36:48 +00001959
drh0acb7e42008-06-25 00:12:41 +00001960/* Opcode: Permutation * * * P4 *
1961**
shanebe217792009-03-05 04:20:31 +00001962** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001963** of integers in P4.
1964**
drh953f7612012-12-07 22:18:54 +00001965** The permutation is only valid until the next OP_Compare that has
1966** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1967** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001968*/
1969case OP_Permutation: {
1970 assert( pOp->p4type==P4_INTARRAY );
1971 assert( pOp->p4.ai );
1972 aPermute = pOp->p4.ai;
1973 break;
1974}
1975
drh953f7612012-12-07 22:18:54 +00001976/* Opcode: Compare P1 P2 P3 P4 P5
drh16ee60f2008-06-20 18:13:25 +00001977**
drh710c4842010-08-30 01:17:20 +00001978** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1979** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001980** the comparison for use by the next OP_Jump instruct.
1981**
drh0ca10df2012-12-08 13:26:23 +00001982** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1983** determined by the most recent OP_Permutation operator. If the
1984** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1985** order.
1986**
drh0acb7e42008-06-25 00:12:41 +00001987** P4 is a KeyInfo structure that defines collating sequences and sort
1988** orders for the comparison. The permutation applies to registers
1989** only. The KeyInfo elements are used sequentially.
1990**
1991** The comparison is a sort comparison, so NULLs compare equal,
1992** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001993** and strings are less than blobs.
1994*/
1995case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001996 int n;
1997 int i;
1998 int p1;
1999 int p2;
2000 const KeyInfo *pKeyInfo;
2001 int idx;
2002 CollSeq *pColl; /* Collating sequence to use on this term */
2003 int bRev; /* True for DESCENDING sort order */
2004
drh953f7612012-12-07 22:18:54 +00002005 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002006 n = pOp->p3;
2007 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002008 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002009 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002010 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002011 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002012#if SQLITE_DEBUG
2013 if( aPermute ){
2014 int k, mx = 0;
2015 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002016 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2017 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002018 }else{
dan3bc9f742013-08-15 16:18:39 +00002019 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2020 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002021 }
2022#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002023 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002024 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002025 assert( memIsValid(&aMem[p1+idx]) );
2026 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002027 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2028 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002029 assert( i<pKeyInfo->nField );
2030 pColl = pKeyInfo->aColl[i];
2031 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002032 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002033 if( iCompare ){
2034 if( bRev ) iCompare = -iCompare;
2035 break;
2036 }
drh16ee60f2008-06-20 18:13:25 +00002037 }
drh0acb7e42008-06-25 00:12:41 +00002038 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002039 break;
2040}
2041
2042/* Opcode: Jump P1 P2 P3 * *
2043**
2044** Jump to the instruction at address P1, P2, or P3 depending on whether
2045** in the most recent OP_Compare instruction the P1 vector was less than
2046** equal to, or greater than the P2 vector, respectively.
2047*/
drh0acb7e42008-06-25 00:12:41 +00002048case OP_Jump: { /* jump */
2049 if( iCompare<0 ){
drh688852a2014-02-17 22:40:43 +00002050 pc = pOp->p1 - 1; VdbeBranchTaken(0,3);
drh0acb7e42008-06-25 00:12:41 +00002051 }else if( iCompare==0 ){
drh688852a2014-02-17 22:40:43 +00002052 pc = pOp->p2 - 1; VdbeBranchTaken(1,3);
drh16ee60f2008-06-20 18:13:25 +00002053 }else{
drh688852a2014-02-17 22:40:43 +00002054 pc = pOp->p3 - 1; VdbeBranchTaken(2,3);
drh16ee60f2008-06-20 18:13:25 +00002055 }
2056 break;
2057}
2058
drh5b6afba2008-01-05 16:29:28 +00002059/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002060** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002061**
drh5b6afba2008-01-05 16:29:28 +00002062** Take the logical AND of the values in registers P1 and P2 and
2063** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002064**
drh5b6afba2008-01-05 16:29:28 +00002065** If either P1 or P2 is 0 (false) then the result is 0 even if
2066** the other input is NULL. A NULL and true or two NULLs give
2067** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002068*/
drh5b6afba2008-01-05 16:29:28 +00002069/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002070** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002071**
2072** Take the logical OR of the values in register P1 and P2 and
2073** store the answer in register P3.
2074**
2075** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2076** even if the other input is NULL. A NULL and false or two NULLs
2077** give a NULL output.
2078*/
2079case OP_And: /* same as TK_AND, in1, in2, out3 */
2080case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002081 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2082 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002083
drh3c657212009-11-17 23:59:58 +00002084 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002085 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002086 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002087 }else{
drh5b6afba2008-01-05 16:29:28 +00002088 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002089 }
drh3c657212009-11-17 23:59:58 +00002090 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002091 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002092 v2 = 2;
2093 }else{
drh5b6afba2008-01-05 16:29:28 +00002094 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002095 }
2096 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002097 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002098 v1 = and_logic[v1*3+v2];
2099 }else{
drh5b6afba2008-01-05 16:29:28 +00002100 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002101 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002102 }
drh3c657212009-11-17 23:59:58 +00002103 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002104 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002105 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002106 }else{
drh5b6afba2008-01-05 16:29:28 +00002107 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002108 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002109 }
drh5e00f6c2001-09-13 13:46:56 +00002110 break;
2111}
2112
drhe99fa2a2008-12-15 15:27:51 +00002113/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002114** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002115**
drhe99fa2a2008-12-15 15:27:51 +00002116** Interpret the value in register P1 as a boolean value. Store the
2117** boolean complement in register P2. If the value in register P1 is
2118** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002119*/
drh93952eb2009-11-13 19:43:43 +00002120case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002121 pIn1 = &aMem[pOp->p1];
2122 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002123 if( pIn1->flags & MEM_Null ){
2124 sqlite3VdbeMemSetNull(pOut);
2125 }else{
2126 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2127 }
drh5e00f6c2001-09-13 13:46:56 +00002128 break;
2129}
2130
drhe99fa2a2008-12-15 15:27:51 +00002131/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002132** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002133**
drhe99fa2a2008-12-15 15:27:51 +00002134** Interpret the content of register P1 as an integer. Store the
2135** ones-complement of the P1 value into register P2. If P1 holds
2136** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002137*/
drh93952eb2009-11-13 19:43:43 +00002138case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002139 pIn1 = &aMem[pOp->p1];
2140 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002141 if( pIn1->flags & MEM_Null ){
2142 sqlite3VdbeMemSetNull(pOut);
2143 }else{
2144 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2145 }
drhbf4133c2001-10-13 02:59:08 +00002146 break;
2147}
2148
drh48f2d3b2011-09-16 01:34:43 +00002149/* Opcode: Once P1 P2 * * *
2150**
dan1d8cb212011-12-09 13:24:16 +00002151** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
drh0fd61352014-02-07 02:29:45 +00002152** set the flag and fall through to the next instruction. In other words,
2153** this opcode causes all following up codes up through P2 (but not including
2154** P2) to run just once and skipped on subsequent times through the loop.
drh48f2d3b2011-09-16 01:34:43 +00002155*/
dan1d8cb212011-12-09 13:24:16 +00002156case OP_Once: { /* jump */
2157 assert( pOp->p1<p->nOnceFlag );
drh688852a2014-02-17 22:40:43 +00002158 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
dan1d8cb212011-12-09 13:24:16 +00002159 if( p->aOnceFlag[pOp->p1] ){
2160 pc = pOp->p2-1;
2161 }else{
2162 p->aOnceFlag[pOp->p1] = 1;
2163 }
2164 break;
2165}
2166
drh3c84ddf2008-01-09 02:15:38 +00002167/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002168**
drhef8662b2011-06-20 21:47:58 +00002169** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002170** is considered true if it is numeric and non-zero. If the value
drhb8475df2011-12-09 16:21:19 +00002171** in P1 is NULL then take the jump if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002172*/
drh3c84ddf2008-01-09 02:15:38 +00002173/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002174**
drhef8662b2011-06-20 21:47:58 +00002175** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002176** is considered false if it has a numeric value of zero. If the value
2177** in P1 is NULL then take the jump if P3 is zero.
drhf5905aa2002-05-26 20:54:33 +00002178*/
drh9cbf3422008-01-17 16:22:13 +00002179case OP_If: /* jump, in1 */
2180case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002181 int c;
drh3c657212009-11-17 23:59:58 +00002182 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002183 if( pIn1->flags & MEM_Null ){
2184 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002185 }else{
drhba0232a2005-06-06 17:27:19 +00002186#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002187 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002188#else
drh3c84ddf2008-01-09 02:15:38 +00002189 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002190#endif
drhf5905aa2002-05-26 20:54:33 +00002191 if( pOp->opcode==OP_IfNot ) c = !c;
2192 }
drh688852a2014-02-17 22:40:43 +00002193 VdbeBranchTaken(c!=0, 2);
drh3c84ddf2008-01-09 02:15:38 +00002194 if( c ){
2195 pc = pOp->p2-1;
2196 }
drh5e00f6c2001-09-13 13:46:56 +00002197 break;
2198}
2199
drh830ecf92009-06-18 00:41:55 +00002200/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002201** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002202**
drh830ecf92009-06-18 00:41:55 +00002203** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002204*/
drh9cbf3422008-01-17 16:22:13 +00002205case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002206 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002207 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
drh830ecf92009-06-18 00:41:55 +00002208 if( (pIn1->flags & MEM_Null)!=0 ){
2209 pc = pOp->p2 - 1;
2210 }
drh477df4b2008-01-05 18:48:24 +00002211 break;
2212}
2213
drh98757152008-01-09 23:04:12 +00002214/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002215** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002216**
drh6a288a32008-01-07 19:20:24 +00002217** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002218*/
drh9cbf3422008-01-17 16:22:13 +00002219case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002220 pIn1 = &aMem[pOp->p1];
drh688852a2014-02-17 22:40:43 +00002221 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
drh6a288a32008-01-07 19:20:24 +00002222 if( (pIn1->flags & MEM_Null)==0 ){
2223 pc = pOp->p2 - 1;
2224 }
drh5e00f6c2001-09-13 13:46:56 +00002225 break;
2226}
2227
drh3e9ca092009-09-08 01:14:48 +00002228/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002229** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002230**
danielk1977cfcdaef2004-05-12 07:33:33 +00002231** Interpret the data that cursor P1 points to as a structure built using
2232** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002233** information about the format of the data.) Extract the P2-th column
2234** from this record. If there are less that (P2+1)
2235** values in the record, extract a NULL.
2236**
drh9cbf3422008-01-17 16:22:13 +00002237** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002238**
danielk19771f4aa332008-01-03 09:51:55 +00002239** If the column contains fewer than P2 fields, then extract a NULL. Or,
2240** if the P4 argument is a P4_MEM use the value of the P4 argument as
2241** the result.
drh3e9ca092009-09-08 01:14:48 +00002242**
2243** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2244** then the cache of the cursor is reset prior to extracting the column.
2245** The first OP_Column against a pseudo-table after the value of the content
2246** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002247**
drhdda5c082012-03-28 13:41:10 +00002248** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2249** the result is guaranteed to only be used as the argument of a length()
2250** or typeof() function, respectively. The loading of large blobs can be
2251** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002252*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002253case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002254 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002255 int p2; /* column number to retrieve */
2256 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002257 BtCursor *pCrsr; /* The BTree cursor */
2258 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2259 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002260 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002261 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002262 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002263 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002264 const u8 *zData; /* Part of the record being decoded */
2265 const u8 *zHdr; /* Next unparsed byte of the header */
2266 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002267 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002268 u32 szField; /* Number of bytes in the content of a field */
drh501932c2013-11-21 21:59:53 +00002269 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002270 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002271 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002272
drh399af1d2013-11-20 17:25:55 +00002273 p2 = pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00002274 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002275 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002276 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002277 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2278 pC = p->apCsr[pOp->p1];
drha5759672012-10-30 14:39:12 +00002279 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002280 assert( p2<pC->nField );
drh399af1d2013-11-20 17:25:55 +00002281 aType = pC->aType;
drh14da87f2013-11-20 21:51:33 +00002282 aOffset = aType + pC->nField;
danielk19770817d0d2007-02-14 09:19:36 +00002283#ifndef SQLITE_OMIT_VIRTUALTABLE
drh380d6852013-11-20 20:58:00 +00002284 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
danielk19770817d0d2007-02-14 09:19:36 +00002285#endif
shane36840fd2009-06-26 16:32:13 +00002286 pCrsr = pC->pCursor;
drh380d6852013-11-20 20:58:00 +00002287 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
2288 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */
drh399af1d2013-11-20 17:25:55 +00002289
2290 /* If the cursor cache is stale, bring it up-to-date */
2291 rc = sqlite3VdbeCursorMoveto(pC);
2292 if( rc ) goto abort_due_to_error;
2293 if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
drhc8606e42013-11-20 19:28:03 +00002294 if( pC->nullRow ){
2295 if( pCrsr==0 ){
2296 assert( pC->pseudoTableReg>0 );
2297 pReg = &aMem[pC->pseudoTableReg];
drhc8606e42013-11-20 19:28:03 +00002298 assert( pReg->flags & MEM_Blob );
2299 assert( memIsValid(pReg) );
2300 pC->payloadSize = pC->szRow = avail = pReg->n;
2301 pC->aRow = (u8*)pReg->z;
2302 }else{
2303 MemSetTypeFlag(pDest, MEM_Null);
drh399af1d2013-11-20 17:25:55 +00002304 goto op_column_out;
2305 }
danielk197784ac9d02004-05-18 09:58:06 +00002306 }else{
drhc8606e42013-11-20 19:28:03 +00002307 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002308 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002309 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2310 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2311 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2312 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2313 ** payload size, so it is impossible for payloadSize64 to be
2314 ** larger than 32 bits. */
2315 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2316 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2317 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002318 }else{
drh399af1d2013-11-20 17:25:55 +00002319 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2320 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2321 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2322 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002323 }
drh399af1d2013-11-20 17:25:55 +00002324 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2325 if( pC->payloadSize <= (u32)avail ){
2326 pC->szRow = pC->payloadSize;
drhe61cffc2004-06-12 18:12:15 +00002327 }else{
drh399af1d2013-11-20 17:25:55 +00002328 pC->szRow = avail;
2329 }
2330 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2331 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002332 }
drhd3194f52004-05-27 19:59:32 +00002333 }
drh399af1d2013-11-20 17:25:55 +00002334 pC->cacheStatus = p->cacheCtr;
2335 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2336 pC->nHdrParsed = 0;
2337 aOffset[0] = offset;
2338 if( avail<offset ){
drh380d6852013-11-20 20:58:00 +00002339 /* pC->aRow does not have to hold the entire row, but it does at least
2340 ** need to cover the header of the record. If pC->aRow does not contain
2341 ** the complete header, then set it to zero, forcing the header to be
2342 ** dynamically allocated. */
drh399af1d2013-11-20 17:25:55 +00002343 pC->aRow = 0;
2344 pC->szRow = 0;
2345 }
drh35cd6432009-06-05 14:17:21 +00002346
2347 /* Make sure a corrupt database has not given us an oversize header.
2348 ** Do this now to avoid an oversize memory allocation.
2349 **
2350 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2351 ** types use so much data space that there can only be 4096 and 32 of
2352 ** them, respectively. So the maximum header length results from a
2353 ** 3-byte type for each of the maximum of 32768 columns plus three
2354 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2355 */
drh399af1d2013-11-20 17:25:55 +00002356 if( offset > 98307 || offset > pC->payloadSize ){
drh35cd6432009-06-05 14:17:21 +00002357 rc = SQLITE_CORRUPT_BKPT;
drhc8606e42013-11-20 19:28:03 +00002358 goto op_column_error;
drh35cd6432009-06-05 14:17:21 +00002359 }
drh399af1d2013-11-20 17:25:55 +00002360 }
drh35cd6432009-06-05 14:17:21 +00002361
drh399af1d2013-11-20 17:25:55 +00002362 /* Make sure at least the first p2+1 entries of the header have been
2363 ** parsed and valid information is in aOffset[] and aType[].
2364 */
drhc8606e42013-11-20 19:28:03 +00002365 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002366 /* If there is more header available for parsing in the record, try
2367 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002368 */
drhc8606e42013-11-20 19:28:03 +00002369 if( pC->iHdrOffset<aOffset[0] ){
2370 /* Make sure zData points to enough of the record to cover the header. */
2371 if( pC->aRow==0 ){
2372 memset(&sMem, 0, sizeof(sMem));
drh14da87f2013-11-20 21:51:33 +00002373 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
2374 !pC->isTable, &sMem);
drhc8606e42013-11-20 19:28:03 +00002375 if( rc!=SQLITE_OK ){
2376 goto op_column_error;
2377 }
2378 zData = (u8*)sMem.z;
2379 }else{
2380 zData = pC->aRow;
2381 }
2382
2383 /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
2384 i = pC->nHdrParsed;
2385 offset = aOffset[i];
2386 zHdr = zData + pC->iHdrOffset;
2387 zEndHdr = zData + aOffset[0];
2388 assert( i<=p2 && zHdr<zEndHdr );
2389 do{
2390 if( zHdr[0]<0x80 ){
2391 t = zHdr[0];
2392 zHdr++;
2393 }else{
2394 zHdr += sqlite3GetVarint32(zHdr, &t);
2395 }
2396 aType[i] = t;
2397 szField = sqlite3VdbeSerialTypeLen(t);
2398 offset += szField;
2399 if( offset<szField ){ /* True if offset overflows */
2400 zHdr = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2401 break;
2402 }
2403 i++;
2404 aOffset[i] = offset;
2405 }while( i<=p2 && zHdr<zEndHdr );
2406 pC->nHdrParsed = i;
2407 pC->iHdrOffset = (u32)(zHdr - zData);
2408 if( pC->aRow==0 ){
2409 sqlite3VdbeMemRelease(&sMem);
2410 sMem.flags = MEM_Null;
2411 }
2412
2413 /* If we have read more header data than was contained in the header,
2414 ** or if the end of the last field appears to be past the end of the
2415 ** record, or if the end of the last field appears to be before the end
2416 ** of the record (when all fields present), then we must be dealing
2417 ** with a corrupt database.
2418 */
2419 if( (zHdr > zEndHdr)
2420 || (offset > pC->payloadSize)
2421 || (zHdr==zEndHdr && offset!=pC->payloadSize)
2422 ){
2423 rc = SQLITE_CORRUPT_BKPT;
2424 goto op_column_error;
2425 }
2426 }
2427
drh380d6852013-11-20 20:58:00 +00002428 /* If after trying to extra new entries from the header, nHdrParsed is
2429 ** still not up to p2, that means that the record has fewer than p2
2430 ** columns. So the result will be either the default value or a NULL.
2431 */
drhc8606e42013-11-20 19:28:03 +00002432 if( pC->nHdrParsed<=p2 ){
2433 if( pOp->p4type==P4_MEM ){
2434 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2435 }else{
2436 MemSetTypeFlag(pDest, MEM_Null);
2437 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002438 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002439 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002440 }
danielk1977192ac1d2004-05-10 07:17:30 +00002441
drh380d6852013-11-20 20:58:00 +00002442 /* Extract the content for the p2+1-th column. Control can only
2443 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
2444 ** all valid.
drh9188b382004-05-14 21:12:22 +00002445 */
drhc8606e42013-11-20 19:28:03 +00002446 assert( p2<pC->nHdrParsed );
2447 assert( rc==SQLITE_OK );
2448 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002449 /* This is the common case where the desired content fits on the original
2450 ** page - where the content is not on an overflow page */
drhc8606e42013-11-20 19:28:03 +00002451 VdbeMemRelease(pDest);
2452 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002453 }else{
drh58c96082013-12-23 11:33:32 +00002454 /* This branch happens only when content is on overflow pages */
drhc8606e42013-11-20 19:28:03 +00002455 t = aType[p2];
drh380d6852013-11-20 20:58:00 +00002456 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2457 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2458 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002459 ){
2460 /* Content is irrelevant for the typeof() function and for
2461 ** the length(X) function if X is a blob. So we might as well use
2462 ** bogus content rather than reading content from disk. NULL works
2463 ** for text and blob and whatever is in the payloadSize64 variable
drh380d6852013-11-20 20:58:00 +00002464 ** will work for everything else. Content is also irrelevant if
2465 ** the content length is 0. */
2466 zData = t<=13 ? (u8*)&payloadSize64 : 0;
drhc8606e42013-11-20 19:28:03 +00002467 sMem.zMalloc = 0;
danielk1977aee18ef2005-03-09 12:26:50 +00002468 }else{
drhc8606e42013-11-20 19:28:03 +00002469 memset(&sMem, 0, sizeof(sMem));
2470 sqlite3VdbeMemMove(&sMem, pDest);
drh14da87f2013-11-20 21:51:33 +00002471 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drhc8606e42013-11-20 19:28:03 +00002472 &sMem);
2473 if( rc!=SQLITE_OK ){
2474 goto op_column_error;
2475 }
2476 zData = (u8*)sMem.z;
2477 }
2478 sqlite3VdbeSerialGet(zData, t, pDest);
2479 /* If we dynamically allocated space to hold the data (in the
2480 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
2481 ** dynamically allocated space over to the pDest structure.
2482 ** This prevents a memory copy. */
2483 if( sMem.zMalloc ){
2484 assert( sMem.z==sMem.zMalloc );
2485 assert( !(pDest->flags & MEM_Dyn) );
2486 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2487 pDest->flags &= ~(MEM_Ephem|MEM_Static);
2488 pDest->flags |= MEM_Term;
2489 pDest->z = sMem.z;
2490 pDest->zMalloc = sMem.zMalloc;
danielk1977aee18ef2005-03-09 12:26:50 +00002491 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002492 }
drhc8606e42013-11-20 19:28:03 +00002493 pDest->enc = encoding;
drhd3194f52004-05-27 19:59:32 +00002494
danielk19773c9cc8d2005-01-17 03:40:08 +00002495op_column_out:
drha2a30282013-12-09 21:06:46 +00002496 Deephemeralize(pDest);
drhc8606e42013-11-20 19:28:03 +00002497op_column_error:
drhb7654112008-01-12 12:48:07 +00002498 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002499 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002500 break;
2501}
2502
danielk1977751de562008-04-18 09:01:15 +00002503/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002504** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002505**
2506** Apply affinities to a range of P2 registers starting with P1.
2507**
2508** P4 is a string that is P2 characters long. The nth character of the
2509** string indicates the column affinity that should be used for the nth
2510** memory cell in the range.
2511*/
2512case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002513 const char *zAffinity; /* The affinity to be applied */
2514 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002515
drh856c1032009-06-02 15:21:42 +00002516 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002517 assert( zAffinity!=0 );
2518 assert( zAffinity[pOp->p2]==0 );
2519 pIn1 = &aMem[pOp->p1];
2520 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002521 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002522 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002523 applyAffinity(pIn1, cAff, encoding);
2524 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002525 }
2526 break;
2527}
2528
drh1db639c2008-01-17 02:36:28 +00002529/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002530** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002531**
drh710c4842010-08-30 01:17:20 +00002532** Convert P2 registers beginning with P1 into the [record format]
2533** use as a data record in a database table or as a key
2534** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002535**
danielk1977751de562008-04-18 09:01:15 +00002536** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002537** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002538** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002539**
drh8a512562005-11-14 22:29:05 +00002540** The mapping from character to affinity is given by the SQLITE_AFF_
2541** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002542**
drh66a51672008-01-03 00:01:23 +00002543** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002544*/
drh1db639c2008-01-17 02:36:28 +00002545case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002546 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2547 Mem *pRec; /* The new record */
2548 u64 nData; /* Number of bytes of data space */
2549 int nHdr; /* Number of bytes of header space */
2550 i64 nByte; /* Data space required for this record */
2551 int nZero; /* Number of zero bytes at the end of the record */
2552 int nVarint; /* Number of bytes in a varint */
2553 u32 serial_type; /* Type field */
2554 Mem *pData0; /* First field to be combined into the record */
2555 Mem *pLast; /* Last field of the record */
2556 int nField; /* Number of fields in the record */
2557 char *zAffinity; /* The affinity string for the record */
2558 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002559 int i; /* Space used in zNewRecord[] header */
2560 int j; /* Space used in zNewRecord[] content */
drh856c1032009-06-02 15:21:42 +00002561 int len; /* Length of a field */
2562
drhf3218fe2004-05-28 08:21:02 +00002563 /* Assuming the record contains N fields, the record format looks
2564 ** like this:
2565 **
drh7a224de2004-06-02 01:22:02 +00002566 ** ------------------------------------------------------------------------
2567 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2568 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002569 **
drh9cbf3422008-01-17 16:22:13 +00002570 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2571 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002572 **
2573 ** Each type field is a varint representing the serial type of the
2574 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002575 ** hdr-size field is also a varint which is the offset from the beginning
2576 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002577 */
drh856c1032009-06-02 15:21:42 +00002578 nData = 0; /* Number of bytes of data space */
2579 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002580 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002581 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002582 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002583 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002584 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002585 nField = pOp->p2;
2586 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002587 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002588
drh2b4ded92010-09-27 21:09:31 +00002589 /* Identify the output register */
2590 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2591 pOut = &aMem[pOp->p3];
2592 memAboutToChange(p, pOut);
2593
drh3e6c0602013-12-10 20:53:01 +00002594 /* Apply the requested affinity to all inputs
2595 */
2596 assert( pData0<=pLast );
2597 if( zAffinity ){
2598 pRec = pData0;
2599 do{
drh57bf4a82014-02-17 14:59:22 +00002600 applyAffinity(pRec++, *(zAffinity++), encoding);
2601 assert( zAffinity[0]==0 || pRec<=pLast );
2602 }while( zAffinity[0] );
drh3e6c0602013-12-10 20:53:01 +00002603 }
2604
drhf3218fe2004-05-28 08:21:02 +00002605 /* Loop through the elements that will make up the record to figure
2606 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002607 */
drh038b7bc2013-12-09 23:17:22 +00002608 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002609 do{
drh2b4ded92010-09-27 21:09:31 +00002610 assert( memIsValid(pRec) );
drhd946db02005-12-29 19:23:06 +00002611 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002612 len = sqlite3VdbeSerialTypeLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002613 if( pRec->flags & MEM_Zero ){
2614 if( nData ){
2615 sqlite3VdbeMemExpandBlob(pRec);
2616 }else{
2617 nZero += pRec->u.nZero;
2618 len -= pRec->u.nZero;
2619 }
2620 }
drhae7e1512007-05-02 16:51:59 +00002621 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002622 testcase( serial_type==127 );
2623 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002624 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002625 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002626
drhf3218fe2004-05-28 08:21:02 +00002627 /* Add the initial header varint and total the size */
drh59bf00c2013-12-08 23:33:28 +00002628 testcase( nHdr==126 );
2629 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002630 if( nHdr<=126 ){
2631 /* The common case */
2632 nHdr += 1;
2633 }else{
2634 /* Rare case of a really large header */
2635 nVarint = sqlite3VarintLen(nHdr);
2636 nHdr += nVarint;
2637 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002638 }
drh038b7bc2013-12-09 23:17:22 +00002639 nByte = nHdr+nData;
drhbb4957f2008-03-20 14:03:29 +00002640 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002641 goto too_big;
2642 }
drhf3218fe2004-05-28 08:21:02 +00002643
danielk1977a7a8e142008-02-13 18:25:27 +00002644 /* Make sure the output register has a buffer large enough to store
2645 ** the new record. The output register (pOp->p3) is not allowed to
2646 ** be one of the input registers (because the following call to
2647 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2648 */
drh9c1905f2008-12-10 22:32:56 +00002649 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002650 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002651 }
danielk1977a7a8e142008-02-13 18:25:27 +00002652 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002653
2654 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002655 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002656 j = nHdr;
2657 assert( pData0<=pLast );
2658 pRec = pData0;
2659 do{
drhd946db02005-12-29 19:23:06 +00002660 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drh038b7bc2013-12-09 23:17:22 +00002661 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drha9ab4812013-12-11 11:00:44 +00002662 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002663 }while( (++pRec)<=pLast );
2664 assert( i==nHdr );
2665 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002666
dan3bc9f742013-08-15 16:18:39 +00002667 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002668 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002669 pOut->flags = MEM_Blob | MEM_Dyn;
2670 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002671 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002672 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002673 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002674 }
drh477df4b2008-01-05 18:48:24 +00002675 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002676 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002677 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002678 break;
2679}
2680
danielk1977a5533162009-02-24 10:01:51 +00002681/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002682** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002683**
2684** Store the number of entries (an integer value) in the table or index
2685** opened by cursor P1 in register P2
2686*/
2687#ifndef SQLITE_OMIT_BTREECOUNT
2688case OP_Count: { /* out2-prerelease */
2689 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002690 BtCursor *pCrsr;
2691
2692 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh3da046d2013-11-11 03:24:11 +00002693 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002694 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002695 rc = sqlite3BtreeCount(pCrsr, &nEntry);
danielk1977a5533162009-02-24 10:01:51 +00002696 pOut->u.i = nEntry;
2697 break;
2698}
2699#endif
2700
danielk1977fd7f0452008-12-17 17:30:26 +00002701/* Opcode: Savepoint P1 * * P4 *
2702**
2703** Open, release or rollback the savepoint named by parameter P4, depending
2704** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2705** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2706*/
2707case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002708 int p1; /* Value of P1 operand */
2709 char *zName; /* Name of savepoint */
2710 int nName;
2711 Savepoint *pNew;
2712 Savepoint *pSavepoint;
2713 Savepoint *pTmp;
2714 int iSavepoint;
2715 int ii;
2716
2717 p1 = pOp->p1;
2718 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002719
2720 /* Assert that the p1 parameter is valid. Also that if there is no open
2721 ** transaction, then there cannot be any savepoints.
2722 */
2723 assert( db->pSavepoint==0 || db->autoCommit==0 );
2724 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2725 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2726 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002727 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002728
2729 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002730 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002731 /* A new savepoint cannot be created if there are active write
2732 ** statements (i.e. open read/write incremental blob handles).
2733 */
2734 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2735 "SQL statements in progress");
2736 rc = SQLITE_BUSY;
2737 }else{
drh856c1032009-06-02 15:21:42 +00002738 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002739
drhbe07ec52011-06-03 12:15:26 +00002740#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002741 /* This call is Ok even if this savepoint is actually a transaction
2742 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2743 ** If this is a transaction savepoint being opened, it is guaranteed
2744 ** that the db->aVTrans[] array is empty. */
2745 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002746 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2747 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002748 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002749#endif
dand9495cd2011-04-27 12:08:04 +00002750
danielk1977fd7f0452008-12-17 17:30:26 +00002751 /* Create a new savepoint structure. */
2752 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2753 if( pNew ){
2754 pNew->zName = (char *)&pNew[1];
2755 memcpy(pNew->zName, zName, nName+1);
2756
2757 /* If there is no open transaction, then mark this as a special
2758 ** "transaction savepoint". */
2759 if( db->autoCommit ){
2760 db->autoCommit = 0;
2761 db->isTransactionSavepoint = 1;
2762 }else{
2763 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002764 }
danielk1977fd7f0452008-12-17 17:30:26 +00002765
2766 /* Link the new savepoint into the database handle's list. */
2767 pNew->pNext = db->pSavepoint;
2768 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002769 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002770 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002771 }
2772 }
2773 }else{
drh856c1032009-06-02 15:21:42 +00002774 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002775
2776 /* Find the named savepoint. If there is no such savepoint, then an
2777 ** an error is returned to the user. */
2778 for(
drh856c1032009-06-02 15:21:42 +00002779 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002780 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002781 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002782 ){
2783 iSavepoint++;
2784 }
2785 if( !pSavepoint ){
2786 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2787 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002788 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002789 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002790 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002791 */
2792 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002793 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002794 );
2795 rc = SQLITE_BUSY;
2796 }else{
2797
2798 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002799 ** and this is a RELEASE command, then the current transaction
2800 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002801 */
2802 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2803 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002804 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002805 goto vdbe_return;
2806 }
danielk1977fd7f0452008-12-17 17:30:26 +00002807 db->autoCommit = 1;
2808 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2809 p->pc = pc;
2810 db->autoCommit = 0;
2811 p->rc = rc = SQLITE_BUSY;
2812 goto vdbe_return;
2813 }
danielk197734cf35d2008-12-18 18:31:38 +00002814 db->isTransactionSavepoint = 0;
2815 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002816 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002817 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002818 if( p1==SAVEPOINT_ROLLBACK ){
2819 for(ii=0; ii<db->nDb; ii++){
2820 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2821 }
drh0f198a72012-02-13 16:43:16 +00002822 }
2823 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002824 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2825 if( rc!=SQLITE_OK ){
2826 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002827 }
danielk1977fd7f0452008-12-17 17:30:26 +00002828 }
drh9f0bbf92009-01-02 21:08:09 +00002829 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002830 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002831 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002832 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002833 }
2834 }
2835
2836 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2837 ** savepoints nested inside of the savepoint being operated on. */
2838 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002839 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002840 db->pSavepoint = pTmp->pNext;
2841 sqlite3DbFree(db, pTmp);
2842 db->nSavepoint--;
2843 }
2844
dan1da40a32009-09-19 17:00:31 +00002845 /* If it is a RELEASE, then destroy the savepoint being operated on
2846 ** too. If it is a ROLLBACK TO, then set the number of deferred
2847 ** constraint violations present in the database to the value stored
2848 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002849 if( p1==SAVEPOINT_RELEASE ){
2850 assert( pSavepoint==db->pSavepoint );
2851 db->pSavepoint = pSavepoint->pNext;
2852 sqlite3DbFree(db, pSavepoint);
2853 if( !isTransaction ){
2854 db->nSavepoint--;
2855 }
dan1da40a32009-09-19 17:00:31 +00002856 }else{
2857 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002858 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002859 }
dand9495cd2011-04-27 12:08:04 +00002860
2861 if( !isTransaction ){
2862 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2863 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2864 }
danielk1977fd7f0452008-12-17 17:30:26 +00002865 }
2866 }
2867
2868 break;
2869}
2870
drh98757152008-01-09 23:04:12 +00002871/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002872**
2873** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002874** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002875** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2876** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002877**
2878** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002879*/
drh9cbf3422008-01-17 16:22:13 +00002880case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002881 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002882 int iRollback;
drh856c1032009-06-02 15:21:42 +00002883 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002884
drh856c1032009-06-02 15:21:42 +00002885 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002886 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002887 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002888 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002889 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002890 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002891 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002892
drh0f198a72012-02-13 16:43:16 +00002893#if 0
drh4f7d3a52013-06-27 23:54:02 +00002894 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002895 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002896 ** still running, and a transaction is active, return an error indicating
2897 ** that the other VMs must complete first.
2898 */
drhad4a4b82008-11-05 16:37:34 +00002899 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2900 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002901 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002902 }else
2903#endif
drh4f7d3a52013-06-27 23:54:02 +00002904 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002905 /* If this instruction implements a COMMIT and other VMs are writing
2906 ** return an error indicating that the other VMs must complete first.
2907 */
2908 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2909 "SQL statements in progress");
2910 rc = SQLITE_BUSY;
2911 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002912 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002913 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002914 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002915 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002916 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002917 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002918 }else{
shane7d3846a2008-12-11 02:58:26 +00002919 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002920 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002921 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002922 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002923 p->rc = rc = SQLITE_BUSY;
2924 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002925 }
danielk19771d850a72004-05-31 08:26:49 +00002926 }
danielk1977bd434552009-03-18 10:33:00 +00002927 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002928 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002929 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002930 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002931 }else{
drh900b31e2007-08-28 02:27:51 +00002932 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002933 }
drh900b31e2007-08-28 02:27:51 +00002934 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002935 }else{
drhf089aa42008-07-08 19:34:06 +00002936 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002937 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002938 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002939 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002940
2941 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002942 }
2943 break;
2944}
2945
drhb22f7c82014-02-06 23:56:27 +00002946/* Opcode: Transaction P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002947**
drh05a86c52014-02-16 01:55:49 +00002948** Begin a transaction on database P1 if a transaction is not already
2949** active.
2950** If P2 is non-zero, then a write-transaction is started, or if a
2951** read-transaction is already active, it is upgraded to a write-transaction.
2952** If P2 is zero, then a read-transaction is started.
drh5e00f6c2001-09-13 13:46:56 +00002953**
drh001bbcb2003-03-19 03:14:00 +00002954** P1 is the index of the database file on which the transaction is
2955** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002956** file used for temporary tables. Indices of 2 or more are used for
2957** attached databases.
drhcabb0812002-09-14 13:47:32 +00002958**
dane0af83a2009-09-08 19:15:01 +00002959** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2960** true (this flag is set if the Vdbe may modify more than one row and may
2961** throw an ABORT exception), a statement transaction may also be opened.
2962** More specifically, a statement transaction is opened iff the database
2963** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002964** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002965** VDBE to be rolled back after an error without having to roll back the
2966** entire transaction. If no error is encountered, the statement transaction
2967** will automatically commit when the VDBE halts.
2968**
drhb22f7c82014-02-06 23:56:27 +00002969** If P5!=0 then this opcode also checks the schema cookie against P3
2970** and the schema generation counter against P4.
2971** The cookie changes its value whenever the database schema changes.
2972** This operation is used to detect when that the cookie has changed
drh05a86c52014-02-16 01:55:49 +00002973** and that the current process needs to reread the schema. If the schema
2974** cookie in P3 differs from the schema cookie in the database header or
2975** if the schema generation counter in P4 differs from the current
2976** generation counter, then an SQLITE_SCHEMA error is raised and execution
2977** halts. The sqlite3_step() wrapper function might then reprepare the
2978** statement and rerun it from the beginning.
drh5e00f6c2001-09-13 13:46:56 +00002979*/
drh9cbf3422008-01-17 16:22:13 +00002980case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002981 Btree *pBt;
drhb22f7c82014-02-06 23:56:27 +00002982 int iMeta;
2983 int iGen;
danielk19771d850a72004-05-31 08:26:49 +00002984
drh1713afb2013-06-28 01:24:57 +00002985 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00002986 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00002987 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002988 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh13447bf2013-07-10 13:33:49 +00002989 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
2990 rc = SQLITE_READONLY;
2991 goto abort_due_to_error;
2992 }
drh653b82a2009-06-22 11:10:47 +00002993 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002994
danielk197724162fe2004-06-04 06:22:00 +00002995 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002996 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002997 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002998 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002999 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00003000 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003001 }
drh9e9f1bd2009-10-13 15:36:51 +00003002 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003003 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003004 }
dane0af83a2009-09-08 19:15:01 +00003005
3006 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003007 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003008 ){
3009 assert( sqlite3BtreeIsInTrans(pBt) );
3010 if( p->iStatement==0 ){
3011 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3012 db->nStatement++;
3013 p->iStatement = db->nSavepoint + db->nStatement;
3014 }
dana311b802011-04-26 19:21:34 +00003015
drh346506f2011-05-25 01:16:42 +00003016 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003017 if( rc==SQLITE_OK ){
3018 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3019 }
dan1da40a32009-09-19 17:00:31 +00003020
3021 /* Store the current value of the database handles deferred constraint
3022 ** counter. If the statement transaction needs to be rolled back,
3023 ** the value of this counter needs to be restored too. */
3024 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003025 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003026 }
drhb22f7c82014-02-06 23:56:27 +00003027
3028 /* Gather the schema version number for checking */
3029 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
3030 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
3031 }else{
3032 iGen = iMeta = 0;
3033 }
3034 assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
3035 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
3036 sqlite3DbFree(db, p->zErrMsg);
3037 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
3038 /* If the schema-cookie from the database file matches the cookie
3039 ** stored with the in-memory representation of the schema, do
3040 ** not reload the schema from the database file.
3041 **
3042 ** If virtual-tables are in use, this is not just an optimization.
3043 ** Often, v-tables store their data in other SQLite tables, which
3044 ** are queried from within xNext() and other v-table methods using
3045 ** prepared queries. If such a query is out-of-date, we do not want to
3046 ** discard the database schema, as the user code implementing the
3047 ** v-table would have to be ready for the sqlite3_vtab structure itself
3048 ** to be invalidated whenever sqlite3_step() is called from within
3049 ** a v-table method.
3050 */
3051 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3052 sqlite3ResetOneSchema(db, pOp->p1);
3053 }
3054 p->expired = 1;
3055 rc = SQLITE_SCHEMA;
drhb86ccfb2003-01-28 23:13:10 +00003056 }
drh5e00f6c2001-09-13 13:46:56 +00003057 break;
3058}
3059
drhb1fdb2a2008-01-05 04:06:03 +00003060/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003061**
drh9cbf3422008-01-17 16:22:13 +00003062** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003063** P3==1 is the schema version. P3==2 is the database format.
3064** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003065** the main database file and P1==1 is the database file used to store
3066** temporary tables.
drh4a324312001-12-21 14:30:42 +00003067**
drh50e5dad2001-09-15 00:57:28 +00003068** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003069** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003070** executing this instruction.
3071*/
drh4c583122008-01-04 22:01:03 +00003072case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003073 int iMeta;
drh856c1032009-06-02 15:21:42 +00003074 int iDb;
3075 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003076
drh1713afb2013-06-28 01:24:57 +00003077 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003078 iDb = pOp->p1;
3079 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003080 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003081 assert( iDb>=0 && iDb<db->nDb );
3082 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00003083 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00003084
danielk1977602b4662009-07-02 07:47:33 +00003085 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003086 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003087 break;
3088}
3089
drh98757152008-01-09 23:04:12 +00003090/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003091**
drh98757152008-01-09 23:04:12 +00003092** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003093** into cookie number P2 of database P1. P2==1 is the schema version.
3094** P2==2 is the database format. P2==3 is the recommended pager cache
3095** size, and so forth. P1==0 is the main database file and P1==1 is the
3096** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003097**
3098** A transaction must be started before executing this opcode.
3099*/
drh9cbf3422008-01-17 16:22:13 +00003100case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003101 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003102 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003103 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003104 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00003105 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003106 pDb = &db->aDb[pOp->p1];
3107 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003108 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003109 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003110 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003111 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003112 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3113 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003114 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003115 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003116 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003117 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003118 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003119 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003120 }
drhfd426c62006-01-30 15:34:22 +00003121 if( pOp->p1==1 ){
3122 /* Invalidate all prepared statements whenever the TEMP database
3123 ** schema is changed. Ticket #1644 */
3124 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003125 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003126 }
drh50e5dad2001-09-15 00:57:28 +00003127 break;
3128}
3129
drh98757152008-01-09 23:04:12 +00003130/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003131** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003132**
drhecdc7532001-09-23 02:35:53 +00003133** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003134** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003135** P3==0 means the main database, P3==1 means the database used for
3136** temporary tables, and P3>1 means used the corresponding attached
3137** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003138** values need not be contiguous but all P1 values should be small integers.
3139** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003140**
drh98757152008-01-09 23:04:12 +00003141** If P5!=0 then use the content of register P2 as the root page, not
3142** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003143**
drhb19a2bc2001-09-16 00:13:26 +00003144** There will be a read lock on the database whenever there is an
3145** open cursor. If the database was unlocked prior to this instruction
3146** then a read lock is acquired as part of this instruction. A read
3147** lock allows other processes to read the database but prohibits
3148** any other process from modifying the database. The read lock is
3149** released when all cursors are closed. If this instruction attempts
3150** to get a read lock but fails, the script terminates with an
3151** SQLITE_BUSY error code.
3152**
danielk1977d336e222009-02-20 10:58:41 +00003153** The P4 value may be either an integer (P4_INT32) or a pointer to
3154** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3155** structure, then said structure defines the content and collating
3156** sequence of the index being opened. Otherwise, if P4 is an integer
3157** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003158**
drh001bbcb2003-03-19 03:14:00 +00003159** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003160*/
drh98757152008-01-09 23:04:12 +00003161/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003162** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003163**
3164** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003165** page is P2. Or if P5!=0 use the content of register P2 to find the
3166** root page.
drhecdc7532001-09-23 02:35:53 +00003167**
danielk1977d336e222009-02-20 10:58:41 +00003168** The P4 value may be either an integer (P4_INT32) or a pointer to
3169** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3170** structure, then said structure defines the content and collating
3171** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003172** value, it is set to the number of columns in the table, or to the
3173** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003174**
drh001bbcb2003-03-19 03:14:00 +00003175** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003176** in read/write mode. For a given table, there can be one or more read-only
3177** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003178**
drh001bbcb2003-03-19 03:14:00 +00003179** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003180*/
drh9cbf3422008-01-17 16:22:13 +00003181case OP_OpenRead:
3182case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003183 int nField;
3184 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003185 int p2;
3186 int iDb;
drhf57b3392001-10-08 13:22:32 +00003187 int wrFlag;
3188 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003189 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003190 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003191
dan428c2182012-08-06 18:50:11 +00003192 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3193 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003194 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003195 assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003196
danfa401de2009-10-16 14:55:03 +00003197 if( p->expired ){
3198 rc = SQLITE_ABORT;
3199 break;
3200 }
3201
drh856c1032009-06-02 15:21:42 +00003202 nField = 0;
3203 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003204 p2 = pOp->p2;
3205 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003206 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003207 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003208 pDb = &db->aDb[iDb];
3209 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003210 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003211 if( pOp->opcode==OP_OpenWrite ){
3212 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003213 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003214 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3215 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003216 }
3217 }else{
3218 wrFlag = 0;
3219 }
dan428c2182012-08-06 18:50:11 +00003220 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003221 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003222 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003223 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003224 assert( memIsValid(pIn2) );
3225 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003226 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003227 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003228 /* The p2 value always comes from a prior OP_CreateTable opcode and
3229 ** that opcode will always set the p2 value to 2 or more or else fail.
3230 ** If there were a failure, the prepared statement would have halted
3231 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003232 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003233 rc = SQLITE_CORRUPT_BKPT;
3234 goto abort_due_to_error;
3235 }
drh5edc3122001-09-13 21:53:09 +00003236 }
danielk1977d336e222009-02-20 10:58:41 +00003237 if( pOp->p4type==P4_KEYINFO ){
3238 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003239 assert( pKeyInfo->enc==ENC(db) );
3240 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003241 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003242 }else if( pOp->p4type==P4_INT32 ){
3243 nField = pOp->p4.i;
3244 }
drh653b82a2009-06-22 11:10:47 +00003245 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003246 assert( nField>=0 );
3247 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drh653b82a2009-06-22 11:10:47 +00003248 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003249 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003250 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003251 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003252 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3253 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003254 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3255 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003256
dana205a482011-08-27 18:48:57 +00003257 /* Since it performs no memory allocation or IO, the only value that
3258 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3259 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003260
drh14da87f2013-11-20 21:51:33 +00003261 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003262 ** SQLite used to check if the root-page flags were sane at this point
3263 ** and report database corruption if they were not, but this check has
3264 ** since moved into the btree layer. */
3265 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drh5e00f6c2001-09-13 13:46:56 +00003266 break;
3267}
3268
drh2a5d9902011-08-26 00:34:45 +00003269/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003270** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003271**
drhb9bb7c12006-06-11 23:41:55 +00003272** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003273** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003274** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003275** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003276**
drh25d3adb2010-04-05 15:11:08 +00003277** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003278** The cursor points to a BTree table if P4==0 and to a BTree index
3279** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003280** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003281**
drh2a5d9902011-08-26 00:34:45 +00003282** The P5 parameter can be a mask of the BTREE_* flags defined
3283** in btree.h. These flags control aspects of the operation of
3284** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3285** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003286*/
drha21a64d2010-04-06 22:33:55 +00003287/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003288** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003289**
3290** This opcode works the same as OP_OpenEphemeral. It has a
3291** different name to distinguish its use. Tables created using
3292** by this opcode will be used for automatically created transient
3293** indices in joins.
3294*/
3295case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003296case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003297 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003298 KeyInfo *pKeyInfo;
3299
drhd4187c72010-08-30 22:15:45 +00003300 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003301 SQLITE_OPEN_READWRITE |
3302 SQLITE_OPEN_CREATE |
3303 SQLITE_OPEN_EXCLUSIVE |
3304 SQLITE_OPEN_DELETEONCLOSE |
3305 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003306 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003307 assert( pOp->p2>=0 );
drh653b82a2009-06-22 11:10:47 +00003308 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003309 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003310 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003311 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3312 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003313 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003314 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003315 }
3316 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003317 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003318 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003319 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003320 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003321 */
drh41e13e12013-11-07 14:09:39 +00003322 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003323 int pgno;
drh66a51672008-01-03 00:01:23 +00003324 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003325 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003326 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003327 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003328 assert( pKeyInfo->db==db );
3329 assert( pKeyInfo->enc==ENC(db) );
3330 pCx->pKeyInfo = pKeyInfo;
3331 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00003332 }
drhf0863fe2005-06-12 21:35:51 +00003333 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003334 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003335 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003336 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003337 }
drh5e00f6c2001-09-13 13:46:56 +00003338 }
drhd4187c72010-08-30 22:15:45 +00003339 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003340 break;
3341}
3342
drh0fd61352014-02-07 02:29:45 +00003343/* Opcode: SorterOpen P1 P2 * P4 *
dan5134d132011-09-02 10:31:11 +00003344**
3345** This opcode works like OP_OpenEphemeral except that it opens
3346** a transient index that is specifically designed to sort large
3347** tables using an external merge-sort algorithm.
3348*/
drhca892a72011-09-03 00:17:51 +00003349case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003350 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003351
drh399af1d2013-11-20 17:25:55 +00003352 assert( pOp->p1>=0 );
3353 assert( pOp->p2>=0 );
dan5134d132011-09-02 10:31:11 +00003354 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3355 if( pCx==0 ) goto no_mem;
3356 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003357 assert( pCx->pKeyInfo->db==db );
3358 assert( pCx->pKeyInfo->enc==ENC(db) );
dan5134d132011-09-02 10:31:11 +00003359 rc = sqlite3VdbeSorterInit(db, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003360 break;
3361}
3362
drh5f612292014-02-08 23:20:32 +00003363/* Opcode: OpenPseudo P1 P2 P3 * *
drh60830e32014-02-10 15:56:34 +00003364** Synopsis: P3 columns in r[P2]
drh70ce3f02003-04-15 19:22:22 +00003365**
3366** Open a new cursor that points to a fake table that contains a single
drh5f612292014-02-08 23:20:32 +00003367** row of data. The content of that one row is the content of memory
3368** register P2. In other words, cursor P1 becomes an alias for the
3369** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003370**
drh2d8d7ce2010-02-15 15:17:05 +00003371** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003372** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003373** individual columns using the OP_Column opcode. The OP_Column opcode
3374** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003375**
3376** P3 is the number of fields in the records that will be stored by
3377** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003378*/
drh9cbf3422008-01-17 16:22:13 +00003379case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003380 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003381
drh653b82a2009-06-22 11:10:47 +00003382 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003383 assert( pOp->p3>=0 );
drh653b82a2009-06-22 11:10:47 +00003384 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003385 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003386 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003387 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003388 pCx->isTable = 1;
drh5f612292014-02-08 23:20:32 +00003389 assert( pOp->p5==0 );
drh70ce3f02003-04-15 19:22:22 +00003390 break;
3391}
3392
drh98757152008-01-09 23:04:12 +00003393/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003394**
3395** Close a cursor previously opened as P1. If P1 is not
3396** currently open, this instruction is a no-op.
3397*/
drh9cbf3422008-01-17 16:22:13 +00003398case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003399 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3400 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3401 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003402 break;
3403}
3404
drh959403f2008-12-12 17:56:16 +00003405/* Opcode: SeekGe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003406** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003407**
danielk1977b790c6c2008-04-18 10:25:24 +00003408** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003409** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003410** to an SQL index, then P3 is the first in an array of P4 registers
3411** that are used as an unpacked index key.
3412**
3413** Reposition cursor P1 so that it points to the smallest entry that
3414** is greater than or equal to the key value. If there are no records
3415** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003416**
drh959403f2008-12-12 17:56:16 +00003417** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003418*/
drh959403f2008-12-12 17:56:16 +00003419/* Opcode: SeekGt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003420** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003421**
danielk1977b790c6c2008-04-18 10:25:24 +00003422** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003423** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003424** to an SQL index, then P3 is the first in an array of P4 registers
3425** that are used as an unpacked index key.
3426**
3427** Reposition cursor P1 so that it points to the smallest entry that
3428** is greater than the key value. If there are no records greater than
3429** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003430**
drh959403f2008-12-12 17:56:16 +00003431** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003432*/
drh959403f2008-12-12 17:56:16 +00003433/* Opcode: SeekLt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003434** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003435**
danielk1977b790c6c2008-04-18 10:25:24 +00003436** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003437** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003438** to an SQL index, then P3 is the first in an array of P4 registers
3439** that are used as an unpacked index key.
3440**
3441** Reposition cursor P1 so that it points to the largest entry that
3442** is less than the key value. If there are no records less than
3443** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003444**
drh959403f2008-12-12 17:56:16 +00003445** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003446*/
drh959403f2008-12-12 17:56:16 +00003447/* Opcode: SeekLe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003448** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003449**
danielk1977b790c6c2008-04-18 10:25:24 +00003450** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003451** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003452** to an SQL index, then P3 is the first in an array of P4 registers
3453** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003454**
danielk1977b790c6c2008-04-18 10:25:24 +00003455** Reposition cursor P1 so that it points to the largest entry that
3456** is less than or equal to the key value. If there are no records
3457** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003458**
drh959403f2008-12-12 17:56:16 +00003459** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003460*/
drh4a1d3652014-02-14 15:13:36 +00003461case OP_SeekLT: /* jump, in3 */
3462case OP_SeekLE: /* jump, in3 */
3463case OP_SeekGE: /* jump, in3 */
3464case OP_SeekGT: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003465 int res;
3466 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003467 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003468 UnpackedRecord r;
3469 int nField;
3470 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003471
drh653b82a2009-06-22 11:10:47 +00003472 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003473 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003474 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003475 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003476 assert( pC->pseudoTableReg==0 );
drh4a1d3652014-02-14 15:13:36 +00003477 assert( OP_SeekLE == OP_SeekLT+1 );
3478 assert( OP_SeekGE == OP_SeekLT+2 );
3479 assert( OP_SeekGT == OP_SeekLT+3 );
drhd4187c72010-08-30 22:15:45 +00003480 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00003481 assert( pC->pCursor!=0 );
3482 oc = pOp->opcode;
3483 pC->nullRow = 0;
3484 if( pC->isTable ){
3485 /* The input value in P3 might be of any type: integer, real, string,
3486 ** blob, or NULL. But it needs to be an integer before we can do
3487 ** the seek, so covert it. */
3488 pIn3 = &aMem[pOp->p3];
3489 applyNumericAffinity(pIn3);
3490 iKey = sqlite3VdbeIntValue(pIn3);
3491 pC->rowidIsValid = 0;
drh959403f2008-12-12 17:56:16 +00003492
drh3da046d2013-11-11 03:24:11 +00003493 /* If the P3 value could not be converted into an integer without
3494 ** loss of information, then special processing is required... */
3495 if( (pIn3->flags & MEM_Int)==0 ){
3496 if( (pIn3->flags & MEM_Real)==0 ){
3497 /* If the P3 value cannot be converted into any kind of a number,
3498 ** then the seek is not possible, so jump to P2 */
drh688852a2014-02-17 22:40:43 +00003499 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003500 break;
3501 }
drh959403f2008-12-12 17:56:16 +00003502
danaa1776f2013-11-26 18:22:59 +00003503 /* If the approximation iKey is larger than the actual real search
3504 ** term, substitute >= for > and < for <=. e.g. if the search term
3505 ** is 4.9 and the integer approximation 5:
3506 **
3507 ** (x > 4.9) -> (x >= 5)
3508 ** (x <= 4.9) -> (x < 5)
3509 */
3510 if( pIn3->r<(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003511 assert( OP_SeekGE==(OP_SeekGT-1) );
3512 assert( OP_SeekLT==(OP_SeekLE-1) );
3513 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
3514 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
danaa1776f2013-11-26 18:22:59 +00003515 }
3516
3517 /* If the approximation iKey is smaller than the actual real search
3518 ** term, substitute <= for < and > for >=. */
3519 else if( pIn3->r>(double)iKey ){
drh4a1d3652014-02-14 15:13:36 +00003520 assert( OP_SeekLE==(OP_SeekLT+1) );
3521 assert( OP_SeekGT==(OP_SeekGE+1) );
3522 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
3523 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003524 }
drh3da046d2013-11-11 03:24:11 +00003525 }
3526 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
3527 if( rc!=SQLITE_OK ){
3528 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003529 }
drh3da046d2013-11-11 03:24:11 +00003530 if( res==0 ){
3531 pC->rowidIsValid = 1;
3532 pC->lastRowid = iKey;
drh8721ce42001-11-07 14:22:00 +00003533 }
drhaa736092009-06-22 00:55:30 +00003534 }else{
drh3da046d2013-11-11 03:24:11 +00003535 nField = pOp->p4.i;
3536 assert( pOp->p4type==P4_INT32 );
3537 assert( nField>0 );
3538 r.pKeyInfo = pC->pKeyInfo;
3539 r.nField = (u16)nField;
3540
3541 /* The next line of code computes as follows, only faster:
drh4a1d3652014-02-14 15:13:36 +00003542 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){
drh3da046d2013-11-11 03:24:11 +00003543 ** r.flags = UNPACKED_INCRKEY;
3544 ** }else{
3545 ** r.flags = 0;
3546 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003547 */
drh4a1d3652014-02-14 15:13:36 +00003548 r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLT)));
3549 assert( oc!=OP_SeekGT || r.flags==UNPACKED_INCRKEY );
3550 assert( oc!=OP_SeekLE || r.flags==UNPACKED_INCRKEY );
3551 assert( oc!=OP_SeekGE || r.flags==0 );
3552 assert( oc!=OP_SeekLT || r.flags==0 );
drh3da046d2013-11-11 03:24:11 +00003553
3554 r.aMem = &aMem[pOp->p3];
3555#ifdef SQLITE_DEBUG
3556 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3557#endif
3558 ExpandBlob(r.aMem);
3559 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
3560 if( rc!=SQLITE_OK ){
3561 goto abort_due_to_error;
3562 }
3563 pC->rowidIsValid = 0;
3564 }
3565 pC->deferredMoveto = 0;
3566 pC->cacheStatus = CACHE_STALE;
3567#ifdef SQLITE_TEST
3568 sqlite3_search_count++;
3569#endif
drh4a1d3652014-02-14 15:13:36 +00003570 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT );
3571 if( res<0 || (res==0 && oc==OP_SeekGT) ){
drhe39a7322014-02-03 14:04:11 +00003572 res = 0;
drh3da046d2013-11-11 03:24:11 +00003573 rc = sqlite3BtreeNext(pC->pCursor, &res);
3574 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3575 pC->rowidIsValid = 0;
3576 }else{
3577 res = 0;
3578 }
3579 }else{
drh4a1d3652014-02-14 15:13:36 +00003580 assert( oc==OP_SeekLT || oc==OP_SeekLE );
3581 if( res>0 || (res==0 && oc==OP_SeekLT) ){
drhe39a7322014-02-03 14:04:11 +00003582 res = 0;
drh3da046d2013-11-11 03:24:11 +00003583 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3584 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3585 pC->rowidIsValid = 0;
3586 }else{
3587 /* res might be negative because the table is empty. Check to
3588 ** see if this is the case.
3589 */
3590 res = sqlite3BtreeEof(pC->pCursor);
3591 }
3592 }
3593 assert( pOp->p2>0 );
drh688852a2014-02-17 22:40:43 +00003594 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003595 if( res ){
danielk1977f7b9d662008-06-23 18:49:43 +00003596 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003597 }
drh5e00f6c2001-09-13 13:46:56 +00003598 break;
3599}
3600
drh959403f2008-12-12 17:56:16 +00003601/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003602** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003603**
3604** P1 is an open table cursor and P2 is a rowid integer. Arrange
3605** for P1 to move so that it points to the rowid given by P2.
3606**
3607** This is actually a deferred seek. Nothing actually happens until
3608** the cursor is used to read a record. That way, if no reads
3609** occur, no unnecessary I/O happens.
3610*/
3611case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003612 VdbeCursor *pC;
3613
drh653b82a2009-06-22 11:10:47 +00003614 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3615 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003616 assert( pC!=0 );
drh3da046d2013-11-11 03:24:11 +00003617 assert( pC->pCursor!=0 );
3618 assert( pC->isTable );
3619 pC->nullRow = 0;
3620 pIn2 = &aMem[pOp->p2];
3621 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3622 pC->rowidIsValid = 0;
3623 pC->deferredMoveto = 1;
drh959403f2008-12-12 17:56:16 +00003624 break;
3625}
3626
3627
drh8cff69d2009-11-12 19:59:44 +00003628/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003629** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003630**
drh8cff69d2009-11-12 19:59:44 +00003631** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3632** P4>0 then register P3 is the first of P4 registers that form an unpacked
3633** record.
3634**
3635** Cursor P1 is on an index btree. If the record identified by P3 and P4
3636** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003637** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003638**
3639** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003640*/
drh8cff69d2009-11-12 19:59:44 +00003641/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003642** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003643**
drh8cff69d2009-11-12 19:59:44 +00003644** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3645** P4>0 then register P3 is the first of P4 registers that form an unpacked
3646** record.
3647**
3648** Cursor P1 is on an index btree. If the record identified by P3 and P4
3649** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3650** does contain an entry whose prefix matches the P3/P4 record then control
3651** falls through to the next instruction and P1 is left pointing at the
3652** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003653**
drh6f225d02013-10-26 13:36:51 +00003654** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003655*/
drh6f225d02013-10-26 13:36:51 +00003656/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003657** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003658**
3659** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3660** P4>0 then register P3 is the first of P4 registers that form an unpacked
3661** record.
3662**
3663** Cursor P1 is on an index btree. If the record identified by P3 and P4
3664** contains any NULL value, jump immediately to P2. If all terms of the
3665** record are not-NULL then a check is done to determine if any row in the
3666** P1 index btree has a matching key prefix. If there are no matches, jump
3667** immediately to P2. If there is a match, fall through and leave the P1
3668** cursor pointing to the matching row.
3669**
3670** This opcode is similar to OP_NotFound with the exceptions that the
3671** branch is always taken if any part of the search key input is NULL.
3672**
3673** See also: NotFound, Found, NotExists
3674*/
3675case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003676case OP_NotFound: /* jump, in3 */
3677case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003678 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00003679 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003680 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003681 int res;
dan03e9cfc2011-09-05 14:20:27 +00003682 char *pFree;
drh856c1032009-06-02 15:21:42 +00003683 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003684 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003685 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003686
dan0ff297e2009-09-25 17:03:14 +00003687#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003688 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003689#endif
3690
drhaa736092009-06-22 00:55:30 +00003691 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003692 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003693 pC = p->apCsr[pOp->p1];
3694 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003695 pIn3 = &aMem[pOp->p3];
drh3da046d2013-11-11 03:24:11 +00003696 assert( pC->pCursor!=0 );
3697 assert( pC->isTable==0 );
drha9ab4812013-12-11 11:00:44 +00003698 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
drh3da046d2013-11-11 03:24:11 +00003699 if( pOp->p4.i>0 ){
3700 r.pKeyInfo = pC->pKeyInfo;
3701 r.nField = (u16)pOp->p4.i;
3702 r.aMem = pIn3;
drh826af372014-02-08 19:12:21 +00003703 for(ii=0; ii<r.nField; ii++){
3704 assert( memIsValid(&r.aMem[ii]) );
3705 ExpandBlob(&r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003706#ifdef SQLITE_DEBUG
drh826af372014-02-08 19:12:21 +00003707 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
drh2b4ded92010-09-27 21:09:31 +00003708#endif
drh826af372014-02-08 19:12:21 +00003709 }
drh3da046d2013-11-11 03:24:11 +00003710 r.flags = UNPACKED_PREFIX_MATCH;
3711 pIdxKey = &r;
3712 }else{
3713 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3714 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3715 );
3716 if( pIdxKey==0 ) goto no_mem;
3717 assert( pIn3->flags & MEM_Blob );
3718 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
3719 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
3720 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3721 }
3722 if( pOp->opcode==OP_NoConflict ){
3723 /* For the OP_NoConflict opcode, take the jump if any of the
3724 ** input fields are NULL, since any key with a NULL will not
3725 ** conflict */
3726 for(ii=0; ii<r.nField; ii++){
3727 if( r.aMem[ii].flags & MEM_Null ){
drh688852a2014-02-17 22:40:43 +00003728 pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
drh3da046d2013-11-11 03:24:11 +00003729 break;
drh6f225d02013-10-26 13:36:51 +00003730 }
3731 }
drh5e00f6c2001-09-13 13:46:56 +00003732 }
drh3da046d2013-11-11 03:24:11 +00003733 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3734 if( pOp->p4.i==0 ){
3735 sqlite3DbFree(db, pFree);
3736 }
3737 if( rc!=SQLITE_OK ){
3738 break;
3739 }
drh1fd522f2013-11-21 00:10:35 +00003740 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003741 alreadyExists = (res==0);
3742 pC->nullRow = 1-alreadyExists;
3743 pC->deferredMoveto = 0;
3744 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003745 if( pOp->opcode==OP_Found ){
drh688852a2014-02-17 22:40:43 +00003746 VdbeBranchTaken(alreadyExists!=0,2);
drh5e00f6c2001-09-13 13:46:56 +00003747 if( alreadyExists ) pc = pOp->p2 - 1;
3748 }else{
drh688852a2014-02-17 22:40:43 +00003749 VdbeBranchTaken(alreadyExists==0,2);
drh5e00f6c2001-09-13 13:46:56 +00003750 if( !alreadyExists ) pc = pOp->p2 - 1;
3751 }
drh5e00f6c2001-09-13 13:46:56 +00003752 break;
3753}
3754
drh9cbf3422008-01-17 16:22:13 +00003755/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003756** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00003757**
drh261c02d2013-10-25 14:46:15 +00003758** P1 is the index of a cursor open on an SQL table btree (with integer
3759** keys). P3 is an integer rowid. If P1 does not contain a record with
3760** rowid P3 then jump immediately to P2. If P1 does contain a record
3761** with rowid P3 then leave the cursor pointing at that record and fall
3762** through to the next instruction.
drh6b125452002-01-28 15:53:03 +00003763**
drh261c02d2013-10-25 14:46:15 +00003764** The OP_NotFound opcode performs the same operation on index btrees
3765** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00003766**
drh11e85272013-10-26 15:40:48 +00003767** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00003768*/
drh9cbf3422008-01-17 16:22:13 +00003769case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003770 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003771 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003772 int res;
3773 u64 iKey;
3774
drh3c657212009-11-17 23:59:58 +00003775 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003776 assert( pIn3->flags & MEM_Int );
3777 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3778 pC = p->apCsr[pOp->p1];
3779 assert( pC!=0 );
3780 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003781 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003782 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00003783 assert( pCrsr!=0 );
3784 res = 0;
3785 iKey = pIn3->u.i;
3786 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
3787 pC->lastRowid = pIn3->u.i;
3788 pC->rowidIsValid = res==0 ?1:0;
3789 pC->nullRow = 0;
3790 pC->cacheStatus = CACHE_STALE;
3791 pC->deferredMoveto = 0;
drh688852a2014-02-17 22:40:43 +00003792 VdbeBranchTaken(res!=0,2);
drh3da046d2013-11-11 03:24:11 +00003793 if( res!=0 ){
danielk1977f7b9d662008-06-23 18:49:43 +00003794 pc = pOp->p2 - 1;
3795 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003796 }
drh1fd522f2013-11-21 00:10:35 +00003797 pC->seekResult = res;
drh6b125452002-01-28 15:53:03 +00003798 break;
3799}
3800
drh4c583122008-01-04 22:01:03 +00003801/* Opcode: Sequence P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003802** Synopsis: r[P2]=rowid
drh4db38a72005-09-01 12:16:28 +00003803**
drh4c583122008-01-04 22:01:03 +00003804** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003805** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003806** The sequence number on the cursor is incremented after this
3807** instruction.
drh4db38a72005-09-01 12:16:28 +00003808*/
drh4c583122008-01-04 22:01:03 +00003809case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003810 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3811 assert( p->apCsr[pOp->p1]!=0 );
3812 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003813 break;
3814}
3815
3816
drh98757152008-01-09 23:04:12 +00003817/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003818** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00003819**
drhf0863fe2005-06-12 21:35:51 +00003820** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003821** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003822** table that cursor P1 points to. The new record number is written
3823** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003824**
dan76d462e2009-08-30 11:42:51 +00003825** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3826** the largest previously generated record number. No new record numbers are
3827** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003828** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003829** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003830** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003831*/
drh4c583122008-01-04 22:01:03 +00003832case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003833 i64 v; /* The new rowid */
3834 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3835 int res; /* Result of an sqlite3BtreeLast() */
3836 int cnt; /* Counter to limit the number of searches */
3837 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003838 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003839
drh856c1032009-06-02 15:21:42 +00003840 v = 0;
3841 res = 0;
drhaa736092009-06-22 00:55:30 +00003842 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3843 pC = p->apCsr[pOp->p1];
3844 assert( pC!=0 );
3845 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003846 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003847 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003848 /* The next rowid or record number (different terms for the same
3849 ** thing) is obtained in a two-step algorithm.
3850 **
3851 ** First we attempt to find the largest existing rowid and add one
3852 ** to that. But if the largest existing rowid is already the maximum
3853 ** positive integer, we have to fall through to the second
3854 ** probabilistic algorithm
3855 **
3856 ** The second algorithm is to select a rowid at random and see if
3857 ** it already exists in the table. If it does not exist, we have
3858 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003859 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003860 */
drhaa736092009-06-22 00:55:30 +00003861 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003862
drh75f86a42005-02-17 00:03:06 +00003863#ifdef SQLITE_32BIT_ROWID
3864# define MAX_ROWID 0x7fffffff
3865#else
drhfe2093d2005-01-20 22:48:47 +00003866 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3867 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3868 ** to provide the constant while making all compilers happy.
3869 */
danielk197764202cf2008-11-17 15:31:47 +00003870# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003871#endif
drhfe2093d2005-01-20 22:48:47 +00003872
drh5cf8e8c2002-02-19 22:42:05 +00003873 if( !pC->useRandomRowid ){
drhe0670b62014-02-12 21:31:12 +00003874 rc = sqlite3BtreeLast(pC->pCursor, &res);
3875 if( rc!=SQLITE_OK ){
3876 goto abort_due_to_error;
3877 }
3878 if( res ){
3879 v = 1; /* IMP: R-61914-48074 */
3880 }else{
3881 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
3882 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3883 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
3884 if( v>=MAX_ROWID ){
3885 pC->useRandomRowid = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003886 }else{
drhe0670b62014-02-12 21:31:12 +00003887 v++; /* IMP: R-29538-34987 */
drh5cf8e8c2002-02-19 22:42:05 +00003888 }
drh3fc190c2001-09-14 03:24:23 +00003889 }
drhe0670b62014-02-12 21:31:12 +00003890 }
drh205f48e2004-11-05 00:43:11 +00003891
3892#ifndef SQLITE_OMIT_AUTOINCREMENT
drhe0670b62014-02-12 21:31:12 +00003893 if( pOp->p3 ){
3894 /* Assert that P3 is a valid memory cell. */
3895 assert( pOp->p3>0 );
3896 if( p->pFrame ){
3897 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003898 /* Assert that P3 is a valid memory cell. */
drhe0670b62014-02-12 21:31:12 +00003899 assert( pOp->p3<=pFrame->nMem );
3900 pMem = &pFrame->aMem[pOp->p3];
3901 }else{
3902 /* Assert that P3 is a valid memory cell. */
3903 assert( pOp->p3<=(p->nMem-p->nCursor) );
3904 pMem = &aMem[pOp->p3];
3905 memAboutToChange(p, pMem);
drh205f48e2004-11-05 00:43:11 +00003906 }
drhe0670b62014-02-12 21:31:12 +00003907 assert( memIsValid(pMem) );
drh205f48e2004-11-05 00:43:11 +00003908
drhe0670b62014-02-12 21:31:12 +00003909 REGISTER_TRACE(pOp->p3, pMem);
3910 sqlite3VdbeMemIntegerify(pMem);
3911 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
3912 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
3913 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
3914 goto abort_due_to_error;
3915 }
3916 if( v<pMem->u.i+1 ){
3917 v = pMem->u.i + 1;
3918 }
3919 pMem->u.i = v;
drh5cf8e8c2002-02-19 22:42:05 +00003920 }
drhe0670b62014-02-12 21:31:12 +00003921#endif
drh5cf8e8c2002-02-19 22:42:05 +00003922 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003923 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003924 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003925 ** engine starts picking positive candidate ROWIDs at random until
3926 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003927 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3928 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003929 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00003930 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003931 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3932 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003933 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003934 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3935 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003936 && (res==0)
3937 && (++cnt<100)){
3938 /* collision - try another random rowid */
3939 sqlite3_randomness(sizeof(v), &v);
3940 if( cnt<5 ){
3941 /* try "small" random rowids for the initial attempts */
3942 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00003943 }else{
shanehc4d340a2010-09-01 02:37:56 +00003944 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00003945 }
shanehc4d340a2010-09-01 02:37:56 +00003946 v++; /* ensure non-zero */
3947 }
drhaa736092009-06-22 00:55:30 +00003948 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00003949 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00003950 goto abort_due_to_error;
3951 }
drh748a52c2010-09-01 11:50:08 +00003952 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00003953 }
drhf0863fe2005-06-12 21:35:51 +00003954 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003955 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003956 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003957 }
drh4c583122008-01-04 22:01:03 +00003958 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003959 break;
3960}
3961
danielk19771f4aa332008-01-03 09:51:55 +00003962/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003963** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00003964**
jplyon5a564222003-06-02 06:15:58 +00003965** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003966** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00003967** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00003968** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00003969** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00003970**
danielk19771f4aa332008-01-03 09:51:55 +00003971** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3972** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003973** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003974** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003975**
drh3e9ca092009-09-08 01:14:48 +00003976** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
3977** the last seek operation (OP_NotExists) was a success, then this
3978** operation will not attempt to find the appropriate row before doing
3979** the insert but will instead overwrite the row that the cursor is
3980** currently pointing to. Presumably, the prior OP_NotExists opcode
3981** has already positioned the cursor correctly. This is an optimization
3982** that boosts performance by avoiding redundant seeks.
3983**
3984** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
3985** UPDATE operation. Otherwise (if the flag is clear) then this opcode
3986** is part of an INSERT operation. The difference is only important to
3987** the update hook.
3988**
drh66a51672008-01-03 00:01:23 +00003989** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003990** may be NULL. If it is not NULL, then the update-hook
3991** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3992**
drh93aed5a2008-01-16 17:46:38 +00003993** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3994** allocated, then ownership of P2 is transferred to the pseudo-cursor
3995** and register P2 becomes ephemeral. If the cursor is changed, the
3996** value of register P2 will then change. Make sure this does not
3997** cause any problems.)
3998**
drhf0863fe2005-06-12 21:35:51 +00003999** This instruction only works on tables. The equivalent instruction
4000** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004001*/
drhe05c9292009-10-29 13:48:10 +00004002/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004003** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004004**
4005** This works exactly like OP_Insert except that the key is the
4006** integer value P3, not the value of the integer stored in register P3.
4007*/
4008case OP_Insert:
4009case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004010 Mem *pData; /* MEM cell holding data for the record to be inserted */
4011 Mem *pKey; /* MEM cell holding key for the record */
4012 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4013 VdbeCursor *pC; /* Cursor to table into which insert is written */
4014 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004015 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004016 const char *zDb; /* database name - used by the update hook */
4017 const char *zTbl; /* Table name - used by the opdate hook */
4018 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004019
drha6c2ed92009-11-14 23:22:23 +00004020 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004021 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004022 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004023 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004024 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004025 assert( pC->pCursor!=0 );
4026 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004027 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004028 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004029
drhe05c9292009-10-29 13:48:10 +00004030 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004031 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004032 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004033 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004034 REGISTER_TRACE(pOp->p3, pKey);
4035 iKey = pKey->u.i;
4036 }else{
4037 assert( pOp->opcode==OP_InsertInt );
4038 iKey = pOp->p3;
4039 }
4040
drha05a7222008-01-19 03:35:58 +00004041 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004042 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004043 if( pData->flags & MEM_Null ){
4044 pData->z = 0;
4045 pData->n = 0;
4046 }else{
4047 assert( pData->flags & (MEM_Blob|MEM_Str) );
4048 }
drh3e9ca092009-09-08 01:14:48 +00004049 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4050 if( pData->flags & MEM_Zero ){
4051 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004052 }else{
drh3e9ca092009-09-08 01:14:48 +00004053 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004054 }
drh3e9ca092009-09-08 01:14:48 +00004055 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4056 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004057 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004058 );
drha05a7222008-01-19 03:35:58 +00004059 pC->rowidIsValid = 0;
4060 pC->deferredMoveto = 0;
4061 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004062
drha05a7222008-01-19 03:35:58 +00004063 /* Invoke the update-hook if required. */
4064 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004065 zDb = db->aDb[pC->iDb].zName;
4066 zTbl = pOp->p4.z;
4067 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004068 assert( pC->isTable );
4069 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4070 assert( pC->iDb>=0 );
4071 }
drh5e00f6c2001-09-13 13:46:56 +00004072 break;
4073}
4074
drh98757152008-01-09 23:04:12 +00004075/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004076**
drh5edc3122001-09-13 21:53:09 +00004077** Delete the record at which the P1 cursor is currently pointing.
4078**
4079** The cursor will be left pointing at either the next or the previous
4080** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004081** the next Next instruction will be a no-op. Hence it is OK to delete
4082** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004083**
rdcb0c374f2004-02-20 22:53:38 +00004084** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004085** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004086**
drh91fd4d42008-01-19 20:11:25 +00004087** P1 must not be pseudo-table. It has to be a real table with
4088** multiple rows.
4089**
4090** If P4 is not NULL, then it is the name of the table that P1 is
4091** pointing to. The update hook will be invoked, if it exists.
4092** If P4 is not NULL then the P1 cursor must have been positioned
4093** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004094*/
drh9cbf3422008-01-17 16:22:13 +00004095case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004096 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004097 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004098
drh653b82a2009-06-22 11:10:47 +00004099 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4100 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004101 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004102 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
drhbbbb0e82013-11-26 23:27:07 +00004103 iKey = pC->lastRowid; /* Only used for the update hook */
danielk197794eb6a12005-12-15 15:22:08 +00004104
drh9a65f2c2009-06-22 19:05:40 +00004105 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4106 ** OP_Column on the same table without any intervening operations that
4107 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4108 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4109 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4110 ** to guard against future changes to the code generator.
4111 **/
4112 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004113 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004114 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4115
drh91fd4d42008-01-19 20:11:25 +00004116 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004117 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004118
drh91fd4d42008-01-19 20:11:25 +00004119 /* Invoke the update-hook if required. */
drhbbbb0e82013-11-26 23:27:07 +00004120 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
drh2c77be02013-11-27 21:07:03 +00004121 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
4122 db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
drh91fd4d42008-01-19 20:11:25 +00004123 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004124 }
danielk1977b28af712004-06-21 06:50:26 +00004125 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004126 break;
4127}
drhb7f1d9a2009-09-08 02:27:58 +00004128/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004129**
drhb7f1d9a2009-09-08 02:27:58 +00004130** The value of the change counter is copied to the database handle
4131** change counter (returned by subsequent calls to sqlite3_changes()).
4132** Then the VMs internal change counter resets to 0.
4133** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004134*/
drh9cbf3422008-01-17 16:22:13 +00004135case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004136 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004137 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004138 break;
4139}
4140
drh1153c7b2013-11-01 22:02:56 +00004141/* Opcode: SorterCompare P1 P2 P3 P4
4142** Synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004143**
drh1153c7b2013-11-01 22:02:56 +00004144** P1 is a sorter cursor. This instruction compares a prefix of the
4145** the record blob in register P3 against a prefix of the entry that
4146** the sorter cursor currently points to. The final P4 fields of both
4147** the P3 and sorter record are ignored.
4148**
4149** If either P3 or the sorter contains a NULL in one of their significant
4150** fields (not counting the P4 fields at the end which are ignored) then
4151** the comparison is assumed to be equal.
4152**
4153** Fall through to next instruction if the two records compare equal to
4154** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004155*/
4156case OP_SorterCompare: {
4157 VdbeCursor *pC;
4158 int res;
drh1153c7b2013-11-01 22:02:56 +00004159 int nIgnore;
dan5134d132011-09-02 10:31:11 +00004160
4161 pC = p->apCsr[pOp->p1];
4162 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004163 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004164 pIn3 = &aMem[pOp->p3];
drh1153c7b2013-11-01 22:02:56 +00004165 nIgnore = pOp->p4.i;
4166 rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
drh688852a2014-02-17 22:40:43 +00004167 VdbeBranchTaken(res!=0,2);
dan5134d132011-09-02 10:31:11 +00004168 if( res ){
4169 pc = pOp->p2-1;
4170 }
4171 break;
4172};
4173
4174/* Opcode: SorterData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004175** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004176**
4177** Write into register P2 the current sorter data for sorter cursor P1.
4178*/
4179case OP_SorterData: {
4180 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004181
dan5134d132011-09-02 10:31:11 +00004182 pOut = &aMem[pOp->p2];
4183 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004184 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004185 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4186 break;
4187}
4188
drh98757152008-01-09 23:04:12 +00004189/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004190** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004191**
drh98757152008-01-09 23:04:12 +00004192** Write into register P2 the complete row data for cursor P1.
4193** There is no interpretation of the data.
4194** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004195** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004196**
drhde4fcfd2008-01-19 23:50:26 +00004197** If the P1 cursor must be pointing to a valid row (not a NULL row)
4198** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004199*/
drh98757152008-01-09 23:04:12 +00004200/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004201** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004202**
drh98757152008-01-09 23:04:12 +00004203** Write into register P2 the complete row key for cursor P1.
4204** There is no interpretation of the data.
drh0fd61352014-02-07 02:29:45 +00004205** The key is copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004206** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004207**
drhde4fcfd2008-01-19 23:50:26 +00004208** If the P1 cursor must be pointing to a valid row (not a NULL row)
4209** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004210*/
danielk1977a7a8e142008-02-13 18:25:27 +00004211case OP_RowKey:
4212case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004213 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004214 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004215 u32 n;
drh856c1032009-06-02 15:21:42 +00004216 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004217
drha6c2ed92009-11-14 23:22:23 +00004218 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004219 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004220
drhf0863fe2005-06-12 21:35:51 +00004221 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004222 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4223 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004224 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004225 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004226 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004227 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004228 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004229 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004230 assert( pC->pCursor!=0 );
4231 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004232 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004233
4234 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4235 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4236 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4237 ** a no-op and can never fail. But we leave it in place as a safety.
4238 */
4239 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004240 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004241 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4242
drh14da87f2013-11-20 21:51:33 +00004243 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004244 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004245 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004246 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004247 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004248 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004249 }
drhbfb19dc2009-06-05 16:46:53 +00004250 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004251 }else{
drhb07028f2011-10-14 21:49:18 +00004252 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004253 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004254 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004255 goto too_big;
4256 }
drhde4fcfd2008-01-19 23:50:26 +00004257 }
danielk1977a7a8e142008-02-13 18:25:27 +00004258 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4259 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004260 }
danielk1977a7a8e142008-02-13 18:25:27 +00004261 pOut->n = n;
4262 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004263 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004264 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4265 }else{
4266 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004267 }
danielk197796cb76f2008-01-04 13:24:28 +00004268 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004269 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004270 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004271 break;
4272}
4273
drh2133d822008-01-03 18:44:59 +00004274/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004275** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004276**
drh2133d822008-01-03 18:44:59 +00004277** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004278** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004279**
4280** P1 can be either an ordinary table or a virtual table. There used to
4281** be a separate OP_VRowid opcode for use with virtual tables, but this
4282** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004283*/
drh4c583122008-01-04 22:01:03 +00004284case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004285 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004286 i64 v;
drh856c1032009-06-02 15:21:42 +00004287 sqlite3_vtab *pVtab;
4288 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004289
drh653b82a2009-06-22 11:10:47 +00004290 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4291 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004292 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004293 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004294 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004295 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004296 break;
4297 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004298 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004299#ifndef SQLITE_OMIT_VIRTUALTABLE
4300 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004301 pVtab = pC->pVtabCursor->pVtab;
4302 pModule = pVtab->pModule;
4303 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004304 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004305 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004306#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004307 }else{
drh6be240e2009-07-14 02:33:02 +00004308 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004309 rc = sqlite3VdbeCursorMoveto(pC);
4310 if( rc ) goto abort_due_to_error;
4311 if( pC->rowidIsValid ){
4312 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004313 }else{
drhc27ae612009-07-14 18:35:44 +00004314 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4315 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004316 }
drh5e00f6c2001-09-13 13:46:56 +00004317 }
drh4c583122008-01-04 22:01:03 +00004318 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004319 break;
4320}
4321
drh9cbf3422008-01-17 16:22:13 +00004322/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004323**
4324** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004325** that occur while the cursor is on the null row will always
4326** write a NULL.
drh17f71932002-02-21 12:01:27 +00004327*/
drh9cbf3422008-01-17 16:22:13 +00004328case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004329 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004330
drh653b82a2009-06-22 11:10:47 +00004331 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4332 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004333 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004334 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004335 pC->rowidIsValid = 0;
drh399af1d2013-11-20 17:25:55 +00004336 pC->cacheStatus = CACHE_STALE;
danielk1977be51a652008-10-08 17:58:48 +00004337 if( pC->pCursor ){
4338 sqlite3BtreeClearCursor(pC->pCursor);
4339 }
drh17f71932002-02-21 12:01:27 +00004340 break;
4341}
4342
drh9cbf3422008-01-17 16:22:13 +00004343/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004344**
drhf0863fe2005-06-12 21:35:51 +00004345** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004346** will refer to the last entry in the database table or index.
4347** If the table or index is empty and P2>0, then jump immediately to P2.
4348** If P2 is 0 or if the table or index is not empty, fall through
4349** to the following instruction.
4350*/
drh9cbf3422008-01-17 16:22:13 +00004351case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004352 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004353 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004354 int res;
drh9562b552002-02-19 15:00:07 +00004355
drh653b82a2009-06-22 11:10:47 +00004356 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4357 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004358 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004359 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004360 res = 0;
drh3da046d2013-11-11 03:24:11 +00004361 assert( pCrsr!=0 );
4362 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004363 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004364 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004365 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004366 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004367 if( pOp->p2>0 ){
4368 VdbeBranchTaken(res!=0,2);
4369 if( res ) pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004370 }
4371 break;
4372}
4373
drh0342b1f2005-09-01 03:07:44 +00004374
drh9cbf3422008-01-17 16:22:13 +00004375/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004376**
4377** This opcode does exactly the same thing as OP_Rewind except that
4378** it increments an undocumented global variable used for testing.
4379**
4380** Sorting is accomplished by writing records into a sorting index,
4381** then rewinding that index and playing it back from beginning to
4382** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4383** rewinding so that the global variable will be incremented and
4384** regression tests can determine whether or not the optimizer is
4385** correctly optimizing out sorts.
4386*/
drhc6aff302011-09-01 15:32:47 +00004387case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004388case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004389#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004390 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004391 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004392#endif
drh9b47ee32013-08-20 03:13:51 +00004393 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004394 /* Fall through into OP_Rewind */
4395}
drh9cbf3422008-01-17 16:22:13 +00004396/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004397**
drhf0863fe2005-06-12 21:35:51 +00004398** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004399** will refer to the first entry in the database table or index.
4400** If the table or index is empty and P2>0, then jump immediately to P2.
4401** If P2 is 0 or if the table or index is not empty, fall through
4402** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004403*/
drh9cbf3422008-01-17 16:22:13 +00004404case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004405 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004406 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004407 int res;
drh5e00f6c2001-09-13 13:46:56 +00004408
drh653b82a2009-06-22 11:10:47 +00004409 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4410 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004411 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004412 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004413 res = 1;
dan689ab892011-08-12 15:02:00 +00004414 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004415 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004416 }else{
4417 pCrsr = pC->pCursor;
4418 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004419 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004420 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004421 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004422 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004423 }
drh9c1905f2008-12-10 22:32:56 +00004424 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004425 assert( pOp->p2>0 && pOp->p2<p->nOp );
drh688852a2014-02-17 22:40:43 +00004426 VdbeBranchTaken(res!=0,2);
drha05a7222008-01-19 03:35:58 +00004427 if( res ){
drhf4dada72004-05-11 09:57:35 +00004428 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004429 }
4430 break;
4431}
4432
drh0fd61352014-02-07 02:29:45 +00004433/* Opcode: Next P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004434**
4435** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004436** table or index. If there are no more key/value pairs then fall through
4437** to the following instruction. But if the cursor advance was successful,
4438** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004439**
drhf93cd942013-11-21 03:12:25 +00004440** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4441** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004442**
drhe39a7322014-02-03 14:04:11 +00004443** The P3 value is a hint to the btree implementation. If P3==1, that
4444** means P1 is an SQL index and that this instruction could have been
4445** omitted if that index had been unique. P3 is usually 0. P3 is
4446** always either 0 or 1.
4447**
dana205a482011-08-27 18:48:57 +00004448** P4 is always of type P4_ADVANCE. The function pointer points to
4449** sqlite3BtreeNext().
4450**
drhafc266a2010-03-31 17:47:44 +00004451** If P5 is positive and the jump is taken, then event counter
4452** number P5-1 in the prepared statement is incremented.
4453**
drhf93cd942013-11-21 03:12:25 +00004454** See also: Prev, NextIfOpen
4455*/
drh0fd61352014-02-07 02:29:45 +00004456/* Opcode: NextIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004457**
4458** This opcode works just like OP_Next except that if cursor P1 is not
4459** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004460*/
drh0fd61352014-02-07 02:29:45 +00004461/* Opcode: Prev P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004462**
4463** Back up cursor P1 so that it points to the previous key/data pair in its
4464** table or index. If there is no previous key/value pairs then fall through
4465** to the following instruction. But if the cursor backup was successful,
4466** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004467**
drhf93cd942013-11-21 03:12:25 +00004468** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4469** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004470**
drhe39a7322014-02-03 14:04:11 +00004471** The P3 value is a hint to the btree implementation. If P3==1, that
4472** means P1 is an SQL index and that this instruction could have been
4473** omitted if that index had been unique. P3 is usually 0. P3 is
4474** always either 0 or 1.
4475**
dana205a482011-08-27 18:48:57 +00004476** P4 is always of type P4_ADVANCE. The function pointer points to
4477** sqlite3BtreePrevious().
4478**
drhafc266a2010-03-31 17:47:44 +00004479** If P5 is positive and the jump is taken, then event counter
4480** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004481*/
drh0fd61352014-02-07 02:29:45 +00004482/* Opcode: PrevIfOpen P1 P2 P3 P4 P5
drhf93cd942013-11-21 03:12:25 +00004483**
4484** This opcode works just like OP_Prev except that if cursor P1 is not
4485** open it behaves a no-op.
4486*/
4487case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004488 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004489 int res;
drh8721ce42001-11-07 14:22:00 +00004490
drhf93cd942013-11-21 03:12:25 +00004491 pC = p->apCsr[pOp->p1];
4492 assert( isSorter(pC) );
4493 rc = sqlite3VdbeSorterNext(db, pC, &res);
4494 goto next_tail;
4495case OP_PrevIfOpen: /* jump */
4496case OP_NextIfOpen: /* jump */
4497 if( p->apCsr[pOp->p1]==0 ) break;
4498 /* Fall through */
4499case OP_Prev: /* jump */
4500case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004501 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004502 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004503 pC = p->apCsr[pOp->p1];
drhe39a7322014-02-03 14:04:11 +00004504 res = pOp->p3;
drhf93cd942013-11-21 03:12:25 +00004505 assert( pC!=0 );
4506 assert( pC->deferredMoveto==0 );
4507 assert( pC->pCursor );
drhe39a7322014-02-03 14:04:11 +00004508 assert( res==0 || (res==1 && pC->isTable==0) );
4509 testcase( res==1 );
drhf93cd942013-11-21 03:12:25 +00004510 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4511 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4512 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4513 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
4514 rc = pOp->p4.xAdvance(pC->pCursor, &res);
4515next_tail:
drha3460582008-07-11 21:02:53 +00004516 pC->cacheStatus = CACHE_STALE;
drh688852a2014-02-17 22:40:43 +00004517 VdbeBranchTaken(res==0,2);
drha3460582008-07-11 21:02:53 +00004518 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004519 pC->nullRow = 0;
drha3460582008-07-11 21:02:53 +00004520 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004521 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004522#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004523 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004524#endif
drhf93cd942013-11-21 03:12:25 +00004525 }else{
4526 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004527 }
drhf0863fe2005-06-12 21:35:51 +00004528 pC->rowidIsValid = 0;
drh49afe3a2013-07-10 03:05:14 +00004529 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004530}
4531
danielk1977de630352009-05-04 11:42:29 +00004532/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004533** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004534**
drhef8662b2011-06-20 21:47:58 +00004535** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004536** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004537** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004538**
drhaa9b8962008-01-08 02:57:55 +00004539** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004540** insert is likely to be an append.
4541**
mistachkin21a919f2014-02-07 03:28:02 +00004542** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
4543** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear,
4544** then the change counter is unchanged.
drh0fd61352014-02-07 02:29:45 +00004545**
mistachkin21a919f2014-02-07 03:28:02 +00004546** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
4547** just done a seek to the spot where the new entry is to be inserted.
4548** This flag avoids doing an extra seek.
drh0fd61352014-02-07 02:29:45 +00004549**
drhf0863fe2005-06-12 21:35:51 +00004550** This instruction only works for indices. The equivalent instruction
4551** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004552*/
drhca892a72011-09-03 00:17:51 +00004553case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004554case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004555 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004556 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004557 int nKey;
4558 const char *zKey;
4559
drh653b82a2009-06-22 11:10:47 +00004560 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4561 pC = p->apCsr[pOp->p1];
4562 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004563 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004564 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004565 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004566 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004567 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh3da046d2013-11-11 03:24:11 +00004568 assert( pCrsr!=0 );
4569 assert( pC->isTable==0 );
4570 rc = ExpandBlob(pIn2);
4571 if( rc==SQLITE_OK ){
4572 if( isSorter(pC) ){
4573 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4574 }else{
4575 nKey = pIn2->n;
4576 zKey = pIn2->z;
4577 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4578 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4579 );
4580 assert( pC->deferredMoveto==0 );
4581 pC->cacheStatus = CACHE_STALE;
danielk1977d908f5a2007-05-11 07:08:28 +00004582 }
drh5e00f6c2001-09-13 13:46:56 +00004583 }
drh5e00f6c2001-09-13 13:46:56 +00004584 break;
4585}
4586
drh4308e342013-11-11 16:55:52 +00004587/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004588** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004589**
drhe14006d2008-03-25 17:23:32 +00004590** The content of P3 registers starting at register P2 form
4591** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004592** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004593*/
drhe14006d2008-03-25 17:23:32 +00004594case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004595 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004596 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004597 int res;
4598 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004599
drhe14006d2008-03-25 17:23:32 +00004600 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004601 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004602 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4603 pC = p->apCsr[pOp->p1];
4604 assert( pC!=0 );
4605 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004606 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004607 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004608 r.pKeyInfo = pC->pKeyInfo;
4609 r.nField = (u16)pOp->p3;
4610 r.flags = UNPACKED_PREFIX_MATCH;
4611 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004612#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004613 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004614#endif
drh3da046d2013-11-11 03:24:11 +00004615 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4616 if( rc==SQLITE_OK && res==0 ){
4617 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004618 }
drh3da046d2013-11-11 03:24:11 +00004619 assert( pC->deferredMoveto==0 );
4620 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004621 break;
4622}
4623
drh2133d822008-01-03 18:44:59 +00004624/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004625** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004626**
drh2133d822008-01-03 18:44:59 +00004627** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004628** the end of the index key pointed to by cursor P1. This integer should be
4629** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004630**
drh9437bd22009-02-01 00:29:56 +00004631** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004632*/
drh4c583122008-01-04 22:01:03 +00004633case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004634 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004635 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004636 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004637
drh653b82a2009-06-22 11:10:47 +00004638 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4639 pC = p->apCsr[pOp->p1];
4640 assert( pC!=0 );
4641 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004642 assert( pCrsr!=0 );
drh3c657212009-11-17 23:59:58 +00004643 pOut->flags = MEM_Null;
drh3da046d2013-11-11 03:24:11 +00004644 rc = sqlite3VdbeCursorMoveto(pC);
4645 if( NEVER(rc) ) goto abort_due_to_error;
4646 assert( pC->deferredMoveto==0 );
4647 assert( pC->isTable==0 );
4648 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00004649 rowid = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00004650 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
4651 if( rc!=SQLITE_OK ){
4652 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00004653 }
drh3da046d2013-11-11 03:24:11 +00004654 pOut->u.i = rowid;
4655 pOut->flags = MEM_Int;
drh8721ce42001-11-07 14:22:00 +00004656 }
4657 break;
4658}
4659
danielk197761dd5832008-04-18 11:31:12 +00004660/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004661** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004662**
danielk197761dd5832008-04-18 11:31:12 +00004663** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004664** key that omits the PRIMARY KEY. Compare this key value against the index
4665** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4666** fields at the end.
drhf3218fe2004-05-28 08:21:02 +00004667**
danielk197761dd5832008-04-18 11:31:12 +00004668** If the P1 index entry is greater than or equal to the key value
4669** then jump to P2. Otherwise fall through to the next instruction.
drh4a1d3652014-02-14 15:13:36 +00004670*/
4671/* Opcode: IdxGT P1 P2 P3 P4 P5
4672** Synopsis: key=r[P3@P4]
drh772ae622004-05-19 13:13:08 +00004673**
drh4a1d3652014-02-14 15:13:36 +00004674** The P4 register values beginning with P3 form an unpacked index
4675** key that omits the PRIMARY KEY. Compare this key value against the index
4676** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
4677** fields at the end.
4678**
4679** If the P1 index entry is greater than the key value
4680** then jump to P2. Otherwise fall through to the next instruction.
drh8721ce42001-11-07 14:22:00 +00004681*/
drh3bb9b932010-08-06 02:10:00 +00004682/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004683** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004684**
danielk197761dd5832008-04-18 11:31:12 +00004685** The P4 register values beginning with P3 form an unpacked index
drh4a1d3652014-02-14 15:13:36 +00004686** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4687** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4688** ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004689**
danielk197761dd5832008-04-18 11:31:12 +00004690** If the P1 index entry is less than the key value then jump to P2.
4691** Otherwise fall through to the next instruction.
drhc045ec52002-12-04 20:01:06 +00004692*/
drh4a1d3652014-02-14 15:13:36 +00004693/* Opcode: IdxLE P1 P2 P3 P4 P5
4694** Synopsis: key=r[P3@P4]
4695**
4696** The P4 register values beginning with P3 form an unpacked index
4697** key that omits the PRIMARY KEY or ROWID. Compare this key value against
4698** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
4699** ROWID on the P1 index.
4700**
4701** If the P1 index entry is less than or equal to the key value then jump
4702** to P2. Otherwise fall through to the next instruction.
4703*/
4704case OP_IdxLE: /* jump */
4705case OP_IdxGT: /* jump */
drh93952eb2009-11-13 19:43:43 +00004706case OP_IdxLT: /* jump */
drh4a1d3652014-02-14 15:13:36 +00004707case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004708 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004709 int res;
4710 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004711
drh653b82a2009-06-22 11:10:47 +00004712 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4713 pC = p->apCsr[pOp->p1];
4714 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004715 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00004716 assert( pC->pCursor!=0);
4717 assert( pC->deferredMoveto==0 );
4718 assert( pOp->p5==0 || pOp->p5==1 );
4719 assert( pOp->p4type==P4_INT32 );
4720 r.pKeyInfo = pC->pKeyInfo;
4721 r.nField = (u16)pOp->p4.i;
drh4a1d3652014-02-14 15:13:36 +00004722 if( pOp->opcode<OP_IdxLT ){
4723 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00004724 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
4725 }else{
drh4a1d3652014-02-14 15:13:36 +00004726 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00004727 r.flags = UNPACKED_PREFIX_MATCH;
4728 }
4729 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004730#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004731 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004732#endif
drh2dc06482013-12-11 00:59:10 +00004733 res = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00004734 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drh4a1d3652014-02-14 15:13:36 +00004735 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
4736 if( (pOp->opcode&1)==(OP_IdxLT&1) ){
4737 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
drh3da046d2013-11-11 03:24:11 +00004738 res = -res;
4739 }else{
drh4a1d3652014-02-14 15:13:36 +00004740 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
drh3da046d2013-11-11 03:24:11 +00004741 res++;
4742 }
drh688852a2014-02-17 22:40:43 +00004743 VdbeBranchTaken(res>0,2);
drh3da046d2013-11-11 03:24:11 +00004744 if( res>0 ){
4745 pc = pOp->p2 - 1 ;
drh8721ce42001-11-07 14:22:00 +00004746 }
4747 break;
4748}
4749
drh98757152008-01-09 23:04:12 +00004750/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004751**
4752** Delete an entire database table or index whose root page in the database
4753** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004754**
drh98757152008-01-09 23:04:12 +00004755** The table being destroyed is in the main database file if P3==0. If
4756** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004757** that is used to store tables create using CREATE TEMPORARY TABLE.
4758**
drh205f48e2004-11-05 00:43:11 +00004759** If AUTOVACUUM is enabled then it is possible that another root page
4760** might be moved into the newly deleted root page in order to keep all
4761** root pages contiguous at the beginning of the database. The former
4762** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004763** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004764** movement was required (because the table being dropped was already
4765** the last one in the database) then a zero is stored in register P2.
4766** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004767**
drhb19a2bc2001-09-16 00:13:26 +00004768** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004769*/
drh98757152008-01-09 23:04:12 +00004770case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004771 int iMoved;
drh3765df42006-06-28 18:18:09 +00004772 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004773 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004774 int iDb;
drh3a949872012-09-18 13:20:13 +00004775
drh9e92a472013-06-27 17:40:30 +00004776 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004777#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004778 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004779 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004780 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4781 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4782 ){
danielk1977212b2182006-06-23 14:32:08 +00004783 iCnt++;
4784 }
4785 }
drh3765df42006-06-28 18:18:09 +00004786#else
danc0537fe2013-06-28 19:41:43 +00004787 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004788#endif
drh3c657212009-11-17 23:59:58 +00004789 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004790 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004791 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004792 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004793 }else{
drh856c1032009-06-02 15:21:42 +00004794 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004795 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004796 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh2dc06482013-12-11 00:59:10 +00004797 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00004798 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004799 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004800 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004801#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004802 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004803 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4804 /* All OP_Destroy operations occur on the same btree */
4805 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4806 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004807 }
drh3765df42006-06-28 18:18:09 +00004808#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004809 }
drh5e00f6c2001-09-13 13:46:56 +00004810 break;
4811}
4812
danielk1977c7af4842008-10-27 13:59:33 +00004813/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004814**
4815** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004816** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004817** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004818**
drhf57b3392001-10-08 13:22:32 +00004819** The table being clear is in the main database file if P2==0. If
4820** P2==1 then the table to be clear is in the auxiliary database file
4821** that is used to store tables create using CREATE TEMPORARY TABLE.
4822**
shanebe217792009-03-05 04:20:31 +00004823** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004824** intkey table (an SQL table, not an index). In this case the row change
4825** count is incremented by the number of rows in the table being cleared.
4826** If P3 is greater than zero, then the value stored in register P3 is
4827** also incremented by the number of rows in the table being cleared.
4828**
drhb19a2bc2001-09-16 00:13:26 +00004829** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004830*/
drh9cbf3422008-01-17 16:22:13 +00004831case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004832 int nChange;
4833
4834 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004835 assert( p->readOnly==0 );
danf52bb8d2013-08-03 20:24:58 +00004836 assert( pOp->p1!=1 );
drhdddd7792011-04-03 18:19:25 +00004837 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004838 rc = sqlite3BtreeClearTable(
4839 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4840 );
4841 if( pOp->p3 ){
4842 p->nChange += nChange;
4843 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004844 assert( memIsValid(&aMem[pOp->p3]) );
4845 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004846 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004847 }
4848 }
drh5edc3122001-09-13 21:53:09 +00004849 break;
4850}
4851
drh4c583122008-01-04 22:01:03 +00004852/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004853** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00004854**
drh4c583122008-01-04 22:01:03 +00004855** Allocate a new table in the main database file if P1==0 or in the
4856** auxiliary database file if P1==1 or in an attached database if
4857** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004858** register P2
drh5b2fd562001-09-13 15:21:31 +00004859**
drhc6b52df2002-01-04 03:09:29 +00004860** The difference between a table and an index is this: A table must
4861** have a 4-byte integer key and can have arbitrary data. An index
4862** has an arbitrary key but no data.
4863**
drhb19a2bc2001-09-16 00:13:26 +00004864** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004865*/
drh4c583122008-01-04 22:01:03 +00004866/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004867** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00004868**
drh4c583122008-01-04 22:01:03 +00004869** Allocate a new index in the main database file if P1==0 or in the
4870** auxiliary database file if P1==1 or in an attached database if
4871** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004872** register P2.
drhf57b3392001-10-08 13:22:32 +00004873**
drhc6b52df2002-01-04 03:09:29 +00004874** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004875*/
drh4c583122008-01-04 22:01:03 +00004876case OP_CreateIndex: /* out2-prerelease */
4877case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004878 int pgno;
drhf328bc82004-05-10 23:29:49 +00004879 int flags;
drh234c39d2004-07-24 03:30:47 +00004880 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004881
4882 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004883 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004884 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00004885 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00004886 pDb = &db->aDb[pOp->p1];
4887 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004888 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004889 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004890 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004891 }else{
drhd4187c72010-08-30 22:15:45 +00004892 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004893 }
drh234c39d2004-07-24 03:30:47 +00004894 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004895 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004896 break;
4897}
4898
drh22645842011-03-24 01:34:03 +00004899/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004900**
4901** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004902** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004903**
4904** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004905** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004906*/
drh9cbf3422008-01-17 16:22:13 +00004907case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004908 int iDb;
4909 const char *zMaster;
4910 char *zSql;
4911 InitData initData;
4912
drhbdaec522011-04-04 00:14:43 +00004913 /* Any prepared statement that invokes this opcode will hold mutexes
4914 ** on every btree. This is a prerequisite for invoking
4915 ** sqlite3InitCallback().
4916 */
4917#ifdef SQLITE_DEBUG
4918 for(iDb=0; iDb<db->nDb; iDb++){
4919 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4920 }
4921#endif
drhbdaec522011-04-04 00:14:43 +00004922
drh856c1032009-06-02 15:21:42 +00004923 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004924 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004925 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004926 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004927 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004928 initData.db = db;
4929 initData.iDb = pOp->p1;
4930 initData.pzErrMsg = &p->zErrMsg;
4931 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004932 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004933 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4934 if( zSql==0 ){
4935 rc = SQLITE_NOMEM;
4936 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004937 assert( db->init.busy==0 );
4938 db->init.busy = 1;
4939 initData.rc = SQLITE_OK;
4940 assert( !db->mallocFailed );
4941 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4942 if( rc==SQLITE_OK ) rc = initData.rc;
4943 sqlite3DbFree(db, zSql);
4944 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004945 }
drh3c23a882007-01-09 14:01:13 +00004946 }
drh81028a42012-05-15 18:28:27 +00004947 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00004948 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004949 goto no_mem;
4950 }
drh234c39d2004-07-24 03:30:47 +00004951 break;
4952}
4953
drh8bfdf722009-06-19 14:06:03 +00004954#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004955/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004956**
4957** Read the sqlite_stat1 table for database P1 and load the content
4958** of that table into the internal index hash table. This will cause
4959** the analysis to be used when preparing all subsequent queries.
4960*/
drh9cbf3422008-01-17 16:22:13 +00004961case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004962 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4963 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004964 break;
4965}
drh8bfdf722009-06-19 14:06:03 +00004966#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004967
drh98757152008-01-09 23:04:12 +00004968/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004969**
4970** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004971** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004972** is dropped in order to keep the internal representation of the
4973** schema consistent with what is on disk.
4974*/
drh9cbf3422008-01-17 16:22:13 +00004975case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004976 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004977 break;
4978}
4979
drh98757152008-01-09 23:04:12 +00004980/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004981**
4982** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004983** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004984** is dropped in order to keep the internal representation of the
4985** schema consistent with what is on disk.
4986*/
drh9cbf3422008-01-17 16:22:13 +00004987case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004988 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004989 break;
4990}
4991
drh98757152008-01-09 23:04:12 +00004992/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004993**
4994** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004995** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004996** is dropped in order to keep the internal representation of the
4997** schema consistent with what is on disk.
4998*/
drh9cbf3422008-01-17 16:22:13 +00004999case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005000 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005001 break;
5002}
5003
drh234c39d2004-07-24 03:30:47 +00005004
drhb7f91642004-10-31 02:22:47 +00005005#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005006/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005007**
drh98757152008-01-09 23:04:12 +00005008** Do an analysis of the currently open database. Store in
5009** register P1 the text of an error message describing any problems.
5010** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005011**
drh98757152008-01-09 23:04:12 +00005012** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005013** At most reg(P3) errors will be reported.
5014** In other words, the analysis stops as soon as reg(P1) errors are
5015** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005016**
drh79069752004-05-22 21:30:40 +00005017** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005018** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005019** total.
drh21504322002-06-25 13:16:02 +00005020**
drh98757152008-01-09 23:04:12 +00005021** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005022** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005023**
drh1dcdbc02007-01-27 02:24:54 +00005024** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005025*/
drhaaab5722002-02-19 13:39:21 +00005026case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005027 int nRoot; /* Number of tables to check. (Number of root pages.) */
5028 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5029 int j; /* Loop counter */
5030 int nErr; /* Number of errors reported */
5031 char *z; /* Text of the error report */
5032 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005033
drh1713afb2013-06-28 01:24:57 +00005034 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005035 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005036 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005037 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005038 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005039 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005040 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005041 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005042 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005043 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005044 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005045 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005046 }
5047 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005048 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005049 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00005050 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005051 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005052 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005053 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005054 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005055 if( nErr==0 ){
5056 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005057 }else if( z==0 ){
5058 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005059 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005060 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005061 }
drhb7654112008-01-12 12:48:07 +00005062 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005063 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005064 break;
5065}
drhb7f91642004-10-31 02:22:47 +00005066#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005067
drh3d4501e2008-12-04 20:40:10 +00005068/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005069** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005070**
drh3d4501e2008-12-04 20:40:10 +00005071** Insert the integer value held by register P2 into a boolean index
5072** held in register P1.
5073**
5074** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005075*/
drh93952eb2009-11-13 19:43:43 +00005076case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005077 pIn1 = &aMem[pOp->p1];
5078 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005079 assert( (pIn2->flags & MEM_Int)!=0 );
5080 if( (pIn1->flags & MEM_RowSet)==0 ){
5081 sqlite3VdbeMemSetRowSet(pIn1);
5082 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005083 }
drh93952eb2009-11-13 19:43:43 +00005084 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005085 break;
5086}
5087
5088/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005089** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005090**
5091** Extract the smallest value from boolean index P1 and put that value into
5092** register P3. Or, if boolean index P1 is initially empty, leave P3
5093** unchanged and jump to instruction P2.
5094*/
drh93952eb2009-11-13 19:43:43 +00005095case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005096 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005097
drh3c657212009-11-17 23:59:58 +00005098 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005099 if( (pIn1->flags & MEM_RowSet)==0
5100 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005101 ){
5102 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005103 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005104 pc = pOp->p2 - 1;
drh688852a2014-02-17 22:40:43 +00005105 VdbeBranchTaken(1,2);
drh3d4501e2008-12-04 20:40:10 +00005106 }else{
5107 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005108 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh688852a2014-02-17 22:40:43 +00005109 VdbeBranchTaken(0,2);
drh17435752007-08-16 04:30:38 +00005110 }
drh49afe3a2013-07-10 03:05:14 +00005111 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005112}
5113
drh1b26c7c2009-04-22 02:15:47 +00005114/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005115** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005116**
drhade97602009-04-21 15:05:18 +00005117** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005118** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005119** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005120** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005121** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005122**
drh1b26c7c2009-04-22 02:15:47 +00005123** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005124** of integers, where each set contains no duplicates. Each set
5125** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005126** must have P4==0, the final set P4=-1. P4 must be either -1 or
5127** non-negative. For non-negative values of P4 only the lower 4
5128** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005129**
5130** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005131** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005132** (b) when P4==-1 there is no need to insert the value, as it will
5133** never be tested for, and (c) when a value that is part of set X is
5134** inserted, there is no need to search to see if the same value was
5135** previously inserted as part of set X (only if it was previously
5136** inserted as part of some other set).
5137*/
drh1b26c7c2009-04-22 02:15:47 +00005138case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005139 int iSet;
5140 int exists;
5141
drh3c657212009-11-17 23:59:58 +00005142 pIn1 = &aMem[pOp->p1];
5143 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005144 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005145 assert( pIn3->flags&MEM_Int );
5146
drh1b26c7c2009-04-22 02:15:47 +00005147 /* If there is anything other than a rowset object in memory cell P1,
5148 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005149 */
drh733bf1b2009-04-22 00:47:00 +00005150 if( (pIn1->flags & MEM_RowSet)==0 ){
5151 sqlite3VdbeMemSetRowSet(pIn1);
5152 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005153 }
5154
5155 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005156 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005157 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005158 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5159 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005160 pIn3->u.i);
drh688852a2014-02-17 22:40:43 +00005161 VdbeBranchTaken(exists!=0,2);
danielk19771d461462009-04-21 09:02:45 +00005162 if( exists ){
5163 pc = pOp->p2 - 1;
5164 break;
5165 }
5166 }
5167 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005168 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005169 }
5170 break;
5171}
5172
drh5e00f6c2001-09-13 13:46:56 +00005173
danielk197793758c82005-01-21 08:13:14 +00005174#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005175
drh0fd61352014-02-07 02:29:45 +00005176/* Opcode: Program P1 P2 P3 P4 P5
dan165921a2009-08-28 18:53:45 +00005177**
dan76d462e2009-08-30 11:42:51 +00005178** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005179**
dan76d462e2009-08-30 11:42:51 +00005180** P1 contains the address of the memory cell that contains the first memory
5181** cell in an array of values used as arguments to the sub-program. P2
5182** contains the address to jump to if the sub-program throws an IGNORE
5183** exception using the RAISE() function. Register P3 contains the address
5184** of a memory cell in this (the parent) VM that is used to allocate the
5185** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005186**
5187** P4 is a pointer to the VM containing the trigger program.
drh0fd61352014-02-07 02:29:45 +00005188**
5189** If P5 is non-zero, then recursive program invocation is enabled.
dan165921a2009-08-28 18:53:45 +00005190*/
dan76d462e2009-08-30 11:42:51 +00005191case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005192 int nMem; /* Number of memory registers for sub-program */
5193 int nByte; /* Bytes of runtime space required for sub-program */
5194 Mem *pRt; /* Register to allocate runtime space */
5195 Mem *pMem; /* Used to iterate through memory cells */
5196 Mem *pEnd; /* Last memory cell in new array */
5197 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5198 SubProgram *pProgram; /* Sub-program to execute */
5199 void *t; /* Token identifying trigger */
5200
5201 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005202 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005203 assert( pProgram->nOp>0 );
5204
dan1da40a32009-09-19 17:00:31 +00005205 /* If the p5 flag is clear, then recursive invocation of triggers is
5206 ** disabled for backwards compatibility (p5 is set if this sub-program
5207 ** is really a trigger, not a foreign key action, and the flag set
5208 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005209 **
5210 ** It is recursive invocation of triggers, at the SQL level, that is
5211 ** disabled. In some cases a single trigger may generate more than one
5212 ** SubProgram (if the trigger may be executed with more than one different
5213 ** ON CONFLICT algorithm). SubProgram structures associated with a
5214 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005215 ** variable. */
5216 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005217 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005218 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5219 if( pFrame ) break;
5220 }
5221
danf5894502009-10-07 18:41:19 +00005222 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005223 rc = SQLITE_ERROR;
5224 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5225 break;
5226 }
5227
5228 /* Register pRt is used to store the memory required to save the state
5229 ** of the current program, and the memory required at runtime to execute
5230 ** the trigger program. If this trigger has been fired before, then pRt
5231 ** is already allocated. Otherwise, it must be initialized. */
5232 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005233 /* SubProgram.nMem is set to the number of memory cells used by the
5234 ** program stored in SubProgram.aOp. As well as these, one memory
5235 ** cell is required for each cursor used by the program. Set local
5236 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5237 */
dan65a7cd12009-09-01 12:16:01 +00005238 nMem = pProgram->nMem + pProgram->nCsr;
5239 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005240 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005241 + pProgram->nCsr * sizeof(VdbeCursor *)
5242 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005243 pFrame = sqlite3DbMallocZero(db, nByte);
5244 if( !pFrame ){
5245 goto no_mem;
5246 }
5247 sqlite3VdbeMemRelease(pRt);
5248 pRt->flags = MEM_Frame;
5249 pRt->u.pFrame = pFrame;
5250
5251 pFrame->v = p;
5252 pFrame->nChildMem = nMem;
5253 pFrame->nChildCsr = pProgram->nCsr;
5254 pFrame->pc = pc;
5255 pFrame->aMem = p->aMem;
5256 pFrame->nMem = p->nMem;
5257 pFrame->apCsr = p->apCsr;
5258 pFrame->nCursor = p->nCursor;
5259 pFrame->aOp = p->aOp;
5260 pFrame->nOp = p->nOp;
5261 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005262 pFrame->aOnceFlag = p->aOnceFlag;
5263 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005264
5265 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5266 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drha5750cf2014-02-07 13:20:31 +00005267 pMem->flags = MEM_Undefined;
dan165921a2009-08-28 18:53:45 +00005268 pMem->db = db;
5269 }
5270 }else{
5271 pFrame = pRt->u.pFrame;
5272 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5273 assert( pProgram->nCsr==pFrame->nChildCsr );
5274 assert( pc==pFrame->pc );
5275 }
5276
5277 p->nFrame++;
5278 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005279 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005280 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005281 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005282 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005283 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005284 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005285 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005286 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005287 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005288 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005289 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5290 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005291 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005292 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005293
5294 break;
5295}
5296
dan76d462e2009-08-30 11:42:51 +00005297/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005298**
dan76d462e2009-08-30 11:42:51 +00005299** This opcode is only ever present in sub-programs called via the
5300** OP_Program instruction. Copy a value currently stored in a memory
5301** cell of the calling (parent) frame to cell P2 in the current frames
5302** address space. This is used by trigger programs to access the new.*
5303** and old.* values.
dan165921a2009-08-28 18:53:45 +00005304**
dan76d462e2009-08-30 11:42:51 +00005305** The address of the cell in the parent frame is determined by adding
5306** the value of the P1 argument to the value of the P1 argument to the
5307** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005308*/
dan76d462e2009-08-30 11:42:51 +00005309case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005310 VdbeFrame *pFrame;
5311 Mem *pIn;
5312 pFrame = p->pFrame;
5313 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005314 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5315 break;
5316}
5317
danielk197793758c82005-01-21 08:13:14 +00005318#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005319
dan1da40a32009-09-19 17:00:31 +00005320#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005321/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005322** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005323**
dan0ff297e2009-09-25 17:03:14 +00005324** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5325** If P1 is non-zero, the database constraint counter is incremented
5326** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005327** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005328*/
dan32b09f22009-09-23 17:29:59 +00005329case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005330 if( db->flags & SQLITE_DeferFKs ){
5331 db->nDeferredImmCons += pOp->p2;
5332 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005333 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005334 }else{
dan0ff297e2009-09-25 17:03:14 +00005335 p->nFkConstraint += pOp->p2;
5336 }
5337 break;
5338}
5339
5340/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005341** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005342**
5343** This opcode tests if a foreign key constraint-counter is currently zero.
5344** If so, jump to instruction P2. Otherwise, fall through to the next
5345** instruction.
5346**
5347** If P1 is non-zero, then the jump is taken if the database constraint-counter
5348** is zero (the one that counts deferred constraint violations). If P1 is
5349** zero, the jump is taken if the statement constraint-counter is zero
5350** (immediate foreign key constraint violations).
5351*/
5352case OP_FkIfZero: { /* jump */
5353 if( pOp->p1 ){
drh688852a2014-02-17 22:40:43 +00005354 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005355 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005356 }else{
drh688852a2014-02-17 22:40:43 +00005357 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
drh648e2642013-07-11 15:03:32 +00005358 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005359 }
dan1da40a32009-09-19 17:00:31 +00005360 break;
5361}
5362#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5363
drh205f48e2004-11-05 00:43:11 +00005364#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005365/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005366** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005367**
dan76d462e2009-08-30 11:42:51 +00005368** P1 is a register in the root frame of this VM (the root frame is
5369** different from the current frame if this instruction is being executed
5370** within a sub-program). Set the value of register P1 to the maximum of
5371** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005372**
5373** This instruction throws an error if the memory cell is not initially
5374** an integer.
5375*/
dan76d462e2009-08-30 11:42:51 +00005376case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005377 VdbeFrame *pFrame;
5378 if( p->pFrame ){
5379 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5380 pIn1 = &pFrame->aMem[pOp->p1];
5381 }else{
drha6c2ed92009-11-14 23:22:23 +00005382 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005383 }
drhec86c722011-12-09 17:27:51 +00005384 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005385 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005386 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005387 sqlite3VdbeMemIntegerify(pIn2);
5388 if( pIn1->u.i<pIn2->u.i){
5389 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005390 }
5391 break;
5392}
5393#endif /* SQLITE_OMIT_AUTOINCREMENT */
5394
drh98757152008-01-09 23:04:12 +00005395/* Opcode: IfPos P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005396** Synopsis: if r[P1]>0 goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005397**
drh98757152008-01-09 23:04:12 +00005398** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005399**
drh98757152008-01-09 23:04:12 +00005400** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005401** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005402*/
drh9cbf3422008-01-17 16:22:13 +00005403case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005404 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005405 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005406 VdbeBranchTaken( pIn1->u.i>0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005407 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005408 pc = pOp->p2 - 1;
5409 }
5410 break;
5411}
5412
drh98757152008-01-09 23:04:12 +00005413/* Opcode: IfNeg P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005414** Synopsis: if r[P1]<0 goto P2
drh15007a92006-01-08 18:10:17 +00005415**
drh98757152008-01-09 23:04:12 +00005416** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005417**
drh98757152008-01-09 23:04:12 +00005418** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005419** not contain an integer. An assertion fault will result if you try.
5420*/
drh9cbf3422008-01-17 16:22:13 +00005421case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005422 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005423 assert( pIn1->flags&MEM_Int );
drh688852a2014-02-17 22:40:43 +00005424 VdbeBranchTaken(pIn1->u.i<0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005425 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005426 pc = pOp->p2 - 1;
5427 }
5428 break;
5429}
5430
drh9b918ed2009-11-12 03:13:26 +00005431/* Opcode: IfZero P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005432** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
drhec7429a2005-10-06 16:53:14 +00005433**
drh9b918ed2009-11-12 03:13:26 +00005434** The register P1 must contain an integer. Add literal P3 to the
5435** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005436**
drh98757152008-01-09 23:04:12 +00005437** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005438** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005439*/
drh9cbf3422008-01-17 16:22:13 +00005440case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005441 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005442 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005443 pIn1->u.i += pOp->p3;
drh688852a2014-02-17 22:40:43 +00005444 VdbeBranchTaken(pIn1->u.i==0, 2);
drh3c84ddf2008-01-09 02:15:38 +00005445 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005446 pc = pOp->p2 - 1;
5447 }
5448 break;
5449}
5450
drh98757152008-01-09 23:04:12 +00005451/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005452** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005453**
drh0bce8352002-02-28 00:41:10 +00005454** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005455** function has P5 arguments. P4 is a pointer to the FuncDef
5456** structure that specifies the function. Use register
5457** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005458**
drh98757152008-01-09 23:04:12 +00005459** The P5 arguments are taken from register P2 and its
5460** successors.
drhe5095352002-02-24 03:25:14 +00005461*/
drh9cbf3422008-01-17 16:22:13 +00005462case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005463 int n;
drhe5095352002-02-24 03:25:14 +00005464 int i;
drhc54a6172009-06-02 16:06:03 +00005465 Mem *pMem;
5466 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005467 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005468 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005469
drh856c1032009-06-02 15:21:42 +00005470 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005471 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005472 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005473 apVal = p->apArg;
5474 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005475 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005476 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005477 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005478 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005479 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005480 }
danielk19772dca4ac2008-01-03 11:50:29 +00005481 ctx.pFunc = pOp->p4.pFunc;
dan3bc9f742013-08-15 16:18:39 +00005482 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005483 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005484 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005485 ctx.s.flags = MEM_Null;
5486 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005487 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005488 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005489 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005490 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005491 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005492 ctx.skipFlag = 0;
drhd36e1042013-09-06 13:10:12 +00005493 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005494 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005495 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005496 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005497 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005498 }
drhee9ff672010-09-03 18:50:48 +00005499 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005500 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005501 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005502 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005503 }
drh7a957892012-02-02 17:35:43 +00005504 if( ctx.skipFlag ){
5505 assert( pOp[-1].opcode==OP_CollSeq );
5506 i = pOp[-1].p1;
5507 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5508 }
drhbdaec522011-04-04 00:14:43 +00005509
drh90669c12006-01-20 15:45:36 +00005510 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005511
drh5e00f6c2001-09-13 13:46:56 +00005512 break;
5513}
5514
drh98757152008-01-09 23:04:12 +00005515/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005516** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005517**
drh13449892005-09-07 21:22:45 +00005518** Execute the finalizer function for an aggregate. P1 is
5519** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005520**
5521** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005522** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005523** argument is not used by this opcode. It is only there to disambiguate
5524** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005525** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005526** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005527*/
drh9cbf3422008-01-17 16:22:13 +00005528case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005529 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005530 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005531 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005532 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005533 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005534 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005535 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005536 }
drh2dca8682008-03-21 17:13:13 +00005537 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005538 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005539 if( sqlite3VdbeMemTooBig(pMem) ){
5540 goto too_big;
5541 }
drh5e00f6c2001-09-13 13:46:56 +00005542 break;
5543}
5544
dan5cf53532010-05-01 16:40:20 +00005545#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005546/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005547**
5548** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005549** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005550** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5551** SQLITE_BUSY or not, respectively. Write the number of pages in the
5552** WAL after the checkpoint into mem[P3+1] and the number of pages
5553** in the WAL that have been checkpointed after the checkpoint
5554** completes into mem[P3+2]. However on an error, mem[P3+1] and
5555** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005556*/
5557case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005558 int i; /* Loop counter */
5559 int aRes[3]; /* Results */
5560 Mem *pMem; /* Write results here */
5561
drh9e92a472013-06-27 17:40:30 +00005562 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005563 aRes[0] = 0;
5564 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005565 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5566 || pOp->p2==SQLITE_CHECKPOINT_FULL
5567 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5568 );
drh30aa3b92011-02-07 23:56:01 +00005569 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005570 if( rc==SQLITE_BUSY ){
5571 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005572 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005573 }
drh30aa3b92011-02-07 23:56:01 +00005574 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5575 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5576 }
dan7c246102010-04-12 19:00:29 +00005577 break;
5578};
dan5cf53532010-05-01 16:40:20 +00005579#endif
drh5e00f6c2001-09-13 13:46:56 +00005580
drhcac29a62010-07-02 19:36:52 +00005581#ifndef SQLITE_OMIT_PRAGMA
drh0fd61352014-02-07 02:29:45 +00005582/* Opcode: JournalMode P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005583**
5584** Change the journal mode of database P1 to P3. P3 must be one of the
5585** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5586** modes (delete, truncate, persist, off and memory), this is a simple
5587** operation. No IO is required.
5588**
5589** If changing into or out of WAL mode the procedure is more complicated.
5590**
5591** Write a string containing the final journal-mode to register P2.
5592*/
drhd80b2332010-05-01 00:59:37 +00005593case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005594 Btree *pBt; /* Btree to change journal mode of */
5595 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005596 int eNew; /* New journal mode */
5597 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005598#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005599 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005600#endif
dane04dc882010-04-20 18:53:15 +00005601
drhd80b2332010-05-01 00:59:37 +00005602 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005603 assert( eNew==PAGER_JOURNALMODE_DELETE
5604 || eNew==PAGER_JOURNALMODE_TRUNCATE
5605 || eNew==PAGER_JOURNALMODE_PERSIST
5606 || eNew==PAGER_JOURNALMODE_OFF
5607 || eNew==PAGER_JOURNALMODE_MEMORY
5608 || eNew==PAGER_JOURNALMODE_WAL
5609 || eNew==PAGER_JOURNALMODE_QUERY
5610 );
5611 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005612 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005613
dane04dc882010-04-20 18:53:15 +00005614 pBt = db->aDb[pOp->p1].pBt;
5615 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005616 eOld = sqlite3PagerGetJournalMode(pPager);
5617 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5618 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005619
5620#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005621 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005622
drhd80b2332010-05-01 00:59:37 +00005623 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005624 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005625 */
5626 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005627 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005628 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005629 ){
drh0b9b4302010-06-11 17:01:24 +00005630 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005631 }
5632
drh0b9b4302010-06-11 17:01:24 +00005633 if( (eNew!=eOld)
5634 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5635 ){
danc0537fe2013-06-28 19:41:43 +00005636 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005637 rc = SQLITE_ERROR;
5638 sqlite3SetString(&p->zErrMsg, db,
5639 "cannot change %s wal mode from within a transaction",
5640 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5641 );
5642 break;
5643 }else{
5644
5645 if( eOld==PAGER_JOURNALMODE_WAL ){
5646 /* If leaving WAL mode, close the log file. If successful, the call
5647 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5648 ** file. An EXCLUSIVE lock may still be held on the database file
5649 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005650 */
drh0b9b4302010-06-11 17:01:24 +00005651 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005652 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005653 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005654 }
drh242c4f72010-06-22 14:49:39 +00005655 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5656 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5657 ** as an intermediate */
5658 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005659 }
5660
5661 /* Open a transaction on the database file. Regardless of the journal
5662 ** mode, this transaction always uses a rollback journal.
5663 */
5664 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5665 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005666 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005667 }
5668 }
5669 }
dan5cf53532010-05-01 16:40:20 +00005670#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005671
dand956efe2010-06-18 16:13:45 +00005672 if( rc ){
dand956efe2010-06-18 16:13:45 +00005673 eNew = eOld;
5674 }
drh0b9b4302010-06-11 17:01:24 +00005675 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005676
dane04dc882010-04-20 18:53:15 +00005677 pOut = &aMem[pOp->p2];
5678 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005679 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005680 pOut->n = sqlite3Strlen30(pOut->z);
5681 pOut->enc = SQLITE_UTF8;
5682 sqlite3VdbeChangeEncoding(pOut, encoding);
5683 break;
drhcac29a62010-07-02 19:36:52 +00005684};
5685#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005686
drhfdbcdee2007-03-27 14:44:50 +00005687#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005688/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005689**
5690** Vacuum the entire database. This opcode will cause other virtual
5691** machines to be created and run. It may not be called from within
5692** a transaction.
5693*/
drh9cbf3422008-01-17 16:22:13 +00005694case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005695 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005696 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005697 break;
5698}
drh154d4b22006-09-21 11:02:16 +00005699#endif
drh6f8c91c2003-12-07 00:24:35 +00005700
danielk1977dddbcdc2007-04-26 14:42:34 +00005701#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005702/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005703**
5704** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005705** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005706** P2. Otherwise, fall through to the next instruction.
5707*/
drh9cbf3422008-01-17 16:22:13 +00005708case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005709 Btree *pBt;
5710
5711 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005712 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00005713 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005714 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005715 rc = sqlite3BtreeIncrVacuum(pBt);
drh688852a2014-02-17 22:40:43 +00005716 VdbeBranchTaken(rc==SQLITE_DONE,2);
danielk1977dddbcdc2007-04-26 14:42:34 +00005717 if( rc==SQLITE_DONE ){
5718 pc = pOp->p2 - 1;
5719 rc = SQLITE_OK;
5720 }
5721 break;
5722}
5723#endif
5724
drh98757152008-01-09 23:04:12 +00005725/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005726**
5727** Cause precompiled statements to become expired. An expired statement
5728** fails with an error code of SQLITE_SCHEMA if it is ever executed
5729** (via sqlite3_step()).
5730**
5731** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5732** then only the currently executing statement is affected.
5733*/
drh9cbf3422008-01-17 16:22:13 +00005734case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005735 if( !pOp->p1 ){
5736 sqlite3ExpirePreparedStatements(db);
5737 }else{
5738 p->expired = 1;
5739 }
5740 break;
5741}
5742
danielk1977c00da102006-01-07 13:21:04 +00005743#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005744/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005745** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00005746**
5747** Obtain a lock on a particular table. This instruction is only used when
5748** the shared-cache feature is enabled.
5749**
danielk197796d48e92009-06-29 06:00:37 +00005750** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005751** on which the lock is acquired. A readlock is obtained if P3==0 or
5752** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005753**
5754** P2 contains the root-page of the table to lock.
5755**
drh66a51672008-01-03 00:01:23 +00005756** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005757** used to generate an error message if the lock cannot be obtained.
5758*/
drh9cbf3422008-01-17 16:22:13 +00005759case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005760 u8 isWriteLock = (u8)pOp->p3;
5761 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5762 int p1 = pOp->p1;
5763 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005764 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005765 assert( isWriteLock==0 || isWriteLock==1 );
5766 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5767 if( (rc&0xFF)==SQLITE_LOCKED ){
5768 const char *z = pOp->p4.z;
5769 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5770 }
danielk1977c00da102006-01-07 13:21:04 +00005771 }
5772 break;
5773}
drhb9bb7c12006-06-11 23:41:55 +00005774#endif /* SQLITE_OMIT_SHARED_CACHE */
5775
5776#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005777/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005778**
danielk19773e3a84d2008-08-01 17:37:40 +00005779** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5780** xBegin method for that table.
5781**
5782** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005783** within a callback to a virtual table xSync() method. If it is, the error
5784** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005785*/
drh9cbf3422008-01-17 16:22:13 +00005786case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005787 VTable *pVTab;
5788 pVTab = pOp->p4.pVtab;
5789 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00005790 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005791 break;
5792}
5793#endif /* SQLITE_OMIT_VIRTUALTABLE */
5794
5795#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005796/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005797**
drh66a51672008-01-03 00:01:23 +00005798** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005799** for that table.
5800*/
drh9cbf3422008-01-17 16:22:13 +00005801case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005802 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005803 break;
5804}
5805#endif /* SQLITE_OMIT_VIRTUALTABLE */
5806
5807#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005808/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005809**
drh66a51672008-01-03 00:01:23 +00005810** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005811** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005812*/
drh9cbf3422008-01-17 16:22:13 +00005813case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005814 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005815 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005816 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005817 break;
5818}
5819#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005820
drh9eff6162006-06-12 21:59:13 +00005821#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005822/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005823**
drh66a51672008-01-03 00:01:23 +00005824** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005825** P1 is a cursor number. This opcode opens a cursor to the virtual
5826** table and stores that cursor in P1.
5827*/
drh9cbf3422008-01-17 16:22:13 +00005828case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005829 VdbeCursor *pCur;
5830 sqlite3_vtab_cursor *pVtabCursor;
5831 sqlite3_vtab *pVtab;
5832 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005833
drh1713afb2013-06-28 01:24:57 +00005834 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005835 pCur = 0;
5836 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005837 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005838 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005839 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005840 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00005841 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005842 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005843 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005844 pVtabCursor->pVtab = pVtab;
5845
mistachkin48864df2013-03-21 21:20:32 +00005846 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005847 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005848 if( pCur ){
5849 pCur->pVtabCursor = pVtabCursor;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005850 }else{
drh17435752007-08-16 04:30:38 +00005851 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005852 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005853 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005854 }
drh9eff6162006-06-12 21:59:13 +00005855 break;
5856}
5857#endif /* SQLITE_OMIT_VIRTUALTABLE */
5858
5859#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005860/* Opcode: VFilter P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005861** Synopsis: iPlan=r[P3] zPlan='P4'
drh9eff6162006-06-12 21:59:13 +00005862**
5863** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5864** the filtered result set is empty.
5865**
drh66a51672008-01-03 00:01:23 +00005866** P4 is either NULL or a string that was generated by the xBestIndex
5867** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005868** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005869**
drh9eff6162006-06-12 21:59:13 +00005870** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005871** by P1. The integer query plan parameter to xFilter is stored in register
5872** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005873** xFilter method. Registers P3+2..P3+1+argc are the argc
5874** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005875** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005876**
danielk19776dbee812008-01-03 18:39:41 +00005877** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005878*/
drh9cbf3422008-01-17 16:22:13 +00005879case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005880 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005881 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005882 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005883 Mem *pQuery;
5884 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005885 sqlite3_vtab_cursor *pVtabCursor;
5886 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005887 VdbeCursor *pCur;
5888 int res;
5889 int i;
5890 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005891
drha6c2ed92009-11-14 23:22:23 +00005892 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005893 pArgc = &pQuery[1];
5894 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005895 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005896 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005897 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005898 pVtabCursor = pCur->pVtabCursor;
5899 pVtab = pVtabCursor->pVtab;
5900 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005901
drh9cbf3422008-01-17 16:22:13 +00005902 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005903 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005904 nArg = (int)pArgc->u.i;
5905 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005906
drh644a5292006-12-20 14:53:38 +00005907 /* Invoke the xFilter method */
5908 {
drh856c1032009-06-02 15:21:42 +00005909 res = 0;
5910 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005911 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005912 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005913 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005914 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005915
danielk1977be718892006-06-23 08:05:19 +00005916 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005917 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005918 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00005919 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005920 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005921 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005922 }
drh688852a2014-02-17 22:40:43 +00005923 VdbeBranchTaken(res!=0,2);
danielk1977a298e902006-06-22 09:53:48 +00005924 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005925 pc = pOp->p2 - 1;
5926 }
5927 }
drh1d454a32008-01-31 19:34:51 +00005928 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005929
drh9eff6162006-06-12 21:59:13 +00005930 break;
5931}
5932#endif /* SQLITE_OMIT_VIRTUALTABLE */
5933
5934#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005935/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005936** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00005937**
drh2133d822008-01-03 18:44:59 +00005938** Store the value of the P2-th column of
5939** the row of the virtual-table that the
5940** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005941*/
5942case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005943 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005944 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005945 Mem *pDest;
5946 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005947
drhdfe88ec2008-11-03 20:55:06 +00005948 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005949 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00005950 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005951 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005952 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005953 if( pCur->nullRow ){
5954 sqlite3VdbeMemSetNull(pDest);
5955 break;
5956 }
danielk19773e3a84d2008-08-01 17:37:40 +00005957 pVtab = pCur->pVtabCursor->pVtab;
5958 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005959 assert( pModule->xColumn );
5960 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005961
5962 /* The output cell may already have a buffer allocated. Move
5963 ** the current contents to sContext.s so in case the user-function
5964 ** can use the already allocated buffer instead of allocating a
5965 ** new one.
5966 */
5967 sqlite3VdbeMemMove(&sContext.s, pDest);
5968 MemSetTypeFlag(&sContext.s, MEM_Null);
5969
drhde4fcfd2008-01-19 23:50:26 +00005970 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00005971 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005972 if( sContext.isError ){
5973 rc = sContext.isError;
5974 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005975
drhde4fcfd2008-01-19 23:50:26 +00005976 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005977 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005978 ** dynamic allocation in sContext.s (a Mem struct) is released.
5979 */
5980 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005981 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005982 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005983 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005984
drhde4fcfd2008-01-19 23:50:26 +00005985 if( sqlite3VdbeMemTooBig(pDest) ){
5986 goto too_big;
5987 }
drh9eff6162006-06-12 21:59:13 +00005988 break;
5989}
5990#endif /* SQLITE_OMIT_VIRTUALTABLE */
5991
5992#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005993/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005994**
5995** Advance virtual table P1 to the next row in its result set and
5996** jump to instruction P2. Or, if the virtual table has reached
5997** the end of its result set, then fall through to the next instruction.
5998*/
drh9cbf3422008-01-17 16:22:13 +00005999case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006000 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006001 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006002 int res;
drh856c1032009-06-02 15:21:42 +00006003 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006004
drhc54a6172009-06-02 16:06:03 +00006005 res = 0;
drh856c1032009-06-02 15:21:42 +00006006 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006007 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006008 if( pCur->nullRow ){
6009 break;
6010 }
danielk19773e3a84d2008-08-01 17:37:40 +00006011 pVtab = pCur->pVtabCursor->pVtab;
6012 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006013 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006014
drhde4fcfd2008-01-19 23:50:26 +00006015 /* Invoke the xNext() method of the module. There is no way for the
6016 ** underlying implementation to return an error if one occurs during
6017 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6018 ** data is available) and the error code returned when xColumn or
6019 ** some other method is next invoked on the save virtual table cursor.
6020 */
drhde4fcfd2008-01-19 23:50:26 +00006021 p->inVtabMethod = 1;
6022 rc = pModule->xNext(pCur->pVtabCursor);
6023 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006024 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006025 if( rc==SQLITE_OK ){
6026 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006027 }
drh688852a2014-02-17 22:40:43 +00006028 VdbeBranchTaken(!res,2);
drhde4fcfd2008-01-19 23:50:26 +00006029 if( !res ){
6030 /* If there is data, jump to P2 */
6031 pc = pOp->p2 - 1;
6032 }
drh49afe3a2013-07-10 03:05:14 +00006033 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006034}
6035#endif /* SQLITE_OMIT_VIRTUALTABLE */
6036
danielk1977182c4ba2007-06-27 15:53:34 +00006037#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006038/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006039**
drh66a51672008-01-03 00:01:23 +00006040** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006041** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006042** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006043*/
drh9cbf3422008-01-17 16:22:13 +00006044case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006045 sqlite3_vtab *pVtab;
6046 Mem *pName;
6047
danielk1977595a5232009-07-24 17:58:53 +00006048 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006049 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006050 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006051 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006052 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006053 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006054 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006055 testcase( pName->enc==SQLITE_UTF8 );
6056 testcase( pName->enc==SQLITE_UTF16BE );
6057 testcase( pName->enc==SQLITE_UTF16LE );
6058 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6059 if( rc==SQLITE_OK ){
6060 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006061 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006062 p->expired = 0;
6063 }
danielk1977182c4ba2007-06-27 15:53:34 +00006064 break;
6065}
6066#endif
drh4cbdda92006-06-14 19:00:20 +00006067
6068#ifndef SQLITE_OMIT_VIRTUALTABLE
drh0fd61352014-02-07 02:29:45 +00006069/* Opcode: VUpdate P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00006070** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006071**
drh66a51672008-01-03 00:01:23 +00006072** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006073** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006074** are contiguous memory cells starting at P3 to pass to the xUpdate
6075** invocation. The value in register (P3+P2-1) corresponds to the
6076** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006077**
6078** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006079** The argv[0] element (which corresponds to memory cell P3)
6080** is the rowid of a row to delete. If argv[0] is NULL then no
6081** deletion occurs. The argv[1] element is the rowid of the new
6082** row. This can be NULL to have the virtual table select the new
6083** rowid for itself. The subsequent elements in the array are
6084** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006085**
6086** If P2==1 then no insert is performed. argv[0] is the rowid of
6087** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006088**
6089** P1 is a boolean flag. If it is set to true and the xUpdate call
6090** is successful, then the value returned by sqlite3_last_insert_rowid()
6091** is set to the value of the rowid for the row just inserted.
drh0fd61352014-02-07 02:29:45 +00006092**
6093** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
6094** apply in the case of a constraint failure on an insert or update.
danielk1977399918f2006-06-14 13:03:23 +00006095*/
drh9cbf3422008-01-17 16:22:13 +00006096case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006097 sqlite3_vtab *pVtab;
6098 sqlite3_module *pModule;
6099 int nArg;
6100 int i;
6101 sqlite_int64 rowid;
6102 Mem **apArg;
6103 Mem *pX;
6104
danb061d052011-04-25 18:49:57 +00006105 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6106 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6107 );
drh9e92a472013-06-27 17:40:30 +00006108 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006109 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006110 pModule = (sqlite3_module *)pVtab->pModule;
6111 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006112 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006113 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006114 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006115 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006116 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006117 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006118 assert( memIsValid(pX) );
6119 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00006120 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00006121 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006122 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006123 }
danb061d052011-04-25 18:49:57 +00006124 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006125 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006126 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006127 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006128 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006129 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006130 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006131 }
drhd91c1a12013-02-09 13:58:25 +00006132 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006133 if( pOp->p5==OE_Ignore ){
6134 rc = SQLITE_OK;
6135 }else{
6136 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6137 }
6138 }else{
6139 p->nChange++;
6140 }
danielk1977399918f2006-06-14 13:03:23 +00006141 }
drh4cbdda92006-06-14 19:00:20 +00006142 break;
danielk1977399918f2006-06-14 13:03:23 +00006143}
6144#endif /* SQLITE_OMIT_VIRTUALTABLE */
6145
danielk197759a93792008-05-15 17:48:20 +00006146#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6147/* Opcode: Pagecount P1 P2 * * *
6148**
6149** Write the current number of pages in database P1 to memory cell P2.
6150*/
6151case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006152 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006153 break;
6154}
6155#endif
6156
drh60ac3f42010-11-23 18:59:27 +00006157
6158#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6159/* Opcode: MaxPgcnt P1 P2 P3 * *
6160**
6161** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006162** Do not let the maximum page count fall below the current page count and
6163** do not change the maximum page count value if P3==0.
6164**
drh60ac3f42010-11-23 18:59:27 +00006165** Store the maximum page count after the change in register P2.
6166*/
6167case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006168 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006169 Btree *pBt;
6170
6171 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006172 newMax = 0;
6173 if( pOp->p3 ){
6174 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006175 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006176 }
6177 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006178 break;
6179}
6180#endif
6181
6182
drhaceb31b2014-02-08 01:40:27 +00006183/* Opcode: Init * P2 * P4 *
6184** Synopsis: Start at P2
6185**
6186** Programs contain a single instance of this opcode as the very first
6187** opcode.
drh949f9cd2008-01-12 21:35:57 +00006188**
6189** If tracing is enabled (by the sqlite3_trace()) interface, then
6190** the UTF-8 string contained in P4 is emitted on the trace callback.
drhaceb31b2014-02-08 01:40:27 +00006191** Or if P4 is blank, use the string returned by sqlite3_sql().
6192**
6193** If P2 is not zero, jump to instruction P2.
drh949f9cd2008-01-12 21:35:57 +00006194*/
drhaceb31b2014-02-08 01:40:27 +00006195case OP_Init: { /* jump */
drh856c1032009-06-02 15:21:42 +00006196 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006197 char *z;
drh856c1032009-06-02 15:21:42 +00006198
drhaceb31b2014-02-08 01:40:27 +00006199 if( pOp->p2 ){
6200 pc = pOp->p2 - 1;
6201 }
6202#ifndef SQLITE_OMIT_TRACE
drh37f58e92012-09-04 21:34:26 +00006203 if( db->xTrace
6204 && !p->doingRerun
6205 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6206 ){
drhc3f1d5f2011-05-30 23:42:16 +00006207 z = sqlite3VdbeExpandSql(p, zTrace);
6208 db->xTrace(db->pTraceArg, z);
6209 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006210 }
drh8f8b2312013-10-18 20:03:43 +00006211#ifdef SQLITE_USE_FCNTL_TRACE
6212 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6213 if( zTrace ){
6214 int i;
6215 for(i=0; i<db->nDb; i++){
drh693e6712014-01-24 22:58:00 +00006216 if( MASKBIT(i) & p->btreeMask)==0 ) continue;
drh8f8b2312013-10-18 20:03:43 +00006217 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6218 }
6219 }
6220#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006221#ifdef SQLITE_DEBUG
6222 if( (db->flags & SQLITE_SqlTrace)!=0
6223 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6224 ){
6225 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6226 }
6227#endif /* SQLITE_DEBUG */
drhaceb31b2014-02-08 01:40:27 +00006228#endif /* SQLITE_OMIT_TRACE */
drh949f9cd2008-01-12 21:35:57 +00006229 break;
6230}
drh949f9cd2008-01-12 21:35:57 +00006231
drh91fd4d42008-01-19 20:11:25 +00006232
6233/* Opcode: Noop * * * * *
6234**
6235** Do nothing. This instruction is often useful as a jump
6236** destination.
drh5e00f6c2001-09-13 13:46:56 +00006237*/
drh91fd4d42008-01-19 20:11:25 +00006238/*
6239** The magic Explain opcode are only inserted when explain==2 (which
6240** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6241** This opcode records information from the optimizer. It is the
6242** the same as a no-op. This opcodesnever appears in a real VM program.
6243*/
6244default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006245 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006246 break;
6247}
6248
6249/*****************************************************************************
6250** The cases of the switch statement above this line should all be indented
6251** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6252** readability. From this point on down, the normal indentation rules are
6253** restored.
6254*****************************************************************************/
6255 }
drh6e142f52000-06-08 13:36:40 +00006256
drh7b396862003-01-01 23:06:20 +00006257#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006258 {
shane9bcbdad2008-05-29 20:22:37 +00006259 u64 elapsed = sqlite3Hwtime() - start;
6260 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006261 pOp->cnt++;
6262#if 0
shane9bcbdad2008-05-29 20:22:37 +00006263 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006264 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006265#endif
6266 }
drh7b396862003-01-01 23:06:20 +00006267#endif
6268
drh6e142f52000-06-08 13:36:40 +00006269 /* The following code adds nothing to the actual functionality
6270 ** of the program. It is only here for testing and debugging.
6271 ** On the other hand, it does burn CPU cycles every time through
6272 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6273 */
6274#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006275 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006276
drhcf1023c2007-05-08 20:59:49 +00006277#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006278 if( db->flags & SQLITE_VdbeTrace ){
6279 if( rc!=0 ) printf("rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006280 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
drh84e55a82013-11-13 17:58:23 +00006281 registerTrace(pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006282 }
drh3c657212009-11-17 23:59:58 +00006283 if( pOp->opflags & OPFLG_OUT3 ){
drh84e55a82013-11-13 17:58:23 +00006284 registerTrace(pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006285 }
drh75897232000-05-29 14:26:00 +00006286 }
danielk1977b5402fb2005-01-12 07:15:04 +00006287#endif /* SQLITE_DEBUG */
6288#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006289 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006290
drha05a7222008-01-19 03:35:58 +00006291 /* If we reach this point, it means that execution is finished with
6292 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006293 */
drha05a7222008-01-19 03:35:58 +00006294vdbe_error_halt:
6295 assert( rc );
6296 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006297 testcase( sqlite3GlobalConfig.xLog!=0 );
6298 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6299 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006300 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006301 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6302 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006303 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006304 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006305 }
drh900b31e2007-08-28 02:27:51 +00006306
6307 /* This is the only way out of this procedure. We have to
6308 ** release the mutexes on btrees that were acquired at the
6309 ** top. */
6310vdbe_return:
drh99a66922011-05-13 18:51:42 +00006311 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006312 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006313 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006314 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006315 return rc;
6316
drh023ae032007-05-08 12:12:16 +00006317 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6318 ** is encountered.
6319 */
6320too_big:
drhf089aa42008-07-08 19:34:06 +00006321 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006322 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006323 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006324
drh98640a32007-06-07 19:08:32 +00006325 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006326 */
6327no_mem:
drh17435752007-08-16 04:30:38 +00006328 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006329 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006330 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006331 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006332
drhb86ccfb2003-01-28 23:13:10 +00006333 /* Jump to here for any other kind of fatal error. The "rc" variable
6334 ** should hold the error number.
6335 */
6336abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006337 assert( p->zErrMsg==0 );
6338 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006339 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006340 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006341 }
drha05a7222008-01-19 03:35:58 +00006342 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006343
danielk19776f8a5032004-05-10 10:34:51 +00006344 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006345 ** flag.
6346 */
6347abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006348 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006349 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006350 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006351 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006352 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006353}