blob: ef6b922a381cdfaffa3245191aec4801bc739dac [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**
drhbbce3382008-12-06 16:46:13 +000046** $Id: vdbe.c,v 1.792 2008/12/06 16:46:14 drh 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
drh7cf6e4d2004-05-19 14:56:55 +000054** moves, either by the OP_MoveXX, 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);
373 zCsr += strlen(zCsr);
374 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
375 zCsr += strlen(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));
378 zCsr += strlen(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]);
drh5bb3eb92007-05-04 13:15:55 +0000387 zCsr += strlen(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000388 if( f & MEM_Zero ){
drh5bb3eb92007-05-04 13:15:55 +0000389 sqlite3_snprintf(100, zCsr,"+%lldz",pMem->u.i);
390 zCsr += strlen(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);
410 k += strlen(&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]);
422 k += strlen(&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
502/*
drhb86ccfb2003-01-28 23:13:10 +0000503** Execute as much of a VDBE program as we can then return.
504**
danielk19774adee202004-05-08 08:23:19 +0000505** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000506** close the program with a final OP_Halt and to set up the callbacks
507** and the error message pointer.
508**
509** Whenever a row or result data is available, this routine will either
510** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000511** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000512**
513** If an attempt is made to open a locked database, then this routine
514** will either invoke the busy callback (if there is one) or it will
515** return SQLITE_BUSY.
516**
517** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000518** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000519** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
520**
521** If the callback ever returns non-zero, then the program exits
522** immediately. There will be no error message but the p->rc field is
523** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
524**
drh9468c7f2003-03-07 19:50:07 +0000525** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
526** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000527**
528** Other fatal errors return SQLITE_ERROR.
529**
danielk19774adee202004-05-08 08:23:19 +0000530** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000531** used to clean up the mess that was left behind.
532*/
danielk19774adee202004-05-08 08:23:19 +0000533int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000534 Vdbe *p /* The VDBE */
535){
536 int pc; /* The program counter */
537 Op *pOp; /* Current operation */
538 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000539 sqlite3 *db = p->db; /* The database */
drh8079a0d2006-01-12 17:20:50 +0000540 u8 encoding = ENC(db); /* The database encoding */
drh5b6afba2008-01-05 16:29:28 +0000541 Mem *pIn1, *pIn2, *pIn3; /* Input operands */
drh4c583122008-01-04 22:01:03 +0000542 Mem *pOut; /* Output operand */
drhb1fdb2a2008-01-05 04:06:03 +0000543 u8 opProperty;
drh0acb7e42008-06-25 00:12:41 +0000544 int iCompare = 0; /* Result of last OP_Compare operation */
545 int *aPermute = 0; /* Permuation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000546#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000547 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000548 int origPc; /* Program counter at start of opcode */
549#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000550#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
551 int nProgressOps = 0; /* Opcodes executed since progress callback. */
552#endif
drh23f79d02008-08-20 22:06:47 +0000553 UnpackedRecord aTempRec[16]; /* Space to hold a transient UnpackedRecord */
drhe63d9992008-08-13 19:11:48 +0000554
drhca48c902008-01-18 14:08:24 +0000555 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhb86ccfb2003-01-28 23:13:10 +0000556 assert( db->magic==SQLITE_MAGIC_BUSY );
drh4cf7c7f2007-08-28 23:28:07 +0000557 sqlite3BtreeMutexArrayEnter(&p->aMutex);
danielk19772e588c72005-12-09 14:25:08 +0000558 if( p->rc==SQLITE_NOMEM ){
559 /* This happens if a malloc() inside a call to sqlite3_column_text() or
560 ** sqlite3_column_text16() failed. */
561 goto no_mem;
562 }
drh3a840692003-01-29 22:58:26 +0000563 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
564 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000565 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000566 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000567 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000568 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000569 sqlite3VdbeIOTraceSql(p);
drh3c23a882007-01-09 14:01:13 +0000570#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000571 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000572 if( p->pc==0
573 && ((p->db->flags & SQLITE_VdbeListing) || fileExists(db, "vdbe_explain"))
drh3c23a882007-01-09 14:01:13 +0000574 ){
575 int i;
576 printf("VDBE Program Listing:\n");
577 sqlite3VdbePrintSql(p);
578 for(i=0; i<p->nOp; i++){
579 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
580 }
581 }
danielk1977861f7452008-06-05 11:39:11 +0000582 if( fileExists(db, "vdbe_trace") ){
drh3c23a882007-01-09 14:01:13 +0000583 p->trace = stdout;
584 }
danielk19772d1d86f2008-06-20 14:59:51 +0000585 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000586#endif
drhb86ccfb2003-01-28 23:13:10 +0000587 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000588 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000589 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000590#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000591 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000592 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000593#endif
drh75897232000-05-29 14:26:00 +0000594 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000595
danielk19778b60e0f2005-01-12 09:10:39 +0000596 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000597 */
danielk19778b60e0f2005-01-12 09:10:39 +0000598#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000599 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000600 if( pc==0 ){
601 printf("VDBE Execution Trace:\n");
602 sqlite3VdbePrintSql(p);
603 }
danielk19774adee202004-05-08 08:23:19 +0000604 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000605 }
drh19db9352008-03-27 22:42:51 +0000606 if( p->trace==0 && pc==0 ){
danielk19772d1d86f2008-06-20 14:59:51 +0000607 sqlite3BeginBenignMalloc();
danielk1977861f7452008-06-05 11:39:11 +0000608 if( fileExists(db, "vdbe_sqltrace") ){
drh19db9352008-03-27 22:42:51 +0000609 sqlite3VdbePrintSql(p);
610 }
danielk19772d1d86f2008-06-20 14:59:51 +0000611 sqlite3EndBenignMalloc();
drh3f7d4e42004-07-24 14:35:58 +0000612 }
613#endif
614
drh6e142f52000-06-08 13:36:40 +0000615
drhf6038712004-02-08 18:07:34 +0000616 /* Check to see if we need to simulate an interrupt. This only happens
617 ** if we have a special test build.
618 */
619#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000620 if( sqlite3_interrupt_count>0 ){
621 sqlite3_interrupt_count--;
622 if( sqlite3_interrupt_count==0 ){
623 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000624 }
625 }
626#endif
627
danielk1977348bb5d2003-10-18 09:37:26 +0000628#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
629 /* Call the progress callback if it is configured and the required number
630 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000631 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000632 ** If the progress callback returns non-zero, exit the virtual machine with
633 ** a return code SQLITE_ABORT.
634 */
drh3914aed2004-01-31 20:40:42 +0000635 if( db->xProgress ){
636 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000637 int prc;
drhf8888bb2006-05-26 19:57:19 +0000638 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000639 prc =db->xProgress(db->pProgressArg);
drhf8888bb2006-05-26 19:57:19 +0000640 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977de523ac2007-06-15 14:53:53 +0000641 if( prc!=0 ){
642 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000643 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000644 }
danielk19773fe11f32007-06-13 16:49:48 +0000645 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000646 }
drh3914aed2004-01-31 20:40:42 +0000647 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000648 }
danielk1977348bb5d2003-10-18 09:37:26 +0000649#endif
650
drh4c583122008-01-04 22:01:03 +0000651 /* Do common setup processing for any opcode that is marked
652 ** with the "out2-prerelease" tag. Such opcodes have a single
drh9cbf3422008-01-17 16:22:13 +0000653 ** output which is specified by the P2 parameter. The P2 register
drh4c583122008-01-04 22:01:03 +0000654 ** is initialized to a NULL.
655 */
drhb1fdb2a2008-01-05 04:06:03 +0000656 opProperty = opcodeProperty[pOp->opcode];
657 if( (opProperty & OPFLG_OUT2_PRERELEASE)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000658 assert( pOp->p2>0 );
659 assert( pOp->p2<=p->nMem );
660 pOut = &p->aMem[pOp->p2];
danielk19775f096132008-03-28 15:44:09 +0000661 sqlite3VdbeMemReleaseExternal(pOut);
drh4c583122008-01-04 22:01:03 +0000662 pOut->flags = MEM_Null;
drhb1fdb2a2008-01-05 04:06:03 +0000663 }else
664
665 /* Do common setup for opcodes marked with one of the following
666 ** combinations of properties.
667 **
668 ** in1
669 ** in1 in2
670 ** in1 in2 out3
671 ** in1 in3
drhb1fdb2a2008-01-05 04:06:03 +0000672 **
drh9cbf3422008-01-17 16:22:13 +0000673 ** Variables pIn1, pIn2, and pIn3 are made to point to appropriate
674 ** registers for inputs. Variable pOut points to the output register.
drhb1fdb2a2008-01-05 04:06:03 +0000675 */
676 if( (opProperty & OPFLG_IN1)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000677 assert( pOp->p1>0 );
678 assert( pOp->p1<=p->nMem );
679 pIn1 = &p->aMem[pOp->p1];
680 REGISTER_TRACE(pOp->p1, pIn1);
drhb1fdb2a2008-01-05 04:06:03 +0000681 if( (opProperty & OPFLG_IN2)!=0 ){
drh9cbf3422008-01-17 16:22:13 +0000682 assert( pOp->p2>0 );
drhaa9b8962008-01-08 02:57:55 +0000683 assert( pOp->p2<=p->nMem );
684 pIn2 = &p->aMem[pOp->p2];
685 REGISTER_TRACE(pOp->p2, pIn2);
drh9cbf3422008-01-17 16:22:13 +0000686 if( (opProperty & OPFLG_OUT3)!=0 ){
687 assert( pOp->p3>0 );
688 assert( pOp->p3<=p->nMem );
689 pOut = &p->aMem[pOp->p3];
690 }
691 }else if( (opProperty & OPFLG_IN3)!=0 ){
692 assert( pOp->p3>0 );
drhaa9b8962008-01-08 02:57:55 +0000693 assert( pOp->p3<=p->nMem );
694 pIn3 = &p->aMem[pOp->p3];
695 REGISTER_TRACE(pOp->p3, pIn3);
696 }
drh9cbf3422008-01-17 16:22:13 +0000697 }else if( (opProperty & OPFLG_IN2)!=0 ){
698 assert( pOp->p2>0 );
699 assert( pOp->p2<=p->nMem );
700 pIn2 = &p->aMem[pOp->p2];
701 REGISTER_TRACE(pOp->p2, pIn2);
702 }else if( (opProperty & OPFLG_IN3)!=0 ){
703 assert( pOp->p3>0 );
704 assert( pOp->p3<=p->nMem );
705 pIn3 = &p->aMem[pOp->p3];
706 REGISTER_TRACE(pOp->p3, pIn3);
drh4c583122008-01-04 22:01:03 +0000707 }
708
drh75897232000-05-29 14:26:00 +0000709 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000710
drh5e00f6c2001-09-13 13:46:56 +0000711/*****************************************************************************
712** What follows is a massive switch statement where each case implements a
713** separate instruction in the virtual machine. If we follow the usual
714** indentation conventions, each case should be indented by 6 spaces. But
715** that is a lot of wasted space on the left margin. So the code within
716** the switch statement will break with convention and be flush-left. Another
717** big comment (similar to this one) will mark the point in the code where
718** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000719**
720** The formatting of each case is important. The makefile for SQLite
721** generates two C files "opcodes.h" and "opcodes.c" by scanning this
722** file looking for lines that begin with "case OP_". The opcodes.h files
723** will be filled with #defines that give unique integer values to each
724** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000725** each string is the symbolic name for the corresponding opcode. If the
726** case statement is followed by a comment of the form "/# same as ... #/"
727** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000728**
drh9cbf3422008-01-17 16:22:13 +0000729** Other keywords in the comment that follows each case are used to
730** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
731** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
732** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000733**
drhac82fcf2002-09-08 17:23:41 +0000734** Documentation about VDBE opcodes is generated by scanning this file
735** for lines of that contain "Opcode:". That line and all subsequent
736** comment lines are used in the generation of the opcode.html documentation
737** file.
738**
739** SUMMARY:
740**
741** Formatting is important to scripts that scan this file.
742** Do not deviate from the formatting style currently in use.
743**
drh5e00f6c2001-09-13 13:46:56 +0000744*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000745
drh9cbf3422008-01-17 16:22:13 +0000746/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000747**
748** An unconditional jump to address P2.
749** The next instruction executed will be
750** the one at index P2 from the beginning of
751** the program.
752*/
drh9cbf3422008-01-17 16:22:13 +0000753case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000754 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000755 pc = pOp->p2 - 1;
756 break;
757}
drh75897232000-05-29 14:26:00 +0000758
drh2eb95372008-06-06 15:04:36 +0000759/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000760**
drh2eb95372008-06-06 15:04:36 +0000761** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000762** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000763*/
drh9cbf3422008-01-17 16:22:13 +0000764case OP_Gosub: { /* jump */
drh2eb95372008-06-06 15:04:36 +0000765 assert( pOp->p1>0 );
766 assert( pOp->p1<=p->nMem );
767 pIn1 = &p->aMem[pOp->p1];
768 assert( (pIn1->flags & MEM_Dyn)==0 );
769 pIn1->flags = MEM_Int;
770 pIn1->u.i = pc;
771 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000772 pc = pOp->p2 - 1;
773 break;
774}
775
drh2eb95372008-06-06 15:04:36 +0000776/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000777**
drh2eb95372008-06-06 15:04:36 +0000778** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000779*/
drh2eb95372008-06-06 15:04:36 +0000780case OP_Return: { /* in1 */
781 assert( pIn1->flags & MEM_Int );
782 pc = pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000783 break;
784}
785
drhe00ee6e2008-06-20 15:24:01 +0000786/* Opcode: Yield P1 * * * *
787**
788** Swap the program counter with the value in register P1.
789*/
790case OP_Yield: {
791 int pcDest;
792 assert( pOp->p1>0 );
793 assert( pOp->p1<=p->nMem );
794 pIn1 = &p->aMem[pOp->p1];
795 assert( (pIn1->flags & MEM_Dyn)==0 );
796 pIn1->flags = MEM_Int;
797 pcDest = pIn1->u.i;
798 pIn1->u.i = pc;
799 REGISTER_TRACE(pOp->p1, pIn1);
800 pc = pcDest;
801 break;
802}
803
804
drh9cbf3422008-01-17 16:22:13 +0000805/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000806**
drh3d4501e2008-12-04 20:40:10 +0000807** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000808** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000809**
drh92f02c32004-09-02 14:57:08 +0000810** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
811** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
812** For errors, it can be some other value. If P1!=0 then P2 will determine
813** whether or not to rollback the current transaction. Do not rollback
814** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
815** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000816** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000817**
drh66a51672008-01-03 00:01:23 +0000818** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000819**
drh9cfcf5d2002-01-29 18:41:24 +0000820** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000821** every program. So a jump past the last instruction of the program
822** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000823*/
drh9cbf3422008-01-17 16:22:13 +0000824case OP_Halt: {
drh92f02c32004-09-02 14:57:08 +0000825 p->rc = pOp->p1;
826 p->pc = pc;
827 p->errorAction = pOp->p2;
danielk19772dca4ac2008-01-03 11:50:29 +0000828 if( pOp->p4.z ){
drhf089aa42008-07-08 19:34:06 +0000829 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drh9cfcf5d2002-01-29 18:41:24 +0000830 }
drh92f02c32004-09-02 14:57:08 +0000831 rc = sqlite3VdbeHalt(p);
danielk197701427a62005-01-11 13:02:33 +0000832 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
drh92f02c32004-09-02 14:57:08 +0000833 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000834 p->rc = rc = SQLITE_BUSY;
835 }else{
836 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000837 }
drh900b31e2007-08-28 02:27:51 +0000838 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000839}
drhc61053b2000-06-04 12:58:36 +0000840
drh4c583122008-01-04 22:01:03 +0000841/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000842**
drh9cbf3422008-01-17 16:22:13 +0000843** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000844*/
drh4c583122008-01-04 22:01:03 +0000845case OP_Integer: { /* out2-prerelease */
846 pOut->flags = MEM_Int;
847 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000848 break;
849}
850
drh4c583122008-01-04 22:01:03 +0000851/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000852**
drh66a51672008-01-03 00:01:23 +0000853** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000854** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000855*/
drh4c583122008-01-04 22:01:03 +0000856case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000857 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000858 pOut->flags = MEM_Int;
859 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000860 break;
861}
drh4f26d6c2004-05-26 23:25:30 +0000862
drh4c583122008-01-04 22:01:03 +0000863/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000864**
drh4c583122008-01-04 22:01:03 +0000865** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000866** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000867*/
drh4c583122008-01-04 22:01:03 +0000868case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
869 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000870 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000871 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000872 break;
873}
danielk1977cbb18d22004-05-28 11:37:27 +0000874
drh3c84ddf2008-01-09 02:15:38 +0000875/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000876**
drh66a51672008-01-03 00:01:23 +0000877** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000878** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000879*/
drh4c583122008-01-04 22:01:03 +0000880case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000881 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000882 pOp->opcode = OP_String;
danielk19772dca4ac2008-01-03 11:50:29 +0000883 pOp->p1 = strlen(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000884
885#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000886 if( encoding!=SQLITE_UTF8 ){
drh4c583122008-01-04 22:01:03 +0000887 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
888 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drhdab898f2008-07-30 13:14:55 +0000889 if( SQLITE_OK!=sqlite3VdbeMemMakeWriteable(pOut) ) goto no_mem;
danielk19775f096132008-03-28 15:44:09 +0000890 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000891 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000892 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000893 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000894 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000895 }
drh66a51672008-01-03 00:01:23 +0000896 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000897 pOp->p4.z = pOut->z;
898 pOp->p1 = pOut->n;
drhbb4957f2008-03-20 14:03:29 +0000899 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000900 goto too_big;
901 }
drhb7654112008-01-12 12:48:07 +0000902 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977bfd6cce2004-06-18 04:24:54 +0000903 break;
danielk19770f69c1e2004-05-29 11:24:50 +0000904 }
danielk197793758c82005-01-21 08:13:14 +0000905#endif
drhbb4957f2008-03-20 14:03:29 +0000906 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000907 goto too_big;
908 }
909 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000910}
drhf4479502004-05-27 03:12:53 +0000911
drh4c583122008-01-04 22:01:03 +0000912/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000913**
drh9cbf3422008-01-17 16:22:13 +0000914** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000915*/
drh4c583122008-01-04 22:01:03 +0000916case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000917 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000918 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
919 pOut->z = pOp->p4.z;
920 pOut->n = pOp->p1;
921 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000922 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000923 break;
924}
925
drh4c583122008-01-04 22:01:03 +0000926/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000927**
drh9cbf3422008-01-17 16:22:13 +0000928** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000929*/
drh4c583122008-01-04 22:01:03 +0000930case OP_Null: { /* out2-prerelease */
drhf0863fe2005-06-12 21:35:51 +0000931 break;
932}
933
934
drh9de221d2008-01-05 06:51:30 +0000935/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000936**
drh9de221d2008-01-05 06:51:30 +0000937** P4 points to a blob of data P1 bytes long. Store this
938** blob in register P2. This instruction is not coded directly
danielk1977cbb18d22004-05-28 11:37:27 +0000939** by the compiler. Instead, the compiler layer specifies
940** an OP_HexBlob opcode, with the hex string representation of
drh66a51672008-01-03 00:01:23 +0000941** the blob as P4. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000942** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000943*/
drh4c583122008-01-04 22:01:03 +0000944case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000945 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000946 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000947 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000948 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000949 break;
950}
951
drh3c84ddf2008-01-09 02:15:38 +0000952/* Opcode: Variable P1 P2 * * *
drh50457892003-09-06 01:10:47 +0000953**
drh9cbf3422008-01-17 16:22:13 +0000954** The value of variable P1 is written into register P2. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000955** an unknown in the original SQL string as handed to sqlite3_compile().
shane21e7feb2008-05-30 15:59:49 +0000956** Any occurrence of the '?' character in the original SQL is considered
drh7c972de2003-09-06 22:18:07 +0000957** a variable. Variables in the SQL string are number from left to
958** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000959** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000960*/
drh4c583122008-01-04 22:01:03 +0000961case OP_Variable: { /* out2-prerelease */
drh7c972de2003-09-06 22:18:07 +0000962 int j = pOp->p1 - 1;
drh023ae032007-05-08 12:12:16 +0000963 Mem *pVar;
danielk1977295ba552004-05-19 10:34:51 +0000964 assert( j>=0 && j<p->nVar );
965
drh023ae032007-05-08 12:12:16 +0000966 pVar = &p->aVar[j];
967 if( sqlite3VdbeMemTooBig(pVar) ){
968 goto too_big;
969 }
drh4c583122008-01-04 22:01:03 +0000970 sqlite3VdbeMemShallowCopy(pOut, &p->aVar[j], MEM_Static);
drhb7654112008-01-12 12:48:07 +0000971 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +0000972 break;
973}
danielk1977295ba552004-05-19 10:34:51 +0000974
drhb21e7c72008-06-22 12:37:57 +0000975/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +0000976**
drhb21e7c72008-06-22 12:37:57 +0000977** Move the values in register P1..P1+P3-1 over into
978** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
979** left holding a NULL. It is an error for register ranges
980** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +0000981*/
drhe1349cb2008-04-01 00:36:10 +0000982case OP_Move: {
983 char *zMalloc;
drhb21e7c72008-06-22 12:37:57 +0000984 int n = pOp->p3;
985 int p1 = pOp->p1;
986 int p2 = pOp->p2;
987 assert( n>0 );
988 assert( p1>0 );
989 assert( p1+n<p->nMem );
990 pIn1 = &p->aMem[p1];
991 assert( p2>0 );
992 assert( p2+n<p->nMem );
993 pOut = &p->aMem[p2];
994 assert( p1+n<=p2 || p2+n<=p1 );
995 while( n-- ){
drhb21e7c72008-06-22 12:37:57 +0000996 zMalloc = pOut->zMalloc;
997 pOut->zMalloc = 0;
998 sqlite3VdbeMemMove(pOut, pIn1);
999 pIn1->zMalloc = zMalloc;
1000 REGISTER_TRACE(p2++, pOut);
1001 pIn1++;
1002 pOut++;
1003 }
drhe1349cb2008-04-01 00:36:10 +00001004 break;
1005}
1006
drhb1fdb2a2008-01-05 04:06:03 +00001007/* Opcode: Copy P1 P2 * * *
1008**
drh9cbf3422008-01-17 16:22:13 +00001009** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001010**
1011** This instruction makes a deep copy of the value. A duplicate
1012** is made of any string or blob constant. See also OP_SCopy.
1013*/
drhe1349cb2008-04-01 00:36:10 +00001014case OP_Copy: {
1015 assert( pOp->p1>0 );
1016 assert( pOp->p1<=p->nMem );
1017 pIn1 = &p->aMem[pOp->p1];
drhe1349cb2008-04-01 00:36:10 +00001018 assert( pOp->p2>0 );
1019 assert( pOp->p2<=p->nMem );
1020 pOut = &p->aMem[pOp->p2];
1021 assert( pOut!=pIn1 );
1022 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1023 Deephemeralize(pOut);
1024 REGISTER_TRACE(pOp->p2, pOut);
1025 break;
1026}
1027
drhb1fdb2a2008-01-05 04:06:03 +00001028/* Opcode: SCopy P1 P2 * * *
1029**
drh9cbf3422008-01-17 16:22:13 +00001030** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001031**
1032** This instruction makes a shallow copy of the value. If the value
1033** is a string or blob, then the copy is only a pointer to the
1034** original and hence if the original changes so will the copy.
1035** Worse, if the original is deallocated, the copy becomes invalid.
1036** Thus the program must guarantee that the original will not change
1037** during the lifetime of the copy. Use OP_Copy to make a complete
1038** copy.
1039*/
drhb1fdb2a2008-01-05 04:06:03 +00001040case OP_SCopy: {
drh9cbf3422008-01-17 16:22:13 +00001041 assert( pOp->p1>0 );
1042 assert( pOp->p1<=p->nMem );
1043 pIn1 = &p->aMem[pOp->p1];
1044 REGISTER_TRACE(pOp->p1, pIn1);
1045 assert( pOp->p2>0 );
1046 assert( pOp->p2<=p->nMem );
1047 pOut = &p->aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001048 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001049 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh5b6afba2008-01-05 16:29:28 +00001050 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001051 break;
1052}
drh75897232000-05-29 14:26:00 +00001053
drh9cbf3422008-01-17 16:22:13 +00001054/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001055**
shane21e7feb2008-05-30 15:59:49 +00001056** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001057** results. This opcode causes the sqlite3_step() call to terminate
1058** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1059** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001060** row.
drhd4e70eb2008-01-02 00:34:36 +00001061*/
drh9cbf3422008-01-17 16:22:13 +00001062case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001063 Mem *pMem;
1064 int i;
1065 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001066 assert( pOp->p1>0 );
1067 assert( pOp->p1+pOp->p2<=p->nMem );
drhd4e70eb2008-01-02 00:34:36 +00001068
drhd4e70eb2008-01-02 00:34:36 +00001069 /* Invalidate all ephemeral cursor row caches */
1070 p->cacheCtr = (p->cacheCtr + 2)|1;
1071
1072 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001073 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001074 ** as side effect.
1075 */
1076 pMem = p->pResultSet = &p->aMem[pOp->p1];
1077 for(i=0; i<pOp->p2; i++){
1078 sqlite3VdbeMemNulTerminate(&pMem[i]);
1079 storeTypeInfo(&pMem[i], encoding);
drh0acb7e42008-06-25 00:12:41 +00001080 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001081 }
drh28039692008-03-17 16:54:01 +00001082 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001083
1084 /* Return SQLITE_ROW
1085 */
1086 p->nCallback++;
drhd4e70eb2008-01-02 00:34:36 +00001087 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001088 rc = SQLITE_ROW;
1089 goto vdbe_return;
1090}
1091
drh5b6afba2008-01-05 16:29:28 +00001092/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001093**
drh5b6afba2008-01-05 16:29:28 +00001094** Add the text in register P1 onto the end of the text in
1095** register P2 and store the result in register P3.
1096** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001097**
1098** P3 = P2 || P1
1099**
1100** It is illegal for P1 and P3 to be the same register. Sometimes,
1101** if P3 is the same register as P2, the implementation is able
1102** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001103*/
drh5b6afba2008-01-05 16:29:28 +00001104case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001105 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001106
danielk1977a7a8e142008-02-13 18:25:27 +00001107 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001108 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001109 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001110 break;
drh5e00f6c2001-09-13 13:46:56 +00001111 }
drh5b6afba2008-01-05 16:29:28 +00001112 ExpandBlob(pIn1);
1113 Stringify(pIn1, encoding);
1114 ExpandBlob(pIn2);
1115 Stringify(pIn2, encoding);
1116 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001117 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001118 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001119 }
danielk1977a7a8e142008-02-13 18:25:27 +00001120 MemSetTypeFlag(pOut, MEM_Str);
1121 if( sqlite3VdbeMemGrow(pOut, nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001122 goto no_mem;
1123 }
danielk1977a7a8e142008-02-13 18:25:27 +00001124 if( pOut!=pIn2 ){
1125 memcpy(pOut->z, pIn2->z, pIn2->n);
1126 }
1127 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1128 pOut->z[nByte] = 0;
1129 pOut->z[nByte+1] = 0;
1130 pOut->flags |= MEM_Term;
drh5b6afba2008-01-05 16:29:28 +00001131 pOut->n = nByte;
drh5b6afba2008-01-05 16:29:28 +00001132 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001133 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001134 break;
1135}
drh75897232000-05-29 14:26:00 +00001136
drh3c84ddf2008-01-09 02:15:38 +00001137/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001138**
drh60a713c2008-01-21 16:22:45 +00001139** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001140** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001141** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001142*/
drh3c84ddf2008-01-09 02:15:38 +00001143/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001144**
drh3c84ddf2008-01-09 02:15:38 +00001145**
shane21e7feb2008-05-30 15:59:49 +00001146** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001147** and store the result in register P3.
1148** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001149*/
drh3c84ddf2008-01-09 02:15:38 +00001150/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001151**
drh60a713c2008-01-21 16:22:45 +00001152** Subtract the value in register P1 from the value in register P2
1153** and store the result in register P3.
1154** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001155*/
drh9cbf3422008-01-17 16:22:13 +00001156/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001157**
drh60a713c2008-01-21 16:22:45 +00001158** Divide the value in register P1 by the value in register P2
1159** and store the result in register P3. If the value in register P2
1160** is zero, then the result is NULL.
1161** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001162*/
drh9cbf3422008-01-17 16:22:13 +00001163/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001164**
drh3c84ddf2008-01-09 02:15:38 +00001165** Compute the remainder after integer division of the value in
1166** register P1 by the value in register P2 and store the result in P3.
1167** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001168** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001169*/
drh5b6afba2008-01-05 16:29:28 +00001170case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1171case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1172case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1173case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1174case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh8a512562005-11-14 22:29:05 +00001175 int flags;
drh61669b32008-07-30 13:27:10 +00001176 applyNumericAffinity(pIn1);
1177 applyNumericAffinity(pIn2);
drh5b6afba2008-01-05 16:29:28 +00001178 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001179 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1180 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001181 i64 a, b;
drh5b6afba2008-01-05 16:29:28 +00001182 a = pIn1->u.i;
1183 b = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001184 switch( pOp->opcode ){
1185 case OP_Add: b += a; break;
1186 case OP_Subtract: b -= a; break;
1187 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001188 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001189 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001190 /* Dividing the largest possible negative 64-bit integer (1<<63) by
drh0f050352008-05-09 18:03:13 +00001191 ** -1 returns an integer too large to store in a 64-bit data-type. On
danielk197742d4ef22007-06-26 11:13:25 +00001192 ** some architectures, the value overflows to (1<<63). On others,
1193 ** a SIGFPE is issued. The following statement normalizes this
shane21e7feb2008-05-30 15:59:49 +00001194 ** behavior so that all architectures behave as if integer
1195 ** overflow occurred.
danielk197742d4ef22007-06-26 11:13:25 +00001196 */
drh0f050352008-05-09 18:03:13 +00001197 if( a==-1 && b==SMALLEST_INT64 ) a = 1;
drh5e00f6c2001-09-13 13:46:56 +00001198 b /= a;
drh75897232000-05-29 14:26:00 +00001199 break;
1200 }
drhbf4133c2001-10-13 02:59:08 +00001201 default: {
drha05a7222008-01-19 03:35:58 +00001202 if( a==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001203 if( a==-1 ) a = 1;
drhbf4133c2001-10-13 02:59:08 +00001204 b %= a;
1205 break;
1206 }
drh75897232000-05-29 14:26:00 +00001207 }
drh5b6afba2008-01-05 16:29:28 +00001208 pOut->u.i = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001209 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001210 }else{
1211 double a, b;
drh5b6afba2008-01-05 16:29:28 +00001212 a = sqlite3VdbeRealValue(pIn1);
1213 b = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001214 switch( pOp->opcode ){
1215 case OP_Add: b += a; break;
1216 case OP_Subtract: b -= a; break;
1217 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001218 case OP_Divide: {
drha05a7222008-01-19 03:35:58 +00001219 if( a==0.0 ) goto arithmetic_result_is_null;
drh5e00f6c2001-09-13 13:46:56 +00001220 b /= a;
1221 break;
1222 }
drhbf4133c2001-10-13 02:59:08 +00001223 default: {
danielk19774b5710e2007-05-08 13:57:34 +00001224 i64 ia = (i64)a;
1225 i64 ib = (i64)b;
drha05a7222008-01-19 03:35:58 +00001226 if( ia==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001227 if( ia==-1 ) ia = 1;
drhbf4133c2001-10-13 02:59:08 +00001228 b = ib % ia;
1229 break;
1230 }
drh5e00f6c2001-09-13 13:46:56 +00001231 }
drh0de3ae92008-04-28 16:55:26 +00001232 if( sqlite3IsNaN(b) ){
drha05a7222008-01-19 03:35:58 +00001233 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001234 }
drh5b6afba2008-01-05 16:29:28 +00001235 pOut->r = b;
danielk1977a7a8e142008-02-13 18:25:27 +00001236 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001237 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001238 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001239 }
drh5e00f6c2001-09-13 13:46:56 +00001240 }
1241 break;
1242
drha05a7222008-01-19 03:35:58 +00001243arithmetic_result_is_null:
1244 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001245 break;
1246}
1247
drh66a51672008-01-03 00:01:23 +00001248/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001249**
drh66a51672008-01-03 00:01:23 +00001250** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001251** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1252** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001253** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001254**
1255** The interface used by the implementation of the aforementioned functions
1256** to retrieve the collation sequence set by this opcode is not available
1257** publicly, only to user functions defined in func.c.
1258*/
drh9cbf3422008-01-17 16:22:13 +00001259case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001260 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001261 break;
1262}
1263
drh98757152008-01-09 23:04:12 +00001264/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001265**
drh66a51672008-01-03 00:01:23 +00001266** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001267** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001268** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001269** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001270**
drh13449892005-09-07 21:22:45 +00001271** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001272** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001273** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001274** whether meta data associated with a user function argument using the
1275** sqlite3_set_auxdata() API may be safely retained until the next
1276** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001277**
drh13449892005-09-07 21:22:45 +00001278** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001279*/
drh0bce8352002-02-28 00:41:10 +00001280case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001281 int i;
drh6810ce62004-01-31 19:22:56 +00001282 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001283 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001284 sqlite3_value **apVal;
drh98757152008-01-09 23:04:12 +00001285 int n = pOp->p5;
drh1350b032002-02-27 19:00:20 +00001286
danielk19776ddcca52004-05-24 23:48:25 +00001287 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001288 assert( apVal || n==0 );
1289
drh9cbf3422008-01-17 16:22:13 +00001290 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem) );
danielk1977a7a8e142008-02-13 18:25:27 +00001291 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drh9cbf3422008-01-17 16:22:13 +00001292 pArg = &p->aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001293 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001294 apVal[i] = pArg;
drh8079a0d2006-01-12 17:20:50 +00001295 storeTypeInfo(pArg, encoding);
drh2dcef112008-01-12 19:03:48 +00001296 REGISTER_TRACE(pOp->p2, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001297 }
danielk197751ad0ec2004-05-24 12:39:02 +00001298
drh66a51672008-01-03 00:01:23 +00001299 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1300 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001301 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001302 ctx.pVdbeFunc = 0;
1303 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001304 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001305 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1306 }
1307
danielk1977a7a8e142008-02-13 18:25:27 +00001308 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1309 pOut = &p->aMem[pOp->p3];
drh00706be2004-01-30 14:49:16 +00001310 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001311 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001312 ctx.s.xDel = 0;
1313 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001314
1315 /* The output cell may already have a buffer allocated. Move
1316 ** the pointer to ctx.s so in case the user-function can use
1317 ** the already allocated buffer instead of allocating a new one.
1318 */
1319 sqlite3VdbeMemMove(&ctx.s, pOut);
1320 MemSetTypeFlag(&ctx.s, MEM_Null);
1321
drh8e0a2f92002-02-23 23:45:45 +00001322 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001323 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001324 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00001325 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001326 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001327 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001328 }
danielk19774adee202004-05-08 08:23:19 +00001329 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001330 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk197775eb0162008-03-28 19:16:33 +00001331 if( sqlite3SafetyOn(db) ){
1332 sqlite3VdbeMemRelease(&ctx.s);
1333 goto abort_due_to_misuse;
1334 }
drh17435752007-08-16 04:30:38 +00001335 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001336 /* Even though a malloc() has failed, the implementation of the
1337 ** user function may have called an sqlite3_result_XXX() function
1338 ** to return a value. The following call releases any resources
1339 ** associated with such a value.
1340 **
1341 ** Note: Maybe MemRelease() should be called if sqlite3SafetyOn()
1342 ** fails also (the if(...) statement above). But if people are
1343 ** misusing sqlite, they have bigger problems than a leaked value.
1344 */
1345 sqlite3VdbeMemRelease(&ctx.s);
1346 goto no_mem;
1347 }
danielk19777e18c252004-05-25 11:47:24 +00001348
shane21e7feb2008-05-30 15:59:49 +00001349 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001350 ** immediately call the destructor for any non-static values.
1351 */
1352 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001353 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001354 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001355 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001356 }
1357
drh90669c12006-01-20 15:45:36 +00001358 /* If the function returned an error, throw an exception */
1359 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001360 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001361 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001362 }
1363
drh9cbf3422008-01-17 16:22:13 +00001364 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001365 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001366 sqlite3VdbeMemMove(pOut, &ctx.s);
1367 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001368 goto too_big;
1369 }
drh2dcef112008-01-12 19:03:48 +00001370 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001371 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001372 break;
1373}
1374
drh98757152008-01-09 23:04:12 +00001375/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001376**
drh98757152008-01-09 23:04:12 +00001377** Take the bit-wise AND of the values in register P1 and P2 and
1378** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001379** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001380*/
drh98757152008-01-09 23:04:12 +00001381/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001382**
drh98757152008-01-09 23:04:12 +00001383** Take the bit-wise OR of the values in register P1 and P2 and
1384** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001385** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001386*/
drh98757152008-01-09 23:04:12 +00001387/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001388**
drh98757152008-01-09 23:04:12 +00001389** Shift the integer value in register P2 to the left by the
drh60a713c2008-01-21 16:22:45 +00001390** number of bits specified by the integer in regiser P1.
drh98757152008-01-09 23:04:12 +00001391** 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: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001395**
drh98757152008-01-09 23:04:12 +00001396** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001397** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001398** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001399** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001400*/
drh5b6afba2008-01-05 16:29:28 +00001401case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1402case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1403case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1404case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drhb1276122005-10-29 15:48:30 +00001405 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001406
drh5b6afba2008-01-05 16:29:28 +00001407 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001408 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001409 break;
1410 }
drh5b6afba2008-01-05 16:29:28 +00001411 a = sqlite3VdbeIntValue(pIn2);
1412 b = sqlite3VdbeIntValue(pIn1);
drhbf4133c2001-10-13 02:59:08 +00001413 switch( pOp->opcode ){
1414 case OP_BitAnd: a &= b; break;
1415 case OP_BitOr: a |= b; break;
1416 case OP_ShiftLeft: a <<= b; break;
drha05a7222008-01-19 03:35:58 +00001417 default: assert( pOp->opcode==OP_ShiftRight );
1418 a >>= b; break;
drhbf4133c2001-10-13 02:59:08 +00001419 }
drh5b6afba2008-01-05 16:29:28 +00001420 pOut->u.i = a;
danielk1977a7a8e142008-02-13 18:25:27 +00001421 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001422 break;
1423}
1424
drh8558cde2008-01-05 05:20:10 +00001425/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001426**
danielk19770cdc0222008-06-26 18:04:03 +00001427** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001428** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001429**
drh8558cde2008-01-05 05:20:10 +00001430** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001431*/
drh9cbf3422008-01-17 16:22:13 +00001432case OP_AddImm: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001433 sqlite3VdbeMemIntegerify(pIn1);
1434 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001435 break;
1436}
1437
drh41c2bf02008-01-05 05:38:21 +00001438/* Opcode: ForceInt P1 P2 P3 * *
drh1dd59e02003-07-06 17:22:25 +00001439**
drh41c2bf02008-01-05 05:38:21 +00001440** Convert value in register P1 into an integer. If the value
1441** in P1 is not numeric (meaning that is is a NULL or a string that
1442** does not look like an integer or floating point number) then
1443** jump to P2. If the value in P1 is numeric then
drh751f4122004-01-14 21:59:22 +00001444** convert it into the least integer that is greater than or equal to its
drh41c2bf02008-01-05 05:38:21 +00001445** current value if P3==0, or to the least integer that is strictly
1446** greater than its current value if P3==1.
drh1dd59e02003-07-06 17:22:25 +00001447*/
drh9cbf3422008-01-17 16:22:13 +00001448case OP_ForceInt: { /* jump, in1 */
drhf4f8fd52005-03-31 18:40:04 +00001449 i64 v;
drh41c2bf02008-01-05 05:38:21 +00001450 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1451 if( (pIn1->flags & (MEM_Int|MEM_Real))==0 ){
drh1dd59e02003-07-06 17:22:25 +00001452 pc = pOp->p2 - 1;
drh751f4122004-01-14 21:59:22 +00001453 break;
drh1dd59e02003-07-06 17:22:25 +00001454 }
drh41c2bf02008-01-05 05:38:21 +00001455 if( pIn1->flags & MEM_Int ){
1456 v = pIn1->u.i + (pOp->p3!=0);
drh751f4122004-01-14 21:59:22 +00001457 }else{
drh41c2bf02008-01-05 05:38:21 +00001458 assert( pIn1->flags & MEM_Real );
1459 v = (sqlite3_int64)pIn1->r;
1460 if( pIn1->r>(double)v ) v++;
1461 if( pOp->p3 && pIn1->r==(double)v ) v++;
drh751f4122004-01-14 21:59:22 +00001462 }
drh41c2bf02008-01-05 05:38:21 +00001463 pIn1->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00001464 MemSetTypeFlag(pIn1, MEM_Int);
drh1dd59e02003-07-06 17:22:25 +00001465 break;
1466}
1467
drh9cbf3422008-01-17 16:22:13 +00001468/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001469**
drh9cbf3422008-01-17 16:22:13 +00001470** Force the value in register P1 to be an integer. If the value
1471** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001472** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001473** raise an SQLITE_MISMATCH exception.
1474*/
drh9cbf3422008-01-17 16:22:13 +00001475case OP_MustBeInt: { /* jump, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001476 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1477 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001478 if( pOp->p2==0 ){
1479 rc = SQLITE_MISMATCH;
1480 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001481 }else{
drh17c40292004-07-21 02:53:29 +00001482 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001483 }
drh8aff1012001-12-22 14:49:24 +00001484 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001485 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001486 }
1487 break;
1488}
1489
drh8558cde2008-01-05 05:20:10 +00001490/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001491**
drh2133d822008-01-03 18:44:59 +00001492** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001493**
drh8a512562005-11-14 22:29:05 +00001494** This opcode is used when extracting information from a column that
1495** has REAL affinity. Such column values may still be stored as
1496** integers, for space efficiency, but after extraction we want them
1497** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001498*/
drh9cbf3422008-01-17 16:22:13 +00001499case OP_RealAffinity: { /* in1 */
drh8558cde2008-01-05 05:20:10 +00001500 if( pIn1->flags & MEM_Int ){
1501 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001502 }
drh487e2622005-06-25 18:42:14 +00001503 break;
1504}
1505
drh8df447f2005-11-01 15:48:24 +00001506#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001507/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001508**
drh8558cde2008-01-05 05:20:10 +00001509** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001510** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001511** equivalent of printf(). Blob values are unchanged and
1512** are afterwards simply interpreted as text.
1513**
1514** A NULL value is not changed by this routine. It remains NULL.
1515*/
drh9cbf3422008-01-17 16:22:13 +00001516case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh8558cde2008-01-05 05:20:10 +00001517 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001518 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001519 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1520 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1521 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001522 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh8558cde2008-01-05 05:20:10 +00001523 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
drhb7654112008-01-12 12:48:07 +00001524 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001525 break;
1526}
1527
drh8558cde2008-01-05 05:20:10 +00001528/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001529**
drh8558cde2008-01-05 05:20:10 +00001530** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001531** If the value is numeric, convert it to a string first.
1532** Strings are simply reinterpreted as blobs with no change
1533** to the underlying data.
1534**
1535** A NULL value is not changed by this routine. It remains NULL.
1536*/
drh9cbf3422008-01-17 16:22:13 +00001537case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh8558cde2008-01-05 05:20:10 +00001538 if( pIn1->flags & MEM_Null ) break;
1539 if( (pIn1->flags & MEM_Blob)==0 ){
1540 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001541 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh487e2622005-06-25 18:42:14 +00001542 }
danielk1977a7a8e142008-02-13 18:25:27 +00001543 MemSetTypeFlag(pIn1, MEM_Blob);
drhb7654112008-01-12 12:48:07 +00001544 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001545 break;
1546}
drh8a512562005-11-14 22:29:05 +00001547
drh8558cde2008-01-05 05:20:10 +00001548/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001549**
drh8558cde2008-01-05 05:20:10 +00001550** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001551** integer or a floating-point number.)
1552** If the value is text or blob, try to convert it to an using the
1553** equivalent of atoi() or atof() and store 0 if no such conversion
1554** 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_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh8558cde2008-01-05 05:20:10 +00001559 if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1560 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001561 }
1562 break;
1563}
1564#endif /* SQLITE_OMIT_CAST */
1565
drh8558cde2008-01-05 05:20:10 +00001566/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001567**
drh8558cde2008-01-05 05:20:10 +00001568** Force the value in register P1 be an integer. If
drh8a512562005-11-14 22:29:05 +00001569** The value is currently a real number, drop its fractional part.
1570** If the value is text or blob, try to convert it to an integer using the
1571** equivalent of atoi() and store 0 if no such conversion is possible.
1572**
1573** A NULL value is not changed by this routine. It remains NULL.
1574*/
drh9cbf3422008-01-17 16:22:13 +00001575case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh8558cde2008-01-05 05:20:10 +00001576 if( (pIn1->flags & MEM_Null)==0 ){
1577 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001578 }
1579 break;
1580}
1581
1582#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001583/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001584**
drh8558cde2008-01-05 05:20:10 +00001585** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001586** If The value is currently an integer, convert it.
1587** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001588** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001589**
1590** A NULL value is not changed by this routine. It remains NULL.
1591*/
drh9cbf3422008-01-17 16:22:13 +00001592case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh8558cde2008-01-05 05:20:10 +00001593 if( (pIn1->flags & MEM_Null)==0 ){
1594 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001595 }
1596 break;
1597}
drh487e2622005-06-25 18:42:14 +00001598#endif /* SQLITE_OMIT_CAST */
1599
drh35573352008-01-08 23:54:25 +00001600/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001601**
drh35573352008-01-08 23:54:25 +00001602** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1603** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001604**
drh35573352008-01-08 23:54:25 +00001605** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1606** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1607** bit is clear then fall thru if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001608**
drh35573352008-01-08 23:54:25 +00001609** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001610** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001611** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001612** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001613** affinity is used. Note that the affinity conversions are stored
1614** back into the input registers P1 and P3. So this opcode can cause
1615** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001616**
1617** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001618** the values are compared. If both values are blobs then memcmp() is
1619** used to determine the results of the comparison. If both values
1620** are text, then the appropriate collating function specified in
1621** P4 is used to do the comparison. If P4 is not specified then
1622** memcmp() is used to compare text string. If both values are
1623** numeric, then a numeric comparison is used. If the two values
1624** are of different types, then numbers are considered less than
1625** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001626**
drh35573352008-01-08 23:54:25 +00001627** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1628** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001629*/
drh9cbf3422008-01-17 16:22:13 +00001630/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001631**
drh35573352008-01-08 23:54:25 +00001632** This works just like the Lt opcode except that the jump is taken if
1633** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001634** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001635*/
drh9cbf3422008-01-17 16:22:13 +00001636/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001637**
drh35573352008-01-08 23:54:25 +00001638** This works just like the Lt opcode except that the jump is taken if
1639** the operands in registers P1 and P3 are equal.
1640** See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001641*/
drh9cbf3422008-01-17 16:22:13 +00001642/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001643**
drh35573352008-01-08 23:54:25 +00001644** This works just like the Lt opcode except that the jump is taken if
1645** the content of register P3 is less than or equal to the content of
1646** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001647*/
drh9cbf3422008-01-17 16:22:13 +00001648/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001649**
drh35573352008-01-08 23:54:25 +00001650** This works just like the Lt opcode except that the jump is taken if
1651** the content of register P3 is greater than the content of
1652** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001653*/
drh9cbf3422008-01-17 16:22:13 +00001654/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001655**
drh35573352008-01-08 23:54:25 +00001656** This works just like the Lt opcode except that the jump is taken if
1657** the content of register P3 is greater than or equal to the content of
1658** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001659*/
drh9cbf3422008-01-17 16:22:13 +00001660case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1661case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1662case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1663case OP_Le: /* same as TK_LE, jump, in1, in3 */
1664case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1665case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
danielk1977a37cdde2004-05-16 11:15:36 +00001666 int flags;
1667 int res;
1668 char affinity;
1669
drh35573352008-01-08 23:54:25 +00001670 flags = pIn1->flags|pIn3->flags;
danielk1977a37cdde2004-05-16 11:15:36 +00001671
danielk1977a37cdde2004-05-16 11:15:36 +00001672 if( flags&MEM_Null ){
drh93a960a2008-07-10 00:32:42 +00001673 /* If either operand is NULL then the result is always NULL.
1674 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1675 */
1676 if( pOp->p5 & SQLITE_STOREP2 ){
1677 pOut = &p->aMem[pOp->p2];
1678 MemSetTypeFlag(pOut, MEM_Null);
1679 REGISTER_TRACE(pOp->p2, pOut);
1680 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1681 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001682 }
drh93a960a2008-07-10 00:32:42 +00001683 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001684 }
1685
drh35573352008-01-08 23:54:25 +00001686 affinity = pOp->p5 & SQLITE_AFF_MASK;
drhe51c44f2004-05-30 20:46:09 +00001687 if( affinity ){
drh35573352008-01-08 23:54:25 +00001688 applyAffinity(pIn1, affinity, encoding);
1689 applyAffinity(pIn3, affinity, encoding);
drhbbce3382008-12-06 16:46:13 +00001690 if( db->mallocFailed ) goto no_mem;
drhe51c44f2004-05-30 20:46:09 +00001691 }
danielk1977a37cdde2004-05-16 11:15:36 +00001692
danielk19772dca4ac2008-01-03 11:50:29 +00001693 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
drh35573352008-01-08 23:54:25 +00001694 ExpandBlob(pIn1);
1695 ExpandBlob(pIn3);
1696 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
danielk1977a37cdde2004-05-16 11:15:36 +00001697 switch( pOp->opcode ){
1698 case OP_Eq: res = res==0; break;
1699 case OP_Ne: res = res!=0; break;
1700 case OP_Lt: res = res<0; break;
1701 case OP_Le: res = res<=0; break;
1702 case OP_Gt: res = res>0; break;
1703 default: res = res>=0; break;
1704 }
1705
drh35573352008-01-08 23:54:25 +00001706 if( pOp->p5 & SQLITE_STOREP2 ){
1707 pOut = &p->aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00001708 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001709 pOut->u.i = res;
1710 REGISTER_TRACE(pOp->p2, pOut);
1711 }else if( res ){
1712 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001713 }
1714 break;
1715}
drhc9b84a12002-06-20 11:36:48 +00001716
drh0acb7e42008-06-25 00:12:41 +00001717/* Opcode: Permutation * * * P4 *
1718**
1719** Set the permuation used by the OP_Compare operator to be the array
1720** of integers in P4.
1721**
1722** The permutation is only valid until the next OP_Permutation, OP_Compare,
1723** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1724** immediately prior to the OP_Compare.
1725*/
1726case OP_Permutation: {
1727 assert( pOp->p4type==P4_INTARRAY );
1728 assert( pOp->p4.ai );
1729 aPermute = pOp->p4.ai;
1730 break;
1731}
1732
drh16ee60f2008-06-20 18:13:25 +00001733/* Opcode: Compare P1 P2 P3 P4 *
1734**
1735** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1736** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1737** the comparison for use by the next OP_Jump instruct.
1738**
drh0acb7e42008-06-25 00:12:41 +00001739** P4 is a KeyInfo structure that defines collating sequences and sort
1740** orders for the comparison. The permutation applies to registers
1741** only. The KeyInfo elements are used sequentially.
1742**
1743** The comparison is a sort comparison, so NULLs compare equal,
1744** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001745** and strings are less than blobs.
1746*/
1747case OP_Compare: {
1748 int n = pOp->p3;
1749 int i, p1, p2;
1750 const KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
1751 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001752 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001753 p1 = pOp->p1;
1754 assert( p1>0 && p1+n-1<p->nMem );
1755 p2 = pOp->p2;
1756 assert( p2>0 && p2+n-1<p->nMem );
drh0acb7e42008-06-25 00:12:41 +00001757 for(i=0; i<n; i++){
1758 int idx = aPermute ? aPermute[i] : i;
1759 CollSeq *pColl; /* Collating sequence to use on this term */
1760 int bRev; /* True for DESCENDING sort order */
drh0acb7e42008-06-25 00:12:41 +00001761 REGISTER_TRACE(p1+idx, &p->aMem[p1+idx]);
1762 REGISTER_TRACE(p2+idx, &p->aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001763 assert( i<pKeyInfo->nField );
1764 pColl = pKeyInfo->aColl[i];
1765 bRev = pKeyInfo->aSortOrder[i];
drh0acb7e42008-06-25 00:12:41 +00001766 iCompare = sqlite3MemCompare(&p->aMem[p1+idx], &p->aMem[p2+idx], pColl);
1767 if( iCompare ){
1768 if( bRev ) iCompare = -iCompare;
1769 break;
1770 }
drh16ee60f2008-06-20 18:13:25 +00001771 }
drh0acb7e42008-06-25 00:12:41 +00001772 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001773 break;
1774}
1775
1776/* Opcode: Jump P1 P2 P3 * *
1777**
1778** Jump to the instruction at address P1, P2, or P3 depending on whether
1779** in the most recent OP_Compare instruction the P1 vector was less than
1780** equal to, or greater than the P2 vector, respectively.
1781*/
drh0acb7e42008-06-25 00:12:41 +00001782case OP_Jump: { /* jump */
1783 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001784 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001785 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001786 pc = pOp->p2 - 1;
1787 }else{
1788 pc = pOp->p3 - 1;
1789 }
1790 break;
1791}
1792
drh5b6afba2008-01-05 16:29:28 +00001793/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001794**
drh5b6afba2008-01-05 16:29:28 +00001795** Take the logical AND of the values in registers P1 and P2 and
1796** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001797**
drh5b6afba2008-01-05 16:29:28 +00001798** If either P1 or P2 is 0 (false) then the result is 0 even if
1799** the other input is NULL. A NULL and true or two NULLs give
1800** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001801*/
drh5b6afba2008-01-05 16:29:28 +00001802/* Opcode: Or P1 P2 P3 * *
1803**
1804** Take the logical OR of the values in register P1 and P2 and
1805** store the answer in register P3.
1806**
1807** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1808** even if the other input is NULL. A NULL and false or two NULLs
1809** give a NULL output.
1810*/
1811case OP_And: /* same as TK_AND, in1, in2, out3 */
1812case OP_Or: { /* same as TK_OR, in1, in2, out3 */
1813 int v1, v2; /* 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001814
drh5b6afba2008-01-05 16:29:28 +00001815 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001816 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001817 }else{
drh5b6afba2008-01-05 16:29:28 +00001818 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001819 }
drh5b6afba2008-01-05 16:29:28 +00001820 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001821 v2 = 2;
1822 }else{
drh5b6afba2008-01-05 16:29:28 +00001823 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001824 }
1825 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001826 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001827 v1 = and_logic[v1*3+v2];
1828 }else{
drh5b6afba2008-01-05 16:29:28 +00001829 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001830 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001831 }
drhbb113512002-05-27 01:04:51 +00001832 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001833 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001834 }else{
drh5b6afba2008-01-05 16:29:28 +00001835 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001836 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001837 }
drh5e00f6c2001-09-13 13:46:56 +00001838 break;
1839}
1840
drh98757152008-01-09 23:04:12 +00001841/* Opcode: Not P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00001842**
drh98757152008-01-09 23:04:12 +00001843** Interpret the value in register P1 as a boolean value. Replace it
1844** with its complement. If the value in register P1 is NULL its value
drhf5905aa2002-05-26 20:54:33 +00001845** is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001846*/
drh9cbf3422008-01-17 16:22:13 +00001847case OP_Not: { /* same as TK_NOT, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001848 if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */
1849 sqlite3VdbeMemIntegerify(pIn1);
drh98757152008-01-09 23:04:12 +00001850 pIn1->u.i = !pIn1->u.i;
danielk1977a7a8e142008-02-13 18:25:27 +00001851 assert( pIn1->flags&MEM_Int );
drh5e00f6c2001-09-13 13:46:56 +00001852 break;
1853}
1854
drh98757152008-01-09 23:04:12 +00001855/* Opcode: BitNot P1 * * * *
drhbf4133c2001-10-13 02:59:08 +00001856**
drh98757152008-01-09 23:04:12 +00001857** Interpret the content of register P1 as an integer. Replace it
1858** with its ones-complement. If the value is originally NULL, leave
1859** it unchanged.
drhbf4133c2001-10-13 02:59:08 +00001860*/
drh9cbf3422008-01-17 16:22:13 +00001861case OP_BitNot: { /* same as TK_BITNOT, in1 */
drh3c84ddf2008-01-09 02:15:38 +00001862 if( pIn1->flags & MEM_Null ) break; /* Do nothing to NULLs */
1863 sqlite3VdbeMemIntegerify(pIn1);
drh98757152008-01-09 23:04:12 +00001864 pIn1->u.i = ~pIn1->u.i;
danielk1977a7a8e142008-02-13 18:25:27 +00001865 assert( pIn1->flags&MEM_Int );
drhbf4133c2001-10-13 02:59:08 +00001866 break;
1867}
1868
drh3c84ddf2008-01-09 02:15:38 +00001869/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001870**
drh3c84ddf2008-01-09 02:15:38 +00001871** Jump to P2 if the value in register P1 is true. The value is
1872** is considered true if it is numeric and non-zero. If the value
1873** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00001874*/
drh3c84ddf2008-01-09 02:15:38 +00001875/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00001876**
drh3c84ddf2008-01-09 02:15:38 +00001877** Jump to P2 if the value in register P1 is False. The value is
1878** is considered true if it has a numeric value of zero. If the value
1879** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00001880*/
drh9cbf3422008-01-17 16:22:13 +00001881case OP_If: /* jump, in1 */
1882case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00001883 int c;
drh3c84ddf2008-01-09 02:15:38 +00001884 if( pIn1->flags & MEM_Null ){
1885 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00001886 }else{
drhba0232a2005-06-06 17:27:19 +00001887#ifdef SQLITE_OMIT_FLOATING_POINT
drh3c84ddf2008-01-09 02:15:38 +00001888 c = sqlite3VdbeIntValue(pIn1);
drhba0232a2005-06-06 17:27:19 +00001889#else
drh3c84ddf2008-01-09 02:15:38 +00001890 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00001891#endif
drhf5905aa2002-05-26 20:54:33 +00001892 if( pOp->opcode==OP_IfNot ) c = !c;
1893 }
drh3c84ddf2008-01-09 02:15:38 +00001894 if( c ){
1895 pc = pOp->p2-1;
1896 }
drh5e00f6c2001-09-13 13:46:56 +00001897 break;
1898}
1899
drh2d401ab2008-01-10 23:50:11 +00001900/* Opcode: IsNull P1 P2 P3 * *
drh477df4b2008-01-05 18:48:24 +00001901**
drh2d401ab2008-01-10 23:50:11 +00001902** Jump to P2 if the value in register P1 is NULL. If P3 is greater
1903** than zero, then check all values reg(P1), reg(P1+1),
1904** reg(P1+2), ..., reg(P1+P3-1).
drh477df4b2008-01-05 18:48:24 +00001905*/
drh9cbf3422008-01-17 16:22:13 +00001906case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh2d401ab2008-01-10 23:50:11 +00001907 int n = pOp->p3;
1908 assert( pOp->p3==0 || pOp->p1>0 );
1909 do{
1910 if( (pIn1->flags & MEM_Null)!=0 ){
1911 pc = pOp->p2 - 1;
1912 break;
1913 }
1914 pIn1++;
1915 }while( --n > 0 );
drh477df4b2008-01-05 18:48:24 +00001916 break;
1917}
1918
drh98757152008-01-09 23:04:12 +00001919/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001920**
drh6a288a32008-01-07 19:20:24 +00001921** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00001922*/
drh9cbf3422008-01-17 16:22:13 +00001923case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh6a288a32008-01-07 19:20:24 +00001924 if( (pIn1->flags & MEM_Null)==0 ){
1925 pc = pOp->p2 - 1;
1926 }
drh5e00f6c2001-09-13 13:46:56 +00001927 break;
1928}
1929
danielk1977cd3e8f72008-03-25 09:47:35 +00001930/* Opcode: SetNumColumns * P2 * * *
danielk1977b4964b72004-05-18 01:23:38 +00001931**
danielk1977cd3e8f72008-03-25 09:47:35 +00001932** This opcode sets the number of columns for the cursor opened by the
1933** following instruction to P2.
danielk1977b4964b72004-05-18 01:23:38 +00001934**
danielk1977cd3e8f72008-03-25 09:47:35 +00001935** An OP_SetNumColumns is only useful if it occurs immediately before
1936** one of the following opcodes:
danielk1977ac171782005-02-05 06:49:54 +00001937**
danielk1977cd3e8f72008-03-25 09:47:35 +00001938** OpenRead
1939** OpenWrite
1940** OpenPseudo
1941**
1942** If the OP_Column opcode is to be executed on a cursor, then
1943** this opcode must be present immediately before the opcode that
1944** opens the cursor.
danielk1977b4964b72004-05-18 01:23:38 +00001945*/
drh9cbf3422008-01-17 16:22:13 +00001946case OP_SetNumColumns: {
danielk1977b4964b72004-05-18 01:23:38 +00001947 break;
1948}
1949
danielk197760585dd2008-01-03 08:08:40 +00001950/* Opcode: Column P1 P2 P3 P4 *
danielk1977192ac1d2004-05-10 07:17:30 +00001951**
danielk1977cfcdaef2004-05-12 07:33:33 +00001952** Interpret the data that cursor P1 points to as a structure built using
1953** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00001954** information about the format of the data.) Extract the P2-th column
1955** from this record. If there are less that (P2+1)
1956** values in the record, extract a NULL.
1957**
drh9cbf3422008-01-17 16:22:13 +00001958** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00001959**
danielk19771f4aa332008-01-03 09:51:55 +00001960** If the column contains fewer than P2 fields, then extract a NULL. Or,
1961** if the P4 argument is a P4_MEM use the value of the P4 argument as
1962** the result.
danielk1977192ac1d2004-05-10 07:17:30 +00001963*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001964case OP_Column: {
danielk197764202cf2008-11-17 15:31:47 +00001965 int payloadSize; /* Number of bytes in the record */
drhd3194f52004-05-27 19:59:32 +00001966 int p1 = pOp->p1; /* P1 value of the opcode */
danielk1977cfcdaef2004-05-12 07:33:33 +00001967 int p2 = pOp->p2; /* column number to retrieve */
drhdfe88ec2008-11-03 20:55:06 +00001968 VdbeCursor *pC = 0;/* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00001969 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00001970 BtCursor *pCrsr; /* The BTree cursor */
1971 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1972 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00001973 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00001974 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00001975 int i; /* Loop counter */
1976 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00001977 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00001978 Mem sMem; /* For storing the record being decoded */
danielk1977192ac1d2004-05-10 07:17:30 +00001979
drhb6f54522004-05-20 02:42:16 +00001980 sMem.flags = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001981 sMem.db = 0;
danielk19775f096132008-03-28 15:44:09 +00001982 sMem.zMalloc = 0;
drhd3194f52004-05-27 19:59:32 +00001983 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00001984 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1985 pDest = &p->aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001986 MemSetTypeFlag(pDest, MEM_Null);
danielk1977cfcdaef2004-05-12 07:33:33 +00001987
drhe61cffc2004-06-12 18:12:15 +00001988 /* This block sets the variable payloadSize to be the total number of
1989 ** bytes in the record.
1990 **
1991 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00001992 ** The complete record text is always available for pseudo-tables
1993 ** If the record is stored in a cursor, the complete record text
1994 ** might be available in the pC->aRow cache. Or it might not be.
1995 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00001996 **
1997 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00001998 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00001999 */
drhb73857f2006-03-17 00:25:59 +00002000 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00002001 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002002#ifndef SQLITE_OMIT_VIRTUALTABLE
2003 assert( pC->pVtabCursor==0 );
2004#endif
drhb73857f2006-03-17 00:25:59 +00002005 if( pC->pCursor!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002006 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002007 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002008 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002009 zRec = 0;
2010 pCrsr = pC->pCursor;
2011 if( pC->nullRow ){
2012 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002013 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002014 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002015 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002016 }else if( pC->isIndex ){
danielk197796fc5fe2004-05-13 11:34:16 +00002017 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002018 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2019 payloadSize = payloadSize64;
2020 }else{
danielk197764202cf2008-11-17 15:31:47 +00002021 sqlite3BtreeDataSize(pCrsr, (u32 *)&payloadSize);
danielk1977192ac1d2004-05-10 07:17:30 +00002022 }
drhd3194f52004-05-27 19:59:32 +00002023 nField = pC->nField;
drha05a7222008-01-19 03:35:58 +00002024 }else{
2025 assert( pC->pseudoTable );
drhe61cffc2004-06-12 18:12:15 +00002026 /* The record is the sole entry of a pseudo-table */
danielk1977192ac1d2004-05-10 07:17:30 +00002027 payloadSize = pC->nData;
2028 zRec = pC->pData;
drh76873ab2006-01-07 18:48:26 +00002029 pC->cacheStatus = CACHE_STALE;
danielk1977192ac1d2004-05-10 07:17:30 +00002030 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00002031 nField = pC->nField;
danielk1977f7df9cc2004-06-16 12:02:47 +00002032 pCrsr = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002033 }
2034
drh9cbf3422008-01-17 16:22:13 +00002035 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002036 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002037 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002038 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002039 }
drhbb4957f2008-03-20 14:03:29 +00002040 if( payloadSize>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002041 goto too_big;
2042 }
danielk1977192ac1d2004-05-10 07:17:30 +00002043
drhd3194f52004-05-27 19:59:32 +00002044 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002045
drh9188b382004-05-14 21:12:22 +00002046 /* Read and parse the table header. Store the results of the parse
2047 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002048 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002049 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002050 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002051 aOffset = pC->aOffset;
2052 }else{
danielk1977dedf45b2006-01-13 17:12:01 +00002053 u8 *zIdx; /* Index into header */
2054 u8 *zEndHdr; /* Pointer to first byte after the header */
danielk197764202cf2008-11-17 15:31:47 +00002055 int offset; /* Offset into the data */
drh0ac07192006-02-10 14:02:07 +00002056 int szHdrSz; /* Size of the header size field at start of record */
danielk1977dedf45b2006-01-13 17:12:01 +00002057 int avail; /* Number of bytes of available data */
drhb73857f2006-03-17 00:25:59 +00002058
danielk1977cd3e8f72008-03-25 09:47:35 +00002059 assert(aType);
drhb73857f2006-03-17 00:25:59 +00002060 pC->aOffset = aOffset = &aType[nField];
2061 pC->payloadSize = payloadSize;
2062 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002063
drhd3194f52004-05-27 19:59:32 +00002064 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002065 if( zRec ){
2066 zData = zRec;
2067 }else{
drhf0863fe2005-06-12 21:35:51 +00002068 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002069 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002070 }else{
drhe51c44f2004-05-30 20:46:09 +00002071 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002072 }
drhe61cffc2004-06-12 18:12:15 +00002073 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2074 ** save the payload in the pC->aRow cache. That will save us from
2075 ** having to make additional calls to fetch the content portion of
2076 ** the record.
2077 */
2078 if( avail>=payloadSize ){
drh2646da72005-12-09 20:02:05 +00002079 zRec = zData;
2080 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002081 }else{
2082 pC->aRow = 0;
2083 }
drhd3194f52004-05-27 19:59:32 +00002084 }
drh588f5bc2007-01-02 18:41:54 +00002085 /* The following assert is true in all cases accept when
2086 ** the database file has been corrupted externally.
2087 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
shane3f8d5cf2008-04-24 19:15:09 +00002088 szHdrSz = getVarint32((u8*)zData, offset);
drhe61cffc2004-06-12 18:12:15 +00002089
2090 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2091 ** record header in most cases. But they will fail to get the complete
2092 ** record header if the record header does not fit on a single page
2093 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2094 ** acquire the complete header text.
2095 */
danielk1977dedf45b2006-01-13 17:12:01 +00002096 if( !zRec && avail<offset ){
danielk1977a7a8e142008-02-13 18:25:27 +00002097 sMem.flags = 0;
2098 sMem.db = 0;
drhb21c8cd2007-08-21 19:33:56 +00002099 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002100 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002101 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002102 }
drhb6f54522004-05-20 02:42:16 +00002103 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002104 }
drh0ac07192006-02-10 14:02:07 +00002105 zEndHdr = (u8 *)&zData[offset];
2106 zIdx = (u8 *)&zData[szHdrSz];
drh9188b382004-05-14 21:12:22 +00002107
drhd3194f52004-05-27 19:59:32 +00002108 /* Scan the header and use it to fill in the aType[] and aOffset[]
2109 ** arrays. aType[i] will contain the type integer for the i-th
2110 ** column and aOffset[i] will contain the offset from the beginning
2111 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002112 */
danielk1977dedf45b2006-01-13 17:12:01 +00002113 for(i=0; i<nField; i++){
2114 if( zIdx<zEndHdr ){
2115 aOffset[i] = offset;
shane3f8d5cf2008-04-24 19:15:09 +00002116 zIdx += getVarint32(zIdx, aType[i]);
danielk1977dedf45b2006-01-13 17:12:01 +00002117 offset += sqlite3VdbeSerialTypeLen(aType[i]);
2118 }else{
2119 /* If i is less that nField, then there are less fields in this
2120 ** record than SetNumColumns indicated there are columns in the
2121 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002122 ** the record to 0. This tells code below to store a NULL
2123 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002124 */
2125 aOffset[i] = 0;
2126 }
drh9188b382004-05-14 21:12:22 +00002127 }
danielk19775f096132008-03-28 15:44:09 +00002128 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002129 sMem.flags = MEM_Null;
2130
danielk19779792eef2006-01-13 15:58:43 +00002131 /* If we have read more header data than was contained in the header,
2132 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002133 ** record, or if the end of the last field appears to be before the end
2134 ** of the record (when all fields present), then we must be dealing
2135 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002136 */
danielk1977fb8f2e22008-09-22 06:13:31 +00002137 if( zIdx>zEndHdr || offset>payloadSize
2138 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002139 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002140 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002141 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002142 }
danielk1977192ac1d2004-05-10 07:17:30 +00002143
danielk197736963fd2005-02-19 08:18:05 +00002144 /* Get the column information. If aOffset[p2] is non-zero, then
2145 ** deserialize the value from the record. If aOffset[p2] is zero,
2146 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002147 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002148 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002149 */
danielk197736963fd2005-02-19 08:18:05 +00002150 if( aOffset[p2] ){
2151 assert( rc==SQLITE_OK );
2152 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002153 sqlite3VdbeMemReleaseExternal(pDest);
2154 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002155 }else{
2156 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002157 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002158 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002159 if( rc!=SQLITE_OK ){
2160 goto op_column_out;
2161 }
2162 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002163 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002164 }
drhd4e70eb2008-01-02 00:34:36 +00002165 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002166 }else{
danielk197760585dd2008-01-03 08:08:40 +00002167 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002168 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002169 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002170 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002171 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002172 }
drhfebe1062004-08-28 18:17:48 +00002173
2174 /* If we dynamically allocated space to hold the data (in the
2175 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002176 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002177 ** This prevents a memory copy.
2178 */
danielk19775f096132008-03-28 15:44:09 +00002179 if( sMem.zMalloc ){
2180 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002181 assert( !(pDest->flags & MEM_Dyn) );
2182 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2183 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002184 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002185 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002186 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002187 }
drhfebe1062004-08-28 18:17:48 +00002188
drhd4e70eb2008-01-02 00:34:36 +00002189 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002190
danielk19773c9cc8d2005-01-17 03:40:08 +00002191op_column_out:
drhb7654112008-01-12 12:48:07 +00002192 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002193 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002194 break;
2195}
2196
danielk1977751de562008-04-18 09:01:15 +00002197/* Opcode: Affinity P1 P2 * P4 *
2198**
2199** Apply affinities to a range of P2 registers starting with P1.
2200**
2201** P4 is a string that is P2 characters long. The nth character of the
2202** string indicates the column affinity that should be used for the nth
2203** memory cell in the range.
2204*/
2205case OP_Affinity: {
2206 char *zAffinity = pOp->p4.z;
2207 Mem *pData0 = &p->aMem[pOp->p1];
2208 Mem *pLast = &pData0[pOp->p2-1];
2209 Mem *pRec;
2210
2211 for(pRec=pData0; pRec<=pLast; pRec++){
danielk1977b790c6c2008-04-18 10:25:24 +00002212 ExpandBlob(pRec);
danielk1977751de562008-04-18 09:01:15 +00002213 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2214 }
2215 break;
2216}
2217
drh1db639c2008-01-17 02:36:28 +00002218/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002219**
drh1db639c2008-01-17 02:36:28 +00002220** Convert P2 registers beginning with P1 into a single entry
drh7a224de2004-06-02 01:22:02 +00002221** suitable for use as a data record in a database table or as a key
shane21e7feb2008-05-30 15:59:49 +00002222** in an index. The details of the format are irrelevant as long as
drh1e968a02008-03-25 00:22:21 +00002223** the OP_Column opcode can decode the record later.
2224** Refer to source code comments for the details of the record
drh7a224de2004-06-02 01:22:02 +00002225** format.
2226**
danielk1977751de562008-04-18 09:01:15 +00002227** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002228** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002229** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002230**
drh8a512562005-11-14 22:29:05 +00002231** The mapping from character to affinity is given by the SQLITE_AFF_
2232** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002233**
drh66a51672008-01-03 00:01:23 +00002234** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002235*/
drh1db639c2008-01-17 02:36:28 +00002236case OP_MakeRecord: {
drhf3218fe2004-05-28 08:21:02 +00002237 /* Assuming the record contains N fields, the record format looks
2238 ** like this:
2239 **
drh7a224de2004-06-02 01:22:02 +00002240 ** ------------------------------------------------------------------------
2241 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2242 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002243 **
drh9cbf3422008-01-17 16:22:13 +00002244 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2245 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002246 **
2247 ** Each type field is a varint representing the serial type of the
2248 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002249 ** hdr-size field is also a varint which is the offset from the beginning
2250 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002251 */
drhfdf972a2007-05-02 13:30:27 +00002252 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2253 Mem *pRec; /* The new record */
drh023ae032007-05-08 12:12:16 +00002254 u64 nData = 0; /* Number of bytes of data space */
danielk1977ededfd52004-06-17 07:53:01 +00002255 int nHdr = 0; /* Number of bytes of header space */
danielk197764202cf2008-11-17 15:31:47 +00002256 i64 nByte = 0; /* Data space required for this record */
drhfdf972a2007-05-02 13:30:27 +00002257 int nZero = 0; /* Number of zero bytes at the end of the record */
drhcb9882a2005-03-17 03:15:40 +00002258 int nVarint; /* Number of bytes in a varint */
danielk1977ededfd52004-06-17 07:53:01 +00002259 u32 serial_type; /* Type field */
drh1db639c2008-01-17 02:36:28 +00002260 Mem *pData0; /* First field to be combined into the record */
2261 Mem *pLast; /* Last field of the record */
danielk1977ededfd52004-06-17 07:53:01 +00002262 int nField; /* Number of fields in the record */
danielk1977ededfd52004-06-17 07:53:01 +00002263 char *zAffinity; /* The affinity string for the record */
drhd946db02005-12-29 19:23:06 +00002264 int file_format; /* File format to use for encoding */
drhfdf972a2007-05-02 13:30:27 +00002265 int i; /* Space used in zNewRecord[] */
danielk1977ededfd52004-06-17 07:53:01 +00002266
drh1db639c2008-01-17 02:36:28 +00002267 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002268 zAffinity = pOp->p4.z;
drh1db639c2008-01-17 02:36:28 +00002269 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem );
2270 pData0 = &p->aMem[nField];
2271 nField = pOp->p2;
2272 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002273 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002274
drhf3218fe2004-05-28 08:21:02 +00002275 /* Loop through the elements that will make up the record to figure
2276 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002277 */
drha2a49dc2008-01-02 14:28:13 +00002278 for(pRec=pData0; pRec<=pLast; pRec++){
drhae7e1512007-05-02 16:51:59 +00002279 int len;
drhd3d39e92004-05-20 22:16:29 +00002280 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002281 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002282 }
danielk1977d908f5a2007-05-11 07:08:28 +00002283 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002284 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002285 }
drhd946db02005-12-29 19:23:06 +00002286 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002287 len = sqlite3VdbeSerialTypeLen(serial_type);
2288 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002289 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002290 if( pRec->flags & MEM_Zero ){
2291 /* Only pure zero-filled BLOBs can be input to this Opcode.
2292 ** We do not allow blobs with a prefix and a zero-filled tail. */
drhfdf972a2007-05-02 13:30:27 +00002293 nZero += pRec->u.i;
drhae7e1512007-05-02 16:51:59 +00002294 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002295 nZero = 0;
2296 }
danielk19778d059842004-05-12 11:24:02 +00002297 }
danielk19773d1bfea2004-05-14 11:00:53 +00002298
drhf3218fe2004-05-28 08:21:02 +00002299 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002300 nHdr += nVarint = sqlite3VarintLen(nHdr);
2301 if( nVarint<sqlite3VarintLen(nHdr) ){
2302 nHdr++;
2303 }
drhfdf972a2007-05-02 13:30:27 +00002304 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002305 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002306 goto too_big;
2307 }
drhf3218fe2004-05-28 08:21:02 +00002308
danielk1977a7a8e142008-02-13 18:25:27 +00002309 /* Make sure the output register has a buffer large enough to store
2310 ** the new record. The output register (pOp->p3) is not allowed to
2311 ** be one of the input registers (because the following call to
2312 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2313 */
2314 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2315 pOut = &p->aMem[pOp->p3];
2316 if( sqlite3VdbeMemGrow(pOut, nByte, 0) ){
2317 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002318 }
danielk1977a7a8e142008-02-13 18:25:27 +00002319 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002320
2321 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002322 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002323 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002324 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002325 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002326 }
drha2a49dc2008-01-02 14:28:13 +00002327 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drhfdf972a2007-05-02 13:30:27 +00002328 i += sqlite3VdbeSerialPut(&zNewRecord[i], nByte-i, pRec, file_format);
drhf3218fe2004-05-28 08:21:02 +00002329 }
drhfdf972a2007-05-02 13:30:27 +00002330 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002331
drh9cbf3422008-01-17 16:22:13 +00002332 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh477df4b2008-01-05 18:48:24 +00002333 pOut->n = nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002334 pOut->flags = MEM_Blob | MEM_Dyn;
2335 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002336 if( nZero ){
drh477df4b2008-01-05 18:48:24 +00002337 pOut->u.i = nZero;
2338 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002339 }
drh477df4b2008-01-05 18:48:24 +00002340 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002341 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002342 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002343 break;
2344}
2345
drh98757152008-01-09 23:04:12 +00002346/* Opcode: Statement P1 * * * *
drh663fc632002-02-02 18:49:19 +00002347**
drh7f0f12e2004-05-21 13:39:50 +00002348** Begin an individual statement transaction which is part of a larger
drh82ed1e52008-04-25 12:25:42 +00002349** transaction. This is needed so that the statement
drh7f0f12e2004-05-21 13:39:50 +00002350** can be rolled back after an error without having to roll back the
2351** entire transaction. The statement transaction will automatically
2352** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002353**
drh82ed1e52008-04-25 12:25:42 +00002354** If the database connection is currently in autocommit mode (that
2355** is to say, if it is in between BEGIN and COMMIT)
2356** and if there are no other active statements on the same database
2357** connection, then this operation is a no-op. No statement transaction
2358** is needed since any error can use the normal ROLLBACK process to
2359** undo changes.
2360**
2361** If a statement transaction is started, then a statement journal file
2362** will be allocated and initialized.
2363**
drh7f0f12e2004-05-21 13:39:50 +00002364** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002365** database file has an index of 0 and the file used for temporary tables
2366** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002367*/
drh9cbf3422008-01-17 16:22:13 +00002368case OP_Statement: {
drha05a7222008-01-19 03:35:58 +00002369 if( db->autoCommit==0 || db->activeVdbeCnt>1 ){
2370 int i = pOp->p1;
2371 Btree *pBt;
2372 assert( i>=0 && i<db->nDb );
2373 assert( db->aDb[i].pBt!=0 );
2374 pBt = db->aDb[i].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002375 assert( sqlite3BtreeIsInTrans(pBt) );
drhfb982642007-08-30 01:19:59 +00002376 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002377 if( !sqlite3BtreeIsInStmt(pBt) ){
2378 rc = sqlite3BtreeBeginStmt(pBt);
danielk1977182c4ba2007-06-27 15:53:34 +00002379 p->openedStatement = 1;
danielk19771d850a72004-05-31 08:26:49 +00002380 }
2381 }
2382 break;
2383}
2384
drh98757152008-01-09 23:04:12 +00002385/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002386**
2387** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002388** back any currently active btree transactions. If there are any active
2389** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
drh92f02c32004-09-02 14:57:08 +00002390**
2391** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002392*/
drh9cbf3422008-01-17 16:22:13 +00002393case OP_AutoCommit: {
drhad4a4b82008-11-05 16:37:34 +00002394 int desiredAutoCommit = pOp->p1;
2395 int rollback = pOp->p2;
2396 int turnOnAC = desiredAutoCommit && !db->autoCommit;
danielk19771d850a72004-05-31 08:26:49 +00002397
drhad4a4b82008-11-05 16:37:34 +00002398 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
2399 assert( desiredAutoCommit==1 || rollback==0 );
danielk19771d850a72004-05-31 08:26:49 +00002400
drh92f02c32004-09-02 14:57:08 +00002401 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002402
drhad4a4b82008-11-05 16:37:34 +00002403 if( turnOnAC && rollback && db->activeVdbeCnt>1 ){
2404 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002405 ** still running, and a transaction is active, return an error indicating
2406 ** that the other VMs must complete first.
2407 */
drhad4a4b82008-11-05 16:37:34 +00002408 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2409 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002410 rc = SQLITE_BUSY;
drhad4a4b82008-11-05 16:37:34 +00002411 }else if( turnOnAC && !rollback && db->writeVdbeCnt>1 ){
2412 /* If this instruction implements a COMMIT and other VMs are writing
2413 ** return an error indicating that the other VMs must complete first.
2414 */
2415 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2416 "SQL statements in progress");
2417 rc = SQLITE_BUSY;
2418 }else if( desiredAutoCommit!=db->autoCommit ){
danielk19771d850a72004-05-31 08:26:49 +00002419 if( pOp->p2 ){
drhad4a4b82008-11-05 16:37:34 +00002420 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002421 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002422 db->autoCommit = 1;
2423 }else{
drhad4a4b82008-11-05 16:37:34 +00002424 db->autoCommit = desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002425 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002426 p->pc = pc;
drhad4a4b82008-11-05 16:37:34 +00002427 db->autoCommit = 1-desiredAutoCommit;
drh900b31e2007-08-28 02:27:51 +00002428 p->rc = rc = SQLITE_BUSY;
2429 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002430 }
danielk19771d850a72004-05-31 08:26:49 +00002431 }
drh83968c42007-04-18 16:45:24 +00002432 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002433 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002434 }else{
drh900b31e2007-08-28 02:27:51 +00002435 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002436 }
drh900b31e2007-08-28 02:27:51 +00002437 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002438 }else{
drhf089aa42008-07-08 19:34:06 +00002439 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002440 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
danielk19771d850a72004-05-31 08:26:49 +00002441 (rollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002442 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002443
2444 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002445 }
2446 break;
2447}
2448
drh98757152008-01-09 23:04:12 +00002449/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002450**
2451** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002452** opcode is encountered. Depending on the ON CONFLICT setting, the
2453** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002454**
drh001bbcb2003-03-19 03:14:00 +00002455** P1 is the index of the database file on which the transaction is
2456** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002457** file used for temporary tables. Indices of 2 or more are used for
2458** attached databases.
drhcabb0812002-09-14 13:47:32 +00002459**
drh80242052004-06-09 00:48:12 +00002460** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002461** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002462** other process can start another write transaction while this transaction is
2463** underway. Starting a write transaction also creates a rollback journal. A
2464** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002465** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2466** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002467**
2468** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002469*/
drh9cbf3422008-01-17 16:22:13 +00002470case OP_Transaction: {
drh001bbcb2003-03-19 03:14:00 +00002471 int i = pOp->p1;
danielk19771d850a72004-05-31 08:26:49 +00002472 Btree *pBt;
2473
drh8bf8dc92003-05-17 17:35:10 +00002474 assert( i>=0 && i<db->nDb );
drhfb982642007-08-30 01:19:59 +00002475 assert( (p->btreeMask & (1<<i))!=0 );
danielk19771d850a72004-05-31 08:26:49 +00002476 pBt = db->aDb[i].pBt;
2477
danielk197724162fe2004-06-04 06:22:00 +00002478 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002479 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002480 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002481 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002482 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002483 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002484 }
danielk19772ef68482008-07-07 17:13:08 +00002485 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
danielk197724162fe2004-06-04 06:22:00 +00002486 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002487 }
drhb86ccfb2003-01-28 23:13:10 +00002488 }
drh5e00f6c2001-09-13 13:46:56 +00002489 break;
2490}
2491
drhb1fdb2a2008-01-05 04:06:03 +00002492/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002493**
drh9cbf3422008-01-17 16:22:13 +00002494** Read cookie number P3 from database P1 and write it into register P2.
drh4c583122008-01-04 22:01:03 +00002495** P3==0 is the schema version. P3==1 is the database format.
2496** P3==2 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002497** the main database file and P1==1 is the database file used to store
2498** temporary tables.
drh4a324312001-12-21 14:30:42 +00002499**
danielk1977418899a2007-06-24 10:14:00 +00002500** If P1 is negative, then this is a request to read the size of a
drh4c583122008-01-04 22:01:03 +00002501** databases free-list. P3 must be set to 1 in this case. The actual
danielk1977418899a2007-06-24 10:14:00 +00002502** database accessed is ((P1+1)*-1). For example, a P1 parameter of -1
danielk1977d62e76c2007-06-24 16:11:03 +00002503** corresponds to database 0 ("main"), a P1 of -2 is database 1 ("temp").
danielk1977418899a2007-06-24 10:14:00 +00002504**
drh50e5dad2001-09-15 00:57:28 +00002505** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002506** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002507** executing this instruction.
2508*/
drh4c583122008-01-04 22:01:03 +00002509case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002510 int iMeta;
danielk1977180b56a2007-06-24 08:00:42 +00002511 int iDb = pOp->p1;
drh4c583122008-01-04 22:01:03 +00002512 int iCookie = pOp->p3;
danielk1977180b56a2007-06-24 08:00:42 +00002513
drhb7654112008-01-12 12:48:07 +00002514 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002515 if( iDb<0 ){
2516 iDb = (-1*(iDb+1));
2517 iCookie *= -1;
2518 }
2519 assert( iDb>=0 && iDb<db->nDb );
2520 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002521 assert( (p->btreeMask & (1<<iDb))!=0 );
drha3b321d2004-05-11 09:31:31 +00002522 /* The indexing of meta values at the schema layer is off by one from
2523 ** the indexing in the btree layer. The btree considers meta[0] to
2524 ** be the number of free pages in the database (a read-only value)
2525 ** and meta[1] to be the schema cookie. The schema layer considers
2526 ** meta[1] to be the schema cookie. So we have to shift the index
2527 ** by one in the following statement.
2528 */
danielk1977180b56a2007-06-24 08:00:42 +00002529 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, 1 + iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002530 pOut->u.i = iMeta;
danielk1977a7a8e142008-02-13 18:25:27 +00002531 MemSetTypeFlag(pOut, MEM_Int);
drh50e5dad2001-09-15 00:57:28 +00002532 break;
2533}
2534
drh98757152008-01-09 23:04:12 +00002535/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002536**
drh98757152008-01-09 23:04:12 +00002537** Write the content of register P3 (interpreted as an integer)
2538** into cookie number P2 of database P1.
drh001bbcb2003-03-19 03:14:00 +00002539** P2==0 is the schema version. P2==1 is the database format.
2540** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2541** the main database file and P1==1 is the database file used to store
2542** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002543**
2544** A transaction must be started before executing this opcode.
2545*/
drh9cbf3422008-01-17 16:22:13 +00002546case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002547 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002548 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002549 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002550 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002551 pDb = &db->aDb[pOp->p1];
2552 assert( pDb->pBt!=0 );
drh98757152008-01-09 23:04:12 +00002553 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002554 /* See note about index shifting on OP_ReadCookie */
drh98757152008-01-09 23:04:12 +00002555 rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pIn3->u.i);
drh3f7d4e42004-07-24 14:35:58 +00002556 if( pOp->p2==0 ){
2557 /* When the schema cookie changes, record the new cookie internally */
drh98757152008-01-09 23:04:12 +00002558 pDb->pSchema->schema_cookie = pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002559 db->flags |= SQLITE_InternChanges;
drhd28bcb32005-12-21 14:43:11 +00002560 }else if( pOp->p2==1 ){
2561 /* Record changes in the file format */
drh98757152008-01-09 23:04:12 +00002562 pDb->pSchema->file_format = pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002563 }
drhfd426c62006-01-30 15:34:22 +00002564 if( pOp->p1==1 ){
2565 /* Invalidate all prepared statements whenever the TEMP database
2566 ** schema is changed. Ticket #1644 */
2567 sqlite3ExpirePreparedStatements(db);
2568 }
drh50e5dad2001-09-15 00:57:28 +00002569 break;
2570}
2571
drh4a324312001-12-21 14:30:42 +00002572/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002573**
drh001bbcb2003-03-19 03:14:00 +00002574** Check the value of global database parameter number 0 (the
2575** schema version) and make sure it is equal to P2.
2576** P1 is the database number which is 0 for the main database file
2577** and 1 for the file holding temporary tables and some higher number
2578** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002579**
2580** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002581** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002582** and that the current process needs to reread the schema.
2583**
2584** Either a transaction needs to have been started or an OP_Open needs
2585** to be executed (to establish a read lock) before this opcode is
2586** invoked.
2587*/
drh9cbf3422008-01-17 16:22:13 +00002588case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002589 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002590 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002591 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002592 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002593 pBt = db->aDb[pOp->p1].pBt;
2594 if( pBt ){
2595 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2596 }else{
2597 rc = SQLITE_OK;
2598 iMeta = 0;
2599 }
drhf328bc82004-05-10 23:29:49 +00002600 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
drh633e6d52008-07-28 19:34:53 +00002601 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002602 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002603 /* If the schema-cookie from the database file matches the cookie
2604 ** stored with the in-memory representation of the schema, do
2605 ** not reload the schema from the database file.
2606 **
shane21e7feb2008-05-30 15:59:49 +00002607 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002608 ** Often, v-tables store their data in other SQLite tables, which
2609 ** are queried from within xNext() and other v-table methods using
2610 ** prepared queries. If such a query is out-of-date, we do not want to
2611 ** discard the database schema, as the user code implementing the
2612 ** v-table would have to be ready for the sqlite3_vtab structure itself
2613 ** to be invalidated whenever sqlite3_step() is called from within
2614 ** a v-table method.
2615 */
2616 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2617 sqlite3ResetInternalSchema(db, pOp->p1);
2618 }
2619
drhf6d8ab82007-01-12 23:43:42 +00002620 sqlite3ExpirePreparedStatements(db);
drh50e5dad2001-09-15 00:57:28 +00002621 rc = SQLITE_SCHEMA;
2622 }
2623 break;
2624}
2625
drh98757152008-01-09 23:04:12 +00002626/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002627**
drhecdc7532001-09-23 02:35:53 +00002628** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002629** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002630** P3==0 means the main database, P3==1 means the database used for
2631** temporary tables, and P3>1 means used the corresponding attached
2632** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002633** values need not be contiguous but all P1 values should be small integers.
2634** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002635**
drh98757152008-01-09 23:04:12 +00002636** If P5!=0 then use the content of register P2 as the root page, not
2637** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002638**
drhb19a2bc2001-09-16 00:13:26 +00002639** There will be a read lock on the database whenever there is an
2640** open cursor. If the database was unlocked prior to this instruction
2641** then a read lock is acquired as part of this instruction. A read
2642** lock allows other processes to read the database but prohibits
2643** any other process from modifying the database. The read lock is
2644** released when all cursors are closed. If this instruction attempts
2645** to get a read lock but fails, the script terminates with an
2646** SQLITE_BUSY error code.
2647**
drh66a51672008-01-03 00:01:23 +00002648** The P4 value is a pointer to a KeyInfo structure that defines the
2649** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002650** that are not pointing to indices.
drhf57b3392001-10-08 13:22:32 +00002651**
drh001bbcb2003-03-19 03:14:00 +00002652** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002653*/
drh98757152008-01-09 23:04:12 +00002654/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002655**
2656** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002657** page is P2. Or if P5!=0 use the content of register P2 to find the
2658** root page.
drhecdc7532001-09-23 02:35:53 +00002659**
drh66a51672008-01-03 00:01:23 +00002660** The P4 value is a pointer to a KeyInfo structure that defines the
2661** content and collating sequence of indices. P4 is NULL for cursors
drhd3d39e92004-05-20 22:16:29 +00002662** that are not pointing to indices.
jplyon5a564222003-06-02 06:15:58 +00002663**
drh001bbcb2003-03-19 03:14:00 +00002664** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002665** in read/write mode. For a given table, there can be one or more read-only
2666** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002667**
drh001bbcb2003-03-19 03:14:00 +00002668** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002669*/
drh9cbf3422008-01-17 16:22:13 +00002670case OP_OpenRead:
2671case OP_OpenWrite: {
drh5e00f6c2001-09-13 13:46:56 +00002672 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002673 int p2 = pOp->p2;
danielk1977207872a2008-01-03 07:54:23 +00002674 int iDb = pOp->p3;
drhf57b3392001-10-08 13:22:32 +00002675 int wrFlag;
2676 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00002677 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00002678 Db *pDb;
drh001bbcb2003-03-19 03:14:00 +00002679
drh6810ce62004-01-31 19:22:56 +00002680 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00002681 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00002682 pDb = &db->aDb[iDb];
2683 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002684 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002685 if( pOp->opcode==OP_OpenWrite ){
2686 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002687 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2688 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002689 }
2690 }else{
2691 wrFlag = 0;
2692 }
drh98757152008-01-09 23:04:12 +00002693 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00002694 assert( p2>0 );
2695 assert( p2<=p->nMem );
2696 pIn2 = &p->aMem[p2];
2697 sqlite3VdbeMemIntegerify(pIn2);
2698 p2 = pIn2->u.i;
shanedcc50b72008-11-13 18:29:50 +00002699 if( p2<2 ) {
2700 rc = SQLITE_CORRUPT_BKPT;
2701 goto abort_due_to_error;
2702 }
drh5edc3122001-09-13 21:53:09 +00002703 }
drh6810ce62004-01-31 19:22:56 +00002704 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002705 pCur = allocateCursor(p, i, &pOp[-1], iDb, 1);
drh4774b132004-06-12 20:12:51 +00002706 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00002707 pCur->nullRow = 1;
danielk1977cd3e8f72008-03-25 09:47:35 +00002708 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pOp->p4.p, pCur->pCursor);
drh66a51672008-01-03 00:01:23 +00002709 if( pOp->p4type==P4_KEYINFO ){
danielk19772dca4ac2008-01-03 11:50:29 +00002710 pCur->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002711 pCur->pKeyInfo->enc = ENC(p->db);
danielk197724162fe2004-06-04 06:22:00 +00002712 }else{
drhf0863fe2005-06-12 21:35:51 +00002713 pCur->pKeyInfo = 0;
danielk197724162fe2004-06-04 06:22:00 +00002714 }
2715 switch( rc ){
2716 case SQLITE_BUSY: {
danielk19772a764eb2004-06-12 01:43:26 +00002717 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002718 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002719 goto vdbe_return;
drhd3d39e92004-05-20 22:16:29 +00002720 }
danielk197724162fe2004-06-04 06:22:00 +00002721 case SQLITE_OK: {
2722 int flags = sqlite3BtreeFlags(pCur->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002723 /* Sanity checking. Only the lower four bits of the flags byte should
danielk1977ad0132d2008-06-07 08:58:22 +00002724 ** be used. Bit 3 (mask 0x08) is unpredictable. The lower 3 bits
drhf0863fe2005-06-12 21:35:51 +00002725 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2726 ** 2 (zerodata for indices). If these conditions are not met it can
2727 ** only mean that we are dealing with a corrupt database file
2728 */
2729 if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
drh49285702005-09-17 15:20:26 +00002730 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002731 goto abort_due_to_error;
2732 }
2733 pCur->isTable = (flags & BTREE_INTKEY)!=0;
2734 pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
drh66a51672008-01-03 00:01:23 +00002735 /* If P4==0 it means we are expected to open a table. If P4!=0 then
drhf0863fe2005-06-12 21:35:51 +00002736 ** we expect to be opening an index. If this is not what happened,
2737 ** then the database is corrupt
2738 */
drh66a51672008-01-03 00:01:23 +00002739 if( (pCur->isTable && pOp->p4type==P4_KEYINFO)
2740 || (pCur->isIndex && pOp->p4type!=P4_KEYINFO) ){
drh49285702005-09-17 15:20:26 +00002741 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002742 goto abort_due_to_error;
2743 }
danielk197724162fe2004-06-04 06:22:00 +00002744 break;
drh5e00f6c2001-09-13 13:46:56 +00002745 }
danielk197724162fe2004-06-04 06:22:00 +00002746 case SQLITE_EMPTY: {
drh66a51672008-01-03 00:01:23 +00002747 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drhf0863fe2005-06-12 21:35:51 +00002748 pCur->isIndex = !pCur->isTable;
danielk1977cd3e8f72008-03-25 09:47:35 +00002749 pCur->pCursor = 0;
danielk197724162fe2004-06-04 06:22:00 +00002750 rc = SQLITE_OK;
2751 break;
2752 }
2753 default: {
2754 goto abort_due_to_error;
2755 }
2756 }
drh5e00f6c2001-09-13 13:46:56 +00002757 break;
2758}
2759
drh98757152008-01-09 23:04:12 +00002760/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00002761**
drhb9bb7c12006-06-11 23:41:55 +00002762** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00002763** The cursor is always opened read/write even if
2764** the main database is read-only. The transient or virtual
2765** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002766**
drh0342b1f2005-09-01 03:07:44 +00002767** P2 is the number of columns in the virtual table.
drh66a51672008-01-03 00:01:23 +00002768** The cursor points to a BTree table if P4==0 and to a BTree index
2769** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00002770** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00002771**
2772** This opcode was once called OpenTemp. But that created
2773** confusion because the term "temp table", might refer either
2774** to a TEMP table at the SQL level, or to a table opened by
2775** this opcode. Then this opcode was call OpenVirtual. But
2776** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00002777*/
drh9cbf3422008-01-17 16:22:13 +00002778case OP_OpenEphemeral: {
drh5e00f6c2001-09-13 13:46:56 +00002779 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002780 VdbeCursor *pCx;
drh33f4e022007-09-03 15:19:34 +00002781 static const int openFlags =
2782 SQLITE_OPEN_READWRITE |
2783 SQLITE_OPEN_CREATE |
2784 SQLITE_OPEN_EXCLUSIVE |
2785 SQLITE_OPEN_DELETEONCLOSE |
2786 SQLITE_OPEN_TRANSIENT_DB;
2787
drh6810ce62004-01-31 19:22:56 +00002788 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002789 pCx = allocateCursor(p, i, pOp, -1, 1);
drh4774b132004-06-12 20:12:51 +00002790 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00002791 pCx->nullRow = 1;
drh33f4e022007-09-03 15:19:34 +00002792 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
2793 &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002794 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00002795 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00002796 }
2797 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002798 /* If a transient index is required, create it by calling
2799 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2800 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002801 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002802 */
danielk19772dca4ac2008-01-03 11:50:29 +00002803 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00002804 int pgno;
drh66a51672008-01-03 00:01:23 +00002805 assert( pOp->p4type==P4_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00002806 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002807 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002808 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00002809 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00002810 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00002811 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00002812 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00002813 }
drhf0863fe2005-06-12 21:35:51 +00002814 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00002815 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002816 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002817 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00002818 }
drh5e00f6c2001-09-13 13:46:56 +00002819 }
drhf0863fe2005-06-12 21:35:51 +00002820 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00002821 break;
2822}
2823
danielk19779882d992008-03-27 17:59:01 +00002824/* Opcode: OpenPseudo P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00002825**
2826** Open a new cursor that points to a fake table that contains a single
2827** row of data. Any attempt to write a second row of data causes the
2828** first row to be deleted. All data is deleted when the cursor is
2829** closed.
2830**
2831** A pseudo-table created by this opcode is useful for holding the
drhcdd536f2006-03-17 00:04:03 +00002832** NEW or OLD tables in a trigger. Also used to hold the a single
2833** row output from the sorter so that the row can be decomposed into
2834** individual columns using the OP_Column opcode.
danielk19779882d992008-03-27 17:59:01 +00002835**
2836** When OP_Insert is executed to insert a row in to the pseudo table,
2837** the pseudo-table cursor may or may not make it's own copy of the
2838** original row data. If P2 is 0, then the pseudo-table will copy the
2839** original row data. Otherwise, a pointer to the original memory cell
2840** is stored. In this case, the vdbe program must ensure that the
2841** memory cell containing the row data is not overwritten until the
2842** pseudo table is closed (or a new row is inserted into it).
drh70ce3f02003-04-15 19:22:22 +00002843*/
drh9cbf3422008-01-17 16:22:13 +00002844case OP_OpenPseudo: {
drh70ce3f02003-04-15 19:22:22 +00002845 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002846 VdbeCursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002847 assert( i>=0 );
danielk1977cd3e8f72008-03-25 09:47:35 +00002848 pCx = allocateCursor(p, i, &pOp[-1], -1, 0);
drh4774b132004-06-12 20:12:51 +00002849 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00002850 pCx->nullRow = 1;
2851 pCx->pseudoTable = 1;
danielk19779882d992008-03-27 17:59:01 +00002852 pCx->ephemPseudoTable = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00002853 pCx->isTable = 1;
2854 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00002855 break;
2856}
2857
drh98757152008-01-09 23:04:12 +00002858/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00002859**
2860** Close a cursor previously opened as P1. If P1 is not
2861** currently open, this instruction is a no-op.
2862*/
drh9cbf3422008-01-17 16:22:13 +00002863case OP_Close: {
drh5e00f6c2001-09-13 13:46:56 +00002864 int i = pOp->p1;
drha05a7222008-01-19 03:35:58 +00002865 assert( i>=0 && i<p->nCursor );
2866 sqlite3VdbeFreeCursor(p, p->apCsr[i]);
2867 p->apCsr[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00002868 break;
2869}
2870
danielk1977b790c6c2008-04-18 10:25:24 +00002871/* Opcode: MoveGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00002872**
danielk1977b790c6c2008-04-18 10:25:24 +00002873** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
2874** use the integer value in register P3 as a key. If cursor P1 refers
2875** to an SQL index, then P3 is the first in an array of P4 registers
2876** that are used as an unpacked index key.
2877**
2878** Reposition cursor P1 so that it points to the smallest entry that
2879** is greater than or equal to the key value. If there are no records
2880** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00002881**
drh91fd4d42008-01-19 20:11:25 +00002882** A special feature of this opcode (and different from the
2883** related OP_MoveGt, OP_MoveLt, and OP_MoveLe) is that if P2 is
2884** zero and P1 is an SQL table (a b-tree with integer keys) then
2885** the seek is deferred until it is actually needed. It might be
2886** the case that the cursor is never accessed. By deferring the
2887** seek, we avoid unnecessary seeks.
2888**
drh7cf6e4d2004-05-19 14:56:55 +00002889** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2890*/
danielk1977b790c6c2008-04-18 10:25:24 +00002891/* Opcode: MoveGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00002892**
danielk1977b790c6c2008-04-18 10:25:24 +00002893** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
2894** use the integer value in register P3 as a key. If cursor P1 refers
2895** to an SQL index, then P3 is the first in an array of P4 registers
2896** that are used as an unpacked index key.
2897**
2898** Reposition cursor P1 so that it points to the smallest entry that
2899** is greater than the key value. If there are no records greater than
2900** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00002901**
drh7cf6e4d2004-05-19 14:56:55 +00002902** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
drh5e00f6c2001-09-13 13:46:56 +00002903*/
danielk1977b790c6c2008-04-18 10:25:24 +00002904/* Opcode: MoveLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00002905**
danielk1977b790c6c2008-04-18 10:25:24 +00002906** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
2907** use the integer value in register P3 as a key. If cursor P1 refers
2908** to an SQL index, then P3 is the first in an array of P4 registers
2909** that are used as an unpacked index key.
2910**
2911** Reposition cursor P1 so that it points to the largest entry that
2912** is less than the key value. If there are no records less than
2913** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00002914**
drh7cf6e4d2004-05-19 14:56:55 +00002915** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2916*/
danielk1977751de562008-04-18 09:01:15 +00002917/* Opcode: MoveLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00002918**
danielk1977b790c6c2008-04-18 10:25:24 +00002919** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
2920** use the integer value in register P3 as a key. If cursor P1 refers
2921** to an SQL index, then P3 is the first in an array of P4 registers
2922** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00002923**
danielk1977b790c6c2008-04-18 10:25:24 +00002924** Reposition cursor P1 so that it points to the largest entry that
2925** is less than or equal to the key value. If there are no records
2926** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00002927**
2928** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
drhc045ec52002-12-04 20:01:06 +00002929*/
drh9cbf3422008-01-17 16:22:13 +00002930case OP_MoveLt: /* jump, in3 */
2931case OP_MoveLe: /* jump, in3 */
2932case OP_MoveGe: /* jump, in3 */
2933case OP_MoveGt: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00002934 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00002935 VdbeCursor *pC;
drh80ff32f2001-11-04 18:32:46 +00002936
drh70ce3f02003-04-15 19:22:22 +00002937 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002938 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00002939 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00002940 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00002941 int res, oc;
drh7cf6e4d2004-05-19 14:56:55 +00002942 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00002943 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00002944 if( pC->isTable ){
drh98757152008-01-09 23:04:12 +00002945 i64 iKey = sqlite3VdbeIntValue(pIn3);
drh91fd4d42008-01-19 20:11:25 +00002946 if( pOp->p2==0 ){
2947 assert( pOp->opcode==OP_MoveGe );
drha11846b2004-01-07 18:52:56 +00002948 pC->movetoTarget = iKey;
drh91fd4d42008-01-19 20:11:25 +00002949 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00002950 pC->deferredMoveto = 1;
drha11846b2004-01-07 18:52:56 +00002951 break;
2952 }
drhe63d9992008-08-13 19:11:48 +00002953 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00002954 if( rc!=SQLITE_OK ){
2955 goto abort_due_to_error;
2956 }
drh98757152008-01-09 23:04:12 +00002957 pC->lastRowid = iKey;
drhf0863fe2005-06-12 21:35:51 +00002958 pC->rowidIsValid = res==0;
drh5e00f6c2001-09-13 13:46:56 +00002959 }else{
danielk1977b790c6c2008-04-18 10:25:24 +00002960 UnpackedRecord r;
2961 int nField = pOp->p4.i;
2962 assert( pOp->p4type==P4_INT32 );
2963 assert( nField>0 );
2964 r.pKeyInfo = pC->pKeyInfo;
2965 r.nField = nField;
drhe63d9992008-08-13 19:11:48 +00002966 if( oc==OP_MoveGt || oc==OP_MoveLe ){
2967 r.flags = UNPACKED_INCRKEY;
2968 }else{
2969 r.flags = 0;
2970 }
danielk1977b790c6c2008-04-18 10:25:24 +00002971 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00002972 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00002973 if( rc!=SQLITE_OK ){
2974 goto abort_due_to_error;
2975 }
drhf0863fe2005-06-12 21:35:51 +00002976 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002977 }
drha11846b2004-01-07 18:52:56 +00002978 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002979 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00002980#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00002981 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00002982#endif
drh7cf6e4d2004-05-19 14:56:55 +00002983 if( oc==OP_MoveGe || oc==OP_MoveGt ){
2984 if( res<0 ){
danielk197728129562005-01-11 10:25:06 +00002985 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00002986 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00002987 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00002988 }else{
2989 res = 0;
drh8721ce42001-11-07 14:22:00 +00002990 }
drh7cf6e4d2004-05-19 14:56:55 +00002991 }else{
2992 assert( oc==OP_MoveLt || oc==OP_MoveLe );
drh1a844c32002-12-04 22:29:28 +00002993 if( res>=0 ){
danielk197701427a62005-01-11 13:02:33 +00002994 rc = sqlite3BtreePrevious(pC->pCursor, &res);
2995 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00002996 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00002997 }else{
2998 /* res might be negative because the table is empty. Check to
2999 ** see if this is the case.
3000 */
drhf328bc82004-05-10 23:29:49 +00003001 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003002 }
drh1af3fdb2004-07-18 21:33:01 +00003003 }
drh91fd4d42008-01-19 20:11:25 +00003004 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003005 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003006 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003007 }
danielk1977f7b9d662008-06-23 18:49:43 +00003008 }else if( !pC->pseudoTable ){
3009 /* This happens when attempting to open the sqlite3_master table
3010 ** for read access returns SQLITE_EMPTY. In this case always
3011 ** take the jump (since there are no records in the table).
3012 */
3013 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003014 }
drh5e00f6c2001-09-13 13:46:56 +00003015 break;
3016}
3017
drh98757152008-01-09 23:04:12 +00003018/* Opcode: Found P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003019**
drh98757152008-01-09 23:04:12 +00003020** Register P3 holds a blob constructed by MakeRecord. P1 is an index.
drh9cbf3422008-01-17 16:22:13 +00003021** If an entry that matches the value in register p3 exists in P1 then
3022** jump to P2. If the P3 value does not match any entry in P1
drhf0863fe2005-06-12 21:35:51 +00003023** then fall thru. The P1 cursor is left pointing at the matching entry
drh2dcef112008-01-12 19:03:48 +00003024** if it exists.
drhf0863fe2005-06-12 21:35:51 +00003025**
3026** This instruction is used to implement the IN operator where the
danielk19779a96b662007-11-29 17:05:18 +00003027** left-hand side is a SELECT statement. P1 may be a true index, or it
3028** may be a temporary index that holds the results of the SELECT
drh2dcef112008-01-12 19:03:48 +00003029** statement. This instruction is also used to implement the
3030** DISTINCT keyword in SELECT statements.
danielk19779a96b662007-11-29 17:05:18 +00003031**
3032** This instruction checks if index P1 contains a record for which
shane21e7feb2008-05-30 15:59:49 +00003033** the first N serialized values exactly match the N serialized values
drh9cbf3422008-01-17 16:22:13 +00003034** in the record in register P3, where N is the total number of values in
3035** the P3 record (the P3 record is a prefix of the P1 record).
drhb19a2bc2001-09-16 00:13:26 +00003036**
drhcb6d50e2008-08-21 19:28:30 +00003037** See also: NotFound, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00003038*/
drh98757152008-01-09 23:04:12 +00003039/* Opcode: NotFound P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003040**
drh98757152008-01-09 23:04:12 +00003041** Register P3 holds a blob constructed by MakeRecord. P1 is
drhf0863fe2005-06-12 21:35:51 +00003042** an index. If no entry exists in P1 that matches the blob then jump
drh795ab9b2007-01-27 13:37:22 +00003043** to P2. If an entry does existing, fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003044** pointing to the entry that matches.
drh5e00f6c2001-09-13 13:46:56 +00003045**
drhcb6d50e2008-08-21 19:28:30 +00003046** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003047*/
drh9cbf3422008-01-17 16:22:13 +00003048case OP_NotFound: /* jump, in3 */
3049case OP_Found: { /* jump, in3 */
drh5e00f6c2001-09-13 13:46:56 +00003050 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00003051 int alreadyExists = 0;
drhdfe88ec2008-11-03 20:55:06 +00003052 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003053 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003054 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003055 if( (pC = p->apCsr[i])->pCursor!=0 ){
danielk197777519402007-08-30 11:48:31 +00003056 int res;
drhe63d9992008-08-13 19:11:48 +00003057 UnpackedRecord *pIdxKey;
3058
drhf0863fe2005-06-12 21:35:51 +00003059 assert( pC->isTable==0 );
drh98757152008-01-09 23:04:12 +00003060 assert( pIn3->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003061 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
drh23f79d02008-08-20 22:06:47 +00003062 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003063 if( pIdxKey==0 ){
3064 goto no_mem;
danielk19779a96b662007-11-29 17:05:18 +00003065 }
drhe63d9992008-08-13 19:11:48 +00003066 if( pOp->opcode==OP_Found ){
3067 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3068 }
3069 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3070 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk197777519402007-08-30 11:48:31 +00003071 if( rc!=SQLITE_OK ){
3072 break;
3073 }
3074 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003075 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003076 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003077 }
3078 if( pOp->opcode==OP_Found ){
3079 if( alreadyExists ) pc = pOp->p2 - 1;
3080 }else{
3081 if( !alreadyExists ) pc = pOp->p2 - 1;
3082 }
drh5e00f6c2001-09-13 13:46:56 +00003083 break;
3084}
3085
drh98757152008-01-09 23:04:12 +00003086/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003087**
drh98757152008-01-09 23:04:12 +00003088** The P3 register contains an integer record number. Call this
3089** record number R. The P4 register contains an index key created
drhe63d9992008-08-13 19:11:48 +00003090** using MakeRecord. Call it K.
drh9cfcf5d2002-01-29 18:41:24 +00003091**
drh7cf6e4d2004-05-19 14:56:55 +00003092** P1 is an index. So it has no data and its key consists of a
drhf0863fe2005-06-12 21:35:51 +00003093** record generated by OP_MakeRecord where the last field is the
3094** rowid of the entry that the index refers to.
drhf3218fe2004-05-28 08:21:02 +00003095**
drh0ca3e242002-01-29 23:07:02 +00003096** This instruction asks if there is an entry in P1 where the
drh7cf6e4d2004-05-19 14:56:55 +00003097** fields matches K but the rowid is different from R.
3098** If there is no such entry, then there is an immediate
drh0ca3e242002-01-29 23:07:02 +00003099** jump to P2. If any entry does exist where the index string
3100** matches K but the record number is not R, then the record
drh98757152008-01-09 23:04:12 +00003101** number for that entry is written into P3 and control
drh0ca3e242002-01-29 23:07:02 +00003102** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003103**
drh9cbf3422008-01-17 16:22:13 +00003104** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003105*/
drh9cbf3422008-01-17 16:22:13 +00003106case OP_IsUnique: { /* jump, in3 */
drh9cfcf5d2002-01-29 18:41:24 +00003107 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003108 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003109 BtCursor *pCrsr;
drh98757152008-01-09 23:04:12 +00003110 Mem *pK;
danielk1977452c9892004-05-13 05:16:15 +00003111 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00003112
drh0ca3e242002-01-29 23:07:02 +00003113 /* Pop the value R off the top of the stack
3114 */
drh98757152008-01-09 23:04:12 +00003115 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003116 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
3117 pK = &p->aMem[pOp->p4.i];
drh98757152008-01-09 23:04:12 +00003118 sqlite3VdbeMemIntegerify(pIn3);
3119 R = pIn3->u.i;
drh73bdf072006-08-15 14:21:16 +00003120 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003121 pCx = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003122 assert( pCx!=0 );
drhf328bc82004-05-10 23:29:49 +00003123 pCrsr = pCx->pCursor;
3124 if( pCrsr!=0 ){
danielk1977f2fa8312006-01-24 13:09:33 +00003125 int res;
drhe63d9992008-08-13 19:11:48 +00003126 i64 v; /* The record number that matches K */
3127 UnpackedRecord *pIdxKey; /* Unpacked version of P4 */
drh0ca3e242002-01-29 23:07:02 +00003128
3129 /* Make sure K is a string and make zKey point to K
3130 */
drh98757152008-01-09 23:04:12 +00003131 assert( pK->flags & MEM_Blob );
drhe63d9992008-08-13 19:11:48 +00003132 pIdxKey = sqlite3VdbeRecordUnpack(pCx->pKeyInfo, pK->n, pK->z,
drh23f79d02008-08-20 22:06:47 +00003133 aTempRec, sizeof(aTempRec));
drhe63d9992008-08-13 19:11:48 +00003134 if( pIdxKey==0 ){
3135 goto no_mem;
3136 }
3137 pIdxKey->flags |= UNPACKED_IGNORE_ROWID;
danielk1977452c9892004-05-13 05:16:15 +00003138
drhe63d9992008-08-13 19:11:48 +00003139 /* Search for an entry in P1 where all but the last rowid match K
drh0ca3e242002-01-29 23:07:02 +00003140 ** If there is no such entry, jump immediately to P2.
3141 */
drh9188b382004-05-14 21:12:22 +00003142 assert( pCx->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003143 pCx->cacheStatus = CACHE_STALE;
drhe63d9992008-08-13 19:11:48 +00003144 rc = sqlite3BtreeMovetoUnpacked(pCrsr, pIdxKey, 0, 0, &res);
danielk1977f0113002006-01-24 12:09:17 +00003145 if( rc!=SQLITE_OK ){
drhe63d9992008-08-13 19:11:48 +00003146 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
danielk1977f0113002006-01-24 12:09:17 +00003147 goto abort_due_to_error;
3148 }
drh9cfcf5d2002-01-29 18:41:24 +00003149 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003150 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00003151 if( res ){
3152 pc = pOp->p2 - 1;
drhe63d9992008-08-13 19:11:48 +00003153 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003154 break;
3155 }
3156 }
drhe63d9992008-08-13 19:11:48 +00003157 rc = sqlite3VdbeIdxKeyCompare(pCx, pIdxKey, &res);
3158 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drh9cfcf5d2002-01-29 18:41:24 +00003159 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3160 if( res>0 ){
3161 pc = pOp->p2 - 1;
3162 break;
3163 }
drh0ca3e242002-01-29 23:07:02 +00003164
3165 /* At this point, pCrsr is pointing to an entry in P1 where all but
drhf3218fe2004-05-28 08:21:02 +00003166 ** the final entry (the rowid) matches K. Check to see if the
3167 ** final rowid column is different from R. If it equals R then jump
danielk1977452c9892004-05-13 05:16:15 +00003168 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00003169 */
drhb21c8cd2007-08-21 19:33:56 +00003170 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
danielk1977452c9892004-05-13 05:16:15 +00003171 if( rc!=SQLITE_OK ){
3172 goto abort_due_to_error;
3173 }
drh0ca3e242002-01-29 23:07:02 +00003174 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003175 pc = pOp->p2 - 1;
3176 break;
3177 }
drh0ca3e242002-01-29 23:07:02 +00003178
drh9cbf3422008-01-17 16:22:13 +00003179 /* The final varint of the key is different from R. Store it back
3180 ** into register R3. (The record number of an entry that violates
3181 ** a UNIQUE constraint.)
drh0ca3e242002-01-29 23:07:02 +00003182 */
drh98757152008-01-09 23:04:12 +00003183 pIn3->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003184 assert( pIn3->flags&MEM_Int );
drh9cfcf5d2002-01-29 18:41:24 +00003185 }
3186 break;
3187}
3188
drh9cbf3422008-01-17 16:22:13 +00003189/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003190**
drh9cbf3422008-01-17 16:22:13 +00003191** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003192** with that key does not exist in table of P1, then jump to P2.
3193** If the record does exist, then fall thru. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003194** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003195**
3196** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003197** operation assumes the key is an integer and that P1 is a table whereas
3198** NotFound assumes key is a blob constructed from MakeRecord and
3199** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003200**
drhcb6d50e2008-08-21 19:28:30 +00003201** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003202*/
drh9cbf3422008-01-17 16:22:13 +00003203case OP_NotExists: { /* jump, in3 */
drh6b125452002-01-28 15:53:03 +00003204 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003205 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003206 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003207 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003208 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003209 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19771400b522005-01-11 11:08:22 +00003210 int res;
danielk197736a3c702004-05-11 06:55:14 +00003211 u64 iKey;
drh98757152008-01-09 23:04:12 +00003212 assert( pIn3->flags & MEM_Int );
drhf0863fe2005-06-12 21:35:51 +00003213 assert( p->apCsr[i]->isTable );
drh98757152008-01-09 23:04:12 +00003214 iKey = intToKey(pIn3->u.i);
drhe63d9992008-08-13 19:11:48 +00003215 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0,&res);
drh98757152008-01-09 23:04:12 +00003216 pC->lastRowid = pIn3->u.i;
drhf0863fe2005-06-12 21:35:51 +00003217 pC->rowidIsValid = res==0;
drh9188b382004-05-14 21:12:22 +00003218 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003219 pC->cacheStatus = CACHE_STALE;
drh0bc53702007-01-04 01:20:11 +00003220 /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK
3221 ** processing is about to abort so we really do not care whether or not
drhc416ba92007-03-30 18:42:55 +00003222 ** the following jump is taken. (In other words, do not stress over
3223 ** the error that valgrind sometimes shows on the next statement when
3224 ** running ioerr.test and similar failure-recovery test scripts.) */
danielk197728129562005-01-11 10:25:06 +00003225 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003226 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003227 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003228 }
danielk1977f7b9d662008-06-23 18:49:43 +00003229 }else if( !pC->pseudoTable ){
3230 /* This happens when an attempt to open a read cursor on the
3231 ** sqlite_master table returns SQLITE_EMPTY.
3232 */
3233 assert( pC->isTable );
3234 pc = pOp->p2 - 1;
3235 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003236 }
drh6b125452002-01-28 15:53:03 +00003237 break;
3238}
3239
drh4c583122008-01-04 22:01:03 +00003240/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003241**
drh4c583122008-01-04 22:01:03 +00003242** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003243** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003244** The sequence number on the cursor is incremented after this
3245** instruction.
drh4db38a72005-09-01 12:16:28 +00003246*/
drh4c583122008-01-04 22:01:03 +00003247case OP_Sequence: { /* out2-prerelease */
drh4db38a72005-09-01 12:16:28 +00003248 int i = pOp->p1;
drh4db38a72005-09-01 12:16:28 +00003249 assert( i>=0 && i<p->nCursor );
3250 assert( p->apCsr[i]!=0 );
drh4c583122008-01-04 22:01:03 +00003251 pOut->u.i = p->apCsr[i]->seqCount++;
danielk1977a7a8e142008-02-13 18:25:27 +00003252 MemSetTypeFlag(pOut, MEM_Int);
drh4db38a72005-09-01 12:16:28 +00003253 break;
3254}
3255
3256
drh98757152008-01-09 23:04:12 +00003257/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003258**
drhf0863fe2005-06-12 21:35:51 +00003259** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003260** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003261** table that cursor P1 points to. The new record number is written
3262** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003263**
drh98757152008-01-09 23:04:12 +00003264** If P3>0 then P3 is a register that holds the largest previously
drh205f48e2004-11-05 00:43:11 +00003265** generated record number. No new record numbers are allowed to be less
drh2958a4e2004-11-12 03:56:15 +00003266** than this value. When this value reaches its maximum, a SQLITE_FULL
drh98757152008-01-09 23:04:12 +00003267** error is generated. The P3 register is updated with the generated
drh4c583122008-01-04 22:01:03 +00003268** record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003269** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003270*/
drh4c583122008-01-04 22:01:03 +00003271case OP_NewRowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003272 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00003273 i64 v = 0;
drhdfe88ec2008-11-03 20:55:06 +00003274 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003275 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003276 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003277 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00003278 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003279 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003280 /* The next rowid or record number (different terms for the same
3281 ** thing) is obtained in a two-step algorithm.
3282 **
3283 ** First we attempt to find the largest existing rowid and add one
3284 ** to that. But if the largest existing rowid is already the maximum
3285 ** positive integer, we have to fall through to the second
3286 ** probabilistic algorithm
3287 **
3288 ** The second algorithm is to select a rowid at random and see if
3289 ** it already exists in the table. If it does not exist, we have
3290 ** succeeded. If the random rowid does exist, we select a new one
3291 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00003292 **
3293 ** For a table with less than 2 billion entries, the probability
3294 ** of not finding a unused rowid is about 1.0e-300. This is a
3295 ** non-zero probability, but it is still vanishingly small and should
3296 ** never cause a problem. You are much, much more likely to have a
3297 ** hardware failure than for this algorithm to fail.
3298 **
drhaf9ff332002-01-16 21:00:27 +00003299 ** The analysis in the previous paragraph assumes that you have a good
3300 ** source of random numbers. Is a library function like lrand48()
3301 ** good enough? Maybe. Maybe not. It's hard to know whether there
3302 ** might be subtle bugs is some implementations of lrand48() that
3303 ** could cause problems. To avoid uncertainty, SQLite uses its own
3304 ** random number generator based on the RC4 algorithm.
3305 **
drhdb5ed6d2001-09-18 22:17:44 +00003306 ** To promote locality of reference for repetitive inserts, the
shane21e7feb2008-05-30 15:59:49 +00003307 ** first few attempts at choosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00003308 ** larger than the previous rowid. This has been shown experimentally
3309 ** to double the speed of the COPY operation.
3310 */
danielk1977f7df9cc2004-06-16 12:02:47 +00003311 int res, rx=SQLITE_OK, cnt;
drhf328bc82004-05-10 23:29:49 +00003312 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00003313 cnt = 0;
drh4e6083c2005-02-04 21:13:00 +00003314 if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3315 BTREE_INTKEY ){
drh49285702005-09-17 15:20:26 +00003316 rc = SQLITE_CORRUPT_BKPT;
drh4e6083c2005-02-04 21:13:00 +00003317 goto abort_due_to_error;
3318 }
drhf328bc82004-05-10 23:29:49 +00003319 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3320 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drhfe2093d2005-01-20 22:48:47 +00003321
drh75f86a42005-02-17 00:03:06 +00003322#ifdef SQLITE_32BIT_ROWID
3323# define MAX_ROWID 0x7fffffff
3324#else
drhfe2093d2005-01-20 22:48:47 +00003325 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3326 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3327 ** to provide the constant while making all compilers happy.
3328 */
danielk197764202cf2008-11-17 15:31:47 +00003329# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003330#endif
drhfe2093d2005-01-20 22:48:47 +00003331
drh5cf8e8c2002-02-19 22:42:05 +00003332 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00003333 if( pC->nextRowidValid ){
3334 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00003335 }else{
danielk1977261919c2005-12-06 12:52:59 +00003336 rc = sqlite3BtreeLast(pC->pCursor, &res);
3337 if( rc!=SQLITE_OK ){
3338 goto abort_due_to_error;
3339 }
drh32fbe342002-10-19 20:16:37 +00003340 if( res ){
3341 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003342 }else{
danielk1977e0d4b062004-06-28 01:11:46 +00003343 sqlite3BtreeKeySize(pC->pCursor, &v);
drh32fbe342002-10-19 20:16:37 +00003344 v = keyToInt(v);
drh75f86a42005-02-17 00:03:06 +00003345 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003346 pC->useRandomRowid = 1;
3347 }else{
3348 v++;
3349 }
drh5cf8e8c2002-02-19 22:42:05 +00003350 }
drh3fc190c2001-09-14 03:24:23 +00003351 }
drh205f48e2004-11-05 00:43:11 +00003352
3353#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003354 if( pOp->p3 ){
drh205f48e2004-11-05 00:43:11 +00003355 Mem *pMem;
drh4c583122008-01-04 22:01:03 +00003356 assert( pOp->p3>0 && pOp->p3<=p->nMem ); /* P3 is a valid memory cell */
3357 pMem = &p->aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00003358 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003359 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003360 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003361 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003362 rc = SQLITE_FULL;
3363 goto abort_due_to_error;
3364 }
drh3c024d62007-03-30 11:23:45 +00003365 if( v<pMem->u.i+1 ){
3366 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003367 }
drh3c024d62007-03-30 11:23:45 +00003368 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003369 }
3370#endif
3371
drh75f86a42005-02-17 00:03:06 +00003372 if( v<MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003373 pC->nextRowidValid = 1;
3374 pC->nextRowid = v+1;
3375 }else{
3376 pC->nextRowidValid = 0;
3377 }
drh5cf8e8c2002-02-19 22:42:05 +00003378 }
3379 if( pC->useRandomRowid ){
drh4c583122008-01-04 22:01:03 +00003380 assert( pOp->p3==0 ); /* SQLITE_FULL must have occurred prior to this */
drh5cf8e8c2002-02-19 22:42:05 +00003381 v = db->priorNewRowid;
3382 cnt = 0;
3383 do{
drh91fd4d42008-01-19 20:11:25 +00003384 if( cnt==0 && (v&0xffffff)==v ){
3385 v++;
3386 }else{
drh2fa18682008-03-19 14:15:34 +00003387 sqlite3_randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003388 if( cnt<5 ) v &= 0xffffff;
drh5cf8e8c2002-02-19 22:42:05 +00003389 }
3390 if( v==0 ) continue;
3391 x = intToKey(v);
drhe63d9992008-08-13 19:11:48 +00003392 rx = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)x, 0, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003393 cnt++;
drh91fd4d42008-01-19 20:11:25 +00003394 }while( cnt<100 && rx==SQLITE_OK && res==0 );
drh5cf8e8c2002-02-19 22:42:05 +00003395 db->priorNewRowid = v;
3396 if( rx==SQLITE_OK && res==0 ){
3397 rc = SQLITE_FULL;
3398 goto abort_due_to_error;
3399 }
drh1eaa2692001-09-18 02:02:23 +00003400 }
drhf0863fe2005-06-12 21:35:51 +00003401 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003402 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003403 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003404 }
danielk1977a7a8e142008-02-13 18:25:27 +00003405 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00003406 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003407 break;
3408}
3409
danielk19771f4aa332008-01-03 09:51:55 +00003410/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003411**
jplyon5a564222003-06-02 06:15:58 +00003412** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003413** created if it doesn't already exist or the data for an existing
danielk19771f4aa332008-01-03 09:51:55 +00003414** entry is overwritten. The data is the value stored register
3415** number P2. The key is stored in register P3. The key must
3416** be an integer.
drh4a324312001-12-21 14:30:42 +00003417**
danielk19771f4aa332008-01-03 09:51:55 +00003418** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3419** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003420** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003421** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003422**
drh66a51672008-01-03 00:01:23 +00003423** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003424** may be NULL. If it is not NULL, then the update-hook
3425** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3426**
drh93aed5a2008-01-16 17:46:38 +00003427** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3428** allocated, then ownership of P2 is transferred to the pseudo-cursor
3429** and register P2 becomes ephemeral. If the cursor is changed, the
3430** value of register P2 will then change. Make sure this does not
3431** cause any problems.)
3432**
drhf0863fe2005-06-12 21:35:51 +00003433** This instruction only works on tables. The equivalent instruction
3434** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003435*/
drh9cbf3422008-01-17 16:22:13 +00003436case OP_Insert: {
danielk19771f4aa332008-01-03 09:51:55 +00003437 Mem *pData = &p->aMem[pOp->p2];
3438 Mem *pKey = &p->aMem[pOp->p3];
3439
drha05a7222008-01-19 03:35:58 +00003440 i64 iKey; /* The integer ROWID or key for the record to be inserted */
drh5e00f6c2001-09-13 13:46:56 +00003441 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003442 VdbeCursor *pC;
drh6810ce62004-01-31 19:22:56 +00003443 assert( i>=0 && i<p->nCursor );
drha05a7222008-01-19 03:35:58 +00003444 pC = p->apCsr[i];
3445 assert( pC!=0 );
3446 assert( pC->pCursor!=0 || pC->pseudoTable );
3447 assert( pKey->flags & MEM_Int );
3448 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003449 REGISTER_TRACE(pOp->p2, pData);
3450 REGISTER_TRACE(pOp->p3, pKey);
danielk19775f8d8a82004-05-11 00:28:42 +00003451
drha05a7222008-01-19 03:35:58 +00003452 iKey = intToKey(pKey->u.i);
3453 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
3454 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = pKey->u.i;
3455 if( pC->nextRowidValid && pKey->u.i>=pC->nextRowid ){
3456 pC->nextRowidValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003457 }
drha05a7222008-01-19 03:35:58 +00003458 if( pData->flags & MEM_Null ){
3459 pData->z = 0;
3460 pData->n = 0;
3461 }else{
3462 assert( pData->flags & (MEM_Blob|MEM_Str) );
3463 }
3464 if( pC->pseudoTable ){
danielk19779882d992008-03-27 17:59:01 +00003465 if( !pC->ephemPseudoTable ){
drh633e6d52008-07-28 19:34:53 +00003466 sqlite3DbFree(db, pC->pData);
danielk19779882d992008-03-27 17:59:01 +00003467 }
drha05a7222008-01-19 03:35:58 +00003468 pC->iKey = iKey;
3469 pC->nData = pData->n;
danielk19775f096132008-03-28 15:44:09 +00003470 if( pData->z==pData->zMalloc || pC->ephemPseudoTable ){
drha05a7222008-01-19 03:35:58 +00003471 pC->pData = pData->z;
danielk19779882d992008-03-27 17:59:01 +00003472 if( !pC->ephemPseudoTable ){
3473 pData->flags &= ~MEM_Dyn;
3474 pData->flags |= MEM_Ephem;
danielk19775f096132008-03-28 15:44:09 +00003475 pData->zMalloc = 0;
danielk19779882d992008-03-27 17:59:01 +00003476 }
drha05a7222008-01-19 03:35:58 +00003477 }else{
drhe5ae5732008-06-15 02:51:47 +00003478 pC->pData = sqlite3Malloc( pC->nData+2 );
drha05a7222008-01-19 03:35:58 +00003479 if( !pC->pData ) goto no_mem;
3480 memcpy(pC->pData, pData->z, pC->nData);
3481 pC->pData[pC->nData] = 0;
3482 pC->pData[pC->nData+1] = 0;
3483 }
3484 pC->nullRow = 0;
3485 }else{
3486 int nZero;
3487 if( pData->flags & MEM_Zero ){
3488 nZero = pData->u.i;
3489 }else{
3490 nZero = 0;
3491 }
3492 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3493 pData->z, pData->n, nZero,
3494 pOp->p5 & OPFLAG_APPEND);
3495 }
3496
3497 pC->rowidIsValid = 0;
3498 pC->deferredMoveto = 0;
3499 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003500
drha05a7222008-01-19 03:35:58 +00003501 /* Invoke the update-hook if required. */
3502 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3503 const char *zDb = db->aDb[pC->iDb].zName;
3504 const char *zTbl = pOp->p4.z;
3505 int op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3506 assert( pC->isTable );
3507 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3508 assert( pC->iDb>=0 );
3509 }
drh5e00f6c2001-09-13 13:46:56 +00003510 break;
3511}
3512
drh98757152008-01-09 23:04:12 +00003513/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003514**
drh5edc3122001-09-13 21:53:09 +00003515** Delete the record at which the P1 cursor is currently pointing.
3516**
3517** The cursor will be left pointing at either the next or the previous
3518** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003519** the next Next instruction will be a no-op. Hence it is OK to delete
3520** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003521**
rdcb0c374f2004-02-20 22:53:38 +00003522** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003523** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003524**
drh91fd4d42008-01-19 20:11:25 +00003525** P1 must not be pseudo-table. It has to be a real table with
3526** multiple rows.
3527**
3528** If P4 is not NULL, then it is the name of the table that P1 is
3529** pointing to. The update hook will be invoked, if it exists.
3530** If P4 is not NULL then the P1 cursor must have been positioned
3531** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003532*/
drh9cbf3422008-01-17 16:22:13 +00003533case OP_Delete: {
drh5e00f6c2001-09-13 13:46:56 +00003534 int i = pOp->p1;
drh91fd4d42008-01-19 20:11:25 +00003535 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003536 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003537
drh70ce3f02003-04-15 19:22:22 +00003538 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003539 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003540 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003541 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003542
drh91fd4d42008-01-19 20:11:25 +00003543 /* If the update-hook will be invoked, set iKey to the rowid of the
3544 ** row being deleted.
3545 */
3546 if( db->xUpdateCallback && pOp->p4.z ){
3547 assert( pC->isTable );
3548 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3549 iKey = pC->lastRowid;
3550 }
danielk197794eb6a12005-12-15 15:22:08 +00003551
drh91fd4d42008-01-19 20:11:25 +00003552 rc = sqlite3VdbeCursorMoveto(pC);
3553 if( rc ) goto abort_due_to_error;
3554 rc = sqlite3BtreeDelete(pC->pCursor);
3555 pC->nextRowidValid = 0;
3556 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003557
drh91fd4d42008-01-19 20:11:25 +00003558 /* Invoke the update-hook if required. */
3559 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3560 const char *zDb = db->aDb[pC->iDb].zName;
3561 const char *zTbl = pOp->p4.z;
3562 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3563 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003564 }
danielk1977b28af712004-06-21 06:50:26 +00003565 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003566 break;
3567}
3568
danielk1977b28af712004-06-21 06:50:26 +00003569/* Opcode: ResetCount P1 * *
rdcb0c374f2004-02-20 22:53:38 +00003570**
danielk1977b28af712004-06-21 06:50:26 +00003571** This opcode resets the VMs internal change counter to 0. If P1 is true,
3572** then the value of the change counter is copied to the database handle
3573** change counter (returned by subsequent calls to sqlite3_changes())
3574** before it is reset. This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003575*/
drh9cbf3422008-01-17 16:22:13 +00003576case OP_ResetCount: {
danielk1977b28af712004-06-21 06:50:26 +00003577 if( pOp->p1 ){
drh344737f2004-09-19 00:50:20 +00003578 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003579 }
3580 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003581 break;
3582}
3583
drh98757152008-01-09 23:04:12 +00003584/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00003585**
drh98757152008-01-09 23:04:12 +00003586** Write into register P2 the complete row data for cursor P1.
3587** There is no interpretation of the data.
3588** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003589** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00003590**
drhde4fcfd2008-01-19 23:50:26 +00003591** If the P1 cursor must be pointing to a valid row (not a NULL row)
3592** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003593*/
drh98757152008-01-09 23:04:12 +00003594/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00003595**
drh98757152008-01-09 23:04:12 +00003596** Write into register P2 the complete row key for cursor P1.
3597** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00003598** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003599** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00003600**
drhde4fcfd2008-01-19 23:50:26 +00003601** If the P1 cursor must be pointing to a valid row (not a NULL row)
3602** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00003603*/
danielk1977a7a8e142008-02-13 18:25:27 +00003604case OP_RowKey:
3605case OP_RowData: {
drh70ce3f02003-04-15 19:22:22 +00003606 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003607 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00003608 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00003609 u32 n;
drh70ce3f02003-04-15 19:22:22 +00003610
danielk1977a7a8e142008-02-13 18:25:27 +00003611 pOut = &p->aMem[pOp->p2];
3612
drhf0863fe2005-06-12 21:35:51 +00003613 /* Note that RowKey and RowData are really exactly the same instruction */
drh70ce3f02003-04-15 19:22:22 +00003614 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003615 pC = p->apCsr[i];
drhf0863fe2005-06-12 21:35:51 +00003616 assert( pC->isTable || pOp->opcode==OP_RowKey );
3617 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003618 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00003619 assert( pC->nullRow==0 );
3620 assert( pC->pseudoTable==0 );
3621 assert( pC->pCursor!=0 );
3622 pCrsr = pC->pCursor;
3623 rc = sqlite3VdbeCursorMoveto(pC);
3624 if( rc ) goto abort_due_to_error;
3625 if( pC->isIndex ){
3626 i64 n64;
3627 assert( !pC->isTable );
3628 sqlite3BtreeKeySize(pCrsr, &n64);
drhbb4957f2008-03-20 14:03:29 +00003629 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00003630 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00003631 }
drhde4fcfd2008-01-19 23:50:26 +00003632 n = n64;
3633 }else{
3634 sqlite3BtreeDataSize(pCrsr, &n);
danielk197764202cf2008-11-17 15:31:47 +00003635 if( (int)n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00003636 goto too_big;
3637 }
drhde4fcfd2008-01-19 23:50:26 +00003638 }
danielk1977a7a8e142008-02-13 18:25:27 +00003639 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
3640 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00003641 }
danielk1977a7a8e142008-02-13 18:25:27 +00003642 pOut->n = n;
3643 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00003644 if( pC->isIndex ){
3645 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
3646 }else{
3647 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00003648 }
danielk197796cb76f2008-01-04 13:24:28 +00003649 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00003650 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00003651 break;
3652}
3653
drh2133d822008-01-03 18:44:59 +00003654/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003655**
drh2133d822008-01-03 18:44:59 +00003656** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00003657** P1 is currently point to.
drh5e00f6c2001-09-13 13:46:56 +00003658*/
drh4c583122008-01-04 22:01:03 +00003659case OP_Rowid: { /* out2-prerelease */
drh5e00f6c2001-09-13 13:46:56 +00003660 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003661 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00003662 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003663
drh70ce3f02003-04-15 19:22:22 +00003664 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003665 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003666 assert( pC!=0 );
drh536065a2005-01-26 21:55:31 +00003667 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003668 if( rc ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003669 if( pC->rowidIsValid ){
3670 v = pC->lastRowid;
drh70ce3f02003-04-15 19:22:22 +00003671 }else if( pC->pseudoTable ){
3672 v = keyToInt(pC->iKey);
drha05a7222008-01-19 03:35:58 +00003673 }else if( pC->nullRow ){
drh4c583122008-01-04 22:01:03 +00003674 /* Leave the rowid set to a NULL */
drhd60ccc62003-06-24 10:39:46 +00003675 break;
drh70ce3f02003-04-15 19:22:22 +00003676 }else{
3677 assert( pC->pCursor!=0 );
danielk1977e0d4b062004-06-28 01:11:46 +00003678 sqlite3BtreeKeySize(pC->pCursor, &v);
drh70ce3f02003-04-15 19:22:22 +00003679 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003680 }
drh4c583122008-01-04 22:01:03 +00003681 pOut->u.i = v;
danielk1977a7a8e142008-02-13 18:25:27 +00003682 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00003683 break;
3684}
3685
drh9cbf3422008-01-17 16:22:13 +00003686/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00003687**
3688** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00003689** that occur while the cursor is on the null row will always
3690** write a NULL.
drh17f71932002-02-21 12:01:27 +00003691*/
drh9cbf3422008-01-17 16:22:13 +00003692case OP_NullRow: {
drh17f71932002-02-21 12:01:27 +00003693 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003694 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00003695
drh70ce3f02003-04-15 19:22:22 +00003696 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003697 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003698 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00003699 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00003700 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00003701 if( pC->pCursor ){
3702 sqlite3BtreeClearCursor(pC->pCursor);
3703 }
drh17f71932002-02-21 12:01:27 +00003704 break;
3705}
3706
drh9cbf3422008-01-17 16:22:13 +00003707/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00003708**
drhf0863fe2005-06-12 21:35:51 +00003709** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00003710** will refer to the last entry in the database table or index.
3711** If the table or index is empty and P2>0, then jump immediately to P2.
3712** If P2 is 0 or if the table or index is not empty, fall through
3713** to the following instruction.
3714*/
drh9cbf3422008-01-17 16:22:13 +00003715case OP_Last: { /* jump */
drh9562b552002-02-19 15:00:07 +00003716 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003717 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00003718 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00003719 int res;
drh9562b552002-02-19 15:00:07 +00003720
drh70ce3f02003-04-15 19:22:22 +00003721 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003722 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003723 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00003724 pCrsr = pC->pCursor;
3725 assert( pCrsr!=0 );
3726 rc = sqlite3BtreeLast(pCrsr, &res);
3727 pC->nullRow = res;
3728 pC->deferredMoveto = 0;
3729 pC->cacheStatus = CACHE_STALE;
3730 if( res && pOp->p2>0 ){
3731 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00003732 }
3733 break;
3734}
3735
drh0342b1f2005-09-01 03:07:44 +00003736
drh9cbf3422008-01-17 16:22:13 +00003737/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00003738**
3739** This opcode does exactly the same thing as OP_Rewind except that
3740** it increments an undocumented global variable used for testing.
3741**
3742** Sorting is accomplished by writing records into a sorting index,
3743** then rewinding that index and playing it back from beginning to
3744** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3745** rewinding so that the global variable will be incremented and
3746** regression tests can determine whether or not the optimizer is
3747** correctly optimizing out sorts.
3748*/
drh9cbf3422008-01-17 16:22:13 +00003749case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00003750#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00003751 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00003752 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00003753#endif
drhd1d38482008-10-07 23:46:38 +00003754 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00003755 /* Fall through into OP_Rewind */
3756}
drh9cbf3422008-01-17 16:22:13 +00003757/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003758**
drhf0863fe2005-06-12 21:35:51 +00003759** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003760** will refer to the first entry in the database table or index.
3761** If the table or index is empty and P2>0, then jump immediately to P2.
3762** If P2 is 0 or if the table or index is not empty, fall through
3763** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003764*/
drh9cbf3422008-01-17 16:22:13 +00003765case OP_Rewind: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +00003766 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003767 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003768 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003769 int res;
drh5e00f6c2001-09-13 13:46:56 +00003770
drh70ce3f02003-04-15 19:22:22 +00003771 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003772 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003773 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003774 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003775 rc = sqlite3BtreeFirst(pCrsr, &res);
drh70ce3f02003-04-15 19:22:22 +00003776 pC->atFirst = res==0;
drha11846b2004-01-07 18:52:56 +00003777 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003778 pC->cacheStatus = CACHE_STALE;
drh70ce3f02003-04-15 19:22:22 +00003779 }else{
drhf4dada72004-05-11 09:57:35 +00003780 res = 1;
3781 }
3782 pC->nullRow = res;
drha05a7222008-01-19 03:35:58 +00003783 assert( pOp->p2>0 && pOp->p2<p->nOp );
3784 if( res ){
drhf4dada72004-05-11 09:57:35 +00003785 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003786 }
3787 break;
3788}
3789
drh9cbf3422008-01-17 16:22:13 +00003790/* Opcode: Next P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00003791**
3792** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003793** table or index. If there are no more key/value pairs then fall through
3794** to the following instruction. But if the cursor advance was successful,
3795** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003796**
drh60a713c2008-01-21 16:22:45 +00003797** The P1 cursor must be for a real table, not a pseudo-table.
3798**
drhc045ec52002-12-04 20:01:06 +00003799** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003800*/
drh9cbf3422008-01-17 16:22:13 +00003801/* Opcode: Prev P1 P2 * * *
drhc045ec52002-12-04 20:01:06 +00003802**
3803** Back up cursor P1 so that it points to the previous key/data pair in its
3804** table or index. If there is no previous key/value pairs then fall through
3805** to the following instruction. But if the cursor backup was successful,
3806** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00003807**
3808** The P1 cursor must be for a real table, not a pseudo-table.
drhc045ec52002-12-04 20:01:06 +00003809*/
drh9cbf3422008-01-17 16:22:13 +00003810case OP_Prev: /* jump */
3811case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00003812 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00003813 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00003814 int res;
drh8721ce42001-11-07 14:22:00 +00003815
drhcaec2f12003-01-07 02:47:47 +00003816 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00003817 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003818 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00003819 if( pC==0 ){
3820 break; /* See ticket #2273 */
3821 }
drh60a713c2008-01-21 16:22:45 +00003822 pCrsr = pC->pCursor;
3823 assert( pCrsr );
drha3460582008-07-11 21:02:53 +00003824 res = 1;
3825 assert( pC->deferredMoveto==0 );
3826 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3827 sqlite3BtreePrevious(pCrsr, &res);
3828 pC->nullRow = res;
3829 pC->cacheStatus = CACHE_STALE;
3830 if( res==0 ){
3831 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00003832 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00003833#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00003834 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003835#endif
drh8721ce42001-11-07 14:22:00 +00003836 }
drhf0863fe2005-06-12 21:35:51 +00003837 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00003838 break;
3839}
3840
drh9cbf3422008-01-17 16:22:13 +00003841/* Opcode: IdxInsert P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003842**
drhaa9b8962008-01-08 02:57:55 +00003843** Register P2 holds a SQL index key made using the
3844** MakeIdxRec instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00003845** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00003846**
drhaa9b8962008-01-08 02:57:55 +00003847** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00003848** insert is likely to be an append.
3849**
drhf0863fe2005-06-12 21:35:51 +00003850** This instruction only works for indices. The equivalent instruction
3851** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00003852*/
drh9cbf3422008-01-17 16:22:13 +00003853case OP_IdxInsert: { /* in2 */
drh5e00f6c2001-09-13 13:46:56 +00003854 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003855 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003856 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003857 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003858 assert( p->apCsr[i]!=0 );
drhaa9b8962008-01-08 02:57:55 +00003859 assert( pIn2->flags & MEM_Blob );
drhd7556d22004-05-14 21:59:40 +00003860 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drhf0863fe2005-06-12 21:35:51 +00003861 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00003862 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00003863 if( rc==SQLITE_OK ){
drhaa9b8962008-01-08 02:57:55 +00003864 int nKey = pIn2->n;
3865 const char *zKey = pIn2->z;
3866 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3);
danielk1977d908f5a2007-05-11 07:08:28 +00003867 assert( pC->deferredMoveto==0 );
3868 pC->cacheStatus = CACHE_STALE;
3869 }
drh5e00f6c2001-09-13 13:46:56 +00003870 }
drh5e00f6c2001-09-13 13:46:56 +00003871 break;
3872}
3873
drhd1d38482008-10-07 23:46:38 +00003874/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003875**
drhe14006d2008-03-25 17:23:32 +00003876** The content of P3 registers starting at register P2 form
3877** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00003878** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00003879*/
drhe14006d2008-03-25 17:23:32 +00003880case OP_IdxDelete: {
drh5e00f6c2001-09-13 13:46:56 +00003881 int i = pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003882 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003883 BtCursor *pCrsr;
drhe14006d2008-03-25 17:23:32 +00003884 assert( pOp->p3>0 );
3885 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem );
drh6810ce62004-01-31 19:22:56 +00003886 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003887 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003888 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197775bab7d2006-01-23 13:09:45 +00003889 int res;
drhe14006d2008-03-25 17:23:32 +00003890 UnpackedRecord r;
3891 r.pKeyInfo = pC->pKeyInfo;
3892 r.nField = pOp->p3;
drhe63d9992008-08-13 19:11:48 +00003893 r.flags = 0;
drhe14006d2008-03-25 17:23:32 +00003894 r.aMem = &p->aMem[pOp->p2];
drhe63d9992008-08-13 19:11:48 +00003895 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00003896 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00003897 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00003898 }
drh9188b382004-05-14 21:12:22 +00003899 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003900 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003901 }
drh5e00f6c2001-09-13 13:46:56 +00003902 break;
3903}
3904
drh2133d822008-01-03 18:44:59 +00003905/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00003906**
drh2133d822008-01-03 18:44:59 +00003907** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00003908** the end of the index key pointed to by cursor P1. This integer should be
3909** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00003910**
drh3c899a62006-01-10 18:44:08 +00003911** See also: Rowid, MakeIdxRec.
drh8721ce42001-11-07 14:22:00 +00003912*/
drh4c583122008-01-04 22:01:03 +00003913case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00003914 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003915 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00003916 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00003917
drh6810ce62004-01-31 19:22:56 +00003918 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003919 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003920 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00003921 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00003922
drhd7556d22004-05-14 21:59:40 +00003923 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00003924 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00003925 if( !pC->nullRow ){
drhb21c8cd2007-08-21 19:33:56 +00003926 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00003927 if( rc!=SQLITE_OK ){
3928 goto abort_due_to_error;
3929 }
danielk1977a7a8e142008-02-13 18:25:27 +00003930 MemSetTypeFlag(pOut, MEM_Int);
drh4c583122008-01-04 22:01:03 +00003931 pOut->u.i = rowid;
danielk19773d1bfea2004-05-14 11:00:53 +00003932 }
drh8721ce42001-11-07 14:22:00 +00003933 }
3934 break;
3935}
3936
danielk197761dd5832008-04-18 11:31:12 +00003937/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00003938**
danielk197761dd5832008-04-18 11:31:12 +00003939** The P4 register values beginning with P3 form an unpacked index
3940** key that omits the ROWID. Compare this key value against the index
3941** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00003942**
danielk197761dd5832008-04-18 11:31:12 +00003943** If the P1 index entry is greater than or equal to the key value
3944** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00003945**
danielk197761dd5832008-04-18 11:31:12 +00003946** If P5 is non-zero then the key value is increased by an epsilon
3947** prior to the comparison. This make the opcode work like IdxGT except
3948** that if the key from register P3 is a prefix of the key in the cursor,
3949** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00003950*/
drh98757152008-01-09 23:04:12 +00003951/* Opcode: IdxLT P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00003952**
danielk197761dd5832008-04-18 11:31:12 +00003953** The P4 register values beginning with P3 form an unpacked index
3954** key that omits the ROWID. Compare this key value against the index
3955** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00003956**
danielk197761dd5832008-04-18 11:31:12 +00003957** If the P1 index entry is less than the key value then jump to P2.
3958** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00003959**
danielk197761dd5832008-04-18 11:31:12 +00003960** If P5 is non-zero then the key value is increased by an epsilon prior
3961** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00003962*/
drh9cbf3422008-01-17 16:22:13 +00003963case OP_IdxLT: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003964case OP_IdxGE: { /* jump, in3 */
drh8721ce42001-11-07 14:22:00 +00003965 int i= pOp->p1;
drhdfe88ec2008-11-03 20:55:06 +00003966 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00003967
drh6810ce62004-01-31 19:22:56 +00003968 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003969 assert( p->apCsr[i]!=0 );
drh4f26bb62005-09-08 14:17:20 +00003970 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh0850b532006-01-31 19:31:43 +00003971 int res;
danielk197761dd5832008-04-18 11:31:12 +00003972 UnpackedRecord r;
drhd7556d22004-05-14 21:59:40 +00003973 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00003974 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00003975 assert( pOp->p4type==P4_INT32 );
3976 r.pKeyInfo = pC->pKeyInfo;
3977 r.nField = pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00003978 if( pOp->p5 ){
3979 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
3980 }else{
3981 r.flags = UNPACKED_IGNORE_ROWID;
3982 }
danielk197761dd5832008-04-18 11:31:12 +00003983 r.aMem = &p->aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00003984 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00003985 if( pOp->opcode==OP_IdxLT ){
3986 res = -res;
drha05a7222008-01-19 03:35:58 +00003987 }else{
3988 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00003989 res++;
3990 }
3991 if( res>0 ){
3992 pc = pOp->p2 - 1 ;
3993 }
3994 }
3995 break;
3996}
3997
drh98757152008-01-09 23:04:12 +00003998/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003999**
4000** Delete an entire database table or index whose root page in the database
4001** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004002**
drh98757152008-01-09 23:04:12 +00004003** The table being destroyed is in the main database file if P3==0. If
4004** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004005** that is used to store tables create using CREATE TEMPORARY TABLE.
4006**
drh205f48e2004-11-05 00:43:11 +00004007** If AUTOVACUUM is enabled then it is possible that another root page
4008** might be moved into the newly deleted root page in order to keep all
4009** root pages contiguous at the beginning of the database. The former
4010** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004011** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004012** movement was required (because the table being dropped was already
4013** the last one in the database) then a zero is stored in register P2.
4014** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004015**
drhb19a2bc2001-09-16 00:13:26 +00004016** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004017*/
drh98757152008-01-09 23:04:12 +00004018case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004019 int iMoved;
drh3765df42006-06-28 18:18:09 +00004020 int iCnt;
danielk1977212b2182006-06-23 14:32:08 +00004021#ifndef SQLITE_OMIT_VIRTUALTABLE
drh5a91a532007-01-05 16:39:43 +00004022 Vdbe *pVdbe;
danielk1977212b2182006-06-23 14:32:08 +00004023 iCnt = 0;
4024 for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
4025 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4026 iCnt++;
4027 }
4028 }
drh3765df42006-06-28 18:18:09 +00004029#else
4030 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004031#endif
4032 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004033 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004034 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004035 }else{
drh98757152008-01-09 23:04:12 +00004036 int iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004037 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004038 assert( (p->btreeMask & (1<<iDb))!=0 );
4039 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
danielk1977a7a8e142008-02-13 18:25:27 +00004040 MemSetTypeFlag(pOut, MEM_Int);
drh98757152008-01-09 23:04:12 +00004041 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004042#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004043 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004044 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
danielk1977e6efa742004-11-10 11:55:10 +00004045 }
drh3765df42006-06-28 18:18:09 +00004046#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004047 }
drh5e00f6c2001-09-13 13:46:56 +00004048 break;
4049}
4050
danielk1977c7af4842008-10-27 13:59:33 +00004051/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004052**
4053** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004054** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004055** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004056**
drhf57b3392001-10-08 13:22:32 +00004057** The table being clear is in the main database file if P2==0. If
4058** P2==1 then the table to be clear is in the auxiliary database file
4059** that is used to store tables create using CREATE TEMPORARY TABLE.
4060**
danielk1977c7af4842008-10-27 13:59:33 +00004061** If the P3 value is non-zero, then the table refered to must be an
4062** intkey table (an SQL table, not an index). In this case the row change
4063** count is incremented by the number of rows in the table being cleared.
4064** If P3 is greater than zero, then the value stored in register P3 is
4065** also incremented by the number of rows in the table being cleared.
4066**
drhb19a2bc2001-09-16 00:13:26 +00004067** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004068*/
drh9cbf3422008-01-17 16:22:13 +00004069case OP_Clear: {
danielk1977c7af4842008-10-27 13:59:33 +00004070 int nChange = 0;
drhfb982642007-08-30 01:19:59 +00004071 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004072 rc = sqlite3BtreeClearTable(
4073 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4074 );
4075 if( pOp->p3 ){
4076 p->nChange += nChange;
4077 if( pOp->p3>0 ){
4078 p->aMem[pOp->p3].u.i += nChange;
4079 }
4080 }
drh5edc3122001-09-13 21:53:09 +00004081 break;
4082}
4083
drh4c583122008-01-04 22:01:03 +00004084/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004085**
drh4c583122008-01-04 22:01:03 +00004086** Allocate a new table in the main database file if P1==0 or in the
4087** auxiliary database file if P1==1 or in an attached database if
4088** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004089** register P2
drh5b2fd562001-09-13 15:21:31 +00004090**
drhc6b52df2002-01-04 03:09:29 +00004091** The difference between a table and an index is this: A table must
4092** have a 4-byte integer key and can have arbitrary data. An index
4093** has an arbitrary key but no data.
4094**
drhb19a2bc2001-09-16 00:13:26 +00004095** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004096*/
drh4c583122008-01-04 22:01:03 +00004097/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004098**
drh4c583122008-01-04 22:01:03 +00004099** Allocate a new index in the main database file if P1==0 or in the
4100** auxiliary database file if P1==1 or in an attached database if
4101** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004102** register P2.
drhf57b3392001-10-08 13:22:32 +00004103**
drhc6b52df2002-01-04 03:09:29 +00004104** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004105*/
drh4c583122008-01-04 22:01:03 +00004106case OP_CreateIndex: /* out2-prerelease */
4107case OP_CreateTable: { /* out2-prerelease */
drh5b2fd562001-09-13 15:21:31 +00004108 int pgno;
drhf328bc82004-05-10 23:29:49 +00004109 int flags;
drh234c39d2004-07-24 03:30:47 +00004110 Db *pDb;
4111 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004112 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004113 pDb = &db->aDb[pOp->p1];
4114 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004115 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004116 /* flags = BTREE_INTKEY; */
4117 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004118 }else{
drhf328bc82004-05-10 23:29:49 +00004119 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00004120 }
drh234c39d2004-07-24 03:30:47 +00004121 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh5b2fd562001-09-13 15:21:31 +00004122 if( rc==SQLITE_OK ){
drh98757152008-01-09 23:04:12 +00004123 pOut->u.i = pgno;
danielk1977a7a8e142008-02-13 18:25:27 +00004124 MemSetTypeFlag(pOut, MEM_Int);
drh5b2fd562001-09-13 15:21:31 +00004125 }
4126 break;
4127}
4128
drh98757152008-01-09 23:04:12 +00004129/* Opcode: ParseSchema P1 P2 * P4 *
drh234c39d2004-07-24 03:30:47 +00004130**
4131** Read and parse all entries from the SQLITE_MASTER table of database P1
drh66a51672008-01-03 00:01:23 +00004132** that match the WHERE clause P4. P2 is the "force" flag. Always do
drh3c23a882007-01-09 14:01:13 +00004133** the parsing if P2 is true. If P2 is false, then this routine is a
4134** no-op if the schema is not currently loaded. In other words, if P2
4135** is false, the SQLITE_MASTER table is only parsed if the rest of the
4136** schema is already loaded into the symbol table.
drh234c39d2004-07-24 03:30:47 +00004137**
4138** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004139** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004140*/
drh9cbf3422008-01-17 16:22:13 +00004141case OP_ParseSchema: {
drh234c39d2004-07-24 03:30:47 +00004142 char *zSql;
4143 int iDb = pOp->p1;
4144 const char *zMaster;
4145 InitData initData;
4146
4147 assert( iDb>=0 && iDb<db->nDb );
drh3c23a882007-01-09 14:01:13 +00004148 if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
4149 break;
4150 }
danielk197753c0f742005-03-29 03:10:59 +00004151 zMaster = SCHEMA_TABLE(iDb);
drh234c39d2004-07-24 03:30:47 +00004152 initData.db = db;
drhece3c722006-09-23 20:36:01 +00004153 initData.iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004154 initData.pzErrMsg = &p->zErrMsg;
danielk19771e536952007-08-16 10:09:01 +00004155 zSql = sqlite3MPrintf(db,
drhece3c722006-09-23 20:36:01 +00004156 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
danielk19772dca4ac2008-01-03 11:50:29 +00004157 db->aDb[iDb].zName, zMaster, pOp->p4.z);
drh71c697e2004-08-08 23:39:19 +00004158 if( zSql==0 ) goto no_mem;
drh7e8b8482008-01-23 03:03:05 +00004159 (void)sqlite3SafetyOff(db);
drh234c39d2004-07-24 03:30:47 +00004160 assert( db->init.busy==0 );
4161 db->init.busy = 1;
drhc456e572008-08-11 18:44:58 +00004162 initData.rc = SQLITE_OK;
drh17435752007-08-16 04:30:38 +00004163 assert( !db->mallocFailed );
drh234c39d2004-07-24 03:30:47 +00004164 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
drhc456e572008-08-11 18:44:58 +00004165 if( rc==SQLITE_OK ) rc = initData.rc;
drh633e6d52008-07-28 19:34:53 +00004166 sqlite3DbFree(db, zSql);
drh234c39d2004-07-24 03:30:47 +00004167 db->init.busy = 0;
drh7e8b8482008-01-23 03:03:05 +00004168 (void)sqlite3SafetyOn(db);
danielk1977261919c2005-12-06 12:52:59 +00004169 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004170 goto no_mem;
4171 }
drh234c39d2004-07-24 03:30:47 +00004172 break;
4173}
4174
drhcfed7bc2006-03-13 14:28:05 +00004175#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
drh98757152008-01-09 23:04:12 +00004176/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004177**
4178** Read the sqlite_stat1 table for database P1 and load the content
4179** of that table into the internal index hash table. This will cause
4180** the analysis to be used when preparing all subsequent queries.
4181*/
drh9cbf3422008-01-17 16:22:13 +00004182case OP_LoadAnalysis: {
drh497e4462005-07-23 03:18:40 +00004183 int iDb = pOp->p1;
4184 assert( iDb>=0 && iDb<db->nDb );
drhcf1be452007-05-12 12:08:51 +00004185 rc = sqlite3AnalysisLoad(db, iDb);
drh497e4462005-07-23 03:18:40 +00004186 break;
4187}
drhcfed7bc2006-03-13 14:28:05 +00004188#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
drh497e4462005-07-23 03:18:40 +00004189
drh98757152008-01-09 23:04:12 +00004190/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004191**
4192** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004193** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004194** is dropped in order to keep the internal representation of the
4195** schema consistent with what is on disk.
4196*/
drh9cbf3422008-01-17 16:22:13 +00004197case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004198 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004199 break;
4200}
4201
drh98757152008-01-09 23:04:12 +00004202/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004203**
4204** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004205** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004206** is dropped in order to keep the internal representation of the
4207** schema consistent with what is on disk.
4208*/
drh9cbf3422008-01-17 16:22:13 +00004209case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004210 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004211 break;
4212}
4213
drh98757152008-01-09 23:04:12 +00004214/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004215**
4216** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004217** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004218** is dropped in order to keep the internal representation of the
4219** schema consistent with what is on disk.
4220*/
drh9cbf3422008-01-17 16:22:13 +00004221case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004222 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004223 break;
4224}
4225
drh234c39d2004-07-24 03:30:47 +00004226
drhb7f91642004-10-31 02:22:47 +00004227#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004228/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004229**
drh98757152008-01-09 23:04:12 +00004230** Do an analysis of the currently open database. Store in
4231** register P1 the text of an error message describing any problems.
4232** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004233**
drh98757152008-01-09 23:04:12 +00004234** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004235** At most reg(P3) errors will be reported.
4236** In other words, the analysis stops as soon as reg(P1) errors are
4237** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004238**
drh79069752004-05-22 21:30:40 +00004239** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004240** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004241** total.
drh21504322002-06-25 13:16:02 +00004242**
drh98757152008-01-09 23:04:12 +00004243** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004244** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004245**
drh1dcdbc02007-01-27 02:24:54 +00004246** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004247*/
drhaaab5722002-02-19 13:39:21 +00004248case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004249 int nRoot; /* Number of tables to check. (Number of root pages.) */
4250 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4251 int j; /* Loop counter */
4252 int nErr; /* Number of errors reported */
4253 char *z; /* Text of the error report */
4254 Mem *pnErr; /* Register keeping track of errors remaining */
4255
4256 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004257 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004258 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004259 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004260 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4261 pnErr = &p->aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004262 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004263 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
4264 pIn1 = &p->aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004265 for(j=0; j<nRoot; j++){
drh98757152008-01-09 23:04:12 +00004266 aRoot[j] = sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004267 }
4268 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004269 assert( pOp->p5<db->nDb );
4270 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4271 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh3c024d62007-03-30 11:23:45 +00004272 pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004273 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004274 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004275 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004276 if( nErr==0 ){
4277 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004278 }else if( z==0 ){
4279 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004280 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004281 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004282 }
drhb7654112008-01-12 12:48:07 +00004283 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004284 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004285 break;
4286}
drhb7f91642004-10-31 02:22:47 +00004287#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004288
drh3d4501e2008-12-04 20:40:10 +00004289/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004290**
drh3d4501e2008-12-04 20:40:10 +00004291** Insert the integer value held by register P2 into a boolean index
4292** held in register P1.
4293**
4294** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004295*/
drh3d4501e2008-12-04 20:40:10 +00004296case OP_RowSetAdd: { /* in2 */
4297 Mem *pIdx;
4298 Mem *pVal;
4299 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4300 pIdx = &p->aMem[pOp->p1];
4301 assert( pOp->p2>0 && pOp->p2<=p->nMem );
4302 pVal = &p->aMem[pOp->p2];
4303 assert( (pVal->flags & MEM_Int)!=0 );
4304 if( (pIdx->flags & MEM_RowSet)==0 ){
4305 sqlite3VdbeMemSetRowSet(pIdx);
drh8d993632008-12-04 22:17:55 +00004306 if( (pIdx->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004307 }
4308 sqlite3RowSetInsert(pIdx->u.pRowSet, pVal->u.i);
4309 break;
4310}
4311
4312/* Opcode: RowSetRead P1 P2 P3 * *
4313**
4314** Extract the smallest value from boolean index P1 and put that value into
4315** register P3. Or, if boolean index P1 is initially empty, leave P3
4316** unchanged and jump to instruction P2.
4317*/
4318case OP_RowSetRead: { /* jump, out3 */
4319 Mem *pIdx;
4320 i64 val;
4321 assert( pOp->p1>0 && pOp->p1<=p->nMem );
4322 CHECK_FOR_INTERRUPT;
4323 pIdx = &p->aMem[pOp->p1];
4324 if( (pIdx->flags & MEM_RowSet)==0
4325 || sqlite3RowSetNext(pIdx->u.pRowSet, &val)==0
4326 ){
4327 /* The boolean index is empty */
4328 sqlite3VdbeMemSetNull(pIdx);
4329 pc = pOp->p2 - 1;
4330 }else{
4331 /* A value was pulled from the index */
4332 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4333 pOut = &p->aMem[pOp->p3];
4334 sqlite3VdbeMemSetInt64(pOut, val);
drh17435752007-08-16 04:30:38 +00004335 }
drh5e00f6c2001-09-13 13:46:56 +00004336 break;
4337}
4338
drh5e00f6c2001-09-13 13:46:56 +00004339
danielk197793758c82005-01-21 08:13:14 +00004340#ifndef SQLITE_OMIT_TRIGGER
rdcb0c374f2004-02-20 22:53:38 +00004341/* Opcode: ContextPush * * *
4342**
4343** Save the current Vdbe context such that it can be restored by a ContextPop
4344** opcode. The context stores the last insert row id, the last statement change
4345** count, and the current statement change count.
4346*/
drh9cbf3422008-01-17 16:22:13 +00004347case OP_ContextPush: {
drh344737f2004-09-19 00:50:20 +00004348 int i = p->contextStackTop++;
4349 Context *pContext;
danielk1977b28af712004-06-21 06:50:26 +00004350
drh344737f2004-09-19 00:50:20 +00004351 assert( i>=0 );
danielk1977b28af712004-06-21 06:50:26 +00004352 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
drh344737f2004-09-19 00:50:20 +00004353 if( i>=p->contextStackDepth ){
4354 p->contextStackDepth = i+1;
danielk19771e536952007-08-16 10:09:01 +00004355 p->contextStack = sqlite3DbReallocOrFree(db, p->contextStack,
drhcf643722007-03-27 13:36:37 +00004356 sizeof(Context)*(i+1));
drh344737f2004-09-19 00:50:20 +00004357 if( p->contextStack==0 ) goto no_mem;
4358 }
4359 pContext = &p->contextStack[i];
4360 pContext->lastRowid = db->lastRowid;
4361 pContext->nChange = p->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004362 break;
4363}
4364
4365/* Opcode: ContextPop * * *
4366**
4367** Restore the Vdbe context to the state it was in when contextPush was last
4368** executed. The context stores the last insert row id, the last statement
4369** change count, and the current statement change count.
4370*/
drh9cbf3422008-01-17 16:22:13 +00004371case OP_ContextPop: {
drh344737f2004-09-19 00:50:20 +00004372 Context *pContext = &p->contextStack[--p->contextStackTop];
4373 assert( p->contextStackTop>=0 );
4374 db->lastRowid = pContext->lastRowid;
4375 p->nChange = pContext->nChange;
rdcb0c374f2004-02-20 22:53:38 +00004376 break;
4377}
danielk197793758c82005-01-21 08:13:14 +00004378#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004379
drh205f48e2004-11-05 00:43:11 +00004380#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00004381/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00004382**
drh98757152008-01-09 23:04:12 +00004383** Set the value of register P1 to the maximum of its current value
4384** and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00004385**
4386** This instruction throws an error if the memory cell is not initially
4387** an integer.
4388*/
drh9cbf3422008-01-17 16:22:13 +00004389case OP_MemMax: { /* in1, in2 */
drh98757152008-01-09 23:04:12 +00004390 sqlite3VdbeMemIntegerify(pIn1);
4391 sqlite3VdbeMemIntegerify(pIn2);
4392 if( pIn1->u.i<pIn2->u.i){
4393 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00004394 }
4395 break;
4396}
4397#endif /* SQLITE_OMIT_AUTOINCREMENT */
4398
drh98757152008-01-09 23:04:12 +00004399/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00004400**
drh98757152008-01-09 23:04:12 +00004401** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004402**
drh98757152008-01-09 23:04:12 +00004403** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004404** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00004405*/
drh9cbf3422008-01-17 16:22:13 +00004406case OP_IfPos: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004407 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004408 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00004409 pc = pOp->p2 - 1;
4410 }
4411 break;
4412}
4413
drh98757152008-01-09 23:04:12 +00004414/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00004415**
drh98757152008-01-09 23:04:12 +00004416** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00004417**
drh98757152008-01-09 23:04:12 +00004418** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00004419** not contain an integer. An assertion fault will result if you try.
4420*/
drh9cbf3422008-01-17 16:22:13 +00004421case OP_IfNeg: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004422 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004423 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00004424 pc = pOp->p2 - 1;
4425 }
4426 break;
4427}
4428
drh98757152008-01-09 23:04:12 +00004429/* Opcode: IfZero P1 P2 * * *
drhec7429a2005-10-06 16:53:14 +00004430**
drh98757152008-01-09 23:04:12 +00004431** If the value of register P1 is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004432**
drh98757152008-01-09 23:04:12 +00004433** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00004434** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00004435*/
drh9cbf3422008-01-17 16:22:13 +00004436case OP_IfZero: { /* jump, in1 */
danielk1977a7a8e142008-02-13 18:25:27 +00004437 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00004438 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00004439 pc = pOp->p2 - 1;
4440 }
4441 break;
4442}
4443
drh98757152008-01-09 23:04:12 +00004444/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00004445**
drh0bce8352002-02-28 00:41:10 +00004446** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00004447** function has P5 arguments. P4 is a pointer to the FuncDef
4448** structure that specifies the function. Use register
4449** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00004450**
drh98757152008-01-09 23:04:12 +00004451** The P5 arguments are taken from register P2 and its
4452** successors.
drhe5095352002-02-24 03:25:14 +00004453*/
drh9cbf3422008-01-17 16:22:13 +00004454case OP_AggStep: {
drh98757152008-01-09 23:04:12 +00004455 int n = pOp->p5;
drhe5095352002-02-24 03:25:14 +00004456 int i;
drh6810ce62004-01-31 19:22:56 +00004457 Mem *pMem, *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004458 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004459 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004460
drh6810ce62004-01-31 19:22:56 +00004461 assert( n>=0 );
drh98757152008-01-09 23:04:12 +00004462 pRec = &p->aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00004463 apVal = p->apArg;
4464 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00004465 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004466 apVal[i] = pRec;
drh8079a0d2006-01-12 17:20:50 +00004467 storeTypeInfo(pRec, encoding);
drhe5095352002-02-24 03:25:14 +00004468 }
danielk19772dca4ac2008-01-03 11:50:29 +00004469 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00004470 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4471 ctx.pMem = pMem = &p->aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00004472 pMem->n++;
drh90669c12006-01-20 15:45:36 +00004473 ctx.s.flags = MEM_Null;
4474 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00004475 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00004476 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00004477 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00004478 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00004479 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00004480 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00004481 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00004482 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00004483 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00004484 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00004485 }
danielk19776ddcca52004-05-24 23:48:25 +00004486 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00004487 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00004488 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00004489 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00004490 }
drh90669c12006-01-20 15:45:36 +00004491 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00004492 break;
4493}
4494
drh98757152008-01-09 23:04:12 +00004495/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004496**
drh13449892005-09-07 21:22:45 +00004497** Execute the finalizer function for an aggregate. P1 is
4498** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00004499**
4500** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00004501** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00004502** argument is not used by this opcode. It is only there to disambiguate
4503** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00004504** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00004505** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00004506*/
drh9cbf3422008-01-17 16:22:13 +00004507case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00004508 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00004509 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh13449892005-09-07 21:22:45 +00004510 pMem = &p->aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00004511 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00004512 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh90669c12006-01-20 15:45:36 +00004513 if( rc==SQLITE_ERROR ){
drhf089aa42008-07-08 19:34:06 +00004514 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00004515 }
drh2dca8682008-03-21 17:13:13 +00004516 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00004517 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00004518 if( sqlite3VdbeMemTooBig(pMem) ){
4519 goto too_big;
4520 }
drh5e00f6c2001-09-13 13:46:56 +00004521 break;
4522}
4523
drh5e00f6c2001-09-13 13:46:56 +00004524
drhfdbcdee2007-03-27 14:44:50 +00004525#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00004526/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00004527**
4528** Vacuum the entire database. This opcode will cause other virtual
4529** machines to be created and run. It may not be called from within
4530** a transaction.
4531*/
drh9cbf3422008-01-17 16:22:13 +00004532case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004533 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4534 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4535 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004536 break;
4537}
drh154d4b22006-09-21 11:02:16 +00004538#endif
drh6f8c91c2003-12-07 00:24:35 +00004539
danielk1977dddbcdc2007-04-26 14:42:34 +00004540#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00004541/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00004542**
4543** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00004544** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00004545** P2. Otherwise, fall through to the next instruction.
4546*/
drh9cbf3422008-01-17 16:22:13 +00004547case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00004548 Btree *pBt;
4549
4550 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004551 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00004552 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00004553 rc = sqlite3BtreeIncrVacuum(pBt);
4554 if( rc==SQLITE_DONE ){
4555 pc = pOp->p2 - 1;
4556 rc = SQLITE_OK;
4557 }
4558 break;
4559}
4560#endif
4561
drh98757152008-01-09 23:04:12 +00004562/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00004563**
4564** Cause precompiled statements to become expired. An expired statement
4565** fails with an error code of SQLITE_SCHEMA if it is ever executed
4566** (via sqlite3_step()).
4567**
4568** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4569** then only the currently executing statement is affected.
4570*/
drh9cbf3422008-01-17 16:22:13 +00004571case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00004572 if( !pOp->p1 ){
4573 sqlite3ExpirePreparedStatements(db);
4574 }else{
4575 p->expired = 1;
4576 }
4577 break;
4578}
4579
danielk1977c00da102006-01-07 13:21:04 +00004580#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00004581/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00004582**
4583** Obtain a lock on a particular table. This instruction is only used when
4584** the shared-cache feature is enabled.
4585**
drh6a9ad3d2008-04-02 16:29:30 +00004586** If P1 is the index of the database in sqlite3.aDb[] of the database
4587** on which the lock is acquired. A readlock is obtained if P3==0 or
4588** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00004589**
4590** P2 contains the root-page of the table to lock.
4591**
drh66a51672008-01-03 00:01:23 +00004592** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00004593** used to generate an error message if the lock cannot be obtained.
4594*/
drh9cbf3422008-01-17 16:22:13 +00004595case OP_TableLock: {
danielk1977c00da102006-01-07 13:21:04 +00004596 int p1 = pOp->p1;
drh6a9ad3d2008-04-02 16:29:30 +00004597 u8 isWriteLock = pOp->p3;
drhfb982642007-08-30 01:19:59 +00004598 assert( p1>=0 && p1<db->nDb );
4599 assert( (p->btreeMask & (1<<p1))!=0 );
drh6a9ad3d2008-04-02 16:29:30 +00004600 assert( isWriteLock==0 || isWriteLock==1 );
danielk1977c00da102006-01-07 13:21:04 +00004601 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4602 if( rc==SQLITE_LOCKED ){
danielk19772dca4ac2008-01-03 11:50:29 +00004603 const char *z = pOp->p4.z;
drhf089aa42008-07-08 19:34:06 +00004604 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
danielk1977c00da102006-01-07 13:21:04 +00004605 }
4606 break;
4607}
drhb9bb7c12006-06-11 23:41:55 +00004608#endif /* SQLITE_OMIT_SHARED_CACHE */
4609
4610#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004611/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004612**
danielk19773e3a84d2008-08-01 17:37:40 +00004613** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
4614** xBegin method for that table.
4615**
4616** Also, whether or not P4 is set, check that this is not being called from
4617** within a callback to a virtual table xSync() method. If it is, set the
4618** error code to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00004619*/
drh9cbf3422008-01-17 16:22:13 +00004620case OP_VBegin: {
danielk19773e3a84d2008-08-01 17:37:40 +00004621 sqlite3_vtab *pVtab = pOp->p4.pVtab;
4622 rc = sqlite3VtabBegin(db, pVtab);
4623 if( pVtab ){
4624 sqlite3DbFree(db, p->zErrMsg);
4625 p->zErrMsg = pVtab->zErrMsg;
4626 pVtab->zErrMsg = 0;
4627 }
danielk1977f9e7dda2006-06-16 16:08:53 +00004628 break;
4629}
4630#endif /* SQLITE_OMIT_VIRTUALTABLE */
4631
4632#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004633/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00004634**
drh66a51672008-01-03 00:01:23 +00004635** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00004636** for that table.
4637*/
drh9cbf3422008-01-17 16:22:13 +00004638case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00004639 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00004640 break;
4641}
4642#endif /* SQLITE_OMIT_VIRTUALTABLE */
4643
4644#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004645/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00004646**
drh66a51672008-01-03 00:01:23 +00004647** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00004648** of that table.
drhb9bb7c12006-06-11 23:41:55 +00004649*/
drh9cbf3422008-01-17 16:22:13 +00004650case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00004651 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00004652 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00004653 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00004654 break;
4655}
4656#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00004657
drh9eff6162006-06-12 21:59:13 +00004658#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004659/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00004660**
drh66a51672008-01-03 00:01:23 +00004661** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00004662** P1 is a cursor number. This opcode opens a cursor to the virtual
4663** table and stores that cursor in P1.
4664*/
drh9cbf3422008-01-17 16:22:13 +00004665case OP_VOpen: {
drhdfe88ec2008-11-03 20:55:06 +00004666 VdbeCursor *pCur = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004667 sqlite3_vtab_cursor *pVtabCursor = 0;
4668
danielk19772dca4ac2008-01-03 11:50:29 +00004669 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004670 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4671
4672 assert(pVtab && pModule);
4673 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4674 rc = pModule->xOpen(pVtab, &pVtabCursor);
drh633e6d52008-07-28 19:34:53 +00004675 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00004676 p->zErrMsg = pVtab->zErrMsg;
4677 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004678 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4679 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00004680 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004681 pVtabCursor->pVtab = pVtab;
4682
4683 /* Initialise vdbe cursor object */
danielk1977cd3e8f72008-03-25 09:47:35 +00004684 pCur = allocateCursor(p, pOp->p1, &pOp[-1], -1, 0);
danielk1977be718892006-06-23 08:05:19 +00004685 if( pCur ){
4686 pCur->pVtabCursor = pVtabCursor;
4687 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004688 }else{
drh17435752007-08-16 04:30:38 +00004689 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00004690 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00004691 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004692 }
drh9eff6162006-06-12 21:59:13 +00004693 break;
4694}
4695#endif /* SQLITE_OMIT_VIRTUALTABLE */
4696
4697#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00004698/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00004699**
4700** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4701** the filtered result set is empty.
4702**
drh66a51672008-01-03 00:01:23 +00004703** P4 is either NULL or a string that was generated by the xBestIndex
4704** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00004705** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00004706**
drh9eff6162006-06-12 21:59:13 +00004707** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00004708** by P1. The integer query plan parameter to xFilter is stored in register
4709** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00004710** xFilter method. Registers P3+2..P3+1+argc are the argc
4711** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00004712** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00004713**
danielk19776dbee812008-01-03 18:39:41 +00004714** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00004715*/
drh9cbf3422008-01-17 16:22:13 +00004716case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00004717 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00004718 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004719 const sqlite3_module *pModule;
danielk19776dbee812008-01-03 18:39:41 +00004720 Mem *pQuery = &p->aMem[pOp->p3];
4721 Mem *pArgc = &pQuery[1];
drh4dc754d2008-07-23 18:17:32 +00004722 sqlite3_vtab_cursor *pVtabCursor;
4723 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004724
drhdfe88ec2008-11-03 20:55:06 +00004725 VdbeCursor *pCur = p->apCsr[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00004726
4727 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004728 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00004729 pVtabCursor = pCur->pVtabCursor;
4730 pVtab = pVtabCursor->pVtab;
4731 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004732
drh9cbf3422008-01-17 16:22:13 +00004733 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00004734 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
4735 nArg = pArgc->u.i;
4736 iQuery = pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004737
drh644a5292006-12-20 14:53:38 +00004738 /* Invoke the xFilter method */
4739 {
drh3f87d2a2006-12-20 14:31:24 +00004740 int res = 0;
drh4be8b512006-06-13 23:51:34 +00004741 int i;
4742 Mem **apArg = p->apArg;
4743 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00004744 apArg[i] = &pArgc[i+1];
drh4be8b512006-06-13 23:51:34 +00004745 storeTypeInfo(apArg[i], 0);
danielk19775fac9f82006-06-13 14:16:58 +00004746 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004747
4748 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00004749 sqlite3VtabLock(pVtab);
danielk1977be718892006-06-23 08:05:19 +00004750 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00004751 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00004752 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00004753 sqlite3DbFree(db, p->zErrMsg);
4754 p->zErrMsg = pVtab->zErrMsg;
4755 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00004756 sqlite3VtabUnlock(db, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00004757 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00004758 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00004759 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004760 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4761
danielk1977a298e902006-06-22 09:53:48 +00004762 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00004763 pc = pOp->p2 - 1;
4764 }
4765 }
drh1d454a32008-01-31 19:34:51 +00004766 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004767
drh9eff6162006-06-12 21:59:13 +00004768 break;
4769}
4770#endif /* SQLITE_OMIT_VIRTUALTABLE */
4771
4772#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004773/* Opcode: VRowid P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00004774**
drh2133d822008-01-03 18:44:59 +00004775** Store into register P2 the rowid of
drh9eff6162006-06-12 21:59:13 +00004776** the virtual-table that the P1 cursor is pointing to.
4777*/
drh4c583122008-01-04 22:01:03 +00004778case OP_VRowid: { /* out2-prerelease */
danielk19773e3a84d2008-08-01 17:37:40 +00004779 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004780 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004781 sqlite_int64 iRow;
drhdfe88ec2008-11-03 20:55:06 +00004782 VdbeCursor *pCur = p->apCsr[pOp->p1];
drhde4fcfd2008-01-19 23:50:26 +00004783
danielk1977b7a7b9a2006-06-13 10:24:42 +00004784 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004785 if( pCur->nullRow ){
4786 break;
4787 }
danielk19773e3a84d2008-08-01 17:37:40 +00004788 pVtab = pCur->pVtabCursor->pVtab;
4789 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00004790 assert( pModule->xRowid );
4791 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4792 rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
danielk19773e3a84d2008-08-01 17:37:40 +00004793 sqlite3DbFree(db, p->zErrMsg);
4794 p->zErrMsg = pVtab->zErrMsg;
4795 pVtab->zErrMsg = 0;
drhde4fcfd2008-01-19 23:50:26 +00004796 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977a7a8e142008-02-13 18:25:27 +00004797 MemSetTypeFlag(pOut, MEM_Int);
drhde4fcfd2008-01-19 23:50:26 +00004798 pOut->u.i = iRow;
drh9eff6162006-06-12 21:59:13 +00004799 break;
4800}
4801#endif /* SQLITE_OMIT_VIRTUALTABLE */
4802
4803#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004804/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00004805**
drh2133d822008-01-03 18:44:59 +00004806** Store the value of the P2-th column of
4807** the row of the virtual-table that the
4808** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00004809*/
4810case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00004811 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004812 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00004813 Mem *pDest;
4814 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004815
drhdfe88ec2008-11-03 20:55:06 +00004816 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00004817 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004818 assert( pOp->p3>0 && pOp->p3<=p->nMem );
4819 pDest = &p->aMem[pOp->p3];
4820 if( pCur->nullRow ){
4821 sqlite3VdbeMemSetNull(pDest);
4822 break;
4823 }
danielk19773e3a84d2008-08-01 17:37:40 +00004824 pVtab = pCur->pVtabCursor->pVtab;
4825 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00004826 assert( pModule->xColumn );
4827 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00004828
4829 /* The output cell may already have a buffer allocated. Move
4830 ** the current contents to sContext.s so in case the user-function
4831 ** can use the already allocated buffer instead of allocating a
4832 ** new one.
4833 */
4834 sqlite3VdbeMemMove(&sContext.s, pDest);
4835 MemSetTypeFlag(&sContext.s, MEM_Null);
4836
drhde4fcfd2008-01-19 23:50:26 +00004837 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4838 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
danielk19773e3a84d2008-08-01 17:37:40 +00004839 sqlite3DbFree(db, p->zErrMsg);
4840 p->zErrMsg = pVtab->zErrMsg;
4841 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004842
drhde4fcfd2008-01-19 23:50:26 +00004843 /* Copy the result of the function to the P3 register. We
4844 ** do this regardless of whether or not an error occured to ensure any
4845 ** dynamic allocation in sContext.s (a Mem struct) is released.
4846 */
4847 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00004848 REGISTER_TRACE(pOp->p3, pDest);
4849 sqlite3VdbeMemMove(pDest, &sContext.s);
4850 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004851
drhde4fcfd2008-01-19 23:50:26 +00004852 if( sqlite3SafetyOn(db) ){
4853 goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004854 }
drhde4fcfd2008-01-19 23:50:26 +00004855 if( sqlite3VdbeMemTooBig(pDest) ){
4856 goto too_big;
4857 }
drh9eff6162006-06-12 21:59:13 +00004858 break;
4859}
4860#endif /* SQLITE_OMIT_VIRTUALTABLE */
4861
4862#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004863/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00004864**
4865** Advance virtual table P1 to the next row in its result set and
4866** jump to instruction P2. Or, if the virtual table has reached
4867** the end of its result set, then fall through to the next instruction.
4868*/
drh9cbf3422008-01-17 16:22:13 +00004869case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00004870 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004871 const sqlite3_module *pModule;
4872 int res = 0;
4873
drhdfe88ec2008-11-03 20:55:06 +00004874 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00004875 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00004876 if( pCur->nullRow ){
4877 break;
4878 }
danielk19773e3a84d2008-08-01 17:37:40 +00004879 pVtab = pCur->pVtabCursor->pVtab;
4880 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00004881 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00004882
drhde4fcfd2008-01-19 23:50:26 +00004883 /* Invoke the xNext() method of the module. There is no way for the
4884 ** underlying implementation to return an error if one occurs during
4885 ** xNext(). Instead, if an error occurs, true is returned (indicating that
4886 ** data is available) and the error code returned when xColumn or
4887 ** some other method is next invoked on the save virtual table cursor.
4888 */
4889 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk19775a114ca2008-08-02 15:10:08 +00004890 sqlite3VtabLock(pVtab);
drhde4fcfd2008-01-19 23:50:26 +00004891 p->inVtabMethod = 1;
4892 rc = pModule->xNext(pCur->pVtabCursor);
4893 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00004894 sqlite3DbFree(db, p->zErrMsg);
4895 p->zErrMsg = pVtab->zErrMsg;
4896 pVtab->zErrMsg = 0;
danielk19775a114ca2008-08-02 15:10:08 +00004897 sqlite3VtabUnlock(db, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00004898 if( rc==SQLITE_OK ){
4899 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004900 }
drhde4fcfd2008-01-19 23:50:26 +00004901 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977b7a7b9a2006-06-13 10:24:42 +00004902
drhde4fcfd2008-01-19 23:50:26 +00004903 if( !res ){
4904 /* If there is data, jump to P2 */
4905 pc = pOp->p2 - 1;
4906 }
drh9eff6162006-06-12 21:59:13 +00004907 break;
4908}
4909#endif /* SQLITE_OMIT_VIRTUALTABLE */
4910
danielk1977182c4ba2007-06-27 15:53:34 +00004911#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004912/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00004913**
drh66a51672008-01-03 00:01:23 +00004914** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00004915** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00004916** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00004917*/
drh9cbf3422008-01-17 16:22:13 +00004918case OP_VRename: {
danielk19772dca4ac2008-01-03 11:50:29 +00004919 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk19776dbee812008-01-03 18:39:41 +00004920 Mem *pName = &p->aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00004921 assert( pVtab->pModule->xRename );
drh5b6afba2008-01-05 16:29:28 +00004922 REGISTER_TRACE(pOp->p1, pName);
danielk1977182c4ba2007-06-27 15:53:34 +00004923
danielk19776dbee812008-01-03 18:39:41 +00004924 Stringify(pName, encoding);
danielk1977182c4ba2007-06-27 15:53:34 +00004925
4926 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4927 sqlite3VtabLock(pVtab);
danielk19776dbee812008-01-03 18:39:41 +00004928 rc = pVtab->pModule->xRename(pVtab, pName->z);
drh633e6d52008-07-28 19:34:53 +00004929 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00004930 p->zErrMsg = pVtab->zErrMsg;
4931 pVtab->zErrMsg = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00004932 sqlite3VtabUnlock(db, pVtab);
4933 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4934
danielk1977182c4ba2007-06-27 15:53:34 +00004935 break;
4936}
4937#endif
drh4cbdda92006-06-14 19:00:20 +00004938
4939#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00004940/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00004941**
drh66a51672008-01-03 00:01:23 +00004942** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00004943** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00004944** are contiguous memory cells starting at P3 to pass to the xUpdate
4945** invocation. The value in register (P3+P2-1) corresponds to the
4946** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00004947**
4948** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00004949** The argv[0] element (which corresponds to memory cell P3)
4950** is the rowid of a row to delete. If argv[0] is NULL then no
4951** deletion occurs. The argv[1] element is the rowid of the new
4952** row. This can be NULL to have the virtual table select the new
4953** rowid for itself. The subsequent elements in the array are
4954** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00004955**
4956** If P2==1 then no insert is performed. argv[0] is the rowid of
4957** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00004958**
4959** P1 is a boolean flag. If it is set to true and the xUpdate call
4960** is successful, then the value returned by sqlite3_last_insert_rowid()
4961** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00004962*/
drh9cbf3422008-01-17 16:22:13 +00004963case OP_VUpdate: {
danielk19772dca4ac2008-01-03 11:50:29 +00004964 sqlite3_vtab *pVtab = pOp->p4.pVtab;
danielk1977399918f2006-06-14 13:03:23 +00004965 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
drh4cbdda92006-06-14 19:00:20 +00004966 int nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00004967 assert( pOp->p4type==P4_VTAB );
danielk1977399918f2006-06-14 13:03:23 +00004968 if( pModule->xUpdate==0 ){
drhf089aa42008-07-08 19:34:06 +00004969 sqlite3SetString(&p->zErrMsg, db, "read-only table");
danielk1977399918f2006-06-14 13:03:23 +00004970 rc = SQLITE_ERROR;
4971 }else{
4972 int i;
danielk19771f6eec52006-06-16 06:17:47 +00004973 sqlite_int64 rowid;
danielk1977399918f2006-06-14 13:03:23 +00004974 Mem **apArg = p->apArg;
danielk19772a339ff2008-01-03 17:31:44 +00004975 Mem *pX = &p->aMem[pOp->p3];
4976 for(i=0; i<nArg; i++){
drh9c419382006-06-16 21:13:21 +00004977 storeTypeInfo(pX, 0);
4978 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00004979 pX++;
danielk1977399918f2006-06-14 13:03:23 +00004980 }
danielk1977c7d54102006-06-15 07:29:00 +00004981 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh189d4af2006-09-02 20:57:52 +00004982 sqlite3VtabLock(pVtab);
danielk19771f6eec52006-06-16 06:17:47 +00004983 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drh633e6d52008-07-28 19:34:53 +00004984 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00004985 p->zErrMsg = pVtab->zErrMsg;
4986 pVtab->zErrMsg = 0;
danielk1977a04a34f2007-04-16 15:06:25 +00004987 sqlite3VtabUnlock(db, pVtab);
danielk1977c7d54102006-06-15 07:29:00 +00004988 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk19771f6eec52006-06-16 06:17:47 +00004989 if( pOp->p1 && rc==SQLITE_OK ){
4990 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
4991 db->lastRowid = rowid;
4992 }
drhb5df1442008-04-10 14:00:09 +00004993 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00004994 }
drh4cbdda92006-06-14 19:00:20 +00004995 break;
danielk1977399918f2006-06-14 13:03:23 +00004996}
4997#endif /* SQLITE_OMIT_VIRTUALTABLE */
4998
danielk197759a93792008-05-15 17:48:20 +00004999#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5000/* Opcode: Pagecount P1 P2 * * *
5001**
5002** Write the current number of pages in database P1 to memory cell P2.
5003*/
5004case OP_Pagecount: { /* out2-prerelease */
5005 int p1 = pOp->p1;
5006 int nPage;
5007 Pager *pPager = sqlite3BtreePager(db->aDb[p1].pBt);
5008
danielk1977ad0132d2008-06-07 08:58:22 +00005009 rc = sqlite3PagerPagecount(pPager, &nPage);
5010 if( rc==SQLITE_OK ){
danielk197759a93792008-05-15 17:48:20 +00005011 pOut->flags = MEM_Int;
5012 pOut->u.i = nPage;
5013 }
5014 break;
5015}
5016#endif
5017
drh949f9cd2008-01-12 21:35:57 +00005018#ifndef SQLITE_OMIT_TRACE
5019/* Opcode: Trace * * * P4 *
5020**
5021** If tracing is enabled (by the sqlite3_trace()) interface, then
5022** the UTF-8 string contained in P4 is emitted on the trace callback.
5023*/
5024case OP_Trace: {
5025 if( pOp->p4.z ){
5026 if( db->xTrace ){
5027 db->xTrace(db->pTraceArg, pOp->p4.z);
5028 }
5029#ifdef SQLITE_DEBUG
5030 if( (db->flags & SQLITE_SqlTrace)!=0 ){
5031 sqlite3DebugPrintf("SQL-trace: %s\n", pOp->p4.z);
5032 }
5033#endif /* SQLITE_DEBUG */
5034 }
5035 break;
5036}
5037#endif
5038
drh91fd4d42008-01-19 20:11:25 +00005039
5040/* Opcode: Noop * * * * *
5041**
5042** Do nothing. This instruction is often useful as a jump
5043** destination.
drh5e00f6c2001-09-13 13:46:56 +00005044*/
drh91fd4d42008-01-19 20:11:25 +00005045/*
5046** The magic Explain opcode are only inserted when explain==2 (which
5047** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5048** This opcode records information from the optimizer. It is the
5049** the same as a no-op. This opcodesnever appears in a real VM program.
5050*/
5051default: { /* This is really OP_Noop and OP_Explain */
drh5e00f6c2001-09-13 13:46:56 +00005052 break;
5053}
5054
5055/*****************************************************************************
5056** The cases of the switch statement above this line should all be indented
5057** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5058** readability. From this point on down, the normal indentation rules are
5059** restored.
5060*****************************************************************************/
5061 }
drh6e142f52000-06-08 13:36:40 +00005062
drh7b396862003-01-01 23:06:20 +00005063#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005064 {
shane9bcbdad2008-05-29 20:22:37 +00005065 u64 elapsed = sqlite3Hwtime() - start;
5066 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005067 pOp->cnt++;
5068#if 0
shane9bcbdad2008-05-29 20:22:37 +00005069 fprintf(stdout, "%10llu ", elapsed);
danielk19774adee202004-05-08 08:23:19 +00005070 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005071#endif
5072 }
drh7b396862003-01-01 23:06:20 +00005073#endif
5074
drh6e142f52000-06-08 13:36:40 +00005075 /* The following code adds nothing to the actual functionality
5076 ** of the program. It is only here for testing and debugging.
5077 ** On the other hand, it does burn CPU cycles every time through
5078 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5079 */
5080#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005081 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005082
drhcf1023c2007-05-08 20:59:49 +00005083#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005084 if( p->trace ){
5085 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drhca48c902008-01-18 14:08:24 +00005086 if( opProperty & OPFLG_OUT2_PRERELEASE ){
drh5b6afba2008-01-05 16:29:28 +00005087 registerTrace(p->trace, pOp->p2, pOut);
drh75897232000-05-29 14:26:00 +00005088 }
drhca48c902008-01-18 14:08:24 +00005089 if( opProperty & OPFLG_OUT3 ){
drh5b6afba2008-01-05 16:29:28 +00005090 registerTrace(p->trace, pOp->p3, pOut);
5091 }
drh75897232000-05-29 14:26:00 +00005092 }
danielk1977b5402fb2005-01-12 07:15:04 +00005093#endif /* SQLITE_DEBUG */
5094#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005095 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005096
drha05a7222008-01-19 03:35:58 +00005097 /* If we reach this point, it means that execution is finished with
5098 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005099 */
drha05a7222008-01-19 03:35:58 +00005100vdbe_error_halt:
5101 assert( rc );
5102 p->rc = rc;
drh92f02c32004-09-02 14:57:08 +00005103 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005104 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5105 rc = SQLITE_ERROR;
drh900b31e2007-08-28 02:27:51 +00005106
5107 /* This is the only way out of this procedure. We have to
5108 ** release the mutexes on btrees that were acquired at the
5109 ** top. */
5110vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005111 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005112 return rc;
5113
drh023ae032007-05-08 12:12:16 +00005114 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5115 ** is encountered.
5116 */
5117too_big:
drhf089aa42008-07-08 19:34:06 +00005118 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005119 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005120 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005121
drh98640a32007-06-07 19:08:32 +00005122 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005123 */
5124no_mem:
drh17435752007-08-16 04:30:38 +00005125 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005126 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005127 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005128 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005129
5130 /* Jump to here for an SQLITE_MISUSE error.
5131 */
5132abort_due_to_misuse:
5133 rc = SQLITE_MISUSE;
5134 /* Fall thru into abort_due_to_error */
5135
5136 /* Jump to here for any other kind of fatal error. The "rc" variable
5137 ** should hold the error number.
5138 */
5139abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005140 assert( p->zErrMsg==0 );
5141 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005142 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005143 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005144 }
drha05a7222008-01-19 03:35:58 +00005145 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005146
danielk19776f8a5032004-05-10 10:34:51 +00005147 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005148 ** flag.
5149 */
5150abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005151 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005152 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005153 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005154 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005155 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005156}