blob: 01b5961e29da3a058a58f61aa3391502206a6a35 [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*************************************************************************
drh9a324642003-09-06 20:12:01 +000012** The code in this file implements execution method of the
13** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
14** handles housekeeping details such as creating and deleting
15** VDBE instances. This file is solely interested in executing
16** the VDBE program.
17**
danielk1977fc57d7b2004-05-26 02:04:57 +000018** In the external interface, an "sqlite3_stmt*" is an opaque pointer
drh9a324642003-09-06 20:12:01 +000019** to a VDBE.
drh75897232000-05-29 14:26:00 +000020**
21** The SQL parser generates a program which is then executed by
22** the VDBE to do the work of the SQL statement. VDBE programs are
23** similar in form to assembly language. The program consists of
24** a linear sequence of operations. Each operation has an opcode
drh9cbf3422008-01-17 16:22:13 +000025** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4
26** is a null-terminated string. Operand P5 is an unsigned character.
27** Few opcodes use all 5 operands.
drh75897232000-05-29 14:26:00 +000028**
drh9cbf3422008-01-17 16:22:13 +000029** Computation results are stored on a set of registers numbered beginning
30** with 1 and going up to Vdbe.nMem. Each register can store
31** either an integer, a null-terminated string, a floating point
shane21e7feb2008-05-30 15:59:49 +000032** number, or the SQL "NULL" value. An implicit conversion from one
drhb19a2bc2001-09-16 00:13:26 +000033** type to the other occurs as necessary.
drh75897232000-05-29 14:26:00 +000034**
danielk19774adee202004-05-08 08:23:19 +000035** Most of the code in this file is taken up by the sqlite3VdbeExec()
drh75897232000-05-29 14:26:00 +000036** function which does the work of interpreting a VDBE program.
37** But other routines are also provided to help in building up
38** a program instruction by instruction.
39**
drhac82fcf2002-09-08 17:23:41 +000040** Various scripts scan this source file in order to generate HTML
41** documentation, headers files, or other derived files. The formatting
42** of the code in this file is, therefore, important. See other comments
43** in this file for details. If in doubt, do not deviate from existing
44** commenting and indentation practices when changing or adding code.
45**
drh27731d72009-06-22 12:05:10 +000046** $Id: vdbe.c,v 1.860 2009/06/22 12:05:10 drh Exp $
drh75897232000-05-29 14:26:00 +000047*/
48#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000049#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000050
51/*
drh487ab3c2001-11-08 00:45:21 +000052** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000053** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000054** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000055** working correctly. This variable has no function other than to
56** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000057*/
drh0f7eb612006-08-08 13:51:43 +000058#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000059int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000060#endif
drh487ab3c2001-11-08 00:45:21 +000061
drhf6038712004-02-08 18:07:34 +000062/*
63** When this global variable is positive, it gets decremented once before
drh881feaa2006-07-26 01:39:30 +000064** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
65** field of the sqlite3 structure is set in order to simulate and interrupt.
drhf6038712004-02-08 18:07:34 +000066**
67** This facility is used for testing purposes only. It does not function
68** in an ordinary build.
69*/
drh0f7eb612006-08-08 13:51:43 +000070#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000071int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000072#endif
drh1350b032002-02-27 19:00:20 +000073
danielk19777e18c252004-05-25 11:47:24 +000074/*
drh6bf89572004-11-03 16:27:01 +000075** The next global variable is incremented each type the OP_Sort opcode
76** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000077** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000078** has no function other than to help verify the correct operation of the
79** library.
80*/
drh0f7eb612006-08-08 13:51:43 +000081#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000082int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000083#endif
drh6bf89572004-11-03 16:27:01 +000084
85/*
drhae7e1512007-05-02 16:51:59 +000086** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000087** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000088** use this information to make sure that the zero-blob functionality
89** is working correctly. This variable has no function other than to
90** help verify the correct operation of the library.
91*/
92#ifdef SQLITE_TEST
93int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000094static void updateMaxBlobsize(Mem *p){
95 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
96 sqlite3_max_blobsize = p->n;
97 }
98}
drhae7e1512007-05-02 16:51:59 +000099#endif
100
101/*
drhb7654112008-01-12 12:48:07 +0000102** Test a register to see if it exceeds the current maximum blob size.
103** If it does, record the new maximum blob size.
104*/
drh678ccce2008-03-31 18:19:54 +0000105#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000106# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000107#else
108# define UPDATE_MAX_BLOBSIZE(P)
109#endif
110
111/*
drh9cbf3422008-01-17 16:22:13 +0000112** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000113** already. Return non-zero if a malloc() fails.
114*/
drhb21c8cd2007-08-21 19:33:56 +0000115#define Stringify(P, enc) \
116 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000117 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000118
119/*
danielk1977bd7e4602004-05-24 07:34:48 +0000120** An ephemeral string value (signified by the MEM_Ephem flag) contains
121** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000122** is responsible for deallocating that string. Because the register
123** does not control the string, it might be deleted without the register
124** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000125**
126** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000127** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000128** converts an MEM_Ephem string into an MEM_Dyn string.
129*/
drhb21c8cd2007-08-21 19:33:56 +0000130#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000131 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000132 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000133
134/*
danielk19771cc5ed82007-05-16 17:28:43 +0000135** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
136** P if required.
137*/
drhb21c8cd2007-08-21 19:33:56 +0000138#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +0000139
140/*
shane21e7feb2008-05-30 15:59:49 +0000141** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000142** user-defined function or returned to the user as the result of a query.
143** The second argument, 'db_enc' is the text encoding used by the vdbe for
drh9cbf3422008-01-17 16:22:13 +0000144** register variables. This routine sets the pMem->enc and pMem->type
danielk1977c572ef72004-05-27 09:28:41 +0000145** variables used by the sqlite3_value_*() routines.
146*/
drh3a41a3f2004-05-30 02:14:17 +0000147#define storeTypeInfo(A,B) _storeTypeInfo(A)
148static void _storeTypeInfo(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000149 int flags = pMem->flags;
150 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000151 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000152 }
153 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000154 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000155 }
156 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000157 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000158 }
159 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000160 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000161 }else{
drh9c054832004-05-31 18:51:57 +0000162 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000163 }
164}
danielk19778a6b5412004-05-24 07:04:25 +0000165
166/*
drh3a40f692008-01-04 16:50:09 +0000167** Properties of opcodes. The OPFLG_INITIALIZER macro is
168** created by mkopcodeh.awk during compilation. Data is obtained
169** from the comments following the "case OP_xxxx:" statements in
170** this file.
drh3a40f692008-01-04 16:50:09 +0000171*/
danielk1977263ac192008-09-02 11:05:01 +0000172static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
drh3a40f692008-01-04 16:50:09 +0000173
174/*
175** Return true if an opcode has any of the OPFLG_xxx properties
176** specified by mask.
177*/
178int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
danielk197764202cf2008-11-17 15:31:47 +0000179 assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
drh3a40f692008-01-04 16:50:09 +0000180 return (opcodeProperty[opcode]&mask)!=0;
181}
182
183/*
drhdfe88ec2008-11-03 20:55:06 +0000184** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000185** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000186*/
drhdfe88ec2008-11-03 20:55:06 +0000187static VdbeCursor *allocateCursor(
188 Vdbe *p, /* The virtual machine */
189 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000190 int nField, /* Number of fields in the table or index */
drh3d4501e2008-12-04 20:40:10 +0000191 int iDb, /* When database the cursor belongs to, or -1 */
drh94c3a2b2009-06-17 16:20:04 +0000192 int isBtreeCursor /* True for B-Tree vs. pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000193){
194 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000195 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000196 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000197 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000198 **
199 ** * Sometimes cursor numbers are used for a couple of different
200 ** purposes in a vdbe program. The different uses might require
201 ** different sized allocations. Memory cells provide growable
202 ** allocations.
203 **
204 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
205 ** be freed lazily via the sqlite3_release_memory() API. This
206 ** minimizes the number of malloc calls made by the system.
207 **
208 ** Memory cells for cursors are allocated at the top of the address
209 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
210 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
211 */
212 Mem *pMem = &p->aMem[p->nMem-iCur];
213
danielk19775f096132008-03-28 15:44:09 +0000214 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000215 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000216 nByte =
drhdfe88ec2008-11-03 20:55:06 +0000217 sizeof(VdbeCursor) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000218 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
219 2*nField*sizeof(u32);
220
drh290c1942004-08-21 17:54:45 +0000221 assert( iCur<p->nCursor );
222 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000223 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000224 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000225 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000226 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000227 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 memset(pMem->z, 0, nByte);
danielk197794eb6a12005-12-15 15:22:08 +0000229 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000230 pCx->nField = nField;
231 if( nField ){
drhdfe88ec2008-11-03 20:55:06 +0000232 pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000233 }
234 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000235 pCx->pCursor = (BtCursor*)
236 &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000237 }
danielk197794eb6a12005-12-15 15:22:08 +0000238 }
drh4774b132004-06-12 20:12:51 +0000239 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000240}
241
danielk19773d1bfea2004-05-14 11:00:53 +0000242/*
drh29d72102006-02-09 22:13:41 +0000243** Try to convert a value into a numeric representation if we can
244** do so without loss of information. In other words, if the string
245** looks like a number, convert it into a number. If it does not
246** look like a number, leave it alone.
247*/
drhb21c8cd2007-08-21 19:33:56 +0000248static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000249 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
250 int realnum;
drhb21c8cd2007-08-21 19:33:56 +0000251 sqlite3VdbeMemNulTerminate(pRec);
drh29d72102006-02-09 22:13:41 +0000252 if( (pRec->flags&MEM_Str)
253 && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
254 i64 value;
drhb21c8cd2007-08-21 19:33:56 +0000255 sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
drhb6a9ece2007-06-26 00:37:27 +0000256 if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
drh3c024d62007-03-30 11:23:45 +0000257 pRec->u.i = value;
danielk1977a7a8e142008-02-13 18:25:27 +0000258 MemSetTypeFlag(pRec, MEM_Int);
drh29d72102006-02-09 22:13:41 +0000259 }else{
260 sqlite3VdbeMemRealify(pRec);
261 }
262 }
263 }
264}
265
266/*
drh8a512562005-11-14 22:29:05 +0000267** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000268**
drh8a512562005-11-14 22:29:05 +0000269** SQLITE_AFF_INTEGER:
270** SQLITE_AFF_REAL:
271** SQLITE_AFF_NUMERIC:
272** Try to convert pRec to an integer representation or a
273** floating-point representation if an integer representation
274** is not possible. Note that the integer representation is
275** always preferred, even if the affinity is REAL, because
276** an integer representation is more space efficient on disk.
277**
278** SQLITE_AFF_TEXT:
279** Convert pRec to a text representation.
280**
281** SQLITE_AFF_NONE:
282** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000283*/
drh17435752007-08-16 04:30:38 +0000284static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000285 Mem *pRec, /* The value to apply affinity to */
286 char affinity, /* The affinity to be applied */
287 u8 enc /* Use this text encoding */
288){
drh8a512562005-11-14 22:29:05 +0000289 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000290 /* Only attempt the conversion to TEXT if there is an integer or real
291 ** representation (blob and NULL do not get converted) but no string
292 ** representation.
293 */
294 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000295 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000296 }
297 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000298 }else if( affinity!=SQLITE_AFF_NONE ){
299 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
300 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000301 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000302 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000303 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000304 }
danielk19773d1bfea2004-05-14 11:00:53 +0000305 }
306}
307
danielk1977aee18ef2005-03-09 12:26:50 +0000308/*
drh29d72102006-02-09 22:13:41 +0000309** Try to convert the type of a function argument or a result column
310** into a numeric representation. Use either INTEGER or REAL whichever
311** is appropriate. But only do the conversion if it is possible without
312** loss of information and return the revised type of the argument.
313**
314** This is an EXPERIMENTAL api and is subject to change or removal.
315*/
316int sqlite3_value_numeric_type(sqlite3_value *pVal){
317 Mem *pMem = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +0000318 applyNumericAffinity(pMem);
drh29d72102006-02-09 22:13:41 +0000319 storeTypeInfo(pMem, 0);
320 return pMem->type;
321}
322
323/*
danielk1977aee18ef2005-03-09 12:26:50 +0000324** Exported version of applyAffinity(). This one works on sqlite3_value*,
325** not the internal Mem* type.
326*/
danielk19771e536952007-08-16 10:09:01 +0000327void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000328 sqlite3_value *pVal,
329 u8 affinity,
330 u8 enc
331){
drhb21c8cd2007-08-21 19:33:56 +0000332 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000333}
334
danielk1977b5402fb2005-01-12 07:15:04 +0000335#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000336/*
danielk1977ca6b2912004-05-21 10:49:47 +0000337** Write a nice string representation of the contents of cell pMem
338** into buffer zBuf, length nBuf.
339*/
drh74161702006-02-24 02:53:49 +0000340void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000341 char *zCsr = zBuf;
342 int f = pMem->flags;
343
drh57196282004-10-06 15:41:16 +0000344 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000345
danielk1977ca6b2912004-05-21 10:49:47 +0000346 if( f&MEM_Blob ){
347 int i;
348 char c;
349 if( f & MEM_Dyn ){
350 c = 'z';
351 assert( (f & (MEM_Static|MEM_Ephem))==0 );
352 }else if( f & MEM_Static ){
353 c = 't';
354 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
355 }else if( f & MEM_Ephem ){
356 c = 'e';
357 assert( (f & (MEM_Static|MEM_Dyn))==0 );
358 }else{
359 c = 's';
360 }
361
drh5bb3eb92007-05-04 13:15:55 +0000362 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000363 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000364 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000365 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000366 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000367 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000368 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000369 }
370 for(i=0; i<16 && i<pMem->n; i++){
371 char z = pMem->z[i];
372 if( z<32 || z>126 ) *zCsr++ = '.';
373 else *zCsr++ = z;
374 }
375
drhe718efe2007-05-10 21:14:03 +0000376 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000377 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000378 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000379 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000380 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000381 }
danielk1977b1bc9532004-05-22 03:05:33 +0000382 *zCsr = '\0';
383 }else if( f & MEM_Str ){
384 int j, k;
385 zBuf[0] = ' ';
386 if( f & MEM_Dyn ){
387 zBuf[1] = 'z';
388 assert( (f & (MEM_Static|MEM_Ephem))==0 );
389 }else if( f & MEM_Static ){
390 zBuf[1] = 't';
391 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
392 }else if( f & MEM_Ephem ){
393 zBuf[1] = 'e';
394 assert( (f & (MEM_Static|MEM_Dyn))==0 );
395 }else{
396 zBuf[1] = 's';
397 }
398 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000399 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000400 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000401 zBuf[k++] = '[';
402 for(j=0; j<15 && j<pMem->n; j++){
403 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000404 if( c>=0x20 && c<0x7f ){
405 zBuf[k++] = c;
406 }else{
407 zBuf[k++] = '.';
408 }
409 }
410 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000411 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000412 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000413 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000414 }
danielk1977ca6b2912004-05-21 10:49:47 +0000415}
416#endif
417
drh5b6afba2008-01-05 16:29:28 +0000418#ifdef SQLITE_DEBUG
419/*
420** Print the value of a register for tracing purposes:
421*/
422static void memTracePrint(FILE *out, Mem *p){
423 if( p->flags & MEM_Null ){
424 fprintf(out, " NULL");
425 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
426 fprintf(out, " si:%lld", p->u.i);
427 }else if( p->flags & MEM_Int ){
428 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000429#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000430 }else if( p->flags & MEM_Real ){
431 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000432#endif
drh733bf1b2009-04-22 00:47:00 +0000433 }else if( p->flags & MEM_RowSet ){
434 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000435 }else{
436 char zBuf[200];
437 sqlite3VdbeMemPrettyPrint(p, zBuf);
438 fprintf(out, " ");
439 fprintf(out, "%s", zBuf);
440 }
441}
442static void registerTrace(FILE *out, int iReg, Mem *p){
443 fprintf(out, "REG[%d] = ", iReg);
444 memTracePrint(out, p);
445 fprintf(out, "\n");
446}
447#endif
448
449#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000450# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000451#else
452# define REGISTER_TRACE(R,M)
453#endif
454
danielk197784ac9d02004-05-18 09:58:06 +0000455
drh7b396862003-01-01 23:06:20 +0000456#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000457
458/*
459** hwtime.h contains inline assembler code for implementing
460** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000461*/
shane9bcbdad2008-05-29 20:22:37 +0000462#include "hwtime.h"
463
drh7b396862003-01-01 23:06:20 +0000464#endif
465
drh8c74a8c2002-08-25 19:20:40 +0000466/*
drhcaec2f12003-01-07 02:47:47 +0000467** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000468** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000469** processing of the VDBE program is interrupted.
470**
471** This macro added to every instruction that does a jump in order to
472** implement a loop. This test used to be on every single instruction,
473** but that meant we more testing that we needed. By only testing the
474** flag on jump instructions, we get a (small) speed improvement.
475*/
476#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000477 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000478
danielk1977861f7452008-06-05 11:39:11 +0000479#ifdef SQLITE_DEBUG
480static int fileExists(sqlite3 *db, const char *zFile){
danielk1977ad0132d2008-06-07 08:58:22 +0000481 int res = 0;
482 int rc = SQLITE_OK;
483#ifdef SQLITE_TEST
484 /* If we are currently testing IO errors, then do not call OsAccess() to
485 ** test for the presence of zFile. This is because any IO error that
486 ** occurs here will not be reported, causing the test to fail.
487 */
488 extern int sqlite3_io_error_pending;
489 if( sqlite3_io_error_pending<=0 )
490#endif
491 rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
danielk1977861f7452008-06-05 11:39:11 +0000492 return (res && rc==SQLITE_OK);
493}
494#endif
drhcaec2f12003-01-07 02:47:47 +0000495
danielk1977fd7f0452008-12-17 17:30:26 +0000496#ifndef NDEBUG
497/*
498** This function is only called from within an assert() expression. It
499** checks that the sqlite3.nTransaction variable is correctly set to
500** the number of non-transaction savepoints currently in the
501** linked list starting at sqlite3.pSavepoint.
502**
503** Usage:
504**
505** assert( checkSavepointCount(db) );
506*/
507static int checkSavepointCount(sqlite3 *db){
508 int n = 0;
509 Savepoint *p;
510 for(p=db->pSavepoint; p; p=p->pNext) n++;
511 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
512 return 1;
513}
514#endif
515
drhcaec2f12003-01-07 02:47:47 +0000516/*
drhb86ccfb2003-01-28 23:13:10 +0000517** Execute as much of a VDBE program as we can then return.
518**
danielk19774adee202004-05-08 08:23:19 +0000519** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000520** close the program with a final OP_Halt and to set up the callbacks
521** and the error message pointer.
522**
523** Whenever a row or result data is available, this routine will either
524** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000525** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000526**
527** If an attempt is made to open a locked database, then this routine
528** will either invoke the busy callback (if there is one) or it will
529** return SQLITE_BUSY.
530**
531** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000532** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000533** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
534**
535** If the callback ever returns non-zero, then the program exits
536** immediately. There will be no error message but the p->rc field is
537** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
538**
drh9468c7f2003-03-07 19:50:07 +0000539** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
540** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000541**
542** Other fatal errors return SQLITE_ERROR.
543**
danielk19774adee202004-05-08 08:23:19 +0000544** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000545** used to clean up the mess that was left behind.
546*/
danielk19774adee202004-05-08 08:23:19 +0000547int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000548 Vdbe *p /* The VDBE */
549){
550 int pc; /* The program counter */
551 Op *pOp; /* Current operation */
552 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000553 sqlite3 *db = p->db; /* The database */
drh8079a0d2006-01-12 17:20:50 +0000554 u8 encoding = ENC(db); /* The database encoding */
drhb27b7f52008-12-10 18:03:45 +0000555 Mem *pIn1 = 0; /* 1st input operand */
556 Mem *pIn2 = 0; /* 2nd input operand */
557 Mem *pIn3 = 0; /* 3rd input operand */
558 Mem *pOut = 0; /* Output operand */
drhb1fdb2a2008-01-05 04:06:03 +0000559 u8 opProperty;
drh0acb7e42008-06-25 00:12:41 +0000560 int iCompare = 0; /* Result of last OP_Compare operation */
shanebe217792009-03-05 04:20:31 +0000561 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000562#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000563 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000564 int origPc; /* Program counter at start of opcode */
565#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000566#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
567 int nProgressOps = 0; /* Opcodes executed since progress callback. */
568#endif
drh856c1032009-06-02 15:21:42 +0000569 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000570
drhca48c902008-01-18 14:08:24 +0000571 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhb86ccfb2003-01-28 23:13:10 +0000572 assert( db->magic==SQLITE_MAGIC_BUSY );
danielk1977f7590db2009-04-10 12:55:16 +0000573 sqlite3VdbeMutexArrayEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000574 if( p->rc==SQLITE_NOMEM ){
575 /* This happens if a malloc() inside a call to sqlite3_column_text() or
576 ** sqlite3_column_text16() failed. */
577 goto no_mem;
578 }
drh3a840692003-01-29 22:58:26 +0000579 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
580 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000581 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000582 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000583 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000584 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000585 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000586#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000587 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000588 if( p->pc==0
589 && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
drh3c23a882007-01-09 14:01:13 +0000590 ){
591 int i;
592 printf("VDBE Program Listing:\n");
593 sqlite3VdbePrintSql(p);
594 for(i=0; i<p->nOp; i++){
595 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
596 }
597 }
danielk1977861f7452008-06-05 11:39:11 +0000598 if( fileExists(db, "vdbe_trace") ){
drh3c23a882007-01-09 14:01:13 +0000599 p->trace = stdout;
600 }
danielk19772d1d86f2008-06-20 14:59:51 +0000601 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000602#endif
drhb86ccfb2003-01-28 23:13:10 +0000603 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000604 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000605 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000606#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000607 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000608 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000609#endif
drh75897232000-05-29 14:26:00 +0000610 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000611
danielk19778b60e0f2005-01-12 09:10:39 +0000612 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000613 */
danielk19778b60e0f2005-01-12 09:10:39 +0000614#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000615 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000616 if( pc==0 ){
617 printf("VDBE Execution Trace:\n");
618 sqlite3VdbePrintSql(p);
619 }
danielk19774adee202004-05-08 08:23:19 +0000620 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000621 }
drh19db9352008-03-27 22:42:51 +0000622 if( p->trace==0 && pc==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000623 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000624 if( fileExists(db, "vdbe_sqltrace") ){
drh19db9352008-03-27 22:42:51 +0000625 sqlite3VdbePrintSql(p);
626 }
danielk19772d1d86f2008-06-20 14:59:51 +0000627 sqlite3EndBenignMalloc();
drh3f7d4e42004-07-24 14:35:58 +0000628 }
629#endif
630
drh6e142f52000-06-08 13:36:40 +0000631
drhf6038712004-02-08 18:07:34 +0000632 /* Check to see if we need to simulate an interrupt. This only happens
633 ** if we have a special test build.
634 */
635#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000636 if( sqlite3_interrupt_count>0 ){
637 sqlite3_interrupt_count--;
638 if( sqlite3_interrupt_count==0 ){
639 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000640 }
641 }
642#endif
643
danielk1977348bb5d2003-10-18 09:37:26 +0000644#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
645 /* Call the progress callback if it is configured and the required number
646 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000647 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000648 ** If the progress callback returns non-zero, exit the virtual machine with
649 ** a return code SQLITE_ABORT.
650 */
drh3914aed2004-01-31 20:40:42 +0000651 if( db->xProgress ){
652 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000653 int prc;
drhf8888bb2006-05-26 19:57:19 +0000654 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000655 prc =db->xProgress(db->pProgressArg);
drhf8888bb2006-05-26 19:57:19 +0000656 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000657 if( prc!=0 ){
658 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000659 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000660 }
danielk19773fe11f32007-06-13 16:49:48 +0000661 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000662 }
drh3914aed2004-01-31 20:40:42 +0000663 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000664 }
danielk1977348bb5d2003-10-18 09:37:26 +0000665#endif
666
drh4c583122008-01-04 22:01:03 +0000667 /* Do common setup processing for any opcode that is marked
668 ** with the "out2-prerelease" tag. Such opcodes have a single
drh9cbf3422008-01-17 16:22:13 +0000669 ** output which is specified by the P2 parameter. The P2 register
drh4c583122008-01-04 22:01:03 +0000670 ** is initialized to a NULL.
671 */
drhb1fdb2a2008-01-05 04:06:03 +0000672 opProperty = opcodeProperty[pOp->opcode];
673 if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000674 assert( pOp->p2>0 );
675 assert( pOp->p2<=p->nMem );
676 pOut = &p->aMem[pOp->p2];
danielk19775f096132008-03-28 15:44:09 +0000677 sqlite3VdbeMemReleaseExternal(pOut);
drh4c583122008-01-04 22:01:03 +0000678 pOut->flags = MEM_Null;
drhcdd03762009-05-07 12:17:33 +0000679 pOut->n = 0;
drhb1fdb2a2008-01-05 04:06:03 +0000680 }else
681
682 /* Do common setup for opcodes marked with one of the following
683 ** combinations of properties.
684 **
685 ** in1
686 ** in1 in2
687 ** in1 in2 out3
688 ** in1 in3
drhb1fdb2a2008-01-05 04:06:03 +0000689 **
drh9cbf3422008-01-17 16:22:13 +0000690 ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
691 ** registers for inputs. Variable pOut points to the output register.
drhb1fdb2a2008-01-05 04:06:03 +0000692 */
693 if( (opProperty & OPFLG_IN1)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000694 assert( pOp->p1>0 );
695 assert( pOp->p1<=p->nMem );
696 pIn1 = &p->aMem[pOp->p1];
697 REGISTER_TRACE(pOp->p1, pIn1);
drhb1fdb2a2008-01-05 04:06:03 +0000698 if( (opProperty & OPFLG_IN2)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000699 assert( pOp->p2>0 );
drhaa9b8962008-01-08 02:57:55 +0000700 assert( pOp->p2<=p->nMem );
701 pIn2 = &p->aMem[pOp->p2];
702 REGISTER_TRACE(pOp->p2, pIn2);
drh9cbf3422008-01-17 16:22:13 +0000703 if( (opProperty & OPFLG_OUT3)!=0 ){
704 assert( pOp->p3>0 );
705 assert( pOp->p3<=p->nMem );
706 pOut = &p->aMem[pOp->p3];
707 }
708 }else if( (opProperty & OPFLG_IN3)!=0 ){
709 assert( pOp->p3>0 );
drhaa9b8962008-01-08 02:57:55 +0000710 assert( pOp->p3<=p->nMem );
711 pIn3 = &p->aMem[pOp->p3];
712 REGISTER_TRACE(pOp->p3, pIn3);
713 }
drh9cbf3422008-01-17 16:22:13 +0000714 }else if( (opProperty & OPFLG_IN2)!=0 ){
715 assert( pOp->p2>0 );
716 assert( pOp->p2<=p->nMem );
717 pIn2 = &p->aMem[pOp->p2];
718 REGISTER_TRACE(pOp->p2, pIn2);
719 }else if( (opProperty & OPFLG_IN3)!=0 ){
720 assert( pOp->p3>0 );
721 assert( pOp->p3<=p->nMem );
722 pIn3 = &p->aMem[pOp->p3];
723 REGISTER_TRACE(pOp->p3, pIn3);
drh4c583122008-01-04 22:01:03 +0000724 }
725
drh75897232000-05-29 14:26:00 +0000726 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000727
drh5e00f6c2001-09-13 13:46:56 +0000728/*****************************************************************************
729** What follows is a massive switch statement where each case implements a
730** separate instruction in the virtual machine. If we follow the usual
731** indentation conventions, each case should be indented by 6 spaces. But
732** that is a lot of wasted space on the left margin. So the code within
733** the switch statement will break with convention and be flush-left. Another
734** big comment (similar to this one) will mark the point in the code where
735** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000736**
737** The formatting of each case is important. The makefile for SQLite
738** generates two C files "opcodes.h" and "opcodes.c" by scanning this
739** file looking for lines that begin with "case OP_". The opcodes.h files
740** will be filled with #defines that give unique integer values to each
741** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000742** each string is the symbolic name for the corresponding opcode. If the
743** case statement is followed by a comment of the form "/# same as ... #/"
744** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000745**
drh9cbf3422008-01-17 16:22:13 +0000746** Other keywords in the comment that follows each case are used to
747** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
748** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
749** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000750**
drhac82fcf2002-09-08 17:23:41 +0000751** Documentation about VDBE opcodes is generated by scanning this file
752** for lines of that contain "Opcode:". That line and all subsequent
753** comment lines are used in the generation of the opcode.html documentation
754** file.
755**
756** SUMMARY:
757**
758** Formatting is important to scripts that scan this file.
759** Do not deviate from the formatting style currently in use.
760**
drh5e00f6c2001-09-13 13:46:56 +0000761*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000762
drh9cbf3422008-01-17 16:22:13 +0000763/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000764**
765** An unconditional jump to address P2.
766** The next instruction executed will be
767** the one at index P2 from the beginning of
768** the program.
769*/
drh9cbf3422008-01-17 16:22:13 +0000770case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000771 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000772 pc = pOp->p2 - 1;
773 break;
774}
drh75897232000-05-29 14:26:00 +0000775
drh2eb95372008-06-06 15:04:36 +0000776/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000777**
drh2eb95372008-06-06 15:04:36 +0000778** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000779** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000780*/
drh9cbf3422008-01-17 16:22:13 +0000781case OP_Gosub: { /* jump */
drh2eb95372008-06-06 15:04:36 +0000782 assert( pOp->p1>0 );
783 assert( pOp->p1<=p->nMem );
784 pIn1 = &p->aMem[pOp->p1];
785 assert( (pIn1->flags & MEM_Dyn)==0 );
786 pIn1->flags = MEM_Int;
787 pIn1->u.i = pc;
788 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000789 pc = pOp->p2 - 1;
790 break;
791}
792
drh2eb95372008-06-06 15:04:36 +0000793/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000794**
drh2eb95372008-06-06 15:04:36 +0000795** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000796*/
drh2eb95372008-06-06 15:04:36 +0000797case OP_Return: { /* in1 */
798 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000799 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000800 break;
801}
802
drhe00ee6e2008-06-20 15:24:01 +0000803/* Opcode: Yield P1 * * * *
804**
805** Swap the program counter with the value in register P1.
806*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000807case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000808 int pcDest;
drhe00ee6e2008-06-20 15:24:01 +0000809 assert( (pIn1->flags & MEM_Dyn)==0 );
810 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000811 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000812 pIn1->u.i = pc;
813 REGISTER_TRACE(pOp->p1, pIn1);
814 pc = pcDest;
815 break;
816}
817
drh5053a792009-02-20 03:02:23 +0000818/* Opcode: HaltIfNull P1 P2 P3 P4 *
819**
820** Check the value in register P3. If is is NULL then Halt using
821** parameter P1, P2, and P4 as if this were a Halt instruction. If the
822** value in register P3 is not NULL, then this routine is a no-op.
823*/
824case OP_HaltIfNull: { /* in3 */
825 if( (pIn3->flags & MEM_Null)==0 ) break;
826 /* Fall through into OP_Halt */
827}
drhe00ee6e2008-06-20 15:24:01 +0000828
drh9cbf3422008-01-17 16:22:13 +0000829/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000830**
drh3d4501e2008-12-04 20:40:10 +0000831** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000832** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000833**
drh92f02c32004-09-02 14:57:08 +0000834** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
835** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
836** For errors, it can be some other value. If P1!=0 then P2 will determine
837** whether or not to rollback the current transaction. Do not rollback
838** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
839** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000840** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000841**
drh66a51672008-01-03 00:01:23 +0000842** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000843**
drh9cfcf5d2002-01-29 18:41:24 +0000844** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000845** every program. So a jump past the last instruction of the program
846** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000847*/
drh9cbf3422008-01-17 16:22:13 +0000848case OP_Halt: {
drh92f02c32004-09-02 14:57:08 +0000849 p->rc = pOp->p1;
850 p->pc = pc;
851 p->errorAction = pOp->p2;
danielk19772dca4ac2008-01-03 11:50:29 +0000852 if( pOp->p4.z ){
drhf089aa42008-07-08 19:34:06 +0000853 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drh9cfcf5d2002-01-29 18:41:24 +0000854 }
drh92f02c32004-09-02 14:57:08 +0000855 rc = sqlite3VdbeHalt(p);
danielk197701427a62005-01-11 13:02:33 +0000856 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
drh92f02c32004-09-02 14:57:08 +0000857 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000858 p->rc = rc = SQLITE_BUSY;
859 }else{
860 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000861 }
drh900b31e2007-08-28 02:27:51 +0000862 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000863}
drhc61053b2000-06-04 12:58:36 +0000864
drh4c583122008-01-04 22:01:03 +0000865/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000866**
drh9cbf3422008-01-17 16:22:13 +0000867** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000868*/
drh4c583122008-01-04 22:01:03 +0000869case OP_Integer: { /* out2-prerelease */
870 pOut->flags = MEM_Int;
871 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000872 break;
873}
874
drh4c583122008-01-04 22:01:03 +0000875/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000876**
drh66a51672008-01-03 00:01:23 +0000877** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000878** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000879*/
drh4c583122008-01-04 22:01:03 +0000880case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000881 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000882 pOut->flags = MEM_Int;
883 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000884 break;
885}
drh4f26d6c2004-05-26 23:25:30 +0000886
drh4c583122008-01-04 22:01:03 +0000887/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000888**
drh4c583122008-01-04 22:01:03 +0000889** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000890** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000891*/
drh4c583122008-01-04 22:01:03 +0000892case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
893 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000894 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000895 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000896 break;
897}
danielk1977cbb18d22004-05-28 11:37:27 +0000898
drh3c84ddf2008-01-09 02:15:38 +0000899/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000900**
drh66a51672008-01-03 00:01:23 +0000901** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000902** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000903*/
drh4c583122008-01-04 22:01:03 +0000904case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000905 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000906 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000907 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000908
909#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000910 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000911 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
912 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000913 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000914 assert( pOut->zMalloc==pOut->z );
915 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000916 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000917 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000918 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000919 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000920 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000921 }
drh66a51672008-01-03 00:01:23 +0000922 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000923 pOp->p4.z = pOut->z;
924 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000925 }
danielk197793758c82005-01-21 08:13:14 +0000926#endif
drhbb4957f2008-03-20 14:03:29 +0000927 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000928 goto too_big;
929 }
930 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000931}
drhf4479502004-05-27 03:12:53 +0000932
drh4c583122008-01-04 22:01:03 +0000933/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000934**
drh9cbf3422008-01-17 16:22:13 +0000935** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000936*/
drh4c583122008-01-04 22:01:03 +0000937case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000938 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000939 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
940 pOut->z = pOp->p4.z;
941 pOut->n = pOp->p1;
942 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000943 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000944 break;
945}
946
drh4c583122008-01-04 22:01:03 +0000947/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000948**
drh9cbf3422008-01-17 16:22:13 +0000949** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000950*/
drh4c583122008-01-04 22:01:03 +0000951case OP_Null: { /* out2-prerelease */
drhf0863fe2005-06-12 21:35:51 +0000952 break;
953}
954
955
drh9de221d2008-01-05 06:51:30 +0000956/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000957**
drh9de221d2008-01-05 06:51:30 +0000958** P4 points to a blob of data P1 bytes long. Store this
959** blob in register P2. This instruction is not coded directly
danielk1977cbb18d22004-05-28 11:37:27 +0000960** by the compiler. Instead, the compiler layer specifies
961** an OP_HexBlob opcode, with the hex string representation of
drh66a51672008-01-03 00:01:23 +0000962** the blob as P4. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000963** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000964*/
drh4c583122008-01-04 22:01:03 +0000965case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000966 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000967 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000968 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000969 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000970 break;
971}
972
drh08de1492009-02-20 03:55:05 +0000973/* Opcode: Variable P1 P2 P3 P4 *
drh50457892003-09-06 01:10:47 +0000974**
drh08de1492009-02-20 03:55:05 +0000975** Transfer the values of bound parameters P1..P1+P3-1 into registers
976** P2..P2+P3-1.
977**
978** If the parameter is named, then its name appears in P4 and P3==1.
979** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +0000980*/
drh08de1492009-02-20 03:55:05 +0000981case OP_Variable: {
drh856c1032009-06-02 15:21:42 +0000982 int p1; /* Variable to copy from */
983 int p2; /* Register to copy to */
984 int n; /* Number of values left to copy */
985 Mem *pVar; /* Value being transferred */
986
987 p1 = pOp->p1 - 1;
988 p2 = pOp->p2;
989 n = pOp->p3;
990 assert( p1>=0 && p1+n<=p->nVar );
991 assert( p2>=1 && p2+n-1<=p->nMem );
drh08de1492009-02-20 03:55:05 +0000992 assert( pOp->p4.z==0 || pOp->p3==1 );
danielk1977295ba552004-05-19 10:34:51 +0000993
drh08de1492009-02-20 03:55:05 +0000994 while( n-- > 0 ){
drh856c1032009-06-02 15:21:42 +0000995 pVar = &p->aVar[p1++];
drh08de1492009-02-20 03:55:05 +0000996 if( sqlite3VdbeMemTooBig(pVar) ){
997 goto too_big;
998 }
drh856c1032009-06-02 15:21:42 +0000999 pOut = &p->aMem[p2++];
drh08de1492009-02-20 03:55:05 +00001000 sqlite3VdbeMemReleaseExternal(pOut);
1001 pOut->flags = MEM_Null;
1002 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1003 UPDATE_MAX_BLOBSIZE(pOut);
drh023ae032007-05-08 12:12:16 +00001004 }
danielk197793d46752004-05-23 13:30:58 +00001005 break;
1006}
danielk1977295ba552004-05-19 10:34:51 +00001007
drhb21e7c72008-06-22 12:37:57 +00001008/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001009**
drhb21e7c72008-06-22 12:37:57 +00001010** Move the values in register P1..P1+P3-1 over into
1011** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
1012** left holding a NULL. It is an error for register ranges
1013** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001014*/
drhe1349cb2008-04-01 00:36:10 +00001015case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001016 char *zMalloc; /* Holding variable for allocated memory */
1017 int n; /* Number of registers left to copy */
1018 int p1; /* Register to copy from */
1019 int p2; /* Register to copy to */
1020
1021 n = pOp->p3;
1022 p1 = pOp->p1;
1023 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001024 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001025 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001026
1027 pIn1 = &p->aMem[p1];
1028 pOut = &p->aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001029 while( n-- ){
danielk19776ab3a2e2009-02-19 14:39:25 +00001030 assert( pOut<=&p->aMem[p->nMem] );
1031 assert( pIn1<=&p->aMem[p->nMem] );
drhb21e7c72008-06-22 12:37:57 +00001032 zMalloc = pOut->zMalloc;
1033 pOut->zMalloc = 0;
1034 sqlite3VdbeMemMove(pOut, pIn1);
1035 pIn1->zMalloc = zMalloc;
1036 REGISTER_TRACE(p2++, pOut);
1037 pIn1++;
1038 pOut++;
1039 }
drhe1349cb2008-04-01 00:36:10 +00001040 break;
1041}
1042
drhb1fdb2a2008-01-05 04:06:03 +00001043/* Opcode: Copy P1 P2 * * *
1044**
drh9cbf3422008-01-17 16:22:13 +00001045** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001046**
1047** This instruction makes a deep copy of the value. A duplicate
1048** is made of any string or blob constant. See also OP_SCopy.
1049*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001050case OP_Copy: { /* in1 */
drhe1349cb2008-04-01 00:36:10 +00001051 assert( pOp->p2>0 );
1052 assert( pOp->p2<=p->nMem );
1053 pOut = &p->aMem[pOp->p2];
1054 assert( pOut!=pIn1 );
1055 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1056 Deephemeralize(pOut);
1057 REGISTER_TRACE(pOp->p2, pOut);
1058 break;
1059}
1060
drhb1fdb2a2008-01-05 04:06:03 +00001061/* Opcode: SCopy P1 P2 * * *
1062**
drh9cbf3422008-01-17 16:22:13 +00001063** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001064**
1065** This instruction makes a shallow copy of the value. If the value
1066** is a string or blob, then the copy is only a pointer to the
1067** original and hence if the original changes so will the copy.
1068** Worse, if the original is deallocated, the copy becomes invalid.
1069** Thus the program must guarantee that the original will not change
1070** during the lifetime of the copy. Use OP_Copy to make a complete
1071** copy.
1072*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001073case OP_SCopy: { /* in1 */
drh9cbf3422008-01-17 16:22:13 +00001074 REGISTER_TRACE(pOp->p1, pIn1);
1075 assert( pOp->p2>0 );
1076 assert( pOp->p2<=p->nMem );
1077 pOut = &p->aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001078 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001079 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh5b6afba2008-01-05 16:29:28 +00001080 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001081 break;
1082}
drh75897232000-05-29 14:26:00 +00001083
drh9cbf3422008-01-17 16:22:13 +00001084/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001085**
shane21e7feb2008-05-30 15:59:49 +00001086** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001087** results. This opcode causes the sqlite3_step() call to terminate
1088** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1089** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001090** row.
drhd4e70eb2008-01-02 00:34:36 +00001091*/
drh9cbf3422008-01-17 16:22:13 +00001092case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001093 Mem *pMem;
1094 int i;
1095 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001096 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001097 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001098
danielk1977bd434552009-03-18 10:33:00 +00001099 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1100 ** DML statements invoke this opcode to return the number of rows
1101 ** modified to the user. This is the only way that a VM that
1102 ** opens a statement transaction may invoke this opcode.
1103 **
1104 ** In case this is such a statement, close any statement transaction
1105 ** opened by this VM before returning control to the user. This is to
1106 ** ensure that statement-transactions are always nested, not overlapping.
1107 ** If the open statement-transaction is not closed here, then the user
1108 ** may step another VM that opens its own statement transaction. This
1109 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001110 **
1111 ** The statement transaction is never a top-level transaction. Hence
1112 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001113 */
1114 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001115 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1116 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001117 break;
1118 }
1119
drhd4e70eb2008-01-02 00:34:36 +00001120 /* Invalidate all ephemeral cursor row caches */
1121 p->cacheCtr = (p->cacheCtr + 2)|1;
1122
1123 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001124 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001125 ** as side effect.
1126 */
1127 pMem = p->pResultSet = &p->aMem[pOp->p1];
1128 for(i=0; i<pOp->p2; i++){
1129 sqlite3VdbeMemNulTerminate(&pMem[i]);
1130 storeTypeInfo(&pMem[i], encoding);
drh0acb7e42008-06-25 00:12:41 +00001131 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001132 }
drh28039692008-03-17 16:54:01 +00001133 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001134
1135 /* Return SQLITE_ROW
1136 */
drhd4e70eb2008-01-02 00:34:36 +00001137 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001138 rc = SQLITE_ROW;
1139 goto vdbe_return;
1140}
1141
drh5b6afba2008-01-05 16:29:28 +00001142/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001143**
drh5b6afba2008-01-05 16:29:28 +00001144** Add the text in register P1 onto the end of the text in
1145** register P2 and store the result in register P3.
1146** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001147**
1148** P3 = P2 || P1
1149**
1150** It is illegal for P1 and P3 to be the same register. Sometimes,
1151** if P3 is the same register as P2, the implementation is able
1152** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001153*/
drh5b6afba2008-01-05 16:29:28 +00001154case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001155 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001156
danielk1977a7a8e142008-02-13 18:25:27 +00001157 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001158 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001159 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001160 break;
drh5e00f6c2001-09-13 13:46:56 +00001161 }
drha0c06522009-06-17 22:50:41 +00001162 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001163 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001164 Stringify(pIn2, encoding);
1165 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001166 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001167 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001168 }
danielk1977a7a8e142008-02-13 18:25:27 +00001169 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001170 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001171 goto no_mem;
1172 }
danielk1977a7a8e142008-02-13 18:25:27 +00001173 if( pOut!=pIn2 ){
1174 memcpy(pOut->z, pIn2->z, pIn2->n);
1175 }
1176 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1177 pOut->z[nByte] = 0;
1178 pOut->z[nByte+1] = 0;
1179 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001180 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001181 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001182 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001183 break;
1184}
drh75897232000-05-29 14:26:00 +00001185
drh3c84ddf2008-01-09 02:15:38 +00001186/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001187**
drh60a713c2008-01-21 16:22:45 +00001188** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001189** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001190** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001191*/
drh3c84ddf2008-01-09 02:15:38 +00001192/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001193**
drh3c84ddf2008-01-09 02:15:38 +00001194**
shane21e7feb2008-05-30 15:59:49 +00001195** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001196** and store the result in register P3.
1197** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001198*/
drh3c84ddf2008-01-09 02:15:38 +00001199/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001200**
drh60a713c2008-01-21 16:22:45 +00001201** Subtract the value in register P1 from the value in register P2
1202** and store the result in register P3.
1203** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001204*/
drh9cbf3422008-01-17 16:22:13 +00001205/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001206**
drh60a713c2008-01-21 16:22:45 +00001207** Divide the value in register P1 by the value in register P2
1208** and store the result in register P3. If the value in register P2
1209** is zero, then the result is NULL.
1210** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001211*/
drh9cbf3422008-01-17 16:22:13 +00001212/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001213**
drh3c84ddf2008-01-09 02:15:38 +00001214** Compute the remainder after integer division of the value in
1215** register P1 by the value in register P2 and store the result in P3.
1216** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001217** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001218*/
drh5b6afba2008-01-05 16:29:28 +00001219case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1220case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1221case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1222case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1223case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001224 int flags; /* Combined MEM_* flags from both inputs */
1225 i64 iA; /* Integer value of left operand */
1226 i64 iB; /* Integer value of right operand */
1227 double rA; /* Real value of left operand */
1228 double rB; /* Real value of right operand */
1229
drh61669b32008-07-30 13:27:10 +00001230 applyNumericAffinity(pIn1);
1231 applyNumericAffinity(pIn2);
drh5b6afba2008-01-05 16:29:28 +00001232 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001233 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1234 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001235 iA = pIn1->u.i;
1236 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001237 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001238 case OP_Add: iB += iA; break;
1239 case OP_Subtract: iB -= iA; break;
1240 case OP_Multiply: iB *= iA; break;
drhbf4133c2001-10-13 02:59:08 +00001241 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001242 if( iA==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001243 /* Dividing the largest possible negative 64-bit integer (1<<63) by
drh0f050352008-05-09 18:03:13 +00001244 ** -1 returns an integer too large to store in a 64-bit data-type. On
danielk197742d4ef22007-06-26 11:13:25 +00001245 ** some architectures, the value overflows to (1<<63). On others,
1246 ** a SIGFPE is issued. The following statement normalizes this
shane21e7feb2008-05-30 15:59:49 +00001247 ** behavior so that all architectures behave as if integer
1248 ** overflow occurred.
danielk197742d4ef22007-06-26 11:13:25 +00001249 */
drh856c1032009-06-02 15:21:42 +00001250 if( iA==-1 && iB==SMALLEST_INT64 ) iA = 1;
1251 iB /= iA;
drh75897232000-05-29 14:26:00 +00001252 break;
1253 }
drhbf4133c2001-10-13 02:59:08 +00001254 default: {
drh856c1032009-06-02 15:21:42 +00001255 if( iA==0 ) goto arithmetic_result_is_null;
1256 if( iA==-1 ) iA = 1;
1257 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001258 break;
1259 }
drh75897232000-05-29 14:26:00 +00001260 }
drh856c1032009-06-02 15:21:42 +00001261 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001262 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001263 }else{
drh856c1032009-06-02 15:21:42 +00001264 rA = sqlite3VdbeRealValue(pIn1);
1265 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001266 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001267 case OP_Add: rB += rA; break;
1268 case OP_Subtract: rB -= rA; break;
1269 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001270 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001271 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001272 if( rA==(double)0 ) goto arithmetic_result_is_null;
1273 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001274 break;
1275 }
drhbf4133c2001-10-13 02:59:08 +00001276 default: {
shane75ac1de2009-06-09 18:58:52 +00001277 iA = (i64)rA;
1278 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001279 if( iA==0 ) goto arithmetic_result_is_null;
1280 if( iA==-1 ) iA = 1;
1281 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001282 break;
1283 }
drh5e00f6c2001-09-13 13:46:56 +00001284 }
drh856c1032009-06-02 15:21:42 +00001285 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001286 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001287 }
drh856c1032009-06-02 15:21:42 +00001288 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001289 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001290 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001291 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001292 }
drh5e00f6c2001-09-13 13:46:56 +00001293 }
1294 break;
1295
drha05a7222008-01-19 03:35:58 +00001296arithmetic_result_is_null:
1297 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001298 break;
1299}
1300
drh66a51672008-01-03 00:01:23 +00001301/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001302**
drh66a51672008-01-03 00:01:23 +00001303** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001304** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1305** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001306** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001307**
1308** The interface used by the implementation of the aforementioned functions
1309** to retrieve the collation sequence set by this opcode is not available
1310** publicly, only to user functions defined in func.c.
1311*/
drh9cbf3422008-01-17 16:22:13 +00001312case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001313 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001314 break;
1315}
1316
drh98757152008-01-09 23:04:12 +00001317/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001318**
drh66a51672008-01-03 00:01:23 +00001319** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001320** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001321** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001322** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001323**
drh13449892005-09-07 21:22:45 +00001324** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001325** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001326** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001327** whether meta data associated with a user function argument using the
1328** sqlite3_set_auxdata() API may be safely retained until the next
1329** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001330**
drh13449892005-09-07 21:22:45 +00001331** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001332*/
drh0bce8352002-02-28 00:41:10 +00001333case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001334 int i;
drh6810ce62004-01-31 19:22:56 +00001335 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001336 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001337 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001338 int n;
drh1350b032002-02-27 19:00:20 +00001339
drh856c1032009-06-02 15:21:42 +00001340 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001341 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001342 assert( apVal || n==0 );
1343
danielk19776ab3a2e2009-02-19 14:39:25 +00001344 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001345 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh9cbf3422008-01-17 16:22:13 +00001346 pArg = &p->aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001347 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001348 apVal[i] = pArg;
drh8079a0d2006-01-12 17:20:50 +00001349 storeTypeInfo(pArg, encoding);
drh2dcef112008-01-12 19:03:48 +00001350 REGISTER_TRACE(pOp->p2, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001351 }
danielk197751ad0ec2004-05-24 12:39:02 +00001352
drh66a51672008-01-03 00:01:23 +00001353 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1354 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001355 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001356 ctx.pVdbeFunc = 0;
1357 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001358 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001359 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1360 }
1361
danielk1977a7a8e142008-02-13 18:25:27 +00001362 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1363 pOut = &p->aMem[pOp->p3];
drh00706be2004-01-30 14:49:16 +00001364 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001365 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001366 ctx.s.xDel = 0;
1367 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001368
1369 /* The output cell may already have a buffer allocated. Move
1370 ** the pointer to ctx.s so in case the user-function can use
1371 ** the already allocated buffer instead of allocating a new one.
1372 */
1373 sqlite3VdbeMemMove(&ctx.s, pOut);
1374 MemSetTypeFlag(&ctx.s, MEM_Null);
1375
drh8e0a2f92002-02-23 23:45:45 +00001376 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001377 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001378 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00001379 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001380 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001381 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001382 }
danielk19774adee202004-05-08 08:23:19 +00001383 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001384 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk197775eb0162008-03-28 19:16:33 +00001385 if( sqlite3SafetyOn(db) ){
1386 sqlite3VdbeMemRelease(&ctx.s);
1387 goto abort_due_to_misuse;
1388 }
drh17435752007-08-16 04:30:38 +00001389 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001390 /* Even though a malloc() has failed, the implementation of the
1391 ** user function may have called an sqlite3_result_XXX() function
1392 ** to return a value. The following call releases any resources
1393 ** associated with such a value.
1394 **
1395 ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1396 ** fails also (the if(...) statement above). But if people are
1397 ** misusing sqlite, they have bigger problems than a leaked value.
1398 */
1399 sqlite3VdbeMemRelease(&ctx.s);
1400 goto no_mem;
1401 }
danielk19777e18c252004-05-25 11:47:24 +00001402
shane21e7feb2008-05-30 15:59:49 +00001403 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001404 ** immediately call the destructor for any non-static values.
1405 */
1406 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001407 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001408 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001409 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001410 }
1411
drh90669c12006-01-20 15:45:36 +00001412 /* If the function returned an error, throw an exception */
1413 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001414 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001415 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001416 }
1417
drh9cbf3422008-01-17 16:22:13 +00001418 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001419 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001420 sqlite3VdbeMemMove(pOut, &ctx.s);
1421 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001422 goto too_big;
1423 }
drh2dcef112008-01-12 19:03:48 +00001424 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001425 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001426 break;
1427}
1428
drh98757152008-01-09 23:04:12 +00001429/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001430**
drh98757152008-01-09 23:04:12 +00001431** Take the bit-wise AND of the values in register P1 and P2 and
1432** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001433** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001434*/
drh98757152008-01-09 23:04:12 +00001435/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001436**
drh98757152008-01-09 23:04:12 +00001437** Take the bit-wise OR of the values in register P1 and P2 and
1438** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001439** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001440*/
drh98757152008-01-09 23:04:12 +00001441/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001442**
drh98757152008-01-09 23:04:12 +00001443** Shift the integer value in register P2 to the left by the
drh60a713c2008-01-21 16:22:45 +00001444** number of bits specified by the integer in regiser P1.
drh98757152008-01-09 23:04:12 +00001445** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001446** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001447*/
drh98757152008-01-09 23:04:12 +00001448/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001449**
drh98757152008-01-09 23:04:12 +00001450** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001451** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001452** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001453** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001454*/
drh5b6afba2008-01-05 16:29:28 +00001455case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1456case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1457case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1458case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001459 i64 a;
1460 i64 b;
drh6810ce62004-01-31 19:22:56 +00001461
drh5b6afba2008-01-05 16:29:28 +00001462 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001463 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001464 break;
1465 }
drh5b6afba2008-01-05 16:29:28 +00001466 a = sqlite3VdbeIntValue(pIn2);
1467 b = sqlite3VdbeIntValue(pIn1);
drhbf4133c2001-10-13 02:59:08 +00001468 switch( pOp->opcode ){
1469 case OP_BitAnd: a &= b; break;
1470 case OP_BitOr: a |= b; break;
1471 case OP_ShiftLeft: a <<= b; break;
drha05a7222008-01-19 03:35:58 +00001472 default: assert( pOp->opcode==OP_ShiftRight );
1473 a >>= b; break;
drhbf4133c2001-10-13 02:59:08 +00001474 }
drh5b6afba2008-01-05 16:29:28 +00001475 pOut->u.i = a;
danielk1977a7a8e142008-02-13 18:25:27 +00001476 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001477 break;
1478}
1479
drh8558cde2008-01-05 05:20:10 +00001480/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001481**
danielk19770cdc0222008-06-26 18:04:03 +00001482** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001483** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001484**
drh8558cde2008-01-05 05:20:10 +00001485** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001486*/
drh9cbf3422008-01-17 16:22:13 +00001487case OP_AddImm: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001488 sqlite3VdbeMemIntegerify(pIn1);
1489 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001490 break;
1491}
1492
drh9cbf3422008-01-17 16:22:13 +00001493/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001494**
drh9cbf3422008-01-17 16:22:13 +00001495** Force the value in register P1 to be an integer. If the value
1496** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001497** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001498** raise an SQLITE_MISMATCH exception.
1499*/
drh9cbf3422008-01-17 16:22:13 +00001500case OP_MustBeInt: { /* jump, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001501 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1502 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001503 if( pOp->p2==0 ){
1504 rc = SQLITE_MISMATCH;
1505 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001506 }else{
drh17c40292004-07-21 02:53:29 +00001507 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001508 }
drh8aff1012001-12-22 14:49:24 +00001509 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001510 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001511 }
1512 break;
1513}
1514
drh8558cde2008-01-05 05:20:10 +00001515/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001516**
drh2133d822008-01-03 18:44:59 +00001517** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001518**
drh8a512562005-11-14 22:29:05 +00001519** This opcode is used when extracting information from a column that
1520** has REAL affinity. Such column values may still be stored as
1521** integers, for space efficiency, but after extraction we want them
1522** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001523*/
drh9cbf3422008-01-17 16:22:13 +00001524case OP_RealAffinity: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001525 if( pIn1->flags & MEM_Int ){
1526 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001527 }
drh487e2622005-06-25 18:42:14 +00001528 break;
1529}
1530
drh8df447f2005-11-01 15:48:24 +00001531#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001532/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001533**
drh8558cde2008-01-05 05:20:10 +00001534** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001535** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001536** equivalent of printf(). Blob values are unchanged and
1537** are afterwards simply interpreted as text.
1538**
1539** A NULL value is not changed by this routine. It remains NULL.
1540*/
drh9cbf3422008-01-17 16:22:13 +00001541case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh8558cde2008-01-05 05:20:10 +00001542 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001543 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001544 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1545 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1546 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001547 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001548 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001549 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001550 break;
1551}
1552
drh8558cde2008-01-05 05:20:10 +00001553/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001554**
drh8558cde2008-01-05 05:20:10 +00001555** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001556** If the value is numeric, convert it to a string first.
1557** Strings are simply reinterpreted as blobs with no change
1558** to the underlying data.
1559**
1560** A NULL value is not changed by this routine. It remains NULL.
1561*/
drh9cbf3422008-01-17 16:22:13 +00001562case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh8558cde2008-01-05 05:20:10 +00001563 if( pIn1->flags & MEM_Null ) break;
1564 if( (pIn1->flags & MEM_Blob)==0 ){
1565 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001566 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001567 MemSetTypeFlag(pIn1, MEM_Blob);
1568 }else{
1569 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001570 }
drhb7654112008-01-12 12:48:07 +00001571 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001572 break;
1573}
drh8a512562005-11-14 22:29:05 +00001574
drh8558cde2008-01-05 05:20:10 +00001575/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001576**
drh8558cde2008-01-05 05:20:10 +00001577** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001578** integer or a floating-point number.)
1579** If the value is text or blob, try to convert it to an using the
1580** equivalent of atoi() or atof() and store 0 if no such conversion
1581** is possible.
1582**
1583** A NULL value is not changed by this routine. It remains NULL.
1584*/
drh9cbf3422008-01-17 16:22:13 +00001585case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh8558cde2008-01-05 05:20:10 +00001586 if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1587 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001588 }
1589 break;
1590}
1591#endif /* SQLITE_OMIT_CAST */
1592
drh8558cde2008-01-05 05:20:10 +00001593/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001594**
drh8558cde2008-01-05 05:20:10 +00001595** Force the value in register P1 be an integer. If
drh8a512562005-11-14 22:29:05 +00001596** The value is currently a real number, drop its fractional part.
1597** If the value is text or blob, try to convert it to an integer using the
1598** equivalent of atoi() and store 0 if no such conversion is possible.
1599**
1600** A NULL value is not changed by this routine. It remains NULL.
1601*/
drh9cbf3422008-01-17 16:22:13 +00001602case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh8558cde2008-01-05 05:20:10 +00001603 if( (pIn1->flags & MEM_Null)==0 ){
1604 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001605 }
1606 break;
1607}
1608
1609#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001610/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001611**
drh8558cde2008-01-05 05:20:10 +00001612** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001613** If The value is currently an integer, convert it.
1614** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001615** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001616**
1617** A NULL value is not changed by this routine. It remains NULL.
1618*/
drh9cbf3422008-01-17 16:22:13 +00001619case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh8558cde2008-01-05 05:20:10 +00001620 if( (pIn1->flags & MEM_Null)==0 ){
1621 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001622 }
1623 break;
1624}
drh487e2622005-06-25 18:42:14 +00001625#endif /* SQLITE_OMIT_CAST */
1626
drh35573352008-01-08 23:54:25 +00001627/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001628**
drh35573352008-01-08 23:54:25 +00001629** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1630** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001631**
drh35573352008-01-08 23:54:25 +00001632** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1633** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1634** bit is clear then fall thru if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001635**
drh35573352008-01-08 23:54:25 +00001636** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001637** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001638** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001639** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001640** affinity is used. Note that the affinity conversions are stored
1641** back into the input registers P1 and P3. So this opcode can cause
1642** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001643**
1644** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001645** the values are compared. If both values are blobs then memcmp() is
1646** used to determine the results of the comparison. If both values
1647** are text, then the appropriate collating function specified in
1648** P4 is used to do the comparison. If P4 is not specified then
1649** memcmp() is used to compare text string. If both values are
1650** numeric, then a numeric comparison is used. If the two values
1651** are of different types, then numbers are considered less than
1652** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001653**
drh35573352008-01-08 23:54:25 +00001654** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1655** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001656*/
drh9cbf3422008-01-17 16:22:13 +00001657/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001658**
drh35573352008-01-08 23:54:25 +00001659** This works just like the Lt opcode except that the jump is taken if
1660** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001661** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001662*/
drh9cbf3422008-01-17 16:22:13 +00001663/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001664**
drh35573352008-01-08 23:54:25 +00001665** This works just like the Lt opcode except that the jump is taken if
1666** the operands in registers P1 and P3 are equal.
1667** See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001668*/
drh9cbf3422008-01-17 16:22:13 +00001669/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001670**
drh35573352008-01-08 23:54:25 +00001671** This works just like the Lt opcode except that the jump is taken if
1672** the content of register P3 is less than or equal to the content of
1673** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001674*/
drh9cbf3422008-01-17 16:22:13 +00001675/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001676**
drh35573352008-01-08 23:54:25 +00001677** This works just like the Lt opcode except that the jump is taken if
1678** the content of register P3 is greater than the content of
1679** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001680*/
drh9cbf3422008-01-17 16:22:13 +00001681/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001682**
drh35573352008-01-08 23:54:25 +00001683** This works just like the Lt opcode except that the jump is taken if
1684** the content of register P3 is greater than or equal to the content of
1685** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001686*/
drh9cbf3422008-01-17 16:22:13 +00001687case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1688case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1689case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1690case OP_Le: /* same as TK_LE, jump, in1, in3 */
1691case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1692case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
danielk1977a37cdde2004-05-16 11:15:36 +00001693 int flags;
1694 int res;
1695 char affinity;
1696
drh35573352008-01-08 23:54:25 +00001697 flags = pIn1->flags|pIn3->flags;
danielk1977a37cdde2004-05-16 11:15:36 +00001698
danielk1977a37cdde2004-05-16 11:15:36 +00001699 if( flags&MEM_Null ){
drh93a960a2008-07-10 00:32:42 +00001700 /* If either operand is NULL then the result is always NULL.
1701 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1702 */
1703 if( pOp->p5 & SQLITE_STOREP2 ){
1704 pOut = &p->aMem[pOp->p2];
1705 MemSetTypeFlag(pOut, MEM_Null);
1706 REGISTER_TRACE(pOp->p2, pOut);
1707 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1708 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001709 }
drh93a960a2008-07-10 00:32:42 +00001710 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001711 }
1712
drh35573352008-01-08 23:54:25 +00001713 affinity = pOp->p5 & SQLITE_AFF_MASK;
drhe51c44f2004-05-30 20:46:09 +00001714 if( affinity ){
drh35573352008-01-08 23:54:25 +00001715 applyAffinity(pIn1, affinity, encoding);
1716 applyAffinity(pIn3, affinity, encoding);
drhbbce3382008-12-06 16:46:13 +00001717 if( db->mallocFailed ) goto no_mem;
drhe51c44f2004-05-30 20:46:09 +00001718 }
danielk1977a37cdde2004-05-16 11:15:36 +00001719
danielk19772dca4ac2008-01-03 11:50:29 +00001720 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh35573352008-01-08 23:54:25 +00001721 ExpandBlob(pIn1);
1722 ExpandBlob(pIn3);
1723 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
danielk1977a37cdde2004-05-16 11:15:36 +00001724 switch( pOp->opcode ){
1725 case OP_Eq: res = res==0; break;
1726 case OP_Ne: res = res!=0; break;
1727 case OP_Lt: res = res<0; break;
1728 case OP_Le: res = res<=0; break;
1729 case OP_Gt: res = res>0; break;
1730 default: res = res>=0; break;
1731 }
1732
drh35573352008-01-08 23:54:25 +00001733 if( pOp->p5 & SQLITE_STOREP2 ){
1734 pOut = &p->aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00001735 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001736 pOut->u.i = res;
1737 REGISTER_TRACE(pOp->p2, pOut);
1738 }else if( res ){
1739 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001740 }
1741 break;
1742}
drhc9b84a12002-06-20 11:36:48 +00001743
drh0acb7e42008-06-25 00:12:41 +00001744/* Opcode: Permutation * * * P4 *
1745**
shanebe217792009-03-05 04:20:31 +00001746** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001747** of integers in P4.
1748**
1749** The permutation is only valid until the next OP_Permutation, OP_Compare,
1750** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1751** immediately prior to the OP_Compare.
1752*/
1753case OP_Permutation: {
1754 assert( pOp->p4type==P4_INTARRAY );
1755 assert( pOp->p4.ai );
1756 aPermute = pOp->p4.ai;
1757 break;
1758}
1759
drh16ee60f2008-06-20 18:13:25 +00001760/* Opcode: Compare P1 P2 P3 P4 *
1761**
1762** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1763** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1764** the comparison for use by the next OP_Jump instruct.
1765**
drh0acb7e42008-06-25 00:12:41 +00001766** P4 is a KeyInfo structure that defines collating sequences and sort
1767** orders for the comparison. The permutation applies to registers
1768** only. The KeyInfo elements are used sequentially.
1769**
1770** The comparison is a sort comparison, so NULLs compare equal,
1771** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001772** and strings are less than blobs.
1773*/
1774case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001775 int n;
1776 int i;
1777 int p1;
1778 int p2;
1779 const KeyInfo *pKeyInfo;
1780 int idx;
1781 CollSeq *pColl; /* Collating sequence to use on this term */
1782 int bRev; /* True for DESCENDING sort order */
1783
1784 n = pOp->p3;
1785 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001786 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001787 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001788 p1 = pOp->p1;
danielk19776ab3a2e2009-02-19 14:39:25 +00001789 assert( p1>0 && p1+n<=p->nMem+1 );
drh16ee60f2008-06-20 18:13:25 +00001790 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001791 assert( p2>0 && p2+n<=p->nMem+1 );
drh0acb7e42008-06-25 00:12:41 +00001792 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001793 idx = aPermute ? aPermute[i] : i;
drh0acb7e42008-06-25 00:12:41 +00001794 REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
1795 REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001796 assert( i<pKeyInfo->nField );
1797 pColl = pKeyInfo->aColl[i];
1798 bRev = pKeyInfo->aSortOrder[i];
drh0acb7e42008-06-25 00:12:41 +00001799 iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
1800 if( iCompare ){
1801 if( bRev ) iCompare = -iCompare;
1802 break;
1803 }
drh16ee60f2008-06-20 18:13:25 +00001804 }
drh0acb7e42008-06-25 00:12:41 +00001805 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001806 break;
1807}
1808
1809/* Opcode: Jump P1 P2 P3 * *
1810**
1811** Jump to the instruction at address P1, P2, or P3 depending on whether
1812** in the most recent OP_Compare instruction the P1 vector was less than
1813** equal to, or greater than the P2 vector, respectively.
1814*/
drh0acb7e42008-06-25 00:12:41 +00001815case OP_Jump: { /* jump */
1816 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001817 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001818 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001819 pc = pOp->p2 - 1;
1820 }else{
1821 pc = pOp->p3 - 1;
1822 }
1823 break;
1824}
1825
drh5b6afba2008-01-05 16:29:28 +00001826/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001827**
drh5b6afba2008-01-05 16:29:28 +00001828** Take the logical AND of the values in registers P1 and P2 and
1829** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001830**
drh5b6afba2008-01-05 16:29:28 +00001831** If either P1 or P2 is 0 (false) then the result is 0 even if
1832** the other input is NULL. A NULL and true or two NULLs give
1833** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001834*/
drh5b6afba2008-01-05 16:29:28 +00001835/* Opcode: Or P1 P2 P3 * *
1836**
1837** Take the logical OR of the values in register P1 and P2 and
1838** store the answer in register P3.
1839**
1840** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1841** even if the other input is NULL. A NULL and false or two NULLs
1842** give a NULL output.
1843*/
1844case OP_And: /* same as TK_AND, in1, in2, out3 */
1845case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001846 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
1847 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001848
drh5b6afba2008-01-05 16:29:28 +00001849 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001850 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001851 }else{
drh5b6afba2008-01-05 16:29:28 +00001852 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001853 }
drh5b6afba2008-01-05 16:29:28 +00001854 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001855 v2 = 2;
1856 }else{
drh5b6afba2008-01-05 16:29:28 +00001857 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001858 }
1859 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001860 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001861 v1 = and_logic[v1*3+v2];
1862 }else{
drh5b6afba2008-01-05 16:29:28 +00001863 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001864 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001865 }
drhbb113512002-05-27 01:04:51 +00001866 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001867 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001868 }else{
drh5b6afba2008-01-05 16:29:28 +00001869 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001870 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001871 }
drh5e00f6c2001-09-13 13:46:56 +00001872 break;
1873}
1874
drhe99fa2a2008-12-15 15:27:51 +00001875/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001876**
drhe99fa2a2008-12-15 15:27:51 +00001877** Interpret the value in register P1 as a boolean value. Store the
1878** boolean complement in register P2. If the value in register P1 is
1879** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001880*/
drh9cbf3422008-01-17 16:22:13 +00001881case OP_Not: { /* same as TK_NOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001882 pOut = &p->aMem[pOp->p2];
1883 if( pIn1->flags & MEM_Null ){
1884 sqlite3VdbeMemSetNull(pOut);
1885 }else{
1886 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1887 }
drh5e00f6c2001-09-13 13:46:56 +00001888 break;
1889}
1890
drhe99fa2a2008-12-15 15:27:51 +00001891/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00001892**
drhe99fa2a2008-12-15 15:27:51 +00001893** Interpret the content of register P1 as an integer. Store the
1894** ones-complement of the P1 value into register P2. If P1 holds
1895** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00001896*/
drh9cbf3422008-01-17 16:22:13 +00001897case OP_BitNot: { /* same as TK_BITNOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001898 pOut = &p->aMem[pOp->p2];
1899 if( pIn1->flags & MEM_Null ){
1900 sqlite3VdbeMemSetNull(pOut);
1901 }else{
1902 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1903 }
drhbf4133c2001-10-13 02:59:08 +00001904 break;
1905}
1906
drh3c84ddf2008-01-09 02:15:38 +00001907/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001908**
drh3c84ddf2008-01-09 02:15:38 +00001909** Jump to P2 if the value in register P1 is true. The value is
1910** is considered true if it is numeric and non-zero. If the value
1911** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00001912*/
drh3c84ddf2008-01-09 02:15:38 +00001913/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00001914**
drh3c84ddf2008-01-09 02:15:38 +00001915** Jump to P2 if the value in register P1 is False. The value is
1916** is considered true if it has a numeric value of zero. If the value
1917** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00001918*/
drh9cbf3422008-01-17 16:22:13 +00001919case OP_If: /* jump, in1 */
1920case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00001921 int c;
drh3c84ddf2008-01-09 02:15:38 +00001922 if( pIn1->flags & MEM_Null ){
1923 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00001924 }else{
drhba0232a2005-06-06 17:27:19 +00001925#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00001926 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00001927#else
drh3c84ddf2008-01-09 02:15:38 +00001928 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00001929#endif
drhf5905aa2002-05-26 20:54:33 +00001930 if( pOp->opcode==OP_IfNot ) c = !c;
1931 }
drh3c84ddf2008-01-09 02:15:38 +00001932 if( c ){
1933 pc = pOp->p2-1;
1934 }
drh5e00f6c2001-09-13 13:46:56 +00001935 break;
1936}
1937
drh830ecf92009-06-18 00:41:55 +00001938/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00001939**
drh830ecf92009-06-18 00:41:55 +00001940** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00001941*/
drh9cbf3422008-01-17 16:22:13 +00001942case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh830ecf92009-06-18 00:41:55 +00001943 if( (pIn1->flags & MEM_Null)!=0 ){
1944 pc = pOp->p2 - 1;
1945 }
drh477df4b2008-01-05 18:48:24 +00001946 break;
1947}
1948
drh98757152008-01-09 23:04:12 +00001949/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001950**
drh6a288a32008-01-07 19:20:24 +00001951** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00001952*/
drh9cbf3422008-01-17 16:22:13 +00001953case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh6a288a32008-01-07 19:20:24 +00001954 if( (pIn1->flags & MEM_Null)==0 ){
1955 pc = pOp->p2 - 1;
1956 }
drh5e00f6c2001-09-13 13:46:56 +00001957 break;
1958}
1959
danielk1977cd3e8f72008-03-25 09:47:35 +00001960/* Opcode: SetNumColumns * P2 * * *
danielk1977b4964b72004-05-18 01:23:38 +00001961**
danielk1977cd3e8f72008-03-25 09:47:35 +00001962** This opcode sets the number of columns for the cursor opened by the
1963** following instruction to P2.
danielk1977b4964b72004-05-18 01:23:38 +00001964**
danielk1977cd3e8f72008-03-25 09:47:35 +00001965** An OP_SetNumColumns is only useful if it occurs immediately before
1966** one of the following opcodes:
danielk1977ac171782005-02-05 06:49:54 +00001967**
danielk1977cd3e8f72008-03-25 09:47:35 +00001968** OpenRead
1969** OpenWrite
1970** OpenPseudo
1971**
1972** If the OP_Column opcode is to be executed on a cursor, then
1973** this opcode must be present immediately before the opcode that
1974** opens the cursor.
danielk1977b4964b72004-05-18 01:23:38 +00001975*/
danielk1977d336e222009-02-20 10:58:41 +00001976#if 0
drh9cbf3422008-01-17 16:22:13 +00001977case OP_SetNumColumns: {
danielk1977b4964b72004-05-18 01:23:38 +00001978 break;
1979}
danielk1977d336e222009-02-20 10:58:41 +00001980#endif
danielk1977b4964b72004-05-18 01:23:38 +00001981
danielk197760585dd2008-01-03 08:08:40 +00001982/* Opcode: Column P1 P2 P3 P4 *
danielk1977192ac1d2004-05-10 07:17:30 +00001983**
danielk1977cfcdaef2004-05-12 07:33:33 +00001984** Interpret the data that cursor P1 points to as a structure built using
1985** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00001986** information about the format of the data.) Extract the P2-th column
1987** from this record. If there are less that (P2+1)
1988** values in the record, extract a NULL.
1989**
drh9cbf3422008-01-17 16:22:13 +00001990** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00001991**
danielk19771f4aa332008-01-03 09:51:55 +00001992** If the column contains fewer than P2 fields, then extract a NULL. Or,
1993** if the P4 argument is a P4_MEM use the value of the P4 argument as
1994** the result.
danielk1977192ac1d2004-05-10 07:17:30 +00001995*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001996case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00001997 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00001998 i64 payloadSize64; /* Number of bytes in the record */
1999 int p1; /* P1 value of the opcode */
2000 int p2; /* column number to retrieve */
2001 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002002 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002003 BtCursor *pCrsr; /* The BTree cursor */
2004 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2005 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002006 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002007 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002008 int i; /* Loop counter */
2009 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002010 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002011 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002012 u8 *zIdx; /* Index into header */
2013 u8 *zEndHdr; /* Pointer to first byte after the header */
2014 u32 offset; /* Offset into the data */
2015 u64 offset64; /* 64-bit offset. 64 bits needed to catch overflow */
2016 int szHdr; /* Size of the header size field at start of record */
2017 int avail; /* Number of bytes of available data */
danielk1977192ac1d2004-05-10 07:17:30 +00002018
drh856c1032009-06-02 15:21:42 +00002019
2020 p1 = pOp->p1;
2021 p2 = pOp->p2;
2022 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002023 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002024 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002025 assert( pOp->p3>0 && pOp->p3<=p->nMem );
2026 pDest = &p->aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00002027 MemSetTypeFlag(pDest, MEM_Null);
danielk1977cfcdaef2004-05-12 07:33:33 +00002028
drhe61cffc2004-06-12 18:12:15 +00002029 /* This block sets the variable payloadSize to be the total number of
2030 ** bytes in the record.
2031 **
2032 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002033 ** The complete record text is always available for pseudo-tables
2034 ** If the record is stored in a cursor, the complete record text
2035 ** might be available in the pC->aRow cache. Or it might not be.
2036 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002037 **
2038 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002039 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002040 */
drhb73857f2006-03-17 00:25:59 +00002041 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00002042 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002043#ifndef SQLITE_OMIT_VIRTUALTABLE
2044 assert( pC->pVtabCursor==0 );
2045#endif
drhb73857f2006-03-17 00:25:59 +00002046 if( pC->pCursor!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002047 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002048 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002049 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002050 zRec = 0;
2051 pCrsr = pC->pCursor;
2052 if( pC->nullRow ){
2053 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002054 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002055 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002056 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002057 }else if( pC->isIndex ){
danielk1977192ac1d2004-05-10 07:17:30 +00002058 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drhaa736092009-06-22 00:55:30 +00002059 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2060 ** payload size, so it is impossible for payloadSize64 to be
2061 ** larger than 32 bits. */
2062 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002063 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002064 }else{
drh35cd6432009-06-05 14:17:21 +00002065 sqlite3BtreeDataSize(pCrsr, &payloadSize);
danielk1977192ac1d2004-05-10 07:17:30 +00002066 }
drhd3194f52004-05-27 19:59:32 +00002067 nField = pC->nField;
drha05a7222008-01-19 03:35:58 +00002068 }else{
2069 assert( pC->pseudoTable );
drhe61cffc2004-06-12 18:12:15 +00002070 /* The record is the sole entry of a pseudo-table */
danielk1977192ac1d2004-05-10 07:17:30 +00002071 payloadSize = pC->nData;
2072 zRec = pC->pData;
drh76873ab2006-01-07 18:48:26 +00002073 pC->cacheStatus = CACHE_STALE;
danielk1977192ac1d2004-05-10 07:17:30 +00002074 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00002075 nField = pC->nField;
danielk1977f7df9cc2004-06-16 12:02:47 +00002076 pCrsr = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002077 }
2078
drh9cbf3422008-01-17 16:22:13 +00002079 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002080 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002081 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002082 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002083 }
drh35cd6432009-06-05 14:17:21 +00002084 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2085 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002086 goto too_big;
2087 }
danielk1977192ac1d2004-05-10 07:17:30 +00002088
drhd3194f52004-05-27 19:59:32 +00002089 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002090
drh9188b382004-05-14 21:12:22 +00002091 /* Read and parse the table header. Store the results of the parse
2092 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002093 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002094 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002095 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002096 aOffset = pC->aOffset;
2097 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002098 assert(aType);
drh856c1032009-06-02 15:21:42 +00002099 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002100 pC->aOffset = aOffset = &aType[nField];
2101 pC->payloadSize = payloadSize;
2102 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002103
drhd3194f52004-05-27 19:59:32 +00002104 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002105 if( zRec ){
2106 zData = zRec;
2107 }else{
drhf0863fe2005-06-12 21:35:51 +00002108 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002109 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002110 }else{
drhe51c44f2004-05-30 20:46:09 +00002111 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002112 }
drhe61cffc2004-06-12 18:12:15 +00002113 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2114 ** save the payload in the pC->aRow cache. That will save us from
2115 ** having to make additional calls to fetch the content portion of
2116 ** the record.
2117 */
drh35cd6432009-06-05 14:17:21 +00002118 assert( avail>=0 );
2119 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002120 zRec = zData;
2121 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002122 }else{
2123 pC->aRow = 0;
2124 }
drhd3194f52004-05-27 19:59:32 +00002125 }
drh588f5bc2007-01-02 18:41:54 +00002126 /* The following assert is true in all cases accept when
2127 ** the database file has been corrupted externally.
2128 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002129 szHdr = getVarint32((u8*)zData, offset);
2130
2131 /* Make sure a corrupt database has not given us an oversize header.
2132 ** Do this now to avoid an oversize memory allocation.
2133 **
2134 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2135 ** types use so much data space that there can only be 4096 and 32 of
2136 ** them, respectively. So the maximum header length results from a
2137 ** 3-byte type for each of the maximum of 32768 columns plus three
2138 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2139 */
2140 if( offset > 98307 ){
2141 rc = SQLITE_CORRUPT_BKPT;
2142 goto op_column_out;
2143 }
2144
2145 /* Compute in len the number of bytes of data we need to read in order
2146 ** to get nField type values. offset is an upper bound on this. But
2147 ** nField might be significantly less than the true number of columns
2148 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2149 ** We want to minimize len in order to limit the size of the memory
2150 ** allocation, especially if a corrupt database file has caused offset
2151 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2152 ** still exceed Robson memory allocation limits on some configurations.
2153 ** On systems that cannot tolerate large memory allocations, nField*5+3
2154 ** will likely be much smaller since nField will likely be less than
2155 ** 20 or so. This insures that Robson memory allocation limits are
2156 ** not exceeded even for corrupt database files.
2157 */
2158 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002159 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002160
2161 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2162 ** record header in most cases. But they will fail to get the complete
2163 ** record header if the record header does not fit on a single page
2164 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2165 ** acquire the complete header text.
2166 */
drh35cd6432009-06-05 14:17:21 +00002167 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002168 sMem.flags = 0;
2169 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002170 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002171 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002172 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002173 }
drhb6f54522004-05-20 02:42:16 +00002174 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002175 }
drh35cd6432009-06-05 14:17:21 +00002176 zEndHdr = (u8 *)&zData[len];
2177 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002178
drhd3194f52004-05-27 19:59:32 +00002179 /* Scan the header and use it to fill in the aType[] and aOffset[]
2180 ** arrays. aType[i] will contain the type integer for the i-th
2181 ** column and aOffset[i] will contain the offset from the beginning
2182 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002183 */
drh35cd6432009-06-05 14:17:21 +00002184 offset64 = offset;
danielk1977dedf45b2006-01-13 17:12:01 +00002185 for(i=0; i<nField; i++){
2186 if( zIdx<zEndHdr ){
drh35cd6432009-06-05 14:17:21 +00002187 aOffset[i] = (u32)offset64;
shane3f8d5cf2008-04-24 19:15:09 +00002188 zIdx += getVarint32(zIdx, aType[i]);
drh35cd6432009-06-05 14:17:21 +00002189 offset64 += sqlite3VdbeSerialTypeLen(aType[i]);
danielk1977dedf45b2006-01-13 17:12:01 +00002190 }else{
2191 /* If i is less that nField, then there are less fields in this
2192 ** record than SetNumColumns indicated there are columns in the
2193 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002194 ** the record to 0. This tells code below to store a NULL
2195 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002196 */
2197 aOffset[i] = 0;
2198 }
drh9188b382004-05-14 21:12:22 +00002199 }
danielk19775f096132008-03-28 15:44:09 +00002200 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002201 sMem.flags = MEM_Null;
2202
danielk19779792eef2006-01-13 15:58:43 +00002203 /* If we have read more header data than was contained in the header,
2204 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002205 ** record, or if the end of the last field appears to be before the end
2206 ** of the record (when all fields present), then we must be dealing
2207 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002208 */
drh35cd6432009-06-05 14:17:21 +00002209 if( (zIdx > zEndHdr)|| (offset64 > payloadSize)
2210 || (zIdx==zEndHdr && offset64!=(u64)payloadSize) ){
drh49285702005-09-17 15:20:26 +00002211 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002212 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002213 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002214 }
danielk1977192ac1d2004-05-10 07:17:30 +00002215
danielk197736963fd2005-02-19 08:18:05 +00002216 /* Get the column information. If aOffset[p2] is non-zero, then
2217 ** deserialize the value from the record. If aOffset[p2] is zero,
2218 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002219 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002220 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002221 */
danielk197736963fd2005-02-19 08:18:05 +00002222 if( aOffset[p2] ){
2223 assert( rc==SQLITE_OK );
2224 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002225 sqlite3VdbeMemReleaseExternal(pDest);
2226 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002227 }else{
2228 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002229 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002230 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002231 if( rc!=SQLITE_OK ){
2232 goto op_column_out;
2233 }
2234 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002235 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002236 }
drhd4e70eb2008-01-02 00:34:36 +00002237 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002238 }else{
danielk197760585dd2008-01-03 08:08:40 +00002239 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002240 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002241 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002242 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002243 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002244 }
drhfebe1062004-08-28 18:17:48 +00002245
2246 /* If we dynamically allocated space to hold the data (in the
2247 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002248 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002249 ** This prevents a memory copy.
2250 */
danielk19775f096132008-03-28 15:44:09 +00002251 if( sMem.zMalloc ){
2252 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002253 assert( !(pDest->flags & MEM_Dyn) );
2254 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2255 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002256 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002257 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002258 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002259 }
drhfebe1062004-08-28 18:17:48 +00002260
drhd4e70eb2008-01-02 00:34:36 +00002261 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002262
danielk19773c9cc8d2005-01-17 03:40:08 +00002263op_column_out:
drhb7654112008-01-12 12:48:07 +00002264 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002265 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002266 break;
2267}
2268
danielk1977751de562008-04-18 09:01:15 +00002269/* Opcode: Affinity P1 P2 * P4 *
2270**
2271** Apply affinities to a range of P2 registers starting with P1.
2272**
2273** P4 is a string that is P2 characters long. The nth character of the
2274** string indicates the column affinity that should be used for the nth
2275** memory cell in the range.
2276*/
2277case OP_Affinity: {
drh856c1032009-06-02 15:21:42 +00002278 char *zAffinity; /* The affinity to be applied */
2279 Mem *pData0; /* First register to which to apply affinity */
2280 Mem *pLast; /* Last register to which to apply affinity */
2281 Mem *pRec; /* Current register */
danielk1977751de562008-04-18 09:01:15 +00002282
drh856c1032009-06-02 15:21:42 +00002283 zAffinity = pOp->p4.z;
2284 pData0 = &p->aMem[pOp->p1];
2285 pLast = &pData0[pOp->p2-1];
danielk1977751de562008-04-18 09:01:15 +00002286 for(pRec=pData0; pRec<=pLast; pRec++){
danielk1977b790c6c2008-04-18 10:25:24 +00002287 ExpandBlob(pRec);
danielk1977751de562008-04-18 09:01:15 +00002288 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2289 }
2290 break;
2291}
2292
drh1db639c2008-01-17 02:36:28 +00002293/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002294**
drh1db639c2008-01-17 02:36:28 +00002295** Convert P2 registers beginning with P1 into a single entry
drh7a224de2004-06-02 01:22:02 +00002296** suitable for use as a data record in a database table or as a key
shane21e7feb2008-05-30 15:59:49 +00002297** in an index. The details of the format are irrelevant as long as
drh1e968a02008-03-25 00:22:21 +00002298** the OP_Column opcode can decode the record later.
2299** Refer to source code comments for the details of the record
drh7a224de2004-06-02 01:22:02 +00002300** format.
2301**
danielk1977751de562008-04-18 09:01:15 +00002302** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002303** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002304** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002305**
drh8a512562005-11-14 22:29:05 +00002306** The mapping from character to affinity is given by the SQLITE_AFF_
2307** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002308**
drh66a51672008-01-03 00:01:23 +00002309** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002310*/
drh1db639c2008-01-17 02:36:28 +00002311case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002312 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2313 Mem *pRec; /* The new record */
2314 u64 nData; /* Number of bytes of data space */
2315 int nHdr; /* Number of bytes of header space */
2316 i64 nByte; /* Data space required for this record */
2317 int nZero; /* Number of zero bytes at the end of the record */
2318 int nVarint; /* Number of bytes in a varint */
2319 u32 serial_type; /* Type field */
2320 Mem *pData0; /* First field to be combined into the record */
2321 Mem *pLast; /* Last field of the record */
2322 int nField; /* Number of fields in the record */
2323 char *zAffinity; /* The affinity string for the record */
2324 int file_format; /* File format to use for encoding */
2325 int i; /* Space used in zNewRecord[] */
2326 int len; /* Length of a field */
2327
drhf3218fe2004-05-28 08:21:02 +00002328 /* Assuming the record contains N fields, the record format looks
2329 ** like this:
2330 **
drh7a224de2004-06-02 01:22:02 +00002331 ** ------------------------------------------------------------------------
2332 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2333 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002334 **
drh9cbf3422008-01-17 16:22:13 +00002335 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2336 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002337 **
2338 ** Each type field is a varint representing the serial type of the
2339 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002340 ** hdr-size field is also a varint which is the offset from the beginning
2341 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002342 */
drh856c1032009-06-02 15:21:42 +00002343 nData = 0; /* Number of bytes of data space */
2344 nHdr = 0; /* Number of bytes of header space */
2345 nByte = 0; /* Data space required for this record */
2346 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002347 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002348 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002349 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drh1db639c2008-01-17 02:36:28 +00002350 pData0 = &p->aMem[nField];
2351 nField = pOp->p2;
2352 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002353 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002354
drhf3218fe2004-05-28 08:21:02 +00002355 /* Loop through the elements that will make up the record to figure
2356 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002357 */
drha2a49dc2008-01-02 14:28:13 +00002358 for(pRec=pData0; pRec<=pLast; pRec++){
drhd3d39e92004-05-20 22:16:29 +00002359 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002360 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002361 }
danielk1977d908f5a2007-05-11 07:08:28 +00002362 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002363 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002364 }
drhd946db02005-12-29 19:23:06 +00002365 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002366 len = sqlite3VdbeSerialTypeLen(serial_type);
2367 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002368 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002369 if( pRec->flags & MEM_Zero ){
2370 /* Only pure zero-filled BLOBs can be input to this Opcode.
2371 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002372 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002373 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002374 nZero = 0;
2375 }
danielk19778d059842004-05-12 11:24:02 +00002376 }
danielk19773d1bfea2004-05-14 11:00:53 +00002377
drhf3218fe2004-05-28 08:21:02 +00002378 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002379 nHdr += nVarint = sqlite3VarintLen(nHdr);
2380 if( nVarint<sqlite3VarintLen(nHdr) ){
2381 nHdr++;
2382 }
drhfdf972a2007-05-02 13:30:27 +00002383 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002384 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002385 goto too_big;
2386 }
drhf3218fe2004-05-28 08:21:02 +00002387
danielk1977a7a8e142008-02-13 18:25:27 +00002388 /* Make sure the output register has a buffer large enough to store
2389 ** the new record. The output register (pOp->p3) is not allowed to
2390 ** be one of the input registers (because the following call to
2391 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2392 */
2393 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2394 pOut = &p->aMem[pOp->p3];
drh9c1905f2008-12-10 22:32:56 +00002395 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002396 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002397 }
danielk1977a7a8e142008-02-13 18:25:27 +00002398 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002399
2400 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002401 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002402 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002403 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002404 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002405 }
drha2a49dc2008-01-02 14:28:13 +00002406 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002407 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002408 }
drhfdf972a2007-05-02 13:30:27 +00002409 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002410
drh9cbf3422008-01-17 16:22:13 +00002411 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002412 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002413 pOut->flags = MEM_Blob | MEM_Dyn;
2414 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002415 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002416 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002417 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002418 }
drh477df4b2008-01-05 18:48:24 +00002419 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002420 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002421 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002422 break;
2423}
2424
danielk1977a5533162009-02-24 10:01:51 +00002425/* Opcode: Count P1 P2 * * *
2426**
2427** Store the number of entries (an integer value) in the table or index
2428** opened by cursor P1 in register P2
2429*/
2430#ifndef SQLITE_OMIT_BTREECOUNT
2431case OP_Count: { /* out2-prerelease */
2432 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002433 BtCursor *pCrsr;
2434
2435 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh818e39a2009-04-02 20:27:28 +00002436 if( pCrsr ){
2437 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2438 }else{
2439 nEntry = 0;
2440 }
danielk1977a5533162009-02-24 10:01:51 +00002441 pOut->flags = MEM_Int;
2442 pOut->u.i = nEntry;
2443 break;
2444}
2445#endif
2446
drh98757152008-01-09 23:04:12 +00002447/* Opcode: Statement P1 * * * *
drh663fc632002-02-02 18:49:19 +00002448**
drh7f0f12e2004-05-21 13:39:50 +00002449** Begin an individual statement transaction which is part of a larger
drh82ed1e52008-04-25 12:25:42 +00002450** transaction. This is needed so that the statement
drh7f0f12e2004-05-21 13:39:50 +00002451** can be rolled back after an error without having to roll back the
2452** entire transaction. The statement transaction will automatically
2453** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002454**
drh82ed1e52008-04-25 12:25:42 +00002455** If the database connection is currently in autocommit mode (that
2456** is to say, if it is in between BEGIN and COMMIT)
2457** and if there are no other active statements on the same database
2458** connection, then this operation is a no-op. No statement transaction
2459** is needed since any error can use the normal ROLLBACK process to
2460** undo changes.
2461**
2462** If a statement transaction is started, then a statement journal file
2463** will be allocated and initialized.
2464**
drh7f0f12e2004-05-21 13:39:50 +00002465** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002466** database file has an index of 0 and the file used for temporary tables
2467** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002468*/
drh9cbf3422008-01-17 16:22:13 +00002469case OP_Statement: {
drh856c1032009-06-02 15:21:42 +00002470 Btree *pBt;
drha05a7222008-01-19 03:35:58 +00002471 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
drh653b82a2009-06-22 11:10:47 +00002472 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2473 assert( db->aDb[pOp->p1].pBt!=0 );
2474 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002475 assert( sqlite3BtreeIsInTrans(pBt) );
drh653b82a2009-06-22 11:10:47 +00002476 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
danielk1977bd434552009-03-18 10:33:00 +00002477 if( p->iStatement==0 ){
2478 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2479 db->nStatement++;
2480 p->iStatement = db->nSavepoint + db->nStatement;
danielk19771d850a72004-05-31 08:26:49 +00002481 }
danielk1977bd434552009-03-18 10:33:00 +00002482 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
danielk19771d850a72004-05-31 08:26:49 +00002483 }
2484 break;
2485}
2486
danielk1977fd7f0452008-12-17 17:30:26 +00002487/* Opcode: Savepoint P1 * * P4 *
2488**
2489** Open, release or rollback the savepoint named by parameter P4, depending
2490** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2491** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2492*/
2493case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002494 int p1; /* Value of P1 operand */
2495 char *zName; /* Name of savepoint */
2496 int nName;
2497 Savepoint *pNew;
2498 Savepoint *pSavepoint;
2499 Savepoint *pTmp;
2500 int iSavepoint;
2501 int ii;
2502
2503 p1 = pOp->p1;
2504 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002505
2506 /* Assert that the p1 parameter is valid. Also that if there is no open
2507 ** transaction, then there cannot be any savepoints.
2508 */
2509 assert( db->pSavepoint==0 || db->autoCommit==0 );
2510 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2511 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2512 assert( checkSavepointCount(db) );
2513
2514 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002515 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002516 /* A new savepoint cannot be created if there are active write
2517 ** statements (i.e. open read/write incremental blob handles).
2518 */
2519 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2520 "SQL statements in progress");
2521 rc = SQLITE_BUSY;
2522 }else{
drh856c1032009-06-02 15:21:42 +00002523 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002524
2525 /* Create a new savepoint structure. */
2526 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2527 if( pNew ){
2528 pNew->zName = (char *)&pNew[1];
2529 memcpy(pNew->zName, zName, nName+1);
2530
2531 /* If there is no open transaction, then mark this as a special
2532 ** "transaction savepoint". */
2533 if( db->autoCommit ){
2534 db->autoCommit = 0;
2535 db->isTransactionSavepoint = 1;
2536 }else{
2537 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002538 }
danielk1977fd7f0452008-12-17 17:30:26 +00002539
2540 /* Link the new savepoint into the database handle's list. */
2541 pNew->pNext = db->pSavepoint;
2542 db->pSavepoint = pNew;
2543 }
2544 }
2545 }else{
drh856c1032009-06-02 15:21:42 +00002546 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002547
2548 /* Find the named savepoint. If there is no such savepoint, then an
2549 ** an error is returned to the user. */
2550 for(
drh856c1032009-06-02 15:21:42 +00002551 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002552 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002553 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002554 ){
2555 iSavepoint++;
2556 }
2557 if( !pSavepoint ){
2558 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2559 rc = SQLITE_ERROR;
2560 }else if(
2561 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2562 ){
2563 /* It is not possible to release (commit) a savepoint if there are
2564 ** active write statements. It is not possible to rollback a savepoint
2565 ** if there are any active statements at all.
2566 */
2567 sqlite3SetString(&p->zErrMsg, db,
2568 "cannot %s savepoint - SQL statements in progress",
2569 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2570 );
2571 rc = SQLITE_BUSY;
2572 }else{
2573
2574 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002575 ** and this is a RELEASE command, then the current transaction
2576 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002577 */
2578 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2579 if( isTransaction && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002580 db->autoCommit = 1;
2581 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2582 p->pc = pc;
2583 db->autoCommit = 0;
2584 p->rc = rc = SQLITE_BUSY;
2585 goto vdbe_return;
2586 }
danielk197734cf35d2008-12-18 18:31:38 +00002587 db->isTransactionSavepoint = 0;
2588 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002589 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002590 iSavepoint = db->nSavepoint - iSavepoint - 1;
2591 for(ii=0; ii<db->nDb; ii++){
2592 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2593 if( rc!=SQLITE_OK ){
2594 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002595 }
danielk1977fd7f0452008-12-17 17:30:26 +00002596 }
drh9f0bbf92009-01-02 21:08:09 +00002597 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002598 sqlite3ExpirePreparedStatements(db);
2599 sqlite3ResetInternalSchema(db, 0);
2600 }
2601 }
2602
2603 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2604 ** savepoints nested inside of the savepoint being operated on. */
2605 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002606 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002607 db->pSavepoint = pTmp->pNext;
2608 sqlite3DbFree(db, pTmp);
2609 db->nSavepoint--;
2610 }
2611
2612 /* If it is a RELEASE, then destroy the savepoint being operated on too */
2613 if( p1==SAVEPOINT_RELEASE ){
2614 assert( pSavepoint==db->pSavepoint );
2615 db->pSavepoint = pSavepoint->pNext;
2616 sqlite3DbFree(db, pSavepoint);
2617 if( !isTransaction ){
2618 db->nSavepoint--;
2619 }
2620 }
2621 }
2622 }
2623
2624 break;
2625}
2626
drh98757152008-01-09 23:04:12 +00002627/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002628**
2629** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002630** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002631** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2632** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002633**
2634** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002635*/
drh9cbf3422008-01-17 16:22:13 +00002636case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002637 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002638 int iRollback;
drh856c1032009-06-02 15:21:42 +00002639 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002640
drh856c1032009-06-02 15:21:42 +00002641 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002642 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002643 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002644 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002645 assert( desiredAutoCommit==1 || iRollback==0 );
drh92f02c32004-09-02 14:57:08 +00002646 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002647
shane68c02732009-06-09 18:14:18 +00002648 if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){
drhad4a4b82008-11-05 16:37:34 +00002649 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002650 ** still running, and a transaction is active, return an error indicating
2651 ** that the other VMs must complete first.
2652 */
drhad4a4b82008-11-05 16:37:34 +00002653 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2654 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002655 rc = SQLITE_BUSY;
drh9eb8cbe2009-06-19 22:23:41 +00002656 }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){
drhad4a4b82008-11-05 16:37:34 +00002657 /* If this instruction implements a COMMIT and other VMs are writing
2658 ** return an error indicating that the other VMs must complete first.
2659 */
2660 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2661 "SQL statements in progress");
2662 rc = SQLITE_BUSY;
2663 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002664 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002665 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002666 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002667 db->autoCommit = 1;
2668 }else{
shane7d3846a2008-12-11 02:58:26 +00002669 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002670 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002671 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002672 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002673 p->rc = rc = SQLITE_BUSY;
2674 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002675 }
danielk19771d850a72004-05-31 08:26:49 +00002676 }
danielk1977bd434552009-03-18 10:33:00 +00002677 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002678 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002679 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002680 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002681 }else{
drh900b31e2007-08-28 02:27:51 +00002682 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002683 }
drh900b31e2007-08-28 02:27:51 +00002684 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002685 }else{
drhf089aa42008-07-08 19:34:06 +00002686 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002687 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002688 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002689 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002690
2691 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002692 }
2693 break;
2694}
2695
drh98757152008-01-09 23:04:12 +00002696/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002697**
2698** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002699** opcode is encountered. Depending on the ON CONFLICT setting, the
2700** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002701**
drh001bbcb2003-03-19 03:14:00 +00002702** P1 is the index of the database file on which the transaction is
2703** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002704** file used for temporary tables. Indices of 2 or more are used for
2705** attached databases.
drhcabb0812002-09-14 13:47:32 +00002706**
drh80242052004-06-09 00:48:12 +00002707** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002708** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002709** other process can start another write transaction while this transaction is
2710** underway. Starting a write transaction also creates a rollback journal. A
2711** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002712** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2713** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002714**
2715** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002716*/
drh9cbf3422008-01-17 16:22:13 +00002717case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002718 Btree *pBt;
2719
drh653b82a2009-06-22 11:10:47 +00002720 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2721 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
2722 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002723
danielk197724162fe2004-06-04 06:22:00 +00002724 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002725 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002726 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002727 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002728 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002729 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002730 }
danielk19772ef68482008-07-07 17:13:08 +00002731 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
danielk197724162fe2004-06-04 06:22:00 +00002732 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002733 }
drhb86ccfb2003-01-28 23:13:10 +00002734 }
drh5e00f6c2001-09-13 13:46:56 +00002735 break;
2736}
2737
drhb1fdb2a2008-01-05 04:06:03 +00002738/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002739**
drh9cbf3422008-01-17 16:22:13 +00002740** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002741** P3==1 is the schema version. P3==2 is the database format.
2742** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002743** the main database file and P1==1 is the database file used to store
2744** temporary tables.
drh4a324312001-12-21 14:30:42 +00002745**
drh50e5dad2001-09-15 00:57:28 +00002746** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002747** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002748** executing this instruction.
2749*/
drh4c583122008-01-04 22:01:03 +00002750case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002751 int iMeta;
drh856c1032009-06-02 15:21:42 +00002752 int iDb;
2753 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00002754
drh856c1032009-06-02 15:21:42 +00002755 iDb = pOp->p1;
2756 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00002757 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002758 assert( iDb>=0 && iDb<db->nDb );
2759 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002760 assert( (p->btreeMask & (1<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00002761
2762 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002763 pOut->u.i = iMeta;
danielk1977a7a8e142008-02-13 18:25:27 +00002764 MemSetTypeFlag(pOut, MEM_Int);
drh50e5dad2001-09-15 00:57:28 +00002765 break;
2766}
2767
drh98757152008-01-09 23:04:12 +00002768/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002769**
drh98757152008-01-09 23:04:12 +00002770** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00002771** into cookie number P2 of database P1. P2==1 is the schema version.
2772** P2==2 is the database format. P2==3 is the recommended pager cache
2773** size, and so forth. P1==0 is the main database file and P1==1 is the
2774** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002775**
2776** A transaction must be started before executing this opcode.
2777*/
drh9cbf3422008-01-17 16:22:13 +00002778case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002779 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002780 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002781 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002782 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002783 pDb = &db->aDb[pOp->p1];
2784 assert( pDb->pBt!=0 );
drh98757152008-01-09 23:04:12 +00002785 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002786 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00002787 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
2788 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00002789 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002790 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002791 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00002792 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00002793 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002794 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002795 }
drhfd426c62006-01-30 15:34:22 +00002796 if( pOp->p1==1 ){
2797 /* Invalidate all prepared statements whenever the TEMP database
2798 ** schema is changed. Ticket #1644 */
2799 sqlite3ExpirePreparedStatements(db);
2800 }
drh50e5dad2001-09-15 00:57:28 +00002801 break;
2802}
2803
drh4a324312001-12-21 14:30:42 +00002804/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002805**
drh001bbcb2003-03-19 03:14:00 +00002806** Check the value of global database parameter number 0 (the
2807** schema version) and make sure it is equal to P2.
2808** P1 is the database number which is 0 for the main database file
2809** and 1 for the file holding temporary tables and some higher number
2810** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002811**
2812** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002813** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002814** and that the current process needs to reread the schema.
2815**
2816** Either a transaction needs to have been started or an OP_Open needs
2817** to be executed (to establish a read lock) before this opcode is
2818** invoked.
2819*/
drh9cbf3422008-01-17 16:22:13 +00002820case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002821 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002822 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002823 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002824 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002825 pBt = db->aDb[pOp->p1].pBt;
2826 if( pBt ){
danielk19770d19f7a2009-06-03 11:25:07 +00002827 rc = sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc275b4e2004-07-19 17:25:24 +00002828 }else{
2829 rc = SQLITE_OK;
2830 iMeta = 0;
2831 }
drhf328bc82004-05-10 23:29:49 +00002832 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
drh633e6d52008-07-28 19:34:53 +00002833 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002834 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002835 /* If the schema-cookie from the database file matches the cookie
2836 ** stored with the in-memory representation of the schema, do
2837 ** not reload the schema from the database file.
2838 **
shane21e7feb2008-05-30 15:59:49 +00002839 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002840 ** Often, v-tables store their data in other SQLite tables, which
2841 ** are queried from within xNext() and other v-table methods using
2842 ** prepared queries. If such a query is out-of-date, we do not want to
2843 ** discard the database schema, as the user code implementing the
2844 ** v-table would have to be ready for the sqlite3_vtab structure itself
2845 ** to be invalidated whenever sqlite3_step() is called from within
2846 ** a v-table method.
2847 */
2848 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2849 sqlite3ResetInternalSchema(db, pOp->p1);
2850 }
2851
drhf6d8ab82007-01-12 23:43:42 +00002852 sqlite3ExpirePreparedStatements(db);
drh50e5dad2001-09-15 00:57:28 +00002853 rc = SQLITE_SCHEMA;
2854 }
2855 break;
2856}
2857
drh98757152008-01-09 23:04:12 +00002858/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002859**
drhecdc7532001-09-23 02:35:53 +00002860** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002861** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002862** P3==0 means the main database, P3==1 means the database used for
2863** temporary tables, and P3>1 means used the corresponding attached
2864** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002865** values need not be contiguous but all P1 values should be small integers.
2866** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002867**
drh98757152008-01-09 23:04:12 +00002868** If P5!=0 then use the content of register P2 as the root page, not
2869** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002870**
drhb19a2bc2001-09-16 00:13:26 +00002871** There will be a read lock on the database whenever there is an
2872** open cursor. If the database was unlocked prior to this instruction
2873** then a read lock is acquired as part of this instruction. A read
2874** lock allows other processes to read the database but prohibits
2875** any other process from modifying the database. The read lock is
2876** released when all cursors are closed. If this instruction attempts
2877** to get a read lock but fails, the script terminates with an
2878** SQLITE_BUSY error code.
2879**
danielk1977d336e222009-02-20 10:58:41 +00002880** The P4 value may be either an integer (P4_INT32) or a pointer to
2881** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2882** structure, then said structure defines the content and collating
2883** sequence of the index being opened. Otherwise, if P4 is an integer
2884** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00002885**
drh001bbcb2003-03-19 03:14:00 +00002886** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002887*/
drh98757152008-01-09 23:04:12 +00002888/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002889**
2890** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002891** page is P2. Or if P5!=0 use the content of register P2 to find the
2892** root page.
drhecdc7532001-09-23 02:35:53 +00002893**
danielk1977d336e222009-02-20 10:58:41 +00002894** The P4 value may be either an integer (P4_INT32) or a pointer to
2895** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2896** structure, then said structure defines the content and collating
2897** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00002898** value, it is set to the number of columns in the table, or to the
2899** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00002900**
drh001bbcb2003-03-19 03:14:00 +00002901** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002902** in read/write mode. For a given table, there can be one or more read-only
2903** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002904**
drh001bbcb2003-03-19 03:14:00 +00002905** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002906*/
drh9cbf3422008-01-17 16:22:13 +00002907case OP_OpenRead:
2908case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00002909 int nField;
2910 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00002911 int p2;
2912 int iDb;
drhf57b3392001-10-08 13:22:32 +00002913 int wrFlag;
2914 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00002915 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00002916 Db *pDb;
drh856c1032009-06-02 15:21:42 +00002917 int flags;
2918
2919 nField = 0;
2920 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00002921 p2 = pOp->p2;
2922 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00002923 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00002924 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00002925 pDb = &db->aDb[iDb];
2926 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002927 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002928 if( pOp->opcode==OP_OpenWrite ){
2929 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002930 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2931 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002932 }
2933 }else{
2934 wrFlag = 0;
2935 }
drh98757152008-01-09 23:04:12 +00002936 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00002937 assert( p2>0 );
2938 assert( p2<=p->nMem );
2939 pIn2 = &p->aMem[p2];
2940 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00002941 p2 = (int)pIn2->u.i;
drh27731d72009-06-22 12:05:10 +00002942 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00002943 rc = SQLITE_CORRUPT_BKPT;
2944 goto abort_due_to_error;
2945 }
drh5edc3122001-09-13 21:53:09 +00002946 }
danielk1977d336e222009-02-20 10:58:41 +00002947 if( pOp->p4type==P4_KEYINFO ){
2948 pKeyInfo = pOp->p4.pKeyInfo;
2949 pKeyInfo->enc = ENC(p->db);
2950 nField = pKeyInfo->nField+1;
2951 }else if( pOp->p4type==P4_INT32 ){
2952 nField = pOp->p4.i;
2953 }
drh653b82a2009-06-22 11:10:47 +00002954 assert( pOp->p1>=0 );
2955 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00002956 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00002957 pCur->nullRow = 1;
danielk1977d336e222009-02-20 10:58:41 +00002958 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
2959 pCur->pKeyInfo = pKeyInfo;
2960
danielk197724162fe2004-06-04 06:22:00 +00002961 switch( rc ){
danielk197724162fe2004-06-04 06:22:00 +00002962 case SQLITE_OK: {
drh856c1032009-06-02 15:21:42 +00002963 flags = sqlite3BtreeFlags(pCur->pCursor);
drhaa736092009-06-22 00:55:30 +00002964
drhf0863fe2005-06-12 21:35:51 +00002965 /* Sanity checking. Only the lower four bits of the flags byte should
danielk1977ad0132d2008-06-07 08:58:22 +00002966 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
drhf0863fe2005-06-12 21:35:51 +00002967 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2968 ** 2 (zerodata for indices). If these conditions are not met it can
drhaa736092009-06-22 00:55:30 +00002969 ** only mean that we are dealing with a corrupt database file.
2970 ** Note: All of the above is checked already in sqlite3BtreeCursor().
drhf0863fe2005-06-12 21:35:51 +00002971 */
drhaa736092009-06-22 00:55:30 +00002972 assert( (flags & 0xf0)==0 );
2973 assert( (flags & 0x07)==5 || (flags & 0x07)==2 );
2974
drh9c1905f2008-12-10 22:32:56 +00002975 pCur->isTable = (flags & BTREE_INTKEY)!=0 ?1:0;
2976 pCur->isIndex = (flags & BTREE_ZERODATA)!=0 ?1:0;
drh66a51672008-01-03 00:01:23 +00002977 /* If P4==0 it means we are expected to open a table. If P4!=0 then
drhf0863fe2005-06-12 21:35:51 +00002978 ** we expect to be opening an index. If this is not what happened,
2979 ** then the database is corrupt
2980 */
drh66a51672008-01-03 00:01:23 +00002981 if( (pCur->isTable && pOp->p4type==P4_KEYINFO)
2982 || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
drh49285702005-09-17 15:20:26 +00002983 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002984 goto abort_due_to_error;
2985 }
danielk197724162fe2004-06-04 06:22:00 +00002986 break;
drh5e00f6c2001-09-13 13:46:56 +00002987 }
danielk197724162fe2004-06-04 06:22:00 +00002988 case SQLITE_EMPTY: {
drh66a51672008-01-03 00:01:23 +00002989 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhf0863fe2005-06-12 21:35:51 +00002990 pCur->isIndex = !pCur->isTable;
danielk1977cd3e8f72008-03-25 09:47:35 +00002991 pCur->pCursor = 0;
danielk197724162fe2004-06-04 06:22:00 +00002992 rc = SQLITE_OK;
2993 break;
2994 }
2995 default: {
drhaa736092009-06-22 00:55:30 +00002996 assert( rc!=SQLITE_BUSY ); /* Busy conditions detected earlier */
danielk197724162fe2004-06-04 06:22:00 +00002997 goto abort_due_to_error;
2998 }
2999 }
drh5e00f6c2001-09-13 13:46:56 +00003000 break;
3001}
3002
drh98757152008-01-09 23:04:12 +00003003/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003004**
drhb9bb7c12006-06-11 23:41:55 +00003005** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003006** The cursor is always opened read/write even if
3007** the main database is read-only. The transient or virtual
3008** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003009**
drh0342b1f2005-09-01 03:07:44 +00003010** P2 is the number of columns in the virtual table.
drh66a51672008-01-03 00:01:23 +00003011** The cursor points to a BTree table if P4==0 and to a BTree index
3012** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003013** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003014**
3015** This opcode was once called OpenTemp. But that created
3016** confusion because the term "temp table", might refer either
3017** to a TEMP table at the SQL level, or to a table opened by
3018** this opcode. Then this opcode was call OpenVirtual. But
3019** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00003020*/
drh9cbf3422008-01-17 16:22:13 +00003021case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003022 VdbeCursor *pCx;
drh33f4e022007-09-03 15:19:34 +00003023 static const int openFlags =
3024 SQLITE_OPEN_READWRITE |
3025 SQLITE_OPEN_CREATE |
3026 SQLITE_OPEN_EXCLUSIVE |
3027 SQLITE_OPEN_DELETEONCLOSE |
3028 SQLITE_OPEN_TRANSIENT_DB;
3029
drh653b82a2009-06-22 11:10:47 +00003030 assert( pOp->p1>=0 );
3031 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003032 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003033 pCx->nullRow = 1;
drh33f4e022007-09-03 15:19:34 +00003034 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
3035 &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00003036 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003037 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003038 }
3039 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003040 /* If a transient index is required, create it by calling
3041 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
3042 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00003043 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003044 */
danielk19772dca4ac2008-01-03 11:50:29 +00003045 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003046 int pgno;
drh66a51672008-01-03 00:01:23 +00003047 assert( pOp->p4type==P4_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00003048 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00003049 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003050 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003051 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003052 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003053 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00003054 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003055 }
drhf0863fe2005-06-12 21:35:51 +00003056 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003057 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003058 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003059 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003060 }
drh5e00f6c2001-09-13 13:46:56 +00003061 }
drhf0863fe2005-06-12 21:35:51 +00003062 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003063 break;
3064}
3065
danielk1977d336e222009-02-20 10:58:41 +00003066/* Opcode: OpenPseudo P1 P2 P3 * *
drh70ce3f02003-04-15 19:22:22 +00003067**
3068** Open a new cursor that points to a fake table that contains a single
3069** row of data. Any attempt to write a second row of data causes the
3070** first row to be deleted. All data is deleted when the cursor is
3071** closed.
3072**
3073** A pseudo-table created by this opcode is useful for holding the
drhcdd536f2006-03-17 00:04:03 +00003074** NEW or OLD tables in a trigger. Also used to hold the a single
3075** row output from the sorter so that the row can be decomposed into
3076** individual columns using the OP_Column opcode.
danielk19779882d992008-03-27 17:59:01 +00003077**
3078** When OP_Insert is executed to insert a row in to the pseudo table,
3079** the pseudo-table cursor may or may not make it's own copy of the
3080** original row data. If P2 is 0, then the pseudo-table will copy the
3081** original row data. Otherwise, a pointer to the original memory cell
3082** is stored. In this case, the vdbe program must ensure that the
3083** memory cell containing the row data is not overwritten until the
3084** pseudo table is closed (or a new row is inserted into it).
danielk1977d336e222009-02-20 10:58:41 +00003085**
3086** P3 is the number of fields in the records that will be stored by
3087** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003088*/
drh9cbf3422008-01-17 16:22:13 +00003089case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003090 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003091
drh653b82a2009-06-22 11:10:47 +00003092 assert( pOp->p1>=0 );
3093 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003094 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003095 pCx->nullRow = 1;
3096 pCx->pseudoTable = 1;
drh9c1905f2008-12-10 22:32:56 +00003097 pCx->ephemPseudoTable = (u8)pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003098 pCx->isTable = 1;
3099 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00003100 break;
3101}
3102
drh98757152008-01-09 23:04:12 +00003103/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003104**
3105** Close a cursor previously opened as P1. If P1 is not
3106** currently open, this instruction is a no-op.
3107*/
drh9cbf3422008-01-17 16:22:13 +00003108case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003109 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3110 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3111 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003112 break;
3113}
3114
drh959403f2008-12-12 17:56:16 +00003115/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003116**
danielk1977b790c6c2008-04-18 10:25:24 +00003117** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003118** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003119** to an SQL index, then P3 is the first in an array of P4 registers
3120** that are used as an unpacked index key.
3121**
3122** Reposition cursor P1 so that it points to the smallest entry that
3123** is greater than or equal to the key value. If there are no records
3124** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003125**
drh959403f2008-12-12 17:56:16 +00003126** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003127*/
drh959403f2008-12-12 17:56:16 +00003128/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003129**
danielk1977b790c6c2008-04-18 10:25:24 +00003130** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003131** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003132** to an SQL index, then P3 is the first in an array of P4 registers
3133** that are used as an unpacked index key.
3134**
3135** Reposition cursor P1 so that it points to the smallest entry that
3136** is greater than the key value. If there are no records greater than
3137** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003138**
drh959403f2008-12-12 17:56:16 +00003139** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003140*/
drh959403f2008-12-12 17:56:16 +00003141/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003142**
danielk1977b790c6c2008-04-18 10:25:24 +00003143** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003144** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003145** to an SQL index, then P3 is the first in an array of P4 registers
3146** that are used as an unpacked index key.
3147**
3148** Reposition cursor P1 so that it points to the largest entry that
3149** is less than the key value. If there are no records less than
3150** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003151**
drh959403f2008-12-12 17:56:16 +00003152** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003153*/
drh959403f2008-12-12 17:56:16 +00003154/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003155**
danielk1977b790c6c2008-04-18 10:25:24 +00003156** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003157** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003158** to an SQL index, then P3 is the first in an array of P4 registers
3159** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003160**
danielk1977b790c6c2008-04-18 10:25:24 +00003161** Reposition cursor P1 so that it points to the largest entry that
3162** is less than or equal to the key value. If there are no records
3163** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003164**
drh959403f2008-12-12 17:56:16 +00003165** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003166*/
drh959403f2008-12-12 17:56:16 +00003167case OP_SeekLt: /* jump, in3 */
3168case OP_SeekLe: /* jump, in3 */
3169case OP_SeekGe: /* jump, in3 */
3170case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003171 int res;
3172 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003173 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003174 UnpackedRecord r;
3175 int nField;
3176 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003177
drh653b82a2009-06-22 11:10:47 +00003178 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003179 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003180 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003181 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003182 if( pC->pCursor!=0 ){
drh7cf6e4d2004-05-19 14:56:55 +00003183 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003184 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003185 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003186 /* The input value in P3 might be of any type: integer, real, string,
3187 ** blob, or NULL. But it needs to be an integer before we can do
3188 ** the seek, so covert it. */
3189 applyNumericAffinity(pIn3);
3190 iKey = sqlite3VdbeIntValue(pIn3);
3191 pC->rowidIsValid = 0;
3192
3193 /* If the P3 value could not be converted into an integer without
3194 ** loss of information, then special processing is required... */
3195 if( (pIn3->flags & MEM_Int)==0 ){
3196 if( (pIn3->flags & MEM_Real)==0 ){
3197 /* If the P3 value cannot be converted into any kind of a number,
3198 ** then the seek is not possible, so jump to P2 */
3199 pc = pOp->p2 - 1;
3200 break;
3201 }
3202 /* If we reach this point, then the P3 value must be a floating
3203 ** point number. */
3204 assert( (pIn3->flags & MEM_Real)!=0 );
3205
3206 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003207 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003208 ** integer. */
3209 res = 1;
3210 if( pIn3->r<0 ){
3211 if( oc==OP_SeekGt || oc==OP_SeekGe ){
3212 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3213 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3214 }
3215 }else{
3216 if( oc==OP_SeekLt || oc==OP_SeekLe ){
3217 rc = sqlite3BtreeLast(pC->pCursor, &res);
3218 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3219 }
3220 }
3221 if( res ){
3222 pc = pOp->p2 - 1;
3223 }
3224 break;
3225 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3226 /* Use the ceiling() function to convert real->int */
3227 if( pIn3->r > (double)iKey ) iKey++;
3228 }else{
3229 /* Use the floor() function to convert real->int */
3230 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3231 if( pIn3->r < (double)iKey ) iKey--;
3232 }
3233 }
drhe63d9992008-08-13 19:11:48 +00003234 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003235 if( rc!=SQLITE_OK ){
3236 goto abort_due_to_error;
3237 }
drh959403f2008-12-12 17:56:16 +00003238 if( res==0 ){
3239 pC->rowidIsValid = 1;
3240 pC->lastRowid = iKey;
3241 }
drh5e00f6c2001-09-13 13:46:56 +00003242 }else{
drh856c1032009-06-02 15:21:42 +00003243 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003244 assert( pOp->p4type==P4_INT32 );
3245 assert( nField>0 );
3246 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003247 r.nField = (u16)nField;
drh959403f2008-12-12 17:56:16 +00003248 if( oc==OP_SeekGt || oc==OP_SeekLe ){
drhe63d9992008-08-13 19:11:48 +00003249 r.flags = UNPACKED_INCRKEY;
3250 }else{
3251 r.flags = 0;
3252 }
danielk1977b790c6c2008-04-18 10:25:24 +00003253 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00003254 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003255 if( rc!=SQLITE_OK ){
3256 goto abort_due_to_error;
3257 }
drhf0863fe2005-06-12 21:35:51 +00003258 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003259 }
drha11846b2004-01-07 18:52:56 +00003260 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003261 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003262#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003263 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003264#endif
drh959403f2008-12-12 17:56:16 +00003265 if( oc==OP_SeekGe || oc==OP_SeekGt ){
3266 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003267 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003268 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003269 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003270 }else{
3271 res = 0;
drh8721ce42001-11-07 14:22:00 +00003272 }
drh7cf6e4d2004-05-19 14:56:55 +00003273 }else{
drh959403f2008-12-12 17:56:16 +00003274 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3275 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003276 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3277 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003278 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003279 }else{
3280 /* res might be negative because the table is empty. Check to
3281 ** see if this is the case.
3282 */
drhf328bc82004-05-10 23:29:49 +00003283 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003284 }
drh1af3fdb2004-07-18 21:33:01 +00003285 }
drh91fd4d42008-01-19 20:11:25 +00003286 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003287 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003288 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003289 }
drhaa736092009-06-22 00:55:30 +00003290 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003291 /* This happens when attempting to open the sqlite3_master table
3292 ** for read access returns SQLITE_EMPTY. In this case always
3293 ** take the jump (since there are no records in the table).
3294 */
drhaa736092009-06-22 00:55:30 +00003295 assert( pC->pseudoTable==0 );
danielk1977f7b9d662008-06-23 18:49:43 +00003296 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003297 }
drh5e00f6c2001-09-13 13:46:56 +00003298 break;
3299}
3300
drh959403f2008-12-12 17:56:16 +00003301/* Opcode: Seek P1 P2 * * *
3302**
3303** P1 is an open table cursor and P2 is a rowid integer. Arrange
3304** for P1 to move so that it points to the rowid given by P2.
3305**
3306** This is actually a deferred seek. Nothing actually happens until
3307** the cursor is used to read a record. That way, if no reads
3308** occur, no unnecessary I/O happens.
3309*/
3310case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003311 VdbeCursor *pC;
3312
drh653b82a2009-06-22 11:10:47 +00003313 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3314 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003315 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003316 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003317 assert( pC->isTable );
3318 pC->nullRow = 0;
3319 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3320 pC->rowidIsValid = 0;
3321 pC->deferredMoveto = 1;
3322 }
3323 break;
3324}
3325
3326
drh98757152008-01-09 23:04:12 +00003327/* Opcode: Found P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003328**
drh98757152008-01-09 23:04:12 +00003329** Register P3 holds a blob constructed by MakeRecord. P1 is an index.
drh9cbf3422008-01-17 16:22:13 +00003330** If an entry that matches the value in register p3 exists in P1 then
3331** jump to P2. If the P3 value does not match any entry in P1
drhf0863fe2005-06-12 21:35:51 +00003332** then fall thru. The P1 cursor is left pointing at the matching entry
drh2dcef112008-01-12 19:03:48 +00003333** if it exists.
drhf0863fe2005-06-12 21:35:51 +00003334**
3335** This instruction is used to implement the IN operator where the
danielk19779a96b662007-11-29 17:05:18 +00003336** left-hand side is a SELECT statement. P1 may be a true index, or it
3337** may be a temporary index that holds the results of the SELECT
drh2dcef112008-01-12 19:03:48 +00003338** statement. This instruction is also used to implement the
3339** DISTINCT keyword in SELECT statements.
danielk19779a96b662007-11-29 17:05:18 +00003340**
3341** This instruction checks if index P1 contains a record for which
shane21e7feb2008-05-30 15:59:49 +00003342** the first N serialized values exactly match the N serialized values
drh9cbf3422008-01-17 16:22:13 +00003343** in the record in register P3, where N is the total number of values in
3344** the P3 record (the P3 record is a prefix of the P1 record).
drhb19a2bc2001-09-16 00:13:26 +00003345**
drhcb6d50e2008-08-21 19:28:30 +00003346** See also: NotFound, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00003347*/
drh98757152008-01-09 23:04:12 +00003348/* Opcode: NotFound P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003349**
drh98757152008-01-09 23:04:12 +00003350** Register P3 holds a blob constructed by MakeRecord. P1 is
drhf0863fe2005-06-12 21:35:51 +00003351** an index. If no entry exists in P1 that matches the blob then jump
drh795ab9b2007-01-27 13:37:22 +00003352** to P2. If an entry does existing, fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003353** pointing to the entry that matches.
drh5e00f6c2001-09-13 13:46:56 +00003354**
drhcb6d50e2008-08-21 19:28:30 +00003355** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003356*/
drh9cbf3422008-01-17 16:22:13 +00003357case OP_NotFound: /* jump, in3 */
3358case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003359 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003360 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003361 int res;
3362 UnpackedRecord *pIdxKey;
3363 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3364
drh856c1032009-06-02 15:21:42 +00003365 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003366 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3367 pC = p->apCsr[pOp->p1];
3368 assert( pC!=0 );
3369 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003370
drhf0863fe2005-06-12 21:35:51 +00003371 assert( pC->isTable==0 );
drh98757152008-01-09 23:04:12 +00003372 assert( pIn3->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003373 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
drh23f79d02008-08-20 22:06:47 +00003374 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003375 if( pIdxKey==0 ){
3376 goto no_mem;
danielk19779a96b662007-11-29 17:05:18 +00003377 }
drhe63d9992008-08-13 19:11:48 +00003378 if( pOp->opcode==OP_Found ){
3379 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3380 }
3381 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3382 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk197777519402007-08-30 11:48:31 +00003383 if( rc!=SQLITE_OK ){
3384 break;
3385 }
3386 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003387 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003388 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003389 }
3390 if( pOp->opcode==OP_Found ){
3391 if( alreadyExists ) pc = pOp->p2 - 1;
3392 }else{
3393 if( !alreadyExists ) pc = pOp->p2 - 1;
3394 }
drh5e00f6c2001-09-13 13:46:56 +00003395 break;
3396}
3397
drh98757152008-01-09 23:04:12 +00003398/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003399**
danielk1977de630352009-05-04 11:42:29 +00003400** Cursor P1 is open on an index. So it has no data and its key consists
3401** of a record generated by OP_MakeRecord where the last field is the
drhf0863fe2005-06-12 21:35:51 +00003402** rowid of the entry that the index refers to.
danielk1977de630352009-05-04 11:42:29 +00003403**
3404** The P3 register contains an integer record number. Call this record
3405** number R. Register P4 is the first in a set of N contiguous registers
3406** that make up an unpacked index key that can be used with cursor P1.
3407** The value of N can be inferred from the cursor. N includes the rowid
3408** value appended to the end of the index record. This rowid value may
3409** or may not be the same as R.
3410**
3411** If any of the N registers beginning with register P4 contains a NULL
3412** value, jump immediately to P2.
3413**
3414** Otherwise, this instruction checks if cursor P1 contains an entry
3415** where the first (N-1) fields match but the rowid value at the end
3416** of the index entry is not R. If there is no such entry, control jumps
3417** to instruction P2. Otherwise, the rowid of the conflicting index
3418** entry is copied to register P3 and control falls through to the next
3419** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003420**
drh9cbf3422008-01-17 16:22:13 +00003421** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003422*/
drh9cbf3422008-01-17 16:22:13 +00003423case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003424 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003425 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003426 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003427 u16 nField;
drh856c1032009-06-02 15:21:42 +00003428 Mem *aMem;
3429 UnpackedRecord r; /* B-Tree index search key */
3430 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003431
drh856c1032009-06-02 15:21:42 +00003432 aMem = &p->aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003433 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003434 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003435 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003436 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3437
3438 /* Find the index cursor. */
3439 pCx = p->apCsr[pOp->p1];
3440 assert( pCx->deferredMoveto==0 );
3441 pCx->seekResult = 0;
3442 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003443 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003444
3445 /* If any of the values are NULL, take the jump. */
3446 nField = pCx->pKeyInfo->nField;
3447 for(ii=0; ii<nField; ii++){
3448 if( aMem[ii].flags & MEM_Null ){
3449 pc = pOp->p2 - 1;
3450 pCrsr = 0;
3451 break;
3452 }
3453 }
3454 assert( (aMem[nField].flags & MEM_Null)==0 );
3455
drhf328bc82004-05-10 23:29:49 +00003456 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003457 /* Populate the index search key. */
3458 r.pKeyInfo = pCx->pKeyInfo;
3459 r.nField = nField + 1;
3460 r.flags = UNPACKED_PREFIX_SEARCH;
3461 r.aMem = aMem;
danielk1977452c9892004-05-13 05:16:15 +00003462
danielk1977de630352009-05-04 11:42:29 +00003463 /* Extract the value of R from register P3. */
3464 sqlite3VdbeMemIntegerify(pIn3);
3465 R = pIn3->u.i;
3466
3467 /* Search the B-Tree index. If no conflicting record is found, jump
3468 ** to P2. Otherwise, copy the rowid of the conflicting record to
3469 ** register P3 and fall through to the next instruction. */
3470 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3471 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003472 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003473 }else{
3474 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003475 }
drh9cfcf5d2002-01-29 18:41:24 +00003476 }
3477 break;
3478}
3479
drh9cbf3422008-01-17 16:22:13 +00003480/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003481**
drh9cbf3422008-01-17 16:22:13 +00003482** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003483** with that key does not exist in table of P1, then jump to P2.
3484** If the record does exist, then fall thru. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003485** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003486**
3487** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003488** operation assumes the key is an integer and that P1 is a table whereas
3489** NotFound assumes key is a blob constructed from MakeRecord and
3490** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003491**
drhcb6d50e2008-08-21 19:28:30 +00003492** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003493*/
drh9cbf3422008-01-17 16:22:13 +00003494case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003495 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003496 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003497 int res;
3498 u64 iKey;
3499
drhaa736092009-06-22 00:55:30 +00003500 assert( pIn3->flags & MEM_Int );
3501 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3502 pC = p->apCsr[pOp->p1];
3503 assert( pC!=0 );
3504 assert( pC->isTable );
3505 pCrsr = pC->pCursor;
3506 if( pCrsr!=0 ){
drh856c1032009-06-02 15:21:42 +00003507 res = 0;
drhaa736092009-06-22 00:55:30 +00003508 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003509 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003510 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003511 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003512 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003513 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003514 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003515 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003516 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003517 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003518 }
danielk1977de630352009-05-04 11:42:29 +00003519 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003520 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003521 /* This happens when an attempt to open a read cursor on the
3522 ** sqlite_master table returns SQLITE_EMPTY.
3523 */
drhaa736092009-06-22 00:55:30 +00003524 assert( !pC->pseudoTable );
danielk1977f7b9d662008-06-23 18:49:43 +00003525 assert( pC->isTable );
3526 pc = pOp->p2 - 1;
3527 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003528 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003529 }
drh6b125452002-01-28 15:53:03 +00003530 break;
3531}
3532
drh4c583122008-01-04 22:01:03 +00003533/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003534**
drh4c583122008-01-04 22:01:03 +00003535** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003536** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003537** The sequence number on the cursor is incremented after this
3538** instruction.
drh4db38a72005-09-01 12:16:28 +00003539*/
drh4c583122008-01-04 22:01:03 +00003540case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003541 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3542 assert( p->apCsr[pOp->p1]!=0 );
3543 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
danielk1977a7a8e142008-02-13 18:25:27 +00003544 MemSetTypeFlag(pOut, MEM_Int);
drh4db38a72005-09-01 12:16:28 +00003545 break;
3546}
3547
3548
drh98757152008-01-09 23:04:12 +00003549/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003550**
drhf0863fe2005-06-12 21:35:51 +00003551** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003552** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003553** table that cursor P1 points to. The new record number is written
3554** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003555**
drh98757152008-01-09 23:04:12 +00003556** If P3>0 then P3 is a register that holds the largest previously
drh205f48e2004-11-05 00:43:11 +00003557** generated record number. No new record numbers are allowed to be less
drh2958a4e2004-11-12 03:56:15 +00003558** than this value. When this value reaches its maximum, a SQLITE_FULL
drh98757152008-01-09 23:04:12 +00003559** error is generated. The P3 register is updated with the generated
drh4c583122008-01-04 22:01:03 +00003560** record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003561** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003562*/
drh4c583122008-01-04 22:01:03 +00003563case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003564 i64 v; /* The new rowid */
3565 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3566 int res; /* Result of an sqlite3BtreeLast() */
3567 int cnt; /* Counter to limit the number of searches */
3568 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
drh856c1032009-06-02 15:21:42 +00003569
drh856c1032009-06-02 15:21:42 +00003570 v = 0;
3571 res = 0;
drhaa736092009-06-22 00:55:30 +00003572 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3573 pC = p->apCsr[pOp->p1];
3574 assert( pC!=0 );
3575 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003576 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003577 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003578 /* The next rowid or record number (different terms for the same
3579 ** thing) is obtained in a two-step algorithm.
3580 **
3581 ** First we attempt to find the largest existing rowid and add one
3582 ** to that. But if the largest existing rowid is already the maximum
3583 ** positive integer, we have to fall through to the second
3584 ** probabilistic algorithm
3585 **
3586 ** The second algorithm is to select a rowid at random and see if
3587 ** it already exists in the table. If it does not exist, we have
3588 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003589 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003590 */
drhaa736092009-06-22 00:55:30 +00003591 assert( pC->isTable );
drh5e00f6c2001-09-13 13:46:56 +00003592 cnt = 0;
drhfe2093d2005-01-20 22:48:47 +00003593
drh75f86a42005-02-17 00:03:06 +00003594#ifdef SQLITE_32BIT_ROWID
3595# define MAX_ROWID 0x7fffffff
3596#else
drhfe2093d2005-01-20 22:48:47 +00003597 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3598 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3599 ** to provide the constant while making all compilers happy.
3600 */
danielk197764202cf2008-11-17 15:31:47 +00003601# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003602#endif
drhfe2093d2005-01-20 22:48:47 +00003603
drh5cf8e8c2002-02-19 22:42:05 +00003604 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003605 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3606 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003607 rc = sqlite3BtreeLast(pC->pCursor, &res);
3608 if( rc!=SQLITE_OK ){
3609 goto abort_due_to_error;
3610 }
drh32fbe342002-10-19 20:16:37 +00003611 if( res ){
3612 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003613 }else{
danielk1977e0d4b062004-06-28 01:11:46 +00003614 sqlite3BtreeKeySize(pC->pCursor, &v);
drh75f86a42005-02-17 00:03:06 +00003615 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003616 pC->useRandomRowid = 1;
3617 }else{
3618 v++;
3619 }
drh5cf8e8c2002-02-19 22:42:05 +00003620 }
drh3fc190c2001-09-14 03:24:23 +00003621 }
drh205f48e2004-11-05 00:43:11 +00003622
3623#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003624 if( pOp->p3 ){
drh4c583122008-01-04 22:01:03 +00003625 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
3626 pMem = &p->aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00003627 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003628 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003629 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003630 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003631 rc = SQLITE_FULL;
3632 goto abort_due_to_error;
3633 }
drh3c024d62007-03-30 11:23:45 +00003634 if( v<pMem->u.i+1 ){
3635 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003636 }
drh3c024d62007-03-30 11:23:45 +00003637 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003638 }
3639#endif
3640
drh7f751222009-03-17 22:33:00 +00003641 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003642 }
3643 if( pC->useRandomRowid ){
drhaa736092009-06-22 00:55:30 +00003644 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3645 ** an AUTOINCREMENT table. */
drh5cf8e8c2002-02-19 22:42:05 +00003646 v = db->priorNewRowid;
3647 cnt = 0;
3648 do{
drh91fd4d42008-01-19 20:11:25 +00003649 if( cnt==0 && (v&0xffffff)==v ){
3650 v++;
3651 }else{
drh2fa18682008-03-19 14:15:34 +00003652 sqlite3_randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003653 if( cnt<5 ) v &= 0xffffff;
drh5cf8e8c2002-02-19 22:42:05 +00003654 }
drhaa736092009-06-22 00:55:30 +00003655 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, 0, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003656 cnt++;
drhaa736092009-06-22 00:55:30 +00003657 }while( cnt<100 && rc==SQLITE_OK && res==0 );
drh5cf8e8c2002-02-19 22:42:05 +00003658 db->priorNewRowid = v;
drhaa736092009-06-22 00:55:30 +00003659 if( rc==SQLITE_OK && res==0 ){
drh5cf8e8c2002-02-19 22:42:05 +00003660 rc = SQLITE_FULL;
3661 goto abort_due_to_error;
3662 }
drh1eaa2692001-09-18 02:02:23 +00003663 }
drhf0863fe2005-06-12 21:35:51 +00003664 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003665 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003666 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003667 }
danielk1977a7a8e142008-02-13 18:25:27 +00003668 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00003669 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003670 break;
3671}
3672
danielk19771f4aa332008-01-03 09:51:55 +00003673/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003674**
jplyon5a564222003-06-02 06:15:58 +00003675** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003676** created if it doesn't already exist or the data for an existing
danielk19771f4aa332008-01-03 09:51:55 +00003677** entry is overwritten. The data is the value stored register
3678** number P2. The key is stored in register P3. The key must
3679** be an integer.
drh4a324312001-12-21 14:30:42 +00003680**
danielk19771f4aa332008-01-03 09:51:55 +00003681** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3682** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003683** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003684** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003685**
drh66a51672008-01-03 00:01:23 +00003686** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003687** may be NULL. If it is not NULL, then the update-hook
3688** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3689**
drh93aed5a2008-01-16 17:46:38 +00003690** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3691** allocated, then ownership of P2 is transferred to the pseudo-cursor
3692** and register P2 becomes ephemeral. If the cursor is changed, the
3693** value of register P2 will then change. Make sure this does not
3694** cause any problems.)
3695**
drhf0863fe2005-06-12 21:35:51 +00003696** This instruction only works on tables. The equivalent instruction
3697** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003698*/
drh9cbf3422008-01-17 16:22:13 +00003699case OP_Insert: {
drh856c1032009-06-02 15:21:42 +00003700 Mem *pData;
3701 Mem *pKey;
drha05a7222008-01-19 03:35:58 +00003702 i64 iKey; /* The integer ROWID or key for the record to be inserted */
drhdfe88ec2008-11-03 20:55:06 +00003703 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003704 int nZero;
3705 int seekResult;
3706 const char *zDb;
3707 const char *zTbl;
3708 int op;
3709
3710 pData = &p->aMem[pOp->p2];
3711 pKey = &p->aMem[pOp->p3];
drh653b82a2009-06-22 11:10:47 +00003712 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3713 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00003714 assert( pC!=0 );
3715 assert( pC->pCursor!=0 || pC->pseudoTable );
3716 assert( pKey->flags & MEM_Int );
3717 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003718 REGISTER_TRACE(pOp->p2, pData);
3719 REGISTER_TRACE(pOp->p3, pKey);
danielk19775f8d8a82004-05-11 00:28:42 +00003720
drhaa736092009-06-22 00:55:30 +00003721 iKey = pKey->u.i;
drha05a7222008-01-19 03:35:58 +00003722 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
3723 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
drha05a7222008-01-19 03:35:58 +00003724 if( pData->flags & MEM_Null ){
3725 pData->z = 0;
3726 pData->n = 0;
3727 }else{
3728 assert( pData->flags & (MEM_Blob|MEM_Str) );
3729 }
3730 if( pC->pseudoTable ){
danielk19779882d992008-03-27 17:59:01 +00003731 if( !pC->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00003732 sqlite3DbFree(db, pC->pData);
danielk19779882d992008-03-27 17:59:01 +00003733 }
drha05a7222008-01-19 03:35:58 +00003734 pC->iKey = iKey;
3735 pC->nData = pData->n;
drh27731d72009-06-22 12:05:10 +00003736 if( pC->ephemPseudoTable || pData->z==pData->zMalloc ){
drha05a7222008-01-19 03:35:58 +00003737 pC->pData = pData->z;
danielk19779882d992008-03-27 17:59:01 +00003738 if( !pC->ephemPseudoTable ){
3739 pData->flags &= ~MEM_Dyn;
3740 pData->flags |= MEM_Ephem;
danielk19775f096132008-03-28 15:44:09 +00003741 pData->zMalloc = 0;
danielk19779882d992008-03-27 17:59:01 +00003742 }
drha05a7222008-01-19 03:35:58 +00003743 }else{
drhe5ae5732008-06-15 02:51:47 +00003744 pC->pData = sqlite3Malloc( pC->nData+2 );
drha05a7222008-01-19 03:35:58 +00003745 if( !pC->pData ) goto no_mem;
3746 memcpy(pC->pData, pData->z, pC->nData);
3747 pC->pData[pC->nData] = 0;
3748 pC->pData[pC->nData+1] = 0;
3749 }
3750 pC->nullRow = 0;
3751 }else{
drh856c1032009-06-02 15:21:42 +00003752 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
drha05a7222008-01-19 03:35:58 +00003753 if( pData->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00003754 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003755 }else{
3756 nZero = 0;
3757 }
drh7f751222009-03-17 22:33:00 +00003758 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drha05a7222008-01-19 03:35:58 +00003759 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3760 pData->z, pData->n, nZero,
danielk1977de630352009-05-04 11:42:29 +00003761 pOp->p5 & OPFLAG_APPEND, seekResult
3762 );
drha05a7222008-01-19 03:35:58 +00003763 }
3764
3765 pC->rowidIsValid = 0;
3766 pC->deferredMoveto = 0;
3767 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003768
drha05a7222008-01-19 03:35:58 +00003769 /* Invoke the update-hook if required. */
3770 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00003771 zDb = db->aDb[pC->iDb].zName;
3772 zTbl = pOp->p4.z;
3773 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00003774 assert( pC->isTable );
3775 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3776 assert( pC->iDb>=0 );
3777 }
drh5e00f6c2001-09-13 13:46:56 +00003778 break;
3779}
3780
drh98757152008-01-09 23:04:12 +00003781/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003782**
drh5edc3122001-09-13 21:53:09 +00003783** Delete the record at which the P1 cursor is currently pointing.
3784**
3785** The cursor will be left pointing at either the next or the previous
3786** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003787** the next Next instruction will be a no-op. Hence it is OK to delete
3788** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003789**
rdcb0c374f2004-02-20 22:53:38 +00003790** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003791** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003792**
drh91fd4d42008-01-19 20:11:25 +00003793** P1 must not be pseudo-table. It has to be a real table with
3794** multiple rows.
3795**
3796** If P4 is not NULL, then it is the name of the table that P1 is
3797** pointing to. The update hook will be invoked, if it exists.
3798** If P4 is not NULL then the P1 cursor must have been positioned
3799** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003800*/
drh9cbf3422008-01-17 16:22:13 +00003801case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00003802 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003803 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003804
drh856c1032009-06-02 15:21:42 +00003805 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00003806 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3807 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003808 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003809 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003810
drh91fd4d42008-01-19 20:11:25 +00003811 /* If the update-hook will be invoked, set iKey to the rowid of the
3812 ** row being deleted.
3813 */
3814 if( db->xUpdateCallback && pOp->p4.z ){
3815 assert( pC->isTable );
3816 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3817 iKey = pC->lastRowid;
3818 }
danielk197794eb6a12005-12-15 15:22:08 +00003819
drh91fd4d42008-01-19 20:11:25 +00003820 rc = sqlite3VdbeCursorMoveto(pC);
3821 if( rc ) goto abort_due_to_error;
drh7f751222009-03-17 22:33:00 +00003822 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00003823 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00003824 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003825
drh91fd4d42008-01-19 20:11:25 +00003826 /* Invoke the update-hook if required. */
3827 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3828 const char *zDb = db->aDb[pC->iDb].zName;
3829 const char *zTbl = pOp->p4.z;
3830 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3831 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003832 }
danielk1977b28af712004-06-21 06:50:26 +00003833 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003834 break;
3835}
3836
danielk1977b28af712004-06-21 06:50:26 +00003837/* Opcode: ResetCount P1 * *
rdcb0c374f2004-02-20 22:53:38 +00003838**
danielk1977b28af712004-06-21 06:50:26 +00003839** This opcode resets the VMs internal change counter to 0. If P1 is true,
3840** then the value of the change counter is copied to the database handle
3841** change counter (returned by subsequent calls to sqlite3_changes())
3842** before it is reset. This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003843*/
drh9cbf3422008-01-17 16:22:13 +00003844case OP_ResetCount: {
danielk1977b28af712004-06-21 06:50:26 +00003845 if( pOp->p1 ){
drh344737f2004-09-19 00:50:20 +00003846 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003847 }
3848 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003849 break;
3850}
3851
drh98757152008-01-09 23:04:12 +00003852/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00003853**
drh98757152008-01-09 23:04:12 +00003854** Write into register P2 the complete row data for cursor P1.
3855** There is no interpretation of the data.
3856** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003857** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00003858**
drhde4fcfd2008-01-19 23:50:26 +00003859** If the P1 cursor must be pointing to a valid row (not a NULL row)
3860** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003861*/
drh98757152008-01-09 23:04:12 +00003862/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00003863**
drh98757152008-01-09 23:04:12 +00003864** Write into register P2 the complete row key for cursor P1.
3865** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00003866** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003867** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00003868**
drhde4fcfd2008-01-19 23:50:26 +00003869** If the P1 cursor must be pointing to a valid row (not a NULL row)
3870** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00003871*/
danielk1977a7a8e142008-02-13 18:25:27 +00003872case OP_RowKey:
3873case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00003874 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00003875 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00003876 u32 n;
drh856c1032009-06-02 15:21:42 +00003877 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00003878
danielk1977a7a8e142008-02-13 18:25:27 +00003879 pOut = &p->aMem[pOp->p2];
3880
drhf0863fe2005-06-12 21:35:51 +00003881 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00003882 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3883 pC = p->apCsr[pOp->p1];
drhf0863fe2005-06-12 21:35:51 +00003884 assert( pC->isTable || pOp->opcode==OP_RowKey );
3885 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003886 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00003887 assert( pC->nullRow==0 );
3888 assert( pC->pseudoTable==0 );
3889 assert( pC->pCursor!=0 );
3890 pCrsr = pC->pCursor;
3891 rc = sqlite3VdbeCursorMoveto(pC);
3892 if( rc ) goto abort_due_to_error;
3893 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00003894 assert( !pC->isTable );
3895 sqlite3BtreeKeySize(pCrsr, &n64);
drhbb4957f2008-03-20 14:03:29 +00003896 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00003897 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00003898 }
drhbfb19dc2009-06-05 16:46:53 +00003899 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00003900 }else{
3901 sqlite3BtreeDataSize(pCrsr, &n);
shane75ac1de2009-06-09 18:58:52 +00003902 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00003903 goto too_big;
3904 }
drhde4fcfd2008-01-19 23:50:26 +00003905 }
danielk1977a7a8e142008-02-13 18:25:27 +00003906 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
3907 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00003908 }
danielk1977a7a8e142008-02-13 18:25:27 +00003909 pOut->n = n;
3910 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00003911 if( pC->isIndex ){
3912 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
3913 }else{
3914 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00003915 }
danielk197796cb76f2008-01-04 13:24:28 +00003916 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00003917 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00003918 break;
3919}
3920
drh2133d822008-01-03 18:44:59 +00003921/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003922**
drh2133d822008-01-03 18:44:59 +00003923** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00003924** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00003925**
3926** P1 can be either an ordinary table or a virtual table. There used to
3927** be a separate OP_VRowid opcode for use with virtual tables, but this
3928** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00003929*/
drh4c583122008-01-04 22:01:03 +00003930case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00003931 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00003932 i64 v;
drh856c1032009-06-02 15:21:42 +00003933 sqlite3_vtab *pVtab;
3934 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00003935
drh653b82a2009-06-22 11:10:47 +00003936 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3937 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003938 assert( pC!=0 );
drh044925b2009-04-22 17:15:02 +00003939 if( pC->nullRow ){
3940 /* Do nothing so that reg[P2] remains NULL */
3941 break;
3942 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00003943 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00003944 }else if( pC->pseudoTable ){
drhaa736092009-06-22 00:55:30 +00003945 v = pC->iKey;
drh044925b2009-04-22 17:15:02 +00003946#ifndef SQLITE_OMIT_VIRTUALTABLE
3947 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00003948 pVtab = pC->pVtabCursor->pVtab;
3949 pModule = pVtab->pModule;
3950 assert( pModule->xRowid );
3951 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
3952 rc = pModule->xRowid(pC->pVtabCursor, &v);
3953 sqlite3DbFree(db, p->zErrMsg);
3954 p->zErrMsg = pVtab->zErrMsg;
3955 pVtab->zErrMsg = 0;
3956 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
3957#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00003958 }else{
drh61495262009-04-22 15:32:59 +00003959 rc = sqlite3VdbeCursorMoveto(pC);
3960 if( rc ) goto abort_due_to_error;
3961 if( pC->rowidIsValid ){
3962 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00003963 }else{
3964 assert( pC->pCursor!=0 );
3965 sqlite3BtreeKeySize(pC->pCursor, &v);
drh61495262009-04-22 15:32:59 +00003966 }
drh5e00f6c2001-09-13 13:46:56 +00003967 }
drh4c583122008-01-04 22:01:03 +00003968 pOut->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003969 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00003970 break;
3971}
3972
drh9cbf3422008-01-17 16:22:13 +00003973/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00003974**
3975** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00003976** that occur while the cursor is on the null row will always
3977** write a NULL.
drh17f71932002-02-21 12:01:27 +00003978*/
drh9cbf3422008-01-17 16:22:13 +00003979case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00003980 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00003981
drh653b82a2009-06-22 11:10:47 +00003982 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3983 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003984 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00003985 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00003986 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00003987 if( pC->pCursor ){
3988 sqlite3BtreeClearCursor(pC->pCursor);
3989 }
drh17f71932002-02-21 12:01:27 +00003990 break;
3991}
3992
drh9cbf3422008-01-17 16:22:13 +00003993/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00003994**
drhf0863fe2005-06-12 21:35:51 +00003995** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00003996** will refer to the last entry in the database table or index.
3997** If the table or index is empty and P2>0, then jump immediately to P2.
3998** If P2 is 0 or if the table or index is not empty, fall through
3999** to the following instruction.
4000*/
drh9cbf3422008-01-17 16:22:13 +00004001case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004002 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004003 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004004 int res;
drh9562b552002-02-19 15:00:07 +00004005
drh653b82a2009-06-22 11:10:47 +00004006 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4007 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004008 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004009 pCrsr = pC->pCursor;
4010 assert( pCrsr!=0 );
4011 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004012 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004013 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004014 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004015 pC->cacheStatus = CACHE_STALE;
4016 if( res && pOp->p2>0 ){
4017 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004018 }
4019 break;
4020}
4021
drh0342b1f2005-09-01 03:07:44 +00004022
drh9cbf3422008-01-17 16:22:13 +00004023/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004024**
4025** This opcode does exactly the same thing as OP_Rewind except that
4026** it increments an undocumented global variable used for testing.
4027**
4028** Sorting is accomplished by writing records into a sorting index,
4029** then rewinding that index and playing it back from beginning to
4030** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4031** rewinding so that the global variable will be incremented and
4032** regression tests can determine whether or not the optimizer is
4033** correctly optimizing out sorts.
4034*/
drh9cbf3422008-01-17 16:22:13 +00004035case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004036#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004037 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004038 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004039#endif
drhd1d38482008-10-07 23:46:38 +00004040 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00004041 /* Fall through into OP_Rewind */
4042}
drh9cbf3422008-01-17 16:22:13 +00004043/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004044**
drhf0863fe2005-06-12 21:35:51 +00004045** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004046** will refer to the first entry in the database table or index.
4047** If the table or index is empty and P2>0, then jump immediately to P2.
4048** If P2 is 0 or if the table or index is not empty, fall through
4049** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004050*/
drh9cbf3422008-01-17 16:22:13 +00004051case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004052 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004053 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004054 int res;
drh5e00f6c2001-09-13 13:46:56 +00004055
drh653b82a2009-06-22 11:10:47 +00004056 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4057 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004058 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00004059 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00004060 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004061 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004062 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004063 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004064 pC->rowidIsValid = 0;
drh70ce3f02003-04-15 19:22:22 +00004065 }else{
drhf4dada72004-05-11 09:57:35 +00004066 res = 1;
4067 }
drh9c1905f2008-12-10 22:32:56 +00004068 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004069 assert( pOp->p2>0 && pOp->p2<p->nOp );
4070 if( res ){
drhf4dada72004-05-11 09:57:35 +00004071 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004072 }
4073 break;
4074}
4075
drh9cbf3422008-01-17 16:22:13 +00004076/* Opcode: Next P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004077**
4078** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004079** table or index. If there are no more key/value pairs then fall through
4080** to the following instruction. But if the cursor advance was successful,
4081** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004082**
drh60a713c2008-01-21 16:22:45 +00004083** The P1 cursor must be for a real table, not a pseudo-table.
4084**
drhc045ec52002-12-04 20:01:06 +00004085** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004086*/
drh9cbf3422008-01-17 16:22:13 +00004087/* Opcode: Prev P1 P2 * * *
drhc045ec52002-12-04 20:01:06 +00004088**
4089** Back up cursor P1 so that it points to the previous key/data pair in its
4090** table or index. If there is no previous key/value pairs then fall through
4091** to the following instruction. But if the cursor backup was successful,
4092** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004093**
4094** The P1 cursor must be for a real table, not a pseudo-table.
drhc045ec52002-12-04 20:01:06 +00004095*/
drh9cbf3422008-01-17 16:22:13 +00004096case OP_Prev: /* jump */
4097case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004098 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004099 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00004100 int res;
drh8721ce42001-11-07 14:22:00 +00004101
drhcaec2f12003-01-07 02:47:47 +00004102 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004103 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00004104 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004105 if( pC==0 ){
4106 break; /* See ticket #2273 */
4107 }
drh60a713c2008-01-21 16:22:45 +00004108 pCrsr = pC->pCursor;
4109 assert( pCrsr );
drha3460582008-07-11 21:02:53 +00004110 res = 1;
4111 assert( pC->deferredMoveto==0 );
4112 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
4113 sqlite3BtreePrevious(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004114 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004115 pC->cacheStatus = CACHE_STALE;
4116 if( res==0 ){
4117 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004118 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004119#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004120 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004121#endif
drh8721ce42001-11-07 14:22:00 +00004122 }
drhf0863fe2005-06-12 21:35:51 +00004123 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004124 break;
4125}
4126
danielk1977de630352009-05-04 11:42:29 +00004127/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004128**
drhaa9b8962008-01-08 02:57:55 +00004129** Register P2 holds a SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004130** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004131** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004132**
drhaa9b8962008-01-08 02:57:55 +00004133** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004134** insert is likely to be an append.
4135**
drhf0863fe2005-06-12 21:35:51 +00004136** This instruction only works for indices. The equivalent instruction
4137** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004138*/
drh9cbf3422008-01-17 16:22:13 +00004139case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004140 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004141 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004142 int nKey;
4143 const char *zKey;
4144
drh653b82a2009-06-22 11:10:47 +00004145 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4146 pC = p->apCsr[pOp->p1];
4147 assert( pC!=0 );
drhaa9b8962008-01-08 02:57:55 +00004148 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004149 pCrsr = pC->pCursor;
4150 if( pCrsr!=0 ){
drhf0863fe2005-06-12 21:35:51 +00004151 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004152 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004153 if( rc==SQLITE_OK ){
drh856c1032009-06-02 15:21:42 +00004154 nKey = pIn2->n;
4155 zKey = pIn2->z;
danielk1977de630352009-05-04 11:42:29 +00004156 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4157 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4158 );
danielk1977d908f5a2007-05-11 07:08:28 +00004159 assert( pC->deferredMoveto==0 );
4160 pC->cacheStatus = CACHE_STALE;
4161 }
drh5e00f6c2001-09-13 13:46:56 +00004162 }
drh5e00f6c2001-09-13 13:46:56 +00004163 break;
4164}
4165
drhd1d38482008-10-07 23:46:38 +00004166/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004167**
drhe14006d2008-03-25 17:23:32 +00004168** The content of P3 registers starting at register P2 form
4169** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004170** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004171*/
drhe14006d2008-03-25 17:23:32 +00004172case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004173 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004174 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004175
drhe14006d2008-03-25 17:23:32 +00004176 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004177 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004178 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4179 pC = p->apCsr[pOp->p1];
4180 assert( pC!=0 );
4181 pCrsr = pC->pCursor;
4182 if( pCrsr!=0 ){
danielk197775bab7d2006-01-23 13:09:45 +00004183 int res;
drhe14006d2008-03-25 17:23:32 +00004184 UnpackedRecord r;
4185 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004186 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004187 r.flags = 0;
drhe14006d2008-03-25 17:23:32 +00004188 r.aMem = &p->aMem[pOp->p2];
drhe63d9992008-08-13 19:11:48 +00004189 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004190 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004191 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004192 }
drh9188b382004-05-14 21:12:22 +00004193 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004194 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004195 }
drh5e00f6c2001-09-13 13:46:56 +00004196 break;
4197}
4198
drh2133d822008-01-03 18:44:59 +00004199/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004200**
drh2133d822008-01-03 18:44:59 +00004201** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004202** the end of the index key pointed to by cursor P1. This integer should be
4203** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004204**
drh9437bd22009-02-01 00:29:56 +00004205** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004206*/
drh4c583122008-01-04 22:01:03 +00004207case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004208 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004209 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004210 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004211
drh653b82a2009-06-22 11:10:47 +00004212 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4213 pC = p->apCsr[pOp->p1];
4214 assert( pC!=0 );
4215 pCrsr = pC->pCursor;
4216 if( pCrsr!=0 ){
danielk1977c4d201c2009-04-07 09:16:56 +00004217 rc = sqlite3VdbeCursorMoveto(pC);
4218 if( rc ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004219 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004220 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004221 if( !pC->nullRow ){
drhb21c8cd2007-08-21 19:33:56 +00004222 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004223 if( rc!=SQLITE_OK ){
4224 goto abort_due_to_error;
4225 }
danielk1977a7a8e142008-02-13 18:25:27 +00004226 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00004227 pOut->u.i = rowid;
danielk19773d1bfea2004-05-14 11:00:53 +00004228 }
drh8721ce42001-11-07 14:22:00 +00004229 }
4230 break;
4231}
4232
danielk197761dd5832008-04-18 11:31:12 +00004233/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004234**
danielk197761dd5832008-04-18 11:31:12 +00004235** The P4 register values beginning with P3 form an unpacked index
4236** key that omits the ROWID. Compare this key value against the index
4237** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004238**
danielk197761dd5832008-04-18 11:31:12 +00004239** If the P1 index entry is greater than or equal to the key value
4240** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004241**
danielk197761dd5832008-04-18 11:31:12 +00004242** If P5 is non-zero then the key value is increased by an epsilon
4243** prior to the comparison. This make the opcode work like IdxGT except
4244** that if the key from register P3 is a prefix of the key in the cursor,
4245** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004246*/
drh98757152008-01-09 23:04:12 +00004247/* Opcode: IdxLT P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00004248**
danielk197761dd5832008-04-18 11:31:12 +00004249** The P4 register values beginning with P3 form an unpacked index
4250** key that omits the ROWID. Compare this key value against the index
4251** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004252**
danielk197761dd5832008-04-18 11:31:12 +00004253** If the P1 index entry is less than the key value then jump to P2.
4254** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004255**
danielk197761dd5832008-04-18 11:31:12 +00004256** If P5 is non-zero then the key value is increased by an epsilon prior
4257** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004258*/
drh9cbf3422008-01-17 16:22:13 +00004259case OP_IdxLT: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004260case OP_IdxGE: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00004261 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004262 int res;
4263 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004264
drh653b82a2009-06-22 11:10:47 +00004265 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4266 pC = p->apCsr[pOp->p1];
4267 assert( pC!=0 );
4268 if( pC->pCursor!=0 ){
drhd7556d22004-05-14 21:59:40 +00004269 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004270 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004271 assert( pOp->p4type==P4_INT32 );
4272 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004273 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004274 if( pOp->p5 ){
4275 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4276 }else{
4277 r.flags = UNPACKED_IGNORE_ROWID;
4278 }
danielk197761dd5832008-04-18 11:31:12 +00004279 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00004280 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004281 if( pOp->opcode==OP_IdxLT ){
4282 res = -res;
drha05a7222008-01-19 03:35:58 +00004283 }else{
4284 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004285 res++;
4286 }
4287 if( res>0 ){
4288 pc = pOp->p2 - 1 ;
4289 }
4290 }
4291 break;
4292}
4293
drh98757152008-01-09 23:04:12 +00004294/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004295**
4296** Delete an entire database table or index whose root page in the database
4297** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004298**
drh98757152008-01-09 23:04:12 +00004299** The table being destroyed is in the main database file if P3==0. If
4300** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004301** that is used to store tables create using CREATE TEMPORARY TABLE.
4302**
drh205f48e2004-11-05 00:43:11 +00004303** If AUTOVACUUM is enabled then it is possible that another root page
4304** might be moved into the newly deleted root page in order to keep all
4305** root pages contiguous at the beginning of the database. The former
4306** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004307** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004308** movement was required (because the table being dropped was already
4309** the last one in the database) then a zero is stored in register P2.
4310** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004311**
drhb19a2bc2001-09-16 00:13:26 +00004312** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004313*/
drh98757152008-01-09 23:04:12 +00004314case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004315 int iMoved;
drh3765df42006-06-28 18:18:09 +00004316 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004317 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004318 int iDb;
4319#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004320 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004321 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danielk1977212b2182006-06-23 14:32:08 +00004322 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4323 iCnt++;
4324 }
4325 }
drh3765df42006-06-28 18:18:09 +00004326#else
4327 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004328#endif
4329 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004330 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004331 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004332 }else{
drh856c1032009-06-02 15:21:42 +00004333 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004334 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004335 assert( (p->btreeMask & (1<<iDb))!=0 );
4336 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
danielk1977a7a8e142008-02-13 18:25:27 +00004337 MemSetTypeFlag(pOut, MEM_Int);
drh98757152008-01-09 23:04:12 +00004338 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004339#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004340 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004341 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
danielk1977e6efa742004-11-10 11:55:10 +00004342 }
drh3765df42006-06-28 18:18:09 +00004343#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004344 }
drh5e00f6c2001-09-13 13:46:56 +00004345 break;
4346}
4347
danielk1977c7af4842008-10-27 13:59:33 +00004348/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004349**
4350** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004351** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004352** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004353**
drhf57b3392001-10-08 13:22:32 +00004354** The table being clear is in the main database file if P2==0. If
4355** P2==1 then the table to be clear is in the auxiliary database file
4356** that is used to store tables create using CREATE TEMPORARY TABLE.
4357**
shanebe217792009-03-05 04:20:31 +00004358** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004359** intkey table (an SQL table, not an index). In this case the row change
4360** count is incremented by the number of rows in the table being cleared.
4361** If P3 is greater than zero, then the value stored in register P3 is
4362** also incremented by the number of rows in the table being cleared.
4363**
drhb19a2bc2001-09-16 00:13:26 +00004364** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004365*/
drh9cbf3422008-01-17 16:22:13 +00004366case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004367 int nChange;
4368
4369 nChange = 0;
drhfb982642007-08-30 01:19:59 +00004370 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004371 rc = sqlite3BtreeClearTable(
4372 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4373 );
4374 if( pOp->p3 ){
4375 p->nChange += nChange;
4376 if( pOp->p3>0 ){
4377 p->aMem[pOp->p3].u.i += nChange;
4378 }
4379 }
drh5edc3122001-09-13 21:53:09 +00004380 break;
4381}
4382
drh4c583122008-01-04 22:01:03 +00004383/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004384**
drh4c583122008-01-04 22:01:03 +00004385** Allocate a new table in the main database file if P1==0 or in the
4386** auxiliary database file if P1==1 or in an attached database if
4387** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004388** register P2
drh5b2fd562001-09-13 15:21:31 +00004389**
drhc6b52df2002-01-04 03:09:29 +00004390** The difference between a table and an index is this: A table must
4391** have a 4-byte integer key and can have arbitrary data. An index
4392** has an arbitrary key but no data.
4393**
drhb19a2bc2001-09-16 00:13:26 +00004394** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004395*/
drh4c583122008-01-04 22:01:03 +00004396/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004397**
drh4c583122008-01-04 22:01:03 +00004398** Allocate a new index in the main database file if P1==0 or in the
4399** auxiliary database file if P1==1 or in an attached database if
4400** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004401** register P2.
drhf57b3392001-10-08 13:22:32 +00004402**
drhc6b52df2002-01-04 03:09:29 +00004403** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004404*/
drh4c583122008-01-04 22:01:03 +00004405case OP_CreateIndex: /* out2-prerelease */
4406case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004407 int pgno;
drhf328bc82004-05-10 23:29:49 +00004408 int flags;
drh234c39d2004-07-24 03:30:47 +00004409 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004410
4411 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004412 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004413 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004414 pDb = &db->aDb[pOp->p1];
4415 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004416 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004417 /* flags = BTREE_INTKEY; */
4418 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004419 }else{
drhf328bc82004-05-10 23:29:49 +00004420 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00004421 }
drh234c39d2004-07-24 03:30:47 +00004422 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004423 pOut->u.i = pgno;
4424 MemSetTypeFlag(pOut, MEM_Int);
drh5b2fd562001-09-13 15:21:31 +00004425 break;
4426}
4427
drh98757152008-01-09 23:04:12 +00004428/* Opcode: ParseSchema P1 P2 * P4 *
drh234c39d2004-07-24 03:30:47 +00004429**
4430** Read and parse all entries from the SQLITE_MASTER table of database P1
drh66a51672008-01-03 00:01:23 +00004431** that match the WHERE clause P4. P2 is the "force" flag. Always do
drh3c23a882007-01-09 14:01:13 +00004432** the parsing if P2 is true. If P2 is false, then this routine is a
4433** no-op if the schema is not currently loaded. In other words, if P2
4434** is false, the SQLITE_MASTER table is only parsed if the rest of the
4435** schema is already loaded into the symbol table.
drh234c39d2004-07-24 03:30:47 +00004436**
4437** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004438** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004439*/
drh9cbf3422008-01-17 16:22:13 +00004440case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004441 int iDb;
4442 const char *zMaster;
4443 char *zSql;
4444 InitData initData;
4445
4446 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004447 assert( iDb>=0 && iDb<db->nDb );
danielk1977a8bbef82009-03-23 17:11:26 +00004448
4449 /* If pOp->p2 is 0, then this opcode is being executed to read a
4450 ** single row, for example the row corresponding to a new index
4451 ** created by this VDBE, from the sqlite_master table. It only
4452 ** does this if the corresponding in-memory schema is currently
4453 ** loaded. Otherwise, the new index definition can be loaded along
4454 ** with the rest of the schema when it is required.
4455 **
4456 ** Although the mutex on the BtShared object that corresponds to
4457 ** database iDb (the database containing the sqlite_master table
4458 ** read by this instruction) is currently held, it is necessary to
4459 ** obtain the mutexes on all attached databases before checking if
4460 ** the schema of iDb is loaded. This is because, at the start of
4461 ** the sqlite3_exec() call below, SQLite will invoke
4462 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
4463 ** iDb mutex may be temporarily released to avoid deadlock. If
4464 ** this happens, then some other thread may delete the in-memory
4465 ** schema of database iDb before the SQL statement runs. The schema
4466 ** will not be reloaded becuase the db->init.busy flag is set. This
4467 ** can result in a "no such table: sqlite_master" or "malformed
4468 ** database schema" error being returned to the user.
4469 */
4470 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4471 sqlite3BtreeEnterAll(db);
4472 if( pOp->p2 || DbHasProperty(db, iDb, DB_SchemaLoaded) ){
drh856c1032009-06-02 15:21:42 +00004473 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004474 initData.db = db;
4475 initData.iDb = pOp->p1;
4476 initData.pzErrMsg = &p->zErrMsg;
4477 zSql = sqlite3MPrintf(db,
4478 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
4479 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4480 if( zSql==0 ){
4481 rc = SQLITE_NOMEM;
4482 }else{
4483 (void)sqlite3SafetyOff(db);
4484 assert( db->init.busy==0 );
4485 db->init.busy = 1;
4486 initData.rc = SQLITE_OK;
4487 assert( !db->mallocFailed );
4488 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4489 if( rc==SQLITE_OK ) rc = initData.rc;
4490 sqlite3DbFree(db, zSql);
4491 db->init.busy = 0;
4492 (void)sqlite3SafetyOn(db);
4493 }
drh3c23a882007-01-09 14:01:13 +00004494 }
danielk1977a8bbef82009-03-23 17:11:26 +00004495 sqlite3BtreeLeaveAll(db);
danielk1977261919c2005-12-06 12:52:59 +00004496 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004497 goto no_mem;
4498 }
drh234c39d2004-07-24 03:30:47 +00004499 break;
4500}
4501
drh8bfdf722009-06-19 14:06:03 +00004502#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004503/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004504**
4505** Read the sqlite_stat1 table for database P1 and load the content
4506** of that table into the internal index hash table. This will cause
4507** the analysis to be used when preparing all subsequent queries.
4508*/
drh9cbf3422008-01-17 16:22:13 +00004509case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004510 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4511 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004512 break;
4513}
drh8bfdf722009-06-19 14:06:03 +00004514#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004515
drh98757152008-01-09 23:04:12 +00004516/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004517**
4518** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004519** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004520** is dropped in order to keep the internal representation of the
4521** schema consistent with what is on disk.
4522*/
drh9cbf3422008-01-17 16:22:13 +00004523case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004524 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004525 break;
4526}
4527
drh98757152008-01-09 23:04:12 +00004528/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004529**
4530** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004531** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004532** is dropped in order to keep the internal representation of the
4533** schema consistent with what is on disk.
4534*/
drh9cbf3422008-01-17 16:22:13 +00004535case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004536 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004537 break;
4538}
4539
drh98757152008-01-09 23:04:12 +00004540/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004541**
4542** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004543** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004544** is dropped in order to keep the internal representation of the
4545** schema consistent with what is on disk.
4546*/
drh9cbf3422008-01-17 16:22:13 +00004547case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004548 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004549 break;
4550}
4551
drh234c39d2004-07-24 03:30:47 +00004552
drhb7f91642004-10-31 02:22:47 +00004553#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004554/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004555**
drh98757152008-01-09 23:04:12 +00004556** Do an analysis of the currently open database. Store in
4557** register P1 the text of an error message describing any problems.
4558** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004559**
drh98757152008-01-09 23:04:12 +00004560** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004561** At most reg(P3) errors will be reported.
4562** In other words, the analysis stops as soon as reg(P1) errors are
4563** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004564**
drh79069752004-05-22 21:30:40 +00004565** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004566** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004567** total.
drh21504322002-06-25 13:16:02 +00004568**
drh98757152008-01-09 23:04:12 +00004569** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004570** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004571**
drh1dcdbc02007-01-27 02:24:54 +00004572** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004573*/
drhaaab5722002-02-19 13:39:21 +00004574case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004575 int nRoot; /* Number of tables to check. (Number of root pages.) */
4576 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4577 int j; /* Loop counter */
4578 int nErr; /* Number of errors reported */
4579 char *z; /* Text of the error report */
4580 Mem *pnErr; /* Register keeping track of errors remaining */
4581
4582 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004583 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004584 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004585 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004586 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4587 pnErr = &p->aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004588 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004589 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
4590 pIn1 = &p->aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004591 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004592 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004593 }
4594 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004595 assert( pOp->p5<db->nDb );
4596 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4597 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004598 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004599 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004600 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004601 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004602 if( nErr==0 ){
4603 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004604 }else if( z==0 ){
4605 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004606 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004607 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004608 }
drhb7654112008-01-12 12:48:07 +00004609 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004610 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004611 break;
4612}
drhb7f91642004-10-31 02:22:47 +00004613#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004614
drh3d4501e2008-12-04 20:40:10 +00004615/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004616**
drh3d4501e2008-12-04 20:40:10 +00004617** Insert the integer value held by register P2 into a boolean index
4618** held in register P1.
4619**
4620** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004621*/
drh3d4501e2008-12-04 20:40:10 +00004622case OP_RowSetAdd: { /* in2 */
4623 Mem *pIdx;
4624 Mem *pVal;
4625 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4626 pIdx = &p->aMem[pOp->p1];
4627 assert( pOp->p2>0 && pOp->p2<=p->nMem );
4628 pVal = &p->aMem[pOp->p2];
4629 assert( (pVal->flags & MEM_Int)!=0 );
4630 if( (pIdx->flags & MEM_RowSet)==0 ){
4631 sqlite3VdbeMemSetRowSet(pIdx);
drh8d993632008-12-04 22:17:55 +00004632 if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004633 }
4634 sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i);
4635 break;
4636}
4637
4638/* Opcode: RowSetRead P1 P2 P3 * *
4639**
4640** Extract the smallest value from boolean index P1 and put that value into
4641** register P3. Or, if boolean index P1 is initially empty, leave P3
4642** unchanged and jump to instruction P2.
4643*/
4644case OP_RowSetRead: { /* jump, out3 */
4645 Mem *pIdx;
4646 i64 val;
4647 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4648 CHECK_FOR_INTERRUPT;
4649 pIdx = &p->aMem[pOp->p1];
drhdd5f5a62008-12-23 13:35:23 +00004650 pOut = &p->aMem[pOp->p3];
drh3d4501e2008-12-04 20:40:10 +00004651 if( (pIdx->flags & MEM_RowSet)==0
4652 || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0
4653 ){
4654 /* The boolean index is empty */
4655 sqlite3VdbeMemSetNull(pIdx);
4656 pc = pOp->p2 - 1;
4657 }else{
4658 /* A value was pulled from the index */
4659 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh3d4501e2008-12-04 20:40:10 +00004660 sqlite3VdbeMemSetInt64(pOut, val);
drh17435752007-08-16 04:30:38 +00004661 }
drh5e00f6c2001-09-13 13:46:56 +00004662 break;
4663}
4664
drh1b26c7c2009-04-22 02:15:47 +00004665/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00004666**
drhade97602009-04-21 15:05:18 +00004667** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00004668** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00004669** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00004670** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00004671** next opcode.
danielk19771d461462009-04-21 09:02:45 +00004672**
drh1b26c7c2009-04-22 02:15:47 +00004673** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00004674** of integers, where each set contains no duplicates. Each set
4675** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00004676** must have P4==0, the final set P4=-1. P4 must be either -1 or
4677** non-negative. For non-negative values of P4 only the lower 4
4678** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00004679**
4680** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00004681** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00004682** (b) when P4==-1 there is no need to insert the value, as it will
4683** never be tested for, and (c) when a value that is part of set X is
4684** inserted, there is no need to search to see if the same value was
4685** previously inserted as part of set X (only if it was previously
4686** inserted as part of some other set).
4687*/
drh1b26c7c2009-04-22 02:15:47 +00004688case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00004689 int iSet;
4690 int exists;
4691
4692 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00004693 assert( pIn3->flags&MEM_Int );
4694
drh1b26c7c2009-04-22 02:15:47 +00004695 /* If there is anything other than a rowset object in memory cell P1,
4696 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00004697 */
drh733bf1b2009-04-22 00:47:00 +00004698 if( (pIn1->flags & MEM_RowSet)==0 ){
4699 sqlite3VdbeMemSetRowSet(pIn1);
4700 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00004701 }
4702
4703 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00004704 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00004705 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00004706 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
4707 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00004708 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004709 if( exists ){
4710 pc = pOp->p2 - 1;
4711 break;
4712 }
4713 }
4714 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00004715 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004716 }
4717 break;
4718}
4719
drh5e00f6c2001-09-13 13:46:56 +00004720
danielk197793758c82005-01-21 08:13:14 +00004721#ifndef SQLITE_OMIT_TRIGGER
rdcb0c374f2004-02-20 22:53:38 +00004722/* Opcode: ContextPush * * *
4723**
4724** Save the current Vdbe context such that it can be restored by a ContextPop
4725** opcode. The context stores the last insert row id, the last statement change
4726** count, and the current statement change count.
4727*/
drh9cbf3422008-01-17 16:22:13 +00004728case OP_ContextPush: {
drh856c1032009-06-02 15:21:42 +00004729 int i;
drh344737f2004-09-19 00:50:20 +00004730 Context *pContext;
danielk1977b28af712004-06-21 06:50:26 +00004731
drh856c1032009-06-02 15:21:42 +00004732 i = p->contextStackTop++;
drh344737f2004-09-19 00:50:20 +00004733 assert( i>=0 );
danielk1977b28af712004-06-21 06:50:26 +00004734 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
drh344737f2004-09-19 00:50:20 +00004735 if( i>=p->contextStackDepth ){
4736 p->contextStackDepth = i+1;
danielk19771e536952007-08-16 10:09:01 +00004737 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
drhcf643722007-03-27 13:36:37 +00004738 sizeof(Context)*(i+1));
drh344737f2004-09-19 00:50:20 +00004739 if( p->contextStack==0 ) goto no_mem;
4740 }
4741 pContext = &p->contextStack[i];
4742 pContext->lastRowid = db->lastRowid;
4743 pContext->nChange = p->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004744 break;
4745}
4746
4747/* Opcode: ContextPop * * *
4748**
4749** Restore the Vdbe context to the state it was in when contextPush was last
4750** executed. The context stores the last insert row id, the last statement
4751** change count, and the current statement change count.
4752*/
drh9cbf3422008-01-17 16:22:13 +00004753case OP_ContextPop: {
drh856c1032009-06-02 15:21:42 +00004754 Context *pContext;
4755 pContext = &p->contextStack[--p->contextStackTop];
drh344737f2004-09-19 00:50:20 +00004756 assert( p->contextStackTop>=0 );
4757 db->lastRowid = pContext->lastRowid;
4758 p->nChange = pContext->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004759 break;
4760}
danielk197793758c82005-01-21 08:13:14 +00004761#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004762
drh205f48e2004-11-05 00:43:11 +00004763#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00004764/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00004765**
drh98757152008-01-09 23:04:12 +00004766** Set the value of register P1 to the maximum of its current value
4767** and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00004768**
4769** This instruction throws an error if the memory cell is not initially
4770** an integer.
4771*/
drh9cbf3422008-01-17 16:22:13 +00004772case OP_MemMax: { /* in1, in2 */
drh98757152008-01-09 23:04:12 +00004773 sqlite3VdbeMemIntegerify(pIn1);
4774 sqlite3VdbeMemIntegerify(pIn2);
4775 if( pIn1->u.i<pIn2->u.i){
4776 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00004777 }
4778 break;
4779}
4780#endif /* SQLITE_OMIT_AUTOINCREMENT */
4781
drh98757152008-01-09 23:04:12 +00004782/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00004783**
drh98757152008-01-09 23:04:12 +00004784** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004785**
drh98757152008-01-09 23:04:12 +00004786** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004787** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00004788*/
drh9cbf3422008-01-17 16:22:13 +00004789case OP_IfPos: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004790 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004791 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00004792 pc = pOp->p2 - 1;
4793 }
4794 break;
4795}
4796
drh98757152008-01-09 23:04:12 +00004797/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00004798**
drh98757152008-01-09 23:04:12 +00004799** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00004800**
drh98757152008-01-09 23:04:12 +00004801** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00004802** not contain an integer. An assertion fault will result if you try.
4803*/
drh9cbf3422008-01-17 16:22:13 +00004804case OP_IfNeg: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004805 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004806 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00004807 pc = pOp->p2 - 1;
4808 }
4809 break;
4810}
4811
drh98757152008-01-09 23:04:12 +00004812/* Opcode: IfZero P1 P2 * * *
drhec7429a2005-10-06 16:53:14 +00004813**
drh98757152008-01-09 23:04:12 +00004814** If the value of register P1 is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004815**
drh98757152008-01-09 23:04:12 +00004816** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004817** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00004818*/
drh9cbf3422008-01-17 16:22:13 +00004819case OP_IfZero: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004820 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004821 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00004822 pc = pOp->p2 - 1;
4823 }
4824 break;
4825}
4826
drh98757152008-01-09 23:04:12 +00004827/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00004828**
drh0bce8352002-02-28 00:41:10 +00004829** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00004830** function has P5 arguments. P4 is a pointer to the FuncDef
4831** structure that specifies the function. Use register
4832** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00004833**
drh98757152008-01-09 23:04:12 +00004834** The P5 arguments are taken from register P2 and its
4835** successors.
drhe5095352002-02-24 03:25:14 +00004836*/
drh9cbf3422008-01-17 16:22:13 +00004837case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00004838 int n;
drhe5095352002-02-24 03:25:14 +00004839 int i;
drhc54a6172009-06-02 16:06:03 +00004840 Mem *pMem;
4841 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004842 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004843 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004844
drh856c1032009-06-02 15:21:42 +00004845 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00004846 assert( n>=0 );
drh98757152008-01-09 23:04:12 +00004847 pRec = &p->aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00004848 apVal = p->apArg;
4849 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00004850 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004851 apVal[i] = pRec;
drh8079a0d2006-01-12 17:20:50 +00004852 storeTypeInfo(pRec, encoding);
drhe5095352002-02-24 03:25:14 +00004853 }
danielk19772dca4ac2008-01-03 11:50:29 +00004854 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00004855 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4856 ctx.pMem = pMem = &p->aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00004857 pMem->n++;
drh90669c12006-01-20 15:45:36 +00004858 ctx.s.flags = MEM_Null;
4859 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00004860 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00004861 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00004862 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00004863 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00004864 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00004865 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00004866 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00004867 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00004868 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00004869 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00004870 }
danielk19776ddcca52004-05-24 23:48:25 +00004871 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00004872 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00004873 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00004874 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00004875 }
drh90669c12006-01-20 15:45:36 +00004876 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00004877 break;
4878}
4879
drh98757152008-01-09 23:04:12 +00004880/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004881**
drh13449892005-09-07 21:22:45 +00004882** Execute the finalizer function for an aggregate. P1 is
4883** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00004884**
4885** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00004886** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00004887** argument is not used by this opcode. It is only there to disambiguate
4888** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00004889** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00004890** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00004891*/
drh9cbf3422008-01-17 16:22:13 +00004892case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00004893 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00004894 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh13449892005-09-07 21:22:45 +00004895 pMem = &p->aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00004896 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00004897 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh90669c12006-01-20 15:45:36 +00004898 if( rc==SQLITE_ERROR ){
drhf089aa42008-07-08 19:34:06 +00004899 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00004900 }
drh2dca8682008-03-21 17:13:13 +00004901 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00004902 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00004903 if( sqlite3VdbeMemTooBig(pMem) ){
4904 goto too_big;
4905 }
drh5e00f6c2001-09-13 13:46:56 +00004906 break;
4907}
4908
drh5e00f6c2001-09-13 13:46:56 +00004909
drhfdbcdee2007-03-27 14:44:50 +00004910#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00004911/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00004912**
4913** Vacuum the entire database. This opcode will cause other virtual
4914** machines to be created and run. It may not be called from within
4915** a transaction.
4916*/
drh9cbf3422008-01-17 16:22:13 +00004917case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004918 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4919 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4920 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004921 break;
4922}
drh154d4b22006-09-21 11:02:16 +00004923#endif
drh6f8c91c2003-12-07 00:24:35 +00004924
danielk1977dddbcdc2007-04-26 14:42:34 +00004925#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00004926/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00004927**
4928** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00004929** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00004930** P2. Otherwise, fall through to the next instruction.
4931*/
drh9cbf3422008-01-17 16:22:13 +00004932case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00004933 Btree *pBt;
4934
4935 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004936 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00004937 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00004938 rc = sqlite3BtreeIncrVacuum(pBt);
4939 if( rc==SQLITE_DONE ){
4940 pc = pOp->p2 - 1;
4941 rc = SQLITE_OK;
4942 }
4943 break;
4944}
4945#endif
4946
drh98757152008-01-09 23:04:12 +00004947/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00004948**
4949** Cause precompiled statements to become expired. An expired statement
4950** fails with an error code of SQLITE_SCHEMA if it is ever executed
4951** (via sqlite3_step()).
4952**
4953** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4954** then only the currently executing statement is affected.
4955*/
drh9cbf3422008-01-17 16:22:13 +00004956case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00004957 if( !pOp->p1 ){
4958 sqlite3ExpirePreparedStatements(db);
4959 }else{
4960 p->expired = 1;
4961 }
4962 break;
4963}
4964
danielk1977c00da102006-01-07 13:21:04 +00004965#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00004966/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00004967**
4968** Obtain a lock on a particular table. This instruction is only used when
4969** the shared-cache feature is enabled.
4970**
drh6a9ad3d2008-04-02 16:29:30 +00004971** If P1 is the index of the database in sqlite3.aDb[] of the database
4972** on which the lock is acquired. A readlock is obtained if P3==0 or
4973** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00004974**
4975** P2 contains the root-page of the table to lock.
4976**
drh66a51672008-01-03 00:01:23 +00004977** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00004978** used to generate an error message if the lock cannot be obtained.
4979*/
drh9cbf3422008-01-17 16:22:13 +00004980case OP_TableLock: {
drh856c1032009-06-02 15:21:42 +00004981 int p1;
4982 u8 isWriteLock;
4983
4984 p1 = pOp->p1;
4985 isWriteLock = (u8)pOp->p3;
drhfb982642007-08-30 01:19:59 +00004986 assert( p1>=0 && p1<db->nDb );
4987 assert( (p->btreeMask & (1<<p1))!=0 );
drh6a9ad3d2008-04-02 16:29:30 +00004988 assert( isWriteLock==0 || isWriteLock==1 );
danielk1977c00da102006-01-07 13:21:04 +00004989 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
danielk1977404ca072009-03-16 13:19:36 +00004990 if( (rc&0xFF)==SQLITE_LOCKED ){
danielk19772dca4ac2008-01-03 11:50:29 +00004991 const char *z = pOp->p4.z;
drhf089aa42008-07-08 19:34:06 +00004992 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
danielk1977c00da102006-01-07 13:21:04 +00004993 }
4994 break;
4995}
drhb9bb7c12006-06-11 23:41:55 +00004996#endif /* SQLITE_OMIT_SHARED_CACHE */
4997
4998#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004999/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005000**
danielk19773e3a84d2008-08-01 17:37:40 +00005001** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5002** xBegin method for that table.
5003**
5004** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005005** within a callback to a virtual table xSync() method. If it is, the error
5006** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005007*/
drh9cbf3422008-01-17 16:22:13 +00005008case OP_VBegin: {
drh856c1032009-06-02 15:21:42 +00005009 sqlite3_vtab *pVtab;
5010 pVtab = pOp->p4.pVtab;
danielk19773e3a84d2008-08-01 17:37:40 +00005011 rc = sqlite3VtabBegin(db, pVtab);
5012 if( pVtab ){
5013 sqlite3DbFree(db, p->zErrMsg);
5014 p->zErrMsg = pVtab->zErrMsg;
5015 pVtab->zErrMsg = 0;
5016 }
danielk1977f9e7dda2006-06-16 16:08:53 +00005017 break;
5018}
5019#endif /* SQLITE_OMIT_VIRTUALTABLE */
5020
5021#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005022/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005023**
drh66a51672008-01-03 00:01:23 +00005024** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005025** for that table.
5026*/
drh9cbf3422008-01-17 16:22:13 +00005027case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005028 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005029 break;
5030}
5031#endif /* SQLITE_OMIT_VIRTUALTABLE */
5032
5033#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005034/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005035**
drh66a51672008-01-03 00:01:23 +00005036** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005037** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005038*/
drh9cbf3422008-01-17 16:22:13 +00005039case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005040 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005041 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005042 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005043 break;
5044}
5045#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005046
drh9eff6162006-06-12 21:59:13 +00005047#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005048/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005049**
drh66a51672008-01-03 00:01:23 +00005050** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005051** P1 is a cursor number. This opcode opens a cursor to the virtual
5052** table and stores that cursor in P1.
5053*/
drh9cbf3422008-01-17 16:22:13 +00005054case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005055 VdbeCursor *pCur;
5056 sqlite3_vtab_cursor *pVtabCursor;
5057 sqlite3_vtab *pVtab;
5058 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005059
drh856c1032009-06-02 15:21:42 +00005060 pCur = 0;
5061 pVtabCursor = 0;
5062 pVtab = pOp->p4.pVtab;
5063 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005064 assert(pVtab && pModule);
5065 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5066 rc = pModule->xOpen(pVtab, &pVtabCursor);
drh633e6d52008-07-28 19:34:53 +00005067 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005068 p->zErrMsg = pVtab->zErrMsg;
5069 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005070 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5071 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005072 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005073 pVtabCursor->pVtab = pVtab;
5074
5075 /* Initialise vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005076 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005077 if( pCur ){
5078 pCur->pVtabCursor = pVtabCursor;
5079 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005080 }else{
drh17435752007-08-16 04:30:38 +00005081 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005082 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005083 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005084 }
drh9eff6162006-06-12 21:59:13 +00005085 break;
5086}
5087#endif /* SQLITE_OMIT_VIRTUALTABLE */
5088
5089#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005090/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005091**
5092** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5093** the filtered result set is empty.
5094**
drh66a51672008-01-03 00:01:23 +00005095** P4 is either NULL or a string that was generated by the xBestIndex
5096** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005097** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005098**
drh9eff6162006-06-12 21:59:13 +00005099** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005100** by P1. The integer query plan parameter to xFilter is stored in register
5101** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005102** xFilter method. Registers P3+2..P3+1+argc are the argc
5103** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005104** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005105**
danielk19776dbee812008-01-03 18:39:41 +00005106** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005107*/
drh9cbf3422008-01-17 16:22:13 +00005108case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005109 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005110 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005111 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005112 Mem *pQuery;
5113 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005114 sqlite3_vtab_cursor *pVtabCursor;
5115 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005116 VdbeCursor *pCur;
5117 int res;
5118 int i;
5119 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005120
drh856c1032009-06-02 15:21:42 +00005121 pQuery = &p->aMem[pOp->p3];
5122 pArgc = &pQuery[1];
5123 pCur = p->apCsr[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00005124 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005125 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005126 pVtabCursor = pCur->pVtabCursor;
5127 pVtab = pVtabCursor->pVtab;
5128 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005129
drh9cbf3422008-01-17 16:22:13 +00005130 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005131 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005132 nArg = (int)pArgc->u.i;
5133 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005134
drh644a5292006-12-20 14:53:38 +00005135 /* Invoke the xFilter method */
5136 {
drh856c1032009-06-02 15:21:42 +00005137 res = 0;
5138 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005139 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005140 apArg[i] = &pArgc[i+1];
drh4be8b512006-06-13 23:51:34 +00005141 storeTypeInfo(apArg[i], 0);
danielk19775fac9f82006-06-13 14:16:58 +00005142 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005143
5144 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00005145 sqlite3VtabLock(pVtab);
danielk1977be718892006-06-23 08:05:19 +00005146 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005147 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005148 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005149 sqlite3DbFree(db, p->zErrMsg);
5150 p->zErrMsg = pVtab->zErrMsg;
5151 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00005152 sqlite3VtabUnlock(db, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005153 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005154 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005155 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005156 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5157
danielk1977a298e902006-06-22 09:53:48 +00005158 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005159 pc = pOp->p2 - 1;
5160 }
5161 }
drh1d454a32008-01-31 19:34:51 +00005162 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005163
drh9eff6162006-06-12 21:59:13 +00005164 break;
5165}
5166#endif /* SQLITE_OMIT_VIRTUALTABLE */
5167
5168#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005169/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005170**
drh2133d822008-01-03 18:44:59 +00005171** Store the value of the P2-th column of
5172** the row of the virtual-table that the
5173** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005174*/
5175case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005176 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005177 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005178 Mem *pDest;
5179 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005180
drhdfe88ec2008-11-03 20:55:06 +00005181 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005182 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005183 assert( pOp->p3>0 && pOp->p3<=p->nMem );
5184 pDest = &p->aMem[pOp->p3];
5185 if( pCur->nullRow ){
5186 sqlite3VdbeMemSetNull(pDest);
5187 break;
5188 }
danielk19773e3a84d2008-08-01 17:37:40 +00005189 pVtab = pCur->pVtabCursor->pVtab;
5190 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005191 assert( pModule->xColumn );
5192 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005193
5194 /* The output cell may already have a buffer allocated. Move
5195 ** the current contents to sContext.s so in case the user-function
5196 ** can use the already allocated buffer instead of allocating a
5197 ** new one.
5198 */
5199 sqlite3VdbeMemMove(&sContext.s, pDest);
5200 MemSetTypeFlag(&sContext.s, MEM_Null);
5201
drhde4fcfd2008-01-19 23:50:26 +00005202 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5203 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
danielk19773e3a84d2008-08-01 17:37:40 +00005204 sqlite3DbFree(db, p->zErrMsg);
5205 p->zErrMsg = pVtab->zErrMsg;
5206 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005207
drhde4fcfd2008-01-19 23:50:26 +00005208 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005209 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005210 ** dynamic allocation in sContext.s (a Mem struct) is released.
5211 */
5212 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005213 REGISTER_TRACE(pOp->p3, pDest);
5214 sqlite3VdbeMemMove(pDest, &sContext.s);
5215 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005216
drhde4fcfd2008-01-19 23:50:26 +00005217 if( sqlite3SafetyOn(db) ){
5218 goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005219 }
drhde4fcfd2008-01-19 23:50:26 +00005220 if( sqlite3VdbeMemTooBig(pDest) ){
5221 goto too_big;
5222 }
drh9eff6162006-06-12 21:59:13 +00005223 break;
5224}
5225#endif /* SQLITE_OMIT_VIRTUALTABLE */
5226
5227#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005228/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005229**
5230** Advance virtual table P1 to the next row in its result set and
5231** jump to instruction P2. Or, if the virtual table has reached
5232** the end of its result set, then fall through to the next instruction.
5233*/
drh9cbf3422008-01-17 16:22:13 +00005234case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005235 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005236 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005237 int res;
drh856c1032009-06-02 15:21:42 +00005238 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005239
drhc54a6172009-06-02 16:06:03 +00005240 res = 0;
drh856c1032009-06-02 15:21:42 +00005241 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005242 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005243 if( pCur->nullRow ){
5244 break;
5245 }
danielk19773e3a84d2008-08-01 17:37:40 +00005246 pVtab = pCur->pVtabCursor->pVtab;
5247 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005248 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005249
drhde4fcfd2008-01-19 23:50:26 +00005250 /* Invoke the xNext() method of the module. There is no way for the
5251 ** underlying implementation to return an error if one occurs during
5252 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5253 ** data is available) and the error code returned when xColumn or
5254 ** some other method is next invoked on the save virtual table cursor.
5255 */
5256 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00005257 sqlite3VtabLock(pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005258 p->inVtabMethod = 1;
5259 rc = pModule->xNext(pCur->pVtabCursor);
5260 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005261 sqlite3DbFree(db, p->zErrMsg);
5262 p->zErrMsg = pVtab->zErrMsg;
5263 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00005264 sqlite3VtabUnlock(db, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005265 if( rc==SQLITE_OK ){
5266 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005267 }
drhde4fcfd2008-01-19 23:50:26 +00005268 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005269
drhde4fcfd2008-01-19 23:50:26 +00005270 if( !res ){
5271 /* If there is data, jump to P2 */
5272 pc = pOp->p2 - 1;
5273 }
drh9eff6162006-06-12 21:59:13 +00005274 break;
5275}
5276#endif /* SQLITE_OMIT_VIRTUALTABLE */
5277
danielk1977182c4ba2007-06-27 15:53:34 +00005278#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005279/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005280**
drh66a51672008-01-03 00:01:23 +00005281** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005282** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005283** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005284*/
drh9cbf3422008-01-17 16:22:13 +00005285case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00005286 sqlite3_vtab *pVtab;
5287 Mem *pName;
5288
5289 pVtab = pOp->p4.pVtab;
5290 pName = &p->aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005291 assert( pVtab->pModule->xRename );
drh5b6afba2008-01-05 16:29:28 +00005292 REGISTER_TRACE(pOp->p1, pName);
danielk1977182c4ba2007-06-27 15:53:34 +00005293
danielk19776dbee812008-01-03 18:39:41 +00005294 Stringify(pName, encoding);
danielk1977182c4ba2007-06-27 15:53:34 +00005295
5296 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5297 sqlite3VtabLock(pVtab);
danielk19776dbee812008-01-03 18:39:41 +00005298 rc = pVtab->pModule->xRename(pVtab, pName->z);
drh633e6d52008-07-28 19:34:53 +00005299 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005300 p->zErrMsg = pVtab->zErrMsg;
5301 pVtab->zErrMsg = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005302 sqlite3VtabUnlock(db, pVtab);
5303 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5304
danielk1977182c4ba2007-06-27 15:53:34 +00005305 break;
5306}
5307#endif
drh4cbdda92006-06-14 19:00:20 +00005308
5309#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005310/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005311**
drh66a51672008-01-03 00:01:23 +00005312** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005313** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005314** are contiguous memory cells starting at P3 to pass to the xUpdate
5315** invocation. The value in register (P3+P2-1) corresponds to the
5316** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005317**
5318** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005319** The argv[0] element (which corresponds to memory cell P3)
5320** is the rowid of a row to delete. If argv[0] is NULL then no
5321** deletion occurs. The argv[1] element is the rowid of the new
5322** row. This can be NULL to have the virtual table select the new
5323** rowid for itself. The subsequent elements in the array are
5324** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005325**
5326** If P2==1 then no insert is performed. argv[0] is the rowid of
5327** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005328**
5329** P1 is a boolean flag. If it is set to true and the xUpdate call
5330** is successful, then the value returned by sqlite3_last_insert_rowid()
5331** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005332*/
drh9cbf3422008-01-17 16:22:13 +00005333case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00005334 sqlite3_vtab *pVtab;
5335 sqlite3_module *pModule;
5336 int nArg;
5337 int i;
5338 sqlite_int64 rowid;
5339 Mem **apArg;
5340 Mem *pX;
5341
5342 pVtab = pOp->p4.pVtab;
5343 pModule = (sqlite3_module *)pVtab->pModule;
5344 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005345 assert( pOp->p4type==P4_VTAB );
danielk1977399918f2006-06-14 13:03:23 +00005346 if( pModule->xUpdate==0 ){
drhf089aa42008-07-08 19:34:06 +00005347 sqlite3SetString(&p->zErrMsg, db, "read-only table");
danielk1977399918f2006-06-14 13:03:23 +00005348 rc = SQLITE_ERROR;
5349 }else{
drh856c1032009-06-02 15:21:42 +00005350 apArg = p->apArg;
5351 pX = &p->aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00005352 for(i=0; i<nArg; i++){
drh9c419382006-06-16 21:13:21 +00005353 storeTypeInfo(pX, 0);
5354 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005355 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005356 }
danielk1977c7d54102006-06-15 07:29:00 +00005357 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh189d4af2006-09-02 20:57:52 +00005358 sqlite3VtabLock(pVtab);
danielk19771f6eec52006-06-16 06:17:47 +00005359 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drh633e6d52008-07-28 19:34:53 +00005360 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005361 p->zErrMsg = pVtab->zErrMsg;
5362 pVtab->zErrMsg = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00005363 sqlite3VtabUnlock(db, pVtab);
danielk1977c7d54102006-06-15 07:29:00 +00005364 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk19771f6eec52006-06-16 06:17:47 +00005365 if( pOp->p1 && rc==SQLITE_OK ){
5366 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5367 db->lastRowid = rowid;
5368 }
drhb5df1442008-04-10 14:00:09 +00005369 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00005370 }
drh4cbdda92006-06-14 19:00:20 +00005371 break;
danielk1977399918f2006-06-14 13:03:23 +00005372}
5373#endif /* SQLITE_OMIT_VIRTUALTABLE */
5374
danielk197759a93792008-05-15 17:48:20 +00005375#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5376/* Opcode: Pagecount P1 P2 * * *
5377**
5378** Write the current number of pages in database P1 to memory cell P2.
5379*/
5380case OP_Pagecount: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00005381 int p1;
danielk197759a93792008-05-15 17:48:20 +00005382 int nPage;
drh856c1032009-06-02 15:21:42 +00005383 Pager *pPager;
danielk197759a93792008-05-15 17:48:20 +00005384
drh856c1032009-06-02 15:21:42 +00005385 p1 = pOp->p1;
5386 pPager = sqlite3BtreePager(db->aDb[p1].pBt);
danielk1977ad0132d2008-06-07 08:58:22 +00005387 rc = sqlite3PagerPagecount(pPager, &nPage);
5388 if( rc==SQLITE_OK ){
danielk197759a93792008-05-15 17:48:20 +00005389 pOut->flags = MEM_Int;
5390 pOut->u.i = nPage;
5391 }
5392 break;
5393}
5394#endif
5395
drh949f9cd2008-01-12 21:35:57 +00005396#ifndef SQLITE_OMIT_TRACE
5397/* Opcode: Trace * * * P4 *
5398**
5399** If tracing is enabled (by the sqlite3_trace()) interface, then
5400** the UTF-8 string contained in P4 is emitted on the trace callback.
5401*/
5402case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00005403 char *zTrace;
5404
5405 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
danielk19776ab3a2e2009-02-19 14:39:25 +00005406 if( zTrace ){
drh949f9cd2008-01-12 21:35:57 +00005407 if( db->xTrace ){
danielk19776ab3a2e2009-02-19 14:39:25 +00005408 db->xTrace(db->pTraceArg, zTrace);
drh949f9cd2008-01-12 21:35:57 +00005409 }
5410#ifdef SQLITE_DEBUG
5411 if( (db->flags & SQLITE_SqlTrace)!=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00005412 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
drh949f9cd2008-01-12 21:35:57 +00005413 }
5414#endif /* SQLITE_DEBUG */
5415 }
5416 break;
5417}
5418#endif
5419
drh91fd4d42008-01-19 20:11:25 +00005420
5421/* Opcode: Noop * * * * *
5422**
5423** Do nothing. This instruction is often useful as a jump
5424** destination.
drh5e00f6c2001-09-13 13:46:56 +00005425*/
drh91fd4d42008-01-19 20:11:25 +00005426/*
5427** The magic Explain opcode are only inserted when explain==2 (which
5428** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5429** This opcode records information from the optimizer. It is the
5430** the same as a no-op. This opcodesnever appears in a real VM program.
5431*/
5432default: { /* This is really OP_Noop and OP_Explain */
drh5e00f6c2001-09-13 13:46:56 +00005433 break;
5434}
5435
5436/*****************************************************************************
5437** The cases of the switch statement above this line should all be indented
5438** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5439** readability. From this point on down, the normal indentation rules are
5440** restored.
5441*****************************************************************************/
5442 }
drh6e142f52000-06-08 13:36:40 +00005443
drh7b396862003-01-01 23:06:20 +00005444#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005445 {
shane9bcbdad2008-05-29 20:22:37 +00005446 u64 elapsed = sqlite3Hwtime() - start;
5447 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005448 pOp->cnt++;
5449#if 0
shane9bcbdad2008-05-29 20:22:37 +00005450 fprintf(stdout, "%10llu ", elapsed);
danielk19774adee202004-05-08 08:23:19 +00005451 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005452#endif
5453 }
drh7b396862003-01-01 23:06:20 +00005454#endif
5455
drh6e142f52000-06-08 13:36:40 +00005456 /* The following code adds nothing to the actual functionality
5457 ** of the program. It is only here for testing and debugging.
5458 ** On the other hand, it does burn CPU cycles every time through
5459 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5460 */
5461#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005462 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005463
drhcf1023c2007-05-08 20:59:49 +00005464#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005465 if( p->trace ){
5466 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drhca48c902008-01-18 14:08:24 +00005467 if( opProperty & OPFLG_OUT2_PRERELEASE ){
drh5b6afba2008-01-05 16:29:28 +00005468 registerTrace(p->trace, pOp->p2, pOut);
drh75897232000-05-29 14:26:00 +00005469 }
drhca48c902008-01-18 14:08:24 +00005470 if( opProperty & OPFLG_OUT3 ){
drh5b6afba2008-01-05 16:29:28 +00005471 registerTrace(p->trace, pOp->p3, pOut);
5472 }
drh75897232000-05-29 14:26:00 +00005473 }
danielk1977b5402fb2005-01-12 07:15:04 +00005474#endif /* SQLITE_DEBUG */
5475#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005476 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005477
drha05a7222008-01-19 03:35:58 +00005478 /* If we reach this point, it means that execution is finished with
5479 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005480 */
drha05a7222008-01-19 03:35:58 +00005481vdbe_error_halt:
5482 assert( rc );
5483 p->rc = rc;
drh92f02c32004-09-02 14:57:08 +00005484 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005485 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5486 rc = SQLITE_ERROR;
drh900b31e2007-08-28 02:27:51 +00005487
5488 /* This is the only way out of this procedure. We have to
5489 ** release the mutexes on btrees that were acquired at the
5490 ** top. */
5491vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005492 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005493 return rc;
5494
drh023ae032007-05-08 12:12:16 +00005495 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5496 ** is encountered.
5497 */
5498too_big:
drhf089aa42008-07-08 19:34:06 +00005499 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005500 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005501 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005502
drh98640a32007-06-07 19:08:32 +00005503 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005504 */
5505no_mem:
drh17435752007-08-16 04:30:38 +00005506 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005507 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005508 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005509 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005510
5511 /* Jump to here for an SQLITE_MISUSE error.
5512 */
5513abort_due_to_misuse:
5514 rc = SQLITE_MISUSE;
5515 /* Fall thru into abort_due_to_error */
5516
5517 /* Jump to here for any other kind of fatal error. The "rc" variable
5518 ** should hold the error number.
5519 */
5520abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005521 assert( p->zErrMsg==0 );
5522 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005523 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005524 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005525 }
drha05a7222008-01-19 03:35:58 +00005526 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005527
danielk19776f8a5032004-05-10 10:34:51 +00005528 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005529 ** flag.
5530 */
5531abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005532 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005533 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005534 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005535 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005536 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005537}