blob: 6b78d27fd80973c6d7e4324a8b8aed8d8b3da0b5 [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
drhe4c88c02012-01-04 12:57:45 +000055# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
drh2b4ded92010-09-27 21:09:31 +000056#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
drhe4c88c02012-01-04 12:57:45 +000073** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
74** field of the sqlite3 structure is set in order to simulate an 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
dan689ab892011-08-12 15:02:00 +0000154/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
155#ifdef SQLITE_OMIT_MERGE_SORT
156# define isSorter(x) 0
157#else
158# define isSorter(x) ((x)->pSorter!=0)
159#endif
160
danielk19771cc5ed82007-05-16 17:28:43 +0000161/*
shane21e7feb2008-05-30 15:59:49 +0000162** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000163** user-defined function or returned to the user as the result of a query.
dan937d0de2009-10-15 18:35:38 +0000164** This routine sets the pMem->type variable used by the sqlite3_value_*()
165** routines.
danielk1977c572ef72004-05-27 09:28:41 +0000166*/
dan937d0de2009-10-15 18:35:38 +0000167void sqlite3VdbeMemStoreType(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000168 int flags = pMem->flags;
169 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000170 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000171 }
172 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000173 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000174 }
175 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000176 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000177 }
178 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000179 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000180 }else{
drh9c054832004-05-31 18:51:57 +0000181 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000182 }
183}
danielk19778a6b5412004-05-24 07:04:25 +0000184
185/*
drhdfe88ec2008-11-03 20:55:06 +0000186** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000187** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000188*/
drhdfe88ec2008-11-03 20:55:06 +0000189static VdbeCursor *allocateCursor(
190 Vdbe *p, /* The virtual machine */
191 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000192 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000193 int iDb, /* Database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000194 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000195){
196 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000197 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000198 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000199 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000200 **
201 ** * Sometimes cursor numbers are used for a couple of different
202 ** purposes in a vdbe program. The different uses might require
203 ** different sized allocations. Memory cells provide growable
204 ** allocations.
205 **
206 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
207 ** be freed lazily via the sqlite3_release_memory() API. This
208 ** minimizes the number of malloc calls made by the system.
209 **
210 ** Memory cells for cursors are allocated at the top of the address
211 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
212 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
213 */
214 Mem *pMem = &p->aMem[p->nMem-iCur];
215
danielk19775f096132008-03-28 15:44:09 +0000216 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000217 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000218 nByte =
drhc54055b2009-11-13 17:05:53 +0000219 ROUND8(sizeof(VdbeCursor)) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000220 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
221 2*nField*sizeof(u32);
222
drh290c1942004-08-21 17:54:45 +0000223 assert( iCur<p->nCursor );
224 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000225 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000226 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000227 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000229 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000230 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000231 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000232 pCx->nField = nField;
233 if( nField ){
drhc54055b2009-11-13 17:05:53 +0000234 pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
danielk1977cd3e8f72008-03-25 09:47:35 +0000235 }
236 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000237 pCx->pCursor = (BtCursor*)
drhc54055b2009-11-13 17:05:53 +0000238 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
drhf25a5072009-11-18 23:01:25 +0000239 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000240 }
danielk197794eb6a12005-12-15 15:22:08 +0000241 }
drh4774b132004-06-12 20:12:51 +0000242 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000243}
244
danielk19773d1bfea2004-05-14 11:00:53 +0000245/*
drh29d72102006-02-09 22:13:41 +0000246** Try to convert a value into a numeric representation if we can
247** do so without loss of information. In other words, if the string
248** looks like a number, convert it into a number. If it does not
249** look like a number, leave it alone.
250*/
drhb21c8cd2007-08-21 19:33:56 +0000251static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000252 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000253 double rValue;
254 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000255 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000256 if( (pRec->flags&MEM_Str)==0 ) return;
257 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000258 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000259 pRec->u.i = iValue;
260 pRec->flags |= MEM_Int;
261 }else{
262 pRec->r = rValue;
263 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000264 }
265 }
266}
267
268/*
drh8a512562005-11-14 22:29:05 +0000269** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000270**
drh8a512562005-11-14 22:29:05 +0000271** SQLITE_AFF_INTEGER:
272** SQLITE_AFF_REAL:
273** SQLITE_AFF_NUMERIC:
274** Try to convert pRec to an integer representation or a
275** floating-point representation if an integer representation
276** is not possible. Note that the integer representation is
277** always preferred, even if the affinity is REAL, because
278** an integer representation is more space efficient on disk.
279**
280** SQLITE_AFF_TEXT:
281** Convert pRec to a text representation.
282**
283** SQLITE_AFF_NONE:
284** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000285*/
drh17435752007-08-16 04:30:38 +0000286static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000287 Mem *pRec, /* The value to apply affinity to */
288 char affinity, /* The affinity to be applied */
289 u8 enc /* Use this text encoding */
290){
drh8a512562005-11-14 22:29:05 +0000291 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000292 /* Only attempt the conversion to TEXT if there is an integer or real
293 ** representation (blob and NULL do not get converted) but no string
294 ** representation.
295 */
296 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000297 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000298 }
299 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000300 }else if( affinity!=SQLITE_AFF_NONE ){
301 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
302 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000303 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000304 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000305 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000306 }
danielk19773d1bfea2004-05-14 11:00:53 +0000307 }
308}
309
danielk1977aee18ef2005-03-09 12:26:50 +0000310/*
drh29d72102006-02-09 22:13:41 +0000311** Try to convert the type of a function argument or a result column
312** into a numeric representation. Use either INTEGER or REAL whichever
313** is appropriate. But only do the conversion if it is possible without
314** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000315*/
316int sqlite3_value_numeric_type(sqlite3_value *pVal){
317 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000318 if( pMem->type==SQLITE_TEXT ){
319 applyNumericAffinity(pMem);
320 sqlite3VdbeMemStoreType(pMem);
321 }
drh29d72102006-02-09 22:13:41 +0000322 return pMem->type;
323}
324
325/*
danielk1977aee18ef2005-03-09 12:26:50 +0000326** Exported version of applyAffinity(). This one works on sqlite3_value*,
327** not the internal Mem* type.
328*/
danielk19771e536952007-08-16 10:09:01 +0000329void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000330 sqlite3_value *pVal,
331 u8 affinity,
332 u8 enc
333){
drhb21c8cd2007-08-21 19:33:56 +0000334 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000335}
336
danielk1977b5402fb2005-01-12 07:15:04 +0000337#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000338/*
danielk1977ca6b2912004-05-21 10:49:47 +0000339** Write a nice string representation of the contents of cell pMem
340** into buffer zBuf, length nBuf.
341*/
drh74161702006-02-24 02:53:49 +0000342void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000343 char *zCsr = zBuf;
344 int f = pMem->flags;
345
drh57196282004-10-06 15:41:16 +0000346 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000347
danielk1977ca6b2912004-05-21 10:49:47 +0000348 if( f&MEM_Blob ){
349 int i;
350 char c;
351 if( f & MEM_Dyn ){
352 c = 'z';
353 assert( (f & (MEM_Static|MEM_Ephem))==0 );
354 }else if( f & MEM_Static ){
355 c = 't';
356 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
357 }else if( f & MEM_Ephem ){
358 c = 'e';
359 assert( (f & (MEM_Static|MEM_Dyn))==0 );
360 }else{
361 c = 's';
362 }
363
drh5bb3eb92007-05-04 13:15:55 +0000364 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000365 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000366 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000367 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000368 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000369 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000370 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000371 }
372 for(i=0; i<16 && i<pMem->n; i++){
373 char z = pMem->z[i];
374 if( z<32 || z>126 ) *zCsr++ = '.';
375 else *zCsr++ = z;
376 }
377
drhe718efe2007-05-10 21:14:03 +0000378 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000379 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000380 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000381 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000382 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000383 }
danielk1977b1bc9532004-05-22 03:05:33 +0000384 *zCsr = '\0';
385 }else if( f & MEM_Str ){
386 int j, k;
387 zBuf[0] = ' ';
388 if( f & MEM_Dyn ){
389 zBuf[1] = 'z';
390 assert( (f & (MEM_Static|MEM_Ephem))==0 );
391 }else if( f & MEM_Static ){
392 zBuf[1] = 't';
393 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
394 }else if( f & MEM_Ephem ){
395 zBuf[1] = 'e';
396 assert( (f & (MEM_Static|MEM_Dyn))==0 );
397 }else{
398 zBuf[1] = 's';
399 }
400 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000401 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000402 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000403 zBuf[k++] = '[';
404 for(j=0; j<15 && j<pMem->n; j++){
405 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000406 if( c>=0x20 && c<0x7f ){
407 zBuf[k++] = c;
408 }else{
409 zBuf[k++] = '.';
410 }
411 }
412 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000413 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000414 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000415 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000416 }
danielk1977ca6b2912004-05-21 10:49:47 +0000417}
418#endif
419
drh5b6afba2008-01-05 16:29:28 +0000420#ifdef SQLITE_DEBUG
421/*
422** Print the value of a register for tracing purposes:
423*/
424static void memTracePrint(FILE *out, Mem *p){
drh953f7612012-12-07 22:18:54 +0000425 if( p->flags & MEM_Invalid ){
426 fprintf(out, " undefined");
427 }else if( p->flags & MEM_Null ){
drh5b6afba2008-01-05 16:29:28 +0000428 fprintf(out, " NULL");
429 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
430 fprintf(out, " si:%lld", p->u.i);
431 }else if( p->flags & MEM_Int ){
432 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000433#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000434 }else if( p->flags & MEM_Real ){
435 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000436#endif
drh733bf1b2009-04-22 00:47:00 +0000437 }else if( p->flags & MEM_RowSet ){
438 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000439 }else{
440 char zBuf[200];
441 sqlite3VdbeMemPrettyPrint(p, zBuf);
442 fprintf(out, " ");
443 fprintf(out, "%s", zBuf);
444 }
445}
446static void registerTrace(FILE *out, int iReg, Mem *p){
447 fprintf(out, "REG[%d] = ", iReg);
448 memTracePrint(out, p);
449 fprintf(out, "\n");
450}
451#endif
452
453#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000454# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000455#else
456# define REGISTER_TRACE(R,M)
457#endif
458
danielk197784ac9d02004-05-18 09:58:06 +0000459
drh7b396862003-01-01 23:06:20 +0000460#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000461
462/*
463** hwtime.h contains inline assembler code for implementing
464** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000465*/
shane9bcbdad2008-05-29 20:22:37 +0000466#include "hwtime.h"
467
drh7b396862003-01-01 23:06:20 +0000468#endif
469
drh8c74a8c2002-08-25 19:20:40 +0000470/*
drhcaec2f12003-01-07 02:47:47 +0000471** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000472** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000473** processing of the VDBE program is interrupted.
474**
475** This macro added to every instruction that does a jump in order to
476** implement a loop. This test used to be on every single instruction,
drhe4c88c02012-01-04 12:57:45 +0000477** but that meant we more testing than we needed. By only testing the
drhcaec2f12003-01-07 02:47:47 +0000478** flag on jump instructions, we get a (small) speed improvement.
479*/
480#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000481 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000482
483
danielk1977fd7f0452008-12-17 17:30:26 +0000484#ifndef NDEBUG
485/*
486** This function is only called from within an assert() expression. It
487** checks that the sqlite3.nTransaction variable is correctly set to
488** the number of non-transaction savepoints currently in the
489** linked list starting at sqlite3.pSavepoint.
490**
491** Usage:
492**
493** assert( checkSavepointCount(db) );
494*/
495static int checkSavepointCount(sqlite3 *db){
496 int n = 0;
497 Savepoint *p;
498 for(p=db->pSavepoint; p; p=p->pNext) n++;
499 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
500 return 1;
501}
502#endif
503
drhcaec2f12003-01-07 02:47:47 +0000504/*
drhb9755982010-07-24 16:34:37 +0000505** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
506** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
507** in memory obtained from sqlite3DbMalloc).
508*/
509static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
510 sqlite3 *db = p->db;
511 sqlite3DbFree(db, p->zErrMsg);
512 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
513 sqlite3_free(pVtab->zErrMsg);
514 pVtab->zErrMsg = 0;
515}
516
517
518/*
drhb86ccfb2003-01-28 23:13:10 +0000519** Execute as much of a VDBE program as we can then return.
520**
danielk19774adee202004-05-08 08:23:19 +0000521** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000522** close the program with a final OP_Halt and to set up the callbacks
523** and the error message pointer.
524**
525** Whenever a row or result data is available, this routine will either
526** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000527** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000528**
529** If an attempt is made to open a locked database, then this routine
530** will either invoke the busy callback (if there is one) or it will
531** return SQLITE_BUSY.
532**
533** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000534** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000535** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
536**
537** If the callback ever returns non-zero, then the program exits
538** immediately. There will be no error message but the p->rc field is
539** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
540**
drh9468c7f2003-03-07 19:50:07 +0000541** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
542** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000543**
544** Other fatal errors return SQLITE_ERROR.
545**
danielk19774adee202004-05-08 08:23:19 +0000546** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000547** used to clean up the mess that was left behind.
548*/
danielk19774adee202004-05-08 08:23:19 +0000549int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000550 Vdbe *p /* The VDBE */
551){
shaneh84f4b2f2010-02-26 01:46:54 +0000552 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000553 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000554 Op *pOp; /* Current operation */
555 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000556 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000557 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000558 u8 encoding = ENC(db); /* The database encoding */
drha6c2ed92009-11-14 23:22:23 +0000559#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
shaneh5e17e8b2009-12-03 04:40:47 +0000560 int checkProgress; /* True if progress callbacks are enabled */
drha6c2ed92009-11-14 23:22:23 +0000561 int nProgressOps = 0; /* Opcodes executed since progress callback. */
562#endif
563 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000564 Mem *pIn1 = 0; /* 1st input operand */
565 Mem *pIn2 = 0; /* 2nd input operand */
566 Mem *pIn3 = 0; /* 3rd input operand */
567 Mem *pOut = 0; /* Output operand */
drh0acb7e42008-06-25 00:12:41 +0000568 int iCompare = 0; /* Result of last OP_Compare operation */
shanebe217792009-03-05 04:20:31 +0000569 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000570 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000571#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000572 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000573 int origPc; /* Program counter at start of opcode */
574#endif
drh856c1032009-06-02 15:21:42 +0000575 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000576
drhca48c902008-01-18 14:08:24 +0000577 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000578 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000579 if( p->rc==SQLITE_NOMEM ){
580 /* This happens if a malloc() inside a call to sqlite3_column_text() or
581 ** sqlite3_column_text16() failed. */
582 goto no_mem;
583 }
drh3a840692003-01-29 22:58:26 +0000584 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
585 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000586 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000587 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000588 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000589 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000590 sqlite3VdbeIOTraceSql(p);
drha6c2ed92009-11-14 23:22:23 +0000591#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
592 checkProgress = db->xProgress!=0;
593#endif
drh3c23a882007-01-09 14:01:13 +0000594#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000595 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000596 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000597 int i;
598 printf("VDBE Program Listing:\n");
599 sqlite3VdbePrintSql(p);
600 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000601 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000602 }
603 }
danielk19772d1d86f2008-06-20 14:59:51 +0000604 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000605#endif
drhb86ccfb2003-01-28 23:13:10 +0000606 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000607 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000608 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000609#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000610 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000611 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000612#endif
drhbbe879d2009-11-14 18:04:35 +0000613 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000614
danielk19778b60e0f2005-01-12 09:10:39 +0000615 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000616 */
danielk19778b60e0f2005-01-12 09:10:39 +0000617#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000618 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000619 if( pc==0 ){
620 printf("VDBE Execution Trace:\n");
621 sqlite3VdbePrintSql(p);
622 }
danielk19774adee202004-05-08 08:23:19 +0000623 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000624 }
drh3f7d4e42004-07-24 14:35:58 +0000625#endif
626
drh6e142f52000-06-08 13:36:40 +0000627
drhf6038712004-02-08 18:07:34 +0000628 /* Check to see if we need to simulate an interrupt. This only happens
629 ** if we have a special test build.
630 */
631#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000632 if( sqlite3_interrupt_count>0 ){
633 sqlite3_interrupt_count--;
634 if( sqlite3_interrupt_count==0 ){
635 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000636 }
637 }
638#endif
639
danielk1977348bb5d2003-10-18 09:37:26 +0000640#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
641 /* Call the progress callback if it is configured and the required number
642 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000643 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000644 ** If the progress callback returns non-zero, exit the virtual machine with
645 ** a return code SQLITE_ABORT.
646 */
drha6c2ed92009-11-14 23:22:23 +0000647 if( checkProgress ){
drh3914aed2004-01-31 20:40:42 +0000648 if( db->nProgressOps==nProgressOps ){
danielk1977de523ac2007-06-15 14:53:53 +0000649 int prc;
drh9978c972010-02-23 17:36:32 +0000650 prc = db->xProgress(db->pProgressArg);
danielk1977de523ac2007-06-15 14:53:53 +0000651 if( prc!=0 ){
652 rc = SQLITE_INTERRUPT;
drha05a7222008-01-19 03:35:58 +0000653 goto vdbe_error_halt;
danielk1977de523ac2007-06-15 14:53:53 +0000654 }
danielk19773fe11f32007-06-13 16:49:48 +0000655 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000656 }
drh3914aed2004-01-31 20:40:42 +0000657 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000658 }
danielk1977348bb5d2003-10-18 09:37:26 +0000659#endif
660
drhb5b407e2012-08-29 10:28:43 +0000661 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000662 ** external allocations out of mem[p2] and set mem[p2] to be
663 ** an undefined integer. Opcodes will either fill in the integer
664 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000665 */
drha6c2ed92009-11-14 23:22:23 +0000666 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000667 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
668 assert( pOp->p2>0 );
669 assert( pOp->p2<=p->nMem );
670 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000671 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000672 VdbeMemRelease(pOut);
drh3c657212009-11-17 23:59:58 +0000673 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000674 }
drh3c657212009-11-17 23:59:58 +0000675
676 /* Sanity checking on other operands */
677#ifdef SQLITE_DEBUG
678 if( (pOp->opflags & OPFLG_IN1)!=0 ){
679 assert( pOp->p1>0 );
680 assert( pOp->p1<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000681 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000682 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
683 }
684 if( (pOp->opflags & OPFLG_IN2)!=0 ){
685 assert( pOp->p2>0 );
686 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000687 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000688 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
689 }
690 if( (pOp->opflags & OPFLG_IN3)!=0 ){
691 assert( pOp->p3>0 );
692 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000693 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000694 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
695 }
696 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
697 assert( pOp->p2>0 );
698 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000699 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000700 }
701 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
702 assert( pOp->p3>0 );
703 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000704 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000705 }
706#endif
drh93952eb2009-11-13 19:43:43 +0000707
drh75897232000-05-29 14:26:00 +0000708 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000709
drh5e00f6c2001-09-13 13:46:56 +0000710/*****************************************************************************
711** What follows is a massive switch statement where each case implements a
712** separate instruction in the virtual machine. If we follow the usual
713** indentation conventions, each case should be indented by 6 spaces. But
714** that is a lot of wasted space on the left margin. So the code within
715** the switch statement will break with convention and be flush-left. Another
716** big comment (similar to this one) will mark the point in the code where
717** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000718**
719** The formatting of each case is important. The makefile for SQLite
720** generates two C files "opcodes.h" and "opcodes.c" by scanning this
721** file looking for lines that begin with "case OP_". The opcodes.h files
722** will be filled with #defines that give unique integer values to each
723** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000724** each string is the symbolic name for the corresponding opcode. If the
725** case statement is followed by a comment of the form "/# same as ... #/"
726** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000727**
drh9cbf3422008-01-17 16:22:13 +0000728** Other keywords in the comment that follows each case are used to
729** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
730** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
731** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000732**
drhac82fcf2002-09-08 17:23:41 +0000733** Documentation about VDBE opcodes is generated by scanning this file
734** for lines of that contain "Opcode:". That line and all subsequent
735** comment lines are used in the generation of the opcode.html documentation
736** file.
737**
738** SUMMARY:
739**
740** Formatting is important to scripts that scan this file.
741** Do not deviate from the formatting style currently in use.
742**
drh5e00f6c2001-09-13 13:46:56 +0000743*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000744
drh9cbf3422008-01-17 16:22:13 +0000745/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000746**
747** An unconditional jump to address P2.
748** The next instruction executed will be
749** the one at index P2 from the beginning of
750** the program.
751*/
drh9cbf3422008-01-17 16:22:13 +0000752case OP_Goto: { /* jump */
drhcaec2f12003-01-07 02:47:47 +0000753 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000754 pc = pOp->p2 - 1;
755 break;
756}
drh75897232000-05-29 14:26:00 +0000757
drh2eb95372008-06-06 15:04:36 +0000758/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000759**
drh2eb95372008-06-06 15:04:36 +0000760** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000761** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000762*/
drhb8475df2011-12-09 16:21:19 +0000763case OP_Gosub: { /* jump */
764 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh3c657212009-11-17 23:59:58 +0000765 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000766 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000767 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000768 pIn1->flags = MEM_Int;
769 pIn1->u.i = pc;
770 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000771 pc = pOp->p2 - 1;
772 break;
773}
774
drh2eb95372008-06-06 15:04:36 +0000775/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000776**
drh2eb95372008-06-06 15:04:36 +0000777** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000778*/
drh2eb95372008-06-06 15:04:36 +0000779case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000780 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000781 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000782 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000783 break;
784}
785
drhe00ee6e2008-06-20 15:24:01 +0000786/* Opcode: Yield P1 * * * *
787**
788** Swap the program counter with the value in register P1.
789*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000790case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000791 int pcDest;
drh3c657212009-11-17 23:59:58 +0000792 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000793 assert( (pIn1->flags & MEM_Dyn)==0 );
794 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000795 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000796 pIn1->u.i = pc;
797 REGISTER_TRACE(pOp->p1, pIn1);
798 pc = pcDest;
799 break;
800}
801
drh5053a792009-02-20 03:02:23 +0000802/* Opcode: HaltIfNull P1 P2 P3 P4 *
803**
drhef8662b2011-06-20 21:47:58 +0000804** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000805** parameter P1, P2, and P4 as if this were a Halt instruction. If the
806** value in register P3 is not NULL, then this routine is a no-op.
807*/
808case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000809 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000810 if( (pIn3->flags & MEM_Null)==0 ) break;
811 /* Fall through into OP_Halt */
812}
drhe00ee6e2008-06-20 15:24:01 +0000813
drh9cbf3422008-01-17 16:22:13 +0000814/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000815**
drh3d4501e2008-12-04 20:40:10 +0000816** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000817** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000818**
drh92f02c32004-09-02 14:57:08 +0000819** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
820** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
821** For errors, it can be some other value. If P1!=0 then P2 will determine
822** whether or not to rollback the current transaction. Do not rollback
823** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
824** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000825** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000826**
drh66a51672008-01-03 00:01:23 +0000827** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000828**
drh9cfcf5d2002-01-29 18:41:24 +0000829** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000830** every program. So a jump past the last instruction of the program
831** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000832*/
drh9cbf3422008-01-17 16:22:13 +0000833case OP_Halt: {
dan165921a2009-08-28 18:53:45 +0000834 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000835 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000836 VdbeFrame *pFrame = p->pFrame;
837 p->pFrame = pFrame->pParent;
838 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000839 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000840 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000841 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000842 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000843 /* Instruction pc is the OP_Program that invoked the sub-program
844 ** currently being halted. If the p2 instruction of this OP_Halt
845 ** instruction is set to OE_Ignore, then the sub-program is throwing
846 ** an IGNORE exception. In this case jump to the address specified
847 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000848 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000849 }
drhbbe879d2009-11-14 18:04:35 +0000850 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000851 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000852 break;
853 }
dan2832ad42009-08-31 15:27:27 +0000854
drh92f02c32004-09-02 14:57:08 +0000855 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000856 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000857 p->pc = pc;
danielk19772dca4ac2008-01-03 11:50:29 +0000858 if( pOp->p4.z ){
drh413c3d32010-02-23 20:11:56 +0000859 assert( p->rc!=SQLITE_OK );
drhf089aa42008-07-08 19:34:06 +0000860 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhaf46dc12010-02-24 21:44:07 +0000861 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000862 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
drhcda455b2010-02-24 19:23:56 +0000863 }else if( p->rc ){
drhaf46dc12010-02-24 21:44:07 +0000864 testcase( sqlite3GlobalConfig.xLog!=0 );
drhcda455b2010-02-24 19:23:56 +0000865 sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
drh9cfcf5d2002-01-29 18:41:24 +0000866 }
drh92f02c32004-09-02 14:57:08 +0000867 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000868 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000869 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000870 p->rc = rc = SQLITE_BUSY;
871 }else{
dan1da40a32009-09-19 17:00:31 +0000872 assert( rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT );
873 assert( rc==SQLITE_OK || db->nDeferredCons>0 );
drh900b31e2007-08-28 02:27:51 +0000874 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000875 }
drh900b31e2007-08-28 02:27:51 +0000876 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000877}
drhc61053b2000-06-04 12:58:36 +0000878
drh4c583122008-01-04 22:01:03 +0000879/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000880**
drh9cbf3422008-01-17 16:22:13 +0000881** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000882*/
drh4c583122008-01-04 22:01:03 +0000883case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000884 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000885 break;
886}
887
drh4c583122008-01-04 22:01:03 +0000888/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000889**
drh66a51672008-01-03 00:01:23 +0000890** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000891** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000892*/
drh4c583122008-01-04 22:01:03 +0000893case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000894 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000895 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000896 break;
897}
drh4f26d6c2004-05-26 23:25:30 +0000898
drh13573c72010-01-12 17:04:07 +0000899#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000900/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000901**
drh4c583122008-01-04 22:01:03 +0000902** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000903** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000904*/
drh4c583122008-01-04 22:01:03 +0000905case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
906 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000907 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000908 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000909 break;
910}
drh13573c72010-01-12 17:04:07 +0000911#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000912
drh3c84ddf2008-01-09 02:15:38 +0000913/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000914**
drh66a51672008-01-03 00:01:23 +0000915** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000916** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000917*/
drh4c583122008-01-04 22:01:03 +0000918case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000919 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000920 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000921 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000922
923#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000924 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000925 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
926 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000927 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000928 assert( pOut->zMalloc==pOut->z );
929 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000930 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000931 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000932 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000933 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000934 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000935 }
drh66a51672008-01-03 00:01:23 +0000936 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000937 pOp->p4.z = pOut->z;
938 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000939 }
danielk197793758c82005-01-21 08:13:14 +0000940#endif
drhbb4957f2008-03-20 14:03:29 +0000941 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000942 goto too_big;
943 }
944 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000945}
drhf4479502004-05-27 03:12:53 +0000946
drh4c583122008-01-04 22:01:03 +0000947/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000948**
drh9cbf3422008-01-17 16:22:13 +0000949** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000950*/
drh4c583122008-01-04 22:01:03 +0000951case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000952 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000953 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
954 pOut->z = pOp->p4.z;
955 pOut->n = pOp->p1;
956 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000957 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000958 break;
959}
960
drh053a1282012-09-19 21:15:46 +0000961/* Opcode: Null P1 P2 P3 * *
drhf0863fe2005-06-12 21:35:51 +0000962**
drhb8475df2011-12-09 16:21:19 +0000963** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +0000964** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +0000965** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +0000966** set to NULL.
967**
968** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
969** NULL values will not compare equal even if SQLITE_NULLEQ is set on
970** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +0000971*/
drh4c583122008-01-04 22:01:03 +0000972case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +0000973 int cnt;
drh053a1282012-09-19 21:15:46 +0000974 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +0000975 cnt = pOp->p3-pOp->p2;
976 assert( pOp->p3<=p->nMem );
drh053a1282012-09-19 21:15:46 +0000977 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +0000978 while( cnt>0 ){
979 pOut++;
980 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000981 VdbeMemRelease(pOut);
drh053a1282012-09-19 21:15:46 +0000982 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +0000983 cnt--;
984 }
drhf0863fe2005-06-12 21:35:51 +0000985 break;
986}
987
988
drh9de221d2008-01-05 06:51:30 +0000989/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +0000990**
drh9de221d2008-01-05 06:51:30 +0000991** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +0000992** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +0000993*/
drh4c583122008-01-04 22:01:03 +0000994case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +0000995 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +0000996 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +0000997 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000998 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +0000999 break;
1000}
1001
drheaf52d82010-05-12 13:50:23 +00001002/* Opcode: Variable P1 P2 * P4 *
drh50457892003-09-06 01:10:47 +00001003**
drheaf52d82010-05-12 13:50:23 +00001004** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001005**
1006** If the parameter is named, then its name appears in P4 and P3==1.
1007** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001008*/
drheaf52d82010-05-12 13:50:23 +00001009case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001010 Mem *pVar; /* Value being transferred */
1011
drheaf52d82010-05-12 13:50:23 +00001012 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001013 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001014 pVar = &p->aVar[pOp->p1 - 1];
1015 if( sqlite3VdbeMemTooBig(pVar) ){
1016 goto too_big;
drh023ae032007-05-08 12:12:16 +00001017 }
drheaf52d82010-05-12 13:50:23 +00001018 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1019 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001020 break;
1021}
danielk1977295ba552004-05-19 10:34:51 +00001022
drhb21e7c72008-06-22 12:37:57 +00001023/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001024**
drhe8e4af72012-09-21 00:04:28 +00001025** Move the values in register P1..P1+P3 over into
1026** registers P2..P2+P3. Registers P1..P1+P3 are
drhb21e7c72008-06-22 12:37:57 +00001027** left holding a NULL. It is an error for register ranges
drhe8e4af72012-09-21 00:04:28 +00001028** P1..P1+P3 and P2..P2+P3 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001029*/
drhe1349cb2008-04-01 00:36:10 +00001030case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001031 char *zMalloc; /* Holding variable for allocated memory */
1032 int n; /* Number of registers left to copy */
1033 int p1; /* Register to copy from */
1034 int p2; /* Register to copy to */
1035
drhe8e4af72012-09-21 00:04:28 +00001036 n = pOp->p3 + 1;
drh856c1032009-06-02 15:21:42 +00001037 p1 = pOp->p1;
1038 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001039 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001040 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001041
drha6c2ed92009-11-14 23:22:23 +00001042 pIn1 = &aMem[p1];
1043 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001044 while( n-- ){
drha6c2ed92009-11-14 23:22:23 +00001045 assert( pOut<=&aMem[p->nMem] );
1046 assert( pIn1<=&aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00001047 assert( memIsValid(pIn1) );
1048 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001049 zMalloc = pOut->zMalloc;
1050 pOut->zMalloc = 0;
1051 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001052#ifdef SQLITE_DEBUG
1053 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1054 pOut->pScopyFrom += p1 - pOp->p2;
1055 }
1056#endif
drhb21e7c72008-06-22 12:37:57 +00001057 pIn1->zMalloc = zMalloc;
1058 REGISTER_TRACE(p2++, pOut);
1059 pIn1++;
1060 pOut++;
1061 }
drhe1349cb2008-04-01 00:36:10 +00001062 break;
1063}
1064
drhe8e4af72012-09-21 00:04:28 +00001065/* Opcode: Copy P1 P2 P3 * *
drhb1fdb2a2008-01-05 04:06:03 +00001066**
drhe8e4af72012-09-21 00:04:28 +00001067** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001068**
1069** This instruction makes a deep copy of the value. A duplicate
1070** is made of any string or blob constant. See also OP_SCopy.
1071*/
drhe8e4af72012-09-21 00:04:28 +00001072case OP_Copy: {
1073 int n;
1074
1075 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001076 pIn1 = &aMem[pOp->p1];
1077 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001078 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001079 while( 1 ){
1080 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1081 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001082#ifdef SQLITE_DEBUG
1083 pOut->pScopyFrom = 0;
1084#endif
drhe8e4af72012-09-21 00:04:28 +00001085 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1086 if( (n--)==0 ) break;
1087 pOut++;
1088 pIn1++;
1089 }
drhe1349cb2008-04-01 00:36:10 +00001090 break;
1091}
1092
drhb1fdb2a2008-01-05 04:06:03 +00001093/* Opcode: SCopy P1 P2 * * *
1094**
drh9cbf3422008-01-17 16:22:13 +00001095** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001096**
1097** This instruction makes a shallow copy of the value. If the value
1098** is a string or blob, then the copy is only a pointer to the
1099** original and hence if the original changes so will the copy.
1100** Worse, if the original is deallocated, the copy becomes invalid.
1101** Thus the program must guarantee that the original will not change
1102** during the lifetime of the copy. Use OP_Copy to make a complete
1103** copy.
1104*/
drh93952eb2009-11-13 19:43:43 +00001105case OP_SCopy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001106 pIn1 = &aMem[pOp->p1];
1107 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001108 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001109 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001110#ifdef SQLITE_DEBUG
1111 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1112#endif
drh5b6afba2008-01-05 16:29:28 +00001113 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001114 break;
1115}
drh75897232000-05-29 14:26:00 +00001116
drh9cbf3422008-01-17 16:22:13 +00001117/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001118**
shane21e7feb2008-05-30 15:59:49 +00001119** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001120** results. This opcode causes the sqlite3_step() call to terminate
1121** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1122** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001123** row.
drhd4e70eb2008-01-02 00:34:36 +00001124*/
drh9cbf3422008-01-17 16:22:13 +00001125case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001126 Mem *pMem;
1127 int i;
1128 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001129 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001130 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001131
dan32b09f22009-09-23 17:29:59 +00001132 /* If this statement has violated immediate foreign key constraints, do
1133 ** not return the number of rows modified. And do not RELEASE the statement
1134 ** transaction. It needs to be rolled back. */
1135 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1136 assert( db->flags&SQLITE_CountRows );
1137 assert( p->usesStmtJournal );
1138 break;
1139 }
1140
danielk1977bd434552009-03-18 10:33:00 +00001141 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1142 ** DML statements invoke this opcode to return the number of rows
1143 ** modified to the user. This is the only way that a VM that
1144 ** opens a statement transaction may invoke this opcode.
1145 **
1146 ** In case this is such a statement, close any statement transaction
1147 ** opened by this VM before returning control to the user. This is to
1148 ** ensure that statement-transactions are always nested, not overlapping.
1149 ** If the open statement-transaction is not closed here, then the user
1150 ** may step another VM that opens its own statement transaction. This
1151 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001152 **
1153 ** The statement transaction is never a top-level transaction. Hence
1154 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001155 */
1156 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001157 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1158 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001159 break;
1160 }
1161
drhd4e70eb2008-01-02 00:34:36 +00001162 /* Invalidate all ephemeral cursor row caches */
1163 p->cacheCtr = (p->cacheCtr + 2)|1;
1164
1165 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001166 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001167 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001168 */
drha6c2ed92009-11-14 23:22:23 +00001169 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001170 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001171 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001172 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001173 assert( (pMem[i].flags & MEM_Ephem)==0
1174 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001175 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001176 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001177 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001178 }
drh28039692008-03-17 16:54:01 +00001179 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001180
1181 /* Return SQLITE_ROW
1182 */
drhd4e70eb2008-01-02 00:34:36 +00001183 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001184 rc = SQLITE_ROW;
1185 goto vdbe_return;
1186}
1187
drh5b6afba2008-01-05 16:29:28 +00001188/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001189**
drh5b6afba2008-01-05 16:29:28 +00001190** Add the text in register P1 onto the end of the text in
1191** register P2 and store the result in register P3.
1192** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001193**
1194** P3 = P2 || P1
1195**
1196** It is illegal for P1 and P3 to be the same register. Sometimes,
1197** if P3 is the same register as P2, the implementation is able
1198** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001199*/
drh5b6afba2008-01-05 16:29:28 +00001200case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001201 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001202
drh3c657212009-11-17 23:59:58 +00001203 pIn1 = &aMem[pOp->p1];
1204 pIn2 = &aMem[pOp->p2];
1205 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001206 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001207 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001208 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001209 break;
drh5e00f6c2001-09-13 13:46:56 +00001210 }
drha0c06522009-06-17 22:50:41 +00001211 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001212 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001213 Stringify(pIn2, encoding);
1214 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001215 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001216 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001217 }
danielk1977a7a8e142008-02-13 18:25:27 +00001218 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001219 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001220 goto no_mem;
1221 }
danielk1977a7a8e142008-02-13 18:25:27 +00001222 if( pOut!=pIn2 ){
1223 memcpy(pOut->z, pIn2->z, pIn2->n);
1224 }
1225 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1226 pOut->z[nByte] = 0;
1227 pOut->z[nByte+1] = 0;
1228 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001229 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001230 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001231 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001232 break;
1233}
drh75897232000-05-29 14:26:00 +00001234
drh3c84ddf2008-01-09 02:15:38 +00001235/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001236**
drh60a713c2008-01-21 16:22:45 +00001237** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001238** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001239** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001240*/
drh3c84ddf2008-01-09 02:15:38 +00001241/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001242**
drh3c84ddf2008-01-09 02:15:38 +00001243**
shane21e7feb2008-05-30 15:59:49 +00001244** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001245** and store the result in register P3.
1246** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001247*/
drh3c84ddf2008-01-09 02:15:38 +00001248/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001249**
drh60a713c2008-01-21 16:22:45 +00001250** Subtract the value in register P1 from the value in register P2
1251** and store the result in register P3.
1252** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001253*/
drh9cbf3422008-01-17 16:22:13 +00001254/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001255**
drh60a713c2008-01-21 16:22:45 +00001256** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001257** and store the result in register P3 (P3=P2/P1). If the value in
1258** register P1 is zero, then the result is NULL. If either input is
1259** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001260*/
drh9cbf3422008-01-17 16:22:13 +00001261/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001262**
drh3c84ddf2008-01-09 02:15:38 +00001263** Compute the remainder after integer division of the value in
1264** register P1 by the value in register P2 and store the result in P3.
1265** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001266** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001267*/
drh5b6afba2008-01-05 16:29:28 +00001268case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1269case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1270case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1271case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1272case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00001273 int flags; /* Combined MEM_* flags from both inputs */
1274 i64 iA; /* Integer value of left operand */
1275 i64 iB; /* Integer value of right operand */
1276 double rA; /* Real value of left operand */
1277 double rB; /* Real value of right operand */
1278
drh3c657212009-11-17 23:59:58 +00001279 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001280 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001281 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001282 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001283 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001284 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001285 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1286 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001287 iA = pIn1->u.i;
1288 iB = pIn2->u.i;
drh5e00f6c2001-09-13 13:46:56 +00001289 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001290 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1291 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1292 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001293 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001294 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001295 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001296 iB /= iA;
drh75897232000-05-29 14:26:00 +00001297 break;
1298 }
drhbf4133c2001-10-13 02:59:08 +00001299 default: {
drh856c1032009-06-02 15:21:42 +00001300 if( iA==0 ) goto arithmetic_result_is_null;
1301 if( iA==-1 ) iA = 1;
1302 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001303 break;
1304 }
drh75897232000-05-29 14:26:00 +00001305 }
drh856c1032009-06-02 15:21:42 +00001306 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001307 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001308 }else{
drh158b9cb2011-03-05 20:59:46 +00001309fp_math:
drh856c1032009-06-02 15:21:42 +00001310 rA = sqlite3VdbeRealValue(pIn1);
1311 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001312 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001313 case OP_Add: rB += rA; break;
1314 case OP_Subtract: rB -= rA; break;
1315 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001316 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001317 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001318 if( rA==(double)0 ) goto arithmetic_result_is_null;
1319 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001320 break;
1321 }
drhbf4133c2001-10-13 02:59:08 +00001322 default: {
shane75ac1de2009-06-09 18:58:52 +00001323 iA = (i64)rA;
1324 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001325 if( iA==0 ) goto arithmetic_result_is_null;
1326 if( iA==-1 ) iA = 1;
1327 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001328 break;
1329 }
drh5e00f6c2001-09-13 13:46:56 +00001330 }
drhc5a7b512010-01-13 16:25:42 +00001331#ifdef SQLITE_OMIT_FLOATING_POINT
1332 pOut->u.i = rB;
1333 MemSetTypeFlag(pOut, MEM_Int);
1334#else
drh856c1032009-06-02 15:21:42 +00001335 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001336 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001337 }
drh856c1032009-06-02 15:21:42 +00001338 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001339 MemSetTypeFlag(pOut, MEM_Real);
drh8a512562005-11-14 22:29:05 +00001340 if( (flags & MEM_Real)==0 ){
drh5b6afba2008-01-05 16:29:28 +00001341 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001342 }
drhc5a7b512010-01-13 16:25:42 +00001343#endif
drh5e00f6c2001-09-13 13:46:56 +00001344 }
1345 break;
1346
drha05a7222008-01-19 03:35:58 +00001347arithmetic_result_is_null:
1348 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001349 break;
1350}
1351
drh7a957892012-02-02 17:35:43 +00001352/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001353**
drh66a51672008-01-03 00:01:23 +00001354** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001355** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1356** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001357** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001358**
drh7a957892012-02-02 17:35:43 +00001359** If P1 is not zero, then it is a register that a subsequent min() or
1360** max() aggregate will set to 1 if the current row is not the minimum or
1361** maximum. The P1 register is initialized to 0 by this instruction.
1362**
danielk1977dc1bdc42004-06-11 10:51:27 +00001363** The interface used by the implementation of the aforementioned functions
1364** to retrieve the collation sequence set by this opcode is not available
1365** publicly, only to user functions defined in func.c.
1366*/
drh9cbf3422008-01-17 16:22:13 +00001367case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001368 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001369 if( pOp->p1 ){
1370 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1371 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001372 break;
1373}
1374
drh98757152008-01-09 23:04:12 +00001375/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001376**
drh66a51672008-01-03 00:01:23 +00001377** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001378** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001379** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001380** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001381**
drh13449892005-09-07 21:22:45 +00001382** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001383** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001384** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001385** whether meta data associated with a user function argument using the
1386** sqlite3_set_auxdata() API may be safely retained until the next
1387** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001388**
drh13449892005-09-07 21:22:45 +00001389** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001390*/
drh0bce8352002-02-28 00:41:10 +00001391case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001392 int i;
drh6810ce62004-01-31 19:22:56 +00001393 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001394 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001395 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001396 int n;
drh1350b032002-02-27 19:00:20 +00001397
drh856c1032009-06-02 15:21:42 +00001398 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001399 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001400 assert( apVal || n==0 );
drhebc16712010-09-28 00:25:58 +00001401 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1402 pOut = &aMem[pOp->p3];
1403 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001404
danielk19776ab3a2e2009-02-19 14:39:25 +00001405 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001406 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001407 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001408 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001409 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001410 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001411 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001412 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001413 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001414 }
danielk197751ad0ec2004-05-24 12:39:02 +00001415
drh66a51672008-01-03 00:01:23 +00001416 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
1417 if( pOp->p4type==P4_FUNCDEF ){
danielk19772dca4ac2008-01-03 11:50:29 +00001418 ctx.pFunc = pOp->p4.pFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001419 ctx.pVdbeFunc = 0;
1420 }else{
danielk19772dca4ac2008-01-03 11:50:29 +00001421 ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
danielk1977682f68b2004-06-05 10:22:17 +00001422 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1423 }
1424
drh00706be2004-01-30 14:49:16 +00001425 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001426 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001427 ctx.s.xDel = 0;
1428 ctx.s.zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001429
1430 /* The output cell may already have a buffer allocated. Move
1431 ** the pointer to ctx.s so in case the user-function can use
1432 ** the already allocated buffer instead of allocating a new one.
1433 */
1434 sqlite3VdbeMemMove(&ctx.s, pOut);
1435 MemSetTypeFlag(&ctx.s, MEM_Null);
1436
drh8e0a2f92002-02-23 23:45:45 +00001437 ctx.isError = 0;
drhe82f5d02008-10-07 19:53:14 +00001438 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001439 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001440 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001441 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001442 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001443 }
drh99a66922011-05-13 18:51:42 +00001444 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001445 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001446 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001447
shane21e7feb2008-05-30 15:59:49 +00001448 /* If any auxiliary data functions have been called by this user function,
danielk1977682f68b2004-06-05 10:22:17 +00001449 ** immediately call the destructor for any non-static values.
1450 */
1451 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001452 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk19772dca4ac2008-01-03 11:50:29 +00001453 pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
drh66a51672008-01-03 00:01:23 +00001454 pOp->p4type = P4_VDBEFUNC;
danielk1977682f68b2004-06-05 10:22:17 +00001455 }
1456
dan5f84e142011-06-14 14:18:45 +00001457 if( db->mallocFailed ){
1458 /* Even though a malloc() has failed, the implementation of the
1459 ** user function may have called an sqlite3_result_XXX() function
1460 ** to return a value. The following call releases any resources
1461 ** associated with such a value.
1462 */
1463 sqlite3VdbeMemRelease(&ctx.s);
1464 goto no_mem;
1465 }
1466
drh90669c12006-01-20 15:45:36 +00001467 /* If the function returned an error, throw an exception */
1468 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00001469 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00001470 rc = ctx.isError;
drh90669c12006-01-20 15:45:36 +00001471 }
1472
drh9cbf3422008-01-17 16:22:13 +00001473 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001474 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001475 sqlite3VdbeMemMove(pOut, &ctx.s);
1476 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001477 goto too_big;
1478 }
drh7b94e7f2011-04-04 12:29:20 +00001479
1480#if 0
1481 /* The app-defined function has done something that as caused this
1482 ** statement to expire. (Perhaps the function called sqlite3_exec()
1483 ** with a CREATE TABLE statement.)
1484 */
1485 if( p->expired ) rc = SQLITE_ABORT;
1486#endif
1487
drh2dcef112008-01-12 19:03:48 +00001488 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001489 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001490 break;
1491}
1492
drh98757152008-01-09 23:04:12 +00001493/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001494**
drh98757152008-01-09 23:04:12 +00001495** Take the bit-wise AND of the values in register P1 and P2 and
1496** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001497** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001498*/
drh98757152008-01-09 23:04:12 +00001499/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001500**
drh98757152008-01-09 23:04:12 +00001501** Take the bit-wise OR of the values in register P1 and P2 and
1502** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001503** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001504*/
drh98757152008-01-09 23:04:12 +00001505/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001506**
drh98757152008-01-09 23:04:12 +00001507** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001508** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001509** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001510** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001511*/
drh98757152008-01-09 23:04:12 +00001512/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001513**
drh98757152008-01-09 23:04:12 +00001514** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001515** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001516** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001517** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001518*/
drh5b6afba2008-01-05 16:29:28 +00001519case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1520case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1521case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1522case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001523 i64 iA;
1524 u64 uA;
1525 i64 iB;
1526 u8 op;
drh6810ce62004-01-31 19:22:56 +00001527
drh3c657212009-11-17 23:59:58 +00001528 pIn1 = &aMem[pOp->p1];
1529 pIn2 = &aMem[pOp->p2];
1530 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001531 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001532 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001533 break;
1534 }
drh158b9cb2011-03-05 20:59:46 +00001535 iA = sqlite3VdbeIntValue(pIn2);
1536 iB = sqlite3VdbeIntValue(pIn1);
1537 op = pOp->opcode;
1538 if( op==OP_BitAnd ){
1539 iA &= iB;
1540 }else if( op==OP_BitOr ){
1541 iA |= iB;
1542 }else if( iB!=0 ){
1543 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1544
1545 /* If shifting by a negative amount, shift in the other direction */
1546 if( iB<0 ){
1547 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1548 op = 2*OP_ShiftLeft + 1 - op;
1549 iB = iB>(-64) ? -iB : 64;
1550 }
1551
1552 if( iB>=64 ){
1553 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1554 }else{
1555 memcpy(&uA, &iA, sizeof(uA));
1556 if( op==OP_ShiftLeft ){
1557 uA <<= iB;
1558 }else{
1559 uA >>= iB;
1560 /* Sign-extend on a right shift of a negative number */
1561 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1562 }
1563 memcpy(&iA, &uA, sizeof(iA));
1564 }
drhbf4133c2001-10-13 02:59:08 +00001565 }
drh158b9cb2011-03-05 20:59:46 +00001566 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001567 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001568 break;
1569}
1570
drh8558cde2008-01-05 05:20:10 +00001571/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001572**
danielk19770cdc0222008-06-26 18:04:03 +00001573** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001574** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001575**
drh8558cde2008-01-05 05:20:10 +00001576** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001577*/
drh9cbf3422008-01-17 16:22:13 +00001578case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001579 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001580 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001581 sqlite3VdbeMemIntegerify(pIn1);
1582 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001583 break;
1584}
1585
drh9cbf3422008-01-17 16:22:13 +00001586/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001587**
drh9cbf3422008-01-17 16:22:13 +00001588** Force the value in register P1 to be an integer. If the value
1589** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001590** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001591** raise an SQLITE_MISMATCH exception.
1592*/
drh9cbf3422008-01-17 16:22:13 +00001593case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001594 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001595 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1596 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001597 if( pOp->p2==0 ){
1598 rc = SQLITE_MISMATCH;
1599 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001600 }else{
drh17c40292004-07-21 02:53:29 +00001601 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001602 }
drh8aff1012001-12-22 14:49:24 +00001603 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001604 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001605 }
1606 break;
1607}
1608
drh13573c72010-01-12 17:04:07 +00001609#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001610/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001611**
drh2133d822008-01-03 18:44:59 +00001612** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001613**
drh8a512562005-11-14 22:29:05 +00001614** This opcode is used when extracting information from a column that
1615** has REAL affinity. Such column values may still be stored as
1616** integers, for space efficiency, but after extraction we want them
1617** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001618*/
drh9cbf3422008-01-17 16:22:13 +00001619case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001620 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001621 if( pIn1->flags & MEM_Int ){
1622 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001623 }
drh487e2622005-06-25 18:42:14 +00001624 break;
1625}
drh13573c72010-01-12 17:04:07 +00001626#endif
drh487e2622005-06-25 18:42:14 +00001627
drh8df447f2005-11-01 15:48:24 +00001628#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001629/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001630**
drh8558cde2008-01-05 05:20:10 +00001631** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001632** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001633** equivalent of printf(). Blob values are unchanged and
1634** are afterwards simply interpreted as text.
1635**
1636** A NULL value is not changed by this routine. It remains NULL.
1637*/
drh9cbf3422008-01-17 16:22:13 +00001638case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001639 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001640 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001641 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001642 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001643 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1644 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1645 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001646 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001647 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001648 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001649 break;
1650}
1651
drh8558cde2008-01-05 05:20:10 +00001652/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001653**
drh8558cde2008-01-05 05:20:10 +00001654** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001655** If the value is numeric, convert it to a string first.
1656** Strings are simply reinterpreted as blobs with no change
1657** to the underlying data.
1658**
1659** A NULL value is not changed by this routine. It remains NULL.
1660*/
drh9cbf3422008-01-17 16:22:13 +00001661case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001662 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001663 if( pIn1->flags & MEM_Null ) break;
1664 if( (pIn1->flags & MEM_Blob)==0 ){
1665 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001666 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001667 MemSetTypeFlag(pIn1, MEM_Blob);
1668 }else{
1669 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001670 }
drhb7654112008-01-12 12:48:07 +00001671 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001672 break;
1673}
drh8a512562005-11-14 22:29:05 +00001674
drh8558cde2008-01-05 05:20:10 +00001675/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001676**
drh8558cde2008-01-05 05:20:10 +00001677** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001678** integer or a floating-point number.)
1679** If the value is text or blob, try to convert it to an using the
1680** equivalent of atoi() or atof() and store 0 if no such conversion
1681** is possible.
1682**
1683** A NULL value is not changed by this routine. It remains NULL.
1684*/
drh9cbf3422008-01-17 16:22:13 +00001685case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001686 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001687 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001688 break;
1689}
1690#endif /* SQLITE_OMIT_CAST */
1691
drh8558cde2008-01-05 05:20:10 +00001692/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001693**
drh710c4842010-08-30 01:17:20 +00001694** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001695** The value is currently a real number, drop its fractional part.
1696** If the value is text or blob, try to convert it to an integer using the
1697** equivalent of atoi() and store 0 if no such conversion is possible.
1698**
1699** A NULL value is not changed by this routine. It remains NULL.
1700*/
drh9cbf3422008-01-17 16:22:13 +00001701case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001702 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001703 if( (pIn1->flags & MEM_Null)==0 ){
1704 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001705 }
1706 break;
1707}
1708
drh13573c72010-01-12 17:04:07 +00001709#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001710/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001711**
drh8558cde2008-01-05 05:20:10 +00001712** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001713** If The value is currently an integer, convert it.
1714** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001715** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001716**
1717** A NULL value is not changed by this routine. It remains NULL.
1718*/
drh9cbf3422008-01-17 16:22:13 +00001719case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001720 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001721 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001722 if( (pIn1->flags & MEM_Null)==0 ){
1723 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001724 }
1725 break;
1726}
drh13573c72010-01-12 17:04:07 +00001727#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001728
drh35573352008-01-08 23:54:25 +00001729/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001730**
drh35573352008-01-08 23:54:25 +00001731** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1732** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001733**
drh35573352008-01-08 23:54:25 +00001734** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1735** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001736** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001737**
drh35573352008-01-08 23:54:25 +00001738** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001739** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001740** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001741** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001742** affinity is used. Note that the affinity conversions are stored
1743** back into the input registers P1 and P3. So this opcode can cause
1744** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001745**
1746** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001747** the values are compared. If both values are blobs then memcmp() is
1748** used to determine the results of the comparison. If both values
1749** are text, then the appropriate collating function specified in
1750** P4 is used to do the comparison. If P4 is not specified then
1751** memcmp() is used to compare text string. If both values are
1752** numeric, then a numeric comparison is used. If the two values
1753** are of different types, then numbers are considered less than
1754** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001755**
drh35573352008-01-08 23:54:25 +00001756** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1757** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001758**
1759** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1760** equal to one another, provided that they do not have their MEM_Cleared
1761** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001762*/
drh9cbf3422008-01-17 16:22:13 +00001763/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001764**
drh35573352008-01-08 23:54:25 +00001765** This works just like the Lt opcode except that the jump is taken if
1766** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001767** additional information.
drh6a2fe092009-09-23 02:29:36 +00001768**
1769** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1770** true or false and is never NULL. If both operands are NULL then the result
1771** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001772** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001773** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001774*/
drh9cbf3422008-01-17 16:22:13 +00001775/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001776**
drh35573352008-01-08 23:54:25 +00001777** This works just like the Lt opcode except that the jump is taken if
1778** the operands in registers P1 and P3 are equal.
1779** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001780**
1781** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1782** true or false and is never NULL. If both operands are NULL then the result
1783** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001784** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001785** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001786*/
drh9cbf3422008-01-17 16:22:13 +00001787/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001788**
drh35573352008-01-08 23:54:25 +00001789** This works just like the Lt opcode except that the jump is taken if
1790** the content of register P3 is less than or equal to the content of
1791** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001792*/
drh9cbf3422008-01-17 16:22:13 +00001793/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001794**
drh35573352008-01-08 23:54:25 +00001795** This works just like the Lt opcode except that the jump is taken if
1796** the content of register P3 is greater than the content of
1797** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001798*/
drh9cbf3422008-01-17 16:22:13 +00001799/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001800**
drh35573352008-01-08 23:54:25 +00001801** This works just like the Lt opcode except that the jump is taken if
1802** the content of register P3 is greater than or equal to the content of
1803** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001804*/
drh9cbf3422008-01-17 16:22:13 +00001805case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1806case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1807case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1808case OP_Le: /* same as TK_LE, jump, in1, in3 */
1809case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1810case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001811 int res; /* Result of the comparison of pIn1 against pIn3 */
1812 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001813 u16 flags1; /* Copy of initial value of pIn1->flags */
1814 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001815
drh3c657212009-11-17 23:59:58 +00001816 pIn1 = &aMem[pOp->p1];
1817 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001818 flags1 = pIn1->flags;
1819 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001820 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001821 /* One or both operands are NULL */
1822 if( pOp->p5 & SQLITE_NULLEQ ){
1823 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1824 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1825 ** or not both operands are null.
1826 */
1827 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001828 assert( (flags1 & MEM_Cleared)==0 );
1829 if( (flags1&MEM_Null)!=0
1830 && (flags3&MEM_Null)!=0
1831 && (flags3&MEM_Cleared)==0
1832 ){
1833 res = 0; /* Results are equal */
1834 }else{
1835 res = 1; /* Results are not equal */
1836 }
drh6a2fe092009-09-23 02:29:36 +00001837 }else{
1838 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1839 ** then the result is always NULL.
1840 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1841 */
1842 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001843 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001844 MemSetTypeFlag(pOut, MEM_Null);
1845 REGISTER_TRACE(pOp->p2, pOut);
1846 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
1847 pc = pOp->p2-1;
1848 }
1849 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001850 }
drh6a2fe092009-09-23 02:29:36 +00001851 }else{
1852 /* Neither operand is NULL. Do a comparison. */
1853 affinity = pOp->p5 & SQLITE_AFF_MASK;
1854 if( affinity ){
1855 applyAffinity(pIn1, affinity, encoding);
1856 applyAffinity(pIn3, affinity, encoding);
1857 if( db->mallocFailed ) goto no_mem;
1858 }
danielk1977a37cdde2004-05-16 11:15:36 +00001859
drh6a2fe092009-09-23 02:29:36 +00001860 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1861 ExpandBlob(pIn1);
1862 ExpandBlob(pIn3);
1863 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001864 }
danielk1977a37cdde2004-05-16 11:15:36 +00001865 switch( pOp->opcode ){
1866 case OP_Eq: res = res==0; break;
1867 case OP_Ne: res = res!=0; break;
1868 case OP_Lt: res = res<0; break;
1869 case OP_Le: res = res<=0; break;
1870 case OP_Gt: res = res>0; break;
1871 default: res = res>=0; break;
1872 }
1873
drh35573352008-01-08 23:54:25 +00001874 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001875 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001876 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001877 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001878 pOut->u.i = res;
1879 REGISTER_TRACE(pOp->p2, pOut);
1880 }else if( res ){
1881 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001882 }
danb7dca7d2010-03-05 16:32:12 +00001883
1884 /* Undo any changes made by applyAffinity() to the input registers. */
1885 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1886 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001887 break;
1888}
drhc9b84a12002-06-20 11:36:48 +00001889
drh0acb7e42008-06-25 00:12:41 +00001890/* Opcode: Permutation * * * P4 *
1891**
shanebe217792009-03-05 04:20:31 +00001892** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001893** of integers in P4.
1894**
drh953f7612012-12-07 22:18:54 +00001895** The permutation is only valid until the next OP_Compare that has
1896** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1897** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001898*/
1899case OP_Permutation: {
1900 assert( pOp->p4type==P4_INTARRAY );
1901 assert( pOp->p4.ai );
1902 aPermute = pOp->p4.ai;
1903 break;
1904}
1905
drh953f7612012-12-07 22:18:54 +00001906/* Opcode: Compare P1 P2 P3 P4 P5
drh16ee60f2008-06-20 18:13:25 +00001907**
drh710c4842010-08-30 01:17:20 +00001908** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1909** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001910** the comparison for use by the next OP_Jump instruct.
1911**
drh0acb7e42008-06-25 00:12:41 +00001912** P4 is a KeyInfo structure that defines collating sequences and sort
1913** orders for the comparison. The permutation applies to registers
1914** only. The KeyInfo elements are used sequentially.
1915**
1916** The comparison is a sort comparison, so NULLs compare equal,
1917** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001918** and strings are less than blobs.
1919*/
1920case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001921 int n;
1922 int i;
1923 int p1;
1924 int p2;
1925 const KeyInfo *pKeyInfo;
1926 int idx;
1927 CollSeq *pColl; /* Collating sequence to use on this term */
1928 int bRev; /* True for DESCENDING sort order */
1929
drh953f7612012-12-07 22:18:54 +00001930 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00001931 n = pOp->p3;
1932 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001933 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001934 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001935 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001936 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00001937#if SQLITE_DEBUG
1938 if( aPermute ){
1939 int k, mx = 0;
1940 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
1941 assert( p1>0 && p1+mx<=p->nMem+1 );
1942 assert( p2>0 && p2+mx<=p->nMem+1 );
1943 }else{
1944 assert( p1>0 && p1+n<=p->nMem+1 );
1945 assert( p2>0 && p2+n<=p->nMem+1 );
1946 }
1947#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00001948 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001949 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00001950 assert( memIsValid(&aMem[p1+idx]) );
1951 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00001952 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
1953 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001954 assert( i<pKeyInfo->nField );
1955 pColl = pKeyInfo->aColl[i];
1956 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00001957 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00001958 if( iCompare ){
1959 if( bRev ) iCompare = -iCompare;
1960 break;
1961 }
drh16ee60f2008-06-20 18:13:25 +00001962 }
drh0acb7e42008-06-25 00:12:41 +00001963 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001964 break;
1965}
1966
1967/* Opcode: Jump P1 P2 P3 * *
1968**
1969** Jump to the instruction at address P1, P2, or P3 depending on whether
1970** in the most recent OP_Compare instruction the P1 vector was less than
1971** equal to, or greater than the P2 vector, respectively.
1972*/
drh0acb7e42008-06-25 00:12:41 +00001973case OP_Jump: { /* jump */
1974 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001975 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001976 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001977 pc = pOp->p2 - 1;
1978 }else{
1979 pc = pOp->p3 - 1;
1980 }
1981 break;
1982}
1983
drh5b6afba2008-01-05 16:29:28 +00001984/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001985**
drh5b6afba2008-01-05 16:29:28 +00001986** Take the logical AND of the values in registers P1 and P2 and
1987** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00001988**
drh5b6afba2008-01-05 16:29:28 +00001989** If either P1 or P2 is 0 (false) then the result is 0 even if
1990** the other input is NULL. A NULL and true or two NULLs give
1991** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00001992*/
drh5b6afba2008-01-05 16:29:28 +00001993/* Opcode: Or P1 P2 P3 * *
1994**
1995** Take the logical OR of the values in register P1 and P2 and
1996** store the answer in register P3.
1997**
1998** If either P1 or P2 is nonzero (true) then the result is 1 (true)
1999** even if the other input is NULL. A NULL and false or two NULLs
2000** give a NULL output.
2001*/
2002case OP_And: /* same as TK_AND, in1, in2, out3 */
2003case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002004 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2005 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002006
drh3c657212009-11-17 23:59:58 +00002007 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002008 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002009 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002010 }else{
drh5b6afba2008-01-05 16:29:28 +00002011 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002012 }
drh3c657212009-11-17 23:59:58 +00002013 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002014 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002015 v2 = 2;
2016 }else{
drh5b6afba2008-01-05 16:29:28 +00002017 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002018 }
2019 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002020 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002021 v1 = and_logic[v1*3+v2];
2022 }else{
drh5b6afba2008-01-05 16:29:28 +00002023 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002024 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002025 }
drh3c657212009-11-17 23:59:58 +00002026 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002027 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002028 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002029 }else{
drh5b6afba2008-01-05 16:29:28 +00002030 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002031 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002032 }
drh5e00f6c2001-09-13 13:46:56 +00002033 break;
2034}
2035
drhe99fa2a2008-12-15 15:27:51 +00002036/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002037**
drhe99fa2a2008-12-15 15:27:51 +00002038** Interpret the value in register P1 as a boolean value. Store the
2039** boolean complement in register P2. If the value in register P1 is
2040** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002041*/
drh93952eb2009-11-13 19:43:43 +00002042case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002043 pIn1 = &aMem[pOp->p1];
2044 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002045 if( pIn1->flags & MEM_Null ){
2046 sqlite3VdbeMemSetNull(pOut);
2047 }else{
2048 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2049 }
drh5e00f6c2001-09-13 13:46:56 +00002050 break;
2051}
2052
drhe99fa2a2008-12-15 15:27:51 +00002053/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00002054**
drhe99fa2a2008-12-15 15:27:51 +00002055** Interpret the content of register P1 as an integer. Store the
2056** ones-complement of the P1 value into register P2. If P1 holds
2057** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002058*/
drh93952eb2009-11-13 19:43:43 +00002059case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002060 pIn1 = &aMem[pOp->p1];
2061 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002062 if( pIn1->flags & MEM_Null ){
2063 sqlite3VdbeMemSetNull(pOut);
2064 }else{
2065 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2066 }
drhbf4133c2001-10-13 02:59:08 +00002067 break;
2068}
2069
drh48f2d3b2011-09-16 01:34:43 +00002070/* Opcode: Once P1 P2 * * *
2071**
dan1d8cb212011-12-09 13:24:16 +00002072** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
2073** set the flag and fall through to the next instruction.
drhb8475df2011-12-09 16:21:19 +00002074**
2075** See also: JumpOnce
drh48f2d3b2011-09-16 01:34:43 +00002076*/
dan1d8cb212011-12-09 13:24:16 +00002077case OP_Once: { /* jump */
2078 assert( pOp->p1<p->nOnceFlag );
2079 if( p->aOnceFlag[pOp->p1] ){
2080 pc = pOp->p2-1;
2081 }else{
2082 p->aOnceFlag[pOp->p1] = 1;
2083 }
2084 break;
2085}
2086
drh3c84ddf2008-01-09 02:15:38 +00002087/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002088**
drhef8662b2011-06-20 21:47:58 +00002089** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002090** is considered true if it is numeric and non-zero. If the value
drhb8475df2011-12-09 16:21:19 +00002091** in P1 is NULL then take the jump if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002092*/
drh3c84ddf2008-01-09 02:15:38 +00002093/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002094**
drhef8662b2011-06-20 21:47:58 +00002095** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002096** is considered false if it has a numeric value of zero. If the value
2097** in P1 is NULL then take the jump if P3 is zero.
drhf5905aa2002-05-26 20:54:33 +00002098*/
drh9cbf3422008-01-17 16:22:13 +00002099case OP_If: /* jump, in1 */
2100case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002101 int c;
drh3c657212009-11-17 23:59:58 +00002102 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002103 if( pIn1->flags & MEM_Null ){
2104 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002105 }else{
drhba0232a2005-06-06 17:27:19 +00002106#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002107 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002108#else
drh3c84ddf2008-01-09 02:15:38 +00002109 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002110#endif
drhf5905aa2002-05-26 20:54:33 +00002111 if( pOp->opcode==OP_IfNot ) c = !c;
2112 }
drh3c84ddf2008-01-09 02:15:38 +00002113 if( c ){
2114 pc = pOp->p2-1;
2115 }
drh5e00f6c2001-09-13 13:46:56 +00002116 break;
2117}
2118
drh830ecf92009-06-18 00:41:55 +00002119/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00002120**
drh830ecf92009-06-18 00:41:55 +00002121** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002122*/
drh9cbf3422008-01-17 16:22:13 +00002123case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002124 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002125 if( (pIn1->flags & MEM_Null)!=0 ){
2126 pc = pOp->p2 - 1;
2127 }
drh477df4b2008-01-05 18:48:24 +00002128 break;
2129}
2130
drh98757152008-01-09 23:04:12 +00002131/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002132**
drh6a288a32008-01-07 19:20:24 +00002133** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002134*/
drh9cbf3422008-01-17 16:22:13 +00002135case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002136 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002137 if( (pIn1->flags & MEM_Null)==0 ){
2138 pc = pOp->p2 - 1;
2139 }
drh5e00f6c2001-09-13 13:46:56 +00002140 break;
2141}
2142
drh3e9ca092009-09-08 01:14:48 +00002143/* Opcode: Column P1 P2 P3 P4 P5
danielk1977192ac1d2004-05-10 07:17:30 +00002144**
danielk1977cfcdaef2004-05-12 07:33:33 +00002145** Interpret the data that cursor P1 points to as a structure built using
2146** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002147** information about the format of the data.) Extract the P2-th column
2148** from this record. If there are less that (P2+1)
2149** values in the record, extract a NULL.
2150**
drh9cbf3422008-01-17 16:22:13 +00002151** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002152**
danielk19771f4aa332008-01-03 09:51:55 +00002153** If the column contains fewer than P2 fields, then extract a NULL. Or,
2154** if the P4 argument is a P4_MEM use the value of the P4 argument as
2155** the result.
drh3e9ca092009-09-08 01:14:48 +00002156**
2157** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2158** then the cache of the cursor is reset prior to extracting the column.
2159** The first OP_Column against a pseudo-table after the value of the content
2160** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002161**
drhdda5c082012-03-28 13:41:10 +00002162** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2163** the result is guaranteed to only be used as the argument of a length()
2164** or typeof() function, respectively. The loading of large blobs can be
2165** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002166*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002167case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002168 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002169 i64 payloadSize64; /* Number of bytes in the record */
2170 int p1; /* P1 value of the opcode */
2171 int p2; /* column number to retrieve */
2172 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002173 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002174 BtCursor *pCrsr; /* The BTree cursor */
2175 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2176 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002177 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002178 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002179 int i; /* Loop counter */
2180 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002181 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002182 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002183 u8 *zIdx; /* Index into header */
2184 u8 *zEndHdr; /* Pointer to first byte after the header */
2185 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002186 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002187 int szHdr; /* Size of the header size field at start of record */
2188 int avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002189 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002190 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002191
drh856c1032009-06-02 15:21:42 +00002192
2193 p1 = pOp->p1;
2194 p2 = pOp->p2;
2195 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002196 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002197 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002198 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002199 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002200 memAboutToChange(p, pDest);
shane36840fd2009-06-26 16:32:13 +00002201 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002202
drhe61cffc2004-06-12 18:12:15 +00002203 /* This block sets the variable payloadSize to be the total number of
2204 ** bytes in the record.
2205 **
2206 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002207 ** The complete record text is always available for pseudo-tables
2208 ** If the record is stored in a cursor, the complete record text
2209 ** might be available in the pC->aRow cache. Or it might not be.
2210 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002211 **
2212 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002213 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002214 */
drhb73857f2006-03-17 00:25:59 +00002215 pC = p->apCsr[p1];
drha5759672012-10-30 14:39:12 +00002216 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002217#ifndef SQLITE_OMIT_VIRTUALTABLE
2218 assert( pC->pVtabCursor==0 );
2219#endif
shane36840fd2009-06-26 16:32:13 +00002220 pCrsr = pC->pCursor;
2221 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002222 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002223 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002224 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002225 if( pC->nullRow ){
2226 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002227 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002228 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002229 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002230 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002231 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002232 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drhc27ae612009-07-14 18:35:44 +00002233 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002234 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2235 ** payload size, so it is impossible for payloadSize64 to be
2236 ** larger than 32 bits. */
2237 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002238 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002239 }else{
drhea8ffdf2009-07-22 00:35:23 +00002240 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002241 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002242 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002243 }
drh4a6f3aa2011-08-28 00:19:26 +00002244 }else if( ALWAYS(pC->pseudoTableReg>0) ){
drha6c2ed92009-11-14 23:22:23 +00002245 pReg = &aMem[pC->pseudoTableReg];
drh21172c42012-10-30 00:29:07 +00002246 if( pC->multiPseudo ){
2247 sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
2248 Deephemeralize(pDest);
2249 goto op_column_out;
2250 }
drh3e9ca092009-09-08 01:14:48 +00002251 assert( pReg->flags & MEM_Blob );
drh2b4ded92010-09-27 21:09:31 +00002252 assert( memIsValid(pReg) );
drh3e9ca092009-09-08 01:14:48 +00002253 payloadSize = pReg->n;
2254 zRec = pReg->z;
2255 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002256 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002257 }else{
2258 /* Consider the row to be NULL */
2259 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002260 }
2261
drhe6f43fc2011-08-28 02:15:34 +00002262 /* If payloadSize is 0, then just store a NULL. This can happen because of
2263 ** nullRow or because of a corrupt database. */
danielk1977192ac1d2004-05-10 07:17:30 +00002264 if( payloadSize==0 ){
drhe6f43fc2011-08-28 02:15:34 +00002265 MemSetTypeFlag(pDest, MEM_Null);
drhd4e70eb2008-01-02 00:34:36 +00002266 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002267 }
drh35cd6432009-06-05 14:17:21 +00002268 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2269 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002270 goto too_big;
2271 }
danielk1977192ac1d2004-05-10 07:17:30 +00002272
shane36840fd2009-06-26 16:32:13 +00002273 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002274 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002275
drh9188b382004-05-14 21:12:22 +00002276 /* Read and parse the table header. Store the results of the parse
2277 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002278 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002279 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002280 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002281 aOffset = pC->aOffset;
2282 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002283 assert(aType);
drh856c1032009-06-02 15:21:42 +00002284 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002285 pC->aOffset = aOffset = &aType[nField];
2286 pC->payloadSize = payloadSize;
2287 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002288
drhd3194f52004-05-27 19:59:32 +00002289 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002290 if( zRec ){
2291 zData = zRec;
2292 }else{
drhf0863fe2005-06-12 21:35:51 +00002293 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002294 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002295 }else{
drhe51c44f2004-05-30 20:46:09 +00002296 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002297 }
drhe61cffc2004-06-12 18:12:15 +00002298 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2299 ** save the payload in the pC->aRow cache. That will save us from
2300 ** having to make additional calls to fetch the content portion of
2301 ** the record.
2302 */
drh35cd6432009-06-05 14:17:21 +00002303 assert( avail>=0 );
2304 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002305 zRec = zData;
2306 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002307 }else{
2308 pC->aRow = 0;
2309 }
drhd3194f52004-05-27 19:59:32 +00002310 }
drhdda5c082012-03-28 13:41:10 +00002311 /* The following assert is true in all cases except when
drh588f5bc2007-01-02 18:41:54 +00002312 ** the database file has been corrupted externally.
2313 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002314 szHdr = getVarint32((u8*)zData, offset);
2315
2316 /* Make sure a corrupt database has not given us an oversize header.
2317 ** Do this now to avoid an oversize memory allocation.
2318 **
2319 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2320 ** types use so much data space that there can only be 4096 and 32 of
2321 ** them, respectively. So the maximum header length results from a
2322 ** 3-byte type for each of the maximum of 32768 columns plus three
2323 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2324 */
2325 if( offset > 98307 ){
2326 rc = SQLITE_CORRUPT_BKPT;
2327 goto op_column_out;
2328 }
2329
2330 /* Compute in len the number of bytes of data we need to read in order
2331 ** to get nField type values. offset is an upper bound on this. But
2332 ** nField might be significantly less than the true number of columns
2333 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2334 ** We want to minimize len in order to limit the size of the memory
2335 ** allocation, especially if a corrupt database file has caused offset
2336 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2337 ** still exceed Robson memory allocation limits on some configurations.
2338 ** On systems that cannot tolerate large memory allocations, nField*5+3
2339 ** will likely be much smaller since nField will likely be less than
2340 ** 20 or so. This insures that Robson memory allocation limits are
2341 ** not exceeded even for corrupt database files.
2342 */
2343 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002344 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002345
2346 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2347 ** record header in most cases. But they will fail to get the complete
2348 ** record header if the record header does not fit on a single page
2349 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2350 ** acquire the complete header text.
2351 */
drh35cd6432009-06-05 14:17:21 +00002352 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002353 sMem.flags = 0;
2354 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002355 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002356 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002357 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002358 }
drhb6f54522004-05-20 02:42:16 +00002359 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002360 }
drh35cd6432009-06-05 14:17:21 +00002361 zEndHdr = (u8 *)&zData[len];
2362 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002363
drhd3194f52004-05-27 19:59:32 +00002364 /* Scan the header and use it to fill in the aType[] and aOffset[]
2365 ** arrays. aType[i] will contain the type integer for the i-th
2366 ** column and aOffset[i] will contain the offset from the beginning
2367 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002368 */
danielk1977dedf45b2006-01-13 17:12:01 +00002369 for(i=0; i<nField; i++){
2370 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002371 aOffset[i] = offset;
drh5a077b72011-08-29 02:16:18 +00002372 if( zIdx[0]<0x80 ){
2373 t = zIdx[0];
2374 zIdx++;
2375 }else{
2376 zIdx += sqlite3GetVarint32(zIdx, &t);
2377 }
2378 aType[i] = t;
2379 szField = sqlite3VdbeSerialTypeLen(t);
drh6658cd92010-02-05 14:12:53 +00002380 offset += szField;
2381 if( offset<szField ){ /* True if offset overflows */
2382 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2383 break;
2384 }
danielk1977dedf45b2006-01-13 17:12:01 +00002385 }else{
drhdda5c082012-03-28 13:41:10 +00002386 /* If i is less that nField, then there are fewer fields in this
danielk1977dedf45b2006-01-13 17:12:01 +00002387 ** record than SetNumColumns indicated there are columns in the
2388 ** table. Set the offset for any extra columns not present in
drhdda5c082012-03-28 13:41:10 +00002389 ** the record to 0. This tells code below to store the default value
2390 ** for the column instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002391 */
2392 aOffset[i] = 0;
2393 }
drh9188b382004-05-14 21:12:22 +00002394 }
danielk19775f096132008-03-28 15:44:09 +00002395 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002396 sMem.flags = MEM_Null;
2397
danielk19779792eef2006-01-13 15:58:43 +00002398 /* If we have read more header data than was contained in the header,
2399 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002400 ** record, or if the end of the last field appears to be before the end
2401 ** of the record (when all fields present), then we must be dealing
2402 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002403 */
drh6658cd92010-02-05 14:12:53 +00002404 if( (zIdx > zEndHdr) || (offset > payloadSize)
2405 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002406 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002407 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002408 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002409 }
danielk1977192ac1d2004-05-10 07:17:30 +00002410
danielk197736963fd2005-02-19 08:18:05 +00002411 /* Get the column information. If aOffset[p2] is non-zero, then
2412 ** deserialize the value from the record. If aOffset[p2] is zero,
2413 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002414 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002415 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002416 */
danielk197736963fd2005-02-19 08:18:05 +00002417 if( aOffset[p2] ){
2418 assert( rc==SQLITE_OK );
2419 if( zRec ){
drhac5e7492012-03-28 16:14:50 +00002420 /* This is the common case where the whole row fits on a single page */
drhe4c88c02012-01-04 12:57:45 +00002421 VdbeMemRelease(pDest);
danielk1977808ec7c2008-07-29 10:18:57 +00002422 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002423 }else{
drhac5e7492012-03-28 16:14:50 +00002424 /* This branch happens only when the row overflows onto multiple pages */
drhdda5c082012-03-28 13:41:10 +00002425 t = aType[p2];
drha748fdc2012-03-28 01:34:47 +00002426 if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
drhdda5c082012-03-28 13:41:10 +00002427 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
drha748fdc2012-03-28 01:34:47 +00002428 ){
2429 /* Content is irrelevant for the typeof() function and for
drhdda5c082012-03-28 13:41:10 +00002430 ** the length(X) function if X is a blob. So we might as well use
drha748fdc2012-03-28 01:34:47 +00002431 ** bogus content rather than reading content from disk. NULL works
2432 ** for text and blob and whatever is in the payloadSize64 variable
2433 ** will work for everything else. */
2434 zData = t<12 ? (char*)&payloadSize64 : 0;
2435 }else{
drhac5e7492012-03-28 16:14:50 +00002436 len = sqlite3VdbeSerialTypeLen(t);
drha748fdc2012-03-28 01:34:47 +00002437 sqlite3VdbeMemMove(&sMem, pDest);
2438 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,
2439 &sMem);
2440 if( rc!=SQLITE_OK ){
2441 goto op_column_out;
2442 }
2443 zData = sMem.z;
danielk197736963fd2005-02-19 08:18:05 +00002444 }
drhdda5c082012-03-28 13:41:10 +00002445 sqlite3VdbeSerialGet((u8*)zData, t, pDest);
danielk19777701e812005-01-10 12:59:51 +00002446 }
drhd4e70eb2008-01-02 00:34:36 +00002447 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002448 }else{
danielk197760585dd2008-01-03 08:08:40 +00002449 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002450 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002451 }else{
drhe6f43fc2011-08-28 02:15:34 +00002452 MemSetTypeFlag(pDest, MEM_Null);
danielk1977aee18ef2005-03-09 12:26:50 +00002453 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002454 }
drhfebe1062004-08-28 18:17:48 +00002455
2456 /* If we dynamically allocated space to hold the data (in the
2457 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002458 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002459 ** This prevents a memory copy.
2460 */
danielk19775f096132008-03-28 15:44:09 +00002461 if( sMem.zMalloc ){
2462 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002463 assert( !(pDest->flags & MEM_Dyn) );
2464 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2465 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002466 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002467 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002468 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002469 }
drhfebe1062004-08-28 18:17:48 +00002470
drhd4e70eb2008-01-02 00:34:36 +00002471 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002472
danielk19773c9cc8d2005-01-17 03:40:08 +00002473op_column_out:
drhb7654112008-01-12 12:48:07 +00002474 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002475 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002476 break;
2477}
2478
danielk1977751de562008-04-18 09:01:15 +00002479/* Opcode: Affinity P1 P2 * P4 *
2480**
2481** Apply affinities to a range of P2 registers starting with P1.
2482**
2483** P4 is a string that is P2 characters long. The nth character of the
2484** string indicates the column affinity that should be used for the nth
2485** memory cell in the range.
2486*/
2487case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002488 const char *zAffinity; /* The affinity to be applied */
2489 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002490
drh856c1032009-06-02 15:21:42 +00002491 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002492 assert( zAffinity!=0 );
2493 assert( zAffinity[pOp->p2]==0 );
2494 pIn1 = &aMem[pOp->p1];
2495 while( (cAff = *(zAffinity++))!=0 ){
2496 assert( pIn1 <= &p->aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00002497 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002498 ExpandBlob(pIn1);
2499 applyAffinity(pIn1, cAff, encoding);
2500 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002501 }
2502 break;
2503}
2504
drh1db639c2008-01-17 02:36:28 +00002505/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002506**
drh710c4842010-08-30 01:17:20 +00002507** Convert P2 registers beginning with P1 into the [record format]
2508** use as a data record in a database table or as a key
2509** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002510**
danielk1977751de562008-04-18 09:01:15 +00002511** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002512** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002513** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002514**
drh8a512562005-11-14 22:29:05 +00002515** The mapping from character to affinity is given by the SQLITE_AFF_
2516** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002517**
drh66a51672008-01-03 00:01:23 +00002518** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002519*/
drh1db639c2008-01-17 02:36:28 +00002520case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002521 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2522 Mem *pRec; /* The new record */
2523 u64 nData; /* Number of bytes of data space */
2524 int nHdr; /* Number of bytes of header space */
2525 i64 nByte; /* Data space required for this record */
2526 int nZero; /* Number of zero bytes at the end of the record */
2527 int nVarint; /* Number of bytes in a varint */
2528 u32 serial_type; /* Type field */
2529 Mem *pData0; /* First field to be combined into the record */
2530 Mem *pLast; /* Last field of the record */
2531 int nField; /* Number of fields in the record */
2532 char *zAffinity; /* The affinity string for the record */
2533 int file_format; /* File format to use for encoding */
2534 int i; /* Space used in zNewRecord[] */
2535 int len; /* Length of a field */
2536
drhf3218fe2004-05-28 08:21:02 +00002537 /* Assuming the record contains N fields, the record format looks
2538 ** like this:
2539 **
drh7a224de2004-06-02 01:22:02 +00002540 ** ------------------------------------------------------------------------
2541 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2542 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002543 **
drh9cbf3422008-01-17 16:22:13 +00002544 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2545 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002546 **
2547 ** Each type field is a varint representing the serial type of the
2548 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002549 ** hdr-size field is also a varint which is the offset from the beginning
2550 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002551 */
drh856c1032009-06-02 15:21:42 +00002552 nData = 0; /* Number of bytes of data space */
2553 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002554 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002555 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002556 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002557 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drha6c2ed92009-11-14 23:22:23 +00002558 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002559 nField = pOp->p2;
2560 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002561 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002562
drh2b4ded92010-09-27 21:09:31 +00002563 /* Identify the output register */
2564 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2565 pOut = &aMem[pOp->p3];
2566 memAboutToChange(p, pOut);
2567
drhf3218fe2004-05-28 08:21:02 +00002568 /* Loop through the elements that will make up the record to figure
2569 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002570 */
drha2a49dc2008-01-02 14:28:13 +00002571 for(pRec=pData0; pRec<=pLast; pRec++){
drh2b4ded92010-09-27 21:09:31 +00002572 assert( memIsValid(pRec) );
drhd3d39e92004-05-20 22:16:29 +00002573 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002574 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002575 }
danielk1977d908f5a2007-05-11 07:08:28 +00002576 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002577 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002578 }
drhd946db02005-12-29 19:23:06 +00002579 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002580 len = sqlite3VdbeSerialTypeLen(serial_type);
2581 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002582 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002583 if( pRec->flags & MEM_Zero ){
2584 /* Only pure zero-filled BLOBs can be input to this Opcode.
2585 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002586 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002587 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002588 nZero = 0;
2589 }
danielk19778d059842004-05-12 11:24:02 +00002590 }
danielk19773d1bfea2004-05-14 11:00:53 +00002591
drhf3218fe2004-05-28 08:21:02 +00002592 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002593 nHdr += nVarint = sqlite3VarintLen(nHdr);
2594 if( nVarint<sqlite3VarintLen(nHdr) ){
2595 nHdr++;
2596 }
drhfdf972a2007-05-02 13:30:27 +00002597 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002598 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002599 goto too_big;
2600 }
drhf3218fe2004-05-28 08:21:02 +00002601
danielk1977a7a8e142008-02-13 18:25:27 +00002602 /* Make sure the output register has a buffer large enough to store
2603 ** the new record. The output register (pOp->p3) is not allowed to
2604 ** be one of the input registers (because the following call to
2605 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2606 */
drh9c1905f2008-12-10 22:32:56 +00002607 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002608 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002609 }
danielk1977a7a8e142008-02-13 18:25:27 +00002610 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002611
2612 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002613 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002614 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002615 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002616 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002617 }
drha2a49dc2008-01-02 14:28:13 +00002618 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002619 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002620 }
drhfdf972a2007-05-02 13:30:27 +00002621 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002622
drh9cbf3422008-01-17 16:22:13 +00002623 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002624 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002625 pOut->flags = MEM_Blob | MEM_Dyn;
2626 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002627 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002628 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002629 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002630 }
drh477df4b2008-01-05 18:48:24 +00002631 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002632 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002633 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002634 break;
2635}
2636
danielk1977a5533162009-02-24 10:01:51 +00002637/* Opcode: Count P1 P2 * * *
2638**
2639** Store the number of entries (an integer value) in the table or index
2640** opened by cursor P1 in register P2
2641*/
2642#ifndef SQLITE_OMIT_BTREECOUNT
2643case OP_Count: { /* out2-prerelease */
2644 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002645 BtCursor *pCrsr;
2646
2647 pCrsr = p->apCsr[pOp->p1]->pCursor;
dana205a482011-08-27 18:48:57 +00002648 if( ALWAYS(pCrsr) ){
drh818e39a2009-04-02 20:27:28 +00002649 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2650 }else{
2651 nEntry = 0;
2652 }
danielk1977a5533162009-02-24 10:01:51 +00002653 pOut->u.i = nEntry;
2654 break;
2655}
2656#endif
2657
danielk1977fd7f0452008-12-17 17:30:26 +00002658/* Opcode: Savepoint P1 * * P4 *
2659**
2660** Open, release or rollback the savepoint named by parameter P4, depending
2661** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2662** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2663*/
2664case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002665 int p1; /* Value of P1 operand */
2666 char *zName; /* Name of savepoint */
2667 int nName;
2668 Savepoint *pNew;
2669 Savepoint *pSavepoint;
2670 Savepoint *pTmp;
2671 int iSavepoint;
2672 int ii;
2673
2674 p1 = pOp->p1;
2675 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002676
2677 /* Assert that the p1 parameter is valid. Also that if there is no open
2678 ** transaction, then there cannot be any savepoints.
2679 */
2680 assert( db->pSavepoint==0 || db->autoCommit==0 );
2681 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2682 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2683 assert( checkSavepointCount(db) );
2684
2685 if( p1==SAVEPOINT_BEGIN ){
danielk197734cf35d2008-12-18 18:31:38 +00002686 if( db->writeVdbeCnt>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002687 /* A new savepoint cannot be created if there are active write
2688 ** statements (i.e. open read/write incremental blob handles).
2689 */
2690 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2691 "SQL statements in progress");
2692 rc = SQLITE_BUSY;
2693 }else{
drh856c1032009-06-02 15:21:42 +00002694 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002695
drhbe07ec52011-06-03 12:15:26 +00002696#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002697 /* This call is Ok even if this savepoint is actually a transaction
2698 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2699 ** If this is a transaction savepoint being opened, it is guaranteed
2700 ** that the db->aVTrans[] array is empty. */
2701 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002702 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2703 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002704 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002705#endif
dand9495cd2011-04-27 12:08:04 +00002706
danielk1977fd7f0452008-12-17 17:30:26 +00002707 /* Create a new savepoint structure. */
2708 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2709 if( pNew ){
2710 pNew->zName = (char *)&pNew[1];
2711 memcpy(pNew->zName, zName, nName+1);
2712
2713 /* If there is no open transaction, then mark this as a special
2714 ** "transaction savepoint". */
2715 if( db->autoCommit ){
2716 db->autoCommit = 0;
2717 db->isTransactionSavepoint = 1;
2718 }else{
2719 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002720 }
danielk1977fd7f0452008-12-17 17:30:26 +00002721
2722 /* Link the new savepoint into the database handle's list. */
2723 pNew->pNext = db->pSavepoint;
2724 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002725 pNew->nDeferredCons = db->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002726 }
2727 }
2728 }else{
drh856c1032009-06-02 15:21:42 +00002729 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002730
2731 /* Find the named savepoint. If there is no such savepoint, then an
2732 ** an error is returned to the user. */
2733 for(
drh856c1032009-06-02 15:21:42 +00002734 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002735 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002736 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002737 ){
2738 iSavepoint++;
2739 }
2740 if( !pSavepoint ){
2741 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2742 rc = SQLITE_ERROR;
drh0f198a72012-02-13 16:43:16 +00002743 }else if( db->writeVdbeCnt>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002744 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002745 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002746 */
2747 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002748 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002749 );
2750 rc = SQLITE_BUSY;
2751 }else{
2752
2753 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002754 ** and this is a RELEASE command, then the current transaction
2755 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002756 */
2757 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2758 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002759 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002760 goto vdbe_return;
2761 }
danielk1977fd7f0452008-12-17 17:30:26 +00002762 db->autoCommit = 1;
2763 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2764 p->pc = pc;
2765 db->autoCommit = 0;
2766 p->rc = rc = SQLITE_BUSY;
2767 goto vdbe_return;
2768 }
danielk197734cf35d2008-12-18 18:31:38 +00002769 db->isTransactionSavepoint = 0;
2770 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002771 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002772 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002773 if( p1==SAVEPOINT_ROLLBACK ){
2774 for(ii=0; ii<db->nDb; ii++){
2775 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2776 }
drh0f198a72012-02-13 16:43:16 +00002777 }
2778 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002779 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2780 if( rc!=SQLITE_OK ){
2781 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002782 }
danielk1977fd7f0452008-12-17 17:30:26 +00002783 }
drh9f0bbf92009-01-02 21:08:09 +00002784 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002785 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002786 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002787 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002788 }
2789 }
2790
2791 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2792 ** savepoints nested inside of the savepoint being operated on. */
2793 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002794 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002795 db->pSavepoint = pTmp->pNext;
2796 sqlite3DbFree(db, pTmp);
2797 db->nSavepoint--;
2798 }
2799
dan1da40a32009-09-19 17:00:31 +00002800 /* If it is a RELEASE, then destroy the savepoint being operated on
2801 ** too. If it is a ROLLBACK TO, then set the number of deferred
2802 ** constraint violations present in the database to the value stored
2803 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002804 if( p1==SAVEPOINT_RELEASE ){
2805 assert( pSavepoint==db->pSavepoint );
2806 db->pSavepoint = pSavepoint->pNext;
2807 sqlite3DbFree(db, pSavepoint);
2808 if( !isTransaction ){
2809 db->nSavepoint--;
2810 }
dan1da40a32009-09-19 17:00:31 +00002811 }else{
2812 db->nDeferredCons = pSavepoint->nDeferredCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002813 }
dand9495cd2011-04-27 12:08:04 +00002814
2815 if( !isTransaction ){
2816 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2817 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2818 }
danielk1977fd7f0452008-12-17 17:30:26 +00002819 }
2820 }
2821
2822 break;
2823}
2824
drh98757152008-01-09 23:04:12 +00002825/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002826**
2827** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002828** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002829** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2830** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002831**
2832** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002833*/
drh9cbf3422008-01-17 16:22:13 +00002834case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002835 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002836 int iRollback;
drh856c1032009-06-02 15:21:42 +00002837 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002838
drh856c1032009-06-02 15:21:42 +00002839 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002840 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002841 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002842 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002843 assert( desiredAutoCommit==1 || iRollback==0 );
drh92f02c32004-09-02 14:57:08 +00002844 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002845
drh0f198a72012-02-13 16:43:16 +00002846#if 0
shane68c02732009-06-09 18:14:18 +00002847 if( turnOnAC && iRollback && db->activeVdbeCnt>1 ){
drhad4a4b82008-11-05 16:37:34 +00002848 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002849 ** still running, and a transaction is active, return an error indicating
2850 ** that the other VMs must complete first.
2851 */
drhad4a4b82008-11-05 16:37:34 +00002852 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2853 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002854 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002855 }else
2856#endif
2857 if( turnOnAC && !iRollback && db->writeVdbeCnt>0 ){
drhad4a4b82008-11-05 16:37:34 +00002858 /* If this instruction implements a COMMIT and other VMs are writing
2859 ** return an error indicating that the other VMs must complete first.
2860 */
2861 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2862 "SQL statements in progress");
2863 rc = SQLITE_BUSY;
2864 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002865 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002866 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002867 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002868 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002869 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002870 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002871 }else{
shane7d3846a2008-12-11 02:58:26 +00002872 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002873 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002874 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002875 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002876 p->rc = rc = SQLITE_BUSY;
2877 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002878 }
danielk19771d850a72004-05-31 08:26:49 +00002879 }
danielk1977bd434552009-03-18 10:33:00 +00002880 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002881 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002882 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002883 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002884 }else{
drh900b31e2007-08-28 02:27:51 +00002885 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002886 }
drh900b31e2007-08-28 02:27:51 +00002887 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002888 }else{
drhf089aa42008-07-08 19:34:06 +00002889 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002890 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002891 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002892 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002893
2894 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002895 }
2896 break;
2897}
2898
drh98757152008-01-09 23:04:12 +00002899/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002900**
2901** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002902** opcode is encountered. Depending on the ON CONFLICT setting, the
2903** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002904**
drh001bbcb2003-03-19 03:14:00 +00002905** P1 is the index of the database file on which the transaction is
2906** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002907** file used for temporary tables. Indices of 2 or more are used for
2908** attached databases.
drhcabb0812002-09-14 13:47:32 +00002909**
drh80242052004-06-09 00:48:12 +00002910** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002911** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002912** other process can start another write transaction while this transaction is
2913** underway. Starting a write transaction also creates a rollback journal. A
2914** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002915** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2916** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002917**
dane0af83a2009-09-08 19:15:01 +00002918** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2919** true (this flag is set if the Vdbe may modify more than one row and may
2920** throw an ABORT exception), a statement transaction may also be opened.
2921** More specifically, a statement transaction is opened iff the database
2922** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002923** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002924** VDBE to be rolled back after an error without having to roll back the
2925** entire transaction. If no error is encountered, the statement transaction
2926** will automatically commit when the VDBE halts.
2927**
danielk1977ee5741e2004-05-31 10:01:34 +00002928** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002929*/
drh9cbf3422008-01-17 16:22:13 +00002930case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002931 Btree *pBt;
2932
drh653b82a2009-06-22 11:10:47 +00002933 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002934 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh653b82a2009-06-22 11:10:47 +00002935 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002936
danielk197724162fe2004-06-04 06:22:00 +00002937 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002938 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002939 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002940 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002941 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002942 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002943 }
drh9e9f1bd2009-10-13 15:36:51 +00002944 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00002945 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002946 }
dane0af83a2009-09-08 19:15:01 +00002947
2948 if( pOp->p2 && p->usesStmtJournal
2949 && (db->autoCommit==0 || db->activeVdbeCnt>1)
2950 ){
2951 assert( sqlite3BtreeIsInTrans(pBt) );
2952 if( p->iStatement==0 ){
2953 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2954 db->nStatement++;
2955 p->iStatement = db->nSavepoint + db->nStatement;
2956 }
dana311b802011-04-26 19:21:34 +00002957
drh346506f2011-05-25 01:16:42 +00002958 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00002959 if( rc==SQLITE_OK ){
2960 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
2961 }
dan1da40a32009-09-19 17:00:31 +00002962
2963 /* Store the current value of the database handles deferred constraint
2964 ** counter. If the statement transaction needs to be rolled back,
2965 ** the value of this counter needs to be restored too. */
2966 p->nStmtDefCons = db->nDeferredCons;
dane0af83a2009-09-08 19:15:01 +00002967 }
drhb86ccfb2003-01-28 23:13:10 +00002968 }
drh5e00f6c2001-09-13 13:46:56 +00002969 break;
2970}
2971
drhb1fdb2a2008-01-05 04:06:03 +00002972/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002973**
drh9cbf3422008-01-17 16:22:13 +00002974** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002975** P3==1 is the schema version. P3==2 is the database format.
2976** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00002977** the main database file and P1==1 is the database file used to store
2978** temporary tables.
drh4a324312001-12-21 14:30:42 +00002979**
drh50e5dad2001-09-15 00:57:28 +00002980** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002981** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002982** executing this instruction.
2983*/
drh4c583122008-01-04 22:01:03 +00002984case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00002985 int iMeta;
drh856c1032009-06-02 15:21:42 +00002986 int iDb;
2987 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00002988
drh856c1032009-06-02 15:21:42 +00002989 iDb = pOp->p1;
2990 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00002991 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00002992 assert( iDb>=0 && iDb<db->nDb );
2993 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00002994 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00002995
danielk1977602b4662009-07-02 07:47:33 +00002996 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00002997 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00002998 break;
2999}
3000
drh98757152008-01-09 23:04:12 +00003001/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003002**
drh98757152008-01-09 23:04:12 +00003003** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003004** into cookie number P2 of database P1. P2==1 is the schema version.
3005** P2==2 is the database format. P2==3 is the recommended pager cache
3006** size, and so forth. P1==0 is the main database file and P1==1 is the
3007** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003008**
3009** A transaction must be started before executing this opcode.
3010*/
drh9cbf3422008-01-17 16:22:13 +00003011case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003012 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003013 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003014 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003015 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh3f7d4e42004-07-24 14:35:58 +00003016 pDb = &db->aDb[pOp->p1];
3017 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003018 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003019 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003020 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003021 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003022 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3023 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003024 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003025 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003026 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003027 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003028 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003029 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003030 }
drhfd426c62006-01-30 15:34:22 +00003031 if( pOp->p1==1 ){
3032 /* Invalidate all prepared statements whenever the TEMP database
3033 ** schema is changed. Ticket #1644 */
3034 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003035 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003036 }
drh50e5dad2001-09-15 00:57:28 +00003037 break;
3038}
3039
drhc2a75552011-03-18 21:55:46 +00003040/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003041**
drh001bbcb2003-03-19 03:14:00 +00003042** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00003043** schema version) and make sure it is equal to P2 and that the
3044** generation counter on the local schema parse equals P3.
3045**
drh001bbcb2003-03-19 03:14:00 +00003046** P1 is the database number which is 0 for the main database file
3047** and 1 for the file holding temporary tables and some higher number
3048** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00003049**
3050** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00003051** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00003052** and that the current process needs to reread the schema.
3053**
3054** Either a transaction needs to have been started or an OP_Open needs
3055** to be executed (to establish a read lock) before this opcode is
3056** invoked.
3057*/
drh9cbf3422008-01-17 16:22:13 +00003058case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00003059 int iMeta;
drhc2a75552011-03-18 21:55:46 +00003060 int iGen;
drhc275b4e2004-07-19 17:25:24 +00003061 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00003062
drh001bbcb2003-03-19 03:14:00 +00003063 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003064 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh21206082011-04-04 18:22:02 +00003065 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drhc275b4e2004-07-19 17:25:24 +00003066 pBt = db->aDb[pOp->p1].pBt;
3067 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00003068 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00003069 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00003070 }else{
drhfcd71b62011-04-05 22:08:24 +00003071 iGen = iMeta = 0;
drhc275b4e2004-07-19 17:25:24 +00003072 }
drhc2a75552011-03-18 21:55:46 +00003073 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00003074 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00003075 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00003076 /* If the schema-cookie from the database file matches the cookie
3077 ** stored with the in-memory representation of the schema, do
3078 ** not reload the schema from the database file.
3079 **
shane21e7feb2008-05-30 15:59:49 +00003080 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00003081 ** Often, v-tables store their data in other SQLite tables, which
3082 ** are queried from within xNext() and other v-table methods using
3083 ** prepared queries. If such a query is out-of-date, we do not want to
3084 ** discard the database schema, as the user code implementing the
3085 ** v-table would have to be ready for the sqlite3_vtab structure itself
3086 ** to be invalidated whenever sqlite3_step() is called from within
3087 ** a v-table method.
3088 */
3089 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
drh81028a42012-05-15 18:28:27 +00003090 sqlite3ResetOneSchema(db, pOp->p1);
danielk1977896e7922007-04-17 08:32:33 +00003091 }
3092
drh5b6c5452011-02-22 03:34:56 +00003093 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00003094 rc = SQLITE_SCHEMA;
3095 }
3096 break;
3097}
3098
drh98757152008-01-09 23:04:12 +00003099/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003100**
drhecdc7532001-09-23 02:35:53 +00003101** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003102** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003103** P3==0 means the main database, P3==1 means the database used for
3104** temporary tables, and P3>1 means used the corresponding attached
3105** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003106** values need not be contiguous but all P1 values should be small integers.
3107** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003108**
drh98757152008-01-09 23:04:12 +00003109** If P5!=0 then use the content of register P2 as the root page, not
3110** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003111**
drhb19a2bc2001-09-16 00:13:26 +00003112** There will be a read lock on the database whenever there is an
3113** open cursor. If the database was unlocked prior to this instruction
3114** then a read lock is acquired as part of this instruction. A read
3115** lock allows other processes to read the database but prohibits
3116** any other process from modifying the database. The read lock is
3117** released when all cursors are closed. If this instruction attempts
3118** to get a read lock but fails, the script terminates with an
3119** SQLITE_BUSY error code.
3120**
danielk1977d336e222009-02-20 10:58:41 +00003121** The P4 value may be either an integer (P4_INT32) or a pointer to
3122** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3123** structure, then said structure defines the content and collating
3124** sequence of the index being opened. Otherwise, if P4 is an integer
3125** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003126**
drh001bbcb2003-03-19 03:14:00 +00003127** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003128*/
drh98757152008-01-09 23:04:12 +00003129/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00003130**
3131** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003132** page is P2. Or if P5!=0 use the content of register P2 to find the
3133** root page.
drhecdc7532001-09-23 02:35:53 +00003134**
danielk1977d336e222009-02-20 10:58:41 +00003135** The P4 value may be either an integer (P4_INT32) or a pointer to
3136** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3137** structure, then said structure defines the content and collating
3138** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003139** value, it is set to the number of columns in the table, or to the
3140** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003141**
drh001bbcb2003-03-19 03:14:00 +00003142** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003143** in read/write mode. For a given table, there can be one or more read-only
3144** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003145**
drh001bbcb2003-03-19 03:14:00 +00003146** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003147*/
drh9cbf3422008-01-17 16:22:13 +00003148case OP_OpenRead:
3149case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003150 int nField;
3151 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003152 int p2;
3153 int iDb;
drhf57b3392001-10-08 13:22:32 +00003154 int wrFlag;
3155 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003156 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003157 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003158
dan428c2182012-08-06 18:50:11 +00003159 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3160 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
3161
danfa401de2009-10-16 14:55:03 +00003162 if( p->expired ){
3163 rc = SQLITE_ABORT;
3164 break;
3165 }
3166
drh856c1032009-06-02 15:21:42 +00003167 nField = 0;
3168 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003169 p2 = pOp->p2;
3170 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003171 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003172 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003173 pDb = &db->aDb[iDb];
3174 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003175 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003176 if( pOp->opcode==OP_OpenWrite ){
3177 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003178 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003179 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3180 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003181 }
3182 }else{
3183 wrFlag = 0;
3184 }
dan428c2182012-08-06 18:50:11 +00003185 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003186 assert( p2>0 );
3187 assert( p2<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003188 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003189 assert( memIsValid(pIn2) );
3190 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003191 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003192 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003193 /* The p2 value always comes from a prior OP_CreateTable opcode and
3194 ** that opcode will always set the p2 value to 2 or more or else fail.
3195 ** If there were a failure, the prepared statement would have halted
3196 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003197 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003198 rc = SQLITE_CORRUPT_BKPT;
3199 goto abort_due_to_error;
3200 }
drh5edc3122001-09-13 21:53:09 +00003201 }
danielk1977d336e222009-02-20 10:58:41 +00003202 if( pOp->p4type==P4_KEYINFO ){
3203 pKeyInfo = pOp->p4.pKeyInfo;
3204 pKeyInfo->enc = ENC(p->db);
3205 nField = pKeyInfo->nField+1;
3206 }else if( pOp->p4type==P4_INT32 ){
3207 nField = pOp->p4.i;
3208 }
drh653b82a2009-06-22 11:10:47 +00003209 assert( pOp->p1>=0 );
3210 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003211 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003212 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003213 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003214 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3215 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003216 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3217 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003218
dana205a482011-08-27 18:48:57 +00003219 /* Since it performs no memory allocation or IO, the only value that
3220 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3221 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003222
3223 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3224 ** SQLite used to check if the root-page flags were sane at this point
3225 ** and report database corruption if they were not, but this check has
3226 ** since moved into the btree layer. */
3227 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3228 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003229 break;
3230}
3231
drh2a5d9902011-08-26 00:34:45 +00003232/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003233**
drhb9bb7c12006-06-11 23:41:55 +00003234** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003235** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003236** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003237** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003238**
drh25d3adb2010-04-05 15:11:08 +00003239** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003240** The cursor points to a BTree table if P4==0 and to a BTree index
3241** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003242** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003243**
3244** This opcode was once called OpenTemp. But that created
3245** confusion because the term "temp table", might refer either
3246** to a TEMP table at the SQL level, or to a table opened by
3247** this opcode. Then this opcode was call OpenVirtual. But
3248** that created confusion with the whole virtual-table idea.
drh2a5d9902011-08-26 00:34:45 +00003249**
3250** The P5 parameter can be a mask of the BTREE_* flags defined
3251** in btree.h. These flags control aspects of the operation of
3252** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3253** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003254*/
drha21a64d2010-04-06 22:33:55 +00003255/* Opcode: OpenAutoindex P1 P2 * P4 *
3256**
3257** This opcode works the same as OP_OpenEphemeral. It has a
3258** different name to distinguish its use. Tables created using
3259** by this opcode will be used for automatically created transient
3260** indices in joins.
3261*/
3262case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003263case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003264 VdbeCursor *pCx;
drhd4187c72010-08-30 22:15:45 +00003265 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003266 SQLITE_OPEN_READWRITE |
3267 SQLITE_OPEN_CREATE |
3268 SQLITE_OPEN_EXCLUSIVE |
3269 SQLITE_OPEN_DELETEONCLOSE |
3270 SQLITE_OPEN_TRANSIENT_DB;
3271
drh653b82a2009-06-22 11:10:47 +00003272 assert( pOp->p1>=0 );
3273 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003274 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003275 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003276 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3277 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003278 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003279 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003280 }
3281 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003282 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003283 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003284 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003285 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003286 */
danielk19772dca4ac2008-01-03 11:50:29 +00003287 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003288 int pgno;
drh66a51672008-01-03 00:01:23 +00003289 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003290 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003291 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003292 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003293 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003294 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003295 pCx->pKeyInfo = pOp->p4.pKeyInfo;
dan689ab892011-08-12 15:02:00 +00003296 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003297 }
drhf0863fe2005-06-12 21:35:51 +00003298 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003299 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003300 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003301 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003302 }
drh5e00f6c2001-09-13 13:46:56 +00003303 }
drhd4187c72010-08-30 22:15:45 +00003304 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drhf0863fe2005-06-12 21:35:51 +00003305 pCx->isIndex = !pCx->isTable;
dan5134d132011-09-02 10:31:11 +00003306 break;
3307}
3308
drhfc5e5462012-12-03 17:04:40 +00003309/* Opcode: SorterOpen P1 P2 * P4 *
dan5134d132011-09-02 10:31:11 +00003310**
3311** This opcode works like OP_OpenEphemeral except that it opens
3312** a transient index that is specifically designed to sort large
3313** tables using an external merge-sort algorithm.
3314*/
drhca892a72011-09-03 00:17:51 +00003315case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003316 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003317
drhca892a72011-09-03 00:17:51 +00003318#ifndef SQLITE_OMIT_MERGE_SORT
dan5134d132011-09-02 10:31:11 +00003319 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3320 if( pCx==0 ) goto no_mem;
3321 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3322 pCx->pKeyInfo->enc = ENC(p->db);
3323 pCx->isSorter = 1;
3324 rc = sqlite3VdbeSorterInit(db, pCx);
drhca892a72011-09-03 00:17:51 +00003325#else
3326 pOp->opcode = OP_OpenEphemeral;
3327 pc--;
3328#endif
drh5e00f6c2001-09-13 13:46:56 +00003329 break;
3330}
3331
drh980db4b2012-10-30 14:44:14 +00003332/* Opcode: OpenPseudo P1 P2 P3 * P5
drh70ce3f02003-04-15 19:22:22 +00003333**
3334** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003335** row of data. The content of that one row in the content of memory
drh21172c42012-10-30 00:29:07 +00003336** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
3337** MEM_Blob content contained in register P2. When P5==1, then the
3338** row is represented by P3 consecutive registers beginning with P2.
drh70ce3f02003-04-15 19:22:22 +00003339**
drh2d8d7ce2010-02-15 15:17:05 +00003340** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003341** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003342** individual columns using the OP_Column opcode. The OP_Column opcode
3343** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003344**
3345** P3 is the number of fields in the records that will be stored by
3346** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003347*/
drh9cbf3422008-01-17 16:22:13 +00003348case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003349 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003350
drh653b82a2009-06-22 11:10:47 +00003351 assert( pOp->p1>=0 );
3352 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003353 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003354 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003355 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003356 pCx->isTable = 1;
3357 pCx->isIndex = 0;
drh21172c42012-10-30 00:29:07 +00003358 pCx->multiPseudo = pOp->p5;
drh70ce3f02003-04-15 19:22:22 +00003359 break;
3360}
3361
drh98757152008-01-09 23:04:12 +00003362/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003363**
3364** Close a cursor previously opened as P1. If P1 is not
3365** currently open, this instruction is a no-op.
3366*/
drh9cbf3422008-01-17 16:22:13 +00003367case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003368 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3369 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3370 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003371 break;
3372}
3373
drh959403f2008-12-12 17:56:16 +00003374/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003375**
danielk1977b790c6c2008-04-18 10:25:24 +00003376** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003377** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003378** to an SQL index, then P3 is the first in an array of P4 registers
3379** that are used as an unpacked index key.
3380**
3381** Reposition cursor P1 so that it points to the smallest entry that
3382** is greater than or equal to the key value. If there are no records
3383** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003384**
drh959403f2008-12-12 17:56:16 +00003385** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003386*/
drh959403f2008-12-12 17:56:16 +00003387/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003388**
danielk1977b790c6c2008-04-18 10:25:24 +00003389** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003390** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003391** to an SQL index, then P3 is the first in an array of P4 registers
3392** that are used as an unpacked index key.
3393**
3394** Reposition cursor P1 so that it points to the smallest entry that
3395** is greater than the key value. If there are no records greater than
3396** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003397**
drh959403f2008-12-12 17:56:16 +00003398** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003399*/
drh959403f2008-12-12 17:56:16 +00003400/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003401**
danielk1977b790c6c2008-04-18 10:25:24 +00003402** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003403** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003404** to an SQL index, then P3 is the first in an array of P4 registers
3405** that are used as an unpacked index key.
3406**
3407** Reposition cursor P1 so that it points to the largest entry that
3408** is less than the key value. If there are no records less than
3409** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003410**
drh959403f2008-12-12 17:56:16 +00003411** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003412*/
drh959403f2008-12-12 17:56:16 +00003413/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003414**
danielk1977b790c6c2008-04-18 10:25:24 +00003415** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003416** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003417** to an SQL index, then P3 is the first in an array of P4 registers
3418** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003419**
danielk1977b790c6c2008-04-18 10:25:24 +00003420** Reposition cursor P1 so that it points to the largest entry that
3421** is less than or equal to the key value. If there are no records
3422** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003423**
drh959403f2008-12-12 17:56:16 +00003424** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003425*/
drh959403f2008-12-12 17:56:16 +00003426case OP_SeekLt: /* jump, in3 */
3427case OP_SeekLe: /* jump, in3 */
3428case OP_SeekGe: /* jump, in3 */
3429case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003430 int res;
3431 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003432 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003433 UnpackedRecord r;
3434 int nField;
3435 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003436
drh653b82a2009-06-22 11:10:47 +00003437 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003438 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003439 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003440 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003441 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003442 assert( OP_SeekLe == OP_SeekLt+1 );
3443 assert( OP_SeekGe == OP_SeekLt+2 );
3444 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003445 assert( pC->isOrdered );
dana205a482011-08-27 18:48:57 +00003446 if( ALWAYS(pC->pCursor!=0) ){
drh7cf6e4d2004-05-19 14:56:55 +00003447 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003448 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003449 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003450 /* The input value in P3 might be of any type: integer, real, string,
3451 ** blob, or NULL. But it needs to be an integer before we can do
3452 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003453 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003454 applyNumericAffinity(pIn3);
3455 iKey = sqlite3VdbeIntValue(pIn3);
3456 pC->rowidIsValid = 0;
3457
3458 /* If the P3 value could not be converted into an integer without
3459 ** loss of information, then special processing is required... */
3460 if( (pIn3->flags & MEM_Int)==0 ){
3461 if( (pIn3->flags & MEM_Real)==0 ){
3462 /* If the P3 value cannot be converted into any kind of a number,
3463 ** then the seek is not possible, so jump to P2 */
3464 pc = pOp->p2 - 1;
3465 break;
3466 }
3467 /* If we reach this point, then the P3 value must be a floating
3468 ** point number. */
3469 assert( (pIn3->flags & MEM_Real)!=0 );
3470
3471 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003472 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003473 ** integer. */
3474 res = 1;
3475 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003476 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003477 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3478 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3479 }
3480 }else{
drh1f350122009-11-13 20:52:43 +00003481 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003482 rc = sqlite3BtreeLast(pC->pCursor, &res);
3483 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3484 }
3485 }
3486 if( res ){
3487 pc = pOp->p2 - 1;
3488 }
3489 break;
3490 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3491 /* Use the ceiling() function to convert real->int */
3492 if( pIn3->r > (double)iKey ) iKey++;
3493 }else{
3494 /* Use the floor() function to convert real->int */
3495 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3496 if( pIn3->r < (double)iKey ) iKey--;
3497 }
3498 }
drhe63d9992008-08-13 19:11:48 +00003499 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003500 if( rc!=SQLITE_OK ){
3501 goto abort_due_to_error;
3502 }
drh959403f2008-12-12 17:56:16 +00003503 if( res==0 ){
3504 pC->rowidIsValid = 1;
3505 pC->lastRowid = iKey;
3506 }
drh5e00f6c2001-09-13 13:46:56 +00003507 }else{
drh856c1032009-06-02 15:21:42 +00003508 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003509 assert( pOp->p4type==P4_INT32 );
3510 assert( nField>0 );
3511 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003512 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003513
3514 /* The next line of code computes as follows, only faster:
3515 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3516 ** r.flags = UNPACKED_INCRKEY;
3517 ** }else{
3518 ** r.flags = 0;
3519 ** }
3520 */
shaneh5e17e8b2009-12-03 04:40:47 +00003521 r.flags = (u16)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003522 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3523 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3524 assert( oc!=OP_SeekGe || r.flags==0 );
3525 assert( oc!=OP_SeekLt || r.flags==0 );
3526
drha6c2ed92009-11-14 23:22:23 +00003527 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003528#ifdef SQLITE_DEBUG
3529 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3530#endif
drh039fc322009-11-17 18:31:47 +00003531 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003532 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003533 if( rc!=SQLITE_OK ){
3534 goto abort_due_to_error;
3535 }
drhf0863fe2005-06-12 21:35:51 +00003536 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003537 }
drha11846b2004-01-07 18:52:56 +00003538 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003539 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003540#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003541 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003542#endif
drh1f350122009-11-13 20:52:43 +00003543 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003544 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003545 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003546 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003547 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003548 }else{
3549 res = 0;
drh8721ce42001-11-07 14:22:00 +00003550 }
drh7cf6e4d2004-05-19 14:56:55 +00003551 }else{
drh959403f2008-12-12 17:56:16 +00003552 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3553 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003554 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3555 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003556 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003557 }else{
3558 /* res might be negative because the table is empty. Check to
3559 ** see if this is the case.
3560 */
drhf328bc82004-05-10 23:29:49 +00003561 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003562 }
drh1af3fdb2004-07-18 21:33:01 +00003563 }
drh91fd4d42008-01-19 20:11:25 +00003564 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003565 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003566 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003567 }
drhaa736092009-06-22 00:55:30 +00003568 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003569 /* This happens when attempting to open the sqlite3_master table
3570 ** for read access returns SQLITE_EMPTY. In this case always
3571 ** take the jump (since there are no records in the table).
3572 */
3573 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003574 }
drh5e00f6c2001-09-13 13:46:56 +00003575 break;
3576}
3577
drh959403f2008-12-12 17:56:16 +00003578/* Opcode: Seek P1 P2 * * *
3579**
3580** P1 is an open table cursor and P2 is a rowid integer. Arrange
3581** for P1 to move so that it points to the rowid given by P2.
3582**
3583** This is actually a deferred seek. Nothing actually happens until
3584** the cursor is used to read a record. That way, if no reads
3585** occur, no unnecessary I/O happens.
3586*/
3587case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003588 VdbeCursor *pC;
3589
drh653b82a2009-06-22 11:10:47 +00003590 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3591 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003592 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003593 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003594 assert( pC->isTable );
3595 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003596 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003597 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3598 pC->rowidIsValid = 0;
3599 pC->deferredMoveto = 1;
3600 }
3601 break;
3602}
3603
3604
drh8cff69d2009-11-12 19:59:44 +00003605/* Opcode: Found P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003606**
drh8cff69d2009-11-12 19:59:44 +00003607** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3608** P4>0 then register P3 is the first of P4 registers that form an unpacked
3609** record.
3610**
3611** Cursor P1 is on an index btree. If the record identified by P3 and P4
3612** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003613** P1 is left pointing at the matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003614*/
drh8cff69d2009-11-12 19:59:44 +00003615/* Opcode: NotFound P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003616**
drh8cff69d2009-11-12 19:59:44 +00003617** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3618** P4>0 then register P3 is the first of P4 registers that form an unpacked
3619** record.
3620**
3621** Cursor P1 is on an index btree. If the record identified by P3 and P4
3622** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3623** does contain an entry whose prefix matches the P3/P4 record then control
3624** falls through to the next instruction and P1 is left pointing at the
3625** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003626**
drhcb6d50e2008-08-21 19:28:30 +00003627** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003628*/
drh9cbf3422008-01-17 16:22:13 +00003629case OP_NotFound: /* jump, in3 */
3630case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003631 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003632 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003633 int res;
dan03e9cfc2011-09-05 14:20:27 +00003634 char *pFree;
drh856c1032009-06-02 15:21:42 +00003635 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003636 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003637 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3638
dan0ff297e2009-09-25 17:03:14 +00003639#ifdef SQLITE_TEST
3640 sqlite3_found_count++;
3641#endif
3642
drh856c1032009-06-02 15:21:42 +00003643 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003644 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003645 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003646 pC = p->apCsr[pOp->p1];
3647 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003648 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003649 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003650
drhf0863fe2005-06-12 21:35:51 +00003651 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003652 if( pOp->p4.i>0 ){
3653 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003654 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003655 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003656#ifdef SQLITE_DEBUG
3657 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3658#endif
drh8cff69d2009-11-12 19:59:44 +00003659 r.flags = UNPACKED_PREFIX_MATCH;
3660 pIdxKey = &r;
3661 }else{
dan03e9cfc2011-09-05 14:20:27 +00003662 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3663 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3664 );
3665 if( pIdxKey==0 ) goto no_mem;
drh8cff69d2009-11-12 19:59:44 +00003666 assert( pIn3->flags & MEM_Blob );
drhd81a1422010-09-28 07:11:24 +00003667 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
dan03e9cfc2011-09-05 14:20:27 +00003668 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh8cff69d2009-11-12 19:59:44 +00003669 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003670 }
drhe63d9992008-08-13 19:11:48 +00003671 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003672 if( pOp->p4.i==0 ){
dan03e9cfc2011-09-05 14:20:27 +00003673 sqlite3DbFree(db, pFree);
drh8cff69d2009-11-12 19:59:44 +00003674 }
danielk197777519402007-08-30 11:48:31 +00003675 if( rc!=SQLITE_OK ){
3676 break;
3677 }
3678 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003679 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003680 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003681 }
3682 if( pOp->opcode==OP_Found ){
3683 if( alreadyExists ) pc = pOp->p2 - 1;
3684 }else{
3685 if( !alreadyExists ) pc = pOp->p2 - 1;
3686 }
drh5e00f6c2001-09-13 13:46:56 +00003687 break;
3688}
3689
drh98757152008-01-09 23:04:12 +00003690/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003691**
drh8cff69d2009-11-12 19:59:44 +00003692** Cursor P1 is open on an index b-tree - that is to say, a btree which
3693** no data and where the key are records generated by OP_MakeRecord with
3694** the list field being the integer ROWID of the entry that the index
3695** entry refers to.
danielk1977de630352009-05-04 11:42:29 +00003696**
3697** The P3 register contains an integer record number. Call this record
3698** number R. Register P4 is the first in a set of N contiguous registers
3699** that make up an unpacked index key that can be used with cursor P1.
3700** The value of N can be inferred from the cursor. N includes the rowid
3701** value appended to the end of the index record. This rowid value may
3702** or may not be the same as R.
3703**
3704** If any of the N registers beginning with register P4 contains a NULL
3705** value, jump immediately to P2.
3706**
3707** Otherwise, this instruction checks if cursor P1 contains an entry
3708** where the first (N-1) fields match but the rowid value at the end
3709** of the index entry is not R. If there is no such entry, control jumps
3710** to instruction P2. Otherwise, the rowid of the conflicting index
3711** entry is copied to register P3 and control falls through to the next
3712** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003713**
drh9cbf3422008-01-17 16:22:13 +00003714** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003715*/
drh9cbf3422008-01-17 16:22:13 +00003716case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003717 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003718 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003719 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003720 u16 nField;
drha6c2ed92009-11-14 23:22:23 +00003721 Mem *aMx;
drh856c1032009-06-02 15:21:42 +00003722 UnpackedRecord r; /* B-Tree index search key */
3723 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003724
drh3c657212009-11-17 23:59:58 +00003725 pIn3 = &aMem[pOp->p3];
drha6c2ed92009-11-14 23:22:23 +00003726 aMx = &aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003727 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003728 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003729 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003730 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3731
3732 /* Find the index cursor. */
3733 pCx = p->apCsr[pOp->p1];
3734 assert( pCx->deferredMoveto==0 );
3735 pCx->seekResult = 0;
3736 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003737 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003738
3739 /* If any of the values are NULL, take the jump. */
3740 nField = pCx->pKeyInfo->nField;
3741 for(ii=0; ii<nField; ii++){
drha6c2ed92009-11-14 23:22:23 +00003742 if( aMx[ii].flags & MEM_Null ){
danielk1977de630352009-05-04 11:42:29 +00003743 pc = pOp->p2 - 1;
3744 pCrsr = 0;
3745 break;
3746 }
3747 }
drha6c2ed92009-11-14 23:22:23 +00003748 assert( (aMx[nField].flags & MEM_Null)==0 );
danielk1977de630352009-05-04 11:42:29 +00003749
drhf328bc82004-05-10 23:29:49 +00003750 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003751 /* Populate the index search key. */
3752 r.pKeyInfo = pCx->pKeyInfo;
3753 r.nField = nField + 1;
3754 r.flags = UNPACKED_PREFIX_SEARCH;
drha6c2ed92009-11-14 23:22:23 +00003755 r.aMem = aMx;
drh2b4ded92010-09-27 21:09:31 +00003756#ifdef SQLITE_DEBUG
3757 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3758#endif
danielk1977452c9892004-05-13 05:16:15 +00003759
danielk1977de630352009-05-04 11:42:29 +00003760 /* Extract the value of R from register P3. */
3761 sqlite3VdbeMemIntegerify(pIn3);
3762 R = pIn3->u.i;
3763
3764 /* Search the B-Tree index. If no conflicting record is found, jump
3765 ** to P2. Otherwise, copy the rowid of the conflicting record to
3766 ** register P3 and fall through to the next instruction. */
3767 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3768 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003769 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003770 }else{
3771 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003772 }
drh9cfcf5d2002-01-29 18:41:24 +00003773 }
3774 break;
3775}
3776
drh9cbf3422008-01-17 16:22:13 +00003777/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003778**
drhef8662b2011-06-20 21:47:58 +00003779** Use the content of register P3 as an integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003780** with that key does not exist in table of P1, then jump to P2.
drh710c4842010-08-30 01:17:20 +00003781** If the record does exist, then fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003782** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003783**
3784** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003785** operation assumes the key is an integer and that P1 is a table whereas
3786** NotFound assumes key is a blob constructed from MakeRecord and
3787** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003788**
drhcb6d50e2008-08-21 19:28:30 +00003789** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003790*/
drh9cbf3422008-01-17 16:22:13 +00003791case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003792 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003793 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003794 int res;
3795 u64 iKey;
3796
drh3c657212009-11-17 23:59:58 +00003797 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003798 assert( pIn3->flags & MEM_Int );
3799 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3800 pC = p->apCsr[pOp->p1];
3801 assert( pC!=0 );
3802 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003803 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003804 pCrsr = pC->pCursor;
dana205a482011-08-27 18:48:57 +00003805 if( ALWAYS(pCrsr!=0) ){
drh856c1032009-06-02 15:21:42 +00003806 res = 0;
drhaa736092009-06-22 00:55:30 +00003807 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003808 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003809 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003810 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003811 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003812 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003813 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003814 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003815 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003816 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003817 }
danielk1977de630352009-05-04 11:42:29 +00003818 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003819 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003820 /* This happens when an attempt to open a read cursor on the
3821 ** sqlite_master table returns SQLITE_EMPTY.
3822 */
danielk1977f7b9d662008-06-23 18:49:43 +00003823 pc = pOp->p2 - 1;
3824 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003825 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003826 }
drh6b125452002-01-28 15:53:03 +00003827 break;
3828}
3829
drh4c583122008-01-04 22:01:03 +00003830/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003831**
drh4c583122008-01-04 22:01:03 +00003832** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003833** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003834** The sequence number on the cursor is incremented after this
3835** instruction.
drh4db38a72005-09-01 12:16:28 +00003836*/
drh4c583122008-01-04 22:01:03 +00003837case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003838 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3839 assert( p->apCsr[pOp->p1]!=0 );
3840 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003841 break;
3842}
3843
3844
drh98757152008-01-09 23:04:12 +00003845/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003846**
drhf0863fe2005-06-12 21:35:51 +00003847** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003848** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003849** table that cursor P1 points to. The new record number is written
3850** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003851**
dan76d462e2009-08-30 11:42:51 +00003852** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3853** the largest previously generated record number. No new record numbers are
3854** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003855** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003856** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003857** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003858*/
drh4c583122008-01-04 22:01:03 +00003859case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003860 i64 v; /* The new rowid */
3861 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3862 int res; /* Result of an sqlite3BtreeLast() */
3863 int cnt; /* Counter to limit the number of searches */
3864 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003865 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003866
drh856c1032009-06-02 15:21:42 +00003867 v = 0;
3868 res = 0;
drhaa736092009-06-22 00:55:30 +00003869 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3870 pC = p->apCsr[pOp->p1];
3871 assert( pC!=0 );
3872 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003873 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003874 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003875 /* The next rowid or record number (different terms for the same
3876 ** thing) is obtained in a two-step algorithm.
3877 **
3878 ** First we attempt to find the largest existing rowid and add one
3879 ** to that. But if the largest existing rowid is already the maximum
3880 ** positive integer, we have to fall through to the second
3881 ** probabilistic algorithm
3882 **
3883 ** The second algorithm is to select a rowid at random and see if
3884 ** it already exists in the table. If it does not exist, we have
3885 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003886 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003887 */
drhaa736092009-06-22 00:55:30 +00003888 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003889
drh75f86a42005-02-17 00:03:06 +00003890#ifdef SQLITE_32BIT_ROWID
3891# define MAX_ROWID 0x7fffffff
3892#else
drhfe2093d2005-01-20 22:48:47 +00003893 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3894 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3895 ** to provide the constant while making all compilers happy.
3896 */
danielk197764202cf2008-11-17 15:31:47 +00003897# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003898#endif
drhfe2093d2005-01-20 22:48:47 +00003899
drh5cf8e8c2002-02-19 22:42:05 +00003900 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003901 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3902 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003903 rc = sqlite3BtreeLast(pC->pCursor, &res);
3904 if( rc!=SQLITE_OK ){
3905 goto abort_due_to_error;
3906 }
drh32fbe342002-10-19 20:16:37 +00003907 if( res ){
drhc79c7612010-01-01 18:57:48 +00003908 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003909 }else{
drhea8ffdf2009-07-22 00:35:23 +00003910 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003911 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3912 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drha40eb7c2012-02-24 00:02:28 +00003913 if( v>=MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003914 pC->useRandomRowid = 1;
3915 }else{
drhc79c7612010-01-01 18:57:48 +00003916 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003917 }
drh5cf8e8c2002-02-19 22:42:05 +00003918 }
drh3fc190c2001-09-14 03:24:23 +00003919 }
drh205f48e2004-11-05 00:43:11 +00003920
3921#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003922 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003923 /* Assert that P3 is a valid memory cell. */
3924 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003925 if( p->pFrame ){
3926 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003927 /* Assert that P3 is a valid memory cell. */
3928 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003929 pMem = &pFrame->aMem[pOp->p3];
3930 }else{
shaneabc6b892009-09-10 19:09:03 +00003931 /* Assert that P3 is a valid memory cell. */
3932 assert( pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003933 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003934 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003935 }
drh2b4ded92010-09-27 21:09:31 +00003936 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003937
3938 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003939 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003940 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003941 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003942 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003943 goto abort_due_to_error;
3944 }
drh3c024d62007-03-30 11:23:45 +00003945 if( v<pMem->u.i+1 ){
3946 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003947 }
drh3c024d62007-03-30 11:23:45 +00003948 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003949 }
3950#endif
3951
drh7f751222009-03-17 22:33:00 +00003952 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003953 }
3954 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003955 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003956 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003957 ** engine starts picking positive candidate ROWIDs at random until
3958 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003959 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3960 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003961 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00003962 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003963 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3964 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003965 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003966 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3967 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003968 && (res==0)
3969 && (++cnt<100)){
3970 /* collision - try another random rowid */
3971 sqlite3_randomness(sizeof(v), &v);
3972 if( cnt<5 ){
3973 /* try "small" random rowids for the initial attempts */
3974 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00003975 }else{
shanehc4d340a2010-09-01 02:37:56 +00003976 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00003977 }
shanehc4d340a2010-09-01 02:37:56 +00003978 v++; /* ensure non-zero */
3979 }
drhaa736092009-06-22 00:55:30 +00003980 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00003981 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00003982 goto abort_due_to_error;
3983 }
drh748a52c2010-09-01 11:50:08 +00003984 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00003985 }
drhf0863fe2005-06-12 21:35:51 +00003986 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003987 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003988 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003989 }
drh4c583122008-01-04 22:01:03 +00003990 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00003991 break;
3992}
3993
danielk19771f4aa332008-01-03 09:51:55 +00003994/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003995**
jplyon5a564222003-06-02 06:15:58 +00003996** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003997** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00003998** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00003999** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004000** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004001**
danielk19771f4aa332008-01-03 09:51:55 +00004002** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4003** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004004** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004005** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004006**
drh3e9ca092009-09-08 01:14:48 +00004007** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4008** the last seek operation (OP_NotExists) was a success, then this
4009** operation will not attempt to find the appropriate row before doing
4010** the insert but will instead overwrite the row that the cursor is
4011** currently pointing to. Presumably, the prior OP_NotExists opcode
4012** has already positioned the cursor correctly. This is an optimization
4013** that boosts performance by avoiding redundant seeks.
4014**
4015** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4016** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4017** is part of an INSERT operation. The difference is only important to
4018** the update hook.
4019**
drh66a51672008-01-03 00:01:23 +00004020** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004021** may be NULL. If it is not NULL, then the update-hook
4022** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4023**
drh93aed5a2008-01-16 17:46:38 +00004024** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4025** allocated, then ownership of P2 is transferred to the pseudo-cursor
4026** and register P2 becomes ephemeral. If the cursor is changed, the
4027** value of register P2 will then change. Make sure this does not
4028** cause any problems.)
4029**
drhf0863fe2005-06-12 21:35:51 +00004030** This instruction only works on tables. The equivalent instruction
4031** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004032*/
drhe05c9292009-10-29 13:48:10 +00004033/* Opcode: InsertInt P1 P2 P3 P4 P5
4034**
4035** This works exactly like OP_Insert except that the key is the
4036** integer value P3, not the value of the integer stored in register P3.
4037*/
4038case OP_Insert:
4039case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004040 Mem *pData; /* MEM cell holding data for the record to be inserted */
4041 Mem *pKey; /* MEM cell holding key for the record */
4042 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4043 VdbeCursor *pC; /* Cursor to table into which insert is written */
4044 int nZero; /* Number of zero-bytes to append */
4045 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4046 const char *zDb; /* database name - used by the update hook */
4047 const char *zTbl; /* Table name - used by the opdate hook */
4048 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004049
drha6c2ed92009-11-14 23:22:23 +00004050 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004051 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004052 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004053 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004054 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004055 assert( pC->pCursor!=0 );
4056 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004057 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004058 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004059
drhe05c9292009-10-29 13:48:10 +00004060 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004061 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004062 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004063 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004064 REGISTER_TRACE(pOp->p3, pKey);
4065 iKey = pKey->u.i;
4066 }else{
4067 assert( pOp->opcode==OP_InsertInt );
4068 iKey = pOp->p3;
4069 }
4070
drha05a7222008-01-19 03:35:58 +00004071 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004072 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004073 if( pData->flags & MEM_Null ){
4074 pData->z = 0;
4075 pData->n = 0;
4076 }else{
4077 assert( pData->flags & (MEM_Blob|MEM_Str) );
4078 }
drh3e9ca092009-09-08 01:14:48 +00004079 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4080 if( pData->flags & MEM_Zero ){
4081 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004082 }else{
drh3e9ca092009-09-08 01:14:48 +00004083 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004084 }
drh3e9ca092009-09-08 01:14:48 +00004085 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
4086 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4087 pData->z, pData->n, nZero,
4088 pOp->p5 & OPFLAG_APPEND, seekResult
4089 );
drha05a7222008-01-19 03:35:58 +00004090 pC->rowidIsValid = 0;
4091 pC->deferredMoveto = 0;
4092 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004093
drha05a7222008-01-19 03:35:58 +00004094 /* Invoke the update-hook if required. */
4095 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004096 zDb = db->aDb[pC->iDb].zName;
4097 zTbl = pOp->p4.z;
4098 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004099 assert( pC->isTable );
4100 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4101 assert( pC->iDb>=0 );
4102 }
drh5e00f6c2001-09-13 13:46:56 +00004103 break;
4104}
4105
drh98757152008-01-09 23:04:12 +00004106/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004107**
drh5edc3122001-09-13 21:53:09 +00004108** Delete the record at which the P1 cursor is currently pointing.
4109**
4110** The cursor will be left pointing at either the next or the previous
4111** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004112** the next Next instruction will be a no-op. Hence it is OK to delete
4113** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004114**
rdcb0c374f2004-02-20 22:53:38 +00004115** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004116** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004117**
drh91fd4d42008-01-19 20:11:25 +00004118** P1 must not be pseudo-table. It has to be a real table with
4119** multiple rows.
4120**
4121** If P4 is not NULL, then it is the name of the table that P1 is
4122** pointing to. The update hook will be invoked, if it exists.
4123** If P4 is not NULL then the P1 cursor must have been positioned
4124** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004125*/
drh9cbf3422008-01-17 16:22:13 +00004126case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004127 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004128 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004129
drh856c1032009-06-02 15:21:42 +00004130 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00004131 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4132 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004133 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004134 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00004135
drh91fd4d42008-01-19 20:11:25 +00004136 /* If the update-hook will be invoked, set iKey to the rowid of the
4137 ** row being deleted.
4138 */
4139 if( db->xUpdateCallback && pOp->p4.z ){
4140 assert( pC->isTable );
4141 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
4142 iKey = pC->lastRowid;
4143 }
danielk197794eb6a12005-12-15 15:22:08 +00004144
drh9a65f2c2009-06-22 19:05:40 +00004145 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4146 ** OP_Column on the same table without any intervening operations that
4147 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4148 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4149 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4150 ** to guard against future changes to the code generator.
4151 **/
4152 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004153 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004154 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4155
drh7f751222009-03-17 22:33:00 +00004156 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00004157 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004158 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004159
drh91fd4d42008-01-19 20:11:25 +00004160 /* Invoke the update-hook if required. */
4161 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
4162 const char *zDb = db->aDb[pC->iDb].zName;
4163 const char *zTbl = pOp->p4.z;
4164 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4165 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004166 }
danielk1977b28af712004-06-21 06:50:26 +00004167 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004168 break;
4169}
drhb7f1d9a2009-09-08 02:27:58 +00004170/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004171**
drhb7f1d9a2009-09-08 02:27:58 +00004172** The value of the change counter is copied to the database handle
4173** change counter (returned by subsequent calls to sqlite3_changes()).
4174** Then the VMs internal change counter resets to 0.
4175** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004176*/
drh9cbf3422008-01-17 16:22:13 +00004177case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004178 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004179 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004180 break;
4181}
4182
dan5134d132011-09-02 10:31:11 +00004183/* Opcode: SorterCompare P1 P2 P3
4184**
4185** P1 is a sorter cursor. This instruction compares the record blob in
4186** register P3 with the entry that the sorter cursor currently points to.
4187** If, excluding the rowid fields at the end, the two records are a match,
4188** fall through to the next instruction. Otherwise, jump to instruction P2.
4189*/
4190case OP_SorterCompare: {
4191 VdbeCursor *pC;
4192 int res;
4193
4194 pC = p->apCsr[pOp->p1];
4195 assert( isSorter(pC) );
4196 pIn3 = &aMem[pOp->p3];
4197 rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
4198 if( res ){
4199 pc = pOp->p2-1;
4200 }
4201 break;
4202};
4203
4204/* Opcode: SorterData P1 P2 * * *
4205**
4206** Write into register P2 the current sorter data for sorter cursor P1.
4207*/
4208case OP_SorterData: {
4209 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004210
drhca892a72011-09-03 00:17:51 +00004211#ifndef SQLITE_OMIT_MERGE_SORT
dan5134d132011-09-02 10:31:11 +00004212 pOut = &aMem[pOp->p2];
4213 pC = p->apCsr[pOp->p1];
4214 assert( pC->isSorter );
4215 rc = sqlite3VdbeSorterRowkey(pC, pOut);
drhca892a72011-09-03 00:17:51 +00004216#else
4217 pOp->opcode = OP_RowKey;
4218 pc--;
4219#endif
dan5134d132011-09-02 10:31:11 +00004220 break;
4221}
4222
drh98757152008-01-09 23:04:12 +00004223/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00004224**
drh98757152008-01-09 23:04:12 +00004225** Write into register P2 the complete row data for cursor P1.
4226** There is no interpretation of the data.
4227** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004228** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004229**
drhde4fcfd2008-01-19 23:50:26 +00004230** If the P1 cursor must be pointing to a valid row (not a NULL row)
4231** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004232*/
drh98757152008-01-09 23:04:12 +00004233/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00004234**
drh98757152008-01-09 23:04:12 +00004235** Write into register P2 the complete row key for cursor P1.
4236** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004237** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004238** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004239**
drhde4fcfd2008-01-19 23:50:26 +00004240** If the P1 cursor must be pointing to a valid row (not a NULL row)
4241** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004242*/
danielk1977a7a8e142008-02-13 18:25:27 +00004243case OP_RowKey:
4244case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004245 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004246 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004247 u32 n;
drh856c1032009-06-02 15:21:42 +00004248 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004249
drha6c2ed92009-11-14 23:22:23 +00004250 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004251 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004252
drhf0863fe2005-06-12 21:35:51 +00004253 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004254 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4255 pC = p->apCsr[pOp->p1];
dan5134d132011-09-02 10:31:11 +00004256 assert( pC->isSorter==0 );
drhc6aff302011-09-01 15:32:47 +00004257 assert( pC->isTable || pOp->opcode!=OP_RowData );
drhf0863fe2005-06-12 21:35:51 +00004258 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004259 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004260 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004261 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004262 assert( pC->pCursor!=0 );
4263 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004264 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004265
4266 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4267 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4268 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4269 ** a no-op and can never fail. But we leave it in place as a safety.
4270 */
4271 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004272 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004273 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4274
drhde4fcfd2008-01-19 23:50:26 +00004275 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00004276 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004277 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004278 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004279 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004280 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004281 }
drhbfb19dc2009-06-05 16:46:53 +00004282 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004283 }else{
drhb07028f2011-10-14 21:49:18 +00004284 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004285 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004286 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004287 goto too_big;
4288 }
drhde4fcfd2008-01-19 23:50:26 +00004289 }
danielk1977a7a8e142008-02-13 18:25:27 +00004290 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4291 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004292 }
danielk1977a7a8e142008-02-13 18:25:27 +00004293 pOut->n = n;
4294 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00004295 if( pC->isIndex ){
4296 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4297 }else{
4298 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004299 }
danielk197796cb76f2008-01-04 13:24:28 +00004300 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004301 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00004302 break;
4303}
4304
drh2133d822008-01-03 18:44:59 +00004305/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004306**
drh2133d822008-01-03 18:44:59 +00004307** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004308** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004309**
4310** P1 can be either an ordinary table or a virtual table. There used to
4311** be a separate OP_VRowid opcode for use with virtual tables, but this
4312** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004313*/
drh4c583122008-01-04 22:01:03 +00004314case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004315 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004316 i64 v;
drh856c1032009-06-02 15:21:42 +00004317 sqlite3_vtab *pVtab;
4318 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004319
drh653b82a2009-06-22 11:10:47 +00004320 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4321 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004322 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004323 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004324 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004325 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004326 break;
4327 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004328 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004329#ifndef SQLITE_OMIT_VIRTUALTABLE
4330 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004331 pVtab = pC->pVtabCursor->pVtab;
4332 pModule = pVtab->pModule;
4333 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004334 rc = pModule->xRowid(pC->pVtabCursor, &v);
drhb9755982010-07-24 16:34:37 +00004335 importVtabErrMsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004336#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004337 }else{
drh6be240e2009-07-14 02:33:02 +00004338 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004339 rc = sqlite3VdbeCursorMoveto(pC);
4340 if( rc ) goto abort_due_to_error;
4341 if( pC->rowidIsValid ){
4342 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004343 }else{
drhc27ae612009-07-14 18:35:44 +00004344 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4345 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004346 }
drh5e00f6c2001-09-13 13:46:56 +00004347 }
drh4c583122008-01-04 22:01:03 +00004348 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004349 break;
4350}
4351
drh9cbf3422008-01-17 16:22:13 +00004352/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004353**
4354** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004355** that occur while the cursor is on the null row will always
4356** write a NULL.
drh17f71932002-02-21 12:01:27 +00004357*/
drh9cbf3422008-01-17 16:22:13 +00004358case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004359 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004360
drh653b82a2009-06-22 11:10:47 +00004361 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4362 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004363 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004364 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004365 pC->rowidIsValid = 0;
dana205a482011-08-27 18:48:57 +00004366 assert( pC->pCursor || pC->pVtabCursor );
danielk1977be51a652008-10-08 17:58:48 +00004367 if( pC->pCursor ){
4368 sqlite3BtreeClearCursor(pC->pCursor);
4369 }
drh17f71932002-02-21 12:01:27 +00004370 break;
4371}
4372
drh9cbf3422008-01-17 16:22:13 +00004373/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004374**
drhf0863fe2005-06-12 21:35:51 +00004375** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004376** will refer to the last entry in the database table or index.
4377** If the table or index is empty and P2>0, then jump immediately to P2.
4378** If P2 is 0 or if the table or index is not empty, fall through
4379** to the following instruction.
4380*/
drh9cbf3422008-01-17 16:22:13 +00004381case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004382 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004383 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004384 int res;
drh9562b552002-02-19 15:00:07 +00004385
drh653b82a2009-06-22 11:10:47 +00004386 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4387 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004388 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004389 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004390 res = 0;
4391 if( ALWAYS(pCrsr!=0) ){
drh9a65f2c2009-06-22 19:05:40 +00004392 rc = sqlite3BtreeLast(pCrsr, &res);
4393 }
drh9c1905f2008-12-10 22:32:56 +00004394 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004395 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004396 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004397 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004398 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004399 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004400 }
4401 break;
4402}
4403
drh0342b1f2005-09-01 03:07:44 +00004404
drh9cbf3422008-01-17 16:22:13 +00004405/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004406**
4407** This opcode does exactly the same thing as OP_Rewind except that
4408** it increments an undocumented global variable used for testing.
4409**
4410** Sorting is accomplished by writing records into a sorting index,
4411** then rewinding that index and playing it back from beginning to
4412** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4413** rewinding so that the global variable will be incremented and
4414** regression tests can determine whether or not the optimizer is
4415** correctly optimizing out sorts.
4416*/
drhc6aff302011-09-01 15:32:47 +00004417case OP_SorterSort: /* jump */
drhca892a72011-09-03 00:17:51 +00004418#ifdef SQLITE_OMIT_MERGE_SORT
4419 pOp->opcode = OP_Sort;
4420#endif
drh9cbf3422008-01-17 16:22:13 +00004421case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004422#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004423 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004424 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004425#endif
drhd1d38482008-10-07 23:46:38 +00004426 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
drh0342b1f2005-09-01 03:07:44 +00004427 /* Fall through into OP_Rewind */
4428}
drh9cbf3422008-01-17 16:22:13 +00004429/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004430**
drhf0863fe2005-06-12 21:35:51 +00004431** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004432** will refer to the first entry in the database table or index.
4433** If the table or index is empty and P2>0, then jump immediately to P2.
4434** If P2 is 0 or if the table or index is not empty, fall through
4435** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004436*/
drh9cbf3422008-01-17 16:22:13 +00004437case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004438 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004439 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004440 int res;
drh5e00f6c2001-09-13 13:46:56 +00004441
drh653b82a2009-06-22 11:10:47 +00004442 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4443 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004444 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004445 assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004446 res = 1;
dan689ab892011-08-12 15:02:00 +00004447 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004448 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004449 }else{
4450 pCrsr = pC->pCursor;
4451 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004452 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004453 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004454 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004455 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004456 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004457 }
drh9c1905f2008-12-10 22:32:56 +00004458 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004459 assert( pOp->p2>0 && pOp->p2<p->nOp );
4460 if( res ){
drhf4dada72004-05-11 09:57:35 +00004461 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004462 }
4463 break;
4464}
4465
dana205a482011-08-27 18:48:57 +00004466/* Opcode: Next P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004467**
4468** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004469** table or index. If there are no more key/value pairs then fall through
4470** to the following instruction. But if the cursor advance was successful,
4471** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004472**
drh60a713c2008-01-21 16:22:45 +00004473** The P1 cursor must be for a real table, not a pseudo-table.
4474**
dana205a482011-08-27 18:48:57 +00004475** P4 is always of type P4_ADVANCE. The function pointer points to
4476** sqlite3BtreeNext().
4477**
drhafc266a2010-03-31 17:47:44 +00004478** If P5 is positive and the jump is taken, then event counter
4479** number P5-1 in the prepared statement is incremented.
4480**
drhc045ec52002-12-04 20:01:06 +00004481** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004482*/
drhafc266a2010-03-31 17:47:44 +00004483/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004484**
4485** Back up cursor P1 so that it points to the previous key/data pair in its
4486** table or index. If there is no previous key/value pairs then fall through
4487** to the following instruction. But if the cursor backup was successful,
4488** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004489**
4490** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004491**
dana205a482011-08-27 18:48:57 +00004492** P4 is always of type P4_ADVANCE. The function pointer points to
4493** sqlite3BtreePrevious().
4494**
drhafc266a2010-03-31 17:47:44 +00004495** If P5 is positive and the jump is taken, then event counter
4496** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004497*/
drhc6aff302011-09-01 15:32:47 +00004498case OP_SorterNext: /* jump */
drhca892a72011-09-03 00:17:51 +00004499#ifdef SQLITE_OMIT_MERGE_SORT
4500 pOp->opcode = OP_Next;
4501#endif
drh9cbf3422008-01-17 16:22:13 +00004502case OP_Prev: /* jump */
4503case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004504 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004505 int res;
drh8721ce42001-11-07 14:22:00 +00004506
drhcaec2f12003-01-07 02:47:47 +00004507 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00004508 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhafc266a2010-03-31 17:47:44 +00004509 assert( pOp->p5<=ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004510 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004511 if( pC==0 ){
4512 break; /* See ticket #2273 */
4513 }
drhc6aff302011-09-01 15:32:47 +00004514 assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
dan689ab892011-08-12 15:02:00 +00004515 if( isSorter(pC) ){
dan5134d132011-09-02 10:31:11 +00004516 assert( pOp->opcode==OP_SorterNext );
dana20fde62011-07-12 14:28:05 +00004517 rc = sqlite3VdbeSorterNext(db, pC, &res);
4518 }else{
dana20fde62011-07-12 14:28:05 +00004519 res = 1;
4520 assert( pC->deferredMoveto==0 );
dana205a482011-08-27 18:48:57 +00004521 assert( pC->pCursor );
4522 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4523 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4524 rc = pOp->p4.xAdvance(pC->pCursor, &res);
drh9a65f2c2009-06-22 19:05:40 +00004525 }
drh9c1905f2008-12-10 22:32:56 +00004526 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004527 pC->cacheStatus = CACHE_STALE;
4528 if( res==0 ){
4529 pc = pOp->p2 - 1;
drhd1d38482008-10-07 23:46:38 +00004530 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
drh0f7eb612006-08-08 13:51:43 +00004531#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004532 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004533#endif
drh8721ce42001-11-07 14:22:00 +00004534 }
drhf0863fe2005-06-12 21:35:51 +00004535 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00004536 break;
4537}
4538
danielk1977de630352009-05-04 11:42:29 +00004539/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004540**
drhef8662b2011-06-20 21:47:58 +00004541** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004542** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004543** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004544**
drhaa9b8962008-01-08 02:57:55 +00004545** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004546** insert is likely to be an append.
4547**
drhf0863fe2005-06-12 21:35:51 +00004548** This instruction only works for indices. The equivalent instruction
4549** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004550*/
drhca892a72011-09-03 00:17:51 +00004551case OP_SorterInsert: /* in2 */
4552#ifdef SQLITE_OMIT_MERGE_SORT
4553 pOp->opcode = OP_IdxInsert;
4554#endif
drh9cbf3422008-01-17 16:22:13 +00004555case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004556 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004557 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004558 int nKey;
4559 const char *zKey;
4560
drh653b82a2009-06-22 11:10:47 +00004561 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4562 pC = p->apCsr[pOp->p1];
4563 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004564 assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004565 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004566 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004567 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004568 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004569 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004570 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004571 if( rc==SQLITE_OK ){
dan5134d132011-09-02 10:31:11 +00004572 if( isSorter(pC) ){
4573 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4574 }else{
4575 nKey = pIn2->n;
4576 zKey = pIn2->z;
dan1e74e602011-08-06 12:01:58 +00004577 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4578 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
dan5134d132011-09-02 10:31:11 +00004579 );
dan1e74e602011-08-06 12:01:58 +00004580 assert( pC->deferredMoveto==0 );
dan5134d132011-09-02 10:31:11 +00004581 pC->cacheStatus = CACHE_STALE;
dan1e74e602011-08-06 12:01:58 +00004582 }
danielk1977d908f5a2007-05-11 07:08:28 +00004583 }
drh5e00f6c2001-09-13 13:46:56 +00004584 }
drh5e00f6c2001-09-13 13:46:56 +00004585 break;
4586}
4587
drhd1d38482008-10-07 23:46:38 +00004588/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004589**
drhe14006d2008-03-25 17:23:32 +00004590** The content of P3 registers starting at register P2 form
4591** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004592** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004593*/
drhe14006d2008-03-25 17:23:32 +00004594case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004595 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004596 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004597 int res;
4598 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004599
drhe14006d2008-03-25 17:23:32 +00004600 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004601 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004602 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4603 pC = p->apCsr[pOp->p1];
4604 assert( pC!=0 );
4605 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004606 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004607 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004608 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004609 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004610 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004611#ifdef SQLITE_DEBUG
4612 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4613#endif
drhe63d9992008-08-13 19:11:48 +00004614 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004615 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004616 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004617 }
drh9188b382004-05-14 21:12:22 +00004618 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004619 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004620 }
drh5e00f6c2001-09-13 13:46:56 +00004621 break;
4622}
4623
drh2133d822008-01-03 18:44:59 +00004624/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004625**
drh2133d822008-01-03 18:44:59 +00004626** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004627** the end of the index key pointed to by cursor P1. This integer should be
4628** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004629**
drh9437bd22009-02-01 00:29:56 +00004630** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004631*/
drh4c583122008-01-04 22:01:03 +00004632case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004633 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004634 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004635 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004636
drh653b82a2009-06-22 11:10:47 +00004637 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4638 pC = p->apCsr[pOp->p1];
4639 assert( pC!=0 );
4640 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004641 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004642 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004643 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004644 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004645 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004646 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004647 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004648 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004649 if( rc!=SQLITE_OK ){
4650 goto abort_due_to_error;
4651 }
drh4c583122008-01-04 22:01:03 +00004652 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004653 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004654 }
drh8721ce42001-11-07 14:22:00 +00004655 }
4656 break;
4657}
4658
danielk197761dd5832008-04-18 11:31:12 +00004659/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004660**
danielk197761dd5832008-04-18 11:31:12 +00004661** The P4 register values beginning with P3 form an unpacked index
4662** key that omits the ROWID. Compare this key value against the index
4663** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004664**
danielk197761dd5832008-04-18 11:31:12 +00004665** If the P1 index entry is greater than or equal to the key value
4666** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004667**
danielk197761dd5832008-04-18 11:31:12 +00004668** If P5 is non-zero then the key value is increased by an epsilon
4669** prior to the comparison. This make the opcode work like IdxGT except
4670** that if the key from register P3 is a prefix of the key in the cursor,
4671** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004672*/
drh3bb9b932010-08-06 02:10:00 +00004673/* Opcode: IdxLT P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004674**
danielk197761dd5832008-04-18 11:31:12 +00004675** The P4 register values beginning with P3 form an unpacked index
4676** key that omits the ROWID. Compare this key value against the index
4677** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004678**
danielk197761dd5832008-04-18 11:31:12 +00004679** If the P1 index entry is less than the key value then jump to P2.
4680** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004681**
danielk197761dd5832008-04-18 11:31:12 +00004682** If P5 is non-zero then the key value is increased by an epsilon prior
4683** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004684*/
drh93952eb2009-11-13 19:43:43 +00004685case OP_IdxLT: /* jump */
4686case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004687 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004688 int res;
4689 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004690
drh653b82a2009-06-22 11:10:47 +00004691 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4692 pC = p->apCsr[pOp->p1];
4693 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004694 assert( pC->isOrdered );
drh9a65f2c2009-06-22 19:05:40 +00004695 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004696 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004697 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004698 assert( pOp->p4type==P4_INT32 );
4699 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004700 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004701 if( pOp->p5 ){
dan0c733f62011-11-16 15:27:09 +00004702 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004703 }else{
dan0c733f62011-11-16 15:27:09 +00004704 r.flags = UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004705 }
drha6c2ed92009-11-14 23:22:23 +00004706 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004707#ifdef SQLITE_DEBUG
4708 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4709#endif
drhe63d9992008-08-13 19:11:48 +00004710 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004711 if( pOp->opcode==OP_IdxLT ){
4712 res = -res;
drha05a7222008-01-19 03:35:58 +00004713 }else{
4714 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004715 res++;
4716 }
4717 if( res>0 ){
4718 pc = pOp->p2 - 1 ;
4719 }
4720 }
4721 break;
4722}
4723
drh98757152008-01-09 23:04:12 +00004724/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004725**
4726** Delete an entire database table or index whose root page in the database
4727** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004728**
drh98757152008-01-09 23:04:12 +00004729** The table being destroyed is in the main database file if P3==0. If
4730** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004731** that is used to store tables create using CREATE TEMPORARY TABLE.
4732**
drh205f48e2004-11-05 00:43:11 +00004733** If AUTOVACUUM is enabled then it is possible that another root page
4734** might be moved into the newly deleted root page in order to keep all
4735** root pages contiguous at the beginning of the database. The former
4736** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004737** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004738** movement was required (because the table being dropped was already
4739** the last one in the database) then a zero is stored in register P2.
4740** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004741**
drhb19a2bc2001-09-16 00:13:26 +00004742** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004743*/
drh98757152008-01-09 23:04:12 +00004744case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004745 int iMoved;
drh3765df42006-06-28 18:18:09 +00004746 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004747 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004748 int iDb;
drh3a949872012-09-18 13:20:13 +00004749
drh856c1032009-06-02 15:21:42 +00004750#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004751 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004752 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danielk1977212b2182006-06-23 14:32:08 +00004753 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
4754 iCnt++;
4755 }
4756 }
drh3765df42006-06-28 18:18:09 +00004757#else
4758 iCnt = db->activeVdbeCnt;
danielk1977212b2182006-06-23 14:32:08 +00004759#endif
drh3c657212009-11-17 23:59:58 +00004760 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004761 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004762 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004763 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004764 }else{
drh856c1032009-06-02 15:21:42 +00004765 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004766 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004767 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh98757152008-01-09 23:04:12 +00004768 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004769 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004770 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004771#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004772 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004773 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4774 /* All OP_Destroy operations occur on the same btree */
4775 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4776 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004777 }
drh3765df42006-06-28 18:18:09 +00004778#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004779 }
drh5e00f6c2001-09-13 13:46:56 +00004780 break;
4781}
4782
danielk1977c7af4842008-10-27 13:59:33 +00004783/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004784**
4785** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004786** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004787** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004788**
drhf57b3392001-10-08 13:22:32 +00004789** The table being clear is in the main database file if P2==0. If
4790** P2==1 then the table to be clear is in the auxiliary database file
4791** that is used to store tables create using CREATE TEMPORARY TABLE.
4792**
shanebe217792009-03-05 04:20:31 +00004793** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004794** intkey table (an SQL table, not an index). In this case the row change
4795** count is incremented by the number of rows in the table being cleared.
4796** If P3 is greater than zero, then the value stored in register P3 is
4797** also incremented by the number of rows in the table being cleared.
4798**
drhb19a2bc2001-09-16 00:13:26 +00004799** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004800*/
drh9cbf3422008-01-17 16:22:13 +00004801case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004802 int nChange;
4803
4804 nChange = 0;
drhdddd7792011-04-03 18:19:25 +00004805 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004806 rc = sqlite3BtreeClearTable(
4807 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4808 );
4809 if( pOp->p3 ){
4810 p->nChange += nChange;
4811 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004812 assert( memIsValid(&aMem[pOp->p3]) );
4813 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004814 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004815 }
4816 }
drh5edc3122001-09-13 21:53:09 +00004817 break;
4818}
4819
drh4c583122008-01-04 22:01:03 +00004820/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004821**
drh4c583122008-01-04 22:01:03 +00004822** Allocate a new table in the main database file if P1==0 or in the
4823** auxiliary database file if P1==1 or in an attached database if
4824** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004825** register P2
drh5b2fd562001-09-13 15:21:31 +00004826**
drhc6b52df2002-01-04 03:09:29 +00004827** The difference between a table and an index is this: A table must
4828** have a 4-byte integer key and can have arbitrary data. An index
4829** has an arbitrary key but no data.
4830**
drhb19a2bc2001-09-16 00:13:26 +00004831** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004832*/
drh4c583122008-01-04 22:01:03 +00004833/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004834**
drh4c583122008-01-04 22:01:03 +00004835** Allocate a new index in the main database file if P1==0 or in the
4836** auxiliary database file if P1==1 or in an attached database if
4837** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004838** register P2.
drhf57b3392001-10-08 13:22:32 +00004839**
drhc6b52df2002-01-04 03:09:29 +00004840** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004841*/
drh4c583122008-01-04 22:01:03 +00004842case OP_CreateIndex: /* out2-prerelease */
4843case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004844 int pgno;
drhf328bc82004-05-10 23:29:49 +00004845 int flags;
drh234c39d2004-07-24 03:30:47 +00004846 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004847
4848 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004849 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004850 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh234c39d2004-07-24 03:30:47 +00004851 pDb = &db->aDb[pOp->p1];
4852 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004853 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004854 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004855 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004856 }else{
drhd4187c72010-08-30 22:15:45 +00004857 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004858 }
drh234c39d2004-07-24 03:30:47 +00004859 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004860 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004861 break;
4862}
4863
drh22645842011-03-24 01:34:03 +00004864/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004865**
4866** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004867** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004868**
4869** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004870** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004871*/
drh9cbf3422008-01-17 16:22:13 +00004872case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004873 int iDb;
4874 const char *zMaster;
4875 char *zSql;
4876 InitData initData;
4877
drhbdaec522011-04-04 00:14:43 +00004878 /* Any prepared statement that invokes this opcode will hold mutexes
4879 ** on every btree. This is a prerequisite for invoking
4880 ** sqlite3InitCallback().
4881 */
4882#ifdef SQLITE_DEBUG
4883 for(iDb=0; iDb<db->nDb; iDb++){
4884 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4885 }
4886#endif
drhbdaec522011-04-04 00:14:43 +00004887
drh856c1032009-06-02 15:21:42 +00004888 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004889 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004890 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004891 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004892 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004893 initData.db = db;
4894 initData.iDb = pOp->p1;
4895 initData.pzErrMsg = &p->zErrMsg;
4896 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004897 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004898 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4899 if( zSql==0 ){
4900 rc = SQLITE_NOMEM;
4901 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004902 assert( db->init.busy==0 );
4903 db->init.busy = 1;
4904 initData.rc = SQLITE_OK;
4905 assert( !db->mallocFailed );
4906 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4907 if( rc==SQLITE_OK ) rc = initData.rc;
4908 sqlite3DbFree(db, zSql);
4909 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004910 }
drh3c23a882007-01-09 14:01:13 +00004911 }
drh81028a42012-05-15 18:28:27 +00004912 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00004913 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004914 goto no_mem;
4915 }
drh234c39d2004-07-24 03:30:47 +00004916 break;
4917}
4918
drh8bfdf722009-06-19 14:06:03 +00004919#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004920/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004921**
4922** Read the sqlite_stat1 table for database P1 and load the content
4923** of that table into the internal index hash table. This will cause
4924** the analysis to be used when preparing all subsequent queries.
4925*/
drh9cbf3422008-01-17 16:22:13 +00004926case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004927 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4928 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004929 break;
4930}
drh8bfdf722009-06-19 14:06:03 +00004931#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004932
drh98757152008-01-09 23:04:12 +00004933/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004934**
4935** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004936** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004937** is dropped in order to keep the internal representation of the
4938** schema consistent with what is on disk.
4939*/
drh9cbf3422008-01-17 16:22:13 +00004940case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004941 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004942 break;
4943}
4944
drh98757152008-01-09 23:04:12 +00004945/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004946**
4947** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004948** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004949** is dropped in order to keep the internal representation of the
4950** schema consistent with what is on disk.
4951*/
drh9cbf3422008-01-17 16:22:13 +00004952case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004953 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004954 break;
4955}
4956
drh98757152008-01-09 23:04:12 +00004957/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004958**
4959** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004960** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004961** is dropped in order to keep the internal representation of the
4962** schema consistent with what is on disk.
4963*/
drh9cbf3422008-01-17 16:22:13 +00004964case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004965 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004966 break;
4967}
4968
drh234c39d2004-07-24 03:30:47 +00004969
drhb7f91642004-10-31 02:22:47 +00004970#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004971/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004972**
drh98757152008-01-09 23:04:12 +00004973** Do an analysis of the currently open database. Store in
4974** register P1 the text of an error message describing any problems.
4975** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004976**
drh98757152008-01-09 23:04:12 +00004977** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004978** At most reg(P3) errors will be reported.
4979** In other words, the analysis stops as soon as reg(P1) errors are
4980** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004981**
drh79069752004-05-22 21:30:40 +00004982** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004983** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004984** total.
drh21504322002-06-25 13:16:02 +00004985**
drh98757152008-01-09 23:04:12 +00004986** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00004987** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004988**
drh1dcdbc02007-01-27 02:24:54 +00004989** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00004990*/
drhaaab5722002-02-19 13:39:21 +00004991case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00004992 int nRoot; /* Number of tables to check. (Number of root pages.) */
4993 int *aRoot; /* Array of rootpage numbers for tables to be checked */
4994 int j; /* Loop counter */
4995 int nErr; /* Number of errors reported */
4996 char *z; /* Text of the error report */
4997 Mem *pnErr; /* Register keeping track of errors remaining */
4998
4999 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005000 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005001 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005002 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00005003 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005004 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005005 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005006 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005007 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005008 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005009 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005010 }
5011 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005012 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005013 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00005014 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005015 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005016 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005017 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005018 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005019 if( nErr==0 ){
5020 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005021 }else if( z==0 ){
5022 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005023 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005024 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005025 }
drhb7654112008-01-12 12:48:07 +00005026 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005027 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005028 break;
5029}
drhb7f91642004-10-31 02:22:47 +00005030#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005031
drh3d4501e2008-12-04 20:40:10 +00005032/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005033**
drh3d4501e2008-12-04 20:40:10 +00005034** Insert the integer value held by register P2 into a boolean index
5035** held in register P1.
5036**
5037** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005038*/
drh93952eb2009-11-13 19:43:43 +00005039case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005040 pIn1 = &aMem[pOp->p1];
5041 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005042 assert( (pIn2->flags & MEM_Int)!=0 );
5043 if( (pIn1->flags & MEM_RowSet)==0 ){
5044 sqlite3VdbeMemSetRowSet(pIn1);
5045 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005046 }
drh93952eb2009-11-13 19:43:43 +00005047 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005048 break;
5049}
5050
5051/* Opcode: RowSetRead P1 P2 P3 * *
5052**
5053** Extract the smallest value from boolean index P1 and put that value into
5054** register P3. Or, if boolean index P1 is initially empty, leave P3
5055** unchanged and jump to instruction P2.
5056*/
drh93952eb2009-11-13 19:43:43 +00005057case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005058 i64 val;
drh3d4501e2008-12-04 20:40:10 +00005059 CHECK_FOR_INTERRUPT;
drh3c657212009-11-17 23:59:58 +00005060 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005061 if( (pIn1->flags & MEM_RowSet)==0
5062 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005063 ){
5064 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005065 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005066 pc = pOp->p2 - 1;
5067 }else{
5068 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005069 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005070 }
drh5e00f6c2001-09-13 13:46:56 +00005071 break;
5072}
5073
drh1b26c7c2009-04-22 02:15:47 +00005074/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00005075**
drhade97602009-04-21 15:05:18 +00005076** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005077** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005078** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005079** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005080** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005081**
drh1b26c7c2009-04-22 02:15:47 +00005082** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005083** of integers, where each set contains no duplicates. Each set
5084** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005085** must have P4==0, the final set P4=-1. P4 must be either -1 or
5086** non-negative. For non-negative values of P4 only the lower 4
5087** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005088**
5089** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005090** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005091** (b) when P4==-1 there is no need to insert the value, as it will
5092** never be tested for, and (c) when a value that is part of set X is
5093** inserted, there is no need to search to see if the same value was
5094** previously inserted as part of set X (only if it was previously
5095** inserted as part of some other set).
5096*/
drh1b26c7c2009-04-22 02:15:47 +00005097case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005098 int iSet;
5099 int exists;
5100
drh3c657212009-11-17 23:59:58 +00005101 pIn1 = &aMem[pOp->p1];
5102 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005103 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005104 assert( pIn3->flags&MEM_Int );
5105
drh1b26c7c2009-04-22 02:15:47 +00005106 /* If there is anything other than a rowset object in memory cell P1,
5107 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005108 */
drh733bf1b2009-04-22 00:47:00 +00005109 if( (pIn1->flags & MEM_RowSet)==0 ){
5110 sqlite3VdbeMemSetRowSet(pIn1);
5111 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005112 }
5113
5114 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005115 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005116 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005117 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5118 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005119 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005120 if( exists ){
5121 pc = pOp->p2 - 1;
5122 break;
5123 }
5124 }
5125 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005126 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005127 }
5128 break;
5129}
5130
drh5e00f6c2001-09-13 13:46:56 +00005131
danielk197793758c82005-01-21 08:13:14 +00005132#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005133
5134/* Opcode: Program P1 P2 P3 P4 *
5135**
dan76d462e2009-08-30 11:42:51 +00005136** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005137**
dan76d462e2009-08-30 11:42:51 +00005138** P1 contains the address of the memory cell that contains the first memory
5139** cell in an array of values used as arguments to the sub-program. P2
5140** contains the address to jump to if the sub-program throws an IGNORE
5141** exception using the RAISE() function. Register P3 contains the address
5142** of a memory cell in this (the parent) VM that is used to allocate the
5143** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005144**
5145** P4 is a pointer to the VM containing the trigger program.
5146*/
dan76d462e2009-08-30 11:42:51 +00005147case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005148 int nMem; /* Number of memory registers for sub-program */
5149 int nByte; /* Bytes of runtime space required for sub-program */
5150 Mem *pRt; /* Register to allocate runtime space */
5151 Mem *pMem; /* Used to iterate through memory cells */
5152 Mem *pEnd; /* Last memory cell in new array */
5153 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5154 SubProgram *pProgram; /* Sub-program to execute */
5155 void *t; /* Token identifying trigger */
5156
5157 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005158 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005159 assert( pProgram->nOp>0 );
5160
dan1da40a32009-09-19 17:00:31 +00005161 /* If the p5 flag is clear, then recursive invocation of triggers is
5162 ** disabled for backwards compatibility (p5 is set if this sub-program
5163 ** is really a trigger, not a foreign key action, and the flag set
5164 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005165 **
5166 ** It is recursive invocation of triggers, at the SQL level, that is
5167 ** disabled. In some cases a single trigger may generate more than one
5168 ** SubProgram (if the trigger may be executed with more than one different
5169 ** ON CONFLICT algorithm). SubProgram structures associated with a
5170 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005171 ** variable. */
5172 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005173 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005174 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5175 if( pFrame ) break;
5176 }
5177
danf5894502009-10-07 18:41:19 +00005178 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005179 rc = SQLITE_ERROR;
5180 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5181 break;
5182 }
5183
5184 /* Register pRt is used to store the memory required to save the state
5185 ** of the current program, and the memory required at runtime to execute
5186 ** the trigger program. If this trigger has been fired before, then pRt
5187 ** is already allocated. Otherwise, it must be initialized. */
5188 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005189 /* SubProgram.nMem is set to the number of memory cells used by the
5190 ** program stored in SubProgram.aOp. As well as these, one memory
5191 ** cell is required for each cursor used by the program. Set local
5192 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5193 */
dan65a7cd12009-09-01 12:16:01 +00005194 nMem = pProgram->nMem + pProgram->nCsr;
5195 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005196 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005197 + pProgram->nCsr * sizeof(VdbeCursor *)
5198 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005199 pFrame = sqlite3DbMallocZero(db, nByte);
5200 if( !pFrame ){
5201 goto no_mem;
5202 }
5203 sqlite3VdbeMemRelease(pRt);
5204 pRt->flags = MEM_Frame;
5205 pRt->u.pFrame = pFrame;
5206
5207 pFrame->v = p;
5208 pFrame->nChildMem = nMem;
5209 pFrame->nChildCsr = pProgram->nCsr;
5210 pFrame->pc = pc;
5211 pFrame->aMem = p->aMem;
5212 pFrame->nMem = p->nMem;
5213 pFrame->apCsr = p->apCsr;
5214 pFrame->nCursor = p->nCursor;
5215 pFrame->aOp = p->aOp;
5216 pFrame->nOp = p->nOp;
5217 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005218 pFrame->aOnceFlag = p->aOnceFlag;
5219 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005220
5221 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5222 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drhec86c722011-12-09 17:27:51 +00005223 pMem->flags = MEM_Invalid;
dan165921a2009-08-28 18:53:45 +00005224 pMem->db = db;
5225 }
5226 }else{
5227 pFrame = pRt->u.pFrame;
5228 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5229 assert( pProgram->nCsr==pFrame->nChildCsr );
5230 assert( pc==pFrame->pc );
5231 }
5232
5233 p->nFrame++;
5234 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005235 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005236 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005237 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005238 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005239 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005240 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005241 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005242 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005243 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005244 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005245 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5246 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005247 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005248 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005249
5250 break;
5251}
5252
dan76d462e2009-08-30 11:42:51 +00005253/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005254**
dan76d462e2009-08-30 11:42:51 +00005255** This opcode is only ever present in sub-programs called via the
5256** OP_Program instruction. Copy a value currently stored in a memory
5257** cell of the calling (parent) frame to cell P2 in the current frames
5258** address space. This is used by trigger programs to access the new.*
5259** and old.* values.
dan165921a2009-08-28 18:53:45 +00005260**
dan76d462e2009-08-30 11:42:51 +00005261** The address of the cell in the parent frame is determined by adding
5262** the value of the P1 argument to the value of the P1 argument to the
5263** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005264*/
dan76d462e2009-08-30 11:42:51 +00005265case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005266 VdbeFrame *pFrame;
5267 Mem *pIn;
5268 pFrame = p->pFrame;
5269 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005270 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5271 break;
5272}
5273
danielk197793758c82005-01-21 08:13:14 +00005274#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005275
dan1da40a32009-09-19 17:00:31 +00005276#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005277/* Opcode: FkCounter P1 P2 * * *
dan1da40a32009-09-19 17:00:31 +00005278**
dan0ff297e2009-09-25 17:03:14 +00005279** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5280** If P1 is non-zero, the database constraint counter is incremented
5281** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005282** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005283*/
dan32b09f22009-09-23 17:29:59 +00005284case OP_FkCounter: {
dan0ff297e2009-09-25 17:03:14 +00005285 if( pOp->p1 ){
5286 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005287 }else{
dan0ff297e2009-09-25 17:03:14 +00005288 p->nFkConstraint += pOp->p2;
5289 }
5290 break;
5291}
5292
5293/* Opcode: FkIfZero P1 P2 * * *
5294**
5295** This opcode tests if a foreign key constraint-counter is currently zero.
5296** If so, jump to instruction P2. Otherwise, fall through to the next
5297** instruction.
5298**
5299** If P1 is non-zero, then the jump is taken if the database constraint-counter
5300** is zero (the one that counts deferred constraint violations). If P1 is
5301** zero, the jump is taken if the statement constraint-counter is zero
5302** (immediate foreign key constraint violations).
5303*/
5304case OP_FkIfZero: { /* jump */
5305 if( pOp->p1 ){
5306 if( db->nDeferredCons==0 ) pc = pOp->p2-1;
5307 }else{
5308 if( p->nFkConstraint==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005309 }
dan1da40a32009-09-19 17:00:31 +00005310 break;
5311}
5312#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5313
drh205f48e2004-11-05 00:43:11 +00005314#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005315/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00005316**
dan76d462e2009-08-30 11:42:51 +00005317** P1 is a register in the root frame of this VM (the root frame is
5318** different from the current frame if this instruction is being executed
5319** within a sub-program). Set the value of register P1 to the maximum of
5320** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005321**
5322** This instruction throws an error if the memory cell is not initially
5323** an integer.
5324*/
dan76d462e2009-08-30 11:42:51 +00005325case OP_MemMax: { /* in2 */
5326 Mem *pIn1;
5327 VdbeFrame *pFrame;
5328 if( p->pFrame ){
5329 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5330 pIn1 = &pFrame->aMem[pOp->p1];
5331 }else{
drha6c2ed92009-11-14 23:22:23 +00005332 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005333 }
drhec86c722011-12-09 17:27:51 +00005334 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005335 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005336 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005337 sqlite3VdbeMemIntegerify(pIn2);
5338 if( pIn1->u.i<pIn2->u.i){
5339 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005340 }
5341 break;
5342}
5343#endif /* SQLITE_OMIT_AUTOINCREMENT */
5344
drh98757152008-01-09 23:04:12 +00005345/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00005346**
drh98757152008-01-09 23:04:12 +00005347** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005348**
drh98757152008-01-09 23:04:12 +00005349** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005350** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005351*/
drh9cbf3422008-01-17 16:22:13 +00005352case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005353 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005354 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005355 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005356 pc = pOp->p2 - 1;
5357 }
5358 break;
5359}
5360
drh98757152008-01-09 23:04:12 +00005361/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00005362**
drh98757152008-01-09 23:04:12 +00005363** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005364**
drh98757152008-01-09 23:04:12 +00005365** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005366** not contain an integer. An assertion fault will result if you try.
5367*/
drh9cbf3422008-01-17 16:22:13 +00005368case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005369 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005370 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005371 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005372 pc = pOp->p2 - 1;
5373 }
5374 break;
5375}
5376
drh9b918ed2009-11-12 03:13:26 +00005377/* Opcode: IfZero P1 P2 P3 * *
drhec7429a2005-10-06 16:53:14 +00005378**
drh9b918ed2009-11-12 03:13:26 +00005379** The register P1 must contain an integer. Add literal P3 to the
5380** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005381**
drh98757152008-01-09 23:04:12 +00005382** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005383** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005384*/
drh9cbf3422008-01-17 16:22:13 +00005385case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005386 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005387 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005388 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005389 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005390 pc = pOp->p2 - 1;
5391 }
5392 break;
5393}
5394
drh98757152008-01-09 23:04:12 +00005395/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00005396**
drh0bce8352002-02-28 00:41:10 +00005397** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005398** function has P5 arguments. P4 is a pointer to the FuncDef
5399** structure that specifies the function. Use register
5400** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005401**
drh98757152008-01-09 23:04:12 +00005402** The P5 arguments are taken from register P2 and its
5403** successors.
drhe5095352002-02-24 03:25:14 +00005404*/
drh9cbf3422008-01-17 16:22:13 +00005405case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005406 int n;
drhe5095352002-02-24 03:25:14 +00005407 int i;
drhc54a6172009-06-02 16:06:03 +00005408 Mem *pMem;
5409 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005410 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005411 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005412
drh856c1032009-06-02 15:21:42 +00005413 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005414 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005415 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005416 apVal = p->apArg;
5417 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005418 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005419 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005420 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005421 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005422 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005423 }
danielk19772dca4ac2008-01-03 11:50:29 +00005424 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00005425 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005426 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005427 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005428 ctx.s.flags = MEM_Null;
5429 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005430 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005431 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005432 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005433 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005434 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005435 ctx.skipFlag = 0;
drhe82f5d02008-10-07 19:53:14 +00005436 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005437 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005438 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005439 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005440 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005441 }
drhee9ff672010-09-03 18:50:48 +00005442 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005443 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005444 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005445 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005446 }
drh7a957892012-02-02 17:35:43 +00005447 if( ctx.skipFlag ){
5448 assert( pOp[-1].opcode==OP_CollSeq );
5449 i = pOp[-1].p1;
5450 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5451 }
drhbdaec522011-04-04 00:14:43 +00005452
drh90669c12006-01-20 15:45:36 +00005453 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005454
drh5e00f6c2001-09-13 13:46:56 +00005455 break;
5456}
5457
drh98757152008-01-09 23:04:12 +00005458/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00005459**
drh13449892005-09-07 21:22:45 +00005460** Execute the finalizer function for an aggregate. P1 is
5461** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005462**
5463** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005464** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005465** argument is not used by this opcode. It is only there to disambiguate
5466** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005467** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005468** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005469*/
drh9cbf3422008-01-17 16:22:13 +00005470case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005471 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00005472 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005473 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005474 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005475 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005476 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005477 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005478 }
drh2dca8682008-03-21 17:13:13 +00005479 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005480 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005481 if( sqlite3VdbeMemTooBig(pMem) ){
5482 goto too_big;
5483 }
drh5e00f6c2001-09-13 13:46:56 +00005484 break;
5485}
5486
dan5cf53532010-05-01 16:40:20 +00005487#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005488/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005489**
5490** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005491** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005492** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5493** SQLITE_BUSY or not, respectively. Write the number of pages in the
5494** WAL after the checkpoint into mem[P3+1] and the number of pages
5495** in the WAL that have been checkpointed after the checkpoint
5496** completes into mem[P3+2]. However on an error, mem[P3+1] and
5497** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005498*/
5499case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005500 int i; /* Loop counter */
5501 int aRes[3]; /* Results */
5502 Mem *pMem; /* Write results here */
5503
5504 aRes[0] = 0;
5505 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005506 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5507 || pOp->p2==SQLITE_CHECKPOINT_FULL
5508 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5509 );
drh30aa3b92011-02-07 23:56:01 +00005510 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005511 if( rc==SQLITE_BUSY ){
5512 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005513 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005514 }
drh30aa3b92011-02-07 23:56:01 +00005515 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5516 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5517 }
dan7c246102010-04-12 19:00:29 +00005518 break;
5519};
dan5cf53532010-05-01 16:40:20 +00005520#endif
drh5e00f6c2001-09-13 13:46:56 +00005521
drhcac29a62010-07-02 19:36:52 +00005522#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005523/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005524**
5525** Change the journal mode of database P1 to P3. P3 must be one of the
5526** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5527** modes (delete, truncate, persist, off and memory), this is a simple
5528** operation. No IO is required.
5529**
5530** If changing into or out of WAL mode the procedure is more complicated.
5531**
5532** Write a string containing the final journal-mode to register P2.
5533*/
drhd80b2332010-05-01 00:59:37 +00005534case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005535 Btree *pBt; /* Btree to change journal mode of */
5536 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005537 int eNew; /* New journal mode */
5538 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005539#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005540 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005541#endif
dane04dc882010-04-20 18:53:15 +00005542
drhd80b2332010-05-01 00:59:37 +00005543 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005544 assert( eNew==PAGER_JOURNALMODE_DELETE
5545 || eNew==PAGER_JOURNALMODE_TRUNCATE
5546 || eNew==PAGER_JOURNALMODE_PERSIST
5547 || eNew==PAGER_JOURNALMODE_OFF
5548 || eNew==PAGER_JOURNALMODE_MEMORY
5549 || eNew==PAGER_JOURNALMODE_WAL
5550 || eNew==PAGER_JOURNALMODE_QUERY
5551 );
5552 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh3ebaee92010-05-06 21:37:22 +00005553
dane04dc882010-04-20 18:53:15 +00005554 pBt = db->aDb[pOp->p1].pBt;
5555 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005556 eOld = sqlite3PagerGetJournalMode(pPager);
5557 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5558 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005559
5560#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005561 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005562
drhd80b2332010-05-01 00:59:37 +00005563 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005564 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005565 */
5566 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005567 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005568 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005569 ){
drh0b9b4302010-06-11 17:01:24 +00005570 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005571 }
5572
drh0b9b4302010-06-11 17:01:24 +00005573 if( (eNew!=eOld)
5574 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5575 ){
5576 if( !db->autoCommit || db->activeVdbeCnt>1 ){
5577 rc = SQLITE_ERROR;
5578 sqlite3SetString(&p->zErrMsg, db,
5579 "cannot change %s wal mode from within a transaction",
5580 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5581 );
5582 break;
5583 }else{
5584
5585 if( eOld==PAGER_JOURNALMODE_WAL ){
5586 /* If leaving WAL mode, close the log file. If successful, the call
5587 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5588 ** file. An EXCLUSIVE lock may still be held on the database file
5589 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005590 */
drh0b9b4302010-06-11 17:01:24 +00005591 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005592 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005593 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005594 }
drh242c4f72010-06-22 14:49:39 +00005595 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5596 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5597 ** as an intermediate */
5598 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005599 }
5600
5601 /* Open a transaction on the database file. Regardless of the journal
5602 ** mode, this transaction always uses a rollback journal.
5603 */
5604 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5605 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005606 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005607 }
5608 }
5609 }
dan5cf53532010-05-01 16:40:20 +00005610#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005611
dand956efe2010-06-18 16:13:45 +00005612 if( rc ){
dand956efe2010-06-18 16:13:45 +00005613 eNew = eOld;
5614 }
drh0b9b4302010-06-11 17:01:24 +00005615 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005616
dane04dc882010-04-20 18:53:15 +00005617 pOut = &aMem[pOp->p2];
5618 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005619 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005620 pOut->n = sqlite3Strlen30(pOut->z);
5621 pOut->enc = SQLITE_UTF8;
5622 sqlite3VdbeChangeEncoding(pOut, encoding);
5623 break;
drhcac29a62010-07-02 19:36:52 +00005624};
5625#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005626
drhfdbcdee2007-03-27 14:44:50 +00005627#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005628/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005629**
5630** Vacuum the entire database. This opcode will cause other virtual
5631** machines to be created and run. It may not be called from within
5632** a transaction.
5633*/
drh9cbf3422008-01-17 16:22:13 +00005634case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00005635 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005636 break;
5637}
drh154d4b22006-09-21 11:02:16 +00005638#endif
drh6f8c91c2003-12-07 00:24:35 +00005639
danielk1977dddbcdc2007-04-26 14:42:34 +00005640#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005641/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005642**
5643** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005644** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005645** P2. Otherwise, fall through to the next instruction.
5646*/
drh9cbf3422008-01-17 16:22:13 +00005647case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005648 Btree *pBt;
5649
5650 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005651 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drhca5557f2007-05-04 18:30:40 +00005652 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005653 rc = sqlite3BtreeIncrVacuum(pBt);
5654 if( rc==SQLITE_DONE ){
5655 pc = pOp->p2 - 1;
5656 rc = SQLITE_OK;
5657 }
5658 break;
5659}
5660#endif
5661
drh98757152008-01-09 23:04:12 +00005662/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005663**
5664** Cause precompiled statements to become expired. An expired statement
5665** fails with an error code of SQLITE_SCHEMA if it is ever executed
5666** (via sqlite3_step()).
5667**
5668** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5669** then only the currently executing statement is affected.
5670*/
drh9cbf3422008-01-17 16:22:13 +00005671case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005672 if( !pOp->p1 ){
5673 sqlite3ExpirePreparedStatements(db);
5674 }else{
5675 p->expired = 1;
5676 }
5677 break;
5678}
5679
danielk1977c00da102006-01-07 13:21:04 +00005680#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005681/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00005682**
5683** Obtain a lock on a particular table. This instruction is only used when
5684** the shared-cache feature is enabled.
5685**
danielk197796d48e92009-06-29 06:00:37 +00005686** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005687** on which the lock is acquired. A readlock is obtained if P3==0 or
5688** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005689**
5690** P2 contains the root-page of the table to lock.
5691**
drh66a51672008-01-03 00:01:23 +00005692** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005693** used to generate an error message if the lock cannot be obtained.
5694*/
drh9cbf3422008-01-17 16:22:13 +00005695case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005696 u8 isWriteLock = (u8)pOp->p3;
5697 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5698 int p1 = pOp->p1;
5699 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005700 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005701 assert( isWriteLock==0 || isWriteLock==1 );
5702 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5703 if( (rc&0xFF)==SQLITE_LOCKED ){
5704 const char *z = pOp->p4.z;
5705 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5706 }
danielk1977c00da102006-01-07 13:21:04 +00005707 }
5708 break;
5709}
drhb9bb7c12006-06-11 23:41:55 +00005710#endif /* SQLITE_OMIT_SHARED_CACHE */
5711
5712#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005713/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005714**
danielk19773e3a84d2008-08-01 17:37:40 +00005715** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5716** xBegin method for that table.
5717**
5718** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005719** within a callback to a virtual table xSync() method. If it is, the error
5720** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005721*/
drh9cbf3422008-01-17 16:22:13 +00005722case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005723 VTable *pVTab;
5724 pVTab = pOp->p4.pVtab;
5725 rc = sqlite3VtabBegin(db, pVTab);
drhb9755982010-07-24 16:34:37 +00005726 if( pVTab ) importVtabErrMsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005727 break;
5728}
5729#endif /* SQLITE_OMIT_VIRTUALTABLE */
5730
5731#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005732/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005733**
drh66a51672008-01-03 00:01:23 +00005734** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005735** for that table.
5736*/
drh9cbf3422008-01-17 16:22:13 +00005737case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005738 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005739 break;
5740}
5741#endif /* SQLITE_OMIT_VIRTUALTABLE */
5742
5743#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005744/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005745**
drh66a51672008-01-03 00:01:23 +00005746** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005747** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005748*/
drh9cbf3422008-01-17 16:22:13 +00005749case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005750 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005751 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005752 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005753 break;
5754}
5755#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005756
drh9eff6162006-06-12 21:59:13 +00005757#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005758/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005759**
drh66a51672008-01-03 00:01:23 +00005760** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005761** P1 is a cursor number. This opcode opens a cursor to the virtual
5762** table and stores that cursor in P1.
5763*/
drh9cbf3422008-01-17 16:22:13 +00005764case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005765 VdbeCursor *pCur;
5766 sqlite3_vtab_cursor *pVtabCursor;
5767 sqlite3_vtab *pVtab;
5768 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005769
drh856c1032009-06-02 15:21:42 +00005770 pCur = 0;
5771 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005772 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005773 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005774 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005775 rc = pModule->xOpen(pVtab, &pVtabCursor);
drhb9755982010-07-24 16:34:37 +00005776 importVtabErrMsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005777 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005778 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005779 pVtabCursor->pVtab = pVtab;
5780
5781 /* Initialise vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005782 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005783 if( pCur ){
5784 pCur->pVtabCursor = pVtabCursor;
5785 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005786 }else{
drh17435752007-08-16 04:30:38 +00005787 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005788 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005789 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005790 }
drh9eff6162006-06-12 21:59:13 +00005791 break;
5792}
5793#endif /* SQLITE_OMIT_VIRTUALTABLE */
5794
5795#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005796/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005797**
5798** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5799** the filtered result set is empty.
5800**
drh66a51672008-01-03 00:01:23 +00005801** P4 is either NULL or a string that was generated by the xBestIndex
5802** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005803** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005804**
drh9eff6162006-06-12 21:59:13 +00005805** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005806** by P1. The integer query plan parameter to xFilter is stored in register
5807** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005808** xFilter method. Registers P3+2..P3+1+argc are the argc
5809** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005810** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005811**
danielk19776dbee812008-01-03 18:39:41 +00005812** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005813*/
drh9cbf3422008-01-17 16:22:13 +00005814case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005815 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005816 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005817 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005818 Mem *pQuery;
5819 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005820 sqlite3_vtab_cursor *pVtabCursor;
5821 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005822 VdbeCursor *pCur;
5823 int res;
5824 int i;
5825 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005826
drha6c2ed92009-11-14 23:22:23 +00005827 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005828 pArgc = &pQuery[1];
5829 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005830 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005831 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005832 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005833 pVtabCursor = pCur->pVtabCursor;
5834 pVtab = pVtabCursor->pVtab;
5835 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005836
drh9cbf3422008-01-17 16:22:13 +00005837 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005838 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005839 nArg = (int)pArgc->u.i;
5840 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005841
drh644a5292006-12-20 14:53:38 +00005842 /* Invoke the xFilter method */
5843 {
drh856c1032009-06-02 15:21:42 +00005844 res = 0;
5845 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005846 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005847 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005848 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005849 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005850
danielk1977be718892006-06-23 08:05:19 +00005851 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005852 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005853 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005854 importVtabErrMsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005855 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005856 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005857 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005858
danielk1977a298e902006-06-22 09:53:48 +00005859 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005860 pc = pOp->p2 - 1;
5861 }
5862 }
drh1d454a32008-01-31 19:34:51 +00005863 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005864
drh9eff6162006-06-12 21:59:13 +00005865 break;
5866}
5867#endif /* SQLITE_OMIT_VIRTUALTABLE */
5868
5869#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005870/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005871**
drh2133d822008-01-03 18:44:59 +00005872** Store the value of the P2-th column of
5873** the row of the virtual-table that the
5874** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005875*/
5876case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005877 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005878 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005879 Mem *pDest;
5880 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005881
drhdfe88ec2008-11-03 20:55:06 +00005882 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005883 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005884 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005885 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005886 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005887 if( pCur->nullRow ){
5888 sqlite3VdbeMemSetNull(pDest);
5889 break;
5890 }
danielk19773e3a84d2008-08-01 17:37:40 +00005891 pVtab = pCur->pVtabCursor->pVtab;
5892 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005893 assert( pModule->xColumn );
5894 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005895
5896 /* The output cell may already have a buffer allocated. Move
5897 ** the current contents to sContext.s so in case the user-function
5898 ** can use the already allocated buffer instead of allocating a
5899 ** new one.
5900 */
5901 sqlite3VdbeMemMove(&sContext.s, pDest);
5902 MemSetTypeFlag(&sContext.s, MEM_Null);
5903
drhde4fcfd2008-01-19 23:50:26 +00005904 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
drhb9755982010-07-24 16:34:37 +00005905 importVtabErrMsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005906 if( sContext.isError ){
5907 rc = sContext.isError;
5908 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005909
drhde4fcfd2008-01-19 23:50:26 +00005910 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005911 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005912 ** dynamic allocation in sContext.s (a Mem struct) is released.
5913 */
5914 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005915 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005916 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005917 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005918
drhde4fcfd2008-01-19 23:50:26 +00005919 if( sqlite3VdbeMemTooBig(pDest) ){
5920 goto too_big;
5921 }
drh9eff6162006-06-12 21:59:13 +00005922 break;
5923}
5924#endif /* SQLITE_OMIT_VIRTUALTABLE */
5925
5926#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005927/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005928**
5929** Advance virtual table P1 to the next row in its result set and
5930** jump to instruction P2. Or, if the virtual table has reached
5931** the end of its result set, then fall through to the next instruction.
5932*/
drh9cbf3422008-01-17 16:22:13 +00005933case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005934 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005935 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005936 int res;
drh856c1032009-06-02 15:21:42 +00005937 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005938
drhc54a6172009-06-02 16:06:03 +00005939 res = 0;
drh856c1032009-06-02 15:21:42 +00005940 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005941 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005942 if( pCur->nullRow ){
5943 break;
5944 }
danielk19773e3a84d2008-08-01 17:37:40 +00005945 pVtab = pCur->pVtabCursor->pVtab;
5946 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005947 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005948
drhde4fcfd2008-01-19 23:50:26 +00005949 /* Invoke the xNext() method of the module. There is no way for the
5950 ** underlying implementation to return an error if one occurs during
5951 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5952 ** data is available) and the error code returned when xColumn or
5953 ** some other method is next invoked on the save virtual table cursor.
5954 */
drhde4fcfd2008-01-19 23:50:26 +00005955 p->inVtabMethod = 1;
5956 rc = pModule->xNext(pCur->pVtabCursor);
5957 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005958 importVtabErrMsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005959 if( rc==SQLITE_OK ){
5960 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005961 }
5962
drhde4fcfd2008-01-19 23:50:26 +00005963 if( !res ){
5964 /* If there is data, jump to P2 */
5965 pc = pOp->p2 - 1;
5966 }
drh9eff6162006-06-12 21:59:13 +00005967 break;
5968}
5969#endif /* SQLITE_OMIT_VIRTUALTABLE */
5970
danielk1977182c4ba2007-06-27 15:53:34 +00005971#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005972/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005973**
drh66a51672008-01-03 00:01:23 +00005974** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005975** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005976** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005977*/
drh9cbf3422008-01-17 16:22:13 +00005978case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00005979 sqlite3_vtab *pVtab;
5980 Mem *pName;
5981
danielk1977595a5232009-07-24 17:58:53 +00005982 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00005983 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00005984 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00005985 assert( memIsValid(pName) );
drh5b6afba2008-01-05 16:29:28 +00005986 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00005987 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00005988 testcase( pName->enc==SQLITE_UTF8 );
5989 testcase( pName->enc==SQLITE_UTF16BE );
5990 testcase( pName->enc==SQLITE_UTF16LE );
5991 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
5992 if( rc==SQLITE_OK ){
5993 rc = pVtab->pModule->xRename(pVtab, pName->z);
5994 importVtabErrMsg(p, pVtab);
5995 p->expired = 0;
5996 }
danielk1977182c4ba2007-06-27 15:53:34 +00005997 break;
5998}
5999#endif
drh4cbdda92006-06-14 19:00:20 +00006000
6001#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006002/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00006003**
drh66a51672008-01-03 00:01:23 +00006004** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006005** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006006** are contiguous memory cells starting at P3 to pass to the xUpdate
6007** invocation. The value in register (P3+P2-1) corresponds to the
6008** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006009**
6010** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006011** The argv[0] element (which corresponds to memory cell P3)
6012** is the rowid of a row to delete. If argv[0] is NULL then no
6013** deletion occurs. The argv[1] element is the rowid of the new
6014** row. This can be NULL to have the virtual table select the new
6015** rowid for itself. The subsequent elements in the array are
6016** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006017**
6018** If P2==1 then no insert is performed. argv[0] is the rowid of
6019** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006020**
6021** P1 is a boolean flag. If it is set to true and the xUpdate call
6022** is successful, then the value returned by sqlite3_last_insert_rowid()
6023** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00006024*/
drh9cbf3422008-01-17 16:22:13 +00006025case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006026 sqlite3_vtab *pVtab;
6027 sqlite3_module *pModule;
6028 int nArg;
6029 int i;
6030 sqlite_int64 rowid;
6031 Mem **apArg;
6032 Mem *pX;
6033
danb061d052011-04-25 18:49:57 +00006034 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6035 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6036 );
danielk1977595a5232009-07-24 17:58:53 +00006037 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006038 pModule = (sqlite3_module *)pVtab->pModule;
6039 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006040 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006041 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006042 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006043 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006044 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006045 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006046 assert( memIsValid(pX) );
6047 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00006048 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00006049 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006050 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006051 }
danb061d052011-04-25 18:49:57 +00006052 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006053 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006054 db->vtabOnConflict = vtabOnConflict;
drhb9755982010-07-24 16:34:37 +00006055 importVtabErrMsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006056 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006057 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006058 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006059 }
danb061d052011-04-25 18:49:57 +00006060 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6061 if( pOp->p5==OE_Ignore ){
6062 rc = SQLITE_OK;
6063 }else{
6064 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6065 }
6066 }else{
6067 p->nChange++;
6068 }
danielk1977399918f2006-06-14 13:03:23 +00006069 }
drh4cbdda92006-06-14 19:00:20 +00006070 break;
danielk1977399918f2006-06-14 13:03:23 +00006071}
6072#endif /* SQLITE_OMIT_VIRTUALTABLE */
6073
danielk197759a93792008-05-15 17:48:20 +00006074#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6075/* Opcode: Pagecount P1 P2 * * *
6076**
6077** Write the current number of pages in database P1 to memory cell P2.
6078*/
6079case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006080 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006081 break;
6082}
6083#endif
6084
drh60ac3f42010-11-23 18:59:27 +00006085
6086#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6087/* Opcode: MaxPgcnt P1 P2 P3 * *
6088**
6089** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006090** Do not let the maximum page count fall below the current page count and
6091** do not change the maximum page count value if P3==0.
6092**
drh60ac3f42010-11-23 18:59:27 +00006093** Store the maximum page count after the change in register P2.
6094*/
6095case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006096 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006097 Btree *pBt;
6098
6099 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006100 newMax = 0;
6101 if( pOp->p3 ){
6102 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006103 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006104 }
6105 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006106 break;
6107}
6108#endif
6109
6110
drh949f9cd2008-01-12 21:35:57 +00006111#ifndef SQLITE_OMIT_TRACE
6112/* Opcode: Trace * * * P4 *
6113**
6114** If tracing is enabled (by the sqlite3_trace()) interface, then
6115** the UTF-8 string contained in P4 is emitted on the trace callback.
6116*/
6117case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00006118 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006119 char *z;
drh856c1032009-06-02 15:21:42 +00006120
drh37f58e92012-09-04 21:34:26 +00006121 if( db->xTrace
6122 && !p->doingRerun
6123 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6124 ){
drhc3f1d5f2011-05-30 23:42:16 +00006125 z = sqlite3VdbeExpandSql(p, zTrace);
6126 db->xTrace(db->pTraceArg, z);
6127 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006128 }
drhc3f1d5f2011-05-30 23:42:16 +00006129#ifdef SQLITE_DEBUG
6130 if( (db->flags & SQLITE_SqlTrace)!=0
6131 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6132 ){
6133 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6134 }
6135#endif /* SQLITE_DEBUG */
drh949f9cd2008-01-12 21:35:57 +00006136 break;
6137}
6138#endif
6139
drh91fd4d42008-01-19 20:11:25 +00006140
6141/* Opcode: Noop * * * * *
6142**
6143** Do nothing. This instruction is often useful as a jump
6144** destination.
drh5e00f6c2001-09-13 13:46:56 +00006145*/
drh91fd4d42008-01-19 20:11:25 +00006146/*
6147** The magic Explain opcode are only inserted when explain==2 (which
6148** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6149** This opcode records information from the optimizer. It is the
6150** the same as a no-op. This opcodesnever appears in a real VM program.
6151*/
6152default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006153 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006154 break;
6155}
6156
6157/*****************************************************************************
6158** The cases of the switch statement above this line should all be indented
6159** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6160** readability. From this point on down, the normal indentation rules are
6161** restored.
6162*****************************************************************************/
6163 }
drh6e142f52000-06-08 13:36:40 +00006164
drh7b396862003-01-01 23:06:20 +00006165#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006166 {
shane9bcbdad2008-05-29 20:22:37 +00006167 u64 elapsed = sqlite3Hwtime() - start;
6168 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006169 pOp->cnt++;
6170#if 0
shane9bcbdad2008-05-29 20:22:37 +00006171 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006172 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006173#endif
6174 }
drh7b396862003-01-01 23:06:20 +00006175#endif
6176
drh6e142f52000-06-08 13:36:40 +00006177 /* The following code adds nothing to the actual functionality
6178 ** of the program. It is only here for testing and debugging.
6179 ** On the other hand, it does burn CPU cycles every time through
6180 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6181 */
6182#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006183 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006184
drhcf1023c2007-05-08 20:59:49 +00006185#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00006186 if( p->trace ){
6187 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006188 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
6189 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006190 }
drh3c657212009-11-17 23:59:58 +00006191 if( pOp->opflags & OPFLG_OUT3 ){
6192 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006193 }
drh75897232000-05-29 14:26:00 +00006194 }
danielk1977b5402fb2005-01-12 07:15:04 +00006195#endif /* SQLITE_DEBUG */
6196#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006197 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006198
drha05a7222008-01-19 03:35:58 +00006199 /* If we reach this point, it means that execution is finished with
6200 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006201 */
drha05a7222008-01-19 03:35:58 +00006202vdbe_error_halt:
6203 assert( rc );
6204 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006205 testcase( sqlite3GlobalConfig.xLog!=0 );
6206 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6207 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006208 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006209 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6210 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006211 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006212 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006213 }
drh900b31e2007-08-28 02:27:51 +00006214
6215 /* This is the only way out of this procedure. We have to
6216 ** release the mutexes on btrees that were acquired at the
6217 ** top. */
6218vdbe_return:
drh99a66922011-05-13 18:51:42 +00006219 db->lastRowid = lastRowid;
drhbdaec522011-04-04 00:14:43 +00006220 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006221 return rc;
6222
drh023ae032007-05-08 12:12:16 +00006223 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6224 ** is encountered.
6225 */
6226too_big:
drhf089aa42008-07-08 19:34:06 +00006227 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006228 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006229 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006230
drh98640a32007-06-07 19:08:32 +00006231 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006232 */
6233no_mem:
drh17435752007-08-16 04:30:38 +00006234 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006235 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006236 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006237 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006238
drhb86ccfb2003-01-28 23:13:10 +00006239 /* Jump to here for any other kind of fatal error. The "rc" variable
6240 ** should hold the error number.
6241 */
6242abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006243 assert( p->zErrMsg==0 );
6244 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006245 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006246 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006247 }
drha05a7222008-01-19 03:35:58 +00006248 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006249
danielk19776f8a5032004-05-10 10:34:51 +00006250 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006251 ** flag.
6252 */
6253abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006254 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006255 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006256 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006257 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006258 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006259}