blob: 14785d35d54d5bb361cbc035575bd4037b08d0bb [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/*
drh487ab3c2001-11-08 00:45:21 +000050** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000051** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000052** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000053** working correctly. This variable has no function other than to
54** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000055*/
drh0f7eb612006-08-08 13:51:43 +000056#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000057int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000058#endif
drh487ab3c2001-11-08 00:45:21 +000059
drhf6038712004-02-08 18:07:34 +000060/*
61** When this global variable is positive, it gets decremented once before
drh881feaa2006-07-26 01:39:30 +000062** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
63** field of the sqlite3 structure is set in order to simulate and interrupt.
drhf6038712004-02-08 18:07:34 +000064**
65** This facility is used for testing purposes only. It does not function
66** in an ordinary build.
67*/
drh0f7eb612006-08-08 13:51:43 +000068#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000069int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000070#endif
drh1350b032002-02-27 19:00:20 +000071
danielk19777e18c252004-05-25 11:47:24 +000072/*
drh6bf89572004-11-03 16:27:01 +000073** The next global variable is incremented each type the OP_Sort opcode
74** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000075** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000076** has no function other than to help verify the correct operation of the
77** library.
78*/
drh0f7eb612006-08-08 13:51:43 +000079#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000080int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000081#endif
drh6bf89572004-11-03 16:27:01 +000082
83/*
drhae7e1512007-05-02 16:51:59 +000084** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000085** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000086** use this information to make sure that the zero-blob functionality
87** is working correctly. This variable has no function other than to
88** help verify the correct operation of the library.
89*/
90#ifdef SQLITE_TEST
91int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +000092static void updateMaxBlobsize(Mem *p){
93 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
94 sqlite3_max_blobsize = p->n;
95 }
96}
drhae7e1512007-05-02 16:51:59 +000097#endif
98
99/*
dan0ff297e2009-09-25 17:03:14 +0000100** The next global variable is incremented each type the OP_Found opcode
101** is executed. This is used to test whether or not the foreign key
102** operation implemented using OP_FkIsZero is working. This variable
103** has no function other than to help verify the correct operation of the
104** library.
105*/
106#ifdef SQLITE_TEST
107int sqlite3_found_count = 0;
108#endif
109
110/*
drhb7654112008-01-12 12:48:07 +0000111** Test a register to see if it exceeds the current maximum blob size.
112** If it does, record the new maximum blob size.
113*/
drh678ccce2008-03-31 18:19:54 +0000114#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000115# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000116#else
117# define UPDATE_MAX_BLOBSIZE(P)
118#endif
119
120/*
drh9cbf3422008-01-17 16:22:13 +0000121** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000122** already. Return non-zero if a malloc() fails.
123*/
drhb21c8cd2007-08-21 19:33:56 +0000124#define Stringify(P, enc) \
125 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000126 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000127
128/*
danielk1977bd7e4602004-05-24 07:34:48 +0000129** An ephemeral string value (signified by the MEM_Ephem flag) contains
130** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000131** is responsible for deallocating that string. Because the register
132** does not control the string, it might be deleted without the register
133** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000134**
135** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000136** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000137** converts an MEM_Ephem string into an MEM_Dyn string.
138*/
drhb21c8cd2007-08-21 19:33:56 +0000139#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000140 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000141 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000142
143/*
danielk19771cc5ed82007-05-16 17:28:43 +0000144** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
145** P if required.
146*/
drhb21c8cd2007-08-21 19:33:56 +0000147#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +0000148
149/*
shane21e7feb2008-05-30 15:59:49 +0000150** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000151** user-defined function or returned to the user as the result of a query.
dan937d0de2009-10-15 18:35:38 +0000152** This routine sets the pMem->type variable used by the sqlite3_value_*()
153** routines.
danielk1977c572ef72004-05-27 09:28:41 +0000154*/
dan937d0de2009-10-15 18:35:38 +0000155void sqlite3VdbeMemStoreType(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000156 int flags = pMem->flags;
157 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000158 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000159 }
160 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000161 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000162 }
163 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000164 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000165 }
166 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000167 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000168 }else{
drh9c054832004-05-31 18:51:57 +0000169 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000170 }
171}
danielk19778a6b5412004-05-24 07:04:25 +0000172
173/*
drhdfe88ec2008-11-03 20:55:06 +0000174** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000175** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000176*/
drhdfe88ec2008-11-03 20:55:06 +0000177static VdbeCursor *allocateCursor(
178 Vdbe *p, /* The virtual machine */
179 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000180 int nField, /* Number of fields in the table or index */
drh3d4501e2008-12-04 20:40:10 +0000181 int iDb, /* When database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000182 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000183){
184 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000185 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000186 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000187 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000188 **
189 ** * Sometimes cursor numbers are used for a couple of different
190 ** purposes in a vdbe program. The different uses might require
191 ** different sized allocations. Memory cells provide growable
192 ** allocations.
193 **
194 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
195 ** be freed lazily via the sqlite3_release_memory() API. This
196 ** minimizes the number of malloc calls made by the system.
197 **
198 ** Memory cells for cursors are allocated at the top of the address
199 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
200 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
201 */
202 Mem *pMem = &p->aMem[p->nMem-iCur];
203
danielk19775f096132008-03-28 15:44:09 +0000204 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000205 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000206 nByte =
drhc54055b2009-11-13 17:05:53 +0000207 ROUND8(sizeof(VdbeCursor)) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000208 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
209 2*nField*sizeof(u32);
210
drh290c1942004-08-21 17:54:45 +0000211 assert( iCur<p->nCursor );
212 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000213 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000214 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000215 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000216 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000217 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000218 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000219 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000220 pCx->nField = nField;
221 if( nField ){
drhc54055b2009-11-13 17:05:53 +0000222 pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
danielk1977cd3e8f72008-03-25 09:47:35 +0000223 }
224 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000225 pCx->pCursor = (BtCursor*)
drhc54055b2009-11-13 17:05:53 +0000226 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
drhf25a5072009-11-18 23:01:25 +0000227 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 }
danielk197794eb6a12005-12-15 15:22:08 +0000229 }
drh4774b132004-06-12 20:12:51 +0000230 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000231}
232
danielk19773d1bfea2004-05-14 11:00:53 +0000233/*
drh29d72102006-02-09 22:13:41 +0000234** Try to convert a value into a numeric representation if we can
235** do so without loss of information. In other words, if the string
236** looks like a number, convert it into a number. If it does not
237** look like a number, leave it alone.
238*/
drhb21c8cd2007-08-21 19:33:56 +0000239static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000240 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
241 int realnum;
danb7dca7d2010-03-05 16:32:12 +0000242 u8 enc = pRec->enc;
drhb21c8cd2007-08-21 19:33:56 +0000243 sqlite3VdbeMemNulTerminate(pRec);
danb7dca7d2010-03-05 16:32:12 +0000244 if( (pRec->flags&MEM_Str) && sqlite3IsNumber(pRec->z, &realnum, enc) ){
drh29d72102006-02-09 22:13:41 +0000245 i64 value;
danb7dca7d2010-03-05 16:32:12 +0000246 char *zUtf8 = pRec->z;
247#ifndef SQLITE_OMIT_UTF16
248 if( enc!=SQLITE_UTF8 ){
249 assert( pRec->db );
250 zUtf8 = sqlite3Utf16to8(pRec->db, pRec->z, pRec->n, enc);
251 if( !zUtf8 ) return;
252 }
253#endif
254 if( !realnum && sqlite3Atoi64(zUtf8, &value) ){
drh3c024d62007-03-30 11:23:45 +0000255 pRec->u.i = value;
danielk1977a7a8e142008-02-13 18:25:27 +0000256 MemSetTypeFlag(pRec, MEM_Int);
drh29d72102006-02-09 22:13:41 +0000257 }else{
danb7dca7d2010-03-05 16:32:12 +0000258 sqlite3AtoF(zUtf8, &pRec->r);
259 MemSetTypeFlag(pRec, MEM_Real);
drh29d72102006-02-09 22:13:41 +0000260 }
danb7dca7d2010-03-05 16:32:12 +0000261#ifndef SQLITE_OMIT_UTF16
262 if( enc!=SQLITE_UTF8 ){
263 sqlite3DbFree(pRec->db, zUtf8);
264 }
265#endif
drh29d72102006-02-09 22:13:41 +0000266 }
267 }
268}
269
270/*
drh8a512562005-11-14 22:29:05 +0000271** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000272**
drh8a512562005-11-14 22:29:05 +0000273** SQLITE_AFF_INTEGER:
274** SQLITE_AFF_REAL:
275** SQLITE_AFF_NUMERIC:
276** Try to convert pRec to an integer representation or a
277** floating-point representation if an integer representation
278** is not possible. Note that the integer representation is
279** always preferred, even if the affinity is REAL, because
280** an integer representation is more space efficient on disk.
281**
282** SQLITE_AFF_TEXT:
283** Convert pRec to a text representation.
284**
285** SQLITE_AFF_NONE:
286** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000287*/
drh17435752007-08-16 04:30:38 +0000288static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000289 Mem *pRec, /* The value to apply affinity to */
290 char affinity, /* The affinity to be applied */
291 u8 enc /* Use this text encoding */
292){
drh8a512562005-11-14 22:29:05 +0000293 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000294 /* Only attempt the conversion to TEXT if there is an integer or real
295 ** representation (blob and NULL do not get converted) but no string
296 ** representation.
297 */
298 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000299 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000300 }
301 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000302 }else if( affinity!=SQLITE_AFF_NONE ){
303 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
304 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000305 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000306 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000307 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000308 }
danielk19773d1bfea2004-05-14 11:00:53 +0000309 }
310}
311
danielk1977aee18ef2005-03-09 12:26:50 +0000312/*
drh29d72102006-02-09 22:13:41 +0000313** Try to convert the type of a function argument or a result column
314** into a numeric representation. Use either INTEGER or REAL whichever
315** is appropriate. But only do the conversion if it is possible without
316** loss of information and return the revised type of the argument.
317**
318** This is an EXPERIMENTAL api and is subject to change or removal.
319*/
320int sqlite3_value_numeric_type(sqlite3_value *pVal){
321 Mem *pMem = (Mem*)pVal;
drhb21c8cd2007-08-21 19:33:56 +0000322 applyNumericAffinity(pMem);
dan937d0de2009-10-15 18:35:38 +0000323 sqlite3VdbeMemStoreType(pMem);
drh29d72102006-02-09 22:13:41 +0000324 return pMem->type;
325}
326
327/*
danielk1977aee18ef2005-03-09 12:26:50 +0000328** Exported version of applyAffinity(). This one works on sqlite3_value*,
329** not the internal Mem* type.
330*/
danielk19771e536952007-08-16 10:09:01 +0000331void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000332 sqlite3_value *pVal,
333 u8 affinity,
334 u8 enc
335){
drhb21c8cd2007-08-21 19:33:56 +0000336 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000337}
338
danielk1977b5402fb2005-01-12 07:15:04 +0000339#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000340/*
danielk1977ca6b2912004-05-21 10:49:47 +0000341** Write a nice string representation of the contents of cell pMem
342** into buffer zBuf, length nBuf.
343*/
drh74161702006-02-24 02:53:49 +0000344void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000345 char *zCsr = zBuf;
346 int f = pMem->flags;
347
drh57196282004-10-06 15:41:16 +0000348 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000349
danielk1977ca6b2912004-05-21 10:49:47 +0000350 if( f&MEM_Blob ){
351 int i;
352 char c;
353 if( f & MEM_Dyn ){
354 c = 'z';
355 assert( (f & (MEM_Static|MEM_Ephem))==0 );
356 }else if( f & MEM_Static ){
357 c = 't';
358 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
359 }else if( f & MEM_Ephem ){
360 c = 'e';
361 assert( (f & (MEM_Static|MEM_Dyn))==0 );
362 }else{
363 c = 's';
364 }
365
drh5bb3eb92007-05-04 13:15:55 +0000366 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000367 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000368 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000369 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000370 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000371 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000372 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000373 }
374 for(i=0; i<16 && i<pMem->n; i++){
375 char z = pMem->z[i];
376 if( z<32 || z>126 ) *zCsr++ = '.';
377 else *zCsr++ = z;
378 }
379
drhe718efe2007-05-10 21:14:03 +0000380 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000381 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000382 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000383 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000384 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000385 }
danielk1977b1bc9532004-05-22 03:05:33 +0000386 *zCsr = '\0';
387 }else if( f & MEM_Str ){
388 int j, k;
389 zBuf[0] = ' ';
390 if( f & MEM_Dyn ){
391 zBuf[1] = 'z';
392 assert( (f & (MEM_Static|MEM_Ephem))==0 );
393 }else if( f & MEM_Static ){
394 zBuf[1] = 't';
395 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
396 }else if( f & MEM_Ephem ){
397 zBuf[1] = 'e';
398 assert( (f & (MEM_Static|MEM_Dyn))==0 );
399 }else{
400 zBuf[1] = 's';
401 }
402 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000403 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000404 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000405 zBuf[k++] = '[';
406 for(j=0; j<15 && j<pMem->n; j++){
407 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000408 if( c>=0x20 && c<0x7f ){
409 zBuf[k++] = c;
410 }else{
411 zBuf[k++] = '.';
412 }
413 }
414 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000415 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000416 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000417 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000418 }
danielk1977ca6b2912004-05-21 10:49:47 +0000419}
420#endif
421
drh5b6afba2008-01-05 16:29:28 +0000422#ifdef SQLITE_DEBUG
423/*
424** Print the value of a register for tracing purposes:
425*/
426static void memTracePrint(FILE *out, Mem *p){
427 if( p->flags & MEM_Null ){
428 fprintf(out, " NULL");
429 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
430 fprintf(out, " si:%lld", p->u.i);
431 }else if( p->flags & MEM_Int ){
432 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000433#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000434 }else if( p->flags & MEM_Real ){
435 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000436#endif
drh733bf1b2009-04-22 00:47:00 +0000437 }else if( p->flags & MEM_RowSet ){
438 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000439 }else{
440 char zBuf[200];
441 sqlite3VdbeMemPrettyPrint(p, zBuf);
442 fprintf(out, " ");
443 fprintf(out, "%s", zBuf);
444 }
445}
446static void registerTrace(FILE *out, int iReg, Mem *p){
447 fprintf(out, "REG[%d] = ", iReg);
448 memTracePrint(out, p);
449 fprintf(out, "\n");
450}
451#endif
452
453#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000454# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000455#else
456# define REGISTER_TRACE(R,M)
457#endif
458
danielk197784ac9d02004-05-18 09:58:06 +0000459
drh7b396862003-01-01 23:06:20 +0000460#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000461
462/*
463** hwtime.h contains inline assembler code for implementing
464** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000465*/
shane9bcbdad2008-05-29 20:22:37 +0000466#include "hwtime.h"
467
drh7b396862003-01-01 23:06:20 +0000468#endif
469
drh8c74a8c2002-08-25 19:20:40 +0000470/*
drhcaec2f12003-01-07 02:47:47 +0000471** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000472** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000473** processing of the VDBE program is interrupted.
474**
475** This macro added to every instruction that does a jump in order to
476** implement a loop. This test used to be on every single instruction,
477** but that meant we more testing that we needed. By only testing the
478** flag on jump instructions, we get a (small) speed improvement.
479*/
480#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000481 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000482
483
danielk1977fd7f0452008-12-17 17:30:26 +0000484#ifndef NDEBUG
485/*
486** This function is only called from within an assert() expression. It
487** checks that the sqlite3.nTransaction variable is correctly set to
488** the number of non-transaction savepoints currently in the
489** linked list starting at sqlite3.pSavepoint.
490**
491** Usage:
492**
493** assert( checkSavepointCount(db) );
494*/
495static int checkSavepointCount(sqlite3 *db){
496 int n = 0;
497 Savepoint *p;
498 for(p=db->pSavepoint; p; p=p->pNext) n++;
499 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
500 return 1;
501}
502#endif
503
drhcaec2f12003-01-07 02:47:47 +0000504/*
drhb86ccfb2003-01-28 23:13:10 +0000505** Execute as much of a VDBE program as we can then return.
506**
danielk19774adee202004-05-08 08:23:19 +0000507** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000508** close the program with a final OP_Halt and to set up the callbacks
509** and the error message pointer.
510**
511** Whenever a row or result data is available, this routine will either
512** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000513** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000514**
515** If an attempt is made to open a locked database, then this routine
516** will either invoke the busy callback (if there is one) or it will
517** return SQLITE_BUSY.
518**
519** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000520** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000521** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
522**
523** If the callback ever returns non-zero, then the program exits
524** immediately. There will be no error message but the p->rc field is
525** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
526**
drh9468c7f2003-03-07 19:50:07 +0000527** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
528** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000529**
530** Other fatal errors return SQLITE_ERROR.
531**
danielk19774adee202004-05-08 08:23:19 +0000532** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000533** used to clean up the mess that was left behind.
534*/
danielk19774adee202004-05-08 08:23:19 +0000535int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000536 Vdbe *p /* The VDBE */
537){
shaneh84f4b2f2010-02-26 01:46:54 +0000538 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000539 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000540 Op *pOp; /* Current operation */
541 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000542 sqlite3 *db = p->db; /* The database */
drh32783152009-11-20 15:02:34 +0000543 u8 resetSchemaOnFault = 0; /* Reset schema after an error if true */
drh8079a0d2006-01-12 17:20:50 +0000544 u8 encoding = ENC(db); /* The database encoding */
drha6c2ed92009-11-14 23:22:23 +0000545#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
shaneh5e17e8b2009-12-03 04:40:47 +0000546 int checkProgress; /* True if progress callbacks are enabled */
drha6c2ed92009-11-14 23:22:23 +0000547 int nProgressOps = 0; /* Opcodes executed since progress callback. */
548#endif
549 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000550 Mem *pIn1 = 0; /* 1st input operand */
551 Mem *pIn2 = 0; /* 2nd input operand */
552 Mem *pIn3 = 0; /* 3rd input operand */
553 Mem *pOut = 0; /* Output operand */
drh0acb7e42008-06-25 00:12:41 +0000554 int iCompare = 0; /* Result of last OP_Compare operation */
shanebe217792009-03-05 04:20:31 +0000555 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drhb86ccfb2003-01-28 23:13:10 +0000556#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000557 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000558 int origPc; /* Program counter at start of opcode */
559#endif
drh856c1032009-06-02 15:21:42 +0000560 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000561
drhca48c902008-01-18 14:08:24 +0000562 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
danielk1977f7590db2009-04-10 12:55:16 +0000563 sqlite3VdbeMutexArrayEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000564 if( p->rc==SQLITE_NOMEM ){
565 /* This happens if a malloc() inside a call to sqlite3_column_text() or
566 ** sqlite3_column_text16() failed. */
567 goto no_mem;
568 }
drh3a840692003-01-29 22:58:26 +0000569 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
570 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000571 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000572 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000573 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000574 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000575 sqlite3VdbeIOTraceSql(p);
drha6c2ed92009-11-14 23:22:23 +0000576#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
577 checkProgress = db->xProgress!=0;
578#endif
drh3c23a882007-01-09 14:01:13 +0000579#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000580 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000581 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000582 int i;
583 printf("VDBE Program Listing:\n");
584 sqlite3VdbePrintSql(p);
585 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000586 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000587 }
588 }
danielk19772d1d86f2008-06-20 14:59:51 +0000589 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000590#endif
drhb86ccfb2003-01-28 23:13:10 +0000591 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000592 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000593 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000594#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000595 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000596 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000597#endif
drhbbe879d2009-11-14 18:04:35 +0000598 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000599
danielk19778b60e0f2005-01-12 09:10:39 +0000600 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000601 */
danielk19778b60e0f2005-01-12 09:10:39 +0000602#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000603 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000604 if( pc==0 ){
605 printf("VDBE Execution Trace:\n");
606 sqlite3VdbePrintSql(p);
607 }
danielk19774adee202004-05-08 08:23:19 +0000608 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000609 }
drh3f7d4e42004-07-24 14:35:58 +0000610#endif
611
drh6e142f52000-06-08 13:36:40 +0000612
drhf6038712004-02-08 18:07:34 +0000613 /* Check to see if we need to simulate an interrupt. This only happens
614 ** if we have a special test build.
615 */
616#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000617 if( sqlite3_interrupt_count>0 ){
618 sqlite3_interrupt_count--;
619 if( sqlite3_interrupt_count==0 ){
620 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000621 }
622 }
623#endif
624
danielk1977348bb5d2003-10-18 09:37:26 +0000625#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
626 /* Call the progress callback if it is configured and the required number
627 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000628 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000629 ** If the progress callback returns non-zero, exit the virtual machine with
630 ** a return code SQLITE_ABORT.
631 */
drha6c2ed92009-11-14 23:22:23 +0000632 if( checkProgress ){
drh3914aed2004-01-31 20:40:42 +0000633 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000634 int prc;
drh9978c972010-02-23 17:36:32 +0000635 prc = db->xProgress(db->pProgressArg);
danielk1977de523ac2007-06-15 14:53:53 +0000636 if( prc!=0 ){
637 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000638 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000639 }
danielk19773fe11f32007-06-13 16:49:48 +0000640 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000641 }
drh3914aed2004-01-31 20:40:42 +0000642 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000643 }
danielk1977348bb5d2003-10-18 09:37:26 +0000644#endif
645
drh3c657212009-11-17 23:59:58 +0000646 /* On any opcode with the "out2-prerelase" tag, free any
647 ** external allocations out of mem[p2] and set mem[p2] to be
648 ** an undefined integer. Opcodes will either fill in the integer
649 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000650 */
drha6c2ed92009-11-14 23:22:23 +0000651 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000652 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
653 assert( pOp->p2>0 );
654 assert( pOp->p2<=p->nMem );
655 pOut = &aMem[pOp->p2];
656 sqlite3VdbeMemReleaseExternal(pOut);
657 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000658 }
drh3c657212009-11-17 23:59:58 +0000659
660 /* Sanity checking on other operands */
661#ifdef SQLITE_DEBUG
662 if( (pOp->opflags & OPFLG_IN1)!=0 ){
663 assert( pOp->p1>0 );
664 assert( pOp->p1<=p->nMem );
665 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
666 }
667 if( (pOp->opflags & OPFLG_IN2)!=0 ){
668 assert( pOp->p2>0 );
669 assert( pOp->p2<=p->nMem );
670 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
671 }
672 if( (pOp->opflags & OPFLG_IN3)!=0 ){
673 assert( pOp->p3>0 );
674 assert( pOp->p3<=p->nMem );
675 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
676 }
677 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
678 assert( pOp->p2>0 );
679 assert( pOp->p2<=p->nMem );
680 }
681 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
682 assert( pOp->p3>0 );
683 assert( pOp->p3<=p->nMem );
684 }
685#endif
drh93952eb2009-11-13 19:43:43 +0000686
drh75897232000-05-29 14:26:00 +0000687 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000688
drh5e00f6c2001-09-13 13:46:56 +0000689/*****************************************************************************
690** What follows is a massive switch statement where each case implements a
691** separate instruction in the virtual machine. If we follow the usual
692** indentation conventions, each case should be indented by 6 spaces. But
693** that is a lot of wasted space on the left margin. So the code within
694** the switch statement will break with convention and be flush-left. Another
695** big comment (similar to this one) will mark the point in the code where
696** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000697**
698** The formatting of each case is important. The makefile for SQLite
699** generates two C files "opcodes.h" and "opcodes.c" by scanning this
700** file looking for lines that begin with "case OP_". The opcodes.h files
701** will be filled with #defines that give unique integer values to each
702** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000703** each string is the symbolic name for the corresponding opcode. If the
704** case statement is followed by a comment of the form "/# same as ... #/"
705** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000706**
drh9cbf3422008-01-17 16:22:13 +0000707** Other keywords in the comment that follows each case are used to
708** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
709** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
710** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000711**
drhac82fcf2002-09-08 17:23:41 +0000712** Documentation about VDBE opcodes is generated by scanning this file
713** for lines of that contain "Opcode:". That line and all subsequent
714** comment lines are used in the generation of the opcode.html documentation
715** file.
716**
717** SUMMARY:
718**
719** Formatting is important to scripts that scan this file.
720** Do not deviate from the formatting style currently in use.
721**
drh5e00f6c2001-09-13 13:46:56 +0000722*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000723
drh9cbf3422008-01-17 16:22:13 +0000724/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000725**
726** An unconditional jump to address P2.
727** The next instruction executed will be
728** the one at index P2 from the beginning of
729** the program.
730*/
drh9cbf3422008-01-17 16:22:13 +0000731case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000732 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000733 pc = pOp->p2 - 1;
734 break;
735}
drh75897232000-05-29 14:26:00 +0000736
drh2eb95372008-06-06 15:04:36 +0000737/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000738**
drh2eb95372008-06-06 15:04:36 +0000739** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000740** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000741*/
drh93952eb2009-11-13 19:43:43 +0000742case OP_Gosub: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +0000743 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000744 assert( (pIn1->flags & MEM_Dyn)==0 );
745 pIn1->flags = MEM_Int;
746 pIn1->u.i = pc;
747 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000748 pc = pOp->p2 - 1;
749 break;
750}
751
drh2eb95372008-06-06 15:04:36 +0000752/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000753**
drh2eb95372008-06-06 15:04:36 +0000754** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000755*/
drh2eb95372008-06-06 15:04:36 +0000756case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000757 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000758 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000759 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000760 break;
761}
762
drhe00ee6e2008-06-20 15:24:01 +0000763/* Opcode: Yield P1 * * * *
764**
765** Swap the program counter with the value in register P1.
766*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000767case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000768 int pcDest;
drh3c657212009-11-17 23:59:58 +0000769 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000770 assert( (pIn1->flags & MEM_Dyn)==0 );
771 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000772 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000773 pIn1->u.i = pc;
774 REGISTER_TRACE(pOp->p1, pIn1);
775 pc = pcDest;
776 break;
777}
778
drh5053a792009-02-20 03:02:23 +0000779/* Opcode: HaltIfNull P1 P2 P3 P4 *
780**
781** Check the value in register P3. If is is NULL then Halt using
782** parameter P1, P2, and P4 as if this were a Halt instruction. If the
783** value in register P3 is not NULL, then this routine is a no-op.
784*/
785case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000786 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000787 if( (pIn3->flags & MEM_Null)==0 ) break;
788 /* Fall through into OP_Halt */
789}
drhe00ee6e2008-06-20 15:24:01 +0000790
drh9cbf3422008-01-17 16:22:13 +0000791/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000792**
drh3d4501e2008-12-04 20:40:10 +0000793** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000794** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000795**
drh92f02c32004-09-02 14:57:08 +0000796** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
797** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
798** For errors, it can be some other value. If P1!=0 then P2 will determine
799** whether or not to rollback the current transaction. Do not rollback
800** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
801** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000802** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000803**
drh66a51672008-01-03 00:01:23 +0000804** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000805**
drh9cfcf5d2002-01-29 18:41:24 +0000806** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000807** every program. So a jump past the last instruction of the program
808** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000809*/
drh9cbf3422008-01-17 16:22:13 +0000810case OP_Halt: {
dan165921a2009-08-28 18:53:45 +0000811 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000812 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000813 VdbeFrame *pFrame = p->pFrame;
814 p->pFrame = pFrame->pParent;
815 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000816 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000817 pc = sqlite3VdbeFrameRestore(pFrame);
818 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000819 /* Instruction pc is the OP_Program that invoked the sub-program
820 ** currently being halted. If the p2 instruction of this OP_Halt
821 ** instruction is set to OE_Ignore, then the sub-program is throwing
822 ** an IGNORE exception. In this case jump to the address specified
823 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000824 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000825 }
drhbbe879d2009-11-14 18:04:35 +0000826 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000827 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000828 break;
829 }
dan2832ad42009-08-31 15:27:27 +0000830
drh92f02c32004-09-02 14:57:08 +0000831 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000832 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000833 p->pc = pc;
danielk19772dca4ac2008-01-03 11:50:29 +0000834 if( pOp->p4.z ){
drh413c3d32010-02-23 20:11:56 +0000835 assert( p->rc!=SQLITE_OK );
drhf089aa42008-07-08 19:34:06 +0000836 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhaf46dc12010-02-24 21:44:07 +0000837 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000838 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
drhcda455b2010-02-24 19:23:56 +0000839 }else if( p->rc ){
drhaf46dc12010-02-24 21:44:07 +0000840 testcase( sqlite3GlobalConfig.xLog!=0 );
drhcda455b2010-02-24 19:23:56 +0000841 sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
drh9cfcf5d2002-01-29 18:41:24 +0000842 }
drh92f02c32004-09-02 14:57:08 +0000843 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000844 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000845 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000846 p->rc = rc = SQLITE_BUSY;
847 }else{
dan1da40a32009-09-19 17:00:31 +0000848 assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
849 assert( rc==SQLITE_OK || db->nDeferredCons>0 );
drh900b31e2007-08-28 02:27:51 +0000850 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000851 }
drh900b31e2007-08-28 02:27:51 +0000852 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000853}
drhc61053b2000-06-04 12:58:36 +0000854
drh4c583122008-01-04 22:01:03 +0000855/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000856**
drh9cbf3422008-01-17 16:22:13 +0000857** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000858*/
drh4c583122008-01-04 22:01:03 +0000859case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000860 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000861 break;
862}
863
drh4c583122008-01-04 22:01:03 +0000864/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000865**
drh66a51672008-01-03 00:01:23 +0000866** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000867** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000868*/
drh4c583122008-01-04 22:01:03 +0000869case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000870 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000871 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000872 break;
873}
drh4f26d6c2004-05-26 23:25:30 +0000874
drh13573c72010-01-12 17:04:07 +0000875#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000876/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000877**
drh4c583122008-01-04 22:01:03 +0000878** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000879** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000880*/
drh4c583122008-01-04 22:01:03 +0000881case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
882 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000883 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000884 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000885 break;
886}
drh13573c72010-01-12 17:04:07 +0000887#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000888
drh3c84ddf2008-01-09 02:15:38 +0000889/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000890**
drh66a51672008-01-03 00:01:23 +0000891** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000892** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000893*/
drh4c583122008-01-04 22:01:03 +0000894case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000895 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000896 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000897 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000898
899#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000900 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000901 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
902 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000903 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000904 assert( pOut->zMalloc==pOut->z );
905 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000906 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000907 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000908 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000909 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000910 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000911 }
drh66a51672008-01-03 00:01:23 +0000912 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000913 pOp->p4.z = pOut->z;
914 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000915 }
danielk197793758c82005-01-21 08:13:14 +0000916#endif
drhbb4957f2008-03-20 14:03:29 +0000917 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000918 goto too_big;
919 }
920 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000921}
drhf4479502004-05-27 03:12:53 +0000922
drh4c583122008-01-04 22:01:03 +0000923/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000924**
drh9cbf3422008-01-17 16:22:13 +0000925** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000926*/
drh4c583122008-01-04 22:01:03 +0000927case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000928 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000929 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
930 pOut->z = pOp->p4.z;
931 pOut->n = pOp->p1;
932 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000933 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000934 break;
935}
936
drh4c583122008-01-04 22:01:03 +0000937/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000938**
drh9cbf3422008-01-17 16:22:13 +0000939** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000940*/
drh4c583122008-01-04 22:01:03 +0000941case OP_Null: { /* out2-prerelease */
drh3c657212009-11-17 23:59:58 +0000942 pOut->flags = MEM_Null;
drhf0863fe2005-06-12 21:35:51 +0000943 break;
944}
945
946
drh9de221d2008-01-05 06:51:30 +0000947/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000948**
drh9de221d2008-01-05 06:51:30 +0000949** P4 points to a blob of data P1 bytes long. Store this
950** blob in register P2. This instruction is not coded directly
danielk1977cbb18d22004-05-28 11:37:27 +0000951** by the compiler. Instead, the compiler layer specifies
952** an OP_HexBlob opcode, with the hex string representation of
drh66a51672008-01-03 00:01:23 +0000953** the blob as P4. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000954** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000955*/
drh4c583122008-01-04 22:01:03 +0000956case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000957 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000958 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000959 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000960 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000961 break;
962}
963
drheaf52d82010-05-12 13:50:23 +0000964/* Opcode: Variable P1 P2 * P4 *
drh50457892003-09-06 01:10:47 +0000965**
drheaf52d82010-05-12 13:50:23 +0000966** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +0000967**
968** If the parameter is named, then its name appears in P4 and P3==1.
969** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +0000970*/
drheaf52d82010-05-12 13:50:23 +0000971case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +0000972 Mem *pVar; /* Value being transferred */
973
drheaf52d82010-05-12 13:50:23 +0000974 assert( pOp->p1>0 && pOp->p1<=p->nVar );
975 pVar = &p->aVar[pOp->p1 - 1];
976 if( sqlite3VdbeMemTooBig(pVar) ){
977 goto too_big;
drh023ae032007-05-08 12:12:16 +0000978 }
drheaf52d82010-05-12 13:50:23 +0000979 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
980 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +0000981 break;
982}
danielk1977295ba552004-05-19 10:34:51 +0000983
drhb21e7c72008-06-22 12:37:57 +0000984/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +0000985**
drhb21e7c72008-06-22 12:37:57 +0000986** Move the values in register P1..P1+P3-1 over into
987** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
988** left holding a NULL. It is an error for register ranges
989** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +0000990*/
drhe1349cb2008-04-01 00:36:10 +0000991case OP_Move: {
drh856c1032009-06-02 15:21:42 +0000992 char *zMalloc; /* Holding variable for allocated memory */
993 int n; /* Number of registers left to copy */
994 int p1; /* Register to copy from */
995 int p2; /* Register to copy to */
996
997 n = pOp->p3;
998 p1 = pOp->p1;
999 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001000 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001001 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001002
drha6c2ed92009-11-14 23:22:23 +00001003 pIn1 = &aMem[p1];
1004 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001005 while( n-- ){
drha6c2ed92009-11-14 23:22:23 +00001006 assert( pOut<=&aMem[p->nMem] );
1007 assert( pIn1<=&aMem[p->nMem] );
drhb21e7c72008-06-22 12:37:57 +00001008 zMalloc = pOut->zMalloc;
1009 pOut->zMalloc = 0;
1010 sqlite3VdbeMemMove(pOut, pIn1);
1011 pIn1->zMalloc = zMalloc;
1012 REGISTER_TRACE(p2++, pOut);
1013 pIn1++;
1014 pOut++;
1015 }
drhe1349cb2008-04-01 00:36:10 +00001016 break;
1017}
1018
drhb1fdb2a2008-01-05 04:06:03 +00001019/* Opcode: Copy P1 P2 * * *
1020**
drh9cbf3422008-01-17 16:22:13 +00001021** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001022**
1023** This instruction makes a deep copy of the value. A duplicate
1024** is made of any string or blob constant. See also OP_SCopy.
1025*/
drh93952eb2009-11-13 19:43:43 +00001026case OP_Copy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001027 pIn1 = &aMem[pOp->p1];
1028 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001029 assert( pOut!=pIn1 );
1030 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1031 Deephemeralize(pOut);
1032 REGISTER_TRACE(pOp->p2, pOut);
1033 break;
1034}
1035
drhb1fdb2a2008-01-05 04:06:03 +00001036/* Opcode: SCopy P1 P2 * * *
1037**
drh9cbf3422008-01-17 16:22:13 +00001038** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001039**
1040** This instruction makes a shallow copy of the value. If the value
1041** is a string or blob, then the copy is only a pointer to the
1042** original and hence if the original changes so will the copy.
1043** Worse, if the original is deallocated, the copy becomes invalid.
1044** Thus the program must guarantee that the original will not change
1045** during the lifetime of the copy. Use OP_Copy to make a complete
1046** copy.
1047*/
drh93952eb2009-11-13 19:43:43 +00001048case OP_SCopy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001049 pIn1 = &aMem[pOp->p1];
1050 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001051 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001052 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh5b6afba2008-01-05 16:29:28 +00001053 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001054 break;
1055}
drh75897232000-05-29 14:26:00 +00001056
drh9cbf3422008-01-17 16:22:13 +00001057/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001058**
shane21e7feb2008-05-30 15:59:49 +00001059** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001060** results. This opcode causes the sqlite3_step() call to terminate
1061** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1062** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001063** row.
drhd4e70eb2008-01-02 00:34:36 +00001064*/
drh9cbf3422008-01-17 16:22:13 +00001065case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001066 Mem *pMem;
1067 int i;
1068 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001069 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001070 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001071
dan32b09f22009-09-23 17:29:59 +00001072 /* If this statement has violated immediate foreign key constraints, do
1073 ** not return the number of rows modified. And do not RELEASE the statement
1074 ** transaction. It needs to be rolled back. */
1075 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1076 assert( db->flags&SQLITE_CountRows );
1077 assert( p->usesStmtJournal );
1078 break;
1079 }
1080
danielk1977bd434552009-03-18 10:33:00 +00001081 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1082 ** DML statements invoke this opcode to return the number of rows
1083 ** modified to the user. This is the only way that a VM that
1084 ** opens a statement transaction may invoke this opcode.
1085 **
1086 ** In case this is such a statement, close any statement transaction
1087 ** opened by this VM before returning control to the user. This is to
1088 ** ensure that statement-transactions are always nested, not overlapping.
1089 ** If the open statement-transaction is not closed here, then the user
1090 ** may step another VM that opens its own statement transaction. This
1091 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001092 **
1093 ** The statement transaction is never a top-level transaction. Hence
1094 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001095 */
1096 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001097 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1098 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001099 break;
1100 }
1101
drhd4e70eb2008-01-02 00:34:36 +00001102 /* Invalidate all ephemeral cursor row caches */
1103 p->cacheCtr = (p->cacheCtr + 2)|1;
1104
1105 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001106 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001107 ** as side effect.
1108 */
drha6c2ed92009-11-14 23:22:23 +00001109 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001110 for(i=0; i<pOp->p2; i++){
1111 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001112 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001113 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001114 }
drh28039692008-03-17 16:54:01 +00001115 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001116
1117 /* Return SQLITE_ROW
1118 */
drhd4e70eb2008-01-02 00:34:36 +00001119 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001120 rc = SQLITE_ROW;
1121 goto vdbe_return;
1122}
1123
drh5b6afba2008-01-05 16:29:28 +00001124/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001125**
drh5b6afba2008-01-05 16:29:28 +00001126** Add the text in register P1 onto the end of the text in
1127** register P2 and store the result in register P3.
1128** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001129**
1130** P3 = P2 || P1
1131**
1132** It is illegal for P1 and P3 to be the same register. Sometimes,
1133** if P3 is the same register as P2, the implementation is able
1134** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001135*/
drh5b6afba2008-01-05 16:29:28 +00001136case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001137 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001138
drh3c657212009-11-17 23:59:58 +00001139 pIn1 = &aMem[pOp->p1];
1140 pIn2 = &aMem[pOp->p2];
1141 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001142 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001143 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001144 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001145 break;
drh5e00f6c2001-09-13 13:46:56 +00001146 }
drha0c06522009-06-17 22:50:41 +00001147 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001148 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001149 Stringify(pIn2, encoding);
1150 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001151 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001152 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001153 }
danielk1977a7a8e142008-02-13 18:25:27 +00001154 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001155 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001156 goto no_mem;
1157 }
danielk1977a7a8e142008-02-13 18:25:27 +00001158 if( pOut!=pIn2 ){
1159 memcpy(pOut->z, pIn2->z, pIn2->n);
1160 }
1161 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1162 pOut->z[nByte] = 0;
1163 pOut->z[nByte+1] = 0;
1164 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001165 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001166 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001167 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001168 break;
1169}
drh75897232000-05-29 14:26:00 +00001170
drh3c84ddf2008-01-09 02:15:38 +00001171/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001172**
drh60a713c2008-01-21 16:22:45 +00001173** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001174** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001175** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001176*/
drh3c84ddf2008-01-09 02:15:38 +00001177/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001178**
drh3c84ddf2008-01-09 02:15:38 +00001179**
shane21e7feb2008-05-30 15:59:49 +00001180** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001181** and store the result in register P3.
1182** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001183*/
drh3c84ddf2008-01-09 02:15:38 +00001184/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001185**
drh60a713c2008-01-21 16:22:45 +00001186** Subtract the value in register P1 from the value in register P2
1187** and store the result in register P3.
1188** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001189*/
drh9cbf3422008-01-17 16:22:13 +00001190/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001191**
drh60a713c2008-01-21 16:22:45 +00001192** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001193** and store the result in register P3 (P3=P2/P1). If the value in
1194** register P1 is zero, then the result is NULL. If either input is
1195** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001196*/
drh9cbf3422008-01-17 16:22:13 +00001197/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001198**
drh3c84ddf2008-01-09 02:15:38 +00001199** Compute the remainder after integer division of the value in
1200** register P1 by the value in register P2 and store the result in P3.
1201** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001202** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001203*/
drh5b6afba2008-01-05 16:29:28 +00001204case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1205case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1206case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1207case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1208case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001209 int flags; /* Combined MEM_* flags from both inputs */
1210 i64 iA; /* Integer value of left operand */
1211 i64 iB; /* Integer value of right operand */
1212 double rA; /* Real value of left operand */
1213 double rB; /* Real value of right operand */
1214
drh3c657212009-11-17 23:59:58 +00001215 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001216 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001217 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001218 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001219 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001220 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001221 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1222 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001223 iA = pIn1->u.i;
1224 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001225 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001226 case OP_Add: iB += iA; break;
1227 case OP_Subtract: iB -= iA; break;
1228 case OP_Multiply: iB *= iA; break;
drhbf4133c2001-10-13 02:59:08 +00001229 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001230 if( iA==0 ) goto arithmetic_result_is_null;
danielk197742d4ef22007-06-26 11:13:25 +00001231 /* Dividing the largest possible negative 64-bit integer (1<<63) by
drh0f050352008-05-09 18:03:13 +00001232 ** -1 returns an integer too large to store in a 64-bit data-type. On
danielk197742d4ef22007-06-26 11:13:25 +00001233 ** some architectures, the value overflows to (1<<63). On others,
1234 ** a SIGFPE is issued. The following statement normalizes this
shane21e7feb2008-05-30 15:59:49 +00001235 ** behavior so that all architectures behave as if integer
1236 ** overflow occurred.
danielk197742d4ef22007-06-26 11:13:25 +00001237 */
drh856c1032009-06-02 15:21:42 +00001238 if( iA==-1 && iB==SMALLEST_INT64 ) iA = 1;
1239 iB /= iA;
drh75897232000-05-29 14:26:00 +00001240 break;
1241 }
drhbf4133c2001-10-13 02:59:08 +00001242 default: {
drh856c1032009-06-02 15:21:42 +00001243 if( iA==0 ) goto arithmetic_result_is_null;
1244 if( iA==-1 ) iA = 1;
1245 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001246 break;
1247 }
drh75897232000-05-29 14:26:00 +00001248 }
drh856c1032009-06-02 15:21:42 +00001249 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001250 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001251 }else{
drh856c1032009-06-02 15:21:42 +00001252 rA = sqlite3VdbeRealValue(pIn1);
1253 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001254 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001255 case OP_Add: rB += rA; break;
1256 case OP_Subtract: rB -= rA; break;
1257 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001258 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001259 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001260 if( rA==(double)0 ) goto arithmetic_result_is_null;
1261 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001262 break;
1263 }
drhbf4133c2001-10-13 02:59:08 +00001264 default: {
shane75ac1de2009-06-09 18:58:52 +00001265 iA = (i64)rA;
1266 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001267 if( iA==0 ) goto arithmetic_result_is_null;
1268 if( iA==-1 ) iA = 1;
1269 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001270 break;
1271 }
drh5e00f6c2001-09-13 13:46:56 +00001272 }
drhc5a7b512010-01-13 16:25:42 +00001273#ifdef SQLITE_OMIT_FLOATING_POINT
1274 pOut->u.i = rB;
1275 MemSetTypeFlag(pOut, MEM_Int);
1276#else
drh856c1032009-06-02 15:21:42 +00001277 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001278 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001279 }
drh856c1032009-06-02 15:21:42 +00001280 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001281 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001282 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001283 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001284 }
drhc5a7b512010-01-13 16:25:42 +00001285#endif
drh5e00f6c2001-09-13 13:46:56 +00001286 }
1287 break;
1288
drha05a7222008-01-19 03:35:58 +00001289arithmetic_result_is_null:
1290 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001291 break;
1292}
1293
drh66a51672008-01-03 00:01:23 +00001294/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001295**
drh66a51672008-01-03 00:01:23 +00001296** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001297** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1298** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001299** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001300**
1301** The interface used by the implementation of the aforementioned functions
1302** to retrieve the collation sequence set by this opcode is not available
1303** publicly, only to user functions defined in func.c.
1304*/
drh9cbf3422008-01-17 16:22:13 +00001305case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001306 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001307 break;
1308}
1309
drh98757152008-01-09 23:04:12 +00001310/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001311**
drh66a51672008-01-03 00:01:23 +00001312** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001313** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001314** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001315** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001316**
drh13449892005-09-07 21:22:45 +00001317** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001318** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001319** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001320** whether meta data associated with a user function argument using the
1321** sqlite3_set_auxdata() API may be safely retained until the next
1322** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001323**
drh13449892005-09-07 21:22:45 +00001324** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001325*/
drh0bce8352002-02-28 00:41:10 +00001326case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001327 int i;
drh6810ce62004-01-31 19:22:56 +00001328 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001329 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001330 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001331 int n;
drh1350b032002-02-27 19:00:20 +00001332
drh856c1032009-06-02 15:21:42 +00001333 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001334 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001335 assert( apVal || n==0 );
1336
danielk19776ab3a2e2009-02-19 14:39:25 +00001337 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001338 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001339 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001340 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001341 apVal[i] = pArg;
dan937d0de2009-10-15 18:35:38 +00001342 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001343 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001344 }
danielk197751ad0ec2004-05-24 12:39:02 +00001345
drh66a51672008-01-03 00:01:23 +00001346 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1347 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001348 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001349 ctx.pVdbeFunc = 0;
1350 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001351 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001352 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1353 }
1354
danielk1977a7a8e142008-02-13 18:25:27 +00001355 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00001356 pOut = &aMem[pOp->p3];
drh00706be2004-01-30 14:49:16 +00001357 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001358 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001359 ctx.s.xDel = 0;
1360 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001361
1362 /* The output cell may already have a buffer allocated. Move
1363 ** the pointer to ctx.s so in case the user-function can use
1364 ** the already allocated buffer instead of allocating a new one.
1365 */
1366 sqlite3VdbeMemMove(&ctx.s, pOut);
1367 MemSetTypeFlag(&ctx.s, MEM_Null);
1368
drh8e0a2f92002-02-23 23:45:45 +00001369 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001370 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001371 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001372 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001373 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001374 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001375 }
danielk197751ad0ec2004-05-24 12:39:02 +00001376 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
drh17435752007-08-16 04:30:38 +00001377 if( db->mallocFailed ){
danielk1977e0fc5262007-07-26 06:50:05 +00001378 /* Even though a malloc() has failed, the implementation of the
1379 ** user function may have called an sqlite3_result_XXX() function
1380 ** to return a value. The following call releases any resources
1381 ** associated with such a value.
danielk1977e0fc5262007-07-26 06:50:05 +00001382 */
1383 sqlite3VdbeMemRelease(&ctx.s);
1384 goto no_mem;
1385 }
danielk19777e18c252004-05-25 11:47:24 +00001386
shane21e7feb2008-05-30 15:59:49 +00001387 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001388 ** immediately call the destructor for any non-static values.
1389 */
1390 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001391 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001392 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001393 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001394 }
1395
drh90669c12006-01-20 15:45:36 +00001396 /* If the function returned an error, throw an exception */
1397 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001398 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001399 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001400 }
1401
drh9cbf3422008-01-17 16:22:13 +00001402 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001403 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001404 sqlite3VdbeMemMove(pOut, &ctx.s);
1405 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001406 goto too_big;
1407 }
drh2dcef112008-01-12 19:03:48 +00001408 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001409 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001410 break;
1411}
1412
drh98757152008-01-09 23:04:12 +00001413/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001414**
drh98757152008-01-09 23:04:12 +00001415** Take the bit-wise AND of the values in register P1 and P2 and
1416** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001417** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001418*/
drh98757152008-01-09 23:04:12 +00001419/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001420**
drh98757152008-01-09 23:04:12 +00001421** Take the bit-wise OR of the values in register P1 and P2 and
1422** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001423** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001424*/
drh98757152008-01-09 23:04:12 +00001425/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001426**
drh98757152008-01-09 23:04:12 +00001427** Shift the integer value in register P2 to the left by the
drh60a713c2008-01-21 16:22:45 +00001428** number of bits specified by the integer in regiser P1.
drh98757152008-01-09 23:04:12 +00001429** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001430** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001431*/
drh98757152008-01-09 23:04:12 +00001432/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001433**
drh98757152008-01-09 23:04:12 +00001434** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001435** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001436** 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*/
drh5b6afba2008-01-05 16:29:28 +00001439case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1440case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1441case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1442case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001443 i64 a;
1444 i64 b;
drh6810ce62004-01-31 19:22:56 +00001445
drh3c657212009-11-17 23:59:58 +00001446 pIn1 = &aMem[pOp->p1];
1447 pIn2 = &aMem[pOp->p2];
1448 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001449 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001450 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001451 break;
1452 }
drh5b6afba2008-01-05 16:29:28 +00001453 a = sqlite3VdbeIntValue(pIn2);
1454 b = sqlite3VdbeIntValue(pIn1);
drhbf4133c2001-10-13 02:59:08 +00001455 switch( pOp->opcode ){
1456 case OP_BitAnd: a &= b; break;
1457 case OP_BitOr: a |= b; break;
1458 case OP_ShiftLeft: a <<= b; break;
drha05a7222008-01-19 03:35:58 +00001459 default: assert( pOp->opcode==OP_ShiftRight );
1460 a >>= b; break;
drhbf4133c2001-10-13 02:59:08 +00001461 }
drh5b6afba2008-01-05 16:29:28 +00001462 pOut->u.i = a;
danielk1977a7a8e142008-02-13 18:25:27 +00001463 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001464 break;
1465}
1466
drh8558cde2008-01-05 05:20:10 +00001467/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001468**
danielk19770cdc0222008-06-26 18:04:03 +00001469** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001470** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001471**
drh8558cde2008-01-05 05:20:10 +00001472** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001473*/
drh9cbf3422008-01-17 16:22:13 +00001474case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001475 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001476 sqlite3VdbeMemIntegerify(pIn1);
1477 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001478 break;
1479}
1480
drh9cbf3422008-01-17 16:22:13 +00001481/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001482**
drh9cbf3422008-01-17 16:22:13 +00001483** Force the value in register P1 to be an integer. If the value
1484** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001485** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001486** raise an SQLITE_MISMATCH exception.
1487*/
drh9cbf3422008-01-17 16:22:13 +00001488case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001489 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001490 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1491 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001492 if( pOp->p2==0 ){
1493 rc = SQLITE_MISMATCH;
1494 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001495 }else{
drh17c40292004-07-21 02:53:29 +00001496 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001497 }
drh8aff1012001-12-22 14:49:24 +00001498 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001499 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001500 }
1501 break;
1502}
1503
drh13573c72010-01-12 17:04:07 +00001504#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001505/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001506**
drh2133d822008-01-03 18:44:59 +00001507** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001508**
drh8a512562005-11-14 22:29:05 +00001509** This opcode is used when extracting information from a column that
1510** has REAL affinity. Such column values may still be stored as
1511** integers, for space efficiency, but after extraction we want them
1512** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001513*/
drh9cbf3422008-01-17 16:22:13 +00001514case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001515 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001516 if( pIn1->flags & MEM_Int ){
1517 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001518 }
drh487e2622005-06-25 18:42:14 +00001519 break;
1520}
drh13573c72010-01-12 17:04:07 +00001521#endif
drh487e2622005-06-25 18:42:14 +00001522
drh8df447f2005-11-01 15:48:24 +00001523#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001524/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001525**
drh8558cde2008-01-05 05:20:10 +00001526** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001527** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001528** equivalent of printf(). Blob values are unchanged and
1529** are afterwards simply interpreted as text.
1530**
1531** A NULL value is not changed by this routine. It remains NULL.
1532*/
drh9cbf3422008-01-17 16:22:13 +00001533case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001534 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001535 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001536 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001537 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1538 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1539 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001540 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001541 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001542 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001543 break;
1544}
1545
drh8558cde2008-01-05 05:20:10 +00001546/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001547**
drh8558cde2008-01-05 05:20:10 +00001548** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001549** If the value is numeric, convert it to a string first.
1550** Strings are simply reinterpreted as blobs with no change
1551** to the underlying data.
1552**
1553** A NULL value is not changed by this routine. It remains NULL.
1554*/
drh9cbf3422008-01-17 16:22:13 +00001555case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001556 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001557 if( pIn1->flags & MEM_Null ) break;
1558 if( (pIn1->flags & MEM_Blob)==0 ){
1559 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001560 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001561 MemSetTypeFlag(pIn1, MEM_Blob);
1562 }else{
1563 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001564 }
drhb7654112008-01-12 12:48:07 +00001565 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001566 break;
1567}
drh8a512562005-11-14 22:29:05 +00001568
drh8558cde2008-01-05 05:20:10 +00001569/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001570**
drh8558cde2008-01-05 05:20:10 +00001571** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001572** integer or a floating-point number.)
1573** If the value is text or blob, try to convert it to an using the
1574** equivalent of atoi() or atof() and store 0 if no such conversion
1575** is possible.
1576**
1577** A NULL value is not changed by this routine. It remains NULL.
1578*/
drh9cbf3422008-01-17 16:22:13 +00001579case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001580 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001581 if( (pIn1->flags & (MEM_Null|MEM_Int|MEM_Real))==0 ){
1582 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001583 }
1584 break;
1585}
1586#endif /* SQLITE_OMIT_CAST */
1587
drh8558cde2008-01-05 05:20:10 +00001588/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001589**
drh8558cde2008-01-05 05:20:10 +00001590** Force the value in register P1 be an integer. If
drh8a512562005-11-14 22:29:05 +00001591** The value is currently a real number, drop its fractional part.
1592** If the value is text or blob, try to convert it to an integer using the
1593** equivalent of atoi() and store 0 if no such conversion is possible.
1594**
1595** A NULL value is not changed by this routine. It remains NULL.
1596*/
drh9cbf3422008-01-17 16:22:13 +00001597case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001598 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001599 if( (pIn1->flags & MEM_Null)==0 ){
1600 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001601 }
1602 break;
1603}
1604
drh13573c72010-01-12 17:04:07 +00001605#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001606/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001607**
drh8558cde2008-01-05 05:20:10 +00001608** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001609** If The value is currently an integer, convert it.
1610** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001611** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001612**
1613** A NULL value is not changed by this routine. It remains NULL.
1614*/
drh9cbf3422008-01-17 16:22:13 +00001615case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001616 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001617 if( (pIn1->flags & MEM_Null)==0 ){
1618 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001619 }
1620 break;
1621}
drh13573c72010-01-12 17:04:07 +00001622#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001623
drh35573352008-01-08 23:54:25 +00001624/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001625**
drh35573352008-01-08 23:54:25 +00001626** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1627** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001628**
drh35573352008-01-08 23:54:25 +00001629** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1630** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
1631** bit is clear then fall thru if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001632**
drh35573352008-01-08 23:54:25 +00001633** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001634** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001635** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001636** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001637** affinity is used. Note that the affinity conversions are stored
1638** back into the input registers P1 and P3. So this opcode can cause
1639** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001640**
1641** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001642** the values are compared. If both values are blobs then memcmp() is
1643** used to determine the results of the comparison. If both values
1644** are text, then the appropriate collating function specified in
1645** P4 is used to do the comparison. If P4 is not specified then
1646** memcmp() is used to compare text string. If both values are
1647** numeric, then a numeric comparison is used. If the two values
1648** are of different types, then numbers are considered less than
1649** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001650**
drh35573352008-01-08 23:54:25 +00001651** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1652** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001653*/
drh9cbf3422008-01-17 16:22:13 +00001654/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001655**
drh35573352008-01-08 23:54:25 +00001656** This works just like the Lt opcode except that the jump is taken if
1657** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001658** additional information.
drh6a2fe092009-09-23 02:29:36 +00001659**
1660** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1661** true or false and is never NULL. If both operands are NULL then the result
1662** of comparison is false. If either operand is NULL then the result is true.
1663** If neither operand is NULL the the result is the same as it would be if
1664** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001665*/
drh9cbf3422008-01-17 16:22:13 +00001666/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001667**
drh35573352008-01-08 23:54:25 +00001668** This works just like the Lt opcode except that the jump is taken if
1669** the operands in registers P1 and P3 are equal.
1670** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001671**
1672** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1673** true or false and is never NULL. If both operands are NULL then the result
1674** of comparison is true. If either operand is NULL then the result is false.
1675** If neither operand is NULL the the result is the same as it would be if
1676** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001677*/
drh9cbf3422008-01-17 16:22:13 +00001678/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001679**
drh35573352008-01-08 23:54:25 +00001680** This works just like the Lt opcode except that the jump is taken if
1681** the content of register P3 is less than or equal to the content of
1682** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001683*/
drh9cbf3422008-01-17 16:22:13 +00001684/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001685**
drh35573352008-01-08 23:54:25 +00001686** This works just like the Lt opcode except that the jump is taken if
1687** the content of register P3 is greater than the content of
1688** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001689*/
drh9cbf3422008-01-17 16:22:13 +00001690/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001691**
drh35573352008-01-08 23:54:25 +00001692** This works just like the Lt opcode except that the jump is taken if
1693** the content of register P3 is greater than or equal to the content of
1694** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001695*/
drh9cbf3422008-01-17 16:22:13 +00001696case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1697case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1698case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1699case OP_Le: /* same as TK_LE, jump, in1, in3 */
1700case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1701case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001702 int res; /* Result of the comparison of pIn1 against pIn3 */
1703 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001704 u16 flags1; /* Copy of initial value of pIn1->flags */
1705 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001706
drh3c657212009-11-17 23:59:58 +00001707 pIn1 = &aMem[pOp->p1];
1708 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001709 flags1 = pIn1->flags;
1710 flags3 = pIn3->flags;
drh6a2fe092009-09-23 02:29:36 +00001711 if( (pIn1->flags | pIn3->flags)&MEM_Null ){
1712 /* One or both operands are NULL */
1713 if( pOp->p5 & SQLITE_NULLEQ ){
1714 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1715 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1716 ** or not both operands are null.
1717 */
1718 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
1719 res = (pIn1->flags & pIn3->flags & MEM_Null)==0;
1720 }else{
1721 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1722 ** then the result is always NULL.
1723 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1724 */
1725 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001726 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001727 MemSetTypeFlag(pOut, MEM_Null);
1728 REGISTER_TRACE(pOp->p2, pOut);
1729 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1730 pc = pOp->p2-1;
1731 }
1732 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001733 }
drh6a2fe092009-09-23 02:29:36 +00001734 }else{
1735 /* Neither operand is NULL. Do a comparison. */
1736 affinity = pOp->p5 & SQLITE_AFF_MASK;
1737 if( affinity ){
1738 applyAffinity(pIn1, affinity, encoding);
1739 applyAffinity(pIn3, affinity, encoding);
1740 if( db->mallocFailed ) goto no_mem;
1741 }
danielk1977a37cdde2004-05-16 11:15:36 +00001742
drh6a2fe092009-09-23 02:29:36 +00001743 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1744 ExpandBlob(pIn1);
1745 ExpandBlob(pIn3);
1746 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001747 }
danielk1977a37cdde2004-05-16 11:15:36 +00001748 switch( pOp->opcode ){
1749 case OP_Eq: res = res==0; break;
1750 case OP_Ne: res = res!=0; break;
1751 case OP_Lt: res = res<0; break;
1752 case OP_Le: res = res<=0; break;
1753 case OP_Gt: res = res>0; break;
1754 default: res = res>=0; break;
1755 }
1756
drh35573352008-01-08 23:54:25 +00001757 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001758 pOut = &aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00001759 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001760 pOut->u.i = res;
1761 REGISTER_TRACE(pOp->p2, pOut);
1762 }else if( res ){
1763 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001764 }
danb7dca7d2010-03-05 16:32:12 +00001765
1766 /* Undo any changes made by applyAffinity() to the input registers. */
1767 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1768 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001769 break;
1770}
drhc9b84a12002-06-20 11:36:48 +00001771
drh0acb7e42008-06-25 00:12:41 +00001772/* Opcode: Permutation * * * P4 *
1773**
shanebe217792009-03-05 04:20:31 +00001774** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001775** of integers in P4.
1776**
1777** The permutation is only valid until the next OP_Permutation, OP_Compare,
1778** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1779** immediately prior to the OP_Compare.
1780*/
1781case OP_Permutation: {
1782 assert( pOp->p4type==P4_INTARRAY );
1783 assert( pOp->p4.ai );
1784 aPermute = pOp->p4.ai;
1785 break;
1786}
1787
drh16ee60f2008-06-20 18:13:25 +00001788/* Opcode: Compare P1 P2 P3 P4 *
1789**
1790** Compare to vectors of registers in reg(P1)..reg(P1+P3-1) (all this
1791** one "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
1792** the comparison for use by the next OP_Jump instruct.
1793**
drh0acb7e42008-06-25 00:12:41 +00001794** P4 is a KeyInfo structure that defines collating sequences and sort
1795** orders for the comparison. The permutation applies to registers
1796** only. The KeyInfo elements are used sequentially.
1797**
1798** The comparison is a sort comparison, so NULLs compare equal,
1799** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001800** and strings are less than blobs.
1801*/
1802case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001803 int n;
1804 int i;
1805 int p1;
1806 int p2;
1807 const KeyInfo *pKeyInfo;
1808 int idx;
1809 CollSeq *pColl; /* Collating sequence to use on this term */
1810 int bRev; /* True for DESCENDING sort order */
1811
1812 n = pOp->p3;
1813 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001814 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001815 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001816 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001817 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00001818#if SQLITE_DEBUG
1819 if( aPermute ){
1820 int k, mx = 0;
1821 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
1822 assert( p1>0 && p1+mx<=p->nMem+1 );
1823 assert( p2>0 && p2+mx<=p->nMem+1 );
1824 }else{
1825 assert( p1>0 && p1+n<=p->nMem+1 );
1826 assert( p2>0 && p2+n<=p->nMem+1 );
1827 }
1828#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00001829 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001830 idx = aPermute ? aPermute[i] : i;
drha6c2ed92009-11-14 23:22:23 +00001831 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
1832 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001833 assert( i<pKeyInfo->nField );
1834 pColl = pKeyInfo->aColl[i];
1835 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00001836 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00001837 if( iCompare ){
1838 if( bRev ) iCompare = -iCompare;
1839 break;
1840 }
drh16ee60f2008-06-20 18:13:25 +00001841 }
drh0acb7e42008-06-25 00:12:41 +00001842 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001843 break;
1844}
1845
1846/* Opcode: Jump P1 P2 P3 * *
1847**
1848** Jump to the instruction at address P1, P2, or P3 depending on whether
1849** in the most recent OP_Compare instruction the P1 vector was less than
1850** equal to, or greater than the P2 vector, respectively.
1851*/
drh0acb7e42008-06-25 00:12:41 +00001852case OP_Jump: { /* jump */
1853 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001854 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001855 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001856 pc = pOp->p2 - 1;
1857 }else{
1858 pc = pOp->p3 - 1;
1859 }
1860 break;
1861}
1862
drh5b6afba2008-01-05 16:29:28 +00001863/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001864**
drh5b6afba2008-01-05 16:29:28 +00001865** Take the logical AND of the values in registers P1 and P2 and
1866** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001867**
drh5b6afba2008-01-05 16:29:28 +00001868** If either P1 or P2 is 0 (false) then the result is 0 even if
1869** the other input is NULL. A NULL and true or two NULLs give
1870** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001871*/
drh5b6afba2008-01-05 16:29:28 +00001872/* Opcode: Or P1 P2 P3 * *
1873**
1874** Take the logical OR of the values in register P1 and P2 and
1875** store the answer in register P3.
1876**
1877** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1878** even if the other input is NULL. A NULL and false or two NULLs
1879** give a NULL output.
1880*/
1881case OP_And: /* same as TK_AND, in1, in2, out3 */
1882case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001883 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
1884 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001885
drh3c657212009-11-17 23:59:58 +00001886 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00001887 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001888 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001889 }else{
drh5b6afba2008-01-05 16:29:28 +00001890 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001891 }
drh3c657212009-11-17 23:59:58 +00001892 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00001893 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001894 v2 = 2;
1895 }else{
drh5b6afba2008-01-05 16:29:28 +00001896 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001897 }
1898 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001899 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001900 v1 = and_logic[v1*3+v2];
1901 }else{
drh5b6afba2008-01-05 16:29:28 +00001902 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001903 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001904 }
drh3c657212009-11-17 23:59:58 +00001905 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00001906 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001907 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001908 }else{
drh5b6afba2008-01-05 16:29:28 +00001909 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001910 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001911 }
drh5e00f6c2001-09-13 13:46:56 +00001912 break;
1913}
1914
drhe99fa2a2008-12-15 15:27:51 +00001915/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001916**
drhe99fa2a2008-12-15 15:27:51 +00001917** Interpret the value in register P1 as a boolean value. Store the
1918** boolean complement in register P2. If the value in register P1 is
1919** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001920*/
drh93952eb2009-11-13 19:43:43 +00001921case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00001922 pIn1 = &aMem[pOp->p1];
1923 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00001924 if( pIn1->flags & MEM_Null ){
1925 sqlite3VdbeMemSetNull(pOut);
1926 }else{
1927 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
1928 }
drh5e00f6c2001-09-13 13:46:56 +00001929 break;
1930}
1931
drhe99fa2a2008-12-15 15:27:51 +00001932/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00001933**
drhe99fa2a2008-12-15 15:27:51 +00001934** Interpret the content of register P1 as an integer. Store the
1935** ones-complement of the P1 value into register P2. If P1 holds
1936** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00001937*/
drh93952eb2009-11-13 19:43:43 +00001938case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00001939 pIn1 = &aMem[pOp->p1];
1940 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00001941 if( pIn1->flags & MEM_Null ){
1942 sqlite3VdbeMemSetNull(pOut);
1943 }else{
1944 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
1945 }
drhbf4133c2001-10-13 02:59:08 +00001946 break;
1947}
1948
drh3c84ddf2008-01-09 02:15:38 +00001949/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001950**
drh3c84ddf2008-01-09 02:15:38 +00001951** Jump to P2 if the value in register P1 is true. The value is
1952** is considered true if it is numeric and non-zero. If the value
1953** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00001954*/
drh3c84ddf2008-01-09 02:15:38 +00001955/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00001956**
drh3c84ddf2008-01-09 02:15:38 +00001957** Jump to P2 if the value in register P1 is False. The value is
1958** is considered true if it has a numeric value of zero. If the value
1959** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00001960*/
drh9cbf3422008-01-17 16:22:13 +00001961case OP_If: /* jump, in1 */
1962case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00001963 int c;
drh3c657212009-11-17 23:59:58 +00001964 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001965 if( pIn1->flags & MEM_Null ){
1966 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00001967 }else{
drhba0232a2005-06-06 17:27:19 +00001968#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00001969 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00001970#else
drh3c84ddf2008-01-09 02:15:38 +00001971 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00001972#endif
drhf5905aa2002-05-26 20:54:33 +00001973 if( pOp->opcode==OP_IfNot ) c = !c;
1974 }
drh3c84ddf2008-01-09 02:15:38 +00001975 if( c ){
1976 pc = pOp->p2-1;
1977 }
drh5e00f6c2001-09-13 13:46:56 +00001978 break;
1979}
1980
drh830ecf92009-06-18 00:41:55 +00001981/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00001982**
drh830ecf92009-06-18 00:41:55 +00001983** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00001984*/
drh9cbf3422008-01-17 16:22:13 +00001985case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00001986 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00001987 if( (pIn1->flags & MEM_Null)!=0 ){
1988 pc = pOp->p2 - 1;
1989 }
drh477df4b2008-01-05 18:48:24 +00001990 break;
1991}
1992
drh98757152008-01-09 23:04:12 +00001993/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001994**
drh6a288a32008-01-07 19:20:24 +00001995** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00001996*/
drh9cbf3422008-01-17 16:22:13 +00001997case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00001998 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00001999 if( (pIn1->flags & MEM_Null)==0 ){
2000 pc = pOp->p2 - 1;
2001 }
drh5e00f6c2001-09-13 13:46:56 +00002002 break;
2003}
2004
drh3e9ca092009-09-08 01:14:48 +00002005/* Opcode: Column P1 P2 P3 P4 P5
danielk1977192ac1d2004-05-10 07:17:30 +00002006**
danielk1977cfcdaef2004-05-12 07:33:33 +00002007** Interpret the data that cursor P1 points to as a structure built using
2008** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002009** information about the format of the data.) Extract the P2-th column
2010** from this record. If there are less that (P2+1)
2011** values in the record, extract a NULL.
2012**
drh9cbf3422008-01-17 16:22:13 +00002013** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002014**
danielk19771f4aa332008-01-03 09:51:55 +00002015** If the column contains fewer than P2 fields, then extract a NULL. Or,
2016** if the P4 argument is a P4_MEM use the value of the P4 argument as
2017** the result.
drh3e9ca092009-09-08 01:14:48 +00002018**
2019** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2020** then the cache of the cursor is reset prior to extracting the column.
2021** The first OP_Column against a pseudo-table after the value of the content
2022** register has changed should have this bit set.
danielk1977192ac1d2004-05-10 07:17:30 +00002023*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002024case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002025 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002026 i64 payloadSize64; /* Number of bytes in the record */
2027 int p1; /* P1 value of the opcode */
2028 int p2; /* column number to retrieve */
2029 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002030 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002031 BtCursor *pCrsr; /* The BTree cursor */
2032 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2033 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002034 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002035 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002036 int i; /* Loop counter */
2037 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002038 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002039 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002040 u8 *zIdx; /* Index into header */
2041 u8 *zEndHdr; /* Pointer to first byte after the header */
2042 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002043 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002044 int szHdr; /* Size of the header size field at start of record */
2045 int avail; /* Number of bytes of available data */
drh3e9ca092009-09-08 01:14:48 +00002046 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002047
drh856c1032009-06-02 15:21:42 +00002048
2049 p1 = pOp->p1;
2050 p2 = pOp->p2;
2051 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002052 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002053 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002054 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002055 pDest = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00002056 MemSetTypeFlag(pDest, MEM_Null);
shane36840fd2009-06-26 16:32:13 +00002057 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002058
drhe61cffc2004-06-12 18:12:15 +00002059 /* This block sets the variable payloadSize to be the total number of
2060 ** bytes in the record.
2061 **
2062 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002063 ** The complete record text is always available for pseudo-tables
2064 ** If the record is stored in a cursor, the complete record text
2065 ** might be available in the pC->aRow cache. Or it might not be.
2066 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002067 **
2068 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002069 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002070 */
drhb73857f2006-03-17 00:25:59 +00002071 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00002072 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002073#ifndef SQLITE_OMIT_VIRTUALTABLE
2074 assert( pC->pVtabCursor==0 );
2075#endif
shane36840fd2009-06-26 16:32:13 +00002076 pCrsr = pC->pCursor;
2077 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002078 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002079 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002080 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002081 if( pC->nullRow ){
2082 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002083 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002084 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002085 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002086 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002087 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhc27ae612009-07-14 18:35:44 +00002088 rc = sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2089 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002090 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2091 ** payload size, so it is impossible for payloadSize64 to be
2092 ** larger than 32 bits. */
2093 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002094 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002095 }else{
drhea8ffdf2009-07-22 00:35:23 +00002096 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhc27ae612009-07-14 18:35:44 +00002097 rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002098 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002099 }
drh3e9ca092009-09-08 01:14:48 +00002100 }else if( pC->pseudoTableReg>0 ){
drha6c2ed92009-11-14 23:22:23 +00002101 pReg = &aMem[pC->pseudoTableReg];
drh3e9ca092009-09-08 01:14:48 +00002102 assert( pReg->flags & MEM_Blob );
2103 payloadSize = pReg->n;
2104 zRec = pReg->z;
2105 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002106 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002107 }else{
2108 /* Consider the row to be NULL */
2109 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002110 }
2111
drh9cbf3422008-01-17 16:22:13 +00002112 /* If payloadSize is 0, then just store a NULL */
danielk1977192ac1d2004-05-10 07:17:30 +00002113 if( payloadSize==0 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002114 assert( pDest->flags&MEM_Null );
drhd4e70eb2008-01-02 00:34:36 +00002115 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002116 }
drh35cd6432009-06-05 14:17:21 +00002117 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2118 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002119 goto too_big;
2120 }
danielk1977192ac1d2004-05-10 07:17:30 +00002121
shane36840fd2009-06-26 16:32:13 +00002122 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002123 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002124
drh9188b382004-05-14 21:12:22 +00002125 /* Read and parse the table header. Store the results of the parse
2126 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002127 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002128 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002129 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002130 aOffset = pC->aOffset;
2131 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002132 assert(aType);
drh856c1032009-06-02 15:21:42 +00002133 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002134 pC->aOffset = aOffset = &aType[nField];
2135 pC->payloadSize = payloadSize;
2136 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002137
drhd3194f52004-05-27 19:59:32 +00002138 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002139 if( zRec ){
2140 zData = zRec;
2141 }else{
drhf0863fe2005-06-12 21:35:51 +00002142 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002143 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002144 }else{
drhe51c44f2004-05-30 20:46:09 +00002145 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002146 }
drhe61cffc2004-06-12 18:12:15 +00002147 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2148 ** save the payload in the pC->aRow cache. That will save us from
2149 ** having to make additional calls to fetch the content portion of
2150 ** the record.
2151 */
drh35cd6432009-06-05 14:17:21 +00002152 assert( avail>=0 );
2153 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002154 zRec = zData;
2155 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002156 }else{
2157 pC->aRow = 0;
2158 }
drhd3194f52004-05-27 19:59:32 +00002159 }
drh588f5bc2007-01-02 18:41:54 +00002160 /* The following assert is true in all cases accept when
2161 ** the database file has been corrupted externally.
2162 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002163 szHdr = getVarint32((u8*)zData, offset);
2164
2165 /* Make sure a corrupt database has not given us an oversize header.
2166 ** Do this now to avoid an oversize memory allocation.
2167 **
2168 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2169 ** types use so much data space that there can only be 4096 and 32 of
2170 ** them, respectively. So the maximum header length results from a
2171 ** 3-byte type for each of the maximum of 32768 columns plus three
2172 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2173 */
2174 if( offset > 98307 ){
2175 rc = SQLITE_CORRUPT_BKPT;
2176 goto op_column_out;
2177 }
2178
2179 /* Compute in len the number of bytes of data we need to read in order
2180 ** to get nField type values. offset is an upper bound on this. But
2181 ** nField might be significantly less than the true number of columns
2182 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2183 ** We want to minimize len in order to limit the size of the memory
2184 ** allocation, especially if a corrupt database file has caused offset
2185 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2186 ** still exceed Robson memory allocation limits on some configurations.
2187 ** On systems that cannot tolerate large memory allocations, nField*5+3
2188 ** will likely be much smaller since nField will likely be less than
2189 ** 20 or so. This insures that Robson memory allocation limits are
2190 ** not exceeded even for corrupt database files.
2191 */
2192 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002193 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002194
2195 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2196 ** record header in most cases. But they will fail to get the complete
2197 ** record header if the record header does not fit on a single page
2198 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2199 ** acquire the complete header text.
2200 */
drh35cd6432009-06-05 14:17:21 +00002201 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002202 sMem.flags = 0;
2203 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002204 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002205 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002206 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002207 }
drhb6f54522004-05-20 02:42:16 +00002208 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002209 }
drh35cd6432009-06-05 14:17:21 +00002210 zEndHdr = (u8 *)&zData[len];
2211 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002212
drhd3194f52004-05-27 19:59:32 +00002213 /* Scan the header and use it to fill in the aType[] and aOffset[]
2214 ** arrays. aType[i] will contain the type integer for the i-th
2215 ** column and aOffset[i] will contain the offset from the beginning
2216 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002217 */
danielk1977dedf45b2006-01-13 17:12:01 +00002218 for(i=0; i<nField; i++){
2219 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002220 aOffset[i] = offset;
shane3f8d5cf2008-04-24 19:15:09 +00002221 zIdx += getVarint32(zIdx, aType[i]);
drh6658cd92010-02-05 14:12:53 +00002222 szField = sqlite3VdbeSerialTypeLen(aType[i]);
2223 offset += szField;
2224 if( offset<szField ){ /* True if offset overflows */
2225 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2226 break;
2227 }
danielk1977dedf45b2006-01-13 17:12:01 +00002228 }else{
2229 /* If i is less that nField, then there are less fields in this
2230 ** record than SetNumColumns indicated there are columns in the
2231 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002232 ** the record to 0. This tells code below to store a NULL
2233 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002234 */
2235 aOffset[i] = 0;
2236 }
drh9188b382004-05-14 21:12:22 +00002237 }
danielk19775f096132008-03-28 15:44:09 +00002238 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002239 sMem.flags = MEM_Null;
2240
danielk19779792eef2006-01-13 15:58:43 +00002241 /* If we have read more header data than was contained in the header,
2242 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002243 ** record, or if the end of the last field appears to be before the end
2244 ** of the record (when all fields present), then we must be dealing
2245 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002246 */
drh6658cd92010-02-05 14:12:53 +00002247 if( (zIdx > zEndHdr) || (offset > payloadSize)
2248 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002249 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002250 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002251 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002252 }
danielk1977192ac1d2004-05-10 07:17:30 +00002253
danielk197736963fd2005-02-19 08:18:05 +00002254 /* Get the column information. If aOffset[p2] is non-zero, then
2255 ** deserialize the value from the record. If aOffset[p2] is zero,
2256 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002257 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002258 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002259 */
danielk197736963fd2005-02-19 08:18:05 +00002260 if( aOffset[p2] ){
2261 assert( rc==SQLITE_OK );
2262 if( zRec ){
danielk1977808ec7c2008-07-29 10:18:57 +00002263 sqlite3VdbeMemReleaseExternal(pDest);
2264 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002265 }else{
2266 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002267 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002268 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002269 if( rc!=SQLITE_OK ){
2270 goto op_column_out;
2271 }
2272 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002273 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002274 }
drhd4e70eb2008-01-02 00:34:36 +00002275 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002276 }else{
danielk197760585dd2008-01-03 08:08:40 +00002277 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002278 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002279 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00002280 assert( pDest->flags&MEM_Null );
danielk1977aee18ef2005-03-09 12:26:50 +00002281 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002282 }
drhfebe1062004-08-28 18:17:48 +00002283
2284 /* If we dynamically allocated space to hold the data (in the
2285 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002286 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002287 ** This prevents a memory copy.
2288 */
danielk19775f096132008-03-28 15:44:09 +00002289 if( sMem.zMalloc ){
2290 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002291 assert( !(pDest->flags & MEM_Dyn) );
2292 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2293 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002294 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002295 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002296 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002297 }
drhfebe1062004-08-28 18:17:48 +00002298
drhd4e70eb2008-01-02 00:34:36 +00002299 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002300
danielk19773c9cc8d2005-01-17 03:40:08 +00002301op_column_out:
drhb7654112008-01-12 12:48:07 +00002302 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002303 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002304 break;
2305}
2306
danielk1977751de562008-04-18 09:01:15 +00002307/* Opcode: Affinity P1 P2 * P4 *
2308**
2309** Apply affinities to a range of P2 registers starting with P1.
2310**
2311** P4 is a string that is P2 characters long. The nth character of the
2312** string indicates the column affinity that should be used for the nth
2313** memory cell in the range.
2314*/
2315case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002316 const char *zAffinity; /* The affinity to be applied */
2317 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002318
drh856c1032009-06-02 15:21:42 +00002319 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002320 assert( zAffinity!=0 );
2321 assert( zAffinity[pOp->p2]==0 );
2322 pIn1 = &aMem[pOp->p1];
2323 while( (cAff = *(zAffinity++))!=0 ){
2324 assert( pIn1 <= &p->aMem[p->nMem] );
2325 ExpandBlob(pIn1);
2326 applyAffinity(pIn1, cAff, encoding);
2327 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002328 }
2329 break;
2330}
2331
drh1db639c2008-01-17 02:36:28 +00002332/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002333**
drh1db639c2008-01-17 02:36:28 +00002334** Convert P2 registers beginning with P1 into a single entry
drh7a224de2004-06-02 01:22:02 +00002335** suitable for use as a data record in a database table or as a key
shane21e7feb2008-05-30 15:59:49 +00002336** in an index. The details of the format are irrelevant as long as
drh1e968a02008-03-25 00:22:21 +00002337** the OP_Column opcode can decode the record later.
2338** Refer to source code comments for the details of the record
drh7a224de2004-06-02 01:22:02 +00002339** format.
2340**
danielk1977751de562008-04-18 09:01:15 +00002341** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002342** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002343** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002344**
drh8a512562005-11-14 22:29:05 +00002345** The mapping from character to affinity is given by the SQLITE_AFF_
2346** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002347**
drh66a51672008-01-03 00:01:23 +00002348** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002349*/
drh1db639c2008-01-17 02:36:28 +00002350case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002351 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2352 Mem *pRec; /* The new record */
2353 u64 nData; /* Number of bytes of data space */
2354 int nHdr; /* Number of bytes of header space */
2355 i64 nByte; /* Data space required for this record */
2356 int nZero; /* Number of zero bytes at the end of the record */
2357 int nVarint; /* Number of bytes in a varint */
2358 u32 serial_type; /* Type field */
2359 Mem *pData0; /* First field to be combined into the record */
2360 Mem *pLast; /* Last field of the record */
2361 int nField; /* Number of fields in the record */
2362 char *zAffinity; /* The affinity string for the record */
2363 int file_format; /* File format to use for encoding */
2364 int i; /* Space used in zNewRecord[] */
2365 int len; /* Length of a field */
2366
drhf3218fe2004-05-28 08:21:02 +00002367 /* Assuming the record contains N fields, the record format looks
2368 ** like this:
2369 **
drh7a224de2004-06-02 01:22:02 +00002370 ** ------------------------------------------------------------------------
2371 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2372 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002373 **
drh9cbf3422008-01-17 16:22:13 +00002374 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2375 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002376 **
2377 ** Each type field is a varint representing the serial type of the
2378 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002379 ** hdr-size field is also a varint which is the offset from the beginning
2380 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002381 */
drh856c1032009-06-02 15:21:42 +00002382 nData = 0; /* Number of bytes of data space */
2383 nHdr = 0; /* Number of bytes of header space */
2384 nByte = 0; /* Data space required for this record */
2385 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002386 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002387 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002388 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drha6c2ed92009-11-14 23:22:23 +00002389 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002390 nField = pOp->p2;
2391 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002392 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002393
drhf3218fe2004-05-28 08:21:02 +00002394 /* Loop through the elements that will make up the record to figure
2395 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002396 */
drha2a49dc2008-01-02 14:28:13 +00002397 for(pRec=pData0; pRec<=pLast; pRec++){
drhd3d39e92004-05-20 22:16:29 +00002398 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002399 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002400 }
danielk1977d908f5a2007-05-11 07:08:28 +00002401 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002402 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002403 }
drhd946db02005-12-29 19:23:06 +00002404 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002405 len = sqlite3VdbeSerialTypeLen(serial_type);
2406 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002407 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002408 if( pRec->flags & MEM_Zero ){
2409 /* Only pure zero-filled BLOBs can be input to this Opcode.
2410 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002411 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002412 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002413 nZero = 0;
2414 }
danielk19778d059842004-05-12 11:24:02 +00002415 }
danielk19773d1bfea2004-05-14 11:00:53 +00002416
drhf3218fe2004-05-28 08:21:02 +00002417 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002418 nHdr += nVarint = sqlite3VarintLen(nHdr);
2419 if( nVarint<sqlite3VarintLen(nHdr) ){
2420 nHdr++;
2421 }
drhfdf972a2007-05-02 13:30:27 +00002422 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002423 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002424 goto too_big;
2425 }
drhf3218fe2004-05-28 08:21:02 +00002426
danielk1977a7a8e142008-02-13 18:25:27 +00002427 /* Make sure the output register has a buffer large enough to store
2428 ** the new record. The output register (pOp->p3) is not allowed to
2429 ** be one of the input registers (because the following call to
2430 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2431 */
2432 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
drha6c2ed92009-11-14 23:22:23 +00002433 pOut = &aMem[pOp->p3];
drh9c1905f2008-12-10 22:32:56 +00002434 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002435 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002436 }
danielk1977a7a8e142008-02-13 18:25:27 +00002437 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002438
2439 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002440 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002441 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002442 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002443 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002444 }
drha2a49dc2008-01-02 14:28:13 +00002445 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002446 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002447 }
drhfdf972a2007-05-02 13:30:27 +00002448 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002449
drh9cbf3422008-01-17 16:22:13 +00002450 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002451 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002452 pOut->flags = MEM_Blob | MEM_Dyn;
2453 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002454 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002455 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002456 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002457 }
drh477df4b2008-01-05 18:48:24 +00002458 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002459 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002460 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002461 break;
2462}
2463
danielk1977a5533162009-02-24 10:01:51 +00002464/* Opcode: Count P1 P2 * * *
2465**
2466** Store the number of entries (an integer value) in the table or index
2467** opened by cursor P1 in register P2
2468*/
2469#ifndef SQLITE_OMIT_BTREECOUNT
2470case OP_Count: { /* out2-prerelease */
2471 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002472 BtCursor *pCrsr;
2473
2474 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh818e39a2009-04-02 20:27:28 +00002475 if( pCrsr ){
2476 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2477 }else{
2478 nEntry = 0;
2479 }
danielk1977a5533162009-02-24 10:01:51 +00002480 pOut->u.i = nEntry;
2481 break;
2482}
2483#endif
2484
danielk1977fd7f0452008-12-17 17:30:26 +00002485/* Opcode: Savepoint P1 * * P4 *
2486**
2487** Open, release or rollback the savepoint named by parameter P4, depending
2488** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2489** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2490*/
2491case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002492 int p1; /* Value of P1 operand */
2493 char *zName; /* Name of savepoint */
2494 int nName;
2495 Savepoint *pNew;
2496 Savepoint *pSavepoint;
2497 Savepoint *pTmp;
2498 int iSavepoint;
2499 int ii;
2500
2501 p1 = pOp->p1;
2502 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002503
2504 /* Assert that the p1 parameter is valid. Also that if there is no open
2505 ** transaction, then there cannot be any savepoints.
2506 */
2507 assert( db->pSavepoint==0 || db->autoCommit==0 );
2508 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2509 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2510 assert( checkSavepointCount(db) );
2511
2512 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002513 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002514 /* A new savepoint cannot be created if there are active write
2515 ** statements (i.e. open read/write incremental blob handles).
2516 */
2517 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2518 "SQL statements in progress");
2519 rc = SQLITE_BUSY;
2520 }else{
drh856c1032009-06-02 15:21:42 +00002521 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002522
2523 /* Create a new savepoint structure. */
2524 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2525 if( pNew ){
2526 pNew->zName = (char *)&pNew[1];
2527 memcpy(pNew->zName, zName, nName+1);
2528
2529 /* If there is no open transaction, then mark this as a special
2530 ** "transaction savepoint". */
2531 if( db->autoCommit ){
2532 db->autoCommit = 0;
2533 db->isTransactionSavepoint = 1;
2534 }else{
2535 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002536 }
danielk1977fd7f0452008-12-17 17:30:26 +00002537
2538 /* Link the new savepoint into the database handle's list. */
2539 pNew->pNext = db->pSavepoint;
2540 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002541 pNew->nDeferredCons = db->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002542 }
2543 }
2544 }else{
drh856c1032009-06-02 15:21:42 +00002545 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002546
2547 /* Find the named savepoint. If there is no such savepoint, then an
2548 ** an error is returned to the user. */
2549 for(
drh856c1032009-06-02 15:21:42 +00002550 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002551 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002552 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002553 ){
2554 iSavepoint++;
2555 }
2556 if( !pSavepoint ){
2557 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2558 rc = SQLITE_ERROR;
2559 }else if(
2560 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2561 ){
2562 /* It is not possible to release (commit) a savepoint if there are
2563 ** active write statements. It is not possible to rollback a savepoint
2564 ** if there are any active statements at all.
2565 */
2566 sqlite3SetString(&p->zErrMsg, db,
2567 "cannot %s savepoint - SQL statements in progress",
2568 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2569 );
2570 rc = SQLITE_BUSY;
2571 }else{
2572
2573 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002574 ** and this is a RELEASE command, then the current transaction
2575 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002576 */
2577 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2578 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002579 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002580 goto vdbe_return;
2581 }
danielk1977fd7f0452008-12-17 17:30:26 +00002582 db->autoCommit = 1;
2583 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2584 p->pc = pc;
2585 db->autoCommit = 0;
2586 p->rc = rc = SQLITE_BUSY;
2587 goto vdbe_return;
2588 }
danielk197734cf35d2008-12-18 18:31:38 +00002589 db->isTransactionSavepoint = 0;
2590 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002591 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002592 iSavepoint = db->nSavepoint - iSavepoint - 1;
2593 for(ii=0; ii<db->nDb; ii++){
2594 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2595 if( rc!=SQLITE_OK ){
2596 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002597 }
danielk1977fd7f0452008-12-17 17:30:26 +00002598 }
drh9f0bbf92009-01-02 21:08:09 +00002599 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002600 sqlite3ExpirePreparedStatements(db);
2601 sqlite3ResetInternalSchema(db, 0);
2602 }
2603 }
2604
2605 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2606 ** savepoints nested inside of the savepoint being operated on. */
2607 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002608 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002609 db->pSavepoint = pTmp->pNext;
2610 sqlite3DbFree(db, pTmp);
2611 db->nSavepoint--;
2612 }
2613
dan1da40a32009-09-19 17:00:31 +00002614 /* If it is a RELEASE, then destroy the savepoint being operated on
2615 ** too. If it is a ROLLBACK TO, then set the number of deferred
2616 ** constraint violations present in the database to the value stored
2617 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002618 if( p1==SAVEPOINT_RELEASE ){
2619 assert( pSavepoint==db->pSavepoint );
2620 db->pSavepoint = pSavepoint->pNext;
2621 sqlite3DbFree(db, pSavepoint);
2622 if( !isTransaction ){
2623 db->nSavepoint--;
2624 }
dan1da40a32009-09-19 17:00:31 +00002625 }else{
2626 db->nDeferredCons = pSavepoint->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002627 }
2628 }
2629 }
2630
2631 break;
2632}
2633
drh98757152008-01-09 23:04:12 +00002634/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002635**
2636** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002637** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002638** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2639** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002640**
2641** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002642*/
drh9cbf3422008-01-17 16:22:13 +00002643case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002644 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002645 int iRollback;
drh856c1032009-06-02 15:21:42 +00002646 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002647
drh856c1032009-06-02 15:21:42 +00002648 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002649 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002650 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002651 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002652 assert( desiredAutoCommit==1 || iRollback==0 );
drh92f02c32004-09-02 14:57:08 +00002653 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002654
shane68c02732009-06-09 18:14:18 +00002655 if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){
drhad4a4b82008-11-05 16:37:34 +00002656 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002657 ** still running, and a transaction is active, return an error indicating
2658 ** that the other VMs must complete first.
2659 */
drhad4a4b82008-11-05 16:37:34 +00002660 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2661 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002662 rc = SQLITE_BUSY;
drh9eb8cbe2009-06-19 22:23:41 +00002663 }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){
drhad4a4b82008-11-05 16:37:34 +00002664 /* If this instruction implements a COMMIT and other VMs are writing
2665 ** return an error indicating that the other VMs must complete first.
2666 */
2667 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2668 "SQL statements in progress");
2669 rc = SQLITE_BUSY;
2670 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002671 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002672 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002673 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002674 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002675 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002676 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002677 }else{
shane7d3846a2008-12-11 02:58:26 +00002678 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002679 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002680 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002681 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002682 p->rc = rc = SQLITE_BUSY;
2683 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002684 }
danielk19771d850a72004-05-31 08:26:49 +00002685 }
danielk1977bd434552009-03-18 10:33:00 +00002686 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002687 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002688 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002689 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002690 }else{
drh900b31e2007-08-28 02:27:51 +00002691 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002692 }
drh900b31e2007-08-28 02:27:51 +00002693 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002694 }else{
drhf089aa42008-07-08 19:34:06 +00002695 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002696 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002697 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002698 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002699
2700 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002701 }
2702 break;
2703}
2704
drh98757152008-01-09 23:04:12 +00002705/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002706**
2707** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002708** opcode is encountered. Depending on the ON CONFLICT setting, the
2709** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002710**
drh001bbcb2003-03-19 03:14:00 +00002711** P1 is the index of the database file on which the transaction is
2712** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002713** file used for temporary tables. Indices of 2 or more are used for
2714** attached databases.
drhcabb0812002-09-14 13:47:32 +00002715**
drh80242052004-06-09 00:48:12 +00002716** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002717** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002718** other process can start another write transaction while this transaction is
2719** underway. Starting a write transaction also creates a rollback journal. A
2720** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002721** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2722** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002723**
dane0af83a2009-09-08 19:15:01 +00002724** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2725** true (this flag is set if the Vdbe may modify more than one row and may
2726** throw an ABORT exception), a statement transaction may also be opened.
2727** More specifically, a statement transaction is opened iff the database
2728** connection is currently not in autocommit mode, or if there are other
2729** active statements. A statement transaction allows the affects of this
2730** VDBE to be rolled back after an error without having to roll back the
2731** entire transaction. If no error is encountered, the statement transaction
2732** will automatically commit when the VDBE halts.
2733**
danielk1977ee5741e2004-05-31 10:01:34 +00002734** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002735*/
drh9cbf3422008-01-17 16:22:13 +00002736case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002737 Btree *pBt;
2738
drh653b82a2009-06-22 11:10:47 +00002739 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2740 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
2741 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002742
danielk197724162fe2004-06-04 06:22:00 +00002743 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002744 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002745 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002746 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002747 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002748 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002749 }
drh9e9f1bd2009-10-13 15:36:51 +00002750 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00002751 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002752 }
dane0af83a2009-09-08 19:15:01 +00002753
2754 if( pOp->p2 && p->usesStmtJournal
2755 && (db->autoCommit==0 || db->activeVdbeCnt>1)
2756 ){
2757 assert( sqlite3BtreeIsInTrans(pBt) );
2758 if( p->iStatement==0 ){
2759 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2760 db->nStatement++;
2761 p->iStatement = db->nSavepoint + db->nStatement;
2762 }
2763 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
dan1da40a32009-09-19 17:00:31 +00002764
2765 /* Store the current value of the database handles deferred constraint
2766 ** counter. If the statement transaction needs to be rolled back,
2767 ** the value of this counter needs to be restored too. */
2768 p->nStmtDefCons = db->nDeferredCons;
dane0af83a2009-09-08 19:15:01 +00002769 }
drhb86ccfb2003-01-28 23:13:10 +00002770 }
drh5e00f6c2001-09-13 13:46:56 +00002771 break;
2772}
2773
drhb1fdb2a2008-01-05 04:06:03 +00002774/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002775**
drh9cbf3422008-01-17 16:22:13 +00002776** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002777** P3==1 is the schema version. P3==2 is the database format.
2778** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002779** the main database file and P1==1 is the database file used to store
2780** temporary tables.
drh4a324312001-12-21 14:30:42 +00002781**
drh50e5dad2001-09-15 00:57:28 +00002782** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002783** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002784** executing this instruction.
2785*/
drh4c583122008-01-04 22:01:03 +00002786case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002787 int iMeta;
drh856c1032009-06-02 15:21:42 +00002788 int iDb;
2789 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00002790
drh856c1032009-06-02 15:21:42 +00002791 iDb = pOp->p1;
2792 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00002793 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002794 assert( iDb>=0 && iDb<db->nDb );
2795 assert( db->aDb[iDb].pBt!=0 );
drhfb982642007-08-30 01:19:59 +00002796 assert( (p->btreeMask & (1<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00002797
danielk1977602b4662009-07-02 07:47:33 +00002798 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002799 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00002800 break;
2801}
2802
drh98757152008-01-09 23:04:12 +00002803/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002804**
drh98757152008-01-09 23:04:12 +00002805** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00002806** into cookie number P2 of database P1. P2==1 is the schema version.
2807** P2==2 is the database format. P2==3 is the recommended pager cache
2808** size, and so forth. P1==0 is the main database file and P1==1 is the
2809** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002810**
2811** A transaction must be started before executing this opcode.
2812*/
drh9cbf3422008-01-17 16:22:13 +00002813case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002814 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002815 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002816 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002817 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002818 pDb = &db->aDb[pOp->p1];
2819 assert( pDb->pBt!=0 );
drh3c657212009-11-17 23:59:58 +00002820 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00002821 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002822 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00002823 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
2824 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00002825 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002826 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002827 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00002828 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00002829 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002830 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002831 }
drhfd426c62006-01-30 15:34:22 +00002832 if( pOp->p1==1 ){
2833 /* Invalidate all prepared statements whenever the TEMP database
2834 ** schema is changed. Ticket #1644 */
2835 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00002836 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00002837 }
drh50e5dad2001-09-15 00:57:28 +00002838 break;
2839}
2840
drh4a324312001-12-21 14:30:42 +00002841/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002842**
drh001bbcb2003-03-19 03:14:00 +00002843** Check the value of global database parameter number 0 (the
2844** schema version) and make sure it is equal to P2.
2845** P1 is the database number which is 0 for the main database file
2846** and 1 for the file holding temporary tables and some higher number
2847** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002848**
2849** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002850** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002851** and that the current process needs to reread the schema.
2852**
2853** Either a transaction needs to have been started or an OP_Open needs
2854** to be executed (to establish a read lock) before this opcode is
2855** invoked.
2856*/
drh9cbf3422008-01-17 16:22:13 +00002857case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002858 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002859 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002860 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00002861 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhc275b4e2004-07-19 17:25:24 +00002862 pBt = db->aDb[pOp->p1].pBt;
2863 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00002864 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc275b4e2004-07-19 17:25:24 +00002865 }else{
drhc275b4e2004-07-19 17:25:24 +00002866 iMeta = 0;
2867 }
danielk1977602b4662009-07-02 07:47:33 +00002868 if( iMeta!=pOp->p2 ){
drh633e6d52008-07-28 19:34:53 +00002869 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00002870 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00002871 /* If the schema-cookie from the database file matches the cookie
2872 ** stored with the in-memory representation of the schema, do
2873 ** not reload the schema from the database file.
2874 **
shane21e7feb2008-05-30 15:59:49 +00002875 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00002876 ** Often, v-tables store their data in other SQLite tables, which
2877 ** are queried from within xNext() and other v-table methods using
2878 ** prepared queries. If such a query is out-of-date, we do not want to
2879 ** discard the database schema, as the user code implementing the
2880 ** v-table would have to be ready for the sqlite3_vtab structure itself
2881 ** to be invalidated whenever sqlite3_step() is called from within
2882 ** a v-table method.
2883 */
2884 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2885 sqlite3ResetInternalSchema(db, pOp->p1);
2886 }
2887
drhf6d8ab82007-01-12 23:43:42 +00002888 sqlite3ExpirePreparedStatements(db);
drh50e5dad2001-09-15 00:57:28 +00002889 rc = SQLITE_SCHEMA;
2890 }
2891 break;
2892}
2893
drh98757152008-01-09 23:04:12 +00002894/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00002895**
drhecdc7532001-09-23 02:35:53 +00002896** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00002897** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00002898** P3==0 means the main database, P3==1 means the database used for
2899** temporary tables, and P3>1 means used the corresponding attached
2900** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00002901** values need not be contiguous but all P1 values should be small integers.
2902** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002903**
drh98757152008-01-09 23:04:12 +00002904** If P5!=0 then use the content of register P2 as the root page, not
2905** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00002906**
drhb19a2bc2001-09-16 00:13:26 +00002907** There will be a read lock on the database whenever there is an
2908** open cursor. If the database was unlocked prior to this instruction
2909** then a read lock is acquired as part of this instruction. A read
2910** lock allows other processes to read the database but prohibits
2911** any other process from modifying the database. The read lock is
2912** released when all cursors are closed. If this instruction attempts
2913** to get a read lock but fails, the script terminates with an
2914** SQLITE_BUSY error code.
2915**
danielk1977d336e222009-02-20 10:58:41 +00002916** The P4 value may be either an integer (P4_INT32) or a pointer to
2917** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2918** structure, then said structure defines the content and collating
2919** sequence of the index being opened. Otherwise, if P4 is an integer
2920** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00002921**
drh001bbcb2003-03-19 03:14:00 +00002922** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002923*/
drh98757152008-01-09 23:04:12 +00002924/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00002925**
2926** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00002927** page is P2. Or if P5!=0 use the content of register P2 to find the
2928** root page.
drhecdc7532001-09-23 02:35:53 +00002929**
danielk1977d336e222009-02-20 10:58:41 +00002930** The P4 value may be either an integer (P4_INT32) or a pointer to
2931** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
2932** structure, then said structure defines the content and collating
2933** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00002934** value, it is set to the number of columns in the table, or to the
2935** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00002936**
drh001bbcb2003-03-19 03:14:00 +00002937** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002938** in read/write mode. For a given table, there can be one or more read-only
2939** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002940**
drh001bbcb2003-03-19 03:14:00 +00002941** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002942*/
drh9cbf3422008-01-17 16:22:13 +00002943case OP_OpenRead:
2944case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00002945 int nField;
2946 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00002947 int p2;
2948 int iDb;
drhf57b3392001-10-08 13:22:32 +00002949 int wrFlag;
2950 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00002951 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00002952 Db *pDb;
drh856c1032009-06-02 15:21:42 +00002953
danfa401de2009-10-16 14:55:03 +00002954 if( p->expired ){
2955 rc = SQLITE_ABORT;
2956 break;
2957 }
2958
drh856c1032009-06-02 15:21:42 +00002959 nField = 0;
2960 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00002961 p2 = pOp->p2;
2962 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00002963 assert( iDb>=0 && iDb<db->nDb );
drhfb982642007-08-30 01:19:59 +00002964 assert( (p->btreeMask & (1<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00002965 pDb = &db->aDb[iDb];
2966 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002967 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002968 if( pOp->opcode==OP_OpenWrite ){
2969 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002970 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2971 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002972 }
2973 }else{
2974 wrFlag = 0;
2975 }
drh98757152008-01-09 23:04:12 +00002976 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00002977 assert( p2>0 );
2978 assert( p2<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002979 pIn2 = &aMem[p2];
drh9cbf3422008-01-17 16:22:13 +00002980 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00002981 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00002982 /* The p2 value always comes from a prior OP_CreateTable opcode and
2983 ** that opcode will always set the p2 value to 2 or more or else fail.
2984 ** If there were a failure, the prepared statement would have halted
2985 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00002986 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00002987 rc = SQLITE_CORRUPT_BKPT;
2988 goto abort_due_to_error;
2989 }
drh5edc3122001-09-13 21:53:09 +00002990 }
danielk1977d336e222009-02-20 10:58:41 +00002991 if( pOp->p4type==P4_KEYINFO ){
2992 pKeyInfo = pOp->p4.pKeyInfo;
2993 pKeyInfo->enc = ENC(p->db);
2994 nField = pKeyInfo->nField+1;
2995 }else if( pOp->p4type==P4_INT32 ){
2996 nField = pOp->p4.i;
2997 }
drh653b82a2009-06-22 11:10:47 +00002998 assert( pOp->p1>=0 );
2999 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003000 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003001 pCur->nullRow = 1;
danielk1977d336e222009-02-20 10:58:41 +00003002 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3003 pCur->pKeyInfo = pKeyInfo;
3004
danielk1977172114a2009-07-07 15:47:12 +00003005 /* Since it performs no memory allocation or IO, the only values that
3006 ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
3007 ** SQLITE_EMPTY is only returned when attempting to open the table
3008 ** rooted at page 1 of a zero-byte database. */
3009 assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
3010 if( rc==SQLITE_EMPTY ){
3011 pCur->pCursor = 0;
3012 rc = SQLITE_OK;
danielk197724162fe2004-06-04 06:22:00 +00003013 }
danielk1977172114a2009-07-07 15:47:12 +00003014
3015 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3016 ** SQLite used to check if the root-page flags were sane at this point
3017 ** and report database corruption if they were not, but this check has
3018 ** since moved into the btree layer. */
3019 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3020 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003021 break;
3022}
3023
drh98757152008-01-09 23:04:12 +00003024/* Opcode: OpenEphemeral P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003025**
drhb9bb7c12006-06-11 23:41:55 +00003026** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003027** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003028** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003029** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003030**
drh25d3adb2010-04-05 15:11:08 +00003031** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003032** The cursor points to a BTree table if P4==0 and to a BTree index
3033** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003034** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003035**
3036** This opcode was once called OpenTemp. But that created
3037** confusion because the term "temp table", might refer either
3038** to a TEMP table at the SQL level, or to a table opened by
3039** this opcode. Then this opcode was call OpenVirtual. But
3040** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00003041*/
drha21a64d2010-04-06 22:33:55 +00003042/* Opcode: OpenAutoindex P1 P2 * P4 *
3043**
3044** This opcode works the same as OP_OpenEphemeral. It has a
3045** different name to distinguish its use. Tables created using
3046** by this opcode will be used for automatically created transient
3047** indices in joins.
3048*/
3049case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003050case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003051 VdbeCursor *pCx;
drh33f4e022007-09-03 15:19:34 +00003052 static const int openFlags =
3053 SQLITE_OPEN_READWRITE |
3054 SQLITE_OPEN_CREATE |
3055 SQLITE_OPEN_EXCLUSIVE |
3056 SQLITE_OPEN_DELETEONCLOSE |
3057 SQLITE_OPEN_TRANSIENT_DB;
3058
drh653b82a2009-06-22 11:10:47 +00003059 assert( pOp->p1>=0 );
3060 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003061 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003062 pCx->nullRow = 1;
drh33f4e022007-09-03 15:19:34 +00003063 rc = sqlite3BtreeFactory(db, 0, 1, SQLITE_DEFAULT_TEMP_CACHE_SIZE, openFlags,
3064 &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00003065 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003066 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003067 }
3068 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003069 /* If a transient index is required, create it by calling
3070 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
3071 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00003072 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003073 */
danielk19772dca4ac2008-01-03 11:50:29 +00003074 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003075 int pgno;
drh66a51672008-01-03 00:01:23 +00003076 assert( pOp->p4type==P4_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00003077 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00003078 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003079 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003080 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003081 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003082 pCx->pKeyInfo = pOp->p4.pKeyInfo;
danielk197714db2662006-01-09 16:12:04 +00003083 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003084 }
drhf0863fe2005-06-12 21:35:51 +00003085 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003086 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003087 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003088 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003089 }
drh5e00f6c2001-09-13 13:46:56 +00003090 }
drhf0863fe2005-06-12 21:35:51 +00003091 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003092 break;
3093}
3094
danielk1977d336e222009-02-20 10:58:41 +00003095/* Opcode: OpenPseudo P1 P2 P3 * *
drh70ce3f02003-04-15 19:22:22 +00003096**
3097** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003098** row of data. The content of that one row in the content of memory
3099** register P2. In other words, cursor P1 becomes an alias for the
3100** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003101**
drh2d8d7ce2010-02-15 15:17:05 +00003102** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003103** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003104** individual columns using the OP_Column opcode. The OP_Column opcode
3105** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003106**
3107** P3 is the number of fields in the records that will be stored by
3108** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003109*/
drh9cbf3422008-01-17 16:22:13 +00003110case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003111 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003112
drh653b82a2009-06-22 11:10:47 +00003113 assert( pOp->p1>=0 );
3114 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003115 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003116 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003117 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003118 pCx->isTable = 1;
3119 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00003120 break;
3121}
3122
drh98757152008-01-09 23:04:12 +00003123/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003124**
3125** Close a cursor previously opened as P1. If P1 is not
3126** currently open, this instruction is a no-op.
3127*/
drh9cbf3422008-01-17 16:22:13 +00003128case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003129 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3130 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3131 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003132 break;
3133}
3134
drh959403f2008-12-12 17:56:16 +00003135/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003136**
danielk1977b790c6c2008-04-18 10:25:24 +00003137** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003138** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003139** to an SQL index, then P3 is the first in an array of P4 registers
3140** that are used as an unpacked index key.
3141**
3142** Reposition cursor P1 so that it points to the smallest entry that
3143** is greater than or equal to the key value. If there are no records
3144** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003145**
drh959403f2008-12-12 17:56:16 +00003146** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003147*/
drh959403f2008-12-12 17:56:16 +00003148/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003149**
danielk1977b790c6c2008-04-18 10:25:24 +00003150** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003151** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003152** to an SQL index, then P3 is the first in an array of P4 registers
3153** that are used as an unpacked index key.
3154**
3155** Reposition cursor P1 so that it points to the smallest entry that
3156** is greater than the key value. If there are no records greater than
3157** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003158**
drh959403f2008-12-12 17:56:16 +00003159** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003160*/
drh959403f2008-12-12 17:56:16 +00003161/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003162**
danielk1977b790c6c2008-04-18 10:25:24 +00003163** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003164** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003165** to an SQL index, then P3 is the first in an array of P4 registers
3166** that are used as an unpacked index key.
3167**
3168** Reposition cursor P1 so that it points to the largest entry that
3169** is less than the key value. If there are no records less than
3170** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003171**
drh959403f2008-12-12 17:56:16 +00003172** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003173*/
drh959403f2008-12-12 17:56:16 +00003174/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003175**
danielk1977b790c6c2008-04-18 10:25:24 +00003176** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003177** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003178** to an SQL index, then P3 is the first in an array of P4 registers
3179** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003180**
danielk1977b790c6c2008-04-18 10:25:24 +00003181** Reposition cursor P1 so that it points to the largest entry that
3182** is less than or equal to the key value. If there are no records
3183** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003184**
drh959403f2008-12-12 17:56:16 +00003185** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003186*/
drh959403f2008-12-12 17:56:16 +00003187case OP_SeekLt: /* jump, in3 */
3188case OP_SeekLe: /* jump, in3 */
3189case OP_SeekGe: /* jump, in3 */
3190case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003191 int res;
3192 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003193 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003194 UnpackedRecord r;
3195 int nField;
3196 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003197
drh653b82a2009-06-22 11:10:47 +00003198 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003199 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003200 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003201 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003202 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003203 assert( OP_SeekLe == OP_SeekLt+1 );
3204 assert( OP_SeekGe == OP_SeekLt+2 );
3205 assert( OP_SeekGt == OP_SeekLt+3 );
drh70ce3f02003-04-15 19:22:22 +00003206 if( pC->pCursor!=0 ){
drh7cf6e4d2004-05-19 14:56:55 +00003207 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003208 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003209 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003210 /* The input value in P3 might be of any type: integer, real, string,
3211 ** blob, or NULL. But it needs to be an integer before we can do
3212 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003213 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003214 applyNumericAffinity(pIn3);
3215 iKey = sqlite3VdbeIntValue(pIn3);
3216 pC->rowidIsValid = 0;
3217
3218 /* If the P3 value could not be converted into an integer without
3219 ** loss of information, then special processing is required... */
3220 if( (pIn3->flags & MEM_Int)==0 ){
3221 if( (pIn3->flags & MEM_Real)==0 ){
3222 /* If the P3 value cannot be converted into any kind of a number,
3223 ** then the seek is not possible, so jump to P2 */
3224 pc = pOp->p2 - 1;
3225 break;
3226 }
3227 /* If we reach this point, then the P3 value must be a floating
3228 ** point number. */
3229 assert( (pIn3->flags & MEM_Real)!=0 );
3230
3231 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003232 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003233 ** integer. */
3234 res = 1;
3235 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003236 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003237 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3238 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3239 }
3240 }else{
drh1f350122009-11-13 20:52:43 +00003241 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003242 rc = sqlite3BtreeLast(pC->pCursor, &res);
3243 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3244 }
3245 }
3246 if( res ){
3247 pc = pOp->p2 - 1;
3248 }
3249 break;
3250 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3251 /* Use the ceiling() function to convert real->int */
3252 if( pIn3->r > (double)iKey ) iKey++;
3253 }else{
3254 /* Use the floor() function to convert real->int */
3255 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3256 if( pIn3->r < (double)iKey ) iKey--;
3257 }
3258 }
drhe63d9992008-08-13 19:11:48 +00003259 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003260 if( rc!=SQLITE_OK ){
3261 goto abort_due_to_error;
3262 }
drh959403f2008-12-12 17:56:16 +00003263 if( res==0 ){
3264 pC->rowidIsValid = 1;
3265 pC->lastRowid = iKey;
3266 }
drh5e00f6c2001-09-13 13:46:56 +00003267 }else{
drh856c1032009-06-02 15:21:42 +00003268 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003269 assert( pOp->p4type==P4_INT32 );
3270 assert( nField>0 );
3271 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003272 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003273
3274 /* The next line of code computes as follows, only faster:
3275 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3276 ** r.flags = UNPACKED_INCRKEY;
3277 ** }else{
3278 ** r.flags = 0;
3279 ** }
3280 */
shaneh5e17e8b2009-12-03 04:40:47 +00003281 r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003282 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3283 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3284 assert( oc!=OP_SeekGe || r.flags==0 );
3285 assert( oc!=OP_SeekLt || r.flags==0 );
3286
drha6c2ed92009-11-14 23:22:23 +00003287 r.aMem = &aMem[pOp->p3];
drh039fc322009-11-17 18:31:47 +00003288 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003289 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003290 if( rc!=SQLITE_OK ){
3291 goto abort_due_to_error;
3292 }
drhf0863fe2005-06-12 21:35:51 +00003293 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003294 }
drha11846b2004-01-07 18:52:56 +00003295 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003296 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003297#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003298 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003299#endif
drh1f350122009-11-13 20:52:43 +00003300 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003301 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003302 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003303 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003304 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003305 }else{
3306 res = 0;
drh8721ce42001-11-07 14:22:00 +00003307 }
drh7cf6e4d2004-05-19 14:56:55 +00003308 }else{
drh959403f2008-12-12 17:56:16 +00003309 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3310 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003311 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3312 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003313 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003314 }else{
3315 /* res might be negative because the table is empty. Check to
3316 ** see if this is the case.
3317 */
drhf328bc82004-05-10 23:29:49 +00003318 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003319 }
drh1af3fdb2004-07-18 21:33:01 +00003320 }
drh91fd4d42008-01-19 20:11:25 +00003321 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003322 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003323 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003324 }
drhaa736092009-06-22 00:55:30 +00003325 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003326 /* This happens when attempting to open the sqlite3_master table
3327 ** for read access returns SQLITE_EMPTY. In this case always
3328 ** take the jump (since there are no records in the table).
3329 */
3330 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003331 }
drh5e00f6c2001-09-13 13:46:56 +00003332 break;
3333}
3334
drh959403f2008-12-12 17:56:16 +00003335/* Opcode: Seek P1 P2 * * *
3336**
3337** P1 is an open table cursor and P2 is a rowid integer. Arrange
3338** for P1 to move so that it points to the rowid given by P2.
3339**
3340** This is actually a deferred seek. Nothing actually happens until
3341** the cursor is used to read a record. That way, if no reads
3342** occur, no unnecessary I/O happens.
3343*/
3344case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003345 VdbeCursor *pC;
3346
drh653b82a2009-06-22 11:10:47 +00003347 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3348 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003349 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003350 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003351 assert( pC->isTable );
3352 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003353 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003354 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3355 pC->rowidIsValid = 0;
3356 pC->deferredMoveto = 1;
3357 }
3358 break;
3359}
3360
3361
drh8cff69d2009-11-12 19:59:44 +00003362/* Opcode: Found P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003363**
drh8cff69d2009-11-12 19:59:44 +00003364** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3365** P4>0 then register P3 is the first of P4 registers that form an unpacked
3366** record.
3367**
3368** Cursor P1 is on an index btree. If the record identified by P3 and P4
3369** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003370** P1 is left pointing at the matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003371*/
drh8cff69d2009-11-12 19:59:44 +00003372/* Opcode: NotFound P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003373**
drh8cff69d2009-11-12 19:59:44 +00003374** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3375** P4>0 then register P3 is the first of P4 registers that form an unpacked
3376** record.
3377**
3378** Cursor P1 is on an index btree. If the record identified by P3 and P4
3379** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3380** does contain an entry whose prefix matches the P3/P4 record then control
3381** falls through to the next instruction and P1 is left pointing at the
3382** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003383**
drhcb6d50e2008-08-21 19:28:30 +00003384** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003385*/
drh9cbf3422008-01-17 16:22:13 +00003386case OP_NotFound: /* jump, in3 */
3387case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003388 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003389 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003390 int res;
3391 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003392 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003393 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3394
dan0ff297e2009-09-25 17:03:14 +00003395#ifdef SQLITE_TEST
3396 sqlite3_found_count++;
3397#endif
3398
drh856c1032009-06-02 15:21:42 +00003399 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003400 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003401 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003402 pC = p->apCsr[pOp->p1];
3403 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003404 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003405 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003406
drhf0863fe2005-06-12 21:35:51 +00003407 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003408 if( pOp->p4.i>0 ){
3409 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003410 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003411 r.aMem = pIn3;
3412 r.flags = UNPACKED_PREFIX_MATCH;
3413 pIdxKey = &r;
3414 }else{
3415 assert( pIn3->flags & MEM_Blob );
3416 ExpandBlob(pIn3);
3417 pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
3418 aTempRec, sizeof(aTempRec));
3419 if( pIdxKey==0 ){
3420 goto no_mem;
3421 }
3422 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003423 }
drhe63d9992008-08-13 19:11:48 +00003424 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003425 if( pOp->p4.i==0 ){
3426 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
3427 }
danielk197777519402007-08-30 11:48:31 +00003428 if( rc!=SQLITE_OK ){
3429 break;
3430 }
3431 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003432 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003433 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003434 }
3435 if( pOp->opcode==OP_Found ){
3436 if( alreadyExists ) pc = pOp->p2 - 1;
3437 }else{
3438 if( !alreadyExists ) pc = pOp->p2 - 1;
3439 }
drh5e00f6c2001-09-13 13:46:56 +00003440 break;
3441}
3442
drh98757152008-01-09 23:04:12 +00003443/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003444**
drh8cff69d2009-11-12 19:59:44 +00003445** Cursor P1 is open on an index b-tree - that is to say, a btree which
3446** no data and where the key are records generated by OP_MakeRecord with
3447** the list field being the integer ROWID of the entry that the index
3448** entry refers to.
danielk1977de630352009-05-04 11:42:29 +00003449**
3450** The P3 register contains an integer record number. Call this record
3451** number R. Register P4 is the first in a set of N contiguous registers
3452** that make up an unpacked index key that can be used with cursor P1.
3453** The value of N can be inferred from the cursor. N includes the rowid
3454** value appended to the end of the index record. This rowid value may
3455** or may not be the same as R.
3456**
3457** If any of the N registers beginning with register P4 contains a NULL
3458** value, jump immediately to P2.
3459**
3460** Otherwise, this instruction checks if cursor P1 contains an entry
3461** where the first (N-1) fields match but the rowid value at the end
3462** of the index entry is not R. If there is no such entry, control jumps
3463** to instruction P2. Otherwise, the rowid of the conflicting index
3464** entry is copied to register P3 and control falls through to the next
3465** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003466**
drh9cbf3422008-01-17 16:22:13 +00003467** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003468*/
drh9cbf3422008-01-17 16:22:13 +00003469case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003470 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003471 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003472 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003473 u16 nField;
drha6c2ed92009-11-14 23:22:23 +00003474 Mem *aMx;
drh856c1032009-06-02 15:21:42 +00003475 UnpackedRecord r; /* B-Tree index search key */
3476 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003477
drh3c657212009-11-17 23:59:58 +00003478 pIn3 = &aMem[pOp->p3];
drha6c2ed92009-11-14 23:22:23 +00003479 aMx = &aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003480 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003481 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003482 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003483 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3484
3485 /* Find the index cursor. */
3486 pCx = p->apCsr[pOp->p1];
3487 assert( pCx->deferredMoveto==0 );
3488 pCx->seekResult = 0;
3489 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003490 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003491
3492 /* If any of the values are NULL, take the jump. */
3493 nField = pCx->pKeyInfo->nField;
3494 for(ii=0; ii<nField; ii++){
drha6c2ed92009-11-14 23:22:23 +00003495 if( aMx[ii].flags & MEM_Null ){
danielk1977de630352009-05-04 11:42:29 +00003496 pc = pOp->p2 - 1;
3497 pCrsr = 0;
3498 break;
3499 }
3500 }
drha6c2ed92009-11-14 23:22:23 +00003501 assert( (aMx[nField].flags & MEM_Null)==0 );
danielk1977de630352009-05-04 11:42:29 +00003502
drhf328bc82004-05-10 23:29:49 +00003503 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003504 /* Populate the index search key. */
3505 r.pKeyInfo = pCx->pKeyInfo;
3506 r.nField = nField + 1;
3507 r.flags = UNPACKED_PREFIX_SEARCH;
drha6c2ed92009-11-14 23:22:23 +00003508 r.aMem = aMx;
danielk1977452c9892004-05-13 05:16:15 +00003509
danielk1977de630352009-05-04 11:42:29 +00003510 /* Extract the value of R from register P3. */
3511 sqlite3VdbeMemIntegerify(pIn3);
3512 R = pIn3->u.i;
3513
3514 /* Search the B-Tree index. If no conflicting record is found, jump
3515 ** to P2. Otherwise, copy the rowid of the conflicting record to
3516 ** register P3 and fall through to the next instruction. */
3517 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3518 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003519 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003520 }else{
3521 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003522 }
drh9cfcf5d2002-01-29 18:41:24 +00003523 }
3524 break;
3525}
3526
drh9cbf3422008-01-17 16:22:13 +00003527/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003528**
drh9cbf3422008-01-17 16:22:13 +00003529** Use the content of register P3 as a integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003530** with that key does not exist in table of P1, then jump to P2.
3531** If the record does exist, then fall thru. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003532** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003533**
3534** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003535** operation assumes the key is an integer and that P1 is a table whereas
3536** NotFound assumes key is a blob constructed from MakeRecord and
3537** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003538**
drhcb6d50e2008-08-21 19:28:30 +00003539** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003540*/
drh9cbf3422008-01-17 16:22:13 +00003541case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003542 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003543 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003544 int res;
3545 u64 iKey;
3546
drh3c657212009-11-17 23:59:58 +00003547 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003548 assert( pIn3->flags & MEM_Int );
3549 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3550 pC = p->apCsr[pOp->p1];
3551 assert( pC!=0 );
3552 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003553 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003554 pCrsr = pC->pCursor;
3555 if( pCrsr!=0 ){
drh856c1032009-06-02 15:21:42 +00003556 res = 0;
drhaa736092009-06-22 00:55:30 +00003557 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003558 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003559 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003560 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003561 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003562 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003563 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003564 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003565 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003566 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003567 }
danielk1977de630352009-05-04 11:42:29 +00003568 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003569 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003570 /* This happens when an attempt to open a read cursor on the
3571 ** sqlite_master table returns SQLITE_EMPTY.
3572 */
danielk1977f7b9d662008-06-23 18:49:43 +00003573 pc = pOp->p2 - 1;
3574 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003575 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003576 }
drh6b125452002-01-28 15:53:03 +00003577 break;
3578}
3579
drh4c583122008-01-04 22:01:03 +00003580/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003581**
drh4c583122008-01-04 22:01:03 +00003582** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003583** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003584** The sequence number on the cursor is incremented after this
3585** instruction.
drh4db38a72005-09-01 12:16:28 +00003586*/
drh4c583122008-01-04 22:01:03 +00003587case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003588 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3589 assert( p->apCsr[pOp->p1]!=0 );
3590 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003591 break;
3592}
3593
3594
drh98757152008-01-09 23:04:12 +00003595/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003596**
drhf0863fe2005-06-12 21:35:51 +00003597** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003598** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003599** table that cursor P1 points to. The new record number is written
3600** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003601**
dan76d462e2009-08-30 11:42:51 +00003602** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3603** the largest previously generated record number. No new record numbers are
3604** allowed to be less than this value. When this value reaches its maximum,
3605** a SQLITE_FULL error is generated. The P3 register is updated with the '
3606** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003607** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003608*/
drh4c583122008-01-04 22:01:03 +00003609case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003610 i64 v; /* The new rowid */
3611 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3612 int res; /* Result of an sqlite3BtreeLast() */
3613 int cnt; /* Counter to limit the number of searches */
3614 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003615 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003616
drh856c1032009-06-02 15:21:42 +00003617 v = 0;
3618 res = 0;
drhaa736092009-06-22 00:55:30 +00003619 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3620 pC = p->apCsr[pOp->p1];
3621 assert( pC!=0 );
3622 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003623 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003624 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003625 /* The next rowid or record number (different terms for the same
3626 ** thing) is obtained in a two-step algorithm.
3627 **
3628 ** First we attempt to find the largest existing rowid and add one
3629 ** to that. But if the largest existing rowid is already the maximum
3630 ** positive integer, we have to fall through to the second
3631 ** probabilistic algorithm
3632 **
3633 ** The second algorithm is to select a rowid at random and see if
3634 ** it already exists in the table. If it does not exist, we have
3635 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003636 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003637 */
drhaa736092009-06-22 00:55:30 +00003638 assert( pC->isTable );
drh5e00f6c2001-09-13 13:46:56 +00003639 cnt = 0;
drhfe2093d2005-01-20 22:48:47 +00003640
drh75f86a42005-02-17 00:03:06 +00003641#ifdef SQLITE_32BIT_ROWID
3642# define MAX_ROWID 0x7fffffff
3643#else
drhfe2093d2005-01-20 22:48:47 +00003644 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3645 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3646 ** to provide the constant while making all compilers happy.
3647 */
danielk197764202cf2008-11-17 15:31:47 +00003648# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003649#endif
drhfe2093d2005-01-20 22:48:47 +00003650
drh5cf8e8c2002-02-19 22:42:05 +00003651 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003652 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3653 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003654 rc = sqlite3BtreeLast(pC->pCursor, &res);
3655 if( rc!=SQLITE_OK ){
3656 goto abort_due_to_error;
3657 }
drh32fbe342002-10-19 20:16:37 +00003658 if( res ){
drhc79c7612010-01-01 18:57:48 +00003659 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003660 }else{
drhea8ffdf2009-07-22 00:35:23 +00003661 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003662 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3663 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drh75f86a42005-02-17 00:03:06 +00003664 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003665 pC->useRandomRowid = 1;
3666 }else{
drhc79c7612010-01-01 18:57:48 +00003667 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003668 }
drh5cf8e8c2002-02-19 22:42:05 +00003669 }
drh3fc190c2001-09-14 03:24:23 +00003670 }
drh205f48e2004-11-05 00:43:11 +00003671
3672#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003673 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003674 /* Assert that P3 is a valid memory cell. */
3675 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003676 if( p->pFrame ){
3677 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003678 /* Assert that P3 is a valid memory cell. */
3679 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003680 pMem = &pFrame->aMem[pOp->p3];
3681 }else{
shaneabc6b892009-09-10 19:09:03 +00003682 /* Assert that P3 is a valid memory cell. */
3683 assert( pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003684 pMem = &aMem[pOp->p3];
dan76d462e2009-08-30 11:42:51 +00003685 }
dan76d462e2009-08-30 11:42:51 +00003686
3687 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003688 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003689 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003690 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003691 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003692 goto abort_due_to_error;
3693 }
drh3c024d62007-03-30 11:23:45 +00003694 if( v<pMem->u.i+1 ){
3695 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003696 }
drh3c024d62007-03-30 11:23:45 +00003697 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003698 }
3699#endif
3700
drh7f751222009-03-17 22:33:00 +00003701 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003702 }
3703 if( pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003704 /* IMPLEMENTATION-OF: R-48598-02938 If the largest ROWID is equal to the
3705 ** largest possible integer (9223372036854775807) then the database
3706 ** engine starts picking candidate ROWIDs at random until it finds one
3707 ** that is not previously used.
3708 */
drhaa736092009-06-22 00:55:30 +00003709 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3710 ** an AUTOINCREMENT table. */
drh9ed7a992009-06-26 15:14:55 +00003711 v = db->lastRowid;
drh5cf8e8c2002-02-19 22:42:05 +00003712 cnt = 0;
3713 do{
drh91fd4d42008-01-19 20:11:25 +00003714 if( cnt==0 && (v&0xffffff)==v ){
3715 v++;
3716 }else{
drh2fa18682008-03-19 14:15:34 +00003717 sqlite3_randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003718 if( cnt<5 ) v &= 0xffffff;
drh5cf8e8c2002-02-19 22:42:05 +00003719 }
drhaa736092009-06-22 00:55:30 +00003720 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, 0, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003721 cnt++;
drhaa736092009-06-22 00:55:30 +00003722 }while( cnt<100 && rc==SQLITE_OK && res==0 );
drhaa736092009-06-22 00:55:30 +00003723 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00003724 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00003725 goto abort_due_to_error;
3726 }
drh1eaa2692001-09-18 02:02:23 +00003727 }
drhf0863fe2005-06-12 21:35:51 +00003728 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003729 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003730 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003731 }
drh4c583122008-01-04 22:01:03 +00003732 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003733 break;
3734}
3735
danielk19771f4aa332008-01-03 09:51:55 +00003736/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003737**
jplyon5a564222003-06-02 06:15:58 +00003738** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003739** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00003740** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00003741** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00003742** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00003743**
danielk19771f4aa332008-01-03 09:51:55 +00003744** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3745** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003746** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003747** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003748**
drh3e9ca092009-09-08 01:14:48 +00003749** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
3750** the last seek operation (OP_NotExists) was a success, then this
3751** operation will not attempt to find the appropriate row before doing
3752** the insert but will instead overwrite the row that the cursor is
3753** currently pointing to. Presumably, the prior OP_NotExists opcode
3754** has already positioned the cursor correctly. This is an optimization
3755** that boosts performance by avoiding redundant seeks.
3756**
3757** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
3758** UPDATE operation. Otherwise (if the flag is clear) then this opcode
3759** is part of an INSERT operation. The difference is only important to
3760** the update hook.
3761**
drh66a51672008-01-03 00:01:23 +00003762** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003763** may be NULL. If it is not NULL, then the update-hook
3764** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3765**
drh93aed5a2008-01-16 17:46:38 +00003766** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3767** allocated, then ownership of P2 is transferred to the pseudo-cursor
3768** and register P2 becomes ephemeral. If the cursor is changed, the
3769** value of register P2 will then change. Make sure this does not
3770** cause any problems.)
3771**
drhf0863fe2005-06-12 21:35:51 +00003772** This instruction only works on tables. The equivalent instruction
3773** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003774*/
drhe05c9292009-10-29 13:48:10 +00003775/* Opcode: InsertInt P1 P2 P3 P4 P5
3776**
3777** This works exactly like OP_Insert except that the key is the
3778** integer value P3, not the value of the integer stored in register P3.
3779*/
3780case OP_Insert:
3781case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00003782 Mem *pData; /* MEM cell holding data for the record to be inserted */
3783 Mem *pKey; /* MEM cell holding key for the record */
3784 i64 iKey; /* The integer ROWID or key for the record to be inserted */
3785 VdbeCursor *pC; /* Cursor to table into which insert is written */
3786 int nZero; /* Number of zero-bytes to append */
3787 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
3788 const char *zDb; /* database name - used by the update hook */
3789 const char *zTbl; /* Table name - used by the opdate hook */
3790 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00003791
drha6c2ed92009-11-14 23:22:23 +00003792 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00003793 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3794 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00003795 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003796 assert( pC->pCursor!=0 );
3797 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00003798 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003799 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00003800
drhe05c9292009-10-29 13:48:10 +00003801 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00003802 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00003803 assert( pKey->flags & MEM_Int );
3804 REGISTER_TRACE(pOp->p3, pKey);
3805 iKey = pKey->u.i;
3806 }else{
3807 assert( pOp->opcode==OP_InsertInt );
3808 iKey = pOp->p3;
3809 }
3810
drha05a7222008-01-19 03:35:58 +00003811 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drhe05c9292009-10-29 13:48:10 +00003812 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00003813 if( pData->flags & MEM_Null ){
3814 pData->z = 0;
3815 pData->n = 0;
3816 }else{
3817 assert( pData->flags & (MEM_Blob|MEM_Str) );
3818 }
drh3e9ca092009-09-08 01:14:48 +00003819 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
3820 if( pData->flags & MEM_Zero ){
3821 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003822 }else{
drh3e9ca092009-09-08 01:14:48 +00003823 nZero = 0;
drha05a7222008-01-19 03:35:58 +00003824 }
drh3e9ca092009-09-08 01:14:48 +00003825 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
3826 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3827 pData->z, pData->n, nZero,
3828 pOp->p5 & OPFLAG_APPEND, seekResult
3829 );
drha05a7222008-01-19 03:35:58 +00003830 pC->rowidIsValid = 0;
3831 pC->deferredMoveto = 0;
3832 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003833
drha05a7222008-01-19 03:35:58 +00003834 /* Invoke the update-hook if required. */
3835 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00003836 zDb = db->aDb[pC->iDb].zName;
3837 zTbl = pOp->p4.z;
3838 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00003839 assert( pC->isTable );
3840 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3841 assert( pC->iDb>=0 );
3842 }
drh5e00f6c2001-09-13 13:46:56 +00003843 break;
3844}
3845
drh98757152008-01-09 23:04:12 +00003846/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00003847**
drh5edc3122001-09-13 21:53:09 +00003848** Delete the record at which the P1 cursor is currently pointing.
3849**
3850** The cursor will be left pointing at either the next or the previous
3851** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003852** the next Next instruction will be a no-op. Hence it is OK to delete
3853** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003854**
rdcb0c374f2004-02-20 22:53:38 +00003855** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003856** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003857**
drh91fd4d42008-01-19 20:11:25 +00003858** P1 must not be pseudo-table. It has to be a real table with
3859** multiple rows.
3860**
3861** If P4 is not NULL, then it is the name of the table that P1 is
3862** pointing to. The update hook will be invoked, if it exists.
3863** If P4 is not NULL then the P1 cursor must have been positioned
3864** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00003865*/
drh9cbf3422008-01-17 16:22:13 +00003866case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00003867 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00003868 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00003869
drh856c1032009-06-02 15:21:42 +00003870 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00003871 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3872 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003873 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00003874 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00003875
drh91fd4d42008-01-19 20:11:25 +00003876 /* If the update-hook will be invoked, set iKey to the rowid of the
3877 ** row being deleted.
3878 */
3879 if( db->xUpdateCallback && pOp->p4.z ){
3880 assert( pC->isTable );
3881 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
3882 iKey = pC->lastRowid;
3883 }
danielk197794eb6a12005-12-15 15:22:08 +00003884
drh9a65f2c2009-06-22 19:05:40 +00003885 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
3886 ** OP_Column on the same table without any intervening operations that
3887 ** might move or invalidate the cursor. Hence cursor pC is always pointing
3888 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
3889 ** below is always a no-op and cannot fail. We will run it anyhow, though,
3890 ** to guard against future changes to the code generator.
3891 **/
3892 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00003893 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00003894 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
3895
drh7f751222009-03-17 22:33:00 +00003896 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00003897 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00003898 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003899
drh91fd4d42008-01-19 20:11:25 +00003900 /* Invoke the update-hook if required. */
3901 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
3902 const char *zDb = db->aDb[pC->iDb].zName;
3903 const char *zTbl = pOp->p4.z;
3904 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3905 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00003906 }
danielk1977b28af712004-06-21 06:50:26 +00003907 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003908 break;
3909}
drhb7f1d9a2009-09-08 02:27:58 +00003910/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00003911**
drhb7f1d9a2009-09-08 02:27:58 +00003912** The value of the change counter is copied to the database handle
3913** change counter (returned by subsequent calls to sqlite3_changes()).
3914** Then the VMs internal change counter resets to 0.
3915** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003916*/
drh9cbf3422008-01-17 16:22:13 +00003917case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00003918 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003919 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003920 break;
3921}
3922
drh98757152008-01-09 23:04:12 +00003923/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00003924**
drh98757152008-01-09 23:04:12 +00003925** Write into register P2 the complete row data for cursor P1.
3926** There is no interpretation of the data.
3927** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003928** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00003929**
drhde4fcfd2008-01-19 23:50:26 +00003930** If the P1 cursor must be pointing to a valid row (not a NULL row)
3931** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003932*/
drh98757152008-01-09 23:04:12 +00003933/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00003934**
drh98757152008-01-09 23:04:12 +00003935** Write into register P2 the complete row key for cursor P1.
3936** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00003937** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00003938** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00003939**
drhde4fcfd2008-01-19 23:50:26 +00003940** If the P1 cursor must be pointing to a valid row (not a NULL row)
3941** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00003942*/
danielk1977a7a8e142008-02-13 18:25:27 +00003943case OP_RowKey:
3944case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00003945 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00003946 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00003947 u32 n;
drh856c1032009-06-02 15:21:42 +00003948 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00003949
drha6c2ed92009-11-14 23:22:23 +00003950 pOut = &aMem[pOp->p2];
danielk1977a7a8e142008-02-13 18:25:27 +00003951
drhf0863fe2005-06-12 21:35:51 +00003952 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00003953 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3954 pC = p->apCsr[pOp->p1];
drhf0863fe2005-06-12 21:35:51 +00003955 assert( pC->isTable || pOp->opcode==OP_RowKey );
3956 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003957 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00003958 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00003959 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00003960 assert( pC->pCursor!=0 );
3961 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00003962 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00003963
3964 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
3965 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
3966 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
3967 ** a no-op and can never fail. But we leave it in place as a safety.
3968 */
3969 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00003970 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00003971 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
3972
drhde4fcfd2008-01-19 23:50:26 +00003973 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00003974 assert( !pC->isTable );
drhc27ae612009-07-14 18:35:44 +00003975 rc = sqlite3BtreeKeySize(pCrsr, &n64);
3976 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00003977 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00003978 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00003979 }
drhbfb19dc2009-06-05 16:46:53 +00003980 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00003981 }else{
drhc27ae612009-07-14 18:35:44 +00003982 rc = sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00003983 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00003984 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00003985 goto too_big;
3986 }
drhde4fcfd2008-01-19 23:50:26 +00003987 }
danielk1977a7a8e142008-02-13 18:25:27 +00003988 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
3989 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00003990 }
danielk1977a7a8e142008-02-13 18:25:27 +00003991 pOut->n = n;
3992 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00003993 if( pC->isIndex ){
3994 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
3995 }else{
3996 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00003997 }
danielk197796cb76f2008-01-04 13:24:28 +00003998 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00003999 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00004000 break;
4001}
4002
drh2133d822008-01-03 18:44:59 +00004003/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004004**
drh2133d822008-01-03 18:44:59 +00004005** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004006** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004007**
4008** P1 can be either an ordinary table or a virtual table. There used to
4009** be a separate OP_VRowid opcode for use with virtual tables, but this
4010** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004011*/
drh4c583122008-01-04 22:01:03 +00004012case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004013 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004014 i64 v;
drh856c1032009-06-02 15:21:42 +00004015 sqlite3_vtab *pVtab;
4016 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004017
drh653b82a2009-06-22 11:10:47 +00004018 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4019 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004020 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004021 assert( pC->pseudoTableReg==0 );
drh044925b2009-04-22 17:15:02 +00004022 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004023 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004024 break;
4025 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004026 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004027#ifndef SQLITE_OMIT_VIRTUALTABLE
4028 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004029 pVtab = pC->pVtabCursor->pVtab;
4030 pModule = pVtab->pModule;
4031 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004032 rc = pModule->xRowid(pC->pVtabCursor, &v);
4033 sqlite3DbFree(db, p->zErrMsg);
4034 p->zErrMsg = pVtab->zErrMsg;
4035 pVtab->zErrMsg = 0;
drh044925b2009-04-22 17:15:02 +00004036#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004037 }else{
drh6be240e2009-07-14 02:33:02 +00004038 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004039 rc = sqlite3VdbeCursorMoveto(pC);
4040 if( rc ) goto abort_due_to_error;
4041 if( pC->rowidIsValid ){
4042 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004043 }else{
drhc27ae612009-07-14 18:35:44 +00004044 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4045 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004046 }
drh5e00f6c2001-09-13 13:46:56 +00004047 }
drh4c583122008-01-04 22:01:03 +00004048 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004049 break;
4050}
4051
drh9cbf3422008-01-17 16:22:13 +00004052/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004053**
4054** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004055** that occur while the cursor is on the null row will always
4056** write a NULL.
drh17f71932002-02-21 12:01:27 +00004057*/
drh9cbf3422008-01-17 16:22:13 +00004058case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004059 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004060
drh653b82a2009-06-22 11:10:47 +00004061 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4062 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004063 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004064 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004065 pC->rowidIsValid = 0;
danielk1977be51a652008-10-08 17:58:48 +00004066 if( pC->pCursor ){
4067 sqlite3BtreeClearCursor(pC->pCursor);
4068 }
drh17f71932002-02-21 12:01:27 +00004069 break;
4070}
4071
drh9cbf3422008-01-17 16:22:13 +00004072/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004073**
drhf0863fe2005-06-12 21:35:51 +00004074** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004075** will refer to the last entry in the database table or index.
4076** If the table or index is empty and P2>0, then jump immediately to P2.
4077** If P2 is 0 or if the table or index is not empty, fall through
4078** to the following instruction.
4079*/
drh9cbf3422008-01-17 16:22:13 +00004080case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004081 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004082 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004083 int res;
drh9562b552002-02-19 15:00:07 +00004084
drh653b82a2009-06-22 11:10:47 +00004085 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4086 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004087 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004088 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004089 if( pCrsr==0 ){
4090 res = 1;
4091 }else{
4092 rc = sqlite3BtreeLast(pCrsr, &res);
4093 }
drh9c1905f2008-12-10 22:32:56 +00004094 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004095 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004096 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004097 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004098 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004099 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004100 }
4101 break;
4102}
4103
drh0342b1f2005-09-01 03:07:44 +00004104
drh9cbf3422008-01-17 16:22:13 +00004105/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004106**
4107** This opcode does exactly the same thing as OP_Rewind except that
4108** it increments an undocumented global variable used for testing.
4109**
4110** Sorting is accomplished by writing records into a sorting index,
4111** then rewinding that index and playing it back from beginning to
4112** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4113** rewinding so that the global variable will be incremented and
4114** regression tests can determine whether or not the optimizer is
4115** correctly optimizing out sorts.
4116*/
drh9cbf3422008-01-17 16:22:13 +00004117case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004118#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004119 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004120 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004121#endif
drhd1d38482008-10-07 23:46:38 +00004122 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00004123 /* Fall through into OP_Rewind */
4124}
drh9cbf3422008-01-17 16:22:13 +00004125/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004126**
drhf0863fe2005-06-12 21:35:51 +00004127** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004128** will refer to the first entry in the database table or index.
4129** If the table or index is empty and P2>0, then jump immediately to P2.
4130** If P2 is 0 or if the table or index is not empty, fall through
4131** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004132*/
drh9cbf3422008-01-17 16:22:13 +00004133case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004134 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004135 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004136 int res;
drh5e00f6c2001-09-13 13:46:56 +00004137
drh653b82a2009-06-22 11:10:47 +00004138 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4139 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004140 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00004141 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00004142 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004143 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004144 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004145 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004146 pC->rowidIsValid = 0;
drh70ce3f02003-04-15 19:22:22 +00004147 }else{
drhf4dada72004-05-11 09:57:35 +00004148 res = 1;
4149 }
drh9c1905f2008-12-10 22:32:56 +00004150 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004151 assert( pOp->p2>0 && pOp->p2<p->nOp );
4152 if( res ){
drhf4dada72004-05-11 09:57:35 +00004153 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004154 }
4155 break;
4156}
4157
drhafc266a2010-03-31 17:47:44 +00004158/* Opcode: Next P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00004159**
4160** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004161** table or index. If there are no more key/value pairs then fall through
4162** to the following instruction. But if the cursor advance was successful,
4163** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004164**
drh60a713c2008-01-21 16:22:45 +00004165** The P1 cursor must be for a real table, not a pseudo-table.
4166**
drhafc266a2010-03-31 17:47:44 +00004167** If P5 is positive and the jump is taken, then event counter
4168** number P5-1 in the prepared statement is incremented.
4169**
drhc045ec52002-12-04 20:01:06 +00004170** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004171*/
drhafc266a2010-03-31 17:47:44 +00004172/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004173**
4174** Back up cursor P1 so that it points to the previous key/data pair in its
4175** table or index. If there is no previous key/value pairs then fall through
4176** to the following instruction. But if the cursor backup was successful,
4177** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004178**
4179** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004180**
4181** If P5 is positive and the jump is taken, then event counter
4182** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004183*/
drh9cbf3422008-01-17 16:22:13 +00004184case OP_Prev: /* jump */
4185case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004186 VdbeCursor *pC;
drh8721ce42001-11-07 14:22:00 +00004187 BtCursor *pCrsr;
drha3460582008-07-11 21:02:53 +00004188 int res;
drh8721ce42001-11-07 14:22:00 +00004189
drhcaec2f12003-01-07 02:47:47 +00004190 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004191 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhafc266a2010-03-31 17:47:44 +00004192 assert( pOp->p5<=ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004193 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004194 if( pC==0 ){
4195 break; /* See ticket #2273 */
4196 }
drh60a713c2008-01-21 16:22:45 +00004197 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004198 if( pCrsr==0 ){
4199 pC->nullRow = 1;
4200 break;
4201 }
drha3460582008-07-11 21:02:53 +00004202 res = 1;
4203 assert( pC->deferredMoveto==0 );
4204 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
4205 sqlite3BtreePrevious(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004206 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004207 pC->cacheStatus = CACHE_STALE;
4208 if( res==0 ){
4209 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004210 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004211#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004212 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004213#endif
drh8721ce42001-11-07 14:22:00 +00004214 }
drhf0863fe2005-06-12 21:35:51 +00004215 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004216 break;
4217}
4218
danielk1977de630352009-05-04 11:42:29 +00004219/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004220**
drhaa9b8962008-01-08 02:57:55 +00004221** Register P2 holds a SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004222** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004223** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004224**
drhaa9b8962008-01-08 02:57:55 +00004225** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004226** insert is likely to be an append.
4227**
drhf0863fe2005-06-12 21:35:51 +00004228** This instruction only works for indices. The equivalent instruction
4229** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004230*/
drh9cbf3422008-01-17 16:22:13 +00004231case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004232 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004233 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004234 int nKey;
4235 const char *zKey;
4236
drh653b82a2009-06-22 11:10:47 +00004237 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4238 pC = p->apCsr[pOp->p1];
4239 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00004240 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004241 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004242 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004243 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004244 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004245 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004246 if( rc==SQLITE_OK ){
drh856c1032009-06-02 15:21:42 +00004247 nKey = pIn2->n;
4248 zKey = pIn2->z;
danielk1977de630352009-05-04 11:42:29 +00004249 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4250 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4251 );
danielk1977d908f5a2007-05-11 07:08:28 +00004252 assert( pC->deferredMoveto==0 );
4253 pC->cacheStatus = CACHE_STALE;
4254 }
drh5e00f6c2001-09-13 13:46:56 +00004255 }
drh5e00f6c2001-09-13 13:46:56 +00004256 break;
4257}
4258
drhd1d38482008-10-07 23:46:38 +00004259/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004260**
drhe14006d2008-03-25 17:23:32 +00004261** The content of P3 registers starting at register P2 form
4262** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004263** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004264*/
drhe14006d2008-03-25 17:23:32 +00004265case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004266 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004267 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004268 int res;
4269 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004270
drhe14006d2008-03-25 17:23:32 +00004271 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004272 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004273 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4274 pC = p->apCsr[pOp->p1];
4275 assert( pC!=0 );
4276 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004277 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004278 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004279 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004280 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004281 r.aMem = &aMem[pOp->p2];
drhe63d9992008-08-13 19:11:48 +00004282 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004283 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004284 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004285 }
drh9188b382004-05-14 21:12:22 +00004286 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004287 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004288 }
drh5e00f6c2001-09-13 13:46:56 +00004289 break;
4290}
4291
drh2133d822008-01-03 18:44:59 +00004292/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004293**
drh2133d822008-01-03 18:44:59 +00004294** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004295** the end of the index key pointed to by cursor P1. This integer should be
4296** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004297**
drh9437bd22009-02-01 00:29:56 +00004298** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004299*/
drh4c583122008-01-04 22:01:03 +00004300case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004301 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004302 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004303 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004304
drh653b82a2009-06-22 11:10:47 +00004305 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4306 pC = p->apCsr[pOp->p1];
4307 assert( pC!=0 );
4308 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004309 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004310 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004311 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004312 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004313 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004314 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004315 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004316 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004317 if( rc!=SQLITE_OK ){
4318 goto abort_due_to_error;
4319 }
drh4c583122008-01-04 22:01:03 +00004320 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004321 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004322 }
drh8721ce42001-11-07 14:22:00 +00004323 }
4324 break;
4325}
4326
danielk197761dd5832008-04-18 11:31:12 +00004327/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004328**
danielk197761dd5832008-04-18 11:31:12 +00004329** The P4 register values beginning with P3 form an unpacked index
4330** key that omits the ROWID. Compare this key value against the index
4331** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004332**
danielk197761dd5832008-04-18 11:31:12 +00004333** If the P1 index entry is greater than or equal to the key value
4334** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004335**
danielk197761dd5832008-04-18 11:31:12 +00004336** If P5 is non-zero then the key value is increased by an epsilon
4337** prior to the comparison. This make the opcode work like IdxGT except
4338** that if the key from register P3 is a prefix of the key in the cursor,
4339** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004340*/
drh98757152008-01-09 23:04:12 +00004341/* Opcode: IdxLT P1 P2 P3 * P5
drhc045ec52002-12-04 20:01:06 +00004342**
danielk197761dd5832008-04-18 11:31:12 +00004343** The P4 register values beginning with P3 form an unpacked index
4344** key that omits the ROWID. Compare this key value against the index
4345** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004346**
danielk197761dd5832008-04-18 11:31:12 +00004347** If the P1 index entry is less than the key value then jump to P2.
4348** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004349**
danielk197761dd5832008-04-18 11:31:12 +00004350** If P5 is non-zero then the key value is increased by an epsilon prior
4351** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004352*/
drh93952eb2009-11-13 19:43:43 +00004353case OP_IdxLT: /* jump */
4354case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004355 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004356 int res;
4357 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004358
drh653b82a2009-06-22 11:10:47 +00004359 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4360 pC = p->apCsr[pOp->p1];
4361 assert( pC!=0 );
drh9a65f2c2009-06-22 19:05:40 +00004362 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004363 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004364 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004365 assert( pOp->p4type==P4_INT32 );
4366 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004367 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004368 if( pOp->p5 ){
4369 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4370 }else{
4371 r.flags = UNPACKED_IGNORE_ROWID;
4372 }
drha6c2ed92009-11-14 23:22:23 +00004373 r.aMem = &aMem[pOp->p3];
drhe63d9992008-08-13 19:11:48 +00004374 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004375 if( pOp->opcode==OP_IdxLT ){
4376 res = -res;
drha05a7222008-01-19 03:35:58 +00004377 }else{
4378 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004379 res++;
4380 }
4381 if( res>0 ){
4382 pc = pOp->p2 - 1 ;
4383 }
4384 }
4385 break;
4386}
4387
drh98757152008-01-09 23:04:12 +00004388/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004389**
4390** Delete an entire database table or index whose root page in the database
4391** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004392**
drh98757152008-01-09 23:04:12 +00004393** The table being destroyed is in the main database file if P3==0. If
4394** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004395** that is used to store tables create using CREATE TEMPORARY TABLE.
4396**
drh205f48e2004-11-05 00:43:11 +00004397** If AUTOVACUUM is enabled then it is possible that another root page
4398** might be moved into the newly deleted root page in order to keep all
4399** root pages contiguous at the beginning of the database. The former
4400** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004401** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004402** movement was required (because the table being dropped was already
4403** the last one in the database) then a zero is stored in register P2.
4404** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004405**
drhb19a2bc2001-09-16 00:13:26 +00004406** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004407*/
drh98757152008-01-09 23:04:12 +00004408case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004409 int iMoved;
drh3765df42006-06-28 18:18:09 +00004410 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004411 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004412 int iDb;
4413#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004414 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004415 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danielk1977212b2182006-06-23 14:32:08 +00004416 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4417 iCnt++;
4418 }
4419 }
drh3765df42006-06-28 18:18:09 +00004420#else
4421 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004422#endif
drh3c657212009-11-17 23:59:58 +00004423 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004424 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004425 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004426 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004427 }else{
drh856c1032009-06-02 15:21:42 +00004428 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004429 assert( iCnt==1 );
drh98757152008-01-09 23:04:12 +00004430 assert( (p->btreeMask & (1<<iDb))!=0 );
4431 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004432 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004433 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004434#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004435 if( rc==SQLITE_OK && iMoved!=0 ){
drh98757152008-01-09 23:04:12 +00004436 sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
drh32783152009-11-20 15:02:34 +00004437 resetSchemaOnFault = 1;
danielk1977e6efa742004-11-10 11:55:10 +00004438 }
drh3765df42006-06-28 18:18:09 +00004439#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004440 }
drh5e00f6c2001-09-13 13:46:56 +00004441 break;
4442}
4443
danielk1977c7af4842008-10-27 13:59:33 +00004444/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004445**
4446** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004447** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004448** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004449**
drhf57b3392001-10-08 13:22:32 +00004450** The table being clear is in the main database file if P2==0. If
4451** P2==1 then the table to be clear is in the auxiliary database file
4452** that is used to store tables create using CREATE TEMPORARY TABLE.
4453**
shanebe217792009-03-05 04:20:31 +00004454** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004455** intkey table (an SQL table, not an index). In this case the row change
4456** count is incremented by the number of rows in the table being cleared.
4457** If P3 is greater than zero, then the value stored in register P3 is
4458** also incremented by the number of rows in the table being cleared.
4459**
drhb19a2bc2001-09-16 00:13:26 +00004460** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004461*/
drh9cbf3422008-01-17 16:22:13 +00004462case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004463 int nChange;
4464
4465 nChange = 0;
drhfb982642007-08-30 01:19:59 +00004466 assert( (p->btreeMask & (1<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004467 rc = sqlite3BtreeClearTable(
4468 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4469 );
4470 if( pOp->p3 ){
4471 p->nChange += nChange;
4472 if( pOp->p3>0 ){
drha6c2ed92009-11-14 23:22:23 +00004473 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004474 }
4475 }
drh5edc3122001-09-13 21:53:09 +00004476 break;
4477}
4478
drh4c583122008-01-04 22:01:03 +00004479/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004480**
drh4c583122008-01-04 22:01:03 +00004481** Allocate a new table in the main database file if P1==0 or in the
4482** auxiliary database file if P1==1 or in an attached database if
4483** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004484** register P2
drh5b2fd562001-09-13 15:21:31 +00004485**
drhc6b52df2002-01-04 03:09:29 +00004486** The difference between a table and an index is this: A table must
4487** have a 4-byte integer key and can have arbitrary data. An index
4488** has an arbitrary key but no data.
4489**
drhb19a2bc2001-09-16 00:13:26 +00004490** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004491*/
drh4c583122008-01-04 22:01:03 +00004492/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004493**
drh4c583122008-01-04 22:01:03 +00004494** Allocate a new index in the main database file if P1==0 or in the
4495** auxiliary database file if P1==1 or in an attached database if
4496** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004497** register P2.
drhf57b3392001-10-08 13:22:32 +00004498**
drhc6b52df2002-01-04 03:09:29 +00004499** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004500*/
drh4c583122008-01-04 22:01:03 +00004501case OP_CreateIndex: /* out2-prerelease */
4502case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004503 int pgno;
drhf328bc82004-05-10 23:29:49 +00004504 int flags;
drh234c39d2004-07-24 03:30:47 +00004505 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004506
4507 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004508 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00004509 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004510 pDb = &db->aDb[pOp->p1];
4511 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004512 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004513 /* flags = BTREE_INTKEY; */
4514 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004515 }else{
drhf328bc82004-05-10 23:29:49 +00004516 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00004517 }
drh234c39d2004-07-24 03:30:47 +00004518 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004519 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004520 break;
4521}
4522
drh98757152008-01-09 23:04:12 +00004523/* Opcode: ParseSchema P1 P2 * P4 *
drh234c39d2004-07-24 03:30:47 +00004524**
4525** Read and parse all entries from the SQLITE_MASTER table of database P1
drh66a51672008-01-03 00:01:23 +00004526** that match the WHERE clause P4. P2 is the "force" flag. Always do
drh3c23a882007-01-09 14:01:13 +00004527** the parsing if P2 is true. If P2 is false, then this routine is a
4528** no-op if the schema is not currently loaded. In other words, if P2
4529** is false, the SQLITE_MASTER table is only parsed if the rest of the
4530** schema is already loaded into the symbol table.
drh234c39d2004-07-24 03:30:47 +00004531**
4532** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004533** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004534*/
drh9cbf3422008-01-17 16:22:13 +00004535case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004536 int iDb;
4537 const char *zMaster;
4538 char *zSql;
4539 InitData initData;
4540
4541 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004542 assert( iDb>=0 && iDb<db->nDb );
danielk1977a8bbef82009-03-23 17:11:26 +00004543
4544 /* If pOp->p2 is 0, then this opcode is being executed to read a
4545 ** single row, for example the row corresponding to a new index
4546 ** created by this VDBE, from the sqlite_master table. It only
4547 ** does this if the corresponding in-memory schema is currently
4548 ** loaded. Otherwise, the new index definition can be loaded along
4549 ** with the rest of the schema when it is required.
4550 **
4551 ** Although the mutex on the BtShared object that corresponds to
4552 ** database iDb (the database containing the sqlite_master table
4553 ** read by this instruction) is currently held, it is necessary to
4554 ** obtain the mutexes on all attached databases before checking if
4555 ** the schema of iDb is loaded. This is because, at the start of
4556 ** the sqlite3_exec() call below, SQLite will invoke
4557 ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
4558 ** iDb mutex may be temporarily released to avoid deadlock. If
4559 ** this happens, then some other thread may delete the in-memory
4560 ** schema of database iDb before the SQL statement runs. The schema
4561 ** will not be reloaded becuase the db->init.busy flag is set. This
4562 ** can result in a "no such table: sqlite_master" or "malformed
4563 ** database schema" error being returned to the user.
4564 */
4565 assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4566 sqlite3BtreeEnterAll(db);
drh46bbabd2009-06-24 13:16:03 +00004567 if( pOp->p2 || DbHasProperty(db, iDb, DB_SchemaLoaded) ){
drh856c1032009-06-02 15:21:42 +00004568 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004569 initData.db = db;
4570 initData.iDb = pOp->p1;
4571 initData.pzErrMsg = &p->zErrMsg;
4572 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004573 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004574 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4575 if( zSql==0 ){
4576 rc = SQLITE_NOMEM;
4577 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004578 assert( db->init.busy==0 );
4579 db->init.busy = 1;
4580 initData.rc = SQLITE_OK;
4581 assert( !db->mallocFailed );
4582 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4583 if( rc==SQLITE_OK ) rc = initData.rc;
4584 sqlite3DbFree(db, zSql);
4585 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004586 }
drh3c23a882007-01-09 14:01:13 +00004587 }
danielk1977a8bbef82009-03-23 17:11:26 +00004588 sqlite3BtreeLeaveAll(db);
danielk1977261919c2005-12-06 12:52:59 +00004589 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004590 goto no_mem;
4591 }
drh234c39d2004-07-24 03:30:47 +00004592 break;
4593}
4594
drh8bfdf722009-06-19 14:06:03 +00004595#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004596/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004597**
4598** Read the sqlite_stat1 table for database P1 and load the content
4599** of that table into the internal index hash table. This will cause
4600** the analysis to be used when preparing all subsequent queries.
4601*/
drh9cbf3422008-01-17 16:22:13 +00004602case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004603 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4604 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004605 break;
4606}
drh8bfdf722009-06-19 14:06:03 +00004607#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004608
drh98757152008-01-09 23:04:12 +00004609/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004610**
4611** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004612** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004613** is dropped in order to keep the internal representation of the
4614** schema consistent with what is on disk.
4615*/
drh9cbf3422008-01-17 16:22:13 +00004616case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004617 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004618 break;
4619}
4620
drh98757152008-01-09 23:04:12 +00004621/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004622**
4623** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004624** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004625** is dropped in order to keep the internal representation of the
4626** schema consistent with what is on disk.
4627*/
drh9cbf3422008-01-17 16:22:13 +00004628case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004629 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004630 break;
4631}
4632
drh98757152008-01-09 23:04:12 +00004633/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004634**
4635** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004636** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004637** is dropped in order to keep the internal representation of the
4638** schema consistent with what is on disk.
4639*/
drh9cbf3422008-01-17 16:22:13 +00004640case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004641 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004642 break;
4643}
4644
drh234c39d2004-07-24 03:30:47 +00004645
drhb7f91642004-10-31 02:22:47 +00004646#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004647/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004648**
drh98757152008-01-09 23:04:12 +00004649** Do an analysis of the currently open database. Store in
4650** register P1 the text of an error message describing any problems.
4651** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004652**
drh98757152008-01-09 23:04:12 +00004653** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004654** At most reg(P3) errors will be reported.
4655** In other words, the analysis stops as soon as reg(P1) errors are
4656** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004657**
drh79069752004-05-22 21:30:40 +00004658** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004659** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004660** total.
drh21504322002-06-25 13:16:02 +00004661**
drh98757152008-01-09 23:04:12 +00004662** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004663** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004664**
drh1dcdbc02007-01-27 02:24:54 +00004665** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004666*/
drhaaab5722002-02-19 13:39:21 +00004667case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004668 int nRoot; /* Number of tables to check. (Number of root pages.) */
4669 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4670 int j; /* Loop counter */
4671 int nErr; /* Number of errors reported */
4672 char *z; /* Text of the error report */
4673 Mem *pnErr; /* Register keeping track of errors remaining */
4674
4675 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004676 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004677 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004678 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004679 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00004680 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004681 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004682 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00004683 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004684 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004685 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004686 }
4687 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004688 assert( pOp->p5<db->nDb );
4689 assert( (p->btreeMask & (1<<pOp->p5))!=0 );
4690 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004691 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004692 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004693 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004694 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004695 if( nErr==0 ){
4696 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004697 }else if( z==0 ){
4698 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004699 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004700 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004701 }
drhb7654112008-01-12 12:48:07 +00004702 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004703 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004704 break;
4705}
drhb7f91642004-10-31 02:22:47 +00004706#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004707
drh3d4501e2008-12-04 20:40:10 +00004708/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004709**
drh3d4501e2008-12-04 20:40:10 +00004710** Insert the integer value held by register P2 into a boolean index
4711** held in register P1.
4712**
4713** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004714*/
drh93952eb2009-11-13 19:43:43 +00004715case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00004716 pIn1 = &aMem[pOp->p1];
4717 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00004718 assert( (pIn2->flags & MEM_Int)!=0 );
4719 if( (pIn1->flags & MEM_RowSet)==0 ){
4720 sqlite3VdbeMemSetRowSet(pIn1);
4721 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004722 }
drh93952eb2009-11-13 19:43:43 +00004723 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00004724 break;
4725}
4726
4727/* Opcode: RowSetRead P1 P2 P3 * *
4728**
4729** Extract the smallest value from boolean index P1 and put that value into
4730** register P3. Or, if boolean index P1 is initially empty, leave P3
4731** unchanged and jump to instruction P2.
4732*/
drh93952eb2009-11-13 19:43:43 +00004733case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00004734 i64 val;
drh3d4501e2008-12-04 20:40:10 +00004735 CHECK_FOR_INTERRUPT;
drh3c657212009-11-17 23:59:58 +00004736 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00004737 if( (pIn1->flags & MEM_RowSet)==0
4738 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00004739 ){
4740 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00004741 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00004742 pc = pOp->p2 - 1;
4743 }else{
4744 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00004745 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00004746 }
drh5e00f6c2001-09-13 13:46:56 +00004747 break;
4748}
4749
drh1b26c7c2009-04-22 02:15:47 +00004750/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00004751**
drhade97602009-04-21 15:05:18 +00004752** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00004753** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00004754** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00004755** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00004756** next opcode.
danielk19771d461462009-04-21 09:02:45 +00004757**
drh1b26c7c2009-04-22 02:15:47 +00004758** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00004759** of integers, where each set contains no duplicates. Each set
4760** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00004761** must have P4==0, the final set P4=-1. P4 must be either -1 or
4762** non-negative. For non-negative values of P4 only the lower 4
4763** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00004764**
4765** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00004766** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00004767** (b) when P4==-1 there is no need to insert the value, as it will
4768** never be tested for, and (c) when a value that is part of set X is
4769** inserted, there is no need to search to see if the same value was
4770** previously inserted as part of set X (only if it was previously
4771** inserted as part of some other set).
4772*/
drh1b26c7c2009-04-22 02:15:47 +00004773case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00004774 int iSet;
4775 int exists;
4776
drh3c657212009-11-17 23:59:58 +00004777 pIn1 = &aMem[pOp->p1];
4778 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00004779 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00004780 assert( pIn3->flags&MEM_Int );
4781
drh1b26c7c2009-04-22 02:15:47 +00004782 /* If there is anything other than a rowset object in memory cell P1,
4783 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00004784 */
drh733bf1b2009-04-22 00:47:00 +00004785 if( (pIn1->flags & MEM_RowSet)==0 ){
4786 sqlite3VdbeMemSetRowSet(pIn1);
4787 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00004788 }
4789
4790 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00004791 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00004792 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00004793 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
4794 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00004795 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004796 if( exists ){
4797 pc = pOp->p2 - 1;
4798 break;
4799 }
4800 }
4801 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00004802 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00004803 }
4804 break;
4805}
4806
drh5e00f6c2001-09-13 13:46:56 +00004807
danielk197793758c82005-01-21 08:13:14 +00004808#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00004809
4810/* Opcode: Program P1 P2 P3 P4 *
4811**
dan76d462e2009-08-30 11:42:51 +00004812** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00004813**
dan76d462e2009-08-30 11:42:51 +00004814** P1 contains the address of the memory cell that contains the first memory
4815** cell in an array of values used as arguments to the sub-program. P2
4816** contains the address to jump to if the sub-program throws an IGNORE
4817** exception using the RAISE() function. Register P3 contains the address
4818** of a memory cell in this (the parent) VM that is used to allocate the
4819** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00004820**
4821** P4 is a pointer to the VM containing the trigger program.
4822*/
dan76d462e2009-08-30 11:42:51 +00004823case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00004824 int nMem; /* Number of memory registers for sub-program */
4825 int nByte; /* Bytes of runtime space required for sub-program */
4826 Mem *pRt; /* Register to allocate runtime space */
4827 Mem *pMem; /* Used to iterate through memory cells */
4828 Mem *pEnd; /* Last memory cell in new array */
4829 VdbeFrame *pFrame; /* New vdbe frame to execute in */
4830 SubProgram *pProgram; /* Sub-program to execute */
4831 void *t; /* Token identifying trigger */
4832
4833 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00004834 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00004835 assert( pProgram->nOp>0 );
4836
dan1da40a32009-09-19 17:00:31 +00004837 /* If the p5 flag is clear, then recursive invocation of triggers is
4838 ** disabled for backwards compatibility (p5 is set if this sub-program
4839 ** is really a trigger, not a foreign key action, and the flag set
4840 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00004841 **
4842 ** It is recursive invocation of triggers, at the SQL level, that is
4843 ** disabled. In some cases a single trigger may generate more than one
4844 ** SubProgram (if the trigger may be executed with more than one different
4845 ** ON CONFLICT algorithm). SubProgram structures associated with a
4846 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00004847 ** variable. */
4848 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00004849 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00004850 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
4851 if( pFrame ) break;
4852 }
4853
danf5894502009-10-07 18:41:19 +00004854 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00004855 rc = SQLITE_ERROR;
4856 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
4857 break;
4858 }
4859
4860 /* Register pRt is used to store the memory required to save the state
4861 ** of the current program, and the memory required at runtime to execute
4862 ** the trigger program. If this trigger has been fired before, then pRt
4863 ** is already allocated. Otherwise, it must be initialized. */
4864 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00004865 /* SubProgram.nMem is set to the number of memory cells used by the
4866 ** program stored in SubProgram.aOp. As well as these, one memory
4867 ** cell is required for each cursor used by the program. Set local
4868 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
4869 */
dan65a7cd12009-09-01 12:16:01 +00004870 nMem = pProgram->nMem + pProgram->nCsr;
4871 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00004872 + nMem * sizeof(Mem)
4873 + pProgram->nCsr * sizeof(VdbeCursor *);
4874 pFrame = sqlite3DbMallocZero(db, nByte);
4875 if( !pFrame ){
4876 goto no_mem;
4877 }
4878 sqlite3VdbeMemRelease(pRt);
4879 pRt->flags = MEM_Frame;
4880 pRt->u.pFrame = pFrame;
4881
4882 pFrame->v = p;
4883 pFrame->nChildMem = nMem;
4884 pFrame->nChildCsr = pProgram->nCsr;
4885 pFrame->pc = pc;
4886 pFrame->aMem = p->aMem;
4887 pFrame->nMem = p->nMem;
4888 pFrame->apCsr = p->apCsr;
4889 pFrame->nCursor = p->nCursor;
4890 pFrame->aOp = p->aOp;
4891 pFrame->nOp = p->nOp;
4892 pFrame->token = pProgram->token;
4893
4894 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
4895 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
4896 pMem->flags = MEM_Null;
4897 pMem->db = db;
4898 }
4899 }else{
4900 pFrame = pRt->u.pFrame;
4901 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
4902 assert( pProgram->nCsr==pFrame->nChildCsr );
4903 assert( pc==pFrame->pc );
4904 }
4905
4906 p->nFrame++;
4907 pFrame->pParent = p->pFrame;
dan76d462e2009-08-30 11:42:51 +00004908 pFrame->lastRowid = db->lastRowid;
4909 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00004910 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00004911 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00004912 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00004913 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00004914 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00004915 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00004916 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00004917 p->nOp = pProgram->nOp;
4918 pc = -1;
4919
4920 break;
4921}
4922
dan76d462e2009-08-30 11:42:51 +00004923/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00004924**
dan76d462e2009-08-30 11:42:51 +00004925** This opcode is only ever present in sub-programs called via the
4926** OP_Program instruction. Copy a value currently stored in a memory
4927** cell of the calling (parent) frame to cell P2 in the current frames
4928** address space. This is used by trigger programs to access the new.*
4929** and old.* values.
dan165921a2009-08-28 18:53:45 +00004930**
dan76d462e2009-08-30 11:42:51 +00004931** The address of the cell in the parent frame is determined by adding
4932** the value of the P1 argument to the value of the P1 argument to the
4933** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00004934*/
dan76d462e2009-08-30 11:42:51 +00004935case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00004936 VdbeFrame *pFrame;
4937 Mem *pIn;
4938 pFrame = p->pFrame;
4939 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00004940 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
4941 break;
4942}
4943
danielk197793758c82005-01-21 08:13:14 +00004944#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004945
dan1da40a32009-09-19 17:00:31 +00004946#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00004947/* Opcode: FkCounter P1 P2 * * *
dan1da40a32009-09-19 17:00:31 +00004948**
dan0ff297e2009-09-25 17:03:14 +00004949** Increment a "constraint counter" by P2 (P2 may be negative or positive).
4950** If P1 is non-zero, the database constraint counter is incremented
4951** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00004952** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00004953*/
dan32b09f22009-09-23 17:29:59 +00004954case OP_FkCounter: {
dan0ff297e2009-09-25 17:03:14 +00004955 if( pOp->p1 ){
4956 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00004957 }else{
dan0ff297e2009-09-25 17:03:14 +00004958 p->nFkConstraint += pOp->p2;
4959 }
4960 break;
4961}
4962
4963/* Opcode: FkIfZero P1 P2 * * *
4964**
4965** This opcode tests if a foreign key constraint-counter is currently zero.
4966** If so, jump to instruction P2. Otherwise, fall through to the next
4967** instruction.
4968**
4969** If P1 is non-zero, then the jump is taken if the database constraint-counter
4970** is zero (the one that counts deferred constraint violations). If P1 is
4971** zero, the jump is taken if the statement constraint-counter is zero
4972** (immediate foreign key constraint violations).
4973*/
4974case OP_FkIfZero: { /* jump */
4975 if( pOp->p1 ){
4976 if( db->nDeferredCons==0 ) pc = pOp->p2-1;
4977 }else{
4978 if( p->nFkConstraint==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00004979 }
dan1da40a32009-09-19 17:00:31 +00004980 break;
4981}
4982#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
4983
drh205f48e2004-11-05 00:43:11 +00004984#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00004985/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00004986**
dan76d462e2009-08-30 11:42:51 +00004987** P1 is a register in the root frame of this VM (the root frame is
4988** different from the current frame if this instruction is being executed
4989** within a sub-program). Set the value of register P1 to the maximum of
4990** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00004991**
4992** This instruction throws an error if the memory cell is not initially
4993** an integer.
4994*/
dan76d462e2009-08-30 11:42:51 +00004995case OP_MemMax: { /* in2 */
4996 Mem *pIn1;
4997 VdbeFrame *pFrame;
4998 if( p->pFrame ){
4999 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5000 pIn1 = &pFrame->aMem[pOp->p1];
5001 }else{
drha6c2ed92009-11-14 23:22:23 +00005002 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005003 }
drh98757152008-01-09 23:04:12 +00005004 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005005 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005006 sqlite3VdbeMemIntegerify(pIn2);
5007 if( pIn1->u.i<pIn2->u.i){
5008 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005009 }
5010 break;
5011}
5012#endif /* SQLITE_OMIT_AUTOINCREMENT */
5013
drh98757152008-01-09 23:04:12 +00005014/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00005015**
drh98757152008-01-09 23:04:12 +00005016** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005017**
drh98757152008-01-09 23:04:12 +00005018** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005019** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005020*/
drh9cbf3422008-01-17 16:22:13 +00005021case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005022 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005023 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005024 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005025 pc = pOp->p2 - 1;
5026 }
5027 break;
5028}
5029
drh98757152008-01-09 23:04:12 +00005030/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00005031**
drh98757152008-01-09 23:04:12 +00005032** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005033**
drh98757152008-01-09 23:04:12 +00005034** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005035** not contain an integer. An assertion fault will result if you try.
5036*/
drh9cbf3422008-01-17 16:22:13 +00005037case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005038 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005039 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005040 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005041 pc = pOp->p2 - 1;
5042 }
5043 break;
5044}
5045
drh9b918ed2009-11-12 03:13:26 +00005046/* Opcode: IfZero P1 P2 P3 * *
drhec7429a2005-10-06 16:53:14 +00005047**
drh9b918ed2009-11-12 03:13:26 +00005048** The register P1 must contain an integer. Add literal P3 to the
5049** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005050**
drh98757152008-01-09 23:04:12 +00005051** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005052** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005053*/
drh9cbf3422008-01-17 16:22:13 +00005054case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005055 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005056 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005057 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005058 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005059 pc = pOp->p2 - 1;
5060 }
5061 break;
5062}
5063
drh98757152008-01-09 23:04:12 +00005064/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00005065**
drh0bce8352002-02-28 00:41:10 +00005066** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005067** function has P5 arguments. P4 is a pointer to the FuncDef
5068** structure that specifies the function. Use register
5069** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005070**
drh98757152008-01-09 23:04:12 +00005071** The P5 arguments are taken from register P2 and its
5072** successors.
drhe5095352002-02-24 03:25:14 +00005073*/
drh9cbf3422008-01-17 16:22:13 +00005074case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005075 int n;
drhe5095352002-02-24 03:25:14 +00005076 int i;
drhc54a6172009-06-02 16:06:03 +00005077 Mem *pMem;
5078 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005079 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005080 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005081
drh856c1032009-06-02 15:21:42 +00005082 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005083 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005084 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005085 apVal = p->apArg;
5086 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005087 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00005088 apVal[i] = pRec;
dan937d0de2009-10-15 18:35:38 +00005089 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005090 }
danielk19772dca4ac2008-01-03 11:50:29 +00005091 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00005092 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005093 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005094 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005095 ctx.s.flags = MEM_Null;
5096 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005097 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005098 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005099 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005100 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005101 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00005102 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005103 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005104 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005105 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005106 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005107 }
danielk19776ddcca52004-05-24 23:48:25 +00005108 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00005109 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005110 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005111 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005112 }
drh90669c12006-01-20 15:45:36 +00005113 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00005114 break;
5115}
5116
drh98757152008-01-09 23:04:12 +00005117/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00005118**
drh13449892005-09-07 21:22:45 +00005119** Execute the finalizer function for an aggregate. P1 is
5120** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005121**
5122** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005123** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005124** argument is not used by this opcode. It is only there to disambiguate
5125** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005126** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005127** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005128*/
drh9cbf3422008-01-17 16:22:13 +00005129case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005130 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00005131 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005132 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005133 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005134 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005135 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005136 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005137 }
drh2dca8682008-03-21 17:13:13 +00005138 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005139 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005140 if( sqlite3VdbeMemTooBig(pMem) ){
5141 goto too_big;
5142 }
drh5e00f6c2001-09-13 13:46:56 +00005143 break;
5144}
5145
dan5cf53532010-05-01 16:40:20 +00005146#ifndef SQLITE_OMIT_WAL
danf05c86d2010-04-13 11:56:03 +00005147/* Opcode: Checkpoint P1 * * * *
dane04dc882010-04-20 18:53:15 +00005148**
5149** Checkpoint database P1. This is a no-op if P1 is not currently in
5150** WAL mode.
dan7c246102010-04-12 19:00:29 +00005151*/
5152case OP_Checkpoint: {
dan586b9c82010-05-03 08:04:49 +00005153 rc = sqlite3Checkpoint(db, pOp->p1);
dan7c246102010-04-12 19:00:29 +00005154 break;
5155};
dan5cf53532010-05-01 16:40:20 +00005156#endif
drh5e00f6c2001-09-13 13:46:56 +00005157
drhab9b7442010-05-10 11:20:05 +00005158/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005159**
5160** Change the journal mode of database P1 to P3. P3 must be one of the
5161** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5162** modes (delete, truncate, persist, off and memory), this is a simple
5163** operation. No IO is required.
5164**
5165** If changing into or out of WAL mode the procedure is more complicated.
5166**
5167** Write a string containing the final journal-mode to register P2.
drhab9b7442010-05-10 11:20:05 +00005168**
5169** If an attempt to change in to or out of WAL mode fails because another
5170** connection also has the same database open, then an SQLITE_BUSY error
5171** is raised if P5==0, or of P5!=0 the journal mode changed is skipped
5172** without signaling the error.
dane04dc882010-04-20 18:53:15 +00005173*/
drhd80b2332010-05-01 00:59:37 +00005174case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005175 Btree *pBt; /* Btree to change journal mode of */
5176 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005177 int eNew; /* New journal mode */
5178 int eOld; /* The old journal mode */
drhd80b2332010-05-01 00:59:37 +00005179 const char *zFilename; /* Name of database file for pPager */
dane04dc882010-04-20 18:53:15 +00005180
drhd80b2332010-05-01 00:59:37 +00005181 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005182 assert( eNew==PAGER_JOURNALMODE_DELETE
5183 || eNew==PAGER_JOURNALMODE_TRUNCATE
5184 || eNew==PAGER_JOURNALMODE_PERSIST
5185 || eNew==PAGER_JOURNALMODE_OFF
5186 || eNew==PAGER_JOURNALMODE_MEMORY
5187 || eNew==PAGER_JOURNALMODE_WAL
5188 || eNew==PAGER_JOURNALMODE_QUERY
5189 );
5190 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh3ebaee92010-05-06 21:37:22 +00005191
5192 /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
5193 ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
5194 ** when the statment is prepared and so p->aMutex.nMutex>0. All mutexes
5195 ** are already acquired. But when used in ATTACH, sqlite3VdbeUsesBtree()
5196 ** is not called when the statement is prepared because it requires the
5197 ** iDb index of the database as a parameter, and the database has not
5198 ** yet been attached so that index is unavailable. We have to wait
5199 ** until runtime (now) to get the mutex on the newly attached database.
5200 ** No other mutexes are required by the ATTACH command so this is safe
5201 ** to do.
5202 */
5203 assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
5204 if( p->aMutex.nMutex==0 ){
5205 /* This occurs right after ATTACH. Get a mutex on the newly ATTACHed
5206 ** database. */
5207 sqlite3VdbeUsesBtree(p, pOp->p1);
5208 sqlite3VdbeMutexArrayEnter(p);
5209 }
dane04dc882010-04-20 18:53:15 +00005210
5211 pBt = db->aDb[pOp->p1].pBt;
5212 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005213 eOld = sqlite3PagerGetJournalMode(pPager);
5214 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5215 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005216
5217#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005218 zFilename = sqlite3PagerFilename(pPager);
dane04dc882010-04-20 18:53:15 +00005219
drhd80b2332010-05-01 00:59:37 +00005220 /* Do not allow a transition to journal_mode=WAL for a database
5221 ** in temporary storage or if the VFS does not support xShmOpen.
5222 */
5223 if( eNew==PAGER_JOURNALMODE_WAL
drhd9e5c4f2010-05-12 18:01:39 +00005224 && (zFilename[0]==0 /* Temp file */
5225 || !sqlite3PagerWalSupported(pPager)) /* No xShmOpen support */
dane180c292010-04-26 17:42:56 +00005226 ){
drh0b9b4302010-06-11 17:01:24 +00005227 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005228 }
5229
drh0b9b4302010-06-11 17:01:24 +00005230 if( (eNew!=eOld)
5231 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5232 ){
5233 if( !db->autoCommit || db->activeVdbeCnt>1 ){
5234 rc = SQLITE_ERROR;
5235 sqlite3SetString(&p->zErrMsg, db,
5236 "cannot change %s wal mode from within a transaction",
5237 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5238 );
5239 break;
5240 }else{
5241
5242 if( eOld==PAGER_JOURNALMODE_WAL ){
5243 /* If leaving WAL mode, close the log file. If successful, the call
5244 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5245 ** file. An EXCLUSIVE lock may still be held on the database file
5246 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005247 */
drh0b9b4302010-06-11 17:01:24 +00005248 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005249 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005250 sqlite3PagerSetJournalMode(pPager, eNew);
5251 }else if( rc==SQLITE_BUSY && pOp->p5==0 ){
5252 goto abort_due_to_error;
drh89c3f2f2010-05-15 01:09:38 +00005253 }
drh0b9b4302010-06-11 17:01:24 +00005254 }else{
5255 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_DELETE);
5256 rc = SQLITE_OK;
5257 }
5258
5259 /* Open a transaction on the database file. Regardless of the journal
5260 ** mode, this transaction always uses a rollback journal.
5261 */
5262 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5263 if( rc==SQLITE_OK ){
5264 rc = sqlite3BtreeSetVersion(pBt,
5265 (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
5266 if( rc==SQLITE_BUSY && pOp->p5==0 ) goto abort_due_to_error;
5267 }
5268 if( rc==SQLITE_BUSY ){
5269 eNew = eOld;
5270 rc = SQLITE_OK;
dane04dc882010-04-20 18:53:15 +00005271 }
5272 }
5273 }
dan5cf53532010-05-01 16:40:20 +00005274#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005275
drh0b9b4302010-06-11 17:01:24 +00005276 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dane04dc882010-04-20 18:53:15 +00005277 pOut = &aMem[pOp->p2];
5278 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005279 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005280 pOut->n = sqlite3Strlen30(pOut->z);
5281 pOut->enc = SQLITE_UTF8;
5282 sqlite3VdbeChangeEncoding(pOut, encoding);
5283 break;
5284};
5285
drhfdbcdee2007-03-27 14:44:50 +00005286#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005287/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005288**
5289** Vacuum the entire database. This opcode will cause other virtual
5290** machines to be created and run. It may not be called from within
5291** a transaction.
5292*/
drh9cbf3422008-01-17 16:22:13 +00005293case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00005294 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005295 break;
5296}
drh154d4b22006-09-21 11:02:16 +00005297#endif
drh6f8c91c2003-12-07 00:24:35 +00005298
danielk1977dddbcdc2007-04-26 14:42:34 +00005299#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005300/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005301**
5302** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005303** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005304** P2. Otherwise, fall through to the next instruction.
5305*/
drh9cbf3422008-01-17 16:22:13 +00005306case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005307 Btree *pBt;
5308
5309 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhfb982642007-08-30 01:19:59 +00005310 assert( (p->btreeMask & (1<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00005311 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005312 rc = sqlite3BtreeIncrVacuum(pBt);
5313 if( rc==SQLITE_DONE ){
5314 pc = pOp->p2 - 1;
5315 rc = SQLITE_OK;
5316 }
5317 break;
5318}
5319#endif
5320
drh98757152008-01-09 23:04:12 +00005321/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005322**
5323** Cause precompiled statements to become expired. An expired statement
5324** fails with an error code of SQLITE_SCHEMA if it is ever executed
5325** (via sqlite3_step()).
5326**
5327** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5328** then only the currently executing statement is affected.
5329*/
drh9cbf3422008-01-17 16:22:13 +00005330case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005331 if( !pOp->p1 ){
5332 sqlite3ExpirePreparedStatements(db);
5333 }else{
5334 p->expired = 1;
5335 }
5336 break;
5337}
5338
danielk1977c00da102006-01-07 13:21:04 +00005339#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005340/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00005341**
5342** Obtain a lock on a particular table. This instruction is only used when
5343** the shared-cache feature is enabled.
5344**
danielk197796d48e92009-06-29 06:00:37 +00005345** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005346** on which the lock is acquired. A readlock is obtained if P3==0 or
5347** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005348**
5349** P2 contains the root-page of the table to lock.
5350**
drh66a51672008-01-03 00:01:23 +00005351** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005352** used to generate an error message if the lock cannot be obtained.
5353*/
drh9cbf3422008-01-17 16:22:13 +00005354case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005355 u8 isWriteLock = (u8)pOp->p3;
5356 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5357 int p1 = pOp->p1;
5358 assert( p1>=0 && p1<db->nDb );
5359 assert( (p->btreeMask & (1<<p1))!=0 );
5360 assert( isWriteLock==0 || isWriteLock==1 );
5361 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5362 if( (rc&0xFF)==SQLITE_LOCKED ){
5363 const char *z = pOp->p4.z;
5364 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5365 }
danielk1977c00da102006-01-07 13:21:04 +00005366 }
5367 break;
5368}
drhb9bb7c12006-06-11 23:41:55 +00005369#endif /* SQLITE_OMIT_SHARED_CACHE */
5370
5371#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005372/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005373**
danielk19773e3a84d2008-08-01 17:37:40 +00005374** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5375** xBegin method for that table.
5376**
5377** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005378** within a callback to a virtual table xSync() method. If it is, the error
5379** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005380*/
drh9cbf3422008-01-17 16:22:13 +00005381case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005382 VTable *pVTab;
5383 pVTab = pOp->p4.pVtab;
5384 rc = sqlite3VtabBegin(db, pVTab);
5385 if( pVTab ){
danielk19773e3a84d2008-08-01 17:37:40 +00005386 sqlite3DbFree(db, p->zErrMsg);
danielk1977595a5232009-07-24 17:58:53 +00005387 p->zErrMsg = pVTab->pVtab->zErrMsg;
5388 pVTab->pVtab->zErrMsg = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005389 }
danielk1977f9e7dda2006-06-16 16:08:53 +00005390 break;
5391}
5392#endif /* SQLITE_OMIT_VIRTUALTABLE */
5393
5394#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005395/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005396**
drh66a51672008-01-03 00:01:23 +00005397** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005398** for that table.
5399*/
drh9cbf3422008-01-17 16:22:13 +00005400case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005401 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005402 break;
5403}
5404#endif /* SQLITE_OMIT_VIRTUALTABLE */
5405
5406#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005407/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005408**
drh66a51672008-01-03 00:01:23 +00005409** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005410** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005411*/
drh9cbf3422008-01-17 16:22:13 +00005412case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005413 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005414 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005415 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005416 break;
5417}
5418#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005419
drh9eff6162006-06-12 21:59:13 +00005420#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005421/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005422**
drh66a51672008-01-03 00:01:23 +00005423** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005424** P1 is a cursor number. This opcode opens a cursor to the virtual
5425** table and stores that cursor in P1.
5426*/
drh9cbf3422008-01-17 16:22:13 +00005427case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005428 VdbeCursor *pCur;
5429 sqlite3_vtab_cursor *pVtabCursor;
5430 sqlite3_vtab *pVtab;
5431 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005432
drh856c1032009-06-02 15:21:42 +00005433 pCur = 0;
5434 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005435 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005436 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005437 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005438 rc = pModule->xOpen(pVtab, &pVtabCursor);
drh633e6d52008-07-28 19:34:53 +00005439 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005440 p->zErrMsg = pVtab->zErrMsg;
5441 pVtab->zErrMsg = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005442 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005443 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005444 pVtabCursor->pVtab = pVtab;
5445
5446 /* Initialise vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005447 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005448 if( pCur ){
5449 pCur->pVtabCursor = pVtabCursor;
5450 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005451 }else{
drh17435752007-08-16 04:30:38 +00005452 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005453 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005454 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005455 }
drh9eff6162006-06-12 21:59:13 +00005456 break;
5457}
5458#endif /* SQLITE_OMIT_VIRTUALTABLE */
5459
5460#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005461/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005462**
5463** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5464** the filtered result set is empty.
5465**
drh66a51672008-01-03 00:01:23 +00005466** P4 is either NULL or a string that was generated by the xBestIndex
5467** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005468** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005469**
drh9eff6162006-06-12 21:59:13 +00005470** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005471** by P1. The integer query plan parameter to xFilter is stored in register
5472** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005473** xFilter method. Registers P3+2..P3+1+argc are the argc
5474** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005475** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005476**
danielk19776dbee812008-01-03 18:39:41 +00005477** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005478*/
drh9cbf3422008-01-17 16:22:13 +00005479case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005480 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005481 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005482 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005483 Mem *pQuery;
5484 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005485 sqlite3_vtab_cursor *pVtabCursor;
5486 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005487 VdbeCursor *pCur;
5488 int res;
5489 int i;
5490 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005491
drha6c2ed92009-11-14 23:22:23 +00005492 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005493 pArgc = &pQuery[1];
5494 pCur = p->apCsr[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00005495 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005496 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005497 pVtabCursor = pCur->pVtabCursor;
5498 pVtab = pVtabCursor->pVtab;
5499 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005500
drh9cbf3422008-01-17 16:22:13 +00005501 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005502 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005503 nArg = (int)pArgc->u.i;
5504 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005505
drh644a5292006-12-20 14:53:38 +00005506 /* Invoke the xFilter method */
5507 {
drh856c1032009-06-02 15:21:42 +00005508 res = 0;
5509 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005510 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005511 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005512 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005513 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005514
danielk1977be718892006-06-23 08:05:19 +00005515 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005516 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005517 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005518 sqlite3DbFree(db, p->zErrMsg);
5519 p->zErrMsg = pVtab->zErrMsg;
5520 pVtab->zErrMsg = 0;
danielk1977a298e902006-06-22 09:53:48 +00005521 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005522 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005523 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005524
danielk1977a298e902006-06-22 09:53:48 +00005525 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005526 pc = pOp->p2 - 1;
5527 }
5528 }
drh1d454a32008-01-31 19:34:51 +00005529 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005530
drh9eff6162006-06-12 21:59:13 +00005531 break;
5532}
5533#endif /* SQLITE_OMIT_VIRTUALTABLE */
5534
5535#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005536/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005537**
drh2133d822008-01-03 18:44:59 +00005538** Store the value of the P2-th column of
5539** the row of the virtual-table that the
5540** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005541*/
5542case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005543 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005544 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005545 Mem *pDest;
5546 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005547
drhdfe88ec2008-11-03 20:55:06 +00005548 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005549 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005550 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005551 pDest = &aMem[pOp->p3];
drh2945b4a2008-01-31 15:53:45 +00005552 if( pCur->nullRow ){
5553 sqlite3VdbeMemSetNull(pDest);
5554 break;
5555 }
danielk19773e3a84d2008-08-01 17:37:40 +00005556 pVtab = pCur->pVtabCursor->pVtab;
5557 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005558 assert( pModule->xColumn );
5559 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005560
5561 /* The output cell may already have a buffer allocated. Move
5562 ** the current contents to sContext.s so in case the user-function
5563 ** can use the already allocated buffer instead of allocating a
5564 ** new one.
5565 */
5566 sqlite3VdbeMemMove(&sContext.s, pDest);
5567 MemSetTypeFlag(&sContext.s, MEM_Null);
5568
drhde4fcfd2008-01-19 23:50:26 +00005569 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
danielk19773e3a84d2008-08-01 17:37:40 +00005570 sqlite3DbFree(db, p->zErrMsg);
5571 p->zErrMsg = pVtab->zErrMsg;
5572 pVtab->zErrMsg = 0;
drh4c8555f2009-06-25 01:47:11 +00005573 if( sContext.isError ){
5574 rc = sContext.isError;
5575 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005576
drhde4fcfd2008-01-19 23:50:26 +00005577 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005578 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005579 ** dynamic allocation in sContext.s (a Mem struct) is released.
5580 */
5581 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005582 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005583 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005584 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005585
drhde4fcfd2008-01-19 23:50:26 +00005586 if( sqlite3VdbeMemTooBig(pDest) ){
5587 goto too_big;
5588 }
drh9eff6162006-06-12 21:59:13 +00005589 break;
5590}
5591#endif /* SQLITE_OMIT_VIRTUALTABLE */
5592
5593#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005594/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005595**
5596** Advance virtual table P1 to the next row in its result set and
5597** jump to instruction P2. Or, if the virtual table has reached
5598** the end of its result set, then fall through to the next instruction.
5599*/
drh9cbf3422008-01-17 16:22:13 +00005600case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005601 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005602 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005603 int res;
drh856c1032009-06-02 15:21:42 +00005604 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005605
drhc54a6172009-06-02 16:06:03 +00005606 res = 0;
drh856c1032009-06-02 15:21:42 +00005607 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005608 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005609 if( pCur->nullRow ){
5610 break;
5611 }
danielk19773e3a84d2008-08-01 17:37:40 +00005612 pVtab = pCur->pVtabCursor->pVtab;
5613 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005614 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005615
drhde4fcfd2008-01-19 23:50:26 +00005616 /* Invoke the xNext() method of the module. There is no way for the
5617 ** underlying implementation to return an error if one occurs during
5618 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5619 ** data is available) and the error code returned when xColumn or
5620 ** some other method is next invoked on the save virtual table cursor.
5621 */
drhde4fcfd2008-01-19 23:50:26 +00005622 p->inVtabMethod = 1;
5623 rc = pModule->xNext(pCur->pVtabCursor);
5624 p->inVtabMethod = 0;
danielk19773e3a84d2008-08-01 17:37:40 +00005625 sqlite3DbFree(db, p->zErrMsg);
5626 p->zErrMsg = pVtab->zErrMsg;
5627 pVtab->zErrMsg = 0;
drhde4fcfd2008-01-19 23:50:26 +00005628 if( rc==SQLITE_OK ){
5629 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005630 }
5631
drhde4fcfd2008-01-19 23:50:26 +00005632 if( !res ){
5633 /* If there is data, jump to P2 */
5634 pc = pOp->p2 - 1;
5635 }
drh9eff6162006-06-12 21:59:13 +00005636 break;
5637}
5638#endif /* SQLITE_OMIT_VIRTUALTABLE */
5639
danielk1977182c4ba2007-06-27 15:53:34 +00005640#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005641/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005642**
drh66a51672008-01-03 00:01:23 +00005643** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005644** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005645** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005646*/
drh9cbf3422008-01-17 16:22:13 +00005647case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00005648 sqlite3_vtab *pVtab;
5649 Mem *pName;
5650
danielk1977595a5232009-07-24 17:58:53 +00005651 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00005652 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005653 assert( pVtab->pModule->xRename );
drh5b6afba2008-01-05 16:29:28 +00005654 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00005655 assert( pName->flags & MEM_Str );
danielk19776dbee812008-01-03 18:39:41 +00005656 rc = pVtab->pModule->xRename(pVtab, pName->z);
drh633e6d52008-07-28 19:34:53 +00005657 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005658 p->zErrMsg = pVtab->zErrMsg;
5659 pVtab->zErrMsg = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005660
danielk1977182c4ba2007-06-27 15:53:34 +00005661 break;
5662}
5663#endif
drh4cbdda92006-06-14 19:00:20 +00005664
5665#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005666/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005667**
drh66a51672008-01-03 00:01:23 +00005668** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005669** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005670** are contiguous memory cells starting at P3 to pass to the xUpdate
5671** invocation. The value in register (P3+P2-1) corresponds to the
5672** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005673**
5674** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005675** The argv[0] element (which corresponds to memory cell P3)
5676** is the rowid of a row to delete. If argv[0] is NULL then no
5677** deletion occurs. The argv[1] element is the rowid of the new
5678** row. This can be NULL to have the virtual table select the new
5679** rowid for itself. The subsequent elements in the array are
5680** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005681**
5682** If P2==1 then no insert is performed. argv[0] is the rowid of
5683** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005684**
5685** P1 is a boolean flag. If it is set to true and the xUpdate call
5686** is successful, then the value returned by sqlite3_last_insert_rowid()
5687** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005688*/
drh9cbf3422008-01-17 16:22:13 +00005689case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00005690 sqlite3_vtab *pVtab;
5691 sqlite3_module *pModule;
5692 int nArg;
5693 int i;
5694 sqlite_int64 rowid;
5695 Mem **apArg;
5696 Mem *pX;
5697
danielk1977595a5232009-07-24 17:58:53 +00005698 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005699 pModule = (sqlite3_module *)pVtab->pModule;
5700 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005701 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00005702 if( ALWAYS(pModule->xUpdate) ){
drh856c1032009-06-02 15:21:42 +00005703 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00005704 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00005705 for(i=0; i<nArg; i++){
dan937d0de2009-10-15 18:35:38 +00005706 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00005707 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005708 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005709 }
danielk19771f6eec52006-06-16 06:17:47 +00005710 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
drh633e6d52008-07-28 19:34:53 +00005711 sqlite3DbFree(db, p->zErrMsg);
drh80cc85b2008-07-23 21:07:25 +00005712 p->zErrMsg = pVtab->zErrMsg;
5713 pVtab->zErrMsg = 0;
drh35f6b932009-06-23 14:15:04 +00005714 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00005715 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
5716 db->lastRowid = rowid;
5717 }
drhb5df1442008-04-10 14:00:09 +00005718 p->nChange++;
danielk1977399918f2006-06-14 13:03:23 +00005719 }
drh4cbdda92006-06-14 19:00:20 +00005720 break;
danielk1977399918f2006-06-14 13:03:23 +00005721}
5722#endif /* SQLITE_OMIT_VIRTUALTABLE */
5723
danielk197759a93792008-05-15 17:48:20 +00005724#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5725/* Opcode: Pagecount P1 P2 * * *
5726**
5727** Write the current number of pages in database P1 to memory cell P2.
5728*/
5729case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00005730 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00005731 break;
5732}
5733#endif
5734
drh949f9cd2008-01-12 21:35:57 +00005735#ifndef SQLITE_OMIT_TRACE
5736/* Opcode: Trace * * * P4 *
5737**
5738** If tracing is enabled (by the sqlite3_trace()) interface, then
5739** the UTF-8 string contained in P4 is emitted on the trace callback.
5740*/
5741case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00005742 char *zTrace;
5743
5744 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
danielk19776ab3a2e2009-02-19 14:39:25 +00005745 if( zTrace ){
drh949f9cd2008-01-12 21:35:57 +00005746 if( db->xTrace ){
drhc7bc4fd2009-11-25 18:03:42 +00005747 char *z = sqlite3VdbeExpandSql(p, zTrace);
5748 db->xTrace(db->pTraceArg, z);
5749 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00005750 }
5751#ifdef SQLITE_DEBUG
5752 if( (db->flags & SQLITE_SqlTrace)!=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00005753 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
drh949f9cd2008-01-12 21:35:57 +00005754 }
5755#endif /* SQLITE_DEBUG */
5756 }
5757 break;
5758}
5759#endif
5760
drh91fd4d42008-01-19 20:11:25 +00005761
5762/* Opcode: Noop * * * * *
5763**
5764** Do nothing. This instruction is often useful as a jump
5765** destination.
drh5e00f6c2001-09-13 13:46:56 +00005766*/
drh91fd4d42008-01-19 20:11:25 +00005767/*
5768** The magic Explain opcode are only inserted when explain==2 (which
5769** is to say when the EXPLAIN QUERY PLAN syntax is used.)
5770** This opcode records information from the optimizer. It is the
5771** the same as a no-op. This opcodesnever appears in a real VM program.
5772*/
5773default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00005774 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00005775 break;
5776}
5777
5778/*****************************************************************************
5779** The cases of the switch statement above this line should all be indented
5780** by 6 spaces. But the left-most 6 spaces have been removed to improve the
5781** readability. From this point on down, the normal indentation rules are
5782** restored.
5783*****************************************************************************/
5784 }
drh6e142f52000-06-08 13:36:40 +00005785
drh7b396862003-01-01 23:06:20 +00005786#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00005787 {
shane9bcbdad2008-05-29 20:22:37 +00005788 u64 elapsed = sqlite3Hwtime() - start;
5789 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00005790 pOp->cnt++;
5791#if 0
shane9bcbdad2008-05-29 20:22:37 +00005792 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00005793 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00005794#endif
5795 }
drh7b396862003-01-01 23:06:20 +00005796#endif
5797
drh6e142f52000-06-08 13:36:40 +00005798 /* The following code adds nothing to the actual functionality
5799 ** of the program. It is only here for testing and debugging.
5800 ** On the other hand, it does burn CPU cycles every time through
5801 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
5802 */
5803#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00005804 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00005805
drhcf1023c2007-05-08 20:59:49 +00005806#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00005807 if( p->trace ){
5808 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00005809 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
5810 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00005811 }
drh3c657212009-11-17 23:59:58 +00005812 if( pOp->opflags & OPFLG_OUT3 ){
5813 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00005814 }
drh75897232000-05-29 14:26:00 +00005815 }
danielk1977b5402fb2005-01-12 07:15:04 +00005816#endif /* SQLITE_DEBUG */
5817#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00005818 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005819
drha05a7222008-01-19 03:35:58 +00005820 /* If we reach this point, it means that execution is finished with
5821 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00005822 */
drha05a7222008-01-19 03:35:58 +00005823vdbe_error_halt:
5824 assert( rc );
5825 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00005826 testcase( sqlite3GlobalConfig.xLog!=0 );
5827 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
5828 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00005829 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00005830 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
5831 rc = SQLITE_ERROR;
drh32783152009-11-20 15:02:34 +00005832 if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);
drh900b31e2007-08-28 02:27:51 +00005833
5834 /* This is the only way out of this procedure. We have to
5835 ** release the mutexes on btrees that were acquired at the
5836 ** top. */
5837vdbe_return:
drh4cf7c7f2007-08-28 23:28:07 +00005838 sqlite3BtreeMutexArrayLeave(&p->aMutex);
drhb86ccfb2003-01-28 23:13:10 +00005839 return rc;
5840
drh023ae032007-05-08 12:12:16 +00005841 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
5842 ** is encountered.
5843 */
5844too_big:
drhf089aa42008-07-08 19:34:06 +00005845 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00005846 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00005847 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00005848
drh98640a32007-06-07 19:08:32 +00005849 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00005850 */
5851no_mem:
drh17435752007-08-16 04:30:38 +00005852 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00005853 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00005854 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00005855 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005856
drhb86ccfb2003-01-28 23:13:10 +00005857 /* Jump to here for any other kind of fatal error. The "rc" variable
5858 ** should hold the error number.
5859 */
5860abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00005861 assert( p->zErrMsg==0 );
5862 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00005863 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00005864 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00005865 }
drha05a7222008-01-19 03:35:58 +00005866 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005867
danielk19776f8a5032004-05-10 10:34:51 +00005868 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005869 ** flag.
5870 */
5871abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00005872 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00005873 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00005874 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00005875 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00005876 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00005877}