blob: 516e8ae9b48dbb5ecb4fdcecbf6b02f1b7f85e54 [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
drhcaec2f12003-01-07 02:47:47 +0000500/*
drhb9755982010-07-24 16:34:37 +0000501** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
502** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
503** in memory obtained from sqlite3DbMalloc).
504*/
505static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
506 sqlite3 *db = p->db;
507 sqlite3DbFree(db, p->zErrMsg);
508 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
509 sqlite3_free(pVtab->zErrMsg);
510 pVtab->zErrMsg = 0;
511}
512
513
514/*
drhb86ccfb2003-01-28 23:13:10 +0000515** Execute as much of a VDBE program as we can then return.
516**
danielk19774adee202004-05-08 08:23:19 +0000517** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000518** close the program with a final OP_Halt and to set up the callbacks
519** and the error message pointer.
520**
521** Whenever a row or result data is available, this routine will either
522** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000523** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000524**
525** If an attempt is made to open a locked database, then this routine
526** will either invoke the busy callback (if there is one) or it will
527** return SQLITE_BUSY.
528**
529** If an error occurs, an error message is written to memory obtained
drh17435752007-08-16 04:30:38 +0000530** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
drhb86ccfb2003-01-28 23:13:10 +0000531** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
532**
533** If the callback ever returns non-zero, then the program exits
534** immediately. There will be no error message but the p->rc field is
535** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
536**
drh9468c7f2003-03-07 19:50:07 +0000537** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
538** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000539**
540** Other fatal errors return SQLITE_ERROR.
541**
danielk19774adee202004-05-08 08:23:19 +0000542** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000543** used to clean up the mess that was left behind.
544*/
danielk19774adee202004-05-08 08:23:19 +0000545int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000546 Vdbe *p /* The VDBE */
547){
shaneh84f4b2f2010-02-26 01:46:54 +0000548 int pc=0; /* The program counter */
drhbbe879d2009-11-14 18:04:35 +0000549 Op *aOp = p->aOp; /* Copy of p->aOp */
drhb86ccfb2003-01-28 23:13:10 +0000550 Op *pOp; /* Current operation */
551 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000552 sqlite3 *db = p->db; /* The database */
drhcdf011d2011-04-04 21:25:28 +0000553 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
drh8079a0d2006-01-12 17:20:50 +0000554 u8 encoding = ENC(db); /* The database encoding */
drhbf159fa2013-06-25 22:01:22 +0000555 int iCompare = 0; /* Result of last OP_Compare operation */
556 unsigned nVmStep = 0; /* Number of virtual machine steps */
drh49afe3a2013-07-10 03:05:14 +0000557#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh323df792013-08-05 19:11:29 +0000558 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
drh49afe3a2013-07-10 03:05:14 +0000559#endif
drha6c2ed92009-11-14 23:22:23 +0000560 Mem *aMem = p->aMem; /* Copy of p->aMem */
drhb27b7f52008-12-10 18:03:45 +0000561 Mem *pIn1 = 0; /* 1st input operand */
562 Mem *pIn2 = 0; /* 2nd input operand */
563 Mem *pIn3 = 0; /* 3rd input operand */
564 Mem *pOut = 0; /* Output operand */
shanebe217792009-03-05 04:20:31 +0000565 int *aPermute = 0; /* Permutation of columns for OP_Compare */
drh99a66922011-05-13 18:51:42 +0000566 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */
drhb86ccfb2003-01-28 23:13:10 +0000567#ifdef VDBE_PROFILE
shane9bcbdad2008-05-29 20:22:37 +0000568 u64 start; /* CPU clock count at start of opcode */
drhb86ccfb2003-01-28 23:13:10 +0000569 int origPc; /* Program counter at start of opcode */
570#endif
drh856c1032009-06-02 15:21:42 +0000571 /*** INSERT STACK UNION HERE ***/
drhe63d9992008-08-13 19:11:48 +0000572
drhca48c902008-01-18 14:08:24 +0000573 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
drhbdaec522011-04-04 00:14:43 +0000574 sqlite3VdbeEnter(p);
danielk19772e588c72005-12-09 14:25:08 +0000575 if( p->rc==SQLITE_NOMEM ){
576 /* This happens if a malloc() inside a call to sqlite3_column_text() or
577 ** sqlite3_column_text16() failed. */
578 goto no_mem;
579 }
drh3a840692003-01-29 22:58:26 +0000580 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
drh1713afb2013-06-28 01:24:57 +0000581 assert( p->bIsReader || p->readOnly!=0 );
drh3a840692003-01-29 22:58:26 +0000582 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000583 assert( p->explain==0 );
drhd4e70eb2008-01-02 00:34:36 +0000584 p->pResultSet = 0;
drha4afb652005-07-09 02:16:02 +0000585 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000586 CHECK_FOR_INTERRUPT;
drh602c2372007-03-01 00:29:13 +0000587 sqlite3VdbeIOTraceSql(p);
drh0d1961e2013-07-25 16:27:51 +0000588#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
589 if( db->xProgress ){
590 assert( 0 < db->nProgressOps );
drh9b47ee32013-08-20 03:13:51 +0000591 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
drh0d1961e2013-07-25 16:27:51 +0000592 if( nProgressLimit==0 ){
593 nProgressLimit = db->nProgressOps;
594 }else{
595 nProgressLimit %= (unsigned)db->nProgressOps;
596 }
597 }
598#endif
drh3c23a882007-01-09 14:01:13 +0000599#ifdef SQLITE_DEBUG
danielk19772d1d86f2008-06-20 14:59:51 +0000600 sqlite3BeginBenignMalloc();
drh42224412010-05-31 14:28:25 +0000601 if( p->pc==0 && (p->db->flags & SQLITE_VdbeListing)!=0 ){
drh3c23a882007-01-09 14:01:13 +0000602 int i;
603 printf("VDBE Program Listing:\n");
604 sqlite3VdbePrintSql(p);
605 for(i=0; i<p->nOp; i++){
drhbbe879d2009-11-14 18:04:35 +0000606 sqlite3VdbePrintOp(stdout, i, &aOp[i]);
drh3c23a882007-01-09 14:01:13 +0000607 }
608 }
danielk19772d1d86f2008-06-20 14:59:51 +0000609 sqlite3EndBenignMalloc();
drh3c23a882007-01-09 14:01:13 +0000610#endif
drhb86ccfb2003-01-28 23:13:10 +0000611 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000612 assert( pc>=0 && pc<p->nOp );
drh17435752007-08-16 04:30:38 +0000613 if( db->mallocFailed ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000614#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000615 origPc = pc;
shane9bcbdad2008-05-29 20:22:37 +0000616 start = sqlite3Hwtime();
drh7b396862003-01-01 23:06:20 +0000617#endif
drhbf159fa2013-06-25 22:01:22 +0000618 nVmStep++;
drhbbe879d2009-11-14 18:04:35 +0000619 pOp = &aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000620
danielk19778b60e0f2005-01-12 09:10:39 +0000621 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000622 */
danielk19778b60e0f2005-01-12 09:10:39 +0000623#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000624 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000625 if( pc==0 ){
626 printf("VDBE Execution Trace:\n");
627 sqlite3VdbePrintSql(p);
628 }
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000630 }
drh3f7d4e42004-07-24 14:35:58 +0000631#endif
632
drh6e142f52000-06-08 13:36:40 +0000633
drhf6038712004-02-08 18:07:34 +0000634 /* Check to see if we need to simulate an interrupt. This only happens
635 ** if we have a special test build.
636 */
637#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000638 if( sqlite3_interrupt_count>0 ){
639 sqlite3_interrupt_count--;
640 if( sqlite3_interrupt_count==0 ){
641 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000642 }
643 }
644#endif
645
drhb5b407e2012-08-29 10:28:43 +0000646 /* On any opcode with the "out2-prerelease" tag, free any
drh3c657212009-11-17 23:59:58 +0000647 ** external allocations out of mem[p2] and set mem[p2] to be
648 ** an undefined integer. Opcodes will either fill in the integer
649 ** value or convert mem[p2] to a different type.
drh4c583122008-01-04 22:01:03 +0000650 */
drha6c2ed92009-11-14 23:22:23 +0000651 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
drh3c657212009-11-17 23:59:58 +0000652 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
653 assert( pOp->p2>0 );
654 assert( pOp->p2<=p->nMem );
655 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +0000656 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000657 VdbeMemRelease(pOut);
drh3c657212009-11-17 23:59:58 +0000658 pOut->flags = MEM_Int;
drh4c583122008-01-04 22:01:03 +0000659 }
drh3c657212009-11-17 23:59:58 +0000660
661 /* Sanity checking on other operands */
662#ifdef SQLITE_DEBUG
663 if( (pOp->opflags & OPFLG_IN1)!=0 ){
664 assert( pOp->p1>0 );
665 assert( pOp->p1<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000666 assert( memIsValid(&aMem[pOp->p1]) );
drh3c657212009-11-17 23:59:58 +0000667 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
668 }
669 if( (pOp->opflags & OPFLG_IN2)!=0 ){
670 assert( pOp->p2>0 );
671 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000672 assert( memIsValid(&aMem[pOp->p2]) );
drh3c657212009-11-17 23:59:58 +0000673 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
674 }
675 if( (pOp->opflags & OPFLG_IN3)!=0 ){
676 assert( pOp->p3>0 );
677 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000678 assert( memIsValid(&aMem[pOp->p3]) );
drh3c657212009-11-17 23:59:58 +0000679 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
680 }
681 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
682 assert( pOp->p2>0 );
683 assert( pOp->p2<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000684 memAboutToChange(p, &aMem[pOp->p2]);
drh3c657212009-11-17 23:59:58 +0000685 }
686 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
687 assert( pOp->p3>0 );
688 assert( pOp->p3<=p->nMem );
drh2b4ded92010-09-27 21:09:31 +0000689 memAboutToChange(p, &aMem[pOp->p3]);
drh3c657212009-11-17 23:59:58 +0000690 }
691#endif
drh93952eb2009-11-13 19:43:43 +0000692
drh75897232000-05-29 14:26:00 +0000693 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000694
drh5e00f6c2001-09-13 13:46:56 +0000695/*****************************************************************************
696** What follows is a massive switch statement where each case implements a
697** separate instruction in the virtual machine. If we follow the usual
698** indentation conventions, each case should be indented by 6 spaces. But
699** that is a lot of wasted space on the left margin. So the code within
700** the switch statement will break with convention and be flush-left. Another
701** big comment (similar to this one) will mark the point in the code where
702** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000703**
704** The formatting of each case is important. The makefile for SQLite
705** generates two C files "opcodes.h" and "opcodes.c" by scanning this
706** file looking for lines that begin with "case OP_". The opcodes.h files
707** will be filled with #defines that give unique integer values to each
708** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000709** each string is the symbolic name for the corresponding opcode. If the
710** case statement is followed by a comment of the form "/# same as ... #/"
711** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000712**
drh9cbf3422008-01-17 16:22:13 +0000713** Other keywords in the comment that follows each case are used to
714** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
715** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See
716** the mkopcodeh.awk script for additional information.
danielk1977bc04f852005-03-29 08:26:13 +0000717**
drhac82fcf2002-09-08 17:23:41 +0000718** Documentation about VDBE opcodes is generated by scanning this file
719** for lines of that contain "Opcode:". That line and all subsequent
720** comment lines are used in the generation of the opcode.html documentation
721** file.
722**
723** SUMMARY:
724**
725** Formatting is important to scripts that scan this file.
726** Do not deviate from the formatting style currently in use.
727**
drh5e00f6c2001-09-13 13:46:56 +0000728*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000729
drh9cbf3422008-01-17 16:22:13 +0000730/* Opcode: Goto * P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000731**
732** An unconditional jump to address P2.
733** The next instruction executed will be
734** the one at index P2 from the beginning of
735** the program.
736*/
drh9cbf3422008-01-17 16:22:13 +0000737case OP_Goto: { /* jump */
drh5e00f6c2001-09-13 13:46:56 +0000738 pc = pOp->p2 - 1;
drh49afe3a2013-07-10 03:05:14 +0000739
740 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
741 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
742 ** completion. Check to see if sqlite3_interrupt() has been called
743 ** or if the progress callback needs to be invoked.
744 **
745 ** This code uses unstructured "goto" statements and does not look clean.
746 ** But that is not due to sloppy coding habits. The code is written this
747 ** way for performance, to avoid having to run the interrupt and progress
748 ** checks on every opcode. This helps sqlite3_step() to run about 1.5%
749 ** faster according to "valgrind --tool=cachegrind" */
750check_for_interrupt:
751 CHECK_FOR_INTERRUPT;
752#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
753 /* Call the progress callback if it is configured and the required number
754 ** of VDBE ops have been executed (either since this invocation of
755 ** sqlite3VdbeExec() or since last time the progress callback was called).
756 ** If the progress callback returns non-zero, exit the virtual machine with
757 ** a return code SQLITE_ABORT.
758 */
drh0d1961e2013-07-25 16:27:51 +0000759 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
drh49afe3a2013-07-10 03:05:14 +0000760 int prc;
761 prc = db->xProgress(db->pProgressArg);
762 if( prc!=0 ){
763 rc = SQLITE_INTERRUPT;
764 goto vdbe_error_halt;
765 }
drh0d1961e2013-07-25 16:27:51 +0000766 if( db->xProgress!=0 ){
767 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
768 }
drh49afe3a2013-07-10 03:05:14 +0000769 }
770#endif
771
drh5e00f6c2001-09-13 13:46:56 +0000772 break;
773}
drh75897232000-05-29 14:26:00 +0000774
drh2eb95372008-06-06 15:04:36 +0000775/* Opcode: Gosub P1 P2 * * *
drh8c74a8c2002-08-25 19:20:40 +0000776**
drh2eb95372008-06-06 15:04:36 +0000777** Write the current address onto register P1
drh8c74a8c2002-08-25 19:20:40 +0000778** and then jump to address P2.
drh8c74a8c2002-08-25 19:20:40 +0000779*/
drhb8475df2011-12-09 16:21:19 +0000780case OP_Gosub: { /* jump */
781 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drh3c657212009-11-17 23:59:58 +0000782 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000783 assert( (pIn1->flags & MEM_Dyn)==0 );
drh2b4ded92010-09-27 21:09:31 +0000784 memAboutToChange(p, pIn1);
drh2eb95372008-06-06 15:04:36 +0000785 pIn1->flags = MEM_Int;
786 pIn1->u.i = pc;
787 REGISTER_TRACE(pOp->p1, pIn1);
drh8c74a8c2002-08-25 19:20:40 +0000788 pc = pOp->p2 - 1;
789 break;
790}
791
drh2eb95372008-06-06 15:04:36 +0000792/* Opcode: Return P1 * * * *
drh8c74a8c2002-08-25 19:20:40 +0000793**
drh2eb95372008-06-06 15:04:36 +0000794** Jump to the next instruction after the address in register P1.
drh8c74a8c2002-08-25 19:20:40 +0000795*/
drh2eb95372008-06-06 15:04:36 +0000796case OP_Return: { /* in1 */
drh3c657212009-11-17 23:59:58 +0000797 pIn1 = &aMem[pOp->p1];
drh2eb95372008-06-06 15:04:36 +0000798 assert( pIn1->flags & MEM_Int );
drh9c1905f2008-12-10 22:32:56 +0000799 pc = (int)pIn1->u.i;
drh8c74a8c2002-08-25 19:20:40 +0000800 break;
801}
802
drhe00ee6e2008-06-20 15:24:01 +0000803/* Opcode: Yield P1 * * * *
804**
805** Swap the program counter with the value in register P1.
806*/
danielk1977f73ab8b2008-12-29 10:39:53 +0000807case OP_Yield: { /* in1 */
drhe00ee6e2008-06-20 15:24:01 +0000808 int pcDest;
drh3c657212009-11-17 23:59:58 +0000809 pIn1 = &aMem[pOp->p1];
drhe00ee6e2008-06-20 15:24:01 +0000810 assert( (pIn1->flags & MEM_Dyn)==0 );
811 pIn1->flags = MEM_Int;
drh9c1905f2008-12-10 22:32:56 +0000812 pcDest = (int)pIn1->u.i;
drhe00ee6e2008-06-20 15:24:01 +0000813 pIn1->u.i = pc;
814 REGISTER_TRACE(pOp->p1, pIn1);
815 pc = pcDest;
816 break;
817}
818
drh5053a792009-02-20 03:02:23 +0000819/* Opcode: HaltIfNull P1 P2 P3 P4 *
820**
drhef8662b2011-06-20 21:47:58 +0000821** Check the value in register P3. If it is NULL then Halt using
drh5053a792009-02-20 03:02:23 +0000822** parameter P1, P2, and P4 as if this were a Halt instruction. If the
823** value in register P3 is not NULL, then this routine is a no-op.
824*/
825case OP_HaltIfNull: { /* in3 */
drh3c657212009-11-17 23:59:58 +0000826 pIn3 = &aMem[pOp->p3];
drh5053a792009-02-20 03:02:23 +0000827 if( (pIn3->flags & MEM_Null)==0 ) break;
828 /* Fall through into OP_Halt */
829}
drhe00ee6e2008-06-20 15:24:01 +0000830
drh9cbf3422008-01-17 16:22:13 +0000831/* Opcode: Halt P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +0000832**
drh3d4501e2008-12-04 20:40:10 +0000833** Exit immediately. All open cursors, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000834** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000835**
drh92f02c32004-09-02 14:57:08 +0000836** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
837** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
838** For errors, it can be some other value. If P1!=0 then P2 will determine
839** whether or not to rollback the current transaction. Do not rollback
840** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
841** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000842** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000843**
drh66a51672008-01-03 00:01:23 +0000844** If P4 is not null then it is an error message string.
drh7f057c92005-06-24 03:53:06 +0000845**
drh9cfcf5d2002-01-29 18:41:24 +0000846** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000847** every program. So a jump past the last instruction of the program
848** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000849*/
drh9cbf3422008-01-17 16:22:13 +0000850case OP_Halt: {
dan165921a2009-08-28 18:53:45 +0000851 if( pOp->p1==SQLITE_OK && p->pFrame ){
dan2832ad42009-08-31 15:27:27 +0000852 /* Halt the sub-program. Return control to the parent frame. */
dan165921a2009-08-28 18:53:45 +0000853 VdbeFrame *pFrame = p->pFrame;
854 p->pFrame = pFrame->pParent;
855 p->nFrame--;
dan2832ad42009-08-31 15:27:27 +0000856 sqlite3VdbeSetChanges(db, p->nChange);
dan165921a2009-08-28 18:53:45 +0000857 pc = sqlite3VdbeFrameRestore(pFrame);
drh99a66922011-05-13 18:51:42 +0000858 lastRowid = db->lastRowid;
dan165921a2009-08-28 18:53:45 +0000859 if( pOp->p2==OE_Ignore ){
dan2832ad42009-08-31 15:27:27 +0000860 /* Instruction pc is the OP_Program that invoked the sub-program
861 ** currently being halted. If the p2 instruction of this OP_Halt
862 ** instruction is set to OE_Ignore, then the sub-program is throwing
863 ** an IGNORE exception. In this case jump to the address specified
864 ** as the p2 of the calling OP_Program. */
dan76d462e2009-08-30 11:42:51 +0000865 pc = p->aOp[pc].p2-1;
dan165921a2009-08-28 18:53:45 +0000866 }
drhbbe879d2009-11-14 18:04:35 +0000867 aOp = p->aOp;
drha6c2ed92009-11-14 23:22:23 +0000868 aMem = p->aMem;
dan165921a2009-08-28 18:53:45 +0000869 break;
870 }
dan2832ad42009-08-31 15:27:27 +0000871
drh92f02c32004-09-02 14:57:08 +0000872 p->rc = pOp->p1;
shane36840fd2009-06-26 16:32:13 +0000873 p->errorAction = (u8)pOp->p2;
dan165921a2009-08-28 18:53:45 +0000874 p->pc = pc;
danielk19772dca4ac2008-01-03 11:50:29 +0000875 if( pOp->p4.z ){
drh413c3d32010-02-23 20:11:56 +0000876 assert( p->rc!=SQLITE_OK );
drhf089aa42008-07-08 19:34:06 +0000877 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
drhaf46dc12010-02-24 21:44:07 +0000878 testcase( sqlite3GlobalConfig.xLog!=0 );
drh413c3d32010-02-23 20:11:56 +0000879 sqlite3_log(pOp->p1, "abort at %d in [%s]: %s", pc, p->zSql, pOp->p4.z);
drhcda455b2010-02-24 19:23:56 +0000880 }else if( p->rc ){
drhaf46dc12010-02-24 21:44:07 +0000881 testcase( sqlite3GlobalConfig.xLog!=0 );
drhcda455b2010-02-24 19:23:56 +0000882 sqlite3_log(pOp->p1, "constraint failed at %d in [%s]", pc, p->zSql);
drh9cfcf5d2002-01-29 18:41:24 +0000883 }
drh92f02c32004-09-02 14:57:08 +0000884 rc = sqlite3VdbeHalt(p);
dan1da40a32009-09-19 17:00:31 +0000885 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
drh92f02c32004-09-02 14:57:08 +0000886 if( rc==SQLITE_BUSY ){
drh900b31e2007-08-28 02:27:51 +0000887 p->rc = rc = SQLITE_BUSY;
888 }else{
drhd91c1a12013-02-09 13:58:25 +0000889 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
drh648e2642013-07-11 15:03:32 +0000890 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
drh900b31e2007-08-28 02:27:51 +0000891 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh92f02c32004-09-02 14:57:08 +0000892 }
drh900b31e2007-08-28 02:27:51 +0000893 goto vdbe_return;
drh5e00f6c2001-09-13 13:46:56 +0000894}
drhc61053b2000-06-04 12:58:36 +0000895
drh4c583122008-01-04 22:01:03 +0000896/* Opcode: Integer P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +0000897**
drh9cbf3422008-01-17 16:22:13 +0000898** The 32-bit integer value P1 is written into register P2.
drh5e00f6c2001-09-13 13:46:56 +0000899*/
drh4c583122008-01-04 22:01:03 +0000900case OP_Integer: { /* out2-prerelease */
drh4c583122008-01-04 22:01:03 +0000901 pOut->u.i = pOp->p1;
drh29dda4a2005-07-21 18:23:20 +0000902 break;
903}
904
drh4c583122008-01-04 22:01:03 +0000905/* Opcode: Int64 * P2 * P4 *
drh29dda4a2005-07-21 18:23:20 +0000906**
drh66a51672008-01-03 00:01:23 +0000907** P4 is a pointer to a 64-bit integer value.
drh9cbf3422008-01-17 16:22:13 +0000908** Write that value into register P2.
drh29dda4a2005-07-21 18:23:20 +0000909*/
drh4c583122008-01-04 22:01:03 +0000910case OP_Int64: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000911 assert( pOp->p4.pI64!=0 );
drh4c583122008-01-04 22:01:03 +0000912 pOut->u.i = *pOp->p4.pI64;
drhf4479502004-05-27 03:12:53 +0000913 break;
914}
drh4f26d6c2004-05-26 23:25:30 +0000915
drh13573c72010-01-12 17:04:07 +0000916#ifndef SQLITE_OMIT_FLOATING_POINT
drh4c583122008-01-04 22:01:03 +0000917/* Opcode: Real * P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000918**
drh4c583122008-01-04 22:01:03 +0000919** P4 is a pointer to a 64-bit floating point value.
drh9cbf3422008-01-17 16:22:13 +0000920** Write that value into register P2.
drhf4479502004-05-27 03:12:53 +0000921*/
drh4c583122008-01-04 22:01:03 +0000922case OP_Real: { /* same as TK_FLOAT, out2-prerelease */
923 pOut->flags = MEM_Real;
drh2eaf93d2008-04-29 00:15:20 +0000924 assert( !sqlite3IsNaN(*pOp->p4.pReal) );
drh4c583122008-01-04 22:01:03 +0000925 pOut->r = *pOp->p4.pReal;
drhf4479502004-05-27 03:12:53 +0000926 break;
927}
drh13573c72010-01-12 17:04:07 +0000928#endif
danielk1977cbb18d22004-05-28 11:37:27 +0000929
drh3c84ddf2008-01-09 02:15:38 +0000930/* Opcode: String8 * P2 * P4 *
danielk1977cbb18d22004-05-28 11:37:27 +0000931**
drh66a51672008-01-03 00:01:23 +0000932** P4 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000933** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000934*/
drh4c583122008-01-04 22:01:03 +0000935case OP_String8: { /* same as TK_STRING, out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000936 assert( pOp->p4.z!=0 );
drhed2df7f2005-11-16 04:34:32 +0000937 pOp->opcode = OP_String;
drhea678832008-12-10 19:26:22 +0000938 pOp->p1 = sqlite3Strlen30(pOp->p4.z);
drhed2df7f2005-11-16 04:34:32 +0000939
940#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000941 if( encoding!=SQLITE_UTF8 ){
drh3a9cf172009-06-17 21:42:33 +0000942 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
943 if( rc==SQLITE_TOOBIG ) goto too_big;
drh4c583122008-01-04 22:01:03 +0000944 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
drh3a9cf172009-06-17 21:42:33 +0000945 assert( pOut->zMalloc==pOut->z );
946 assert( pOut->flags & MEM_Dyn );
danielk19775f096132008-03-28 15:44:09 +0000947 pOut->zMalloc = 0;
drh4c583122008-01-04 22:01:03 +0000948 pOut->flags |= MEM_Static;
drh191b54c2008-04-15 12:14:21 +0000949 pOut->flags &= ~MEM_Dyn;
drh66a51672008-01-03 00:01:23 +0000950 if( pOp->p4type==P4_DYNAMIC ){
drh633e6d52008-07-28 19:34:53 +0000951 sqlite3DbFree(db, pOp->p4.z);
danielk1977e0048402004-06-15 16:51:01 +0000952 }
drh66a51672008-01-03 00:01:23 +0000953 pOp->p4type = P4_DYNAMIC;
drh4c583122008-01-04 22:01:03 +0000954 pOp->p4.z = pOut->z;
955 pOp->p1 = pOut->n;
danielk19770f69c1e2004-05-29 11:24:50 +0000956 }
danielk197793758c82005-01-21 08:13:14 +0000957#endif
drhbb4957f2008-03-20 14:03:29 +0000958 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhcbd2da92007-12-17 16:20:06 +0000959 goto too_big;
960 }
961 /* Fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000962}
drhf4479502004-05-27 03:12:53 +0000963
drh4c583122008-01-04 22:01:03 +0000964/* Opcode: String P1 P2 * P4 *
drhf4479502004-05-27 03:12:53 +0000965**
drh9cbf3422008-01-17 16:22:13 +0000966** The string value P4 of length P1 (bytes) is stored in register P2.
drhf4479502004-05-27 03:12:53 +0000967*/
drh4c583122008-01-04 22:01:03 +0000968case OP_String: { /* out2-prerelease */
danielk19772dca4ac2008-01-03 11:50:29 +0000969 assert( pOp->p4.z!=0 );
drh4c583122008-01-04 22:01:03 +0000970 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
971 pOut->z = pOp->p4.z;
972 pOut->n = pOp->p1;
973 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +0000974 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977c572ef72004-05-27 09:28:41 +0000975 break;
976}
977
drh053a1282012-09-19 21:15:46 +0000978/* Opcode: Null P1 P2 P3 * *
drhf0863fe2005-06-12 21:35:51 +0000979**
drhb8475df2011-12-09 16:21:19 +0000980** Write a NULL into registers P2. If P3 greater than P2, then also write
drh053a1282012-09-19 21:15:46 +0000981** NULL into register P3 and every register in between P2 and P3. If P3
drhb8475df2011-12-09 16:21:19 +0000982** is less than P2 (typically P3 is zero) then only register P2 is
drh053a1282012-09-19 21:15:46 +0000983** set to NULL.
984**
985** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
986** NULL values will not compare equal even if SQLITE_NULLEQ is set on
987** OP_Ne or OP_Eq.
drhf0863fe2005-06-12 21:35:51 +0000988*/
drh4c583122008-01-04 22:01:03 +0000989case OP_Null: { /* out2-prerelease */
drhb8475df2011-12-09 16:21:19 +0000990 int cnt;
drh053a1282012-09-19 21:15:46 +0000991 u16 nullFlag;
drhb8475df2011-12-09 16:21:19 +0000992 cnt = pOp->p3-pOp->p2;
993 assert( pOp->p3<=p->nMem );
drh053a1282012-09-19 21:15:46 +0000994 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
drhb8475df2011-12-09 16:21:19 +0000995 while( cnt>0 ){
996 pOut++;
997 memAboutToChange(p, pOut);
drhe4c88c02012-01-04 12:57:45 +0000998 VdbeMemRelease(pOut);
drh053a1282012-09-19 21:15:46 +0000999 pOut->flags = nullFlag;
drhb8475df2011-12-09 16:21:19 +00001000 cnt--;
1001 }
drhf0863fe2005-06-12 21:35:51 +00001002 break;
1003}
1004
1005
drh9de221d2008-01-05 06:51:30 +00001006/* Opcode: Blob P1 P2 * P4
danielk1977c572ef72004-05-27 09:28:41 +00001007**
drh9de221d2008-01-05 06:51:30 +00001008** P4 points to a blob of data P1 bytes long. Store this
drh710c4842010-08-30 01:17:20 +00001009** blob in register P2.
danielk1977c572ef72004-05-27 09:28:41 +00001010*/
drh4c583122008-01-04 22:01:03 +00001011case OP_Blob: { /* out2-prerelease */
drhcbd2da92007-12-17 16:20:06 +00001012 assert( pOp->p1 <= SQLITE_MAX_LENGTH );
drh4c583122008-01-04 22:01:03 +00001013 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
drh9de221d2008-01-05 06:51:30 +00001014 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001015 UPDATE_MAX_BLOBSIZE(pOut);
danielk1977a37cdde2004-05-16 11:15:36 +00001016 break;
1017}
1018
drheaf52d82010-05-12 13:50:23 +00001019/* Opcode: Variable P1 P2 * P4 *
drh50457892003-09-06 01:10:47 +00001020**
drheaf52d82010-05-12 13:50:23 +00001021** Transfer the values of bound parameter P1 into register P2
drh08de1492009-02-20 03:55:05 +00001022**
1023** If the parameter is named, then its name appears in P4 and P3==1.
1024** The P4 value is used by sqlite3_bind_parameter_name().
drh50457892003-09-06 01:10:47 +00001025*/
drheaf52d82010-05-12 13:50:23 +00001026case OP_Variable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00001027 Mem *pVar; /* Value being transferred */
1028
drheaf52d82010-05-12 13:50:23 +00001029 assert( pOp->p1>0 && pOp->p1<=p->nVar );
drh04e9eea2011-06-01 19:16:06 +00001030 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
drheaf52d82010-05-12 13:50:23 +00001031 pVar = &p->aVar[pOp->p1 - 1];
1032 if( sqlite3VdbeMemTooBig(pVar) ){
1033 goto too_big;
drh023ae032007-05-08 12:12:16 +00001034 }
drheaf52d82010-05-12 13:50:23 +00001035 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
1036 UPDATE_MAX_BLOBSIZE(pOut);
danielk197793d46752004-05-23 13:30:58 +00001037 break;
1038}
danielk1977295ba552004-05-19 10:34:51 +00001039
drhb21e7c72008-06-22 12:37:57 +00001040/* Opcode: Move P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001041**
drhe8e4af72012-09-21 00:04:28 +00001042** Move the values in register P1..P1+P3 over into
1043** registers P2..P2+P3. Registers P1..P1+P3 are
drhb21e7c72008-06-22 12:37:57 +00001044** left holding a NULL. It is an error for register ranges
drhe8e4af72012-09-21 00:04:28 +00001045** P1..P1+P3 and P2..P2+P3 to overlap.
drh5e00f6c2001-09-13 13:46:56 +00001046*/
drhe1349cb2008-04-01 00:36:10 +00001047case OP_Move: {
drh856c1032009-06-02 15:21:42 +00001048 char *zMalloc; /* Holding variable for allocated memory */
1049 int n; /* Number of registers left to copy */
1050 int p1; /* Register to copy from */
1051 int p2; /* Register to copy to */
1052
drhe8e4af72012-09-21 00:04:28 +00001053 n = pOp->p3 + 1;
drh856c1032009-06-02 15:21:42 +00001054 p1 = pOp->p1;
1055 p2 = pOp->p2;
danielk19776ab3a2e2009-02-19 14:39:25 +00001056 assert( n>0 && p1>0 && p2>0 );
drhb21e7c72008-06-22 12:37:57 +00001057 assert( p1+n<=p2 || p2+n<=p1 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001058
drha6c2ed92009-11-14 23:22:23 +00001059 pIn1 = &aMem[p1];
1060 pOut = &aMem[p2];
drhb21e7c72008-06-22 12:37:57 +00001061 while( n-- ){
drha6c2ed92009-11-14 23:22:23 +00001062 assert( pOut<=&aMem[p->nMem] );
1063 assert( pIn1<=&aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00001064 assert( memIsValid(pIn1) );
1065 memAboutToChange(p, pOut);
drhb21e7c72008-06-22 12:37:57 +00001066 zMalloc = pOut->zMalloc;
1067 pOut->zMalloc = 0;
1068 sqlite3VdbeMemMove(pOut, pIn1);
drh52043d72011-08-03 16:40:15 +00001069#ifdef SQLITE_DEBUG
1070 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
1071 pOut->pScopyFrom += p1 - pOp->p2;
1072 }
1073#endif
drhb21e7c72008-06-22 12:37:57 +00001074 pIn1->zMalloc = zMalloc;
1075 REGISTER_TRACE(p2++, pOut);
1076 pIn1++;
1077 pOut++;
1078 }
drhe1349cb2008-04-01 00:36:10 +00001079 break;
1080}
1081
drhe8e4af72012-09-21 00:04:28 +00001082/* Opcode: Copy P1 P2 P3 * *
drhb1fdb2a2008-01-05 04:06:03 +00001083**
drhe8e4af72012-09-21 00:04:28 +00001084** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
drhb1fdb2a2008-01-05 04:06:03 +00001085**
1086** This instruction makes a deep copy of the value. A duplicate
1087** is made of any string or blob constant. See also OP_SCopy.
1088*/
drhe8e4af72012-09-21 00:04:28 +00001089case OP_Copy: {
1090 int n;
1091
1092 n = pOp->p3;
drh3c657212009-11-17 23:59:58 +00001093 pIn1 = &aMem[pOp->p1];
1094 pOut = &aMem[pOp->p2];
drhe1349cb2008-04-01 00:36:10 +00001095 assert( pOut!=pIn1 );
drhe8e4af72012-09-21 00:04:28 +00001096 while( 1 ){
1097 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
1098 Deephemeralize(pOut);
drh953f7612012-12-07 22:18:54 +00001099#ifdef SQLITE_DEBUG
1100 pOut->pScopyFrom = 0;
1101#endif
drhe8e4af72012-09-21 00:04:28 +00001102 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
1103 if( (n--)==0 ) break;
1104 pOut++;
1105 pIn1++;
1106 }
drhe1349cb2008-04-01 00:36:10 +00001107 break;
1108}
1109
drhb1fdb2a2008-01-05 04:06:03 +00001110/* Opcode: SCopy P1 P2 * * *
1111**
drh9cbf3422008-01-17 16:22:13 +00001112** Make a shallow copy of register P1 into register P2.
drhb1fdb2a2008-01-05 04:06:03 +00001113**
1114** This instruction makes a shallow copy of the value. If the value
1115** is a string or blob, then the copy is only a pointer to the
1116** original and hence if the original changes so will the copy.
1117** Worse, if the original is deallocated, the copy becomes invalid.
1118** Thus the program must guarantee that the original will not change
1119** during the lifetime of the copy. Use OP_Copy to make a complete
1120** copy.
1121*/
drh93952eb2009-11-13 19:43:43 +00001122case OP_SCopy: { /* in1, out2 */
drh3c657212009-11-17 23:59:58 +00001123 pIn1 = &aMem[pOp->p1];
1124 pOut = &aMem[pOp->p2];
drh2d401ab2008-01-10 23:50:11 +00001125 assert( pOut!=pIn1 );
drhe1349cb2008-04-01 00:36:10 +00001126 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
drh2b4ded92010-09-27 21:09:31 +00001127#ifdef SQLITE_DEBUG
1128 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
1129#endif
drh5b6afba2008-01-05 16:29:28 +00001130 REGISTER_TRACE(pOp->p2, pOut);
drh5e00f6c2001-09-13 13:46:56 +00001131 break;
1132}
drh75897232000-05-29 14:26:00 +00001133
drh9cbf3422008-01-17 16:22:13 +00001134/* Opcode: ResultRow P1 P2 * * *
drhd4e70eb2008-01-02 00:34:36 +00001135**
shane21e7feb2008-05-30 15:59:49 +00001136** The registers P1 through P1+P2-1 contain a single row of
drhd4e70eb2008-01-02 00:34:36 +00001137** results. This opcode causes the sqlite3_step() call to terminate
1138** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
1139** structure to provide access to the top P1 values as the result
drh9cbf3422008-01-17 16:22:13 +00001140** row.
drhd4e70eb2008-01-02 00:34:36 +00001141*/
drh9cbf3422008-01-17 16:22:13 +00001142case OP_ResultRow: {
drhd4e70eb2008-01-02 00:34:36 +00001143 Mem *pMem;
1144 int i;
1145 assert( p->nResColumn==pOp->p2 );
drh0a07c102008-01-03 18:03:08 +00001146 assert( pOp->p1>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00001147 assert( pOp->p1+pOp->p2<=p->nMem+1 );
drhd4e70eb2008-01-02 00:34:36 +00001148
dan32b09f22009-09-23 17:29:59 +00001149 /* If this statement has violated immediate foreign key constraints, do
1150 ** not return the number of rows modified. And do not RELEASE the statement
1151 ** transaction. It needs to be rolled back. */
1152 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
1153 assert( db->flags&SQLITE_CountRows );
1154 assert( p->usesStmtJournal );
1155 break;
1156 }
1157
danielk1977bd434552009-03-18 10:33:00 +00001158 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
1159 ** DML statements invoke this opcode to return the number of rows
1160 ** modified to the user. This is the only way that a VM that
1161 ** opens a statement transaction may invoke this opcode.
1162 **
1163 ** In case this is such a statement, close any statement transaction
1164 ** opened by this VM before returning control to the user. This is to
1165 ** ensure that statement-transactions are always nested, not overlapping.
1166 ** If the open statement-transaction is not closed here, then the user
1167 ** may step another VM that opens its own statement transaction. This
1168 ** may lead to overlapping statement transactions.
drhaa736092009-06-22 00:55:30 +00001169 **
1170 ** The statement transaction is never a top-level transaction. Hence
1171 ** the RELEASE call below can never fail.
danielk1977bd434552009-03-18 10:33:00 +00001172 */
1173 assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
drhaa736092009-06-22 00:55:30 +00001174 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
1175 if( NEVER(rc!=SQLITE_OK) ){
danielk1977bd434552009-03-18 10:33:00 +00001176 break;
1177 }
1178
drhd4e70eb2008-01-02 00:34:36 +00001179 /* Invalidate all ephemeral cursor row caches */
1180 p->cacheCtr = (p->cacheCtr + 2)|1;
1181
1182 /* Make sure the results of the current row are \000 terminated
shane21e7feb2008-05-30 15:59:49 +00001183 ** and have an assigned type. The results are de-ephemeralized as
drhb8a45bb2011-12-31 21:51:55 +00001184 ** a side effect.
drhd4e70eb2008-01-02 00:34:36 +00001185 */
drha6c2ed92009-11-14 23:22:23 +00001186 pMem = p->pResultSet = &aMem[pOp->p1];
drhd4e70eb2008-01-02 00:34:36 +00001187 for(i=0; i<pOp->p2; i++){
drh2b4ded92010-09-27 21:09:31 +00001188 assert( memIsValid(&pMem[i]) );
drhebc16712010-09-28 00:25:58 +00001189 Deephemeralize(&pMem[i]);
drh746fd9c2010-09-28 06:00:47 +00001190 assert( (pMem[i].flags & MEM_Ephem)==0
1191 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
drhd4e70eb2008-01-02 00:34:36 +00001192 sqlite3VdbeMemNulTerminate(&pMem[i]);
dan937d0de2009-10-15 18:35:38 +00001193 sqlite3VdbeMemStoreType(&pMem[i]);
drh0acb7e42008-06-25 00:12:41 +00001194 REGISTER_TRACE(pOp->p1+i, &pMem[i]);
drhd4e70eb2008-01-02 00:34:36 +00001195 }
drh28039692008-03-17 16:54:01 +00001196 if( db->mallocFailed ) goto no_mem;
drhd4e70eb2008-01-02 00:34:36 +00001197
1198 /* Return SQLITE_ROW
1199 */
drhd4e70eb2008-01-02 00:34:36 +00001200 p->pc = pc + 1;
drhd4e70eb2008-01-02 00:34:36 +00001201 rc = SQLITE_ROW;
1202 goto vdbe_return;
1203}
1204
drh5b6afba2008-01-05 16:29:28 +00001205/* Opcode: Concat P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001206**
drh5b6afba2008-01-05 16:29:28 +00001207** Add the text in register P1 onto the end of the text in
1208** register P2 and store the result in register P3.
1209** If either the P1 or P2 text are NULL then store NULL in P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001210**
1211** P3 = P2 || P1
1212**
1213** It is illegal for P1 and P3 to be the same register. Sometimes,
1214** if P3 is the same register as P2, the implementation is able
1215** to avoid a memcpy().
drh5e00f6c2001-09-13 13:46:56 +00001216*/
drh5b6afba2008-01-05 16:29:28 +00001217case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
drh023ae032007-05-08 12:12:16 +00001218 i64 nByte;
danielk19778a6b5412004-05-24 07:04:25 +00001219
drh3c657212009-11-17 23:59:58 +00001220 pIn1 = &aMem[pOp->p1];
1221 pIn2 = &aMem[pOp->p2];
1222 pOut = &aMem[pOp->p3];
danielk1977a7a8e142008-02-13 18:25:27 +00001223 assert( pIn1!=pOut );
drh5b6afba2008-01-05 16:29:28 +00001224 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
danielk1977a7a8e142008-02-13 18:25:27 +00001225 sqlite3VdbeMemSetNull(pOut);
drh5b6afba2008-01-05 16:29:28 +00001226 break;
drh5e00f6c2001-09-13 13:46:56 +00001227 }
drha0c06522009-06-17 22:50:41 +00001228 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
drh5b6afba2008-01-05 16:29:28 +00001229 Stringify(pIn1, encoding);
drh5b6afba2008-01-05 16:29:28 +00001230 Stringify(pIn2, encoding);
1231 nByte = pIn1->n + pIn2->n;
drhbb4957f2008-03-20 14:03:29 +00001232 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh5b6afba2008-01-05 16:29:28 +00001233 goto too_big;
drh5e00f6c2001-09-13 13:46:56 +00001234 }
danielk1977a7a8e142008-02-13 18:25:27 +00001235 MemSetTypeFlag(pOut, MEM_Str);
drh9c1905f2008-12-10 22:32:56 +00001236 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
drh5b6afba2008-01-05 16:29:28 +00001237 goto no_mem;
1238 }
danielk1977a7a8e142008-02-13 18:25:27 +00001239 if( pOut!=pIn2 ){
1240 memcpy(pOut->z, pIn2->z, pIn2->n);
1241 }
1242 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
1243 pOut->z[nByte] = 0;
1244 pOut->z[nByte+1] = 0;
1245 pOut->flags |= MEM_Term;
drh9c1905f2008-12-10 22:32:56 +00001246 pOut->n = (int)nByte;
drh5b6afba2008-01-05 16:29:28 +00001247 pOut->enc = encoding;
drhb7654112008-01-12 12:48:07 +00001248 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001249 break;
1250}
drh75897232000-05-29 14:26:00 +00001251
drh3c84ddf2008-01-09 02:15:38 +00001252/* Opcode: Add P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001253**
drh60a713c2008-01-21 16:22:45 +00001254** Add the value in register P1 to the value in register P2
shane21e7feb2008-05-30 15:59:49 +00001255** and store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001256** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001257*/
drh3c84ddf2008-01-09 02:15:38 +00001258/* Opcode: Multiply P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001259**
drh3c84ddf2008-01-09 02:15:38 +00001260**
shane21e7feb2008-05-30 15:59:49 +00001261** Multiply the value in register P1 by the value in register P2
drh60a713c2008-01-21 16:22:45 +00001262** and store the result in register P3.
1263** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001264*/
drh3c84ddf2008-01-09 02:15:38 +00001265/* Opcode: Subtract P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001266**
drh60a713c2008-01-21 16:22:45 +00001267** Subtract the value in register P1 from the value in register P2
1268** and store the result in register P3.
1269** If either input is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001270*/
drh9cbf3422008-01-17 16:22:13 +00001271/* Opcode: Divide P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001272**
drh60a713c2008-01-21 16:22:45 +00001273** Divide the value in register P1 by the value in register P2
dane275dc32009-08-18 16:24:58 +00001274** and store the result in register P3 (P3=P2/P1). If the value in
1275** register P1 is zero, then the result is NULL. If either input is
1276** NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001277*/
drh9cbf3422008-01-17 16:22:13 +00001278/* Opcode: Remainder P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001279**
drh3c84ddf2008-01-09 02:15:38 +00001280** Compute the remainder after integer division of the value in
1281** register P1 by the value in register P2 and store the result in P3.
1282** If the value in register P2 is zero the result is NULL.
drhf5905aa2002-05-26 20:54:33 +00001283** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001284*/
drh5b6afba2008-01-05 16:29:28 +00001285case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
1286case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
1287case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
1288case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
1289case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
drhbe707b32012-12-10 22:19:14 +00001290 char bIntint; /* Started out as two integer operands */
drh856c1032009-06-02 15:21:42 +00001291 int flags; /* Combined MEM_* flags from both inputs */
1292 i64 iA; /* Integer value of left operand */
1293 i64 iB; /* Integer value of right operand */
1294 double rA; /* Real value of left operand */
1295 double rB; /* Real value of right operand */
1296
drh3c657212009-11-17 23:59:58 +00001297 pIn1 = &aMem[pOp->p1];
drh61669b32008-07-30 13:27:10 +00001298 applyNumericAffinity(pIn1);
drh3c657212009-11-17 23:59:58 +00001299 pIn2 = &aMem[pOp->p2];
drh61669b32008-07-30 13:27:10 +00001300 applyNumericAffinity(pIn2);
drh3c657212009-11-17 23:59:58 +00001301 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001302 flags = pIn1->flags | pIn2->flags;
drha05a7222008-01-19 03:35:58 +00001303 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
1304 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
drh856c1032009-06-02 15:21:42 +00001305 iA = pIn1->u.i;
1306 iB = pIn2->u.i;
drhbe707b32012-12-10 22:19:14 +00001307 bIntint = 1;
drh5e00f6c2001-09-13 13:46:56 +00001308 switch( pOp->opcode ){
drh158b9cb2011-03-05 20:59:46 +00001309 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break;
1310 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break;
1311 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break;
drhbf4133c2001-10-13 02:59:08 +00001312 case OP_Divide: {
drh856c1032009-06-02 15:21:42 +00001313 if( iA==0 ) goto arithmetic_result_is_null;
drh158b9cb2011-03-05 20:59:46 +00001314 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
drh856c1032009-06-02 15:21:42 +00001315 iB /= iA;
drh75897232000-05-29 14:26:00 +00001316 break;
1317 }
drhbf4133c2001-10-13 02:59:08 +00001318 default: {
drh856c1032009-06-02 15:21:42 +00001319 if( iA==0 ) goto arithmetic_result_is_null;
1320 if( iA==-1 ) iA = 1;
1321 iB %= iA;
drhbf4133c2001-10-13 02:59:08 +00001322 break;
1323 }
drh75897232000-05-29 14:26:00 +00001324 }
drh856c1032009-06-02 15:21:42 +00001325 pOut->u.i = iB;
danielk1977a7a8e142008-02-13 18:25:27 +00001326 MemSetTypeFlag(pOut, MEM_Int);
drh5e00f6c2001-09-13 13:46:56 +00001327 }else{
drhbe707b32012-12-10 22:19:14 +00001328 bIntint = 0;
drh158b9cb2011-03-05 20:59:46 +00001329fp_math:
drh856c1032009-06-02 15:21:42 +00001330 rA = sqlite3VdbeRealValue(pIn1);
1331 rB = sqlite3VdbeRealValue(pIn2);
drh5e00f6c2001-09-13 13:46:56 +00001332 switch( pOp->opcode ){
drh856c1032009-06-02 15:21:42 +00001333 case OP_Add: rB += rA; break;
1334 case OP_Subtract: rB -= rA; break;
1335 case OP_Multiply: rB *= rA; break;
drhbf4133c2001-10-13 02:59:08 +00001336 case OP_Divide: {
shanefbd60f82009-02-04 03:59:25 +00001337 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
drh856c1032009-06-02 15:21:42 +00001338 if( rA==(double)0 ) goto arithmetic_result_is_null;
1339 rB /= rA;
drh5e00f6c2001-09-13 13:46:56 +00001340 break;
1341 }
drhbf4133c2001-10-13 02:59:08 +00001342 default: {
shane75ac1de2009-06-09 18:58:52 +00001343 iA = (i64)rA;
1344 iB = (i64)rB;
drh856c1032009-06-02 15:21:42 +00001345 if( iA==0 ) goto arithmetic_result_is_null;
1346 if( iA==-1 ) iA = 1;
1347 rB = (double)(iB % iA);
drhbf4133c2001-10-13 02:59:08 +00001348 break;
1349 }
drh5e00f6c2001-09-13 13:46:56 +00001350 }
drhc5a7b512010-01-13 16:25:42 +00001351#ifdef SQLITE_OMIT_FLOATING_POINT
1352 pOut->u.i = rB;
1353 MemSetTypeFlag(pOut, MEM_Int);
1354#else
drh856c1032009-06-02 15:21:42 +00001355 if( sqlite3IsNaN(rB) ){
drha05a7222008-01-19 03:35:58 +00001356 goto arithmetic_result_is_null;
drh53c14022007-05-10 17:23:11 +00001357 }
drh856c1032009-06-02 15:21:42 +00001358 pOut->r = rB;
danielk1977a7a8e142008-02-13 18:25:27 +00001359 MemSetTypeFlag(pOut, MEM_Real);
drhbe707b32012-12-10 22:19:14 +00001360 if( (flags & MEM_Real)==0 && !bIntint ){
drh5b6afba2008-01-05 16:29:28 +00001361 sqlite3VdbeIntegerAffinity(pOut);
drh8a512562005-11-14 22:29:05 +00001362 }
drhc5a7b512010-01-13 16:25:42 +00001363#endif
drh5e00f6c2001-09-13 13:46:56 +00001364 }
1365 break;
1366
drha05a7222008-01-19 03:35:58 +00001367arithmetic_result_is_null:
1368 sqlite3VdbeMemSetNull(pOut);
drh5e00f6c2001-09-13 13:46:56 +00001369 break;
1370}
1371
drh7a957892012-02-02 17:35:43 +00001372/* Opcode: CollSeq P1 * * P4
danielk1977dc1bdc42004-06-11 10:51:27 +00001373**
drh66a51672008-01-03 00:01:23 +00001374** P4 is a pointer to a CollSeq struct. If the next call to a user function
danielk1977dc1bdc42004-06-11 10:51:27 +00001375** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1376** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001377** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001378**
drh7a957892012-02-02 17:35:43 +00001379** If P1 is not zero, then it is a register that a subsequent min() or
1380** max() aggregate will set to 1 if the current row is not the minimum or
1381** maximum. The P1 register is initialized to 0 by this instruction.
1382**
danielk1977dc1bdc42004-06-11 10:51:27 +00001383** The interface used by the implementation of the aforementioned functions
1384** to retrieve the collation sequence set by this opcode is not available
1385** publicly, only to user functions defined in func.c.
1386*/
drh9cbf3422008-01-17 16:22:13 +00001387case OP_CollSeq: {
drh66a51672008-01-03 00:01:23 +00001388 assert( pOp->p4type==P4_COLLSEQ );
drh7a957892012-02-02 17:35:43 +00001389 if( pOp->p1 ){
1390 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
1391 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001392 break;
1393}
1394
drh98757152008-01-09 23:04:12 +00001395/* Opcode: Function P1 P2 P3 P4 P5
drh8e0a2f92002-02-23 23:45:45 +00001396**
drh66a51672008-01-03 00:01:23 +00001397** Invoke a user function (P4 is a pointer to a Function structure that
drh98757152008-01-09 23:04:12 +00001398** defines the function) with P5 arguments taken from register P2 and
drh9cbf3422008-01-17 16:22:13 +00001399** successors. The result of the function is stored in register P3.
danielk1977a7a8e142008-02-13 18:25:27 +00001400** Register P3 must not be one of the function inputs.
danielk1977682f68b2004-06-05 10:22:17 +00001401**
drh13449892005-09-07 21:22:45 +00001402** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001403** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001404** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001405** whether meta data associated with a user function argument using the
1406** sqlite3_set_auxdata() API may be safely retained until the next
1407** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001408**
drh13449892005-09-07 21:22:45 +00001409** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001410*/
drh0bce8352002-02-28 00:41:10 +00001411case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001412 int i;
drh6810ce62004-01-31 19:22:56 +00001413 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001414 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001415 sqlite3_value **apVal;
drh856c1032009-06-02 15:21:42 +00001416 int n;
drh1350b032002-02-27 19:00:20 +00001417
drh856c1032009-06-02 15:21:42 +00001418 n = pOp->p5;
danielk19776ddcca52004-05-24 23:48:25 +00001419 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001420 assert( apVal || n==0 );
drhebc16712010-09-28 00:25:58 +00001421 assert( pOp->p3>0 && pOp->p3<=p->nMem );
1422 pOut = &aMem[pOp->p3];
1423 memAboutToChange(p, pOut);
danielk197751ad0ec2004-05-24 12:39:02 +00001424
danielk19776ab3a2e2009-02-19 14:39:25 +00001425 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=p->nMem+1) );
danielk1977a7a8e142008-02-13 18:25:27 +00001426 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
drha6c2ed92009-11-14 23:22:23 +00001427 pArg = &aMem[pOp->p2];
drh6810ce62004-01-31 19:22:56 +00001428 for(i=0; i<n; i++, pArg++){
drh2b4ded92010-09-27 21:09:31 +00001429 assert( memIsValid(pArg) );
danielk197751ad0ec2004-05-24 12:39:02 +00001430 apVal[i] = pArg;
drhebc16712010-09-28 00:25:58 +00001431 Deephemeralize(pArg);
dan937d0de2009-10-15 18:35:38 +00001432 sqlite3VdbeMemStoreType(pArg);
drhab5cd702010-04-07 14:32:11 +00001433 REGISTER_TRACE(pOp->p2+i, pArg);
drh8e0a2f92002-02-23 23:45:45 +00001434 }
danielk197751ad0ec2004-05-24 12:39:02 +00001435
dan0c547792013-07-18 17:12:08 +00001436 assert( pOp->p4type==P4_FUNCDEF );
1437 ctx.pFunc = pOp->p4.pFunc;
drh00706be2004-01-30 14:49:16 +00001438 ctx.s.flags = MEM_Null;
drhfa4a4b92008-03-19 21:45:51 +00001439 ctx.s.db = db;
danielk19775f096132008-03-28 15:44:09 +00001440 ctx.s.xDel = 0;
1441 ctx.s.zMalloc = 0;
dan0c547792013-07-18 17:12:08 +00001442 ctx.iOp = pc;
1443 ctx.pVdbe = p;
danielk1977a7a8e142008-02-13 18:25:27 +00001444
1445 /* The output cell may already have a buffer allocated. Move
1446 ** the pointer to ctx.s so in case the user-function can use
1447 ** the already allocated buffer instead of allocating a new one.
1448 */
1449 sqlite3VdbeMemMove(&ctx.s, pOut);
1450 MemSetTypeFlag(&ctx.s, MEM_Null);
1451
drh9b47ee32013-08-20 03:13:51 +00001452 ctx.fErrorOrAux = 0;
drhe82f5d02008-10-07 19:53:14 +00001453 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
drhbbe879d2009-11-14 18:04:35 +00001454 assert( pOp>aOp );
drh66a51672008-01-03 00:01:23 +00001455 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00001456 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00001457 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00001458 }
drh99a66922011-05-13 18:51:42 +00001459 db->lastRowid = lastRowid;
drhee9ff672010-09-03 18:50:48 +00001460 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh99a66922011-05-13 18:51:42 +00001461 lastRowid = db->lastRowid;
danielk19777e18c252004-05-25 11:47:24 +00001462
dan5f84e142011-06-14 14:18:45 +00001463 if( db->mallocFailed ){
1464 /* Even though a malloc() has failed, the implementation of the
1465 ** user function may have called an sqlite3_result_XXX() function
1466 ** to return a value. The following call releases any resources
1467 ** associated with such a value.
1468 */
1469 sqlite3VdbeMemRelease(&ctx.s);
1470 goto no_mem;
1471 }
1472
drh90669c12006-01-20 15:45:36 +00001473 /* If the function returned an error, throw an exception */
drh9b47ee32013-08-20 03:13:51 +00001474 if( ctx.fErrorOrAux ){
1475 if( ctx.isError ){
1476 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
1477 rc = ctx.isError;
1478 }
1479 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
drh90669c12006-01-20 15:45:36 +00001480 }
1481
drh9cbf3422008-01-17 16:22:13 +00001482 /* Copy the result of the function into register P3 */
drhb21c8cd2007-08-21 19:33:56 +00001483 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drh98757152008-01-09 23:04:12 +00001484 sqlite3VdbeMemMove(pOut, &ctx.s);
1485 if( sqlite3VdbeMemTooBig(pOut) ){
drh023ae032007-05-08 12:12:16 +00001486 goto too_big;
1487 }
drh7b94e7f2011-04-04 12:29:20 +00001488
1489#if 0
1490 /* The app-defined function has done something that as caused this
1491 ** statement to expire. (Perhaps the function called sqlite3_exec()
1492 ** with a CREATE TABLE statement.)
1493 */
1494 if( p->expired ) rc = SQLITE_ABORT;
1495#endif
1496
drh2dcef112008-01-12 19:03:48 +00001497 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00001498 UPDATE_MAX_BLOBSIZE(pOut);
drh8e0a2f92002-02-23 23:45:45 +00001499 break;
1500}
1501
drh98757152008-01-09 23:04:12 +00001502/* Opcode: BitAnd P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001503**
drh98757152008-01-09 23:04:12 +00001504** Take the bit-wise AND of the values in register P1 and P2 and
1505** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001506** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001507*/
drh98757152008-01-09 23:04:12 +00001508/* Opcode: BitOr P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001509**
drh98757152008-01-09 23:04:12 +00001510** Take the bit-wise OR of the values in register P1 and P2 and
1511** store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001512** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001513*/
drh98757152008-01-09 23:04:12 +00001514/* Opcode: ShiftLeft P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001515**
drh98757152008-01-09 23:04:12 +00001516** Shift the integer value in register P2 to the left by the
drh710c4842010-08-30 01:17:20 +00001517** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001518** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001519** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001520*/
drh98757152008-01-09 23:04:12 +00001521/* Opcode: ShiftRight P1 P2 P3 * *
drhbf4133c2001-10-13 02:59:08 +00001522**
drh98757152008-01-09 23:04:12 +00001523** Shift the integer value in register P2 to the right by the
drh60a713c2008-01-21 16:22:45 +00001524** number of bits specified by the integer in register P1.
drh98757152008-01-09 23:04:12 +00001525** Store the result in register P3.
drh60a713c2008-01-21 16:22:45 +00001526** If either input is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001527*/
drh5b6afba2008-01-05 16:29:28 +00001528case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
1529case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
1530case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
1531case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
drh158b9cb2011-03-05 20:59:46 +00001532 i64 iA;
1533 u64 uA;
1534 i64 iB;
1535 u8 op;
drh6810ce62004-01-31 19:22:56 +00001536
drh3c657212009-11-17 23:59:58 +00001537 pIn1 = &aMem[pOp->p1];
1538 pIn2 = &aMem[pOp->p2];
1539 pOut = &aMem[pOp->p3];
drh5b6afba2008-01-05 16:29:28 +00001540 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
drha05a7222008-01-19 03:35:58 +00001541 sqlite3VdbeMemSetNull(pOut);
drhf5905aa2002-05-26 20:54:33 +00001542 break;
1543 }
drh158b9cb2011-03-05 20:59:46 +00001544 iA = sqlite3VdbeIntValue(pIn2);
1545 iB = sqlite3VdbeIntValue(pIn1);
1546 op = pOp->opcode;
1547 if( op==OP_BitAnd ){
1548 iA &= iB;
1549 }else if( op==OP_BitOr ){
1550 iA |= iB;
1551 }else if( iB!=0 ){
1552 assert( op==OP_ShiftRight || op==OP_ShiftLeft );
1553
1554 /* If shifting by a negative amount, shift in the other direction */
1555 if( iB<0 ){
1556 assert( OP_ShiftRight==OP_ShiftLeft+1 );
1557 op = 2*OP_ShiftLeft + 1 - op;
1558 iB = iB>(-64) ? -iB : 64;
1559 }
1560
1561 if( iB>=64 ){
1562 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
1563 }else{
1564 memcpy(&uA, &iA, sizeof(uA));
1565 if( op==OP_ShiftLeft ){
1566 uA <<= iB;
1567 }else{
1568 uA >>= iB;
1569 /* Sign-extend on a right shift of a negative number */
1570 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
1571 }
1572 memcpy(&iA, &uA, sizeof(iA));
1573 }
drhbf4133c2001-10-13 02:59:08 +00001574 }
drh158b9cb2011-03-05 20:59:46 +00001575 pOut->u.i = iA;
danielk1977a7a8e142008-02-13 18:25:27 +00001576 MemSetTypeFlag(pOut, MEM_Int);
drhbf4133c2001-10-13 02:59:08 +00001577 break;
1578}
1579
drh8558cde2008-01-05 05:20:10 +00001580/* Opcode: AddImm P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00001581**
danielk19770cdc0222008-06-26 18:04:03 +00001582** Add the constant P2 to the value in register P1.
drh8558cde2008-01-05 05:20:10 +00001583** The result is always an integer.
drh4a324312001-12-21 14:30:42 +00001584**
drh8558cde2008-01-05 05:20:10 +00001585** To force any register to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001586*/
drh9cbf3422008-01-17 16:22:13 +00001587case OP_AddImm: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001588 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001589 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001590 sqlite3VdbeMemIntegerify(pIn1);
1591 pIn1->u.i += pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00001592 break;
1593}
1594
drh9cbf3422008-01-17 16:22:13 +00001595/* Opcode: MustBeInt P1 P2 * * *
drh8aff1012001-12-22 14:49:24 +00001596**
drh9cbf3422008-01-17 16:22:13 +00001597** Force the value in register P1 to be an integer. If the value
1598** in P1 is not an integer and cannot be converted into an integer
danielk19779a96b662007-11-29 17:05:18 +00001599** without data loss, then jump immediately to P2, or if P2==0
drh8aff1012001-12-22 14:49:24 +00001600** raise an SQLITE_MISMATCH exception.
1601*/
drh9cbf3422008-01-17 16:22:13 +00001602case OP_MustBeInt: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00001603 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00001604 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
1605 if( (pIn1->flags & MEM_Int)==0 ){
drh17c40292004-07-21 02:53:29 +00001606 if( pOp->p2==0 ){
1607 rc = SQLITE_MISMATCH;
1608 goto abort_due_to_error;
drh3c84ddf2008-01-09 02:15:38 +00001609 }else{
drh17c40292004-07-21 02:53:29 +00001610 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001611 }
drh8aff1012001-12-22 14:49:24 +00001612 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00001613 MemSetTypeFlag(pIn1, MEM_Int);
drh8aff1012001-12-22 14:49:24 +00001614 }
1615 break;
1616}
1617
drh13573c72010-01-12 17:04:07 +00001618#ifndef SQLITE_OMIT_FLOATING_POINT
drh8558cde2008-01-05 05:20:10 +00001619/* Opcode: RealAffinity P1 * * * *
drh487e2622005-06-25 18:42:14 +00001620**
drh2133d822008-01-03 18:44:59 +00001621** If register P1 holds an integer convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001622**
drh8a512562005-11-14 22:29:05 +00001623** This opcode is used when extracting information from a column that
1624** has REAL affinity. Such column values may still be stored as
1625** integers, for space efficiency, but after extraction we want them
1626** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001627*/
drh9cbf3422008-01-17 16:22:13 +00001628case OP_RealAffinity: { /* in1 */
drh3c657212009-11-17 23:59:58 +00001629 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001630 if( pIn1->flags & MEM_Int ){
1631 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001632 }
drh487e2622005-06-25 18:42:14 +00001633 break;
1634}
drh13573c72010-01-12 17:04:07 +00001635#endif
drh487e2622005-06-25 18:42:14 +00001636
drh8df447f2005-11-01 15:48:24 +00001637#ifndef SQLITE_OMIT_CAST
drh8558cde2008-01-05 05:20:10 +00001638/* Opcode: ToText P1 * * * *
drh487e2622005-06-25 18:42:14 +00001639**
drh8558cde2008-01-05 05:20:10 +00001640** Force the value in register P1 to be text.
drh31beae92005-11-24 14:34:36 +00001641** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001642** equivalent of printf(). Blob values are unchanged and
1643** are afterwards simply interpreted as text.
1644**
1645** A NULL value is not changed by this routine. It remains NULL.
1646*/
drh9cbf3422008-01-17 16:22:13 +00001647case OP_ToText: { /* same as TK_TO_TEXT, in1 */
drh3c657212009-11-17 23:59:58 +00001648 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001649 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001650 if( pIn1->flags & MEM_Null ) break;
drh487e2622005-06-25 18:42:14 +00001651 assert( MEM_Str==(MEM_Blob>>3) );
drh8558cde2008-01-05 05:20:10 +00001652 pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
1653 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
1654 rc = ExpandBlob(pIn1);
danielk1977a7a8e142008-02-13 18:25:27 +00001655 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drh68ac65e2009-01-05 18:02:27 +00001656 pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
drhb7654112008-01-12 12:48:07 +00001657 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001658 break;
1659}
1660
drh8558cde2008-01-05 05:20:10 +00001661/* Opcode: ToBlob P1 * * * *
drh487e2622005-06-25 18:42:14 +00001662**
drh8558cde2008-01-05 05:20:10 +00001663** Force the value in register P1 to be a BLOB.
drh487e2622005-06-25 18:42:14 +00001664** If the value is numeric, convert it to a string first.
1665** Strings are simply reinterpreted as blobs with no change
1666** to the underlying data.
1667**
1668** A NULL value is not changed by this routine. It remains NULL.
1669*/
drh9cbf3422008-01-17 16:22:13 +00001670case OP_ToBlob: { /* same as TK_TO_BLOB, in1 */
drh3c657212009-11-17 23:59:58 +00001671 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001672 if( pIn1->flags & MEM_Null ) break;
1673 if( (pIn1->flags & MEM_Blob)==0 ){
1674 applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
danielk1977a7a8e142008-02-13 18:25:27 +00001675 assert( pIn1->flags & MEM_Str || db->mallocFailed );
drhde58ddb2009-01-05 22:30:38 +00001676 MemSetTypeFlag(pIn1, MEM_Blob);
1677 }else{
1678 pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
drh487e2622005-06-25 18:42:14 +00001679 }
drhb7654112008-01-12 12:48:07 +00001680 UPDATE_MAX_BLOBSIZE(pIn1);
drh487e2622005-06-25 18:42:14 +00001681 break;
1682}
drh8a512562005-11-14 22:29:05 +00001683
drh8558cde2008-01-05 05:20:10 +00001684/* Opcode: ToNumeric P1 * * * *
drh8a512562005-11-14 22:29:05 +00001685**
drh8558cde2008-01-05 05:20:10 +00001686** Force the value in register P1 to be numeric (either an
drh8a512562005-11-14 22:29:05 +00001687** integer or a floating-point number.)
1688** If the value is text or blob, try to convert it to an using the
1689** equivalent of atoi() or atof() and store 0 if no such conversion
1690** is possible.
1691**
1692** A NULL value is not changed by this routine. It remains NULL.
1693*/
drh9cbf3422008-01-17 16:22:13 +00001694case OP_ToNumeric: { /* same as TK_TO_NUMERIC, in1 */
drh3c657212009-11-17 23:59:58 +00001695 pIn1 = &aMem[pOp->p1];
drh93518622010-09-30 14:48:06 +00001696 sqlite3VdbeMemNumerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001697 break;
1698}
1699#endif /* SQLITE_OMIT_CAST */
1700
drh8558cde2008-01-05 05:20:10 +00001701/* Opcode: ToInt P1 * * * *
drh8a512562005-11-14 22:29:05 +00001702**
drh710c4842010-08-30 01:17:20 +00001703** Force the value in register P1 to be an integer. If
drh8a512562005-11-14 22:29:05 +00001704** The value is currently a real number, drop its fractional part.
1705** If the value is text or blob, try to convert it to an integer using the
1706** equivalent of atoi() and store 0 if no such conversion is possible.
1707**
1708** A NULL value is not changed by this routine. It remains NULL.
1709*/
drh9cbf3422008-01-17 16:22:13 +00001710case OP_ToInt: { /* same as TK_TO_INT, in1 */
drh3c657212009-11-17 23:59:58 +00001711 pIn1 = &aMem[pOp->p1];
drh8558cde2008-01-05 05:20:10 +00001712 if( (pIn1->flags & MEM_Null)==0 ){
1713 sqlite3VdbeMemIntegerify(pIn1);
drh8a512562005-11-14 22:29:05 +00001714 }
1715 break;
1716}
1717
drh13573c72010-01-12 17:04:07 +00001718#if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
drh8558cde2008-01-05 05:20:10 +00001719/* Opcode: ToReal P1 * * * *
drh8a512562005-11-14 22:29:05 +00001720**
drh8558cde2008-01-05 05:20:10 +00001721** Force the value in register P1 to be a floating point number.
drh8a512562005-11-14 22:29:05 +00001722** If The value is currently an integer, convert it.
1723** If the value is text or blob, try to convert it to an integer using the
drh60a713c2008-01-21 16:22:45 +00001724** equivalent of atoi() and store 0.0 if no such conversion is possible.
drh8a512562005-11-14 22:29:05 +00001725**
1726** A NULL value is not changed by this routine. It remains NULL.
1727*/
drh9cbf3422008-01-17 16:22:13 +00001728case OP_ToReal: { /* same as TK_TO_REAL, in1 */
drh3c657212009-11-17 23:59:58 +00001729 pIn1 = &aMem[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00001730 memAboutToChange(p, pIn1);
drh8558cde2008-01-05 05:20:10 +00001731 if( (pIn1->flags & MEM_Null)==0 ){
1732 sqlite3VdbeMemRealify(pIn1);
drh8a512562005-11-14 22:29:05 +00001733 }
1734 break;
1735}
drh13573c72010-01-12 17:04:07 +00001736#endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
drh487e2622005-06-25 18:42:14 +00001737
drh35573352008-01-08 23:54:25 +00001738/* Opcode: Lt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001739**
drh35573352008-01-08 23:54:25 +00001740** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then
1741** jump to address P2.
drhf5905aa2002-05-26 20:54:33 +00001742**
drh35573352008-01-08 23:54:25 +00001743** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
1744** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL
drh710c4842010-08-30 01:17:20 +00001745** bit is clear then fall through if either operand is NULL.
drh4f686232005-09-20 13:55:18 +00001746**
drh35573352008-01-08 23:54:25 +00001747** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001748** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
drh60a713c2008-01-21 16:22:45 +00001749** to coerce both inputs according to this affinity before the
drh35573352008-01-08 23:54:25 +00001750** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
drh60a713c2008-01-21 16:22:45 +00001751** affinity is used. Note that the affinity conversions are stored
1752** back into the input registers P1 and P3. So this opcode can cause
1753** persistent changes to registers P1 and P3.
danielk1977a37cdde2004-05-16 11:15:36 +00001754**
1755** Once any conversions have taken place, and neither value is NULL,
drh35573352008-01-08 23:54:25 +00001756** the values are compared. If both values are blobs then memcmp() is
1757** used to determine the results of the comparison. If both values
1758** are text, then the appropriate collating function specified in
1759** P4 is used to do the comparison. If P4 is not specified then
1760** memcmp() is used to compare text string. If both values are
1761** numeric, then a numeric comparison is used. If the two values
1762** are of different types, then numbers are considered less than
1763** strings and strings are considered less than blobs.
drhc9b84a12002-06-20 11:36:48 +00001764**
drh35573352008-01-08 23:54:25 +00001765** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead,
1766** store a boolean result (either 0, or 1, or NULL) in register P2.
drh053a1282012-09-19 21:15:46 +00001767**
1768** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
1769** equal to one another, provided that they do not have their MEM_Cleared
1770** bit set.
drh5e00f6c2001-09-13 13:46:56 +00001771*/
drh9cbf3422008-01-17 16:22:13 +00001772/* Opcode: Ne P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001773**
drh35573352008-01-08 23:54:25 +00001774** This works just like the Lt opcode except that the jump is taken if
1775** the operands in registers P1 and P3 are not equal. See the Lt opcode for
drh53db1452004-05-20 13:54:53 +00001776** additional information.
drh6a2fe092009-09-23 02:29:36 +00001777**
1778** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1779** true or false and is never NULL. If both operands are NULL then the result
1780** of comparison is false. If either operand is NULL then the result is true.
drhef8662b2011-06-20 21:47:58 +00001781** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001782** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001783*/
drh9cbf3422008-01-17 16:22:13 +00001784/* Opcode: Eq P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001785**
drh35573352008-01-08 23:54:25 +00001786** This works just like the Lt opcode except that the jump is taken if
1787** the operands in registers P1 and P3 are equal.
1788** See the Lt opcode for additional information.
drh6a2fe092009-09-23 02:29:36 +00001789**
1790** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
1791** true or false and is never NULL. If both operands are NULL then the result
1792** of comparison is true. If either operand is NULL then the result is false.
drhef8662b2011-06-20 21:47:58 +00001793** If neither operand is NULL the result is the same as it would be if
drh6a2fe092009-09-23 02:29:36 +00001794** the SQLITE_NULLEQ flag were omitted from P5.
drh5e00f6c2001-09-13 13:46:56 +00001795*/
drh9cbf3422008-01-17 16:22:13 +00001796/* Opcode: Le P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001797**
drh35573352008-01-08 23:54:25 +00001798** This works just like the Lt opcode except that the jump is taken if
1799** the content of register P3 is less than or equal to the content of
1800** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001801*/
drh9cbf3422008-01-17 16:22:13 +00001802/* Opcode: Gt P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001803**
drh35573352008-01-08 23:54:25 +00001804** This works just like the Lt opcode except that the jump is taken if
1805** the content of register P3 is greater than the content of
1806** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001807*/
drh9cbf3422008-01-17 16:22:13 +00001808/* Opcode: Ge P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00001809**
drh35573352008-01-08 23:54:25 +00001810** This works just like the Lt opcode except that the jump is taken if
1811** the content of register P3 is greater than or equal to the content of
1812** register P1. See the Lt opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001813*/
drh9cbf3422008-01-17 16:22:13 +00001814case OP_Eq: /* same as TK_EQ, jump, in1, in3 */
1815case OP_Ne: /* same as TK_NE, jump, in1, in3 */
1816case OP_Lt: /* same as TK_LT, jump, in1, in3 */
1817case OP_Le: /* same as TK_LE, jump, in1, in3 */
1818case OP_Gt: /* same as TK_GT, jump, in1, in3 */
1819case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
drh6a2fe092009-09-23 02:29:36 +00001820 int res; /* Result of the comparison of pIn1 against pIn3 */
1821 char affinity; /* Affinity to use for comparison */
danb7dca7d2010-03-05 16:32:12 +00001822 u16 flags1; /* Copy of initial value of pIn1->flags */
1823 u16 flags3; /* Copy of initial value of pIn3->flags */
danielk1977a37cdde2004-05-16 11:15:36 +00001824
drh3c657212009-11-17 23:59:58 +00001825 pIn1 = &aMem[pOp->p1];
1826 pIn3 = &aMem[pOp->p3];
danb7dca7d2010-03-05 16:32:12 +00001827 flags1 = pIn1->flags;
1828 flags3 = pIn3->flags;
drhc3f1d5f2011-05-30 23:42:16 +00001829 if( (flags1 | flags3)&MEM_Null ){
drh6a2fe092009-09-23 02:29:36 +00001830 /* One or both operands are NULL */
1831 if( pOp->p5 & SQLITE_NULLEQ ){
1832 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
1833 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
1834 ** or not both operands are null.
1835 */
1836 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
drh053a1282012-09-19 21:15:46 +00001837 assert( (flags1 & MEM_Cleared)==0 );
1838 if( (flags1&MEM_Null)!=0
1839 && (flags3&MEM_Null)!=0
1840 && (flags3&MEM_Cleared)==0
1841 ){
1842 res = 0; /* Results are equal */
1843 }else{
1844 res = 1; /* Results are not equal */
1845 }
drh6a2fe092009-09-23 02:29:36 +00001846 }else{
1847 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
1848 ** then the result is always NULL.
1849 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
1850 */
drh9b47ee32013-08-20 03:13:51 +00001851 if( pOp->p5 & SQLITE_JUMPIFNULL ){
1852 pc = pOp->p2-1;
1853 }else if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001854 pOut = &aMem[pOp->p2];
drh6a2fe092009-09-23 02:29:36 +00001855 MemSetTypeFlag(pOut, MEM_Null);
1856 REGISTER_TRACE(pOp->p2, pOut);
drh6a2fe092009-09-23 02:29:36 +00001857 }
1858 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001859 }
drh6a2fe092009-09-23 02:29:36 +00001860 }else{
1861 /* Neither operand is NULL. Do a comparison. */
1862 affinity = pOp->p5 & SQLITE_AFF_MASK;
1863 if( affinity ){
1864 applyAffinity(pIn1, affinity, encoding);
1865 applyAffinity(pIn3, affinity, encoding);
1866 if( db->mallocFailed ) goto no_mem;
1867 }
danielk1977a37cdde2004-05-16 11:15:36 +00001868
drh6a2fe092009-09-23 02:29:36 +00001869 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
1870 ExpandBlob(pIn1);
1871 ExpandBlob(pIn3);
1872 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
drhe51c44f2004-05-30 20:46:09 +00001873 }
danielk1977a37cdde2004-05-16 11:15:36 +00001874 switch( pOp->opcode ){
1875 case OP_Eq: res = res==0; break;
1876 case OP_Ne: res = res!=0; break;
1877 case OP_Lt: res = res<0; break;
1878 case OP_Le: res = res<=0; break;
1879 case OP_Gt: res = res>0; break;
1880 default: res = res>=0; break;
1881 }
1882
drh35573352008-01-08 23:54:25 +00001883 if( pOp->p5 & SQLITE_STOREP2 ){
drha6c2ed92009-11-14 23:22:23 +00001884 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00001885 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00001886 MemSetTypeFlag(pOut, MEM_Int);
drh35573352008-01-08 23:54:25 +00001887 pOut->u.i = res;
1888 REGISTER_TRACE(pOp->p2, pOut);
1889 }else if( res ){
1890 pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001891 }
danb7dca7d2010-03-05 16:32:12 +00001892
1893 /* Undo any changes made by applyAffinity() to the input registers. */
1894 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
1895 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
danielk1977a37cdde2004-05-16 11:15:36 +00001896 break;
1897}
drhc9b84a12002-06-20 11:36:48 +00001898
drh0acb7e42008-06-25 00:12:41 +00001899/* Opcode: Permutation * * * P4 *
1900**
shanebe217792009-03-05 04:20:31 +00001901** Set the permutation used by the OP_Compare operator to be the array
drh0acb7e42008-06-25 00:12:41 +00001902** of integers in P4.
1903**
drh953f7612012-12-07 22:18:54 +00001904** The permutation is only valid until the next OP_Compare that has
1905** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
1906** occur immediately prior to the OP_Compare.
drh0acb7e42008-06-25 00:12:41 +00001907*/
1908case OP_Permutation: {
1909 assert( pOp->p4type==P4_INTARRAY );
1910 assert( pOp->p4.ai );
1911 aPermute = pOp->p4.ai;
1912 break;
1913}
1914
drh953f7612012-12-07 22:18:54 +00001915/* Opcode: Compare P1 P2 P3 P4 P5
drh16ee60f2008-06-20 18:13:25 +00001916**
drh710c4842010-08-30 01:17:20 +00001917** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
1918** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of
drh16ee60f2008-06-20 18:13:25 +00001919** the comparison for use by the next OP_Jump instruct.
1920**
drh0ca10df2012-12-08 13:26:23 +00001921** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
1922** determined by the most recent OP_Permutation operator. If the
1923** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
1924** order.
1925**
drh0acb7e42008-06-25 00:12:41 +00001926** P4 is a KeyInfo structure that defines collating sequences and sort
1927** orders for the comparison. The permutation applies to registers
1928** only. The KeyInfo elements are used sequentially.
1929**
1930** The comparison is a sort comparison, so NULLs compare equal,
1931** NULLs are less than numbers, numbers are less than strings,
drh16ee60f2008-06-20 18:13:25 +00001932** and strings are less than blobs.
1933*/
1934case OP_Compare: {
drh856c1032009-06-02 15:21:42 +00001935 int n;
1936 int i;
1937 int p1;
1938 int p2;
1939 const KeyInfo *pKeyInfo;
1940 int idx;
1941 CollSeq *pColl; /* Collating sequence to use on this term */
1942 int bRev; /* True for DESCENDING sort order */
1943
drh953f7612012-12-07 22:18:54 +00001944 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
drh856c1032009-06-02 15:21:42 +00001945 n = pOp->p3;
1946 pKeyInfo = pOp->p4.pKeyInfo;
drh16ee60f2008-06-20 18:13:25 +00001947 assert( n>0 );
drh93a960a2008-07-10 00:32:42 +00001948 assert( pKeyInfo!=0 );
drh16ee60f2008-06-20 18:13:25 +00001949 p1 = pOp->p1;
drh16ee60f2008-06-20 18:13:25 +00001950 p2 = pOp->p2;
drh6a2fe092009-09-23 02:29:36 +00001951#if SQLITE_DEBUG
1952 if( aPermute ){
1953 int k, mx = 0;
1954 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
1955 assert( p1>0 && p1+mx<=p->nMem+1 );
1956 assert( p2>0 && p2+mx<=p->nMem+1 );
1957 }else{
1958 assert( p1>0 && p1+n<=p->nMem+1 );
1959 assert( p2>0 && p2+n<=p->nMem+1 );
1960 }
1961#endif /* SQLITE_DEBUG */
drh0acb7e42008-06-25 00:12:41 +00001962 for(i=0; i<n; i++){
drh856c1032009-06-02 15:21:42 +00001963 idx = aPermute ? aPermute[i] : i;
drh2b4ded92010-09-27 21:09:31 +00001964 assert( memIsValid(&aMem[p1+idx]) );
1965 assert( memIsValid(&aMem[p2+idx]) );
drha6c2ed92009-11-14 23:22:23 +00001966 REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
1967 REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
drh93a960a2008-07-10 00:32:42 +00001968 assert( i<pKeyInfo->nField );
1969 pColl = pKeyInfo->aColl[i];
1970 bRev = pKeyInfo->aSortOrder[i];
drha6c2ed92009-11-14 23:22:23 +00001971 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
drh0acb7e42008-06-25 00:12:41 +00001972 if( iCompare ){
1973 if( bRev ) iCompare = -iCompare;
1974 break;
1975 }
drh16ee60f2008-06-20 18:13:25 +00001976 }
drh0acb7e42008-06-25 00:12:41 +00001977 aPermute = 0;
drh16ee60f2008-06-20 18:13:25 +00001978 break;
1979}
1980
1981/* Opcode: Jump P1 P2 P3 * *
1982**
1983** Jump to the instruction at address P1, P2, or P3 depending on whether
1984** in the most recent OP_Compare instruction the P1 vector was less than
1985** equal to, or greater than the P2 vector, respectively.
1986*/
drh0acb7e42008-06-25 00:12:41 +00001987case OP_Jump: { /* jump */
1988 if( iCompare<0 ){
drh16ee60f2008-06-20 18:13:25 +00001989 pc = pOp->p1 - 1;
drh0acb7e42008-06-25 00:12:41 +00001990 }else if( iCompare==0 ){
drh16ee60f2008-06-20 18:13:25 +00001991 pc = pOp->p2 - 1;
1992 }else{
1993 pc = pOp->p3 - 1;
1994 }
1995 break;
1996}
1997
drh5b6afba2008-01-05 16:29:28 +00001998/* Opcode: And P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00001999**
drh5b6afba2008-01-05 16:29:28 +00002000** Take the logical AND of the values in registers P1 and P2 and
2001** write the result into register P3.
drh5e00f6c2001-09-13 13:46:56 +00002002**
drh5b6afba2008-01-05 16:29:28 +00002003** If either P1 or P2 is 0 (false) then the result is 0 even if
2004** the other input is NULL. A NULL and true or two NULLs give
2005** a NULL output.
drh5e00f6c2001-09-13 13:46:56 +00002006*/
drh5b6afba2008-01-05 16:29:28 +00002007/* Opcode: Or P1 P2 P3 * *
2008**
2009** Take the logical OR of the values in register P1 and P2 and
2010** store the answer in register P3.
2011**
2012** If either P1 or P2 is nonzero (true) then the result is 1 (true)
2013** even if the other input is NULL. A NULL and false or two NULLs
2014** give a NULL output.
2015*/
2016case OP_And: /* same as TK_AND, in1, in2, out3 */
2017case OP_Or: { /* same as TK_OR, in1, in2, out3 */
drh856c1032009-06-02 15:21:42 +00002018 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
2019 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
drhbb113512002-05-27 01:04:51 +00002020
drh3c657212009-11-17 23:59:58 +00002021 pIn1 = &aMem[pOp->p1];
drh5b6afba2008-01-05 16:29:28 +00002022 if( pIn1->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002023 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00002024 }else{
drh5b6afba2008-01-05 16:29:28 +00002025 v1 = sqlite3VdbeIntValue(pIn1)!=0;
drhbb113512002-05-27 01:04:51 +00002026 }
drh3c657212009-11-17 23:59:58 +00002027 pIn2 = &aMem[pOp->p2];
drh5b6afba2008-01-05 16:29:28 +00002028 if( pIn2->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00002029 v2 = 2;
2030 }else{
drh5b6afba2008-01-05 16:29:28 +00002031 v2 = sqlite3VdbeIntValue(pIn2)!=0;
drhbb113512002-05-27 01:04:51 +00002032 }
2033 if( pOp->opcode==OP_And ){
drh5b6afba2008-01-05 16:29:28 +00002034 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
drhbb113512002-05-27 01:04:51 +00002035 v1 = and_logic[v1*3+v2];
2036 }else{
drh5b6afba2008-01-05 16:29:28 +00002037 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
drhbb113512002-05-27 01:04:51 +00002038 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00002039 }
drh3c657212009-11-17 23:59:58 +00002040 pOut = &aMem[pOp->p3];
drhbb113512002-05-27 01:04:51 +00002041 if( v1==2 ){
danielk1977a7a8e142008-02-13 18:25:27 +00002042 MemSetTypeFlag(pOut, MEM_Null);
drhbb113512002-05-27 01:04:51 +00002043 }else{
drh5b6afba2008-01-05 16:29:28 +00002044 pOut->u.i = v1;
danielk1977a7a8e142008-02-13 18:25:27 +00002045 MemSetTypeFlag(pOut, MEM_Int);
drhbb113512002-05-27 01:04:51 +00002046 }
drh5e00f6c2001-09-13 13:46:56 +00002047 break;
2048}
2049
drhe99fa2a2008-12-15 15:27:51 +00002050/* Opcode: Not P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002051**
drhe99fa2a2008-12-15 15:27:51 +00002052** Interpret the value in register P1 as a boolean value. Store the
2053** boolean complement in register P2. If the value in register P1 is
2054** NULL, then a NULL is stored in P2.
drh5e00f6c2001-09-13 13:46:56 +00002055*/
drh93952eb2009-11-13 19:43:43 +00002056case OP_Not: { /* same as TK_NOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002057 pIn1 = &aMem[pOp->p1];
2058 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002059 if( pIn1->flags & MEM_Null ){
2060 sqlite3VdbeMemSetNull(pOut);
2061 }else{
2062 sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
2063 }
drh5e00f6c2001-09-13 13:46:56 +00002064 break;
2065}
2066
drhe99fa2a2008-12-15 15:27:51 +00002067/* Opcode: BitNot P1 P2 * * *
drhbf4133c2001-10-13 02:59:08 +00002068**
drhe99fa2a2008-12-15 15:27:51 +00002069** Interpret the content of register P1 as an integer. Store the
2070** ones-complement of the P1 value into register P2. If P1 holds
2071** a NULL then store a NULL in P2.
drhbf4133c2001-10-13 02:59:08 +00002072*/
drh93952eb2009-11-13 19:43:43 +00002073case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
drh3c657212009-11-17 23:59:58 +00002074 pIn1 = &aMem[pOp->p1];
2075 pOut = &aMem[pOp->p2];
drhe99fa2a2008-12-15 15:27:51 +00002076 if( pIn1->flags & MEM_Null ){
2077 sqlite3VdbeMemSetNull(pOut);
2078 }else{
2079 sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
2080 }
drhbf4133c2001-10-13 02:59:08 +00002081 break;
2082}
2083
drh48f2d3b2011-09-16 01:34:43 +00002084/* Opcode: Once P1 P2 * * *
2085**
dan1d8cb212011-12-09 13:24:16 +00002086** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
2087** set the flag and fall through to the next instruction.
drh48f2d3b2011-09-16 01:34:43 +00002088*/
dan1d8cb212011-12-09 13:24:16 +00002089case OP_Once: { /* jump */
2090 assert( pOp->p1<p->nOnceFlag );
2091 if( p->aOnceFlag[pOp->p1] ){
2092 pc = pOp->p2-1;
2093 }else{
2094 p->aOnceFlag[pOp->p1] = 1;
2095 }
2096 break;
2097}
2098
drh3c84ddf2008-01-09 02:15:38 +00002099/* Opcode: If P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00002100**
drhef8662b2011-06-20 21:47:58 +00002101** Jump to P2 if the value in register P1 is true. The value
drh3c84ddf2008-01-09 02:15:38 +00002102** is considered true if it is numeric and non-zero. If the value
drhb8475df2011-12-09 16:21:19 +00002103** in P1 is NULL then take the jump if P3 is non-zero.
drh5e00f6c2001-09-13 13:46:56 +00002104*/
drh3c84ddf2008-01-09 02:15:38 +00002105/* Opcode: IfNot P1 P2 P3 * *
drhf5905aa2002-05-26 20:54:33 +00002106**
drhef8662b2011-06-20 21:47:58 +00002107** Jump to P2 if the value in register P1 is False. The value
drhb8475df2011-12-09 16:21:19 +00002108** is considered false if it has a numeric value of zero. If the value
2109** in P1 is NULL then take the jump if P3 is zero.
drhf5905aa2002-05-26 20:54:33 +00002110*/
drh9cbf3422008-01-17 16:22:13 +00002111case OP_If: /* jump, in1 */
2112case OP_IfNot: { /* jump, in1 */
drh5e00f6c2001-09-13 13:46:56 +00002113 int c;
drh3c657212009-11-17 23:59:58 +00002114 pIn1 = &aMem[pOp->p1];
drh3c84ddf2008-01-09 02:15:38 +00002115 if( pIn1->flags & MEM_Null ){
2116 c = pOp->p3;
drhf5905aa2002-05-26 20:54:33 +00002117 }else{
drhba0232a2005-06-06 17:27:19 +00002118#ifdef SQLITE_OMIT_FLOATING_POINT
shanefbd60f82009-02-04 03:59:25 +00002119 c = sqlite3VdbeIntValue(pIn1)!=0;
drhba0232a2005-06-06 17:27:19 +00002120#else
drh3c84ddf2008-01-09 02:15:38 +00002121 c = sqlite3VdbeRealValue(pIn1)!=0.0;
drhba0232a2005-06-06 17:27:19 +00002122#endif
drhf5905aa2002-05-26 20:54:33 +00002123 if( pOp->opcode==OP_IfNot ) c = !c;
2124 }
drh3c84ddf2008-01-09 02:15:38 +00002125 if( c ){
2126 pc = pOp->p2-1;
2127 }
drh5e00f6c2001-09-13 13:46:56 +00002128 break;
2129}
2130
drh830ecf92009-06-18 00:41:55 +00002131/* Opcode: IsNull P1 P2 * * *
drh477df4b2008-01-05 18:48:24 +00002132**
drh830ecf92009-06-18 00:41:55 +00002133** Jump to P2 if the value in register P1 is NULL.
drh477df4b2008-01-05 18:48:24 +00002134*/
drh9cbf3422008-01-17 16:22:13 +00002135case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002136 pIn1 = &aMem[pOp->p1];
drh830ecf92009-06-18 00:41:55 +00002137 if( (pIn1->flags & MEM_Null)!=0 ){
2138 pc = pOp->p2 - 1;
2139 }
drh477df4b2008-01-05 18:48:24 +00002140 break;
2141}
2142
drh98757152008-01-09 23:04:12 +00002143/* Opcode: NotNull P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002144**
drh6a288a32008-01-07 19:20:24 +00002145** Jump to P2 if the value in register P1 is not NULL.
drh5e00f6c2001-09-13 13:46:56 +00002146*/
drh9cbf3422008-01-17 16:22:13 +00002147case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */
drh3c657212009-11-17 23:59:58 +00002148 pIn1 = &aMem[pOp->p1];
drh6a288a32008-01-07 19:20:24 +00002149 if( (pIn1->flags & MEM_Null)==0 ){
2150 pc = pOp->p2 - 1;
2151 }
drh5e00f6c2001-09-13 13:46:56 +00002152 break;
2153}
2154
drh3e9ca092009-09-08 01:14:48 +00002155/* Opcode: Column P1 P2 P3 P4 P5
danielk1977192ac1d2004-05-10 07:17:30 +00002156**
danielk1977cfcdaef2004-05-12 07:33:33 +00002157** Interpret the data that cursor P1 points to as a structure built using
2158** the MakeRecord instruction. (See the MakeRecord opcode for additional
drhd4e70eb2008-01-02 00:34:36 +00002159** information about the format of the data.) Extract the P2-th column
2160** from this record. If there are less that (P2+1)
2161** values in the record, extract a NULL.
2162**
drh9cbf3422008-01-17 16:22:13 +00002163** The value extracted is stored in register P3.
danielk1977192ac1d2004-05-10 07:17:30 +00002164**
danielk19771f4aa332008-01-03 09:51:55 +00002165** If the column contains fewer than P2 fields, then extract a NULL. Or,
2166** if the P4 argument is a P4_MEM use the value of the P4 argument as
2167** the result.
drh3e9ca092009-09-08 01:14:48 +00002168**
2169** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
2170** then the cache of the cursor is reset prior to extracting the column.
2171** The first OP_Column against a pseudo-table after the value of the content
2172** register has changed should have this bit set.
drha748fdc2012-03-28 01:34:47 +00002173**
drhdda5c082012-03-28 13:41:10 +00002174** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
2175** the result is guaranteed to only be used as the argument of a length()
2176** or typeof() function, respectively. The loading of large blobs can be
2177** skipped for length() and all content loading can be skipped for typeof().
danielk1977192ac1d2004-05-10 07:17:30 +00002178*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002179case OP_Column: {
drh35cd6432009-06-05 14:17:21 +00002180 u32 payloadSize; /* Number of bytes in the record */
drh856c1032009-06-02 15:21:42 +00002181 i64 payloadSize64; /* Number of bytes in the record */
2182 int p1; /* P1 value of the opcode */
2183 int p2; /* column number to retrieve */
2184 VdbeCursor *pC; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00002185 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00002186 BtCursor *pCrsr; /* The BTree cursor */
2187 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
2188 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
danielk197764202cf2008-11-17 15:31:47 +00002189 int nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00002190 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00002191 int i; /* Loop counter */
2192 char *zData; /* Part of the record being decoded */
drhd4e70eb2008-01-02 00:34:36 +00002193 Mem *pDest; /* Where to write the extracted value */
drhd3194f52004-05-27 19:59:32 +00002194 Mem sMem; /* For storing the record being decoded */
drh35cd6432009-06-05 14:17:21 +00002195 u8 *zIdx; /* Index into header */
2196 u8 *zEndHdr; /* Pointer to first byte after the header */
2197 u32 offset; /* Offset into the data */
drh6658cd92010-02-05 14:12:53 +00002198 u32 szField; /* Number of bytes in the content of a field */
drh35cd6432009-06-05 14:17:21 +00002199 int szHdr; /* Size of the header size field at start of record */
2200 int avail; /* Number of bytes of available data */
drh5a077b72011-08-29 02:16:18 +00002201 u32 t; /* A type code from the record header */
drh3e9ca092009-09-08 01:14:48 +00002202 Mem *pReg; /* PseudoTable input register */
danielk1977192ac1d2004-05-10 07:17:30 +00002203
drh856c1032009-06-02 15:21:42 +00002204
2205 p1 = pOp->p1;
2206 p2 = pOp->p2;
2207 pC = 0;
drhb27b7f52008-12-10 18:03:45 +00002208 memset(&sMem, 0, sizeof(sMem));
drhd3194f52004-05-27 19:59:32 +00002209 assert( p1<p->nCursor );
drh9cbf3422008-01-17 16:22:13 +00002210 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00002211 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00002212 memAboutToChange(p, pDest);
shane36840fd2009-06-26 16:32:13 +00002213 zRec = 0;
danielk1977cfcdaef2004-05-12 07:33:33 +00002214
drhe61cffc2004-06-12 18:12:15 +00002215 /* This block sets the variable payloadSize to be the total number of
2216 ** bytes in the record.
2217 **
2218 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00002219 ** The complete record text is always available for pseudo-tables
2220 ** If the record is stored in a cursor, the complete record text
2221 ** might be available in the pC->aRow cache. Or it might not be.
2222 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00002223 **
2224 ** We also compute the number of columns in the record. For cursors,
drhdfe88ec2008-11-03 20:55:06 +00002225 ** the number of columns is stored in the VdbeCursor.nField element.
danielk1977cfcdaef2004-05-12 07:33:33 +00002226 */
drhb73857f2006-03-17 00:25:59 +00002227 pC = p->apCsr[p1];
drha5759672012-10-30 14:39:12 +00002228 assert( pC!=0 );
danielk19770817d0d2007-02-14 09:19:36 +00002229#ifndef SQLITE_OMIT_VIRTUALTABLE
2230 assert( pC->pVtabCursor==0 );
2231#endif
shane36840fd2009-06-26 16:32:13 +00002232 pCrsr = pC->pCursor;
2233 if( pCrsr!=0 ){
drhe61cffc2004-06-12 18:12:15 +00002234 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00002235 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00002236 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002237 if( pC->nullRow ){
2238 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00002239 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00002240 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00002241 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00002242 }else if( pC->isIndex ){
drhea8ffdf2009-07-22 00:35:23 +00002243 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002244 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
drhc27ae612009-07-14 18:35:44 +00002245 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhaa736092009-06-22 00:55:30 +00002246 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
2247 ** payload size, so it is impossible for payloadSize64 to be
2248 ** larger than 32 bits. */
2249 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
drh35cd6432009-06-05 14:17:21 +00002250 payloadSize = (u32)payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00002251 }else{
drhea8ffdf2009-07-22 00:35:23 +00002252 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drhb07028f2011-10-14 21:49:18 +00002253 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
drhea8ffdf2009-07-22 00:35:23 +00002254 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
danielk1977192ac1d2004-05-10 07:17:30 +00002255 }
drh4a6f3aa2011-08-28 00:19:26 +00002256 }else if( ALWAYS(pC->pseudoTableReg>0) ){
drha6c2ed92009-11-14 23:22:23 +00002257 pReg = &aMem[pC->pseudoTableReg];
drh21172c42012-10-30 00:29:07 +00002258 if( pC->multiPseudo ){
2259 sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
2260 Deephemeralize(pDest);
2261 goto op_column_out;
2262 }
drh3e9ca092009-09-08 01:14:48 +00002263 assert( pReg->flags & MEM_Blob );
drh2b4ded92010-09-27 21:09:31 +00002264 assert( memIsValid(pReg) );
drh3e9ca092009-09-08 01:14:48 +00002265 payloadSize = pReg->n;
2266 zRec = pReg->z;
2267 pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002268 assert( payloadSize==0 || zRec!=0 );
drh9a65f2c2009-06-22 19:05:40 +00002269 }else{
2270 /* Consider the row to be NULL */
2271 payloadSize = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00002272 }
2273
drhe6f43fc2011-08-28 02:15:34 +00002274 /* If payloadSize is 0, then just store a NULL. This can happen because of
2275 ** nullRow or because of a corrupt database. */
danielk1977192ac1d2004-05-10 07:17:30 +00002276 if( payloadSize==0 ){
drhe6f43fc2011-08-28 02:15:34 +00002277 MemSetTypeFlag(pDest, MEM_Null);
drhd4e70eb2008-01-02 00:34:36 +00002278 goto op_column_out;
danielk1977192ac1d2004-05-10 07:17:30 +00002279 }
drh35cd6432009-06-05 14:17:21 +00002280 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
2281 if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002282 goto too_big;
2283 }
danielk1977192ac1d2004-05-10 07:17:30 +00002284
shane36840fd2009-06-26 16:32:13 +00002285 nField = pC->nField;
drhd3194f52004-05-27 19:59:32 +00002286 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00002287
drh9188b382004-05-14 21:12:22 +00002288 /* Read and parse the table header. Store the results of the parse
2289 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00002290 */
danielk1977cd3e8f72008-03-25 09:47:35 +00002291 aType = pC->aType;
drha05a7222008-01-19 03:35:58 +00002292 if( pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00002293 aOffset = pC->aOffset;
2294 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00002295 assert(aType);
drh856c1032009-06-02 15:21:42 +00002296 avail = 0;
drhb73857f2006-03-17 00:25:59 +00002297 pC->aOffset = aOffset = &aType[nField];
2298 pC->payloadSize = payloadSize;
2299 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00002300
drhd3194f52004-05-27 19:59:32 +00002301 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00002302 if( zRec ){
2303 zData = zRec;
2304 }else{
drhf0863fe2005-06-12 21:35:51 +00002305 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00002306 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00002307 }else{
drhe51c44f2004-05-30 20:46:09 +00002308 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00002309 }
drhe61cffc2004-06-12 18:12:15 +00002310 /* If KeyFetch()/DataFetch() managed to get the entire payload,
2311 ** save the payload in the pC->aRow cache. That will save us from
2312 ** having to make additional calls to fetch the content portion of
2313 ** the record.
2314 */
drh35cd6432009-06-05 14:17:21 +00002315 assert( avail>=0 );
2316 if( payloadSize <= (u32)avail ){
drh2646da72005-12-09 20:02:05 +00002317 zRec = zData;
2318 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002319 }else{
2320 pC->aRow = 0;
2321 }
drhd3194f52004-05-27 19:59:32 +00002322 }
drhdda5c082012-03-28 13:41:10 +00002323 /* The following assert is true in all cases except when
drh588f5bc2007-01-02 18:41:54 +00002324 ** the database file has been corrupted externally.
2325 ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
drh35cd6432009-06-05 14:17:21 +00002326 szHdr = getVarint32((u8*)zData, offset);
2327
2328 /* Make sure a corrupt database has not given us an oversize header.
2329 ** Do this now to avoid an oversize memory allocation.
2330 **
2331 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
2332 ** types use so much data space that there can only be 4096 and 32 of
2333 ** them, respectively. So the maximum header length results from a
2334 ** 3-byte type for each of the maximum of 32768 columns plus three
2335 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
2336 */
2337 if( offset > 98307 ){
2338 rc = SQLITE_CORRUPT_BKPT;
2339 goto op_column_out;
2340 }
2341
2342 /* Compute in len the number of bytes of data we need to read in order
2343 ** to get nField type values. offset is an upper bound on this. But
2344 ** nField might be significantly less than the true number of columns
2345 ** in the table, and in that case, 5*nField+3 might be smaller than offset.
2346 ** We want to minimize len in order to limit the size of the memory
2347 ** allocation, especially if a corrupt database file has caused offset
2348 ** to be oversized. Offset is limited to 98307 above. But 98307 might
2349 ** still exceed Robson memory allocation limits on some configurations.
2350 ** On systems that cannot tolerate large memory allocations, nField*5+3
2351 ** will likely be much smaller since nField will likely be less than
2352 ** 20 or so. This insures that Robson memory allocation limits are
2353 ** not exceeded even for corrupt database files.
2354 */
2355 len = nField*5 + 3;
shane75ac1de2009-06-09 18:58:52 +00002356 if( len > (int)offset ) len = (int)offset;
drhe61cffc2004-06-12 18:12:15 +00002357
2358 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2359 ** record header in most cases. But they will fail to get the complete
2360 ** record header if the record header does not fit on a single page
2361 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2362 ** acquire the complete header text.
2363 */
drh35cd6432009-06-05 14:17:21 +00002364 if( !zRec && avail<len ){
danielk1977a7a8e142008-02-13 18:25:27 +00002365 sMem.flags = 0;
2366 sMem.db = 0;
drh35cd6432009-06-05 14:17:21 +00002367 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002368 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002369 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002370 }
drhb6f54522004-05-20 02:42:16 +00002371 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002372 }
drh35cd6432009-06-05 14:17:21 +00002373 zEndHdr = (u8 *)&zData[len];
2374 zIdx = (u8 *)&zData[szHdr];
drh9188b382004-05-14 21:12:22 +00002375
drhd3194f52004-05-27 19:59:32 +00002376 /* Scan the header and use it to fill in the aType[] and aOffset[]
2377 ** arrays. aType[i] will contain the type integer for the i-th
2378 ** column and aOffset[i] will contain the offset from the beginning
2379 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002380 */
danielk1977dedf45b2006-01-13 17:12:01 +00002381 for(i=0; i<nField; i++){
2382 if( zIdx<zEndHdr ){
drh6658cd92010-02-05 14:12:53 +00002383 aOffset[i] = offset;
drh5a077b72011-08-29 02:16:18 +00002384 if( zIdx[0]<0x80 ){
2385 t = zIdx[0];
2386 zIdx++;
2387 }else{
2388 zIdx += sqlite3GetVarint32(zIdx, &t);
2389 }
2390 aType[i] = t;
2391 szField = sqlite3VdbeSerialTypeLen(t);
drh6658cd92010-02-05 14:12:53 +00002392 offset += szField;
2393 if( offset<szField ){ /* True if offset overflows */
2394 zIdx = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
2395 break;
2396 }
danielk1977dedf45b2006-01-13 17:12:01 +00002397 }else{
drhdda5c082012-03-28 13:41:10 +00002398 /* If i is less that nField, then there are fewer fields in this
danielk1977dedf45b2006-01-13 17:12:01 +00002399 ** record than SetNumColumns indicated there are columns in the
2400 ** table. Set the offset for any extra columns not present in
drhdda5c082012-03-28 13:41:10 +00002401 ** the record to 0. This tells code below to store the default value
2402 ** for the column instead of deserializing a value from the record.
danielk1977dedf45b2006-01-13 17:12:01 +00002403 */
2404 aOffset[i] = 0;
2405 }
drh9188b382004-05-14 21:12:22 +00002406 }
danielk19775f096132008-03-28 15:44:09 +00002407 sqlite3VdbeMemRelease(&sMem);
drhd3194f52004-05-27 19:59:32 +00002408 sMem.flags = MEM_Null;
2409
danielk19779792eef2006-01-13 15:58:43 +00002410 /* If we have read more header data than was contained in the header,
2411 ** or if the end of the last field appears to be past the end of the
shane2ca8bc02008-05-07 18:59:28 +00002412 ** record, or if the end of the last field appears to be before the end
2413 ** of the record (when all fields present), then we must be dealing
2414 ** with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002415 */
drh6658cd92010-02-05 14:12:53 +00002416 if( (zIdx > zEndHdr) || (offset > payloadSize)
2417 || (zIdx==zEndHdr && offset!=payloadSize) ){
drh49285702005-09-17 15:20:26 +00002418 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002419 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002420 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002421 }
danielk1977192ac1d2004-05-10 07:17:30 +00002422
danielk197736963fd2005-02-19 08:18:05 +00002423 /* Get the column information. If aOffset[p2] is non-zero, then
2424 ** deserialize the value from the record. If aOffset[p2] is zero,
2425 ** then there are not enough fields in the record to satisfy the
drh66a51672008-01-03 00:01:23 +00002426 ** request. In this case, set the value NULL or to P4 if P4 is
drh29dda4a2005-07-21 18:23:20 +00002427 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002428 */
danielk197736963fd2005-02-19 08:18:05 +00002429 if( aOffset[p2] ){
2430 assert( rc==SQLITE_OK );
2431 if( zRec ){
drhac5e7492012-03-28 16:14:50 +00002432 /* This is the common case where the whole row fits on a single page */
drhe4c88c02012-01-04 12:57:45 +00002433 VdbeMemRelease(pDest);
danielk1977808ec7c2008-07-29 10:18:57 +00002434 sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
danielk197736963fd2005-02-19 08:18:05 +00002435 }else{
drhac5e7492012-03-28 16:14:50 +00002436 /* This branch happens only when the row overflows onto multiple pages */
drhdda5c082012-03-28 13:41:10 +00002437 t = aType[p2];
drha748fdc2012-03-28 01:34:47 +00002438 if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
drhdda5c082012-03-28 13:41:10 +00002439 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
drha748fdc2012-03-28 01:34:47 +00002440 ){
2441 /* Content is irrelevant for the typeof() function and for
drhdda5c082012-03-28 13:41:10 +00002442 ** the length(X) function if X is a blob. So we might as well use
drha748fdc2012-03-28 01:34:47 +00002443 ** bogus content rather than reading content from disk. NULL works
2444 ** for text and blob and whatever is in the payloadSize64 variable
2445 ** will work for everything else. */
2446 zData = t<12 ? (char*)&payloadSize64 : 0;
2447 }else{
drhac5e7492012-03-28 16:14:50 +00002448 len = sqlite3VdbeSerialTypeLen(t);
drha748fdc2012-03-28 01:34:47 +00002449 sqlite3VdbeMemMove(&sMem, pDest);
2450 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,
2451 &sMem);
2452 if( rc!=SQLITE_OK ){
2453 goto op_column_out;
2454 }
2455 zData = sMem.z;
danielk197736963fd2005-02-19 08:18:05 +00002456 }
drhdda5c082012-03-28 13:41:10 +00002457 sqlite3VdbeSerialGet((u8*)zData, t, pDest);
danielk19777701e812005-01-10 12:59:51 +00002458 }
drhd4e70eb2008-01-02 00:34:36 +00002459 pDest->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002460 }else{
danielk197760585dd2008-01-03 08:08:40 +00002461 if( pOp->p4type==P4_MEM ){
danielk19772dca4ac2008-01-03 11:50:29 +00002462 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
danielk1977aee18ef2005-03-09 12:26:50 +00002463 }else{
drhe6f43fc2011-08-28 02:15:34 +00002464 MemSetTypeFlag(pDest, MEM_Null);
danielk1977aee18ef2005-03-09 12:26:50 +00002465 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002466 }
drhfebe1062004-08-28 18:17:48 +00002467
2468 /* If we dynamically allocated space to hold the data (in the
2469 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhd4e70eb2008-01-02 00:34:36 +00002470 ** dynamically allocated space over to the pDest structure.
drhfebe1062004-08-28 18:17:48 +00002471 ** This prevents a memory copy.
2472 */
danielk19775f096132008-03-28 15:44:09 +00002473 if( sMem.zMalloc ){
2474 assert( sMem.z==sMem.zMalloc );
danielk1977a7a8e142008-02-13 18:25:27 +00002475 assert( !(pDest->flags & MEM_Dyn) );
2476 assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
2477 pDest->flags &= ~(MEM_Ephem|MEM_Static);
danielk19775f096132008-03-28 15:44:09 +00002478 pDest->flags |= MEM_Term;
danielk1977a7a8e142008-02-13 18:25:27 +00002479 pDest->z = sMem.z;
danielk19775f096132008-03-28 15:44:09 +00002480 pDest->zMalloc = sMem.zMalloc;
danielk1977b1bc9532004-05-22 03:05:33 +00002481 }
drhfebe1062004-08-28 18:17:48 +00002482
drhd4e70eb2008-01-02 00:34:36 +00002483 rc = sqlite3VdbeMemMakeWriteable(pDest);
drhd3194f52004-05-27 19:59:32 +00002484
danielk19773c9cc8d2005-01-17 03:40:08 +00002485op_column_out:
drhb7654112008-01-12 12:48:07 +00002486 UPDATE_MAX_BLOBSIZE(pDest);
drh5b6afba2008-01-05 16:29:28 +00002487 REGISTER_TRACE(pOp->p3, pDest);
danielk1977192ac1d2004-05-10 07:17:30 +00002488 break;
2489}
2490
danielk1977751de562008-04-18 09:01:15 +00002491/* Opcode: Affinity P1 P2 * P4 *
2492**
2493** Apply affinities to a range of P2 registers starting with P1.
2494**
2495** P4 is a string that is P2 characters long. The nth character of the
2496** string indicates the column affinity that should be used for the nth
2497** memory cell in the range.
2498*/
2499case OP_Affinity: {
drh039fc322009-11-17 18:31:47 +00002500 const char *zAffinity; /* The affinity to be applied */
2501 char cAff; /* A single character of affinity */
danielk1977751de562008-04-18 09:01:15 +00002502
drh856c1032009-06-02 15:21:42 +00002503 zAffinity = pOp->p4.z;
drh039fc322009-11-17 18:31:47 +00002504 assert( zAffinity!=0 );
2505 assert( zAffinity[pOp->p2]==0 );
2506 pIn1 = &aMem[pOp->p1];
2507 while( (cAff = *(zAffinity++))!=0 ){
2508 assert( pIn1 <= &p->aMem[p->nMem] );
drh2b4ded92010-09-27 21:09:31 +00002509 assert( memIsValid(pIn1) );
drh039fc322009-11-17 18:31:47 +00002510 ExpandBlob(pIn1);
2511 applyAffinity(pIn1, cAff, encoding);
2512 pIn1++;
danielk1977751de562008-04-18 09:01:15 +00002513 }
2514 break;
2515}
2516
drh1db639c2008-01-17 02:36:28 +00002517/* Opcode: MakeRecord P1 P2 P3 P4 *
drh7a224de2004-06-02 01:22:02 +00002518**
drh710c4842010-08-30 01:17:20 +00002519** Convert P2 registers beginning with P1 into the [record format]
2520** use as a data record in a database table or as a key
2521** in an index. The OP_Column opcode can decode the record later.
drh7a224de2004-06-02 01:22:02 +00002522**
danielk1977751de562008-04-18 09:01:15 +00002523** P4 may be a string that is P2 characters long. The nth character of the
drh7a224de2004-06-02 01:22:02 +00002524** string indicates the column affinity that should be used for the nth
drh9cbf3422008-01-17 16:22:13 +00002525** field of the index key.
drh7a224de2004-06-02 01:22:02 +00002526**
drh8a512562005-11-14 22:29:05 +00002527** The mapping from character to affinity is given by the SQLITE_AFF_
2528** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002529**
drh66a51672008-01-03 00:01:23 +00002530** If P4 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002531*/
drh1db639c2008-01-17 02:36:28 +00002532case OP_MakeRecord: {
drh856c1032009-06-02 15:21:42 +00002533 u8 *zNewRecord; /* A buffer to hold the data for the new record */
2534 Mem *pRec; /* The new record */
2535 u64 nData; /* Number of bytes of data space */
2536 int nHdr; /* Number of bytes of header space */
2537 i64 nByte; /* Data space required for this record */
2538 int nZero; /* Number of zero bytes at the end of the record */
2539 int nVarint; /* Number of bytes in a varint */
2540 u32 serial_type; /* Type field */
2541 Mem *pData0; /* First field to be combined into the record */
2542 Mem *pLast; /* Last field of the record */
2543 int nField; /* Number of fields in the record */
2544 char *zAffinity; /* The affinity string for the record */
2545 int file_format; /* File format to use for encoding */
2546 int i; /* Space used in zNewRecord[] */
2547 int len; /* Length of a field */
2548
drhf3218fe2004-05-28 08:21:02 +00002549 /* Assuming the record contains N fields, the record format looks
2550 ** like this:
2551 **
drh7a224de2004-06-02 01:22:02 +00002552 ** ------------------------------------------------------------------------
2553 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2554 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002555 **
drh9cbf3422008-01-17 16:22:13 +00002556 ** Data(0) is taken from register P1. Data(1) comes from register P1+1
2557 ** and so froth.
drhf3218fe2004-05-28 08:21:02 +00002558 **
2559 ** Each type field is a varint representing the serial type of the
2560 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002561 ** hdr-size field is also a varint which is the offset from the beginning
2562 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002563 */
drh856c1032009-06-02 15:21:42 +00002564 nData = 0; /* Number of bytes of data space */
2565 nHdr = 0; /* Number of bytes of header space */
drh856c1032009-06-02 15:21:42 +00002566 nZero = 0; /* Number of zero bytes at the end of the record */
drh1db639c2008-01-17 02:36:28 +00002567 nField = pOp->p1;
danielk19772dca4ac2008-01-03 11:50:29 +00002568 zAffinity = pOp->p4.z;
danielk19776ab3a2e2009-02-19 14:39:25 +00002569 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=p->nMem+1 );
drha6c2ed92009-11-14 23:22:23 +00002570 pData0 = &aMem[nField];
drh1db639c2008-01-17 02:36:28 +00002571 nField = pOp->p2;
2572 pLast = &pData0[nField-1];
drhd946db02005-12-29 19:23:06 +00002573 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002574
drh2b4ded92010-09-27 21:09:31 +00002575 /* Identify the output register */
2576 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
2577 pOut = &aMem[pOp->p3];
2578 memAboutToChange(p, pOut);
2579
drhf3218fe2004-05-28 08:21:02 +00002580 /* Loop through the elements that will make up the record to figure
2581 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002582 */
drha2a49dc2008-01-02 14:28:13 +00002583 for(pRec=pData0; pRec<=pLast; pRec++){
drh2b4ded92010-09-27 21:09:31 +00002584 assert( memIsValid(pRec) );
drhd3d39e92004-05-20 22:16:29 +00002585 if( zAffinity ){
drhb21c8cd2007-08-21 19:33:56 +00002586 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002587 }
danielk1977d908f5a2007-05-11 07:08:28 +00002588 if( pRec->flags&MEM_Zero && pRec->n>0 ){
drha05a7222008-01-19 03:35:58 +00002589 sqlite3VdbeMemExpandBlob(pRec);
danielk1977d908f5a2007-05-11 07:08:28 +00002590 }
drhd946db02005-12-29 19:23:06 +00002591 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhae7e1512007-05-02 16:51:59 +00002592 len = sqlite3VdbeSerialTypeLen(serial_type);
2593 nData += len;
drhf3218fe2004-05-28 08:21:02 +00002594 nHdr += sqlite3VarintLen(serial_type);
drhfdf972a2007-05-02 13:30:27 +00002595 if( pRec->flags & MEM_Zero ){
2596 /* Only pure zero-filled BLOBs can be input to this Opcode.
2597 ** We do not allow blobs with a prefix and a zero-filled tail. */
drh8df32842008-12-09 02:51:23 +00002598 nZero += pRec->u.nZero;
drhae7e1512007-05-02 16:51:59 +00002599 }else if( len ){
drhfdf972a2007-05-02 13:30:27 +00002600 nZero = 0;
2601 }
danielk19778d059842004-05-12 11:24:02 +00002602 }
danielk19773d1bfea2004-05-14 11:00:53 +00002603
drhf3218fe2004-05-28 08:21:02 +00002604 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002605 nHdr += nVarint = sqlite3VarintLen(nHdr);
2606 if( nVarint<sqlite3VarintLen(nHdr) ){
2607 nHdr++;
2608 }
drhfdf972a2007-05-02 13:30:27 +00002609 nByte = nHdr+nData-nZero;
drhbb4957f2008-03-20 14:03:29 +00002610 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00002611 goto too_big;
2612 }
drhf3218fe2004-05-28 08:21:02 +00002613
danielk1977a7a8e142008-02-13 18:25:27 +00002614 /* Make sure the output register has a buffer large enough to store
2615 ** the new record. The output register (pOp->p3) is not allowed to
2616 ** be one of the input registers (because the following call to
2617 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
2618 */
drh9c1905f2008-12-10 22:32:56 +00002619 if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
danielk1977a7a8e142008-02-13 18:25:27 +00002620 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002621 }
danielk1977a7a8e142008-02-13 18:25:27 +00002622 zNewRecord = (u8 *)pOut->z;
drhf3218fe2004-05-28 08:21:02 +00002623
2624 /* Write the record */
shane3f8d5cf2008-04-24 19:15:09 +00002625 i = putVarint32(zNewRecord, nHdr);
drha2a49dc2008-01-02 14:28:13 +00002626 for(pRec=pData0; pRec<=pLast; pRec++){
drhd946db02005-12-29 19:23:06 +00002627 serial_type = sqlite3VdbeSerialType(pRec, file_format);
shane3f8d5cf2008-04-24 19:15:09 +00002628 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002629 }
drha2a49dc2008-01-02 14:28:13 +00002630 for(pRec=pData0; pRec<=pLast; pRec++){ /* serial data */
drh9c1905f2008-12-10 22:32:56 +00002631 i += sqlite3VdbeSerialPut(&zNewRecord[i], (int)(nByte-i), pRec,file_format);
drhf3218fe2004-05-28 08:21:02 +00002632 }
drhfdf972a2007-05-02 13:30:27 +00002633 assert( i==nByte );
drhf3218fe2004-05-28 08:21:02 +00002634
drh9cbf3422008-01-17 16:22:13 +00002635 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drh9c1905f2008-12-10 22:32:56 +00002636 pOut->n = (int)nByte;
danielk1977a7a8e142008-02-13 18:25:27 +00002637 pOut->flags = MEM_Blob | MEM_Dyn;
2638 pOut->xDel = 0;
drhfdf972a2007-05-02 13:30:27 +00002639 if( nZero ){
drh8df32842008-12-09 02:51:23 +00002640 pOut->u.nZero = nZero;
drh477df4b2008-01-05 18:48:24 +00002641 pOut->flags |= MEM_Zero;
drhfdf972a2007-05-02 13:30:27 +00002642 }
drh477df4b2008-01-05 18:48:24 +00002643 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
drh1013c932008-01-06 00:25:21 +00002644 REGISTER_TRACE(pOp->p3, pOut);
drhb7654112008-01-12 12:48:07 +00002645 UPDATE_MAX_BLOBSIZE(pOut);
danielk19778d059842004-05-12 11:24:02 +00002646 break;
2647}
2648
danielk1977a5533162009-02-24 10:01:51 +00002649/* Opcode: Count P1 P2 * * *
2650**
2651** Store the number of entries (an integer value) in the table or index
2652** opened by cursor P1 in register P2
2653*/
2654#ifndef SQLITE_OMIT_BTREECOUNT
2655case OP_Count: { /* out2-prerelease */
2656 i64 nEntry;
drhc54a6172009-06-02 16:06:03 +00002657 BtCursor *pCrsr;
2658
2659 pCrsr = p->apCsr[pOp->p1]->pCursor;
dana205a482011-08-27 18:48:57 +00002660 if( ALWAYS(pCrsr) ){
drh818e39a2009-04-02 20:27:28 +00002661 rc = sqlite3BtreeCount(pCrsr, &nEntry);
2662 }else{
2663 nEntry = 0;
2664 }
danielk1977a5533162009-02-24 10:01:51 +00002665 pOut->u.i = nEntry;
2666 break;
2667}
2668#endif
2669
danielk1977fd7f0452008-12-17 17:30:26 +00002670/* Opcode: Savepoint P1 * * P4 *
2671**
2672** Open, release or rollback the savepoint named by parameter P4, depending
2673** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
2674** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
2675*/
2676case OP_Savepoint: {
drh856c1032009-06-02 15:21:42 +00002677 int p1; /* Value of P1 operand */
2678 char *zName; /* Name of savepoint */
2679 int nName;
2680 Savepoint *pNew;
2681 Savepoint *pSavepoint;
2682 Savepoint *pTmp;
2683 int iSavepoint;
2684 int ii;
2685
2686 p1 = pOp->p1;
2687 zName = pOp->p4.z;
danielk1977fd7f0452008-12-17 17:30:26 +00002688
2689 /* Assert that the p1 parameter is valid. Also that if there is no open
2690 ** transaction, then there cannot be any savepoints.
2691 */
2692 assert( db->pSavepoint==0 || db->autoCommit==0 );
2693 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
2694 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
2695 assert( checkSavepointCount(db) );
danc0537fe2013-06-28 19:41:43 +00002696 assert( p->bIsReader );
danielk1977fd7f0452008-12-17 17:30:26 +00002697
2698 if( p1==SAVEPOINT_BEGIN ){
drh4f7d3a52013-06-27 23:54:02 +00002699 if( db->nVdbeWrite>0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002700 /* A new savepoint cannot be created if there are active write
2701 ** statements (i.e. open read/write incremental blob handles).
2702 */
2703 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
2704 "SQL statements in progress");
2705 rc = SQLITE_BUSY;
2706 }else{
drh856c1032009-06-02 15:21:42 +00002707 nName = sqlite3Strlen30(zName);
danielk1977fd7f0452008-12-17 17:30:26 +00002708
drhbe07ec52011-06-03 12:15:26 +00002709#ifndef SQLITE_OMIT_VIRTUALTABLE
dand9495cd2011-04-27 12:08:04 +00002710 /* This call is Ok even if this savepoint is actually a transaction
2711 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
2712 ** If this is a transaction savepoint being opened, it is guaranteed
2713 ** that the db->aVTrans[] array is empty. */
2714 assert( db->autoCommit==0 || db->nVTrans==0 );
drha24bc9c2011-05-24 00:35:56 +00002715 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
2716 db->nStatement+db->nSavepoint);
dand9495cd2011-04-27 12:08:04 +00002717 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drh305ebab2011-05-26 14:19:14 +00002718#endif
dand9495cd2011-04-27 12:08:04 +00002719
danielk1977fd7f0452008-12-17 17:30:26 +00002720 /* Create a new savepoint structure. */
2721 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
2722 if( pNew ){
2723 pNew->zName = (char *)&pNew[1];
2724 memcpy(pNew->zName, zName, nName+1);
2725
2726 /* If there is no open transaction, then mark this as a special
2727 ** "transaction savepoint". */
2728 if( db->autoCommit ){
2729 db->autoCommit = 0;
2730 db->isTransactionSavepoint = 1;
2731 }else{
2732 db->nSavepoint++;
danielk1977d8293352009-04-30 09:10:37 +00002733 }
danielk1977fd7f0452008-12-17 17:30:26 +00002734
2735 /* Link the new savepoint into the database handle's list. */
2736 pNew->pNext = db->pSavepoint;
2737 db->pSavepoint = pNew;
danba9108b2009-09-22 07:13:42 +00002738 pNew->nDeferredCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002739 pNew->nDeferredImmCons = db->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002740 }
2741 }
2742 }else{
drh856c1032009-06-02 15:21:42 +00002743 iSavepoint = 0;
danielk1977fd7f0452008-12-17 17:30:26 +00002744
2745 /* Find the named savepoint. If there is no such savepoint, then an
2746 ** an error is returned to the user. */
2747 for(
drh856c1032009-06-02 15:21:42 +00002748 pSavepoint = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002749 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
drh856c1032009-06-02 15:21:42 +00002750 pSavepoint = pSavepoint->pNext
danielk1977fd7f0452008-12-17 17:30:26 +00002751 ){
2752 iSavepoint++;
2753 }
2754 if( !pSavepoint ){
2755 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
2756 rc = SQLITE_ERROR;
drh4f7d3a52013-06-27 23:54:02 +00002757 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
danielk1977fd7f0452008-12-17 17:30:26 +00002758 /* It is not possible to release (commit) a savepoint if there are
drh0f198a72012-02-13 16:43:16 +00002759 ** active write statements.
danielk1977fd7f0452008-12-17 17:30:26 +00002760 */
2761 sqlite3SetString(&p->zErrMsg, db,
drh0f198a72012-02-13 16:43:16 +00002762 "cannot release savepoint - SQL statements in progress"
danielk1977fd7f0452008-12-17 17:30:26 +00002763 );
2764 rc = SQLITE_BUSY;
2765 }else{
2766
2767 /* Determine whether or not this is a transaction savepoint. If so,
danielk197734cf35d2008-12-18 18:31:38 +00002768 ** and this is a RELEASE command, then the current transaction
2769 ** is committed.
danielk1977fd7f0452008-12-17 17:30:26 +00002770 */
2771 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
2772 if( isTransaction && p1==SAVEPOINT_RELEASE ){
dan32b09f22009-09-23 17:29:59 +00002773 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002774 goto vdbe_return;
2775 }
danielk1977fd7f0452008-12-17 17:30:26 +00002776 db->autoCommit = 1;
2777 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2778 p->pc = pc;
2779 db->autoCommit = 0;
2780 p->rc = rc = SQLITE_BUSY;
2781 goto vdbe_return;
2782 }
danielk197734cf35d2008-12-18 18:31:38 +00002783 db->isTransactionSavepoint = 0;
2784 rc = p->rc;
danielk1977fd7f0452008-12-17 17:30:26 +00002785 }else{
danielk1977fd7f0452008-12-17 17:30:26 +00002786 iSavepoint = db->nSavepoint - iSavepoint - 1;
drh31f10052012-03-31 17:17:26 +00002787 if( p1==SAVEPOINT_ROLLBACK ){
2788 for(ii=0; ii<db->nDb; ii++){
2789 sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
2790 }
drh0f198a72012-02-13 16:43:16 +00002791 }
2792 for(ii=0; ii<db->nDb; ii++){
danielk1977fd7f0452008-12-17 17:30:26 +00002793 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
2794 if( rc!=SQLITE_OK ){
2795 goto abort_due_to_error;
danielk1977bd434552009-03-18 10:33:00 +00002796 }
danielk1977fd7f0452008-12-17 17:30:26 +00002797 }
drh9f0bbf92009-01-02 21:08:09 +00002798 if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
danielk1977fd7f0452008-12-17 17:30:26 +00002799 sqlite3ExpirePreparedStatements(db);
drh81028a42012-05-15 18:28:27 +00002800 sqlite3ResetAllSchemasOfConnection(db);
danc311fee2010-08-31 16:25:19 +00002801 db->flags = (db->flags | SQLITE_InternChanges);
danielk1977fd7f0452008-12-17 17:30:26 +00002802 }
2803 }
2804
2805 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
2806 ** savepoints nested inside of the savepoint being operated on. */
2807 while( db->pSavepoint!=pSavepoint ){
drh856c1032009-06-02 15:21:42 +00002808 pTmp = db->pSavepoint;
danielk1977fd7f0452008-12-17 17:30:26 +00002809 db->pSavepoint = pTmp->pNext;
2810 sqlite3DbFree(db, pTmp);
2811 db->nSavepoint--;
2812 }
2813
dan1da40a32009-09-19 17:00:31 +00002814 /* If it is a RELEASE, then destroy the savepoint being operated on
2815 ** too. If it is a ROLLBACK TO, then set the number of deferred
2816 ** constraint violations present in the database to the value stored
2817 ** when the savepoint was created. */
danielk1977fd7f0452008-12-17 17:30:26 +00002818 if( p1==SAVEPOINT_RELEASE ){
2819 assert( pSavepoint==db->pSavepoint );
2820 db->pSavepoint = pSavepoint->pNext;
2821 sqlite3DbFree(db, pSavepoint);
2822 if( !isTransaction ){
2823 db->nSavepoint--;
2824 }
dan1da40a32009-09-19 17:00:31 +00002825 }else{
2826 db->nDeferredCons = pSavepoint->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002827 db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
danielk1977fd7f0452008-12-17 17:30:26 +00002828 }
dand9495cd2011-04-27 12:08:04 +00002829
2830 if( !isTransaction ){
2831 rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
2832 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2833 }
danielk1977fd7f0452008-12-17 17:30:26 +00002834 }
2835 }
2836
2837 break;
2838}
2839
drh98757152008-01-09 23:04:12 +00002840/* Opcode: AutoCommit P1 P2 * * *
danielk19771d850a72004-05-31 08:26:49 +00002841**
2842** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002843** back any currently active btree transactions. If there are any active
drhc25eabe2009-02-24 18:57:31 +00002844** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if
2845** there are active writing VMs or active VMs that use shared cache.
drh92f02c32004-09-02 14:57:08 +00002846**
2847** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002848*/
drh9cbf3422008-01-17 16:22:13 +00002849case OP_AutoCommit: {
drh856c1032009-06-02 15:21:42 +00002850 int desiredAutoCommit;
shane68c02732009-06-09 18:14:18 +00002851 int iRollback;
drh856c1032009-06-02 15:21:42 +00002852 int turnOnAC;
danielk19771d850a72004-05-31 08:26:49 +00002853
drh856c1032009-06-02 15:21:42 +00002854 desiredAutoCommit = pOp->p1;
shane68c02732009-06-09 18:14:18 +00002855 iRollback = pOp->p2;
drh856c1032009-06-02 15:21:42 +00002856 turnOnAC = desiredAutoCommit && !db->autoCommit;
drhad4a4b82008-11-05 16:37:34 +00002857 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
shane68c02732009-06-09 18:14:18 +00002858 assert( desiredAutoCommit==1 || iRollback==0 );
drh4f7d3a52013-06-27 23:54:02 +00002859 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
danc0537fe2013-06-28 19:41:43 +00002860 assert( p->bIsReader );
danielk197746c43ed2004-06-30 06:30:25 +00002861
drh0f198a72012-02-13 16:43:16 +00002862#if 0
drh4f7d3a52013-06-27 23:54:02 +00002863 if( turnOnAC && iRollback && db->nVdbeActive>1 ){
drhad4a4b82008-11-05 16:37:34 +00002864 /* If this instruction implements a ROLLBACK and other VMs are
danielk197746c43ed2004-06-30 06:30:25 +00002865 ** still running, and a transaction is active, return an error indicating
2866 ** that the other VMs must complete first.
2867 */
drhad4a4b82008-11-05 16:37:34 +00002868 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
2869 "SQL statements in progress");
drh99dfe5e2008-10-30 15:03:15 +00002870 rc = SQLITE_BUSY;
drh0f198a72012-02-13 16:43:16 +00002871 }else
2872#endif
drh4f7d3a52013-06-27 23:54:02 +00002873 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
drhad4a4b82008-11-05 16:37:34 +00002874 /* If this instruction implements a COMMIT and other VMs are writing
2875 ** return an error indicating that the other VMs must complete first.
2876 */
2877 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
2878 "SQL statements in progress");
2879 rc = SQLITE_BUSY;
2880 }else if( desiredAutoCommit!=db->autoCommit ){
shane68c02732009-06-09 18:14:18 +00002881 if( iRollback ){
drhad4a4b82008-11-05 16:37:34 +00002882 assert( desiredAutoCommit==1 );
drh21021a52012-02-13 17:01:51 +00002883 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
danielk1977f3f06bb2005-12-16 15:24:28 +00002884 db->autoCommit = 1;
dan32b09f22009-09-23 17:29:59 +00002885 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
dan1da40a32009-09-19 17:00:31 +00002886 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002887 }else{
shane7d3846a2008-12-11 02:58:26 +00002888 db->autoCommit = (u8)desiredAutoCommit;
danielk1977f3f06bb2005-12-16 15:24:28 +00002889 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
danielk1977f3f06bb2005-12-16 15:24:28 +00002890 p->pc = pc;
drh9c1905f2008-12-10 22:32:56 +00002891 db->autoCommit = (u8)(1-desiredAutoCommit);
drh900b31e2007-08-28 02:27:51 +00002892 p->rc = rc = SQLITE_BUSY;
2893 goto vdbe_return;
danielk1977f3f06bb2005-12-16 15:24:28 +00002894 }
danielk19771d850a72004-05-31 08:26:49 +00002895 }
danielk1977bd434552009-03-18 10:33:00 +00002896 assert( db->nStatement==0 );
danielk1977fd7f0452008-12-17 17:30:26 +00002897 sqlite3CloseSavepoints(db);
drh83968c42007-04-18 16:45:24 +00002898 if( p->rc==SQLITE_OK ){
drh900b31e2007-08-28 02:27:51 +00002899 rc = SQLITE_DONE;
drh83968c42007-04-18 16:45:24 +00002900 }else{
drh900b31e2007-08-28 02:27:51 +00002901 rc = SQLITE_ERROR;
drh83968c42007-04-18 16:45:24 +00002902 }
drh900b31e2007-08-28 02:27:51 +00002903 goto vdbe_return;
danielk19771d850a72004-05-31 08:26:49 +00002904 }else{
drhf089aa42008-07-08 19:34:06 +00002905 sqlite3SetString(&p->zErrMsg, db,
drhad4a4b82008-11-05 16:37:34 +00002906 (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
shane68c02732009-06-09 18:14:18 +00002907 (iRollback)?"cannot rollback - no transaction is active":
drhf089aa42008-07-08 19:34:06 +00002908 "cannot commit - no transaction is active"));
danielk19771d850a72004-05-31 08:26:49 +00002909
2910 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002911 }
2912 break;
2913}
2914
drh98757152008-01-09 23:04:12 +00002915/* Opcode: Transaction P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00002916**
2917** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002918** opcode is encountered. Depending on the ON CONFLICT setting, the
2919** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002920**
drh001bbcb2003-03-19 03:14:00 +00002921** P1 is the index of the database file on which the transaction is
2922** started. Index 0 is the main database file and index 1 is the
drh60a713c2008-01-21 16:22:45 +00002923** file used for temporary tables. Indices of 2 or more are used for
2924** attached databases.
drhcabb0812002-09-14 13:47:32 +00002925**
drh80242052004-06-09 00:48:12 +00002926** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002927** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002928** other process can start another write transaction while this transaction is
2929** underway. Starting a write transaction also creates a rollback journal. A
2930** write transaction must be started before any changes can be made to the
drhf7b54962013-05-28 12:11:54 +00002931** database. If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
2932** also obtained on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002933**
dane0af83a2009-09-08 19:15:01 +00002934** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
2935** true (this flag is set if the Vdbe may modify more than one row and may
2936** throw an ABORT exception), a statement transaction may also be opened.
2937** More specifically, a statement transaction is opened iff the database
2938** connection is currently not in autocommit mode, or if there are other
drha4510172012-02-02 15:50:17 +00002939** active statements. A statement transaction allows the changes made by this
dane0af83a2009-09-08 19:15:01 +00002940** VDBE to be rolled back after an error without having to roll back the
2941** entire transaction. If no error is encountered, the statement transaction
2942** will automatically commit when the VDBE halts.
2943**
danielk1977ee5741e2004-05-31 10:01:34 +00002944** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002945*/
drh9cbf3422008-01-17 16:22:13 +00002946case OP_Transaction: {
danielk19771d850a72004-05-31 08:26:49 +00002947 Btree *pBt;
2948
drh1713afb2013-06-28 01:24:57 +00002949 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00002950 assert( p->readOnly==0 || pOp->p2==0 );
drh653b82a2009-06-22 11:10:47 +00002951 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00002952 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh13447bf2013-07-10 13:33:49 +00002953 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
2954 rc = SQLITE_READONLY;
2955 goto abort_due_to_error;
2956 }
drh653b82a2009-06-22 11:10:47 +00002957 pBt = db->aDb[pOp->p1].pBt;
danielk19771d850a72004-05-31 08:26:49 +00002958
danielk197724162fe2004-06-04 06:22:00 +00002959 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002960 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002961 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002962 p->pc = pc;
drh900b31e2007-08-28 02:27:51 +00002963 p->rc = rc = SQLITE_BUSY;
drh900b31e2007-08-28 02:27:51 +00002964 goto vdbe_return;
danielk197724162fe2004-06-04 06:22:00 +00002965 }
drh9e9f1bd2009-10-13 15:36:51 +00002966 if( rc!=SQLITE_OK ){
danielk197724162fe2004-06-04 06:22:00 +00002967 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002968 }
dane0af83a2009-09-08 19:15:01 +00002969
2970 if( pOp->p2 && p->usesStmtJournal
danc0537fe2013-06-28 19:41:43 +00002971 && (db->autoCommit==0 || db->nVdbeRead>1)
dane0af83a2009-09-08 19:15:01 +00002972 ){
2973 assert( sqlite3BtreeIsInTrans(pBt) );
2974 if( p->iStatement==0 ){
2975 assert( db->nStatement>=0 && db->nSavepoint>=0 );
2976 db->nStatement++;
2977 p->iStatement = db->nSavepoint + db->nStatement;
2978 }
dana311b802011-04-26 19:21:34 +00002979
drh346506f2011-05-25 01:16:42 +00002980 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
dana311b802011-04-26 19:21:34 +00002981 if( rc==SQLITE_OK ){
2982 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
2983 }
dan1da40a32009-09-19 17:00:31 +00002984
2985 /* Store the current value of the database handles deferred constraint
2986 ** counter. If the statement transaction needs to be rolled back,
2987 ** the value of this counter needs to be restored too. */
2988 p->nStmtDefCons = db->nDeferredCons;
drh648e2642013-07-11 15:03:32 +00002989 p->nStmtDefImmCons = db->nDeferredImmCons;
dane0af83a2009-09-08 19:15:01 +00002990 }
drhb86ccfb2003-01-28 23:13:10 +00002991 }
drh5e00f6c2001-09-13 13:46:56 +00002992 break;
2993}
2994
drhb1fdb2a2008-01-05 04:06:03 +00002995/* Opcode: ReadCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00002996**
drh9cbf3422008-01-17 16:22:13 +00002997** Read cookie number P3 from database P1 and write it into register P2.
danielk19770d19f7a2009-06-03 11:25:07 +00002998** P3==1 is the schema version. P3==2 is the database format.
2999** P3==3 is the recommended pager cache size, and so forth. P1==0 is
drh001bbcb2003-03-19 03:14:00 +00003000** the main database file and P1==1 is the database file used to store
3001** temporary tables.
drh4a324312001-12-21 14:30:42 +00003002**
drh50e5dad2001-09-15 00:57:28 +00003003** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00003004** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00003005** executing this instruction.
3006*/
drh4c583122008-01-04 22:01:03 +00003007case OP_ReadCookie: { /* out2-prerelease */
drhf328bc82004-05-10 23:29:49 +00003008 int iMeta;
drh856c1032009-06-02 15:21:42 +00003009 int iDb;
3010 int iCookie;
danielk1977180b56a2007-06-24 08:00:42 +00003011
drh1713afb2013-06-28 01:24:57 +00003012 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00003013 iDb = pOp->p1;
3014 iCookie = pOp->p3;
drhb7654112008-01-12 12:48:07 +00003015 assert( pOp->p3<SQLITE_N_BTREE_META );
danielk1977180b56a2007-06-24 08:00:42 +00003016 assert( iDb>=0 && iDb<db->nDb );
3017 assert( db->aDb[iDb].pBt!=0 );
drhdddd7792011-04-03 18:19:25 +00003018 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
danielk19770d19f7a2009-06-03 11:25:07 +00003019
danielk1977602b4662009-07-02 07:47:33 +00003020 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
drh4c583122008-01-04 22:01:03 +00003021 pOut->u.i = iMeta;
drh50e5dad2001-09-15 00:57:28 +00003022 break;
3023}
3024
drh98757152008-01-09 23:04:12 +00003025/* Opcode: SetCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003026**
drh98757152008-01-09 23:04:12 +00003027** Write the content of register P3 (interpreted as an integer)
danielk19770d19f7a2009-06-03 11:25:07 +00003028** into cookie number P2 of database P1. P2==1 is the schema version.
3029** P2==2 is the database format. P2==3 is the recommended pager cache
3030** size, and so forth. P1==0 is the main database file and P1==1 is the
3031** database file used to store temporary tables.
drh50e5dad2001-09-15 00:57:28 +00003032**
3033** A transaction must be started before executing this opcode.
3034*/
drh9cbf3422008-01-17 16:22:13 +00003035case OP_SetCookie: { /* in3 */
drh3f7d4e42004-07-24 14:35:58 +00003036 Db *pDb;
drh4a324312001-12-21 14:30:42 +00003037 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00003038 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003039 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00003040 assert( p->readOnly==0 );
drh3f7d4e42004-07-24 14:35:58 +00003041 pDb = &db->aDb[pOp->p1];
3042 assert( pDb->pBt!=0 );
drh21206082011-04-04 18:22:02 +00003043 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh3c657212009-11-17 23:59:58 +00003044 pIn3 = &aMem[pOp->p3];
drh98757152008-01-09 23:04:12 +00003045 sqlite3VdbeMemIntegerify(pIn3);
drha3b321d2004-05-11 09:31:31 +00003046 /* See note about index shifting on OP_ReadCookie */
danielk19770d19f7a2009-06-03 11:25:07 +00003047 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
3048 if( pOp->p2==BTREE_SCHEMA_VERSION ){
drh3f7d4e42004-07-24 14:35:58 +00003049 /* When the schema cookie changes, record the new cookie internally */
drh9c1905f2008-12-10 22:32:56 +00003050 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003051 db->flags |= SQLITE_InternChanges;
danielk19770d19f7a2009-06-03 11:25:07 +00003052 }else if( pOp->p2==BTREE_FILE_FORMAT ){
drhd28bcb32005-12-21 14:43:11 +00003053 /* Record changes in the file format */
drh9c1905f2008-12-10 22:32:56 +00003054 pDb->pSchema->file_format = (u8)pIn3->u.i;
drh3f7d4e42004-07-24 14:35:58 +00003055 }
drhfd426c62006-01-30 15:34:22 +00003056 if( pOp->p1==1 ){
3057 /* Invalidate all prepared statements whenever the TEMP database
3058 ** schema is changed. Ticket #1644 */
3059 sqlite3ExpirePreparedStatements(db);
danfa401de2009-10-16 14:55:03 +00003060 p->expired = 0;
drhfd426c62006-01-30 15:34:22 +00003061 }
drh50e5dad2001-09-15 00:57:28 +00003062 break;
3063}
3064
drhc2a75552011-03-18 21:55:46 +00003065/* Opcode: VerifyCookie P1 P2 P3 * *
drh50e5dad2001-09-15 00:57:28 +00003066**
drh001bbcb2003-03-19 03:14:00 +00003067** Check the value of global database parameter number 0 (the
drhc2a75552011-03-18 21:55:46 +00003068** schema version) and make sure it is equal to P2 and that the
3069** generation counter on the local schema parse equals P3.
3070**
drh001bbcb2003-03-19 03:14:00 +00003071** P1 is the database number which is 0 for the main database file
3072** and 1 for the file holding temporary tables and some higher number
3073** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00003074**
3075** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00003076** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00003077** and that the current process needs to reread the schema.
3078**
3079** Either a transaction needs to have been started or an OP_Open needs
3080** to be executed (to establish a read lock) before this opcode is
3081** invoked.
3082*/
drh9cbf3422008-01-17 16:22:13 +00003083case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00003084 int iMeta;
drhc2a75552011-03-18 21:55:46 +00003085 int iGen;
drhc275b4e2004-07-19 17:25:24 +00003086 Btree *pBt;
drhc2a75552011-03-18 21:55:46 +00003087
drh001bbcb2003-03-19 03:14:00 +00003088 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003089 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh21206082011-04-04 18:22:02 +00003090 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
drh1713afb2013-06-28 01:24:57 +00003091 assert( p->bIsReader );
drhc275b4e2004-07-19 17:25:24 +00003092 pBt = db->aDb[pOp->p1].pBt;
3093 if( pBt ){
danielk1977602b4662009-07-02 07:47:33 +00003094 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
drhc2a75552011-03-18 21:55:46 +00003095 iGen = db->aDb[pOp->p1].pSchema->iGeneration;
drhc275b4e2004-07-19 17:25:24 +00003096 }else{
drhfcd71b62011-04-05 22:08:24 +00003097 iGen = iMeta = 0;
drhc275b4e2004-07-19 17:25:24 +00003098 }
drhc2a75552011-03-18 21:55:46 +00003099 if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
drh633e6d52008-07-28 19:34:53 +00003100 sqlite3DbFree(db, p->zErrMsg);
danielk1977a1644fd2007-08-29 12:31:25 +00003101 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
danielk1977896e7922007-04-17 08:32:33 +00003102 /* If the schema-cookie from the database file matches the cookie
3103 ** stored with the in-memory representation of the schema, do
3104 ** not reload the schema from the database file.
3105 **
shane21e7feb2008-05-30 15:59:49 +00003106 ** If virtual-tables are in use, this is not just an optimization.
danielk1977896e7922007-04-17 08:32:33 +00003107 ** Often, v-tables store their data in other SQLite tables, which
3108 ** are queried from within xNext() and other v-table methods using
3109 ** prepared queries. If such a query is out-of-date, we do not want to
3110 ** discard the database schema, as the user code implementing the
3111 ** v-table would have to be ready for the sqlite3_vtab structure itself
3112 ** to be invalidated whenever sqlite3_step() is called from within
3113 ** a v-table method.
3114 */
3115 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
drh81028a42012-05-15 18:28:27 +00003116 sqlite3ResetOneSchema(db, pOp->p1);
danielk1977896e7922007-04-17 08:32:33 +00003117 }
3118
drh5b6c5452011-02-22 03:34:56 +00003119 p->expired = 1;
drh50e5dad2001-09-15 00:57:28 +00003120 rc = SQLITE_SCHEMA;
3121 }
3122 break;
3123}
3124
drh98757152008-01-09 23:04:12 +00003125/* Opcode: OpenRead P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003126**
drhecdc7532001-09-23 02:35:53 +00003127** Open a read-only cursor for the database table whose root page is
danielk1977207872a2008-01-03 07:54:23 +00003128** P2 in a database file. The database file is determined by P3.
drh60a713c2008-01-21 16:22:45 +00003129** P3==0 means the main database, P3==1 means the database used for
3130** temporary tables, and P3>1 means used the corresponding attached
3131** database. Give the new cursor an identifier of P1. The P1
danielk1977207872a2008-01-03 07:54:23 +00003132** values need not be contiguous but all P1 values should be small integers.
3133** It is an error for P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00003134**
drh98757152008-01-09 23:04:12 +00003135** If P5!=0 then use the content of register P2 as the root page, not
3136** the value of P2 itself.
drh5edc3122001-09-13 21:53:09 +00003137**
drhb19a2bc2001-09-16 00:13:26 +00003138** There will be a read lock on the database whenever there is an
3139** open cursor. If the database was unlocked prior to this instruction
3140** then a read lock is acquired as part of this instruction. A read
3141** lock allows other processes to read the database but prohibits
3142** any other process from modifying the database. The read lock is
3143** released when all cursors are closed. If this instruction attempts
3144** to get a read lock but fails, the script terminates with an
3145** SQLITE_BUSY error code.
3146**
danielk1977d336e222009-02-20 10:58:41 +00003147** The P4 value may be either an integer (P4_INT32) or a pointer to
3148** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3149** structure, then said structure defines the content and collating
3150** sequence of the index being opened. Otherwise, if P4 is an integer
3151** value, it is set to the number of columns in the table.
drhf57b3392001-10-08 13:22:32 +00003152**
drh001bbcb2003-03-19 03:14:00 +00003153** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00003154*/
drh98757152008-01-09 23:04:12 +00003155/* Opcode: OpenWrite P1 P2 P3 P4 P5
drhecdc7532001-09-23 02:35:53 +00003156**
3157** Open a read/write cursor named P1 on the table or index whose root
drh98757152008-01-09 23:04:12 +00003158** page is P2. Or if P5!=0 use the content of register P2 to find the
3159** root page.
drhecdc7532001-09-23 02:35:53 +00003160**
danielk1977d336e222009-02-20 10:58:41 +00003161** The P4 value may be either an integer (P4_INT32) or a pointer to
3162** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
3163** structure, then said structure defines the content and collating
3164** sequence of the index being opened. Otherwise, if P4 is an integer
drh35cd6432009-06-05 14:17:21 +00003165** value, it is set to the number of columns in the table, or to the
3166** largest index of any column of the table that is actually used.
jplyon5a564222003-06-02 06:15:58 +00003167**
drh001bbcb2003-03-19 03:14:00 +00003168** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00003169** in read/write mode. For a given table, there can be one or more read-only
3170** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00003171**
drh001bbcb2003-03-19 03:14:00 +00003172** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00003173*/
drh9cbf3422008-01-17 16:22:13 +00003174case OP_OpenRead:
3175case OP_OpenWrite: {
drh856c1032009-06-02 15:21:42 +00003176 int nField;
3177 KeyInfo *pKeyInfo;
drh856c1032009-06-02 15:21:42 +00003178 int p2;
3179 int iDb;
drhf57b3392001-10-08 13:22:32 +00003180 int wrFlag;
3181 Btree *pX;
drhdfe88ec2008-11-03 20:55:06 +00003182 VdbeCursor *pCur;
drhd946db02005-12-29 19:23:06 +00003183 Db *pDb;
drh856c1032009-06-02 15:21:42 +00003184
dan428c2182012-08-06 18:50:11 +00003185 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
3186 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
drh1713afb2013-06-28 01:24:57 +00003187 assert( p->bIsReader );
drh9e92a472013-06-27 17:40:30 +00003188 assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
dan428c2182012-08-06 18:50:11 +00003189
danfa401de2009-10-16 14:55:03 +00003190 if( p->expired ){
3191 rc = SQLITE_ABORT;
3192 break;
3193 }
3194
drh856c1032009-06-02 15:21:42 +00003195 nField = 0;
3196 pKeyInfo = 0;
drh856c1032009-06-02 15:21:42 +00003197 p2 = pOp->p2;
3198 iDb = pOp->p3;
drh6810ce62004-01-31 19:22:56 +00003199 assert( iDb>=0 && iDb<db->nDb );
drhdddd7792011-04-03 18:19:25 +00003200 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drhd946db02005-12-29 19:23:06 +00003201 pDb = &db->aDb[iDb];
3202 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00003203 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00003204 if( pOp->opcode==OP_OpenWrite ){
3205 wrFlag = 1;
drh21206082011-04-04 18:22:02 +00003206 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
danielk1977da184232006-01-05 11:34:32 +00003207 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
3208 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00003209 }
3210 }else{
3211 wrFlag = 0;
3212 }
dan428c2182012-08-06 18:50:11 +00003213 if( pOp->p5 & OPFLAG_P2ISREG ){
drh9cbf3422008-01-17 16:22:13 +00003214 assert( p2>0 );
3215 assert( p2<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003216 pIn2 = &aMem[p2];
drh2b4ded92010-09-27 21:09:31 +00003217 assert( memIsValid(pIn2) );
3218 assert( (pIn2->flags & MEM_Int)!=0 );
drh9cbf3422008-01-17 16:22:13 +00003219 sqlite3VdbeMemIntegerify(pIn2);
drh9c1905f2008-12-10 22:32:56 +00003220 p2 = (int)pIn2->u.i;
drh9a65f2c2009-06-22 19:05:40 +00003221 /* The p2 value always comes from a prior OP_CreateTable opcode and
3222 ** that opcode will always set the p2 value to 2 or more or else fail.
3223 ** If there were a failure, the prepared statement would have halted
3224 ** before reaching this instruction. */
drh27731d72009-06-22 12:05:10 +00003225 if( NEVER(p2<2) ) {
shanedcc50b72008-11-13 18:29:50 +00003226 rc = SQLITE_CORRUPT_BKPT;
3227 goto abort_due_to_error;
3228 }
drh5edc3122001-09-13 21:53:09 +00003229 }
danielk1977d336e222009-02-20 10:58:41 +00003230 if( pOp->p4type==P4_KEYINFO ){
3231 pKeyInfo = pOp->p4.pKeyInfo;
3232 pKeyInfo->enc = ENC(p->db);
3233 nField = pKeyInfo->nField+1;
3234 }else if( pOp->p4type==P4_INT32 ){
3235 nField = pOp->p4.i;
3236 }
drh653b82a2009-06-22 11:10:47 +00003237 assert( pOp->p1>=0 );
3238 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
drh4774b132004-06-12 20:12:51 +00003239 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00003240 pCur->nullRow = 1;
drhd4187c72010-08-30 22:15:45 +00003241 pCur->isOrdered = 1;
danielk1977d336e222009-02-20 10:58:41 +00003242 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
3243 pCur->pKeyInfo = pKeyInfo;
dan428c2182012-08-06 18:50:11 +00003244 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
3245 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
danielk1977d336e222009-02-20 10:58:41 +00003246
dana205a482011-08-27 18:48:57 +00003247 /* Since it performs no memory allocation or IO, the only value that
3248 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
3249 assert( rc==SQLITE_OK );
danielk1977172114a2009-07-07 15:47:12 +00003250
3251 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
3252 ** SQLite used to check if the root-page flags were sane at this point
3253 ** and report database corruption if they were not, but this check has
3254 ** since moved into the btree layer. */
3255 pCur->isTable = pOp->p4type!=P4_KEYINFO;
3256 pCur->isIndex = !pCur->isTable;
drh5e00f6c2001-09-13 13:46:56 +00003257 break;
3258}
3259
drh2a5d9902011-08-26 00:34:45 +00003260/* Opcode: OpenEphemeral P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00003261**
drhb9bb7c12006-06-11 23:41:55 +00003262** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00003263** The cursor is always opened read/write even if
drh25d3adb2010-04-05 15:11:08 +00003264** the main database is read-only. The ephemeral
drh9170dd72005-07-08 17:13:46 +00003265** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00003266**
drh25d3adb2010-04-05 15:11:08 +00003267** P2 is the number of columns in the ephemeral table.
drh66a51672008-01-03 00:01:23 +00003268** The cursor points to a BTree table if P4==0 and to a BTree index
3269** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure
drhd3d39e92004-05-20 22:16:29 +00003270** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00003271**
3272** This opcode was once called OpenTemp. But that created
3273** confusion because the term "temp table", might refer either
3274** to a TEMP table at the SQL level, or to a table opened by
3275** this opcode. Then this opcode was call OpenVirtual. But
3276** that created confusion with the whole virtual-table idea.
drh2a5d9902011-08-26 00:34:45 +00003277**
3278** The P5 parameter can be a mask of the BTREE_* flags defined
3279** in btree.h. These flags control aspects of the operation of
3280** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
3281** added automatically.
drh5e00f6c2001-09-13 13:46:56 +00003282*/
drha21a64d2010-04-06 22:33:55 +00003283/* Opcode: OpenAutoindex P1 P2 * P4 *
3284**
3285** This opcode works the same as OP_OpenEphemeral. It has a
3286** different name to distinguish its use. Tables created using
3287** by this opcode will be used for automatically created transient
3288** indices in joins.
3289*/
3290case OP_OpenAutoindex:
drh9cbf3422008-01-17 16:22:13 +00003291case OP_OpenEphemeral: {
drhdfe88ec2008-11-03 20:55:06 +00003292 VdbeCursor *pCx;
drhd4187c72010-08-30 22:15:45 +00003293 static const int vfsFlags =
drh33f4e022007-09-03 15:19:34 +00003294 SQLITE_OPEN_READWRITE |
3295 SQLITE_OPEN_CREATE |
3296 SQLITE_OPEN_EXCLUSIVE |
3297 SQLITE_OPEN_DELETEONCLOSE |
3298 SQLITE_OPEN_TRANSIENT_DB;
3299
drh653b82a2009-06-22 11:10:47 +00003300 assert( pOp->p1>=0 );
3301 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
drh4774b132004-06-12 20:12:51 +00003302 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00003303 pCx->nullRow = 1;
dan689ab892011-08-12 15:02:00 +00003304 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
3305 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
drh5e00f6c2001-09-13 13:46:56 +00003306 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00003307 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00003308 }
3309 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00003310 /* If a transient index is required, create it by calling
drhd4187c72010-08-30 22:15:45 +00003311 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
danielk19774adee202004-05-08 08:23:19 +00003312 ** opening it. If a transient table is required, just use the
drhd4187c72010-08-30 22:15:45 +00003313 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00003314 */
danielk19772dca4ac2008-01-03 11:50:29 +00003315 if( pOp->p4.pKeyInfo ){
drhc6b52df2002-01-04 03:09:29 +00003316 int pgno;
drh66a51672008-01-03 00:01:23 +00003317 assert( pOp->p4type==P4_KEYINFO );
drhe1b4f0f2011-06-29 17:11:39 +00003318 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
drhc6b52df2002-01-04 03:09:29 +00003319 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00003320 assert( pgno==MASTER_ROOT+1 );
drh1e968a02008-03-25 00:22:21 +00003321 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1,
danielk1977cd3e8f72008-03-25 09:47:35 +00003322 (KeyInfo*)pOp->p4.z, pCx->pCursor);
danielk19772dca4ac2008-01-03 11:50:29 +00003323 pCx->pKeyInfo = pOp->p4.pKeyInfo;
dan689ab892011-08-12 15:02:00 +00003324 pCx->pKeyInfo->enc = ENC(p->db);
drhc6b52df2002-01-04 03:09:29 +00003325 }
drhf0863fe2005-06-12 21:35:51 +00003326 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00003327 }else{
danielk1977cd3e8f72008-03-25 09:47:35 +00003328 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00003329 pCx->isTable = 1;
drhc6b52df2002-01-04 03:09:29 +00003330 }
drh5e00f6c2001-09-13 13:46:56 +00003331 }
drhd4187c72010-08-30 22:15:45 +00003332 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
drhf0863fe2005-06-12 21:35:51 +00003333 pCx->isIndex = !pCx->isTable;
dan5134d132011-09-02 10:31:11 +00003334 break;
3335}
3336
drhfc5e5462012-12-03 17:04:40 +00003337/* Opcode: SorterOpen P1 P2 * P4 *
dan5134d132011-09-02 10:31:11 +00003338**
3339** This opcode works like OP_OpenEphemeral except that it opens
3340** a transient index that is specifically designed to sort large
3341** tables using an external merge-sort algorithm.
3342*/
drhca892a72011-09-03 00:17:51 +00003343case OP_SorterOpen: {
dan5134d132011-09-02 10:31:11 +00003344 VdbeCursor *pCx;
drh3a949872012-09-18 13:20:13 +00003345
dan5134d132011-09-02 10:31:11 +00003346 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
3347 if( pCx==0 ) goto no_mem;
3348 pCx->pKeyInfo = pOp->p4.pKeyInfo;
3349 pCx->pKeyInfo->enc = ENC(p->db);
3350 pCx->isSorter = 1;
3351 rc = sqlite3VdbeSorterInit(db, pCx);
drh5e00f6c2001-09-13 13:46:56 +00003352 break;
3353}
3354
drh980db4b2012-10-30 14:44:14 +00003355/* Opcode: OpenPseudo P1 P2 P3 * P5
drh70ce3f02003-04-15 19:22:22 +00003356**
3357** Open a new cursor that points to a fake table that contains a single
drh3e9ca092009-09-08 01:14:48 +00003358** row of data. The content of that one row in the content of memory
drh21172c42012-10-30 00:29:07 +00003359** register P2 when P5==0. In other words, cursor P1 becomes an alias for the
3360** MEM_Blob content contained in register P2. When P5==1, then the
3361** row is represented by P3 consecutive registers beginning with P2.
drh70ce3f02003-04-15 19:22:22 +00003362**
drh2d8d7ce2010-02-15 15:17:05 +00003363** A pseudo-table created by this opcode is used to hold a single
drhcdd536f2006-03-17 00:04:03 +00003364** row output from the sorter so that the row can be decomposed into
drh3e9ca092009-09-08 01:14:48 +00003365** individual columns using the OP_Column opcode. The OP_Column opcode
3366** is the only cursor opcode that works with a pseudo-table.
danielk1977d336e222009-02-20 10:58:41 +00003367**
3368** P3 is the number of fields in the records that will be stored by
3369** the pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00003370*/
drh9cbf3422008-01-17 16:22:13 +00003371case OP_OpenPseudo: {
drhdfe88ec2008-11-03 20:55:06 +00003372 VdbeCursor *pCx;
drh856c1032009-06-02 15:21:42 +00003373
drh653b82a2009-06-22 11:10:47 +00003374 assert( pOp->p1>=0 );
3375 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
drh4774b132004-06-12 20:12:51 +00003376 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00003377 pCx->nullRow = 1;
drh3e9ca092009-09-08 01:14:48 +00003378 pCx->pseudoTableReg = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00003379 pCx->isTable = 1;
3380 pCx->isIndex = 0;
drh21172c42012-10-30 00:29:07 +00003381 pCx->multiPseudo = pOp->p5;
drh70ce3f02003-04-15 19:22:22 +00003382 break;
3383}
3384
drh98757152008-01-09 23:04:12 +00003385/* Opcode: Close P1 * * * *
drh5e00f6c2001-09-13 13:46:56 +00003386**
3387** Close a cursor previously opened as P1. If P1 is not
3388** currently open, this instruction is a no-op.
3389*/
drh9cbf3422008-01-17 16:22:13 +00003390case OP_Close: {
drh653b82a2009-06-22 11:10:47 +00003391 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3392 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
3393 p->apCsr[pOp->p1] = 0;
drh5e00f6c2001-09-13 13:46:56 +00003394 break;
3395}
3396
drh959403f2008-12-12 17:56:16 +00003397/* Opcode: SeekGe P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003398**
danielk1977b790c6c2008-04-18 10:25:24 +00003399** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003400** use the value in register P3 as the key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003401** to an SQL index, then P3 is the first in an array of P4 registers
3402** that are used as an unpacked index key.
3403**
3404** Reposition cursor P1 so that it points to the smallest entry that
3405** is greater than or equal to the key value. If there are no records
3406** greater than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003407**
drh959403f2008-12-12 17:56:16 +00003408** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003409*/
drh959403f2008-12-12 17:56:16 +00003410/* Opcode: SeekGt P1 P2 P3 P4 *
drh7cf6e4d2004-05-19 14:56:55 +00003411**
danielk1977b790c6c2008-04-18 10:25:24 +00003412** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003413** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003414** to an SQL index, then P3 is the first in an array of P4 registers
3415** that are used as an unpacked index key.
3416**
3417** Reposition cursor P1 so that it points to the smallest entry that
3418** is greater than the key value. If there are no records greater than
3419** the key and P2 is not zero, then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00003420**
drh959403f2008-12-12 17:56:16 +00003421** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
drh5e00f6c2001-09-13 13:46:56 +00003422*/
drh959403f2008-12-12 17:56:16 +00003423/* Opcode: SeekLt P1 P2 P3 P4 *
drhc045ec52002-12-04 20:01:06 +00003424**
danielk1977b790c6c2008-04-18 10:25:24 +00003425** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003426** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003427** to an SQL index, then P3 is the first in an array of P4 registers
3428** that are used as an unpacked index key.
3429**
3430** Reposition cursor P1 so that it points to the largest entry that
3431** is less than the key value. If there are no records less than
3432** the key and P2 is not zero, then jump to P2.
drhc045ec52002-12-04 20:01:06 +00003433**
drh959403f2008-12-12 17:56:16 +00003434** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
drh7cf6e4d2004-05-19 14:56:55 +00003435*/
drh959403f2008-12-12 17:56:16 +00003436/* Opcode: SeekLe P1 P2 P3 P4 *
danielk19773d1bfea2004-05-14 11:00:53 +00003437**
danielk1977b790c6c2008-04-18 10:25:24 +00003438** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
drh959403f2008-12-12 17:56:16 +00003439** use the value in register P3 as a key. If cursor P1 refers
danielk1977b790c6c2008-04-18 10:25:24 +00003440** to an SQL index, then P3 is the first in an array of P4 registers
3441** that are used as an unpacked index key.
danielk1977751de562008-04-18 09:01:15 +00003442**
danielk1977b790c6c2008-04-18 10:25:24 +00003443** Reposition cursor P1 so that it points to the largest entry that
3444** is less than or equal to the key value. If there are no records
3445** less than or equal to the key and P2 is not zero, then jump to P2.
drh7cf6e4d2004-05-19 14:56:55 +00003446**
drh959403f2008-12-12 17:56:16 +00003447** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
drhc045ec52002-12-04 20:01:06 +00003448*/
drh959403f2008-12-12 17:56:16 +00003449case OP_SeekLt: /* jump, in3 */
3450case OP_SeekLe: /* jump, in3 */
3451case OP_SeekGe: /* jump, in3 */
3452case OP_SeekGt: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003453 int res;
3454 int oc;
drhdfe88ec2008-11-03 20:55:06 +00003455 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003456 UnpackedRecord r;
3457 int nField;
3458 i64 iKey; /* The rowid we are to seek to */
drh80ff32f2001-11-04 18:32:46 +00003459
drh653b82a2009-06-22 11:10:47 +00003460 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh959403f2008-12-12 17:56:16 +00003461 assert( pOp->p2!=0 );
drh653b82a2009-06-22 11:10:47 +00003462 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003463 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00003464 assert( pC->pseudoTableReg==0 );
drh1f350122009-11-13 20:52:43 +00003465 assert( OP_SeekLe == OP_SeekLt+1 );
3466 assert( OP_SeekGe == OP_SeekLt+2 );
3467 assert( OP_SeekGt == OP_SeekLt+3 );
drhd4187c72010-08-30 22:15:45 +00003468 assert( pC->isOrdered );
dana205a482011-08-27 18:48:57 +00003469 if( ALWAYS(pC->pCursor!=0) ){
drh7cf6e4d2004-05-19 14:56:55 +00003470 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00003471 pC->nullRow = 0;
drhf0863fe2005-06-12 21:35:51 +00003472 if( pC->isTable ){
drh959403f2008-12-12 17:56:16 +00003473 /* The input value in P3 might be of any type: integer, real, string,
3474 ** blob, or NULL. But it needs to be an integer before we can do
3475 ** the seek, so covert it. */
drh3c657212009-11-17 23:59:58 +00003476 pIn3 = &aMem[pOp->p3];
drh959403f2008-12-12 17:56:16 +00003477 applyNumericAffinity(pIn3);
3478 iKey = sqlite3VdbeIntValue(pIn3);
3479 pC->rowidIsValid = 0;
3480
3481 /* If the P3 value could not be converted into an integer without
3482 ** loss of information, then special processing is required... */
3483 if( (pIn3->flags & MEM_Int)==0 ){
3484 if( (pIn3->flags & MEM_Real)==0 ){
3485 /* If the P3 value cannot be converted into any kind of a number,
3486 ** then the seek is not possible, so jump to P2 */
3487 pc = pOp->p2 - 1;
3488 break;
3489 }
3490 /* If we reach this point, then the P3 value must be a floating
3491 ** point number. */
3492 assert( (pIn3->flags & MEM_Real)!=0 );
3493
3494 if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
drhaa736092009-06-22 00:55:30 +00003495 /* The P3 value is too large in magnitude to be expressed as an
drh959403f2008-12-12 17:56:16 +00003496 ** integer. */
3497 res = 1;
3498 if( pIn3->r<0 ){
drh1f350122009-11-13 20:52:43 +00003499 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003500 rc = sqlite3BtreeFirst(pC->pCursor, &res);
3501 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3502 }
3503 }else{
drh1f350122009-11-13 20:52:43 +00003504 if( oc<=OP_SeekLe ){ assert( oc==OP_SeekLt || oc==OP_SeekLe );
drh959403f2008-12-12 17:56:16 +00003505 rc = sqlite3BtreeLast(pC->pCursor, &res);
3506 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3507 }
3508 }
3509 if( res ){
3510 pc = pOp->p2 - 1;
3511 }
3512 break;
3513 }else if( oc==OP_SeekLt || oc==OP_SeekGe ){
3514 /* Use the ceiling() function to convert real->int */
3515 if( pIn3->r > (double)iKey ) iKey++;
3516 }else{
3517 /* Use the floor() function to convert real->int */
3518 assert( oc==OP_SeekLe || oc==OP_SeekGt );
3519 if( pIn3->r < (double)iKey ) iKey--;
3520 }
3521 }
drhe63d9992008-08-13 19:11:48 +00003522 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003523 if( rc!=SQLITE_OK ){
3524 goto abort_due_to_error;
3525 }
drh959403f2008-12-12 17:56:16 +00003526 if( res==0 ){
3527 pC->rowidIsValid = 1;
3528 pC->lastRowid = iKey;
3529 }
drh5e00f6c2001-09-13 13:46:56 +00003530 }else{
drh856c1032009-06-02 15:21:42 +00003531 nField = pOp->p4.i;
danielk1977b790c6c2008-04-18 10:25:24 +00003532 assert( pOp->p4type==P4_INT32 );
3533 assert( nField>0 );
3534 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00003535 r.nField = (u16)nField;
drh1f350122009-11-13 20:52:43 +00003536
3537 /* The next line of code computes as follows, only faster:
3538 ** if( oc==OP_SeekGt || oc==OP_SeekLe ){
3539 ** r.flags = UNPACKED_INCRKEY;
3540 ** }else{
3541 ** r.flags = 0;
3542 ** }
3543 */
danfbfe3882013-04-08 10:38:57 +00003544 r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
drh1f350122009-11-13 20:52:43 +00003545 assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
3546 assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
3547 assert( oc!=OP_SeekGe || r.flags==0 );
3548 assert( oc!=OP_SeekLt || r.flags==0 );
3549
drha6c2ed92009-11-14 23:22:23 +00003550 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003551#ifdef SQLITE_DEBUG
3552 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3553#endif
drh039fc322009-11-17 18:31:47 +00003554 ExpandBlob(r.aMem);
drhe63d9992008-08-13 19:11:48 +00003555 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
danielk197728129562005-01-11 10:25:06 +00003556 if( rc!=SQLITE_OK ){
3557 goto abort_due_to_error;
3558 }
drhf0863fe2005-06-12 21:35:51 +00003559 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003560 }
drha11846b2004-01-07 18:52:56 +00003561 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003562 pC->cacheStatus = CACHE_STALE;
drh0f7eb612006-08-08 13:51:43 +00003563#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +00003564 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00003565#endif
drh1f350122009-11-13 20:52:43 +00003566 if( oc>=OP_SeekGe ){ assert( oc==OP_SeekGe || oc==OP_SeekGt );
drh959403f2008-12-12 17:56:16 +00003567 if( res<0 || (res==0 && oc==OP_SeekGt) ){
danielk197728129562005-01-11 10:25:06 +00003568 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00003569 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003570 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00003571 }else{
3572 res = 0;
drh8721ce42001-11-07 14:22:00 +00003573 }
drh7cf6e4d2004-05-19 14:56:55 +00003574 }else{
drh959403f2008-12-12 17:56:16 +00003575 assert( oc==OP_SeekLt || oc==OP_SeekLe );
3576 if( res>0 || (res==0 && oc==OP_SeekLt) ){
danielk197701427a62005-01-11 13:02:33 +00003577 rc = sqlite3BtreePrevious(pC->pCursor, &res);
3578 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00003579 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00003580 }else{
3581 /* res might be negative because the table is empty. Check to
3582 ** see if this is the case.
3583 */
drhf328bc82004-05-10 23:29:49 +00003584 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00003585 }
drh1af3fdb2004-07-18 21:33:01 +00003586 }
drh91fd4d42008-01-19 20:11:25 +00003587 assert( pOp->p2>0 );
drh1af3fdb2004-07-18 21:33:01 +00003588 if( res ){
drh91fd4d42008-01-19 20:11:25 +00003589 pc = pOp->p2 - 1;
drh8721ce42001-11-07 14:22:00 +00003590 }
drhaa736092009-06-22 00:55:30 +00003591 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003592 /* This happens when attempting to open the sqlite3_master table
3593 ** for read access returns SQLITE_EMPTY. In this case always
3594 ** take the jump (since there are no records in the table).
3595 */
3596 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003597 }
drh5e00f6c2001-09-13 13:46:56 +00003598 break;
3599}
3600
drh959403f2008-12-12 17:56:16 +00003601/* Opcode: Seek P1 P2 * * *
3602**
3603** P1 is an open table cursor and P2 is a rowid integer. Arrange
3604** for P1 to move so that it points to the rowid given by P2.
3605**
3606** This is actually a deferred seek. Nothing actually happens until
3607** the cursor is used to read a record. That way, if no reads
3608** occur, no unnecessary I/O happens.
3609*/
3610case OP_Seek: { /* in2 */
drh959403f2008-12-12 17:56:16 +00003611 VdbeCursor *pC;
3612
drh653b82a2009-06-22 11:10:47 +00003613 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3614 pC = p->apCsr[pOp->p1];
drh959403f2008-12-12 17:56:16 +00003615 assert( pC!=0 );
drhaa736092009-06-22 00:55:30 +00003616 if( ALWAYS(pC->pCursor!=0) ){
drh959403f2008-12-12 17:56:16 +00003617 assert( pC->isTable );
3618 pC->nullRow = 0;
drh3c657212009-11-17 23:59:58 +00003619 pIn2 = &aMem[pOp->p2];
drh959403f2008-12-12 17:56:16 +00003620 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
3621 pC->rowidIsValid = 0;
3622 pC->deferredMoveto = 1;
3623 }
3624 break;
3625}
3626
3627
drh8cff69d2009-11-12 19:59:44 +00003628/* Opcode: Found P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003629**
drh8cff69d2009-11-12 19:59:44 +00003630** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3631** P4>0 then register P3 is the first of P4 registers that form an unpacked
3632** record.
3633**
3634** Cursor P1 is on an index btree. If the record identified by P3 and P4
3635** is a prefix of any entry in P1 then a jump is made to P2 and
drhe3365e62009-11-12 17:52:24 +00003636** P1 is left pointing at the matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003637*/
drh8cff69d2009-11-12 19:59:44 +00003638/* Opcode: NotFound P1 P2 P3 P4 *
drh5e00f6c2001-09-13 13:46:56 +00003639**
drh8cff69d2009-11-12 19:59:44 +00003640** If P4==0 then register P3 holds a blob constructed by MakeRecord. If
3641** P4>0 then register P3 is the first of P4 registers that form an unpacked
3642** record.
3643**
3644** Cursor P1 is on an index btree. If the record identified by P3 and P4
3645** is not the prefix of any entry in P1 then a jump is made to P2. If P1
3646** does contain an entry whose prefix matches the P3/P4 record then control
3647** falls through to the next instruction and P1 is left pointing at the
3648** matching entry.
drh5e00f6c2001-09-13 13:46:56 +00003649**
drhcb6d50e2008-08-21 19:28:30 +00003650** See also: Found, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00003651*/
drh9cbf3422008-01-17 16:22:13 +00003652case OP_NotFound: /* jump, in3 */
3653case OP_Found: { /* jump, in3 */
drh856c1032009-06-02 15:21:42 +00003654 int alreadyExists;
drhdfe88ec2008-11-03 20:55:06 +00003655 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00003656 int res;
dan03e9cfc2011-09-05 14:20:27 +00003657 char *pFree;
drh856c1032009-06-02 15:21:42 +00003658 UnpackedRecord *pIdxKey;
drh8cff69d2009-11-12 19:59:44 +00003659 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00003660 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
3661
dan0ff297e2009-09-25 17:03:14 +00003662#ifdef SQLITE_TEST
3663 sqlite3_found_count++;
3664#endif
3665
drh856c1032009-06-02 15:21:42 +00003666 alreadyExists = 0;
drhaa736092009-06-22 00:55:30 +00003667 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh8cff69d2009-11-12 19:59:44 +00003668 assert( pOp->p4type==P4_INT32 );
drhaa736092009-06-22 00:55:30 +00003669 pC = p->apCsr[pOp->p1];
3670 assert( pC!=0 );
drh3c657212009-11-17 23:59:58 +00003671 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003672 if( ALWAYS(pC->pCursor!=0) ){
drhe63d9992008-08-13 19:11:48 +00003673
drhf0863fe2005-06-12 21:35:51 +00003674 assert( pC->isTable==0 );
drh8cff69d2009-11-12 19:59:44 +00003675 if( pOp->p4.i>0 ){
3676 r.pKeyInfo = pC->pKeyInfo;
shaneh5e17e8b2009-12-03 04:40:47 +00003677 r.nField = (u16)pOp->p4.i;
drh8cff69d2009-11-12 19:59:44 +00003678 r.aMem = pIn3;
drh2b4ded92010-09-27 21:09:31 +00003679#ifdef SQLITE_DEBUG
3680 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3681#endif
drh8cff69d2009-11-12 19:59:44 +00003682 r.flags = UNPACKED_PREFIX_MATCH;
3683 pIdxKey = &r;
3684 }else{
dan03e9cfc2011-09-05 14:20:27 +00003685 pIdxKey = sqlite3VdbeAllocUnpackedRecord(
3686 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
3687 );
3688 if( pIdxKey==0 ) goto no_mem;
drh8cff69d2009-11-12 19:59:44 +00003689 assert( pIn3->flags & MEM_Blob );
drhd81a1422010-09-28 07:11:24 +00003690 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
dan03e9cfc2011-09-05 14:20:27 +00003691 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
drh8cff69d2009-11-12 19:59:44 +00003692 pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
danielk19779a96b662007-11-29 17:05:18 +00003693 }
drhe63d9992008-08-13 19:11:48 +00003694 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
drh8cff69d2009-11-12 19:59:44 +00003695 if( pOp->p4.i==0 ){
dan03e9cfc2011-09-05 14:20:27 +00003696 sqlite3DbFree(db, pFree);
drh8cff69d2009-11-12 19:59:44 +00003697 }
danielk197777519402007-08-30 11:48:31 +00003698 if( rc!=SQLITE_OK ){
3699 break;
3700 }
3701 alreadyExists = (res==0);
drha11846b2004-01-07 18:52:56 +00003702 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003703 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003704 }
3705 if( pOp->opcode==OP_Found ){
3706 if( alreadyExists ) pc = pOp->p2 - 1;
3707 }else{
3708 if( !alreadyExists ) pc = pOp->p2 - 1;
3709 }
drh5e00f6c2001-09-13 13:46:56 +00003710 break;
3711}
3712
drh98757152008-01-09 23:04:12 +00003713/* Opcode: IsUnique P1 P2 P3 P4 *
drh9cfcf5d2002-01-29 18:41:24 +00003714**
drh8cff69d2009-11-12 19:59:44 +00003715** Cursor P1 is open on an index b-tree - that is to say, a btree which
3716** no data and where the key are records generated by OP_MakeRecord with
3717** the list field being the integer ROWID of the entry that the index
3718** entry refers to.
danielk1977de630352009-05-04 11:42:29 +00003719**
3720** The P3 register contains an integer record number. Call this record
3721** number R. Register P4 is the first in a set of N contiguous registers
3722** that make up an unpacked index key that can be used with cursor P1.
3723** The value of N can be inferred from the cursor. N includes the rowid
3724** value appended to the end of the index record. This rowid value may
3725** or may not be the same as R.
3726**
3727** If any of the N registers beginning with register P4 contains a NULL
3728** value, jump immediately to P2.
3729**
3730** Otherwise, this instruction checks if cursor P1 contains an entry
3731** where the first (N-1) fields match but the rowid value at the end
3732** of the index entry is not R. If there is no such entry, control jumps
3733** to instruction P2. Otherwise, the rowid of the conflicting index
3734** entry is copied to register P3 and control falls through to the next
3735** instruction.
drh9cfcf5d2002-01-29 18:41:24 +00003736**
drh9cbf3422008-01-17 16:22:13 +00003737** See also: NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00003738*/
drh9cbf3422008-01-17 16:22:13 +00003739case OP_IsUnique: { /* jump, in3 */
shane60a4b532009-05-06 18:57:09 +00003740 u16 ii;
drhdfe88ec2008-11-03 20:55:06 +00003741 VdbeCursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00003742 BtCursor *pCrsr;
shane60a4b532009-05-06 18:57:09 +00003743 u16 nField;
drha6c2ed92009-11-14 23:22:23 +00003744 Mem *aMx;
drh856c1032009-06-02 15:21:42 +00003745 UnpackedRecord r; /* B-Tree index search key */
3746 i64 R; /* Rowid stored in register P3 */
drh9cfcf5d2002-01-29 18:41:24 +00003747
drh3c657212009-11-17 23:59:58 +00003748 pIn3 = &aMem[pOp->p3];
drha6c2ed92009-11-14 23:22:23 +00003749 aMx = &aMem[pOp->p4.i];
danielk1977de630352009-05-04 11:42:29 +00003750 /* Assert that the values of parameters P1 and P4 are in range. */
drh98757152008-01-09 23:04:12 +00003751 assert( pOp->p4type==P4_INT32 );
drh9cbf3422008-01-17 16:22:13 +00003752 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
danielk1977de630352009-05-04 11:42:29 +00003753 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3754
3755 /* Find the index cursor. */
3756 pCx = p->apCsr[pOp->p1];
3757 assert( pCx->deferredMoveto==0 );
3758 pCx->seekResult = 0;
3759 pCx->cacheStatus = CACHE_STALE;
drhf328bc82004-05-10 23:29:49 +00003760 pCrsr = pCx->pCursor;
danielk1977de630352009-05-04 11:42:29 +00003761
3762 /* If any of the values are NULL, take the jump. */
3763 nField = pCx->pKeyInfo->nField;
3764 for(ii=0; ii<nField; ii++){
drha6c2ed92009-11-14 23:22:23 +00003765 if( aMx[ii].flags & MEM_Null ){
danielk1977de630352009-05-04 11:42:29 +00003766 pc = pOp->p2 - 1;
3767 pCrsr = 0;
3768 break;
3769 }
3770 }
drha6c2ed92009-11-14 23:22:23 +00003771 assert( (aMx[nField].flags & MEM_Null)==0 );
danielk1977de630352009-05-04 11:42:29 +00003772
drhf328bc82004-05-10 23:29:49 +00003773 if( pCrsr!=0 ){
danielk1977de630352009-05-04 11:42:29 +00003774 /* Populate the index search key. */
3775 r.pKeyInfo = pCx->pKeyInfo;
3776 r.nField = nField + 1;
3777 r.flags = UNPACKED_PREFIX_SEARCH;
drha6c2ed92009-11-14 23:22:23 +00003778 r.aMem = aMx;
drh2b4ded92010-09-27 21:09:31 +00003779#ifdef SQLITE_DEBUG
3780 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
3781#endif
danielk1977452c9892004-05-13 05:16:15 +00003782
danielk1977de630352009-05-04 11:42:29 +00003783 /* Extract the value of R from register P3. */
3784 sqlite3VdbeMemIntegerify(pIn3);
3785 R = pIn3->u.i;
3786
3787 /* Search the B-Tree index. If no conflicting record is found, jump
3788 ** to P2. Otherwise, copy the rowid of the conflicting record to
3789 ** register P3 and fall through to the next instruction. */
3790 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &pCx->seekResult);
3791 if( (r.flags & UNPACKED_PREFIX_SEARCH) || r.rowid==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003792 pc = pOp->p2 - 1;
danielk1977de630352009-05-04 11:42:29 +00003793 }else{
3794 pIn3->u.i = r.rowid;
drh9cfcf5d2002-01-29 18:41:24 +00003795 }
drh9cfcf5d2002-01-29 18:41:24 +00003796 }
3797 break;
3798}
3799
drh9cbf3422008-01-17 16:22:13 +00003800/* Opcode: NotExists P1 P2 P3 * *
drh6b125452002-01-28 15:53:03 +00003801**
drhef8662b2011-06-20 21:47:58 +00003802** Use the content of register P3 as an integer key. If a record
danielk197796cb76f2008-01-04 13:24:28 +00003803** with that key does not exist in table of P1, then jump to P2.
drh710c4842010-08-30 01:17:20 +00003804** If the record does exist, then fall through. The cursor is left
drh9cbf3422008-01-17 16:22:13 +00003805** pointing to the record if it exists.
drh6b125452002-01-28 15:53:03 +00003806**
3807** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003808** operation assumes the key is an integer and that P1 is a table whereas
3809** NotFound assumes key is a blob constructed from MakeRecord and
3810** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003811**
drhcb6d50e2008-08-21 19:28:30 +00003812** See also: Found, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003813*/
drh9cbf3422008-01-17 16:22:13 +00003814case OP_NotExists: { /* jump, in3 */
drhdfe88ec2008-11-03 20:55:06 +00003815 VdbeCursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003816 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00003817 int res;
3818 u64 iKey;
3819
drh3c657212009-11-17 23:59:58 +00003820 pIn3 = &aMem[pOp->p3];
drhaa736092009-06-22 00:55:30 +00003821 assert( pIn3->flags & MEM_Int );
3822 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3823 pC = p->apCsr[pOp->p1];
3824 assert( pC!=0 );
3825 assert( pC->isTable );
drh3e9ca092009-09-08 01:14:48 +00003826 assert( pC->pseudoTableReg==0 );
drhaa736092009-06-22 00:55:30 +00003827 pCrsr = pC->pCursor;
dana205a482011-08-27 18:48:57 +00003828 if( ALWAYS(pCrsr!=0) ){
drh856c1032009-06-02 15:21:42 +00003829 res = 0;
drhaa736092009-06-22 00:55:30 +00003830 iKey = pIn3->u.i;
danielk1977de630352009-05-04 11:42:29 +00003831 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
drh98757152008-01-09 23:04:12 +00003832 pC->lastRowid = pIn3->u.i;
drh9c1905f2008-12-10 22:32:56 +00003833 pC->rowidIsValid = res==0 ?1:0;
drh9188b382004-05-14 21:12:22 +00003834 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003835 pC->cacheStatus = CACHE_STALE;
danielk19771d461462009-04-21 09:02:45 +00003836 pC->deferredMoveto = 0;
danielk197728129562005-01-11 10:25:06 +00003837 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003838 pc = pOp->p2 - 1;
drh91fd4d42008-01-19 20:11:25 +00003839 assert( pC->rowidIsValid==0 );
drh6b125452002-01-28 15:53:03 +00003840 }
danielk1977de630352009-05-04 11:42:29 +00003841 pC->seekResult = res;
drhaa736092009-06-22 00:55:30 +00003842 }else{
danielk1977f7b9d662008-06-23 18:49:43 +00003843 /* This happens when an attempt to open a read cursor on the
3844 ** sqlite_master table returns SQLITE_EMPTY.
3845 */
danielk1977f7b9d662008-06-23 18:49:43 +00003846 pc = pOp->p2 - 1;
3847 assert( pC->rowidIsValid==0 );
danielk1977de630352009-05-04 11:42:29 +00003848 pC->seekResult = 0;
drh6b125452002-01-28 15:53:03 +00003849 }
drh6b125452002-01-28 15:53:03 +00003850 break;
3851}
3852
drh4c583122008-01-04 22:01:03 +00003853/* Opcode: Sequence P1 P2 * * *
drh4db38a72005-09-01 12:16:28 +00003854**
drh4c583122008-01-04 22:01:03 +00003855** Find the next available sequence number for cursor P1.
drh9cbf3422008-01-17 16:22:13 +00003856** Write the sequence number into register P2.
drh4c583122008-01-04 22:01:03 +00003857** The sequence number on the cursor is incremented after this
3858** instruction.
drh4db38a72005-09-01 12:16:28 +00003859*/
drh4c583122008-01-04 22:01:03 +00003860case OP_Sequence: { /* out2-prerelease */
drh653b82a2009-06-22 11:10:47 +00003861 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3862 assert( p->apCsr[pOp->p1]!=0 );
3863 pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
drh4db38a72005-09-01 12:16:28 +00003864 break;
3865}
3866
3867
drh98757152008-01-09 23:04:12 +00003868/* Opcode: NewRowid P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00003869**
drhf0863fe2005-06-12 21:35:51 +00003870** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003871** The record number is not previously used as a key in the database
drh9cbf3422008-01-17 16:22:13 +00003872** table that cursor P1 points to. The new record number is written
3873** written to register P2.
drh205f48e2004-11-05 00:43:11 +00003874**
dan76d462e2009-08-30 11:42:51 +00003875** If P3>0 then P3 is a register in the root frame of this VDBE that holds
3876** the largest previously generated record number. No new record numbers are
3877** allowed to be less than this value. When this value reaches its maximum,
drhef8662b2011-06-20 21:47:58 +00003878** an SQLITE_FULL error is generated. The P3 register is updated with the '
dan76d462e2009-08-30 11:42:51 +00003879** generated record number. This P3 mechanism is used to help implement the
drh205f48e2004-11-05 00:43:11 +00003880** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003881*/
drh4c583122008-01-04 22:01:03 +00003882case OP_NewRowid: { /* out2-prerelease */
drhaa736092009-06-22 00:55:30 +00003883 i64 v; /* The new rowid */
3884 VdbeCursor *pC; /* Cursor of table to get the new rowid */
3885 int res; /* Result of an sqlite3BtreeLast() */
3886 int cnt; /* Counter to limit the number of searches */
3887 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
dan76d462e2009-08-30 11:42:51 +00003888 VdbeFrame *pFrame; /* Root frame of VDBE */
drh856c1032009-06-02 15:21:42 +00003889
drh856c1032009-06-02 15:21:42 +00003890 v = 0;
3891 res = 0;
drhaa736092009-06-22 00:55:30 +00003892 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3893 pC = p->apCsr[pOp->p1];
3894 assert( pC!=0 );
3895 if( NEVER(pC->pCursor==0) ){
drhf328bc82004-05-10 23:29:49 +00003896 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003897 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003898 /* The next rowid or record number (different terms for the same
3899 ** thing) is obtained in a two-step algorithm.
3900 **
3901 ** First we attempt to find the largest existing rowid and add one
3902 ** to that. But if the largest existing rowid is already the maximum
3903 ** positive integer, we have to fall through to the second
3904 ** probabilistic algorithm
3905 **
3906 ** The second algorithm is to select a rowid at random and see if
3907 ** it already exists in the table. If it does not exist, we have
3908 ** succeeded. If the random rowid does exist, we select a new one
drhaa736092009-06-22 00:55:30 +00003909 ** and try again, up to 100 times.
drhdb5ed6d2001-09-18 22:17:44 +00003910 */
drhaa736092009-06-22 00:55:30 +00003911 assert( pC->isTable );
drhfe2093d2005-01-20 22:48:47 +00003912
drh75f86a42005-02-17 00:03:06 +00003913#ifdef SQLITE_32BIT_ROWID
3914# define MAX_ROWID 0x7fffffff
3915#else
drhfe2093d2005-01-20 22:48:47 +00003916 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3917 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3918 ** to provide the constant while making all compilers happy.
3919 */
danielk197764202cf2008-11-17 15:31:47 +00003920# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
drh75f86a42005-02-17 00:03:06 +00003921#endif
drhfe2093d2005-01-20 22:48:47 +00003922
drh5cf8e8c2002-02-19 22:42:05 +00003923 if( !pC->useRandomRowid ){
drh7f751222009-03-17 22:33:00 +00003924 v = sqlite3BtreeGetCachedRowid(pC->pCursor);
3925 if( v==0 ){
danielk1977261919c2005-12-06 12:52:59 +00003926 rc = sqlite3BtreeLast(pC->pCursor, &res);
3927 if( rc!=SQLITE_OK ){
3928 goto abort_due_to_error;
3929 }
drh32fbe342002-10-19 20:16:37 +00003930 if( res ){
drhc79c7612010-01-01 18:57:48 +00003931 v = 1; /* IMP: R-61914-48074 */
drh5cf8e8c2002-02-19 22:42:05 +00003932 }else{
drhea8ffdf2009-07-22 00:35:23 +00003933 assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
drhc27ae612009-07-14 18:35:44 +00003934 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
3935 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
drha40eb7c2012-02-24 00:02:28 +00003936 if( v>=MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003937 pC->useRandomRowid = 1;
3938 }else{
drhc79c7612010-01-01 18:57:48 +00003939 v++; /* IMP: R-29538-34987 */
drh32fbe342002-10-19 20:16:37 +00003940 }
drh5cf8e8c2002-02-19 22:42:05 +00003941 }
drh3fc190c2001-09-14 03:24:23 +00003942 }
drh205f48e2004-11-05 00:43:11 +00003943
3944#ifndef SQLITE_OMIT_AUTOINCREMENT
drh4c583122008-01-04 22:01:03 +00003945 if( pOp->p3 ){
shaneabc6b892009-09-10 19:09:03 +00003946 /* Assert that P3 is a valid memory cell. */
3947 assert( pOp->p3>0 );
dan76d462e2009-08-30 11:42:51 +00003948 if( p->pFrame ){
3949 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
shaneabc6b892009-09-10 19:09:03 +00003950 /* Assert that P3 is a valid memory cell. */
3951 assert( pOp->p3<=pFrame->nMem );
dan76d462e2009-08-30 11:42:51 +00003952 pMem = &pFrame->aMem[pOp->p3];
3953 }else{
shaneabc6b892009-09-10 19:09:03 +00003954 /* Assert that P3 is a valid memory cell. */
3955 assert( pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00003956 pMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00003957 memAboutToChange(p, pMem);
dan76d462e2009-08-30 11:42:51 +00003958 }
drh2b4ded92010-09-27 21:09:31 +00003959 assert( memIsValid(pMem) );
dan76d462e2009-08-30 11:42:51 +00003960
3961 REGISTER_TRACE(pOp->p3, pMem);
drh8a512562005-11-14 22:29:05 +00003962 sqlite3VdbeMemIntegerify(pMem);
drh4c583122008-01-04 22:01:03 +00003963 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
drh3c024d62007-03-30 11:23:45 +00003964 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
drhc79c7612010-01-01 18:57:48 +00003965 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
drh205f48e2004-11-05 00:43:11 +00003966 goto abort_due_to_error;
3967 }
drh3c024d62007-03-30 11:23:45 +00003968 if( v<pMem->u.i+1 ){
3969 v = pMem->u.i + 1;
drh205f48e2004-11-05 00:43:11 +00003970 }
drh3c024d62007-03-30 11:23:45 +00003971 pMem->u.i = v;
drh205f48e2004-11-05 00:43:11 +00003972 }
3973#endif
3974
drh7f751222009-03-17 22:33:00 +00003975 sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
drh5cf8e8c2002-02-19 22:42:05 +00003976 }
3977 if( pC->useRandomRowid ){
drh748a52c2010-09-01 11:50:08 +00003978 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
drhc79c7612010-01-01 18:57:48 +00003979 ** largest possible integer (9223372036854775807) then the database
drh748a52c2010-09-01 11:50:08 +00003980 ** engine starts picking positive candidate ROWIDs at random until
3981 ** it finds one that is not previously used. */
drhaa736092009-06-22 00:55:30 +00003982 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
3983 ** an AUTOINCREMENT table. */
shanehc4d340a2010-09-01 02:37:56 +00003984 /* on the first attempt, simply do one more than previous */
drh99a66922011-05-13 18:51:42 +00003985 v = lastRowid;
shanehc4d340a2010-09-01 02:37:56 +00003986 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
3987 v++; /* ensure non-zero */
drh5cf8e8c2002-02-19 22:42:05 +00003988 cnt = 0;
drh748a52c2010-09-01 11:50:08 +00003989 while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
3990 0, &res))==SQLITE_OK)
shanehc4d340a2010-09-01 02:37:56 +00003991 && (res==0)
3992 && (++cnt<100)){
3993 /* collision - try another random rowid */
3994 sqlite3_randomness(sizeof(v), &v);
3995 if( cnt<5 ){
3996 /* try "small" random rowids for the initial attempts */
3997 v &= 0xffffff;
drh91fd4d42008-01-19 20:11:25 +00003998 }else{
shanehc4d340a2010-09-01 02:37:56 +00003999 v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
drh5cf8e8c2002-02-19 22:42:05 +00004000 }
shanehc4d340a2010-09-01 02:37:56 +00004001 v++; /* ensure non-zero */
4002 }
drhaa736092009-06-22 00:55:30 +00004003 if( rc==SQLITE_OK && res==0 ){
drhc79c7612010-01-01 18:57:48 +00004004 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
drh5cf8e8c2002-02-19 22:42:05 +00004005 goto abort_due_to_error;
4006 }
drh748a52c2010-09-01 11:50:08 +00004007 assert( v>0 ); /* EV: R-40812-03570 */
drh1eaa2692001-09-18 02:02:23 +00004008 }
drhf0863fe2005-06-12 21:35:51 +00004009 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00004010 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004011 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004012 }
drh4c583122008-01-04 22:01:03 +00004013 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004014 break;
4015}
4016
danielk19771f4aa332008-01-03 09:51:55 +00004017/* Opcode: Insert P1 P2 P3 P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004018**
jplyon5a564222003-06-02 06:15:58 +00004019** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00004020** created if it doesn't already exist or the data for an existing
drh3e9ca092009-09-08 01:14:48 +00004021** entry is overwritten. The data is the value MEM_Blob stored in register
danielk19771f4aa332008-01-03 09:51:55 +00004022** number P2. The key is stored in register P3. The key must
drh3e9ca092009-09-08 01:14:48 +00004023** be a MEM_Int.
drh4a324312001-12-21 14:30:42 +00004024**
danielk19771f4aa332008-01-03 09:51:55 +00004025** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
4026** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set,
danielk1977b28af712004-06-21 06:50:26 +00004027** then rowid is stored for subsequent return by the
drh85b623f2007-12-13 21:54:09 +00004028** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
drh6b125452002-01-28 15:53:03 +00004029**
drh3e9ca092009-09-08 01:14:48 +00004030** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
4031** the last seek operation (OP_NotExists) was a success, then this
4032** operation will not attempt to find the appropriate row before doing
4033** the insert but will instead overwrite the row that the cursor is
4034** currently pointing to. Presumably, the prior OP_NotExists opcode
4035** has already positioned the cursor correctly. This is an optimization
4036** that boosts performance by avoiding redundant seeks.
4037**
4038** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
4039** UPDATE operation. Otherwise (if the flag is clear) then this opcode
4040** is part of an INSERT operation. The difference is only important to
4041** the update hook.
4042**
drh66a51672008-01-03 00:01:23 +00004043** Parameter P4 may point to a string containing the table-name, or
danielk19771f6eec52006-06-16 06:17:47 +00004044** may be NULL. If it is not NULL, then the update-hook
4045** (sqlite3.xUpdateCallback) is invoked following a successful insert.
4046**
drh93aed5a2008-01-16 17:46:38 +00004047** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
4048** allocated, then ownership of P2 is transferred to the pseudo-cursor
4049** and register P2 becomes ephemeral. If the cursor is changed, the
4050** value of register P2 will then change. Make sure this does not
4051** cause any problems.)
4052**
drhf0863fe2005-06-12 21:35:51 +00004053** This instruction only works on tables. The equivalent instruction
4054** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00004055*/
drhe05c9292009-10-29 13:48:10 +00004056/* Opcode: InsertInt P1 P2 P3 P4 P5
4057**
4058** This works exactly like OP_Insert except that the key is the
4059** integer value P3, not the value of the integer stored in register P3.
4060*/
4061case OP_Insert:
4062case OP_InsertInt: {
drh3e9ca092009-09-08 01:14:48 +00004063 Mem *pData; /* MEM cell holding data for the record to be inserted */
4064 Mem *pKey; /* MEM cell holding key for the record */
4065 i64 iKey; /* The integer ROWID or key for the record to be inserted */
4066 VdbeCursor *pC; /* Cursor to table into which insert is written */
4067 int nZero; /* Number of zero-bytes to append */
4068 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
4069 const char *zDb; /* database name - used by the update hook */
4070 const char *zTbl; /* Table name - used by the opdate hook */
4071 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
drh856c1032009-06-02 15:21:42 +00004072
drha6c2ed92009-11-14 23:22:23 +00004073 pData = &aMem[pOp->p2];
drh653b82a2009-06-22 11:10:47 +00004074 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh2b4ded92010-09-27 21:09:31 +00004075 assert( memIsValid(pData) );
drh653b82a2009-06-22 11:10:47 +00004076 pC = p->apCsr[pOp->p1];
drha05a7222008-01-19 03:35:58 +00004077 assert( pC!=0 );
drh3e9ca092009-09-08 01:14:48 +00004078 assert( pC->pCursor!=0 );
4079 assert( pC->pseudoTableReg==0 );
drha05a7222008-01-19 03:35:58 +00004080 assert( pC->isTable );
drh5b6afba2008-01-05 16:29:28 +00004081 REGISTER_TRACE(pOp->p2, pData);
danielk19775f8d8a82004-05-11 00:28:42 +00004082
drhe05c9292009-10-29 13:48:10 +00004083 if( pOp->opcode==OP_Insert ){
drha6c2ed92009-11-14 23:22:23 +00004084 pKey = &aMem[pOp->p3];
drhe05c9292009-10-29 13:48:10 +00004085 assert( pKey->flags & MEM_Int );
drh2b4ded92010-09-27 21:09:31 +00004086 assert( memIsValid(pKey) );
drhe05c9292009-10-29 13:48:10 +00004087 REGISTER_TRACE(pOp->p3, pKey);
4088 iKey = pKey->u.i;
4089 }else{
4090 assert( pOp->opcode==OP_InsertInt );
4091 iKey = pOp->p3;
4092 }
4093
drha05a7222008-01-19 03:35:58 +00004094 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
drh99a66922011-05-13 18:51:42 +00004095 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
drha05a7222008-01-19 03:35:58 +00004096 if( pData->flags & MEM_Null ){
4097 pData->z = 0;
4098 pData->n = 0;
4099 }else{
4100 assert( pData->flags & (MEM_Blob|MEM_Str) );
4101 }
drh3e9ca092009-09-08 01:14:48 +00004102 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
4103 if( pData->flags & MEM_Zero ){
4104 nZero = pData->u.nZero;
drha05a7222008-01-19 03:35:58 +00004105 }else{
drh3e9ca092009-09-08 01:14:48 +00004106 nZero = 0;
drha05a7222008-01-19 03:35:58 +00004107 }
drh3e9ca092009-09-08 01:14:48 +00004108 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
4109 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
4110 pData->z, pData->n, nZero,
4111 pOp->p5 & OPFLAG_APPEND, seekResult
4112 );
drha05a7222008-01-19 03:35:58 +00004113 pC->rowidIsValid = 0;
4114 pC->deferredMoveto = 0;
4115 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004116
drha05a7222008-01-19 03:35:58 +00004117 /* Invoke the update-hook if required. */
4118 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
drh856c1032009-06-02 15:21:42 +00004119 zDb = db->aDb[pC->iDb].zName;
4120 zTbl = pOp->p4.z;
4121 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
drha05a7222008-01-19 03:35:58 +00004122 assert( pC->isTable );
4123 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
4124 assert( pC->iDb>=0 );
4125 }
drh5e00f6c2001-09-13 13:46:56 +00004126 break;
4127}
4128
drh98757152008-01-09 23:04:12 +00004129/* Opcode: Delete P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00004130**
drh5edc3122001-09-13 21:53:09 +00004131** Delete the record at which the P1 cursor is currently pointing.
4132**
4133** The cursor will be left pointing at either the next or the previous
4134** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00004135** the next Next instruction will be a no-op. Hence it is OK to delete
4136** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00004137**
rdcb0c374f2004-02-20 22:53:38 +00004138** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00004139** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00004140**
drh91fd4d42008-01-19 20:11:25 +00004141** P1 must not be pseudo-table. It has to be a real table with
4142** multiple rows.
4143**
4144** If P4 is not NULL, then it is the name of the table that P1 is
4145** pointing to. The update hook will be invoked, if it exists.
4146** If P4 is not NULL then the P1 cursor must have been positioned
4147** using OP_NotFound prior to invoking this opcode.
drh5e00f6c2001-09-13 13:46:56 +00004148*/
drh9cbf3422008-01-17 16:22:13 +00004149case OP_Delete: {
drh856c1032009-06-02 15:21:42 +00004150 i64 iKey;
drhdfe88ec2008-11-03 20:55:06 +00004151 VdbeCursor *pC;
drh91fd4d42008-01-19 20:11:25 +00004152
drh856c1032009-06-02 15:21:42 +00004153 iKey = 0;
drh653b82a2009-06-22 11:10:47 +00004154 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4155 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004156 assert( pC!=0 );
drh91fd4d42008-01-19 20:11:25 +00004157 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
danielk197794eb6a12005-12-15 15:22:08 +00004158
drh91fd4d42008-01-19 20:11:25 +00004159 /* If the update-hook will be invoked, set iKey to the rowid of the
4160 ** row being deleted.
4161 */
4162 if( db->xUpdateCallback && pOp->p4.z ){
4163 assert( pC->isTable );
4164 assert( pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
4165 iKey = pC->lastRowid;
4166 }
danielk197794eb6a12005-12-15 15:22:08 +00004167
drh9a65f2c2009-06-22 19:05:40 +00004168 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
4169 ** OP_Column on the same table without any intervening operations that
4170 ** might move or invalidate the cursor. Hence cursor pC is always pointing
4171 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
4172 ** below is always a no-op and cannot fail. We will run it anyhow, though,
4173 ** to guard against future changes to the code generator.
4174 **/
4175 assert( pC->deferredMoveto==0 );
drh91fd4d42008-01-19 20:11:25 +00004176 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004177 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4178
drh7f751222009-03-17 22:33:00 +00004179 sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
drh91fd4d42008-01-19 20:11:25 +00004180 rc = sqlite3BtreeDelete(pC->pCursor);
drh91fd4d42008-01-19 20:11:25 +00004181 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00004182
drh91fd4d42008-01-19 20:11:25 +00004183 /* Invoke the update-hook if required. */
4184 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
4185 const char *zDb = db->aDb[pC->iDb].zName;
4186 const char *zTbl = pOp->p4.z;
4187 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4188 assert( pC->iDb>=0 );
drh5e00f6c2001-09-13 13:46:56 +00004189 }
danielk1977b28af712004-06-21 06:50:26 +00004190 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00004191 break;
4192}
drhb7f1d9a2009-09-08 02:27:58 +00004193/* Opcode: ResetCount * * * * *
rdcb0c374f2004-02-20 22:53:38 +00004194**
drhb7f1d9a2009-09-08 02:27:58 +00004195** The value of the change counter is copied to the database handle
4196** change counter (returned by subsequent calls to sqlite3_changes()).
4197** Then the VMs internal change counter resets to 0.
4198** This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00004199*/
drh9cbf3422008-01-17 16:22:13 +00004200case OP_ResetCount: {
drhb7f1d9a2009-09-08 02:27:58 +00004201 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00004202 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00004203 break;
4204}
4205
dan5134d132011-09-02 10:31:11 +00004206/* Opcode: SorterCompare P1 P2 P3
4207**
4208** P1 is a sorter cursor. This instruction compares the record blob in
4209** register P3 with the entry that the sorter cursor currently points to.
4210** If, excluding the rowid fields at the end, the two records are a match,
4211** fall through to the next instruction. Otherwise, jump to instruction P2.
4212*/
4213case OP_SorterCompare: {
4214 VdbeCursor *pC;
4215 int res;
4216
4217 pC = p->apCsr[pOp->p1];
4218 assert( isSorter(pC) );
4219 pIn3 = &aMem[pOp->p3];
4220 rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
4221 if( res ){
4222 pc = pOp->p2-1;
4223 }
4224 break;
4225};
4226
4227/* Opcode: SorterData P1 P2 * * *
4228**
4229** Write into register P2 the current sorter data for sorter cursor P1.
4230*/
4231case OP_SorterData: {
4232 VdbeCursor *pC;
drh3a949872012-09-18 13:20:13 +00004233
dan5134d132011-09-02 10:31:11 +00004234 pOut = &aMem[pOp->p2];
4235 pC = p->apCsr[pOp->p1];
4236 assert( pC->isSorter );
4237 rc = sqlite3VdbeSorterRowkey(pC, pOut);
4238 break;
4239}
4240
drh98757152008-01-09 23:04:12 +00004241/* Opcode: RowData P1 P2 * * *
drh70ce3f02003-04-15 19:22:22 +00004242**
drh98757152008-01-09 23:04:12 +00004243** Write into register P2 the complete row data for cursor P1.
4244** There is no interpretation of the data.
4245** It is just copied onto the P2 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004246** it is found in the database file.
drh70ce3f02003-04-15 19:22:22 +00004247**
drhde4fcfd2008-01-19 23:50:26 +00004248** If the P1 cursor must be pointing to a valid row (not a NULL row)
4249** of a real table, not a pseudo-table.
drh70ce3f02003-04-15 19:22:22 +00004250*/
drh98757152008-01-09 23:04:12 +00004251/* Opcode: RowKey P1 P2 * * *
drh143f3c42004-01-07 20:37:52 +00004252**
drh98757152008-01-09 23:04:12 +00004253** Write into register P2 the complete row key for cursor P1.
4254** There is no interpretation of the data.
drh9cbf3422008-01-17 16:22:13 +00004255** The key is copied onto the P3 register exactly as
danielk197796cb76f2008-01-04 13:24:28 +00004256** it is found in the database file.
drh143f3c42004-01-07 20:37:52 +00004257**
drhde4fcfd2008-01-19 23:50:26 +00004258** If the P1 cursor must be pointing to a valid row (not a NULL row)
4259** of a real table, not a pseudo-table.
drh143f3c42004-01-07 20:37:52 +00004260*/
danielk1977a7a8e142008-02-13 18:25:27 +00004261case OP_RowKey:
4262case OP_RowData: {
drhdfe88ec2008-11-03 20:55:06 +00004263 VdbeCursor *pC;
drhde4fcfd2008-01-19 23:50:26 +00004264 BtCursor *pCrsr;
danielk1977e0d4b062004-06-28 01:11:46 +00004265 u32 n;
drh856c1032009-06-02 15:21:42 +00004266 i64 n64;
drh70ce3f02003-04-15 19:22:22 +00004267
drha6c2ed92009-11-14 23:22:23 +00004268 pOut = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004269 memAboutToChange(p, pOut);
danielk1977a7a8e142008-02-13 18:25:27 +00004270
drhf0863fe2005-06-12 21:35:51 +00004271 /* Note that RowKey and RowData are really exactly the same instruction */
drh653b82a2009-06-22 11:10:47 +00004272 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4273 pC = p->apCsr[pOp->p1];
dan5134d132011-09-02 10:31:11 +00004274 assert( pC->isSorter==0 );
drhc6aff302011-09-01 15:32:47 +00004275 assert( pC->isTable || pOp->opcode!=OP_RowData );
drhf0863fe2005-06-12 21:35:51 +00004276 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00004277 assert( pC!=0 );
drhde4fcfd2008-01-19 23:50:26 +00004278 assert( pC->nullRow==0 );
drh3e9ca092009-09-08 01:14:48 +00004279 assert( pC->pseudoTableReg==0 );
drhde4fcfd2008-01-19 23:50:26 +00004280 assert( pC->pCursor!=0 );
4281 pCrsr = pC->pCursor;
drhea8ffdf2009-07-22 00:35:23 +00004282 assert( sqlite3BtreeCursorIsValid(pCrsr) );
drh9a65f2c2009-06-22 19:05:40 +00004283
4284 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
4285 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
4286 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
4287 ** a no-op and can never fail. But we leave it in place as a safety.
4288 */
4289 assert( pC->deferredMoveto==0 );
drhde4fcfd2008-01-19 23:50:26 +00004290 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004291 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
4292
drhde4fcfd2008-01-19 23:50:26 +00004293 if( pC->isIndex ){
drhde4fcfd2008-01-19 23:50:26 +00004294 assert( !pC->isTable );
drhb07028f2011-10-14 21:49:18 +00004295 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
drhc27ae612009-07-14 18:35:44 +00004296 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
drhbb4957f2008-03-20 14:03:29 +00004297 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
drhde4fcfd2008-01-19 23:50:26 +00004298 goto too_big;
drh70ce3f02003-04-15 19:22:22 +00004299 }
drhbfb19dc2009-06-05 16:46:53 +00004300 n = (u32)n64;
drhde4fcfd2008-01-19 23:50:26 +00004301 }else{
drhb07028f2011-10-14 21:49:18 +00004302 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
drhea8ffdf2009-07-22 00:35:23 +00004303 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
shane75ac1de2009-06-09 18:58:52 +00004304 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
drh023ae032007-05-08 12:12:16 +00004305 goto too_big;
4306 }
drhde4fcfd2008-01-19 23:50:26 +00004307 }
danielk1977a7a8e142008-02-13 18:25:27 +00004308 if( sqlite3VdbeMemGrow(pOut, n, 0) ){
4309 goto no_mem;
drhde4fcfd2008-01-19 23:50:26 +00004310 }
danielk1977a7a8e142008-02-13 18:25:27 +00004311 pOut->n = n;
4312 MemSetTypeFlag(pOut, MEM_Blob);
drhde4fcfd2008-01-19 23:50:26 +00004313 if( pC->isIndex ){
4314 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
4315 }else{
4316 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
drh5e00f6c2001-09-13 13:46:56 +00004317 }
danielk197796cb76f2008-01-04 13:24:28 +00004318 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drhb7654112008-01-12 12:48:07 +00004319 UPDATE_MAX_BLOBSIZE(pOut);
drh5e00f6c2001-09-13 13:46:56 +00004320 break;
4321}
4322
drh2133d822008-01-03 18:44:59 +00004323/* Opcode: Rowid P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004324**
drh2133d822008-01-03 18:44:59 +00004325** Store in register P2 an integer which is the key of the table entry that
drhbfdc7542008-05-29 03:12:54 +00004326** P1 is currently point to.
drh044925b2009-04-22 17:15:02 +00004327**
4328** P1 can be either an ordinary table or a virtual table. There used to
4329** be a separate OP_VRowid opcode for use with virtual tables, but this
4330** one opcode now works for both table types.
drh5e00f6c2001-09-13 13:46:56 +00004331*/
drh4c583122008-01-04 22:01:03 +00004332case OP_Rowid: { /* out2-prerelease */
drhdfe88ec2008-11-03 20:55:06 +00004333 VdbeCursor *pC;
drhf328bc82004-05-10 23:29:49 +00004334 i64 v;
drh856c1032009-06-02 15:21:42 +00004335 sqlite3_vtab *pVtab;
4336 const sqlite3_module *pModule;
drh5e00f6c2001-09-13 13:46:56 +00004337
drh653b82a2009-06-22 11:10:47 +00004338 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4339 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004340 assert( pC!=0 );
drh21172c42012-10-30 00:29:07 +00004341 assert( pC->pseudoTableReg==0 || pC->nullRow );
drh044925b2009-04-22 17:15:02 +00004342 if( pC->nullRow ){
drh3c657212009-11-17 23:59:58 +00004343 pOut->flags = MEM_Null;
drh044925b2009-04-22 17:15:02 +00004344 break;
4345 }else if( pC->deferredMoveto ){
drh61495262009-04-22 15:32:59 +00004346 v = pC->movetoTarget;
drh044925b2009-04-22 17:15:02 +00004347#ifndef SQLITE_OMIT_VIRTUALTABLE
4348 }else if( pC->pVtabCursor ){
drh044925b2009-04-22 17:15:02 +00004349 pVtab = pC->pVtabCursor->pVtab;
4350 pModule = pVtab->pModule;
4351 assert( pModule->xRowid );
drh044925b2009-04-22 17:15:02 +00004352 rc = pModule->xRowid(pC->pVtabCursor, &v);
drhb9755982010-07-24 16:34:37 +00004353 importVtabErrMsg(p, pVtab);
drh044925b2009-04-22 17:15:02 +00004354#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh70ce3f02003-04-15 19:22:22 +00004355 }else{
drh6be240e2009-07-14 02:33:02 +00004356 assert( pC->pCursor!=0 );
drh61495262009-04-22 15:32:59 +00004357 rc = sqlite3VdbeCursorMoveto(pC);
4358 if( rc ) goto abort_due_to_error;
4359 if( pC->rowidIsValid ){
4360 v = pC->lastRowid;
drh61495262009-04-22 15:32:59 +00004361 }else{
drhc27ae612009-07-14 18:35:44 +00004362 rc = sqlite3BtreeKeySize(pC->pCursor, &v);
4363 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
drh61495262009-04-22 15:32:59 +00004364 }
drh5e00f6c2001-09-13 13:46:56 +00004365 }
drh4c583122008-01-04 22:01:03 +00004366 pOut->u.i = v;
drh5e00f6c2001-09-13 13:46:56 +00004367 break;
4368}
4369
drh9cbf3422008-01-17 16:22:13 +00004370/* Opcode: NullRow P1 * * * *
drh17f71932002-02-21 12:01:27 +00004371**
4372** Move the cursor P1 to a null row. Any OP_Column operations
drh9cbf3422008-01-17 16:22:13 +00004373** that occur while the cursor is on the null row will always
4374** write a NULL.
drh17f71932002-02-21 12:01:27 +00004375*/
drh9cbf3422008-01-17 16:22:13 +00004376case OP_NullRow: {
drhdfe88ec2008-11-03 20:55:06 +00004377 VdbeCursor *pC;
drh17f71932002-02-21 12:01:27 +00004378
drh653b82a2009-06-22 11:10:47 +00004379 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4380 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004381 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00004382 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00004383 pC->rowidIsValid = 0;
dana205a482011-08-27 18:48:57 +00004384 assert( pC->pCursor || pC->pVtabCursor );
danielk1977be51a652008-10-08 17:58:48 +00004385 if( pC->pCursor ){
4386 sqlite3BtreeClearCursor(pC->pCursor);
4387 }
drh17f71932002-02-21 12:01:27 +00004388 break;
4389}
4390
drh9cbf3422008-01-17 16:22:13 +00004391/* Opcode: Last P1 P2 * * *
drh9562b552002-02-19 15:00:07 +00004392**
drhf0863fe2005-06-12 21:35:51 +00004393** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00004394** will refer to the last entry in the database table or index.
4395** If the table or index is empty and P2>0, then jump immediately to P2.
4396** If P2 is 0 or if the table or index is not empty, fall through
4397** to the following instruction.
4398*/
drh9cbf3422008-01-17 16:22:13 +00004399case OP_Last: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004400 VdbeCursor *pC;
drh9562b552002-02-19 15:00:07 +00004401 BtCursor *pCrsr;
drha05a7222008-01-19 03:35:58 +00004402 int res;
drh9562b552002-02-19 15:00:07 +00004403
drh653b82a2009-06-22 11:10:47 +00004404 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4405 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004406 assert( pC!=0 );
drha05a7222008-01-19 03:35:58 +00004407 pCrsr = pC->pCursor;
drh7abc5402011-10-22 21:00:46 +00004408 res = 0;
4409 if( ALWAYS(pCrsr!=0) ){
drh9a65f2c2009-06-22 19:05:40 +00004410 rc = sqlite3BtreeLast(pCrsr, &res);
4411 }
drh9c1905f2008-12-10 22:32:56 +00004412 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004413 pC->deferredMoveto = 0;
drha7e77062009-01-14 00:55:09 +00004414 pC->rowidIsValid = 0;
drha05a7222008-01-19 03:35:58 +00004415 pC->cacheStatus = CACHE_STALE;
drh9a65f2c2009-06-22 19:05:40 +00004416 if( pOp->p2>0 && res ){
drha05a7222008-01-19 03:35:58 +00004417 pc = pOp->p2 - 1;
drh9562b552002-02-19 15:00:07 +00004418 }
4419 break;
4420}
4421
drh0342b1f2005-09-01 03:07:44 +00004422
drh9cbf3422008-01-17 16:22:13 +00004423/* Opcode: Sort P1 P2 * * *
drh0342b1f2005-09-01 03:07:44 +00004424**
4425** This opcode does exactly the same thing as OP_Rewind except that
4426** it increments an undocumented global variable used for testing.
4427**
4428** Sorting is accomplished by writing records into a sorting index,
4429** then rewinding that index and playing it back from beginning to
4430** end. We use the OP_Sort opcode instead of OP_Rewind to do the
4431** rewinding so that the global variable will be incremented and
4432** regression tests can determine whether or not the optimizer is
4433** correctly optimizing out sorts.
4434*/
drhc6aff302011-09-01 15:32:47 +00004435case OP_SorterSort: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004436case OP_Sort: { /* jump */
drh0f7eb612006-08-08 13:51:43 +00004437#ifdef SQLITE_TEST
drh0342b1f2005-09-01 03:07:44 +00004438 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00004439 sqlite3_search_count--;
drh0f7eb612006-08-08 13:51:43 +00004440#endif
drh9b47ee32013-08-20 03:13:51 +00004441 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
drh0342b1f2005-09-01 03:07:44 +00004442 /* Fall through into OP_Rewind */
4443}
drh9cbf3422008-01-17 16:22:13 +00004444/* Opcode: Rewind P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00004445**
drhf0863fe2005-06-12 21:35:51 +00004446** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00004447** will refer to the first entry in the database table or index.
4448** If the table or index is empty and P2>0, then jump immediately to P2.
4449** If P2 is 0 or if the table or index is not empty, fall through
4450** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00004451*/
drh9cbf3422008-01-17 16:22:13 +00004452case OP_Rewind: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004453 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004454 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00004455 int res;
drh5e00f6c2001-09-13 13:46:56 +00004456
drh653b82a2009-06-22 11:10:47 +00004457 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4458 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00004459 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004460 assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
dan2411dea2010-07-03 05:56:09 +00004461 res = 1;
dan689ab892011-08-12 15:02:00 +00004462 if( isSorter(pC) ){
dana20fde62011-07-12 14:28:05 +00004463 rc = sqlite3VdbeSorterRewind(db, pC, &res);
dana205a482011-08-27 18:48:57 +00004464 }else{
4465 pCrsr = pC->pCursor;
4466 assert( pCrsr );
danielk19774adee202004-05-08 08:23:19 +00004467 rc = sqlite3BtreeFirst(pCrsr, &res);
drh9c1905f2008-12-10 22:32:56 +00004468 pC->atFirst = res==0 ?1:0;
drha11846b2004-01-07 18:52:56 +00004469 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00004470 pC->cacheStatus = CACHE_STALE;
drha7e77062009-01-14 00:55:09 +00004471 pC->rowidIsValid = 0;
drhf4dada72004-05-11 09:57:35 +00004472 }
drh9c1905f2008-12-10 22:32:56 +00004473 pC->nullRow = (u8)res;
drha05a7222008-01-19 03:35:58 +00004474 assert( pOp->p2>0 && pOp->p2<p->nOp );
4475 if( res ){
drhf4dada72004-05-11 09:57:35 +00004476 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00004477 }
4478 break;
4479}
4480
dana205a482011-08-27 18:48:57 +00004481/* Opcode: Next P1 P2 * P4 P5
drh5e00f6c2001-09-13 13:46:56 +00004482**
4483** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00004484** table or index. If there are no more key/value pairs then fall through
4485** to the following instruction. But if the cursor advance was successful,
4486** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00004487**
drh60a713c2008-01-21 16:22:45 +00004488** The P1 cursor must be for a real table, not a pseudo-table.
4489**
dana205a482011-08-27 18:48:57 +00004490** P4 is always of type P4_ADVANCE. The function pointer points to
4491** sqlite3BtreeNext().
4492**
drhafc266a2010-03-31 17:47:44 +00004493** If P5 is positive and the jump is taken, then event counter
4494** number P5-1 in the prepared statement is incremented.
4495**
drhc045ec52002-12-04 20:01:06 +00004496** See also: Prev
drh8721ce42001-11-07 14:22:00 +00004497*/
drhafc266a2010-03-31 17:47:44 +00004498/* Opcode: Prev P1 P2 * * P5
drhc045ec52002-12-04 20:01:06 +00004499**
4500** Back up cursor P1 so that it points to the previous key/data pair in its
4501** table or index. If there is no previous key/value pairs then fall through
4502** to the following instruction. But if the cursor backup was successful,
4503** jump immediately to P2.
drh60a713c2008-01-21 16:22:45 +00004504**
4505** The P1 cursor must be for a real table, not a pseudo-table.
drhafc266a2010-03-31 17:47:44 +00004506**
dana205a482011-08-27 18:48:57 +00004507** P4 is always of type P4_ADVANCE. The function pointer points to
4508** sqlite3BtreePrevious().
4509**
drhafc266a2010-03-31 17:47:44 +00004510** If P5 is positive and the jump is taken, then event counter
4511** number P5-1 in the prepared statement is incremented.
drhc045ec52002-12-04 20:01:06 +00004512*/
drhc6aff302011-09-01 15:32:47 +00004513case OP_SorterNext: /* jump */
drh9cbf3422008-01-17 16:22:13 +00004514case OP_Prev: /* jump */
4515case OP_Next: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004516 VdbeCursor *pC;
drha3460582008-07-11 21:02:53 +00004517 int res;
drh8721ce42001-11-07 14:22:00 +00004518
drh70ce3f02003-04-15 19:22:22 +00004519 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drh9b47ee32013-08-20 03:13:51 +00004520 assert( pOp->p5<ArraySize(p->aCounter) );
drhd7556d22004-05-14 21:59:40 +00004521 pC = p->apCsr[pOp->p1];
drh72e8fa42007-03-28 14:30:06 +00004522 if( pC==0 ){
4523 break; /* See ticket #2273 */
4524 }
drhc6aff302011-09-01 15:32:47 +00004525 assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
dan689ab892011-08-12 15:02:00 +00004526 if( isSorter(pC) ){
dan5134d132011-09-02 10:31:11 +00004527 assert( pOp->opcode==OP_SorterNext );
dana20fde62011-07-12 14:28:05 +00004528 rc = sqlite3VdbeSorterNext(db, pC, &res);
4529 }else{
drh9b47ee32013-08-20 03:13:51 +00004530 /* res = 1; // Always initialized by the xAdvance() call */
dana20fde62011-07-12 14:28:05 +00004531 assert( pC->deferredMoveto==0 );
dana205a482011-08-27 18:48:57 +00004532 assert( pC->pCursor );
4533 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
4534 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
4535 rc = pOp->p4.xAdvance(pC->pCursor, &res);
drh9a65f2c2009-06-22 19:05:40 +00004536 }
drh9c1905f2008-12-10 22:32:56 +00004537 pC->nullRow = (u8)res;
drha3460582008-07-11 21:02:53 +00004538 pC->cacheStatus = CACHE_STALE;
4539 if( res==0 ){
4540 pc = pOp->p2 - 1;
drh9b47ee32013-08-20 03:13:51 +00004541 p->aCounter[pOp->p5]++;
drh0f7eb612006-08-08 13:51:43 +00004542#ifdef SQLITE_TEST
drha3460582008-07-11 21:02:53 +00004543 sqlite3_search_count++;
drh0f7eb612006-08-08 13:51:43 +00004544#endif
drh8721ce42001-11-07 14:22:00 +00004545 }
drhf0863fe2005-06-12 21:35:51 +00004546 pC->rowidIsValid = 0;
drh49afe3a2013-07-10 03:05:14 +00004547 goto check_for_interrupt;
drh8721ce42001-11-07 14:22:00 +00004548}
4549
danielk1977de630352009-05-04 11:42:29 +00004550/* Opcode: IdxInsert P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004551**
drhef8662b2011-06-20 21:47:58 +00004552** Register P2 holds an SQL index key made using the
drh9437bd22009-02-01 00:29:56 +00004553** MakeRecord instructions. This opcode writes that key
drhee32e0a2006-01-10 19:45:49 +00004554** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00004555**
drhaa9b8962008-01-08 02:57:55 +00004556** P3 is a flag that provides a hint to the b-tree layer that this
drhe4d90812007-03-29 05:51:49 +00004557** insert is likely to be an append.
4558**
drhf0863fe2005-06-12 21:35:51 +00004559** This instruction only works for indices. The equivalent instruction
4560** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00004561*/
drhca892a72011-09-03 00:17:51 +00004562case OP_SorterInsert: /* in2 */
drh9cbf3422008-01-17 16:22:13 +00004563case OP_IdxInsert: { /* in2 */
drhdfe88ec2008-11-03 20:55:06 +00004564 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004565 BtCursor *pCrsr;
drh856c1032009-06-02 15:21:42 +00004566 int nKey;
4567 const char *zKey;
4568
drh653b82a2009-06-22 11:10:47 +00004569 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4570 pC = p->apCsr[pOp->p1];
4571 assert( pC!=0 );
drhc6aff302011-09-01 15:32:47 +00004572 assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
drh3c657212009-11-17 23:59:58 +00004573 pIn2 = &aMem[pOp->p2];
drhaa9b8962008-01-08 02:57:55 +00004574 assert( pIn2->flags & MEM_Blob );
drh653b82a2009-06-22 11:10:47 +00004575 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004576 if( ALWAYS(pCrsr!=0) ){
drhf0863fe2005-06-12 21:35:51 +00004577 assert( pC->isTable==0 );
drhaa9b8962008-01-08 02:57:55 +00004578 rc = ExpandBlob(pIn2);
danielk1977d908f5a2007-05-11 07:08:28 +00004579 if( rc==SQLITE_OK ){
dan5134d132011-09-02 10:31:11 +00004580 if( isSorter(pC) ){
4581 rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
4582 }else{
4583 nKey = pIn2->n;
4584 zKey = pIn2->z;
dan1e74e602011-08-06 12:01:58 +00004585 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
4586 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
dan5134d132011-09-02 10:31:11 +00004587 );
dan1e74e602011-08-06 12:01:58 +00004588 assert( pC->deferredMoveto==0 );
dan5134d132011-09-02 10:31:11 +00004589 pC->cacheStatus = CACHE_STALE;
dan1e74e602011-08-06 12:01:58 +00004590 }
danielk1977d908f5a2007-05-11 07:08:28 +00004591 }
drh5e00f6c2001-09-13 13:46:56 +00004592 }
drh5e00f6c2001-09-13 13:46:56 +00004593 break;
4594}
4595
drhd1d38482008-10-07 23:46:38 +00004596/* Opcode: IdxDelete P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004597**
drhe14006d2008-03-25 17:23:32 +00004598** The content of P3 registers starting at register P2 form
4599** an unpacked index key. This opcode removes that entry from the
danielk1977a7a8e142008-02-13 18:25:27 +00004600** index opened by cursor P1.
drh5e00f6c2001-09-13 13:46:56 +00004601*/
drhe14006d2008-03-25 17:23:32 +00004602case OP_IdxDelete: {
drhdfe88ec2008-11-03 20:55:06 +00004603 VdbeCursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00004604 BtCursor *pCrsr;
drh9a65f2c2009-06-22 19:05:40 +00004605 int res;
4606 UnpackedRecord r;
drh856c1032009-06-02 15:21:42 +00004607
drhe14006d2008-03-25 17:23:32 +00004608 assert( pOp->p3>0 );
danielk19776ab3a2e2009-02-19 14:39:25 +00004609 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
drh653b82a2009-06-22 11:10:47 +00004610 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4611 pC = p->apCsr[pOp->p1];
4612 assert( pC!=0 );
4613 pCrsr = pC->pCursor;
drh9a65f2c2009-06-22 19:05:40 +00004614 if( ALWAYS(pCrsr!=0) ){
drhe14006d2008-03-25 17:23:32 +00004615 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004616 r.nField = (u16)pOp->p3;
drhe63d9992008-08-13 19:11:48 +00004617 r.flags = 0;
drha6c2ed92009-11-14 23:22:23 +00004618 r.aMem = &aMem[pOp->p2];
drh2b4ded92010-09-27 21:09:31 +00004619#ifdef SQLITE_DEBUG
4620 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4621#endif
drhe63d9992008-08-13 19:11:48 +00004622 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
danielk197775bab7d2006-01-23 13:09:45 +00004623 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00004624 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00004625 }
drh9188b382004-05-14 21:12:22 +00004626 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00004627 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00004628 }
drh5e00f6c2001-09-13 13:46:56 +00004629 break;
4630}
4631
drh2133d822008-01-03 18:44:59 +00004632/* Opcode: IdxRowid P1 P2 * * *
drh8721ce42001-11-07 14:22:00 +00004633**
drh2133d822008-01-03 18:44:59 +00004634** Write into register P2 an integer which is the last entry in the record at
drhf0863fe2005-06-12 21:35:51 +00004635** the end of the index key pointed to by cursor P1. This integer should be
4636** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00004637**
drh9437bd22009-02-01 00:29:56 +00004638** See also: Rowid, MakeRecord.
drh8721ce42001-11-07 14:22:00 +00004639*/
drh4c583122008-01-04 22:01:03 +00004640case OP_IdxRowid: { /* out2-prerelease */
drh8721ce42001-11-07 14:22:00 +00004641 BtCursor *pCrsr;
drhdfe88ec2008-11-03 20:55:06 +00004642 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004643 i64 rowid;
drh8721ce42001-11-07 14:22:00 +00004644
drh653b82a2009-06-22 11:10:47 +00004645 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4646 pC = p->apCsr[pOp->p1];
4647 assert( pC!=0 );
4648 pCrsr = pC->pCursor;
drh3c657212009-11-17 23:59:58 +00004649 pOut->flags = MEM_Null;
drh9a65f2c2009-06-22 19:05:40 +00004650 if( ALWAYS(pCrsr!=0) ){
danielk1977c4d201c2009-04-07 09:16:56 +00004651 rc = sqlite3VdbeCursorMoveto(pC);
drh9a65f2c2009-06-22 19:05:40 +00004652 if( NEVER(rc) ) goto abort_due_to_error;
drhd7556d22004-05-14 21:59:40 +00004653 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00004654 assert( pC->isTable==0 );
drh4c583122008-01-04 22:01:03 +00004655 if( !pC->nullRow ){
drh35f6b932009-06-23 14:15:04 +00004656 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
danielk19771d850a72004-05-31 08:26:49 +00004657 if( rc!=SQLITE_OK ){
4658 goto abort_due_to_error;
4659 }
drh4c583122008-01-04 22:01:03 +00004660 pOut->u.i = rowid;
drh3c657212009-11-17 23:59:58 +00004661 pOut->flags = MEM_Int;
danielk19773d1bfea2004-05-14 11:00:53 +00004662 }
drh8721ce42001-11-07 14:22:00 +00004663 }
4664 break;
4665}
4666
danielk197761dd5832008-04-18 11:31:12 +00004667/* Opcode: IdxGE P1 P2 P3 P4 P5
drh8721ce42001-11-07 14:22:00 +00004668**
danielk197761dd5832008-04-18 11:31:12 +00004669** The P4 register values beginning with P3 form an unpacked index
4670** key that omits the ROWID. Compare this key value against the index
4671** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004672**
danielk197761dd5832008-04-18 11:31:12 +00004673** If the P1 index entry is greater than or equal to the key value
4674** then jump to P2. Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004675**
danielk197761dd5832008-04-18 11:31:12 +00004676** If P5 is non-zero then the key value is increased by an epsilon
4677** prior to the comparison. This make the opcode work like IdxGT except
4678** that if the key from register P3 is a prefix of the key in the cursor,
4679** the result is false whereas it would be true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00004680*/
drh3bb9b932010-08-06 02:10:00 +00004681/* Opcode: IdxLT P1 P2 P3 P4 P5
drhc045ec52002-12-04 20:01:06 +00004682**
danielk197761dd5832008-04-18 11:31:12 +00004683** The P4 register values beginning with P3 form an unpacked index
4684** key that omits the ROWID. Compare this key value against the index
4685** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
drhf3218fe2004-05-28 08:21:02 +00004686**
danielk197761dd5832008-04-18 11:31:12 +00004687** If the P1 index entry is less than the key value then jump to P2.
4688** Otherwise fall through to the next instruction.
drh772ae622004-05-19 13:13:08 +00004689**
danielk197761dd5832008-04-18 11:31:12 +00004690** If P5 is non-zero then the key value is increased by an epsilon prior
4691** to the comparison. This makes the opcode work like IdxLE.
drhc045ec52002-12-04 20:01:06 +00004692*/
drh93952eb2009-11-13 19:43:43 +00004693case OP_IdxLT: /* jump */
4694case OP_IdxGE: { /* jump */
drhdfe88ec2008-11-03 20:55:06 +00004695 VdbeCursor *pC;
drh856c1032009-06-02 15:21:42 +00004696 int res;
4697 UnpackedRecord r;
drh8721ce42001-11-07 14:22:00 +00004698
drh653b82a2009-06-22 11:10:47 +00004699 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
4700 pC = p->apCsr[pOp->p1];
4701 assert( pC!=0 );
drhd4187c72010-08-30 22:15:45 +00004702 assert( pC->isOrdered );
drh9a65f2c2009-06-22 19:05:40 +00004703 if( ALWAYS(pC->pCursor!=0) ){
drhd7556d22004-05-14 21:59:40 +00004704 assert( pC->deferredMoveto==0 );
drha05a7222008-01-19 03:35:58 +00004705 assert( pOp->p5==0 || pOp->p5==1 );
danielk197761dd5832008-04-18 11:31:12 +00004706 assert( pOp->p4type==P4_INT32 );
4707 r.pKeyInfo = pC->pKeyInfo;
drh9c1905f2008-12-10 22:32:56 +00004708 r.nField = (u16)pOp->p4.i;
drhe63d9992008-08-13 19:11:48 +00004709 if( pOp->p5 ){
dan0c733f62011-11-16 15:27:09 +00004710 r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004711 }else{
dan0c733f62011-11-16 15:27:09 +00004712 r.flags = UNPACKED_PREFIX_MATCH;
drhe63d9992008-08-13 19:11:48 +00004713 }
drha6c2ed92009-11-14 23:22:23 +00004714 r.aMem = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00004715#ifdef SQLITE_DEBUG
4716 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
4717#endif
drhe63d9992008-08-13 19:11:48 +00004718 rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
drhc045ec52002-12-04 20:01:06 +00004719 if( pOp->opcode==OP_IdxLT ){
4720 res = -res;
drha05a7222008-01-19 03:35:58 +00004721 }else{
4722 assert( pOp->opcode==OP_IdxGE );
drh8721ce42001-11-07 14:22:00 +00004723 res++;
4724 }
4725 if( res>0 ){
4726 pc = pOp->p2 - 1 ;
4727 }
4728 }
4729 break;
4730}
4731
drh98757152008-01-09 23:04:12 +00004732/* Opcode: Destroy P1 P2 P3 * *
drh5e00f6c2001-09-13 13:46:56 +00004733**
4734** Delete an entire database table or index whose root page in the database
4735** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00004736**
drh98757152008-01-09 23:04:12 +00004737** The table being destroyed is in the main database file if P3==0. If
4738** P3==1 then the table to be clear is in the auxiliary database file
drhf57b3392001-10-08 13:22:32 +00004739** that is used to store tables create using CREATE TEMPORARY TABLE.
4740**
drh205f48e2004-11-05 00:43:11 +00004741** If AUTOVACUUM is enabled then it is possible that another root page
4742** might be moved into the newly deleted root page in order to keep all
4743** root pages contiguous at the beginning of the database. The former
4744** value of the root page that moved - its value before the move occurred -
drh9cbf3422008-01-17 16:22:13 +00004745** is stored in register P2. If no page
drh98757152008-01-09 23:04:12 +00004746** movement was required (because the table being dropped was already
4747** the last one in the database) then a zero is stored in register P2.
4748** If AUTOVACUUM is disabled then a zero is stored in register P2.
drh205f48e2004-11-05 00:43:11 +00004749**
drhb19a2bc2001-09-16 00:13:26 +00004750** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00004751*/
drh98757152008-01-09 23:04:12 +00004752case OP_Destroy: { /* out2-prerelease */
danielk1977a0bf2652004-11-04 14:30:04 +00004753 int iMoved;
drh3765df42006-06-28 18:18:09 +00004754 int iCnt;
drh5a91a532007-01-05 16:39:43 +00004755 Vdbe *pVdbe;
drh856c1032009-06-02 15:21:42 +00004756 int iDb;
drh3a949872012-09-18 13:20:13 +00004757
drh9e92a472013-06-27 17:40:30 +00004758 assert( p->readOnly==0 );
drh856c1032009-06-02 15:21:42 +00004759#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk1977212b2182006-06-23 14:32:08 +00004760 iCnt = 0;
drh856c1032009-06-02 15:21:42 +00004761 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
danc0537fe2013-06-28 19:41:43 +00004762 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
4763 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
4764 ){
danielk1977212b2182006-06-23 14:32:08 +00004765 iCnt++;
4766 }
4767 }
drh3765df42006-06-28 18:18:09 +00004768#else
danc0537fe2013-06-28 19:41:43 +00004769 iCnt = db->nVdbeRead;
danielk1977212b2182006-06-23 14:32:08 +00004770#endif
drh3c657212009-11-17 23:59:58 +00004771 pOut->flags = MEM_Null;
danielk1977212b2182006-06-23 14:32:08 +00004772 if( iCnt>1 ){
danielk1977e6efa742004-11-10 11:55:10 +00004773 rc = SQLITE_LOCKED;
drh77658e22007-12-04 16:54:52 +00004774 p->errorAction = OE_Abort;
danielk1977e6efa742004-11-10 11:55:10 +00004775 }else{
drh856c1032009-06-02 15:21:42 +00004776 iDb = pOp->p3;
danielk1977212b2182006-06-23 14:32:08 +00004777 assert( iCnt==1 );
drhdddd7792011-04-03 18:19:25 +00004778 assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
drh98757152008-01-09 23:04:12 +00004779 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
drh3c657212009-11-17 23:59:58 +00004780 pOut->flags = MEM_Int;
drh98757152008-01-09 23:04:12 +00004781 pOut->u.i = iMoved;
drh3765df42006-06-28 18:18:09 +00004782#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977e6efa742004-11-10 11:55:10 +00004783 if( rc==SQLITE_OK && iMoved!=0 ){
drhcdf011d2011-04-04 21:25:28 +00004784 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
4785 /* All OP_Destroy operations occur on the same btree */
4786 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
4787 resetSchemaOnFault = iDb+1;
danielk1977e6efa742004-11-10 11:55:10 +00004788 }
drh3765df42006-06-28 18:18:09 +00004789#endif
danielk1977a0bf2652004-11-04 14:30:04 +00004790 }
drh5e00f6c2001-09-13 13:46:56 +00004791 break;
4792}
4793
danielk1977c7af4842008-10-27 13:59:33 +00004794/* Opcode: Clear P1 P2 P3
drh5edc3122001-09-13 21:53:09 +00004795**
4796** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00004797** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00004798** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00004799**
drhf57b3392001-10-08 13:22:32 +00004800** The table being clear is in the main database file if P2==0. If
4801** P2==1 then the table to be clear is in the auxiliary database file
4802** that is used to store tables create using CREATE TEMPORARY TABLE.
4803**
shanebe217792009-03-05 04:20:31 +00004804** If the P3 value is non-zero, then the table referred to must be an
danielk1977c7af4842008-10-27 13:59:33 +00004805** intkey table (an SQL table, not an index). In this case the row change
4806** count is incremented by the number of rows in the table being cleared.
4807** If P3 is greater than zero, then the value stored in register P3 is
4808** also incremented by the number of rows in the table being cleared.
4809**
drhb19a2bc2001-09-16 00:13:26 +00004810** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00004811*/
drh9cbf3422008-01-17 16:22:13 +00004812case OP_Clear: {
drh856c1032009-06-02 15:21:42 +00004813 int nChange;
4814
4815 nChange = 0;
drh9e92a472013-06-27 17:40:30 +00004816 assert( p->readOnly==0 );
drhdddd7792011-04-03 18:19:25 +00004817 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
danielk1977c7af4842008-10-27 13:59:33 +00004818 rc = sqlite3BtreeClearTable(
4819 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
4820 );
4821 if( pOp->p3 ){
4822 p->nChange += nChange;
4823 if( pOp->p3>0 ){
drh2b4ded92010-09-27 21:09:31 +00004824 assert( memIsValid(&aMem[pOp->p3]) );
4825 memAboutToChange(p, &aMem[pOp->p3]);
drha6c2ed92009-11-14 23:22:23 +00004826 aMem[pOp->p3].u.i += nChange;
danielk1977c7af4842008-10-27 13:59:33 +00004827 }
4828 }
drh5edc3122001-09-13 21:53:09 +00004829 break;
4830}
4831
drh4c583122008-01-04 22:01:03 +00004832/* Opcode: CreateTable P1 P2 * * *
drh5b2fd562001-09-13 15:21:31 +00004833**
drh4c583122008-01-04 22:01:03 +00004834** Allocate a new table in the main database file if P1==0 or in the
4835** auxiliary database file if P1==1 or in an attached database if
4836** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004837** register P2
drh5b2fd562001-09-13 15:21:31 +00004838**
drhc6b52df2002-01-04 03:09:29 +00004839** The difference between a table and an index is this: A table must
4840** have a 4-byte integer key and can have arbitrary data. An index
4841** has an arbitrary key but no data.
4842**
drhb19a2bc2001-09-16 00:13:26 +00004843** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00004844*/
drh4c583122008-01-04 22:01:03 +00004845/* Opcode: CreateIndex P1 P2 * * *
drhf57b3392001-10-08 13:22:32 +00004846**
drh4c583122008-01-04 22:01:03 +00004847** Allocate a new index in the main database file if P1==0 or in the
4848** auxiliary database file if P1==1 or in an attached database if
4849** P1>1. Write the root page number of the new table into
drh9cbf3422008-01-17 16:22:13 +00004850** register P2.
drhf57b3392001-10-08 13:22:32 +00004851**
drhc6b52df2002-01-04 03:09:29 +00004852** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00004853*/
drh4c583122008-01-04 22:01:03 +00004854case OP_CreateIndex: /* out2-prerelease */
4855case OP_CreateTable: { /* out2-prerelease */
drh856c1032009-06-02 15:21:42 +00004856 int pgno;
drhf328bc82004-05-10 23:29:49 +00004857 int flags;
drh234c39d2004-07-24 03:30:47 +00004858 Db *pDb;
drh856c1032009-06-02 15:21:42 +00004859
4860 pgno = 0;
drh234c39d2004-07-24 03:30:47 +00004861 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00004862 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00004863 assert( p->readOnly==0 );
drh234c39d2004-07-24 03:30:47 +00004864 pDb = &db->aDb[pOp->p1];
4865 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00004866 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00004867 /* flags = BTREE_INTKEY; */
drhd4187c72010-08-30 22:15:45 +00004868 flags = BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00004869 }else{
drhd4187c72010-08-30 22:15:45 +00004870 flags = BTREE_BLOBKEY;
drhc6b52df2002-01-04 03:09:29 +00004871 }
drh234c39d2004-07-24 03:30:47 +00004872 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh88a003e2008-12-11 16:17:03 +00004873 pOut->u.i = pgno;
drh5b2fd562001-09-13 15:21:31 +00004874 break;
4875}
4876
drh22645842011-03-24 01:34:03 +00004877/* Opcode: ParseSchema P1 * * P4 *
drh234c39d2004-07-24 03:30:47 +00004878**
4879** Read and parse all entries from the SQLITE_MASTER table of database P1
drh22645842011-03-24 01:34:03 +00004880** that match the WHERE clause P4.
drh234c39d2004-07-24 03:30:47 +00004881**
4882** This opcode invokes the parser to create a new virtual machine,
shane21e7feb2008-05-30 15:59:49 +00004883** then runs the new virtual machine. It is thus a re-entrant opcode.
drh234c39d2004-07-24 03:30:47 +00004884*/
drh9cbf3422008-01-17 16:22:13 +00004885case OP_ParseSchema: {
drh856c1032009-06-02 15:21:42 +00004886 int iDb;
4887 const char *zMaster;
4888 char *zSql;
4889 InitData initData;
4890
drhbdaec522011-04-04 00:14:43 +00004891 /* Any prepared statement that invokes this opcode will hold mutexes
4892 ** on every btree. This is a prerequisite for invoking
4893 ** sqlite3InitCallback().
4894 */
4895#ifdef SQLITE_DEBUG
4896 for(iDb=0; iDb<db->nDb; iDb++){
4897 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
4898 }
4899#endif
drhbdaec522011-04-04 00:14:43 +00004900
drh856c1032009-06-02 15:21:42 +00004901 iDb = pOp->p1;
drh234c39d2004-07-24 03:30:47 +00004902 assert( iDb>=0 && iDb<db->nDb );
dan6c154872011-04-02 09:44:43 +00004903 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
drhbdaec522011-04-04 00:14:43 +00004904 /* Used to be a conditional */ {
drh856c1032009-06-02 15:21:42 +00004905 zMaster = SCHEMA_TABLE(iDb);
danielk1977a8bbef82009-03-23 17:11:26 +00004906 initData.db = db;
4907 initData.iDb = pOp->p1;
4908 initData.pzErrMsg = &p->zErrMsg;
4909 zSql = sqlite3MPrintf(db,
drh6a9c64b2010-01-12 23:54:14 +00004910 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
danielk1977a8bbef82009-03-23 17:11:26 +00004911 db->aDb[iDb].zName, zMaster, pOp->p4.z);
4912 if( zSql==0 ){
4913 rc = SQLITE_NOMEM;
4914 }else{
danielk1977a8bbef82009-03-23 17:11:26 +00004915 assert( db->init.busy==0 );
4916 db->init.busy = 1;
4917 initData.rc = SQLITE_OK;
4918 assert( !db->mallocFailed );
4919 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4920 if( rc==SQLITE_OK ) rc = initData.rc;
4921 sqlite3DbFree(db, zSql);
4922 db->init.busy = 0;
danielk1977a8bbef82009-03-23 17:11:26 +00004923 }
drh3c23a882007-01-09 14:01:13 +00004924 }
drh81028a42012-05-15 18:28:27 +00004925 if( rc ) sqlite3ResetAllSchemasOfConnection(db);
danielk1977261919c2005-12-06 12:52:59 +00004926 if( rc==SQLITE_NOMEM ){
danielk1977261919c2005-12-06 12:52:59 +00004927 goto no_mem;
4928 }
drh234c39d2004-07-24 03:30:47 +00004929 break;
4930}
4931
drh8bfdf722009-06-19 14:06:03 +00004932#if !defined(SQLITE_OMIT_ANALYZE)
drh98757152008-01-09 23:04:12 +00004933/* Opcode: LoadAnalysis P1 * * * *
drh497e4462005-07-23 03:18:40 +00004934**
4935** Read the sqlite_stat1 table for database P1 and load the content
4936** of that table into the internal index hash table. This will cause
4937** the analysis to be used when preparing all subsequent queries.
4938*/
drh9cbf3422008-01-17 16:22:13 +00004939case OP_LoadAnalysis: {
drh856c1032009-06-02 15:21:42 +00004940 assert( pOp->p1>=0 && pOp->p1<db->nDb );
4941 rc = sqlite3AnalysisLoad(db, pOp->p1);
drh497e4462005-07-23 03:18:40 +00004942 break;
4943}
drh8bfdf722009-06-19 14:06:03 +00004944#endif /* !defined(SQLITE_OMIT_ANALYZE) */
drh497e4462005-07-23 03:18:40 +00004945
drh98757152008-01-09 23:04:12 +00004946/* Opcode: DropTable P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004947**
4948** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004949** the table named P4 in database P1. This is called after a table
drh956bc922004-07-24 17:38:29 +00004950** is dropped in order to keep the internal representation of the
4951** schema consistent with what is on disk.
4952*/
drh9cbf3422008-01-17 16:22:13 +00004953case OP_DropTable: {
danielk19772dca4ac2008-01-03 11:50:29 +00004954 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004955 break;
4956}
4957
drh98757152008-01-09 23:04:12 +00004958/* Opcode: DropIndex P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004959**
4960** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004961** the index named P4 in database P1. This is called after an index
drh956bc922004-07-24 17:38:29 +00004962** is dropped in order to keep the internal representation of the
4963** schema consistent with what is on disk.
4964*/
drh9cbf3422008-01-17 16:22:13 +00004965case OP_DropIndex: {
danielk19772dca4ac2008-01-03 11:50:29 +00004966 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004967 break;
4968}
4969
drh98757152008-01-09 23:04:12 +00004970/* Opcode: DropTrigger P1 * * P4 *
drh956bc922004-07-24 17:38:29 +00004971**
4972** Remove the internal (in-memory) data structures that describe
drh66a51672008-01-03 00:01:23 +00004973** the trigger named P4 in database P1. This is called after a trigger
drh956bc922004-07-24 17:38:29 +00004974** is dropped in order to keep the internal representation of the
4975** schema consistent with what is on disk.
4976*/
drh9cbf3422008-01-17 16:22:13 +00004977case OP_DropTrigger: {
danielk19772dca4ac2008-01-03 11:50:29 +00004978 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
drh956bc922004-07-24 17:38:29 +00004979 break;
4980}
4981
drh234c39d2004-07-24 03:30:47 +00004982
drhb7f91642004-10-31 02:22:47 +00004983#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh98757152008-01-09 23:04:12 +00004984/* Opcode: IntegrityCk P1 P2 P3 * P5
drh5e00f6c2001-09-13 13:46:56 +00004985**
drh98757152008-01-09 23:04:12 +00004986** Do an analysis of the currently open database. Store in
4987** register P1 the text of an error message describing any problems.
4988** If no problems are found, store a NULL in register P1.
drh1dcdbc02007-01-27 02:24:54 +00004989**
drh98757152008-01-09 23:04:12 +00004990** The register P3 contains the maximum number of allowed errors.
drh60a713c2008-01-21 16:22:45 +00004991** At most reg(P3) errors will be reported.
4992** In other words, the analysis stops as soon as reg(P1) errors are
4993** seen. Reg(P1) is updated with the number of errors remaining.
drhb19a2bc2001-09-16 00:13:26 +00004994**
drh79069752004-05-22 21:30:40 +00004995** The root page numbers of all tables in the database are integer
drh60a713c2008-01-21 16:22:45 +00004996** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables
drh98757152008-01-09 23:04:12 +00004997** total.
drh21504322002-06-25 13:16:02 +00004998**
drh98757152008-01-09 23:04:12 +00004999** If P5 is not zero, the check is done on the auxiliary database
drh21504322002-06-25 13:16:02 +00005000** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00005001**
drh1dcdbc02007-01-27 02:24:54 +00005002** This opcode is used to implement the integrity_check pragma.
drh5e00f6c2001-09-13 13:46:56 +00005003*/
drhaaab5722002-02-19 13:39:21 +00005004case OP_IntegrityCk: {
drh98757152008-01-09 23:04:12 +00005005 int nRoot; /* Number of tables to check. (Number of root pages.) */
5006 int *aRoot; /* Array of rootpage numbers for tables to be checked */
5007 int j; /* Loop counter */
5008 int nErr; /* Number of errors reported */
5009 char *z; /* Text of the error report */
5010 Mem *pnErr; /* Register keeping track of errors remaining */
drh9e92a472013-06-27 17:40:30 +00005011
drh1713afb2013-06-28 01:24:57 +00005012 assert( p->bIsReader );
drh98757152008-01-09 23:04:12 +00005013 nRoot = pOp->p2;
drh79069752004-05-22 21:30:40 +00005014 assert( nRoot>0 );
drh633e6d52008-07-28 19:34:53 +00005015 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00005016 if( aRoot==0 ) goto no_mem;
drh98757152008-01-09 23:04:12 +00005017 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005018 pnErr = &aMem[pOp->p3];
drh1dcdbc02007-01-27 02:24:54 +00005019 assert( (pnErr->flags & MEM_Int)!=0 );
drh98757152008-01-09 23:04:12 +00005020 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
drha6c2ed92009-11-14 23:22:23 +00005021 pIn1 = &aMem[pOp->p1];
drh79069752004-05-22 21:30:40 +00005022 for(j=0; j<nRoot; j++){
drh9c1905f2008-12-10 22:32:56 +00005023 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
drh1dd397f2002-02-03 03:34:07 +00005024 }
5025 aRoot[j] = 0;
drh98757152008-01-09 23:04:12 +00005026 assert( pOp->p5<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005027 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
drh98757152008-01-09 23:04:12 +00005028 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
drh9c1905f2008-12-10 22:32:56 +00005029 (int)pnErr->u.i, &nErr);
drhc890fec2008-08-01 20:10:08 +00005030 sqlite3DbFree(db, aRoot);
drh3c024d62007-03-30 11:23:45 +00005031 pnErr->u.i -= nErr;
drha05a7222008-01-19 03:35:58 +00005032 sqlite3VdbeMemSetNull(pIn1);
drh1dcdbc02007-01-27 02:24:54 +00005033 if( nErr==0 ){
5034 assert( z==0 );
drhc890fec2008-08-01 20:10:08 +00005035 }else if( z==0 ){
5036 goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00005037 }else{
danielk1977a7a8e142008-02-13 18:25:27 +00005038 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
danielk19778a6b5412004-05-24 07:04:25 +00005039 }
drhb7654112008-01-12 12:48:07 +00005040 UPDATE_MAX_BLOBSIZE(pIn1);
drh98757152008-01-09 23:04:12 +00005041 sqlite3VdbeChangeEncoding(pIn1, encoding);
drh5e00f6c2001-09-13 13:46:56 +00005042 break;
5043}
drhb7f91642004-10-31 02:22:47 +00005044#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00005045
drh3d4501e2008-12-04 20:40:10 +00005046/* Opcode: RowSetAdd P1 P2 * * *
drh5e00f6c2001-09-13 13:46:56 +00005047**
drh3d4501e2008-12-04 20:40:10 +00005048** Insert the integer value held by register P2 into a boolean index
5049** held in register P1.
5050**
5051** An assertion fails if P2 is not an integer.
drh5e00f6c2001-09-13 13:46:56 +00005052*/
drh93952eb2009-11-13 19:43:43 +00005053case OP_RowSetAdd: { /* in1, in2 */
drh3c657212009-11-17 23:59:58 +00005054 pIn1 = &aMem[pOp->p1];
5055 pIn2 = &aMem[pOp->p2];
drh93952eb2009-11-13 19:43:43 +00005056 assert( (pIn2->flags & MEM_Int)!=0 );
5057 if( (pIn1->flags & MEM_RowSet)==0 ){
5058 sqlite3VdbeMemSetRowSet(pIn1);
5059 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
drh3d4501e2008-12-04 20:40:10 +00005060 }
drh93952eb2009-11-13 19:43:43 +00005061 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
drh3d4501e2008-12-04 20:40:10 +00005062 break;
5063}
5064
5065/* Opcode: RowSetRead P1 P2 P3 * *
5066**
5067** Extract the smallest value from boolean index P1 and put that value into
5068** register P3. Or, if boolean index P1 is initially empty, leave P3
5069** unchanged and jump to instruction P2.
5070*/
drh93952eb2009-11-13 19:43:43 +00005071case OP_RowSetRead: { /* jump, in1, out3 */
drh3d4501e2008-12-04 20:40:10 +00005072 i64 val;
drh49afe3a2013-07-10 03:05:14 +00005073
drh3c657212009-11-17 23:59:58 +00005074 pIn1 = &aMem[pOp->p1];
drh93952eb2009-11-13 19:43:43 +00005075 if( (pIn1->flags & MEM_RowSet)==0
5076 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
drh3d4501e2008-12-04 20:40:10 +00005077 ){
5078 /* The boolean index is empty */
drh93952eb2009-11-13 19:43:43 +00005079 sqlite3VdbeMemSetNull(pIn1);
drh3d4501e2008-12-04 20:40:10 +00005080 pc = pOp->p2 - 1;
5081 }else{
5082 /* A value was pulled from the index */
drh3c657212009-11-17 23:59:58 +00005083 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
drh17435752007-08-16 04:30:38 +00005084 }
drh49afe3a2013-07-10 03:05:14 +00005085 goto check_for_interrupt;
drh5e00f6c2001-09-13 13:46:56 +00005086}
5087
drh1b26c7c2009-04-22 02:15:47 +00005088/* Opcode: RowSetTest P1 P2 P3 P4
danielk19771d461462009-04-21 09:02:45 +00005089**
drhade97602009-04-21 15:05:18 +00005090** Register P3 is assumed to hold a 64-bit integer value. If register P1
drh1b26c7c2009-04-22 02:15:47 +00005091** contains a RowSet object and that RowSet object contains
danielk19771d461462009-04-21 09:02:45 +00005092** the value held in P3, jump to register P2. Otherwise, insert the
drh1b26c7c2009-04-22 02:15:47 +00005093** integer in P3 into the RowSet and continue on to the
drhade97602009-04-21 15:05:18 +00005094** next opcode.
danielk19771d461462009-04-21 09:02:45 +00005095**
drh1b26c7c2009-04-22 02:15:47 +00005096** The RowSet object is optimized for the case where successive sets
danielk19771d461462009-04-21 09:02:45 +00005097** of integers, where each set contains no duplicates. Each set
5098** of values is identified by a unique P4 value. The first set
drh1b26c7c2009-04-22 02:15:47 +00005099** must have P4==0, the final set P4=-1. P4 must be either -1 or
5100** non-negative. For non-negative values of P4 only the lower 4
5101** bits are significant.
danielk19771d461462009-04-21 09:02:45 +00005102**
5103** This allows optimizations: (a) when P4==0 there is no need to test
drh1b26c7c2009-04-22 02:15:47 +00005104** the rowset object for P3, as it is guaranteed not to contain it,
danielk19771d461462009-04-21 09:02:45 +00005105** (b) when P4==-1 there is no need to insert the value, as it will
5106** never be tested for, and (c) when a value that is part of set X is
5107** inserted, there is no need to search to see if the same value was
5108** previously inserted as part of set X (only if it was previously
5109** inserted as part of some other set).
5110*/
drh1b26c7c2009-04-22 02:15:47 +00005111case OP_RowSetTest: { /* jump, in1, in3 */
drh856c1032009-06-02 15:21:42 +00005112 int iSet;
5113 int exists;
5114
drh3c657212009-11-17 23:59:58 +00005115 pIn1 = &aMem[pOp->p1];
5116 pIn3 = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005117 iSet = pOp->p4.i;
danielk19771d461462009-04-21 09:02:45 +00005118 assert( pIn3->flags&MEM_Int );
5119
drh1b26c7c2009-04-22 02:15:47 +00005120 /* If there is anything other than a rowset object in memory cell P1,
5121 ** delete it now and initialize P1 with an empty rowset
danielk19771d461462009-04-21 09:02:45 +00005122 */
drh733bf1b2009-04-22 00:47:00 +00005123 if( (pIn1->flags & MEM_RowSet)==0 ){
5124 sqlite3VdbeMemSetRowSet(pIn1);
5125 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
danielk19771d461462009-04-21 09:02:45 +00005126 }
5127
5128 assert( pOp->p4type==P4_INT32 );
drh1b26c7c2009-04-22 02:15:47 +00005129 assert( iSet==-1 || iSet>=0 );
danielk19771d461462009-04-21 09:02:45 +00005130 if( iSet ){
shane60a4b532009-05-06 18:57:09 +00005131 exists = sqlite3RowSetTest(pIn1->u.pRowSet,
5132 (u8)(iSet>=0 ? iSet & 0xf : 0xff),
drh733bf1b2009-04-22 00:47:00 +00005133 pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005134 if( exists ){
5135 pc = pOp->p2 - 1;
5136 break;
5137 }
5138 }
5139 if( iSet>=0 ){
drh733bf1b2009-04-22 00:47:00 +00005140 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
danielk19771d461462009-04-21 09:02:45 +00005141 }
5142 break;
5143}
5144
drh5e00f6c2001-09-13 13:46:56 +00005145
danielk197793758c82005-01-21 08:13:14 +00005146#ifndef SQLITE_OMIT_TRIGGER
dan165921a2009-08-28 18:53:45 +00005147
5148/* Opcode: Program P1 P2 P3 P4 *
5149**
dan76d462e2009-08-30 11:42:51 +00005150** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
dan165921a2009-08-28 18:53:45 +00005151**
dan76d462e2009-08-30 11:42:51 +00005152** P1 contains the address of the memory cell that contains the first memory
5153** cell in an array of values used as arguments to the sub-program. P2
5154** contains the address to jump to if the sub-program throws an IGNORE
5155** exception using the RAISE() function. Register P3 contains the address
5156** of a memory cell in this (the parent) VM that is used to allocate the
5157** memory required by the sub-vdbe at runtime.
dan165921a2009-08-28 18:53:45 +00005158**
5159** P4 is a pointer to the VM containing the trigger program.
5160*/
dan76d462e2009-08-30 11:42:51 +00005161case OP_Program: { /* jump */
dan65a7cd12009-09-01 12:16:01 +00005162 int nMem; /* Number of memory registers for sub-program */
5163 int nByte; /* Bytes of runtime space required for sub-program */
5164 Mem *pRt; /* Register to allocate runtime space */
5165 Mem *pMem; /* Used to iterate through memory cells */
5166 Mem *pEnd; /* Last memory cell in new array */
5167 VdbeFrame *pFrame; /* New vdbe frame to execute in */
5168 SubProgram *pProgram; /* Sub-program to execute */
5169 void *t; /* Token identifying trigger */
5170
5171 pProgram = pOp->p4.pProgram;
drha6c2ed92009-11-14 23:22:23 +00005172 pRt = &aMem[pOp->p3];
dan165921a2009-08-28 18:53:45 +00005173 assert( pProgram->nOp>0 );
5174
dan1da40a32009-09-19 17:00:31 +00005175 /* If the p5 flag is clear, then recursive invocation of triggers is
5176 ** disabled for backwards compatibility (p5 is set if this sub-program
5177 ** is really a trigger, not a foreign key action, and the flag set
5178 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
dan165921a2009-08-28 18:53:45 +00005179 **
5180 ** It is recursive invocation of triggers, at the SQL level, that is
5181 ** disabled. In some cases a single trigger may generate more than one
5182 ** SubProgram (if the trigger may be executed with more than one different
5183 ** ON CONFLICT algorithm). SubProgram structures associated with a
5184 ** single trigger all have the same value for the SubProgram.token
dan1da40a32009-09-19 17:00:31 +00005185 ** variable. */
5186 if( pOp->p5 ){
dan65a7cd12009-09-01 12:16:01 +00005187 t = pProgram->token;
dan165921a2009-08-28 18:53:45 +00005188 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
5189 if( pFrame ) break;
5190 }
5191
danf5894502009-10-07 18:41:19 +00005192 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
dan165921a2009-08-28 18:53:45 +00005193 rc = SQLITE_ERROR;
5194 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
5195 break;
5196 }
5197
5198 /* Register pRt is used to store the memory required to save the state
5199 ** of the current program, and the memory required at runtime to execute
5200 ** the trigger program. If this trigger has been fired before, then pRt
5201 ** is already allocated. Otherwise, it must be initialized. */
5202 if( (pRt->flags&MEM_Frame)==0 ){
dan165921a2009-08-28 18:53:45 +00005203 /* SubProgram.nMem is set to the number of memory cells used by the
5204 ** program stored in SubProgram.aOp. As well as these, one memory
5205 ** cell is required for each cursor used by the program. Set local
5206 ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
5207 */
dan65a7cd12009-09-01 12:16:01 +00005208 nMem = pProgram->nMem + pProgram->nCsr;
5209 nByte = ROUND8(sizeof(VdbeFrame))
dan165921a2009-08-28 18:53:45 +00005210 + nMem * sizeof(Mem)
dan1d8cb212011-12-09 13:24:16 +00005211 + pProgram->nCsr * sizeof(VdbeCursor *)
5212 + pProgram->nOnce * sizeof(u8);
dan165921a2009-08-28 18:53:45 +00005213 pFrame = sqlite3DbMallocZero(db, nByte);
5214 if( !pFrame ){
5215 goto no_mem;
5216 }
5217 sqlite3VdbeMemRelease(pRt);
5218 pRt->flags = MEM_Frame;
5219 pRt->u.pFrame = pFrame;
5220
5221 pFrame->v = p;
5222 pFrame->nChildMem = nMem;
5223 pFrame->nChildCsr = pProgram->nCsr;
5224 pFrame->pc = pc;
5225 pFrame->aMem = p->aMem;
5226 pFrame->nMem = p->nMem;
5227 pFrame->apCsr = p->apCsr;
5228 pFrame->nCursor = p->nCursor;
5229 pFrame->aOp = p->aOp;
5230 pFrame->nOp = p->nOp;
5231 pFrame->token = pProgram->token;
dan1d8cb212011-12-09 13:24:16 +00005232 pFrame->aOnceFlag = p->aOnceFlag;
5233 pFrame->nOnceFlag = p->nOnceFlag;
dan165921a2009-08-28 18:53:45 +00005234
5235 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
5236 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
drhec86c722011-12-09 17:27:51 +00005237 pMem->flags = MEM_Invalid;
dan165921a2009-08-28 18:53:45 +00005238 pMem->db = db;
5239 }
5240 }else{
5241 pFrame = pRt->u.pFrame;
5242 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
5243 assert( pProgram->nCsr==pFrame->nChildCsr );
5244 assert( pc==pFrame->pc );
5245 }
5246
5247 p->nFrame++;
5248 pFrame->pParent = p->pFrame;
drh99a66922011-05-13 18:51:42 +00005249 pFrame->lastRowid = lastRowid;
dan76d462e2009-08-30 11:42:51 +00005250 pFrame->nChange = p->nChange;
dan2832ad42009-08-31 15:27:27 +00005251 p->nChange = 0;
dan165921a2009-08-28 18:53:45 +00005252 p->pFrame = pFrame;
drha6c2ed92009-11-14 23:22:23 +00005253 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
dan165921a2009-08-28 18:53:45 +00005254 p->nMem = pFrame->nChildMem;
shanecea72b22009-09-07 04:38:36 +00005255 p->nCursor = (u16)pFrame->nChildCsr;
drha6c2ed92009-11-14 23:22:23 +00005256 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
drhbbe879d2009-11-14 18:04:35 +00005257 p->aOp = aOp = pProgram->aOp;
dan165921a2009-08-28 18:53:45 +00005258 p->nOp = pProgram->nOp;
dan1d8cb212011-12-09 13:24:16 +00005259 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
5260 p->nOnceFlag = pProgram->nOnce;
dan165921a2009-08-28 18:53:45 +00005261 pc = -1;
dan1d8cb212011-12-09 13:24:16 +00005262 memset(p->aOnceFlag, 0, p->nOnceFlag);
dan165921a2009-08-28 18:53:45 +00005263
5264 break;
5265}
5266
dan76d462e2009-08-30 11:42:51 +00005267/* Opcode: Param P1 P2 * * *
dan165921a2009-08-28 18:53:45 +00005268**
dan76d462e2009-08-30 11:42:51 +00005269** This opcode is only ever present in sub-programs called via the
5270** OP_Program instruction. Copy a value currently stored in a memory
5271** cell of the calling (parent) frame to cell P2 in the current frames
5272** address space. This is used by trigger programs to access the new.*
5273** and old.* values.
dan165921a2009-08-28 18:53:45 +00005274**
dan76d462e2009-08-30 11:42:51 +00005275** The address of the cell in the parent frame is determined by adding
5276** the value of the P1 argument to the value of the P1 argument to the
5277** calling OP_Program instruction.
dan165921a2009-08-28 18:53:45 +00005278*/
dan76d462e2009-08-30 11:42:51 +00005279case OP_Param: { /* out2-prerelease */
dan65a7cd12009-09-01 12:16:01 +00005280 VdbeFrame *pFrame;
5281 Mem *pIn;
5282 pFrame = p->pFrame;
5283 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
dan165921a2009-08-28 18:53:45 +00005284 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
5285 break;
5286}
5287
danielk197793758c82005-01-21 08:13:14 +00005288#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00005289
dan1da40a32009-09-19 17:00:31 +00005290#ifndef SQLITE_OMIT_FOREIGN_KEY
dan32b09f22009-09-23 17:29:59 +00005291/* Opcode: FkCounter P1 P2 * * *
dan1da40a32009-09-19 17:00:31 +00005292**
dan0ff297e2009-09-25 17:03:14 +00005293** Increment a "constraint counter" by P2 (P2 may be negative or positive).
5294** If P1 is non-zero, the database constraint counter is incremented
5295** (deferred foreign key constraints). Otherwise, if P1 is zero, the
dan32b09f22009-09-23 17:29:59 +00005296** statement counter is incremented (immediate foreign key constraints).
dan1da40a32009-09-19 17:00:31 +00005297*/
dan32b09f22009-09-23 17:29:59 +00005298case OP_FkCounter: {
drh648e2642013-07-11 15:03:32 +00005299 if( db->flags & SQLITE_DeferFKs ){
5300 db->nDeferredImmCons += pOp->p2;
5301 }else if( pOp->p1 ){
dan0ff297e2009-09-25 17:03:14 +00005302 db->nDeferredCons += pOp->p2;
dan32b09f22009-09-23 17:29:59 +00005303 }else{
dan0ff297e2009-09-25 17:03:14 +00005304 p->nFkConstraint += pOp->p2;
5305 }
5306 break;
5307}
5308
5309/* Opcode: FkIfZero P1 P2 * * *
5310**
5311** This opcode tests if a foreign key constraint-counter is currently zero.
5312** If so, jump to instruction P2. Otherwise, fall through to the next
5313** instruction.
5314**
5315** If P1 is non-zero, then the jump is taken if the database constraint-counter
5316** is zero (the one that counts deferred constraint violations). If P1 is
5317** zero, the jump is taken if the statement constraint-counter is zero
5318** (immediate foreign key constraint violations).
5319*/
5320case OP_FkIfZero: { /* jump */
5321 if( pOp->p1 ){
drh648e2642013-07-11 15:03:32 +00005322 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan0ff297e2009-09-25 17:03:14 +00005323 }else{
drh648e2642013-07-11 15:03:32 +00005324 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
dan32b09f22009-09-23 17:29:59 +00005325 }
dan1da40a32009-09-19 17:00:31 +00005326 break;
5327}
5328#endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
5329
drh205f48e2004-11-05 00:43:11 +00005330#ifndef SQLITE_OMIT_AUTOINCREMENT
drh98757152008-01-09 23:04:12 +00005331/* Opcode: MemMax P1 P2 * * *
drh205f48e2004-11-05 00:43:11 +00005332**
dan76d462e2009-08-30 11:42:51 +00005333** P1 is a register in the root frame of this VM (the root frame is
5334** different from the current frame if this instruction is being executed
5335** within a sub-program). Set the value of register P1 to the maximum of
5336** its current value and the value in register P2.
drh205f48e2004-11-05 00:43:11 +00005337**
5338** This instruction throws an error if the memory cell is not initially
5339** an integer.
5340*/
dan76d462e2009-08-30 11:42:51 +00005341case OP_MemMax: { /* in2 */
5342 Mem *pIn1;
5343 VdbeFrame *pFrame;
5344 if( p->pFrame ){
5345 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
5346 pIn1 = &pFrame->aMem[pOp->p1];
5347 }else{
drha6c2ed92009-11-14 23:22:23 +00005348 pIn1 = &aMem[pOp->p1];
dan76d462e2009-08-30 11:42:51 +00005349 }
drhec86c722011-12-09 17:27:51 +00005350 assert( memIsValid(pIn1) );
drh98757152008-01-09 23:04:12 +00005351 sqlite3VdbeMemIntegerify(pIn1);
drh3c657212009-11-17 23:59:58 +00005352 pIn2 = &aMem[pOp->p2];
drh98757152008-01-09 23:04:12 +00005353 sqlite3VdbeMemIntegerify(pIn2);
5354 if( pIn1->u.i<pIn2->u.i){
5355 pIn1->u.i = pIn2->u.i;
drh205f48e2004-11-05 00:43:11 +00005356 }
5357 break;
5358}
5359#endif /* SQLITE_OMIT_AUTOINCREMENT */
5360
drh98757152008-01-09 23:04:12 +00005361/* Opcode: IfPos P1 P2 * * *
danielk1977a2dc3b12005-02-05 12:48:48 +00005362**
drh98757152008-01-09 23:04:12 +00005363** If the value of register P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005364**
drh98757152008-01-09 23:04:12 +00005365** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005366** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00005367*/
drh9cbf3422008-01-17 16:22:13 +00005368case OP_IfPos: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005369 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005370 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005371 if( pIn1->u.i>0 ){
drhec7429a2005-10-06 16:53:14 +00005372 pc = pOp->p2 - 1;
5373 }
5374 break;
5375}
5376
drh98757152008-01-09 23:04:12 +00005377/* Opcode: IfNeg P1 P2 * * *
drh15007a92006-01-08 18:10:17 +00005378**
drh98757152008-01-09 23:04:12 +00005379** If the value of register P1 is less than zero, jump to P2.
drh15007a92006-01-08 18:10:17 +00005380**
drh98757152008-01-09 23:04:12 +00005381** It is illegal to use this instruction on a register that does
drh15007a92006-01-08 18:10:17 +00005382** not contain an integer. An assertion fault will result if you try.
5383*/
drh9cbf3422008-01-17 16:22:13 +00005384case OP_IfNeg: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005385 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005386 assert( pIn1->flags&MEM_Int );
drh3c84ddf2008-01-09 02:15:38 +00005387 if( pIn1->u.i<0 ){
drh15007a92006-01-08 18:10:17 +00005388 pc = pOp->p2 - 1;
5389 }
5390 break;
5391}
5392
drh9b918ed2009-11-12 03:13:26 +00005393/* Opcode: IfZero P1 P2 P3 * *
drhec7429a2005-10-06 16:53:14 +00005394**
drh9b918ed2009-11-12 03:13:26 +00005395** The register P1 must contain an integer. Add literal P3 to the
5396** value in register P1. If the result is exactly 0, jump to P2.
drh6f58f702006-01-08 05:26:41 +00005397**
drh98757152008-01-09 23:04:12 +00005398** It is illegal to use this instruction on a register that does
drh6f58f702006-01-08 05:26:41 +00005399** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00005400*/
drh9cbf3422008-01-17 16:22:13 +00005401case OP_IfZero: { /* jump, in1 */
drh3c657212009-11-17 23:59:58 +00005402 pIn1 = &aMem[pOp->p1];
danielk1977a7a8e142008-02-13 18:25:27 +00005403 assert( pIn1->flags&MEM_Int );
drh9b918ed2009-11-12 03:13:26 +00005404 pIn1->u.i += pOp->p3;
drh3c84ddf2008-01-09 02:15:38 +00005405 if( pIn1->u.i==0 ){
drha2a49dc2008-01-02 14:28:13 +00005406 pc = pOp->p2 - 1;
5407 }
5408 break;
5409}
5410
drh98757152008-01-09 23:04:12 +00005411/* Opcode: AggStep * P2 P3 P4 P5
drhe5095352002-02-24 03:25:14 +00005412**
drh0bce8352002-02-28 00:41:10 +00005413** Execute the step function for an aggregate. The
drh98757152008-01-09 23:04:12 +00005414** function has P5 arguments. P4 is a pointer to the FuncDef
5415** structure that specifies the function. Use register
5416** P3 as the accumulator.
drhe5095352002-02-24 03:25:14 +00005417**
drh98757152008-01-09 23:04:12 +00005418** The P5 arguments are taken from register P2 and its
5419** successors.
drhe5095352002-02-24 03:25:14 +00005420*/
drh9cbf3422008-01-17 16:22:13 +00005421case OP_AggStep: {
drh856c1032009-06-02 15:21:42 +00005422 int n;
drhe5095352002-02-24 03:25:14 +00005423 int i;
drhc54a6172009-06-02 16:06:03 +00005424 Mem *pMem;
5425 Mem *pRec;
danielk197722322fd2004-05-25 23:35:17 +00005426 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00005427 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00005428
drh856c1032009-06-02 15:21:42 +00005429 n = pOp->p5;
drh6810ce62004-01-31 19:22:56 +00005430 assert( n>=0 );
drha6c2ed92009-11-14 23:22:23 +00005431 pRec = &aMem[pOp->p2];
danielk19776ddcca52004-05-24 23:48:25 +00005432 apVal = p->apArg;
5433 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00005434 for(i=0; i<n; i++, pRec++){
drh2b4ded92010-09-27 21:09:31 +00005435 assert( memIsValid(pRec) );
danielk1977c572ef72004-05-27 09:28:41 +00005436 apVal[i] = pRec;
drh2b4ded92010-09-27 21:09:31 +00005437 memAboutToChange(p, pRec);
dan937d0de2009-10-15 18:35:38 +00005438 sqlite3VdbeMemStoreType(pRec);
drhe5095352002-02-24 03:25:14 +00005439 }
danielk19772dca4ac2008-01-03 11:50:29 +00005440 ctx.pFunc = pOp->p4.pFunc;
drh98757152008-01-09 23:04:12 +00005441 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005442 ctx.pMem = pMem = &aMem[pOp->p3];
drhabfcea22005-09-06 20:36:48 +00005443 pMem->n++;
drh90669c12006-01-20 15:45:36 +00005444 ctx.s.flags = MEM_Null;
5445 ctx.s.z = 0;
danielk19775f096132008-03-28 15:44:09 +00005446 ctx.s.zMalloc = 0;
drh90669c12006-01-20 15:45:36 +00005447 ctx.s.xDel = 0;
drhb21c8cd2007-08-21 19:33:56 +00005448 ctx.s.db = db;
drh1350b032002-02-27 19:00:20 +00005449 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00005450 ctx.pColl = 0;
drh7a957892012-02-02 17:35:43 +00005451 ctx.skipFlag = 0;
drhe82f5d02008-10-07 19:53:14 +00005452 if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
danielk1977dc1bdc42004-06-11 10:51:27 +00005453 assert( pOp>p->aOp );
drh66a51672008-01-03 00:01:23 +00005454 assert( pOp[-1].p4type==P4_COLLSEQ );
danielk1977dc1bdc42004-06-11 10:51:27 +00005455 assert( pOp[-1].opcode==OP_CollSeq );
danielk19772dca4ac2008-01-03 11:50:29 +00005456 ctx.pColl = pOp[-1].p4.pColl;
danielk1977dc1bdc42004-06-11 10:51:27 +00005457 }
drhee9ff672010-09-03 18:50:48 +00005458 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
drh1350b032002-02-27 19:00:20 +00005459 if( ctx.isError ){
drhf089aa42008-07-08 19:34:06 +00005460 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
drh69544ec2008-02-06 14:11:34 +00005461 rc = ctx.isError;
drh1350b032002-02-27 19:00:20 +00005462 }
drh7a957892012-02-02 17:35:43 +00005463 if( ctx.skipFlag ){
5464 assert( pOp[-1].opcode==OP_CollSeq );
5465 i = pOp[-1].p1;
5466 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
5467 }
drhbdaec522011-04-04 00:14:43 +00005468
drh90669c12006-01-20 15:45:36 +00005469 sqlite3VdbeMemRelease(&ctx.s);
drhbdaec522011-04-04 00:14:43 +00005470
drh5e00f6c2001-09-13 13:46:56 +00005471 break;
5472}
5473
drh98757152008-01-09 23:04:12 +00005474/* Opcode: AggFinal P1 P2 * P4 *
drh5e00f6c2001-09-13 13:46:56 +00005475**
drh13449892005-09-07 21:22:45 +00005476** Execute the finalizer function for an aggregate. P1 is
5477** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00005478**
5479** P2 is the number of arguments that the step function takes and
drh66a51672008-01-03 00:01:23 +00005480** P4 is a pointer to the FuncDef for this function. The P2
drha10a34b2005-09-07 22:09:48 +00005481** argument is not used by this opcode. It is only there to disambiguate
5482** functions that can take varying numbers of arguments. The
drh66a51672008-01-03 00:01:23 +00005483** P4 argument is only needed for the degenerate case where
drha10a34b2005-09-07 22:09:48 +00005484** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00005485*/
drh9cbf3422008-01-17 16:22:13 +00005486case OP_AggFinal: {
drh13449892005-09-07 21:22:45 +00005487 Mem *pMem;
drh0a07c102008-01-03 18:03:08 +00005488 assert( pOp->p1>0 && pOp->p1<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005489 pMem = &aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00005490 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
danielk19772dca4ac2008-01-03 11:50:29 +00005491 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
drh4c8555f2009-06-25 01:47:11 +00005492 if( rc ){
drhf089aa42008-07-08 19:34:06 +00005493 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
drh90669c12006-01-20 15:45:36 +00005494 }
drh2dca8682008-03-21 17:13:13 +00005495 sqlite3VdbeChangeEncoding(pMem, encoding);
drhb7654112008-01-12 12:48:07 +00005496 UPDATE_MAX_BLOBSIZE(pMem);
drh023ae032007-05-08 12:12:16 +00005497 if( sqlite3VdbeMemTooBig(pMem) ){
5498 goto too_big;
5499 }
drh5e00f6c2001-09-13 13:46:56 +00005500 break;
5501}
5502
dan5cf53532010-05-01 16:40:20 +00005503#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +00005504/* Opcode: Checkpoint P1 P2 P3 * *
dane04dc882010-04-20 18:53:15 +00005505**
5506** Checkpoint database P1. This is a no-op if P1 is not currently in
dancdc1f042010-11-18 12:11:05 +00005507** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
drh30aa3b92011-02-07 23:56:01 +00005508** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns
5509** SQLITE_BUSY or not, respectively. Write the number of pages in the
5510** WAL after the checkpoint into mem[P3+1] and the number of pages
5511** in the WAL that have been checkpointed after the checkpoint
5512** completes into mem[P3+2]. However on an error, mem[P3+1] and
5513** mem[P3+2] are initialized to -1.
dan7c246102010-04-12 19:00:29 +00005514*/
5515case OP_Checkpoint: {
drh30aa3b92011-02-07 23:56:01 +00005516 int i; /* Loop counter */
5517 int aRes[3]; /* Results */
5518 Mem *pMem; /* Write results here */
5519
drh9e92a472013-06-27 17:40:30 +00005520 assert( p->readOnly==0 );
drh30aa3b92011-02-07 23:56:01 +00005521 aRes[0] = 0;
5522 aRes[1] = aRes[2] = -1;
dancdc1f042010-11-18 12:11:05 +00005523 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
5524 || pOp->p2==SQLITE_CHECKPOINT_FULL
5525 || pOp->p2==SQLITE_CHECKPOINT_RESTART
5526 );
drh30aa3b92011-02-07 23:56:01 +00005527 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
dancdc1f042010-11-18 12:11:05 +00005528 if( rc==SQLITE_BUSY ){
5529 rc = SQLITE_OK;
drh30aa3b92011-02-07 23:56:01 +00005530 aRes[0] = 1;
dancdc1f042010-11-18 12:11:05 +00005531 }
drh30aa3b92011-02-07 23:56:01 +00005532 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
5533 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
5534 }
dan7c246102010-04-12 19:00:29 +00005535 break;
5536};
dan5cf53532010-05-01 16:40:20 +00005537#endif
drh5e00f6c2001-09-13 13:46:56 +00005538
drhcac29a62010-07-02 19:36:52 +00005539#ifndef SQLITE_OMIT_PRAGMA
drhab9b7442010-05-10 11:20:05 +00005540/* Opcode: JournalMode P1 P2 P3 * P5
dane04dc882010-04-20 18:53:15 +00005541**
5542** Change the journal mode of database P1 to P3. P3 must be one of the
5543** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
5544** modes (delete, truncate, persist, off and memory), this is a simple
5545** operation. No IO is required.
5546**
5547** If changing into or out of WAL mode the procedure is more complicated.
5548**
5549** Write a string containing the final journal-mode to register P2.
5550*/
drhd80b2332010-05-01 00:59:37 +00005551case OP_JournalMode: { /* out2-prerelease */
dane04dc882010-04-20 18:53:15 +00005552 Btree *pBt; /* Btree to change journal mode of */
5553 Pager *pPager; /* Pager associated with pBt */
drhd80b2332010-05-01 00:59:37 +00005554 int eNew; /* New journal mode */
5555 int eOld; /* The old journal mode */
mistachkin59ee77c2012-09-13 15:26:44 +00005556#ifndef SQLITE_OMIT_WAL
drhd80b2332010-05-01 00:59:37 +00005557 const char *zFilename; /* Name of database file for pPager */
mistachkin59ee77c2012-09-13 15:26:44 +00005558#endif
dane04dc882010-04-20 18:53:15 +00005559
drhd80b2332010-05-01 00:59:37 +00005560 eNew = pOp->p3;
dane04dc882010-04-20 18:53:15 +00005561 assert( eNew==PAGER_JOURNALMODE_DELETE
5562 || eNew==PAGER_JOURNALMODE_TRUNCATE
5563 || eNew==PAGER_JOURNALMODE_PERSIST
5564 || eNew==PAGER_JOURNALMODE_OFF
5565 || eNew==PAGER_JOURNALMODE_MEMORY
5566 || eNew==PAGER_JOURNALMODE_WAL
5567 || eNew==PAGER_JOURNALMODE_QUERY
5568 );
5569 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh9e92a472013-06-27 17:40:30 +00005570 assert( p->readOnly==0 );
drh3ebaee92010-05-06 21:37:22 +00005571
dane04dc882010-04-20 18:53:15 +00005572 pBt = db->aDb[pOp->p1].pBt;
5573 pPager = sqlite3BtreePager(pBt);
drh0b9b4302010-06-11 17:01:24 +00005574 eOld = sqlite3PagerGetJournalMode(pPager);
5575 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
5576 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
dan5cf53532010-05-01 16:40:20 +00005577
5578#ifndef SQLITE_OMIT_WAL
drhd4e0bb02012-05-27 01:19:04 +00005579 zFilename = sqlite3PagerFilename(pPager, 1);
dane04dc882010-04-20 18:53:15 +00005580
drhd80b2332010-05-01 00:59:37 +00005581 /* Do not allow a transition to journal_mode=WAL for a database
drh6e1f4822010-07-13 23:41:40 +00005582 ** in temporary storage or if the VFS does not support shared memory
drhd80b2332010-05-01 00:59:37 +00005583 */
5584 if( eNew==PAGER_JOURNALMODE_WAL
drh057fc812011-10-17 23:15:31 +00005585 && (sqlite3Strlen30(zFilename)==0 /* Temp file */
drh6e1f4822010-07-13 23:41:40 +00005586 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */
dane180c292010-04-26 17:42:56 +00005587 ){
drh0b9b4302010-06-11 17:01:24 +00005588 eNew = eOld;
dane180c292010-04-26 17:42:56 +00005589 }
5590
drh0b9b4302010-06-11 17:01:24 +00005591 if( (eNew!=eOld)
5592 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
5593 ){
danc0537fe2013-06-28 19:41:43 +00005594 if( !db->autoCommit || db->nVdbeRead>1 ){
drh0b9b4302010-06-11 17:01:24 +00005595 rc = SQLITE_ERROR;
5596 sqlite3SetString(&p->zErrMsg, db,
5597 "cannot change %s wal mode from within a transaction",
5598 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
5599 );
5600 break;
5601 }else{
5602
5603 if( eOld==PAGER_JOURNALMODE_WAL ){
5604 /* If leaving WAL mode, close the log file. If successful, the call
5605 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
5606 ** file. An EXCLUSIVE lock may still be held on the database file
5607 ** after a successful return.
dane04dc882010-04-20 18:53:15 +00005608 */
drh0b9b4302010-06-11 17:01:24 +00005609 rc = sqlite3PagerCloseWal(pPager);
drhab9b7442010-05-10 11:20:05 +00005610 if( rc==SQLITE_OK ){
drh0b9b4302010-06-11 17:01:24 +00005611 sqlite3PagerSetJournalMode(pPager, eNew);
drh89c3f2f2010-05-15 01:09:38 +00005612 }
drh242c4f72010-06-22 14:49:39 +00005613 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
5614 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
5615 ** as an intermediate */
5616 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
drh0b9b4302010-06-11 17:01:24 +00005617 }
5618
5619 /* Open a transaction on the database file. Regardless of the journal
5620 ** mode, this transaction always uses a rollback journal.
5621 */
5622 assert( sqlite3BtreeIsInTrans(pBt)==0 );
5623 if( rc==SQLITE_OK ){
dan731bf5b2010-06-17 16:44:21 +00005624 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
dane04dc882010-04-20 18:53:15 +00005625 }
5626 }
5627 }
dan5cf53532010-05-01 16:40:20 +00005628#endif /* ifndef SQLITE_OMIT_WAL */
dane04dc882010-04-20 18:53:15 +00005629
dand956efe2010-06-18 16:13:45 +00005630 if( rc ){
dand956efe2010-06-18 16:13:45 +00005631 eNew = eOld;
5632 }
drh0b9b4302010-06-11 17:01:24 +00005633 eNew = sqlite3PagerSetJournalMode(pPager, eNew);
dan731bf5b2010-06-17 16:44:21 +00005634
dane04dc882010-04-20 18:53:15 +00005635 pOut = &aMem[pOp->p2];
5636 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
danb9780022010-04-21 18:37:57 +00005637 pOut->z = (char *)sqlite3JournalModename(eNew);
dane04dc882010-04-20 18:53:15 +00005638 pOut->n = sqlite3Strlen30(pOut->z);
5639 pOut->enc = SQLITE_UTF8;
5640 sqlite3VdbeChangeEncoding(pOut, encoding);
5641 break;
drhcac29a62010-07-02 19:36:52 +00005642};
5643#endif /* SQLITE_OMIT_PRAGMA */
dane04dc882010-04-20 18:53:15 +00005644
drhfdbcdee2007-03-27 14:44:50 +00005645#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
drh98757152008-01-09 23:04:12 +00005646/* Opcode: Vacuum * * * * *
drh6f8c91c2003-12-07 00:24:35 +00005647**
5648** Vacuum the entire database. This opcode will cause other virtual
5649** machines to be created and run. It may not be called from within
5650** a transaction.
5651*/
drh9cbf3422008-01-17 16:22:13 +00005652case OP_Vacuum: {
drh9e92a472013-06-27 17:40:30 +00005653 assert( p->readOnly==0 );
danielk19774adee202004-05-08 08:23:19 +00005654 rc = sqlite3RunVacuum(&p->zErrMsg, db);
drh6f8c91c2003-12-07 00:24:35 +00005655 break;
5656}
drh154d4b22006-09-21 11:02:16 +00005657#endif
drh6f8c91c2003-12-07 00:24:35 +00005658
danielk1977dddbcdc2007-04-26 14:42:34 +00005659#if !defined(SQLITE_OMIT_AUTOVACUUM)
drh98757152008-01-09 23:04:12 +00005660/* Opcode: IncrVacuum P1 P2 * * *
danielk1977dddbcdc2007-04-26 14:42:34 +00005661**
5662** Perform a single step of the incremental vacuum procedure on
drhca5557f2007-05-04 18:30:40 +00005663** the P1 database. If the vacuum has finished, jump to instruction
danielk1977dddbcdc2007-04-26 14:42:34 +00005664** P2. Otherwise, fall through to the next instruction.
5665*/
drh9cbf3422008-01-17 16:22:13 +00005666case OP_IncrVacuum: { /* jump */
drhca5557f2007-05-04 18:30:40 +00005667 Btree *pBt;
5668
5669 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005670 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
drh9e92a472013-06-27 17:40:30 +00005671 assert( p->readOnly==0 );
drhca5557f2007-05-04 18:30:40 +00005672 pBt = db->aDb[pOp->p1].pBt;
danielk1977dddbcdc2007-04-26 14:42:34 +00005673 rc = sqlite3BtreeIncrVacuum(pBt);
5674 if( rc==SQLITE_DONE ){
5675 pc = pOp->p2 - 1;
5676 rc = SQLITE_OK;
5677 }
5678 break;
5679}
5680#endif
5681
drh98757152008-01-09 23:04:12 +00005682/* Opcode: Expire P1 * * * *
danielk1977a21c6b62005-01-24 10:25:59 +00005683**
5684** Cause precompiled statements to become expired. An expired statement
5685** fails with an error code of SQLITE_SCHEMA if it is ever executed
5686** (via sqlite3_step()).
5687**
5688** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
5689** then only the currently executing statement is affected.
5690*/
drh9cbf3422008-01-17 16:22:13 +00005691case OP_Expire: {
danielk1977a21c6b62005-01-24 10:25:59 +00005692 if( !pOp->p1 ){
5693 sqlite3ExpirePreparedStatements(db);
5694 }else{
5695 p->expired = 1;
5696 }
5697 break;
5698}
5699
danielk1977c00da102006-01-07 13:21:04 +00005700#ifndef SQLITE_OMIT_SHARED_CACHE
drh6a9ad3d2008-04-02 16:29:30 +00005701/* Opcode: TableLock P1 P2 P3 P4 *
danielk1977c00da102006-01-07 13:21:04 +00005702**
5703** Obtain a lock on a particular table. This instruction is only used when
5704** the shared-cache feature is enabled.
5705**
danielk197796d48e92009-06-29 06:00:37 +00005706** P1 is the index of the database in sqlite3.aDb[] of the database
drh6a9ad3d2008-04-02 16:29:30 +00005707** on which the lock is acquired. A readlock is obtained if P3==0 or
5708** a write lock if P3==1.
danielk1977c00da102006-01-07 13:21:04 +00005709**
5710** P2 contains the root-page of the table to lock.
5711**
drh66a51672008-01-03 00:01:23 +00005712** P4 contains a pointer to the name of the table being locked. This is only
danielk1977c00da102006-01-07 13:21:04 +00005713** used to generate an error message if the lock cannot be obtained.
5714*/
drh9cbf3422008-01-17 16:22:13 +00005715case OP_TableLock: {
danielk1977e0d9e6f2009-07-03 16:25:06 +00005716 u8 isWriteLock = (u8)pOp->p3;
5717 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
5718 int p1 = pOp->p1;
5719 assert( p1>=0 && p1<db->nDb );
drhdddd7792011-04-03 18:19:25 +00005720 assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
danielk1977e0d9e6f2009-07-03 16:25:06 +00005721 assert( isWriteLock==0 || isWriteLock==1 );
5722 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
5723 if( (rc&0xFF)==SQLITE_LOCKED ){
5724 const char *z = pOp->p4.z;
5725 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
5726 }
danielk1977c00da102006-01-07 13:21:04 +00005727 }
5728 break;
5729}
drhb9bb7c12006-06-11 23:41:55 +00005730#endif /* SQLITE_OMIT_SHARED_CACHE */
5731
5732#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005733/* Opcode: VBegin * * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005734**
danielk19773e3a84d2008-08-01 17:37:40 +00005735** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
5736** xBegin method for that table.
5737**
5738** Also, whether or not P4 is set, check that this is not being called from
danielk1977404ca072009-03-16 13:19:36 +00005739** within a callback to a virtual table xSync() method. If it is, the error
5740** code will be set to SQLITE_LOCKED.
drhb9bb7c12006-06-11 23:41:55 +00005741*/
drh9cbf3422008-01-17 16:22:13 +00005742case OP_VBegin: {
danielk1977595a5232009-07-24 17:58:53 +00005743 VTable *pVTab;
5744 pVTab = pOp->p4.pVtab;
5745 rc = sqlite3VtabBegin(db, pVTab);
drhb9755982010-07-24 16:34:37 +00005746 if( pVTab ) importVtabErrMsg(p, pVTab->pVtab);
danielk1977f9e7dda2006-06-16 16:08:53 +00005747 break;
5748}
5749#endif /* SQLITE_OMIT_VIRTUALTABLE */
5750
5751#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005752/* Opcode: VCreate P1 * * P4 *
danielk1977f9e7dda2006-06-16 16:08:53 +00005753**
drh66a51672008-01-03 00:01:23 +00005754** P4 is the name of a virtual table in database P1. Call the xCreate method
danielk1977f9e7dda2006-06-16 16:08:53 +00005755** for that table.
5756*/
drh9cbf3422008-01-17 16:22:13 +00005757case OP_VCreate: {
danielk19772dca4ac2008-01-03 11:50:29 +00005758 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00005759 break;
5760}
5761#endif /* SQLITE_OMIT_VIRTUALTABLE */
5762
5763#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005764/* Opcode: VDestroy P1 * * P4 *
drhb9bb7c12006-06-11 23:41:55 +00005765**
drh66a51672008-01-03 00:01:23 +00005766** P4 is the name of a virtual table in database P1. Call the xDestroy method
danielk19779e39ce82006-06-12 16:01:21 +00005767** of that table.
drhb9bb7c12006-06-11 23:41:55 +00005768*/
drh9cbf3422008-01-17 16:22:13 +00005769case OP_VDestroy: {
danielk1977212b2182006-06-23 14:32:08 +00005770 p->inVtabMethod = 2;
danielk19772dca4ac2008-01-03 11:50:29 +00005771 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
danielk1977212b2182006-06-23 14:32:08 +00005772 p->inVtabMethod = 0;
drhb9bb7c12006-06-11 23:41:55 +00005773 break;
5774}
5775#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00005776
drh9eff6162006-06-12 21:59:13 +00005777#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005778/* Opcode: VOpen P1 * * P4 *
drh9eff6162006-06-12 21:59:13 +00005779**
drh66a51672008-01-03 00:01:23 +00005780** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
drh9eff6162006-06-12 21:59:13 +00005781** P1 is a cursor number. This opcode opens a cursor to the virtual
5782** table and stores that cursor in P1.
5783*/
drh9cbf3422008-01-17 16:22:13 +00005784case OP_VOpen: {
drh856c1032009-06-02 15:21:42 +00005785 VdbeCursor *pCur;
5786 sqlite3_vtab_cursor *pVtabCursor;
5787 sqlite3_vtab *pVtab;
5788 sqlite3_module *pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005789
drh1713afb2013-06-28 01:24:57 +00005790 assert( p->bIsReader );
drh856c1032009-06-02 15:21:42 +00005791 pCur = 0;
5792 pVtabCursor = 0;
danielk1977595a5232009-07-24 17:58:53 +00005793 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00005794 pModule = (sqlite3_module *)pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005795 assert(pVtab && pModule);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005796 rc = pModule->xOpen(pVtab, &pVtabCursor);
drhb9755982010-07-24 16:34:37 +00005797 importVtabErrMsg(p, pVtab);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005798 if( SQLITE_OK==rc ){
shane21e7feb2008-05-30 15:59:49 +00005799 /* Initialize sqlite3_vtab_cursor base class */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005800 pVtabCursor->pVtab = pVtab;
5801
mistachkin48864df2013-03-21 21:20:32 +00005802 /* Initialize vdbe cursor object */
danielk1977d336e222009-02-20 10:58:41 +00005803 pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
danielk1977be718892006-06-23 08:05:19 +00005804 if( pCur ){
5805 pCur->pVtabCursor = pVtabCursor;
5806 pCur->pModule = pVtabCursor->pVtab->pModule;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005807 }else{
drh17435752007-08-16 04:30:38 +00005808 db->mallocFailed = 1;
danielk1977b7a2f2e2006-06-23 11:34:54 +00005809 pModule->xClose(pVtabCursor);
danielk1977be718892006-06-23 08:05:19 +00005810 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005811 }
drh9eff6162006-06-12 21:59:13 +00005812 break;
5813}
5814#endif /* SQLITE_OMIT_VIRTUALTABLE */
5815
5816#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19776dbee812008-01-03 18:39:41 +00005817/* Opcode: VFilter P1 P2 P3 P4 *
drh9eff6162006-06-12 21:59:13 +00005818**
5819** P1 is a cursor opened using VOpen. P2 is an address to jump to if
5820** the filtered result set is empty.
5821**
drh66a51672008-01-03 00:01:23 +00005822** P4 is either NULL or a string that was generated by the xBestIndex
5823** method of the module. The interpretation of the P4 string is left
drh4be8b512006-06-13 23:51:34 +00005824** to the module implementation.
danielk19775fac9f82006-06-13 14:16:58 +00005825**
drh9eff6162006-06-12 21:59:13 +00005826** This opcode invokes the xFilter method on the virtual table specified
danielk19776dbee812008-01-03 18:39:41 +00005827** by P1. The integer query plan parameter to xFilter is stored in register
5828** P3. Register P3+1 stores the argc parameter to be passed to the
drh174edc62008-05-29 05:23:41 +00005829** xFilter method. Registers P3+2..P3+1+argc are the argc
5830** additional parameters which are passed to
danielk19776dbee812008-01-03 18:39:41 +00005831** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
danielk1977b7a7b9a2006-06-13 10:24:42 +00005832**
danielk19776dbee812008-01-03 18:39:41 +00005833** A jump is made to P2 if the result set after filtering would be empty.
drh9eff6162006-06-12 21:59:13 +00005834*/
drh9cbf3422008-01-17 16:22:13 +00005835case OP_VFilter: { /* jump */
danielk1977b7a7b9a2006-06-13 10:24:42 +00005836 int nArg;
danielk19776dbee812008-01-03 18:39:41 +00005837 int iQuery;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005838 const sqlite3_module *pModule;
drh856c1032009-06-02 15:21:42 +00005839 Mem *pQuery;
5840 Mem *pArgc;
drh4dc754d2008-07-23 18:17:32 +00005841 sqlite3_vtab_cursor *pVtabCursor;
5842 sqlite3_vtab *pVtab;
drh856c1032009-06-02 15:21:42 +00005843 VdbeCursor *pCur;
5844 int res;
5845 int i;
5846 Mem **apArg;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005847
drha6c2ed92009-11-14 23:22:23 +00005848 pQuery = &aMem[pOp->p3];
drh856c1032009-06-02 15:21:42 +00005849 pArgc = &pQuery[1];
5850 pCur = p->apCsr[pOp->p1];
drh2b4ded92010-09-27 21:09:31 +00005851 assert( memIsValid(pQuery) );
drh5b6afba2008-01-05 16:29:28 +00005852 REGISTER_TRACE(pOp->p3, pQuery);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005853 assert( pCur->pVtabCursor );
drh4dc754d2008-07-23 18:17:32 +00005854 pVtabCursor = pCur->pVtabCursor;
5855 pVtab = pVtabCursor->pVtab;
5856 pModule = pVtab->pModule;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005857
drh9cbf3422008-01-17 16:22:13 +00005858 /* Grab the index number and argc parameters */
danielk19776dbee812008-01-03 18:39:41 +00005859 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
drh9c1905f2008-12-10 22:32:56 +00005860 nArg = (int)pArgc->u.i;
5861 iQuery = (int)pQuery->u.i;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005862
drh644a5292006-12-20 14:53:38 +00005863 /* Invoke the xFilter method */
5864 {
drh856c1032009-06-02 15:21:42 +00005865 res = 0;
5866 apArg = p->apArg;
drh4be8b512006-06-13 23:51:34 +00005867 for(i = 0; i<nArg; i++){
danielk19776dbee812008-01-03 18:39:41 +00005868 apArg[i] = &pArgc[i+1];
dan937d0de2009-10-15 18:35:38 +00005869 sqlite3VdbeMemStoreType(apArg[i]);
danielk19775fac9f82006-06-13 14:16:58 +00005870 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005871
danielk1977be718892006-06-23 08:05:19 +00005872 p->inVtabMethod = 1;
drh4dc754d2008-07-23 18:17:32 +00005873 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
danielk1977be718892006-06-23 08:05:19 +00005874 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005875 importVtabErrMsg(p, pVtab);
danielk1977a298e902006-06-22 09:53:48 +00005876 if( rc==SQLITE_OK ){
drh4dc754d2008-07-23 18:17:32 +00005877 res = pModule->xEof(pVtabCursor);
danielk1977a298e902006-06-22 09:53:48 +00005878 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005879
danielk1977a298e902006-06-22 09:53:48 +00005880 if( res ){
danielk1977b7a7b9a2006-06-13 10:24:42 +00005881 pc = pOp->p2 - 1;
5882 }
5883 }
drh1d454a32008-01-31 19:34:51 +00005884 pCur->nullRow = 0;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005885
drh9eff6162006-06-12 21:59:13 +00005886 break;
5887}
5888#endif /* SQLITE_OMIT_VIRTUALTABLE */
5889
5890#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005891/* Opcode: VColumn P1 P2 P3 * *
drh9eff6162006-06-12 21:59:13 +00005892**
drh2133d822008-01-03 18:44:59 +00005893** Store the value of the P2-th column of
5894** the row of the virtual-table that the
5895** P1 cursor is pointing to into register P3.
drh9eff6162006-06-12 21:59:13 +00005896*/
5897case OP_VColumn: {
danielk19773e3a84d2008-08-01 17:37:40 +00005898 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005899 const sqlite3_module *pModule;
drhde4fcfd2008-01-19 23:50:26 +00005900 Mem *pDest;
5901 sqlite3_context sContext;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005902
drhdfe88ec2008-11-03 20:55:06 +00005903 VdbeCursor *pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005904 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005905 assert( pOp->p3>0 && pOp->p3<=p->nMem );
drha6c2ed92009-11-14 23:22:23 +00005906 pDest = &aMem[pOp->p3];
drh2b4ded92010-09-27 21:09:31 +00005907 memAboutToChange(p, pDest);
drh2945b4a2008-01-31 15:53:45 +00005908 if( pCur->nullRow ){
5909 sqlite3VdbeMemSetNull(pDest);
5910 break;
5911 }
danielk19773e3a84d2008-08-01 17:37:40 +00005912 pVtab = pCur->pVtabCursor->pVtab;
5913 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005914 assert( pModule->xColumn );
5915 memset(&sContext, 0, sizeof(sContext));
danielk1977a7a8e142008-02-13 18:25:27 +00005916
5917 /* The output cell may already have a buffer allocated. Move
5918 ** the current contents to sContext.s so in case the user-function
5919 ** can use the already allocated buffer instead of allocating a
5920 ** new one.
5921 */
5922 sqlite3VdbeMemMove(&sContext.s, pDest);
5923 MemSetTypeFlag(&sContext.s, MEM_Null);
5924
drhde4fcfd2008-01-19 23:50:26 +00005925 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
drhb9755982010-07-24 16:34:37 +00005926 importVtabErrMsg(p, pVtab);
drh4c8555f2009-06-25 01:47:11 +00005927 if( sContext.isError ){
5928 rc = sContext.isError;
5929 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00005930
drhde4fcfd2008-01-19 23:50:26 +00005931 /* Copy the result of the function to the P3 register. We
shanebe217792009-03-05 04:20:31 +00005932 ** do this regardless of whether or not an error occurred to ensure any
drhde4fcfd2008-01-19 23:50:26 +00005933 ** dynamic allocation in sContext.s (a Mem struct) is released.
5934 */
5935 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
drhde4fcfd2008-01-19 23:50:26 +00005936 sqlite3VdbeMemMove(pDest, &sContext.s);
drh5ff44372009-11-24 16:26:17 +00005937 REGISTER_TRACE(pOp->p3, pDest);
drhde4fcfd2008-01-19 23:50:26 +00005938 UPDATE_MAX_BLOBSIZE(pDest);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005939
drhde4fcfd2008-01-19 23:50:26 +00005940 if( sqlite3VdbeMemTooBig(pDest) ){
5941 goto too_big;
5942 }
drh9eff6162006-06-12 21:59:13 +00005943 break;
5944}
5945#endif /* SQLITE_OMIT_VIRTUALTABLE */
5946
5947#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005948/* Opcode: VNext P1 P2 * * *
drh9eff6162006-06-12 21:59:13 +00005949**
5950** Advance virtual table P1 to the next row in its result set and
5951** jump to instruction P2. Or, if the virtual table has reached
5952** the end of its result set, then fall through to the next instruction.
5953*/
drh9cbf3422008-01-17 16:22:13 +00005954case OP_VNext: { /* jump */
danielk19773e3a84d2008-08-01 17:37:40 +00005955 sqlite3_vtab *pVtab;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005956 const sqlite3_module *pModule;
drhc54a6172009-06-02 16:06:03 +00005957 int res;
drh856c1032009-06-02 15:21:42 +00005958 VdbeCursor *pCur;
danielk1977b7a7b9a2006-06-13 10:24:42 +00005959
drhc54a6172009-06-02 16:06:03 +00005960 res = 0;
drh856c1032009-06-02 15:21:42 +00005961 pCur = p->apCsr[pOp->p1];
danielk1977b7a7b9a2006-06-13 10:24:42 +00005962 assert( pCur->pVtabCursor );
drh2945b4a2008-01-31 15:53:45 +00005963 if( pCur->nullRow ){
5964 break;
5965 }
danielk19773e3a84d2008-08-01 17:37:40 +00005966 pVtab = pCur->pVtabCursor->pVtab;
5967 pModule = pVtab->pModule;
drhde4fcfd2008-01-19 23:50:26 +00005968 assert( pModule->xNext );
danielk1977b7a7b9a2006-06-13 10:24:42 +00005969
drhde4fcfd2008-01-19 23:50:26 +00005970 /* Invoke the xNext() method of the module. There is no way for the
5971 ** underlying implementation to return an error if one occurs during
5972 ** xNext(). Instead, if an error occurs, true is returned (indicating that
5973 ** data is available) and the error code returned when xColumn or
5974 ** some other method is next invoked on the save virtual table cursor.
5975 */
drhde4fcfd2008-01-19 23:50:26 +00005976 p->inVtabMethod = 1;
5977 rc = pModule->xNext(pCur->pVtabCursor);
5978 p->inVtabMethod = 0;
drhb9755982010-07-24 16:34:37 +00005979 importVtabErrMsg(p, pVtab);
drhde4fcfd2008-01-19 23:50:26 +00005980 if( rc==SQLITE_OK ){
5981 res = pModule->xEof(pCur->pVtabCursor);
danielk1977b7a7b9a2006-06-13 10:24:42 +00005982 }
5983
drhde4fcfd2008-01-19 23:50:26 +00005984 if( !res ){
5985 /* If there is data, jump to P2 */
5986 pc = pOp->p2 - 1;
5987 }
drh49afe3a2013-07-10 03:05:14 +00005988 goto check_for_interrupt;
drh9eff6162006-06-12 21:59:13 +00005989}
5990#endif /* SQLITE_OMIT_VIRTUALTABLE */
5991
danielk1977182c4ba2007-06-27 15:53:34 +00005992#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00005993/* Opcode: VRename P1 * * P4 *
danielk1977182c4ba2007-06-27 15:53:34 +00005994**
drh66a51672008-01-03 00:01:23 +00005995** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977182c4ba2007-06-27 15:53:34 +00005996** This opcode invokes the corresponding xRename method. The value
danielk19776dbee812008-01-03 18:39:41 +00005997** in register P1 is passed as the zName argument to the xRename method.
danielk1977182c4ba2007-06-27 15:53:34 +00005998*/
drh9cbf3422008-01-17 16:22:13 +00005999case OP_VRename: {
drh856c1032009-06-02 15:21:42 +00006000 sqlite3_vtab *pVtab;
6001 Mem *pName;
6002
danielk1977595a5232009-07-24 17:58:53 +00006003 pVtab = pOp->p4.pVtab->pVtab;
drha6c2ed92009-11-14 23:22:23 +00006004 pName = &aMem[pOp->p1];
danielk1977182c4ba2007-06-27 15:53:34 +00006005 assert( pVtab->pModule->xRename );
drh2b4ded92010-09-27 21:09:31 +00006006 assert( memIsValid(pName) );
drh9e92a472013-06-27 17:40:30 +00006007 assert( p->readOnly==0 );
drh5b6afba2008-01-05 16:29:28 +00006008 REGISTER_TRACE(pOp->p1, pName);
drh35f6b932009-06-23 14:15:04 +00006009 assert( pName->flags & MEM_Str );
drh98655a62011-10-18 22:07:47 +00006010 testcase( pName->enc==SQLITE_UTF8 );
6011 testcase( pName->enc==SQLITE_UTF16BE );
6012 testcase( pName->enc==SQLITE_UTF16LE );
6013 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
6014 if( rc==SQLITE_OK ){
6015 rc = pVtab->pModule->xRename(pVtab, pName->z);
6016 importVtabErrMsg(p, pVtab);
6017 p->expired = 0;
6018 }
danielk1977182c4ba2007-06-27 15:53:34 +00006019 break;
6020}
6021#endif
drh4cbdda92006-06-14 19:00:20 +00006022
6023#ifndef SQLITE_OMIT_VIRTUALTABLE
drh98757152008-01-09 23:04:12 +00006024/* Opcode: VUpdate P1 P2 P3 P4 *
danielk1977399918f2006-06-14 13:03:23 +00006025**
drh66a51672008-01-03 00:01:23 +00006026** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
danielk1977399918f2006-06-14 13:03:23 +00006027** This opcode invokes the corresponding xUpdate method. P2 values
danielk19772a339ff2008-01-03 17:31:44 +00006028** are contiguous memory cells starting at P3 to pass to the xUpdate
6029** invocation. The value in register (P3+P2-1) corresponds to the
6030** p2th element of the argv array passed to xUpdate.
drh4cbdda92006-06-14 19:00:20 +00006031**
6032** The xUpdate method will do a DELETE or an INSERT or both.
danielk19772a339ff2008-01-03 17:31:44 +00006033** The argv[0] element (which corresponds to memory cell P3)
6034** is the rowid of a row to delete. If argv[0] is NULL then no
6035** deletion occurs. The argv[1] element is the rowid of the new
6036** row. This can be NULL to have the virtual table select the new
6037** rowid for itself. The subsequent elements in the array are
6038** the values of columns in the new row.
drh4cbdda92006-06-14 19:00:20 +00006039**
6040** If P2==1 then no insert is performed. argv[0] is the rowid of
6041** a row to delete.
danielk19771f6eec52006-06-16 06:17:47 +00006042**
6043** P1 is a boolean flag. If it is set to true and the xUpdate call
6044** is successful, then the value returned by sqlite3_last_insert_rowid()
6045** is set to the value of the rowid for the row just inserted.
danielk1977399918f2006-06-14 13:03:23 +00006046*/
drh9cbf3422008-01-17 16:22:13 +00006047case OP_VUpdate: {
drh856c1032009-06-02 15:21:42 +00006048 sqlite3_vtab *pVtab;
6049 sqlite3_module *pModule;
6050 int nArg;
6051 int i;
6052 sqlite_int64 rowid;
6053 Mem **apArg;
6054 Mem *pX;
6055
danb061d052011-04-25 18:49:57 +00006056 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6057 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6058 );
drh9e92a472013-06-27 17:40:30 +00006059 assert( p->readOnly==0 );
danielk1977595a5232009-07-24 17:58:53 +00006060 pVtab = pOp->p4.pVtab->pVtab;
drh856c1032009-06-02 15:21:42 +00006061 pModule = (sqlite3_module *)pVtab->pModule;
6062 nArg = pOp->p2;
drh66a51672008-01-03 00:01:23 +00006063 assert( pOp->p4type==P4_VTAB );
drh35f6b932009-06-23 14:15:04 +00006064 if( ALWAYS(pModule->xUpdate) ){
danb061d052011-04-25 18:49:57 +00006065 u8 vtabOnConflict = db->vtabOnConflict;
drh856c1032009-06-02 15:21:42 +00006066 apArg = p->apArg;
drha6c2ed92009-11-14 23:22:23 +00006067 pX = &aMem[pOp->p3];
danielk19772a339ff2008-01-03 17:31:44 +00006068 for(i=0; i<nArg; i++){
drh2b4ded92010-09-27 21:09:31 +00006069 assert( memIsValid(pX) );
6070 memAboutToChange(p, pX);
dan937d0de2009-10-15 18:35:38 +00006071 sqlite3VdbeMemStoreType(pX);
drh9c419382006-06-16 21:13:21 +00006072 apArg[i] = pX;
danielk19772a339ff2008-01-03 17:31:44 +00006073 pX++;
danielk1977399918f2006-06-14 13:03:23 +00006074 }
danb061d052011-04-25 18:49:57 +00006075 db->vtabOnConflict = pOp->p5;
danielk19771f6eec52006-06-16 06:17:47 +00006076 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
danb061d052011-04-25 18:49:57 +00006077 db->vtabOnConflict = vtabOnConflict;
drhb9755982010-07-24 16:34:37 +00006078 importVtabErrMsg(p, pVtab);
drh35f6b932009-06-23 14:15:04 +00006079 if( rc==SQLITE_OK && pOp->p1 ){
danielk19771f6eec52006-06-16 06:17:47 +00006080 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
drh99a66922011-05-13 18:51:42 +00006081 db->lastRowid = lastRowid = rowid;
danielk19771f6eec52006-06-16 06:17:47 +00006082 }
drhd91c1a12013-02-09 13:58:25 +00006083 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
danb061d052011-04-25 18:49:57 +00006084 if( pOp->p5==OE_Ignore ){
6085 rc = SQLITE_OK;
6086 }else{
6087 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
6088 }
6089 }else{
6090 p->nChange++;
6091 }
danielk1977399918f2006-06-14 13:03:23 +00006092 }
drh4cbdda92006-06-14 19:00:20 +00006093 break;
danielk1977399918f2006-06-14 13:03:23 +00006094}
6095#endif /* SQLITE_OMIT_VIRTUALTABLE */
6096
danielk197759a93792008-05-15 17:48:20 +00006097#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6098/* Opcode: Pagecount P1 P2 * * *
6099**
6100** Write the current number of pages in database P1 to memory cell P2.
6101*/
6102case OP_Pagecount: { /* out2-prerelease */
drhb1299152010-03-30 22:58:33 +00006103 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
danielk197759a93792008-05-15 17:48:20 +00006104 break;
6105}
6106#endif
6107
drh60ac3f42010-11-23 18:59:27 +00006108
6109#ifndef SQLITE_OMIT_PAGER_PRAGMAS
6110/* Opcode: MaxPgcnt P1 P2 P3 * *
6111**
6112** Try to set the maximum page count for database P1 to the value in P3.
drhc84e0332010-11-23 20:25:08 +00006113** Do not let the maximum page count fall below the current page count and
6114** do not change the maximum page count value if P3==0.
6115**
drh60ac3f42010-11-23 18:59:27 +00006116** Store the maximum page count after the change in register P2.
6117*/
6118case OP_MaxPgcnt: { /* out2-prerelease */
drhc84e0332010-11-23 20:25:08 +00006119 unsigned int newMax;
drh60ac3f42010-11-23 18:59:27 +00006120 Btree *pBt;
6121
6122 pBt = db->aDb[pOp->p1].pBt;
drhc84e0332010-11-23 20:25:08 +00006123 newMax = 0;
6124 if( pOp->p3 ){
6125 newMax = sqlite3BtreeLastPage(pBt);
drh6ea28d62010-11-26 16:49:59 +00006126 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
drhc84e0332010-11-23 20:25:08 +00006127 }
6128 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
drh60ac3f42010-11-23 18:59:27 +00006129 break;
6130}
6131#endif
6132
6133
drh949f9cd2008-01-12 21:35:57 +00006134#ifndef SQLITE_OMIT_TRACE
6135/* Opcode: Trace * * * P4 *
6136**
6137** If tracing is enabled (by the sqlite3_trace()) interface, then
6138** the UTF-8 string contained in P4 is emitted on the trace callback.
6139*/
6140case OP_Trace: {
drh856c1032009-06-02 15:21:42 +00006141 char *zTrace;
drhc3f1d5f2011-05-30 23:42:16 +00006142 char *z;
drh856c1032009-06-02 15:21:42 +00006143
drh37f58e92012-09-04 21:34:26 +00006144 if( db->xTrace
6145 && !p->doingRerun
6146 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6147 ){
drhc3f1d5f2011-05-30 23:42:16 +00006148 z = sqlite3VdbeExpandSql(p, zTrace);
6149 db->xTrace(db->pTraceArg, z);
6150 sqlite3DbFree(db, z);
drh949f9cd2008-01-12 21:35:57 +00006151 }
drhc3f1d5f2011-05-30 23:42:16 +00006152#ifdef SQLITE_DEBUG
6153 if( (db->flags & SQLITE_SqlTrace)!=0
6154 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6155 ){
6156 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
6157 }
6158#endif /* SQLITE_DEBUG */
drh949f9cd2008-01-12 21:35:57 +00006159 break;
6160}
6161#endif
6162
drh91fd4d42008-01-19 20:11:25 +00006163
6164/* Opcode: Noop * * * * *
6165**
6166** Do nothing. This instruction is often useful as a jump
6167** destination.
drh5e00f6c2001-09-13 13:46:56 +00006168*/
drh91fd4d42008-01-19 20:11:25 +00006169/*
6170** The magic Explain opcode are only inserted when explain==2 (which
6171** is to say when the EXPLAIN QUERY PLAN syntax is used.)
6172** This opcode records information from the optimizer. It is the
6173** the same as a no-op. This opcodesnever appears in a real VM program.
6174*/
6175default: { /* This is really OP_Noop and OP_Explain */
drh13573c72010-01-12 17:04:07 +00006176 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
drh5e00f6c2001-09-13 13:46:56 +00006177 break;
6178}
6179
6180/*****************************************************************************
6181** The cases of the switch statement above this line should all be indented
6182** by 6 spaces. But the left-most 6 spaces have been removed to improve the
6183** readability. From this point on down, the normal indentation rules are
6184** restored.
6185*****************************************************************************/
6186 }
drh6e142f52000-06-08 13:36:40 +00006187
drh7b396862003-01-01 23:06:20 +00006188#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00006189 {
shane9bcbdad2008-05-29 20:22:37 +00006190 u64 elapsed = sqlite3Hwtime() - start;
6191 pOp->cycles += elapsed;
drh8178a752003-01-05 21:41:40 +00006192 pOp->cnt++;
6193#if 0
shane9bcbdad2008-05-29 20:22:37 +00006194 fprintf(stdout, "%10llu ", elapsed);
drhbbe879d2009-11-14 18:04:35 +00006195 sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00006196#endif
6197 }
drh7b396862003-01-01 23:06:20 +00006198#endif
6199
drh6e142f52000-06-08 13:36:40 +00006200 /* The following code adds nothing to the actual functionality
6201 ** of the program. It is only here for testing and debugging.
6202 ** On the other hand, it does burn CPU cycles every time through
6203 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
6204 */
6205#ifndef NDEBUG
drha6110402005-07-28 20:51:19 +00006206 assert( pc>=-1 && pc<p->nOp );
drhae7e1512007-05-02 16:51:59 +00006207
drhcf1023c2007-05-08 20:59:49 +00006208#ifdef SQLITE_DEBUG
drh5b6afba2008-01-05 16:29:28 +00006209 if( p->trace ){
6210 if( rc!=0 ) fprintf(p->trace,"rc=%d\n",rc);
drh3c657212009-11-17 23:59:58 +00006211 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
6212 registerTrace(p->trace, pOp->p2, &aMem[pOp->p2]);
drh75897232000-05-29 14:26:00 +00006213 }
drh3c657212009-11-17 23:59:58 +00006214 if( pOp->opflags & OPFLG_OUT3 ){
6215 registerTrace(p->trace, pOp->p3, &aMem[pOp->p3]);
drh5b6afba2008-01-05 16:29:28 +00006216 }
drh75897232000-05-29 14:26:00 +00006217 }
danielk1977b5402fb2005-01-12 07:15:04 +00006218#endif /* SQLITE_DEBUG */
6219#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00006220 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00006221
drha05a7222008-01-19 03:35:58 +00006222 /* If we reach this point, it means that execution is finished with
6223 ** an error of some kind.
drhb86ccfb2003-01-28 23:13:10 +00006224 */
drha05a7222008-01-19 03:35:58 +00006225vdbe_error_halt:
6226 assert( rc );
6227 p->rc = rc;
drha64fa912010-03-04 00:53:32 +00006228 testcase( sqlite3GlobalConfig.xLog!=0 );
6229 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
6230 pc, p->zSql, p->zErrMsg);
drh92f02c32004-09-02 14:57:08 +00006231 sqlite3VdbeHalt(p);
danielk19777eaabcd2008-07-07 14:56:56 +00006232 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
6233 rc = SQLITE_ERROR;
drhcdf011d2011-04-04 21:25:28 +00006234 if( resetSchemaOnFault>0 ){
drh81028a42012-05-15 18:28:27 +00006235 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
drhbdaec522011-04-04 00:14:43 +00006236 }
drh900b31e2007-08-28 02:27:51 +00006237
6238 /* This is the only way out of this procedure. We have to
6239 ** release the mutexes on btrees that were acquired at the
6240 ** top. */
6241vdbe_return:
drh99a66922011-05-13 18:51:42 +00006242 db->lastRowid = lastRowid;
drh77dfd5b2013-08-19 11:15:48 +00006243 testcase( nVmStep>0 );
drh9b47ee32013-08-20 03:13:51 +00006244 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
drhbdaec522011-04-04 00:14:43 +00006245 sqlite3VdbeLeave(p);
drhb86ccfb2003-01-28 23:13:10 +00006246 return rc;
6247
drh023ae032007-05-08 12:12:16 +00006248 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
6249 ** is encountered.
6250 */
6251too_big:
drhf089aa42008-07-08 19:34:06 +00006252 sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
drh023ae032007-05-08 12:12:16 +00006253 rc = SQLITE_TOOBIG;
drha05a7222008-01-19 03:35:58 +00006254 goto vdbe_error_halt;
drh023ae032007-05-08 12:12:16 +00006255
drh98640a32007-06-07 19:08:32 +00006256 /* Jump to here if a malloc() fails.
drhb86ccfb2003-01-28 23:13:10 +00006257 */
6258no_mem:
drh17435752007-08-16 04:30:38 +00006259 db->mallocFailed = 1;
drhf089aa42008-07-08 19:34:06 +00006260 sqlite3SetString(&p->zErrMsg, db, "out of memory");
drhb86ccfb2003-01-28 23:13:10 +00006261 rc = SQLITE_NOMEM;
drha05a7222008-01-19 03:35:58 +00006262 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006263
drhb86ccfb2003-01-28 23:13:10 +00006264 /* Jump to here for any other kind of fatal error. The "rc" variable
6265 ** should hold the error number.
6266 */
6267abort_due_to_error:
drha05a7222008-01-19 03:35:58 +00006268 assert( p->zErrMsg==0 );
6269 if( db->mallocFailed ) rc = SQLITE_NOMEM;
danielk19777eaabcd2008-07-07 14:56:56 +00006270 if( rc!=SQLITE_IOERR_NOMEM ){
drhf089aa42008-07-08 19:34:06 +00006271 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
danielk19777eaabcd2008-07-07 14:56:56 +00006272 }
drha05a7222008-01-19 03:35:58 +00006273 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006274
danielk19776f8a5032004-05-10 10:34:51 +00006275 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00006276 ** flag.
6277 */
6278abort_due_to_interrupt:
drh881feaa2006-07-26 01:39:30 +00006279 assert( db->u1.isInterrupted );
drh7e8b8482008-01-23 03:03:05 +00006280 rc = SQLITE_INTERRUPT;
danielk1977026d2702004-06-14 13:14:59 +00006281 p->rc = rc;
drhf089aa42008-07-08 19:34:06 +00006282 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
drha05a7222008-01-19 03:35:58 +00006283 goto vdbe_error_halt;
drhb86ccfb2003-01-28 23:13:10 +00006284}