blob: 9bdeef95f46dd39e460748e3009891a1291b9c87 [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**
danielk1977f73ab8b2008-12-29 10:39:53 +000046** $Id: vdbe.c,v 1.807 2008/12/29 10:39:54 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000047*/
48#include "sqliteInt.h"
drh7c68d602000-10-11 19:28:51 +000049#include <ctype.h>
drh9a324642003-09-06 20:12:01 +000050#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000051
52/*
drh487ab3c2001-11-08 00:45:21 +000053** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000054** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000055** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000056** working correctly. This variable has no function other than to
57** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000058*/
drh0f7eb612006-08-08 13:51:43 +000059#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000060int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000061#endif
drh487ab3c2001-11-08 00:45:21 +000062
drhf6038712004-02-08 18:07:34 +000063/*
64** When this global variable is positive, it gets decremented once before
drh881feaa2006-07-26 01:39:30 +000065** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
66** field of the sqlite3 structure is set in order to simulate and interrupt.
drhf6038712004-02-08 18:07:34 +000067**
68** This facility is used for testing purposes only. It does not function
69** in an ordinary build.
70*/
drh0f7eb612006-08-08 13:51:43 +000071#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000072int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000073#endif
drh1350b032002-02-27 19:00:20 +000074
danielk19777e18c252004-05-25 11:47:24 +000075/*
drh6bf89572004-11-03 16:27:01 +000076** The next global variable is incremented each type the OP_Sort opcode
77** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000078** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000079** has no function other than to help verify the correct operation of the
80** library.
81*/
drh0f7eb612006-08-08 13:51:43 +000082#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000083int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000084#endif
drh6bf89572004-11-03 16:27:01 +000085
86/*
drhae7e1512007-05-02 16:51:59 +000087** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000088** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000089** use this information to make sure that the zero-blob functionality
90** is working correctly. This variable has no function other than to
91** help verify the correct operation of the library.
92*/
93#ifdef SQLITE_TEST
94int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000095static void updateMaxBlobsize(Mem *p){
96 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
97 sqlite3_max_blobsize = p->n;
98 }
99}
drhae7e1512007-05-02 16:51:59 +0000100#endif
101
102/*
drhb7654112008-01-12 12:48:07 +0000103** Test a register to see if it exceeds the current maximum blob size.
104** If it does, record the new maximum blob size.
105*/
drh678ccce2008-03-31 18:19:54 +0000106#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000107# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000108#else
109# define UPDATE_MAX_BLOBSIZE(P)
110#endif
111
112/*
drh9cbf3422008-01-17 16:22:13 +0000113** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000114** already. Return non-zero if a malloc() fails.
115*/
drhb21c8cd2007-08-21 19:33:56 +0000116#define Stringify(P, enc) \
117 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000118 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000119
120/*
danielk1977bd7e4602004-05-24 07:34:48 +0000121** An ephemeral string value (signified by the MEM_Ephem flag) contains
122** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000123** is responsible for deallocating that string. Because the register
124** does not control the string, it might be deleted without the register
125** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000126**
127** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000128** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000129** converts an MEM_Ephem string into an MEM_Dyn string.
130*/
drhb21c8cd2007-08-21 19:33:56 +0000131#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000132 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000133 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000134
135/*
danielk19771cc5ed82007-05-16 17:28:43 +0000136** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
137** P if required.
138*/
drhb21c8cd2007-08-21 19:33:56 +0000139#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +0000140
141/*
shane21e7feb2008-05-30 15:59:49 +0000142** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000143** user-defined function or returned to the user as the result of a query.
144** The second argument, 'db_enc' is the text encoding used by the vdbe for
drh9cbf3422008-01-17 16:22:13 +0000145** register variables. This routine sets the pMem->enc and pMem->type
danielk1977c572ef72004-05-27 09:28:41 +0000146** variables used by the sqlite3_value_*() routines.
147*/
drh3a41a3f2004-05-30 02:14:17 +0000148#define storeTypeInfo(A,B) _storeTypeInfo(A)
149static void _storeTypeInfo(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000150 int flags = pMem->flags;
151 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000152 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000153 }
154 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000155 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000156 }
157 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000158 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000159 }
160 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000161 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000162 }else{
drh9c054832004-05-31 18:51:57 +0000163 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000164 }
165}
danielk19778a6b5412004-05-24 07:04:25 +0000166
167/*
drh3a40f692008-01-04 16:50:09 +0000168** Properties of opcodes. The OPFLG_INITIALIZER macro is
169** created by mkopcodeh.awk during compilation. Data is obtained
170** from the comments following the "case OP_xxxx:" statements in
171** this file.
drh3a40f692008-01-04 16:50:09 +0000172*/
danielk1977263ac192008-09-02 11:05:01 +0000173static const unsigned char opcodeProperty[] = OPFLG_INITIALIZER;
drh3a40f692008-01-04 16:50:09 +0000174
175/*
176** Return true if an opcode has any of the OPFLG_xxx properties
177** specified by mask.
178*/
179int sqlite3VdbeOpcodeHasProperty(int opcode, int mask){
danielk197764202cf2008-11-17 15:31:47 +0000180 assert( opcode>0 && opcode<(int)sizeof(opcodeProperty) );
drh3a40f692008-01-04 16:50:09 +0000181 return (opcodeProperty[opcode]&mask)!=0;
182}
183
184/*
drhdfe88ec2008-11-03 20:55:06 +0000185** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000186** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000187*/
drhdfe88ec2008-11-03 20:55:06 +0000188static VdbeCursor *allocateCursor(
189 Vdbe *p, /* The virtual machine */
190 int iCur, /* Index of the new VdbeCursor */
191 Op *pOp, /* */
drh3d4501e2008-12-04 20:40:10 +0000192 int iDb, /* When database the cursor belongs to, or -1 */
drhdfe88ec2008-11-03 20:55:06 +0000193 int isBtreeCursor /* */
danielk1977cd3e8f72008-03-25 09:47:35 +0000194){
195 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000196 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000197 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000198 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000199 **
200 ** * Sometimes cursor numbers are used for a couple of different
201 ** purposes in a vdbe program. The different uses might require
202 ** different sized allocations. Memory cells provide growable
203 ** allocations.
204 **
205 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
206 ** be freed lazily via the sqlite3_release_memory() API. This
207 ** minimizes the number of malloc calls made by the system.
208 **
209 ** Memory cells for cursors are allocated at the top of the address
210 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
211 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
212 */
213 Mem *pMem = &p->aMem[p->nMem-iCur];
214
danielk19775f096132008-03-28 15:44:09 +0000215 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000216 VdbeCursor *pCx = 0;
danielk1977cd3e8f72008-03-25 09:47:35 +0000217 /* If the opcode of pOp is OP_SetNumColumns, then pOp->p2 contains
218 ** the number of fields in the records contained in the table or
219 ** index being opened. Use this to reserve space for the
drhdfe88ec2008-11-03 20:55:06 +0000220 ** VdbeCursor.aType[] array.
danielk1977cd3e8f72008-03-25 09:47:35 +0000221 */
222 int nField = 0;
223 if( pOp->opcode==OP_SetNumColumns || pOp->opcode==OP_OpenEphemeral ){
224 nField = pOp->p2;
225 }
danielk19775f096132008-03-28 15:44:09 +0000226 nByte =
drhdfe88ec2008-11-03 20:55:06 +0000227 sizeof(VdbeCursor) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
229 2*nField*sizeof(u32);
230
drh290c1942004-08-21 17:54:45 +0000231 assert( iCur<p->nCursor );
232 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000233 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000234 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000235 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000236 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000237 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
danielk1977cd3e8f72008-03-25 09:47:35 +0000238 memset(pMem->z, 0, nByte);
danielk197794eb6a12005-12-15 15:22:08 +0000239 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000240 pCx->nField = nField;
241 if( nField ){
drhdfe88ec2008-11-03 20:55:06 +0000242 pCx->aType = (u32 *)&pMem->z[sizeof(VdbeCursor)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000243 }
244 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000245 pCx->pCursor = (BtCursor*)
246 &pMem->z[sizeof(VdbeCursor)+2*nField*sizeof(u32)];
danielk1977cd3e8f72008-03-25 09:47:35 +0000247 }
danielk197794eb6a12005-12-15 15:22:08 +0000248 }
drh4774b132004-06-12 20:12:51 +0000249 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000250}
251
danielk19773d1bfea2004-05-14 11:00:53 +0000252/*
drh29d72102006-02-09 22:13:41 +0000253** Try to convert a value into a numeric representation if we can
254** do so without loss of information. In other words, if the string
255** looks like a number, convert it into a number. If it does not
256** look like a number, leave it alone.
257*/
drhb21c8cd2007-08-21 19:33:56 +0000258static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000259 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
260 int realnum;
drhb21c8cd2007-08-21 19:33:56 +0000261 sqlite3VdbeMemNulTerminate(pRec);
drh29d72102006-02-09 22:13:41 +0000262 if( (pRec->flags&MEM_Str)
263 && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
264 i64 value;
drhb21c8cd2007-08-21 19:33:56 +0000265 sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
drhb6a9ece2007-06-26 00:37:27 +0000266 if( !realnum && sqlite3Atoi64(pRec->z, &value) ){
drh3c024d62007-03-30 11:23:45 +0000267 pRec->u.i = value;
danielk1977a7a8e142008-02-13 18:25:27 +0000268 MemSetTypeFlag(pRec, MEM_Int);
drh29d72102006-02-09 22:13:41 +0000269 }else{
270 sqlite3VdbeMemRealify(pRec);
271 }
272 }
273 }
274}
275
276/*
drh8a512562005-11-14 22:29:05 +0000277** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000278**
drh8a512562005-11-14 22:29:05 +0000279** SQLITE_AFF_INTEGER:
280** SQLITE_AFF_REAL:
281** SQLITE_AFF_NUMERIC:
282** Try to convert pRec to an integer representation or a
283** floating-point representation if an integer representation
284** is not possible. Note that the integer representation is
285** always preferred, even if the affinity is REAL, because
286** an integer representation is more space efficient on disk.
287**
288** SQLITE_AFF_TEXT:
289** Convert pRec to a text representation.
290**
291** SQLITE_AFF_NONE:
292** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000293*/
drh17435752007-08-16 04:30:38 +0000294static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000295 Mem *pRec, /* The value to apply affinity to */
296 char affinity, /* The affinity to be applied */
297 u8 enc /* Use this text encoding */
298){
drh8a512562005-11-14 22:29:05 +0000299 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000300 /* Only attempt the conversion to TEXT if there is an integer or real
301 ** representation (blob and NULL do not get converted) but no string
302 ** representation.
303 */
304 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000305 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000306 }
307 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000308 }else if( affinity!=SQLITE_AFF_NONE ){
309 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
310 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000311 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000312 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000313 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000314 }
danielk19773d1bfea2004-05-14 11:00:53 +0000315 }
316}
317
danielk1977aee18ef2005-03-09 12:26:50 +0000318/*
drh29d72102006-02-09 22:13:41 +0000319** Try to convert the type of a function argument or a result column
320** into a numeric representation. Use either INTEGER or REAL whichever
321** is appropriate. But only do the conversion if it is possible without
322** loss of information and return the revised type of the argument.
323**
324** This is an EXPERIMENTAL api and is subject to change or removal.
325*/
326int sqlite3_value_numeric_type(sqlite3_value *pVal){
327 Mem *pMem = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +0000328 applyNumericAffinity(pMem);
drh29d72102006-02-09 22:13:41 +0000329 storeTypeInfo(pMem, 0);
330 return pMem->type;
331}
332
333/*
danielk1977aee18ef2005-03-09 12:26:50 +0000334** Exported version of applyAffinity(). This one works on sqlite3_value*,
335** not the internal Mem* type.
336*/
danielk19771e536952007-08-16 10:09:01 +0000337void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000338 sqlite3_value *pVal,
339 u8 affinity,
340 u8 enc
341){
drhb21c8cd2007-08-21 19:33:56 +0000342 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000343}
344
danielk1977b5402fb2005-01-12 07:15:04 +0000345#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000346/*
danielk1977ca6b2912004-05-21 10:49:47 +0000347** Write a nice string representation of the contents of cell pMem
348** into buffer zBuf, length nBuf.
349*/
drh74161702006-02-24 02:53:49 +0000350void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000351 char *zCsr = zBuf;
352 int f = pMem->flags;
353
drh57196282004-10-06 15:41:16 +0000354 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000355
danielk1977ca6b2912004-05-21 10:49:47 +0000356 if( f&MEM_Blob ){
357 int i;
358 char c;
359 if( f & MEM_Dyn ){
360 c = 'z';
361 assert( (f & (MEM_Static|MEM_Ephem))==0 );
362 }else if( f & MEM_Static ){
363 c = 't';
364 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
365 }else if( f & MEM_Ephem ){
366 c = 'e';
367 assert( (f & (MEM_Static|MEM_Dyn))==0 );
368 }else{
369 c = 's';
370 }
371
drh5bb3eb92007-05-04 13:15:55 +0000372 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000373 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000374 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000375 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000376 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000377 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000378 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000379 }
380 for(i=0; i<16 && i<pMem->n; i++){
381 char z = pMem->z[i];
382 if( z<32 || z>126 ) *zCsr++ = '.';
383 else *zCsr++ = z;
384 }
385
drhe718efe2007-05-10 21:14:03 +0000386 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000387 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000388 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000389 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000390 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000391 }
danielk1977b1bc9532004-05-22 03:05:33 +0000392 *zCsr = '\0';
393 }else if( f & MEM_Str ){
394 int j, k;
395 zBuf[0] = ' ';
396 if( f & MEM_Dyn ){
397 zBuf[1] = 'z';
398 assert( (f & (MEM_Static|MEM_Ephem))==0 );
399 }else if( f & MEM_Static ){
400 zBuf[1] = 't';
401 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
402 }else if( f & MEM_Ephem ){
403 zBuf[1] = 'e';
404 assert( (f & (MEM_Static|MEM_Dyn))==0 );
405 }else{
406 zBuf[1] = 's';
407 }
408 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000409 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000410 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000411 zBuf[k++] = '[';
412 for(j=0; j<15 && j<pMem->n; j++){
413 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000414 if( c>=0x20 && c<0x7f ){
415 zBuf[k++] = c;
416 }else{
417 zBuf[k++] = '.';
418 }
419 }
420 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000421 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000422 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000423 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000424 }
danielk1977ca6b2912004-05-21 10:49:47 +0000425}
426#endif
427
drh5b6afba2008-01-05 16:29:28 +0000428#ifdef SQLITE_DEBUG
429/*
430** Print the value of a register for tracing purposes:
431*/
432static void memTracePrint(FILE *out, Mem *p){
433 if( p->flags & MEM_Null ){
434 fprintf(out, " NULL");
435 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
436 fprintf(out, " si:%lld", p->u.i);
437 }else if( p->flags & MEM_Int ){
438 fprintf(out, " i:%lld", p->u.i);
439 }else if( p->flags & MEM_Real ){
440 fprintf(out, " r:%g", p->r);
441 }else{
442 char zBuf[200];
443 sqlite3VdbeMemPrettyPrint(p, zBuf);
444 fprintf(out, " ");
445 fprintf(out, "%s", zBuf);
446 }
447}
448static void registerTrace(FILE *out, int iReg, Mem *p){
449 fprintf(out, "REG[%d] = ", iReg);
450 memTracePrint(out, p);
451 fprintf(out, "\n");
452}
453#endif
454
455#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000456# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000457#else
458# define REGISTER_TRACE(R,M)
459#endif
460
danielk197784ac9d02004-05-18 09:58:06 +0000461
drh7b396862003-01-01 23:06:20 +0000462#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000463
464/*
465** hwtime.h contains inline assembler code for implementing
466** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000467*/
shane9bcbdad2008-05-29 20:22:37 +0000468#include "hwtime.h"
469
drh7b396862003-01-01 23:06:20 +0000470#endif
471
drh8c74a8c2002-08-25 19:20:40 +0000472/*
drhcaec2f12003-01-07 02:47:47 +0000473** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000474** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000475** processing of the VDBE program is interrupted.
476**
477** This macro added to every instruction that does a jump in order to
478** implement a loop. This test used to be on every single instruction,
479** but that meant we more testing that we needed. By only testing the
480** flag on jump instructions, we get a (small) speed improvement.
481*/
482#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000483 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000484
danielk1977861f7452008-06-05 11:39:11 +0000485#ifdef SQLITE_DEBUG
486static int fileExists(sqlite3 *db, const char *zFile){
danielk1977ad0132d2008-06-07 08:58:22 +0000487 int res = 0;
488 int rc = SQLITE_OK;
489#ifdef SQLITE_TEST
490 /* If we are currently testing IO errors, then do not call OsAccess() to
491 ** test for the presence of zFile. This is because any IO error that
492 ** occurs here will not be reported, causing the test to fail.
493 */
494 extern int sqlite3_io_error_pending;
495 if( sqlite3_io_error_pending<=0 )
496#endif
497 rc = sqlite3OsAccess(db->pVfs, zFile, SQLITE_ACCESS_EXISTS, &res);
danielk1977861f7452008-06-05 11:39:11 +0000498 return (res && rc==SQLITE_OK);
499}
500#endif
drhcaec2f12003-01-07 02:47:47 +0000501
danielk1977fd7f0452008-12-17 17:30:26 +0000502#ifndef NDEBUG
503/*
504** This function is only called from within an assert() expression. It
505** checks that the sqlite3.nTransaction variable is correctly set to
506** the number of non-transaction savepoints currently in the
507** linked list starting at sqlite3.pSavepoint.
508**
509** Usage:
510**
511** assert( checkSavepointCount(db) );
512*/
513static int checkSavepointCount(sqlite3 *db){
514 int n = 0;
515 Savepoint *p;
516 for(p=db->pSavepoint; p; p=p->pNext) n++;
517 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
518 return 1;
519}
520#endif
521
drhcaec2f12003-01-07 02:47:47 +0000522/*
drhb86ccfb2003-01-28 23:13:10 +0000523** Execute as much of a VDBE program as we can then return.
524**
danielk19774adee202004-05-08 08:23:19 +0000525** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000526** close the program with a final OP_Halt and to set up the callbacks
527** and the error message pointer.
528**
529** Whenever a row or result data is available, this routine will either
530** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000531** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000532**
533** If an attempt is made to open a locked database, then this routine
534** will either invoke the busy callback (if there is one) or it will
535** return SQLITE_BUSY.
536**
537** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000538** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000539** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
540**
541** If the callback ever returns non-zero, then the program exits
542** immediately. There will be no error message but the p->rc field is
543** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
544**
drh9468c7f2003-03-07 19:50:07 +0000545** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
546** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000547**
548** Other fatal errors return SQLITE_ERROR.
549**
danielk19774adee202004-05-08 08:23:19 +0000550** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000551** used to clean up the mess that was left behind.
552*/
danielk19774adee202004-05-08 08:23:19 +0000553int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000554 Vdbe *p /* The VDBE */
555){
556 int pc; /* The program counter */
557 Op *pOp; /* Current operation */
558 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000559 sqlite3 *db = p->db; /* The database */
drh8079a0d2006-01-12 17:20:50 +0000560 u8 encoding = ENC(db); /* The database encoding */
drhb27b7f52008-12-10 18:03:45 +0000561 Mem *pIn1 = 0; /* 1st input operand */
562 Mem *pIn2 = 0; /* 2nd input operand */
563 Mem *pIn3 = 0; /* 3rd input operand */
564 Mem *pOut = 0; /* Output operand */
drhb1fdb2a2008-01-05 04:06:03 +0000565 u8 opProperty;
drh0acb7e42008-06-25 00:12:41 +0000566 int iCompare = 0; /* Result of last OP_Compare operation */
567 int *aPermute = 0; /* Permuation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000568#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000569 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000570 int origPc; /* Program counter at start of opcode */
571#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000572#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
573 int nProgressOps = 0; /* Opcodes executed since progress callback. */
574#endif
drh23f79d02008-08-20 22:06:47 +0000575 UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
drhe63d9992008-08-13 19:11:48 +0000576
drhca48c902008-01-18 14:08:24 +0000577 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhb86ccfb2003-01-28 23:13:10 +0000578 assert( db->magic==SQLITE_MAGIC_BUSY );
drh4cf7c7f2007-08-28 23:28:07 +0000579 sqlite3BtreeMutexArrayEnter(&p->aMutex);
danielk19772e588c72005-12-09 14:25:08 +0000580 if( p->rc==SQLITE_NOMEM ){
581 /* This happens if a malloc() inside a call to sqlite3_column_text() or
582 ** sqlite3_column_text16() failed. */
583 goto no_mem;
584 }
drh3a840692003-01-29 22:58:26 +0000585 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
586 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000587 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000588 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000589 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000590 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000591 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000592#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000593 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000594 if( p->pc==0
595 && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
drh3c23a882007-01-09 14:01:13 +0000596 ){
597 int i;
598 printf("VDBE Program Listing:\n");
599 sqlite3VdbePrintSql(p);
600 for(i=0; i<p->nOp; i++){
601 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
602 }
603 }
danielk1977861f7452008-06-05 11:39:11 +0000604 if( fileExists(db, "vdbe_trace") ){
drh3c23a882007-01-09 14:01:13 +0000605 p->trace = stdout;
606 }
danielk19772d1d86f2008-06-20 14:59:51 +0000607 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000608#endif
drhb86ccfb2003-01-28 23:13:10 +0000609 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000610 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000611 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000612#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000613 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000614 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000615#endif
drh75897232000-05-29 14:26:00 +0000616 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000617
danielk19778b60e0f2005-01-12 09:10:39 +0000618 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000619 */
danielk19778b60e0f2005-01-12 09:10:39 +0000620#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000621 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000622 if( pc==0 ){
623 printf("VDBE Execution Trace:\n");
624 sqlite3VdbePrintSql(p);
625 }
danielk19774adee202004-05-08 08:23:19 +0000626 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000627 }
drh19db9352008-03-27 22:42:51 +0000628 if( p->trace==0 && pc==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000629 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000630 if( fileExists(db, "vdbe_sqltrace") ){
drh19db9352008-03-27 22:42:51 +0000631 sqlite3VdbePrintSql(p);
632 }
danielk19772d1d86f2008-06-20 14:59:51 +0000633 sqlite3EndBenignMalloc();
drh3f7d4e42004-07-24 14:35:58 +0000634 }
635#endif
636
drh6e142f52000-06-08 13:36:40 +0000637
drhf6038712004-02-08 18:07:34 +0000638 /* Check to see if we need to simulate an interrupt. This only happens
639 ** if we have a special test build.
640 */
641#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000642 if( sqlite3_interrupt_count>0 ){
643 sqlite3_interrupt_count--;
644 if( sqlite3_interrupt_count==0 ){
645 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000646 }
647 }
648#endif
649
danielk1977348bb5d2003-10-18 09:37:26 +0000650#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
651 /* Call the progress callback if it is configured and the required number
652 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000653 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000654 ** If the progress callback returns non-zero, exit the virtual machine with
655 ** a return code SQLITE_ABORT.
656 */
drh3914aed2004-01-31 20:40:42 +0000657 if( db->xProgress ){
658 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000659 int prc;
drhf8888bb2006-05-26 19:57:19 +0000660 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000661 prc =db->xProgress(db->pProgressArg);
drhf8888bb2006-05-26 19:57:19 +0000662 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000663 if( prc!=0 ){
664 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000665 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000666 }
danielk19773fe11f32007-06-13 16:49:48 +0000667 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000668 }
drh3914aed2004-01-31 20:40:42 +0000669 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000670 }
danielk1977348bb5d2003-10-18 09:37:26 +0000671#endif
672
drh4c583122008-01-04 22:01:03 +0000673 /* Do common setup processing for any opcode that is marked
674 ** with the "out2-prerelease" tag. Such opcodes have a single
drh9cbf3422008-01-17 16:22:13 +0000675 ** output which is specified by the P2 parameter. The P2 register
drh4c583122008-01-04 22:01:03 +0000676 ** is initialized to a NULL.
677 */
drhb1fdb2a2008-01-05 04:06:03 +0000678 opProperty = opcodeProperty[pOp->opcode];
679 if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000680 assert( pOp->p2>0 );
681 assert( pOp->p2<=p->nMem );
682 pOut = &p->aMem[pOp->p2];
danielk19775f096132008-03-28 15:44:09 +0000683 sqlite3VdbeMemReleaseExternal(pOut);
drh4c583122008-01-04 22:01:03 +0000684 pOut->flags = MEM_Null;
drhb1fdb2a2008-01-05 04:06:03 +0000685 }else
686
687 /* Do common setup for opcodes marked with one of the following
688 ** combinations of properties.
689 **
690 ** in1
691 ** in1 in2
692 ** in1 in2 out3
693 ** in1 in3
drhb1fdb2a2008-01-05 04:06:03 +0000694 **
drh9cbf3422008-01-17 16:22:13 +0000695 ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
696 ** registers for inputs. Variable pOut points to the output register.
drhb1fdb2a2008-01-05 04:06:03 +0000697 */
698 if( (opProperty & OPFLG_IN1)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000699 assert( pOp->p1>0 );
700 assert( pOp->p1<=p->nMem );
701 pIn1 = &p->aMem[pOp->p1];
702 REGISTER_TRACE(pOp->p1, pIn1);
drhb1fdb2a2008-01-05 04:06:03 +0000703 if( (opProperty & OPFLG_IN2)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000704 assert( pOp->p2>0 );
drhaa9b8962008-01-08 02:57:55 +0000705 assert( pOp->p2<=p->nMem );
706 pIn2 = &p->aMem[pOp->p2];
707 REGISTER_TRACE(pOp->p2, pIn2);
drh9cbf3422008-01-17 16:22:13 +0000708 if( (opProperty & OPFLG_OUT3)!=0 ){
709 assert( pOp->p3>0 );
710 assert( pOp->p3<=p->nMem );
711 pOut = &p->aMem[pOp->p3];
712 }
713 }else if( (opProperty & OPFLG_IN3)!=0 ){
714 assert( pOp->p3>0 );
drhaa9b8962008-01-08 02:57:55 +0000715 assert( pOp->p3<=p->nMem );
716 pIn3 = &p->aMem[pOp->p3];
717 REGISTER_TRACE(pOp->p3, pIn3);
718 }
drh9cbf3422008-01-17 16:22:13 +0000719 }else if( (opProperty & OPFLG_IN2)!=0 ){
720 assert( pOp->p2>0 );
721 assert( pOp->p2<=p->nMem );
722 pIn2 = &p->aMem[pOp->p2];
723 REGISTER_TRACE(pOp->p2, pIn2);
724 }else if( (opProperty & OPFLG_IN3)!=0 ){
725 assert( pOp->p3>0 );
726 assert( pOp->p3<=p->nMem );
727 pIn3 = &p->aMem[pOp->p3];
728 REGISTER_TRACE(pOp->p3, pIn3);
drh4c583122008-01-04 22:01:03 +0000729 }
730
drh75897232000-05-29 14:26:00 +0000731 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000732
drh5e00f6c2001-09-13 13:46:56 +0000733/*****************************************************************************
734** What follows is a massive switch statement where each case implements a
735** separate instruction in the virtual machine. If we follow the usual
736** indentation conventions, each case should be indented by 6 spaces. But
737** that is a lot of wasted space on the left margin. So the code within
738** the switch statement will break with convention and be flush-left. Another
739** big comment (similar to this one) will mark the point in the code where
740** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000741**
742** The formatting of each case is important. The makefile for SQLite
743** generates two C files "opcodes.h" and "opcodes.c" by scanning this
744** file looking for lines that begin with "case OP_". The opcodes.h files
745** will be filled with #defines that give unique integer values to each
746** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000747** each string is the symbolic name for the corresponding opcode. If the
748** case statement is followed by a comment of the form "/# same as ... #/"
749** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000750**
drh9cbf3422008-01-17 16:22:13 +0000751** Other keywords in the comment that follows each case are used to
752** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
753** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
754** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000755**
drhac82fcf2002-09-08 17:23:41 +0000756** Documentation about VDBE opcodes is generated by scanning this file
757** for lines of that contain "Opcode:". That line and all subsequent
758** comment lines are used in the generation of the opcode.html documentation
759** file.
760**
761** SUMMARY:
762**
763** Formatting is important to scripts that scan this file.
764** Do not deviate from the formatting style currently in use.
765**
drh5e00f6c2001-09-13 13:46:56 +0000766*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000767
drh9cbf3422008-01-17 16:22:13 +0000768/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000769**
770** An unconditional jump to address P2.
771** The next instruction executed will be
772** the one at index P2 from the beginning of
773** the program.
774*/
drh9cbf3422008-01-17 16:22:13 +0000775case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000776 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000777 pc = pOp->p2 - 1;
778 break;
779}
drh75897232000-05-29 14:26:00 +0000780
drh2eb95372008-06-06 15:04:36 +0000781/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000782**
drh2eb95372008-06-06 15:04:36 +0000783** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000784** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000785*/
drh9cbf3422008-01-17 16:22:13 +0000786case OP_Gosub: { /* jump */
drh2eb95372008-06-06 15:04:36 +0000787 assert( pOp->p1>0 );
788 assert( pOp->p1<=p->nMem );
789 pIn1 = &p->aMem[pOp->p1];
790 assert( (pIn1->flags & MEM_Dyn)==0 );
791 pIn1->flags = MEM_Int;
792 pIn1->u.i = pc;
793 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000794 pc = pOp->p2 - 1;
795 break;
796}
797
drh2eb95372008-06-06 15:04:36 +0000798/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000799**
drh2eb95372008-06-06 15:04:36 +0000800** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000801*/
drh2eb95372008-06-06 15:04:36 +0000802case OP_Return: { /* in1 */
803 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000804 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000805 break;
806}
807
drhe00ee6e2008-06-20 15:24:01 +0000808/* Opcode: Yield P1 * * * *
809**
810** Swap the program counter with the value in register P1.
811*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000812case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000813 int pcDest;
drhe00ee6e2008-06-20 15:24:01 +0000814 assert( (pIn1->flags & MEM_Dyn)==0 );
815 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000816 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000817 pIn1->u.i = pc;
818 REGISTER_TRACE(pOp->p1, pIn1);
819 pc = pcDest;
820 break;
821}
822
823
drh9cbf3422008-01-17 16:22:13 +0000824/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000825**
drh3d4501e2008-12-04 20:40:10 +0000826** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000827** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000828**
drh92f02c32004-09-02 14:57:08 +0000829** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
830** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
831** For errors, it can be some other value. If P1!=0 then P2 will determine
832** whether or not to rollback the current transaction. Do not rollback
833** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
834** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000835** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000836**
drh66a51672008-01-03 00:01:23 +0000837** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000838**
drh9cfcf5d2002-01-29 18:41:24 +0000839** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000840** every program. So a jump past the last instruction of the program
841** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000842*/
drh9cbf3422008-01-17 16:22:13 +0000843case OP_Halt: {
drh92f02c32004-09-02 14:57:08 +0000844 p->rc = pOp->p1;
845 p->pc = pc;
846 p->errorAction = pOp->p2;
danielk19772dca4ac2008-01-03 11:50:29 +0000847 if( pOp->p4.z ){
drhf089aa42008-07-08 19:34:06 +0000848 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drh9cfcf5d2002-01-29 18:41:24 +0000849 }
drh92f02c32004-09-02 14:57:08 +0000850 rc = sqlite3VdbeHalt(p);
danielk197701427a62005-01-11 13:02:33 +0000851 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
drh92f02c32004-09-02 14:57:08 +0000852 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000853 p->rc = rc = SQLITE_BUSY;
854 }else{
855 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000856 }
drh900b31e2007-08-28 02:27:51 +0000857 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000858}
drhc61053b2000-06-04 12:58:36 +0000859
drh4c583122008-01-04 22:01:03 +0000860/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000861**
drh9cbf3422008-01-17 16:22:13 +0000862** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000863*/
drh4c583122008-01-04 22:01:03 +0000864case OP_Integer: { /* out2-prerelease */
865 pOut->flags = MEM_Int;
866 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000867 break;
868}
869
drh4c583122008-01-04 22:01:03 +0000870/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000871**
drh66a51672008-01-03 00:01:23 +0000872** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000873** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000874*/
drh4c583122008-01-04 22:01:03 +0000875case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000876 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000877 pOut->flags = MEM_Int;
878 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000879 break;
880}
drh4f26d6c2004-05-26 23:25:30 +0000881
drh4c583122008-01-04 22:01:03 +0000882/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000883**
drh4c583122008-01-04 22:01:03 +0000884** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000885** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000886*/
drh4c583122008-01-04 22:01:03 +0000887case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
888 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000889 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000890 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000891 break;
892}
danielk1977cbb18d22004-05-28 11:37:27 +0000893
drh3c84ddf2008-01-09 02:15:38 +0000894/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000895**
drh66a51672008-01-03 00:01:23 +0000896** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000897** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000898*/
drh4c583122008-01-04 22:01:03 +0000899case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000900 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000901 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000902 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000903
904#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000905 if( encoding!=SQLITE_UTF8 ){
drh4c583122008-01-04 22:01:03 +0000906 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
907 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drhdab898f2008-07-30 13:14:55 +0000908 if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
danielk19775f096132008-03-28 15:44:09 +0000909 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000910 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000911 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000912 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000913 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000914 }
drh66a51672008-01-03 00:01:23 +0000915 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000916 pOp->p4.z = pOut->z;
917 pOp->p1 = pOut->n;
drhbb4957f2008-03-20 14:03:29 +0000918 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000919 goto too_big;
920 }
drhb7654112008-01-12 12:48:07 +0000921 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977bfd6cce2004-06-18 04:24:54 +0000922 break;
danielk19770f69c1e2004-05-29 11:24:50 +0000923 }
danielk197793758c82005-01-21 08:13:14 +0000924#endif
drhbb4957f2008-03-20 14:03:29 +0000925 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000926 goto too_big;
927 }
928 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000929}
drhf4479502004-05-27 03:12:53 +0000930
drh4c583122008-01-04 22:01:03 +0000931/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000932**
drh9cbf3422008-01-17 16:22:13 +0000933** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000934*/
drh4c583122008-01-04 22:01:03 +0000935case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000936 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000937 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
938 pOut->z = pOp->p4.z;
939 pOut->n = pOp->p1;
940 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000941 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000942 break;
943}
944
drh4c583122008-01-04 22:01:03 +0000945/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000946**
drh9cbf3422008-01-17 16:22:13 +0000947** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000948*/
drh4c583122008-01-04 22:01:03 +0000949case OP_Null: { /* out2-prerelease */
drhf0863fe2005-06-12 21:35:51 +0000950 break;
951}
952
953
drh9de221d2008-01-05 06:51:30 +0000954/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000955**
drh9de221d2008-01-05 06:51:30 +0000956** P4 points to a blob of data P1 bytes long. Store this
957** blob in register P2. This instruction is not coded directly
danielk1977cbb18d22004-05-28 11:37:27 +0000958** by the compiler. Instead, the compiler layer specifies
959** an OP_HexBlob opcode, with the hex string representation of
drh66a51672008-01-03 00:01:23 +0000960** the blob as P4. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000961** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000962*/
drh4c583122008-01-04 22:01:03 +0000963case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000964 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000965 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000966 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000967 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000968 break;
969}
970
drh3c84ddf2008-01-09 02:15:38 +0000971/* Opcode: Variable P1 P2 * * *
drh50457892003-09-06 01:10:47 +0000972**
drh9cbf3422008-01-17 16:22:13 +0000973** The value of variable P1 is written into register P2. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000974** an unknown in the original SQL string as handed to sqlite3_compile().
shane21e7feb2008-05-30 15:59:49 +0000975** Any occurrence of the '?' character in the original SQL is considered
drh7c972de2003-09-06 22:18:07 +0000976** a variable. Variables in the SQL string are number from left to
977** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000978** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000979*/
drh4c583122008-01-04 22:01:03 +0000980case OP_Variable: { /* out2-prerelease */
drh7c972de2003-09-06 22:18:07 +0000981 int j = pOp->p1 - 1;
drh023ae032007-05-08 12:12:16 +0000982 Mem *pVar;
danielk1977295ba552004-05-19 10:34:51 +0000983 assert( j>=0 && j<p->nVar );
984
drh023ae032007-05-08 12:12:16 +0000985 pVar = &p->aVar[j];
986 if( sqlite3VdbeMemTooBig(pVar) ){
987 goto too_big;
988 }
drh4c583122008-01-04 22:01:03 +0000989 sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static);
drhb7654112008-01-12 12:48:07 +0000990 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +0000991 break;
992}
danielk1977295ba552004-05-19 10:34:51 +0000993
drhb21e7c72008-06-22 12:37:57 +0000994/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +0000995**
drhb21e7c72008-06-22 12:37:57 +0000996** Move the values in register P1..P1+P3-1 over into
997** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
998** left holding a NULL. It is an error for register ranges
999** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001000*/
drhe1349cb2008-04-01 00:36:10 +00001001case OP_Move: {
1002 char *zMalloc;
drhb21e7c72008-06-22 12:37:57 +00001003 int n = pOp->p3;
1004 int p1 = pOp->p1;
1005 int p2 = pOp->p2;
1006 assert( n>0 );
1007 assert( p1>0 );
1008 assert( p1+n<p->nMem );
1009 pIn1 = &p->aMem[p1];
1010 assert( p2>0 );
1011 assert( p2+n<p->nMem );
1012 pOut = &p->aMem[p2];
1013 assert( p1+n<=p2 || p2+n<=p1 );
1014 while( n-- ){
drhb21e7c72008-06-22 12:37:57 +00001015 zMalloc = pOut->zMalloc;
1016 pOut->zMalloc = 0;
1017 sqlite3VdbeMemMove(pOut, pIn1);
1018 pIn1->zMalloc = zMalloc;
1019 REGISTER_TRACE(p2++, pOut);
1020 pIn1++;
1021 pOut++;
1022 }
drhe1349cb2008-04-01 00:36:10 +00001023 break;
1024}
1025
drhb1fdb2a2008-01-05 04:06:03 +00001026/* Opcode: Copy P1 P2 * * *
1027**
drh9cbf3422008-01-17 16:22:13 +00001028** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001029**
1030** This instruction makes a deep copy of the value. A duplicate
1031** is made of any string or blob constant. See also OP_SCopy.
1032*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001033case OP_Copy: { /* in1 */
drhe1349cb2008-04-01 00:36:10 +00001034 assert( pOp->p2>0 );
1035 assert( pOp->p2<=p->nMem );
1036 pOut = &p->aMem[pOp->p2];
1037 assert( pOut!=pIn1 );
1038 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1039 Deephemeralize(pOut);
1040 REGISTER_TRACE(pOp->p2, pOut);
1041 break;
1042}
1043
drhb1fdb2a2008-01-05 04:06:03 +00001044/* Opcode: SCopy P1 P2 * * *
1045**
drh9cbf3422008-01-17 16:22:13 +00001046** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001047**
1048** This instruction makes a shallow copy of the value. If the value
1049** is a string or blob, then the copy is only a pointer to the
1050** original and hence if the original changes so will the copy.
1051** Worse, if the original is deallocated, the copy becomes invalid.
1052** Thus the program must guarantee that the original will not change
1053** during the lifetime of the copy. Use OP_Copy to make a complete
1054** copy.
1055*/
danielk1977f73ab8b2008-12-29 10:39:53 +00001056case OP_SCopy: { /* in1 */
drh9cbf3422008-01-17 16:22:13 +00001057 REGISTER_TRACE(pOp->p1, pIn1);
1058 assert( pOp->p2>0 );
1059 assert( pOp->p2<=p->nMem );
1060 pOut = &p->aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001061 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001062 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh5b6afba2008-01-05 16:29:28 +00001063 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001064 break;
1065}
drh75897232000-05-29 14:26:00 +00001066
drh9cbf3422008-01-17 16:22:13 +00001067/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001068**
shane21e7feb2008-05-30 15:59:49 +00001069** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001070** results. This opcode causes the sqlite3_step() call to terminate
1071** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1072** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001073** row.
drhd4e70eb2008-01-02 00:34:36 +00001074*/
drh9cbf3422008-01-17 16:22:13 +00001075case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001076 Mem *pMem;
1077 int i;
1078 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001079 assert( pOp->p1>0 );
1080 assert( pOp->p1+pOp->p2<=p->nMem );
drhd4e70eb2008-01-02 00:34:36 +00001081
drhd4e70eb2008-01-02 00:34:36 +00001082 /* Invalidate all ephemeral cursor row caches */
1083 p->cacheCtr = (p->cacheCtr + 2)|1;
1084
1085 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001086 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001087 ** as side effect.
1088 */
1089 pMem = p->pResultSet = &p->aMem[pOp->p1];
1090 for(i=0; i<pOp->p2; i++){
1091 sqlite3VdbeMemNulTerminate(&pMem[i]);
1092 storeTypeInfo(&pMem[i], encoding);
drh0acb7e42008-06-25 00:12:41 +00001093 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001094 }
drh28039692008-03-17 16:54:01 +00001095 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001096
1097 /* Return SQLITE_ROW
1098 */
1099 p->nCallback++;
drhd4e70eb2008-01-02 00:34:36 +00001100 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001101 rc = SQLITE_ROW;
1102 goto vdbe_return;
1103}
1104
drh5b6afba2008-01-05 16:29:28 +00001105/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001106**
drh5b6afba2008-01-05 16:29:28 +00001107** Add the text in register P1 onto the end of the text in
1108** register P2 and store the result in register P3.
1109** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001110**
1111** P3 = P2 || P1
1112**
1113** It is illegal for P1 and P3 to be the same register. Sometimes,
1114** if P3 is the same register as P2, the implementation is able
1115** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001116*/
drh5b6afba2008-01-05 16:29:28 +00001117case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001118 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001119
danielk1977a7a8e142008-02-13 18:25:27 +00001120 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001121 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001122 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001123 break;
drh5e00f6c2001-09-13 13:46:56 +00001124 }
drh5b6afba2008-01-05 16:29:28 +00001125 ExpandBlob(pIn1);
1126 Stringify(pIn1, encoding);
1127 ExpandBlob(pIn2);
1128 Stringify(pIn2, encoding);
1129 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001130 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001131 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001132 }
danielk1977a7a8e142008-02-13 18:25:27 +00001133 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001134 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001135 goto no_mem;
1136 }
danielk1977a7a8e142008-02-13 18:25:27 +00001137 if( pOut!=pIn2 ){
1138 memcpy(pOut->z, pIn2->z, pIn2->n);
1139 }
1140 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1141 pOut->z[nByte] = 0;
1142 pOut->z[nByte+1] = 0;
1143 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001144 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001145 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001146 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001147 break;
1148}
drh75897232000-05-29 14:26:00 +00001149
drh3c84ddf2008-01-09 02:15:38 +00001150/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001151**
drh60a713c2008-01-21 16:22:45 +00001152** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001153** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001154** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001155*/
drh3c84ddf2008-01-09 02:15:38 +00001156/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001157**
drh3c84ddf2008-01-09 02:15:38 +00001158**
shane21e7feb2008-05-30 15:59:49 +00001159** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001160** and store the result in register P3.
1161** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001162*/
drh3c84ddf2008-01-09 02:15:38 +00001163/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001164**
drh60a713c2008-01-21 16:22:45 +00001165** Subtract the value in register P1 from the value in register P2
1166** and store the result in register P3.
1167** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001168*/
drh9cbf3422008-01-17 16:22:13 +00001169/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001170**
drh60a713c2008-01-21 16:22:45 +00001171** Divide the value in register P1 by the value in register P2
1172** and store the result in register P3. If the value in register P2
1173** is zero, then the result is NULL.
1174** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001175*/
drh9cbf3422008-01-17 16:22:13 +00001176/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001177**
drh3c84ddf2008-01-09 02:15:38 +00001178** Compute the remainder after integer division of the value in
1179** register P1 by the value in register P2 and store the result in P3.
1180** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001181** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001182*/
drh5b6afba2008-01-05 16:29:28 +00001183case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1184case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1185case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1186case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1187case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh8a512562005-11-14 22:29:05 +00001188 int flags;
drh61669b32008-07-30 13:27:10 +00001189 applyNumericAffinity(pIn1);
1190 applyNumericAffinity(pIn2);
drh5b6afba2008-01-05 16:29:28 +00001191 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001192 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1193 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001194 i64 a, b;
drh5b6afba2008-01-05 16:29:28 +00001195 a = pIn1->u.i;
1196 b = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001197 switch( pOp->opcode ){
1198 case OP_Add: b += a; break;
1199 case OP_Subtract: b -= a; break;
1200 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001201 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001202 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001203 /* Dividing the largest possible negative 64-bit integer (1<<63) by
drh0f050352008-05-09 18:03:13 +00001204 ** -1 returns an integer too large to store in a 64-bit data-type. On
danielk197742d4ef22007-06-26 11:13:25 +00001205 ** some architectures, the value overflows to (1<<63). On others,
1206 ** a SIGFPE is issued. The following statement normalizes this
shane21e7feb2008-05-30 15:59:49 +00001207 ** behavior so that all architectures behave as if integer
1208 ** overflow occurred.
danielk197742d4ef22007-06-26 11:13:25 +00001209 */
drh0f050352008-05-09 18:03:13 +00001210 if( a==-1 && b==SMALLEST_INT64 ) a = 1;
drh5e00f6c2001-09-13 13:46:56 +00001211 b /= a;
drh75897232000-05-29 14:26:00 +00001212 break;
1213 }
drhbf4133c2001-10-13 02:59:08 +00001214 default: {
drha05a7222008-01-19 03:35:58 +00001215 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001216 if( a==-1 ) a = 1;
drhbf4133c2001-10-13 02:59:08 +00001217 b %= a;
1218 break;
1219 }
drh75897232000-05-29 14:26:00 +00001220 }
drh5b6afba2008-01-05 16:29:28 +00001221 pOut->u.i = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001222 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001223 }else{
1224 double a, b;
drh5b6afba2008-01-05 16:29:28 +00001225 a = sqlite3VdbeRealValue(pIn1);
1226 b = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001227 switch( pOp->opcode ){
1228 case OP_Add: b += a; break;
1229 case OP_Subtract: b -= a; break;
1230 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001231 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001232 if( a==0.0 ) goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001233 b /= a;
1234 break;
1235 }
drhbf4133c2001-10-13 02:59:08 +00001236 default: {
danielk19774b5710e2007-05-08 13:57:34 +00001237 i64 ia = (i64)a;
1238 i64 ib = (i64)b;
drha05a7222008-01-19 03:35:58 +00001239 if( ia==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001240 if( ia==-1 ) ia = 1;
drh9c1905f2008-12-10 22:32:56 +00001241 b = (double)(ib % ia);
drhbf4133c2001-10-13 02:59:08 +00001242 break;
1243 }
drh5e00f6c2001-09-13 13:46:56 +00001244 }
drh0de3ae92008-04-28 16:55:26 +00001245 if( sqlite3IsNaN(b) ){
drha05a7222008-01-19 03:35:58 +00001246 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001247 }
drh5b6afba2008-01-05 16:29:28 +00001248 pOut->r = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001249 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001250 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001251 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001252 }
drh5e00f6c2001-09-13 13:46:56 +00001253 }
1254 break;
1255
drha05a7222008-01-19 03:35:58 +00001256arithmetic_result_is_null:
1257 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001258 break;
1259}
1260
drh66a51672008-01-03 00:01:23 +00001261/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001262**
drh66a51672008-01-03 00:01:23 +00001263** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001264** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1265** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001266** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001267**
1268** The interface used by the implementation of the aforementioned functions
1269** to retrieve the collation sequence set by this opcode is not available
1270** publicly, only to user functions defined in func.c.
1271*/
drh9cbf3422008-01-17 16:22:13 +00001272case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001273 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001274 break;
1275}
1276
drh98757152008-01-09 23:04:12 +00001277/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001278**
drh66a51672008-01-03 00:01:23 +00001279** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001280** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001281** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001282** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001283**
drh13449892005-09-07 21:22:45 +00001284** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001285** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001286** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001287** whether meta data associated with a user function argument using the
1288** sqlite3_set_auxdata() API may be safely retained until the next
1289** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001290**
drh13449892005-09-07 21:22:45 +00001291** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001292*/
drh0bce8352002-02-28 00:41:10 +00001293case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001294 int i;
drh6810ce62004-01-31 19:22:56 +00001295 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001296 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001297 sqlite3_value **apVal;
drh98757152008-01-09 23:04:12 +00001298 int n = pOp->p5;
drh1350b032002-02-27 19:00:20 +00001299
danielk19776ddcca52004-05-24 23:48:25 +00001300 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001301 assert( apVal || n==0 );
1302
drh9cbf3422008-01-17 16:22:13 +00001303 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) );
danielk1977a7a8e142008-02-13 18:25:27 +00001304 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh9cbf3422008-01-17 16:22:13 +00001305 pArg = &p->aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001306 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001307 apVal[i] = pArg;
drh8079a0d2006-01-12 17:20:50 +00001308 storeTypeInfo(pArg, encoding);
drh2dcef112008-01-12 19:03:48 +00001309 REGISTER_TRACE(pOp->p2, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001310 }
danielk197751ad0ec2004-05-24 12:39:02 +00001311
drh66a51672008-01-03 00:01:23 +00001312 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1313 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001314 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001315 ctx.pVdbeFunc = 0;
1316 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001317 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001318 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1319 }
1320
danielk1977a7a8e142008-02-13 18:25:27 +00001321 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1322 pOut = &p->aMem[pOp->p3];
drh00706be2004-01-30 14:49:16 +00001323 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001324 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001325 ctx.s.xDel = 0;
1326 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001327
1328 /* The output cell may already have a buffer allocated. Move
1329 ** the pointer to ctx.s so in case the user-function can use
1330 ** the already allocated buffer instead of allocating a new one.
1331 */
1332 sqlite3VdbeMemMove(&ctx.s, pOut);
1333 MemSetTypeFlag(&ctx.s, MEM_Null);
1334
drh8e0a2f92002-02-23 23:45:45 +00001335 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001336 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001337 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00001338 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001339 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001340 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001341 }
danielk19774adee202004-05-08 08:23:19 +00001342 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001343 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk197775eb0162008-03-28 19:16:33 +00001344 if( sqlite3SafetyOn(db) ){
1345 sqlite3VdbeMemRelease(&ctx.s);
1346 goto abort_due_to_misuse;
1347 }
drh17435752007-08-16 04:30:38 +00001348 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001349 /* Even though a malloc() has failed, the implementation of the
1350 ** user function may have called an sqlite3_result_XXX() function
1351 ** to return a value. The following call releases any resources
1352 ** associated with such a value.
1353 **
1354 ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1355 ** fails also (the if(...) statement above). But if people are
1356 ** misusing sqlite, they have bigger problems than a leaked value.
1357 */
1358 sqlite3VdbeMemRelease(&ctx.s);
1359 goto no_mem;
1360 }
danielk19777e18c252004-05-25 11:47:24 +00001361
shane21e7feb2008-05-30 15:59:49 +00001362 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001363 ** immediately call the destructor for any non-static values.
1364 */
1365 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001366 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001367 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001368 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001369 }
1370
drh90669c12006-01-20 15:45:36 +00001371 /* If the function returned an error, throw an exception */
1372 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001373 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001374 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001375 }
1376
drh9cbf3422008-01-17 16:22:13 +00001377 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001378 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001379 sqlite3VdbeMemMove(pOut, &ctx.s);
1380 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001381 goto too_big;
1382 }
drh2dcef112008-01-12 19:03:48 +00001383 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001384 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001385 break;
1386}
1387
drh98757152008-01-09 23:04:12 +00001388/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001389**
drh98757152008-01-09 23:04:12 +00001390** Take the bit-wise AND of the values in register P1 and P2 and
1391** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001392** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001393*/
drh98757152008-01-09 23:04:12 +00001394/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001395**
drh98757152008-01-09 23:04:12 +00001396** Take the bit-wise OR of the values in register P1 and P2 and
1397** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001398** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001399*/
drh98757152008-01-09 23:04:12 +00001400/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001401**
drh98757152008-01-09 23:04:12 +00001402** Shift the integer value in register P2 to the left by the
drh60a713c2008-01-21 16:22:45 +00001403** number of bits specified by the integer in regiser P1.
drh98757152008-01-09 23:04:12 +00001404** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001405** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001406*/
drh98757152008-01-09 23:04:12 +00001407/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001408**
drh98757152008-01-09 23:04:12 +00001409** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001410** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001411** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001412** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001413*/
drh5b6afba2008-01-05 16:29:28 +00001414case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1415case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1416case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1417case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drhb1276122005-10-29 15:48:30 +00001418 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001419
drh5b6afba2008-01-05 16:29:28 +00001420 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001421 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001422 break;
1423 }
drh5b6afba2008-01-05 16:29:28 +00001424 a = sqlite3VdbeIntValue(pIn2);
1425 b = sqlite3VdbeIntValue(pIn1);
drhbf4133c2001-10-13 02:59:08 +00001426 switch( pOp->opcode ){
1427 case OP_BitAnd: a &= b; break;
1428 case OP_BitOr: a |= b; break;
1429 case OP_ShiftLeft: a <<= b; break;
drha05a7222008-01-19 03:35:58 +00001430 default: assert( pOp->opcode==OP_ShiftRight );
1431 a >>= b; break;
drhbf4133c2001-10-13 02:59:08 +00001432 }
drh5b6afba2008-01-05 16:29:28 +00001433 pOut->u.i = a;
danielk1977a7a8e142008-02-13 18:25:27 +00001434 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001435 break;
1436}
1437
drh8558cde2008-01-05 05:20:10 +00001438/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001439**
danielk19770cdc0222008-06-26 18:04:03 +00001440** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001441** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001442**
drh8558cde2008-01-05 05:20:10 +00001443** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001444*/
drh9cbf3422008-01-17 16:22:13 +00001445case OP_AddImm: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001446 sqlite3VdbeMemIntegerify(pIn1);
1447 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001448 break;
1449}
1450
drh9cbf3422008-01-17 16:22:13 +00001451/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001452**
drh9cbf3422008-01-17 16:22:13 +00001453** Force the value in register P1 to be an integer. If the value
1454** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001455** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001456** raise an SQLITE_MISMATCH exception.
1457*/
drh9cbf3422008-01-17 16:22:13 +00001458case OP_MustBeInt: { /* jump, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001459 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1460 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001461 if( pOp->p2==0 ){
1462 rc = SQLITE_MISMATCH;
1463 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001464 }else{
drh17c40292004-07-21 02:53:29 +00001465 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001466 }
drh8aff1012001-12-22 14:49:24 +00001467 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001468 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001469 }
1470 break;
1471}
1472
drh8558cde2008-01-05 05:20:10 +00001473/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001474**
drh2133d822008-01-03 18:44:59 +00001475** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001476**
drh8a512562005-11-14 22:29:05 +00001477** This opcode is used when extracting information from a column that
1478** has REAL affinity. Such column values may still be stored as
1479** integers, for space efficiency, but after extraction we want them
1480** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001481*/
drh9cbf3422008-01-17 16:22:13 +00001482case OP_RealAffinity: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001483 if( pIn1->flags & MEM_Int ){
1484 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001485 }
drh487e2622005-06-25 18:42:14 +00001486 break;
1487}
1488
drh8df447f2005-11-01 15:48:24 +00001489#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001490/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001491**
drh8558cde2008-01-05 05:20:10 +00001492** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001493** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001494** equivalent of printf(). Blob values are unchanged and
1495** are afterwards simply interpreted as text.
1496**
1497** A NULL value is not changed by this routine. It remains NULL.
1498*/
drh9cbf3422008-01-17 16:22:13 +00001499case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh8558cde2008-01-05 05:20:10 +00001500 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001501 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001502 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1503 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1504 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001505 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh8558cde2008-01-05 05:20:10 +00001506 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
drhb7654112008-01-12 12:48:07 +00001507 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001508 break;
1509}
1510
drh8558cde2008-01-05 05:20:10 +00001511/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001512**
drh8558cde2008-01-05 05:20:10 +00001513** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001514** If the value is numeric, convert it to a string first.
1515** Strings are simply reinterpreted as blobs with no change
1516** to the underlying data.
1517**
1518** A NULL value is not changed by this routine. It remains NULL.
1519*/
drh9cbf3422008-01-17 16:22:13 +00001520case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh8558cde2008-01-05 05:20:10 +00001521 if( pIn1->flags & MEM_Null ) break;
1522 if( (pIn1->flags & MEM_Blob)==0 ){
1523 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001524 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh487e2622005-06-25 18:42:14 +00001525 }
danielk1977a7a8e142008-02-13 18:25:27 +00001526 MemSetTypeFlag(pIn1, MEM_Blob);
drhb7654112008-01-12 12:48:07 +00001527 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001528 break;
1529}
drh8a512562005-11-14 22:29:05 +00001530
drh8558cde2008-01-05 05:20:10 +00001531/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001532**
drh8558cde2008-01-05 05:20:10 +00001533** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001534** integer or a floating-point number.)
1535** If the value is text or blob, try to convert it to an using the
1536** equivalent of atoi() or atof() and store 0 if no such conversion
1537** is possible.
1538**
1539** A NULL value is not changed by this routine. It remains NULL.
1540*/
drh9cbf3422008-01-17 16:22:13 +00001541case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh8558cde2008-01-05 05:20:10 +00001542 if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1543 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001544 }
1545 break;
1546}
1547#endif /* SQLITE_OMIT_CAST */
1548
drh8558cde2008-01-05 05:20:10 +00001549/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001550**
drh8558cde2008-01-05 05:20:10 +00001551** Force the value in register P1 be an integer. If
drh8a512562005-11-14 22:29:05 +00001552** The value is currently a real number, drop its fractional part.
1553** If the value is text or blob, try to convert it to an integer using the
1554** equivalent of atoi() and store 0 if no such conversion is possible.
1555**
1556** A NULL value is not changed by this routine. It remains NULL.
1557*/
drh9cbf3422008-01-17 16:22:13 +00001558case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh8558cde2008-01-05 05:20:10 +00001559 if( (pIn1->flags & MEM_Null)==0 ){
1560 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001561 }
1562 break;
1563}
1564
1565#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001566/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001567**
drh8558cde2008-01-05 05:20:10 +00001568** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001569** If The value is currently an integer, convert it.
1570** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001571** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001572**
1573** A NULL value is not changed by this routine. It remains NULL.
1574*/
drh9cbf3422008-01-17 16:22:13 +00001575case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh8558cde2008-01-05 05:20:10 +00001576 if( (pIn1->flags & MEM_Null)==0 ){
1577 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001578 }
1579 break;
1580}
drh487e2622005-06-25 18:42:14 +00001581#endif /* SQLITE_OMIT_CAST */
1582
drh35573352008-01-08 23:54:25 +00001583/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001584**
drh35573352008-01-08 23:54:25 +00001585** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1586** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001587**
drh35573352008-01-08 23:54:25 +00001588** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1589** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1590** bit is clear then fall thru if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001591**
drh35573352008-01-08 23:54:25 +00001592** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001593** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001594** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001595** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001596** affinity is used. Note that the affinity conversions are stored
1597** back into the input registers P1 and P3. So this opcode can cause
1598** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001599**
1600** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001601** the values are compared. If both values are blobs then memcmp() is
1602** used to determine the results of the comparison. If both values
1603** are text, then the appropriate collating function specified in
1604** P4 is used to do the comparison. If P4 is not specified then
1605** memcmp() is used to compare text string. If both values are
1606** numeric, then a numeric comparison is used. If the two values
1607** are of different types, then numbers are considered less than
1608** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001609**
drh35573352008-01-08 23:54:25 +00001610** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1611** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001612*/
drh9cbf3422008-01-17 16:22:13 +00001613/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001614**
drh35573352008-01-08 23:54:25 +00001615** This works just like the Lt opcode except that the jump is taken if
1616** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001617** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001618*/
drh9cbf3422008-01-17 16:22:13 +00001619/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001620**
drh35573352008-01-08 23:54:25 +00001621** This works just like the Lt opcode except that the jump is taken if
1622** the operands in registers P1 and P3 are equal.
1623** See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001624*/
drh9cbf3422008-01-17 16:22:13 +00001625/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001626**
drh35573352008-01-08 23:54:25 +00001627** This works just like the Lt opcode except that the jump is taken if
1628** the content of register P3 is less than or equal to the content of
1629** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001630*/
drh9cbf3422008-01-17 16:22:13 +00001631/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001632**
drh35573352008-01-08 23:54:25 +00001633** This works just like the Lt opcode except that the jump is taken if
1634** the content of register P3 is greater than the content of
1635** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001636*/
drh9cbf3422008-01-17 16:22:13 +00001637/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001638**
drh35573352008-01-08 23:54:25 +00001639** This works just like the Lt opcode except that the jump is taken if
1640** the content of register P3 is greater than or equal to the content of
1641** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001642*/
drh9cbf3422008-01-17 16:22:13 +00001643case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1644case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1645case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1646case OP_Le: /* same as TK_LE, jump, in1, in3 */
1647case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1648case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
danielk1977a37cdde2004-05-16 11:15:36 +00001649 int flags;
1650 int res;
1651 char affinity;
1652
drh35573352008-01-08 23:54:25 +00001653 flags = pIn1->flags|pIn3->flags;
danielk1977a37cdde2004-05-16 11:15:36 +00001654
danielk1977a37cdde2004-05-16 11:15:36 +00001655 if( flags&MEM_Null ){
drh93a960a2008-07-10 00:32:42 +00001656 /* If either operand is NULL then the result is always NULL.
1657 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1658 */
1659 if( pOp->p5 & SQLITE_STOREP2 ){
1660 pOut = &p->aMem[pOp->p2];
1661 MemSetTypeFlag(pOut, MEM_Null);
1662 REGISTER_TRACE(pOp->p2, pOut);
1663 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1664 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001665 }
drh93a960a2008-07-10 00:32:42 +00001666 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001667 }
1668
drh35573352008-01-08 23:54:25 +00001669 affinity = pOp->p5 & SQLITE_AFF_MASK;
drhe51c44f2004-05-30 20:46:09 +00001670 if( affinity ){
drh35573352008-01-08 23:54:25 +00001671 applyAffinity(pIn1, affinity, encoding);
1672 applyAffinity(pIn3, affinity, encoding);
drhbbce3382008-12-06 16:46:13 +00001673 if( db->mallocFailed ) goto no_mem;
drhe51c44f2004-05-30 20:46:09 +00001674 }
danielk1977a37cdde2004-05-16 11:15:36 +00001675
danielk19772dca4ac2008-01-03 11:50:29 +00001676 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh35573352008-01-08 23:54:25 +00001677 ExpandBlob(pIn1);
1678 ExpandBlob(pIn3);
1679 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
danielk1977a37cdde2004-05-16 11:15:36 +00001680 switch( pOp->opcode ){
1681 case OP_Eq: res = res==0; break;
1682 case OP_Ne: res = res!=0; break;
1683 case OP_Lt: res = res<0; break;
1684 case OP_Le: res = res<=0; break;
1685 case OP_Gt: res = res>0; break;
1686 default: res = res>=0; break;
1687 }
1688
drh35573352008-01-08 23:54:25 +00001689 if( pOp->p5 & SQLITE_STOREP2 ){
1690 pOut = &p->aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00001691 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001692 pOut->u.i = res;
1693 REGISTER_TRACE(pOp->p2, pOut);
1694 }else if( res ){
1695 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001696 }
1697 break;
1698}
drhc9b84a12002-06-20 11:36:48 +00001699
drh0acb7e42008-06-25 00:12:41 +00001700/* Opcode: Permutation * * * P4 *
1701**
1702** Set the permuation used by the OP_Compare operator to be the array
1703** of integers in P4.
1704**
1705** The permutation is only valid until the next OP_Permutation, OP_Compare,
1706** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1707** immediately prior to the OP_Compare.
1708*/
1709case OP_Permutation: {
1710 assert( pOp->p4type==P4_INTARRAY );
1711 assert( pOp->p4.ai );
1712 aPermute = pOp->p4.ai;
1713 break;
1714}
1715
drh16ee60f2008-06-20 18:13:25 +00001716/* Opcode: Compare P1 P2 P3 P4 *
1717**
1718** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1719** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1720** the comparison for use by the next OP_Jump instruct.
1721**
drh0acb7e42008-06-25 00:12:41 +00001722** P4 is a KeyInfo structure that defines collating sequences and sort
1723** orders for the comparison. The permutation applies to registers
1724** only. The KeyInfo elements are used sequentially.
1725**
1726** The comparison is a sort comparison, so NULLs compare equal,
1727** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001728** and strings are less than blobs.
1729*/
1730case OP_Compare: {
1731 int n = pOp->p3;
1732 int i, p1, p2;
1733 const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1734 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001735 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001736 p1 = pOp->p1;
1737 assert( p1>0 && p1+n-1<p->nMem );
1738 p2 = pOp->p2;
1739 assert( p2>0 && p2+n-1<p->nMem );
drh0acb7e42008-06-25 00:12:41 +00001740 for(i=0; i<n; i++){
1741 int idx = aPermute ? aPermute[i] : i;
1742 CollSeq *pColl; /* Collating sequence to use on this term */
1743 int bRev; /* True for DESCENDING sort order */
drh0acb7e42008-06-25 00:12:41 +00001744 REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
1745 REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001746 assert( i<pKeyInfo->nField );
1747 pColl = pKeyInfo->aColl[i];
1748 bRev = pKeyInfo->aSortOrder[i];
drh0acb7e42008-06-25 00:12:41 +00001749 iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
1750 if( iCompare ){
1751 if( bRev ) iCompare = -iCompare;
1752 break;
1753 }
drh16ee60f2008-06-20 18:13:25 +00001754 }
drh0acb7e42008-06-25 00:12:41 +00001755 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001756 break;
1757}
1758
1759/* Opcode: Jump P1 P2 P3 * *
1760**
1761** Jump to the instruction at address P1, P2, or P3 depending on whether
1762** in the most recent OP_Compare instruction the P1 vector was less than
1763** equal to, or greater than the P2 vector, respectively.
1764*/
drh0acb7e42008-06-25 00:12:41 +00001765case OP_Jump: { /* jump */
1766 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001767 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001768 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001769 pc = pOp->p2 - 1;
1770 }else{
1771 pc = pOp->p3 - 1;
1772 }
1773 break;
1774}
1775
drh5b6afba2008-01-05 16:29:28 +00001776/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001777**
drh5b6afba2008-01-05 16:29:28 +00001778** Take the logical AND of the values in registers P1 and P2 and
1779** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001780**
drh5b6afba2008-01-05 16:29:28 +00001781** If either P1 or P2 is 0 (false) then the result is 0 even if
1782** the other input is NULL. A NULL and true or two NULLs give
1783** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001784*/
drh5b6afba2008-01-05 16:29:28 +00001785/* Opcode: Or P1 P2 P3 * *
1786**
1787** Take the logical OR of the values in register P1 and P2 and
1788** store the answer in register P3.
1789**
1790** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1791** even if the other input is NULL. A NULL and false or two NULLs
1792** give a NULL output.
1793*/
1794case OP_And: /* same as TK_AND, in1, in2, out3 */
1795case OP_Or: { /* same as TK_OR, in1, in2, out3 */
1796 int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001797
drh5b6afba2008-01-05 16:29:28 +00001798 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001799 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001800 }else{
drh5b6afba2008-01-05 16:29:28 +00001801 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001802 }
drh5b6afba2008-01-05 16:29:28 +00001803 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001804 v2 = 2;
1805 }else{
drh5b6afba2008-01-05 16:29:28 +00001806 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001807 }
1808 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001809 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001810 v1 = and_logic[v1*3+v2];
1811 }else{
drh5b6afba2008-01-05 16:29:28 +00001812 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001813 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001814 }
drhbb113512002-05-27 01:04:51 +00001815 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001816 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001817 }else{
drh5b6afba2008-01-05 16:29:28 +00001818 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001819 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001820 }
drh5e00f6c2001-09-13 13:46:56 +00001821 break;
1822}
1823
drhe99fa2a2008-12-15 15:27:51 +00001824/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001825**
drhe99fa2a2008-12-15 15:27:51 +00001826** Interpret the value in register P1 as a boolean value. Store the
1827** boolean complement in register P2. If the value in register P1 is
1828** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001829*/
drh9cbf3422008-01-17 16:22:13 +00001830case OP_Not: { /* same as TK_NOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001831 pOut = &p->aMem[pOp->p2];
1832 if( pIn1->flags & MEM_Null ){
1833 sqlite3VdbeMemSetNull(pOut);
1834 }else{
1835 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1836 }
drh5e00f6c2001-09-13 13:46:56 +00001837 break;
1838}
1839
drhe99fa2a2008-12-15 15:27:51 +00001840/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00001841**
drhe99fa2a2008-12-15 15:27:51 +00001842** Interpret the content of register P1 as an integer. Store the
1843** ones-complement of the P1 value into register P2. If P1 holds
1844** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00001845*/
drh9cbf3422008-01-17 16:22:13 +00001846case OP_BitNot: { /* same as TK_BITNOT, in1 */
drhe99fa2a2008-12-15 15:27:51 +00001847 pOut = &p->aMem[pOp->p2];
1848 if( pIn1->flags & MEM_Null ){
1849 sqlite3VdbeMemSetNull(pOut);
1850 }else{
1851 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1852 }
drhbf4133c2001-10-13 02:59:08 +00001853 break;
1854}
1855
drh3c84ddf2008-01-09 02:15:38 +00001856/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001857**
drh3c84ddf2008-01-09 02:15:38 +00001858** Jump to P2 if the value in register P1 is true. The value is
1859** is considered true if it is numeric and non-zero. If the value
1860** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00001861*/
drh3c84ddf2008-01-09 02:15:38 +00001862/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00001863**
drh3c84ddf2008-01-09 02:15:38 +00001864** Jump to P2 if the value in register P1 is False. The value is
1865** is considered true if it has a numeric value of zero. If the value
1866** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00001867*/
drh9cbf3422008-01-17 16:22:13 +00001868case OP_If: /* jump, in1 */
1869case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00001870 int c;
drh3c84ddf2008-01-09 02:15:38 +00001871 if( pIn1->flags & MEM_Null ){
1872 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00001873 }else{
drhba0232a2005-06-06 17:27:19 +00001874#ifdef SQLITE_OMIT_FLOATING_POINT
drh3c84ddf2008-01-09 02:15:38 +00001875 c = sqlite3VdbeIntValue(pIn1);
drhba0232a2005-06-06 17:27:19 +00001876#else
drh3c84ddf2008-01-09 02:15:38 +00001877 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00001878#endif
drhf5905aa2002-05-26 20:54:33 +00001879 if( pOp->opcode==OP_IfNot ) c = !c;
1880 }
drh3c84ddf2008-01-09 02:15:38 +00001881 if( c ){
1882 pc = pOp->p2-1;
1883 }
drh5e00f6c2001-09-13 13:46:56 +00001884 break;
1885}
1886
drh2d401ab2008-01-10 23:50:11 +00001887/* Opcode: IsNull P1 P2 P3 * *
drh477df4b2008-01-05 18:48:24 +00001888**
drh2d401ab2008-01-10 23:50:11 +00001889** Jump to P2 if the value in register P1 is NULL. If P3 is greater
1890** than zero, then check all values reg(P1), reg(P1+1),
1891** reg(P1+2), ..., reg(P1+P3-1).
drh477df4b2008-01-05 18:48:24 +00001892*/
drh9cbf3422008-01-17 16:22:13 +00001893case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh2d401ab2008-01-10 23:50:11 +00001894 int n = pOp->p3;
1895 assert( pOp->p3==0 || pOp->p1>0 );
1896 do{
1897 if( (pIn1->flags & MEM_Null)!=0 ){
1898 pc = pOp->p2 - 1;
1899 break;
1900 }
1901 pIn1++;
1902 }while( --n > 0 );
drh477df4b2008-01-05 18:48:24 +00001903 break;
1904}
1905
drh98757152008-01-09 23:04:12 +00001906/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001907**
drh6a288a32008-01-07 19:20:24 +00001908** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00001909*/
drh9cbf3422008-01-17 16:22:13 +00001910case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh6a288a32008-01-07 19:20:24 +00001911 if( (pIn1->flags & MEM_Null)==0 ){
1912 pc = pOp->p2 - 1;
1913 }
drh5e00f6c2001-09-13 13:46:56 +00001914 break;
1915}
1916
danielk1977cd3e8f72008-03-25 09:47:35 +00001917/* Opcode: SetNumColumns * P2 * * *
danielk1977b4964b72004-05-18 01:23:38 +00001918**
danielk1977cd3e8f72008-03-25 09:47:35 +00001919** This opcode sets the number of columns for the cursor opened by the
1920** following instruction to P2.
danielk1977b4964b72004-05-18 01:23:38 +00001921**
danielk1977cd3e8f72008-03-25 09:47:35 +00001922** An OP_SetNumColumns is only useful if it occurs immediately before
1923** one of the following opcodes:
danielk1977ac171782005-02-05 06:49:54 +00001924**
danielk1977cd3e8f72008-03-25 09:47:35 +00001925** OpenRead
1926** OpenWrite
1927** OpenPseudo
1928**
1929** If the OP_Column opcode is to be executed on a cursor, then
1930** this opcode must be present immediately before the opcode that
1931** opens the cursor.
danielk1977b4964b72004-05-18 01:23:38 +00001932*/
drh9cbf3422008-01-17 16:22:13 +00001933case OP_SetNumColumns: {
danielk1977b4964b72004-05-18 01:23:38 +00001934 break;
1935}
1936
danielk197760585dd2008-01-03 08:08:40 +00001937/* Opcode: Column P1 P2 P3 P4 *
danielk1977192ac1d2004-05-10 07:17:30 +00001938**
danielk1977cfcdaef2004-05-12 07:33:33 +00001939** Interpret the data that cursor P1 points to as a structure built using
1940** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00001941** information about the format of the data.) Extract the P2-th column
1942** from this record. If there are less that (P2+1)
1943** values in the record, extract a NULL.
1944**
drh9cbf3422008-01-17 16:22:13 +00001945** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00001946**
danielk19771f4aa332008-01-03 09:51:55 +00001947** If the column contains fewer than P2 fields, then extract a NULL. Or,
1948** if the P4 argument is a P4_MEM use the value of the P4 argument as
1949** the result.
danielk1977192ac1d2004-05-10 07:17:30 +00001950*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001951case OP_Column: {
danielk197764202cf2008-11-17 15:31:47 +00001952 int payloadSize; /* Number of bytes in the record */
drhd3194f52004-05-27 19:59:32 +00001953 int p1 = pOp->p1; /* P1 value of the opcode */
danielk1977cfcdaef2004-05-12 07:33:33 +00001954 int p2 = pOp->p2; /* column number to retrieve */
drhdfe88ec2008-11-03 20:55:06 +00001955 VdbeCursor *pC = 0;/* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00001956 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00001957 BtCursor *pCrsr; /* The BTree cursor */
1958 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1959 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00001960 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00001961 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00001962 int i; /* Loop counter */
1963 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00001964 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00001965 Mem sMem; /* For storing the record being decoded */
danielk1977192ac1d2004-05-10 07:17:30 +00001966
drhb27b7f52008-12-10 18:03:45 +00001967 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00001968 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00001969 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1970 pDest = &p->aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001971 MemSetTypeFlag(pDest, MEM_Null);
danielk1977cfcdaef2004-05-12 07:33:33 +00001972
drhe61cffc2004-06-12 18:12:15 +00001973 /* This block sets the variable payloadSize to be the total number of
1974 ** bytes in the record.
1975 **
1976 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00001977 ** The complete record text is always available for pseudo-tables
1978 ** If the record is stored in a cursor, the complete record text
1979 ** might be available in the pC->aRow cache. Or it might not be.
1980 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00001981 **
1982 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00001983 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00001984 */
drhb73857f2006-03-17 00:25:59 +00001985 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00001986 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00001987#ifndef SQLITE_OMIT_VIRTUALTABLE
1988 assert( pC->pVtabCursor==0 );
1989#endif
drhb73857f2006-03-17 00:25:59 +00001990 if( pC->pCursor!=0 ){
drhe61cffc2004-06-12 18:12:15 +00001991 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00001992 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00001993 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00001994 zRec = 0;
1995 pCrsr = pC->pCursor;
1996 if( pC->nullRow ){
1997 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00001998 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00001999 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002000 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002001 }else if( pC->isIndex ){
danielk197796fc5fe2004-05-13 11:34:16 +00002002 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002003 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drh9c1905f2008-12-10 22:32:56 +00002004 payloadSize = (int)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002005 }else{
danielk197764202cf2008-11-17 15:31:47 +00002006 sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize);
danielk1977192ac1d2004-05-10 07:17:30 +00002007 }
drhd3194f52004-05-27 19:59:32 +00002008 nField = pC->nField;
drha05a7222008-01-19 03:35:58 +00002009 }else{
2010 assert( pC->pseudoTable );
drhe61cffc2004-06-12 18:12:15 +00002011 /* The record is the sole entry of a pseudo-table */
danielk1977192ac1d2004-05-10 07:17:30 +00002012 payloadSize = pC->nData;
2013 zRec = pC->pData;
drh76873ab2006-01-07 18:48:26 +00002014 pC->cacheStatus = CACHE_STALE;
danielk1977192ac1d2004-05-10 07:17:30 +00002015 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00002016 nField = pC->nField;
danielk1977f7df9cc2004-06-16 12:02:47 +00002017 pCrsr = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002018 }
2019
drh9cbf3422008-01-17 16:22:13 +00002020 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002021 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002022 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002023 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002024 }
drhbb4957f2008-03-20 14:03:29 +00002025 if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002026 goto too_big;
2027 }
danielk1977192ac1d2004-05-10 07:17:30 +00002028
drhd3194f52004-05-27 19:59:32 +00002029 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002030
drh9188b382004-05-14 21:12:22 +00002031 /* Read and parse the table header. Store the results of the parse
2032 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002033 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002034 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002035 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002036 aOffset = pC->aOffset;
2037 }else{
danielk1977dedf45b2006-01-13 17:12:01 +00002038 u8 *zIdx; /* Index into header */
2039 u8 *zEndHdr; /* Pointer to first byte after the header */
danielk197764202cf2008-11-17 15:31:47 +00002040 int offset; /* Offset into the data */
drh0ac07192006-02-10 14:02:07 +00002041 int szHdrSz; /* Size of the header size field at start of record */
drhb27b7f52008-12-10 18:03:45 +00002042 int avail = 0; /* Number of bytes of available data */
drhb73857f2006-03-17 00:25:59 +00002043
danielk1977cd3e8f72008-03-25 09:47:35 +00002044 assert(aType);
drhb73857f2006-03-17 00:25:59 +00002045 pC->aOffset = aOffset = &aType[nField];
2046 pC->payloadSize = payloadSize;
2047 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002048
drhd3194f52004-05-27 19:59:32 +00002049 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002050 if( zRec ){
2051 zData = zRec;
2052 }else{
drhf0863fe2005-06-12 21:35:51 +00002053 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002054 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002055 }else{
drhe51c44f2004-05-30 20:46:09 +00002056 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002057 }
drhe61cffc2004-06-12 18:12:15 +00002058 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2059 ** save the payload in the pC->aRow cache. That will save us from
2060 ** having to make additional calls to fetch the content portion of
2061 ** the record.
2062 */
2063 if( avail>=payloadSize ){
drh2646da72005-12-09 20:02:05 +00002064 zRec = zData;
2065 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002066 }else{
2067 pC->aRow = 0;
2068 }
drhd3194f52004-05-27 19:59:32 +00002069 }
drh588f5bc2007-01-02 18:41:54 +00002070 /* The following assert is true in all cases accept when
2071 ** the database file has been corrupted externally.
2072 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
shane3f8d5cf2008-04-24 19:15:09 +00002073 szHdrSz = getVarint32((u8*)zData, offset);
drhe61cffc2004-06-12 18:12:15 +00002074
2075 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2076 ** record header in most cases. But they will fail to get the complete
2077 ** record header if the record header does not fit on a single page
2078 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2079 ** acquire the complete header text.
2080 */
danielk1977dedf45b2006-01-13 17:12:01 +00002081 if( !zRec && avail<offset ){
danielk1977a7a8e142008-02-13 18:25:27 +00002082 sMem.flags = 0;
2083 sMem.db = 0;
drhb21c8cd2007-08-21 19:33:56 +00002084 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002085 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002086 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002087 }
drhb6f54522004-05-20 02:42:16 +00002088 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002089 }
drh0ac07192006-02-10 14:02:07 +00002090 zEndHdr = (u8 *)&zData[offset];
2091 zIdx = (u8 *)&zData[szHdrSz];
drh9188b382004-05-14 21:12:22 +00002092
drhd3194f52004-05-27 19:59:32 +00002093 /* Scan the header and use it to fill in the aType[] and aOffset[]
2094 ** arrays. aType[i] will contain the type integer for the i-th
2095 ** column and aOffset[i] will contain the offset from the beginning
2096 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002097 */
danielk1977dedf45b2006-01-13 17:12:01 +00002098 for(i=0; i<nField; i++){
2099 if( zIdx<zEndHdr ){
2100 aOffset[i] = offset;
shane3f8d5cf2008-04-24 19:15:09 +00002101 zIdx += getVarint32(zIdx, aType[i]);
danielk1977dedf45b2006-01-13 17:12:01 +00002102 offset += sqlite3VdbeSerialTypeLen(aType[i]);
2103 }else{
2104 /* If i is less that nField, then there are less fields in this
2105 ** record than SetNumColumns indicated there are columns in the
2106 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002107 ** the record to 0. This tells code below to store a NULL
2108 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002109 */
2110 aOffset[i] = 0;
2111 }
drh9188b382004-05-14 21:12:22 +00002112 }
danielk19775f096132008-03-28 15:44:09 +00002113 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002114 sMem.flags = MEM_Null;
2115
danielk19779792eef2006-01-13 15:58:43 +00002116 /* If we have read more header data than was contained in the header,
2117 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002118 ** record, or if the end of the last field appears to be before the end
2119 ** of the record (when all fields present), then we must be dealing
2120 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002121 */
danielk1977fb8f2e22008-09-22 06:13:31 +00002122 if( zIdx>zEndHdr || offset>payloadSize
2123 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002124 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002125 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002126 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002127 }
danielk1977192ac1d2004-05-10 07:17:30 +00002128
danielk197736963fd2005-02-19 08:18:05 +00002129 /* Get the column information. If aOffset[p2] is non-zero, then
2130 ** deserialize the value from the record. If aOffset[p2] is zero,
2131 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002132 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002133 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002134 */
danielk197736963fd2005-02-19 08:18:05 +00002135 if( aOffset[p2] ){
2136 assert( rc==SQLITE_OK );
2137 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002138 sqlite3VdbeMemReleaseExternal(pDest);
2139 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002140 }else{
2141 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002142 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002143 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002144 if( rc!=SQLITE_OK ){
2145 goto op_column_out;
2146 }
2147 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002148 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002149 }
drhd4e70eb2008-01-02 00:34:36 +00002150 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002151 }else{
danielk197760585dd2008-01-03 08:08:40 +00002152 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002153 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002154 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002155 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002156 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002157 }
drhfebe1062004-08-28 18:17:48 +00002158
2159 /* If we dynamically allocated space to hold the data (in the
2160 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002161 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002162 ** This prevents a memory copy.
2163 */
danielk19775f096132008-03-28 15:44:09 +00002164 if( sMem.zMalloc ){
2165 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002166 assert( !(pDest->flags & MEM_Dyn) );
2167 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2168 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002169 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002170 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002171 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002172 }
drhfebe1062004-08-28 18:17:48 +00002173
drhd4e70eb2008-01-02 00:34:36 +00002174 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002175
danielk19773c9cc8d2005-01-17 03:40:08 +00002176op_column_out:
drhb7654112008-01-12 12:48:07 +00002177 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002178 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002179 break;
2180}
2181
danielk1977751de562008-04-18 09:01:15 +00002182/* Opcode: Affinity P1 P2 * P4 *
2183**
2184** Apply affinities to a range of P2 registers starting with P1.
2185**
2186** P4 is a string that is P2 characters long. The nth character of the
2187** string indicates the column affinity that should be used for the nth
2188** memory cell in the range.
2189*/
2190case OP_Affinity: {
2191 char *zAffinity = pOp->p4.z;
2192 Mem *pData0 = &p->aMem[pOp->p1];
2193 Mem *pLast = &pData0[pOp->p2-1];
2194 Mem *pRec;
2195
2196 for(pRec=pData0; pRec<=pLast; pRec++){
danielk1977b790c6c2008-04-18 10:25:24 +00002197 ExpandBlob(pRec);
danielk1977751de562008-04-18 09:01:15 +00002198 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2199 }
2200 break;
2201}
2202
drh1db639c2008-01-17 02:36:28 +00002203/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002204**
drh1db639c2008-01-17 02:36:28 +00002205** Convert P2 registers beginning with P1 into a single entry
drh7a224de2004-06-02 01:22:02 +00002206** suitable for use as a data record in a database table or as a key
shane21e7feb2008-05-30 15:59:49 +00002207** in an index. The details of the format are irrelevant as long as
drh1e968a02008-03-25 00:22:21 +00002208** the OP_Column opcode can decode the record later.
2209** Refer to source code comments for the details of the record
drh7a224de2004-06-02 01:22:02 +00002210** format.
2211**
danielk1977751de562008-04-18 09:01:15 +00002212** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002213** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002214** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002215**
drh8a512562005-11-14 22:29:05 +00002216** The mapping from character to affinity is given by the SQLITE_AFF_
2217** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002218**
drh66a51672008-01-03 00:01:23 +00002219** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002220*/
drh1db639c2008-01-17 02:36:28 +00002221case OP_MakeRecord: {
drhf3218fe2004-05-28 08:21:02 +00002222 /* Assuming the record contains N fields, the record format looks
2223 ** like this:
2224 **
drh7a224de2004-06-02 01:22:02 +00002225 ** ------------------------------------------------------------------------
2226 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2227 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002228 **
drh9cbf3422008-01-17 16:22:13 +00002229 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2230 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002231 **
2232 ** Each type field is a varint representing the serial type of the
2233 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002234 ** hdr-size field is also a varint which is the offset from the beginning
2235 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002236 */
drhfdf972a2007-05-02 13:30:27 +00002237 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2238 Mem *pRec; /* The new record */
drh023ae032007-05-08 12:12:16 +00002239 u64 nData = 0; /* Number of bytes of data space */
danielk1977ededfd52004-06-17 07:53:01 +00002240 int nHdr = 0; /* Number of bytes of header space */
danielk197764202cf2008-11-17 15:31:47 +00002241 i64 nByte = 0; /* Data space required for this record */
drhfdf972a2007-05-02 13:30:27 +00002242 int nZero = 0; /* Number of zero bytes at the end of the record */
drhcb9882a2005-03-17 03:15:40 +00002243 int nVarint; /* Number of bytes in a varint */
danielk1977ededfd52004-06-17 07:53:01 +00002244 u32 serial_type; /* Type field */
drh1db639c2008-01-17 02:36:28 +00002245 Mem *pData0; /* First field to be combined into the record */
2246 Mem *pLast; /* Last field of the record */
danielk1977ededfd52004-06-17 07:53:01 +00002247 int nField; /* Number of fields in the record */
danielk1977ededfd52004-06-17 07:53:01 +00002248 char *zAffinity; /* The affinity string for the record */
drhd946db02005-12-29 19:23:06 +00002249 int file_format; /* File format to use for encoding */
drhfdf972a2007-05-02 13:30:27 +00002250 int i; /* Space used in zNewRecord[] */
danielk1977ededfd52004-06-17 07:53:01 +00002251
drh1db639c2008-01-17 02:36:28 +00002252 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002253 zAffinity = pOp->p4.z;
drh1db639c2008-01-17 02:36:28 +00002254 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem );
2255 pData0 = &p->aMem[nField];
2256 nField = pOp->p2;
2257 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002258 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002259
drhf3218fe2004-05-28 08:21:02 +00002260 /* Loop through the elements that will make up the record to figure
2261 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002262 */
drha2a49dc2008-01-02 14:28:13 +00002263 for(pRec=pData0; pRec<=pLast; pRec++){
drhae7e1512007-05-02 16:51:59 +00002264 int len;
drhd3d39e92004-05-20 22:16:29 +00002265 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002266 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002267 }
danielk1977d908f5a2007-05-11 07:08:28 +00002268 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002269 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002270 }
drhd946db02005-12-29 19:23:06 +00002271 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002272 len = sqlite3VdbeSerialTypeLen(serial_type);
2273 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002274 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002275 if( pRec->flags & MEM_Zero ){
2276 /* Only pure zero-filled BLOBs can be input to this Opcode.
2277 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002278 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002279 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002280 nZero = 0;
2281 }
danielk19778d059842004-05-12 11:24:02 +00002282 }
danielk19773d1bfea2004-05-14 11:00:53 +00002283
drhf3218fe2004-05-28 08:21:02 +00002284 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002285 nHdr += nVarint = sqlite3VarintLen(nHdr);
2286 if( nVarint<sqlite3VarintLen(nHdr) ){
2287 nHdr++;
2288 }
drhfdf972a2007-05-02 13:30:27 +00002289 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002290 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002291 goto too_big;
2292 }
drhf3218fe2004-05-28 08:21:02 +00002293
danielk1977a7a8e142008-02-13 18:25:27 +00002294 /* Make sure the output register has a buffer large enough to store
2295 ** the new record. The output register (pOp->p3) is not allowed to
2296 ** be one of the input registers (because the following call to
2297 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2298 */
2299 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2300 pOut = &p->aMem[pOp->p3];
drh9c1905f2008-12-10 22:32:56 +00002301 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002302 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002303 }
danielk1977a7a8e142008-02-13 18:25:27 +00002304 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002305
2306 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002307 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002308 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002309 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002310 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002311 }
drha2a49dc2008-01-02 14:28:13 +00002312 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002313 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002314 }
drhfdf972a2007-05-02 13:30:27 +00002315 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002316
drh9cbf3422008-01-17 16:22:13 +00002317 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002318 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002319 pOut->flags = MEM_Blob | MEM_Dyn;
2320 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002321 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002322 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002323 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002324 }
drh477df4b2008-01-05 18:48:24 +00002325 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002326 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002327 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002328 break;
2329}
2330
drh98757152008-01-09 23:04:12 +00002331/* Opcode: Statement P1 * * * *
drh663fc632002-02-02 18:49:19 +00002332**
drh7f0f12e2004-05-21 13:39:50 +00002333** Begin an individual statement transaction which is part of a larger
drh82ed1e52008-04-25 12:25:42 +00002334** transaction. This is needed so that the statement
drh7f0f12e2004-05-21 13:39:50 +00002335** can be rolled back after an error without having to roll back the
2336** entire transaction. The statement transaction will automatically
2337** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002338**
drh82ed1e52008-04-25 12:25:42 +00002339** If the database connection is currently in autocommit mode (that
2340** is to say, if it is in between BEGIN and COMMIT)
2341** and if there are no other active statements on the same database
2342** connection, then this operation is a no-op. No statement transaction
2343** is needed since any error can use the normal ROLLBACK process to
2344** undo changes.
2345**
2346** If a statement transaction is started, then a statement journal file
2347** will be allocated and initialized.
2348**
drh7f0f12e2004-05-21 13:39:50 +00002349** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002350** database file has an index of 0 and the file used for temporary tables
2351** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002352*/
drh9cbf3422008-01-17 16:22:13 +00002353case OP_Statement: {
drha05a7222008-01-19 03:35:58 +00002354 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
2355 int i = pOp->p1;
2356 Btree *pBt;
2357 assert( i>=0 && i<db->nDb );
2358 assert( db->aDb[i].pBt!=0 );
2359 pBt = db->aDb[i].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002360 assert( sqlite3BtreeIsInTrans(pBt) );
drhfb982642007-08-30 01:19:59 +00002361 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002362 if( !sqlite3BtreeIsInStmt(pBt) ){
2363 rc = sqlite3BtreeBeginStmt(pBt);
danielk1977182c4ba2007-06-27 15:53:34 +00002364 p->openedStatement = 1;
danielk19771d850a72004-05-31 08:26:49 +00002365 }
2366 }
2367 break;
2368}
2369
danielk1977fd7f0452008-12-17 17:30:26 +00002370/* Opcode: Savepoint P1 * * P4 *
2371**
2372** Open, release or rollback the savepoint named by parameter P4, depending
2373** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2374** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2375*/
2376case OP_Savepoint: {
2377 int p1 = pOp->p1;
2378 char *zName = pOp->p4.z; /* Name of savepoint */
2379
2380 /* Assert that the p1 parameter is valid. Also that if there is no open
2381 ** transaction, then there cannot be any savepoints.
2382 */
2383 assert( db->pSavepoint==0 || db->autoCommit==0 );
2384 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2385 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2386 assert( checkSavepointCount(db) );
2387
2388 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002389 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002390 /* A new savepoint cannot be created if there are active write
2391 ** statements (i.e. open read/write incremental blob handles).
2392 */
2393 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2394 "SQL statements in progress");
2395 rc = SQLITE_BUSY;
2396 }else{
2397 int nName = sqlite3Strlen30(zName);
2398 Savepoint *pNew;
2399
2400 /* Create a new savepoint structure. */
2401 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2402 if( pNew ){
2403 pNew->zName = (char *)&pNew[1];
2404 memcpy(pNew->zName, zName, nName+1);
2405
2406 /* If there is no open transaction, then mark this as a special
2407 ** "transaction savepoint". */
2408 if( db->autoCommit ){
2409 db->autoCommit = 0;
2410 db->isTransactionSavepoint = 1;
2411 }else{
2412 db->nSavepoint++;
2413 }
2414
2415 /* Link the new savepoint into the database handle's list. */
2416 pNew->pNext = db->pSavepoint;
2417 db->pSavepoint = pNew;
2418 }
2419 }
2420 }else{
2421 Savepoint *pSavepoint;
2422 int iSavepoint = 0;
2423
2424 /* Find the named savepoint. If there is no such savepoint, then an
2425 ** an error is returned to the user. */
2426 for(
2427 pSavepoint=db->pSavepoint;
2428 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
2429 pSavepoint=pSavepoint->pNext
2430 ){
2431 iSavepoint++;
2432 }
2433 if( !pSavepoint ){
2434 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2435 rc = SQLITE_ERROR;
2436 }else if(
2437 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2438 ){
2439 /* It is not possible to release (commit) a savepoint if there are
2440 ** active write statements. It is not possible to rollback a savepoint
2441 ** if there are any active statements at all.
2442 */
2443 sqlite3SetString(&p->zErrMsg, db,
2444 "cannot %s savepoint - SQL statements in progress",
2445 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2446 );
2447 rc = SQLITE_BUSY;
2448 }else{
2449
2450 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002451 ** and this is a RELEASE command, then the current transaction
2452 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002453 */
2454 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2455 if( isTransaction && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002456 db->autoCommit = 1;
2457 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2458 p->pc = pc;
2459 db->autoCommit = 0;
2460 p->rc = rc = SQLITE_BUSY;
2461 goto vdbe_return;
2462 }
danielk197734cf35d2008-12-18 18:31:38 +00002463 db->isTransactionSavepoint = 0;
2464 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002465 }else{
2466 int ii;
2467 iSavepoint = db->nSavepoint - iSavepoint - 1;
2468 for(ii=0; ii<db->nDb; ii++){
2469 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2470 if( rc!=SQLITE_OK ){
2471 goto abort_due_to_error;
2472 }
2473 }
2474 if( p1==SAVEPOINT_ROLLBACK && db->flags&SQLITE_InternChanges ){
2475 sqlite3ExpirePreparedStatements(db);
2476 sqlite3ResetInternalSchema(db, 0);
2477 }
2478 }
2479
2480 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2481 ** savepoints nested inside of the savepoint being operated on. */
2482 while( db->pSavepoint!=pSavepoint ){
2483 Savepoint *pTmp = db->pSavepoint;
2484 db->pSavepoint = pTmp->pNext;
2485 sqlite3DbFree(db, pTmp);
2486 db->nSavepoint--;
2487 }
2488
2489 /* If it is a RELEASE, then destroy the savepoint being operated on too */
2490 if( p1==SAVEPOINT_RELEASE ){
2491 assert( pSavepoint==db->pSavepoint );
2492 db->pSavepoint = pSavepoint->pNext;
2493 sqlite3DbFree(db, pSavepoint);
2494 if( !isTransaction ){
2495 db->nSavepoint--;
2496 }
2497 }
2498 }
2499 }
2500
2501 break;
2502}
2503
drh98757152008-01-09 23:04:12 +00002504/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002505**
2506** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002507** back any currently active btree transactions. If there are any active
2508** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
drh92f02c32004-09-02 14:57:08 +00002509**
2510** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002511*/
drh9cbf3422008-01-17 16:22:13 +00002512case OP_AutoCommit: {
drhad4a4b82008-11-05 16:37:34 +00002513 int desiredAutoCommit = pOp->p1;
2514 int rollback = pOp->p2;
2515 int turnOnAC = desiredAutoCommit && !db->autoCommit;
danielk19771d850a72004-05-31 08:26:49 +00002516
drhad4a4b82008-11-05 16:37:34 +00002517 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
2518 assert( desiredAutoCommit==1 || rollback==0 );
danielk19771d850a72004-05-31 08:26:49 +00002519
drh92f02c32004-09-02 14:57:08 +00002520 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002521
drhad4a4b82008-11-05 16:37:34 +00002522 if( turnOnAC && rollback && db->activeVdbeCnt>1 ){
2523 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002524 ** still running, and a transaction is active, return an error indicating
2525 ** that the other VMs must complete first.
2526 */
drhad4a4b82008-11-05 16:37:34 +00002527 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2528 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002529 rc = SQLITE_BUSY;
drhad4a4b82008-11-05 16:37:34 +00002530 }else if( turnOnAC && !rollback && db->writeVdbeCnt>1 ){
2531 /* If this instruction implements a COMMIT and other VMs are writing
2532 ** return an error indicating that the other VMs must complete first.
2533 */
2534 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2535 "SQL statements in progress");
2536 rc = SQLITE_BUSY;
2537 }else if( desiredAutoCommit!=db->autoCommit ){
danielk1977fd7f0452008-12-17 17:30:26 +00002538 if( rollback ){
drhad4a4b82008-11-05 16:37:34 +00002539 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002540 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002541 db->autoCommit = 1;
2542 }else{
shane7d3846a2008-12-11 02:58:26 +00002543 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002544 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002545 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002546 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002547 p->rc = rc = SQLITE_BUSY;
2548 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002549 }
danielk19771d850a72004-05-31 08:26:49 +00002550 }
danielk1977fd7f0452008-12-17 17:30:26 +00002551 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002552 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002553 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002554 }else{
drh900b31e2007-08-28 02:27:51 +00002555 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002556 }
drh900b31e2007-08-28 02:27:51 +00002557 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002558 }else{
drhf089aa42008-07-08 19:34:06 +00002559 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002560 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
danielk19771d850a72004-05-31 08:26:49 +00002561 (rollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002562 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002563
2564 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002565 }
2566 break;
2567}
2568
drh98757152008-01-09 23:04:12 +00002569/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002570**
2571** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002572** opcode is encountered. Depending on the ON CONFLICT setting, the
2573** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002574**
drh001bbcb2003-03-19 03:14:00 +00002575** P1 is the index of the database file on which the transaction is
2576** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002577** file used for temporary tables. Indices of 2 or more are used for
2578** attached databases.
drhcabb0812002-09-14 13:47:32 +00002579**
drh80242052004-06-09 00:48:12 +00002580** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002581** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002582** other process can start another write transaction while this transaction is
2583** underway. Starting a write transaction also creates a rollback journal. A
2584** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002585** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2586** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002587**
2588** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002589*/
drh9cbf3422008-01-17 16:22:13 +00002590case OP_Transaction: {
drh001bbcb2003-03-19 03:14:00 +00002591 int i = pOp->p1;
danielk19771d850a72004-05-31 08:26:49 +00002592 Btree *pBt;
2593
drh8bf8dc92003-05-17 17:35:10 +00002594 assert( i>=0 && i<db->nDb );
drhfb982642007-08-30 01:19:59 +00002595 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002596 pBt = db->aDb[i].pBt;
2597
danielk197724162fe2004-06-04 06:22:00 +00002598 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002599 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002600 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002601 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002602 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002603 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002604 }
danielk19772ef68482008-07-07 17:13:08 +00002605 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
danielk197724162fe2004-06-04 06:22:00 +00002606 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002607 }
drhb86ccfb2003-01-28 23:13:10 +00002608 }
drh5e00f6c2001-09-13 13:46:56 +00002609 break;
2610}
2611
drhb1fdb2a2008-01-05 04:06:03 +00002612/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002613**
drh9cbf3422008-01-17 16:22:13 +00002614** Read cookie number P3 from database P1 and write it into register P2.
drh4c583122008-01-04 22:01:03 +00002615** P3==0 is the schema version. P3==1 is the database format.
2616** P3==2 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002617** the main database file and P1==1 is the database file used to store
2618** temporary tables.
drh4a324312001-12-21 14:30:42 +00002619**
danielk1977418899a2007-06-24 10:14:00 +00002620** If P1 is negative, then this is a request to read the size of a
drh4c583122008-01-04 22:01:03 +00002621** databases free-list. P3 must be set to 1 in this case. The actual
danielk1977418899a2007-06-24 10:14:00 +00002622** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
danielk1977d62e76c2007-06-24 16:11:03 +00002623** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
danielk1977418899a2007-06-24 10:14:00 +00002624**
drh50e5dad2001-09-15 00:57:28 +00002625** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002626** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002627** executing this instruction.
2628*/
drh4c583122008-01-04 22:01:03 +00002629case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002630 int iMeta;
danielk1977180b56a2007-06-24 08:00:42 +00002631 int iDb = pOp->p1;
drh4c583122008-01-04 22:01:03 +00002632 int iCookie = pOp->p3;
danielk1977180b56a2007-06-24 08:00:42 +00002633
drhb7654112008-01-12 12:48:07 +00002634 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002635 if( iDb<0 ){
2636 iDb = (-1*(iDb+1));
2637 iCookie *= -1;
2638 }
2639 assert( iDb>=0 && iDb<db->nDb );
2640 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002641 assert( (p->btreeMask & (1<<iDb))!=0 );
drha3b321d2004-05-11 09:31:31 +00002642 /* The indexing of meta values at the schema layer is off by one from
2643 ** the indexing in the btree layer. The btree considers meta[0] to
2644 ** be the number of free pages in the database (a read-only value)
2645 ** and meta[1] to be the schema cookie. The schema layer considers
2646 ** meta[1] to be the schema cookie. So we have to shift the index
2647 ** by one in the following statement.
2648 */
danielk1977180b56a2007-06-24 08:00:42 +00002649 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002650 pOut->u.i = iMeta;
danielk1977a7a8e142008-02-13 18:25:27 +00002651 MemSetTypeFlag(pOut, MEM_Int);
drh50e5dad2001-09-15 00:57:28 +00002652 break;
2653}
2654
drh98757152008-01-09 23:04:12 +00002655/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002656**
drh98757152008-01-09 23:04:12 +00002657** Write the content of register P3 (interpreted as an integer)
2658** into cookie number P2 of database P1.
drh001bbcb2003-03-19 03:14:00 +00002659** P2==0 is the schema version. P2==1 is the database format.
2660** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2661** the main database file and P1==1 is the database file used to store
2662** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002663**
2664** A transaction must be started before executing this opcode.
2665*/
drh9cbf3422008-01-17 16:22:13 +00002666case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002667 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002668 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002669 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002670 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002671 pDb = &db->aDb[pOp->p1];
2672 assert( pDb->pBt!=0 );
drh98757152008-01-09 23:04:12 +00002673 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002674 /* See note about index shifting on OP_ReadCookie */
drh98757152008-01-09 23:04:12 +00002675 rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i);
drh3f7d4e42004-07-24 14:35:58 +00002676 if( pOp->p2==0 ){
2677 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002678 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002679 db->flags |= SQLITE_InternChanges;
drhd28bcb32005-12-21 14:43:11 +00002680 }else if( pOp->p2==1 ){
2681 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002682 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002683 }
drhfd426c62006-01-30 15:34:22 +00002684 if( pOp->p1==1 ){
2685 /* Invalidate all prepared statements whenever the TEMP database
2686 ** schema is changed. Ticket #1644 */
2687 sqlite3ExpirePreparedStatements(db);
2688 }
drh50e5dad2001-09-15 00:57:28 +00002689 break;
2690}
2691
drh4a324312001-12-21 14:30:42 +00002692/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002693**
drh001bbcb2003-03-19 03:14:00 +00002694** Check the value of global database parameter number 0 (the
2695** schema version) and make sure it is equal to P2.
2696** P1 is the database number which is 0 for the main database file
2697** and 1 for the file holding temporary tables and some higher number
2698** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002699**
2700** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002701** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002702** and that the current process needs to reread the schema.
2703**
2704** Either a transaction needs to have been started or an OP_Open needs
2705** to be executed (to establish a read lock) before this opcode is
2706** invoked.
2707*/
drh9cbf3422008-01-17 16:22:13 +00002708case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002709 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002710 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002711 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002712 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002713 pBt = db->aDb[pOp->p1].pBt;
2714 if( pBt ){
2715 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2716 }else{
2717 rc = SQLITE_OK;
2718 iMeta = 0;
2719 }
drhf328bc82004-05-10 23:29:49 +00002720 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
drh633e6d52008-07-28 19:34:53 +00002721 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002722 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002723 /* If the schema-cookie from the database file matches the cookie
2724 ** stored with the in-memory representation of the schema, do
2725 ** not reload the schema from the database file.
2726 **
shane21e7feb2008-05-30 15:59:49 +00002727 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002728 ** Often, v-tables store their data in other SQLite tables, which
2729 ** are queried from within xNext() and other v-table methods using
2730 ** prepared queries. If such a query is out-of-date, we do not want to
2731 ** discard the database schema, as the user code implementing the
2732 ** v-table would have to be ready for the sqlite3_vtab structure itself
2733 ** to be invalidated whenever sqlite3_step() is called from within
2734 ** a v-table method.
2735 */
2736 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2737 sqlite3ResetInternalSchema(db, pOp->p1);
2738 }
2739
drhf6d8ab82007-01-12 23:43:42 +00002740 sqlite3ExpirePreparedStatements(db);
drh50e5dad2001-09-15 00:57:28 +00002741 rc = SQLITE_SCHEMA;
2742 }
2743 break;
2744}
2745
drh98757152008-01-09 23:04:12 +00002746/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002747**
drhecdc7532001-09-23 02:35:53 +00002748** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002749** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002750** P3==0 means the main database, P3==1 means the database used for
2751** temporary tables, and P3>1 means used the corresponding attached
2752** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002753** values need not be contiguous but all P1 values should be small integers.
2754** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002755**
drh98757152008-01-09 23:04:12 +00002756** If P5!=0 then use the content of register P2 as the root page, not
2757** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002758**
drhb19a2bc2001-09-16 00:13:26 +00002759** There will be a read lock on the database whenever there is an
2760** open cursor. If the database was unlocked prior to this instruction
2761** then a read lock is acquired as part of this instruction. A read
2762** lock allows other processes to read the database but prohibits
2763** any other process from modifying the database. The read lock is
2764** released when all cursors are closed. If this instruction attempts
2765** to get a read lock but fails, the script terminates with an
2766** SQLITE_BUSY error code.
2767**
drh66a51672008-01-03 00:01:23 +00002768** The P4 value is a pointer to a KeyInfo structure that defines the
2769** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002770** that are not pointing to indices.
drhf57b3392001-10-08 13:22:32 +00002771**
drh001bbcb2003-03-19 03:14:00 +00002772** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002773*/
drh98757152008-01-09 23:04:12 +00002774/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002775**
2776** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002777** page is P2. Or if P5!=0 use the content of register P2 to find the
2778** root page.
drhecdc7532001-09-23 02:35:53 +00002779**
drh66a51672008-01-03 00:01:23 +00002780** The P4 value is a pointer to a KeyInfo structure that defines the
2781** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002782** that are not pointing to indices.
jplyon5a564222003-06-02 06:15:58 +00002783**
drh001bbcb2003-03-19 03:14:00 +00002784** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002785** in read/write mode. For a given table, there can be one or more read-only
2786** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002787**
drh001bbcb2003-03-19 03:14:00 +00002788** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002789*/
drh9cbf3422008-01-17 16:22:13 +00002790case OP_OpenRead:
2791case OP_OpenWrite: {
drh5e00f6c2001-09-13 13:46:56 +00002792 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002793 int p2 = pOp->p2;
danielk1977207872a2008-01-03 07:54:23 +00002794 int iDb = pOp->p3;
drhf57b3392001-10-08 13:22:32 +00002795 int wrFlag;
2796 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00002797 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00002798 Db *pDb;
drh001bbcb2003-03-19 03:14:00 +00002799
drh6810ce62004-01-31 19:22:56 +00002800 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00002801 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00002802 pDb = &db->aDb[iDb];
2803 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002804 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002805 if( pOp->opcode==OP_OpenWrite ){
2806 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002807 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2808 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002809 }
2810 }else{
2811 wrFlag = 0;
2812 }
drh98757152008-01-09 23:04:12 +00002813 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00002814 assert( p2>0 );
2815 assert( p2<=p->nMem );
2816 pIn2 = &p->aMem[p2];
2817 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00002818 p2 = (int)pIn2->u.i;
shanedcc50b72008-11-13 18:29:50 +00002819 if( p2<2 ) {
2820 rc = SQLITE_CORRUPT_BKPT;
2821 goto abort_due_to_error;
2822 }
drh5edc3122001-09-13 21:53:09 +00002823 }
drh6810ce62004-01-31 19:22:56 +00002824 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002825 pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
drh4774b132004-06-12 20:12:51 +00002826 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00002827 pCur->nullRow = 1;
danielk1977cd3e8f72008-03-25 09:47:35 +00002828 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor);
drh66a51672008-01-03 00:01:23 +00002829 if( pOp->p4type==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +00002830 pCur->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002831 pCur->pKeyInfo->enc = ENC(p->db);
danielk197724162fe2004-06-04 06:22:00 +00002832 }else{
drhf0863fe2005-06-12 21:35:51 +00002833 pCur->pKeyInfo = 0;
danielk197724162fe2004-06-04 06:22:00 +00002834 }
2835 switch( rc ){
2836 case SQLITE_BUSY: {
danielk19772a764eb2004-06-12 01:43:26 +00002837 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002838 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002839 goto vdbe_return;
drhd3d39e92004-05-20 22:16:29 +00002840 }
danielk197724162fe2004-06-04 06:22:00 +00002841 case SQLITE_OK: {
2842 int flags = sqlite3BtreeFlags(pCur->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002843 /* Sanity checking. Only the lower four bits of the flags byte should
danielk1977ad0132d2008-06-07 08:58:22 +00002844 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
drhf0863fe2005-06-12 21:35:51 +00002845 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2846 ** 2 (zerodata for indices). If these conditions are not met it can
2847 ** only mean that we are dealing with a corrupt database file
2848 */
2849 if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
drh49285702005-09-17 15:20:26 +00002850 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002851 goto abort_due_to_error;
2852 }
drh9c1905f2008-12-10 22:32:56 +00002853 pCur->isTable = (flags & BTREE_INTKEY)!=0 ?1:0;
2854 pCur->isIndex = (flags & BTREE_ZERODATA)!=0 ?1:0;
drh66a51672008-01-03 00:01:23 +00002855 /* If P4==0 it means we are expected to open a table. If P4!=0 then
drhf0863fe2005-06-12 21:35:51 +00002856 ** we expect to be opening an index. If this is not what happened,
2857 ** then the database is corrupt
2858 */
drh66a51672008-01-03 00:01:23 +00002859 if( (pCur->isTable && pOp->p4type==P4_KEYINFO)
2860 || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
drh49285702005-09-17 15:20:26 +00002861 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002862 goto abort_due_to_error;
2863 }
danielk197724162fe2004-06-04 06:22:00 +00002864 break;
drh5e00f6c2001-09-13 13:46:56 +00002865 }
danielk197724162fe2004-06-04 06:22:00 +00002866 case SQLITE_EMPTY: {
drh66a51672008-01-03 00:01:23 +00002867 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhf0863fe2005-06-12 21:35:51 +00002868 pCur->isIndex = !pCur->isTable;
danielk1977cd3e8f72008-03-25 09:47:35 +00002869 pCur->pCursor = 0;
danielk197724162fe2004-06-04 06:22:00 +00002870 rc = SQLITE_OK;
2871 break;
2872 }
2873 default: {
2874 goto abort_due_to_error;
2875 }
2876 }
drh5e00f6c2001-09-13 13:46:56 +00002877 break;
2878}
2879
drh98757152008-01-09 23:04:12 +00002880/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00002881**
drhb9bb7c12006-06-11 23:41:55 +00002882** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00002883** The cursor is always opened read/write even if
2884** the main database is read-only. The transient or virtual
2885** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002886**
drh0342b1f2005-09-01 03:07:44 +00002887** P2 is the number of columns in the virtual table.
drh66a51672008-01-03 00:01:23 +00002888** The cursor points to a BTree table if P4==0 and to a BTree index
2889** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00002890** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00002891**
2892** This opcode was once called OpenTemp. But that created
2893** confusion because the term "temp table", might refer either
2894** to a TEMP table at the SQL level, or to a table opened by
2895** this opcode. Then this opcode was call OpenVirtual. But
2896** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00002897*/
drh9cbf3422008-01-17 16:22:13 +00002898case OP_OpenEphemeral: {
drh5e00f6c2001-09-13 13:46:56 +00002899 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002900 VdbeCursor *pCx;
drh33f4e022007-09-03 15:19:34 +00002901 static const int openFlags =
2902 SQLITE_OPEN_READWRITE |
2903 SQLITE_OPEN_CREATE |
2904 SQLITE_OPEN_EXCLUSIVE |
2905 SQLITE_OPEN_DELETEONCLOSE |
2906 SQLITE_OPEN_TRANSIENT_DB;
2907
drh6810ce62004-01-31 19:22:56 +00002908 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002909 pCx = allocateCursor(p, i, pOp, -1, 1);
drh4774b132004-06-12 20:12:51 +00002910 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00002911 pCx->nullRow = 1;
drh33f4e022007-09-03 15:19:34 +00002912 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
2913 &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002914 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00002915 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00002916 }
2917 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002918 /* If a transient index is required, create it by calling
2919 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2920 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002921 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002922 */
danielk19772dca4ac2008-01-03 11:50:29 +00002923 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00002924 int pgno;
drh66a51672008-01-03 00:01:23 +00002925 assert( pOp->p4type==P4_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00002926 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002927 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002928 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00002929 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00002930 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00002931 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002932 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00002933 }
drhf0863fe2005-06-12 21:35:51 +00002934 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00002935 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002936 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002937 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00002938 }
drh5e00f6c2001-09-13 13:46:56 +00002939 }
drhf0863fe2005-06-12 21:35:51 +00002940 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00002941 break;
2942}
2943
danielk19779882d992008-03-27 17:59:01 +00002944/* Opcode: OpenPseudo P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00002945**
2946** Open a new cursor that points to a fake table that contains a single
2947** row of data. Any attempt to write a second row of data causes the
2948** first row to be deleted. All data is deleted when the cursor is
2949** closed.
2950**
2951** A pseudo-table created by this opcode is useful for holding the
drhcdd536f2006-03-17 00:04:03 +00002952** NEW or OLD tables in a trigger. Also used to hold the a single
2953** row output from the sorter so that the row can be decomposed into
2954** individual columns using the OP_Column opcode.
danielk19779882d992008-03-27 17:59:01 +00002955**
2956** When OP_Insert is executed to insert a row in to the pseudo table,
2957** the pseudo-table cursor may or may not make it's own copy of the
2958** original row data. If P2 is 0, then the pseudo-table will copy the
2959** original row data. Otherwise, a pointer to the original memory cell
2960** is stored. In this case, the vdbe program must ensure that the
2961** memory cell containing the row data is not overwritten until the
2962** pseudo table is closed (or a new row is inserted into it).
drh70ce3f02003-04-15 19:22:22 +00002963*/
drh9cbf3422008-01-17 16:22:13 +00002964case OP_OpenPseudo: {
drh70ce3f02003-04-15 19:22:22 +00002965 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002966 VdbeCursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002967 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002968 pCx = allocateCursor(p, i, &pOp[-1], -1, 0);
drh4774b132004-06-12 20:12:51 +00002969 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00002970 pCx->nullRow = 1;
2971 pCx->pseudoTable = 1;
drh9c1905f2008-12-10 22:32:56 +00002972 pCx->ephemPseudoTable = (u8)pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00002973 pCx->isTable = 1;
2974 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00002975 break;
2976}
2977
drh98757152008-01-09 23:04:12 +00002978/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00002979**
2980** Close a cursor previously opened as P1. If P1 is not
2981** currently open, this instruction is a no-op.
2982*/
drh9cbf3422008-01-17 16:22:13 +00002983case OP_Close: {
drh5e00f6c2001-09-13 13:46:56 +00002984 int i = pOp->p1;
drha05a7222008-01-19 03:35:58 +00002985 assert( i>=0 && i<p->nCursor );
2986 sqlite3VdbeFreeCursor(p, p->apCsr[i]);
2987 p->apCsr[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00002988 break;
2989}
2990
drh959403f2008-12-12 17:56:16 +00002991/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00002992**
danielk1977b790c6c2008-04-18 10:25:24 +00002993** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00002994** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00002995** to an SQL index, then P3 is the first in an array of P4 registers
2996** that are used as an unpacked index key.
2997**
2998** Reposition cursor P1 so that it points to the smallest entry that
2999** is greater than or equal to the key value. If there are no records
3000** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003001**
drh959403f2008-12-12 17:56:16 +00003002** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003003*/
drh959403f2008-12-12 17:56:16 +00003004/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003005**
danielk1977b790c6c2008-04-18 10:25:24 +00003006** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003007** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003008** to an SQL index, then P3 is the first in an array of P4 registers
3009** that are used as an unpacked index key.
3010**
3011** Reposition cursor P1 so that it points to the smallest entry that
3012** is greater than the key value. If there are no records greater than
3013** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003014**
drh959403f2008-12-12 17:56:16 +00003015** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003016*/
drh959403f2008-12-12 17:56:16 +00003017/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003018**
danielk1977b790c6c2008-04-18 10:25:24 +00003019** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003020** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003021** to an SQL index, then P3 is the first in an array of P4 registers
3022** that are used as an unpacked index key.
3023**
3024** Reposition cursor P1 so that it points to the largest entry that
3025** is less than the key value. If there are no records less than
3026** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003027**
drh959403f2008-12-12 17:56:16 +00003028** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003029*/
drh959403f2008-12-12 17:56:16 +00003030/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003031**
danielk1977b790c6c2008-04-18 10:25:24 +00003032** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003033** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003034** to an SQL index, then P3 is the first in an array of P4 registers
3035** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003036**
danielk1977b790c6c2008-04-18 10:25:24 +00003037** Reposition cursor P1 so that it points to the largest entry that
3038** is less than or equal to the key value. If there are no records
3039** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003040**
drh959403f2008-12-12 17:56:16 +00003041** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003042*/
drh959403f2008-12-12 17:56:16 +00003043case OP_SeekLt: /* jump, in3 */
3044case OP_SeekLe: /* jump, in3 */
3045case OP_SeekGe: /* jump, in3 */
3046case OP_SeekGt: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00003047 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003048 VdbeCursor *pC;
drh80ff32f2001-11-04 18:32:46 +00003049
drh70ce3f02003-04-15 19:22:22 +00003050 assert( i>=0 && i<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003051 assert( pOp->p2!=0 );
drhd7556d22004-05-14 21:59:40 +00003052 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003053 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003054 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00003055 int res, oc;
drh7cf6e4d2004-05-19 14:56:55 +00003056 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003057 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003058 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003059 i64 iKey; /* The rowid we are to seek to */
3060
3061 /* The input value in P3 might be of any type: integer, real, string,
3062 ** blob, or NULL. But it needs to be an integer before we can do
3063 ** the seek, so covert it. */
3064 applyNumericAffinity(pIn3);
3065 iKey = sqlite3VdbeIntValue(pIn3);
3066 pC->rowidIsValid = 0;
3067
3068 /* If the P3 value could not be converted into an integer without
3069 ** loss of information, then special processing is required... */
3070 if( (pIn3->flags & MEM_Int)==0 ){
3071 if( (pIn3->flags & MEM_Real)==0 ){
3072 /* If the P3 value cannot be converted into any kind of a number,
3073 ** then the seek is not possible, so jump to P2 */
3074 pc = pOp->p2 - 1;
3075 break;
3076 }
3077 /* If we reach this point, then the P3 value must be a floating
3078 ** point number. */
3079 assert( (pIn3->flags & MEM_Real)!=0 );
3080
3081 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
3082 /* The P3 value is to large in magnitude to be expressed as an
3083 ** integer. */
3084 res = 1;
3085 if( pIn3->r<0 ){
3086 if( oc==OP_SeekGt || oc==OP_SeekGe ){
3087 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3088 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3089 }
3090 }else{
3091 if( oc==OP_SeekLt || oc==OP_SeekLe ){
3092 rc = sqlite3BtreeLast(pC->pCursor, &res);
3093 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3094 }
3095 }
3096 if( res ){
3097 pc = pOp->p2 - 1;
3098 }
3099 break;
3100 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3101 /* Use the ceiling() function to convert real->int */
3102 if( pIn3->r > (double)iKey ) iKey++;
3103 }else{
3104 /* Use the floor() function to convert real->int */
3105 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3106 if( pIn3->r < (double)iKey ) iKey--;
3107 }
3108 }
drhe63d9992008-08-13 19:11:48 +00003109 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003110 if( rc!=SQLITE_OK ){
3111 goto abort_due_to_error;
3112 }
drh959403f2008-12-12 17:56:16 +00003113 if( res==0 ){
3114 pC->rowidIsValid = 1;
3115 pC->lastRowid = iKey;
3116 }
drh5e00f6c2001-09-13 13:46:56 +00003117 }else{
danielk1977b790c6c2008-04-18 10:25:24 +00003118 UnpackedRecord r;
3119 int nField = pOp->p4.i;
3120 assert( pOp->p4type==P4_INT32 );
3121 assert( nField>0 );
3122 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003123 r.nField = (u16)nField;
drh959403f2008-12-12 17:56:16 +00003124 if( oc==OP_SeekGt || oc==OP_SeekLe ){
drhe63d9992008-08-13 19:11:48 +00003125 r.flags = UNPACKED_INCRKEY;
3126 }else{
3127 r.flags = 0;
3128 }
danielk1977b790c6c2008-04-18 10:25:24 +00003129 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00003130 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003131 if( rc!=SQLITE_OK ){
3132 goto abort_due_to_error;
3133 }
drhf0863fe2005-06-12 21:35:51 +00003134 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003135 }
drha11846b2004-01-07 18:52:56 +00003136 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003137 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003138#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003139 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003140#endif
drh959403f2008-12-12 17:56:16 +00003141 if( oc==OP_SeekGe || oc==OP_SeekGt ){
3142 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003143 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003144 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003145 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003146 }else{
3147 res = 0;
drh8721ce42001-11-07 14:22:00 +00003148 }
drh7cf6e4d2004-05-19 14:56:55 +00003149 }else{
drh959403f2008-12-12 17:56:16 +00003150 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3151 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003152 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3153 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003154 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003155 }else{
3156 /* res might be negative because the table is empty. Check to
3157 ** see if this is the case.
3158 */
drhf328bc82004-05-10 23:29:49 +00003159 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003160 }
drh1af3fdb2004-07-18 21:33:01 +00003161 }
drh91fd4d42008-01-19 20:11:25 +00003162 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003163 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003164 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003165 }
danielk1977f7b9d662008-06-23 18:49:43 +00003166 }else if( !pC->pseudoTable ){
3167 /* This happens when attempting to open the sqlite3_master table
3168 ** for read access returns SQLITE_EMPTY. In this case always
3169 ** take the jump (since there are no records in the table).
3170 */
3171 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003172 }
drh5e00f6c2001-09-13 13:46:56 +00003173 break;
3174}
3175
drh959403f2008-12-12 17:56:16 +00003176/* Opcode: Seek P1 P2 * * *
3177**
3178** P1 is an open table cursor and P2 is a rowid integer. Arrange
3179** for P1 to move so that it points to the rowid given by P2.
3180**
3181** This is actually a deferred seek. Nothing actually happens until
3182** the cursor is used to read a record. That way, if no reads
3183** occur, no unnecessary I/O happens.
3184*/
3185case OP_Seek: { /* in2 */
3186 int i = pOp->p1;
3187 VdbeCursor *pC;
3188
3189 assert( i>=0 && i<p->nCursor );
3190 pC = p->apCsr[i];
3191 assert( pC!=0 );
3192 if( pC->pCursor!=0 ){
3193 assert( pC->isTable );
3194 pC->nullRow = 0;
3195 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3196 pC->rowidIsValid = 0;
3197 pC->deferredMoveto = 1;
3198 }
3199 break;
3200}
3201
3202
drh98757152008-01-09 23:04:12 +00003203/* Opcode: Found P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003204**
drh98757152008-01-09 23:04:12 +00003205** Register P3 holds a blob constructed by MakeRecord. P1 is an index.
drh9cbf3422008-01-17 16:22:13 +00003206** If an entry that matches the value in register p3 exists in P1 then
3207** jump to P2. If the P3 value does not match any entry in P1
drhf0863fe2005-06-12 21:35:51 +00003208** then fall thru. The P1 cursor is left pointing at the matching entry
drh2dcef112008-01-12 19:03:48 +00003209** if it exists.
drhf0863fe2005-06-12 21:35:51 +00003210**
3211** This instruction is used to implement the IN operator where the
danielk19779a96b662007-11-29 17:05:18 +00003212** left-hand side is a SELECT statement. P1 may be a true index, or it
3213** may be a temporary index that holds the results of the SELECT
drh2dcef112008-01-12 19:03:48 +00003214** statement. This instruction is also used to implement the
3215** DISTINCT keyword in SELECT statements.
danielk19779a96b662007-11-29 17:05:18 +00003216**
3217** This instruction checks if index P1 contains a record for which
shane21e7feb2008-05-30 15:59:49 +00003218** the first N serialized values exactly match the N serialized values
drh9cbf3422008-01-17 16:22:13 +00003219** in the record in register P3, where N is the total number of values in
3220** the P3 record (the P3 record is a prefix of the P1 record).
drhb19a2bc2001-09-16 00:13:26 +00003221**
drhcb6d50e2008-08-21 19:28:30 +00003222** See also: NotFound, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00003223*/
drh98757152008-01-09 23:04:12 +00003224/* Opcode: NotFound P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003225**
drh98757152008-01-09 23:04:12 +00003226** Register P3 holds a blob constructed by MakeRecord. P1 is
drhf0863fe2005-06-12 21:35:51 +00003227** an index. If no entry exists in P1 that matches the blob then jump
drh795ab9b2007-01-27 13:37:22 +00003228** to P2. If an entry does existing, fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003229** pointing to the entry that matches.
drh5e00f6c2001-09-13 13:46:56 +00003230**
drhcb6d50e2008-08-21 19:28:30 +00003231** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003232*/
drh9cbf3422008-01-17 16:22:13 +00003233case OP_NotFound: /* jump, in3 */
3234case OP_Found: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00003235 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00003236 int alreadyExists = 0;
drhdfe88ec2008-11-03 20:55:06 +00003237 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003238 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003239 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003240 if( (pC = p->apCsr[i])->pCursor!=0 ){
danielk197777519402007-08-30 11:48:31 +00003241 int res;
drhe63d9992008-08-13 19:11:48 +00003242 UnpackedRecord *pIdxKey;
3243
drhf0863fe2005-06-12 21:35:51 +00003244 assert( pC->isTable==0 );
drh98757152008-01-09 23:04:12 +00003245 assert( pIn3->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003246 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
drh23f79d02008-08-20 22:06:47 +00003247 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003248 if( pIdxKey==0 ){
3249 goto no_mem;
danielk19779a96b662007-11-29 17:05:18 +00003250 }
drhe63d9992008-08-13 19:11:48 +00003251 if( pOp->opcode==OP_Found ){
3252 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3253 }
3254 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3255 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk197777519402007-08-30 11:48:31 +00003256 if( rc!=SQLITE_OK ){
3257 break;
3258 }
3259 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003260 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003261 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003262 }
3263 if( pOp->opcode==OP_Found ){
3264 if( alreadyExists ) pc = pOp->p2 - 1;
3265 }else{
3266 if( !alreadyExists ) pc = pOp->p2 - 1;
3267 }
drh5e00f6c2001-09-13 13:46:56 +00003268 break;
3269}
3270
drh98757152008-01-09 23:04:12 +00003271/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003272**
drh98757152008-01-09 23:04:12 +00003273** The P3 register contains an integer record number. Call this
3274** record number R. The P4 register contains an index key created
drhe63d9992008-08-13 19:11:48 +00003275** using MakeRecord. Call it K.
drh9cfcf5d2002-01-29 18:41:24 +00003276**
drh7cf6e4d2004-05-19 14:56:55 +00003277** P1 is an index. So it has no data and its key consists of a
drhf0863fe2005-06-12 21:35:51 +00003278** record generated by OP_MakeRecord where the last field is the
3279** rowid of the entry that the index refers to.
drhf3218fe2004-05-28 08:21:02 +00003280**
drh0ca3e242002-01-29 23:07:02 +00003281** This instruction asks if there is an entry in P1 where the
drh7cf6e4d2004-05-19 14:56:55 +00003282** fields matches K but the rowid is different from R.
3283** If there is no such entry, then there is an immediate
drh0ca3e242002-01-29 23:07:02 +00003284** jump to P2. If any entry does exist where the index string
3285** matches K but the record number is not R, then the record
drh98757152008-01-09 23:04:12 +00003286** number for that entry is written into P3 and control
drh0ca3e242002-01-29 23:07:02 +00003287** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003288**
drh9cbf3422008-01-17 16:22:13 +00003289** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003290*/
drh9cbf3422008-01-17 16:22:13 +00003291case OP_IsUnique: { /* jump, in3 */
drh9cfcf5d2002-01-29 18:41:24 +00003292 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003293 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003294 BtCursor *pCrsr;
drh98757152008-01-09 23:04:12 +00003295 Mem *pK;
danielk1977452c9892004-05-13 05:16:15 +00003296 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00003297
drh0ca3e242002-01-29 23:07:02 +00003298 /* Pop the value R off the top of the stack
3299 */
drh98757152008-01-09 23:04:12 +00003300 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003301 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
3302 pK = &p->aMem[pOp->p4.i];
drh98757152008-01-09 23:04:12 +00003303 sqlite3VdbeMemIntegerify(pIn3);
3304 R = pIn3->u.i;
drh73bdf072006-08-15 14:21:16 +00003305 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003306 pCx = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003307 assert( pCx!=0 );
drhf328bc82004-05-10 23:29:49 +00003308 pCrsr = pCx->pCursor;
3309 if( pCrsr!=0 ){
danielk1977f2fa8312006-01-24 13:09:33 +00003310 int res;
drhe63d9992008-08-13 19:11:48 +00003311 i64 v; /* The record number that matches K */
3312 UnpackedRecord *pIdxKey; /* Unpacked version of P4 */
drh0ca3e242002-01-29 23:07:02 +00003313
3314 /* Make sure K is a string and make zKey point to K
3315 */
drh98757152008-01-09 23:04:12 +00003316 assert( pK->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003317 pIdxKey = sqlite3VdbeRecordUnpack(pCx->pKeyInfo, pK->n, pK->z,
drh23f79d02008-08-20 22:06:47 +00003318 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003319 if( pIdxKey==0 ){
3320 goto no_mem;
3321 }
3322 pIdxKey->flags |= UNPACKED_IGNORE_ROWID;
danielk1977452c9892004-05-13 05:16:15 +00003323
drhe63d9992008-08-13 19:11:48 +00003324 /* Search for an entry in P1 where all but the last rowid match K
drh0ca3e242002-01-29 23:07:02 +00003325 ** If there is no such entry, jump immediately to P2.
3326 */
drh9188b382004-05-14 21:12:22 +00003327 assert( pCx->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003328 pCx->cacheStatus = CACHE_STALE;
drhe63d9992008-08-13 19:11:48 +00003329 rc = sqlite3BtreeMovetoUnpacked(pCrsr, pIdxKey, 0, 0, &res);
danielk1977f0113002006-01-24 12:09:17 +00003330 if( rc!=SQLITE_OK ){
drhe63d9992008-08-13 19:11:48 +00003331 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk1977f0113002006-01-24 12:09:17 +00003332 goto abort_due_to_error;
3333 }
drh9cfcf5d2002-01-29 18:41:24 +00003334 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003335 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00003336 if( res ){
3337 pc = pOp->p2 - 1;
drhe63d9992008-08-13 19:11:48 +00003338 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003339 break;
3340 }
3341 }
drhe63d9992008-08-13 19:11:48 +00003342 rc = sqlite3VdbeIdxKeyCompare(pCx, pIdxKey, &res);
3343 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003344 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3345 if( res>0 ){
3346 pc = pOp->p2 - 1;
3347 break;
3348 }
drh0ca3e242002-01-29 23:07:02 +00003349
3350 /* At this point, pCrsr is pointing to an entry in P1 where all but
drhf3218fe2004-05-28 08:21:02 +00003351 ** the final entry (the rowid) matches K. Check to see if the
3352 ** final rowid column is different from R. If it equals R then jump
danielk1977452c9892004-05-13 05:16:15 +00003353 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00003354 */
drhb21c8cd2007-08-21 19:33:56 +00003355 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
danielk1977452c9892004-05-13 05:16:15 +00003356 if( rc!=SQLITE_OK ){
3357 goto abort_due_to_error;
3358 }
drh0ca3e242002-01-29 23:07:02 +00003359 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003360 pc = pOp->p2 - 1;
3361 break;
3362 }
drh0ca3e242002-01-29 23:07:02 +00003363
drh9cbf3422008-01-17 16:22:13 +00003364 /* The final varint of the key is different from R. Store it back
3365 ** into register R3. (The record number of an entry that violates
3366 ** a UNIQUE constraint.)
drh0ca3e242002-01-29 23:07:02 +00003367 */
drh98757152008-01-09 23:04:12 +00003368 pIn3->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003369 assert( pIn3->flags&MEM_Int );
drh9cfcf5d2002-01-29 18:41:24 +00003370 }
3371 break;
3372}
3373
drh9cbf3422008-01-17 16:22:13 +00003374/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003375**
drh9cbf3422008-01-17 16:22:13 +00003376** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003377** with that key does not exist in table of P1, then jump to P2.
3378** If the record does exist, then fall thru. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003379** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003380**
3381** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003382** operation assumes the key is an integer and that P1 is a table whereas
3383** NotFound assumes key is a blob constructed from MakeRecord and
3384** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003385**
drhcb6d50e2008-08-21 19:28:30 +00003386** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003387*/
drh9cbf3422008-01-17 16:22:13 +00003388case OP_NotExists: { /* jump, in3 */
drh6b125452002-01-28 15:53:03 +00003389 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003390 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003391 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003392 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003393 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003394 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh03e1f512008-12-11 13:05:00 +00003395 int res = 0;
danielk197736a3c702004-05-11 06:55:14 +00003396 u64 iKey;
drh98757152008-01-09 23:04:12 +00003397 assert( pIn3->flags & MEM_Int );
drhf0863fe2005-06-12 21:35:51 +00003398 assert( p->apCsr[i]->isTable );
drh98757152008-01-09 23:04:12 +00003399 iKey = intToKey(pIn3->u.i);
drhe63d9992008-08-13 19:11:48 +00003400 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0,&res);
drh98757152008-01-09 23:04:12 +00003401 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003402 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003403 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003404 pC->cacheStatus = CACHE_STALE;
danielk197728129562005-01-11 10:25:06 +00003405 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003406 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003407 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003408 }
danielk1977f7b9d662008-06-23 18:49:43 +00003409 }else if( !pC->pseudoTable ){
3410 /* This happens when an attempt to open a read cursor on the
3411 ** sqlite_master table returns SQLITE_EMPTY.
3412 */
3413 assert( pC->isTable );
3414 pc = pOp->p2 - 1;
3415 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003416 }
drh6b125452002-01-28 15:53:03 +00003417 break;
3418}
3419
drh4c583122008-01-04 22:01:03 +00003420/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003421**
drh4c583122008-01-04 22:01:03 +00003422** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003423** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003424** The sequence number on the cursor is incremented after this
3425** instruction.
drh4db38a72005-09-01 12:16:28 +00003426*/
drh4c583122008-01-04 22:01:03 +00003427case OP_Sequence: { /* out2-prerelease */
drh4db38a72005-09-01 12:16:28 +00003428 int i = pOp->p1;
drh4db38a72005-09-01 12:16:28 +00003429 assert( i>=0 && i<p->nCursor );
3430 assert( p->apCsr[i]!=0 );
drh4c583122008-01-04 22:01:03 +00003431 pOut->u.i = p->apCsr[i]->seqCount++;
danielk1977a7a8e142008-02-13 18:25:27 +00003432 MemSetTypeFlag(pOut, MEM_Int);
drh4db38a72005-09-01 12:16:28 +00003433 break;
3434}
3435
3436
drh98757152008-01-09 23:04:12 +00003437/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003438**
drhf0863fe2005-06-12 21:35:51 +00003439** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003440** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003441** table that cursor P1 points to. The new record number is written
3442** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003443**
drh98757152008-01-09 23:04:12 +00003444** If P3>0 then P3 is a register that holds the largest previously
drh205f48e2004-11-05 00:43:11 +00003445** generated record number. No new record numbers are allowed to be less
drh2958a4e2004-11-12 03:56:15 +00003446** than this value. When this value reaches its maximum, a SQLITE_FULL
drh98757152008-01-09 23:04:12 +00003447** error is generated. The P3 register is updated with the generated
drh4c583122008-01-04 22:01:03 +00003448** record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003449** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003450*/
drh4c583122008-01-04 22:01:03 +00003451case OP_NewRowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003452 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00003453 i64 v = 0;
drhdfe88ec2008-11-03 20:55:06 +00003454 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003455 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003456 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003457 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00003458 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003459 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003460 /* The next rowid or record number (different terms for the same
3461 ** thing) is obtained in a two-step algorithm.
3462 **
3463 ** First we attempt to find the largest existing rowid and add one
3464 ** to that. But if the largest existing rowid is already the maximum
3465 ** positive integer, we have to fall through to the second
3466 ** probabilistic algorithm
3467 **
3468 ** The second algorithm is to select a rowid at random and see if
3469 ** it already exists in the table. If it does not exist, we have
3470 ** succeeded. If the random rowid does exist, we select a new one
3471 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00003472 **
3473 ** For a table with less than 2 billion entries, the probability
3474 ** of not finding a unused rowid is about 1.0e-300. This is a
3475 ** non-zero probability, but it is still vanishingly small and should
3476 ** never cause a problem. You are much, much more likely to have a
3477 ** hardware failure than for this algorithm to fail.
3478 **
drhaf9ff332002-01-16 21:00:27 +00003479 ** The analysis in the previous paragraph assumes that you have a good
3480 ** source of random numbers. Is a library function like lrand48()
3481 ** good enough? Maybe. Maybe not. It's hard to know whether there
3482 ** might be subtle bugs is some implementations of lrand48() that
3483 ** could cause problems. To avoid uncertainty, SQLite uses its own
3484 ** random number generator based on the RC4 algorithm.
3485 **
drhdb5ed6d2001-09-18 22:17:44 +00003486 ** To promote locality of reference for repetitive inserts, the
shane21e7feb2008-05-30 15:59:49 +00003487 ** first few attempts at choosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00003488 ** larger than the previous rowid. This has been shown experimentally
3489 ** to double the speed of the COPY operation.
3490 */
danielk1977f7df9cc2004-06-16 12:02:47 +00003491 int res, rx=SQLITE_OK, cnt;
drhf328bc82004-05-10 23:29:49 +00003492 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00003493 cnt = 0;
drh4e6083c2005-02-04 21:13:00 +00003494 if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3495 BTREE_INTKEY ){
drh49285702005-09-17 15:20:26 +00003496 rc = SQLITE_CORRUPT_BKPT;
drh4e6083c2005-02-04 21:13:00 +00003497 goto abort_due_to_error;
3498 }
drhf328bc82004-05-10 23:29:49 +00003499 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3500 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drhfe2093d2005-01-20 22:48:47 +00003501
drh75f86a42005-02-17 00:03:06 +00003502#ifdef SQLITE_32BIT_ROWID
3503# define MAX_ROWID 0x7fffffff
3504#else
drhfe2093d2005-01-20 22:48:47 +00003505 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3506 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3507 ** to provide the constant while making all compilers happy.
3508 */
danielk197764202cf2008-11-17 15:31:47 +00003509# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003510#endif
drhfe2093d2005-01-20 22:48:47 +00003511
drh5cf8e8c2002-02-19 22:42:05 +00003512 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00003513 if( pC->nextRowidValid ){
3514 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00003515 }else{
danielk1977261919c2005-12-06 12:52:59 +00003516 rc = sqlite3BtreeLast(pC->pCursor, &res);
3517 if( rc!=SQLITE_OK ){
3518 goto abort_due_to_error;
3519 }
drh32fbe342002-10-19 20:16:37 +00003520 if( res ){
3521 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003522 }else{
danielk1977e0d4b062004-06-28 01:11:46 +00003523 sqlite3BtreeKeySize(pC->pCursor, &v);
drh32fbe342002-10-19 20:16:37 +00003524 v = keyToInt(v);
drh75f86a42005-02-17 00:03:06 +00003525 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003526 pC->useRandomRowid = 1;
3527 }else{
3528 v++;
3529 }
drh5cf8e8c2002-02-19 22:42:05 +00003530 }
drh3fc190c2001-09-14 03:24:23 +00003531 }
drh205f48e2004-11-05 00:43:11 +00003532
3533#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003534 if( pOp->p3 ){
drh205f48e2004-11-05 00:43:11 +00003535 Mem *pMem;
drh4c583122008-01-04 22:01:03 +00003536 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
3537 pMem = &p->aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00003538 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003539 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003540 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003541 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003542 rc = SQLITE_FULL;
3543 goto abort_due_to_error;
3544 }
drh3c024d62007-03-30 11:23:45 +00003545 if( v<pMem->u.i+1 ){
3546 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003547 }
drh3c024d62007-03-30 11:23:45 +00003548 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003549 }
3550#endif
3551
drh75f86a42005-02-17 00:03:06 +00003552 if( v<MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003553 pC->nextRowidValid = 1;
3554 pC->nextRowid = v+1;
3555 }else{
3556 pC->nextRowidValid = 0;
3557 }
drh5cf8e8c2002-02-19 22:42:05 +00003558 }
3559 if( pC->useRandomRowid ){
drh4c583122008-01-04 22:01:03 +00003560 assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
drh5cf8e8c2002-02-19 22:42:05 +00003561 v = db->priorNewRowid;
3562 cnt = 0;
3563 do{
drh91fd4d42008-01-19 20:11:25 +00003564 if( cnt==0 && (v&0xffffff)==v ){
3565 v++;
3566 }else{
drh2fa18682008-03-19 14:15:34 +00003567 sqlite3_randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003568 if( cnt<5 ) v &= 0xffffff;
drh5cf8e8c2002-02-19 22:42:05 +00003569 }
3570 if( v==0 ) continue;
3571 x = intToKey(v);
drhe63d9992008-08-13 19:11:48 +00003572 rx = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)x, 0, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003573 cnt++;
drh91fd4d42008-01-19 20:11:25 +00003574 }while( cnt<100 && rx==SQLITE_OK && res==0 );
drh5cf8e8c2002-02-19 22:42:05 +00003575 db->priorNewRowid = v;
3576 if( rx==SQLITE_OK && res==0 ){
3577 rc = SQLITE_FULL;
3578 goto abort_due_to_error;
3579 }
drh1eaa2692001-09-18 02:02:23 +00003580 }
drhf0863fe2005-06-12 21:35:51 +00003581 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003582 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003583 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003584 }
danielk1977a7a8e142008-02-13 18:25:27 +00003585 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00003586 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003587 break;
3588}
3589
danielk19771f4aa332008-01-03 09:51:55 +00003590/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003591**
jplyon5a564222003-06-02 06:15:58 +00003592** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003593** created if it doesn't already exist or the data for an existing
danielk19771f4aa332008-01-03 09:51:55 +00003594** entry is overwritten. The data is the value stored register
3595** number P2. The key is stored in register P3. The key must
3596** be an integer.
drh4a324312001-12-21 14:30:42 +00003597**
danielk19771f4aa332008-01-03 09:51:55 +00003598** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3599** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003600** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003601** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003602**
drh66a51672008-01-03 00:01:23 +00003603** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003604** may be NULL. If it is not NULL, then the update-hook
3605** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3606**
drh93aed5a2008-01-16 17:46:38 +00003607** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3608** allocated, then ownership of P2 is transferred to the pseudo-cursor
3609** and register P2 becomes ephemeral. If the cursor is changed, the
3610** value of register P2 will then change. Make sure this does not
3611** cause any problems.)
3612**
drhf0863fe2005-06-12 21:35:51 +00003613** This instruction only works on tables. The equivalent instruction
3614** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003615*/
drh9cbf3422008-01-17 16:22:13 +00003616case OP_Insert: {
danielk19771f4aa332008-01-03 09:51:55 +00003617 Mem *pData = &p->aMem[pOp->p2];
3618 Mem *pKey = &p->aMem[pOp->p3];
3619
drha05a7222008-01-19 03:35:58 +00003620 i64 iKey; /* The integer ROWID or key for the record to be inserted */
drh5e00f6c2001-09-13 13:46:56 +00003621 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003622 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003623 assert( i>=0 && i<p->nCursor );
drha05a7222008-01-19 03:35:58 +00003624 pC = p->apCsr[i];
3625 assert( pC!=0 );
3626 assert( pC->pCursor!=0 || pC->pseudoTable );
3627 assert( pKey->flags & MEM_Int );
3628 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003629 REGISTER_TRACE(pOp->p2, pData);
3630 REGISTER_TRACE(pOp->p3, pKey);
danielk19775f8d8a82004-05-11 00:28:42 +00003631
drha05a7222008-01-19 03:35:58 +00003632 iKey = intToKey(pKey->u.i);
3633 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
3634 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
3635 if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){
3636 pC->nextRowidValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003637 }
drha05a7222008-01-19 03:35:58 +00003638 if( pData->flags & MEM_Null ){
3639 pData->z = 0;
3640 pData->n = 0;
3641 }else{
3642 assert( pData->flags & (MEM_Blob|MEM_Str) );
3643 }
3644 if( pC->pseudoTable ){
danielk19779882d992008-03-27 17:59:01 +00003645 if( !pC->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00003646 sqlite3DbFree(db, pC->pData);
danielk19779882d992008-03-27 17:59:01 +00003647 }
drha05a7222008-01-19 03:35:58 +00003648 pC->iKey = iKey;
3649 pC->nData = pData->n;
danielk19775f096132008-03-28 15:44:09 +00003650 if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){
drha05a7222008-01-19 03:35:58 +00003651 pC->pData = pData->z;
danielk19779882d992008-03-27 17:59:01 +00003652 if( !pC->ephemPseudoTable ){
3653 pData->flags &= ~MEM_Dyn;
3654 pData->flags |= MEM_Ephem;
danielk19775f096132008-03-28 15:44:09 +00003655 pData->zMalloc = 0;
danielk19779882d992008-03-27 17:59:01 +00003656 }
drha05a7222008-01-19 03:35:58 +00003657 }else{
drhe5ae5732008-06-15 02:51:47 +00003658 pC->pData = sqlite3Malloc( pC->nData+2 );
drha05a7222008-01-19 03:35:58 +00003659 if( !pC->pData ) goto no_mem;
3660 memcpy(pC->pData, pData->z, pC->nData);
3661 pC->pData[pC->nData] = 0;
3662 pC->pData[pC->nData+1] = 0;
3663 }
3664 pC->nullRow = 0;
3665 }else{
3666 int nZero;
3667 if( pData->flags & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +00003668 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003669 }else{
3670 nZero = 0;
3671 }
3672 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3673 pData->z, pData->n, nZero,
3674 pOp->p5 & OPFLAG_APPEND);
3675 }
3676
3677 pC->rowidIsValid = 0;
3678 pC->deferredMoveto = 0;
3679 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003680
drha05a7222008-01-19 03:35:58 +00003681 /* Invoke the update-hook if required. */
3682 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3683 const char *zDb = db->aDb[pC->iDb].zName;
3684 const char *zTbl = pOp->p4.z;
3685 int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3686 assert( pC->isTable );
3687 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3688 assert( pC->iDb>=0 );
3689 }
drh5e00f6c2001-09-13 13:46:56 +00003690 break;
3691}
3692
drh98757152008-01-09 23:04:12 +00003693/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003694**
drh5edc3122001-09-13 21:53:09 +00003695** Delete the record at which the P1 cursor is currently pointing.
3696**
3697** The cursor will be left pointing at either the next or the previous
3698** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003699** the next Next instruction will be a no-op. Hence it is OK to delete
3700** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003701**
rdcb0c374f2004-02-20 22:53:38 +00003702** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003703** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003704**
drh91fd4d42008-01-19 20:11:25 +00003705** P1 must not be pseudo-table. It has to be a real table with
3706** multiple rows.
3707**
3708** If P4 is not NULL, then it is the name of the table that P1 is
3709** pointing to. The update hook will be invoked, if it exists.
3710** If P4 is not NULL then the P1 cursor must have been positioned
3711** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003712*/
drh9cbf3422008-01-17 16:22:13 +00003713case OP_Delete: {
drh5e00f6c2001-09-13 13:46:56 +00003714 int i = pOp->p1;
drh91fd4d42008-01-19 20:11:25 +00003715 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003716 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003717
drh70ce3f02003-04-15 19:22:22 +00003718 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003719 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003720 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003721 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003722
drh91fd4d42008-01-19 20:11:25 +00003723 /* If the update-hook will be invoked, set iKey to the rowid of the
3724 ** row being deleted.
3725 */
3726 if( db->xUpdateCallback && pOp->p4.z ){
3727 assert( pC->isTable );
3728 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3729 iKey = pC->lastRowid;
3730 }
danielk197794eb6a12005-12-15 15:22:08 +00003731
drh91fd4d42008-01-19 20:11:25 +00003732 rc = sqlite3VdbeCursorMoveto(pC);
3733 if( rc ) goto abort_due_to_error;
3734 rc = sqlite3BtreeDelete(pC->pCursor);
3735 pC->nextRowidValid = 0;
3736 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003737
drh91fd4d42008-01-19 20:11:25 +00003738 /* Invoke the update-hook if required. */
3739 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3740 const char *zDb = db->aDb[pC->iDb].zName;
3741 const char *zTbl = pOp->p4.z;
3742 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3743 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003744 }
danielk1977b28af712004-06-21 06:50:26 +00003745 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003746 break;
3747}
3748
danielk1977b28af712004-06-21 06:50:26 +00003749/* Opcode: ResetCount P1 * *
rdcb0c374f2004-02-20 22:53:38 +00003750**
danielk1977b28af712004-06-21 06:50:26 +00003751** This opcode resets the VMs internal change counter to 0. If P1 is true,
3752** then the value of the change counter is copied to the database handle
3753** change counter (returned by subsequent calls to sqlite3_changes())
3754** before it is reset. This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003755*/
drh9cbf3422008-01-17 16:22:13 +00003756case OP_ResetCount: {
danielk1977b28af712004-06-21 06:50:26 +00003757 if( pOp->p1 ){
drh344737f2004-09-19 00:50:20 +00003758 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003759 }
3760 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003761 break;
3762}
3763
drh98757152008-01-09 23:04:12 +00003764/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00003765**
drh98757152008-01-09 23:04:12 +00003766** Write into register P2 the complete row data for cursor P1.
3767** There is no interpretation of the data.
3768** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003769** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00003770**
drhde4fcfd2008-01-19 23:50:26 +00003771** If the P1 cursor must be pointing to a valid row (not a NULL row)
3772** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003773*/
drh98757152008-01-09 23:04:12 +00003774/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00003775**
drh98757152008-01-09 23:04:12 +00003776** Write into register P2 the complete row key for cursor P1.
3777** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00003778** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003779** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00003780**
drhde4fcfd2008-01-19 23:50:26 +00003781** If the P1 cursor must be pointing to a valid row (not a NULL row)
3782** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00003783*/
danielk1977a7a8e142008-02-13 18:25:27 +00003784case OP_RowKey:
3785case OP_RowData: {
drh70ce3f02003-04-15 19:22:22 +00003786 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003787 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00003788 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00003789 u32 n;
drh70ce3f02003-04-15 19:22:22 +00003790
danielk1977a7a8e142008-02-13 18:25:27 +00003791 pOut = &p->aMem[pOp->p2];
3792
drhf0863fe2005-06-12 21:35:51 +00003793 /* Note that RowKey and RowData are really exactly the same instruction */
drh70ce3f02003-04-15 19:22:22 +00003794 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003795 pC = p->apCsr[i];
drhf0863fe2005-06-12 21:35:51 +00003796 assert( pC->isTable || pOp->opcode==OP_RowKey );
3797 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003798 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00003799 assert( pC->nullRow==0 );
3800 assert( pC->pseudoTable==0 );
3801 assert( pC->pCursor!=0 );
3802 pCrsr = pC->pCursor;
3803 rc = sqlite3VdbeCursorMoveto(pC);
3804 if( rc ) goto abort_due_to_error;
3805 if( pC->isIndex ){
3806 i64 n64;
3807 assert( !pC->isTable );
3808 sqlite3BtreeKeySize(pCrsr, &n64);
drhbb4957f2008-03-20 14:03:29 +00003809 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00003810 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00003811 }
drh9c1905f2008-12-10 22:32:56 +00003812 n = (int)n64;
drhde4fcfd2008-01-19 23:50:26 +00003813 }else{
3814 sqlite3BtreeDataSize(pCrsr, &n);
danielk197764202cf2008-11-17 15:31:47 +00003815 if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00003816 goto too_big;
3817 }
drhde4fcfd2008-01-19 23:50:26 +00003818 }
danielk1977a7a8e142008-02-13 18:25:27 +00003819 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
3820 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00003821 }
danielk1977a7a8e142008-02-13 18:25:27 +00003822 pOut->n = n;
3823 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00003824 if( pC->isIndex ){
3825 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
3826 }else{
3827 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00003828 }
danielk197796cb76f2008-01-04 13:24:28 +00003829 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00003830 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00003831 break;
3832}
3833
drh2133d822008-01-03 18:44:59 +00003834/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003835**
drh2133d822008-01-03 18:44:59 +00003836** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00003837** P1 is currently point to.
drh5e00f6c2001-09-13 13:46:56 +00003838*/
drh4c583122008-01-04 22:01:03 +00003839case OP_Rowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003840 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003841 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00003842 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003843
drh70ce3f02003-04-15 19:22:22 +00003844 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003845 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003846 assert( pC!=0 );
drh536065a2005-01-26 21:55:31 +00003847 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003848 if( rc ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003849 if( pC->rowidIsValid ){
3850 v = pC->lastRowid;
drh70ce3f02003-04-15 19:22:22 +00003851 }else if( pC->pseudoTable ){
3852 v = keyToInt(pC->iKey);
drha05a7222008-01-19 03:35:58 +00003853 }else if( pC->nullRow ){
drh4c583122008-01-04 22:01:03 +00003854 /* Leave the rowid set to a NULL */
drhd60ccc62003-06-24 10:39:46 +00003855 break;
drh70ce3f02003-04-15 19:22:22 +00003856 }else{
3857 assert( pC->pCursor!=0 );
danielk1977e0d4b062004-06-28 01:11:46 +00003858 sqlite3BtreeKeySize(pC->pCursor, &v);
drh70ce3f02003-04-15 19:22:22 +00003859 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003860 }
drh4c583122008-01-04 22:01:03 +00003861 pOut->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003862 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00003863 break;
3864}
3865
drh9cbf3422008-01-17 16:22:13 +00003866/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00003867**
3868** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00003869** that occur while the cursor is on the null row will always
3870** write a NULL.
drh17f71932002-02-21 12:01:27 +00003871*/
drh9cbf3422008-01-17 16:22:13 +00003872case OP_NullRow: {
drh17f71932002-02-21 12:01:27 +00003873 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003874 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00003875
drh70ce3f02003-04-15 19:22:22 +00003876 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003877 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003878 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00003879 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00003880 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00003881 if( pC->pCursor ){
3882 sqlite3BtreeClearCursor(pC->pCursor);
3883 }
drh17f71932002-02-21 12:01:27 +00003884 break;
3885}
3886
drh9cbf3422008-01-17 16:22:13 +00003887/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00003888**
drhf0863fe2005-06-12 21:35:51 +00003889** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00003890** will refer to the last entry in the database table or index.
3891** If the table or index is empty and P2>0, then jump immediately to P2.
3892** If P2 is 0 or if the table or index is not empty, fall through
3893** to the following instruction.
3894*/
drh9cbf3422008-01-17 16:22:13 +00003895case OP_Last: { /* jump */
drh9562b552002-02-19 15:00:07 +00003896 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003897 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00003898 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00003899 int res;
drh9562b552002-02-19 15:00:07 +00003900
drh70ce3f02003-04-15 19:22:22 +00003901 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003902 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003903 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00003904 pCrsr = pC->pCursor;
3905 assert( pCrsr!=0 );
3906 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00003907 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00003908 pC->deferredMoveto = 0;
3909 pC->cacheStatus = CACHE_STALE;
3910 if( res && pOp->p2>0 ){
3911 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00003912 }
3913 break;
3914}
3915
drh0342b1f2005-09-01 03:07:44 +00003916
drh9cbf3422008-01-17 16:22:13 +00003917/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00003918**
3919** This opcode does exactly the same thing as OP_Rewind except that
3920** it increments an undocumented global variable used for testing.
3921**
3922** Sorting is accomplished by writing records into a sorting index,
3923** then rewinding that index and playing it back from beginning to
3924** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3925** rewinding so that the global variable will be incremented and
3926** regression tests can determine whether or not the optimizer is
3927** correctly optimizing out sorts.
3928*/
drh9cbf3422008-01-17 16:22:13 +00003929case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00003930#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00003931 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00003932 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00003933#endif
drhd1d38482008-10-07 23:46:38 +00003934 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00003935 /* Fall through into OP_Rewind */
3936}
drh9cbf3422008-01-17 16:22:13 +00003937/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003938**
drhf0863fe2005-06-12 21:35:51 +00003939** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003940** will refer to the first entry in the database table or index.
3941** If the table or index is empty and P2>0, then jump immediately to P2.
3942** If P2 is 0 or if the table or index is not empty, fall through
3943** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003944*/
drh9cbf3422008-01-17 16:22:13 +00003945case OP_Rewind: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +00003946 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003947 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003948 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003949 int res;
drh5e00f6c2001-09-13 13:46:56 +00003950
drh70ce3f02003-04-15 19:22:22 +00003951 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003952 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003953 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003954 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003955 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00003956 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00003957 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003958 pC->cacheStatus = CACHE_STALE;
drh70ce3f02003-04-15 19:22:22 +00003959 }else{
drhf4dada72004-05-11 09:57:35 +00003960 res = 1;
3961 }
drh9c1905f2008-12-10 22:32:56 +00003962 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00003963 assert( pOp->p2>0 && pOp->p2<p->nOp );
3964 if( res ){
drhf4dada72004-05-11 09:57:35 +00003965 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003966 }
3967 break;
3968}
3969
drh9cbf3422008-01-17 16:22:13 +00003970/* Opcode: Next P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003971**
3972** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003973** table or index. If there are no more key/value pairs then fall through
3974** to the following instruction. But if the cursor advance was successful,
3975** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003976**
drh60a713c2008-01-21 16:22:45 +00003977** The P1 cursor must be for a real table, not a pseudo-table.
3978**
drhc045ec52002-12-04 20:01:06 +00003979** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003980*/
drh9cbf3422008-01-17 16:22:13 +00003981/* Opcode: Prev P1 P2 * * *
drhc045ec52002-12-04 20:01:06 +00003982**
3983** Back up cursor P1 so that it points to the previous key/data pair in its
3984** table or index. If there is no previous key/value pairs then fall through
3985** to the following instruction. But if the cursor backup was successful,
3986** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00003987**
3988** The P1 cursor must be for a real table, not a pseudo-table.
drhc045ec52002-12-04 20:01:06 +00003989*/
drh9cbf3422008-01-17 16:22:13 +00003990case OP_Prev: /* jump */
3991case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00003992 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00003993 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00003994 int res;
drh8721ce42001-11-07 14:22:00 +00003995
drhcaec2f12003-01-07 02:47:47 +00003996 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00003997 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003998 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00003999 if( pC==0 ){
4000 break; /* See ticket #2273 */
4001 }
drh60a713c2008-01-21 16:22:45 +00004002 pCrsr = pC->pCursor;
4003 assert( pCrsr );
drha3460582008-07-11 21:02:53 +00004004 res = 1;
4005 assert( pC->deferredMoveto==0 );
4006 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
4007 sqlite3BtreePrevious(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004008 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004009 pC->cacheStatus = CACHE_STALE;
4010 if( res==0 ){
4011 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004012 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004013#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004014 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004015#endif
drh8721ce42001-11-07 14:22:00 +00004016 }
drhf0863fe2005-06-12 21:35:51 +00004017 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004018 break;
4019}
4020
drh9cbf3422008-01-17 16:22:13 +00004021/* Opcode: IdxInsert P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004022**
drhaa9b8962008-01-08 02:57:55 +00004023** Register P2 holds a SQL index key made using the
4024** MakeIdxRec instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004025** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004026**
drhaa9b8962008-01-08 02:57:55 +00004027** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004028** insert is likely to be an append.
4029**
drhf0863fe2005-06-12 21:35:51 +00004030** This instruction only works for indices. The equivalent instruction
4031** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004032*/
drh9cbf3422008-01-17 16:22:13 +00004033case OP_IdxInsert: { /* in2 */
drh5e00f6c2001-09-13 13:46:56 +00004034 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004035 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004036 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00004037 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004038 assert( p->apCsr[i]!=0 );
drhaa9b8962008-01-08 02:57:55 +00004039 assert( pIn2->flags & MEM_Blob );
drhd7556d22004-05-14 21:59:40 +00004040 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drhf0863fe2005-06-12 21:35:51 +00004041 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004042 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004043 if( rc==SQLITE_OK ){
drhaa9b8962008-01-08 02:57:55 +00004044 int nKey = pIn2->n;
4045 const char *zKey = pIn2->z;
4046 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3);
danielk1977d908f5a2007-05-11 07:08:28 +00004047 assert( pC->deferredMoveto==0 );
4048 pC->cacheStatus = CACHE_STALE;
4049 }
drh5e00f6c2001-09-13 13:46:56 +00004050 }
drh5e00f6c2001-09-13 13:46:56 +00004051 break;
4052}
4053
drhd1d38482008-10-07 23:46:38 +00004054/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004055**
drhe14006d2008-03-25 17:23:32 +00004056** The content of P3 registers starting at register P2 form
4057** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004058** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004059*/
drhe14006d2008-03-25 17:23:32 +00004060case OP_IdxDelete: {
drh5e00f6c2001-09-13 13:46:56 +00004061 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004062 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004063 BtCursor *pCrsr;
drhe14006d2008-03-25 17:23:32 +00004064 assert( pOp->p3>0 );
4065 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem );
drh6810ce62004-01-31 19:22:56 +00004066 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004067 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00004068 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197775bab7d2006-01-23 13:09:45 +00004069 int res;
drhe14006d2008-03-25 17:23:32 +00004070 UnpackedRecord r;
4071 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004072 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004073 r.flags = 0;
drhe14006d2008-03-25 17:23:32 +00004074 r.aMem = &p->aMem[pOp->p2];
drhe63d9992008-08-13 19:11:48 +00004075 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004076 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004077 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004078 }
drh9188b382004-05-14 21:12:22 +00004079 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004080 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004081 }
drh5e00f6c2001-09-13 13:46:56 +00004082 break;
4083}
4084
drh2133d822008-01-03 18:44:59 +00004085/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004086**
drh2133d822008-01-03 18:44:59 +00004087** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004088** the end of the index key pointed to by cursor P1. This integer should be
4089** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004090**
drh3c899a62006-01-10 18:44:08 +00004091** See also: Rowid, MakeIdxRec.
drh8721ce42001-11-07 14:22:00 +00004092*/
drh4c583122008-01-04 22:01:03 +00004093case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004094 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00004095 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004096 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004097
drh6810ce62004-01-31 19:22:56 +00004098 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004099 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00004100 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00004101 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00004102
drhd7556d22004-05-14 21:59:40 +00004103 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004104 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004105 if( !pC->nullRow ){
drhb21c8cd2007-08-21 19:33:56 +00004106 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004107 if( rc!=SQLITE_OK ){
4108 goto abort_due_to_error;
4109 }
danielk1977a7a8e142008-02-13 18:25:27 +00004110 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00004111 pOut->u.i = rowid;
danielk19773d1bfea2004-05-14 11:00:53 +00004112 }
drh8721ce42001-11-07 14:22:00 +00004113 }
4114 break;
4115}
4116
danielk197761dd5832008-04-18 11:31:12 +00004117/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004118**
danielk197761dd5832008-04-18 11:31:12 +00004119** The P4 register values beginning with P3 form an unpacked index
4120** key that omits the ROWID. Compare this key value against the index
4121** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004122**
danielk197761dd5832008-04-18 11:31:12 +00004123** If the P1 index entry is greater than or equal to the key value
4124** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004125**
danielk197761dd5832008-04-18 11:31:12 +00004126** If P5 is non-zero then the key value is increased by an epsilon
4127** prior to the comparison. This make the opcode work like IdxGT except
4128** that if the key from register P3 is a prefix of the key in the cursor,
4129** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004130*/
drh98757152008-01-09 23:04:12 +00004131/* Opcode: IdxLT P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00004132**
danielk197761dd5832008-04-18 11:31:12 +00004133** The P4 register values beginning with P3 form an unpacked index
4134** key that omits the ROWID. Compare this key value against the index
4135** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004136**
danielk197761dd5832008-04-18 11:31:12 +00004137** If the P1 index entry is less than the key value then jump to P2.
4138** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004139**
danielk197761dd5832008-04-18 11:31:12 +00004140** If P5 is non-zero then the key value is increased by an epsilon prior
4141** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004142*/
drh9cbf3422008-01-17 16:22:13 +00004143case OP_IdxLT: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00004144case OP_IdxGE: { /* jump, in3 */
drh8721ce42001-11-07 14:22:00 +00004145 int i= pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00004146 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004147
drh6810ce62004-01-31 19:22:56 +00004148 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00004149 assert( p->apCsr[i]!=0 );
drh4f26bb62005-09-08 14:17:20 +00004150 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh0850b532006-01-31 19:31:43 +00004151 int res;
danielk197761dd5832008-04-18 11:31:12 +00004152 UnpackedRecord r;
drhd7556d22004-05-14 21:59:40 +00004153 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004154 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004155 assert( pOp->p4type==P4_INT32 );
4156 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004157 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004158 if( pOp->p5 ){
4159 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4160 }else{
4161 r.flags = UNPACKED_IGNORE_ROWID;
4162 }
danielk197761dd5832008-04-18 11:31:12 +00004163 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00004164 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004165 if( pOp->opcode==OP_IdxLT ){
4166 res = -res;
drha05a7222008-01-19 03:35:58 +00004167 }else{
4168 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004169 res++;
4170 }
4171 if( res>0 ){
4172 pc = pOp->p2 - 1 ;
4173 }
4174 }
4175 break;
4176}
4177
drh98757152008-01-09 23:04:12 +00004178/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004179**
4180** Delete an entire database table or index whose root page in the database
4181** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004182**
drh98757152008-01-09 23:04:12 +00004183** The table being destroyed is in the main database file if P3==0. If
4184** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004185** that is used to store tables create using CREATE TEMPORARY TABLE.
4186**
drh205f48e2004-11-05 00:43:11 +00004187** If AUTOVACUUM is enabled then it is possible that another root page
4188** might be moved into the newly deleted root page in order to keep all
4189** root pages contiguous at the beginning of the database. The former
4190** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004191** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004192** movement was required (because the table being dropped was already
4193** the last one in the database) then a zero is stored in register P2.
4194** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004195**
drhb19a2bc2001-09-16 00:13:26 +00004196** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004197*/
drh98757152008-01-09 23:04:12 +00004198case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004199 int iMoved;
drh3765df42006-06-28 18:18:09 +00004200 int iCnt;
danielk1977212b2182006-06-23 14:32:08 +00004201#ifndef SQLITE_OMIT_VIRTUALTABLE
drh5a91a532007-01-05 16:39:43 +00004202 Vdbe *pVdbe;
danielk1977212b2182006-06-23 14:32:08 +00004203 iCnt = 0;
4204 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
4205 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4206 iCnt++;
4207 }
4208 }
drh3765df42006-06-28 18:18:09 +00004209#else
4210 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004211#endif
4212 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004213 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004214 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004215 }else{
drh98757152008-01-09 23:04:12 +00004216 int iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004217 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004218 assert( (p->btreeMask & (1<<iDb))!=0 );
4219 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
danielk1977a7a8e142008-02-13 18:25:27 +00004220 MemSetTypeFlag(pOut, MEM_Int);
drh98757152008-01-09 23:04:12 +00004221 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004222#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004223 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004224 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
danielk1977e6efa742004-11-10 11:55:10 +00004225 }
drh3765df42006-06-28 18:18:09 +00004226#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004227 }
drh5e00f6c2001-09-13 13:46:56 +00004228 break;
4229}
4230
danielk1977c7af4842008-10-27 13:59:33 +00004231/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004232**
4233** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004234** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004235** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004236**
drhf57b3392001-10-08 13:22:32 +00004237** The table being clear is in the main database file if P2==0. If
4238** P2==1 then the table to be clear is in the auxiliary database file
4239** that is used to store tables create using CREATE TEMPORARY TABLE.
4240**
danielk1977c7af4842008-10-27 13:59:33 +00004241** If the P3 value is non-zero, then the table refered to must be an
4242** intkey table (an SQL table, not an index). In this case the row change
4243** count is incremented by the number of rows in the table being cleared.
4244** If P3 is greater than zero, then the value stored in register P3 is
4245** also incremented by the number of rows in the table being cleared.
4246**
drhb19a2bc2001-09-16 00:13:26 +00004247** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004248*/
drh9cbf3422008-01-17 16:22:13 +00004249case OP_Clear: {
danielk1977c7af4842008-10-27 13:59:33 +00004250 int nChange = 0;
drhfb982642007-08-30 01:19:59 +00004251 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004252 rc = sqlite3BtreeClearTable(
4253 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4254 );
4255 if( pOp->p3 ){
4256 p->nChange += nChange;
4257 if( pOp->p3>0 ){
4258 p->aMem[pOp->p3].u.i += nChange;
4259 }
4260 }
drh5edc3122001-09-13 21:53:09 +00004261 break;
4262}
4263
drh4c583122008-01-04 22:01:03 +00004264/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004265**
drh4c583122008-01-04 22:01:03 +00004266** Allocate a new table in the main database file if P1==0 or in the
4267** auxiliary database file if P1==1 or in an attached database if
4268** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004269** register P2
drh5b2fd562001-09-13 15:21:31 +00004270**
drhc6b52df2002-01-04 03:09:29 +00004271** The difference between a table and an index is this: A table must
4272** have a 4-byte integer key and can have arbitrary data. An index
4273** has an arbitrary key but no data.
4274**
drhb19a2bc2001-09-16 00:13:26 +00004275** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004276*/
drh4c583122008-01-04 22:01:03 +00004277/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004278**
drh4c583122008-01-04 22:01:03 +00004279** Allocate a new index in the main database file if P1==0 or in the
4280** auxiliary database file if P1==1 or in an attached database if
4281** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004282** register P2.
drhf57b3392001-10-08 13:22:32 +00004283**
drhc6b52df2002-01-04 03:09:29 +00004284** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004285*/
drh4c583122008-01-04 22:01:03 +00004286case OP_CreateIndex: /* out2-prerelease */
4287case OP_CreateTable: { /* out2-prerelease */
drh88a003e2008-12-11 16:17:03 +00004288 int pgno = 0;
drhf328bc82004-05-10 23:29:49 +00004289 int flags;
drh234c39d2004-07-24 03:30:47 +00004290 Db *pDb;
4291 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004292 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004293 pDb = &db->aDb[pOp->p1];
4294 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004295 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004296 /* flags = BTREE_INTKEY; */
4297 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004298 }else{
drhf328bc82004-05-10 23:29:49 +00004299 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00004300 }
drh234c39d2004-07-24 03:30:47 +00004301 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004302 pOut->u.i = pgno;
4303 MemSetTypeFlag(pOut, MEM_Int);
drh5b2fd562001-09-13 15:21:31 +00004304 break;
4305}
4306
drh98757152008-01-09 23:04:12 +00004307/* Opcode: ParseSchema P1 P2 * P4 *
drh234c39d2004-07-24 03:30:47 +00004308**
4309** Read and parse all entries from the SQLITE_MASTER table of database P1
drh66a51672008-01-03 00:01:23 +00004310** that match the WHERE clause P4. P2 is the "force" flag. Always do
drh3c23a882007-01-09 14:01:13 +00004311** the parsing if P2 is true. If P2 is false, then this routine is a
4312** no-op if the schema is not currently loaded. In other words, if P2
4313** is false, the SQLITE_MASTER table is only parsed if the rest of the
4314** schema is already loaded into the symbol table.
drh234c39d2004-07-24 03:30:47 +00004315**
4316** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004317** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004318*/
drh9cbf3422008-01-17 16:22:13 +00004319case OP_ParseSchema: {
drh234c39d2004-07-24 03:30:47 +00004320 char *zSql;
4321 int iDb = pOp->p1;
4322 const char *zMaster;
4323 InitData initData;
4324
4325 assert( iDb>=0 && iDb<db->nDb );
drh3c23a882007-01-09 14:01:13 +00004326 if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
4327 break;
4328 }
danielk197753c0f742005-03-29 03:10:59 +00004329 zMaster = SCHEMA_TABLE(iDb);
drh234c39d2004-07-24 03:30:47 +00004330 initData.db = db;
drhece3c722006-09-23 20:36:01 +00004331 initData.iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004332 initData.pzErrMsg = &p->zErrMsg;
danielk19771e536952007-08-16 10:09:01 +00004333 zSql = sqlite3MPrintf(db,
drhece3c722006-09-23 20:36:01 +00004334 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
danielk19772dca4ac2008-01-03 11:50:29 +00004335 db->aDb[iDb].zName, zMaster, pOp->p4.z);
drh71c697e2004-08-08 23:39:19 +00004336 if( zSql==0 ) goto no_mem;
drh7e8b8482008-01-23 03:03:05 +00004337 (void)sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +00004338 assert( db->init.busy==0 );
4339 db->init.busy = 1;
drhc456e572008-08-11 18:44:58 +00004340 initData.rc = SQLITE_OK;
drh17435752007-08-16 04:30:38 +00004341 assert( !db->mallocFailed );
drh234c39d2004-07-24 03:30:47 +00004342 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhc456e572008-08-11 18:44:58 +00004343 if( rc==SQLITE_OK ) rc = initData.rc;
drh633e6d52008-07-28 19:34:53 +00004344 sqlite3DbFree(db, zSql);
drh234c39d2004-07-24 03:30:47 +00004345 db->init.busy = 0;
drh7e8b8482008-01-23 03:03:05 +00004346 (void)sqlite3SafetyOn(db);
danielk1977261919c2005-12-06 12:52:59 +00004347 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004348 goto no_mem;
4349 }
drh234c39d2004-07-24 03:30:47 +00004350 break;
4351}
4352
drhcfed7bc2006-03-13 14:28:05 +00004353#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
drh98757152008-01-09 23:04:12 +00004354/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004355**
4356** Read the sqlite_stat1 table for database P1 and load the content
4357** of that table into the internal index hash table. This will cause
4358** the analysis to be used when preparing all subsequent queries.
4359*/
drh9cbf3422008-01-17 16:22:13 +00004360case OP_LoadAnalysis: {
drh497e4462005-07-23 03:18:40 +00004361 int iDb = pOp->p1;
4362 assert( iDb>=0 && iDb<db->nDb );
drhcf1be452007-05-12 12:08:51 +00004363 rc = sqlite3AnalysisLoad(db, iDb);
drh497e4462005-07-23 03:18:40 +00004364 break;
4365}
drhcfed7bc2006-03-13 14:28:05 +00004366#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
drh497e4462005-07-23 03:18:40 +00004367
drh98757152008-01-09 23:04:12 +00004368/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004369**
4370** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004371** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004372** is dropped in order to keep the internal representation of the
4373** schema consistent with what is on disk.
4374*/
drh9cbf3422008-01-17 16:22:13 +00004375case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004376 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004377 break;
4378}
4379
drh98757152008-01-09 23:04:12 +00004380/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004381**
4382** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004383** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004384** is dropped in order to keep the internal representation of the
4385** schema consistent with what is on disk.
4386*/
drh9cbf3422008-01-17 16:22:13 +00004387case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004388 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004389 break;
4390}
4391
drh98757152008-01-09 23:04:12 +00004392/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004393**
4394** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004395** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004396** is dropped in order to keep the internal representation of the
4397** schema consistent with what is on disk.
4398*/
drh9cbf3422008-01-17 16:22:13 +00004399case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004400 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004401 break;
4402}
4403
drh234c39d2004-07-24 03:30:47 +00004404
drhb7f91642004-10-31 02:22:47 +00004405#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004406/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004407**
drh98757152008-01-09 23:04:12 +00004408** Do an analysis of the currently open database. Store in
4409** register P1 the text of an error message describing any problems.
4410** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004411**
drh98757152008-01-09 23:04:12 +00004412** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004413** At most reg(P3) errors will be reported.
4414** In other words, the analysis stops as soon as reg(P1) errors are
4415** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004416**
drh79069752004-05-22 21:30:40 +00004417** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004418** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004419** total.
drh21504322002-06-25 13:16:02 +00004420**
drh98757152008-01-09 23:04:12 +00004421** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004422** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004423**
drh1dcdbc02007-01-27 02:24:54 +00004424** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004425*/
drhaaab5722002-02-19 13:39:21 +00004426case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004427 int nRoot; /* Number of tables to check. (Number of root pages.) */
4428 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4429 int j; /* Loop counter */
4430 int nErr; /* Number of errors reported */
4431 char *z; /* Text of the error report */
4432 Mem *pnErr; /* Register keeping track of errors remaining */
4433
4434 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004435 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004436 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004437 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004438 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4439 pnErr = &p->aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004440 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004441 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
4442 pIn1 = &p->aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004443 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004444 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004445 }
4446 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004447 assert( pOp->p5<db->nDb );
4448 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4449 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004450 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004451 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004452 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004453 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004454 if( nErr==0 ){
4455 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004456 }else if( z==0 ){
4457 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004458 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004459 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004460 }
drhb7654112008-01-12 12:48:07 +00004461 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004462 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004463 break;
4464}
drhb7f91642004-10-31 02:22:47 +00004465#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004466
drh3d4501e2008-12-04 20:40:10 +00004467/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004468**
drh3d4501e2008-12-04 20:40:10 +00004469** Insert the integer value held by register P2 into a boolean index
4470** held in register P1.
4471**
4472** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004473*/
drh3d4501e2008-12-04 20:40:10 +00004474case OP_RowSetAdd: { /* in2 */
4475 Mem *pIdx;
4476 Mem *pVal;
4477 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4478 pIdx = &p->aMem[pOp->p1];
4479 assert( pOp->p2>0 && pOp->p2<=p->nMem );
4480 pVal = &p->aMem[pOp->p2];
4481 assert( (pVal->flags & MEM_Int)!=0 );
4482 if( (pIdx->flags & MEM_RowSet)==0 ){
4483 sqlite3VdbeMemSetRowSet(pIdx);
drh8d993632008-12-04 22:17:55 +00004484 if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004485 }
4486 sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i);
4487 break;
4488}
4489
4490/* Opcode: RowSetRead P1 P2 P3 * *
4491**
4492** Extract the smallest value from boolean index P1 and put that value into
4493** register P3. Or, if boolean index P1 is initially empty, leave P3
4494** unchanged and jump to instruction P2.
4495*/
4496case OP_RowSetRead: { /* jump, out3 */
4497 Mem *pIdx;
4498 i64 val;
4499 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4500 CHECK_FOR_INTERRUPT;
4501 pIdx = &p->aMem[pOp->p1];
drhdd5f5a62008-12-23 13:35:23 +00004502 pOut = &p->aMem[pOp->p3];
drh3d4501e2008-12-04 20:40:10 +00004503 if( (pIdx->flags & MEM_RowSet)==0
4504 || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0
4505 ){
4506 /* The boolean index is empty */
4507 sqlite3VdbeMemSetNull(pIdx);
4508 pc = pOp->p2 - 1;
4509 }else{
4510 /* A value was pulled from the index */
4511 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh3d4501e2008-12-04 20:40:10 +00004512 sqlite3VdbeMemSetInt64(pOut, val);
drh17435752007-08-16 04:30:38 +00004513 }
drh5e00f6c2001-09-13 13:46:56 +00004514 break;
4515}
4516
drh5e00f6c2001-09-13 13:46:56 +00004517
danielk197793758c82005-01-21 08:13:14 +00004518#ifndef SQLITE_OMIT_TRIGGER
rdcb0c374f2004-02-20 22:53:38 +00004519/* Opcode: ContextPush * * *
4520**
4521** Save the current Vdbe context such that it can be restored by a ContextPop
4522** opcode. The context stores the last insert row id, the last statement change
4523** count, and the current statement change count.
4524*/
drh9cbf3422008-01-17 16:22:13 +00004525case OP_ContextPush: {
drh344737f2004-09-19 00:50:20 +00004526 int i = p->contextStackTop++;
4527 Context *pContext;
danielk1977b28af712004-06-21 06:50:26 +00004528
drh344737f2004-09-19 00:50:20 +00004529 assert( i>=0 );
danielk1977b28af712004-06-21 06:50:26 +00004530 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
drh344737f2004-09-19 00:50:20 +00004531 if( i>=p->contextStackDepth ){
4532 p->contextStackDepth = i+1;
danielk19771e536952007-08-16 10:09:01 +00004533 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
drhcf643722007-03-27 13:36:37 +00004534 sizeof(Context)*(i+1));
drh344737f2004-09-19 00:50:20 +00004535 if( p->contextStack==0 ) goto no_mem;
4536 }
4537 pContext = &p->contextStack[i];
4538 pContext->lastRowid = db->lastRowid;
4539 pContext->nChange = p->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004540 break;
4541}
4542
4543/* Opcode: ContextPop * * *
4544**
4545** Restore the Vdbe context to the state it was in when contextPush was last
4546** executed. The context stores the last insert row id, the last statement
4547** change count, and the current statement change count.
4548*/
drh9cbf3422008-01-17 16:22:13 +00004549case OP_ContextPop: {
drh344737f2004-09-19 00:50:20 +00004550 Context *pContext = &p->contextStack[--p->contextStackTop];
4551 assert( p->contextStackTop>=0 );
4552 db->lastRowid = pContext->lastRowid;
4553 p->nChange = pContext->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004554 break;
4555}
danielk197793758c82005-01-21 08:13:14 +00004556#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004557
drh205f48e2004-11-05 00:43:11 +00004558#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00004559/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00004560**
drh98757152008-01-09 23:04:12 +00004561** Set the value of register P1 to the maximum of its current value
4562** and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00004563**
4564** This instruction throws an error if the memory cell is not initially
4565** an integer.
4566*/
drh9cbf3422008-01-17 16:22:13 +00004567case OP_MemMax: { /* in1, in2 */
drh98757152008-01-09 23:04:12 +00004568 sqlite3VdbeMemIntegerify(pIn1);
4569 sqlite3VdbeMemIntegerify(pIn2);
4570 if( pIn1->u.i<pIn2->u.i){
4571 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00004572 }
4573 break;
4574}
4575#endif /* SQLITE_OMIT_AUTOINCREMENT */
4576
drh98757152008-01-09 23:04:12 +00004577/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00004578**
drh98757152008-01-09 23:04:12 +00004579** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004580**
drh98757152008-01-09 23:04:12 +00004581** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004582** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00004583*/
drh9cbf3422008-01-17 16:22:13 +00004584case OP_IfPos: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004585 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004586 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00004587 pc = pOp->p2 - 1;
4588 }
4589 break;
4590}
4591
drh98757152008-01-09 23:04:12 +00004592/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00004593**
drh98757152008-01-09 23:04:12 +00004594** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00004595**
drh98757152008-01-09 23:04:12 +00004596** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00004597** not contain an integer. An assertion fault will result if you try.
4598*/
drh9cbf3422008-01-17 16:22:13 +00004599case OP_IfNeg: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004600 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004601 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00004602 pc = pOp->p2 - 1;
4603 }
4604 break;
4605}
4606
drh98757152008-01-09 23:04:12 +00004607/* Opcode: IfZero P1 P2 * * *
drhec7429a2005-10-06 16:53:14 +00004608**
drh98757152008-01-09 23:04:12 +00004609** If the value of register P1 is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004610**
drh98757152008-01-09 23:04:12 +00004611** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004612** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00004613*/
drh9cbf3422008-01-17 16:22:13 +00004614case OP_IfZero: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004615 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004616 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00004617 pc = pOp->p2 - 1;
4618 }
4619 break;
4620}
4621
drh98757152008-01-09 23:04:12 +00004622/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00004623**
drh0bce8352002-02-28 00:41:10 +00004624** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00004625** function has P5 arguments. P4 is a pointer to the FuncDef
4626** structure that specifies the function. Use register
4627** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00004628**
drh98757152008-01-09 23:04:12 +00004629** The P5 arguments are taken from register P2 and its
4630** successors.
drhe5095352002-02-24 03:25:14 +00004631*/
drh9cbf3422008-01-17 16:22:13 +00004632case OP_AggStep: {
drh98757152008-01-09 23:04:12 +00004633 int n = pOp->p5;
drhe5095352002-02-24 03:25:14 +00004634 int i;
drh6810ce62004-01-31 19:22:56 +00004635 Mem *pMem, *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004636 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004637 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004638
drh6810ce62004-01-31 19:22:56 +00004639 assert( n>=0 );
drh98757152008-01-09 23:04:12 +00004640 pRec = &p->aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00004641 apVal = p->apArg;
4642 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00004643 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004644 apVal[i] = pRec;
drh8079a0d2006-01-12 17:20:50 +00004645 storeTypeInfo(pRec, encoding);
drhe5095352002-02-24 03:25:14 +00004646 }
danielk19772dca4ac2008-01-03 11:50:29 +00004647 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00004648 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4649 ctx.pMem = pMem = &p->aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00004650 pMem->n++;
drh90669c12006-01-20 15:45:36 +00004651 ctx.s.flags = MEM_Null;
4652 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00004653 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00004654 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00004655 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00004656 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00004657 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00004658 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00004659 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00004660 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00004661 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00004662 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00004663 }
danielk19776ddcca52004-05-24 23:48:25 +00004664 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00004665 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00004666 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00004667 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00004668 }
drh90669c12006-01-20 15:45:36 +00004669 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00004670 break;
4671}
4672
drh98757152008-01-09 23:04:12 +00004673/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004674**
drh13449892005-09-07 21:22:45 +00004675** Execute the finalizer function for an aggregate. P1 is
4676** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00004677**
4678** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00004679** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00004680** argument is not used by this opcode. It is only there to disambiguate
4681** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00004682** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00004683** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00004684*/
drh9cbf3422008-01-17 16:22:13 +00004685case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00004686 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00004687 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh13449892005-09-07 21:22:45 +00004688 pMem = &p->aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00004689 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00004690 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh90669c12006-01-20 15:45:36 +00004691 if( rc==SQLITE_ERROR ){
drhf089aa42008-07-08 19:34:06 +00004692 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00004693 }
drh2dca8682008-03-21 17:13:13 +00004694 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00004695 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00004696 if( sqlite3VdbeMemTooBig(pMem) ){
4697 goto too_big;
4698 }
drh5e00f6c2001-09-13 13:46:56 +00004699 break;
4700}
4701
drh5e00f6c2001-09-13 13:46:56 +00004702
drhfdbcdee2007-03-27 14:44:50 +00004703#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00004704/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00004705**
4706** Vacuum the entire database. This opcode will cause other virtual
4707** machines to be created and run. It may not be called from within
4708** a transaction.
4709*/
drh9cbf3422008-01-17 16:22:13 +00004710case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004711 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4712 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4713 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004714 break;
4715}
drh154d4b22006-09-21 11:02:16 +00004716#endif
drh6f8c91c2003-12-07 00:24:35 +00004717
danielk1977dddbcdc2007-04-26 14:42:34 +00004718#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00004719/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00004720**
4721** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00004722** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00004723** P2. Otherwise, fall through to the next instruction.
4724*/
drh9cbf3422008-01-17 16:22:13 +00004725case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00004726 Btree *pBt;
4727
4728 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004729 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00004730 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00004731 rc = sqlite3BtreeIncrVacuum(pBt);
4732 if( rc==SQLITE_DONE ){
4733 pc = pOp->p2 - 1;
4734 rc = SQLITE_OK;
4735 }
4736 break;
4737}
4738#endif
4739
drh98757152008-01-09 23:04:12 +00004740/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00004741**
4742** Cause precompiled statements to become expired. An expired statement
4743** fails with an error code of SQLITE_SCHEMA if it is ever executed
4744** (via sqlite3_step()).
4745**
4746** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4747** then only the currently executing statement is affected.
4748*/
drh9cbf3422008-01-17 16:22:13 +00004749case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00004750 if( !pOp->p1 ){
4751 sqlite3ExpirePreparedStatements(db);
4752 }else{
4753 p->expired = 1;
4754 }
4755 break;
4756}
4757
danielk1977c00da102006-01-07 13:21:04 +00004758#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00004759/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00004760**
4761** Obtain a lock on a particular table. This instruction is only used when
4762** the shared-cache feature is enabled.
4763**
drh6a9ad3d2008-04-02 16:29:30 +00004764** If P1 is the index of the database in sqlite3.aDb[] of the database
4765** on which the lock is acquired. A readlock is obtained if P3==0 or
4766** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00004767**
4768** P2 contains the root-page of the table to lock.
4769**
drh66a51672008-01-03 00:01:23 +00004770** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00004771** used to generate an error message if the lock cannot be obtained.
4772*/
drh9cbf3422008-01-17 16:22:13 +00004773case OP_TableLock: {
danielk1977c00da102006-01-07 13:21:04 +00004774 int p1 = pOp->p1;
drh9c1905f2008-12-10 22:32:56 +00004775 u8 isWriteLock = (u8)pOp->p3;
drhfb982642007-08-30 01:19:59 +00004776 assert( p1>=0 && p1<db->nDb );
4777 assert( (p->btreeMask & (1<<p1))!=0 );
drh6a9ad3d2008-04-02 16:29:30 +00004778 assert( isWriteLock==0 || isWriteLock==1 );
danielk1977c00da102006-01-07 13:21:04 +00004779 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4780 if( rc==SQLITE_LOCKED ){
danielk19772dca4ac2008-01-03 11:50:29 +00004781 const char *z = pOp->p4.z;
drhf089aa42008-07-08 19:34:06 +00004782 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
danielk1977c00da102006-01-07 13:21:04 +00004783 }
4784 break;
4785}
drhb9bb7c12006-06-11 23:41:55 +00004786#endif /* SQLITE_OMIT_SHARED_CACHE */
4787
4788#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004789/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004790**
danielk19773e3a84d2008-08-01 17:37:40 +00004791** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
4792** xBegin method for that table.
4793**
4794** Also, whether or not P4 is set, check that this is not being called from
4795** within a callback to a virtual table xSync() method. If it is, set the
4796** error code to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00004797*/
drh9cbf3422008-01-17 16:22:13 +00004798case OP_VBegin: {
danielk19773e3a84d2008-08-01 17:37:40 +00004799 sqlite3_vtab *pVtab = pOp->p4.pVtab;
4800 rc = sqlite3VtabBegin(db, pVtab);
4801 if( pVtab ){
4802 sqlite3DbFree(db, p->zErrMsg);
4803 p->zErrMsg = pVtab->zErrMsg;
4804 pVtab->zErrMsg = 0;
4805 }
danielk1977f9e7dda2006-06-16 16:08:53 +00004806 break;
4807}
4808#endif /* SQLITE_OMIT_VIRTUALTABLE */
4809
4810#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004811/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00004812**
drh66a51672008-01-03 00:01:23 +00004813** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00004814** for that table.
4815*/
drh9cbf3422008-01-17 16:22:13 +00004816case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00004817 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00004818 break;
4819}
4820#endif /* SQLITE_OMIT_VIRTUALTABLE */
4821
4822#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004823/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004824**
drh66a51672008-01-03 00:01:23 +00004825** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00004826** of that table.
drhb9bb7c12006-06-11 23:41:55 +00004827*/
drh9cbf3422008-01-17 16:22:13 +00004828case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00004829 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00004830 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00004831 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00004832 break;
4833}
4834#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00004835
drh9eff6162006-06-12 21:59:13 +00004836#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004837/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00004838**
drh66a51672008-01-03 00:01:23 +00004839** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00004840** P1 is a cursor number. This opcode opens a cursor to the virtual
4841** table and stores that cursor in P1.
4842*/
drh9cbf3422008-01-17 16:22:13 +00004843case OP_VOpen: {
drhdfe88ec2008-11-03 20:55:06 +00004844 VdbeCursor *pCur = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004845 sqlite3_vtab_cursor *pVtabCursor = 0;
4846
danielk19772dca4ac2008-01-03 11:50:29 +00004847 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004848 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4849
4850 assert(pVtab && pModule);
4851 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4852 rc = pModule->xOpen(pVtab, &pVtabCursor);
drh633e6d52008-07-28 19:34:53 +00004853 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00004854 p->zErrMsg = pVtab->zErrMsg;
4855 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004856 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4857 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00004858 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004859 pVtabCursor->pVtab = pVtab;
4860
4861 /* Initialise vdbe cursor object */
danielk1977cd3e8f72008-03-25 09:47:35 +00004862 pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0);
danielk1977be718892006-06-23 08:05:19 +00004863 if( pCur ){
4864 pCur->pVtabCursor = pVtabCursor;
4865 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004866 }else{
drh17435752007-08-16 04:30:38 +00004867 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004868 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00004869 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004870 }
drh9eff6162006-06-12 21:59:13 +00004871 break;
4872}
4873#endif /* SQLITE_OMIT_VIRTUALTABLE */
4874
4875#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00004876/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00004877**
4878** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4879** the filtered result set is empty.
4880**
drh66a51672008-01-03 00:01:23 +00004881** P4 is either NULL or a string that was generated by the xBestIndex
4882** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00004883** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00004884**
drh9eff6162006-06-12 21:59:13 +00004885** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00004886** by P1. The integer query plan parameter to xFilter is stored in register
4887** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00004888** xFilter method. Registers P3+2..P3+1+argc are the argc
4889** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00004890** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00004891**
danielk19776dbee812008-01-03 18:39:41 +00004892** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00004893*/
drh9cbf3422008-01-17 16:22:13 +00004894case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004895 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00004896 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004897 const sqlite3_module *pModule;
danielk19776dbee812008-01-03 18:39:41 +00004898 Mem *pQuery = &p->aMem[pOp->p3];
4899 Mem *pArgc = &pQuery[1];
drh4dc754d2008-07-23 18:17:32 +00004900 sqlite3_vtab_cursor *pVtabCursor;
4901 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004902
drhdfe88ec2008-11-03 20:55:06 +00004903 VdbeCursor *pCur = p->apCsr[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00004904
4905 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004906 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00004907 pVtabCursor = pCur->pVtabCursor;
4908 pVtab = pVtabCursor->pVtab;
4909 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004910
drh9cbf3422008-01-17 16:22:13 +00004911 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00004912 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00004913 nArg = (int)pArgc->u.i;
4914 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004915
drh644a5292006-12-20 14:53:38 +00004916 /* Invoke the xFilter method */
4917 {
drh3f87d2a2006-12-20 14:31:24 +00004918 int res = 0;
drh4be8b512006-06-13 23:51:34 +00004919 int i;
4920 Mem **apArg = p->apArg;
4921 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00004922 apArg[i] = &pArgc[i+1];
drh4be8b512006-06-13 23:51:34 +00004923 storeTypeInfo(apArg[i], 0);
danielk19775fac9f82006-06-13 14:16:58 +00004924 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004925
4926 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00004927 sqlite3VtabLock(pVtab);
danielk1977be718892006-06-23 08:05:19 +00004928 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00004929 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00004930 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00004931 sqlite3DbFree(db, p->zErrMsg);
4932 p->zErrMsg = pVtab->zErrMsg;
4933 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00004934 sqlite3VtabUnlock(db, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00004935 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00004936 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00004937 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004938 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4939
danielk1977a298e902006-06-22 09:53:48 +00004940 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00004941 pc = pOp->p2 - 1;
4942 }
4943 }
drh1d454a32008-01-31 19:34:51 +00004944 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004945
drh9eff6162006-06-12 21:59:13 +00004946 break;
4947}
4948#endif /* SQLITE_OMIT_VIRTUALTABLE */
4949
4950#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004951/* Opcode: VRowid P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00004952**
drh2133d822008-01-03 18:44:59 +00004953** Store into register P2 the rowid of
drh9eff6162006-06-12 21:59:13 +00004954** the virtual-table that the P1 cursor is pointing to.
4955*/
drh4c583122008-01-04 22:01:03 +00004956case OP_VRowid: { /* out2-prerelease */
danielk19773e3a84d2008-08-01 17:37:40 +00004957 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004958 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004959 sqlite_int64 iRow;
drhdfe88ec2008-11-03 20:55:06 +00004960 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhde4fcfd2008-01-19 23:50:26 +00004961
danielk1977b7a7b9a2006-06-13 10:24:42 +00004962 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004963 if( pCur->nullRow ){
4964 break;
4965 }
danielk19773e3a84d2008-08-01 17:37:40 +00004966 pVtab = pCur->pVtabCursor->pVtab;
4967 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00004968 assert( pModule->xRowid );
4969 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4970 rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
danielk19773e3a84d2008-08-01 17:37:40 +00004971 sqlite3DbFree(db, p->zErrMsg);
4972 p->zErrMsg = pVtab->zErrMsg;
4973 pVtab->zErrMsg = 0;
drhde4fcfd2008-01-19 23:50:26 +00004974 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977a7a8e142008-02-13 18:25:27 +00004975 MemSetTypeFlag(pOut, MEM_Int);
drhde4fcfd2008-01-19 23:50:26 +00004976 pOut->u.i = iRow;
drh9eff6162006-06-12 21:59:13 +00004977 break;
4978}
4979#endif /* SQLITE_OMIT_VIRTUALTABLE */
4980
4981#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004982/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00004983**
drh2133d822008-01-03 18:44:59 +00004984** Store the value of the P2-th column of
4985** the row of the virtual-table that the
4986** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00004987*/
4988case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00004989 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004990 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004991 Mem *pDest;
4992 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004993
drhdfe88ec2008-11-03 20:55:06 +00004994 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00004995 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004996 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4997 pDest = &p->aMem[pOp->p3];
4998 if( pCur->nullRow ){
4999 sqlite3VdbeMemSetNull(pDest);
5000 break;
5001 }
danielk19773e3a84d2008-08-01 17:37:40 +00005002 pVtab = pCur->pVtabCursor->pVtab;
5003 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005004 assert( pModule->xColumn );
5005 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005006
5007 /* The output cell may already have a buffer allocated. Move
5008 ** the current contents to sContext.s so in case the user-function
5009 ** can use the already allocated buffer instead of allocating a
5010 ** new one.
5011 */
5012 sqlite3VdbeMemMove(&sContext.s, pDest);
5013 MemSetTypeFlag(&sContext.s, MEM_Null);
5014
drhde4fcfd2008-01-19 23:50:26 +00005015 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5016 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
danielk19773e3a84d2008-08-01 17:37:40 +00005017 sqlite3DbFree(db, p->zErrMsg);
5018 p->zErrMsg = pVtab->zErrMsg;
5019 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005020
drhde4fcfd2008-01-19 23:50:26 +00005021 /* Copy the result of the function to the P3 register. We
5022 ** do this regardless of whether or not an error occured to ensure any
5023 ** dynamic allocation in sContext.s (a Mem struct) is released.
5024 */
5025 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005026 REGISTER_TRACE(pOp->p3, pDest);
5027 sqlite3VdbeMemMove(pDest, &sContext.s);
5028 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005029
drhde4fcfd2008-01-19 23:50:26 +00005030 if( sqlite3SafetyOn(db) ){
5031 goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005032 }
drhde4fcfd2008-01-19 23:50:26 +00005033 if( sqlite3VdbeMemTooBig(pDest) ){
5034 goto too_big;
5035 }
drh9eff6162006-06-12 21:59:13 +00005036 break;
5037}
5038#endif /* SQLITE_OMIT_VIRTUALTABLE */
5039
5040#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005041/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005042**
5043** Advance virtual table P1 to the next row in its result set and
5044** jump to instruction P2. Or, if the virtual table has reached
5045** the end of its result set, then fall through to the next instruction.
5046*/
drh9cbf3422008-01-17 16:22:13 +00005047case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005048 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005049 const sqlite3_module *pModule;
5050 int res = 0;
5051
drhdfe88ec2008-11-03 20:55:06 +00005052 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005053 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005054 if( pCur->nullRow ){
5055 break;
5056 }
danielk19773e3a84d2008-08-01 17:37:40 +00005057 pVtab = pCur->pVtabCursor->pVtab;
5058 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005059 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005060
drhde4fcfd2008-01-19 23:50:26 +00005061 /* Invoke the xNext() method of the module. There is no way for the
5062 ** underlying implementation to return an error if one occurs during
5063 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5064 ** data is available) and the error code returned when xColumn or
5065 ** some other method is next invoked on the save virtual table cursor.
5066 */
5067 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00005068 sqlite3VtabLock(pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005069 p->inVtabMethod = 1;
5070 rc = pModule->xNext(pCur->pVtabCursor);
5071 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005072 sqlite3DbFree(db, p->zErrMsg);
5073 p->zErrMsg = pVtab->zErrMsg;
5074 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00005075 sqlite3VtabUnlock(db, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005076 if( rc==SQLITE_OK ){
5077 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005078 }
drhde4fcfd2008-01-19 23:50:26 +00005079 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005080
drhde4fcfd2008-01-19 23:50:26 +00005081 if( !res ){
5082 /* If there is data, jump to P2 */
5083 pc = pOp->p2 - 1;
5084 }
drh9eff6162006-06-12 21:59:13 +00005085 break;
5086}
5087#endif /* SQLITE_OMIT_VIRTUALTABLE */
5088
danielk1977182c4ba2007-06-27 15:53:34 +00005089#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005090/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005091**
drh66a51672008-01-03 00:01:23 +00005092** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005093** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005094** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005095*/
drh9cbf3422008-01-17 16:22:13 +00005096case OP_VRename: {
danielk19772dca4ac2008-01-03 11:50:29 +00005097 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk19776dbee812008-01-03 18:39:41 +00005098 Mem *pName = &p->aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005099 assert( pVtab->pModule->xRename );
drh5b6afba2008-01-05 16:29:28 +00005100 REGISTER_TRACE(pOp->p1, pName);
danielk1977182c4ba2007-06-27 15:53:34 +00005101
danielk19776dbee812008-01-03 18:39:41 +00005102 Stringify(pName, encoding);
danielk1977182c4ba2007-06-27 15:53:34 +00005103
5104 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
5105 sqlite3VtabLock(pVtab);
danielk19776dbee812008-01-03 18:39:41 +00005106 rc = pVtab->pModule->xRename(pVtab, pName->z);
drh633e6d52008-07-28 19:34:53 +00005107 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005108 p->zErrMsg = pVtab->zErrMsg;
5109 pVtab->zErrMsg = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005110 sqlite3VtabUnlock(db, pVtab);
5111 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
5112
danielk1977182c4ba2007-06-27 15:53:34 +00005113 break;
5114}
5115#endif
drh4cbdda92006-06-14 19:00:20 +00005116
5117#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005118/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005119**
drh66a51672008-01-03 00:01:23 +00005120** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005121** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005122** are contiguous memory cells starting at P3 to pass to the xUpdate
5123** invocation. The value in register (P3+P2-1) corresponds to the
5124** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005125**
5126** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005127** The argv[0] element (which corresponds to memory cell P3)
5128** is the rowid of a row to delete. If argv[0] is NULL then no
5129** deletion occurs. The argv[1] element is the rowid of the new
5130** row. This can be NULL to have the virtual table select the new
5131** rowid for itself. The subsequent elements in the array are
5132** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005133**
5134** If P2==1 then no insert is performed. argv[0] is the rowid of
5135** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005136**
5137** P1 is a boolean flag. If it is set to true and the xUpdate call
5138** is successful, then the value returned by sqlite3_last_insert_rowid()
5139** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005140*/
drh9cbf3422008-01-17 16:22:13 +00005141case OP_VUpdate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005142 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977399918f2006-06-14 13:03:23 +00005143 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
drh4cbdda92006-06-14 19:00:20 +00005144 int nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005145 assert( pOp->p4type==P4_VTAB );
danielk1977399918f2006-06-14 13:03:23 +00005146 if( pModule->xUpdate==0 ){
drhf089aa42008-07-08 19:34:06 +00005147 sqlite3SetString(&p->zErrMsg, db, "read-only table");
danielk1977399918f2006-06-14 13:03:23 +00005148 rc = SQLITE_ERROR;
5149 }else{
5150 int i;
danielk19771f6eec52006-06-16 06:17:47 +00005151 sqlite_int64 rowid;
danielk1977399918f2006-06-14 13:03:23 +00005152 Mem **apArg = p->apArg;
danielk19772a339ff2008-01-03 17:31:44 +00005153 Mem *pX = &p->aMem[pOp->p3];
5154 for(i=0; i<nArg; i++){
drh9c419382006-06-16 21:13:21 +00005155 storeTypeInfo(pX, 0);
5156 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005157 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005158 }
danielk1977c7d54102006-06-15 07:29:00 +00005159 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh189d4af2006-09-02 20:57:52 +00005160 sqlite3VtabLock(pVtab);
danielk19771f6eec52006-06-16 06:17:47 +00005161 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drh633e6d52008-07-28 19:34:53 +00005162 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005163 p->zErrMsg = pVtab->zErrMsg;
5164 pVtab->zErrMsg = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00005165 sqlite3VtabUnlock(db, pVtab);
danielk1977c7d54102006-06-15 07:29:00 +00005166 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk19771f6eec52006-06-16 06:17:47 +00005167 if( pOp->p1 && rc==SQLITE_OK ){
5168 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5169 db->lastRowid = rowid;
5170 }
drhb5df1442008-04-10 14:00:09 +00005171 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00005172 }
drh4cbdda92006-06-14 19:00:20 +00005173 break;
danielk1977399918f2006-06-14 13:03:23 +00005174}
5175#endif /* SQLITE_OMIT_VIRTUALTABLE */
5176
danielk197759a93792008-05-15 17:48:20 +00005177#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5178/* Opcode: Pagecount P1 P2 * * *
5179**
5180** Write the current number of pages in database P1 to memory cell P2.
5181*/
5182case OP_Pagecount: { /* out2-prerelease */
5183 int p1 = pOp->p1;
5184 int nPage;
5185 Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt);
5186
danielk1977ad0132d2008-06-07 08:58:22 +00005187 rc = sqlite3PagerPagecount(pPager, &nPage);
5188 if( rc==SQLITE_OK ){
danielk197759a93792008-05-15 17:48:20 +00005189 pOut->flags = MEM_Int;
5190 pOut->u.i = nPage;
5191 }
5192 break;
5193}
5194#endif
5195
drh949f9cd2008-01-12 21:35:57 +00005196#ifndef SQLITE_OMIT_TRACE
5197/* Opcode: Trace * * * P4 *
5198**
5199** If tracing is enabled (by the sqlite3_trace()) interface, then
5200** the UTF-8 string contained in P4 is emitted on the trace callback.
5201*/
5202case OP_Trace: {
5203 if( pOp->p4.z ){
5204 if( db->xTrace ){
5205 db->xTrace(db->pTraceArg, pOp->p4.z);
5206 }
5207#ifdef SQLITE_DEBUG
5208 if( (db->flags & SQLITE_SqlTrace)!=0 ){
5209 sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z);
5210 }
5211#endif /* SQLITE_DEBUG */
5212 }
5213 break;
5214}
5215#endif
5216
drh91fd4d42008-01-19 20:11:25 +00005217
5218/* Opcode: Noop * * * * *
5219**
5220** Do nothing. This instruction is often useful as a jump
5221** destination.
drh5e00f6c2001-09-13 13:46:56 +00005222*/
drh91fd4d42008-01-19 20:11:25 +00005223/*
5224** The magic Explain opcode are only inserted when explain==2 (which
5225** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5226** This opcode records information from the optimizer. It is the
5227** the same as a no-op. This opcodesnever appears in a real VM program.
5228*/
5229default: { /* This is really OP_Noop and OP_Explain */
drh5e00f6c2001-09-13 13:46:56 +00005230 break;
5231}
5232
5233/*****************************************************************************
5234** The cases of the switch statement above this line should all be indented
5235** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5236** readability. From this point on down, the normal indentation rules are
5237** restored.
5238*****************************************************************************/
5239 }
drh6e142f52000-06-08 13:36:40 +00005240
drh7b396862003-01-01 23:06:20 +00005241#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005242 {
shane9bcbdad2008-05-29 20:22:37 +00005243 u64 elapsed = sqlite3Hwtime() - start;
5244 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005245 pOp->cnt++;
5246#if 0
shane9bcbdad2008-05-29 20:22:37 +00005247 fprintf(stdout, "%10llu ", elapsed);
danielk19774adee202004-05-08 08:23:19 +00005248 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005249#endif
5250 }
drh7b396862003-01-01 23:06:20 +00005251#endif
5252
drh6e142f52000-06-08 13:36:40 +00005253 /* The following code adds nothing to the actual functionality
5254 ** of the program. It is only here for testing and debugging.
5255 ** On the other hand, it does burn CPU cycles every time through
5256 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5257 */
5258#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005259 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005260
drhcf1023c2007-05-08 20:59:49 +00005261#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005262 if( p->trace ){
5263 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drhca48c902008-01-18 14:08:24 +00005264 if( opProperty & OPFLG_OUT2_PRERELEASE ){
drh5b6afba2008-01-05 16:29:28 +00005265 registerTrace(p->trace, pOp->p2, pOut);
drh75897232000-05-29 14:26:00 +00005266 }
drhca48c902008-01-18 14:08:24 +00005267 if( opProperty & OPFLG_OUT3 ){
drh5b6afba2008-01-05 16:29:28 +00005268 registerTrace(p->trace, pOp->p3, pOut);
5269 }
drh75897232000-05-29 14:26:00 +00005270 }
danielk1977b5402fb2005-01-12 07:15:04 +00005271#endif /* SQLITE_DEBUG */
5272#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005273 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005274
drha05a7222008-01-19 03:35:58 +00005275 /* If we reach this point, it means that execution is finished with
5276 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005277 */
drha05a7222008-01-19 03:35:58 +00005278vdbe_error_halt:
5279 assert( rc );
5280 p->rc = rc;
drh92f02c32004-09-02 14:57:08 +00005281 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005282 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5283 rc = SQLITE_ERROR;
drh900b31e2007-08-28 02:27:51 +00005284
5285 /* This is the only way out of this procedure. We have to
5286 ** release the mutexes on btrees that were acquired at the
5287 ** top. */
5288vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005289 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005290 return rc;
5291
drh023ae032007-05-08 12:12:16 +00005292 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5293 ** is encountered.
5294 */
5295too_big:
drhf089aa42008-07-08 19:34:06 +00005296 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005297 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005298 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005299
drh98640a32007-06-07 19:08:32 +00005300 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005301 */
5302no_mem:
drh17435752007-08-16 04:30:38 +00005303 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005304 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005305 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005306 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005307
5308 /* Jump to here for an SQLITE_MISUSE error.
5309 */
5310abort_due_to_misuse:
5311 rc = SQLITE_MISUSE;
5312 /* Fall thru into abort_due_to_error */
5313
5314 /* Jump to here for any other kind of fatal error. The "rc" variable
5315 ** should hold the error number.
5316 */
5317abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005318 assert( p->zErrMsg==0 );
5319 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005320 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005321 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005322 }
drha05a7222008-01-19 03:35:58 +00005323 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005324
danielk19776f8a5032004-05-10 10:34:51 +00005325 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005326 ** flag.
5327 */
5328abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005329 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005330 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005331 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005332 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005333 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005334}