blob: 9af4a62d7ef5c24027c9aa5e0c1b1c017813c848 [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. */
dan689ab892011-08-12 15:02:00 +0000155# define isSorter(x) ((x)->pSorter!=0)
dan689ab892011-08-12 15:02:00 +0000156
danielk19771cc5ed82007-05-16 17:28:43 +0000157/*
shane21e7feb2008-05-30 15:59:49 +0000158** Argument pMem points at a register that will be passed to a
danielk1977c572ef72004-05-27 09:28:41 +0000159** user-defined function or returned to the user as the result of a query.
dan937d0de2009-10-15 18:35:38 +0000160** This routine sets the pMem->type variable used by the sqlite3_value_*()
161** routines.
danielk1977c572ef72004-05-27 09:28:41 +0000162*/
dan937d0de2009-10-15 18:35:38 +0000163void sqlite3VdbeMemStoreType(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000164 int flags = pMem->flags;
165 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000166 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000167 }
168 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000169 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000170 }
171 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000172 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000173 }
174 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000175 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000176 }else{
drh9c054832004-05-31 18:51:57 +0000177 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000178 }
179}
danielk19778a6b5412004-05-24 07:04:25 +0000180
181/*
drhdfe88ec2008-11-03 20:55:06 +0000182** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL
drh4774b132004-06-12 20:12:51 +0000183** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000184*/
drhdfe88ec2008-11-03 20:55:06 +0000185static VdbeCursor *allocateCursor(
186 Vdbe *p, /* The virtual machine */
187 int iCur, /* Index of the new VdbeCursor */
danielk1977d336e222009-02-20 10:58:41 +0000188 int nField, /* Number of fields in the table or index */
drhe4c88c02012-01-04 12:57:45 +0000189 int iDb, /* Database the cursor belongs to, or -1 */
drh3e9ca092009-09-08 01:14:48 +0000190 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
danielk1977cd3e8f72008-03-25 09:47:35 +0000191){
192 /* Find the memory cell that will be used to store the blob of memory
drhdfe88ec2008-11-03 20:55:06 +0000193 ** required for this VdbeCursor structure. It is convenient to use a
danielk1977cd3e8f72008-03-25 09:47:35 +0000194 ** vdbe memory cell to manage the memory allocation required for a
drhdfe88ec2008-11-03 20:55:06 +0000195 ** VdbeCursor structure for the following reasons:
danielk1977cd3e8f72008-03-25 09:47:35 +0000196 **
197 ** * Sometimes cursor numbers are used for a couple of different
198 ** purposes in a vdbe program. The different uses might require
199 ** different sized allocations. Memory cells provide growable
200 ** allocations.
201 **
202 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
203 ** be freed lazily via the sqlite3_release_memory() API. This
204 ** minimizes the number of malloc calls made by the system.
205 **
206 ** Memory cells for cursors are allocated at the top of the address
207 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
208 ** cursor 1 is managed by memory cell (p->nMem-1), etc.
209 */
210 Mem *pMem = &p->aMem[p->nMem-iCur];
211
danielk19775f096132008-03-28 15:44:09 +0000212 int nByte;
drhdfe88ec2008-11-03 20:55:06 +0000213 VdbeCursor *pCx = 0;
danielk19775f096132008-03-28 15:44:09 +0000214 nByte =
drh5cc10232013-11-21 01:04:02 +0000215 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
216 (isBtreeCursor?sqlite3BtreeCursorSize():0);
danielk1977cd3e8f72008-03-25 09:47:35 +0000217
drh290c1942004-08-21 17:54:45 +0000218 assert( iCur<p->nCursor );
219 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000220 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000221 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000222 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000223 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000224 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000225 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000226 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000227 pCx->nField = nField;
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000229 pCx->pCursor = (BtCursor*)
drh5cc10232013-11-21 01:04:02 +0000230 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
drhf25a5072009-11-18 23:01:25 +0000231 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000232 }
danielk197794eb6a12005-12-15 15:22:08 +0000233 }
drh4774b132004-06-12 20:12:51 +0000234 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000235}
236
danielk19773d1bfea2004-05-14 11:00:53 +0000237/*
drh29d72102006-02-09 22:13:41 +0000238** Try to convert a value into a numeric representation if we can
239** do so without loss of information. In other words, if the string
240** looks like a number, convert it into a number. If it does not
241** look like a number, leave it alone.
242*/
drhb21c8cd2007-08-21 19:33:56 +0000243static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000244 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000245 double rValue;
246 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000247 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000248 if( (pRec->flags&MEM_Str)==0 ) return;
249 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000250 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000251 pRec->u.i = iValue;
252 pRec->flags |= MEM_Int;
253 }else{
254 pRec->r = rValue;
255 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000256 }
257 }
258}
259
260/*
drh8a512562005-11-14 22:29:05 +0000261** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000262**
drh8a512562005-11-14 22:29:05 +0000263** SQLITE_AFF_INTEGER:
264** SQLITE_AFF_REAL:
265** SQLITE_AFF_NUMERIC:
266** Try to convert pRec to an integer representation or a
267** floating-point representation if an integer representation
268** is not possible. Note that the integer representation is
269** always preferred, even if the affinity is REAL, because
270** an integer representation is more space efficient on disk.
271**
272** SQLITE_AFF_TEXT:
273** Convert pRec to a text representation.
274**
275** SQLITE_AFF_NONE:
276** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000277*/
drh17435752007-08-16 04:30:38 +0000278static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000279 Mem *pRec, /* The value to apply affinity to */
280 char affinity, /* The affinity to be applied */
281 u8 enc /* Use this text encoding */
282){
drh8a512562005-11-14 22:29:05 +0000283 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000284 /* Only attempt the conversion to TEXT if there is an integer or real
285 ** representation (blob and NULL do not get converted) but no string
286 ** representation.
287 */
288 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000289 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000290 }
291 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000292 }else if( affinity!=SQLITE_AFF_NONE ){
293 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
294 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000295 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000296 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000297 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000298 }
danielk19773d1bfea2004-05-14 11:00:53 +0000299 }
300}
301
danielk1977aee18ef2005-03-09 12:26:50 +0000302/*
drh29d72102006-02-09 22:13:41 +0000303** Try to convert the type of a function argument or a result column
304** into a numeric representation. Use either INTEGER or REAL whichever
305** is appropriate. But only do the conversion if it is possible without
306** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000307*/
308int sqlite3_value_numeric_type(sqlite3_value *pVal){
309 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000310 if( pMem->type==SQLITE_TEXT ){
311 applyNumericAffinity(pMem);
312 sqlite3VdbeMemStoreType(pMem);
313 }
drh29d72102006-02-09 22:13:41 +0000314 return pMem->type;
315}
316
317/*
danielk1977aee18ef2005-03-09 12:26:50 +0000318** Exported version of applyAffinity(). This one works on sqlite3_value*,
319** not the internal Mem* type.
320*/
danielk19771e536952007-08-16 10:09:01 +0000321void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000322 sqlite3_value *pVal,
323 u8 affinity,
324 u8 enc
325){
drhb21c8cd2007-08-21 19:33:56 +0000326 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000327}
328
danielk1977b5402fb2005-01-12 07:15:04 +0000329#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000330/*
danielk1977ca6b2912004-05-21 10:49:47 +0000331** Write a nice string representation of the contents of cell pMem
332** into buffer zBuf, length nBuf.
333*/
drh74161702006-02-24 02:53:49 +0000334void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000335 char *zCsr = zBuf;
336 int f = pMem->flags;
337
drh57196282004-10-06 15:41:16 +0000338 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000339
danielk1977ca6b2912004-05-21 10:49:47 +0000340 if( f&MEM_Blob ){
341 int i;
342 char c;
343 if( f & MEM_Dyn ){
344 c = 'z';
345 assert( (f & (MEM_Static|MEM_Ephem))==0 );
346 }else if( f & MEM_Static ){
347 c = 't';
348 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
349 }else if( f & MEM_Ephem ){
350 c = 'e';
351 assert( (f & (MEM_Static|MEM_Dyn))==0 );
352 }else{
353 c = 's';
354 }
355
drh5bb3eb92007-05-04 13:15:55 +0000356 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000357 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000358 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000359 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000360 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000361 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000362 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000363 }
364 for(i=0; i<16 && i<pMem->n; i++){
365 char z = pMem->z[i];
366 if( z<32 || z>126 ) *zCsr++ = '.';
367 else *zCsr++ = z;
368 }
369
drhe718efe2007-05-10 21:14:03 +0000370 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000371 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000372 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000373 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000374 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000375 }
danielk1977b1bc9532004-05-22 03:05:33 +0000376 *zCsr = '\0';
377 }else if( f & MEM_Str ){
378 int j, k;
379 zBuf[0] = ' ';
380 if( f & MEM_Dyn ){
381 zBuf[1] = 'z';
382 assert( (f & (MEM_Static|MEM_Ephem))==0 );
383 }else if( f & MEM_Static ){
384 zBuf[1] = 't';
385 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
386 }else if( f & MEM_Ephem ){
387 zBuf[1] = 'e';
388 assert( (f & (MEM_Static|MEM_Dyn))==0 );
389 }else{
390 zBuf[1] = 's';
391 }
392 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000393 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000394 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000395 zBuf[k++] = '[';
396 for(j=0; j<15 && j<pMem->n; j++){
397 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000398 if( c>=0x20 && c<0x7f ){
399 zBuf[k++] = c;
400 }else{
401 zBuf[k++] = '.';
402 }
403 }
404 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000405 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000406 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000407 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000408 }
danielk1977ca6b2912004-05-21 10:49:47 +0000409}
410#endif
411
drh5b6afba2008-01-05 16:29:28 +0000412#ifdef SQLITE_DEBUG
413/*
414** Print the value of a register for tracing purposes:
415*/
drh84e55a82013-11-13 17:58:23 +0000416static void memTracePrint(Mem *p){
drh953f7612012-12-07 22:18:54 +0000417 if( p->flags & MEM_Invalid ){
drh84e55a82013-11-13 17:58:23 +0000418 printf(" undefined");
drh953f7612012-12-07 22:18:54 +0000419 }else if( p->flags & MEM_Null ){
drh84e55a82013-11-13 17:58:23 +0000420 printf(" NULL");
drh5b6afba2008-01-05 16:29:28 +0000421 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drh84e55a82013-11-13 17:58:23 +0000422 printf(" si:%lld", p->u.i);
drh5b6afba2008-01-05 16:29:28 +0000423 }else if( p->flags & MEM_Int ){
drh84e55a82013-11-13 17:58:23 +0000424 printf(" i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000425#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000426 }else if( p->flags & MEM_Real ){
drh84e55a82013-11-13 17:58:23 +0000427 printf(" r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000428#endif
drh733bf1b2009-04-22 00:47:00 +0000429 }else if( p->flags & MEM_RowSet ){
drh84e55a82013-11-13 17:58:23 +0000430 printf(" (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000431 }else{
432 char zBuf[200];
433 sqlite3VdbeMemPrettyPrint(p, zBuf);
drh84e55a82013-11-13 17:58:23 +0000434 printf(" %s", zBuf);
drh5b6afba2008-01-05 16:29:28 +0000435 }
436}
drh84e55a82013-11-13 17:58:23 +0000437static void registerTrace(int iReg, Mem *p){
438 printf("REG[%d] = ", iReg);
439 memTracePrint(p);
440 printf("\n");
drh5b6afba2008-01-05 16:29:28 +0000441}
442#endif
443
444#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000445# define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
drh5b6afba2008-01-05 16:29:28 +0000446#else
447# define REGISTER_TRACE(R,M)
448#endif
449
danielk197784ac9d02004-05-18 09:58:06 +0000450
drh7b396862003-01-01 23:06:20 +0000451#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000452
453/*
454** hwtime.h contains inline assembler code for implementing
455** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000456*/
shane9bcbdad2008-05-29 20:22:37 +0000457#include "hwtime.h"
458
drh7b396862003-01-01 23:06:20 +0000459#endif
460
drh8c74a8c2002-08-25 19:20:40 +0000461/*
drhcaec2f12003-01-07 02:47:47 +0000462** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000463** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000464** processing of the VDBE program is interrupted.
465**
466** This macro added to every instruction that does a jump in order to
467** implement a loop. This test used to be on every single instruction,
drhe4c88c02012-01-04 12:57:45 +0000468** but that meant we more testing than we needed. By only testing the
drhcaec2f12003-01-07 02:47:47 +0000469** flag on jump instructions, we get a (small) speed improvement.
470*/
471#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000472 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000473
474
danielk1977fd7f0452008-12-17 17:30:26 +0000475#ifndef NDEBUG
476/*
477** This function is only called from within an assert() expression. It
478** checks that the sqlite3.nTransaction variable is correctly set to
479** the number of non-transaction savepoints currently in the
480** linked list starting at sqlite3.pSavepoint.
481**
482** Usage:
483**
484** assert( checkSavepointCount(db) );
485*/
486static int checkSavepointCount(sqlite3 *db){
487 int n = 0;
488 Savepoint *p;
489 for(p=db->pSavepoint; p; p=p->pNext) n++;
490 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
491 return 1;
492}
493#endif
494
drhb9755982010-07-24 16:34:37 +0000495
496/*
drhb86ccfb2003-01-28 23:13:10 +0000497** Execute as much of a VDBE program as we can then return.
498**
danielk19774adee202004-05-08 08:23:19 +0000499** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000500** close the program with a final OP_Halt and to set up the callbacks
501** and the error message pointer.
502**
503** Whenever a row or result data is available, this routine will either
504** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000505** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000506**
507** If an attempt is made to open a locked database, then this routine
508** will either invoke the busy callback (if there is one) or it will
509** return SQLITE_BUSY.
510**
511** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000512** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000513** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
514**
515** If the callback ever returns non-zero, then the program exits
516** immediately. There will be no error message but the p->rc field is
517** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
518**
drh9468c7f2003-03-07 19:50:07 +0000519** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
520** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000521**
522** Other fatal errors return SQLITE_ERROR.
523**
danielk19774adee202004-05-08 08:23:19 +0000524** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000525** used to clean up the mess that was left behind.
526*/
danielk19774adee202004-05-08 08:23:19 +0000527int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000528 Vdbe *p /* The VDBE */
529){
shaneh84f4b2f2010-02-26 01:46:54 +0000530 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000531 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000532 Op *pOp; /* Current operation */
533 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000534 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000535 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000536 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000537 int iCompare = 0; /* Result of last OP_Compare operation */
538 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000539#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000540 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000541#endif
drha6c2ed92009-11-14 23:22:23 +0000542 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000543 Mem *pIn1 = 0; /* 1st input operand */
544 Mem *pIn2 = 0; /* 2nd input operand */
545 Mem *pIn3 = 0; /* 3rd input operand */
546 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000547 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000548 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000549#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000550 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000551 int origPc; /* Program counter at start of opcode */
552#endif
drh856c1032009-06-02 15:21:42 +0000553 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000554
drhca48c902008-01-18 14:08:24 +0000555 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000556 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000557 if( p->rc==SQLITE_NOMEM ){
558 /* This happens if a malloc() inside a call to sqlite3_column_text() or
559 ** sqlite3_column_text16() failed. */
560 goto no_mem;
561 }
drh3a840692003-01-29 22:58:26 +0000562 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000563 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000564 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000565 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000566 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000567 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000568 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000569 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000570 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000571#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
572 if( db->xProgress ){
573 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000574 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000575 if( nProgressLimit==0 ){
576 nProgressLimit = db->nProgressOps;
577 }else{
578 nProgressLimit %= (unsigned)db->nProgressOps;
579 }
580 }
581#endif
drh3c23a882007-01-09 14:01:13 +0000582#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000583 sqlite3BeginBenignMalloc();
drh84e55a82013-11-13 17:58:23 +0000584 if( p->pc==0
585 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
586 ){
drh3c23a882007-01-09 14:01:13 +0000587 int i;
drh84e55a82013-11-13 17:58:23 +0000588 int once = 1;
drh3c23a882007-01-09 14:01:13 +0000589 sqlite3VdbePrintSql(p);
drh84e55a82013-11-13 17:58:23 +0000590 if( p->db->flags & SQLITE_VdbeListing ){
591 printf("VDBE Program Listing:\n");
592 for(i=0; i<p->nOp; i++){
593 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
594 }
drh3c23a882007-01-09 14:01:13 +0000595 }
drh84e55a82013-11-13 17:58:23 +0000596 if( p->db->flags & SQLITE_VdbeEQP ){
597 for(i=0; i<p->nOp; i++){
598 if( aOp[i].opcode==OP_Explain ){
599 if( once ) printf("VDBE Query Plan:\n");
600 printf("%s\n", aOp[i].p4.z);
601 once = 0;
602 }
603 }
604 }
605 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n");
drh3c23a882007-01-09 14:01:13 +0000606 }
danielk19772d1d86f2008-06-20 14:59:51 +0000607 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000608#endif
drhb86ccfb2003-01-28 23:13:10 +0000609 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000610 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000611 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000612#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000613 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000614 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000615#endif
drhbf159fa2013-06-25 22:01:22 +0000616 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000617 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000618
danielk19778b60e0f2005-01-12 09:10:39 +0000619 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000620 */
danielk19778b60e0f2005-01-12 09:10:39 +0000621#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +0000622 if( db->flags & SQLITE_VdbeTrace ){
623 sqlite3VdbePrintOp(stdout, 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
drhb5b407e2012-08-29 10:28:43 +0000640 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000641 ** external allocations out of mem[p2] and set mem[p2] to be
642 ** an undefined integer. Opcodes will either fill in the integer
643 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000644 */
drha6c2ed92009-11-14 23:22:23 +0000645 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000646 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
647 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000648 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000649 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000650 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000651 VdbeMemRelease(pOut);
drh3c657212009-11-17 23:59:58 +0000652 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000653 }
drh3c657212009-11-17 23:59:58 +0000654
655 /* Sanity checking on other operands */
656#ifdef SQLITE_DEBUG
657 if( (pOp->opflags & OPFLG_IN1)!=0 ){
658 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000659 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000660 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000661 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
662 }
663 if( (pOp->opflags & OPFLG_IN2)!=0 ){
664 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000665 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000666 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000667 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
668 }
669 if( (pOp->opflags & OPFLG_IN3)!=0 ){
670 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000671 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000672 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000673 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
674 }
675 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
676 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000677 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000678 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000679 }
680 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
681 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000682 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000683 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000684 }
685#endif
drh93952eb2009-11-13 19:43:43 +0000686
drh75897232000-05-29 14:26:00 +0000687 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000688
drh5e00f6c2001-09-13 13:46:56 +0000689/*****************************************************************************
690** What follows is a massive switch statement where each case implements a
691** separate instruction in the virtual machine. If we follow the usual
692** indentation conventions, each case should be indented by 6 spaces. But
693** that is a lot of wasted space on the left margin. So the code within
694** the switch statement will break with convention and be flush-left. Another
695** big comment (similar to this one) will mark the point in the code where
696** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000697**
698** The formatting of each case is important. The makefile for SQLite
699** generates two C files "opcodes.h" and "opcodes.c" by scanning this
700** file looking for lines that begin with "case OP_". The opcodes.h files
701** will be filled with #defines that give unique integer values to each
702** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000703** each string is the symbolic name for the corresponding opcode. If the
704** case statement is followed by a comment of the form "/# same as ... #/"
705** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000706**
drh9cbf3422008-01-17 16:22:13 +0000707** Other keywords in the comment that follows each case are used to
708** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
709** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
710** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000711**
drhac82fcf2002-09-08 17:23:41 +0000712** Documentation about VDBE opcodes is generated by scanning this file
713** for lines of that contain "Opcode:". That line and all subsequent
714** comment lines are used in the generation of the opcode.html documentation
715** file.
716**
717** SUMMARY:
718**
719** Formatting is important to scripts that scan this file.
720** Do not deviate from the formatting style currently in use.
721**
drh5e00f6c2001-09-13 13:46:56 +0000722*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000723
drh9cbf3422008-01-17 16:22:13 +0000724/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000725**
726** An unconditional jump to address P2.
727** The next instruction executed will be
728** the one at index P2 from the beginning of
729** the program.
730*/
drh9cbf3422008-01-17 16:22:13 +0000731case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000732 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000733
734 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
735 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
736 ** completion. Check to see if sqlite3_interrupt() has been called
737 ** or if the progress callback needs to be invoked.
738 **
739 ** This code uses unstructured "goto" statements and does not look clean.
740 ** But that is not due to sloppy coding habits. The code is written this
741 ** way for performance, to avoid having to run the interrupt and progress
742 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
743 ** faster according to "valgrind --tool=cachegrind" */
744check_for_interrupt:
745 CHECK_FOR_INTERRUPT;
746#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
747 /* Call the progress callback if it is configured and the required number
748 ** of VDBE ops have been executed (either since this invocation of
749 ** sqlite3VdbeExec() or since last time the progress callback was called).
750 ** If the progress callback returns non-zero, exit the virtual machine with
751 ** a return code SQLITE_ABORT.
752 */
drh0d1961e2013-07-25 16:27:51 +0000753 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh400fcba2013-11-14 00:09:48 +0000754 assert( db->nProgressOps!=0 );
755 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
756 if( db->xProgress(db->pProgressArg) ){
drh49afe3a2013-07-10 03:05:14 +0000757 rc = SQLITE_INTERRUPT;
758 goto vdbe_error_halt;
759 }
drh49afe3a2013-07-10 03:05:14 +0000760 }
761#endif
762
drh5e00f6c2001-09-13 13:46:56 +0000763 break;
764}
drh75897232000-05-29 14:26:00 +0000765
drh2eb95372008-06-06 15:04:36 +0000766/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000767**
drh2eb95372008-06-06 15:04:36 +0000768** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000769** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000770*/
drhb8475df2011-12-09 16:21:19 +0000771case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000772 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000773 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000774 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000775 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000776 pIn1->flags = MEM_Int;
777 pIn1->u.i = pc;
778 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000779 pc = pOp->p2 - 1;
780 break;
781}
782
drh2eb95372008-06-06 15:04:36 +0000783/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000784**
drh2eb95372008-06-06 15:04:36 +0000785** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000786*/
drh2eb95372008-06-06 15:04:36 +0000787case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000788 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000789 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000790 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000791 break;
792}
793
drhe00ee6e2008-06-20 15:24:01 +0000794/* Opcode: Yield P1 * * * *
795**
796** Swap the program counter with the value in register P1.
797*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000798case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000799 int pcDest;
drh3c657212009-11-17 23:59:58 +0000800 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000801 assert( (pIn1->flags & MEM_Dyn)==0 );
802 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000803 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000804 pIn1->u.i = pc;
805 REGISTER_TRACE(pOp->p1, pIn1);
806 pc = pcDest;
807 break;
808}
809
drhf9c8ce32013-11-05 13:33:55 +0000810/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +0000811** Synopsis: if r[P3] null then halt
drh5053a792009-02-20 03:02:23 +0000812**
drhef8662b2011-06-20 21:47:58 +0000813** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000814** parameter P1, P2, and P4 as if this were a Halt instruction. If the
815** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000816** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000817*/
818case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000819 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000820 if( (pIn3->flags & MEM_Null)==0 ) break;
821 /* Fall through into OP_Halt */
822}
drhe00ee6e2008-06-20 15:24:01 +0000823
drhf9c8ce32013-11-05 13:33:55 +0000824/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000825**
drh3d4501e2008-12-04 20:40:10 +0000826** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000827** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000828**
drh92f02c32004-09-02 14:57:08 +0000829** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
830** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
831** For errors, it can be some other value. If P1!=0 then P2 will determine
832** whether or not to rollback the current transaction. Do not rollback
833** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
834** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000835** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000836**
drh66a51672008-01-03 00:01:23 +0000837** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000838**
drhf9c8ce32013-11-05 13:33:55 +0000839** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
840**
841** 0: (no change)
842** 1: NOT NULL contraint failed: P4
843** 2: UNIQUE constraint failed: P4
844** 3: CHECK constraint failed: P4
845** 4: FOREIGN KEY constraint failed: P4
846**
847** If P5 is not zero and P4 is NULL, then everything after the ":" is
848** omitted.
849**
drh9cfcf5d2002-01-29 18:41:24 +0000850** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000851** every program. So a jump past the last instruction of the program
852** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000853*/
drh9cbf3422008-01-17 16:22:13 +0000854case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000855 const char *zType;
856 const char *zLogFmt;
857
dan165921a2009-08-28 18:53:45 +0000858 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000859 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000860 VdbeFrame *pFrame = p->pFrame;
861 p->pFrame = pFrame->pParent;
862 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000863 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000864 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000865 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000866 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000867 /* Instruction pc is the OP_Program that invoked the sub-program
868 ** currently being halted. If the p2 instruction of this OP_Halt
869 ** instruction is set to OE_Ignore, then the sub-program is throwing
870 ** an IGNORE exception. In this case jump to the address specified
871 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000872 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000873 }
drhbbe879d2009-11-14 18:04:35 +0000874 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000875 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000876 break;
877 }
drh92f02c32004-09-02 14:57:08 +0000878 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000879 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000880 p->pc = pc;
drhf9c8ce32013-11-05 13:33:55 +0000881 if( p->rc ){
drhd9b7ec92013-11-06 14:05:21 +0000882 if( pOp->p5 ){
883 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
884 "FOREIGN KEY" };
885 assert( pOp->p5>=1 && pOp->p5<=4 );
886 testcase( pOp->p5==1 );
887 testcase( pOp->p5==2 );
888 testcase( pOp->p5==3 );
889 testcase( pOp->p5==4 );
890 zType = azType[pOp->p5-1];
891 }else{
892 zType = 0;
893 }
drh4308e342013-11-11 16:55:52 +0000894 assert( zType!=0 || pOp->p4.z!=0 );
drhf9c8ce32013-11-05 13:33:55 +0000895 zLogFmt = "abort at %d in [%s]: %s";
896 if( zType && pOp->p4.z ){
897 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
898 zType, pOp->p4.z);
899 }else if( pOp->p4.z ){
900 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhf9c8ce32013-11-05 13:33:55 +0000901 }else{
drh4308e342013-11-11 16:55:52 +0000902 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
drhf9c8ce32013-11-05 13:33:55 +0000903 }
904 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000905 }
drh92f02c32004-09-02 14:57:08 +0000906 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000907 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000908 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000909 p->rc = rc = SQLITE_BUSY;
910 }else{
drhd91c1a12013-02-09 13:58:25 +0000911 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000912 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000913 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000914 }
drh900b31e2007-08-28 02:27:51 +0000915 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000916}
drhc61053b2000-06-04 12:58:36 +0000917
drh4c583122008-01-04 22:01:03 +0000918/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000919** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000920**
drh9cbf3422008-01-17 16:22:13 +0000921** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000922*/
drh4c583122008-01-04 22:01:03 +0000923case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000924 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000925 break;
926}
927
drh4c583122008-01-04 22:01:03 +0000928/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000929** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +0000930**
drh66a51672008-01-03 00:01:23 +0000931** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000932** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000933*/
drh4c583122008-01-04 22:01:03 +0000934case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000935 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000936 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000937 break;
938}
drh4f26d6c2004-05-26 23:25:30 +0000939
drh13573c72010-01-12 17:04:07 +0000940#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000941/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000942** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +0000943**
drh4c583122008-01-04 22:01:03 +0000944** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000945** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000946*/
drh4c583122008-01-04 22:01:03 +0000947case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
948 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000949 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000950 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000951 break;
952}
drh13573c72010-01-12 17:04:07 +0000953#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000954
drh3c84ddf2008-01-09 02:15:38 +0000955/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000956** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +0000957**
drh66a51672008-01-03 00:01:23 +0000958** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000959** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000960*/
drh4c583122008-01-04 22:01:03 +0000961case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000962 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000963 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000964 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000965
966#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000967 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000968 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
969 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000970 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000971 assert( pOut->zMalloc==pOut->z );
972 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000973 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000974 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000975 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000976 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000977 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000978 }
drh66a51672008-01-03 00:01:23 +0000979 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000980 pOp->p4.z = pOut->z;
981 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000982 }
danielk197793758c82005-01-21 08:13:14 +0000983#endif
drhbb4957f2008-03-20 14:03:29 +0000984 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000985 goto too_big;
986 }
987 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000988}
drhf4479502004-05-27 03:12:53 +0000989
drh4c583122008-01-04 22:01:03 +0000990/* Opcode: String P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000991** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +0000992**
drh9cbf3422008-01-17 16:22:13 +0000993** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000994*/
drh4c583122008-01-04 22:01:03 +0000995case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000996 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000997 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
998 pOut->z = pOp->p4.z;
999 pOut->n = pOp->p1;
1000 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001001 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +00001002 break;
1003}
1004
drh053a1282012-09-19 21:15:46 +00001005/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001006** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001007**
drhb8475df2011-12-09 16:21:19 +00001008** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001009** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001010** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001011** set to NULL.
1012**
1013** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1014** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1015** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001016*/
drh4c583122008-01-04 22:01:03 +00001017case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +00001018 int cnt;
drh053a1282012-09-19 21:15:46 +00001019 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +00001020 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001021 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001022 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +00001023 while( cnt>0 ){
1024 pOut++;
1025 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +00001026 VdbeMemRelease(pOut);
drh053a1282012-09-19 21:15:46 +00001027 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001028 cnt--;
1029 }
drhf0863fe2005-06-12 21:35:51 +00001030 break;
1031}
1032
1033
drh9de221d2008-01-05 06:51:30 +00001034/* Opcode: Blob P1 P2 * P4
drh81316f82013-10-29 20:40:47 +00001035** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001036**
drh9de221d2008-01-05 06:51:30 +00001037** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001038** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001039*/
drh4c583122008-01-04 22:01:03 +00001040case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001041 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001042 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001043 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001044 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001045 break;
1046}
1047
drheaf52d82010-05-12 13:50:23 +00001048/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001049** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001050**
drheaf52d82010-05-12 13:50:23 +00001051** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001052**
1053** If the parameter is named, then its name appears in P4 and P3==1.
1054** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001055*/
drheaf52d82010-05-12 13:50:23 +00001056case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001057 Mem *pVar; /* Value being transferred */
1058
drheaf52d82010-05-12 13:50:23 +00001059 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001060 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001061 pVar = &p->aVar[pOp->p1 - 1];
1062 if( sqlite3VdbeMemTooBig(pVar) ){
1063 goto too_big;
drh023ae032007-05-08 12:12:16 +00001064 }
drheaf52d82010-05-12 13:50:23 +00001065 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1066 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001067 break;
1068}
danielk1977295ba552004-05-19 10:34:51 +00001069
drhb21e7c72008-06-22 12:37:57 +00001070/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001071** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001072**
drhe8e4af72012-09-21 00:04:28 +00001073** Move the values in register P1..P1+P3 over into
1074** registers P2..P2+P3. Registers P1..P1+P3 are
drhb21e7c72008-06-22 12:37:57 +00001075** left holding a NULL. It is an error for register ranges
drhe8e4af72012-09-21 00:04:28 +00001076** P1..P1+P3 and P2..P2+P3 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001077*/
drhe1349cb2008-04-01 00:36:10 +00001078case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001079 char *zMalloc; /* Holding variable for allocated memory */
1080 int n; /* Number of registers left to copy */
1081 int p1; /* Register to copy from */
1082 int p2; /* Register to copy to */
1083
drhe09f43f2013-11-21 04:18:31 +00001084 n = pOp->p3;
drh856c1032009-06-02 15:21:42 +00001085 p1 = pOp->p1;
1086 p2 = pOp->p2;
drhe09f43f2013-11-21 04:18:31 +00001087 assert( n>=0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001088 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001089
drha6c2ed92009-11-14 23:22:23 +00001090 pIn1 = &aMem[p1];
1091 pOut = &aMem[p2];
drhe09f43f2013-11-21 04:18:31 +00001092 do{
dan3bc9f742013-08-15 16:18:39 +00001093 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1094 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001095 assert( memIsValid(pIn1) );
1096 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001097 zMalloc = pOut->zMalloc;
1098 pOut->zMalloc = 0;
1099 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001100#ifdef SQLITE_DEBUG
1101 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1102 pOut->pScopyFrom += p1 - pOp->p2;
1103 }
1104#endif
drhb21e7c72008-06-22 12:37:57 +00001105 pIn1->zMalloc = zMalloc;
1106 REGISTER_TRACE(p2++, pOut);
1107 pIn1++;
1108 pOut++;
drhe09f43f2013-11-21 04:18:31 +00001109 }while( n-- );
drhe1349cb2008-04-01 00:36:10 +00001110 break;
1111}
1112
drhe8e4af72012-09-21 00:04:28 +00001113/* Opcode: Copy P1 P2 P3 * *
drh4eded602013-12-20 15:59:20 +00001114** Synopsis: r[P2@P3+1]=r[P1@P3+1]
drhb1fdb2a2008-01-05 04:06:03 +00001115**
drhe8e4af72012-09-21 00:04:28 +00001116** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001117**
1118** This instruction makes a deep copy of the value. A duplicate
1119** is made of any string or blob constant. See also OP_SCopy.
1120*/
drhe8e4af72012-09-21 00:04:28 +00001121case OP_Copy: {
1122 int n;
1123
1124 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001125 pIn1 = &aMem[pOp->p1];
1126 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001127 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001128 while( 1 ){
1129 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1130 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001131#ifdef SQLITE_DEBUG
1132 pOut->pScopyFrom = 0;
1133#endif
drhe8e4af72012-09-21 00:04:28 +00001134 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1135 if( (n--)==0 ) break;
1136 pOut++;
1137 pIn1++;
1138 }
drhe1349cb2008-04-01 00:36:10 +00001139 break;
1140}
1141
drhb1fdb2a2008-01-05 04:06:03 +00001142/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001143** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001144**
drh9cbf3422008-01-17 16:22:13 +00001145** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001146**
1147** This instruction makes a shallow copy of the value. If the value
1148** is a string or blob, then the copy is only a pointer to the
1149** original and hence if the original changes so will the copy.
1150** Worse, if the original is deallocated, the copy becomes invalid.
1151** Thus the program must guarantee that the original will not change
1152** during the lifetime of the copy. Use OP_Copy to make a complete
1153** copy.
1154*/
drh26198bb2013-10-31 11:15:09 +00001155case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001156 pIn1 = &aMem[pOp->p1];
1157 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001158 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001159 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001160#ifdef SQLITE_DEBUG
1161 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1162#endif
drh5e00f6c2001-09-13 13:46:56 +00001163 break;
1164}
drh75897232000-05-29 14:26:00 +00001165
drh9cbf3422008-01-17 16:22:13 +00001166/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001167** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001168**
shane21e7feb2008-05-30 15:59:49 +00001169** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001170** results. This opcode causes the sqlite3_step() call to terminate
1171** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1172** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001173** row.
drhd4e70eb2008-01-02 00:34:36 +00001174*/
drh9cbf3422008-01-17 16:22:13 +00001175case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001176 Mem *pMem;
1177 int i;
1178 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001179 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001180 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001181
drhe6400b92013-11-13 23:48:46 +00001182#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
1183 /* Run the progress counter just before returning.
1184 */
1185 if( db->xProgress!=0
1186 && nVmStep>=nProgressLimit
1187 && db->xProgress(db->pProgressArg)!=0
1188 ){
1189 rc = SQLITE_INTERRUPT;
1190 goto vdbe_error_halt;
1191 }
1192#endif
1193
dan32b09f22009-09-23 17:29:59 +00001194 /* If this statement has violated immediate foreign key constraints, do
1195 ** not return the number of rows modified. And do not RELEASE the statement
1196 ** transaction. It needs to be rolled back. */
1197 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1198 assert( db->flags&SQLITE_CountRows );
1199 assert( p->usesStmtJournal );
1200 break;
1201 }
1202
danielk1977bd434552009-03-18 10:33:00 +00001203 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1204 ** DML statements invoke this opcode to return the number of rows
1205 ** modified to the user. This is the only way that a VM that
1206 ** opens a statement transaction may invoke this opcode.
1207 **
1208 ** In case this is such a statement, close any statement transaction
1209 ** opened by this VM before returning control to the user. This is to
1210 ** ensure that statement-transactions are always nested, not overlapping.
1211 ** If the open statement-transaction is not closed here, then the user
1212 ** may step another VM that opens its own statement transaction. This
1213 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001214 **
1215 ** The statement transaction is never a top-level transaction. Hence
1216 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001217 */
1218 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001219 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1220 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001221 break;
1222 }
1223
drhd4e70eb2008-01-02 00:34:36 +00001224 /* Invalidate all ephemeral cursor row caches */
1225 p->cacheCtr = (p->cacheCtr + 2)|1;
1226
1227 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001228 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001229 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001230 */
drha6c2ed92009-11-14 23:22:23 +00001231 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001232 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001233 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001234 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001235 assert( (pMem[i].flags & MEM_Ephem)==0
1236 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001237 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001238 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001239 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001240 }
drh28039692008-03-17 16:54:01 +00001241 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001242
1243 /* Return SQLITE_ROW
1244 */
drhd4e70eb2008-01-02 00:34:36 +00001245 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001246 rc = SQLITE_ROW;
1247 goto vdbe_return;
1248}
1249
drh5b6afba2008-01-05 16:29:28 +00001250/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001251** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001252**
drh5b6afba2008-01-05 16:29:28 +00001253** Add the text in register P1 onto the end of the text in
1254** register P2 and store the result in register P3.
1255** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001256**
1257** P3 = P2 || P1
1258**
1259** It is illegal for P1 and P3 to be the same register. Sometimes,
1260** if P3 is the same register as P2, the implementation is able
1261** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001262*/
drh5b6afba2008-01-05 16:29:28 +00001263case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001264 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001265
drh3c657212009-11-17 23:59:58 +00001266 pIn1 = &aMem[pOp->p1];
1267 pIn2 = &aMem[pOp->p2];
1268 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001269 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001270 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001271 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001272 break;
drh5e00f6c2001-09-13 13:46:56 +00001273 }
drha0c06522009-06-17 22:50:41 +00001274 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001275 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001276 Stringify(pIn2, encoding);
1277 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001278 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001279 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001280 }
danielk1977a7a8e142008-02-13 18:25:27 +00001281 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001282 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001283 goto no_mem;
1284 }
danielk1977a7a8e142008-02-13 18:25:27 +00001285 if( pOut!=pIn2 ){
1286 memcpy(pOut->z, pIn2->z, pIn2->n);
1287 }
1288 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001289 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001290 pOut->z[nByte+1] = 0;
1291 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001292 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001293 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001294 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001295 break;
1296}
drh75897232000-05-29 14:26:00 +00001297
drh3c84ddf2008-01-09 02:15:38 +00001298/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001299** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001300**
drh60a713c2008-01-21 16:22:45 +00001301** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001302** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001303** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001304*/
drh3c84ddf2008-01-09 02:15:38 +00001305/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001306** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001307**
drh3c84ddf2008-01-09 02:15:38 +00001308**
shane21e7feb2008-05-30 15:59:49 +00001309** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001310** and store the result in register P3.
1311** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001312*/
drh3c84ddf2008-01-09 02:15:38 +00001313/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001314** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001315**
drh60a713c2008-01-21 16:22:45 +00001316** Subtract the value in register P1 from the value in register P2
1317** and store the result in register P3.
1318** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001319*/
drh9cbf3422008-01-17 16:22:13 +00001320/* Opcode: Divide P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001321** Synopsis: r[P3]=r[P2]/r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001322**
drh60a713c2008-01-21 16:22:45 +00001323** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001324** and store the result in register P3 (P3=P2/P1). If the value in
1325** register P1 is zero, then the result is NULL. If either input is
1326** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001327*/
drh9cbf3422008-01-17 16:22:13 +00001328/* Opcode: Remainder P1 P2 P3 * *
drh40864a12013-11-15 18:58:37 +00001329** Synopsis: r[P3]=r[P2]%r[P1]
drhbf4133c2001-10-13 02:59:08 +00001330**
drh40864a12013-11-15 18:58:37 +00001331** Compute the remainder after integer register P2 is divided by
1332** register P1 and store the result in register P3.
1333** If the value in register P1 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001334** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001335*/
drh5b6afba2008-01-05 16:29:28 +00001336case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1337case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1338case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1339case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1340case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001341 char bIntint; /* Started out as two integer operands */
drh856c1032009-06-02 15:21:42 +00001342 int flags; /* Combined MEM_* flags from both inputs */
1343 i64 iA; /* Integer value of left operand */
1344 i64 iB; /* Integer value of right operand */
1345 double rA; /* Real value of left operand */
1346 double rB; /* Real value of right operand */
1347
drh3c657212009-11-17 23:59:58 +00001348 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001349 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001350 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001351 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001352 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001353 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001354 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1355 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001356 iA = pIn1->u.i;
1357 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001358 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001359 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001360 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1361 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1362 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001363 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001364 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001365 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001366 iB /= iA;
drh75897232000-05-29 14:26:00 +00001367 break;
1368 }
drhbf4133c2001-10-13 02:59:08 +00001369 default: {
drh856c1032009-06-02 15:21:42 +00001370 if( iA==0 ) goto arithmetic_result_is_null;
1371 if( iA==-1 ) iA = 1;
1372 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001373 break;
1374 }
drh75897232000-05-29 14:26:00 +00001375 }
drh856c1032009-06-02 15:21:42 +00001376 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001377 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001378 }else{
drhbe707b32012-12-10 22:19:14 +00001379 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001380fp_math:
drh856c1032009-06-02 15:21:42 +00001381 rA = sqlite3VdbeRealValue(pIn1);
1382 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001383 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001384 case OP_Add: rB += rA; break;
1385 case OP_Subtract: rB -= rA; break;
1386 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001387 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001388 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001389 if( rA==(double)0 ) goto arithmetic_result_is_null;
1390 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001391 break;
1392 }
drhbf4133c2001-10-13 02:59:08 +00001393 default: {
shane75ac1de2009-06-09 18:58:52 +00001394 iA = (i64)rA;
1395 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001396 if( iA==0 ) goto arithmetic_result_is_null;
1397 if( iA==-1 ) iA = 1;
1398 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001399 break;
1400 }
drh5e00f6c2001-09-13 13:46:56 +00001401 }
drhc5a7b512010-01-13 16:25:42 +00001402#ifdef SQLITE_OMIT_FLOATING_POINT
1403 pOut->u.i = rB;
1404 MemSetTypeFlag(pOut, MEM_Int);
1405#else
drh856c1032009-06-02 15:21:42 +00001406 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001407 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001408 }
drh856c1032009-06-02 15:21:42 +00001409 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001410 MemSetTypeFlag(pOut, MEM_Real);
drhbe707b32012-12-10 22:19:14 +00001411 if( (flags & MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001412 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001413 }
drhc5a7b512010-01-13 16:25:42 +00001414#endif
drh5e00f6c2001-09-13 13:46:56 +00001415 }
1416 break;
1417
drha05a7222008-01-19 03:35:58 +00001418arithmetic_result_is_null:
1419 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001420 break;
1421}
1422
drh7a957892012-02-02 17:35:43 +00001423/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001424**
drh66a51672008-01-03 00:01:23 +00001425** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001426** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1427** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001428** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001429**
drh7a957892012-02-02 17:35:43 +00001430** If P1 is not zero, then it is a register that a subsequent min() or
1431** max() aggregate will set to 1 if the current row is not the minimum or
1432** maximum. The P1 register is initialized to 0 by this instruction.
1433**
danielk1977dc1bdc42004-06-11 10:51:27 +00001434** The interface used by the implementation of the aforementioned functions
1435** to retrieve the collation sequence set by this opcode is not available
1436** publicly, only to user functions defined in func.c.
1437*/
drh9cbf3422008-01-17 16:22:13 +00001438case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001439 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001440 if( pOp->p1 ){
1441 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1442 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001443 break;
1444}
1445
drh98757152008-01-09 23:04:12 +00001446/* Opcode: Function P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001447** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001448**
drh66a51672008-01-03 00:01:23 +00001449** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001450** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001451** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001452** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001453**
drh13449892005-09-07 21:22:45 +00001454** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001455** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001456** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001457** whether meta data associated with a user function argument using the
1458** sqlite3_set_auxdata() API may be safely retained until the next
1459** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001460**
drh13449892005-09-07 21:22:45 +00001461** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001462*/
drh0bce8352002-02-28 00:41:10 +00001463case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001464 int i;
drh6810ce62004-01-31 19:22:56 +00001465 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001466 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001467 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001468 int n;
drh1350b032002-02-27 19:00:20 +00001469
drh856c1032009-06-02 15:21:42 +00001470 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001471 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001472 assert( apVal || n==0 );
dan3bc9f742013-08-15 16:18:39 +00001473 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drhebc16712010-09-28 00:25:58 +00001474 pOut = &aMem[pOp->p3];
1475 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001476
dan3bc9f742013-08-15 16:18:39 +00001477 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001478 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001479 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001480 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001481 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001482 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001483 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001484 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001485 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001486 }
danielk197751ad0ec2004-05-24 12:39:02 +00001487
dan0c547792013-07-18 17:12:08 +00001488 assert( pOp->p4type==P4_FUNCDEF );
1489 ctx.pFunc = pOp->p4.pFunc;
dan0c547792013-07-18 17:12:08 +00001490 ctx.iOp = pc;
1491 ctx.pVdbe = p;
danielk1977a7a8e142008-02-13 18:25:27 +00001492
1493 /* The output cell may already have a buffer allocated. Move
1494 ** the pointer to ctx.s so in case the user-function can use
1495 ** the already allocated buffer instead of allocating a new one.
1496 */
drh76694c32013-11-21 03:43:12 +00001497 memcpy(&ctx.s, pOut, sizeof(Mem));
1498 pOut->flags = MEM_Null;
1499 pOut->xDel = 0;
1500 pOut->zMalloc = 0;
danielk1977a7a8e142008-02-13 18:25:27 +00001501 MemSetTypeFlag(&ctx.s, MEM_Null);
1502
drh9b47ee32013-08-20 03:13:51 +00001503 ctx.fErrorOrAux = 0;
drhd36e1042013-09-06 13:10:12 +00001504 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001505 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001506 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001507 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001508 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001509 }
drh99a66922011-05-13 18:51:42 +00001510 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001511 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001512 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001513
dan5f84e142011-06-14 14:18:45 +00001514 if( db->mallocFailed ){
1515 /* Even though a malloc() has failed, the implementation of the
1516 ** user function may have called an sqlite3_result_XXX() function
1517 ** to return a value. The following call releases any resources
1518 ** associated with such a value.
1519 */
1520 sqlite3VdbeMemRelease(&ctx.s);
1521 goto no_mem;
1522 }
1523
drh90669c12006-01-20 15:45:36 +00001524 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001525 if( ctx.fErrorOrAux ){
1526 if( ctx.isError ){
1527 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1528 rc = ctx.isError;
1529 }
1530 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001531 }
1532
drh9cbf3422008-01-17 16:22:13 +00001533 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001534 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drhe09f43f2013-11-21 04:18:31 +00001535 assert( pOut->flags==MEM_Null );
1536 memcpy(pOut, &ctx.s, sizeof(Mem));
drh98757152008-01-09 23:04:12 +00001537 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001538 goto too_big;
1539 }
drh7b94e7f2011-04-04 12:29:20 +00001540
1541#if 0
1542 /* The app-defined function has done something that as caused this
1543 ** statement to expire. (Perhaps the function called sqlite3_exec()
1544 ** with a CREATE TABLE statement.)
1545 */
1546 if( p->expired ) rc = SQLITE_ABORT;
1547#endif
1548
drh2dcef112008-01-12 19:03:48 +00001549 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001550 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001551 break;
1552}
1553
drh98757152008-01-09 23:04:12 +00001554/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001555** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001556**
drh98757152008-01-09 23:04:12 +00001557** Take the bit-wise AND of the values in register P1 and P2 and
1558** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001559** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001560*/
drh98757152008-01-09 23:04:12 +00001561/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001562** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001563**
drh98757152008-01-09 23:04:12 +00001564** Take the bit-wise OR of the values in register P1 and P2 and
1565** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001566** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001567*/
drh98757152008-01-09 23:04:12 +00001568/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001569** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001570**
drh98757152008-01-09 23:04:12 +00001571** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001572** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001573** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001574** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001575*/
drh98757152008-01-09 23:04:12 +00001576/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001577** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001578**
drh98757152008-01-09 23:04:12 +00001579** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001580** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001581** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001582** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001583*/
drh5b6afba2008-01-05 16:29:28 +00001584case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1585case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1586case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1587case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001588 i64 iA;
1589 u64 uA;
1590 i64 iB;
1591 u8 op;
drh6810ce62004-01-31 19:22:56 +00001592
drh3c657212009-11-17 23:59:58 +00001593 pIn1 = &aMem[pOp->p1];
1594 pIn2 = &aMem[pOp->p2];
1595 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001596 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001597 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001598 break;
1599 }
drh158b9cb2011-03-05 20:59:46 +00001600 iA = sqlite3VdbeIntValue(pIn2);
1601 iB = sqlite3VdbeIntValue(pIn1);
1602 op = pOp->opcode;
1603 if( op==OP_BitAnd ){
1604 iA &= iB;
1605 }else if( op==OP_BitOr ){
1606 iA |= iB;
1607 }else if( iB!=0 ){
1608 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1609
1610 /* If shifting by a negative amount, shift in the other direction */
1611 if( iB<0 ){
1612 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1613 op = 2*OP_ShiftLeft + 1 - op;
1614 iB = iB>(-64) ? -iB : 64;
1615 }
1616
1617 if( iB>=64 ){
1618 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1619 }else{
1620 memcpy(&uA, &iA, sizeof(uA));
1621 if( op==OP_ShiftLeft ){
1622 uA <<= iB;
1623 }else{
1624 uA >>= iB;
1625 /* Sign-extend on a right shift of a negative number */
1626 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1627 }
1628 memcpy(&iA, &uA, sizeof(iA));
1629 }
drhbf4133c2001-10-13 02:59:08 +00001630 }
drh158b9cb2011-03-05 20:59:46 +00001631 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001632 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001633 break;
1634}
1635
drh8558cde2008-01-05 05:20:10 +00001636/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001637** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001638**
danielk19770cdc0222008-06-26 18:04:03 +00001639** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001640** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001641**
drh8558cde2008-01-05 05:20:10 +00001642** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001643*/
drh9cbf3422008-01-17 16:22:13 +00001644case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001645 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001646 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001647 sqlite3VdbeMemIntegerify(pIn1);
1648 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001649 break;
1650}
1651
drh9cbf3422008-01-17 16:22:13 +00001652/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001653**
drh9cbf3422008-01-17 16:22:13 +00001654** Force the value in register P1 to be an integer. If the value
1655** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001656** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001657** raise an SQLITE_MISMATCH exception.
1658*/
drh9cbf3422008-01-17 16:22:13 +00001659case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001660 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001661 if( (pIn1->flags & MEM_Int)==0 ){
drh83b301b2013-11-20 00:59:02 +00001662 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1663 if( (pIn1->flags & MEM_Int)==0 ){
1664 if( pOp->p2==0 ){
1665 rc = SQLITE_MISMATCH;
1666 goto abort_due_to_error;
1667 }else{
1668 pc = pOp->p2 - 1;
1669 break;
1670 }
drh8aff1012001-12-22 14:49:24 +00001671 }
drh8aff1012001-12-22 14:49:24 +00001672 }
drh83b301b2013-11-20 00:59:02 +00001673 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001674 break;
1675}
1676
drh13573c72010-01-12 17:04:07 +00001677#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001678/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001679**
drh2133d822008-01-03 18:44:59 +00001680** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001681**
drh8a512562005-11-14 22:29:05 +00001682** This opcode is used when extracting information from a column that
1683** has REAL affinity. Such column values may still be stored as
1684** integers, for space efficiency, but after extraction we want them
1685** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001686*/
drh9cbf3422008-01-17 16:22:13 +00001687case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001688 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001689 if( pIn1->flags & MEM_Int ){
1690 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001691 }
drh487e2622005-06-25 18:42:14 +00001692 break;
1693}
drh13573c72010-01-12 17:04:07 +00001694#endif
drh487e2622005-06-25 18:42:14 +00001695
drh8df447f2005-11-01 15:48:24 +00001696#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001697/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001698**
drh8558cde2008-01-05 05:20:10 +00001699** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001700** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001701** equivalent of printf(). Blob values are unchanged and
1702** are afterwards simply interpreted as text.
1703**
1704** A NULL value is not changed by this routine. It remains NULL.
1705*/
drh9cbf3422008-01-17 16:22:13 +00001706case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001707 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001708 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001709 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001710 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001711 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1712 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1713 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001714 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001715 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001716 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001717 break;
1718}
1719
drh8558cde2008-01-05 05:20:10 +00001720/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001721**
drh8558cde2008-01-05 05:20:10 +00001722** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001723** If the value is numeric, convert it to a string first.
1724** Strings are simply reinterpreted as blobs with no change
1725** to the underlying data.
1726**
1727** A NULL value is not changed by this routine. It remains NULL.
1728*/
drh9cbf3422008-01-17 16:22:13 +00001729case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001730 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001731 if( pIn1->flags & MEM_Null ) break;
1732 if( (pIn1->flags & MEM_Blob)==0 ){
1733 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001734 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001735 MemSetTypeFlag(pIn1, MEM_Blob);
1736 }else{
1737 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001738 }
drhb7654112008-01-12 12:48:07 +00001739 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001740 break;
1741}
drh8a512562005-11-14 22:29:05 +00001742
drh8558cde2008-01-05 05:20:10 +00001743/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001744**
drh8558cde2008-01-05 05:20:10 +00001745** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001746** integer or a floating-point number.)
1747** If the value is text or blob, try to convert it to an using the
1748** equivalent of atoi() or atof() and store 0 if no such conversion
1749** is possible.
1750**
1751** A NULL value is not changed by this routine. It remains NULL.
1752*/
drh9cbf3422008-01-17 16:22:13 +00001753case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001754 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001755 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001756 break;
1757}
1758#endif /* SQLITE_OMIT_CAST */
1759
drh8558cde2008-01-05 05:20:10 +00001760/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001761**
drh710c4842010-08-30 01:17:20 +00001762** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001763** The value is currently a real number, drop its fractional part.
1764** If the value is text or blob, try to convert it to an integer using the
1765** equivalent of atoi() and store 0 if no such conversion is possible.
1766**
1767** A NULL value is not changed by this routine. It remains NULL.
1768*/
drh9cbf3422008-01-17 16:22:13 +00001769case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001770 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001771 if( (pIn1->flags & MEM_Null)==0 ){
1772 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001773 }
1774 break;
1775}
1776
drh13573c72010-01-12 17:04:07 +00001777#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001778/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001779**
drh8558cde2008-01-05 05:20:10 +00001780** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001781** If The value is currently an integer, convert it.
1782** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001783** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001784**
1785** A NULL value is not changed by this routine. It remains NULL.
1786*/
drh9cbf3422008-01-17 16:22:13 +00001787case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001788 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001789 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001790 if( (pIn1->flags & MEM_Null)==0 ){
1791 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001792 }
1793 break;
1794}
drh13573c72010-01-12 17:04:07 +00001795#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001796
drh35573352008-01-08 23:54:25 +00001797/* Opcode: Lt P1 P2 P3 P4 P5
drh72dbffd2013-11-15 03:21:43 +00001798** Synopsis: if r[P1]<r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001799**
drh35573352008-01-08 23:54:25 +00001800** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1801** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001802**
drh35573352008-01-08 23:54:25 +00001803** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1804** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001805** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001806**
drh35573352008-01-08 23:54:25 +00001807** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001808** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001809** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001810** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001811** affinity is used. Note that the affinity conversions are stored
1812** back into the input registers P1 and P3. So this opcode can cause
1813** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001814**
1815** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001816** the values are compared. If both values are blobs then memcmp() is
1817** used to determine the results of the comparison. If both values
1818** are text, then the appropriate collating function specified in
1819** P4 is used to do the comparison. If P4 is not specified then
1820** memcmp() is used to compare text string. If both values are
1821** numeric, then a numeric comparison is used. If the two values
1822** are of different types, then numbers are considered less than
1823** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001824**
drh35573352008-01-08 23:54:25 +00001825** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1826** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001827**
1828** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1829** equal to one another, provided that they do not have their MEM_Cleared
1830** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001831*/
drh9cbf3422008-01-17 16:22:13 +00001832/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001833** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001834**
drh35573352008-01-08 23:54:25 +00001835** This works just like the Lt opcode except that the jump is taken if
1836** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001837** additional information.
drh6a2fe092009-09-23 02:29:36 +00001838**
1839** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1840** true or false and is never NULL. If both operands are NULL then the result
1841** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001842** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001843** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001844*/
drh9cbf3422008-01-17 16:22:13 +00001845/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001846** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001847**
drh35573352008-01-08 23:54:25 +00001848** This works just like the Lt opcode except that the jump is taken if
1849** the operands in registers P1 and P3 are equal.
1850** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001851**
1852** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1853** true or false and is never NULL. If both operands are NULL then the result
1854** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001855** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001856** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001857*/
drh9cbf3422008-01-17 16:22:13 +00001858/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001859** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001860**
drh35573352008-01-08 23:54:25 +00001861** This works just like the Lt opcode except that the jump is taken if
1862** the content of register P3 is less than or equal to the content of
1863** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001864*/
drh9cbf3422008-01-17 16:22:13 +00001865/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001866** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001867**
drh35573352008-01-08 23:54:25 +00001868** This works just like the Lt opcode except that the jump is taken if
1869** the content of register P3 is greater than the content of
1870** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001871*/
drh9cbf3422008-01-17 16:22:13 +00001872/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001873** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001874**
drh35573352008-01-08 23:54:25 +00001875** This works just like the Lt opcode except that the jump is taken if
1876** the content of register P3 is greater than or equal to the content of
1877** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001878*/
drh9cbf3422008-01-17 16:22:13 +00001879case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1880case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1881case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1882case OP_Le: /* same as TK_LE, jump, in1, in3 */
1883case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1884case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001885 int res; /* Result of the comparison of pIn1 against pIn3 */
1886 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001887 u16 flags1; /* Copy of initial value of pIn1->flags */
1888 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001889
drh3c657212009-11-17 23:59:58 +00001890 pIn1 = &aMem[pOp->p1];
1891 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001892 flags1 = pIn1->flags;
1893 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001894 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001895 /* One or both operands are NULL */
1896 if( pOp->p5 & SQLITE_NULLEQ ){
1897 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1898 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1899 ** or not both operands are null.
1900 */
1901 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001902 assert( (flags1 & MEM_Cleared)==0 );
1903 if( (flags1&MEM_Null)!=0
1904 && (flags3&MEM_Null)!=0
1905 && (flags3&MEM_Cleared)==0
1906 ){
1907 res = 0; /* Results are equal */
1908 }else{
1909 res = 1; /* Results are not equal */
1910 }
drh6a2fe092009-09-23 02:29:36 +00001911 }else{
1912 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1913 ** then the result is always NULL.
1914 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1915 */
drh9b47ee32013-08-20 03:13:51 +00001916 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1917 pc = pOp->p2-1;
1918 }else if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001919 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001920 MemSetTypeFlag(pOut, MEM_Null);
1921 REGISTER_TRACE(pOp->p2, pOut);
drh6a2fe092009-09-23 02:29:36 +00001922 }
1923 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001924 }
drh6a2fe092009-09-23 02:29:36 +00001925 }else{
1926 /* Neither operand is NULL. Do a comparison. */
1927 affinity = pOp->p5 & SQLITE_AFF_MASK;
1928 if( affinity ){
1929 applyAffinity(pIn1, affinity, encoding);
1930 applyAffinity(pIn3, affinity, encoding);
1931 if( db->mallocFailed ) goto no_mem;
1932 }
danielk1977a37cdde2004-05-16 11:15:36 +00001933
drh6a2fe092009-09-23 02:29:36 +00001934 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1935 ExpandBlob(pIn1);
1936 ExpandBlob(pIn3);
1937 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001938 }
danielk1977a37cdde2004-05-16 11:15:36 +00001939 switch( pOp->opcode ){
1940 case OP_Eq: res = res==0; break;
1941 case OP_Ne: res = res!=0; break;
1942 case OP_Lt: res = res<0; break;
1943 case OP_Le: res = res<=0; break;
1944 case OP_Gt: res = res>0; break;
1945 default: res = res>=0; break;
1946 }
1947
drh35573352008-01-08 23:54:25 +00001948 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001949 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001950 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001951 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001952 pOut->u.i = res;
1953 REGISTER_TRACE(pOp->p2, pOut);
1954 }else if( res ){
1955 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001956 }
danb7dca7d2010-03-05 16:32:12 +00001957
1958 /* Undo any changes made by applyAffinity() to the input registers. */
1959 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1960 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001961 break;
1962}
drhc9b84a12002-06-20 11:36:48 +00001963
drh0acb7e42008-06-25 00:12:41 +00001964/* Opcode: Permutation * * * P4 *
1965**
shanebe217792009-03-05 04:20:31 +00001966** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001967** of integers in P4.
1968**
drh953f7612012-12-07 22:18:54 +00001969** The permutation is only valid until the next OP_Compare that has
1970** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1971** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001972*/
1973case OP_Permutation: {
1974 assert( pOp->p4type==P4_INTARRAY );
1975 assert( pOp->p4.ai );
1976 aPermute = pOp->p4.ai;
1977 break;
1978}
1979
drh953f7612012-12-07 22:18:54 +00001980/* Opcode: Compare P1 P2 P3 P4 P5
drh16ee60f2008-06-20 18:13:25 +00001981**
drh710c4842010-08-30 01:17:20 +00001982** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1983** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001984** the comparison for use by the next OP_Jump instruct.
1985**
drh0ca10df2012-12-08 13:26:23 +00001986** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1987** determined by the most recent OP_Permutation operator. If the
1988** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1989** order.
1990**
drh0acb7e42008-06-25 00:12:41 +00001991** P4 is a KeyInfo structure that defines collating sequences and sort
1992** orders for the comparison. The permutation applies to registers
1993** only. The KeyInfo elements are used sequentially.
1994**
1995** The comparison is a sort comparison, so NULLs compare equal,
1996** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001997** and strings are less than blobs.
1998*/
1999case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00002000 int n;
2001 int i;
2002 int p1;
2003 int p2;
2004 const KeyInfo *pKeyInfo;
2005 int idx;
2006 CollSeq *pColl; /* Collating sequence to use on this term */
2007 int bRev; /* True for DESCENDING sort order */
2008
drh953f7612012-12-07 22:18:54 +00002009 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00002010 n = pOp->p3;
2011 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00002012 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00002013 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00002014 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00002015 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002016#if SQLITE_DEBUG
2017 if( aPermute ){
2018 int k, mx = 0;
2019 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002020 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2021 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002022 }else{
dan3bc9f742013-08-15 16:18:39 +00002023 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2024 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002025 }
2026#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002027 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002028 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002029 assert( memIsValid(&aMem[p1+idx]) );
2030 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002031 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2032 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002033 assert( i<pKeyInfo->nField );
2034 pColl = pKeyInfo->aColl[i];
2035 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002036 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002037 if( iCompare ){
2038 if( bRev ) iCompare = -iCompare;
2039 break;
2040 }
drh16ee60f2008-06-20 18:13:25 +00002041 }
drh0acb7e42008-06-25 00:12:41 +00002042 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002043 break;
2044}
2045
2046/* Opcode: Jump P1 P2 P3 * *
2047**
2048** Jump to the instruction at address P1, P2, or P3 depending on whether
2049** in the most recent OP_Compare instruction the P1 vector was less than
2050** equal to, or greater than the P2 vector, respectively.
2051*/
drh0acb7e42008-06-25 00:12:41 +00002052case OP_Jump: { /* jump */
2053 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00002054 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00002055 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00002056 pc = pOp->p2 - 1;
2057 }else{
2058 pc = pOp->p3 - 1;
2059 }
2060 break;
2061}
2062
drh5b6afba2008-01-05 16:29:28 +00002063/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002064** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002065**
drh5b6afba2008-01-05 16:29:28 +00002066** Take the logical AND of the values in registers P1 and P2 and
2067** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002068**
drh5b6afba2008-01-05 16:29:28 +00002069** If either P1 or P2 is 0 (false) then the result is 0 even if
2070** the other input is NULL. A NULL and true or two NULLs give
2071** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002072*/
drh5b6afba2008-01-05 16:29:28 +00002073/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002074** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002075**
2076** Take the logical OR of the values in register P1 and P2 and
2077** store the answer in register P3.
2078**
2079** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2080** even if the other input is NULL. A NULL and false or two NULLs
2081** give a NULL output.
2082*/
2083case OP_And: /* same as TK_AND, in1, in2, out3 */
2084case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002085 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2086 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002087
drh3c657212009-11-17 23:59:58 +00002088 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002089 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002090 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002091 }else{
drh5b6afba2008-01-05 16:29:28 +00002092 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002093 }
drh3c657212009-11-17 23:59:58 +00002094 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002095 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002096 v2 = 2;
2097 }else{
drh5b6afba2008-01-05 16:29:28 +00002098 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002099 }
2100 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002101 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002102 v1 = and_logic[v1*3+v2];
2103 }else{
drh5b6afba2008-01-05 16:29:28 +00002104 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002105 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002106 }
drh3c657212009-11-17 23:59:58 +00002107 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002108 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002109 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002110 }else{
drh5b6afba2008-01-05 16:29:28 +00002111 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002112 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002113 }
drh5e00f6c2001-09-13 13:46:56 +00002114 break;
2115}
2116
drhe99fa2a2008-12-15 15:27:51 +00002117/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002118** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002119**
drhe99fa2a2008-12-15 15:27:51 +00002120** Interpret the value in register P1 as a boolean value. Store the
2121** boolean complement in register P2. If the value in register P1 is
2122** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002123*/
drh93952eb2009-11-13 19:43:43 +00002124case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002125 pIn1 = &aMem[pOp->p1];
2126 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002127 if( pIn1->flags & MEM_Null ){
2128 sqlite3VdbeMemSetNull(pOut);
2129 }else{
2130 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2131 }
drh5e00f6c2001-09-13 13:46:56 +00002132 break;
2133}
2134
drhe99fa2a2008-12-15 15:27:51 +00002135/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002136** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002137**
drhe99fa2a2008-12-15 15:27:51 +00002138** Interpret the content of register P1 as an integer. Store the
2139** ones-complement of the P1 value into register P2. If P1 holds
2140** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002141*/
drh93952eb2009-11-13 19:43:43 +00002142case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002143 pIn1 = &aMem[pOp->p1];
2144 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002145 if( pIn1->flags & MEM_Null ){
2146 sqlite3VdbeMemSetNull(pOut);
2147 }else{
2148 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2149 }
drhbf4133c2001-10-13 02:59:08 +00002150 break;
2151}
2152
drh48f2d3b2011-09-16 01:34:43 +00002153/* Opcode: Once P1 P2 * * *
2154**
dan1d8cb212011-12-09 13:24:16 +00002155** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
2156** set the flag and fall through to the next instruction.
drh48f2d3b2011-09-16 01:34:43 +00002157*/
dan1d8cb212011-12-09 13:24:16 +00002158case OP_Once: { /* jump */
2159 assert( pOp->p1<p->nOnceFlag );
2160 if( p->aOnceFlag[pOp->p1] ){
2161 pc = pOp->p2-1;
2162 }else{
2163 p->aOnceFlag[pOp->p1] = 1;
2164 }
2165 break;
2166}
2167
drh3c84ddf2008-01-09 02:15:38 +00002168/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002169**
drhef8662b2011-06-20 21:47:58 +00002170** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002171** is considered true if it is numeric and non-zero. If the value
drhb8475df2011-12-09 16:21:19 +00002172** in P1 is NULL then take the jump if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002173*/
drh3c84ddf2008-01-09 02:15:38 +00002174/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002175**
drhef8662b2011-06-20 21:47:58 +00002176** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002177** is considered false if it has a numeric value of zero. If the value
2178** in P1 is NULL then take the jump if P3 is zero.
drhf5905aa2002-05-26 20:54:33 +00002179*/
drh9cbf3422008-01-17 16:22:13 +00002180case OP_If: /* jump, in1 */
2181case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002182 int c;
drh3c657212009-11-17 23:59:58 +00002183 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002184 if( pIn1->flags & MEM_Null ){
2185 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002186 }else{
drhba0232a2005-06-06 17:27:19 +00002187#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002188 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002189#else
drh3c84ddf2008-01-09 02:15:38 +00002190 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002191#endif
drhf5905aa2002-05-26 20:54:33 +00002192 if( pOp->opcode==OP_IfNot ) c = !c;
2193 }
drh3c84ddf2008-01-09 02:15:38 +00002194 if( c ){
2195 pc = pOp->p2-1;
2196 }
drh5e00f6c2001-09-13 13:46:56 +00002197 break;
2198}
2199
drh830ecf92009-06-18 00:41:55 +00002200/* Opcode: IsNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002201** Synopsis: if r[P1]==NULL goto P2
drh477df4b2008-01-05 18:48:24 +00002202**
drh830ecf92009-06-18 00:41:55 +00002203** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002204*/
drh9cbf3422008-01-17 16:22:13 +00002205case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002206 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002207 if( (pIn1->flags & MEM_Null)!=0 ){
2208 pc = pOp->p2 - 1;
2209 }
drh477df4b2008-01-05 18:48:24 +00002210 break;
2211}
2212
drh98757152008-01-09 23:04:12 +00002213/* Opcode: NotNull P1 P2 * * *
drhfc8d4f92013-11-08 15:19:46 +00002214** Synopsis: if r[P1]!=NULL goto P2
drh5e00f6c2001-09-13 13:46:56 +00002215**
drh6a288a32008-01-07 19:20:24 +00002216** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002217*/
drh9cbf3422008-01-17 16:22:13 +00002218case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002219 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002220 if( (pIn1->flags & MEM_Null)==0 ){
2221 pc = pOp->p2 - 1;
2222 }
drh5e00f6c2001-09-13 13:46:56 +00002223 break;
2224}
2225
drh3e9ca092009-09-08 01:14:48 +00002226/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002227** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002228**
danielk1977cfcdaef2004-05-12 07:33:33 +00002229** Interpret the data that cursor P1 points to as a structure built using
2230** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002231** information about the format of the data.) Extract the P2-th column
2232** from this record. If there are less that (P2+1)
2233** values in the record, extract a NULL.
2234**
drh9cbf3422008-01-17 16:22:13 +00002235** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002236**
danielk19771f4aa332008-01-03 09:51:55 +00002237** If the column contains fewer than P2 fields, then extract a NULL. Or,
2238** if the P4 argument is a P4_MEM use the value of the P4 argument as
2239** the result.
drh3e9ca092009-09-08 01:14:48 +00002240**
2241** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2242** then the cache of the cursor is reset prior to extracting the column.
2243** The first OP_Column against a pseudo-table after the value of the content
2244** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002245**
drhdda5c082012-03-28 13:41:10 +00002246** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2247** the result is guaranteed to only be used as the argument of a length()
2248** or typeof() function, respectively. The loading of large blobs can be
2249** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002250*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002251case OP_Column: {
drh856c1032009-06-02 15:21:42 +00002252 i64 payloadSize64; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002253 int p2; /* column number to retrieve */
2254 VdbeCursor *pC; /* The VDBE cursor */
drhd3194f52004-05-27 19:59:32 +00002255 BtCursor *pCrsr; /* The BTree cursor */
2256 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2257 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk1977cfcdaef2004-05-12 07:33:33 +00002258 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002259 int i; /* Loop counter */
drhd4e70eb2008-01-02 00:34:36 +00002260 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002261 Mem sMem; /* For storing the record being decoded */
drh399af1d2013-11-20 17:25:55 +00002262 const u8 *zData; /* Part of the record being decoded */
2263 const u8 *zHdr; /* Next unparsed byte of the header */
2264 const u8 *zEndHdr; /* Pointer to first byte after the header */
drh35cd6432009-06-05 14:17:21 +00002265 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002266 u32 szField; /* Number of bytes in the content of a field */
drh501932c2013-11-21 21:59:53 +00002267 u32 avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002268 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002269 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002270
drh399af1d2013-11-20 17:25:55 +00002271 p2 = pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00002272 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002273 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002274 memAboutToChange(p, pDest);
drhc8606e42013-11-20 19:28:03 +00002275 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
2276 pC = p->apCsr[pOp->p1];
drha5759672012-10-30 14:39:12 +00002277 assert( pC!=0 );
drhc8606e42013-11-20 19:28:03 +00002278 assert( p2<pC->nField );
drh399af1d2013-11-20 17:25:55 +00002279 aType = pC->aType;
drh14da87f2013-11-20 21:51:33 +00002280 aOffset = aType + pC->nField;
danielk19770817d0d2007-02-14 09:19:36 +00002281#ifndef SQLITE_OMIT_VIRTUALTABLE
drh380d6852013-11-20 20:58:00 +00002282 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
danielk19770817d0d2007-02-14 09:19:36 +00002283#endif
shane36840fd2009-06-26 16:32:13 +00002284 pCrsr = pC->pCursor;
drh380d6852013-11-20 20:58:00 +00002285 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
2286 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */
drh399af1d2013-11-20 17:25:55 +00002287
2288 /* If the cursor cache is stale, bring it up-to-date */
2289 rc = sqlite3VdbeCursorMoveto(pC);
2290 if( rc ) goto abort_due_to_error;
2291 if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
drhc8606e42013-11-20 19:28:03 +00002292 if( pC->nullRow ){
2293 if( pCrsr==0 ){
2294 assert( pC->pseudoTableReg>0 );
2295 pReg = &aMem[pC->pseudoTableReg];
2296 if( pC->multiPseudo ){
2297 sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
2298 Deephemeralize(pDest);
2299 goto op_column_out;
2300 }
2301 assert( pReg->flags & MEM_Blob );
2302 assert( memIsValid(pReg) );
2303 pC->payloadSize = pC->szRow = avail = pReg->n;
2304 pC->aRow = (u8*)pReg->z;
2305 }else{
2306 MemSetTypeFlag(pDest, MEM_Null);
drh399af1d2013-11-20 17:25:55 +00002307 goto op_column_out;
2308 }
danielk197784ac9d02004-05-18 09:58:06 +00002309 }else{
drhc8606e42013-11-20 19:28:03 +00002310 assert( pCrsr );
drh14da87f2013-11-20 21:51:33 +00002311 if( pC->isTable==0 ){
drh399af1d2013-11-20 17:25:55 +00002312 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2313 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
2314 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
2315 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2316 ** payload size, so it is impossible for payloadSize64 to be
2317 ** larger than 32 bits. */
2318 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
2319 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
2320 pC->payloadSize = (u32)payloadSize64;
drhd3194f52004-05-27 19:59:32 +00002321 }else{
drh399af1d2013-11-20 17:25:55 +00002322 assert( sqlite3BtreeCursorIsValid(pCrsr) );
2323 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
2324 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
2325 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002326 }
drh399af1d2013-11-20 17:25:55 +00002327 assert( avail<=65536 ); /* Maximum page size is 64KiB */
2328 if( pC->payloadSize <= (u32)avail ){
2329 pC->szRow = pC->payloadSize;
drhe61cffc2004-06-12 18:12:15 +00002330 }else{
drh399af1d2013-11-20 17:25:55 +00002331 pC->szRow = avail;
2332 }
2333 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
2334 goto too_big;
drhe61cffc2004-06-12 18:12:15 +00002335 }
drhd3194f52004-05-27 19:59:32 +00002336 }
drh399af1d2013-11-20 17:25:55 +00002337 pC->cacheStatus = p->cacheCtr;
2338 pC->iHdrOffset = getVarint32(pC->aRow, offset);
2339 pC->nHdrParsed = 0;
2340 aOffset[0] = offset;
2341 if( avail<offset ){
drh380d6852013-11-20 20:58:00 +00002342 /* pC->aRow does not have to hold the entire row, but it does at least
2343 ** need to cover the header of the record. If pC->aRow does not contain
2344 ** the complete header, then set it to zero, forcing the header to be
2345 ** dynamically allocated. */
drh399af1d2013-11-20 17:25:55 +00002346 pC->aRow = 0;
2347 pC->szRow = 0;
2348 }
drh35cd6432009-06-05 14:17:21 +00002349
2350 /* Make sure a corrupt database has not given us an oversize header.
2351 ** Do this now to avoid an oversize memory allocation.
2352 **
2353 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2354 ** types use so much data space that there can only be 4096 and 32 of
2355 ** them, respectively. So the maximum header length results from a
2356 ** 3-byte type for each of the maximum of 32768 columns plus three
2357 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2358 */
drh399af1d2013-11-20 17:25:55 +00002359 if( offset > 98307 || offset > pC->payloadSize ){
drh35cd6432009-06-05 14:17:21 +00002360 rc = SQLITE_CORRUPT_BKPT;
drhc8606e42013-11-20 19:28:03 +00002361 goto op_column_error;
drh35cd6432009-06-05 14:17:21 +00002362 }
drh399af1d2013-11-20 17:25:55 +00002363 }
drh35cd6432009-06-05 14:17:21 +00002364
drh399af1d2013-11-20 17:25:55 +00002365 /* Make sure at least the first p2+1 entries of the header have been
2366 ** parsed and valid information is in aOffset[] and aType[].
2367 */
drhc8606e42013-11-20 19:28:03 +00002368 if( pC->nHdrParsed<=p2 ){
drh380d6852013-11-20 20:58:00 +00002369 /* If there is more header available for parsing in the record, try
2370 ** to extract additional fields up through the p2+1-th field
drhd3194f52004-05-27 19:59:32 +00002371 */
drhc8606e42013-11-20 19:28:03 +00002372 if( pC->iHdrOffset<aOffset[0] ){
2373 /* Make sure zData points to enough of the record to cover the header. */
2374 if( pC->aRow==0 ){
2375 memset(&sMem, 0, sizeof(sMem));
drh14da87f2013-11-20 21:51:33 +00002376 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
2377 !pC->isTable, &sMem);
drhc8606e42013-11-20 19:28:03 +00002378 if( rc!=SQLITE_OK ){
2379 goto op_column_error;
2380 }
2381 zData = (u8*)sMem.z;
2382 }else{
2383 zData = pC->aRow;
2384 }
2385
2386 /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
2387 i = pC->nHdrParsed;
2388 offset = aOffset[i];
2389 zHdr = zData + pC->iHdrOffset;
2390 zEndHdr = zData + aOffset[0];
2391 assert( i<=p2 && zHdr<zEndHdr );
2392 do{
2393 if( zHdr[0]<0x80 ){
2394 t = zHdr[0];
2395 zHdr++;
2396 }else{
2397 zHdr += sqlite3GetVarint32(zHdr, &t);
2398 }
2399 aType[i] = t;
2400 szField = sqlite3VdbeSerialTypeLen(t);
2401 offset += szField;
2402 if( offset<szField ){ /* True if offset overflows */
2403 zHdr = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2404 break;
2405 }
2406 i++;
2407 aOffset[i] = offset;
2408 }while( i<=p2 && zHdr<zEndHdr );
2409 pC->nHdrParsed = i;
2410 pC->iHdrOffset = (u32)(zHdr - zData);
2411 if( pC->aRow==0 ){
2412 sqlite3VdbeMemRelease(&sMem);
2413 sMem.flags = MEM_Null;
2414 }
2415
2416 /* If we have read more header data than was contained in the header,
2417 ** or if the end of the last field appears to be past the end of the
2418 ** record, or if the end of the last field appears to be before the end
2419 ** of the record (when all fields present), then we must be dealing
2420 ** with a corrupt database.
2421 */
2422 if( (zHdr > zEndHdr)
2423 || (offset > pC->payloadSize)
2424 || (zHdr==zEndHdr && offset!=pC->payloadSize)
2425 ){
2426 rc = SQLITE_CORRUPT_BKPT;
2427 goto op_column_error;
2428 }
2429 }
2430
drh380d6852013-11-20 20:58:00 +00002431 /* If after trying to extra new entries from the header, nHdrParsed is
2432 ** still not up to p2, that means that the record has fewer than p2
2433 ** columns. So the result will be either the default value or a NULL.
2434 */
drhc8606e42013-11-20 19:28:03 +00002435 if( pC->nHdrParsed<=p2 ){
2436 if( pOp->p4type==P4_MEM ){
2437 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
2438 }else{
2439 MemSetTypeFlag(pDest, MEM_Null);
2440 }
danielk19773c9cc8d2005-01-17 03:40:08 +00002441 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002442 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002443 }
danielk1977192ac1d2004-05-10 07:17:30 +00002444
drh380d6852013-11-20 20:58:00 +00002445 /* Extract the content for the p2+1-th column. Control can only
2446 ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
2447 ** all valid.
drh9188b382004-05-14 21:12:22 +00002448 */
drhc8606e42013-11-20 19:28:03 +00002449 assert( p2<pC->nHdrParsed );
2450 assert( rc==SQLITE_OK );
2451 if( pC->szRow>=aOffset[p2+1] ){
drh380d6852013-11-20 20:58:00 +00002452 /* This is the common case where the desired content fits on the original
2453 ** page - where the content is not on an overflow page */
drhc8606e42013-11-20 19:28:03 +00002454 VdbeMemRelease(pDest);
2455 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002456 }else{
drh58c96082013-12-23 11:33:32 +00002457 /* This branch happens only when content is on overflow pages */
drhc8606e42013-11-20 19:28:03 +00002458 t = aType[p2];
drh380d6852013-11-20 20:58:00 +00002459 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
2460 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
2461 || (len = sqlite3VdbeSerialTypeLen(t))==0
drhc8606e42013-11-20 19:28:03 +00002462 ){
2463 /* Content is irrelevant for the typeof() function and for
2464 ** the length(X) function if X is a blob. So we might as well use
2465 ** bogus content rather than reading content from disk. NULL works
2466 ** for text and blob and whatever is in the payloadSize64 variable
drh380d6852013-11-20 20:58:00 +00002467 ** will work for everything else. Content is also irrelevant if
2468 ** the content length is 0. */
2469 zData = t<=13 ? (u8*)&payloadSize64 : 0;
drhc8606e42013-11-20 19:28:03 +00002470 sMem.zMalloc = 0;
danielk1977aee18ef2005-03-09 12:26:50 +00002471 }else{
drhc8606e42013-11-20 19:28:03 +00002472 memset(&sMem, 0, sizeof(sMem));
2473 sqlite3VdbeMemMove(&sMem, pDest);
drh14da87f2013-11-20 21:51:33 +00002474 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
drhc8606e42013-11-20 19:28:03 +00002475 &sMem);
2476 if( rc!=SQLITE_OK ){
2477 goto op_column_error;
2478 }
2479 zData = (u8*)sMem.z;
2480 }
2481 sqlite3VdbeSerialGet(zData, t, pDest);
2482 /* If we dynamically allocated space to hold the data (in the
2483 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
2484 ** dynamically allocated space over to the pDest structure.
2485 ** This prevents a memory copy. */
2486 if( sMem.zMalloc ){
2487 assert( sMem.z==sMem.zMalloc );
2488 assert( !(pDest->flags & MEM_Dyn) );
2489 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2490 pDest->flags &= ~(MEM_Ephem|MEM_Static);
2491 pDest->flags |= MEM_Term;
2492 pDest->z = sMem.z;
2493 pDest->zMalloc = sMem.zMalloc;
danielk1977aee18ef2005-03-09 12:26:50 +00002494 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002495 }
drhc8606e42013-11-20 19:28:03 +00002496 pDest->enc = encoding;
drhd3194f52004-05-27 19:59:32 +00002497
danielk19773c9cc8d2005-01-17 03:40:08 +00002498op_column_out:
drha2a30282013-12-09 21:06:46 +00002499 Deephemeralize(pDest);
drhc8606e42013-11-20 19:28:03 +00002500op_column_error:
drhb7654112008-01-12 12:48:07 +00002501 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002502 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002503 break;
2504}
2505
danielk1977751de562008-04-18 09:01:15 +00002506/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002507** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002508**
2509** Apply affinities to a range of P2 registers starting with P1.
2510**
2511** P4 is a string that is P2 characters long. The nth character of the
2512** string indicates the column affinity that should be used for the nth
2513** memory cell in the range.
2514*/
2515case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002516 const char *zAffinity; /* The affinity to be applied */
2517 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002518
drh856c1032009-06-02 15:21:42 +00002519 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002520 assert( zAffinity!=0 );
2521 assert( zAffinity[pOp->p2]==0 );
2522 pIn1 = &aMem[pOp->p1];
2523 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002524 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002525 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002526 ExpandBlob(pIn1);
2527 applyAffinity(pIn1, cAff, encoding);
2528 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002529 }
2530 break;
2531}
2532
drh1db639c2008-01-17 02:36:28 +00002533/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002534** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002535**
drh710c4842010-08-30 01:17:20 +00002536** Convert P2 registers beginning with P1 into the [record format]
2537** use as a data record in a database table or as a key
2538** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002539**
danielk1977751de562008-04-18 09:01:15 +00002540** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002541** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002542** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002543**
drh8a512562005-11-14 22:29:05 +00002544** The mapping from character to affinity is given by the SQLITE_AFF_
2545** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002546**
drh66a51672008-01-03 00:01:23 +00002547** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002548*/
drh1db639c2008-01-17 02:36:28 +00002549case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002550 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2551 Mem *pRec; /* The new record */
2552 u64 nData; /* Number of bytes of data space */
2553 int nHdr; /* Number of bytes of header space */
2554 i64 nByte; /* Data space required for this record */
2555 int nZero; /* Number of zero bytes at the end of the record */
2556 int nVarint; /* Number of bytes in a varint */
2557 u32 serial_type; /* Type field */
2558 Mem *pData0; /* First field to be combined into the record */
2559 Mem *pLast; /* Last field of the record */
2560 int nField; /* Number of fields in the record */
2561 char *zAffinity; /* The affinity string for the record */
2562 int file_format; /* File format to use for encoding */
drh59bf00c2013-12-08 23:33:28 +00002563 int i; /* Space used in zNewRecord[] header */
2564 int j; /* Space used in zNewRecord[] content */
drh856c1032009-06-02 15:21:42 +00002565 int len; /* Length of a field */
2566
drhf3218fe2004-05-28 08:21:02 +00002567 /* Assuming the record contains N fields, the record format looks
2568 ** like this:
2569 **
drh7a224de2004-06-02 01:22:02 +00002570 ** ------------------------------------------------------------------------
2571 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2572 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002573 **
drh9cbf3422008-01-17 16:22:13 +00002574 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2575 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002576 **
2577 ** Each type field is a varint representing the serial type of the
2578 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002579 ** hdr-size field is also a varint which is the offset from the beginning
2580 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002581 */
drh856c1032009-06-02 15:21:42 +00002582 nData = 0; /* Number of bytes of data space */
2583 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002584 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002585 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002586 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002587 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002588 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002589 nField = pOp->p2;
2590 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002591 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002592
drh2b4ded92010-09-27 21:09:31 +00002593 /* Identify the output register */
2594 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2595 pOut = &aMem[pOp->p3];
2596 memAboutToChange(p, pOut);
2597
drh3e6c0602013-12-10 20:53:01 +00002598 /* Apply the requested affinity to all inputs
2599 */
2600 assert( pData0<=pLast );
2601 if( zAffinity ){
2602 pRec = pData0;
2603 do{
2604 applyAffinity(pRec, *(zAffinity++), encoding);
2605 }while( (++pRec)<=pLast );
2606 }
2607
drhf3218fe2004-05-28 08:21:02 +00002608 /* Loop through the elements that will make up the record to figure
2609 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002610 */
drh038b7bc2013-12-09 23:17:22 +00002611 pRec = pLast;
drh59bf00c2013-12-08 23:33:28 +00002612 do{
drh2b4ded92010-09-27 21:09:31 +00002613 assert( memIsValid(pRec) );
drhd946db02005-12-29 19:23:06 +00002614 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002615 len = sqlite3VdbeSerialTypeLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002616 if( pRec->flags & MEM_Zero ){
2617 if( nData ){
2618 sqlite3VdbeMemExpandBlob(pRec);
2619 }else{
2620 nZero += pRec->u.nZero;
2621 len -= pRec->u.nZero;
2622 }
2623 }
drhae7e1512007-05-02 16:51:59 +00002624 nData += len;
drh59bf00c2013-12-08 23:33:28 +00002625 testcase( serial_type==127 );
2626 testcase( serial_type==128 );
drh2a242872013-12-08 22:59:29 +00002627 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
drh038b7bc2013-12-09 23:17:22 +00002628 }while( (--pRec)>=pData0 );
danielk19773d1bfea2004-05-14 11:00:53 +00002629
drhf3218fe2004-05-28 08:21:02 +00002630 /* Add the initial header varint and total the size */
drh59bf00c2013-12-08 23:33:28 +00002631 testcase( nHdr==126 );
2632 testcase( nHdr==127 );
drh2a242872013-12-08 22:59:29 +00002633 if( nHdr<=126 ){
2634 /* The common case */
2635 nHdr += 1;
2636 }else{
2637 /* Rare case of a really large header */
2638 nVarint = sqlite3VarintLen(nHdr);
2639 nHdr += nVarint;
2640 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
drhcb9882a2005-03-17 03:15:40 +00002641 }
drh038b7bc2013-12-09 23:17:22 +00002642 nByte = nHdr+nData;
drhbb4957f2008-03-20 14:03:29 +00002643 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002644 goto too_big;
2645 }
drhf3218fe2004-05-28 08:21:02 +00002646
danielk1977a7a8e142008-02-13 18:25:27 +00002647 /* Make sure the output register has a buffer large enough to store
2648 ** the new record. The output register (pOp->p3) is not allowed to
2649 ** be one of the input registers (because the following call to
2650 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2651 */
drh9c1905f2008-12-10 22:32:56 +00002652 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002653 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002654 }
danielk1977a7a8e142008-02-13 18:25:27 +00002655 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002656
2657 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002658 i = putVarint32(zNewRecord, nHdr);
drh59bf00c2013-12-08 23:33:28 +00002659 j = nHdr;
2660 assert( pData0<=pLast );
2661 pRec = pData0;
2662 do{
drhd946db02005-12-29 19:23:06 +00002663 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drh038b7bc2013-12-09 23:17:22 +00002664 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
drha9ab4812013-12-11 11:00:44 +00002665 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
drh59bf00c2013-12-08 23:33:28 +00002666 }while( (++pRec)<=pLast );
2667 assert( i==nHdr );
2668 assert( j==nByte );
drhf3218fe2004-05-28 08:21:02 +00002669
dan3bc9f742013-08-15 16:18:39 +00002670 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002671 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002672 pOut->flags = MEM_Blob | MEM_Dyn;
2673 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002674 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002675 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002676 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002677 }
drh477df4b2008-01-05 18:48:24 +00002678 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002679 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002680 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002681 break;
2682}
2683
danielk1977a5533162009-02-24 10:01:51 +00002684/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002685** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002686**
2687** Store the number of entries (an integer value) in the table or index
2688** opened by cursor P1 in register P2
2689*/
2690#ifndef SQLITE_OMIT_BTREECOUNT
2691case OP_Count: { /* out2-prerelease */
2692 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002693 BtCursor *pCrsr;
2694
2695 pCrsr = p->apCsr[pOp->p1]->pCursor;
drh3da046d2013-11-11 03:24:11 +00002696 assert( pCrsr );
drh2dc06482013-12-11 00:59:10 +00002697 nEntry = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00002698 rc = sqlite3BtreeCount(pCrsr, &nEntry);
danielk1977a5533162009-02-24 10:01:51 +00002699 pOut->u.i = nEntry;
2700 break;
2701}
2702#endif
2703
danielk1977fd7f0452008-12-17 17:30:26 +00002704/* Opcode: Savepoint P1 * * P4 *
2705**
2706** Open, release or rollback the savepoint named by parameter P4, depending
2707** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2708** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2709*/
2710case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002711 int p1; /* Value of P1 operand */
2712 char *zName; /* Name of savepoint */
2713 int nName;
2714 Savepoint *pNew;
2715 Savepoint *pSavepoint;
2716 Savepoint *pTmp;
2717 int iSavepoint;
2718 int ii;
2719
2720 p1 = pOp->p1;
2721 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002722
2723 /* Assert that the p1 parameter is valid. Also that if there is no open
2724 ** transaction, then there cannot be any savepoints.
2725 */
2726 assert( db->pSavepoint==0 || db->autoCommit==0 );
2727 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2728 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2729 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002730 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002731
2732 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002733 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002734 /* A new savepoint cannot be created if there are active write
2735 ** statements (i.e. open read/write incremental blob handles).
2736 */
2737 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2738 "SQL statements in progress");
2739 rc = SQLITE_BUSY;
2740 }else{
drh856c1032009-06-02 15:21:42 +00002741 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002742
drhbe07ec52011-06-03 12:15:26 +00002743#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002744 /* This call is Ok even if this savepoint is actually a transaction
2745 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2746 ** If this is a transaction savepoint being opened, it is guaranteed
2747 ** that the db->aVTrans[] array is empty. */
2748 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002749 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2750 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002751 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002752#endif
dand9495cd2011-04-27 12:08:04 +00002753
danielk1977fd7f0452008-12-17 17:30:26 +00002754 /* Create a new savepoint structure. */
2755 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2756 if( pNew ){
2757 pNew->zName = (char *)&pNew[1];
2758 memcpy(pNew->zName, zName, nName+1);
2759
2760 /* If there is no open transaction, then mark this as a special
2761 ** "transaction savepoint". */
2762 if( db->autoCommit ){
2763 db->autoCommit = 0;
2764 db->isTransactionSavepoint = 1;
2765 }else{
2766 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002767 }
danielk1977fd7f0452008-12-17 17:30:26 +00002768
2769 /* Link the new savepoint into the database handle's list. */
2770 pNew->pNext = db->pSavepoint;
2771 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002772 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002773 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002774 }
2775 }
2776 }else{
drh856c1032009-06-02 15:21:42 +00002777 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002778
2779 /* Find the named savepoint. If there is no such savepoint, then an
2780 ** an error is returned to the user. */
2781 for(
drh856c1032009-06-02 15:21:42 +00002782 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002783 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002784 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002785 ){
2786 iSavepoint++;
2787 }
2788 if( !pSavepoint ){
2789 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2790 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002791 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002792 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002793 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002794 */
2795 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002796 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002797 );
2798 rc = SQLITE_BUSY;
2799 }else{
2800
2801 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002802 ** and this is a RELEASE command, then the current transaction
2803 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002804 */
2805 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2806 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002807 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002808 goto vdbe_return;
2809 }
danielk1977fd7f0452008-12-17 17:30:26 +00002810 db->autoCommit = 1;
2811 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2812 p->pc = pc;
2813 db->autoCommit = 0;
2814 p->rc = rc = SQLITE_BUSY;
2815 goto vdbe_return;
2816 }
danielk197734cf35d2008-12-18 18:31:38 +00002817 db->isTransactionSavepoint = 0;
2818 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002819 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002820 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002821 if( p1==SAVEPOINT_ROLLBACK ){
2822 for(ii=0; ii<db->nDb; ii++){
2823 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2824 }
drh0f198a72012-02-13 16:43:16 +00002825 }
2826 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002827 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2828 if( rc!=SQLITE_OK ){
2829 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002830 }
danielk1977fd7f0452008-12-17 17:30:26 +00002831 }
drh9f0bbf92009-01-02 21:08:09 +00002832 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002833 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002834 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002835 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002836 }
2837 }
2838
2839 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2840 ** savepoints nested inside of the savepoint being operated on. */
2841 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002842 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002843 db->pSavepoint = pTmp->pNext;
2844 sqlite3DbFree(db, pTmp);
2845 db->nSavepoint--;
2846 }
2847
dan1da40a32009-09-19 17:00:31 +00002848 /* If it is a RELEASE, then destroy the savepoint being operated on
2849 ** too. If it is a ROLLBACK TO, then set the number of deferred
2850 ** constraint violations present in the database to the value stored
2851 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002852 if( p1==SAVEPOINT_RELEASE ){
2853 assert( pSavepoint==db->pSavepoint );
2854 db->pSavepoint = pSavepoint->pNext;
2855 sqlite3DbFree(db, pSavepoint);
2856 if( !isTransaction ){
2857 db->nSavepoint--;
2858 }
dan1da40a32009-09-19 17:00:31 +00002859 }else{
2860 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002861 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002862 }
dand9495cd2011-04-27 12:08:04 +00002863
2864 if( !isTransaction ){
2865 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2866 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2867 }
danielk1977fd7f0452008-12-17 17:30:26 +00002868 }
2869 }
2870
2871 break;
2872}
2873
drh98757152008-01-09 23:04:12 +00002874/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002875**
2876** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002877** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002878** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2879** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002880**
2881** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002882*/
drh9cbf3422008-01-17 16:22:13 +00002883case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002884 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002885 int iRollback;
drh856c1032009-06-02 15:21:42 +00002886 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002887
drh856c1032009-06-02 15:21:42 +00002888 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002889 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002890 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002891 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002892 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002893 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002894 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002895
drh0f198a72012-02-13 16:43:16 +00002896#if 0
drh4f7d3a52013-06-27 23:54:02 +00002897 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002898 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002899 ** still running, and a transaction is active, return an error indicating
2900 ** that the other VMs must complete first.
2901 */
drhad4a4b82008-11-05 16:37:34 +00002902 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2903 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002904 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002905 }else
2906#endif
drh4f7d3a52013-06-27 23:54:02 +00002907 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002908 /* If this instruction implements a COMMIT and other VMs are writing
2909 ** return an error indicating that the other VMs must complete first.
2910 */
2911 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2912 "SQL statements in progress");
2913 rc = SQLITE_BUSY;
2914 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002915 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002916 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002917 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002918 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002919 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002920 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002921 }else{
shane7d3846a2008-12-11 02:58:26 +00002922 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002923 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002924 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002925 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002926 p->rc = rc = SQLITE_BUSY;
2927 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002928 }
danielk19771d850a72004-05-31 08:26:49 +00002929 }
danielk1977bd434552009-03-18 10:33:00 +00002930 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002931 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002932 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002933 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002934 }else{
drh900b31e2007-08-28 02:27:51 +00002935 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002936 }
drh900b31e2007-08-28 02:27:51 +00002937 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002938 }else{
drhf089aa42008-07-08 19:34:06 +00002939 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002940 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002941 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002942 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002943
2944 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002945 }
2946 break;
2947}
2948
drh98757152008-01-09 23:04:12 +00002949/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002950**
2951** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002952** opcode is encountered. Depending on the ON CONFLICT setting, the
2953** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002954**
drh001bbcb2003-03-19 03:14:00 +00002955** P1 is the index of the database file on which the transaction is
2956** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002957** file used for temporary tables. Indices of 2 or more are used for
2958** attached databases.
drhcabb0812002-09-14 13:47:32 +00002959**
drh80242052004-06-09 00:48:12 +00002960** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002961** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002962** other process can start another write transaction while this transaction is
2963** underway. Starting a write transaction also creates a rollback journal. A
2964** write transaction must be started before any changes can be made to the
drhf7b54962013-05-28 12:11:54 +00002965** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
2966** also obtained on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002967**
dane0af83a2009-09-08 19:15:01 +00002968** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2969** true (this flag is set if the Vdbe may modify more than one row and may
2970** throw an ABORT exception), a statement transaction may also be opened.
2971** More specifically, a statement transaction is opened iff the database
2972** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002973** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002974** VDBE to be rolled back after an error without having to roll back the
2975** entire transaction. If no error is encountered, the statement transaction
2976** will automatically commit when the VDBE halts.
2977**
danielk1977ee5741e2004-05-31 10:01:34 +00002978** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002979*/
drh9cbf3422008-01-17 16:22:13 +00002980case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002981 Btree *pBt;
2982
drh1713afb2013-06-28 01:24:57 +00002983 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00002984 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00002985 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002986 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh13447bf2013-07-10 13:33:49 +00002987 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
2988 rc = SQLITE_READONLY;
2989 goto abort_due_to_error;
2990 }
drh653b82a2009-06-22 11:10:47 +00002991 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002992
danielk197724162fe2004-06-04 06:22:00 +00002993 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002994 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002995 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002996 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002997 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002998 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002999 }
drh9e9f1bd2009-10-13 15:36:51 +00003000 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003001 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003002 }
dane0af83a2009-09-08 19:15:01 +00003003
3004 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003005 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003006 ){
3007 assert( sqlite3BtreeIsInTrans(pBt) );
3008 if( p->iStatement==0 ){
3009 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3010 db->nStatement++;
3011 p->iStatement = db->nSavepoint + db->nStatement;
3012 }
dana311b802011-04-26 19:21:34 +00003013
drh346506f2011-05-25 01:16:42 +00003014 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003015 if( rc==SQLITE_OK ){
3016 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3017 }
dan1da40a32009-09-19 17:00:31 +00003018
3019 /* Store the current value of the database handles deferred constraint
3020 ** counter. If the statement transaction needs to be rolled back,
3021 ** the value of this counter needs to be restored too. */
3022 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003023 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003024 }
drhb86ccfb2003-01-28 23:13:10 +00003025 }
drh5e00f6c2001-09-13 13:46:56 +00003026 break;
3027}
3028
drhb1fdb2a2008-01-05 04:06:03 +00003029/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003030**
drh9cbf3422008-01-17 16:22:13 +00003031** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003032** P3==1 is the schema version. P3==2 is the database format.
3033** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003034** the main database file and P1==1 is the database file used to store
3035** temporary tables.
drh4a324312001-12-21 14:30:42 +00003036**
drh50e5dad2001-09-15 00:57:28 +00003037** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003038** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003039** executing this instruction.
3040*/
drh4c583122008-01-04 22:01:03 +00003041case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003042 int iMeta;
drh856c1032009-06-02 15:21:42 +00003043 int iDb;
3044 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003045
drh1713afb2013-06-28 01:24:57 +00003046 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003047 iDb = pOp->p1;
3048 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003049 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003050 assert( iDb>=0 && iDb<db->nDb );
3051 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00003052 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00003053
danielk1977602b4662009-07-02 07:47:33 +00003054 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003055 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003056 break;
3057}
3058
drh98757152008-01-09 23:04:12 +00003059/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003060**
drh98757152008-01-09 23:04:12 +00003061** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003062** into cookie number P2 of database P1. P2==1 is the schema version.
3063** P2==2 is the database format. P2==3 is the recommended pager cache
3064** size, and so forth. P1==0 is the main database file and P1==1 is the
3065** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003066**
3067** A transaction must be started before executing this opcode.
3068*/
drh9cbf3422008-01-17 16:22:13 +00003069case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003070 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003071 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003072 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003073 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00003074 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003075 pDb = &db->aDb[pOp->p1];
3076 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003077 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003078 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003079 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003080 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003081 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3082 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003083 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003084 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003085 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003086 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003087 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003088 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003089 }
drhfd426c62006-01-30 15:34:22 +00003090 if( pOp->p1==1 ){
3091 /* Invalidate all prepared statements whenever the TEMP database
3092 ** schema is changed. Ticket #1644 */
3093 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003094 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003095 }
drh50e5dad2001-09-15 00:57:28 +00003096 break;
3097}
3098
drhc2a75552011-03-18 21:55:46 +00003099/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003100**
drh001bbcb2003-03-19 03:14:00 +00003101** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00003102** schema version) and make sure it is equal to P2 and that the
3103** generation counter on the local schema parse equals P3.
3104**
drh001bbcb2003-03-19 03:14:00 +00003105** P1 is the database number which is 0 for the main database file
3106** and 1 for the file holding temporary tables and some higher number
3107** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00003108**
3109** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00003110** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00003111** and that the current process needs to reread the schema.
3112**
3113** Either a transaction needs to have been started or an OP_Open needs
3114** to be executed (to establish a read lock) before this opcode is
3115** invoked.
3116*/
drh9cbf3422008-01-17 16:22:13 +00003117case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00003118 int iMeta;
drhc2a75552011-03-18 21:55:46 +00003119 int iGen;
drhc275b4e2004-07-19 17:25:24 +00003120 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00003121
drh001bbcb2003-03-19 03:14:00 +00003122 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003123 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh21206082011-04-04 18:22:02 +00003124 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh1713afb2013-06-28 01:24:57 +00003125 assert( p->bIsReader );
drhc275b4e2004-07-19 17:25:24 +00003126 pBt = db->aDb[pOp->p1].pBt;
3127 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00003128 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00003129 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00003130 }else{
drhfcd71b62011-04-05 22:08:24 +00003131 iGen = iMeta = 0;
drhc275b4e2004-07-19 17:25:24 +00003132 }
drhc2a75552011-03-18 21:55:46 +00003133 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00003134 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00003135 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00003136 /* If the schema-cookie from the database file matches the cookie
3137 ** stored with the in-memory representation of the schema, do
3138 ** not reload the schema from the database file.
3139 **
shane21e7feb2008-05-30 15:59:49 +00003140 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00003141 ** Often, v-tables store their data in other SQLite tables, which
3142 ** are queried from within xNext() and other v-table methods using
3143 ** prepared queries. If such a query is out-of-date, we do not want to
3144 ** discard the database schema, as the user code implementing the
3145 ** v-table would have to be ready for the sqlite3_vtab structure itself
3146 ** to be invalidated whenever sqlite3_step() is called from within
3147 ** a v-table method.
3148 */
3149 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
drh81028a42012-05-15 18:28:27 +00003150 sqlite3ResetOneSchema(db, pOp->p1);
danielk1977896e7922007-04-17 08:32:33 +00003151 }
3152
drh5b6c5452011-02-22 03:34:56 +00003153 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00003154 rc = SQLITE_SCHEMA;
3155 }
3156 break;
3157}
3158
drh98757152008-01-09 23:04:12 +00003159/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003160** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003161**
drhecdc7532001-09-23 02:35:53 +00003162** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003163** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003164** P3==0 means the main database, P3==1 means the database used for
3165** temporary tables, and P3>1 means used the corresponding attached
3166** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003167** values need not be contiguous but all P1 values should be small integers.
3168** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003169**
drh98757152008-01-09 23:04:12 +00003170** If P5!=0 then use the content of register P2 as the root page, not
3171** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003172**
drhb19a2bc2001-09-16 00:13:26 +00003173** There will be a read lock on the database whenever there is an
3174** open cursor. If the database was unlocked prior to this instruction
3175** then a read lock is acquired as part of this instruction. A read
3176** lock allows other processes to read the database but prohibits
3177** any other process from modifying the database. The read lock is
3178** released when all cursors are closed. If this instruction attempts
3179** to get a read lock but fails, the script terminates with an
3180** SQLITE_BUSY error code.
3181**
danielk1977d336e222009-02-20 10:58:41 +00003182** The P4 value may be either an integer (P4_INT32) or a pointer to
3183** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3184** structure, then said structure defines the content and collating
3185** sequence of the index being opened. Otherwise, if P4 is an integer
3186** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003187**
drh001bbcb2003-03-19 03:14:00 +00003188** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003189*/
drh98757152008-01-09 23:04:12 +00003190/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003191** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003192**
3193** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003194** page is P2. Or if P5!=0 use the content of register P2 to find the
3195** root page.
drhecdc7532001-09-23 02:35:53 +00003196**
danielk1977d336e222009-02-20 10:58:41 +00003197** The P4 value may be either an integer (P4_INT32) or a pointer to
3198** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3199** structure, then said structure defines the content and collating
3200** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003201** value, it is set to the number of columns in the table, or to the
3202** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003203**
drh001bbcb2003-03-19 03:14:00 +00003204** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003205** in read/write mode. For a given table, there can be one or more read-only
3206** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003207**
drh001bbcb2003-03-19 03:14:00 +00003208** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003209*/
drh9cbf3422008-01-17 16:22:13 +00003210case OP_OpenRead:
3211case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003212 int nField;
3213 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003214 int p2;
3215 int iDb;
drhf57b3392001-10-08 13:22:32 +00003216 int wrFlag;
3217 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003218 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003219 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003220
dan428c2182012-08-06 18:50:11 +00003221 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3222 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003223 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003224 assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003225
danfa401de2009-10-16 14:55:03 +00003226 if( p->expired ){
3227 rc = SQLITE_ABORT;
3228 break;
3229 }
3230
drh856c1032009-06-02 15:21:42 +00003231 nField = 0;
3232 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003233 p2 = pOp->p2;
3234 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003235 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003236 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003237 pDb = &db->aDb[iDb];
3238 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003239 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003240 if( pOp->opcode==OP_OpenWrite ){
3241 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003242 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003243 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3244 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003245 }
3246 }else{
3247 wrFlag = 0;
3248 }
dan428c2182012-08-06 18:50:11 +00003249 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003250 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003251 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003252 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003253 assert( memIsValid(pIn2) );
3254 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003255 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003256 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003257 /* The p2 value always comes from a prior OP_CreateTable opcode and
3258 ** that opcode will always set the p2 value to 2 or more or else fail.
3259 ** If there were a failure, the prepared statement would have halted
3260 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003261 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003262 rc = SQLITE_CORRUPT_BKPT;
3263 goto abort_due_to_error;
3264 }
drh5edc3122001-09-13 21:53:09 +00003265 }
danielk1977d336e222009-02-20 10:58:41 +00003266 if( pOp->p4type==P4_KEYINFO ){
3267 pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003268 assert( pKeyInfo->enc==ENC(db) );
3269 assert( pKeyInfo->db==db );
drhad124322013-10-23 13:30:58 +00003270 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003271 }else if( pOp->p4type==P4_INT32 ){
3272 nField = pOp->p4.i;
3273 }
drh653b82a2009-06-22 11:10:47 +00003274 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003275 assert( nField>=0 );
3276 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */
drh653b82a2009-06-22 11:10:47 +00003277 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003278 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003279 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003280 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003281 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3282 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003283 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3284 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003285
dana205a482011-08-27 18:48:57 +00003286 /* Since it performs no memory allocation or IO, the only value that
3287 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3288 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003289
drh14da87f2013-11-20 21:51:33 +00003290 /* Set the VdbeCursor.isTable variable. Previous versions of
danielk1977172114a2009-07-07 15:47:12 +00003291 ** SQLite used to check if the root-page flags were sane at this point
3292 ** and report database corruption if they were not, but this check has
3293 ** since moved into the btree layer. */
3294 pCur->isTable = pOp->p4type!=P4_KEYINFO;
drh5e00f6c2001-09-13 13:46:56 +00003295 break;
3296}
3297
drh2a5d9902011-08-26 00:34:45 +00003298/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003299** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003300**
drhb9bb7c12006-06-11 23:41:55 +00003301** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003302** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003303** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003304** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003305**
drh25d3adb2010-04-05 15:11:08 +00003306** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003307** The cursor points to a BTree table if P4==0 and to a BTree index
3308** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003309** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003310**
drh2a5d9902011-08-26 00:34:45 +00003311** The P5 parameter can be a mask of the BTREE_* flags defined
3312** in btree.h. These flags control aspects of the operation of
3313** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3314** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003315*/
drha21a64d2010-04-06 22:33:55 +00003316/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003317** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003318**
3319** This opcode works the same as OP_OpenEphemeral. It has a
3320** different name to distinguish its use. Tables created using
3321** by this opcode will be used for automatically created transient
3322** indices in joins.
3323*/
3324case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003325case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003326 VdbeCursor *pCx;
drh41e13e12013-11-07 14:09:39 +00003327 KeyInfo *pKeyInfo;
3328
drhd4187c72010-08-30 22:15:45 +00003329 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003330 SQLITE_OPEN_READWRITE |
3331 SQLITE_OPEN_CREATE |
3332 SQLITE_OPEN_EXCLUSIVE |
3333 SQLITE_OPEN_DELETEONCLOSE |
3334 SQLITE_OPEN_TRANSIENT_DB;
drh653b82a2009-06-22 11:10:47 +00003335 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003336 assert( pOp->p2>=0 );
drh653b82a2009-06-22 11:10:47 +00003337 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003338 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003339 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003340 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3341 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003342 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003343 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003344 }
3345 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003346 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003347 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003348 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003349 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003350 */
drh41e13e12013-11-07 14:09:39 +00003351 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
drhc6b52df2002-01-04 03:09:29 +00003352 int pgno;
drh66a51672008-01-03 00:01:23 +00003353 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003354 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003355 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003356 assert( pgno==MASTER_ROOT+1 );
drh41e13e12013-11-07 14:09:39 +00003357 assert( pKeyInfo->db==db );
3358 assert( pKeyInfo->enc==ENC(db) );
3359 pCx->pKeyInfo = pKeyInfo;
3360 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00003361 }
drhf0863fe2005-06-12 21:35:51 +00003362 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003363 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003364 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003365 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003366 }
drh5e00f6c2001-09-13 13:46:56 +00003367 }
drhd4187c72010-08-30 22:15:45 +00003368 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
dan5134d132011-09-02 10:31:11 +00003369 break;
3370}
3371
daneede6a52014-01-15 19:42:23 +00003372#ifndef SQLITE_OMIT_CTE
dan8ce71842014-01-14 20:14:09 +00003373/* Opcode: SwapCursors P1 P2 * * *
3374**
3375** Parameters P1 and P2 are both cursors opened by the OpenEphemeral
3376** opcode. This opcode deletes the contents of epheremal table P1,
3377** then renames P2 to P1 and P1 to P2. In other words, following this
3378** opcode cursor P2 is open on an empty table and P1 is open on the
3379** table that was initially accessed by P2.
3380*/
3381case OP_SwapCursors: {
3382 Mem tmp;
3383 VdbeCursor *pTmp;
3384
3385 tmp = p->aMem[p->nMem - pOp->p1];
3386 p->aMem[p->nMem - pOp->p1] = p->aMem[p->nMem - pOp->p2];
3387 p->aMem[p->nMem - pOp->p2] = tmp;
3388
3389 pTmp = p->apCsr[pOp->p1];
3390 p->apCsr[pOp->p1] = p->apCsr[pOp->p2];
3391 p->apCsr[pOp->p2] = pTmp;
3392
3393 rc = sqlite3BtreeClearTable(pTmp->pBt, MASTER_ROOT + !pTmp->isTable, 0);
3394 break;
3395}
daneede6a52014-01-15 19:42:23 +00003396#endif /* ifndef SQLITE_OMIT_CTE */
dan8ce71842014-01-14 20:14:09 +00003397
drh1153c7b2013-11-01 22:02:56 +00003398/* Opcode: SorterOpen P1 * * P4 *
dan5134d132011-09-02 10:31:11 +00003399**
3400** This opcode works like OP_OpenEphemeral except that it opens
3401** a transient index that is specifically designed to sort large
3402** tables using an external merge-sort algorithm.
3403*/
drhca892a72011-09-03 00:17:51 +00003404case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003405 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003406
drh399af1d2013-11-20 17:25:55 +00003407 assert( pOp->p1>=0 );
3408 assert( pOp->p2>=0 );
dan5134d132011-09-02 10:31:11 +00003409 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3410 if( pCx==0 ) goto no_mem;
3411 pCx->pKeyInfo = pOp->p4.pKeyInfo;
drh41e13e12013-11-07 14:09:39 +00003412 assert( pCx->pKeyInfo->db==db );
3413 assert( pCx->pKeyInfo->enc==ENC(db) );
dan5134d132011-09-02 10:31:11 +00003414 rc = sqlite3VdbeSorterInit(db, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003415 break;
3416}
3417
drh980db4b2012-10-30 14:44:14 +00003418/* Opcode: OpenPseudo P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00003419** Synopsis: content in r[P2@P3]
drh70ce3f02003-04-15 19:22:22 +00003420**
3421** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003422** row of data. The content of that one row in the content of memory
drh21172c42012-10-30 00:29:07 +00003423** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
3424** MEM_Blob content contained in register P2. When P5==1, then the
3425** row is represented by P3 consecutive registers beginning with P2.
drh70ce3f02003-04-15 19:22:22 +00003426**
drh2d8d7ce2010-02-15 15:17:05 +00003427** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003428** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003429** individual columns using the OP_Column opcode. The OP_Column opcode
3430** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003431**
3432** P3 is the number of fields in the records that will be stored by
3433** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003434*/
drh9cbf3422008-01-17 16:22:13 +00003435case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003436 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003437
drh653b82a2009-06-22 11:10:47 +00003438 assert( pOp->p1>=0 );
drh399af1d2013-11-20 17:25:55 +00003439 assert( pOp->p3>=0 );
drh653b82a2009-06-22 11:10:47 +00003440 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003441 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003442 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003443 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003444 pCx->isTable = 1;
drh21172c42012-10-30 00:29:07 +00003445 pCx->multiPseudo = pOp->p5;
drh70ce3f02003-04-15 19:22:22 +00003446 break;
3447}
3448
drh98757152008-01-09 23:04:12 +00003449/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003450**
3451** Close a cursor previously opened as P1. If P1 is not
3452** currently open, this instruction is a no-op.
3453*/
drh9cbf3422008-01-17 16:22:13 +00003454case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003455 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3456 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3457 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003458 break;
3459}
3460
drh959403f2008-12-12 17:56:16 +00003461/* Opcode: SeekGe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003462** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003463**
danielk1977b790c6c2008-04-18 10:25:24 +00003464** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003465** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003466** to an SQL index, then P3 is the first in an array of P4 registers
3467** that are used as an unpacked index key.
3468**
3469** Reposition cursor P1 so that it points to the smallest entry that
3470** is greater than or equal to the key value. If there are no records
3471** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003472**
drh959403f2008-12-12 17:56:16 +00003473** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003474*/
drh959403f2008-12-12 17:56:16 +00003475/* Opcode: SeekGt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003476** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003477**
danielk1977b790c6c2008-04-18 10:25:24 +00003478** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003479** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003480** to an SQL index, then P3 is the first in an array of P4 registers
3481** that are used as an unpacked index key.
3482**
3483** Reposition cursor P1 so that it points to the smallest entry that
3484** is greater than the key value. If there are no records greater than
3485** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003486**
drh959403f2008-12-12 17:56:16 +00003487** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003488*/
drh959403f2008-12-12 17:56:16 +00003489/* Opcode: SeekLt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003490** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003491**
danielk1977b790c6c2008-04-18 10:25:24 +00003492** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003493** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003494** to an SQL index, then P3 is the first in an array of P4 registers
3495** that are used as an unpacked index key.
3496**
3497** Reposition cursor P1 so that it points to the largest entry that
3498** is less than the key value. If there are no records less than
3499** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003500**
drh959403f2008-12-12 17:56:16 +00003501** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003502*/
drh959403f2008-12-12 17:56:16 +00003503/* Opcode: SeekLe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003504** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003505**
danielk1977b790c6c2008-04-18 10:25:24 +00003506** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003507** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003508** to an SQL index, then P3 is the first in an array of P4 registers
3509** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003510**
danielk1977b790c6c2008-04-18 10:25:24 +00003511** Reposition cursor P1 so that it points to the largest entry that
3512** is less than or equal to the key value. If there are no records
3513** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003514**
drh959403f2008-12-12 17:56:16 +00003515** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003516*/
drh959403f2008-12-12 17:56:16 +00003517case OP_SeekLt: /* jump, in3 */
3518case OP_SeekLe: /* jump, in3 */
3519case OP_SeekGe: /* jump, in3 */
3520case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003521 int res;
3522 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003523 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003524 UnpackedRecord r;
3525 int nField;
3526 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003527
drh653b82a2009-06-22 11:10:47 +00003528 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003529 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003530 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003531 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003532 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003533 assert( OP_SeekLe == OP_SeekLt+1 );
3534 assert( OP_SeekGe == OP_SeekLt+2 );
3535 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003536 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00003537 assert( pC->pCursor!=0 );
3538 oc = pOp->opcode;
3539 pC->nullRow = 0;
3540 if( pC->isTable ){
3541 /* The input value in P3 might be of any type: integer, real, string,
3542 ** blob, or NULL. But it needs to be an integer before we can do
3543 ** the seek, so covert it. */
3544 pIn3 = &aMem[pOp->p3];
3545 applyNumericAffinity(pIn3);
3546 iKey = sqlite3VdbeIntValue(pIn3);
3547 pC->rowidIsValid = 0;
drh959403f2008-12-12 17:56:16 +00003548
drh3da046d2013-11-11 03:24:11 +00003549 /* If the P3 value could not be converted into an integer without
3550 ** loss of information, then special processing is required... */
3551 if( (pIn3->flags & MEM_Int)==0 ){
3552 if( (pIn3->flags & MEM_Real)==0 ){
3553 /* If the P3 value cannot be converted into any kind of a number,
3554 ** then the seek is not possible, so jump to P2 */
3555 pc = pOp->p2 - 1;
3556 break;
3557 }
drh959403f2008-12-12 17:56:16 +00003558
danaa1776f2013-11-26 18:22:59 +00003559 /* If the approximation iKey is larger than the actual real search
3560 ** term, substitute >= for > and < for <=. e.g. if the search term
3561 ** is 4.9 and the integer approximation 5:
3562 **
3563 ** (x > 4.9) -> (x >= 5)
3564 ** (x <= 4.9) -> (x < 5)
3565 */
3566 if( pIn3->r<(double)iKey ){
3567 assert( OP_SeekGe==(OP_SeekGt-1) );
3568 assert( OP_SeekLt==(OP_SeekLe-1) );
3569 assert( (OP_SeekLe & 0x0001)==(OP_SeekGt & 0x0001) );
3570 if( (oc & 0x0001)==(OP_SeekGt & 0x0001) ) oc--;
3571 }
3572
3573 /* If the approximation iKey is smaller than the actual real search
3574 ** term, substitute <= for < and > for >=. */
3575 else if( pIn3->r>(double)iKey ){
3576 assert( OP_SeekLe==(OP_SeekLt+1) );
3577 assert( OP_SeekGt==(OP_SeekGe+1) );
3578 assert( (OP_SeekLt & 0x0001)==(OP_SeekGe & 0x0001) );
3579 if( (oc & 0x0001)==(OP_SeekLt & 0x0001) ) oc++;
drh8721ce42001-11-07 14:22:00 +00003580 }
drh3da046d2013-11-11 03:24:11 +00003581 }
3582 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
3583 if( rc!=SQLITE_OK ){
3584 goto abort_due_to_error;
drh1af3fdb2004-07-18 21:33:01 +00003585 }
drh3da046d2013-11-11 03:24:11 +00003586 if( res==0 ){
3587 pC->rowidIsValid = 1;
3588 pC->lastRowid = iKey;
drh8721ce42001-11-07 14:22:00 +00003589 }
drhaa736092009-06-22 00:55:30 +00003590 }else{
drh3da046d2013-11-11 03:24:11 +00003591 nField = pOp->p4.i;
3592 assert( pOp->p4type==P4_INT32 );
3593 assert( nField>0 );
3594 r.pKeyInfo = pC->pKeyInfo;
3595 r.nField = (u16)nField;
3596
3597 /* The next line of code computes as follows, only faster:
3598 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3599 ** r.flags = UNPACKED_INCRKEY;
3600 ** }else{
3601 ** r.flags = 0;
3602 ** }
danielk1977f7b9d662008-06-23 18:49:43 +00003603 */
drh3da046d2013-11-11 03:24:11 +00003604 r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
3605 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3606 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3607 assert( oc!=OP_SeekGe || r.flags==0 );
3608 assert( oc!=OP_SeekLt || r.flags==0 );
3609
3610 r.aMem = &aMem[pOp->p3];
3611#ifdef SQLITE_DEBUG
3612 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3613#endif
3614 ExpandBlob(r.aMem);
3615 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
3616 if( rc!=SQLITE_OK ){
3617 goto abort_due_to_error;
3618 }
3619 pC->rowidIsValid = 0;
3620 }
3621 pC->deferredMoveto = 0;
3622 pC->cacheStatus = CACHE_STALE;
3623#ifdef SQLITE_TEST
3624 sqlite3_search_count++;
3625#endif
3626 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
3627 if( res<0 || (res==0 && oc==OP_SeekGt) ){
3628 rc = sqlite3BtreeNext(pC->pCursor, &res);
3629 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3630 pC->rowidIsValid = 0;
3631 }else{
3632 res = 0;
3633 }
3634 }else{
3635 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3636 if( res>0 || (res==0 && oc==OP_SeekLt) ){
3637 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3638 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3639 pC->rowidIsValid = 0;
3640 }else{
3641 /* res might be negative because the table is empty. Check to
3642 ** see if this is the case.
3643 */
3644 res = sqlite3BtreeEof(pC->pCursor);
3645 }
3646 }
3647 assert( pOp->p2>0 );
3648 if( res ){
danielk1977f7b9d662008-06-23 18:49:43 +00003649 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003650 }
drh5e00f6c2001-09-13 13:46:56 +00003651 break;
3652}
3653
drh959403f2008-12-12 17:56:16 +00003654/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003655** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003656**
3657** P1 is an open table cursor and P2 is a rowid integer. Arrange
3658** for P1 to move so that it points to the rowid given by P2.
3659**
3660** This is actually a deferred seek. Nothing actually happens until
3661** the cursor is used to read a record. That way, if no reads
3662** occur, no unnecessary I/O happens.
3663*/
3664case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003665 VdbeCursor *pC;
3666
drh653b82a2009-06-22 11:10:47 +00003667 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3668 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003669 assert( pC!=0 );
drh3da046d2013-11-11 03:24:11 +00003670 assert( pC->pCursor!=0 );
3671 assert( pC->isTable );
3672 pC->nullRow = 0;
3673 pIn2 = &aMem[pOp->p2];
3674 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3675 pC->rowidIsValid = 0;
3676 pC->deferredMoveto = 1;
drh959403f2008-12-12 17:56:16 +00003677 break;
3678}
3679
3680
drh8cff69d2009-11-12 19:59:44 +00003681/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003682** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003683**
drh8cff69d2009-11-12 19:59:44 +00003684** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3685** P4>0 then register P3 is the first of P4 registers that form an unpacked
3686** record.
3687**
3688** Cursor P1 is on an index btree. If the record identified by P3 and P4
3689** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003690** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003691**
3692** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003693*/
drh8cff69d2009-11-12 19:59:44 +00003694/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003695** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003696**
drh8cff69d2009-11-12 19:59:44 +00003697** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3698** P4>0 then register P3 is the first of P4 registers that form an unpacked
3699** record.
3700**
3701** Cursor P1 is on an index btree. If the record identified by P3 and P4
3702** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3703** does contain an entry whose prefix matches the P3/P4 record then control
3704** falls through to the next instruction and P1 is left pointing at the
3705** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003706**
drh6f225d02013-10-26 13:36:51 +00003707** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003708*/
drh6f225d02013-10-26 13:36:51 +00003709/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003710** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003711**
3712** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3713** P4>0 then register P3 is the first of P4 registers that form an unpacked
3714** record.
3715**
3716** Cursor P1 is on an index btree. If the record identified by P3 and P4
3717** contains any NULL value, jump immediately to P2. If all terms of the
3718** record are not-NULL then a check is done to determine if any row in the
3719** P1 index btree has a matching key prefix. If there are no matches, jump
3720** immediately to P2. If there is a match, fall through and leave the P1
3721** cursor pointing to the matching row.
3722**
3723** This opcode is similar to OP_NotFound with the exceptions that the
3724** branch is always taken if any part of the search key input is NULL.
3725**
3726** See also: NotFound, Found, NotExists
3727*/
3728case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003729case OP_NotFound: /* jump, in3 */
3730case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003731 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00003732 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003733 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003734 int res;
dan03e9cfc2011-09-05 14:20:27 +00003735 char *pFree;
drh856c1032009-06-02 15:21:42 +00003736 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003737 UnpackedRecord r;
drhb4139222013-11-06 14:36:08 +00003738 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
drh856c1032009-06-02 15:21:42 +00003739
dan0ff297e2009-09-25 17:03:14 +00003740#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003741 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003742#endif
3743
drhaa736092009-06-22 00:55:30 +00003744 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003745 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003746 pC = p->apCsr[pOp->p1];
3747 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003748 pIn3 = &aMem[pOp->p3];
drh3da046d2013-11-11 03:24:11 +00003749 assert( pC->pCursor!=0 );
3750 assert( pC->isTable==0 );
drha9ab4812013-12-11 11:00:44 +00003751 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */
drh3da046d2013-11-11 03:24:11 +00003752 if( pOp->p4.i>0 ){
3753 r.pKeyInfo = pC->pKeyInfo;
3754 r.nField = (u16)pOp->p4.i;
3755 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003756#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00003757 {
3758 int i;
3759 for(i=0; i<r.nField; i++){
3760 assert( memIsValid(&r.aMem[i]) );
3761 if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
drh6fbe41a2013-10-30 20:22:55 +00003762 }
drh3da046d2013-11-11 03:24:11 +00003763 }
drh2b4ded92010-09-27 21:09:31 +00003764#endif
drh3da046d2013-11-11 03:24:11 +00003765 r.flags = UNPACKED_PREFIX_MATCH;
3766 pIdxKey = &r;
3767 }else{
3768 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3769 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3770 );
3771 if( pIdxKey==0 ) goto no_mem;
3772 assert( pIn3->flags & MEM_Blob );
3773 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
3774 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
3775 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
3776 }
3777 if( pOp->opcode==OP_NoConflict ){
3778 /* For the OP_NoConflict opcode, take the jump if any of the
3779 ** input fields are NULL, since any key with a NULL will not
3780 ** conflict */
3781 for(ii=0; ii<r.nField; ii++){
3782 if( r.aMem[ii].flags & MEM_Null ){
3783 pc = pOp->p2 - 1;
3784 break;
drh6f225d02013-10-26 13:36:51 +00003785 }
3786 }
drh5e00f6c2001-09-13 13:46:56 +00003787 }
drh3da046d2013-11-11 03:24:11 +00003788 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
3789 if( pOp->p4.i==0 ){
3790 sqlite3DbFree(db, pFree);
3791 }
3792 if( rc!=SQLITE_OK ){
3793 break;
3794 }
drh1fd522f2013-11-21 00:10:35 +00003795 pC->seekResult = res;
drh3da046d2013-11-11 03:24:11 +00003796 alreadyExists = (res==0);
3797 pC->nullRow = 1-alreadyExists;
3798 pC->deferredMoveto = 0;
3799 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003800 if( pOp->opcode==OP_Found ){
3801 if( alreadyExists ) pc = pOp->p2 - 1;
3802 }else{
3803 if( !alreadyExists ) pc = pOp->p2 - 1;
3804 }
drh5e00f6c2001-09-13 13:46:56 +00003805 break;
3806}
3807
drh9cbf3422008-01-17 16:22:13 +00003808/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003809** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00003810**
drh261c02d2013-10-25 14:46:15 +00003811** P1 is the index of a cursor open on an SQL table btree (with integer
3812** keys). P3 is an integer rowid. If P1 does not contain a record with
3813** rowid P3 then jump immediately to P2. If P1 does contain a record
3814** with rowid P3 then leave the cursor pointing at that record and fall
3815** through to the next instruction.
drh6b125452002-01-28 15:53:03 +00003816**
drh261c02d2013-10-25 14:46:15 +00003817** The OP_NotFound opcode performs the same operation on index btrees
3818** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00003819**
drh11e85272013-10-26 15:40:48 +00003820** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00003821*/
drh9cbf3422008-01-17 16:22:13 +00003822case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003823 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003824 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003825 int res;
3826 u64 iKey;
3827
drh3c657212009-11-17 23:59:58 +00003828 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003829 assert( pIn3->flags & MEM_Int );
3830 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3831 pC = p->apCsr[pOp->p1];
3832 assert( pC!=0 );
3833 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003834 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003835 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00003836 assert( pCrsr!=0 );
3837 res = 0;
3838 iKey = pIn3->u.i;
3839 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
3840 pC->lastRowid = pIn3->u.i;
3841 pC->rowidIsValid = res==0 ?1:0;
3842 pC->nullRow = 0;
3843 pC->cacheStatus = CACHE_STALE;
3844 pC->deferredMoveto = 0;
3845 if( res!=0 ){
danielk1977f7b9d662008-06-23 18:49:43 +00003846 pc = pOp->p2 - 1;
3847 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003848 }
drh1fd522f2013-11-21 00:10:35 +00003849 pC->seekResult = res;
drh6b125452002-01-28 15:53:03 +00003850 break;
3851}
3852
drh4c583122008-01-04 22:01:03 +00003853/* Opcode: Sequence P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003854** Synopsis: r[P2]=rowid
drh4db38a72005-09-01 12:16:28 +00003855**
drh4c583122008-01-04 22:01:03 +00003856** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003857** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003858** The sequence number on the cursor is incremented after this
3859** instruction.
drh4db38a72005-09-01 12:16:28 +00003860*/
drh4c583122008-01-04 22:01:03 +00003861case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003862 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3863 assert( p->apCsr[pOp->p1]!=0 );
3864 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003865 break;
3866}
3867
3868
drh98757152008-01-09 23:04:12 +00003869/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003870** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00003871**
drhf0863fe2005-06-12 21:35:51 +00003872** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003873** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003874** table that cursor P1 points to. The new record number is written
3875** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003876**
dan76d462e2009-08-30 11:42:51 +00003877** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3878** the largest previously generated record number. No new record numbers are
3879** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003880** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003881** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003882** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003883*/
drh4c583122008-01-04 22:01:03 +00003884case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003885 i64 v; /* The new rowid */
3886 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3887 int res; /* Result of an sqlite3BtreeLast() */
3888 int cnt; /* Counter to limit the number of searches */
3889 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003890 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003891
drh856c1032009-06-02 15:21:42 +00003892 v = 0;
3893 res = 0;
drhaa736092009-06-22 00:55:30 +00003894 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3895 pC = p->apCsr[pOp->p1];
3896 assert( pC!=0 );
3897 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003898 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003899 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003900 /* The next rowid or record number (different terms for the same
3901 ** thing) is obtained in a two-step algorithm.
3902 **
3903 ** First we attempt to find the largest existing rowid and add one
3904 ** to that. But if the largest existing rowid is already the maximum
3905 ** positive integer, we have to fall through to the second
3906 ** probabilistic algorithm
3907 **
3908 ** The second algorithm is to select a rowid at random and see if
3909 ** it already exists in the table. If it does not exist, we have
3910 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003911 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003912 */
drhaa736092009-06-22 00:55:30 +00003913 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003914
drh75f86a42005-02-17 00:03:06 +00003915#ifdef SQLITE_32BIT_ROWID
3916# define MAX_ROWID 0x7fffffff
3917#else
drhfe2093d2005-01-20 22:48:47 +00003918 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3919 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3920 ** to provide the constant while making all compilers happy.
3921 */
danielk197764202cf2008-11-17 15:31:47 +00003922# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003923#endif
drhfe2093d2005-01-20 22:48:47 +00003924
drh5cf8e8c2002-02-19 22:42:05 +00003925 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003926 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3927 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003928 rc = sqlite3BtreeLast(pC->pCursor, &res);
3929 if( rc!=SQLITE_OK ){
3930 goto abort_due_to_error;
3931 }
drh32fbe342002-10-19 20:16:37 +00003932 if( res ){
drhc79c7612010-01-01 18:57:48 +00003933 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003934 }else{
drhea8ffdf2009-07-22 00:35:23 +00003935 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003936 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3937 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drha40eb7c2012-02-24 00:02:28 +00003938 if( v>=MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003939 pC->useRandomRowid = 1;
3940 }else{
drhc79c7612010-01-01 18:57:48 +00003941 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003942 }
drh5cf8e8c2002-02-19 22:42:05 +00003943 }
drh3fc190c2001-09-14 03:24:23 +00003944 }
drh205f48e2004-11-05 00:43:11 +00003945
3946#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003947 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003948 /* Assert that P3 is a valid memory cell. */
3949 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003950 if( p->pFrame ){
3951 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003952 /* Assert that P3 is a valid memory cell. */
3953 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003954 pMem = &pFrame->aMem[pOp->p3];
3955 }else{
shaneabc6b892009-09-10 19:09:03 +00003956 /* Assert that P3 is a valid memory cell. */
dan3bc9f742013-08-15 16:18:39 +00003957 assert( pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003958 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003959 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003960 }
drh2b4ded92010-09-27 21:09:31 +00003961 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003962
3963 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003964 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003965 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003966 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003967 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003968 goto abort_due_to_error;
3969 }
drh3c024d62007-03-30 11:23:45 +00003970 if( v<pMem->u.i+1 ){
3971 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003972 }
drh3c024d62007-03-30 11:23:45 +00003973 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003974 }
3975#endif
3976
drh7f751222009-03-17 22:33:00 +00003977 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003978 }
3979 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003980 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003981 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003982 ** engine starts picking positive candidate ROWIDs at random until
3983 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003984 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3985 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003986 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00003987 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003988 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3989 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003990 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003991 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3992 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003993 && (res==0)
3994 && (++cnt<100)){
3995 /* collision - try another random rowid */
3996 sqlite3_randomness(sizeof(v), &v);
3997 if( cnt<5 ){
3998 /* try "small" random rowids for the initial attempts */
3999 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00004000 }else{
shanehc4d340a2010-09-01 02:37:56 +00004001 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00004002 }
shanehc4d340a2010-09-01 02:37:56 +00004003 v++; /* ensure non-zero */
4004 }
drhaa736092009-06-22 00:55:30 +00004005 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004006 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004007 goto abort_due_to_error;
4008 }
drh748a52c2010-09-01 11:50:08 +00004009 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004010 }
drhf0863fe2005-06-12 21:35:51 +00004011 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00004012 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004013 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004014 }
drh4c583122008-01-04 22:01:03 +00004015 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004016 break;
4017}
4018
danielk19771f4aa332008-01-03 09:51:55 +00004019/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004020** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004021**
jplyon5a564222003-06-02 06:15:58 +00004022** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004023** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004024** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004025** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004026** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004027**
danielk19771f4aa332008-01-03 09:51:55 +00004028** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4029** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004030** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004031** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004032**
drh3e9ca092009-09-08 01:14:48 +00004033** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4034** the last seek operation (OP_NotExists) was a success, then this
4035** operation will not attempt to find the appropriate row before doing
4036** the insert but will instead overwrite the row that the cursor is
4037** currently pointing to. Presumably, the prior OP_NotExists opcode
4038** has already positioned the cursor correctly. This is an optimization
4039** that boosts performance by avoiding redundant seeks.
4040**
4041** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4042** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4043** is part of an INSERT operation. The difference is only important to
4044** the update hook.
4045**
drh66a51672008-01-03 00:01:23 +00004046** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004047** may be NULL. If it is not NULL, then the update-hook
4048** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4049**
drh93aed5a2008-01-16 17:46:38 +00004050** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4051** allocated, then ownership of P2 is transferred to the pseudo-cursor
4052** and register P2 becomes ephemeral. If the cursor is changed, the
4053** value of register P2 will then change. Make sure this does not
4054** cause any problems.)
4055**
drhf0863fe2005-06-12 21:35:51 +00004056** This instruction only works on tables. The equivalent instruction
4057** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004058*/
drhe05c9292009-10-29 13:48:10 +00004059/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004060** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004061**
4062** This works exactly like OP_Insert except that the key is the
4063** integer value P3, not the value of the integer stored in register P3.
4064*/
4065case OP_Insert:
4066case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004067 Mem *pData; /* MEM cell holding data for the record to be inserted */
4068 Mem *pKey; /* MEM cell holding key for the record */
4069 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4070 VdbeCursor *pC; /* Cursor to table into which insert is written */
4071 int nZero; /* Number of zero-bytes to append */
drh1fd522f2013-11-21 00:10:35 +00004072 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
drh3e9ca092009-09-08 01:14:48 +00004073 const char *zDb; /* database name - used by the update hook */
4074 const char *zTbl; /* Table name - used by the opdate hook */
4075 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004076
drha6c2ed92009-11-14 23:22:23 +00004077 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004078 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004079 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004080 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004081 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004082 assert( pC->pCursor!=0 );
4083 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004084 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004085 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004086
drhe05c9292009-10-29 13:48:10 +00004087 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004088 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004089 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004090 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004091 REGISTER_TRACE(pOp->p3, pKey);
4092 iKey = pKey->u.i;
4093 }else{
4094 assert( pOp->opcode==OP_InsertInt );
4095 iKey = pOp->p3;
4096 }
4097
drha05a7222008-01-19 03:35:58 +00004098 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004099 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004100 if( pData->flags & MEM_Null ){
4101 pData->z = 0;
4102 pData->n = 0;
4103 }else{
4104 assert( pData->flags & (MEM_Blob|MEM_Str) );
4105 }
drh3e9ca092009-09-08 01:14:48 +00004106 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4107 if( pData->flags & MEM_Zero ){
4108 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004109 }else{
drh3e9ca092009-09-08 01:14:48 +00004110 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004111 }
drh3e9ca092009-09-08 01:14:48 +00004112 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
4113 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4114 pData->z, pData->n, nZero,
drhebf10b12013-11-25 17:38:26 +00004115 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
drh3e9ca092009-09-08 01:14:48 +00004116 );
drha05a7222008-01-19 03:35:58 +00004117 pC->rowidIsValid = 0;
4118 pC->deferredMoveto = 0;
4119 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004120
drha05a7222008-01-19 03:35:58 +00004121 /* Invoke the update-hook if required. */
4122 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004123 zDb = db->aDb[pC->iDb].zName;
4124 zTbl = pOp->p4.z;
4125 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004126 assert( pC->isTable );
4127 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4128 assert( pC->iDb>=0 );
4129 }
drh5e00f6c2001-09-13 13:46:56 +00004130 break;
4131}
4132
drh98757152008-01-09 23:04:12 +00004133/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004134**
drh5edc3122001-09-13 21:53:09 +00004135** Delete the record at which the P1 cursor is currently pointing.
4136**
4137** The cursor will be left pointing at either the next or the previous
4138** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004139** the next Next instruction will be a no-op. Hence it is OK to delete
4140** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004141**
rdcb0c374f2004-02-20 22:53:38 +00004142** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004143** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004144**
drh91fd4d42008-01-19 20:11:25 +00004145** P1 must not be pseudo-table. It has to be a real table with
4146** multiple rows.
4147**
4148** If P4 is not NULL, then it is the name of the table that P1 is
4149** pointing to. The update hook will be invoked, if it exists.
4150** If P4 is not NULL then the P1 cursor must have been positioned
4151** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004152*/
drh9cbf3422008-01-17 16:22:13 +00004153case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004154 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004155 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004156
drh653b82a2009-06-22 11:10:47 +00004157 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4158 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004159 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004160 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
drhbbbb0e82013-11-26 23:27:07 +00004161 iKey = pC->lastRowid; /* Only used for the update hook */
danielk197794eb6a12005-12-15 15:22:08 +00004162
drh9a65f2c2009-06-22 19:05:40 +00004163 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4164 ** OP_Column on the same table without any intervening operations that
4165 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4166 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4167 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4168 ** to guard against future changes to the code generator.
4169 **/
4170 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004171 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004172 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4173
drh7f751222009-03-17 22:33:00 +00004174 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00004175 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004176 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004177
drh91fd4d42008-01-19 20:11:25 +00004178 /* Invoke the update-hook if required. */
drhbbbb0e82013-11-26 23:27:07 +00004179 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
drh2c77be02013-11-27 21:07:03 +00004180 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
4181 db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
drh91fd4d42008-01-19 20:11:25 +00004182 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004183 }
danielk1977b28af712004-06-21 06:50:26 +00004184 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004185 break;
4186}
drhb7f1d9a2009-09-08 02:27:58 +00004187/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004188**
drhb7f1d9a2009-09-08 02:27:58 +00004189** The value of the change counter is copied to the database handle
4190** change counter (returned by subsequent calls to sqlite3_changes()).
4191** Then the VMs internal change counter resets to 0.
4192** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004193*/
drh9cbf3422008-01-17 16:22:13 +00004194case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004195 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004196 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004197 break;
4198}
4199
drh1153c7b2013-11-01 22:02:56 +00004200/* Opcode: SorterCompare P1 P2 P3 P4
4201** Synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004202**
drh1153c7b2013-11-01 22:02:56 +00004203** P1 is a sorter cursor. This instruction compares a prefix of the
4204** the record blob in register P3 against a prefix of the entry that
4205** the sorter cursor currently points to. The final P4 fields of both
4206** the P3 and sorter record are ignored.
4207**
4208** If either P3 or the sorter contains a NULL in one of their significant
4209** fields (not counting the P4 fields at the end which are ignored) then
4210** the comparison is assumed to be equal.
4211**
4212** Fall through to next instruction if the two records compare equal to
4213** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004214*/
4215case OP_SorterCompare: {
4216 VdbeCursor *pC;
4217 int res;
drh1153c7b2013-11-01 22:02:56 +00004218 int nIgnore;
dan5134d132011-09-02 10:31:11 +00004219
4220 pC = p->apCsr[pOp->p1];
4221 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004222 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004223 pIn3 = &aMem[pOp->p3];
drh1153c7b2013-11-01 22:02:56 +00004224 nIgnore = pOp->p4.i;
4225 rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
dan5134d132011-09-02 10:31:11 +00004226 if( res ){
4227 pc = pOp->p2-1;
4228 }
4229 break;
4230};
4231
4232/* Opcode: SorterData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004233** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004234**
4235** Write into register P2 the current sorter data for sorter cursor P1.
4236*/
4237case OP_SorterData: {
4238 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004239
dan5134d132011-09-02 10:31:11 +00004240 pOut = &aMem[pOp->p2];
4241 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004242 assert( isSorter(pC) );
dan5134d132011-09-02 10:31:11 +00004243 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4244 break;
4245}
4246
drh98757152008-01-09 23:04:12 +00004247/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004248** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004249**
drh98757152008-01-09 23:04:12 +00004250** Write into register P2 the complete row data for cursor P1.
4251** There is no interpretation of the data.
4252** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004253** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004254**
drhde4fcfd2008-01-19 23:50:26 +00004255** If the P1 cursor must be pointing to a valid row (not a NULL row)
4256** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004257*/
drh98757152008-01-09 23:04:12 +00004258/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004259** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004260**
drh98757152008-01-09 23:04:12 +00004261** Write into register P2 the complete row key for cursor P1.
4262** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004263** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004264** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004265**
drhde4fcfd2008-01-19 23:50:26 +00004266** If the P1 cursor must be pointing to a valid row (not a NULL row)
4267** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004268*/
danielk1977a7a8e142008-02-13 18:25:27 +00004269case OP_RowKey:
4270case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004271 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004272 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004273 u32 n;
drh856c1032009-06-02 15:21:42 +00004274 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004275
drha6c2ed92009-11-14 23:22:23 +00004276 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004277 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004278
drhf0863fe2005-06-12 21:35:51 +00004279 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004280 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4281 pC = p->apCsr[pOp->p1];
drh14da87f2013-11-20 21:51:33 +00004282 assert( isSorter(pC)==0 );
drhc6aff302011-09-01 15:32:47 +00004283 assert( pC->isTable || pOp->opcode!=OP_RowData );
drh14da87f2013-11-20 21:51:33 +00004284 assert( pC->isTable==0 || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004285 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004286 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004287 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004288 assert( pC->pCursor!=0 );
4289 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004290 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004291
4292 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4293 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4294 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4295 ** a no-op and can never fail. But we leave it in place as a safety.
4296 */
4297 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004298 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004299 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4300
drh14da87f2013-11-20 21:51:33 +00004301 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004302 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004303 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004304 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004305 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004306 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004307 }
drhbfb19dc2009-06-05 16:46:53 +00004308 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004309 }else{
drhb07028f2011-10-14 21:49:18 +00004310 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004311 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004312 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004313 goto too_big;
4314 }
drhde4fcfd2008-01-19 23:50:26 +00004315 }
danielk1977a7a8e142008-02-13 18:25:27 +00004316 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4317 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004318 }
danielk1977a7a8e142008-02-13 18:25:27 +00004319 pOut->n = n;
4320 MemSetTypeFlag(pOut, MEM_Blob);
drh14da87f2013-11-20 21:51:33 +00004321 if( pC->isTable==0 ){
drhde4fcfd2008-01-19 23:50:26 +00004322 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4323 }else{
4324 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004325 }
danielk197796cb76f2008-01-04 13:24:28 +00004326 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004327 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004328 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004329 break;
4330}
4331
drh2133d822008-01-03 18:44:59 +00004332/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004333** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004334**
drh2133d822008-01-03 18:44:59 +00004335** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004336** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004337**
4338** P1 can be either an ordinary table or a virtual table. There used to
4339** be a separate OP_VRowid opcode for use with virtual tables, but this
4340** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004341*/
drh4c583122008-01-04 22:01:03 +00004342case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004343 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004344 i64 v;
drh856c1032009-06-02 15:21:42 +00004345 sqlite3_vtab *pVtab;
4346 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004347
drh653b82a2009-06-22 11:10:47 +00004348 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4349 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004350 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004351 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004352 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004353 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004354 break;
4355 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004356 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004357#ifndef SQLITE_OMIT_VIRTUALTABLE
4358 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004359 pVtab = pC->pVtabCursor->pVtab;
4360 pModule = pVtab->pModule;
4361 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004362 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004363 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004364#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004365 }else{
drh6be240e2009-07-14 02:33:02 +00004366 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004367 rc = sqlite3VdbeCursorMoveto(pC);
4368 if( rc ) goto abort_due_to_error;
4369 if( pC->rowidIsValid ){
4370 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004371 }else{
drhc27ae612009-07-14 18:35:44 +00004372 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4373 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004374 }
drh5e00f6c2001-09-13 13:46:56 +00004375 }
drh4c583122008-01-04 22:01:03 +00004376 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004377 break;
4378}
4379
drh9cbf3422008-01-17 16:22:13 +00004380/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004381**
4382** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004383** that occur while the cursor is on the null row will always
4384** write a NULL.
drh17f71932002-02-21 12:01:27 +00004385*/
drh9cbf3422008-01-17 16:22:13 +00004386case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004387 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004388
drh653b82a2009-06-22 11:10:47 +00004389 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4390 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004391 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004392 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004393 pC->rowidIsValid = 0;
drh399af1d2013-11-20 17:25:55 +00004394 pC->cacheStatus = CACHE_STALE;
dana205a482011-08-27 18:48:57 +00004395 assert( pC->pCursor || pC->pVtabCursor );
danielk1977be51a652008-10-08 17:58:48 +00004396 if( pC->pCursor ){
4397 sqlite3BtreeClearCursor(pC->pCursor);
4398 }
drh17f71932002-02-21 12:01:27 +00004399 break;
4400}
4401
drh9cbf3422008-01-17 16:22:13 +00004402/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004403**
drhf0863fe2005-06-12 21:35:51 +00004404** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004405** will refer to the last entry in the database table or index.
4406** If the table or index is empty and P2>0, then jump immediately to P2.
4407** If P2 is 0 or if the table or index is not empty, fall through
4408** to the following instruction.
4409*/
drh9cbf3422008-01-17 16:22:13 +00004410case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004411 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004412 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004413 int res;
drh9562b552002-02-19 15:00:07 +00004414
drh653b82a2009-06-22 11:10:47 +00004415 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4416 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004417 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004418 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004419 res = 0;
drh3da046d2013-11-11 03:24:11 +00004420 assert( pCrsr!=0 );
4421 rc = sqlite3BtreeLast(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004422 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004423 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004424 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004425 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004426 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004427 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004428 }
4429 break;
4430}
4431
drh0342b1f2005-09-01 03:07:44 +00004432
drh9cbf3422008-01-17 16:22:13 +00004433/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004434**
4435** This opcode does exactly the same thing as OP_Rewind except that
4436** it increments an undocumented global variable used for testing.
4437**
4438** Sorting is accomplished by writing records into a sorting index,
4439** then rewinding that index and playing it back from beginning to
4440** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4441** rewinding so that the global variable will be incremented and
4442** regression tests can determine whether or not the optimizer is
4443** correctly optimizing out sorts.
4444*/
drhc6aff302011-09-01 15:32:47 +00004445case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004446case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004447#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004448 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004449 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004450#endif
drh9b47ee32013-08-20 03:13:51 +00004451 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004452 /* Fall through into OP_Rewind */
4453}
drh9cbf3422008-01-17 16:22:13 +00004454/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004455**
drhf0863fe2005-06-12 21:35:51 +00004456** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004457** will refer to the first entry in the database table or index.
4458** If the table or index is empty and P2>0, then jump immediately to P2.
4459** If P2 is 0 or if the table or index is not empty, fall through
4460** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004461*/
drh9cbf3422008-01-17 16:22:13 +00004462case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004463 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004464 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004465 int res;
drh5e00f6c2001-09-13 13:46:56 +00004466
drh653b82a2009-06-22 11:10:47 +00004467 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4468 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004469 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004470 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004471 res = 1;
dan689ab892011-08-12 15:02:00 +00004472 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004473 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004474 }else{
4475 pCrsr = pC->pCursor;
4476 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004477 rc = sqlite3BtreeFirst(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00004478 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004479 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004480 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004481 }
drh9c1905f2008-12-10 22:32:56 +00004482 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004483 assert( pOp->p2>0 && pOp->p2<p->nOp );
4484 if( res ){
drhf4dada72004-05-11 09:57:35 +00004485 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004486 }
4487 break;
4488}
4489
drh81316f82013-10-29 20:40:47 +00004490/* Opcode: Next P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00004491**
4492** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004493** table or index. If there are no more key/value pairs then fall through
4494** to the following instruction. But if the cursor advance was successful,
4495** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004496**
drhf93cd942013-11-21 03:12:25 +00004497** The P1 cursor must be for a real table, not a pseudo-table. P1 must have
4498** been opened prior to this opcode or the program will segfault.
drh60a713c2008-01-21 16:22:45 +00004499**
dana205a482011-08-27 18:48:57 +00004500** P4 is always of type P4_ADVANCE. The function pointer points to
4501** sqlite3BtreeNext().
4502**
drhafc266a2010-03-31 17:47:44 +00004503** If P5 is positive and the jump is taken, then event counter
4504** number P5-1 in the prepared statement is incremented.
4505**
drhf93cd942013-11-21 03:12:25 +00004506** See also: Prev, NextIfOpen
4507*/
4508/* Opcode: NextIfOpen P1 P2 * * P5
4509**
4510** This opcode works just like OP_Next except that if cursor P1 is not
4511** open it behaves a no-op.
drh8721ce42001-11-07 14:22:00 +00004512*/
drhafc266a2010-03-31 17:47:44 +00004513/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004514**
4515** Back up cursor P1 so that it points to the previous key/data pair in its
4516** table or index. If there is no previous key/value pairs then fall through
4517** to the following instruction. But if the cursor backup was successful,
4518** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004519**
drhf93cd942013-11-21 03:12:25 +00004520** The P1 cursor must be for a real table, not a pseudo-table. If P1 is
4521** not open then the behavior is undefined.
drhafc266a2010-03-31 17:47:44 +00004522**
dana205a482011-08-27 18:48:57 +00004523** P4 is always of type P4_ADVANCE. The function pointer points to
4524** sqlite3BtreePrevious().
4525**
drhafc266a2010-03-31 17:47:44 +00004526** If P5 is positive and the jump is taken, then event counter
4527** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004528*/
drhf93cd942013-11-21 03:12:25 +00004529/* Opcode: PrevIfOpen P1 P2 * * P5
4530**
4531** This opcode works just like OP_Prev except that if cursor P1 is not
4532** open it behaves a no-op.
4533*/
4534case OP_SorterNext: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004535 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004536 int res;
drh8721ce42001-11-07 14:22:00 +00004537
drhf93cd942013-11-21 03:12:25 +00004538 pC = p->apCsr[pOp->p1];
4539 assert( isSorter(pC) );
4540 rc = sqlite3VdbeSorterNext(db, pC, &res);
4541 goto next_tail;
4542case OP_PrevIfOpen: /* jump */
4543case OP_NextIfOpen: /* jump */
4544 if( p->apCsr[pOp->p1]==0 ) break;
4545 /* Fall through */
4546case OP_Prev: /* jump */
4547case OP_Next: /* jump */
drh70ce3f02003-04-15 19:22:22 +00004548 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004549 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004550 pC = p->apCsr[pOp->p1];
drhf93cd942013-11-21 03:12:25 +00004551 assert( pC!=0 );
4552 assert( pC->deferredMoveto==0 );
4553 assert( pC->pCursor );
4554 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4555 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4556 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
4557 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
4558 rc = pOp->p4.xAdvance(pC->pCursor, &res);
4559next_tail:
drha3460582008-07-11 21:02:53 +00004560 pC->cacheStatus = CACHE_STALE;
4561 if( res==0 ){
drhf93cd942013-11-21 03:12:25 +00004562 pC->nullRow = 0;
drha3460582008-07-11 21:02:53 +00004563 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004564 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004565#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004566 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004567#endif
drhf93cd942013-11-21 03:12:25 +00004568 }else{
4569 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00004570 }
drhf0863fe2005-06-12 21:35:51 +00004571 pC->rowidIsValid = 0;
drh49afe3a2013-07-10 03:05:14 +00004572 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004573}
4574
danielk1977de630352009-05-04 11:42:29 +00004575/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004576** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004577**
drhef8662b2011-06-20 21:47:58 +00004578** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004579** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004580** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004581**
drhaa9b8962008-01-08 02:57:55 +00004582** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004583** insert is likely to be an append.
4584**
drhf0863fe2005-06-12 21:35:51 +00004585** This instruction only works for indices. The equivalent instruction
4586** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004587*/
drhca892a72011-09-03 00:17:51 +00004588case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004589case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004590 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004591 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004592 int nKey;
4593 const char *zKey;
4594
drh653b82a2009-06-22 11:10:47 +00004595 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4596 pC = p->apCsr[pOp->p1];
4597 assert( pC!=0 );
drh14da87f2013-11-20 21:51:33 +00004598 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004599 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004600 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004601 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004602 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh3da046d2013-11-11 03:24:11 +00004603 assert( pCrsr!=0 );
4604 assert( pC->isTable==0 );
4605 rc = ExpandBlob(pIn2);
4606 if( rc==SQLITE_OK ){
4607 if( isSorter(pC) ){
4608 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4609 }else{
4610 nKey = pIn2->n;
4611 zKey = pIn2->z;
4612 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4613 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
4614 );
4615 assert( pC->deferredMoveto==0 );
4616 pC->cacheStatus = CACHE_STALE;
danielk1977d908f5a2007-05-11 07:08:28 +00004617 }
drh5e00f6c2001-09-13 13:46:56 +00004618 }
drh5e00f6c2001-09-13 13:46:56 +00004619 break;
4620}
4621
drh4308e342013-11-11 16:55:52 +00004622/* Opcode: IdxDelete P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00004623** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004624**
drhe14006d2008-03-25 17:23:32 +00004625** The content of P3 registers starting at register P2 form
4626** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004627** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004628*/
drhe14006d2008-03-25 17:23:32 +00004629case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004630 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004631 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004632 int res;
4633 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004634
drhe14006d2008-03-25 17:23:32 +00004635 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004636 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
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;
drh3da046d2013-11-11 03:24:11 +00004641 assert( pCrsr!=0 );
drh4308e342013-11-11 16:55:52 +00004642 assert( pOp->p5==0 );
drh3da046d2013-11-11 03:24:11 +00004643 r.pKeyInfo = pC->pKeyInfo;
4644 r.nField = (u16)pOp->p3;
4645 r.flags = UNPACKED_PREFIX_MATCH;
4646 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004647#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004648 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004649#endif
drh3da046d2013-11-11 03:24:11 +00004650 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
4651 if( rc==SQLITE_OK && res==0 ){
4652 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004653 }
drh3da046d2013-11-11 03:24:11 +00004654 assert( pC->deferredMoveto==0 );
4655 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004656 break;
4657}
4658
drh2133d822008-01-03 18:44:59 +00004659/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004660** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004661**
drh2133d822008-01-03 18:44:59 +00004662** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004663** the end of the index key pointed to by cursor P1. This integer should be
4664** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004665**
drh9437bd22009-02-01 00:29:56 +00004666** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004667*/
drh4c583122008-01-04 22:01:03 +00004668case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004669 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004670 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004671 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004672
drh653b82a2009-06-22 11:10:47 +00004673 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4674 pC = p->apCsr[pOp->p1];
4675 assert( pC!=0 );
4676 pCrsr = pC->pCursor;
drh3da046d2013-11-11 03:24:11 +00004677 assert( pCrsr!=0 );
drh3c657212009-11-17 23:59:58 +00004678 pOut->flags = MEM_Null;
drh3da046d2013-11-11 03:24:11 +00004679 rc = sqlite3VdbeCursorMoveto(pC);
4680 if( NEVER(rc) ) goto abort_due_to_error;
4681 assert( pC->deferredMoveto==0 );
4682 assert( pC->isTable==0 );
4683 if( !pC->nullRow ){
drh2dc06482013-12-11 00:59:10 +00004684 rowid = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00004685 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
4686 if( rc!=SQLITE_OK ){
4687 goto abort_due_to_error;
danielk19773d1bfea2004-05-14 11:00:53 +00004688 }
drh3da046d2013-11-11 03:24:11 +00004689 pOut->u.i = rowid;
4690 pOut->flags = MEM_Int;
drh8721ce42001-11-07 14:22:00 +00004691 }
4692 break;
4693}
4694
danielk197761dd5832008-04-18 11:31:12 +00004695/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004696** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004697**
danielk197761dd5832008-04-18 11:31:12 +00004698** The P4 register values beginning with P3 form an unpacked index
4699** key that omits the ROWID. Compare this key value against the index
4700** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004701**
danielk197761dd5832008-04-18 11:31:12 +00004702** If the P1 index entry is greater than or equal to the key value
4703** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004704**
danielk197761dd5832008-04-18 11:31:12 +00004705** If P5 is non-zero then the key value is increased by an epsilon
4706** prior to the comparison. This make the opcode work like IdxGT except
4707** that if the key from register P3 is a prefix of the key in the cursor,
4708** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004709*/
drh3bb9b932010-08-06 02:10:00 +00004710/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004711** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004712**
danielk197761dd5832008-04-18 11:31:12 +00004713** The P4 register values beginning with P3 form an unpacked index
4714** key that omits the ROWID. Compare this key value against the index
4715** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004716**
danielk197761dd5832008-04-18 11:31:12 +00004717** If the P1 index entry is less than the key value then jump to P2.
4718** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004719**
danielk197761dd5832008-04-18 11:31:12 +00004720** If P5 is non-zero then the key value is increased by an epsilon prior
4721** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004722*/
drh93952eb2009-11-13 19:43:43 +00004723case OP_IdxLT: /* jump */
4724case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004725 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004726 int res;
4727 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004728
drh653b82a2009-06-22 11:10:47 +00004729 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4730 pC = p->apCsr[pOp->p1];
4731 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004732 assert( pC->isOrdered );
drh3da046d2013-11-11 03:24:11 +00004733 assert( pC->pCursor!=0);
4734 assert( pC->deferredMoveto==0 );
4735 assert( pOp->p5==0 || pOp->p5==1 );
4736 assert( pOp->p4type==P4_INT32 );
4737 r.pKeyInfo = pC->pKeyInfo;
4738 r.nField = (u16)pOp->p4.i;
4739 if( pOp->p5 ){
4740 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
4741 }else{
4742 r.flags = UNPACKED_PREFIX_MATCH;
4743 }
4744 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004745#ifdef SQLITE_DEBUG
drh3da046d2013-11-11 03:24:11 +00004746 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
drh2b4ded92010-09-27 21:09:31 +00004747#endif
drh2dc06482013-12-11 00:59:10 +00004748 res = 0; /* Not needed. Only used to silence a warning. */
drh3da046d2013-11-11 03:24:11 +00004749 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
4750 if( pOp->opcode==OP_IdxLT ){
4751 res = -res;
4752 }else{
4753 assert( pOp->opcode==OP_IdxGE );
4754 res++;
4755 }
4756 if( res>0 ){
4757 pc = pOp->p2 - 1 ;
drh8721ce42001-11-07 14:22:00 +00004758 }
4759 break;
4760}
4761
drh98757152008-01-09 23:04:12 +00004762/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004763**
4764** Delete an entire database table or index whose root page in the database
4765** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004766**
drh98757152008-01-09 23:04:12 +00004767** The table being destroyed is in the main database file if P3==0. If
4768** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004769** that is used to store tables create using CREATE TEMPORARY TABLE.
4770**
drh205f48e2004-11-05 00:43:11 +00004771** If AUTOVACUUM is enabled then it is possible that another root page
4772** might be moved into the newly deleted root page in order to keep all
4773** root pages contiguous at the beginning of the database. The former
4774** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004775** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004776** movement was required (because the table being dropped was already
4777** the last one in the database) then a zero is stored in register P2.
4778** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004779**
drhb19a2bc2001-09-16 00:13:26 +00004780** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004781*/
drh98757152008-01-09 23:04:12 +00004782case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004783 int iMoved;
drh3765df42006-06-28 18:18:09 +00004784 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004785 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004786 int iDb;
drh3a949872012-09-18 13:20:13 +00004787
drh9e92a472013-06-27 17:40:30 +00004788 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004789#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004790 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004791 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004792 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4793 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4794 ){
danielk1977212b2182006-06-23 14:32:08 +00004795 iCnt++;
4796 }
4797 }
drh3765df42006-06-28 18:18:09 +00004798#else
danc0537fe2013-06-28 19:41:43 +00004799 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004800#endif
drh3c657212009-11-17 23:59:58 +00004801 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004802 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004803 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004804 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004805 }else{
drh856c1032009-06-02 15:21:42 +00004806 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004807 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004808 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh2dc06482013-12-11 00:59:10 +00004809 iMoved = 0; /* Not needed. Only to silence a warning. */
drh98757152008-01-09 23:04:12 +00004810 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004811 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004812 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004813#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004814 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004815 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4816 /* All OP_Destroy operations occur on the same btree */
4817 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4818 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004819 }
drh3765df42006-06-28 18:18:09 +00004820#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004821 }
drh5e00f6c2001-09-13 13:46:56 +00004822 break;
4823}
4824
danielk1977c7af4842008-10-27 13:59:33 +00004825/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004826**
4827** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004828** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004829** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004830**
drhf57b3392001-10-08 13:22:32 +00004831** The table being clear is in the main database file if P2==0. If
4832** P2==1 then the table to be clear is in the auxiliary database file
4833** that is used to store tables create using CREATE TEMPORARY TABLE.
4834**
shanebe217792009-03-05 04:20:31 +00004835** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004836** intkey table (an SQL table, not an index). In this case the row change
4837** count is incremented by the number of rows in the table being cleared.
4838** If P3 is greater than zero, then the value stored in register P3 is
4839** also incremented by the number of rows in the table being cleared.
4840**
drhb19a2bc2001-09-16 00:13:26 +00004841** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004842*/
drh9cbf3422008-01-17 16:22:13 +00004843case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004844 int nChange;
4845
4846 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004847 assert( p->readOnly==0 );
danf52bb8d2013-08-03 20:24:58 +00004848 assert( pOp->p1!=1 );
drhdddd7792011-04-03 18:19:25 +00004849 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004850 rc = sqlite3BtreeClearTable(
4851 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4852 );
4853 if( pOp->p3 ){
4854 p->nChange += nChange;
4855 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004856 assert( memIsValid(&aMem[pOp->p3]) );
4857 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004858 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004859 }
4860 }
drh5edc3122001-09-13 21:53:09 +00004861 break;
4862}
4863
drh4c583122008-01-04 22:01:03 +00004864/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004865** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00004866**
drh4c583122008-01-04 22:01:03 +00004867** Allocate a new table in the main database file if P1==0 or in the
4868** auxiliary database file if P1==1 or in an attached database if
4869** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004870** register P2
drh5b2fd562001-09-13 15:21:31 +00004871**
drhc6b52df2002-01-04 03:09:29 +00004872** The difference between a table and an index is this: A table must
4873** have a 4-byte integer key and can have arbitrary data. An index
4874** has an arbitrary key but no data.
4875**
drhb19a2bc2001-09-16 00:13:26 +00004876** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004877*/
drh4c583122008-01-04 22:01:03 +00004878/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004879** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00004880**
drh4c583122008-01-04 22:01:03 +00004881** Allocate a new index in the main database file if P1==0 or in the
4882** auxiliary database file if P1==1 or in an attached database if
4883** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004884** register P2.
drhf57b3392001-10-08 13:22:32 +00004885**
drhc6b52df2002-01-04 03:09:29 +00004886** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004887*/
drh4c583122008-01-04 22:01:03 +00004888case OP_CreateIndex: /* out2-prerelease */
4889case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004890 int pgno;
drhf328bc82004-05-10 23:29:49 +00004891 int flags;
drh234c39d2004-07-24 03:30:47 +00004892 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004893
4894 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004895 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004896 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00004897 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00004898 pDb = &db->aDb[pOp->p1];
4899 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004900 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004901 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004902 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004903 }else{
drhd4187c72010-08-30 22:15:45 +00004904 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004905 }
drh234c39d2004-07-24 03:30:47 +00004906 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004907 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004908 break;
4909}
4910
drh22645842011-03-24 01:34:03 +00004911/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004912**
4913** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004914** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004915**
4916** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004917** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004918*/
drh9cbf3422008-01-17 16:22:13 +00004919case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004920 int iDb;
4921 const char *zMaster;
4922 char *zSql;
4923 InitData initData;
4924
drhbdaec522011-04-04 00:14:43 +00004925 /* Any prepared statement that invokes this opcode will hold mutexes
4926 ** on every btree. This is a prerequisite for invoking
4927 ** sqlite3InitCallback().
4928 */
4929#ifdef SQLITE_DEBUG
4930 for(iDb=0; iDb<db->nDb; iDb++){
4931 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4932 }
4933#endif
drhbdaec522011-04-04 00:14:43 +00004934
drh856c1032009-06-02 15:21:42 +00004935 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004936 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004937 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004938 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004939 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004940 initData.db = db;
4941 initData.iDb = pOp->p1;
4942 initData.pzErrMsg = &p->zErrMsg;
4943 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004944 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004945 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4946 if( zSql==0 ){
4947 rc = SQLITE_NOMEM;
4948 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004949 assert( db->init.busy==0 );
4950 db->init.busy = 1;
4951 initData.rc = SQLITE_OK;
4952 assert( !db->mallocFailed );
4953 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4954 if( rc==SQLITE_OK ) rc = initData.rc;
4955 sqlite3DbFree(db, zSql);
4956 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004957 }
drh3c23a882007-01-09 14:01:13 +00004958 }
drh81028a42012-05-15 18:28:27 +00004959 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00004960 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004961 goto no_mem;
4962 }
drh234c39d2004-07-24 03:30:47 +00004963 break;
4964}
4965
drh8bfdf722009-06-19 14:06:03 +00004966#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004967/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004968**
4969** Read the sqlite_stat1 table for database P1 and load the content
4970** of that table into the internal index hash table. This will cause
4971** the analysis to be used when preparing all subsequent queries.
4972*/
drh9cbf3422008-01-17 16:22:13 +00004973case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004974 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4975 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004976 break;
4977}
drh8bfdf722009-06-19 14:06:03 +00004978#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004979
drh98757152008-01-09 23:04:12 +00004980/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004981**
4982** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004983** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004984** is dropped in order to keep the internal representation of the
4985** schema consistent with what is on disk.
4986*/
drh9cbf3422008-01-17 16:22:13 +00004987case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004988 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004989 break;
4990}
4991
drh98757152008-01-09 23:04:12 +00004992/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004993**
4994** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004995** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004996** is dropped in order to keep the internal representation of the
4997** schema consistent with what is on disk.
4998*/
drh9cbf3422008-01-17 16:22:13 +00004999case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005000 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005001 break;
5002}
5003
drh98757152008-01-09 23:04:12 +00005004/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005005**
5006** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005007** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00005008** is dropped in order to keep the internal representation of the
5009** schema consistent with what is on disk.
5010*/
drh9cbf3422008-01-17 16:22:13 +00005011case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005012 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005013 break;
5014}
5015
drh234c39d2004-07-24 03:30:47 +00005016
drhb7f91642004-10-31 02:22:47 +00005017#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005018/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005019**
drh98757152008-01-09 23:04:12 +00005020** Do an analysis of the currently open database. Store in
5021** register P1 the text of an error message describing any problems.
5022** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005023**
drh98757152008-01-09 23:04:12 +00005024** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005025** At most reg(P3) errors will be reported.
5026** In other words, the analysis stops as soon as reg(P1) errors are
5027** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005028**
drh79069752004-05-22 21:30:40 +00005029** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005030** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005031** total.
drh21504322002-06-25 13:16:02 +00005032**
drh98757152008-01-09 23:04:12 +00005033** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005034** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005035**
drh1dcdbc02007-01-27 02:24:54 +00005036** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005037*/
drhaaab5722002-02-19 13:39:21 +00005038case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005039 int nRoot; /* Number of tables to check. (Number of root pages.) */
5040 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5041 int j; /* Loop counter */
5042 int nErr; /* Number of errors reported */
5043 char *z; /* Text of the error report */
5044 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005045
drh1713afb2013-06-28 01:24:57 +00005046 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005047 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005048 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005049 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005050 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005051 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005052 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005053 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005054 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005055 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005056 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005057 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005058 }
5059 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005060 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005061 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00005062 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005063 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005064 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005065 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005066 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005067 if( nErr==0 ){
5068 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005069 }else if( z==0 ){
5070 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005071 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005072 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005073 }
drhb7654112008-01-12 12:48:07 +00005074 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005075 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005076 break;
5077}
drhb7f91642004-10-31 02:22:47 +00005078#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005079
drh3d4501e2008-12-04 20:40:10 +00005080/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005081** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005082**
drh3d4501e2008-12-04 20:40:10 +00005083** Insert the integer value held by register P2 into a boolean index
5084** held in register P1.
5085**
5086** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005087*/
drh93952eb2009-11-13 19:43:43 +00005088case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005089 pIn1 = &aMem[pOp->p1];
5090 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005091 assert( (pIn2->flags & MEM_Int)!=0 );
5092 if( (pIn1->flags & MEM_RowSet)==0 ){
5093 sqlite3VdbeMemSetRowSet(pIn1);
5094 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005095 }
drh93952eb2009-11-13 19:43:43 +00005096 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005097 break;
5098}
5099
5100/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005101** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005102**
5103** Extract the smallest value from boolean index P1 and put that value into
5104** register P3. Or, if boolean index P1 is initially empty, leave P3
5105** unchanged and jump to instruction P2.
5106*/
drh93952eb2009-11-13 19:43:43 +00005107case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005108 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005109
drh3c657212009-11-17 23:59:58 +00005110 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005111 if( (pIn1->flags & MEM_RowSet)==0
5112 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005113 ){
5114 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005115 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005116 pc = pOp->p2 - 1;
5117 }else{
5118 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005119 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005120 }
drh49afe3a2013-07-10 03:05:14 +00005121 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005122}
5123
drh1b26c7c2009-04-22 02:15:47 +00005124/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005125** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005126**
drhade97602009-04-21 15:05:18 +00005127** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005128** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005129** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005130** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005131** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005132**
drh1b26c7c2009-04-22 02:15:47 +00005133** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005134** of integers, where each set contains no duplicates. Each set
5135** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005136** must have P4==0, the final set P4=-1. P4 must be either -1 or
5137** non-negative. For non-negative values of P4 only the lower 4
5138** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005139**
5140** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005141** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005142** (b) when P4==-1 there is no need to insert the value, as it will
5143** never be tested for, and (c) when a value that is part of set X is
5144** inserted, there is no need to search to see if the same value was
5145** previously inserted as part of set X (only if it was previously
5146** inserted as part of some other set).
5147*/
drh1b26c7c2009-04-22 02:15:47 +00005148case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005149 int iSet;
5150 int exists;
5151
drh3c657212009-11-17 23:59:58 +00005152 pIn1 = &aMem[pOp->p1];
5153 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005154 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005155 assert( pIn3->flags&MEM_Int );
5156
drh1b26c7c2009-04-22 02:15:47 +00005157 /* If there is anything other than a rowset object in memory cell P1,
5158 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005159 */
drh733bf1b2009-04-22 00:47:00 +00005160 if( (pIn1->flags & MEM_RowSet)==0 ){
5161 sqlite3VdbeMemSetRowSet(pIn1);
5162 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005163 }
5164
5165 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005166 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005167 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005168 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5169 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005170 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005171 if( exists ){
5172 pc = pOp->p2 - 1;
5173 break;
5174 }
5175 }
5176 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005177 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005178 }
5179 break;
5180}
5181
drh5e00f6c2001-09-13 13:46:56 +00005182
danielk197793758c82005-01-21 08:13:14 +00005183#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005184
5185/* Opcode: Program P1 P2 P3 P4 *
5186**
dan76d462e2009-08-30 11:42:51 +00005187** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005188**
dan76d462e2009-08-30 11:42:51 +00005189** P1 contains the address of the memory cell that contains the first memory
5190** cell in an array of values used as arguments to the sub-program. P2
5191** contains the address to jump to if the sub-program throws an IGNORE
5192** exception using the RAISE() function. Register P3 contains the address
5193** of a memory cell in this (the parent) VM that is used to allocate the
5194** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005195**
5196** P4 is a pointer to the VM containing the trigger program.
5197*/
dan76d462e2009-08-30 11:42:51 +00005198case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005199 int nMem; /* Number of memory registers for sub-program */
5200 int nByte; /* Bytes of runtime space required for sub-program */
5201 Mem *pRt; /* Register to allocate runtime space */
5202 Mem *pMem; /* Used to iterate through memory cells */
5203 Mem *pEnd; /* Last memory cell in new array */
5204 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5205 SubProgram *pProgram; /* Sub-program to execute */
5206 void *t; /* Token identifying trigger */
5207
5208 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005209 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005210 assert( pProgram->nOp>0 );
5211
dan1da40a32009-09-19 17:00:31 +00005212 /* If the p5 flag is clear, then recursive invocation of triggers is
5213 ** disabled for backwards compatibility (p5 is set if this sub-program
5214 ** is really a trigger, not a foreign key action, and the flag set
5215 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005216 **
5217 ** It is recursive invocation of triggers, at the SQL level, that is
5218 ** disabled. In some cases a single trigger may generate more than one
5219 ** SubProgram (if the trigger may be executed with more than one different
5220 ** ON CONFLICT algorithm). SubProgram structures associated with a
5221 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005222 ** variable. */
5223 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005224 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005225 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5226 if( pFrame ) break;
5227 }
5228
danf5894502009-10-07 18:41:19 +00005229 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005230 rc = SQLITE_ERROR;
5231 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5232 break;
5233 }
5234
5235 /* Register pRt is used to store the memory required to save the state
5236 ** of the current program, and the memory required at runtime to execute
5237 ** the trigger program. If this trigger has been fired before, then pRt
5238 ** is already allocated. Otherwise, it must be initialized. */
5239 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005240 /* SubProgram.nMem is set to the number of memory cells used by the
5241 ** program stored in SubProgram.aOp. As well as these, one memory
5242 ** cell is required for each cursor used by the program. Set local
5243 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5244 */
dan65a7cd12009-09-01 12:16:01 +00005245 nMem = pProgram->nMem + pProgram->nCsr;
5246 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005247 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005248 + pProgram->nCsr * sizeof(VdbeCursor *)
5249 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005250 pFrame = sqlite3DbMallocZero(db, nByte);
5251 if( !pFrame ){
5252 goto no_mem;
5253 }
5254 sqlite3VdbeMemRelease(pRt);
5255 pRt->flags = MEM_Frame;
5256 pRt->u.pFrame = pFrame;
5257
5258 pFrame->v = p;
5259 pFrame->nChildMem = nMem;
5260 pFrame->nChildCsr = pProgram->nCsr;
5261 pFrame->pc = pc;
5262 pFrame->aMem = p->aMem;
5263 pFrame->nMem = p->nMem;
5264 pFrame->apCsr = p->apCsr;
5265 pFrame->nCursor = p->nCursor;
5266 pFrame->aOp = p->aOp;
5267 pFrame->nOp = p->nOp;
5268 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005269 pFrame->aOnceFlag = p->aOnceFlag;
5270 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005271
5272 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5273 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drhec86c722011-12-09 17:27:51 +00005274 pMem->flags = MEM_Invalid;
dan165921a2009-08-28 18:53:45 +00005275 pMem->db = db;
5276 }
5277 }else{
5278 pFrame = pRt->u.pFrame;
5279 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5280 assert( pProgram->nCsr==pFrame->nChildCsr );
5281 assert( pc==pFrame->pc );
5282 }
5283
5284 p->nFrame++;
5285 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005286 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005287 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005288 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005289 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005290 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005291 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005292 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005293 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005294 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005295 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005296 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5297 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005298 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005299 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005300
5301 break;
5302}
5303
dan76d462e2009-08-30 11:42:51 +00005304/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005305**
dan76d462e2009-08-30 11:42:51 +00005306** This opcode is only ever present in sub-programs called via the
5307** OP_Program instruction. Copy a value currently stored in a memory
5308** cell of the calling (parent) frame to cell P2 in the current frames
5309** address space. This is used by trigger programs to access the new.*
5310** and old.* values.
dan165921a2009-08-28 18:53:45 +00005311**
dan76d462e2009-08-30 11:42:51 +00005312** The address of the cell in the parent frame is determined by adding
5313** the value of the P1 argument to the value of the P1 argument to the
5314** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005315*/
dan76d462e2009-08-30 11:42:51 +00005316case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005317 VdbeFrame *pFrame;
5318 Mem *pIn;
5319 pFrame = p->pFrame;
5320 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005321 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5322 break;
5323}
5324
danielk197793758c82005-01-21 08:13:14 +00005325#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005326
dan1da40a32009-09-19 17:00:31 +00005327#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005328/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005329** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005330**
dan0ff297e2009-09-25 17:03:14 +00005331** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5332** If P1 is non-zero, the database constraint counter is incremented
5333** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005334** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005335*/
dan32b09f22009-09-23 17:29:59 +00005336case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005337 if( db->flags & SQLITE_DeferFKs ){
5338 db->nDeferredImmCons += pOp->p2;
5339 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005340 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005341 }else{
dan0ff297e2009-09-25 17:03:14 +00005342 p->nFkConstraint += pOp->p2;
5343 }
5344 break;
5345}
5346
5347/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005348** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005349**
5350** This opcode tests if a foreign key constraint-counter is currently zero.
5351** If so, jump to instruction P2. Otherwise, fall through to the next
5352** instruction.
5353**
5354** If P1 is non-zero, then the jump is taken if the database constraint-counter
5355** is zero (the one that counts deferred constraint violations). If P1 is
5356** zero, the jump is taken if the statement constraint-counter is zero
5357** (immediate foreign key constraint violations).
5358*/
5359case OP_FkIfZero: { /* jump */
5360 if( pOp->p1 ){
drh648e2642013-07-11 15:03:32 +00005361 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005362 }else{
drh648e2642013-07-11 15:03:32 +00005363 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005364 }
dan1da40a32009-09-19 17:00:31 +00005365 break;
5366}
5367#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5368
drh205f48e2004-11-05 00:43:11 +00005369#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005370/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005371** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005372**
dan76d462e2009-08-30 11:42:51 +00005373** P1 is a register in the root frame of this VM (the root frame is
5374** different from the current frame if this instruction is being executed
5375** within a sub-program). Set the value of register P1 to the maximum of
5376** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005377**
5378** This instruction throws an error if the memory cell is not initially
5379** an integer.
5380*/
dan76d462e2009-08-30 11:42:51 +00005381case OP_MemMax: { /* in2 */
dan76d462e2009-08-30 11:42:51 +00005382 VdbeFrame *pFrame;
5383 if( p->pFrame ){
5384 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5385 pIn1 = &pFrame->aMem[pOp->p1];
5386 }else{
drha6c2ed92009-11-14 23:22:23 +00005387 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005388 }
drhec86c722011-12-09 17:27:51 +00005389 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005390 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005391 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005392 sqlite3VdbeMemIntegerify(pIn2);
5393 if( pIn1->u.i<pIn2->u.i){
5394 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005395 }
5396 break;
5397}
5398#endif /* SQLITE_OMIT_AUTOINCREMENT */
5399
drh98757152008-01-09 23:04:12 +00005400/* Opcode: IfPos P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005401** Synopsis: if r[P1]>0 goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005402**
drh98757152008-01-09 23:04:12 +00005403** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005404**
drh98757152008-01-09 23:04:12 +00005405** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005406** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005407*/
drh9cbf3422008-01-17 16:22:13 +00005408case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005409 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005410 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005411 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005412 pc = pOp->p2 - 1;
5413 }
5414 break;
5415}
5416
drh98757152008-01-09 23:04:12 +00005417/* Opcode: IfNeg P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005418** Synopsis: if r[P1]<0 goto P2
drh15007a92006-01-08 18:10:17 +00005419**
drh98757152008-01-09 23:04:12 +00005420** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005421**
drh98757152008-01-09 23:04:12 +00005422** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005423** not contain an integer. An assertion fault will result if you try.
5424*/
drh9cbf3422008-01-17 16:22:13 +00005425case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005426 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005427 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005428 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005429 pc = pOp->p2 - 1;
5430 }
5431 break;
5432}
5433
drh9b918ed2009-11-12 03:13:26 +00005434/* Opcode: IfZero P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005435** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
drhec7429a2005-10-06 16:53:14 +00005436**
drh9b918ed2009-11-12 03:13:26 +00005437** The register P1 must contain an integer. Add literal P3 to the
5438** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005439**
drh98757152008-01-09 23:04:12 +00005440** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005441** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005442*/
drh9cbf3422008-01-17 16:22:13 +00005443case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005444 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005445 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005446 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005447 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005448 pc = pOp->p2 - 1;
5449 }
5450 break;
5451}
5452
drh98757152008-01-09 23:04:12 +00005453/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005454** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005455**
drh0bce8352002-02-28 00:41:10 +00005456** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005457** function has P5 arguments. P4 is a pointer to the FuncDef
5458** structure that specifies the function. Use register
5459** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005460**
drh98757152008-01-09 23:04:12 +00005461** The P5 arguments are taken from register P2 and its
5462** successors.
drhe5095352002-02-24 03:25:14 +00005463*/
drh9cbf3422008-01-17 16:22:13 +00005464case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005465 int n;
drhe5095352002-02-24 03:25:14 +00005466 int i;
drhc54a6172009-06-02 16:06:03 +00005467 Mem *pMem;
5468 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005469 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005470 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005471
drh856c1032009-06-02 15:21:42 +00005472 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005473 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005474 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005475 apVal = p->apArg;
5476 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005477 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005478 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005479 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005480 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005481 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005482 }
danielk19772dca4ac2008-01-03 11:50:29 +00005483 ctx.pFunc = pOp->p4.pFunc;
dan3bc9f742013-08-15 16:18:39 +00005484 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005485 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005486 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005487 ctx.s.flags = MEM_Null;
5488 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005489 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005490 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005491 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005492 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005493 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005494 ctx.skipFlag = 0;
drhd36e1042013-09-06 13:10:12 +00005495 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005496 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005497 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005498 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005499 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005500 }
drhee9ff672010-09-03 18:50:48 +00005501 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005502 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005503 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005504 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005505 }
drh7a957892012-02-02 17:35:43 +00005506 if( ctx.skipFlag ){
5507 assert( pOp[-1].opcode==OP_CollSeq );
5508 i = pOp[-1].p1;
5509 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5510 }
drhbdaec522011-04-04 00:14:43 +00005511
drh90669c12006-01-20 15:45:36 +00005512 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005513
drh5e00f6c2001-09-13 13:46:56 +00005514 break;
5515}
5516
drh98757152008-01-09 23:04:12 +00005517/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005518** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005519**
drh13449892005-09-07 21:22:45 +00005520** Execute the finalizer function for an aggregate. P1 is
5521** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005522**
5523** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005524** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005525** argument is not used by this opcode. It is only there to disambiguate
5526** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005527** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005528** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005529*/
drh9cbf3422008-01-17 16:22:13 +00005530case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005531 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005532 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005533 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005534 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005535 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005536 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005537 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005538 }
drh2dca8682008-03-21 17:13:13 +00005539 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005540 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005541 if( sqlite3VdbeMemTooBig(pMem) ){
5542 goto too_big;
5543 }
drh5e00f6c2001-09-13 13:46:56 +00005544 break;
5545}
5546
dan5cf53532010-05-01 16:40:20 +00005547#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005548/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005549**
5550** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005551** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005552** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5553** SQLITE_BUSY or not, respectively. Write the number of pages in the
5554** WAL after the checkpoint into mem[P3+1] and the number of pages
5555** in the WAL that have been checkpointed after the checkpoint
5556** completes into mem[P3+2]. However on an error, mem[P3+1] and
5557** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005558*/
5559case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005560 int i; /* Loop counter */
5561 int aRes[3]; /* Results */
5562 Mem *pMem; /* Write results here */
5563
drh9e92a472013-06-27 17:40:30 +00005564 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005565 aRes[0] = 0;
5566 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005567 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5568 || pOp->p2==SQLITE_CHECKPOINT_FULL
5569 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5570 );
drh30aa3b92011-02-07 23:56:01 +00005571 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005572 if( rc==SQLITE_BUSY ){
5573 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005574 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005575 }
drh30aa3b92011-02-07 23:56:01 +00005576 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5577 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5578 }
dan7c246102010-04-12 19:00:29 +00005579 break;
5580};
dan5cf53532010-05-01 16:40:20 +00005581#endif
drh5e00f6c2001-09-13 13:46:56 +00005582
drhcac29a62010-07-02 19:36:52 +00005583#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005584/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005585**
5586** Change the journal mode of database P1 to P3. P3 must be one of the
5587** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5588** modes (delete, truncate, persist, off and memory), this is a simple
5589** operation. No IO is required.
5590**
5591** If changing into or out of WAL mode the procedure is more complicated.
5592**
5593** Write a string containing the final journal-mode to register P2.
5594*/
drhd80b2332010-05-01 00:59:37 +00005595case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005596 Btree *pBt; /* Btree to change journal mode of */
5597 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005598 int eNew; /* New journal mode */
5599 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005600#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005601 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005602#endif
dane04dc882010-04-20 18:53:15 +00005603
drhd80b2332010-05-01 00:59:37 +00005604 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005605 assert( eNew==PAGER_JOURNALMODE_DELETE
5606 || eNew==PAGER_JOURNALMODE_TRUNCATE
5607 || eNew==PAGER_JOURNALMODE_PERSIST
5608 || eNew==PAGER_JOURNALMODE_OFF
5609 || eNew==PAGER_JOURNALMODE_MEMORY
5610 || eNew==PAGER_JOURNALMODE_WAL
5611 || eNew==PAGER_JOURNALMODE_QUERY
5612 );
5613 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005614 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005615
dane04dc882010-04-20 18:53:15 +00005616 pBt = db->aDb[pOp->p1].pBt;
5617 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005618 eOld = sqlite3PagerGetJournalMode(pPager);
5619 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5620 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005621
5622#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005623 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005624
drhd80b2332010-05-01 00:59:37 +00005625 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005626 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005627 */
5628 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005629 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005630 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005631 ){
drh0b9b4302010-06-11 17:01:24 +00005632 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005633 }
5634
drh0b9b4302010-06-11 17:01:24 +00005635 if( (eNew!=eOld)
5636 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5637 ){
danc0537fe2013-06-28 19:41:43 +00005638 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005639 rc = SQLITE_ERROR;
5640 sqlite3SetString(&p->zErrMsg, db,
5641 "cannot change %s wal mode from within a transaction",
5642 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5643 );
5644 break;
5645 }else{
5646
5647 if( eOld==PAGER_JOURNALMODE_WAL ){
5648 /* If leaving WAL mode, close the log file. If successful, the call
5649 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5650 ** file. An EXCLUSIVE lock may still be held on the database file
5651 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005652 */
drh0b9b4302010-06-11 17:01:24 +00005653 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005654 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005655 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005656 }
drh242c4f72010-06-22 14:49:39 +00005657 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5658 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5659 ** as an intermediate */
5660 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005661 }
5662
5663 /* Open a transaction on the database file. Regardless of the journal
5664 ** mode, this transaction always uses a rollback journal.
5665 */
5666 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5667 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005668 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005669 }
5670 }
5671 }
dan5cf53532010-05-01 16:40:20 +00005672#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005673
dand956efe2010-06-18 16:13:45 +00005674 if( rc ){
dand956efe2010-06-18 16:13:45 +00005675 eNew = eOld;
5676 }
drh0b9b4302010-06-11 17:01:24 +00005677 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005678
dane04dc882010-04-20 18:53:15 +00005679 pOut = &aMem[pOp->p2];
5680 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005681 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005682 pOut->n = sqlite3Strlen30(pOut->z);
5683 pOut->enc = SQLITE_UTF8;
5684 sqlite3VdbeChangeEncoding(pOut, encoding);
5685 break;
drhcac29a62010-07-02 19:36:52 +00005686};
5687#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005688
drhfdbcdee2007-03-27 14:44:50 +00005689#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005690/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005691**
5692** Vacuum the entire database. This opcode will cause other virtual
5693** machines to be created and run. It may not be called from within
5694** a transaction.
5695*/
drh9cbf3422008-01-17 16:22:13 +00005696case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005697 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005698 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005699 break;
5700}
drh154d4b22006-09-21 11:02:16 +00005701#endif
drh6f8c91c2003-12-07 00:24:35 +00005702
danielk1977dddbcdc2007-04-26 14:42:34 +00005703#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005704/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005705**
5706** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005707** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005708** P2. Otherwise, fall through to the next instruction.
5709*/
drh9cbf3422008-01-17 16:22:13 +00005710case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005711 Btree *pBt;
5712
5713 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005714 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00005715 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005716 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005717 rc = sqlite3BtreeIncrVacuum(pBt);
5718 if( rc==SQLITE_DONE ){
5719 pc = pOp->p2 - 1;
5720 rc = SQLITE_OK;
5721 }
5722 break;
5723}
5724#endif
5725
drh98757152008-01-09 23:04:12 +00005726/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005727**
5728** Cause precompiled statements to become expired. An expired statement
5729** fails with an error code of SQLITE_SCHEMA if it is ever executed
5730** (via sqlite3_step()).
5731**
5732** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5733** then only the currently executing statement is affected.
5734*/
drh9cbf3422008-01-17 16:22:13 +00005735case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005736 if( !pOp->p1 ){
5737 sqlite3ExpirePreparedStatements(db);
5738 }else{
5739 p->expired = 1;
5740 }
5741 break;
5742}
5743
danielk1977c00da102006-01-07 13:21:04 +00005744#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005745/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005746** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00005747**
5748** Obtain a lock on a particular table. This instruction is only used when
5749** the shared-cache feature is enabled.
5750**
danielk197796d48e92009-06-29 06:00:37 +00005751** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005752** on which the lock is acquired. A readlock is obtained if P3==0 or
5753** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005754**
5755** P2 contains the root-page of the table to lock.
5756**
drh66a51672008-01-03 00:01:23 +00005757** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005758** used to generate an error message if the lock cannot be obtained.
5759*/
drh9cbf3422008-01-17 16:22:13 +00005760case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005761 u8 isWriteLock = (u8)pOp->p3;
5762 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5763 int p1 = pOp->p1;
5764 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005765 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005766 assert( isWriteLock==0 || isWriteLock==1 );
5767 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5768 if( (rc&0xFF)==SQLITE_LOCKED ){
5769 const char *z = pOp->p4.z;
5770 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5771 }
danielk1977c00da102006-01-07 13:21:04 +00005772 }
5773 break;
5774}
drhb9bb7c12006-06-11 23:41:55 +00005775#endif /* SQLITE_OMIT_SHARED_CACHE */
5776
5777#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005778/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005779**
danielk19773e3a84d2008-08-01 17:37:40 +00005780** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5781** xBegin method for that table.
5782**
5783** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005784** within a callback to a virtual table xSync() method. If it is, the error
5785** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005786*/
drh9cbf3422008-01-17 16:22:13 +00005787case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005788 VTable *pVTab;
5789 pVTab = pOp->p4.pVtab;
5790 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00005791 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005792 break;
5793}
5794#endif /* SQLITE_OMIT_VIRTUALTABLE */
5795
5796#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005797/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005798**
drh66a51672008-01-03 00:01:23 +00005799** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005800** for that table.
5801*/
drh9cbf3422008-01-17 16:22:13 +00005802case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005803 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005804 break;
5805}
5806#endif /* SQLITE_OMIT_VIRTUALTABLE */
5807
5808#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005809/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005810**
drh66a51672008-01-03 00:01:23 +00005811** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005812** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005813*/
drh9cbf3422008-01-17 16:22:13 +00005814case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005815 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005816 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005817 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005818 break;
5819}
5820#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005821
drh9eff6162006-06-12 21:59:13 +00005822#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005823/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005824**
drh66a51672008-01-03 00:01:23 +00005825** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005826** P1 is a cursor number. This opcode opens a cursor to the virtual
5827** table and stores that cursor in P1.
5828*/
drh9cbf3422008-01-17 16:22:13 +00005829case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005830 VdbeCursor *pCur;
5831 sqlite3_vtab_cursor *pVtabCursor;
5832 sqlite3_vtab *pVtab;
5833 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005834
drh1713afb2013-06-28 01:24:57 +00005835 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005836 pCur = 0;
5837 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005838 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005839 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005840 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005841 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00005842 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005843 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005844 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005845 pVtabCursor->pVtab = pVtab;
5846
mistachkin48864df2013-03-21 21:20:32 +00005847 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005848 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005849 if( pCur ){
5850 pCur->pVtabCursor = pVtabCursor;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005851 }else{
drh17435752007-08-16 04:30:38 +00005852 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005853 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005854 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005855 }
drh9eff6162006-06-12 21:59:13 +00005856 break;
5857}
5858#endif /* SQLITE_OMIT_VIRTUALTABLE */
5859
5860#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005861/* Opcode: VFilter P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005862** Synopsis: iPlan=r[P3] zPlan='P4'
drh9eff6162006-06-12 21:59:13 +00005863**
5864** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5865** the filtered result set is empty.
5866**
drh66a51672008-01-03 00:01:23 +00005867** P4 is either NULL or a string that was generated by the xBestIndex
5868** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005869** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005870**
drh9eff6162006-06-12 21:59:13 +00005871** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005872** by P1. The integer query plan parameter to xFilter is stored in register
5873** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005874** xFilter method. Registers P3+2..P3+1+argc are the argc
5875** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005876** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005877**
danielk19776dbee812008-01-03 18:39:41 +00005878** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005879*/
drh9cbf3422008-01-17 16:22:13 +00005880case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005881 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005882 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005883 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005884 Mem *pQuery;
5885 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005886 sqlite3_vtab_cursor *pVtabCursor;
5887 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005888 VdbeCursor *pCur;
5889 int res;
5890 int i;
5891 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005892
drha6c2ed92009-11-14 23:22:23 +00005893 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005894 pArgc = &pQuery[1];
5895 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005896 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005897 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005898 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005899 pVtabCursor = pCur->pVtabCursor;
5900 pVtab = pVtabCursor->pVtab;
5901 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005902
drh9cbf3422008-01-17 16:22:13 +00005903 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005904 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005905 nArg = (int)pArgc->u.i;
5906 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005907
drh644a5292006-12-20 14:53:38 +00005908 /* Invoke the xFilter method */
5909 {
drh856c1032009-06-02 15:21:42 +00005910 res = 0;
5911 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005912 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005913 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005914 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005915 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005916
danielk1977be718892006-06-23 08:05:19 +00005917 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005918 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005919 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00005920 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005921 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005922 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005923 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005924
danielk1977a298e902006-06-22 09:53:48 +00005925 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005926 pc = pOp->p2 - 1;
5927 }
5928 }
drh1d454a32008-01-31 19:34:51 +00005929 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005930
drh9eff6162006-06-12 21:59:13 +00005931 break;
5932}
5933#endif /* SQLITE_OMIT_VIRTUALTABLE */
5934
5935#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005936/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005937** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00005938**
drh2133d822008-01-03 18:44:59 +00005939** Store the value of the P2-th column of
5940** the row of the virtual-table that the
5941** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005942*/
5943case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005944 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005945 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005946 Mem *pDest;
5947 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005948
drhdfe88ec2008-11-03 20:55:06 +00005949 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005950 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00005951 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005952 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005953 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005954 if( pCur->nullRow ){
5955 sqlite3VdbeMemSetNull(pDest);
5956 break;
5957 }
danielk19773e3a84d2008-08-01 17:37:40 +00005958 pVtab = pCur->pVtabCursor->pVtab;
5959 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005960 assert( pModule->xColumn );
5961 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005962
5963 /* The output cell may already have a buffer allocated. Move
5964 ** the current contents to sContext.s so in case the user-function
5965 ** can use the already allocated buffer instead of allocating a
5966 ** new one.
5967 */
5968 sqlite3VdbeMemMove(&sContext.s, pDest);
5969 MemSetTypeFlag(&sContext.s, MEM_Null);
5970
drhde4fcfd2008-01-19 23:50:26 +00005971 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00005972 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005973 if( sContext.isError ){
5974 rc = sContext.isError;
5975 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005976
drhde4fcfd2008-01-19 23:50:26 +00005977 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005978 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005979 ** dynamic allocation in sContext.s (a Mem struct) is released.
5980 */
5981 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005982 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005983 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005984 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005985
drhde4fcfd2008-01-19 23:50:26 +00005986 if( sqlite3VdbeMemTooBig(pDest) ){
5987 goto too_big;
5988 }
drh9eff6162006-06-12 21:59:13 +00005989 break;
5990}
5991#endif /* SQLITE_OMIT_VIRTUALTABLE */
5992
5993#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005994/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005995**
5996** Advance virtual table P1 to the next row in its result set and
5997** jump to instruction P2. Or, if the virtual table has reached
5998** the end of its result set, then fall through to the next instruction.
5999*/
drh9cbf3422008-01-17 16:22:13 +00006000case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006001 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006002 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006003 int res;
drh856c1032009-06-02 15:21:42 +00006004 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006005
drhc54a6172009-06-02 16:06:03 +00006006 res = 0;
drh856c1032009-06-02 15:21:42 +00006007 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006008 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006009 if( pCur->nullRow ){
6010 break;
6011 }
danielk19773e3a84d2008-08-01 17:37:40 +00006012 pVtab = pCur->pVtabCursor->pVtab;
6013 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006014 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006015
drhde4fcfd2008-01-19 23:50:26 +00006016 /* Invoke the xNext() method of the module. There is no way for the
6017 ** underlying implementation to return an error if one occurs during
6018 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6019 ** data is available) and the error code returned when xColumn or
6020 ** some other method is next invoked on the save virtual table cursor.
6021 */
drhde4fcfd2008-01-19 23:50:26 +00006022 p->inVtabMethod = 1;
6023 rc = pModule->xNext(pCur->pVtabCursor);
6024 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006025 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006026 if( rc==SQLITE_OK ){
6027 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006028 }
6029
drhde4fcfd2008-01-19 23:50:26 +00006030 if( !res ){
6031 /* If there is data, jump to P2 */
6032 pc = pOp->p2 - 1;
6033 }
drh49afe3a2013-07-10 03:05:14 +00006034 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006035}
6036#endif /* SQLITE_OMIT_VIRTUALTABLE */
6037
danielk1977182c4ba2007-06-27 15:53:34 +00006038#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006039/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006040**
drh66a51672008-01-03 00:01:23 +00006041** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006042** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006043** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006044*/
drh9cbf3422008-01-17 16:22:13 +00006045case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006046 sqlite3_vtab *pVtab;
6047 Mem *pName;
6048
danielk1977595a5232009-07-24 17:58:53 +00006049 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006050 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006051 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006052 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006053 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006054 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006055 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006056 testcase( pName->enc==SQLITE_UTF8 );
6057 testcase( pName->enc==SQLITE_UTF16BE );
6058 testcase( pName->enc==SQLITE_UTF16LE );
6059 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6060 if( rc==SQLITE_OK ){
6061 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006062 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006063 p->expired = 0;
6064 }
danielk1977182c4ba2007-06-27 15:53:34 +00006065 break;
6066}
6067#endif
drh4cbdda92006-06-14 19:00:20 +00006068
6069#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006070/* Opcode: VUpdate P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006071** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006072**
drh66a51672008-01-03 00:01:23 +00006073** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006074** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006075** are contiguous memory cells starting at P3 to pass to the xUpdate
6076** invocation. The value in register (P3+P2-1) corresponds to the
6077** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006078**
6079** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006080** The argv[0] element (which corresponds to memory cell P3)
6081** is the rowid of a row to delete. If argv[0] is NULL then no
6082** deletion occurs. The argv[1] element is the rowid of the new
6083** row. This can be NULL to have the virtual table select the new
6084** rowid for itself. The subsequent elements in the array are
6085** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006086**
6087** If P2==1 then no insert is performed. argv[0] is the rowid of
6088** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006089**
6090** P1 is a boolean flag. If it is set to true and the xUpdate call
6091** is successful, then the value returned by sqlite3_last_insert_rowid()
6092** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00006093*/
drh9cbf3422008-01-17 16:22:13 +00006094case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006095 sqlite3_vtab *pVtab;
6096 sqlite3_module *pModule;
6097 int nArg;
6098 int i;
6099 sqlite_int64 rowid;
6100 Mem **apArg;
6101 Mem *pX;
6102
danb061d052011-04-25 18:49:57 +00006103 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6104 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6105 );
drh9e92a472013-06-27 17:40:30 +00006106 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006107 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006108 pModule = (sqlite3_module *)pVtab->pModule;
6109 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006110 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006111 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006112 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006113 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006114 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006115 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006116 assert( memIsValid(pX) );
6117 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00006118 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00006119 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006120 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006121 }
danb061d052011-04-25 18:49:57 +00006122 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006123 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006124 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006125 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006126 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006127 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006128 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006129 }
drhd91c1a12013-02-09 13:58:25 +00006130 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006131 if( pOp->p5==OE_Ignore ){
6132 rc = SQLITE_OK;
6133 }else{
6134 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6135 }
6136 }else{
6137 p->nChange++;
6138 }
danielk1977399918f2006-06-14 13:03:23 +00006139 }
drh4cbdda92006-06-14 19:00:20 +00006140 break;
danielk1977399918f2006-06-14 13:03:23 +00006141}
6142#endif /* SQLITE_OMIT_VIRTUALTABLE */
6143
danielk197759a93792008-05-15 17:48:20 +00006144#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6145/* Opcode: Pagecount P1 P2 * * *
6146**
6147** Write the current number of pages in database P1 to memory cell P2.
6148*/
6149case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006150 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006151 break;
6152}
6153#endif
6154
drh60ac3f42010-11-23 18:59:27 +00006155
6156#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6157/* Opcode: MaxPgcnt P1 P2 P3 * *
6158**
6159** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006160** Do not let the maximum page count fall below the current page count and
6161** do not change the maximum page count value if P3==0.
6162**
drh60ac3f42010-11-23 18:59:27 +00006163** Store the maximum page count after the change in register P2.
6164*/
6165case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006166 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006167 Btree *pBt;
6168
6169 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006170 newMax = 0;
6171 if( pOp->p3 ){
6172 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006173 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006174 }
6175 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006176 break;
6177}
6178#endif
6179
6180
drh949f9cd2008-01-12 21:35:57 +00006181#ifndef SQLITE_OMIT_TRACE
6182/* Opcode: Trace * * * P4 *
6183**
6184** If tracing is enabled (by the sqlite3_trace()) interface, then
6185** the UTF-8 string contained in P4 is emitted on the trace callback.
6186*/
6187case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00006188 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006189 char *z;
drh856c1032009-06-02 15:21:42 +00006190
drh37f58e92012-09-04 21:34:26 +00006191 if( db->xTrace
6192 && !p->doingRerun
6193 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6194 ){
drhc3f1d5f2011-05-30 23:42:16 +00006195 z = sqlite3VdbeExpandSql(p, zTrace);
6196 db->xTrace(db->pTraceArg, z);
6197 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006198 }
drh8f8b2312013-10-18 20:03:43 +00006199#ifdef SQLITE_USE_FCNTL_TRACE
6200 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6201 if( zTrace ){
6202 int i;
6203 for(i=0; i<db->nDb; i++){
6204 if( ((1<<i) & p->btreeMask)==0 ) continue;
6205 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6206 }
6207 }
6208#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006209#ifdef SQLITE_DEBUG
6210 if( (db->flags & SQLITE_SqlTrace)!=0
6211 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6212 ){
6213 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6214 }
6215#endif /* SQLITE_DEBUG */
drh949f9cd2008-01-12 21:35:57 +00006216 break;
6217}
6218#endif
6219
drh91fd4d42008-01-19 20:11:25 +00006220
6221/* Opcode: Noop * * * * *
6222**
6223** Do nothing. This instruction is often useful as a jump
6224** destination.
drh5e00f6c2001-09-13 13:46:56 +00006225*/
drh91fd4d42008-01-19 20:11:25 +00006226/*
6227** The magic Explain opcode are only inserted when explain==2 (which
6228** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6229** This opcode records information from the optimizer. It is the
6230** the same as a no-op. This opcodesnever appears in a real VM program.
6231*/
6232default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006233 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006234 break;
6235}
6236
6237/*****************************************************************************
6238** The cases of the switch statement above this line should all be indented
6239** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6240** readability. From this point on down, the normal indentation rules are
6241** restored.
6242*****************************************************************************/
6243 }
drh6e142f52000-06-08 13:36:40 +00006244
drh7b396862003-01-01 23:06:20 +00006245#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006246 {
shane9bcbdad2008-05-29 20:22:37 +00006247 u64 elapsed = sqlite3Hwtime() - start;
6248 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006249 pOp->cnt++;
6250#if 0
shane9bcbdad2008-05-29 20:22:37 +00006251 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006252 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006253#endif
6254 }
drh7b396862003-01-01 23:06:20 +00006255#endif
6256
drh6e142f52000-06-08 13:36:40 +00006257 /* The following code adds nothing to the actual functionality
6258 ** of the program. It is only here for testing and debugging.
6259 ** On the other hand, it does burn CPU cycles every time through
6260 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6261 */
6262#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006263 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006264
drhcf1023c2007-05-08 20:59:49 +00006265#ifdef SQLITE_DEBUG
drh84e55a82013-11-13 17:58:23 +00006266 if( db->flags & SQLITE_VdbeTrace ){
6267 if( rc!=0 ) printf("rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006268 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
drh84e55a82013-11-13 17:58:23 +00006269 registerTrace(pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006270 }
drh3c657212009-11-17 23:59:58 +00006271 if( pOp->opflags & OPFLG_OUT3 ){
drh84e55a82013-11-13 17:58:23 +00006272 registerTrace(pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006273 }
drh75897232000-05-29 14:26:00 +00006274 }
danielk1977b5402fb2005-01-12 07:15:04 +00006275#endif /* SQLITE_DEBUG */
6276#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006277 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006278
drha05a7222008-01-19 03:35:58 +00006279 /* If we reach this point, it means that execution is finished with
6280 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006281 */
drha05a7222008-01-19 03:35:58 +00006282vdbe_error_halt:
6283 assert( rc );
6284 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006285 testcase( sqlite3GlobalConfig.xLog!=0 );
6286 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6287 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006288 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006289 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6290 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006291 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006292 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006293 }
drh900b31e2007-08-28 02:27:51 +00006294
6295 /* This is the only way out of this procedure. We have to
6296 ** release the mutexes on btrees that were acquired at the
6297 ** top. */
6298vdbe_return:
drh99a66922011-05-13 18:51:42 +00006299 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006300 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006301 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006302 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006303 return rc;
6304
drh023ae032007-05-08 12:12:16 +00006305 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6306 ** is encountered.
6307 */
6308too_big:
drhf089aa42008-07-08 19:34:06 +00006309 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006310 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006311 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006312
drh98640a32007-06-07 19:08:32 +00006313 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006314 */
6315no_mem:
drh17435752007-08-16 04:30:38 +00006316 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006317 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006318 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006319 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006320
drhb86ccfb2003-01-28 23:13:10 +00006321 /* Jump to here for any other kind of fatal error. The "rc" variable
6322 ** should hold the error number.
6323 */
6324abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006325 assert( p->zErrMsg==0 );
6326 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006327 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006328 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006329 }
drha05a7222008-01-19 03:35:58 +00006330 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006331
danielk19776f8a5032004-05-10 10:34:51 +00006332 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006333 ** flag.
6334 */
6335abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006336 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006337 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006338 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006339 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006340 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006341}