blob: 7dcdf4621bdd899f7ffbcc85a6874d3559404922 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drh9a324642003-09-06 20:12:01 +000012** The code in this file implements execution method of the
13** Virtual Database Engine (VDBE). A separate file ("vdbeaux.c")
14** handles housekeeping details such as creating and deleting
15** VDBE instances. This file is solely interested in executing
16** the VDBE program.
17**
danielk1977fc57d7b2004-05-26 02:04:57 +000018** In the external interface, an "sqlite3_stmt*" is an opaque pointer
drh9a324642003-09-06 20:12:01 +000019** to a VDBE.
drh75897232000-05-29 14:26:00 +000020**
21** The SQL parser generates a program which is then executed by
22** the VDBE to do the work of the SQL statement. VDBE programs are
23** similar in form to assembly language. The program consists of
24** a linear sequence of operations. Each operation has an opcode
drh9cbf3422008-01-17 16:22:13 +000025** and 5 operands. Operands P1, P2, and P3 are integers. Operand P4
26** is a null-terminated string. Operand P5 is an unsigned character.
27** Few opcodes use all 5 operands.
drh75897232000-05-29 14:26:00 +000028**
drh9cbf3422008-01-17 16:22:13 +000029** Computation results are stored on a set of registers numbered beginning
30** with 1 and going up to Vdbe.nMem. Each register can store
31** either an integer, a null-terminated string, a floating point
shane21e7feb2008-05-30 15:59:49 +000032** number, or the SQL "NULL" value. An implicit conversion from one
drhb19a2bc2001-09-16 00:13:26 +000033** type to the other occurs as necessary.
drh75897232000-05-29 14:26:00 +000034**
danielk19774adee202004-05-08 08:23:19 +000035** Most of the code in this file is taken up by the sqlite3VdbeExec()
drh75897232000-05-29 14:26:00 +000036** function which does the work of interpreting a VDBE program.
37** But other routines are also provided to help in building up
38** a program instruction by instruction.
39**
drhac82fcf2002-09-08 17:23:41 +000040** Various scripts scan this source file in order to generate HTML
41** documentation, headers files, or other derived files. The formatting
42** of the code in this file is, therefore, important. See other comments
43** in this file for details. If in doubt, do not deviate from existing
44** commenting and indentation practices when changing or adding code.
drh75897232000-05-29 14:26:00 +000045*/
46#include "sqliteInt.h"
drh9a324642003-09-06 20:12:01 +000047#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000048
49/*
drh2b4ded92010-09-27 21:09:31 +000050** Invoke this macro on memory cells just prior to changing the
51** value of the cell. This macro verifies that shallow copies are
52** not misused.
53*/
54#ifdef SQLITE_DEBUG
55# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
56#else
57# define memAboutToChange(P,M)
58#endif
59
60/*
drh487ab3c2001-11-08 00:45:21 +000061** The following global variable is incremented every time a cursor
drh959403f2008-12-12 17:56:16 +000062** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000063** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000064** working correctly. This variable has no function other than to
65** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000066*/
drh0f7eb612006-08-08 13:51:43 +000067#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000068int sqlite3_search_count = 0;
drh0f7eb612006-08-08 13:51:43 +000069#endif
drh487ab3c2001-11-08 00:45:21 +000070
drhf6038712004-02-08 18:07:34 +000071/*
72** When this global variable is positive, it gets decremented once before
drh881feaa2006-07-26 01:39:30 +000073** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
74** field of the sqlite3 structure is set in order to simulate and interrupt.
drhf6038712004-02-08 18:07:34 +000075**
76** This facility is used for testing purposes only. It does not function
77** in an ordinary build.
78*/
drh0f7eb612006-08-08 13:51:43 +000079#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +000080int sqlite3_interrupt_count = 0;
drh0f7eb612006-08-08 13:51:43 +000081#endif
drh1350b032002-02-27 19:00:20 +000082
danielk19777e18c252004-05-25 11:47:24 +000083/*
drh6bf89572004-11-03 16:27:01 +000084** The next global variable is incremented each type the OP_Sort opcode
85** is executed. The test procedures use this information to make sure that
shane21e7feb2008-05-30 15:59:49 +000086** sorting is occurring or not occurring at appropriate times. This variable
drh6bf89572004-11-03 16:27:01 +000087** has no function other than to help verify the correct operation of the
88** library.
89*/
drh0f7eb612006-08-08 13:51:43 +000090#ifdef SQLITE_TEST
drh6bf89572004-11-03 16:27:01 +000091int sqlite3_sort_count = 0;
drh0f7eb612006-08-08 13:51:43 +000092#endif
drh6bf89572004-11-03 16:27:01 +000093
94/*
drhae7e1512007-05-02 16:51:59 +000095** The next global variable records the size of the largest MEM_Blob
drh9cbf3422008-01-17 16:22:13 +000096** or MEM_Str that has been used by a VDBE opcode. The test procedures
drhae7e1512007-05-02 16:51:59 +000097** use this information to make sure that the zero-blob functionality
98** is working correctly. This variable has no function other than to
99** help verify the correct operation of the library.
100*/
101#ifdef SQLITE_TEST
102int sqlite3_max_blobsize = 0;
drhca48c902008-01-18 14:08:24 +0000103static void updateMaxBlobsize(Mem *p){
104 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
105 sqlite3_max_blobsize = p->n;
106 }
107}
drhae7e1512007-05-02 16:51:59 +0000108#endif
109
110/*
dan0ff297e2009-09-25 17:03:14 +0000111** The next global variable is incremented each type the OP_Found opcode
112** is executed. This is used to test whether or not the foreign key
113** operation implemented using OP_FkIsZero is working. This variable
114** has no function other than to help verify the correct operation of the
115** library.
116*/
117#ifdef SQLITE_TEST
118int sqlite3_found_count = 0;
119#endif
120
121/*
drhb7654112008-01-12 12:48:07 +0000122** Test a register to see if it exceeds the current maximum blob size.
123** If it does, record the new maximum blob size.
124*/
drh678ccce2008-03-31 18:19:54 +0000125#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
drhca48c902008-01-18 14:08:24 +0000126# define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P)
drhb7654112008-01-12 12:48:07 +0000127#else
128# define UPDATE_MAX_BLOBSIZE(P)
129#endif
130
131/*
drh9cbf3422008-01-17 16:22:13 +0000132** Convert the given register into a string if it isn't one
danielk1977bd7e4602004-05-24 07:34:48 +0000133** already. Return non-zero if a malloc() fails.
134*/
drhb21c8cd2007-08-21 19:33:56 +0000135#define Stringify(P, enc) \
136 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
drhf4479502004-05-27 03:12:53 +0000137 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +0000138
139/*
danielk1977bd7e4602004-05-24 07:34:48 +0000140** An ephemeral string value (signified by the MEM_Ephem flag) contains
141** a pointer to a dynamically allocated string where some other entity
drh9cbf3422008-01-17 16:22:13 +0000142** is responsible for deallocating that string. Because the register
143** does not control the string, it might be deleted without the register
144** knowing it.
danielk1977bd7e4602004-05-24 07:34:48 +0000145**
146** This routine converts an ephemeral string into a dynamically allocated
drh9cbf3422008-01-17 16:22:13 +0000147** string that the register itself controls. In other words, it
danielk1977bd7e4602004-05-24 07:34:48 +0000148** converts an MEM_Ephem string into an MEM_Dyn string.
149*/
drhb21c8cd2007-08-21 19:33:56 +0000150#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000151 if( ((P)->flags&MEM_Ephem)!=0 \
drhb21c8cd2007-08-21 19:33:56 +0000152 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000153
154/*
danielk19771cc5ed82007-05-16 17:28:43 +0000155** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
156** P if required.
157*/
drhb21c8cd2007-08-21 19:33:56 +0000158#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
danielk19771cc5ed82007-05-16 17:28:43 +0000159
dan689ab892011-08-12 15:02:00 +0000160/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
161#ifdef SQLITE_OMIT_MERGE_SORT
162# define isSorter(x) 0
163#else
164# define isSorter(x) ((x)->pSorter!=0)
165#endif
166
danielk19771cc5ed82007-05-16 17:28:43 +0000167/*
shane21e7feb2008-05-30 15:59:49 +0000168** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000169** user-defined function or returned to the user as the result of a query.
dan937d0de2009-10-15 18:35:38 +0000170** This routine sets the pMem->type variable used by the sqlite3_value_*()
171** routines.
danielk1977c572ef72004-05-27 09:28:41 +0000172*/
dan937d0de2009-10-15 18:35:38 +0000173void sqlite3VdbeMemStoreType(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000174 int flags = pMem->flags;
175 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000176 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000177 }
178 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000179 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000180 }
181 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000182 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000183 }
184 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000185 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000186 }else{
drh9c054832004-05-31 18:51:57 +0000187 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000188 }
189}
danielk19778a6b5412004-05-24 07:04:25 +0000190
191/*
drhdfe88ec2008-11-03 20:55:06 +0000192** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000193** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000194*/
drhdfe88ec2008-11-03 20:55:06 +0000195static VdbeCursor *allocateCursor(
196 Vdbe *p, /* The virtual machine */
197 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000198 int nField, /* Number of fields in the table or index */
drh3d4501e2008-12-04 20:40:10 +0000199 int iDb, /* When database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000200 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000201){
202 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000203 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000204 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000205 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000206 **
207 ** * Sometimes cursor numbers are used for a couple of different
208 ** purposes in a vdbe program. The different uses might require
209 ** different sized allocations. Memory cells provide growable
210 ** allocations.
211 **
212 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
213 ** be freed lazily via the sqlite3_release_memory() API. This
214 ** minimizes the number of malloc calls made by the system.
215 **
216 ** Memory cells for cursors are allocated at the top of the address
217 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
218 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
219 */
220 Mem *pMem = &p->aMem[p->nMem-iCur];
221
danielk19775f096132008-03-28 15:44:09 +0000222 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000223 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000224 nByte =
drhc54055b2009-11-13 17:05:53 +0000225 ROUND8(sizeof(VdbeCursor)) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000226 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
227 2*nField*sizeof(u32);
228
drh290c1942004-08-21 17:54:45 +0000229 assert( iCur<p->nCursor );
230 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000231 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000232 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000233 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000234 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000235 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000236 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000237 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000238 pCx->nField = nField;
239 if( nField ){
drhc54055b2009-11-13 17:05:53 +0000240 pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
danielk1977cd3e8f72008-03-25 09:47:35 +0000241 }
242 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000243 pCx->pCursor = (BtCursor*)
drhc54055b2009-11-13 17:05:53 +0000244 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
drhf25a5072009-11-18 23:01:25 +0000245 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000246 }
danielk197794eb6a12005-12-15 15:22:08 +0000247 }
drh4774b132004-06-12 20:12:51 +0000248 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000249}
250
danielk19773d1bfea2004-05-14 11:00:53 +0000251/*
drh29d72102006-02-09 22:13:41 +0000252** Try to convert a value into a numeric representation if we can
253** do so without loss of information. In other words, if the string
254** looks like a number, convert it into a number. If it does not
255** look like a number, leave it alone.
256*/
drhb21c8cd2007-08-21 19:33:56 +0000257static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000258 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000259 double rValue;
260 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000261 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000262 if( (pRec->flags&MEM_Str)==0 ) return;
263 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000264 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000265 pRec->u.i = iValue;
266 pRec->flags |= MEM_Int;
267 }else{
268 pRec->r = rValue;
269 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000270 }
271 }
272}
273
274/*
drh8a512562005-11-14 22:29:05 +0000275** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000276**
drh8a512562005-11-14 22:29:05 +0000277** SQLITE_AFF_INTEGER:
278** SQLITE_AFF_REAL:
279** SQLITE_AFF_NUMERIC:
280** Try to convert pRec to an integer representation or a
281** floating-point representation if an integer representation
282** is not possible. Note that the integer representation is
283** always preferred, even if the affinity is REAL, because
284** an integer representation is more space efficient on disk.
285**
286** SQLITE_AFF_TEXT:
287** Convert pRec to a text representation.
288**
289** SQLITE_AFF_NONE:
290** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000291*/
drh17435752007-08-16 04:30:38 +0000292static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000293 Mem *pRec, /* The value to apply affinity to */
294 char affinity, /* The affinity to be applied */
295 u8 enc /* Use this text encoding */
296){
drh8a512562005-11-14 22:29:05 +0000297 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000298 /* Only attempt the conversion to TEXT if there is an integer or real
299 ** representation (blob and NULL do not get converted) but no string
300 ** representation.
301 */
302 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000303 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000304 }
305 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000306 }else if( affinity!=SQLITE_AFF_NONE ){
307 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
308 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000309 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000310 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000311 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000312 }
danielk19773d1bfea2004-05-14 11:00:53 +0000313 }
314}
315
danielk1977aee18ef2005-03-09 12:26:50 +0000316/*
drh29d72102006-02-09 22:13:41 +0000317** Try to convert the type of a function argument or a result column
318** into a numeric representation. Use either INTEGER or REAL whichever
319** is appropriate. But only do the conversion if it is possible without
320** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000321*/
322int sqlite3_value_numeric_type(sqlite3_value *pVal){
323 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000324 if( pMem->type==SQLITE_TEXT ){
325 applyNumericAffinity(pMem);
326 sqlite3VdbeMemStoreType(pMem);
327 }
drh29d72102006-02-09 22:13:41 +0000328 return pMem->type;
329}
330
331/*
danielk1977aee18ef2005-03-09 12:26:50 +0000332** Exported version of applyAffinity(). This one works on sqlite3_value*,
333** not the internal Mem* type.
334*/
danielk19771e536952007-08-16 10:09:01 +0000335void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000336 sqlite3_value *pVal,
337 u8 affinity,
338 u8 enc
339){
drhb21c8cd2007-08-21 19:33:56 +0000340 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000341}
342
danielk1977b5402fb2005-01-12 07:15:04 +0000343#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000344/*
danielk1977ca6b2912004-05-21 10:49:47 +0000345** Write a nice string representation of the contents of cell pMem
346** into buffer zBuf, length nBuf.
347*/
drh74161702006-02-24 02:53:49 +0000348void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000349 char *zCsr = zBuf;
350 int f = pMem->flags;
351
drh57196282004-10-06 15:41:16 +0000352 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000353
danielk1977ca6b2912004-05-21 10:49:47 +0000354 if( f&MEM_Blob ){
355 int i;
356 char c;
357 if( f & MEM_Dyn ){
358 c = 'z';
359 assert( (f & (MEM_Static|MEM_Ephem))==0 );
360 }else if( f & MEM_Static ){
361 c = 't';
362 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
363 }else if( f & MEM_Ephem ){
364 c = 'e';
365 assert( (f & (MEM_Static|MEM_Dyn))==0 );
366 }else{
367 c = 's';
368 }
369
drh5bb3eb92007-05-04 13:15:55 +0000370 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000371 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000372 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000373 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000374 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000375 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000376 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000377 }
378 for(i=0; i<16 && i<pMem->n; i++){
379 char z = pMem->z[i];
380 if( z<32 || z>126 ) *zCsr++ = '.';
381 else *zCsr++ = z;
382 }
383
drhe718efe2007-05-10 21:14:03 +0000384 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000385 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000386 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000387 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000388 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000389 }
danielk1977b1bc9532004-05-22 03:05:33 +0000390 *zCsr = '\0';
391 }else if( f & MEM_Str ){
392 int j, k;
393 zBuf[0] = ' ';
394 if( f & MEM_Dyn ){
395 zBuf[1] = 'z';
396 assert( (f & (MEM_Static|MEM_Ephem))==0 );
397 }else if( f & MEM_Static ){
398 zBuf[1] = 't';
399 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
400 }else if( f & MEM_Ephem ){
401 zBuf[1] = 'e';
402 assert( (f & (MEM_Static|MEM_Dyn))==0 );
403 }else{
404 zBuf[1] = 's';
405 }
406 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000407 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000408 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000409 zBuf[k++] = '[';
410 for(j=0; j<15 && j<pMem->n; j++){
411 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000412 if( c>=0x20 && c<0x7f ){
413 zBuf[k++] = c;
414 }else{
415 zBuf[k++] = '.';
416 }
417 }
418 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000419 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000420 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000421 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000422 }
danielk1977ca6b2912004-05-21 10:49:47 +0000423}
424#endif
425
drh5b6afba2008-01-05 16:29:28 +0000426#ifdef SQLITE_DEBUG
427/*
428** Print the value of a register for tracing purposes:
429*/
430static void memTracePrint(FILE *out, Mem *p){
431 if( p->flags & MEM_Null ){
432 fprintf(out, " NULL");
433 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
434 fprintf(out, " si:%lld", p->u.i);
435 }else if( p->flags & MEM_Int ){
436 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000437#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000438 }else if( p->flags & MEM_Real ){
439 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000440#endif
drh733bf1b2009-04-22 00:47:00 +0000441 }else if( p->flags & MEM_RowSet ){
442 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000443 }else{
444 char zBuf[200];
445 sqlite3VdbeMemPrettyPrint(p, zBuf);
446 fprintf(out, " ");
447 fprintf(out, "%s", zBuf);
448 }
449}
450static void registerTrace(FILE *out, int iReg, Mem *p){
451 fprintf(out, "REG[%d] = ", iReg);
452 memTracePrint(out, p);
453 fprintf(out, "\n");
454}
455#endif
456
457#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000458# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000459#else
460# define REGISTER_TRACE(R,M)
461#endif
462
danielk197784ac9d02004-05-18 09:58:06 +0000463
drh7b396862003-01-01 23:06:20 +0000464#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000465
466/*
467** hwtime.h contains inline assembler code for implementing
468** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000469*/
shane9bcbdad2008-05-29 20:22:37 +0000470#include "hwtime.h"
471
drh7b396862003-01-01 23:06:20 +0000472#endif
473
drh8c74a8c2002-08-25 19:20:40 +0000474/*
drhcaec2f12003-01-07 02:47:47 +0000475** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000476** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000477** processing of the VDBE program is interrupted.
478**
479** This macro added to every instruction that does a jump in order to
480** implement a loop. This test used to be on every single instruction,
481** but that meant we more testing that we needed. By only testing the
482** flag on jump instructions, we get a (small) speed improvement.
483*/
484#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000485 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000486
487
danielk1977fd7f0452008-12-17 17:30:26 +0000488#ifndef NDEBUG
489/*
490** This function is only called from within an assert() expression. It
491** checks that the sqlite3.nTransaction variable is correctly set to
492** the number of non-transaction savepoints currently in the
493** linked list starting at sqlite3.pSavepoint.
494**
495** Usage:
496**
497** assert( checkSavepointCount(db) );
498*/
499static int checkSavepointCount(sqlite3 *db){
500 int n = 0;
501 Savepoint *p;
502 for(p=db->pSavepoint; p; p=p->pNext) n++;
503 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
504 return 1;
505}
506#endif
507
drhcaec2f12003-01-07 02:47:47 +0000508/*
drhb9755982010-07-24 16:34:37 +0000509** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
510** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
511** in memory obtained from sqlite3DbMalloc).
512*/
513static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
514 sqlite3 *db = p->db;
515 sqlite3DbFree(db, p->zErrMsg);
516 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
517 sqlite3_free(pVtab->zErrMsg);
518 pVtab->zErrMsg = 0;
519}
520
521
522/*
drhb86ccfb2003-01-28 23:13:10 +0000523** Execute as much of a VDBE program as we can then return.
524**
danielk19774adee202004-05-08 08:23:19 +0000525** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000526** close the program with a final OP_Halt and to set up the callbacks
527** and the error message pointer.
528**
529** Whenever a row or result data is available, this routine will either
530** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000531** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000532**
533** If an attempt is made to open a locked database, then this routine
534** will either invoke the busy callback (if there is one) or it will
535** return SQLITE_BUSY.
536**
537** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000538** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000539** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
540**
541** If the callback ever returns non-zero, then the program exits
542** immediately. There will be no error message but the p->rc field is
543** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
544**
drh9468c7f2003-03-07 19:50:07 +0000545** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
546** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000547**
548** Other fatal errors return SQLITE_ERROR.
549**
danielk19774adee202004-05-08 08:23:19 +0000550** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000551** used to clean up the mess that was left behind.
552*/
danielk19774adee202004-05-08 08:23:19 +0000553int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000554 Vdbe *p /* The VDBE */
555){
shaneh84f4b2f2010-02-26 01:46:54 +0000556 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000557 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000558 Op *pOp; /* Current operation */
559 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000560 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000561 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000562 u8 encoding = ENC(db); /* The database encoding */
drha6c2ed92009-11-14 23:22:23 +0000563#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
shaneh5e17e8b2009-12-03 04:40:47 +0000564 int checkProgress; /* True if progress callbacks are enabled */
drha6c2ed92009-11-14 23:22:23 +0000565 int nProgressOps = 0; /* Opcodes executed since progress callback. */
566#endif
567 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000568 Mem *pIn1 = 0; /* 1st input operand */
569 Mem *pIn2 = 0; /* 2nd input operand */
570 Mem *pIn3 = 0; /* 3rd input operand */
571 Mem *pOut = 0; /* Output operand */
drh0acb7e42008-06-25 00:12:41 +0000572 int iCompare = 0; /* Result of last OP_Compare operation */
shanebe217792009-03-05 04:20:31 +0000573 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000574 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000575#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000576 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000577 int origPc; /* Program counter at start of opcode */
578#endif
drh856c1032009-06-02 15:21:42 +0000579 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000580
drhca48c902008-01-18 14:08:24 +0000581 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000582 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000583 if( p->rc==SQLITE_NOMEM ){
584 /* This happens if a malloc() inside a call to sqlite3_column_text() or
585 ** sqlite3_column_text16() failed. */
586 goto no_mem;
587 }
drh3a840692003-01-29 22:58:26 +0000588 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
589 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000590 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000591 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000592 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000593 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000594 sqlite3VdbeIOTraceSql(p);
drha6c2ed92009-11-14 23:22:23 +0000595#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
596 checkProgress = db->xProgress!=0;
597#endif
drh3c23a882007-01-09 14:01:13 +0000598#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000599 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000600 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000601 int i;
602 printf("VDBE Program Listing:\n");
603 sqlite3VdbePrintSql(p);
604 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000605 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000606 }
607 }
danielk19772d1d86f2008-06-20 14:59:51 +0000608 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000609#endif
drhb86ccfb2003-01-28 23:13:10 +0000610 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000611 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000612 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000613#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000614 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000615 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000616#endif
drhbbe879d2009-11-14 18:04:35 +0000617 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000618
danielk19778b60e0f2005-01-12 09:10:39 +0000619 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000620 */
danielk19778b60e0f2005-01-12 09:10:39 +0000621#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000622 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000623 if( pc==0 ){
624 printf("VDBE Execution Trace:\n");
625 sqlite3VdbePrintSql(p);
626 }
danielk19774adee202004-05-08 08:23:19 +0000627 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000628 }
drh3f7d4e42004-07-24 14:35:58 +0000629#endif
630
drh6e142f52000-06-08 13:36:40 +0000631
drhf6038712004-02-08 18:07:34 +0000632 /* Check to see if we need to simulate an interrupt. This only happens
633 ** if we have a special test build.
634 */
635#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000636 if( sqlite3_interrupt_count>0 ){
637 sqlite3_interrupt_count--;
638 if( sqlite3_interrupt_count==0 ){
639 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000640 }
641 }
642#endif
643
danielk1977348bb5d2003-10-18 09:37:26 +0000644#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
645 /* Call the progress callback if it is configured and the required number
646 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000647 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000648 ** If the progress callback returns non-zero, exit the virtual machine with
649 ** a return code SQLITE_ABORT.
650 */
drha6c2ed92009-11-14 23:22:23 +0000651 if( checkProgress ){
drh3914aed2004-01-31 20:40:42 +0000652 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000653 int prc;
drh9978c972010-02-23 17:36:32 +0000654 prc = db->xProgress(db->pProgressArg);
danielk1977de523ac2007-06-15 14:53:53 +0000655 if( prc!=0 ){
656 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000657 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000658 }
danielk19773fe11f32007-06-13 16:49:48 +0000659 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000660 }
drh3914aed2004-01-31 20:40:42 +0000661 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000662 }
danielk1977348bb5d2003-10-18 09:37:26 +0000663#endif
664
drh3c657212009-11-17 23:59:58 +0000665 /* On any opcode with the "out2-prerelase" tag, free any
666 ** external allocations out of mem[p2] and set mem[p2] to be
667 ** an undefined integer. Opcodes will either fill in the integer
668 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000669 */
drha6c2ed92009-11-14 23:22:23 +0000670 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000671 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
672 assert( pOp->p2>0 );
673 assert( pOp->p2<=p->nMem );
674 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000675 memAboutToChange(p, pOut);
drh2d36eb42011-08-29 02:49:41 +0000676 MemReleaseExt(pOut);
drh3c657212009-11-17 23:59:58 +0000677 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000678 }
drh3c657212009-11-17 23:59:58 +0000679
680 /* Sanity checking on other operands */
681#ifdef SQLITE_DEBUG
682 if( (pOp->opflags & OPFLG_IN1)!=0 ){
683 assert( pOp->p1>0 );
684 assert( pOp->p1<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000685 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000686 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
687 }
688 if( (pOp->opflags & OPFLG_IN2)!=0 ){
689 assert( pOp->p2>0 );
690 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000691 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000692 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
693 }
694 if( (pOp->opflags & OPFLG_IN3)!=0 ){
695 assert( pOp->p3>0 );
696 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000697 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000698 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
699 }
700 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
701 assert( pOp->p2>0 );
702 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000703 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000704 }
705 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
706 assert( pOp->p3>0 );
707 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000708 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000709 }
710#endif
drh93952eb2009-11-13 19:43:43 +0000711
drh75897232000-05-29 14:26:00 +0000712 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000713
drh5e00f6c2001-09-13 13:46:56 +0000714/*****************************************************************************
715** What follows is a massive switch statement where each case implements a
716** separate instruction in the virtual machine. If we follow the usual
717** indentation conventions, each case should be indented by 6 spaces. But
718** that is a lot of wasted space on the left margin. So the code within
719** the switch statement will break with convention and be flush-left. Another
720** big comment (similar to this one) will mark the point in the code where
721** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000722**
723** The formatting of each case is important. The makefile for SQLite
724** generates two C files "opcodes.h" and "opcodes.c" by scanning this
725** file looking for lines that begin with "case OP_". The opcodes.h files
726** will be filled with #defines that give unique integer values to each
727** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000728** each string is the symbolic name for the corresponding opcode. If the
729** case statement is followed by a comment of the form "/# same as ... #/"
730** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000731**
drh9cbf3422008-01-17 16:22:13 +0000732** Other keywords in the comment that follows each case are used to
733** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
734** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
735** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000736**
drhac82fcf2002-09-08 17:23:41 +0000737** Documentation about VDBE opcodes is generated by scanning this file
738** for lines of that contain "Opcode:". That line and all subsequent
739** comment lines are used in the generation of the opcode.html documentation
740** file.
741**
742** SUMMARY:
743**
744** Formatting is important to scripts that scan this file.
745** Do not deviate from the formatting style currently in use.
746**
drh5e00f6c2001-09-13 13:46:56 +0000747*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000748
drh9cbf3422008-01-17 16:22:13 +0000749/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000750**
751** An unconditional jump to address P2.
752** The next instruction executed will be
753** the one at index P2 from the beginning of
754** the program.
755*/
drh9cbf3422008-01-17 16:22:13 +0000756case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000757 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000758 pc = pOp->p2 - 1;
759 break;
760}
drh75897232000-05-29 14:26:00 +0000761
drh2eb95372008-06-06 15:04:36 +0000762/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000763**
drh2eb95372008-06-06 15:04:36 +0000764** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000765** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000766*/
drh93952eb2009-11-13 19:43:43 +0000767case OP_Gosub: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +0000768 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000769 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000770 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000771 pIn1->flags = MEM_Int;
772 pIn1->u.i = pc;
773 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000774 pc = pOp->p2 - 1;
775 break;
776}
777
drh2eb95372008-06-06 15:04:36 +0000778/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000779**
drh2eb95372008-06-06 15:04:36 +0000780** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000781*/
drh2eb95372008-06-06 15:04:36 +0000782case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000783 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000784 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000785 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000786 break;
787}
788
drhe00ee6e2008-06-20 15:24:01 +0000789/* Opcode: Yield P1 * * * *
790**
791** Swap the program counter with the value in register P1.
792*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000793case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000794 int pcDest;
drh3c657212009-11-17 23:59:58 +0000795 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000796 assert( (pIn1->flags & MEM_Dyn)==0 );
797 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000798 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000799 pIn1->u.i = pc;
800 REGISTER_TRACE(pOp->p1, pIn1);
801 pc = pcDest;
802 break;
803}
804
drh5053a792009-02-20 03:02:23 +0000805/* Opcode: HaltIfNull P1 P2 P3 P4 *
806**
drhef8662b2011-06-20 21:47:58 +0000807** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000808** parameter P1, P2, and P4 as if this were a Halt instruction. If the
809** value in register P3 is not NULL, then this routine is a no-op.
810*/
811case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000812 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000813 if( (pIn3->flags & MEM_Null)==0 ) break;
814 /* Fall through into OP_Halt */
815}
drhe00ee6e2008-06-20 15:24:01 +0000816
drh9cbf3422008-01-17 16:22:13 +0000817/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000818**
drh3d4501e2008-12-04 20:40:10 +0000819** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000820** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000821**
drh92f02c32004-09-02 14:57:08 +0000822** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
823** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
824** For errors, it can be some other value. If P1!=0 then P2 will determine
825** whether or not to rollback the current transaction. Do not rollback
826** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
827** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000828** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000829**
drh66a51672008-01-03 00:01:23 +0000830** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000831**
drh9cfcf5d2002-01-29 18:41:24 +0000832** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000833** every program. So a jump past the last instruction of the program
834** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000835*/
drh9cbf3422008-01-17 16:22:13 +0000836case OP_Halt: {
dan165921a2009-08-28 18:53:45 +0000837 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000838 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000839 VdbeFrame *pFrame = p->pFrame;
840 p->pFrame = pFrame->pParent;
841 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000842 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000843 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000844 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000845 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000846 /* Instruction pc is the OP_Program that invoked the sub-program
847 ** currently being halted. If the p2 instruction of this OP_Halt
848 ** instruction is set to OE_Ignore, then the sub-program is throwing
849 ** an IGNORE exception. In this case jump to the address specified
850 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000851 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000852 }
drhbbe879d2009-11-14 18:04:35 +0000853 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000854 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000855 break;
856 }
dan2832ad42009-08-31 15:27:27 +0000857
drh92f02c32004-09-02 14:57:08 +0000858 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000859 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000860 p->pc = pc;
danielk19772dca4ac2008-01-03 11:50:29 +0000861 if( pOp->p4.z ){
drh413c3d32010-02-23 20:11:56 +0000862 assert( p->rc!=SQLITE_OK );
drhf089aa42008-07-08 19:34:06 +0000863 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhaf46dc12010-02-24 21:44:07 +0000864 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000865 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
drhcda455b2010-02-24 19:23:56 +0000866 }else if( p->rc ){
drhaf46dc12010-02-24 21:44:07 +0000867 testcase( sqlite3GlobalConfig.xLog!=0 );
drhcda455b2010-02-24 19:23:56 +0000868 sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
drh9cfcf5d2002-01-29 18:41:24 +0000869 }
drh92f02c32004-09-02 14:57:08 +0000870 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000871 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000872 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000873 p->rc = rc = SQLITE_BUSY;
874 }else{
dan1da40a32009-09-19 17:00:31 +0000875 assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
876 assert( rc==SQLITE_OK || db->nDeferredCons>0 );
drh900b31e2007-08-28 02:27:51 +0000877 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000878 }
drh900b31e2007-08-28 02:27:51 +0000879 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000880}
drhc61053b2000-06-04 12:58:36 +0000881
drh4c583122008-01-04 22:01:03 +0000882/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000883**
drh9cbf3422008-01-17 16:22:13 +0000884** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000885*/
drh4c583122008-01-04 22:01:03 +0000886case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000887 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000888 break;
889}
890
drh4c583122008-01-04 22:01:03 +0000891/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000892**
drh66a51672008-01-03 00:01:23 +0000893** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000894** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000895*/
drh4c583122008-01-04 22:01:03 +0000896case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000897 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000898 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000899 break;
900}
drh4f26d6c2004-05-26 23:25:30 +0000901
drh13573c72010-01-12 17:04:07 +0000902#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000903/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000904**
drh4c583122008-01-04 22:01:03 +0000905** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000906** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000907*/
drh4c583122008-01-04 22:01:03 +0000908case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
909 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000910 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000911 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000912 break;
913}
drh13573c72010-01-12 17:04:07 +0000914#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000915
drh3c84ddf2008-01-09 02:15:38 +0000916/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000917**
drh66a51672008-01-03 00:01:23 +0000918** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000919** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000920*/
drh4c583122008-01-04 22:01:03 +0000921case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000922 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000923 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000924 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000925
926#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000927 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000928 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
929 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000930 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000931 assert( pOut->zMalloc==pOut->z );
932 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000933 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000934 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000935 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000936 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000937 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000938 }
drh66a51672008-01-03 00:01:23 +0000939 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000940 pOp->p4.z = pOut->z;
941 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000942 }
danielk197793758c82005-01-21 08:13:14 +0000943#endif
drhbb4957f2008-03-20 14:03:29 +0000944 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000945 goto too_big;
946 }
947 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000948}
drhf4479502004-05-27 03:12:53 +0000949
drh4c583122008-01-04 22:01:03 +0000950/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000951**
drh9cbf3422008-01-17 16:22:13 +0000952** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000953*/
drh4c583122008-01-04 22:01:03 +0000954case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000955 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000956 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
957 pOut->z = pOp->p4.z;
958 pOut->n = pOp->p1;
959 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000960 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000961 break;
962}
963
drh4c583122008-01-04 22:01:03 +0000964/* Opcode: Null * P2 * * *
drhf0863fe2005-06-12 21:35:51 +0000965**
drh9cbf3422008-01-17 16:22:13 +0000966** Write a NULL into register P2.
drhf0863fe2005-06-12 21:35:51 +0000967*/
drh4c583122008-01-04 22:01:03 +0000968case OP_Null: { /* out2-prerelease */
drh3c657212009-11-17 23:59:58 +0000969 pOut->flags = MEM_Null;
drhf0863fe2005-06-12 21:35:51 +0000970 break;
971}
972
973
drh9de221d2008-01-05 06:51:30 +0000974/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000975**
drh9de221d2008-01-05 06:51:30 +0000976** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +0000977** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +0000978*/
drh4c583122008-01-04 22:01:03 +0000979case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000980 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000981 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000982 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000983 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000984 break;
985}
986
drheaf52d82010-05-12 13:50:23 +0000987/* Opcode: Variable P1 P2 * P4 *
drh50457892003-09-06 01:10:47 +0000988**
drheaf52d82010-05-12 13:50:23 +0000989** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +0000990**
991** If the parameter is named, then its name appears in P4 and P3==1.
992** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +0000993*/
drheaf52d82010-05-12 13:50:23 +0000994case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +0000995 Mem *pVar; /* Value being transferred */
996
drheaf52d82010-05-12 13:50:23 +0000997 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +0000998 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +0000999 pVar = &p->aVar[pOp->p1 - 1];
1000 if( sqlite3VdbeMemTooBig(pVar) ){
1001 goto too_big;
drh023ae032007-05-08 12:12:16 +00001002 }
drheaf52d82010-05-12 13:50:23 +00001003 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1004 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001005 break;
1006}
danielk1977295ba552004-05-19 10:34:51 +00001007
drhb21e7c72008-06-22 12:37:57 +00001008/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001009**
drhb21e7c72008-06-22 12:37:57 +00001010** Move the values in register P1..P1+P3-1 over into
1011** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
1012** left holding a NULL. It is an error for register ranges
1013** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001014*/
drhe1349cb2008-04-01 00:36:10 +00001015case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001016 char *zMalloc; /* Holding variable for allocated memory */
1017 int n; /* Number of registers left to copy */
1018 int p1; /* Register to copy from */
1019 int p2; /* Register to copy to */
1020
1021 n = pOp->p3;
1022 p1 = pOp->p1;
1023 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001024 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001025 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001026
drha6c2ed92009-11-14 23:22:23 +00001027 pIn1 = &aMem[p1];
1028 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001029 while( n-- ){
drha6c2ed92009-11-14 23:22:23 +00001030 assert( pOut<=&aMem[p->nMem] );
1031 assert( pIn1<=&aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00001032 assert( memIsValid(pIn1) );
1033 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001034 zMalloc = pOut->zMalloc;
1035 pOut->zMalloc = 0;
1036 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001037#ifdef SQLITE_DEBUG
1038 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1039 pOut->pScopyFrom += p1 - pOp->p2;
1040 }
1041#endif
drhb21e7c72008-06-22 12:37:57 +00001042 pIn1->zMalloc = zMalloc;
1043 REGISTER_TRACE(p2++, pOut);
1044 pIn1++;
1045 pOut++;
1046 }
drhe1349cb2008-04-01 00:36:10 +00001047 break;
1048}
1049
drhb1fdb2a2008-01-05 04:06:03 +00001050/* Opcode: Copy P1 P2 * * *
1051**
drh9cbf3422008-01-17 16:22:13 +00001052** Make a copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001053**
1054** This instruction makes a deep copy of the value. A duplicate
1055** is made of any string or blob constant. See also OP_SCopy.
1056*/
drh93952eb2009-11-13 19:43:43 +00001057case OP_Copy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001058 pIn1 = &aMem[pOp->p1];
1059 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001060 assert( pOut!=pIn1 );
1061 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1062 Deephemeralize(pOut);
1063 REGISTER_TRACE(pOp->p2, pOut);
1064 break;
1065}
1066
drhb1fdb2a2008-01-05 04:06:03 +00001067/* Opcode: SCopy P1 P2 * * *
1068**
drh9cbf3422008-01-17 16:22:13 +00001069** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001070**
1071** This instruction makes a shallow copy of the value. If the value
1072** is a string or blob, then the copy is only a pointer to the
1073** original and hence if the original changes so will the copy.
1074** Worse, if the original is deallocated, the copy becomes invalid.
1075** Thus the program must guarantee that the original will not change
1076** during the lifetime of the copy. Use OP_Copy to make a complete
1077** copy.
1078*/
drh93952eb2009-11-13 19:43:43 +00001079case OP_SCopy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001080 pIn1 = &aMem[pOp->p1];
1081 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001082 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001083 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001084#ifdef SQLITE_DEBUG
1085 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1086#endif
drh5b6afba2008-01-05 16:29:28 +00001087 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001088 break;
1089}
drh75897232000-05-29 14:26:00 +00001090
drh9cbf3422008-01-17 16:22:13 +00001091/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001092**
shane21e7feb2008-05-30 15:59:49 +00001093** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001094** results. This opcode causes the sqlite3_step() call to terminate
1095** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1096** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001097** row.
drhd4e70eb2008-01-02 00:34:36 +00001098*/
drh9cbf3422008-01-17 16:22:13 +00001099case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001100 Mem *pMem;
1101 int i;
1102 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001103 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001104 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001105
dan32b09f22009-09-23 17:29:59 +00001106 /* If this statement has violated immediate foreign key constraints, do
1107 ** not return the number of rows modified. And do not RELEASE the statement
1108 ** transaction. It needs to be rolled back. */
1109 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1110 assert( db->flags&SQLITE_CountRows );
1111 assert( p->usesStmtJournal );
1112 break;
1113 }
1114
danielk1977bd434552009-03-18 10:33:00 +00001115 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1116 ** DML statements invoke this opcode to return the number of rows
1117 ** modified to the user. This is the only way that a VM that
1118 ** opens a statement transaction may invoke this opcode.
1119 **
1120 ** In case this is such a statement, close any statement transaction
1121 ** opened by this VM before returning control to the user. This is to
1122 ** ensure that statement-transactions are always nested, not overlapping.
1123 ** If the open statement-transaction is not closed here, then the user
1124 ** may step another VM that opens its own statement transaction. This
1125 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001126 **
1127 ** The statement transaction is never a top-level transaction. Hence
1128 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001129 */
1130 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001131 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1132 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001133 break;
1134 }
1135
drhd4e70eb2008-01-02 00:34:36 +00001136 /* Invalidate all ephemeral cursor row caches */
1137 p->cacheCtr = (p->cacheCtr + 2)|1;
1138
1139 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001140 ** and have an assigned type. The results are de-ephemeralized as
drhd4e70eb2008-01-02 00:34:36 +00001141 ** as side effect.
1142 */
drha6c2ed92009-11-14 23:22:23 +00001143 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001144 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001145 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001146 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001147 assert( (pMem[i].flags & MEM_Ephem)==0
1148 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001149 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001150 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001151 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001152 }
drh28039692008-03-17 16:54:01 +00001153 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001154
1155 /* Return SQLITE_ROW
1156 */
drhd4e70eb2008-01-02 00:34:36 +00001157 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001158 rc = SQLITE_ROW;
1159 goto vdbe_return;
1160}
1161
drh5b6afba2008-01-05 16:29:28 +00001162/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001163**
drh5b6afba2008-01-05 16:29:28 +00001164** Add the text in register P1 onto the end of the text in
1165** register P2 and store the result in register P3.
1166** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001167**
1168** P3 = P2 || P1
1169**
1170** It is illegal for P1 and P3 to be the same register. Sometimes,
1171** if P3 is the same register as P2, the implementation is able
1172** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001173*/
drh5b6afba2008-01-05 16:29:28 +00001174case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001175 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001176
drh3c657212009-11-17 23:59:58 +00001177 pIn1 = &aMem[pOp->p1];
1178 pIn2 = &aMem[pOp->p2];
1179 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001180 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001181 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001182 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001183 break;
drh5e00f6c2001-09-13 13:46:56 +00001184 }
drha0c06522009-06-17 22:50:41 +00001185 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001186 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001187 Stringify(pIn2, encoding);
1188 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001189 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001190 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001191 }
danielk1977a7a8e142008-02-13 18:25:27 +00001192 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001193 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001194 goto no_mem;
1195 }
danielk1977a7a8e142008-02-13 18:25:27 +00001196 if( pOut!=pIn2 ){
1197 memcpy(pOut->z, pIn2->z, pIn2->n);
1198 }
1199 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1200 pOut->z[nByte] = 0;
1201 pOut->z[nByte+1] = 0;
1202 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001203 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001204 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001205 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001206 break;
1207}
drh75897232000-05-29 14:26:00 +00001208
drh3c84ddf2008-01-09 02:15:38 +00001209/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001210**
drh60a713c2008-01-21 16:22:45 +00001211** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001212** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001213** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001214*/
drh3c84ddf2008-01-09 02:15:38 +00001215/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001216**
drh3c84ddf2008-01-09 02:15:38 +00001217**
shane21e7feb2008-05-30 15:59:49 +00001218** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001219** and store the result in register P3.
1220** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001221*/
drh3c84ddf2008-01-09 02:15:38 +00001222/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001223**
drh60a713c2008-01-21 16:22:45 +00001224** Subtract the value in register P1 from the value in register P2
1225** and store the result in register P3.
1226** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001227*/
drh9cbf3422008-01-17 16:22:13 +00001228/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001229**
drh60a713c2008-01-21 16:22:45 +00001230** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001231** and store the result in register P3 (P3=P2/P1). If the value in
1232** register P1 is zero, then the result is NULL. If either input is
1233** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001234*/
drh9cbf3422008-01-17 16:22:13 +00001235/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001236**
drh3c84ddf2008-01-09 02:15:38 +00001237** Compute the remainder after integer division of the value in
1238** register P1 by the value in register P2 and store the result in P3.
1239** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001240** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001241*/
drh5b6afba2008-01-05 16:29:28 +00001242case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1243case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1244case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1245case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1246case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001247 int flags; /* Combined MEM_* flags from both inputs */
1248 i64 iA; /* Integer value of left operand */
1249 i64 iB; /* Integer value of right operand */
1250 double rA; /* Real value of left operand */
1251 double rB; /* Real value of right operand */
1252
drh3c657212009-11-17 23:59:58 +00001253 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001254 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001255 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001256 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001257 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001258 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001259 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1260 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001261 iA = pIn1->u.i;
1262 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001263 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001264 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1265 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1266 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001267 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001268 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001269 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001270 iB /= iA;
drh75897232000-05-29 14:26:00 +00001271 break;
1272 }
drhbf4133c2001-10-13 02:59:08 +00001273 default: {
drh856c1032009-06-02 15:21:42 +00001274 if( iA==0 ) goto arithmetic_result_is_null;
1275 if( iA==-1 ) iA = 1;
1276 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001277 break;
1278 }
drh75897232000-05-29 14:26:00 +00001279 }
drh856c1032009-06-02 15:21:42 +00001280 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001281 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001282 }else{
drh158b9cb2011-03-05 20:59:46 +00001283fp_math:
drh856c1032009-06-02 15:21:42 +00001284 rA = sqlite3VdbeRealValue(pIn1);
1285 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001286 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001287 case OP_Add: rB += rA; break;
1288 case OP_Subtract: rB -= rA; break;
1289 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001290 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001291 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001292 if( rA==(double)0 ) goto arithmetic_result_is_null;
1293 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001294 break;
1295 }
drhbf4133c2001-10-13 02:59:08 +00001296 default: {
shane75ac1de2009-06-09 18:58:52 +00001297 iA = (i64)rA;
1298 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001299 if( iA==0 ) goto arithmetic_result_is_null;
1300 if( iA==-1 ) iA = 1;
1301 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001302 break;
1303 }
drh5e00f6c2001-09-13 13:46:56 +00001304 }
drhc5a7b512010-01-13 16:25:42 +00001305#ifdef SQLITE_OMIT_FLOATING_POINT
1306 pOut->u.i = rB;
1307 MemSetTypeFlag(pOut, MEM_Int);
1308#else
drh856c1032009-06-02 15:21:42 +00001309 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001310 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001311 }
drh856c1032009-06-02 15:21:42 +00001312 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001313 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001314 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001315 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001316 }
drhc5a7b512010-01-13 16:25:42 +00001317#endif
drh5e00f6c2001-09-13 13:46:56 +00001318 }
1319 break;
1320
drha05a7222008-01-19 03:35:58 +00001321arithmetic_result_is_null:
1322 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001323 break;
1324}
1325
drh66a51672008-01-03 00:01:23 +00001326/* Opcode: CollSeq * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001327**
drh66a51672008-01-03 00:01:23 +00001328** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001329** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1330** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001331** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001332**
1333** The interface used by the implementation of the aforementioned functions
1334** to retrieve the collation sequence set by this opcode is not available
1335** publicly, only to user functions defined in func.c.
1336*/
drh9cbf3422008-01-17 16:22:13 +00001337case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001338 assert( pOp->p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001339 break;
1340}
1341
drh98757152008-01-09 23:04:12 +00001342/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001343**
drh66a51672008-01-03 00:01:23 +00001344** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001345** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001346** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001347** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001348**
drh13449892005-09-07 21:22:45 +00001349** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001350** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001351** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001352** whether meta data associated with a user function argument using the
1353** sqlite3_set_auxdata() API may be safely retained until the next
1354** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001355**
drh13449892005-09-07 21:22:45 +00001356** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001357*/
drh0bce8352002-02-28 00:41:10 +00001358case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001359 int i;
drh6810ce62004-01-31 19:22:56 +00001360 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001361 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001362 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001363 int n;
drh1350b032002-02-27 19:00:20 +00001364
drh856c1032009-06-02 15:21:42 +00001365 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001366 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001367 assert( apVal || n==0 );
drhebc16712010-09-28 00:25:58 +00001368 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1369 pOut = &aMem[pOp->p3];
1370 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001371
danielk19776ab3a2e2009-02-19 14:39:25 +00001372 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001373 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001374 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001375 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001376 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001377 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001378 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001379 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001380 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001381 }
danielk197751ad0ec2004-05-24 12:39:02 +00001382
drh66a51672008-01-03 00:01:23 +00001383 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1384 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001385 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001386 ctx.pVdbeFunc = 0;
1387 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001388 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001389 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1390 }
1391
drh00706be2004-01-30 14:49:16 +00001392 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001393 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001394 ctx.s.xDel = 0;
1395 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001396
1397 /* The output cell may already have a buffer allocated. Move
1398 ** the pointer to ctx.s so in case the user-function can use
1399 ** the already allocated buffer instead of allocating a new one.
1400 */
1401 sqlite3VdbeMemMove(&ctx.s, pOut);
1402 MemSetTypeFlag(&ctx.s, MEM_Null);
1403
drh8e0a2f92002-02-23 23:45:45 +00001404 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001405 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001406 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001407 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001408 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001409 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001410 }
drh99a66922011-05-13 18:51:42 +00001411 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001412 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001413 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001414
shane21e7feb2008-05-30 15:59:49 +00001415 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001416 ** immediately call the destructor for any non-static values.
1417 */
1418 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001419 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001420 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001421 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001422 }
1423
dan5f84e142011-06-14 14:18:45 +00001424 if( db->mallocFailed ){
1425 /* Even though a malloc() has failed, the implementation of the
1426 ** user function may have called an sqlite3_result_XXX() function
1427 ** to return a value. The following call releases any resources
1428 ** associated with such a value.
1429 */
1430 sqlite3VdbeMemRelease(&ctx.s);
1431 goto no_mem;
1432 }
1433
drh90669c12006-01-20 15:45:36 +00001434 /* If the function returned an error, throw an exception */
1435 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001436 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001437 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001438 }
1439
drh9cbf3422008-01-17 16:22:13 +00001440 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001441 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001442 sqlite3VdbeMemMove(pOut, &ctx.s);
1443 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001444 goto too_big;
1445 }
drh7b94e7f2011-04-04 12:29:20 +00001446
1447#if 0
1448 /* The app-defined function has done something that as caused this
1449 ** statement to expire. (Perhaps the function called sqlite3_exec()
1450 ** with a CREATE TABLE statement.)
1451 */
1452 if( p->expired ) rc = SQLITE_ABORT;
1453#endif
1454
drh2dcef112008-01-12 19:03:48 +00001455 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001456 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001457 break;
1458}
1459
drh98757152008-01-09 23:04:12 +00001460/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001461**
drh98757152008-01-09 23:04:12 +00001462** Take the bit-wise AND of the values in register P1 and P2 and
1463** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001464** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001465*/
drh98757152008-01-09 23:04:12 +00001466/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001467**
drh98757152008-01-09 23:04:12 +00001468** Take the bit-wise OR of the values in register P1 and P2 and
1469** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001470** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001471*/
drh98757152008-01-09 23:04:12 +00001472/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001473**
drh98757152008-01-09 23:04:12 +00001474** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001475** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001476** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001477** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001478*/
drh98757152008-01-09 23:04:12 +00001479/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001480**
drh98757152008-01-09 23:04:12 +00001481** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001482** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001483** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001484** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001485*/
drh5b6afba2008-01-05 16:29:28 +00001486case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1487case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1488case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1489case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001490 i64 iA;
1491 u64 uA;
1492 i64 iB;
1493 u8 op;
drh6810ce62004-01-31 19:22:56 +00001494
drh3c657212009-11-17 23:59:58 +00001495 pIn1 = &aMem[pOp->p1];
1496 pIn2 = &aMem[pOp->p2];
1497 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001498 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001499 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001500 break;
1501 }
drh158b9cb2011-03-05 20:59:46 +00001502 iA = sqlite3VdbeIntValue(pIn2);
1503 iB = sqlite3VdbeIntValue(pIn1);
1504 op = pOp->opcode;
1505 if( op==OP_BitAnd ){
1506 iA &= iB;
1507 }else if( op==OP_BitOr ){
1508 iA |= iB;
1509 }else if( iB!=0 ){
1510 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1511
1512 /* If shifting by a negative amount, shift in the other direction */
1513 if( iB<0 ){
1514 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1515 op = 2*OP_ShiftLeft + 1 - op;
1516 iB = iB>(-64) ? -iB : 64;
1517 }
1518
1519 if( iB>=64 ){
1520 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1521 }else{
1522 memcpy(&uA, &iA, sizeof(uA));
1523 if( op==OP_ShiftLeft ){
1524 uA <<= iB;
1525 }else{
1526 uA >>= iB;
1527 /* Sign-extend on a right shift of a negative number */
1528 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1529 }
1530 memcpy(&iA, &uA, sizeof(iA));
1531 }
drhbf4133c2001-10-13 02:59:08 +00001532 }
drh158b9cb2011-03-05 20:59:46 +00001533 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001534 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001535 break;
1536}
1537
drh8558cde2008-01-05 05:20:10 +00001538/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001539**
danielk19770cdc0222008-06-26 18:04:03 +00001540** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001541** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001542**
drh8558cde2008-01-05 05:20:10 +00001543** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001544*/
drh9cbf3422008-01-17 16:22:13 +00001545case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001546 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001547 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001548 sqlite3VdbeMemIntegerify(pIn1);
1549 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001550 break;
1551}
1552
drh9cbf3422008-01-17 16:22:13 +00001553/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001554**
drh9cbf3422008-01-17 16:22:13 +00001555** Force the value in register P1 to be an integer. If the value
1556** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001557** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001558** raise an SQLITE_MISMATCH exception.
1559*/
drh9cbf3422008-01-17 16:22:13 +00001560case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001561 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001562 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1563 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001564 if( pOp->p2==0 ){
1565 rc = SQLITE_MISMATCH;
1566 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001567 }else{
drh17c40292004-07-21 02:53:29 +00001568 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001569 }
drh8aff1012001-12-22 14:49:24 +00001570 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001571 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001572 }
1573 break;
1574}
1575
drh13573c72010-01-12 17:04:07 +00001576#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001577/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001578**
drh2133d822008-01-03 18:44:59 +00001579** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001580**
drh8a512562005-11-14 22:29:05 +00001581** This opcode is used when extracting information from a column that
1582** has REAL affinity. Such column values may still be stored as
1583** integers, for space efficiency, but after extraction we want them
1584** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001585*/
drh9cbf3422008-01-17 16:22:13 +00001586case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001587 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001588 if( pIn1->flags & MEM_Int ){
1589 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001590 }
drh487e2622005-06-25 18:42:14 +00001591 break;
1592}
drh13573c72010-01-12 17:04:07 +00001593#endif
drh487e2622005-06-25 18:42:14 +00001594
drh8df447f2005-11-01 15:48:24 +00001595#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001596/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001597**
drh8558cde2008-01-05 05:20:10 +00001598** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001599** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001600** equivalent of printf(). Blob values are unchanged and
1601** are afterwards simply interpreted as text.
1602**
1603** A NULL value is not changed by this routine. It remains NULL.
1604*/
drh9cbf3422008-01-17 16:22:13 +00001605case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001606 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001607 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001608 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001609 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001610 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1611 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1612 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001613 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001614 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001615 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001616 break;
1617}
1618
drh8558cde2008-01-05 05:20:10 +00001619/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001620**
drh8558cde2008-01-05 05:20:10 +00001621** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001622** If the value is numeric, convert it to a string first.
1623** Strings are simply reinterpreted as blobs with no change
1624** to the underlying data.
1625**
1626** A NULL value is not changed by this routine. It remains NULL.
1627*/
drh9cbf3422008-01-17 16:22:13 +00001628case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001629 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001630 if( pIn1->flags & MEM_Null ) break;
1631 if( (pIn1->flags & MEM_Blob)==0 ){
1632 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001633 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001634 MemSetTypeFlag(pIn1, MEM_Blob);
1635 }else{
1636 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001637 }
drhb7654112008-01-12 12:48:07 +00001638 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001639 break;
1640}
drh8a512562005-11-14 22:29:05 +00001641
drh8558cde2008-01-05 05:20:10 +00001642/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001643**
drh8558cde2008-01-05 05:20:10 +00001644** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001645** integer or a floating-point number.)
1646** If the value is text or blob, try to convert it to an using the
1647** equivalent of atoi() or atof() and store 0 if no such conversion
1648** is possible.
1649**
1650** A NULL value is not changed by this routine. It remains NULL.
1651*/
drh9cbf3422008-01-17 16:22:13 +00001652case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001653 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001654 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001655 break;
1656}
1657#endif /* SQLITE_OMIT_CAST */
1658
drh8558cde2008-01-05 05:20:10 +00001659/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001660**
drh710c4842010-08-30 01:17:20 +00001661** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001662** The value is currently a real number, drop its fractional part.
1663** If the value is text or blob, try to convert it to an integer using the
1664** equivalent of atoi() and store 0 if no such conversion is possible.
1665**
1666** A NULL value is not changed by this routine. It remains NULL.
1667*/
drh9cbf3422008-01-17 16:22:13 +00001668case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001669 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001670 if( (pIn1->flags & MEM_Null)==0 ){
1671 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001672 }
1673 break;
1674}
1675
drh13573c72010-01-12 17:04:07 +00001676#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001677/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001678**
drh8558cde2008-01-05 05:20:10 +00001679** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001680** If The value is currently an integer, convert it.
1681** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001682** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001683**
1684** A NULL value is not changed by this routine. It remains NULL.
1685*/
drh9cbf3422008-01-17 16:22:13 +00001686case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001687 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001688 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001689 if( (pIn1->flags & MEM_Null)==0 ){
1690 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001691 }
1692 break;
1693}
drh13573c72010-01-12 17:04:07 +00001694#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001695
drh35573352008-01-08 23:54:25 +00001696/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001697**
drh35573352008-01-08 23:54:25 +00001698** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1699** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001700**
drh35573352008-01-08 23:54:25 +00001701** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1702** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001703** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001704**
drh35573352008-01-08 23:54:25 +00001705** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001706** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001707** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001708** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001709** affinity is used. Note that the affinity conversions are stored
1710** back into the input registers P1 and P3. So this opcode can cause
1711** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001712**
1713** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001714** the values are compared. If both values are blobs then memcmp() is
1715** used to determine the results of the comparison. If both values
1716** are text, then the appropriate collating function specified in
1717** P4 is used to do the comparison. If P4 is not specified then
1718** memcmp() is used to compare text string. If both values are
1719** numeric, then a numeric comparison is used. If the two values
1720** are of different types, then numbers are considered less than
1721** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001722**
drh35573352008-01-08 23:54:25 +00001723** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1724** store a boolean result (either 0, or 1, or NULL) in register P2.
drh5e00f6c2001-09-13 13:46:56 +00001725*/
drh9cbf3422008-01-17 16:22:13 +00001726/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001727**
drh35573352008-01-08 23:54:25 +00001728** This works just like the Lt opcode except that the jump is taken if
1729** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001730** additional information.
drh6a2fe092009-09-23 02:29:36 +00001731**
1732** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1733** true or false and is never NULL. If both operands are NULL then the result
1734** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001735** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001736** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001737*/
drh9cbf3422008-01-17 16:22:13 +00001738/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001739**
drh35573352008-01-08 23:54:25 +00001740** This works just like the Lt opcode except that the jump is taken if
1741** the operands in registers P1 and P3 are equal.
1742** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001743**
1744** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1745** true or false and is never NULL. If both operands are NULL then the result
1746** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001747** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001748** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001749*/
drh9cbf3422008-01-17 16:22:13 +00001750/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001751**
drh35573352008-01-08 23:54:25 +00001752** This works just like the Lt opcode except that the jump is taken if
1753** the content of register P3 is less than or equal to the content of
1754** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001755*/
drh9cbf3422008-01-17 16:22:13 +00001756/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001757**
drh35573352008-01-08 23:54:25 +00001758** This works just like the Lt opcode except that the jump is taken if
1759** the content of register P3 is greater than the content of
1760** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001761*/
drh9cbf3422008-01-17 16:22:13 +00001762/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001763**
drh35573352008-01-08 23:54:25 +00001764** This works just like the Lt opcode except that the jump is taken if
1765** the content of register P3 is greater than or equal to the content of
1766** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001767*/
drh9cbf3422008-01-17 16:22:13 +00001768case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1769case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1770case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1771case OP_Le: /* same as TK_LE, jump, in1, in3 */
1772case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1773case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001774 int res; /* Result of the comparison of pIn1 against pIn3 */
1775 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001776 u16 flags1; /* Copy of initial value of pIn1->flags */
1777 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001778
drh3c657212009-11-17 23:59:58 +00001779 pIn1 = &aMem[pOp->p1];
1780 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001781 flags1 = pIn1->flags;
1782 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001783 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001784 /* One or both operands are NULL */
1785 if( pOp->p5 & SQLITE_NULLEQ ){
1786 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1787 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1788 ** or not both operands are null.
1789 */
1790 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drhc3f1d5f2011-05-30 23:42:16 +00001791 res = (flags1 & flags3 & MEM_Null)==0;
drh6a2fe092009-09-23 02:29:36 +00001792 }else{
1793 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1794 ** then the result is always NULL.
1795 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1796 */
1797 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001798 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001799 MemSetTypeFlag(pOut, MEM_Null);
1800 REGISTER_TRACE(pOp->p2, pOut);
1801 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1802 pc = pOp->p2-1;
1803 }
1804 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001805 }
drh6a2fe092009-09-23 02:29:36 +00001806 }else{
1807 /* Neither operand is NULL. Do a comparison. */
1808 affinity = pOp->p5 & SQLITE_AFF_MASK;
1809 if( affinity ){
1810 applyAffinity(pIn1, affinity, encoding);
1811 applyAffinity(pIn3, affinity, encoding);
1812 if( db->mallocFailed ) goto no_mem;
1813 }
danielk1977a37cdde2004-05-16 11:15:36 +00001814
drh6a2fe092009-09-23 02:29:36 +00001815 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1816 ExpandBlob(pIn1);
1817 ExpandBlob(pIn3);
1818 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001819 }
danielk1977a37cdde2004-05-16 11:15:36 +00001820 switch( pOp->opcode ){
1821 case OP_Eq: res = res==0; break;
1822 case OP_Ne: res = res!=0; break;
1823 case OP_Lt: res = res<0; break;
1824 case OP_Le: res = res<=0; break;
1825 case OP_Gt: res = res>0; break;
1826 default: res = res>=0; break;
1827 }
1828
drh35573352008-01-08 23:54:25 +00001829 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001830 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001831 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001832 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001833 pOut->u.i = res;
1834 REGISTER_TRACE(pOp->p2, pOut);
1835 }else if( res ){
1836 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001837 }
danb7dca7d2010-03-05 16:32:12 +00001838
1839 /* Undo any changes made by applyAffinity() to the input registers. */
1840 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1841 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001842 break;
1843}
drhc9b84a12002-06-20 11:36:48 +00001844
drh0acb7e42008-06-25 00:12:41 +00001845/* Opcode: Permutation * * * P4 *
1846**
shanebe217792009-03-05 04:20:31 +00001847** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001848** of integers in P4.
1849**
1850** The permutation is only valid until the next OP_Permutation, OP_Compare,
1851** OP_Halt, or OP_ResultRow. Typically the OP_Permutation should occur
1852** immediately prior to the OP_Compare.
1853*/
1854case OP_Permutation: {
1855 assert( pOp->p4type==P4_INTARRAY );
1856 assert( pOp->p4.ai );
1857 aPermute = pOp->p4.ai;
1858 break;
1859}
1860
drh16ee60f2008-06-20 18:13:25 +00001861/* Opcode: Compare P1 P2 P3 P4 *
1862**
drh710c4842010-08-30 01:17:20 +00001863** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1864** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001865** the comparison for use by the next OP_Jump instruct.
1866**
drh0acb7e42008-06-25 00:12:41 +00001867** P4 is a KeyInfo structure that defines collating sequences and sort
1868** orders for the comparison. The permutation applies to registers
1869** only. The KeyInfo elements are used sequentially.
1870**
1871** The comparison is a sort comparison, so NULLs compare equal,
1872** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001873** and strings are less than blobs.
1874*/
1875case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001876 int n;
1877 int i;
1878 int p1;
1879 int p2;
1880 const KeyInfo *pKeyInfo;
1881 int idx;
1882 CollSeq *pColl; /* Collating sequence to use on this term */
1883 int bRev; /* True for DESCENDING sort order */
1884
1885 n = pOp->p3;
1886 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001887 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001888 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001889 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001890 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00001891#if SQLITE_DEBUG
1892 if( aPermute ){
1893 int k, mx = 0;
1894 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
1895 assert( p1>0 && p1+mx<=p->nMem+1 );
1896 assert( p2>0 && p2+mx<=p->nMem+1 );
1897 }else{
1898 assert( p1>0 && p1+n<=p->nMem+1 );
1899 assert( p2>0 && p2+n<=p->nMem+1 );
1900 }
1901#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00001902 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001903 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00001904 assert( memIsValid(&aMem[p1+idx]) );
1905 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00001906 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
1907 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001908 assert( i<pKeyInfo->nField );
1909 pColl = pKeyInfo->aColl[i];
1910 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00001911 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00001912 if( iCompare ){
1913 if( bRev ) iCompare = -iCompare;
1914 break;
1915 }
drh16ee60f2008-06-20 18:13:25 +00001916 }
drh0acb7e42008-06-25 00:12:41 +00001917 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001918 break;
1919}
1920
1921/* Opcode: Jump P1 P2 P3 * *
1922**
1923** Jump to the instruction at address P1, P2, or P3 depending on whether
1924** in the most recent OP_Compare instruction the P1 vector was less than
1925** equal to, or greater than the P2 vector, respectively.
1926*/
drh0acb7e42008-06-25 00:12:41 +00001927case OP_Jump: { /* jump */
1928 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001929 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001930 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001931 pc = pOp->p2 - 1;
1932 }else{
1933 pc = pOp->p3 - 1;
1934 }
1935 break;
1936}
1937
drh5b6afba2008-01-05 16:29:28 +00001938/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001939**
drh5b6afba2008-01-05 16:29:28 +00001940** Take the logical AND of the values in registers P1 and P2 and
1941** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001942**
drh5b6afba2008-01-05 16:29:28 +00001943** If either P1 or P2 is 0 (false) then the result is 0 even if
1944** the other input is NULL. A NULL and true or two NULLs give
1945** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001946*/
drh5b6afba2008-01-05 16:29:28 +00001947/* Opcode: Or P1 P2 P3 * *
1948**
1949** Take the logical OR of the values in register P1 and P2 and
1950** store the answer in register P3.
1951**
1952** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1953** even if the other input is NULL. A NULL and false or two NULLs
1954** give a NULL output.
1955*/
1956case OP_And: /* same as TK_AND, in1, in2, out3 */
1957case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001958 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
1959 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00001960
drh3c657212009-11-17 23:59:58 +00001961 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00001962 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001963 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001964 }else{
drh5b6afba2008-01-05 16:29:28 +00001965 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00001966 }
drh3c657212009-11-17 23:59:58 +00001967 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00001968 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001969 v2 = 2;
1970 }else{
drh5b6afba2008-01-05 16:29:28 +00001971 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00001972 }
1973 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00001974 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00001975 v1 = and_logic[v1*3+v2];
1976 }else{
drh5b6afba2008-01-05 16:29:28 +00001977 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00001978 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001979 }
drh3c657212009-11-17 23:59:58 +00001980 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00001981 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00001982 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00001983 }else{
drh5b6afba2008-01-05 16:29:28 +00001984 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00001985 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00001986 }
drh5e00f6c2001-09-13 13:46:56 +00001987 break;
1988}
1989
drhe99fa2a2008-12-15 15:27:51 +00001990/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001991**
drhe99fa2a2008-12-15 15:27:51 +00001992** Interpret the value in register P1 as a boolean value. Store the
1993** boolean complement in register P2. If the value in register P1 is
1994** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00001995*/
drh93952eb2009-11-13 19:43:43 +00001996case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00001997 pIn1 = &aMem[pOp->p1];
1998 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00001999 if( pIn1->flags & MEM_Null ){
2000 sqlite3VdbeMemSetNull(pOut);
2001 }else{
2002 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2003 }
drh5e00f6c2001-09-13 13:46:56 +00002004 break;
2005}
2006
drhe99fa2a2008-12-15 15:27:51 +00002007/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00002008**
drhe99fa2a2008-12-15 15:27:51 +00002009** Interpret the content of register P1 as an integer. Store the
2010** ones-complement of the P1 value into register P2. If P1 holds
2011** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002012*/
drh93952eb2009-11-13 19:43:43 +00002013case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002014 pIn1 = &aMem[pOp->p1];
2015 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002016 if( pIn1->flags & MEM_Null ){
2017 sqlite3VdbeMemSetNull(pOut);
2018 }else{
2019 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2020 }
drhbf4133c2001-10-13 02:59:08 +00002021 break;
2022}
2023
drh48f2d3b2011-09-16 01:34:43 +00002024/* Opcode: Once P1 P2 * * *
2025**
2026** Jump to P2 if the value in register P1 is a not null or zero. If
2027** the value is NULL or zero, fall through and change the P1 register
2028** to an integer 1.
2029**
2030** When P1 is not used otherwise in a program, this opcode falls through
2031** once and jumps on all subsequent invocations. It is the equivalent
2032** of "OP_If P1 P2", followed by "OP_Integer 1 P1".
2033*/
drh3c84ddf2008-01-09 02:15:38 +00002034/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002035**
drhef8662b2011-06-20 21:47:58 +00002036** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002037** is considered true if it is numeric and non-zero. If the value
2038** in P1 is NULL then take the jump if P3 is true.
drh5e00f6c2001-09-13 13:46:56 +00002039*/
drh3c84ddf2008-01-09 02:15:38 +00002040/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002041**
drhef8662b2011-06-20 21:47:58 +00002042** Jump to P2 if the value in register P1 is False. The value
drh3c84ddf2008-01-09 02:15:38 +00002043** is considered true if it has a numeric value of zero. If the value
2044** in P1 is NULL then take the jump if P3 is true.
drhf5905aa2002-05-26 20:54:33 +00002045*/
drh48f2d3b2011-09-16 01:34:43 +00002046case OP_Once: /* jump, in1 */
drh9cbf3422008-01-17 16:22:13 +00002047case OP_If: /* jump, in1 */
2048case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002049 int c;
drh3c657212009-11-17 23:59:58 +00002050 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002051 if( pIn1->flags & MEM_Null ){
2052 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002053 }else{
drhba0232a2005-06-06 17:27:19 +00002054#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002055 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002056#else
drh3c84ddf2008-01-09 02:15:38 +00002057 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002058#endif
drhf5905aa2002-05-26 20:54:33 +00002059 if( pOp->opcode==OP_IfNot ) c = !c;
2060 }
drh3c84ddf2008-01-09 02:15:38 +00002061 if( c ){
2062 pc = pOp->p2-1;
drh48f2d3b2011-09-16 01:34:43 +00002063 }else if( pOp->opcode==OP_Once ){
2064 assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 );
2065 memAboutToChange(p, pIn1);
2066 pIn1->flags = MEM_Int;
2067 pIn1->u.i = 1;
2068 REGISTER_TRACE(pOp->p1, pIn1);
drh3c84ddf2008-01-09 02:15:38 +00002069 }
drh5e00f6c2001-09-13 13:46:56 +00002070 break;
2071}
2072
drh830ecf92009-06-18 00:41:55 +00002073/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00002074**
drh830ecf92009-06-18 00:41:55 +00002075** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002076*/
drh9cbf3422008-01-17 16:22:13 +00002077case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002078 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002079 if( (pIn1->flags & MEM_Null)!=0 ){
2080 pc = pOp->p2 - 1;
2081 }
drh477df4b2008-01-05 18:48:24 +00002082 break;
2083}
2084
drh98757152008-01-09 23:04:12 +00002085/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002086**
drh6a288a32008-01-07 19:20:24 +00002087** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002088*/
drh9cbf3422008-01-17 16:22:13 +00002089case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002090 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002091 if( (pIn1->flags & MEM_Null)==0 ){
2092 pc = pOp->p2 - 1;
2093 }
drh5e00f6c2001-09-13 13:46:56 +00002094 break;
2095}
2096
drh3e9ca092009-09-08 01:14:48 +00002097/* Opcode: Column P1 P2 P3 P4 P5
danielk1977192ac1d2004-05-10 07:17:30 +00002098**
danielk1977cfcdaef2004-05-12 07:33:33 +00002099** Interpret the data that cursor P1 points to as a structure built using
2100** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002101** information about the format of the data.) Extract the P2-th column
2102** from this record. If there are less that (P2+1)
2103** values in the record, extract a NULL.
2104**
drh9cbf3422008-01-17 16:22:13 +00002105** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002106**
danielk19771f4aa332008-01-03 09:51:55 +00002107** If the column contains fewer than P2 fields, then extract a NULL. Or,
2108** if the P4 argument is a P4_MEM use the value of the P4 argument as
2109** the result.
drh3e9ca092009-09-08 01:14:48 +00002110**
2111** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2112** then the cache of the cursor is reset prior to extracting the column.
2113** The first OP_Column against a pseudo-table after the value of the content
2114** register has changed should have this bit set.
danielk1977192ac1d2004-05-10 07:17:30 +00002115*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002116case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002117 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002118 i64 payloadSize64; /* Number of bytes in the record */
2119 int p1; /* P1 value of the opcode */
2120 int p2; /* column number to retrieve */
2121 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002122 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002123 BtCursor *pCrsr; /* The BTree cursor */
2124 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2125 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002126 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002127 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002128 int i; /* Loop counter */
2129 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002130 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002131 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002132 u8 *zIdx; /* Index into header */
2133 u8 *zEndHdr; /* Pointer to first byte after the header */
2134 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002135 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002136 int szHdr; /* Size of the header size field at start of record */
2137 int avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002138 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002139 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002140
drh856c1032009-06-02 15:21:42 +00002141
2142 p1 = pOp->p1;
2143 p2 = pOp->p2;
2144 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002145 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002146 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002147 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002148 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002149 memAboutToChange(p, pDest);
shane36840fd2009-06-26 16:32:13 +00002150 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002151
drhe61cffc2004-06-12 18:12:15 +00002152 /* This block sets the variable payloadSize to be the total number of
2153 ** bytes in the record.
2154 **
2155 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002156 ** The complete record text is always available for pseudo-tables
2157 ** If the record is stored in a cursor, the complete record text
2158 ** might be available in the pC->aRow cache. Or it might not be.
2159 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002160 **
2161 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002162 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002163 */
drhb73857f2006-03-17 00:25:59 +00002164 pC = p->apCsr[p1];
danielk19776c924092007-11-12 08:09:34 +00002165 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002166#ifndef SQLITE_OMIT_VIRTUALTABLE
2167 assert( pC->pVtabCursor==0 );
2168#endif
shane36840fd2009-06-26 16:32:13 +00002169 pCrsr = pC->pCursor;
2170 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002171 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002172 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002173 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002174 if( pC->nullRow ){
2175 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002176 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002177 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002178 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002179 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002180 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002181 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drhc27ae612009-07-14 18:35:44 +00002182 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002183 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2184 ** payload size, so it is impossible for payloadSize64 to be
2185 ** larger than 32 bits. */
2186 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002187 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002188 }else{
drhea8ffdf2009-07-22 00:35:23 +00002189 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002190 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002191 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002192 }
drh4a6f3aa2011-08-28 00:19:26 +00002193 }else if( ALWAYS(pC->pseudoTableReg>0) ){
drha6c2ed92009-11-14 23:22:23 +00002194 pReg = &aMem[pC->pseudoTableReg];
drh3e9ca092009-09-08 01:14:48 +00002195 assert( pReg->flags & MEM_Blob );
drh2b4ded92010-09-27 21:09:31 +00002196 assert( memIsValid(pReg) );
drh3e9ca092009-09-08 01:14:48 +00002197 payloadSize = pReg->n;
2198 zRec = pReg->z;
2199 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002200 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002201 }else{
2202 /* Consider the row to be NULL */
2203 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002204 }
2205
drhe6f43fc2011-08-28 02:15:34 +00002206 /* If payloadSize is 0, then just store a NULL. This can happen because of
2207 ** nullRow or because of a corrupt database. */
danielk1977192ac1d2004-05-10 07:17:30 +00002208 if( payloadSize==0 ){
drhe6f43fc2011-08-28 02:15:34 +00002209 MemSetTypeFlag(pDest, MEM_Null);
drhd4e70eb2008-01-02 00:34:36 +00002210 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002211 }
drh35cd6432009-06-05 14:17:21 +00002212 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2213 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002214 goto too_big;
2215 }
danielk1977192ac1d2004-05-10 07:17:30 +00002216
shane36840fd2009-06-26 16:32:13 +00002217 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002218 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002219
drh9188b382004-05-14 21:12:22 +00002220 /* Read and parse the table header. Store the results of the parse
2221 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002222 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002223 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002224 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002225 aOffset = pC->aOffset;
2226 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002227 assert(aType);
drh856c1032009-06-02 15:21:42 +00002228 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002229 pC->aOffset = aOffset = &aType[nField];
2230 pC->payloadSize = payloadSize;
2231 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002232
drhd3194f52004-05-27 19:59:32 +00002233 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002234 if( zRec ){
2235 zData = zRec;
2236 }else{
drhf0863fe2005-06-12 21:35:51 +00002237 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002238 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002239 }else{
drhe51c44f2004-05-30 20:46:09 +00002240 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002241 }
drhe61cffc2004-06-12 18:12:15 +00002242 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2243 ** save the payload in the pC->aRow cache. That will save us from
2244 ** having to make additional calls to fetch the content portion of
2245 ** the record.
2246 */
drh35cd6432009-06-05 14:17:21 +00002247 assert( avail>=0 );
2248 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002249 zRec = zData;
2250 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002251 }else{
2252 pC->aRow = 0;
2253 }
drhd3194f52004-05-27 19:59:32 +00002254 }
drh588f5bc2007-01-02 18:41:54 +00002255 /* The following assert is true in all cases accept when
2256 ** the database file has been corrupted externally.
2257 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002258 szHdr = getVarint32((u8*)zData, offset);
2259
2260 /* Make sure a corrupt database has not given us an oversize header.
2261 ** Do this now to avoid an oversize memory allocation.
2262 **
2263 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2264 ** types use so much data space that there can only be 4096 and 32 of
2265 ** them, respectively. So the maximum header length results from a
2266 ** 3-byte type for each of the maximum of 32768 columns plus three
2267 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2268 */
2269 if( offset > 98307 ){
2270 rc = SQLITE_CORRUPT_BKPT;
2271 goto op_column_out;
2272 }
2273
2274 /* Compute in len the number of bytes of data we need to read in order
2275 ** to get nField type values. offset is an upper bound on this. But
2276 ** nField might be significantly less than the true number of columns
2277 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2278 ** We want to minimize len in order to limit the size of the memory
2279 ** allocation, especially if a corrupt database file has caused offset
2280 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2281 ** still exceed Robson memory allocation limits on some configurations.
2282 ** On systems that cannot tolerate large memory allocations, nField*5+3
2283 ** will likely be much smaller since nField will likely be less than
2284 ** 20 or so. This insures that Robson memory allocation limits are
2285 ** not exceeded even for corrupt database files.
2286 */
2287 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002288 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002289
2290 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2291 ** record header in most cases. But they will fail to get the complete
2292 ** record header if the record header does not fit on a single page
2293 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2294 ** acquire the complete header text.
2295 */
drh35cd6432009-06-05 14:17:21 +00002296 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002297 sMem.flags = 0;
2298 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002299 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002300 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002301 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002302 }
drhb6f54522004-05-20 02:42:16 +00002303 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002304 }
drh35cd6432009-06-05 14:17:21 +00002305 zEndHdr = (u8 *)&zData[len];
2306 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002307
drhd3194f52004-05-27 19:59:32 +00002308 /* Scan the header and use it to fill in the aType[] and aOffset[]
2309 ** arrays. aType[i] will contain the type integer for the i-th
2310 ** column and aOffset[i] will contain the offset from the beginning
2311 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002312 */
danielk1977dedf45b2006-01-13 17:12:01 +00002313 for(i=0; i<nField; i++){
2314 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002315 aOffset[i] = offset;
drh5a077b72011-08-29 02:16:18 +00002316 if( zIdx[0]<0x80 ){
2317 t = zIdx[0];
2318 zIdx++;
2319 }else{
2320 zIdx += sqlite3GetVarint32(zIdx, &t);
2321 }
2322 aType[i] = t;
2323 szField = sqlite3VdbeSerialTypeLen(t);
drh6658cd92010-02-05 14:12:53 +00002324 offset += szField;
2325 if( offset<szField ){ /* True if offset overflows */
2326 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2327 break;
2328 }
danielk1977dedf45b2006-01-13 17:12:01 +00002329 }else{
2330 /* If i is less that nField, then there are less fields in this
2331 ** record than SetNumColumns indicated there are columns in the
2332 ** table. Set the offset for any extra columns not present in
drh9cbf3422008-01-17 16:22:13 +00002333 ** the record to 0. This tells code below to store a NULL
2334 ** instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002335 */
2336 aOffset[i] = 0;
2337 }
drh9188b382004-05-14 21:12:22 +00002338 }
danielk19775f096132008-03-28 15:44:09 +00002339 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002340 sMem.flags = MEM_Null;
2341
danielk19779792eef2006-01-13 15:58:43 +00002342 /* If we have read more header data than was contained in the header,
2343 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002344 ** record, or if the end of the last field appears to be before the end
2345 ** of the record (when all fields present), then we must be dealing
2346 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002347 */
drh6658cd92010-02-05 14:12:53 +00002348 if( (zIdx > zEndHdr) || (offset > payloadSize)
2349 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002350 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002351 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002352 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002353 }
danielk1977192ac1d2004-05-10 07:17:30 +00002354
danielk197736963fd2005-02-19 08:18:05 +00002355 /* Get the column information. If aOffset[p2] is non-zero, then
2356 ** deserialize the value from the record. If aOffset[p2] is zero,
2357 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002358 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002359 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002360 */
danielk197736963fd2005-02-19 08:18:05 +00002361 if( aOffset[p2] ){
2362 assert( rc==SQLITE_OK );
2363 if( zRec ){
drh2d36eb42011-08-29 02:49:41 +00002364 MemReleaseExt(pDest);
danielk1977808ec7c2008-07-29 10:18:57 +00002365 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002366 }else{
2367 len = sqlite3VdbeSerialTypeLen(aType[p2]);
danielk1977a7a8e142008-02-13 18:25:27 +00002368 sqlite3VdbeMemMove(&sMem, pDest);
drhb21c8cd2007-08-21 19:33:56 +00002369 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
danielk197736963fd2005-02-19 08:18:05 +00002370 if( rc!=SQLITE_OK ){
2371 goto op_column_out;
2372 }
2373 zData = sMem.z;
danielk1977a7a8e142008-02-13 18:25:27 +00002374 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
danielk19777701e812005-01-10 12:59:51 +00002375 }
drhd4e70eb2008-01-02 00:34:36 +00002376 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002377 }else{
danielk197760585dd2008-01-03 08:08:40 +00002378 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002379 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002380 }else{
drhe6f43fc2011-08-28 02:15:34 +00002381 MemSetTypeFlag(pDest, MEM_Null);
danielk1977aee18ef2005-03-09 12:26:50 +00002382 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002383 }
drhfebe1062004-08-28 18:17:48 +00002384
2385 /* If we dynamically allocated space to hold the data (in the
2386 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002387 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002388 ** This prevents a memory copy.
2389 */
danielk19775f096132008-03-28 15:44:09 +00002390 if( sMem.zMalloc ){
2391 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002392 assert( !(pDest->flags & MEM_Dyn) );
2393 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2394 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002395 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002396 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002397 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002398 }
drhfebe1062004-08-28 18:17:48 +00002399
drhd4e70eb2008-01-02 00:34:36 +00002400 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002401
danielk19773c9cc8d2005-01-17 03:40:08 +00002402op_column_out:
drhb7654112008-01-12 12:48:07 +00002403 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002404 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002405 break;
2406}
2407
danielk1977751de562008-04-18 09:01:15 +00002408/* Opcode: Affinity P1 P2 * P4 *
2409**
2410** Apply affinities to a range of P2 registers starting with P1.
2411**
2412** P4 is a string that is P2 characters long. The nth character of the
2413** string indicates the column affinity that should be used for the nth
2414** memory cell in the range.
2415*/
2416case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002417 const char *zAffinity; /* The affinity to be applied */
2418 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002419
drh856c1032009-06-02 15:21:42 +00002420 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002421 assert( zAffinity!=0 );
2422 assert( zAffinity[pOp->p2]==0 );
2423 pIn1 = &aMem[pOp->p1];
2424 while( (cAff = *(zAffinity++))!=0 ){
2425 assert( pIn1 <= &p->aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00002426 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002427 ExpandBlob(pIn1);
2428 applyAffinity(pIn1, cAff, encoding);
2429 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002430 }
2431 break;
2432}
2433
drh1db639c2008-01-17 02:36:28 +00002434/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002435**
drh710c4842010-08-30 01:17:20 +00002436** Convert P2 registers beginning with P1 into the [record format]
2437** use as a data record in a database table or as a key
2438** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002439**
danielk1977751de562008-04-18 09:01:15 +00002440** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002441** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002442** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002443**
drh8a512562005-11-14 22:29:05 +00002444** The mapping from character to affinity is given by the SQLITE_AFF_
2445** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002446**
drh66a51672008-01-03 00:01:23 +00002447** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002448*/
drh1db639c2008-01-17 02:36:28 +00002449case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002450 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2451 Mem *pRec; /* The new record */
2452 u64 nData; /* Number of bytes of data space */
2453 int nHdr; /* Number of bytes of header space */
2454 i64 nByte; /* Data space required for this record */
2455 int nZero; /* Number of zero bytes at the end of the record */
2456 int nVarint; /* Number of bytes in a varint */
2457 u32 serial_type; /* Type field */
2458 Mem *pData0; /* First field to be combined into the record */
2459 Mem *pLast; /* Last field of the record */
2460 int nField; /* Number of fields in the record */
2461 char *zAffinity; /* The affinity string for the record */
2462 int file_format; /* File format to use for encoding */
2463 int i; /* Space used in zNewRecord[] */
2464 int len; /* Length of a field */
2465
drhf3218fe2004-05-28 08:21:02 +00002466 /* Assuming the record contains N fields, the record format looks
2467 ** like this:
2468 **
drh7a224de2004-06-02 01:22:02 +00002469 ** ------------------------------------------------------------------------
2470 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2471 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002472 **
drh9cbf3422008-01-17 16:22:13 +00002473 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2474 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002475 **
2476 ** Each type field is a varint representing the serial type of the
2477 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002478 ** hdr-size field is also a varint which is the offset from the beginning
2479 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002480 */
drh856c1032009-06-02 15:21:42 +00002481 nData = 0; /* Number of bytes of data space */
2482 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002483 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002484 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002485 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002486 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drha6c2ed92009-11-14 23:22:23 +00002487 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002488 nField = pOp->p2;
2489 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002490 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002491
drh2b4ded92010-09-27 21:09:31 +00002492 /* Identify the output register */
2493 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2494 pOut = &aMem[pOp->p3];
2495 memAboutToChange(p, pOut);
2496
drhf3218fe2004-05-28 08:21:02 +00002497 /* Loop through the elements that will make up the record to figure
2498 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002499 */
drha2a49dc2008-01-02 14:28:13 +00002500 for(pRec=pData0; pRec<=pLast; pRec++){
drh2b4ded92010-09-27 21:09:31 +00002501 assert( memIsValid(pRec) );
drhd3d39e92004-05-20 22:16:29 +00002502 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002503 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002504 }
danielk1977d908f5a2007-05-11 07:08:28 +00002505 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002506 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002507 }
drhd946db02005-12-29 19:23:06 +00002508 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002509 len = sqlite3VdbeSerialTypeLen(serial_type);
2510 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002511 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002512 if( pRec->flags & MEM_Zero ){
2513 /* Only pure zero-filled BLOBs can be input to this Opcode.
2514 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002515 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002516 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002517 nZero = 0;
2518 }
danielk19778d059842004-05-12 11:24:02 +00002519 }
danielk19773d1bfea2004-05-14 11:00:53 +00002520
drhf3218fe2004-05-28 08:21:02 +00002521 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002522 nHdr += nVarint = sqlite3VarintLen(nHdr);
2523 if( nVarint<sqlite3VarintLen(nHdr) ){
2524 nHdr++;
2525 }
drhfdf972a2007-05-02 13:30:27 +00002526 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002527 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002528 goto too_big;
2529 }
drhf3218fe2004-05-28 08:21:02 +00002530
danielk1977a7a8e142008-02-13 18:25:27 +00002531 /* Make sure the output register has a buffer large enough to store
2532 ** the new record. The output register (pOp->p3) is not allowed to
2533 ** be one of the input registers (because the following call to
2534 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2535 */
drh9c1905f2008-12-10 22:32:56 +00002536 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002537 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002538 }
danielk1977a7a8e142008-02-13 18:25:27 +00002539 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002540
2541 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002542 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002543 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002544 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002545 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002546 }
drha2a49dc2008-01-02 14:28:13 +00002547 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002548 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002549 }
drhfdf972a2007-05-02 13:30:27 +00002550 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002551
drh9cbf3422008-01-17 16:22:13 +00002552 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002553 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002554 pOut->flags = MEM_Blob | MEM_Dyn;
2555 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002556 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002557 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002558 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002559 }
drh477df4b2008-01-05 18:48:24 +00002560 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002561 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002562 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002563 break;
2564}
2565
danielk1977a5533162009-02-24 10:01:51 +00002566/* Opcode: Count P1 P2 * * *
2567**
2568** Store the number of entries (an integer value) in the table or index
2569** opened by cursor P1 in register P2
2570*/
2571#ifndef SQLITE_OMIT_BTREECOUNT
2572case OP_Count: { /* out2-prerelease */
2573 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002574 BtCursor *pCrsr;
2575
2576 pCrsr = p->apCsr[pOp->p1]->pCursor;
dana205a482011-08-27 18:48:57 +00002577 if( ALWAYS(pCrsr) ){
drh818e39a2009-04-02 20:27:28 +00002578 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2579 }else{
2580 nEntry = 0;
2581 }
danielk1977a5533162009-02-24 10:01:51 +00002582 pOut->u.i = nEntry;
2583 break;
2584}
2585#endif
2586
danielk1977fd7f0452008-12-17 17:30:26 +00002587/* Opcode: Savepoint P1 * * P4 *
2588**
2589** Open, release or rollback the savepoint named by parameter P4, depending
2590** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2591** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2592*/
2593case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002594 int p1; /* Value of P1 operand */
2595 char *zName; /* Name of savepoint */
2596 int nName;
2597 Savepoint *pNew;
2598 Savepoint *pSavepoint;
2599 Savepoint *pTmp;
2600 int iSavepoint;
2601 int ii;
2602
2603 p1 = pOp->p1;
2604 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002605
2606 /* Assert that the p1 parameter is valid. Also that if there is no open
2607 ** transaction, then there cannot be any savepoints.
2608 */
2609 assert( db->pSavepoint==0 || db->autoCommit==0 );
2610 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2611 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2612 assert( checkSavepointCount(db) );
2613
2614 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002615 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002616 /* A new savepoint cannot be created if there are active write
2617 ** statements (i.e. open read/write incremental blob handles).
2618 */
2619 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2620 "SQL statements in progress");
2621 rc = SQLITE_BUSY;
2622 }else{
drh856c1032009-06-02 15:21:42 +00002623 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002624
drhbe07ec52011-06-03 12:15:26 +00002625#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002626 /* This call is Ok even if this savepoint is actually a transaction
2627 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2628 ** If this is a transaction savepoint being opened, it is guaranteed
2629 ** that the db->aVTrans[] array is empty. */
2630 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002631 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2632 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002633 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002634#endif
dand9495cd2011-04-27 12:08:04 +00002635
danielk1977fd7f0452008-12-17 17:30:26 +00002636 /* Create a new savepoint structure. */
2637 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2638 if( pNew ){
2639 pNew->zName = (char *)&pNew[1];
2640 memcpy(pNew->zName, zName, nName+1);
2641
2642 /* If there is no open transaction, then mark this as a special
2643 ** "transaction savepoint". */
2644 if( db->autoCommit ){
2645 db->autoCommit = 0;
2646 db->isTransactionSavepoint = 1;
2647 }else{
2648 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002649 }
danielk1977fd7f0452008-12-17 17:30:26 +00002650
2651 /* Link the new savepoint into the database handle's list. */
2652 pNew->pNext = db->pSavepoint;
2653 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002654 pNew->nDeferredCons = db->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002655 }
2656 }
2657 }else{
drh856c1032009-06-02 15:21:42 +00002658 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002659
2660 /* Find the named savepoint. If there is no such savepoint, then an
2661 ** an error is returned to the user. */
2662 for(
drh856c1032009-06-02 15:21:42 +00002663 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002664 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002665 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002666 ){
2667 iSavepoint++;
2668 }
2669 if( !pSavepoint ){
2670 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2671 rc = SQLITE_ERROR;
2672 }else if(
2673 db->writeVdbeCnt>0 || (p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
2674 ){
2675 /* It is not possible to release (commit) a savepoint if there are
2676 ** active write statements. It is not possible to rollback a savepoint
2677 ** if there are any active statements at all.
2678 */
2679 sqlite3SetString(&p->zErrMsg, db,
2680 "cannot %s savepoint - SQL statements in progress",
2681 (p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
2682 );
2683 rc = SQLITE_BUSY;
2684 }else{
2685
2686 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002687 ** and this is a RELEASE command, then the current transaction
2688 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002689 */
2690 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2691 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002692 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002693 goto vdbe_return;
2694 }
danielk1977fd7f0452008-12-17 17:30:26 +00002695 db->autoCommit = 1;
2696 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2697 p->pc = pc;
2698 db->autoCommit = 0;
2699 p->rc = rc = SQLITE_BUSY;
2700 goto vdbe_return;
2701 }
danielk197734cf35d2008-12-18 18:31:38 +00002702 db->isTransactionSavepoint = 0;
2703 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002704 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002705 iSavepoint = db->nSavepoint - iSavepoint - 1;
2706 for(ii=0; ii<db->nDb; ii++){
2707 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2708 if( rc!=SQLITE_OK ){
2709 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002710 }
danielk1977fd7f0452008-12-17 17:30:26 +00002711 }
drh9f0bbf92009-01-02 21:08:09 +00002712 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002713 sqlite3ExpirePreparedStatements(db);
drhc7792fa2011-04-02 16:28:52 +00002714 sqlite3ResetInternalSchema(db, -1);
danc311fee2010-08-31 16:25:19 +00002715 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002716 }
2717 }
2718
2719 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2720 ** savepoints nested inside of the savepoint being operated on. */
2721 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002722 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002723 db->pSavepoint = pTmp->pNext;
2724 sqlite3DbFree(db, pTmp);
2725 db->nSavepoint--;
2726 }
2727
dan1da40a32009-09-19 17:00:31 +00002728 /* If it is a RELEASE, then destroy the savepoint being operated on
2729 ** too. If it is a ROLLBACK TO, then set the number of deferred
2730 ** constraint violations present in the database to the value stored
2731 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002732 if( p1==SAVEPOINT_RELEASE ){
2733 assert( pSavepoint==db->pSavepoint );
2734 db->pSavepoint = pSavepoint->pNext;
2735 sqlite3DbFree(db, pSavepoint);
2736 if( !isTransaction ){
2737 db->nSavepoint--;
2738 }
dan1da40a32009-09-19 17:00:31 +00002739 }else{
2740 db->nDeferredCons = pSavepoint->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002741 }
dand9495cd2011-04-27 12:08:04 +00002742
2743 if( !isTransaction ){
2744 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2745 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2746 }
danielk1977fd7f0452008-12-17 17:30:26 +00002747 }
2748 }
2749
2750 break;
2751}
2752
drh98757152008-01-09 23:04:12 +00002753/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002754**
2755** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002756** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002757** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2758** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002759**
2760** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002761*/
drh9cbf3422008-01-17 16:22:13 +00002762case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002763 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002764 int iRollback;
drh856c1032009-06-02 15:21:42 +00002765 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002766
drh856c1032009-06-02 15:21:42 +00002767 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002768 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002769 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002770 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002771 assert( desiredAutoCommit==1 || iRollback==0 );
drh92f02c32004-09-02 14:57:08 +00002772 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002773
shane68c02732009-06-09 18:14:18 +00002774 if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){
drhad4a4b82008-11-05 16:37:34 +00002775 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002776 ** still running, and a transaction is active, return an error indicating
2777 ** that the other VMs must complete first.
2778 */
drhad4a4b82008-11-05 16:37:34 +00002779 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2780 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002781 rc = SQLITE_BUSY;
drh9eb8cbe2009-06-19 22:23:41 +00002782 }else if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){
drhad4a4b82008-11-05 16:37:34 +00002783 /* If this instruction implements a COMMIT and other VMs are writing
2784 ** return an error indicating that the other VMs must complete first.
2785 */
2786 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2787 "SQL statements in progress");
2788 rc = SQLITE_BUSY;
2789 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002790 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002791 assert( desiredAutoCommit==1 );
danielk19771d850a72004-05-31 08:26:49 +00002792 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002793 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002794 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002795 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002796 }else{
shane7d3846a2008-12-11 02:58:26 +00002797 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002798 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002799 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002800 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002801 p->rc = rc = SQLITE_BUSY;
2802 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002803 }
danielk19771d850a72004-05-31 08:26:49 +00002804 }
danielk1977bd434552009-03-18 10:33:00 +00002805 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002806 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002807 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002808 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002809 }else{
drh900b31e2007-08-28 02:27:51 +00002810 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002811 }
drh900b31e2007-08-28 02:27:51 +00002812 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002813 }else{
drhf089aa42008-07-08 19:34:06 +00002814 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002815 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002816 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002817 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002818
2819 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002820 }
2821 break;
2822}
2823
drh98757152008-01-09 23:04:12 +00002824/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002825**
2826** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002827** opcode is encountered. Depending on the ON CONFLICT setting, the
2828** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002829**
drh001bbcb2003-03-19 03:14:00 +00002830** P1 is the index of the database file on which the transaction is
2831** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002832** file used for temporary tables. Indices of 2 or more are used for
2833** attached databases.
drhcabb0812002-09-14 13:47:32 +00002834**
drh80242052004-06-09 00:48:12 +00002835** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002836** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002837** other process can start another write transaction while this transaction is
2838** underway. Starting a write transaction also creates a rollback journal. A
2839** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002840** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2841** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002842**
dane0af83a2009-09-08 19:15:01 +00002843** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2844** true (this flag is set if the Vdbe may modify more than one row and may
2845** throw an ABORT exception), a statement transaction may also be opened.
2846** More specifically, a statement transaction is opened iff the database
2847** connection is currently not in autocommit mode, or if there are other
2848** active statements. A statement transaction allows the affects of this
2849** VDBE to be rolled back after an error without having to roll back the
2850** entire transaction. If no error is encountered, the statement transaction
2851** will automatically commit when the VDBE halts.
2852**
danielk1977ee5741e2004-05-31 10:01:34 +00002853** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002854*/
drh9cbf3422008-01-17 16:22:13 +00002855case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002856 Btree *pBt;
2857
drh653b82a2009-06-22 11:10:47 +00002858 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002859 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh653b82a2009-06-22 11:10:47 +00002860 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002861
danielk197724162fe2004-06-04 06:22:00 +00002862 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002863 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002864 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002865 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002866 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002867 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002868 }
drh9e9f1bd2009-10-13 15:36:51 +00002869 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00002870 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002871 }
dane0af83a2009-09-08 19:15:01 +00002872
2873 if( pOp->p2 && p->usesStmtJournal
2874 && (db->autoCommit==0 || db->activeVdbeCnt>1)
2875 ){
2876 assert( sqlite3BtreeIsInTrans(pBt) );
2877 if( p->iStatement==0 ){
2878 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2879 db->nStatement++;
2880 p->iStatement = db->nSavepoint + db->nStatement;
2881 }
dana311b802011-04-26 19:21:34 +00002882
drh346506f2011-05-25 01:16:42 +00002883 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00002884 if( rc==SQLITE_OK ){
2885 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
2886 }
dan1da40a32009-09-19 17:00:31 +00002887
2888 /* Store the current value of the database handles deferred constraint
2889 ** counter. If the statement transaction needs to be rolled back,
2890 ** the value of this counter needs to be restored too. */
2891 p->nStmtDefCons = db->nDeferredCons;
dane0af83a2009-09-08 19:15:01 +00002892 }
drhb86ccfb2003-01-28 23:13:10 +00002893 }
drh5e00f6c2001-09-13 13:46:56 +00002894 break;
2895}
2896
drhb1fdb2a2008-01-05 04:06:03 +00002897/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002898**
drh9cbf3422008-01-17 16:22:13 +00002899** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002900** P3==1 is the schema version. P3==2 is the database format.
2901** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002902** the main database file and P1==1 is the database file used to store
2903** temporary tables.
drh4a324312001-12-21 14:30:42 +00002904**
drh50e5dad2001-09-15 00:57:28 +00002905** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002906** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002907** executing this instruction.
2908*/
drh4c583122008-01-04 22:01:03 +00002909case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002910 int iMeta;
drh856c1032009-06-02 15:21:42 +00002911 int iDb;
2912 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00002913
drh856c1032009-06-02 15:21:42 +00002914 iDb = pOp->p1;
2915 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00002916 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002917 assert( iDb>=0 && iDb<db->nDb );
2918 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00002919 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00002920
danielk1977602b4662009-07-02 07:47:33 +00002921 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002922 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00002923 break;
2924}
2925
drh98757152008-01-09 23:04:12 +00002926/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002927**
drh98757152008-01-09 23:04:12 +00002928** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00002929** into cookie number P2 of database P1. P2==1 is the schema version.
2930** P2==2 is the database format. P2==3 is the recommended pager cache
2931** size, and so forth. P1==0 is the main database file and P1==1 is the
2932** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002933**
2934** A transaction must be started before executing this opcode.
2935*/
drh9cbf3422008-01-17 16:22:13 +00002936case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00002937 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002938 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002939 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002940 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00002941 pDb = &db->aDb[pOp->p1];
2942 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00002943 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00002944 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00002945 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00002946 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00002947 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
2948 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00002949 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00002950 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002951 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00002952 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00002953 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00002954 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00002955 }
drhfd426c62006-01-30 15:34:22 +00002956 if( pOp->p1==1 ){
2957 /* Invalidate all prepared statements whenever the TEMP database
2958 ** schema is changed. Ticket #1644 */
2959 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00002960 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00002961 }
drh50e5dad2001-09-15 00:57:28 +00002962 break;
2963}
2964
drhc2a75552011-03-18 21:55:46 +00002965/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002966**
drh001bbcb2003-03-19 03:14:00 +00002967** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00002968** schema version) and make sure it is equal to P2 and that the
2969** generation counter on the local schema parse equals P3.
2970**
drh001bbcb2003-03-19 03:14:00 +00002971** P1 is the database number which is 0 for the main database file
2972** and 1 for the file holding temporary tables and some higher number
2973** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002974**
2975** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002976** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002977** and that the current process needs to reread the schema.
2978**
2979** Either a transaction needs to have been started or an OP_Open needs
2980** to be executed (to establish a read lock) before this opcode is
2981** invoked.
2982*/
drh9cbf3422008-01-17 16:22:13 +00002983case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002984 int iMeta;
drhc2a75552011-03-18 21:55:46 +00002985 int iGen;
drhc275b4e2004-07-19 17:25:24 +00002986 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00002987
drh001bbcb2003-03-19 03:14:00 +00002988 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002989 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh21206082011-04-04 18:22:02 +00002990 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drhc275b4e2004-07-19 17:25:24 +00002991 pBt = db->aDb[pOp->p1].pBt;
2992 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00002993 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00002994 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00002995 }else{
drhfcd71b62011-04-05 22:08:24 +00002996 iGen = iMeta = 0;
drhc275b4e2004-07-19 17:25:24 +00002997 }
drhc2a75552011-03-18 21:55:46 +00002998 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00002999 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00003000 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00003001 /* If the schema-cookie from the database file matches the cookie
3002 ** stored with the in-memory representation of the schema, do
3003 ** not reload the schema from the database file.
3004 **
shane21e7feb2008-05-30 15:59:49 +00003005 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00003006 ** Often, v-tables store their data in other SQLite tables, which
3007 ** are queried from within xNext() and other v-table methods using
3008 ** prepared queries. If such a query is out-of-date, we do not want to
3009 ** discard the database schema, as the user code implementing the
3010 ** v-table would have to be ready for the sqlite3_vtab structure itself
3011 ** to be invalidated whenever sqlite3_step() is called from within
3012 ** a v-table method.
3013 */
3014 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
3015 sqlite3ResetInternalSchema(db, pOp->p1);
3016 }
3017
drh5b6c5452011-02-22 03:34:56 +00003018 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00003019 rc = SQLITE_SCHEMA;
3020 }
3021 break;
3022}
3023
drh98757152008-01-09 23:04:12 +00003024/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003025**
drhecdc7532001-09-23 02:35:53 +00003026** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003027** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003028** P3==0 means the main database, P3==1 means the database used for
3029** temporary tables, and P3>1 means used the corresponding attached
3030** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003031** values need not be contiguous but all P1 values should be small integers.
3032** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003033**
drh98757152008-01-09 23:04:12 +00003034** If P5!=0 then use the content of register P2 as the root page, not
3035** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003036**
drhb19a2bc2001-09-16 00:13:26 +00003037** There will be a read lock on the database whenever there is an
3038** open cursor. If the database was unlocked prior to this instruction
3039** then a read lock is acquired as part of this instruction. A read
3040** lock allows other processes to read the database but prohibits
3041** any other process from modifying the database. The read lock is
3042** released when all cursors are closed. If this instruction attempts
3043** to get a read lock but fails, the script terminates with an
3044** SQLITE_BUSY error code.
3045**
danielk1977d336e222009-02-20 10:58:41 +00003046** The P4 value may be either an integer (P4_INT32) or a pointer to
3047** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3048** structure, then said structure defines the content and collating
3049** sequence of the index being opened. Otherwise, if P4 is an integer
3050** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003051**
drh001bbcb2003-03-19 03:14:00 +00003052** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003053*/
drh98757152008-01-09 23:04:12 +00003054/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00003055**
3056** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003057** page is P2. Or if P5!=0 use the content of register P2 to find the
3058** root page.
drhecdc7532001-09-23 02:35:53 +00003059**
danielk1977d336e222009-02-20 10:58:41 +00003060** The P4 value may be either an integer (P4_INT32) or a pointer to
3061** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3062** structure, then said structure defines the content and collating
3063** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003064** value, it is set to the number of columns in the table, or to the
3065** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003066**
drh001bbcb2003-03-19 03:14:00 +00003067** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003068** in read/write mode. For a given table, there can be one or more read-only
3069** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003070**
drh001bbcb2003-03-19 03:14:00 +00003071** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003072*/
drh9cbf3422008-01-17 16:22:13 +00003073case OP_OpenRead:
3074case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003075 int nField;
3076 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003077 int p2;
3078 int iDb;
drhf57b3392001-10-08 13:22:32 +00003079 int wrFlag;
3080 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003081 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003082 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003083
danfa401de2009-10-16 14:55:03 +00003084 if( p->expired ){
3085 rc = SQLITE_ABORT;
3086 break;
3087 }
3088
drh856c1032009-06-02 15:21:42 +00003089 nField = 0;
3090 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003091 p2 = pOp->p2;
3092 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003093 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003094 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003095 pDb = &db->aDb[iDb];
3096 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003097 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003098 if( pOp->opcode==OP_OpenWrite ){
3099 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003100 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003101 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3102 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003103 }
3104 }else{
3105 wrFlag = 0;
3106 }
drh98757152008-01-09 23:04:12 +00003107 if( pOp->p5 ){
drh9cbf3422008-01-17 16:22:13 +00003108 assert( p2>0 );
3109 assert( p2<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003110 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003111 assert( memIsValid(pIn2) );
3112 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003113 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003114 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003115 /* The p2 value always comes from a prior OP_CreateTable opcode and
3116 ** that opcode will always set the p2 value to 2 or more or else fail.
3117 ** If there were a failure, the prepared statement would have halted
3118 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003119 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003120 rc = SQLITE_CORRUPT_BKPT;
3121 goto abort_due_to_error;
3122 }
drh5edc3122001-09-13 21:53:09 +00003123 }
danielk1977d336e222009-02-20 10:58:41 +00003124 if( pOp->p4type==P4_KEYINFO ){
3125 pKeyInfo = pOp->p4.pKeyInfo;
3126 pKeyInfo->enc = ENC(p->db);
3127 nField = pKeyInfo->nField+1;
3128 }else if( pOp->p4type==P4_INT32 ){
3129 nField = pOp->p4.i;
3130 }
drh653b82a2009-06-22 11:10:47 +00003131 assert( pOp->p1>=0 );
3132 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003133 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003134 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003135 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003136 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3137 pCur->pKeyInfo = pKeyInfo;
3138
dana205a482011-08-27 18:48:57 +00003139 /* Since it performs no memory allocation or IO, the only value that
3140 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3141 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003142
3143 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3144 ** SQLite used to check if the root-page flags were sane at this point
3145 ** and report database corruption if they were not, but this check has
3146 ** since moved into the btree layer. */
3147 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3148 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003149 break;
3150}
3151
drh2a5d9902011-08-26 00:34:45 +00003152/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003153**
drhb9bb7c12006-06-11 23:41:55 +00003154** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003155** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003156** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003157** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003158**
drh25d3adb2010-04-05 15:11:08 +00003159** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003160** The cursor points to a BTree table if P4==0 and to a BTree index
3161** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003162** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003163**
3164** This opcode was once called OpenTemp. But that created
3165** confusion because the term "temp table", might refer either
3166** to a TEMP table at the SQL level, or to a table opened by
3167** this opcode. Then this opcode was call OpenVirtual. But
3168** that created confusion with the whole virtual-table idea.
drh2a5d9902011-08-26 00:34:45 +00003169**
3170** The P5 parameter can be a mask of the BTREE_* flags defined
3171** in btree.h. These flags control aspects of the operation of
3172** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3173** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003174*/
drha21a64d2010-04-06 22:33:55 +00003175/* Opcode: OpenAutoindex P1 P2 * P4 *
3176**
3177** This opcode works the same as OP_OpenEphemeral. It has a
3178** different name to distinguish its use. Tables created using
3179** by this opcode will be used for automatically created transient
3180** indices in joins.
3181*/
3182case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003183case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003184 VdbeCursor *pCx;
drhd4187c72010-08-30 22:15:45 +00003185 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003186 SQLITE_OPEN_READWRITE |
3187 SQLITE_OPEN_CREATE |
3188 SQLITE_OPEN_EXCLUSIVE |
3189 SQLITE_OPEN_DELETEONCLOSE |
3190 SQLITE_OPEN_TRANSIENT_DB;
3191
drh653b82a2009-06-22 11:10:47 +00003192 assert( pOp->p1>=0 );
3193 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003194 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003195 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003196 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3197 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003198 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003199 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003200 }
3201 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003202 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003203 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003204 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003205 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003206 */
danielk19772dca4ac2008-01-03 11:50:29 +00003207 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003208 int pgno;
drh66a51672008-01-03 00:01:23 +00003209 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003210 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003211 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003212 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003213 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003214 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003215 pCx->pKeyInfo = pOp->p4.pKeyInfo;
dan689ab892011-08-12 15:02:00 +00003216 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003217 }
drhf0863fe2005-06-12 21:35:51 +00003218 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003219 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003220 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003221 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003222 }
drh5e00f6c2001-09-13 13:46:56 +00003223 }
drhd4187c72010-08-30 22:15:45 +00003224 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drhf0863fe2005-06-12 21:35:51 +00003225 pCx->isIndex = !pCx->isTable;
dan5134d132011-09-02 10:31:11 +00003226 break;
3227}
3228
3229/* Opcode: OpenSorter P1 P2 * P4 *
3230**
3231** This opcode works like OP_OpenEphemeral except that it opens
3232** a transient index that is specifically designed to sort large
3233** tables using an external merge-sort algorithm.
3234*/
drhca892a72011-09-03 00:17:51 +00003235case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003236 VdbeCursor *pCx;
drhca892a72011-09-03 00:17:51 +00003237#ifndef SQLITE_OMIT_MERGE_SORT
dan5134d132011-09-02 10:31:11 +00003238 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3239 if( pCx==0 ) goto no_mem;
3240 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3241 pCx->pKeyInfo->enc = ENC(p->db);
3242 pCx->isSorter = 1;
3243 rc = sqlite3VdbeSorterInit(db, pCx);
drhca892a72011-09-03 00:17:51 +00003244#else
3245 pOp->opcode = OP_OpenEphemeral;
3246 pc--;
3247#endif
drh5e00f6c2001-09-13 13:46:56 +00003248 break;
3249}
3250
danielk1977d336e222009-02-20 10:58:41 +00003251/* Opcode: OpenPseudo P1 P2 P3 * *
drh70ce3f02003-04-15 19:22:22 +00003252**
3253** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003254** row of data. The content of that one row in the content of memory
3255** register P2. In other words, cursor P1 becomes an alias for the
3256** MEM_Blob content contained in register P2.
drh70ce3f02003-04-15 19:22:22 +00003257**
drh2d8d7ce2010-02-15 15:17:05 +00003258** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003259** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003260** individual columns using the OP_Column opcode. The OP_Column opcode
3261** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003262**
3263** P3 is the number of fields in the records that will be stored by
3264** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003265*/
drh9cbf3422008-01-17 16:22:13 +00003266case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003267 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003268
drh653b82a2009-06-22 11:10:47 +00003269 assert( pOp->p1>=0 );
3270 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003271 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003272 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003273 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003274 pCx->isTable = 1;
3275 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00003276 break;
3277}
3278
drh98757152008-01-09 23:04:12 +00003279/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003280**
3281** Close a cursor previously opened as P1. If P1 is not
3282** currently open, this instruction is a no-op.
3283*/
drh9cbf3422008-01-17 16:22:13 +00003284case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003285 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3286 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3287 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003288 break;
3289}
3290
drh959403f2008-12-12 17:56:16 +00003291/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003292**
danielk1977b790c6c2008-04-18 10:25:24 +00003293** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003294** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003295** to an SQL index, then P3 is the first in an array of P4 registers
3296** that are used as an unpacked index key.
3297**
3298** Reposition cursor P1 so that it points to the smallest entry that
3299** is greater than or equal to the key value. If there are no records
3300** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003301**
drh959403f2008-12-12 17:56:16 +00003302** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003303*/
drh959403f2008-12-12 17:56:16 +00003304/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003305**
danielk1977b790c6c2008-04-18 10:25:24 +00003306** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003307** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003308** to an SQL index, then P3 is the first in an array of P4 registers
3309** that are used as an unpacked index key.
3310**
3311** Reposition cursor P1 so that it points to the smallest entry that
3312** is greater than the key value. If there are no records greater than
3313** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003314**
drh959403f2008-12-12 17:56:16 +00003315** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003316*/
drh959403f2008-12-12 17:56:16 +00003317/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003318**
danielk1977b790c6c2008-04-18 10:25:24 +00003319** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003320** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003321** to an SQL index, then P3 is the first in an array of P4 registers
3322** that are used as an unpacked index key.
3323**
3324** Reposition cursor P1 so that it points to the largest entry that
3325** is less than the key value. If there are no records less than
3326** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003327**
drh959403f2008-12-12 17:56:16 +00003328** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003329*/
drh959403f2008-12-12 17:56:16 +00003330/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003331**
danielk1977b790c6c2008-04-18 10:25:24 +00003332** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003333** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003334** to an SQL index, then P3 is the first in an array of P4 registers
3335** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003336**
danielk1977b790c6c2008-04-18 10:25:24 +00003337** Reposition cursor P1 so that it points to the largest entry that
3338** is less than or equal to the key value. If there are no records
3339** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003340**
drh959403f2008-12-12 17:56:16 +00003341** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003342*/
drh959403f2008-12-12 17:56:16 +00003343case OP_SeekLt: /* jump, in3 */
3344case OP_SeekLe: /* jump, in3 */
3345case OP_SeekGe: /* jump, in3 */
3346case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003347 int res;
3348 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003349 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003350 UnpackedRecord r;
3351 int nField;
3352 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003353
drh653b82a2009-06-22 11:10:47 +00003354 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003355 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003356 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003357 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003358 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003359 assert( OP_SeekLe == OP_SeekLt+1 );
3360 assert( OP_SeekGe == OP_SeekLt+2 );
3361 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003362 assert( pC->isOrdered );
dana205a482011-08-27 18:48:57 +00003363 if( ALWAYS(pC->pCursor!=0) ){
drh7cf6e4d2004-05-19 14:56:55 +00003364 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003365 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003366 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003367 /* The input value in P3 might be of any type: integer, real, string,
3368 ** blob, or NULL. But it needs to be an integer before we can do
3369 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003370 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003371 applyNumericAffinity(pIn3);
3372 iKey = sqlite3VdbeIntValue(pIn3);
3373 pC->rowidIsValid = 0;
3374
3375 /* If the P3 value could not be converted into an integer without
3376 ** loss of information, then special processing is required... */
3377 if( (pIn3->flags & MEM_Int)==0 ){
3378 if( (pIn3->flags & MEM_Real)==0 ){
3379 /* If the P3 value cannot be converted into any kind of a number,
3380 ** then the seek is not possible, so jump to P2 */
3381 pc = pOp->p2 - 1;
3382 break;
3383 }
3384 /* If we reach this point, then the P3 value must be a floating
3385 ** point number. */
3386 assert( (pIn3->flags & MEM_Real)!=0 );
3387
3388 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003389 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003390 ** integer. */
3391 res = 1;
3392 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003393 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003394 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3395 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3396 }
3397 }else{
drh1f350122009-11-13 20:52:43 +00003398 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003399 rc = sqlite3BtreeLast(pC->pCursor, &res);
3400 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3401 }
3402 }
3403 if( res ){
3404 pc = pOp->p2 - 1;
3405 }
3406 break;
3407 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3408 /* Use the ceiling() function to convert real->int */
3409 if( pIn3->r > (double)iKey ) iKey++;
3410 }else{
3411 /* Use the floor() function to convert real->int */
3412 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3413 if( pIn3->r < (double)iKey ) iKey--;
3414 }
3415 }
drhe63d9992008-08-13 19:11:48 +00003416 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003417 if( rc!=SQLITE_OK ){
3418 goto abort_due_to_error;
3419 }
drh959403f2008-12-12 17:56:16 +00003420 if( res==0 ){
3421 pC->rowidIsValid = 1;
3422 pC->lastRowid = iKey;
3423 }
drh5e00f6c2001-09-13 13:46:56 +00003424 }else{
drh856c1032009-06-02 15:21:42 +00003425 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003426 assert( pOp->p4type==P4_INT32 );
3427 assert( nField>0 );
3428 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003429 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003430
3431 /* The next line of code computes as follows, only faster:
3432 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3433 ** r.flags = UNPACKED_INCRKEY;
3434 ** }else{
3435 ** r.flags = 0;
3436 ** }
3437 */
shaneh5e17e8b2009-12-03 04:40:47 +00003438 r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003439 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3440 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3441 assert( oc!=OP_SeekGe || r.flags==0 );
3442 assert( oc!=OP_SeekLt || r.flags==0 );
3443
drha6c2ed92009-11-14 23:22:23 +00003444 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003445#ifdef SQLITE_DEBUG
3446 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3447#endif
drh039fc322009-11-17 18:31:47 +00003448 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003449 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003450 if( rc!=SQLITE_OK ){
3451 goto abort_due_to_error;
3452 }
drhf0863fe2005-06-12 21:35:51 +00003453 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003454 }
drha11846b2004-01-07 18:52:56 +00003455 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003456 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003457#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003458 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003459#endif
drh1f350122009-11-13 20:52:43 +00003460 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003461 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003462 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003463 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003464 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003465 }else{
3466 res = 0;
drh8721ce42001-11-07 14:22:00 +00003467 }
drh7cf6e4d2004-05-19 14:56:55 +00003468 }else{
drh959403f2008-12-12 17:56:16 +00003469 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3470 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003471 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3472 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003473 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003474 }else{
3475 /* res might be negative because the table is empty. Check to
3476 ** see if this is the case.
3477 */
drhf328bc82004-05-10 23:29:49 +00003478 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003479 }
drh1af3fdb2004-07-18 21:33:01 +00003480 }
drh91fd4d42008-01-19 20:11:25 +00003481 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003482 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003483 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003484 }
drhaa736092009-06-22 00:55:30 +00003485 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003486 /* This happens when attempting to open the sqlite3_master table
3487 ** for read access returns SQLITE_EMPTY. In this case always
3488 ** take the jump (since there are no records in the table).
3489 */
3490 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003491 }
drh5e00f6c2001-09-13 13:46:56 +00003492 break;
3493}
3494
drh959403f2008-12-12 17:56:16 +00003495/* Opcode: Seek P1 P2 * * *
3496**
3497** P1 is an open table cursor and P2 is a rowid integer. Arrange
3498** for P1 to move so that it points to the rowid given by P2.
3499**
3500** This is actually a deferred seek. Nothing actually happens until
3501** the cursor is used to read a record. That way, if no reads
3502** occur, no unnecessary I/O happens.
3503*/
3504case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003505 VdbeCursor *pC;
3506
drh653b82a2009-06-22 11:10:47 +00003507 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3508 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003509 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003510 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003511 assert( pC->isTable );
3512 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003513 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003514 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3515 pC->rowidIsValid = 0;
3516 pC->deferredMoveto = 1;
3517 }
3518 break;
3519}
3520
3521
drh8cff69d2009-11-12 19:59:44 +00003522/* Opcode: Found P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003523**
drh8cff69d2009-11-12 19:59:44 +00003524** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3525** P4>0 then register P3 is the first of P4 registers that form an unpacked
3526** record.
3527**
3528** Cursor P1 is on an index btree. If the record identified by P3 and P4
3529** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003530** P1 is left pointing at the matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003531*/
drh8cff69d2009-11-12 19:59:44 +00003532/* Opcode: NotFound P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003533**
drh8cff69d2009-11-12 19:59:44 +00003534** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3535** P4>0 then register P3 is the first of P4 registers that form an unpacked
3536** record.
3537**
3538** Cursor P1 is on an index btree. If the record identified by P3 and P4
3539** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3540** does contain an entry whose prefix matches the P3/P4 record then control
3541** falls through to the next instruction and P1 is left pointing at the
3542** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003543**
drhcb6d50e2008-08-21 19:28:30 +00003544** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003545*/
drh9cbf3422008-01-17 16:22:13 +00003546case OP_NotFound: /* jump, in3 */
3547case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003548 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003549 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003550 int res;
dan03e9cfc2011-09-05 14:20:27 +00003551 char *pFree;
drh856c1032009-06-02 15:21:42 +00003552 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003553 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003554 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3555
dan0ff297e2009-09-25 17:03:14 +00003556#ifdef SQLITE_TEST
3557 sqlite3_found_count++;
3558#endif
3559
drh856c1032009-06-02 15:21:42 +00003560 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003561 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003562 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003563 pC = p->apCsr[pOp->p1];
3564 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003565 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003566 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003567
drhf0863fe2005-06-12 21:35:51 +00003568 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003569 if( pOp->p4.i>0 ){
3570 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003571 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003572 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003573#ifdef SQLITE_DEBUG
3574 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3575#endif
drh8cff69d2009-11-12 19:59:44 +00003576 r.flags = UNPACKED_PREFIX_MATCH;
3577 pIdxKey = &r;
3578 }else{
dan03e9cfc2011-09-05 14:20:27 +00003579 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3580 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3581 );
3582 if( pIdxKey==0 ) goto no_mem;
drh8cff69d2009-11-12 19:59:44 +00003583 assert( pIn3->flags & MEM_Blob );
drhd81a1422010-09-28 07:11:24 +00003584 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
dan03e9cfc2011-09-05 14:20:27 +00003585 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh8cff69d2009-11-12 19:59:44 +00003586 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003587 }
drhe63d9992008-08-13 19:11:48 +00003588 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003589 if( pOp->p4.i==0 ){
dan03e9cfc2011-09-05 14:20:27 +00003590 sqlite3DbFree(db, pFree);
drh8cff69d2009-11-12 19:59:44 +00003591 }
danielk197777519402007-08-30 11:48:31 +00003592 if( rc!=SQLITE_OK ){
3593 break;
3594 }
3595 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003596 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003597 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003598 }
3599 if( pOp->opcode==OP_Found ){
3600 if( alreadyExists ) pc = pOp->p2 - 1;
3601 }else{
3602 if( !alreadyExists ) pc = pOp->p2 - 1;
3603 }
drh5e00f6c2001-09-13 13:46:56 +00003604 break;
3605}
3606
drh98757152008-01-09 23:04:12 +00003607/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003608**
drh8cff69d2009-11-12 19:59:44 +00003609** Cursor P1 is open on an index b-tree - that is to say, a btree which
3610** no data and where the key are records generated by OP_MakeRecord with
3611** the list field being the integer ROWID of the entry that the index
3612** entry refers to.
danielk1977de630352009-05-04 11:42:29 +00003613**
3614** The P3 register contains an integer record number. Call this record
3615** number R. Register P4 is the first in a set of N contiguous registers
3616** that make up an unpacked index key that can be used with cursor P1.
3617** The value of N can be inferred from the cursor. N includes the rowid
3618** value appended to the end of the index record. This rowid value may
3619** or may not be the same as R.
3620**
3621** If any of the N registers beginning with register P4 contains a NULL
3622** value, jump immediately to P2.
3623**
3624** Otherwise, this instruction checks if cursor P1 contains an entry
3625** where the first (N-1) fields match but the rowid value at the end
3626** of the index entry is not R. If there is no such entry, control jumps
3627** to instruction P2. Otherwise, the rowid of the conflicting index
3628** entry is copied to register P3 and control falls through to the next
3629** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003630**
drh9cbf3422008-01-17 16:22:13 +00003631** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003632*/
drh9cbf3422008-01-17 16:22:13 +00003633case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003634 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003635 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003636 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003637 u16 nField;
drha6c2ed92009-11-14 23:22:23 +00003638 Mem *aMx;
drh856c1032009-06-02 15:21:42 +00003639 UnpackedRecord r; /* B-Tree index search key */
3640 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003641
drh3c657212009-11-17 23:59:58 +00003642 pIn3 = &aMem[pOp->p3];
drha6c2ed92009-11-14 23:22:23 +00003643 aMx = &aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003644 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003645 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003646 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003647 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3648
3649 /* Find the index cursor. */
3650 pCx = p->apCsr[pOp->p1];
3651 assert( pCx->deferredMoveto==0 );
3652 pCx->seekResult = 0;
3653 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003654 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003655
3656 /* If any of the values are NULL, take the jump. */
3657 nField = pCx->pKeyInfo->nField;
3658 for(ii=0; ii<nField; ii++){
drha6c2ed92009-11-14 23:22:23 +00003659 if( aMx[ii].flags & MEM_Null ){
danielk1977de630352009-05-04 11:42:29 +00003660 pc = pOp->p2 - 1;
3661 pCrsr = 0;
3662 break;
3663 }
3664 }
drha6c2ed92009-11-14 23:22:23 +00003665 assert( (aMx[nField].flags & MEM_Null)==0 );
danielk1977de630352009-05-04 11:42:29 +00003666
drhf328bc82004-05-10 23:29:49 +00003667 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003668 /* Populate the index search key. */
3669 r.pKeyInfo = pCx->pKeyInfo;
3670 r.nField = nField + 1;
3671 r.flags = UNPACKED_PREFIX_SEARCH;
drha6c2ed92009-11-14 23:22:23 +00003672 r.aMem = aMx;
drh2b4ded92010-09-27 21:09:31 +00003673#ifdef SQLITE_DEBUG
3674 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3675#endif
danielk1977452c9892004-05-13 05:16:15 +00003676
danielk1977de630352009-05-04 11:42:29 +00003677 /* Extract the value of R from register P3. */
3678 sqlite3VdbeMemIntegerify(pIn3);
3679 R = pIn3->u.i;
3680
3681 /* Search the B-Tree index. If no conflicting record is found, jump
3682 ** to P2. Otherwise, copy the rowid of the conflicting record to
3683 ** register P3 and fall through to the next instruction. */
3684 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3685 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003686 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003687 }else{
3688 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003689 }
drh9cfcf5d2002-01-29 18:41:24 +00003690 }
3691 break;
3692}
3693
drh9cbf3422008-01-17 16:22:13 +00003694/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003695**
drhef8662b2011-06-20 21:47:58 +00003696** Use the content of register P3 as an integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003697** with that key does not exist in table of P1, then jump to P2.
drh710c4842010-08-30 01:17:20 +00003698** If the record does exist, then fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003699** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003700**
3701** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003702** operation assumes the key is an integer and that P1 is a table whereas
3703** NotFound assumes key is a blob constructed from MakeRecord and
3704** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003705**
drhcb6d50e2008-08-21 19:28:30 +00003706** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003707*/
drh9cbf3422008-01-17 16:22:13 +00003708case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003709 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003710 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003711 int res;
3712 u64 iKey;
3713
drh3c657212009-11-17 23:59:58 +00003714 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003715 assert( pIn3->flags & MEM_Int );
3716 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3717 pC = p->apCsr[pOp->p1];
3718 assert( pC!=0 );
3719 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003720 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003721 pCrsr = pC->pCursor;
dana205a482011-08-27 18:48:57 +00003722 if( ALWAYS(pCrsr!=0) ){
drh856c1032009-06-02 15:21:42 +00003723 res = 0;
drhaa736092009-06-22 00:55:30 +00003724 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003725 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003726 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003727 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003728 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003729 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003730 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003731 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003732 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003733 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003734 }
danielk1977de630352009-05-04 11:42:29 +00003735 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003736 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003737 /* This happens when an attempt to open a read cursor on the
3738 ** sqlite_master table returns SQLITE_EMPTY.
3739 */
danielk1977f7b9d662008-06-23 18:49:43 +00003740 pc = pOp->p2 - 1;
3741 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003742 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003743 }
drh6b125452002-01-28 15:53:03 +00003744 break;
3745}
3746
drh4c583122008-01-04 22:01:03 +00003747/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003748**
drh4c583122008-01-04 22:01:03 +00003749** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003750** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003751** The sequence number on the cursor is incremented after this
3752** instruction.
drh4db38a72005-09-01 12:16:28 +00003753*/
drh4c583122008-01-04 22:01:03 +00003754case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003755 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3756 assert( p->apCsr[pOp->p1]!=0 );
3757 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003758 break;
3759}
3760
3761
drh98757152008-01-09 23:04:12 +00003762/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003763**
drhf0863fe2005-06-12 21:35:51 +00003764** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003765** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003766** table that cursor P1 points to. The new record number is written
3767** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003768**
dan76d462e2009-08-30 11:42:51 +00003769** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3770** the largest previously generated record number. No new record numbers are
3771** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003772** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003773** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003774** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003775*/
drh4c583122008-01-04 22:01:03 +00003776case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003777 i64 v; /* The new rowid */
3778 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3779 int res; /* Result of an sqlite3BtreeLast() */
3780 int cnt; /* Counter to limit the number of searches */
3781 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003782 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003783
drh856c1032009-06-02 15:21:42 +00003784 v = 0;
3785 res = 0;
drhaa736092009-06-22 00:55:30 +00003786 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3787 pC = p->apCsr[pOp->p1];
3788 assert( pC!=0 );
3789 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003790 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003791 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003792 /* The next rowid or record number (different terms for the same
3793 ** thing) is obtained in a two-step algorithm.
3794 **
3795 ** First we attempt to find the largest existing rowid and add one
3796 ** to that. But if the largest existing rowid is already the maximum
3797 ** positive integer, we have to fall through to the second
3798 ** probabilistic algorithm
3799 **
3800 ** The second algorithm is to select a rowid at random and see if
3801 ** it already exists in the table. If it does not exist, we have
3802 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003803 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003804 */
drhaa736092009-06-22 00:55:30 +00003805 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003806
drh75f86a42005-02-17 00:03:06 +00003807#ifdef SQLITE_32BIT_ROWID
3808# define MAX_ROWID 0x7fffffff
3809#else
drhfe2093d2005-01-20 22:48:47 +00003810 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3811 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3812 ** to provide the constant while making all compilers happy.
3813 */
danielk197764202cf2008-11-17 15:31:47 +00003814# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003815#endif
drhfe2093d2005-01-20 22:48:47 +00003816
drh5cf8e8c2002-02-19 22:42:05 +00003817 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003818 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3819 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003820 rc = sqlite3BtreeLast(pC->pCursor, &res);
3821 if( rc!=SQLITE_OK ){
3822 goto abort_due_to_error;
3823 }
drh32fbe342002-10-19 20:16:37 +00003824 if( res ){
drhc79c7612010-01-01 18:57:48 +00003825 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003826 }else{
drhea8ffdf2009-07-22 00:35:23 +00003827 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003828 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3829 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drh75f86a42005-02-17 00:03:06 +00003830 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003831 pC->useRandomRowid = 1;
3832 }else{
drhc79c7612010-01-01 18:57:48 +00003833 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003834 }
drh5cf8e8c2002-02-19 22:42:05 +00003835 }
drh3fc190c2001-09-14 03:24:23 +00003836 }
drh205f48e2004-11-05 00:43:11 +00003837
3838#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003839 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003840 /* Assert that P3 is a valid memory cell. */
3841 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003842 if( p->pFrame ){
3843 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003844 /* Assert that P3 is a valid memory cell. */
3845 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003846 pMem = &pFrame->aMem[pOp->p3];
3847 }else{
shaneabc6b892009-09-10 19:09:03 +00003848 /* Assert that P3 is a valid memory cell. */
3849 assert( pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003850 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003851 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003852 }
drh2b4ded92010-09-27 21:09:31 +00003853 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003854
3855 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003856 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003857 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003858 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003859 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003860 goto abort_due_to_error;
3861 }
drh3c024d62007-03-30 11:23:45 +00003862 if( v<pMem->u.i+1 ){
3863 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003864 }
drh3c024d62007-03-30 11:23:45 +00003865 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003866 }
3867#endif
3868
drh7f751222009-03-17 22:33:00 +00003869 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003870 }
3871 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003872 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003873 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003874 ** engine starts picking positive candidate ROWIDs at random until
3875 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003876 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3877 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003878 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00003879 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003880 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3881 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003882 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003883 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3884 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003885 && (res==0)
3886 && (++cnt<100)){
3887 /* collision - try another random rowid */
3888 sqlite3_randomness(sizeof(v), &v);
3889 if( cnt<5 ){
3890 /* try "small" random rowids for the initial attempts */
3891 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00003892 }else{
shanehc4d340a2010-09-01 02:37:56 +00003893 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00003894 }
shanehc4d340a2010-09-01 02:37:56 +00003895 v++; /* ensure non-zero */
3896 }
drhaa736092009-06-22 00:55:30 +00003897 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00003898 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00003899 goto abort_due_to_error;
3900 }
drh748a52c2010-09-01 11:50:08 +00003901 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00003902 }
drhf0863fe2005-06-12 21:35:51 +00003903 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003904 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003905 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003906 }
drh4c583122008-01-04 22:01:03 +00003907 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003908 break;
3909}
3910
danielk19771f4aa332008-01-03 09:51:55 +00003911/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003912**
jplyon5a564222003-06-02 06:15:58 +00003913** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003914** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00003915** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00003916** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00003917** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00003918**
danielk19771f4aa332008-01-03 09:51:55 +00003919** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
3920** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00003921** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00003922** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00003923**
drh3e9ca092009-09-08 01:14:48 +00003924** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
3925** the last seek operation (OP_NotExists) was a success, then this
3926** operation will not attempt to find the appropriate row before doing
3927** the insert but will instead overwrite the row that the cursor is
3928** currently pointing to. Presumably, the prior OP_NotExists opcode
3929** has already positioned the cursor correctly. This is an optimization
3930** that boosts performance by avoiding redundant seeks.
3931**
3932** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
3933** UPDATE operation. Otherwise (if the flag is clear) then this opcode
3934** is part of an INSERT operation. The difference is only important to
3935** the update hook.
3936**
drh66a51672008-01-03 00:01:23 +00003937** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00003938** may be NULL. If it is not NULL, then the update-hook
3939** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3940**
drh93aed5a2008-01-16 17:46:38 +00003941** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
3942** allocated, then ownership of P2 is transferred to the pseudo-cursor
3943** and register P2 becomes ephemeral. If the cursor is changed, the
3944** value of register P2 will then change. Make sure this does not
3945** cause any problems.)
3946**
drhf0863fe2005-06-12 21:35:51 +00003947** This instruction only works on tables. The equivalent instruction
3948** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003949*/
drhe05c9292009-10-29 13:48:10 +00003950/* Opcode: InsertInt P1 P2 P3 P4 P5
3951**
3952** This works exactly like OP_Insert except that the key is the
3953** integer value P3, not the value of the integer stored in register P3.
3954*/
3955case OP_Insert:
3956case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00003957 Mem *pData; /* MEM cell holding data for the record to be inserted */
3958 Mem *pKey; /* MEM cell holding key for the record */
3959 i64 iKey; /* The integer ROWID or key for the record to be inserted */
3960 VdbeCursor *pC; /* Cursor to table into which insert is written */
3961 int nZero; /* Number of zero-bytes to append */
3962 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
3963 const char *zDb; /* database name - used by the update hook */
3964 const char *zTbl; /* Table name - used by the opdate hook */
3965 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00003966
drha6c2ed92009-11-14 23:22:23 +00003967 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00003968 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00003969 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00003970 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00003971 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003972 assert( pC->pCursor!=0 );
3973 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00003974 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00003975 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00003976
drhe05c9292009-10-29 13:48:10 +00003977 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00003978 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00003979 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00003980 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00003981 REGISTER_TRACE(pOp->p3, pKey);
3982 iKey = pKey->u.i;
3983 }else{
3984 assert( pOp->opcode==OP_InsertInt );
3985 iKey = pOp->p3;
3986 }
3987
drha05a7222008-01-19 03:35:58 +00003988 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00003989 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00003990 if( pData->flags & MEM_Null ){
3991 pData->z = 0;
3992 pData->n = 0;
3993 }else{
3994 assert( pData->flags & (MEM_Blob|MEM_Str) );
3995 }
drh3e9ca092009-09-08 01:14:48 +00003996 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
3997 if( pData->flags & MEM_Zero ){
3998 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00003999 }else{
drh3e9ca092009-09-08 01:14:48 +00004000 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004001 }
drh3e9ca092009-09-08 01:14:48 +00004002 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
4003 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4004 pData->z, pData->n, nZero,
4005 pOp->p5 & OPFLAG_APPEND, seekResult
4006 );
drha05a7222008-01-19 03:35:58 +00004007 pC->rowidIsValid = 0;
4008 pC->deferredMoveto = 0;
4009 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004010
drha05a7222008-01-19 03:35:58 +00004011 /* Invoke the update-hook if required. */
4012 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004013 zDb = db->aDb[pC->iDb].zName;
4014 zTbl = pOp->p4.z;
4015 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004016 assert( pC->isTable );
4017 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4018 assert( pC->iDb>=0 );
4019 }
drh5e00f6c2001-09-13 13:46:56 +00004020 break;
4021}
4022
drh98757152008-01-09 23:04:12 +00004023/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004024**
drh5edc3122001-09-13 21:53:09 +00004025** Delete the record at which the P1 cursor is currently pointing.
4026**
4027** The cursor will be left pointing at either the next or the previous
4028** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004029** the next Next instruction will be a no-op. Hence it is OK to delete
4030** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004031**
rdcb0c374f2004-02-20 22:53:38 +00004032** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004033** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004034**
drh91fd4d42008-01-19 20:11:25 +00004035** P1 must not be pseudo-table. It has to be a real table with
4036** multiple rows.
4037**
4038** If P4 is not NULL, then it is the name of the table that P1 is
4039** pointing to. The update hook will be invoked, if it exists.
4040** If P4 is not NULL then the P1 cursor must have been positioned
4041** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004042*/
drh9cbf3422008-01-17 16:22:13 +00004043case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004044 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004045 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004046
drh856c1032009-06-02 15:21:42 +00004047 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00004048 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4049 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004050 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004051 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00004052
drh91fd4d42008-01-19 20:11:25 +00004053 /* If the update-hook will be invoked, set iKey to the rowid of the
4054 ** row being deleted.
4055 */
4056 if( db->xUpdateCallback && pOp->p4.z ){
4057 assert( pC->isTable );
4058 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
4059 iKey = pC->lastRowid;
4060 }
danielk197794eb6a12005-12-15 15:22:08 +00004061
drh9a65f2c2009-06-22 19:05:40 +00004062 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4063 ** OP_Column on the same table without any intervening operations that
4064 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4065 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4066 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4067 ** to guard against future changes to the code generator.
4068 **/
4069 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004070 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004071 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4072
drh7f751222009-03-17 22:33:00 +00004073 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00004074 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004075 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004076
drh91fd4d42008-01-19 20:11:25 +00004077 /* Invoke the update-hook if required. */
4078 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
4079 const char *zDb = db->aDb[pC->iDb].zName;
4080 const char *zTbl = pOp->p4.z;
4081 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4082 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004083 }
danielk1977b28af712004-06-21 06:50:26 +00004084 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004085 break;
4086}
drhb7f1d9a2009-09-08 02:27:58 +00004087/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004088**
drhb7f1d9a2009-09-08 02:27:58 +00004089** The value of the change counter is copied to the database handle
4090** change counter (returned by subsequent calls to sqlite3_changes()).
4091** Then the VMs internal change counter resets to 0.
4092** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004093*/
drh9cbf3422008-01-17 16:22:13 +00004094case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004095 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004096 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004097 break;
4098}
4099
dan5134d132011-09-02 10:31:11 +00004100/* Opcode: SorterCompare P1 P2 P3
4101**
4102** P1 is a sorter cursor. This instruction compares the record blob in
4103** register P3 with the entry that the sorter cursor currently points to.
4104** If, excluding the rowid fields at the end, the two records are a match,
4105** fall through to the next instruction. Otherwise, jump to instruction P2.
4106*/
4107case OP_SorterCompare: {
4108 VdbeCursor *pC;
4109 int res;
4110
4111 pC = p->apCsr[pOp->p1];
4112 assert( isSorter(pC) );
4113 pIn3 = &aMem[pOp->p3];
4114 rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
4115 if( res ){
4116 pc = pOp->p2-1;
4117 }
4118 break;
4119};
4120
4121/* Opcode: SorterData P1 P2 * * *
4122**
4123** Write into register P2 the current sorter data for sorter cursor P1.
4124*/
4125case OP_SorterData: {
4126 VdbeCursor *pC;
drhca892a72011-09-03 00:17:51 +00004127#ifndef SQLITE_OMIT_MERGE_SORT
dan5134d132011-09-02 10:31:11 +00004128 pOut = &aMem[pOp->p2];
4129 pC = p->apCsr[pOp->p1];
4130 assert( pC->isSorter );
4131 rc = sqlite3VdbeSorterRowkey(pC, pOut);
drhca892a72011-09-03 00:17:51 +00004132#else
4133 pOp->opcode = OP_RowKey;
4134 pc--;
4135#endif
dan5134d132011-09-02 10:31:11 +00004136 break;
4137}
4138
drh98757152008-01-09 23:04:12 +00004139/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00004140**
drh98757152008-01-09 23:04:12 +00004141** Write into register P2 the complete row data for cursor P1.
4142** There is no interpretation of the data.
4143** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004144** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004145**
drhde4fcfd2008-01-19 23:50:26 +00004146** If the P1 cursor must be pointing to a valid row (not a NULL row)
4147** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004148*/
drh98757152008-01-09 23:04:12 +00004149/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00004150**
drh98757152008-01-09 23:04:12 +00004151** Write into register P2 the complete row key for cursor P1.
4152** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004153** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004154** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004155**
drhde4fcfd2008-01-19 23:50:26 +00004156** If the P1 cursor must be pointing to a valid row (not a NULL row)
4157** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004158*/
danielk1977a7a8e142008-02-13 18:25:27 +00004159case OP_RowKey:
4160case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004161 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004162 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004163 u32 n;
drh856c1032009-06-02 15:21:42 +00004164 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004165
drha6c2ed92009-11-14 23:22:23 +00004166 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004167 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004168
drhf0863fe2005-06-12 21:35:51 +00004169 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004170 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4171 pC = p->apCsr[pOp->p1];
dan5134d132011-09-02 10:31:11 +00004172 assert( pC->isSorter==0 );
drhc6aff302011-09-01 15:32:47 +00004173 assert( pC->isTable || pOp->opcode!=OP_RowData );
drhf0863fe2005-06-12 21:35:51 +00004174 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004175 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004176 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004177 assert( pC->pseudoTableReg==0 );
drhca892a72011-09-03 00:17:51 +00004178 assert( !pC->isSorter );
drhde4fcfd2008-01-19 23:50:26 +00004179 assert( pC->pCursor!=0 );
4180 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004181 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004182
4183 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4184 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4185 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4186 ** a no-op and can never fail. But we leave it in place as a safety.
4187 */
4188 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004189 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004190 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4191
drhde4fcfd2008-01-19 23:50:26 +00004192 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00004193 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004194 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004195 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004196 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004197 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004198 }
drhbfb19dc2009-06-05 16:46:53 +00004199 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004200 }else{
drhb07028f2011-10-14 21:49:18 +00004201 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004202 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004203 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004204 goto too_big;
4205 }
drhde4fcfd2008-01-19 23:50:26 +00004206 }
danielk1977a7a8e142008-02-13 18:25:27 +00004207 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4208 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004209 }
danielk1977a7a8e142008-02-13 18:25:27 +00004210 pOut->n = n;
4211 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00004212 if( pC->isIndex ){
4213 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4214 }else{
4215 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004216 }
danielk197796cb76f2008-01-04 13:24:28 +00004217 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004218 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00004219 break;
4220}
4221
drh2133d822008-01-03 18:44:59 +00004222/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004223**
drh2133d822008-01-03 18:44:59 +00004224** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004225** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004226**
4227** P1 can be either an ordinary table or a virtual table. There used to
4228** be a separate OP_VRowid opcode for use with virtual tables, but this
4229** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004230*/
drh4c583122008-01-04 22:01:03 +00004231case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004232 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004233 i64 v;
drh856c1032009-06-02 15:21:42 +00004234 sqlite3_vtab *pVtab;
4235 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004236
drh653b82a2009-06-22 11:10:47 +00004237 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4238 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004239 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004240 assert( pC->pseudoTableReg==0 );
drh044925b2009-04-22 17:15:02 +00004241 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004242 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004243 break;
4244 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004245 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004246#ifndef SQLITE_OMIT_VIRTUALTABLE
4247 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004248 pVtab = pC->pVtabCursor->pVtab;
4249 pModule = pVtab->pModule;
4250 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004251 rc = pModule->xRowid(pC->pVtabCursor, &v);
drhb9755982010-07-24 16:34:37 +00004252 importVtabErrMsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004253#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004254 }else{
drh6be240e2009-07-14 02:33:02 +00004255 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004256 rc = sqlite3VdbeCursorMoveto(pC);
4257 if( rc ) goto abort_due_to_error;
4258 if( pC->rowidIsValid ){
4259 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004260 }else{
drhc27ae612009-07-14 18:35:44 +00004261 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4262 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004263 }
drh5e00f6c2001-09-13 13:46:56 +00004264 }
drh4c583122008-01-04 22:01:03 +00004265 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004266 break;
4267}
4268
drh9cbf3422008-01-17 16:22:13 +00004269/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004270**
4271** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004272** that occur while the cursor is on the null row will always
4273** write a NULL.
drh17f71932002-02-21 12:01:27 +00004274*/
drh9cbf3422008-01-17 16:22:13 +00004275case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004276 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004277
drh653b82a2009-06-22 11:10:47 +00004278 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4279 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004280 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004281 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004282 pC->rowidIsValid = 0;
dana205a482011-08-27 18:48:57 +00004283 assert( pC->pCursor || pC->pVtabCursor );
danielk1977be51a652008-10-08 17:58:48 +00004284 if( pC->pCursor ){
4285 sqlite3BtreeClearCursor(pC->pCursor);
4286 }
drh17f71932002-02-21 12:01:27 +00004287 break;
4288}
4289
drh9cbf3422008-01-17 16:22:13 +00004290/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004291**
drhf0863fe2005-06-12 21:35:51 +00004292** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004293** will refer to the last entry in the database table or index.
4294** If the table or index is empty and P2>0, then jump immediately to P2.
4295** If P2 is 0 or if the table or index is not empty, fall through
4296** to the following instruction.
4297*/
drh9cbf3422008-01-17 16:22:13 +00004298case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004299 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004300 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004301 int res;
drh9562b552002-02-19 15:00:07 +00004302
drh653b82a2009-06-22 11:10:47 +00004303 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4304 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004305 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004306 pCrsr = pC->pCursor;
dana205a482011-08-27 18:48:57 +00004307 if( NEVER(pCrsr==0) ){
drh9a65f2c2009-06-22 19:05:40 +00004308 res = 1;
4309 }else{
4310 rc = sqlite3BtreeLast(pCrsr, &res);
4311 }
drh9c1905f2008-12-10 22:32:56 +00004312 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004313 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004314 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004315 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004316 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004317 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004318 }
4319 break;
4320}
4321
drh0342b1f2005-09-01 03:07:44 +00004322
drh9cbf3422008-01-17 16:22:13 +00004323/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004324**
4325** This opcode does exactly the same thing as OP_Rewind except that
4326** it increments an undocumented global variable used for testing.
4327**
4328** Sorting is accomplished by writing records into a sorting index,
4329** then rewinding that index and playing it back from beginning to
4330** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4331** rewinding so that the global variable will be incremented and
4332** regression tests can determine whether or not the optimizer is
4333** correctly optimizing out sorts.
4334*/
drhc6aff302011-09-01 15:32:47 +00004335case OP_SorterSort: /* jump */
drhca892a72011-09-03 00:17:51 +00004336#ifdef SQLITE_OMIT_MERGE_SORT
4337 pOp->opcode = OP_Sort;
4338#endif
drh9cbf3422008-01-17 16:22:13 +00004339case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004340#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004341 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004342 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004343#endif
drhd1d38482008-10-07 23:46:38 +00004344 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00004345 /* Fall through into OP_Rewind */
4346}
drh9cbf3422008-01-17 16:22:13 +00004347/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004348**
drhf0863fe2005-06-12 21:35:51 +00004349** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004350** will refer to the first entry in the database table or index.
4351** If the table or index is empty and P2>0, then jump immediately to P2.
4352** If P2 is 0 or if the table or index is not empty, fall through
4353** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004354*/
drh9cbf3422008-01-17 16:22:13 +00004355case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004356 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004357 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004358 int res;
drh5e00f6c2001-09-13 13:46:56 +00004359
drh653b82a2009-06-22 11:10:47 +00004360 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4361 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004362 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004363 assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004364 res = 1;
dan689ab892011-08-12 15:02:00 +00004365 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004366 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004367 }else{
4368 pCrsr = pC->pCursor;
4369 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004370 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004371 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004372 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004373 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004374 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004375 }
drh9c1905f2008-12-10 22:32:56 +00004376 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004377 assert( pOp->p2>0 && pOp->p2<p->nOp );
4378 if( res ){
drhf4dada72004-05-11 09:57:35 +00004379 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004380 }
4381 break;
4382}
4383
dana205a482011-08-27 18:48:57 +00004384/* Opcode: Next P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004385**
4386** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004387** table or index. If there are no more key/value pairs then fall through
4388** to the following instruction. But if the cursor advance was successful,
4389** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004390**
drh60a713c2008-01-21 16:22:45 +00004391** The P1 cursor must be for a real table, not a pseudo-table.
4392**
dana205a482011-08-27 18:48:57 +00004393** P4 is always of type P4_ADVANCE. The function pointer points to
4394** sqlite3BtreeNext().
4395**
drhafc266a2010-03-31 17:47:44 +00004396** If P5 is positive and the jump is taken, then event counter
4397** number P5-1 in the prepared statement is incremented.
4398**
drhc045ec52002-12-04 20:01:06 +00004399** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004400*/
drhafc266a2010-03-31 17:47:44 +00004401/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004402**
4403** Back up cursor P1 so that it points to the previous key/data pair in its
4404** table or index. If there is no previous key/value pairs then fall through
4405** to the following instruction. But if the cursor backup was successful,
4406** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004407**
4408** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004409**
dana205a482011-08-27 18:48:57 +00004410** P4 is always of type P4_ADVANCE. The function pointer points to
4411** sqlite3BtreePrevious().
4412**
drhafc266a2010-03-31 17:47:44 +00004413** If P5 is positive and the jump is taken, then event counter
4414** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004415*/
drhc6aff302011-09-01 15:32:47 +00004416case OP_SorterNext: /* jump */
drhca892a72011-09-03 00:17:51 +00004417#ifdef SQLITE_OMIT_MERGE_SORT
4418 pOp->opcode = OP_Next;
4419#endif
drh9cbf3422008-01-17 16:22:13 +00004420case OP_Prev: /* jump */
4421case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004422 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004423 int res;
drh8721ce42001-11-07 14:22:00 +00004424
drhcaec2f12003-01-07 02:47:47 +00004425 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004426 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhafc266a2010-03-31 17:47:44 +00004427 assert( pOp->p5<=ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004428 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004429 if( pC==0 ){
4430 break; /* See ticket #2273 */
4431 }
drhc6aff302011-09-01 15:32:47 +00004432 assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
dan689ab892011-08-12 15:02:00 +00004433 if( isSorter(pC) ){
dan5134d132011-09-02 10:31:11 +00004434 assert( pOp->opcode==OP_SorterNext );
dana20fde62011-07-12 14:28:05 +00004435 rc = sqlite3VdbeSorterNext(db, pC, &res);
4436 }else{
dana20fde62011-07-12 14:28:05 +00004437 res = 1;
4438 assert( pC->deferredMoveto==0 );
dana205a482011-08-27 18:48:57 +00004439 assert( pC->pCursor );
4440 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4441 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4442 rc = pOp->p4.xAdvance(pC->pCursor, &res);
drh9a65f2c2009-06-22 19:05:40 +00004443 }
drh9c1905f2008-12-10 22:32:56 +00004444 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004445 pC->cacheStatus = CACHE_STALE;
4446 if( res==0 ){
4447 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004448 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004449#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004450 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004451#endif
drh8721ce42001-11-07 14:22:00 +00004452 }
drhf0863fe2005-06-12 21:35:51 +00004453 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004454 break;
4455}
4456
danielk1977de630352009-05-04 11:42:29 +00004457/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004458**
drhef8662b2011-06-20 21:47:58 +00004459** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004460** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004461** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004462**
drhaa9b8962008-01-08 02:57:55 +00004463** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004464** insert is likely to be an append.
4465**
drhf0863fe2005-06-12 21:35:51 +00004466** This instruction only works for indices. The equivalent instruction
4467** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004468*/
drhca892a72011-09-03 00:17:51 +00004469case OP_SorterInsert: /* in2 */
4470#ifdef SQLITE_OMIT_MERGE_SORT
4471 pOp->opcode = OP_IdxInsert;
4472#endif
drh9cbf3422008-01-17 16:22:13 +00004473case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004474 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004475 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004476 int nKey;
4477 const char *zKey;
4478
drh653b82a2009-06-22 11:10:47 +00004479 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4480 pC = p->apCsr[pOp->p1];
4481 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004482 assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004483 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004484 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004485 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004486 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004487 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004488 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004489 if( rc==SQLITE_OK ){
dan5134d132011-09-02 10:31:11 +00004490 if( isSorter(pC) ){
4491 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4492 }else{
4493 nKey = pIn2->n;
4494 zKey = pIn2->z;
dan1e74e602011-08-06 12:01:58 +00004495 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4496 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
dan5134d132011-09-02 10:31:11 +00004497 );
dan1e74e602011-08-06 12:01:58 +00004498 assert( pC->deferredMoveto==0 );
dan5134d132011-09-02 10:31:11 +00004499 pC->cacheStatus = CACHE_STALE;
dan1e74e602011-08-06 12:01:58 +00004500 }
danielk1977d908f5a2007-05-11 07:08:28 +00004501 }
drh5e00f6c2001-09-13 13:46:56 +00004502 }
drh5e00f6c2001-09-13 13:46:56 +00004503 break;
4504}
4505
drhd1d38482008-10-07 23:46:38 +00004506/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004507**
drhe14006d2008-03-25 17:23:32 +00004508** The content of P3 registers starting at register P2 form
4509** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004510** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004511*/
drhe14006d2008-03-25 17:23:32 +00004512case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004513 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004514 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004515 int res;
4516 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004517
drhe14006d2008-03-25 17:23:32 +00004518 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004519 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004520 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4521 pC = p->apCsr[pOp->p1];
4522 assert( pC!=0 );
4523 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004524 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004525 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004526 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004527 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004528 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004529#ifdef SQLITE_DEBUG
4530 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4531#endif
drhe63d9992008-08-13 19:11:48 +00004532 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004533 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004534 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004535 }
drh9188b382004-05-14 21:12:22 +00004536 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004537 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004538 }
drh5e00f6c2001-09-13 13:46:56 +00004539 break;
4540}
4541
drh2133d822008-01-03 18:44:59 +00004542/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004543**
drh2133d822008-01-03 18:44:59 +00004544** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004545** the end of the index key pointed to by cursor P1. This integer should be
4546** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004547**
drh9437bd22009-02-01 00:29:56 +00004548** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004549*/
drh4c583122008-01-04 22:01:03 +00004550case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004551 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004552 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004553 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004554
drh653b82a2009-06-22 11:10:47 +00004555 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4556 pC = p->apCsr[pOp->p1];
4557 assert( pC!=0 );
4558 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004559 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004560 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004561 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004562 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004563 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004564 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004565 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004566 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004567 if( rc!=SQLITE_OK ){
4568 goto abort_due_to_error;
4569 }
drh4c583122008-01-04 22:01:03 +00004570 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004571 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004572 }
drh8721ce42001-11-07 14:22:00 +00004573 }
4574 break;
4575}
4576
danielk197761dd5832008-04-18 11:31:12 +00004577/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004578**
danielk197761dd5832008-04-18 11:31:12 +00004579** The P4 register values beginning with P3 form an unpacked index
4580** key that omits the ROWID. Compare this key value against the index
4581** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004582**
danielk197761dd5832008-04-18 11:31:12 +00004583** If the P1 index entry is greater than or equal to the key value
4584** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004585**
danielk197761dd5832008-04-18 11:31:12 +00004586** If P5 is non-zero then the key value is increased by an epsilon
4587** prior to the comparison. This make the opcode work like IdxGT except
4588** that if the key from register P3 is a prefix of the key in the cursor,
4589** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004590*/
drh3bb9b932010-08-06 02:10:00 +00004591/* Opcode: IdxLT P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004592**
danielk197761dd5832008-04-18 11:31:12 +00004593** The P4 register values beginning with P3 form an unpacked index
4594** key that omits the ROWID. Compare this key value against the index
4595** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004596**
danielk197761dd5832008-04-18 11:31:12 +00004597** If the P1 index entry is less than the key value then jump to P2.
4598** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004599**
danielk197761dd5832008-04-18 11:31:12 +00004600** If P5 is non-zero then the key value is increased by an epsilon prior
4601** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004602*/
drh93952eb2009-11-13 19:43:43 +00004603case OP_IdxLT: /* jump */
4604case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004605 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004606 int res;
4607 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004608
drh653b82a2009-06-22 11:10:47 +00004609 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4610 pC = p->apCsr[pOp->p1];
4611 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004612 assert( pC->isOrdered );
drh9a65f2c2009-06-22 19:05:40 +00004613 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004614 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004615 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004616 assert( pOp->p4type==P4_INT32 );
4617 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004618 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004619 if( pOp->p5 ){
4620 r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
4621 }else{
4622 r.flags = UNPACKED_IGNORE_ROWID;
4623 }
drha6c2ed92009-11-14 23:22:23 +00004624 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004625#ifdef SQLITE_DEBUG
4626 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4627#endif
drhe63d9992008-08-13 19:11:48 +00004628 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004629 if( pOp->opcode==OP_IdxLT ){
4630 res = -res;
drha05a7222008-01-19 03:35:58 +00004631 }else{
4632 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004633 res++;
4634 }
4635 if( res>0 ){
4636 pc = pOp->p2 - 1 ;
4637 }
4638 }
4639 break;
4640}
4641
drh98757152008-01-09 23:04:12 +00004642/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004643**
4644** Delete an entire database table or index whose root page in the database
4645** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004646**
drh98757152008-01-09 23:04:12 +00004647** The table being destroyed is in the main database file if P3==0. If
4648** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004649** that is used to store tables create using CREATE TEMPORARY TABLE.
4650**
drh205f48e2004-11-05 00:43:11 +00004651** If AUTOVACUUM is enabled then it is possible that another root page
4652** might be moved into the newly deleted root page in order to keep all
4653** root pages contiguous at the beginning of the database. The former
4654** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004655** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004656** movement was required (because the table being dropped was already
4657** the last one in the database) then a zero is stored in register P2.
4658** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004659**
drhb19a2bc2001-09-16 00:13:26 +00004660** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004661*/
drh98757152008-01-09 23:04:12 +00004662case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004663 int iMoved;
drh3765df42006-06-28 18:18:09 +00004664 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004665 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004666 int iDb;
4667#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004668 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004669 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danielk1977212b2182006-06-23 14:32:08 +00004670 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4671 iCnt++;
4672 }
4673 }
drh3765df42006-06-28 18:18:09 +00004674#else
4675 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004676#endif
drh3c657212009-11-17 23:59:58 +00004677 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004678 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004679 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004680 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004681 }else{
drh856c1032009-06-02 15:21:42 +00004682 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004683 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004684 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh98757152008-01-09 23:04:12 +00004685 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004686 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004687 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004688#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004689 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004690 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4691 /* All OP_Destroy operations occur on the same btree */
4692 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4693 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004694 }
drh3765df42006-06-28 18:18:09 +00004695#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004696 }
drh5e00f6c2001-09-13 13:46:56 +00004697 break;
4698}
4699
danielk1977c7af4842008-10-27 13:59:33 +00004700/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004701**
4702** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004703** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004704** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004705**
drhf57b3392001-10-08 13:22:32 +00004706** The table being clear is in the main database file if P2==0. If
4707** P2==1 then the table to be clear is in the auxiliary database file
4708** that is used to store tables create using CREATE TEMPORARY TABLE.
4709**
shanebe217792009-03-05 04:20:31 +00004710** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004711** intkey table (an SQL table, not an index). In this case the row change
4712** count is incremented by the number of rows in the table being cleared.
4713** If P3 is greater than zero, then the value stored in register P3 is
4714** also incremented by the number of rows in the table being cleared.
4715**
drhb19a2bc2001-09-16 00:13:26 +00004716** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004717*/
drh9cbf3422008-01-17 16:22:13 +00004718case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004719 int nChange;
4720
4721 nChange = 0;
drhdddd7792011-04-03 18:19:25 +00004722 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004723 rc = sqlite3BtreeClearTable(
4724 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4725 );
4726 if( pOp->p3 ){
4727 p->nChange += nChange;
4728 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004729 assert( memIsValid(&aMem[pOp->p3]) );
4730 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004731 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004732 }
4733 }
drh5edc3122001-09-13 21:53:09 +00004734 break;
4735}
4736
drh4c583122008-01-04 22:01:03 +00004737/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004738**
drh4c583122008-01-04 22:01:03 +00004739** Allocate a new table in the main database file if P1==0 or in the
4740** auxiliary database file if P1==1 or in an attached database if
4741** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004742** register P2
drh5b2fd562001-09-13 15:21:31 +00004743**
drhc6b52df2002-01-04 03:09:29 +00004744** The difference between a table and an index is this: A table must
4745** have a 4-byte integer key and can have arbitrary data. An index
4746** has an arbitrary key but no data.
4747**
drhb19a2bc2001-09-16 00:13:26 +00004748** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004749*/
drh4c583122008-01-04 22:01:03 +00004750/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004751**
drh4c583122008-01-04 22:01:03 +00004752** Allocate a new index in the main database file if P1==0 or in the
4753** auxiliary database file if P1==1 or in an attached database if
4754** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004755** register P2.
drhf57b3392001-10-08 13:22:32 +00004756**
drhc6b52df2002-01-04 03:09:29 +00004757** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004758*/
drh4c583122008-01-04 22:01:03 +00004759case OP_CreateIndex: /* out2-prerelease */
4760case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004761 int pgno;
drhf328bc82004-05-10 23:29:49 +00004762 int flags;
drh234c39d2004-07-24 03:30:47 +00004763 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004764
4765 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004766 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004767 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004768 pDb = &db->aDb[pOp->p1];
4769 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004770 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004771 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004772 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004773 }else{
drhd4187c72010-08-30 22:15:45 +00004774 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004775 }
drh234c39d2004-07-24 03:30:47 +00004776 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004777 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004778 break;
4779}
4780
drh22645842011-03-24 01:34:03 +00004781/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004782**
4783** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004784** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004785**
4786** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004787** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004788*/
drh9cbf3422008-01-17 16:22:13 +00004789case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004790 int iDb;
4791 const char *zMaster;
4792 char *zSql;
4793 InitData initData;
4794
drhbdaec522011-04-04 00:14:43 +00004795 /* Any prepared statement that invokes this opcode will hold mutexes
4796 ** on every btree. This is a prerequisite for invoking
4797 ** sqlite3InitCallback().
4798 */
4799#ifdef SQLITE_DEBUG
4800 for(iDb=0; iDb<db->nDb; iDb++){
4801 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4802 }
4803#endif
drhbdaec522011-04-04 00:14:43 +00004804
drh856c1032009-06-02 15:21:42 +00004805 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004806 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004807 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004808 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004809 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004810 initData.db = db;
4811 initData.iDb = pOp->p1;
4812 initData.pzErrMsg = &p->zErrMsg;
4813 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004814 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004815 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4816 if( zSql==0 ){
4817 rc = SQLITE_NOMEM;
4818 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004819 assert( db->init.busy==0 );
4820 db->init.busy = 1;
4821 initData.rc = SQLITE_OK;
4822 assert( !db->mallocFailed );
4823 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4824 if( rc==SQLITE_OK ) rc = initData.rc;
4825 sqlite3DbFree(db, zSql);
4826 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004827 }
drh3c23a882007-01-09 14:01:13 +00004828 }
danielk1977261919c2005-12-06 12:52:59 +00004829 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004830 goto no_mem;
4831 }
drh234c39d2004-07-24 03:30:47 +00004832 break;
4833}
4834
drh8bfdf722009-06-19 14:06:03 +00004835#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004836/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004837**
4838** Read the sqlite_stat1 table for database P1 and load the content
4839** of that table into the internal index hash table. This will cause
4840** the analysis to be used when preparing all subsequent queries.
4841*/
drh9cbf3422008-01-17 16:22:13 +00004842case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004843 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4844 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004845 break;
4846}
drh8bfdf722009-06-19 14:06:03 +00004847#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004848
drh98757152008-01-09 23:04:12 +00004849/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004850**
4851** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004852** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004853** is dropped in order to keep the internal representation of the
4854** schema consistent with what is on disk.
4855*/
drh9cbf3422008-01-17 16:22:13 +00004856case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004857 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004858 break;
4859}
4860
drh98757152008-01-09 23:04:12 +00004861/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004862**
4863** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004864** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004865** is dropped in order to keep the internal representation of the
4866** schema consistent with what is on disk.
4867*/
drh9cbf3422008-01-17 16:22:13 +00004868case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004869 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004870 break;
4871}
4872
drh98757152008-01-09 23:04:12 +00004873/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004874**
4875** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004876** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004877** is dropped in order to keep the internal representation of the
4878** schema consistent with what is on disk.
4879*/
drh9cbf3422008-01-17 16:22:13 +00004880case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004881 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004882 break;
4883}
4884
drh234c39d2004-07-24 03:30:47 +00004885
drhb7f91642004-10-31 02:22:47 +00004886#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004887/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004888**
drh98757152008-01-09 23:04:12 +00004889** Do an analysis of the currently open database. Store in
4890** register P1 the text of an error message describing any problems.
4891** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004892**
drh98757152008-01-09 23:04:12 +00004893** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004894** At most reg(P3) errors will be reported.
4895** In other words, the analysis stops as soon as reg(P1) errors are
4896** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004897**
drh79069752004-05-22 21:30:40 +00004898** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004899** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004900** total.
drh21504322002-06-25 13:16:02 +00004901**
drh98757152008-01-09 23:04:12 +00004902** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004903** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004904**
drh1dcdbc02007-01-27 02:24:54 +00004905** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004906*/
drhaaab5722002-02-19 13:39:21 +00004907case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004908 int nRoot; /* Number of tables to check. (Number of root pages.) */
4909 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4910 int j; /* Loop counter */
4911 int nErr; /* Number of errors reported */
4912 char *z; /* Text of the error report */
4913 Mem *pnErr; /* Register keeping track of errors remaining */
4914
4915 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00004916 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00004917 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004918 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00004919 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00004920 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00004921 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00004922 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00004923 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00004924 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00004925 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00004926 }
4927 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00004928 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004929 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00004930 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00004931 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00004932 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00004933 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00004934 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00004935 if( nErr==0 ){
4936 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00004937 }else if( z==0 ){
4938 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00004939 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00004940 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00004941 }
drhb7654112008-01-12 12:48:07 +00004942 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00004943 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00004944 break;
4945}
drhb7f91642004-10-31 02:22:47 +00004946#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004947
drh3d4501e2008-12-04 20:40:10 +00004948/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004949**
drh3d4501e2008-12-04 20:40:10 +00004950** Insert the integer value held by register P2 into a boolean index
4951** held in register P1.
4952**
4953** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00004954*/
drh93952eb2009-11-13 19:43:43 +00004955case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00004956 pIn1 = &aMem[pOp->p1];
4957 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00004958 assert( (pIn2->flags & MEM_Int)!=0 );
4959 if( (pIn1->flags & MEM_RowSet)==0 ){
4960 sqlite3VdbeMemSetRowSet(pIn1);
4961 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00004962 }
drh93952eb2009-11-13 19:43:43 +00004963 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00004964 break;
4965}
4966
4967/* Opcode: RowSetRead P1 P2 P3 * *
4968**
4969** Extract the smallest value from boolean index P1 and put that value into
4970** register P3. Or, if boolean index P1 is initially empty, leave P3
4971** unchanged and jump to instruction P2.
4972*/
drh93952eb2009-11-13 19:43:43 +00004973case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00004974 i64 val;
drh3d4501e2008-12-04 20:40:10 +00004975 CHECK_FOR_INTERRUPT;
drh3c657212009-11-17 23:59:58 +00004976 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00004977 if( (pIn1->flags & MEM_RowSet)==0
4978 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00004979 ){
4980 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00004981 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00004982 pc = pOp->p2 - 1;
4983 }else{
4984 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00004985 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00004986 }
drh5e00f6c2001-09-13 13:46:56 +00004987 break;
4988}
4989
drh1b26c7c2009-04-22 02:15:47 +00004990/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00004991**
drhade97602009-04-21 15:05:18 +00004992** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00004993** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00004994** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00004995** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00004996** next opcode.
danielk19771d461462009-04-21 09:02:45 +00004997**
drh1b26c7c2009-04-22 02:15:47 +00004998** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00004999** of integers, where each set contains no duplicates. Each set
5000** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005001** must have P4==0, the final set P4=-1. P4 must be either -1 or
5002** non-negative. For non-negative values of P4 only the lower 4
5003** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005004**
5005** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005006** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005007** (b) when P4==-1 there is no need to insert the value, as it will
5008** never be tested for, and (c) when a value that is part of set X is
5009** inserted, there is no need to search to see if the same value was
5010** previously inserted as part of set X (only if it was previously
5011** inserted as part of some other set).
5012*/
drh1b26c7c2009-04-22 02:15:47 +00005013case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005014 int iSet;
5015 int exists;
5016
drh3c657212009-11-17 23:59:58 +00005017 pIn1 = &aMem[pOp->p1];
5018 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005019 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005020 assert( pIn3->flags&MEM_Int );
5021
drh1b26c7c2009-04-22 02:15:47 +00005022 /* If there is anything other than a rowset object in memory cell P1,
5023 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005024 */
drh733bf1b2009-04-22 00:47:00 +00005025 if( (pIn1->flags & MEM_RowSet)==0 ){
5026 sqlite3VdbeMemSetRowSet(pIn1);
5027 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005028 }
5029
5030 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005031 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005032 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005033 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5034 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005035 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005036 if( exists ){
5037 pc = pOp->p2 - 1;
5038 break;
5039 }
5040 }
5041 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005042 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005043 }
5044 break;
5045}
5046
drh5e00f6c2001-09-13 13:46:56 +00005047
danielk197793758c82005-01-21 08:13:14 +00005048#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005049
5050/* Opcode: Program P1 P2 P3 P4 *
5051**
dan76d462e2009-08-30 11:42:51 +00005052** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005053**
dan76d462e2009-08-30 11:42:51 +00005054** P1 contains the address of the memory cell that contains the first memory
5055** cell in an array of values used as arguments to the sub-program. P2
5056** contains the address to jump to if the sub-program throws an IGNORE
5057** exception using the RAISE() function. Register P3 contains the address
5058** of a memory cell in this (the parent) VM that is used to allocate the
5059** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005060**
5061** P4 is a pointer to the VM containing the trigger program.
5062*/
dan76d462e2009-08-30 11:42:51 +00005063case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005064 int nMem; /* Number of memory registers for sub-program */
5065 int nByte; /* Bytes of runtime space required for sub-program */
5066 Mem *pRt; /* Register to allocate runtime space */
5067 Mem *pMem; /* Used to iterate through memory cells */
5068 Mem *pEnd; /* Last memory cell in new array */
5069 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5070 SubProgram *pProgram; /* Sub-program to execute */
5071 void *t; /* Token identifying trigger */
5072
5073 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005074 pRt = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005075 assert( memIsValid(pRt) );
dan165921a2009-08-28 18:53:45 +00005076 assert( pProgram->nOp>0 );
5077
dan1da40a32009-09-19 17:00:31 +00005078 /* If the p5 flag is clear, then recursive invocation of triggers is
5079 ** disabled for backwards compatibility (p5 is set if this sub-program
5080 ** is really a trigger, not a foreign key action, and the flag set
5081 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005082 **
5083 ** It is recursive invocation of triggers, at the SQL level, that is
5084 ** disabled. In some cases a single trigger may generate more than one
5085 ** SubProgram (if the trigger may be executed with more than one different
5086 ** ON CONFLICT algorithm). SubProgram structures associated with a
5087 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005088 ** variable. */
5089 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005090 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005091 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5092 if( pFrame ) break;
5093 }
5094
danf5894502009-10-07 18:41:19 +00005095 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005096 rc = SQLITE_ERROR;
5097 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5098 break;
5099 }
5100
5101 /* Register pRt is used to store the memory required to save the state
5102 ** of the current program, and the memory required at runtime to execute
5103 ** the trigger program. If this trigger has been fired before, then pRt
5104 ** is already allocated. Otherwise, it must be initialized. */
5105 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005106 /* SubProgram.nMem is set to the number of memory cells used by the
5107 ** program stored in SubProgram.aOp. As well as these, one memory
5108 ** cell is required for each cursor used by the program. Set local
5109 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5110 */
dan65a7cd12009-09-01 12:16:01 +00005111 nMem = pProgram->nMem + pProgram->nCsr;
5112 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005113 + nMem * sizeof(Mem)
5114 + pProgram->nCsr * sizeof(VdbeCursor *);
5115 pFrame = sqlite3DbMallocZero(db, nByte);
5116 if( !pFrame ){
5117 goto no_mem;
5118 }
5119 sqlite3VdbeMemRelease(pRt);
5120 pRt->flags = MEM_Frame;
5121 pRt->u.pFrame = pFrame;
5122
5123 pFrame->v = p;
5124 pFrame->nChildMem = nMem;
5125 pFrame->nChildCsr = pProgram->nCsr;
5126 pFrame->pc = pc;
5127 pFrame->aMem = p->aMem;
5128 pFrame->nMem = p->nMem;
5129 pFrame->apCsr = p->apCsr;
5130 pFrame->nCursor = p->nCursor;
5131 pFrame->aOp = p->aOp;
5132 pFrame->nOp = p->nOp;
5133 pFrame->token = pProgram->token;
5134
5135 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5136 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
5137 pMem->flags = MEM_Null;
5138 pMem->db = db;
5139 }
5140 }else{
5141 pFrame = pRt->u.pFrame;
5142 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5143 assert( pProgram->nCsr==pFrame->nChildCsr );
5144 assert( pc==pFrame->pc );
5145 }
5146
5147 p->nFrame++;
5148 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005149 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005150 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005151 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005152 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005153 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005154 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005155 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005156 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005157 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005158 p->nOp = pProgram->nOp;
5159 pc = -1;
5160
5161 break;
5162}
5163
dan76d462e2009-08-30 11:42:51 +00005164/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005165**
dan76d462e2009-08-30 11:42:51 +00005166** This opcode is only ever present in sub-programs called via the
5167** OP_Program instruction. Copy a value currently stored in a memory
5168** cell of the calling (parent) frame to cell P2 in the current frames
5169** address space. This is used by trigger programs to access the new.*
5170** and old.* values.
dan165921a2009-08-28 18:53:45 +00005171**
dan76d462e2009-08-30 11:42:51 +00005172** The address of the cell in the parent frame is determined by adding
5173** the value of the P1 argument to the value of the P1 argument to the
5174** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005175*/
dan76d462e2009-08-30 11:42:51 +00005176case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005177 VdbeFrame *pFrame;
5178 Mem *pIn;
5179 pFrame = p->pFrame;
5180 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005181 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5182 break;
5183}
5184
danielk197793758c82005-01-21 08:13:14 +00005185#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005186
dan1da40a32009-09-19 17:00:31 +00005187#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005188/* Opcode: FkCounter P1 P2 * * *
dan1da40a32009-09-19 17:00:31 +00005189**
dan0ff297e2009-09-25 17:03:14 +00005190** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5191** If P1 is non-zero, the database constraint counter is incremented
5192** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005193** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005194*/
dan32b09f22009-09-23 17:29:59 +00005195case OP_FkCounter: {
dan0ff297e2009-09-25 17:03:14 +00005196 if( pOp->p1 ){
5197 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005198 }else{
dan0ff297e2009-09-25 17:03:14 +00005199 p->nFkConstraint += pOp->p2;
5200 }
5201 break;
5202}
5203
5204/* Opcode: FkIfZero P1 P2 * * *
5205**
5206** This opcode tests if a foreign key constraint-counter is currently zero.
5207** If so, jump to instruction P2. Otherwise, fall through to the next
5208** instruction.
5209**
5210** If P1 is non-zero, then the jump is taken if the database constraint-counter
5211** is zero (the one that counts deferred constraint violations). If P1 is
5212** zero, the jump is taken if the statement constraint-counter is zero
5213** (immediate foreign key constraint violations).
5214*/
5215case OP_FkIfZero: { /* jump */
5216 if( pOp->p1 ){
5217 if( db->nDeferredCons==0 ) pc = pOp->p2-1;
5218 }else{
5219 if( p->nFkConstraint==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005220 }
dan1da40a32009-09-19 17:00:31 +00005221 break;
5222}
5223#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5224
drh205f48e2004-11-05 00:43:11 +00005225#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005226/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00005227**
dan76d462e2009-08-30 11:42:51 +00005228** P1 is a register in the root frame of this VM (the root frame is
5229** different from the current frame if this instruction is being executed
5230** within a sub-program). Set the value of register P1 to the maximum of
5231** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005232**
5233** This instruction throws an error if the memory cell is not initially
5234** an integer.
5235*/
dan76d462e2009-08-30 11:42:51 +00005236case OP_MemMax: { /* in2 */
5237 Mem *pIn1;
5238 VdbeFrame *pFrame;
5239 if( p->pFrame ){
5240 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5241 pIn1 = &pFrame->aMem[pOp->p1];
5242 }else{
drha6c2ed92009-11-14 23:22:23 +00005243 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005244 }
drh2b4ded92010-09-27 21:09:31 +00005245 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005246 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005247 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005248 sqlite3VdbeMemIntegerify(pIn2);
5249 if( pIn1->u.i<pIn2->u.i){
5250 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005251 }
5252 break;
5253}
5254#endif /* SQLITE_OMIT_AUTOINCREMENT */
5255
drh98757152008-01-09 23:04:12 +00005256/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00005257**
drh98757152008-01-09 23:04:12 +00005258** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005259**
drh98757152008-01-09 23:04:12 +00005260** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005261** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005262*/
drh9cbf3422008-01-17 16:22:13 +00005263case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005264 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005265 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005266 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005267 pc = pOp->p2 - 1;
5268 }
5269 break;
5270}
5271
drh98757152008-01-09 23:04:12 +00005272/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00005273**
drh98757152008-01-09 23:04:12 +00005274** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005275**
drh98757152008-01-09 23:04:12 +00005276** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005277** not contain an integer. An assertion fault will result if you try.
5278*/
drh9cbf3422008-01-17 16:22:13 +00005279case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005280 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005281 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005282 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005283 pc = pOp->p2 - 1;
5284 }
5285 break;
5286}
5287
drh9b918ed2009-11-12 03:13:26 +00005288/* Opcode: IfZero P1 P2 P3 * *
drhec7429a2005-10-06 16:53:14 +00005289**
drh9b918ed2009-11-12 03:13:26 +00005290** The register P1 must contain an integer. Add literal P3 to the
5291** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005292**
drh98757152008-01-09 23:04:12 +00005293** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005294** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005295*/
drh9cbf3422008-01-17 16:22:13 +00005296case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005297 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005298 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005299 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005300 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005301 pc = pOp->p2 - 1;
5302 }
5303 break;
5304}
5305
drh98757152008-01-09 23:04:12 +00005306/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00005307**
drh0bce8352002-02-28 00:41:10 +00005308** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005309** function has P5 arguments. P4 is a pointer to the FuncDef
5310** structure that specifies the function. Use register
5311** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005312**
drh98757152008-01-09 23:04:12 +00005313** The P5 arguments are taken from register P2 and its
5314** successors.
drhe5095352002-02-24 03:25:14 +00005315*/
drh9cbf3422008-01-17 16:22:13 +00005316case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005317 int n;
drhe5095352002-02-24 03:25:14 +00005318 int i;
drhc54a6172009-06-02 16:06:03 +00005319 Mem *pMem;
5320 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005321 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005322 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005323
drh856c1032009-06-02 15:21:42 +00005324 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005325 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005326 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005327 apVal = p->apArg;
5328 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005329 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005330 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005331 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005332 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005333 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005334 }
danielk19772dca4ac2008-01-03 11:50:29 +00005335 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00005336 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005337 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005338 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005339 ctx.s.flags = MEM_Null;
5340 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005341 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005342 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005343 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005344 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005345 ctx.pColl = 0;
drhe82f5d02008-10-07 19:53:14 +00005346 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005347 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005348 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005349 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005350 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005351 }
drhee9ff672010-09-03 18:50:48 +00005352 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005353 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005354 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005355 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005356 }
drhbdaec522011-04-04 00:14:43 +00005357
drh90669c12006-01-20 15:45:36 +00005358 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005359
drh5e00f6c2001-09-13 13:46:56 +00005360 break;
5361}
5362
drh98757152008-01-09 23:04:12 +00005363/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00005364**
drh13449892005-09-07 21:22:45 +00005365** Execute the finalizer function for an aggregate. P1 is
5366** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005367**
5368** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005369** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005370** argument is not used by this opcode. It is only there to disambiguate
5371** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005372** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005373** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005374*/
drh9cbf3422008-01-17 16:22:13 +00005375case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005376 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00005377 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005378 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005379 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005380 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005381 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005382 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005383 }
drh2dca8682008-03-21 17:13:13 +00005384 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005385 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005386 if( sqlite3VdbeMemTooBig(pMem) ){
5387 goto too_big;
5388 }
drh5e00f6c2001-09-13 13:46:56 +00005389 break;
5390}
5391
dan5cf53532010-05-01 16:40:20 +00005392#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005393/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005394**
5395** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005396** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005397** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5398** SQLITE_BUSY or not, respectively. Write the number of pages in the
5399** WAL after the checkpoint into mem[P3+1] and the number of pages
5400** in the WAL that have been checkpointed after the checkpoint
5401** completes into mem[P3+2]. However on an error, mem[P3+1] and
5402** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005403*/
5404case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005405 int i; /* Loop counter */
5406 int aRes[3]; /* Results */
5407 Mem *pMem; /* Write results here */
5408
5409 aRes[0] = 0;
5410 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005411 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5412 || pOp->p2==SQLITE_CHECKPOINT_FULL
5413 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5414 );
drh30aa3b92011-02-07 23:56:01 +00005415 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005416 if( rc==SQLITE_BUSY ){
5417 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005418 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005419 }
drh30aa3b92011-02-07 23:56:01 +00005420 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5421 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5422 }
dan7c246102010-04-12 19:00:29 +00005423 break;
5424};
dan5cf53532010-05-01 16:40:20 +00005425#endif
drh5e00f6c2001-09-13 13:46:56 +00005426
drhcac29a62010-07-02 19:36:52 +00005427#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005428/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005429**
5430** Change the journal mode of database P1 to P3. P3 must be one of the
5431** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5432** modes (delete, truncate, persist, off and memory), this is a simple
5433** operation. No IO is required.
5434**
5435** If changing into or out of WAL mode the procedure is more complicated.
5436**
5437** Write a string containing the final journal-mode to register P2.
5438*/
drhd80b2332010-05-01 00:59:37 +00005439case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005440 Btree *pBt; /* Btree to change journal mode of */
5441 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005442 int eNew; /* New journal mode */
5443 int eOld; /* The old journal mode */
drhd80b2332010-05-01 00:59:37 +00005444 const char *zFilename; /* Name of database file for pPager */
dane04dc882010-04-20 18:53:15 +00005445
drhd80b2332010-05-01 00:59:37 +00005446 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005447 assert( eNew==PAGER_JOURNALMODE_DELETE
5448 || eNew==PAGER_JOURNALMODE_TRUNCATE
5449 || eNew==PAGER_JOURNALMODE_PERSIST
5450 || eNew==PAGER_JOURNALMODE_OFF
5451 || eNew==PAGER_JOURNALMODE_MEMORY
5452 || eNew==PAGER_JOURNALMODE_WAL
5453 || eNew==PAGER_JOURNALMODE_QUERY
5454 );
5455 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh3ebaee92010-05-06 21:37:22 +00005456
dane04dc882010-04-20 18:53:15 +00005457 pBt = db->aDb[pOp->p1].pBt;
5458 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005459 eOld = sqlite3PagerGetJournalMode(pPager);
5460 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5461 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005462
5463#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005464 zFilename = sqlite3PagerFilename(pPager);
dane04dc882010-04-20 18:53:15 +00005465
drhd80b2332010-05-01 00:59:37 +00005466 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005467 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005468 */
5469 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005470 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005471 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005472 ){
drh0b9b4302010-06-11 17:01:24 +00005473 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005474 }
5475
drh0b9b4302010-06-11 17:01:24 +00005476 if( (eNew!=eOld)
5477 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5478 ){
5479 if( !db->autoCommit || db->activeVdbeCnt>1 ){
5480 rc = SQLITE_ERROR;
5481 sqlite3SetString(&p->zErrMsg, db,
5482 "cannot change %s wal mode from within a transaction",
5483 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5484 );
5485 break;
5486 }else{
5487
5488 if( eOld==PAGER_JOURNALMODE_WAL ){
5489 /* If leaving WAL mode, close the log file. If successful, the call
5490 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5491 ** file. An EXCLUSIVE lock may still be held on the database file
5492 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005493 */
drh0b9b4302010-06-11 17:01:24 +00005494 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005495 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005496 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005497 }
drh242c4f72010-06-22 14:49:39 +00005498 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5499 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5500 ** as an intermediate */
5501 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005502 }
5503
5504 /* Open a transaction on the database file. Regardless of the journal
5505 ** mode, this transaction always uses a rollback journal.
5506 */
5507 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5508 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005509 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005510 }
5511 }
5512 }
dan5cf53532010-05-01 16:40:20 +00005513#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005514
dand956efe2010-06-18 16:13:45 +00005515 if( rc ){
dand956efe2010-06-18 16:13:45 +00005516 eNew = eOld;
5517 }
drh0b9b4302010-06-11 17:01:24 +00005518 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005519
dane04dc882010-04-20 18:53:15 +00005520 pOut = &aMem[pOp->p2];
5521 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005522 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005523 pOut->n = sqlite3Strlen30(pOut->z);
5524 pOut->enc = SQLITE_UTF8;
5525 sqlite3VdbeChangeEncoding(pOut, encoding);
5526 break;
drhcac29a62010-07-02 19:36:52 +00005527};
5528#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005529
drhfdbcdee2007-03-27 14:44:50 +00005530#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005531/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005532**
5533** Vacuum the entire database. This opcode will cause other virtual
5534** machines to be created and run. It may not be called from within
5535** a transaction.
5536*/
drh9cbf3422008-01-17 16:22:13 +00005537case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00005538 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005539 break;
5540}
drh154d4b22006-09-21 11:02:16 +00005541#endif
drh6f8c91c2003-12-07 00:24:35 +00005542
danielk1977dddbcdc2007-04-26 14:42:34 +00005543#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005544/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005545**
5546** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005547** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005548** P2. Otherwise, fall through to the next instruction.
5549*/
drh9cbf3422008-01-17 16:22:13 +00005550case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005551 Btree *pBt;
5552
5553 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005554 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00005555 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005556 rc = sqlite3BtreeIncrVacuum(pBt);
5557 if( rc==SQLITE_DONE ){
5558 pc = pOp->p2 - 1;
5559 rc = SQLITE_OK;
5560 }
5561 break;
5562}
5563#endif
5564
drh98757152008-01-09 23:04:12 +00005565/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005566**
5567** Cause precompiled statements to become expired. An expired statement
5568** fails with an error code of SQLITE_SCHEMA if it is ever executed
5569** (via sqlite3_step()).
5570**
5571** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5572** then only the currently executing statement is affected.
5573*/
drh9cbf3422008-01-17 16:22:13 +00005574case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005575 if( !pOp->p1 ){
5576 sqlite3ExpirePreparedStatements(db);
5577 }else{
5578 p->expired = 1;
5579 }
5580 break;
5581}
5582
danielk1977c00da102006-01-07 13:21:04 +00005583#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005584/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00005585**
5586** Obtain a lock on a particular table. This instruction is only used when
5587** the shared-cache feature is enabled.
5588**
danielk197796d48e92009-06-29 06:00:37 +00005589** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005590** on which the lock is acquired. A readlock is obtained if P3==0 or
5591** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005592**
5593** P2 contains the root-page of the table to lock.
5594**
drh66a51672008-01-03 00:01:23 +00005595** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005596** used to generate an error message if the lock cannot be obtained.
5597*/
drh9cbf3422008-01-17 16:22:13 +00005598case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005599 u8 isWriteLock = (u8)pOp->p3;
5600 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5601 int p1 = pOp->p1;
5602 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005603 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005604 assert( isWriteLock==0 || isWriteLock==1 );
5605 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5606 if( (rc&0xFF)==SQLITE_LOCKED ){
5607 const char *z = pOp->p4.z;
5608 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5609 }
danielk1977c00da102006-01-07 13:21:04 +00005610 }
5611 break;
5612}
drhb9bb7c12006-06-11 23:41:55 +00005613#endif /* SQLITE_OMIT_SHARED_CACHE */
5614
5615#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005616/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005617**
danielk19773e3a84d2008-08-01 17:37:40 +00005618** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5619** xBegin method for that table.
5620**
5621** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005622** within a callback to a virtual table xSync() method. If it is, the error
5623** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005624*/
drh9cbf3422008-01-17 16:22:13 +00005625case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005626 VTable *pVTab;
5627 pVTab = pOp->p4.pVtab;
5628 rc = sqlite3VtabBegin(db, pVTab);
drhb9755982010-07-24 16:34:37 +00005629 if( pVTab ) importVtabErrMsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005630 break;
5631}
5632#endif /* SQLITE_OMIT_VIRTUALTABLE */
5633
5634#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005635/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005636**
drh66a51672008-01-03 00:01:23 +00005637** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005638** for that table.
5639*/
drh9cbf3422008-01-17 16:22:13 +00005640case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005641 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005642 break;
5643}
5644#endif /* SQLITE_OMIT_VIRTUALTABLE */
5645
5646#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005647/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005648**
drh66a51672008-01-03 00:01:23 +00005649** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005650** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005651*/
drh9cbf3422008-01-17 16:22:13 +00005652case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005653 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005654 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005655 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005656 break;
5657}
5658#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005659
drh9eff6162006-06-12 21:59:13 +00005660#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005661/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005662**
drh66a51672008-01-03 00:01:23 +00005663** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005664** P1 is a cursor number. This opcode opens a cursor to the virtual
5665** table and stores that cursor in P1.
5666*/
drh9cbf3422008-01-17 16:22:13 +00005667case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005668 VdbeCursor *pCur;
5669 sqlite3_vtab_cursor *pVtabCursor;
5670 sqlite3_vtab *pVtab;
5671 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005672
drh856c1032009-06-02 15:21:42 +00005673 pCur = 0;
5674 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005675 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005676 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005677 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005678 rc = pModule->xOpen(pVtab, &pVtabCursor);
drhb9755982010-07-24 16:34:37 +00005679 importVtabErrMsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005680 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005681 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005682 pVtabCursor->pVtab = pVtab;
5683
5684 /* Initialise vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005685 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005686 if( pCur ){
5687 pCur->pVtabCursor = pVtabCursor;
5688 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005689 }else{
drh17435752007-08-16 04:30:38 +00005690 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005691 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005692 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005693 }
drh9eff6162006-06-12 21:59:13 +00005694 break;
5695}
5696#endif /* SQLITE_OMIT_VIRTUALTABLE */
5697
5698#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005699/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005700**
5701** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5702** the filtered result set is empty.
5703**
drh66a51672008-01-03 00:01:23 +00005704** P4 is either NULL or a string that was generated by the xBestIndex
5705** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005706** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005707**
drh9eff6162006-06-12 21:59:13 +00005708** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005709** by P1. The integer query plan parameter to xFilter is stored in register
5710** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005711** xFilter method. Registers P3+2..P3+1+argc are the argc
5712** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005713** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005714**
danielk19776dbee812008-01-03 18:39:41 +00005715** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005716*/
drh9cbf3422008-01-17 16:22:13 +00005717case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005718 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005719 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005720 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005721 Mem *pQuery;
5722 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005723 sqlite3_vtab_cursor *pVtabCursor;
5724 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005725 VdbeCursor *pCur;
5726 int res;
5727 int i;
5728 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005729
drha6c2ed92009-11-14 23:22:23 +00005730 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005731 pArgc = &pQuery[1];
5732 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005733 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005734 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005735 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005736 pVtabCursor = pCur->pVtabCursor;
5737 pVtab = pVtabCursor->pVtab;
5738 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005739
drh9cbf3422008-01-17 16:22:13 +00005740 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005741 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005742 nArg = (int)pArgc->u.i;
5743 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005744
drh644a5292006-12-20 14:53:38 +00005745 /* Invoke the xFilter method */
5746 {
drh856c1032009-06-02 15:21:42 +00005747 res = 0;
5748 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005749 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005750 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005751 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005752 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005753
danielk1977be718892006-06-23 08:05:19 +00005754 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005755 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005756 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005757 importVtabErrMsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005758 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005759 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005760 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005761
danielk1977a298e902006-06-22 09:53:48 +00005762 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005763 pc = pOp->p2 - 1;
5764 }
5765 }
drh1d454a32008-01-31 19:34:51 +00005766 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005767
drh9eff6162006-06-12 21:59:13 +00005768 break;
5769}
5770#endif /* SQLITE_OMIT_VIRTUALTABLE */
5771
5772#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005773/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005774**
drh2133d822008-01-03 18:44:59 +00005775** Store the value of the P2-th column of
5776** the row of the virtual-table that the
5777** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005778*/
5779case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005780 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005781 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005782 Mem *pDest;
5783 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005784
drhdfe88ec2008-11-03 20:55:06 +00005785 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005786 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005787 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005788 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005789 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005790 if( pCur->nullRow ){
5791 sqlite3VdbeMemSetNull(pDest);
5792 break;
5793 }
danielk19773e3a84d2008-08-01 17:37:40 +00005794 pVtab = pCur->pVtabCursor->pVtab;
5795 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005796 assert( pModule->xColumn );
5797 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005798
5799 /* The output cell may already have a buffer allocated. Move
5800 ** the current contents to sContext.s so in case the user-function
5801 ** can use the already allocated buffer instead of allocating a
5802 ** new one.
5803 */
5804 sqlite3VdbeMemMove(&sContext.s, pDest);
5805 MemSetTypeFlag(&sContext.s, MEM_Null);
5806
drhde4fcfd2008-01-19 23:50:26 +00005807 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
drhb9755982010-07-24 16:34:37 +00005808 importVtabErrMsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005809 if( sContext.isError ){
5810 rc = sContext.isError;
5811 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005812
drhde4fcfd2008-01-19 23:50:26 +00005813 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005814 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005815 ** dynamic allocation in sContext.s (a Mem struct) is released.
5816 */
5817 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005818 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005819 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005820 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005821
drhde4fcfd2008-01-19 23:50:26 +00005822 if( sqlite3VdbeMemTooBig(pDest) ){
5823 goto too_big;
5824 }
drh9eff6162006-06-12 21:59:13 +00005825 break;
5826}
5827#endif /* SQLITE_OMIT_VIRTUALTABLE */
5828
5829#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005830/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005831**
5832** Advance virtual table P1 to the next row in its result set and
5833** jump to instruction P2. Or, if the virtual table has reached
5834** the end of its result set, then fall through to the next instruction.
5835*/
drh9cbf3422008-01-17 16:22:13 +00005836case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005837 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005838 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005839 int res;
drh856c1032009-06-02 15:21:42 +00005840 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005841
drhc54a6172009-06-02 16:06:03 +00005842 res = 0;
drh856c1032009-06-02 15:21:42 +00005843 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005844 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005845 if( pCur->nullRow ){
5846 break;
5847 }
danielk19773e3a84d2008-08-01 17:37:40 +00005848 pVtab = pCur->pVtabCursor->pVtab;
5849 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005850 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005851
drhde4fcfd2008-01-19 23:50:26 +00005852 /* Invoke the xNext() method of the module. There is no way for the
5853 ** underlying implementation to return an error if one occurs during
5854 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5855 ** data is available) and the error code returned when xColumn or
5856 ** some other method is next invoked on the save virtual table cursor.
5857 */
drhde4fcfd2008-01-19 23:50:26 +00005858 p->inVtabMethod = 1;
5859 rc = pModule->xNext(pCur->pVtabCursor);
5860 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005861 importVtabErrMsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005862 if( rc==SQLITE_OK ){
5863 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005864 }
5865
drhde4fcfd2008-01-19 23:50:26 +00005866 if( !res ){
5867 /* If there is data, jump to P2 */
5868 pc = pOp->p2 - 1;
5869 }
drh9eff6162006-06-12 21:59:13 +00005870 break;
5871}
5872#endif /* SQLITE_OMIT_VIRTUALTABLE */
5873
danielk1977182c4ba2007-06-27 15:53:34 +00005874#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005875/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005876**
drh66a51672008-01-03 00:01:23 +00005877** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005878** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005879** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005880*/
drh9cbf3422008-01-17 16:22:13 +00005881case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00005882 sqlite3_vtab *pVtab;
5883 Mem *pName;
5884
danielk1977595a5232009-07-24 17:58:53 +00005885 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00005886 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005887 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00005888 assert( memIsValid(pName) );
drh5b6afba2008-01-05 16:29:28 +00005889 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00005890 assert( pName->flags & MEM_Str );
danielk19776dbee812008-01-03 18:39:41 +00005891 rc = pVtab->pModule->xRename(pVtab, pName->z);
drhb9755982010-07-24 16:34:37 +00005892 importVtabErrMsg(p, pVtab);
dana235d0c2010-08-24 16:59:47 +00005893 p->expired = 0;
danielk1977182c4ba2007-06-27 15:53:34 +00005894
danielk1977182c4ba2007-06-27 15:53:34 +00005895 break;
5896}
5897#endif
drh4cbdda92006-06-14 19:00:20 +00005898
5899#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005900/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00005901**
drh66a51672008-01-03 00:01:23 +00005902** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00005903** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00005904** are contiguous memory cells starting at P3 to pass to the xUpdate
5905** invocation. The value in register (P3+P2-1) corresponds to the
5906** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00005907**
5908** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00005909** The argv[0] element (which corresponds to memory cell P3)
5910** is the rowid of a row to delete. If argv[0] is NULL then no
5911** deletion occurs. The argv[1] element is the rowid of the new
5912** row. This can be NULL to have the virtual table select the new
5913** rowid for itself. The subsequent elements in the array are
5914** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00005915**
5916** If P2==1 then no insert is performed. argv[0] is the rowid of
5917** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00005918**
5919** P1 is a boolean flag. If it is set to true and the xUpdate call
5920** is successful, then the value returned by sqlite3_last_insert_rowid()
5921** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00005922*/
drh9cbf3422008-01-17 16:22:13 +00005923case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00005924 sqlite3_vtab *pVtab;
5925 sqlite3_module *pModule;
5926 int nArg;
5927 int i;
5928 sqlite_int64 rowid;
5929 Mem **apArg;
5930 Mem *pX;
5931
danb061d052011-04-25 18:49:57 +00005932 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
5933 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
5934 );
danielk1977595a5232009-07-24 17:58:53 +00005935 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005936 pModule = (sqlite3_module *)pVtab->pModule;
5937 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00005938 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00005939 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00005940 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00005941 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00005942 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00005943 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00005944 assert( memIsValid(pX) );
5945 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00005946 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00005947 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00005948 pX++;
danielk1977399918f2006-06-14 13:03:23 +00005949 }
danb061d052011-04-25 18:49:57 +00005950 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00005951 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00005952 db->vtabOnConflict = vtabOnConflict;
drhb9755982010-07-24 16:34:37 +00005953 importVtabErrMsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00005954 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00005955 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00005956 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00005957 }
danb061d052011-04-25 18:49:57 +00005958 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
5959 if( pOp->p5==OE_Ignore ){
5960 rc = SQLITE_OK;
5961 }else{
5962 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
5963 }
5964 }else{
5965 p->nChange++;
5966 }
danielk1977399918f2006-06-14 13:03:23 +00005967 }
drh4cbdda92006-06-14 19:00:20 +00005968 break;
danielk1977399918f2006-06-14 13:03:23 +00005969}
5970#endif /* SQLITE_OMIT_VIRTUALTABLE */
5971
danielk197759a93792008-05-15 17:48:20 +00005972#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5973/* Opcode: Pagecount P1 P2 * * *
5974**
5975** Write the current number of pages in database P1 to memory cell P2.
5976*/
5977case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00005978 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00005979 break;
5980}
5981#endif
5982
drh60ac3f42010-11-23 18:59:27 +00005983
5984#ifndef SQLITE_OMIT_PAGER_PRAGMAS
5985/* Opcode: MaxPgcnt P1 P2 P3 * *
5986**
5987** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00005988** Do not let the maximum page count fall below the current page count and
5989** do not change the maximum page count value if P3==0.
5990**
drh60ac3f42010-11-23 18:59:27 +00005991** Store the maximum page count after the change in register P2.
5992*/
5993case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00005994 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00005995 Btree *pBt;
5996
5997 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00005998 newMax = 0;
5999 if( pOp->p3 ){
6000 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006001 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006002 }
6003 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006004 break;
6005}
6006#endif
6007
6008
drh949f9cd2008-01-12 21:35:57 +00006009#ifndef SQLITE_OMIT_TRACE
6010/* Opcode: Trace * * * P4 *
6011**
6012** If tracing is enabled (by the sqlite3_trace()) interface, then
6013** the UTF-8 string contained in P4 is emitted on the trace callback.
6014*/
6015case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00006016 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006017 char *z;
drh856c1032009-06-02 15:21:42 +00006018
drhc3f1d5f2011-05-30 23:42:16 +00006019 if( db->xTrace && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
6020 z = sqlite3VdbeExpandSql(p, zTrace);
6021 db->xTrace(db->pTraceArg, z);
6022 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006023 }
drhc3f1d5f2011-05-30 23:42:16 +00006024#ifdef SQLITE_DEBUG
6025 if( (db->flags & SQLITE_SqlTrace)!=0
6026 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6027 ){
6028 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6029 }
6030#endif /* SQLITE_DEBUG */
drh949f9cd2008-01-12 21:35:57 +00006031 break;
6032}
6033#endif
6034
drh91fd4d42008-01-19 20:11:25 +00006035
6036/* Opcode: Noop * * * * *
6037**
6038** Do nothing. This instruction is often useful as a jump
6039** destination.
drh5e00f6c2001-09-13 13:46:56 +00006040*/
drh91fd4d42008-01-19 20:11:25 +00006041/*
6042** The magic Explain opcode are only inserted when explain==2 (which
6043** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6044** This opcode records information from the optimizer. It is the
6045** the same as a no-op. This opcodesnever appears in a real VM program.
6046*/
6047default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006048 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006049 break;
6050}
6051
6052/*****************************************************************************
6053** The cases of the switch statement above this line should all be indented
6054** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6055** readability. From this point on down, the normal indentation rules are
6056** restored.
6057*****************************************************************************/
6058 }
drh6e142f52000-06-08 13:36:40 +00006059
drh7b396862003-01-01 23:06:20 +00006060#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006061 {
shane9bcbdad2008-05-29 20:22:37 +00006062 u64 elapsed = sqlite3Hwtime() - start;
6063 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006064 pOp->cnt++;
6065#if 0
shane9bcbdad2008-05-29 20:22:37 +00006066 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006067 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006068#endif
6069 }
drh7b396862003-01-01 23:06:20 +00006070#endif
6071
drh6e142f52000-06-08 13:36:40 +00006072 /* The following code adds nothing to the actual functionality
6073 ** of the program. It is only here for testing and debugging.
6074 ** On the other hand, it does burn CPU cycles every time through
6075 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6076 */
6077#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006078 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006079
drhcf1023c2007-05-08 20:59:49 +00006080#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00006081 if( p->trace ){
6082 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006083 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
6084 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006085 }
drh3c657212009-11-17 23:59:58 +00006086 if( pOp->opflags & OPFLG_OUT3 ){
6087 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006088 }
drh75897232000-05-29 14:26:00 +00006089 }
danielk1977b5402fb2005-01-12 07:15:04 +00006090#endif /* SQLITE_DEBUG */
6091#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006092 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006093
drha05a7222008-01-19 03:35:58 +00006094 /* If we reach this point, it means that execution is finished with
6095 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006096 */
drha05a7222008-01-19 03:35:58 +00006097vdbe_error_halt:
6098 assert( rc );
6099 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006100 testcase( sqlite3GlobalConfig.xLog!=0 );
6101 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6102 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006103 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006104 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6105 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006106 if( resetSchemaOnFault>0 ){
6107 sqlite3ResetInternalSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006108 }
drh900b31e2007-08-28 02:27:51 +00006109
6110 /* This is the only way out of this procedure. We have to
6111 ** release the mutexes on btrees that were acquired at the
6112 ** top. */
6113vdbe_return:
drh99a66922011-05-13 18:51:42 +00006114 db->lastRowid = lastRowid;
drhbdaec522011-04-04 00:14:43 +00006115 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006116 return rc;
6117
drh023ae032007-05-08 12:12:16 +00006118 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6119 ** is encountered.
6120 */
6121too_big:
drhf089aa42008-07-08 19:34:06 +00006122 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006123 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006124 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006125
drh98640a32007-06-07 19:08:32 +00006126 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006127 */
6128no_mem:
drh17435752007-08-16 04:30:38 +00006129 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006130 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006131 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006132 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006133
drhb86ccfb2003-01-28 23:13:10 +00006134 /* Jump to here for any other kind of fatal error. The "rc" variable
6135 ** should hold the error number.
6136 */
6137abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006138 assert( p->zErrMsg==0 );
6139 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006140 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006141 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006142 }
drha05a7222008-01-19 03:35:58 +00006143 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006144
danielk19776f8a5032004-05-10 10:34:51 +00006145 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006146 ** flag.
6147 */
6148abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006149 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006150 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006151 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006152 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006153 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006154}