blob: 43d132f1bad87a498147e0ffa1443d2620e884c0 [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.
drh75897232000-05-29 14:26:00 +000045*/
46#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000047#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000048
49/*
drh2b4ded92010-09-27 21:09:31 +000050** Invoke this macro on memory cells just prior to changing the
51** value of the cell. This macro verifies that shallow copies are
52** not misused.
53*/
54#ifdef SQLITE_DEBUG
55# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
56#else
57# define memAboutToChange(P,M)
58#endif
59
60/*
drh487ab3c2001-11-08 00:45:21 +000061** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000062** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000063** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000064** working correctly. This variable has no function other than to
65** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000066*/
drh0f7eb612006-08-08 13:51:43 +000067#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000068int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000069#endif
drh487ab3c2001-11-08 00:45:21 +000070
drhf6038712004-02-08 18:07:34 +000071/*
72** When this global variable is positive, it gets decremented once before
drh881feaa2006-07-26 01:39:30 +000073** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
74** field of the sqlite3 structure is set in order to simulate and interrupt.
drhf6038712004-02-08 18:07:34 +000075**
76** This facility is used for testing purposes only. It does not function
77** in an ordinary build.
78*/
drh0f7eb612006-08-08 13:51:43 +000079#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000080int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000081#endif
drh1350b032002-02-27 19:00:20 +000082
danielk19777e18c252004-05-25 11:47:24 +000083/*
drh6bf89572004-11-03 16:27:01 +000084** The next global variable is incremented each type the OP_Sort opcode
85** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000086** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000087** has no function other than to help verify the correct operation of the
88** library.
89*/
drh0f7eb612006-08-08 13:51:43 +000090#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000091int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000092#endif
drh6bf89572004-11-03 16:27:01 +000093
94/*
drhae7e1512007-05-02 16:51:59 +000095** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000096** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000097** use this information to make sure that the zero-blob functionality
98** is working correctly. This variable has no function other than to
99** help verify the correct operation of the library.
100*/
101#ifdef SQLITE_TEST
102int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +0000103static void updateMaxBlobsize(Mem *p){
104 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
105 sqlite3_max_blobsize = p->n;
106 }
107}
drhae7e1512007-05-02 16:51:59 +0000108#endif
109
110/*
dan0ff297e2009-09-25 17:03:14 +0000111** The next global variable is incremented each type the OP_Found opcode
112** is executed. This is used to test whether or not the foreign key
113** operation implemented using OP_FkIsZero is working. This variable
114** has no function other than to help verify the correct operation of the
115** library.
116*/
117#ifdef SQLITE_TEST
118int sqlite3_found_count = 0;
119#endif
120
121/*
drhb7654112008-01-12 12:48:07 +0000122** Test a register to see if it exceeds the current maximum blob size.
123** If it does, record the new maximum blob size.
124*/
drh678ccce2008-03-31 18:19:54 +0000125#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000126# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000127#else
128# define UPDATE_MAX_BLOBSIZE(P)
129#endif
130
131/*
drh9cbf3422008-01-17 16:22:13 +0000132** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000133** already. Return non-zero if a malloc() fails.
134*/
drhb21c8cd2007-08-21 19:33:56 +0000135#define Stringify(P, enc) \
136 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000137 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000138
139/*
danielk1977bd7e4602004-05-24 07:34:48 +0000140** An ephemeral string value (signified by the MEM_Ephem flag) contains
141** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000142** is responsible for deallocating that string. Because the register
143** does not control the string, it might be deleted without the register
144** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000145**
146** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000147** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000148** converts an MEM_Ephem string into an MEM_Dyn string.
149*/
drhb21c8cd2007-08-21 19:33:56 +0000150#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000151 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000152 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000153
154/*
danielk19771cc5ed82007-05-16 17:28:43 +0000155** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
156** P if required.
157*/
drhb21c8cd2007-08-21 19:33:56 +0000158#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +0000159
160/*
shane21e7feb2008-05-30 15:59:49 +0000161** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000162** user-defined function or returned to the user as the result of a query.
dan937d0de2009-10-15 18:35:38 +0000163** This routine sets the pMem->type variable used by the sqlite3_value_*()
164** routines.
danielk1977c572ef72004-05-27 09:28:41 +0000165*/
dan937d0de2009-10-15 18:35:38 +0000166void sqlite3VdbeMemStoreType(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000167 int flags = pMem->flags;
168 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000169 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000170 }
171 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000172 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000173 }
174 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000175 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000176 }
177 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000178 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000179 }else{
drh9c054832004-05-31 18:51:57 +0000180 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000181 }
182}
danielk19778a6b5412004-05-24 07:04:25 +0000183
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 */
danielk1977d336e222009-02-20 10:58:41 +0000191 int nField, /* Number of fields in the table or index */
drh3d4501e2008-12-04 20:40:10 +0000192 int iDb, /* When database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000193 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
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;
danielk19775f096132008-03-28 15:44:09 +0000217 nByte =
drhc54055b2009-11-13 17:05:53 +0000218 ROUND8(sizeof(VdbeCursor)) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000219 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
220 2*nField*sizeof(u32);
221
drh290c1942004-08-21 17:54:45 +0000222 assert( iCur<p->nCursor );
223 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000224 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000225 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000226 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000227 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000228 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000229 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000230 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000231 pCx->nField = nField;
232 if( nField ){
drhc54055b2009-11-13 17:05:53 +0000233 pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
danielk1977cd3e8f72008-03-25 09:47:35 +0000234 }
235 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000236 pCx->pCursor = (BtCursor*)
drhc54055b2009-11-13 17:05:53 +0000237 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
drhf25a5072009-11-18 23:01:25 +0000238 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000239 }
danielk197794eb6a12005-12-15 15:22:08 +0000240 }
drh4774b132004-06-12 20:12:51 +0000241 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000242}
243
danielk19773d1bfea2004-05-14 11:00:53 +0000244/*
drh29d72102006-02-09 22:13:41 +0000245** Try to convert a value into a numeric representation if we can
246** do so without loss of information. In other words, if the string
247** looks like a number, convert it into a number. If it does not
248** look like a number, leave it alone.
249*/
drhb21c8cd2007-08-21 19:33:56 +0000250static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000251 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000252 double rValue;
253 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000254 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000255 if( (pRec->flags&MEM_Str)==0 ) return;
256 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000257 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000258 pRec->u.i = iValue;
259 pRec->flags |= MEM_Int;
260 }else{
261 pRec->r = rValue;
262 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000263 }
264 }
265}
266
267/*
drh8a512562005-11-14 22:29:05 +0000268** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000269**
drh8a512562005-11-14 22:29:05 +0000270** SQLITE_AFF_INTEGER:
271** SQLITE_AFF_REAL:
272** SQLITE_AFF_NUMERIC:
273** Try to convert pRec to an integer representation or a
274** floating-point representation if an integer representation
275** is not possible. Note that the integer representation is
276** always preferred, even if the affinity is REAL, because
277** an integer representation is more space efficient on disk.
278**
279** SQLITE_AFF_TEXT:
280** Convert pRec to a text representation.
281**
282** SQLITE_AFF_NONE:
283** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000284*/
drh17435752007-08-16 04:30:38 +0000285static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000286 Mem *pRec, /* The value to apply affinity to */
287 char affinity, /* The affinity to be applied */
288 u8 enc /* Use this text encoding */
289){
drh8a512562005-11-14 22:29:05 +0000290 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000291 /* Only attempt the conversion to TEXT if there is an integer or real
292 ** representation (blob and NULL do not get converted) but no string
293 ** representation.
294 */
295 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000296 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000297 }
298 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000299 }else if( affinity!=SQLITE_AFF_NONE ){
300 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
301 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000302 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000303 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000304 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000305 }
danielk19773d1bfea2004-05-14 11:00:53 +0000306 }
307}
308
danielk1977aee18ef2005-03-09 12:26:50 +0000309/*
drh29d72102006-02-09 22:13:41 +0000310** Try to convert the type of a function argument or a result column
311** into a numeric representation. Use either INTEGER or REAL whichever
312** is appropriate. But only do the conversion if it is possible without
313** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000314*/
315int sqlite3_value_numeric_type(sqlite3_value *pVal){
316 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000317 if( pMem->type==SQLITE_TEXT ){
318 applyNumericAffinity(pMem);
319 sqlite3VdbeMemStoreType(pMem);
320 }
drh29d72102006-02-09 22:13:41 +0000321 return pMem->type;
322}
323
324/*
danielk1977aee18ef2005-03-09 12:26:50 +0000325** Exported version of applyAffinity(). This one works on sqlite3_value*,
326** not the internal Mem* type.
327*/
danielk19771e536952007-08-16 10:09:01 +0000328void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000329 sqlite3_value *pVal,
330 u8 affinity,
331 u8 enc
332){
drhb21c8cd2007-08-21 19:33:56 +0000333 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000334}
335
danielk1977b5402fb2005-01-12 07:15:04 +0000336#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000337/*
danielk1977ca6b2912004-05-21 10:49:47 +0000338** Write a nice string representation of the contents of cell pMem
339** into buffer zBuf, length nBuf.
340*/
drh74161702006-02-24 02:53:49 +0000341void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000342 char *zCsr = zBuf;
343 int f = pMem->flags;
344
drh57196282004-10-06 15:41:16 +0000345 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000346
danielk1977ca6b2912004-05-21 10:49:47 +0000347 if( f&MEM_Blob ){
348 int i;
349 char c;
350 if( f & MEM_Dyn ){
351 c = 'z';
352 assert( (f & (MEM_Static|MEM_Ephem))==0 );
353 }else if( f & MEM_Static ){
354 c = 't';
355 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
356 }else if( f & MEM_Ephem ){
357 c = 'e';
358 assert( (f & (MEM_Static|MEM_Dyn))==0 );
359 }else{
360 c = 's';
361 }
362
drh5bb3eb92007-05-04 13:15:55 +0000363 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000364 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000365 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000366 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000367 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000368 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000369 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000370 }
371 for(i=0; i<16 && i<pMem->n; i++){
372 char z = pMem->z[i];
373 if( z<32 || z>126 ) *zCsr++ = '.';
374 else *zCsr++ = z;
375 }
376
drhe718efe2007-05-10 21:14:03 +0000377 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000378 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000379 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000380 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000381 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000382 }
danielk1977b1bc9532004-05-22 03:05:33 +0000383 *zCsr = '\0';
384 }else if( f & MEM_Str ){
385 int j, k;
386 zBuf[0] = ' ';
387 if( f & MEM_Dyn ){
388 zBuf[1] = 'z';
389 assert( (f & (MEM_Static|MEM_Ephem))==0 );
390 }else if( f & MEM_Static ){
391 zBuf[1] = 't';
392 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
393 }else if( f & MEM_Ephem ){
394 zBuf[1] = 'e';
395 assert( (f & (MEM_Static|MEM_Dyn))==0 );
396 }else{
397 zBuf[1] = 's';
398 }
399 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000400 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000401 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000402 zBuf[k++] = '[';
403 for(j=0; j<15 && j<pMem->n; j++){
404 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000405 if( c>=0x20 && c<0x7f ){
406 zBuf[k++] = c;
407 }else{
408 zBuf[k++] = '.';
409 }
410 }
411 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000412 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000413 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000414 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000415 }
danielk1977ca6b2912004-05-21 10:49:47 +0000416}
417#endif
418
drh5b6afba2008-01-05 16:29:28 +0000419#ifdef SQLITE_DEBUG
420/*
421** Print the value of a register for tracing purposes:
422*/
423static void memTracePrint(FILE *out, Mem *p){
424 if( p->flags & MEM_Null ){
425 fprintf(out, " NULL");
426 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
427 fprintf(out, " si:%lld", p->u.i);
428 }else if( p->flags & MEM_Int ){
429 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000430#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000431 }else if( p->flags & MEM_Real ){
432 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000433#endif
drh733bf1b2009-04-22 00:47:00 +0000434 }else if( p->flags & MEM_RowSet ){
435 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000436 }else{
437 char zBuf[200];
438 sqlite3VdbeMemPrettyPrint(p, zBuf);
439 fprintf(out, " ");
440 fprintf(out, "%s", zBuf);
441 }
442}
443static void registerTrace(FILE *out, int iReg, Mem *p){
444 fprintf(out, "REG[%d] = ", iReg);
445 memTracePrint(out, p);
446 fprintf(out, "\n");
447}
448#endif
449
450#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000451# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000452#else
453# define REGISTER_TRACE(R,M)
454#endif
455
danielk197784ac9d02004-05-18 09:58:06 +0000456
drh7b396862003-01-01 23:06:20 +0000457#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000458
459/*
460** hwtime.h contains inline assembler code for implementing
461** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000462*/
shane9bcbdad2008-05-29 20:22:37 +0000463#include "hwtime.h"
464
drh7b396862003-01-01 23:06:20 +0000465#endif
466
drh8c74a8c2002-08-25 19:20:40 +0000467/*
drhcaec2f12003-01-07 02:47:47 +0000468** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000469** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000470** processing of the VDBE program is interrupted.
471**
472** This macro added to every instruction that does a jump in order to
473** implement a loop. This test used to be on every single instruction,
474** but that meant we more testing that we needed. By only testing the
475** flag on jump instructions, we get a (small) speed improvement.
476*/
477#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000478 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000479
480
danielk1977fd7f0452008-12-17 17:30:26 +0000481#ifndef NDEBUG
482/*
483** This function is only called from within an assert() expression. It
484** checks that the sqlite3.nTransaction variable is correctly set to
485** the number of non-transaction savepoints currently in the
486** linked list starting at sqlite3.pSavepoint.
487**
488** Usage:
489**
490** assert( checkSavepointCount(db) );
491*/
492static int checkSavepointCount(sqlite3 *db){
493 int n = 0;
494 Savepoint *p;
495 for(p=db->pSavepoint; p; p=p->pNext) n++;
496 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
497 return 1;
498}
499#endif
500
drhcaec2f12003-01-07 02:47:47 +0000501/*
drhb9755982010-07-24 16:34:37 +0000502** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
503** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
504** in memory obtained from sqlite3DbMalloc).
505*/
506static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
507 sqlite3 *db = p->db;
508 sqlite3DbFree(db, p->zErrMsg);
509 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
510 sqlite3_free(pVtab->zErrMsg);
511 pVtab->zErrMsg = 0;
512}
513
514
515/*
drhb86ccfb2003-01-28 23:13:10 +0000516** Execute as much of a VDBE program as we can then return.
517**
danielk19774adee202004-05-08 08:23:19 +0000518** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000519** close the program with a final OP_Halt and to set up the callbacks
520** and the error message pointer.
521**
522** Whenever a row or result data is available, this routine will either
523** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000524** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000525**
526** If an attempt is made to open a locked database, then this routine
527** will either invoke the busy callback (if there is one) or it will
528** return SQLITE_BUSY.
529**
530** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000531** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000532** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
533**
534** If the callback ever returns non-zero, then the program exits
535** immediately. There will be no error message but the p->rc field is
536** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
537**
drh9468c7f2003-03-07 19:50:07 +0000538** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
539** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000540**
541** Other fatal errors return SQLITE_ERROR.
542**
danielk19774adee202004-05-08 08:23:19 +0000543** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000544** used to clean up the mess that was left behind.
545*/
danielk19774adee202004-05-08 08:23:19 +0000546int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000547 Vdbe *p /* The VDBE */
548){
shaneh84f4b2f2010-02-26 01:46:54 +0000549 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000550 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000551 Op *pOp; /* Current operation */
552 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000553 sqlite3 *db = p->db; /* The database */
drh32783152009-11-20 15:02:34 +0000554 u8 resetSchemaOnFault = 0; /* Reset schema after an error if true */
drh8079a0d2006-01-12 17:20:50 +0000555 u8 encoding = ENC(db); /* The database encoding */
drha6c2ed92009-11-14 23:22:23 +0000556#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
shaneh5e17e8b2009-12-03 04:40:47 +0000557 int checkProgress; /* True if progress callbacks are enabled */
drha6c2ed92009-11-14 23:22:23 +0000558 int nProgressOps = 0; /* Opcodes executed since progress callback. */
559#endif
560 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000561 Mem *pIn1 = 0; /* 1st input operand */
562 Mem *pIn2 = 0; /* 2nd input operand */
563 Mem *pIn3 = 0; /* 3rd input operand */
564 Mem *pOut = 0; /* Output operand */
drh0acb7e42008-06-25 00:12:41 +0000565 int iCompare = 0; /* Result of last OP_Compare operation */
shanebe217792009-03-05 04:20:31 +0000566 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000567#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000568 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000569 int origPc; /* Program counter at start of opcode */
570#endif
drh856c1032009-06-02 15:21:42 +0000571 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000572
drhca48c902008-01-18 14:08:24 +0000573 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
danielk1977f7590db2009-04-10 12:55:16 +0000574 sqlite3VdbeMutexArrayEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000575 if( p->rc==SQLITE_NOMEM ){
576 /* This happens if a malloc() inside a call to sqlite3_column_text() or
577 ** sqlite3_column_text16() failed. */
578 goto no_mem;
579 }
drh3a840692003-01-29 22:58:26 +0000580 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
581 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000582 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000583 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000584 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000585 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000586 sqlite3VdbeIOTraceSql(p);
drha6c2ed92009-11-14 23:22:23 +0000587#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
588 checkProgress = db->xProgress!=0;
589#endif
drh3c23a882007-01-09 14:01:13 +0000590#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000591 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000592 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000593 int i;
594 printf("VDBE Program Listing:\n");
595 sqlite3VdbePrintSql(p);
596 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000597 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000598 }
599 }
danielk19772d1d86f2008-06-20 14:59:51 +0000600 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000601#endif
drhb86ccfb2003-01-28 23:13:10 +0000602 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000603 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000604 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000605#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000606 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000607 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000608#endif
drhbbe879d2009-11-14 18:04:35 +0000609 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000610
danielk19778b60e0f2005-01-12 09:10:39 +0000611 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000612 */
danielk19778b60e0f2005-01-12 09:10:39 +0000613#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000614 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000615 if( pc==0 ){
616 printf("VDBE Execution Trace:\n");
617 sqlite3VdbePrintSql(p);
618 }
danielk19774adee202004-05-08 08:23:19 +0000619 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000620 }
drh3f7d4e42004-07-24 14:35:58 +0000621#endif
622
drh6e142f52000-06-08 13:36:40 +0000623
drhf6038712004-02-08 18:07:34 +0000624 /* Check to see if we need to simulate an interrupt. This only happens
625 ** if we have a special test build.
626 */
627#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000628 if( sqlite3_interrupt_count>0 ){
629 sqlite3_interrupt_count--;
630 if( sqlite3_interrupt_count==0 ){
631 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000632 }
633 }
634#endif
635
danielk1977348bb5d2003-10-18 09:37:26 +0000636#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
637 /* Call the progress callback if it is configured and the required number
638 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000639 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000640 ** If the progress callback returns non-zero, exit the virtual machine with
641 ** a return code SQLITE_ABORT.
642 */
drha6c2ed92009-11-14 23:22:23 +0000643 if( checkProgress ){
drh3914aed2004-01-31 20:40:42 +0000644 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000645 int prc;
drh9978c972010-02-23 17:36:32 +0000646 prc = db->xProgress(db->pProgressArg);
danielk1977de523ac2007-06-15 14:53:53 +0000647 if( prc!=0 ){
648 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000649 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000650 }
danielk19773fe11f32007-06-13 16:49:48 +0000651 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000652 }
drh3914aed2004-01-31 20:40:42 +0000653 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000654 }
danielk1977348bb5d2003-10-18 09:37:26 +0000655#endif
656
drh3c657212009-11-17 23:59:58 +0000657 /* On any opcode with the "out2-prerelase" tag, free any
658 ** external allocations out of mem[p2] and set mem[p2] to be
659 ** an undefined integer. Opcodes will either fill in the integer
660 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000661 */
drha6c2ed92009-11-14 23:22:23 +0000662 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000663 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
664 assert( pOp->p2>0 );
665 assert( pOp->p2<=p->nMem );
666 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000667 memAboutToChange(p, pOut);
drh3c657212009-11-17 23:59:58 +0000668 sqlite3VdbeMemReleaseExternal(pOut);
669 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000670 }
drh3c657212009-11-17 23:59:58 +0000671
672 /* Sanity checking on other operands */
673#ifdef SQLITE_DEBUG
674 if( (pOp->opflags & OPFLG_IN1)!=0 ){
675 assert( pOp->p1>0 );
676 assert( pOp->p1<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000677 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000678 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
679 }
680 if( (pOp->opflags & OPFLG_IN2)!=0 ){
681 assert( pOp->p2>0 );
682 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000683 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000684 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
685 }
686 if( (pOp->opflags & OPFLG_IN3)!=0 ){
687 assert( pOp->p3>0 );
688 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000689 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000690 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
691 }
692 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
693 assert( pOp->p2>0 );
694 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000695 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000696 }
697 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
698 assert( pOp->p3>0 );
699 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000700 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000701 }
702#endif
drh93952eb2009-11-13 19:43:43 +0000703
drh75897232000-05-29 14:26:00 +0000704 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000705
drh5e00f6c2001-09-13 13:46:56 +0000706/*****************************************************************************
707** What follows is a massive switch statement where each case implements a
708** separate instruction in the virtual machine. If we follow the usual
709** indentation conventions, each case should be indented by 6 spaces. But
710** that is a lot of wasted space on the left margin. So the code within
711** the switch statement will break with convention and be flush-left. Another
712** big comment (similar to this one) will mark the point in the code where
713** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000714**
715** The formatting of each case is important. The makefile for SQLite
716** generates two C files "opcodes.h" and "opcodes.c" by scanning this
717** file looking for lines that begin with "case OP_". The opcodes.h files
718** will be filled with #defines that give unique integer values to each
719** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000720** each string is the symbolic name for the corresponding opcode. If the
721** case statement is followed by a comment of the form "/# same as ... #/"
722** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000723**
drh9cbf3422008-01-17 16:22:13 +0000724** Other keywords in the comment that follows each case are used to
725** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
726** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
727** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000728**
drhac82fcf2002-09-08 17:23:41 +0000729** Documentation about VDBE opcodes is generated by scanning this file
730** for lines of that contain "Opcode:". That line and all subsequent
731** comment lines are used in the generation of the opcode.html documentation
732** file.
733**
734** SUMMARY:
735**
736** Formatting is important to scripts that scan this file.
737** Do not deviate from the formatting style currently in use.
738**
drh5e00f6c2001-09-13 13:46:56 +0000739*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000740
drh9cbf3422008-01-17 16:22:13 +0000741/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000742**
743** An unconditional jump to address P2.
744** The next instruction executed will be
745** the one at index P2 from the beginning of
746** the program.
747*/
drh9cbf3422008-01-17 16:22:13 +0000748case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000749 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000750 pc = pOp->p2 - 1;
751 break;
752}
drh75897232000-05-29 14:26:00 +0000753
drh2eb95372008-06-06 15:04:36 +0000754/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000755**
drh2eb95372008-06-06 15:04:36 +0000756** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000757** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000758*/
drh93952eb2009-11-13 19:43:43 +0000759case OP_Gosub: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +0000760 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000761 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000762 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000763 pIn1->flags = MEM_Int;
764 pIn1->u.i = pc;
765 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000766 pc = pOp->p2 - 1;
767 break;
768}
769
drh2eb95372008-06-06 15:04:36 +0000770/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000771**
drh2eb95372008-06-06 15:04:36 +0000772** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000773*/
drh2eb95372008-06-06 15:04:36 +0000774case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000775 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000776 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000777 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000778 break;
779}
780
drhe00ee6e2008-06-20 15:24:01 +0000781/* Opcode: Yield P1 * * * *
782**
783** Swap the program counter with the value in register P1.
784*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000785case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000786 int pcDest;
drh3c657212009-11-17 23:59:58 +0000787 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000788 assert( (pIn1->flags & MEM_Dyn)==0 );
789 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000790 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000791 pIn1->u.i = pc;
792 REGISTER_TRACE(pOp->p1, pIn1);
793 pc = pcDest;
794 break;
795}
796
drh5053a792009-02-20 03:02:23 +0000797/* Opcode: HaltIfNull P1 P2 P3 P4 *
798**
799** Check the value in register P3. If is is NULL then Halt using
800** parameter P1, P2, and P4 as if this were a Halt instruction. If the
801** value in register P3 is not NULL, then this routine is a no-op.
802*/
803case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000804 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000805 if( (pIn3->flags & MEM_Null)==0 ) break;
806 /* Fall through into OP_Halt */
807}
drhe00ee6e2008-06-20 15:24:01 +0000808
drh9cbf3422008-01-17 16:22:13 +0000809/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000810**
drh3d4501e2008-12-04 20:40:10 +0000811** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000812** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000813**
drh92f02c32004-09-02 14:57:08 +0000814** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
815** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
816** For errors, it can be some other value. If P1!=0 then P2 will determine
817** whether or not to rollback the current transaction. Do not rollback
818** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
819** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000820** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000821**
drh66a51672008-01-03 00:01:23 +0000822** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000823**
drh9cfcf5d2002-01-29 18:41:24 +0000824** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000825** every program. So a jump past the last instruction of the program
826** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000827*/
drh9cbf3422008-01-17 16:22:13 +0000828case OP_Halt: {
dan165921a2009-08-28 18:53:45 +0000829 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000830 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000831 VdbeFrame *pFrame = p->pFrame;
832 p->pFrame = pFrame->pParent;
833 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000834 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000835 pc = sqlite3VdbeFrameRestore(pFrame);
836 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000837 /* Instruction pc is the OP_Program that invoked the sub-program
838 ** currently being halted. If the p2 instruction of this OP_Halt
839 ** instruction is set to OE_Ignore, then the sub-program is throwing
840 ** an IGNORE exception. In this case jump to the address specified
841 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000842 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000843 }
drhbbe879d2009-11-14 18:04:35 +0000844 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000845 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000846 break;
847 }
dan2832ad42009-08-31 15:27:27 +0000848
drh92f02c32004-09-02 14:57:08 +0000849 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000850 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000851 p->pc = pc;
danielk19772dca4ac2008-01-03 11:50:29 +0000852 if( pOp->p4.z ){
drh413c3d32010-02-23 20:11:56 +0000853 assert( p->rc!=SQLITE_OK );
drhf089aa42008-07-08 19:34:06 +0000854 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhaf46dc12010-02-24 21:44:07 +0000855 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000856 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
drhcda455b2010-02-24 19:23:56 +0000857 }else if( p->rc ){
drhaf46dc12010-02-24 21:44:07 +0000858 testcase( sqlite3GlobalConfig.xLog!=0 );
drhcda455b2010-02-24 19:23:56 +0000859 sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
drh9cfcf5d2002-01-29 18:41:24 +0000860 }
drh92f02c32004-09-02 14:57:08 +0000861 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000862 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000863 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000864 p->rc = rc = SQLITE_BUSY;
865 }else{
dan1da40a32009-09-19 17:00:31 +0000866 assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
867 assert( rc==SQLITE_OK || db->nDeferredCons>0 );
drh900b31e2007-08-28 02:27:51 +0000868 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000869 }
drh900b31e2007-08-28 02:27:51 +0000870 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000871}
drhc61053b2000-06-04 12:58:36 +0000872
drh4c583122008-01-04 22:01:03 +0000873/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000874**
drh9cbf3422008-01-17 16:22:13 +0000875** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000876*/
drh4c583122008-01-04 22:01:03 +0000877case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000878 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000879 break;
880}
881
drh4c583122008-01-04 22:01:03 +0000882/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000883**
drh66a51672008-01-03 00:01:23 +0000884** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000885** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000886*/
drh4c583122008-01-04 22:01:03 +0000887case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000888 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000889 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000890 break;
891}
drh4f26d6c2004-05-26 23:25:30 +0000892
drh13573c72010-01-12 17:04:07 +0000893#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000894/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000895**
drh4c583122008-01-04 22:01:03 +0000896** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000897** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000898*/
drh4c583122008-01-04 22:01:03 +0000899case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
900 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000901 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000902 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000903 break;
904}
drh13573c72010-01-12 17:04:07 +0000905#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000906
drh3c84ddf2008-01-09 02:15:38 +0000907/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000908**
drh66a51672008-01-03 00:01:23 +0000909** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000910** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000911*/
drh4c583122008-01-04 22:01:03 +0000912case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000913 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000914 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000915 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000916
917#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000918 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000919 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
920 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000921 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000922 assert( pOut->zMalloc==pOut->z );
923 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000924 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000925 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000926 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000927 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000928 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000929 }
drh66a51672008-01-03 00:01:23 +0000930 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000931 pOp->p4.z = pOut->z;
932 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000933 }
danielk197793758c82005-01-21 08:13:14 +0000934#endif
drhbb4957f2008-03-20 14:03:29 +0000935 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000936 goto too_big;
937 }
938 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000939}
drhf4479502004-05-27 03:12:53 +0000940
drh4c583122008-01-04 22:01:03 +0000941/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000942**
drh9cbf3422008-01-17 16:22:13 +0000943** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000944*/
drh4c583122008-01-04 22:01:03 +0000945case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000946 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000947 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
948 pOut->z = pOp->p4.z;
949 pOut->n = pOp->p1;
950 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000951 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000952 break;
953}
954
drh4c583122008-01-04 22:01:03 +0000955/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000956**
drh9cbf3422008-01-17 16:22:13 +0000957** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000958*/
drh4c583122008-01-04 22:01:03 +0000959case OP_Null: { /* out2-prerelease */
drh3c657212009-11-17 23:59:58 +0000960 pOut->flags = MEM_Null;
drhf0863fe2005-06-12 21:35:51 +0000961 break;
962}
963
964
drh9de221d2008-01-05 06:51:30 +0000965/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000966**
drh9de221d2008-01-05 06:51:30 +0000967** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +0000968** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +0000969*/
drh4c583122008-01-04 22:01:03 +0000970case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000971 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000972 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000973 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000974 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000975 break;
976}
977
drheaf52d82010-05-12 13:50:23 +0000978/* Opcode: Variable P1 P2 * P4 *
drh50457892003-09-06 01:10:47 +0000979**
drheaf52d82010-05-12 13:50:23 +0000980** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +0000981**
982** If the parameter is named, then its name appears in P4 and P3==1.
983** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +0000984*/
drheaf52d82010-05-12 13:50:23 +0000985case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +0000986 Mem *pVar; /* Value being transferred */
987
drheaf52d82010-05-12 13:50:23 +0000988 assert( pOp->p1>0 && pOp->p1<=p->nVar );
989 pVar = &p->aVar[pOp->p1 - 1];
990 if( sqlite3VdbeMemTooBig(pVar) ){
991 goto too_big;
drh023ae032007-05-08 12:12:16 +0000992 }
drheaf52d82010-05-12 13:50:23 +0000993 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
994 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +0000995 break;
996}
danielk1977295ba552004-05-19 10:34:51 +0000997
drhb21e7c72008-06-22 12:37:57 +0000998/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +0000999**
drhb21e7c72008-06-22 12:37:57 +00001000** Move the values in register P1..P1+P3-1 over into
1001** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
1002** left holding a NULL. It is an error for register ranges
1003** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001004*/
drhe1349cb2008-04-01 00:36:10 +00001005case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001006 char *zMalloc; /* Holding variable for allocated memory */
1007 int n; /* Number of registers left to copy */
1008 int p1; /* Register to copy from */
1009 int p2; /* Register to copy to */
1010
1011 n = pOp->p3;
1012 p1 = pOp->p1;
1013 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001014 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001015 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001016
drha6c2ed92009-11-14 23:22:23 +00001017 pIn1 = &aMem[p1];
1018 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001019 while( n-- ){
drha6c2ed92009-11-14 23:22:23 +00001020 assert( pOut<=&aMem[p->nMem] );
1021 assert( pIn1<=&aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00001022 assert( memIsValid(pIn1) );
1023 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001024 zMalloc = pOut->zMalloc;
1025 pOut->zMalloc = 0;
1026 sqlite3VdbeMemMove(pOut, pIn1);
1027 pIn1->zMalloc = zMalloc;
1028 REGISTER_TRACE(p2++, pOut);
1029 pIn1++;
1030 pOut++;
1031 }
drhe1349cb2008-04-01 00:36:10 +00001032 break;
1033}
1034
drhb1fdb2a2008-01-05 04:06:03 +00001035/* Opcode: Copy P1 P2 * * *
1036**
drh9cbf3422008-01-17 16:22:13 +00001037** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001038**
1039** This instruction makes a deep copy of the value. A duplicate
1040** is made of any string or blob constant. See also OP_SCopy.
1041*/
drh93952eb2009-11-13 19:43:43 +00001042case OP_Copy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001043 pIn1 = &aMem[pOp->p1];
1044 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001045 assert( pOut!=pIn1 );
1046 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1047 Deephemeralize(pOut);
1048 REGISTER_TRACE(pOp->p2, pOut);
1049 break;
1050}
1051
drhb1fdb2a2008-01-05 04:06:03 +00001052/* Opcode: SCopy P1 P2 * * *
1053**
drh9cbf3422008-01-17 16:22:13 +00001054** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001055**
1056** This instruction makes a shallow copy of the value. If the value
1057** is a string or blob, then the copy is only a pointer to the
1058** original and hence if the original changes so will the copy.
1059** Worse, if the original is deallocated, the copy becomes invalid.
1060** Thus the program must guarantee that the original will not change
1061** during the lifetime of the copy. Use OP_Copy to make a complete
1062** copy.
1063*/
drh93952eb2009-11-13 19:43:43 +00001064case OP_SCopy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001065 pIn1 = &aMem[pOp->p1];
1066 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001067 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001068 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001069#ifdef SQLITE_DEBUG
1070 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1071#endif
drh5b6afba2008-01-05 16:29:28 +00001072 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001073 break;
1074}
drh75897232000-05-29 14:26:00 +00001075
drh9cbf3422008-01-17 16:22:13 +00001076/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001077**
shane21e7feb2008-05-30 15:59:49 +00001078** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001079** results. This opcode causes the sqlite3_step() call to terminate
1080** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1081** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001082** row.
drhd4e70eb2008-01-02 00:34:36 +00001083*/
drh9cbf3422008-01-17 16:22:13 +00001084case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001085 Mem *pMem;
1086 int i;
1087 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001088 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001089 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001090
dan32b09f22009-09-23 17:29:59 +00001091 /* If this statement has violated immediate foreign key constraints, do
1092 ** not return the number of rows modified. And do not RELEASE the statement
1093 ** transaction. It needs to be rolled back. */
1094 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1095 assert( db->flags&SQLITE_CountRows );
1096 assert( p->usesStmtJournal );
1097 break;
1098 }
1099
danielk1977bd434552009-03-18 10:33:00 +00001100 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1101 ** DML statements invoke this opcode to return the number of rows
1102 ** modified to the user. This is the only way that a VM that
1103 ** opens a statement transaction may invoke this opcode.
1104 **
1105 ** In case this is such a statement, close any statement transaction
1106 ** opened by this VM before returning control to the user. This is to
1107 ** ensure that statement-transactions are always nested, not overlapping.
1108 ** If the open statement-transaction is not closed here, then the user
1109 ** may step another VM that opens its own statement transaction. This
1110 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001111 **
1112 ** The statement transaction is never a top-level transaction. Hence
1113 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001114 */
1115 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001116 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1117 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001118 break;
1119 }
1120
drhd4e70eb2008-01-02 00:34:36 +00001121 /* Invalidate all ephemeral cursor row caches */
1122 p->cacheCtr = (p->cacheCtr + 2)|1;
1123
1124 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001125 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001126 ** as side effect.
1127 */
drha6c2ed92009-11-14 23:22:23 +00001128 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001129 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001130 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001131 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001132 assert( (pMem[i].flags & MEM_Ephem)==0
1133 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001134 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001135 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001136 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001137 }
drh28039692008-03-17 16:54:01 +00001138 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001139
1140 /* Return SQLITE_ROW
1141 */
drhd4e70eb2008-01-02 00:34:36 +00001142 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001143 rc = SQLITE_ROW;
1144 goto vdbe_return;
1145}
1146
drh5b6afba2008-01-05 16:29:28 +00001147/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001148**
drh5b6afba2008-01-05 16:29:28 +00001149** Add the text in register P1 onto the end of the text in
1150** register P2 and store the result in register P3.
1151** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001152**
1153** P3 = P2 || P1
1154**
1155** It is illegal for P1 and P3 to be the same register. Sometimes,
1156** if P3 is the same register as P2, the implementation is able
1157** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001158*/
drh5b6afba2008-01-05 16:29:28 +00001159case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001160 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001161
drh3c657212009-11-17 23:59:58 +00001162 pIn1 = &aMem[pOp->p1];
1163 pIn2 = &aMem[pOp->p2];
1164 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001165 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001166 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001167 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001168 break;
drh5e00f6c2001-09-13 13:46:56 +00001169 }
drha0c06522009-06-17 22:50:41 +00001170 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001171 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001172 Stringify(pIn2, encoding);
1173 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001174 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001175 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001176 }
danielk1977a7a8e142008-02-13 18:25:27 +00001177 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001178 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001179 goto no_mem;
1180 }
danielk1977a7a8e142008-02-13 18:25:27 +00001181 if( pOut!=pIn2 ){
1182 memcpy(pOut->z, pIn2->z, pIn2->n);
1183 }
1184 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1185 pOut->z[nByte] = 0;
1186 pOut->z[nByte+1] = 0;
1187 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001188 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001189 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001190 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001191 break;
1192}
drh75897232000-05-29 14:26:00 +00001193
drh3c84ddf2008-01-09 02:15:38 +00001194/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001195**
drh60a713c2008-01-21 16:22:45 +00001196** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001197** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001198** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001199*/
drh3c84ddf2008-01-09 02:15:38 +00001200/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001201**
drh3c84ddf2008-01-09 02:15:38 +00001202**
shane21e7feb2008-05-30 15:59:49 +00001203** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001204** and store the result in register P3.
1205** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001206*/
drh3c84ddf2008-01-09 02:15:38 +00001207/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001208**
drh60a713c2008-01-21 16:22:45 +00001209** Subtract the value in register P1 from the value in register P2
1210** and store the result in register P3.
1211** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001212*/
drh9cbf3422008-01-17 16:22:13 +00001213/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001214**
drh60a713c2008-01-21 16:22:45 +00001215** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001216** and store the result in register P3 (P3=P2/P1). If the value in
1217** register P1 is zero, then the result is NULL. If either input is
1218** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001219*/
drh9cbf3422008-01-17 16:22:13 +00001220/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001221**
drh3c84ddf2008-01-09 02:15:38 +00001222** Compute the remainder after integer division of the value in
1223** register P1 by the value in register P2 and store the result in P3.
1224** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001225** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001226*/
drh5b6afba2008-01-05 16:29:28 +00001227case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1228case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1229case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1230case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1231case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001232 int flags; /* Combined MEM_* flags from both inputs */
1233 i64 iA; /* Integer value of left operand */
1234 i64 iB; /* Integer value of right operand */
1235 double rA; /* Real value of left operand */
1236 double rB; /* Real value of right operand */
1237
drh3c657212009-11-17 23:59:58 +00001238 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001239 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001240 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001241 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001242 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001243 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001244 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1245 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001246 iA = pIn1->u.i;
1247 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001248 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001249 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1250 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1251 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001252 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001253 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001254 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001255 iB /= iA;
drh75897232000-05-29 14:26:00 +00001256 break;
1257 }
drhbf4133c2001-10-13 02:59:08 +00001258 default: {
drh856c1032009-06-02 15:21:42 +00001259 if( iA==0 ) goto arithmetic_result_is_null;
1260 if( iA==-1 ) iA = 1;
1261 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001262 break;
1263 }
drh75897232000-05-29 14:26:00 +00001264 }
drh856c1032009-06-02 15:21:42 +00001265 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001266 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001267 }else{
drh158b9cb2011-03-05 20:59:46 +00001268fp_math:
drh856c1032009-06-02 15:21:42 +00001269 rA = sqlite3VdbeRealValue(pIn1);
1270 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001271 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001272 case OP_Add: rB += rA; break;
1273 case OP_Subtract: rB -= rA; break;
1274 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001275 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001276 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001277 if( rA==(double)0 ) goto arithmetic_result_is_null;
1278 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001279 break;
1280 }
drhbf4133c2001-10-13 02:59:08 +00001281 default: {
shane75ac1de2009-06-09 18:58:52 +00001282 iA = (i64)rA;
1283 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001284 if( iA==0 ) goto arithmetic_result_is_null;
1285 if( iA==-1 ) iA = 1;
1286 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001287 break;
1288 }
drh5e00f6c2001-09-13 13:46:56 +00001289 }
drhc5a7b512010-01-13 16:25:42 +00001290#ifdef SQLITE_OMIT_FLOATING_POINT
1291 pOut->u.i = rB;
1292 MemSetTypeFlag(pOut, MEM_Int);
1293#else
drh856c1032009-06-02 15:21:42 +00001294 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001295 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001296 }
drh856c1032009-06-02 15:21:42 +00001297 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001298 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001299 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001300 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001301 }
drhc5a7b512010-01-13 16:25:42 +00001302#endif
drh5e00f6c2001-09-13 13:46:56 +00001303 }
1304 break;
1305
drha05a7222008-01-19 03:35:58 +00001306arithmetic_result_is_null:
1307 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001308 break;
1309}
1310
drh66a51672008-01-03 00:01:23 +00001311/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001312**
drh66a51672008-01-03 00:01:23 +00001313** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001314** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1315** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001316** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001317**
1318** The interface used by the implementation of the aforementioned functions
1319** to retrieve the collation sequence set by this opcode is not available
1320** publicly, only to user functions defined in func.c.
1321*/
drh9cbf3422008-01-17 16:22:13 +00001322case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001323 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001324 break;
1325}
1326
drh98757152008-01-09 23:04:12 +00001327/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001328**
drh66a51672008-01-03 00:01:23 +00001329** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001330** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001331** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001332** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001333**
drh13449892005-09-07 21:22:45 +00001334** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001335** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001336** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001337** whether meta data associated with a user function argument using the
1338** sqlite3_set_auxdata() API may be safely retained until the next
1339** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001340**
drh13449892005-09-07 21:22:45 +00001341** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001342*/
drh0bce8352002-02-28 00:41:10 +00001343case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001344 int i;
drh6810ce62004-01-31 19:22:56 +00001345 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001346 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001347 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001348 int n;
drh1350b032002-02-27 19:00:20 +00001349
drh856c1032009-06-02 15:21:42 +00001350 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001351 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001352 assert( apVal || n==0 );
drhebc16712010-09-28 00:25:58 +00001353 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1354 pOut = &aMem[pOp->p3];
1355 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001356
danielk19776ab3a2e2009-02-19 14:39:25 +00001357 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001358 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001359 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001360 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001361 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001362 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001363 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001364 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001365 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001366 }
danielk197751ad0ec2004-05-24 12:39:02 +00001367
drh66a51672008-01-03 00:01:23 +00001368 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1369 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001370 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001371 ctx.pVdbeFunc = 0;
1372 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001373 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001374 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1375 }
1376
drh00706be2004-01-30 14:49:16 +00001377 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001378 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001379 ctx.s.xDel = 0;
1380 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001381
1382 /* The output cell may already have a buffer allocated. Move
1383 ** the pointer to ctx.s so in case the user-function can use
1384 ** the already allocated buffer instead of allocating a new one.
1385 */
1386 sqlite3VdbeMemMove(&ctx.s, pOut);
1387 MemSetTypeFlag(&ctx.s, MEM_Null);
1388
drh8e0a2f92002-02-23 23:45:45 +00001389 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001390 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001391 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001392 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001393 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001394 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001395 }
drhee9ff672010-09-03 18:50:48 +00001396 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh17435752007-08-16 04:30:38 +00001397 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001398 /* Even though a malloc() has failed, the implementation of the
1399 ** user function may have called an sqlite3_result_XXX() function
1400 ** to return a value. The following call releases any resources
1401 ** associated with such a value.
danielk1977e0fc5262007-07-26 06:50:05 +00001402 */
1403 sqlite3VdbeMemRelease(&ctx.s);
1404 goto no_mem;
1405 }
danielk19777e18c252004-05-25 11:47:24 +00001406
shane21e7feb2008-05-30 15:59:49 +00001407 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001408 ** immediately call the destructor for any non-static values.
1409 */
1410 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001411 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001412 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001413 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001414 }
1415
drh90669c12006-01-20 15:45:36 +00001416 /* If the function returned an error, throw an exception */
1417 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001418 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001419 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001420 }
1421
drh9cbf3422008-01-17 16:22:13 +00001422 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001423 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001424 sqlite3VdbeMemMove(pOut, &ctx.s);
1425 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001426 goto too_big;
1427 }
drh2dcef112008-01-12 19:03:48 +00001428 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001429 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001430 break;
1431}
1432
drh98757152008-01-09 23:04:12 +00001433/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001434**
drh98757152008-01-09 23:04:12 +00001435** Take the bit-wise AND of the values in register P1 and P2 and
1436** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001437** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001438*/
drh98757152008-01-09 23:04:12 +00001439/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001440**
drh98757152008-01-09 23:04:12 +00001441** Take the bit-wise OR of the values in register P1 and P2 and
1442** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001443** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001444*/
drh98757152008-01-09 23:04:12 +00001445/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001446**
drh98757152008-01-09 23:04:12 +00001447** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001448** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001449** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001450** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001451*/
drh98757152008-01-09 23:04:12 +00001452/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001453**
drh98757152008-01-09 23:04:12 +00001454** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001455** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001456** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001457** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001458*/
drh5b6afba2008-01-05 16:29:28 +00001459case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1460case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1461case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1462case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001463 i64 iA;
1464 u64 uA;
1465 i64 iB;
1466 u8 op;
drh6810ce62004-01-31 19:22:56 +00001467
drh3c657212009-11-17 23:59:58 +00001468 pIn1 = &aMem[pOp->p1];
1469 pIn2 = &aMem[pOp->p2];
1470 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001471 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001472 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001473 break;
1474 }
drh158b9cb2011-03-05 20:59:46 +00001475 iA = sqlite3VdbeIntValue(pIn2);
1476 iB = sqlite3VdbeIntValue(pIn1);
1477 op = pOp->opcode;
1478 if( op==OP_BitAnd ){
1479 iA &= iB;
1480 }else if( op==OP_BitOr ){
1481 iA |= iB;
1482 }else if( iB!=0 ){
1483 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1484
1485 /* If shifting by a negative amount, shift in the other direction */
1486 if( iB<0 ){
1487 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1488 op = 2*OP_ShiftLeft + 1 - op;
1489 iB = iB>(-64) ? -iB : 64;
1490 }
1491
1492 if( iB>=64 ){
1493 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1494 }else{
1495 memcpy(&uA, &iA, sizeof(uA));
1496 if( op==OP_ShiftLeft ){
1497 uA <<= iB;
1498 }else{
1499 uA >>= iB;
1500 /* Sign-extend on a right shift of a negative number */
1501 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1502 }
1503 memcpy(&iA, &uA, sizeof(iA));
1504 }
drhbf4133c2001-10-13 02:59:08 +00001505 }
drh158b9cb2011-03-05 20:59:46 +00001506 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001507 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001508 break;
1509}
1510
drh8558cde2008-01-05 05:20:10 +00001511/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001512**
danielk19770cdc0222008-06-26 18:04:03 +00001513** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001514** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001515**
drh8558cde2008-01-05 05:20:10 +00001516** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001517*/
drh9cbf3422008-01-17 16:22:13 +00001518case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001519 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001520 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001521 sqlite3VdbeMemIntegerify(pIn1);
1522 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001523 break;
1524}
1525
drh9cbf3422008-01-17 16:22:13 +00001526/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001527**
drh9cbf3422008-01-17 16:22:13 +00001528** Force the value in register P1 to be an integer. If the value
1529** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001530** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001531** raise an SQLITE_MISMATCH exception.
1532*/
drh9cbf3422008-01-17 16:22:13 +00001533case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001534 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001535 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1536 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001537 if( pOp->p2==0 ){
1538 rc = SQLITE_MISMATCH;
1539 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001540 }else{
drh17c40292004-07-21 02:53:29 +00001541 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001542 }
drh8aff1012001-12-22 14:49:24 +00001543 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001544 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001545 }
1546 break;
1547}
1548
drh13573c72010-01-12 17:04:07 +00001549#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001550/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001551**
drh2133d822008-01-03 18:44:59 +00001552** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001553**
drh8a512562005-11-14 22:29:05 +00001554** This opcode is used when extracting information from a column that
1555** has REAL affinity. Such column values may still be stored as
1556** integers, for space efficiency, but after extraction we want them
1557** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001558*/
drh9cbf3422008-01-17 16:22:13 +00001559case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001560 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001561 if( pIn1->flags & MEM_Int ){
1562 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001563 }
drh487e2622005-06-25 18:42:14 +00001564 break;
1565}
drh13573c72010-01-12 17:04:07 +00001566#endif
drh487e2622005-06-25 18:42:14 +00001567
drh8df447f2005-11-01 15:48:24 +00001568#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001569/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001570**
drh8558cde2008-01-05 05:20:10 +00001571** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001572** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001573** equivalent of printf(). Blob values are unchanged and
1574** are afterwards simply interpreted as text.
1575**
1576** A NULL value is not changed by this routine. It remains NULL.
1577*/
drh9cbf3422008-01-17 16:22:13 +00001578case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001579 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001580 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001581 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001582 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001583 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1584 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1585 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001586 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001587 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001588 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001589 break;
1590}
1591
drh8558cde2008-01-05 05:20:10 +00001592/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001593**
drh8558cde2008-01-05 05:20:10 +00001594** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001595** If the value is numeric, convert it to a string first.
1596** Strings are simply reinterpreted as blobs with no change
1597** to the underlying data.
1598**
1599** A NULL value is not changed by this routine. It remains NULL.
1600*/
drh9cbf3422008-01-17 16:22:13 +00001601case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001602 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001603 if( pIn1->flags & MEM_Null ) break;
1604 if( (pIn1->flags & MEM_Blob)==0 ){
1605 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001606 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001607 MemSetTypeFlag(pIn1, MEM_Blob);
1608 }else{
1609 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001610 }
drhb7654112008-01-12 12:48:07 +00001611 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001612 break;
1613}
drh8a512562005-11-14 22:29:05 +00001614
drh8558cde2008-01-05 05:20:10 +00001615/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001616**
drh8558cde2008-01-05 05:20:10 +00001617** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001618** integer or a floating-point number.)
1619** If the value is text or blob, try to convert it to an using the
1620** equivalent of atoi() or atof() and store 0 if no such conversion
1621** is possible.
1622**
1623** A NULL value is not changed by this routine. It remains NULL.
1624*/
drh9cbf3422008-01-17 16:22:13 +00001625case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001626 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001627 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001628 break;
1629}
1630#endif /* SQLITE_OMIT_CAST */
1631
drh8558cde2008-01-05 05:20:10 +00001632/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001633**
drh710c4842010-08-30 01:17:20 +00001634** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001635** The value is currently a real number, drop its fractional part.
1636** If the value is text or blob, try to convert it to an integer using the
1637** equivalent of atoi() and store 0 if no such conversion is possible.
1638**
1639** A NULL value is not changed by this routine. It remains NULL.
1640*/
drh9cbf3422008-01-17 16:22:13 +00001641case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001642 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001643 if( (pIn1->flags & MEM_Null)==0 ){
1644 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001645 }
1646 break;
1647}
1648
drh13573c72010-01-12 17:04:07 +00001649#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001650/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001651**
drh8558cde2008-01-05 05:20:10 +00001652** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001653** If The value is currently an integer, convert it.
1654** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001655** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001656**
1657** A NULL value is not changed by this routine. It remains NULL.
1658*/
drh9cbf3422008-01-17 16:22:13 +00001659case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001660 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001661 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001662 if( (pIn1->flags & MEM_Null)==0 ){
1663 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001664 }
1665 break;
1666}
drh13573c72010-01-12 17:04:07 +00001667#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001668
drh35573352008-01-08 23:54:25 +00001669/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001670**
drh35573352008-01-08 23:54:25 +00001671** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1672** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001673**
drh35573352008-01-08 23:54:25 +00001674** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1675** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001676** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001677**
drh35573352008-01-08 23:54:25 +00001678** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001679** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001680** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001681** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001682** affinity is used. Note that the affinity conversions are stored
1683** back into the input registers P1 and P3. So this opcode can cause
1684** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001685**
1686** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001687** the values are compared. If both values are blobs then memcmp() is
1688** used to determine the results of the comparison. If both values
1689** are text, then the appropriate collating function specified in
1690** P4 is used to do the comparison. If P4 is not specified then
1691** memcmp() is used to compare text string. If both values are
1692** numeric, then a numeric comparison is used. If the two values
1693** are of different types, then numbers are considered less than
1694** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001695**
drh35573352008-01-08 23:54:25 +00001696** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1697** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001698*/
drh9cbf3422008-01-17 16:22:13 +00001699/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001700**
drh35573352008-01-08 23:54:25 +00001701** This works just like the Lt opcode except that the jump is taken if
1702** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001703** additional information.
drh6a2fe092009-09-23 02:29:36 +00001704**
1705** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1706** true or false and is never NULL. If both operands are NULL then the result
1707** of comparison is false. If either operand is NULL then the result is true.
1708** If neither operand is NULL the the result is the same as it would be if
1709** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001710*/
drh9cbf3422008-01-17 16:22:13 +00001711/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001712**
drh35573352008-01-08 23:54:25 +00001713** This works just like the Lt opcode except that the jump is taken if
1714** the operands in registers P1 and P3 are equal.
1715** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001716**
1717** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1718** true or false and is never NULL. If both operands are NULL then the result
1719** of comparison is true. If either operand is NULL then the result is false.
1720** If neither operand is NULL the the result is the same as it would be if
1721** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001722*/
drh9cbf3422008-01-17 16:22:13 +00001723/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001724**
drh35573352008-01-08 23:54:25 +00001725** This works just like the Lt opcode except that the jump is taken if
1726** the content of register P3 is less than or equal to the content of
1727** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001728*/
drh9cbf3422008-01-17 16:22:13 +00001729/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001730**
drh35573352008-01-08 23:54:25 +00001731** This works just like the Lt opcode except that the jump is taken if
1732** the content of register P3 is greater than the content of
1733** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001734*/
drh9cbf3422008-01-17 16:22:13 +00001735/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001736**
drh35573352008-01-08 23:54:25 +00001737** This works just like the Lt opcode except that the jump is taken if
1738** the content of register P3 is greater than or equal to the content of
1739** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001740*/
drh9cbf3422008-01-17 16:22:13 +00001741case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1742case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1743case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1744case OP_Le: /* same as TK_LE, jump, in1, in3 */
1745case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1746case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001747 int res; /* Result of the comparison of pIn1 against pIn3 */
1748 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001749 u16 flags1; /* Copy of initial value of pIn1->flags */
1750 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001751
drh3c657212009-11-17 23:59:58 +00001752 pIn1 = &aMem[pOp->p1];
1753 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001754 flags1 = pIn1->flags;
1755 flags3 = pIn3->flags;
drh6a2fe092009-09-23 02:29:36 +00001756 if( (pIn1->flags | pIn3->flags)&MEM_Null ){
1757 /* One or both operands are NULL */
1758 if( pOp->p5 & SQLITE_NULLEQ ){
1759 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1760 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1761 ** or not both operands are null.
1762 */
1763 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
1764 res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
1765 }else{
1766 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1767 ** then the result is always NULL.
1768 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1769 */
1770 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001771 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001772 MemSetTypeFlag(pOut, MEM_Null);
1773 REGISTER_TRACE(pOp->p2, pOut);
1774 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1775 pc = pOp->p2-1;
1776 }
1777 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001778 }
drh6a2fe092009-09-23 02:29:36 +00001779 }else{
1780 /* Neither operand is NULL. Do a comparison. */
1781 affinity = pOp->p5 & SQLITE_AFF_MASK;
1782 if( affinity ){
1783 applyAffinity(pIn1, affinity, encoding);
1784 applyAffinity(pIn3, affinity, encoding);
1785 if( db->mallocFailed ) goto no_mem;
1786 }
danielk1977a37cdde2004-05-16 11:15:36 +00001787
drh6a2fe092009-09-23 02:29:36 +00001788 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1789 ExpandBlob(pIn1);
1790 ExpandBlob(pIn3);
1791 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001792 }
danielk1977a37cdde2004-05-16 11:15:36 +00001793 switch( pOp->opcode ){
1794 case OP_Eq: res = res==0; break;
1795 case OP_Ne: res = res!=0; break;
1796 case OP_Lt: res = res<0; break;
1797 case OP_Le: res = res<=0; break;
1798 case OP_Gt: res = res>0; break;
1799 default: res = res>=0; break;
1800 }
1801
drh35573352008-01-08 23:54:25 +00001802 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001803 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001804 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001805 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001806 pOut->u.i = res;
1807 REGISTER_TRACE(pOp->p2, pOut);
1808 }else if( res ){
1809 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001810 }
danb7dca7d2010-03-05 16:32:12 +00001811
1812 /* Undo any changes made by applyAffinity() to the input registers. */
1813 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1814 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001815 break;
1816}
drhc9b84a12002-06-20 11:36:48 +00001817
drh0acb7e42008-06-25 00:12:41 +00001818/* Opcode: Permutation * * * P4 *
1819**
shanebe217792009-03-05 04:20:31 +00001820** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001821** of integers in P4.
1822**
1823** The permutation is only valid until the next OP_Permutation, OP_Compare,
1824** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1825** immediately prior to the OP_Compare.
1826*/
1827case OP_Permutation: {
1828 assert( pOp->p4type==P4_INTARRAY );
1829 assert( pOp->p4.ai );
1830 aPermute = pOp->p4.ai;
1831 break;
1832}
1833
drh16ee60f2008-06-20 18:13:25 +00001834/* Opcode: Compare P1 P2 P3 P4 *
1835**
drh710c4842010-08-30 01:17:20 +00001836** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1837** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001838** the comparison for use by the next OP_Jump instruct.
1839**
drh0acb7e42008-06-25 00:12:41 +00001840** P4 is a KeyInfo structure that defines collating sequences and sort
1841** orders for the comparison. The permutation applies to registers
1842** only. The KeyInfo elements are used sequentially.
1843**
1844** The comparison is a sort comparison, so NULLs compare equal,
1845** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001846** and strings are less than blobs.
1847*/
1848case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001849 int n;
1850 int i;
1851 int p1;
1852 int p2;
1853 const KeyInfo *pKeyInfo;
1854 int idx;
1855 CollSeq *pColl; /* Collating sequence to use on this term */
1856 int bRev; /* True for DESCENDING sort order */
1857
1858 n = pOp->p3;
1859 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001860 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001861 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001862 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001863 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00001864#if SQLITE_DEBUG
1865 if( aPermute ){
1866 int k, mx = 0;
1867 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
1868 assert( p1>0 && p1+mx<=p->nMem+1 );
1869 assert( p2>0 && p2+mx<=p->nMem+1 );
1870 }else{
1871 assert( p1>0 && p1+n<=p->nMem+1 );
1872 assert( p2>0 && p2+n<=p->nMem+1 );
1873 }
1874#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00001875 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001876 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00001877 assert( memIsValid(&aMem[p1+idx]) );
1878 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00001879 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
1880 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001881 assert( i<pKeyInfo->nField );
1882 pColl = pKeyInfo->aColl[i];
1883 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00001884 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00001885 if( iCompare ){
1886 if( bRev ) iCompare = -iCompare;
1887 break;
1888 }
drh16ee60f2008-06-20 18:13:25 +00001889 }
drh0acb7e42008-06-25 00:12:41 +00001890 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001891 break;
1892}
1893
1894/* Opcode: Jump P1 P2 P3 * *
1895**
1896** Jump to the instruction at address P1, P2, or P3 depending on whether
1897** in the most recent OP_Compare instruction the P1 vector was less than
1898** equal to, or greater than the P2 vector, respectively.
1899*/
drh0acb7e42008-06-25 00:12:41 +00001900case OP_Jump: { /* jump */
1901 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001902 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001903 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001904 pc = pOp->p2 - 1;
1905 }else{
1906 pc = pOp->p3 - 1;
1907 }
1908 break;
1909}
1910
drh5b6afba2008-01-05 16:29:28 +00001911/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001912**
drh5b6afba2008-01-05 16:29:28 +00001913** Take the logical AND of the values in registers P1 and P2 and
1914** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001915**
drh5b6afba2008-01-05 16:29:28 +00001916** If either P1 or P2 is 0 (false) then the result is 0 even if
1917** the other input is NULL. A NULL and true or two NULLs give
1918** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001919*/
drh5b6afba2008-01-05 16:29:28 +00001920/* Opcode: Or P1 P2 P3 * *
1921**
1922** Take the logical OR of the values in register P1 and P2 and
1923** store the answer in register P3.
1924**
1925** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1926** even if the other input is NULL. A NULL and false or two NULLs
1927** give a NULL output.
1928*/
1929case OP_And: /* same as TK_AND, in1, in2, out3 */
1930case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001931 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
1932 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001933
drh3c657212009-11-17 23:59:58 +00001934 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00001935 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001936 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001937 }else{
drh5b6afba2008-01-05 16:29:28 +00001938 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001939 }
drh3c657212009-11-17 23:59:58 +00001940 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00001941 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001942 v2 = 2;
1943 }else{
drh5b6afba2008-01-05 16:29:28 +00001944 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001945 }
1946 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001947 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001948 v1 = and_logic[v1*3+v2];
1949 }else{
drh5b6afba2008-01-05 16:29:28 +00001950 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001951 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001952 }
drh3c657212009-11-17 23:59:58 +00001953 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00001954 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001955 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001956 }else{
drh5b6afba2008-01-05 16:29:28 +00001957 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001958 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001959 }
drh5e00f6c2001-09-13 13:46:56 +00001960 break;
1961}
1962
drhe99fa2a2008-12-15 15:27:51 +00001963/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001964**
drhe99fa2a2008-12-15 15:27:51 +00001965** Interpret the value in register P1 as a boolean value. Store the
1966** boolean complement in register P2. If the value in register P1 is
1967** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001968*/
drh93952eb2009-11-13 19:43:43 +00001969case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00001970 pIn1 = &aMem[pOp->p1];
1971 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00001972 if( pIn1->flags & MEM_Null ){
1973 sqlite3VdbeMemSetNull(pOut);
1974 }else{
1975 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1976 }
drh5e00f6c2001-09-13 13:46:56 +00001977 break;
1978}
1979
drhe99fa2a2008-12-15 15:27:51 +00001980/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00001981**
drhe99fa2a2008-12-15 15:27:51 +00001982** Interpret the content of register P1 as an integer. Store the
1983** ones-complement of the P1 value into register P2. If P1 holds
1984** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00001985*/
drh93952eb2009-11-13 19:43:43 +00001986case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00001987 pIn1 = &aMem[pOp->p1];
1988 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00001989 if( pIn1->flags & MEM_Null ){
1990 sqlite3VdbeMemSetNull(pOut);
1991 }else{
1992 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1993 }
drhbf4133c2001-10-13 02:59:08 +00001994 break;
1995}
1996
drh3c84ddf2008-01-09 02:15:38 +00001997/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001998**
drh3c84ddf2008-01-09 02:15:38 +00001999** Jump to P2 if the value in register P1 is true. The value is
2000** is considered true if it is numeric and non-zero. If the value
2001** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00002002*/
drh3c84ddf2008-01-09 02:15:38 +00002003/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002004**
drh3c84ddf2008-01-09 02:15:38 +00002005** Jump to P2 if the value in register P1 is False. The value is
2006** is considered true if it has a numeric value of zero. If the value
2007** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00002008*/
drh9cbf3422008-01-17 16:22:13 +00002009case OP_If: /* jump, in1 */
2010case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002011 int c;
drh3c657212009-11-17 23:59:58 +00002012 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002013 if( pIn1->flags & MEM_Null ){
2014 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002015 }else{
drhba0232a2005-06-06 17:27:19 +00002016#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002017 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002018#else
drh3c84ddf2008-01-09 02:15:38 +00002019 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002020#endif
drhf5905aa2002-05-26 20:54:33 +00002021 if( pOp->opcode==OP_IfNot ) c = !c;
2022 }
drh3c84ddf2008-01-09 02:15:38 +00002023 if( c ){
2024 pc = pOp->p2-1;
2025 }
drh5e00f6c2001-09-13 13:46:56 +00002026 break;
2027}
2028
drh830ecf92009-06-18 00:41:55 +00002029/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00002030**
drh830ecf92009-06-18 00:41:55 +00002031** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002032*/
drh9cbf3422008-01-17 16:22:13 +00002033case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002034 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002035 if( (pIn1->flags & MEM_Null)!=0 ){
2036 pc = pOp->p2 - 1;
2037 }
drh477df4b2008-01-05 18:48:24 +00002038 break;
2039}
2040
drh98757152008-01-09 23:04:12 +00002041/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002042**
drh6a288a32008-01-07 19:20:24 +00002043** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002044*/
drh9cbf3422008-01-17 16:22:13 +00002045case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002046 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002047 if( (pIn1->flags & MEM_Null)==0 ){
2048 pc = pOp->p2 - 1;
2049 }
drh5e00f6c2001-09-13 13:46:56 +00002050 break;
2051}
2052
drh3e9ca092009-09-08 01:14:48 +00002053/* Opcode: Column P1 P2 P3 P4 P5
danielk1977192ac1d2004-05-10 07:17:30 +00002054**
danielk1977cfcdaef2004-05-12 07:33:33 +00002055** Interpret the data that cursor P1 points to as a structure built using
2056** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002057** information about the format of the data.) Extract the P2-th column
2058** from this record. If there are less that (P2+1)
2059** values in the record, extract a NULL.
2060**
drh9cbf3422008-01-17 16:22:13 +00002061** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002062**
danielk19771f4aa332008-01-03 09:51:55 +00002063** If the column contains fewer than P2 fields, then extract a NULL. Or,
2064** if the P4 argument is a P4_MEM use the value of the P4 argument as
2065** the result.
drh3e9ca092009-09-08 01:14:48 +00002066**
2067** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2068** then the cache of the cursor is reset prior to extracting the column.
2069** The first OP_Column against a pseudo-table after the value of the content
2070** register has changed should have this bit set.
danielk1977192ac1d2004-05-10 07:17:30 +00002071*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002072case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002073 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002074 i64 payloadSize64; /* Number of bytes in the record */
2075 int p1; /* P1 value of the opcode */
2076 int p2; /* column number to retrieve */
2077 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002078 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002079 BtCursor *pCrsr; /* The BTree cursor */
2080 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2081 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002082 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002083 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002084 int i; /* Loop counter */
2085 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002086 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002087 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002088 u8 *zIdx; /* Index into header */
2089 u8 *zEndHdr; /* Pointer to first byte after the header */
2090 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002091 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002092 int szHdr; /* Size of the header size field at start of record */
2093 int avail; /* Number of bytes of available data */
drh3e9ca092009-09-08 01:14:48 +00002094 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002095
drh856c1032009-06-02 15:21:42 +00002096
2097 p1 = pOp->p1;
2098 p2 = pOp->p2;
2099 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002100 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002101 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002102 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002103 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002104 memAboutToChange(p, pDest);
danielk1977a7a8e142008-02-13 18:25:27 +00002105 MemSetTypeFlag(pDest, MEM_Null);
shane36840fd2009-06-26 16:32:13 +00002106 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002107
drhe61cffc2004-06-12 18:12:15 +00002108 /* This block sets the variable payloadSize to be the total number of
2109 ** bytes in the record.
2110 **
2111 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002112 ** The complete record text is always available for pseudo-tables
2113 ** If the record is stored in a cursor, the complete record text
2114 ** might be available in the pC->aRow cache. Or it might not be.
2115 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002116 **
2117 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002118 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002119 */
drhb73857f2006-03-17 00:25:59 +00002120 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00002121 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002122#ifndef SQLITE_OMIT_VIRTUALTABLE
2123 assert( pC->pVtabCursor==0 );
2124#endif
shane36840fd2009-06-26 16:32:13 +00002125 pCrsr = pC->pCursor;
2126 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002127 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002128 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002129 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002130 if( pC->nullRow ){
2131 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002132 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002133 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002134 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002135 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002136 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhc27ae612009-07-14 18:35:44 +00002137 rc = sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2138 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002139 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2140 ** payload size, so it is impossible for payloadSize64 to be
2141 ** larger than 32 bits. */
2142 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002143 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002144 }else{
drhea8ffdf2009-07-22 00:35:23 +00002145 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhc27ae612009-07-14 18:35:44 +00002146 rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002147 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002148 }
drh3e9ca092009-09-08 01:14:48 +00002149 }else if( pC->pseudoTableReg>0 ){
drha6c2ed92009-11-14 23:22:23 +00002150 pReg = &aMem[pC->pseudoTableReg];
drh3e9ca092009-09-08 01:14:48 +00002151 assert( pReg->flags & MEM_Blob );
drh2b4ded92010-09-27 21:09:31 +00002152 assert( memIsValid(pReg) );
drh3e9ca092009-09-08 01:14:48 +00002153 payloadSize = pReg->n;
2154 zRec = pReg->z;
2155 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002156 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002157 }else{
2158 /* Consider the row to be NULL */
2159 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002160 }
2161
drh9cbf3422008-01-17 16:22:13 +00002162 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002163 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002164 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002165 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002166 }
drh35cd6432009-06-05 14:17:21 +00002167 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2168 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002169 goto too_big;
2170 }
danielk1977192ac1d2004-05-10 07:17:30 +00002171
shane36840fd2009-06-26 16:32:13 +00002172 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002173 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002174
drh9188b382004-05-14 21:12:22 +00002175 /* Read and parse the table header. Store the results of the parse
2176 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002177 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002178 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002179 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002180 aOffset = pC->aOffset;
2181 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002182 assert(aType);
drh856c1032009-06-02 15:21:42 +00002183 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002184 pC->aOffset = aOffset = &aType[nField];
2185 pC->payloadSize = payloadSize;
2186 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002187
drhd3194f52004-05-27 19:59:32 +00002188 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002189 if( zRec ){
2190 zData = zRec;
2191 }else{
drhf0863fe2005-06-12 21:35:51 +00002192 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002193 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002194 }else{
drhe51c44f2004-05-30 20:46:09 +00002195 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002196 }
drhe61cffc2004-06-12 18:12:15 +00002197 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2198 ** save the payload in the pC->aRow cache. That will save us from
2199 ** having to make additional calls to fetch the content portion of
2200 ** the record.
2201 */
drh35cd6432009-06-05 14:17:21 +00002202 assert( avail>=0 );
2203 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002204 zRec = zData;
2205 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002206 }else{
2207 pC->aRow = 0;
2208 }
drhd3194f52004-05-27 19:59:32 +00002209 }
drh588f5bc2007-01-02 18:41:54 +00002210 /* The following assert is true in all cases accept when
2211 ** the database file has been corrupted externally.
2212 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002213 szHdr = getVarint32((u8*)zData, offset);
2214
2215 /* Make sure a corrupt database has not given us an oversize header.
2216 ** Do this now to avoid an oversize memory allocation.
2217 **
2218 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2219 ** types use so much data space that there can only be 4096 and 32 of
2220 ** them, respectively. So the maximum header length results from a
2221 ** 3-byte type for each of the maximum of 32768 columns plus three
2222 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2223 */
2224 if( offset > 98307 ){
2225 rc = SQLITE_CORRUPT_BKPT;
2226 goto op_column_out;
2227 }
2228
2229 /* Compute in len the number of bytes of data we need to read in order
2230 ** to get nField type values. offset is an upper bound on this. But
2231 ** nField might be significantly less than the true number of columns
2232 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2233 ** We want to minimize len in order to limit the size of the memory
2234 ** allocation, especially if a corrupt database file has caused offset
2235 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2236 ** still exceed Robson memory allocation limits on some configurations.
2237 ** On systems that cannot tolerate large memory allocations, nField*5+3
2238 ** will likely be much smaller since nField will likely be less than
2239 ** 20 or so. This insures that Robson memory allocation limits are
2240 ** not exceeded even for corrupt database files.
2241 */
2242 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002243 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002244
2245 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2246 ** record header in most cases. But they will fail to get the complete
2247 ** record header if the record header does not fit on a single page
2248 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2249 ** acquire the complete header text.
2250 */
drh35cd6432009-06-05 14:17:21 +00002251 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002252 sMem.flags = 0;
2253 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002254 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002255 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002256 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002257 }
drhb6f54522004-05-20 02:42:16 +00002258 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002259 }
drh35cd6432009-06-05 14:17:21 +00002260 zEndHdr = (u8 *)&zData[len];
2261 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002262
drhd3194f52004-05-27 19:59:32 +00002263 /* Scan the header and use it to fill in the aType[] and aOffset[]
2264 ** arrays. aType[i] will contain the type integer for the i-th
2265 ** column and aOffset[i] will contain the offset from the beginning
2266 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002267 */
danielk1977dedf45b2006-01-13 17:12:01 +00002268 for(i=0; i<nField; i++){
2269 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002270 aOffset[i] = offset;
shane3f8d5cf2008-04-24 19:15:09 +00002271 zIdx += getVarint32(zIdx, aType[i]);
drh6658cd92010-02-05 14:12:53 +00002272 szField = sqlite3VdbeSerialTypeLen(aType[i]);
2273 offset += szField;
2274 if( offset<szField ){ /* True if offset overflows */
2275 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2276 break;
2277 }
danielk1977dedf45b2006-01-13 17:12:01 +00002278 }else{
2279 /* If i is less that nField, then there are less fields in this
2280 ** record than SetNumColumns indicated there are columns in the
2281 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002282 ** the record to 0. This tells code below to store a NULL
2283 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002284 */
2285 aOffset[i] = 0;
2286 }
drh9188b382004-05-14 21:12:22 +00002287 }
danielk19775f096132008-03-28 15:44:09 +00002288 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002289 sMem.flags = MEM_Null;
2290
danielk19779792eef2006-01-13 15:58:43 +00002291 /* If we have read more header data than was contained in the header,
2292 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002293 ** record, or if the end of the last field appears to be before the end
2294 ** of the record (when all fields present), then we must be dealing
2295 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002296 */
drh6658cd92010-02-05 14:12:53 +00002297 if( (zIdx > zEndHdr) || (offset > payloadSize)
2298 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002299 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002300 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002301 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002302 }
danielk1977192ac1d2004-05-10 07:17:30 +00002303
danielk197736963fd2005-02-19 08:18:05 +00002304 /* Get the column information. If aOffset[p2] is non-zero, then
2305 ** deserialize the value from the record. If aOffset[p2] is zero,
2306 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002307 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002308 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002309 */
danielk197736963fd2005-02-19 08:18:05 +00002310 if( aOffset[p2] ){
2311 assert( rc==SQLITE_OK );
2312 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002313 sqlite3VdbeMemReleaseExternal(pDest);
2314 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002315 }else{
2316 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002317 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002318 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002319 if( rc!=SQLITE_OK ){
2320 goto op_column_out;
2321 }
2322 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002323 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002324 }
drhd4e70eb2008-01-02 00:34:36 +00002325 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002326 }else{
danielk197760585dd2008-01-03 08:08:40 +00002327 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002328 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002329 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002330 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002331 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002332 }
drhfebe1062004-08-28 18:17:48 +00002333
2334 /* If we dynamically allocated space to hold the data (in the
2335 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002336 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002337 ** This prevents a memory copy.
2338 */
danielk19775f096132008-03-28 15:44:09 +00002339 if( sMem.zMalloc ){
2340 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002341 assert( !(pDest->flags & MEM_Dyn) );
2342 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2343 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002344 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002345 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002346 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002347 }
drhfebe1062004-08-28 18:17:48 +00002348
drhd4e70eb2008-01-02 00:34:36 +00002349 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002350
danielk19773c9cc8d2005-01-17 03:40:08 +00002351op_column_out:
drhb7654112008-01-12 12:48:07 +00002352 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002353 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002354 break;
2355}
2356
danielk1977751de562008-04-18 09:01:15 +00002357/* Opcode: Affinity P1 P2 * P4 *
2358**
2359** Apply affinities to a range of P2 registers starting with P1.
2360**
2361** P4 is a string that is P2 characters long. The nth character of the
2362** string indicates the column affinity that should be used for the nth
2363** memory cell in the range.
2364*/
2365case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002366 const char *zAffinity; /* The affinity to be applied */
2367 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002368
drh856c1032009-06-02 15:21:42 +00002369 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002370 assert( zAffinity!=0 );
2371 assert( zAffinity[pOp->p2]==0 );
2372 pIn1 = &aMem[pOp->p1];
2373 while( (cAff = *(zAffinity++))!=0 ){
2374 assert( pIn1 <= &p->aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00002375 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002376 ExpandBlob(pIn1);
2377 applyAffinity(pIn1, cAff, encoding);
2378 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002379 }
2380 break;
2381}
2382
drh1db639c2008-01-17 02:36:28 +00002383/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002384**
drh710c4842010-08-30 01:17:20 +00002385** Convert P2 registers beginning with P1 into the [record format]
2386** use as a data record in a database table or as a key
2387** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002388**
danielk1977751de562008-04-18 09:01:15 +00002389** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002390** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002391** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002392**
drh8a512562005-11-14 22:29:05 +00002393** The mapping from character to affinity is given by the SQLITE_AFF_
2394** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002395**
drh66a51672008-01-03 00:01:23 +00002396** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002397*/
drh1db639c2008-01-17 02:36:28 +00002398case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002399 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2400 Mem *pRec; /* The new record */
2401 u64 nData; /* Number of bytes of data space */
2402 int nHdr; /* Number of bytes of header space */
2403 i64 nByte; /* Data space required for this record */
2404 int nZero; /* Number of zero bytes at the end of the record */
2405 int nVarint; /* Number of bytes in a varint */
2406 u32 serial_type; /* Type field */
2407 Mem *pData0; /* First field to be combined into the record */
2408 Mem *pLast; /* Last field of the record */
2409 int nField; /* Number of fields in the record */
2410 char *zAffinity; /* The affinity string for the record */
2411 int file_format; /* File format to use for encoding */
2412 int i; /* Space used in zNewRecord[] */
2413 int len; /* Length of a field */
2414
drhf3218fe2004-05-28 08:21:02 +00002415 /* Assuming the record contains N fields, the record format looks
2416 ** like this:
2417 **
drh7a224de2004-06-02 01:22:02 +00002418 ** ------------------------------------------------------------------------
2419 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2420 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002421 **
drh9cbf3422008-01-17 16:22:13 +00002422 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2423 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002424 **
2425 ** Each type field is a varint representing the serial type of the
2426 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002427 ** hdr-size field is also a varint which is the offset from the beginning
2428 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002429 */
drh856c1032009-06-02 15:21:42 +00002430 nData = 0; /* Number of bytes of data space */
2431 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002432 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002433 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002434 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002435 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drha6c2ed92009-11-14 23:22:23 +00002436 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002437 nField = pOp->p2;
2438 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002439 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002440
drh2b4ded92010-09-27 21:09:31 +00002441 /* Identify the output register */
2442 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2443 pOut = &aMem[pOp->p3];
2444 memAboutToChange(p, pOut);
2445
drhf3218fe2004-05-28 08:21:02 +00002446 /* Loop through the elements that will make up the record to figure
2447 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002448 */
drha2a49dc2008-01-02 14:28:13 +00002449 for(pRec=pData0; pRec<=pLast; pRec++){
drh2b4ded92010-09-27 21:09:31 +00002450 assert( memIsValid(pRec) );
drhd3d39e92004-05-20 22:16:29 +00002451 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002452 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002453 }
danielk1977d908f5a2007-05-11 07:08:28 +00002454 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002455 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002456 }
drhd946db02005-12-29 19:23:06 +00002457 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002458 len = sqlite3VdbeSerialTypeLen(serial_type);
2459 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002460 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002461 if( pRec->flags & MEM_Zero ){
2462 /* Only pure zero-filled BLOBs can be input to this Opcode.
2463 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002464 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002465 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002466 nZero = 0;
2467 }
danielk19778d059842004-05-12 11:24:02 +00002468 }
danielk19773d1bfea2004-05-14 11:00:53 +00002469
drhf3218fe2004-05-28 08:21:02 +00002470 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002471 nHdr += nVarint = sqlite3VarintLen(nHdr);
2472 if( nVarint<sqlite3VarintLen(nHdr) ){
2473 nHdr++;
2474 }
drhfdf972a2007-05-02 13:30:27 +00002475 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002476 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002477 goto too_big;
2478 }
drhf3218fe2004-05-28 08:21:02 +00002479
danielk1977a7a8e142008-02-13 18:25:27 +00002480 /* Make sure the output register has a buffer large enough to store
2481 ** the new record. The output register (pOp->p3) is not allowed to
2482 ** be one of the input registers (because the following call to
2483 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2484 */
drh9c1905f2008-12-10 22:32:56 +00002485 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002486 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002487 }
danielk1977a7a8e142008-02-13 18:25:27 +00002488 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002489
2490 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002491 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002492 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002493 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002494 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002495 }
drha2a49dc2008-01-02 14:28:13 +00002496 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002497 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002498 }
drhfdf972a2007-05-02 13:30:27 +00002499 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002500
drh9cbf3422008-01-17 16:22:13 +00002501 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002502 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002503 pOut->flags = MEM_Blob | MEM_Dyn;
2504 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002505 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002506 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002507 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002508 }
drh477df4b2008-01-05 18:48:24 +00002509 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002510 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002511 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002512 break;
2513}
2514
danielk1977a5533162009-02-24 10:01:51 +00002515/* Opcode: Count P1 P2 * * *
2516**
2517** Store the number of entries (an integer value) in the table or index
2518** opened by cursor P1 in register P2
2519*/
2520#ifndef SQLITE_OMIT_BTREECOUNT
2521case OP_Count: { /* out2-prerelease */
2522 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002523 BtCursor *pCrsr;
2524
2525 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh818e39a2009-04-02 20:27:28 +00002526 if( pCrsr ){
2527 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2528 }else{
2529 nEntry = 0;
2530 }
danielk1977a5533162009-02-24 10:01:51 +00002531 pOut->u.i = nEntry;
2532 break;
2533}
2534#endif
2535
danielk1977fd7f0452008-12-17 17:30:26 +00002536/* Opcode: Savepoint P1 * * P4 *
2537**
2538** Open, release or rollback the savepoint named by parameter P4, depending
2539** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2540** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2541*/
2542case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002543 int p1; /* Value of P1 operand */
2544 char *zName; /* Name of savepoint */
2545 int nName;
2546 Savepoint *pNew;
2547 Savepoint *pSavepoint;
2548 Savepoint *pTmp;
2549 int iSavepoint;
2550 int ii;
2551
2552 p1 = pOp->p1;
2553 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002554
2555 /* Assert that the p1 parameter is valid. Also that if there is no open
2556 ** transaction, then there cannot be any savepoints.
2557 */
2558 assert( db->pSavepoint==0 || db->autoCommit==0 );
2559 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2560 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2561 assert( checkSavepointCount(db) );
2562
2563 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002564 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002565 /* A new savepoint cannot be created if there are active write
2566 ** statements (i.e. open read/write incremental blob handles).
2567 */
2568 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2569 "SQL statements in progress");
2570 rc = SQLITE_BUSY;
2571 }else{
drh856c1032009-06-02 15:21:42 +00002572 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002573
2574 /* Create a new savepoint structure. */
2575 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2576 if( pNew ){
2577 pNew->zName = (char *)&pNew[1];
2578 memcpy(pNew->zName, zName, nName+1);
2579
2580 /* If there is no open transaction, then mark this as a special
2581 ** "transaction savepoint". */
2582 if( db->autoCommit ){
2583 db->autoCommit = 0;
2584 db->isTransactionSavepoint = 1;
2585 }else{
2586 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002587 }
danielk1977fd7f0452008-12-17 17:30:26 +00002588
2589 /* Link the new savepoint into the database handle's list. */
2590 pNew->pNext = db->pSavepoint;
2591 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002592 pNew->nDeferredCons = db->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002593 }
2594 }
2595 }else{
drh856c1032009-06-02 15:21:42 +00002596 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002597
2598 /* Find the named savepoint. If there is no such savepoint, then an
2599 ** an error is returned to the user. */
2600 for(
drh856c1032009-06-02 15:21:42 +00002601 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002602 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002603 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002604 ){
2605 iSavepoint++;
2606 }
2607 if( !pSavepoint ){
2608 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2609 rc = SQLITE_ERROR;
2610 }else if(
2611 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2612 ){
2613 /* It is not possible to release (commit) a savepoint if there are
2614 ** active write statements. It is not possible to rollback a savepoint
2615 ** if there are any active statements at all.
2616 */
2617 sqlite3SetString(&p->zErrMsg, db,
2618 "cannot %s savepoint - SQL statements in progress",
2619 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2620 );
2621 rc = SQLITE_BUSY;
2622 }else{
2623
2624 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002625 ** and this is a RELEASE command, then the current transaction
2626 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002627 */
2628 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2629 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002630 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002631 goto vdbe_return;
2632 }
danielk1977fd7f0452008-12-17 17:30:26 +00002633 db->autoCommit = 1;
2634 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2635 p->pc = pc;
2636 db->autoCommit = 0;
2637 p->rc = rc = SQLITE_BUSY;
2638 goto vdbe_return;
2639 }
danielk197734cf35d2008-12-18 18:31:38 +00002640 db->isTransactionSavepoint = 0;
2641 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002642 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002643 iSavepoint = db->nSavepoint - iSavepoint - 1;
2644 for(ii=0; ii<db->nDb; ii++){
2645 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2646 if( rc!=SQLITE_OK ){
2647 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002648 }
danielk1977fd7f0452008-12-17 17:30:26 +00002649 }
drh9f0bbf92009-01-02 21:08:09 +00002650 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002651 sqlite3ExpirePreparedStatements(db);
2652 sqlite3ResetInternalSchema(db, 0);
danc311fee2010-08-31 16:25:19 +00002653 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002654 }
2655 }
2656
2657 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2658 ** savepoints nested inside of the savepoint being operated on. */
2659 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002660 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002661 db->pSavepoint = pTmp->pNext;
2662 sqlite3DbFree(db, pTmp);
2663 db->nSavepoint--;
2664 }
2665
dan1da40a32009-09-19 17:00:31 +00002666 /* If it is a RELEASE, then destroy the savepoint being operated on
2667 ** too. If it is a ROLLBACK TO, then set the number of deferred
2668 ** constraint violations present in the database to the value stored
2669 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002670 if( p1==SAVEPOINT_RELEASE ){
2671 assert( pSavepoint==db->pSavepoint );
2672 db->pSavepoint = pSavepoint->pNext;
2673 sqlite3DbFree(db, pSavepoint);
2674 if( !isTransaction ){
2675 db->nSavepoint--;
2676 }
dan1da40a32009-09-19 17:00:31 +00002677 }else{
2678 db->nDeferredCons = pSavepoint->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002679 }
2680 }
2681 }
2682
2683 break;
2684}
2685
drh98757152008-01-09 23:04:12 +00002686/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002687**
2688** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002689** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002690** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2691** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002692**
2693** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002694*/
drh9cbf3422008-01-17 16:22:13 +00002695case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002696 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002697 int iRollback;
drh856c1032009-06-02 15:21:42 +00002698 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002699
drh856c1032009-06-02 15:21:42 +00002700 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002701 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002702 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002703 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002704 assert( desiredAutoCommit==1 || iRollback==0 );
drh92f02c32004-09-02 14:57:08 +00002705 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002706
shane68c02732009-06-09 18:14:18 +00002707 if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){
drhad4a4b82008-11-05 16:37:34 +00002708 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002709 ** still running, and a transaction is active, return an error indicating
2710 ** that the other VMs must complete first.
2711 */
drhad4a4b82008-11-05 16:37:34 +00002712 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2713 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002714 rc = SQLITE_BUSY;
drh9eb8cbe2009-06-19 22:23:41 +00002715 }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){
drhad4a4b82008-11-05 16:37:34 +00002716 /* If this instruction implements a COMMIT and other VMs are writing
2717 ** return an error indicating that the other VMs must complete first.
2718 */
2719 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2720 "SQL statements in progress");
2721 rc = SQLITE_BUSY;
2722 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002723 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002724 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002725 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002726 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002727 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002728 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002729 }else{
shane7d3846a2008-12-11 02:58:26 +00002730 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002731 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002732 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002733 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002734 p->rc = rc = SQLITE_BUSY;
2735 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002736 }
danielk19771d850a72004-05-31 08:26:49 +00002737 }
danielk1977bd434552009-03-18 10:33:00 +00002738 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002739 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002740 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002741 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002742 }else{
drh900b31e2007-08-28 02:27:51 +00002743 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002744 }
drh900b31e2007-08-28 02:27:51 +00002745 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002746 }else{
drhf089aa42008-07-08 19:34:06 +00002747 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002748 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002749 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002750 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002751
2752 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002753 }
2754 break;
2755}
2756
drh98757152008-01-09 23:04:12 +00002757/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002758**
2759** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002760** opcode is encountered. Depending on the ON CONFLICT setting, the
2761** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002762**
drh001bbcb2003-03-19 03:14:00 +00002763** P1 is the index of the database file on which the transaction is
2764** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002765** file used for temporary tables. Indices of 2 or more are used for
2766** attached databases.
drhcabb0812002-09-14 13:47:32 +00002767**
drh80242052004-06-09 00:48:12 +00002768** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002769** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002770** other process can start another write transaction while this transaction is
2771** underway. Starting a write transaction also creates a rollback journal. A
2772** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002773** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2774** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002775**
dane0af83a2009-09-08 19:15:01 +00002776** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2777** true (this flag is set if the Vdbe may modify more than one row and may
2778** throw an ABORT exception), a statement transaction may also be opened.
2779** More specifically, a statement transaction is opened iff the database
2780** connection is currently not in autocommit mode, or if there are other
2781** active statements. A statement transaction allows the affects of this
2782** VDBE to be rolled back after an error without having to roll back the
2783** entire transaction. If no error is encountered, the statement transaction
2784** will automatically commit when the VDBE halts.
2785**
danielk1977ee5741e2004-05-31 10:01:34 +00002786** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002787*/
drh9cbf3422008-01-17 16:22:13 +00002788case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002789 Btree *pBt;
2790
drh653b82a2009-06-22 11:10:47 +00002791 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2792 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
2793 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002794
danielk197724162fe2004-06-04 06:22:00 +00002795 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002796 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002797 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002798 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002799 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002800 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002801 }
drh9e9f1bd2009-10-13 15:36:51 +00002802 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00002803 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002804 }
dane0af83a2009-09-08 19:15:01 +00002805
2806 if( pOp->p2 && p->usesStmtJournal
2807 && (db->autoCommit==0 || db->activeVdbeCnt>1)
2808 ){
2809 assert( sqlite3BtreeIsInTrans(pBt) );
2810 if( p->iStatement==0 ){
2811 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2812 db->nStatement++;
2813 p->iStatement = db->nSavepoint + db->nStatement;
2814 }
2815 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
dan1da40a32009-09-19 17:00:31 +00002816
2817 /* Store the current value of the database handles deferred constraint
2818 ** counter. If the statement transaction needs to be rolled back,
2819 ** the value of this counter needs to be restored too. */
2820 p->nStmtDefCons = db->nDeferredCons;
dane0af83a2009-09-08 19:15:01 +00002821 }
drhb86ccfb2003-01-28 23:13:10 +00002822 }
drh5e00f6c2001-09-13 13:46:56 +00002823 break;
2824}
2825
drhb1fdb2a2008-01-05 04:06:03 +00002826/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002827**
drh9cbf3422008-01-17 16:22:13 +00002828** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002829** P3==1 is the schema version. P3==2 is the database format.
2830** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002831** the main database file and P1==1 is the database file used to store
2832** temporary tables.
drh4a324312001-12-21 14:30:42 +00002833**
drh50e5dad2001-09-15 00:57:28 +00002834** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002835** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002836** executing this instruction.
2837*/
drh4c583122008-01-04 22:01:03 +00002838case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002839 int iMeta;
drh856c1032009-06-02 15:21:42 +00002840 int iDb;
2841 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00002842
drh856c1032009-06-02 15:21:42 +00002843 iDb = pOp->p1;
2844 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00002845 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002846 assert( iDb>=0 && iDb<db->nDb );
2847 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002848 assert( (p->btreeMask & (1<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00002849
danielk1977602b4662009-07-02 07:47:33 +00002850 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002851 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00002852 break;
2853}
2854
drh98757152008-01-09 23:04:12 +00002855/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002856**
drh98757152008-01-09 23:04:12 +00002857** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00002858** into cookie number P2 of database P1. P2==1 is the schema version.
2859** P2==2 is the database format. P2==3 is the recommended pager cache
2860** size, and so forth. P1==0 is the main database file and P1==1 is the
2861** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002862**
2863** A transaction must be started before executing this opcode.
2864*/
drh9cbf3422008-01-17 16:22:13 +00002865case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002866 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002867 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002868 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002869 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002870 pDb = &db->aDb[pOp->p1];
2871 assert( pDb->pBt!=0 );
drh3c657212009-11-17 23:59:58 +00002872 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00002873 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002874 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00002875 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
2876 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00002877 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002878 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002879 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00002880 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00002881 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002882 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002883 }
drhfd426c62006-01-30 15:34:22 +00002884 if( pOp->p1==1 ){
2885 /* Invalidate all prepared statements whenever the TEMP database
2886 ** schema is changed. Ticket #1644 */
2887 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00002888 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00002889 }
drh50e5dad2001-09-15 00:57:28 +00002890 break;
2891}
2892
drhc2a75552011-03-18 21:55:46 +00002893/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002894**
drh001bbcb2003-03-19 03:14:00 +00002895** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00002896** schema version) and make sure it is equal to P2 and that the
2897** generation counter on the local schema parse equals P3.
2898**
drh001bbcb2003-03-19 03:14:00 +00002899** P1 is the database number which is 0 for the main database file
2900** and 1 for the file holding temporary tables and some higher number
2901** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002902**
2903** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002904** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002905** and that the current process needs to reread the schema.
2906**
2907** Either a transaction needs to have been started or an OP_Open needs
2908** to be executed (to establish a read lock) before this opcode is
2909** invoked.
2910*/
drh9cbf3422008-01-17 16:22:13 +00002911case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002912 int iMeta;
drhc2a75552011-03-18 21:55:46 +00002913 int iGen;
drhc275b4e2004-07-19 17:25:24 +00002914 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00002915
drh001bbcb2003-03-19 03:14:00 +00002916 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002917 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002918 pBt = db->aDb[pOp->p1].pBt;
2919 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00002920 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00002921 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00002922 }else{
drhc275b4e2004-07-19 17:25:24 +00002923 iMeta = 0;
2924 }
drhc2a75552011-03-18 21:55:46 +00002925 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00002926 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002927 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002928 /* If the schema-cookie from the database file matches the cookie
2929 ** stored with the in-memory representation of the schema, do
2930 ** not reload the schema from the database file.
2931 **
shane21e7feb2008-05-30 15:59:49 +00002932 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002933 ** Often, v-tables store their data in other SQLite tables, which
2934 ** are queried from within xNext() and other v-table methods using
2935 ** prepared queries. If such a query is out-of-date, we do not want to
2936 ** discard the database schema, as the user code implementing the
2937 ** v-table would have to be ready for the sqlite3_vtab structure itself
2938 ** to be invalidated whenever sqlite3_step() is called from within
2939 ** a v-table method.
2940 */
2941 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2942 sqlite3ResetInternalSchema(db, pOp->p1);
2943 }
2944
drh5b6c5452011-02-22 03:34:56 +00002945 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00002946 rc = SQLITE_SCHEMA;
2947 }
2948 break;
2949}
2950
drh98757152008-01-09 23:04:12 +00002951/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002952**
drhecdc7532001-09-23 02:35:53 +00002953** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002954** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002955** P3==0 means the main database, P3==1 means the database used for
2956** temporary tables, and P3>1 means used the corresponding attached
2957** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002958** values need not be contiguous but all P1 values should be small integers.
2959** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002960**
drh98757152008-01-09 23:04:12 +00002961** If P5!=0 then use the content of register P2 as the root page, not
2962** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002963**
drhb19a2bc2001-09-16 00:13:26 +00002964** There will be a read lock on the database whenever there is an
2965** open cursor. If the database was unlocked prior to this instruction
2966** then a read lock is acquired as part of this instruction. A read
2967** lock allows other processes to read the database but prohibits
2968** any other process from modifying the database. The read lock is
2969** released when all cursors are closed. If this instruction attempts
2970** to get a read lock but fails, the script terminates with an
2971** SQLITE_BUSY error code.
2972**
danielk1977d336e222009-02-20 10:58:41 +00002973** The P4 value may be either an integer (P4_INT32) or a pointer to
2974** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2975** structure, then said structure defines the content and collating
2976** sequence of the index being opened. Otherwise, if P4 is an integer
2977** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00002978**
drh001bbcb2003-03-19 03:14:00 +00002979** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002980*/
drh98757152008-01-09 23:04:12 +00002981/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002982**
2983** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002984** page is P2. Or if P5!=0 use the content of register P2 to find the
2985** root page.
drhecdc7532001-09-23 02:35:53 +00002986**
danielk1977d336e222009-02-20 10:58:41 +00002987** The P4 value may be either an integer (P4_INT32) or a pointer to
2988** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2989** structure, then said structure defines the content and collating
2990** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00002991** value, it is set to the number of columns in the table, or to the
2992** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00002993**
drh001bbcb2003-03-19 03:14:00 +00002994** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002995** in read/write mode. For a given table, there can be one or more read-only
2996** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002997**
drh001bbcb2003-03-19 03:14:00 +00002998** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002999*/
drh9cbf3422008-01-17 16:22:13 +00003000case OP_OpenRead:
3001case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003002 int nField;
3003 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003004 int p2;
3005 int iDb;
drhf57b3392001-10-08 13:22:32 +00003006 int wrFlag;
3007 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003008 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003009 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003010
danfa401de2009-10-16 14:55:03 +00003011 if( p->expired ){
3012 rc = SQLITE_ABORT;
3013 break;
3014 }
3015
drh856c1032009-06-02 15:21:42 +00003016 nField = 0;
3017 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003018 p2 = pOp->p2;
3019 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003020 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00003021 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003022 pDb = &db->aDb[iDb];
3023 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003024 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003025 if( pOp->opcode==OP_OpenWrite ){
3026 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00003027 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3028 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003029 }
3030 }else{
3031 wrFlag = 0;
3032 }
drh98757152008-01-09 23:04:12 +00003033 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00003034 assert( p2>0 );
3035 assert( p2<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003036 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003037 assert( memIsValid(pIn2) );
3038 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003039 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003040 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003041 /* The p2 value always comes from a prior OP_CreateTable opcode and
3042 ** that opcode will always set the p2 value to 2 or more or else fail.
3043 ** If there were a failure, the prepared statement would have halted
3044 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003045 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003046 rc = SQLITE_CORRUPT_BKPT;
3047 goto abort_due_to_error;
3048 }
drh5edc3122001-09-13 21:53:09 +00003049 }
danielk1977d336e222009-02-20 10:58:41 +00003050 if( pOp->p4type==P4_KEYINFO ){
3051 pKeyInfo = pOp->p4.pKeyInfo;
3052 pKeyInfo->enc = ENC(p->db);
3053 nField = pKeyInfo->nField+1;
3054 }else if( pOp->p4type==P4_INT32 ){
3055 nField = pOp->p4.i;
3056 }
drh653b82a2009-06-22 11:10:47 +00003057 assert( pOp->p1>=0 );
3058 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003059 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003060 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003061 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003062 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3063 pCur->pKeyInfo = pKeyInfo;
3064
danielk1977172114a2009-07-07 15:47:12 +00003065 /* Since it performs no memory allocation or IO, the only values that
3066 ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
3067 ** SQLITE_EMPTY is only returned when attempting to open the table
3068 ** rooted at page 1 of a zero-byte database. */
3069 assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
3070 if( rc==SQLITE_EMPTY ){
3071 pCur->pCursor = 0;
3072 rc = SQLITE_OK;
danielk197724162fe2004-06-04 06:22:00 +00003073 }
danielk1977172114a2009-07-07 15:47:12 +00003074
3075 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3076 ** SQLite used to check if the root-page flags were sane at this point
3077 ** and report database corruption if they were not, but this check has
3078 ** since moved into the btree layer. */
3079 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3080 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003081 break;
3082}
3083
drh98757152008-01-09 23:04:12 +00003084/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003085**
drhb9bb7c12006-06-11 23:41:55 +00003086** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003087** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003088** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003089** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003090**
drh25d3adb2010-04-05 15:11:08 +00003091** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003092** The cursor points to a BTree table if P4==0 and to a BTree index
3093** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003094** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003095**
3096** This opcode was once called OpenTemp. But that created
3097** confusion because the term "temp table", might refer either
3098** to a TEMP table at the SQL level, or to a table opened by
3099** this opcode. Then this opcode was call OpenVirtual. But
3100** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00003101*/
drha21a64d2010-04-06 22:33:55 +00003102/* Opcode: OpenAutoindex P1 P2 * P4 *
3103**
3104** This opcode works the same as OP_OpenEphemeral. It has a
3105** different name to distinguish its use. Tables created using
3106** by this opcode will be used for automatically created transient
3107** indices in joins.
3108*/
3109case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003110case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003111 VdbeCursor *pCx;
drhd4187c72010-08-30 22:15:45 +00003112 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003113 SQLITE_OPEN_READWRITE |
3114 SQLITE_OPEN_CREATE |
3115 SQLITE_OPEN_EXCLUSIVE |
3116 SQLITE_OPEN_DELETEONCLOSE |
3117 SQLITE_OPEN_TRANSIENT_DB;
3118
drh653b82a2009-06-22 11:10:47 +00003119 assert( pOp->p1>=0 );
3120 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003121 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003122 pCx->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003123 rc = sqlite3BtreeOpen(0, db, &pCx->pBt,
3124 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003125 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003126 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003127 }
3128 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003129 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003130 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003131 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003132 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003133 */
danielk19772dca4ac2008-01-03 11:50:29 +00003134 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003135 int pgno;
drh66a51672008-01-03 00:01:23 +00003136 assert( pOp->p4type==P4_KEYINFO );
drhd4187c72010-08-30 22:15:45 +00003137 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY);
drhc6b52df2002-01-04 03:09:29 +00003138 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003139 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003140 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003141 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003142 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00003143 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003144 }
drhf0863fe2005-06-12 21:35:51 +00003145 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003146 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003147 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003148 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003149 }
drh5e00f6c2001-09-13 13:46:56 +00003150 }
drhd4187c72010-08-30 22:15:45 +00003151 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drhf0863fe2005-06-12 21:35:51 +00003152 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003153 break;
3154}
3155
danielk1977d336e222009-02-20 10:58:41 +00003156/* Opcode: OpenPseudo P1 P2 P3 * *
drh70ce3f02003-04-15 19:22:22 +00003157**
3158** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003159** row of data. The content of that one row in the content of memory
3160** register P2. In other words, cursor P1 becomes an alias for the
3161** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003162**
drh2d8d7ce2010-02-15 15:17:05 +00003163** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003164** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003165** individual columns using the OP_Column opcode. The OP_Column opcode
3166** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003167**
3168** P3 is the number of fields in the records that will be stored by
3169** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003170*/
drh9cbf3422008-01-17 16:22:13 +00003171case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003172 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003173
drh653b82a2009-06-22 11:10:47 +00003174 assert( pOp->p1>=0 );
3175 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003176 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003177 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003178 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003179 pCx->isTable = 1;
3180 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00003181 break;
3182}
3183
drh98757152008-01-09 23:04:12 +00003184/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003185**
3186** Close a cursor previously opened as P1. If P1 is not
3187** currently open, this instruction is a no-op.
3188*/
drh9cbf3422008-01-17 16:22:13 +00003189case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003190 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3191 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3192 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003193 break;
3194}
3195
drh959403f2008-12-12 17:56:16 +00003196/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003197**
danielk1977b790c6c2008-04-18 10:25:24 +00003198** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003199** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003200** to an SQL index, then P3 is the first in an array of P4 registers
3201** that are used as an unpacked index key.
3202**
3203** Reposition cursor P1 so that it points to the smallest entry that
3204** is greater than or equal to the key value. If there are no records
3205** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003206**
drh959403f2008-12-12 17:56:16 +00003207** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003208*/
drh959403f2008-12-12 17:56:16 +00003209/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003210**
danielk1977b790c6c2008-04-18 10:25:24 +00003211** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003212** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003213** to an SQL index, then P3 is the first in an array of P4 registers
3214** that are used as an unpacked index key.
3215**
3216** Reposition cursor P1 so that it points to the smallest entry that
3217** is greater than the key value. If there are no records greater than
3218** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003219**
drh959403f2008-12-12 17:56:16 +00003220** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003221*/
drh959403f2008-12-12 17:56:16 +00003222/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003223**
danielk1977b790c6c2008-04-18 10:25:24 +00003224** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003225** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003226** to an SQL index, then P3 is the first in an array of P4 registers
3227** that are used as an unpacked index key.
3228**
3229** Reposition cursor P1 so that it points to the largest entry that
3230** is less than the key value. If there are no records less than
3231** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003232**
drh959403f2008-12-12 17:56:16 +00003233** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003234*/
drh959403f2008-12-12 17:56:16 +00003235/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003236**
danielk1977b790c6c2008-04-18 10:25:24 +00003237** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003238** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003239** to an SQL index, then P3 is the first in an array of P4 registers
3240** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003241**
danielk1977b790c6c2008-04-18 10:25:24 +00003242** Reposition cursor P1 so that it points to the largest entry that
3243** is less than or equal to the key value. If there are no records
3244** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003245**
drh959403f2008-12-12 17:56:16 +00003246** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003247*/
drh959403f2008-12-12 17:56:16 +00003248case OP_SeekLt: /* jump, in3 */
3249case OP_SeekLe: /* jump, in3 */
3250case OP_SeekGe: /* jump, in3 */
3251case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003252 int res;
3253 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003254 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003255 UnpackedRecord r;
3256 int nField;
3257 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003258
drh653b82a2009-06-22 11:10:47 +00003259 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003260 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003261 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003262 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003263 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003264 assert( OP_SeekLe == OP_SeekLt+1 );
3265 assert( OP_SeekGe == OP_SeekLt+2 );
3266 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003267 assert( pC->isOrdered );
drh70ce3f02003-04-15 19:22:22 +00003268 if( pC->pCursor!=0 ){
drh7cf6e4d2004-05-19 14:56:55 +00003269 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003270 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003271 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003272 /* The input value in P3 might be of any type: integer, real, string,
3273 ** blob, or NULL. But it needs to be an integer before we can do
3274 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003275 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003276 applyNumericAffinity(pIn3);
3277 iKey = sqlite3VdbeIntValue(pIn3);
3278 pC->rowidIsValid = 0;
3279
3280 /* If the P3 value could not be converted into an integer without
3281 ** loss of information, then special processing is required... */
3282 if( (pIn3->flags & MEM_Int)==0 ){
3283 if( (pIn3->flags & MEM_Real)==0 ){
3284 /* If the P3 value cannot be converted into any kind of a number,
3285 ** then the seek is not possible, so jump to P2 */
3286 pc = pOp->p2 - 1;
3287 break;
3288 }
3289 /* If we reach this point, then the P3 value must be a floating
3290 ** point number. */
3291 assert( (pIn3->flags & MEM_Real)!=0 );
3292
3293 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003294 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003295 ** integer. */
3296 res = 1;
3297 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003298 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003299 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3300 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3301 }
3302 }else{
drh1f350122009-11-13 20:52:43 +00003303 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003304 rc = sqlite3BtreeLast(pC->pCursor, &res);
3305 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3306 }
3307 }
3308 if( res ){
3309 pc = pOp->p2 - 1;
3310 }
3311 break;
3312 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3313 /* Use the ceiling() function to convert real->int */
3314 if( pIn3->r > (double)iKey ) iKey++;
3315 }else{
3316 /* Use the floor() function to convert real->int */
3317 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3318 if( pIn3->r < (double)iKey ) iKey--;
3319 }
3320 }
drhe63d9992008-08-13 19:11:48 +00003321 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003322 if( rc!=SQLITE_OK ){
3323 goto abort_due_to_error;
3324 }
drh959403f2008-12-12 17:56:16 +00003325 if( res==0 ){
3326 pC->rowidIsValid = 1;
3327 pC->lastRowid = iKey;
3328 }
drh5e00f6c2001-09-13 13:46:56 +00003329 }else{
drh856c1032009-06-02 15:21:42 +00003330 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003331 assert( pOp->p4type==P4_INT32 );
3332 assert( nField>0 );
3333 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003334 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003335
3336 /* The next line of code computes as follows, only faster:
3337 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3338 ** r.flags = UNPACKED_INCRKEY;
3339 ** }else{
3340 ** r.flags = 0;
3341 ** }
3342 */
shaneh5e17e8b2009-12-03 04:40:47 +00003343 r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003344 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3345 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3346 assert( oc!=OP_SeekGe || r.flags==0 );
3347 assert( oc!=OP_SeekLt || r.flags==0 );
3348
drha6c2ed92009-11-14 23:22:23 +00003349 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003350#ifdef SQLITE_DEBUG
3351 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3352#endif
drh039fc322009-11-17 18:31:47 +00003353 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003354 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003355 if( rc!=SQLITE_OK ){
3356 goto abort_due_to_error;
3357 }
drhf0863fe2005-06-12 21:35:51 +00003358 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003359 }
drha11846b2004-01-07 18:52:56 +00003360 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003361 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003362#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003363 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003364#endif
drh1f350122009-11-13 20:52:43 +00003365 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003366 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003367 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003368 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003369 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003370 }else{
3371 res = 0;
drh8721ce42001-11-07 14:22:00 +00003372 }
drh7cf6e4d2004-05-19 14:56:55 +00003373 }else{
drh959403f2008-12-12 17:56:16 +00003374 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3375 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003376 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3377 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003378 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003379 }else{
3380 /* res might be negative because the table is empty. Check to
3381 ** see if this is the case.
3382 */
drhf328bc82004-05-10 23:29:49 +00003383 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003384 }
drh1af3fdb2004-07-18 21:33:01 +00003385 }
drh91fd4d42008-01-19 20:11:25 +00003386 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003387 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003388 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003389 }
drhaa736092009-06-22 00:55:30 +00003390 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003391 /* This happens when attempting to open the sqlite3_master table
3392 ** for read access returns SQLITE_EMPTY. In this case always
3393 ** take the jump (since there are no records in the table).
3394 */
3395 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003396 }
drh5e00f6c2001-09-13 13:46:56 +00003397 break;
3398}
3399
drh959403f2008-12-12 17:56:16 +00003400/* Opcode: Seek P1 P2 * * *
3401**
3402** P1 is an open table cursor and P2 is a rowid integer. Arrange
3403** for P1 to move so that it points to the rowid given by P2.
3404**
3405** This is actually a deferred seek. Nothing actually happens until
3406** the cursor is used to read a record. That way, if no reads
3407** occur, no unnecessary I/O happens.
3408*/
3409case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003410 VdbeCursor *pC;
3411
drh653b82a2009-06-22 11:10:47 +00003412 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3413 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003414 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003415 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003416 assert( pC->isTable );
3417 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003418 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003419 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3420 pC->rowidIsValid = 0;
3421 pC->deferredMoveto = 1;
3422 }
3423 break;
3424}
3425
3426
drh8cff69d2009-11-12 19:59:44 +00003427/* Opcode: Found P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003428**
drh8cff69d2009-11-12 19:59:44 +00003429** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3430** P4>0 then register P3 is the first of P4 registers that form an unpacked
3431** record.
3432**
3433** Cursor P1 is on an index btree. If the record identified by P3 and P4
3434** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003435** P1 is left pointing at the matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003436*/
drh8cff69d2009-11-12 19:59:44 +00003437/* Opcode: NotFound P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003438**
drh8cff69d2009-11-12 19:59:44 +00003439** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3440** P4>0 then register P3 is the first of P4 registers that form an unpacked
3441** record.
3442**
3443** Cursor P1 is on an index btree. If the record identified by P3 and P4
3444** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3445** does contain an entry whose prefix matches the P3/P4 record then control
3446** falls through to the next instruction and P1 is left pointing at the
3447** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003448**
drhcb6d50e2008-08-21 19:28:30 +00003449** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003450*/
drh9cbf3422008-01-17 16:22:13 +00003451case OP_NotFound: /* jump, in3 */
3452case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003453 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003454 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003455 int res;
3456 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003457 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003458 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3459
dan0ff297e2009-09-25 17:03:14 +00003460#ifdef SQLITE_TEST
3461 sqlite3_found_count++;
3462#endif
3463
drh856c1032009-06-02 15:21:42 +00003464 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003465 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003466 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003467 pC = p->apCsr[pOp->p1];
3468 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003469 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003470 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003471
drhf0863fe2005-06-12 21:35:51 +00003472 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003473 if( pOp->p4.i>0 ){
3474 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003475 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003476 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003477#ifdef SQLITE_DEBUG
3478 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3479#endif
drh8cff69d2009-11-12 19:59:44 +00003480 r.flags = UNPACKED_PREFIX_MATCH;
3481 pIdxKey = &r;
3482 }else{
3483 assert( pIn3->flags & MEM_Blob );
drhd81a1422010-09-28 07:11:24 +00003484 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
drh8cff69d2009-11-12 19:59:44 +00003485 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
3486 aTempRec, sizeof(aTempRec));
3487 if( pIdxKey==0 ){
3488 goto no_mem;
3489 }
3490 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003491 }
drhe63d9992008-08-13 19:11:48 +00003492 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003493 if( pOp->p4.i==0 ){
3494 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
3495 }
danielk197777519402007-08-30 11:48:31 +00003496 if( rc!=SQLITE_OK ){
3497 break;
3498 }
3499 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003500 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003501 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003502 }
3503 if( pOp->opcode==OP_Found ){
3504 if( alreadyExists ) pc = pOp->p2 - 1;
3505 }else{
3506 if( !alreadyExists ) pc = pOp->p2 - 1;
3507 }
drh5e00f6c2001-09-13 13:46:56 +00003508 break;
3509}
3510
drh98757152008-01-09 23:04:12 +00003511/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003512**
drh8cff69d2009-11-12 19:59:44 +00003513** Cursor P1 is open on an index b-tree - that is to say, a btree which
3514** no data and where the key are records generated by OP_MakeRecord with
3515** the list field being the integer ROWID of the entry that the index
3516** entry refers to.
danielk1977de630352009-05-04 11:42:29 +00003517**
3518** The P3 register contains an integer record number. Call this record
3519** number R. Register P4 is the first in a set of N contiguous registers
3520** that make up an unpacked index key that can be used with cursor P1.
3521** The value of N can be inferred from the cursor. N includes the rowid
3522** value appended to the end of the index record. This rowid value may
3523** or may not be the same as R.
3524**
3525** If any of the N registers beginning with register P4 contains a NULL
3526** value, jump immediately to P2.
3527**
3528** Otherwise, this instruction checks if cursor P1 contains an entry
3529** where the first (N-1) fields match but the rowid value at the end
3530** of the index entry is not R. If there is no such entry, control jumps
3531** to instruction P2. Otherwise, the rowid of the conflicting index
3532** entry is copied to register P3 and control falls through to the next
3533** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003534**
drh9cbf3422008-01-17 16:22:13 +00003535** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003536*/
drh9cbf3422008-01-17 16:22:13 +00003537case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003538 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003539 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003540 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003541 u16 nField;
drha6c2ed92009-11-14 23:22:23 +00003542 Mem *aMx;
drh856c1032009-06-02 15:21:42 +00003543 UnpackedRecord r; /* B-Tree index search key */
3544 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003545
drh3c657212009-11-17 23:59:58 +00003546 pIn3 = &aMem[pOp->p3];
drha6c2ed92009-11-14 23:22:23 +00003547 aMx = &aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003548 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003549 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003550 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003551 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3552
3553 /* Find the index cursor. */
3554 pCx = p->apCsr[pOp->p1];
3555 assert( pCx->deferredMoveto==0 );
3556 pCx->seekResult = 0;
3557 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003558 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003559
3560 /* If any of the values are NULL, take the jump. */
3561 nField = pCx->pKeyInfo->nField;
3562 for(ii=0; ii<nField; ii++){
drha6c2ed92009-11-14 23:22:23 +00003563 if( aMx[ii].flags & MEM_Null ){
danielk1977de630352009-05-04 11:42:29 +00003564 pc = pOp->p2 - 1;
3565 pCrsr = 0;
3566 break;
3567 }
3568 }
drha6c2ed92009-11-14 23:22:23 +00003569 assert( (aMx[nField].flags & MEM_Null)==0 );
danielk1977de630352009-05-04 11:42:29 +00003570
drhf328bc82004-05-10 23:29:49 +00003571 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003572 /* Populate the index search key. */
3573 r.pKeyInfo = pCx->pKeyInfo;
3574 r.nField = nField + 1;
3575 r.flags = UNPACKED_PREFIX_SEARCH;
drha6c2ed92009-11-14 23:22:23 +00003576 r.aMem = aMx;
drh2b4ded92010-09-27 21:09:31 +00003577#ifdef SQLITE_DEBUG
3578 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3579#endif
danielk1977452c9892004-05-13 05:16:15 +00003580
danielk1977de630352009-05-04 11:42:29 +00003581 /* Extract the value of R from register P3. */
3582 sqlite3VdbeMemIntegerify(pIn3);
3583 R = pIn3->u.i;
3584
3585 /* Search the B-Tree index. If no conflicting record is found, jump
3586 ** to P2. Otherwise, copy the rowid of the conflicting record to
3587 ** register P3 and fall through to the next instruction. */
3588 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3589 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003590 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003591 }else{
3592 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003593 }
drh9cfcf5d2002-01-29 18:41:24 +00003594 }
3595 break;
3596}
3597
drh9cbf3422008-01-17 16:22:13 +00003598/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003599**
drh9cbf3422008-01-17 16:22:13 +00003600** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003601** with that key does not exist in table of P1, then jump to P2.
drh710c4842010-08-30 01:17:20 +00003602** If the record does exist, then fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003603** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003604**
3605** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003606** operation assumes the key is an integer and that P1 is a table whereas
3607** NotFound assumes key is a blob constructed from MakeRecord and
3608** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003609**
drhcb6d50e2008-08-21 19:28:30 +00003610** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003611*/
drh9cbf3422008-01-17 16:22:13 +00003612case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003613 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003614 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003615 int res;
3616 u64 iKey;
3617
drh3c657212009-11-17 23:59:58 +00003618 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003619 assert( pIn3->flags & MEM_Int );
3620 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3621 pC = p->apCsr[pOp->p1];
3622 assert( pC!=0 );
3623 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003624 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003625 pCrsr = pC->pCursor;
3626 if( pCrsr!=0 ){
drh856c1032009-06-02 15:21:42 +00003627 res = 0;
drhaa736092009-06-22 00:55:30 +00003628 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003629 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003630 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003631 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003632 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003633 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003634 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003635 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003636 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003637 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003638 }
danielk1977de630352009-05-04 11:42:29 +00003639 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003640 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003641 /* This happens when an attempt to open a read cursor on the
3642 ** sqlite_master table returns SQLITE_EMPTY.
3643 */
danielk1977f7b9d662008-06-23 18:49:43 +00003644 pc = pOp->p2 - 1;
3645 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003646 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003647 }
drh6b125452002-01-28 15:53:03 +00003648 break;
3649}
3650
drh4c583122008-01-04 22:01:03 +00003651/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003652**
drh4c583122008-01-04 22:01:03 +00003653** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003654** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003655** The sequence number on the cursor is incremented after this
3656** instruction.
drh4db38a72005-09-01 12:16:28 +00003657*/
drh4c583122008-01-04 22:01:03 +00003658case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003659 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3660 assert( p->apCsr[pOp->p1]!=0 );
3661 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003662 break;
3663}
3664
3665
drh98757152008-01-09 23:04:12 +00003666/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003667**
drhf0863fe2005-06-12 21:35:51 +00003668** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003669** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003670** table that cursor P1 points to. The new record number is written
3671** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003672**
dan76d462e2009-08-30 11:42:51 +00003673** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3674** the largest previously generated record number. No new record numbers are
3675** allowed to be less than this value. When this value reaches its maximum,
3676** a SQLITE_FULL error is generated. The P3 register is updated with the '
3677** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003678** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003679*/
drh4c583122008-01-04 22:01:03 +00003680case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003681 i64 v; /* The new rowid */
3682 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3683 int res; /* Result of an sqlite3BtreeLast() */
3684 int cnt; /* Counter to limit the number of searches */
3685 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003686 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003687
drh856c1032009-06-02 15:21:42 +00003688 v = 0;
3689 res = 0;
drhaa736092009-06-22 00:55:30 +00003690 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3691 pC = p->apCsr[pOp->p1];
3692 assert( pC!=0 );
3693 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003694 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003695 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003696 /* The next rowid or record number (different terms for the same
3697 ** thing) is obtained in a two-step algorithm.
3698 **
3699 ** First we attempt to find the largest existing rowid and add one
3700 ** to that. But if the largest existing rowid is already the maximum
3701 ** positive integer, we have to fall through to the second
3702 ** probabilistic algorithm
3703 **
3704 ** The second algorithm is to select a rowid at random and see if
3705 ** it already exists in the table. If it does not exist, we have
3706 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003707 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003708 */
drhaa736092009-06-22 00:55:30 +00003709 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003710
drh75f86a42005-02-17 00:03:06 +00003711#ifdef SQLITE_32BIT_ROWID
3712# define MAX_ROWID 0x7fffffff
3713#else
drhfe2093d2005-01-20 22:48:47 +00003714 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3715 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3716 ** to provide the constant while making all compilers happy.
3717 */
danielk197764202cf2008-11-17 15:31:47 +00003718# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003719#endif
drhfe2093d2005-01-20 22:48:47 +00003720
drh5cf8e8c2002-02-19 22:42:05 +00003721 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003722 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3723 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003724 rc = sqlite3BtreeLast(pC->pCursor, &res);
3725 if( rc!=SQLITE_OK ){
3726 goto abort_due_to_error;
3727 }
drh32fbe342002-10-19 20:16:37 +00003728 if( res ){
drhc79c7612010-01-01 18:57:48 +00003729 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003730 }else{
drhea8ffdf2009-07-22 00:35:23 +00003731 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003732 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3733 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drh75f86a42005-02-17 00:03:06 +00003734 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003735 pC->useRandomRowid = 1;
3736 }else{
drhc79c7612010-01-01 18:57:48 +00003737 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003738 }
drh5cf8e8c2002-02-19 22:42:05 +00003739 }
drh3fc190c2001-09-14 03:24:23 +00003740 }
drh205f48e2004-11-05 00:43:11 +00003741
3742#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003743 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003744 /* Assert that P3 is a valid memory cell. */
3745 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003746 if( p->pFrame ){
3747 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003748 /* Assert that P3 is a valid memory cell. */
3749 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003750 pMem = &pFrame->aMem[pOp->p3];
3751 }else{
shaneabc6b892009-09-10 19:09:03 +00003752 /* Assert that P3 is a valid memory cell. */
3753 assert( pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003754 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003755 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003756 }
drh2b4ded92010-09-27 21:09:31 +00003757 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003758
3759 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003760 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003761 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003762 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003763 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003764 goto abort_due_to_error;
3765 }
drh3c024d62007-03-30 11:23:45 +00003766 if( v<pMem->u.i+1 ){
3767 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003768 }
drh3c024d62007-03-30 11:23:45 +00003769 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003770 }
3771#endif
3772
drh7f751222009-03-17 22:33:00 +00003773 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003774 }
3775 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003776 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003777 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003778 ** engine starts picking positive candidate ROWIDs at random until
3779 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003780 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3781 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003782 /* on the first attempt, simply do one more than previous */
drh9ed7a992009-06-26 15:14:55 +00003783 v = db->lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003784 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3785 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003786 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003787 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3788 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003789 && (res==0)
3790 && (++cnt<100)){
3791 /* collision - try another random rowid */
3792 sqlite3_randomness(sizeof(v), &v);
3793 if( cnt<5 ){
3794 /* try "small" random rowids for the initial attempts */
3795 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00003796 }else{
shanehc4d340a2010-09-01 02:37:56 +00003797 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00003798 }
shanehc4d340a2010-09-01 02:37:56 +00003799 v++; /* ensure non-zero */
3800 }
drhaa736092009-06-22 00:55:30 +00003801 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00003802 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00003803 goto abort_due_to_error;
3804 }
drh748a52c2010-09-01 11:50:08 +00003805 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00003806 }
drhf0863fe2005-06-12 21:35:51 +00003807 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003808 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003809 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003810 }
drh4c583122008-01-04 22:01:03 +00003811 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003812 break;
3813}
3814
danielk19771f4aa332008-01-03 09:51:55 +00003815/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003816**
jplyon5a564222003-06-02 06:15:58 +00003817** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003818** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00003819** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00003820** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00003821** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00003822**
danielk19771f4aa332008-01-03 09:51:55 +00003823** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3824** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003825** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003826** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003827**
drh3e9ca092009-09-08 01:14:48 +00003828** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
3829** the last seek operation (OP_NotExists) was a success, then this
3830** operation will not attempt to find the appropriate row before doing
3831** the insert but will instead overwrite the row that the cursor is
3832** currently pointing to. Presumably, the prior OP_NotExists opcode
3833** has already positioned the cursor correctly. This is an optimization
3834** that boosts performance by avoiding redundant seeks.
3835**
3836** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
3837** UPDATE operation. Otherwise (if the flag is clear) then this opcode
3838** is part of an INSERT operation. The difference is only important to
3839** the update hook.
3840**
drh66a51672008-01-03 00:01:23 +00003841** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003842** may be NULL. If it is not NULL, then the update-hook
3843** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3844**
drh93aed5a2008-01-16 17:46:38 +00003845** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3846** allocated, then ownership of P2 is transferred to the pseudo-cursor
3847** and register P2 becomes ephemeral. If the cursor is changed, the
3848** value of register P2 will then change. Make sure this does not
3849** cause any problems.)
3850**
drhf0863fe2005-06-12 21:35:51 +00003851** This instruction only works on tables. The equivalent instruction
3852** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003853*/
drhe05c9292009-10-29 13:48:10 +00003854/* Opcode: InsertInt P1 P2 P3 P4 P5
3855**
3856** This works exactly like OP_Insert except that the key is the
3857** integer value P3, not the value of the integer stored in register P3.
3858*/
3859case OP_Insert:
3860case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00003861 Mem *pData; /* MEM cell holding data for the record to be inserted */
3862 Mem *pKey; /* MEM cell holding key for the record */
3863 i64 iKey; /* The integer ROWID or key for the record to be inserted */
3864 VdbeCursor *pC; /* Cursor to table into which insert is written */
3865 int nZero; /* Number of zero-bytes to append */
3866 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
3867 const char *zDb; /* database name - used by the update hook */
3868 const char *zTbl; /* Table name - used by the opdate hook */
3869 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00003870
drha6c2ed92009-11-14 23:22:23 +00003871 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00003872 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00003873 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00003874 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00003875 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003876 assert( pC->pCursor!=0 );
3877 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00003878 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003879 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00003880
drhe05c9292009-10-29 13:48:10 +00003881 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00003882 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00003883 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00003884 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00003885 REGISTER_TRACE(pOp->p3, pKey);
3886 iKey = pKey->u.i;
3887 }else{
3888 assert( pOp->opcode==OP_InsertInt );
3889 iKey = pOp->p3;
3890 }
3891
drha05a7222008-01-19 03:35:58 +00003892 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhe05c9292009-10-29 13:48:10 +00003893 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00003894 if( pData->flags & MEM_Null ){
3895 pData->z = 0;
3896 pData->n = 0;
3897 }else{
3898 assert( pData->flags & (MEM_Blob|MEM_Str) );
3899 }
drh3e9ca092009-09-08 01:14:48 +00003900 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
3901 if( pData->flags & MEM_Zero ){
3902 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003903 }else{
drh3e9ca092009-09-08 01:14:48 +00003904 nZero = 0;
drha05a7222008-01-19 03:35:58 +00003905 }
drh3e9ca092009-09-08 01:14:48 +00003906 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
3907 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3908 pData->z, pData->n, nZero,
3909 pOp->p5 & OPFLAG_APPEND, seekResult
3910 );
drha05a7222008-01-19 03:35:58 +00003911 pC->rowidIsValid = 0;
3912 pC->deferredMoveto = 0;
3913 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003914
drha05a7222008-01-19 03:35:58 +00003915 /* Invoke the update-hook if required. */
3916 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00003917 zDb = db->aDb[pC->iDb].zName;
3918 zTbl = pOp->p4.z;
3919 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00003920 assert( pC->isTable );
3921 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3922 assert( pC->iDb>=0 );
3923 }
drh5e00f6c2001-09-13 13:46:56 +00003924 break;
3925}
3926
drh98757152008-01-09 23:04:12 +00003927/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003928**
drh5edc3122001-09-13 21:53:09 +00003929** Delete the record at which the P1 cursor is currently pointing.
3930**
3931** The cursor will be left pointing at either the next or the previous
3932** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003933** the next Next instruction will be a no-op. Hence it is OK to delete
3934** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003935**
rdcb0c374f2004-02-20 22:53:38 +00003936** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003937** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003938**
drh91fd4d42008-01-19 20:11:25 +00003939** P1 must not be pseudo-table. It has to be a real table with
3940** multiple rows.
3941**
3942** If P4 is not NULL, then it is the name of the table that P1 is
3943** pointing to. The update hook will be invoked, if it exists.
3944** If P4 is not NULL then the P1 cursor must have been positioned
3945** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003946*/
drh9cbf3422008-01-17 16:22:13 +00003947case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00003948 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003949 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003950
drh856c1032009-06-02 15:21:42 +00003951 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00003952 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3953 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003954 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003955 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003956
drh91fd4d42008-01-19 20:11:25 +00003957 /* If the update-hook will be invoked, set iKey to the rowid of the
3958 ** row being deleted.
3959 */
3960 if( db->xUpdateCallback && pOp->p4.z ){
3961 assert( pC->isTable );
3962 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3963 iKey = pC->lastRowid;
3964 }
danielk197794eb6a12005-12-15 15:22:08 +00003965
drh9a65f2c2009-06-22 19:05:40 +00003966 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
3967 ** OP_Column on the same table without any intervening operations that
3968 ** might move or invalidate the cursor. Hence cursor pC is always pointing
3969 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
3970 ** below is always a no-op and cannot fail. We will run it anyhow, though,
3971 ** to guard against future changes to the code generator.
3972 **/
3973 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00003974 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00003975 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
3976
drh7f751222009-03-17 22:33:00 +00003977 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00003978 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00003979 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003980
drh91fd4d42008-01-19 20:11:25 +00003981 /* Invoke the update-hook if required. */
3982 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3983 const char *zDb = db->aDb[pC->iDb].zName;
3984 const char *zTbl = pOp->p4.z;
3985 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3986 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003987 }
danielk1977b28af712004-06-21 06:50:26 +00003988 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003989 break;
3990}
drhb7f1d9a2009-09-08 02:27:58 +00003991/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00003992**
drhb7f1d9a2009-09-08 02:27:58 +00003993** The value of the change counter is copied to the database handle
3994** change counter (returned by subsequent calls to sqlite3_changes()).
3995** Then the VMs internal change counter resets to 0.
3996** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003997*/
drh9cbf3422008-01-17 16:22:13 +00003998case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00003999 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004000 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004001 break;
4002}
4003
drh98757152008-01-09 23:04:12 +00004004/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00004005**
drh98757152008-01-09 23:04:12 +00004006** Write into register P2 the complete row data for cursor P1.
4007** There is no interpretation of the data.
4008** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004009** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004010**
drhde4fcfd2008-01-19 23:50:26 +00004011** If the P1 cursor must be pointing to a valid row (not a NULL row)
4012** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004013*/
drh98757152008-01-09 23:04:12 +00004014/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00004015**
drh98757152008-01-09 23:04:12 +00004016** Write into register P2 the complete row key for cursor P1.
4017** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004018** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004019** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004020**
drhde4fcfd2008-01-19 23:50:26 +00004021** If the P1 cursor must be pointing to a valid row (not a NULL row)
4022** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004023*/
danielk1977a7a8e142008-02-13 18:25:27 +00004024case OP_RowKey:
4025case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004026 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004027 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004028 u32 n;
drh856c1032009-06-02 15:21:42 +00004029 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004030
drha6c2ed92009-11-14 23:22:23 +00004031 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004032 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004033
drhf0863fe2005-06-12 21:35:51 +00004034 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004035 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4036 pC = p->apCsr[pOp->p1];
drhf0863fe2005-06-12 21:35:51 +00004037 assert( pC->isTable || pOp->opcode==OP_RowKey );
4038 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004039 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004040 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004041 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004042 assert( pC->pCursor!=0 );
4043 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004044 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004045
4046 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4047 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4048 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4049 ** a no-op and can never fail. But we leave it in place as a safety.
4050 */
4051 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004052 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004053 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4054
drhde4fcfd2008-01-19 23:50:26 +00004055 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00004056 assert( !pC->isTable );
drhc27ae612009-07-14 18:35:44 +00004057 rc = sqlite3BtreeKeySize(pCrsr, &n64);
4058 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004059 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004060 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004061 }
drhbfb19dc2009-06-05 16:46:53 +00004062 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004063 }else{
drhc27ae612009-07-14 18:35:44 +00004064 rc = sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004065 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004066 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004067 goto too_big;
4068 }
drhde4fcfd2008-01-19 23:50:26 +00004069 }
danielk1977a7a8e142008-02-13 18:25:27 +00004070 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4071 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004072 }
danielk1977a7a8e142008-02-13 18:25:27 +00004073 pOut->n = n;
4074 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00004075 if( pC->isIndex ){
4076 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4077 }else{
4078 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004079 }
danielk197796cb76f2008-01-04 13:24:28 +00004080 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004081 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00004082 break;
4083}
4084
drh2133d822008-01-03 18:44:59 +00004085/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004086**
drh2133d822008-01-03 18:44:59 +00004087** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004088** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004089**
4090** P1 can be either an ordinary table or a virtual table. There used to
4091** be a separate OP_VRowid opcode for use with virtual tables, but this
4092** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004093*/
drh4c583122008-01-04 22:01:03 +00004094case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004095 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004096 i64 v;
drh856c1032009-06-02 15:21:42 +00004097 sqlite3_vtab *pVtab;
4098 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004099
drh653b82a2009-06-22 11:10:47 +00004100 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4101 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004102 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004103 assert( pC->pseudoTableReg==0 );
drh044925b2009-04-22 17:15:02 +00004104 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004105 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004106 break;
4107 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004108 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004109#ifndef SQLITE_OMIT_VIRTUALTABLE
4110 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004111 pVtab = pC->pVtabCursor->pVtab;
4112 pModule = pVtab->pModule;
4113 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004114 rc = pModule->xRowid(pC->pVtabCursor, &v);
drhb9755982010-07-24 16:34:37 +00004115 importVtabErrMsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004116#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004117 }else{
drh6be240e2009-07-14 02:33:02 +00004118 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004119 rc = sqlite3VdbeCursorMoveto(pC);
4120 if( rc ) goto abort_due_to_error;
4121 if( pC->rowidIsValid ){
4122 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004123 }else{
drhc27ae612009-07-14 18:35:44 +00004124 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4125 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004126 }
drh5e00f6c2001-09-13 13:46:56 +00004127 }
drh4c583122008-01-04 22:01:03 +00004128 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004129 break;
4130}
4131
drh9cbf3422008-01-17 16:22:13 +00004132/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004133**
4134** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004135** that occur while the cursor is on the null row will always
4136** write a NULL.
drh17f71932002-02-21 12:01:27 +00004137*/
drh9cbf3422008-01-17 16:22:13 +00004138case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004139 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004140
drh653b82a2009-06-22 11:10:47 +00004141 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4142 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004143 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004144 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004145 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00004146 if( pC->pCursor ){
4147 sqlite3BtreeClearCursor(pC->pCursor);
4148 }
drh17f71932002-02-21 12:01:27 +00004149 break;
4150}
4151
drh9cbf3422008-01-17 16:22:13 +00004152/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004153**
drhf0863fe2005-06-12 21:35:51 +00004154** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004155** will refer to the last entry in the database table or index.
4156** If the table or index is empty and P2>0, then jump immediately to P2.
4157** If P2 is 0 or if the table or index is not empty, fall through
4158** to the following instruction.
4159*/
drh9cbf3422008-01-17 16:22:13 +00004160case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004161 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004162 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004163 int res;
drh9562b552002-02-19 15:00:07 +00004164
drh653b82a2009-06-22 11:10:47 +00004165 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4166 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004167 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004168 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004169 if( pCrsr==0 ){
4170 res = 1;
4171 }else{
4172 rc = sqlite3BtreeLast(pCrsr, &res);
4173 }
drh9c1905f2008-12-10 22:32:56 +00004174 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004175 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004176 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004177 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004178 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004179 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004180 }
4181 break;
4182}
4183
drh0342b1f2005-09-01 03:07:44 +00004184
drh9cbf3422008-01-17 16:22:13 +00004185/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004186**
4187** This opcode does exactly the same thing as OP_Rewind except that
4188** it increments an undocumented global variable used for testing.
4189**
4190** Sorting is accomplished by writing records into a sorting index,
4191** then rewinding that index and playing it back from beginning to
4192** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4193** rewinding so that the global variable will be incremented and
4194** regression tests can determine whether or not the optimizer is
4195** correctly optimizing out sorts.
4196*/
drh9cbf3422008-01-17 16:22:13 +00004197case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004198#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004199 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004200 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004201#endif
drhd1d38482008-10-07 23:46:38 +00004202 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00004203 /* Fall through into OP_Rewind */
4204}
drh9cbf3422008-01-17 16:22:13 +00004205/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004206**
drhf0863fe2005-06-12 21:35:51 +00004207** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004208** will refer to the first entry in the database table or index.
4209** If the table or index is empty and P2>0, then jump immediately to P2.
4210** If P2 is 0 or if the table or index is not empty, fall through
4211** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004212*/
drh9cbf3422008-01-17 16:22:13 +00004213case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004214 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004215 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004216 int res;
drh5e00f6c2001-09-13 13:46:56 +00004217
drh653b82a2009-06-22 11:10:47 +00004218 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4219 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004220 assert( pC!=0 );
dan2411dea2010-07-03 05:56:09 +00004221 res = 1;
drh70ce3f02003-04-15 19:22:22 +00004222 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00004223 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004224 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004225 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004226 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004227 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004228 }
drh9c1905f2008-12-10 22:32:56 +00004229 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004230 assert( pOp->p2>0 && pOp->p2<p->nOp );
4231 if( res ){
drhf4dada72004-05-11 09:57:35 +00004232 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004233 }
4234 break;
4235}
4236
drhafc266a2010-03-31 17:47:44 +00004237/* Opcode: Next P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00004238**
4239** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004240** table or index. If there are no more key/value pairs then fall through
4241** to the following instruction. But if the cursor advance was successful,
4242** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004243**
drh60a713c2008-01-21 16:22:45 +00004244** The P1 cursor must be for a real table, not a pseudo-table.
4245**
drhafc266a2010-03-31 17:47:44 +00004246** If P5 is positive and the jump is taken, then event counter
4247** number P5-1 in the prepared statement is incremented.
4248**
drhc045ec52002-12-04 20:01:06 +00004249** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004250*/
drhafc266a2010-03-31 17:47:44 +00004251/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004252**
4253** Back up cursor P1 so that it points to the previous key/data pair in its
4254** table or index. If there is no previous key/value pairs then fall through
4255** to the following instruction. But if the cursor backup was successful,
4256** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004257**
4258** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004259**
4260** If P5 is positive and the jump is taken, then event counter
4261** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004262*/
drh9cbf3422008-01-17 16:22:13 +00004263case OP_Prev: /* jump */
4264case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004265 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004266 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00004267 int res;
drh8721ce42001-11-07 14:22:00 +00004268
drhcaec2f12003-01-07 02:47:47 +00004269 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004270 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhafc266a2010-03-31 17:47:44 +00004271 assert( pOp->p5<=ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004272 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004273 if( pC==0 ){
4274 break; /* See ticket #2273 */
4275 }
drh60a713c2008-01-21 16:22:45 +00004276 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004277 if( pCrsr==0 ){
4278 pC->nullRow = 1;
4279 break;
4280 }
drha3460582008-07-11 21:02:53 +00004281 res = 1;
4282 assert( pC->deferredMoveto==0 );
4283 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
4284 sqlite3BtreePrevious(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004285 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004286 pC->cacheStatus = CACHE_STALE;
4287 if( res==0 ){
4288 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004289 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004290#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004291 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004292#endif
drh8721ce42001-11-07 14:22:00 +00004293 }
drhf0863fe2005-06-12 21:35:51 +00004294 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004295 break;
4296}
4297
danielk1977de630352009-05-04 11:42:29 +00004298/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004299**
drhaa9b8962008-01-08 02:57:55 +00004300** Register P2 holds a SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004301** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004302** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004303**
drhaa9b8962008-01-08 02:57:55 +00004304** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004305** insert is likely to be an append.
4306**
drhf0863fe2005-06-12 21:35:51 +00004307** This instruction only works for indices. The equivalent instruction
4308** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004309*/
drh9cbf3422008-01-17 16:22:13 +00004310case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004311 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004312 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004313 int nKey;
4314 const char *zKey;
4315
drh653b82a2009-06-22 11:10:47 +00004316 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4317 pC = p->apCsr[pOp->p1];
4318 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00004319 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004320 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004321 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004322 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004323 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004324 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004325 if( rc==SQLITE_OK ){
drh856c1032009-06-02 15:21:42 +00004326 nKey = pIn2->n;
4327 zKey = pIn2->z;
danielk1977de630352009-05-04 11:42:29 +00004328 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4329 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4330 );
danielk1977d908f5a2007-05-11 07:08:28 +00004331 assert( pC->deferredMoveto==0 );
4332 pC->cacheStatus = CACHE_STALE;
4333 }
drh5e00f6c2001-09-13 13:46:56 +00004334 }
drh5e00f6c2001-09-13 13:46:56 +00004335 break;
4336}
4337
drhd1d38482008-10-07 23:46:38 +00004338/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004339**
drhe14006d2008-03-25 17:23:32 +00004340** The content of P3 registers starting at register P2 form
4341** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004342** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004343*/
drhe14006d2008-03-25 17:23:32 +00004344case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004345 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004346 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004347 int res;
4348 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004349
drhe14006d2008-03-25 17:23:32 +00004350 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004351 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004352 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4353 pC = p->apCsr[pOp->p1];
4354 assert( pC!=0 );
4355 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004356 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004357 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004358 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004359 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004360 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004361#ifdef SQLITE_DEBUG
4362 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4363#endif
drhe63d9992008-08-13 19:11:48 +00004364 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004365 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004366 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004367 }
drh9188b382004-05-14 21:12:22 +00004368 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004369 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004370 }
drh5e00f6c2001-09-13 13:46:56 +00004371 break;
4372}
4373
drh2133d822008-01-03 18:44:59 +00004374/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004375**
drh2133d822008-01-03 18:44:59 +00004376** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004377** the end of the index key pointed to by cursor P1. This integer should be
4378** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004379**
drh9437bd22009-02-01 00:29:56 +00004380** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004381*/
drh4c583122008-01-04 22:01:03 +00004382case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004383 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004384 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004385 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004386
drh653b82a2009-06-22 11:10:47 +00004387 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4388 pC = p->apCsr[pOp->p1];
4389 assert( pC!=0 );
4390 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004391 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004392 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004393 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004394 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004395 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004396 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004397 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004398 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004399 if( rc!=SQLITE_OK ){
4400 goto abort_due_to_error;
4401 }
drh4c583122008-01-04 22:01:03 +00004402 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004403 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004404 }
drh8721ce42001-11-07 14:22:00 +00004405 }
4406 break;
4407}
4408
danielk197761dd5832008-04-18 11:31:12 +00004409/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004410**
danielk197761dd5832008-04-18 11:31:12 +00004411** The P4 register values beginning with P3 form an unpacked index
4412** key that omits the ROWID. Compare this key value against the index
4413** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004414**
danielk197761dd5832008-04-18 11:31:12 +00004415** If the P1 index entry is greater than or equal to the key value
4416** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004417**
danielk197761dd5832008-04-18 11:31:12 +00004418** If P5 is non-zero then the key value is increased by an epsilon
4419** prior to the comparison. This make the opcode work like IdxGT except
4420** that if the key from register P3 is a prefix of the key in the cursor,
4421** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004422*/
drh3bb9b932010-08-06 02:10:00 +00004423/* Opcode: IdxLT P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004424**
danielk197761dd5832008-04-18 11:31:12 +00004425** The P4 register values beginning with P3 form an unpacked index
4426** key that omits the ROWID. Compare this key value against the index
4427** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004428**
danielk197761dd5832008-04-18 11:31:12 +00004429** If the P1 index entry is less than the key value then jump to P2.
4430** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004431**
danielk197761dd5832008-04-18 11:31:12 +00004432** If P5 is non-zero then the key value is increased by an epsilon prior
4433** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004434*/
drh93952eb2009-11-13 19:43:43 +00004435case OP_IdxLT: /* jump */
4436case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004437 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004438 int res;
4439 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004440
drh653b82a2009-06-22 11:10:47 +00004441 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4442 pC = p->apCsr[pOp->p1];
4443 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004444 assert( pC->isOrdered );
drh9a65f2c2009-06-22 19:05:40 +00004445 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004446 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004447 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004448 assert( pOp->p4type==P4_INT32 );
4449 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004450 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004451 if( pOp->p5 ){
4452 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4453 }else{
4454 r.flags = UNPACKED_IGNORE_ROWID;
4455 }
drha6c2ed92009-11-14 23:22:23 +00004456 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004457#ifdef SQLITE_DEBUG
4458 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4459#endif
drhe63d9992008-08-13 19:11:48 +00004460 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004461 if( pOp->opcode==OP_IdxLT ){
4462 res = -res;
drha05a7222008-01-19 03:35:58 +00004463 }else{
4464 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004465 res++;
4466 }
4467 if( res>0 ){
4468 pc = pOp->p2 - 1 ;
4469 }
4470 }
4471 break;
4472}
4473
drh98757152008-01-09 23:04:12 +00004474/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004475**
4476** Delete an entire database table or index whose root page in the database
4477** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004478**
drh98757152008-01-09 23:04:12 +00004479** The table being destroyed is in the main database file if P3==0. If
4480** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004481** that is used to store tables create using CREATE TEMPORARY TABLE.
4482**
drh205f48e2004-11-05 00:43:11 +00004483** If AUTOVACUUM is enabled then it is possible that another root page
4484** might be moved into the newly deleted root page in order to keep all
4485** root pages contiguous at the beginning of the database. The former
4486** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004487** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004488** movement was required (because the table being dropped was already
4489** the last one in the database) then a zero is stored in register P2.
4490** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004491**
drhb19a2bc2001-09-16 00:13:26 +00004492** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004493*/
drh98757152008-01-09 23:04:12 +00004494case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004495 int iMoved;
drh3765df42006-06-28 18:18:09 +00004496 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004497 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004498 int iDb;
4499#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004500 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004501 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danielk1977212b2182006-06-23 14:32:08 +00004502 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4503 iCnt++;
4504 }
4505 }
drh3765df42006-06-28 18:18:09 +00004506#else
4507 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004508#endif
drh3c657212009-11-17 23:59:58 +00004509 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004510 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004511 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004512 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004513 }else{
drh856c1032009-06-02 15:21:42 +00004514 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004515 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004516 assert( (p->btreeMask & (1<<iDb))!=0 );
4517 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004518 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004519 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004520#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004521 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004522 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
drh32783152009-11-20 15:02:34 +00004523 resetSchemaOnFault = 1;
danielk1977e6efa742004-11-10 11:55:10 +00004524 }
drh3765df42006-06-28 18:18:09 +00004525#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004526 }
drh5e00f6c2001-09-13 13:46:56 +00004527 break;
4528}
4529
danielk1977c7af4842008-10-27 13:59:33 +00004530/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004531**
4532** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004533** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004534** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004535**
drhf57b3392001-10-08 13:22:32 +00004536** The table being clear is in the main database file if P2==0. If
4537** P2==1 then the table to be clear is in the auxiliary database file
4538** that is used to store tables create using CREATE TEMPORARY TABLE.
4539**
shanebe217792009-03-05 04:20:31 +00004540** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004541** intkey table (an SQL table, not an index). In this case the row change
4542** count is incremented by the number of rows in the table being cleared.
4543** If P3 is greater than zero, then the value stored in register P3 is
4544** also incremented by the number of rows in the table being cleared.
4545**
drhb19a2bc2001-09-16 00:13:26 +00004546** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004547*/
drh9cbf3422008-01-17 16:22:13 +00004548case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004549 int nChange;
4550
4551 nChange = 0;
drhfb982642007-08-30 01:19:59 +00004552 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004553 rc = sqlite3BtreeClearTable(
4554 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4555 );
4556 if( pOp->p3 ){
4557 p->nChange += nChange;
4558 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004559 assert( memIsValid(&aMem[pOp->p3]) );
4560 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004561 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004562 }
4563 }
drh5edc3122001-09-13 21:53:09 +00004564 break;
4565}
4566
drh4c583122008-01-04 22:01:03 +00004567/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004568**
drh4c583122008-01-04 22:01:03 +00004569** Allocate a new table in the main database file if P1==0 or in the
4570** auxiliary database file if P1==1 or in an attached database if
4571** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004572** register P2
drh5b2fd562001-09-13 15:21:31 +00004573**
drhc6b52df2002-01-04 03:09:29 +00004574** The difference between a table and an index is this: A table must
4575** have a 4-byte integer key and can have arbitrary data. An index
4576** has an arbitrary key but no data.
4577**
drhb19a2bc2001-09-16 00:13:26 +00004578** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004579*/
drh4c583122008-01-04 22:01:03 +00004580/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004581**
drh4c583122008-01-04 22:01:03 +00004582** Allocate a new index in the main database file if P1==0 or in the
4583** auxiliary database file if P1==1 or in an attached database if
4584** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004585** register P2.
drhf57b3392001-10-08 13:22:32 +00004586**
drhc6b52df2002-01-04 03:09:29 +00004587** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004588*/
drh4c583122008-01-04 22:01:03 +00004589case OP_CreateIndex: /* out2-prerelease */
4590case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004591 int pgno;
drhf328bc82004-05-10 23:29:49 +00004592 int flags;
drh234c39d2004-07-24 03:30:47 +00004593 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004594
4595 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004596 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004597 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004598 pDb = &db->aDb[pOp->p1];
4599 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004600 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004601 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004602 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004603 }else{
drhd4187c72010-08-30 22:15:45 +00004604 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004605 }
drh234c39d2004-07-24 03:30:47 +00004606 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004607 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004608 break;
4609}
4610
drh22645842011-03-24 01:34:03 +00004611/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004612**
4613** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004614** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004615**
4616** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004617** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004618*/
drh9cbf3422008-01-17 16:22:13 +00004619case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004620 int iDb;
4621 const char *zMaster;
4622 char *zSql;
4623 InitData initData;
4624
4625 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004626 assert( iDb>=0 && iDb<db->nDb );
danielk1977a8bbef82009-03-23 17:11:26 +00004627
drh22645842011-03-24 01:34:03 +00004628 /* Although the mutex on the BtShared object that corresponds to
danielk1977a8bbef82009-03-23 17:11:26 +00004629 ** database iDb (the database containing the sqlite_master table
4630 ** read by this instruction) is currently held, it is necessary to
4631 ** obtain the mutexes on all attached databases before checking if
4632 ** the schema of iDb is loaded. This is because, at the start of
4633 ** the sqlite3_exec() call below, SQLite will invoke
4634 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
4635 ** iDb mutex may be temporarily released to avoid deadlock. If
4636 ** this happens, then some other thread may delete the in-memory
4637 ** schema of database iDb before the SQL statement runs. The schema
4638 ** will not be reloaded becuase the db->init.busy flag is set. This
4639 ** can result in a "no such table: sqlite_master" or "malformed
4640 ** database schema" error being returned to the user.
4641 */
4642 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4643 sqlite3BtreeEnterAll(db);
drh22645842011-03-24 01:34:03 +00004644 if( ALWAYS(DbHasProperty(db, iDb, DB_SchemaLoaded)) ){
drh856c1032009-06-02 15:21:42 +00004645 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004646 initData.db = db;
4647 initData.iDb = pOp->p1;
4648 initData.pzErrMsg = &p->zErrMsg;
4649 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004650 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004651 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4652 if( zSql==0 ){
4653 rc = SQLITE_NOMEM;
4654 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004655 assert( db->init.busy==0 );
4656 db->init.busy = 1;
4657 initData.rc = SQLITE_OK;
4658 assert( !db->mallocFailed );
4659 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4660 if( rc==SQLITE_OK ) rc = initData.rc;
4661 sqlite3DbFree(db, zSql);
4662 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004663 }
drh3c23a882007-01-09 14:01:13 +00004664 }
danielk1977a8bbef82009-03-23 17:11:26 +00004665 sqlite3BtreeLeaveAll(db);
danielk1977261919c2005-12-06 12:52:59 +00004666 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004667 goto no_mem;
4668 }
drh234c39d2004-07-24 03:30:47 +00004669 break;
4670}
4671
drh8bfdf722009-06-19 14:06:03 +00004672#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004673/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004674**
4675** Read the sqlite_stat1 table for database P1 and load the content
4676** of that table into the internal index hash table. This will cause
4677** the analysis to be used when preparing all subsequent queries.
4678*/
drh9cbf3422008-01-17 16:22:13 +00004679case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004680 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4681 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004682 break;
4683}
drh8bfdf722009-06-19 14:06:03 +00004684#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004685
drh98757152008-01-09 23:04:12 +00004686/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004687**
4688** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004689** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004690** is dropped in order to keep the internal representation of the
4691** schema consistent with what is on disk.
4692*/
drh9cbf3422008-01-17 16:22:13 +00004693case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004694 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004695 break;
4696}
4697
drh98757152008-01-09 23:04:12 +00004698/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004699**
4700** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004701** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004702** is dropped in order to keep the internal representation of the
4703** schema consistent with what is on disk.
4704*/
drh9cbf3422008-01-17 16:22:13 +00004705case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004706 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004707 break;
4708}
4709
drh98757152008-01-09 23:04:12 +00004710/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004711**
4712** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004713** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004714** is dropped in order to keep the internal representation of the
4715** schema consistent with what is on disk.
4716*/
drh9cbf3422008-01-17 16:22:13 +00004717case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004718 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004719 break;
4720}
4721
drh234c39d2004-07-24 03:30:47 +00004722
drhb7f91642004-10-31 02:22:47 +00004723#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004724/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004725**
drh98757152008-01-09 23:04:12 +00004726** Do an analysis of the currently open database. Store in
4727** register P1 the text of an error message describing any problems.
4728** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004729**
drh98757152008-01-09 23:04:12 +00004730** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004731** At most reg(P3) errors will be reported.
4732** In other words, the analysis stops as soon as reg(P1) errors are
4733** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004734**
drh79069752004-05-22 21:30:40 +00004735** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004736** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004737** total.
drh21504322002-06-25 13:16:02 +00004738**
drh98757152008-01-09 23:04:12 +00004739** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004740** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004741**
drh1dcdbc02007-01-27 02:24:54 +00004742** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004743*/
drhaaab5722002-02-19 13:39:21 +00004744case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004745 int nRoot; /* Number of tables to check. (Number of root pages.) */
4746 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4747 int j; /* Loop counter */
4748 int nErr; /* Number of errors reported */
4749 char *z; /* Text of the error report */
4750 Mem *pnErr; /* Register keeping track of errors remaining */
4751
4752 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004753 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004754 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004755 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004756 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00004757 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004758 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004759 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00004760 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004761 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004762 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004763 }
4764 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004765 assert( pOp->p5<db->nDb );
4766 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4767 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004768 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004769 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004770 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004771 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004772 if( nErr==0 ){
4773 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004774 }else if( z==0 ){
4775 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004776 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004777 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004778 }
drhb7654112008-01-12 12:48:07 +00004779 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004780 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004781 break;
4782}
drhb7f91642004-10-31 02:22:47 +00004783#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004784
drh3d4501e2008-12-04 20:40:10 +00004785/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004786**
drh3d4501e2008-12-04 20:40:10 +00004787** Insert the integer value held by register P2 into a boolean index
4788** held in register P1.
4789**
4790** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004791*/
drh93952eb2009-11-13 19:43:43 +00004792case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00004793 pIn1 = &aMem[pOp->p1];
4794 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00004795 assert( (pIn2->flags & MEM_Int)!=0 );
4796 if( (pIn1->flags & MEM_RowSet)==0 ){
4797 sqlite3VdbeMemSetRowSet(pIn1);
4798 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004799 }
drh93952eb2009-11-13 19:43:43 +00004800 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00004801 break;
4802}
4803
4804/* Opcode: RowSetRead P1 P2 P3 * *
4805**
4806** Extract the smallest value from boolean index P1 and put that value into
4807** register P3. Or, if boolean index P1 is initially empty, leave P3
4808** unchanged and jump to instruction P2.
4809*/
drh93952eb2009-11-13 19:43:43 +00004810case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00004811 i64 val;
drh3d4501e2008-12-04 20:40:10 +00004812 CHECK_FOR_INTERRUPT;
drh3c657212009-11-17 23:59:58 +00004813 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00004814 if( (pIn1->flags & MEM_RowSet)==0
4815 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00004816 ){
4817 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00004818 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00004819 pc = pOp->p2 - 1;
4820 }else{
4821 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00004822 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00004823 }
drh5e00f6c2001-09-13 13:46:56 +00004824 break;
4825}
4826
drh1b26c7c2009-04-22 02:15:47 +00004827/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00004828**
drhade97602009-04-21 15:05:18 +00004829** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00004830** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00004831** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00004832** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00004833** next opcode.
danielk19771d461462009-04-21 09:02:45 +00004834**
drh1b26c7c2009-04-22 02:15:47 +00004835** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00004836** of integers, where each set contains no duplicates. Each set
4837** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00004838** must have P4==0, the final set P4=-1. P4 must be either -1 or
4839** non-negative. For non-negative values of P4 only the lower 4
4840** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00004841**
4842** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00004843** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00004844** (b) when P4==-1 there is no need to insert the value, as it will
4845** never be tested for, and (c) when a value that is part of set X is
4846** inserted, there is no need to search to see if the same value was
4847** previously inserted as part of set X (only if it was previously
4848** inserted as part of some other set).
4849*/
drh1b26c7c2009-04-22 02:15:47 +00004850case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00004851 int iSet;
4852 int exists;
4853
drh3c657212009-11-17 23:59:58 +00004854 pIn1 = &aMem[pOp->p1];
4855 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00004856 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00004857 assert( pIn3->flags&MEM_Int );
4858
drh1b26c7c2009-04-22 02:15:47 +00004859 /* If there is anything other than a rowset object in memory cell P1,
4860 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00004861 */
drh733bf1b2009-04-22 00:47:00 +00004862 if( (pIn1->flags & MEM_RowSet)==0 ){
4863 sqlite3VdbeMemSetRowSet(pIn1);
4864 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00004865 }
4866
4867 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00004868 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00004869 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00004870 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
4871 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00004872 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004873 if( exists ){
4874 pc = pOp->p2 - 1;
4875 break;
4876 }
4877 }
4878 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00004879 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004880 }
4881 break;
4882}
4883
drh5e00f6c2001-09-13 13:46:56 +00004884
danielk197793758c82005-01-21 08:13:14 +00004885#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00004886
4887/* Opcode: Program P1 P2 P3 P4 *
4888**
dan76d462e2009-08-30 11:42:51 +00004889** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00004890**
dan76d462e2009-08-30 11:42:51 +00004891** P1 contains the address of the memory cell that contains the first memory
4892** cell in an array of values used as arguments to the sub-program. P2
4893** contains the address to jump to if the sub-program throws an IGNORE
4894** exception using the RAISE() function. Register P3 contains the address
4895** of a memory cell in this (the parent) VM that is used to allocate the
4896** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00004897**
4898** P4 is a pointer to the VM containing the trigger program.
4899*/
dan76d462e2009-08-30 11:42:51 +00004900case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00004901 int nMem; /* Number of memory registers for sub-program */
4902 int nByte; /* Bytes of runtime space required for sub-program */
4903 Mem *pRt; /* Register to allocate runtime space */
4904 Mem *pMem; /* Used to iterate through memory cells */
4905 Mem *pEnd; /* Last memory cell in new array */
4906 VdbeFrame *pFrame; /* New vdbe frame to execute in */
4907 SubProgram *pProgram; /* Sub-program to execute */
4908 void *t; /* Token identifying trigger */
4909
4910 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00004911 pRt = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004912 assert( memIsValid(pRt) );
dan165921a2009-08-28 18:53:45 +00004913 assert( pProgram->nOp>0 );
4914
dan1da40a32009-09-19 17:00:31 +00004915 /* If the p5 flag is clear, then recursive invocation of triggers is
4916 ** disabled for backwards compatibility (p5 is set if this sub-program
4917 ** is really a trigger, not a foreign key action, and the flag set
4918 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00004919 **
4920 ** It is recursive invocation of triggers, at the SQL level, that is
4921 ** disabled. In some cases a single trigger may generate more than one
4922 ** SubProgram (if the trigger may be executed with more than one different
4923 ** ON CONFLICT algorithm). SubProgram structures associated with a
4924 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00004925 ** variable. */
4926 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00004927 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00004928 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
4929 if( pFrame ) break;
4930 }
4931
danf5894502009-10-07 18:41:19 +00004932 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00004933 rc = SQLITE_ERROR;
4934 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
4935 break;
4936 }
4937
4938 /* Register pRt is used to store the memory required to save the state
4939 ** of the current program, and the memory required at runtime to execute
4940 ** the trigger program. If this trigger has been fired before, then pRt
4941 ** is already allocated. Otherwise, it must be initialized. */
4942 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00004943 /* SubProgram.nMem is set to the number of memory cells used by the
4944 ** program stored in SubProgram.aOp. As well as these, one memory
4945 ** cell is required for each cursor used by the program. Set local
4946 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
4947 */
dan65a7cd12009-09-01 12:16:01 +00004948 nMem = pProgram->nMem + pProgram->nCsr;
4949 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00004950 + nMem * sizeof(Mem)
4951 + pProgram->nCsr * sizeof(VdbeCursor *);
4952 pFrame = sqlite3DbMallocZero(db, nByte);
4953 if( !pFrame ){
4954 goto no_mem;
4955 }
4956 sqlite3VdbeMemRelease(pRt);
4957 pRt->flags = MEM_Frame;
4958 pRt->u.pFrame = pFrame;
4959
4960 pFrame->v = p;
4961 pFrame->nChildMem = nMem;
4962 pFrame->nChildCsr = pProgram->nCsr;
4963 pFrame->pc = pc;
4964 pFrame->aMem = p->aMem;
4965 pFrame->nMem = p->nMem;
4966 pFrame->apCsr = p->apCsr;
4967 pFrame->nCursor = p->nCursor;
4968 pFrame->aOp = p->aOp;
4969 pFrame->nOp = p->nOp;
4970 pFrame->token = pProgram->token;
4971
4972 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
4973 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
4974 pMem->flags = MEM_Null;
4975 pMem->db = db;
4976 }
4977 }else{
4978 pFrame = pRt->u.pFrame;
4979 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
4980 assert( pProgram->nCsr==pFrame->nChildCsr );
4981 assert( pc==pFrame->pc );
4982 }
4983
4984 p->nFrame++;
4985 pFrame->pParent = p->pFrame;
dan76d462e2009-08-30 11:42:51 +00004986 pFrame->lastRowid = db->lastRowid;
4987 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00004988 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00004989 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00004990 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00004991 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00004992 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00004993 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00004994 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00004995 p->nOp = pProgram->nOp;
4996 pc = -1;
4997
4998 break;
4999}
5000
dan76d462e2009-08-30 11:42:51 +00005001/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005002**
dan76d462e2009-08-30 11:42:51 +00005003** This opcode is only ever present in sub-programs called via the
5004** OP_Program instruction. Copy a value currently stored in a memory
5005** cell of the calling (parent) frame to cell P2 in the current frames
5006** address space. This is used by trigger programs to access the new.*
5007** and old.* values.
dan165921a2009-08-28 18:53:45 +00005008**
dan76d462e2009-08-30 11:42:51 +00005009** The address of the cell in the parent frame is determined by adding
5010** the value of the P1 argument to the value of the P1 argument to the
5011** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005012*/
dan76d462e2009-08-30 11:42:51 +00005013case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005014 VdbeFrame *pFrame;
5015 Mem *pIn;
5016 pFrame = p->pFrame;
5017 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005018 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5019 break;
5020}
5021
danielk197793758c82005-01-21 08:13:14 +00005022#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005023
dan1da40a32009-09-19 17:00:31 +00005024#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005025/* Opcode: FkCounter P1 P2 * * *
dan1da40a32009-09-19 17:00:31 +00005026**
dan0ff297e2009-09-25 17:03:14 +00005027** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5028** If P1 is non-zero, the database constraint counter is incremented
5029** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005030** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005031*/
dan32b09f22009-09-23 17:29:59 +00005032case OP_FkCounter: {
dan0ff297e2009-09-25 17:03:14 +00005033 if( pOp->p1 ){
5034 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005035 }else{
dan0ff297e2009-09-25 17:03:14 +00005036 p->nFkConstraint += pOp->p2;
5037 }
5038 break;
5039}
5040
5041/* Opcode: FkIfZero P1 P2 * * *
5042**
5043** This opcode tests if a foreign key constraint-counter is currently zero.
5044** If so, jump to instruction P2. Otherwise, fall through to the next
5045** instruction.
5046**
5047** If P1 is non-zero, then the jump is taken if the database constraint-counter
5048** is zero (the one that counts deferred constraint violations). If P1 is
5049** zero, the jump is taken if the statement constraint-counter is zero
5050** (immediate foreign key constraint violations).
5051*/
5052case OP_FkIfZero: { /* jump */
5053 if( pOp->p1 ){
5054 if( db->nDeferredCons==0 ) pc = pOp->p2-1;
5055 }else{
5056 if( p->nFkConstraint==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005057 }
dan1da40a32009-09-19 17:00:31 +00005058 break;
5059}
5060#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5061
drh205f48e2004-11-05 00:43:11 +00005062#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005063/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00005064**
dan76d462e2009-08-30 11:42:51 +00005065** P1 is a register in the root frame of this VM (the root frame is
5066** different from the current frame if this instruction is being executed
5067** within a sub-program). Set the value of register P1 to the maximum of
5068** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005069**
5070** This instruction throws an error if the memory cell is not initially
5071** an integer.
5072*/
dan76d462e2009-08-30 11:42:51 +00005073case OP_MemMax: { /* in2 */
5074 Mem *pIn1;
5075 VdbeFrame *pFrame;
5076 if( p->pFrame ){
5077 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5078 pIn1 = &pFrame->aMem[pOp->p1];
5079 }else{
drha6c2ed92009-11-14 23:22:23 +00005080 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005081 }
drh2b4ded92010-09-27 21:09:31 +00005082 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005083 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005084 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005085 sqlite3VdbeMemIntegerify(pIn2);
5086 if( pIn1->u.i<pIn2->u.i){
5087 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005088 }
5089 break;
5090}
5091#endif /* SQLITE_OMIT_AUTOINCREMENT */
5092
drh98757152008-01-09 23:04:12 +00005093/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00005094**
drh98757152008-01-09 23:04:12 +00005095** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005096**
drh98757152008-01-09 23:04:12 +00005097** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005098** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005099*/
drh9cbf3422008-01-17 16:22:13 +00005100case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005101 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005102 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005103 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005104 pc = pOp->p2 - 1;
5105 }
5106 break;
5107}
5108
drh98757152008-01-09 23:04:12 +00005109/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00005110**
drh98757152008-01-09 23:04:12 +00005111** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005112**
drh98757152008-01-09 23:04:12 +00005113** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005114** not contain an integer. An assertion fault will result if you try.
5115*/
drh9cbf3422008-01-17 16:22:13 +00005116case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005117 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005118 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005119 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005120 pc = pOp->p2 - 1;
5121 }
5122 break;
5123}
5124
drh9b918ed2009-11-12 03:13:26 +00005125/* Opcode: IfZero P1 P2 P3 * *
drhec7429a2005-10-06 16:53:14 +00005126**
drh9b918ed2009-11-12 03:13:26 +00005127** The register P1 must contain an integer. Add literal P3 to the
5128** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005129**
drh98757152008-01-09 23:04:12 +00005130** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005131** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005132*/
drh9cbf3422008-01-17 16:22:13 +00005133case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005134 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005135 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005136 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005137 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005138 pc = pOp->p2 - 1;
5139 }
5140 break;
5141}
5142
drh98757152008-01-09 23:04:12 +00005143/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00005144**
drh0bce8352002-02-28 00:41:10 +00005145** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005146** function has P5 arguments. P4 is a pointer to the FuncDef
5147** structure that specifies the function. Use register
5148** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005149**
drh98757152008-01-09 23:04:12 +00005150** The P5 arguments are taken from register P2 and its
5151** successors.
drhe5095352002-02-24 03:25:14 +00005152*/
drh9cbf3422008-01-17 16:22:13 +00005153case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005154 int n;
drhe5095352002-02-24 03:25:14 +00005155 int i;
drhc54a6172009-06-02 16:06:03 +00005156 Mem *pMem;
5157 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005158 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005159 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005160
drh856c1032009-06-02 15:21:42 +00005161 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005162 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005163 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005164 apVal = p->apArg;
5165 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005166 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005167 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005168 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005169 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005170 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005171 }
danielk19772dca4ac2008-01-03 11:50:29 +00005172 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00005173 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005174 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005175 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005176 ctx.s.flags = MEM_Null;
5177 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005178 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005179 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005180 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005181 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005182 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00005183 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005184 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005185 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005186 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005187 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005188 }
drhee9ff672010-09-03 18:50:48 +00005189 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005190 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005191 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005192 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005193 }
drh90669c12006-01-20 15:45:36 +00005194 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00005195 break;
5196}
5197
drh98757152008-01-09 23:04:12 +00005198/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00005199**
drh13449892005-09-07 21:22:45 +00005200** Execute the finalizer function for an aggregate. P1 is
5201** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005202**
5203** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005204** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005205** argument is not used by this opcode. It is only there to disambiguate
5206** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005207** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005208** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005209*/
drh9cbf3422008-01-17 16:22:13 +00005210case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005211 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00005212 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005213 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005214 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005215 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005216 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005217 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005218 }
drh2dca8682008-03-21 17:13:13 +00005219 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005220 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005221 if( sqlite3VdbeMemTooBig(pMem) ){
5222 goto too_big;
5223 }
drh5e00f6c2001-09-13 13:46:56 +00005224 break;
5225}
5226
dan5cf53532010-05-01 16:40:20 +00005227#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005228/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005229**
5230** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005231** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005232** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5233** SQLITE_BUSY or not, respectively. Write the number of pages in the
5234** WAL after the checkpoint into mem[P3+1] and the number of pages
5235** in the WAL that have been checkpointed after the checkpoint
5236** completes into mem[P3+2]. However on an error, mem[P3+1] and
5237** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005238*/
5239case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005240 int i; /* Loop counter */
5241 int aRes[3]; /* Results */
5242 Mem *pMem; /* Write results here */
5243
5244 aRes[0] = 0;
5245 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005246 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5247 || pOp->p2==SQLITE_CHECKPOINT_FULL
5248 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5249 );
drh30aa3b92011-02-07 23:56:01 +00005250 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005251 if( rc==SQLITE_BUSY ){
5252 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005253 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005254 }
drh30aa3b92011-02-07 23:56:01 +00005255 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5256 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5257 }
dan7c246102010-04-12 19:00:29 +00005258 break;
5259};
dan5cf53532010-05-01 16:40:20 +00005260#endif
drh5e00f6c2001-09-13 13:46:56 +00005261
drhcac29a62010-07-02 19:36:52 +00005262#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005263/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005264**
5265** Change the journal mode of database P1 to P3. P3 must be one of the
5266** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5267** modes (delete, truncate, persist, off and memory), this is a simple
5268** operation. No IO is required.
5269**
5270** If changing into or out of WAL mode the procedure is more complicated.
5271**
5272** Write a string containing the final journal-mode to register P2.
5273*/
drhd80b2332010-05-01 00:59:37 +00005274case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005275 Btree *pBt; /* Btree to change journal mode of */
5276 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005277 int eNew; /* New journal mode */
5278 int eOld; /* The old journal mode */
drhd80b2332010-05-01 00:59:37 +00005279 const char *zFilename; /* Name of database file for pPager */
dane04dc882010-04-20 18:53:15 +00005280
drhd80b2332010-05-01 00:59:37 +00005281 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005282 assert( eNew==PAGER_JOURNALMODE_DELETE
5283 || eNew==PAGER_JOURNALMODE_TRUNCATE
5284 || eNew==PAGER_JOURNALMODE_PERSIST
5285 || eNew==PAGER_JOURNALMODE_OFF
5286 || eNew==PAGER_JOURNALMODE_MEMORY
5287 || eNew==PAGER_JOURNALMODE_WAL
5288 || eNew==PAGER_JOURNALMODE_QUERY
5289 );
5290 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh3ebaee92010-05-06 21:37:22 +00005291
5292 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
5293 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
5294 ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
5295 ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
5296 ** is not called when the statement is prepared because it requires the
5297 ** iDb index of the database as a parameter, and the database has not
5298 ** yet been attached so that index is unavailable. We have to wait
5299 ** until runtime (now) to get the mutex on the newly attached database.
5300 ** No other mutexes are required by the ATTACH command so this is safe
5301 ** to do.
5302 */
5303 assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
5304 if( p->aMutex.nMutex==0 ){
5305 /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
5306 ** database. */
5307 sqlite3VdbeUsesBtree(p, pOp->p1);
5308 sqlite3VdbeMutexArrayEnter(p);
5309 }
dane04dc882010-04-20 18:53:15 +00005310
5311 pBt = db->aDb[pOp->p1].pBt;
5312 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005313 eOld = sqlite3PagerGetJournalMode(pPager);
5314 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5315 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005316
5317#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005318 zFilename = sqlite3PagerFilename(pPager);
dane04dc882010-04-20 18:53:15 +00005319
drhd80b2332010-05-01 00:59:37 +00005320 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005321 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005322 */
5323 if( eNew==PAGER_JOURNALMODE_WAL
drhd9e5c4f2010-05-12 18:01:39 +00005324 && (zFilename[0]==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005325 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005326 ){
drh0b9b4302010-06-11 17:01:24 +00005327 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005328 }
5329
drh0b9b4302010-06-11 17:01:24 +00005330 if( (eNew!=eOld)
5331 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5332 ){
5333 if( !db->autoCommit || db->activeVdbeCnt>1 ){
5334 rc = SQLITE_ERROR;
5335 sqlite3SetString(&p->zErrMsg, db,
5336 "cannot change %s wal mode from within a transaction",
5337 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5338 );
5339 break;
5340 }else{
5341
5342 if( eOld==PAGER_JOURNALMODE_WAL ){
5343 /* If leaving WAL mode, close the log file. If successful, the call
5344 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5345 ** file. An EXCLUSIVE lock may still be held on the database file
5346 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005347 */
drh0b9b4302010-06-11 17:01:24 +00005348 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005349 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005350 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005351 }
drh242c4f72010-06-22 14:49:39 +00005352 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5353 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5354 ** as an intermediate */
5355 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005356 }
5357
5358 /* Open a transaction on the database file. Regardless of the journal
5359 ** mode, this transaction always uses a rollback journal.
5360 */
5361 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5362 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005363 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005364 }
5365 }
5366 }
dan5cf53532010-05-01 16:40:20 +00005367#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005368
dand956efe2010-06-18 16:13:45 +00005369 if( rc ){
dand956efe2010-06-18 16:13:45 +00005370 eNew = eOld;
5371 }
drh0b9b4302010-06-11 17:01:24 +00005372 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005373
dane04dc882010-04-20 18:53:15 +00005374 pOut = &aMem[pOp->p2];
5375 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005376 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005377 pOut->n = sqlite3Strlen30(pOut->z);
5378 pOut->enc = SQLITE_UTF8;
5379 sqlite3VdbeChangeEncoding(pOut, encoding);
5380 break;
drhcac29a62010-07-02 19:36:52 +00005381};
5382#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005383
drhfdbcdee2007-03-27 14:44:50 +00005384#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005385/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005386**
5387** Vacuum the entire database. This opcode will cause other virtual
5388** machines to be created and run. It may not be called from within
5389** a transaction.
5390*/
drh9cbf3422008-01-17 16:22:13 +00005391case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00005392 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005393 break;
5394}
drh154d4b22006-09-21 11:02:16 +00005395#endif
drh6f8c91c2003-12-07 00:24:35 +00005396
danielk1977dddbcdc2007-04-26 14:42:34 +00005397#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005398/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005399**
5400** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005401** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005402** P2. Otherwise, fall through to the next instruction.
5403*/
drh9cbf3422008-01-17 16:22:13 +00005404case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005405 Btree *pBt;
5406
5407 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00005408 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00005409 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005410 rc = sqlite3BtreeIncrVacuum(pBt);
5411 if( rc==SQLITE_DONE ){
5412 pc = pOp->p2 - 1;
5413 rc = SQLITE_OK;
5414 }
5415 break;
5416}
5417#endif
5418
drh98757152008-01-09 23:04:12 +00005419/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005420**
5421** Cause precompiled statements to become expired. An expired statement
5422** fails with an error code of SQLITE_SCHEMA if it is ever executed
5423** (via sqlite3_step()).
5424**
5425** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5426** then only the currently executing statement is affected.
5427*/
drh9cbf3422008-01-17 16:22:13 +00005428case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005429 if( !pOp->p1 ){
5430 sqlite3ExpirePreparedStatements(db);
5431 }else{
5432 p->expired = 1;
5433 }
5434 break;
5435}
5436
danielk1977c00da102006-01-07 13:21:04 +00005437#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005438/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00005439**
5440** Obtain a lock on a particular table. This instruction is only used when
5441** the shared-cache feature is enabled.
5442**
danielk197796d48e92009-06-29 06:00:37 +00005443** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005444** on which the lock is acquired. A readlock is obtained if P3==0 or
5445** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005446**
5447** P2 contains the root-page of the table to lock.
5448**
drh66a51672008-01-03 00:01:23 +00005449** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005450** used to generate an error message if the lock cannot be obtained.
5451*/
drh9cbf3422008-01-17 16:22:13 +00005452case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005453 u8 isWriteLock = (u8)pOp->p3;
5454 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5455 int p1 = pOp->p1;
5456 assert( p1>=0 && p1<db->nDb );
5457 assert( (p->btreeMask & (1<<p1))!=0 );
5458 assert( isWriteLock==0 || isWriteLock==1 );
5459 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5460 if( (rc&0xFF)==SQLITE_LOCKED ){
5461 const char *z = pOp->p4.z;
5462 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5463 }
danielk1977c00da102006-01-07 13:21:04 +00005464 }
5465 break;
5466}
drhb9bb7c12006-06-11 23:41:55 +00005467#endif /* SQLITE_OMIT_SHARED_CACHE */
5468
5469#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005470/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005471**
danielk19773e3a84d2008-08-01 17:37:40 +00005472** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5473** xBegin method for that table.
5474**
5475** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005476** within a callback to a virtual table xSync() method. If it is, the error
5477** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005478*/
drh9cbf3422008-01-17 16:22:13 +00005479case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005480 VTable *pVTab;
5481 pVTab = pOp->p4.pVtab;
5482 rc = sqlite3VtabBegin(db, pVTab);
drhb9755982010-07-24 16:34:37 +00005483 if( pVTab ) importVtabErrMsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005484 break;
5485}
5486#endif /* SQLITE_OMIT_VIRTUALTABLE */
5487
5488#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005489/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005490**
drh66a51672008-01-03 00:01:23 +00005491** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005492** for that table.
5493*/
drh9cbf3422008-01-17 16:22:13 +00005494case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005495 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005496 break;
5497}
5498#endif /* SQLITE_OMIT_VIRTUALTABLE */
5499
5500#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005501/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005502**
drh66a51672008-01-03 00:01:23 +00005503** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005504** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005505*/
drh9cbf3422008-01-17 16:22:13 +00005506case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005507 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005508 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005509 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005510 break;
5511}
5512#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005513
drh9eff6162006-06-12 21:59:13 +00005514#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005515/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005516**
drh66a51672008-01-03 00:01:23 +00005517** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005518** P1 is a cursor number. This opcode opens a cursor to the virtual
5519** table and stores that cursor in P1.
5520*/
drh9cbf3422008-01-17 16:22:13 +00005521case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005522 VdbeCursor *pCur;
5523 sqlite3_vtab_cursor *pVtabCursor;
5524 sqlite3_vtab *pVtab;
5525 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005526
drh856c1032009-06-02 15:21:42 +00005527 pCur = 0;
5528 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005529 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005530 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005531 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005532 rc = pModule->xOpen(pVtab, &pVtabCursor);
drhb9755982010-07-24 16:34:37 +00005533 importVtabErrMsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005534 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005535 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005536 pVtabCursor->pVtab = pVtab;
5537
5538 /* Initialise vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005539 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005540 if( pCur ){
5541 pCur->pVtabCursor = pVtabCursor;
5542 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005543 }else{
drh17435752007-08-16 04:30:38 +00005544 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005545 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005546 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005547 }
drh9eff6162006-06-12 21:59:13 +00005548 break;
5549}
5550#endif /* SQLITE_OMIT_VIRTUALTABLE */
5551
5552#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005553/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005554**
5555** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5556** the filtered result set is empty.
5557**
drh66a51672008-01-03 00:01:23 +00005558** P4 is either NULL or a string that was generated by the xBestIndex
5559** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005560** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005561**
drh9eff6162006-06-12 21:59:13 +00005562** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005563** by P1. The integer query plan parameter to xFilter is stored in register
5564** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005565** xFilter method. Registers P3+2..P3+1+argc are the argc
5566** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005567** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005568**
danielk19776dbee812008-01-03 18:39:41 +00005569** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005570*/
drh9cbf3422008-01-17 16:22:13 +00005571case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005572 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005573 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005574 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005575 Mem *pQuery;
5576 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005577 sqlite3_vtab_cursor *pVtabCursor;
5578 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005579 VdbeCursor *pCur;
5580 int res;
5581 int i;
5582 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005583
drha6c2ed92009-11-14 23:22:23 +00005584 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005585 pArgc = &pQuery[1];
5586 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005587 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005588 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005589 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005590 pVtabCursor = pCur->pVtabCursor;
5591 pVtab = pVtabCursor->pVtab;
5592 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005593
drh9cbf3422008-01-17 16:22:13 +00005594 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005595 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005596 nArg = (int)pArgc->u.i;
5597 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005598
drh644a5292006-12-20 14:53:38 +00005599 /* Invoke the xFilter method */
5600 {
drh856c1032009-06-02 15:21:42 +00005601 res = 0;
5602 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005603 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005604 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005605 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005606 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005607
danielk1977be718892006-06-23 08:05:19 +00005608 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005609 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005610 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005611 importVtabErrMsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005612 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005613 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005614 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005615
danielk1977a298e902006-06-22 09:53:48 +00005616 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005617 pc = pOp->p2 - 1;
5618 }
5619 }
drh1d454a32008-01-31 19:34:51 +00005620 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005621
drh9eff6162006-06-12 21:59:13 +00005622 break;
5623}
5624#endif /* SQLITE_OMIT_VIRTUALTABLE */
5625
5626#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005627/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005628**
drh2133d822008-01-03 18:44:59 +00005629** Store the value of the P2-th column of
5630** the row of the virtual-table that the
5631** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005632*/
5633case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005634 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005635 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005636 Mem *pDest;
5637 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005638
drhdfe88ec2008-11-03 20:55:06 +00005639 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005640 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005641 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005642 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005643 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005644 if( pCur->nullRow ){
5645 sqlite3VdbeMemSetNull(pDest);
5646 break;
5647 }
danielk19773e3a84d2008-08-01 17:37:40 +00005648 pVtab = pCur->pVtabCursor->pVtab;
5649 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005650 assert( pModule->xColumn );
5651 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005652
5653 /* The output cell may already have a buffer allocated. Move
5654 ** the current contents to sContext.s so in case the user-function
5655 ** can use the already allocated buffer instead of allocating a
5656 ** new one.
5657 */
5658 sqlite3VdbeMemMove(&sContext.s, pDest);
5659 MemSetTypeFlag(&sContext.s, MEM_Null);
5660
drhde4fcfd2008-01-19 23:50:26 +00005661 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
drhb9755982010-07-24 16:34:37 +00005662 importVtabErrMsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005663 if( sContext.isError ){
5664 rc = sContext.isError;
5665 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005666
drhde4fcfd2008-01-19 23:50:26 +00005667 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005668 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005669 ** dynamic allocation in sContext.s (a Mem struct) is released.
5670 */
5671 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005672 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005673 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005674 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005675
drhde4fcfd2008-01-19 23:50:26 +00005676 if( sqlite3VdbeMemTooBig(pDest) ){
5677 goto too_big;
5678 }
drh9eff6162006-06-12 21:59:13 +00005679 break;
5680}
5681#endif /* SQLITE_OMIT_VIRTUALTABLE */
5682
5683#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005684/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005685**
5686** Advance virtual table P1 to the next row in its result set and
5687** jump to instruction P2. Or, if the virtual table has reached
5688** the end of its result set, then fall through to the next instruction.
5689*/
drh9cbf3422008-01-17 16:22:13 +00005690case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005691 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005692 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005693 int res;
drh856c1032009-06-02 15:21:42 +00005694 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005695
drhc54a6172009-06-02 16:06:03 +00005696 res = 0;
drh856c1032009-06-02 15:21:42 +00005697 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005698 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005699 if( pCur->nullRow ){
5700 break;
5701 }
danielk19773e3a84d2008-08-01 17:37:40 +00005702 pVtab = pCur->pVtabCursor->pVtab;
5703 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005704 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005705
drhde4fcfd2008-01-19 23:50:26 +00005706 /* Invoke the xNext() method of the module. There is no way for the
5707 ** underlying implementation to return an error if one occurs during
5708 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5709 ** data is available) and the error code returned when xColumn or
5710 ** some other method is next invoked on the save virtual table cursor.
5711 */
drhde4fcfd2008-01-19 23:50:26 +00005712 p->inVtabMethod = 1;
5713 rc = pModule->xNext(pCur->pVtabCursor);
5714 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005715 importVtabErrMsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005716 if( rc==SQLITE_OK ){
5717 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005718 }
5719
drhde4fcfd2008-01-19 23:50:26 +00005720 if( !res ){
5721 /* If there is data, jump to P2 */
5722 pc = pOp->p2 - 1;
5723 }
drh9eff6162006-06-12 21:59:13 +00005724 break;
5725}
5726#endif /* SQLITE_OMIT_VIRTUALTABLE */
5727
danielk1977182c4ba2007-06-27 15:53:34 +00005728#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005729/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005730**
drh66a51672008-01-03 00:01:23 +00005731** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005732** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005733** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005734*/
drh9cbf3422008-01-17 16:22:13 +00005735case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00005736 sqlite3_vtab *pVtab;
5737 Mem *pName;
5738
danielk1977595a5232009-07-24 17:58:53 +00005739 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00005740 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005741 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00005742 assert( memIsValid(pName) );
drh5b6afba2008-01-05 16:29:28 +00005743 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00005744 assert( pName->flags & MEM_Str );
danielk19776dbee812008-01-03 18:39:41 +00005745 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhb9755982010-07-24 16:34:37 +00005746 importVtabErrMsg(p, pVtab);
dana235d0c2010-08-24 16:59:47 +00005747 p->expired = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005748
danielk1977182c4ba2007-06-27 15:53:34 +00005749 break;
5750}
5751#endif
drh4cbdda92006-06-14 19:00:20 +00005752
5753#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005754/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005755**
drh66a51672008-01-03 00:01:23 +00005756** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005757** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005758** are contiguous memory cells starting at P3 to pass to the xUpdate
5759** invocation. The value in register (P3+P2-1) corresponds to the
5760** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005761**
5762** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005763** The argv[0] element (which corresponds to memory cell P3)
5764** is the rowid of a row to delete. If argv[0] is NULL then no
5765** deletion occurs. The argv[1] element is the rowid of the new
5766** row. This can be NULL to have the virtual table select the new
5767** rowid for itself. The subsequent elements in the array are
5768** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005769**
5770** If P2==1 then no insert is performed. argv[0] is the rowid of
5771** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005772**
5773** P1 is a boolean flag. If it is set to true and the xUpdate call
5774** is successful, then the value returned by sqlite3_last_insert_rowid()
5775** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005776*/
drh9cbf3422008-01-17 16:22:13 +00005777case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00005778 sqlite3_vtab *pVtab;
5779 sqlite3_module *pModule;
5780 int nArg;
5781 int i;
5782 sqlite_int64 rowid;
5783 Mem **apArg;
5784 Mem *pX;
5785
danielk1977595a5232009-07-24 17:58:53 +00005786 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005787 pModule = (sqlite3_module *)pVtab->pModule;
5788 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005789 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00005790 if( ALWAYS(pModule->xUpdate) ){
drh856c1032009-06-02 15:21:42 +00005791 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00005792 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00005793 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00005794 assert( memIsValid(pX) );
5795 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00005796 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00005797 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005798 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005799 }
danielk19771f6eec52006-06-16 06:17:47 +00005800 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drhb9755982010-07-24 16:34:37 +00005801 importVtabErrMsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00005802 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00005803 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5804 db->lastRowid = rowid;
5805 }
drhb5df1442008-04-10 14:00:09 +00005806 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00005807 }
drh4cbdda92006-06-14 19:00:20 +00005808 break;
danielk1977399918f2006-06-14 13:03:23 +00005809}
5810#endif /* SQLITE_OMIT_VIRTUALTABLE */
5811
danielk197759a93792008-05-15 17:48:20 +00005812#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5813/* Opcode: Pagecount P1 P2 * * *
5814**
5815** Write the current number of pages in database P1 to memory cell P2.
5816*/
5817case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00005818 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00005819 break;
5820}
5821#endif
5822
drh60ac3f42010-11-23 18:59:27 +00005823
5824#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5825/* Opcode: MaxPgcnt P1 P2 P3 * *
5826**
5827** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00005828** Do not let the maximum page count fall below the current page count and
5829** do not change the maximum page count value if P3==0.
5830**
drh60ac3f42010-11-23 18:59:27 +00005831** Store the maximum page count after the change in register P2.
5832*/
5833case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00005834 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00005835 Btree *pBt;
5836
5837 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00005838 newMax = 0;
5839 if( pOp->p3 ){
5840 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00005841 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00005842 }
5843 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00005844 break;
5845}
5846#endif
5847
5848
drh949f9cd2008-01-12 21:35:57 +00005849#ifndef SQLITE_OMIT_TRACE
5850/* Opcode: Trace * * * P4 *
5851**
5852** If tracing is enabled (by the sqlite3_trace()) interface, then
5853** the UTF-8 string contained in P4 is emitted on the trace callback.
5854*/
5855case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00005856 char *zTrace;
5857
5858 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
danielk19776ab3a2e2009-02-19 14:39:25 +00005859 if( zTrace ){
drh949f9cd2008-01-12 21:35:57 +00005860 if( db->xTrace ){
drhc7bc4fd2009-11-25 18:03:42 +00005861 char *z = sqlite3VdbeExpandSql(p, zTrace);
5862 db->xTrace(db->pTraceArg, z);
5863 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00005864 }
5865#ifdef SQLITE_DEBUG
5866 if( (db->flags & SQLITE_SqlTrace)!=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00005867 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
drh949f9cd2008-01-12 21:35:57 +00005868 }
5869#endif /* SQLITE_DEBUG */
5870 }
5871 break;
5872}
5873#endif
5874
drh91fd4d42008-01-19 20:11:25 +00005875
5876/* Opcode: Noop * * * * *
5877**
5878** Do nothing. This instruction is often useful as a jump
5879** destination.
drh5e00f6c2001-09-13 13:46:56 +00005880*/
drh91fd4d42008-01-19 20:11:25 +00005881/*
5882** The magic Explain opcode are only inserted when explain==2 (which
5883** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5884** This opcode records information from the optimizer. It is the
5885** the same as a no-op. This opcodesnever appears in a real VM program.
5886*/
5887default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00005888 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00005889 break;
5890}
5891
5892/*****************************************************************************
5893** The cases of the switch statement above this line should all be indented
5894** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5895** readability. From this point on down, the normal indentation rules are
5896** restored.
5897*****************************************************************************/
5898 }
drh6e142f52000-06-08 13:36:40 +00005899
drh7b396862003-01-01 23:06:20 +00005900#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005901 {
shane9bcbdad2008-05-29 20:22:37 +00005902 u64 elapsed = sqlite3Hwtime() - start;
5903 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005904 pOp->cnt++;
5905#if 0
shane9bcbdad2008-05-29 20:22:37 +00005906 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00005907 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005908#endif
5909 }
drh7b396862003-01-01 23:06:20 +00005910#endif
5911
drh6e142f52000-06-08 13:36:40 +00005912 /* The following code adds nothing to the actual functionality
5913 ** of the program. It is only here for testing and debugging.
5914 ** On the other hand, it does burn CPU cycles every time through
5915 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5916 */
5917#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005918 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005919
drhcf1023c2007-05-08 20:59:49 +00005920#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005921 if( p->trace ){
5922 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00005923 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
5924 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00005925 }
drh3c657212009-11-17 23:59:58 +00005926 if( pOp->opflags & OPFLG_OUT3 ){
5927 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00005928 }
drh75897232000-05-29 14:26:00 +00005929 }
danielk1977b5402fb2005-01-12 07:15:04 +00005930#endif /* SQLITE_DEBUG */
5931#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005932 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005933
drha05a7222008-01-19 03:35:58 +00005934 /* If we reach this point, it means that execution is finished with
5935 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005936 */
drha05a7222008-01-19 03:35:58 +00005937vdbe_error_halt:
5938 assert( rc );
5939 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00005940 testcase( sqlite3GlobalConfig.xLog!=0 );
5941 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
5942 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00005943 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005944 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5945 rc = SQLITE_ERROR;
drh32783152009-11-20 15:02:34 +00005946 if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
drh900b31e2007-08-28 02:27:51 +00005947
5948 /* This is the only way out of this procedure. We have to
5949 ** release the mutexes on btrees that were acquired at the
5950 ** top. */
5951vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005952 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005953 return rc;
5954
drh023ae032007-05-08 12:12:16 +00005955 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5956 ** is encountered.
5957 */
5958too_big:
drhf089aa42008-07-08 19:34:06 +00005959 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005960 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005961 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005962
drh98640a32007-06-07 19:08:32 +00005963 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005964 */
5965no_mem:
drh17435752007-08-16 04:30:38 +00005966 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005967 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005968 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005969 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005970
drhb86ccfb2003-01-28 23:13:10 +00005971 /* Jump to here for any other kind of fatal error. The "rc" variable
5972 ** should hold the error number.
5973 */
5974abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005975 assert( p->zErrMsg==0 );
5976 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005977 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005978 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005979 }
drha05a7222008-01-19 03:35:58 +00005980 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005981
danielk19776f8a5032004-05-10 10:34:51 +00005982 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005983 ** flag.
5984 */
5985abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005986 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005987 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005988 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005989 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005990 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005991}