blob: 22967d41f6cb381a22fd9c01738bef49061291dd [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
25** and 3 operands. Operands P1 and P2 are integers. Operand P3
26** is a null-terminated string. The P2 operand must be non-negative.
27** Opcodes will typically ignore one or more operands. Many opcodes
28** ignore all three operands.
29**
30** Computation results are stored on a stack. Each entry on the
drhb19a2bc2001-09-16 00:13:26 +000031** stack is either an integer, a null-terminated string, a floating point
32** number, or the SQL "NULL" value. An inplicit conversion from one
33** 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.
45**
danielk1977be8a7832006-06-13 15:00:54 +000046** $Id: vdbe.c,v 1.557 2006/06/13 15:00:55 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000047*/
48#include "sqliteInt.h"
drh6f8fd3c2003-06-07 11:33:45 +000049#include "os.h"
drh7c68d602000-10-11 19:28:51 +000050#include <ctype.h>
drh9a324642003-09-06 20:12:01 +000051#include "vdbeInt.h"
drh8f619cc2002-09-08 00:04:50 +000052
53/*
drh487ab3c2001-11-08 00:45:21 +000054** The following global variable is incremented every time a cursor
drh7cf6e4d2004-05-19 14:56:55 +000055** moves, either by the OP_MoveXX, OP_Next, or OP_Prev opcodes. The test
drh487ab3c2001-11-08 00:45:21 +000056** procedures use this information to make sure that indices are
drhac82fcf2002-09-08 17:23:41 +000057** working correctly. This variable has no function other than to
58** help verify the correct operation of the library.
drh487ab3c2001-11-08 00:45:21 +000059*/
danielk19776f8a5032004-05-10 10:34:51 +000060int sqlite3_search_count = 0;
drh487ab3c2001-11-08 00:45:21 +000061
drhf6038712004-02-08 18:07:34 +000062/*
63** When this global variable is positive, it gets decremented once before
64** each instruction in the VDBE. When reaches zero, the SQLITE_Interrupt
65** of the db.flags field is set in order to simulate and interrupt.
66**
67** This facility is used for testing purposes only. It does not function
68** in an ordinary build.
69*/
danielk19776f8a5032004-05-10 10:34:51 +000070int sqlite3_interrupt_count = 0;
drh1350b032002-02-27 19:00:20 +000071
danielk19777e18c252004-05-25 11:47:24 +000072/*
drh6bf89572004-11-03 16:27:01 +000073** The next global variable is incremented each type the OP_Sort opcode
74** is executed. The test procedures use this information to make sure that
75** sorting is occurring or not occuring at appropriate times. This variable
76** has no function other than to help verify the correct operation of the
77** library.
78*/
79int sqlite3_sort_count = 0;
80
81/*
drh6810ce62004-01-31 19:22:56 +000082** Release the memory associated with the given stack level. This
83** leaves the Mem.flags field in an inconsistent state.
drhc61053b2000-06-04 12:58:36 +000084*/
danielk1977d8123362004-06-12 09:25:12 +000085#define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); }
drh6810ce62004-01-31 19:22:56 +000086
87/*
danielk1977bd7e4602004-05-24 07:34:48 +000088** Convert the given stack entity into a string if it isn't one
89** already. Return non-zero if a malloc() fails.
90*/
91#define Stringify(P, enc) \
drhf4479502004-05-27 03:12:53 +000092 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
93 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +000094
95/*
96** Convert the given stack entity into a string that has been obtained
97** from sqliteMalloc(). This is different from Stringify() above in that
98** Stringify() will use the NBFS bytes of static string space if the string
99** will fit but this routine always mallocs for space.
100** Return non-zero if we run out of memory.
101*/
drheb2e1762004-05-27 01:53:56 +0000102#define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
103
drhd508e7f2006-01-13 01:48:59 +0000104/*
105** The header of a record consists of a sequence variable-length integers.
106** These integers are almost always small and are encoded as a single byte.
107** The following macro takes advantage this fact to provide a fast decode
108** of the integers in a record header. It is faster for the common case
109** where the integer is a single byte. It is a little slower when the
110** integer is two or more bytes. But overall it is faster.
111**
112** The following expressions are equivalent:
113**
114** x = sqlite3GetVarint32( A, &B );
115**
116** x = GetVarint( A, B );
117**
118*/
119#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
danielk1977bd7e4602004-05-24 07:34:48 +0000120
121/*
122** An ephemeral string value (signified by the MEM_Ephem flag) contains
123** a pointer to a dynamically allocated string where some other entity
124** is responsible for deallocating that string. Because the stack entry
125** does not control the string, it might be deleted without the stack
126** entry knowing it.
127**
128** This routine converts an ephemeral string into a dynamically allocated
129** string that the stack entry itself controls. In other words, it
130** converts an MEM_Ephem string into an MEM_Dyn string.
131*/
132#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000133 if( ((P)->flags&MEM_Ephem)!=0 \
134 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000135
136/*
danielk1977c572ef72004-05-27 09:28:41 +0000137** Argument pMem points at a memory cell that will be passed to a
138** user-defined function or returned to the user as the result of a query.
139** The second argument, 'db_enc' is the text encoding used by the vdbe for
140** stack variables. This routine sets the pMem->enc and pMem->type
141** variables used by the sqlite3_value_*() routines.
142*/
drh3a41a3f2004-05-30 02:14:17 +0000143#define storeTypeInfo(A,B) _storeTypeInfo(A)
144static void _storeTypeInfo(Mem *pMem){
danielk1977c572ef72004-05-27 09:28:41 +0000145 int flags = pMem->flags;
146 if( flags & MEM_Null ){
drh9c054832004-05-31 18:51:57 +0000147 pMem->type = SQLITE_NULL;
danielk1977c572ef72004-05-27 09:28:41 +0000148 }
149 else if( flags & MEM_Int ){
drh9c054832004-05-31 18:51:57 +0000150 pMem->type = SQLITE_INTEGER;
danielk1977c572ef72004-05-27 09:28:41 +0000151 }
152 else if( flags & MEM_Real ){
drh9c054832004-05-31 18:51:57 +0000153 pMem->type = SQLITE_FLOAT;
danielk1977c572ef72004-05-27 09:28:41 +0000154 }
155 else if( flags & MEM_Str ){
drh9c054832004-05-31 18:51:57 +0000156 pMem->type = SQLITE_TEXT;
danielk1977c572ef72004-05-27 09:28:41 +0000157 }else{
drh9c054832004-05-31 18:51:57 +0000158 pMem->type = SQLITE_BLOB;
danielk1977c572ef72004-05-27 09:28:41 +0000159 }
160}
danielk19778a6b5412004-05-24 07:04:25 +0000161
162/*
danielk1977106bb232004-05-21 10:08:53 +0000163** Pop the stack N times.
164*/
165static void popStack(Mem **ppTos, int N){
166 Mem *pTos = *ppTos;
167 while( N>0 ){
168 N--;
169 Release(pTos);
170 pTos--;
171 }
172 *ppTos = pTos;
173}
174
175/*
drh4774b132004-06-12 20:12:51 +0000176** Allocate cursor number iCur. Return a pointer to it. Return NULL
177** if we run out of memory.
drh8c74a8c2002-08-25 19:20:40 +0000178*/
danielk197794eb6a12005-12-15 15:22:08 +0000179static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){
drh4774b132004-06-12 20:12:51 +0000180 Cursor *pCx;
drh290c1942004-08-21 17:54:45 +0000181 assert( iCur<p->nCursor );
182 if( p->apCsr[iCur] ){
drh4774b132004-06-12 20:12:51 +0000183 sqlite3VdbeFreeCursor(p->apCsr[iCur]);
drh8c74a8c2002-08-25 19:20:40 +0000184 }
drh4774b132004-06-12 20:12:51 +0000185 p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) );
danielk197794eb6a12005-12-15 15:22:08 +0000186 if( pCx ){
187 pCx->iDb = iDb;
188 }
drh4774b132004-06-12 20:12:51 +0000189 return pCx;
drh8c74a8c2002-08-25 19:20:40 +0000190}
191
danielk19773d1bfea2004-05-14 11:00:53 +0000192/*
drh29d72102006-02-09 22:13:41 +0000193** Try to convert a value into a numeric representation if we can
194** do so without loss of information. In other words, if the string
195** looks like a number, convert it into a number. If it does not
196** look like a number, leave it alone.
197*/
198static void applyNumericAffinity(Mem *pRec){
199 if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
200 int realnum;
201 sqlite3VdbeMemNulTerminate(pRec);
202 if( (pRec->flags&MEM_Str)
203 && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
204 i64 value;
205 sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
206 if( !realnum && sqlite3atoi64(pRec->z, &value) ){
207 sqlite3VdbeMemRelease(pRec);
208 pRec->i = value;
209 pRec->flags = MEM_Int;
210 }else{
211 sqlite3VdbeMemRealify(pRec);
212 }
213 }
214 }
215}
216
217/*
drh8a512562005-11-14 22:29:05 +0000218** Processing is determine by the affinity parameter:
danielk19773d1bfea2004-05-14 11:00:53 +0000219**
drh8a512562005-11-14 22:29:05 +0000220** SQLITE_AFF_INTEGER:
221** SQLITE_AFF_REAL:
222** SQLITE_AFF_NUMERIC:
223** Try to convert pRec to an integer representation or a
224** floating-point representation if an integer representation
225** is not possible. Note that the integer representation is
226** always preferred, even if the affinity is REAL, because
227** an integer representation is more space efficient on disk.
228**
229** SQLITE_AFF_TEXT:
230** Convert pRec to a text representation.
231**
232** SQLITE_AFF_NONE:
233** No-op. pRec is unchanged.
danielk19773d1bfea2004-05-14 11:00:53 +0000234*/
danielk19778a6b5412004-05-24 07:04:25 +0000235static void applyAffinity(Mem *pRec, char affinity, u8 enc){
drh8a512562005-11-14 22:29:05 +0000236 if( affinity==SQLITE_AFF_TEXT ){
drh17c40292004-07-21 02:53:29 +0000237 /* Only attempt the conversion to TEXT if there is an integer or real
238 ** representation (blob and NULL do not get converted) but no string
239 ** representation.
240 */
241 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
242 sqlite3VdbeMemStringify(pRec, enc);
243 }
244 pRec->flags &= ~(MEM_Real|MEM_Int);
drh8a512562005-11-14 22:29:05 +0000245 }else if( affinity!=SQLITE_AFF_NONE ){
246 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
247 || affinity==SQLITE_AFF_NUMERIC );
drh29d72102006-02-09 22:13:41 +0000248 applyNumericAffinity(pRec);
249 if( pRec->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +0000250 sqlite3VdbeIntegerAffinity(pRec);
drh17c40292004-07-21 02:53:29 +0000251 }
danielk19773d1bfea2004-05-14 11:00:53 +0000252 }
253}
254
danielk1977aee18ef2005-03-09 12:26:50 +0000255/*
drh29d72102006-02-09 22:13:41 +0000256** Try to convert the type of a function argument or a result column
257** into a numeric representation. Use either INTEGER or REAL whichever
258** is appropriate. But only do the conversion if it is possible without
259** loss of information and return the revised type of the argument.
260**
261** This is an EXPERIMENTAL api and is subject to change or removal.
262*/
263int sqlite3_value_numeric_type(sqlite3_value *pVal){
264 Mem *pMem = (Mem*)pVal;
265 applyNumericAffinity(pMem);
266 storeTypeInfo(pMem, 0);
267 return pMem->type;
268}
269
270/*
danielk1977aee18ef2005-03-09 12:26:50 +0000271** Exported version of applyAffinity(). This one works on sqlite3_value*,
272** not the internal Mem* type.
273*/
274void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){
275 applyAffinity((Mem *)pVal, affinity, enc);
276}
277
danielk1977b5402fb2005-01-12 07:15:04 +0000278#ifdef SQLITE_DEBUG
drhb6f54522004-05-20 02:42:16 +0000279/*
danielk1977ca6b2912004-05-21 10:49:47 +0000280** Write a nice string representation of the contents of cell pMem
281** into buffer zBuf, length nBuf.
282*/
drh74161702006-02-24 02:53:49 +0000283void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
danielk1977ca6b2912004-05-21 10:49:47 +0000284 char *zCsr = zBuf;
285 int f = pMem->flags;
286
drh57196282004-10-06 15:41:16 +0000287 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
danielk1977bfd6cce2004-06-18 04:24:54 +0000288
danielk1977ca6b2912004-05-21 10:49:47 +0000289 if( f&MEM_Blob ){
290 int i;
291 char c;
292 if( f & MEM_Dyn ){
293 c = 'z';
294 assert( (f & (MEM_Static|MEM_Ephem))==0 );
295 }else if( f & MEM_Static ){
296 c = 't';
297 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
298 }else if( f & MEM_Ephem ){
299 c = 'e';
300 assert( (f & (MEM_Static|MEM_Dyn))==0 );
301 }else{
302 c = 's';
303 }
304
danielk1977b1bc9532004-05-22 03:05:33 +0000305 zCsr += sprintf(zCsr, "%c", c);
306 zCsr += sprintf(zCsr, "%d[", pMem->n);
danielk1977ca6b2912004-05-21 10:49:47 +0000307 for(i=0; i<16 && i<pMem->n; i++){
308 zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
309 }
310 for(i=0; i<16 && i<pMem->n; i++){
311 char z = pMem->z[i];
312 if( z<32 || z>126 ) *zCsr++ = '.';
313 else *zCsr++ = z;
314 }
315
316 zCsr += sprintf(zCsr, "]");
danielk1977b1bc9532004-05-22 03:05:33 +0000317 *zCsr = '\0';
318 }else if( f & MEM_Str ){
319 int j, k;
320 zBuf[0] = ' ';
321 if( f & MEM_Dyn ){
322 zBuf[1] = 'z';
323 assert( (f & (MEM_Static|MEM_Ephem))==0 );
324 }else if( f & MEM_Static ){
325 zBuf[1] = 't';
326 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
327 }else if( f & MEM_Ephem ){
328 zBuf[1] = 'e';
329 assert( (f & (MEM_Static|MEM_Dyn))==0 );
330 }else{
331 zBuf[1] = 's';
332 }
333 k = 2;
334 k += sprintf(&zBuf[k], "%d", pMem->n);
335 zBuf[k++] = '[';
336 for(j=0; j<15 && j<pMem->n; j++){
337 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000338 if( c>=0x20 && c<0x7f ){
339 zBuf[k++] = c;
340 }else{
341 zBuf[k++] = '.';
342 }
343 }
344 zBuf[k++] = ']';
danielk1977bfd6cce2004-06-18 04:24:54 +0000345 k += sprintf(&zBuf[k], encnames[pMem->enc]);
danielk1977b1bc9532004-05-22 03:05:33 +0000346 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000347 }
danielk1977ca6b2912004-05-21 10:49:47 +0000348}
349#endif
350
danielk197784ac9d02004-05-18 09:58:06 +0000351
drh7b396862003-01-01 23:06:20 +0000352#ifdef VDBE_PROFILE
353/*
354** The following routine only works on pentium-class processors.
drh81db88e2004-12-07 12:29:17 +0000355** It uses the RDTSC opcode to read the cycle count value out of the
drh7b396862003-01-01 23:06:20 +0000356** processor and returns that value. This can be used for high-res
357** profiling.
358*/
359__inline__ unsigned long long int hwtime(void){
360 unsigned long long int x;
361 __asm__("rdtsc\n\t"
362 "mov %%edx, %%ecx\n\t"
363 :"=A" (x));
364 return x;
365}
366#endif
367
drh8c74a8c2002-08-25 19:20:40 +0000368/*
drhcaec2f12003-01-07 02:47:47 +0000369** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000370** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000371** processing of the VDBE program is interrupted.
372**
373** This macro added to every instruction that does a jump in order to
374** implement a loop. This test used to be on every single instruction,
375** but that meant we more testing that we needed. By only testing the
376** flag on jump instructions, we get a (small) speed improvement.
377*/
378#define CHECK_FOR_INTERRUPT \
379 if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
380
381
382/*
drhb86ccfb2003-01-28 23:13:10 +0000383** Execute as much of a VDBE program as we can then return.
384**
danielk19774adee202004-05-08 08:23:19 +0000385** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000386** close the program with a final OP_Halt and to set up the callbacks
387** and the error message pointer.
388**
389** Whenever a row or result data is available, this routine will either
390** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000391** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000392**
393** If an attempt is made to open a locked database, then this routine
394** will either invoke the busy callback (if there is one) or it will
395** return SQLITE_BUSY.
396**
397** If an error occurs, an error message is written to memory obtained
398** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
399** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
400**
401** If the callback ever returns non-zero, then the program exits
402** immediately. There will be no error message but the p->rc field is
403** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
404**
drh9468c7f2003-03-07 19:50:07 +0000405** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
406** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000407**
408** Other fatal errors return SQLITE_ERROR.
409**
danielk19774adee202004-05-08 08:23:19 +0000410** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000411** used to clean up the mess that was left behind.
412*/
danielk19774adee202004-05-08 08:23:19 +0000413int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000414 Vdbe *p /* The VDBE */
415){
416 int pc; /* The program counter */
417 Op *pOp; /* Current operation */
418 int rc = SQLITE_OK; /* Value to return */
drh9bb575f2004-09-06 17:24:11 +0000419 sqlite3 *db = p->db; /* The database */
drh8079a0d2006-01-12 17:20:50 +0000420 u8 encoding = ENC(db); /* The database encoding */
drh6810ce62004-01-31 19:22:56 +0000421 Mem *pTos; /* Top entry in the operand stack */
drhb86ccfb2003-01-28 23:13:10 +0000422#ifdef VDBE_PROFILE
423 unsigned long long start; /* CPU clock count at start of opcode */
424 int origPc; /* Program counter at start of opcode */
425#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000426#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
427 int nProgressOps = 0; /* Opcodes executed since progress callback. */
428#endif
danielk1977bc04f852005-03-29 08:26:13 +0000429#ifndef NDEBUG
430 Mem *pStackLimit;
431#endif
drhb86ccfb2003-01-28 23:13:10 +0000432
433 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
434 assert( db->magic==SQLITE_MAGIC_BUSY );
danielk19772e588c72005-12-09 14:25:08 +0000435 pTos = p->pTos;
436 if( p->rc==SQLITE_NOMEM ){
437 /* This happens if a malloc() inside a call to sqlite3_column_text() or
438 ** sqlite3_column_text16() failed. */
439 goto no_mem;
440 }
drh3a840692003-01-29 22:58:26 +0000441 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
442 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000443 assert( p->explain==0 );
drhb86ccfb2003-01-28 23:13:10 +0000444 if( p->popStack ){
drh6810ce62004-01-31 19:22:56 +0000445 popStack(&pTos, p->popStack);
drhb86ccfb2003-01-28 23:13:10 +0000446 p->popStack = 0;
447 }
danielk1977106bb232004-05-21 10:08:53 +0000448 p->resOnStack = 0;
drha4afb652005-07-09 02:16:02 +0000449 db->busyHandler.nBusy = 0;
drh93581642004-02-12 13:02:55 +0000450 CHECK_FOR_INTERRUPT;
drhb86ccfb2003-01-28 23:13:10 +0000451 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000452 assert( pc>=0 && pc<p->nOp );
drh6810ce62004-01-31 19:22:56 +0000453 assert( pTos<=&p->aStack[pc] );
danielk19779e128002006-01-18 16:51:35 +0000454 if( sqlite3MallocFailed() ) goto no_mem;
drh7b396862003-01-01 23:06:20 +0000455#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000456 origPc = pc;
drh7b396862003-01-01 23:06:20 +0000457 start = hwtime();
458#endif
drh75897232000-05-29 14:26:00 +0000459 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000460
danielk19778b60e0f2005-01-12 09:10:39 +0000461 /* Only allow tracing if SQLITE_DEBUG is defined.
drh6e142f52000-06-08 13:36:40 +0000462 */
danielk19778b60e0f2005-01-12 09:10:39 +0000463#ifdef SQLITE_DEBUG
drh75897232000-05-29 14:26:00 +0000464 if( p->trace ){
drh3f7d4e42004-07-24 14:35:58 +0000465 if( pc==0 ){
466 printf("VDBE Execution Trace:\n");
467 sqlite3VdbePrintSql(p);
468 }
danielk19774adee202004-05-08 08:23:19 +0000469 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000470 }
drh66560ad2006-01-06 14:32:19 +0000471 if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){
drh3f7d4e42004-07-24 14:35:58 +0000472 sqlite3VdbePrintSql(p);
473 }
474#endif
475
drh6e142f52000-06-08 13:36:40 +0000476
drhf6038712004-02-08 18:07:34 +0000477 /* Check to see if we need to simulate an interrupt. This only happens
478 ** if we have a special test build.
479 */
480#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000481 if( sqlite3_interrupt_count>0 ){
482 sqlite3_interrupt_count--;
483 if( sqlite3_interrupt_count==0 ){
484 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000485 }
486 }
487#endif
488
danielk1977348bb5d2003-10-18 09:37:26 +0000489#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
490 /* Call the progress callback if it is configured and the required number
491 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000492 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000493 ** If the progress callback returns non-zero, exit the virtual machine with
494 ** a return code SQLITE_ABORT.
495 */
drh3914aed2004-01-31 20:40:42 +0000496 if( db->xProgress ){
497 if( db->nProgressOps==nProgressOps ){
drhf8888bb2006-05-26 19:57:19 +0000498 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh3914aed2004-01-31 20:40:42 +0000499 if( db->xProgress(db->pProgressArg)!=0 ){
drhf8888bb2006-05-26 19:57:19 +0000500 sqlite3SafetyOn(db);
drh3914aed2004-01-31 20:40:42 +0000501 rc = SQLITE_ABORT;
502 continue; /* skip to the next iteration of the for loop */
503 }
504 nProgressOps = 0;
drhf8888bb2006-05-26 19:57:19 +0000505 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk1977348bb5d2003-10-18 09:37:26 +0000506 }
drh3914aed2004-01-31 20:40:42 +0000507 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000508 }
danielk1977348bb5d2003-10-18 09:37:26 +0000509#endif
510
danielk1977bc04f852005-03-29 08:26:13 +0000511#ifndef NDEBUG
512 /* This is to check that the return value of static function
danielk19777a5147c2005-03-29 13:07:00 +0000513 ** opcodeNoPush() (see vdbeaux.c) returns values that match the
danielk1977bc04f852005-03-29 08:26:13 +0000514 ** implementation of the virtual machine in this file. If
danielk19777a5147c2005-03-29 13:07:00 +0000515 ** opcodeNoPush() returns non-zero, then the stack is guarenteed
danielk1977bc04f852005-03-29 08:26:13 +0000516 ** not to grow when the opcode is executed. If it returns zero, then
517 ** the stack may grow by at most 1.
518 **
519 ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not
520 ** available if NDEBUG is defined at build time.
521 */
522 pStackLimit = pTos;
danielk19777a5147c2005-03-29 13:07:00 +0000523 if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){
danielk1977bc04f852005-03-29 08:26:13 +0000524 pStackLimit++;
525 }
526#endif
527
drh75897232000-05-29 14:26:00 +0000528 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000529
drh5e00f6c2001-09-13 13:46:56 +0000530/*****************************************************************************
531** What follows is a massive switch statement where each case implements a
532** separate instruction in the virtual machine. If we follow the usual
533** indentation conventions, each case should be indented by 6 spaces. But
534** that is a lot of wasted space on the left margin. So the code within
535** the switch statement will break with convention and be flush-left. Another
536** big comment (similar to this one) will mark the point in the code where
537** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000538**
539** The formatting of each case is important. The makefile for SQLite
540** generates two C files "opcodes.h" and "opcodes.c" by scanning this
541** file looking for lines that begin with "case OP_". The opcodes.h files
542** will be filled with #defines that give unique integer values to each
543** opcode and the opcodes.c file is filled with an array of strings where
drhf2bc0132004-10-04 13:19:23 +0000544** each string is the symbolic name for the corresponding opcode. If the
545** case statement is followed by a comment of the form "/# same as ... #/"
546** that comment is used to determine the particular value of the opcode.
drhac82fcf2002-09-08 17:23:41 +0000547**
danielk1977bc04f852005-03-29 08:26:13 +0000548** If a comment on the same line as the "case OP_" construction contains
danielk19777a5147c2005-03-29 13:07:00 +0000549** the word "no-push", then the opcode is guarenteed not to grow the
550** vdbe stack when it is executed. See function opcode() in
danielk1977bc04f852005-03-29 08:26:13 +0000551** vdbeaux.c for details.
552**
drhac82fcf2002-09-08 17:23:41 +0000553** Documentation about VDBE opcodes is generated by scanning this file
554** for lines of that contain "Opcode:". That line and all subsequent
555** comment lines are used in the generation of the opcode.html documentation
556** file.
557**
558** SUMMARY:
559**
560** Formatting is important to scripts that scan this file.
561** Do not deviate from the formatting style currently in use.
562**
drh5e00f6c2001-09-13 13:46:56 +0000563*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000564
drh58a11682001-11-10 13:51:08 +0000565/* Opcode: Goto * P2 *
drh5e00f6c2001-09-13 13:46:56 +0000566**
567** An unconditional jump to address P2.
568** The next instruction executed will be
569** the one at index P2 from the beginning of
570** the program.
571*/
danielk19777a5147c2005-03-29 13:07:00 +0000572case OP_Goto: { /* no-push */
drhcaec2f12003-01-07 02:47:47 +0000573 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000574 pc = pOp->p2 - 1;
575 break;
576}
drh75897232000-05-29 14:26:00 +0000577
drh8c74a8c2002-08-25 19:20:40 +0000578/* Opcode: Gosub * P2 *
579**
580** Push the current address plus 1 onto the return address stack
581** and then jump to address P2.
582**
583** The return address stack is of limited depth. If too many
584** OP_Gosub operations occur without intervening OP_Returns, then
585** the return address stack will fill up and processing will abort
586** with a fatal error.
587*/
danielk19777a5147c2005-03-29 13:07:00 +0000588case OP_Gosub: { /* no-push */
drh17c40292004-07-21 02:53:29 +0000589 assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) );
drhb86ccfb2003-01-28 23:13:10 +0000590 p->returnStack[p->returnDepth++] = pc+1;
drh8c74a8c2002-08-25 19:20:40 +0000591 pc = pOp->p2 - 1;
592 break;
593}
594
595/* Opcode: Return * * *
596**
597** Jump immediately to the next instruction after the last unreturned
598** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
599** processing aborts with a fatal error.
600*/
danielk19777a5147c2005-03-29 13:07:00 +0000601case OP_Return: { /* no-push */
drh17c40292004-07-21 02:53:29 +0000602 assert( p->returnDepth>0 );
drhb86ccfb2003-01-28 23:13:10 +0000603 p->returnDepth--;
604 pc = p->returnStack[p->returnDepth] - 1;
drh8c74a8c2002-08-25 19:20:40 +0000605 break;
606}
607
drh7f057c92005-06-24 03:53:06 +0000608/* Opcode: Halt P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +0000609**
drh0342b1f2005-09-01 03:07:44 +0000610** Exit immediately. All open cursors, Fifos, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000611** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000612**
drh92f02c32004-09-02 14:57:08 +0000613** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
614** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
615** For errors, it can be some other value. If P1!=0 then P2 will determine
616** whether or not to rollback the current transaction. Do not rollback
617** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
618** then back out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000619** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000620**
drh7f057c92005-06-24 03:53:06 +0000621** If P3 is not null then it is an error message string.
622**
drh9cfcf5d2002-01-29 18:41:24 +0000623** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000624** every program. So a jump past the last instruction of the program
625** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000626*/
danielk19777a5147c2005-03-29 13:07:00 +0000627case OP_Halt: { /* no-push */
drh6810ce62004-01-31 19:22:56 +0000628 p->pTos = pTos;
drh92f02c32004-09-02 14:57:08 +0000629 p->rc = pOp->p1;
630 p->pc = pc;
631 p->errorAction = pOp->p2;
632 if( pOp->p3 ){
drh17c40292004-07-21 02:53:29 +0000633 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
drh9cfcf5d2002-01-29 18:41:24 +0000634 }
drh92f02c32004-09-02 14:57:08 +0000635 rc = sqlite3VdbeHalt(p);
danielk197701427a62005-01-11 13:02:33 +0000636 assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
drh92f02c32004-09-02 14:57:08 +0000637 if( rc==SQLITE_BUSY ){
638 p->rc = SQLITE_BUSY;
639 return SQLITE_BUSY;
drh92f02c32004-09-02 14:57:08 +0000640 }
641 return p->rc ? SQLITE_ERROR : SQLITE_DONE;
drh5e00f6c2001-09-13 13:46:56 +0000642}
drhc61053b2000-06-04 12:58:36 +0000643
drh29dda4a2005-07-21 18:23:20 +0000644/* Opcode: Integer P1 * *
drh5e00f6c2001-09-13 13:46:56 +0000645**
drh29dda4a2005-07-21 18:23:20 +0000646** The 32-bit integer value P1 is pushed onto the stack.
drh5e00f6c2001-09-13 13:46:56 +0000647*/
drhf4479502004-05-27 03:12:53 +0000648case OP_Integer: {
drh6810ce62004-01-31 19:22:56 +0000649 pTos++;
drh29dda4a2005-07-21 18:23:20 +0000650 pTos->flags = MEM_Int;
651 pTos->i = pOp->p1;
652 break;
653}
654
655/* Opcode: Int64 * * P3
656**
657** P3 is a string representation of an integer. Convert that integer
658** to a 64-bit value and push it onto the stack.
659*/
660case OP_Int64: {
661 pTos++;
662 assert( pOp->p3!=0 );
663 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
664 pTos->z = pOp->p3;
665 pTos->n = strlen(pTos->z);
666 pTos->enc = SQLITE_UTF8;
667 pTos->i = sqlite3VdbeIntValue(pTos);
668 pTos->flags |= MEM_Int;
drhf4479502004-05-27 03:12:53 +0000669 break;
670}
drh4f26d6c2004-05-26 23:25:30 +0000671
drhf4479502004-05-27 03:12:53 +0000672/* Opcode: Real * * P3
673**
674** The string value P3 is converted to a real and pushed on to the stack.
675*/
danielk1977bc04f852005-03-29 08:26:13 +0000676case OP_Real: { /* same as TK_FLOAT, */
drhf4479502004-05-27 03:12:53 +0000677 pTos++;
678 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
679 pTos->z = pOp->p3;
680 pTos->n = strlen(pTos->z);
danielk1977dc8453f2004-06-12 00:42:34 +0000681 pTos->enc = SQLITE_UTF8;
drh6a6124e2004-06-27 01:56:33 +0000682 pTos->r = sqlite3VdbeRealValue(pTos);
683 pTos->flags |= MEM_Real;
drh8079a0d2006-01-12 17:20:50 +0000684 sqlite3VdbeChangeEncoding(pTos, encoding);
drhf4479502004-05-27 03:12:53 +0000685 break;
686}
danielk1977cbb18d22004-05-28 11:37:27 +0000687
danielk1977cbb18d22004-05-28 11:37:27 +0000688/* Opcode: String8 * * P3
689**
danielk1977819d7f42006-01-15 14:11:48 +0000690** P3 points to a nul terminated UTF-8 string. This opcode is transformed
danielk19770f69c1e2004-05-29 11:24:50 +0000691** into an OP_String before it is executed for the first time.
danielk1977cbb18d22004-05-28 11:37:27 +0000692*/
drhf2bc0132004-10-04 13:19:23 +0000693case OP_String8: { /* same as TK_STRING */
drhf0863fe2005-06-12 21:35:51 +0000694 assert( pOp->p3!=0 );
drhed2df7f2005-11-16 04:34:32 +0000695 pOp->opcode = OP_String;
696 pOp->p1 = strlen(pOp->p3);
697
698#ifndef SQLITE_OMIT_UTF16
drh8079a0d2006-01-12 17:20:50 +0000699 if( encoding!=SQLITE_UTF8 ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000700 pTos++;
701 sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC);
drh8079a0d2006-01-12 17:20:50 +0000702 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem;
danielk1977bfd6cce2004-06-18 04:24:54 +0000703 if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
704 pTos->flags &= ~(MEM_Dyn);
705 pTos->flags |= MEM_Static;
danielk1977e0048402004-06-15 16:51:01 +0000706 if( pOp->p3type==P3_DYNAMIC ){
danielk1977bfd6cce2004-06-18 04:24:54 +0000707 sqliteFree(pOp->p3);
danielk1977e0048402004-06-15 16:51:01 +0000708 }
709 pOp->p3type = P3_DYNAMIC;
danielk1977bfd6cce2004-06-18 04:24:54 +0000710 pOp->p3 = pTos->z;
danielk1977819d7f42006-01-15 14:11:48 +0000711 pOp->p1 = pTos->n;
danielk1977bfd6cce2004-06-18 04:24:54 +0000712 break;
danielk19770f69c1e2004-05-29 11:24:50 +0000713 }
danielk197793758c82005-01-21 08:13:14 +0000714#endif
danielk1977bfd6cce2004-06-18 04:24:54 +0000715 /* Otherwise fall through to the next case, OP_String */
danielk1977cbb18d22004-05-28 11:37:27 +0000716}
drhf4479502004-05-27 03:12:53 +0000717
drhed2df7f2005-11-16 04:34:32 +0000718/* Opcode: String P1 * P3
drhf4479502004-05-27 03:12:53 +0000719**
danielk1977819d7f42006-01-15 14:11:48 +0000720** The string value P3 of length P1 (bytes) is pushed onto the stack.
drhf4479502004-05-27 03:12:53 +0000721*/
722case OP_String: {
723 pTos++;
drhf0863fe2005-06-12 21:35:51 +0000724 assert( pOp->p3!=0 );
725 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
726 pTos->z = pOp->p3;
drhed2df7f2005-11-16 04:34:32 +0000727 pTos->n = pOp->p1;
drh8079a0d2006-01-12 17:20:50 +0000728 pTos->enc = encoding;
danielk1977c572ef72004-05-27 09:28:41 +0000729 break;
730}
731
drhf0863fe2005-06-12 21:35:51 +0000732/* Opcode: Null * * *
733**
734** Push a NULL onto the stack.
735*/
736case OP_Null: {
737 pTos++;
738 pTos->flags = MEM_Null;
drhd1c301e2005-09-07 23:05:21 +0000739 pTos->n = 0;
drhf0863fe2005-06-12 21:35:51 +0000740 break;
741}
742
743
drha71aa002004-11-03 13:59:04 +0000744#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +0000745/* Opcode: HexBlob * * P3
746**
danielk197772c952a2004-06-21 09:06:41 +0000747** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the
748** vdbe stack.
danielk19770f69c1e2004-05-29 11:24:50 +0000749**
danielk197772c952a2004-06-21 09:06:41 +0000750** The first time this instruction executes, in transforms itself into a
751** 'Blob' opcode with a binary blob as P3.
danielk1977c572ef72004-05-27 09:28:41 +0000752*/
drhf2bc0132004-10-04 13:19:23 +0000753case OP_HexBlob: { /* same as TK_BLOB */
danielk19770f69c1e2004-05-29 11:24:50 +0000754 pOp->opcode = OP_Blob;
755 pOp->p1 = strlen(pOp->p3)/2;
756 if( pOp->p1 ){
757 char *zBlob = sqlite3HexToBlob(pOp->p3);
758 if( !zBlob ) goto no_mem;
759 if( pOp->p3type==P3_DYNAMIC ){
760 sqliteFree(pOp->p3);
761 }
762 pOp->p3 = zBlob;
763 pOp->p3type = P3_DYNAMIC;
764 }else{
danielk1977e0048402004-06-15 16:51:01 +0000765 if( pOp->p3type==P3_DYNAMIC ){
766 sqliteFree(pOp->p3);
767 }
danielk19770f69c1e2004-05-29 11:24:50 +0000768 pOp->p3type = P3_STATIC;
769 pOp->p3 = "";
770 }
771
772 /* Fall through to the next case, OP_Blob. */
danielk1977c572ef72004-05-27 09:28:41 +0000773}
danielk1977c572ef72004-05-27 09:28:41 +0000774
775/* Opcode: Blob P1 * P3
776**
777** P3 points to a blob of data P1 bytes long. Push this
danielk1977cbb18d22004-05-28 11:37:27 +0000778** value onto the stack. This instruction is not coded directly
779** by the compiler. Instead, the compiler layer specifies
780** an OP_HexBlob opcode, with the hex string representation of
781** the blob as P3. This opcode is transformed to an OP_Blob
danielk197793758c82005-01-21 08:13:14 +0000782** the first time it is executed.
danielk1977c572ef72004-05-27 09:28:41 +0000783*/
784case OP_Blob: {
785 pTos++;
786 sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000787 break;
788}
danielk197793758c82005-01-21 08:13:14 +0000789#endif /* SQLITE_OMIT_BLOB_LITERAL */
danielk1977a37cdde2004-05-16 11:15:36 +0000790
drh50457892003-09-06 01:10:47 +0000791/* Opcode: Variable P1 * *
792**
793** Push the value of variable P1 onto the stack. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000794** an unknown in the original SQL string as handed to sqlite3_compile().
drh7c972de2003-09-06 22:18:07 +0000795** Any occurance of the '?' character in the original SQL is considered
796** a variable. Variables in the SQL string are number from left to
797** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000798** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000799*/
800case OP_Variable: {
drh7c972de2003-09-06 22:18:07 +0000801 int j = pOp->p1 - 1;
danielk1977295ba552004-05-19 10:34:51 +0000802 assert( j>=0 && j<p->nVar );
803
danielk1977295ba552004-05-19 10:34:51 +0000804 pTos++;
drhfebe1062004-08-28 18:17:48 +0000805 sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static);
danielk197793d46752004-05-23 13:30:58 +0000806 break;
807}
danielk1977295ba552004-05-19 10:34:51 +0000808
drh5e00f6c2001-09-13 13:46:56 +0000809/* Opcode: Pop P1 * *
810**
811** P1 elements are popped off of the top of stack and discarded.
812*/
danielk19777a5147c2005-03-29 13:07:00 +0000813case OP_Pop: { /* no-push */
drh6810ce62004-01-31 19:22:56 +0000814 assert( pOp->p1>=0 );
815 popStack(&pTos, pOp->p1);
816 assert( pTos>=&p->aStack[-1] );
drh5e00f6c2001-09-13 13:46:56 +0000817 break;
818}
drh75897232000-05-29 14:26:00 +0000819
drh9cfcf5d2002-01-29 18:41:24 +0000820/* Opcode: Dup P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000821**
822** A copy of the P1-th element of the stack
823** is made and pushed onto the top of the stack.
824** The top of the stack is element 0. So the
825** instruction "Dup 0 0 0" will make a copy of the
826** top of the stack.
drhb19a2bc2001-09-16 00:13:26 +0000827**
drh9cfcf5d2002-01-29 18:41:24 +0000828** If the content of the P1-th element is a dynamically
829** allocated string, then a new copy of that string
830** is made if P2==0. If P2!=0, then just a pointer
831** to the string is copied.
832**
drhb19a2bc2001-09-16 00:13:26 +0000833** Also see the Pull instruction.
drh5e00f6c2001-09-13 13:46:56 +0000834*/
835case OP_Dup: {
drh6810ce62004-01-31 19:22:56 +0000836 Mem *pFrom = &pTos[-pOp->p1];
837 assert( pFrom<=pTos && pFrom>=p->aStack );
838 pTos++;
drhfebe1062004-08-28 18:17:48 +0000839 sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem);
840 if( pOp->p2 ){
841 Deephemeralize(pTos);
drh5e00f6c2001-09-13 13:46:56 +0000842 }
843 break;
844}
drh75897232000-05-29 14:26:00 +0000845
drh5e00f6c2001-09-13 13:46:56 +0000846/* Opcode: Pull P1 * *
847**
848** The P1-th element is removed from its current location on
849** the stack and pushed back on top of the stack. The
850** top of the stack is element 0, so "Pull 0 0 0" is
drhb19a2bc2001-09-16 00:13:26 +0000851** a no-op. "Pull 1 0 0" swaps the top two elements of
852** the stack.
853**
854** See also the Dup instruction.
drh5e00f6c2001-09-13 13:46:56 +0000855*/
danielk19777a5147c2005-03-29 13:07:00 +0000856case OP_Pull: { /* no-push */
drh6810ce62004-01-31 19:22:56 +0000857 Mem *pFrom = &pTos[-pOp->p1];
drh5e00f6c2001-09-13 13:46:56 +0000858 int i;
drh00706be2004-01-30 14:49:16 +0000859 Mem ts;
drh6810ce62004-01-31 19:22:56 +0000860
drh6810ce62004-01-31 19:22:56 +0000861 ts = *pFrom;
862 Deephemeralize(pTos);
863 for(i=0; i<pOp->p1; i++, pFrom++){
864 Deephemeralize(&pFrom[1]);
drh6810ce62004-01-31 19:22:56 +0000865 assert( (pFrom->flags & MEM_Ephem)==0 );
danielk197718f41892004-05-22 07:27:46 +0000866 *pFrom = pFrom[1];
drh6810ce62004-01-31 19:22:56 +0000867 if( pFrom->flags & MEM_Short ){
danielk1977106bb232004-05-21 10:08:53 +0000868 assert( pFrom->flags & (MEM_Str|MEM_Blob) );
drh6810ce62004-01-31 19:22:56 +0000869 assert( pFrom->z==pFrom[1].zShort );
drh6810ce62004-01-31 19:22:56 +0000870 pFrom->z = pFrom->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000871 }
drh5e00f6c2001-09-13 13:46:56 +0000872 }
drh6810ce62004-01-31 19:22:56 +0000873 *pTos = ts;
drh6810ce62004-01-31 19:22:56 +0000874 if( pTos->flags & MEM_Short ){
danielk1977106bb232004-05-21 10:08:53 +0000875 assert( pTos->flags & (MEM_Str|MEM_Blob) );
drh6810ce62004-01-31 19:22:56 +0000876 assert( pTos->z==pTos[-pOp->p1].zShort );
drh6810ce62004-01-31 19:22:56 +0000877 pTos->z = pTos->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000878 }
drh5e00f6c2001-09-13 13:46:56 +0000879 break;
880}
drh75897232000-05-29 14:26:00 +0000881
drh9cfcf5d2002-01-29 18:41:24 +0000882/* Opcode: Push P1 * *
883**
884** Overwrite the value of the P1-th element down on the
885** stack (P1==0 is the top of the stack) with the value
drhac82fcf2002-09-08 17:23:41 +0000886** of the top of the stack. Then pop the top of the stack.
drh9cfcf5d2002-01-29 18:41:24 +0000887*/
danielk19777a5147c2005-03-29 13:07:00 +0000888case OP_Push: { /* no-push */
drh6810ce62004-01-31 19:22:56 +0000889 Mem *pTo = &pTos[-pOp->p1];
drh0ca3e242002-01-29 23:07:02 +0000890
drh6810ce62004-01-31 19:22:56 +0000891 assert( pTo>=p->aStack );
drhfebe1062004-08-28 18:17:48 +0000892 sqlite3VdbeMemMove(pTo, pTos);
drh6810ce62004-01-31 19:22:56 +0000893 pTos--;
drh9cfcf5d2002-01-29 18:41:24 +0000894 break;
895}
896
drhdf199a22002-06-14 22:38:41 +0000897/* Opcode: Callback P1 * *
drh5e00f6c2001-09-13 13:46:56 +0000898**
drh403110c2006-01-07 18:10:32 +0000899** The top P1 values on the stack represent a single result row from
900** a query. This opcode causes the sqlite3_step() call to terminate
901** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
902** structure to provide access to the top P1 values as the result
903** row. When the sqlite3_step() function is run again, the top P1
904** values will be automatically popped from the stack before the next
905** instruction executes.
drh5e00f6c2001-09-13 13:46:56 +0000906*/
danielk19777a5147c2005-03-29 13:07:00 +0000907case OP_Callback: { /* no-push */
drh403110c2006-01-07 18:10:32 +0000908 Mem *pMem;
909 Mem *pFirstColumn;
drhd6502752004-02-16 03:44:01 +0000910 assert( p->nResColumn==pOp->p1 );
danielk19778a6b5412004-05-24 07:04:25 +0000911
drh403110c2006-01-07 18:10:32 +0000912 /* Data in the pager might be moved or changed out from under us
913 ** in between the return from this sqlite3_step() call and the
914 ** next call to sqlite3_step(). So deephermeralize everything on
915 ** the stack. Note that ephemeral data is never stored in memory
916 ** cells so we do not have to worry about them.
917 */
918 pFirstColumn = &pTos[0-pOp->p1];
919 for(pMem = p->aStack; pMem<pFirstColumn; pMem++){
920 Deephemeralize(pMem);
danielk19778a6b5412004-05-24 07:04:25 +0000921 }
922
drh76873ab2006-01-07 18:48:26 +0000923 /* Invalidate all ephemeral cursor row caches */
924 p->cacheCtr = (p->cacheCtr + 2)|1;
925
drh403110c2006-01-07 18:10:32 +0000926 /* Make sure the results of the current row are \000 terminated
927 ** and have an assigned type. The results are deephemeralized as
928 ** as side effect.
929 */
930 for(; pMem<=pTos; pMem++ ){
931 sqlite3VdbeMemNulTerminate(pMem);
drh8079a0d2006-01-12 17:20:50 +0000932 storeTypeInfo(pMem, encoding);
drh403110c2006-01-07 18:10:32 +0000933 }
934
935 /* Set up the statement structure so that it will pop the current
936 ** results from the stack when the statement returns.
937 */
danielk19778a6b5412004-05-24 07:04:25 +0000938 p->resOnStack = 1;
939 p->nCallback++;
drh826fb5a2004-02-14 23:59:57 +0000940 p->popStack = pOp->p1;
941 p->pc = pc + 1;
942 p->pTos = pTos;
943 return SQLITE_ROW;
drh5e00f6c2001-09-13 13:46:56 +0000944}
drh75897232000-05-29 14:26:00 +0000945
drh4230e2c2004-06-29 13:54:50 +0000946/* Opcode: Concat P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000947**
drh855eb1c2004-08-31 13:45:11 +0000948** Look at the first P1+2 elements of the stack. Append them all
949** together with the lowest element first. The original P1+2 elements
drha9f9d1c2002-06-29 02:20:08 +0000950** are popped from the stack if P2==0 and retained if P2==1. If
951** any element of the stack is NULL, then the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +0000952**
drh4230e2c2004-06-29 13:54:50 +0000953** When P1==1, this routine makes a copy of the top stack element
954** into memory obtained from sqliteMalloc().
drh5e00f6c2001-09-13 13:46:56 +0000955*/
drhf2bc0132004-10-04 13:19:23 +0000956case OP_Concat: { /* same as TK_CONCAT */
drh5e00f6c2001-09-13 13:46:56 +0000957 char *zNew;
958 int nByte;
959 int nField;
960 int i, j;
drh6810ce62004-01-31 19:22:56 +0000961 Mem *pTerm;
danielk19778a6b5412004-05-24 07:04:25 +0000962
963 /* Loop through the stack elements to see how long the result will be. */
drh855eb1c2004-08-31 13:45:11 +0000964 nField = pOp->p1 + 2;
drh6810ce62004-01-31 19:22:56 +0000965 pTerm = &pTos[1-nField];
drh4230e2c2004-06-29 13:54:50 +0000966 nByte = 0;
drh6810ce62004-01-31 19:22:56 +0000967 for(i=0; i<nField; i++, pTerm++){
danielk19778a6b5412004-05-24 07:04:25 +0000968 assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
969 if( pTerm->flags&MEM_Null ){
drha9f9d1c2002-06-29 02:20:08 +0000970 nByte = -1;
971 break;
drh5e00f6c2001-09-13 13:46:56 +0000972 }
drh8079a0d2006-01-12 17:20:50 +0000973 Stringify(pTerm, encoding);
drheb2e1762004-05-27 01:53:56 +0000974 nByte += pTerm->n;
drh5e00f6c2001-09-13 13:46:56 +0000975 }
danielk19778a6b5412004-05-24 07:04:25 +0000976
drha9f9d1c2002-06-29 02:20:08 +0000977 if( nByte<0 ){
danielk19778a6b5412004-05-24 07:04:25 +0000978 /* If nByte is less than zero, then there is a NULL value on the stack.
979 ** In this case just pop the values off the stack (if required) and
980 ** push on a NULL.
981 */
drh6810ce62004-01-31 19:22:56 +0000982 if( pOp->p2==0 ){
983 popStack(&pTos, nField);
984 }
985 pTos++;
986 pTos->flags = MEM_Null;
danielk19778a6b5412004-05-24 07:04:25 +0000987 }else{
988 /* Otherwise malloc() space for the result and concatenate all the
989 ** stack values.
990 */
drheb2e1762004-05-27 01:53:56 +0000991 zNew = sqliteMallocRaw( nByte+2 );
danielk19778a6b5412004-05-24 07:04:25 +0000992 if( zNew==0 ) goto no_mem;
993 j = 0;
994 pTerm = &pTos[1-nField];
995 for(i=j=0; i<nField; i++, pTerm++){
drheb2e1762004-05-27 01:53:56 +0000996 int n = pTerm->n;
drhedef8fc2005-06-22 02:36:37 +0000997 assert( pTerm->flags & (MEM_Str|MEM_Blob) );
danielk19778a6b5412004-05-24 07:04:25 +0000998 memcpy(&zNew[j], pTerm->z, n);
999 j += n;
drh5e00f6c2001-09-13 13:46:56 +00001000 }
drheb2e1762004-05-27 01:53:56 +00001001 zNew[j] = 0;
1002 zNew[j+1] = 0;
danielk1977c572ef72004-05-27 09:28:41 +00001003 assert( j==nByte );
danielk19778a6b5412004-05-24 07:04:25 +00001004
1005 if( pOp->p2==0 ){
1006 popStack(&pTos, nField);
1007 }
1008 pTos++;
drheb2e1762004-05-27 01:53:56 +00001009 pTos->n = j;
drhf4479502004-05-27 03:12:53 +00001010 pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
danielk1977d8123362004-06-12 09:25:12 +00001011 pTos->xDel = 0;
drh8079a0d2006-01-12 17:20:50 +00001012 pTos->enc = encoding;
danielk19778a6b5412004-05-24 07:04:25 +00001013 pTos->z = zNew;
drh5e00f6c2001-09-13 13:46:56 +00001014 }
drh5e00f6c2001-09-13 13:46:56 +00001015 break;
1016}
drh75897232000-05-29 14:26:00 +00001017
drh5e00f6c2001-09-13 13:46:56 +00001018/* Opcode: Add * * *
1019**
1020** Pop the top two elements from the stack, add them together,
1021** and push the result back onto the stack. If either element
1022** is a string then it is converted to a double using the atof()
1023** function before the addition.
drhf5905aa2002-05-26 20:54:33 +00001024** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001025*/
1026/* Opcode: Multiply * * *
1027**
1028** Pop the top two elements from the stack, multiply them together,
1029** and push the result back onto the stack. If either element
1030** is a string then it is converted to a double using the atof()
1031** function before the multiplication.
drhf5905aa2002-05-26 20:54:33 +00001032** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001033*/
1034/* Opcode: Subtract * * *
1035**
1036** Pop the top two elements from the stack, subtract the
1037** first (what was on top of the stack) from the second (the
1038** next on stack)
1039** and push the result back onto the stack. If either element
1040** is a string then it is converted to a double using the atof()
1041** function before the subtraction.
drhf5905aa2002-05-26 20:54:33 +00001042** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001043*/
1044/* Opcode: Divide * * *
1045**
1046** Pop the top two elements from the stack, divide the
1047** first (what was on top of the stack) from the second (the
1048** next on stack)
1049** and push the result back onto the stack. If either element
1050** is a string then it is converted to a double using the atof()
1051** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001052** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001053*/
drhbf4133c2001-10-13 02:59:08 +00001054/* Opcode: Remainder * * *
1055**
1056** Pop the top two elements from the stack, divide the
1057** first (what was on top of the stack) from the second (the
1058** next on stack)
1059** and push the remainder after division onto the stack. If either element
1060** is a string then it is converted to a double using the atof()
1061** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001062** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001063*/
danielk19777a5147c2005-03-29 13:07:00 +00001064case OP_Add: /* same as TK_PLUS, no-push */
1065case OP_Subtract: /* same as TK_MINUS, no-push */
1066case OP_Multiply: /* same as TK_STAR, no-push */
1067case OP_Divide: /* same as TK_SLASH, no-push */
1068case OP_Remainder: { /* same as TK_REM, no-push */
drh6810ce62004-01-31 19:22:56 +00001069 Mem *pNos = &pTos[-1];
drh8a512562005-11-14 22:29:05 +00001070 int flags;
drh6810ce62004-01-31 19:22:56 +00001071 assert( pNos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00001072 flags = pTos->flags | pNos->flags;
1073 if( (flags & MEM_Null)!=0 ){
drh6810ce62004-01-31 19:22:56 +00001074 Release(pTos);
1075 pTos--;
1076 Release(pTos);
1077 pTos->flags = MEM_Null;
1078 }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001079 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001080 a = pTos->i;
1081 b = pNos->i;
drh5e00f6c2001-09-13 13:46:56 +00001082 switch( pOp->opcode ){
1083 case OP_Add: b += a; break;
1084 case OP_Subtract: b -= a; break;
1085 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001086 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001087 if( a==0 ) goto divide_by_zero;
1088 b /= a;
drh75897232000-05-29 14:26:00 +00001089 break;
1090 }
drhbf4133c2001-10-13 02:59:08 +00001091 default: {
1092 if( a==0 ) goto divide_by_zero;
1093 b %= a;
1094 break;
1095 }
drh75897232000-05-29 14:26:00 +00001096 }
drh6810ce62004-01-31 19:22:56 +00001097 Release(pTos);
1098 pTos--;
1099 Release(pTos);
1100 pTos->i = b;
1101 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001102 }else{
1103 double a, b;
drh6a6124e2004-06-27 01:56:33 +00001104 a = sqlite3VdbeRealValue(pTos);
1105 b = sqlite3VdbeRealValue(pNos);
drh5e00f6c2001-09-13 13:46:56 +00001106 switch( pOp->opcode ){
1107 case OP_Add: b += a; break;
1108 case OP_Subtract: b -= a; break;
1109 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001110 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001111 if( a==0.0 ) goto divide_by_zero;
1112 b /= a;
1113 break;
1114 }
drhbf4133c2001-10-13 02:59:08 +00001115 default: {
drh1ab43002002-01-14 09:28:19 +00001116 int ia = (int)a;
1117 int ib = (int)b;
drhbf4133c2001-10-13 02:59:08 +00001118 if( ia==0.0 ) goto divide_by_zero;
1119 b = ib % ia;
1120 break;
1121 }
drh5e00f6c2001-09-13 13:46:56 +00001122 }
drh6810ce62004-01-31 19:22:56 +00001123 Release(pTos);
1124 pTos--;
1125 Release(pTos);
1126 pTos->r = b;
1127 pTos->flags = MEM_Real;
drh8a512562005-11-14 22:29:05 +00001128 if( (flags & MEM_Real)==0 ){
1129 sqlite3VdbeIntegerAffinity(pTos);
1130 }
drh5e00f6c2001-09-13 13:46:56 +00001131 }
1132 break;
1133
1134divide_by_zero:
drh6810ce62004-01-31 19:22:56 +00001135 Release(pTos);
1136 pTos--;
1137 Release(pTos);
1138 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00001139 break;
1140}
1141
danielk1977dc1bdc42004-06-11 10:51:27 +00001142/* Opcode: CollSeq * * P3
1143**
1144** P3 is a pointer to a CollSeq struct. If the next call to a user function
1145** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1146** be returned. This is used by the built-in min(), max() and nullif()
drhe6f85e72004-12-25 01:03:13 +00001147** functions.
danielk1977dc1bdc42004-06-11 10:51:27 +00001148**
1149** The interface used by the implementation of the aforementioned functions
1150** to retrieve the collation sequence set by this opcode is not available
1151** publicly, only to user functions defined in func.c.
1152*/
danielk19777a5147c2005-03-29 13:07:00 +00001153case OP_CollSeq: { /* no-push */
danielk1977dc1bdc42004-06-11 10:51:27 +00001154 assert( pOp->p3type==P3_COLLSEQ );
1155 break;
1156}
1157
danielk1977682f68b2004-06-05 10:22:17 +00001158/* Opcode: Function P1 P2 P3
drh8e0a2f92002-02-23 23:45:45 +00001159**
drh0bce8352002-02-28 00:41:10 +00001160** Invoke a user function (P3 is a pointer to a Function structure that
drh13449892005-09-07 21:22:45 +00001161** defines the function) with P2 arguments taken from the stack. Pop all
danielk1977682f68b2004-06-05 10:22:17 +00001162** arguments from the stack and push back the result.
1163**
drh13449892005-09-07 21:22:45 +00001164** P1 is a 32-bit bitmask indicating whether or not each argument to the
danielk1977682f68b2004-06-05 10:22:17 +00001165** function was determined to be constant at compile time. If the first
drh13449892005-09-07 21:22:45 +00001166** argument was constant then bit 0 of P1 is set. This is used to determine
danielk1977682f68b2004-06-05 10:22:17 +00001167** whether meta data associated with a user function argument using the
1168** sqlite3_set_auxdata() API may be safely retained until the next
1169** invocation of this opcode.
drh1350b032002-02-27 19:00:20 +00001170**
drh13449892005-09-07 21:22:45 +00001171** See also: AggStep and AggFinal
drh8e0a2f92002-02-23 23:45:45 +00001172*/
drh0bce8352002-02-28 00:41:10 +00001173case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001174 int i;
drh6810ce62004-01-31 19:22:56 +00001175 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001176 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001177 sqlite3_value **apVal;
drh13449892005-09-07 21:22:45 +00001178 int n = pOp->p2;
drh1350b032002-02-27 19:00:20 +00001179
danielk19776ddcca52004-05-24 23:48:25 +00001180 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001181 assert( apVal || n==0 );
1182
drh6810ce62004-01-31 19:22:56 +00001183 pArg = &pTos[1-n];
drh6810ce62004-01-31 19:22:56 +00001184 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001185 apVal[i] = pArg;
drh8079a0d2006-01-12 17:20:50 +00001186 storeTypeInfo(pArg, encoding);
drh8e0a2f92002-02-23 23:45:45 +00001187 }
danielk197751ad0ec2004-05-24 12:39:02 +00001188
danielk1977682f68b2004-06-05 10:22:17 +00001189 assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
1190 if( pOp->p3type==P3_FUNCDEF ){
1191 ctx.pFunc = (FuncDef*)pOp->p3;
1192 ctx.pVdbeFunc = 0;
1193 }else{
1194 ctx.pVdbeFunc = (VdbeFunc*)pOp->p3;
1195 ctx.pFunc = ctx.pVdbeFunc->pFunc;
1196 }
1197
drh00706be2004-01-30 14:49:16 +00001198 ctx.s.flags = MEM_Null;
1199 ctx.s.z = 0;
drh22276bd2004-06-22 22:54:22 +00001200 ctx.s.xDel = 0;
drh8e0a2f92002-02-23 23:45:45 +00001201 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00001202 if( ctx.pFunc->needCollSeq ){
1203 assert( pOp>p->aOp );
1204 assert( pOp[-1].p3type==P3_COLLSEQ );
1205 assert( pOp[-1].opcode==OP_CollSeq );
1206 ctx.pColl = (CollSeq *)pOp[-1].p3;
1207 }
danielk19774adee202004-05-08 08:23:19 +00001208 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001209 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk19774adee202004-05-08 08:23:19 +00001210 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
danielk19779e128002006-01-18 16:51:35 +00001211 if( sqlite3MallocFailed() ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +00001212 popStack(&pTos, n);
danielk19777e18c252004-05-25 11:47:24 +00001213
danielk1977682f68b2004-06-05 10:22:17 +00001214 /* If any auxilary data functions have been called by this user function,
1215 ** immediately call the destructor for any non-static values.
1216 */
1217 if( ctx.pVdbeFunc ){
drh13449892005-09-07 21:22:45 +00001218 sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
danielk1977682f68b2004-06-05 10:22:17 +00001219 pOp->p3 = (char *)ctx.pVdbeFunc;
1220 pOp->p3type = P3_VDBEFUNC;
1221 }
1222
drh90669c12006-01-20 15:45:36 +00001223 /* If the function returned an error, throw an exception */
1224 if( ctx.isError ){
1225 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
1226 rc = SQLITE_ERROR;
1227 }
1228
danielk19777e18c252004-05-25 11:47:24 +00001229 /* Copy the result of the function to the top of the stack */
drh8079a0d2006-01-12 17:20:50 +00001230 sqlite3VdbeChangeEncoding(&ctx.s, encoding);
drhfebe1062004-08-28 18:17:48 +00001231 pTos++;
1232 pTos->flags = 0;
1233 sqlite3VdbeMemMove(pTos, &ctx.s);
drh8e0a2f92002-02-23 23:45:45 +00001234 break;
1235}
1236
drhbf4133c2001-10-13 02:59:08 +00001237/* Opcode: BitAnd * * *
1238**
1239** Pop the top two elements from the stack. Convert both elements
1240** to integers. Push back onto the stack the bit-wise AND of the
1241** two elements.
drhf5905aa2002-05-26 20:54:33 +00001242** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001243*/
1244/* Opcode: BitOr * * *
1245**
1246** Pop the top two elements from the stack. Convert both elements
1247** to integers. Push back onto the stack the bit-wise OR of the
1248** two elements.
drhf5905aa2002-05-26 20:54:33 +00001249** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001250*/
1251/* Opcode: ShiftLeft * * *
1252**
1253** Pop the top two elements from the stack. Convert both elements
drh17c40292004-07-21 02:53:29 +00001254** to integers. Push back onto the stack the second element shifted
1255** left by N bits where N is the top element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001256** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001257*/
1258/* Opcode: ShiftRight * * *
1259**
1260** Pop the top two elements from the stack. Convert both elements
drh17c40292004-07-21 02:53:29 +00001261** to integers. Push back onto the stack the second element shifted
1262** right by N bits where N is the top element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001263** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001264*/
danielk19777a5147c2005-03-29 13:07:00 +00001265case OP_BitAnd: /* same as TK_BITAND, no-push */
1266case OP_BitOr: /* same as TK_BITOR, no-push */
1267case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */
1268case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */
drh6810ce62004-01-31 19:22:56 +00001269 Mem *pNos = &pTos[-1];
drhb1276122005-10-29 15:48:30 +00001270 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001271
1272 assert( pNos>=p->aStack );
1273 if( (pTos->flags | pNos->flags) & MEM_Null ){
1274 popStack(&pTos, 2);
1275 pTos++;
1276 pTos->flags = MEM_Null;
drhf5905aa2002-05-26 20:54:33 +00001277 break;
1278 }
drh17c40292004-07-21 02:53:29 +00001279 a = sqlite3VdbeIntValue(pNos);
1280 b = sqlite3VdbeIntValue(pTos);
drhbf4133c2001-10-13 02:59:08 +00001281 switch( pOp->opcode ){
1282 case OP_BitAnd: a &= b; break;
1283 case OP_BitOr: a |= b; break;
1284 case OP_ShiftLeft: a <<= b; break;
1285 case OP_ShiftRight: a >>= b; break;
1286 default: /* CANT HAPPEN */ break;
1287 }
danielk19778a6b5412004-05-24 07:04:25 +00001288 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001289 pTos--;
drh79f14b72004-03-03 01:51:24 +00001290 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001291 pTos->i = a;
drh79f14b72004-03-03 01:51:24 +00001292 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001293 break;
1294}
1295
drh5e00f6c2001-09-13 13:46:56 +00001296/* Opcode: AddImm P1 * *
1297**
drh4a324312001-12-21 14:30:42 +00001298** Add the value P1 to whatever is on top of the stack. The result
1299** is always an integer.
1300**
1301** To force the top of the stack to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001302*/
danielk19777a5147c2005-03-29 13:07:00 +00001303case OP_AddImm: { /* no-push */
drh6810ce62004-01-31 19:22:56 +00001304 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00001305 sqlite3VdbeMemIntegerify(pTos);
drh6810ce62004-01-31 19:22:56 +00001306 pTos->i += pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00001307 break;
1308}
1309
drh751f4122004-01-14 21:59:22 +00001310/* Opcode: ForceInt P1 P2 *
drh1dd59e02003-07-06 17:22:25 +00001311**
drh751f4122004-01-14 21:59:22 +00001312** Convert the top of the stack into an integer. If the current top of
1313** the stack is not numeric (meaning that is is a NULL or a string that
1314** does not look like an integer or floating point number) then pop the
1315** stack and jump to P2. If the top of the stack is numeric then
1316** convert it into the least integer that is greater than or equal to its
1317** current value if P1==0, or to the least integer that is strictly
1318** greater than its current value if P1==1.
drh1dd59e02003-07-06 17:22:25 +00001319*/
danielk19777a5147c2005-03-29 13:07:00 +00001320case OP_ForceInt: { /* no-push */
drhf4f8fd52005-03-31 18:40:04 +00001321 i64 v;
drh6810ce62004-01-31 19:22:56 +00001322 assert( pTos>=p->aStack );
drh8079a0d2006-01-12 17:20:50 +00001323 applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
drh17c40292004-07-21 02:53:29 +00001324 if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){
drh6810ce62004-01-31 19:22:56 +00001325 Release(pTos);
1326 pTos--;
drh1dd59e02003-07-06 17:22:25 +00001327 pc = pOp->p2 - 1;
drh751f4122004-01-14 21:59:22 +00001328 break;
drh1dd59e02003-07-06 17:22:25 +00001329 }
drh6810ce62004-01-31 19:22:56 +00001330 if( pTos->flags & MEM_Int ){
1331 v = pTos->i + (pOp->p1!=0);
drh751f4122004-01-14 21:59:22 +00001332 }else{
drh8a512562005-11-14 22:29:05 +00001333 /* FIX ME: should this not be assert( pTos->flags & MEM_Real ) ??? */
1334 sqlite3VdbeMemRealify(pTos);
drh6810ce62004-01-31 19:22:56 +00001335 v = (int)pTos->r;
1336 if( pTos->r>(double)v ) v++;
1337 if( pOp->p1 && pTos->r==(double)v ) v++;
drh751f4122004-01-14 21:59:22 +00001338 }
drh6810ce62004-01-31 19:22:56 +00001339 Release(pTos);
1340 pTos->i = v;
1341 pTos->flags = MEM_Int;
drh1dd59e02003-07-06 17:22:25 +00001342 break;
1343}
1344
drhf1351b62002-07-31 19:50:26 +00001345/* Opcode: MustBeInt P1 P2 *
drh8aff1012001-12-22 14:49:24 +00001346**
1347** Force the top of the stack to be an integer. If the top of the
drhd99f7062002-06-08 23:25:08 +00001348** stack is not an integer and cannot be converted into an integer
drh8aff1012001-12-22 14:49:24 +00001349** with out data loss, then jump immediately to P2, or if P2==0
1350** raise an SQLITE_MISMATCH exception.
drhf1351b62002-07-31 19:50:26 +00001351**
1352** If the top of the stack is not an integer and P2 is not zero and
1353** P1 is 1, then the stack is popped. In all other cases, the depth
1354** of the stack is unchanged.
drh8aff1012001-12-22 14:49:24 +00001355*/
danielk19777a5147c2005-03-29 13:07:00 +00001356case OP_MustBeInt: { /* no-push */
drh6810ce62004-01-31 19:22:56 +00001357 assert( pTos>=p->aStack );
drh8079a0d2006-01-12 17:20:50 +00001358 applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
drh17c40292004-07-21 02:53:29 +00001359 if( (pTos->flags & MEM_Int)==0 ){
1360 if( pOp->p2==0 ){
1361 rc = SQLITE_MISMATCH;
1362 goto abort_due_to_error;
1363 }else{
1364 if( pOp->p1 ) popStack(&pTos, 1);
1365 pc = pOp->p2 - 1;
drh8aff1012001-12-22 14:49:24 +00001366 }
drh8aff1012001-12-22 14:49:24 +00001367 }else{
drh17c40292004-07-21 02:53:29 +00001368 Release(pTos);
1369 pTos->flags = MEM_Int;
drh8aff1012001-12-22 14:49:24 +00001370 }
1371 break;
1372}
1373
drh8a512562005-11-14 22:29:05 +00001374/* Opcode: RealAffinity * * *
drh487e2622005-06-25 18:42:14 +00001375**
drh8a512562005-11-14 22:29:05 +00001376** If the top of the stack is an integer, convert it to a real value.
drh487e2622005-06-25 18:42:14 +00001377**
drh8a512562005-11-14 22:29:05 +00001378** This opcode is used when extracting information from a column that
1379** has REAL affinity. Such column values may still be stored as
1380** integers, for space efficiency, but after extraction we want them
1381** to have only a real value.
drh487e2622005-06-25 18:42:14 +00001382*/
drh8a512562005-11-14 22:29:05 +00001383case OP_RealAffinity: { /* no-push */
drh487e2622005-06-25 18:42:14 +00001384 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00001385 if( pTos->flags & MEM_Int ){
1386 sqlite3VdbeMemRealify(pTos);
1387 }
drh487e2622005-06-25 18:42:14 +00001388 break;
1389}
1390
drh8df447f2005-11-01 15:48:24 +00001391#ifndef SQLITE_OMIT_CAST
drh487e2622005-06-25 18:42:14 +00001392/* Opcode: ToText * * *
1393**
1394** Force the value on the top of the stack to be text.
drh31beae92005-11-24 14:34:36 +00001395** If the value is numeric, convert it to a string using the
drh487e2622005-06-25 18:42:14 +00001396** equivalent of printf(). Blob values are unchanged and
1397** are afterwards simply interpreted as text.
1398**
1399** A NULL value is not changed by this routine. It remains NULL.
1400*/
drh8a512562005-11-14 22:29:05 +00001401case OP_ToText: { /* same as TK_TO_TEXT, no-push */
drh487e2622005-06-25 18:42:14 +00001402 assert( pTos>=p->aStack );
1403 if( pTos->flags & MEM_Null ) break;
1404 assert( MEM_Str==(MEM_Blob>>3) );
1405 pTos->flags |= (pTos->flags&MEM_Blob)>>3;
drh8079a0d2006-01-12 17:20:50 +00001406 applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
drh487e2622005-06-25 18:42:14 +00001407 assert( pTos->flags & MEM_Str );
1408 pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
1409 break;
1410}
1411
1412/* Opcode: ToBlob * * *
1413**
1414** Force the value on the top of the stack to be a BLOB.
1415** If the value is numeric, convert it to a string first.
1416** Strings are simply reinterpreted as blobs with no change
1417** to the underlying data.
1418**
1419** A NULL value is not changed by this routine. It remains NULL.
1420*/
drh8a512562005-11-14 22:29:05 +00001421case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */
drh487e2622005-06-25 18:42:14 +00001422 assert( pTos>=p->aStack );
1423 if( pTos->flags & MEM_Null ) break;
1424 if( (pTos->flags & MEM_Blob)==0 ){
drh8079a0d2006-01-12 17:20:50 +00001425 applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
drh487e2622005-06-25 18:42:14 +00001426 assert( pTos->flags & MEM_Str );
1427 pTos->flags |= MEM_Blob;
1428 }
1429 pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str);
1430 break;
1431}
drh8a512562005-11-14 22:29:05 +00001432
1433/* Opcode: ToNumeric * * *
1434**
1435** Force the value on the top of the stack to be numeric (either an
1436** integer or a floating-point number.)
1437** If the value is text or blob, try to convert it to an using the
1438** equivalent of atoi() or atof() and store 0 if no such conversion
1439** is possible.
1440**
1441** A NULL value is not changed by this routine. It remains NULL.
1442*/
1443case OP_ToNumeric: { /* same as TK_TO_NUMERIC, no-push */
1444 assert( pTos>=p->aStack );
1445 if( (pTos->flags & MEM_Null)==0 ){
1446 sqlite3VdbeMemNumerify(pTos);
1447 }
1448 break;
1449}
1450#endif /* SQLITE_OMIT_CAST */
1451
1452/* Opcode: ToInt * * *
1453**
1454** Force the value on the top of the stack to be an integer. If
1455** The value is currently a real number, drop its fractional part.
1456** If the value is text or blob, try to convert it to an integer using the
1457** equivalent of atoi() and store 0 if no such conversion is possible.
1458**
1459** A NULL value is not changed by this routine. It remains NULL.
1460*/
1461case OP_ToInt: { /* same as TK_TO_INT, no-push */
1462 assert( pTos>=p->aStack );
1463 if( (pTos->flags & MEM_Null)==0 ){
1464 sqlite3VdbeMemIntegerify(pTos);
1465 }
1466 break;
1467}
1468
1469#ifndef SQLITE_OMIT_CAST
1470/* Opcode: ToReal * * *
1471**
1472** Force the value on the top of the stack to be a floating point number.
1473** If The value is currently an integer, convert it.
1474** If the value is text or blob, try to convert it to an integer using the
1475** equivalent of atoi() and store 0 if no such conversion is possible.
1476**
1477** A NULL value is not changed by this routine. It remains NULL.
1478*/
1479case OP_ToReal: { /* same as TK_TO_REAL, no-push */
1480 assert( pTos>=p->aStack );
1481 if( (pTos->flags & MEM_Null)==0 ){
1482 sqlite3VdbeMemRealify(pTos);
1483 }
1484 break;
1485}
drh487e2622005-06-25 18:42:14 +00001486#endif /* SQLITE_OMIT_CAST */
1487
drh53db1452004-05-20 13:54:53 +00001488/* Opcode: Eq P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001489**
1490** Pop the top two elements from the stack. If they are equal, then
1491** jump to instruction P2. Otherwise, continue to the next instruction.
drhf5905aa2002-05-26 20:54:33 +00001492**
drhf0863fe2005-06-12 21:35:51 +00001493** If the 0x100 bit of P1 is true and either operand is NULL then take the
drhe3133822005-09-20 13:11:59 +00001494** jump. If the 0x100 bit of P1 is clear then fall thru if either operand
drh4f686232005-09-20 13:55:18 +00001495** is NULL.
1496**
1497** If the 0x200 bit of P1 is set and either operand is NULL then
1498** both operands are converted to integers prior to comparison.
1499** NULL operands are converted to zero and non-NULL operands are
1500** converted to 1. Thus, for example, with 0x200 set, NULL==NULL is true
1501** whereas it would normally be NULL. Similarly, NULL==123 is false when
1502** 0x200 is set but is NULL when the 0x200 bit of P1 is clear.
drhf5905aa2002-05-26 20:54:33 +00001503**
drhf0863fe2005-06-12 21:35:51 +00001504** The least significant byte of P1 (mask 0xff) must be an affinity character -
drh8a512562005-11-14 22:29:05 +00001505** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1506** to coerce both values
danielk1977e014a832004-05-17 10:48:57 +00001507** according to the affinity before the comparison is made. If the byte is
1508** 0x00, then numeric affinity is used.
danielk1977a37cdde2004-05-16 11:15:36 +00001509**
1510** Once any conversions have taken place, and neither value is NULL,
1511** the values are compared. If both values are blobs, or both are text,
1512** then memcmp() is used to determine the results of the comparison. If
1513** both values are numeric, then a numeric comparison is used. If the
1514** two values are of different types, then they are inequal.
drhc9b84a12002-06-20 11:36:48 +00001515**
drhf5905aa2002-05-26 20:54:33 +00001516** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1517** stack if the jump would have been taken, or a 0 if not. Push a
1518** NULL if either operand was NULL.
danielk1977a37cdde2004-05-16 11:15:36 +00001519**
drh53db1452004-05-20 13:54:53 +00001520** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
1521** structure) that defines how to compare text.
drh5e00f6c2001-09-13 13:46:56 +00001522*/
drh53db1452004-05-20 13:54:53 +00001523/* Opcode: Ne P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001524**
drh53db1452004-05-20 13:54:53 +00001525** This works just like the Eq opcode except that the jump is taken if
1526** the operands from the stack are not equal. See the Eq opcode for
1527** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001528*/
drh53db1452004-05-20 13:54:53 +00001529/* Opcode: Lt P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001530**
drh53db1452004-05-20 13:54:53 +00001531** This works just like the Eq opcode except that the jump is taken if
danielk19770202b292004-06-09 09:55:16 +00001532** the 2nd element down on the stack is less than the top of the stack.
drh53db1452004-05-20 13:54:53 +00001533** See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001534*/
drh53db1452004-05-20 13:54:53 +00001535/* Opcode: Le P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001536**
drh53db1452004-05-20 13:54:53 +00001537** This works just like the Eq opcode except that the jump is taken if
danielk19770202b292004-06-09 09:55:16 +00001538** the 2nd element down on the stack is less than or equal to the
drh53db1452004-05-20 13:54:53 +00001539** top of the stack. See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001540*/
drh53db1452004-05-20 13:54:53 +00001541/* Opcode: Gt P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001542**
drh53db1452004-05-20 13:54:53 +00001543** This works just like the Eq opcode except that the jump is taken if
danielk19770202b292004-06-09 09:55:16 +00001544** the 2nd element down on the stack is greater than the top of the stack.
drh53db1452004-05-20 13:54:53 +00001545** See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001546*/
drh53db1452004-05-20 13:54:53 +00001547/* Opcode: Ge P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001548**
drh53db1452004-05-20 13:54:53 +00001549** This works just like the Eq opcode except that the jump is taken if
danielk19770202b292004-06-09 09:55:16 +00001550** the 2nd element down on the stack is greater than or equal to the
drh53db1452004-05-20 13:54:53 +00001551** top of the stack. See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001552*/
danielk19777a5147c2005-03-29 13:07:00 +00001553case OP_Eq: /* same as TK_EQ, no-push */
1554case OP_Ne: /* same as TK_NE, no-push */
1555case OP_Lt: /* same as TK_LT, no-push */
1556case OP_Le: /* same as TK_LE, no-push */
1557case OP_Gt: /* same as TK_GT, no-push */
1558case OP_Ge: { /* same as TK_GE, no-push */
danielk1977a37cdde2004-05-16 11:15:36 +00001559 Mem *pNos;
1560 int flags;
1561 int res;
1562 char affinity;
1563
1564 pNos = &pTos[-1];
1565 flags = pTos->flags|pNos->flags;
1566
1567 /* If either value is a NULL P2 is not zero, take the jump if the least
1568 ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
1569 ** the stack.
1570 */
1571 if( flags&MEM_Null ){
drh4f686232005-09-20 13:55:18 +00001572 if( (pOp->p1 & 0x200)!=0 ){
1573 /* The 0x200 bit of P1 means, roughly "do not treat NULL as the
1574 ** magic SQL value it normally is - treat it as if it were another
1575 ** integer".
1576 **
1577 ** With 0x200 set, if either operand is NULL then both operands
1578 ** are converted to integers prior to being passed down into the
1579 ** normal comparison logic below. NULL operands are converted to
1580 ** zero and non-NULL operands are converted to 1. Thus, for example,
1581 ** with 0x200 set, NULL==NULL is true whereas it would normally
1582 ** be NULL. Similarly, NULL!=123 is true.
drhe3133822005-09-20 13:11:59 +00001583 */
drh4f686232005-09-20 13:55:18 +00001584 sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0);
1585 sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0);
danielk1977a37cdde2004-05-16 11:15:36 +00001586 }else{
drh4f686232005-09-20 13:55:18 +00001587 /* If the 0x200 bit of P1 is clear and either operand is NULL then
1588 ** the result is always NULL. The jump is taken if the 0x100 bit
drhe3133822005-09-20 13:11:59 +00001589 ** of P1 is set.
1590 */
1591 popStack(&pTos, 2);
1592 if( pOp->p2 ){
1593 if( pOp->p1 & 0x100 ){
1594 pc = pOp->p2-1;
1595 }
1596 }else{
1597 pTos++;
1598 pTos->flags = MEM_Null;
1599 }
1600 break;
danielk1977a37cdde2004-05-16 11:15:36 +00001601 }
danielk1977a37cdde2004-05-16 11:15:36 +00001602 }
1603
drhf0863fe2005-06-12 21:35:51 +00001604 affinity = pOp->p1 & 0xFF;
drhe51c44f2004-05-30 20:46:09 +00001605 if( affinity ){
drh8079a0d2006-01-12 17:20:50 +00001606 applyAffinity(pNos, affinity, encoding);
1607 applyAffinity(pTos, affinity, encoding);
drhe51c44f2004-05-30 20:46:09 +00001608 }
danielk1977a37cdde2004-05-16 11:15:36 +00001609
drh53db1452004-05-20 13:54:53 +00001610 assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
1611 res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
danielk1977a37cdde2004-05-16 11:15:36 +00001612 switch( pOp->opcode ){
1613 case OP_Eq: res = res==0; break;
1614 case OP_Ne: res = res!=0; break;
1615 case OP_Lt: res = res<0; break;
1616 case OP_Le: res = res<=0; break;
1617 case OP_Gt: res = res>0; break;
1618 default: res = res>=0; break;
1619 }
1620
1621 popStack(&pTos, 2);
1622 if( pOp->p2 ){
1623 if( res ){
1624 pc = pOp->p2-1;
1625 }
1626 }else{
1627 pTos++;
1628 pTos->flags = MEM_Int;
1629 pTos->i = res;
1630 }
1631 break;
1632}
drhc9b84a12002-06-20 11:36:48 +00001633
drh5e00f6c2001-09-13 13:46:56 +00001634/* Opcode: And * * *
1635**
1636** Pop two values off the stack. Take the logical AND of the
1637** two values and push the resulting boolean value back onto the
1638** stack.
1639*/
1640/* Opcode: Or * * *
1641**
1642** Pop two values off the stack. Take the logical OR of the
1643** two values and push the resulting boolean value back onto the
1644** stack.
1645*/
danielk19777a5147c2005-03-29 13:07:00 +00001646case OP_And: /* same as TK_AND, no-push */
1647case OP_Or: { /* same as TK_OR, no-push */
drh6810ce62004-01-31 19:22:56 +00001648 Mem *pNos = &pTos[-1];
drhbb113512002-05-27 01:04:51 +00001649 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1650
drh6810ce62004-01-31 19:22:56 +00001651 assert( pNos>=p->aStack );
1652 if( pTos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001653 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001654 }else{
drh8a512562005-11-14 22:29:05 +00001655 sqlite3VdbeMemIntegerify(pTos);
drh6810ce62004-01-31 19:22:56 +00001656 v1 = pTos->i==0;
drhbb113512002-05-27 01:04:51 +00001657 }
drh6810ce62004-01-31 19:22:56 +00001658 if( pNos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001659 v2 = 2;
1660 }else{
drh8a512562005-11-14 22:29:05 +00001661 sqlite3VdbeMemIntegerify(pNos);
drh6810ce62004-01-31 19:22:56 +00001662 v2 = pNos->i==0;
drhbb113512002-05-27 01:04:51 +00001663 }
1664 if( pOp->opcode==OP_And ){
1665 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1666 v1 = and_logic[v1*3+v2];
1667 }else{
1668 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1669 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001670 }
drh6810ce62004-01-31 19:22:56 +00001671 popStack(&pTos, 2);
1672 pTos++;
drhbb113512002-05-27 01:04:51 +00001673 if( v1==2 ){
drh6810ce62004-01-31 19:22:56 +00001674 pTos->flags = MEM_Null;
drhbb113512002-05-27 01:04:51 +00001675 }else{
drh6810ce62004-01-31 19:22:56 +00001676 pTos->i = v1==0;
1677 pTos->flags = MEM_Int;
drhbb113512002-05-27 01:04:51 +00001678 }
drh5e00f6c2001-09-13 13:46:56 +00001679 break;
1680}
1681
1682/* Opcode: Negative * * *
1683**
1684** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001685** with its additive inverse. If the top of the stack is NULL
1686** its value is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001687*/
drhbf4133c2001-10-13 02:59:08 +00001688/* Opcode: AbsValue * * *
1689**
1690** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001691** with its absolute value. If the top of the stack is NULL
1692** its value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001693*/
danielk19777a5147c2005-03-29 13:07:00 +00001694case OP_Negative: /* same as TK_UMINUS, no-push */
drhbf4133c2001-10-13 02:59:08 +00001695case OP_AbsValue: {
drh6810ce62004-01-31 19:22:56 +00001696 assert( pTos>=p->aStack );
1697 if( pTos->flags & MEM_Real ){
drh8df447f2005-11-01 15:48:24 +00001698 neg_abs_real_case:
drh6810ce62004-01-31 19:22:56 +00001699 Release(pTos);
1700 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1701 pTos->r = -pTos->r;
drhbf4133c2001-10-13 02:59:08 +00001702 }
drh6810ce62004-01-31 19:22:56 +00001703 pTos->flags = MEM_Real;
1704 }else if( pTos->flags & MEM_Int ){
1705 Release(pTos);
1706 if( pOp->opcode==OP_Negative || pTos->i<0 ){
1707 pTos->i = -pTos->i;
drhbf4133c2001-10-13 02:59:08 +00001708 }
drh6810ce62004-01-31 19:22:56 +00001709 pTos->flags = MEM_Int;
1710 }else if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001711 /* Do nothing */
drh5e00f6c2001-09-13 13:46:56 +00001712 }else{
drh8a512562005-11-14 22:29:05 +00001713 sqlite3VdbeMemNumerify(pTos);
drh8df447f2005-11-01 15:48:24 +00001714 goto neg_abs_real_case;
drh5e00f6c2001-09-13 13:46:56 +00001715 }
1716 break;
1717}
1718
1719/* Opcode: Not * * *
1720**
1721** Interpret the top of the stack as a boolean value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001722** with its complement. If the top of the stack is NULL its value
1723** is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001724*/
danielk19777a5147c2005-03-29 13:07:00 +00001725case OP_Not: { /* same as TK_NOT, no-push */
drh6810ce62004-01-31 19:22:56 +00001726 assert( pTos>=p->aStack );
1727 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
drh8a512562005-11-14 22:29:05 +00001728 sqlite3VdbeMemIntegerify(pTos);
drh6a6124e2004-06-27 01:56:33 +00001729 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00001730 pTos->i = !pTos->i;
drh79f14b72004-03-03 01:51:24 +00001731 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001732 break;
1733}
1734
drh18b81e52001-11-01 13:52:52 +00001735/* Opcode: BitNot * * *
drhbf4133c2001-10-13 02:59:08 +00001736**
1737** Interpret the top of the stack as an value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001738** with its ones-complement. If the top of the stack is NULL its
1739** value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001740*/
danielk19777a5147c2005-03-29 13:07:00 +00001741case OP_BitNot: { /* same as TK_BITNOT, no-push */
drh6810ce62004-01-31 19:22:56 +00001742 assert( pTos>=p->aStack );
1743 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
drh8a512562005-11-14 22:29:05 +00001744 sqlite3VdbeMemIntegerify(pTos);
drh6a6124e2004-06-27 01:56:33 +00001745 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00001746 pTos->i = ~pTos->i;
drh79f14b72004-03-03 01:51:24 +00001747 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001748 break;
1749}
1750
drh5e00f6c2001-09-13 13:46:56 +00001751/* Opcode: Noop * * *
1752**
1753** Do nothing. This instruction is often useful as a jump
1754** destination.
1755*/
drhecc92422005-09-10 16:46:12 +00001756/*
1757** The magic Explain opcode are only inserted when explain==2 (which
1758** is to say when the EXPLAIN QUERY PLAN syntax is used.)
1759** This opcode records information from the optimizer. It is the
1760** the same as a no-op. This opcodesnever appears in a real VM program.
1761*/
1762case OP_Explain:
danielk19777a5147c2005-03-29 13:07:00 +00001763case OP_Noop: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00001764 break;
1765}
1766
drhf5905aa2002-05-26 20:54:33 +00001767/* Opcode: If P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001768**
1769** Pop a single boolean from the stack. If the boolean popped is
1770** true, then jump to p2. Otherwise continue to the next instruction.
1771** An integer is false if zero and true otherwise. A string is
1772** false if it has zero length and true otherwise.
drhf5905aa2002-05-26 20:54:33 +00001773**
1774** If the value popped of the stack is NULL, then take the jump if P1
1775** is true and fall through if P1 is false.
drh5e00f6c2001-09-13 13:46:56 +00001776*/
drhf5905aa2002-05-26 20:54:33 +00001777/* Opcode: IfNot P1 P2 *
1778**
1779** Pop a single boolean from the stack. If the boolean popped is
1780** false, then jump to p2. Otherwise continue to the next instruction.
1781** An integer is false if zero and true otherwise. A string is
1782** false if it has zero length and true otherwise.
1783**
1784** If the value popped of the stack is NULL, then take the jump if P1
1785** is true and fall through if P1 is false.
1786*/
danielk19777a5147c2005-03-29 13:07:00 +00001787case OP_If: /* no-push */
1788case OP_IfNot: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00001789 int c;
drh6810ce62004-01-31 19:22:56 +00001790 assert( pTos>=p->aStack );
1791 if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001792 c = pOp->p1;
1793 }else{
drhba0232a2005-06-06 17:27:19 +00001794#ifdef SQLITE_OMIT_FLOATING_POINT
drh6a6124e2004-06-27 01:56:33 +00001795 c = sqlite3VdbeIntValue(pTos);
drhba0232a2005-06-06 17:27:19 +00001796#else
1797 c = sqlite3VdbeRealValue(pTos)!=0.0;
1798#endif
drhf5905aa2002-05-26 20:54:33 +00001799 if( pOp->opcode==OP_IfNot ) c = !c;
1800 }
danielk19778a6b5412004-05-24 07:04:25 +00001801 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001802 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00001803 if( c ) pc = pOp->p2-1;
1804 break;
1805}
1806
drhf5905aa2002-05-26 20:54:33 +00001807/* Opcode: IsNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001808**
drhf5905aa2002-05-26 20:54:33 +00001809** If any of the top abs(P1) values on the stack are NULL, then jump
drh143f3c42004-01-07 20:37:52 +00001810** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack
1811** unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001812*/
danielk19777a5147c2005-03-29 13:07:00 +00001813case OP_IsNull: { /* same as TK_ISNULL, no-push */
drhf5905aa2002-05-26 20:54:33 +00001814 int i, cnt;
drh6810ce62004-01-31 19:22:56 +00001815 Mem *pTerm;
drhf5905aa2002-05-26 20:54:33 +00001816 cnt = pOp->p1;
1817 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001818 pTerm = &pTos[1-cnt];
1819 assert( pTerm>=p->aStack );
1820 for(i=0; i<cnt; i++, pTerm++){
1821 if( pTerm->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001822 pc = pOp->p2-1;
1823 break;
1824 }
1825 }
drh6810ce62004-01-31 19:22:56 +00001826 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001827 break;
1828}
1829
drhf5905aa2002-05-26 20:54:33 +00001830/* Opcode: NotNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001831**
drh143f3c42004-01-07 20:37:52 +00001832** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the
1833** stack if P1 times if P1 is greater than zero. If P1 is less than
1834** zero then leave the stack unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001835*/
danielk19777a5147c2005-03-29 13:07:00 +00001836case OP_NotNull: { /* same as TK_NOTNULL, no-push */
drh143f3c42004-01-07 20:37:52 +00001837 int i, cnt;
1838 cnt = pOp->p1;
1839 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001840 assert( &pTos[1-cnt] >= p->aStack );
1841 for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
drh143f3c42004-01-07 20:37:52 +00001842 if( i>=cnt ) pc = pOp->p2-1;
drh6810ce62004-01-31 19:22:56 +00001843 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001844 break;
1845}
1846
danielk1977b4964b72004-05-18 01:23:38 +00001847/* Opcode: SetNumColumns P1 P2 *
1848**
1849** Before the OP_Column opcode can be executed on a cursor, this
1850** opcode must be called to set the number of fields in the table.
1851**
1852** This opcode sets the number of columns for cursor P1 to P2.
danielk1977ac171782005-02-05 06:49:54 +00001853**
1854** If OP_KeyAsData is to be applied to cursor P1, it must be executed
1855** before this op-code.
danielk1977b4964b72004-05-18 01:23:38 +00001856*/
danielk19777a5147c2005-03-29 13:07:00 +00001857case OP_SetNumColumns: { /* no-push */
danielk1977ac171782005-02-05 06:49:54 +00001858 Cursor *pC;
danielk1977b4964b72004-05-18 01:23:38 +00001859 assert( (pOp->p1)<p->nCursor );
drh4774b132004-06-12 20:12:51 +00001860 assert( p->apCsr[pOp->p1]!=0 );
danielk1977ac171782005-02-05 06:49:54 +00001861 pC = p->apCsr[pOp->p1];
1862 pC->nField = pOp->p2;
danielk1977b4964b72004-05-18 01:23:38 +00001863 break;
1864}
1865
danielk1977aee18ef2005-03-09 12:26:50 +00001866/* Opcode: Column P1 P2 P3
danielk1977192ac1d2004-05-10 07:17:30 +00001867**
danielk1977cfcdaef2004-05-12 07:33:33 +00001868** Interpret the data that cursor P1 points to as a structure built using
1869** the MakeRecord instruction. (See the MakeRecord opcode for additional
1870** information about the format of the data.) Push onto the stack the value
danielk197736963fd2005-02-19 08:18:05 +00001871** of the P2-th column contained in the data. If there are less that (P2+1)
1872** values in the record, push a NULL onto the stack.
danielk1977192ac1d2004-05-10 07:17:30 +00001873**
danielk1977cfcdaef2004-05-12 07:33:33 +00001874** If the KeyAsData opcode has previously executed on this cursor, then the
1875** field might be extracted from the key rather than the data.
danielk1977192ac1d2004-05-10 07:17:30 +00001876**
drh29dda4a2005-07-21 18:23:20 +00001877** If the column contains fewer than P2 fields, then push a NULL. Or
1878** if P3 is of type P3_MEM, then push the P3 value. The P3 value will
1879** be default value for a column that has been added using the ALTER TABLE
1880** ADD COLUMN command. If P3 is an ordinary string, just push a NULL.
1881** When P3 is a string it is really just a comment describing the value
1882** to be pushed, not a default value.
danielk1977192ac1d2004-05-10 07:17:30 +00001883*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001884case OP_Column: {
danielk1977e0d4b062004-06-28 01:11:46 +00001885 u32 payloadSize; /* Number of bytes in the record */
drhd3194f52004-05-27 19:59:32 +00001886 int p1 = pOp->p1; /* P1 value of the opcode */
danielk1977cfcdaef2004-05-12 07:33:33 +00001887 int p2 = pOp->p2; /* column number to retrieve */
drhd3194f52004-05-27 19:59:32 +00001888 Cursor *pC = 0; /* The VDBE cursor */
drhe61cffc2004-06-12 18:12:15 +00001889 char *zRec; /* Pointer to complete record-data */
drhd3194f52004-05-27 19:59:32 +00001890 BtCursor *pCrsr; /* The BTree cursor */
1891 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1892 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
drh25aa1b42004-05-28 01:39:01 +00001893 u32 nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00001894 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00001895 int i; /* Loop counter */
1896 char *zData; /* Part of the record being decoded */
1897 Mem sMem; /* For storing the record being decoded */
danielk1977192ac1d2004-05-10 07:17:30 +00001898
drhb6f54522004-05-20 02:42:16 +00001899 sMem.flags = 0;
drhd3194f52004-05-27 19:59:32 +00001900 assert( p1<p->nCursor );
danielk1977192ac1d2004-05-10 07:17:30 +00001901 pTos++;
drhfebe1062004-08-28 18:17:48 +00001902 pTos->flags = MEM_Null;
danielk1977cfcdaef2004-05-12 07:33:33 +00001903
drhe61cffc2004-06-12 18:12:15 +00001904 /* This block sets the variable payloadSize to be the total number of
1905 ** bytes in the record.
1906 **
1907 ** zRec is set to be the complete text of the record if it is available.
drhb73857f2006-03-17 00:25:59 +00001908 ** The complete record text is always available for pseudo-tables
1909 ** If the record is stored in a cursor, the complete record text
1910 ** might be available in the pC->aRow cache. Or it might not be.
1911 ** If the data is unavailable, zRec is set to NULL.
drhd3194f52004-05-27 19:59:32 +00001912 **
1913 ** We also compute the number of columns in the record. For cursors,
1914 ** the number of columns is stored in the Cursor.nField element. For
1915 ** records on the stack, the next entry down on the stack is an integer
1916 ** which is the number of records.
danielk1977cfcdaef2004-05-12 07:33:33 +00001917 */
drhb73857f2006-03-17 00:25:59 +00001918 pC = p->apCsr[p1];
1919 assert( pC!=0 );
1920 if( pC->pCursor!=0 ){
drhe61cffc2004-06-12 18:12:15 +00001921 /* The record is stored in a B-Tree */
drh536065a2005-01-26 21:55:31 +00001922 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00001923 if( rc ) goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00001924 zRec = 0;
1925 pCrsr = pC->pCursor;
1926 if( pC->nullRow ){
1927 payloadSize = 0;
drh76873ab2006-01-07 18:48:26 +00001928 }else if( pC->cacheStatus==p->cacheCtr ){
drh9188b382004-05-14 21:12:22 +00001929 payloadSize = pC->payloadSize;
drh2646da72005-12-09 20:02:05 +00001930 zRec = (char*)pC->aRow;
drhf0863fe2005-06-12 21:35:51 +00001931 }else if( pC->isIndex ){
danielk197796fc5fe2004-05-13 11:34:16 +00001932 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00001933 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1934 payloadSize = payloadSize64;
1935 }else{
1936 sqlite3BtreeDataSize(pCrsr, &payloadSize);
1937 }
drhd3194f52004-05-27 19:59:32 +00001938 nField = pC->nField;
danielk1977192ac1d2004-05-10 07:17:30 +00001939 }else if( pC->pseudoTable ){
drhe61cffc2004-06-12 18:12:15 +00001940 /* The record is the sole entry of a pseudo-table */
danielk1977192ac1d2004-05-10 07:17:30 +00001941 payloadSize = pC->nData;
1942 zRec = pC->pData;
drh76873ab2006-01-07 18:48:26 +00001943 pC->cacheStatus = CACHE_STALE;
danielk1977192ac1d2004-05-10 07:17:30 +00001944 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00001945 nField = pC->nField;
danielk1977f7df9cc2004-06-16 12:02:47 +00001946 pCrsr = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001947 }else{
danielk1977f7df9cc2004-06-16 12:02:47 +00001948 zRec = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001949 payloadSize = 0;
danielk1977f7df9cc2004-06-16 12:02:47 +00001950 pCrsr = 0;
1951 nField = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001952 }
1953
1954 /* If payloadSize is 0, then just push a NULL onto the stack. */
1955 if( payloadSize==0 ){
danielk19779792eef2006-01-13 15:58:43 +00001956 assert( pTos->flags==MEM_Null );
danielk1977192ac1d2004-05-10 07:17:30 +00001957 break;
1958 }
1959
drhd3194f52004-05-27 19:59:32 +00001960 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00001961
drh9188b382004-05-14 21:12:22 +00001962 /* Read and parse the table header. Store the results of the parse
1963 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00001964 */
drh76873ab2006-01-07 18:48:26 +00001965 if( pC && pC->cacheStatus==p->cacheCtr ){
drhd3194f52004-05-27 19:59:32 +00001966 aType = pC->aType;
1967 aOffset = pC->aOffset;
1968 }else{
danielk1977dedf45b2006-01-13 17:12:01 +00001969 u8 *zIdx; /* Index into header */
1970 u8 *zEndHdr; /* Pointer to first byte after the header */
1971 u32 offset; /* Offset into the data */
drh0ac07192006-02-10 14:02:07 +00001972 int szHdrSz; /* Size of the header size field at start of record */
danielk1977dedf45b2006-01-13 17:12:01 +00001973 int avail; /* Number of bytes of available data */
drhb73857f2006-03-17 00:25:59 +00001974
1975 aType = pC->aType;
1976 if( aType==0 ){
1977 pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
drh51846b52004-05-28 16:00:21 +00001978 }
drhd3194f52004-05-27 19:59:32 +00001979 if( aType==0 ){
1980 goto no_mem;
drh9188b382004-05-14 21:12:22 +00001981 }
drhb73857f2006-03-17 00:25:59 +00001982 pC->aOffset = aOffset = &aType[nField];
1983 pC->payloadSize = payloadSize;
1984 pC->cacheStatus = p->cacheCtr;
danielk1977192ac1d2004-05-10 07:17:30 +00001985
drhd3194f52004-05-27 19:59:32 +00001986 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00001987 if( zRec ){
1988 zData = zRec;
1989 }else{
drhf0863fe2005-06-12 21:35:51 +00001990 if( pC->isIndex ){
drhe51c44f2004-05-30 20:46:09 +00001991 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
drhd3194f52004-05-27 19:59:32 +00001992 }else{
drhe51c44f2004-05-30 20:46:09 +00001993 zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
drh9188b382004-05-14 21:12:22 +00001994 }
drhe61cffc2004-06-12 18:12:15 +00001995 /* If KeyFetch()/DataFetch() managed to get the entire payload,
1996 ** save the payload in the pC->aRow cache. That will save us from
1997 ** having to make additional calls to fetch the content portion of
1998 ** the record.
1999 */
2000 if( avail>=payloadSize ){
drh2646da72005-12-09 20:02:05 +00002001 zRec = zData;
2002 pC->aRow = (u8*)zData;
drhe61cffc2004-06-12 18:12:15 +00002003 }else{
2004 pC->aRow = 0;
2005 }
drhd3194f52004-05-27 19:59:32 +00002006 }
drh0ac07192006-02-10 14:02:07 +00002007 assert( zRec!=0 || avail>=payloadSize || avail>=9 );
2008 szHdrSz = GetVarint((u8*)zData, offset);
drhe61cffc2004-06-12 18:12:15 +00002009
2010 /* The KeyFetch() or DataFetch() above are fast and will get the entire
2011 ** record header in most cases. But they will fail to get the complete
2012 ** record header if the record header does not fit on a single page
2013 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2014 ** acquire the complete header text.
2015 */
danielk1977dedf45b2006-01-13 17:12:01 +00002016 if( !zRec && avail<offset ){
2017 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00002018 if( rc!=SQLITE_OK ){
danielk19773c9cc8d2005-01-17 03:40:08 +00002019 goto op_column_out;
drh9188b382004-05-14 21:12:22 +00002020 }
drhb6f54522004-05-20 02:42:16 +00002021 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00002022 }
drh0ac07192006-02-10 14:02:07 +00002023 zEndHdr = (u8 *)&zData[offset];
2024 zIdx = (u8 *)&zData[szHdrSz];
drh9188b382004-05-14 21:12:22 +00002025
drhd3194f52004-05-27 19:59:32 +00002026 /* Scan the header and use it to fill in the aType[] and aOffset[]
2027 ** arrays. aType[i] will contain the type integer for the i-th
2028 ** column and aOffset[i] will contain the offset from the beginning
2029 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00002030 */
danielk1977dedf45b2006-01-13 17:12:01 +00002031 for(i=0; i<nField; i++){
2032 if( zIdx<zEndHdr ){
2033 aOffset[i] = offset;
2034 zIdx += GetVarint(zIdx, aType[i]);
2035 offset += sqlite3VdbeSerialTypeLen(aType[i]);
2036 }else{
2037 /* If i is less that nField, then there are less fields in this
2038 ** record than SetNumColumns indicated there are columns in the
2039 ** table. Set the offset for any extra columns not present in
2040 ** the record to 0. This tells code below to push a NULL onto the
2041 ** stack instead of deserializing a value from the record.
2042 */
2043 aOffset[i] = 0;
2044 }
drh9188b382004-05-14 21:12:22 +00002045 }
drhb6f54522004-05-20 02:42:16 +00002046 Release(&sMem);
drhd3194f52004-05-27 19:59:32 +00002047 sMem.flags = MEM_Null;
2048
danielk19779792eef2006-01-13 15:58:43 +00002049 /* If we have read more header data than was contained in the header,
2050 ** or if the end of the last field appears to be past the end of the
2051 ** record, then we must be dealing with a corrupt database.
drhd3194f52004-05-27 19:59:32 +00002052 */
danielk1977dedf45b2006-01-13 17:12:01 +00002053 if( zIdx>zEndHdr || offset>payloadSize ){
drh49285702005-09-17 15:20:26 +00002054 rc = SQLITE_CORRUPT_BKPT;
danielk19773c9cc8d2005-01-17 03:40:08 +00002055 goto op_column_out;
drhd3194f52004-05-27 19:59:32 +00002056 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002057 }
danielk1977192ac1d2004-05-10 07:17:30 +00002058
danielk197736963fd2005-02-19 08:18:05 +00002059 /* Get the column information. If aOffset[p2] is non-zero, then
2060 ** deserialize the value from the record. If aOffset[p2] is zero,
2061 ** then there are not enough fields in the record to satisfy the
drh29dda4a2005-07-21 18:23:20 +00002062 ** request. In this case, set the value NULL or to P3 if P3 is
2063 ** a pointer to a Mem object.
drh9188b382004-05-14 21:12:22 +00002064 */
danielk197736963fd2005-02-19 08:18:05 +00002065 if( aOffset[p2] ){
2066 assert( rc==SQLITE_OK );
2067 if( zRec ){
2068 zData = &zRec[aOffset[p2]];
2069 }else{
2070 len = sqlite3VdbeSerialTypeLen(aType[p2]);
drhf0863fe2005-06-12 21:35:51 +00002071 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);
danielk197736963fd2005-02-19 08:18:05 +00002072 if( rc!=SQLITE_OK ){
2073 goto op_column_out;
2074 }
2075 zData = sMem.z;
danielk19777701e812005-01-10 12:59:51 +00002076 }
drh2646da72005-12-09 20:02:05 +00002077 sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos);
drh8079a0d2006-01-12 17:20:50 +00002078 pTos->enc = encoding;
danielk197736963fd2005-02-19 08:18:05 +00002079 }else{
drh29dda4a2005-07-21 18:23:20 +00002080 if( pOp->p3type==P3_MEM ){
danielk1977aee18ef2005-03-09 12:26:50 +00002081 sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
2082 }else{
2083 pTos->flags = MEM_Null;
2084 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002085 }
drhfebe1062004-08-28 18:17:48 +00002086
2087 /* If we dynamically allocated space to hold the data (in the
2088 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
drhcdd536f2006-03-17 00:04:03 +00002089 ** dynamically allocated space over to the pTos structure.
drhfebe1062004-08-28 18:17:48 +00002090 ** This prevents a memory copy.
2091 */
2092 if( (sMem.flags & MEM_Dyn)!=0 ){
2093 assert( pTos->flags & MEM_Ephem );
2094 assert( pTos->flags & (MEM_Str|MEM_Blob) );
2095 assert( pTos->z==sMem.z );
2096 assert( sMem.flags & MEM_Term );
2097 pTos->flags &= ~MEM_Ephem;
2098 pTos->flags |= MEM_Dyn|MEM_Term;
danielk1977b1bc9532004-05-22 03:05:33 +00002099 }
drhfebe1062004-08-28 18:17:48 +00002100
2101 /* pTos->z might be pointing to sMem.zShort[]. Fix that so that we
2102 ** can abandon sMem */
2103 rc = sqlite3VdbeMemMakeWriteable(pTos);
drhd3194f52004-05-27 19:59:32 +00002104
danielk19773c9cc8d2005-01-17 03:40:08 +00002105op_column_out:
danielk1977192ac1d2004-05-10 07:17:30 +00002106 break;
2107}
2108
drh670fb032004-11-15 23:42:27 +00002109/* Opcode: MakeRecord P1 P2 P3
drh7a224de2004-06-02 01:22:02 +00002110**
2111** Convert the top abs(P1) entries of the stack into a single entry
2112** suitable for use as a data record in a database table or as a key
2113** in an index. The details of the format are irrelavant as long as
2114** the OP_Column opcode can decode the record later and as long as the
2115** sqlite3VdbeRecordCompare function will correctly compare two encoded
2116** records. Refer to source code comments for the details of the record
2117** format.
2118**
2119** The original stack entries are popped from the stack if P1>0 but
2120** remain on the stack if P1<0.
2121**
drh7f057c92005-06-24 03:53:06 +00002122** If P2 is not zero and one or more of the entries are NULL, then jump
2123** to the address given by P2. This feature can be used to skip a
2124** uniqueness test on indices.
drh7a224de2004-06-02 01:22:02 +00002125**
2126** P3 may be a string that is P1 characters long. The nth character of the
2127** string indicates the column affinity that should be used for the nth
2128** field of the index key (i.e. the first character of P3 corresponds to the
2129** lowest element on the stack).
2130**
drh8a512562005-11-14 22:29:05 +00002131** The mapping from character to affinity is given by the SQLITE_AFF_
2132** macros defined in sqliteInt.h.
drh7a224de2004-06-02 01:22:02 +00002133**
2134** If P3 is NULL then all index fields have the affinity NONE.
drh7f057c92005-06-24 03:53:06 +00002135**
2136** See also OP_MakeIdxRec
drh7a224de2004-06-02 01:22:02 +00002137*/
drhd946db02005-12-29 19:23:06 +00002138/* Opcode: MakeIdxRec P1 P2 P3
drh7f057c92005-06-24 03:53:06 +00002139**
2140** This opcode works just OP_MakeRecord except that it reads an extra
2141** integer from the stack (thus reading a total of abs(P1+1) entries)
2142** and appends that extra integer to the end of the record as a varint.
2143** This results in an index key.
2144*/
2145case OP_MakeIdxRec:
drhf3218fe2004-05-28 08:21:02 +00002146case OP_MakeRecord: {
2147 /* Assuming the record contains N fields, the record format looks
2148 ** like this:
2149 **
drh7a224de2004-06-02 01:22:02 +00002150 ** ------------------------------------------------------------------------
2151 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2152 ** ------------------------------------------------------------------------
drhf3218fe2004-05-28 08:21:02 +00002153 **
2154 ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2155 ** the top of the stack.
2156 **
2157 ** Each type field is a varint representing the serial type of the
2158 ** corresponding data element (see sqlite3VdbeSerialType()). The
drh7a224de2004-06-02 01:22:02 +00002159 ** hdr-size field is also a varint which is the offset from the beginning
2160 ** of the record to data0.
drhf3218fe2004-05-28 08:21:02 +00002161 */
drhf3218fe2004-05-28 08:21:02 +00002162 unsigned char *zNewRecord;
2163 unsigned char *zCsr;
danielk19778d059842004-05-12 11:24:02 +00002164 Mem *pRec;
danielk1977f7df9cc2004-06-16 12:02:47 +00002165 Mem *pRowid = 0;
danielk1977ededfd52004-06-17 07:53:01 +00002166 int nData = 0; /* Number of bytes of data space */
2167 int nHdr = 0; /* Number of bytes of header space */
2168 int nByte = 0; /* Space required for this record */
drhcb9882a2005-03-17 03:15:40 +00002169 int nVarint; /* Number of bytes in a varint */
danielk1977ededfd52004-06-17 07:53:01 +00002170 u32 serial_type; /* Type field */
2171 int containsNull = 0; /* True if any of the data fields are NULL */
2172 char zTemp[NBFS]; /* Space to hold small records */
2173 Mem *pData0;
drhf3218fe2004-05-28 08:21:02 +00002174
danielk1977ededfd52004-06-17 07:53:01 +00002175 int leaveOnStack; /* If true, leave the entries on the stack */
2176 int nField; /* Number of fields in the record */
2177 int jumpIfNull; /* Jump here if non-zero and any entries are NULL. */
2178 int addRowid; /* True to append a rowid column at the end */
2179 char *zAffinity; /* The affinity string for the record */
drhd946db02005-12-29 19:23:06 +00002180 int file_format; /* File format to use for encoding */
danielk1977ededfd52004-06-17 07:53:01 +00002181
2182 leaveOnStack = ((pOp->p1<0)?1:0);
2183 nField = pOp->p1 * (leaveOnStack?-1:1);
drh7f057c92005-06-24 03:53:06 +00002184 jumpIfNull = pOp->p2;
2185 addRowid = pOp->opcode==OP_MakeIdxRec;
drhf3218fe2004-05-28 08:21:02 +00002186 zAffinity = pOp->p3;
danielk1977ededfd52004-06-17 07:53:01 +00002187
2188 pData0 = &pTos[1-nField];
2189 assert( pData0>=p->aStack );
drhf3218fe2004-05-28 08:21:02 +00002190 containsNull = 0;
drhd946db02005-12-29 19:23:06 +00002191 file_format = p->minWriteFileFormat;
danielk19778d059842004-05-12 11:24:02 +00002192
drhf3218fe2004-05-28 08:21:02 +00002193 /* Loop through the elements that will make up the record to figure
2194 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002195 */
2196 for(pRec=pData0; pRec<=pTos; pRec++){
drhd3d39e92004-05-20 22:16:29 +00002197 if( zAffinity ){
drh8079a0d2006-01-12 17:20:50 +00002198 applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
drhd3d39e92004-05-20 22:16:29 +00002199 }
danielk19773d1bfea2004-05-14 11:00:53 +00002200 if( pRec->flags&MEM_Null ){
danielk1977452c9892004-05-13 05:16:15 +00002201 containsNull = 1;
2202 }
drhd946db02005-12-29 19:23:06 +00002203 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhf3218fe2004-05-28 08:21:02 +00002204 nData += sqlite3VdbeSerialTypeLen(serial_type);
2205 nHdr += sqlite3VarintLen(serial_type);
danielk19778d059842004-05-12 11:24:02 +00002206 }
danielk19773d1bfea2004-05-14 11:00:53 +00002207
2208 /* If we have to append a varint rowid to this record, set 'rowid'
2209 ** to the value of the rowid and increase nByte by the amount of space
2210 ** required to store it and the 0x00 seperator byte.
2211 */
danielk19778d059842004-05-12 11:24:02 +00002212 if( addRowid ){
drhf3218fe2004-05-28 08:21:02 +00002213 pRowid = &pTos[0-nField];
2214 assert( pRowid>=p->aStack );
drh8a512562005-11-14 22:29:05 +00002215 sqlite3VdbeMemIntegerify(pRowid);
drhd946db02005-12-29 19:23:06 +00002216 serial_type = sqlite3VdbeSerialType(pRowid, 0);
drhf3218fe2004-05-28 08:21:02 +00002217 nData += sqlite3VdbeSerialTypeLen(serial_type);
2218 nHdr += sqlite3VarintLen(serial_type);
danielk19778d059842004-05-12 11:24:02 +00002219 }
drhf3218fe2004-05-28 08:21:02 +00002220
2221 /* Add the initial header varint and total the size */
drhcb9882a2005-03-17 03:15:40 +00002222 nHdr += nVarint = sqlite3VarintLen(nHdr);
2223 if( nVarint<sqlite3VarintLen(nHdr) ){
2224 nHdr++;
2225 }
drhf3218fe2004-05-28 08:21:02 +00002226 nByte = nHdr+nData;
2227
drhf3218fe2004-05-28 08:21:02 +00002228 /* Allocate space for the new record. */
drh51846b52004-05-28 16:00:21 +00002229 if( nByte>sizeof(zTemp) ){
2230 zNewRecord = sqliteMallocRaw(nByte);
2231 if( !zNewRecord ){
2232 goto no_mem;
2233 }
2234 }else{
drh2646da72005-12-09 20:02:05 +00002235 zNewRecord = (u8*)zTemp;
danielk19778d059842004-05-12 11:24:02 +00002236 }
drhf3218fe2004-05-28 08:21:02 +00002237
2238 /* Write the record */
2239 zCsr = zNewRecord;
2240 zCsr += sqlite3PutVarint(zCsr, nHdr);
danielk19778d059842004-05-12 11:24:02 +00002241 for(pRec=pData0; pRec<=pTos; pRec++){
drhd946db02005-12-29 19:23:06 +00002242 serial_type = sqlite3VdbeSerialType(pRec, file_format);
drhf3218fe2004-05-28 08:21:02 +00002243 zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002244 }
2245 if( addRowid ){
drhd946db02005-12-29 19:23:06 +00002246 zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid, 0));
danielk19778d059842004-05-12 11:24:02 +00002247 }
drhf3218fe2004-05-28 08:21:02 +00002248 for(pRec=pData0; pRec<=pTos; pRec++){
drhd946db02005-12-29 19:23:06 +00002249 zCsr += sqlite3VdbeSerialPut(zCsr, pRec, file_format); /* serial data */
drhf3218fe2004-05-28 08:21:02 +00002250 }
2251 if( addRowid ){
drhd946db02005-12-29 19:23:06 +00002252 zCsr += sqlite3VdbeSerialPut(zCsr, pRowid, 0);
drhf3218fe2004-05-28 08:21:02 +00002253 }
drh4ff55082005-03-17 03:52:47 +00002254 assert( zCsr==(zNewRecord+nByte) );
drhf3218fe2004-05-28 08:21:02 +00002255
danielk1977ededfd52004-06-17 07:53:01 +00002256 /* Pop entries off the stack if required. Push the new record on. */
2257 if( !leaveOnStack ){
danielk1977452c9892004-05-13 05:16:15 +00002258 popStack(&pTos, nField+addRowid);
2259 }
danielk19778d059842004-05-12 11:24:02 +00002260 pTos++;
danielk19778d059842004-05-12 11:24:02 +00002261 pTos->n = nByte;
drh51846b52004-05-28 16:00:21 +00002262 if( nByte<=sizeof(zTemp) ){
danielk1977ef2cb632004-05-29 02:37:19 +00002263 assert( zNewRecord==(unsigned char *)zTemp );
drh51846b52004-05-28 16:00:21 +00002264 pTos->z = pTos->zShort;
2265 memcpy(pTos->zShort, zTemp, nByte);
2266 pTos->flags = MEM_Blob | MEM_Short;
2267 }else{
danielk1977ef2cb632004-05-29 02:37:19 +00002268 assert( zNewRecord!=(unsigned char *)zTemp );
drh2646da72005-12-09 20:02:05 +00002269 pTos->z = (char*)zNewRecord;
drh51846b52004-05-28 16:00:21 +00002270 pTos->flags = MEM_Blob | MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00002271 pTos->xDel = 0;
drh51846b52004-05-28 16:00:21 +00002272 }
drh487e2622005-06-25 18:42:14 +00002273 pTos->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
danielk19778d059842004-05-12 11:24:02 +00002274
danielk1977ededfd52004-06-17 07:53:01 +00002275 /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */
2276 if( jumpIfNull && containsNull ){
2277 pc = jumpIfNull - 1;
danielk1977452c9892004-05-13 05:16:15 +00002278 }
danielk19778d059842004-05-12 11:24:02 +00002279 break;
2280}
2281
drh7f0f12e2004-05-21 13:39:50 +00002282/* Opcode: Statement P1 * *
drh663fc632002-02-02 18:49:19 +00002283**
drh7f0f12e2004-05-21 13:39:50 +00002284** Begin an individual statement transaction which is part of a larger
2285** BEGIN..COMMIT transaction. This is needed so that the statement
2286** can be rolled back after an error without having to roll back the
2287** entire transaction. The statement transaction will automatically
2288** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002289**
drh7f0f12e2004-05-21 13:39:50 +00002290** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002291** database file has an index of 0 and the file used for temporary tables
2292** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002293*/
danielk19777a5147c2005-03-29 13:07:00 +00002294case OP_Statement: { /* no-push */
drh001bbcb2003-03-19 03:14:00 +00002295 int i = pOp->p1;
danielk19771d850a72004-05-31 08:26:49 +00002296 Btree *pBt;
drhd9cb6ac2005-10-20 07:28:17 +00002297 if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){
danielk19771d850a72004-05-31 08:26:49 +00002298 assert( sqlite3BtreeIsInTrans(pBt) );
2299 if( !sqlite3BtreeIsInStmt(pBt) ){
2300 rc = sqlite3BtreeBeginStmt(pBt);
2301 }
2302 }
2303 break;
2304}
2305
2306/* Opcode: AutoCommit P1 P2 *
2307**
2308** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
danielk197746c43ed2004-06-30 06:30:25 +00002309** back any currently active btree transactions. If there are any active
2310** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
drh92f02c32004-09-02 14:57:08 +00002311**
2312** This instruction causes the VM to halt.
danielk19771d850a72004-05-31 08:26:49 +00002313*/
danielk19777a5147c2005-03-29 13:07:00 +00002314case OP_AutoCommit: { /* no-push */
danielk19771d850a72004-05-31 08:26:49 +00002315 u8 i = pOp->p1;
2316 u8 rollback = pOp->p2;
2317
2318 assert( i==1 || i==0 );
2319 assert( i==1 || rollback==0 );
2320
drh92f02c32004-09-02 14:57:08 +00002321 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
danielk197746c43ed2004-06-30 06:30:25 +00002322
2323 if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
2324 /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
2325 ** still running, and a transaction is active, return an error indicating
2326 ** that the other VMs must complete first.
2327 */
2328 sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit",
drhf93339d2006-01-03 15:16:26 +00002329 " transaction - SQL statements in progress", (char*)0);
danielk197746c43ed2004-06-30 06:30:25 +00002330 rc = SQLITE_ERROR;
2331 }else if( i!=db->autoCommit ){
danielk19771d850a72004-05-31 08:26:49 +00002332 if( pOp->p2 ){
drh92f02c32004-09-02 14:57:08 +00002333 assert( i==1 );
danielk19771d850a72004-05-31 08:26:49 +00002334 sqlite3RollbackAll(db);
danielk1977f3f06bb2005-12-16 15:24:28 +00002335 db->autoCommit = 1;
2336 }else{
2337 db->autoCommit = i;
2338 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2339 p->pTos = pTos;
2340 p->pc = pc;
2341 db->autoCommit = 1-i;
2342 p->rc = SQLITE_BUSY;
2343 return SQLITE_BUSY;
2344 }
danielk19771d850a72004-05-31 08:26:49 +00002345 }
drh92f02c32004-09-02 14:57:08 +00002346 return SQLITE_DONE;
danielk19771d850a72004-05-31 08:26:49 +00002347 }else{
2348 sqlite3SetString(&p->zErrMsg,
2349 (!i)?"cannot start a transaction within a transaction":(
2350 (rollback)?"cannot rollback - no transaction is active":
drhf93339d2006-01-03 15:16:26 +00002351 "cannot commit - no transaction is active"), (char*)0);
danielk19771d850a72004-05-31 08:26:49 +00002352
2353 rc = SQLITE_ERROR;
drh663fc632002-02-02 18:49:19 +00002354 }
2355 break;
2356}
2357
danielk1977ee5741e2004-05-31 10:01:34 +00002358/* Opcode: Transaction P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002359**
2360** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002361** opcode is encountered. Depending on the ON CONFLICT setting, the
2362** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002363**
drh001bbcb2003-03-19 03:14:00 +00002364** P1 is the index of the database file on which the transaction is
2365** started. Index 0 is the main database file and index 1 is the
2366** file used for temporary tables.
drhcabb0812002-09-14 13:47:32 +00002367**
drh80242052004-06-09 00:48:12 +00002368** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
danielk1977ee5741e2004-05-31 10:01:34 +00002369** obtained on the database file when a write-transaction is started. No
drh80242052004-06-09 00:48:12 +00002370** other process can start another write transaction while this transaction is
2371** underway. Starting a write transaction also creates a rollback journal. A
2372** write transaction must be started before any changes can be made to the
drh684917c2004-10-05 02:41:42 +00002373** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2374** on the file.
danielk1977ee5741e2004-05-31 10:01:34 +00002375**
2376** If P2 is zero, then a read-lock is obtained on the database file.
drh5e00f6c2001-09-13 13:46:56 +00002377*/
danielk19777a5147c2005-03-29 13:07:00 +00002378case OP_Transaction: { /* no-push */
drh001bbcb2003-03-19 03:14:00 +00002379 int i = pOp->p1;
danielk19771d850a72004-05-31 08:26:49 +00002380 Btree *pBt;
2381
drh8bf8dc92003-05-17 17:35:10 +00002382 assert( i>=0 && i<db->nDb );
danielk19771d850a72004-05-31 08:26:49 +00002383 pBt = db->aDb[i].pBt;
2384
danielk197724162fe2004-06-04 06:22:00 +00002385 if( pBt ){
danielk197740b38dc2004-06-26 08:38:24 +00002386 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
danielk197724162fe2004-06-04 06:22:00 +00002387 if( rc==SQLITE_BUSY ){
danielk19772a764eb2004-06-12 01:43:26 +00002388 p->pc = pc;
2389 p->rc = SQLITE_BUSY;
2390 p->pTos = pTos;
2391 return SQLITE_BUSY;
danielk197724162fe2004-06-04 06:22:00 +00002392 }
danielk19772a764eb2004-06-12 01:43:26 +00002393 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
danielk197724162fe2004-06-04 06:22:00 +00002394 goto abort_due_to_error;
drh90bfcda2001-09-23 19:46:51 +00002395 }
drhb86ccfb2003-01-28 23:13:10 +00002396 }
drh5e00f6c2001-09-13 13:46:56 +00002397 break;
2398}
2399
drh001bbcb2003-03-19 03:14:00 +00002400/* Opcode: ReadCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002401**
drh001bbcb2003-03-19 03:14:00 +00002402** Read cookie number P2 from database P1 and push it onto the stack.
2403** P2==0 is the schema version. P2==1 is the database format.
2404** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2405** the main database file and P1==1 is the database file used to store
2406** temporary tables.
drh4a324312001-12-21 14:30:42 +00002407**
drh50e5dad2001-09-15 00:57:28 +00002408** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002409** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002410** executing this instruction.
2411*/
2412case OP_ReadCookie: {
drhf328bc82004-05-10 23:29:49 +00002413 int iMeta;
drh4a324312001-12-21 14:30:42 +00002414 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002415 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2416 assert( db->aDb[pOp->p1].pBt!=0 );
drha3b321d2004-05-11 09:31:31 +00002417 /* The indexing of meta values at the schema layer is off by one from
2418 ** the indexing in the btree layer. The btree considers meta[0] to
2419 ** be the number of free pages in the database (a read-only value)
2420 ** and meta[1] to be the schema cookie. The schema layer considers
2421 ** meta[1] to be the schema cookie. So we have to shift the index
2422 ** by one in the following statement.
2423 */
danielk1977e0d4b062004-06-28 01:11:46 +00002424 rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, (u32 *)&iMeta);
drh6810ce62004-01-31 19:22:56 +00002425 pTos++;
drhf328bc82004-05-10 23:29:49 +00002426 pTos->i = iMeta;
drh6810ce62004-01-31 19:22:56 +00002427 pTos->flags = MEM_Int;
drh50e5dad2001-09-15 00:57:28 +00002428 break;
2429}
2430
drh001bbcb2003-03-19 03:14:00 +00002431/* Opcode: SetCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002432**
drh001bbcb2003-03-19 03:14:00 +00002433** Write the top of the stack into cookie number P2 of database P1.
2434** P2==0 is the schema version. P2==1 is the database format.
2435** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2436** the main database file and P1==1 is the database file used to store
2437** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002438**
2439** A transaction must be started before executing this opcode.
2440*/
danielk19777a5147c2005-03-29 13:07:00 +00002441case OP_SetCookie: { /* no-push */
drh3f7d4e42004-07-24 14:35:58 +00002442 Db *pDb;
drh4a324312001-12-21 14:30:42 +00002443 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002444 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drh3f7d4e42004-07-24 14:35:58 +00002445 pDb = &db->aDb[pOp->p1];
2446 assert( pDb->pBt!=0 );
drh6810ce62004-01-31 19:22:56 +00002447 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00002448 sqlite3VdbeMemIntegerify(pTos);
drha3b321d2004-05-11 09:31:31 +00002449 /* See note about index shifting on OP_ReadCookie */
drh3f7d4e42004-07-24 14:35:58 +00002450 rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->i);
2451 if( pOp->p2==0 ){
2452 /* When the schema cookie changes, record the new cookie internally */
danielk1977da184232006-01-05 11:34:32 +00002453 pDb->pSchema->schema_cookie = pTos->i;
drh3f7d4e42004-07-24 14:35:58 +00002454 db->flags |= SQLITE_InternChanges;
drhd28bcb32005-12-21 14:43:11 +00002455 }else if( pOp->p2==1 ){
2456 /* Record changes in the file format */
danielk1977da184232006-01-05 11:34:32 +00002457 pDb->pSchema->file_format = pTos->i;
drh3f7d4e42004-07-24 14:35:58 +00002458 }
drh6a6124e2004-06-27 01:56:33 +00002459 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00002460 pTos--;
drhfd426c62006-01-30 15:34:22 +00002461 if( pOp->p1==1 ){
2462 /* Invalidate all prepared statements whenever the TEMP database
2463 ** schema is changed. Ticket #1644 */
2464 sqlite3ExpirePreparedStatements(db);
2465 }
drh50e5dad2001-09-15 00:57:28 +00002466 break;
2467}
2468
drh4a324312001-12-21 14:30:42 +00002469/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002470**
drh001bbcb2003-03-19 03:14:00 +00002471** Check the value of global database parameter number 0 (the
2472** schema version) and make sure it is equal to P2.
2473** P1 is the database number which is 0 for the main database file
2474** and 1 for the file holding temporary tables and some higher number
2475** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002476**
2477** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002478** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002479** and that the current process needs to reread the schema.
2480**
2481** Either a transaction needs to have been started or an OP_Open needs
2482** to be executed (to establish a read lock) before this opcode is
2483** invoked.
2484*/
danielk19777a5147c2005-03-29 13:07:00 +00002485case OP_VerifyCookie: { /* no-push */
drhf328bc82004-05-10 23:29:49 +00002486 int iMeta;
drhc275b4e2004-07-19 17:25:24 +00002487 Btree *pBt;
drh001bbcb2003-03-19 03:14:00 +00002488 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhc275b4e2004-07-19 17:25:24 +00002489 pBt = db->aDb[pOp->p1].pBt;
2490 if( pBt ){
2491 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2492 }else{
2493 rc = SQLITE_OK;
2494 iMeta = 0;
2495 }
drhf328bc82004-05-10 23:29:49 +00002496 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
danielk19774adee202004-05-08 08:23:19 +00002497 sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
drh50e5dad2001-09-15 00:57:28 +00002498 rc = SQLITE_SCHEMA;
2499 }
2500 break;
2501}
2502
drh001bbcb2003-03-19 03:14:00 +00002503/* Opcode: OpenRead P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00002504**
drhecdc7532001-09-23 02:35:53 +00002505** Open a read-only cursor for the database table whose root page is
drh001bbcb2003-03-19 03:14:00 +00002506** P2 in a database file. The database file is determined by an
2507** integer from the top of the stack. 0 means the main database and
2508** 1 means the database used for temporary tables. Give the new
2509** cursor an identifier of P1. The P1 values need not be contiguous
2510** but all P1 values should be small integers. It is an error for
2511** P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002512**
drh001bbcb2003-03-19 03:14:00 +00002513** If P2==0 then take the root page number from the next of the stack.
drh5edc3122001-09-13 21:53:09 +00002514**
drhb19a2bc2001-09-16 00:13:26 +00002515** There will be a read lock on the database whenever there is an
2516** open cursor. If the database was unlocked prior to this instruction
2517** then a read lock is acquired as part of this instruction. A read
2518** lock allows other processes to read the database but prohibits
2519** any other process from modifying the database. The read lock is
2520** released when all cursors are closed. If this instruction attempts
2521** to get a read lock but fails, the script terminates with an
2522** SQLITE_BUSY error code.
2523**
drhd3d39e92004-05-20 22:16:29 +00002524** The P3 value is a pointer to a KeyInfo structure that defines the
2525** content and collating sequence of indices. P3 is NULL for cursors
2526** that are not pointing to indices.
drhf57b3392001-10-08 13:22:32 +00002527**
drh001bbcb2003-03-19 03:14:00 +00002528** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002529*/
drhecdc7532001-09-23 02:35:53 +00002530/* Opcode: OpenWrite P1 P2 P3
2531**
2532** Open a read/write cursor named P1 on the table or index whose root
2533** page is P2. If P2==0 then take the root page number from the stack.
2534**
drhd3d39e92004-05-20 22:16:29 +00002535** The P3 value is a pointer to a KeyInfo structure that defines the
2536** content and collating sequence of indices. P3 is NULL for cursors
2537** that are not pointing to indices.
jplyon5a564222003-06-02 06:15:58 +00002538**
drh001bbcb2003-03-19 03:14:00 +00002539** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002540** in read/write mode. For a given table, there can be one or more read-only
2541** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002542**
drh001bbcb2003-03-19 03:14:00 +00002543** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002544*/
danielk19777a5147c2005-03-29 13:07:00 +00002545case OP_OpenRead: /* no-push */
2546case OP_OpenWrite: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00002547 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002548 int p2 = pOp->p2;
drhf57b3392001-10-08 13:22:32 +00002549 int wrFlag;
2550 Btree *pX;
drh001bbcb2003-03-19 03:14:00 +00002551 int iDb;
drhf328bc82004-05-10 23:29:49 +00002552 Cursor *pCur;
drhd946db02005-12-29 19:23:06 +00002553 Db *pDb;
drh001bbcb2003-03-19 03:14:00 +00002554
drh6810ce62004-01-31 19:22:56 +00002555 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00002556 sqlite3VdbeMemIntegerify(pTos);
drh6810ce62004-01-31 19:22:56 +00002557 iDb = pTos->i;
drh6a6124e2004-06-27 01:56:33 +00002558 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00002559 pTos--;
2560 assert( iDb>=0 && iDb<db->nDb );
drhd946db02005-12-29 19:23:06 +00002561 pDb = &db->aDb[iDb];
2562 pX = pDb->pBt;
drh6810ce62004-01-31 19:22:56 +00002563 assert( pX!=0 );
drhd946db02005-12-29 19:23:06 +00002564 if( pOp->opcode==OP_OpenWrite ){
2565 wrFlag = 1;
danielk1977da184232006-01-05 11:34:32 +00002566 if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2567 p->minWriteFileFormat = pDb->pSchema->file_format;
drhd946db02005-12-29 19:23:06 +00002568 }
2569 }else{
2570 wrFlag = 0;
2571 }
drh5edc3122001-09-13 21:53:09 +00002572 if( p2<=0 ){
drh6810ce62004-01-31 19:22:56 +00002573 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00002574 sqlite3VdbeMemIntegerify(pTos);
drh6810ce62004-01-31 19:22:56 +00002575 p2 = pTos->i;
drh6a6124e2004-06-27 01:56:33 +00002576 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00002577 pTos--;
drha6110402005-07-28 20:51:19 +00002578 assert( p2>=2 );
drh5edc3122001-09-13 21:53:09 +00002579 }
drh6810ce62004-01-31 19:22:56 +00002580 assert( i>=0 );
danielk197794eb6a12005-12-15 15:22:08 +00002581 pCur = allocateCursor(p, i, iDb);
drh4774b132004-06-12 20:12:51 +00002582 if( pCur==0 ) goto no_mem;
drhf328bc82004-05-10 23:29:49 +00002583 pCur->nullRow = 1;
drhe0bc4042002-06-25 01:09:11 +00002584 if( pX==0 ) break;
danielk197724162fe2004-06-04 06:22:00 +00002585 /* We always provide a key comparison function. If the table being
2586 ** opened is of type INTKEY, the comparision function will be ignored. */
2587 rc = sqlite3BtreeCursor(pX, p2, wrFlag,
2588 sqlite3VdbeRecordCompare, pOp->p3,
2589 &pCur->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002590 if( pOp->p3type==P3_KEYINFO ){
2591 pCur->pKeyInfo = (KeyInfo*)pOp->p3;
danielk197724162fe2004-06-04 06:22:00 +00002592 pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
danielk197714db2662006-01-09 16:12:04 +00002593 pCur->pKeyInfo->enc = ENC(p->db);
danielk197724162fe2004-06-04 06:22:00 +00002594 }else{
drhf0863fe2005-06-12 21:35:51 +00002595 pCur->pKeyInfo = 0;
danielk197724162fe2004-06-04 06:22:00 +00002596 pCur->pIncrKey = &pCur->bogusIncrKey;
2597 }
2598 switch( rc ){
2599 case SQLITE_BUSY: {
danielk19772a764eb2004-06-12 01:43:26 +00002600 p->pc = pc;
2601 p->rc = SQLITE_BUSY;
2602 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
2603 return SQLITE_BUSY;
drhd3d39e92004-05-20 22:16:29 +00002604 }
danielk197724162fe2004-06-04 06:22:00 +00002605 case SQLITE_OK: {
2606 int flags = sqlite3BtreeFlags(pCur->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002607 /* Sanity checking. Only the lower four bits of the flags byte should
2608 ** be used. Bit 3 (mask 0x08) is unpreditable. The lower 3 bits
2609 ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2610 ** 2 (zerodata for indices). If these conditions are not met it can
2611 ** only mean that we are dealing with a corrupt database file
2612 */
2613 if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
drh49285702005-09-17 15:20:26 +00002614 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002615 goto abort_due_to_error;
2616 }
2617 pCur->isTable = (flags & BTREE_INTKEY)!=0;
2618 pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
2619 /* If P3==0 it means we are expected to open a table. If P3!=0 then
2620 ** we expect to be opening an index. If this is not what happened,
2621 ** then the database is corrupt
2622 */
2623 if( (pCur->isTable && pOp->p3type==P3_KEYINFO)
2624 || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){
drh49285702005-09-17 15:20:26 +00002625 rc = SQLITE_CORRUPT_BKPT;
drhf0863fe2005-06-12 21:35:51 +00002626 goto abort_due_to_error;
2627 }
danielk197724162fe2004-06-04 06:22:00 +00002628 break;
drh5e00f6c2001-09-13 13:46:56 +00002629 }
danielk197724162fe2004-06-04 06:22:00 +00002630 case SQLITE_EMPTY: {
drhf0863fe2005-06-12 21:35:51 +00002631 pCur->isTable = pOp->p3type!=P3_KEYINFO;
2632 pCur->isIndex = !pCur->isTable;
danielk197724162fe2004-06-04 06:22:00 +00002633 rc = SQLITE_OK;
2634 break;
2635 }
2636 default: {
2637 goto abort_due_to_error;
2638 }
2639 }
drh5e00f6c2001-09-13 13:46:56 +00002640 break;
2641}
2642
drhb9bb7c12006-06-11 23:41:55 +00002643/* Opcode: OpenEphemeral P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00002644**
drhb9bb7c12006-06-11 23:41:55 +00002645** Open a new cursor P1 to a transient table.
drh9170dd72005-07-08 17:13:46 +00002646** The cursor is always opened read/write even if
2647** the main database is read-only. The transient or virtual
2648** table is deleted automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002649**
drh0342b1f2005-09-01 03:07:44 +00002650** P2 is the number of columns in the virtual table.
drhd3d39e92004-05-20 22:16:29 +00002651** The cursor points to a BTree table if P3==0 and to a BTree index
2652** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure
2653** that defines the format of keys in the index.
drhb9bb7c12006-06-11 23:41:55 +00002654**
2655** This opcode was once called OpenTemp. But that created
2656** confusion because the term "temp table", might refer either
2657** to a TEMP table at the SQL level, or to a table opened by
2658** this opcode. Then this opcode was call OpenVirtual. But
2659** that created confusion with the whole virtual-table idea.
drh5e00f6c2001-09-13 13:46:56 +00002660*/
drhb9bb7c12006-06-11 23:41:55 +00002661case OP_OpenEphemeral: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00002662 int i = pOp->p1;
2663 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002664 assert( i>=0 );
danielk197794eb6a12005-12-15 15:22:08 +00002665 pCx = allocateCursor(p, i, -1);
drh4774b132004-06-12 20:12:51 +00002666 if( pCx==0 ) goto no_mem;
drh17f71932002-02-21 12:01:27 +00002667 pCx->nullRow = 1;
danielk19774adee202004-05-08 08:23:19 +00002668 rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002669 if( rc==SQLITE_OK ){
danielk197740b38dc2004-06-26 08:38:24 +00002670 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
drh5e00f6c2001-09-13 13:46:56 +00002671 }
2672 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002673 /* If a transient index is required, create it by calling
2674 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2675 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002676 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002677 */
drhd3d39e92004-05-20 22:16:29 +00002678 if( pOp->p3 ){
drhc6b52df2002-01-04 03:09:29 +00002679 int pgno;
drhd3d39e92004-05-20 22:16:29 +00002680 assert( pOp->p3type==P3_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00002681 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002682 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002683 assert( pgno==MASTER_ROOT+1 );
drh7a224de2004-06-02 01:22:02 +00002684 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare,
drhd3d39e92004-05-20 22:16:29 +00002685 pOp->p3, &pCx->pCursor);
2686 pCx->pKeyInfo = (KeyInfo*)pOp->p3;
danielk197714db2662006-01-09 16:12:04 +00002687 pCx->pKeyInfo->enc = ENC(p->db);
drhd3d39e92004-05-20 22:16:29 +00002688 pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
drhc6b52df2002-01-04 03:09:29 +00002689 }
drhf0863fe2005-06-12 21:35:51 +00002690 pCx->isTable = 0;
drhc6b52df2002-01-04 03:09:29 +00002691 }else{
drhf328bc82004-05-10 23:29:49 +00002692 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
drhf0863fe2005-06-12 21:35:51 +00002693 pCx->isTable = 1;
drhd3d39e92004-05-20 22:16:29 +00002694 pCx->pIncrKey = &pCx->bogusIncrKey;
drhc6b52df2002-01-04 03:09:29 +00002695 }
drh5e00f6c2001-09-13 13:46:56 +00002696 }
drh0342b1f2005-09-01 03:07:44 +00002697 pCx->nField = pOp->p2;
drhf0863fe2005-06-12 21:35:51 +00002698 pCx->isIndex = !pCx->isTable;
drh5e00f6c2001-09-13 13:46:56 +00002699 break;
2700}
2701
drh70ce3f02003-04-15 19:22:22 +00002702/* Opcode: OpenPseudo P1 * *
2703**
2704** Open a new cursor that points to a fake table that contains a single
2705** row of data. Any attempt to write a second row of data causes the
2706** first row to be deleted. All data is deleted when the cursor is
2707** closed.
2708**
2709** A pseudo-table created by this opcode is useful for holding the
drhcdd536f2006-03-17 00:04:03 +00002710** NEW or OLD tables in a trigger. Also used to hold the a single
2711** row output from the sorter so that the row can be decomposed into
2712** individual columns using the OP_Column opcode.
drh70ce3f02003-04-15 19:22:22 +00002713*/
danielk19777a5147c2005-03-29 13:07:00 +00002714case OP_OpenPseudo: { /* no-push */
drh70ce3f02003-04-15 19:22:22 +00002715 int i = pOp->p1;
2716 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002717 assert( i>=0 );
danielk197794eb6a12005-12-15 15:22:08 +00002718 pCx = allocateCursor(p, i, -1);
drh4774b132004-06-12 20:12:51 +00002719 if( pCx==0 ) goto no_mem;
drh70ce3f02003-04-15 19:22:22 +00002720 pCx->nullRow = 1;
2721 pCx->pseudoTable = 1;
drhd3d39e92004-05-20 22:16:29 +00002722 pCx->pIncrKey = &pCx->bogusIncrKey;
drhf0863fe2005-06-12 21:35:51 +00002723 pCx->isTable = 1;
2724 pCx->isIndex = 0;
drh70ce3f02003-04-15 19:22:22 +00002725 break;
2726}
2727
drh5e00f6c2001-09-13 13:46:56 +00002728/* Opcode: Close P1 * *
2729**
2730** Close a cursor previously opened as P1. If P1 is not
2731** currently open, this instruction is a no-op.
2732*/
danielk19777a5147c2005-03-29 13:07:00 +00002733case OP_Close: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00002734 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00002735 if( i>=0 && i<p->nCursor ){
drh4774b132004-06-12 20:12:51 +00002736 sqlite3VdbeFreeCursor(p->apCsr[i]);
2737 p->apCsr[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00002738 }
2739 break;
2740}
2741
drh7cf6e4d2004-05-19 14:56:55 +00002742/* Opcode: MoveGe P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002743**
2744** Pop the top of the stack and use its value as a key. Reposition
drh7cf6e4d2004-05-19 14:56:55 +00002745** cursor P1 so that it points to the smallest entry that is greater
2746** than or equal to the key that was popped ffrom the stack.
2747** If there are no records greater than or equal to the key and P2
2748** is not zero, then jump to P2.
2749**
2750** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2751*/
2752/* Opcode: MoveGt P1 P2 *
2753**
2754** Pop the top of the stack and use its value as a key. Reposition
2755** cursor P1 so that it points to the smallest entry that is greater
2756** than the key from the stack.
drh8721ce42001-11-07 14:22:00 +00002757** If there are no records greater than the key and P2 is not zero,
drh7cf6e4d2004-05-19 14:56:55 +00002758** then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00002759**
drh7cf6e4d2004-05-19 14:56:55 +00002760** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
drh5e00f6c2001-09-13 13:46:56 +00002761*/
drhc045ec52002-12-04 20:01:06 +00002762/* Opcode: MoveLt P1 P2 *
2763**
2764** Pop the top of the stack and use its value as a key. Reposition
drh7cf6e4d2004-05-19 14:56:55 +00002765** cursor P1 so that it points to the largest entry that is less
2766** than the key from the stack.
2767** If there are no records less than the key and P2 is not zero,
2768** then jump to P2.
drhc045ec52002-12-04 20:01:06 +00002769**
drh7cf6e4d2004-05-19 14:56:55 +00002770** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2771*/
2772/* Opcode: MoveLe P1 P2 *
danielk19773d1bfea2004-05-14 11:00:53 +00002773**
drh7cf6e4d2004-05-19 14:56:55 +00002774** Pop the top of the stack and use its value as a key. Reposition
2775** cursor P1 so that it points to the largest entry that is less than
2776** or equal to the key that was popped from the stack.
2777** If there are no records less than or eqal to the key and P2 is not zero,
2778** then jump to P2.
2779**
2780** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
drhc045ec52002-12-04 20:01:06 +00002781*/
danielk19777a5147c2005-03-29 13:07:00 +00002782case OP_MoveLt: /* no-push */
2783case OP_MoveLe: /* no-push */
2784case OP_MoveGe: /* no-push */
2785case OP_MoveGt: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00002786 int i = pOp->p1;
drh80ff32f2001-11-04 18:32:46 +00002787 Cursor *pC;
2788
drh6810ce62004-01-31 19:22:56 +00002789 assert( pTos>=p->aStack );
drh70ce3f02003-04-15 19:22:22 +00002790 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002791 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00002792 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00002793 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00002794 int res, oc;
drh7cf6e4d2004-05-19 14:56:55 +00002795 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00002796 pC->nullRow = 0;
drhd3d39e92004-05-20 22:16:29 +00002797 *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
drhf0863fe2005-06-12 21:35:51 +00002798 if( pC->isTable ){
drhf328bc82004-05-10 23:29:49 +00002799 i64 iKey;
drh8a512562005-11-14 22:29:05 +00002800 sqlite3VdbeMemIntegerify(pTos);
drhf328bc82004-05-10 23:29:49 +00002801 iKey = intToKey(pTos->i);
drh7cf6e4d2004-05-19 14:56:55 +00002802 if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
drha11846b2004-01-07 18:52:56 +00002803 pC->movetoTarget = iKey;
2804 pC->deferredMoveto = 1;
drh6a6124e2004-06-27 01:56:33 +00002805 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00002806 pTos--;
drha11846b2004-01-07 18:52:56 +00002807 break;
2808 }
danielk197728129562005-01-11 10:25:06 +00002809 rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res);
2810 if( rc!=SQLITE_OK ){
2811 goto abort_due_to_error;
2812 }
drhf0863fe2005-06-12 21:35:51 +00002813 pC->lastRowid = pTos->i;
2814 pC->rowidIsValid = res==0;
drh5e00f6c2001-09-13 13:46:56 +00002815 }else{
drh0b2f3162005-12-21 18:36:45 +00002816 assert( pTos->flags & MEM_Blob );
drh8079a0d2006-01-12 17:20:50 +00002817 /* Stringify(pTos, encoding); */
danielk197728129562005-01-11 10:25:06 +00002818 rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2819 if( rc!=SQLITE_OK ){
2820 goto abort_due_to_error;
2821 }
drhf0863fe2005-06-12 21:35:51 +00002822 pC->rowidIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002823 }
drha11846b2004-01-07 18:52:56 +00002824 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002825 pC->cacheStatus = CACHE_STALE;
drhd3d39e92004-05-20 22:16:29 +00002826 *pC->pIncrKey = 0;
danielk19776f8a5032004-05-10 10:34:51 +00002827 sqlite3_search_count++;
drh7cf6e4d2004-05-19 14:56:55 +00002828 if( oc==OP_MoveGe || oc==OP_MoveGt ){
2829 if( res<0 ){
danielk197728129562005-01-11 10:25:06 +00002830 rc = sqlite3BtreeNext(pC->pCursor, &res);
danielk197701427a62005-01-11 13:02:33 +00002831 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00002832 pC->rowidIsValid = 0;
drh1af3fdb2004-07-18 21:33:01 +00002833 }else{
2834 res = 0;
drh8721ce42001-11-07 14:22:00 +00002835 }
drh7cf6e4d2004-05-19 14:56:55 +00002836 }else{
2837 assert( oc==OP_MoveLt || oc==OP_MoveLe );
drh1a844c32002-12-04 22:29:28 +00002838 if( res>=0 ){
danielk197701427a62005-01-11 13:02:33 +00002839 rc = sqlite3BtreePrevious(pC->pCursor, &res);
2840 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf0863fe2005-06-12 21:35:51 +00002841 pC->rowidIsValid = 0;
drh1a844c32002-12-04 22:29:28 +00002842 }else{
2843 /* res might be negative because the table is empty. Check to
2844 ** see if this is the case.
2845 */
drhf328bc82004-05-10 23:29:49 +00002846 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00002847 }
drh1af3fdb2004-07-18 21:33:01 +00002848 }
2849 if( res ){
2850 if( pOp->p2>0 ){
drhc045ec52002-12-04 20:01:06 +00002851 pc = pOp->p2 - 1;
drh1af3fdb2004-07-18 21:33:01 +00002852 }else{
2853 pC->nullRow = 1;
drhc045ec52002-12-04 20:01:06 +00002854 }
drh8721ce42001-11-07 14:22:00 +00002855 }
drh5e00f6c2001-09-13 13:46:56 +00002856 }
drh6810ce62004-01-31 19:22:56 +00002857 Release(pTos);
2858 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002859 break;
2860}
2861
drh5e00f6c2001-09-13 13:46:56 +00002862/* Opcode: Distinct P1 P2 *
2863**
drhf0863fe2005-06-12 21:35:51 +00002864** Use the top of the stack as a record created using MakeRecord. P1 is a
2865** cursor on a table that declared as an index. If that table contains an
2866** entry that matches the top of the stack fall thru. If the top of the stack
2867** matches no entry in P1 then jump to P2.
drh5e00f6c2001-09-13 13:46:56 +00002868**
drhf0863fe2005-06-12 21:35:51 +00002869** The cursor is left pointing at the matching entry if it exists. The
2870** record on the top of the stack is not popped.
2871**
2872** This instruction is similar to NotFound except that this operation
drh5e00f6c2001-09-13 13:46:56 +00002873** does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002874**
drhf0863fe2005-06-12 21:35:51 +00002875** The instruction is used to implement the DISTINCT operator on SELECT
2876** statements. The P1 table is not a true index but rather a record of
2877** all results that have produced so far.
2878**
drhd99f7062002-06-08 23:25:08 +00002879** See also: Found, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002880*/
2881/* Opcode: Found P1 P2 *
2882**
drhf0863fe2005-06-12 21:35:51 +00002883** Top of the stack holds a blob constructed by MakeRecord. P1 is an index.
2884** If an entry that matches the top of the stack exists in P1 then
2885** jump to P2. If the top of the stack does not match any entry in P1
2886** then fall thru. The P1 cursor is left pointing at the matching entry
2887** if it exists. The blob is popped off the top of the stack.
2888**
2889** This instruction is used to implement the IN operator where the
2890** left-hand side is a SELECT statement. P1 is not a true index but
2891** is instead a temporary index that holds the results of the SELECT
2892** statement. This instruction just checks to see if the left-hand side
2893** of the IN operator (stored on the top of the stack) exists in the
2894** result of the SELECT statement.
drhb19a2bc2001-09-16 00:13:26 +00002895**
drhd99f7062002-06-08 23:25:08 +00002896** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002897*/
2898/* Opcode: NotFound P1 P2 *
2899**
drhf0863fe2005-06-12 21:35:51 +00002900** The top of the stack holds a blob constructed by MakeRecord. P1 is
2901** an index. If no entry exists in P1 that matches the blob then jump
2902** to P1. If an entry does existing, fall through. The cursor is left
2903** pointing to the entry that matches. The blob is popped from the stack.
drh5e00f6c2001-09-13 13:46:56 +00002904**
2905** The difference between this operation and Distinct is that
2906** Distinct does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002907**
drh9cfcf5d2002-01-29 18:41:24 +00002908** See also: Distinct, Found, MoveTo, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00002909*/
danielk19777a5147c2005-03-29 13:07:00 +00002910case OP_Distinct: /* no-push */
2911case OP_NotFound: /* no-push */
2912case OP_Found: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00002913 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00002914 int alreadyExists = 0;
drh80ff32f2001-11-04 18:32:46 +00002915 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00002916 assert( pTos>=p->aStack );
2917 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00002918 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00002919 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh5e00f6c2001-09-13 13:46:56 +00002920 int res, rx;
drhf0863fe2005-06-12 21:35:51 +00002921 assert( pC->isTable==0 );
drh8079a0d2006-01-12 17:20:50 +00002922 Stringify(pTos, encoding);
danielk19774adee202004-05-08 08:23:19 +00002923 rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
drh5e00f6c2001-09-13 13:46:56 +00002924 alreadyExists = rx==SQLITE_OK && res==0;
drha11846b2004-01-07 18:52:56 +00002925 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00002926 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00002927 }
2928 if( pOp->opcode==OP_Found ){
2929 if( alreadyExists ) pc = pOp->p2 - 1;
2930 }else{
2931 if( !alreadyExists ) pc = pOp->p2 - 1;
2932 }
2933 if( pOp->opcode!=OP_Distinct ){
drh6810ce62004-01-31 19:22:56 +00002934 Release(pTos);
2935 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002936 }
2937 break;
2938}
2939
drh9cfcf5d2002-01-29 18:41:24 +00002940/* Opcode: IsUnique P1 P2 *
2941**
drh0ca3e242002-01-29 23:07:02 +00002942** The top of the stack is an integer record number. Call this
2943** record number R. The next on the stack is an index key created
drh3c899a62006-01-10 18:44:08 +00002944** using MakeIdxRec. Call it K. This instruction pops R from the
drh0ca3e242002-01-29 23:07:02 +00002945** stack but it leaves K unchanged.
drh9cfcf5d2002-01-29 18:41:24 +00002946**
drh7cf6e4d2004-05-19 14:56:55 +00002947** P1 is an index. So it has no data and its key consists of a
drhf0863fe2005-06-12 21:35:51 +00002948** record generated by OP_MakeRecord where the last field is the
2949** rowid of the entry that the index refers to.
drhf3218fe2004-05-28 08:21:02 +00002950**
drh0ca3e242002-01-29 23:07:02 +00002951** This instruction asks if there is an entry in P1 where the
drh7cf6e4d2004-05-19 14:56:55 +00002952** fields matches K but the rowid is different from R.
2953** If there is no such entry, then there is an immediate
drh0ca3e242002-01-29 23:07:02 +00002954** jump to P2. If any entry does exist where the index string
2955** matches K but the record number is not R, then the record
2956** number for that entry is pushed onto the stack and control
2957** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00002958**
drhd99f7062002-06-08 23:25:08 +00002959** See also: Distinct, NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00002960*/
danielk19777a5147c2005-03-29 13:07:00 +00002961case OP_IsUnique: { /* no-push */
drh9cfcf5d2002-01-29 18:41:24 +00002962 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00002963 Mem *pNos = &pTos[-1];
drhf328bc82004-05-10 23:29:49 +00002964 Cursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00002965 BtCursor *pCrsr;
danielk1977452c9892004-05-13 05:16:15 +00002966 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00002967
drh0ca3e242002-01-29 23:07:02 +00002968 /* Pop the value R off the top of the stack
2969 */
drh6810ce62004-01-31 19:22:56 +00002970 assert( pNos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00002971 sqlite3VdbeMemIntegerify(pTos);
drh6810ce62004-01-31 19:22:56 +00002972 R = pTos->i;
drh6a6124e2004-06-27 01:56:33 +00002973 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00002974 pTos--;
2975 assert( i>=0 && i<=p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002976 pCx = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00002977 assert( pCx!=0 );
drhf328bc82004-05-10 23:29:49 +00002978 pCrsr = pCx->pCursor;
2979 if( pCrsr!=0 ){
danielk1977f2fa8312006-01-24 13:09:33 +00002980 int res;
danielk1977452c9892004-05-13 05:16:15 +00002981 i64 v; /* The record number on the P1 entry that matches K */
drh0ca3e242002-01-29 23:07:02 +00002982 char *zKey; /* The value of K */
2983 int nKey; /* Number of bytes in K */
danielk1977452c9892004-05-13 05:16:15 +00002984 int len; /* Number of bytes in K without the rowid at the end */
drhf3218fe2004-05-28 08:21:02 +00002985 int szRowid; /* Size of the rowid column at the end of zKey */
drh0ca3e242002-01-29 23:07:02 +00002986
2987 /* Make sure K is a string and make zKey point to K
2988 */
drh8079a0d2006-01-12 17:20:50 +00002989 Stringify(pNos, encoding);
drh6810ce62004-01-31 19:22:56 +00002990 zKey = pNos->z;
2991 nKey = pNos->n;
danielk1977452c9892004-05-13 05:16:15 +00002992
drh74161702006-02-24 02:53:49 +00002993 szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey);
drhf3218fe2004-05-28 08:21:02 +00002994 len = nKey-szRowid;
drh0ca3e242002-01-29 23:07:02 +00002995
drha3b321d2004-05-11 09:31:31 +00002996 /* Search for an entry in P1 where all but the last four bytes match K.
drh0ca3e242002-01-29 23:07:02 +00002997 ** If there is no such entry, jump immediately to P2.
2998 */
drh9188b382004-05-14 21:12:22 +00002999 assert( pCx->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003000 pCx->cacheStatus = CACHE_STALE;
danielk1977452c9892004-05-13 05:16:15 +00003001 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
danielk1977f0113002006-01-24 12:09:17 +00003002 if( rc!=SQLITE_OK ){
3003 goto abort_due_to_error;
3004 }
drh9cfcf5d2002-01-29 18:41:24 +00003005 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003006 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00003007 if( res ){
3008 pc = pOp->p2 - 1;
3009 break;
3010 }
3011 }
drh2646da72005-12-09 20:02:05 +00003012 rc = sqlite3VdbeIdxKeyCompare(pCx, len, (u8*)zKey, &res);
drh9cfcf5d2002-01-29 18:41:24 +00003013 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3014 if( res>0 ){
3015 pc = pOp->p2 - 1;
3016 break;
3017 }
drh0ca3e242002-01-29 23:07:02 +00003018
3019 /* At this point, pCrsr is pointing to an entry in P1 where all but
drhf3218fe2004-05-28 08:21:02 +00003020 ** the final entry (the rowid) matches K. Check to see if the
3021 ** final rowid column is different from R. If it equals R then jump
danielk1977452c9892004-05-13 05:16:15 +00003022 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00003023 */
danielk1977452c9892004-05-13 05:16:15 +00003024 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
3025 if( rc!=SQLITE_OK ){
3026 goto abort_due_to_error;
3027 }
drh0ca3e242002-01-29 23:07:02 +00003028 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003029 pc = pOp->p2 - 1;
3030 break;
3031 }
drh0ca3e242002-01-29 23:07:02 +00003032
danielk1977452c9892004-05-13 05:16:15 +00003033 /* The final varint of the key is different from R. Push it onto
3034 ** the stack. (The record number of an entry that violates a UNIQUE
3035 ** constraint.)
drh0ca3e242002-01-29 23:07:02 +00003036 */
drh6810ce62004-01-31 19:22:56 +00003037 pTos++;
3038 pTos->i = v;
3039 pTos->flags = MEM_Int;
drh9cfcf5d2002-01-29 18:41:24 +00003040 }
3041 break;
3042}
3043
drh6b125452002-01-28 15:53:03 +00003044/* Opcode: NotExists P1 P2 *
3045**
3046** Use the top of the stack as a integer key. If a record with that key
3047** does not exist in table of P1, then jump to P2. If the record
3048** does exist, then fall thru. The cursor is left pointing to the
3049** record if it exists. The integer key is popped from the stack.
3050**
3051** The difference between this operation and NotFound is that this
drhf0863fe2005-06-12 21:35:51 +00003052** operation assumes the key is an integer and that P1 is a table whereas
3053** NotFound assumes key is a blob constructed from MakeRecord and
3054** P1 is an index.
drh6b125452002-01-28 15:53:03 +00003055**
drhd99f7062002-06-08 23:25:08 +00003056** See also: Distinct, Found, MoveTo, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003057*/
danielk19777a5147c2005-03-29 13:07:00 +00003058case OP_NotExists: { /* no-push */
drh6b125452002-01-28 15:53:03 +00003059 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003060 Cursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003061 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003062 assert( pTos>=p->aStack );
3063 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003064 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003065 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19771400b522005-01-11 11:08:22 +00003066 int res;
danielk197736a3c702004-05-11 06:55:14 +00003067 u64 iKey;
drh6810ce62004-01-31 19:22:56 +00003068 assert( pTos->flags & MEM_Int );
drhf0863fe2005-06-12 21:35:51 +00003069 assert( p->apCsr[i]->isTable );
drh6810ce62004-01-31 19:22:56 +00003070 iKey = intToKey(pTos->i);
danielk197728129562005-01-11 10:25:06 +00003071 rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res);
drhf0863fe2005-06-12 21:35:51 +00003072 pC->lastRowid = pTos->i;
3073 pC->rowidIsValid = res==0;
drh9188b382004-05-14 21:12:22 +00003074 pC->nullRow = 0;
drh76873ab2006-01-07 18:48:26 +00003075 pC->cacheStatus = CACHE_STALE;
danielk197728129562005-01-11 10:25:06 +00003076 if( res!=0 ){
drh17f71932002-02-21 12:01:27 +00003077 pc = pOp->p2 - 1;
drhf0863fe2005-06-12 21:35:51 +00003078 pC->rowidIsValid = 0;
drh6b125452002-01-28 15:53:03 +00003079 }
3080 }
drh6810ce62004-01-31 19:22:56 +00003081 Release(pTos);
3082 pTos--;
drh6b125452002-01-28 15:53:03 +00003083 break;
3084}
3085
drh4db38a72005-09-01 12:16:28 +00003086/* Opcode: Sequence P1 * *
3087**
3088** Push an integer onto the stack which is the next available
3089** sequence number for cursor P1. The sequence number on the
3090** cursor is incremented after the push.
3091*/
3092case OP_Sequence: {
3093 int i = pOp->p1;
3094 assert( pTos>=p->aStack );
3095 assert( i>=0 && i<p->nCursor );
3096 assert( p->apCsr[i]!=0 );
3097 pTos++;
3098 pTos->i = p->apCsr[i]->seqCount++;
3099 pTos->flags = MEM_Int;
3100 break;
3101}
3102
3103
drhf0863fe2005-06-12 21:35:51 +00003104/* Opcode: NewRowid P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003105**
drhf0863fe2005-06-12 21:35:51 +00003106** Get a new integer record number (a.k.a "rowid") used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003107** The record number is not previously used as a key in the database
drh4a324312001-12-21 14:30:42 +00003108** table that cursor P1 points to. The new record number is pushed
drh5e00f6c2001-09-13 13:46:56 +00003109** onto the stack.
drh205f48e2004-11-05 00:43:11 +00003110**
3111** If P2>0 then P2 is a memory cell that holds the largest previously
3112** generated record number. No new record numbers are allowed to be less
drh2958a4e2004-11-12 03:56:15 +00003113** than this value. When this value reaches its maximum, a SQLITE_FULL
drh205f48e2004-11-05 00:43:11 +00003114** error is generated. The P2 memory cell is updated with the generated
3115** record number. This P2 mechanism is used to help implement the
3116** AUTOINCREMENT feature.
drh5e00f6c2001-09-13 13:46:56 +00003117*/
drhf0863fe2005-06-12 21:35:51 +00003118case OP_NewRowid: {
drh5e00f6c2001-09-13 13:46:56 +00003119 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00003120 i64 v = 0;
drh80ff32f2001-11-04 18:32:46 +00003121 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00003122 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003123 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003124 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00003125 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003126 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003127 /* The next rowid or record number (different terms for the same
3128 ** thing) is obtained in a two-step algorithm.
3129 **
3130 ** First we attempt to find the largest existing rowid and add one
3131 ** to that. But if the largest existing rowid is already the maximum
3132 ** positive integer, we have to fall through to the second
3133 ** probabilistic algorithm
3134 **
3135 ** The second algorithm is to select a rowid at random and see if
3136 ** it already exists in the table. If it does not exist, we have
3137 ** succeeded. If the random rowid does exist, we select a new one
3138 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00003139 **
3140 ** For a table with less than 2 billion entries, the probability
3141 ** of not finding a unused rowid is about 1.0e-300. This is a
3142 ** non-zero probability, but it is still vanishingly small and should
3143 ** never cause a problem. You are much, much more likely to have a
3144 ** hardware failure than for this algorithm to fail.
3145 **
drhaf9ff332002-01-16 21:00:27 +00003146 ** The analysis in the previous paragraph assumes that you have a good
3147 ** source of random numbers. Is a library function like lrand48()
3148 ** good enough? Maybe. Maybe not. It's hard to know whether there
3149 ** might be subtle bugs is some implementations of lrand48() that
3150 ** could cause problems. To avoid uncertainty, SQLite uses its own
3151 ** random number generator based on the RC4 algorithm.
3152 **
drhdb5ed6d2001-09-18 22:17:44 +00003153 ** To promote locality of reference for repetitive inserts, the
drh5cf8e8c2002-02-19 22:42:05 +00003154 ** first few attempts at chosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00003155 ** larger than the previous rowid. This has been shown experimentally
3156 ** to double the speed of the COPY operation.
3157 */
danielk1977f7df9cc2004-06-16 12:02:47 +00003158 int res, rx=SQLITE_OK, cnt;
drhf328bc82004-05-10 23:29:49 +00003159 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00003160 cnt = 0;
drh4e6083c2005-02-04 21:13:00 +00003161 if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3162 BTREE_INTKEY ){
drh49285702005-09-17 15:20:26 +00003163 rc = SQLITE_CORRUPT_BKPT;
drh4e6083c2005-02-04 21:13:00 +00003164 goto abort_due_to_error;
3165 }
drhf328bc82004-05-10 23:29:49 +00003166 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3167 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drhfe2093d2005-01-20 22:48:47 +00003168
drh75f86a42005-02-17 00:03:06 +00003169#ifdef SQLITE_32BIT_ROWID
3170# define MAX_ROWID 0x7fffffff
3171#else
drhfe2093d2005-01-20 22:48:47 +00003172 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3173 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3174 ** to provide the constant while making all compilers happy.
3175 */
drh75f86a42005-02-17 00:03:06 +00003176# define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
3177#endif
drhfe2093d2005-01-20 22:48:47 +00003178
drh5cf8e8c2002-02-19 22:42:05 +00003179 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00003180 if( pC->nextRowidValid ){
3181 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00003182 }else{
danielk1977261919c2005-12-06 12:52:59 +00003183 rc = sqlite3BtreeLast(pC->pCursor, &res);
3184 if( rc!=SQLITE_OK ){
3185 goto abort_due_to_error;
3186 }
drh32fbe342002-10-19 20:16:37 +00003187 if( res ){
3188 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003189 }else{
danielk1977e0d4b062004-06-28 01:11:46 +00003190 sqlite3BtreeKeySize(pC->pCursor, &v);
drh32fbe342002-10-19 20:16:37 +00003191 v = keyToInt(v);
drh75f86a42005-02-17 00:03:06 +00003192 if( v==MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003193 pC->useRandomRowid = 1;
3194 }else{
3195 v++;
3196 }
drh5cf8e8c2002-02-19 22:42:05 +00003197 }
drh3fc190c2001-09-14 03:24:23 +00003198 }
drh205f48e2004-11-05 00:43:11 +00003199
3200#ifndef SQLITE_OMIT_AUTOINCREMENT
3201 if( pOp->p2 ){
3202 Mem *pMem;
3203 assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */
3204 pMem = &p->aMem[pOp->p2];
drh8a512562005-11-14 22:29:05 +00003205 sqlite3VdbeMemIntegerify(pMem);
drh205f48e2004-11-05 00:43:11 +00003206 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */
drh75f86a42005-02-17 00:03:06 +00003207 if( pMem->i==MAX_ROWID || pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003208 rc = SQLITE_FULL;
3209 goto abort_due_to_error;
3210 }
3211 if( v<pMem->i+1 ){
3212 v = pMem->i + 1;
3213 }
3214 pMem->i = v;
3215 }
3216#endif
3217
drh75f86a42005-02-17 00:03:06 +00003218 if( v<MAX_ROWID ){
drh32fbe342002-10-19 20:16:37 +00003219 pC->nextRowidValid = 1;
3220 pC->nextRowid = v+1;
3221 }else{
3222 pC->nextRowidValid = 0;
3223 }
drh5cf8e8c2002-02-19 22:42:05 +00003224 }
3225 if( pC->useRandomRowid ){
drh205f48e2004-11-05 00:43:11 +00003226 assert( pOp->p2==0 ); /* SQLITE_FULL must have occurred prior to this */
drh5cf8e8c2002-02-19 22:42:05 +00003227 v = db->priorNewRowid;
3228 cnt = 0;
3229 do{
3230 if( v==0 || cnt>2 ){
danielk19774adee202004-05-08 08:23:19 +00003231 sqlite3Randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003232 if( cnt<5 ) v &= 0xffffff;
3233 }else{
drhbbd82df2004-02-11 09:46:30 +00003234 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +00003235 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +00003236 v += r + 1;
drh5cf8e8c2002-02-19 22:42:05 +00003237 }
3238 if( v==0 ) continue;
3239 x = intToKey(v);
drhf328bc82004-05-10 23:29:49 +00003240 rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003241 cnt++;
3242 }while( cnt<1000 && rx==SQLITE_OK && res==0 );
3243 db->priorNewRowid = v;
3244 if( rx==SQLITE_OK && res==0 ){
3245 rc = SQLITE_FULL;
3246 goto abort_due_to_error;
3247 }
drh1eaa2692001-09-18 02:02:23 +00003248 }
drhf0863fe2005-06-12 21:35:51 +00003249 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003250 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003251 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003252 }
drh6810ce62004-01-31 19:22:56 +00003253 pTos++;
3254 pTos->i = v;
3255 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003256 break;
3257}
3258
danielk197794eb6a12005-12-15 15:22:08 +00003259/* Opcode: Insert P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00003260**
jplyon5a564222003-06-02 06:15:58 +00003261** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003262** created if it doesn't already exist or the data for an existing
drh5e00f6c2001-09-13 13:46:56 +00003263** entry is overwritten. The data is the value on the top of the
drh6b125452002-01-28 15:53:03 +00003264** stack. The key is the next value down on the stack. The key must
3265** be an integer. The stack is popped twice by this instruction.
drh4a324312001-12-21 14:30:42 +00003266**
rdcb0c374f2004-02-20 22:53:38 +00003267** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003268** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P2 is set,
3269** then rowid is stored for subsequent return by the
3270** sqlite3_last_insert_rowid() function (otherwise it's unmodified).
drh6b125452002-01-28 15:53:03 +00003271**
drhf0863fe2005-06-12 21:35:51 +00003272** This instruction only works on tables. The equivalent instruction
3273** for indices is OP_IdxInsert.
drh6b125452002-01-28 15:53:03 +00003274*/
drhf0863fe2005-06-12 21:35:51 +00003275case OP_Insert: { /* no-push */
drh6810ce62004-01-31 19:22:56 +00003276 Mem *pNos = &pTos[-1];
drh5e00f6c2001-09-13 13:46:56 +00003277 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003278 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00003279 assert( pNos>=p->aStack );
3280 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003281 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003282 if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
drhf0863fe2005-06-12 21:35:51 +00003283 i64 iKey; /* The integer ROWID or key for the record to be inserted */
danielk19775f8d8a82004-05-11 00:28:42 +00003284
drhf0863fe2005-06-12 21:35:51 +00003285 assert( pNos->flags & MEM_Int );
3286 assert( pC->isTable );
3287 iKey = intToKey(pNos->i);
danielk19775f8d8a82004-05-11 00:28:42 +00003288
drhf0863fe2005-06-12 21:35:51 +00003289 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
3290 if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
drhc490e7d2006-01-23 17:43:53 +00003291 if( pC->nextRowidValid && pNos->i>=pC->nextRowid ){
drhf0863fe2005-06-12 21:35:51 +00003292 pC->nextRowidValid = 0;
drh4a324312001-12-21 14:30:42 +00003293 }
drh78a75832004-02-13 14:07:12 +00003294 if( pTos->flags & MEM_Null ){
3295 pTos->z = 0;
3296 pTos->n = 0;
3297 }else{
danielk1977106bb232004-05-21 10:08:53 +00003298 assert( pTos->flags & (MEM_Blob|MEM_Str) );
drh78a75832004-02-13 14:07:12 +00003299 }
drh70ce3f02003-04-15 19:22:22 +00003300 if( pC->pseudoTable ){
drh70ce3f02003-04-15 19:22:22 +00003301 sqliteFree(pC->pData);
3302 pC->iKey = iKey;
drh6810ce62004-01-31 19:22:56 +00003303 pC->nData = pTos->n;
3304 if( pTos->flags & MEM_Dyn ){
3305 pC->pData = pTos->z;
3306 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003307 }else{
drhf4479502004-05-27 03:12:53 +00003308 pC->pData = sqliteMallocRaw( pC->nData+2 );
danielk19778def5ea2004-06-16 10:39:52 +00003309 if( !pC->pData ) goto no_mem;
3310 memcpy(pC->pData, pTos->z, pC->nData);
drhf4479502004-05-27 03:12:53 +00003311 pC->pData[pC->nData] = 0;
3312 pC->pData[pC->nData+1] = 0;
drh70ce3f02003-04-15 19:22:22 +00003313 }
3314 pC->nullRow = 0;
3315 }else{
drhf0863fe2005-06-12 21:35:51 +00003316 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, pTos->z, pTos->n);
drh70ce3f02003-04-15 19:22:22 +00003317 }
danielk197793758c82005-01-21 08:13:14 +00003318
drhf0863fe2005-06-12 21:35:51 +00003319 pC->rowidIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003320 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003321 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003322
3323 /* Invoke the update-hook if required. */
3324 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3325 const char *zDb = db->aDb[pC->iDb].zName;
3326 const char *zTbl = pOp->p3;
3327 int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3328 assert( pC->isTable );
3329 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3330 assert( pC->iDb>=0 );
3331 }
drh5e00f6c2001-09-13 13:46:56 +00003332 }
drh6810ce62004-01-31 19:22:56 +00003333 popStack(&pTos, 2);
danielk197794eb6a12005-12-15 15:22:08 +00003334
drh5e00f6c2001-09-13 13:46:56 +00003335 break;
3336}
3337
danielk197794eb6a12005-12-15 15:22:08 +00003338/* Opcode: Delete P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00003339**
drh5edc3122001-09-13 21:53:09 +00003340** Delete the record at which the P1 cursor is currently pointing.
3341**
3342** The cursor will be left pointing at either the next or the previous
3343** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003344** the next Next instruction will be a no-op. Hence it is OK to delete
3345** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003346**
rdcb0c374f2004-02-20 22:53:38 +00003347** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
danielk1977b28af712004-06-21 06:50:26 +00003348** incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003349**
3350** If P1 is a pseudo-table, then this instruction is a no-op.
drh5e00f6c2001-09-13 13:46:56 +00003351*/
danielk19777a5147c2005-03-29 13:07:00 +00003352case OP_Delete: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00003353 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003354 Cursor *pC;
drh70ce3f02003-04-15 19:22:22 +00003355 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003356 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003357 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003358 if( pC->pCursor!=0 ){
danielk197794eb6a12005-12-15 15:22:08 +00003359 i64 iKey;
3360
3361 /* If the update-hook will be invoked, set iKey to the rowid of the
3362 ** row being deleted.
3363 */
3364 if( db->xUpdateCallback && pOp->p3 ){
3365 assert( pC->isTable );
3366 if( pC->rowidIsValid ){
3367 iKey = pC->lastRowid;
3368 }else{
3369 rc = sqlite3BtreeKeySize(pC->pCursor, &iKey);
3370 if( rc ){
3371 goto abort_due_to_error;
3372 }
3373 iKey = keyToInt(iKey);
3374 }
3375 }
3376
drh536065a2005-01-26 21:55:31 +00003377 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003378 if( rc ) goto abort_due_to_error;
danielk19774adee202004-05-08 08:23:19 +00003379 rc = sqlite3BtreeDelete(pC->pCursor);
drh32fbe342002-10-19 20:16:37 +00003380 pC->nextRowidValid = 0;
drh76873ab2006-01-07 18:48:26 +00003381 pC->cacheStatus = CACHE_STALE;
danielk197794eb6a12005-12-15 15:22:08 +00003382
3383 /* Invoke the update-hook if required. */
3384 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3385 const char *zDb = db->aDb[pC->iDb].zName;
3386 const char *zTbl = pOp->p3;
3387 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3388 assert( pC->iDb>=0 );
3389 }
drh5e00f6c2001-09-13 13:46:56 +00003390 }
danielk1977b28af712004-06-21 06:50:26 +00003391 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
rdcb0c374f2004-02-20 22:53:38 +00003392 break;
3393}
3394
danielk1977b28af712004-06-21 06:50:26 +00003395/* Opcode: ResetCount P1 * *
rdcb0c374f2004-02-20 22:53:38 +00003396**
danielk1977b28af712004-06-21 06:50:26 +00003397** This opcode resets the VMs internal change counter to 0. If P1 is true,
3398** then the value of the change counter is copied to the database handle
3399** change counter (returned by subsequent calls to sqlite3_changes())
3400** before it is reset. This is used by trigger programs.
rdcb0c374f2004-02-20 22:53:38 +00003401*/
danielk19777a5147c2005-03-29 13:07:00 +00003402case OP_ResetCount: { /* no-push */
danielk1977b28af712004-06-21 06:50:26 +00003403 if( pOp->p1 ){
drh344737f2004-09-19 00:50:20 +00003404 sqlite3VdbeSetChanges(db, p->nChange);
danielk1977b28af712004-06-21 06:50:26 +00003405 }
3406 p->nChange = 0;
drh5e00f6c2001-09-13 13:46:56 +00003407 break;
3408}
3409
drh70ce3f02003-04-15 19:22:22 +00003410/* Opcode: RowData P1 * *
3411**
3412** Push onto the stack the complete row data for cursor P1.
3413** There is no interpretation of the data. It is just copied
3414** onto the stack exactly as it is found in the database file.
3415**
3416** If the cursor is not pointing to a valid row, a NULL is pushed
3417** onto the stack.
3418*/
drh143f3c42004-01-07 20:37:52 +00003419/* Opcode: RowKey P1 * *
3420**
3421** Push onto the stack the complete row key for cursor P1.
3422** There is no interpretation of the key. It is just copied
3423** onto the stack exactly as it is found in the database file.
3424**
3425** If the cursor is not pointing to a valid row, a NULL is pushed
3426** onto the stack.
3427*/
3428case OP_RowKey:
drh70ce3f02003-04-15 19:22:22 +00003429case OP_RowData: {
3430 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003431 Cursor *pC;
danielk1977e0d4b062004-06-28 01:11:46 +00003432 u32 n;
drh70ce3f02003-04-15 19:22:22 +00003433
drhf0863fe2005-06-12 21:35:51 +00003434 /* Note that RowKey and RowData are really exactly the same instruction */
drh6810ce62004-01-31 19:22:56 +00003435 pTos++;
drh70ce3f02003-04-15 19:22:22 +00003436 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003437 pC = p->apCsr[i];
drhf0863fe2005-06-12 21:35:51 +00003438 assert( pC->isTable || pOp->opcode==OP_RowKey );
3439 assert( pC->isIndex || pOp->opcode==OP_RowData );
drh4774b132004-06-12 20:12:51 +00003440 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003441 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003442 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003443 }else if( pC->pCursor!=0 ){
3444 BtCursor *pCrsr = pC->pCursor;
drh536065a2005-01-26 21:55:31 +00003445 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003446 if( rc ) goto abort_due_to_error;
drh70ce3f02003-04-15 19:22:22 +00003447 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003448 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003449 break;
drhf0863fe2005-06-12 21:35:51 +00003450 }else if( pC->isIndex ){
danielk19776490beb2004-05-11 06:17:21 +00003451 i64 n64;
drhf0863fe2005-06-12 21:35:51 +00003452 assert( !pC->isTable );
danielk19776490beb2004-05-11 06:17:21 +00003453 sqlite3BtreeKeySize(pCrsr, &n64);
3454 n = n64;
drh70ce3f02003-04-15 19:22:22 +00003455 }else{
danielk19774adee202004-05-08 08:23:19 +00003456 sqlite3BtreeDataSize(pCrsr, &n);
drh70ce3f02003-04-15 19:22:22 +00003457 }
drh6810ce62004-01-31 19:22:56 +00003458 pTos->n = n;
drh70ce3f02003-04-15 19:22:22 +00003459 if( n<=NBFS ){
danielk1977106bb232004-05-21 10:08:53 +00003460 pTos->flags = MEM_Blob | MEM_Short;
drh6810ce62004-01-31 19:22:56 +00003461 pTos->z = pTos->zShort;
drh70ce3f02003-04-15 19:22:22 +00003462 }else{
3463 char *z = sqliteMallocRaw( n );
3464 if( z==0 ) goto no_mem;
danielk1977106bb232004-05-21 10:08:53 +00003465 pTos->flags = MEM_Blob | MEM_Dyn;
danielk1977d8123362004-06-12 09:25:12 +00003466 pTos->xDel = 0;
drh6810ce62004-01-31 19:22:56 +00003467 pTos->z = z;
drh70ce3f02003-04-15 19:22:22 +00003468 }
drhf0863fe2005-06-12 21:35:51 +00003469 if( pC->isIndex ){
danielk19774adee202004-05-08 08:23:19 +00003470 sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003471 }else{
danielk19774adee202004-05-08 08:23:19 +00003472 sqlite3BtreeData(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003473 }
3474 }else if( pC->pseudoTable ){
drh6810ce62004-01-31 19:22:56 +00003475 pTos->n = pC->nData;
3476 pTos->z = pC->pData;
danielk1977106bb232004-05-21 10:08:53 +00003477 pTos->flags = MEM_Blob|MEM_Ephem;
drh70ce3f02003-04-15 19:22:22 +00003478 }else{
drh6810ce62004-01-31 19:22:56 +00003479 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00003480 }
drh487e2622005-06-25 18:42:14 +00003481 pTos->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
drh5e00f6c2001-09-13 13:46:56 +00003482 break;
3483}
3484
drhf0863fe2005-06-12 21:35:51 +00003485/* Opcode: Rowid P1 * *
drh5e00f6c2001-09-13 13:46:56 +00003486**
drhf0863fe2005-06-12 21:35:51 +00003487** Push onto the stack an integer which is the key of the table entry that
3488** P1 is currently point to.
drh5e00f6c2001-09-13 13:46:56 +00003489*/
drhf0863fe2005-06-12 21:35:51 +00003490case OP_Rowid: {
drh5e00f6c2001-09-13 13:46:56 +00003491 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003492 Cursor *pC;
drhf328bc82004-05-10 23:29:49 +00003493 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003494
drh70ce3f02003-04-15 19:22:22 +00003495 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003496 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003497 assert( pC!=0 );
drh536065a2005-01-26 21:55:31 +00003498 rc = sqlite3VdbeCursorMoveto(pC);
drh52f159e2005-01-27 00:33:21 +00003499 if( rc ) goto abort_due_to_error;
drh6810ce62004-01-31 19:22:56 +00003500 pTos++;
drhf0863fe2005-06-12 21:35:51 +00003501 if( pC->rowidIsValid ){
3502 v = pC->lastRowid;
drh70ce3f02003-04-15 19:22:22 +00003503 }else if( pC->pseudoTable ){
3504 v = keyToInt(pC->iKey);
drhd60ccc62003-06-24 10:39:46 +00003505 }else if( pC->nullRow || pC->pCursor==0 ){
drh6810ce62004-01-31 19:22:56 +00003506 pTos->flags = MEM_Null;
drhd60ccc62003-06-24 10:39:46 +00003507 break;
drh70ce3f02003-04-15 19:22:22 +00003508 }else{
3509 assert( pC->pCursor!=0 );
danielk1977e0d4b062004-06-28 01:11:46 +00003510 sqlite3BtreeKeySize(pC->pCursor, &v);
drh70ce3f02003-04-15 19:22:22 +00003511 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003512 }
drh6810ce62004-01-31 19:22:56 +00003513 pTos->i = v;
3514 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003515 break;
3516}
3517
drh17f71932002-02-21 12:01:27 +00003518/* Opcode: NullRow P1 * *
3519**
3520** Move the cursor P1 to a null row. Any OP_Column operations
3521** that occur while the cursor is on the null row will always push
3522** a NULL onto the stack.
3523*/
danielk19777a5147c2005-03-29 13:07:00 +00003524case OP_NullRow: { /* no-push */
drh17f71932002-02-21 12:01:27 +00003525 int i = pOp->p1;
drhd7556d22004-05-14 21:59:40 +00003526 Cursor *pC;
drh17f71932002-02-21 12:01:27 +00003527
drh70ce3f02003-04-15 19:22:22 +00003528 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003529 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003530 assert( pC!=0 );
drhd7556d22004-05-14 21:59:40 +00003531 pC->nullRow = 1;
drhf0863fe2005-06-12 21:35:51 +00003532 pC->rowidIsValid = 0;
drh17f71932002-02-21 12:01:27 +00003533 break;
3534}
3535
drh9562b552002-02-19 15:00:07 +00003536/* Opcode: Last P1 P2 *
3537**
drhf0863fe2005-06-12 21:35:51 +00003538** The next use of the Rowid or Column or Next instruction for P1
drh9562b552002-02-19 15:00:07 +00003539** will refer to the last entry in the database table or index.
3540** If the table or index is empty and P2>0, then jump immediately to P2.
3541** If P2 is 0 or if the table or index is not empty, fall through
3542** to the following instruction.
3543*/
danielk19777a5147c2005-03-29 13:07:00 +00003544case OP_Last: { /* no-push */
drh9562b552002-02-19 15:00:07 +00003545 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003546 Cursor *pC;
drh9562b552002-02-19 15:00:07 +00003547 BtCursor *pCrsr;
3548
drh70ce3f02003-04-15 19:22:22 +00003549 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003550 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003551 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003552 if( (pCrsr = pC->pCursor)!=0 ){
drh9562b552002-02-19 15:00:07 +00003553 int res;
danielk19774adee202004-05-08 08:23:19 +00003554 rc = sqlite3BtreeLast(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00003555 pC->nullRow = res;
3556 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003557 pC->cacheStatus = CACHE_STALE;
drh9562b552002-02-19 15:00:07 +00003558 if( res && pOp->p2>0 ){
3559 pc = pOp->p2 - 1;
3560 }
drh70ce3f02003-04-15 19:22:22 +00003561 }else{
3562 pC->nullRow = 0;
drh9562b552002-02-19 15:00:07 +00003563 }
3564 break;
3565}
3566
drh0342b1f2005-09-01 03:07:44 +00003567
3568/* Opcode: Sort P1 P2 *
3569**
3570** This opcode does exactly the same thing as OP_Rewind except that
3571** it increments an undocumented global variable used for testing.
3572**
3573** Sorting is accomplished by writing records into a sorting index,
3574** then rewinding that index and playing it back from beginning to
3575** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3576** rewinding so that the global variable will be incremented and
3577** regression tests can determine whether or not the optimizer is
3578** correctly optimizing out sorts.
3579*/
3580case OP_Sort: { /* no-push */
3581 sqlite3_sort_count++;
drh4db38a72005-09-01 12:16:28 +00003582 sqlite3_search_count--;
drh0342b1f2005-09-01 03:07:44 +00003583 /* Fall through into OP_Rewind */
3584}
drh8721ce42001-11-07 14:22:00 +00003585/* Opcode: Rewind P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003586**
drhf0863fe2005-06-12 21:35:51 +00003587** The next use of the Rowid or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003588** will refer to the first entry in the database table or index.
3589** If the table or index is empty and P2>0, then jump immediately to P2.
3590** If P2 is 0 or if the table or index is not empty, fall through
3591** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003592*/
danielk19777a5147c2005-03-29 13:07:00 +00003593case OP_Rewind: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00003594 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003595 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003596 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003597 int res;
drh5e00f6c2001-09-13 13:46:56 +00003598
drh70ce3f02003-04-15 19:22:22 +00003599 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003600 pC = p->apCsr[i];
drh4774b132004-06-12 20:12:51 +00003601 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003602 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003603 rc = sqlite3BtreeFirst(pCrsr, &res);
drh70ce3f02003-04-15 19:22:22 +00003604 pC->atFirst = res==0;
drha11846b2004-01-07 18:52:56 +00003605 pC->deferredMoveto = 0;
drh76873ab2006-01-07 18:48:26 +00003606 pC->cacheStatus = CACHE_STALE;
drh70ce3f02003-04-15 19:22:22 +00003607 }else{
drhf4dada72004-05-11 09:57:35 +00003608 res = 1;
3609 }
3610 pC->nullRow = res;
3611 if( res && pOp->p2>0 ){
3612 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003613 }
3614 break;
3615}
3616
3617/* Opcode: Next P1 P2 *
3618**
3619** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003620** table or index. If there are no more key/value pairs then fall through
3621** to the following instruction. But if the cursor advance was successful,
3622** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003623**
3624** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003625*/
drhc045ec52002-12-04 20:01:06 +00003626/* Opcode: Prev P1 P2 *
3627**
3628** Back up cursor P1 so that it points to the previous key/data pair in its
3629** table or index. If there is no previous key/value pairs then fall through
3630** to the following instruction. But if the cursor backup was successful,
3631** jump immediately to P2.
3632*/
danielk19777a5147c2005-03-29 13:07:00 +00003633case OP_Prev: /* no-push */
3634case OP_Next: { /* no-push */
drh7b396862003-01-01 23:06:20 +00003635 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003636 BtCursor *pCrsr;
3637
drhcaec2f12003-01-07 02:47:47 +00003638 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00003639 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003640 pC = p->apCsr[pOp->p1];
drh4774b132004-06-12 20:12:51 +00003641 assert( pC!=0 );
drh70ce3f02003-04-15 19:22:22 +00003642 if( (pCrsr = pC->pCursor)!=0 ){
drh8721ce42001-11-07 14:22:00 +00003643 int res;
drh7b396862003-01-01 23:06:20 +00003644 if( pC->nullRow ){
drhad2d8302002-05-24 20:31:36 +00003645 res = 1;
3646 }else{
drha11846b2004-01-07 18:52:56 +00003647 assert( pC->deferredMoveto==0 );
danielk19774adee202004-05-08 08:23:19 +00003648 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3649 sqlite3BtreePrevious(pCrsr, &res);
drh7b396862003-01-01 23:06:20 +00003650 pC->nullRow = res;
drh76873ab2006-01-07 18:48:26 +00003651 pC->cacheStatus = CACHE_STALE;
drhad2d8302002-05-24 20:31:36 +00003652 }
drh8721ce42001-11-07 14:22:00 +00003653 if( res==0 ){
3654 pc = pOp->p2 - 1;
danielk19776f8a5032004-05-10 10:34:51 +00003655 sqlite3_search_count++;
drh8721ce42001-11-07 14:22:00 +00003656 }
drh70ce3f02003-04-15 19:22:22 +00003657 }else{
3658 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00003659 }
drhf0863fe2005-06-12 21:35:51 +00003660 pC->rowidIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00003661 break;
3662}
3663
drh7f057c92005-06-24 03:53:06 +00003664/* Opcode: IdxInsert P1 * *
drh5e00f6c2001-09-13 13:46:56 +00003665**
drhee32e0a2006-01-10 19:45:49 +00003666** The top of the stack holds a SQL index key made using either the
3667** MakeIdxRec or MakeRecord instructions. This opcode writes that key
3668** into the index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00003669**
drhf0863fe2005-06-12 21:35:51 +00003670** This instruction only works for indices. The equivalent instruction
3671** for tables is OP_Insert.
drh5e00f6c2001-09-13 13:46:56 +00003672*/
drhf0863fe2005-06-12 21:35:51 +00003673case OP_IdxInsert: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00003674 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003675 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003676 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003677 assert( pTos>=p->aStack );
3678 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003679 assert( p->apCsr[i]!=0 );
danielk1977106bb232004-05-21 10:08:53 +00003680 assert( pTos->flags & MEM_Blob );
drh7f057c92005-06-24 03:53:06 +00003681 assert( pOp->p2==0 );
drhd7556d22004-05-14 21:59:40 +00003682 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh6810ce62004-01-31 19:22:56 +00003683 int nKey = pTos->n;
3684 const char *zKey = pTos->z;
drhf0863fe2005-06-12 21:35:51 +00003685 assert( pC->isTable==0 );
danielk19774adee202004-05-08 08:23:19 +00003686 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0);
drh9188b382004-05-14 21:12:22 +00003687 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003688 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003689 }
drh6810ce62004-01-31 19:22:56 +00003690 Release(pTos);
3691 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003692 break;
3693}
3694
drh8721ce42001-11-07 14:22:00 +00003695/* Opcode: IdxDelete P1 * *
drh5e00f6c2001-09-13 13:46:56 +00003696**
drhee32e0a2006-01-10 19:45:49 +00003697** The top of the stack is an index key built using the either the
3698** MakeIdxRec or MakeRecord opcodes.
drh5e00f6c2001-09-13 13:46:56 +00003699** This opcode removes that entry from the index.
3700*/
danielk19777a5147c2005-03-29 13:07:00 +00003701case OP_IdxDelete: { /* no-push */
drh5e00f6c2001-09-13 13:46:56 +00003702 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003703 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003704 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003705 assert( pTos>=p->aStack );
danielk1977106bb232004-05-21 10:08:53 +00003706 assert( pTos->flags & MEM_Blob );
drh6810ce62004-01-31 19:22:56 +00003707 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003708 assert( p->apCsr[i]!=0 );
drhd7556d22004-05-14 21:59:40 +00003709 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197775bab7d2006-01-23 13:09:45 +00003710 int res;
3711 rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
3712 if( rc==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00003713 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00003714 }
drh9188b382004-05-14 21:12:22 +00003715 assert( pC->deferredMoveto==0 );
drh76873ab2006-01-07 18:48:26 +00003716 pC->cacheStatus = CACHE_STALE;
drh5e00f6c2001-09-13 13:46:56 +00003717 }
drh6810ce62004-01-31 19:22:56 +00003718 Release(pTos);
3719 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003720 break;
3721}
3722
drhf0863fe2005-06-12 21:35:51 +00003723/* Opcode: IdxRowid P1 * *
drh8721ce42001-11-07 14:22:00 +00003724**
drhf0863fe2005-06-12 21:35:51 +00003725** Push onto the stack an integer which is the last entry in the record at
3726** the end of the index key pointed to by cursor P1. This integer should be
3727** the rowid of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00003728**
drh3c899a62006-01-10 18:44:08 +00003729** See also: Rowid, MakeIdxRec.
drh8721ce42001-11-07 14:22:00 +00003730*/
drhf0863fe2005-06-12 21:35:51 +00003731case OP_IdxRowid: {
drh8721ce42001-11-07 14:22:00 +00003732 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003733 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003734 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003735
drh6810ce62004-01-31 19:22:56 +00003736 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003737 assert( p->apCsr[i]!=0 );
drh6810ce62004-01-31 19:22:56 +00003738 pTos++;
drhfebe1062004-08-28 18:17:48 +00003739 pTos->flags = MEM_Null;
drhd7556d22004-05-14 21:59:40 +00003740 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00003741 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00003742
drhd7556d22004-05-14 21:59:40 +00003743 assert( pC->deferredMoveto==0 );
drhf0863fe2005-06-12 21:35:51 +00003744 assert( pC->isTable==0 );
danielk19771d850a72004-05-31 08:26:49 +00003745 if( pC->nullRow ){
3746 pTos->flags = MEM_Null;
3747 }else{
3748 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3749 if( rc!=SQLITE_OK ){
3750 goto abort_due_to_error;
3751 }
3752 pTos->flags = MEM_Int;
3753 pTos->i = rowid;
danielk19773d1bfea2004-05-14 11:00:53 +00003754 }
drh8721ce42001-11-07 14:22:00 +00003755 }
3756 break;
3757}
3758
drh7cf6e4d2004-05-19 14:56:55 +00003759/* Opcode: IdxGT P1 P2 *
drh8721ce42001-11-07 14:22:00 +00003760**
drhf3218fe2004-05-28 08:21:02 +00003761** The top of the stack is an index entry that omits the ROWID. Compare
3762** the top of stack against the index that P1 is currently pointing to.
3763** Ignore the ROWID on the P1 index.
3764**
3765** The top of the stack might have fewer columns that P1.
3766**
3767** If the P1 index entry is greater than the top of the stack
drh8721ce42001-11-07 14:22:00 +00003768** then jump to P2. Otherwise fall through to the next instruction.
3769** In either case, the stack is popped once.
3770*/
drh7cf6e4d2004-05-19 14:56:55 +00003771/* Opcode: IdxGE P1 P2 P3
drh8721ce42001-11-07 14:22:00 +00003772**
drhf3218fe2004-05-28 08:21:02 +00003773** The top of the stack is an index entry that omits the ROWID. Compare
3774** the top of stack against the index that P1 is currently pointing to.
3775** Ignore the ROWID on the P1 index.
3776**
3777** If the P1 index entry is greater than or equal to the top of the stack
drh8721ce42001-11-07 14:22:00 +00003778** then jump to P2. Otherwise fall through to the next instruction.
3779** In either case, the stack is popped once.
drh772ae622004-05-19 13:13:08 +00003780**
3781** If P3 is the "+" string (or any other non-NULL string) then the
3782** index taken from the top of the stack is temporarily increased by
drh7cf6e4d2004-05-19 14:56:55 +00003783** an epsilon prior to the comparison. This make the opcode work
3784** like IdxGT except that if the key from the stack is a prefix of
3785** the key in the cursor, the result is false whereas it would be
3786** true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00003787*/
drh7cf6e4d2004-05-19 14:56:55 +00003788/* Opcode: IdxLT P1 P2 P3
drhc045ec52002-12-04 20:01:06 +00003789**
drhf3218fe2004-05-28 08:21:02 +00003790** The top of the stack is an index entry that omits the ROWID. Compare
3791** the top of stack against the index that P1 is currently pointing to.
3792** Ignore the ROWID on the P1 index.
3793**
3794** If the P1 index entry is less than the top of the stack
drhc045ec52002-12-04 20:01:06 +00003795** then jump to P2. Otherwise fall through to the next instruction.
3796** In either case, the stack is popped once.
drh772ae622004-05-19 13:13:08 +00003797**
3798** If P3 is the "+" string (or any other non-NULL string) then the
3799** index taken from the top of the stack is temporarily increased by
drh7cf6e4d2004-05-19 14:56:55 +00003800** an epsilon prior to the comparison. This makes the opcode work
3801** like IdxLE.
drhc045ec52002-12-04 20:01:06 +00003802*/
danielk19777a5147c2005-03-29 13:07:00 +00003803case OP_IdxLT: /* no-push */
3804case OP_IdxGT: /* no-push */
3805case OP_IdxGE: { /* no-push */
drh8721ce42001-11-07 14:22:00 +00003806 int i= pOp->p1;
drhd7556d22004-05-14 21:59:40 +00003807 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003808
drh6810ce62004-01-31 19:22:56 +00003809 assert( i>=0 && i<p->nCursor );
drh4774b132004-06-12 20:12:51 +00003810 assert( p->apCsr[i]!=0 );
drh6810ce62004-01-31 19:22:56 +00003811 assert( pTos>=p->aStack );
drh4f26bb62005-09-08 14:17:20 +00003812 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh0850b532006-01-31 19:31:43 +00003813 int res;
drh8721ce42001-11-07 14:22:00 +00003814
drhf3218fe2004-05-28 08:21:02 +00003815 assert( pTos->flags & MEM_Blob ); /* Created using OP_Make*Key */
drh8079a0d2006-01-12 17:20:50 +00003816 Stringify(pTos, encoding);
drhd7556d22004-05-14 21:59:40 +00003817 assert( pC->deferredMoveto==0 );
drhd3d39e92004-05-20 22:16:29 +00003818 *pC->pIncrKey = pOp->p3!=0;
drh7cf6e4d2004-05-19 14:56:55 +00003819 assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
drh2646da72005-12-09 20:02:05 +00003820 rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res);
drhd3d39e92004-05-20 22:16:29 +00003821 *pC->pIncrKey = 0;
drh8721ce42001-11-07 14:22:00 +00003822 if( rc!=SQLITE_OK ){
3823 break;
3824 }
drhc045ec52002-12-04 20:01:06 +00003825 if( pOp->opcode==OP_IdxLT ){
3826 res = -res;
3827 }else if( pOp->opcode==OP_IdxGE ){
drh8721ce42001-11-07 14:22:00 +00003828 res++;
3829 }
3830 if( res>0 ){
3831 pc = pOp->p2 - 1 ;
3832 }
3833 }
drh6810ce62004-01-31 19:22:56 +00003834 Release(pTos);
3835 pTos--;
drh8721ce42001-11-07 14:22:00 +00003836 break;
3837}
3838
drh143f3c42004-01-07 20:37:52 +00003839/* Opcode: IdxIsNull P1 P2 *
3840**
3841** The top of the stack contains an index entry such as might be generated
drh3c899a62006-01-10 18:44:08 +00003842** by the MakeIdxRec opcode. This routine looks at the first P1 fields of
drh143f3c42004-01-07 20:37:52 +00003843** that key. If any of the first P1 fields are NULL, then a jump is made
3844** to address P2. Otherwise we fall straight through.
3845**
3846** The index entry is always popped from the stack.
3847*/
danielk19777a5147c2005-03-29 13:07:00 +00003848case OP_IdxIsNull: { /* no-push */
drh143f3c42004-01-07 20:37:52 +00003849 int i = pOp->p1;
drh143f3c42004-01-07 20:37:52 +00003850 int k, n;
3851 const char *z;
drhf3218fe2004-05-28 08:21:02 +00003852 u32 serial_type;
drh143f3c42004-01-07 20:37:52 +00003853
drh6810ce62004-01-31 19:22:56 +00003854 assert( pTos>=p->aStack );
danielk1977106bb232004-05-21 10:08:53 +00003855 assert( pTos->flags & MEM_Blob );
drh6810ce62004-01-31 19:22:56 +00003856 z = pTos->z;
3857 n = pTos->n;
drh2646da72005-12-09 20:02:05 +00003858 k = sqlite3GetVarint32((u8*)z, &serial_type);
drhf3218fe2004-05-28 08:21:02 +00003859 for(; k<n && i>0; i--){
drh2646da72005-12-09 20:02:05 +00003860 k += sqlite3GetVarint32((u8*)&z[k], &serial_type);
drha19b7752004-05-30 21:14:58 +00003861 if( serial_type==0 ){ /* Serial type 0 is a NULL */
drh143f3c42004-01-07 20:37:52 +00003862 pc = pOp->p2-1;
3863 break;
3864 }
drh143f3c42004-01-07 20:37:52 +00003865 }
drh6810ce62004-01-31 19:22:56 +00003866 Release(pTos);
3867 pTos--;
drh143f3c42004-01-07 20:37:52 +00003868 break;
3869}
3870
drhf57b3392001-10-08 13:22:32 +00003871/* Opcode: Destroy P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003872**
3873** Delete an entire database table or index whose root page in the database
3874** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00003875**
drhf57b3392001-10-08 13:22:32 +00003876** The table being destroyed is in the main database file if P2==0. If
3877** P2==1 then the table to be clear is in the auxiliary database file
3878** that is used to store tables create using CREATE TEMPORARY TABLE.
3879**
drh205f48e2004-11-05 00:43:11 +00003880** If AUTOVACUUM is enabled then it is possible that another root page
3881** might be moved into the newly deleted root page in order to keep all
3882** root pages contiguous at the beginning of the database. The former
3883** value of the root page that moved - its value before the move occurred -
3884** is pushed onto the stack. If no page movement was required (because
3885** the table being dropped was already the last one in the database) then
drh81db88e2004-12-07 12:29:17 +00003886** a zero is pushed onto the stack. If AUTOVACUUM is disabled
drh205f48e2004-11-05 00:43:11 +00003887** then a zero is pushed onto the stack.
3888**
drhb19a2bc2001-09-16 00:13:26 +00003889** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00003890*/
3891case OP_Destroy: {
danielk1977a0bf2652004-11-04 14:30:04 +00003892 int iMoved;
danielk1977e6efa742004-11-10 11:55:10 +00003893 if( db->activeVdbeCnt>1 ){
3894 rc = SQLITE_LOCKED;
3895 }else{
3896 assert( db->activeVdbeCnt==1 );
3897 rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
3898 pTos++;
3899 pTos->flags = MEM_Int;
3900 pTos->i = iMoved;
3901 #ifndef SQLITE_OMIT_AUTOVACUUM
3902 if( rc==SQLITE_OK && iMoved!=0 ){
3903 sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1);
3904 }
3905 #endif
danielk1977a0bf2652004-11-04 14:30:04 +00003906 }
drh5e00f6c2001-09-13 13:46:56 +00003907 break;
3908}
3909
drhf57b3392001-10-08 13:22:32 +00003910/* Opcode: Clear P1 P2 *
drh5edc3122001-09-13 21:53:09 +00003911**
3912** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00003913** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00003914** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00003915**
drhf57b3392001-10-08 13:22:32 +00003916** The table being clear is in the main database file if P2==0. If
3917** P2==1 then the table to be clear is in the auxiliary database file
3918** that is used to store tables create using CREATE TEMPORARY TABLE.
3919**
drhb19a2bc2001-09-16 00:13:26 +00003920** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00003921*/
danielk19777a5147c2005-03-29 13:07:00 +00003922case OP_Clear: { /* no-push */
drhd78901d2006-01-05 23:42:50 +00003923
3924 /* For consistency with the way other features of SQLite operate
3925 ** with a truncate, we will also skip the update callback.
3926 */
3927#if 0
danielk197794eb6a12005-12-15 15:22:08 +00003928 Btree *pBt = db->aDb[pOp->p2].pBt;
3929 if( db->xUpdateCallback && pOp->p3 ){
3930 const char *zDb = db->aDb[pOp->p2].zName;
3931 const char *zTbl = pOp->p3;
3932 BtCursor *pCur = 0;
3933 int fin = 0;
3934
3935 rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur);
3936 if( rc!=SQLITE_OK ){
3937 goto abort_due_to_error;
3938 }
3939 for(
3940 rc=sqlite3BtreeFirst(pCur, &fin);
3941 rc==SQLITE_OK && !fin;
3942 rc=sqlite3BtreeNext(pCur, &fin)
3943 ){
3944 i64 iKey;
3945 rc = sqlite3BtreeKeySize(pCur, &iKey);
3946 if( rc ){
3947 break;
3948 }
3949 iKey = keyToInt(iKey);
3950 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3951 }
3952 sqlite3BtreeCloseCursor(pCur);
3953 if( rc!=SQLITE_OK ){
3954 goto abort_due_to_error;
3955 }
3956 }
drhd78901d2006-01-05 23:42:50 +00003957#endif
danielk19774adee202004-05-08 08:23:19 +00003958 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
drh5edc3122001-09-13 21:53:09 +00003959 break;
3960}
3961
drh234c39d2004-07-24 03:30:47 +00003962/* Opcode: CreateTable P1 * *
drh5b2fd562001-09-13 15:21:31 +00003963**
drhf57b3392001-10-08 13:22:32 +00003964** Allocate a new table in the main database file if P2==0 or in the
3965** auxiliary database file if P2==1. Push the page number
drh5b2fd562001-09-13 15:21:31 +00003966** for the root page of the new table onto the stack.
3967**
drhc6b52df2002-01-04 03:09:29 +00003968** The difference between a table and an index is this: A table must
3969** have a 4-byte integer key and can have arbitrary data. An index
3970** has an arbitrary key but no data.
3971**
drhb19a2bc2001-09-16 00:13:26 +00003972** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00003973*/
drh234c39d2004-07-24 03:30:47 +00003974/* Opcode: CreateIndex P1 * *
drhf57b3392001-10-08 13:22:32 +00003975**
drhc6b52df2002-01-04 03:09:29 +00003976** Allocate a new index in the main database file if P2==0 or in the
3977** auxiliary database file if P2==1. Push the page number of the
3978** root page of the new index onto the stack.
drhf57b3392001-10-08 13:22:32 +00003979**
drhc6b52df2002-01-04 03:09:29 +00003980** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00003981*/
3982case OP_CreateIndex:
drh5b2fd562001-09-13 15:21:31 +00003983case OP_CreateTable: {
drh5b2fd562001-09-13 15:21:31 +00003984 int pgno;
drhf328bc82004-05-10 23:29:49 +00003985 int flags;
drh234c39d2004-07-24 03:30:47 +00003986 Db *pDb;
3987 assert( pOp->p1>=0 && pOp->p1<db->nDb );
3988 pDb = &db->aDb[pOp->p1];
3989 assert( pDb->pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00003990 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00003991 /* flags = BTREE_INTKEY; */
3992 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00003993 }else{
drhf328bc82004-05-10 23:29:49 +00003994 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00003995 }
drh234c39d2004-07-24 03:30:47 +00003996 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
drh6810ce62004-01-31 19:22:56 +00003997 pTos++;
drh5b2fd562001-09-13 15:21:31 +00003998 if( rc==SQLITE_OK ){
drh6810ce62004-01-31 19:22:56 +00003999 pTos->i = pgno;
4000 pTos->flags = MEM_Int;
drhf1b07b02004-02-08 06:17:19 +00004001 }else{
4002 pTos->flags = MEM_Null;
drh5b2fd562001-09-13 15:21:31 +00004003 }
4004 break;
4005}
4006
drh234c39d2004-07-24 03:30:47 +00004007/* Opcode: ParseSchema P1 * P3
4008**
4009** Read and parse all entries from the SQLITE_MASTER table of database P1
4010** that match the WHERE clause P3.
4011**
4012** This opcode invokes the parser to create a new virtual machine,
4013** then runs the new virtual machine. It is thus a reentrant opcode.
4014*/
danielk19777a5147c2005-03-29 13:07:00 +00004015case OP_ParseSchema: { /* no-push */
drh234c39d2004-07-24 03:30:47 +00004016 char *zSql;
4017 int iDb = pOp->p1;
4018 const char *zMaster;
4019 InitData initData;
4020
4021 assert( iDb>=0 && iDb<db->nDb );
drh3f7d4e42004-07-24 14:35:58 +00004022 if( !DbHasProperty(db, iDb, DB_SchemaLoaded) ) break;
danielk197753c0f742005-03-29 03:10:59 +00004023 zMaster = SCHEMA_TABLE(iDb);
drh234c39d2004-07-24 03:30:47 +00004024 initData.db = db;
4025 initData.pzErrMsg = &p->zErrMsg;
4026 zSql = sqlite3MPrintf(
4027 "SELECT name, rootpage, sql, %d FROM '%q'.%s WHERE %s",
4028 pOp->p1, db->aDb[iDb].zName, zMaster, pOp->p3);
drh71c697e2004-08-08 23:39:19 +00004029 if( zSql==0 ) goto no_mem;
drh234c39d2004-07-24 03:30:47 +00004030 sqlite3SafetyOff(db);
4031 assert( db->init.busy==0 );
4032 db->init.busy = 1;
danielk19779e128002006-01-18 16:51:35 +00004033 assert( !sqlite3MallocFailed() );
drh234c39d2004-07-24 03:30:47 +00004034 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
danielk19772e588c72005-12-09 14:25:08 +00004035 sqliteFree(zSql);
drh234c39d2004-07-24 03:30:47 +00004036 db->init.busy = 0;
4037 sqlite3SafetyOn(db);
danielk1977261919c2005-12-06 12:52:59 +00004038 if( rc==SQLITE_NOMEM ){
danielk19779e128002006-01-18 16:51:35 +00004039 sqlite3FailedMalloc();
danielk1977261919c2005-12-06 12:52:59 +00004040 goto no_mem;
4041 }
drh234c39d2004-07-24 03:30:47 +00004042 break;
4043}
4044
drhcfed7bc2006-03-13 14:28:05 +00004045#if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
drh497e4462005-07-23 03:18:40 +00004046/* Opcode: LoadAnalysis P1 * *
4047**
4048** Read the sqlite_stat1 table for database P1 and load the content
4049** of that table into the internal index hash table. This will cause
4050** the analysis to be used when preparing all subsequent queries.
4051*/
4052case OP_LoadAnalysis: { /* no-push */
4053 int iDb = pOp->p1;
4054 assert( iDb>=0 && iDb<db->nDb );
4055 sqlite3AnalysisLoad(db, iDb);
4056 break;
4057}
drhcfed7bc2006-03-13 14:28:05 +00004058#endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
drh497e4462005-07-23 03:18:40 +00004059
drh956bc922004-07-24 17:38:29 +00004060/* Opcode: DropTable P1 * P3
4061**
4062** Remove the internal (in-memory) data structures that describe
4063** the table named P3 in database P1. This is called after a table
4064** is dropped in order to keep the internal representation of the
4065** schema consistent with what is on disk.
4066*/
danielk19777a5147c2005-03-29 13:07:00 +00004067case OP_DropTable: { /* no-push */
drh956bc922004-07-24 17:38:29 +00004068 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3);
4069 break;
4070}
4071
4072/* Opcode: DropIndex P1 * P3
4073**
4074** Remove the internal (in-memory) data structures that describe
4075** the index named P3 in database P1. This is called after an index
4076** is dropped in order to keep the internal representation of the
4077** schema consistent with what is on disk.
4078*/
danielk19777a5147c2005-03-29 13:07:00 +00004079case OP_DropIndex: { /* no-push */
drh956bc922004-07-24 17:38:29 +00004080 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3);
4081 break;
4082}
4083
4084/* Opcode: DropTrigger P1 * P3
4085**
4086** Remove the internal (in-memory) data structures that describe
4087** the trigger named P3 in database P1. This is called after a trigger
4088** is dropped in order to keep the internal representation of the
4089** schema consistent with what is on disk.
4090*/
danielk19777a5147c2005-03-29 13:07:00 +00004091case OP_DropTrigger: { /* no-push */
drh956bc922004-07-24 17:38:29 +00004092 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3);
4093 break;
4094}
4095
drh234c39d2004-07-24 03:30:47 +00004096
drhb7f91642004-10-31 02:22:47 +00004097#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh79069752004-05-22 21:30:40 +00004098/* Opcode: IntegrityCk * P2 *
drh5e00f6c2001-09-13 13:46:56 +00004099**
drh1dd397f2002-02-03 03:34:07 +00004100** Do an analysis of the currently open database. Push onto the
4101** stack the text of an error message describing any problems.
4102** If there are no errors, push a "ok" onto the stack.
drhb19a2bc2001-09-16 00:13:26 +00004103**
drh79069752004-05-22 21:30:40 +00004104** The root page numbers of all tables in the database are integer
4105** values on the stack. This opcode pulls as many integers as it
4106** can off of the stack and uses those numbers as the root pages.
drh21504322002-06-25 13:16:02 +00004107**
4108** If P2 is not zero, the check is done on the auxiliary database
4109** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00004110**
4111** This opcode is used for testing purposes only.
drh5e00f6c2001-09-13 13:46:56 +00004112*/
drhaaab5722002-02-19 13:39:21 +00004113case OP_IntegrityCk: {
drh1dd397f2002-02-03 03:34:07 +00004114 int nRoot;
4115 int *aRoot;
drh1dd397f2002-02-03 03:34:07 +00004116 int j;
drh1dd397f2002-02-03 03:34:07 +00004117 char *z;
4118
drh79069752004-05-22 21:30:40 +00004119 for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
4120 if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
4121 }
4122 assert( nRoot>0 );
4123 aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00004124 if( aRoot==0 ) goto no_mem;
drh79069752004-05-22 21:30:40 +00004125 for(j=0; j<nRoot; j++){
4126 Mem *pMem = &pTos[-j];
4127 aRoot[j] = pMem->i;
drh1dd397f2002-02-03 03:34:07 +00004128 }
4129 aRoot[j] = 0;
drh79069752004-05-22 21:30:40 +00004130 popStack(&pTos, nRoot);
4131 pTos++;
danielk19774adee202004-05-08 08:23:19 +00004132 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
drh1dd397f2002-02-03 03:34:07 +00004133 if( z==0 || z[0]==0 ){
drh21504322002-06-25 13:16:02 +00004134 if( z ) sqliteFree(z);
drh6810ce62004-01-31 19:22:56 +00004135 pTos->z = "ok";
drhf4479502004-05-27 03:12:53 +00004136 pTos->n = 2;
4137 pTos->flags = MEM_Str | MEM_Static | MEM_Term;
drh1dd397f2002-02-03 03:34:07 +00004138 }else{
drh6810ce62004-01-31 19:22:56 +00004139 pTos->z = z;
drhf4479502004-05-27 03:12:53 +00004140 pTos->n = strlen(z);
4141 pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
danielk1977d8123362004-06-12 09:25:12 +00004142 pTos->xDel = 0;
danielk19778a6b5412004-05-24 07:04:25 +00004143 }
danielk1977dc8453f2004-06-12 00:42:34 +00004144 pTos->enc = SQLITE_UTF8;
drh8079a0d2006-01-12 17:20:50 +00004145 sqlite3VdbeChangeEncoding(pTos, encoding);
drh24e97df2002-02-03 19:06:02 +00004146 sqliteFree(aRoot);
drh5e00f6c2001-09-13 13:46:56 +00004147 break;
4148}
drhb7f91642004-10-31 02:22:47 +00004149#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5e00f6c2001-09-13 13:46:56 +00004150
drha01f79d2005-07-08 13:07:59 +00004151/* Opcode: FifoWrite * * *
drh5e00f6c2001-09-13 13:46:56 +00004152**
4153** Write the integer on the top of the stack
drha01f79d2005-07-08 13:07:59 +00004154** into the Fifo.
drh5e00f6c2001-09-13 13:46:56 +00004155*/
drha01f79d2005-07-08 13:07:59 +00004156case OP_FifoWrite: { /* no-push */
drh6810ce62004-01-31 19:22:56 +00004157 assert( pTos>=p->aStack );
drh8a512562005-11-14 22:29:05 +00004158 sqlite3VdbeMemIntegerify(pTos);
drha01f79d2005-07-08 13:07:59 +00004159 sqlite3VdbeFifoPush(&p->sFifo, pTos->i);
drh6a6124e2004-06-27 01:56:33 +00004160 assert( (pTos->flags & MEM_Dyn)==0 );
drh6810ce62004-01-31 19:22:56 +00004161 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004162 break;
4163}
4164
drha01f79d2005-07-08 13:07:59 +00004165/* Opcode: FifoRead * P2 *
drh5e00f6c2001-09-13 13:46:56 +00004166**
drha01f79d2005-07-08 13:07:59 +00004167** Attempt to read a single integer from the Fifo
4168** and push it onto the stack. If the Fifo is empty
drh5e00f6c2001-09-13 13:46:56 +00004169** push nothing but instead jump to P2.
4170*/
drha01f79d2005-07-08 13:07:59 +00004171case OP_FifoRead: {
4172 i64 v;
drhcaec2f12003-01-07 02:47:47 +00004173 CHECK_FOR_INTERRUPT;
drha01f79d2005-07-08 13:07:59 +00004174 if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){
drh5e00f6c2001-09-13 13:46:56 +00004175 pc = pOp->p2 - 1;
drha01f79d2005-07-08 13:07:59 +00004176 }else{
4177 pTos++;
4178 pTos->i = v;
4179 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00004180 }
4181 break;
4182}
4183
danielk197793758c82005-01-21 08:13:14 +00004184#ifndef SQLITE_OMIT_TRIGGER
rdcb0c374f2004-02-20 22:53:38 +00004185/* Opcode: ContextPush * * *
4186**
4187** Save the current Vdbe context such that it can be restored by a ContextPop
4188** opcode. The context stores the last insert row id, the last statement change
4189** count, and the current statement change count.
4190*/
danielk19777a5147c2005-03-29 13:07:00 +00004191case OP_ContextPush: { /* no-push */
drh344737f2004-09-19 00:50:20 +00004192 int i = p->contextStackTop++;
4193 Context *pContext;
danielk1977b28af712004-06-21 06:50:26 +00004194
drh344737f2004-09-19 00:50:20 +00004195 assert( i>=0 );
danielk1977b28af712004-06-21 06:50:26 +00004196 /* FIX ME: This should be allocated as part of the vdbe at compile-time */
drh344737f2004-09-19 00:50:20 +00004197 if( i>=p->contextStackDepth ){
4198 p->contextStackDepth = i+1;
danielk1977e7259292006-01-13 06:33:23 +00004199 sqliteReallocOrFree((void**)&p->contextStack, sizeof(Context)*(i+1));
drh344737f2004-09-19 00:50:20 +00004200 if( p->contextStack==0 ) goto no_mem;
4201 }
4202 pContext = &p->contextStack[i];
4203 pContext->lastRowid = db->lastRowid;
4204 pContext->nChange = p->nChange;
drha01f79d2005-07-08 13:07:59 +00004205 pContext->sFifo = p->sFifo;
4206 sqlite3VdbeFifoInit(&p->sFifo);
rdcb0c374f2004-02-20 22:53:38 +00004207 break;
4208}
4209
4210/* Opcode: ContextPop * * *
4211**
4212** Restore the Vdbe context to the state it was in when contextPush was last
4213** executed. The context stores the last insert row id, the last statement
4214** change count, and the current statement change count.
4215*/
danielk19777a5147c2005-03-29 13:07:00 +00004216case OP_ContextPop: { /* no-push */
drh344737f2004-09-19 00:50:20 +00004217 Context *pContext = &p->contextStack[--p->contextStackTop];
4218 assert( p->contextStackTop>=0 );
4219 db->lastRowid = pContext->lastRowid;
4220 p->nChange = pContext->nChange;
drha01f79d2005-07-08 13:07:59 +00004221 sqlite3VdbeFifoClear(&p->sFifo);
4222 p->sFifo = pContext->sFifo;
rdcb0c374f2004-02-20 22:53:38 +00004223 break;
4224}
danielk197793758c82005-01-21 08:13:14 +00004225#endif /* #ifndef SQLITE_OMIT_TRIGGER */
rdcb0c374f2004-02-20 22:53:38 +00004226
drh8721ce42001-11-07 14:22:00 +00004227/* Opcode: MemStore P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00004228**
drh8721ce42001-11-07 14:22:00 +00004229** Write the top of the stack into memory location P1.
4230** P1 should be a small integer since space is allocated
drh5e00f6c2001-09-13 13:46:56 +00004231** for all memory locations between 0 and P1 inclusive.
drh8721ce42001-11-07 14:22:00 +00004232**
4233** After the data is stored in the memory location, the
4234** stack is popped once if P2 is 1. If P2 is zero, then
4235** the original data remains on the stack.
drh5e00f6c2001-09-13 13:46:56 +00004236*/
danielk19777a5147c2005-03-29 13:07:00 +00004237case OP_MemStore: { /* no-push */
drh6810ce62004-01-31 19:22:56 +00004238 assert( pTos>=p->aStack );
drhfebe1062004-08-28 18:17:48 +00004239 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4240 rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos);
drh290c1942004-08-21 17:54:45 +00004241 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004242
drh290c1942004-08-21 17:54:45 +00004243 /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will
4244 ** restore the top of the stack to its original value.
4245 */
4246 if( pOp->p2 ){
4247 break;
4248 }
4249}
drh5e00f6c2001-09-13 13:46:56 +00004250/* Opcode: MemLoad P1 * *
4251**
4252** Push a copy of the value in memory location P1 onto the stack.
drh8721ce42001-11-07 14:22:00 +00004253**
4254** If the value is a string, then the value pushed is a pointer to
4255** the string that is stored in the memory location. If the memory
4256** location is subsequently changed (using OP_MemStore) then the
4257** value pushed onto the stack will change too.
drh5e00f6c2001-09-13 13:46:56 +00004258*/
4259case OP_MemLoad: {
drh5e00f6c2001-09-13 13:46:56 +00004260 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004261 assert( i>=0 && i<p->nMem );
4262 pTos++;
drhfebe1062004-08-28 18:17:48 +00004263 sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem);
drh5e00f6c2001-09-13 13:46:56 +00004264 break;
4265}
4266
drh205f48e2004-11-05 00:43:11 +00004267#ifndef SQLITE_OMIT_AUTOINCREMENT
4268/* Opcode: MemMax P1 * *
4269**
4270** Set the value of memory cell P1 to the maximum of its current value
4271** and the value on the top of the stack. The stack is unchanged.
4272**
4273** This instruction throws an error if the memory cell is not initially
4274** an integer.
4275*/
danielk19777a5147c2005-03-29 13:07:00 +00004276case OP_MemMax: { /* no-push */
drh205f48e2004-11-05 00:43:11 +00004277 int i = pOp->p1;
4278 Mem *pMem;
4279 assert( pTos>=p->aStack );
4280 assert( i>=0 && i<p->nMem );
4281 pMem = &p->aMem[i];
drh8a512562005-11-14 22:29:05 +00004282 sqlite3VdbeMemIntegerify(pMem);
4283 sqlite3VdbeMemIntegerify(pTos);
drh205f48e2004-11-05 00:43:11 +00004284 if( pMem->i<pTos->i){
4285 pMem->i = pTos->i;
4286 }
4287 break;
4288}
4289#endif /* SQLITE_OMIT_AUTOINCREMENT */
4290
drh15007a92006-01-08 18:10:17 +00004291/* Opcode: MemIncr P1 P2 *
drhd11d3822002-06-21 23:01:49 +00004292**
drh15007a92006-01-08 18:10:17 +00004293** Increment the integer valued memory cell P2 by the value in P1.
drhd11d3822002-06-21 23:01:49 +00004294**
drh6f58f702006-01-08 05:26:41 +00004295** It is illegal to use this instruction on a memory cell that does
4296** not contain an integer. An assertion fault will result if you try.
drhd11d3822002-06-21 23:01:49 +00004297*/
danielk19777a5147c2005-03-29 13:07:00 +00004298case OP_MemIncr: { /* no-push */
drh15007a92006-01-08 18:10:17 +00004299 int i = pOp->p2;
drhd11d3822002-06-21 23:01:49 +00004300 Mem *pMem;
drh6810ce62004-01-31 19:22:56 +00004301 assert( i>=0 && i<p->nMem );
drhd11d3822002-06-21 23:01:49 +00004302 pMem = &p->aMem[i];
drh6810ce62004-01-31 19:22:56 +00004303 assert( pMem->flags==MEM_Int );
drh15007a92006-01-08 18:10:17 +00004304 pMem->i += pOp->p1;
danielk1977a2dc3b12005-02-05 12:48:48 +00004305 break;
4306}
4307
4308/* Opcode: IfMemPos P1 P2 *
4309**
drh15007a92006-01-08 18:10:17 +00004310** If the value of memory cell P1 is 1 or greater, jump to P2.
drh6f58f702006-01-08 05:26:41 +00004311**
4312** It is illegal to use this instruction on a memory cell that does
4313** not contain an integer. An assertion fault will result if you try.
danielk1977a2dc3b12005-02-05 12:48:48 +00004314*/
danielk19777a5147c2005-03-29 13:07:00 +00004315case OP_IfMemPos: { /* no-push */
danielk1977a2dc3b12005-02-05 12:48:48 +00004316 int i = pOp->p1;
4317 Mem *pMem;
4318 assert( i>=0 && i<p->nMem );
4319 pMem = &p->aMem[i];
drh6f58f702006-01-08 05:26:41 +00004320 assert( pMem->flags==MEM_Int );
4321 if( pMem->i>0 ){
drhec7429a2005-10-06 16:53:14 +00004322 pc = pOp->p2 - 1;
4323 }
4324 break;
4325}
4326
drh15007a92006-01-08 18:10:17 +00004327/* Opcode: IfMemNeg P1 P2 *
4328**
4329** If the value of memory cell P1 is less than zero, jump to P2.
4330**
4331** It is illegal to use this instruction on a memory cell that does
4332** not contain an integer. An assertion fault will result if you try.
4333*/
4334case OP_IfMemNeg: { /* no-push */
4335 int i = pOp->p1;
4336 Mem *pMem;
4337 assert( i>=0 && i<p->nMem );
4338 pMem = &p->aMem[i];
4339 assert( pMem->flags==MEM_Int );
4340 if( pMem->i<0 ){
4341 pc = pOp->p2 - 1;
4342 }
4343 break;
4344}
4345
drhec7429a2005-10-06 16:53:14 +00004346/* Opcode: IfMemZero P1 P2 *
4347**
drh6f58f702006-01-08 05:26:41 +00004348** If the value of memory cell P1 is exactly 0, jump to P2.
4349**
4350** It is illegal to use this instruction on a memory cell that does
4351** not contain an integer. An assertion fault will result if you try.
drhec7429a2005-10-06 16:53:14 +00004352*/
4353case OP_IfMemZero: { /* no-push */
4354 int i = pOp->p1;
4355 Mem *pMem;
4356 assert( i>=0 && i<p->nMem );
4357 pMem = &p->aMem[i];
drh6f58f702006-01-08 05:26:41 +00004358 assert( pMem->flags==MEM_Int );
4359 if( pMem->i==0 ){
drhd11d3822002-06-21 23:01:49 +00004360 pc = pOp->p2 - 1;
4361 }
4362 break;
4363}
4364
drhd654be82005-09-20 17:42:23 +00004365/* Opcode: MemNull P1 * *
4366**
4367** Store a NULL in memory cell P1
4368*/
4369case OP_MemNull: {
4370 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4371 sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]);
4372 break;
4373}
4374
4375/* Opcode: MemInt P1 P2 *
4376**
4377** Store the integer value P1 in memory cell P2.
4378*/
4379case OP_MemInt: {
4380 assert( pOp->p2>=0 && pOp->p2<p->nMem );
4381 sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1);
4382 break;
4383}
4384
4385/* Opcode: MemMove P1 P2 *
4386**
4387** Move the content of memory cell P2 over to memory cell P1.
4388** Any prior content of P1 is erased. Memory cell P2 is left
4389** containing a NULL.
4390*/
4391case OP_MemMove: {
4392 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4393 assert( pOp->p2>=0 && pOp->p2<p->nMem );
4394 rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]);
4395 break;
4396}
4397
drh13449892005-09-07 21:22:45 +00004398/* Opcode: AggStep P1 P2 P3
drhe5095352002-02-24 03:25:14 +00004399**
drh0bce8352002-02-28 00:41:10 +00004400** Execute the step function for an aggregate. The
4401** function has P2 arguments. P3 is a pointer to the FuncDef
drh13449892005-09-07 21:22:45 +00004402** structure that specifies the function. Use memory location
4403** P1 as the accumulator.
drhe5095352002-02-24 03:25:14 +00004404**
drh13449892005-09-07 21:22:45 +00004405** The P2 arguments are popped from the stack.
drhe5095352002-02-24 03:25:14 +00004406*/
drh13449892005-09-07 21:22:45 +00004407case OP_AggStep: { /* no-push */
drhe5095352002-02-24 03:25:14 +00004408 int n = pOp->p2;
4409 int i;
drh6810ce62004-01-31 19:22:56 +00004410 Mem *pMem, *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004411 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004412 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004413
drh6810ce62004-01-31 19:22:56 +00004414 assert( n>=0 );
drh13449892005-09-07 21:22:45 +00004415 pRec = &pTos[1-n];
drh6810ce62004-01-31 19:22:56 +00004416 assert( pRec>=p->aStack );
danielk19776ddcca52004-05-24 23:48:25 +00004417 apVal = p->apArg;
4418 assert( apVal || n==0 );
drh6810ce62004-01-31 19:22:56 +00004419 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004420 apVal[i] = pRec;
drh8079a0d2006-01-12 17:20:50 +00004421 storeTypeInfo(pRec, encoding);
drhe5095352002-02-24 03:25:14 +00004422 }
drh0bce8352002-02-28 00:41:10 +00004423 ctx.pFunc = (FuncDef*)pOp->p3;
drh13449892005-09-07 21:22:45 +00004424 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4425 ctx.pMem = pMem = &p->aMem[pOp->p1];
drhabfcea22005-09-06 20:36:48 +00004426 pMem->n++;
drh90669c12006-01-20 15:45:36 +00004427 ctx.s.flags = MEM_Null;
4428 ctx.s.z = 0;
4429 ctx.s.xDel = 0;
drh1350b032002-02-27 19:00:20 +00004430 ctx.isError = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00004431 ctx.pColl = 0;
4432 if( ctx.pFunc->needCollSeq ){
4433 assert( pOp>p->aOp );
4434 assert( pOp[-1].p3type==P3_COLLSEQ );
4435 assert( pOp[-1].opcode==OP_CollSeq );
4436 ctx.pColl = (CollSeq *)pOp[-1].p3;
4437 }
danielk19776ddcca52004-05-24 23:48:25 +00004438 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh13449892005-09-07 21:22:45 +00004439 popStack(&pTos, n);
drh1350b032002-02-27 19:00:20 +00004440 if( ctx.isError ){
drh90669c12006-01-20 15:45:36 +00004441 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
drh1350b032002-02-27 19:00:20 +00004442 rc = SQLITE_ERROR;
4443 }
drh90669c12006-01-20 15:45:36 +00004444 sqlite3VdbeMemRelease(&ctx.s);
drh5e00f6c2001-09-13 13:46:56 +00004445 break;
4446}
4447
drha10a34b2005-09-07 22:09:48 +00004448/* Opcode: AggFinal P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00004449**
drh13449892005-09-07 21:22:45 +00004450** Execute the finalizer function for an aggregate. P1 is
4451** the memory location that is the accumulator for the aggregate.
drha10a34b2005-09-07 22:09:48 +00004452**
4453** P2 is the number of arguments that the step function takes and
4454** P3 is a pointer to the FuncDef for this function. The P2
4455** argument is not used by this opcode. It is only there to disambiguate
4456** functions that can take varying numbers of arguments. The
4457** P3 argument is only needed for the degenerate case where
4458** the step function was not previously called.
drh5e00f6c2001-09-13 13:46:56 +00004459*/
drh13449892005-09-07 21:22:45 +00004460case OP_AggFinal: { /* no-push */
4461 Mem *pMem;
4462 assert( pOp->p1>=0 && pOp->p1<p->nMem );
4463 pMem = &p->aMem[pOp->p1];
drha10a34b2005-09-07 22:09:48 +00004464 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
drh90669c12006-01-20 15:45:36 +00004465 rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
4466 if( rc==SQLITE_ERROR ){
4467 sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
4468 }
drh5e00f6c2001-09-13 13:46:56 +00004469 break;
4470}
4471
drh5e00f6c2001-09-13 13:46:56 +00004472
drh6f8c91c2003-12-07 00:24:35 +00004473/* Opcode: Vacuum * * *
4474**
4475** Vacuum the entire database. This opcode will cause other virtual
4476** machines to be created and run. It may not be called from within
4477** a transaction.
4478*/
danielk19777a5147c2005-03-29 13:07:00 +00004479case OP_Vacuum: { /* no-push */
danielk19774adee202004-05-08 08:23:19 +00004480 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4481 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4482 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004483 break;
4484}
4485
danielk1977a21c6b62005-01-24 10:25:59 +00004486/* Opcode: Expire P1 * *
4487**
4488** Cause precompiled statements to become expired. An expired statement
4489** fails with an error code of SQLITE_SCHEMA if it is ever executed
4490** (via sqlite3_step()).
4491**
4492** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4493** then only the currently executing statement is affected.
4494*/
danielk19777a5147c2005-03-29 13:07:00 +00004495case OP_Expire: { /* no-push */
danielk1977a21c6b62005-01-24 10:25:59 +00004496 if( !pOp->p1 ){
4497 sqlite3ExpirePreparedStatements(db);
4498 }else{
4499 p->expired = 1;
4500 }
4501 break;
4502}
4503
danielk1977c00da102006-01-07 13:21:04 +00004504#ifndef SQLITE_OMIT_SHARED_CACHE
4505/* Opcode: TableLock P1 P2 P3
4506**
4507** Obtain a lock on a particular table. This instruction is only used when
4508** the shared-cache feature is enabled.
4509**
drha154dcd2006-03-22 22:10:07 +00004510** If P1 is not negative, then it is the index of the database
danielk1977c00da102006-01-07 13:21:04 +00004511** in sqlite3.aDb[] and a read-lock is required. If P1 is negative, a
4512** write-lock is required. In this case the index of the database is the
4513** absolute value of P1 minus one (iDb = abs(P1) - 1;) and a write-lock is
4514** required.
4515**
4516** P2 contains the root-page of the table to lock.
4517**
4518** P3 contains a pointer to the name of the table being locked. This is only
4519** used to generate an error message if the lock cannot be obtained.
4520*/
4521case OP_TableLock: { /* no-push */
4522 int p1 = pOp->p1;
4523 u8 isWriteLock = (p1<0);
4524 if( isWriteLock ){
4525 p1 = (-1*p1)-1;
4526 }
4527 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4528 if( rc==SQLITE_LOCKED ){
4529 const char *z = (const char *)pOp->p3;
4530 sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
4531 }
4532 break;
4533}
drhb9bb7c12006-06-11 23:41:55 +00004534#endif /* SQLITE_OMIT_SHARED_CACHE */
4535
4536#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk197778efaba2006-06-12 06:09:17 +00004537/* Opcode: VCreate P1 * P3
drhb9bb7c12006-06-11 23:41:55 +00004538**
danielk197778efaba2006-06-12 06:09:17 +00004539** P3 is the name of a virtual table in database P1. Call the xCreate method
4540** for that table.
drhb9bb7c12006-06-11 23:41:55 +00004541*/
4542case OP_VCreate: {
danielk197778efaba2006-06-12 06:09:17 +00004543 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg);
drhb9bb7c12006-06-11 23:41:55 +00004544 break;
4545}
4546#endif /* SQLITE_OMIT_VIRTUALTABLE */
4547
4548#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19779e39ce82006-06-12 16:01:21 +00004549/* Opcode: VDestroy P1 * P3
drhb9bb7c12006-06-11 23:41:55 +00004550**
danielk19779e39ce82006-06-12 16:01:21 +00004551** P3 is the name of a virtual table in database P1. Call the xDestroy method
4552** of that table.
drhb9bb7c12006-06-11 23:41:55 +00004553*/
4554case OP_VDestroy: {
danielk19779e39ce82006-06-12 16:01:21 +00004555 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3);
drhb9bb7c12006-06-11 23:41:55 +00004556 break;
4557}
4558#endif /* SQLITE_OMIT_VIRTUALTABLE */
danielk1977c00da102006-01-07 13:21:04 +00004559
drh9eff6162006-06-12 21:59:13 +00004560#ifndef SQLITE_OMIT_VIRTUALTABLE
4561/* Opcode: VOpen P1 * P3
4562**
4563** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
4564** P1 is a cursor number. This opcode opens a cursor to the virtual
4565** table and stores that cursor in P1.
4566*/
4567case OP_VOpen: {
danielk1977b7a7b9a2006-06-13 10:24:42 +00004568 Cursor *pCur = 0;
4569 sqlite3_vtab_cursor *pVtabCursor = 0;
4570
4571 sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
4572 sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4573
4574 assert(pVtab && pModule);
4575 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4576 rc = pModule->xOpen(pVtab, &pVtabCursor);
4577 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4578 if( SQLITE_OK==rc ){
4579 /* Initialise sqlite3_vtab_cursor base class */
4580 pVtabCursor->pVtab = pVtab;
4581
4582 /* Initialise vdbe cursor object */
4583 pCur = allocateCursor(p, pOp->p1, -1);
4584 pCur->pVtabCursor = pVtabCursor;
4585 }
drh9eff6162006-06-12 21:59:13 +00004586 break;
4587}
4588#endif /* SQLITE_OMIT_VIRTUALTABLE */
4589
4590#ifndef SQLITE_OMIT_VIRTUALTABLE
danielk19775fac9f82006-06-13 14:16:58 +00004591/* Opcode: VFilter P1 P2 P3
drh9eff6162006-06-12 21:59:13 +00004592**
4593** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4594** the filtered result set is empty.
4595**
danielk19775fac9f82006-06-13 14:16:58 +00004596** P3 points to enough free space to use to marshall the arguments.
4597**
drh9eff6162006-06-12 21:59:13 +00004598** This opcode invokes the xFilter method on the virtual table specified
danielk1977be8a7832006-06-13 15:00:54 +00004599** by P1. The query plan parameter to xFilter is the top of the stack.
drh9eff6162006-06-12 21:59:13 +00004600** Next down on the stack is the argc parameter. Beneath the
4601** next of stack are argc additional parameters which are passed to
danielk1977b7a7b9a2006-06-13 10:24:42 +00004602** xFilter as argv. The topmost parameter (i.e. 3rd element popped from
4603** the stack) becomes argv[argc-1] when passed to xFilter.
4604**
danielk1977be8a7832006-06-13 15:00:54 +00004605** The query plan, argc, and all argv stack values are popped from the
danielk1977b7a7b9a2006-06-13 10:24:42 +00004606** stack before this instruction completes.
drh9eff6162006-06-12 21:59:13 +00004607**
4608** A jump is made to P2 if the result set after filtering would be
4609** empty.
4610*/
4611case OP_VFilter: {
danielk1977b7a7b9a2006-06-13 10:24:42 +00004612 int nArg;
4613
4614 const sqlite3_module *pModule;
4615
4616 Cursor *pCur = p->apCsr[pOp->p1];
4617 assert( pCur->pVtabCursor );
4618 pModule = pCur->pVtabCursor->pVtab->pModule;
4619
4620 /* Grab the index number and argc parameters off the top of the stack. */
4621 assert( (&pTos[-1])>=p->aStack );
danielk1977be8a7832006-06-13 15:00:54 +00004622 assert( pTos[0].flags&MEM_Blob && pTos[-1].flags==MEM_Int );
danielk1977b7a7b9a2006-06-13 10:24:42 +00004623 nArg = pTos[-1].i;
4624
4625 /* Invoke the xFilter method if one is defined. */
4626 if( pModule->xFilter ){
4627 int res;
danielk19775fac9f82006-06-13 14:16:58 +00004628 int ii;
danielk1977be8a7832006-06-13 15:00:54 +00004629 Mem **apArg = (Mem **)pOp->p3;
danielk19775fac9f82006-06-13 14:16:58 +00004630 for(ii = 0; ii<nArg; ii++){
4631 apArg[ii] = &pTos[ii+1-2-nArg];
4632 }
danielk1977b7a7b9a2006-06-13 10:24:42 +00004633
4634 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk1977be8a7832006-06-13 15:00:54 +00004635 res = pModule->xFilter(pCur->pVtabCursor, pTos->z, pTos->n, nArg, apArg);
danielk1977b7a7b9a2006-06-13 10:24:42 +00004636 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4637
4638 if( res==0 ){
4639 pc = pOp->p2 - 1;
4640 }
4641 }
4642
4643 /* Pop the index number, argc value and parameters off the stack */
4644 popStack(&pTos, 2+nArg);
drh9eff6162006-06-12 21:59:13 +00004645 break;
4646}
4647#endif /* SQLITE_OMIT_VIRTUALTABLE */
4648
4649#ifndef SQLITE_OMIT_VIRTUALTABLE
4650/* Opcode: VRowid P1 * *
4651**
4652** Push an integer onto the stack which is the rowid of
4653** the virtual-table that the P1 cursor is pointing to.
4654*/
4655case OP_VRowid: {
danielk1977b7a7b9a2006-06-13 10:24:42 +00004656 const sqlite3_module *pModule;
4657
4658 Cursor *pCur = p->apCsr[pOp->p1];
4659 assert( pCur->pVtabCursor );
4660 pModule = pCur->pVtabCursor->pVtab->pModule;
4661 if( pModule->xRowid==0 ){
4662 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0);
4663 rc = SQLITE_ERROR;
4664 } else {
4665 sqlite_int64 iRow;
4666
4667 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4668 rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
4669 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4670
4671 pTos++;
4672 pTos->flags = MEM_Int;
4673 pTos->i = iRow;
4674 }
4675
drh9eff6162006-06-12 21:59:13 +00004676 break;
4677}
4678#endif /* SQLITE_OMIT_VIRTUALTABLE */
4679
4680#ifndef SQLITE_OMIT_VIRTUALTABLE
4681/* Opcode: VColumn P1 P2 *
4682**
4683** Push onto the stack the value of the P2-th column of
4684** the row of the virtual-table that the P1 cursor is pointing to.
4685*/
4686case OP_VColumn: {
danielk1977b7a7b9a2006-06-13 10:24:42 +00004687 const sqlite3_module *pModule;
4688
4689 Cursor *pCur = p->apCsr[pOp->p1];
4690 assert( pCur->pVtabCursor );
4691 pModule = pCur->pVtabCursor->pVtab->pModule;
4692 if( pModule->xColumn==0 ){
4693 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0);
4694 rc = SQLITE_ERROR;
4695 } else {
4696 sqlite3_context sContext;
4697 memset(&sContext, 0, sizeof(sContext));
4698 sContext.s.flags = MEM_Null;
4699 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4700 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
4701
4702 /* Copy the result of the function to the top of the stack. We
4703 ** do this regardless of whether or not an error occured to ensure any
4704 ** dynamic allocation in sContext.s (a Mem struct) is released.
4705 */
4706 sqlite3VdbeChangeEncoding(&sContext.s, encoding);
4707 pTos++;
4708 pTos->flags = 0;
4709 sqlite3VdbeMemMove(pTos, &sContext.s);
4710
4711 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4712 }
4713
drh9eff6162006-06-12 21:59:13 +00004714 break;
4715}
4716#endif /* SQLITE_OMIT_VIRTUALTABLE */
4717
4718#ifndef SQLITE_OMIT_VIRTUALTABLE
4719/* Opcode: VNext P1 P2 *
4720**
4721** Advance virtual table P1 to the next row in its result set and
4722** jump to instruction P2. Or, if the virtual table has reached
4723** the end of its result set, then fall through to the next instruction.
4724*/
4725case OP_VNext: {
danielk1977b7a7b9a2006-06-13 10:24:42 +00004726 const sqlite3_module *pModule;
4727 int res = 0;
4728
4729 Cursor *pCur = p->apCsr[pOp->p1];
4730 assert( pCur->pVtabCursor );
4731 pModule = pCur->pVtabCursor->pVtab->pModule;
4732 if( pModule->xNext==0 ){
4733 sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0);
4734 rc = SQLITE_ERROR;
4735 } else {
4736 /* Invoke the xNext() method of the module. There is no way for the
4737 ** underlying implementation to return an error if one occurs during
4738 ** xNext(). Instead, if an error occurs, true is returned (indicating that
4739 ** data is available) and the error code returned when xColumn or
4740 ** some other method is next invoked on the save virtual table cursor.
4741 */
4742 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4743 res = pModule->xNext(pCur->pVtabCursor);
4744 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4745
4746 if( res ){
4747 /* If there is data (or an error), jump to P2 */
4748 pc = pOp->p2 - 1;
4749 }
4750 }
4751
drh9eff6162006-06-12 21:59:13 +00004752 break;
4753}
4754#endif /* SQLITE_OMIT_VIRTUALTABLE */
4755
drh5e00f6c2001-09-13 13:46:56 +00004756/* An other opcode is illegal...
4757*/
4758default: {
drha6110402005-07-28 20:51:19 +00004759 assert( 0 );
drh5e00f6c2001-09-13 13:46:56 +00004760 break;
4761}
4762
4763/*****************************************************************************
4764** The cases of the switch statement above this line should all be indented
4765** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4766** readability. From this point on down, the normal indentation rules are
4767** restored.
4768*****************************************************************************/
4769 }
drh6e142f52000-06-08 13:36:40 +00004770
danielk1977bc04f852005-03-29 08:26:13 +00004771 /* Make sure the stack limit was not exceeded */
4772 assert( pTos<=pStackLimit );
4773
drh7b396862003-01-01 23:06:20 +00004774#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00004775 {
4776 long long elapse = hwtime() - start;
4777 pOp->cycles += elapse;
4778 pOp->cnt++;
4779#if 0
4780 fprintf(stdout, "%10lld ", elapse);
danielk19774adee202004-05-08 08:23:19 +00004781 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00004782#endif
4783 }
drh7b396862003-01-01 23:06:20 +00004784#endif
4785
drh6e142f52000-06-08 13:36:40 +00004786 /* The following code adds nothing to the actual functionality
4787 ** of the program. It is only here for testing and debugging.
4788 ** On the other hand, it does burn CPU cycles every time through
4789 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4790 */
4791#ifndef NDEBUG
drh3914aed2004-01-31 20:40:42 +00004792 /* Sanity checking on the top element of the stack */
4793 if( pTos>=p->aStack ){
drh74161702006-02-24 02:53:49 +00004794 sqlite3VdbeMemSanity(pTos);
drh3914aed2004-01-31 20:40:42 +00004795 }
drha6110402005-07-28 20:51:19 +00004796 assert( pc>=-1 && pc<p->nOp );
danielk1977b5402fb2005-01-12 07:15:04 +00004797#ifdef SQLITE_DEBUG
4798 /* Code for tracing the vdbe stack. */
drh6810ce62004-01-31 19:22:56 +00004799 if( p->trace && pTos>=p->aStack ){
drh75897232000-05-29 14:26:00 +00004800 int i;
4801 fprintf(p->trace, "Stack:");
drh6810ce62004-01-31 19:22:56 +00004802 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4803 if( pTos[i].flags & MEM_Null ){
drhc61053b2000-06-04 12:58:36 +00004804 fprintf(p->trace, " NULL");
drh6810ce62004-01-31 19:22:56 +00004805 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drha3b321d2004-05-11 09:31:31 +00004806 fprintf(p->trace, " si:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00004807 }else if( pTos[i].flags & MEM_Int ){
drha3b321d2004-05-11 09:31:31 +00004808 fprintf(p->trace, " i:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00004809 }else if( pTos[i].flags & MEM_Real ){
4810 fprintf(p->trace, " r:%g", pTos[i].r);
drh75897232000-05-29 14:26:00 +00004811 }else{
danielk1977ca6b2912004-05-21 10:49:47 +00004812 char zBuf[100];
drh74161702006-02-24 02:53:49 +00004813 sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf);
danielk1977ca6b2912004-05-21 10:49:47 +00004814 fprintf(p->trace, " ");
danielk1977ec8450f2004-06-19 09:35:36 +00004815 fprintf(p->trace, "%s", zBuf);
drh75897232000-05-29 14:26:00 +00004816 }
4817 }
drh7bc09d32002-11-01 01:55:36 +00004818 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
drh75897232000-05-29 14:26:00 +00004819 fprintf(p->trace,"\n");
4820 }
danielk1977b5402fb2005-01-12 07:15:04 +00004821#endif /* SQLITE_DEBUG */
4822#endif /* NDEBUG */
drhb86ccfb2003-01-28 23:13:10 +00004823 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00004824
drhb86ccfb2003-01-28 23:13:10 +00004825 /* If we reach this point, it means that execution is finished.
4826 */
4827vdbe_halt:
4828 if( rc ){
4829 p->rc = rc;
4830 rc = SQLITE_ERROR;
4831 }else{
4832 rc = SQLITE_DONE;
4833 }
drh92f02c32004-09-02 14:57:08 +00004834 sqlite3VdbeHalt(p);
drh6810ce62004-01-31 19:22:56 +00004835 p->pTos = pTos;
drhb86ccfb2003-01-28 23:13:10 +00004836 return rc;
4837
4838 /* Jump to here if a malloc() fails. It's hard to get a malloc()
4839 ** to fail on a modern VM computer, so this code is untested.
4840 */
4841no_mem:
danielk19774adee202004-05-08 08:23:19 +00004842 sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00004843 rc = SQLITE_NOMEM;
4844 goto vdbe_halt;
4845
4846 /* Jump to here for an SQLITE_MISUSE error.
4847 */
4848abort_due_to_misuse:
4849 rc = SQLITE_MISUSE;
4850 /* Fall thru into abort_due_to_error */
4851
4852 /* Jump to here for any other kind of fatal error. The "rc" variable
4853 ** should hold the error number.
4854 */
4855abort_due_to_error:
drh483750b2003-01-29 18:46:51 +00004856 if( p->zErrMsg==0 ){
danielk19779e128002006-01-18 16:51:35 +00004857 if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM;
danielk1977f20b21c2004-05-31 23:56:42 +00004858 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
drh483750b2003-01-29 18:46:51 +00004859 }
drhb86ccfb2003-01-28 23:13:10 +00004860 goto vdbe_halt;
4861
danielk19776f8a5032004-05-10 10:34:51 +00004862 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00004863 ** flag.
4864 */
4865abort_due_to_interrupt:
4866 assert( db->flags & SQLITE_Interrupt );
4867 db->flags &= ~SQLITE_Interrupt;
4868 if( db->magic!=SQLITE_MAGIC_BUSY ){
4869 rc = SQLITE_MISUSE;
4870 }else{
4871 rc = SQLITE_INTERRUPT;
4872 }
danielk1977026d2702004-06-14 13:14:59 +00004873 p->rc = rc;
danielk1977f20b21c2004-05-31 23:56:42 +00004874 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00004875 goto vdbe_halt;
drhb86ccfb2003-01-28 23:13:10 +00004876}