blob: 602bd1b51816d3dbd23b837ab3166cabc6b2e88b [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 =
drhc54055b2009-11-13 17:05:53 +0000215 ROUND8(sizeof(VdbeCursor)) +
danielk1977cd3e8f72008-03-25 09:47:35 +0000216 (isBtreeCursor?sqlite3BtreeCursorSize():0) +
217 2*nField*sizeof(u32);
218
drh290c1942004-08-21 17:54:45 +0000219 assert( iCur<p->nCursor );
220 if( p->apCsr[iCur] ){
danielk1977be718892006-06-23 08:05:19 +0000221 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
danielk1977cd3e8f72008-03-25 09:47:35 +0000222 p->apCsr[iCur] = 0;
drh8c74a8c2002-08-25 19:20:40 +0000223 }
danielk1977cd3e8f72008-03-25 09:47:35 +0000224 if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
drhdfe88ec2008-11-03 20:55:06 +0000225 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
drhf25a5072009-11-18 23:01:25 +0000226 memset(pCx, 0, sizeof(VdbeCursor));
danielk197794eb6a12005-12-15 15:22:08 +0000227 pCx->iDb = iDb;
danielk1977cd3e8f72008-03-25 09:47:35 +0000228 pCx->nField = nField;
229 if( nField ){
drhc54055b2009-11-13 17:05:53 +0000230 pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
danielk1977cd3e8f72008-03-25 09:47:35 +0000231 }
232 if( isBtreeCursor ){
drhdfe88ec2008-11-03 20:55:06 +0000233 pCx->pCursor = (BtCursor*)
drhc54055b2009-11-13 17:05:53 +0000234 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
drhf25a5072009-11-18 23:01:25 +0000235 sqlite3BtreeCursorZero(pCx->pCursor);
danielk1977cd3e8f72008-03-25 09:47:35 +0000236 }
danielk197794eb6a12005-12-15 15:22:08 +0000237 }
drh4774b132004-06-12 20:12:51 +0000238 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000239}
240
danielk19773d1bfea2004-05-14 11:00:53 +0000241/*
drh29d72102006-02-09 22:13:41 +0000242** Try to convert a value into a numeric representation if we can
243** do so without loss of information. In other words, if the string
244** looks like a number, convert it into a number. If it does not
245** look like a number, leave it alone.
246*/
drhb21c8cd2007-08-21 19:33:56 +0000247static void applyNumericAffinity(Mem *pRec){
drh29d72102006-02-09 22:13:41 +0000248 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
drh9339da12010-09-30 00:50:49 +0000249 double rValue;
250 i64 iValue;
danb7dca7d2010-03-05 16:32:12 +0000251 u8 enc = pRec->enc;
drh9339da12010-09-30 00:50:49 +0000252 if( (pRec->flags&MEM_Str)==0 ) return;
253 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
shaneh5f1d6b62010-09-30 16:51:25 +0000254 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
drh9339da12010-09-30 00:50:49 +0000255 pRec->u.i = iValue;
256 pRec->flags |= MEM_Int;
257 }else{
258 pRec->r = rValue;
259 pRec->flags |= MEM_Real;
drh29d72102006-02-09 22:13:41 +0000260 }
261 }
262}
263
264/*
drh8a512562005-11-14 22:29:05 +0000265** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000266**
drh8a512562005-11-14 22:29:05 +0000267** SQLITE_AFF_INTEGER:
268** SQLITE_AFF_REAL:
269** SQLITE_AFF_NUMERIC:
270** Try to convert pRec to an integer representation or a
271** floating-point representation if an integer representation
272** is not possible. Note that the integer representation is
273** always preferred, even if the affinity is REAL, because
274** an integer representation is more space efficient on disk.
275**
276** SQLITE_AFF_TEXT:
277** Convert pRec to a text representation.
278**
279** SQLITE_AFF_NONE:
280** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000281*/
drh17435752007-08-16 04:30:38 +0000282static void applyAffinity(
drh17435752007-08-16 04:30:38 +0000283 Mem *pRec, /* The value to apply affinity to */
284 char affinity, /* The affinity to be applied */
285 u8 enc /* Use this text encoding */
286){
drh8a512562005-11-14 22:29:05 +0000287 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000288 /* Only attempt the conversion to TEXT if there is an integer or real
289 ** representation (blob and NULL do not get converted) but no string
290 ** representation.
291 */
292 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhb21c8cd2007-08-21 19:33:56 +0000293 sqlite3VdbeMemStringify(pRec, enc);
drh17c40292004-07-21 02:53:29 +0000294 }
295 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000296 }else if( affinity!=SQLITE_AFF_NONE ){
297 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
298 || affinity==SQLITE_AFF_NUMERIC );
drhb21c8cd2007-08-21 19:33:56 +0000299 applyNumericAffinity(pRec);
drh29d72102006-02-09 22:13:41 +0000300 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000301 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000302 }
danielk19773d1bfea2004-05-14 11:00:53 +0000303 }
304}
305
danielk1977aee18ef2005-03-09 12:26:50 +0000306/*
drh29d72102006-02-09 22:13:41 +0000307** Try to convert the type of a function argument or a result column
308** into a numeric representation. Use either INTEGER or REAL whichever
309** is appropriate. But only do the conversion if it is possible without
310** loss of information and return the revised type of the argument.
drh29d72102006-02-09 22:13:41 +0000311*/
312int sqlite3_value_numeric_type(sqlite3_value *pVal){
313 Mem *pMem = (Mem*)pVal;
drhe5a8a1d2010-11-18 12:31:24 +0000314 if( pMem->type==SQLITE_TEXT ){
315 applyNumericAffinity(pMem);
316 sqlite3VdbeMemStoreType(pMem);
317 }
drh29d72102006-02-09 22:13:41 +0000318 return pMem->type;
319}
320
321/*
danielk1977aee18ef2005-03-09 12:26:50 +0000322** Exported version of applyAffinity(). This one works on sqlite3_value*,
323** not the internal Mem* type.
324*/
danielk19771e536952007-08-16 10:09:01 +0000325void sqlite3ValueApplyAffinity(
danielk19771e536952007-08-16 10:09:01 +0000326 sqlite3_value *pVal,
327 u8 affinity,
328 u8 enc
329){
drhb21c8cd2007-08-21 19:33:56 +0000330 applyAffinity((Mem *)pVal, affinity, enc);
danielk1977aee18ef2005-03-09 12:26:50 +0000331}
332
danielk1977b5402fb2005-01-12 07:15:04 +0000333#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000334/*
danielk1977ca6b2912004-05-21 10:49:47 +0000335** Write a nice string representation of the contents of cell pMem
336** into buffer zBuf, length nBuf.
337*/
drh74161702006-02-24 02:53:49 +0000338void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000339 char *zCsr = zBuf;
340 int f = pMem->flags;
341
drh57196282004-10-06 15:41:16 +0000342 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000343
danielk1977ca6b2912004-05-21 10:49:47 +0000344 if( f&MEM_Blob ){
345 int i;
346 char c;
347 if( f & MEM_Dyn ){
348 c = 'z';
349 assert( (f & (MEM_Static|MEM_Ephem))==0 );
350 }else if( f & MEM_Static ){
351 c = 't';
352 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
353 }else if( f & MEM_Ephem ){
354 c = 'e';
355 assert( (f & (MEM_Static|MEM_Dyn))==0 );
356 }else{
357 c = 's';
358 }
359
drh5bb3eb92007-05-04 13:15:55 +0000360 sqlite3_snprintf(100, zCsr, "%c", c);
drhea678832008-12-10 19:26:22 +0000361 zCsr += sqlite3Strlen30(zCsr);
drh5bb3eb92007-05-04 13:15:55 +0000362 sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
drhea678832008-12-10 19:26:22 +0000363 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000364 for(i=0; i<16 && i<pMem->n; i++){
drh5bb3eb92007-05-04 13:15:55 +0000365 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
drhea678832008-12-10 19:26:22 +0000366 zCsr += sqlite3Strlen30(zCsr);
danielk1977ca6b2912004-05-21 10:49:47 +0000367 }
368 for(i=0; i<16 && i<pMem->n; i++){
369 char z = pMem->z[i];
370 if( z<32 || z>126 ) *zCsr++ = '.';
371 else *zCsr++ = z;
372 }
373
drhe718efe2007-05-10 21:14:03 +0000374 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000375 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000376 if( f & MEM_Zero ){
drh8df32842008-12-09 02:51:23 +0000377 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
drhea678832008-12-10 19:26:22 +0000378 zCsr += sqlite3Strlen30(zCsr);
drhfdf972a2007-05-02 13:30:27 +0000379 }
danielk1977b1bc9532004-05-22 03:05:33 +0000380 *zCsr = '\0';
381 }else if( f & MEM_Str ){
382 int j, k;
383 zBuf[0] = ' ';
384 if( f & MEM_Dyn ){
385 zBuf[1] = 'z';
386 assert( (f & (MEM_Static|MEM_Ephem))==0 );
387 }else if( f & MEM_Static ){
388 zBuf[1] = 't';
389 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
390 }else if( f & MEM_Ephem ){
391 zBuf[1] = 'e';
392 assert( (f & (MEM_Static|MEM_Dyn))==0 );
393 }else{
394 zBuf[1] = 's';
395 }
396 k = 2;
drh5bb3eb92007-05-04 13:15:55 +0000397 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
drhea678832008-12-10 19:26:22 +0000398 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000399 zBuf[k++] = '[';
400 for(j=0; j<15 && j<pMem->n; j++){
401 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000402 if( c>=0x20 && c<0x7f ){
403 zBuf[k++] = c;
404 }else{
405 zBuf[k++] = '.';
406 }
407 }
408 zBuf[k++] = ']';
drh5bb3eb92007-05-04 13:15:55 +0000409 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
drhea678832008-12-10 19:26:22 +0000410 k += sqlite3Strlen30(&zBuf[k]);
danielk1977b1bc9532004-05-22 03:05:33 +0000411 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000412 }
danielk1977ca6b2912004-05-21 10:49:47 +0000413}
414#endif
415
drh5b6afba2008-01-05 16:29:28 +0000416#ifdef SQLITE_DEBUG
417/*
418** Print the value of a register for tracing purposes:
419*/
420static void memTracePrint(FILE *out, Mem *p){
drh953f7612012-12-07 22:18:54 +0000421 if( p->flags & MEM_Invalid ){
422 fprintf(out, " undefined");
423 }else if( p->flags & MEM_Null ){
drh5b6afba2008-01-05 16:29:28 +0000424 fprintf(out, " NULL");
425 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
426 fprintf(out, " si:%lld", p->u.i);
427 }else if( p->flags & MEM_Int ){
428 fprintf(out, " i:%lld", p->u.i);
drh0b3bf922009-06-15 20:45:34 +0000429#ifndef SQLITE_OMIT_FLOATING_POINT
drh5b6afba2008-01-05 16:29:28 +0000430 }else if( p->flags & MEM_Real ){
431 fprintf(out, " r:%g", p->r);
drh0b3bf922009-06-15 20:45:34 +0000432#endif
drh733bf1b2009-04-22 00:47:00 +0000433 }else if( p->flags & MEM_RowSet ){
434 fprintf(out, " (rowset)");
drh5b6afba2008-01-05 16:29:28 +0000435 }else{
436 char zBuf[200];
437 sqlite3VdbeMemPrettyPrint(p, zBuf);
438 fprintf(out, " ");
439 fprintf(out, "%s", zBuf);
440 }
441}
442static void registerTrace(FILE *out, int iReg, Mem *p){
443 fprintf(out, "REG[%d] = ", iReg);
444 memTracePrint(out, p);
445 fprintf(out, "\n");
446}
447#endif
448
449#ifdef SQLITE_DEBUG
drhb21e7c72008-06-22 12:37:57 +0000450# define REGISTER_TRACE(R,M) if(p->trace)registerTrace(p->trace,R,M)
drh5b6afba2008-01-05 16:29:28 +0000451#else
452# define REGISTER_TRACE(R,M)
453#endif
454
danielk197784ac9d02004-05-18 09:58:06 +0000455
drh7b396862003-01-01 23:06:20 +0000456#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000457
458/*
459** hwtime.h contains inline assembler code for implementing
460** high-performance timing routines.
drh7b396862003-01-01 23:06:20 +0000461*/
shane9bcbdad2008-05-29 20:22:37 +0000462#include "hwtime.h"
463
drh7b396862003-01-01 23:06:20 +0000464#endif
465
drh8c74a8c2002-08-25 19:20:40 +0000466/*
drhcaec2f12003-01-07 02:47:47 +0000467** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000468** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000469** processing of the VDBE program is interrupted.
470**
471** This macro added to every instruction that does a jump in order to
472** implement a loop. This test used to be on every single instruction,
drhe4c88c02012-01-04 12:57:45 +0000473** but that meant we more testing than we needed. By only testing the
drhcaec2f12003-01-07 02:47:47 +0000474** flag on jump instructions, we get a (small) speed improvement.
475*/
476#define CHECK_FOR_INTERRUPT \
drh881feaa2006-07-26 01:39:30 +0000477 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
drhcaec2f12003-01-07 02:47:47 +0000478
479
danielk1977fd7f0452008-12-17 17:30:26 +0000480#ifndef NDEBUG
481/*
482** This function is only called from within an assert() expression. It
483** checks that the sqlite3.nTransaction variable is correctly set to
484** the number of non-transaction savepoints currently in the
485** linked list starting at sqlite3.pSavepoint.
486**
487** Usage:
488**
489** assert( checkSavepointCount(db) );
490*/
491static int checkSavepointCount(sqlite3 *db){
492 int n = 0;
493 Savepoint *p;
494 for(p=db->pSavepoint; p; p=p->pNext) n++;
495 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
496 return 1;
497}
498#endif
499
drhb9755982010-07-24 16:34:37 +0000500
501/*
drhb86ccfb2003-01-28 23:13:10 +0000502** Execute as much of a VDBE program as we can then return.
503**
danielk19774adee202004-05-08 08:23:19 +0000504** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000505** close the program with a final OP_Halt and to set up the callbacks
506** and the error message pointer.
507**
508** Whenever a row or result data is available, this routine will either
509** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000510** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000511**
512** If an attempt is made to open a locked database, then this routine
513** will either invoke the busy callback (if there is one) or it will
514** return SQLITE_BUSY.
515**
516** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000517** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000518** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
519**
520** If the callback ever returns non-zero, then the program exits
521** immediately. There will be no error message but the p->rc field is
522** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
523**
drh9468c7f2003-03-07 19:50:07 +0000524** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
525** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000526**
527** Other fatal errors return SQLITE_ERROR.
528**
danielk19774adee202004-05-08 08:23:19 +0000529** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000530** used to clean up the mess that was left behind.
531*/
danielk19774adee202004-05-08 08:23:19 +0000532int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000533 Vdbe *p /* The VDBE */
534){
shaneh84f4b2f2010-02-26 01:46:54 +0000535 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000536 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000537 Op *pOp; /* Current operation */
538 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000539 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000540 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000541 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000542 int iCompare = 0; /* Result of last OP_Compare operation */
543 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000544#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000545 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000546#endif
drha6c2ed92009-11-14 23:22:23 +0000547 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000548 Mem *pIn1 = 0; /* 1st input operand */
549 Mem *pIn2 = 0; /* 2nd input operand */
550 Mem *pIn3 = 0; /* 3rd input operand */
551 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000552 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000553 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000554#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000555 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000556 int origPc; /* Program counter at start of opcode */
557#endif
drh856c1032009-06-02 15:21:42 +0000558 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000559
drhca48c902008-01-18 14:08:24 +0000560 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000561 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000562 if( p->rc==SQLITE_NOMEM ){
563 /* This happens if a malloc() inside a call to sqlite3_column_text() or
564 ** sqlite3_column_text16() failed. */
565 goto no_mem;
566 }
drh3a840692003-01-29 22:58:26 +0000567 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000568 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000569 p->rc = SQLITE_OK;
drh95a7b3e2013-09-16 12:57:19 +0000570 p->iCurrentTime = 0;
drhb86ccfb2003-01-28 23:13:10 +0000571 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000572 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000573 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000574 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000575 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000576#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
577 if( db->xProgress ){
578 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000579 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000580 if( nProgressLimit==0 ){
581 nProgressLimit = db->nProgressOps;
582 }else{
583 nProgressLimit %= (unsigned)db->nProgressOps;
584 }
585 }
586#endif
drh3c23a882007-01-09 14:01:13 +0000587#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000588 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000589 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000590 int i;
591 printf("VDBE Program Listing:\n");
592 sqlite3VdbePrintSql(p);
593 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000594 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000595 }
596 }
danielk19772d1d86f2008-06-20 14:59:51 +0000597 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000598#endif
drhb86ccfb2003-01-28 23:13:10 +0000599 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000600 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000601 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000602#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000603 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000604 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000605#endif
drhbf159fa2013-06-25 22:01:22 +0000606 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000607 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000608
danielk19778b60e0f2005-01-12 09:10:39 +0000609 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000610 */
danielk19778b60e0f2005-01-12 09:10:39 +0000611#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000612 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000613 if( pc==0 ){
614 printf("VDBE Execution Trace:\n");
615 sqlite3VdbePrintSql(p);
616 }
danielk19774adee202004-05-08 08:23:19 +0000617 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000618 }
drh3f7d4e42004-07-24 14:35:58 +0000619#endif
620
drh6e142f52000-06-08 13:36:40 +0000621
drhf6038712004-02-08 18:07:34 +0000622 /* Check to see if we need to simulate an interrupt. This only happens
623 ** if we have a special test build.
624 */
625#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000626 if( sqlite3_interrupt_count>0 ){
627 sqlite3_interrupt_count--;
628 if( sqlite3_interrupt_count==0 ){
629 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000630 }
631 }
632#endif
633
drhb5b407e2012-08-29 10:28:43 +0000634 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000635 ** external allocations out of mem[p2] and set mem[p2] to be
636 ** an undefined integer. Opcodes will either fill in the integer
637 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000638 */
drha6c2ed92009-11-14 23:22:23 +0000639 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000640 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
641 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000642 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000643 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000644 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000645 VdbeMemRelease(pOut);
drh3c657212009-11-17 23:59:58 +0000646 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000647 }
drh3c657212009-11-17 23:59:58 +0000648
649 /* Sanity checking on other operands */
650#ifdef SQLITE_DEBUG
651 if( (pOp->opflags & OPFLG_IN1)!=0 ){
652 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +0000653 assert( pOp->p1<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000654 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000655 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
656 }
657 if( (pOp->opflags & OPFLG_IN2)!=0 ){
658 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000659 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000660 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000661 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
662 }
663 if( (pOp->opflags & OPFLG_IN3)!=0 ){
664 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000665 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000666 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000667 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
668 }
669 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
670 assert( pOp->p2>0 );
dan3bc9f742013-08-15 16:18:39 +0000671 assert( pOp->p2<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000672 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000673 }
674 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
675 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +0000676 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh2b4ded92010-09-27 21:09:31 +0000677 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000678 }
679#endif
drh93952eb2009-11-13 19:43:43 +0000680
drh75897232000-05-29 14:26:00 +0000681 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000682
drh5e00f6c2001-09-13 13:46:56 +0000683/*****************************************************************************
684** What follows is a massive switch statement where each case implements a
685** separate instruction in the virtual machine. If we follow the usual
686** indentation conventions, each case should be indented by 6 spaces. But
687** that is a lot of wasted space on the left margin. So the code within
688** the switch statement will break with convention and be flush-left. Another
689** big comment (similar to this one) will mark the point in the code where
690** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000691**
692** The formatting of each case is important. The makefile for SQLite
693** generates two C files "opcodes.h" and "opcodes.c" by scanning this
694** file looking for lines that begin with "case OP_". The opcodes.h files
695** will be filled with #defines that give unique integer values to each
696** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000697** each string is the symbolic name for the corresponding opcode. If the
698** case statement is followed by a comment of the form "/# same as ... #/"
699** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000700**
drh9cbf3422008-01-17 16:22:13 +0000701** Other keywords in the comment that follows each case are used to
702** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
703** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
704** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000705**
drhac82fcf2002-09-08 17:23:41 +0000706** Documentation about VDBE opcodes is generated by scanning this file
707** for lines of that contain "Opcode:". That line and all subsequent
708** comment lines are used in the generation of the opcode.html documentation
709** file.
710**
711** SUMMARY:
712**
713** Formatting is important to scripts that scan this file.
714** Do not deviate from the formatting style currently in use.
715**
drh5e00f6c2001-09-13 13:46:56 +0000716*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000717
drh9cbf3422008-01-17 16:22:13 +0000718/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000719**
720** An unconditional jump to address P2.
721** The next instruction executed will be
722** the one at index P2 from the beginning of
723** the program.
724*/
drh9cbf3422008-01-17 16:22:13 +0000725case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000726 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000727
728 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
729 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
730 ** completion. Check to see if sqlite3_interrupt() has been called
731 ** or if the progress callback needs to be invoked.
732 **
733 ** This code uses unstructured "goto" statements and does not look clean.
734 ** But that is not due to sloppy coding habits. The code is written this
735 ** way for performance, to avoid having to run the interrupt and progress
736 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
737 ** faster according to "valgrind --tool=cachegrind" */
738check_for_interrupt:
739 CHECK_FOR_INTERRUPT;
740#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
741 /* Call the progress callback if it is configured and the required number
742 ** of VDBE ops have been executed (either since this invocation of
743 ** sqlite3VdbeExec() or since last time the progress callback was called).
744 ** If the progress callback returns non-zero, exit the virtual machine with
745 ** a return code SQLITE_ABORT.
746 */
drh0d1961e2013-07-25 16:27:51 +0000747 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh49afe3a2013-07-10 03:05:14 +0000748 int prc;
749 prc = db->xProgress(db->pProgressArg);
750 if( prc!=0 ){
751 rc = SQLITE_INTERRUPT;
752 goto vdbe_error_halt;
753 }
drh0d1961e2013-07-25 16:27:51 +0000754 if( db->xProgress!=0 ){
755 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
756 }
drh49afe3a2013-07-10 03:05:14 +0000757 }
758#endif
759
drh5e00f6c2001-09-13 13:46:56 +0000760 break;
761}
drh75897232000-05-29 14:26:00 +0000762
drh2eb95372008-06-06 15:04:36 +0000763/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000764**
drh2eb95372008-06-06 15:04:36 +0000765** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000766** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000767*/
drhb8475df2011-12-09 16:21:19 +0000768case OP_Gosub: { /* jump */
dan3bc9f742013-08-15 16:18:39 +0000769 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drh3c657212009-11-17 23:59:58 +0000770 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000771 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000772 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000773 pIn1->flags = MEM_Int;
774 pIn1->u.i = pc;
775 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000776 pc = pOp->p2 - 1;
777 break;
778}
779
drh2eb95372008-06-06 15:04:36 +0000780/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000781**
drh2eb95372008-06-06 15:04:36 +0000782** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000783*/
drh2eb95372008-06-06 15:04:36 +0000784case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000785 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000786 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000787 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000788 break;
789}
790
drhe00ee6e2008-06-20 15:24:01 +0000791/* Opcode: Yield P1 * * * *
792**
793** Swap the program counter with the value in register P1.
794*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000795case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000796 int pcDest;
drh3c657212009-11-17 23:59:58 +0000797 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000798 assert( (pIn1->flags & MEM_Dyn)==0 );
799 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000800 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000801 pIn1->u.i = pc;
802 REGISTER_TRACE(pOp->p1, pIn1);
803 pc = pcDest;
804 break;
805}
806
drhf9c8ce32013-11-05 13:33:55 +0000807/* Opcode: HaltIfNull P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +0000808** Synopsis: if r[P3] null then halt
drh5053a792009-02-20 03:02:23 +0000809**
drhef8662b2011-06-20 21:47:58 +0000810** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000811** parameter P1, P2, and P4 as if this were a Halt instruction. If the
812** value in register P3 is not NULL, then this routine is a no-op.
drhf9c8ce32013-11-05 13:33:55 +0000813** The P5 parameter should be 1.
drh5053a792009-02-20 03:02:23 +0000814*/
815case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000816 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000817 if( (pIn3->flags & MEM_Null)==0 ) break;
818 /* Fall through into OP_Halt */
819}
drhe00ee6e2008-06-20 15:24:01 +0000820
drhf9c8ce32013-11-05 13:33:55 +0000821/* Opcode: Halt P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +0000822**
drh3d4501e2008-12-04 20:40:10 +0000823** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000824** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000825**
drh92f02c32004-09-02 14:57:08 +0000826** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
827** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
828** For errors, it can be some other value. If P1!=0 then P2 will determine
829** whether or not to rollback the current transaction. Do not rollback
830** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
831** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000832** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000833**
drh66a51672008-01-03 00:01:23 +0000834** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000835**
drhf9c8ce32013-11-05 13:33:55 +0000836** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
837**
838** 0: (no change)
839** 1: NOT NULL contraint failed: P4
840** 2: UNIQUE constraint failed: P4
841** 3: CHECK constraint failed: P4
842** 4: FOREIGN KEY constraint failed: P4
843**
844** If P5 is not zero and P4 is NULL, then everything after the ":" is
845** omitted.
846**
drh9cfcf5d2002-01-29 18:41:24 +0000847** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000848** every program. So a jump past the last instruction of the program
849** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000850*/
drh9cbf3422008-01-17 16:22:13 +0000851case OP_Halt: {
drhf9c8ce32013-11-05 13:33:55 +0000852 const char *zType;
853 const char *zLogFmt;
854
dan165921a2009-08-28 18:53:45 +0000855 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000856 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000857 VdbeFrame *pFrame = p->pFrame;
858 p->pFrame = pFrame->pParent;
859 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000860 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000861 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000862 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000863 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000864 /* Instruction pc is the OP_Program that invoked the sub-program
865 ** currently being halted. If the p2 instruction of this OP_Halt
866 ** instruction is set to OE_Ignore, then the sub-program is throwing
867 ** an IGNORE exception. In this case jump to the address specified
868 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000869 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000870 }
drhbbe879d2009-11-14 18:04:35 +0000871 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000872 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000873 break;
874 }
drhf9c8ce32013-11-05 13:33:55 +0000875 if( pOp->p5 ){
876 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
877 "FOREIGN KEY" };
878 assert( pOp->p5>=1 && pOp->p5<=4 );
879 testcase( pOp->p5==1 );
880 testcase( pOp->p5==2 );
881 testcase( pOp->p5==3 );
882 testcase( pOp->p5==4 );
883 zType = azType[pOp->p5-1];
884 }else{
885 zType = 0;
886 }
drh92f02c32004-09-02 14:57:08 +0000887 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000888 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000889 p->pc = pc;
drhf9c8ce32013-11-05 13:33:55 +0000890 if( p->rc ){
891 zLogFmt = "abort at %d in [%s]: %s";
892 if( zType && pOp->p4.z ){
893 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
894 zType, pOp->p4.z);
895 }else if( pOp->p4.z ){
896 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
897 }else if( zType ){
898 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
899 }else{
900 zLogFmt = "abort at %d in [%s]";
901 }
902 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
drh9cfcf5d2002-01-29 18:41:24 +0000903 }
drh92f02c32004-09-02 14:57:08 +0000904 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000905 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000906 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000907 p->rc = rc = SQLITE_BUSY;
908 }else{
drhd91c1a12013-02-09 13:58:25 +0000909 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000910 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000911 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000912 }
drh900b31e2007-08-28 02:27:51 +0000913 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000914}
drhc61053b2000-06-04 12:58:36 +0000915
drh4c583122008-01-04 22:01:03 +0000916/* Opcode: Integer P1 P2 * * *
drh81316f82013-10-29 20:40:47 +0000917** Synopsis: r[P2]=P1
drh5e00f6c2001-09-13 13:46:56 +0000918**
drh9cbf3422008-01-17 16:22:13 +0000919** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000920*/
drh4c583122008-01-04 22:01:03 +0000921case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000922 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000923 break;
924}
925
drh4c583122008-01-04 22:01:03 +0000926/* Opcode: Int64 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000927** Synopsis: r[P2]=P4
drh29dda4a2005-07-21 18:23:20 +0000928**
drh66a51672008-01-03 00:01:23 +0000929** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000930** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000931*/
drh4c583122008-01-04 22:01:03 +0000932case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000933 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000934 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000935 break;
936}
drh4f26d6c2004-05-26 23:25:30 +0000937
drh13573c72010-01-12 17:04:07 +0000938#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000939/* Opcode: Real * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000940** Synopsis: r[P2]=P4
drhf4479502004-05-27 03:12:53 +0000941**
drh4c583122008-01-04 22:01:03 +0000942** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000943** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000944*/
drh4c583122008-01-04 22:01:03 +0000945case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
946 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000947 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000948 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000949 break;
950}
drh13573c72010-01-12 17:04:07 +0000951#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000952
drh3c84ddf2008-01-09 02:15:38 +0000953/* Opcode: String8 * P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000954** Synopsis: r[P2]='P4'
danielk1977cbb18d22004-05-28 11:37:27 +0000955**
drh66a51672008-01-03 00:01:23 +0000956** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000957** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000958*/
drh4c583122008-01-04 22:01:03 +0000959case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000960 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000961 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000962 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000963
964#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000965 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000966 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
967 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000968 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000969 assert( pOut->zMalloc==pOut->z );
970 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000971 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000972 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000973 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000974 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000975 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000976 }
drh66a51672008-01-03 00:01:23 +0000977 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000978 pOp->p4.z = pOut->z;
979 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000980 }
danielk197793758c82005-01-21 08:13:14 +0000981#endif
drhbb4957f2008-03-20 14:03:29 +0000982 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000983 goto too_big;
984 }
985 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000986}
drhf4479502004-05-27 03:12:53 +0000987
drh4c583122008-01-04 22:01:03 +0000988/* Opcode: String P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +0000989** Synopsis: r[P2]='P4' (len=P1)
drhf4479502004-05-27 03:12:53 +0000990**
drh9cbf3422008-01-17 16:22:13 +0000991** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000992*/
drh4c583122008-01-04 22:01:03 +0000993case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000994 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000995 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
996 pOut->z = pOp->p4.z;
997 pOut->n = pOp->p1;
998 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000999 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +00001000 break;
1001}
1002
drh053a1282012-09-19 21:15:46 +00001003/* Opcode: Null P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001004** Synopsis: r[P2..P3]=NULL
drhf0863fe2005-06-12 21:35:51 +00001005**
drhb8475df2011-12-09 16:21:19 +00001006** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +00001007** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +00001008** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +00001009** set to NULL.
1010**
1011** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
1012** NULL values will not compare equal even if SQLITE_NULLEQ is set on
1013** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +00001014*/
drh4c583122008-01-04 22:01:03 +00001015case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +00001016 int cnt;
drh053a1282012-09-19 21:15:46 +00001017 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +00001018 cnt = pOp->p3-pOp->p2;
dan3bc9f742013-08-15 16:18:39 +00001019 assert( pOp->p3<=(p->nMem-p->nCursor) );
drh053a1282012-09-19 21:15:46 +00001020 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +00001021 while( cnt>0 ){
1022 pOut++;
1023 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +00001024 VdbeMemRelease(pOut);
drh053a1282012-09-19 21:15:46 +00001025 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001026 cnt--;
1027 }
drhf0863fe2005-06-12 21:35:51 +00001028 break;
1029}
1030
1031
drh9de221d2008-01-05 06:51:30 +00001032/* Opcode: Blob P1 P2 * P4
drh81316f82013-10-29 20:40:47 +00001033** Synopsis: r[P2]=P4 (len=P1)
danielk1977c572ef72004-05-27 09:28:41 +00001034**
drh9de221d2008-01-05 06:51:30 +00001035** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001036** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001037*/
drh4c583122008-01-04 22:01:03 +00001038case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001039 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001040 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001041 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001042 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001043 break;
1044}
1045
drheaf52d82010-05-12 13:50:23 +00001046/* Opcode: Variable P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00001047** Synopsis: r[P2]=parameter(P1,P4)
drh50457892003-09-06 01:10:47 +00001048**
drheaf52d82010-05-12 13:50:23 +00001049** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001050**
1051** If the parameter is named, then its name appears in P4 and P3==1.
1052** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001053*/
drheaf52d82010-05-12 13:50:23 +00001054case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001055 Mem *pVar; /* Value being transferred */
1056
drheaf52d82010-05-12 13:50:23 +00001057 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001058 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001059 pVar = &p->aVar[pOp->p1 - 1];
1060 if( sqlite3VdbeMemTooBig(pVar) ){
1061 goto too_big;
drh023ae032007-05-08 12:12:16 +00001062 }
drheaf52d82010-05-12 13:50:23 +00001063 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1064 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001065 break;
1066}
danielk1977295ba552004-05-19 10:34:51 +00001067
drhb21e7c72008-06-22 12:37:57 +00001068/* Opcode: Move P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001069** Synopsis: r[P2@P3]=r[P1@P3]
drh5e00f6c2001-09-13 13:46:56 +00001070**
drhe8e4af72012-09-21 00:04:28 +00001071** Move the values in register P1..P1+P3 over into
1072** registers P2..P2+P3. Registers P1..P1+P3 are
drhb21e7c72008-06-22 12:37:57 +00001073** left holding a NULL. It is an error for register ranges
drhe8e4af72012-09-21 00:04:28 +00001074** P1..P1+P3 and P2..P2+P3 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001075*/
drhe1349cb2008-04-01 00:36:10 +00001076case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001077 char *zMalloc; /* Holding variable for allocated memory */
1078 int n; /* Number of registers left to copy */
1079 int p1; /* Register to copy from */
1080 int p2; /* Register to copy to */
1081
drhe8e4af72012-09-21 00:04:28 +00001082 n = pOp->p3 + 1;
drh856c1032009-06-02 15:21:42 +00001083 p1 = pOp->p1;
1084 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001085 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001086 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001087
drha6c2ed92009-11-14 23:22:23 +00001088 pIn1 = &aMem[p1];
1089 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001090 while( n-- ){
dan3bc9f742013-08-15 16:18:39 +00001091 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
1092 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00001093 assert( memIsValid(pIn1) );
1094 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001095 zMalloc = pOut->zMalloc;
1096 pOut->zMalloc = 0;
1097 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001098#ifdef SQLITE_DEBUG
1099 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1100 pOut->pScopyFrom += p1 - pOp->p2;
1101 }
1102#endif
drhb21e7c72008-06-22 12:37:57 +00001103 pIn1->zMalloc = zMalloc;
1104 REGISTER_TRACE(p2++, pOut);
1105 pIn1++;
1106 pOut++;
1107 }
drhe1349cb2008-04-01 00:36:10 +00001108 break;
1109}
1110
drhe8e4af72012-09-21 00:04:28 +00001111/* Opcode: Copy P1 P2 P3 * *
drhf63552b2013-10-30 00:25:03 +00001112** Synopsis: r[P2@P3]=r[P1@P3]
drhb1fdb2a2008-01-05 04:06:03 +00001113**
drhe8e4af72012-09-21 00:04:28 +00001114** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001115**
1116** This instruction makes a deep copy of the value. A duplicate
1117** is made of any string or blob constant. See also OP_SCopy.
1118*/
drhe8e4af72012-09-21 00:04:28 +00001119case OP_Copy: {
1120 int n;
1121
1122 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001123 pIn1 = &aMem[pOp->p1];
1124 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001125 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001126 while( 1 ){
1127 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1128 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001129#ifdef SQLITE_DEBUG
1130 pOut->pScopyFrom = 0;
1131#endif
drhe8e4af72012-09-21 00:04:28 +00001132 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1133 if( (n--)==0 ) break;
1134 pOut++;
1135 pIn1++;
1136 }
drhe1349cb2008-04-01 00:36:10 +00001137 break;
1138}
1139
drhb1fdb2a2008-01-05 04:06:03 +00001140/* Opcode: SCopy P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001141** Synopsis: r[P2]=r[P1]
drhb1fdb2a2008-01-05 04:06:03 +00001142**
drh9cbf3422008-01-17 16:22:13 +00001143** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001144**
1145** This instruction makes a shallow copy of the value. If the value
1146** is a string or blob, then the copy is only a pointer to the
1147** original and hence if the original changes so will the copy.
1148** Worse, if the original is deallocated, the copy becomes invalid.
1149** Thus the program must guarantee that the original will not change
1150** during the lifetime of the copy. Use OP_Copy to make a complete
1151** copy.
1152*/
drh26198bb2013-10-31 11:15:09 +00001153case OP_SCopy: { /* out2 */
drh3c657212009-11-17 23:59:58 +00001154 pIn1 = &aMem[pOp->p1];
1155 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001156 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001157 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001158#ifdef SQLITE_DEBUG
1159 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1160#endif
drh5e00f6c2001-09-13 13:46:56 +00001161 break;
1162}
drh75897232000-05-29 14:26:00 +00001163
drh9cbf3422008-01-17 16:22:13 +00001164/* Opcode: ResultRow P1 P2 * * *
drh4af5bee2013-10-30 02:37:50 +00001165** Synopsis: output=r[P1@P2]
drhd4e70eb2008-01-02 00:34:36 +00001166**
shane21e7feb2008-05-30 15:59:49 +00001167** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001168** results. This opcode causes the sqlite3_step() call to terminate
1169** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1170** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001171** row.
drhd4e70eb2008-01-02 00:34:36 +00001172*/
drh9cbf3422008-01-17 16:22:13 +00001173case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001174 Mem *pMem;
1175 int i;
1176 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001177 assert( pOp->p1>0 );
dan3bc9f742013-08-15 16:18:39 +00001178 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
drhd4e70eb2008-01-02 00:34:36 +00001179
dan32b09f22009-09-23 17:29:59 +00001180 /* If this statement has violated immediate foreign key constraints, do
1181 ** not return the number of rows modified. And do not RELEASE the statement
1182 ** transaction. It needs to be rolled back. */
1183 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1184 assert( db->flags&SQLITE_CountRows );
1185 assert( p->usesStmtJournal );
1186 break;
1187 }
1188
danielk1977bd434552009-03-18 10:33:00 +00001189 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1190 ** DML statements invoke this opcode to return the number of rows
1191 ** modified to the user. This is the only way that a VM that
1192 ** opens a statement transaction may invoke this opcode.
1193 **
1194 ** In case this is such a statement, close any statement transaction
1195 ** opened by this VM before returning control to the user. This is to
1196 ** ensure that statement-transactions are always nested, not overlapping.
1197 ** If the open statement-transaction is not closed here, then the user
1198 ** may step another VM that opens its own statement transaction. This
1199 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001200 **
1201 ** The statement transaction is never a top-level transaction. Hence
1202 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001203 */
1204 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001205 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1206 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001207 break;
1208 }
1209
drhd4e70eb2008-01-02 00:34:36 +00001210 /* Invalidate all ephemeral cursor row caches */
1211 p->cacheCtr = (p->cacheCtr + 2)|1;
1212
1213 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001214 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001215 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001216 */
drha6c2ed92009-11-14 23:22:23 +00001217 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001218 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001219 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001220 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001221 assert( (pMem[i].flags & MEM_Ephem)==0
1222 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001223 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001224 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001225 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001226 }
drh28039692008-03-17 16:54:01 +00001227 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001228
1229 /* Return SQLITE_ROW
1230 */
drhd4e70eb2008-01-02 00:34:36 +00001231 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001232 rc = SQLITE_ROW;
1233 goto vdbe_return;
1234}
1235
drh5b6afba2008-01-05 16:29:28 +00001236/* Opcode: Concat P1 P2 P3 * *
drh313619f2013-10-31 20:34:06 +00001237** Synopsis: r[P3]=r[P2]+r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001238**
drh5b6afba2008-01-05 16:29:28 +00001239** Add the text in register P1 onto the end of the text in
1240** register P2 and store the result in register P3.
1241** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001242**
1243** P3 = P2 || P1
1244**
1245** It is illegal for P1 and P3 to be the same register. Sometimes,
1246** if P3 is the same register as P2, the implementation is able
1247** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001248*/
drh5b6afba2008-01-05 16:29:28 +00001249case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001250 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001251
drh3c657212009-11-17 23:59:58 +00001252 pIn1 = &aMem[pOp->p1];
1253 pIn2 = &aMem[pOp->p2];
1254 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001255 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001256 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001257 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001258 break;
drh5e00f6c2001-09-13 13:46:56 +00001259 }
drha0c06522009-06-17 22:50:41 +00001260 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001261 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001262 Stringify(pIn2, encoding);
1263 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001264 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001265 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001266 }
danielk1977a7a8e142008-02-13 18:25:27 +00001267 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001268 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001269 goto no_mem;
1270 }
danielk1977a7a8e142008-02-13 18:25:27 +00001271 if( pOut!=pIn2 ){
1272 memcpy(pOut->z, pIn2->z, pIn2->n);
1273 }
1274 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
drh81316f82013-10-29 20:40:47 +00001275 pOut->z[nByte]=0;
danielk1977a7a8e142008-02-13 18:25:27 +00001276 pOut->z[nByte+1] = 0;
1277 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001278 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001279 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001280 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001281 break;
1282}
drh75897232000-05-29 14:26:00 +00001283
drh3c84ddf2008-01-09 02:15:38 +00001284/* Opcode: Add P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001285** Synopsis: r[P3]=r[P1]+r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001286**
drh60a713c2008-01-21 16:22:45 +00001287** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001288** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001289** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001290*/
drh3c84ddf2008-01-09 02:15:38 +00001291/* Opcode: Multiply P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001292** Synopsis: r[P3]=r[P1]*r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001293**
drh3c84ddf2008-01-09 02:15:38 +00001294**
shane21e7feb2008-05-30 15:59:49 +00001295** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001296** and store the result in register P3.
1297** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001298*/
drh3c84ddf2008-01-09 02:15:38 +00001299/* Opcode: Subtract P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001300** Synopsis: r[P3]=r[P2]-r[P1]
drh5e00f6c2001-09-13 13:46:56 +00001301**
drh60a713c2008-01-21 16:22:45 +00001302** Subtract the value in register P1 from the value in register P2
1303** and store the result in register P3.
1304** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001305*/
drh9cbf3422008-01-17 16:22:13 +00001306/* Opcode: Divide P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001307** Synopsis: r[P3]=r[P1]/r[P2]
drh5e00f6c2001-09-13 13:46:56 +00001308**
drh60a713c2008-01-21 16:22:45 +00001309** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001310** and store the result in register P3 (P3=P2/P1). If the value in
1311** register P1 is zero, then the result is NULL. If either input is
1312** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001313*/
drh9cbf3422008-01-17 16:22:13 +00001314/* Opcode: Remainder P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001315** Synopsis: r[P3]=r[P1]%r[P2]
drhbf4133c2001-10-13 02:59:08 +00001316**
drh3c84ddf2008-01-09 02:15:38 +00001317** Compute the remainder after integer division of the value in
1318** register P1 by the value in register P2 and store the result in P3.
1319** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001320** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001321*/
drh5b6afba2008-01-05 16:29:28 +00001322case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1323case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1324case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1325case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1326case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001327 char bIntint; /* Started out as two integer operands */
drh856c1032009-06-02 15:21:42 +00001328 int flags; /* Combined MEM_* flags from both inputs */
1329 i64 iA; /* Integer value of left operand */
1330 i64 iB; /* Integer value of right operand */
1331 double rA; /* Real value of left operand */
1332 double rB; /* Real value of right operand */
1333
drh3c657212009-11-17 23:59:58 +00001334 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001335 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001336 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001337 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001338 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001339 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001340 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1341 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001342 iA = pIn1->u.i;
1343 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001344 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001345 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001346 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1347 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1348 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001349 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001350 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001351 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001352 iB /= iA;
drh75897232000-05-29 14:26:00 +00001353 break;
1354 }
drhbf4133c2001-10-13 02:59:08 +00001355 default: {
drh856c1032009-06-02 15:21:42 +00001356 if( iA==0 ) goto arithmetic_result_is_null;
1357 if( iA==-1 ) iA = 1;
1358 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001359 break;
1360 }
drh75897232000-05-29 14:26:00 +00001361 }
drh856c1032009-06-02 15:21:42 +00001362 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001363 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001364 }else{
drhbe707b32012-12-10 22:19:14 +00001365 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001366fp_math:
drh856c1032009-06-02 15:21:42 +00001367 rA = sqlite3VdbeRealValue(pIn1);
1368 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001369 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001370 case OP_Add: rB += rA; break;
1371 case OP_Subtract: rB -= rA; break;
1372 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001373 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001374 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001375 if( rA==(double)0 ) goto arithmetic_result_is_null;
1376 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001377 break;
1378 }
drhbf4133c2001-10-13 02:59:08 +00001379 default: {
shane75ac1de2009-06-09 18:58:52 +00001380 iA = (i64)rA;
1381 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001382 if( iA==0 ) goto arithmetic_result_is_null;
1383 if( iA==-1 ) iA = 1;
1384 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001385 break;
1386 }
drh5e00f6c2001-09-13 13:46:56 +00001387 }
drhc5a7b512010-01-13 16:25:42 +00001388#ifdef SQLITE_OMIT_FLOATING_POINT
1389 pOut->u.i = rB;
1390 MemSetTypeFlag(pOut, MEM_Int);
1391#else
drh856c1032009-06-02 15:21:42 +00001392 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001393 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001394 }
drh856c1032009-06-02 15:21:42 +00001395 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001396 MemSetTypeFlag(pOut, MEM_Real);
drhbe707b32012-12-10 22:19:14 +00001397 if( (flags & MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001398 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001399 }
drhc5a7b512010-01-13 16:25:42 +00001400#endif
drh5e00f6c2001-09-13 13:46:56 +00001401 }
1402 break;
1403
drha05a7222008-01-19 03:35:58 +00001404arithmetic_result_is_null:
1405 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001406 break;
1407}
1408
drh7a957892012-02-02 17:35:43 +00001409/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001410**
drh66a51672008-01-03 00:01:23 +00001411** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001412** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1413** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001414** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001415**
drh7a957892012-02-02 17:35:43 +00001416** If P1 is not zero, then it is a register that a subsequent min() or
1417** max() aggregate will set to 1 if the current row is not the minimum or
1418** maximum. The P1 register is initialized to 0 by this instruction.
1419**
danielk1977dc1bdc42004-06-11 10:51:27 +00001420** The interface used by the implementation of the aforementioned functions
1421** to retrieve the collation sequence set by this opcode is not available
1422** publicly, only to user functions defined in func.c.
1423*/
drh9cbf3422008-01-17 16:22:13 +00001424case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001425 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001426 if( pOp->p1 ){
1427 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1428 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001429 break;
1430}
1431
drh98757152008-01-09 23:04:12 +00001432/* Opcode: Function P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00001433** Synopsis: r[P3]=func(r[P2@P5])
drh8e0a2f92002-02-23 23:45:45 +00001434**
drh66a51672008-01-03 00:01:23 +00001435** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001436** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001437** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001438** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001439**
drh13449892005-09-07 21:22:45 +00001440** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001441** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001442** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001443** whether meta data associated with a user function argument using the
1444** sqlite3_set_auxdata() API may be safely retained until the next
1445** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001446**
drh13449892005-09-07 21:22:45 +00001447** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001448*/
drh0bce8352002-02-28 00:41:10 +00001449case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001450 int i;
drh6810ce62004-01-31 19:22:56 +00001451 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001452 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001453 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001454 int n;
drh1350b032002-02-27 19:00:20 +00001455
drh856c1032009-06-02 15:21:42 +00001456 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001457 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001458 assert( apVal || n==0 );
dan3bc9f742013-08-15 16:18:39 +00001459 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drhebc16712010-09-28 00:25:58 +00001460 pOut = &aMem[pOp->p3];
1461 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001462
dan3bc9f742013-08-15 16:18:39 +00001463 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001464 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001465 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001466 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001467 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001468 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001469 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001470 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001471 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001472 }
danielk197751ad0ec2004-05-24 12:39:02 +00001473
dan0c547792013-07-18 17:12:08 +00001474 assert( pOp->p4type==P4_FUNCDEF );
1475 ctx.pFunc = pOp->p4.pFunc;
drh00706be2004-01-30 14:49:16 +00001476 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001477 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001478 ctx.s.xDel = 0;
1479 ctx.s.zMalloc = 0;
dan0c547792013-07-18 17:12:08 +00001480 ctx.iOp = pc;
1481 ctx.pVdbe = p;
danielk1977a7a8e142008-02-13 18:25:27 +00001482
1483 /* The output cell may already have a buffer allocated. Move
1484 ** the pointer to ctx.s so in case the user-function can use
1485 ** the already allocated buffer instead of allocating a new one.
1486 */
1487 sqlite3VdbeMemMove(&ctx.s, pOut);
1488 MemSetTypeFlag(&ctx.s, MEM_Null);
1489
drh9b47ee32013-08-20 03:13:51 +00001490 ctx.fErrorOrAux = 0;
drhd36e1042013-09-06 13:10:12 +00001491 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001492 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001493 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001494 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001495 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001496 }
drh99a66922011-05-13 18:51:42 +00001497 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001498 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001499 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001500
dan5f84e142011-06-14 14:18:45 +00001501 if( db->mallocFailed ){
1502 /* Even though a malloc() has failed, the implementation of the
1503 ** user function may have called an sqlite3_result_XXX() function
1504 ** to return a value. The following call releases any resources
1505 ** associated with such a value.
1506 */
1507 sqlite3VdbeMemRelease(&ctx.s);
1508 goto no_mem;
1509 }
1510
drh90669c12006-01-20 15:45:36 +00001511 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001512 if( ctx.fErrorOrAux ){
1513 if( ctx.isError ){
1514 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1515 rc = ctx.isError;
1516 }
1517 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001518 }
1519
drh9cbf3422008-01-17 16:22:13 +00001520 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001521 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001522 sqlite3VdbeMemMove(pOut, &ctx.s);
1523 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001524 goto too_big;
1525 }
drh7b94e7f2011-04-04 12:29:20 +00001526
1527#if 0
1528 /* The app-defined function has done something that as caused this
1529 ** statement to expire. (Perhaps the function called sqlite3_exec()
1530 ** with a CREATE TABLE statement.)
1531 */
1532 if( p->expired ) rc = SQLITE_ABORT;
1533#endif
1534
drh2dcef112008-01-12 19:03:48 +00001535 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001536 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001537 break;
1538}
1539
drh98757152008-01-09 23:04:12 +00001540/* Opcode: BitAnd P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001541** Synopsis: r[P3]=r[P1]&r[P2]
drhbf4133c2001-10-13 02:59:08 +00001542**
drh98757152008-01-09 23:04:12 +00001543** Take the bit-wise AND of the values in register P1 and P2 and
1544** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001545** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001546*/
drh98757152008-01-09 23:04:12 +00001547/* Opcode: BitOr P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001548** Synopsis: r[P3]=r[P1]|r[P2]
drhbf4133c2001-10-13 02:59:08 +00001549**
drh98757152008-01-09 23:04:12 +00001550** Take the bit-wise OR of the values in register P1 and P2 and
1551** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001552** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001553*/
drh98757152008-01-09 23:04:12 +00001554/* Opcode: ShiftLeft P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001555** Synopsis: r[P3]=r[P2]<<r[P1]
drhbf4133c2001-10-13 02:59:08 +00001556**
drh98757152008-01-09 23:04:12 +00001557** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001558** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001559** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001560** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001561*/
drh98757152008-01-09 23:04:12 +00001562/* Opcode: ShiftRight P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00001563** Synopsis: r[P3]=r[P2]>>r[P1]
drhbf4133c2001-10-13 02:59:08 +00001564**
drh98757152008-01-09 23:04:12 +00001565** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001566** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001567** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001568** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001569*/
drh5b6afba2008-01-05 16:29:28 +00001570case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1571case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1572case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1573case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001574 i64 iA;
1575 u64 uA;
1576 i64 iB;
1577 u8 op;
drh6810ce62004-01-31 19:22:56 +00001578
drh3c657212009-11-17 23:59:58 +00001579 pIn1 = &aMem[pOp->p1];
1580 pIn2 = &aMem[pOp->p2];
1581 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001582 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001583 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001584 break;
1585 }
drh158b9cb2011-03-05 20:59:46 +00001586 iA = sqlite3VdbeIntValue(pIn2);
1587 iB = sqlite3VdbeIntValue(pIn1);
1588 op = pOp->opcode;
1589 if( op==OP_BitAnd ){
1590 iA &= iB;
1591 }else if( op==OP_BitOr ){
1592 iA |= iB;
1593 }else if( iB!=0 ){
1594 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1595
1596 /* If shifting by a negative amount, shift in the other direction */
1597 if( iB<0 ){
1598 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1599 op = 2*OP_ShiftLeft + 1 - op;
1600 iB = iB>(-64) ? -iB : 64;
1601 }
1602
1603 if( iB>=64 ){
1604 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1605 }else{
1606 memcpy(&uA, &iA, sizeof(uA));
1607 if( op==OP_ShiftLeft ){
1608 uA <<= iB;
1609 }else{
1610 uA >>= iB;
1611 /* Sign-extend on a right shift of a negative number */
1612 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1613 }
1614 memcpy(&iA, &uA, sizeof(iA));
1615 }
drhbf4133c2001-10-13 02:59:08 +00001616 }
drh158b9cb2011-03-05 20:59:46 +00001617 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001618 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001619 break;
1620}
1621
drh8558cde2008-01-05 05:20:10 +00001622/* Opcode: AddImm P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00001623** Synopsis: r[P1]=r[P1]+P2
drh5e00f6c2001-09-13 13:46:56 +00001624**
danielk19770cdc0222008-06-26 18:04:03 +00001625** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001626** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001627**
drh8558cde2008-01-05 05:20:10 +00001628** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001629*/
drh9cbf3422008-01-17 16:22:13 +00001630case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001631 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001632 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001633 sqlite3VdbeMemIntegerify(pIn1);
1634 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001635 break;
1636}
1637
drh9cbf3422008-01-17 16:22:13 +00001638/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001639**
drh9cbf3422008-01-17 16:22:13 +00001640** Force the value in register P1 to be an integer. If the value
1641** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001642** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001643** raise an SQLITE_MISMATCH exception.
1644*/
drh9cbf3422008-01-17 16:22:13 +00001645case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001646 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001647 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1648 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001649 if( pOp->p2==0 ){
1650 rc = SQLITE_MISMATCH;
1651 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001652 }else{
drh17c40292004-07-21 02:53:29 +00001653 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001654 }
drh8aff1012001-12-22 14:49:24 +00001655 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001656 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001657 }
1658 break;
1659}
1660
drh13573c72010-01-12 17:04:07 +00001661#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001662/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001663**
drh2133d822008-01-03 18:44:59 +00001664** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001665**
drh8a512562005-11-14 22:29:05 +00001666** This opcode is used when extracting information from a column that
1667** has REAL affinity. Such column values may still be stored as
1668** integers, for space efficiency, but after extraction we want them
1669** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001670*/
drh9cbf3422008-01-17 16:22:13 +00001671case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001672 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001673 if( pIn1->flags & MEM_Int ){
1674 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001675 }
drh487e2622005-06-25 18:42:14 +00001676 break;
1677}
drh13573c72010-01-12 17:04:07 +00001678#endif
drh487e2622005-06-25 18:42:14 +00001679
drh8df447f2005-11-01 15:48:24 +00001680#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001681/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001682**
drh8558cde2008-01-05 05:20:10 +00001683** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001684** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001685** equivalent of printf(). Blob values are unchanged and
1686** are afterwards simply interpreted as text.
1687**
1688** A NULL value is not changed by this routine. It remains NULL.
1689*/
drh9cbf3422008-01-17 16:22:13 +00001690case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001691 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001692 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001693 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001694 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001695 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1696 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1697 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001698 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001699 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001700 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001701 break;
1702}
1703
drh8558cde2008-01-05 05:20:10 +00001704/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001705**
drh8558cde2008-01-05 05:20:10 +00001706** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001707** If the value is numeric, convert it to a string first.
1708** Strings are simply reinterpreted as blobs with no change
1709** to the underlying data.
1710**
1711** A NULL value is not changed by this routine. It remains NULL.
1712*/
drh9cbf3422008-01-17 16:22:13 +00001713case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001714 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001715 if( pIn1->flags & MEM_Null ) break;
1716 if( (pIn1->flags & MEM_Blob)==0 ){
1717 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001718 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001719 MemSetTypeFlag(pIn1, MEM_Blob);
1720 }else{
1721 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001722 }
drhb7654112008-01-12 12:48:07 +00001723 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001724 break;
1725}
drh8a512562005-11-14 22:29:05 +00001726
drh8558cde2008-01-05 05:20:10 +00001727/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001728**
drh8558cde2008-01-05 05:20:10 +00001729** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001730** integer or a floating-point number.)
1731** If the value is text or blob, try to convert it to an using the
1732** equivalent of atoi() or atof() and store 0 if no such conversion
1733** is possible.
1734**
1735** A NULL value is not changed by this routine. It remains NULL.
1736*/
drh9cbf3422008-01-17 16:22:13 +00001737case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001738 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001739 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001740 break;
1741}
1742#endif /* SQLITE_OMIT_CAST */
1743
drh8558cde2008-01-05 05:20:10 +00001744/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001745**
drh710c4842010-08-30 01:17:20 +00001746** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001747** The value is currently a real number, drop its fractional part.
1748** If the value is text or blob, try to convert it to an integer using the
1749** equivalent of atoi() and store 0 if no such conversion 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_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001754 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001755 if( (pIn1->flags & MEM_Null)==0 ){
1756 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001757 }
1758 break;
1759}
1760
drh13573c72010-01-12 17:04:07 +00001761#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001762/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001763**
drh8558cde2008-01-05 05:20:10 +00001764** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001765** If The value is currently an integer, convert it.
1766** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001767** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001768**
1769** A NULL value is not changed by this routine. It remains NULL.
1770*/
drh9cbf3422008-01-17 16:22:13 +00001771case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001772 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001773 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001774 if( (pIn1->flags & MEM_Null)==0 ){
1775 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001776 }
1777 break;
1778}
drh13573c72010-01-12 17:04:07 +00001779#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001780
drh35573352008-01-08 23:54:25 +00001781/* Opcode: Lt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001782** Synopsis: if r[P1]<r[P3] goto P3
drh5e00f6c2001-09-13 13:46:56 +00001783**
drh35573352008-01-08 23:54:25 +00001784** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1785** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001786**
drh35573352008-01-08 23:54:25 +00001787** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1788** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001789** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001790**
drh35573352008-01-08 23:54:25 +00001791** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001792** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001793** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001794** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001795** affinity is used. Note that the affinity conversions are stored
1796** back into the input registers P1 and P3. So this opcode can cause
1797** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001798**
1799** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001800** the values are compared. If both values are blobs then memcmp() is
1801** used to determine the results of the comparison. If both values
1802** are text, then the appropriate collating function specified in
1803** P4 is used to do the comparison. If P4 is not specified then
1804** memcmp() is used to compare text string. If both values are
1805** numeric, then a numeric comparison is used. If the two values
1806** are of different types, then numbers are considered less than
1807** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001808**
drh35573352008-01-08 23:54:25 +00001809** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1810** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001811**
1812** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1813** equal to one another, provided that they do not have their MEM_Cleared
1814** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001815*/
drh9cbf3422008-01-17 16:22:13 +00001816/* Opcode: Ne P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001817** Synopsis: if r[P1]!=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001818**
drh35573352008-01-08 23:54:25 +00001819** This works just like the Lt opcode except that the jump is taken if
1820** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001821** additional information.
drh6a2fe092009-09-23 02:29:36 +00001822**
1823** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1824** true or false and is never NULL. If both operands are NULL then the result
1825** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001826** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001827** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001828*/
drh9cbf3422008-01-17 16:22:13 +00001829/* Opcode: Eq P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001830** Synopsis: if r[P1]==r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001831**
drh35573352008-01-08 23:54:25 +00001832** This works just like the Lt opcode except that the jump is taken if
1833** the operands in registers P1 and P3 are equal.
1834** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001835**
1836** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1837** true or false and is never NULL. If both operands are NULL then the result
1838** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001839** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001840** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001841*/
drh9cbf3422008-01-17 16:22:13 +00001842/* Opcode: Le P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001843** Synopsis: if r[P1]<=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001844**
drh35573352008-01-08 23:54:25 +00001845** This works just like the Lt opcode except that the jump is taken if
1846** the content of register P3 is less than or equal to the content of
1847** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001848*/
drh9cbf3422008-01-17 16:22:13 +00001849/* Opcode: Gt P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001850** Synopsis: if r[P1]>r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001851**
drh35573352008-01-08 23:54:25 +00001852** This works just like the Lt opcode except that the jump is taken if
1853** the content of register P3 is greater than the content of
1854** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001855*/
drh9cbf3422008-01-17 16:22:13 +00001856/* Opcode: Ge P1 P2 P3 P4 P5
drh2552d432013-11-02 22:29:34 +00001857** Synopsis: if r[P1]>=r[P3] goto P2
drh5e00f6c2001-09-13 13:46:56 +00001858**
drh35573352008-01-08 23:54:25 +00001859** This works just like the Lt opcode except that the jump is taken if
1860** the content of register P3 is greater than or equal to the content of
1861** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001862*/
drh9cbf3422008-01-17 16:22:13 +00001863case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1864case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1865case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1866case OP_Le: /* same as TK_LE, jump, in1, in3 */
1867case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1868case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001869 int res; /* Result of the comparison of pIn1 against pIn3 */
1870 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001871 u16 flags1; /* Copy of initial value of pIn1->flags */
1872 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001873
drh3c657212009-11-17 23:59:58 +00001874 pIn1 = &aMem[pOp->p1];
1875 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001876 flags1 = pIn1->flags;
1877 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001878 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001879 /* One or both operands are NULL */
1880 if( pOp->p5 & SQLITE_NULLEQ ){
1881 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1882 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1883 ** or not both operands are null.
1884 */
1885 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001886 assert( (flags1 & MEM_Cleared)==0 );
1887 if( (flags1&MEM_Null)!=0
1888 && (flags3&MEM_Null)!=0
1889 && (flags3&MEM_Cleared)==0
1890 ){
1891 res = 0; /* Results are equal */
1892 }else{
1893 res = 1; /* Results are not equal */
1894 }
drh6a2fe092009-09-23 02:29:36 +00001895 }else{
1896 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1897 ** then the result is always NULL.
1898 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1899 */
drh9b47ee32013-08-20 03:13:51 +00001900 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1901 pc = pOp->p2-1;
1902 }else if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001903 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001904 MemSetTypeFlag(pOut, MEM_Null);
1905 REGISTER_TRACE(pOp->p2, pOut);
drh6a2fe092009-09-23 02:29:36 +00001906 }
1907 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001908 }
drh6a2fe092009-09-23 02:29:36 +00001909 }else{
1910 /* Neither operand is NULL. Do a comparison. */
1911 affinity = pOp->p5 & SQLITE_AFF_MASK;
1912 if( affinity ){
1913 applyAffinity(pIn1, affinity, encoding);
1914 applyAffinity(pIn3, affinity, encoding);
1915 if( db->mallocFailed ) goto no_mem;
1916 }
danielk1977a37cdde2004-05-16 11:15:36 +00001917
drh6a2fe092009-09-23 02:29:36 +00001918 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1919 ExpandBlob(pIn1);
1920 ExpandBlob(pIn3);
1921 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001922 }
danielk1977a37cdde2004-05-16 11:15:36 +00001923 switch( pOp->opcode ){
1924 case OP_Eq: res = res==0; break;
1925 case OP_Ne: res = res!=0; break;
1926 case OP_Lt: res = res<0; break;
1927 case OP_Le: res = res<=0; break;
1928 case OP_Gt: res = res>0; break;
1929 default: res = res>=0; break;
1930 }
1931
drh35573352008-01-08 23:54:25 +00001932 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001933 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001934 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001935 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001936 pOut->u.i = res;
1937 REGISTER_TRACE(pOp->p2, pOut);
1938 }else if( res ){
1939 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001940 }
danb7dca7d2010-03-05 16:32:12 +00001941
1942 /* Undo any changes made by applyAffinity() to the input registers. */
1943 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1944 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001945 break;
1946}
drhc9b84a12002-06-20 11:36:48 +00001947
drh0acb7e42008-06-25 00:12:41 +00001948/* Opcode: Permutation * * * P4 *
1949**
shanebe217792009-03-05 04:20:31 +00001950** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001951** of integers in P4.
1952**
drh953f7612012-12-07 22:18:54 +00001953** The permutation is only valid until the next OP_Compare that has
1954** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1955** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001956*/
1957case OP_Permutation: {
1958 assert( pOp->p4type==P4_INTARRAY );
1959 assert( pOp->p4.ai );
1960 aPermute = pOp->p4.ai;
1961 break;
1962}
1963
drh953f7612012-12-07 22:18:54 +00001964/* Opcode: Compare P1 P2 P3 P4 P5
drh16ee60f2008-06-20 18:13:25 +00001965**
drh710c4842010-08-30 01:17:20 +00001966** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1967** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001968** the comparison for use by the next OP_Jump instruct.
1969**
drh0ca10df2012-12-08 13:26:23 +00001970** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1971** determined by the most recent OP_Permutation operator. If the
1972** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1973** order.
1974**
drh0acb7e42008-06-25 00:12:41 +00001975** P4 is a KeyInfo structure that defines collating sequences and sort
1976** orders for the comparison. The permutation applies to registers
1977** only. The KeyInfo elements are used sequentially.
1978**
1979** The comparison is a sort comparison, so NULLs compare equal,
1980** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001981** and strings are less than blobs.
1982*/
1983case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001984 int n;
1985 int i;
1986 int p1;
1987 int p2;
1988 const KeyInfo *pKeyInfo;
1989 int idx;
1990 CollSeq *pColl; /* Collating sequence to use on this term */
1991 int bRev; /* True for DESCENDING sort order */
1992
drh953f7612012-12-07 22:18:54 +00001993 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00001994 n = pOp->p3;
1995 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001996 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001997 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001998 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001999 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00002000#if SQLITE_DEBUG
2001 if( aPermute ){
2002 int k, mx = 0;
2003 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
dan3bc9f742013-08-15 16:18:39 +00002004 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
2005 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002006 }else{
dan3bc9f742013-08-15 16:18:39 +00002007 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
2008 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
drh6a2fe092009-09-23 02:29:36 +00002009 }
2010#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00002011 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00002012 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00002013 assert( memIsValid(&aMem[p1+idx]) );
2014 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00002015 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
2016 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00002017 assert( i<pKeyInfo->nField );
2018 pColl = pKeyInfo->aColl[i];
2019 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00002020 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00002021 if( iCompare ){
2022 if( bRev ) iCompare = -iCompare;
2023 break;
2024 }
drh16ee60f2008-06-20 18:13:25 +00002025 }
drh0acb7e42008-06-25 00:12:41 +00002026 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00002027 break;
2028}
2029
2030/* Opcode: Jump P1 P2 P3 * *
2031**
2032** Jump to the instruction at address P1, P2, or P3 depending on whether
2033** in the most recent OP_Compare instruction the P1 vector was less than
2034** equal to, or greater than the P2 vector, respectively.
2035*/
drh0acb7e42008-06-25 00:12:41 +00002036case OP_Jump: { /* jump */
2037 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00002038 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00002039 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00002040 pc = pOp->p2 - 1;
2041 }else{
2042 pc = pOp->p3 - 1;
2043 }
2044 break;
2045}
2046
drh5b6afba2008-01-05 16:29:28 +00002047/* Opcode: And P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002048** Synopsis: r[P3]=(r[P1] && r[P2])
drh5e00f6c2001-09-13 13:46:56 +00002049**
drh5b6afba2008-01-05 16:29:28 +00002050** Take the logical AND of the values in registers P1 and P2 and
2051** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002052**
drh5b6afba2008-01-05 16:29:28 +00002053** If either P1 or P2 is 0 (false) then the result is 0 even if
2054** the other input is NULL. A NULL and true or two NULLs give
2055** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002056*/
drh5b6afba2008-01-05 16:29:28 +00002057/* Opcode: Or P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00002058** Synopsis: r[P3]=(r[P1] || r[P2])
drh5b6afba2008-01-05 16:29:28 +00002059**
2060** Take the logical OR of the values in register P1 and P2 and
2061** store the answer in register P3.
2062**
2063** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2064** even if the other input is NULL. A NULL and false or two NULLs
2065** give a NULL output.
2066*/
2067case OP_And: /* same as TK_AND, in1, in2, out3 */
2068case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002069 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2070 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002071
drh3c657212009-11-17 23:59:58 +00002072 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002073 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002074 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002075 }else{
drh5b6afba2008-01-05 16:29:28 +00002076 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002077 }
drh3c657212009-11-17 23:59:58 +00002078 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002079 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002080 v2 = 2;
2081 }else{
drh5b6afba2008-01-05 16:29:28 +00002082 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002083 }
2084 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002085 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002086 v1 = and_logic[v1*3+v2];
2087 }else{
drh5b6afba2008-01-05 16:29:28 +00002088 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002089 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002090 }
drh3c657212009-11-17 23:59:58 +00002091 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002092 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002093 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002094 }else{
drh5b6afba2008-01-05 16:29:28 +00002095 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002096 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002097 }
drh5e00f6c2001-09-13 13:46:56 +00002098 break;
2099}
2100
drhe99fa2a2008-12-15 15:27:51 +00002101/* Opcode: Not P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002102** Synopsis: r[P2]= !r[P1]
drh5e00f6c2001-09-13 13:46:56 +00002103**
drhe99fa2a2008-12-15 15:27:51 +00002104** Interpret the value in register P1 as a boolean value. Store the
2105** boolean complement in register P2. If the value in register P1 is
2106** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002107*/
drh93952eb2009-11-13 19:43:43 +00002108case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002109 pIn1 = &aMem[pOp->p1];
2110 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002111 if( pIn1->flags & MEM_Null ){
2112 sqlite3VdbeMemSetNull(pOut);
2113 }else{
2114 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2115 }
drh5e00f6c2001-09-13 13:46:56 +00002116 break;
2117}
2118
drhe99fa2a2008-12-15 15:27:51 +00002119/* Opcode: BitNot P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002120** Synopsis: r[P1]= ~r[P1]
drhbf4133c2001-10-13 02:59:08 +00002121**
drhe99fa2a2008-12-15 15:27:51 +00002122** Interpret the content of register P1 as an integer. Store the
2123** ones-complement of the P1 value into register P2. If P1 holds
2124** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002125*/
drh93952eb2009-11-13 19:43:43 +00002126case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002127 pIn1 = &aMem[pOp->p1];
2128 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002129 if( pIn1->flags & MEM_Null ){
2130 sqlite3VdbeMemSetNull(pOut);
2131 }else{
2132 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2133 }
drhbf4133c2001-10-13 02:59:08 +00002134 break;
2135}
2136
drh48f2d3b2011-09-16 01:34:43 +00002137/* Opcode: Once P1 P2 * * *
2138**
dan1d8cb212011-12-09 13:24:16 +00002139** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
2140** set the flag and fall through to the next instruction.
drh48f2d3b2011-09-16 01:34:43 +00002141*/
dan1d8cb212011-12-09 13:24:16 +00002142case OP_Once: { /* jump */
2143 assert( pOp->p1<p->nOnceFlag );
2144 if( p->aOnceFlag[pOp->p1] ){
2145 pc = pOp->p2-1;
2146 }else{
2147 p->aOnceFlag[pOp->p1] = 1;
2148 }
2149 break;
2150}
2151
drh3c84ddf2008-01-09 02:15:38 +00002152/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002153**
drhef8662b2011-06-20 21:47:58 +00002154** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002155** is considered true if it is numeric and non-zero. If the value
drhb8475df2011-12-09 16:21:19 +00002156** in P1 is NULL then take the jump if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002157*/
drh3c84ddf2008-01-09 02:15:38 +00002158/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002159**
drhef8662b2011-06-20 21:47:58 +00002160** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002161** is considered false if it has a numeric value of zero. If the value
2162** in P1 is NULL then take the jump if P3 is zero.
drhf5905aa2002-05-26 20:54:33 +00002163*/
drh9cbf3422008-01-17 16:22:13 +00002164case OP_If: /* jump, in1 */
2165case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002166 int c;
drh3c657212009-11-17 23:59:58 +00002167 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002168 if( pIn1->flags & MEM_Null ){
2169 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002170 }else{
drhba0232a2005-06-06 17:27:19 +00002171#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002172 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002173#else
drh3c84ddf2008-01-09 02:15:38 +00002174 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002175#endif
drhf5905aa2002-05-26 20:54:33 +00002176 if( pOp->opcode==OP_IfNot ) c = !c;
2177 }
drh3c84ddf2008-01-09 02:15:38 +00002178 if( c ){
2179 pc = pOp->p2-1;
2180 }
drh5e00f6c2001-09-13 13:46:56 +00002181 break;
2182}
2183
drh830ecf92009-06-18 00:41:55 +00002184/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00002185**
drh830ecf92009-06-18 00:41:55 +00002186** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002187*/
drh9cbf3422008-01-17 16:22:13 +00002188case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002189 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002190 if( (pIn1->flags & MEM_Null)!=0 ){
2191 pc = pOp->p2 - 1;
2192 }
drh477df4b2008-01-05 18:48:24 +00002193 break;
2194}
2195
drh98757152008-01-09 23:04:12 +00002196/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002197**
drh6a288a32008-01-07 19:20:24 +00002198** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002199*/
drh9cbf3422008-01-17 16:22:13 +00002200case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002201 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002202 if( (pIn1->flags & MEM_Null)==0 ){
2203 pc = pOp->p2 - 1;
2204 }
drh5e00f6c2001-09-13 13:46:56 +00002205 break;
2206}
2207
drh3e9ca092009-09-08 01:14:48 +00002208/* Opcode: Column P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00002209** Synopsis: r[P3]=PX
danielk1977192ac1d2004-05-10 07:17:30 +00002210**
danielk1977cfcdaef2004-05-12 07:33:33 +00002211** Interpret the data that cursor P1 points to as a structure built using
2212** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002213** information about the format of the data.) Extract the P2-th column
2214** from this record. If there are less that (P2+1)
2215** values in the record, extract a NULL.
2216**
drh9cbf3422008-01-17 16:22:13 +00002217** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002218**
danielk19771f4aa332008-01-03 09:51:55 +00002219** If the column contains fewer than P2 fields, then extract a NULL. Or,
2220** if the P4 argument is a P4_MEM use the value of the P4 argument as
2221** the result.
drh3e9ca092009-09-08 01:14:48 +00002222**
2223** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2224** then the cache of the cursor is reset prior to extracting the column.
2225** The first OP_Column against a pseudo-table after the value of the content
2226** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002227**
drhdda5c082012-03-28 13:41:10 +00002228** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2229** the result is guaranteed to only be used as the argument of a length()
2230** or typeof() function, respectively. The loading of large blobs can be
2231** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002232*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002233case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002234 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002235 i64 payloadSize64; /* Number of bytes in the record */
2236 int p1; /* P1 value of the opcode */
2237 int p2; /* column number to retrieve */
2238 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002239 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002240 BtCursor *pCrsr; /* The BTree cursor */
2241 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2242 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002243 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002244 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002245 int i; /* Loop counter */
2246 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002247 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002248 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002249 u8 *zIdx; /* Index into header */
2250 u8 *zEndHdr; /* Pointer to first byte after the header */
2251 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002252 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002253 int szHdr; /* Size of the header size field at start of record */
2254 int avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002255 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002256 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002257
drh856c1032009-06-02 15:21:42 +00002258
2259 p1 = pOp->p1;
2260 p2 = pOp->p2;
2261 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002262 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002263 assert( p1<p->nCursor );
dan3bc9f742013-08-15 16:18:39 +00002264 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00002265 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002266 memAboutToChange(p, pDest);
shane36840fd2009-06-26 16:32:13 +00002267 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002268
drhe61cffc2004-06-12 18:12:15 +00002269 /* This block sets the variable payloadSize to be the total number of
2270 ** bytes in the record.
2271 **
2272 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002273 ** The complete record text is always available for pseudo-tables
2274 ** If the record is stored in a cursor, the complete record text
2275 ** might be available in the pC->aRow cache. Or it might not be.
2276 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002277 **
2278 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002279 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002280 */
drhb73857f2006-03-17 00:25:59 +00002281 pC = p->apCsr[p1];
drha5759672012-10-30 14:39:12 +00002282 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002283#ifndef SQLITE_OMIT_VIRTUALTABLE
2284 assert( pC->pVtabCursor==0 );
2285#endif
shane36840fd2009-06-26 16:32:13 +00002286 pCrsr = pC->pCursor;
2287 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002288 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002289 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002290 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002291 if( pC->nullRow ){
2292 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002293 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002294 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002295 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002296 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002297 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002298 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drhc27ae612009-07-14 18:35:44 +00002299 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002300 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2301 ** payload size, so it is impossible for payloadSize64 to be
2302 ** larger than 32 bits. */
2303 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002304 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002305 }else{
drhea8ffdf2009-07-22 00:35:23 +00002306 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002307 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002308 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002309 }
drh4a6f3aa2011-08-28 00:19:26 +00002310 }else if( ALWAYS(pC->pseudoTableReg>0) ){
drha6c2ed92009-11-14 23:22:23 +00002311 pReg = &aMem[pC->pseudoTableReg];
drh21172c42012-10-30 00:29:07 +00002312 if( pC->multiPseudo ){
2313 sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
2314 Deephemeralize(pDest);
2315 goto op_column_out;
2316 }
drh3e9ca092009-09-08 01:14:48 +00002317 assert( pReg->flags & MEM_Blob );
drh2b4ded92010-09-27 21:09:31 +00002318 assert( memIsValid(pReg) );
drh3e9ca092009-09-08 01:14:48 +00002319 payloadSize = pReg->n;
2320 zRec = pReg->z;
2321 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002322 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002323 }else{
2324 /* Consider the row to be NULL */
2325 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002326 }
2327
drhe6f43fc2011-08-28 02:15:34 +00002328 /* If payloadSize is 0, then just store a NULL. This can happen because of
2329 ** nullRow or because of a corrupt database. */
danielk1977192ac1d2004-05-10 07:17:30 +00002330 if( payloadSize==0 ){
drhe6f43fc2011-08-28 02:15:34 +00002331 MemSetTypeFlag(pDest, MEM_Null);
drhd4e70eb2008-01-02 00:34:36 +00002332 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002333 }
drh35cd6432009-06-05 14:17:21 +00002334 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2335 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002336 goto too_big;
2337 }
danielk1977192ac1d2004-05-10 07:17:30 +00002338
shane36840fd2009-06-26 16:32:13 +00002339 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002340 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002341
drh9188b382004-05-14 21:12:22 +00002342 /* Read and parse the table header. Store the results of the parse
2343 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002344 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002345 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002346 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002347 aOffset = pC->aOffset;
2348 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002349 assert(aType);
drh856c1032009-06-02 15:21:42 +00002350 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002351 pC->aOffset = aOffset = &aType[nField];
2352 pC->payloadSize = payloadSize;
2353 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002354
drhd3194f52004-05-27 19:59:32 +00002355 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002356 if( zRec ){
2357 zData = zRec;
2358 }else{
drhf0863fe2005-06-12 21:35:51 +00002359 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002360 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002361 }else{
drhe51c44f2004-05-30 20:46:09 +00002362 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002363 }
drhe61cffc2004-06-12 18:12:15 +00002364 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2365 ** save the payload in the pC->aRow cache. That will save us from
2366 ** having to make additional calls to fetch the content portion of
2367 ** the record.
2368 */
drh35cd6432009-06-05 14:17:21 +00002369 assert( avail>=0 );
2370 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002371 zRec = zData;
2372 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002373 }else{
2374 pC->aRow = 0;
2375 }
drhd3194f52004-05-27 19:59:32 +00002376 }
drhdda5c082012-03-28 13:41:10 +00002377 /* The following assert is true in all cases except when
drh588f5bc2007-01-02 18:41:54 +00002378 ** the database file has been corrupted externally.
2379 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002380 szHdr = getVarint32((u8*)zData, offset);
2381
2382 /* Make sure a corrupt database has not given us an oversize header.
2383 ** Do this now to avoid an oversize memory allocation.
2384 **
2385 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2386 ** types use so much data space that there can only be 4096 and 32 of
2387 ** them, respectively. So the maximum header length results from a
2388 ** 3-byte type for each of the maximum of 32768 columns plus three
2389 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2390 */
2391 if( offset > 98307 ){
2392 rc = SQLITE_CORRUPT_BKPT;
2393 goto op_column_out;
2394 }
2395
2396 /* Compute in len the number of bytes of data we need to read in order
2397 ** to get nField type values. offset is an upper bound on this. But
2398 ** nField might be significantly less than the true number of columns
2399 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2400 ** We want to minimize len in order to limit the size of the memory
2401 ** allocation, especially if a corrupt database file has caused offset
2402 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2403 ** still exceed Robson memory allocation limits on some configurations.
2404 ** On systems that cannot tolerate large memory allocations, nField*5+3
2405 ** will likely be much smaller since nField will likely be less than
2406 ** 20 or so. This insures that Robson memory allocation limits are
2407 ** not exceeded even for corrupt database files.
2408 */
2409 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002410 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002411
2412 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2413 ** record header in most cases. But they will fail to get the complete
2414 ** record header if the record header does not fit on a single page
2415 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2416 ** acquire the complete header text.
2417 */
drh35cd6432009-06-05 14:17:21 +00002418 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002419 sMem.flags = 0;
2420 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002421 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002422 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002423 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002424 }
drhb6f54522004-05-20 02:42:16 +00002425 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002426 }
drh35cd6432009-06-05 14:17:21 +00002427 zEndHdr = (u8 *)&zData[len];
2428 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002429
drhd3194f52004-05-27 19:59:32 +00002430 /* Scan the header and use it to fill in the aType[] and aOffset[]
2431 ** arrays. aType[i] will contain the type integer for the i-th
2432 ** column and aOffset[i] will contain the offset from the beginning
2433 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002434 */
danielk1977dedf45b2006-01-13 17:12:01 +00002435 for(i=0; i<nField; i++){
2436 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002437 aOffset[i] = offset;
drh5a077b72011-08-29 02:16:18 +00002438 if( zIdx[0]<0x80 ){
2439 t = zIdx[0];
2440 zIdx++;
2441 }else{
2442 zIdx += sqlite3GetVarint32(zIdx, &t);
2443 }
2444 aType[i] = t;
2445 szField = sqlite3VdbeSerialTypeLen(t);
drh6658cd92010-02-05 14:12:53 +00002446 offset += szField;
2447 if( offset<szField ){ /* True if offset overflows */
2448 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2449 break;
2450 }
danielk1977dedf45b2006-01-13 17:12:01 +00002451 }else{
drhdda5c082012-03-28 13:41:10 +00002452 /* If i is less that nField, then there are fewer fields in this
danielk1977dedf45b2006-01-13 17:12:01 +00002453 ** record than SetNumColumns indicated there are columns in the
2454 ** table. Set the offset for any extra columns not present in
drhdda5c082012-03-28 13:41:10 +00002455 ** the record to 0. This tells code below to store the default value
2456 ** for the column instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002457 */
2458 aOffset[i] = 0;
2459 }
drh9188b382004-05-14 21:12:22 +00002460 }
danielk19775f096132008-03-28 15:44:09 +00002461 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002462 sMem.flags = MEM_Null;
2463
danielk19779792eef2006-01-13 15:58:43 +00002464 /* If we have read more header data than was contained in the header,
2465 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002466 ** record, or if the end of the last field appears to be before the end
2467 ** of the record (when all fields present), then we must be dealing
2468 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002469 */
drh6658cd92010-02-05 14:12:53 +00002470 if( (zIdx > zEndHdr) || (offset > payloadSize)
2471 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002472 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002473 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002474 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002475 }
danielk1977192ac1d2004-05-10 07:17:30 +00002476
danielk197736963fd2005-02-19 08:18:05 +00002477 /* Get the column information. If aOffset[p2] is non-zero, then
2478 ** deserialize the value from the record. If aOffset[p2] is zero,
2479 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002480 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002481 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002482 */
danielk197736963fd2005-02-19 08:18:05 +00002483 if( aOffset[p2] ){
2484 assert( rc==SQLITE_OK );
2485 if( zRec ){
drhac5e7492012-03-28 16:14:50 +00002486 /* This is the common case where the whole row fits on a single page */
drhe4c88c02012-01-04 12:57:45 +00002487 VdbeMemRelease(pDest);
danielk1977808ec7c2008-07-29 10:18:57 +00002488 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002489 }else{
drhac5e7492012-03-28 16:14:50 +00002490 /* This branch happens only when the row overflows onto multiple pages */
drhdda5c082012-03-28 13:41:10 +00002491 t = aType[p2];
drha748fdc2012-03-28 01:34:47 +00002492 if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
drhdda5c082012-03-28 13:41:10 +00002493 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
drha748fdc2012-03-28 01:34:47 +00002494 ){
2495 /* Content is irrelevant for the typeof() function and for
drhdda5c082012-03-28 13:41:10 +00002496 ** the length(X) function if X is a blob. So we might as well use
drha748fdc2012-03-28 01:34:47 +00002497 ** bogus content rather than reading content from disk. NULL works
2498 ** for text and blob and whatever is in the payloadSize64 variable
2499 ** will work for everything else. */
2500 zData = t<12 ? (char*)&payloadSize64 : 0;
2501 }else{
drhac5e7492012-03-28 16:14:50 +00002502 len = sqlite3VdbeSerialTypeLen(t);
drha748fdc2012-03-28 01:34:47 +00002503 sqlite3VdbeMemMove(&sMem, pDest);
2504 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,
2505 &sMem);
2506 if( rc!=SQLITE_OK ){
2507 goto op_column_out;
2508 }
2509 zData = sMem.z;
danielk197736963fd2005-02-19 08:18:05 +00002510 }
drhdda5c082012-03-28 13:41:10 +00002511 sqlite3VdbeSerialGet((u8*)zData, t, pDest);
danielk19777701e812005-01-10 12:59:51 +00002512 }
drhd4e70eb2008-01-02 00:34:36 +00002513 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002514 }else{
danielk197760585dd2008-01-03 08:08:40 +00002515 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002516 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002517 }else{
drhe6f43fc2011-08-28 02:15:34 +00002518 MemSetTypeFlag(pDest, MEM_Null);
danielk1977aee18ef2005-03-09 12:26:50 +00002519 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002520 }
drhfebe1062004-08-28 18:17:48 +00002521
2522 /* If we dynamically allocated space to hold the data (in the
2523 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002524 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002525 ** This prevents a memory copy.
2526 */
danielk19775f096132008-03-28 15:44:09 +00002527 if( sMem.zMalloc ){
2528 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002529 assert( !(pDest->flags & MEM_Dyn) );
2530 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2531 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002532 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002533 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002534 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002535 }
drhfebe1062004-08-28 18:17:48 +00002536
drhd4e70eb2008-01-02 00:34:36 +00002537 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002538
danielk19773c9cc8d2005-01-17 03:40:08 +00002539op_column_out:
drhb7654112008-01-12 12:48:07 +00002540 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002541 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002542 break;
2543}
2544
danielk1977751de562008-04-18 09:01:15 +00002545/* Opcode: Affinity P1 P2 * P4 *
drhf63552b2013-10-30 00:25:03 +00002546** Synopsis: affinity(r[P1@P2])
danielk1977751de562008-04-18 09:01:15 +00002547**
2548** Apply affinities to a range of P2 registers starting with P1.
2549**
2550** P4 is a string that is P2 characters long. The nth character of the
2551** string indicates the column affinity that should be used for the nth
2552** memory cell in the range.
2553*/
2554case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002555 const char *zAffinity; /* The affinity to be applied */
2556 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002557
drh856c1032009-06-02 15:21:42 +00002558 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002559 assert( zAffinity!=0 );
2560 assert( zAffinity[pOp->p2]==0 );
2561 pIn1 = &aMem[pOp->p1];
2562 while( (cAff = *(zAffinity++))!=0 ){
dan3bc9f742013-08-15 16:18:39 +00002563 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
drh2b4ded92010-09-27 21:09:31 +00002564 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002565 ExpandBlob(pIn1);
2566 applyAffinity(pIn1, cAff, encoding);
2567 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002568 }
2569 break;
2570}
2571
drh1db639c2008-01-17 02:36:28 +00002572/* Opcode: MakeRecord P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00002573** Synopsis: r[P3]=mkrec(r[P1@P2])
drh7a224de2004-06-02 01:22:02 +00002574**
drh710c4842010-08-30 01:17:20 +00002575** Convert P2 registers beginning with P1 into the [record format]
2576** use as a data record in a database table or as a key
2577** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002578**
danielk1977751de562008-04-18 09:01:15 +00002579** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002580** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002581** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002582**
drh8a512562005-11-14 22:29:05 +00002583** The mapping from character to affinity is given by the SQLITE_AFF_
2584** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002585**
drh66a51672008-01-03 00:01:23 +00002586** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002587*/
drh1db639c2008-01-17 02:36:28 +00002588case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002589 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2590 Mem *pRec; /* The new record */
2591 u64 nData; /* Number of bytes of data space */
2592 int nHdr; /* Number of bytes of header space */
2593 i64 nByte; /* Data space required for this record */
2594 int nZero; /* Number of zero bytes at the end of the record */
2595 int nVarint; /* Number of bytes in a varint */
2596 u32 serial_type; /* Type field */
2597 Mem *pData0; /* First field to be combined into the record */
2598 Mem *pLast; /* Last field of the record */
2599 int nField; /* Number of fields in the record */
2600 char *zAffinity; /* The affinity string for the record */
2601 int file_format; /* File format to use for encoding */
2602 int i; /* Space used in zNewRecord[] */
2603 int len; /* Length of a field */
2604
drhf3218fe2004-05-28 08:21:02 +00002605 /* Assuming the record contains N fields, the record format looks
2606 ** like this:
2607 **
drh7a224de2004-06-02 01:22:02 +00002608 ** ------------------------------------------------------------------------
2609 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2610 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002611 **
drh9cbf3422008-01-17 16:22:13 +00002612 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2613 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002614 **
2615 ** Each type field is a varint representing the serial type of the
2616 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002617 ** hdr-size field is also a varint which is the offset from the beginning
2618 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002619 */
drh856c1032009-06-02 15:21:42 +00002620 nData = 0; /* Number of bytes of data space */
2621 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002622 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002623 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002624 zAffinity = pOp->p4.z;
dan3bc9f742013-08-15 16:18:39 +00002625 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
drha6c2ed92009-11-14 23:22:23 +00002626 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002627 nField = pOp->p2;
2628 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002629 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002630
drh2b4ded92010-09-27 21:09:31 +00002631 /* Identify the output register */
2632 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2633 pOut = &aMem[pOp->p3];
2634 memAboutToChange(p, pOut);
2635
drhf3218fe2004-05-28 08:21:02 +00002636 /* Loop through the elements that will make up the record to figure
2637 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002638 */
drha2a49dc2008-01-02 14:28:13 +00002639 for(pRec=pData0; pRec<=pLast; pRec++){
drh2b4ded92010-09-27 21:09:31 +00002640 assert( memIsValid(pRec) );
drhd3d39e92004-05-20 22:16:29 +00002641 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002642 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002643 }
danielk1977d908f5a2007-05-11 07:08:28 +00002644 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002645 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002646 }
drhd946db02005-12-29 19:23:06 +00002647 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002648 len = sqlite3VdbeSerialTypeLen(serial_type);
2649 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002650 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002651 if( pRec->flags & MEM_Zero ){
2652 /* Only pure zero-filled BLOBs can be input to this Opcode.
2653 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002654 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002655 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002656 nZero = 0;
2657 }
danielk19778d059842004-05-12 11:24:02 +00002658 }
danielk19773d1bfea2004-05-14 11:00:53 +00002659
drhf3218fe2004-05-28 08:21:02 +00002660 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002661 nHdr += nVarint = sqlite3VarintLen(nHdr);
2662 if( nVarint<sqlite3VarintLen(nHdr) ){
2663 nHdr++;
2664 }
drhfdf972a2007-05-02 13:30:27 +00002665 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002666 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002667 goto too_big;
2668 }
drhf3218fe2004-05-28 08:21:02 +00002669
danielk1977a7a8e142008-02-13 18:25:27 +00002670 /* Make sure the output register has a buffer large enough to store
2671 ** the new record. The output register (pOp->p3) is not allowed to
2672 ** be one of the input registers (because the following call to
2673 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2674 */
drh9c1905f2008-12-10 22:32:56 +00002675 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002676 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002677 }
danielk1977a7a8e142008-02-13 18:25:27 +00002678 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002679
2680 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002681 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002682 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002683 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002684 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002685 }
drha2a49dc2008-01-02 14:28:13 +00002686 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002687 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002688 }
drhfdf972a2007-05-02 13:30:27 +00002689 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002690
dan3bc9f742013-08-15 16:18:39 +00002691 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drh9c1905f2008-12-10 22:32:56 +00002692 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002693 pOut->flags = MEM_Blob | MEM_Dyn;
2694 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002695 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002696 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002697 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002698 }
drh477df4b2008-01-05 18:48:24 +00002699 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002700 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002701 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002702 break;
2703}
2704
danielk1977a5533162009-02-24 10:01:51 +00002705/* Opcode: Count P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00002706** Synopsis: r[P2]=count()
danielk1977a5533162009-02-24 10:01:51 +00002707**
2708** Store the number of entries (an integer value) in the table or index
2709** opened by cursor P1 in register P2
2710*/
2711#ifndef SQLITE_OMIT_BTREECOUNT
2712case OP_Count: { /* out2-prerelease */
2713 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002714 BtCursor *pCrsr;
2715
2716 pCrsr = p->apCsr[pOp->p1]->pCursor;
dana205a482011-08-27 18:48:57 +00002717 if( ALWAYS(pCrsr) ){
drh818e39a2009-04-02 20:27:28 +00002718 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2719 }else{
2720 nEntry = 0;
2721 }
danielk1977a5533162009-02-24 10:01:51 +00002722 pOut->u.i = nEntry;
2723 break;
2724}
2725#endif
2726
danielk1977fd7f0452008-12-17 17:30:26 +00002727/* Opcode: Savepoint P1 * * P4 *
2728**
2729** Open, release or rollback the savepoint named by parameter P4, depending
2730** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2731** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2732*/
2733case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002734 int p1; /* Value of P1 operand */
2735 char *zName; /* Name of savepoint */
2736 int nName;
2737 Savepoint *pNew;
2738 Savepoint *pSavepoint;
2739 Savepoint *pTmp;
2740 int iSavepoint;
2741 int ii;
2742
2743 p1 = pOp->p1;
2744 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002745
2746 /* Assert that the p1 parameter is valid. Also that if there is no open
2747 ** transaction, then there cannot be any savepoints.
2748 */
2749 assert( db->pSavepoint==0 || db->autoCommit==0 );
2750 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2751 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2752 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002753 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002754
2755 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002756 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002757 /* A new savepoint cannot be created if there are active write
2758 ** statements (i.e. open read/write incremental blob handles).
2759 */
2760 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2761 "SQL statements in progress");
2762 rc = SQLITE_BUSY;
2763 }else{
drh856c1032009-06-02 15:21:42 +00002764 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002765
drhbe07ec52011-06-03 12:15:26 +00002766#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002767 /* This call is Ok even if this savepoint is actually a transaction
2768 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2769 ** If this is a transaction savepoint being opened, it is guaranteed
2770 ** that the db->aVTrans[] array is empty. */
2771 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002772 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2773 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002774 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002775#endif
dand9495cd2011-04-27 12:08:04 +00002776
danielk1977fd7f0452008-12-17 17:30:26 +00002777 /* Create a new savepoint structure. */
2778 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2779 if( pNew ){
2780 pNew->zName = (char *)&pNew[1];
2781 memcpy(pNew->zName, zName, nName+1);
2782
2783 /* If there is no open transaction, then mark this as a special
2784 ** "transaction savepoint". */
2785 if( db->autoCommit ){
2786 db->autoCommit = 0;
2787 db->isTransactionSavepoint = 1;
2788 }else{
2789 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002790 }
danielk1977fd7f0452008-12-17 17:30:26 +00002791
2792 /* Link the new savepoint into the database handle's list. */
2793 pNew->pNext = db->pSavepoint;
2794 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002795 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002796 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002797 }
2798 }
2799 }else{
drh856c1032009-06-02 15:21:42 +00002800 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002801
2802 /* Find the named savepoint. If there is no such savepoint, then an
2803 ** an error is returned to the user. */
2804 for(
drh856c1032009-06-02 15:21:42 +00002805 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002806 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002807 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002808 ){
2809 iSavepoint++;
2810 }
2811 if( !pSavepoint ){
2812 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2813 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002814 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002815 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002816 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002817 */
2818 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002819 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002820 );
2821 rc = SQLITE_BUSY;
2822 }else{
2823
2824 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002825 ** and this is a RELEASE command, then the current transaction
2826 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002827 */
2828 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2829 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002830 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002831 goto vdbe_return;
2832 }
danielk1977fd7f0452008-12-17 17:30:26 +00002833 db->autoCommit = 1;
2834 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2835 p->pc = pc;
2836 db->autoCommit = 0;
2837 p->rc = rc = SQLITE_BUSY;
2838 goto vdbe_return;
2839 }
danielk197734cf35d2008-12-18 18:31:38 +00002840 db->isTransactionSavepoint = 0;
2841 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002842 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002843 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002844 if( p1==SAVEPOINT_ROLLBACK ){
2845 for(ii=0; ii<db->nDb; ii++){
2846 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2847 }
drh0f198a72012-02-13 16:43:16 +00002848 }
2849 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002850 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2851 if( rc!=SQLITE_OK ){
2852 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002853 }
danielk1977fd7f0452008-12-17 17:30:26 +00002854 }
drh9f0bbf92009-01-02 21:08:09 +00002855 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002856 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002857 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002858 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002859 }
2860 }
2861
2862 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2863 ** savepoints nested inside of the savepoint being operated on. */
2864 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002865 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002866 db->pSavepoint = pTmp->pNext;
2867 sqlite3DbFree(db, pTmp);
2868 db->nSavepoint--;
2869 }
2870
dan1da40a32009-09-19 17:00:31 +00002871 /* If it is a RELEASE, then destroy the savepoint being operated on
2872 ** too. If it is a ROLLBACK TO, then set the number of deferred
2873 ** constraint violations present in the database to the value stored
2874 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002875 if( p1==SAVEPOINT_RELEASE ){
2876 assert( pSavepoint==db->pSavepoint );
2877 db->pSavepoint = pSavepoint->pNext;
2878 sqlite3DbFree(db, pSavepoint);
2879 if( !isTransaction ){
2880 db->nSavepoint--;
2881 }
dan1da40a32009-09-19 17:00:31 +00002882 }else{
2883 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002884 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002885 }
dand9495cd2011-04-27 12:08:04 +00002886
2887 if( !isTransaction ){
2888 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2889 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2890 }
danielk1977fd7f0452008-12-17 17:30:26 +00002891 }
2892 }
2893
2894 break;
2895}
2896
drh98757152008-01-09 23:04:12 +00002897/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002898**
2899** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002900** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002901** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2902** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002903**
2904** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002905*/
drh9cbf3422008-01-17 16:22:13 +00002906case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002907 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002908 int iRollback;
drh856c1032009-06-02 15:21:42 +00002909 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002910
drh856c1032009-06-02 15:21:42 +00002911 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002912 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002913 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002914 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002915 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002916 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002917 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002918
drh0f198a72012-02-13 16:43:16 +00002919#if 0
drh4f7d3a52013-06-27 23:54:02 +00002920 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002921 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002922 ** still running, and a transaction is active, return an error indicating
2923 ** that the other VMs must complete first.
2924 */
drhad4a4b82008-11-05 16:37:34 +00002925 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2926 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002927 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002928 }else
2929#endif
drh4f7d3a52013-06-27 23:54:02 +00002930 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002931 /* If this instruction implements a COMMIT and other VMs are writing
2932 ** return an error indicating that the other VMs must complete first.
2933 */
2934 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2935 "SQL statements in progress");
2936 rc = SQLITE_BUSY;
2937 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002938 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002939 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002940 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002941 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002942 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002943 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002944 }else{
shane7d3846a2008-12-11 02:58:26 +00002945 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002946 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002947 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002948 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002949 p->rc = rc = SQLITE_BUSY;
2950 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002951 }
danielk19771d850a72004-05-31 08:26:49 +00002952 }
danielk1977bd434552009-03-18 10:33:00 +00002953 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002954 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002955 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002956 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002957 }else{
drh900b31e2007-08-28 02:27:51 +00002958 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002959 }
drh900b31e2007-08-28 02:27:51 +00002960 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002961 }else{
drhf089aa42008-07-08 19:34:06 +00002962 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002963 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002964 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002965 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002966
2967 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002968 }
2969 break;
2970}
2971
drh98757152008-01-09 23:04:12 +00002972/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002973**
2974** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002975** opcode is encountered. Depending on the ON CONFLICT setting, the
2976** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002977**
drh001bbcb2003-03-19 03:14:00 +00002978** P1 is the index of the database file on which the transaction is
2979** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002980** file used for temporary tables. Indices of 2 or more are used for
2981** attached databases.
drhcabb0812002-09-14 13:47:32 +00002982**
drh80242052004-06-09 00:48:12 +00002983** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002984** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002985** other process can start another write transaction while this transaction is
2986** underway. Starting a write transaction also creates a rollback journal. A
2987** write transaction must be started before any changes can be made to the
drhf7b54962013-05-28 12:11:54 +00002988** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
2989** also obtained on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002990**
dane0af83a2009-09-08 19:15:01 +00002991** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2992** true (this flag is set if the Vdbe may modify more than one row and may
2993** throw an ABORT exception), a statement transaction may also be opened.
2994** More specifically, a statement transaction is opened iff the database
2995** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002996** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002997** VDBE to be rolled back after an error without having to roll back the
2998** entire transaction. If no error is encountered, the statement transaction
2999** will automatically commit when the VDBE halts.
3000**
danielk1977ee5741e2004-05-31 10:01:34 +00003001** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00003002*/
drh9cbf3422008-01-17 16:22:13 +00003003case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00003004 Btree *pBt;
3005
drh1713afb2013-06-28 01:24:57 +00003006 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003007 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00003008 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003009 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh13447bf2013-07-10 13:33:49 +00003010 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
3011 rc = SQLITE_READONLY;
3012 goto abort_due_to_error;
3013 }
drh653b82a2009-06-22 11:10:47 +00003014 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00003015
danielk197724162fe2004-06-04 06:22:00 +00003016 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00003017 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00003018 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00003019 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00003020 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00003021 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00003022 }
drh9e9f1bd2009-10-13 15:36:51 +00003023 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00003024 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00003025 }
dane0af83a2009-09-08 19:15:01 +00003026
3027 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00003028 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00003029 ){
3030 assert( sqlite3BtreeIsInTrans(pBt) );
3031 if( p->iStatement==0 ){
3032 assert( db->nStatement>=0 && db->nSavepoint>=0 );
3033 db->nStatement++;
3034 p->iStatement = db->nSavepoint + db->nStatement;
3035 }
dana311b802011-04-26 19:21:34 +00003036
drh346506f2011-05-25 01:16:42 +00003037 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00003038 if( rc==SQLITE_OK ){
3039 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
3040 }
dan1da40a32009-09-19 17:00:31 +00003041
3042 /* Store the current value of the database handles deferred constraint
3043 ** counter. If the statement transaction needs to be rolled back,
3044 ** the value of this counter needs to be restored too. */
3045 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00003046 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00003047 }
drhb86ccfb2003-01-28 23:13:10 +00003048 }
drh5e00f6c2001-09-13 13:46:56 +00003049 break;
3050}
3051
drhb1fdb2a2008-01-05 04:06:03 +00003052/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003053**
drh9cbf3422008-01-17 16:22:13 +00003054** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00003055** P3==1 is the schema version. P3==2 is the database format.
3056** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003057** the main database file and P1==1 is the database file used to store
3058** temporary tables.
drh4a324312001-12-21 14:30:42 +00003059**
drh50e5dad2001-09-15 00:57:28 +00003060** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003061** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003062** executing this instruction.
3063*/
drh4c583122008-01-04 22:01:03 +00003064case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003065 int iMeta;
drh856c1032009-06-02 15:21:42 +00003066 int iDb;
3067 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003068
drh1713afb2013-06-28 01:24:57 +00003069 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003070 iDb = pOp->p1;
3071 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003072 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003073 assert( iDb>=0 && iDb<db->nDb );
3074 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00003075 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00003076
danielk1977602b4662009-07-02 07:47:33 +00003077 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003078 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003079 break;
3080}
3081
drh98757152008-01-09 23:04:12 +00003082/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003083**
drh98757152008-01-09 23:04:12 +00003084** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003085** into cookie number P2 of database P1. P2==1 is the schema version.
3086** P2==2 is the database format. P2==3 is the recommended pager cache
3087** size, and so forth. P1==0 is the main database file and P1==1 is the
3088** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003089**
3090** A transaction must be started before executing this opcode.
3091*/
drh9cbf3422008-01-17 16:22:13 +00003092case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003093 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003094 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003095 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003096 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00003097 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003098 pDb = &db->aDb[pOp->p1];
3099 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003100 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003101 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003102 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003103 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003104 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3105 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003106 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003107 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003108 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003109 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003110 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003111 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003112 }
drhfd426c62006-01-30 15:34:22 +00003113 if( pOp->p1==1 ){
3114 /* Invalidate all prepared statements whenever the TEMP database
3115 ** schema is changed. Ticket #1644 */
3116 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003117 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003118 }
drh50e5dad2001-09-15 00:57:28 +00003119 break;
3120}
3121
drhc2a75552011-03-18 21:55:46 +00003122/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003123**
drh001bbcb2003-03-19 03:14:00 +00003124** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00003125** schema version) and make sure it is equal to P2 and that the
3126** generation counter on the local schema parse equals P3.
3127**
drh001bbcb2003-03-19 03:14:00 +00003128** P1 is the database number which is 0 for the main database file
3129** and 1 for the file holding temporary tables and some higher number
3130** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00003131**
3132** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00003133** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00003134** and that the current process needs to reread the schema.
3135**
3136** Either a transaction needs to have been started or an OP_Open needs
3137** to be executed (to establish a read lock) before this opcode is
3138** invoked.
3139*/
drh9cbf3422008-01-17 16:22:13 +00003140case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00003141 int iMeta;
drhc2a75552011-03-18 21:55:46 +00003142 int iGen;
drhc275b4e2004-07-19 17:25:24 +00003143 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00003144
drh001bbcb2003-03-19 03:14:00 +00003145 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003146 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh21206082011-04-04 18:22:02 +00003147 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh1713afb2013-06-28 01:24:57 +00003148 assert( p->bIsReader );
drhc275b4e2004-07-19 17:25:24 +00003149 pBt = db->aDb[pOp->p1].pBt;
3150 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00003151 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00003152 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00003153 }else{
drhfcd71b62011-04-05 22:08:24 +00003154 iGen = iMeta = 0;
drhc275b4e2004-07-19 17:25:24 +00003155 }
drhc2a75552011-03-18 21:55:46 +00003156 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00003157 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00003158 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00003159 /* If the schema-cookie from the database file matches the cookie
3160 ** stored with the in-memory representation of the schema, do
3161 ** not reload the schema from the database file.
3162 **
shane21e7feb2008-05-30 15:59:49 +00003163 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00003164 ** Often, v-tables store their data in other SQLite tables, which
3165 ** are queried from within xNext() and other v-table methods using
3166 ** prepared queries. If such a query is out-of-date, we do not want to
3167 ** discard the database schema, as the user code implementing the
3168 ** v-table would have to be ready for the sqlite3_vtab structure itself
3169 ** to be invalidated whenever sqlite3_step() is called from within
3170 ** a v-table method.
3171 */
3172 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
drh81028a42012-05-15 18:28:27 +00003173 sqlite3ResetOneSchema(db, pOp->p1);
danielk1977896e7922007-04-17 08:32:33 +00003174 }
3175
drh5b6c5452011-02-22 03:34:56 +00003176 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00003177 rc = SQLITE_SCHEMA;
3178 }
3179 break;
3180}
3181
drh98757152008-01-09 23:04:12 +00003182/* Opcode: OpenRead P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003183** Synopsis: root=P2 iDb=P3
drh5e00f6c2001-09-13 13:46:56 +00003184**
drhecdc7532001-09-23 02:35:53 +00003185** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003186** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003187** P3==0 means the main database, P3==1 means the database used for
3188** temporary tables, and P3>1 means used the corresponding attached
3189** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003190** values need not be contiguous but all P1 values should be small integers.
3191** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003192**
drh98757152008-01-09 23:04:12 +00003193** If P5!=0 then use the content of register P2 as the root page, not
3194** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003195**
drhb19a2bc2001-09-16 00:13:26 +00003196** There will be a read lock on the database whenever there is an
3197** open cursor. If the database was unlocked prior to this instruction
3198** then a read lock is acquired as part of this instruction. A read
3199** lock allows other processes to read the database but prohibits
3200** any other process from modifying the database. The read lock is
3201** released when all cursors are closed. If this instruction attempts
3202** to get a read lock but fails, the script terminates with an
3203** SQLITE_BUSY error code.
3204**
danielk1977d336e222009-02-20 10:58:41 +00003205** The P4 value may be either an integer (P4_INT32) or a pointer to
3206** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3207** structure, then said structure defines the content and collating
3208** sequence of the index being opened. Otherwise, if P4 is an integer
3209** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003210**
drh001bbcb2003-03-19 03:14:00 +00003211** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003212*/
drh98757152008-01-09 23:04:12 +00003213/* Opcode: OpenWrite P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00003214** Synopsis: root=P2 iDb=P3
drhecdc7532001-09-23 02:35:53 +00003215**
3216** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003217** page is P2. Or if P5!=0 use the content of register P2 to find the
3218** root page.
drhecdc7532001-09-23 02:35:53 +00003219**
danielk1977d336e222009-02-20 10:58:41 +00003220** The P4 value may be either an integer (P4_INT32) or a pointer to
3221** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3222** structure, then said structure defines the content and collating
3223** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003224** value, it is set to the number of columns in the table, or to the
3225** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003226**
drh001bbcb2003-03-19 03:14:00 +00003227** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003228** in read/write mode. For a given table, there can be one or more read-only
3229** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003230**
drh001bbcb2003-03-19 03:14:00 +00003231** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003232*/
drh9cbf3422008-01-17 16:22:13 +00003233case OP_OpenRead:
3234case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003235 int nField;
3236 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003237 int p2;
3238 int iDb;
drhf57b3392001-10-08 13:22:32 +00003239 int wrFlag;
3240 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003241 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003242 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003243
dan428c2182012-08-06 18:50:11 +00003244 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3245 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003246 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003247 assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003248
danfa401de2009-10-16 14:55:03 +00003249 if( p->expired ){
3250 rc = SQLITE_ABORT;
3251 break;
3252 }
3253
drh856c1032009-06-02 15:21:42 +00003254 nField = 0;
3255 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003256 p2 = pOp->p2;
3257 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003258 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003259 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003260 pDb = &db->aDb[iDb];
3261 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003262 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003263 if( pOp->opcode==OP_OpenWrite ){
3264 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003265 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003266 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3267 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003268 }
3269 }else{
3270 wrFlag = 0;
3271 }
dan428c2182012-08-06 18:50:11 +00003272 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003273 assert( p2>0 );
dan3bc9f742013-08-15 16:18:39 +00003274 assert( p2<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003275 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003276 assert( memIsValid(pIn2) );
3277 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003278 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003279 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003280 /* The p2 value always comes from a prior OP_CreateTable opcode and
3281 ** that opcode will always set the p2 value to 2 or more or else fail.
3282 ** If there were a failure, the prepared statement would have halted
3283 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003284 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003285 rc = SQLITE_CORRUPT_BKPT;
3286 goto abort_due_to_error;
3287 }
drh5edc3122001-09-13 21:53:09 +00003288 }
danielk1977d336e222009-02-20 10:58:41 +00003289 if( pOp->p4type==P4_KEYINFO ){
3290 pKeyInfo = pOp->p4.pKeyInfo;
3291 pKeyInfo->enc = ENC(p->db);
drhad124322013-10-23 13:30:58 +00003292 nField = pKeyInfo->nField+pKeyInfo->nXField;
danielk1977d336e222009-02-20 10:58:41 +00003293 }else if( pOp->p4type==P4_INT32 ){
3294 nField = pOp->p4.i;
3295 }
drh653b82a2009-06-22 11:10:47 +00003296 assert( pOp->p1>=0 );
3297 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003298 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003299 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003300 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003301 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3302 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003303 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3304 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003305
dana205a482011-08-27 18:48:57 +00003306 /* Since it performs no memory allocation or IO, the only value that
3307 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3308 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003309
3310 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3311 ** SQLite used to check if the root-page flags were sane at this point
3312 ** and report database corruption if they were not, but this check has
3313 ** since moved into the btree layer. */
3314 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3315 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003316 break;
3317}
3318
drh2a5d9902011-08-26 00:34:45 +00003319/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh81316f82013-10-29 20:40:47 +00003320** Synopsis: nColumn=P2
drh5e00f6c2001-09-13 13:46:56 +00003321**
drhb9bb7c12006-06-11 23:41:55 +00003322** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003323** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003324** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003325** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003326**
drh25d3adb2010-04-05 15:11:08 +00003327** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003328** The cursor points to a BTree table if P4==0 and to a BTree index
3329** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003330** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003331**
drh2a5d9902011-08-26 00:34:45 +00003332** The P5 parameter can be a mask of the BTREE_* flags defined
3333** in btree.h. These flags control aspects of the operation of
3334** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3335** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003336*/
drha21a64d2010-04-06 22:33:55 +00003337/* Opcode: OpenAutoindex P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00003338** Synopsis: nColumn=P2
drha21a64d2010-04-06 22:33:55 +00003339**
3340** This opcode works the same as OP_OpenEphemeral. It has a
3341** different name to distinguish its use. Tables created using
3342** by this opcode will be used for automatically created transient
3343** indices in joins.
3344*/
3345case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003346case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003347 VdbeCursor *pCx;
drhd4187c72010-08-30 22:15:45 +00003348 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003349 SQLITE_OPEN_READWRITE |
3350 SQLITE_OPEN_CREATE |
3351 SQLITE_OPEN_EXCLUSIVE |
3352 SQLITE_OPEN_DELETEONCLOSE |
3353 SQLITE_OPEN_TRANSIENT_DB;
3354
drh653b82a2009-06-22 11:10:47 +00003355 assert( pOp->p1>=0 );
3356 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003357 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003358 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003359 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3360 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003361 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003362 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003363 }
3364 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003365 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003366 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003367 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003368 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003369 */
danielk19772dca4ac2008-01-03 11:50:29 +00003370 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003371 int pgno;
drh66a51672008-01-03 00:01:23 +00003372 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003373 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003374 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003375 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003376 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003377 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003378 pCx->pKeyInfo = pOp->p4.pKeyInfo;
dan689ab892011-08-12 15:02:00 +00003379 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003380 }
drhf0863fe2005-06-12 21:35:51 +00003381 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003382 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003383 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003384 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003385 }
drh5e00f6c2001-09-13 13:46:56 +00003386 }
drhd4187c72010-08-30 22:15:45 +00003387 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drhf0863fe2005-06-12 21:35:51 +00003388 pCx->isIndex = !pCx->isTable;
dan5134d132011-09-02 10:31:11 +00003389 break;
3390}
3391
drh1153c7b2013-11-01 22:02:56 +00003392/* Opcode: SorterOpen P1 * * P4 *
dan5134d132011-09-02 10:31:11 +00003393**
3394** This opcode works like OP_OpenEphemeral except that it opens
3395** a transient index that is specifically designed to sort large
3396** tables using an external merge-sort algorithm.
3397*/
drhca892a72011-09-03 00:17:51 +00003398case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003399 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003400
dan5134d132011-09-02 10:31:11 +00003401 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3402 if( pCx==0 ) goto no_mem;
3403 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3404 pCx->pKeyInfo->enc = ENC(p->db);
3405 pCx->isSorter = 1;
3406 rc = sqlite3VdbeSorterInit(db, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003407 break;
3408}
3409
drh980db4b2012-10-30 14:44:14 +00003410/* Opcode: OpenPseudo P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00003411** Synopsis: content in r[P2@P3]
drh70ce3f02003-04-15 19:22:22 +00003412**
3413** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003414** row of data. The content of that one row in the content of memory
drh21172c42012-10-30 00:29:07 +00003415** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
3416** MEM_Blob content contained in register P2. When P5==1, then the
3417** row is represented by P3 consecutive registers beginning with P2.
drh70ce3f02003-04-15 19:22:22 +00003418**
drh2d8d7ce2010-02-15 15:17:05 +00003419** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003420** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003421** individual columns using the OP_Column opcode. The OP_Column opcode
3422** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003423**
3424** P3 is the number of fields in the records that will be stored by
3425** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003426*/
drh9cbf3422008-01-17 16:22:13 +00003427case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003428 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003429
drh653b82a2009-06-22 11:10:47 +00003430 assert( pOp->p1>=0 );
3431 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003432 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003433 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003434 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003435 pCx->isTable = 1;
3436 pCx->isIndex = 0;
drh21172c42012-10-30 00:29:07 +00003437 pCx->multiPseudo = pOp->p5;
drh70ce3f02003-04-15 19:22:22 +00003438 break;
3439}
3440
drh98757152008-01-09 23:04:12 +00003441/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003442**
3443** Close a cursor previously opened as P1. If P1 is not
3444** currently open, this instruction is a no-op.
3445*/
drh9cbf3422008-01-17 16:22:13 +00003446case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003447 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3448 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3449 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003450 break;
3451}
3452
drh959403f2008-12-12 17:56:16 +00003453/* Opcode: SeekGe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003454** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003455**
danielk1977b790c6c2008-04-18 10:25:24 +00003456** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003457** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003458** to an SQL index, then P3 is the first in an array of P4 registers
3459** that are used as an unpacked index key.
3460**
3461** Reposition cursor P1 so that it points to the smallest entry that
3462** is greater than or equal to the key value. If there are no records
3463** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003464**
drh959403f2008-12-12 17:56:16 +00003465** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003466*/
drh959403f2008-12-12 17:56:16 +00003467/* Opcode: SeekGt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003468** Synopsis: key=r[P3@P4]
drh7cf6e4d2004-05-19 14:56:55 +00003469**
danielk1977b790c6c2008-04-18 10:25:24 +00003470** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003471** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003472** to an SQL index, then P3 is the first in an array of P4 registers
3473** that are used as an unpacked index key.
3474**
3475** Reposition cursor P1 so that it points to the smallest entry that
3476** is greater than the key value. If there are no records greater than
3477** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003478**
drh959403f2008-12-12 17:56:16 +00003479** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003480*/
drh959403f2008-12-12 17:56:16 +00003481/* Opcode: SeekLt P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003482** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00003483**
danielk1977b790c6c2008-04-18 10:25:24 +00003484** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003485** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003486** to an SQL index, then P3 is the first in an array of P4 registers
3487** that are used as an unpacked index key.
3488**
3489** Reposition cursor P1 so that it points to the largest entry that
3490** is less than the key value. If there are no records less than
3491** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003492**
drh959403f2008-12-12 17:56:16 +00003493** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003494*/
drh959403f2008-12-12 17:56:16 +00003495/* Opcode: SeekLe P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003496** Synopsis: key=r[P3@P4]
danielk19773d1bfea2004-05-14 11:00:53 +00003497**
danielk1977b790c6c2008-04-18 10:25:24 +00003498** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003499** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003500** to an SQL index, then P3 is the first in an array of P4 registers
3501** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003502**
danielk1977b790c6c2008-04-18 10:25:24 +00003503** Reposition cursor P1 so that it points to the largest entry that
3504** is less than or equal to the key value. If there are no records
3505** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003506**
drh959403f2008-12-12 17:56:16 +00003507** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003508*/
drh959403f2008-12-12 17:56:16 +00003509case OP_SeekLt: /* jump, in3 */
3510case OP_SeekLe: /* jump, in3 */
3511case OP_SeekGe: /* jump, in3 */
3512case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003513 int res;
3514 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003515 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003516 UnpackedRecord r;
3517 int nField;
3518 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003519
drh653b82a2009-06-22 11:10:47 +00003520 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003521 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003522 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003523 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003524 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003525 assert( OP_SeekLe == OP_SeekLt+1 );
3526 assert( OP_SeekGe == OP_SeekLt+2 );
3527 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003528 assert( pC->isOrdered );
dana205a482011-08-27 18:48:57 +00003529 if( ALWAYS(pC->pCursor!=0) ){
drh7cf6e4d2004-05-19 14:56:55 +00003530 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003531 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003532 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003533 /* The input value in P3 might be of any type: integer, real, string,
3534 ** blob, or NULL. But it needs to be an integer before we can do
3535 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003536 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003537 applyNumericAffinity(pIn3);
3538 iKey = sqlite3VdbeIntValue(pIn3);
3539 pC->rowidIsValid = 0;
3540
3541 /* If the P3 value could not be converted into an integer without
3542 ** loss of information, then special processing is required... */
3543 if( (pIn3->flags & MEM_Int)==0 ){
3544 if( (pIn3->flags & MEM_Real)==0 ){
3545 /* If the P3 value cannot be converted into any kind of a number,
3546 ** then the seek is not possible, so jump to P2 */
3547 pc = pOp->p2 - 1;
3548 break;
3549 }
3550 /* If we reach this point, then the P3 value must be a floating
3551 ** point number. */
3552 assert( (pIn3->flags & MEM_Real)!=0 );
3553
3554 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003555 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003556 ** integer. */
3557 res = 1;
3558 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003559 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003560 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3561 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3562 }
3563 }else{
drh1f350122009-11-13 20:52:43 +00003564 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003565 rc = sqlite3BtreeLast(pC->pCursor, &res);
3566 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3567 }
3568 }
3569 if( res ){
3570 pc = pOp->p2 - 1;
3571 }
3572 break;
3573 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3574 /* Use the ceiling() function to convert real->int */
3575 if( pIn3->r > (double)iKey ) iKey++;
3576 }else{
3577 /* Use the floor() function to convert real->int */
3578 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3579 if( pIn3->r < (double)iKey ) iKey--;
3580 }
3581 }
drhe63d9992008-08-13 19:11:48 +00003582 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003583 if( rc!=SQLITE_OK ){
3584 goto abort_due_to_error;
3585 }
drh959403f2008-12-12 17:56:16 +00003586 if( res==0 ){
3587 pC->rowidIsValid = 1;
3588 pC->lastRowid = iKey;
3589 }
drh5e00f6c2001-09-13 13:46:56 +00003590 }else{
drh856c1032009-06-02 15:21:42 +00003591 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003592 assert( pOp->p4type==P4_INT32 );
3593 assert( nField>0 );
3594 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003595 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003596
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 ** }
3603 */
danfbfe3882013-04-08 10:38:57 +00003604 r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003605 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
drha6c2ed92009-11-14 23:22:23 +00003610 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003611#ifdef SQLITE_DEBUG
3612 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3613#endif
drh039fc322009-11-17 18:31:47 +00003614 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003615 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003616 if( rc!=SQLITE_OK ){
3617 goto abort_due_to_error;
3618 }
drhf0863fe2005-06-12 21:35:51 +00003619 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003620 }
drha11846b2004-01-07 18:52:56 +00003621 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003622 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003623#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003624 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003625#endif
drh1f350122009-11-13 20:52:43 +00003626 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003627 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003628 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003629 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003630 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003631 }else{
3632 res = 0;
drh8721ce42001-11-07 14:22:00 +00003633 }
drh7cf6e4d2004-05-19 14:56:55 +00003634 }else{
drh959403f2008-12-12 17:56:16 +00003635 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3636 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003637 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3638 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003639 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003640 }else{
3641 /* res might be negative because the table is empty. Check to
3642 ** see if this is the case.
3643 */
drhf328bc82004-05-10 23:29:49 +00003644 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003645 }
drh1af3fdb2004-07-18 21:33:01 +00003646 }
drh91fd4d42008-01-19 20:11:25 +00003647 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003648 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003649 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003650 }
drhaa736092009-06-22 00:55:30 +00003651 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003652 /* This happens when attempting to open the sqlite3_master table
3653 ** for read access returns SQLITE_EMPTY. In this case always
3654 ** take the jump (since there are no records in the table).
3655 */
3656 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003657 }
drh5e00f6c2001-09-13 13:46:56 +00003658 break;
3659}
3660
drh959403f2008-12-12 17:56:16 +00003661/* Opcode: Seek P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003662** Synopsis: intkey=r[P2]
drh959403f2008-12-12 17:56:16 +00003663**
3664** P1 is an open table cursor and P2 is a rowid integer. Arrange
3665** for P1 to move so that it points to the rowid given by P2.
3666**
3667** This is actually a deferred seek. Nothing actually happens until
3668** the cursor is used to read a record. That way, if no reads
3669** occur, no unnecessary I/O happens.
3670*/
3671case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003672 VdbeCursor *pC;
3673
drh653b82a2009-06-22 11:10:47 +00003674 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3675 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003676 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003677 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003678 assert( pC->isTable );
3679 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003680 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003681 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3682 pC->rowidIsValid = 0;
3683 pC->deferredMoveto = 1;
3684 }
3685 break;
3686}
3687
3688
drh8cff69d2009-11-12 19:59:44 +00003689/* Opcode: Found P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003690** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003691**
drh8cff69d2009-11-12 19:59:44 +00003692** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3693** P4>0 then register P3 is the first of P4 registers that form an unpacked
3694** record.
3695**
3696** Cursor P1 is on an index btree. If the record identified by P3 and P4
3697** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003698** P1 is left pointing at the matching entry.
drh6f225d02013-10-26 13:36:51 +00003699**
3700** See also: NotFound, NoConflict, NotExists. SeekGe
drh5e00f6c2001-09-13 13:46:56 +00003701*/
drh8cff69d2009-11-12 19:59:44 +00003702/* Opcode: NotFound P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00003703** Synopsis: key=r[P3@P4]
drh5e00f6c2001-09-13 13:46:56 +00003704**
drh8cff69d2009-11-12 19:59:44 +00003705** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3706** P4>0 then register P3 is the first of P4 registers that form an unpacked
3707** record.
3708**
3709** Cursor P1 is on an index btree. If the record identified by P3 and P4
3710** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3711** does contain an entry whose prefix matches the P3/P4 record then control
3712** falls through to the next instruction and P1 is left pointing at the
3713** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003714**
drh6f225d02013-10-26 13:36:51 +00003715** See also: Found, NotExists, NoConflict
drh5e00f6c2001-09-13 13:46:56 +00003716*/
drh6f225d02013-10-26 13:36:51 +00003717/* Opcode: NoConflict P1 P2 P3 P4 *
drh4af5bee2013-10-30 02:37:50 +00003718** Synopsis: key=r[P3@P4]
drh6f225d02013-10-26 13:36:51 +00003719**
3720** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3721** P4>0 then register P3 is the first of P4 registers that form an unpacked
3722** record.
3723**
3724** Cursor P1 is on an index btree. If the record identified by P3 and P4
3725** contains any NULL value, jump immediately to P2. If all terms of the
3726** record are not-NULL then a check is done to determine if any row in the
3727** P1 index btree has a matching key prefix. If there are no matches, jump
3728** immediately to P2. If there is a match, fall through and leave the P1
3729** cursor pointing to the matching row.
3730**
3731** This opcode is similar to OP_NotFound with the exceptions that the
3732** branch is always taken if any part of the search key input is NULL.
3733**
3734** See also: NotFound, Found, NotExists
3735*/
3736case OP_NoConflict: /* jump, in3 */
drh9cbf3422008-01-17 16:22:13 +00003737case OP_NotFound: /* jump, in3 */
3738case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003739 int alreadyExists;
drh6f225d02013-10-26 13:36:51 +00003740 int ii;
drhdfe88ec2008-11-03 20:55:06 +00003741 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003742 int res;
dan03e9cfc2011-09-05 14:20:27 +00003743 char *pFree;
drh856c1032009-06-02 15:21:42 +00003744 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003745 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003746 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3747
dan0ff297e2009-09-25 17:03:14 +00003748#ifdef SQLITE_TEST
drh6f225d02013-10-26 13:36:51 +00003749 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
dan0ff297e2009-09-25 17:03:14 +00003750#endif
3751
drh856c1032009-06-02 15:21:42 +00003752 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003753 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003754 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003755 pC = p->apCsr[pOp->p1];
3756 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003757 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003758 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003759
drhf0863fe2005-06-12 21:35:51 +00003760 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003761 if( pOp->p4.i>0 ){
3762 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003763 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003764 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003765#ifdef SQLITE_DEBUG
drh6fbe41a2013-10-30 20:22:55 +00003766 {
3767 int i;
3768 for(i=0; i<r.nField; i++){
3769 assert( memIsValid(&r.aMem[i]) );
3770 if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
3771 }
3772 }
drh2b4ded92010-09-27 21:09:31 +00003773#endif
drh8cff69d2009-11-12 19:59:44 +00003774 r.flags = UNPACKED_PREFIX_MATCH;
3775 pIdxKey = &r;
3776 }else{
dan03e9cfc2011-09-05 14:20:27 +00003777 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3778 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3779 );
3780 if( pIdxKey==0 ) goto no_mem;
drh8cff69d2009-11-12 19:59:44 +00003781 assert( pIn3->flags & MEM_Blob );
drhd81a1422010-09-28 07:11:24 +00003782 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
dan03e9cfc2011-09-05 14:20:27 +00003783 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh8cff69d2009-11-12 19:59:44 +00003784 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003785 }
drh6f225d02013-10-26 13:36:51 +00003786 if( pOp->opcode==OP_NoConflict ){
3787 /* For the OP_NoConflict opcode, take the jump if any of the
3788 ** input fields are NULL, since any key with a NULL will not
3789 ** conflict */
3790 for(ii=0; ii<r.nField; ii++){
3791 if( r.aMem[ii].flags & MEM_Null ){
3792 pc = pOp->p2 - 1;
3793 break;
3794 }
3795 }
3796 }
drhe63d9992008-08-13 19:11:48 +00003797 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003798 if( pOp->p4.i==0 ){
dan03e9cfc2011-09-05 14:20:27 +00003799 sqlite3DbFree(db, pFree);
drh8cff69d2009-11-12 19:59:44 +00003800 }
danielk197777519402007-08-30 11:48:31 +00003801 if( rc!=SQLITE_OK ){
3802 break;
3803 }
3804 alreadyExists = (res==0);
drh261c02d2013-10-25 14:46:15 +00003805 pC->nullRow = 1-alreadyExists;
drha11846b2004-01-07 18:52:56 +00003806 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003807 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003808 }
3809 if( pOp->opcode==OP_Found ){
3810 if( alreadyExists ) pc = pOp->p2 - 1;
3811 }else{
3812 if( !alreadyExists ) pc = pOp->p2 - 1;
3813 }
drh5e00f6c2001-09-13 13:46:56 +00003814 break;
3815}
3816
drh9cbf3422008-01-17 16:22:13 +00003817/* Opcode: NotExists P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003818** Synopsis: intkey=r[P3]
drh6b125452002-01-28 15:53:03 +00003819**
drh261c02d2013-10-25 14:46:15 +00003820** P1 is the index of a cursor open on an SQL table btree (with integer
3821** keys). P3 is an integer rowid. If P1 does not contain a record with
3822** rowid P3 then jump immediately to P2. If P1 does contain a record
3823** with rowid P3 then leave the cursor pointing at that record and fall
3824** through to the next instruction.
drh6b125452002-01-28 15:53:03 +00003825**
drh261c02d2013-10-25 14:46:15 +00003826** The OP_NotFound opcode performs the same operation on index btrees
3827** (with arbitrary multi-value keys).
drh6b125452002-01-28 15:53:03 +00003828**
drh11e85272013-10-26 15:40:48 +00003829** See also: Found, NotFound, NoConflict
drh6b125452002-01-28 15:53:03 +00003830*/
drh9cbf3422008-01-17 16:22:13 +00003831case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003832 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003833 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003834 int res;
3835 u64 iKey;
3836
drh3c657212009-11-17 23:59:58 +00003837 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003838 assert( pIn3->flags & MEM_Int );
3839 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3840 pC = p->apCsr[pOp->p1];
3841 assert( pC!=0 );
3842 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003843 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003844 pCrsr = pC->pCursor;
dana205a482011-08-27 18:48:57 +00003845 if( ALWAYS(pCrsr!=0) ){
drh856c1032009-06-02 15:21:42 +00003846 res = 0;
drhaa736092009-06-22 00:55:30 +00003847 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003848 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003849 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003850 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003851 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003852 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003853 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003854 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003855 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003856 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003857 }
danielk1977de630352009-05-04 11:42:29 +00003858 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003859 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003860 /* This happens when an attempt to open a read cursor on the
3861 ** sqlite_master table returns SQLITE_EMPTY.
3862 */
danielk1977f7b9d662008-06-23 18:49:43 +00003863 pc = pOp->p2 - 1;
3864 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003865 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003866 }
drh6b125452002-01-28 15:53:03 +00003867 break;
3868}
3869
drh4c583122008-01-04 22:01:03 +00003870/* Opcode: Sequence P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00003871** Synopsis: r[P2]=rowid
drh4db38a72005-09-01 12:16:28 +00003872**
drh4c583122008-01-04 22:01:03 +00003873** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003874** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003875** The sequence number on the cursor is incremented after this
3876** instruction.
drh4db38a72005-09-01 12:16:28 +00003877*/
drh4c583122008-01-04 22:01:03 +00003878case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003879 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3880 assert( p->apCsr[pOp->p1]!=0 );
3881 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003882 break;
3883}
3884
3885
drh98757152008-01-09 23:04:12 +00003886/* Opcode: NewRowid P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00003887** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00003888**
drhf0863fe2005-06-12 21:35:51 +00003889** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003890** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003891** table that cursor P1 points to. The new record number is written
3892** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003893**
dan76d462e2009-08-30 11:42:51 +00003894** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3895** the largest previously generated record number. No new record numbers are
3896** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003897** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003898** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003899** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003900*/
drh4c583122008-01-04 22:01:03 +00003901case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003902 i64 v; /* The new rowid */
3903 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3904 int res; /* Result of an sqlite3BtreeLast() */
3905 int cnt; /* Counter to limit the number of searches */
3906 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003907 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003908
drh856c1032009-06-02 15:21:42 +00003909 v = 0;
3910 res = 0;
drhaa736092009-06-22 00:55:30 +00003911 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3912 pC = p->apCsr[pOp->p1];
3913 assert( pC!=0 );
3914 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003915 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003916 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003917 /* The next rowid or record number (different terms for the same
3918 ** thing) is obtained in a two-step algorithm.
3919 **
3920 ** First we attempt to find the largest existing rowid and add one
3921 ** to that. But if the largest existing rowid is already the maximum
3922 ** positive integer, we have to fall through to the second
3923 ** probabilistic algorithm
3924 **
3925 ** The second algorithm is to select a rowid at random and see if
3926 ** it already exists in the table. If it does not exist, we have
3927 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003928 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003929 */
drhaa736092009-06-22 00:55:30 +00003930 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003931
drh75f86a42005-02-17 00:03:06 +00003932#ifdef SQLITE_32BIT_ROWID
3933# define MAX_ROWID 0x7fffffff
3934#else
drhfe2093d2005-01-20 22:48:47 +00003935 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3936 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3937 ** to provide the constant while making all compilers happy.
3938 */
danielk197764202cf2008-11-17 15:31:47 +00003939# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003940#endif
drhfe2093d2005-01-20 22:48:47 +00003941
drh5cf8e8c2002-02-19 22:42:05 +00003942 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003943 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3944 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003945 rc = sqlite3BtreeLast(pC->pCursor, &res);
3946 if( rc!=SQLITE_OK ){
3947 goto abort_due_to_error;
3948 }
drh32fbe342002-10-19 20:16:37 +00003949 if( res ){
drhc79c7612010-01-01 18:57:48 +00003950 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003951 }else{
drhea8ffdf2009-07-22 00:35:23 +00003952 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003953 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3954 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drha40eb7c2012-02-24 00:02:28 +00003955 if( v>=MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003956 pC->useRandomRowid = 1;
3957 }else{
drhc79c7612010-01-01 18:57:48 +00003958 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003959 }
drh5cf8e8c2002-02-19 22:42:05 +00003960 }
drh3fc190c2001-09-14 03:24:23 +00003961 }
drh205f48e2004-11-05 00:43:11 +00003962
3963#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003964 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003965 /* Assert that P3 is a valid memory cell. */
3966 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003967 if( p->pFrame ){
3968 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003969 /* Assert that P3 is a valid memory cell. */
3970 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003971 pMem = &pFrame->aMem[pOp->p3];
3972 }else{
shaneabc6b892009-09-10 19:09:03 +00003973 /* Assert that P3 is a valid memory cell. */
dan3bc9f742013-08-15 16:18:39 +00003974 assert( pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00003975 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003976 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003977 }
drh2b4ded92010-09-27 21:09:31 +00003978 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003979
3980 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003981 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003982 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003983 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003984 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003985 goto abort_due_to_error;
3986 }
drh3c024d62007-03-30 11:23:45 +00003987 if( v<pMem->u.i+1 ){
3988 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003989 }
drh3c024d62007-03-30 11:23:45 +00003990 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003991 }
3992#endif
3993
drh7f751222009-03-17 22:33:00 +00003994 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003995 }
3996 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003997 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003998 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003999 ** engine starts picking positive candidate ROWIDs at random until
4000 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00004001 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
4002 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00004003 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00004004 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00004005 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
4006 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00004007 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00004008 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
4009 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00004010 && (res==0)
4011 && (++cnt<100)){
4012 /* collision - try another random rowid */
4013 sqlite3_randomness(sizeof(v), &v);
4014 if( cnt<5 ){
4015 /* try "small" random rowids for the initial attempts */
4016 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00004017 }else{
shanehc4d340a2010-09-01 02:37:56 +00004018 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00004019 }
shanehc4d340a2010-09-01 02:37:56 +00004020 v++; /* ensure non-zero */
4021 }
drhaa736092009-06-22 00:55:30 +00004022 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004023 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004024 goto abort_due_to_error;
4025 }
drh748a52c2010-09-01 11:50:08 +00004026 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004027 }
drhf0863fe2005-06-12 21:35:51 +00004028 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00004029 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004030 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004031 }
drh4c583122008-01-04 22:01:03 +00004032 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004033 break;
4034}
4035
danielk19771f4aa332008-01-03 09:51:55 +00004036/* Opcode: Insert P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004037** Synopsis: intkey=r[P3] data=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004038**
jplyon5a564222003-06-02 06:15:58 +00004039** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004040** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004041** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004042** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004043** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004044**
danielk19771f4aa332008-01-03 09:51:55 +00004045** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4046** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004047** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004048** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004049**
drh3e9ca092009-09-08 01:14:48 +00004050** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4051** the last seek operation (OP_NotExists) was a success, then this
4052** operation will not attempt to find the appropriate row before doing
4053** the insert but will instead overwrite the row that the cursor is
4054** currently pointing to. Presumably, the prior OP_NotExists opcode
4055** has already positioned the cursor correctly. This is an optimization
4056** that boosts performance by avoiding redundant seeks.
4057**
4058** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4059** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4060** is part of an INSERT operation. The difference is only important to
4061** the update hook.
4062**
drh66a51672008-01-03 00:01:23 +00004063** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004064** may be NULL. If it is not NULL, then the update-hook
4065** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4066**
drh93aed5a2008-01-16 17:46:38 +00004067** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4068** allocated, then ownership of P2 is transferred to the pseudo-cursor
4069** and register P2 becomes ephemeral. If the cursor is changed, the
4070** value of register P2 will then change. Make sure this does not
4071** cause any problems.)
4072**
drhf0863fe2005-06-12 21:35:51 +00004073** This instruction only works on tables. The equivalent instruction
4074** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004075*/
drhe05c9292009-10-29 13:48:10 +00004076/* Opcode: InsertInt P1 P2 P3 P4 P5
drh81316f82013-10-29 20:40:47 +00004077** Synopsis: intkey=P3 data=r[P2]
drhe05c9292009-10-29 13:48:10 +00004078**
4079** This works exactly like OP_Insert except that the key is the
4080** integer value P3, not the value of the integer stored in register P3.
4081*/
4082case OP_Insert:
4083case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004084 Mem *pData; /* MEM cell holding data for the record to be inserted */
4085 Mem *pKey; /* MEM cell holding key for the record */
4086 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4087 VdbeCursor *pC; /* Cursor to table into which insert is written */
4088 int nZero; /* Number of zero-bytes to append */
4089 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4090 const char *zDb; /* database name - used by the update hook */
4091 const char *zTbl; /* Table name - used by the opdate hook */
4092 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004093
drha6c2ed92009-11-14 23:22:23 +00004094 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004095 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004096 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004097 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004098 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004099 assert( pC->pCursor!=0 );
4100 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004101 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004102 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004103
drhe05c9292009-10-29 13:48:10 +00004104 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004105 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004106 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004107 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004108 REGISTER_TRACE(pOp->p3, pKey);
4109 iKey = pKey->u.i;
4110 }else{
4111 assert( pOp->opcode==OP_InsertInt );
4112 iKey = pOp->p3;
4113 }
4114
drha05a7222008-01-19 03:35:58 +00004115 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004116 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004117 if( pData->flags & MEM_Null ){
4118 pData->z = 0;
4119 pData->n = 0;
4120 }else{
4121 assert( pData->flags & (MEM_Blob|MEM_Str) );
4122 }
drh3e9ca092009-09-08 01:14:48 +00004123 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4124 if( pData->flags & MEM_Zero ){
4125 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004126 }else{
drh3e9ca092009-09-08 01:14:48 +00004127 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004128 }
drh3e9ca092009-09-08 01:14:48 +00004129 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
4130 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4131 pData->z, pData->n, nZero,
4132 pOp->p5 & OPFLAG_APPEND, seekResult
4133 );
drha05a7222008-01-19 03:35:58 +00004134 pC->rowidIsValid = 0;
4135 pC->deferredMoveto = 0;
4136 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004137
drha05a7222008-01-19 03:35:58 +00004138 /* Invoke the update-hook if required. */
4139 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004140 zDb = db->aDb[pC->iDb].zName;
4141 zTbl = pOp->p4.z;
4142 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004143 assert( pC->isTable );
4144 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4145 assert( pC->iDb>=0 );
4146 }
drh5e00f6c2001-09-13 13:46:56 +00004147 break;
4148}
4149
drh98757152008-01-09 23:04:12 +00004150/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004151**
drh5edc3122001-09-13 21:53:09 +00004152** Delete the record at which the P1 cursor is currently pointing.
4153**
4154** The cursor will be left pointing at either the next or the previous
4155** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004156** the next Next instruction will be a no-op. Hence it is OK to delete
4157** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004158**
rdcb0c374f2004-02-20 22:53:38 +00004159** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004160** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004161**
drh91fd4d42008-01-19 20:11:25 +00004162** P1 must not be pseudo-table. It has to be a real table with
4163** multiple rows.
4164**
4165** If P4 is not NULL, then it is the name of the table that P1 is
4166** pointing to. The update hook will be invoked, if it exists.
4167** If P4 is not NULL then the P1 cursor must have been positioned
4168** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004169*/
drh9cbf3422008-01-17 16:22:13 +00004170case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004171 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004172 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004173
drh856c1032009-06-02 15:21:42 +00004174 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00004175 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4176 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004177 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004178 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00004179
drh91fd4d42008-01-19 20:11:25 +00004180 /* If the update-hook will be invoked, set iKey to the rowid of the
4181 ** row being deleted.
4182 */
4183 if( db->xUpdateCallback && pOp->p4.z ){
4184 assert( pC->isTable );
4185 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
4186 iKey = pC->lastRowid;
4187 }
danielk197794eb6a12005-12-15 15:22:08 +00004188
drh9a65f2c2009-06-22 19:05:40 +00004189 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4190 ** OP_Column on the same table without any intervening operations that
4191 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4192 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4193 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4194 ** to guard against future changes to the code generator.
4195 **/
4196 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004197 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004198 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4199
drh7f751222009-03-17 22:33:00 +00004200 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00004201 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004202 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004203
drh91fd4d42008-01-19 20:11:25 +00004204 /* Invoke the update-hook if required. */
4205 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
4206 const char *zDb = db->aDb[pC->iDb].zName;
4207 const char *zTbl = pOp->p4.z;
4208 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4209 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004210 }
danielk1977b28af712004-06-21 06:50:26 +00004211 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004212 break;
4213}
drhb7f1d9a2009-09-08 02:27:58 +00004214/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004215**
drhb7f1d9a2009-09-08 02:27:58 +00004216** The value of the change counter is copied to the database handle
4217** change counter (returned by subsequent calls to sqlite3_changes()).
4218** Then the VMs internal change counter resets to 0.
4219** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004220*/
drh9cbf3422008-01-17 16:22:13 +00004221case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004222 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004223 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004224 break;
4225}
4226
drh1153c7b2013-11-01 22:02:56 +00004227/* Opcode: SorterCompare P1 P2 P3 P4
4228** Synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2
dan5134d132011-09-02 10:31:11 +00004229**
drh1153c7b2013-11-01 22:02:56 +00004230** P1 is a sorter cursor. This instruction compares a prefix of the
4231** the record blob in register P3 against a prefix of the entry that
4232** the sorter cursor currently points to. The final P4 fields of both
4233** the P3 and sorter record are ignored.
4234**
4235** If either P3 or the sorter contains a NULL in one of their significant
4236** fields (not counting the P4 fields at the end which are ignored) then
4237** the comparison is assumed to be equal.
4238**
4239** Fall through to next instruction if the two records compare equal to
4240** each other. Jump to P2 if they are different.
dan5134d132011-09-02 10:31:11 +00004241*/
4242case OP_SorterCompare: {
4243 VdbeCursor *pC;
4244 int res;
drh1153c7b2013-11-01 22:02:56 +00004245 int nIgnore;
dan5134d132011-09-02 10:31:11 +00004246
4247 pC = p->apCsr[pOp->p1];
4248 assert( isSorter(pC) );
drh1153c7b2013-11-01 22:02:56 +00004249 assert( pOp->p4type==P4_INT32 );
dan5134d132011-09-02 10:31:11 +00004250 pIn3 = &aMem[pOp->p3];
drh1153c7b2013-11-01 22:02:56 +00004251 nIgnore = pOp->p4.i;
4252 rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
dan5134d132011-09-02 10:31:11 +00004253 if( res ){
4254 pc = pOp->p2-1;
4255 }
4256 break;
4257};
4258
4259/* Opcode: SorterData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004260** Synopsis: r[P2]=data
dan5134d132011-09-02 10:31:11 +00004261**
4262** Write into register P2 the current sorter data for sorter cursor P1.
4263*/
4264case OP_SorterData: {
4265 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004266
dan5134d132011-09-02 10:31:11 +00004267 pOut = &aMem[pOp->p2];
4268 pC = p->apCsr[pOp->p1];
4269 assert( pC->isSorter );
4270 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4271 break;
4272}
4273
drh98757152008-01-09 23:04:12 +00004274/* Opcode: RowData P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004275** Synopsis: r[P2]=data
drh70ce3f02003-04-15 19:22:22 +00004276**
drh98757152008-01-09 23:04:12 +00004277** Write into register P2 the complete row data for cursor P1.
4278** There is no interpretation of the data.
4279** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004280** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004281**
drhde4fcfd2008-01-19 23:50:26 +00004282** If the P1 cursor must be pointing to a valid row (not a NULL row)
4283** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004284*/
drh98757152008-01-09 23:04:12 +00004285/* Opcode: RowKey P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004286** Synopsis: r[P2]=key
drh143f3c42004-01-07 20:37:52 +00004287**
drh98757152008-01-09 23:04:12 +00004288** Write into register P2 the complete row key for cursor P1.
4289** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004290** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004291** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004292**
drhde4fcfd2008-01-19 23:50:26 +00004293** If the P1 cursor must be pointing to a valid row (not a NULL row)
4294** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004295*/
danielk1977a7a8e142008-02-13 18:25:27 +00004296case OP_RowKey:
4297case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004298 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004299 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004300 u32 n;
drh856c1032009-06-02 15:21:42 +00004301 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004302
drha6c2ed92009-11-14 23:22:23 +00004303 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004304 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004305
drhf0863fe2005-06-12 21:35:51 +00004306 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004307 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4308 pC = p->apCsr[pOp->p1];
dan5134d132011-09-02 10:31:11 +00004309 assert( pC->isSorter==0 );
drhc6aff302011-09-01 15:32:47 +00004310 assert( pC->isTable || pOp->opcode!=OP_RowData );
drhf0863fe2005-06-12 21:35:51 +00004311 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004312 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004313 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004314 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004315 assert( pC->pCursor!=0 );
4316 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004317 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004318
4319 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4320 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4321 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4322 ** a no-op and can never fail. But we leave it in place as a safety.
4323 */
4324 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004325 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004326 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4327
drhde4fcfd2008-01-19 23:50:26 +00004328 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00004329 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004330 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004331 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004332 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004333 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004334 }
drhbfb19dc2009-06-05 16:46:53 +00004335 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004336 }else{
drhb07028f2011-10-14 21:49:18 +00004337 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004338 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004339 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004340 goto too_big;
4341 }
drhde4fcfd2008-01-19 23:50:26 +00004342 }
danielk1977a7a8e142008-02-13 18:25:27 +00004343 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4344 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004345 }
danielk1977a7a8e142008-02-13 18:25:27 +00004346 pOut->n = n;
4347 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00004348 if( pC->isIndex ){
4349 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4350 }else{
4351 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004352 }
danielk197796cb76f2008-01-04 13:24:28 +00004353 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004354 UPDATE_MAX_BLOBSIZE(pOut);
drhee0ec8e2013-10-31 17:38:01 +00004355 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00004356 break;
4357}
4358
drh2133d822008-01-03 18:44:59 +00004359/* Opcode: Rowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004360** Synopsis: r[P2]=rowid
drh5e00f6c2001-09-13 13:46:56 +00004361**
drh2133d822008-01-03 18:44:59 +00004362** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004363** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004364**
4365** P1 can be either an ordinary table or a virtual table. There used to
4366** be a separate OP_VRowid opcode for use with virtual tables, but this
4367** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004368*/
drh4c583122008-01-04 22:01:03 +00004369case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004370 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004371 i64 v;
drh856c1032009-06-02 15:21:42 +00004372 sqlite3_vtab *pVtab;
4373 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004374
drh653b82a2009-06-22 11:10:47 +00004375 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4376 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004377 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004378 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004379 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004380 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004381 break;
4382 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004383 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004384#ifndef SQLITE_OMIT_VIRTUALTABLE
4385 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004386 pVtab = pC->pVtabCursor->pVtab;
4387 pModule = pVtab->pModule;
4388 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004389 rc = pModule->xRowid(pC->pVtabCursor, &v);
dan016f7812013-08-21 17:35:48 +00004390 sqlite3VtabImportErrmsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004391#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004392 }else{
drh6be240e2009-07-14 02:33:02 +00004393 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004394 rc = sqlite3VdbeCursorMoveto(pC);
4395 if( rc ) goto abort_due_to_error;
4396 if( pC->rowidIsValid ){
4397 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004398 }else{
drhc27ae612009-07-14 18:35:44 +00004399 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4400 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004401 }
drh5e00f6c2001-09-13 13:46:56 +00004402 }
drh4c583122008-01-04 22:01:03 +00004403 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004404 break;
4405}
4406
drh9cbf3422008-01-17 16:22:13 +00004407/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004408**
4409** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004410** that occur while the cursor is on the null row will always
4411** write a NULL.
drh17f71932002-02-21 12:01:27 +00004412*/
drh9cbf3422008-01-17 16:22:13 +00004413case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004414 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004415
drh653b82a2009-06-22 11:10:47 +00004416 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4417 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004418 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004419 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004420 pC->rowidIsValid = 0;
dana205a482011-08-27 18:48:57 +00004421 assert( pC->pCursor || pC->pVtabCursor );
danielk1977be51a652008-10-08 17:58:48 +00004422 if( pC->pCursor ){
4423 sqlite3BtreeClearCursor(pC->pCursor);
4424 }
drh17f71932002-02-21 12:01:27 +00004425 break;
4426}
4427
drh9cbf3422008-01-17 16:22:13 +00004428/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004429**
drhf0863fe2005-06-12 21:35:51 +00004430** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004431** will refer to the last entry in the database table or index.
4432** If the table or index is empty and P2>0, then jump immediately to P2.
4433** If P2 is 0 or if the table or index is not empty, fall through
4434** to the following instruction.
4435*/
drh9cbf3422008-01-17 16:22:13 +00004436case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004437 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004438 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004439 int res;
drh9562b552002-02-19 15:00:07 +00004440
drh653b82a2009-06-22 11:10:47 +00004441 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4442 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004443 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004444 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004445 res = 0;
4446 if( ALWAYS(pCrsr!=0) ){
drh9a65f2c2009-06-22 19:05:40 +00004447 rc = sqlite3BtreeLast(pCrsr, &res);
4448 }
drh9c1905f2008-12-10 22:32:56 +00004449 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004450 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004451 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004452 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004453 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004454 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004455 }
4456 break;
4457}
4458
drh0342b1f2005-09-01 03:07:44 +00004459
drh9cbf3422008-01-17 16:22:13 +00004460/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004461**
4462** This opcode does exactly the same thing as OP_Rewind except that
4463** it increments an undocumented global variable used for testing.
4464**
4465** Sorting is accomplished by writing records into a sorting index,
4466** then rewinding that index and playing it back from beginning to
4467** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4468** rewinding so that the global variable will be incremented and
4469** regression tests can determine whether or not the optimizer is
4470** correctly optimizing out sorts.
4471*/
drhc6aff302011-09-01 15:32:47 +00004472case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004473case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004474#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004475 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004476 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004477#endif
drh9b47ee32013-08-20 03:13:51 +00004478 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004479 /* Fall through into OP_Rewind */
4480}
drh9cbf3422008-01-17 16:22:13 +00004481/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004482**
drhf0863fe2005-06-12 21:35:51 +00004483** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004484** will refer to the first entry in the database table or index.
4485** If the table or index is empty and P2>0, then jump immediately to P2.
4486** If P2 is 0 or if the table or index is not empty, fall through
4487** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004488*/
drh9cbf3422008-01-17 16:22:13 +00004489case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004490 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004491 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004492 int res;
drh5e00f6c2001-09-13 13:46:56 +00004493
drh653b82a2009-06-22 11:10:47 +00004494 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4495 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004496 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004497 assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004498 res = 1;
dan689ab892011-08-12 15:02:00 +00004499 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004500 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004501 }else{
4502 pCrsr = pC->pCursor;
4503 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004504 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004505 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004506 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004507 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004508 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004509 }
drh9c1905f2008-12-10 22:32:56 +00004510 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004511 assert( pOp->p2>0 && pOp->p2<p->nOp );
4512 if( res ){
drhf4dada72004-05-11 09:57:35 +00004513 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004514 }
4515 break;
4516}
4517
drh81316f82013-10-29 20:40:47 +00004518/* Opcode: Next P1 P2 * * P5
drh5e00f6c2001-09-13 13:46:56 +00004519**
4520** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004521** table or index. If there are no more key/value pairs then fall through
4522** to the following instruction. But if the cursor advance was successful,
4523** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004524**
drh60a713c2008-01-21 16:22:45 +00004525** The P1 cursor must be for a real table, not a pseudo-table.
4526**
dana205a482011-08-27 18:48:57 +00004527** P4 is always of type P4_ADVANCE. The function pointer points to
4528** sqlite3BtreeNext().
4529**
drhafc266a2010-03-31 17:47:44 +00004530** If P5 is positive and the jump is taken, then event counter
4531** number P5-1 in the prepared statement is incremented.
4532**
drhc045ec52002-12-04 20:01:06 +00004533** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004534*/
drhafc266a2010-03-31 17:47:44 +00004535/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004536**
4537** Back up cursor P1 so that it points to the previous key/data pair in its
4538** table or index. If there is no previous key/value pairs then fall through
4539** to the following instruction. But if the cursor backup was successful,
4540** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004541**
4542** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004543**
dana205a482011-08-27 18:48:57 +00004544** P4 is always of type P4_ADVANCE. The function pointer points to
4545** sqlite3BtreePrevious().
4546**
drhafc266a2010-03-31 17:47:44 +00004547** If P5 is positive and the jump is taken, then event counter
4548** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004549*/
drhc6aff302011-09-01 15:32:47 +00004550case OP_SorterNext: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004551case OP_Prev: /* jump */
4552case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004553 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004554 int res;
drh8721ce42001-11-07 14:22:00 +00004555
drh70ce3f02003-04-15 19:22:22 +00004556 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004557 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004558 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004559 if( pC==0 ){
4560 break; /* See ticket #2273 */
4561 }
drhc6aff302011-09-01 15:32:47 +00004562 assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
dan689ab892011-08-12 15:02:00 +00004563 if( isSorter(pC) ){
dan5134d132011-09-02 10:31:11 +00004564 assert( pOp->opcode==OP_SorterNext );
dana20fde62011-07-12 14:28:05 +00004565 rc = sqlite3VdbeSorterNext(db, pC, &res);
4566 }else{
drh9b47ee32013-08-20 03:13:51 +00004567 /* res = 1; // Always initialized by the xAdvance() call */
dana20fde62011-07-12 14:28:05 +00004568 assert( pC->deferredMoveto==0 );
dana205a482011-08-27 18:48:57 +00004569 assert( pC->pCursor );
4570 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4571 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4572 rc = pOp->p4.xAdvance(pC->pCursor, &res);
drh9a65f2c2009-06-22 19:05:40 +00004573 }
drh9c1905f2008-12-10 22:32:56 +00004574 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004575 pC->cacheStatus = CACHE_STALE;
4576 if( res==0 ){
4577 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004578 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004579#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004580 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004581#endif
drh8721ce42001-11-07 14:22:00 +00004582 }
drhf0863fe2005-06-12 21:35:51 +00004583 pC->rowidIsValid = 0;
drh49afe3a2013-07-10 03:05:14 +00004584 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004585}
4586
danielk1977de630352009-05-04 11:42:29 +00004587/* Opcode: IdxInsert P1 P2 P3 * P5
drh81316f82013-10-29 20:40:47 +00004588** Synopsis: key=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00004589**
drhef8662b2011-06-20 21:47:58 +00004590** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004591** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004592** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004593**
drhaa9b8962008-01-08 02:57:55 +00004594** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004595** insert is likely to be an append.
4596**
drhf0863fe2005-06-12 21:35:51 +00004597** This instruction only works for indices. The equivalent instruction
4598** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004599*/
drhca892a72011-09-03 00:17:51 +00004600case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004601case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004602 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004603 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004604 int nKey;
4605 const char *zKey;
4606
drh653b82a2009-06-22 11:10:47 +00004607 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4608 pC = p->apCsr[pOp->p1];
4609 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004610 assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004611 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004612 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004613 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004614 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh9a65f2c2009-06-22 19:05:40 +00004615 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004616 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004617 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004618 if( rc==SQLITE_OK ){
dan5134d132011-09-02 10:31:11 +00004619 if( isSorter(pC) ){
4620 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4621 }else{
4622 nKey = pIn2->n;
4623 zKey = pIn2->z;
dan1e74e602011-08-06 12:01:58 +00004624 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4625 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
dan5134d132011-09-02 10:31:11 +00004626 );
dan1e74e602011-08-06 12:01:58 +00004627 assert( pC->deferredMoveto==0 );
dan5134d132011-09-02 10:31:11 +00004628 pC->cacheStatus = CACHE_STALE;
dan1e74e602011-08-06 12:01:58 +00004629 }
danielk1977d908f5a2007-05-11 07:08:28 +00004630 }
drh5e00f6c2001-09-13 13:46:56 +00004631 }
drh5e00f6c2001-09-13 13:46:56 +00004632 break;
4633}
4634
drh6546af12013-11-04 15:23:25 +00004635/* Opcode: IdxDelete P1 P2 P3 * P5
drhf63552b2013-10-30 00:25:03 +00004636** Synopsis: key=r[P2@P3]
drh5e00f6c2001-09-13 13:46:56 +00004637**
drhe14006d2008-03-25 17:23:32 +00004638** The content of P3 registers starting at register P2 form
4639** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004640** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004641*/
drhe14006d2008-03-25 17:23:32 +00004642case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004643 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004644 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004645 int res;
4646 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004647
drhe14006d2008-03-25 17:23:32 +00004648 assert( pOp->p3>0 );
dan3bc9f742013-08-15 16:18:39 +00004649 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
drh653b82a2009-06-22 11:10:47 +00004650 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4651 pC = p->apCsr[pOp->p1];
4652 assert( pC!=0 );
4653 pCrsr = pC->pCursor;
drh6546af12013-11-04 15:23:25 +00004654 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh9a65f2c2009-06-22 19:05:40 +00004655 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004656 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004657 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004658 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004659 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004660#ifdef SQLITE_DEBUG
4661 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4662#endif
drhe63d9992008-08-13 19:11:48 +00004663 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004664 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004665 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004666 }
drh9188b382004-05-14 21:12:22 +00004667 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004668 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004669 }
drh5e00f6c2001-09-13 13:46:56 +00004670 break;
4671}
4672
drh2133d822008-01-03 18:44:59 +00004673/* Opcode: IdxRowid P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004674** Synopsis: r[P2]=rowid
drh8721ce42001-11-07 14:22:00 +00004675**
drh2133d822008-01-03 18:44:59 +00004676** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004677** the end of the index key pointed to by cursor P1. This integer should be
4678** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004679**
drh9437bd22009-02-01 00:29:56 +00004680** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004681*/
drh4c583122008-01-04 22:01:03 +00004682case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004683 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004684 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004685 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004686
drh653b82a2009-06-22 11:10:47 +00004687 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4688 pC = p->apCsr[pOp->p1];
4689 assert( pC!=0 );
4690 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004691 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004692 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004693 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004694 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004695 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004696 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004697 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004698 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004699 if( rc!=SQLITE_OK ){
4700 goto abort_due_to_error;
4701 }
drh4c583122008-01-04 22:01:03 +00004702 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004703 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004704 }
drh8721ce42001-11-07 14:22:00 +00004705 }
4706 break;
4707}
4708
danielk197761dd5832008-04-18 11:31:12 +00004709/* Opcode: IdxGE P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004710** Synopsis: key=r[P3@P4]
drh8721ce42001-11-07 14:22:00 +00004711**
danielk197761dd5832008-04-18 11:31:12 +00004712** The P4 register values beginning with P3 form an unpacked index
4713** key that omits the ROWID. Compare this key value against the index
4714** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004715**
danielk197761dd5832008-04-18 11:31:12 +00004716** If the P1 index entry is greater than or equal to the key value
4717** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004718**
danielk197761dd5832008-04-18 11:31:12 +00004719** If P5 is non-zero then the key value is increased by an epsilon
4720** prior to the comparison. This make the opcode work like IdxGT except
4721** that if the key from register P3 is a prefix of the key in the cursor,
4722** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004723*/
drh3bb9b932010-08-06 02:10:00 +00004724/* Opcode: IdxLT P1 P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00004725** Synopsis: key=r[P3@P4]
drhc045ec52002-12-04 20:01:06 +00004726**
danielk197761dd5832008-04-18 11:31:12 +00004727** The P4 register values beginning with P3 form an unpacked index
4728** key that omits the ROWID. Compare this key value against the index
4729** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004730**
danielk197761dd5832008-04-18 11:31:12 +00004731** If the P1 index entry is less than the key value then jump to P2.
4732** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004733**
danielk197761dd5832008-04-18 11:31:12 +00004734** If P5 is non-zero then the key value is increased by an epsilon prior
4735** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004736*/
drh93952eb2009-11-13 19:43:43 +00004737case OP_IdxLT: /* jump */
4738case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004739 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004740 int res;
4741 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004742
drh653b82a2009-06-22 11:10:47 +00004743 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4744 pC = p->apCsr[pOp->p1];
4745 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004746 assert( pC->isOrdered );
drh9a65f2c2009-06-22 19:05:40 +00004747 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004748 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004749 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004750 assert( pOp->p4type==P4_INT32 );
4751 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004752 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004753 if( pOp->p5 ){
dan0c733f62011-11-16 15:27:09 +00004754 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004755 }else{
dan0c733f62011-11-16 15:27:09 +00004756 r.flags = UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004757 }
drha6c2ed92009-11-14 23:22:23 +00004758 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004759#ifdef SQLITE_DEBUG
4760 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4761#endif
drhe63d9992008-08-13 19:11:48 +00004762 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004763 if( pOp->opcode==OP_IdxLT ){
4764 res = -res;
drha05a7222008-01-19 03:35:58 +00004765 }else{
4766 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004767 res++;
4768 }
4769 if( res>0 ){
4770 pc = pOp->p2 - 1 ;
4771 }
4772 }
4773 break;
4774}
4775
drh98757152008-01-09 23:04:12 +00004776/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004777**
4778** Delete an entire database table or index whose root page in the database
4779** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004780**
drh98757152008-01-09 23:04:12 +00004781** The table being destroyed is in the main database file if P3==0. If
4782** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004783** that is used to store tables create using CREATE TEMPORARY TABLE.
4784**
drh205f48e2004-11-05 00:43:11 +00004785** If AUTOVACUUM is enabled then it is possible that another root page
4786** might be moved into the newly deleted root page in order to keep all
4787** root pages contiguous at the beginning of the database. The former
4788** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004789** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004790** movement was required (because the table being dropped was already
4791** the last one in the database) then a zero is stored in register P2.
4792** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004793**
drhb19a2bc2001-09-16 00:13:26 +00004794** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004795*/
drh98757152008-01-09 23:04:12 +00004796case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004797 int iMoved;
drh3765df42006-06-28 18:18:09 +00004798 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004799 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004800 int iDb;
drh3a949872012-09-18 13:20:13 +00004801
drh9e92a472013-06-27 17:40:30 +00004802 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004803#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004804 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004805 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004806 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4807 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4808 ){
danielk1977212b2182006-06-23 14:32:08 +00004809 iCnt++;
4810 }
4811 }
drh3765df42006-06-28 18:18:09 +00004812#else
danc0537fe2013-06-28 19:41:43 +00004813 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004814#endif
drh3c657212009-11-17 23:59:58 +00004815 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004816 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004817 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004818 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004819 }else{
drh856c1032009-06-02 15:21:42 +00004820 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004821 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004822 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh98757152008-01-09 23:04:12 +00004823 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004824 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004825 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004826#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004827 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004828 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4829 /* All OP_Destroy operations occur on the same btree */
4830 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4831 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004832 }
drh3765df42006-06-28 18:18:09 +00004833#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004834 }
drh5e00f6c2001-09-13 13:46:56 +00004835 break;
4836}
4837
danielk1977c7af4842008-10-27 13:59:33 +00004838/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004839**
4840** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004841** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004842** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004843**
drhf57b3392001-10-08 13:22:32 +00004844** The table being clear is in the main database file if P2==0. If
4845** P2==1 then the table to be clear is in the auxiliary database file
4846** that is used to store tables create using CREATE TEMPORARY TABLE.
4847**
shanebe217792009-03-05 04:20:31 +00004848** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004849** intkey table (an SQL table, not an index). In this case the row change
4850** count is incremented by the number of rows in the table being cleared.
4851** If P3 is greater than zero, then the value stored in register P3 is
4852** also incremented by the number of rows in the table being cleared.
4853**
drhb19a2bc2001-09-16 00:13:26 +00004854** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004855*/
drh9cbf3422008-01-17 16:22:13 +00004856case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004857 int nChange;
4858
4859 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004860 assert( p->readOnly==0 );
danf52bb8d2013-08-03 20:24:58 +00004861 assert( pOp->p1!=1 );
drhdddd7792011-04-03 18:19:25 +00004862 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004863 rc = sqlite3BtreeClearTable(
4864 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4865 );
4866 if( pOp->p3 ){
4867 p->nChange += nChange;
4868 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004869 assert( memIsValid(&aMem[pOp->p3]) );
4870 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004871 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004872 }
4873 }
drh5edc3122001-09-13 21:53:09 +00004874 break;
4875}
4876
drh4c583122008-01-04 22:01:03 +00004877/* Opcode: CreateTable P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004878** Synopsis: r[P2]=root iDb=P1
drh5b2fd562001-09-13 15:21:31 +00004879**
drh4c583122008-01-04 22:01:03 +00004880** Allocate a new table in the main database file if P1==0 or in the
4881** auxiliary database file if P1==1 or in an attached database if
4882** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004883** register P2
drh5b2fd562001-09-13 15:21:31 +00004884**
drhc6b52df2002-01-04 03:09:29 +00004885** The difference between a table and an index is this: A table must
4886** have a 4-byte integer key and can have arbitrary data. An index
4887** has an arbitrary key but no data.
4888**
drhb19a2bc2001-09-16 00:13:26 +00004889** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004890*/
drh4c583122008-01-04 22:01:03 +00004891/* Opcode: CreateIndex P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00004892** Synopsis: r[P2]=root iDb=P1
drhf57b3392001-10-08 13:22:32 +00004893**
drh4c583122008-01-04 22:01:03 +00004894** Allocate a new index in the main database file if P1==0 or in the
4895** auxiliary database file if P1==1 or in an attached database if
4896** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004897** register P2.
drhf57b3392001-10-08 13:22:32 +00004898**
drhc6b52df2002-01-04 03:09:29 +00004899** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004900*/
drh4c583122008-01-04 22:01:03 +00004901case OP_CreateIndex: /* out2-prerelease */
4902case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004903 int pgno;
drhf328bc82004-05-10 23:29:49 +00004904 int flags;
drh234c39d2004-07-24 03:30:47 +00004905 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004906
4907 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004908 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004909 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00004910 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00004911 pDb = &db->aDb[pOp->p1];
4912 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004913 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004914 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004915 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004916 }else{
drhd4187c72010-08-30 22:15:45 +00004917 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004918 }
drh234c39d2004-07-24 03:30:47 +00004919 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004920 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004921 break;
4922}
4923
drh22645842011-03-24 01:34:03 +00004924/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004925**
4926** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004927** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004928**
4929** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004930** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004931*/
drh9cbf3422008-01-17 16:22:13 +00004932case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004933 int iDb;
4934 const char *zMaster;
4935 char *zSql;
4936 InitData initData;
4937
drhbdaec522011-04-04 00:14:43 +00004938 /* Any prepared statement that invokes this opcode will hold mutexes
4939 ** on every btree. This is a prerequisite for invoking
4940 ** sqlite3InitCallback().
4941 */
4942#ifdef SQLITE_DEBUG
4943 for(iDb=0; iDb<db->nDb; iDb++){
4944 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4945 }
4946#endif
drhbdaec522011-04-04 00:14:43 +00004947
drh856c1032009-06-02 15:21:42 +00004948 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004949 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004950 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004951 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004952 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004953 initData.db = db;
4954 initData.iDb = pOp->p1;
4955 initData.pzErrMsg = &p->zErrMsg;
4956 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004957 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004958 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4959 if( zSql==0 ){
4960 rc = SQLITE_NOMEM;
4961 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004962 assert( db->init.busy==0 );
4963 db->init.busy = 1;
4964 initData.rc = SQLITE_OK;
4965 assert( !db->mallocFailed );
4966 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4967 if( rc==SQLITE_OK ) rc = initData.rc;
4968 sqlite3DbFree(db, zSql);
4969 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004970 }
drh3c23a882007-01-09 14:01:13 +00004971 }
drh81028a42012-05-15 18:28:27 +00004972 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00004973 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004974 goto no_mem;
4975 }
drh234c39d2004-07-24 03:30:47 +00004976 break;
4977}
4978
drh8bfdf722009-06-19 14:06:03 +00004979#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004980/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004981**
4982** Read the sqlite_stat1 table for database P1 and load the content
4983** of that table into the internal index hash table. This will cause
4984** the analysis to be used when preparing all subsequent queries.
4985*/
drh9cbf3422008-01-17 16:22:13 +00004986case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004987 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4988 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004989 break;
4990}
drh8bfdf722009-06-19 14:06:03 +00004991#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004992
drh98757152008-01-09 23:04:12 +00004993/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004994**
4995** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004996** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004997** is dropped in order to keep the internal representation of the
4998** schema consistent with what is on disk.
4999*/
drh9cbf3422008-01-17 16:22:13 +00005000case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00005001 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005002 break;
5003}
5004
drh98757152008-01-09 23:04:12 +00005005/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005006**
5007** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005008** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00005009** is dropped in order to keep the internal representation of the
5010** schema consistent with what is on disk.
5011*/
drh9cbf3422008-01-17 16:22:13 +00005012case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00005013 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005014 break;
5015}
5016
drh98757152008-01-09 23:04:12 +00005017/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00005018**
5019** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00005020** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00005021** is dropped in order to keep the internal representation of the
5022** schema consistent with what is on disk.
5023*/
drh9cbf3422008-01-17 16:22:13 +00005024case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00005025 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00005026 break;
5027}
5028
drh234c39d2004-07-24 03:30:47 +00005029
drhb7f91642004-10-31 02:22:47 +00005030#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00005031/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00005032**
drh98757152008-01-09 23:04:12 +00005033** Do an analysis of the currently open database. Store in
5034** register P1 the text of an error message describing any problems.
5035** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00005036**
drh98757152008-01-09 23:04:12 +00005037** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00005038** At most reg(P3) errors will be reported.
5039** In other words, the analysis stops as soon as reg(P1) errors are
5040** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00005041**
drh79069752004-05-22 21:30:40 +00005042** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00005043** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00005044** total.
drh21504322002-06-25 13:16:02 +00005045**
drh98757152008-01-09 23:04:12 +00005046** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005047** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005048**
drh1dcdbc02007-01-27 02:24:54 +00005049** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005050*/
drhaaab5722002-02-19 13:39:21 +00005051case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005052 int nRoot; /* Number of tables to check. (Number of root pages.) */
5053 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5054 int j; /* Loop counter */
5055 int nErr; /* Number of errors reported */
5056 char *z; /* Text of the error report */
5057 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005058
drh1713afb2013-06-28 01:24:57 +00005059 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005060 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005061 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005062 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005063 if( aRoot==0 ) goto no_mem;
dan3bc9f742013-08-15 16:18:39 +00005064 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005065 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005066 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005067 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005068 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005069 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005070 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005071 }
5072 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005073 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005074 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00005075 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005076 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005077 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005078 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005079 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005080 if( nErr==0 ){
5081 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005082 }else if( z==0 ){
5083 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005084 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005085 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005086 }
drhb7654112008-01-12 12:48:07 +00005087 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005088 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005089 break;
5090}
drhb7f91642004-10-31 02:22:47 +00005091#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005092
drh3d4501e2008-12-04 20:40:10 +00005093/* Opcode: RowSetAdd P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005094** Synopsis: rowset(P1)=r[P2]
drh5e00f6c2001-09-13 13:46:56 +00005095**
drh3d4501e2008-12-04 20:40:10 +00005096** Insert the integer value held by register P2 into a boolean index
5097** held in register P1.
5098**
5099** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005100*/
drh93952eb2009-11-13 19:43:43 +00005101case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005102 pIn1 = &aMem[pOp->p1];
5103 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005104 assert( (pIn2->flags & MEM_Int)!=0 );
5105 if( (pIn1->flags & MEM_RowSet)==0 ){
5106 sqlite3VdbeMemSetRowSet(pIn1);
5107 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005108 }
drh93952eb2009-11-13 19:43:43 +00005109 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005110 break;
5111}
5112
5113/* Opcode: RowSetRead P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005114** Synopsis: r[P3]=rowset(P1)
drh3d4501e2008-12-04 20:40:10 +00005115**
5116** Extract the smallest value from boolean index P1 and put that value into
5117** register P3. Or, if boolean index P1 is initially empty, leave P3
5118** unchanged and jump to instruction P2.
5119*/
drh93952eb2009-11-13 19:43:43 +00005120case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005121 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005122
drh3c657212009-11-17 23:59:58 +00005123 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005124 if( (pIn1->flags & MEM_RowSet)==0
5125 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005126 ){
5127 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005128 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005129 pc = pOp->p2 - 1;
5130 }else{
5131 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005132 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005133 }
drh49afe3a2013-07-10 03:05:14 +00005134 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005135}
5136
drh1b26c7c2009-04-22 02:15:47 +00005137/* Opcode: RowSetTest P1 P2 P3 P4
drh81316f82013-10-29 20:40:47 +00005138** Synopsis: if r[P3] in rowset(P1) goto P2
danielk19771d461462009-04-21 09:02:45 +00005139**
drhade97602009-04-21 15:05:18 +00005140** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005141** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005142** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005143** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005144** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005145**
drh1b26c7c2009-04-22 02:15:47 +00005146** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005147** of integers, where each set contains no duplicates. Each set
5148** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005149** must have P4==0, the final set P4=-1. P4 must be either -1 or
5150** non-negative. For non-negative values of P4 only the lower 4
5151** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005152**
5153** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005154** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005155** (b) when P4==-1 there is no need to insert the value, as it will
5156** never be tested for, and (c) when a value that is part of set X is
5157** inserted, there is no need to search to see if the same value was
5158** previously inserted as part of set X (only if it was previously
5159** inserted as part of some other set).
5160*/
drh1b26c7c2009-04-22 02:15:47 +00005161case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005162 int iSet;
5163 int exists;
5164
drh3c657212009-11-17 23:59:58 +00005165 pIn1 = &aMem[pOp->p1];
5166 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005167 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005168 assert( pIn3->flags&MEM_Int );
5169
drh1b26c7c2009-04-22 02:15:47 +00005170 /* If there is anything other than a rowset object in memory cell P1,
5171 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005172 */
drh733bf1b2009-04-22 00:47:00 +00005173 if( (pIn1->flags & MEM_RowSet)==0 ){
5174 sqlite3VdbeMemSetRowSet(pIn1);
5175 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005176 }
5177
5178 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005179 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005180 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005181 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5182 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005183 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005184 if( exists ){
5185 pc = pOp->p2 - 1;
5186 break;
5187 }
5188 }
5189 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005190 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005191 }
5192 break;
5193}
5194
drh5e00f6c2001-09-13 13:46:56 +00005195
danielk197793758c82005-01-21 08:13:14 +00005196#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005197
5198/* Opcode: Program P1 P2 P3 P4 *
5199**
dan76d462e2009-08-30 11:42:51 +00005200** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005201**
dan76d462e2009-08-30 11:42:51 +00005202** P1 contains the address of the memory cell that contains the first memory
5203** cell in an array of values used as arguments to the sub-program. P2
5204** contains the address to jump to if the sub-program throws an IGNORE
5205** exception using the RAISE() function. Register P3 contains the address
5206** of a memory cell in this (the parent) VM that is used to allocate the
5207** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005208**
5209** P4 is a pointer to the VM containing the trigger program.
5210*/
dan76d462e2009-08-30 11:42:51 +00005211case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005212 int nMem; /* Number of memory registers for sub-program */
5213 int nByte; /* Bytes of runtime space required for sub-program */
5214 Mem *pRt; /* Register to allocate runtime space */
5215 Mem *pMem; /* Used to iterate through memory cells */
5216 Mem *pEnd; /* Last memory cell in new array */
5217 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5218 SubProgram *pProgram; /* Sub-program to execute */
5219 void *t; /* Token identifying trigger */
5220
5221 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005222 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005223 assert( pProgram->nOp>0 );
5224
dan1da40a32009-09-19 17:00:31 +00005225 /* If the p5 flag is clear, then recursive invocation of triggers is
5226 ** disabled for backwards compatibility (p5 is set if this sub-program
5227 ** is really a trigger, not a foreign key action, and the flag set
5228 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005229 **
5230 ** It is recursive invocation of triggers, at the SQL level, that is
5231 ** disabled. In some cases a single trigger may generate more than one
5232 ** SubProgram (if the trigger may be executed with more than one different
5233 ** ON CONFLICT algorithm). SubProgram structures associated with a
5234 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005235 ** variable. */
5236 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005237 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005238 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5239 if( pFrame ) break;
5240 }
5241
danf5894502009-10-07 18:41:19 +00005242 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005243 rc = SQLITE_ERROR;
5244 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5245 break;
5246 }
5247
5248 /* Register pRt is used to store the memory required to save the state
5249 ** of the current program, and the memory required at runtime to execute
5250 ** the trigger program. If this trigger has been fired before, then pRt
5251 ** is already allocated. Otherwise, it must be initialized. */
5252 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005253 /* SubProgram.nMem is set to the number of memory cells used by the
5254 ** program stored in SubProgram.aOp. As well as these, one memory
5255 ** cell is required for each cursor used by the program. Set local
5256 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5257 */
dan65a7cd12009-09-01 12:16:01 +00005258 nMem = pProgram->nMem + pProgram->nCsr;
5259 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005260 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005261 + pProgram->nCsr * sizeof(VdbeCursor *)
5262 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005263 pFrame = sqlite3DbMallocZero(db, nByte);
5264 if( !pFrame ){
5265 goto no_mem;
5266 }
5267 sqlite3VdbeMemRelease(pRt);
5268 pRt->flags = MEM_Frame;
5269 pRt->u.pFrame = pFrame;
5270
5271 pFrame->v = p;
5272 pFrame->nChildMem = nMem;
5273 pFrame->nChildCsr = pProgram->nCsr;
5274 pFrame->pc = pc;
5275 pFrame->aMem = p->aMem;
5276 pFrame->nMem = p->nMem;
5277 pFrame->apCsr = p->apCsr;
5278 pFrame->nCursor = p->nCursor;
5279 pFrame->aOp = p->aOp;
5280 pFrame->nOp = p->nOp;
5281 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005282 pFrame->aOnceFlag = p->aOnceFlag;
5283 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005284
5285 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5286 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drhec86c722011-12-09 17:27:51 +00005287 pMem->flags = MEM_Invalid;
dan165921a2009-08-28 18:53:45 +00005288 pMem->db = db;
5289 }
5290 }else{
5291 pFrame = pRt->u.pFrame;
5292 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5293 assert( pProgram->nCsr==pFrame->nChildCsr );
5294 assert( pc==pFrame->pc );
5295 }
5296
5297 p->nFrame++;
5298 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005299 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005300 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005301 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005302 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005303 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005304 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005305 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005306 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005307 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005308 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005309 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5310 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005311 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005312 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005313
5314 break;
5315}
5316
dan76d462e2009-08-30 11:42:51 +00005317/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005318**
dan76d462e2009-08-30 11:42:51 +00005319** This opcode is only ever present in sub-programs called via the
5320** OP_Program instruction. Copy a value currently stored in a memory
5321** cell of the calling (parent) frame to cell P2 in the current frames
5322** address space. This is used by trigger programs to access the new.*
5323** and old.* values.
dan165921a2009-08-28 18:53:45 +00005324**
dan76d462e2009-08-30 11:42:51 +00005325** The address of the cell in the parent frame is determined by adding
5326** the value of the P1 argument to the value of the P1 argument to the
5327** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005328*/
dan76d462e2009-08-30 11:42:51 +00005329case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005330 VdbeFrame *pFrame;
5331 Mem *pIn;
5332 pFrame = p->pFrame;
5333 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005334 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5335 break;
5336}
5337
danielk197793758c82005-01-21 08:13:14 +00005338#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005339
dan1da40a32009-09-19 17:00:31 +00005340#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005341/* Opcode: FkCounter P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005342** Synopsis: fkctr[P1]+=P2
dan1da40a32009-09-19 17:00:31 +00005343**
dan0ff297e2009-09-25 17:03:14 +00005344** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5345** If P1 is non-zero, the database constraint counter is incremented
5346** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005347** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005348*/
dan32b09f22009-09-23 17:29:59 +00005349case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005350 if( db->flags & SQLITE_DeferFKs ){
5351 db->nDeferredImmCons += pOp->p2;
5352 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005353 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005354 }else{
dan0ff297e2009-09-25 17:03:14 +00005355 p->nFkConstraint += pOp->p2;
5356 }
5357 break;
5358}
5359
5360/* Opcode: FkIfZero P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005361** Synopsis: if fkctr[P1]==0 goto P2
dan0ff297e2009-09-25 17:03:14 +00005362**
5363** This opcode tests if a foreign key constraint-counter is currently zero.
5364** If so, jump to instruction P2. Otherwise, fall through to the next
5365** instruction.
5366**
5367** If P1 is non-zero, then the jump is taken if the database constraint-counter
5368** is zero (the one that counts deferred constraint violations). If P1 is
5369** zero, the jump is taken if the statement constraint-counter is zero
5370** (immediate foreign key constraint violations).
5371*/
5372case OP_FkIfZero: { /* jump */
5373 if( pOp->p1 ){
drh648e2642013-07-11 15:03:32 +00005374 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005375 }else{
drh648e2642013-07-11 15:03:32 +00005376 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005377 }
dan1da40a32009-09-19 17:00:31 +00005378 break;
5379}
5380#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5381
drh205f48e2004-11-05 00:43:11 +00005382#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005383/* Opcode: MemMax P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005384** Synopsis: r[P1]=max(r[P1],r[P2])
drh205f48e2004-11-05 00:43:11 +00005385**
dan76d462e2009-08-30 11:42:51 +00005386** P1 is a register in the root frame of this VM (the root frame is
5387** different from the current frame if this instruction is being executed
5388** within a sub-program). Set the value of register P1 to the maximum of
5389** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005390**
5391** This instruction throws an error if the memory cell is not initially
5392** an integer.
5393*/
dan76d462e2009-08-30 11:42:51 +00005394case OP_MemMax: { /* in2 */
5395 Mem *pIn1;
5396 VdbeFrame *pFrame;
5397 if( p->pFrame ){
5398 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5399 pIn1 = &pFrame->aMem[pOp->p1];
5400 }else{
drha6c2ed92009-11-14 23:22:23 +00005401 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005402 }
drhec86c722011-12-09 17:27:51 +00005403 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005404 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005405 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005406 sqlite3VdbeMemIntegerify(pIn2);
5407 if( pIn1->u.i<pIn2->u.i){
5408 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005409 }
5410 break;
5411}
5412#endif /* SQLITE_OMIT_AUTOINCREMENT */
5413
drh98757152008-01-09 23:04:12 +00005414/* Opcode: IfPos P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005415** Synopsis: if r[P1]>0 goto P2
danielk1977a2dc3b12005-02-05 12:48:48 +00005416**
drh98757152008-01-09 23:04:12 +00005417** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005418**
drh98757152008-01-09 23:04:12 +00005419** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005420** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005421*/
drh9cbf3422008-01-17 16:22:13 +00005422case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005423 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005424 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005425 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005426 pc = pOp->p2 - 1;
5427 }
5428 break;
5429}
5430
drh98757152008-01-09 23:04:12 +00005431/* Opcode: IfNeg P1 P2 * * *
drh81316f82013-10-29 20:40:47 +00005432** Synopsis: if r[P1]<0 goto P2
drh15007a92006-01-08 18:10:17 +00005433**
drh98757152008-01-09 23:04:12 +00005434** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005435**
drh98757152008-01-09 23:04:12 +00005436** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005437** not contain an integer. An assertion fault will result if you try.
5438*/
drh9cbf3422008-01-17 16:22:13 +00005439case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005440 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005441 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005442 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005443 pc = pOp->p2 - 1;
5444 }
5445 break;
5446}
5447
drh9b918ed2009-11-12 03:13:26 +00005448/* Opcode: IfZero P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005449** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
drhec7429a2005-10-06 16:53:14 +00005450**
drh9b918ed2009-11-12 03:13:26 +00005451** The register P1 must contain an integer. Add literal P3 to the
5452** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005453**
drh98757152008-01-09 23:04:12 +00005454** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005455** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005456*/
drh9cbf3422008-01-17 16:22:13 +00005457case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005458 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005459 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005460 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005461 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005462 pc = pOp->p2 - 1;
5463 }
5464 break;
5465}
5466
drh98757152008-01-09 23:04:12 +00005467/* Opcode: AggStep * P2 P3 P4 P5
drhf63552b2013-10-30 00:25:03 +00005468** Synopsis: accum=r[P3] step(r[P2@P5])
drhe5095352002-02-24 03:25:14 +00005469**
drh0bce8352002-02-28 00:41:10 +00005470** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005471** function has P5 arguments. P4 is a pointer to the FuncDef
5472** structure that specifies the function. Use register
5473** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005474**
drh98757152008-01-09 23:04:12 +00005475** The P5 arguments are taken from register P2 and its
5476** successors.
drhe5095352002-02-24 03:25:14 +00005477*/
drh9cbf3422008-01-17 16:22:13 +00005478case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005479 int n;
drhe5095352002-02-24 03:25:14 +00005480 int i;
drhc54a6172009-06-02 16:06:03 +00005481 Mem *pMem;
5482 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005483 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005484 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005485
drh856c1032009-06-02 15:21:42 +00005486 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005487 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005488 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005489 apVal = p->apArg;
5490 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005491 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005492 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005493 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005494 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005495 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005496 }
danielk19772dca4ac2008-01-03 11:50:29 +00005497 ctx.pFunc = pOp->p4.pFunc;
dan3bc9f742013-08-15 16:18:39 +00005498 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005499 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005500 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005501 ctx.s.flags = MEM_Null;
5502 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005503 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005504 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005505 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005506 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005507 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005508 ctx.skipFlag = 0;
drhd36e1042013-09-06 13:10:12 +00005509 if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005510 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005511 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005512 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005513 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005514 }
drhee9ff672010-09-03 18:50:48 +00005515 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005516 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005517 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005518 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005519 }
drh7a957892012-02-02 17:35:43 +00005520 if( ctx.skipFlag ){
5521 assert( pOp[-1].opcode==OP_CollSeq );
5522 i = pOp[-1].p1;
5523 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5524 }
drhbdaec522011-04-04 00:14:43 +00005525
drh90669c12006-01-20 15:45:36 +00005526 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005527
drh5e00f6c2001-09-13 13:46:56 +00005528 break;
5529}
5530
drh98757152008-01-09 23:04:12 +00005531/* Opcode: AggFinal P1 P2 * P4 *
drh81316f82013-10-29 20:40:47 +00005532** Synopsis: accum=r[P1] N=P2
drh5e00f6c2001-09-13 13:46:56 +00005533**
drh13449892005-09-07 21:22:45 +00005534** Execute the finalizer function for an aggregate. P1 is
5535** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005536**
5537** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005538** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005539** argument is not used by this opcode. It is only there to disambiguate
5540** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005541** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005542** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005543*/
drh9cbf3422008-01-17 16:22:13 +00005544case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005545 Mem *pMem;
dan3bc9f742013-08-15 16:18:39 +00005546 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005547 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005548 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005549 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005550 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005551 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005552 }
drh2dca8682008-03-21 17:13:13 +00005553 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005554 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005555 if( sqlite3VdbeMemTooBig(pMem) ){
5556 goto too_big;
5557 }
drh5e00f6c2001-09-13 13:46:56 +00005558 break;
5559}
5560
dan5cf53532010-05-01 16:40:20 +00005561#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005562/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005563**
5564** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005565** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005566** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5567** SQLITE_BUSY or not, respectively. Write the number of pages in the
5568** WAL after the checkpoint into mem[P3+1] and the number of pages
5569** in the WAL that have been checkpointed after the checkpoint
5570** completes into mem[P3+2]. However on an error, mem[P3+1] and
5571** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005572*/
5573case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005574 int i; /* Loop counter */
5575 int aRes[3]; /* Results */
5576 Mem *pMem; /* Write results here */
5577
drh9e92a472013-06-27 17:40:30 +00005578 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005579 aRes[0] = 0;
5580 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005581 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5582 || pOp->p2==SQLITE_CHECKPOINT_FULL
5583 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5584 );
drh30aa3b92011-02-07 23:56:01 +00005585 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005586 if( rc==SQLITE_BUSY ){
5587 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005588 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005589 }
drh30aa3b92011-02-07 23:56:01 +00005590 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5591 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5592 }
dan7c246102010-04-12 19:00:29 +00005593 break;
5594};
dan5cf53532010-05-01 16:40:20 +00005595#endif
drh5e00f6c2001-09-13 13:46:56 +00005596
drhcac29a62010-07-02 19:36:52 +00005597#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005598/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005599**
5600** Change the journal mode of database P1 to P3. P3 must be one of the
5601** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5602** modes (delete, truncate, persist, off and memory), this is a simple
5603** operation. No IO is required.
5604**
5605** If changing into or out of WAL mode the procedure is more complicated.
5606**
5607** Write a string containing the final journal-mode to register P2.
5608*/
drhd80b2332010-05-01 00:59:37 +00005609case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005610 Btree *pBt; /* Btree to change journal mode of */
5611 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005612 int eNew; /* New journal mode */
5613 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005614#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005615 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005616#endif
dane04dc882010-04-20 18:53:15 +00005617
drhd80b2332010-05-01 00:59:37 +00005618 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005619 assert( eNew==PAGER_JOURNALMODE_DELETE
5620 || eNew==PAGER_JOURNALMODE_TRUNCATE
5621 || eNew==PAGER_JOURNALMODE_PERSIST
5622 || eNew==PAGER_JOURNALMODE_OFF
5623 || eNew==PAGER_JOURNALMODE_MEMORY
5624 || eNew==PAGER_JOURNALMODE_WAL
5625 || eNew==PAGER_JOURNALMODE_QUERY
5626 );
5627 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005628 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005629
dane04dc882010-04-20 18:53:15 +00005630 pBt = db->aDb[pOp->p1].pBt;
5631 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005632 eOld = sqlite3PagerGetJournalMode(pPager);
5633 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5634 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005635
5636#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005637 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005638
drhd80b2332010-05-01 00:59:37 +00005639 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005640 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005641 */
5642 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005643 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005644 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005645 ){
drh0b9b4302010-06-11 17:01:24 +00005646 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005647 }
5648
drh0b9b4302010-06-11 17:01:24 +00005649 if( (eNew!=eOld)
5650 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5651 ){
danc0537fe2013-06-28 19:41:43 +00005652 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005653 rc = SQLITE_ERROR;
5654 sqlite3SetString(&p->zErrMsg, db,
5655 "cannot change %s wal mode from within a transaction",
5656 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5657 );
5658 break;
5659 }else{
5660
5661 if( eOld==PAGER_JOURNALMODE_WAL ){
5662 /* If leaving WAL mode, close the log file. If successful, the call
5663 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5664 ** file. An EXCLUSIVE lock may still be held on the database file
5665 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005666 */
drh0b9b4302010-06-11 17:01:24 +00005667 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005668 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005669 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005670 }
drh242c4f72010-06-22 14:49:39 +00005671 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5672 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5673 ** as an intermediate */
5674 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005675 }
5676
5677 /* Open a transaction on the database file. Regardless of the journal
5678 ** mode, this transaction always uses a rollback journal.
5679 */
5680 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5681 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005682 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005683 }
5684 }
5685 }
dan5cf53532010-05-01 16:40:20 +00005686#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005687
dand956efe2010-06-18 16:13:45 +00005688 if( rc ){
dand956efe2010-06-18 16:13:45 +00005689 eNew = eOld;
5690 }
drh0b9b4302010-06-11 17:01:24 +00005691 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005692
dane04dc882010-04-20 18:53:15 +00005693 pOut = &aMem[pOp->p2];
5694 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005695 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005696 pOut->n = sqlite3Strlen30(pOut->z);
5697 pOut->enc = SQLITE_UTF8;
5698 sqlite3VdbeChangeEncoding(pOut, encoding);
5699 break;
drhcac29a62010-07-02 19:36:52 +00005700};
5701#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005702
drhfdbcdee2007-03-27 14:44:50 +00005703#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005704/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005705**
5706** Vacuum the entire database. This opcode will cause other virtual
5707** machines to be created and run. It may not be called from within
5708** a transaction.
5709*/
drh9cbf3422008-01-17 16:22:13 +00005710case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005711 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005712 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005713 break;
5714}
drh154d4b22006-09-21 11:02:16 +00005715#endif
drh6f8c91c2003-12-07 00:24:35 +00005716
danielk1977dddbcdc2007-04-26 14:42:34 +00005717#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005718/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005719**
5720** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005721** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005722** P2. Otherwise, fall through to the next instruction.
5723*/
drh9cbf3422008-01-17 16:22:13 +00005724case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005725 Btree *pBt;
5726
5727 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005728 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00005729 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005730 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005731 rc = sqlite3BtreeIncrVacuum(pBt);
5732 if( rc==SQLITE_DONE ){
5733 pc = pOp->p2 - 1;
5734 rc = SQLITE_OK;
5735 }
5736 break;
5737}
5738#endif
5739
drh98757152008-01-09 23:04:12 +00005740/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005741**
5742** Cause precompiled statements to become expired. An expired statement
5743** fails with an error code of SQLITE_SCHEMA if it is ever executed
5744** (via sqlite3_step()).
5745**
5746** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5747** then only the currently executing statement is affected.
5748*/
drh9cbf3422008-01-17 16:22:13 +00005749case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005750 if( !pOp->p1 ){
5751 sqlite3ExpirePreparedStatements(db);
5752 }else{
5753 p->expired = 1;
5754 }
5755 break;
5756}
5757
danielk1977c00da102006-01-07 13:21:04 +00005758#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005759/* Opcode: TableLock P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005760** Synopsis: iDb=P1 root=P2 write=P3
danielk1977c00da102006-01-07 13:21:04 +00005761**
5762** Obtain a lock on a particular table. This instruction is only used when
5763** the shared-cache feature is enabled.
5764**
danielk197796d48e92009-06-29 06:00:37 +00005765** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005766** on which the lock is acquired. A readlock is obtained if P3==0 or
5767** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005768**
5769** P2 contains the root-page of the table to lock.
5770**
drh66a51672008-01-03 00:01:23 +00005771** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005772** used to generate an error message if the lock cannot be obtained.
5773*/
drh9cbf3422008-01-17 16:22:13 +00005774case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005775 u8 isWriteLock = (u8)pOp->p3;
5776 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5777 int p1 = pOp->p1;
5778 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005779 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005780 assert( isWriteLock==0 || isWriteLock==1 );
5781 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5782 if( (rc&0xFF)==SQLITE_LOCKED ){
5783 const char *z = pOp->p4.z;
5784 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5785 }
danielk1977c00da102006-01-07 13:21:04 +00005786 }
5787 break;
5788}
drhb9bb7c12006-06-11 23:41:55 +00005789#endif /* SQLITE_OMIT_SHARED_CACHE */
5790
5791#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005792/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005793**
danielk19773e3a84d2008-08-01 17:37:40 +00005794** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5795** xBegin method for that table.
5796**
5797** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005798** within a callback to a virtual table xSync() method. If it is, the error
5799** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005800*/
drh9cbf3422008-01-17 16:22:13 +00005801case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005802 VTable *pVTab;
5803 pVTab = pOp->p4.pVtab;
5804 rc = sqlite3VtabBegin(db, pVTab);
dan016f7812013-08-21 17:35:48 +00005805 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005806 break;
5807}
5808#endif /* SQLITE_OMIT_VIRTUALTABLE */
5809
5810#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005811/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005812**
drh66a51672008-01-03 00:01:23 +00005813** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005814** for that table.
5815*/
drh9cbf3422008-01-17 16:22:13 +00005816case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005817 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005818 break;
5819}
5820#endif /* SQLITE_OMIT_VIRTUALTABLE */
5821
5822#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005823/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005824**
drh66a51672008-01-03 00:01:23 +00005825** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005826** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005827*/
drh9cbf3422008-01-17 16:22:13 +00005828case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005829 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005830 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005831 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005832 break;
5833}
5834#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005835
drh9eff6162006-06-12 21:59:13 +00005836#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005837/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005838**
drh66a51672008-01-03 00:01:23 +00005839** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005840** P1 is a cursor number. This opcode opens a cursor to the virtual
5841** table and stores that cursor in P1.
5842*/
drh9cbf3422008-01-17 16:22:13 +00005843case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005844 VdbeCursor *pCur;
5845 sqlite3_vtab_cursor *pVtabCursor;
5846 sqlite3_vtab *pVtab;
5847 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005848
drh1713afb2013-06-28 01:24:57 +00005849 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005850 pCur = 0;
5851 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005852 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005853 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005854 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005855 rc = pModule->xOpen(pVtab, &pVtabCursor);
dan016f7812013-08-21 17:35:48 +00005856 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005857 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005858 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005859 pVtabCursor->pVtab = pVtab;
5860
mistachkin48864df2013-03-21 21:20:32 +00005861 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005862 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005863 if( pCur ){
5864 pCur->pVtabCursor = pVtabCursor;
5865 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005866 }else{
drh17435752007-08-16 04:30:38 +00005867 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005868 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005869 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005870 }
drh9eff6162006-06-12 21:59:13 +00005871 break;
5872}
5873#endif /* SQLITE_OMIT_VIRTUALTABLE */
5874
5875#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005876/* Opcode: VFilter P1 P2 P3 P4 *
drh81316f82013-10-29 20:40:47 +00005877** Synopsis: iPlan=r[P3] zPlan='P4'
drh9eff6162006-06-12 21:59:13 +00005878**
5879** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5880** the filtered result set is empty.
5881**
drh66a51672008-01-03 00:01:23 +00005882** P4 is either NULL or a string that was generated by the xBestIndex
5883** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005884** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005885**
drh9eff6162006-06-12 21:59:13 +00005886** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005887** by P1. The integer query plan parameter to xFilter is stored in register
5888** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005889** xFilter method. Registers P3+2..P3+1+argc are the argc
5890** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005891** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005892**
danielk19776dbee812008-01-03 18:39:41 +00005893** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005894*/
drh9cbf3422008-01-17 16:22:13 +00005895case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005896 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005897 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005898 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005899 Mem *pQuery;
5900 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005901 sqlite3_vtab_cursor *pVtabCursor;
5902 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005903 VdbeCursor *pCur;
5904 int res;
5905 int i;
5906 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005907
drha6c2ed92009-11-14 23:22:23 +00005908 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005909 pArgc = &pQuery[1];
5910 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005911 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005912 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005913 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005914 pVtabCursor = pCur->pVtabCursor;
5915 pVtab = pVtabCursor->pVtab;
5916 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005917
drh9cbf3422008-01-17 16:22:13 +00005918 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005919 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005920 nArg = (int)pArgc->u.i;
5921 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005922
drh644a5292006-12-20 14:53:38 +00005923 /* Invoke the xFilter method */
5924 {
drh856c1032009-06-02 15:21:42 +00005925 res = 0;
5926 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005927 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005928 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005929 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005930 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005931
danielk1977be718892006-06-23 08:05:19 +00005932 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005933 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005934 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00005935 sqlite3VtabImportErrmsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005936 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005937 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005938 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005939
danielk1977a298e902006-06-22 09:53:48 +00005940 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005941 pc = pOp->p2 - 1;
5942 }
5943 }
drh1d454a32008-01-31 19:34:51 +00005944 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005945
drh9eff6162006-06-12 21:59:13 +00005946 break;
5947}
5948#endif /* SQLITE_OMIT_VIRTUALTABLE */
5949
5950#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005951/* Opcode: VColumn P1 P2 P3 * *
drh81316f82013-10-29 20:40:47 +00005952** Synopsis: r[P3]=vcolumn(P2)
drh9eff6162006-06-12 21:59:13 +00005953**
drh2133d822008-01-03 18:44:59 +00005954** Store the value of the P2-th column of
5955** the row of the virtual-table that the
5956** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005957*/
5958case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005959 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005960 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005961 Mem *pDest;
5962 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005963
drhdfe88ec2008-11-03 20:55:06 +00005964 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005965 assert( pCur->pVtabCursor );
dan3bc9f742013-08-15 16:18:39 +00005966 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
drha6c2ed92009-11-14 23:22:23 +00005967 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005968 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005969 if( pCur->nullRow ){
5970 sqlite3VdbeMemSetNull(pDest);
5971 break;
5972 }
danielk19773e3a84d2008-08-01 17:37:40 +00005973 pVtab = pCur->pVtabCursor->pVtab;
5974 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005975 assert( pModule->xColumn );
5976 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005977
5978 /* The output cell may already have a buffer allocated. Move
5979 ** the current contents to sContext.s so in case the user-function
5980 ** can use the already allocated buffer instead of allocating a
5981 ** new one.
5982 */
5983 sqlite3VdbeMemMove(&sContext.s, pDest);
5984 MemSetTypeFlag(&sContext.s, MEM_Null);
5985
drhde4fcfd2008-01-19 23:50:26 +00005986 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
dan016f7812013-08-21 17:35:48 +00005987 sqlite3VtabImportErrmsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005988 if( sContext.isError ){
5989 rc = sContext.isError;
5990 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005991
drhde4fcfd2008-01-19 23:50:26 +00005992 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005993 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005994 ** dynamic allocation in sContext.s (a Mem struct) is released.
5995 */
5996 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005997 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005998 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005999 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006000
drhde4fcfd2008-01-19 23:50:26 +00006001 if( sqlite3VdbeMemTooBig(pDest) ){
6002 goto too_big;
6003 }
drh9eff6162006-06-12 21:59:13 +00006004 break;
6005}
6006#endif /* SQLITE_OMIT_VIRTUALTABLE */
6007
6008#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006009/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00006010**
6011** Advance virtual table P1 to the next row in its result set and
6012** jump to instruction P2. Or, if the virtual table has reached
6013** the end of its result set, then fall through to the next instruction.
6014*/
drh9cbf3422008-01-17 16:22:13 +00006015case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00006016 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006017 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00006018 int res;
drh856c1032009-06-02 15:21:42 +00006019 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00006020
drhc54a6172009-06-02 16:06:03 +00006021 res = 0;
drh856c1032009-06-02 15:21:42 +00006022 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00006023 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00006024 if( pCur->nullRow ){
6025 break;
6026 }
danielk19773e3a84d2008-08-01 17:37:40 +00006027 pVtab = pCur->pVtabCursor->pVtab;
6028 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00006029 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00006030
drhde4fcfd2008-01-19 23:50:26 +00006031 /* Invoke the xNext() method of the module. There is no way for the
6032 ** underlying implementation to return an error if one occurs during
6033 ** xNext(). Instead, if an error occurs, true is returned (indicating that
6034 ** data is available) and the error code returned when xColumn or
6035 ** some other method is next invoked on the save virtual table cursor.
6036 */
drhde4fcfd2008-01-19 23:50:26 +00006037 p->inVtabMethod = 1;
6038 rc = pModule->xNext(pCur->pVtabCursor);
6039 p->inVtabMethod = 0;
dan016f7812013-08-21 17:35:48 +00006040 sqlite3VtabImportErrmsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00006041 if( rc==SQLITE_OK ){
6042 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00006043 }
6044
drhde4fcfd2008-01-19 23:50:26 +00006045 if( !res ){
6046 /* If there is data, jump to P2 */
6047 pc = pOp->p2 - 1;
6048 }
drh49afe3a2013-07-10 03:05:14 +00006049 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00006050}
6051#endif /* SQLITE_OMIT_VIRTUALTABLE */
6052
danielk1977182c4ba2007-06-27 15:53:34 +00006053#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006054/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00006055**
drh66a51672008-01-03 00:01:23 +00006056** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00006057** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00006058** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00006059*/
drh9cbf3422008-01-17 16:22:13 +00006060case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006061 sqlite3_vtab *pVtab;
6062 Mem *pName;
6063
danielk1977595a5232009-07-24 17:58:53 +00006064 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006065 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006066 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006067 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006068 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006069 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006070 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006071 testcase( pName->enc==SQLITE_UTF8 );
6072 testcase( pName->enc==SQLITE_UTF16BE );
6073 testcase( pName->enc==SQLITE_UTF16LE );
6074 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6075 if( rc==SQLITE_OK ){
6076 rc = pVtab->pModule->xRename(pVtab, pName->z);
dan016f7812013-08-21 17:35:48 +00006077 sqlite3VtabImportErrmsg(p, pVtab);
drh98655a62011-10-18 22:07:47 +00006078 p->expired = 0;
6079 }
danielk1977182c4ba2007-06-27 15:53:34 +00006080 break;
6081}
6082#endif
drh4cbdda92006-06-14 19:00:20 +00006083
6084#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006085/* Opcode: VUpdate P1 P2 P3 P4 *
drhf63552b2013-10-30 00:25:03 +00006086** Synopsis: data=r[P3@P2]
danielk1977399918f2006-06-14 13:03:23 +00006087**
drh66a51672008-01-03 00:01:23 +00006088** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006089** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006090** are contiguous memory cells starting at P3 to pass to the xUpdate
6091** invocation. The value in register (P3+P2-1) corresponds to the
6092** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006093**
6094** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006095** The argv[0] element (which corresponds to memory cell P3)
6096** is the rowid of a row to delete. If argv[0] is NULL then no
6097** deletion occurs. The argv[1] element is the rowid of the new
6098** row. This can be NULL to have the virtual table select the new
6099** rowid for itself. The subsequent elements in the array are
6100** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006101**
6102** If P2==1 then no insert is performed. argv[0] is the rowid of
6103** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006104**
6105** P1 is a boolean flag. If it is set to true and the xUpdate call
6106** is successful, then the value returned by sqlite3_last_insert_rowid()
6107** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00006108*/
drh9cbf3422008-01-17 16:22:13 +00006109case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006110 sqlite3_vtab *pVtab;
6111 sqlite3_module *pModule;
6112 int nArg;
6113 int i;
6114 sqlite_int64 rowid;
6115 Mem **apArg;
6116 Mem *pX;
6117
danb061d052011-04-25 18:49:57 +00006118 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6119 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6120 );
drh9e92a472013-06-27 17:40:30 +00006121 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006122 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006123 pModule = (sqlite3_module *)pVtab->pModule;
6124 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006125 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006126 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006127 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006128 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006129 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006130 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006131 assert( memIsValid(pX) );
6132 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00006133 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00006134 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006135 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006136 }
danb061d052011-04-25 18:49:57 +00006137 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006138 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006139 db->vtabOnConflict = vtabOnConflict;
dan016f7812013-08-21 17:35:48 +00006140 sqlite3VtabImportErrmsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006141 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006142 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006143 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006144 }
drhd91c1a12013-02-09 13:58:25 +00006145 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006146 if( pOp->p5==OE_Ignore ){
6147 rc = SQLITE_OK;
6148 }else{
6149 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6150 }
6151 }else{
6152 p->nChange++;
6153 }
danielk1977399918f2006-06-14 13:03:23 +00006154 }
drh4cbdda92006-06-14 19:00:20 +00006155 break;
danielk1977399918f2006-06-14 13:03:23 +00006156}
6157#endif /* SQLITE_OMIT_VIRTUALTABLE */
6158
danielk197759a93792008-05-15 17:48:20 +00006159#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6160/* Opcode: Pagecount P1 P2 * * *
6161**
6162** Write the current number of pages in database P1 to memory cell P2.
6163*/
6164case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006165 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006166 break;
6167}
6168#endif
6169
drh60ac3f42010-11-23 18:59:27 +00006170
6171#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6172/* Opcode: MaxPgcnt P1 P2 P3 * *
6173**
6174** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006175** Do not let the maximum page count fall below the current page count and
6176** do not change the maximum page count value if P3==0.
6177**
drh60ac3f42010-11-23 18:59:27 +00006178** Store the maximum page count after the change in register P2.
6179*/
6180case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006181 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006182 Btree *pBt;
6183
6184 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006185 newMax = 0;
6186 if( pOp->p3 ){
6187 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006188 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006189 }
6190 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006191 break;
6192}
6193#endif
6194
6195
drh949f9cd2008-01-12 21:35:57 +00006196#ifndef SQLITE_OMIT_TRACE
6197/* Opcode: Trace * * * P4 *
6198**
6199** If tracing is enabled (by the sqlite3_trace()) interface, then
6200** the UTF-8 string contained in P4 is emitted on the trace callback.
6201*/
6202case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00006203 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006204 char *z;
drh856c1032009-06-02 15:21:42 +00006205
drh37f58e92012-09-04 21:34:26 +00006206 if( db->xTrace
6207 && !p->doingRerun
6208 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6209 ){
drhc3f1d5f2011-05-30 23:42:16 +00006210 z = sqlite3VdbeExpandSql(p, zTrace);
6211 db->xTrace(db->pTraceArg, z);
6212 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006213 }
drh8f8b2312013-10-18 20:03:43 +00006214#ifdef SQLITE_USE_FCNTL_TRACE
6215 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
6216 if( zTrace ){
6217 int i;
6218 for(i=0; i<db->nDb; i++){
6219 if( ((1<<i) & p->btreeMask)==0 ) continue;
6220 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
6221 }
6222 }
6223#endif /* SQLITE_USE_FCNTL_TRACE */
drhc3f1d5f2011-05-30 23:42:16 +00006224#ifdef SQLITE_DEBUG
6225 if( (db->flags & SQLITE_SqlTrace)!=0
6226 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6227 ){
6228 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6229 }
6230#endif /* SQLITE_DEBUG */
drh949f9cd2008-01-12 21:35:57 +00006231 break;
6232}
6233#endif
6234
drh91fd4d42008-01-19 20:11:25 +00006235
6236/* Opcode: Noop * * * * *
6237**
6238** Do nothing. This instruction is often useful as a jump
6239** destination.
drh5e00f6c2001-09-13 13:46:56 +00006240*/
drh91fd4d42008-01-19 20:11:25 +00006241/*
6242** The magic Explain opcode are only inserted when explain==2 (which
6243** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6244** This opcode records information from the optimizer. It is the
6245** the same as a no-op. This opcodesnever appears in a real VM program.
6246*/
6247default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006248 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006249 break;
6250}
6251
6252/*****************************************************************************
6253** The cases of the switch statement above this line should all be indented
6254** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6255** readability. From this point on down, the normal indentation rules are
6256** restored.
6257*****************************************************************************/
6258 }
drh6e142f52000-06-08 13:36:40 +00006259
drh7b396862003-01-01 23:06:20 +00006260#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006261 {
shane9bcbdad2008-05-29 20:22:37 +00006262 u64 elapsed = sqlite3Hwtime() - start;
6263 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006264 pOp->cnt++;
6265#if 0
shane9bcbdad2008-05-29 20:22:37 +00006266 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006267 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006268#endif
6269 }
drh7b396862003-01-01 23:06:20 +00006270#endif
6271
drh6e142f52000-06-08 13:36:40 +00006272 /* The following code adds nothing to the actual functionality
6273 ** of the program. It is only here for testing and debugging.
6274 ** On the other hand, it does burn CPU cycles every time through
6275 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6276 */
6277#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006278 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006279
drhcf1023c2007-05-08 20:59:49 +00006280#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00006281 if( p->trace ){
6282 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006283 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
6284 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006285 }
drh3c657212009-11-17 23:59:58 +00006286 if( pOp->opflags & OPFLG_OUT3 ){
6287 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006288 }
drh75897232000-05-29 14:26:00 +00006289 }
danielk1977b5402fb2005-01-12 07:15:04 +00006290#endif /* SQLITE_DEBUG */
6291#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006292 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006293
drha05a7222008-01-19 03:35:58 +00006294 /* If we reach this point, it means that execution is finished with
6295 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006296 */
drha05a7222008-01-19 03:35:58 +00006297vdbe_error_halt:
6298 assert( rc );
6299 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006300 testcase( sqlite3GlobalConfig.xLog!=0 );
6301 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6302 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006303 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006304 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6305 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006306 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006307 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006308 }
drh900b31e2007-08-28 02:27:51 +00006309
6310 /* This is the only way out of this procedure. We have to
6311 ** release the mutexes on btrees that were acquired at the
6312 ** top. */
6313vdbe_return:
drh99a66922011-05-13 18:51:42 +00006314 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006315 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006316 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006317 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006318 return rc;
6319
drh023ae032007-05-08 12:12:16 +00006320 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6321 ** is encountered.
6322 */
6323too_big:
drhf089aa42008-07-08 19:34:06 +00006324 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006325 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006326 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006327
drh98640a32007-06-07 19:08:32 +00006328 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006329 */
6330no_mem:
drh17435752007-08-16 04:30:38 +00006331 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006332 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006333 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006334 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006335
drhb86ccfb2003-01-28 23:13:10 +00006336 /* Jump to here for any other kind of fatal error. The "rc" variable
6337 ** should hold the error number.
6338 */
6339abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006340 assert( p->zErrMsg==0 );
6341 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006342 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006343 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006344 }
drha05a7222008-01-19 03:35:58 +00006345 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006346
danielk19776f8a5032004-05-10 10:34:51 +00006347 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006348 ** flag.
6349 */
6350abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006351 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006352 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006353 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006354 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006355 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006356}