blob: b6da22237ceda1cf3ffaf9f91ee02a537c6315cd [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**
drh9437bd22009-02-01 00:29:56 +000046** $Id: vdbe.c,v 1.813 2009/02/01 00:29:57 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 */
190 Op *pOp, /* */
drh3d4501e2008-12-04 20:40:10 +0000191 int iDb, /* When database the cursor belongs to, or -1 */
drhdfe88ec2008-11-03 20:55:06 +0000192 int isBtreeCursor /* */
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;
danielk1977cd3e8f72008-03-25 09:47:35 +0000216 /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains
217 ** the number of fields in the records contained in the table or
218 ** index being opened. Use this to reserve space for the
drhdfe88ec2008-11-03 20:55:06 +0000219 ** VdbeCursor.aType[] array.
danielk1977cd3e8f72008-03-25 09:47:35 +0000220 */
221 int nField = 0;
222 if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){
223 nField = pOp->p2;
224 }
danielk19775f096132008-03-28 15:44:09 +0000225 nByte =
drhdfe88ec2008-11-03 20:55:06 +0000226 sizeof(VdbeCursor) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000227 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
228 2*nField*sizeof(u32);
229
drh290c1942004-08-21 17:54:45 +0000230 assert( iCur<p->nCursor );
231 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000232 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000233 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000234 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000235 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000236 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
danielk1977cd3e8f72008-03-25 09:47:35 +0000237 memset(pMem->z, 0, nByte);
danielk197794eb6a12005-12-15 15:22:08 +0000238 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000239 pCx->nField = nField;
240 if( nField ){
drhdfe88ec2008-11-03 20:55:06 +0000241 pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000242 }
243 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000244 pCx->pCursor = (BtCursor*)
245 &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000246 }
danielk197794eb6a12005-12-15 15:22:08 +0000247 }
drh4774b132004-06-12 20:12:51 +0000248 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000249}
250
danielk19773d1bfea2004-05-14 11:00:53 +0000251/*
drh29d72102006-02-09 22:13:41 +0000252** Try to convert a value into a numeric representation if we can
253** do so without loss of information. In other words, if the string
254** looks like a number, convert it into a number. If it does not
255** look like a number, leave it alone.
256*/
drhb21c8cd2007-08-21 19:33:56 +0000257static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000258 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
259 int realnum;
drhb21c8cd2007-08-21 19:33:56 +0000260 sqlite3VdbeMemNulTerminate(pRec);
drh29d72102006-02-09 22:13:41 +0000261 if( (pRec->flags&MEM_Str)
262 && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
263 i64 value;
drhb21c8cd2007-08-21 19:33:56 +0000264 sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
drhb6a9ece2007-06-26 00:37:27 +0000265 if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
drh3c024d62007-03-30 11:23:45 +0000266 pRec->u.i = value;
danielk1977a7a8e142008-02-13 18:25:27 +0000267 MemSetTypeFlag(pRec, MEM_Int);
drh29d72102006-02-09 22:13:41 +0000268 }else{
269 sqlite3VdbeMemRealify(pRec);
270 }
271 }
272 }
273}
274
275/*
drh8a512562005-11-14 22:29:05 +0000276** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000277**
drh8a512562005-11-14 22:29:05 +0000278** SQLITE_AFF_INTEGER:
279** SQLITE_AFF_REAL:
280** SQLITE_AFF_NUMERIC:
281** Try to convert pRec to an integer representation or a
282** floating-point representation if an integer representation
283** is not possible. Note that the integer representation is
284** always preferred, even if the affinity is REAL, because
285** an integer representation is more space efficient on disk.
286**
287** SQLITE_AFF_TEXT:
288** Convert pRec to a text representation.
289**
290** SQLITE_AFF_NONE:
291** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000292*/
drh17435752007-08-16 04:30:38 +0000293static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000294 Mem *pRec, /* The value to apply affinity to */
295 char affinity, /* The affinity to be applied */
296 u8 enc /* Use this text encoding */
297){
drh8a512562005-11-14 22:29:05 +0000298 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000299 /* Only attempt the conversion to TEXT if there is an integer or real
300 ** representation (blob and NULL do not get converted) but no string
301 ** representation.
302 */
303 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000304 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000305 }
306 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000307 }else if( affinity!=SQLITE_AFF_NONE ){
308 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
309 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000310 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000311 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000312 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000313 }
danielk19773d1bfea2004-05-14 11:00:53 +0000314 }
315}
316
danielk1977aee18ef2005-03-09 12:26:50 +0000317/*
drh29d72102006-02-09 22:13:41 +0000318** Try to convert the type of a function argument or a result column
319** into a numeric representation. Use either INTEGER or REAL whichever
320** is appropriate. But only do the conversion if it is possible without
321** loss of information and return the revised type of the argument.
322**
323** This is an EXPERIMENTAL api and is subject to change or removal.
324*/
325int sqlite3_value_numeric_type(sqlite3_value *pVal){
326 Mem *pMem = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +0000327 applyNumericAffinity(pMem);
drh29d72102006-02-09 22:13:41 +0000328 storeTypeInfo(pMem, 0);
329 return pMem->type;
330}
331
332/*
danielk1977aee18ef2005-03-09 12:26:50 +0000333** Exported version of applyAffinity(). This one works on sqlite3_value*,
334** not the internal Mem* type.
335*/
danielk19771e536952007-08-16 10:09:01 +0000336void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000337 sqlite3_value *pVal,
338 u8 affinity,
339 u8 enc
340){
drhb21c8cd2007-08-21 19:33:56 +0000341 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000342}
343
danielk1977b5402fb2005-01-12 07:15:04 +0000344#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000345/*
danielk1977ca6b2912004-05-21 10:49:47 +0000346** Write a nice string representation of the contents of cell pMem
347** into buffer zBuf, length nBuf.
348*/
drh74161702006-02-24 02:53:49 +0000349void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000350 char *zCsr = zBuf;
351 int f = pMem->flags;
352
drh57196282004-10-06 15:41:16 +0000353 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000354
danielk1977ca6b2912004-05-21 10:49:47 +0000355 if( f&MEM_Blob ){
356 int i;
357 char c;
358 if( f & MEM_Dyn ){
359 c = 'z';
360 assert( (f & (MEM_Static|MEM_Ephem))==0 );
361 }else if( f & MEM_Static ){
362 c = 't';
363 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
364 }else if( f & MEM_Ephem ){
365 c = 'e';
366 assert( (f & (MEM_Static|MEM_Dyn))==0 );
367 }else{
368 c = 's';
369 }
370
drh5bb3eb92007-05-04 13:15:55 +0000371 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000372 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000373 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000374 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000375 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000376 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000377 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000378 }
379 for(i=0; i<16 && i<pMem->n; i++){
380 char z = pMem->z[i];
381 if( z<32 || z>126 ) *zCsr++ = '.';
382 else *zCsr++ = z;
383 }
384
drhe718efe2007-05-10 21:14:03 +0000385 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000386 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000387 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000388 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000389 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000390 }
danielk1977b1bc9532004-05-22 03:05:33 +0000391 *zCsr = '\0';
392 }else if( f & MEM_Str ){
393 int j, k;
394 zBuf[0] = ' ';
395 if( f & MEM_Dyn ){
396 zBuf[1] = 'z';
397 assert( (f & (MEM_Static|MEM_Ephem))==0 );
398 }else if( f & MEM_Static ){
399 zBuf[1] = 't';
400 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
401 }else if( f & MEM_Ephem ){
402 zBuf[1] = 'e';
403 assert( (f & (MEM_Static|MEM_Dyn))==0 );
404 }else{
405 zBuf[1] = 's';
406 }
407 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000408 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000409 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000410 zBuf[k++] = '[';
411 for(j=0; j<15 && j<pMem->n; j++){
412 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000413 if( c>=0x20 && c<0x7f ){
414 zBuf[k++] = c;
415 }else{
416 zBuf[k++] = '.';
417 }
418 }
419 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000420 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000421 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000422 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000423 }
danielk1977ca6b2912004-05-21 10:49:47 +0000424}
425#endif
426
drh5b6afba2008-01-05 16:29:28 +0000427#ifdef SQLITE_DEBUG
428/*
429** Print the value of a register for tracing purposes:
430*/
431static void memTracePrint(FILE *out, Mem *p){
432 if( p->flags & MEM_Null ){
433 fprintf(out, " NULL");
434 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
435 fprintf(out, " si:%lld", p->u.i);
436 }else if( p->flags & MEM_Int ){
437 fprintf(out, " i:%lld", p->u.i);
438 }else if( p->flags & MEM_Real ){
439 fprintf(out, " r:%g", p->r);
440 }else{
441 char zBuf[200];
442 sqlite3VdbeMemPrettyPrint(p, zBuf);
443 fprintf(out, " ");
444 fprintf(out, "%s", zBuf);
445 }
446}
447static void registerTrace(FILE *out, int iReg, Mem *p){
448 fprintf(out, "REG[%d] = ", iReg);
449 memTracePrint(out, p);
450 fprintf(out, "\n");
451}
452#endif
453
454#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000455# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000456#else
457# define REGISTER_TRACE(R,M)
458#endif
459
danielk197784ac9d02004-05-18 09:58:06 +0000460
drh7b396862003-01-01 23:06:20 +0000461#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000462
463/*
464** hwtime.h contains inline assembler code for implementing
465** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000466*/
shane9bcbdad2008-05-29 20:22:37 +0000467#include "hwtime.h"
468
drh7b396862003-01-01 23:06:20 +0000469#endif
470
drh8c74a8c2002-08-25 19:20:40 +0000471/*
drhcaec2f12003-01-07 02:47:47 +0000472** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000473** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000474** processing of the VDBE program is interrupted.
475**
476** This macro added to every instruction that does a jump in order to
477** implement a loop. This test used to be on every single instruction,
478** but that meant we more testing that we needed. By only testing the
479** flag on jump instructions, we get a (small) speed improvement.
480*/
481#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000482 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000483
danielk1977861f7452008-06-05 11:39:11 +0000484#ifdef SQLITE_DEBUG
485static int fileExists(sqlite3 *db, const char *zFile){
danielk1977ad0132d2008-06-07 08:58:22 +0000486 int res = 0;
487 int rc = SQLITE_OK;
488#ifdef SQLITE_TEST
489 /* If we are currently testing IO errors, then do not call OsAccess() to
490 ** test for the presence of zFile. This is because any IO error that
491 ** occurs here will not be reported, causing the test to fail.
492 */
493 extern int sqlite3_io_error_pending;
494 if( sqlite3_io_error_pending<=0 )
495#endif
496 rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
danielk1977861f7452008-06-05 11:39:11 +0000497 return (res && rc==SQLITE_OK);
498}
499#endif
drhcaec2f12003-01-07 02:47:47 +0000500
danielk1977fd7f0452008-12-17 17:30:26 +0000501#ifndef NDEBUG
502/*
503** This function is only called from within an assert() expression. It
504** checks that the sqlite3.nTransaction variable is correctly set to
505** the number of non-transaction savepoints currently in the
506** linked list starting at sqlite3.pSavepoint.
507**
508** Usage:
509**
510** assert( checkSavepointCount(db) );
511*/
512static int checkSavepointCount(sqlite3 *db){
513 int n = 0;
514 Savepoint *p;
515 for(p=db->pSavepoint; p; p=p->pNext) n++;
516 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
517 return 1;
518}
519#endif
520
drhcaec2f12003-01-07 02:47:47 +0000521/*
drhb86ccfb2003-01-28 23:13:10 +0000522** Execute as much of a VDBE program as we can then return.
523**
danielk19774adee202004-05-08 08:23:19 +0000524** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000525** close the program with a final OP_Halt and to set up the callbacks
526** and the error message pointer.
527**
528** Whenever a row or result data is available, this routine will either
529** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000530** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000531**
532** If an attempt is made to open a locked database, then this routine
533** will either invoke the busy callback (if there is one) or it will
534** return SQLITE_BUSY.
535**
536** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000537** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000538** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
539**
540** If the callback ever returns non-zero, then the program exits
541** immediately. There will be no error message but the p->rc field is
542** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
543**
drh9468c7f2003-03-07 19:50:07 +0000544** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
545** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000546**
547** Other fatal errors return SQLITE_ERROR.
548**
danielk19774adee202004-05-08 08:23:19 +0000549** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000550** used to clean up the mess that was left behind.
551*/
danielk19774adee202004-05-08 08:23:19 +0000552int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000553 Vdbe *p /* The VDBE */
554){
555 int pc; /* The program counter */
556 Op *pOp; /* Current operation */
557 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000558 sqlite3 *db = p->db; /* The database */
drh8079a0d2006-01-12 17:20:50 +0000559 u8 encoding = ENC(db); /* The database encoding */
drhb27b7f52008-12-10 18:03:45 +0000560 Mem *pIn1 = 0; /* 1st input operand */
561 Mem *pIn2 = 0; /* 2nd input operand */
562 Mem *pIn3 = 0; /* 3rd input operand */
563 Mem *pOut = 0; /* Output operand */
drhb1fdb2a2008-01-05 04:06:03 +0000564 u8 opProperty;
drh0acb7e42008-06-25 00:12:41 +0000565 int iCompare = 0; /* Result of last OP_Compare operation */
566 int *aPermute = 0; /* Permuation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000567#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000568 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000569 int origPc; /* Program counter at start of opcode */
570#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000571#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
572 int nProgressOps = 0; /* Opcodes executed since progress callback. */
573#endif
drh23f79d02008-08-20 22:06:47 +0000574 UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
drhe63d9992008-08-13 19:11:48 +0000575
drhca48c902008-01-18 14:08:24 +0000576 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhb86ccfb2003-01-28 23:13:10 +0000577 assert( db->magic==SQLITE_MAGIC_BUSY );
drh4cf7c7f2007-08-28 23:28:07 +0000578 sqlite3BtreeMutexArrayEnter(&p->aMutex);
danielk19772e588c72005-12-09 14:25:08 +0000579 if( p->rc==SQLITE_NOMEM ){
580 /* This happens if a malloc() inside a call to sqlite3_column_text() or
581 ** sqlite3_column_text16() failed. */
582 goto no_mem;
583 }
drh3a840692003-01-29 22:58:26 +0000584 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
585 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000586 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000587 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000588 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000589 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000590 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000591#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000592 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000593 if( p->pc==0
594 && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
drh3c23a882007-01-09 14:01:13 +0000595 ){
596 int i;
597 printf("VDBE Program Listing:\n");
598 sqlite3VdbePrintSql(p);
599 for(i=0; i<p->nOp; i++){
600 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
601 }
602 }
danielk1977861f7452008-06-05 11:39:11 +0000603 if( fileExists(db, "vdbe_trace") ){
drh3c23a882007-01-09 14:01:13 +0000604 p->trace = stdout;
605 }
danielk19772d1d86f2008-06-20 14:59:51 +0000606 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000607#endif
drhb86ccfb2003-01-28 23:13:10 +0000608 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000609 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000610 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000611#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000612 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000613 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000614#endif
drh75897232000-05-29 14:26:00 +0000615 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000616
danielk19778b60e0f2005-01-12 09:10:39 +0000617 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000618 */
danielk19778b60e0f2005-01-12 09:10:39 +0000619#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000620 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000621 if( pc==0 ){
622 printf("VDBE Execution Trace:\n");
623 sqlite3VdbePrintSql(p);
624 }
danielk19774adee202004-05-08 08:23:19 +0000625 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000626 }
drh19db9352008-03-27 22:42:51 +0000627 if( p->trace==0 && pc==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000628 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000629 if( fileExists(db, "vdbe_sqltrace") ){
drh19db9352008-03-27 22:42:51 +0000630 sqlite3VdbePrintSql(p);
631 }
danielk19772d1d86f2008-06-20 14:59:51 +0000632 sqlite3EndBenignMalloc();
drh3f7d4e42004-07-24 14:35:58 +0000633 }
634#endif
635
drh6e142f52000-06-08 13:36:40 +0000636
drhf6038712004-02-08 18:07:34 +0000637 /* Check to see if we need to simulate an interrupt. This only happens
638 ** if we have a special test build.
639 */
640#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000641 if( sqlite3_interrupt_count>0 ){
642 sqlite3_interrupt_count--;
643 if( sqlite3_interrupt_count==0 ){
644 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000645 }
646 }
647#endif
648
danielk1977348bb5d2003-10-18 09:37:26 +0000649#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
650 /* Call the progress callback if it is configured and the required number
651 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000652 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000653 ** If the progress callback returns non-zero, exit the virtual machine with
654 ** a return code SQLITE_ABORT.
655 */
drh3914aed2004-01-31 20:40:42 +0000656 if( db->xProgress ){
657 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000658 int prc;
drhf8888bb2006-05-26 19:57:19 +0000659 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000660 prc =db->xProgress(db->pProgressArg);
drhf8888bb2006-05-26 19:57:19 +0000661 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000662 if( prc!=0 ){
663 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000664 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000665 }
danielk19773fe11f32007-06-13 16:49:48 +0000666 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000667 }
drh3914aed2004-01-31 20:40:42 +0000668 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000669 }
danielk1977348bb5d2003-10-18 09:37:26 +0000670#endif
671
drh4c583122008-01-04 22:01:03 +0000672 /* Do common setup processing for any opcode that is marked
673 ** with the "out2-prerelease" tag. Such opcodes have a single
drh9cbf3422008-01-17 16:22:13 +0000674 ** output which is specified by the P2 parameter. The P2 register
drh4c583122008-01-04 22:01:03 +0000675 ** is initialized to a NULL.
676 */
drhb1fdb2a2008-01-05 04:06:03 +0000677 opProperty = opcodeProperty[pOp->opcode];
678 if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000679 assert( pOp->p2>0 );
680 assert( pOp->p2<=p->nMem );
681 pOut = &p->aMem[pOp->p2];
danielk19775f096132008-03-28 15:44:09 +0000682 sqlite3VdbeMemReleaseExternal(pOut);
drh4c583122008-01-04 22:01:03 +0000683 pOut->flags = MEM_Null;
drhb1fdb2a2008-01-05 04:06:03 +0000684 }else
685
686 /* Do common setup for opcodes marked with one of the following
687 ** combinations of properties.
688 **
689 ** in1
690 ** in1 in2
691 ** in1 in2 out3
692 ** in1 in3
drhb1fdb2a2008-01-05 04:06:03 +0000693 **
drh9cbf3422008-01-17 16:22:13 +0000694 ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
695 ** registers for inputs. Variable pOut points to the output register.
drhb1fdb2a2008-01-05 04:06:03 +0000696 */
697 if( (opProperty & OPFLG_IN1)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000698 assert( pOp->p1>0 );
699 assert( pOp->p1<=p->nMem );
700 pIn1 = &p->aMem[pOp->p1];
701 REGISTER_TRACE(pOp->p1, pIn1);
drhb1fdb2a2008-01-05 04:06:03 +0000702 if( (opProperty & OPFLG_IN2)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000703 assert( pOp->p2>0 );
drhaa9b8962008-01-08 02:57:55 +0000704 assert( pOp->p2<=p->nMem );
705 pIn2 = &p->aMem[pOp->p2];
706 REGISTER_TRACE(pOp->p2, pIn2);
drh9cbf3422008-01-17 16:22:13 +0000707 if( (opProperty & OPFLG_OUT3)!=0 ){
708 assert( pOp->p3>0 );
709 assert( pOp->p3<=p->nMem );
710 pOut = &p->aMem[pOp->p3];
711 }
712 }else if( (opProperty & OPFLG_IN3)!=0 ){
713 assert( pOp->p3>0 );
drhaa9b8962008-01-08 02:57:55 +0000714 assert( pOp->p3<=p->nMem );
715 pIn3 = &p->aMem[pOp->p3];
716 REGISTER_TRACE(pOp->p3, pIn3);
717 }
drh9cbf3422008-01-17 16:22:13 +0000718 }else if( (opProperty & OPFLG_IN2)!=0 ){
719 assert( pOp->p2>0 );
720 assert( pOp->p2<=p->nMem );
721 pIn2 = &p->aMem[pOp->p2];
722 REGISTER_TRACE(pOp->p2, pIn2);
723 }else if( (opProperty & OPFLG_IN3)!=0 ){
724 assert( pOp->p3>0 );
725 assert( pOp->p3<=p->nMem );
726 pIn3 = &p->aMem[pOp->p3];
727 REGISTER_TRACE(pOp->p3, pIn3);
drh4c583122008-01-04 22:01:03 +0000728 }
729
drh75897232000-05-29 14:26:00 +0000730 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000731
drh5e00f6c2001-09-13 13:46:56 +0000732/*****************************************************************************
733** What follows is a massive switch statement where each case implements a
734** separate instruction in the virtual machine. If we follow the usual
735** indentation conventions, each case should be indented by 6 spaces. But
736** that is a lot of wasted space on the left margin. So the code within
737** the switch statement will break with convention and be flush-left. Another
738** big comment (similar to this one) will mark the point in the code where
739** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000740**
741** The formatting of each case is important. The makefile for SQLite
742** generates two C files "opcodes.h" and "opcodes.c" by scanning this
743** file looking for lines that begin with "case OP_". The opcodes.h files
744** will be filled with #defines that give unique integer values to each
745** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000746** each string is the symbolic name for the corresponding opcode. If the
747** case statement is followed by a comment of the form "/# same as ... #/"
748** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000749**
drh9cbf3422008-01-17 16:22:13 +0000750** Other keywords in the comment that follows each case are used to
751** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
752** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
753** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000754**
drhac82fcf2002-09-08 17:23:41 +0000755** Documentation about VDBE opcodes is generated by scanning this file
756** for lines of that contain "Opcode:". That line and all subsequent
757** comment lines are used in the generation of the opcode.html documentation
758** file.
759**
760** SUMMARY:
761**
762** Formatting is important to scripts that scan this file.
763** Do not deviate from the formatting style currently in use.
764**
drh5e00f6c2001-09-13 13:46:56 +0000765*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000766
drh9cbf3422008-01-17 16:22:13 +0000767/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000768**
769** An unconditional jump to address P2.
770** The next instruction executed will be
771** the one at index P2 from the beginning of
772** the program.
773*/
drh9cbf3422008-01-17 16:22:13 +0000774case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000775 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000776 pc = pOp->p2 - 1;
777 break;
778}
drh75897232000-05-29 14:26:00 +0000779
drh2eb95372008-06-06 15:04:36 +0000780/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000781**
drh2eb95372008-06-06 15:04:36 +0000782** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000783** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000784*/
drh9cbf3422008-01-17 16:22:13 +0000785case OP_Gosub: { /* jump */
drh2eb95372008-06-06 15:04:36 +0000786 assert( pOp->p1>0 );
787 assert( pOp->p1<=p->nMem );
788 pIn1 = &p->aMem[pOp->p1];
789 assert( (pIn1->flags & MEM_Dyn)==0 );
790 pIn1->flags = MEM_Int;
791 pIn1->u.i = pc;
792 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000793 pc = pOp->p2 - 1;
794 break;
795}
796
drh2eb95372008-06-06 15:04:36 +0000797/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000798**
drh2eb95372008-06-06 15:04:36 +0000799** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000800*/
drh2eb95372008-06-06 15:04:36 +0000801case OP_Return: { /* in1 */
802 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000803 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000804 break;
805}
806
drhe00ee6e2008-06-20 15:24:01 +0000807/* Opcode: Yield P1 * * * *
808**
809** Swap the program counter with the value in register P1.
810*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000811case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000812 int pcDest;
drhe00ee6e2008-06-20 15:24:01 +0000813 assert( (pIn1->flags & MEM_Dyn)==0 );
814 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000815 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000816 pIn1->u.i = pc;
817 REGISTER_TRACE(pOp->p1, pIn1);
818 pc = pcDest;
819 break;
820}
821
822
drh9cbf3422008-01-17 16:22:13 +0000823/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000824**
drh3d4501e2008-12-04 20:40:10 +0000825** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000826** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000827**
drh92f02c32004-09-02 14:57:08 +0000828** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
829** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
830** For errors, it can be some other value. If P1!=0 then P2 will determine
831** whether or not to rollback the current transaction. Do not rollback
832** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
833** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000834** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000835**
drh66a51672008-01-03 00:01:23 +0000836** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000837**
drh9cfcf5d2002-01-29 18:41:24 +0000838** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000839** every program. So a jump past the last instruction of the program
840** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000841*/
drh9cbf3422008-01-17 16:22:13 +0000842case OP_Halt: {
drh92f02c32004-09-02 14:57:08 +0000843 p->rc = pOp->p1;
844 p->pc = pc;
845 p->errorAction = pOp->p2;
danielk19772dca4ac2008-01-03 11:50:29 +0000846 if( pOp->p4.z ){
drhf089aa42008-07-08 19:34:06 +0000847 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drh9cfcf5d2002-01-29 18:41:24 +0000848 }
drh92f02c32004-09-02 14:57:08 +0000849 rc = sqlite3VdbeHalt(p);
danielk197701427a62005-01-11 13:02:33 +0000850 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
drh92f02c32004-09-02 14:57:08 +0000851 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000852 p->rc = rc = SQLITE_BUSY;
853 }else{
854 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000855 }
drh900b31e2007-08-28 02:27:51 +0000856 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000857}
drhc61053b2000-06-04 12:58:36 +0000858
drh4c583122008-01-04 22:01:03 +0000859/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000860**
drh9cbf3422008-01-17 16:22:13 +0000861** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000862*/
drh4c583122008-01-04 22:01:03 +0000863case OP_Integer: { /* out2-prerelease */
864 pOut->flags = MEM_Int;
865 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000866 break;
867}
868
drh4c583122008-01-04 22:01:03 +0000869/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000870**
drh66a51672008-01-03 00:01:23 +0000871** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000872** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000873*/
drh4c583122008-01-04 22:01:03 +0000874case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000875 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000876 pOut->flags = MEM_Int;
877 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000878 break;
879}
drh4f26d6c2004-05-26 23:25:30 +0000880
drh4c583122008-01-04 22:01:03 +0000881/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000882**
drh4c583122008-01-04 22:01:03 +0000883** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000884** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000885*/
drh4c583122008-01-04 22:01:03 +0000886case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
887 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000888 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000889 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000890 break;
891}
danielk1977cbb18d22004-05-28 11:37:27 +0000892
drh3c84ddf2008-01-09 02:15:38 +0000893/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000894**
drh66a51672008-01-03 00:01:23 +0000895** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000896** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000897*/
drh4c583122008-01-04 22:01:03 +0000898case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000899 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000900 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000901 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000902
903#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000904 if( encoding!=SQLITE_UTF8 ){
drh4c583122008-01-04 22:01:03 +0000905 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
906 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drhdab898f2008-07-30 13:14:55 +0000907 if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
danielk19775f096132008-03-28 15:44:09 +0000908 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000909 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000910 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000911 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000912 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000913 }
drh66a51672008-01-03 00:01:23 +0000914 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000915 pOp->p4.z = pOut->z;
916 pOp->p1 = pOut->n;
drhbb4957f2008-03-20 14:03:29 +0000917 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000918 goto too_big;
919 }
drhb7654112008-01-12 12:48:07 +0000920 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977bfd6cce2004-06-18 04:24:54 +0000921 break;
danielk19770f69c1e2004-05-29 11:24:50 +0000922 }
danielk197793758c82005-01-21 08:13:14 +0000923#endif
drhbb4957f2008-03-20 14:03:29 +0000924 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000925 goto too_big;
926 }
927 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000928}
drhf4479502004-05-27 03:12:53 +0000929
drh4c583122008-01-04 22:01:03 +0000930/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000931**
drh9cbf3422008-01-17 16:22:13 +0000932** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000933*/
drh4c583122008-01-04 22:01:03 +0000934case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000935 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000936 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
937 pOut->z = pOp->p4.z;
938 pOut->n = pOp->p1;
939 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000940 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000941 break;
942}
943
drh4c583122008-01-04 22:01:03 +0000944/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000945**
drh9cbf3422008-01-17 16:22:13 +0000946** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000947*/
drh4c583122008-01-04 22:01:03 +0000948case OP_Null: { /* out2-prerelease */
drhf0863fe2005-06-12 21:35:51 +0000949 break;
950}
951
952
drh9de221d2008-01-05 06:51:30 +0000953/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000954**
drh9de221d2008-01-05 06:51:30 +0000955** P4 points to a blob of data P1 bytes long. Store this
956** blob in register P2. This instruction is not coded directly
danielk1977cbb18d22004-05-28 11:37:27 +0000957** by the compiler. Instead, the compiler layer specifies
958** an OP_HexBlob opcode, with the hex string representation of
drh66a51672008-01-03 00:01:23 +0000959** the blob as P4. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000960** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000961*/
drh4c583122008-01-04 22:01:03 +0000962case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000963 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000964 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000965 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000966 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000967 break;
968}
969
drh3c84ddf2008-01-09 02:15:38 +0000970/* Opcode: Variable P1 P2 * * *
drh50457892003-09-06 01:10:47 +0000971**
drh9cbf3422008-01-17 16:22:13 +0000972** The value of variable P1 is written into register P2. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000973** an unknown in the original SQL string as handed to sqlite3_compile().
shane21e7feb2008-05-30 15:59:49 +0000974** Any occurrence of the '?' character in the original SQL is considered
drh7c972de2003-09-06 22:18:07 +0000975** a variable. Variables in the SQL string are number from left to
976** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000977** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000978*/
drh4c583122008-01-04 22:01:03 +0000979case OP_Variable: { /* out2-prerelease */
drh7c972de2003-09-06 22:18:07 +0000980 int j = pOp->p1 - 1;
drh023ae032007-05-08 12:12:16 +0000981 Mem *pVar;
danielk1977295ba552004-05-19 10:34:51 +0000982 assert( j>=0 && j<p->nVar );
983
drh023ae032007-05-08 12:12:16 +0000984 pVar = &p->aVar[j];
985 if( sqlite3VdbeMemTooBig(pVar) ){
986 goto too_big;
987 }
drh4c583122008-01-04 22:01:03 +0000988 sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static);
drhb7654112008-01-12 12:48:07 +0000989 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +0000990 break;
991}
danielk1977295ba552004-05-19 10:34:51 +0000992
drhb21e7c72008-06-22 12:37:57 +0000993/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +0000994**
drhb21e7c72008-06-22 12:37:57 +0000995** Move the values in register P1..P1+P3-1 over into
996** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
997** left holding a NULL. It is an error for register ranges
998** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +0000999*/
drhe1349cb2008-04-01 00:36:10 +00001000case OP_Move: {
1001 char *zMalloc;
drhb21e7c72008-06-22 12:37:57 +00001002 int n = pOp->p3;
1003 int p1 = pOp->p1;
1004 int p2 = pOp->p2;
1005 assert( n>0 );
1006 assert( p1>0 );
1007 assert( p1+n<p->nMem );
1008 pIn1 = &p->aMem[p1];
1009 assert( p2>0 );
1010 assert( p2+n<p->nMem );
1011 pOut = &p->aMem[p2];
1012 assert( p1+n<=p2 || p2+n<=p1 );
1013 while( n-- ){
drhb21e7c72008-06-22 12:37:57 +00001014 zMalloc = pOut->zMalloc;
1015 pOut->zMalloc = 0;
1016 sqlite3VdbeMemMove(pOut, pIn1);
1017 pIn1->zMalloc = zMalloc;
1018 REGISTER_TRACE(p2++, pOut);
1019 pIn1++;
1020 pOut++;
1021 }
drhe1349cb2008-04-01 00:36:10 +00001022 break;
1023}
1024
drhb1fdb2a2008-01-05 04:06:03 +00001025/* Opcode: Copy P1 P2 * * *
1026**
drh9cbf3422008-01-17 16:22:13 +00001027** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001028**
1029** This instruction makes a deep copy of the value. A duplicate
1030** is made of any string or blob constant. See also OP_SCopy.
1031*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001032case OP_Copy: { /* in1 */
drhe1349cb2008-04-01 00:36:10 +00001033 assert( pOp->p2>0 );
1034 assert( pOp->p2<=p->nMem );
1035 pOut = &p->aMem[pOp->p2];
1036 assert( pOut!=pIn1 );
1037 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1038 Deephemeralize(pOut);
1039 REGISTER_TRACE(pOp->p2, pOut);
1040 break;
1041}
1042
drhb1fdb2a2008-01-05 04:06:03 +00001043/* Opcode: SCopy P1 P2 * * *
1044**
drh9cbf3422008-01-17 16:22:13 +00001045** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001046**
1047** This instruction makes a shallow copy of the value. If the value
1048** is a string or blob, then the copy is only a pointer to the
1049** original and hence if the original changes so will the copy.
1050** Worse, if the original is deallocated, the copy becomes invalid.
1051** Thus the program must guarantee that the original will not change
1052** during the lifetime of the copy. Use OP_Copy to make a complete
1053** copy.
1054*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001055case OP_SCopy: { /* in1 */
drh9cbf3422008-01-17 16:22:13 +00001056 REGISTER_TRACE(pOp->p1, pIn1);
1057 assert( pOp->p2>0 );
1058 assert( pOp->p2<=p->nMem );
1059 pOut = &p->aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001060 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001061 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh5b6afba2008-01-05 16:29:28 +00001062 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001063 break;
1064}
drh75897232000-05-29 14:26:00 +00001065
drh9cbf3422008-01-17 16:22:13 +00001066/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001067**
shane21e7feb2008-05-30 15:59:49 +00001068** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001069** results. This opcode causes the sqlite3_step() call to terminate
1070** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1071** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001072** row.
drhd4e70eb2008-01-02 00:34:36 +00001073*/
drh9cbf3422008-01-17 16:22:13 +00001074case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001075 Mem *pMem;
1076 int i;
1077 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001078 assert( pOp->p1>0 );
1079 assert( pOp->p1+pOp->p2<=p->nMem );
drhd4e70eb2008-01-02 00:34:36 +00001080
drhd4e70eb2008-01-02 00:34:36 +00001081 /* Invalidate all ephemeral cursor row caches */
1082 p->cacheCtr = (p->cacheCtr + 2)|1;
1083
1084 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001085 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001086 ** as side effect.
1087 */
1088 pMem = p->pResultSet = &p->aMem[pOp->p1];
1089 for(i=0; i<pOp->p2; i++){
1090 sqlite3VdbeMemNulTerminate(&pMem[i]);
1091 storeTypeInfo(&pMem[i], encoding);
drh0acb7e42008-06-25 00:12:41 +00001092 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001093 }
drh28039692008-03-17 16:54:01 +00001094 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001095
1096 /* Return SQLITE_ROW
1097 */
1098 p->nCallback++;
drhd4e70eb2008-01-02 00:34:36 +00001099 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001100 rc = SQLITE_ROW;
1101 goto vdbe_return;
1102}
1103
drh5b6afba2008-01-05 16:29:28 +00001104/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001105**
drh5b6afba2008-01-05 16:29:28 +00001106** Add the text in register P1 onto the end of the text in
1107** register P2 and store the result in register P3.
1108** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001109**
1110** P3 = P2 || P1
1111**
1112** It is illegal for P1 and P3 to be the same register. Sometimes,
1113** if P3 is the same register as P2, the implementation is able
1114** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001115*/
drh5b6afba2008-01-05 16:29:28 +00001116case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001117 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001118
danielk1977a7a8e142008-02-13 18:25:27 +00001119 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001120 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001121 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001122 break;
drh5e00f6c2001-09-13 13:46:56 +00001123 }
drh5b6afba2008-01-05 16:29:28 +00001124 ExpandBlob(pIn1);
1125 Stringify(pIn1, encoding);
1126 ExpandBlob(pIn2);
1127 Stringify(pIn2, encoding);
1128 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001129 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001130 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001131 }
danielk1977a7a8e142008-02-13 18:25:27 +00001132 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001133 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001134 goto no_mem;
1135 }
danielk1977a7a8e142008-02-13 18:25:27 +00001136 if( pOut!=pIn2 ){
1137 memcpy(pOut->z, pIn2->z, pIn2->n);
1138 }
1139 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1140 pOut->z[nByte] = 0;
1141 pOut->z[nByte+1] = 0;
1142 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001143 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001144 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001145 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001146 break;
1147}
drh75897232000-05-29 14:26:00 +00001148
drh3c84ddf2008-01-09 02:15:38 +00001149/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001150**
drh60a713c2008-01-21 16:22:45 +00001151** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001152** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001153** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001154*/
drh3c84ddf2008-01-09 02:15:38 +00001155/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001156**
drh3c84ddf2008-01-09 02:15:38 +00001157**
shane21e7feb2008-05-30 15:59:49 +00001158** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001159** and store the result in register P3.
1160** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001161*/
drh3c84ddf2008-01-09 02:15:38 +00001162/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001163**
drh60a713c2008-01-21 16:22:45 +00001164** Subtract the value in register P1 from the value in register P2
1165** and store the result in register P3.
1166** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001167*/
drh9cbf3422008-01-17 16:22:13 +00001168/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001169**
drh60a713c2008-01-21 16:22:45 +00001170** Divide the value in register P1 by the value in register P2
1171** and store the result in register P3. If the value in register P2
1172** is zero, then the result is NULL.
1173** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001174*/
drh9cbf3422008-01-17 16:22:13 +00001175/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001176**
drh3c84ddf2008-01-09 02:15:38 +00001177** Compute the remainder after integer division of the value in
1178** register P1 by the value in register P2 and store the result in P3.
1179** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001180** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001181*/
drh5b6afba2008-01-05 16:29:28 +00001182case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1183case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1184case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1185case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1186case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh8a512562005-11-14 22:29:05 +00001187 int flags;
drh61669b32008-07-30 13:27:10 +00001188 applyNumericAffinity(pIn1);
1189 applyNumericAffinity(pIn2);
drh5b6afba2008-01-05 16:29:28 +00001190 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001191 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1192 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001193 i64 a, b;
drh5b6afba2008-01-05 16:29:28 +00001194 a = pIn1->u.i;
1195 b = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001196 switch( pOp->opcode ){
1197 case OP_Add: b += a; break;
1198 case OP_Subtract: b -= a; break;
1199 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001200 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001201 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001202 /* Dividing the largest possible negative 64-bit integer (1<<63) by
drh0f050352008-05-09 18:03:13 +00001203 ** -1 returns an integer too large to store in a 64-bit data-type. On
danielk197742d4ef22007-06-26 11:13:25 +00001204 ** some architectures, the value overflows to (1<<63). On others,
1205 ** a SIGFPE is issued. The following statement normalizes this
shane21e7feb2008-05-30 15:59:49 +00001206 ** behavior so that all architectures behave as if integer
1207 ** overflow occurred.
danielk197742d4ef22007-06-26 11:13:25 +00001208 */
drh0f050352008-05-09 18:03:13 +00001209 if( a==-1 && b==SMALLEST_INT64 ) a = 1;
drh5e00f6c2001-09-13 13:46:56 +00001210 b /= a;
drh75897232000-05-29 14:26:00 +00001211 break;
1212 }
drhbf4133c2001-10-13 02:59:08 +00001213 default: {
drha05a7222008-01-19 03:35:58 +00001214 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001215 if( a==-1 ) a = 1;
drhbf4133c2001-10-13 02:59:08 +00001216 b %= a;
1217 break;
1218 }
drh75897232000-05-29 14:26:00 +00001219 }
drh5b6afba2008-01-05 16:29:28 +00001220 pOut->u.i = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001221 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001222 }else{
1223 double a, b;
drh5b6afba2008-01-05 16:29:28 +00001224 a = sqlite3VdbeRealValue(pIn1);
1225 b = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001226 switch( pOp->opcode ){
1227 case OP_Add: b += a; break;
1228 case OP_Subtract: b -= a; break;
1229 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001230 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001231 if( a==0.0 ) goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001232 b /= a;
1233 break;
1234 }
drhbf4133c2001-10-13 02:59:08 +00001235 default: {
danielk19774b5710e2007-05-08 13:57:34 +00001236 i64 ia = (i64)a;
1237 i64 ib = (i64)b;
drha05a7222008-01-19 03:35:58 +00001238 if( ia==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001239 if( ia==-1 ) ia = 1;
drh9c1905f2008-12-10 22:32:56 +00001240 b = (double)(ib % ia);
drhbf4133c2001-10-13 02:59:08 +00001241 break;
1242 }
drh5e00f6c2001-09-13 13:46:56 +00001243 }
drh0de3ae92008-04-28 16:55:26 +00001244 if( sqlite3IsNaN(b) ){
drha05a7222008-01-19 03:35:58 +00001245 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001246 }
drh5b6afba2008-01-05 16:29:28 +00001247 pOut->r = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001248 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001249 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001250 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001251 }
drh5e00f6c2001-09-13 13:46:56 +00001252 }
1253 break;
1254
drha05a7222008-01-19 03:35:58 +00001255arithmetic_result_is_null:
1256 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001257 break;
1258}
1259
drh66a51672008-01-03 00:01:23 +00001260/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001261**
drh66a51672008-01-03 00:01:23 +00001262** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001263** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1264** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001265** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001266**
1267** The interface used by the implementation of the aforementioned functions
1268** to retrieve the collation sequence set by this opcode is not available
1269** publicly, only to user functions defined in func.c.
1270*/
drh9cbf3422008-01-17 16:22:13 +00001271case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001272 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001273 break;
1274}
1275
drh98757152008-01-09 23:04:12 +00001276/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001277**
drh66a51672008-01-03 00:01:23 +00001278** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001279** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001280** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001281** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001282**
drh13449892005-09-07 21:22:45 +00001283** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001284** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001285** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001286** whether meta data associated with a user function argument using the
1287** sqlite3_set_auxdata() API may be safely retained until the next
1288** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001289**
drh13449892005-09-07 21:22:45 +00001290** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001291*/
drh0bce8352002-02-28 00:41:10 +00001292case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001293 int i;
drh6810ce62004-01-31 19:22:56 +00001294 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001295 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001296 sqlite3_value **apVal;
drh98757152008-01-09 23:04:12 +00001297 int n = pOp->p5;
drh1350b032002-02-27 19:00:20 +00001298
danielk19776ddcca52004-05-24 23:48:25 +00001299 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001300 assert( apVal || n==0 );
1301
drh9cbf3422008-01-17 16:22:13 +00001302 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) );
danielk1977a7a8e142008-02-13 18:25:27 +00001303 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh9cbf3422008-01-17 16:22:13 +00001304 pArg = &p->aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001305 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001306 apVal[i] = pArg;
drh8079a0d2006-01-12 17:20:50 +00001307 storeTypeInfo(pArg, encoding);
drh2dcef112008-01-12 19:03:48 +00001308 REGISTER_TRACE(pOp->p2, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001309 }
danielk197751ad0ec2004-05-24 12:39:02 +00001310
drh66a51672008-01-03 00:01:23 +00001311 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1312 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001313 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001314 ctx.pVdbeFunc = 0;
1315 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001316 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001317 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1318 }
1319
danielk1977a7a8e142008-02-13 18:25:27 +00001320 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1321 pOut = &p->aMem[pOp->p3];
drh00706be2004-01-30 14:49:16 +00001322 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001323 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001324 ctx.s.xDel = 0;
1325 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001326
1327 /* The output cell may already have a buffer allocated. Move
1328 ** the pointer to ctx.s so in case the user-function can use
1329 ** the already allocated buffer instead of allocating a new one.
1330 */
1331 sqlite3VdbeMemMove(&ctx.s, pOut);
1332 MemSetTypeFlag(&ctx.s, MEM_Null);
1333
drh8e0a2f92002-02-23 23:45:45 +00001334 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001335 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001336 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00001337 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001338 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001339 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001340 }
danielk19774adee202004-05-08 08:23:19 +00001341 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001342 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk197775eb0162008-03-28 19:16:33 +00001343 if( sqlite3SafetyOn(db) ){
1344 sqlite3VdbeMemRelease(&ctx.s);
1345 goto abort_due_to_misuse;
1346 }
drh17435752007-08-16 04:30:38 +00001347 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001348 /* Even though a malloc() has failed, the implementation of the
1349 ** user function may have called an sqlite3_result_XXX() function
1350 ** to return a value. The following call releases any resources
1351 ** associated with such a value.
1352 **
1353 ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1354 ** fails also (the if(...) statement above). But if people are
1355 ** misusing sqlite, they have bigger problems than a leaked value.
1356 */
1357 sqlite3VdbeMemRelease(&ctx.s);
1358 goto no_mem;
1359 }
danielk19777e18c252004-05-25 11:47:24 +00001360
shane21e7feb2008-05-30 15:59:49 +00001361 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001362 ** immediately call the destructor for any non-static values.
1363 */
1364 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001365 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001366 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001367 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001368 }
1369
drh90669c12006-01-20 15:45:36 +00001370 /* If the function returned an error, throw an exception */
1371 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001372 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001373 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001374 }
1375
drh9cbf3422008-01-17 16:22:13 +00001376 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001377 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001378 sqlite3VdbeMemMove(pOut, &ctx.s);
1379 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001380 goto too_big;
1381 }
drh2dcef112008-01-12 19:03:48 +00001382 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001383 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001384 break;
1385}
1386
drh98757152008-01-09 23:04:12 +00001387/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001388**
drh98757152008-01-09 23:04:12 +00001389** Take the bit-wise AND of the values in register P1 and P2 and
1390** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001391** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001392*/
drh98757152008-01-09 23:04:12 +00001393/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001394**
drh98757152008-01-09 23:04:12 +00001395** Take the bit-wise OR of the values in register P1 and P2 and
1396** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001397** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001398*/
drh98757152008-01-09 23:04:12 +00001399/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001400**
drh98757152008-01-09 23:04:12 +00001401** Shift the integer value in register P2 to the left by the
drh60a713c2008-01-21 16:22:45 +00001402** number of bits specified by the integer in regiser P1.
drh98757152008-01-09 23:04:12 +00001403** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001404** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001405*/
drh98757152008-01-09 23:04:12 +00001406/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001407**
drh98757152008-01-09 23:04:12 +00001408** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001409** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001410** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001411** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001412*/
drh5b6afba2008-01-05 16:29:28 +00001413case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1414case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1415case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1416case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drhb1276122005-10-29 15:48:30 +00001417 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001418
drh5b6afba2008-01-05 16:29:28 +00001419 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001420 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001421 break;
1422 }
drh5b6afba2008-01-05 16:29:28 +00001423 a = sqlite3VdbeIntValue(pIn2);
1424 b = sqlite3VdbeIntValue(pIn1);
drhbf4133c2001-10-13 02:59:08 +00001425 switch( pOp->opcode ){
1426 case OP_BitAnd: a &= b; break;
1427 case OP_BitOr: a |= b; break;
1428 case OP_ShiftLeft: a <<= b; break;
drha05a7222008-01-19 03:35:58 +00001429 default: assert( pOp->opcode==OP_ShiftRight );
1430 a >>= b; break;
drhbf4133c2001-10-13 02:59:08 +00001431 }
drh5b6afba2008-01-05 16:29:28 +00001432 pOut->u.i = a;
danielk1977a7a8e142008-02-13 18:25:27 +00001433 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001434 break;
1435}
1436
drh8558cde2008-01-05 05:20:10 +00001437/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001438**
danielk19770cdc0222008-06-26 18:04:03 +00001439** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001440** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001441**
drh8558cde2008-01-05 05:20:10 +00001442** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001443*/
drh9cbf3422008-01-17 16:22:13 +00001444case OP_AddImm: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001445 sqlite3VdbeMemIntegerify(pIn1);
1446 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001447 break;
1448}
1449
drh9cbf3422008-01-17 16:22:13 +00001450/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001451**
drh9cbf3422008-01-17 16:22:13 +00001452** Force the value in register P1 to be an integer. If the value
1453** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001454** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001455** raise an SQLITE_MISMATCH exception.
1456*/
drh9cbf3422008-01-17 16:22:13 +00001457case OP_MustBeInt: { /* jump, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001458 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1459 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001460 if( pOp->p2==0 ){
1461 rc = SQLITE_MISMATCH;
1462 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001463 }else{
drh17c40292004-07-21 02:53:29 +00001464 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001465 }
drh8aff1012001-12-22 14:49:24 +00001466 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001467 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001468 }
1469 break;
1470}
1471
drh8558cde2008-01-05 05:20:10 +00001472/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001473**
drh2133d822008-01-03 18:44:59 +00001474** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001475**
drh8a512562005-11-14 22:29:05 +00001476** This opcode is used when extracting information from a column that
1477** has REAL affinity. Such column values may still be stored as
1478** integers, for space efficiency, but after extraction we want them
1479** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001480*/
drh9cbf3422008-01-17 16:22:13 +00001481case OP_RealAffinity: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001482 if( pIn1->flags & MEM_Int ){
1483 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001484 }
drh487e2622005-06-25 18:42:14 +00001485 break;
1486}
1487
drh8df447f2005-11-01 15:48:24 +00001488#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001489/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001490**
drh8558cde2008-01-05 05:20:10 +00001491** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001492** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001493** equivalent of printf(). Blob values are unchanged and
1494** are afterwards simply interpreted as text.
1495**
1496** A NULL value is not changed by this routine. It remains NULL.
1497*/
drh9cbf3422008-01-17 16:22:13 +00001498case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh8558cde2008-01-05 05:20:10 +00001499 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001500 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001501 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1502 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1503 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001504 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001505 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001506 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001507 break;
1508}
1509
drh8558cde2008-01-05 05:20:10 +00001510/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001511**
drh8558cde2008-01-05 05:20:10 +00001512** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001513** If the value is numeric, convert it to a string first.
1514** Strings are simply reinterpreted as blobs with no change
1515** to the underlying data.
1516**
1517** A NULL value is not changed by this routine. It remains NULL.
1518*/
drh9cbf3422008-01-17 16:22:13 +00001519case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh8558cde2008-01-05 05:20:10 +00001520 if( pIn1->flags & MEM_Null ) break;
1521 if( (pIn1->flags & MEM_Blob)==0 ){
1522 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001523 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001524 MemSetTypeFlag(pIn1, MEM_Blob);
1525 }else{
1526 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001527 }
drhb7654112008-01-12 12:48:07 +00001528 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001529 break;
1530}
drh8a512562005-11-14 22:29:05 +00001531
drh8558cde2008-01-05 05:20:10 +00001532/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001533**
drh8558cde2008-01-05 05:20:10 +00001534** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001535** integer or a floating-point number.)
1536** If the value is text or blob, try to convert it to an using the
1537** equivalent of atoi() or atof() and store 0 if no such conversion
1538** is possible.
1539**
1540** A NULL value is not changed by this routine. It remains NULL.
1541*/
drh9cbf3422008-01-17 16:22:13 +00001542case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh8558cde2008-01-05 05:20:10 +00001543 if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1544 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001545 }
1546 break;
1547}
1548#endif /* SQLITE_OMIT_CAST */
1549
drh8558cde2008-01-05 05:20:10 +00001550/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001551**
drh8558cde2008-01-05 05:20:10 +00001552** Force the value in register P1 be an integer. If
drh8a512562005-11-14 22:29:05 +00001553** The value is currently a real number, drop its fractional part.
1554** If the value is text or blob, try to convert it to an integer using the
1555** equivalent of atoi() and store 0 if no such conversion is possible.
1556**
1557** A NULL value is not changed by this routine. It remains NULL.
1558*/
drh9cbf3422008-01-17 16:22:13 +00001559case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh8558cde2008-01-05 05:20:10 +00001560 if( (pIn1->flags & MEM_Null)==0 ){
1561 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001562 }
1563 break;
1564}
1565
1566#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001567/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001568**
drh8558cde2008-01-05 05:20:10 +00001569** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001570** If The value is currently an integer, convert it.
1571** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001572** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001573**
1574** A NULL value is not changed by this routine. It remains NULL.
1575*/
drh9cbf3422008-01-17 16:22:13 +00001576case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh8558cde2008-01-05 05:20:10 +00001577 if( (pIn1->flags & MEM_Null)==0 ){
1578 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001579 }
1580 break;
1581}
drh487e2622005-06-25 18:42:14 +00001582#endif /* SQLITE_OMIT_CAST */
1583
drh35573352008-01-08 23:54:25 +00001584/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001585**
drh35573352008-01-08 23:54:25 +00001586** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1587** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001588**
drh35573352008-01-08 23:54:25 +00001589** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1590** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1591** bit is clear then fall thru if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001592**
drh35573352008-01-08 23:54:25 +00001593** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001594** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001595** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001596** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001597** affinity is used. Note that the affinity conversions are stored
1598** back into the input registers P1 and P3. So this opcode can cause
1599** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001600**
1601** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001602** the values are compared. If both values are blobs then memcmp() is
1603** used to determine the results of the comparison. If both values
1604** are text, then the appropriate collating function specified in
1605** P4 is used to do the comparison. If P4 is not specified then
1606** memcmp() is used to compare text string. If both values are
1607** numeric, then a numeric comparison is used. If the two values
1608** are of different types, then numbers are considered less than
1609** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001610**
drh35573352008-01-08 23:54:25 +00001611** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1612** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001613*/
drh9cbf3422008-01-17 16:22:13 +00001614/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001615**
drh35573352008-01-08 23:54:25 +00001616** This works just like the Lt opcode except that the jump is taken if
1617** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001618** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001619*/
drh9cbf3422008-01-17 16:22:13 +00001620/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001621**
drh35573352008-01-08 23:54:25 +00001622** This works just like the Lt opcode except that the jump is taken if
1623** the operands in registers P1 and P3 are equal.
1624** See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001625*/
drh9cbf3422008-01-17 16:22:13 +00001626/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001627**
drh35573352008-01-08 23:54:25 +00001628** This works just like the Lt opcode except that the jump is taken if
1629** the content of register P3 is less than or equal to the content of
1630** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001631*/
drh9cbf3422008-01-17 16:22:13 +00001632/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001633**
drh35573352008-01-08 23:54:25 +00001634** This works just like the Lt opcode except that the jump is taken if
1635** the content of register P3 is greater than the content of
1636** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001637*/
drh9cbf3422008-01-17 16:22:13 +00001638/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001639**
drh35573352008-01-08 23:54:25 +00001640** This works just like the Lt opcode except that the jump is taken if
1641** the content of register P3 is greater than or equal to the content of
1642** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001643*/
drh9cbf3422008-01-17 16:22:13 +00001644case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1645case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1646case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1647case OP_Le: /* same as TK_LE, jump, in1, in3 */
1648case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1649case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
danielk1977a37cdde2004-05-16 11:15:36 +00001650 int flags;
1651 int res;
1652 char affinity;
1653
drh35573352008-01-08 23:54:25 +00001654 flags = pIn1->flags|pIn3->flags;
danielk1977a37cdde2004-05-16 11:15:36 +00001655
danielk1977a37cdde2004-05-16 11:15:36 +00001656 if( flags&MEM_Null ){
drh93a960a2008-07-10 00:32:42 +00001657 /* If either operand is NULL then the result is always NULL.
1658 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1659 */
1660 if( pOp->p5 & SQLITE_STOREP2 ){
1661 pOut = &p->aMem[pOp->p2];
1662 MemSetTypeFlag(pOut, MEM_Null);
1663 REGISTER_TRACE(pOp->p2, pOut);
1664 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1665 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001666 }
drh93a960a2008-07-10 00:32:42 +00001667 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001668 }
1669
drh35573352008-01-08 23:54:25 +00001670 affinity = pOp->p5 & SQLITE_AFF_MASK;
drhe51c44f2004-05-30 20:46:09 +00001671 if( affinity ){
drh35573352008-01-08 23:54:25 +00001672 applyAffinity(pIn1, affinity, encoding);
1673 applyAffinity(pIn3, affinity, encoding);
drhbbce3382008-12-06 16:46:13 +00001674 if( db->mallocFailed ) goto no_mem;
drhe51c44f2004-05-30 20:46:09 +00001675 }
danielk1977a37cdde2004-05-16 11:15:36 +00001676
danielk19772dca4ac2008-01-03 11:50:29 +00001677 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh35573352008-01-08 23:54:25 +00001678 ExpandBlob(pIn1);
1679 ExpandBlob(pIn3);
1680 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
danielk1977a37cdde2004-05-16 11:15:36 +00001681 switch( pOp->opcode ){
1682 case OP_Eq: res = res==0; break;
1683 case OP_Ne: res = res!=0; break;
1684 case OP_Lt: res = res<0; break;
1685 case OP_Le: res = res<=0; break;
1686 case OP_Gt: res = res>0; break;
1687 default: res = res>=0; break;
1688 }
1689
drh35573352008-01-08 23:54:25 +00001690 if( pOp->p5 & SQLITE_STOREP2 ){
1691 pOut = &p->aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00001692 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001693 pOut->u.i = res;
1694 REGISTER_TRACE(pOp->p2, pOut);
1695 }else if( res ){
1696 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001697 }
1698 break;
1699}
drhc9b84a12002-06-20 11:36:48 +00001700
drh0acb7e42008-06-25 00:12:41 +00001701/* Opcode: Permutation * * * P4 *
1702**
1703** Set the permuation used by the OP_Compare operator to be the array
1704** of integers in P4.
1705**
1706** The permutation is only valid until the next OP_Permutation, OP_Compare,
1707** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1708** immediately prior to the OP_Compare.
1709*/
1710case OP_Permutation: {
1711 assert( pOp->p4type==P4_INTARRAY );
1712 assert( pOp->p4.ai );
1713 aPermute = pOp->p4.ai;
1714 break;
1715}
1716
drh16ee60f2008-06-20 18:13:25 +00001717/* Opcode: Compare P1 P2 P3 P4 *
1718**
1719** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1720** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1721** the comparison for use by the next OP_Jump instruct.
1722**
drh0acb7e42008-06-25 00:12:41 +00001723** P4 is a KeyInfo structure that defines collating sequences and sort
1724** orders for the comparison. The permutation applies to registers
1725** only. The KeyInfo elements are used sequentially.
1726**
1727** The comparison is a sort comparison, so NULLs compare equal,
1728** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001729** and strings are less than blobs.
1730*/
1731case OP_Compare: {
1732 int n = pOp->p3;
1733 int i, p1, p2;
1734 const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1735 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001736 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001737 p1 = pOp->p1;
1738 assert( p1>0 && p1+n-1<p->nMem );
1739 p2 = pOp->p2;
1740 assert( p2>0 && p2+n-1<p->nMem );
drh0acb7e42008-06-25 00:12:41 +00001741 for(i=0; i<n; i++){
1742 int idx = aPermute ? aPermute[i] : i;
1743 CollSeq *pColl; /* Collating sequence to use on this term */
1744 int bRev; /* True for DESCENDING sort order */
drh0acb7e42008-06-25 00:12:41 +00001745 REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
1746 REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001747 assert( i<pKeyInfo->nField );
1748 pColl = pKeyInfo->aColl[i];
1749 bRev = pKeyInfo->aSortOrder[i];
drh0acb7e42008-06-25 00:12:41 +00001750 iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
1751 if( iCompare ){
1752 if( bRev ) iCompare = -iCompare;
1753 break;
1754 }
drh16ee60f2008-06-20 18:13:25 +00001755 }
drh0acb7e42008-06-25 00:12:41 +00001756 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001757 break;
1758}
1759
1760/* Opcode: Jump P1 P2 P3 * *
1761**
1762** Jump to the instruction at address P1, P2, or P3 depending on whether
1763** in the most recent OP_Compare instruction the P1 vector was less than
1764** equal to, or greater than the P2 vector, respectively.
1765*/
drh0acb7e42008-06-25 00:12:41 +00001766case OP_Jump: { /* jump */
1767 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001768 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001769 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001770 pc = pOp->p2 - 1;
1771 }else{
1772 pc = pOp->p3 - 1;
1773 }
1774 break;
1775}
1776
drh5b6afba2008-01-05 16:29:28 +00001777/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001778**
drh5b6afba2008-01-05 16:29:28 +00001779** Take the logical AND of the values in registers P1 and P2 and
1780** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001781**
drh5b6afba2008-01-05 16:29:28 +00001782** If either P1 or P2 is 0 (false) then the result is 0 even if
1783** the other input is NULL. A NULL and true or two NULLs give
1784** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001785*/
drh5b6afba2008-01-05 16:29:28 +00001786/* Opcode: Or P1 P2 P3 * *
1787**
1788** Take the logical OR of the values in register P1 and P2 and
1789** store the answer in register P3.
1790**
1791** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1792** even if the other input is NULL. A NULL and false or two NULLs
1793** give a NULL output.
1794*/
1795case OP_And: /* same as TK_AND, in1, in2, out3 */
1796case OP_Or: { /* same as TK_OR, in1, in2, out3 */
1797 int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001798
drh5b6afba2008-01-05 16:29:28 +00001799 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001800 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001801 }else{
drh5b6afba2008-01-05 16:29:28 +00001802 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001803 }
drh5b6afba2008-01-05 16:29:28 +00001804 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001805 v2 = 2;
1806 }else{
drh5b6afba2008-01-05 16:29:28 +00001807 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001808 }
1809 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001810 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001811 v1 = and_logic[v1*3+v2];
1812 }else{
drh5b6afba2008-01-05 16:29:28 +00001813 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001814 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001815 }
drhbb113512002-05-27 01:04:51 +00001816 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001817 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001818 }else{
drh5b6afba2008-01-05 16:29:28 +00001819 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001820 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001821 }
drh5e00f6c2001-09-13 13:46:56 +00001822 break;
1823}
1824
drhe99fa2a2008-12-15 15:27:51 +00001825/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001826**
drhe99fa2a2008-12-15 15:27:51 +00001827** Interpret the value in register P1 as a boolean value. Store the
1828** boolean complement in register P2. If the value in register P1 is
1829** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001830*/
drh9cbf3422008-01-17 16:22:13 +00001831case OP_Not: { /* same as TK_NOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001832 pOut = &p->aMem[pOp->p2];
1833 if( pIn1->flags & MEM_Null ){
1834 sqlite3VdbeMemSetNull(pOut);
1835 }else{
1836 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1837 }
drh5e00f6c2001-09-13 13:46:56 +00001838 break;
1839}
1840
drhe99fa2a2008-12-15 15:27:51 +00001841/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00001842**
drhe99fa2a2008-12-15 15:27:51 +00001843** Interpret the content of register P1 as an integer. Store the
1844** ones-complement of the P1 value into register P2. If P1 holds
1845** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00001846*/
drh9cbf3422008-01-17 16:22:13 +00001847case OP_BitNot: { /* same as TK_BITNOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001848 pOut = &p->aMem[pOp->p2];
1849 if( pIn1->flags & MEM_Null ){
1850 sqlite3VdbeMemSetNull(pOut);
1851 }else{
1852 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1853 }
drhbf4133c2001-10-13 02:59:08 +00001854 break;
1855}
1856
drh3c84ddf2008-01-09 02:15:38 +00001857/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001858**
drh3c84ddf2008-01-09 02:15:38 +00001859** Jump to P2 if the value in register P1 is true. The value is
1860** is considered true if it is numeric and non-zero. If the value
1861** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00001862*/
drh3c84ddf2008-01-09 02:15:38 +00001863/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00001864**
drh3c84ddf2008-01-09 02:15:38 +00001865** Jump to P2 if the value in register P1 is False. The value is
1866** is considered true if it has a numeric value of zero. If the value
1867** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00001868*/
drh9cbf3422008-01-17 16:22:13 +00001869case OP_If: /* jump, in1 */
1870case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00001871 int c;
drh3c84ddf2008-01-09 02:15:38 +00001872 if( pIn1->flags & MEM_Null ){
1873 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00001874 }else{
drhba0232a2005-06-06 17:27:19 +00001875#ifdef SQLITE_OMIT_FLOATING_POINT
drh3c84ddf2008-01-09 02:15:38 +00001876 c = sqlite3VdbeIntValue(pIn1);
drhba0232a2005-06-06 17:27:19 +00001877#else
drh3c84ddf2008-01-09 02:15:38 +00001878 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00001879#endif
drhf5905aa2002-05-26 20:54:33 +00001880 if( pOp->opcode==OP_IfNot ) c = !c;
1881 }
drh3c84ddf2008-01-09 02:15:38 +00001882 if( c ){
1883 pc = pOp->p2-1;
1884 }
drh5e00f6c2001-09-13 13:46:56 +00001885 break;
1886}
1887
drh2d401ab2008-01-10 23:50:11 +00001888/* Opcode: IsNull P1 P2 P3 * *
drh477df4b2008-01-05 18:48:24 +00001889**
drh2d401ab2008-01-10 23:50:11 +00001890** Jump to P2 if the value in register P1 is NULL. If P3 is greater
1891** than zero, then check all values reg(P1), reg(P1+1),
1892** reg(P1+2), ..., reg(P1+P3-1).
drh477df4b2008-01-05 18:48:24 +00001893*/
drh9cbf3422008-01-17 16:22:13 +00001894case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh2d401ab2008-01-10 23:50:11 +00001895 int n = pOp->p3;
1896 assert( pOp->p3==0 || pOp->p1>0 );
1897 do{
1898 if( (pIn1->flags & MEM_Null)!=0 ){
1899 pc = pOp->p2 - 1;
1900 break;
1901 }
1902 pIn1++;
1903 }while( --n > 0 );
drh477df4b2008-01-05 18:48:24 +00001904 break;
1905}
1906
drh98757152008-01-09 23:04:12 +00001907/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001908**
drh6a288a32008-01-07 19:20:24 +00001909** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00001910*/
drh9cbf3422008-01-17 16:22:13 +00001911case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh6a288a32008-01-07 19:20:24 +00001912 if( (pIn1->flags & MEM_Null)==0 ){
1913 pc = pOp->p2 - 1;
1914 }
drh5e00f6c2001-09-13 13:46:56 +00001915 break;
1916}
1917
danielk1977cd3e8f72008-03-25 09:47:35 +00001918/* Opcode: SetNumColumns * P2 * * *
danielk1977b4964b72004-05-18 01:23:38 +00001919**
danielk1977cd3e8f72008-03-25 09:47:35 +00001920** This opcode sets the number of columns for the cursor opened by the
1921** following instruction to P2.
danielk1977b4964b72004-05-18 01:23:38 +00001922**
danielk1977cd3e8f72008-03-25 09:47:35 +00001923** An OP_SetNumColumns is only useful if it occurs immediately before
1924** one of the following opcodes:
danielk1977ac171782005-02-05 06:49:54 +00001925**
danielk1977cd3e8f72008-03-25 09:47:35 +00001926** OpenRead
1927** OpenWrite
1928** OpenPseudo
1929**
1930** If the OP_Column opcode is to be executed on a cursor, then
1931** this opcode must be present immediately before the opcode that
1932** opens the cursor.
danielk1977b4964b72004-05-18 01:23:38 +00001933*/
drh9cbf3422008-01-17 16:22:13 +00001934case OP_SetNumColumns: {
danielk1977b4964b72004-05-18 01:23:38 +00001935 break;
1936}
1937
danielk197760585dd2008-01-03 08:08:40 +00001938/* Opcode: Column P1 P2 P3 P4 *
danielk1977192ac1d2004-05-10 07:17:30 +00001939**
danielk1977cfcdaef2004-05-12 07:33:33 +00001940** Interpret the data that cursor P1 points to as a structure built using
1941** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00001942** information about the format of the data.) Extract the P2-th column
1943** from this record. If there are less that (P2+1)
1944** values in the record, extract a NULL.
1945**
drh9cbf3422008-01-17 16:22:13 +00001946** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00001947**
danielk19771f4aa332008-01-03 09:51:55 +00001948** If the column contains fewer than P2 fields, then extract a NULL. Or,
1949** if the P4 argument is a P4_MEM use the value of the P4 argument as
1950** the result.
danielk1977192ac1d2004-05-10 07:17:30 +00001951*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001952case OP_Column: {
danielk197764202cf2008-11-17 15:31:47 +00001953 int payloadSize; /* Number of bytes in the record */
drhd3194f52004-05-27 19:59:32 +00001954 int p1 = pOp->p1; /* P1 value of the opcode */
danielk1977cfcdaef2004-05-12 07:33:33 +00001955 int p2 = pOp->p2; /* column number to retrieve */
drhdfe88ec2008-11-03 20:55:06 +00001956 VdbeCursor *pC = 0;/* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00001957 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00001958 BtCursor *pCrsr; /* The BTree cursor */
1959 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1960 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00001961 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00001962 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00001963 int i; /* Loop counter */
1964 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00001965 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00001966 Mem sMem; /* For storing the record being decoded */
danielk1977192ac1d2004-05-10 07:17:30 +00001967
drhb27b7f52008-12-10 18:03:45 +00001968 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00001969 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00001970 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1971 pDest = &p->aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001972 MemSetTypeFlag(pDest, MEM_Null);
danielk1977cfcdaef2004-05-12 07:33:33 +00001973
drhe61cffc2004-06-12 18:12:15 +00001974 /* This block sets the variable payloadSize to be the total number of
1975 ** bytes in the record.
1976 **
1977 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00001978 ** The complete record text is always available for pseudo-tables
1979 ** If the record is stored in a cursor, the complete record text
1980 ** might be available in the pC->aRow cache. Or it might not be.
1981 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00001982 **
1983 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00001984 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00001985 */
drhb73857f2006-03-17 00:25:59 +00001986 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00001987 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00001988#ifndef SQLITE_OMIT_VIRTUALTABLE
1989 assert( pC->pVtabCursor==0 );
1990#endif
drhb73857f2006-03-17 00:25:59 +00001991 if( pC->pCursor!=0 ){
drhe61cffc2004-06-12 18:12:15 +00001992 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00001993 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00001994 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00001995 zRec = 0;
1996 pCrsr = pC->pCursor;
1997 if( pC->nullRow ){
1998 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00001999 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002000 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002001 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002002 }else if( pC->isIndex ){
danielk197796fc5fe2004-05-13 11:34:16 +00002003 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002004 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drh9c1905f2008-12-10 22:32:56 +00002005 payloadSize = (int)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002006 }else{
danielk197764202cf2008-11-17 15:31:47 +00002007 sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize);
danielk1977192ac1d2004-05-10 07:17:30 +00002008 }
drhd3194f52004-05-27 19:59:32 +00002009 nField = pC->nField;
drha05a7222008-01-19 03:35:58 +00002010 }else{
2011 assert( pC->pseudoTable );
drhe61cffc2004-06-12 18:12:15 +00002012 /* The record is the sole entry of a pseudo-table */
danielk1977192ac1d2004-05-10 07:17:30 +00002013 payloadSize = pC->nData;
2014 zRec = pC->pData;
drh76873ab2006-01-07 18:48:26 +00002015 pC->cacheStatus = CACHE_STALE;
danielk1977192ac1d2004-05-10 07:17:30 +00002016 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00002017 nField = pC->nField;
danielk1977f7df9cc2004-06-16 12:02:47 +00002018 pCrsr = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002019 }
2020
drh9cbf3422008-01-17 16:22:13 +00002021 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002022 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002023 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002024 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002025 }
drhbb4957f2008-03-20 14:03:29 +00002026 if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002027 goto too_big;
2028 }
danielk1977192ac1d2004-05-10 07:17:30 +00002029
drhd3194f52004-05-27 19:59:32 +00002030 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002031
drh9188b382004-05-14 21:12:22 +00002032 /* Read and parse the table header. Store the results of the parse
2033 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002034 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002035 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002036 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002037 aOffset = pC->aOffset;
2038 }else{
danielk1977dedf45b2006-01-13 17:12:01 +00002039 u8 *zIdx; /* Index into header */
2040 u8 *zEndHdr; /* Pointer to first byte after the header */
danielk197764202cf2008-11-17 15:31:47 +00002041 int offset; /* Offset into the data */
drh0ac07192006-02-10 14:02:07 +00002042 int szHdrSz; /* Size of the header size field at start of record */
drhb27b7f52008-12-10 18:03:45 +00002043 int avail = 0; /* Number of bytes of available data */
drhb73857f2006-03-17 00:25:59 +00002044
danielk1977cd3e8f72008-03-25 09:47:35 +00002045 assert(aType);
drhb73857f2006-03-17 00:25:59 +00002046 pC->aOffset = aOffset = &aType[nField];
2047 pC->payloadSize = payloadSize;
2048 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002049
drhd3194f52004-05-27 19:59:32 +00002050 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002051 if( zRec ){
2052 zData = zRec;
2053 }else{
drhf0863fe2005-06-12 21:35:51 +00002054 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002055 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002056 }else{
drhe51c44f2004-05-30 20:46:09 +00002057 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002058 }
drhe61cffc2004-06-12 18:12:15 +00002059 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2060 ** save the payload in the pC->aRow cache. That will save us from
2061 ** having to make additional calls to fetch the content portion of
2062 ** the record.
2063 */
2064 if( avail>=payloadSize ){
drh2646da72005-12-09 20:02:05 +00002065 zRec = zData;
2066 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002067 }else{
2068 pC->aRow = 0;
2069 }
drhd3194f52004-05-27 19:59:32 +00002070 }
drh588f5bc2007-01-02 18:41:54 +00002071 /* The following assert is true in all cases accept when
2072 ** the database file has been corrupted externally.
2073 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
shane3f8d5cf2008-04-24 19:15:09 +00002074 szHdrSz = getVarint32((u8*)zData, offset);
drhe61cffc2004-06-12 18:12:15 +00002075
2076 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2077 ** record header in most cases. But they will fail to get the complete
2078 ** record header if the record header does not fit on a single page
2079 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2080 ** acquire the complete header text.
2081 */
danielk1977dedf45b2006-01-13 17:12:01 +00002082 if( !zRec && avail<offset ){
danielk1977a7a8e142008-02-13 18:25:27 +00002083 sMem.flags = 0;
2084 sMem.db = 0;
drhb21c8cd2007-08-21 19:33:56 +00002085 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002086 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002087 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002088 }
drhb6f54522004-05-20 02:42:16 +00002089 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002090 }
drh0ac07192006-02-10 14:02:07 +00002091 zEndHdr = (u8 *)&zData[offset];
2092 zIdx = (u8 *)&zData[szHdrSz];
drh9188b382004-05-14 21:12:22 +00002093
drhd3194f52004-05-27 19:59:32 +00002094 /* Scan the header and use it to fill in the aType[] and aOffset[]
2095 ** arrays. aType[i] will contain the type integer for the i-th
2096 ** column and aOffset[i] will contain the offset from the beginning
2097 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002098 */
danielk1977dedf45b2006-01-13 17:12:01 +00002099 for(i=0; i<nField; i++){
2100 if( zIdx<zEndHdr ){
2101 aOffset[i] = offset;
shane3f8d5cf2008-04-24 19:15:09 +00002102 zIdx += getVarint32(zIdx, aType[i]);
danielk1977dedf45b2006-01-13 17:12:01 +00002103 offset += sqlite3VdbeSerialTypeLen(aType[i]);
2104 }else{
2105 /* If i is less that nField, then there are less fields in this
2106 ** record than SetNumColumns indicated there are columns in the
2107 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002108 ** the record to 0. This tells code below to store a NULL
2109 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002110 */
2111 aOffset[i] = 0;
2112 }
drh9188b382004-05-14 21:12:22 +00002113 }
danielk19775f096132008-03-28 15:44:09 +00002114 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002115 sMem.flags = MEM_Null;
2116
danielk19779792eef2006-01-13 15:58:43 +00002117 /* If we have read more header data than was contained in the header,
2118 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002119 ** record, or if the end of the last field appears to be before the end
2120 ** of the record (when all fields present), then we must be dealing
2121 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002122 */
danielk1977fb8f2e22008-09-22 06:13:31 +00002123 if( zIdx>zEndHdr || offset>payloadSize
2124 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002125 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002126 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002127 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002128 }
danielk1977192ac1d2004-05-10 07:17:30 +00002129
danielk197736963fd2005-02-19 08:18:05 +00002130 /* Get the column information. If aOffset[p2] is non-zero, then
2131 ** deserialize the value from the record. If aOffset[p2] is zero,
2132 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002133 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002134 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002135 */
danielk197736963fd2005-02-19 08:18:05 +00002136 if( aOffset[p2] ){
2137 assert( rc==SQLITE_OK );
2138 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002139 sqlite3VdbeMemReleaseExternal(pDest);
2140 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002141 }else{
2142 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002143 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002144 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002145 if( rc!=SQLITE_OK ){
2146 goto op_column_out;
2147 }
2148 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002149 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002150 }
drhd4e70eb2008-01-02 00:34:36 +00002151 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002152 }else{
danielk197760585dd2008-01-03 08:08:40 +00002153 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002154 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002155 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002156 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002157 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002158 }
drhfebe1062004-08-28 18:17:48 +00002159
2160 /* If we dynamically allocated space to hold the data (in the
2161 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002162 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002163 ** This prevents a memory copy.
2164 */
danielk19775f096132008-03-28 15:44:09 +00002165 if( sMem.zMalloc ){
2166 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002167 assert( !(pDest->flags & MEM_Dyn) );
2168 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2169 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002170 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002171 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002172 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002173 }
drhfebe1062004-08-28 18:17:48 +00002174
drhd4e70eb2008-01-02 00:34:36 +00002175 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002176
danielk19773c9cc8d2005-01-17 03:40:08 +00002177op_column_out:
drhb7654112008-01-12 12:48:07 +00002178 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002179 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002180 break;
2181}
2182
danielk1977751de562008-04-18 09:01:15 +00002183/* Opcode: Affinity P1 P2 * P4 *
2184**
2185** Apply affinities to a range of P2 registers starting with P1.
2186**
2187** P4 is a string that is P2 characters long. The nth character of the
2188** string indicates the column affinity that should be used for the nth
2189** memory cell in the range.
2190*/
2191case OP_Affinity: {
2192 char *zAffinity = pOp->p4.z;
2193 Mem *pData0 = &p->aMem[pOp->p1];
2194 Mem *pLast = &pData0[pOp->p2-1];
2195 Mem *pRec;
2196
2197 for(pRec=pData0; pRec<=pLast; pRec++){
danielk1977b790c6c2008-04-18 10:25:24 +00002198 ExpandBlob(pRec);
danielk1977751de562008-04-18 09:01:15 +00002199 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2200 }
2201 break;
2202}
2203
drh1db639c2008-01-17 02:36:28 +00002204/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002205**
drh1db639c2008-01-17 02:36:28 +00002206** Convert P2 registers beginning with P1 into a single entry
drh7a224de2004-06-02 01:22:02 +00002207** suitable for use as a data record in a database table or as a key
shane21e7feb2008-05-30 15:59:49 +00002208** in an index. The details of the format are irrelevant as long as
drh1e968a02008-03-25 00:22:21 +00002209** the OP_Column opcode can decode the record later.
2210** Refer to source code comments for the details of the record
drh7a224de2004-06-02 01:22:02 +00002211** format.
2212**
danielk1977751de562008-04-18 09:01:15 +00002213** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002214** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002215** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002216**
drh8a512562005-11-14 22:29:05 +00002217** The mapping from character to affinity is given by the SQLITE_AFF_
2218** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002219**
drh66a51672008-01-03 00:01:23 +00002220** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002221*/
drh1db639c2008-01-17 02:36:28 +00002222case OP_MakeRecord: {
drhf3218fe2004-05-28 08:21:02 +00002223 /* Assuming the record contains N fields, the record format looks
2224 ** like this:
2225 **
drh7a224de2004-06-02 01:22:02 +00002226 ** ------------------------------------------------------------------------
2227 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2228 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002229 **
drh9cbf3422008-01-17 16:22:13 +00002230 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2231 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002232 **
2233 ** Each type field is a varint representing the serial type of the
2234 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002235 ** hdr-size field is also a varint which is the offset from the beginning
2236 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002237 */
drhfdf972a2007-05-02 13:30:27 +00002238 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2239 Mem *pRec; /* The new record */
drh023ae032007-05-08 12:12:16 +00002240 u64 nData = 0; /* Number of bytes of data space */
danielk1977ededfd52004-06-17 07:53:01 +00002241 int nHdr = 0; /* Number of bytes of header space */
danielk197764202cf2008-11-17 15:31:47 +00002242 i64 nByte = 0; /* Data space required for this record */
drhfdf972a2007-05-02 13:30:27 +00002243 int nZero = 0; /* Number of zero bytes at the end of the record */
drhcb9882a2005-03-17 03:15:40 +00002244 int nVarint; /* Number of bytes in a varint */
danielk1977ededfd52004-06-17 07:53:01 +00002245 u32 serial_type; /* Type field */
drh1db639c2008-01-17 02:36:28 +00002246 Mem *pData0; /* First field to be combined into the record */
2247 Mem *pLast; /* Last field of the record */
danielk1977ededfd52004-06-17 07:53:01 +00002248 int nField; /* Number of fields in the record */
danielk1977ededfd52004-06-17 07:53:01 +00002249 char *zAffinity; /* The affinity string for the record */
drhd946db02005-12-29 19:23:06 +00002250 int file_format; /* File format to use for encoding */
drhfdf972a2007-05-02 13:30:27 +00002251 int i; /* Space used in zNewRecord[] */
danielk1977ededfd52004-06-17 07:53:01 +00002252
drh1db639c2008-01-17 02:36:28 +00002253 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002254 zAffinity = pOp->p4.z;
drh1db639c2008-01-17 02:36:28 +00002255 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem );
2256 pData0 = &p->aMem[nField];
2257 nField = pOp->p2;
2258 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002259 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002260
drhf3218fe2004-05-28 08:21:02 +00002261 /* Loop through the elements that will make up the record to figure
2262 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002263 */
drha2a49dc2008-01-02 14:28:13 +00002264 for(pRec=pData0; pRec<=pLast; pRec++){
drhae7e1512007-05-02 16:51:59 +00002265 int len;
drhd3d39e92004-05-20 22:16:29 +00002266 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002267 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002268 }
danielk1977d908f5a2007-05-11 07:08:28 +00002269 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002270 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002271 }
drhd946db02005-12-29 19:23:06 +00002272 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002273 len = sqlite3VdbeSerialTypeLen(serial_type);
2274 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002275 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002276 if( pRec->flags & MEM_Zero ){
2277 /* Only pure zero-filled BLOBs can be input to this Opcode.
2278 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002279 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002280 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002281 nZero = 0;
2282 }
danielk19778d059842004-05-12 11:24:02 +00002283 }
danielk19773d1bfea2004-05-14 11:00:53 +00002284
drhf3218fe2004-05-28 08:21:02 +00002285 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002286 nHdr += nVarint = sqlite3VarintLen(nHdr);
2287 if( nVarint<sqlite3VarintLen(nHdr) ){
2288 nHdr++;
2289 }
drhfdf972a2007-05-02 13:30:27 +00002290 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002291 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002292 goto too_big;
2293 }
drhf3218fe2004-05-28 08:21:02 +00002294
danielk1977a7a8e142008-02-13 18:25:27 +00002295 /* Make sure the output register has a buffer large enough to store
2296 ** the new record. The output register (pOp->p3) is not allowed to
2297 ** be one of the input registers (because the following call to
2298 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2299 */
2300 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2301 pOut = &p->aMem[pOp->p3];
drh9c1905f2008-12-10 22:32:56 +00002302 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002303 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002304 }
danielk1977a7a8e142008-02-13 18:25:27 +00002305 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002306
2307 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002308 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002309 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002310 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002311 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002312 }
drha2a49dc2008-01-02 14:28:13 +00002313 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002314 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002315 }
drhfdf972a2007-05-02 13:30:27 +00002316 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002317
drh9cbf3422008-01-17 16:22:13 +00002318 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002319 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002320 pOut->flags = MEM_Blob | MEM_Dyn;
2321 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002322 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002323 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002324 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002325 }
drh477df4b2008-01-05 18:48:24 +00002326 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002327 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002328 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002329 break;
2330}
2331
drh98757152008-01-09 23:04:12 +00002332/* Opcode: Statement P1 * * * *
drh663fc632002-02-02 18:49:19 +00002333**
drh7f0f12e2004-05-21 13:39:50 +00002334** Begin an individual statement transaction which is part of a larger
drh82ed1e52008-04-25 12:25:42 +00002335** transaction. This is needed so that the statement
drh7f0f12e2004-05-21 13:39:50 +00002336** can be rolled back after an error without having to roll back the
2337** entire transaction. The statement transaction will automatically
2338** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002339**
drh82ed1e52008-04-25 12:25:42 +00002340** If the database connection is currently in autocommit mode (that
2341** is to say, if it is in between BEGIN and COMMIT)
2342** and if there are no other active statements on the same database
2343** connection, then this operation is a no-op. No statement transaction
2344** is needed since any error can use the normal ROLLBACK process to
2345** undo changes.
2346**
2347** If a statement transaction is started, then a statement journal file
2348** will be allocated and initialized.
2349**
drh7f0f12e2004-05-21 13:39:50 +00002350** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002351** database file has an index of 0 and the file used for temporary tables
2352** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002353*/
drh9cbf3422008-01-17 16:22:13 +00002354case OP_Statement: {
drha05a7222008-01-19 03:35:58 +00002355 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
2356 int i = pOp->p1;
2357 Btree *pBt;
2358 assert( i>=0 && i<db->nDb );
2359 assert( db->aDb[i].pBt!=0 );
2360 pBt = db->aDb[i].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002361 assert( sqlite3BtreeIsInTrans(pBt) );
drhfb982642007-08-30 01:19:59 +00002362 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002363 if( !sqlite3BtreeIsInStmt(pBt) ){
2364 rc = sqlite3BtreeBeginStmt(pBt);
danielk1977182c4ba2007-06-27 15:53:34 +00002365 p->openedStatement = 1;
danielk19771d850a72004-05-31 08:26:49 +00002366 }
2367 }
2368 break;
2369}
2370
danielk1977fd7f0452008-12-17 17:30:26 +00002371/* Opcode: Savepoint P1 * * P4 *
2372**
2373** Open, release or rollback the savepoint named by parameter P4, depending
2374** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2375** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2376*/
2377case OP_Savepoint: {
2378 int p1 = pOp->p1;
2379 char *zName = pOp->p4.z; /* Name of savepoint */
2380
2381 /* Assert that the p1 parameter is valid. Also that if there is no open
2382 ** transaction, then there cannot be any savepoints.
2383 */
2384 assert( db->pSavepoint==0 || db->autoCommit==0 );
2385 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2386 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2387 assert( checkSavepointCount(db) );
2388
2389 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002390 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002391 /* A new savepoint cannot be created if there are active write
2392 ** statements (i.e. open read/write incremental blob handles).
2393 */
2394 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2395 "SQL statements in progress");
2396 rc = SQLITE_BUSY;
2397 }else{
2398 int nName = sqlite3Strlen30(zName);
2399 Savepoint *pNew;
2400
2401 /* Create a new savepoint structure. */
2402 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2403 if( pNew ){
2404 pNew->zName = (char *)&pNew[1];
2405 memcpy(pNew->zName, zName, nName+1);
2406
2407 /* If there is no open transaction, then mark this as a special
2408 ** "transaction savepoint". */
2409 if( db->autoCommit ){
2410 db->autoCommit = 0;
2411 db->isTransactionSavepoint = 1;
2412 }else{
2413 db->nSavepoint++;
2414 }
2415
2416 /* Link the new savepoint into the database handle's list. */
2417 pNew->pNext = db->pSavepoint;
2418 db->pSavepoint = pNew;
2419 }
2420 }
2421 }else{
2422 Savepoint *pSavepoint;
2423 int iSavepoint = 0;
2424
2425 /* Find the named savepoint. If there is no such savepoint, then an
2426 ** an error is returned to the user. */
2427 for(
2428 pSavepoint=db->pSavepoint;
2429 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
2430 pSavepoint=pSavepoint->pNext
2431 ){
2432 iSavepoint++;
2433 }
2434 if( !pSavepoint ){
2435 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2436 rc = SQLITE_ERROR;
2437 }else if(
2438 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2439 ){
2440 /* It is not possible to release (commit) a savepoint if there are
2441 ** active write statements. It is not possible to rollback a savepoint
2442 ** if there are any active statements at all.
2443 */
2444 sqlite3SetString(&p->zErrMsg, db,
2445 "cannot %s savepoint - SQL statements in progress",
2446 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2447 );
2448 rc = SQLITE_BUSY;
2449 }else{
2450
2451 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002452 ** and this is a RELEASE command, then the current transaction
2453 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002454 */
2455 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2456 if( isTransaction && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002457 db->autoCommit = 1;
2458 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2459 p->pc = pc;
2460 db->autoCommit = 0;
2461 p->rc = rc = SQLITE_BUSY;
2462 goto vdbe_return;
2463 }
danielk197734cf35d2008-12-18 18:31:38 +00002464 db->isTransactionSavepoint = 0;
2465 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002466 }else{
2467 int ii;
2468 iSavepoint = db->nSavepoint - iSavepoint - 1;
2469 for(ii=0; ii<db->nDb; ii++){
2470 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2471 if( rc!=SQLITE_OK ){
2472 goto abort_due_to_error;
2473 }
2474 }
drh9f0bbf92009-01-02 21:08:09 +00002475 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002476 sqlite3ExpirePreparedStatements(db);
2477 sqlite3ResetInternalSchema(db, 0);
2478 }
2479 }
2480
2481 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2482 ** savepoints nested inside of the savepoint being operated on. */
2483 while( db->pSavepoint!=pSavepoint ){
2484 Savepoint *pTmp = db->pSavepoint;
2485 db->pSavepoint = pTmp->pNext;
2486 sqlite3DbFree(db, pTmp);
2487 db->nSavepoint--;
2488 }
2489
2490 /* If it is a RELEASE, then destroy the savepoint being operated on too */
2491 if( p1==SAVEPOINT_RELEASE ){
2492 assert( pSavepoint==db->pSavepoint );
2493 db->pSavepoint = pSavepoint->pNext;
2494 sqlite3DbFree(db, pSavepoint);
2495 if( !isTransaction ){
2496 db->nSavepoint--;
2497 }
2498 }
2499 }
2500 }
2501
2502 break;
2503}
2504
drh98757152008-01-09 23:04:12 +00002505/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002506**
2507** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002508** back any currently active btree transactions. If there are any active
2509** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
drh92f02c32004-09-02 14:57:08 +00002510**
2511** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002512*/
drh9cbf3422008-01-17 16:22:13 +00002513case OP_AutoCommit: {
drhad4a4b82008-11-05 16:37:34 +00002514 int desiredAutoCommit = pOp->p1;
2515 int rollback = pOp->p2;
2516 int turnOnAC = desiredAutoCommit && !db->autoCommit;
danielk19771d850a72004-05-31 08:26:49 +00002517
drhad4a4b82008-11-05 16:37:34 +00002518 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
2519 assert( desiredAutoCommit==1 || rollback==0 );
danielk19771d850a72004-05-31 08:26:49 +00002520
drh92f02c32004-09-02 14:57:08 +00002521 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002522
drhad4a4b82008-11-05 16:37:34 +00002523 if( turnOnAC && rollback && db->activeVdbeCnt>1 ){
2524 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002525 ** still running, and a transaction is active, return an error indicating
2526 ** that the other VMs must complete first.
2527 */
drhad4a4b82008-11-05 16:37:34 +00002528 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2529 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002530 rc = SQLITE_BUSY;
drhad4a4b82008-11-05 16:37:34 +00002531 }else if( turnOnAC && !rollback && db->writeVdbeCnt>1 ){
2532 /* If this instruction implements a COMMIT and other VMs are writing
2533 ** return an error indicating that the other VMs must complete first.
2534 */
2535 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2536 "SQL statements in progress");
2537 rc = SQLITE_BUSY;
2538 }else if( desiredAutoCommit!=db->autoCommit ){
danielk1977fd7f0452008-12-17 17:30:26 +00002539 if( rollback ){
drhad4a4b82008-11-05 16:37:34 +00002540 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002541 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002542 db->autoCommit = 1;
2543 }else{
shane7d3846a2008-12-11 02:58:26 +00002544 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002545 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002546 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002547 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002548 p->rc = rc = SQLITE_BUSY;
2549 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002550 }
danielk19771d850a72004-05-31 08:26:49 +00002551 }
danielk1977fd7f0452008-12-17 17:30:26 +00002552 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002553 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002554 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002555 }else{
drh900b31e2007-08-28 02:27:51 +00002556 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002557 }
drh900b31e2007-08-28 02:27:51 +00002558 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002559 }else{
drhf089aa42008-07-08 19:34:06 +00002560 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002561 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
danielk19771d850a72004-05-31 08:26:49 +00002562 (rollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002563 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002564
2565 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002566 }
2567 break;
2568}
2569
drh98757152008-01-09 23:04:12 +00002570/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002571**
2572** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002573** opcode is encountered. Depending on the ON CONFLICT setting, the
2574** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002575**
drh001bbcb2003-03-19 03:14:00 +00002576** P1 is the index of the database file on which the transaction is
2577** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002578** file used for temporary tables. Indices of 2 or more are used for
2579** attached databases.
drhcabb0812002-09-14 13:47:32 +00002580**
drh80242052004-06-09 00:48:12 +00002581** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002582** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002583** other process can start another write transaction while this transaction is
2584** underway. Starting a write transaction also creates a rollback journal. A
2585** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002586** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2587** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002588**
2589** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002590*/
drh9cbf3422008-01-17 16:22:13 +00002591case OP_Transaction: {
drh001bbcb2003-03-19 03:14:00 +00002592 int i = pOp->p1;
danielk19771d850a72004-05-31 08:26:49 +00002593 Btree *pBt;
2594
drh8bf8dc92003-05-17 17:35:10 +00002595 assert( i>=0 && i<db->nDb );
drhfb982642007-08-30 01:19:59 +00002596 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002597 pBt = db->aDb[i].pBt;
2598
danielk197724162fe2004-06-04 06:22:00 +00002599 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002600 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002601 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002602 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002603 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002604 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002605 }
danielk19772ef68482008-07-07 17:13:08 +00002606 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
danielk197724162fe2004-06-04 06:22:00 +00002607 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002608 }
drhb86ccfb2003-01-28 23:13:10 +00002609 }
drh5e00f6c2001-09-13 13:46:56 +00002610 break;
2611}
2612
drhb1fdb2a2008-01-05 04:06:03 +00002613/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002614**
drh9cbf3422008-01-17 16:22:13 +00002615** Read cookie number P3 from database P1 and write it into register P2.
drh4c583122008-01-04 22:01:03 +00002616** P3==0 is the schema version. P3==1 is the database format.
2617** P3==2 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002618** the main database file and P1==1 is the database file used to store
2619** temporary tables.
drh4a324312001-12-21 14:30:42 +00002620**
danielk1977418899a2007-06-24 10:14:00 +00002621** If P1 is negative, then this is a request to read the size of a
drh4c583122008-01-04 22:01:03 +00002622** databases free-list. P3 must be set to 1 in this case. The actual
danielk1977418899a2007-06-24 10:14:00 +00002623** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
danielk1977d62e76c2007-06-24 16:11:03 +00002624** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
danielk1977418899a2007-06-24 10:14:00 +00002625**
drh50e5dad2001-09-15 00:57:28 +00002626** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002627** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002628** executing this instruction.
2629*/
drh4c583122008-01-04 22:01:03 +00002630case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002631 int iMeta;
danielk1977180b56a2007-06-24 08:00:42 +00002632 int iDb = pOp->p1;
drh4c583122008-01-04 22:01:03 +00002633 int iCookie = pOp->p3;
danielk1977180b56a2007-06-24 08:00:42 +00002634
drhb7654112008-01-12 12:48:07 +00002635 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002636 if( iDb<0 ){
2637 iDb = (-1*(iDb+1));
2638 iCookie *= -1;
2639 }
2640 assert( iDb>=0 && iDb<db->nDb );
2641 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002642 assert( (p->btreeMask & (1<<iDb))!=0 );
drha3b321d2004-05-11 09:31:31 +00002643 /* The indexing of meta values at the schema layer is off by one from
2644 ** the indexing in the btree layer. The btree considers meta[0] to
2645 ** be the number of free pages in the database (a read-only value)
2646 ** and meta[1] to be the schema cookie. The schema layer considers
2647 ** meta[1] to be the schema cookie. So we have to shift the index
2648 ** by one in the following statement.
2649 */
danielk1977180b56a2007-06-24 08:00:42 +00002650 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002651 pOut->u.i = iMeta;
danielk1977a7a8e142008-02-13 18:25:27 +00002652 MemSetTypeFlag(pOut, MEM_Int);
drh50e5dad2001-09-15 00:57:28 +00002653 break;
2654}
2655
drh98757152008-01-09 23:04:12 +00002656/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002657**
drh98757152008-01-09 23:04:12 +00002658** Write the content of register P3 (interpreted as an integer)
2659** into cookie number P2 of database P1.
drh001bbcb2003-03-19 03:14:00 +00002660** P2==0 is the schema version. P2==1 is the database format.
2661** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2662** the main database file and P1==1 is the database file used to store
2663** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002664**
2665** A transaction must be started before executing this opcode.
2666*/
drh9cbf3422008-01-17 16:22:13 +00002667case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002668 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002669 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002670 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002671 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002672 pDb = &db->aDb[pOp->p1];
2673 assert( pDb->pBt!=0 );
drh98757152008-01-09 23:04:12 +00002674 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002675 /* See note about index shifting on OP_ReadCookie */
drh98757152008-01-09 23:04:12 +00002676 rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i);
drh3f7d4e42004-07-24 14:35:58 +00002677 if( pOp->p2==0 ){
2678 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002679 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002680 db->flags |= SQLITE_InternChanges;
drhd28bcb32005-12-21 14:43:11 +00002681 }else if( pOp->p2==1 ){
2682 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002683 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002684 }
drhfd426c62006-01-30 15:34:22 +00002685 if( pOp->p1==1 ){
2686 /* Invalidate all prepared statements whenever the TEMP database
2687 ** schema is changed. Ticket #1644 */
2688 sqlite3ExpirePreparedStatements(db);
2689 }
drh50e5dad2001-09-15 00:57:28 +00002690 break;
2691}
2692
drh4a324312001-12-21 14:30:42 +00002693/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002694**
drh001bbcb2003-03-19 03:14:00 +00002695** Check the value of global database parameter number 0 (the
2696** schema version) and make sure it is equal to P2.
2697** P1 is the database number which is 0 for the main database file
2698** and 1 for the file holding temporary tables and some higher number
2699** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002700**
2701** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002702** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002703** and that the current process needs to reread the schema.
2704**
2705** Either a transaction needs to have been started or an OP_Open needs
2706** to be executed (to establish a read lock) before this opcode is
2707** invoked.
2708*/
drh9cbf3422008-01-17 16:22:13 +00002709case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002710 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002711 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002712 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002713 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002714 pBt = db->aDb[pOp->p1].pBt;
2715 if( pBt ){
2716 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2717 }else{
2718 rc = SQLITE_OK;
2719 iMeta = 0;
2720 }
drhf328bc82004-05-10 23:29:49 +00002721 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
drh633e6d52008-07-28 19:34:53 +00002722 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002723 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002724 /* If the schema-cookie from the database file matches the cookie
2725 ** stored with the in-memory representation of the schema, do
2726 ** not reload the schema from the database file.
2727 **
shane21e7feb2008-05-30 15:59:49 +00002728 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002729 ** Often, v-tables store their data in other SQLite tables, which
2730 ** are queried from within xNext() and other v-table methods using
2731 ** prepared queries. If such a query is out-of-date, we do not want to
2732 ** discard the database schema, as the user code implementing the
2733 ** v-table would have to be ready for the sqlite3_vtab structure itself
2734 ** to be invalidated whenever sqlite3_step() is called from within
2735 ** a v-table method.
2736 */
2737 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2738 sqlite3ResetInternalSchema(db, pOp->p1);
2739 }
2740
drhf6d8ab82007-01-12 23:43:42 +00002741 sqlite3ExpirePreparedStatements(db);
drh50e5dad2001-09-15 00:57:28 +00002742 rc = SQLITE_SCHEMA;
2743 }
2744 break;
2745}
2746
drh98757152008-01-09 23:04:12 +00002747/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002748**
drhecdc7532001-09-23 02:35:53 +00002749** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002750** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002751** P3==0 means the main database, P3==1 means the database used for
2752** temporary tables, and P3>1 means used the corresponding attached
2753** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002754** values need not be contiguous but all P1 values should be small integers.
2755** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002756**
drh98757152008-01-09 23:04:12 +00002757** If P5!=0 then use the content of register P2 as the root page, not
2758** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002759**
drhb19a2bc2001-09-16 00:13:26 +00002760** There will be a read lock on the database whenever there is an
2761** open cursor. If the database was unlocked prior to this instruction
2762** then a read lock is acquired as part of this instruction. A read
2763** lock allows other processes to read the database but prohibits
2764** any other process from modifying the database. The read lock is
2765** released when all cursors are closed. If this instruction attempts
2766** to get a read lock but fails, the script terminates with an
2767** SQLITE_BUSY error code.
2768**
drh66a51672008-01-03 00:01:23 +00002769** The P4 value is a pointer to a KeyInfo structure that defines the
2770** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002771** that are not pointing to indices.
drhf57b3392001-10-08 13:22:32 +00002772**
drh001bbcb2003-03-19 03:14:00 +00002773** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002774*/
drh98757152008-01-09 23:04:12 +00002775/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002776**
2777** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002778** page is P2. Or if P5!=0 use the content of register P2 to find the
2779** root page.
drhecdc7532001-09-23 02:35:53 +00002780**
drh66a51672008-01-03 00:01:23 +00002781** The P4 value is a pointer to a KeyInfo structure that defines the
2782** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002783** that are not pointing to indices.
jplyon5a564222003-06-02 06:15:58 +00002784**
drh001bbcb2003-03-19 03:14:00 +00002785** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002786** in read/write mode. For a given table, there can be one or more read-only
2787** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002788**
drh001bbcb2003-03-19 03:14:00 +00002789** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002790*/
drh9cbf3422008-01-17 16:22:13 +00002791case OP_OpenRead:
2792case OP_OpenWrite: {
drh5e00f6c2001-09-13 13:46:56 +00002793 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002794 int p2 = pOp->p2;
danielk1977207872a2008-01-03 07:54:23 +00002795 int iDb = pOp->p3;
drhf57b3392001-10-08 13:22:32 +00002796 int wrFlag;
2797 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00002798 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00002799 Db *pDb;
drh001bbcb2003-03-19 03:14:00 +00002800
drh6810ce62004-01-31 19:22:56 +00002801 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00002802 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00002803 pDb = &db->aDb[iDb];
2804 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002805 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002806 if( pOp->opcode==OP_OpenWrite ){
2807 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002808 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2809 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002810 }
2811 }else{
2812 wrFlag = 0;
2813 }
drh98757152008-01-09 23:04:12 +00002814 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00002815 assert( p2>0 );
2816 assert( p2<=p->nMem );
2817 pIn2 = &p->aMem[p2];
2818 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00002819 p2 = (int)pIn2->u.i;
shanedcc50b72008-11-13 18:29:50 +00002820 if( p2<2 ) {
2821 rc = SQLITE_CORRUPT_BKPT;
2822 goto abort_due_to_error;
2823 }
drh5edc3122001-09-13 21:53:09 +00002824 }
drh6810ce62004-01-31 19:22:56 +00002825 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002826 pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
drh4774b132004-06-12 20:12:51 +00002827 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00002828 pCur->nullRow = 1;
danielk1977cd3e8f72008-03-25 09:47:35 +00002829 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor);
drh66a51672008-01-03 00:01:23 +00002830 if( pOp->p4type==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +00002831 pCur->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002832 pCur->pKeyInfo->enc = ENC(p->db);
danielk197724162fe2004-06-04 06:22:00 +00002833 }else{
drhf0863fe2005-06-12 21:35:51 +00002834 pCur->pKeyInfo = 0;
danielk197724162fe2004-06-04 06:22:00 +00002835 }
2836 switch( rc ){
2837 case SQLITE_BUSY: {
danielk19772a764eb2004-06-12 01:43:26 +00002838 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002839 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002840 goto vdbe_return;
drhd3d39e92004-05-20 22:16:29 +00002841 }
danielk197724162fe2004-06-04 06:22:00 +00002842 case SQLITE_OK: {
2843 int flags = sqlite3BtreeFlags(pCur->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002844 /* Sanity checking. Only the lower four bits of the flags byte should
danielk1977ad0132d2008-06-07 08:58:22 +00002845 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
drhf0863fe2005-06-12 21:35:51 +00002846 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2847 ** 2 (zerodata for indices). If these conditions are not met it can
2848 ** only mean that we are dealing with a corrupt database file
2849 */
2850 if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
drh49285702005-09-17 15:20:26 +00002851 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002852 goto abort_due_to_error;
2853 }
drh9c1905f2008-12-10 22:32:56 +00002854 pCur->isTable = (flags & BTREE_INTKEY)!=0 ?1:0;
2855 pCur->isIndex = (flags & BTREE_ZERODATA)!=0 ?1:0;
drh66a51672008-01-03 00:01:23 +00002856 /* If P4==0 it means we are expected to open a table. If P4!=0 then
drhf0863fe2005-06-12 21:35:51 +00002857 ** we expect to be opening an index. If this is not what happened,
2858 ** then the database is corrupt
2859 */
drh66a51672008-01-03 00:01:23 +00002860 if( (pCur->isTable && pOp->p4type==P4_KEYINFO)
2861 || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
drh49285702005-09-17 15:20:26 +00002862 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002863 goto abort_due_to_error;
2864 }
danielk197724162fe2004-06-04 06:22:00 +00002865 break;
drh5e00f6c2001-09-13 13:46:56 +00002866 }
danielk197724162fe2004-06-04 06:22:00 +00002867 case SQLITE_EMPTY: {
drh66a51672008-01-03 00:01:23 +00002868 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhf0863fe2005-06-12 21:35:51 +00002869 pCur->isIndex = !pCur->isTable;
danielk1977cd3e8f72008-03-25 09:47:35 +00002870 pCur->pCursor = 0;
danielk197724162fe2004-06-04 06:22:00 +00002871 rc = SQLITE_OK;
2872 break;
2873 }
2874 default: {
2875 goto abort_due_to_error;
2876 }
2877 }
drh5e00f6c2001-09-13 13:46:56 +00002878 break;
2879}
2880
drh98757152008-01-09 23:04:12 +00002881/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00002882**
drhb9bb7c12006-06-11 23:41:55 +00002883** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00002884** The cursor is always opened read/write even if
2885** the main database is read-only. The transient or virtual
2886** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002887**
drh0342b1f2005-09-01 03:07:44 +00002888** P2 is the number of columns in the virtual table.
drh66a51672008-01-03 00:01:23 +00002889** The cursor points to a BTree table if P4==0 and to a BTree index
2890** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00002891** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00002892**
2893** This opcode was once called OpenTemp. But that created
2894** confusion because the term "temp table", might refer either
2895** to a TEMP table at the SQL level, or to a table opened by
2896** this opcode. Then this opcode was call OpenVirtual. But
2897** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00002898*/
drh9cbf3422008-01-17 16:22:13 +00002899case OP_OpenEphemeral: {
drh5e00f6c2001-09-13 13:46:56 +00002900 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002901 VdbeCursor *pCx;
drh33f4e022007-09-03 15:19:34 +00002902 static const int openFlags =
2903 SQLITE_OPEN_READWRITE |
2904 SQLITE_OPEN_CREATE |
2905 SQLITE_OPEN_EXCLUSIVE |
2906 SQLITE_OPEN_DELETEONCLOSE |
2907 SQLITE_OPEN_TRANSIENT_DB;
2908
drh6810ce62004-01-31 19:22:56 +00002909 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002910 pCx = allocateCursor(p, i, pOp, -1, 1);
drh4774b132004-06-12 20:12:51 +00002911 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00002912 pCx->nullRow = 1;
drh33f4e022007-09-03 15:19:34 +00002913 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
2914 &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002915 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00002916 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00002917 }
2918 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002919 /* If a transient index is required, create it by calling
2920 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2921 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002922 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002923 */
danielk19772dca4ac2008-01-03 11:50:29 +00002924 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00002925 int pgno;
drh66a51672008-01-03 00:01:23 +00002926 assert( pOp->p4type==P4_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00002927 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002928 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002929 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00002930 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00002931 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00002932 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002933 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00002934 }
drhf0863fe2005-06-12 21:35:51 +00002935 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00002936 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002937 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002938 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00002939 }
drh5e00f6c2001-09-13 13:46:56 +00002940 }
drhf0863fe2005-06-12 21:35:51 +00002941 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00002942 break;
2943}
2944
danielk19779882d992008-03-27 17:59:01 +00002945/* Opcode: OpenPseudo P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00002946**
2947** Open a new cursor that points to a fake table that contains a single
2948** row of data. Any attempt to write a second row of data causes the
2949** first row to be deleted. All data is deleted when the cursor is
2950** closed.
2951**
2952** A pseudo-table created by this opcode is useful for holding the
drhcdd536f2006-03-17 00:04:03 +00002953** NEW or OLD tables in a trigger. Also used to hold the a single
2954** row output from the sorter so that the row can be decomposed into
2955** individual columns using the OP_Column opcode.
danielk19779882d992008-03-27 17:59:01 +00002956**
2957** When OP_Insert is executed to insert a row in to the pseudo table,
2958** the pseudo-table cursor may or may not make it's own copy of the
2959** original row data. If P2 is 0, then the pseudo-table will copy the
2960** original row data. Otherwise, a pointer to the original memory cell
2961** is stored. In this case, the vdbe program must ensure that the
2962** memory cell containing the row data is not overwritten until the
2963** pseudo table is closed (or a new row is inserted into it).
drh70ce3f02003-04-15 19:22:22 +00002964*/
drh9cbf3422008-01-17 16:22:13 +00002965case OP_OpenPseudo: {
drh70ce3f02003-04-15 19:22:22 +00002966 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002967 VdbeCursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002968 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002969 pCx = allocateCursor(p, i, &pOp[-1], -1, 0);
drh4774b132004-06-12 20:12:51 +00002970 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00002971 pCx->nullRow = 1;
2972 pCx->pseudoTable = 1;
drh9c1905f2008-12-10 22:32:56 +00002973 pCx->ephemPseudoTable = (u8)pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00002974 pCx->isTable = 1;
2975 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00002976 break;
2977}
2978
drh98757152008-01-09 23:04:12 +00002979/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00002980**
2981** Close a cursor previously opened as P1. If P1 is not
2982** currently open, this instruction is a no-op.
2983*/
drh9cbf3422008-01-17 16:22:13 +00002984case OP_Close: {
drh5e00f6c2001-09-13 13:46:56 +00002985 int i = pOp->p1;
drha05a7222008-01-19 03:35:58 +00002986 assert( i>=0 && i<p->nCursor );
2987 sqlite3VdbeFreeCursor(p, p->apCsr[i]);
2988 p->apCsr[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00002989 break;
2990}
2991
drh959403f2008-12-12 17:56:16 +00002992/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00002993**
danielk1977b790c6c2008-04-18 10:25:24 +00002994** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00002995** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00002996** to an SQL index, then P3 is the first in an array of P4 registers
2997** that are used as an unpacked index key.
2998**
2999** Reposition cursor P1 so that it points to the smallest entry that
3000** is greater than or equal to the key value. If there are no records
3001** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003002**
drh959403f2008-12-12 17:56:16 +00003003** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003004*/
drh959403f2008-12-12 17:56:16 +00003005/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003006**
danielk1977b790c6c2008-04-18 10:25:24 +00003007** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003008** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003009** to an SQL index, then P3 is the first in an array of P4 registers
3010** that are used as an unpacked index key.
3011**
3012** Reposition cursor P1 so that it points to the smallest entry that
3013** is greater than the key value. If there are no records greater than
3014** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003015**
drh959403f2008-12-12 17:56:16 +00003016** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003017*/
drh959403f2008-12-12 17:56:16 +00003018/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003019**
danielk1977b790c6c2008-04-18 10:25:24 +00003020** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003021** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003022** to an SQL index, then P3 is the first in an array of P4 registers
3023** that are used as an unpacked index key.
3024**
3025** Reposition cursor P1 so that it points to the largest entry that
3026** is less than the key value. If there are no records less than
3027** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003028**
drh959403f2008-12-12 17:56:16 +00003029** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003030*/
drh959403f2008-12-12 17:56:16 +00003031/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003032**
danielk1977b790c6c2008-04-18 10:25:24 +00003033** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003034** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003035** to an SQL index, then P3 is the first in an array of P4 registers
3036** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003037**
danielk1977b790c6c2008-04-18 10:25:24 +00003038** Reposition cursor P1 so that it points to the largest entry that
3039** is less than or equal to the key value. If there are no records
3040** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003041**
drh959403f2008-12-12 17:56:16 +00003042** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003043*/
drh959403f2008-12-12 17:56:16 +00003044case OP_SeekLt: /* jump, in3 */
3045case OP_SeekLe: /* jump, in3 */
3046case OP_SeekGe: /* jump, in3 */
3047case OP_SeekGt: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00003048 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003049 VdbeCursor *pC;
drh80ff32f2001-11-04 18:32:46 +00003050
drh70ce3f02003-04-15 19:22:22 +00003051 assert( i>=0 && i<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003052 assert( pOp->p2!=0 );
drhd7556d22004-05-14 21:59:40 +00003053 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003054 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003055 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00003056 int res, oc;
drh7cf6e4d2004-05-19 14:56:55 +00003057 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003058 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003059 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003060 i64 iKey; /* The rowid we are to seek to */
3061
3062 /* The input value in P3 might be of any type: integer, real, string,
3063 ** blob, or NULL. But it needs to be an integer before we can do
3064 ** the seek, so covert it. */
3065 applyNumericAffinity(pIn3);
3066 iKey = sqlite3VdbeIntValue(pIn3);
3067 pC->rowidIsValid = 0;
3068
3069 /* If the P3 value could not be converted into an integer without
3070 ** loss of information, then special processing is required... */
3071 if( (pIn3->flags & MEM_Int)==0 ){
3072 if( (pIn3->flags & MEM_Real)==0 ){
3073 /* If the P3 value cannot be converted into any kind of a number,
3074 ** then the seek is not possible, so jump to P2 */
3075 pc = pOp->p2 - 1;
3076 break;
3077 }
3078 /* If we reach this point, then the P3 value must be a floating
3079 ** point number. */
3080 assert( (pIn3->flags & MEM_Real)!=0 );
3081
3082 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
3083 /* The P3 value is to large in magnitude to be expressed as an
3084 ** integer. */
3085 res = 1;
3086 if( pIn3->r<0 ){
3087 if( oc==OP_SeekGt || oc==OP_SeekGe ){
3088 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3089 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3090 }
3091 }else{
3092 if( oc==OP_SeekLt || oc==OP_SeekLe ){
3093 rc = sqlite3BtreeLast(pC->pCursor, &res);
3094 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3095 }
3096 }
3097 if( res ){
3098 pc = pOp->p2 - 1;
3099 }
3100 break;
3101 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3102 /* Use the ceiling() function to convert real->int */
3103 if( pIn3->r > (double)iKey ) iKey++;
3104 }else{
3105 /* Use the floor() function to convert real->int */
3106 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3107 if( pIn3->r < (double)iKey ) iKey--;
3108 }
3109 }
drhe63d9992008-08-13 19:11:48 +00003110 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003111 if( rc!=SQLITE_OK ){
3112 goto abort_due_to_error;
3113 }
drh959403f2008-12-12 17:56:16 +00003114 if( res==0 ){
3115 pC->rowidIsValid = 1;
3116 pC->lastRowid = iKey;
3117 }
drh5e00f6c2001-09-13 13:46:56 +00003118 }else{
danielk1977b790c6c2008-04-18 10:25:24 +00003119 UnpackedRecord r;
3120 int nField = pOp->p4.i;
3121 assert( pOp->p4type==P4_INT32 );
3122 assert( nField>0 );
3123 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003124 r.nField = (u16)nField;
drh959403f2008-12-12 17:56:16 +00003125 if( oc==OP_SeekGt || oc==OP_SeekLe ){
drhe63d9992008-08-13 19:11:48 +00003126 r.flags = UNPACKED_INCRKEY;
3127 }else{
3128 r.flags = 0;
3129 }
danielk1977b790c6c2008-04-18 10:25:24 +00003130 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00003131 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003132 if( rc!=SQLITE_OK ){
3133 goto abort_due_to_error;
3134 }
drhf0863fe2005-06-12 21:35:51 +00003135 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003136 }
drha11846b2004-01-07 18:52:56 +00003137 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003138 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003139#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003140 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003141#endif
drh959403f2008-12-12 17:56:16 +00003142 if( oc==OP_SeekGe || oc==OP_SeekGt ){
3143 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003144 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003145 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003146 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003147 }else{
3148 res = 0;
drh8721ce42001-11-07 14:22:00 +00003149 }
drh7cf6e4d2004-05-19 14:56:55 +00003150 }else{
drh959403f2008-12-12 17:56:16 +00003151 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3152 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003153 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3154 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003155 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003156 }else{
3157 /* res might be negative because the table is empty. Check to
3158 ** see if this is the case.
3159 */
drhf328bc82004-05-10 23:29:49 +00003160 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003161 }
drh1af3fdb2004-07-18 21:33:01 +00003162 }
drh91fd4d42008-01-19 20:11:25 +00003163 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003164 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003165 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003166 }
danielk1977f7b9d662008-06-23 18:49:43 +00003167 }else if( !pC->pseudoTable ){
3168 /* This happens when attempting to open the sqlite3_master table
3169 ** for read access returns SQLITE_EMPTY. In this case always
3170 ** take the jump (since there are no records in the table).
3171 */
3172 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003173 }
drh5e00f6c2001-09-13 13:46:56 +00003174 break;
3175}
3176
drh959403f2008-12-12 17:56:16 +00003177/* Opcode: Seek P1 P2 * * *
3178**
3179** P1 is an open table cursor and P2 is a rowid integer. Arrange
3180** for P1 to move so that it points to the rowid given by P2.
3181**
3182** This is actually a deferred seek. Nothing actually happens until
3183** the cursor is used to read a record. That way, if no reads
3184** occur, no unnecessary I/O happens.
3185*/
3186case OP_Seek: { /* in2 */
3187 int i = pOp->p1;
3188 VdbeCursor *pC;
3189
3190 assert( i>=0 && i<p->nCursor );
3191 pC = p->apCsr[i];
3192 assert( pC!=0 );
3193 if( pC->pCursor!=0 ){
3194 assert( pC->isTable );
3195 pC->nullRow = 0;
3196 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3197 pC->rowidIsValid = 0;
3198 pC->deferredMoveto = 1;
3199 }
3200 break;
3201}
3202
3203
drh98757152008-01-09 23:04:12 +00003204/* Opcode: Found P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003205**
drh98757152008-01-09 23:04:12 +00003206** Register P3 holds a blob constructed by MakeRecord. P1 is an index.
drh9cbf3422008-01-17 16:22:13 +00003207** If an entry that matches the value in register p3 exists in P1 then
3208** jump to P2. If the P3 value does not match any entry in P1
drhf0863fe2005-06-12 21:35:51 +00003209** then fall thru. The P1 cursor is left pointing at the matching entry
drh2dcef112008-01-12 19:03:48 +00003210** if it exists.
drhf0863fe2005-06-12 21:35:51 +00003211**
3212** This instruction is used to implement the IN operator where the
danielk19779a96b662007-11-29 17:05:18 +00003213** left-hand side is a SELECT statement. P1 may be a true index, or it
3214** may be a temporary index that holds the results of the SELECT
drh2dcef112008-01-12 19:03:48 +00003215** statement. This instruction is also used to implement the
3216** DISTINCT keyword in SELECT statements.
danielk19779a96b662007-11-29 17:05:18 +00003217**
3218** This instruction checks if index P1 contains a record for which
shane21e7feb2008-05-30 15:59:49 +00003219** the first N serialized values exactly match the N serialized values
drh9cbf3422008-01-17 16:22:13 +00003220** in the record in register P3, where N is the total number of values in
3221** the P3 record (the P3 record is a prefix of the P1 record).
drhb19a2bc2001-09-16 00:13:26 +00003222**
drhcb6d50e2008-08-21 19:28:30 +00003223** See also: NotFound, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00003224*/
drh98757152008-01-09 23:04:12 +00003225/* Opcode: NotFound P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003226**
drh98757152008-01-09 23:04:12 +00003227** Register P3 holds a blob constructed by MakeRecord. P1 is
drhf0863fe2005-06-12 21:35:51 +00003228** an index. If no entry exists in P1 that matches the blob then jump
drh795ab9b2007-01-27 13:37:22 +00003229** to P2. If an entry does existing, fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003230** pointing to the entry that matches.
drh5e00f6c2001-09-13 13:46:56 +00003231**
drhcb6d50e2008-08-21 19:28:30 +00003232** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003233*/
drh9cbf3422008-01-17 16:22:13 +00003234case OP_NotFound: /* jump, in3 */
3235case OP_Found: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00003236 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00003237 int alreadyExists = 0;
drhdfe88ec2008-11-03 20:55:06 +00003238 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003239 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003240 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003241 if( (pC = p->apCsr[i])->pCursor!=0 ){
danielk197777519402007-08-30 11:48:31 +00003242 int res;
drhe63d9992008-08-13 19:11:48 +00003243 UnpackedRecord *pIdxKey;
3244
drhf0863fe2005-06-12 21:35:51 +00003245 assert( pC->isTable==0 );
drh98757152008-01-09 23:04:12 +00003246 assert( pIn3->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003247 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
drh23f79d02008-08-20 22:06:47 +00003248 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003249 if( pIdxKey==0 ){
3250 goto no_mem;
danielk19779a96b662007-11-29 17:05:18 +00003251 }
drhe63d9992008-08-13 19:11:48 +00003252 if( pOp->opcode==OP_Found ){
3253 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3254 }
3255 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3256 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk197777519402007-08-30 11:48:31 +00003257 if( rc!=SQLITE_OK ){
3258 break;
3259 }
3260 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003261 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003262 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003263 }
3264 if( pOp->opcode==OP_Found ){
3265 if( alreadyExists ) pc = pOp->p2 - 1;
3266 }else{
3267 if( !alreadyExists ) pc = pOp->p2 - 1;
3268 }
drh5e00f6c2001-09-13 13:46:56 +00003269 break;
3270}
3271
drh98757152008-01-09 23:04:12 +00003272/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003273**
drh98757152008-01-09 23:04:12 +00003274** The P3 register contains an integer record number. Call this
3275** record number R. The P4 register contains an index key created
drhe63d9992008-08-13 19:11:48 +00003276** using MakeRecord. Call it K.
drh9cfcf5d2002-01-29 18:41:24 +00003277**
drh7cf6e4d2004-05-19 14:56:55 +00003278** P1 is an index. So it has no data and its key consists of a
drhf0863fe2005-06-12 21:35:51 +00003279** record generated by OP_MakeRecord where the last field is the
3280** rowid of the entry that the index refers to.
drhf3218fe2004-05-28 08:21:02 +00003281**
drh0ca3e242002-01-29 23:07:02 +00003282** This instruction asks if there is an entry in P1 where the
drh7cf6e4d2004-05-19 14:56:55 +00003283** fields matches K but the rowid is different from R.
3284** If there is no such entry, then there is an immediate
drh0ca3e242002-01-29 23:07:02 +00003285** jump to P2. If any entry does exist where the index string
3286** matches K but the record number is not R, then the record
drh98757152008-01-09 23:04:12 +00003287** number for that entry is written into P3 and control
drh0ca3e242002-01-29 23:07:02 +00003288** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003289**
drh9cbf3422008-01-17 16:22:13 +00003290** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003291*/
drh9cbf3422008-01-17 16:22:13 +00003292case OP_IsUnique: { /* jump, in3 */
drh9cfcf5d2002-01-29 18:41:24 +00003293 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003294 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003295 BtCursor *pCrsr;
drh98757152008-01-09 23:04:12 +00003296 Mem *pK;
danielk1977452c9892004-05-13 05:16:15 +00003297 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00003298
drh0ca3e242002-01-29 23:07:02 +00003299 /* Pop the value R off the top of the stack
3300 */
drh98757152008-01-09 23:04:12 +00003301 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003302 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
3303 pK = &p->aMem[pOp->p4.i];
drh98757152008-01-09 23:04:12 +00003304 sqlite3VdbeMemIntegerify(pIn3);
3305 R = pIn3->u.i;
drh73bdf072006-08-15 14:21:16 +00003306 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003307 pCx = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003308 assert( pCx!=0 );
drhf328bc82004-05-10 23:29:49 +00003309 pCrsr = pCx->pCursor;
3310 if( pCrsr!=0 ){
danielk1977f2fa8312006-01-24 13:09:33 +00003311 int res;
drhe63d9992008-08-13 19:11:48 +00003312 i64 v; /* The record number that matches K */
3313 UnpackedRecord *pIdxKey; /* Unpacked version of P4 */
drh0ca3e242002-01-29 23:07:02 +00003314
3315 /* Make sure K is a string and make zKey point to K
3316 */
drh98757152008-01-09 23:04:12 +00003317 assert( pK->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003318 pIdxKey = sqlite3VdbeRecordUnpack(pCx->pKeyInfo, pK->n, pK->z,
drh23f79d02008-08-20 22:06:47 +00003319 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003320 if( pIdxKey==0 ){
3321 goto no_mem;
3322 }
3323 pIdxKey->flags |= UNPACKED_IGNORE_ROWID;
danielk1977452c9892004-05-13 05:16:15 +00003324
drhe63d9992008-08-13 19:11:48 +00003325 /* Search for an entry in P1 where all but the last rowid match K
drh0ca3e242002-01-29 23:07:02 +00003326 ** If there is no such entry, jump immediately to P2.
3327 */
drh9188b382004-05-14 21:12:22 +00003328 assert( pCx->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003329 pCx->cacheStatus = CACHE_STALE;
drhe63d9992008-08-13 19:11:48 +00003330 rc = sqlite3BtreeMovetoUnpacked(pCrsr, pIdxKey, 0, 0, &res);
danielk1977f0113002006-01-24 12:09:17 +00003331 if( rc!=SQLITE_OK ){
drhe63d9992008-08-13 19:11:48 +00003332 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk1977f0113002006-01-24 12:09:17 +00003333 goto abort_due_to_error;
3334 }
drh9cfcf5d2002-01-29 18:41:24 +00003335 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003336 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00003337 if( res ){
3338 pc = pOp->p2 - 1;
drhe63d9992008-08-13 19:11:48 +00003339 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003340 break;
3341 }
3342 }
drhe63d9992008-08-13 19:11:48 +00003343 rc = sqlite3VdbeIdxKeyCompare(pCx, pIdxKey, &res);
3344 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003345 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3346 if( res>0 ){
3347 pc = pOp->p2 - 1;
3348 break;
3349 }
drh0ca3e242002-01-29 23:07:02 +00003350
3351 /* At this point, pCrsr is pointing to an entry in P1 where all but
drhf3218fe2004-05-28 08:21:02 +00003352 ** the final entry (the rowid) matches K. Check to see if the
3353 ** final rowid column is different from R. If it equals R then jump
danielk1977452c9892004-05-13 05:16:15 +00003354 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00003355 */
drhb21c8cd2007-08-21 19:33:56 +00003356 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
danielk1977452c9892004-05-13 05:16:15 +00003357 if( rc!=SQLITE_OK ){
3358 goto abort_due_to_error;
3359 }
drh0ca3e242002-01-29 23:07:02 +00003360 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003361 pc = pOp->p2 - 1;
3362 break;
3363 }
drh0ca3e242002-01-29 23:07:02 +00003364
drh9cbf3422008-01-17 16:22:13 +00003365 /* The final varint of the key is different from R. Store it back
3366 ** into register R3. (The record number of an entry that violates
3367 ** a UNIQUE constraint.)
drh0ca3e242002-01-29 23:07:02 +00003368 */
drh98757152008-01-09 23:04:12 +00003369 pIn3->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003370 assert( pIn3->flags&MEM_Int );
drh9cfcf5d2002-01-29 18:41:24 +00003371 }
3372 break;
3373}
3374
drh9cbf3422008-01-17 16:22:13 +00003375/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003376**
drh9cbf3422008-01-17 16:22:13 +00003377** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003378** with that key does not exist in table of P1, then jump to P2.
3379** If the record does exist, then fall thru. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003380** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003381**
3382** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003383** operation assumes the key is an integer and that P1 is a table whereas
3384** NotFound assumes key is a blob constructed from MakeRecord and
3385** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003386**
drhcb6d50e2008-08-21 19:28:30 +00003387** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003388*/
drh9cbf3422008-01-17 16:22:13 +00003389case OP_NotExists: { /* jump, in3 */
drh6b125452002-01-28 15:53:03 +00003390 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003391 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003392 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003393 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003394 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003395 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh03e1f512008-12-11 13:05:00 +00003396 int res = 0;
danielk197736a3c702004-05-11 06:55:14 +00003397 u64 iKey;
drh98757152008-01-09 23:04:12 +00003398 assert( pIn3->flags & MEM_Int );
drhf0863fe2005-06-12 21:35:51 +00003399 assert( p->apCsr[i]->isTable );
drh98757152008-01-09 23:04:12 +00003400 iKey = intToKey(pIn3->u.i);
drhe63d9992008-08-13 19:11:48 +00003401 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0,&res);
drh98757152008-01-09 23:04:12 +00003402 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003403 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003404 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003405 pC->cacheStatus = CACHE_STALE;
danielk197728129562005-01-11 10:25:06 +00003406 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003407 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003408 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003409 }
danielk1977f7b9d662008-06-23 18:49:43 +00003410 }else if( !pC->pseudoTable ){
3411 /* This happens when an attempt to open a read cursor on the
3412 ** sqlite_master table returns SQLITE_EMPTY.
3413 */
3414 assert( pC->isTable );
3415 pc = pOp->p2 - 1;
3416 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003417 }
drh6b125452002-01-28 15:53:03 +00003418 break;
3419}
3420
drh4c583122008-01-04 22:01:03 +00003421/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003422**
drh4c583122008-01-04 22:01:03 +00003423** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003424** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003425** The sequence number on the cursor is incremented after this
3426** instruction.
drh4db38a72005-09-01 12:16:28 +00003427*/
drh4c583122008-01-04 22:01:03 +00003428case OP_Sequence: { /* out2-prerelease */
drh4db38a72005-09-01 12:16:28 +00003429 int i = pOp->p1;
drh4db38a72005-09-01 12:16:28 +00003430 assert( i>=0 && i<p->nCursor );
3431 assert( p->apCsr[i]!=0 );
drh4c583122008-01-04 22:01:03 +00003432 pOut->u.i = p->apCsr[i]->seqCount++;
danielk1977a7a8e142008-02-13 18:25:27 +00003433 MemSetTypeFlag(pOut, MEM_Int);
drh4db38a72005-09-01 12:16:28 +00003434 break;
3435}
3436
3437
drh98757152008-01-09 23:04:12 +00003438/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003439**
drhf0863fe2005-06-12 21:35:51 +00003440** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003441** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003442** table that cursor P1 points to. The new record number is written
3443** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003444**
drh98757152008-01-09 23:04:12 +00003445** If P3>0 then P3 is a register that holds the largest previously
drh205f48e2004-11-05 00:43:11 +00003446** generated record number. No new record numbers are allowed to be less
drh2958a4e2004-11-12 03:56:15 +00003447** than this value. When this value reaches its maximum, a SQLITE_FULL
drh98757152008-01-09 23:04:12 +00003448** error is generated. The P3 register is updated with the generated
drh4c583122008-01-04 22:01:03 +00003449** record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003450** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003451*/
drh4c583122008-01-04 22:01:03 +00003452case OP_NewRowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003453 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00003454 i64 v = 0;
drhdfe88ec2008-11-03 20:55:06 +00003455 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003456 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003457 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003458 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00003459 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003460 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003461 /* The next rowid or record number (different terms for the same
3462 ** thing) is obtained in a two-step algorithm.
3463 **
3464 ** First we attempt to find the largest existing rowid and add one
3465 ** to that. But if the largest existing rowid is already the maximum
3466 ** positive integer, we have to fall through to the second
3467 ** probabilistic algorithm
3468 **
3469 ** The second algorithm is to select a rowid at random and see if
3470 ** it already exists in the table. If it does not exist, we have
3471 ** succeeded. If the random rowid does exist, we select a new one
3472 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00003473 **
3474 ** For a table with less than 2 billion entries, the probability
3475 ** of not finding a unused rowid is about 1.0e-300. This is a
3476 ** non-zero probability, but it is still vanishingly small and should
3477 ** never cause a problem. You are much, much more likely to have a
3478 ** hardware failure than for this algorithm to fail.
3479 **
drhaf9ff332002-01-16 21:00:27 +00003480 ** The analysis in the previous paragraph assumes that you have a good
3481 ** source of random numbers. Is a library function like lrand48()
3482 ** good enough? Maybe. Maybe not. It's hard to know whether there
3483 ** might be subtle bugs is some implementations of lrand48() that
3484 ** could cause problems. To avoid uncertainty, SQLite uses its own
3485 ** random number generator based on the RC4 algorithm.
3486 **
drhdb5ed6d2001-09-18 22:17:44 +00003487 ** To promote locality of reference for repetitive inserts, the
shane21e7feb2008-05-30 15:59:49 +00003488 ** first few attempts at choosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00003489 ** larger than the previous rowid. This has been shown experimentally
3490 ** to double the speed of the COPY operation.
3491 */
danielk1977f7df9cc2004-06-16 12:02:47 +00003492 int res, rx=SQLITE_OK, cnt;
drhf328bc82004-05-10 23:29:49 +00003493 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00003494 cnt = 0;
drh4e6083c2005-02-04 21:13:00 +00003495 if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3496 BTREE_INTKEY ){
drh49285702005-09-17 15:20:26 +00003497 rc = SQLITE_CORRUPT_BKPT;
drh4e6083c2005-02-04 21:13:00 +00003498 goto abort_due_to_error;
3499 }
drhf328bc82004-05-10 23:29:49 +00003500 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3501 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drhfe2093d2005-01-20 22:48:47 +00003502
drh75f86a42005-02-17 00:03:06 +00003503#ifdef SQLITE_32BIT_ROWID
3504# define MAX_ROWID 0x7fffffff
3505#else
drhfe2093d2005-01-20 22:48:47 +00003506 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3507 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3508 ** to provide the constant while making all compilers happy.
3509 */
danielk197764202cf2008-11-17 15:31:47 +00003510# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003511#endif
drhfe2093d2005-01-20 22:48:47 +00003512
drh5cf8e8c2002-02-19 22:42:05 +00003513 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00003514 if( pC->nextRowidValid ){
3515 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00003516 }else{
danielk1977261919c2005-12-06 12:52:59 +00003517 rc = sqlite3BtreeLast(pC->pCursor, &res);
3518 if( rc!=SQLITE_OK ){
3519 goto abort_due_to_error;
3520 }
drh32fbe342002-10-19 20:16:37 +00003521 if( res ){
3522 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003523 }else{
danielk1977e0d4b062004-06-28 01:11:46 +00003524 sqlite3BtreeKeySize(pC->pCursor, &v);
drh32fbe342002-10-19 20:16:37 +00003525 v = keyToInt(v);
drh75f86a42005-02-17 00:03:06 +00003526 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003527 pC->useRandomRowid = 1;
3528 }else{
3529 v++;
3530 }
drh5cf8e8c2002-02-19 22:42:05 +00003531 }
drh3fc190c2001-09-14 03:24:23 +00003532 }
drh205f48e2004-11-05 00:43:11 +00003533
3534#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003535 if( pOp->p3 ){
drh205f48e2004-11-05 00:43:11 +00003536 Mem *pMem;
drh4c583122008-01-04 22:01:03 +00003537 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
3538 pMem = &p->aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00003539 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003540 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003541 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003542 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003543 rc = SQLITE_FULL;
3544 goto abort_due_to_error;
3545 }
drh3c024d62007-03-30 11:23:45 +00003546 if( v<pMem->u.i+1 ){
3547 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003548 }
drh3c024d62007-03-30 11:23:45 +00003549 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003550 }
3551#endif
3552
drh75f86a42005-02-17 00:03:06 +00003553 if( v<MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003554 pC->nextRowidValid = 1;
3555 pC->nextRowid = v+1;
3556 }else{
3557 pC->nextRowidValid = 0;
3558 }
drh5cf8e8c2002-02-19 22:42:05 +00003559 }
3560 if( pC->useRandomRowid ){
drh4c583122008-01-04 22:01:03 +00003561 assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
drh5cf8e8c2002-02-19 22:42:05 +00003562 v = db->priorNewRowid;
3563 cnt = 0;
3564 do{
drh91fd4d42008-01-19 20:11:25 +00003565 if( cnt==0 && (v&0xffffff)==v ){
3566 v++;
3567 }else{
drh2fa18682008-03-19 14:15:34 +00003568 sqlite3_randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003569 if( cnt<5 ) v &= 0xffffff;
drh5cf8e8c2002-02-19 22:42:05 +00003570 }
3571 if( v==0 ) continue;
3572 x = intToKey(v);
drhe63d9992008-08-13 19:11:48 +00003573 rx = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)x, 0, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003574 cnt++;
drh91fd4d42008-01-19 20:11:25 +00003575 }while( cnt<100 && rx==SQLITE_OK && res==0 );
drh5cf8e8c2002-02-19 22:42:05 +00003576 db->priorNewRowid = v;
3577 if( rx==SQLITE_OK && res==0 ){
3578 rc = SQLITE_FULL;
3579 goto abort_due_to_error;
3580 }
drh1eaa2692001-09-18 02:02:23 +00003581 }
drhf0863fe2005-06-12 21:35:51 +00003582 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003583 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003584 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003585 }
danielk1977a7a8e142008-02-13 18:25:27 +00003586 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00003587 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003588 break;
3589}
3590
danielk19771f4aa332008-01-03 09:51:55 +00003591/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003592**
jplyon5a564222003-06-02 06:15:58 +00003593** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003594** created if it doesn't already exist or the data for an existing
danielk19771f4aa332008-01-03 09:51:55 +00003595** entry is overwritten. The data is the value stored register
3596** number P2. The key is stored in register P3. The key must
3597** be an integer.
drh4a324312001-12-21 14:30:42 +00003598**
danielk19771f4aa332008-01-03 09:51:55 +00003599** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3600** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003601** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003602** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003603**
drh66a51672008-01-03 00:01:23 +00003604** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003605** may be NULL. If it is not NULL, then the update-hook
3606** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3607**
drh93aed5a2008-01-16 17:46:38 +00003608** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3609** allocated, then ownership of P2 is transferred to the pseudo-cursor
3610** and register P2 becomes ephemeral. If the cursor is changed, the
3611** value of register P2 will then change. Make sure this does not
3612** cause any problems.)
3613**
drhf0863fe2005-06-12 21:35:51 +00003614** This instruction only works on tables. The equivalent instruction
3615** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003616*/
drh9cbf3422008-01-17 16:22:13 +00003617case OP_Insert: {
danielk19771f4aa332008-01-03 09:51:55 +00003618 Mem *pData = &p->aMem[pOp->p2];
3619 Mem *pKey = &p->aMem[pOp->p3];
3620
drha05a7222008-01-19 03:35:58 +00003621 i64 iKey; /* The integer ROWID or key for the record to be inserted */
drh5e00f6c2001-09-13 13:46:56 +00003622 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003623 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003624 assert( i>=0 && i<p->nCursor );
drha05a7222008-01-19 03:35:58 +00003625 pC = p->apCsr[i];
3626 assert( pC!=0 );
3627 assert( pC->pCursor!=0 || pC->pseudoTable );
3628 assert( pKey->flags & MEM_Int );
3629 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003630 REGISTER_TRACE(pOp->p2, pData);
3631 REGISTER_TRACE(pOp->p3, pKey);
danielk19775f8d8a82004-05-11 00:28:42 +00003632
drha05a7222008-01-19 03:35:58 +00003633 iKey = intToKey(pKey->u.i);
3634 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
3635 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
3636 if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){
3637 pC->nextRowidValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003638 }
drha05a7222008-01-19 03:35:58 +00003639 if( pData->flags & MEM_Null ){
3640 pData->z = 0;
3641 pData->n = 0;
3642 }else{
3643 assert( pData->flags & (MEM_Blob|MEM_Str) );
3644 }
3645 if( pC->pseudoTable ){
danielk19779882d992008-03-27 17:59:01 +00003646 if( !pC->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00003647 sqlite3DbFree(db, pC->pData);
danielk19779882d992008-03-27 17:59:01 +00003648 }
drha05a7222008-01-19 03:35:58 +00003649 pC->iKey = iKey;
3650 pC->nData = pData->n;
danielk19775f096132008-03-28 15:44:09 +00003651 if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){
drha05a7222008-01-19 03:35:58 +00003652 pC->pData = pData->z;
danielk19779882d992008-03-27 17:59:01 +00003653 if( !pC->ephemPseudoTable ){
3654 pData->flags &= ~MEM_Dyn;
3655 pData->flags |= MEM_Ephem;
danielk19775f096132008-03-28 15:44:09 +00003656 pData->zMalloc = 0;
danielk19779882d992008-03-27 17:59:01 +00003657 }
drha05a7222008-01-19 03:35:58 +00003658 }else{
drhe5ae5732008-06-15 02:51:47 +00003659 pC->pData = sqlite3Malloc( pC->nData+2 );
drha05a7222008-01-19 03:35:58 +00003660 if( !pC->pData ) goto no_mem;
3661 memcpy(pC->pData, pData->z, pC->nData);
3662 pC->pData[pC->nData] = 0;
3663 pC->pData[pC->nData+1] = 0;
3664 }
3665 pC->nullRow = 0;
3666 }else{
3667 int nZero;
3668 if( pData->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00003669 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003670 }else{
3671 nZero = 0;
3672 }
3673 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3674 pData->z, pData->n, nZero,
3675 pOp->p5 & OPFLAG_APPEND);
3676 }
3677
3678 pC->rowidIsValid = 0;
3679 pC->deferredMoveto = 0;
3680 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003681
drha05a7222008-01-19 03:35:58 +00003682 /* Invoke the update-hook if required. */
3683 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3684 const char *zDb = db->aDb[pC->iDb].zName;
3685 const char *zTbl = pOp->p4.z;
3686 int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3687 assert( pC->isTable );
3688 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3689 assert( pC->iDb>=0 );
3690 }
drh5e00f6c2001-09-13 13:46:56 +00003691 break;
3692}
3693
drh98757152008-01-09 23:04:12 +00003694/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003695**
drh5edc3122001-09-13 21:53:09 +00003696** Delete the record at which the P1 cursor is currently pointing.
3697**
3698** The cursor will be left pointing at either the next or the previous
3699** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003700** the next Next instruction will be a no-op. Hence it is OK to delete
3701** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003702**
rdcb0c374f2004-02-20 22:53:38 +00003703** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003704** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003705**
drh91fd4d42008-01-19 20:11:25 +00003706** P1 must not be pseudo-table. It has to be a real table with
3707** multiple rows.
3708**
3709** If P4 is not NULL, then it is the name of the table that P1 is
3710** pointing to. The update hook will be invoked, if it exists.
3711** If P4 is not NULL then the P1 cursor must have been positioned
3712** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003713*/
drh9cbf3422008-01-17 16:22:13 +00003714case OP_Delete: {
drh5e00f6c2001-09-13 13:46:56 +00003715 int i = pOp->p1;
drh91fd4d42008-01-19 20:11:25 +00003716 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003717 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003718
drh70ce3f02003-04-15 19:22:22 +00003719 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003720 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003721 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003722 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003723
drh91fd4d42008-01-19 20:11:25 +00003724 /* If the update-hook will be invoked, set iKey to the rowid of the
3725 ** row being deleted.
3726 */
3727 if( db->xUpdateCallback && pOp->p4.z ){
3728 assert( pC->isTable );
3729 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3730 iKey = pC->lastRowid;
3731 }
danielk197794eb6a12005-12-15 15:22:08 +00003732
drh91fd4d42008-01-19 20:11:25 +00003733 rc = sqlite3VdbeCursorMoveto(pC);
3734 if( rc ) goto abort_due_to_error;
3735 rc = sqlite3BtreeDelete(pC->pCursor);
3736 pC->nextRowidValid = 0;
3737 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003738
drh91fd4d42008-01-19 20:11:25 +00003739 /* Invoke the update-hook if required. */
3740 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3741 const char *zDb = db->aDb[pC->iDb].zName;
3742 const char *zTbl = pOp->p4.z;
3743 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3744 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003745 }
danielk1977b28af712004-06-21 06:50:26 +00003746 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003747 break;
3748}
3749
danielk1977b28af712004-06-21 06:50:26 +00003750/* Opcode: ResetCount P1 * *
rdcb0c374f2004-02-20 22:53:38 +00003751**
danielk1977b28af712004-06-21 06:50:26 +00003752** This opcode resets the VMs internal change counter to 0. If P1 is true,
3753** then the value of the change counter is copied to the database handle
3754** change counter (returned by subsequent calls to sqlite3_changes())
3755** before it is reset. This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003756*/
drh9cbf3422008-01-17 16:22:13 +00003757case OP_ResetCount: {
danielk1977b28af712004-06-21 06:50:26 +00003758 if( pOp->p1 ){
drh344737f2004-09-19 00:50:20 +00003759 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003760 }
3761 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003762 break;
3763}
3764
drh98757152008-01-09 23:04:12 +00003765/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00003766**
drh98757152008-01-09 23:04:12 +00003767** Write into register P2 the complete row data for cursor P1.
3768** There is no interpretation of the data.
3769** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003770** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00003771**
drhde4fcfd2008-01-19 23:50:26 +00003772** If the P1 cursor must be pointing to a valid row (not a NULL row)
3773** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003774*/
drh98757152008-01-09 23:04:12 +00003775/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00003776**
drh98757152008-01-09 23:04:12 +00003777** Write into register P2 the complete row key for cursor P1.
3778** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00003779** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003780** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00003781**
drhde4fcfd2008-01-19 23:50:26 +00003782** If the P1 cursor must be pointing to a valid row (not a NULL row)
3783** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00003784*/
danielk1977a7a8e142008-02-13 18:25:27 +00003785case OP_RowKey:
3786case OP_RowData: {
drh70ce3f02003-04-15 19:22:22 +00003787 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003788 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00003789 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00003790 u32 n;
drh70ce3f02003-04-15 19:22:22 +00003791
danielk1977a7a8e142008-02-13 18:25:27 +00003792 pOut = &p->aMem[pOp->p2];
3793
drhf0863fe2005-06-12 21:35:51 +00003794 /* Note that RowKey and RowData are really exactly the same instruction */
drh70ce3f02003-04-15 19:22:22 +00003795 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003796 pC = p->apCsr[i];
drhf0863fe2005-06-12 21:35:51 +00003797 assert( pC->isTable || pOp->opcode==OP_RowKey );
3798 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003799 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00003800 assert( pC->nullRow==0 );
3801 assert( pC->pseudoTable==0 );
3802 assert( pC->pCursor!=0 );
3803 pCrsr = pC->pCursor;
3804 rc = sqlite3VdbeCursorMoveto(pC);
3805 if( rc ) goto abort_due_to_error;
3806 if( pC->isIndex ){
3807 i64 n64;
3808 assert( !pC->isTable );
3809 sqlite3BtreeKeySize(pCrsr, &n64);
drhbb4957f2008-03-20 14:03:29 +00003810 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00003811 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00003812 }
drh9c1905f2008-12-10 22:32:56 +00003813 n = (int)n64;
drhde4fcfd2008-01-19 23:50:26 +00003814 }else{
3815 sqlite3BtreeDataSize(pCrsr, &n);
danielk197764202cf2008-11-17 15:31:47 +00003816 if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00003817 goto too_big;
3818 }
drhde4fcfd2008-01-19 23:50:26 +00003819 }
danielk1977a7a8e142008-02-13 18:25:27 +00003820 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
3821 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00003822 }
danielk1977a7a8e142008-02-13 18:25:27 +00003823 pOut->n = n;
3824 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00003825 if( pC->isIndex ){
3826 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
3827 }else{
3828 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00003829 }
danielk197796cb76f2008-01-04 13:24:28 +00003830 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00003831 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00003832 break;
3833}
3834
drh2133d822008-01-03 18:44:59 +00003835/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003836**
drh2133d822008-01-03 18:44:59 +00003837** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00003838** P1 is currently point to.
drh5e00f6c2001-09-13 13:46:56 +00003839*/
drh4c583122008-01-04 22:01:03 +00003840case OP_Rowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003841 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003842 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00003843 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003844
drh70ce3f02003-04-15 19:22:22 +00003845 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003846 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003847 assert( pC!=0 );
drh536065a2005-01-26 21:55:31 +00003848 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003849 if( rc ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003850 if( pC->rowidIsValid ){
3851 v = pC->lastRowid;
drh70ce3f02003-04-15 19:22:22 +00003852 }else if( pC->pseudoTable ){
3853 v = keyToInt(pC->iKey);
drha05a7222008-01-19 03:35:58 +00003854 }else if( pC->nullRow ){
drh4c583122008-01-04 22:01:03 +00003855 /* Leave the rowid set to a NULL */
drhd60ccc62003-06-24 10:39:46 +00003856 break;
drh70ce3f02003-04-15 19:22:22 +00003857 }else{
3858 assert( pC->pCursor!=0 );
danielk1977e0d4b062004-06-28 01:11:46 +00003859 sqlite3BtreeKeySize(pC->pCursor, &v);
drh70ce3f02003-04-15 19:22:22 +00003860 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003861 }
drh4c583122008-01-04 22:01:03 +00003862 pOut->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003863 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00003864 break;
3865}
3866
drh9cbf3422008-01-17 16:22:13 +00003867/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00003868**
3869** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00003870** that occur while the cursor is on the null row will always
3871** write a NULL.
drh17f71932002-02-21 12:01:27 +00003872*/
drh9cbf3422008-01-17 16:22:13 +00003873case OP_NullRow: {
drh17f71932002-02-21 12:01:27 +00003874 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003875 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00003876
drh70ce3f02003-04-15 19:22:22 +00003877 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003878 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003879 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00003880 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00003881 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00003882 if( pC->pCursor ){
3883 sqlite3BtreeClearCursor(pC->pCursor);
3884 }
drh17f71932002-02-21 12:01:27 +00003885 break;
3886}
3887
drh9cbf3422008-01-17 16:22:13 +00003888/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00003889**
drhf0863fe2005-06-12 21:35:51 +00003890** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00003891** will refer to the last entry in the database table or index.
3892** If the table or index is empty and P2>0, then jump immediately to P2.
3893** If P2 is 0 or if the table or index is not empty, fall through
3894** to the following instruction.
3895*/
drh9cbf3422008-01-17 16:22:13 +00003896case OP_Last: { /* jump */
drh9562b552002-02-19 15:00:07 +00003897 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003898 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00003899 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00003900 int res;
drh9562b552002-02-19 15:00:07 +00003901
drh70ce3f02003-04-15 19:22:22 +00003902 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003903 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003904 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00003905 pCrsr = pC->pCursor;
3906 assert( pCrsr!=0 );
3907 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00003908 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00003909 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00003910 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00003911 pC->cacheStatus = CACHE_STALE;
3912 if( res && pOp->p2>0 ){
3913 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00003914 }
3915 break;
3916}
3917
drh0342b1f2005-09-01 03:07:44 +00003918
drh9cbf3422008-01-17 16:22:13 +00003919/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00003920**
3921** This opcode does exactly the same thing as OP_Rewind except that
3922** it increments an undocumented global variable used for testing.
3923**
3924** Sorting is accomplished by writing records into a sorting index,
3925** then rewinding that index and playing it back from beginning to
3926** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3927** rewinding so that the global variable will be incremented and
3928** regression tests can determine whether or not the optimizer is
3929** correctly optimizing out sorts.
3930*/
drh9cbf3422008-01-17 16:22:13 +00003931case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00003932#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00003933 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00003934 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00003935#endif
drhd1d38482008-10-07 23:46:38 +00003936 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00003937 /* Fall through into OP_Rewind */
3938}
drh9cbf3422008-01-17 16:22:13 +00003939/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003940**
drhf0863fe2005-06-12 21:35:51 +00003941** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003942** will refer to the first entry in the database table or index.
3943** If the table or index is empty and P2>0, then jump immediately to P2.
3944** If P2 is 0 or if the table or index is not empty, fall through
3945** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003946*/
drh9cbf3422008-01-17 16:22:13 +00003947case OP_Rewind: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +00003948 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003949 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003950 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003951 int res;
drh5e00f6c2001-09-13 13:46:56 +00003952
drh70ce3f02003-04-15 19:22:22 +00003953 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003954 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003955 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003956 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003957 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00003958 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00003959 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003960 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00003961 pC->rowidIsValid = 0;
drh70ce3f02003-04-15 19:22:22 +00003962 }else{
drhf4dada72004-05-11 09:57:35 +00003963 res = 1;
3964 }
drh9c1905f2008-12-10 22:32:56 +00003965 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00003966 assert( pOp->p2>0 && pOp->p2<p->nOp );
3967 if( res ){
drhf4dada72004-05-11 09:57:35 +00003968 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003969 }
3970 break;
3971}
3972
drh9cbf3422008-01-17 16:22:13 +00003973/* Opcode: Next P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003974**
3975** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003976** table or index. If there are no more key/value pairs then fall through
3977** to the following instruction. But if the cursor advance was successful,
3978** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003979**
drh60a713c2008-01-21 16:22:45 +00003980** The P1 cursor must be for a real table, not a pseudo-table.
3981**
drhc045ec52002-12-04 20:01:06 +00003982** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003983*/
drh9cbf3422008-01-17 16:22:13 +00003984/* Opcode: Prev P1 P2 * * *
drhc045ec52002-12-04 20:01:06 +00003985**
3986** Back up cursor P1 so that it points to the previous key/data pair in its
3987** table or index. If there is no previous key/value pairs then fall through
3988** to the following instruction. But if the cursor backup was successful,
3989** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00003990**
3991** The P1 cursor must be for a real table, not a pseudo-table.
drhc045ec52002-12-04 20:01:06 +00003992*/
drh9cbf3422008-01-17 16:22:13 +00003993case OP_Prev: /* jump */
3994case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00003995 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00003996 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00003997 int res;
drh8721ce42001-11-07 14:22:00 +00003998
drhcaec2f12003-01-07 02:47:47 +00003999 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004000 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00004001 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004002 if( pC==0 ){
4003 break; /* See ticket #2273 */
4004 }
drh60a713c2008-01-21 16:22:45 +00004005 pCrsr = pC->pCursor;
4006 assert( pCrsr );
drha3460582008-07-11 21:02:53 +00004007 res = 1;
4008 assert( pC->deferredMoveto==0 );
4009 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
4010 sqlite3BtreePrevious(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004011 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004012 pC->cacheStatus = CACHE_STALE;
4013 if( res==0 ){
4014 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004015 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004016#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004017 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004018#endif
drh8721ce42001-11-07 14:22:00 +00004019 }
drhf0863fe2005-06-12 21:35:51 +00004020 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004021 break;
4022}
4023
drh9cbf3422008-01-17 16:22:13 +00004024/* Opcode: IdxInsert P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004025**
drhaa9b8962008-01-08 02:57:55 +00004026** Register P2 holds a SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004027** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004028** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004029**
drhaa9b8962008-01-08 02:57:55 +00004030** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004031** insert is likely to be an append.
4032**
drhf0863fe2005-06-12 21:35:51 +00004033** This instruction only works for indices. The equivalent instruction
4034** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004035*/
drh9cbf3422008-01-17 16:22:13 +00004036case OP_IdxInsert: { /* in2 */
drh5e00f6c2001-09-13 13:46:56 +00004037 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004038 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004039 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00004040 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004041 assert( p->apCsr[i]!=0 );
drhaa9b8962008-01-08 02:57:55 +00004042 assert( pIn2->flags & MEM_Blob );
drhd7556d22004-05-14 21:59:40 +00004043 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drhf0863fe2005-06-12 21:35:51 +00004044 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004045 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004046 if( rc==SQLITE_OK ){
drhaa9b8962008-01-08 02:57:55 +00004047 int nKey = pIn2->n;
4048 const char *zKey = pIn2->z;
4049 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3);
danielk1977d908f5a2007-05-11 07:08:28 +00004050 assert( pC->deferredMoveto==0 );
4051 pC->cacheStatus = CACHE_STALE;
4052 }
drh5e00f6c2001-09-13 13:46:56 +00004053 }
drh5e00f6c2001-09-13 13:46:56 +00004054 break;
4055}
4056
drhd1d38482008-10-07 23:46:38 +00004057/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004058**
drhe14006d2008-03-25 17:23:32 +00004059** The content of P3 registers starting at register P2 form
4060** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004061** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004062*/
drhe14006d2008-03-25 17:23:32 +00004063case OP_IdxDelete: {
drh5e00f6c2001-09-13 13:46:56 +00004064 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004065 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004066 BtCursor *pCrsr;
drhe14006d2008-03-25 17:23:32 +00004067 assert( pOp->p3>0 );
4068 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem );
drh6810ce62004-01-31 19:22:56 +00004069 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004070 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00004071 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197775bab7d2006-01-23 13:09:45 +00004072 int res;
drhe14006d2008-03-25 17:23:32 +00004073 UnpackedRecord r;
4074 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004075 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004076 r.flags = 0;
drhe14006d2008-03-25 17:23:32 +00004077 r.aMem = &p->aMem[pOp->p2];
drhe63d9992008-08-13 19:11:48 +00004078 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004079 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004080 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004081 }
drh9188b382004-05-14 21:12:22 +00004082 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004083 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004084 }
drh5e00f6c2001-09-13 13:46:56 +00004085 break;
4086}
4087
drh2133d822008-01-03 18:44:59 +00004088/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004089**
drh2133d822008-01-03 18:44:59 +00004090** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004091** the end of the index key pointed to by cursor P1. This integer should be
4092** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004093**
drh9437bd22009-02-01 00:29:56 +00004094** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004095*/
drh4c583122008-01-04 22:01:03 +00004096case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004097 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00004098 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004099 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004100
drh6810ce62004-01-31 19:22:56 +00004101 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004102 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00004103 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00004104 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00004105
drhd7556d22004-05-14 21:59:40 +00004106 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004107 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004108 if( !pC->nullRow ){
drhb21c8cd2007-08-21 19:33:56 +00004109 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004110 if( rc!=SQLITE_OK ){
4111 goto abort_due_to_error;
4112 }
danielk1977a7a8e142008-02-13 18:25:27 +00004113 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00004114 pOut->u.i = rowid;
danielk19773d1bfea2004-05-14 11:00:53 +00004115 }
drh8721ce42001-11-07 14:22:00 +00004116 }
4117 break;
4118}
4119
danielk197761dd5832008-04-18 11:31:12 +00004120/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004121**
danielk197761dd5832008-04-18 11:31:12 +00004122** The P4 register values beginning with P3 form an unpacked index
4123** key that omits the ROWID. Compare this key value against the index
4124** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004125**
danielk197761dd5832008-04-18 11:31:12 +00004126** If the P1 index entry is greater than or equal to the key value
4127** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004128**
danielk197761dd5832008-04-18 11:31:12 +00004129** If P5 is non-zero then the key value is increased by an epsilon
4130** prior to the comparison. This make the opcode work like IdxGT except
4131** that if the key from register P3 is a prefix of the key in the cursor,
4132** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004133*/
drh98757152008-01-09 23:04:12 +00004134/* Opcode: IdxLT P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00004135**
danielk197761dd5832008-04-18 11:31:12 +00004136** The P4 register values beginning with P3 form an unpacked index
4137** key that omits the ROWID. Compare this key value against the index
4138** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004139**
danielk197761dd5832008-04-18 11:31:12 +00004140** If the P1 index entry is less than the key value then jump to P2.
4141** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004142**
danielk197761dd5832008-04-18 11:31:12 +00004143** If P5 is non-zero then the key value is increased by an epsilon prior
4144** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004145*/
drh9cbf3422008-01-17 16:22:13 +00004146case OP_IdxLT: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004147case OP_IdxGE: { /* jump, in3 */
drh8721ce42001-11-07 14:22:00 +00004148 int i= pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004149 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004150
drh6810ce62004-01-31 19:22:56 +00004151 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004152 assert( p->apCsr[i]!=0 );
drh4f26bb62005-09-08 14:17:20 +00004153 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh0850b532006-01-31 19:31:43 +00004154 int res;
danielk197761dd5832008-04-18 11:31:12 +00004155 UnpackedRecord r;
drhd7556d22004-05-14 21:59:40 +00004156 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004157 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004158 assert( pOp->p4type==P4_INT32 );
4159 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004160 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004161 if( pOp->p5 ){
4162 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4163 }else{
4164 r.flags = UNPACKED_IGNORE_ROWID;
4165 }
danielk197761dd5832008-04-18 11:31:12 +00004166 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00004167 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004168 if( pOp->opcode==OP_IdxLT ){
4169 res = -res;
drha05a7222008-01-19 03:35:58 +00004170 }else{
4171 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004172 res++;
4173 }
4174 if( res>0 ){
4175 pc = pOp->p2 - 1 ;
4176 }
4177 }
4178 break;
4179}
4180
drh98757152008-01-09 23:04:12 +00004181/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004182**
4183** Delete an entire database table or index whose root page in the database
4184** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004185**
drh98757152008-01-09 23:04:12 +00004186** The table being destroyed is in the main database file if P3==0. If
4187** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004188** that is used to store tables create using CREATE TEMPORARY TABLE.
4189**
drh205f48e2004-11-05 00:43:11 +00004190** If AUTOVACUUM is enabled then it is possible that another root page
4191** might be moved into the newly deleted root page in order to keep all
4192** root pages contiguous at the beginning of the database. The former
4193** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004194** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004195** movement was required (because the table being dropped was already
4196** the last one in the database) then a zero is stored in register P2.
4197** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004198**
drhb19a2bc2001-09-16 00:13:26 +00004199** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004200*/
drh98757152008-01-09 23:04:12 +00004201case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004202 int iMoved;
drh3765df42006-06-28 18:18:09 +00004203 int iCnt;
danielk1977212b2182006-06-23 14:32:08 +00004204#ifndef SQLITE_OMIT_VIRTUALTABLE
drh5a91a532007-01-05 16:39:43 +00004205 Vdbe *pVdbe;
danielk1977212b2182006-06-23 14:32:08 +00004206 iCnt = 0;
4207 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
4208 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4209 iCnt++;
4210 }
4211 }
drh3765df42006-06-28 18:18:09 +00004212#else
4213 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004214#endif
4215 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004216 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004217 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004218 }else{
drh98757152008-01-09 23:04:12 +00004219 int iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004220 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004221 assert( (p->btreeMask & (1<<iDb))!=0 );
4222 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
danielk1977a7a8e142008-02-13 18:25:27 +00004223 MemSetTypeFlag(pOut, MEM_Int);
drh98757152008-01-09 23:04:12 +00004224 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004225#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004226 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004227 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
danielk1977e6efa742004-11-10 11:55:10 +00004228 }
drh3765df42006-06-28 18:18:09 +00004229#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004230 }
drh5e00f6c2001-09-13 13:46:56 +00004231 break;
4232}
4233
danielk1977c7af4842008-10-27 13:59:33 +00004234/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004235**
4236** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004237** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004238** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004239**
drhf57b3392001-10-08 13:22:32 +00004240** The table being clear is in the main database file if P2==0. If
4241** P2==1 then the table to be clear is in the auxiliary database file
4242** that is used to store tables create using CREATE TEMPORARY TABLE.
4243**
danielk1977c7af4842008-10-27 13:59:33 +00004244** If the P3 value is non-zero, then the table refered to must be an
4245** intkey table (an SQL table, not an index). In this case the row change
4246** count is incremented by the number of rows in the table being cleared.
4247** If P3 is greater than zero, then the value stored in register P3 is
4248** also incremented by the number of rows in the table being cleared.
4249**
drhb19a2bc2001-09-16 00:13:26 +00004250** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004251*/
drh9cbf3422008-01-17 16:22:13 +00004252case OP_Clear: {
danielk1977c7af4842008-10-27 13:59:33 +00004253 int nChange = 0;
drhfb982642007-08-30 01:19:59 +00004254 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004255 rc = sqlite3BtreeClearTable(
4256 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4257 );
4258 if( pOp->p3 ){
4259 p->nChange += nChange;
4260 if( pOp->p3>0 ){
4261 p->aMem[pOp->p3].u.i += nChange;
4262 }
4263 }
drh5edc3122001-09-13 21:53:09 +00004264 break;
4265}
4266
drh4c583122008-01-04 22:01:03 +00004267/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004268**
drh4c583122008-01-04 22:01:03 +00004269** Allocate a new table in the main database file if P1==0 or in the
4270** auxiliary database file if P1==1 or in an attached database if
4271** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004272** register P2
drh5b2fd562001-09-13 15:21:31 +00004273**
drhc6b52df2002-01-04 03:09:29 +00004274** The difference between a table and an index is this: A table must
4275** have a 4-byte integer key and can have arbitrary data. An index
4276** has an arbitrary key but no data.
4277**
drhb19a2bc2001-09-16 00:13:26 +00004278** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004279*/
drh4c583122008-01-04 22:01:03 +00004280/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004281**
drh4c583122008-01-04 22:01:03 +00004282** Allocate a new index in the main database file if P1==0 or in the
4283** auxiliary database file if P1==1 or in an attached database if
4284** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004285** register P2.
drhf57b3392001-10-08 13:22:32 +00004286**
drhc6b52df2002-01-04 03:09:29 +00004287** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004288*/
drh4c583122008-01-04 22:01:03 +00004289case OP_CreateIndex: /* out2-prerelease */
4290case OP_CreateTable: { /* out2-prerelease */
drh88a003e2008-12-11 16:17:03 +00004291 int pgno = 0;
drhf328bc82004-05-10 23:29:49 +00004292 int flags;
drh234c39d2004-07-24 03:30:47 +00004293 Db *pDb;
4294 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004295 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004296 pDb = &db->aDb[pOp->p1];
4297 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004298 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004299 /* flags = BTREE_INTKEY; */
4300 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004301 }else{
drhf328bc82004-05-10 23:29:49 +00004302 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00004303 }
drh234c39d2004-07-24 03:30:47 +00004304 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004305 pOut->u.i = pgno;
4306 MemSetTypeFlag(pOut, MEM_Int);
drh5b2fd562001-09-13 15:21:31 +00004307 break;
4308}
4309
drh98757152008-01-09 23:04:12 +00004310/* Opcode: ParseSchema P1 P2 * P4 *
drh234c39d2004-07-24 03:30:47 +00004311**
4312** Read and parse all entries from the SQLITE_MASTER table of database P1
drh66a51672008-01-03 00:01:23 +00004313** that match the WHERE clause P4. P2 is the "force" flag. Always do
drh3c23a882007-01-09 14:01:13 +00004314** the parsing if P2 is true. If P2 is false, then this routine is a
4315** no-op if the schema is not currently loaded. In other words, if P2
4316** is false, the SQLITE_MASTER table is only parsed if the rest of the
4317** schema is already loaded into the symbol table.
drh234c39d2004-07-24 03:30:47 +00004318**
4319** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004320** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004321*/
drh9cbf3422008-01-17 16:22:13 +00004322case OP_ParseSchema: {
drh234c39d2004-07-24 03:30:47 +00004323 char *zSql;
4324 int iDb = pOp->p1;
4325 const char *zMaster;
4326 InitData initData;
4327
4328 assert( iDb>=0 && iDb<db->nDb );
drh3c23a882007-01-09 14:01:13 +00004329 if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
4330 break;
4331 }
danielk197753c0f742005-03-29 03:10:59 +00004332 zMaster = SCHEMA_TABLE(iDb);
drh234c39d2004-07-24 03:30:47 +00004333 initData.db = db;
drhece3c722006-09-23 20:36:01 +00004334 initData.iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004335 initData.pzErrMsg = &p->zErrMsg;
danielk19771e536952007-08-16 10:09:01 +00004336 zSql = sqlite3MPrintf(db,
drhece3c722006-09-23 20:36:01 +00004337 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
danielk19772dca4ac2008-01-03 11:50:29 +00004338 db->aDb[iDb].zName, zMaster, pOp->p4.z);
drh71c697e2004-08-08 23:39:19 +00004339 if( zSql==0 ) goto no_mem;
drh7e8b8482008-01-23 03:03:05 +00004340 (void)sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +00004341 assert( db->init.busy==0 );
4342 db->init.busy = 1;
drhc456e572008-08-11 18:44:58 +00004343 initData.rc = SQLITE_OK;
drh17435752007-08-16 04:30:38 +00004344 assert( !db->mallocFailed );
drh234c39d2004-07-24 03:30:47 +00004345 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhc456e572008-08-11 18:44:58 +00004346 if( rc==SQLITE_OK ) rc = initData.rc;
drh633e6d52008-07-28 19:34:53 +00004347 sqlite3DbFree(db, zSql);
drh234c39d2004-07-24 03:30:47 +00004348 db->init.busy = 0;
drh7e8b8482008-01-23 03:03:05 +00004349 (void)sqlite3SafetyOn(db);
danielk1977261919c2005-12-06 12:52:59 +00004350 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004351 goto no_mem;
4352 }
drh234c39d2004-07-24 03:30:47 +00004353 break;
4354}
4355
drhcfed7bc2006-03-13 14:28:05 +00004356#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
drh98757152008-01-09 23:04:12 +00004357/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004358**
4359** Read the sqlite_stat1 table for database P1 and load the content
4360** of that table into the internal index hash table. This will cause
4361** the analysis to be used when preparing all subsequent queries.
4362*/
drh9cbf3422008-01-17 16:22:13 +00004363case OP_LoadAnalysis: {
drh497e4462005-07-23 03:18:40 +00004364 int iDb = pOp->p1;
4365 assert( iDb>=0 && iDb<db->nDb );
drhcf1be452007-05-12 12:08:51 +00004366 rc = sqlite3AnalysisLoad(db, iDb);
drh497e4462005-07-23 03:18:40 +00004367 break;
4368}
drhcfed7bc2006-03-13 14:28:05 +00004369#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
drh497e4462005-07-23 03:18:40 +00004370
drh98757152008-01-09 23:04:12 +00004371/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004372**
4373** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004374** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004375** is dropped in order to keep the internal representation of the
4376** schema consistent with what is on disk.
4377*/
drh9cbf3422008-01-17 16:22:13 +00004378case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004379 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004380 break;
4381}
4382
drh98757152008-01-09 23:04:12 +00004383/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004384**
4385** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004386** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004387** is dropped in order to keep the internal representation of the
4388** schema consistent with what is on disk.
4389*/
drh9cbf3422008-01-17 16:22:13 +00004390case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004391 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004392 break;
4393}
4394
drh98757152008-01-09 23:04:12 +00004395/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004396**
4397** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004398** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004399** is dropped in order to keep the internal representation of the
4400** schema consistent with what is on disk.
4401*/
drh9cbf3422008-01-17 16:22:13 +00004402case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004403 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004404 break;
4405}
4406
drh234c39d2004-07-24 03:30:47 +00004407
drhb7f91642004-10-31 02:22:47 +00004408#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004409/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004410**
drh98757152008-01-09 23:04:12 +00004411** Do an analysis of the currently open database. Store in
4412** register P1 the text of an error message describing any problems.
4413** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004414**
drh98757152008-01-09 23:04:12 +00004415** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004416** At most reg(P3) errors will be reported.
4417** In other words, the analysis stops as soon as reg(P1) errors are
4418** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004419**
drh79069752004-05-22 21:30:40 +00004420** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004421** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004422** total.
drh21504322002-06-25 13:16:02 +00004423**
drh98757152008-01-09 23:04:12 +00004424** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004425** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004426**
drh1dcdbc02007-01-27 02:24:54 +00004427** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004428*/
drhaaab5722002-02-19 13:39:21 +00004429case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004430 int nRoot; /* Number of tables to check. (Number of root pages.) */
4431 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4432 int j; /* Loop counter */
4433 int nErr; /* Number of errors reported */
4434 char *z; /* Text of the error report */
4435 Mem *pnErr; /* Register keeping track of errors remaining */
4436
4437 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004438 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004439 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004440 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004441 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4442 pnErr = &p->aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004443 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004444 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
4445 pIn1 = &p->aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004446 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004447 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004448 }
4449 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004450 assert( pOp->p5<db->nDb );
4451 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4452 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004453 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004454 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004455 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004456 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004457 if( nErr==0 ){
4458 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004459 }else if( z==0 ){
4460 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004461 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004462 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004463 }
drhb7654112008-01-12 12:48:07 +00004464 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004465 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004466 break;
4467}
drhb7f91642004-10-31 02:22:47 +00004468#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004469
drh3d4501e2008-12-04 20:40:10 +00004470/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004471**
drh3d4501e2008-12-04 20:40:10 +00004472** Insert the integer value held by register P2 into a boolean index
4473** held in register P1.
4474**
4475** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004476*/
drh3d4501e2008-12-04 20:40:10 +00004477case OP_RowSetAdd: { /* in2 */
4478 Mem *pIdx;
4479 Mem *pVal;
4480 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4481 pIdx = &p->aMem[pOp->p1];
4482 assert( pOp->p2>0 && pOp->p2<=p->nMem );
4483 pVal = &p->aMem[pOp->p2];
4484 assert( (pVal->flags & MEM_Int)!=0 );
4485 if( (pIdx->flags & MEM_RowSet)==0 ){
4486 sqlite3VdbeMemSetRowSet(pIdx);
drh8d993632008-12-04 22:17:55 +00004487 if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004488 }
4489 sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i);
4490 break;
4491}
4492
4493/* Opcode: RowSetRead P1 P2 P3 * *
4494**
4495** Extract the smallest value from boolean index P1 and put that value into
4496** register P3. Or, if boolean index P1 is initially empty, leave P3
4497** unchanged and jump to instruction P2.
4498*/
4499case OP_RowSetRead: { /* jump, out3 */
4500 Mem *pIdx;
4501 i64 val;
4502 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4503 CHECK_FOR_INTERRUPT;
4504 pIdx = &p->aMem[pOp->p1];
drhdd5f5a62008-12-23 13:35:23 +00004505 pOut = &p->aMem[pOp->p3];
drh3d4501e2008-12-04 20:40:10 +00004506 if( (pIdx->flags & MEM_RowSet)==0
4507 || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0
4508 ){
4509 /* The boolean index is empty */
4510 sqlite3VdbeMemSetNull(pIdx);
4511 pc = pOp->p2 - 1;
4512 }else{
4513 /* A value was pulled from the index */
4514 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh3d4501e2008-12-04 20:40:10 +00004515 sqlite3VdbeMemSetInt64(pOut, val);
drh17435752007-08-16 04:30:38 +00004516 }
drh5e00f6c2001-09-13 13:46:56 +00004517 break;
4518}
4519
drh5e00f6c2001-09-13 13:46:56 +00004520
danielk197793758c82005-01-21 08:13:14 +00004521#ifndef SQLITE_OMIT_TRIGGER
rdcb0c374f2004-02-20 22:53:38 +00004522/* Opcode: ContextPush * * *
4523**
4524** Save the current Vdbe context such that it can be restored by a ContextPop
4525** opcode. The context stores the last insert row id, the last statement change
4526** count, and the current statement change count.
4527*/
drh9cbf3422008-01-17 16:22:13 +00004528case OP_ContextPush: {
drh344737f2004-09-19 00:50:20 +00004529 int i = p->contextStackTop++;
4530 Context *pContext;
danielk1977b28af712004-06-21 06:50:26 +00004531
drh344737f2004-09-19 00:50:20 +00004532 assert( i>=0 );
danielk1977b28af712004-06-21 06:50:26 +00004533 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
drh344737f2004-09-19 00:50:20 +00004534 if( i>=p->contextStackDepth ){
4535 p->contextStackDepth = i+1;
danielk19771e536952007-08-16 10:09:01 +00004536 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
drhcf643722007-03-27 13:36:37 +00004537 sizeof(Context)*(i+1));
drh344737f2004-09-19 00:50:20 +00004538 if( p->contextStack==0 ) goto no_mem;
4539 }
4540 pContext = &p->contextStack[i];
4541 pContext->lastRowid = db->lastRowid;
4542 pContext->nChange = p->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004543 break;
4544}
4545
4546/* Opcode: ContextPop * * *
4547**
4548** Restore the Vdbe context to the state it was in when contextPush was last
4549** executed. The context stores the last insert row id, the last statement
4550** change count, and the current statement change count.
4551*/
drh9cbf3422008-01-17 16:22:13 +00004552case OP_ContextPop: {
drh344737f2004-09-19 00:50:20 +00004553 Context *pContext = &p->contextStack[--p->contextStackTop];
4554 assert( p->contextStackTop>=0 );
4555 db->lastRowid = pContext->lastRowid;
4556 p->nChange = pContext->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004557 break;
4558}
danielk197793758c82005-01-21 08:13:14 +00004559#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004560
drh205f48e2004-11-05 00:43:11 +00004561#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00004562/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00004563**
drh98757152008-01-09 23:04:12 +00004564** Set the value of register P1 to the maximum of its current value
4565** and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00004566**
4567** This instruction throws an error if the memory cell is not initially
4568** an integer.
4569*/
drh9cbf3422008-01-17 16:22:13 +00004570case OP_MemMax: { /* in1, in2 */
drh98757152008-01-09 23:04:12 +00004571 sqlite3VdbeMemIntegerify(pIn1);
4572 sqlite3VdbeMemIntegerify(pIn2);
4573 if( pIn1->u.i<pIn2->u.i){
4574 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00004575 }
4576 break;
4577}
4578#endif /* SQLITE_OMIT_AUTOINCREMENT */
4579
drh98757152008-01-09 23:04:12 +00004580/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00004581**
drh98757152008-01-09 23:04:12 +00004582** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004583**
drh98757152008-01-09 23:04:12 +00004584** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004585** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00004586*/
drh9cbf3422008-01-17 16:22:13 +00004587case OP_IfPos: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004588 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004589 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00004590 pc = pOp->p2 - 1;
4591 }
4592 break;
4593}
4594
drh98757152008-01-09 23:04:12 +00004595/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00004596**
drh98757152008-01-09 23:04:12 +00004597** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00004598**
drh98757152008-01-09 23:04:12 +00004599** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00004600** not contain an integer. An assertion fault will result if you try.
4601*/
drh9cbf3422008-01-17 16:22:13 +00004602case OP_IfNeg: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004603 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004604 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00004605 pc = pOp->p2 - 1;
4606 }
4607 break;
4608}
4609
drh98757152008-01-09 23:04:12 +00004610/* Opcode: IfZero P1 P2 * * *
drhec7429a2005-10-06 16:53:14 +00004611**
drh98757152008-01-09 23:04:12 +00004612** If the value of register P1 is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004613**
drh98757152008-01-09 23:04:12 +00004614** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004615** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00004616*/
drh9cbf3422008-01-17 16:22:13 +00004617case OP_IfZero: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004618 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004619 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00004620 pc = pOp->p2 - 1;
4621 }
4622 break;
4623}
4624
drh98757152008-01-09 23:04:12 +00004625/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00004626**
drh0bce8352002-02-28 00:41:10 +00004627** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00004628** function has P5 arguments. P4 is a pointer to the FuncDef
4629** structure that specifies the function. Use register
4630** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00004631**
drh98757152008-01-09 23:04:12 +00004632** The P5 arguments are taken from register P2 and its
4633** successors.
drhe5095352002-02-24 03:25:14 +00004634*/
drh9cbf3422008-01-17 16:22:13 +00004635case OP_AggStep: {
drh98757152008-01-09 23:04:12 +00004636 int n = pOp->p5;
drhe5095352002-02-24 03:25:14 +00004637 int i;
drh6810ce62004-01-31 19:22:56 +00004638 Mem *pMem, *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004639 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004640 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004641
drh6810ce62004-01-31 19:22:56 +00004642 assert( n>=0 );
drh98757152008-01-09 23:04:12 +00004643 pRec = &p->aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00004644 apVal = p->apArg;
4645 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00004646 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004647 apVal[i] = pRec;
drh8079a0d2006-01-12 17:20:50 +00004648 storeTypeInfo(pRec, encoding);
drhe5095352002-02-24 03:25:14 +00004649 }
danielk19772dca4ac2008-01-03 11:50:29 +00004650 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00004651 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4652 ctx.pMem = pMem = &p->aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00004653 pMem->n++;
drh90669c12006-01-20 15:45:36 +00004654 ctx.s.flags = MEM_Null;
4655 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00004656 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00004657 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00004658 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00004659 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00004660 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00004661 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00004662 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00004663 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00004664 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00004665 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00004666 }
danielk19776ddcca52004-05-24 23:48:25 +00004667 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00004668 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00004669 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00004670 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00004671 }
drh90669c12006-01-20 15:45:36 +00004672 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00004673 break;
4674}
4675
drh98757152008-01-09 23:04:12 +00004676/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004677**
drh13449892005-09-07 21:22:45 +00004678** Execute the finalizer function for an aggregate. P1 is
4679** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00004680**
4681** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00004682** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00004683** argument is not used by this opcode. It is only there to disambiguate
4684** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00004685** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00004686** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00004687*/
drh9cbf3422008-01-17 16:22:13 +00004688case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00004689 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00004690 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh13449892005-09-07 21:22:45 +00004691 pMem = &p->aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00004692 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00004693 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh90669c12006-01-20 15:45:36 +00004694 if( rc==SQLITE_ERROR ){
drhf089aa42008-07-08 19:34:06 +00004695 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00004696 }
drh2dca8682008-03-21 17:13:13 +00004697 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00004698 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00004699 if( sqlite3VdbeMemTooBig(pMem) ){
4700 goto too_big;
4701 }
drh5e00f6c2001-09-13 13:46:56 +00004702 break;
4703}
4704
drh5e00f6c2001-09-13 13:46:56 +00004705
drhfdbcdee2007-03-27 14:44:50 +00004706#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00004707/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00004708**
4709** Vacuum the entire database. This opcode will cause other virtual
4710** machines to be created and run. It may not be called from within
4711** a transaction.
4712*/
drh9cbf3422008-01-17 16:22:13 +00004713case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004714 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4715 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4716 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004717 break;
4718}
drh154d4b22006-09-21 11:02:16 +00004719#endif
drh6f8c91c2003-12-07 00:24:35 +00004720
danielk1977dddbcdc2007-04-26 14:42:34 +00004721#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00004722/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00004723**
4724** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00004725** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00004726** P2. Otherwise, fall through to the next instruction.
4727*/
drh9cbf3422008-01-17 16:22:13 +00004728case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00004729 Btree *pBt;
4730
4731 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004732 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00004733 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00004734 rc = sqlite3BtreeIncrVacuum(pBt);
4735 if( rc==SQLITE_DONE ){
4736 pc = pOp->p2 - 1;
4737 rc = SQLITE_OK;
4738 }
4739 break;
4740}
4741#endif
4742
drh98757152008-01-09 23:04:12 +00004743/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00004744**
4745** Cause precompiled statements to become expired. An expired statement
4746** fails with an error code of SQLITE_SCHEMA if it is ever executed
4747** (via sqlite3_step()).
4748**
4749** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4750** then only the currently executing statement is affected.
4751*/
drh9cbf3422008-01-17 16:22:13 +00004752case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00004753 if( !pOp->p1 ){
4754 sqlite3ExpirePreparedStatements(db);
4755 }else{
4756 p->expired = 1;
4757 }
4758 break;
4759}
4760
danielk1977c00da102006-01-07 13:21:04 +00004761#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00004762/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00004763**
4764** Obtain a lock on a particular table. This instruction is only used when
4765** the shared-cache feature is enabled.
4766**
drh6a9ad3d2008-04-02 16:29:30 +00004767** If P1 is the index of the database in sqlite3.aDb[] of the database
4768** on which the lock is acquired. A readlock is obtained if P3==0 or
4769** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00004770**
4771** P2 contains the root-page of the table to lock.
4772**
drh66a51672008-01-03 00:01:23 +00004773** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00004774** used to generate an error message if the lock cannot be obtained.
4775*/
drh9cbf3422008-01-17 16:22:13 +00004776case OP_TableLock: {
danielk1977c00da102006-01-07 13:21:04 +00004777 int p1 = pOp->p1;
drh9c1905f2008-12-10 22:32:56 +00004778 u8 isWriteLock = (u8)pOp->p3;
drhfb982642007-08-30 01:19:59 +00004779 assert( p1>=0 && p1<db->nDb );
4780 assert( (p->btreeMask & (1<<p1))!=0 );
drh6a9ad3d2008-04-02 16:29:30 +00004781 assert( isWriteLock==0 || isWriteLock==1 );
danielk1977c00da102006-01-07 13:21:04 +00004782 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4783 if( rc==SQLITE_LOCKED ){
danielk19772dca4ac2008-01-03 11:50:29 +00004784 const char *z = pOp->p4.z;
drhf089aa42008-07-08 19:34:06 +00004785 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
danielk1977c00da102006-01-07 13:21:04 +00004786 }
4787 break;
4788}
drhb9bb7c12006-06-11 23:41:55 +00004789#endif /* SQLITE_OMIT_SHARED_CACHE */
4790
4791#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004792/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004793**
danielk19773e3a84d2008-08-01 17:37:40 +00004794** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
4795** xBegin method for that table.
4796**
4797** Also, whether or not P4 is set, check that this is not being called from
4798** within a callback to a virtual table xSync() method. If it is, set the
4799** error code to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00004800*/
drh9cbf3422008-01-17 16:22:13 +00004801case OP_VBegin: {
danielk19773e3a84d2008-08-01 17:37:40 +00004802 sqlite3_vtab *pVtab = pOp->p4.pVtab;
4803 rc = sqlite3VtabBegin(db, pVtab);
4804 if( pVtab ){
4805 sqlite3DbFree(db, p->zErrMsg);
4806 p->zErrMsg = pVtab->zErrMsg;
4807 pVtab->zErrMsg = 0;
4808 }
danielk1977f9e7dda2006-06-16 16:08:53 +00004809 break;
4810}
4811#endif /* SQLITE_OMIT_VIRTUALTABLE */
4812
4813#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004814/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00004815**
drh66a51672008-01-03 00:01:23 +00004816** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00004817** for that table.
4818*/
drh9cbf3422008-01-17 16:22:13 +00004819case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00004820 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00004821 break;
4822}
4823#endif /* SQLITE_OMIT_VIRTUALTABLE */
4824
4825#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004826/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004827**
drh66a51672008-01-03 00:01:23 +00004828** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00004829** of that table.
drhb9bb7c12006-06-11 23:41:55 +00004830*/
drh9cbf3422008-01-17 16:22:13 +00004831case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00004832 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00004833 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00004834 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00004835 break;
4836}
4837#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00004838
drh9eff6162006-06-12 21:59:13 +00004839#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004840/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00004841**
drh66a51672008-01-03 00:01:23 +00004842** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00004843** P1 is a cursor number. This opcode opens a cursor to the virtual
4844** table and stores that cursor in P1.
4845*/
drh9cbf3422008-01-17 16:22:13 +00004846case OP_VOpen: {
drhdfe88ec2008-11-03 20:55:06 +00004847 VdbeCursor *pCur = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004848 sqlite3_vtab_cursor *pVtabCursor = 0;
4849
danielk19772dca4ac2008-01-03 11:50:29 +00004850 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004851 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4852
4853 assert(pVtab && pModule);
4854 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4855 rc = pModule->xOpen(pVtab, &pVtabCursor);
drh633e6d52008-07-28 19:34:53 +00004856 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00004857 p->zErrMsg = pVtab->zErrMsg;
4858 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004859 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4860 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00004861 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004862 pVtabCursor->pVtab = pVtab;
4863
4864 /* Initialise vdbe cursor object */
danielk1977cd3e8f72008-03-25 09:47:35 +00004865 pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0);
danielk1977be718892006-06-23 08:05:19 +00004866 if( pCur ){
4867 pCur->pVtabCursor = pVtabCursor;
4868 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004869 }else{
drh17435752007-08-16 04:30:38 +00004870 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004871 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00004872 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004873 }
drh9eff6162006-06-12 21:59:13 +00004874 break;
4875}
4876#endif /* SQLITE_OMIT_VIRTUALTABLE */
4877
4878#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00004879/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00004880**
4881** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4882** the filtered result set is empty.
4883**
drh66a51672008-01-03 00:01:23 +00004884** P4 is either NULL or a string that was generated by the xBestIndex
4885** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00004886** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00004887**
drh9eff6162006-06-12 21:59:13 +00004888** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00004889** by P1. The integer query plan parameter to xFilter is stored in register
4890** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00004891** xFilter method. Registers P3+2..P3+1+argc are the argc
4892** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00004893** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00004894**
danielk19776dbee812008-01-03 18:39:41 +00004895** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00004896*/
drh9cbf3422008-01-17 16:22:13 +00004897case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004898 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00004899 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004900 const sqlite3_module *pModule;
danielk19776dbee812008-01-03 18:39:41 +00004901 Mem *pQuery = &p->aMem[pOp->p3];
4902 Mem *pArgc = &pQuery[1];
drh4dc754d2008-07-23 18:17:32 +00004903 sqlite3_vtab_cursor *pVtabCursor;
4904 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004905
drhdfe88ec2008-11-03 20:55:06 +00004906 VdbeCursor *pCur = p->apCsr[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00004907
4908 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004909 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00004910 pVtabCursor = pCur->pVtabCursor;
4911 pVtab = pVtabCursor->pVtab;
4912 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004913
drh9cbf3422008-01-17 16:22:13 +00004914 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00004915 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00004916 nArg = (int)pArgc->u.i;
4917 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004918
drh644a5292006-12-20 14:53:38 +00004919 /* Invoke the xFilter method */
4920 {
drh3f87d2a2006-12-20 14:31:24 +00004921 int res = 0;
drh4be8b512006-06-13 23:51:34 +00004922 int i;
4923 Mem **apArg = p->apArg;
4924 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00004925 apArg[i] = &pArgc[i+1];
drh4be8b512006-06-13 23:51:34 +00004926 storeTypeInfo(apArg[i], 0);
danielk19775fac9f82006-06-13 14:16:58 +00004927 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004928
4929 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00004930 sqlite3VtabLock(pVtab);
danielk1977be718892006-06-23 08:05:19 +00004931 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00004932 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00004933 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00004934 sqlite3DbFree(db, p->zErrMsg);
4935 p->zErrMsg = pVtab->zErrMsg;
4936 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00004937 sqlite3VtabUnlock(db, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00004938 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00004939 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00004940 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004941 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4942
danielk1977a298e902006-06-22 09:53:48 +00004943 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00004944 pc = pOp->p2 - 1;
4945 }
4946 }
drh1d454a32008-01-31 19:34:51 +00004947 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004948
drh9eff6162006-06-12 21:59:13 +00004949 break;
4950}
4951#endif /* SQLITE_OMIT_VIRTUALTABLE */
4952
4953#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004954/* Opcode: VRowid P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00004955**
drh2133d822008-01-03 18:44:59 +00004956** Store into register P2 the rowid of
drh9eff6162006-06-12 21:59:13 +00004957** the virtual-table that the P1 cursor is pointing to.
4958*/
drh4c583122008-01-04 22:01:03 +00004959case OP_VRowid: { /* out2-prerelease */
danielk19773e3a84d2008-08-01 17:37:40 +00004960 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004961 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004962 sqlite_int64 iRow;
drhdfe88ec2008-11-03 20:55:06 +00004963 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhde4fcfd2008-01-19 23:50:26 +00004964
danielk1977b7a7b9a2006-06-13 10:24:42 +00004965 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004966 if( pCur->nullRow ){
4967 break;
4968 }
danielk19773e3a84d2008-08-01 17:37:40 +00004969 pVtab = pCur->pVtabCursor->pVtab;
4970 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00004971 assert( pModule->xRowid );
4972 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4973 rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
danielk19773e3a84d2008-08-01 17:37:40 +00004974 sqlite3DbFree(db, p->zErrMsg);
4975 p->zErrMsg = pVtab->zErrMsg;
4976 pVtab->zErrMsg = 0;
drhde4fcfd2008-01-19 23:50:26 +00004977 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977a7a8e142008-02-13 18:25:27 +00004978 MemSetTypeFlag(pOut, MEM_Int);
drhde4fcfd2008-01-19 23:50:26 +00004979 pOut->u.i = iRow;
drh9eff6162006-06-12 21:59:13 +00004980 break;
4981}
4982#endif /* SQLITE_OMIT_VIRTUALTABLE */
4983
4984#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004985/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00004986**
drh2133d822008-01-03 18:44:59 +00004987** Store the value of the P2-th column of
4988** the row of the virtual-table that the
4989** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00004990*/
4991case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00004992 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004993 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004994 Mem *pDest;
4995 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004996
drhdfe88ec2008-11-03 20:55:06 +00004997 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00004998 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004999 assert( pOp->p3>0 && pOp->p3<=p->nMem );
5000 pDest = &p->aMem[pOp->p3];
5001 if( pCur->nullRow ){
5002 sqlite3VdbeMemSetNull(pDest);
5003 break;
5004 }
danielk19773e3a84d2008-08-01 17:37:40 +00005005 pVtab = pCur->pVtabCursor->pVtab;
5006 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005007 assert( pModule->xColumn );
5008 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005009
5010 /* The output cell may already have a buffer allocated. Move
5011 ** the current contents to sContext.s so in case the user-function
5012 ** can use the already allocated buffer instead of allocating a
5013 ** new one.
5014 */
5015 sqlite3VdbeMemMove(&sContext.s, pDest);
5016 MemSetTypeFlag(&sContext.s, MEM_Null);
5017
drhde4fcfd2008-01-19 23:50:26 +00005018 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5019 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
danielk19773e3a84d2008-08-01 17:37:40 +00005020 sqlite3DbFree(db, p->zErrMsg);
5021 p->zErrMsg = pVtab->zErrMsg;
5022 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005023
drhde4fcfd2008-01-19 23:50:26 +00005024 /* Copy the result of the function to the P3 register. We
5025 ** do this regardless of whether or not an error occured to ensure any
5026 ** dynamic allocation in sContext.s (a Mem struct) is released.
5027 */
5028 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005029 REGISTER_TRACE(pOp->p3, pDest);
5030 sqlite3VdbeMemMove(pDest, &sContext.s);
5031 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005032
drhde4fcfd2008-01-19 23:50:26 +00005033 if( sqlite3SafetyOn(db) ){
5034 goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005035 }
drhde4fcfd2008-01-19 23:50:26 +00005036 if( sqlite3VdbeMemTooBig(pDest) ){
5037 goto too_big;
5038 }
drh9eff6162006-06-12 21:59:13 +00005039 break;
5040}
5041#endif /* SQLITE_OMIT_VIRTUALTABLE */
5042
5043#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005044/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005045**
5046** Advance virtual table P1 to the next row in its result set and
5047** jump to instruction P2. Or, if the virtual table has reached
5048** the end of its result set, then fall through to the next instruction.
5049*/
drh9cbf3422008-01-17 16:22:13 +00005050case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005051 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005052 const sqlite3_module *pModule;
5053 int res = 0;
5054
drhdfe88ec2008-11-03 20:55:06 +00005055 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005056 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005057 if( pCur->nullRow ){
5058 break;
5059 }
danielk19773e3a84d2008-08-01 17:37:40 +00005060 pVtab = pCur->pVtabCursor->pVtab;
5061 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005062 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005063
drhde4fcfd2008-01-19 23:50:26 +00005064 /* Invoke the xNext() method of the module. There is no way for the
5065 ** underlying implementation to return an error if one occurs during
5066 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5067 ** data is available) and the error code returned when xColumn or
5068 ** some other method is next invoked on the save virtual table cursor.
5069 */
5070 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00005071 sqlite3VtabLock(pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005072 p->inVtabMethod = 1;
5073 rc = pModule->xNext(pCur->pVtabCursor);
5074 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005075 sqlite3DbFree(db, p->zErrMsg);
5076 p->zErrMsg = pVtab->zErrMsg;
5077 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00005078 sqlite3VtabUnlock(db, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005079 if( rc==SQLITE_OK ){
5080 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005081 }
drhde4fcfd2008-01-19 23:50:26 +00005082 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005083
drhde4fcfd2008-01-19 23:50:26 +00005084 if( !res ){
5085 /* If there is data, jump to P2 */
5086 pc = pOp->p2 - 1;
5087 }
drh9eff6162006-06-12 21:59:13 +00005088 break;
5089}
5090#endif /* SQLITE_OMIT_VIRTUALTABLE */
5091
danielk1977182c4ba2007-06-27 15:53:34 +00005092#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005093/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005094**
drh66a51672008-01-03 00:01:23 +00005095** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005096** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005097** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005098*/
drh9cbf3422008-01-17 16:22:13 +00005099case OP_VRename: {
danielk19772dca4ac2008-01-03 11:50:29 +00005100 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk19776dbee812008-01-03 18:39:41 +00005101 Mem *pName = &p->aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005102 assert( pVtab->pModule->xRename );
drh5b6afba2008-01-05 16:29:28 +00005103 REGISTER_TRACE(pOp->p1, pName);
danielk1977182c4ba2007-06-27 15:53:34 +00005104
danielk19776dbee812008-01-03 18:39:41 +00005105 Stringify(pName, encoding);
danielk1977182c4ba2007-06-27 15:53:34 +00005106
5107 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5108 sqlite3VtabLock(pVtab);
danielk19776dbee812008-01-03 18:39:41 +00005109 rc = pVtab->pModule->xRename(pVtab, pName->z);
drh633e6d52008-07-28 19:34:53 +00005110 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005111 p->zErrMsg = pVtab->zErrMsg;
5112 pVtab->zErrMsg = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005113 sqlite3VtabUnlock(db, pVtab);
5114 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5115
danielk1977182c4ba2007-06-27 15:53:34 +00005116 break;
5117}
5118#endif
drh4cbdda92006-06-14 19:00:20 +00005119
5120#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005121/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005122**
drh66a51672008-01-03 00:01:23 +00005123** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005124** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005125** are contiguous memory cells starting at P3 to pass to the xUpdate
5126** invocation. The value in register (P3+P2-1) corresponds to the
5127** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005128**
5129** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005130** The argv[0] element (which corresponds to memory cell P3)
5131** is the rowid of a row to delete. If argv[0] is NULL then no
5132** deletion occurs. The argv[1] element is the rowid of the new
5133** row. This can be NULL to have the virtual table select the new
5134** rowid for itself. The subsequent elements in the array are
5135** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005136**
5137** If P2==1 then no insert is performed. argv[0] is the rowid of
5138** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005139**
5140** P1 is a boolean flag. If it is set to true and the xUpdate call
5141** is successful, then the value returned by sqlite3_last_insert_rowid()
5142** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005143*/
drh9cbf3422008-01-17 16:22:13 +00005144case OP_VUpdate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005145 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977399918f2006-06-14 13:03:23 +00005146 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
drh4cbdda92006-06-14 19:00:20 +00005147 int nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005148 assert( pOp->p4type==P4_VTAB );
danielk1977399918f2006-06-14 13:03:23 +00005149 if( pModule->xUpdate==0 ){
drhf089aa42008-07-08 19:34:06 +00005150 sqlite3SetString(&p->zErrMsg, db, "read-only table");
danielk1977399918f2006-06-14 13:03:23 +00005151 rc = SQLITE_ERROR;
5152 }else{
5153 int i;
danielk19771f6eec52006-06-16 06:17:47 +00005154 sqlite_int64 rowid;
danielk1977399918f2006-06-14 13:03:23 +00005155 Mem **apArg = p->apArg;
danielk19772a339ff2008-01-03 17:31:44 +00005156 Mem *pX = &p->aMem[pOp->p3];
5157 for(i=0; i<nArg; i++){
drh9c419382006-06-16 21:13:21 +00005158 storeTypeInfo(pX, 0);
5159 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005160 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005161 }
danielk1977c7d54102006-06-15 07:29:00 +00005162 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh189d4af2006-09-02 20:57:52 +00005163 sqlite3VtabLock(pVtab);
danielk19771f6eec52006-06-16 06:17:47 +00005164 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drh633e6d52008-07-28 19:34:53 +00005165 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005166 p->zErrMsg = pVtab->zErrMsg;
5167 pVtab->zErrMsg = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00005168 sqlite3VtabUnlock(db, pVtab);
danielk1977c7d54102006-06-15 07:29:00 +00005169 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk19771f6eec52006-06-16 06:17:47 +00005170 if( pOp->p1 && rc==SQLITE_OK ){
5171 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5172 db->lastRowid = rowid;
5173 }
drhb5df1442008-04-10 14:00:09 +00005174 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00005175 }
drh4cbdda92006-06-14 19:00:20 +00005176 break;
danielk1977399918f2006-06-14 13:03:23 +00005177}
5178#endif /* SQLITE_OMIT_VIRTUALTABLE */
5179
danielk197759a93792008-05-15 17:48:20 +00005180#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5181/* Opcode: Pagecount P1 P2 * * *
5182**
5183** Write the current number of pages in database P1 to memory cell P2.
5184*/
5185case OP_Pagecount: { /* out2-prerelease */
5186 int p1 = pOp->p1;
5187 int nPage;
5188 Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt);
5189
danielk1977ad0132d2008-06-07 08:58:22 +00005190 rc = sqlite3PagerPagecount(pPager, &nPage);
5191 if( rc==SQLITE_OK ){
danielk197759a93792008-05-15 17:48:20 +00005192 pOut->flags = MEM_Int;
5193 pOut->u.i = nPage;
5194 }
5195 break;
5196}
5197#endif
5198
drh949f9cd2008-01-12 21:35:57 +00005199#ifndef SQLITE_OMIT_TRACE
5200/* Opcode: Trace * * * P4 *
5201**
5202** If tracing is enabled (by the sqlite3_trace()) interface, then
5203** the UTF-8 string contained in P4 is emitted on the trace callback.
5204*/
5205case OP_Trace: {
5206 if( pOp->p4.z ){
5207 if( db->xTrace ){
5208 db->xTrace(db->pTraceArg, pOp->p4.z);
5209 }
5210#ifdef SQLITE_DEBUG
5211 if( (db->flags & SQLITE_SqlTrace)!=0 ){
5212 sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z);
5213 }
5214#endif /* SQLITE_DEBUG */
5215 }
5216 break;
5217}
5218#endif
5219
drh91fd4d42008-01-19 20:11:25 +00005220
5221/* Opcode: Noop * * * * *
5222**
5223** Do nothing. This instruction is often useful as a jump
5224** destination.
drh5e00f6c2001-09-13 13:46:56 +00005225*/
drh91fd4d42008-01-19 20:11:25 +00005226/*
5227** The magic Explain opcode are only inserted when explain==2 (which
5228** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5229** This opcode records information from the optimizer. It is the
5230** the same as a no-op. This opcodesnever appears in a real VM program.
5231*/
5232default: { /* This is really OP_Noop and OP_Explain */
drh5e00f6c2001-09-13 13:46:56 +00005233 break;
5234}
5235
5236/*****************************************************************************
5237** The cases of the switch statement above this line should all be indented
5238** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5239** readability. From this point on down, the normal indentation rules are
5240** restored.
5241*****************************************************************************/
5242 }
drh6e142f52000-06-08 13:36:40 +00005243
drh7b396862003-01-01 23:06:20 +00005244#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005245 {
shane9bcbdad2008-05-29 20:22:37 +00005246 u64 elapsed = sqlite3Hwtime() - start;
5247 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005248 pOp->cnt++;
5249#if 0
shane9bcbdad2008-05-29 20:22:37 +00005250 fprintf(stdout, "%10llu ", elapsed);
danielk19774adee202004-05-08 08:23:19 +00005251 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005252#endif
5253 }
drh7b396862003-01-01 23:06:20 +00005254#endif
5255
drh6e142f52000-06-08 13:36:40 +00005256 /* The following code adds nothing to the actual functionality
5257 ** of the program. It is only here for testing and debugging.
5258 ** On the other hand, it does burn CPU cycles every time through
5259 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5260 */
5261#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005262 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005263
drhcf1023c2007-05-08 20:59:49 +00005264#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005265 if( p->trace ){
5266 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drhca48c902008-01-18 14:08:24 +00005267 if( opProperty & OPFLG_OUT2_PRERELEASE ){
drh5b6afba2008-01-05 16:29:28 +00005268 registerTrace(p->trace, pOp->p2, pOut);
drh75897232000-05-29 14:26:00 +00005269 }
drhca48c902008-01-18 14:08:24 +00005270 if( opProperty & OPFLG_OUT3 ){
drh5b6afba2008-01-05 16:29:28 +00005271 registerTrace(p->trace, pOp->p3, pOut);
5272 }
drh75897232000-05-29 14:26:00 +00005273 }
danielk1977b5402fb2005-01-12 07:15:04 +00005274#endif /* SQLITE_DEBUG */
5275#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005276 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005277
drha05a7222008-01-19 03:35:58 +00005278 /* If we reach this point, it means that execution is finished with
5279 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005280 */
drha05a7222008-01-19 03:35:58 +00005281vdbe_error_halt:
5282 assert( rc );
5283 p->rc = rc;
drh92f02c32004-09-02 14:57:08 +00005284 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005285 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5286 rc = SQLITE_ERROR;
drh900b31e2007-08-28 02:27:51 +00005287
5288 /* This is the only way out of this procedure. We have to
5289 ** release the mutexes on btrees that were acquired at the
5290 ** top. */
5291vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005292 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005293 return rc;
5294
drh023ae032007-05-08 12:12:16 +00005295 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5296 ** is encountered.
5297 */
5298too_big:
drhf089aa42008-07-08 19:34:06 +00005299 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005300 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005301 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005302
drh98640a32007-06-07 19:08:32 +00005303 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005304 */
5305no_mem:
drh17435752007-08-16 04:30:38 +00005306 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005307 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005308 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005309 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005310
5311 /* Jump to here for an SQLITE_MISUSE error.
5312 */
5313abort_due_to_misuse:
5314 rc = SQLITE_MISUSE;
5315 /* Fall thru into abort_due_to_error */
5316
5317 /* Jump to here for any other kind of fatal error. The "rc" variable
5318 ** should hold the error number.
5319 */
5320abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005321 assert( p->zErrMsg==0 );
5322 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005323 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005324 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005325 }
drha05a7222008-01-19 03:35:58 +00005326 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005327
danielk19776f8a5032004-05-10 10:34:51 +00005328 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005329 ** flag.
5330 */
5331abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005332 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005333 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005334 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005335 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005336 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005337}