blob: e695961b97004f13a79f5182870edfb3c17a74d8 [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**
danielk1977cbb18d22004-05-28 11:37:27 +000046** $Id: vdbe.c,v 1.344 2004/05/28 11:37:28 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/*
drh6810ce62004-01-31 19:22:56 +000073** Release the memory associated with the given stack level. This
74** leaves the Mem.flags field in an inconsistent state.
drhc61053b2000-06-04 12:58:36 +000075*/
drh6810ce62004-01-31 19:22:56 +000076#define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); }
77
78/*
danielk1977bd7e4602004-05-24 07:34:48 +000079** Convert the given stack entity into a string if it isn't one
80** already. Return non-zero if a malloc() fails.
81*/
82#define Stringify(P, enc) \
drhf4479502004-05-27 03:12:53 +000083 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
84 { goto no_mem; }
danielk1977bd7e4602004-05-24 07:34:48 +000085
86/*
87** Convert the given stack entity into a string that has been obtained
88** from sqliteMalloc(). This is different from Stringify() above in that
89** Stringify() will use the NBFS bytes of static string space if the string
90** will fit but this routine always mallocs for space.
91** Return non-zero if we run out of memory.
92*/
drheb2e1762004-05-27 01:53:56 +000093#define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
94
danielk1977bd7e4602004-05-24 07:34:48 +000095
96/*
97** An ephemeral string value (signified by the MEM_Ephem flag) contains
98** a pointer to a dynamically allocated string where some other entity
99** is responsible for deallocating that string. Because the stack entry
100** does not control the string, it might be deleted without the stack
101** entry knowing it.
102**
103** This routine converts an ephemeral string into a dynamically allocated
104** string that the stack entry itself controls. In other words, it
105** converts an MEM_Ephem string into an MEM_Dyn string.
106*/
107#define Deephemeralize(P) \
drheb2e1762004-05-27 01:53:56 +0000108 if( ((P)->flags&MEM_Ephem)!=0 \
109 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
danielk197793d46752004-05-23 13:30:58 +0000110
111/*
danielk19778a6b5412004-05-24 07:04:25 +0000112** Convert the given stack entity into a integer if it isn't one
113** already.
114**
115** Any prior string or real representation is invalidated.
116** NULLs are converted into 0.
117*/
118#define Integerify(P, enc) \
drheb2e1762004-05-27 01:53:56 +0000119 if((P)->flags!=MEM_Int){ sqlite3VdbeMemIntegerify(P); }
danielk19778a6b5412004-05-24 07:04:25 +0000120
121/*
122** Get a valid Real representation for the given stack element.
123**
124** Any prior string or integer representation is retained.
125** NULLs are converted into 0.0.
126*/
drheb2e1762004-05-27 01:53:56 +0000127#define Realify(P,enc) \
128 if(((P)->flags&MEM_Real)==0){ sqlite3VdbeMemRealify(P); }
danielk19778a6b5412004-05-24 07:04:25 +0000129
danielk1977c572ef72004-05-27 09:28:41 +0000130/*
131** Argument pMem points at a memory cell that will be passed to a
132** user-defined function or returned to the user as the result of a query.
133** The second argument, 'db_enc' is the text encoding used by the vdbe for
134** stack variables. This routine sets the pMem->enc and pMem->type
135** variables used by the sqlite3_value_*() routines.
136*/
137static void StoreTypeInfo(Mem *pMem, u8 db_enc){
138 int flags = pMem->flags;
139 if( flags & MEM_Null ){
140 pMem->type = SQLITE3_NULL;
141 }
142 else if( flags & MEM_Int ){
143 pMem->type = SQLITE3_INTEGER;
144 }
145 else if( flags & MEM_Real ){
146 pMem->type = SQLITE3_FLOAT;
147 }
148 else if( flags & MEM_Str ){
149 pMem->type = SQLITE3_TEXT;
150 }else{
151 pMem->type = SQLITE3_BLOB;
152 }
153}
danielk19778a6b5412004-05-24 07:04:25 +0000154
155/*
danielk1977106bb232004-05-21 10:08:53 +0000156** Insert a new aggregate element and make it the element that
157** has focus.
158**
159** Return 0 on success and 1 if memory is exhausted.
160*/
161static int AggInsert(Agg *p, char *zKey, int nKey){
162 AggElem *pElem, *pOld;
163 int i;
164 Mem *pMem;
165 pElem = sqliteMalloc( sizeof(AggElem) + nKey +
166 (p->nMem-1)*sizeof(pElem->aMem[0]) );
167 if( pElem==0 ) return 1;
168 pElem->zKey = (char*)&pElem->aMem[p->nMem];
169 memcpy(pElem->zKey, zKey, nKey);
170 pElem->nKey = nKey;
171 pOld = sqlite3HashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
172 if( pOld!=0 ){
173 assert( pOld==pElem ); /* Malloc failed on insert */
174 sqliteFree(pOld);
175 return 0;
176 }
177 for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
178 pMem->flags = MEM_Null;
179 }
180 p->pCurrent = pElem;
181 return 0;
182}
183
184/*
185** Get the AggElem currently in focus
186*/
187#define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P)))
188static AggElem *_AggInFocus(Agg *p){
189 HashElem *pElem = sqliteHashFirst(&p->hash);
190 if( pElem==0 ){
191 AggInsert(p,"",1);
192 pElem = sqliteHashFirst(&p->hash);
193 }
194 return pElem ? sqliteHashData(pElem) : 0;
195}
196
197/*
danielk1977106bb232004-05-21 10:08:53 +0000198** Pop the stack N times.
199*/
200static void popStack(Mem **ppTos, int N){
201 Mem *pTos = *ppTos;
202 while( N>0 ){
203 N--;
204 Release(pTos);
205 pTos--;
206 }
207 *ppTos = pTos;
208}
209
210/*
drh75897232000-05-29 14:26:00 +0000211** The parameters are pointers to the head of two sorted lists
212** of Sorter structures. Merge these two lists together and return
213** a single sorted list. This routine forms the core of the merge-sort
214** algorithm.
215**
216** In the case of a tie, left sorts in front of right.
217*/
drhffbc3082004-05-21 01:29:06 +0000218static Sorter *Merge(Sorter *pLeft, Sorter *pRight, KeyInfo *pKeyInfo){
drh75897232000-05-29 14:26:00 +0000219 Sorter sHead;
220 Sorter *pTail;
221 pTail = &sHead;
222 pTail->pNext = 0;
223 while( pLeft && pRight ){
drhffbc3082004-05-21 01:29:06 +0000224 int c = sqlite3VdbeKeyCompare(pKeyInfo, pLeft->nKey, pLeft->zKey,
225 pRight->nKey, pRight->zKey);
226 /* int c = sqlite3SortCompare(pLeft->zKey, pRight->zKey); */
drh75897232000-05-29 14:26:00 +0000227 if( c<=0 ){
228 pTail->pNext = pLeft;
229 pLeft = pLeft->pNext;
230 }else{
231 pTail->pNext = pRight;
232 pRight = pRight->pNext;
233 }
234 pTail = pTail->pNext;
235 }
236 if( pLeft ){
237 pTail->pNext = pLeft;
238 }else if( pRight ){
239 pTail->pNext = pRight;
240 }
241 return sHead.pNext;
242}
243
drh75897232000-05-29 14:26:00 +0000244/*
drh8c74a8c2002-08-25 19:20:40 +0000245** Make sure there is space in the Vdbe structure to hold at least
246** mxCursor cursors. If there is not currently enough space, then
247** allocate more.
248**
249** If a memory allocation error occurs, return 1. Return 0 if
250** everything works.
251*/
252static int expandCursorArraySize(Vdbe *p, int mxCursor){
253 if( mxCursor>=p->nCursor ){
drhd7556d22004-05-14 21:59:40 +0000254 p->apCsr = sqliteRealloc( p->apCsr, (mxCursor+1)*sizeof(Cursor*) );
255 if( p->apCsr==0 ) return 1;
256 while( p->nCursor<=mxCursor ){
257 Cursor *pC;
258 p->apCsr[p->nCursor++] = pC = sqliteMalloc( sizeof(Cursor) );
259 if( pC==0 ) return 1;
260 }
drh8c74a8c2002-08-25 19:20:40 +0000261 }
262 return 0;
263}
264
danielk19773d1bfea2004-05-14 11:00:53 +0000265/*
266** Apply any conversion required by the supplied column affinity to
267** memory cell pRec. affinity may be one of:
268**
danielk1977a37cdde2004-05-16 11:15:36 +0000269** SQLITE_AFF_NUMERIC
danielk19773d1bfea2004-05-14 11:00:53 +0000270** SQLITE_AFF_TEXT
271** SQLITE_AFF_NONE
272** SQLITE_AFF_INTEGER
273**
274*/
danielk19778a6b5412004-05-24 07:04:25 +0000275static void applyAffinity(Mem *pRec, char affinity, u8 enc){
danielk19773d1bfea2004-05-14 11:00:53 +0000276 switch( affinity ){
danielk1977a37cdde2004-05-16 11:15:36 +0000277 case SQLITE_AFF_INTEGER:
278 case SQLITE_AFF_NUMERIC:
danielk19773d1bfea2004-05-14 11:00:53 +0000279 if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){
280 /* pRec does not have a valid integer or real representation.
281 ** Attempt a conversion if pRec has a string representation and
282 ** it looks like a number.
283 */
284 int realnum;
danielk1977c572ef72004-05-27 09:28:41 +0000285 sqlite3VdbeMemNulTerminate(pRec);
danielk19778a6b5412004-05-24 07:04:25 +0000286 if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum, enc) ){
danielk19773d1bfea2004-05-14 11:00:53 +0000287 if( realnum ){
danielk19778a6b5412004-05-24 07:04:25 +0000288 Realify(pRec, enc);
danielk19773d1bfea2004-05-14 11:00:53 +0000289 }else{
danielk19778a6b5412004-05-24 07:04:25 +0000290 Integerify(pRec, enc);
danielk19773d1bfea2004-05-14 11:00:53 +0000291 }
292 }
293 }
danielk1977a37cdde2004-05-16 11:15:36 +0000294
295 if( affinity==SQLITE_AFF_INTEGER ){
296 /* For INTEGER affinity, try to convert a real value to an int */
danielk1977c572ef72004-05-27 09:28:41 +0000297 if( (pRec->flags&MEM_Real) && !(pRec->flags&MEM_Int) ){
danielk1977a37cdde2004-05-16 11:15:36 +0000298 pRec->i = pRec->r;
299 if( ((double)pRec->i)==pRec->r ){
300 pRec->flags |= MEM_Int;
301 }
302 }
303 }
danielk19773d1bfea2004-05-14 11:00:53 +0000304 break;
danielk1977a37cdde2004-05-16 11:15:36 +0000305
306 case SQLITE_AFF_TEXT:
danielk19773d1bfea2004-05-14 11:00:53 +0000307 /* Only attempt the conversion if there is an integer or real
308 ** representation (blob and NULL do not get converted) but no string
309 ** representation.
310 */
311 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
drhf4479502004-05-27 03:12:53 +0000312 sqlite3VdbeMemStringify(pRec, enc);
danielk19773d1bfea2004-05-14 11:00:53 +0000313 }
314 pRec->flags &= ~(MEM_Real|MEM_Int);
315
316 break;
317
danielk19773d1bfea2004-05-14 11:00:53 +0000318 case SQLITE_AFF_NONE:
danielk1977a37cdde2004-05-16 11:15:36 +0000319 /* Affinity NONE. Do nothing. */
danielk19773d1bfea2004-05-14 11:00:53 +0000320 break;
danielk1977a37cdde2004-05-16 11:15:36 +0000321
danielk19773d1bfea2004-05-14 11:00:53 +0000322 default:
323 assert(0);
324 }
325}
326
danielk1977b1bc9532004-05-22 03:05:33 +0000327#ifndef NDEBUG
drhb6f54522004-05-20 02:42:16 +0000328/*
danielk1977ca6b2912004-05-21 10:49:47 +0000329** Write a nice string representation of the contents of cell pMem
330** into buffer zBuf, length nBuf.
331*/
danielk1977ca6b2912004-05-21 10:49:47 +0000332void prettyPrintMem(Mem *pMem, char *zBuf, int nBuf){
333 char *zCsr = zBuf;
334 int f = pMem->flags;
335
336 if( f&MEM_Blob ){
337 int i;
338 char c;
339 if( f & MEM_Dyn ){
340 c = 'z';
341 assert( (f & (MEM_Static|MEM_Ephem))==0 );
342 }else if( f & MEM_Static ){
343 c = 't';
344 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
345 }else if( f & MEM_Ephem ){
346 c = 'e';
347 assert( (f & (MEM_Static|MEM_Dyn))==0 );
348 }else{
349 c = 's';
350 }
351
danielk1977b1bc9532004-05-22 03:05:33 +0000352 zCsr += sprintf(zCsr, "%c", c);
353 zCsr += sprintf(zCsr, "%d[", pMem->n);
danielk1977ca6b2912004-05-21 10:49:47 +0000354 for(i=0; i<16 && i<pMem->n; i++){
355 zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
356 }
357 for(i=0; i<16 && i<pMem->n; i++){
358 char z = pMem->z[i];
359 if( z<32 || z>126 ) *zCsr++ = '.';
360 else *zCsr++ = z;
361 }
362
363 zCsr += sprintf(zCsr, "]");
danielk1977b1bc9532004-05-22 03:05:33 +0000364 *zCsr = '\0';
365 }else if( f & MEM_Str ){
366 int j, k;
367 zBuf[0] = ' ';
368 if( f & MEM_Dyn ){
369 zBuf[1] = 'z';
370 assert( (f & (MEM_Static|MEM_Ephem))==0 );
371 }else if( f & MEM_Static ){
372 zBuf[1] = 't';
373 assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
374 }else if( f & MEM_Ephem ){
375 zBuf[1] = 'e';
376 assert( (f & (MEM_Static|MEM_Dyn))==0 );
377 }else{
378 zBuf[1] = 's';
379 }
380 k = 2;
381 k += sprintf(&zBuf[k], "%d", pMem->n);
382 zBuf[k++] = '[';
383 for(j=0; j<15 && j<pMem->n; j++){
384 u8 c = pMem->z[j];
danielk1977b1bc9532004-05-22 03:05:33 +0000385/*
danielk19778a6b5412004-05-24 07:04:25 +0000386 if( c==0 && j==pMem->n-1 ) break;
danielk1977b1bc9532004-05-22 03:05:33 +0000387 zBuf[k++] = "0123456789ABCDEF"[c>>4];
388 zBuf[k++] = "0123456789ABCDEF"[c&0xf];
389*/
390 if( c>=0x20 && c<0x7f ){
391 zBuf[k++] = c;
392 }else{
393 zBuf[k++] = '.';
394 }
395 }
396 zBuf[k++] = ']';
397 zBuf[k++] = 0;
danielk1977ca6b2912004-05-21 10:49:47 +0000398 }
danielk1977ca6b2912004-05-21 10:49:47 +0000399}
danielk1977b1bc9532004-05-22 03:05:33 +0000400
401/* Temporary - this is useful in conjunction with prettyPrintMem whilst
402** debugging.
403*/
404char zGdbBuf[100];
danielk1977ca6b2912004-05-21 10:49:47 +0000405#endif
406
danielk197784ac9d02004-05-18 09:58:06 +0000407
drh7b396862003-01-01 23:06:20 +0000408#ifdef VDBE_PROFILE
409/*
410** The following routine only works on pentium-class processors.
411** It uses the RDTSC opcode to read cycle count value out of the
412** processor and returns that value. This can be used for high-res
413** profiling.
414*/
415__inline__ unsigned long long int hwtime(void){
416 unsigned long long int x;
417 __asm__("rdtsc\n\t"
418 "mov %%edx, %%ecx\n\t"
419 :"=A" (x));
420 return x;
421}
422#endif
423
drh8c74a8c2002-08-25 19:20:40 +0000424/*
drhcaec2f12003-01-07 02:47:47 +0000425** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000426** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000427** processing of the VDBE program is interrupted.
428**
429** This macro added to every instruction that does a jump in order to
430** implement a loop. This test used to be on every single instruction,
431** but that meant we more testing that we needed. By only testing the
432** flag on jump instructions, we get a (small) speed improvement.
433*/
434#define CHECK_FOR_INTERRUPT \
435 if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
436
437
438/*
drhb86ccfb2003-01-28 23:13:10 +0000439** Execute as much of a VDBE program as we can then return.
440**
danielk19774adee202004-05-08 08:23:19 +0000441** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000442** close the program with a final OP_Halt and to set up the callbacks
443** and the error message pointer.
444**
445** Whenever a row or result data is available, this routine will either
446** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000447** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000448**
449** If an attempt is made to open a locked database, then this routine
450** will either invoke the busy callback (if there is one) or it will
451** return SQLITE_BUSY.
452**
453** If an error occurs, an error message is written to memory obtained
454** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
455** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
456**
457** If the callback ever returns non-zero, then the program exits
458** immediately. There will be no error message but the p->rc field is
459** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
460**
drh9468c7f2003-03-07 19:50:07 +0000461** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
462** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000463**
464** Other fatal errors return SQLITE_ERROR.
465**
danielk19774adee202004-05-08 08:23:19 +0000466** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000467** used to clean up the mess that was left behind.
468*/
danielk19774adee202004-05-08 08:23:19 +0000469int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000470 Vdbe *p /* The VDBE */
471){
472 int pc; /* The program counter */
473 Op *pOp; /* Current operation */
474 int rc = SQLITE_OK; /* Value to return */
drhb86ccfb2003-01-28 23:13:10 +0000475 sqlite *db = p->db; /* The database */
drh6810ce62004-01-31 19:22:56 +0000476 Mem *pTos; /* Top entry in the operand stack */
drhb86ccfb2003-01-28 23:13:10 +0000477 char zBuf[100]; /* Space to sprintf() an integer */
478#ifdef VDBE_PROFILE
479 unsigned long long start; /* CPU clock count at start of opcode */
480 int origPc; /* Program counter at start of opcode */
481#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000482#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
483 int nProgressOps = 0; /* Opcodes executed since progress callback. */
484#endif
drhb86ccfb2003-01-28 23:13:10 +0000485
486 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
487 assert( db->magic==SQLITE_MAGIC_BUSY );
drh3a840692003-01-29 22:58:26 +0000488 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
489 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000490 assert( p->explain==0 );
danielk19776f8a5032004-05-10 10:34:51 +0000491 if( sqlite3_malloc_failed ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +0000492 pTos = p->pTos;
drhb86ccfb2003-01-28 23:13:10 +0000493 if( p->popStack ){
drh6810ce62004-01-31 19:22:56 +0000494 popStack(&pTos, p->popStack);
drhb86ccfb2003-01-28 23:13:10 +0000495 p->popStack = 0;
496 }
danielk1977106bb232004-05-21 10:08:53 +0000497 p->resOnStack = 0;
drh93581642004-02-12 13:02:55 +0000498 CHECK_FOR_INTERRUPT;
drhb86ccfb2003-01-28 23:13:10 +0000499 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000500 assert( pc>=0 && pc<p->nOp );
drh6810ce62004-01-31 19:22:56 +0000501 assert( pTos<=&p->aStack[pc] );
drh7b396862003-01-01 23:06:20 +0000502#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000503 origPc = pc;
drh7b396862003-01-01 23:06:20 +0000504 start = hwtime();
505#endif
drh75897232000-05-29 14:26:00 +0000506 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000507
508 /* Only allow tracing if NDEBUG is not defined.
509 */
510#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000511 if( p->trace ){
danielk19774adee202004-05-08 08:23:19 +0000512 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000513 }
drh6e142f52000-06-08 13:36:40 +0000514#endif
515
drhf6038712004-02-08 18:07:34 +0000516 /* Check to see if we need to simulate an interrupt. This only happens
517 ** if we have a special test build.
518 */
519#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000520 if( sqlite3_interrupt_count>0 ){
521 sqlite3_interrupt_count--;
522 if( sqlite3_interrupt_count==0 ){
523 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000524 }
525 }
526#endif
527
danielk1977348bb5d2003-10-18 09:37:26 +0000528#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
529 /* Call the progress callback if it is configured and the required number
530 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000531 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000532 ** If the progress callback returns non-zero, exit the virtual machine with
533 ** a return code SQLITE_ABORT.
534 */
drh3914aed2004-01-31 20:40:42 +0000535 if( db->xProgress ){
536 if( db->nProgressOps==nProgressOps ){
537 if( db->xProgress(db->pProgressArg)!=0 ){
538 rc = SQLITE_ABORT;
539 continue; /* skip to the next iteration of the for loop */
540 }
541 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000542 }
drh3914aed2004-01-31 20:40:42 +0000543 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000544 }
danielk1977348bb5d2003-10-18 09:37:26 +0000545#endif
546
drh75897232000-05-29 14:26:00 +0000547 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000548
drh5e00f6c2001-09-13 13:46:56 +0000549/*****************************************************************************
550** What follows is a massive switch statement where each case implements a
551** separate instruction in the virtual machine. If we follow the usual
552** indentation conventions, each case should be indented by 6 spaces. But
553** that is a lot of wasted space on the left margin. So the code within
554** the switch statement will break with convention and be flush-left. Another
555** big comment (similar to this one) will mark the point in the code where
556** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000557**
558** The formatting of each case is important. The makefile for SQLite
559** generates two C files "opcodes.h" and "opcodes.c" by scanning this
560** file looking for lines that begin with "case OP_". The opcodes.h files
561** will be filled with #defines that give unique integer values to each
562** opcode and the opcodes.c file is filled with an array of strings where
563** each string is the symbolic name for the corresponding opcode.
564**
565** Documentation about VDBE opcodes is generated by scanning this file
566** for lines of that contain "Opcode:". That line and all subsequent
567** comment lines are used in the generation of the opcode.html documentation
568** file.
569**
570** SUMMARY:
571**
572** Formatting is important to scripts that scan this file.
573** Do not deviate from the formatting style currently in use.
574**
drh5e00f6c2001-09-13 13:46:56 +0000575*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000576
drh58a11682001-11-10 13:51:08 +0000577/* Opcode: Goto * P2 *
drh5e00f6c2001-09-13 13:46:56 +0000578**
579** An unconditional jump to address P2.
580** The next instruction executed will be
581** the one at index P2 from the beginning of
582** the program.
583*/
584case OP_Goto: {
drhcaec2f12003-01-07 02:47:47 +0000585 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000586 pc = pOp->p2 - 1;
587 break;
588}
drh75897232000-05-29 14:26:00 +0000589
drh8c74a8c2002-08-25 19:20:40 +0000590/* Opcode: Gosub * P2 *
591**
592** Push the current address plus 1 onto the return address stack
593** and then jump to address P2.
594**
595** The return address stack is of limited depth. If too many
596** OP_Gosub operations occur without intervening OP_Returns, then
597** the return address stack will fill up and processing will abort
598** with a fatal error.
599*/
600case OP_Gosub: {
drhb86ccfb2003-01-28 23:13:10 +0000601 if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
danielk19774adee202004-05-08 08:23:19 +0000602 sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000603 p->rc = SQLITE_INTERNAL;
604 return SQLITE_ERROR;
drh8c74a8c2002-08-25 19:20:40 +0000605 }
drhb86ccfb2003-01-28 23:13:10 +0000606 p->returnStack[p->returnDepth++] = pc+1;
drh8c74a8c2002-08-25 19:20:40 +0000607 pc = pOp->p2 - 1;
608 break;
609}
610
611/* Opcode: Return * * *
612**
613** Jump immediately to the next instruction after the last unreturned
614** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
615** processing aborts with a fatal error.
616*/
617case OP_Return: {
drhb86ccfb2003-01-28 23:13:10 +0000618 if( p->returnDepth<=0 ){
danielk19774adee202004-05-08 08:23:19 +0000619 sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000620 p->rc = SQLITE_INTERNAL;
621 return SQLITE_ERROR;
drh8c74a8c2002-08-25 19:20:40 +0000622 }
drhb86ccfb2003-01-28 23:13:10 +0000623 p->returnDepth--;
624 pc = p->returnStack[p->returnDepth] - 1;
drh8c74a8c2002-08-25 19:20:40 +0000625 break;
626}
627
drh1c928532002-01-31 15:54:21 +0000628/* Opcode: Halt P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000629**
drhb19a2bc2001-09-16 00:13:26 +0000630** Exit immediately. All open cursors, Lists, Sorts, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000631** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000632**
danielk19776f8a5032004-05-10 10:34:51 +0000633** P1 is the result code returned by sqlite3_exec(). For a normal
drh9cfcf5d2002-01-29 18:41:24 +0000634** halt, this should be SQLITE_OK (0). For errors, it can be some
drh1c928532002-01-31 15:54:21 +0000635** other value. If P1!=0 then P2 will determine whether or not to
636** rollback the current transaction. Do not rollback if P2==OE_Fail.
637** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back
638** out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000639** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000640**
641** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000642** every program. So a jump past the last instruction of the program
643** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000644*/
645case OP_Halt: {
drh483750b2003-01-29 18:46:51 +0000646 p->magic = VDBE_MAGIC_HALT;
drh6810ce62004-01-31 19:22:56 +0000647 p->pTos = pTos;
drh9cfcf5d2002-01-29 18:41:24 +0000648 if( pOp->p1!=SQLITE_OK ){
drhb86ccfb2003-01-28 23:13:10 +0000649 p->rc = pOp->p1;
650 p->errorAction = pOp->p2;
danielk19776f349032002-06-11 02:25:40 +0000651 if( pOp->p3 ){
danielk19774adee202004-05-08 08:23:19 +0000652 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
danielk19776f349032002-06-11 02:25:40 +0000653 }
drh483750b2003-01-29 18:46:51 +0000654 return SQLITE_ERROR;
drh9cfcf5d2002-01-29 18:41:24 +0000655 }else{
drhb86ccfb2003-01-28 23:13:10 +0000656 p->rc = SQLITE_OK;
drh483750b2003-01-29 18:46:51 +0000657 return SQLITE_DONE;
drh9cfcf5d2002-01-29 18:41:24 +0000658 }
drh5e00f6c2001-09-13 13:46:56 +0000659}
drhc61053b2000-06-04 12:58:36 +0000660
drhe6840902002-03-06 03:08:25 +0000661/* Opcode: Integer P1 * P3
drh5e00f6c2001-09-13 13:46:56 +0000662**
drhe6840902002-03-06 03:08:25 +0000663** The integer value P1 is pushed onto the stack. If P3 is not zero
664** then it is assumed to be a string representation of the same integer.
drhfec19aa2004-05-19 20:41:03 +0000665** If P1 is zero and P3 is not zero, then the value is derived from P3.
drh5e00f6c2001-09-13 13:46:56 +0000666*/
drhf4479502004-05-27 03:12:53 +0000667case OP_Integer: {
drh6810ce62004-01-31 19:22:56 +0000668 pTos++;
drhf4479502004-05-27 03:12:53 +0000669 if( pOp->p3==0 ){
drh4f26d6c2004-05-26 23:25:30 +0000670 pTos->flags = MEM_Int;
danielk197793d46752004-05-23 13:30:58 +0000671 pTos->i = pOp->p1;
drhf4479502004-05-27 03:12:53 +0000672 pTos->type = SQLITE3_INTEGER;
673 }else{
674 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
675 pTos->z = pOp->p3;
676 pTos->n = strlen(pTos->z);
677 pTos->enc = TEXT_Utf8;
678 Integerify(pTos, 0);
drh4f26d6c2004-05-26 23:25:30 +0000679 }
drhf4479502004-05-27 03:12:53 +0000680 break;
681}
drh4f26d6c2004-05-26 23:25:30 +0000682
drhf4479502004-05-27 03:12:53 +0000683/* Opcode: Real * * P3
684**
685** The string value P3 is converted to a real and pushed on to the stack.
686*/
687case OP_Real: {
688 pTos++;
689 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
690 pTos->z = pOp->p3;
691 pTos->n = strlen(pTos->z);
692 pTos->enc = TEXT_Utf8;
693 Realify(pTos, 0);
694 break;
695}
danielk1977cbb18d22004-05-28 11:37:27 +0000696
697#if 0
698/* Opcode: String8 * * P3
699**
700** This opcode does not exist at vdbe execution time.
701*/
702case OP_String8: {
703 break;
704}
705#endif
drhf4479502004-05-27 03:12:53 +0000706
707/* Opcode: String * * P3
708**
709** The string value P3 is pushed onto the stack. If P3==0 then a
danielk1977cbb18d22004-05-28 11:37:27 +0000710** NULL is pushed onto the stack. P3 is assumed to be a nul terminated
711** string encoded with the database native encoding.
drhf4479502004-05-27 03:12:53 +0000712*/
713case OP_String: {
714 pTos++;
danielk1977c572ef72004-05-27 09:28:41 +0000715 if( pOp->p3 ){
716 pTos->flags = MEM_Str|MEM_Static|MEM_Term;
danielk1977c572ef72004-05-27 09:28:41 +0000717 pTos->z = pOp->p3;
718 pTos->n = strlen(pTos->z);
danielk1977cbb18d22004-05-28 11:37:27 +0000719 pTos->enc = TEXT_Utf8;
danielk1977c572ef72004-05-27 09:28:41 +0000720 sqlite3VdbeChangeEncoding(pTos, db->enc);
danielk1977cbb18d22004-05-28 11:37:27 +0000721/*
722 if( db->enc==TEXT_Utf8 ){
723 pTos->n = strlen(pTos->z);
724 }else{
725 pTos->n = sqlite3utf16ByteLen(pTos->z, -1);
726 }
727 pTos->enc = db->enc;
728*/
danielk1977c572ef72004-05-27 09:28:41 +0000729 }else{
730 pTos->flags = MEM_Null;
731 }
732 break;
733}
734
735#if 0
736/* Opcode: HexBlob * * P3
737**
738** This opcode does not exist at vdbe execution time.
739*/
740case OP_HexBlob: {
741 break;
742}
743#endif
744
745/* Opcode: Blob P1 * P3
746**
747** P3 points to a blob of data P1 bytes long. Push this
danielk1977cbb18d22004-05-28 11:37:27 +0000748** value onto the stack. This instruction is not coded directly
749** by the compiler. Instead, the compiler layer specifies
750** an OP_HexBlob opcode, with the hex string representation of
751** the blob as P3. This opcode is transformed to an OP_Blob
752** before execution (within the sqlite3_prepare() function).
danielk1977c572ef72004-05-27 09:28:41 +0000753*/
754case OP_Blob: {
755 pTos++;
756 sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +0000757 break;
758}
759
drh50457892003-09-06 01:10:47 +0000760/* Opcode: Variable P1 * *
761**
762** Push the value of variable P1 onto the stack. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000763** an unknown in the original SQL string as handed to sqlite3_compile().
drh7c972de2003-09-06 22:18:07 +0000764** Any occurance of the '?' character in the original SQL is considered
765** a variable. Variables in the SQL string are number from left to
766** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000767** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000768*/
769case OP_Variable: {
drh7c972de2003-09-06 22:18:07 +0000770 int j = pOp->p1 - 1;
danielk1977295ba552004-05-19 10:34:51 +0000771 assert( j>=0 && j<p->nVar );
772
danielk1977295ba552004-05-19 10:34:51 +0000773 pTos++;
danielk197793d46752004-05-23 13:30:58 +0000774 memcpy(pTos, &p->apVar[j], sizeof(*pTos)-NBFS);
danielk1977295ba552004-05-19 10:34:51 +0000775 if( pTos->flags&(MEM_Str|MEM_Blob) ){
776 pTos->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Short);
777 pTos->flags |= MEM_Static;
778 }
danielk197793d46752004-05-23 13:30:58 +0000779 break;
780}
danielk1977295ba552004-05-19 10:34:51 +0000781
danielk197793d46752004-05-23 13:30:58 +0000782/* Opcode: Utf16le_8 * * *
783**
784** The element on the top of the stack must be a little-endian UTF-16
785** encoded string. It is translated in-place to UTF-8.
786*/
787case OP_Utf16le_8: {
788 rc = SQLITE_INTERNAL;
789 break;
790}
791
792/* Opcode: Utf16be_8 * * *
793**
794** The element on the top of the stack must be a big-endian UTF-16
795** encoded string. It is translated in-place to UTF-8.
796*/
797case OP_Utf16be_8: {
798 rc = SQLITE_INTERNAL;
799 break;
800}
801
802/* Opcode: Utf8_16be * * *
803**
804** The element on the top of the stack must be a UTF-8 encoded
805** string. It is translated to big-endian UTF-16.
806*/
807case OP_Utf8_16be: {
808 rc = SQLITE_INTERNAL;
809 break;
810}
811
812/* Opcode: Utf8_16le * * *
813**
814** The element on the top of the stack must be a UTF-8 encoded
815** string. It is translated to little-endian UTF-16.
816*/
817case OP_Utf8_16le: {
818 rc = SQLITE_INTERNAL;
819 break;
820}
821
822/*
823** Opcode: UtfSwab
824**
825** The element on the top of the stack must be an UTF-16 encoded
826** string. Every second byte is exchanged, so as to translate
827** the string from little-endian to big-endian or vice versa.
828*/
829case OP_UtfSwab: {
830 rc = SQLITE_INTERNAL;
drh50457892003-09-06 01:10:47 +0000831 break;
832}
833
drh5e00f6c2001-09-13 13:46:56 +0000834/* Opcode: Pop P1 * *
835**
836** P1 elements are popped off of the top of stack and discarded.
837*/
838case OP_Pop: {
drh6810ce62004-01-31 19:22:56 +0000839 assert( pOp->p1>=0 );
840 popStack(&pTos, pOp->p1);
841 assert( pTos>=&p->aStack[-1] );
drh5e00f6c2001-09-13 13:46:56 +0000842 break;
843}
drh75897232000-05-29 14:26:00 +0000844
drh9cfcf5d2002-01-29 18:41:24 +0000845/* Opcode: Dup P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000846**
847** A copy of the P1-th element of the stack
848** is made and pushed onto the top of the stack.
849** The top of the stack is element 0. So the
850** instruction "Dup 0 0 0" will make a copy of the
851** top of the stack.
drhb19a2bc2001-09-16 00:13:26 +0000852**
drh9cfcf5d2002-01-29 18:41:24 +0000853** If the content of the P1-th element is a dynamically
854** allocated string, then a new copy of that string
855** is made if P2==0. If P2!=0, then just a pointer
856** to the string is copied.
857**
drhb19a2bc2001-09-16 00:13:26 +0000858** Also see the Pull instruction.
drh5e00f6c2001-09-13 13:46:56 +0000859*/
860case OP_Dup: {
drh6810ce62004-01-31 19:22:56 +0000861 Mem *pFrom = &pTos[-pOp->p1];
862 assert( pFrom<=pTos && pFrom>=p->aStack );
863 pTos++;
864 memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
danielk1977106bb232004-05-21 10:08:53 +0000865 if( pTos->flags & (MEM_Str|MEM_Blob) ){
drh6810ce62004-01-31 19:22:56 +0000866 if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
867 pTos->flags &= ~MEM_Dyn;
868 pTos->flags |= MEM_Ephem;
869 }else if( pTos->flags & MEM_Short ){
drhf4479502004-05-27 03:12:53 +0000870 memcpy(pTos->zShort, pFrom->zShort, pTos->n+2);
drh6810ce62004-01-31 19:22:56 +0000871 pTos->z = pTos->zShort;
872 }else if( (pTos->flags & MEM_Static)==0 ){
drhf4479502004-05-27 03:12:53 +0000873 pTos->z = sqliteMallocRaw(pFrom->n+2);
danielk19776f8a5032004-05-10 10:34:51 +0000874 if( sqlite3_malloc_failed ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +0000875 memcpy(pTos->z, pFrom->z, pFrom->n);
drhf4479502004-05-27 03:12:53 +0000876 memcpy(&pTos->z[pTos->n], "\0", 2);
drh6810ce62004-01-31 19:22:56 +0000877 pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
drhf4479502004-05-27 03:12:53 +0000878 pTos->flags |= MEM_Dyn|MEM_Term;
drh9bbca4c2001-11-06 04:00:18 +0000879 }
drh5e00f6c2001-09-13 13:46:56 +0000880 }
881 break;
882}
drh75897232000-05-29 14:26:00 +0000883
drh5e00f6c2001-09-13 13:46:56 +0000884/* Opcode: Pull P1 * *
885**
886** The P1-th element is removed from its current location on
887** the stack and pushed back on top of the stack. The
888** top of the stack is element 0, so "Pull 0 0 0" is
drhb19a2bc2001-09-16 00:13:26 +0000889** a no-op. "Pull 1 0 0" swaps the top two elements of
890** the stack.
891**
892** See also the Dup instruction.
drh5e00f6c2001-09-13 13:46:56 +0000893*/
894case OP_Pull: {
drh6810ce62004-01-31 19:22:56 +0000895 Mem *pFrom = &pTos[-pOp->p1];
drh5e00f6c2001-09-13 13:46:56 +0000896 int i;
drh00706be2004-01-30 14:49:16 +0000897 Mem ts;
drh6810ce62004-01-31 19:22:56 +0000898
drh6810ce62004-01-31 19:22:56 +0000899 ts = *pFrom;
900 Deephemeralize(pTos);
901 for(i=0; i<pOp->p1; i++, pFrom++){
902 Deephemeralize(&pFrom[1]);
drh6810ce62004-01-31 19:22:56 +0000903 assert( (pFrom->flags & MEM_Ephem)==0 );
danielk197718f41892004-05-22 07:27:46 +0000904 *pFrom = pFrom[1];
drh6810ce62004-01-31 19:22:56 +0000905 if( pFrom->flags & MEM_Short ){
danielk1977106bb232004-05-21 10:08:53 +0000906 assert( pFrom->flags & (MEM_Str|MEM_Blob) );
drh6810ce62004-01-31 19:22:56 +0000907 assert( pFrom->z==pFrom[1].zShort );
drh6810ce62004-01-31 19:22:56 +0000908 pFrom->z = pFrom->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000909 }
drh5e00f6c2001-09-13 13:46:56 +0000910 }
drh6810ce62004-01-31 19:22:56 +0000911 *pTos = ts;
drh6810ce62004-01-31 19:22:56 +0000912 if( pTos->flags & MEM_Short ){
danielk1977106bb232004-05-21 10:08:53 +0000913 assert( pTos->flags & (MEM_Str|MEM_Blob) );
drh6810ce62004-01-31 19:22:56 +0000914 assert( pTos->z==pTos[-pOp->p1].zShort );
drh6810ce62004-01-31 19:22:56 +0000915 pTos->z = pTos->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000916 }
drh5e00f6c2001-09-13 13:46:56 +0000917 break;
918}
drh75897232000-05-29 14:26:00 +0000919
drh9cfcf5d2002-01-29 18:41:24 +0000920/* Opcode: Push P1 * *
921**
922** Overwrite the value of the P1-th element down on the
923** stack (P1==0 is the top of the stack) with the value
drhac82fcf2002-09-08 17:23:41 +0000924** of the top of the stack. Then pop the top of the stack.
drh9cfcf5d2002-01-29 18:41:24 +0000925*/
926case OP_Push: {
drh6810ce62004-01-31 19:22:56 +0000927 Mem *pTo = &pTos[-pOp->p1];
drh0ca3e242002-01-29 23:07:02 +0000928
drh6810ce62004-01-31 19:22:56 +0000929 assert( pTo>=p->aStack );
930 Deephemeralize(pTos);
931 Release(pTo);
932 *pTo = *pTos;
933 if( pTo->flags & MEM_Short ){
934 assert( pTo->z==pTos->zShort );
935 pTo->z = pTo->zShort;
drh9cfcf5d2002-01-29 18:41:24 +0000936 }
drh6810ce62004-01-31 19:22:56 +0000937 pTos--;
drh9cfcf5d2002-01-29 18:41:24 +0000938 break;
939}
940
drh6810ce62004-01-31 19:22:56 +0000941
drhd6502752004-02-16 03:44:01 +0000942/* Opcode: ColumnName P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +0000943**
944** P3 becomes the P1-th column name (first is 0). An array of pointers
945** to all column names is passed as the 4th parameter to the callback.
drhd6502752004-02-16 03:44:01 +0000946** If P2==1 then this is the last column in the result set and thus the
947** number of columns in the result set will be P1. There must be at least
948** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
949** number of columns specified in OP_Callback must one more than the P1
950** value of the OP_ColumnName that has P2==1.
drh5e00f6c2001-09-13 13:46:56 +0000951*/
952case OP_ColumnName: {
danielk19773cf86062004-05-26 10:11:05 +0000953 assert(0);
drh001bbcb2003-03-19 03:14:00 +0000954 assert( pOp->p1>=0 && pOp->p1<p->nOp );
drhb1363202002-06-26 02:45:03 +0000955 p->azColName[pOp->p1] = pOp->p3;
drh6a535342001-10-19 16:44:56 +0000956 p->nCallback = 0;
danielk197722322fd2004-05-25 23:35:17 +0000957 assert( !pOp->p2 || p->nResColumn==(pOp->p1+1) );
958 /* if( pOp->p2 ) p->nResColumn = pOp->p1+1; */
drh5e00f6c2001-09-13 13:46:56 +0000959 break;
960}
drhc61053b2000-06-04 12:58:36 +0000961
drhdf199a22002-06-14 22:38:41 +0000962/* Opcode: Callback P1 * *
drh5e00f6c2001-09-13 13:46:56 +0000963**
964** Pop P1 values off the stack and form them into an array. Then
965** invoke the callback function using the newly formed array as the
966** 3rd parameter.
967*/
968case OP_Callback: {
danielk19778a6b5412004-05-24 07:04:25 +0000969 int i;
drhd6502752004-02-16 03:44:01 +0000970 assert( p->nResColumn==pOp->p1 );
danielk19778a6b5412004-05-24 07:04:25 +0000971
972 for(i=0; i<pOp->p1; i++){
973 Mem *pVal = &pTos[0-i];
drh4f26d6c2004-05-26 23:25:30 +0000974 sqlite3VdbeMemNulTerminate(pVal);
danielk1977c572ef72004-05-27 09:28:41 +0000975 StoreTypeInfo(pVal, db->enc);
danielk19778a6b5412004-05-24 07:04:25 +0000976 }
977
978 p->resOnStack = 1;
979 p->nCallback++;
drh826fb5a2004-02-14 23:59:57 +0000980 p->popStack = pOp->p1;
981 p->pc = pc + 1;
982 p->pTos = pTos;
983 return SQLITE_ROW;
drh5e00f6c2001-09-13 13:46:56 +0000984}
drh75897232000-05-29 14:26:00 +0000985
drh5e00f6c2001-09-13 13:46:56 +0000986/* Opcode: Concat P1 P2 P3
987**
988** Look at the first P1 elements of the stack. Append them all
989** together with the lowest element first. Use P3 as a separator.
990** Put the result on the top of the stack. The original P1 elements
drha9f9d1c2002-06-29 02:20:08 +0000991** are popped from the stack if P2==0 and retained if P2==1. If
992** any element of the stack is NULL, then the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +0000993**
994** If P3 is NULL, then use no separator. When P1==1, this routine
995** makes a copy of the top stack element into memory obtained
996** from sqliteMalloc().
997*/
998case OP_Concat: {
999 char *zNew;
1000 int nByte;
1001 int nField;
1002 int i, j;
drh6810ce62004-01-31 19:22:56 +00001003 Mem *pTerm;
drheb2e1762004-05-27 01:53:56 +00001004 Mem mSep; /* Memory cell containing the seperator string, if any */
danielk19778a6b5412004-05-24 07:04:25 +00001005
1006 /* FIX ME: Eventually, P3 will be in database native encoding. But for
1007 ** now it is always UTF-8. So set up zSep to hold the native encoding of
1008 ** P3.
1009 */
1010 if( pOp->p3 ){
drheb2e1762004-05-27 01:53:56 +00001011 mSep.z = pOp->p3;
1012 mSep.n = strlen(mSep.z);
1013 mSep.flags = MEM_Str|MEM_Static|MEM_Term;
1014 mSep.enc = TEXT_Utf8;
1015 sqlite3VdbeChangeEncoding(&mSep, db->enc);
danielk19778a6b5412004-05-24 07:04:25 +00001016 }else{
drheb2e1762004-05-27 01:53:56 +00001017 mSep.flags = MEM_Null;
danielk1977c572ef72004-05-27 09:28:41 +00001018 mSep.n = 0;
danielk19778a6b5412004-05-24 07:04:25 +00001019 }
1020
1021 /* Loop through the stack elements to see how long the result will be. */
drh5e00f6c2001-09-13 13:46:56 +00001022 nField = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00001023 pTerm = &pTos[1-nField];
drheb2e1762004-05-27 01:53:56 +00001024 nByte = (nField-1)*mSep.n;
drh6810ce62004-01-31 19:22:56 +00001025 for(i=0; i<nField; i++, pTerm++){
danielk19778a6b5412004-05-24 07:04:25 +00001026 assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
1027 if( pTerm->flags&MEM_Null ){
drha9f9d1c2002-06-29 02:20:08 +00001028 nByte = -1;
1029 break;
drh5e00f6c2001-09-13 13:46:56 +00001030 }
danielk19778a6b5412004-05-24 07:04:25 +00001031 Stringify(pTerm, db->enc);
drheb2e1762004-05-27 01:53:56 +00001032 nByte += pTerm->n;
drh5e00f6c2001-09-13 13:46:56 +00001033 }
danielk19778a6b5412004-05-24 07:04:25 +00001034
drha9f9d1c2002-06-29 02:20:08 +00001035 if( nByte<0 ){
danielk19778a6b5412004-05-24 07:04:25 +00001036 /* If nByte is less than zero, then there is a NULL value on the stack.
1037 ** In this case just pop the values off the stack (if required) and
1038 ** push on a NULL.
1039 */
drh6810ce62004-01-31 19:22:56 +00001040 if( pOp->p2==0 ){
1041 popStack(&pTos, nField);
1042 }
1043 pTos++;
1044 pTos->flags = MEM_Null;
danielk19778a6b5412004-05-24 07:04:25 +00001045 }else{
1046 /* Otherwise malloc() space for the result and concatenate all the
1047 ** stack values.
1048 */
drheb2e1762004-05-27 01:53:56 +00001049 zNew = sqliteMallocRaw( nByte+2 );
danielk19778a6b5412004-05-24 07:04:25 +00001050 if( zNew==0 ) goto no_mem;
1051 j = 0;
1052 pTerm = &pTos[1-nField];
1053 for(i=j=0; i<nField; i++, pTerm++){
drheb2e1762004-05-27 01:53:56 +00001054 int n = pTerm->n;
danielk19778a6b5412004-05-24 07:04:25 +00001055 assert( pTerm->flags & MEM_Str );
1056 memcpy(&zNew[j], pTerm->z, n);
1057 j += n;
drheb2e1762004-05-27 01:53:56 +00001058 if( i<nField-1 && !(mSep.flags|MEM_Null) ){
1059 memcpy(&zNew[j], mSep.z, mSep.n);
1060 j += mSep.n;
danielk19778a6b5412004-05-24 07:04:25 +00001061 }
drh5e00f6c2001-09-13 13:46:56 +00001062 }
drheb2e1762004-05-27 01:53:56 +00001063 zNew[j] = 0;
1064 zNew[j+1] = 0;
danielk1977c572ef72004-05-27 09:28:41 +00001065 assert( j==nByte );
danielk19778a6b5412004-05-24 07:04:25 +00001066
1067 if( pOp->p2==0 ){
1068 popStack(&pTos, nField);
1069 }
1070 pTos++;
drheb2e1762004-05-27 01:53:56 +00001071 pTos->n = j;
drhf4479502004-05-27 03:12:53 +00001072 pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
drheb2e1762004-05-27 01:53:56 +00001073 pTos->enc = db->enc;
1074 pTos->type = SQLITE3_TEXT;
danielk19778a6b5412004-05-24 07:04:25 +00001075 pTos->z = zNew;
drh5e00f6c2001-09-13 13:46:56 +00001076 }
drh5e00f6c2001-09-13 13:46:56 +00001077 break;
1078}
drh75897232000-05-29 14:26:00 +00001079
drh5e00f6c2001-09-13 13:46:56 +00001080/* Opcode: Add * * *
1081**
1082** Pop the top two elements from the stack, add them together,
1083** and push the result back onto the stack. If either element
1084** is a string then it is converted to a double using the atof()
1085** function before the addition.
drhf5905aa2002-05-26 20:54:33 +00001086** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001087*/
1088/* Opcode: Multiply * * *
1089**
1090** Pop the top two elements from the stack, multiply them together,
1091** and push the result back onto the stack. If either element
1092** is a string then it is converted to a double using the atof()
1093** function before the multiplication.
drhf5905aa2002-05-26 20:54:33 +00001094** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001095*/
1096/* Opcode: Subtract * * *
1097**
1098** Pop the top two elements from the stack, subtract the
1099** first (what was on top of the stack) from the second (the
1100** next on stack)
1101** and push the result back onto the stack. If either element
1102** is a string then it is converted to a double using the atof()
1103** function before the subtraction.
drhf5905aa2002-05-26 20:54:33 +00001104** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001105*/
1106/* Opcode: Divide * * *
1107**
1108** Pop the top two elements from the stack, divide the
1109** first (what was on top of the stack) from the second (the
1110** next on stack)
1111** and push the result back onto the stack. If either element
1112** is a string then it is converted to a double using the atof()
1113** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001114** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001115*/
drhbf4133c2001-10-13 02:59:08 +00001116/* Opcode: Remainder * * *
1117**
1118** Pop the top two elements from the stack, divide the
1119** first (what was on top of the stack) from the second (the
1120** next on stack)
1121** and push the remainder after division onto the stack. If either element
1122** is a string then it is converted to a double using the atof()
1123** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001124** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001125*/
drh5e00f6c2001-09-13 13:46:56 +00001126case OP_Add:
1127case OP_Subtract:
1128case OP_Multiply:
drhbf4133c2001-10-13 02:59:08 +00001129case OP_Divide:
1130case OP_Remainder: {
drh6810ce62004-01-31 19:22:56 +00001131 Mem *pNos = &pTos[-1];
1132 assert( pNos>=p->aStack );
1133 if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
1134 Release(pTos);
1135 pTos--;
1136 Release(pTos);
1137 pTos->flags = MEM_Null;
1138 }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001139 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001140 a = pTos->i;
1141 b = pNos->i;
drh5e00f6c2001-09-13 13:46:56 +00001142 switch( pOp->opcode ){
1143 case OP_Add: b += a; break;
1144 case OP_Subtract: b -= a; break;
1145 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001146 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001147 if( a==0 ) goto divide_by_zero;
1148 b /= a;
drh75897232000-05-29 14:26:00 +00001149 break;
1150 }
drhbf4133c2001-10-13 02:59:08 +00001151 default: {
1152 if( a==0 ) goto divide_by_zero;
1153 b %= a;
1154 break;
1155 }
drh75897232000-05-29 14:26:00 +00001156 }
drh6810ce62004-01-31 19:22:56 +00001157 Release(pTos);
1158 pTos--;
1159 Release(pTos);
1160 pTos->i = b;
1161 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001162 }else{
1163 double a, b;
danielk19778a6b5412004-05-24 07:04:25 +00001164 Realify(pTos, db->enc);
1165 Realify(pNos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001166 a = pTos->r;
1167 b = pNos->r;
drh5e00f6c2001-09-13 13:46:56 +00001168 switch( pOp->opcode ){
1169 case OP_Add: b += a; break;
1170 case OP_Subtract: b -= a; break;
1171 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001172 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001173 if( a==0.0 ) goto divide_by_zero;
1174 b /= a;
1175 break;
1176 }
drhbf4133c2001-10-13 02:59:08 +00001177 default: {
drh1ab43002002-01-14 09:28:19 +00001178 int ia = (int)a;
1179 int ib = (int)b;
drhbf4133c2001-10-13 02:59:08 +00001180 if( ia==0.0 ) goto divide_by_zero;
1181 b = ib % ia;
1182 break;
1183 }
drh5e00f6c2001-09-13 13:46:56 +00001184 }
drh6810ce62004-01-31 19:22:56 +00001185 Release(pTos);
1186 pTos--;
1187 Release(pTos);
1188 pTos->r = b;
1189 pTos->flags = MEM_Real;
drh5e00f6c2001-09-13 13:46:56 +00001190 }
1191 break;
1192
1193divide_by_zero:
drh6810ce62004-01-31 19:22:56 +00001194 Release(pTos);
1195 pTos--;
1196 Release(pTos);
1197 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00001198 break;
1199}
1200
drh0bce8352002-02-28 00:41:10 +00001201/* Opcode: Function P1 * P3
drh8e0a2f92002-02-23 23:45:45 +00001202**
drh0bce8352002-02-28 00:41:10 +00001203** Invoke a user function (P3 is a pointer to a Function structure that
drh1350b032002-02-27 19:00:20 +00001204** defines the function) with P1 string arguments taken from the stack.
1205** Pop all arguments from the stack and push back the result.
1206**
1207** See also: AggFunc
drh8e0a2f92002-02-23 23:45:45 +00001208*/
drh0bce8352002-02-28 00:41:10 +00001209case OP_Function: {
danielk197751ad0ec2004-05-24 12:39:02 +00001210 int i;
drh6810ce62004-01-31 19:22:56 +00001211 Mem *pArg;
danielk197722322fd2004-05-25 23:35:17 +00001212 sqlite3_context ctx;
danielk197751ad0ec2004-05-24 12:39:02 +00001213 sqlite3_value **apVal;
1214 int n = pOp->p1;
drh1350b032002-02-27 19:00:20 +00001215
drh8e0a2f92002-02-23 23:45:45 +00001216 n = pOp->p1;
danielk19776ddcca52004-05-24 23:48:25 +00001217 apVal = p->apArg;
danielk197751ad0ec2004-05-24 12:39:02 +00001218 assert( apVal || n==0 );
1219
drh6810ce62004-01-31 19:22:56 +00001220 pArg = &pTos[1-n];
drh6810ce62004-01-31 19:22:56 +00001221 for(i=0; i<n; i++, pArg++){
danielk197751ad0ec2004-05-24 12:39:02 +00001222 apVal[i] = pArg;
danielk1977c572ef72004-05-27 09:28:41 +00001223 StoreTypeInfo(pArg, db->enc);
drh8e0a2f92002-02-23 23:45:45 +00001224 }
danielk197751ad0ec2004-05-24 12:39:02 +00001225
drh0bce8352002-02-28 00:41:10 +00001226 ctx.pFunc = (FuncDef*)pOp->p3;
drh00706be2004-01-30 14:49:16 +00001227 ctx.s.flags = MEM_Null;
1228 ctx.s.z = 0;
drh8e0a2f92002-02-23 23:45:45 +00001229 ctx.isError = 0;
drh1350b032002-02-27 19:00:20 +00001230 ctx.isStep = 0;
danielk19774adee202004-05-08 08:23:19 +00001231 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
danielk197751ad0ec2004-05-24 12:39:02 +00001232 (*ctx.pFunc->xFunc)(&ctx, n, apVal);
danielk19774adee202004-05-08 08:23:19 +00001233 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6810ce62004-01-31 19:22:56 +00001234 popStack(&pTos, n);
danielk19777e18c252004-05-25 11:47:24 +00001235
1236 /* Copy the result of the function to the top of the stack */
drh6810ce62004-01-31 19:22:56 +00001237 pTos++;
1238 *pTos = ctx.s;
1239 if( pTos->flags & MEM_Short ){
1240 pTos->z = pTos->zShort;
drh8e0a2f92002-02-23 23:45:45 +00001241 }
danielk19777e18c252004-05-25 11:47:24 +00001242 /* If the function returned an error, throw an exception */
drh8e0a2f92002-02-23 23:45:45 +00001243 if( ctx.isError ){
danielk19774adee202004-05-08 08:23:19 +00001244 sqlite3SetString(&p->zErrMsg,
drh6810ce62004-01-31 19:22:56 +00001245 (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
drh8e0a2f92002-02-23 23:45:45 +00001246 rc = SQLITE_ERROR;
1247 }
1248 break;
1249}
1250
drhbf4133c2001-10-13 02:59:08 +00001251/* Opcode: BitAnd * * *
1252**
1253** Pop the top two elements from the stack. Convert both elements
1254** to integers. Push back onto the stack the bit-wise AND of the
1255** two elements.
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: BitOr * * *
1259**
1260** Pop the top two elements from the stack. Convert both elements
1261** to integers. Push back onto the stack the bit-wise OR of the
1262** two elements.
drhf5905aa2002-05-26 20:54:33 +00001263** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001264*/
1265/* Opcode: ShiftLeft * * *
1266**
1267** Pop the top two elements from the stack. Convert both elements
1268** to integers. Push back onto the stack the top element shifted
1269** left by N bits where N is the second element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001270** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001271*/
1272/* Opcode: ShiftRight * * *
1273**
1274** Pop the top two elements from the stack. Convert both elements
1275** to integers. Push back onto the stack the top element shifted
1276** right by N bits where N is the second element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001277** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001278*/
1279case OP_BitAnd:
1280case OP_BitOr:
1281case OP_ShiftLeft:
1282case OP_ShiftRight: {
drh6810ce62004-01-31 19:22:56 +00001283 Mem *pNos = &pTos[-1];
drhbf4133c2001-10-13 02:59:08 +00001284 int a, b;
drh6810ce62004-01-31 19:22:56 +00001285
1286 assert( pNos>=p->aStack );
1287 if( (pTos->flags | pNos->flags) & MEM_Null ){
1288 popStack(&pTos, 2);
1289 pTos++;
1290 pTos->flags = MEM_Null;
drhf5905aa2002-05-26 20:54:33 +00001291 break;
1292 }
danielk19778a6b5412004-05-24 07:04:25 +00001293 Integerify(pTos, db->enc);
1294 Integerify(pNos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001295 a = pTos->i;
1296 b = pNos->i;
drhbf4133c2001-10-13 02:59:08 +00001297 switch( pOp->opcode ){
1298 case OP_BitAnd: a &= b; break;
1299 case OP_BitOr: a |= b; break;
1300 case OP_ShiftLeft: a <<= b; break;
1301 case OP_ShiftRight: a >>= b; break;
1302 default: /* CANT HAPPEN */ break;
1303 }
danielk19778a6b5412004-05-24 07:04:25 +00001304 /* FIX ME: Because constant P3 values sometimes need to be translated,
1305 ** the following assert() can fail. When P3 is always in the native text
1306 ** encoding, this assert() will be valid again. Until then, the Release()
1307 ** is neeed instead.
drh6810ce62004-01-31 19:22:56 +00001308 assert( (pTos->flags & MEM_Dyn)==0 );
1309 assert( (pNos->flags & MEM_Dyn)==0 );
danielk19778a6b5412004-05-24 07:04:25 +00001310 */
1311 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001312 pTos--;
drh79f14b72004-03-03 01:51:24 +00001313 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001314 pTos->i = a;
drh79f14b72004-03-03 01:51:24 +00001315 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001316 break;
1317}
1318
drh5e00f6c2001-09-13 13:46:56 +00001319/* Opcode: AddImm P1 * *
1320**
drh4a324312001-12-21 14:30:42 +00001321** Add the value P1 to whatever is on top of the stack. The result
1322** is always an integer.
1323**
1324** To force the top of the stack to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001325*/
1326case OP_AddImm: {
drh6810ce62004-01-31 19:22:56 +00001327 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00001328 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001329 pTos->i += pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00001330 break;
1331}
1332
drh751f4122004-01-14 21:59:22 +00001333/* Opcode: ForceInt P1 P2 *
drh1dd59e02003-07-06 17:22:25 +00001334**
drh751f4122004-01-14 21:59:22 +00001335** Convert the top of the stack into an integer. If the current top of
1336** the stack is not numeric (meaning that is is a NULL or a string that
1337** does not look like an integer or floating point number) then pop the
1338** stack and jump to P2. If the top of the stack is numeric then
1339** convert it into the least integer that is greater than or equal to its
1340** current value if P1==0, or to the least integer that is strictly
1341** greater than its current value if P1==1.
drh1dd59e02003-07-06 17:22:25 +00001342*/
drh751f4122004-01-14 21:59:22 +00001343case OP_ForceInt: {
drh751f4122004-01-14 21:59:22 +00001344 int v;
drh6810ce62004-01-31 19:22:56 +00001345 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00001346 if( (pTos->flags & (MEM_Int|MEM_Real))==0 && ((pTos->flags & MEM_Str)==0
1347 || sqlite3IsNumber(pTos->z, 0, db->enc)==0) ){
drh6810ce62004-01-31 19:22:56 +00001348 Release(pTos);
1349 pTos--;
drh1dd59e02003-07-06 17:22:25 +00001350 pc = pOp->p2 - 1;
drh751f4122004-01-14 21:59:22 +00001351 break;
drh1dd59e02003-07-06 17:22:25 +00001352 }
drh6810ce62004-01-31 19:22:56 +00001353 if( pTos->flags & MEM_Int ){
1354 v = pTos->i + (pOp->p1!=0);
drh751f4122004-01-14 21:59:22 +00001355 }else{
danielk19778a6b5412004-05-24 07:04:25 +00001356 Realify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001357 v = (int)pTos->r;
1358 if( pTos->r>(double)v ) v++;
1359 if( pOp->p1 && pTos->r==(double)v ) v++;
drh751f4122004-01-14 21:59:22 +00001360 }
drh6810ce62004-01-31 19:22:56 +00001361 Release(pTos);
1362 pTos->i = v;
1363 pTos->flags = MEM_Int;
drh1dd59e02003-07-06 17:22:25 +00001364 break;
1365}
1366
drhf1351b62002-07-31 19:50:26 +00001367/* Opcode: MustBeInt P1 P2 *
drh8aff1012001-12-22 14:49:24 +00001368**
1369** Force the top of the stack to be an integer. If the top of the
drhd99f7062002-06-08 23:25:08 +00001370** stack is not an integer and cannot be converted into an integer
drh8aff1012001-12-22 14:49:24 +00001371** with out data loss, then jump immediately to P2, or if P2==0
1372** raise an SQLITE_MISMATCH exception.
drhf1351b62002-07-31 19:50:26 +00001373**
1374** If the top of the stack is not an integer and P2 is not zero and
1375** P1 is 1, then the stack is popped. In all other cases, the depth
1376** of the stack is unchanged.
drh8aff1012001-12-22 14:49:24 +00001377*/
1378case OP_MustBeInt: {
drh6810ce62004-01-31 19:22:56 +00001379 assert( pTos>=p->aStack );
1380 if( pTos->flags & MEM_Int ){
drh8aff1012001-12-22 14:49:24 +00001381 /* Do nothing */
drh6810ce62004-01-31 19:22:56 +00001382 }else if( pTos->flags & MEM_Real ){
1383 int i = (int)pTos->r;
drh7d02cb72003-06-04 16:24:39 +00001384 double r = (double)i;
drh6810ce62004-01-31 19:22:56 +00001385 if( r!=pTos->r ){
drh8aff1012001-12-22 14:49:24 +00001386 goto mismatch;
1387 }
drh6810ce62004-01-31 19:22:56 +00001388 pTos->i = i;
1389 }else if( pTos->flags & MEM_Str ){
danielk19773d1bfea2004-05-14 11:00:53 +00001390 i64 v;
drheb2e1762004-05-27 01:53:56 +00001391 if( sqlite3VdbeChangeEncoding(pTos, TEXT_Utf8)
drhf4479502004-05-27 03:12:53 +00001392 || sqlite3VdbeMemNulTerminate(pTos) ){
drheb2e1762004-05-27 01:53:56 +00001393 goto no_mem;
1394 }
1395 if( !sqlite3atoi64(pTos->z, &v) ){
drh1dd59e02003-07-06 17:22:25 +00001396 double r;
drheb2e1762004-05-27 01:53:56 +00001397 if( !sqlite3IsNumber(pTos->z, 0, TEXT_Utf8) ){
drh1dd59e02003-07-06 17:22:25 +00001398 goto mismatch;
1399 }
drheb2e1762004-05-27 01:53:56 +00001400 Realify(pTos, TEXT_Utf8);
drh6810ce62004-01-31 19:22:56 +00001401 v = (int)pTos->r;
drh1dd59e02003-07-06 17:22:25 +00001402 r = (double)v;
drh6810ce62004-01-31 19:22:56 +00001403 if( r!=pTos->r ){
drh1dd59e02003-07-06 17:22:25 +00001404 goto mismatch;
1405 }
drh8aff1012001-12-22 14:49:24 +00001406 }
drh6810ce62004-01-31 19:22:56 +00001407 pTos->i = v;
drh8aff1012001-12-22 14:49:24 +00001408 }else{
1409 goto mismatch;
1410 }
drh6810ce62004-01-31 19:22:56 +00001411 Release(pTos);
1412 pTos->flags = MEM_Int;
drheb2e1762004-05-27 01:53:56 +00001413 pTos->type = SQLITE3_INTEGER;
drh8aff1012001-12-22 14:49:24 +00001414 break;
1415
1416mismatch:
1417 if( pOp->p2==0 ){
1418 rc = SQLITE_MISMATCH;
1419 goto abort_due_to_error;
1420 }else{
drh6810ce62004-01-31 19:22:56 +00001421 if( pOp->p1 ) popStack(&pTos, 1);
drh8aff1012001-12-22 14:49:24 +00001422 pc = pOp->p2 - 1;
1423 }
1424 break;
1425}
1426
drh53db1452004-05-20 13:54:53 +00001427/* Opcode: Eq P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001428**
1429** Pop the top two elements from the stack. If they are equal, then
1430** jump to instruction P2. Otherwise, continue to the next instruction.
drhf5905aa2002-05-26 20:54:33 +00001431**
danielk1977a37cdde2004-05-16 11:15:36 +00001432** The least significant byte of P1 may be either 0x00 or 0x01. If either
1433** operand is NULL (and thus if the result is unknown) then take the jump
1434** only if the least significant byte of P1 is 0x01.
drhf5905aa2002-05-26 20:54:33 +00001435**
danielk1977e014a832004-05-17 10:48:57 +00001436** The second least significant byte of P1 must be an affinity character -
1437** 'n', 't', 'i' or 'o' - or 0x00. An attempt is made to coerce both values
1438** according to the affinity before the comparison is made. If the byte is
1439** 0x00, then numeric affinity is used.
danielk1977a37cdde2004-05-16 11:15:36 +00001440**
1441** Once any conversions have taken place, and neither value is NULL,
1442** the values are compared. If both values are blobs, or both are text,
1443** then memcmp() is used to determine the results of the comparison. If
1444** both values are numeric, then a numeric comparison is used. If the
1445** two values are of different types, then they are inequal.
drhc9b84a12002-06-20 11:36:48 +00001446**
drhf5905aa2002-05-26 20:54:33 +00001447** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1448** stack if the jump would have been taken, or a 0 if not. Push a
1449** NULL if either operand was NULL.
danielk1977a37cdde2004-05-16 11:15:36 +00001450**
drh53db1452004-05-20 13:54:53 +00001451** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
1452** structure) that defines how to compare text.
drh5e00f6c2001-09-13 13:46:56 +00001453*/
drh53db1452004-05-20 13:54:53 +00001454/* Opcode: Ne P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001455**
drh53db1452004-05-20 13:54:53 +00001456** This works just like the Eq opcode except that the jump is taken if
1457** the operands from the stack are not equal. See the Eq opcode for
1458** additional information.
drh5e00f6c2001-09-13 13:46:56 +00001459*/
drh53db1452004-05-20 13:54:53 +00001460/* Opcode: Lt P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001461**
drh53db1452004-05-20 13:54:53 +00001462** This works just like the Eq opcode except that the jump is taken if
1463** the 2nd element down on the task is less than the top of the stack.
1464** See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001465*/
drh53db1452004-05-20 13:54:53 +00001466/* Opcode: Le P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001467**
drh53db1452004-05-20 13:54:53 +00001468** This works just like the Eq opcode except that the jump is taken if
1469** the 2nd element down on the task is less than or equal to the
1470** top of the stack. See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001471*/
drh53db1452004-05-20 13:54:53 +00001472/* Opcode: Gt P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001473**
drh53db1452004-05-20 13:54:53 +00001474** This works just like the Eq opcode except that the jump is taken if
1475** the 2nd element down on the task is greater than the top of the stack.
1476** See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001477*/
drh53db1452004-05-20 13:54:53 +00001478/* Opcode: Ge P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001479**
drh53db1452004-05-20 13:54:53 +00001480** This works just like the Eq opcode except that the jump is taken if
1481** the 2nd element down on the task is greater than or equal to the
1482** top of the stack. See the Eq opcode for additional information.
drh5e00f6c2001-09-13 13:46:56 +00001483*/
1484case OP_Eq:
1485case OP_Ne:
1486case OP_Lt:
1487case OP_Le:
1488case OP_Gt:
1489case OP_Ge: {
danielk1977a37cdde2004-05-16 11:15:36 +00001490 Mem *pNos;
1491 int flags;
1492 int res;
1493 char affinity;
1494
1495 pNos = &pTos[-1];
1496 flags = pTos->flags|pNos->flags;
1497
1498 /* If either value is a NULL P2 is not zero, take the jump if the least
1499 ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
1500 ** the stack.
1501 */
1502 if( flags&MEM_Null ){
1503 popStack(&pTos, 2);
1504 if( pOp->p2 ){
danielk1977f9dd2c22004-05-16 11:57:28 +00001505 if( (pOp->p1&0xFF) ) pc = pOp->p2-1;
danielk1977a37cdde2004-05-16 11:15:36 +00001506 }else{
1507 pTos++;
1508 pTos->flags = MEM_Null;
1509 }
1510 break;
1511 }
1512
1513 affinity = (pOp->p1>>8)&0xFF;
danielk1977e014a832004-05-17 10:48:57 +00001514 if( affinity=='\0' ) affinity = 'n';
danielk19778a6b5412004-05-24 07:04:25 +00001515 applyAffinity(pNos, affinity, db->enc);
1516 applyAffinity(pTos, affinity, db->enc);
danielk1977a37cdde2004-05-16 11:15:36 +00001517
drh53db1452004-05-20 13:54:53 +00001518 assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
1519 res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
danielk1977a37cdde2004-05-16 11:15:36 +00001520 switch( pOp->opcode ){
1521 case OP_Eq: res = res==0; break;
1522 case OP_Ne: res = res!=0; break;
1523 case OP_Lt: res = res<0; break;
1524 case OP_Le: res = res<=0; break;
1525 case OP_Gt: res = res>0; break;
1526 default: res = res>=0; break;
1527 }
1528
1529 popStack(&pTos, 2);
1530 if( pOp->p2 ){
1531 if( res ){
1532 pc = pOp->p2-1;
1533 }
1534 }else{
1535 pTos++;
1536 pTos->flags = MEM_Int;
1537 pTos->i = res;
1538 }
1539 break;
1540}
drhc9b84a12002-06-20 11:36:48 +00001541
drh5e00f6c2001-09-13 13:46:56 +00001542/* Opcode: And * * *
1543**
1544** Pop two values off the stack. Take the logical AND of the
1545** two values and push the resulting boolean value back onto the
1546** stack.
1547*/
1548/* Opcode: Or * * *
1549**
1550** Pop two values off the stack. Take the logical OR of the
1551** two values and push the resulting boolean value back onto the
1552** stack.
1553*/
1554case OP_And:
1555case OP_Or: {
drh6810ce62004-01-31 19:22:56 +00001556 Mem *pNos = &pTos[-1];
drhbb113512002-05-27 01:04:51 +00001557 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1558
drh6810ce62004-01-31 19:22:56 +00001559 assert( pNos>=p->aStack );
1560 if( pTos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001561 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001562 }else{
danielk19778a6b5412004-05-24 07:04:25 +00001563 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001564 v1 = pTos->i==0;
drhbb113512002-05-27 01:04:51 +00001565 }
drh6810ce62004-01-31 19:22:56 +00001566 if( pNos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001567 v2 = 2;
1568 }else{
danielk19778a6b5412004-05-24 07:04:25 +00001569 Integerify(pNos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001570 v2 = pNos->i==0;
drhbb113512002-05-27 01:04:51 +00001571 }
1572 if( pOp->opcode==OP_And ){
1573 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1574 v1 = and_logic[v1*3+v2];
1575 }else{
1576 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1577 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001578 }
drh6810ce62004-01-31 19:22:56 +00001579 popStack(&pTos, 2);
1580 pTos++;
drhbb113512002-05-27 01:04:51 +00001581 if( v1==2 ){
drh6810ce62004-01-31 19:22:56 +00001582 pTos->flags = MEM_Null;
drhbb113512002-05-27 01:04:51 +00001583 }else{
drh6810ce62004-01-31 19:22:56 +00001584 pTos->i = v1==0;
1585 pTos->flags = MEM_Int;
drhbb113512002-05-27 01:04:51 +00001586 }
drh5e00f6c2001-09-13 13:46:56 +00001587 break;
1588}
1589
1590/* Opcode: Negative * * *
1591**
1592** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001593** with its additive inverse. If the top of the stack is NULL
1594** its value is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001595*/
drhbf4133c2001-10-13 02:59:08 +00001596/* Opcode: AbsValue * * *
1597**
1598** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001599** with its absolute value. If the top of the stack is NULL
1600** its value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001601*/
1602case OP_Negative:
1603case OP_AbsValue: {
drh6810ce62004-01-31 19:22:56 +00001604 assert( pTos>=p->aStack );
1605 if( pTos->flags & MEM_Real ){
1606 Release(pTos);
1607 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1608 pTos->r = -pTos->r;
drhbf4133c2001-10-13 02:59:08 +00001609 }
drh6810ce62004-01-31 19:22:56 +00001610 pTos->flags = MEM_Real;
1611 }else if( pTos->flags & MEM_Int ){
1612 Release(pTos);
1613 if( pOp->opcode==OP_Negative || pTos->i<0 ){
1614 pTos->i = -pTos->i;
drhbf4133c2001-10-13 02:59:08 +00001615 }
drh6810ce62004-01-31 19:22:56 +00001616 pTos->flags = MEM_Int;
1617 }else if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001618 /* Do nothing */
drh5e00f6c2001-09-13 13:46:56 +00001619 }else{
danielk19778a6b5412004-05-24 07:04:25 +00001620 Realify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001621 Release(pTos);
1622 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1623 pTos->r = -pTos->r;
drhbf4133c2001-10-13 02:59:08 +00001624 }
drh6810ce62004-01-31 19:22:56 +00001625 pTos->flags = MEM_Real;
drh5e00f6c2001-09-13 13:46:56 +00001626 }
1627 break;
1628}
1629
1630/* Opcode: Not * * *
1631**
1632** Interpret the top of the stack as a boolean value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001633** with its complement. If the top of the stack is NULL its value
1634** is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001635*/
1636case OP_Not: {
drh6810ce62004-01-31 19:22:56 +00001637 assert( pTos>=p->aStack );
1638 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
danielk19778a6b5412004-05-24 07:04:25 +00001639 Integerify(pTos, db->enc);
drh79f14b72004-03-03 01:51:24 +00001640 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001641 pTos->i = !pTos->i;
drh79f14b72004-03-03 01:51:24 +00001642 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001643 break;
1644}
1645
drh18b81e52001-11-01 13:52:52 +00001646/* Opcode: BitNot * * *
drhbf4133c2001-10-13 02:59:08 +00001647**
1648** Interpret the top of the stack as an value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001649** with its ones-complement. If the top of the stack is NULL its
1650** value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001651*/
1652case OP_BitNot: {
drh6810ce62004-01-31 19:22:56 +00001653 assert( pTos>=p->aStack );
1654 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
danielk19778a6b5412004-05-24 07:04:25 +00001655 Integerify(pTos, db->enc);
drh79f14b72004-03-03 01:51:24 +00001656 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001657 pTos->i = ~pTos->i;
drh79f14b72004-03-03 01:51:24 +00001658 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001659 break;
1660}
1661
drh5e00f6c2001-09-13 13:46:56 +00001662/* Opcode: Noop * * *
1663**
1664** Do nothing. This instruction is often useful as a jump
1665** destination.
1666*/
1667case OP_Noop: {
1668 break;
1669}
1670
drhf5905aa2002-05-26 20:54:33 +00001671/* Opcode: If P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001672**
1673** Pop a single boolean from the stack. If the boolean popped is
1674** true, then jump to p2. Otherwise continue to the next instruction.
1675** An integer is false if zero and true otherwise. A string is
1676** false if it has zero length and true otherwise.
drhf5905aa2002-05-26 20:54:33 +00001677**
1678** If the value popped of the stack is NULL, then take the jump if P1
1679** is true and fall through if P1 is false.
drh5e00f6c2001-09-13 13:46:56 +00001680*/
drhf5905aa2002-05-26 20:54:33 +00001681/* Opcode: IfNot P1 P2 *
1682**
1683** Pop a single boolean from the stack. If the boolean popped is
1684** false, then jump to p2. Otherwise continue to the next instruction.
1685** An integer is false if zero and true otherwise. A string is
1686** false if it has zero length and true otherwise.
1687**
1688** If the value popped of the stack is NULL, then take the jump if P1
1689** is true and fall through if P1 is false.
1690*/
1691case OP_If:
1692case OP_IfNot: {
drh5e00f6c2001-09-13 13:46:56 +00001693 int c;
drh6810ce62004-01-31 19:22:56 +00001694 assert( pTos>=p->aStack );
1695 if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001696 c = pOp->p1;
1697 }else{
danielk19778a6b5412004-05-24 07:04:25 +00001698 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00001699 c = pTos->i;
drhf5905aa2002-05-26 20:54:33 +00001700 if( pOp->opcode==OP_IfNot ) c = !c;
1701 }
danielk19778a6b5412004-05-24 07:04:25 +00001702 /* FIX ME: Because constant P3 values sometimes need to be translated,
1703 ** the following assert() can fail. When P3 is always in the native text
1704 ** encoding, this assert() will be valid again. Until then, the Release()
1705 ** is neeed instead.
1706 assert( (pTos->flags & MEM_Dyn)==0 );
1707 */
1708 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001709 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00001710 if( c ) pc = pOp->p2-1;
1711 break;
1712}
1713
drhf5905aa2002-05-26 20:54:33 +00001714/* Opcode: IsNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001715**
drhf5905aa2002-05-26 20:54:33 +00001716** If any of the top abs(P1) values on the stack are NULL, then jump
drh143f3c42004-01-07 20:37:52 +00001717** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack
1718** unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001719*/
1720case OP_IsNull: {
drhf5905aa2002-05-26 20:54:33 +00001721 int i, cnt;
drh6810ce62004-01-31 19:22:56 +00001722 Mem *pTerm;
drhf5905aa2002-05-26 20:54:33 +00001723 cnt = pOp->p1;
1724 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001725 pTerm = &pTos[1-cnt];
1726 assert( pTerm>=p->aStack );
1727 for(i=0; i<cnt; i++, pTerm++){
1728 if( pTerm->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001729 pc = pOp->p2-1;
1730 break;
1731 }
1732 }
drh6810ce62004-01-31 19:22:56 +00001733 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001734 break;
1735}
1736
drhf5905aa2002-05-26 20:54:33 +00001737/* Opcode: NotNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001738**
drh143f3c42004-01-07 20:37:52 +00001739** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the
1740** stack if P1 times if P1 is greater than zero. If P1 is less than
1741** zero then leave the stack unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001742*/
1743case OP_NotNull: {
drh143f3c42004-01-07 20:37:52 +00001744 int i, cnt;
1745 cnt = pOp->p1;
1746 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001747 assert( &pTos[1-cnt] >= p->aStack );
1748 for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
drh143f3c42004-01-07 20:37:52 +00001749 if( i>=cnt ) pc = pOp->p2-1;
drh6810ce62004-01-31 19:22:56 +00001750 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001751 break;
1752}
1753
danielk1977b4964b72004-05-18 01:23:38 +00001754/* Opcode: SetNumColumns P1 P2 *
1755**
1756** Before the OP_Column opcode can be executed on a cursor, this
1757** opcode must be called to set the number of fields in the table.
1758**
1759** This opcode sets the number of columns for cursor P1 to P2.
1760*/
1761case OP_SetNumColumns: {
1762 assert( (pOp->p1)<p->nCursor );
1763 p->apCsr[pOp->p1]->nField = pOp->p2;
1764 break;
1765}
1766
drhf3218fe2004-05-28 08:21:02 +00001767/* Opcode: IdxColumn P1 * *
1768**
1769** P1 is a cursor opened on an index. Push the first field from the
1770** current index key onto the stack.
1771*/
drh9188b382004-05-14 21:12:22 +00001772/* Opcode: Column P1 P2 *
danielk1977192ac1d2004-05-10 07:17:30 +00001773**
danielk1977cfcdaef2004-05-12 07:33:33 +00001774** Interpret the data that cursor P1 points to as a structure built using
1775** the MakeRecord instruction. (See the MakeRecord opcode for additional
1776** information about the format of the data.) Push onto the stack the value
1777** of the P2-th column contained in the data.
danielk1977192ac1d2004-05-10 07:17:30 +00001778**
danielk1977cfcdaef2004-05-12 07:33:33 +00001779** If the KeyAsData opcode has previously executed on this cursor, then the
1780** field might be extracted from the key rather than the data.
danielk1977192ac1d2004-05-10 07:17:30 +00001781**
danielk1977cfcdaef2004-05-12 07:33:33 +00001782** If P1 is negative, then the record is stored on the stack rather than in
1783** a table. For P1==-1, the top of the stack is used. For P1==-2, the
1784** next on the stack is used. And so forth. The value pushed is always
1785** just a pointer into the record which is stored further down on the
danielk197784ac9d02004-05-18 09:58:06 +00001786** stack. The column value is not copied. The number of columns in the
1787** record is stored on the stack just above the record itself.
danielk1977192ac1d2004-05-10 07:17:30 +00001788*/
drhf3218fe2004-05-28 08:21:02 +00001789case OP_IdxColumn:
danielk1977cfcdaef2004-05-12 07:33:33 +00001790case OP_Column: {
1791 int payloadSize; /* Number of bytes in the record */
drhd3194f52004-05-27 19:59:32 +00001792 int p1 = pOp->p1; /* P1 value of the opcode */
danielk1977cfcdaef2004-05-12 07:33:33 +00001793 int p2 = pOp->p2; /* column number to retrieve */
drhd3194f52004-05-27 19:59:32 +00001794 Cursor *pC = 0; /* The VDBE cursor */
danielk1977cfcdaef2004-05-12 07:33:33 +00001795 char *zRec; /* Pointer to record-data from stack or pseudo-table. */
drhd3194f52004-05-27 19:59:32 +00001796 BtCursor *pCrsr; /* The BTree cursor */
1797 u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1798 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
drh25aa1b42004-05-28 01:39:01 +00001799 u32 nField; /* number of fields in the record */
drhd3194f52004-05-27 19:59:32 +00001800 u32 szHdr; /* Number of bytes in the record header */
danielk1977cfcdaef2004-05-12 07:33:33 +00001801 int len; /* The length of the serialized data for the column */
drhd3194f52004-05-27 19:59:32 +00001802 int offset = 0; /* Offset into the data */
1803 int idx; /* Index into the header */
1804 int i; /* Loop counter */
1805 char *zData; /* Part of the record being decoded */
1806 Mem sMem; /* For storing the record being decoded */
danielk1977192ac1d2004-05-10 07:17:30 +00001807
drhb6f54522004-05-20 02:42:16 +00001808 sMem.flags = 0;
drhd3194f52004-05-27 19:59:32 +00001809 assert( p1<p->nCursor );
danielk1977192ac1d2004-05-10 07:17:30 +00001810 pTos++;
danielk1977cfcdaef2004-05-12 07:33:33 +00001811
1812 /* This block sets the variable payloadSize, and if the data is coming
1813 ** from the stack or from a pseudo-table zRec. If the data is coming
1814 ** from a real cursor, then zRec is left as NULL.
drhd3194f52004-05-27 19:59:32 +00001815 **
1816 ** We also compute the number of columns in the record. For cursors,
1817 ** the number of columns is stored in the Cursor.nField element. For
1818 ** records on the stack, the next entry down on the stack is an integer
1819 ** which is the number of records.
danielk1977cfcdaef2004-05-12 07:33:33 +00001820 */
drhd3194f52004-05-27 19:59:32 +00001821 if( p1<0 ){
1822 Mem *pRec = &pTos[p1];
1823 Mem *pCnt = &pRec[-1];
1824 assert( pRec>=p->aStack );
1825 assert( pRec->flags & MEM_Blob );
1826 payloadSize = pRec->n;
1827 zRec = pRec->z;
1828 assert( pCnt>=p->aStack );
1829 assert( pCnt->flags & MEM_Int );
1830 nField = pCnt->i;
1831 }else if( (pC = p->apCsr[p1])->pCursor!=0 ){
danielk1977192ac1d2004-05-10 07:17:30 +00001832 sqlite3VdbeCursorMoveto(pC);
1833 zRec = 0;
1834 pCrsr = pC->pCursor;
1835 if( pC->nullRow ){
1836 payloadSize = 0;
drh9188b382004-05-14 21:12:22 +00001837 }else if( pC->cacheValid ){
1838 payloadSize = pC->payloadSize;
danielk1977192ac1d2004-05-10 07:17:30 +00001839 }else if( pC->keyAsData ){
danielk197796fc5fe2004-05-13 11:34:16 +00001840 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00001841 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1842 payloadSize = payloadSize64;
1843 }else{
1844 sqlite3BtreeDataSize(pCrsr, &payloadSize);
1845 }
drhd3194f52004-05-27 19:59:32 +00001846 nField = pC->nField;
danielk1977192ac1d2004-05-10 07:17:30 +00001847 }else if( pC->pseudoTable ){
1848 payloadSize = pC->nData;
1849 zRec = pC->pData;
drh9188b382004-05-14 21:12:22 +00001850 pC->cacheValid = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001851 assert( payloadSize==0 || zRec!=0 );
drhd3194f52004-05-27 19:59:32 +00001852 nField = pC->nField;
danielk1977192ac1d2004-05-10 07:17:30 +00001853 }else{
1854 payloadSize = 0;
1855 }
1856
1857 /* If payloadSize is 0, then just push a NULL onto the stack. */
1858 if( payloadSize==0 ){
1859 pTos->flags = MEM_Null;
1860 break;
1861 }
1862
drhd3194f52004-05-27 19:59:32 +00001863 assert( p2<nField );
danielk1977b4964b72004-05-18 01:23:38 +00001864
drh9188b382004-05-14 21:12:22 +00001865 /* Read and parse the table header. Store the results of the parse
1866 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00001867 */
drhd3194f52004-05-27 19:59:32 +00001868 if( pC && pC->cacheValid ){
1869 aType = pC->aType;
1870 aOffset = pC->aOffset;
1871 }else{
1872 aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
1873 aOffset = &aType[nField];
1874 if( aType==0 ){
1875 goto no_mem;
drh9188b382004-05-14 21:12:22 +00001876 }
danielk1977192ac1d2004-05-10 07:17:30 +00001877
drhd3194f52004-05-27 19:59:32 +00001878 /* Figure out how many bytes are in the header */
danielk197784ac9d02004-05-18 09:58:06 +00001879 if( zRec ){
1880 zData = zRec;
1881 }else{
drhd3194f52004-05-27 19:59:32 +00001882 int sz = payloadSize<5 ? payloadSize : 5;
1883 if( pC->keyAsData ){
1884 zData = (char*)sqlite3BtreeKeyFetch(pCrsr, sz);
1885 }else{
1886 zData = (char*)sqlite3BtreeDataFetch(pCrsr, sz);
drh9188b382004-05-14 21:12:22 +00001887 }
drhd3194f52004-05-27 19:59:32 +00001888 }
1889 idx = sqlite3GetVarint32(zData, &szHdr);
drh9188b382004-05-14 21:12:22 +00001890
drhd3194f52004-05-27 19:59:32 +00001891 /* Get the complete header text */
1892 if( !zRec ){
drhf3218fe2004-05-28 08:21:02 +00001893 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, szHdr, pC->keyAsData, &sMem);
danielk197784ac9d02004-05-18 09:58:06 +00001894 if( rc!=SQLITE_OK ){
1895 goto abort_due_to_error;
drh9188b382004-05-14 21:12:22 +00001896 }
drhb6f54522004-05-20 02:42:16 +00001897 zData = sMem.z;
drh9188b382004-05-14 21:12:22 +00001898 }
1899
drhd3194f52004-05-27 19:59:32 +00001900 /* Scan the header and use it to fill in the aType[] and aOffset[]
1901 ** arrays. aType[i] will contain the type integer for the i-th
1902 ** column and aOffset[i] will contain the offset from the beginning
1903 ** of the record to the start of the data for the i-th column
drh9188b382004-05-14 21:12:22 +00001904 */
drhd3194f52004-05-27 19:59:32 +00001905 offset = szHdr;
1906 i = 0;
1907 while( idx<szHdr && i<nField && offset<=payloadSize ){
1908 aOffset[i] = offset;
1909 idx += sqlite3GetVarint32(&zData[idx], &aType[i]);
1910 offset += sqlite3VdbeSerialTypeLen(aType[i]);
1911 i++;
drh9188b382004-05-14 21:12:22 +00001912 }
drhb6f54522004-05-20 02:42:16 +00001913 Release(&sMem);
drhd3194f52004-05-27 19:59:32 +00001914 sMem.flags = MEM_Null;
1915
1916 /* The header should end at the start of data and the data should
1917 ** end at last byte of the record. If this is not the case then
1918 ** we are dealing with a malformed record.
1919 */
1920 if( idx!=szHdr || offset!=payloadSize ){
1921 sqliteFree(aType);
1922 if( pC ) pC->aType = 0;
1923 rc = SQLITE_CORRUPT;
1924 break;
1925 }
1926
1927 /* Remember all aType and aColumn information if we have a cursor
1928 ** to remember it in. */
1929 if( pC ){
1930 pC->payloadSize = payloadSize;
1931 pC->aType = aType;
1932 pC->aOffset = aOffset;
1933 pC->cacheValid = 1;
1934 }
danielk1977cfcdaef2004-05-12 07:33:33 +00001935 }
danielk1977192ac1d2004-05-10 07:17:30 +00001936
drhd3194f52004-05-27 19:59:32 +00001937 /* Get the column information.
drh9188b382004-05-14 21:12:22 +00001938 */
danielk197784ac9d02004-05-18 09:58:06 +00001939 if( zRec ){
drhd3194f52004-05-27 19:59:32 +00001940 zData = &zRec[aOffset[p2]];
danielk197784ac9d02004-05-18 09:58:06 +00001941 }else{
drhd3194f52004-05-27 19:59:32 +00001942 len = sqlite3VdbeSerialTypeLen(aType[p2]);
drhf3218fe2004-05-28 08:21:02 +00001943 sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->keyAsData, &sMem);
drhb6f54522004-05-20 02:42:16 +00001944 zData = sMem.z;
danielk1977cfcdaef2004-05-12 07:33:33 +00001945 }
drh25aa1b42004-05-28 01:39:01 +00001946 sqlite3VdbeSerialGet(zData, aType[p2], pTos);
1947 pTos->enc = db->enc;
danielk1977b1bc9532004-05-22 03:05:33 +00001948 if( rc!=SQLITE_OK ){
1949 goto abort_due_to_error;
1950 }
drhb6f54522004-05-20 02:42:16 +00001951 Release(&sMem);
drhd3194f52004-05-27 19:59:32 +00001952
1953 /* Release the aType[] memory if we are not dealing with cursor */
1954 if( !pC ){
1955 sqliteFree(aType);
1956 }
danielk1977192ac1d2004-05-10 07:17:30 +00001957 break;
1958}
1959
drhc9b84a12002-06-20 11:36:48 +00001960/* Opcode: MakeKey P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00001961**
1962** Convert the top P1 entries of the stack into a single entry suitable
danielk197784ac9d02004-05-18 09:58:06 +00001963** for use as the key in an index. If P2 is zero, then the original
1964** entries are popped off the stack. If P2 is not zero, the original
1965** entries remain on the stack.
danielk19773d1bfea2004-05-14 11:00:53 +00001966**
1967** P3 is interpreted in the same way as for MakeIdxKey.
1968*/
1969/* Opcode: MakeIdxKey P1 P2 P3
danielk19778d059842004-05-12 11:24:02 +00001970**
1971** Convert the top P1 entries of the stack into a single entry suitable
1972** for use as the key in an index. In addition, take one additional integer
1973** off of the stack, treat that integer as an eight-byte record number, and
1974** append the integer to the key as a varint. Thus a total of P1+1 entries
1975** are popped from the stack for this instruction and a single entry is
danielk19773d1bfea2004-05-14 11:00:53 +00001976** pushed back.
1977**
1978** If P2 is not zero and one or more of the P1 entries that go into the
1979** generated key is NULL, then jump to P2 after the new key has been
1980** pushed on the stack. In other words, jump to P2 if the key is
1981** guaranteed to be unique. This jump can be used to skip a subsequent
1982** uniqueness test.
1983**
1984** P3 may be a string that is P1 characters long. The nth character of the
1985** string indicates the column affinity that should be used for the nth
1986** field of the index key (i.e. the first character of P3 corresponds to the
1987** lowest element on the stack).
1988**
1989** Character Column affinity
1990** ------------------------------
1991** 'n' NUMERIC
1992** 'i' INTEGER
1993** 't' TEXT
1994** 'o' NONE
1995**
drhd3d39e92004-05-20 22:16:29 +00001996** If P3 is NULL then datatype coercion occurs.
danielk19778d059842004-05-12 11:24:02 +00001997*/
drhf3218fe2004-05-28 08:21:02 +00001998/* Opcode MakeRecord P1 * P3
1999**
2000** Convert the top P1 entries of the stack into a single entry
2001** suitable for use as a data record in a database table. The
2002** details of the format are irrelavant as long as the OP_Column
2003** opcode can decode the record later. Refer to source code
2004** comments for the details of the record format.
2005**
2006** P3 may be a string that is P1 characters long. The nth character of the
2007** string indicates the column affinity that should be used for the nth
2008** field of the index key (i.e. the first character of P3 corresponds to the
2009** lowest element on the stack).
2010**
2011** Character Column affinity
2012** ------------------------------
2013** 'n' NUMERIC
2014** 'i' INTEGER
2015** 't' TEXT
2016** 'o' NONE
2017**
2018** If P3 is NULL then all index fields have the affinity NONE.
2019*/
danielk1977452c9892004-05-13 05:16:15 +00002020case OP_MakeKey:
drhf3218fe2004-05-28 08:21:02 +00002021case OP_MakeIdxKey:
2022case OP_MakeRecord: {
2023 /* Assuming the record contains N fields, the record format looks
2024 ** like this:
2025 **
2026 ** --------------------------------------------------------------------------
2027 ** | header-siz | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2028 ** --------------------------------------------------------------------------
2029 **
2030 ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2031 ** the top of the stack.
2032 **
2033 ** Each type field is a varint representing the serial type of the
2034 ** corresponding data element (see sqlite3VdbeSerialType()). The
2035 ** num-fields field is also a varint storing N.
2036 **
2037 ** TODO: Even when the record is short enough for Mem::zShort, this opcode
2038 ** allocates it dynamically.
2039 */
2040 int nField = pOp->p1;
2041 unsigned char *zNewRecord;
2042 unsigned char *zCsr;
2043 char *zAffinity;
danielk19778d059842004-05-12 11:24:02 +00002044 Mem *pRec;
drhf3218fe2004-05-28 08:21:02 +00002045 Mem *pRowid;
2046 int nData = 0; /* Number of bytes of data space */
2047 int nHdr = 0; /* Number of bytes of header space */
2048 int nByte = 0; /* Space required for this record */
2049 int addRowid; /* True to append a rowid column at the end */
2050 u32 serial_type; /* Type field */
2051 int containsNull; /* True if any of the data fields are NULL */
2052
2053 Mem *pData0 = &pTos[1-nField];
danielk19778d059842004-05-12 11:24:02 +00002054 assert( pData0>=p->aStack );
drhf3218fe2004-05-28 08:21:02 +00002055 zAffinity = pOp->p3;
2056 addRowid = pOp->opcode==OP_MakeIdxKey;
2057 containsNull = 0;
danielk19778d059842004-05-12 11:24:02 +00002058
drhf3218fe2004-05-28 08:21:02 +00002059 /* Loop through the elements that will make up the record to figure
2060 ** out how much space is required for the new record.
danielk19778d059842004-05-12 11:24:02 +00002061 */
2062 for(pRec=pData0; pRec<=pTos; pRec++){
drhd3d39e92004-05-20 22:16:29 +00002063 if( zAffinity ){
danielk19778a6b5412004-05-24 07:04:25 +00002064 applyAffinity(pRec, zAffinity[pRec-pData0], db->enc);
drhd3d39e92004-05-20 22:16:29 +00002065 }
danielk19773d1bfea2004-05-14 11:00:53 +00002066 if( pRec->flags&MEM_Null ){
danielk1977452c9892004-05-13 05:16:15 +00002067 containsNull = 1;
2068 }
danielk19773d1bfea2004-05-14 11:00:53 +00002069 serial_type = sqlite3VdbeSerialType(pRec);
drhf3218fe2004-05-28 08:21:02 +00002070 nData += sqlite3VdbeSerialTypeLen(serial_type);
2071 nHdr += sqlite3VarintLen(serial_type);
danielk19778d059842004-05-12 11:24:02 +00002072 }
danielk19773d1bfea2004-05-14 11:00:53 +00002073
2074 /* If we have to append a varint rowid to this record, set 'rowid'
2075 ** to the value of the rowid and increase nByte by the amount of space
2076 ** required to store it and the 0x00 seperator byte.
2077 */
danielk19778d059842004-05-12 11:24:02 +00002078 if( addRowid ){
drhf3218fe2004-05-28 08:21:02 +00002079 pRowid = &pTos[0-nField];
2080 assert( pRowid>=p->aStack );
2081 Integerify(pRowid, db->enc);
2082 serial_type = sqlite3VdbeSerialType(pRowid);
2083 nData += sqlite3VdbeSerialTypeLen(serial_type);
2084 nHdr += sqlite3VarintLen(serial_type);
danielk19778d059842004-05-12 11:24:02 +00002085 }
drhf3218fe2004-05-28 08:21:02 +00002086
2087 /* Add the initial header varint and total the size */
2088 nHdr += sqlite3VarintLen(nHdr);
2089 nByte = nHdr+nData;
2090
danielk197796fc5fe2004-05-13 11:34:16 +00002091 if( nByte>MAX_BYTES_PER_ROW ){
2092 rc = SQLITE_TOOBIG;
2093 goto abort_due_to_error;
2094 }
danielk19778d059842004-05-12 11:24:02 +00002095
drhf3218fe2004-05-28 08:21:02 +00002096 /* Allocate space for the new record. */
2097 zNewRecord = sqliteMallocRaw(nByte);
2098 if( !zNewRecord ){
danielk1977b4964b72004-05-18 01:23:38 +00002099 goto no_mem;
danielk19778d059842004-05-12 11:24:02 +00002100 }
drhf3218fe2004-05-28 08:21:02 +00002101
2102 /* Write the record */
2103 zCsr = zNewRecord;
2104 zCsr += sqlite3PutVarint(zCsr, nHdr);
danielk19778d059842004-05-12 11:24:02 +00002105 for(pRec=pData0; pRec<=pTos; pRec++){
drhf3218fe2004-05-28 08:21:02 +00002106 serial_type = sqlite3VdbeSerialType(pRec);
2107 zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */
danielk19778d059842004-05-12 11:24:02 +00002108 }
2109 if( addRowid ){
drhf3218fe2004-05-28 08:21:02 +00002110 zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid));
danielk19778d059842004-05-12 11:24:02 +00002111 }
drhf3218fe2004-05-28 08:21:02 +00002112 for(pRec=pData0; pRec<=pTos; pRec++){
2113 zCsr += sqlite3VdbeSerialPut(zCsr, pRec); /* serial data */
2114 }
2115 if( addRowid ){
2116 zCsr += sqlite3VdbeSerialPut(zCsr, pRowid);
2117 }
danielk19778d059842004-05-12 11:24:02 +00002118
drhf3218fe2004-05-28 08:21:02 +00002119 /* If zCsr has not been advanced exactly nByte bytes, then one
2120 ** of the sqlite3PutVarint() or sqlite3VdbeSerialPut() calls above
2121 ** failed. This indicates a corrupted memory cell or code bug.
2122 */
2123 if( zCsr!=(zNewRecord+nByte) ){
2124 rc = SQLITE_INTERNAL;
2125 goto abort_due_to_error;
2126 }
2127
2128 /* Pop nField entries from the stack and push the new entry on */
2129 if( addRowid || pOp->p2==0 ){
danielk1977452c9892004-05-13 05:16:15 +00002130 popStack(&pTos, nField+addRowid);
2131 }
danielk19778d059842004-05-12 11:24:02 +00002132 pTos++;
danielk19778d059842004-05-12 11:24:02 +00002133 pTos->n = nByte;
drhf3218fe2004-05-28 08:21:02 +00002134 pTos->z = zNewRecord;
2135 pTos->flags = MEM_Blob | MEM_Dyn;
danielk19778d059842004-05-12 11:24:02 +00002136
danielk19773d1bfea2004-05-14 11:00:53 +00002137 /* If P2 is non-zero, and if the key contains a NULL value, and if this
2138 ** was an OP_MakeIdxKey instruction, not OP_MakeKey, jump to P2.
2139 */
2140 if( pOp->p2 && containsNull && addRowid ){
danielk1977452c9892004-05-13 05:16:15 +00002141 pc = pOp->p2 - 1;
2142 }
danielk19778d059842004-05-12 11:24:02 +00002143 break;
2144}
2145
drh7f0f12e2004-05-21 13:39:50 +00002146/* Opcode: Statement P1 * *
drh663fc632002-02-02 18:49:19 +00002147**
drh7f0f12e2004-05-21 13:39:50 +00002148** Begin an individual statement transaction which is part of a larger
2149** BEGIN..COMMIT transaction. This is needed so that the statement
2150** can be rolled back after an error without having to roll back the
2151** entire transaction. The statement transaction will automatically
2152** commit when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002153**
drh7f0f12e2004-05-21 13:39:50 +00002154** The statement is begun on the database file with index P1. The main
drh001bbcb2003-03-19 03:14:00 +00002155** database file has an index of 0 and the file used for temporary tables
2156** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002157*/
drh7f0f12e2004-05-21 13:39:50 +00002158case OP_Statement: {
drh001bbcb2003-03-19 03:14:00 +00002159 int i = pOp->p1;
drh1aa49652003-06-02 23:14:13 +00002160 if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){
danielk19774adee202004-05-08 08:23:19 +00002161 rc = sqlite3BtreeBeginStmt(db->aDb[i].pBt);
drh1aa49652003-06-02 23:14:13 +00002162 if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2;
drh663fc632002-02-02 18:49:19 +00002163 }
2164 break;
2165}
2166
drhcabb0812002-09-14 13:47:32 +00002167/* Opcode: Transaction P1 * *
drh5e00f6c2001-09-13 13:46:56 +00002168**
2169** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002170** opcode is encountered. Depending on the ON CONFLICT setting, the
2171** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002172**
drh001bbcb2003-03-19 03:14:00 +00002173** P1 is the index of the database file on which the transaction is
2174** started. Index 0 is the main database file and index 1 is the
2175** file used for temporary tables.
drhcabb0812002-09-14 13:47:32 +00002176**
drhb19a2bc2001-09-16 00:13:26 +00002177** A write lock is obtained on the database file when a transaction is
2178** started. No other process can read or write the file while the
2179** transaction is underway. Starting a transaction also creates a
drh663fc632002-02-02 18:49:19 +00002180** rollback journal. A transaction must be started before any changes
2181** can be made to the database.
drh5e00f6c2001-09-13 13:46:56 +00002182*/
2183case OP_Transaction: {
drhb86ccfb2003-01-28 23:13:10 +00002184 int busy = 1;
drh001bbcb2003-03-19 03:14:00 +00002185 int i = pOp->p1;
drh8bf8dc92003-05-17 17:35:10 +00002186 assert( i>=0 && i<db->nDb );
2187 if( db->aDb[i].inTrans ) break;
2188 while( db->aDb[i].pBt!=0 && busy ){
danielk19774adee202004-05-08 08:23:19 +00002189 rc = sqlite3BtreeBeginTrans(db->aDb[i].pBt);
drh90bfcda2001-09-23 19:46:51 +00002190 switch( rc ){
2191 case SQLITE_BUSY: {
drhb86ccfb2003-01-28 23:13:10 +00002192 if( db->xBusyCallback==0 ){
2193 p->pc = pc;
2194 p->undoTransOnError = 1;
2195 p->rc = SQLITE_BUSY;
drh6810ce62004-01-31 19:22:56 +00002196 p->pTos = pTos;
drhb86ccfb2003-01-28 23:13:10 +00002197 return SQLITE_BUSY;
2198 }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00002199 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh90bfcda2001-09-23 19:46:51 +00002200 busy = 0;
2201 }
2202 break;
2203 }
drhf74b8d92002-09-01 23:20:45 +00002204 case SQLITE_READONLY: {
2205 rc = SQLITE_OK;
2206 /* Fall thru into the next case */
2207 }
drh90bfcda2001-09-23 19:46:51 +00002208 case SQLITE_OK: {
drhb86ccfb2003-01-28 23:13:10 +00002209 p->inTempTrans = 0;
drh90bfcda2001-09-23 19:46:51 +00002210 busy = 0;
2211 break;
2212 }
2213 default: {
2214 goto abort_due_to_error;
2215 }
2216 }
drhb86ccfb2003-01-28 23:13:10 +00002217 }
drh001bbcb2003-03-19 03:14:00 +00002218 db->aDb[i].inTrans = 1;
drhb86ccfb2003-01-28 23:13:10 +00002219 p->undoTransOnError = 1;
drh5e00f6c2001-09-13 13:46:56 +00002220 break;
2221}
2222
2223/* Opcode: Commit * * *
2224**
2225** Cause all modifications to the database that have been made since the
2226** last Transaction to actually take effect. No additional modifications
drhb19a2bc2001-09-16 00:13:26 +00002227** are allowed until another transaction is started. The Commit instruction
2228** deletes the journal file and releases the write lock on the database.
2229** A read lock continues to be held if there are still cursors open.
drh5e00f6c2001-09-13 13:46:56 +00002230*/
2231case OP_Commit: {
drh001bbcb2003-03-19 03:14:00 +00002232 int i;
drhaa940ea2004-01-15 02:44:03 +00002233 if( db->xCommitCallback!=0 ){
danielk19774adee202004-05-08 08:23:19 +00002234 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drhaa940ea2004-01-15 02:44:03 +00002235 if( db->xCommitCallback(db->pCommitArg)!=0 ){
2236 rc = SQLITE_CONSTRAINT;
2237 }
danielk19774adee202004-05-08 08:23:19 +00002238 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drhaa940ea2004-01-15 02:44:03 +00002239 }
drh001bbcb2003-03-19 03:14:00 +00002240 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2241 if( db->aDb[i].inTrans ){
danielk19774adee202004-05-08 08:23:19 +00002242 rc = sqlite3BtreeCommit(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +00002243 db->aDb[i].inTrans = 0;
2244 }
drhf57b3392001-10-08 13:22:32 +00002245 }
drh5e00f6c2001-09-13 13:46:56 +00002246 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002247 sqlite3CommitInternalChanges(db);
drh5e00f6c2001-09-13 13:46:56 +00002248 }else{
danielk19774adee202004-05-08 08:23:19 +00002249 sqlite3RollbackAll(db);
drh5e00f6c2001-09-13 13:46:56 +00002250 }
2251 break;
2252}
2253
drh001bbcb2003-03-19 03:14:00 +00002254/* Opcode: Rollback P1 * *
drh5e00f6c2001-09-13 13:46:56 +00002255**
2256** Cause all modifications to the database that have been made since the
2257** last Transaction to be undone. The database is restored to its state
2258** before the Transaction opcode was executed. No additional modifications
2259** are allowed until another transaction is started.
drhb19a2bc2001-09-16 00:13:26 +00002260**
drh001bbcb2003-03-19 03:14:00 +00002261** P1 is the index of the database file that is committed. An index of 0
2262** is used for the main database and an index of 1 is used for the file used
2263** to hold temporary tables.
2264**
drhb19a2bc2001-09-16 00:13:26 +00002265** This instruction automatically closes all cursors and releases both
drh001bbcb2003-03-19 03:14:00 +00002266** the read and write locks on the indicated database.
drh5e00f6c2001-09-13 13:46:56 +00002267*/
2268case OP_Rollback: {
danielk19774adee202004-05-08 08:23:19 +00002269 sqlite3RollbackAll(db);
drh5e00f6c2001-09-13 13:46:56 +00002270 break;
2271}
2272
drh001bbcb2003-03-19 03:14:00 +00002273/* Opcode: ReadCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002274**
drh001bbcb2003-03-19 03:14:00 +00002275** Read cookie number P2 from database P1 and push it onto the stack.
2276** P2==0 is the schema version. P2==1 is the database format.
2277** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2278** the main database file and P1==1 is the database file used to store
2279** temporary tables.
drh4a324312001-12-21 14:30:42 +00002280**
drh50e5dad2001-09-15 00:57:28 +00002281** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002282** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002283** executing this instruction.
2284*/
2285case OP_ReadCookie: {
drhf328bc82004-05-10 23:29:49 +00002286 int iMeta;
drh4a324312001-12-21 14:30:42 +00002287 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002288 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2289 assert( db->aDb[pOp->p1].pBt!=0 );
drha3b321d2004-05-11 09:31:31 +00002290 /* The indexing of meta values at the schema layer is off by one from
2291 ** the indexing in the btree layer. The btree considers meta[0] to
2292 ** be the number of free pages in the database (a read-only value)
2293 ** and meta[1] to be the schema cookie. The schema layer considers
2294 ** meta[1] to be the schema cookie. So we have to shift the index
2295 ** by one in the following statement.
2296 */
2297 rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, &iMeta);
drh6810ce62004-01-31 19:22:56 +00002298 pTos++;
drhf328bc82004-05-10 23:29:49 +00002299 pTos->i = iMeta;
drh6810ce62004-01-31 19:22:56 +00002300 pTos->flags = MEM_Int;
drh50e5dad2001-09-15 00:57:28 +00002301 break;
2302}
2303
drh001bbcb2003-03-19 03:14:00 +00002304/* Opcode: SetCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002305**
drh001bbcb2003-03-19 03:14:00 +00002306** Write the top of the stack into cookie number P2 of database P1.
2307** P2==0 is the schema version. P2==1 is the database format.
2308** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2309** the main database file and P1==1 is the database file used to store
2310** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002311**
2312** A transaction must be started before executing this opcode.
2313*/
2314case OP_SetCookie: {
drh4a324312001-12-21 14:30:42 +00002315 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002316 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2317 assert( db->aDb[pOp->p1].pBt!=0 );
drh6810ce62004-01-31 19:22:56 +00002318 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00002319 Integerify(pTos, db->enc);
drha3b321d2004-05-11 09:31:31 +00002320 /* See note about index shifting on OP_ReadCookie */
drhf328bc82004-05-10 23:29:49 +00002321 rc = sqlite3BtreeUpdateMeta(db->aDb[pOp->p1].pBt, 1+pOp->p2, (int)pTos->i);
drh79f14b72004-03-03 01:51:24 +00002322 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00002323 pTos--;
drh50e5dad2001-09-15 00:57:28 +00002324 break;
2325}
2326
drh4a324312001-12-21 14:30:42 +00002327/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002328**
drh001bbcb2003-03-19 03:14:00 +00002329** Check the value of global database parameter number 0 (the
2330** schema version) and make sure it is equal to P2.
2331** P1 is the database number which is 0 for the main database file
2332** and 1 for the file holding temporary tables and some higher number
2333** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002334**
2335** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002336** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002337** and that the current process needs to reread the schema.
2338**
2339** Either a transaction needs to have been started or an OP_Open needs
2340** to be executed (to establish a read lock) before this opcode is
2341** invoked.
2342*/
2343case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002344 int iMeta;
drh001bbcb2003-03-19 03:14:00 +00002345 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhf328bc82004-05-10 23:29:49 +00002346 rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1, &iMeta);
2347 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
danielk19774adee202004-05-08 08:23:19 +00002348 sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
drh50e5dad2001-09-15 00:57:28 +00002349 rc = SQLITE_SCHEMA;
2350 }
2351 break;
2352}
2353
drh001bbcb2003-03-19 03:14:00 +00002354/* Opcode: OpenRead P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00002355**
drhecdc7532001-09-23 02:35:53 +00002356** Open a read-only cursor for the database table whose root page is
drh001bbcb2003-03-19 03:14:00 +00002357** P2 in a database file. The database file is determined by an
2358** integer from the top of the stack. 0 means the main database and
2359** 1 means the database used for temporary tables. Give the new
2360** cursor an identifier of P1. The P1 values need not be contiguous
2361** but all P1 values should be small integers. It is an error for
2362** P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002363**
drh001bbcb2003-03-19 03:14:00 +00002364** If P2==0 then take the root page number from the next of the stack.
drh5edc3122001-09-13 21:53:09 +00002365**
drhb19a2bc2001-09-16 00:13:26 +00002366** There will be a read lock on the database whenever there is an
2367** open cursor. If the database was unlocked prior to this instruction
2368** then a read lock is acquired as part of this instruction. A read
2369** lock allows other processes to read the database but prohibits
2370** any other process from modifying the database. The read lock is
2371** released when all cursors are closed. If this instruction attempts
2372** to get a read lock but fails, the script terminates with an
2373** SQLITE_BUSY error code.
2374**
drhd3d39e92004-05-20 22:16:29 +00002375** The P3 value is a pointer to a KeyInfo structure that defines the
2376** content and collating sequence of indices. P3 is NULL for cursors
2377** that are not pointing to indices.
drhf57b3392001-10-08 13:22:32 +00002378**
drh001bbcb2003-03-19 03:14:00 +00002379** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002380*/
drhecdc7532001-09-23 02:35:53 +00002381/* Opcode: OpenWrite P1 P2 P3
2382**
2383** Open a read/write cursor named P1 on the table or index whose root
2384** page is P2. If P2==0 then take the root page number from the stack.
2385**
drhd3d39e92004-05-20 22:16:29 +00002386** The P3 value is a pointer to a KeyInfo structure that defines the
2387** content and collating sequence of indices. P3 is NULL for cursors
2388** that are not pointing to indices.
jplyon5a564222003-06-02 06:15:58 +00002389**
drh001bbcb2003-03-19 03:14:00 +00002390** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002391** in read/write mode. For a given table, there can be one or more read-only
2392** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002393**
drh001bbcb2003-03-19 03:14:00 +00002394** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002395*/
drh001bbcb2003-03-19 03:14:00 +00002396case OP_OpenRead:
2397case OP_OpenWrite: {
drh5e00f6c2001-09-13 13:46:56 +00002398 int busy = 0;
2399 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002400 int p2 = pOp->p2;
drhf57b3392001-10-08 13:22:32 +00002401 int wrFlag;
2402 Btree *pX;
drh001bbcb2003-03-19 03:14:00 +00002403 int iDb;
drhf328bc82004-05-10 23:29:49 +00002404 Cursor *pCur;
drh001bbcb2003-03-19 03:14:00 +00002405
drh6810ce62004-01-31 19:22:56 +00002406 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00002407 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00002408 iDb = pTos->i;
2409 pTos--;
2410 assert( iDb>=0 && iDb<db->nDb );
drh001bbcb2003-03-19 03:14:00 +00002411 pX = db->aDb[iDb].pBt;
drh6810ce62004-01-31 19:22:56 +00002412 assert( pX!=0 );
drh001bbcb2003-03-19 03:14:00 +00002413 wrFlag = pOp->opcode==OP_OpenWrite;
drh5edc3122001-09-13 21:53:09 +00002414 if( p2<=0 ){
drh6810ce62004-01-31 19:22:56 +00002415 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00002416 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00002417 p2 = pTos->i;
2418 pTos--;
drh5edc3122001-09-13 21:53:09 +00002419 if( p2<2 ){
danielk19774adee202004-05-08 08:23:19 +00002420 sqlite3SetString(&p->zErrMsg, "root page number less than 2", (char*)0);
drh5edc3122001-09-13 21:53:09 +00002421 rc = SQLITE_INTERNAL;
drhb86ccfb2003-01-28 23:13:10 +00002422 break;
drh5edc3122001-09-13 21:53:09 +00002423 }
2424 }
drh6810ce62004-01-31 19:22:56 +00002425 assert( i>=0 );
drh8c74a8c2002-08-25 19:20:40 +00002426 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002427 pCur = p->apCsr[i];
drhf328bc82004-05-10 23:29:49 +00002428 sqlite3VdbeCleanupCursor(pCur);
drhf328bc82004-05-10 23:29:49 +00002429 pCur->nullRow = 1;
drhe0bc4042002-06-25 01:09:11 +00002430 if( pX==0 ) break;
drhbe0072d2001-09-13 14:46:09 +00002431 do{
danielk1977452c9892004-05-13 05:16:15 +00002432 /* When opening cursors, always supply the comparison function
2433 ** sqlite3VdbeKeyCompare(). If the table being opened is of type
2434 ** INTKEY, the btree layer won't call the comparison function anyway.
2435 */
drhd3d39e92004-05-20 22:16:29 +00002436 rc = sqlite3BtreeCursor(pX, p2, wrFlag,
2437 sqlite3VdbeKeyCompare, pOp->p3,
2438 &pCur->pCursor);
2439 pCur->pKeyInfo = (KeyInfo*)pOp->p3;
2440 if( pCur->pKeyInfo ){
2441 pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
danielk1977b1bc9532004-05-22 03:05:33 +00002442 pCur->pKeyInfo->enc = p->db->enc;
drhd3d39e92004-05-20 22:16:29 +00002443 }else{
2444 pCur->pIncrKey = &pCur->bogusIncrKey;
2445 }
drh5e00f6c2001-09-13 13:46:56 +00002446 switch( rc ){
2447 case SQLITE_BUSY: {
drhb86ccfb2003-01-28 23:13:10 +00002448 if( db->xBusyCallback==0 ){
2449 p->pc = pc;
2450 p->rc = SQLITE_BUSY;
drh6810ce62004-01-31 19:22:56 +00002451 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
drhb86ccfb2003-01-28 23:13:10 +00002452 return SQLITE_BUSY;
2453 }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00002454 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh5e00f6c2001-09-13 13:46:56 +00002455 busy = 0;
2456 }
2457 break;
2458 }
2459 case SQLITE_OK: {
drhf328bc82004-05-10 23:29:49 +00002460 int flags = sqlite3BtreeFlags(pCur->pCursor);
2461 pCur->intKey = (flags & BTREE_INTKEY)!=0;
2462 pCur->zeroData = (flags & BTREE_ZERODATA)!=0;
drh5e00f6c2001-09-13 13:46:56 +00002463 busy = 0;
2464 break;
2465 }
drhf4dada72004-05-11 09:57:35 +00002466 case SQLITE_EMPTY: {
2467 rc = SQLITE_OK;
2468 busy = 0;
2469 break;
2470 }
drh5e00f6c2001-09-13 13:46:56 +00002471 default: {
2472 goto abort_due_to_error;
2473 }
2474 }
2475 }while( busy );
2476 break;
2477}
2478
drhd3d39e92004-05-20 22:16:29 +00002479/* Opcode: OpenTemp P1 * P3
drh5e00f6c2001-09-13 13:46:56 +00002480**
drh5fe2d8c2003-05-10 03:36:53 +00002481** Open a new cursor to a transient table.
2482** The transient cursor is always opened read/write even if
2483** the main database is read-only. The transient table is deleted
2484** automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002485**
drhd3d39e92004-05-20 22:16:29 +00002486** The cursor points to a BTree table if P3==0 and to a BTree index
2487** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure
2488** that defines the format of keys in the index.
drhf57b3392001-10-08 13:22:32 +00002489**
2490** This opcode is used for tables that exist for the duration of a single
2491** SQL statement only. Tables created using CREATE TEMPORARY TABLE
drh5fe2d8c2003-05-10 03:36:53 +00002492** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the
drhf57b3392001-10-08 13:22:32 +00002493** context of this opcode means for the duration of a single SQL statement
2494** whereas "Temporary" in the context of CREATE TABLE means for the duration
2495** of the connection to the database. Same word; different meanings.
drh5e00f6c2001-09-13 13:46:56 +00002496*/
2497case OP_OpenTemp: {
drh5e00f6c2001-09-13 13:46:56 +00002498 int i = pOp->p1;
2499 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002500 assert( i>=0 );
drh8c74a8c2002-08-25 19:20:40 +00002501 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002502 pCx = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00002503 sqlite3VdbeCleanupCursor(pCx);
drh5e00f6c2001-09-13 13:46:56 +00002504 memset(pCx, 0, sizeof(*pCx));
drh17f71932002-02-21 12:01:27 +00002505 pCx->nullRow = 1;
danielk19774adee202004-05-08 08:23:19 +00002506 rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
paulb0208cc2003-04-13 18:26:49 +00002507
drh5e00f6c2001-09-13 13:46:56 +00002508 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002509 rc = sqlite3BtreeBeginTrans(pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002510 }
2511 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002512 /* If a transient index is required, create it by calling
2513 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2514 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002515 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002516 */
drhd3d39e92004-05-20 22:16:29 +00002517 if( pOp->p3 ){
drhc6b52df2002-01-04 03:09:29 +00002518 int pgno;
drhd3d39e92004-05-20 22:16:29 +00002519 assert( pOp->p3type==P3_KEYINFO );
danielk19774adee202004-05-08 08:23:19 +00002520 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002521 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002522 assert( pgno==MASTER_ROOT+1 );
danielk1977e014a832004-05-17 10:48:57 +00002523 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeKeyCompare,
drhd3d39e92004-05-20 22:16:29 +00002524 pOp->p3, &pCx->pCursor);
2525 pCx->pKeyInfo = (KeyInfo*)pOp->p3;
danielk1977b1bc9532004-05-22 03:05:33 +00002526 pCx->pKeyInfo->enc = p->db->enc;
drhd3d39e92004-05-20 22:16:29 +00002527 pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
drhc6b52df2002-01-04 03:09:29 +00002528 }
2529 }else{
drhf328bc82004-05-10 23:29:49 +00002530 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
danielk19770dbe72b2004-05-11 04:54:49 +00002531 pCx->intKey = 1;
drhd3d39e92004-05-20 22:16:29 +00002532 pCx->pIncrKey = &pCx->bogusIncrKey;
drhc6b52df2002-01-04 03:09:29 +00002533 }
drh5e00f6c2001-09-13 13:46:56 +00002534 }
2535 break;
2536}
2537
drh70ce3f02003-04-15 19:22:22 +00002538/* Opcode: OpenPseudo P1 * *
2539**
2540** Open a new cursor that points to a fake table that contains a single
2541** row of data. Any attempt to write a second row of data causes the
2542** first row to be deleted. All data is deleted when the cursor is
2543** closed.
2544**
2545** A pseudo-table created by this opcode is useful for holding the
2546** NEW or OLD tables in a trigger.
2547*/
2548case OP_OpenPseudo: {
2549 int i = pOp->p1;
2550 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002551 assert( i>=0 );
drh70ce3f02003-04-15 19:22:22 +00002552 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002553 pCx = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00002554 sqlite3VdbeCleanupCursor(pCx);
drh70ce3f02003-04-15 19:22:22 +00002555 memset(pCx, 0, sizeof(*pCx));
2556 pCx->nullRow = 1;
2557 pCx->pseudoTable = 1;
drhd3d39e92004-05-20 22:16:29 +00002558 pCx->pIncrKey = &pCx->bogusIncrKey;
drh70ce3f02003-04-15 19:22:22 +00002559 break;
2560}
2561
drh5e00f6c2001-09-13 13:46:56 +00002562/* Opcode: Close P1 * *
2563**
2564** Close a cursor previously opened as P1. If P1 is not
2565** currently open, this instruction is a no-op.
2566*/
2567case OP_Close: {
2568 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00002569 if( i>=0 && i<p->nCursor ){
drhd7556d22004-05-14 21:59:40 +00002570 sqlite3VdbeCleanupCursor(p->apCsr[i]);
drh5e00f6c2001-09-13 13:46:56 +00002571 }
2572 break;
2573}
2574
drh7cf6e4d2004-05-19 14:56:55 +00002575/* Opcode: MoveGe P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002576**
2577** Pop the top of the stack and use its value as a key. Reposition
drh7cf6e4d2004-05-19 14:56:55 +00002578** cursor P1 so that it points to the smallest entry that is greater
2579** than or equal to the key that was popped ffrom the stack.
2580** If there are no records greater than or equal to the key and P2
2581** is not zero, then jump to P2.
2582**
2583** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2584*/
2585/* Opcode: MoveGt P1 P2 *
2586**
2587** Pop the top of the stack and use its value as a key. Reposition
2588** cursor P1 so that it points to the smallest entry that is greater
2589** than the key from the stack.
drh8721ce42001-11-07 14:22:00 +00002590** If there are no records greater than the key and P2 is not zero,
drh7cf6e4d2004-05-19 14:56:55 +00002591** then jump to P2.
drhb19a2bc2001-09-16 00:13:26 +00002592**
drh7cf6e4d2004-05-19 14:56:55 +00002593** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
drh5e00f6c2001-09-13 13:46:56 +00002594*/
drhc045ec52002-12-04 20:01:06 +00002595/* Opcode: MoveLt P1 P2 *
2596**
2597** Pop the top of the stack and use its value as a key. Reposition
drh7cf6e4d2004-05-19 14:56:55 +00002598** cursor P1 so that it points to the largest entry that is less
2599** than the key from the stack.
2600** If there are no records less than the key and P2 is not zero,
2601** then jump to P2.
drhc045ec52002-12-04 20:01:06 +00002602**
drh7cf6e4d2004-05-19 14:56:55 +00002603** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2604*/
2605/* Opcode: MoveLe P1 P2 *
danielk19773d1bfea2004-05-14 11:00:53 +00002606**
drh7cf6e4d2004-05-19 14:56:55 +00002607** Pop the top of the stack and use its value as a key. Reposition
2608** cursor P1 so that it points to the largest entry that is less than
2609** or equal to the key that was popped from the stack.
2610** If there are no records less than or eqal to the key and P2 is not zero,
2611** then jump to P2.
2612**
2613** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
drhc045ec52002-12-04 20:01:06 +00002614*/
2615case OP_MoveLt:
drh7cf6e4d2004-05-19 14:56:55 +00002616case OP_MoveLe:
2617case OP_MoveGe:
2618case OP_MoveGt: {
drh5e00f6c2001-09-13 13:46:56 +00002619 int i = pOp->p1;
drh80ff32f2001-11-04 18:32:46 +00002620 Cursor *pC;
2621
drh6810ce62004-01-31 19:22:56 +00002622 assert( pTos>=p->aStack );
drh70ce3f02003-04-15 19:22:22 +00002623 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002624 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00002625 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00002626 int res, oc;
drh7cf6e4d2004-05-19 14:56:55 +00002627 oc = pOp->opcode;
drha11846b2004-01-07 18:52:56 +00002628 pC->nullRow = 0;
drhd3d39e92004-05-20 22:16:29 +00002629 *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
drhf328bc82004-05-10 23:29:49 +00002630 if( pC->intKey ){
2631 i64 iKey;
danielk19773d1bfea2004-05-14 11:00:53 +00002632 assert( !pOp->p3 );
danielk19778a6b5412004-05-24 07:04:25 +00002633 Integerify(pTos, db->enc);
drhf328bc82004-05-10 23:29:49 +00002634 iKey = intToKey(pTos->i);
drh7cf6e4d2004-05-19 14:56:55 +00002635 if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
drha11846b2004-01-07 18:52:56 +00002636 pC->movetoTarget = iKey;
2637 pC->deferredMoveto = 1;
drh6810ce62004-01-31 19:22:56 +00002638 Release(pTos);
2639 pTos--;
drha11846b2004-01-07 18:52:56 +00002640 break;
2641 }
drhf328bc82004-05-10 23:29:49 +00002642 sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res);
drh6810ce62004-01-31 19:22:56 +00002643 pC->lastRecno = pTos->i;
drh8aff1012001-12-22 14:49:24 +00002644 pC->recnoIsValid = res==0;
drh5e00f6c2001-09-13 13:46:56 +00002645 }else{
danielk19778a6b5412004-05-24 07:04:25 +00002646 Stringify(pTos, db->enc);
danielk19774adee202004-05-08 08:23:19 +00002647 sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
drh80ff32f2001-11-04 18:32:46 +00002648 pC->recnoIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002649 }
drha11846b2004-01-07 18:52:56 +00002650 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00002651 pC->cacheValid = 0;
drhd3d39e92004-05-20 22:16:29 +00002652 *pC->pIncrKey = 0;
danielk19776f8a5032004-05-10 10:34:51 +00002653 sqlite3_search_count++;
drh7cf6e4d2004-05-19 14:56:55 +00002654 if( oc==OP_MoveGe || oc==OP_MoveGt ){
2655 if( res<0 ){
2656 sqlite3BtreeNext(pC->pCursor, &res);
2657 pC->recnoIsValid = 0;
2658 if( res && pOp->p2>0 ){
2659 pc = pOp->p2 - 1;
2660 }
drh8721ce42001-11-07 14:22:00 +00002661 }
drh7cf6e4d2004-05-19 14:56:55 +00002662 }else{
2663 assert( oc==OP_MoveLt || oc==OP_MoveLe );
drh1a844c32002-12-04 22:29:28 +00002664 if( res>=0 ){
danielk19774adee202004-05-08 08:23:19 +00002665 sqlite3BtreePrevious(pC->pCursor, &res);
drh1a844c32002-12-04 22:29:28 +00002666 pC->recnoIsValid = 0;
2667 }else{
2668 /* res might be negative because the table is empty. Check to
2669 ** see if this is the case.
2670 */
drhf328bc82004-05-10 23:29:49 +00002671 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00002672 }
drhc045ec52002-12-04 20:01:06 +00002673 if( res && pOp->p2>0 ){
2674 pc = pOp->p2 - 1;
2675 }
drh8721ce42001-11-07 14:22:00 +00002676 }
drh5e00f6c2001-09-13 13:46:56 +00002677 }
drh6810ce62004-01-31 19:22:56 +00002678 Release(pTos);
2679 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002680 break;
2681}
2682
drh5e00f6c2001-09-13 13:46:56 +00002683/* Opcode: Distinct P1 P2 *
2684**
drh6b125452002-01-28 15:53:03 +00002685** Use the top of the stack as a string key. If a record with that key does
drhb19a2bc2001-09-16 00:13:26 +00002686** not exist in the table of cursor P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002687** does already exist, then fall thru. The cursor is left pointing
2688** at the record if it exists. The key is not popped from the stack.
drh5e00f6c2001-09-13 13:46:56 +00002689**
2690** This operation is similar to NotFound except that this operation
2691** does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002692**
drhd99f7062002-06-08 23:25:08 +00002693** See also: Found, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002694*/
2695/* Opcode: Found P1 P2 *
2696**
drh6b125452002-01-28 15:53:03 +00002697** Use the top of the stack as a string key. If a record with that key
drhb19a2bc2001-09-16 00:13:26 +00002698** does exist in table of P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002699** does not exist, then fall thru. The cursor is left pointing
2700** to the record if it exists. The key is popped from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002701**
drhd99f7062002-06-08 23:25:08 +00002702** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002703*/
2704/* Opcode: NotFound P1 P2 *
2705**
drh6b125452002-01-28 15:53:03 +00002706** Use the top of the stack as a string key. If a record with that key
drhb19a2bc2001-09-16 00:13:26 +00002707** does not exist in table of P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002708** does exist, then fall thru. The cursor is left pointing to the
2709** record if it exists. The key is popped from the stack.
drh5e00f6c2001-09-13 13:46:56 +00002710**
2711** The difference between this operation and Distinct is that
2712** Distinct does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002713**
drh9cfcf5d2002-01-29 18:41:24 +00002714** See also: Distinct, Found, MoveTo, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00002715*/
2716case OP_Distinct:
2717case OP_NotFound:
2718case OP_Found: {
2719 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00002720 int alreadyExists = 0;
drh80ff32f2001-11-04 18:32:46 +00002721 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00002722 assert( pTos>=p->aStack );
2723 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002724 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh5e00f6c2001-09-13 13:46:56 +00002725 int res, rx;
drhf328bc82004-05-10 23:29:49 +00002726 assert( pC->intKey==0 );
danielk19778a6b5412004-05-24 07:04:25 +00002727 Stringify(pTos, db->enc);
danielk19774adee202004-05-08 08:23:19 +00002728 rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
drh5e00f6c2001-09-13 13:46:56 +00002729 alreadyExists = rx==SQLITE_OK && res==0;
drha11846b2004-01-07 18:52:56 +00002730 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00002731 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002732 }
2733 if( pOp->opcode==OP_Found ){
2734 if( alreadyExists ) pc = pOp->p2 - 1;
2735 }else{
2736 if( !alreadyExists ) pc = pOp->p2 - 1;
2737 }
2738 if( pOp->opcode!=OP_Distinct ){
drh6810ce62004-01-31 19:22:56 +00002739 Release(pTos);
2740 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002741 }
2742 break;
2743}
2744
drh9cfcf5d2002-01-29 18:41:24 +00002745/* Opcode: IsUnique P1 P2 *
2746**
drh0ca3e242002-01-29 23:07:02 +00002747** The top of the stack is an integer record number. Call this
2748** record number R. The next on the stack is an index key created
2749** using MakeIdxKey. Call it K. This instruction pops R from the
2750** stack but it leaves K unchanged.
drh9cfcf5d2002-01-29 18:41:24 +00002751**
drh7cf6e4d2004-05-19 14:56:55 +00002752** P1 is an index. So it has no data and its key consists of a
2753** record generated by OP_MakeIdxKey. This key contains one or more
drhf3218fe2004-05-28 08:21:02 +00002754** fields followed by a ROWID field.
2755**
drh0ca3e242002-01-29 23:07:02 +00002756** This instruction asks if there is an entry in P1 where the
drh7cf6e4d2004-05-19 14:56:55 +00002757** fields matches K but the rowid is different from R.
2758** If there is no such entry, then there is an immediate
drh0ca3e242002-01-29 23:07:02 +00002759** jump to P2. If any entry does exist where the index string
2760** matches K but the record number is not R, then the record
2761** number for that entry is pushed onto the stack and control
2762** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00002763**
drhd99f7062002-06-08 23:25:08 +00002764** See also: Distinct, NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00002765*/
2766case OP_IsUnique: {
2767 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00002768 Mem *pNos = &pTos[-1];
drhf328bc82004-05-10 23:29:49 +00002769 Cursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00002770 BtCursor *pCrsr;
danielk1977452c9892004-05-13 05:16:15 +00002771 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00002772
drh0ca3e242002-01-29 23:07:02 +00002773 /* Pop the value R off the top of the stack
2774 */
drh6810ce62004-01-31 19:22:56 +00002775 assert( pNos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00002776 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00002777 R = pTos->i;
2778 pTos--;
2779 assert( i>=0 && i<=p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002780 pCx = p->apCsr[i];
drhf328bc82004-05-10 23:29:49 +00002781 pCrsr = pCx->pCursor;
2782 if( pCrsr!=0 ){
drh9cfcf5d2002-01-29 18:41:24 +00002783 int res, rc;
danielk1977452c9892004-05-13 05:16:15 +00002784 i64 v; /* The record number on the P1 entry that matches K */
drh0ca3e242002-01-29 23:07:02 +00002785 char *zKey; /* The value of K */
2786 int nKey; /* Number of bytes in K */
danielk1977452c9892004-05-13 05:16:15 +00002787 int len; /* Number of bytes in K without the rowid at the end */
drhf3218fe2004-05-28 08:21:02 +00002788 int szRowid; /* Size of the rowid column at the end of zKey */
drh0ca3e242002-01-29 23:07:02 +00002789
2790 /* Make sure K is a string and make zKey point to K
2791 */
danielk19778a6b5412004-05-24 07:04:25 +00002792 Stringify(pNos, db->enc);
drh6810ce62004-01-31 19:22:56 +00002793 zKey = pNos->z;
2794 nKey = pNos->n;
danielk1977452c9892004-05-13 05:16:15 +00002795
drhf3218fe2004-05-28 08:21:02 +00002796 szRowid = sqlite3VdbeIdxRowidLen(nKey, zKey);
2797 len = nKey-szRowid;
drh0ca3e242002-01-29 23:07:02 +00002798
drha3b321d2004-05-11 09:31:31 +00002799 /* Search for an entry in P1 where all but the last four bytes match K.
drh0ca3e242002-01-29 23:07:02 +00002800 ** If there is no such entry, jump immediately to P2.
2801 */
drh9188b382004-05-14 21:12:22 +00002802 assert( pCx->deferredMoveto==0 );
2803 pCx->cacheValid = 0;
danielk1977452c9892004-05-13 05:16:15 +00002804 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002805 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2806 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00002807 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002808 if( res ){
2809 pc = pOp->p2 - 1;
2810 break;
2811 }
2812 }
drh7cf6e4d2004-05-19 14:56:55 +00002813 rc = sqlite3VdbeIdxKeyCompare(pCx, len, zKey, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002814 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2815 if( res>0 ){
2816 pc = pOp->p2 - 1;
2817 break;
2818 }
drh0ca3e242002-01-29 23:07:02 +00002819
2820 /* At this point, pCrsr is pointing to an entry in P1 where all but
drhf3218fe2004-05-28 08:21:02 +00002821 ** the final entry (the rowid) matches K. Check to see if the
2822 ** final rowid column is different from R. If it equals R then jump
danielk1977452c9892004-05-13 05:16:15 +00002823 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00002824 */
danielk1977452c9892004-05-13 05:16:15 +00002825 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
2826 if( rc!=SQLITE_OK ){
2827 goto abort_due_to_error;
2828 }
drh0ca3e242002-01-29 23:07:02 +00002829 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00002830 pc = pOp->p2 - 1;
2831 break;
2832 }
drh0ca3e242002-01-29 23:07:02 +00002833
danielk1977452c9892004-05-13 05:16:15 +00002834 /* The final varint of the key is different from R. Push it onto
2835 ** the stack. (The record number of an entry that violates a UNIQUE
2836 ** constraint.)
drh0ca3e242002-01-29 23:07:02 +00002837 */
drh6810ce62004-01-31 19:22:56 +00002838 pTos++;
2839 pTos->i = v;
2840 pTos->flags = MEM_Int;
drh9cfcf5d2002-01-29 18:41:24 +00002841 }
2842 break;
2843}
2844
drh6b125452002-01-28 15:53:03 +00002845/* Opcode: NotExists P1 P2 *
2846**
2847** Use the top of the stack as a integer key. If a record with that key
2848** does not exist in table of P1, then jump to P2. If the record
2849** does exist, then fall thru. The cursor is left pointing to the
2850** record if it exists. The integer key is popped from the stack.
2851**
2852** The difference between this operation and NotFound is that this
2853** operation assumes the key is an integer and NotFound assumes it
2854** is a string.
2855**
drhd99f7062002-06-08 23:25:08 +00002856** See also: Distinct, Found, MoveTo, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00002857*/
2858case OP_NotExists: {
2859 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00002860 Cursor *pC;
drh0ca3e242002-01-29 23:07:02 +00002861 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00002862 assert( pTos>=p->aStack );
2863 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002864 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197736a3c702004-05-11 06:55:14 +00002865 int res, rx;
2866 u64 iKey;
drh6810ce62004-01-31 19:22:56 +00002867 assert( pTos->flags & MEM_Int );
drhd7556d22004-05-14 21:59:40 +00002868 assert( p->apCsr[i]->intKey );
drh6810ce62004-01-31 19:22:56 +00002869 iKey = intToKey(pTos->i);
danielk197736a3c702004-05-11 06:55:14 +00002870 rx = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res);
drh9188b382004-05-14 21:12:22 +00002871 pC->lastRecno = pTos->i;
2872 pC->recnoIsValid = res==0;
2873 pC->nullRow = 0;
2874 pC->cacheValid = 0;
drh6b125452002-01-28 15:53:03 +00002875 if( rx!=SQLITE_OK || res!=0 ){
drh17f71932002-02-21 12:01:27 +00002876 pc = pOp->p2 - 1;
drh9188b382004-05-14 21:12:22 +00002877 pC->recnoIsValid = 0;
drh6b125452002-01-28 15:53:03 +00002878 }
2879 }
drh6810ce62004-01-31 19:22:56 +00002880 Release(pTos);
2881 pTos--;
drh6b125452002-01-28 15:53:03 +00002882 break;
2883}
2884
drh5e00f6c2001-09-13 13:46:56 +00002885/* Opcode: NewRecno P1 * *
2886**
2887** Get a new integer record number used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00002888** The record number is not previously used as a key in the database
drh4a324312001-12-21 14:30:42 +00002889** table that cursor P1 points to. The new record number is pushed
drh5e00f6c2001-09-13 13:46:56 +00002890** onto the stack.
2891*/
2892case OP_NewRecno: {
2893 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00002894 i64 v = 0;
drh80ff32f2001-11-04 18:32:46 +00002895 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00002896 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002897 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00002898 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00002899 }else{
drh5cf8e8c2002-02-19 22:42:05 +00002900 /* The next rowid or record number (different terms for the same
2901 ** thing) is obtained in a two-step algorithm.
2902 **
2903 ** First we attempt to find the largest existing rowid and add one
2904 ** to that. But if the largest existing rowid is already the maximum
2905 ** positive integer, we have to fall through to the second
2906 ** probabilistic algorithm
2907 **
2908 ** The second algorithm is to select a rowid at random and see if
2909 ** it already exists in the table. If it does not exist, we have
2910 ** succeeded. If the random rowid does exist, we select a new one
2911 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00002912 **
2913 ** For a table with less than 2 billion entries, the probability
2914 ** of not finding a unused rowid is about 1.0e-300. This is a
2915 ** non-zero probability, but it is still vanishingly small and should
2916 ** never cause a problem. You are much, much more likely to have a
2917 ** hardware failure than for this algorithm to fail.
2918 **
drhaf9ff332002-01-16 21:00:27 +00002919 ** The analysis in the previous paragraph assumes that you have a good
2920 ** source of random numbers. Is a library function like lrand48()
2921 ** good enough? Maybe. Maybe not. It's hard to know whether there
2922 ** might be subtle bugs is some implementations of lrand48() that
2923 ** could cause problems. To avoid uncertainty, SQLite uses its own
2924 ** random number generator based on the RC4 algorithm.
2925 **
drhdb5ed6d2001-09-18 22:17:44 +00002926 ** To promote locality of reference for repetitive inserts, the
drh5cf8e8c2002-02-19 22:42:05 +00002927 ** first few attempts at chosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00002928 ** larger than the previous rowid. This has been shown experimentally
2929 ** to double the speed of the COPY operation.
2930 */
drhf328bc82004-05-10 23:29:49 +00002931 int res, rx, cnt;
2932 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00002933 cnt = 0;
drhf328bc82004-05-10 23:29:49 +00002934 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
2935 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drh5cf8e8c2002-02-19 22:42:05 +00002936 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00002937 if( pC->nextRowidValid ){
2938 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00002939 }else{
danielk19774adee202004-05-08 08:23:19 +00002940 rx = sqlite3BtreeLast(pC->pCursor, &res);
drh32fbe342002-10-19 20:16:37 +00002941 if( res ){
2942 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00002943 }else{
drhf328bc82004-05-10 23:29:49 +00002944 sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
drh32fbe342002-10-19 20:16:37 +00002945 v = keyToInt(v);
drhf328bc82004-05-10 23:29:49 +00002946 if( v==0x7fffffffffffffff ){
drh32fbe342002-10-19 20:16:37 +00002947 pC->useRandomRowid = 1;
2948 }else{
2949 v++;
2950 }
drh5cf8e8c2002-02-19 22:42:05 +00002951 }
drh3fc190c2001-09-14 03:24:23 +00002952 }
drhf328bc82004-05-10 23:29:49 +00002953 if( v<0x7fffffffffffffff ){
drh32fbe342002-10-19 20:16:37 +00002954 pC->nextRowidValid = 1;
2955 pC->nextRowid = v+1;
2956 }else{
2957 pC->nextRowidValid = 0;
2958 }
drh5cf8e8c2002-02-19 22:42:05 +00002959 }
2960 if( pC->useRandomRowid ){
2961 v = db->priorNewRowid;
2962 cnt = 0;
2963 do{
2964 if( v==0 || cnt>2 ){
danielk19774adee202004-05-08 08:23:19 +00002965 sqlite3Randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00002966 if( cnt<5 ) v &= 0xffffff;
2967 }else{
drhbbd82df2004-02-11 09:46:30 +00002968 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +00002969 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +00002970 v += r + 1;
drh5cf8e8c2002-02-19 22:42:05 +00002971 }
2972 if( v==0 ) continue;
2973 x = intToKey(v);
drhf328bc82004-05-10 23:29:49 +00002974 rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res);
drh5cf8e8c2002-02-19 22:42:05 +00002975 cnt++;
2976 }while( cnt<1000 && rx==SQLITE_OK && res==0 );
2977 db->priorNewRowid = v;
2978 if( rx==SQLITE_OK && res==0 ){
2979 rc = SQLITE_FULL;
2980 goto abort_due_to_error;
2981 }
drh1eaa2692001-09-18 02:02:23 +00002982 }
drh17f71932002-02-21 12:01:27 +00002983 pC->recnoIsValid = 0;
drha11846b2004-01-07 18:52:56 +00002984 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00002985 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002986 }
drh6810ce62004-01-31 19:22:56 +00002987 pTos++;
2988 pTos->i = v;
2989 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00002990 break;
2991}
2992
drh0ca3e242002-01-29 23:07:02 +00002993/* Opcode: PutIntKey P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002994**
jplyon5a564222003-06-02 06:15:58 +00002995** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00002996** created if it doesn't already exist or the data for an existing
drh5e00f6c2001-09-13 13:46:56 +00002997** entry is overwritten. The data is the value on the top of the
drh6b125452002-01-28 15:53:03 +00002998** stack. The key is the next value down on the stack. The key must
2999** be an integer. The stack is popped twice by this instruction.
drh4a324312001-12-21 14:30:42 +00003000**
rdcb0c374f2004-02-20 22:53:38 +00003001** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3002** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set,
3003** then the current statement change count is incremented (otherwise not).
3004** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
danielk19776f8a5032004-05-10 10:34:51 +00003005** stored for subsequent return by the sqlite3_last_insert_rowid() function
rdcb0c374f2004-02-20 22:53:38 +00003006** (otherwise it's unmodified).
drh5e00f6c2001-09-13 13:46:56 +00003007*/
drhc8d30ac2002-04-12 10:08:59 +00003008/* Opcode: PutStrKey P1 * *
drh6b125452002-01-28 15:53:03 +00003009**
jplyon5a564222003-06-02 06:15:58 +00003010** Write an entry into the table of cursor P1. A new entry is
drh6b125452002-01-28 15:53:03 +00003011** created if it doesn't already exist or the data for an existing
3012** entry is overwritten. The data is the value on the top of the
3013** stack. The key is the next value down on the stack. The key must
3014** be a string. The stack is popped twice by this instruction.
drh70ce3f02003-04-15 19:22:22 +00003015**
3016** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
drh6b125452002-01-28 15:53:03 +00003017*/
3018case OP_PutIntKey:
3019case OP_PutStrKey: {
drh6810ce62004-01-31 19:22:56 +00003020 Mem *pNos = &pTos[-1];
drh5e00f6c2001-09-13 13:46:56 +00003021 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003022 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00003023 assert( pNos>=p->aStack );
3024 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003025 if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
drh5e00f6c2001-09-13 13:46:56 +00003026 char *zKey;
danielk19775f8d8a82004-05-11 00:28:42 +00003027 i64 nKey;
danielk1977e7c8d582004-05-13 13:38:52 +00003028 i64 iKey;
drh6b125452002-01-28 15:53:03 +00003029 if( pOp->opcode==OP_PutStrKey ){
danielk19778a6b5412004-05-24 07:04:25 +00003030 Stringify(pNos, db->enc);
drh6810ce62004-01-31 19:22:56 +00003031 nKey = pNos->n;
3032 zKey = pNos->z;
drh5e00f6c2001-09-13 13:46:56 +00003033 }else{
drh6810ce62004-01-31 19:22:56 +00003034 assert( pNos->flags & MEM_Int );
danielk19775f8d8a82004-05-11 00:28:42 +00003035
3036 /* If the table is an INTKEY table, set nKey to the value of
danielk19770dbe72b2004-05-11 04:54:49 +00003037 ** the integer key, and zKey to NULL. Otherwise, set nKey to
3038 ** sizeof(i64) and point zKey at iKey. iKey contains the integer
3039 ** key in the on-disk byte order.
danielk19775f8d8a82004-05-11 00:28:42 +00003040 */
danielk19770dbe72b2004-05-11 04:54:49 +00003041 iKey = intToKey(pNos->i);
danielk19775f8d8a82004-05-11 00:28:42 +00003042 if( pC->intKey ){
danielk197749f737d2004-05-11 02:10:06 +00003043 nKey = intToKey(pNos->i);
danielk19775f8d8a82004-05-11 00:28:42 +00003044 zKey = 0;
3045 }else{
danielk19775f8d8a82004-05-11 00:28:42 +00003046 nKey = sizeof(i64);
3047 zKey = (char*)&iKey;
3048 }
danielk19775f8d8a82004-05-11 00:28:42 +00003049
rdcb0c374f2004-02-20 22:53:38 +00003050 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3051 if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
3052 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
drh6810ce62004-01-31 19:22:56 +00003053 if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
drh32fbe342002-10-19 20:16:37 +00003054 pC->nextRowidValid = 0;
3055 }
drh4a324312001-12-21 14:30:42 +00003056 }
drh78a75832004-02-13 14:07:12 +00003057 if( pTos->flags & MEM_Null ){
3058 pTos->z = 0;
3059 pTos->n = 0;
3060 }else{
danielk1977106bb232004-05-21 10:08:53 +00003061 assert( pTos->flags & (MEM_Blob|MEM_Str) );
drh78a75832004-02-13 14:07:12 +00003062 }
drh70ce3f02003-04-15 19:22:22 +00003063 if( pC->pseudoTable ){
3064 /* PutStrKey does not work for pseudo-tables.
3065 ** The following assert makes sure we are not trying to use
3066 ** PutStrKey on a pseudo-table
3067 */
3068 assert( pOp->opcode==OP_PutIntKey );
3069 sqliteFree(pC->pData);
3070 pC->iKey = iKey;
drh6810ce62004-01-31 19:22:56 +00003071 pC->nData = pTos->n;
3072 if( pTos->flags & MEM_Dyn ){
3073 pC->pData = pTos->z;
3074 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003075 }else{
drhf4479502004-05-27 03:12:53 +00003076 pC->pData = sqliteMallocRaw( pC->nData+2 );
drh70ce3f02003-04-15 19:22:22 +00003077 if( pC->pData ){
drh6810ce62004-01-31 19:22:56 +00003078 memcpy(pC->pData, pTos->z, pC->nData);
drh70ce3f02003-04-15 19:22:22 +00003079 }
drhf4479502004-05-27 03:12:53 +00003080 pC->pData[pC->nData] = 0;
3081 pC->pData[pC->nData+1] = 0;
drh70ce3f02003-04-15 19:22:22 +00003082 }
3083 pC->nullRow = 0;
3084 }else{
danielk19774adee202004-05-08 08:23:19 +00003085 rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
drh70ce3f02003-04-15 19:22:22 +00003086 }
drh32fbe342002-10-19 20:16:37 +00003087 pC->recnoIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003088 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003089 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003090 }
drh6810ce62004-01-31 19:22:56 +00003091 popStack(&pTos, 2);
drh5e00f6c2001-09-13 13:46:56 +00003092 break;
3093}
3094
drhc8d30ac2002-04-12 10:08:59 +00003095/* Opcode: Delete P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003096**
drh5edc3122001-09-13 21:53:09 +00003097** Delete the record at which the P1 cursor is currently pointing.
3098**
3099** The cursor will be left pointing at either the next or the previous
3100** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003101** the next Next instruction will be a no-op. Hence it is OK to delete
3102** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003103**
rdcb0c374f2004-02-20 22:53:38 +00003104** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3105** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set,
3106** then the current statement change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003107**
3108** If P1 is a pseudo-table, then this instruction is a no-op.
drh5e00f6c2001-09-13 13:46:56 +00003109*/
3110case OP_Delete: {
drh5e00f6c2001-09-13 13:46:56 +00003111 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003112 Cursor *pC;
drh70ce3f02003-04-15 19:22:22 +00003113 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003114 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003115 if( pC->pCursor!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003116 sqlite3VdbeCursorMoveto(pC);
3117 rc = sqlite3BtreeDelete(pC->pCursor);
drh32fbe342002-10-19 20:16:37 +00003118 pC->nextRowidValid = 0;
drh9188b382004-05-14 21:12:22 +00003119 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003120 }
rdcb0c374f2004-02-20 22:53:38 +00003121 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3122 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
3123 break;
3124}
3125
3126/* Opcode: SetCounts * * *
3127**
3128** Called at end of statement. Updates lsChange (last statement change count)
3129** and resets csChange (current statement change count) to 0.
3130*/
3131case OP_SetCounts: {
3132 db->lsChange=db->csChange;
3133 db->csChange=0;
drh5e00f6c2001-09-13 13:46:56 +00003134 break;
3135}
3136
3137/* Opcode: KeyAsData P1 P2 *
3138**
3139** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
drh5fe2d8c2003-05-10 03:36:53 +00003140** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls
3141** data off of the key rather than the data. This is used for
drh5e00f6c2001-09-13 13:46:56 +00003142** processing compound selects.
danielk197784ac9d02004-05-18 09:58:06 +00003143**
3144** This opcode also instructs the cursor that the keys used will be
3145** serialized in the record format usually used for table data, not
3146** the usual index key format.
drh5e00f6c2001-09-13 13:46:56 +00003147*/
3148case OP_KeyAsData: {
3149 int i = pOp->p1;
drhfec19aa2004-05-19 20:41:03 +00003150 Cursor *pC;
drh70ce3f02003-04-15 19:22:22 +00003151 assert( i>=0 && i<p->nCursor );
drhfec19aa2004-05-19 20:41:03 +00003152 pC = p->apCsr[i];
3153 pC->keyAsData = pOp->p2;
drhd3d39e92004-05-20 22:16:29 +00003154 sqlite3BtreeSetCompare(pC->pCursor, sqlite3VdbeRowCompare, pC->pKeyInfo);
drh70ce3f02003-04-15 19:22:22 +00003155 break;
3156}
3157
3158/* Opcode: RowData P1 * *
3159**
3160** Push onto the stack the complete row data for cursor P1.
3161** There is no interpretation of the data. It is just copied
3162** onto the stack exactly as it is found in the database file.
3163**
3164** If the cursor is not pointing to a valid row, a NULL is pushed
3165** onto the stack.
3166*/
drh143f3c42004-01-07 20:37:52 +00003167/* Opcode: RowKey P1 * *
3168**
3169** Push onto the stack the complete row key for cursor P1.
3170** There is no interpretation of the key. It is just copied
3171** onto the stack exactly as it is found in the database file.
3172**
3173** If the cursor is not pointing to a valid row, a NULL is pushed
3174** onto the stack.
3175*/
3176case OP_RowKey:
drh70ce3f02003-04-15 19:22:22 +00003177case OP_RowData: {
3178 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003179 Cursor *pC;
3180 int n;
3181
drh6810ce62004-01-31 19:22:56 +00003182 pTos++;
drh70ce3f02003-04-15 19:22:22 +00003183 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003184 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003185 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003186 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003187 }else if( pC->pCursor!=0 ){
3188 BtCursor *pCrsr = pC->pCursor;
danielk19774adee202004-05-08 08:23:19 +00003189 sqlite3VdbeCursorMoveto(pC);
drh70ce3f02003-04-15 19:22:22 +00003190 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003191 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003192 break;
drh143f3c42004-01-07 20:37:52 +00003193 }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
danielk19776490beb2004-05-11 06:17:21 +00003194 i64 n64;
3195 assert( !pC->intKey );
3196 sqlite3BtreeKeySize(pCrsr, &n64);
3197 n = n64;
drh70ce3f02003-04-15 19:22:22 +00003198 }else{
danielk19774adee202004-05-08 08:23:19 +00003199 sqlite3BtreeDataSize(pCrsr, &n);
drh70ce3f02003-04-15 19:22:22 +00003200 }
drh6810ce62004-01-31 19:22:56 +00003201 pTos->n = n;
drh70ce3f02003-04-15 19:22:22 +00003202 if( n<=NBFS ){
danielk1977106bb232004-05-21 10:08:53 +00003203 pTos->flags = MEM_Blob | MEM_Short;
drh6810ce62004-01-31 19:22:56 +00003204 pTos->z = pTos->zShort;
drh70ce3f02003-04-15 19:22:22 +00003205 }else{
3206 char *z = sqliteMallocRaw( n );
3207 if( z==0 ) goto no_mem;
danielk1977106bb232004-05-21 10:08:53 +00003208 pTos->flags = MEM_Blob | MEM_Dyn;
drh6810ce62004-01-31 19:22:56 +00003209 pTos->z = z;
drh70ce3f02003-04-15 19:22:22 +00003210 }
drh143f3c42004-01-07 20:37:52 +00003211 if( pC->keyAsData || pOp->opcode==OP_RowKey ){
danielk19774adee202004-05-08 08:23:19 +00003212 sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003213 }else{
danielk19774adee202004-05-08 08:23:19 +00003214 sqlite3BtreeData(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003215 }
3216 }else if( pC->pseudoTable ){
drh6810ce62004-01-31 19:22:56 +00003217 pTos->n = pC->nData;
3218 pTos->z = pC->pData;
danielk1977106bb232004-05-21 10:08:53 +00003219 pTos->flags = MEM_Blob|MEM_Ephem;
drh70ce3f02003-04-15 19:22:22 +00003220 }else{
drh6810ce62004-01-31 19:22:56 +00003221 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00003222 }
3223 break;
3224}
3225
drh5e00f6c2001-09-13 13:46:56 +00003226/* Opcode: Recno P1 * *
3227**
3228** Push onto the stack an integer which is the first 4 bytes of the
3229** the key to the current entry in a sequential scan of the database
3230** file P1. The sequential scan should have been started using the
3231** Next opcode.
3232*/
3233case OP_Recno: {
3234 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003235 Cursor *pC;
drhf328bc82004-05-10 23:29:49 +00003236 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003237
drh70ce3f02003-04-15 19:22:22 +00003238 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003239 pC = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00003240 sqlite3VdbeCursorMoveto(pC);
drh6810ce62004-01-31 19:22:56 +00003241 pTos++;
drha11846b2004-01-07 18:52:56 +00003242 if( pC->recnoIsValid ){
drh70ce3f02003-04-15 19:22:22 +00003243 v = pC->lastRecno;
drh70ce3f02003-04-15 19:22:22 +00003244 }else if( pC->pseudoTable ){
3245 v = keyToInt(pC->iKey);
drhd60ccc62003-06-24 10:39:46 +00003246 }else if( pC->nullRow || pC->pCursor==0 ){
drh6810ce62004-01-31 19:22:56 +00003247 pTos->flags = MEM_Null;
drhd60ccc62003-06-24 10:39:46 +00003248 break;
drh70ce3f02003-04-15 19:22:22 +00003249 }else{
3250 assert( pC->pCursor!=0 );
drhf328bc82004-05-10 23:29:49 +00003251 sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
drh70ce3f02003-04-15 19:22:22 +00003252 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003253 }
drh6810ce62004-01-31 19:22:56 +00003254 pTos->i = v;
3255 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003256 break;
3257}
3258
3259/* Opcode: FullKey P1 * *
3260**
drhb19a2bc2001-09-16 00:13:26 +00003261** Extract the complete key from the record that cursor P1 is currently
3262** pointing to and push the key onto the stack as a string.
3263**
3264** Compare this opcode to Recno. The Recno opcode extracts the first
3265** 4 bytes of the key and pushes those bytes onto the stack as an
3266** integer. This instruction pushes the entire key as a string.
drh70ce3f02003-04-15 19:22:22 +00003267**
3268** This opcode may not be used on a pseudo-table.
drh5e00f6c2001-09-13 13:46:56 +00003269*/
3270case OP_FullKey: {
3271 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00003272 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003273 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003274
drhd7556d22004-05-14 21:59:40 +00003275 assert( p->apCsr[i]->keyAsData );
3276 assert( !p->apCsr[i]->pseudoTable );
drh6810ce62004-01-31 19:22:56 +00003277 assert( i>=0 && i<p->nCursor );
3278 pTos++;
drhd7556d22004-05-14 21:59:40 +00003279 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197736a3c702004-05-11 06:55:14 +00003280 u64 amt;
drh5e00f6c2001-09-13 13:46:56 +00003281 char *z;
3282
drhd7556d22004-05-14 21:59:40 +00003283 sqlite3VdbeCursorMoveto(pC);
3284 assert( pC->intKey==0 );
danielk197736a3c702004-05-11 06:55:14 +00003285 sqlite3BtreeKeySize(pCrsr, &amt);
drh5e00f6c2001-09-13 13:46:56 +00003286 if( amt<=0 ){
3287 rc = SQLITE_CORRUPT;
3288 goto abort_due_to_error;
3289 }
drh9bbca4c2001-11-06 04:00:18 +00003290 if( amt>NBFS ){
drh8c1238a2003-01-02 14:43:55 +00003291 z = sqliteMallocRaw( amt );
drhcaec2f12003-01-07 02:47:47 +00003292 if( z==0 ) goto no_mem;
danielk1977106bb232004-05-21 10:08:53 +00003293 pTos->flags = MEM_Blob | MEM_Dyn;
drh9bbca4c2001-11-06 04:00:18 +00003294 }else{
drh6810ce62004-01-31 19:22:56 +00003295 z = pTos->zShort;
danielk1977106bb232004-05-21 10:08:53 +00003296 pTos->flags = MEM_Blob | MEM_Short;
drh9bbca4c2001-11-06 04:00:18 +00003297 }
danielk19774adee202004-05-08 08:23:19 +00003298 sqlite3BtreeKey(pCrsr, 0, amt, z);
drh6810ce62004-01-31 19:22:56 +00003299 pTos->z = z;
3300 pTos->n = amt;
drh5e00f6c2001-09-13 13:46:56 +00003301 }
3302 break;
3303}
3304
drh17f71932002-02-21 12:01:27 +00003305/* Opcode: NullRow P1 * *
3306**
3307** Move the cursor P1 to a null row. Any OP_Column operations
3308** that occur while the cursor is on the null row will always push
3309** a NULL onto the stack.
3310*/
3311case OP_NullRow: {
3312 int i = pOp->p1;
drhd7556d22004-05-14 21:59:40 +00003313 Cursor *pC;
drh17f71932002-02-21 12:01:27 +00003314
drh70ce3f02003-04-15 19:22:22 +00003315 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003316 pC = p->apCsr[i];
3317 pC->nullRow = 1;
3318 pC->recnoIsValid = 0;
drh17f71932002-02-21 12:01:27 +00003319 break;
3320}
3321
drh9562b552002-02-19 15:00:07 +00003322/* Opcode: Last P1 P2 *
3323**
3324** The next use of the Recno or Column or Next instruction for P1
3325** will refer to the last entry in the database table or index.
3326** If the table or index is empty and P2>0, then jump immediately to P2.
3327** If P2 is 0 or if the table or index is not empty, fall through
3328** to the following instruction.
3329*/
3330case OP_Last: {
3331 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003332 Cursor *pC;
drh9562b552002-02-19 15:00:07 +00003333 BtCursor *pCrsr;
3334
drh70ce3f02003-04-15 19:22:22 +00003335 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003336 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003337 if( (pCrsr = pC->pCursor)!=0 ){
drh9562b552002-02-19 15:00:07 +00003338 int res;
danielk19774adee202004-05-08 08:23:19 +00003339 rc = sqlite3BtreeLast(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00003340 pC->nullRow = res;
3341 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003342 pC->cacheValid = 0;
drh9562b552002-02-19 15:00:07 +00003343 if( res && pOp->p2>0 ){
3344 pc = pOp->p2 - 1;
3345 }
drh70ce3f02003-04-15 19:22:22 +00003346 }else{
3347 pC->nullRow = 0;
drh9562b552002-02-19 15:00:07 +00003348 }
3349 break;
3350}
3351
drh8721ce42001-11-07 14:22:00 +00003352/* Opcode: Rewind P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003353**
3354** The next use of the Recno or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003355** will refer to the first entry in the database table or index.
3356** If the table or index is empty and P2>0, then jump immediately to P2.
3357** If P2 is 0 or if the table or index is not empty, fall through
3358** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003359*/
3360case OP_Rewind: {
3361 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003362 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003363 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003364 int res;
drh5e00f6c2001-09-13 13:46:56 +00003365
drh70ce3f02003-04-15 19:22:22 +00003366 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003367 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003368 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003369 rc = sqlite3BtreeFirst(pCrsr, &res);
drh70ce3f02003-04-15 19:22:22 +00003370 pC->atFirst = res==0;
drha11846b2004-01-07 18:52:56 +00003371 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003372 pC->cacheValid = 0;
drh70ce3f02003-04-15 19:22:22 +00003373 }else{
drhf4dada72004-05-11 09:57:35 +00003374 res = 1;
3375 }
3376 pC->nullRow = res;
3377 if( res && pOp->p2>0 ){
3378 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003379 }
3380 break;
3381}
3382
3383/* Opcode: Next P1 P2 *
3384**
3385** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003386** table or index. If there are no more key/value pairs then fall through
3387** to the following instruction. But if the cursor advance was successful,
3388** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003389**
3390** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003391*/
drhc045ec52002-12-04 20:01:06 +00003392/* Opcode: Prev P1 P2 *
3393**
3394** Back up cursor P1 so that it points to the previous key/data pair in its
3395** table or index. If there is no previous key/value pairs then fall through
3396** to the following instruction. But if the cursor backup was successful,
3397** jump immediately to P2.
3398*/
3399case OP_Prev:
drh6b563442001-11-07 16:48:26 +00003400case OP_Next: {
drh7b396862003-01-01 23:06:20 +00003401 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003402 BtCursor *pCrsr;
3403
drhcaec2f12003-01-07 02:47:47 +00003404 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00003405 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003406 pC = p->apCsr[pOp->p1];
drh70ce3f02003-04-15 19:22:22 +00003407 if( (pCrsr = pC->pCursor)!=0 ){
drh8721ce42001-11-07 14:22:00 +00003408 int res;
drh7b396862003-01-01 23:06:20 +00003409 if( pC->nullRow ){
drhad2d8302002-05-24 20:31:36 +00003410 res = 1;
3411 }else{
drha11846b2004-01-07 18:52:56 +00003412 assert( pC->deferredMoveto==0 );
danielk19774adee202004-05-08 08:23:19 +00003413 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3414 sqlite3BtreePrevious(pCrsr, &res);
drh7b396862003-01-01 23:06:20 +00003415 pC->nullRow = res;
drh9188b382004-05-14 21:12:22 +00003416 pC->cacheValid = 0;
drhad2d8302002-05-24 20:31:36 +00003417 }
drh8721ce42001-11-07 14:22:00 +00003418 if( res==0 ){
3419 pc = pOp->p2 - 1;
danielk19776f8a5032004-05-10 10:34:51 +00003420 sqlite3_search_count++;
drh8721ce42001-11-07 14:22:00 +00003421 }
drh70ce3f02003-04-15 19:22:22 +00003422 }else{
3423 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00003424 }
drh70ce3f02003-04-15 19:22:22 +00003425 pC->recnoIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00003426 break;
3427}
3428
drh8721ce42001-11-07 14:22:00 +00003429/* Opcode: IdxPut P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00003430**
jplyon5a564222003-06-02 06:15:58 +00003431** The top of the stack holds a SQL index key made using the
drh5e00f6c2001-09-13 13:46:56 +00003432** MakeIdxKey instruction. This opcode writes that key into the
3433** index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00003434**
3435** If P2==1, then the key must be unique. If the key is not unique,
3436** the program aborts with a SQLITE_CONSTRAINT error and the database
jplyon5a564222003-06-02 06:15:58 +00003437** is rolled back. If P3 is not null, then it becomes part of the
drh717e6402001-09-27 03:22:32 +00003438** error message returned with the SQLITE_CONSTRAINT.
drh5e00f6c2001-09-13 13:46:56 +00003439*/
drh8721ce42001-11-07 14:22:00 +00003440case OP_IdxPut: {
drh5e00f6c2001-09-13 13:46:56 +00003441 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003442 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003443 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003444 assert( pTos>=p->aStack );
3445 assert( i>=0 && i<p->nCursor );
danielk1977106bb232004-05-21 10:08:53 +00003446 assert( pTos->flags & MEM_Blob );
drhd7556d22004-05-14 21:59:40 +00003447 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh6810ce62004-01-31 19:22:56 +00003448 int nKey = pTos->n;
3449 const char *zKey = pTos->z;
drh717e6402001-09-27 03:22:32 +00003450 if( pOp->p2 ){
danielk197736a3c702004-05-11 06:55:14 +00003451 int res;
danielk1977452c9892004-05-13 05:16:15 +00003452 int len;
danielk1977452c9892004-05-13 05:16:15 +00003453
3454 /* 'len' is the length of the key minus the rowid at the end */
drhf3218fe2004-05-28 08:21:02 +00003455 len = nKey - sqlite3VdbeIdxRowidLen(nKey, zKey);
danielk1977452c9892004-05-13 05:16:15 +00003456
3457 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
drh717e6402001-09-27 03:22:32 +00003458 if( rc!=SQLITE_OK ) goto abort_due_to_error;
drhf3218fe2004-05-28 08:21:02 +00003459 while( res!=0 && !sqlite3BtreeEof(pCrsr) ){
drh717e6402001-09-27 03:22:32 +00003460 int c;
drhf3218fe2004-05-28 08:21:02 +00003461 if( sqlite3VdbeIdxKeyCompare(pC, len, zKey, &c)==SQLITE_OK && c==0 ){
drh717e6402001-09-27 03:22:32 +00003462 rc = SQLITE_CONSTRAINT;
3463 if( pOp->p3 && pOp->p3[0] ){
danielk19774adee202004-05-08 08:23:19 +00003464 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
drh717e6402001-09-27 03:22:32 +00003465 }
3466 goto abort_due_to_error;
3467 }
3468 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003469 sqlite3BtreeNext(pCrsr, &res);
drh717e6402001-09-27 03:22:32 +00003470 res = +1;
3471 }else{
3472 break;
3473 }
3474 }
3475 }
drh9188b382004-05-14 21:12:22 +00003476 assert( pC->intKey==0 );
danielk19774adee202004-05-08 08:23:19 +00003477 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0);
drh9188b382004-05-14 21:12:22 +00003478 assert( pC->deferredMoveto==0 );
3479 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003480 }
drh6810ce62004-01-31 19:22:56 +00003481 Release(pTos);
3482 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003483 break;
3484}
3485
drh8721ce42001-11-07 14:22:00 +00003486/* Opcode: IdxDelete P1 * *
drh5e00f6c2001-09-13 13:46:56 +00003487**
3488** The top of the stack is an index key built using the MakeIdxKey opcode.
3489** This opcode removes that entry from the index.
3490*/
drh8721ce42001-11-07 14:22:00 +00003491case OP_IdxDelete: {
drh5e00f6c2001-09-13 13:46:56 +00003492 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003493 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003494 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003495 assert( pTos>=p->aStack );
danielk1977106bb232004-05-21 10:08:53 +00003496 assert( pTos->flags & MEM_Blob );
drh6810ce62004-01-31 19:22:56 +00003497 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003498 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh5e00f6c2001-09-13 13:46:56 +00003499 int rx, res;
danielk19774adee202004-05-08 08:23:19 +00003500 rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
drh5e00f6c2001-09-13 13:46:56 +00003501 if( rx==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00003502 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00003503 }
drh9188b382004-05-14 21:12:22 +00003504 assert( pC->deferredMoveto==0 );
3505 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003506 }
drh6810ce62004-01-31 19:22:56 +00003507 Release(pTos);
3508 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003509 break;
3510}
3511
drh8721ce42001-11-07 14:22:00 +00003512/* Opcode: IdxRecno P1 * *
3513**
danielk1977452c9892004-05-13 05:16:15 +00003514** Push onto the stack an integer which is the varint located at the
3515** end of the index key pointed to by cursor P1. These integer should be
3516** the record number of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00003517**
3518** See also: Recno, MakeIdxKey.
3519*/
3520case OP_IdxRecno: {
3521 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003522 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003523 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003524
drh6810ce62004-01-31 19:22:56 +00003525 assert( i>=0 && i<p->nCursor );
3526 pTos++;
drhd7556d22004-05-14 21:59:40 +00003527 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00003528 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00003529
drhd7556d22004-05-14 21:59:40 +00003530 assert( pC->deferredMoveto==0 );
3531 assert( pC->intKey==0 );
danielk19773d1bfea2004-05-14 11:00:53 +00003532 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3533 if( rc!=SQLITE_OK ){
3534 goto abort_due_to_error;
3535 }
3536 pTos->flags = MEM_Int;
3537 pTos->i = rowid;
danielk1977452c9892004-05-13 05:16:15 +00003538
danielk19773d1bfea2004-05-14 11:00:53 +00003539#if 0
danielk1977452c9892004-05-13 05:16:15 +00003540 /* Read the final 9 bytes of the key into buf[]. If the whole key is
3541 ** less than 9 bytes then just load the whole thing. Set len to the
3542 ** number of bytes read.
3543 */
danielk19776490beb2004-05-11 06:17:21 +00003544 sqlite3BtreeKeySize(pCrsr, &sz);
danielk19773d1bfea2004-05-14 11:00:53 +00003545 len = ((sz>10)?10:sz);
danielk1977452c9892004-05-13 05:16:15 +00003546 rc = sqlite3BtreeKey(pCrsr, sz-len, len, buf);
3547 if( rc!=SQLITE_OK ){
3548 goto abort_due_to_error;
3549 }
3550
3551 len--;
3552 if( buf[len]&0x80 ){
3553 /* If the last byte read has the 0x80 bit set, then the key does
3554 ** not end with a varint. Push a NULL onto the stack instead.
3555 */
drh6810ce62004-01-31 19:22:56 +00003556 pTos->flags = MEM_Null;
drhd4d595f2003-04-17 12:44:23 +00003557 }else{
danielk1977452c9892004-05-13 05:16:15 +00003558 /* Find the start of the varint by searching backwards for a 0x00
3559 ** byte. If one does not exists, then intepret the whole 9 bytes as a
3560 ** varint.
3561 */
3562 while( len && buf[len-1] ){
3563 len--;
3564 }
drh25aa1b42004-05-28 01:39:01 +00003565 sqlite3GetVarint32(&buf[len], &sz);
drh6810ce62004-01-31 19:22:56 +00003566 pTos->flags = MEM_Int;
danielk1977452c9892004-05-13 05:16:15 +00003567 pTos->i = sz;
drhd4d595f2003-04-17 12:44:23 +00003568 }
danielk19773d1bfea2004-05-14 11:00:53 +00003569#endif
drh6810ce62004-01-31 19:22:56 +00003570 }else{
3571 pTos->flags = MEM_Null;
drh8721ce42001-11-07 14:22:00 +00003572 }
3573 break;
3574}
3575
drh7cf6e4d2004-05-19 14:56:55 +00003576/* Opcode: IdxGT P1 P2 *
drh8721ce42001-11-07 14:22:00 +00003577**
drhf3218fe2004-05-28 08:21:02 +00003578** The top of the stack is an index entry that omits the ROWID. Compare
3579** the top of stack against the index that P1 is currently pointing to.
3580** Ignore the ROWID on the P1 index.
3581**
3582** The top of the stack might have fewer columns that P1.
3583**
3584** If the P1 index entry is greater than the top of the stack
drh8721ce42001-11-07 14:22:00 +00003585** then jump to P2. Otherwise fall through to the next instruction.
3586** In either case, the stack is popped once.
3587*/
drh7cf6e4d2004-05-19 14:56:55 +00003588/* Opcode: IdxGE P1 P2 P3
drh8721ce42001-11-07 14:22:00 +00003589**
drhf3218fe2004-05-28 08:21:02 +00003590** The top of the stack is an index entry that omits the ROWID. Compare
3591** the top of stack against the index that P1 is currently pointing to.
3592** Ignore the ROWID on the P1 index.
3593**
3594** If the P1 index entry is greater than or equal to the top of the stack
drh8721ce42001-11-07 14:22:00 +00003595** then jump to P2. Otherwise fall through to the next instruction.
3596** In either case, the stack is popped once.
drh772ae622004-05-19 13:13:08 +00003597**
3598** If P3 is the "+" string (or any other non-NULL string) then the
3599** index taken from the top of the stack is temporarily increased by
drh7cf6e4d2004-05-19 14:56:55 +00003600** an epsilon prior to the comparison. This make the opcode work
3601** like IdxGT except that if the key from the stack is a prefix of
3602** the key in the cursor, the result is false whereas it would be
3603** true with IdxGT.
drh8721ce42001-11-07 14:22:00 +00003604*/
drh7cf6e4d2004-05-19 14:56:55 +00003605/* Opcode: IdxLT P1 P2 P3
drhc045ec52002-12-04 20:01:06 +00003606**
drhf3218fe2004-05-28 08:21:02 +00003607** The top of the stack is an index entry that omits the ROWID. Compare
3608** the top of stack against the index that P1 is currently pointing to.
3609** Ignore the ROWID on the P1 index.
3610**
3611** If the P1 index entry is less than the top of the stack
drhc045ec52002-12-04 20:01:06 +00003612** then jump to P2. Otherwise fall through to the next instruction.
3613** In either case, the stack is popped once.
drh772ae622004-05-19 13:13:08 +00003614**
3615** If P3 is the "+" string (or any other non-NULL string) then the
3616** index taken from the top of the stack is temporarily increased by
drh7cf6e4d2004-05-19 14:56:55 +00003617** an epsilon prior to the comparison. This makes the opcode work
3618** like IdxLE.
drhc045ec52002-12-04 20:01:06 +00003619*/
3620case OP_IdxLT:
drh8721ce42001-11-07 14:22:00 +00003621case OP_IdxGT:
3622case OP_IdxGE: {
3623 int i= pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003624 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003625 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003626
drh6810ce62004-01-31 19:22:56 +00003627 assert( i>=0 && i<p->nCursor );
3628 assert( pTos>=p->aStack );
drhd7556d22004-05-14 21:59:40 +00003629 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh8721ce42001-11-07 14:22:00 +00003630 int res, rc;
3631
drhf3218fe2004-05-28 08:21:02 +00003632 assert( pTos->flags & MEM_Blob ); /* Created using OP_Make*Key */
danielk19778a6b5412004-05-24 07:04:25 +00003633 Stringify(pTos, db->enc);
drhd7556d22004-05-14 21:59:40 +00003634 assert( pC->deferredMoveto==0 );
drhd3d39e92004-05-20 22:16:29 +00003635 *pC->pIncrKey = pOp->p3!=0;
drh7cf6e4d2004-05-19 14:56:55 +00003636 assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
3637 rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, pTos->z, &res);
drhd3d39e92004-05-20 22:16:29 +00003638 *pC->pIncrKey = 0;
drh8721ce42001-11-07 14:22:00 +00003639 if( rc!=SQLITE_OK ){
3640 break;
3641 }
drhc045ec52002-12-04 20:01:06 +00003642 if( pOp->opcode==OP_IdxLT ){
3643 res = -res;
3644 }else if( pOp->opcode==OP_IdxGE ){
drh8721ce42001-11-07 14:22:00 +00003645 res++;
3646 }
3647 if( res>0 ){
3648 pc = pOp->p2 - 1 ;
3649 }
3650 }
drh6810ce62004-01-31 19:22:56 +00003651 Release(pTos);
3652 pTos--;
drh8721ce42001-11-07 14:22:00 +00003653 break;
3654}
3655
drh143f3c42004-01-07 20:37:52 +00003656/* Opcode: IdxIsNull P1 P2 *
3657**
3658** The top of the stack contains an index entry such as might be generated
3659** by the MakeIdxKey opcode. This routine looks at the first P1 fields of
3660** that key. If any of the first P1 fields are NULL, then a jump is made
3661** to address P2. Otherwise we fall straight through.
3662**
3663** The index entry is always popped from the stack.
3664*/
3665case OP_IdxIsNull: {
3666 int i = pOp->p1;
drh143f3c42004-01-07 20:37:52 +00003667 int k, n;
3668 const char *z;
drhf3218fe2004-05-28 08:21:02 +00003669 u32 serial_type;
drh143f3c42004-01-07 20:37:52 +00003670
drh6810ce62004-01-31 19:22:56 +00003671 assert( pTos>=p->aStack );
danielk1977106bb232004-05-21 10:08:53 +00003672 assert( pTos->flags & MEM_Blob );
drh6810ce62004-01-31 19:22:56 +00003673 z = pTos->z;
3674 n = pTos->n;
drhf3218fe2004-05-28 08:21:02 +00003675 k = sqlite3GetVarint32(z, &serial_type);
3676 for(; k<n && i>0; i--){
drh25aa1b42004-05-28 01:39:01 +00003677 k += sqlite3GetVarint32(&z[k], &serial_type);
danielk19773d1bfea2004-05-14 11:00:53 +00003678 if( serial_type==6 ){ /* Serial type 6 is a NULL */
drh143f3c42004-01-07 20:37:52 +00003679 pc = pOp->p2-1;
3680 break;
3681 }
drh143f3c42004-01-07 20:37:52 +00003682 }
drh6810ce62004-01-31 19:22:56 +00003683 Release(pTos);
3684 pTos--;
drh143f3c42004-01-07 20:37:52 +00003685 break;
3686}
3687
drhf57b3392001-10-08 13:22:32 +00003688/* Opcode: Destroy P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003689**
3690** Delete an entire database table or index whose root page in the database
3691** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00003692**
drhf57b3392001-10-08 13:22:32 +00003693** The table being destroyed is in the main database file if P2==0. If
3694** P2==1 then the table to be clear is in the auxiliary database file
3695** that is used to store tables create using CREATE TEMPORARY TABLE.
3696**
drhb19a2bc2001-09-16 00:13:26 +00003697** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00003698*/
3699case OP_Destroy: {
danielk19774adee202004-05-08 08:23:19 +00003700 rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
drh5e00f6c2001-09-13 13:46:56 +00003701 break;
3702}
3703
drhf57b3392001-10-08 13:22:32 +00003704/* Opcode: Clear P1 P2 *
drh5edc3122001-09-13 21:53:09 +00003705**
3706** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00003707** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00003708** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00003709**
drhf57b3392001-10-08 13:22:32 +00003710** The table being clear is in the main database file if P2==0. If
3711** P2==1 then the table to be clear is in the auxiliary database file
3712** that is used to store tables create using CREATE TEMPORARY TABLE.
3713**
drhb19a2bc2001-09-16 00:13:26 +00003714** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00003715*/
3716case OP_Clear: {
danielk19774adee202004-05-08 08:23:19 +00003717 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
drh5edc3122001-09-13 21:53:09 +00003718 break;
3719}
3720
drhf57b3392001-10-08 13:22:32 +00003721/* Opcode: CreateTable * P2 P3
drh5b2fd562001-09-13 15:21:31 +00003722**
drhf57b3392001-10-08 13:22:32 +00003723** Allocate a new table in the main database file if P2==0 or in the
3724** auxiliary database file if P2==1. Push the page number
drh5b2fd562001-09-13 15:21:31 +00003725** for the root page of the new table onto the stack.
3726**
drhadbca9c2001-09-27 15:11:53 +00003727** The root page number is also written to a memory location that P3
3728** points to. This is the mechanism is used to write the root page
3729** number into the parser's internal data structures that describe the
3730** new table.
drhb19a2bc2001-09-16 00:13:26 +00003731**
drhc6b52df2002-01-04 03:09:29 +00003732** The difference between a table and an index is this: A table must
3733** have a 4-byte integer key and can have arbitrary data. An index
3734** has an arbitrary key but no data.
3735**
drhb19a2bc2001-09-16 00:13:26 +00003736** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00003737*/
drhf57b3392001-10-08 13:22:32 +00003738/* Opcode: CreateIndex * P2 P3
3739**
drhc6b52df2002-01-04 03:09:29 +00003740** Allocate a new index in the main database file if P2==0 or in the
3741** auxiliary database file if P2==1. Push the page number of the
3742** root page of the new index onto the stack.
drhf57b3392001-10-08 13:22:32 +00003743**
drhc6b52df2002-01-04 03:09:29 +00003744** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00003745*/
3746case OP_CreateIndex:
drh5b2fd562001-09-13 15:21:31 +00003747case OP_CreateTable: {
drh5b2fd562001-09-13 15:21:31 +00003748 int pgno;
drhf328bc82004-05-10 23:29:49 +00003749 int flags;
drh99fcd712001-10-13 01:06:47 +00003750 assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
drh001bbcb2003-03-19 03:14:00 +00003751 assert( pOp->p2>=0 && pOp->p2<db->nDb );
3752 assert( db->aDb[pOp->p2].pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00003753 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00003754 /* flags = BTREE_INTKEY; */
3755 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00003756 }else{
drhf328bc82004-05-10 23:29:49 +00003757 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00003758 }
drhf328bc82004-05-10 23:29:49 +00003759 rc = sqlite3BtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno, flags);
drh6810ce62004-01-31 19:22:56 +00003760 pTos++;
drh5b2fd562001-09-13 15:21:31 +00003761 if( rc==SQLITE_OK ){
drh6810ce62004-01-31 19:22:56 +00003762 pTos->i = pgno;
3763 pTos->flags = MEM_Int;
drhadbca9c2001-09-27 15:11:53 +00003764 *(u32*)pOp->p3 = pgno;
3765 pOp->p3 = 0;
drhf1b07b02004-02-08 06:17:19 +00003766 }else{
3767 pTos->flags = MEM_Null;
drh5b2fd562001-09-13 15:21:31 +00003768 }
3769 break;
3770}
3771
drh79069752004-05-22 21:30:40 +00003772/* Opcode: IntegrityCk * P2 *
drh5e00f6c2001-09-13 13:46:56 +00003773**
drh1dd397f2002-02-03 03:34:07 +00003774** Do an analysis of the currently open database. Push onto the
3775** stack the text of an error message describing any problems.
3776** If there are no errors, push a "ok" onto the stack.
drhb19a2bc2001-09-16 00:13:26 +00003777**
drh79069752004-05-22 21:30:40 +00003778** The root page numbers of all tables in the database are integer
3779** values on the stack. This opcode pulls as many integers as it
3780** can off of the stack and uses those numbers as the root pages.
drh21504322002-06-25 13:16:02 +00003781**
3782** If P2 is not zero, the check is done on the auxiliary database
3783** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00003784**
3785** This opcode is used for testing purposes only.
drh5e00f6c2001-09-13 13:46:56 +00003786*/
drhaaab5722002-02-19 13:39:21 +00003787case OP_IntegrityCk: {
drh1dd397f2002-02-03 03:34:07 +00003788 int nRoot;
3789 int *aRoot;
drh1dd397f2002-02-03 03:34:07 +00003790 int j;
drh1dd397f2002-02-03 03:34:07 +00003791 char *z;
3792
drh79069752004-05-22 21:30:40 +00003793 for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
3794 if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
3795 }
3796 assert( nRoot>0 );
3797 aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) );
drhcaec2f12003-01-07 02:47:47 +00003798 if( aRoot==0 ) goto no_mem;
drh79069752004-05-22 21:30:40 +00003799 for(j=0; j<nRoot; j++){
3800 Mem *pMem = &pTos[-j];
3801 aRoot[j] = pMem->i;
drh1dd397f2002-02-03 03:34:07 +00003802 }
3803 aRoot[j] = 0;
drh79069752004-05-22 21:30:40 +00003804 popStack(&pTos, nRoot);
3805 pTos++;
danielk19774adee202004-05-08 08:23:19 +00003806 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
drh1dd397f2002-02-03 03:34:07 +00003807 if( z==0 || z[0]==0 ){
drh21504322002-06-25 13:16:02 +00003808 if( z ) sqliteFree(z);
drh6810ce62004-01-31 19:22:56 +00003809 pTos->z = "ok";
drhf4479502004-05-27 03:12:53 +00003810 pTos->n = 2;
3811 pTos->flags = MEM_Str | MEM_Static | MEM_Term;
drh1dd397f2002-02-03 03:34:07 +00003812 }else{
drh6810ce62004-01-31 19:22:56 +00003813 pTos->z = z;
drhf4479502004-05-27 03:12:53 +00003814 pTos->n = strlen(z);
3815 pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
danielk19778a6b5412004-05-24 07:04:25 +00003816 }
drhf4479502004-05-27 03:12:53 +00003817 pTos->enc = TEXT_Utf8;
3818 sqlite3VdbeChangeEncoding(pTos, db->enc);
drh24e97df2002-02-03 19:06:02 +00003819 sqliteFree(aRoot);
drh5e00f6c2001-09-13 13:46:56 +00003820 break;
3821}
3822
drha8b38d22001-11-01 14:41:34 +00003823/* Opcode: ListWrite * * *
drh5e00f6c2001-09-13 13:46:56 +00003824**
3825** Write the integer on the top of the stack
drha8b38d22001-11-01 14:41:34 +00003826** into the temporary storage list.
drh5e00f6c2001-09-13 13:46:56 +00003827*/
3828case OP_ListWrite: {
drh5e00f6c2001-09-13 13:46:56 +00003829 Keylist *pKeylist;
drh6810ce62004-01-31 19:22:56 +00003830 assert( pTos>=p->aStack );
drha8b38d22001-11-01 14:41:34 +00003831 pKeylist = p->pList;
drh5e00f6c2001-09-13 13:46:56 +00003832 if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
drh8c1238a2003-01-02 14:43:55 +00003833 pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
drh5e00f6c2001-09-13 13:46:56 +00003834 if( pKeylist==0 ) goto no_mem;
3835 pKeylist->nKey = 1000;
3836 pKeylist->nRead = 0;
3837 pKeylist->nUsed = 0;
drha8b38d22001-11-01 14:41:34 +00003838 pKeylist->pNext = p->pList;
3839 p->pList = pKeylist;
drh5e00f6c2001-09-13 13:46:56 +00003840 }
danielk19778a6b5412004-05-24 07:04:25 +00003841 Integerify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00003842 pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
drh79f14b72004-03-03 01:51:24 +00003843 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00003844 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003845 break;
3846}
3847
drha8b38d22001-11-01 14:41:34 +00003848/* Opcode: ListRewind * * *
drh5e00f6c2001-09-13 13:46:56 +00003849**
drhfb044c12004-02-10 13:41:52 +00003850** Rewind the temporary buffer back to the beginning.
drh5e00f6c2001-09-13 13:46:56 +00003851*/
3852case OP_ListRewind: {
drhfb044c12004-02-10 13:41:52 +00003853 /* What this opcode codes, really, is reverse the order of the
3854 ** linked list of Keylist structures so that they are read out
3855 ** in the same order that they were read in. */
3856 Keylist *pRev, *pTop;
3857 pRev = 0;
3858 while( p->pList ){
3859 pTop = p->pList;
3860 p->pList = pTop->pNext;
3861 pTop->pNext = pRev;
3862 pRev = pTop;
3863 }
3864 p->pList = pRev;
drh5e00f6c2001-09-13 13:46:56 +00003865 break;
3866}
3867
drha8b38d22001-11-01 14:41:34 +00003868/* Opcode: ListRead * P2 *
drh5e00f6c2001-09-13 13:46:56 +00003869**
drha8b38d22001-11-01 14:41:34 +00003870** Attempt to read an integer from the temporary storage buffer
drh5e00f6c2001-09-13 13:46:56 +00003871** and push it onto the stack. If the storage buffer is empty,
3872** push nothing but instead jump to P2.
3873*/
3874case OP_ListRead: {
drh5e00f6c2001-09-13 13:46:56 +00003875 Keylist *pKeylist;
drhcaec2f12003-01-07 02:47:47 +00003876 CHECK_FOR_INTERRUPT;
drha8b38d22001-11-01 14:41:34 +00003877 pKeylist = p->pList;
drh5e00f6c2001-09-13 13:46:56 +00003878 if( pKeylist!=0 ){
drh6810ce62004-01-31 19:22:56 +00003879 assert( pKeylist->nRead>=0 );
3880 assert( pKeylist->nRead<pKeylist->nUsed );
3881 assert( pKeylist->nRead<pKeylist->nKey );
3882 pTos++;
3883 pTos->i = pKeylist->aKey[pKeylist->nRead++];
3884 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003885 if( pKeylist->nRead>=pKeylist->nUsed ){
drha8b38d22001-11-01 14:41:34 +00003886 p->pList = pKeylist->pNext;
drh5e00f6c2001-09-13 13:46:56 +00003887 sqliteFree(pKeylist);
3888 }
3889 }else{
3890 pc = pOp->p2 - 1;
3891 }
3892 break;
3893}
3894
drha8b38d22001-11-01 14:41:34 +00003895/* Opcode: ListReset * * *
drh5e00f6c2001-09-13 13:46:56 +00003896**
drha8b38d22001-11-01 14:41:34 +00003897** Reset the temporary storage buffer so that it holds nothing.
drh5e00f6c2001-09-13 13:46:56 +00003898*/
drha8b38d22001-11-01 14:41:34 +00003899case OP_ListReset: {
3900 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +00003901 sqlite3VdbeKeylistFree(p->pList);
drha8b38d22001-11-01 14:41:34 +00003902 p->pList = 0;
drh5e00f6c2001-09-13 13:46:56 +00003903 }
3904 break;
3905}
3906
drhbd5a4512002-05-23 22:07:02 +00003907/* Opcode: ListPush * * *
3908**
drhad3cab52002-05-24 02:04:32 +00003909** Save the current Vdbe list such that it can be restored by a ListPop
drhbd5a4512002-05-23 22:07:02 +00003910** opcode. The list is empty after this is executed.
3911*/
3912case OP_ListPush: {
3913 p->keylistStackDepth++;
3914 assert(p->keylistStackDepth > 0);
3915 p->keylistStack = sqliteRealloc(p->keylistStack,
3916 sizeof(Keylist *) * p->keylistStackDepth);
drhcaec2f12003-01-07 02:47:47 +00003917 if( p->keylistStack==0 ) goto no_mem;
drhbd5a4512002-05-23 22:07:02 +00003918 p->keylistStack[p->keylistStackDepth - 1] = p->pList;
3919 p->pList = 0;
3920 break;
3921}
3922
3923/* Opcode: ListPop * * *
3924**
drhad3cab52002-05-24 02:04:32 +00003925** Restore the Vdbe list to the state it was in when ListPush was last
drhbd5a4512002-05-23 22:07:02 +00003926** executed.
3927*/
3928case OP_ListPop: {
3929 assert(p->keylistStackDepth > 0);
3930 p->keylistStackDepth--;
danielk19774adee202004-05-08 08:23:19 +00003931 sqlite3VdbeKeylistFree(p->pList);
drhbd5a4512002-05-23 22:07:02 +00003932 p->pList = p->keylistStack[p->keylistStackDepth];
3933 p->keylistStack[p->keylistStackDepth] = 0;
3934 if( p->keylistStackDepth == 0 ){
3935 sqliteFree(p->keylistStack);
3936 p->keylistStack = 0;
3937 }
3938 break;
3939}
3940
rdcb0c374f2004-02-20 22:53:38 +00003941/* Opcode: ContextPush * * *
3942**
3943** Save the current Vdbe context such that it can be restored by a ContextPop
3944** opcode. The context stores the last insert row id, the last statement change
3945** count, and the current statement change count.
3946*/
3947case OP_ContextPush: {
3948 p->contextStackDepth++;
3949 assert(p->contextStackDepth > 0);
3950 p->contextStack = sqliteRealloc(p->contextStack,
3951 sizeof(Context) * p->contextStackDepth);
3952 if( p->contextStack==0 ) goto no_mem;
3953 p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
3954 p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
3955 p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
3956 break;
3957}
3958
3959/* Opcode: ContextPop * * *
3960**
3961** Restore the Vdbe context to the state it was in when contextPush was last
3962** executed. The context stores the last insert row id, the last statement
3963** change count, and the current statement change count.
3964*/
3965case OP_ContextPop: {
3966 assert(p->contextStackDepth > 0);
3967 p->contextStackDepth--;
3968 p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
3969 p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
3970 p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
3971 if( p->contextStackDepth == 0 ){
3972 sqliteFree(p->contextStack);
3973 p->contextStack = 0;
3974 }
3975 break;
3976}
3977
drha8b38d22001-11-01 14:41:34 +00003978/* Opcode: SortPut * * *
drh5e00f6c2001-09-13 13:46:56 +00003979**
3980** The TOS is the key and the NOS is the data. Pop both from the stack
drh9bbca4c2001-11-06 04:00:18 +00003981** and put them on the sorter. The key and data should have been
3982** made using SortMakeKey and SortMakeRec, respectively.
drh5e00f6c2001-09-13 13:46:56 +00003983*/
3984case OP_SortPut: {
drh6810ce62004-01-31 19:22:56 +00003985 Mem *pNos = &pTos[-1];
drh5e00f6c2001-09-13 13:46:56 +00003986 Sorter *pSorter;
drh6810ce62004-01-31 19:22:56 +00003987 assert( pNos>=p->aStack );
danielk1977c572ef72004-05-27 09:28:41 +00003988 Stringify(pNos, db->enc);
danielk19778a6b5412004-05-24 07:04:25 +00003989 if( Dynamicify(pTos, db->enc) || Dynamicify(pNos, db->enc) ) goto no_mem;
drh8c1238a2003-01-02 14:43:55 +00003990 pSorter = sqliteMallocRaw( sizeof(Sorter) );
drh5e00f6c2001-09-13 13:46:56 +00003991 if( pSorter==0 ) goto no_mem;
drha8b38d22001-11-01 14:41:34 +00003992 pSorter->pNext = p->pSort;
3993 p->pSort = pSorter;
drh6810ce62004-01-31 19:22:56 +00003994 assert( pTos->flags & MEM_Dyn );
3995 pSorter->nKey = pTos->n;
3996 pSorter->zKey = pTos->z;
3997 assert( pNos->flags & MEM_Dyn );
3998 pSorter->nData = pNos->n;
3999 pSorter->pData = pNos->z;
4000 pTos -= 2;
drh5e00f6c2001-09-13 13:46:56 +00004001 break;
4002}
4003
drhffbc3082004-05-21 01:29:06 +00004004/* Opcode: Sort * * P3
drh5e00f6c2001-09-13 13:46:56 +00004005**
drha8b38d22001-11-01 14:41:34 +00004006** Sort all elements on the sorter. The algorithm is a
drhffbc3082004-05-21 01:29:06 +00004007** mergesort. The P3 argument is a pointer to a KeyInfo structure
4008** that describes the keys to be sorted.
drh5e00f6c2001-09-13 13:46:56 +00004009*/
4010case OP_Sort: {
drha8b38d22001-11-01 14:41:34 +00004011 int i;
drhffbc3082004-05-21 01:29:06 +00004012 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
drha8b38d22001-11-01 14:41:34 +00004013 Sorter *pElem;
4014 Sorter *apSorter[NSORT];
drh60ca8042004-05-22 11:09:30 +00004015 pKeyInfo->enc = p->db->enc;
drha8b38d22001-11-01 14:41:34 +00004016 for(i=0; i<NSORT; i++){
4017 apSorter[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004018 }
drha8b38d22001-11-01 14:41:34 +00004019 while( p->pSort ){
4020 pElem = p->pSort;
4021 p->pSort = pElem->pNext;
4022 pElem->pNext = 0;
4023 for(i=0; i<NSORT-1; i++){
4024 if( apSorter[i]==0 ){
4025 apSorter[i] = pElem;
4026 break;
4027 }else{
drhffbc3082004-05-21 01:29:06 +00004028 pElem = Merge(apSorter[i], pElem, pKeyInfo);
drha8b38d22001-11-01 14:41:34 +00004029 apSorter[i] = 0;
4030 }
4031 }
4032 if( i>=NSORT-1 ){
drhffbc3082004-05-21 01:29:06 +00004033 apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem, pKeyInfo);
drha8b38d22001-11-01 14:41:34 +00004034 }
4035 }
4036 pElem = 0;
4037 for(i=0; i<NSORT; i++){
drhffbc3082004-05-21 01:29:06 +00004038 pElem = Merge(apSorter[i], pElem, pKeyInfo);
drha8b38d22001-11-01 14:41:34 +00004039 }
4040 p->pSort = pElem;
drh5e00f6c2001-09-13 13:46:56 +00004041 break;
4042}
4043
drha8b38d22001-11-01 14:41:34 +00004044/* Opcode: SortNext * P2 *
drh5e00f6c2001-09-13 13:46:56 +00004045**
drha8b38d22001-11-01 14:41:34 +00004046** Push the data for the topmost element in the sorter onto the
4047** stack, then remove the element from the sorter. If the sorter
4048** is empty, push nothing on the stack and instead jump immediately
4049** to instruction P2.
drh5e00f6c2001-09-13 13:46:56 +00004050*/
4051case OP_SortNext: {
drha8b38d22001-11-01 14:41:34 +00004052 Sorter *pSorter = p->pSort;
drhcaec2f12003-01-07 02:47:47 +00004053 CHECK_FOR_INTERRUPT;
drha8b38d22001-11-01 14:41:34 +00004054 if( pSorter!=0 ){
4055 p->pSort = pSorter->pNext;
drh6810ce62004-01-31 19:22:56 +00004056 pTos++;
4057 pTos->z = pSorter->pData;
4058 pTos->n = pSorter->nData;
drheb2e1762004-05-27 01:53:56 +00004059 pTos->flags = MEM_Blob|MEM_Dyn|MEM_Term;
4060 pTos->enc = 0;
4061 pTos->type = SQLITE3_BLOB;
drh5e00f6c2001-09-13 13:46:56 +00004062 sqliteFree(pSorter->zKey);
4063 sqliteFree(pSorter);
4064 }else{
4065 pc = pOp->p2 - 1;
4066 }
4067 break;
4068}
4069
drha8b38d22001-11-01 14:41:34 +00004070/* Opcode: SortReset * * *
drh5e00f6c2001-09-13 13:46:56 +00004071**
drha8b38d22001-11-01 14:41:34 +00004072** Remove any elements that remain on the sorter.
drh5e00f6c2001-09-13 13:46:56 +00004073*/
drha8b38d22001-11-01 14:41:34 +00004074case OP_SortReset: {
danielk19774adee202004-05-08 08:23:19 +00004075 sqlite3VdbeSorterReset(p);
drh5e00f6c2001-09-13 13:46:56 +00004076 break;
4077}
4078
drh8721ce42001-11-07 14:22:00 +00004079/* Opcode: MemStore P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00004080**
drh8721ce42001-11-07 14:22:00 +00004081** Write the top of the stack into memory location P1.
4082** P1 should be a small integer since space is allocated
drh5e00f6c2001-09-13 13:46:56 +00004083** for all memory locations between 0 and P1 inclusive.
drh8721ce42001-11-07 14:22:00 +00004084**
4085** After the data is stored in the memory location, the
4086** stack is popped once if P2 is 1. If P2 is zero, then
4087** the original data remains on the stack.
drh5e00f6c2001-09-13 13:46:56 +00004088*/
4089case OP_MemStore: {
4090 int i = pOp->p1;
drh9bbca4c2001-11-06 04:00:18 +00004091 Mem *pMem;
drh6810ce62004-01-31 19:22:56 +00004092 assert( pTos>=p->aStack );
drh5e00f6c2001-09-13 13:46:56 +00004093 if( i>=p->nMem ){
4094 int nOld = p->nMem;
drh6d4abfb2001-10-22 02:58:08 +00004095 Mem *aMem;
drh5e00f6c2001-09-13 13:46:56 +00004096 p->nMem = i + 5;
drh6d4abfb2001-10-22 02:58:08 +00004097 aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
4098 if( aMem==0 ) goto no_mem;
drh3e56c042002-09-17 03:20:46 +00004099 if( aMem!=p->aMem ){
4100 int j;
4101 for(j=0; j<nOld; j++){
drh6810ce62004-01-31 19:22:56 +00004102 if( aMem[j].flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004103 aMem[j].z = aMem[j].zShort;
drh3e56c042002-09-17 03:20:46 +00004104 }
4105 }
4106 }
drh6d4abfb2001-10-22 02:58:08 +00004107 p->aMem = aMem;
drh5e00f6c2001-09-13 13:46:56 +00004108 if( nOld<p->nMem ){
4109 memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
4110 }
4111 }
drh2c79c672004-01-31 20:20:29 +00004112 Deephemeralize(pTos);
drh5e00f6c2001-09-13 13:46:56 +00004113 pMem = &p->aMem[i];
drh2c79c672004-01-31 20:20:29 +00004114 Release(pMem);
drh6810ce62004-01-31 19:22:56 +00004115 *pMem = *pTos;
drh2c79c672004-01-31 20:20:29 +00004116 if( pMem->flags & MEM_Dyn ){
drh6810ce62004-01-31 19:22:56 +00004117 if( pOp->p2 ){
4118 pTos->flags = MEM_Null;
4119 }else{
drhf4479502004-05-27 03:12:53 +00004120 pMem->z = sqliteMallocRaw( pMem->n+2 );
drh6b563442001-11-07 16:48:26 +00004121 if( pMem->z==0 ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +00004122 memcpy(pMem->z, pTos->z, pMem->n);
drhf4479502004-05-27 03:12:53 +00004123 memcpy(&pMem->z[pMem->n], "\000", 2);
4124 pMem->flags |= MEM_Term;
drh8721ce42001-11-07 14:22:00 +00004125 }
drh2c79c672004-01-31 20:20:29 +00004126 }else if( pMem->flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004127 pMem->z = pMem->zShort;
drh5e00f6c2001-09-13 13:46:56 +00004128 }
drh8721ce42001-11-07 14:22:00 +00004129 if( pOp->p2 ){
drh6810ce62004-01-31 19:22:56 +00004130 Release(pTos);
4131 pTos--;
drh8721ce42001-11-07 14:22:00 +00004132 }
drh5e00f6c2001-09-13 13:46:56 +00004133 break;
4134}
4135
4136/* Opcode: MemLoad P1 * *
4137**
4138** Push a copy of the value in memory location P1 onto the stack.
drh8721ce42001-11-07 14:22:00 +00004139**
4140** If the value is a string, then the value pushed is a pointer to
4141** the string that is stored in the memory location. If the memory
4142** location is subsequently changed (using OP_MemStore) then the
4143** value pushed onto the stack will change too.
drh5e00f6c2001-09-13 13:46:56 +00004144*/
4145case OP_MemLoad: {
drh5e00f6c2001-09-13 13:46:56 +00004146 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004147 assert( i>=0 && i<p->nMem );
4148 pTos++;
4149 memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
danielk1977106bb232004-05-21 10:08:53 +00004150 if( pTos->flags & (MEM_Str|MEM_Blob) ){
drh6810ce62004-01-31 19:22:56 +00004151 pTos->flags |= MEM_Ephem;
4152 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
drh5e00f6c2001-09-13 13:46:56 +00004153 }
4154 break;
4155}
4156
drhd11d3822002-06-21 23:01:49 +00004157/* Opcode: MemIncr P1 P2 *
4158**
4159** Increment the integer valued memory cell P1 by 1. If P2 is not zero
4160** and the result after the increment is greater than zero, then jump
4161** to P2.
4162**
4163** This instruction throws an error if the memory cell is not initially
4164** an integer.
4165*/
4166case OP_MemIncr: {
4167 int i = pOp->p1;
4168 Mem *pMem;
drh6810ce62004-01-31 19:22:56 +00004169 assert( i>=0 && i<p->nMem );
drhd11d3822002-06-21 23:01:49 +00004170 pMem = &p->aMem[i];
drh6810ce62004-01-31 19:22:56 +00004171 assert( pMem->flags==MEM_Int );
drh00706be2004-01-30 14:49:16 +00004172 pMem->i++;
4173 if( pOp->p2>0 && pMem->i>0 ){
drhd11d3822002-06-21 23:01:49 +00004174 pc = pOp->p2 - 1;
4175 }
4176 break;
4177}
4178
drh5e00f6c2001-09-13 13:46:56 +00004179/* Opcode: AggReset * P2 *
4180**
4181** Reset the aggregator so that it no longer contains any data.
4182** Future aggregator elements will contain P2 values each.
4183*/
4184case OP_AggReset: {
danielk19774adee202004-05-08 08:23:19 +00004185 sqlite3VdbeAggReset(&p->agg);
drh5e00f6c2001-09-13 13:46:56 +00004186 p->agg.nMem = pOp->p2;
drh1350b032002-02-27 19:00:20 +00004187 p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
drhcaec2f12003-01-07 02:47:47 +00004188 if( p->agg.apFunc==0 ) goto no_mem;
drhe5095352002-02-24 03:25:14 +00004189 break;
4190}
4191
drh1350b032002-02-27 19:00:20 +00004192/* Opcode: AggInit * P2 P3
drhe5095352002-02-24 03:25:14 +00004193**
drh0bce8352002-02-28 00:41:10 +00004194** Initialize the function parameters for an aggregate function.
4195** The aggregate will operate out of aggregate column P2.
4196** P3 is a pointer to the FuncDef structure for the function.
drhe5095352002-02-24 03:25:14 +00004197*/
drh1350b032002-02-27 19:00:20 +00004198case OP_AggInit: {
drhe5095352002-02-24 03:25:14 +00004199 int i = pOp->p2;
drh6810ce62004-01-31 19:22:56 +00004200 assert( i>=0 && i<p->agg.nMem );
drh0bce8352002-02-28 00:41:10 +00004201 p->agg.apFunc[i] = (FuncDef*)pOp->p3;
drhe5095352002-02-24 03:25:14 +00004202 break;
4203}
4204
4205/* Opcode: AggFunc * P2 P3
4206**
drh0bce8352002-02-28 00:41:10 +00004207** Execute the step function for an aggregate. The
4208** function has P2 arguments. P3 is a pointer to the FuncDef
4209** structure that specifies the function.
drhe5095352002-02-24 03:25:14 +00004210**
drh1350b032002-02-27 19:00:20 +00004211** The top of the stack must be an integer which is the index of
4212** the aggregate column that corresponds to this aggregate function.
4213** Ideally, this index would be another parameter, but there are
4214** no free parameters left. The integer is popped from the stack.
drhe5095352002-02-24 03:25:14 +00004215*/
4216case OP_AggFunc: {
4217 int n = pOp->p2;
4218 int i;
drh6810ce62004-01-31 19:22:56 +00004219 Mem *pMem, *pRec;
danielk197722322fd2004-05-25 23:35:17 +00004220 sqlite3_context ctx;
danielk19776ddcca52004-05-24 23:48:25 +00004221 sqlite3_value **apVal;
drhe5095352002-02-24 03:25:14 +00004222
drh6810ce62004-01-31 19:22:56 +00004223 assert( n>=0 );
4224 assert( pTos->flags==MEM_Int );
4225 pRec = &pTos[-n];
4226 assert( pRec>=p->aStack );
danielk19776ddcca52004-05-24 23:48:25 +00004227
4228 apVal = p->apArg;
4229 assert( apVal || n==0 );
4230
drh6810ce62004-01-31 19:22:56 +00004231 for(i=0; i<n; i++, pRec++){
danielk1977c572ef72004-05-27 09:28:41 +00004232 apVal[i] = pRec;
4233 StoreTypeInfo(pRec, db->enc);
drhe5095352002-02-24 03:25:14 +00004234 }
drh6810ce62004-01-31 19:22:56 +00004235 i = pTos->i;
4236 assert( i>=0 && i<p->agg.nMem );
drh0bce8352002-02-28 00:41:10 +00004237 ctx.pFunc = (FuncDef*)pOp->p3;
drh1350b032002-02-27 19:00:20 +00004238 pMem = &p->agg.pCurrent->aMem[i];
drh00706be2004-01-30 14:49:16 +00004239 ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */
drh1350b032002-02-27 19:00:20 +00004240 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +00004241 ctx.cnt = ++pMem->i;
drh1350b032002-02-27 19:00:20 +00004242 ctx.isError = 0;
4243 ctx.isStep = 1;
danielk19776ddcca52004-05-24 23:48:25 +00004244 (ctx.pFunc->xStep)(&ctx, n, apVal);
drh1350b032002-02-27 19:00:20 +00004245 pMem->z = ctx.pAgg;
drh00706be2004-01-30 14:49:16 +00004246 pMem->flags = MEM_AggCtx;
drh6810ce62004-01-31 19:22:56 +00004247 popStack(&pTos, n+1);
drh1350b032002-02-27 19:00:20 +00004248 if( ctx.isError ){
4249 rc = SQLITE_ERROR;
4250 }
drh5e00f6c2001-09-13 13:46:56 +00004251 break;
4252}
4253
4254/* Opcode: AggFocus * P2 *
4255**
4256** Pop the top of the stack and use that as an aggregator key. If
4257** an aggregator with that same key already exists, then make the
4258** aggregator the current aggregator and jump to P2. If no aggregator
4259** with the given key exists, create one and make it current but
4260** do not jump.
4261**
4262** The order of aggregator opcodes is important. The order is:
4263** AggReset AggFocus AggNext. In other words, you must execute
4264** AggReset first, then zero or more AggFocus operations, then
4265** zero or more AggNext operations. You must not execute an AggFocus
4266** in between an AggNext and an AggReset.
4267*/
4268case OP_AggFocus: {
drh5e00f6c2001-09-13 13:46:56 +00004269 AggElem *pElem;
4270 char *zKey;
4271 int nKey;
4272
drh6810ce62004-01-31 19:22:56 +00004273 assert( pTos>=p->aStack );
danielk19778a6b5412004-05-24 07:04:25 +00004274 Stringify(pTos, db->enc);
drh6810ce62004-01-31 19:22:56 +00004275 zKey = pTos->z;
4276 nKey = pTos->n;
danielk19774adee202004-05-08 08:23:19 +00004277 pElem = sqlite3HashFind(&p->agg.hash, zKey, nKey);
drh5e00f6c2001-09-13 13:46:56 +00004278 if( pElem ){
4279 p->agg.pCurrent = pElem;
4280 pc = pOp->p2 - 1;
4281 }else{
drhdb5ed6d2001-09-18 22:17:44 +00004282 AggInsert(&p->agg, zKey, nKey);
danielk19776f8a5032004-05-10 10:34:51 +00004283 if( sqlite3_malloc_failed ) goto no_mem;
drh5e00f6c2001-09-13 13:46:56 +00004284 }
drh6810ce62004-01-31 19:22:56 +00004285 Release(pTos);
4286 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004287 break;
4288}
4289
drh5e00f6c2001-09-13 13:46:56 +00004290/* Opcode: AggSet * P2 *
4291**
4292** Move the top of the stack into the P2-th field of the current
4293** aggregate. String values are duplicated into new memory.
4294*/
4295case OP_AggSet: {
4296 AggElem *pFocus = AggInFocus(p->agg);
drh2c79c672004-01-31 20:20:29 +00004297 Mem *pMem;
drh5e00f6c2001-09-13 13:46:56 +00004298 int i = pOp->p2;
drh6810ce62004-01-31 19:22:56 +00004299 assert( pTos>=p->aStack );
drh5e00f6c2001-09-13 13:46:56 +00004300 if( pFocus==0 ) goto no_mem;
drh2c79c672004-01-31 20:20:29 +00004301 assert( i>=0 && i<p->agg.nMem );
4302 Deephemeralize(pTos);
4303 pMem = &pFocus->aMem[i];
4304 Release(pMem);
4305 *pMem = *pTos;
4306 if( pMem->flags & MEM_Dyn ){
4307 pTos->flags = MEM_Null;
4308 }else if( pMem->flags & MEM_Short ){
4309 pMem->z = pMem->zShort;
drh5e00f6c2001-09-13 13:46:56 +00004310 }
drh6810ce62004-01-31 19:22:56 +00004311 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004312 break;
4313}
4314
4315/* Opcode: AggGet * P2 *
4316**
4317** Push a new entry onto the stack which is a copy of the P2-th field
4318** of the current aggregate. Strings are not duplicated so
4319** string values will be ephemeral.
4320*/
4321case OP_AggGet: {
4322 AggElem *pFocus = AggInFocus(p->agg);
drh2c79c672004-01-31 20:20:29 +00004323 Mem *pMem;
drh5e00f6c2001-09-13 13:46:56 +00004324 int i = pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00004325 if( pFocus==0 ) goto no_mem;
drh2c79c672004-01-31 20:20:29 +00004326 assert( i>=0 && i<p->agg.nMem );
drh6810ce62004-01-31 19:22:56 +00004327 pTos++;
drh2c79c672004-01-31 20:20:29 +00004328 pMem = &pFocus->aMem[i];
4329 *pTos = *pMem;
danielk1977106bb232004-05-21 10:08:53 +00004330 if( pTos->flags & (MEM_Str|MEM_Blob) ){
drh3914aed2004-01-31 20:40:42 +00004331 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4332 pTos->flags |= MEM_Ephem;
4333 }
danielk19778a6b5412004-05-24 07:04:25 +00004334 if( pTos->flags&MEM_Str ){
drhf4479502004-05-27 03:12:53 +00004335 sqlite3VdbeChangeEncoding(pTos, db->enc);
danielk19778a6b5412004-05-24 07:04:25 +00004336 }
drh5e00f6c2001-09-13 13:46:56 +00004337 break;
4338}
4339
4340/* Opcode: AggNext * P2 *
4341**
4342** Make the next aggregate value the current aggregate. The prior
4343** aggregate is deleted. If all aggregate values have been consumed,
4344** jump to P2.
4345**
4346** The order of aggregator opcodes is important. The order is:
4347** AggReset AggFocus AggNext. In other words, you must execute
4348** AggReset first, then zero or more AggFocus operations, then
4349** zero or more AggNext operations. You must not execute an AggFocus
4350** in between an AggNext and an AggReset.
4351*/
4352case OP_AggNext: {
drhcaec2f12003-01-07 02:47:47 +00004353 CHECK_FOR_INTERRUPT;
drhbeae3192001-09-22 18:12:08 +00004354 if( p->agg.pSearch==0 ){
4355 p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
4356 }else{
4357 p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
drh5e00f6c2001-09-13 13:46:56 +00004358 }
drhbeae3192001-09-22 18:12:08 +00004359 if( p->agg.pSearch==0 ){
4360 pc = pOp->p2 - 1;
4361 } else {
drhe5095352002-02-24 03:25:14 +00004362 int i;
danielk197722322fd2004-05-25 23:35:17 +00004363 sqlite3_context ctx;
drhe5095352002-02-24 03:25:14 +00004364 Mem *aMem;
drhbeae3192001-09-22 18:12:08 +00004365 p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
drhe5095352002-02-24 03:25:14 +00004366 aMem = p->agg.pCurrent->aMem;
4367 for(i=0; i<p->agg.nMem; i++){
drh0bce8352002-02-28 00:41:10 +00004368 int freeCtx;
drh1350b032002-02-27 19:00:20 +00004369 if( p->agg.apFunc[i]==0 ) continue;
4370 if( p->agg.apFunc[i]->xFinalize==0 ) continue;
drh00706be2004-01-30 14:49:16 +00004371 ctx.s.flags = MEM_Null;
4372 ctx.s.z = aMem[i].zShort;
drh1350b032002-02-27 19:00:20 +00004373 ctx.pAgg = (void*)aMem[i].z;
drh00706be2004-01-30 14:49:16 +00004374 freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
4375 ctx.cnt = aMem[i].i;
drh1350b032002-02-27 19:00:20 +00004376 ctx.isStep = 0;
4377 ctx.pFunc = p->agg.apFunc[i];
4378 (*p->agg.apFunc[i]->xFinalize)(&ctx);
drh0bce8352002-02-28 00:41:10 +00004379 if( freeCtx ){
4380 sqliteFree( aMem[i].z );
4381 }
drh00706be2004-01-30 14:49:16 +00004382 aMem[i] = ctx.s;
drh6810ce62004-01-31 19:22:56 +00004383 if( aMem[i].flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004384 aMem[i].z = aMem[i].zShort;
drhe5095352002-02-24 03:25:14 +00004385 }
drhe5095352002-02-24 03:25:14 +00004386 }
drh5e00f6c2001-09-13 13:46:56 +00004387 }
4388 break;
4389}
4390
drh6f8c91c2003-12-07 00:24:35 +00004391/* Opcode: Vacuum * * *
4392**
4393** Vacuum the entire database. This opcode will cause other virtual
4394** machines to be created and run. It may not be called from within
4395** a transaction.
4396*/
4397case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004398 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4399 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4400 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004401 break;
4402}
4403
drh5e00f6c2001-09-13 13:46:56 +00004404/* An other opcode is illegal...
4405*/
4406default: {
danielk19776f8a5032004-05-10 10:34:51 +00004407 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
danielk19774adee202004-05-08 08:23:19 +00004408 sqlite3SetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
drh5e00f6c2001-09-13 13:46:56 +00004409 rc = SQLITE_INTERNAL;
4410 break;
4411}
4412
4413/*****************************************************************************
4414** The cases of the switch statement above this line should all be indented
4415** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4416** readability. From this point on down, the normal indentation rules are
4417** restored.
4418*****************************************************************************/
4419 }
drh6e142f52000-06-08 13:36:40 +00004420
drh7b396862003-01-01 23:06:20 +00004421#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00004422 {
4423 long long elapse = hwtime() - start;
4424 pOp->cycles += elapse;
4425 pOp->cnt++;
4426#if 0
4427 fprintf(stdout, "%10lld ", elapse);
danielk19774adee202004-05-08 08:23:19 +00004428 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00004429#endif
4430 }
drh7b396862003-01-01 23:06:20 +00004431#endif
4432
drh6e142f52000-06-08 13:36:40 +00004433 /* The following code adds nothing to the actual functionality
4434 ** of the program. It is only here for testing and debugging.
4435 ** On the other hand, it does burn CPU cycles every time through
4436 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4437 */
4438#ifndef NDEBUG
drh3914aed2004-01-31 20:40:42 +00004439 /* Sanity checking on the top element of the stack */
4440 if( pTos>=p->aStack ){
danielk1977c572ef72004-05-27 09:28:41 +00004441 sqlite3VdbeMemSanity(pTos, db->enc);
drh3914aed2004-01-31 20:40:42 +00004442 }
drh58b95762000-06-02 01:17:37 +00004443 if( pc<-1 || pc>=p->nOp ){
danielk19774adee202004-05-08 08:23:19 +00004444 sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0);
drh58b95762000-06-02 01:17:37 +00004445 rc = SQLITE_INTERNAL;
4446 }
drh6810ce62004-01-31 19:22:56 +00004447 if( p->trace && pTos>=p->aStack ){
drh75897232000-05-29 14:26:00 +00004448 int i;
4449 fprintf(p->trace, "Stack:");
drh6810ce62004-01-31 19:22:56 +00004450 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4451 if( pTos[i].flags & MEM_Null ){
drhc61053b2000-06-04 12:58:36 +00004452 fprintf(p->trace, " NULL");
drh6810ce62004-01-31 19:22:56 +00004453 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drha3b321d2004-05-11 09:31:31 +00004454 fprintf(p->trace, " si:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00004455 }else if( pTos[i].flags & MEM_Int ){
drha3b321d2004-05-11 09:31:31 +00004456 fprintf(p->trace, " i:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00004457 }else if( pTos[i].flags & MEM_Real ){
4458 fprintf(p->trace, " r:%g", pTos[i].r);
drh75897232000-05-29 14:26:00 +00004459 }else{
danielk1977ca6b2912004-05-21 10:49:47 +00004460 char zBuf[100];
drh79069752004-05-22 21:30:40 +00004461 prettyPrintMem(&pTos[i], zBuf, 100);
danielk1977ca6b2912004-05-21 10:49:47 +00004462 fprintf(p->trace, " ");
4463 fprintf(p->trace, zBuf);
drh75897232000-05-29 14:26:00 +00004464 }
4465 }
drh7bc09d32002-11-01 01:55:36 +00004466 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
drh75897232000-05-29 14:26:00 +00004467 fprintf(p->trace,"\n");
4468 }
drh6e142f52000-06-08 13:36:40 +00004469#endif
drhb86ccfb2003-01-28 23:13:10 +00004470 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00004471
drhb86ccfb2003-01-28 23:13:10 +00004472 /* If we reach this point, it means that execution is finished.
4473 */
4474vdbe_halt:
4475 if( rc ){
4476 p->rc = rc;
4477 rc = SQLITE_ERROR;
4478 }else{
4479 rc = SQLITE_DONE;
4480 }
4481 p->magic = VDBE_MAGIC_HALT;
drh6810ce62004-01-31 19:22:56 +00004482 p->pTos = pTos;
drhb86ccfb2003-01-28 23:13:10 +00004483 return rc;
4484
4485 /* Jump to here if a malloc() fails. It's hard to get a malloc()
4486 ** to fail on a modern VM computer, so this code is untested.
4487 */
4488no_mem:
danielk19774adee202004-05-08 08:23:19 +00004489 sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00004490 rc = SQLITE_NOMEM;
4491 goto vdbe_halt;
4492
4493 /* Jump to here for an SQLITE_MISUSE error.
4494 */
4495abort_due_to_misuse:
4496 rc = SQLITE_MISUSE;
4497 /* Fall thru into abort_due_to_error */
4498
4499 /* Jump to here for any other kind of fatal error. The "rc" variable
4500 ** should hold the error number.
4501 */
4502abort_due_to_error:
drh483750b2003-01-29 18:46:51 +00004503 if( p->zErrMsg==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00004504 if( sqlite3_malloc_failed ) rc = SQLITE_NOMEM;
4505 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh483750b2003-01-29 18:46:51 +00004506 }
drhb86ccfb2003-01-28 23:13:10 +00004507 goto vdbe_halt;
4508
danielk19776f8a5032004-05-10 10:34:51 +00004509 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00004510 ** flag.
4511 */
4512abort_due_to_interrupt:
4513 assert( db->flags & SQLITE_Interrupt );
4514 db->flags &= ~SQLITE_Interrupt;
4515 if( db->magic!=SQLITE_MAGIC_BUSY ){
4516 rc = SQLITE_MISUSE;
4517 }else{
4518 rc = SQLITE_INTERRUPT;
4519 }
danielk19776f8a5032004-05-10 10:34:51 +00004520 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00004521 goto vdbe_halt;
drhb86ccfb2003-01-28 23:13:10 +00004522}