blob: c4223c6f32eab330bfc9c95d1f731ddf757e7ed3 [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**
18** In the external interface, an "sqlite_vm*" is an opaque pointer
19** 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**
drhd7556d22004-05-14 21:59:40 +000046** $Id: vdbe.c,v 1.293 2004/05/14 21:59:40 drh 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
55** moves, either by the OP_MoveTo or the OP_Next opcode. The test
56** 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
72/*
drhb86ccfb2003-01-28 23:13:10 +000073** Advance the virtual machine to the next output row.
74**
drh3a840692003-01-29 22:58:26 +000075** The return vale will be either SQLITE_BUSY, SQLITE_DONE,
76** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
drhb86ccfb2003-01-28 23:13:10 +000077**
78** SQLITE_BUSY means that the virtual machine attempted to open
79** a locked database and there is no busy callback registered.
danielk19776f8a5032004-05-10 10:34:51 +000080** Call sqlite3_step() again to retry the open. *pN is set to 0
drhb86ccfb2003-01-28 23:13:10 +000081** and *pazColName and *pazValue are both set to NULL.
82**
83** SQLITE_DONE means that the virtual machine has finished
danielk19776f8a5032004-05-10 10:34:51 +000084** executing. sqlite3_step() should not be called again on this
drhb86ccfb2003-01-28 23:13:10 +000085** virtual machine. *pN and *pazColName are set appropriately
86** but *pazValue is set to NULL.
87**
88** SQLITE_ROW means that the virtual machine has generated another
89** row of the result set. *pN is set to the number of columns in
90** the row. *pazColName is set to the names of the columns followed
91** by the column datatypes. *pazValue is set to the values of each
92** column in the row. The value of the i-th column is (*pazValue)[i].
93** The name of the i-th column is (*pazColName)[i] and the datatype
94** of the i-th column is (*pazColName)[i+*pN].
95**
drh3a840692003-01-29 22:58:26 +000096** SQLITE_ERROR means that a run-time error (such as a constraint
97** violation) has occurred. The details of the error will be returned
danielk19776f8a5032004-05-10 10:34:51 +000098** by the next call to sqlite3_finalize(). sqlite3_step() should not
drh3a840692003-01-29 22:58:26 +000099** be called again on the VM.
100**
101** SQLITE_MISUSE means that the this routine was called inappropriately.
102** Perhaps it was called on a virtual machine that had already been
103** finalized or on one that had previously returned SQLITE_ERROR or
104** SQLITE_DONE. Or it could be the case the the same database connection
105** is being used simulataneously by two or more threads.
drhb86ccfb2003-01-28 23:13:10 +0000106*/
danielk19776f8a5032004-05-10 10:34:51 +0000107int sqlite3_step(
drhb86ccfb2003-01-28 23:13:10 +0000108 sqlite_vm *pVm, /* The virtual machine to execute */
109 int *pN, /* OUT: Number of columns in result */
110 const char ***pazValue, /* OUT: Column data */
111 const char ***pazColName /* OUT: Column names and datatypes */
112){
113 Vdbe *p = (Vdbe*)pVm;
drh326dce72003-01-29 14:06:07 +0000114 sqlite *db;
drhb86ccfb2003-01-28 23:13:10 +0000115 int rc;
116
117 if( p->magic!=VDBE_MAGIC_RUN ){
118 return SQLITE_MISUSE;
119 }
drh326dce72003-01-29 14:06:07 +0000120 db = p->db;
danielk19774adee202004-05-08 08:23:19 +0000121 if( sqlite3SafetyOn(db) ){
drha1f9b5e2004-02-14 16:31:02 +0000122 p->rc = SQLITE_MISUSE;
drh326dce72003-01-29 14:06:07 +0000123 return SQLITE_MISUSE;
124 }
drhb86ccfb2003-01-28 23:13:10 +0000125 if( p->explain ){
danielk19774adee202004-05-08 08:23:19 +0000126 rc = sqlite3VdbeList(p);
drhb86ccfb2003-01-28 23:13:10 +0000127 }else{
danielk19774adee202004-05-08 08:23:19 +0000128 rc = sqlite3VdbeExec(p);
drhb86ccfb2003-01-28 23:13:10 +0000129 }
drh326dce72003-01-29 14:06:07 +0000130 if( rc==SQLITE_DONE || rc==SQLITE_ROW ){
drh073e5a72003-07-09 00:28:13 +0000131 if( pazColName ) *pazColName = (const char**)p->azColName;
132 if( pN ) *pN = p->nResColumn;
drhb86ccfb2003-01-28 23:13:10 +0000133 }else{
drh073e5a72003-07-09 00:28:13 +0000134 if( pazColName) *pazColName = 0;
135 if( pN ) *pN = 0;
drhb86ccfb2003-01-28 23:13:10 +0000136 }
drh073e5a72003-07-09 00:28:13 +0000137 if( pazValue ){
138 if( rc==SQLITE_ROW ){
139 *pazValue = (const char**)p->azResColumn;
140 }else{
141 *pazValue = 0;
142 }
drhb86ccfb2003-01-28 23:13:10 +0000143 }
danielk19774adee202004-05-08 08:23:19 +0000144 if( sqlite3SafetyOff(db) ){
drh326dce72003-01-29 14:06:07 +0000145 return SQLITE_MISUSE;
146 }
drhb86ccfb2003-01-28 23:13:10 +0000147 return rc;
148}
149
150/*
drhac82fcf2002-09-08 17:23:41 +0000151** Insert a new aggregate element and make it the element that
152** has focus.
drh600b1b22000-06-05 21:39:48 +0000153**
154** Return 0 on success and 1 if memory is exhausted.
155*/
drhdb5ed6d2001-09-18 22:17:44 +0000156static int AggInsert(Agg *p, char *zKey, int nKey){
drh6d4abfb2001-10-22 02:58:08 +0000157 AggElem *pElem, *pOld;
drh22827922000-06-06 17:27:05 +0000158 int i;
drh2c79c672004-01-31 20:20:29 +0000159 Mem *pMem;
drhdb5ed6d2001-09-18 22:17:44 +0000160 pElem = sqliteMalloc( sizeof(AggElem) + nKey +
drh600b1b22000-06-05 21:39:48 +0000161 (p->nMem-1)*sizeof(pElem->aMem[0]) );
162 if( pElem==0 ) return 1;
163 pElem->zKey = (char*)&pElem->aMem[p->nMem];
drhdb5ed6d2001-09-18 22:17:44 +0000164 memcpy(pElem->zKey, zKey, nKey);
165 pElem->nKey = nKey;
danielk19774adee202004-05-08 08:23:19 +0000166 pOld = sqlite3HashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
drh6d4abfb2001-10-22 02:58:08 +0000167 if( pOld!=0 ){
168 assert( pOld==pElem ); /* Malloc failed on insert */
169 sqliteFree(pOld);
170 return 0;
171 }
drh2c79c672004-01-31 20:20:29 +0000172 for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
173 pMem->flags = MEM_Null;
drh22827922000-06-06 17:27:05 +0000174 }
drhbeae3192001-09-22 18:12:08 +0000175 p->pCurrent = pElem;
drh600b1b22000-06-05 21:39:48 +0000176 return 0;
177}
178
179/*
180** Get the AggElem currently in focus
181*/
182#define AggInFocus(P) ((P).pCurrent ? (P).pCurrent : _AggInFocus(&(P)))
183static AggElem *_AggInFocus(Agg *p){
drhbeae3192001-09-22 18:12:08 +0000184 HashElem *pElem = sqliteHashFirst(&p->hash);
drhdaffd0e2001-04-11 14:28:42 +0000185 if( pElem==0 ){
drhbeae3192001-09-22 18:12:08 +0000186 AggInsert(p,"",1);
187 pElem = sqliteHashFirst(&p->hash);
drhdaffd0e2001-04-11 14:28:42 +0000188 }
drhbeae3192001-09-22 18:12:08 +0000189 return pElem ? sqliteHashData(pElem) : 0;
drhfef52082000-06-06 01:50:43 +0000190}
191
192/*
drhc61053b2000-06-04 12:58:36 +0000193** Convert the given stack entity into a string if it isn't one
drh371ac442003-01-07 13:43:45 +0000194** already.
drhc61053b2000-06-04 12:58:36 +0000195*/
drh6810ce62004-01-31 19:22:56 +0000196#define Stringify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);}
197static int hardStringify(Mem *pStack){
drhefa4e172000-10-19 14:42:04 +0000198 int fg = pStack->flags;
drh00706be2004-01-30 14:49:16 +0000199 if( fg & MEM_Real ){
danielk19776f8a5032004-05-10 10:34:51 +0000200 sqlite3_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r);
drh00706be2004-01-30 14:49:16 +0000201 }else if( fg & MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +0000202 sqlite3_snprintf(sizeof(pStack->zShort),pStack->zShort,"%lld",pStack->i);
drhc61053b2000-06-04 12:58:36 +0000203 }else{
drh00706be2004-01-30 14:49:16 +0000204 pStack->zShort[0] = 0;
drhc61053b2000-06-04 12:58:36 +0000205 }
drh6810ce62004-01-31 19:22:56 +0000206 pStack->z = pStack->zShort;
drh00706be2004-01-30 14:49:16 +0000207 pStack->n = strlen(pStack->zShort)+1;
drh6810ce62004-01-31 19:22:56 +0000208 pStack->flags = MEM_Str | MEM_Short;
drhc61053b2000-06-04 12:58:36 +0000209 return 0;
210}
211
212/*
drh371ac442003-01-07 13:43:45 +0000213** Convert the given stack entity into a string that has been obtained
214** from sqliteMalloc(). This is different from Stringify() above in that
215** Stringify() will use the NBFS bytes of static string space if the string
216** will fit but this routine always mallocs for space.
217** Return non-zero if we run out of memory.
218*/
drh6810ce62004-01-31 19:22:56 +0000219#define Dynamicify(P) (((P)->flags & MEM_Dyn)==0 ? hardDynamicify(P):0)
220static int hardDynamicify(Mem *pStack){
drh371ac442003-01-07 13:43:45 +0000221 int fg = pStack->flags;
222 char *z;
drh00706be2004-01-30 14:49:16 +0000223 if( (fg & MEM_Str)==0 ){
drh6810ce62004-01-31 19:22:56 +0000224 hardStringify(pStack);
drh371ac442003-01-07 13:43:45 +0000225 }
drh00706be2004-01-30 14:49:16 +0000226 assert( (fg & MEM_Dyn)==0 );
drh371ac442003-01-07 13:43:45 +0000227 z = sqliteMallocRaw( pStack->n );
228 if( z==0 ) return 1;
drh6810ce62004-01-31 19:22:56 +0000229 memcpy(z, pStack->z, pStack->n);
230 pStack->z = z;
drh00706be2004-01-30 14:49:16 +0000231 pStack->flags |= MEM_Dyn;
drh371ac442003-01-07 13:43:45 +0000232 return 0;
233}
234
235/*
drh00706be2004-01-30 14:49:16 +0000236** An ephemeral string value (signified by the MEM_Ephem flag) contains
drhe958bb42002-10-22 15:04:34 +0000237** a pointer to a dynamically allocated string where some other entity
238** is responsible for deallocating that string. Because the stack entry
239** does not control the string, it might be deleted without the stack
240** entry knowing it.
241**
242** This routine converts an ephemeral string into a dynamically allocated
243** string that the stack entry itself controls. In other words, it
drh00706be2004-01-30 14:49:16 +0000244** converts an MEM_Ephem string into an MEM_Dyn string.
drhe958bb42002-10-22 15:04:34 +0000245*/
drh6810ce62004-01-31 19:22:56 +0000246#define Deephemeralize(P) \
247 if( ((P)->flags&MEM_Ephem)!=0 && hardDeephem(P) ){ goto no_mem;}
248static int hardDeephem(Mem *pStack){
drhe958bb42002-10-22 15:04:34 +0000249 char *z;
drh00706be2004-01-30 14:49:16 +0000250 assert( (pStack->flags & MEM_Ephem)!=0 );
drh8c1238a2003-01-02 14:43:55 +0000251 z = sqliteMallocRaw( pStack->n );
drhe958bb42002-10-22 15:04:34 +0000252 if( z==0 ) return 1;
drh00706be2004-01-30 14:49:16 +0000253 memcpy(z, pStack->z, pStack->n);
254 pStack->z = z;
255 pStack->flags &= ~MEM_Ephem;
256 pStack->flags |= MEM_Dyn;
drhe958bb42002-10-22 15:04:34 +0000257 return 0;
258}
259
260/*
drh6810ce62004-01-31 19:22:56 +0000261** Release the memory associated with the given stack level. This
262** leaves the Mem.flags field in an inconsistent state.
drhc61053b2000-06-04 12:58:36 +0000263*/
drh6810ce62004-01-31 19:22:56 +0000264#define Release(P) if((P)->flags&MEM_Dyn){ sqliteFree((P)->z); }
265
266/*
267** Pop the stack N times.
268*/
269static void popStack(Mem **ppTos, int N){
270 Mem *pTos = *ppTos;
271 while( N>0 ){
272 N--;
273 Release(pTos);
274 pTos--;
275 }
276 *ppTos = pTos;
drhc61053b2000-06-04 12:58:36 +0000277}
278
279/*
drhdc2d94d2003-07-27 17:16:06 +0000280** Return TRUE if zNum is a 32-bit signed integer and write
281** the value of the integer into *pNum. If zNum is not an integer
282** or is an integer that is too large to be expressed with just 32
283** bits, then return false.
drh32c05e92002-11-11 00:05:42 +0000284**
285** Under Linux (RedHat 7.2) this routine is much faster than atoi()
286** for converting strings into integers.
287*/
danielk19773d1bfea2004-05-14 11:00:53 +0000288static int toInt(const char *zNum, i64 *pNum){
289 i64 v = 0;
drh32c05e92002-11-11 00:05:42 +0000290 int neg;
drhdc2d94d2003-07-27 17:16:06 +0000291 int i, c;
drh32c05e92002-11-11 00:05:42 +0000292 if( *zNum=='-' ){
293 neg = 1;
294 zNum++;
295 }else if( *zNum=='+' ){
296 neg = 0;
297 zNum++;
298 }else{
299 neg = 0;
300 }
drhdc2d94d2003-07-27 17:16:06 +0000301 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
302 v = v*10 + c - '0';
drh32c05e92002-11-11 00:05:42 +0000303 }
304 *pNum = neg ? -v : v;
danielk19773d1bfea2004-05-14 11:00:53 +0000305 return c==0 && i>0 &&
306 (i<10 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
drh32c05e92002-11-11 00:05:42 +0000307}
308
309/*
drhc61053b2000-06-04 12:58:36 +0000310** Convert the given stack entity into a integer if it isn't one
311** already.
312**
313** Any prior string or real representation is invalidated.
314** NULLs are converted into 0.
315*/
drh6810ce62004-01-31 19:22:56 +0000316#define Integerify(P) if(((P)->flags&MEM_Int)==0){ hardIntegerify(P); }
317static void hardIntegerify(Mem *pStack){
318 if( pStack->flags & MEM_Real ){
319 pStack->i = (int)pStack->r;
320 Release(pStack);
321 }else if( pStack->flags & MEM_Str ){
322 toInt(pStack->z, &pStack->i);
323 Release(pStack);
drhc61053b2000-06-04 12:58:36 +0000324 }else{
drh6810ce62004-01-31 19:22:56 +0000325 pStack->i = 0;
drhc61053b2000-06-04 12:58:36 +0000326 }
drh6810ce62004-01-31 19:22:56 +0000327 pStack->flags = MEM_Int;
drhc61053b2000-06-04 12:58:36 +0000328}
329
330/*
331** Get a valid Real representation for the given stack element.
332**
333** Any prior string or integer representation is retained.
334** NULLs are converted into 0.0.
335*/
drh6810ce62004-01-31 19:22:56 +0000336#define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); }
337static void hardRealify(Mem *pStack){
338 if( pStack->flags & MEM_Str ){
danielk19774adee202004-05-08 08:23:19 +0000339 pStack->r = sqlite3AtoF(pStack->z, 0);
drh6810ce62004-01-31 19:22:56 +0000340 }else if( pStack->flags & MEM_Int ){
341 pStack->r = pStack->i;
drhc61053b2000-06-04 12:58:36 +0000342 }else{
drh6810ce62004-01-31 19:22:56 +0000343 pStack->r = 0.0;
drhc61053b2000-06-04 12:58:36 +0000344 }
drh6810ce62004-01-31 19:22:56 +0000345 pStack->flags |= MEM_Real;
drhc61053b2000-06-04 12:58:36 +0000346}
347
348/*
drh75897232000-05-29 14:26:00 +0000349** The parameters are pointers to the head of two sorted lists
350** of Sorter structures. Merge these two lists together and return
351** a single sorted list. This routine forms the core of the merge-sort
352** algorithm.
353**
354** In the case of a tie, left sorts in front of right.
355*/
356static Sorter *Merge(Sorter *pLeft, Sorter *pRight){
357 Sorter sHead;
358 Sorter *pTail;
359 pTail = &sHead;
360 pTail->pNext = 0;
361 while( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000362 int c = sqlite3SortCompare(pLeft->zKey, pRight->zKey);
drh75897232000-05-29 14:26:00 +0000363 if( c<=0 ){
364 pTail->pNext = pLeft;
365 pLeft = pLeft->pNext;
366 }else{
367 pTail->pNext = pRight;
368 pRight = pRight->pNext;
369 }
370 pTail = pTail->pNext;
371 }
372 if( pLeft ){
373 pTail->pNext = pLeft;
374 }else if( pRight ){
375 pTail->pNext = pRight;
376 }
377 return sHead.pNext;
378}
379
drh75897232000-05-29 14:26:00 +0000380/*
drh62160e72002-07-30 17:20:40 +0000381** The following routine works like a replacement for the standard
382** library routine fgets(). The difference is in how end-of-line (EOL)
383** is handled. Standard fgets() uses LF for EOL under unix, CRLF
384** under windows, and CR under mac. This routine accepts any of these
385** character sequences as an EOL mark. The EOL mark is replaced by
386** a single LF character in zBuf.
387*/
388static char *vdbe_fgets(char *zBuf, int nBuf, FILE *in){
389 int i, c;
390 for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){
391 zBuf[i] = c;
392 if( c=='\r' || c=='\n' ){
393 if( c=='\r' ){
394 zBuf[i] = '\n';
395 c = getc(in);
396 if( c!=EOF && c!='\n' ) ungetc(c, in);
397 }
398 i++;
399 break;
400 }
401 }
402 zBuf[i] = 0;
403 return i>0 ? zBuf : 0;
404}
405
406/*
drh8c74a8c2002-08-25 19:20:40 +0000407** Make sure there is space in the Vdbe structure to hold at least
408** mxCursor cursors. If there is not currently enough space, then
409** allocate more.
410**
411** If a memory allocation error occurs, return 1. Return 0 if
412** everything works.
413*/
414static int expandCursorArraySize(Vdbe *p, int mxCursor){
415 if( mxCursor>=p->nCursor ){
drhd7556d22004-05-14 21:59:40 +0000416 p->apCsr = sqliteRealloc( p->apCsr, (mxCursor+1)*sizeof(Cursor*) );
417 if( p->apCsr==0 ) return 1;
418 while( p->nCursor<=mxCursor ){
419 Cursor *pC;
420 p->apCsr[p->nCursor++] = pC = sqliteMalloc( sizeof(Cursor) );
421 if( pC==0 ) return 1;
422 }
drh8c74a8c2002-08-25 19:20:40 +0000423 }
424 return 0;
425}
426
danielk19773d1bfea2004-05-14 11:00:53 +0000427/*
428** Apply any conversion required by the supplied column affinity to
429** memory cell pRec. affinity may be one of:
430**
431** SQLITE_AFF_NUM
432** SQLITE_AFF_TEXT
433** SQLITE_AFF_NONE
434** SQLITE_AFF_INTEGER
435**
436*/
437static void applyAffinity(Mem *pRec, int affinity){
438 switch( affinity ){
439 case SQLITE_SO_NUM:
440 if( 0==(pRec->flags&(MEM_Real|MEM_Int)) ){
441 /* pRec does not have a valid integer or real representation.
442 ** Attempt a conversion if pRec has a string representation and
443 ** it looks like a number.
444 */
445 int realnum;
446 if( pRec->flags&MEM_Str && sqlite3IsNumber(pRec->z, &realnum) ){
447 if( realnum ){
448 Realify(pRec);
449 }else{
450 Integerify(pRec);
451 }
452 }
453 }
454 break;
455 case SQLITE_SO_TEXT:
456 /* Only attempt the conversion if there is an integer or real
457 ** representation (blob and NULL do not get converted) but no string
458 ** representation.
459 */
460 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
461 Stringify(pRec);
462 }
463 pRec->flags &= ~(MEM_Real|MEM_Int);
464
465 break;
466
467/*
468 case SQLITE_AFF_INTEGER:
469 case SQLITE_AFF_NONE:
470 break;
471*/
472 default:
473 assert(0);
474 }
475}
476
477/*
478** This function interprets the character 'affinity' according to the
479** following table and calls the applyAffinity() function.
480*/
481static void applyAffinityByChar(Mem *pRec, char affinity){
482 switch( affinity ){
483 case 'n': return applyAffinity(pRec, SQLITE_SO_NUM);
484 case 't': return applyAffinity(pRec, SQLITE_SO_TEXT);
485 default: assert(0);
486 }
487}
488
drh7b396862003-01-01 23:06:20 +0000489#ifdef VDBE_PROFILE
490/*
491** The following routine only works on pentium-class processors.
492** It uses the RDTSC opcode to read cycle count value out of the
493** processor and returns that value. This can be used for high-res
494** profiling.
495*/
496__inline__ unsigned long long int hwtime(void){
497 unsigned long long int x;
498 __asm__("rdtsc\n\t"
499 "mov %%edx, %%ecx\n\t"
500 :"=A" (x));
501 return x;
502}
503#endif
504
drh8c74a8c2002-08-25 19:20:40 +0000505/*
drhcaec2f12003-01-07 02:47:47 +0000506** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
danielk19776f8a5032004-05-10 10:34:51 +0000507** sqlite3_interrupt() routine has been called. If it has been, then
drhcaec2f12003-01-07 02:47:47 +0000508** processing of the VDBE program is interrupted.
509**
510** This macro added to every instruction that does a jump in order to
511** implement a loop. This test used to be on every single instruction,
512** but that meant we more testing that we needed. By only testing the
513** flag on jump instructions, we get a (small) speed improvement.
514*/
515#define CHECK_FOR_INTERRUPT \
516 if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
517
518
519/*
drhb86ccfb2003-01-28 23:13:10 +0000520** Execute as much of a VDBE program as we can then return.
521**
danielk19774adee202004-05-08 08:23:19 +0000522** sqlite3VdbeMakeReady() must be called before this routine in order to
drhb86ccfb2003-01-28 23:13:10 +0000523** close the program with a final OP_Halt and to set up the callbacks
524** and the error message pointer.
525**
526** Whenever a row or result data is available, this routine will either
527** invoke the result callback (if there is one) or return with
drh326dce72003-01-29 14:06:07 +0000528** SQLITE_ROW.
drhb86ccfb2003-01-28 23:13:10 +0000529**
530** If an attempt is made to open a locked database, then this routine
531** will either invoke the busy callback (if there is one) or it will
532** return SQLITE_BUSY.
533**
534** If an error occurs, an error message is written to memory obtained
535** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
536** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
537**
538** If the callback ever returns non-zero, then the program exits
539** immediately. There will be no error message but the p->rc field is
540** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
541**
drh9468c7f2003-03-07 19:50:07 +0000542** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
543** routine to return SQLITE_ERROR.
drhb86ccfb2003-01-28 23:13:10 +0000544**
545** Other fatal errors return SQLITE_ERROR.
546**
danielk19774adee202004-05-08 08:23:19 +0000547** After this routine has finished, sqlite3VdbeFinalize() should be
drhb86ccfb2003-01-28 23:13:10 +0000548** used to clean up the mess that was left behind.
549*/
danielk19774adee202004-05-08 08:23:19 +0000550int sqlite3VdbeExec(
drhb86ccfb2003-01-28 23:13:10 +0000551 Vdbe *p /* The VDBE */
552){
553 int pc; /* The program counter */
554 Op *pOp; /* Current operation */
555 int rc = SQLITE_OK; /* Value to return */
drhb86ccfb2003-01-28 23:13:10 +0000556 sqlite *db = p->db; /* The database */
drh6810ce62004-01-31 19:22:56 +0000557 Mem *pTos; /* Top entry in the operand stack */
drhb86ccfb2003-01-28 23:13:10 +0000558 char zBuf[100]; /* Space to sprintf() an integer */
559#ifdef VDBE_PROFILE
560 unsigned long long start; /* CPU clock count at start of opcode */
561 int origPc; /* Program counter at start of opcode */
562#endif
danielk1977348bb5d2003-10-18 09:37:26 +0000563#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
564 int nProgressOps = 0; /* Opcodes executed since progress callback. */
565#endif
drhb86ccfb2003-01-28 23:13:10 +0000566
567 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
568 assert( db->magic==SQLITE_MAGIC_BUSY );
drh3a840692003-01-29 22:58:26 +0000569 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
570 p->rc = SQLITE_OK;
drhb86ccfb2003-01-28 23:13:10 +0000571 assert( p->explain==0 );
danielk19776f8a5032004-05-10 10:34:51 +0000572 if( sqlite3_malloc_failed ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +0000573 pTos = p->pTos;
drhb86ccfb2003-01-28 23:13:10 +0000574 if( p->popStack ){
drh6810ce62004-01-31 19:22:56 +0000575 popStack(&pTos, p->popStack);
drhb86ccfb2003-01-28 23:13:10 +0000576 p->popStack = 0;
577 }
drh93581642004-02-12 13:02:55 +0000578 CHECK_FOR_INTERRUPT;
drhb86ccfb2003-01-28 23:13:10 +0000579 for(pc=p->pc; rc==SQLITE_OK; pc++){
drhcaec2f12003-01-07 02:47:47 +0000580 assert( pc>=0 && pc<p->nOp );
drh6810ce62004-01-31 19:22:56 +0000581 assert( pTos<=&p->aStack[pc] );
drh7b396862003-01-01 23:06:20 +0000582#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +0000583 origPc = pc;
drh7b396862003-01-01 23:06:20 +0000584 start = hwtime();
585#endif
drh75897232000-05-29 14:26:00 +0000586 pOp = &p->aOp[pc];
drh6e142f52000-06-08 13:36:40 +0000587
588 /* Only allow tracing if NDEBUG is not defined.
589 */
590#ifndef NDEBUG
drh75897232000-05-29 14:26:00 +0000591 if( p->trace ){
danielk19774adee202004-05-08 08:23:19 +0000592 sqlite3VdbePrintOp(p->trace, pc, pOp);
drh75897232000-05-29 14:26:00 +0000593 }
drh6e142f52000-06-08 13:36:40 +0000594#endif
595
drhf6038712004-02-08 18:07:34 +0000596 /* Check to see if we need to simulate an interrupt. This only happens
597 ** if we have a special test build.
598 */
599#ifdef SQLITE_TEST
danielk19776f8a5032004-05-10 10:34:51 +0000600 if( sqlite3_interrupt_count>0 ){
601 sqlite3_interrupt_count--;
602 if( sqlite3_interrupt_count==0 ){
603 sqlite3_interrupt(db);
drhf6038712004-02-08 18:07:34 +0000604 }
605 }
606#endif
607
danielk1977348bb5d2003-10-18 09:37:26 +0000608#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
609 /* Call the progress callback if it is configured and the required number
610 ** of VDBE ops have been executed (either since this invocation of
danielk19774adee202004-05-08 08:23:19 +0000611 ** sqlite3VdbeExec() or since last time the progress callback was called).
danielk1977348bb5d2003-10-18 09:37:26 +0000612 ** If the progress callback returns non-zero, exit the virtual machine with
613 ** a return code SQLITE_ABORT.
614 */
drh3914aed2004-01-31 20:40:42 +0000615 if( db->xProgress ){
616 if( db->nProgressOps==nProgressOps ){
617 if( db->xProgress(db->pProgressArg)!=0 ){
618 rc = SQLITE_ABORT;
619 continue; /* skip to the next iteration of the for loop */
620 }
621 nProgressOps = 0;
danielk1977348bb5d2003-10-18 09:37:26 +0000622 }
drh3914aed2004-01-31 20:40:42 +0000623 nProgressOps++;
danielk1977348bb5d2003-10-18 09:37:26 +0000624 }
danielk1977348bb5d2003-10-18 09:37:26 +0000625#endif
626
drh75897232000-05-29 14:26:00 +0000627 switch( pOp->opcode ){
drh75897232000-05-29 14:26:00 +0000628
drh5e00f6c2001-09-13 13:46:56 +0000629/*****************************************************************************
630** What follows is a massive switch statement where each case implements a
631** separate instruction in the virtual machine. If we follow the usual
632** indentation conventions, each case should be indented by 6 spaces. But
633** that is a lot of wasted space on the left margin. So the code within
634** the switch statement will break with convention and be flush-left. Another
635** big comment (similar to this one) will mark the point in the code where
636** we transition back to normal indentation.
drhac82fcf2002-09-08 17:23:41 +0000637**
638** The formatting of each case is important. The makefile for SQLite
639** generates two C files "opcodes.h" and "opcodes.c" by scanning this
640** file looking for lines that begin with "case OP_". The opcodes.h files
641** will be filled with #defines that give unique integer values to each
642** opcode and the opcodes.c file is filled with an array of strings where
643** each string is the symbolic name for the corresponding opcode.
644**
645** Documentation about VDBE opcodes is generated by scanning this file
646** for lines of that contain "Opcode:". That line and all subsequent
647** comment lines are used in the generation of the opcode.html documentation
648** file.
649**
650** SUMMARY:
651**
652** Formatting is important to scripts that scan this file.
653** Do not deviate from the formatting style currently in use.
654**
drh5e00f6c2001-09-13 13:46:56 +0000655*****************************************************************************/
drh75897232000-05-29 14:26:00 +0000656
drh58a11682001-11-10 13:51:08 +0000657/* Opcode: Goto * P2 *
drh5e00f6c2001-09-13 13:46:56 +0000658**
659** An unconditional jump to address P2.
660** The next instruction executed will be
661** the one at index P2 from the beginning of
662** the program.
663*/
664case OP_Goto: {
drhcaec2f12003-01-07 02:47:47 +0000665 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +0000666 pc = pOp->p2 - 1;
667 break;
668}
drh75897232000-05-29 14:26:00 +0000669
drh8c74a8c2002-08-25 19:20:40 +0000670/* Opcode: Gosub * P2 *
671**
672** Push the current address plus 1 onto the return address stack
673** and then jump to address P2.
674**
675** The return address stack is of limited depth. If too many
676** OP_Gosub operations occur without intervening OP_Returns, then
677** the return address stack will fill up and processing will abort
678** with a fatal error.
679*/
680case OP_Gosub: {
drhb86ccfb2003-01-28 23:13:10 +0000681 if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
danielk19774adee202004-05-08 08:23:19 +0000682 sqlite3SetString(&p->zErrMsg, "return address stack overflow", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000683 p->rc = SQLITE_INTERNAL;
684 return SQLITE_ERROR;
drh8c74a8c2002-08-25 19:20:40 +0000685 }
drhb86ccfb2003-01-28 23:13:10 +0000686 p->returnStack[p->returnDepth++] = pc+1;
drh8c74a8c2002-08-25 19:20:40 +0000687 pc = pOp->p2 - 1;
688 break;
689}
690
691/* Opcode: Return * * *
692**
693** Jump immediately to the next instruction after the last unreturned
694** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
695** processing aborts with a fatal error.
696*/
697case OP_Return: {
drhb86ccfb2003-01-28 23:13:10 +0000698 if( p->returnDepth<=0 ){
danielk19774adee202004-05-08 08:23:19 +0000699 sqlite3SetString(&p->zErrMsg, "return address stack underflow", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +0000700 p->rc = SQLITE_INTERNAL;
701 return SQLITE_ERROR;
drh8c74a8c2002-08-25 19:20:40 +0000702 }
drhb86ccfb2003-01-28 23:13:10 +0000703 p->returnDepth--;
704 pc = p->returnStack[p->returnDepth] - 1;
drh8c74a8c2002-08-25 19:20:40 +0000705 break;
706}
707
drh1c928532002-01-31 15:54:21 +0000708/* Opcode: Halt P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000709**
drhb19a2bc2001-09-16 00:13:26 +0000710** Exit immediately. All open cursors, Lists, Sorts, etc are closed
drh5e00f6c2001-09-13 13:46:56 +0000711** automatically.
drhb19a2bc2001-09-16 00:13:26 +0000712**
danielk19776f8a5032004-05-10 10:34:51 +0000713** P1 is the result code returned by sqlite3_exec(). For a normal
drh9cfcf5d2002-01-29 18:41:24 +0000714** halt, this should be SQLITE_OK (0). For errors, it can be some
drh1c928532002-01-31 15:54:21 +0000715** other value. If P1!=0 then P2 will determine whether or not to
716** rollback the current transaction. Do not rollback if P2==OE_Fail.
717** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back
718** out all changes that have occurred during this execution of the
drhb798fa62002-09-03 19:43:23 +0000719** VDBE, but do not rollback the transaction.
drh9cfcf5d2002-01-29 18:41:24 +0000720**
721** There is an implied "Halt 0 0 0" instruction inserted at the very end of
drhb19a2bc2001-09-16 00:13:26 +0000722** every program. So a jump past the last instruction of the program
723** is the same as executing Halt.
drh5e00f6c2001-09-13 13:46:56 +0000724*/
725case OP_Halt: {
drh483750b2003-01-29 18:46:51 +0000726 p->magic = VDBE_MAGIC_HALT;
drh6810ce62004-01-31 19:22:56 +0000727 p->pTos = pTos;
drh9cfcf5d2002-01-29 18:41:24 +0000728 if( pOp->p1!=SQLITE_OK ){
drhb86ccfb2003-01-28 23:13:10 +0000729 p->rc = pOp->p1;
730 p->errorAction = pOp->p2;
danielk19776f349032002-06-11 02:25:40 +0000731 if( pOp->p3 ){
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
danielk19776f349032002-06-11 02:25:40 +0000733 }
drh483750b2003-01-29 18:46:51 +0000734 return SQLITE_ERROR;
drh9cfcf5d2002-01-29 18:41:24 +0000735 }else{
drhb86ccfb2003-01-28 23:13:10 +0000736 p->rc = SQLITE_OK;
drh483750b2003-01-29 18:46:51 +0000737 return SQLITE_DONE;
drh9cfcf5d2002-01-29 18:41:24 +0000738 }
drh5e00f6c2001-09-13 13:46:56 +0000739}
drhc61053b2000-06-04 12:58:36 +0000740
drhe6840902002-03-06 03:08:25 +0000741/* Opcode: Integer P1 * P3
drh5e00f6c2001-09-13 13:46:56 +0000742**
drhe6840902002-03-06 03:08:25 +0000743** The integer value P1 is pushed onto the stack. If P3 is not zero
744** then it is assumed to be a string representation of the same integer.
drh5e00f6c2001-09-13 13:46:56 +0000745*/
746case OP_Integer: {
drh6810ce62004-01-31 19:22:56 +0000747 pTos++;
748 pTos->i = pOp->p1;
749 pTos->flags = MEM_Int;
drhe6840902002-03-06 03:08:25 +0000750 if( pOp->p3 ){
drh6810ce62004-01-31 19:22:56 +0000751 pTos->z = pOp->p3;
752 pTos->flags |= MEM_Str | MEM_Static;
753 pTos->n = strlen(pOp->p3)+1;
drhe6840902002-03-06 03:08:25 +0000754 }
drh5e00f6c2001-09-13 13:46:56 +0000755 break;
756}
drh75897232000-05-29 14:26:00 +0000757
drh5e00f6c2001-09-13 13:46:56 +0000758/* Opcode: String * * P3
759**
drh382c0242001-10-06 16:33:02 +0000760** The string value P3 is pushed onto the stack. If P3==0 then a
761** NULL is pushed onto the stack.
drh5e00f6c2001-09-13 13:46:56 +0000762*/
763case OP_String: {
drh6810ce62004-01-31 19:22:56 +0000764 char *z = pOp->p3;
765 pTos++;
drh382c0242001-10-06 16:33:02 +0000766 if( z==0 ){
drh6810ce62004-01-31 19:22:56 +0000767 pTos->flags = MEM_Null;
drh382c0242001-10-06 16:33:02 +0000768 }else{
drh6810ce62004-01-31 19:22:56 +0000769 pTos->z = z;
770 pTos->n = strlen(z) + 1;
771 pTos->flags = MEM_Str | MEM_Static;
drh382c0242001-10-06 16:33:02 +0000772 }
drh5e00f6c2001-09-13 13:46:56 +0000773 break;
774}
drh75897232000-05-29 14:26:00 +0000775
drh50457892003-09-06 01:10:47 +0000776/* Opcode: Variable P1 * *
777**
778** Push the value of variable P1 onto the stack. A variable is
danielk19776f8a5032004-05-10 10:34:51 +0000779** an unknown in the original SQL string as handed to sqlite3_compile().
drh7c972de2003-09-06 22:18:07 +0000780** Any occurance of the '?' character in the original SQL is considered
781** a variable. Variables in the SQL string are number from left to
782** right beginning with 1. The values of variables are set using the
danielk19776f8a5032004-05-10 10:34:51 +0000783** sqlite3_bind() API.
drh50457892003-09-06 01:10:47 +0000784*/
785case OP_Variable: {
drh7c972de2003-09-06 22:18:07 +0000786 int j = pOp->p1 - 1;
drh6810ce62004-01-31 19:22:56 +0000787 pTos++;
drh7c972de2003-09-06 22:18:07 +0000788 if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){
drh6810ce62004-01-31 19:22:56 +0000789 pTos->z = p->azVar[j];
790 pTos->n = p->anVar[j];
791 pTos->flags = MEM_Str | MEM_Static;
drh50457892003-09-06 01:10:47 +0000792 }else{
drh6810ce62004-01-31 19:22:56 +0000793 pTos->flags = MEM_Null;
drh50457892003-09-06 01:10:47 +0000794 }
795 break;
796}
797
drh5e00f6c2001-09-13 13:46:56 +0000798/* Opcode: Pop P1 * *
799**
800** P1 elements are popped off of the top of stack and discarded.
801*/
802case OP_Pop: {
drh6810ce62004-01-31 19:22:56 +0000803 assert( pOp->p1>=0 );
804 popStack(&pTos, pOp->p1);
805 assert( pTos>=&p->aStack[-1] );
drh5e00f6c2001-09-13 13:46:56 +0000806 break;
807}
drh75897232000-05-29 14:26:00 +0000808
drh9cfcf5d2002-01-29 18:41:24 +0000809/* Opcode: Dup P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +0000810**
811** A copy of the P1-th element of the stack
812** is made and pushed onto the top of the stack.
813** The top of the stack is element 0. So the
814** instruction "Dup 0 0 0" will make a copy of the
815** top of the stack.
drhb19a2bc2001-09-16 00:13:26 +0000816**
drh9cfcf5d2002-01-29 18:41:24 +0000817** If the content of the P1-th element is a dynamically
818** allocated string, then a new copy of that string
819** is made if P2==0. If P2!=0, then just a pointer
820** to the string is copied.
821**
drhb19a2bc2001-09-16 00:13:26 +0000822** Also see the Pull instruction.
drh5e00f6c2001-09-13 13:46:56 +0000823*/
824case OP_Dup: {
drh6810ce62004-01-31 19:22:56 +0000825 Mem *pFrom = &pTos[-pOp->p1];
826 assert( pFrom<=pTos && pFrom>=p->aStack );
827 pTos++;
828 memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
829 if( pTos->flags & MEM_Str ){
830 if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
831 pTos->flags &= ~MEM_Dyn;
832 pTos->flags |= MEM_Ephem;
833 }else if( pTos->flags & MEM_Short ){
834 memcpy(pTos->zShort, pFrom->zShort, pTos->n);
835 pTos->z = pTos->zShort;
836 }else if( (pTos->flags & MEM_Static)==0 ){
837 pTos->z = sqliteMallocRaw(pFrom->n);
danielk19776f8a5032004-05-10 10:34:51 +0000838 if( sqlite3_malloc_failed ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +0000839 memcpy(pTos->z, pFrom->z, pFrom->n);
840 pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
841 pTos->flags |= MEM_Dyn;
drh9bbca4c2001-11-06 04:00:18 +0000842 }
drh5e00f6c2001-09-13 13:46:56 +0000843 }
844 break;
845}
drh75897232000-05-29 14:26:00 +0000846
drh5e00f6c2001-09-13 13:46:56 +0000847/* Opcode: Pull P1 * *
848**
849** The P1-th element is removed from its current location on
850** the stack and pushed back on top of the stack. The
851** top of the stack is element 0, so "Pull 0 0 0" is
drhb19a2bc2001-09-16 00:13:26 +0000852** a no-op. "Pull 1 0 0" swaps the top two elements of
853** the stack.
854**
855** See also the Dup instruction.
drh5e00f6c2001-09-13 13:46:56 +0000856*/
857case OP_Pull: {
drh6810ce62004-01-31 19:22:56 +0000858 Mem *pFrom = &pTos[-pOp->p1];
drh5e00f6c2001-09-13 13:46:56 +0000859 int i;
drh00706be2004-01-30 14:49:16 +0000860 Mem ts;
drh6810ce62004-01-31 19:22:56 +0000861
drh6810ce62004-01-31 19:22:56 +0000862 ts = *pFrom;
863 Deephemeralize(pTos);
864 for(i=0; i<pOp->p1; i++, pFrom++){
865 Deephemeralize(&pFrom[1]);
866 *pFrom = pFrom[1];
867 assert( (pFrom->flags & MEM_Ephem)==0 );
868 if( pFrom->flags & MEM_Short ){
869 assert( pFrom->flags & MEM_Str );
870 assert( pFrom->z==pFrom[1].zShort );
drh6810ce62004-01-31 19:22:56 +0000871 pFrom->z = pFrom->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000872 }
drh5e00f6c2001-09-13 13:46:56 +0000873 }
drh6810ce62004-01-31 19:22:56 +0000874 *pTos = ts;
drh6810ce62004-01-31 19:22:56 +0000875 if( pTos->flags & MEM_Short ){
876 assert( pTos->flags & MEM_Str );
877 assert( pTos->z==pTos[-pOp->p1].zShort );
drh6810ce62004-01-31 19:22:56 +0000878 pTos->z = pTos->zShort;
drh9bbca4c2001-11-06 04:00:18 +0000879 }
drh5e00f6c2001-09-13 13:46:56 +0000880 break;
881}
drh75897232000-05-29 14:26:00 +0000882
drh9cfcf5d2002-01-29 18:41:24 +0000883/* Opcode: Push P1 * *
884**
885** Overwrite the value of the P1-th element down on the
886** stack (P1==0 is the top of the stack) with the value
drhac82fcf2002-09-08 17:23:41 +0000887** of the top of the stack. Then pop the top of the stack.
drh9cfcf5d2002-01-29 18:41:24 +0000888*/
889case OP_Push: {
drh6810ce62004-01-31 19:22:56 +0000890 Mem *pTo = &pTos[-pOp->p1];
drh0ca3e242002-01-29 23:07:02 +0000891
drh6810ce62004-01-31 19:22:56 +0000892 assert( pTo>=p->aStack );
893 Deephemeralize(pTos);
894 Release(pTo);
895 *pTo = *pTos;
896 if( pTo->flags & MEM_Short ){
897 assert( pTo->z==pTos->zShort );
898 pTo->z = pTo->zShort;
drh9cfcf5d2002-01-29 18:41:24 +0000899 }
drh6810ce62004-01-31 19:22:56 +0000900 pTos--;
drh9cfcf5d2002-01-29 18:41:24 +0000901 break;
902}
903
drh6810ce62004-01-31 19:22:56 +0000904
drhd6502752004-02-16 03:44:01 +0000905/* Opcode: ColumnName P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +0000906**
907** P3 becomes the P1-th column name (first is 0). An array of pointers
908** to all column names is passed as the 4th parameter to the callback.
drhd6502752004-02-16 03:44:01 +0000909** If P2==1 then this is the last column in the result set and thus the
910** number of columns in the result set will be P1. There must be at least
911** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
912** number of columns specified in OP_Callback must one more than the P1
913** value of the OP_ColumnName that has P2==1.
drh5e00f6c2001-09-13 13:46:56 +0000914*/
915case OP_ColumnName: {
drh001bbcb2003-03-19 03:14:00 +0000916 assert( pOp->p1>=0 && pOp->p1<p->nOp );
drhb1363202002-06-26 02:45:03 +0000917 p->azColName[pOp->p1] = pOp->p3;
drh6a535342001-10-19 16:44:56 +0000918 p->nCallback = 0;
drhd6502752004-02-16 03:44:01 +0000919 if( pOp->p2 ) p->nResColumn = pOp->p1+1;
drh5e00f6c2001-09-13 13:46:56 +0000920 break;
921}
drhc61053b2000-06-04 12:58:36 +0000922
drhdf199a22002-06-14 22:38:41 +0000923/* Opcode: Callback P1 * *
drh5e00f6c2001-09-13 13:46:56 +0000924**
925** Pop P1 values off the stack and form them into an array. Then
926** invoke the callback function using the newly formed array as the
927** 3rd parameter.
928*/
929case OP_Callback: {
drh6810ce62004-01-31 19:22:56 +0000930 int i;
931 char **azArgv = p->zArgv;
932 Mem *pCol;
933
934 pCol = &pTos[1-pOp->p1];
935 assert( pCol>=p->aStack );
936 for(i=0; i<pOp->p1; i++, pCol++){
937 if( pCol->flags & MEM_Null ){
938 azArgv[i] = 0;
drha84f6002001-11-13 19:35:14 +0000939 }else{
drh6810ce62004-01-31 19:22:56 +0000940 Stringify(pCol);
941 azArgv[i] = pCol->z;
drh5e00f6c2001-09-13 13:46:56 +0000942 }
943 }
drh6810ce62004-01-31 19:22:56 +0000944 azArgv[i] = 0;
drha1f9b5e2004-02-14 16:31:02 +0000945 p->nCallback++;
drh826fb5a2004-02-14 23:59:57 +0000946 p->azResColumn = azArgv;
drhd6502752004-02-16 03:44:01 +0000947 assert( p->nResColumn==pOp->p1 );
drh826fb5a2004-02-14 23:59:57 +0000948 p->popStack = pOp->p1;
949 p->pc = pc + 1;
950 p->pTos = pTos;
951 return SQLITE_ROW;
drh5e00f6c2001-09-13 13:46:56 +0000952}
drh75897232000-05-29 14:26:00 +0000953
drh5e00f6c2001-09-13 13:46:56 +0000954/* Opcode: Concat P1 P2 P3
955**
956** Look at the first P1 elements of the stack. Append them all
957** together with the lowest element first. Use P3 as a separator.
958** Put the result on the top of the stack. The original P1 elements
drha9f9d1c2002-06-29 02:20:08 +0000959** are popped from the stack if P2==0 and retained if P2==1. If
960** any element of the stack is NULL, then the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +0000961**
962** If P3 is NULL, then use no separator. When P1==1, this routine
963** makes a copy of the top stack element into memory obtained
964** from sqliteMalloc().
965*/
966case OP_Concat: {
967 char *zNew;
968 int nByte;
969 int nField;
970 int i, j;
971 char *zSep;
972 int nSep;
drh6810ce62004-01-31 19:22:56 +0000973 Mem *pTerm;
drh22827922000-06-06 17:27:05 +0000974
drh5e00f6c2001-09-13 13:46:56 +0000975 nField = pOp->p1;
976 zSep = pOp->p3;
977 if( zSep==0 ) zSep = "";
978 nSep = strlen(zSep);
drh6810ce62004-01-31 19:22:56 +0000979 assert( &pTos[1-nField] >= p->aStack );
drh5e00f6c2001-09-13 13:46:56 +0000980 nByte = 1 - nSep;
drh6810ce62004-01-31 19:22:56 +0000981 pTerm = &pTos[1-nField];
982 for(i=0; i<nField; i++, pTerm++){
983 if( pTerm->flags & MEM_Null ){
drha9f9d1c2002-06-29 02:20:08 +0000984 nByte = -1;
985 break;
drh5e00f6c2001-09-13 13:46:56 +0000986 }else{
drh6810ce62004-01-31 19:22:56 +0000987 Stringify(pTerm);
988 nByte += pTerm->n - 1 + nSep;
drh5e00f6c2001-09-13 13:46:56 +0000989 }
990 }
drha9f9d1c2002-06-29 02:20:08 +0000991 if( nByte<0 ){
drh6810ce62004-01-31 19:22:56 +0000992 if( pOp->p2==0 ){
993 popStack(&pTos, nField);
994 }
995 pTos++;
996 pTos->flags = MEM_Null;
drha9f9d1c2002-06-29 02:20:08 +0000997 break;
998 }
drh8c1238a2003-01-02 14:43:55 +0000999 zNew = sqliteMallocRaw( nByte );
drh5e00f6c2001-09-13 13:46:56 +00001000 if( zNew==0 ) goto no_mem;
1001 j = 0;
drh6810ce62004-01-31 19:22:56 +00001002 pTerm = &pTos[1-nField];
1003 for(i=j=0; i<nField; i++, pTerm++){
1004 assert( pTerm->flags & MEM_Str );
1005 memcpy(&zNew[j], pTerm->z, pTerm->n-1);
1006 j += pTerm->n-1;
1007 if( nSep>0 && i<nField-1 ){
drh5e00f6c2001-09-13 13:46:56 +00001008 memcpy(&zNew[j], zSep, nSep);
1009 j += nSep;
1010 }
1011 }
1012 zNew[j] = 0;
drh6810ce62004-01-31 19:22:56 +00001013 if( pOp->p2==0 ){
1014 popStack(&pTos, nField);
1015 }
1016 pTos++;
1017 pTos->n = nByte;
1018 pTos->flags = MEM_Str|MEM_Dyn;
1019 pTos->z = zNew;
drh5e00f6c2001-09-13 13:46:56 +00001020 break;
1021}
drh75897232000-05-29 14:26:00 +00001022
drh5e00f6c2001-09-13 13:46:56 +00001023/* Opcode: Add * * *
1024**
1025** Pop the top two elements from the stack, add them together,
1026** and push the result back onto the stack. If either element
1027** is a string then it is converted to a double using the atof()
1028** function before the addition.
drhf5905aa2002-05-26 20:54:33 +00001029** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001030*/
1031/* Opcode: Multiply * * *
1032**
1033** Pop the top two elements from the stack, multiply them together,
1034** and push the result back onto the stack. If either element
1035** is a string then it is converted to a double using the atof()
1036** function before the multiplication.
drhf5905aa2002-05-26 20:54:33 +00001037** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001038*/
1039/* Opcode: Subtract * * *
1040**
1041** Pop the top two elements from the stack, subtract the
1042** first (what was on top of the stack) from the second (the
1043** next on stack)
1044** and push the result back onto the stack. If either element
1045** is a string then it is converted to a double using the atof()
1046** function before the subtraction.
drhf5905aa2002-05-26 20:54:33 +00001047** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001048*/
1049/* Opcode: Divide * * *
1050**
1051** Pop the top two elements from the stack, divide the
1052** first (what was on top of the stack) from the second (the
1053** next on stack)
1054** and push the result back onto the stack. If either element
1055** is a string then it is converted to a double using the atof()
1056** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001057** If either operand is NULL, the result is NULL.
drh5e00f6c2001-09-13 13:46:56 +00001058*/
drhbf4133c2001-10-13 02:59:08 +00001059/* Opcode: Remainder * * *
1060**
1061** Pop the top two elements from the stack, divide the
1062** first (what was on top of the stack) from the second (the
1063** next on stack)
1064** and push the remainder after division onto the stack. If either element
1065** is a string then it is converted to a double using the atof()
1066** function before the division. Division by zero returns NULL.
drhf5905aa2002-05-26 20:54:33 +00001067** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001068*/
drh5e00f6c2001-09-13 13:46:56 +00001069case OP_Add:
1070case OP_Subtract:
1071case OP_Multiply:
drhbf4133c2001-10-13 02:59:08 +00001072case OP_Divide:
1073case OP_Remainder: {
drh6810ce62004-01-31 19:22:56 +00001074 Mem *pNos = &pTos[-1];
1075 assert( pNos>=p->aStack );
1076 if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
1077 Release(pTos);
1078 pTos--;
1079 Release(pTos);
1080 pTos->flags = MEM_Null;
1081 }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
danielk1977cfcdaef2004-05-12 07:33:33 +00001082 i64 a, b;
drh6810ce62004-01-31 19:22:56 +00001083 a = pTos->i;
1084 b = pNos->i;
drh5e00f6c2001-09-13 13:46:56 +00001085 switch( pOp->opcode ){
1086 case OP_Add: b += a; break;
1087 case OP_Subtract: b -= a; break;
1088 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001089 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001090 if( a==0 ) goto divide_by_zero;
1091 b /= a;
drh75897232000-05-29 14:26:00 +00001092 break;
1093 }
drhbf4133c2001-10-13 02:59:08 +00001094 default: {
1095 if( a==0 ) goto divide_by_zero;
1096 b %= a;
1097 break;
1098 }
drh75897232000-05-29 14:26:00 +00001099 }
drh6810ce62004-01-31 19:22:56 +00001100 Release(pTos);
1101 pTos--;
1102 Release(pTos);
1103 pTos->i = b;
1104 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001105 }else{
1106 double a, b;
drh6810ce62004-01-31 19:22:56 +00001107 Realify(pTos);
1108 Realify(pNos);
1109 a = pTos->r;
1110 b = pNos->r;
drh5e00f6c2001-09-13 13:46:56 +00001111 switch( pOp->opcode ){
1112 case OP_Add: b += a; break;
1113 case OP_Subtract: b -= a; break;
1114 case OP_Multiply: b *= a; break;
drhbf4133c2001-10-13 02:59:08 +00001115 case OP_Divide: {
drh5e00f6c2001-09-13 13:46:56 +00001116 if( a==0.0 ) goto divide_by_zero;
1117 b /= a;
1118 break;
1119 }
drhbf4133c2001-10-13 02:59:08 +00001120 default: {
drh1ab43002002-01-14 09:28:19 +00001121 int ia = (int)a;
1122 int ib = (int)b;
drhbf4133c2001-10-13 02:59:08 +00001123 if( ia==0.0 ) goto divide_by_zero;
1124 b = ib % ia;
1125 break;
1126 }
drh5e00f6c2001-09-13 13:46:56 +00001127 }
drh6810ce62004-01-31 19:22:56 +00001128 Release(pTos);
1129 pTos--;
1130 Release(pTos);
1131 pTos->r = b;
1132 pTos->flags = MEM_Real;
drh5e00f6c2001-09-13 13:46:56 +00001133 }
1134 break;
1135
1136divide_by_zero:
drh6810ce62004-01-31 19:22:56 +00001137 Release(pTos);
1138 pTos--;
1139 Release(pTos);
1140 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00001141 break;
1142}
1143
drh0bce8352002-02-28 00:41:10 +00001144/* Opcode: Function P1 * P3
drh8e0a2f92002-02-23 23:45:45 +00001145**
drh0bce8352002-02-28 00:41:10 +00001146** Invoke a user function (P3 is a pointer to a Function structure that
drh1350b032002-02-27 19:00:20 +00001147** defines the function) with P1 string arguments taken from the stack.
1148** Pop all arguments from the stack and push back the result.
1149**
1150** See also: AggFunc
drh8e0a2f92002-02-23 23:45:45 +00001151*/
drh0bce8352002-02-28 00:41:10 +00001152case OP_Function: {
drh8e0a2f92002-02-23 23:45:45 +00001153 int n, i;
drh6810ce62004-01-31 19:22:56 +00001154 Mem *pArg;
1155 char **azArgv;
drh1350b032002-02-27 19:00:20 +00001156 sqlite_func ctx;
1157
drh8e0a2f92002-02-23 23:45:45 +00001158 n = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00001159 pArg = &pTos[1-n];
1160 azArgv = p->zArgv;
1161 for(i=0; i<n; i++, pArg++){
1162 if( pArg->flags & MEM_Null ){
1163 azArgv[i] = 0;
drhf5905aa2002-05-26 20:54:33 +00001164 }else{
drh6810ce62004-01-31 19:22:56 +00001165 Stringify(pArg);
1166 azArgv[i] = pArg->z;
drh8e0a2f92002-02-23 23:45:45 +00001167 }
1168 }
drh0bce8352002-02-28 00:41:10 +00001169 ctx.pFunc = (FuncDef*)pOp->p3;
drh00706be2004-01-30 14:49:16 +00001170 ctx.s.flags = MEM_Null;
1171 ctx.s.z = 0;
drh8e0a2f92002-02-23 23:45:45 +00001172 ctx.isError = 0;
drh1350b032002-02-27 19:00:20 +00001173 ctx.isStep = 0;
danielk19774adee202004-05-08 08:23:19 +00001174 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drh6810ce62004-01-31 19:22:56 +00001175 (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
danielk19774adee202004-05-08 08:23:19 +00001176 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6810ce62004-01-31 19:22:56 +00001177 popStack(&pTos, n);
1178 pTos++;
1179 *pTos = ctx.s;
1180 if( pTos->flags & MEM_Short ){
1181 pTos->z = pTos->zShort;
drh8e0a2f92002-02-23 23:45:45 +00001182 }
1183 if( ctx.isError ){
danielk19774adee202004-05-08 08:23:19 +00001184 sqlite3SetString(&p->zErrMsg,
drh6810ce62004-01-31 19:22:56 +00001185 (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
drh8e0a2f92002-02-23 23:45:45 +00001186 rc = SQLITE_ERROR;
1187 }
1188 break;
1189}
1190
drhbf4133c2001-10-13 02:59:08 +00001191/* Opcode: BitAnd * * *
1192**
1193** Pop the top two elements from the stack. Convert both elements
1194** to integers. Push back onto the stack the bit-wise AND of the
1195** two elements.
drhf5905aa2002-05-26 20:54:33 +00001196** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001197*/
1198/* Opcode: BitOr * * *
1199**
1200** Pop the top two elements from the stack. Convert both elements
1201** to integers. Push back onto the stack the bit-wise OR of the
1202** two elements.
drhf5905aa2002-05-26 20:54:33 +00001203** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001204*/
1205/* Opcode: ShiftLeft * * *
1206**
1207** Pop the top two elements from the stack. Convert both elements
1208** to integers. Push back onto the stack the top element shifted
1209** left by N bits where N is the second element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001210** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001211*/
1212/* Opcode: ShiftRight * * *
1213**
1214** Pop the top two elements from the stack. Convert both elements
1215** to integers. Push back onto the stack the top element shifted
1216** right by N bits where N is the second element on the stack.
drhf5905aa2002-05-26 20:54:33 +00001217** If either operand is NULL, the result is NULL.
drhbf4133c2001-10-13 02:59:08 +00001218*/
1219case OP_BitAnd:
1220case OP_BitOr:
1221case OP_ShiftLeft:
1222case OP_ShiftRight: {
drh6810ce62004-01-31 19:22:56 +00001223 Mem *pNos = &pTos[-1];
drhbf4133c2001-10-13 02:59:08 +00001224 int a, b;
drh6810ce62004-01-31 19:22:56 +00001225
1226 assert( pNos>=p->aStack );
1227 if( (pTos->flags | pNos->flags) & MEM_Null ){
1228 popStack(&pTos, 2);
1229 pTos++;
1230 pTos->flags = MEM_Null;
drhf5905aa2002-05-26 20:54:33 +00001231 break;
1232 }
drh6810ce62004-01-31 19:22:56 +00001233 Integerify(pTos);
1234 Integerify(pNos);
1235 a = pTos->i;
1236 b = pNos->i;
drhbf4133c2001-10-13 02:59:08 +00001237 switch( pOp->opcode ){
1238 case OP_BitAnd: a &= b; break;
1239 case OP_BitOr: a |= b; break;
1240 case OP_ShiftLeft: a <<= b; break;
1241 case OP_ShiftRight: a >>= b; break;
1242 default: /* CANT HAPPEN */ break;
1243 }
drh6810ce62004-01-31 19:22:56 +00001244 assert( (pTos->flags & MEM_Dyn)==0 );
1245 assert( (pNos->flags & MEM_Dyn)==0 );
1246 pTos--;
drh79f14b72004-03-03 01:51:24 +00001247 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001248 pTos->i = a;
drh79f14b72004-03-03 01:51:24 +00001249 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001250 break;
1251}
1252
drh5e00f6c2001-09-13 13:46:56 +00001253/* Opcode: AddImm P1 * *
1254**
drh4a324312001-12-21 14:30:42 +00001255** Add the value P1 to whatever is on top of the stack. The result
1256** is always an integer.
1257**
1258** To force the top of the stack to be an integer, just add 0.
drh5e00f6c2001-09-13 13:46:56 +00001259*/
1260case OP_AddImm: {
drh6810ce62004-01-31 19:22:56 +00001261 assert( pTos>=p->aStack );
1262 Integerify(pTos);
1263 pTos->i += pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00001264 break;
1265}
1266
drh751f4122004-01-14 21:59:22 +00001267/* Opcode: ForceInt P1 P2 *
drh1dd59e02003-07-06 17:22:25 +00001268**
drh751f4122004-01-14 21:59:22 +00001269** Convert the top of the stack into an integer. If the current top of
1270** the stack is not numeric (meaning that is is a NULL or a string that
1271** does not look like an integer or floating point number) then pop the
1272** stack and jump to P2. If the top of the stack is numeric then
1273** convert it into the least integer that is greater than or equal to its
1274** current value if P1==0, or to the least integer that is strictly
1275** greater than its current value if P1==1.
drh1dd59e02003-07-06 17:22:25 +00001276*/
drh751f4122004-01-14 21:59:22 +00001277case OP_ForceInt: {
drh751f4122004-01-14 21:59:22 +00001278 int v;
drh6810ce62004-01-31 19:22:56 +00001279 assert( pTos>=p->aStack );
1280 if( (pTos->flags & (MEM_Int|MEM_Real))==0
danielk19773d1bfea2004-05-14 11:00:53 +00001281 && ((pTos->flags & MEM_Str)==0 || sqlite3IsNumber(pTos->z, 0)==0) ){
drh6810ce62004-01-31 19:22:56 +00001282 Release(pTos);
1283 pTos--;
drh1dd59e02003-07-06 17:22:25 +00001284 pc = pOp->p2 - 1;
drh751f4122004-01-14 21:59:22 +00001285 break;
drh1dd59e02003-07-06 17:22:25 +00001286 }
drh6810ce62004-01-31 19:22:56 +00001287 if( pTos->flags & MEM_Int ){
1288 v = pTos->i + (pOp->p1!=0);
drh751f4122004-01-14 21:59:22 +00001289 }else{
drh6810ce62004-01-31 19:22:56 +00001290 Realify(pTos);
1291 v = (int)pTos->r;
1292 if( pTos->r>(double)v ) v++;
1293 if( pOp->p1 && pTos->r==(double)v ) v++;
drh751f4122004-01-14 21:59:22 +00001294 }
drh6810ce62004-01-31 19:22:56 +00001295 Release(pTos);
1296 pTos->i = v;
1297 pTos->flags = MEM_Int;
drh1dd59e02003-07-06 17:22:25 +00001298 break;
1299}
1300
drhf1351b62002-07-31 19:50:26 +00001301/* Opcode: MustBeInt P1 P2 *
drh8aff1012001-12-22 14:49:24 +00001302**
1303** Force the top of the stack to be an integer. If the top of the
drhd99f7062002-06-08 23:25:08 +00001304** stack is not an integer and cannot be converted into an integer
drh8aff1012001-12-22 14:49:24 +00001305** with out data loss, then jump immediately to P2, or if P2==0
1306** raise an SQLITE_MISMATCH exception.
drhf1351b62002-07-31 19:50:26 +00001307**
1308** If the top of the stack is not an integer and P2 is not zero and
1309** P1 is 1, then the stack is popped. In all other cases, the depth
1310** of the stack is unchanged.
drh8aff1012001-12-22 14:49:24 +00001311*/
1312case OP_MustBeInt: {
drh6810ce62004-01-31 19:22:56 +00001313 assert( pTos>=p->aStack );
1314 if( pTos->flags & MEM_Int ){
drh8aff1012001-12-22 14:49:24 +00001315 /* Do nothing */
drh6810ce62004-01-31 19:22:56 +00001316 }else if( pTos->flags & MEM_Real ){
1317 int i = (int)pTos->r;
drh7d02cb72003-06-04 16:24:39 +00001318 double r = (double)i;
drh6810ce62004-01-31 19:22:56 +00001319 if( r!=pTos->r ){
drh8aff1012001-12-22 14:49:24 +00001320 goto mismatch;
1321 }
drh6810ce62004-01-31 19:22:56 +00001322 pTos->i = i;
1323 }else if( pTos->flags & MEM_Str ){
danielk19773d1bfea2004-05-14 11:00:53 +00001324 i64 v;
drh6810ce62004-01-31 19:22:56 +00001325 if( !toInt(pTos->z, &v) ){
drh1dd59e02003-07-06 17:22:25 +00001326 double r;
danielk19773d1bfea2004-05-14 11:00:53 +00001327 if( !sqlite3IsNumber(pTos->z, 0) ){
drh1dd59e02003-07-06 17:22:25 +00001328 goto mismatch;
1329 }
drh6810ce62004-01-31 19:22:56 +00001330 Realify(pTos);
1331 v = (int)pTos->r;
drh1dd59e02003-07-06 17:22:25 +00001332 r = (double)v;
drh6810ce62004-01-31 19:22:56 +00001333 if( r!=pTos->r ){
drh1dd59e02003-07-06 17:22:25 +00001334 goto mismatch;
1335 }
drh8aff1012001-12-22 14:49:24 +00001336 }
drh6810ce62004-01-31 19:22:56 +00001337 pTos->i = v;
drh8aff1012001-12-22 14:49:24 +00001338 }else{
1339 goto mismatch;
1340 }
drh6810ce62004-01-31 19:22:56 +00001341 Release(pTos);
1342 pTos->flags = MEM_Int;
drh8aff1012001-12-22 14:49:24 +00001343 break;
1344
1345mismatch:
1346 if( pOp->p2==0 ){
1347 rc = SQLITE_MISMATCH;
1348 goto abort_due_to_error;
1349 }else{
drh6810ce62004-01-31 19:22:56 +00001350 if( pOp->p1 ) popStack(&pTos, 1);
drh8aff1012001-12-22 14:49:24 +00001351 pc = pOp->p2 - 1;
1352 }
1353 break;
1354}
1355
drhf5905aa2002-05-26 20:54:33 +00001356/* Opcode: Eq P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001357**
1358** Pop the top two elements from the stack. If they are equal, then
1359** jump to instruction P2. Otherwise, continue to the next instruction.
drhf5905aa2002-05-26 20:54:33 +00001360**
1361** If either operand is NULL (and thus if the result is unknown) then
1362** take the jump if P1 is true.
1363**
drhc9b84a12002-06-20 11:36:48 +00001364** If both values are numeric, they are converted to doubles using atof()
1365** and compared for equality that way. Otherwise the strcmp() library
1366** routine is used for the comparison. For a pure text comparison
1367** use OP_StrEq.
1368**
drhf5905aa2002-05-26 20:54:33 +00001369** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1370** stack if the jump would have been taken, or a 0 if not. Push a
1371** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001372*/
drhf5905aa2002-05-26 20:54:33 +00001373/* Opcode: Ne P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001374**
1375** Pop the top two elements from the stack. If they are not equal, then
1376** jump to instruction P2. Otherwise, continue to the next instruction.
drhf5905aa2002-05-26 20:54:33 +00001377**
1378** If either operand is NULL (and thus if the result is unknown) then
1379** take the jump if P1 is true.
1380**
drhc9b84a12002-06-20 11:36:48 +00001381** If both values are numeric, they are converted to doubles using atof()
1382** and compared in that format. Otherwise the strcmp() library
1383** routine is used for the comparison. For a pure text comparison
1384** use OP_StrNe.
1385**
drhf5905aa2002-05-26 20:54:33 +00001386** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1387** stack if the jump would have been taken, or a 0 if not. Push a
1388** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001389*/
drhf5905aa2002-05-26 20:54:33 +00001390/* Opcode: Lt P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001391**
1392** Pop the top two elements from the stack. If second element (the
1393** next on stack) is less than the first (the top of stack), then
1394** jump to instruction P2. Otherwise, continue to the next instruction.
1395** In other words, jump if NOS<TOS.
drhf5905aa2002-05-26 20:54:33 +00001396**
1397** If either operand is NULL (and thus if the result is unknown) then
1398** take the jump if P1 is true.
1399**
drhc9b84a12002-06-20 11:36:48 +00001400** If both values are numeric, they are converted to doubles using atof()
1401** and compared in that format. Numeric values are always less than
1402** non-numeric values. If both operands are non-numeric, the strcmp() library
1403** routine is used for the comparison. For a pure text comparison
1404** use OP_StrLt.
1405**
drhf5905aa2002-05-26 20:54:33 +00001406** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1407** stack if the jump would have been taken, or a 0 if not. Push a
1408** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001409*/
drhf5905aa2002-05-26 20:54:33 +00001410/* Opcode: Le P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001411**
1412** Pop the top two elements from the stack. If second element (the
1413** next on stack) is less than or equal to the first (the top of stack),
1414** then jump to instruction P2. In other words, jump if NOS<=TOS.
drhf5905aa2002-05-26 20:54:33 +00001415**
1416** If either operand is NULL (and thus if the result is unknown) then
1417** take the jump if P1 is true.
1418**
drhc9b84a12002-06-20 11:36:48 +00001419** If both values are numeric, they are converted to doubles using atof()
1420** and compared in that format. Numeric values are always less than
1421** non-numeric values. If both operands are non-numeric, the strcmp() library
1422** routine is used for the comparison. For a pure text comparison
1423** use OP_StrLe.
1424**
drhf5905aa2002-05-26 20:54:33 +00001425** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1426** stack if the jump would have been taken, or a 0 if not. Push a
1427** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001428*/
drhf5905aa2002-05-26 20:54:33 +00001429/* Opcode: Gt P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001430**
1431** Pop the top two elements from the stack. If second element (the
1432** next on stack) is greater than the first (the top of stack),
1433** then jump to instruction P2. In other words, jump if NOS>TOS.
drhf5905aa2002-05-26 20:54:33 +00001434**
1435** If either operand is NULL (and thus if the result is unknown) then
1436** take the jump if P1 is true.
1437**
drhc9b84a12002-06-20 11:36:48 +00001438** If both values are numeric, they are converted to doubles using atof()
1439** and compared in that format. Numeric values are always less than
1440** non-numeric values. If both operands are non-numeric, the strcmp() library
1441** routine is used for the comparison. For a pure text comparison
1442** use OP_StrGt.
1443**
drhf5905aa2002-05-26 20:54:33 +00001444** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1445** stack if the jump would have been taken, or a 0 if not. Push a
1446** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001447*/
drhf5905aa2002-05-26 20:54:33 +00001448/* Opcode: Ge P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001449**
1450** Pop the top two elements from the stack. If second element (the next
1451** on stack) is greater than or equal to the first (the top of stack),
1452** then jump to instruction P2. In other words, jump if NOS>=TOS.
drhf5905aa2002-05-26 20:54:33 +00001453**
1454** If either operand is NULL (and thus if the result is unknown) then
1455** take the jump if P1 is true.
1456**
drhc9b84a12002-06-20 11:36:48 +00001457** If both values are numeric, they are converted to doubles using atof()
1458** and compared in that format. Numeric values are always less than
1459** non-numeric values. If both operands are non-numeric, the strcmp() library
1460** routine is used for the comparison. For a pure text comparison
1461** use OP_StrGe.
1462**
drhf5905aa2002-05-26 20:54:33 +00001463** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1464** stack if the jump would have been taken, or a 0 if not. Push a
1465** NULL if either operand was NULL.
drh5e00f6c2001-09-13 13:46:56 +00001466*/
1467case OP_Eq:
1468case OP_Ne:
1469case OP_Lt:
1470case OP_Le:
1471case OP_Gt:
1472case OP_Ge: {
drh6810ce62004-01-31 19:22:56 +00001473 Mem *pNos = &pTos[-1];
danielk19773d1bfea2004-05-14 11:00:53 +00001474 i64 c, v;
drh5e00f6c2001-09-13 13:46:56 +00001475 int ft, fn;
drh6810ce62004-01-31 19:22:56 +00001476 assert( pNos>=p->aStack );
1477 ft = pTos->flags;
1478 fn = pNos->flags;
drh00706be2004-01-30 14:49:16 +00001479 if( (ft | fn) & MEM_Null ){
drh6810ce62004-01-31 19:22:56 +00001480 popStack(&pTos, 2);
drhf5905aa2002-05-26 20:54:33 +00001481 if( pOp->p2 ){
1482 if( pOp->p1 ) pc = pOp->p2-1;
1483 }else{
drh6810ce62004-01-31 19:22:56 +00001484 pTos++;
1485 pTos->flags = MEM_Null;
drhf5905aa2002-05-26 20:54:33 +00001486 }
1487 break;
drh00706be2004-01-30 14:49:16 +00001488 }else if( (ft & fn & MEM_Int)==MEM_Int ){
drh6810ce62004-01-31 19:22:56 +00001489 c = pNos->i - pTos->i;
1490 }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){
1491 c = v - pTos->i;
1492 }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){
1493 c = pNos->i - v;
drh5e00f6c2001-09-13 13:46:56 +00001494 }else{
drh6810ce62004-01-31 19:22:56 +00001495 Stringify(pTos);
1496 Stringify(pNos);
danielk19774adee202004-05-08 08:23:19 +00001497 c = sqlite3Compare(pNos->z, pTos->z);
drh5e00f6c2001-09-13 13:46:56 +00001498 }
1499 switch( pOp->opcode ){
1500 case OP_Eq: c = c==0; break;
1501 case OP_Ne: c = c!=0; break;
1502 case OP_Lt: c = c<0; break;
1503 case OP_Le: c = c<=0; break;
1504 case OP_Gt: c = c>0; break;
1505 default: c = c>=0; break;
1506 }
drh6810ce62004-01-31 19:22:56 +00001507 popStack(&pTos, 2);
drhf5905aa2002-05-26 20:54:33 +00001508 if( pOp->p2 ){
1509 if( c ) pc = pOp->p2-1;
1510 }else{
drh6810ce62004-01-31 19:22:56 +00001511 pTos++;
1512 pTos->i = c;
1513 pTos->flags = MEM_Int;
drhf5905aa2002-05-26 20:54:33 +00001514 }
drh5e00f6c2001-09-13 13:46:56 +00001515 break;
1516}
drh8f619cc2002-09-08 00:04:50 +00001517/* INSERT NO CODE HERE!
1518**
1519** The opcode numbers are extracted from this source file by doing
1520**
1521** grep '^case OP_' vdbe.c | ... >opcodes.h
1522**
1523** The opcodes are numbered in the order that they appear in this file.
1524** But in order for the expression generating code to work right, the
1525** string comparison operators that follow must be numbered exactly 6
1526** greater than the numeric comparison opcodes above. So no other
1527** cases can appear between the two.
1528*/
drhc9b84a12002-06-20 11:36:48 +00001529/* Opcode: StrEq P1 P2 *
1530**
1531** Pop the top two elements from the stack. If they are equal, then
1532** jump to instruction P2. Otherwise, continue to the next instruction.
1533**
1534** If either operand is NULL (and thus if the result is unknown) then
1535** take the jump if P1 is true.
1536**
1537** The strcmp() library routine is used for the comparison. For a
1538** numeric comparison, use OP_Eq.
1539**
1540** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1541** stack if the jump would have been taken, or a 0 if not. Push a
1542** NULL if either operand was NULL.
1543*/
1544/* Opcode: StrNe P1 P2 *
1545**
1546** Pop the top two elements from the stack. If they are not equal, then
1547** jump to instruction P2. Otherwise, continue to the next instruction.
1548**
1549** If either operand is NULL (and thus if the result is unknown) then
1550** take the jump if P1 is true.
1551**
1552** The strcmp() library routine is used for the comparison. For a
1553** numeric comparison, use OP_Ne.
1554**
1555** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1556** stack if the jump would have been taken, or a 0 if not. Push a
1557** NULL if either operand was NULL.
1558*/
1559/* Opcode: StrLt P1 P2 *
1560**
1561** Pop the top two elements from the stack. If second element (the
1562** next on stack) is less than the first (the top of stack), then
1563** jump to instruction P2. Otherwise, continue to the next instruction.
1564** In other words, jump if NOS<TOS.
1565**
1566** If either operand is NULL (and thus if the result is unknown) then
1567** take the jump if P1 is true.
1568**
1569** The strcmp() library routine is used for the comparison. For a
1570** numeric comparison, use OP_Lt.
1571**
1572** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1573** stack if the jump would have been taken, or a 0 if not. Push a
1574** NULL if either operand was NULL.
1575*/
1576/* Opcode: StrLe P1 P2 *
1577**
1578** Pop the top two elements from the stack. If second element (the
1579** next on stack) is less than or equal to the first (the top of stack),
1580** then jump to instruction P2. In other words, jump if NOS<=TOS.
1581**
1582** If either operand is NULL (and thus if the result is unknown) then
1583** take the jump if P1 is true.
1584**
1585** The strcmp() library routine is used for the comparison. For a
1586** numeric comparison, use OP_Le.
1587**
1588** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1589** stack if the jump would have been taken, or a 0 if not. Push a
1590** NULL if either operand was NULL.
1591*/
1592/* Opcode: StrGt P1 P2 *
1593**
1594** Pop the top two elements from the stack. If second element (the
1595** next on stack) is greater than the first (the top of stack),
1596** then jump to instruction P2. In other words, jump if NOS>TOS.
1597**
1598** If either operand is NULL (and thus if the result is unknown) then
1599** take the jump if P1 is true.
1600**
1601** The strcmp() library routine is used for the comparison. For a
1602** numeric comparison, use OP_Gt.
1603**
1604** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1605** stack if the jump would have been taken, or a 0 if not. Push a
1606** NULL if either operand was NULL.
1607*/
1608/* Opcode: StrGe P1 P2 *
1609**
1610** Pop the top two elements from the stack. If second element (the next
1611** on stack) is greater than or equal to the first (the top of stack),
1612** then jump to instruction P2. In other words, jump if NOS>=TOS.
1613**
1614** If either operand is NULL (and thus if the result is unknown) then
1615** take the jump if P1 is true.
1616**
1617** The strcmp() library routine is used for the comparison. For a
1618** numeric comparison, use OP_Ge.
1619**
1620** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1621** stack if the jump would have been taken, or a 0 if not. Push a
1622** NULL if either operand was NULL.
1623*/
1624case OP_StrEq:
1625case OP_StrNe:
1626case OP_StrLt:
1627case OP_StrLe:
1628case OP_StrGt:
1629case OP_StrGe: {
drh6810ce62004-01-31 19:22:56 +00001630 Mem *pNos = &pTos[-1];
drhc9b84a12002-06-20 11:36:48 +00001631 int c;
drh6810ce62004-01-31 19:22:56 +00001632 assert( pNos>=p->aStack );
1633 if( (pNos->flags | pTos->flags) & MEM_Null ){
1634 popStack(&pTos, 2);
drhc9b84a12002-06-20 11:36:48 +00001635 if( pOp->p2 ){
1636 if( pOp->p1 ) pc = pOp->p2-1;
1637 }else{
drh6810ce62004-01-31 19:22:56 +00001638 pTos++;
1639 pTos->flags = MEM_Null;
drhc9b84a12002-06-20 11:36:48 +00001640 }
1641 break;
1642 }else{
drh6810ce62004-01-31 19:22:56 +00001643 Stringify(pTos);
1644 Stringify(pNos);
1645 c = strcmp(pNos->z, pTos->z);
drhc9b84a12002-06-20 11:36:48 +00001646 }
drh8f619cc2002-09-08 00:04:50 +00001647 /* The asserts on each case of the following switch are there to verify
1648 ** that string comparison opcodes are always exactly 6 greater than the
1649 ** corresponding numeric comparison opcodes. The code generator depends
1650 ** on this fact.
1651 */
drhc9b84a12002-06-20 11:36:48 +00001652 switch( pOp->opcode ){
drh8f619cc2002-09-08 00:04:50 +00001653 case OP_StrEq: c = c==0; assert( pOp->opcode-6==OP_Eq ); break;
1654 case OP_StrNe: c = c!=0; assert( pOp->opcode-6==OP_Ne ); break;
1655 case OP_StrLt: c = c<0; assert( pOp->opcode-6==OP_Lt ); break;
1656 case OP_StrLe: c = c<=0; assert( pOp->opcode-6==OP_Le ); break;
1657 case OP_StrGt: c = c>0; assert( pOp->opcode-6==OP_Gt ); break;
1658 default: c = c>=0; assert( pOp->opcode-6==OP_Ge ); break;
drhc9b84a12002-06-20 11:36:48 +00001659 }
drh6810ce62004-01-31 19:22:56 +00001660 popStack(&pTos, 2);
drhc9b84a12002-06-20 11:36:48 +00001661 if( pOp->p2 ){
1662 if( c ) pc = pOp->p2-1;
1663 }else{
drh6810ce62004-01-31 19:22:56 +00001664 pTos++;
1665 pTos->flags = MEM_Int;
1666 pTos->i = c;
drhc9b84a12002-06-20 11:36:48 +00001667 }
1668 break;
1669}
1670
drh5e00f6c2001-09-13 13:46:56 +00001671/* Opcode: And * * *
1672**
1673** Pop two values off the stack. Take the logical AND of the
1674** two values and push the resulting boolean value back onto the
1675** stack.
1676*/
1677/* Opcode: Or * * *
1678**
1679** Pop two values off the stack. Take the logical OR of the
1680** two values and push the resulting boolean value back onto the
1681** stack.
1682*/
1683case OP_And:
1684case OP_Or: {
drh6810ce62004-01-31 19:22:56 +00001685 Mem *pNos = &pTos[-1];
drhbb113512002-05-27 01:04:51 +00001686 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1687
drh6810ce62004-01-31 19:22:56 +00001688 assert( pNos>=p->aStack );
1689 if( pTos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001690 v1 = 2;
drh5e00f6c2001-09-13 13:46:56 +00001691 }else{
drh6810ce62004-01-31 19:22:56 +00001692 Integerify(pTos);
1693 v1 = pTos->i==0;
drhbb113512002-05-27 01:04:51 +00001694 }
drh6810ce62004-01-31 19:22:56 +00001695 if( pNos->flags & MEM_Null ){
drhbb113512002-05-27 01:04:51 +00001696 v2 = 2;
1697 }else{
drh6810ce62004-01-31 19:22:56 +00001698 Integerify(pNos);
1699 v2 = pNos->i==0;
drhbb113512002-05-27 01:04:51 +00001700 }
1701 if( pOp->opcode==OP_And ){
1702 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1703 v1 = and_logic[v1*3+v2];
1704 }else{
1705 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1706 v1 = or_logic[v1*3+v2];
drh5e00f6c2001-09-13 13:46:56 +00001707 }
drh6810ce62004-01-31 19:22:56 +00001708 popStack(&pTos, 2);
1709 pTos++;
drhbb113512002-05-27 01:04:51 +00001710 if( v1==2 ){
drh6810ce62004-01-31 19:22:56 +00001711 pTos->flags = MEM_Null;
drhbb113512002-05-27 01:04:51 +00001712 }else{
drh6810ce62004-01-31 19:22:56 +00001713 pTos->i = v1==0;
1714 pTos->flags = MEM_Int;
drhbb113512002-05-27 01:04:51 +00001715 }
drh5e00f6c2001-09-13 13:46:56 +00001716 break;
1717}
1718
1719/* Opcode: Negative * * *
1720**
1721** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001722** with its additive inverse. If the top of the stack is NULL
1723** its value is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001724*/
drhbf4133c2001-10-13 02:59:08 +00001725/* Opcode: AbsValue * * *
1726**
1727** Treat the top of the stack as a numeric quantity. Replace it
drhf5905aa2002-05-26 20:54:33 +00001728** with its absolute value. If the top of the stack is NULL
1729** its value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001730*/
1731case OP_Negative:
1732case OP_AbsValue: {
drh6810ce62004-01-31 19:22:56 +00001733 assert( pTos>=p->aStack );
1734 if( pTos->flags & MEM_Real ){
1735 Release(pTos);
1736 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1737 pTos->r = -pTos->r;
drhbf4133c2001-10-13 02:59:08 +00001738 }
drh6810ce62004-01-31 19:22:56 +00001739 pTos->flags = MEM_Real;
1740 }else if( pTos->flags & MEM_Int ){
1741 Release(pTos);
1742 if( pOp->opcode==OP_Negative || pTos->i<0 ){
1743 pTos->i = -pTos->i;
drhbf4133c2001-10-13 02:59:08 +00001744 }
drh6810ce62004-01-31 19:22:56 +00001745 pTos->flags = MEM_Int;
1746 }else if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001747 /* Do nothing */
drh5e00f6c2001-09-13 13:46:56 +00001748 }else{
drh6810ce62004-01-31 19:22:56 +00001749 Realify(pTos);
1750 Release(pTos);
1751 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1752 pTos->r = -pTos->r;
drhbf4133c2001-10-13 02:59:08 +00001753 }
drh6810ce62004-01-31 19:22:56 +00001754 pTos->flags = MEM_Real;
drh5e00f6c2001-09-13 13:46:56 +00001755 }
1756 break;
1757}
1758
1759/* Opcode: Not * * *
1760**
1761** Interpret the top of the stack as a boolean value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001762** with its complement. If the top of the stack is NULL its value
1763** is unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001764*/
1765case OP_Not: {
drh6810ce62004-01-31 19:22:56 +00001766 assert( pTos>=p->aStack );
1767 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1768 Integerify(pTos);
drh79f14b72004-03-03 01:51:24 +00001769 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001770 pTos->i = !pTos->i;
drh79f14b72004-03-03 01:51:24 +00001771 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00001772 break;
1773}
1774
drh18b81e52001-11-01 13:52:52 +00001775/* Opcode: BitNot * * *
drhbf4133c2001-10-13 02:59:08 +00001776**
1777** Interpret the top of the stack as an value. Replace it
drhf5905aa2002-05-26 20:54:33 +00001778** with its ones-complement. If the top of the stack is NULL its
1779** value is unchanged.
drhbf4133c2001-10-13 02:59:08 +00001780*/
1781case OP_BitNot: {
drh6810ce62004-01-31 19:22:56 +00001782 assert( pTos>=p->aStack );
1783 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1784 Integerify(pTos);
drh79f14b72004-03-03 01:51:24 +00001785 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00001786 pTos->i = ~pTos->i;
drh79f14b72004-03-03 01:51:24 +00001787 pTos->flags = MEM_Int;
drhbf4133c2001-10-13 02:59:08 +00001788 break;
1789}
1790
drh5e00f6c2001-09-13 13:46:56 +00001791/* Opcode: Noop * * *
1792**
1793** Do nothing. This instruction is often useful as a jump
1794** destination.
1795*/
1796case OP_Noop: {
1797 break;
1798}
1799
drhf5905aa2002-05-26 20:54:33 +00001800/* Opcode: If P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001801**
1802** Pop a single boolean from the stack. If the boolean popped is
1803** true, then jump to p2. Otherwise continue to the next instruction.
1804** An integer is false if zero and true otherwise. A string is
1805** false if it has zero length and true otherwise.
drhf5905aa2002-05-26 20:54:33 +00001806**
1807** If the value popped of the stack is NULL, then take the jump if P1
1808** is true and fall through if P1 is false.
drh5e00f6c2001-09-13 13:46:56 +00001809*/
drhf5905aa2002-05-26 20:54:33 +00001810/* Opcode: IfNot P1 P2 *
1811**
1812** Pop a single boolean from the stack. If the boolean popped is
1813** false, then jump to p2. Otherwise continue to the next instruction.
1814** An integer is false if zero and true otherwise. A string is
1815** false if it has zero length and true otherwise.
1816**
1817** If the value popped of the stack is NULL, then take the jump if P1
1818** is true and fall through if P1 is false.
1819*/
1820case OP_If:
1821case OP_IfNot: {
drh5e00f6c2001-09-13 13:46:56 +00001822 int c;
drh6810ce62004-01-31 19:22:56 +00001823 assert( pTos>=p->aStack );
1824 if( pTos->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001825 c = pOp->p1;
1826 }else{
drh6810ce62004-01-31 19:22:56 +00001827 Integerify(pTos);
1828 c = pTos->i;
drhf5905aa2002-05-26 20:54:33 +00001829 if( pOp->opcode==OP_IfNot ) c = !c;
1830 }
drh6810ce62004-01-31 19:22:56 +00001831 assert( (pTos->flags & MEM_Dyn)==0 );
1832 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00001833 if( c ) pc = pOp->p2-1;
1834 break;
1835}
1836
drhf5905aa2002-05-26 20:54:33 +00001837/* Opcode: IsNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001838**
drhf5905aa2002-05-26 20:54:33 +00001839** If any of the top abs(P1) values on the stack are NULL, then jump
drh143f3c42004-01-07 20:37:52 +00001840** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack
1841** unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001842*/
1843case OP_IsNull: {
drhf5905aa2002-05-26 20:54:33 +00001844 int i, cnt;
drh6810ce62004-01-31 19:22:56 +00001845 Mem *pTerm;
drhf5905aa2002-05-26 20:54:33 +00001846 cnt = pOp->p1;
1847 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001848 pTerm = &pTos[1-cnt];
1849 assert( pTerm>=p->aStack );
1850 for(i=0; i<cnt; i++, pTerm++){
1851 if( pTerm->flags & MEM_Null ){
drhf5905aa2002-05-26 20:54:33 +00001852 pc = pOp->p2-1;
1853 break;
1854 }
1855 }
drh6810ce62004-01-31 19:22:56 +00001856 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001857 break;
1858}
1859
drhf5905aa2002-05-26 20:54:33 +00001860/* Opcode: NotNull P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00001861**
drh143f3c42004-01-07 20:37:52 +00001862** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the
1863** stack if P1 times if P1 is greater than zero. If P1 is less than
1864** zero then leave the stack unchanged.
drh5e00f6c2001-09-13 13:46:56 +00001865*/
1866case OP_NotNull: {
drh143f3c42004-01-07 20:37:52 +00001867 int i, cnt;
1868 cnt = pOp->p1;
1869 if( cnt<0 ) cnt = -cnt;
drh6810ce62004-01-31 19:22:56 +00001870 assert( &pTos[1-cnt] >= p->aStack );
1871 for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
drh143f3c42004-01-07 20:37:52 +00001872 if( i>=cnt ) pc = pOp->p2-1;
drh6810ce62004-01-31 19:22:56 +00001873 if( pOp->p1>0 ) popStack(&pTos, cnt);
drh5e00f6c2001-09-13 13:46:56 +00001874 break;
1875}
1876
drh9188b382004-05-14 21:12:22 +00001877/* Opcode: Column P1 P2 *
danielk1977192ac1d2004-05-10 07:17:30 +00001878**
danielk1977cfcdaef2004-05-12 07:33:33 +00001879** Interpret the data that cursor P1 points to as a structure built using
1880** the MakeRecord instruction. (See the MakeRecord opcode for additional
1881** information about the format of the data.) Push onto the stack the value
1882** of the P2-th column contained in the data.
danielk1977192ac1d2004-05-10 07:17:30 +00001883**
danielk1977cfcdaef2004-05-12 07:33:33 +00001884** If the KeyAsData opcode has previously executed on this cursor, then the
1885** field might be extracted from the key rather than the data.
danielk1977192ac1d2004-05-10 07:17:30 +00001886**
danielk1977cfcdaef2004-05-12 07:33:33 +00001887** If P1 is negative, then the record is stored on the stack rather than in
1888** a table. For P1==-1, the top of the stack is used. For P1==-2, the
1889** next on the stack is used. And so forth. The value pushed is always
1890** just a pointer into the record which is stored further down on the
1891** stack. The column value is not copied.
danielk1977192ac1d2004-05-10 07:17:30 +00001892*/
danielk1977cfcdaef2004-05-12 07:33:33 +00001893case OP_Column: {
1894 int payloadSize; /* Number of bytes in the record */
danielk1977192ac1d2004-05-10 07:17:30 +00001895 int i = pOp->p1;
danielk1977cfcdaef2004-05-12 07:33:33 +00001896 int p2 = pOp->p2; /* column number to retrieve */
danielk1977192ac1d2004-05-10 07:17:30 +00001897 Cursor *pC;
danielk1977cfcdaef2004-05-12 07:33:33 +00001898 char *zRec; /* Pointer to record-data from stack or pseudo-table. */
danielk1977192ac1d2004-05-10 07:17:30 +00001899 BtCursor *pCrsr;
1900
danielk1977cfcdaef2004-05-12 07:33:33 +00001901 char *zData;
1902 int freeZdata = 0; /* zData requires sqliteFree() */
1903
drh9188b382004-05-14 21:12:22 +00001904 u64 nField; /* number of fields in the record */
danielk1977cfcdaef2004-05-12 07:33:33 +00001905
1906 int len; /* The length of the serialized data for the column */
1907 int offset;
1908 int nn;
danielk1977192ac1d2004-05-10 07:17:30 +00001909
1910 assert( i<p->nCursor );
1911 pTos++;
danielk1977cfcdaef2004-05-12 07:33:33 +00001912
1913 /* This block sets the variable payloadSize, and if the data is coming
1914 ** from the stack or from a pseudo-table zRec. If the data is coming
1915 ** from a real cursor, then zRec is left as NULL.
1916 */
danielk1977192ac1d2004-05-10 07:17:30 +00001917 if( i<0 ){
1918 assert( &pTos[i]>=p->aStack );
1919 assert( pTos[i].flags & MEM_Str );
1920 zRec = pTos[i].z;
1921 payloadSize = pTos[i].n;
drh9188b382004-05-14 21:12:22 +00001922 pC->cacheValid = 0;
drhd7556d22004-05-14 21:59:40 +00001923 }else if( (pC = p->apCsr[i])->pCursor!=0 ){
danielk1977192ac1d2004-05-10 07:17:30 +00001924 sqlite3VdbeCursorMoveto(pC);
1925 zRec = 0;
1926 pCrsr = pC->pCursor;
1927 if( pC->nullRow ){
1928 payloadSize = 0;
drh9188b382004-05-14 21:12:22 +00001929 }else if( pC->cacheValid ){
1930 payloadSize = pC->payloadSize;
danielk1977192ac1d2004-05-10 07:17:30 +00001931 }else if( pC->keyAsData ){
danielk197796fc5fe2004-05-13 11:34:16 +00001932 i64 payloadSize64;
danielk1977192ac1d2004-05-10 07:17:30 +00001933 sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1934 payloadSize = payloadSize64;
1935 }else{
1936 sqlite3BtreeDataSize(pCrsr, &payloadSize);
1937 }
1938 }else if( pC->pseudoTable ){
1939 payloadSize = pC->nData;
1940 zRec = pC->pData;
drh9188b382004-05-14 21:12:22 +00001941 pC->cacheValid = 0;
danielk1977192ac1d2004-05-10 07:17:30 +00001942 assert( payloadSize==0 || zRec!=0 );
1943 }else{
1944 payloadSize = 0;
1945 }
1946
1947 /* If payloadSize is 0, then just push a NULL onto the stack. */
1948 if( payloadSize==0 ){
1949 pTos->flags = MEM_Null;
1950 break;
1951 }
1952
drh9188b382004-05-14 21:12:22 +00001953 /* Read and parse the table header. Store the results of the parse
1954 ** into the record header cache fields of the cursor.
danielk1977192ac1d2004-05-10 07:17:30 +00001955 */
drh9188b382004-05-14 21:12:22 +00001956 if( !pC->cacheValid ){
1957 pC->payloadSize = payloadSize;
1958 if( zRec ){
1959 zData = zRec;
danielk1977192ac1d2004-05-10 07:17:30 +00001960 }else{
drh9188b382004-05-14 21:12:22 +00001961 /* We can assume that 9 bytes (maximum length of a varint) fits
1962 ** on the main page in all cases.
danielk1977cfcdaef2004-05-12 07:33:33 +00001963 */
drh9188b382004-05-14 21:12:22 +00001964 int n = 9;
1965 if( payloadSize<9 ) n = payloadSize;
danielk1977cfcdaef2004-05-12 07:33:33 +00001966 if( pC->keyAsData ){
drh9188b382004-05-14 21:12:22 +00001967 zData = (char *)sqlite3BtreeKeyFetch(pCrsr, n);
danielk1977cfcdaef2004-05-12 07:33:33 +00001968 }else{
drh9188b382004-05-14 21:12:22 +00001969 zData = (char *)sqlite3BtreeDataFetch(pCrsr, n);
danielk1977cfcdaef2004-05-12 07:33:33 +00001970 }
drh9188b382004-05-14 21:12:22 +00001971 assert( zData );
danielk1977192ac1d2004-05-10 07:17:30 +00001972 }
drh9188b382004-05-14 21:12:22 +00001973 offset = sqlite3GetVarint(zData, &nField);
1974 if( nField>pC->nField ){
1975 sqliteFree(pC->aType);
1976 pC->aType = sqliteMallocRaw( nField*sizeof(pC->aType[0]) );
1977 if( pC->aType==0 ){
1978 goto no_mem;
1979 }
1980 }
1981 pC->nField = nField;
danielk1977192ac1d2004-05-10 07:17:30 +00001982
drh9188b382004-05-14 21:12:22 +00001983 if( !zRec ){
1984 /* If the record is stored in a table, see if enough of it is on
1985 ** the main page to use sqlite3BtreeDataFetch() to get the data
1986 ** containing the nField serial types (varints). This will almost
1987 ** always work, but if it doesn't sqliteMalloc() space and use
1988 ** sqlite3BtreeData().
1989 **
1990 ** Estimate the maximum space required by the nField varints by
1991 ** assuming the maximum space for each is the length required to store:
1992 **
1993 ** (<record length> * 2) + 13
1994 **
1995 ** This is the serial-type for a text object as long as the record
1996 ** itself. In all cases the length required to store this is three
1997 ** bytes or less.
1998 */
1999 int max_space = sqlite3VarintLen((((u64)payloadSize)<<1)+13)*nField;
2000 max_space += offset;
2001 if( max_space>payloadSize ){
2002 max_space = payloadSize;
2003 }
2004
2005 if( pC->keyAsData ){
2006 zData = (char *)sqlite3BtreeKeyFetch(pCrsr, max_space);
2007 }else{
2008 zData = (char *)sqlite3BtreeDataFetch(pCrsr, max_space);
2009 }
2010 if( !zData ){
2011 /* This code will run very infrequently (e.g. tables with several
2012 ** hundred columns).
2013 */
2014 zData = (char *)sqliteMallocRaw(max_space);
2015 if( !zData ){
2016 goto no_mem;
2017 }
2018 if( pC->keyAsData ){
2019 rc = sqlite3BtreeKey(pCrsr, 0, max_space, zData);
2020 }else{
2021 rc = sqlite3BtreeData(pCrsr, 0, max_space, zData);
2022 }
2023 if( rc!=SQLITE_OK ){
2024 sqliteFree(zData);
2025 goto abort_due_to_error;
2026 }
2027 freeZdata = 1;
2028 }
2029 }
2030
2031 /* Read all the serial types for the record. At the end of this block
2032 ** variable offset is set to the offset to the start of Data0 in the record.
2033 */
2034 for(nn=0; nn<nField; nn++){
2035 offset += sqlite3GetVarint(&zData[offset], &pC->aType[nn]);
2036 }
danielk1977cfcdaef2004-05-12 07:33:33 +00002037 if( freeZdata ){
danielk1977cfcdaef2004-05-12 07:33:33 +00002038 freeZdata = 0;
drh9188b382004-05-14 21:12:22 +00002039 sqliteFree(zData);
danielk1977192ac1d2004-05-10 07:17:30 +00002040 }
drh9188b382004-05-14 21:12:22 +00002041 pC->nHeader = offset;
2042 pC->cacheValid = 1;
danielk1977cfcdaef2004-05-12 07:33:33 +00002043 }
danielk1977192ac1d2004-05-10 07:17:30 +00002044
drh9188b382004-05-14 21:12:22 +00002045 /* Compute the offset from the beginning of the record to the beginning
2046 ** of the data. And get the length of the data.
2047 */
2048 offset = pC->nHeader;
danielk1977cfcdaef2004-05-12 07:33:33 +00002049 for(nn=0; nn<p2; nn++){
drh9188b382004-05-14 21:12:22 +00002050 offset += sqlite3VdbeSerialTypeLen(pC->aType[nn]);
danielk1977cfcdaef2004-05-12 07:33:33 +00002051 }
drh9188b382004-05-14 21:12:22 +00002052 len = sqlite3VdbeSerialTypeLen(pC->aType[p2]);
danielk1977cfcdaef2004-05-12 07:33:33 +00002053
2054 if( !zRec ){
2055 /* If the record is stored in a table, see if enough of it
2056 ** is on the main page to read our column using
2057 ** sqlite3BtreeDataFetch(). If not sqliteMalloc() space and read data
2058 ** with sqlite3BtreeData().
2059 */
2060 if( pC->keyAsData ){
2061 zData = (char *)sqlite3BtreeKeyFetch(pCrsr, offset+len);
2062 }else{
2063 zData = (char *)sqlite3BtreeDataFetch(pCrsr, offset+len);
2064 }
drh9188b382004-05-14 21:12:22 +00002065 if( !zData ){
drh10617cd2004-05-14 15:27:27 +00002066 zData = (char *)sqliteMallocRaw(len);
danielk1977cfcdaef2004-05-12 07:33:33 +00002067 if( !zData ){
drh9188b382004-05-14 21:12:22 +00002068 goto no_mem;
danielk1977cfcdaef2004-05-12 07:33:33 +00002069 }
2070 if( pC->keyAsData ){
2071 rc = sqlite3BtreeKey(pCrsr, offset, len, zData);
2072 }else{
2073 rc = sqlite3BtreeData(pCrsr, offset, len, zData);
2074 }
2075 if( rc!=SQLITE_OK ){
danielk1977cfcdaef2004-05-12 07:33:33 +00002076 sqliteFree( zData );
2077 goto abort_due_to_error;
2078 }
2079 freeZdata = 1;
2080 offset = 0;
2081 }
2082 }
2083
2084 /* Deserialize the value directly into the top of the stack */
drh9188b382004-05-14 21:12:22 +00002085 sqlite3VdbeSerialGet(&zData[offset], pC->aType[p2], pTos);
danielk1977cfcdaef2004-05-12 07:33:33 +00002086
danielk1977cfcdaef2004-05-12 07:33:33 +00002087 if( freeZdata ){
2088 sqliteFree(zData);
danielk1977192ac1d2004-05-10 07:17:30 +00002089 }
2090 break;
2091}
2092
danielk19773d1bfea2004-05-14 11:00:53 +00002093/* Opcode MakeRecord P1 * P3
danielk1977192ac1d2004-05-10 07:17:30 +00002094**
2095** This opcode (not yet in use) is a replacement for the current
2096** OP_MakeRecord that supports the SQLite3 manifest typing feature.
2097** It drops the (P2==1) option that was never use.
2098**
2099** Convert the top P1 entries of the stack into a single entry
2100** suitable for use as a data record in a database table. The
2101** details of the format are irrelavant as long as the OP_Column
2102** opcode can decode the record later. Refer to source code
2103** comments for the details of the record format.
danielk19773d1bfea2004-05-14 11:00:53 +00002104**
2105** P3 may be a string that is P1 characters long. The nth character of the
2106** string indicates the column affinity that should be used for the nth
2107** field of the index key (i.e. the first character of P3 corresponds to the
2108** lowest element on the stack).
2109**
2110** Character Column affinity
2111** ------------------------------
2112** 'n' NUMERIC
2113** 'i' INTEGER
2114** 't' TEXT
2115** 'o' NONE
2116**
2117** If P3 is NULL then all index fields have the affinity NONE.
danielk1977192ac1d2004-05-10 07:17:30 +00002118*/
danielk1977cfcdaef2004-05-12 07:33:33 +00002119case OP_MakeRecord: {
danielk1977192ac1d2004-05-10 07:17:30 +00002120 /* Assuming the record contains N fields, the record format looks
2121 ** like this:
2122 **
2123 ** --------------------------------------------------------------------------
danielk1977cfcdaef2004-05-12 07:33:33 +00002124 ** | num-fields | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
danielk1977192ac1d2004-05-10 07:17:30 +00002125 ** --------------------------------------------------------------------------
2126 **
2127 ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2128 ** the top of the stack.
2129 **
danielk1977cfcdaef2004-05-12 07:33:33 +00002130 ** Each type field is a varint representing the serial type of the
2131 ** corresponding data element (see sqlite3VdbeSerialType()). The
2132 ** num-fields field is also a varint storing N.
danielk1977192ac1d2004-05-10 07:17:30 +00002133 **
danielk1977192ac1d2004-05-10 07:17:30 +00002134 ** TODO: Even when the record is short enough for Mem::zShort, this opcode
2135 ** allocates it dynamically.
2136 */
danielk1977192ac1d2004-05-10 07:17:30 +00002137 int nField = pOp->p1;
2138 unsigned char *zNewRecord;
danielk1977cfcdaef2004-05-12 07:33:33 +00002139 unsigned char *zCsr;
danielk19773d1bfea2004-05-14 11:00:53 +00002140 char *zAffinity;
danielk1977192ac1d2004-05-10 07:17:30 +00002141 Mem *pRec;
danielk1977cfcdaef2004-05-12 07:33:33 +00002142 int nBytes; /* Space required for this record */
danielk1977192ac1d2004-05-10 07:17:30 +00002143
2144 Mem *pData0 = &pTos[1-nField];
2145 assert( pData0>=p->aStack );
danielk19773d1bfea2004-05-14 11:00:53 +00002146 zAffinity = pOp->p3;
danielk1977192ac1d2004-05-10 07:17:30 +00002147
danielk1977cfcdaef2004-05-12 07:33:33 +00002148 /* Loop through the elements that will make up the record to figure
2149 ** out how much space is required for the new record.
danielk1977192ac1d2004-05-10 07:17:30 +00002150 */
danielk1977cfcdaef2004-05-12 07:33:33 +00002151 nBytes = sqlite3VarintLen(nField);
2152 for(pRec=pData0; pRec<=pTos; pRec++){
danielk19773d1bfea2004-05-14 11:00:53 +00002153 u64 serial_type;
2154 if( zAffinity ){
2155 applyAffinityByChar(pRec, zAffinity[pRec-pData0]);
2156 }
2157 serial_type = sqlite3VdbeSerialType(pRec);
danielk1977cfcdaef2004-05-12 07:33:33 +00002158 nBytes += sqlite3VdbeSerialTypeLen(serial_type);
2159 nBytes += sqlite3VarintLen(serial_type);
danielk1977192ac1d2004-05-10 07:17:30 +00002160 }
2161
danielk197796fc5fe2004-05-13 11:34:16 +00002162 if( nBytes>MAX_BYTES_PER_ROW ){
2163 rc = SQLITE_TOOBIG;
2164 goto abort_due_to_error;
2165 }
2166
danielk1977cfcdaef2004-05-12 07:33:33 +00002167 /* Allocate space for the new record. */
drh10617cd2004-05-14 15:27:27 +00002168 zNewRecord = sqliteMallocRaw(nBytes);
danielk1977192ac1d2004-05-10 07:17:30 +00002169 if( !zNewRecord ){
2170 rc = SQLITE_NOMEM;
2171 goto abort_due_to_error;
2172 }
2173
danielk1977cfcdaef2004-05-12 07:33:33 +00002174 /* Write the record */
2175 zCsr = zNewRecord;
2176 zCsr += sqlite3PutVarint(zCsr, nField); /* number of fields */
2177 for(pRec=pData0; pRec<=pTos; pRec++){
2178 u64 serial_type = sqlite3VdbeSerialType(pRec);
2179 zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */
2180 }
2181 for(pRec=pData0; pRec<=pTos; pRec++){
2182 zCsr += sqlite3VdbeSerialPut(zCsr, pRec); /* serial data */
2183 }
danielk1977192ac1d2004-05-10 07:17:30 +00002184
danielk1977cfcdaef2004-05-12 07:33:33 +00002185 /* If zCsr has not been advanced exactly nBytes bytes, then one
2186 ** of the sqlite3PutVarint() or sqlite3VdbeSerialPut() calls above
2187 ** failed. This indicates a corrupted memory cell or code bug.
danielk1977192ac1d2004-05-10 07:17:30 +00002188 */
danielk1977cfcdaef2004-05-12 07:33:33 +00002189 if( zCsr!=(zNewRecord+nBytes) ){
2190 rc = SQLITE_INTERNAL;
2191 goto abort_due_to_error;
danielk1977192ac1d2004-05-10 07:17:30 +00002192 }
2193
2194 /* Pop nField entries from the stack and push the new entry on */
2195 popStack(&pTos, nField);
2196 pTos++;
danielk1977cfcdaef2004-05-12 07:33:33 +00002197 pTos->n = nBytes;
danielk1977192ac1d2004-05-10 07:17:30 +00002198 pTos->z = zNewRecord;
2199 pTos->flags = MEM_Str | MEM_Dyn;
2200
2201 break;
2202}
2203
drhc9b84a12002-06-20 11:36:48 +00002204/* Opcode: MakeKey P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00002205**
2206** Convert the top P1 entries of the stack into a single entry suitable
danielk19773d1bfea2004-05-14 11:00:53 +00002207** for use as the key in an index. If P2 is not zero, then the original
2208** entries are popped off the stack. If P2 is zero, the original entries
2209** remain on the stack.
2210**
2211** P3 is interpreted in the same way as for MakeIdxKey.
2212*/
2213/* Opcode: MakeIdxKey P1 P2 P3
danielk19778d059842004-05-12 11:24:02 +00002214**
2215** Convert the top P1 entries of the stack into a single entry suitable
2216** for use as the key in an index. In addition, take one additional integer
2217** off of the stack, treat that integer as an eight-byte record number, and
2218** append the integer to the key as a varint. Thus a total of P1+1 entries
2219** are popped from the stack for this instruction and a single entry is
danielk19773d1bfea2004-05-14 11:00:53 +00002220** pushed back.
2221**
2222** If P2 is not zero and one or more of the P1 entries that go into the
2223** generated key is NULL, then jump to P2 after the new key has been
2224** pushed on the stack. In other words, jump to P2 if the key is
2225** guaranteed to be unique. This jump can be used to skip a subsequent
2226** uniqueness test.
2227**
2228** P3 may be a string that is P1 characters long. The nth character of the
2229** string indicates the column affinity that should be used for the nth
2230** field of the index key (i.e. the first character of P3 corresponds to the
2231** lowest element on the stack).
2232**
2233** Character Column affinity
2234** ------------------------------
2235** 'n' NUMERIC
2236** 'i' INTEGER
2237** 't' TEXT
2238** 'o' NONE
2239**
2240** If P3 is NULL then all index fields have the affinity NUMERIC.
danielk19778d059842004-05-12 11:24:02 +00002241*/
danielk1977452c9892004-05-13 05:16:15 +00002242case OP_MakeKey:
2243case OP_MakeIdxKey: {
danielk19778d059842004-05-12 11:24:02 +00002244 Mem *pRec;
2245 Mem *pData0;
2246 int nField;
2247 u64 rowid;
2248 int nByte = 0;
2249 int addRowid;
danielk1977452c9892004-05-13 05:16:15 +00002250 int containsNull = 0;
danielk19778d059842004-05-12 11:24:02 +00002251 char *zKey; /* The new key */
danielk1977452c9892004-05-13 05:16:15 +00002252 int offset = 0;
danielk19773d1bfea2004-05-14 11:00:53 +00002253 char *zAffinity = pOp->p3;
danielk19778d059842004-05-12 11:24:02 +00002254
danielk19773d1bfea2004-05-14 11:00:53 +00002255 assert( zAffinity );
danielk19778d059842004-05-12 11:24:02 +00002256 nField = pOp->p1;
2257 pData0 = &pTos[1-nField];
2258 assert( pData0>=p->aStack );
2259
danielk1977452c9892004-05-13 05:16:15 +00002260 addRowid = ((pOp->opcode==OP_MakeIdxKey)?1:0);
danielk19778d059842004-05-12 11:24:02 +00002261
danielk19773d1bfea2004-05-14 11:00:53 +00002262 /* Loop through the P1 elements that will make up the new index
2263 ** key. Call applyAffinity() to perform any conversion required
2264 ** the column affinity string P3 to modify stack elements in place.
2265 ** Set containsNull to 1 if a NULL value is encountered.
2266 **
2267 ** Once the value has been coerced, figure out how much space is required
2268 ** to store the coerced values serial-type and blob, and add this
2269 ** quantity to nByte.
2270 **
2271 ** TODO: Figure out if the in-place coercion causes a problem for
2272 ** OP_MakeKey when P2 is 0 (used by DISTINCT).
danielk19778d059842004-05-12 11:24:02 +00002273 */
2274 for(pRec=pData0; pRec<=pTos; pRec++){
danielk19773d1bfea2004-05-14 11:00:53 +00002275 u64 serial_type;
2276 if( zAffinity ){
2277 applyAffinityByChar(pRec, zAffinity[pRec-pData0]);
2278 }else{
2279 applyAffinity(pRec, SQLITE_SO_NUM);
2280 }
2281 if( pRec->flags&MEM_Null ){
danielk1977452c9892004-05-13 05:16:15 +00002282 containsNull = 1;
2283 }
danielk19773d1bfea2004-05-14 11:00:53 +00002284 serial_type = sqlite3VdbeSerialType(pRec);
danielk19778d059842004-05-12 11:24:02 +00002285 nByte += sqlite3VarintLen(serial_type);
2286 nByte += sqlite3VdbeSerialTypeLen(serial_type);
2287 }
danielk19773d1bfea2004-05-14 11:00:53 +00002288
2289 /* If we have to append a varint rowid to this record, set 'rowid'
2290 ** to the value of the rowid and increase nByte by the amount of space
2291 ** required to store it and the 0x00 seperator byte.
2292 */
danielk19778d059842004-05-12 11:24:02 +00002293 if( addRowid ){
danielk1977452c9892004-05-13 05:16:15 +00002294 pRec = &pTos[0-nField];
danielk19778d059842004-05-12 11:24:02 +00002295 assert( pRec>=p->aStack );
2296 Integerify(pRec);
2297 rowid = pRec->i;
2298 nByte += sqlite3VarintLen(rowid);
danielk1977452c9892004-05-13 05:16:15 +00002299 nByte++;
danielk19778d059842004-05-12 11:24:02 +00002300 }
danielk197796fc5fe2004-05-13 11:34:16 +00002301
2302 if( nByte>MAX_BYTES_PER_ROW ){
2303 rc = SQLITE_TOOBIG;
2304 goto abort_due_to_error;
2305 }
danielk19778d059842004-05-12 11:24:02 +00002306
2307 /* Allocate space for the new key */
drh10617cd2004-05-14 15:27:27 +00002308 zKey = (char *)sqliteMallocRaw(nByte);
danielk19778d059842004-05-12 11:24:02 +00002309 if( !zKey ){
2310 rc = SQLITE_NOMEM;
2311 goto abort_due_to_error;
2312 }
2313
2314 /* Build the key in the buffer pointed to by zKey. */
2315 for(pRec=pData0; pRec<=pTos; pRec++){
danielk1977452c9892004-05-13 05:16:15 +00002316 offset += sqlite3PutVarint(&zKey[offset], sqlite3VdbeSerialType(pRec));
2317 offset += sqlite3VdbeSerialPut(&zKey[offset], pRec);
danielk19778d059842004-05-12 11:24:02 +00002318 }
2319 if( addRowid ){
danielk1977452c9892004-05-13 05:16:15 +00002320 zKey[offset++] = '\0';
2321 offset += sqlite3PutVarint(&zKey[offset], rowid);
danielk19778d059842004-05-12 11:24:02 +00002322 }
danielk1977452c9892004-05-13 05:16:15 +00002323 assert( offset==nByte );
danielk19778d059842004-05-12 11:24:02 +00002324
2325 /* Pop the consumed values off the stack and push on the new key. */
danielk1977452c9892004-05-13 05:16:15 +00002326 if( addRowid||(pOp->p2==0) ){
2327 popStack(&pTos, nField+addRowid);
2328 }
danielk19778d059842004-05-12 11:24:02 +00002329 pTos++;
danielk1977452c9892004-05-13 05:16:15 +00002330 pTos->flags = MEM_Str|MEM_Dyn; /* TODO: should eventually be MEM_Blob */
danielk19778d059842004-05-12 11:24:02 +00002331 pTos->z = zKey;
2332 pTos->n = nByte;
2333
danielk19773d1bfea2004-05-14 11:00:53 +00002334 /* If P2 is non-zero, and if the key contains a NULL value, and if this
2335 ** was an OP_MakeIdxKey instruction, not OP_MakeKey, jump to P2.
2336 */
2337 if( pOp->p2 && containsNull && addRowid ){
danielk1977452c9892004-05-13 05:16:15 +00002338 pc = pOp->p2 - 1;
2339 }
danielk19778d059842004-05-12 11:24:02 +00002340 break;
2341}
2342
drh001bbcb2003-03-19 03:14:00 +00002343/* Opcode: Checkpoint P1 * *
drh663fc632002-02-02 18:49:19 +00002344**
2345** Begin a checkpoint. A checkpoint is the beginning of a operation that
2346** is part of a larger transaction but which might need to be rolled back
2347** itself without effecting the containing transaction. A checkpoint will
2348** be automatically committed or rollback when the VDBE halts.
drh001bbcb2003-03-19 03:14:00 +00002349**
2350** The checkpoint is begun on the database file with index P1. The main
2351** database file has an index of 0 and the file used for temporary tables
2352** has an index of 1.
drh663fc632002-02-02 18:49:19 +00002353*/
2354case OP_Checkpoint: {
drh001bbcb2003-03-19 03:14:00 +00002355 int i = pOp->p1;
drh1aa49652003-06-02 23:14:13 +00002356 if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){
danielk19774adee202004-05-08 08:23:19 +00002357 rc = sqlite3BtreeBeginStmt(db->aDb[i].pBt);
drh1aa49652003-06-02 23:14:13 +00002358 if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2;
drh663fc632002-02-02 18:49:19 +00002359 }
2360 break;
2361}
2362
drhcabb0812002-09-14 13:47:32 +00002363/* Opcode: Transaction P1 * *
drh5e00f6c2001-09-13 13:46:56 +00002364**
2365** Begin a transaction. The transaction ends when a Commit or Rollback
drh663fc632002-02-02 18:49:19 +00002366** opcode is encountered. Depending on the ON CONFLICT setting, the
2367** transaction might also be rolled back if an error is encountered.
drh5e00f6c2001-09-13 13:46:56 +00002368**
drh001bbcb2003-03-19 03:14:00 +00002369** P1 is the index of the database file on which the transaction is
2370** started. Index 0 is the main database file and index 1 is the
2371** file used for temporary tables.
drhcabb0812002-09-14 13:47:32 +00002372**
drhb19a2bc2001-09-16 00:13:26 +00002373** A write lock is obtained on the database file when a transaction is
2374** started. No other process can read or write the file while the
2375** transaction is underway. Starting a transaction also creates a
drh663fc632002-02-02 18:49:19 +00002376** rollback journal. A transaction must be started before any changes
2377** can be made to the database.
drh5e00f6c2001-09-13 13:46:56 +00002378*/
2379case OP_Transaction: {
drhb86ccfb2003-01-28 23:13:10 +00002380 int busy = 1;
drh001bbcb2003-03-19 03:14:00 +00002381 int i = pOp->p1;
drh8bf8dc92003-05-17 17:35:10 +00002382 assert( i>=0 && i<db->nDb );
2383 if( db->aDb[i].inTrans ) break;
2384 while( db->aDb[i].pBt!=0 && busy ){
danielk19774adee202004-05-08 08:23:19 +00002385 rc = sqlite3BtreeBeginTrans(db->aDb[i].pBt);
drh90bfcda2001-09-23 19:46:51 +00002386 switch( rc ){
2387 case SQLITE_BUSY: {
drhb86ccfb2003-01-28 23:13:10 +00002388 if( db->xBusyCallback==0 ){
2389 p->pc = pc;
2390 p->undoTransOnError = 1;
2391 p->rc = SQLITE_BUSY;
drh6810ce62004-01-31 19:22:56 +00002392 p->pTos = pTos;
drhb86ccfb2003-01-28 23:13:10 +00002393 return SQLITE_BUSY;
2394 }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00002395 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh90bfcda2001-09-23 19:46:51 +00002396 busy = 0;
2397 }
2398 break;
2399 }
drhf74b8d92002-09-01 23:20:45 +00002400 case SQLITE_READONLY: {
2401 rc = SQLITE_OK;
2402 /* Fall thru into the next case */
2403 }
drh90bfcda2001-09-23 19:46:51 +00002404 case SQLITE_OK: {
drhb86ccfb2003-01-28 23:13:10 +00002405 p->inTempTrans = 0;
drh90bfcda2001-09-23 19:46:51 +00002406 busy = 0;
2407 break;
2408 }
2409 default: {
2410 goto abort_due_to_error;
2411 }
2412 }
drhb86ccfb2003-01-28 23:13:10 +00002413 }
drh001bbcb2003-03-19 03:14:00 +00002414 db->aDb[i].inTrans = 1;
drhb86ccfb2003-01-28 23:13:10 +00002415 p->undoTransOnError = 1;
drh5e00f6c2001-09-13 13:46:56 +00002416 break;
2417}
2418
2419/* Opcode: Commit * * *
2420**
2421** Cause all modifications to the database that have been made since the
2422** last Transaction to actually take effect. No additional modifications
drhb19a2bc2001-09-16 00:13:26 +00002423** are allowed until another transaction is started. The Commit instruction
2424** deletes the journal file and releases the write lock on the database.
2425** A read lock continues to be held if there are still cursors open.
drh5e00f6c2001-09-13 13:46:56 +00002426*/
2427case OP_Commit: {
drh001bbcb2003-03-19 03:14:00 +00002428 int i;
drhaa940ea2004-01-15 02:44:03 +00002429 if( db->xCommitCallback!=0 ){
danielk19774adee202004-05-08 08:23:19 +00002430 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
drhaa940ea2004-01-15 02:44:03 +00002431 if( db->xCommitCallback(db->pCommitArg)!=0 ){
2432 rc = SQLITE_CONSTRAINT;
2433 }
danielk19774adee202004-05-08 08:23:19 +00002434 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drhaa940ea2004-01-15 02:44:03 +00002435 }
drh001bbcb2003-03-19 03:14:00 +00002436 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2437 if( db->aDb[i].inTrans ){
danielk19774adee202004-05-08 08:23:19 +00002438 rc = sqlite3BtreeCommit(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +00002439 db->aDb[i].inTrans = 0;
2440 }
drhf57b3392001-10-08 13:22:32 +00002441 }
drh5e00f6c2001-09-13 13:46:56 +00002442 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002443 sqlite3CommitInternalChanges(db);
drh5e00f6c2001-09-13 13:46:56 +00002444 }else{
danielk19774adee202004-05-08 08:23:19 +00002445 sqlite3RollbackAll(db);
drh5e00f6c2001-09-13 13:46:56 +00002446 }
2447 break;
2448}
2449
drh001bbcb2003-03-19 03:14:00 +00002450/* Opcode: Rollback P1 * *
drh5e00f6c2001-09-13 13:46:56 +00002451**
2452** Cause all modifications to the database that have been made since the
2453** last Transaction to be undone. The database is restored to its state
2454** before the Transaction opcode was executed. No additional modifications
2455** are allowed until another transaction is started.
drhb19a2bc2001-09-16 00:13:26 +00002456**
drh001bbcb2003-03-19 03:14:00 +00002457** P1 is the index of the database file that is committed. An index of 0
2458** is used for the main database and an index of 1 is used for the file used
2459** to hold temporary tables.
2460**
drhb19a2bc2001-09-16 00:13:26 +00002461** This instruction automatically closes all cursors and releases both
drh001bbcb2003-03-19 03:14:00 +00002462** the read and write locks on the indicated database.
drh5e00f6c2001-09-13 13:46:56 +00002463*/
2464case OP_Rollback: {
danielk19774adee202004-05-08 08:23:19 +00002465 sqlite3RollbackAll(db);
drh5e00f6c2001-09-13 13:46:56 +00002466 break;
2467}
2468
drh001bbcb2003-03-19 03:14:00 +00002469/* Opcode: ReadCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002470**
drh001bbcb2003-03-19 03:14:00 +00002471** Read cookie number P2 from database P1 and push it onto the stack.
2472** P2==0 is the schema version. P2==1 is the database format.
2473** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2474** the main database file and P1==1 is the database file used to store
2475** temporary tables.
drh4a324312001-12-21 14:30:42 +00002476**
drh50e5dad2001-09-15 00:57:28 +00002477** There must be a read-lock on the database (either a transaction
drhb19a2bc2001-09-16 00:13:26 +00002478** must be started or there must be an open cursor) before
drh50e5dad2001-09-15 00:57:28 +00002479** executing this instruction.
2480*/
2481case OP_ReadCookie: {
drhf328bc82004-05-10 23:29:49 +00002482 int iMeta;
drh4a324312001-12-21 14:30:42 +00002483 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002484 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2485 assert( db->aDb[pOp->p1].pBt!=0 );
drha3b321d2004-05-11 09:31:31 +00002486 /* The indexing of meta values at the schema layer is off by one from
2487 ** the indexing in the btree layer. The btree considers meta[0] to
2488 ** be the number of free pages in the database (a read-only value)
2489 ** and meta[1] to be the schema cookie. The schema layer considers
2490 ** meta[1] to be the schema cookie. So we have to shift the index
2491 ** by one in the following statement.
2492 */
2493 rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, &iMeta);
drh6810ce62004-01-31 19:22:56 +00002494 pTos++;
drhf328bc82004-05-10 23:29:49 +00002495 pTos->i = iMeta;
drh6810ce62004-01-31 19:22:56 +00002496 pTos->flags = MEM_Int;
drh50e5dad2001-09-15 00:57:28 +00002497 break;
2498}
2499
drh001bbcb2003-03-19 03:14:00 +00002500/* Opcode: SetCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002501**
drh001bbcb2003-03-19 03:14:00 +00002502** Write the top of the stack into cookie number P2 of database P1.
2503** P2==0 is the schema version. P2==1 is the database format.
2504** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2505** the main database file and P1==1 is the database file used to store
2506** temporary tables.
drh50e5dad2001-09-15 00:57:28 +00002507**
2508** A transaction must be started before executing this opcode.
2509*/
2510case OP_SetCookie: {
drh4a324312001-12-21 14:30:42 +00002511 assert( pOp->p2<SQLITE_N_BTREE_META );
drh001bbcb2003-03-19 03:14:00 +00002512 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2513 assert( db->aDb[pOp->p1].pBt!=0 );
drh6810ce62004-01-31 19:22:56 +00002514 assert( pTos>=p->aStack );
drhf328bc82004-05-10 23:29:49 +00002515 Integerify(pTos);
drha3b321d2004-05-11 09:31:31 +00002516 /* See note about index shifting on OP_ReadCookie */
drhf328bc82004-05-10 23:29:49 +00002517 rc = sqlite3BtreeUpdateMeta(db->aDb[pOp->p1].pBt, 1+pOp->p2, (int)pTos->i);
drh79f14b72004-03-03 01:51:24 +00002518 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00002519 pTos--;
drh50e5dad2001-09-15 00:57:28 +00002520 break;
2521}
2522
drh4a324312001-12-21 14:30:42 +00002523/* Opcode: VerifyCookie P1 P2 *
drh50e5dad2001-09-15 00:57:28 +00002524**
drh001bbcb2003-03-19 03:14:00 +00002525** Check the value of global database parameter number 0 (the
2526** schema version) and make sure it is equal to P2.
2527** P1 is the database number which is 0 for the main database file
2528** and 1 for the file holding temporary tables and some higher number
2529** for auxiliary databases.
drh50e5dad2001-09-15 00:57:28 +00002530**
2531** The cookie changes its value whenever the database schema changes.
drhb19a2bc2001-09-16 00:13:26 +00002532** This operation is used to detect when that the cookie has changed
drh50e5dad2001-09-15 00:57:28 +00002533** and that the current process needs to reread the schema.
2534**
2535** Either a transaction needs to have been started or an OP_Open needs
2536** to be executed (to establish a read lock) before this opcode is
2537** invoked.
2538*/
2539case OP_VerifyCookie: {
drhf328bc82004-05-10 23:29:49 +00002540 int iMeta;
drh001bbcb2003-03-19 03:14:00 +00002541 assert( pOp->p1>=0 && pOp->p1<db->nDb );
drhf328bc82004-05-10 23:29:49 +00002542 rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1, &iMeta);
2543 if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
danielk19774adee202004-05-08 08:23:19 +00002544 sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
drh50e5dad2001-09-15 00:57:28 +00002545 rc = SQLITE_SCHEMA;
2546 }
2547 break;
2548}
2549
drh001bbcb2003-03-19 03:14:00 +00002550/* Opcode: OpenRead P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00002551**
drhecdc7532001-09-23 02:35:53 +00002552** Open a read-only cursor for the database table whose root page is
drh001bbcb2003-03-19 03:14:00 +00002553** P2 in a database file. The database file is determined by an
2554** integer from the top of the stack. 0 means the main database and
2555** 1 means the database used for temporary tables. Give the new
2556** cursor an identifier of P1. The P1 values need not be contiguous
2557** but all P1 values should be small integers. It is an error for
2558** P1 to be negative.
drh5e00f6c2001-09-13 13:46:56 +00002559**
drh001bbcb2003-03-19 03:14:00 +00002560** If P2==0 then take the root page number from the next of the stack.
drh5edc3122001-09-13 21:53:09 +00002561**
drhb19a2bc2001-09-16 00:13:26 +00002562** There will be a read lock on the database whenever there is an
2563** open cursor. If the database was unlocked prior to this instruction
2564** then a read lock is acquired as part of this instruction. A read
2565** lock allows other processes to read the database but prohibits
2566** any other process from modifying the database. The read lock is
2567** released when all cursors are closed. If this instruction attempts
2568** to get a read lock but fails, the script terminates with an
2569** SQLITE_BUSY error code.
2570**
drh5e00f6c2001-09-13 13:46:56 +00002571** The P3 value is the name of the table or index being opened.
2572** The P3 value is not actually used by this opcode and may be
2573** omitted. But the code generator usually inserts the index or
2574** table name into P3 to make the code easier to read.
drhf57b3392001-10-08 13:22:32 +00002575**
drh001bbcb2003-03-19 03:14:00 +00002576** See also OpenWrite.
drh5e00f6c2001-09-13 13:46:56 +00002577*/
drhecdc7532001-09-23 02:35:53 +00002578/* Opcode: OpenWrite P1 P2 P3
2579**
2580** Open a read/write cursor named P1 on the table or index whose root
2581** page is P2. If P2==0 then take the root page number from the stack.
2582**
jplyon5a564222003-06-02 06:15:58 +00002583** The P3 value is the name of the table or index being opened.
2584** The P3 value is not actually used by this opcode and may be
2585** omitted. But the code generator usually inserts the index or
2586** table name into P3 to make the code easier to read.
2587**
drh001bbcb2003-03-19 03:14:00 +00002588** This instruction works just like OpenRead except that it opens the cursor
drhecdc7532001-09-23 02:35:53 +00002589** in read/write mode. For a given table, there can be one or more read-only
2590** cursors or a single read/write cursor but not both.
drhf57b3392001-10-08 13:22:32 +00002591**
drh001bbcb2003-03-19 03:14:00 +00002592** See also OpenRead.
drhecdc7532001-09-23 02:35:53 +00002593*/
drh001bbcb2003-03-19 03:14:00 +00002594case OP_OpenRead:
2595case OP_OpenWrite: {
drh5e00f6c2001-09-13 13:46:56 +00002596 int busy = 0;
2597 int i = pOp->p1;
drh5edc3122001-09-13 21:53:09 +00002598 int p2 = pOp->p2;
drhf57b3392001-10-08 13:22:32 +00002599 int wrFlag;
2600 Btree *pX;
drh001bbcb2003-03-19 03:14:00 +00002601 int iDb;
drhf328bc82004-05-10 23:29:49 +00002602 Cursor *pCur;
drh001bbcb2003-03-19 03:14:00 +00002603
drh6810ce62004-01-31 19:22:56 +00002604 assert( pTos>=p->aStack );
2605 Integerify(pTos);
2606 iDb = pTos->i;
2607 pTos--;
2608 assert( iDb>=0 && iDb<db->nDb );
drh001bbcb2003-03-19 03:14:00 +00002609 pX = db->aDb[iDb].pBt;
drh6810ce62004-01-31 19:22:56 +00002610 assert( pX!=0 );
drh001bbcb2003-03-19 03:14:00 +00002611 wrFlag = pOp->opcode==OP_OpenWrite;
drh5edc3122001-09-13 21:53:09 +00002612 if( p2<=0 ){
drh6810ce62004-01-31 19:22:56 +00002613 assert( pTos>=p->aStack );
2614 Integerify(pTos);
2615 p2 = pTos->i;
2616 pTos--;
drh5edc3122001-09-13 21:53:09 +00002617 if( p2<2 ){
danielk19774adee202004-05-08 08:23:19 +00002618 sqlite3SetString(&p->zErrMsg, "root page number less than 2", (char*)0);
drh5edc3122001-09-13 21:53:09 +00002619 rc = SQLITE_INTERNAL;
drhb86ccfb2003-01-28 23:13:10 +00002620 break;
drh5edc3122001-09-13 21:53:09 +00002621 }
2622 }
drh6810ce62004-01-31 19:22:56 +00002623 assert( i>=0 );
drh8c74a8c2002-08-25 19:20:40 +00002624 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002625 pCur = p->apCsr[i];
drhf328bc82004-05-10 23:29:49 +00002626 sqlite3VdbeCleanupCursor(pCur);
drhf328bc82004-05-10 23:29:49 +00002627 pCur->nullRow = 1;
drhe0bc4042002-06-25 01:09:11 +00002628 if( pX==0 ) break;
drhbe0072d2001-09-13 14:46:09 +00002629 do{
danielk1977452c9892004-05-13 05:16:15 +00002630 /* When opening cursors, always supply the comparison function
2631 ** sqlite3VdbeKeyCompare(). If the table being opened is of type
2632 ** INTKEY, the btree layer won't call the comparison function anyway.
2633 */
danielk19773d1bfea2004-05-14 11:00:53 +00002634 rc = sqlite3BtreeCursor(pX, p2, wrFlag, sqlite3VdbeKeyCompare, pCur,
danielk1977452c9892004-05-13 05:16:15 +00002635 &pCur->pCursor);
drh5e00f6c2001-09-13 13:46:56 +00002636 switch( rc ){
2637 case SQLITE_BUSY: {
drhb86ccfb2003-01-28 23:13:10 +00002638 if( db->xBusyCallback==0 ){
2639 p->pc = pc;
2640 p->rc = SQLITE_BUSY;
drh6810ce62004-01-31 19:22:56 +00002641 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
drhb86ccfb2003-01-28 23:13:10 +00002642 return SQLITE_BUSY;
2643 }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00002644 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh5e00f6c2001-09-13 13:46:56 +00002645 busy = 0;
2646 }
2647 break;
2648 }
2649 case SQLITE_OK: {
drhf328bc82004-05-10 23:29:49 +00002650 int flags = sqlite3BtreeFlags(pCur->pCursor);
2651 pCur->intKey = (flags & BTREE_INTKEY)!=0;
2652 pCur->zeroData = (flags & BTREE_ZERODATA)!=0;
drh5e00f6c2001-09-13 13:46:56 +00002653 busy = 0;
2654 break;
2655 }
drhf4dada72004-05-11 09:57:35 +00002656 case SQLITE_EMPTY: {
2657 rc = SQLITE_OK;
2658 busy = 0;
2659 break;
2660 }
drh5e00f6c2001-09-13 13:46:56 +00002661 default: {
2662 goto abort_due_to_error;
2663 }
2664 }
2665 }while( busy );
2666 break;
2667}
2668
drhc6b52df2002-01-04 03:09:29 +00002669/* Opcode: OpenTemp P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002670**
drh5fe2d8c2003-05-10 03:36:53 +00002671** Open a new cursor to a transient table.
2672** The transient cursor is always opened read/write even if
2673** the main database is read-only. The transient table is deleted
2674** automatically when the cursor is closed.
drhc6b52df2002-01-04 03:09:29 +00002675**
2676** The cursor points to a BTree table if P2==0 and to a BTree index
2677** if P2==1. A BTree table must have an integer key and can have arbitrary
2678** data. A BTree index has no data but can have an arbitrary key.
drhf57b3392001-10-08 13:22:32 +00002679**
2680** This opcode is used for tables that exist for the duration of a single
2681** SQL statement only. Tables created using CREATE TEMPORARY TABLE
drh5fe2d8c2003-05-10 03:36:53 +00002682** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the
drhf57b3392001-10-08 13:22:32 +00002683** context of this opcode means for the duration of a single SQL statement
2684** whereas "Temporary" in the context of CREATE TABLE means for the duration
2685** of the connection to the database. Same word; different meanings.
drh5e00f6c2001-09-13 13:46:56 +00002686*/
2687case OP_OpenTemp: {
drh5e00f6c2001-09-13 13:46:56 +00002688 int i = pOp->p1;
2689 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002690 assert( i>=0 );
drh8c74a8c2002-08-25 19:20:40 +00002691 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002692 pCx = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00002693 sqlite3VdbeCleanupCursor(pCx);
drh5e00f6c2001-09-13 13:46:56 +00002694 memset(pCx, 0, sizeof(*pCx));
drh17f71932002-02-21 12:01:27 +00002695 pCx->nullRow = 1;
danielk19774adee202004-05-08 08:23:19 +00002696 rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
paulb0208cc2003-04-13 18:26:49 +00002697
drh5e00f6c2001-09-13 13:46:56 +00002698 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002699 rc = sqlite3BtreeBeginTrans(pCx->pBt);
drh5e00f6c2001-09-13 13:46:56 +00002700 }
2701 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002702 /* If a transient index is required, create it by calling
2703 ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2704 ** opening it. If a transient table is required, just use the
danielk19770dbe72b2004-05-11 04:54:49 +00002705 ** automatically created table with root-page 1 (an INTKEY table).
danielk19774adee202004-05-08 08:23:19 +00002706 */
drhc6b52df2002-01-04 03:09:29 +00002707 if( pOp->p2 ){
2708 int pgno;
danielk19774adee202004-05-08 08:23:19 +00002709 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
drhc6b52df2002-01-04 03:09:29 +00002710 if( rc==SQLITE_OK ){
drhf328bc82004-05-10 23:29:49 +00002711 assert( pgno==MASTER_ROOT+1 );
danielk19774adee202004-05-08 08:23:19 +00002712 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, 0, 0, &pCx->pCursor);
drhc6b52df2002-01-04 03:09:29 +00002713 }
2714 }else{
drhf328bc82004-05-10 23:29:49 +00002715 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
danielk19770dbe72b2004-05-11 04:54:49 +00002716 pCx->intKey = 1;
drhc6b52df2002-01-04 03:09:29 +00002717 }
drh5e00f6c2001-09-13 13:46:56 +00002718 }
2719 break;
2720}
2721
drh70ce3f02003-04-15 19:22:22 +00002722/* Opcode: OpenPseudo P1 * *
2723**
2724** Open a new cursor that points to a fake table that contains a single
2725** row of data. Any attempt to write a second row of data causes the
2726** first row to be deleted. All data is deleted when the cursor is
2727** closed.
2728**
2729** A pseudo-table created by this opcode is useful for holding the
2730** NEW or OLD tables in a trigger.
2731*/
2732case OP_OpenPseudo: {
2733 int i = pOp->p1;
2734 Cursor *pCx;
drh6810ce62004-01-31 19:22:56 +00002735 assert( i>=0 );
drh70ce3f02003-04-15 19:22:22 +00002736 if( expandCursorArraySize(p, i) ) goto no_mem;
drhd7556d22004-05-14 21:59:40 +00002737 pCx = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00002738 sqlite3VdbeCleanupCursor(pCx);
drh70ce3f02003-04-15 19:22:22 +00002739 memset(pCx, 0, sizeof(*pCx));
2740 pCx->nullRow = 1;
2741 pCx->pseudoTable = 1;
2742 break;
2743}
2744
drh5e00f6c2001-09-13 13:46:56 +00002745/* Opcode: Close P1 * *
2746**
2747** Close a cursor previously opened as P1. If P1 is not
2748** currently open, this instruction is a no-op.
2749*/
2750case OP_Close: {
2751 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00002752 if( i>=0 && i<p->nCursor ){
drhd7556d22004-05-14 21:59:40 +00002753 sqlite3VdbeCleanupCursor(p->apCsr[i]);
drh5e00f6c2001-09-13 13:46:56 +00002754 }
2755 break;
2756}
2757
drh8721ce42001-11-07 14:22:00 +00002758/* Opcode: MoveTo P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00002759**
2760** Pop the top of the stack and use its value as a key. Reposition
2761** cursor P1 so that it points to an entry with a matching key. If
2762** the table contains no record with a matching key, then the cursor
drh8721ce42001-11-07 14:22:00 +00002763** is left pointing at the first record that is greater than the key.
2764** If there are no records greater than the key and P2 is not zero,
2765** then an immediate jump to P2 is made.
drhb19a2bc2001-09-16 00:13:26 +00002766**
danielk19773d1bfea2004-05-14 11:00:53 +00002767** If P3 is not NULL, then the cursor is left pointing at the first
2768** record that is greater than the key of which the key is not a prefix.
2769** This is the same effect that executing OP_IncrKey on the key value
2770** before OP_MoveTo used to have.
2771**
drhc045ec52002-12-04 20:01:06 +00002772** See also: Found, NotFound, Distinct, MoveLt
drh5e00f6c2001-09-13 13:46:56 +00002773*/
drhc045ec52002-12-04 20:01:06 +00002774/* Opcode: MoveLt P1 P2 *
2775**
2776** Pop the top of the stack and use its value as a key. Reposition
2777** cursor P1 so that it points to the entry with the largest key that is
2778** less than the key popped from the stack.
2779** If there are no records less than than the key and P2
2780** is not zero then an immediate jump to P2 is made.
2781**
danielk19773d1bfea2004-05-14 11:00:53 +00002782** If P3 is not NULL, and keys exist in the index of which the stack key
2783** is a prefix, leave the cursor pointing at the largest of these.
2784** This is the same effect that executing OP_IncrKey on the key value
2785** before OP_MoveLt used to have.
2786**
drhc045ec52002-12-04 20:01:06 +00002787** See also: MoveTo
2788*/
2789case OP_MoveLt:
drh5e00f6c2001-09-13 13:46:56 +00002790case OP_MoveTo: {
2791 int i = pOp->p1;
drh80ff32f2001-11-04 18:32:46 +00002792 Cursor *pC;
2793
drh6810ce62004-01-31 19:22:56 +00002794 assert( pTos>=p->aStack );
drh70ce3f02003-04-15 19:22:22 +00002795 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002796 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00002797 if( pC->pCursor!=0 ){
drhc045ec52002-12-04 20:01:06 +00002798 int res, oc;
drha11846b2004-01-07 18:52:56 +00002799 pC->nullRow = 0;
drhf328bc82004-05-10 23:29:49 +00002800 if( pC->intKey ){
2801 i64 iKey;
danielk19773d1bfea2004-05-14 11:00:53 +00002802 assert( !pOp->p3 );
danielk197796fc5fe2004-05-13 11:34:16 +00002803 Integerify(pTos);
drhf328bc82004-05-10 23:29:49 +00002804 iKey = intToKey(pTos->i);
drha11846b2004-01-07 18:52:56 +00002805 if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){
2806 pC->movetoTarget = iKey;
2807 pC->deferredMoveto = 1;
drh6810ce62004-01-31 19:22:56 +00002808 Release(pTos);
2809 pTos--;
drha11846b2004-01-07 18:52:56 +00002810 break;
2811 }
drhf328bc82004-05-10 23:29:49 +00002812 sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, &res);
drh6810ce62004-01-31 19:22:56 +00002813 pC->lastRecno = pTos->i;
drh8aff1012001-12-22 14:49:24 +00002814 pC->recnoIsValid = res==0;
drh5e00f6c2001-09-13 13:46:56 +00002815 }else{
danielk19773d1bfea2004-05-14 11:00:53 +00002816 if( pOp->p3 ){
2817 pC->incrKey = 1;
2818 }
drh6810ce62004-01-31 19:22:56 +00002819 Stringify(pTos);
danielk19774adee202004-05-08 08:23:19 +00002820 sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
danielk19773d1bfea2004-05-14 11:00:53 +00002821 pC->incrKey = 0;
drh80ff32f2001-11-04 18:32:46 +00002822 pC->recnoIsValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002823 }
drha11846b2004-01-07 18:52:56 +00002824 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00002825 pC->cacheValid = 0;
danielk19773d1bfea2004-05-14 11:00:53 +00002826 pC->incrKey = 0;
danielk19776f8a5032004-05-10 10:34:51 +00002827 sqlite3_search_count++;
drhc045ec52002-12-04 20:01:06 +00002828 oc = pOp->opcode;
2829 if( oc==OP_MoveTo && res<0 ){
danielk19774adee202004-05-08 08:23:19 +00002830 sqlite3BtreeNext(pC->pCursor, &res);
drh8721ce42001-11-07 14:22:00 +00002831 pC->recnoIsValid = 0;
2832 if( res && pOp->p2>0 ){
2833 pc = pOp->p2 - 1;
2834 }
drh1a844c32002-12-04 22:29:28 +00002835 }else if( oc==OP_MoveLt ){
2836 if( res>=0 ){
danielk19774adee202004-05-08 08:23:19 +00002837 sqlite3BtreePrevious(pC->pCursor, &res);
drh1a844c32002-12-04 22:29:28 +00002838 pC->recnoIsValid = 0;
2839 }else{
2840 /* res might be negative because the table is empty. Check to
2841 ** see if this is the case.
2842 */
drhf328bc82004-05-10 23:29:49 +00002843 res = sqlite3BtreeEof(pC->pCursor);
drh1a844c32002-12-04 22:29:28 +00002844 }
drhc045ec52002-12-04 20:01:06 +00002845 if( res && pOp->p2>0 ){
2846 pc = pOp->p2 - 1;
2847 }
drh8721ce42001-11-07 14:22:00 +00002848 }
drh5e00f6c2001-09-13 13:46:56 +00002849 }
drh6810ce62004-01-31 19:22:56 +00002850 Release(pTos);
2851 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002852 break;
2853}
2854
drh5e00f6c2001-09-13 13:46:56 +00002855/* Opcode: Distinct P1 P2 *
2856**
drh6b125452002-01-28 15:53:03 +00002857** Use the top of the stack as a string key. If a record with that key does
drhb19a2bc2001-09-16 00:13:26 +00002858** not exist in the table of cursor P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002859** does already exist, then fall thru. The cursor is left pointing
2860** at the record if it exists. The key is not popped from the stack.
drh5e00f6c2001-09-13 13:46:56 +00002861**
2862** This operation is similar to NotFound except that this operation
2863** does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002864**
drhd99f7062002-06-08 23:25:08 +00002865** See also: Found, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002866*/
2867/* Opcode: Found P1 P2 *
2868**
drh6b125452002-01-28 15:53:03 +00002869** Use the top of the stack as a string key. If a record with that key
drhb19a2bc2001-09-16 00:13:26 +00002870** does exist in table of P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002871** does not exist, then fall thru. The cursor is left pointing
2872** to the record if it exists. The key is popped from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002873**
drhd99f7062002-06-08 23:25:08 +00002874** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
drh5e00f6c2001-09-13 13:46:56 +00002875*/
2876/* Opcode: NotFound P1 P2 *
2877**
drh6b125452002-01-28 15:53:03 +00002878** Use the top of the stack as a string key. If a record with that key
drhb19a2bc2001-09-16 00:13:26 +00002879** does not exist in table of P1, then jump to P2. If the record
drh3fc190c2001-09-14 03:24:23 +00002880** does exist, then fall thru. The cursor is left pointing to the
2881** record if it exists. The key is popped from the stack.
drh5e00f6c2001-09-13 13:46:56 +00002882**
2883** The difference between this operation and Distinct is that
2884** Distinct does not pop the key from the stack.
drhb19a2bc2001-09-16 00:13:26 +00002885**
drh9cfcf5d2002-01-29 18:41:24 +00002886** See also: Distinct, Found, MoveTo, NotExists, IsUnique
drh5e00f6c2001-09-13 13:46:56 +00002887*/
2888case OP_Distinct:
2889case OP_NotFound:
2890case OP_Found: {
2891 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00002892 int alreadyExists = 0;
drh80ff32f2001-11-04 18:32:46 +00002893 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00002894 assert( pTos>=p->aStack );
2895 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002896 if( (pC = p->apCsr[i])->pCursor!=0 ){
drh5e00f6c2001-09-13 13:46:56 +00002897 int res, rx;
drhf328bc82004-05-10 23:29:49 +00002898 assert( pC->intKey==0 );
drh6810ce62004-01-31 19:22:56 +00002899 Stringify(pTos);
danielk19774adee202004-05-08 08:23:19 +00002900 rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
drh5e00f6c2001-09-13 13:46:56 +00002901 alreadyExists = rx==SQLITE_OK && res==0;
drha11846b2004-01-07 18:52:56 +00002902 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00002903 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00002904 }
2905 if( pOp->opcode==OP_Found ){
2906 if( alreadyExists ) pc = pOp->p2 - 1;
2907 }else{
2908 if( !alreadyExists ) pc = pOp->p2 - 1;
2909 }
2910 if( pOp->opcode!=OP_Distinct ){
drh6810ce62004-01-31 19:22:56 +00002911 Release(pTos);
2912 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00002913 }
2914 break;
2915}
2916
drh9cfcf5d2002-01-29 18:41:24 +00002917/* Opcode: IsUnique P1 P2 *
2918**
drh0ca3e242002-01-29 23:07:02 +00002919** The top of the stack is an integer record number. Call this
2920** record number R. The next on the stack is an index key created
2921** using MakeIdxKey. Call it K. This instruction pops R from the
2922** stack but it leaves K unchanged.
drh9cfcf5d2002-01-29 18:41:24 +00002923**
drha3b321d2004-05-11 09:31:31 +00002924** P1 is an index. So all but the last four bytes of K are an
2925** index string. The last four bytes of K are a record number.
drh0ca3e242002-01-29 23:07:02 +00002926**
2927** This instruction asks if there is an entry in P1 where the
2928** index string matches K but the record number is different
2929** from R. If there is no such entry, then there is an immediate
2930** jump to P2. If any entry does exist where the index string
2931** matches K but the record number is not R, then the record
2932** number for that entry is pushed onto the stack and control
2933** falls through to the next instruction.
drh9cfcf5d2002-01-29 18:41:24 +00002934**
drhd99f7062002-06-08 23:25:08 +00002935** See also: Distinct, NotFound, NotExists, Found
drh9cfcf5d2002-01-29 18:41:24 +00002936*/
2937case OP_IsUnique: {
2938 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00002939 Mem *pNos = &pTos[-1];
drhf328bc82004-05-10 23:29:49 +00002940 Cursor *pCx;
drh9cfcf5d2002-01-29 18:41:24 +00002941 BtCursor *pCrsr;
danielk1977452c9892004-05-13 05:16:15 +00002942 i64 R;
drh9cfcf5d2002-01-29 18:41:24 +00002943
drh0ca3e242002-01-29 23:07:02 +00002944 /* Pop the value R off the top of the stack
2945 */
drh6810ce62004-01-31 19:22:56 +00002946 assert( pNos>=p->aStack );
2947 Integerify(pTos);
2948 R = pTos->i;
2949 pTos--;
2950 assert( i>=0 && i<=p->nCursor );
drhd7556d22004-05-14 21:59:40 +00002951 pCx = p->apCsr[i];
drhf328bc82004-05-10 23:29:49 +00002952 pCrsr = pCx->pCursor;
2953 if( pCrsr!=0 ){
drh9cfcf5d2002-01-29 18:41:24 +00002954 int res, rc;
danielk1977452c9892004-05-13 05:16:15 +00002955 i64 v; /* The record number on the P1 entry that matches K */
drh0ca3e242002-01-29 23:07:02 +00002956 char *zKey; /* The value of K */
2957 int nKey; /* Number of bytes in K */
danielk1977452c9892004-05-13 05:16:15 +00002958 int len; /* Number of bytes in K without the rowid at the end */
drh0ca3e242002-01-29 23:07:02 +00002959
2960 /* Make sure K is a string and make zKey point to K
2961 */
drh6810ce62004-01-31 19:22:56 +00002962 Stringify(pNos);
2963 zKey = pNos->z;
2964 nKey = pNos->n;
danielk1977452c9892004-05-13 05:16:15 +00002965
2966 assert( nKey >= 2 );
2967 len = nKey-2;
2968 while( zKey[len] && --len );
drh0ca3e242002-01-29 23:07:02 +00002969
drha3b321d2004-05-11 09:31:31 +00002970 /* Search for an entry in P1 where all but the last four bytes match K.
drh0ca3e242002-01-29 23:07:02 +00002971 ** If there is no such entry, jump immediately to P2.
2972 */
drh9188b382004-05-14 21:12:22 +00002973 assert( pCx->deferredMoveto==0 );
2974 pCx->cacheValid = 0;
danielk1977452c9892004-05-13 05:16:15 +00002975 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002976 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2977 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00002978 rc = sqlite3BtreeNext(pCrsr, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002979 if( res ){
2980 pc = pOp->p2 - 1;
2981 break;
2982 }
2983 }
danielk19773d1bfea2004-05-14 11:00:53 +00002984 rc = sqlite3VdbeIdxKeyCompare(pCx, len, zKey, 0, &res);
drh9cfcf5d2002-01-29 18:41:24 +00002985 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2986 if( res>0 ){
2987 pc = pOp->p2 - 1;
2988 break;
2989 }
drh0ca3e242002-01-29 23:07:02 +00002990
2991 /* At this point, pCrsr is pointing to an entry in P1 where all but
danielk1977452c9892004-05-13 05:16:15 +00002992 ** the final varint (the rowid) matches K. Check to see if the
2993 ** final varint is different from R. If it equals R then jump
2994 ** immediately to P2.
drh0ca3e242002-01-29 23:07:02 +00002995 */
danielk1977452c9892004-05-13 05:16:15 +00002996 rc = sqlite3VdbeIdxRowid(pCrsr, &v);
2997 if( rc!=SQLITE_OK ){
2998 goto abort_due_to_error;
2999 }
drh0ca3e242002-01-29 23:07:02 +00003000 if( v==R ){
drh9cfcf5d2002-01-29 18:41:24 +00003001 pc = pOp->p2 - 1;
3002 break;
3003 }
drh0ca3e242002-01-29 23:07:02 +00003004
danielk1977452c9892004-05-13 05:16:15 +00003005 /* The final varint of the key is different from R. Push it onto
3006 ** the stack. (The record number of an entry that violates a UNIQUE
3007 ** constraint.)
drh0ca3e242002-01-29 23:07:02 +00003008 */
drh6810ce62004-01-31 19:22:56 +00003009 pTos++;
3010 pTos->i = v;
3011 pTos->flags = MEM_Int;
drh9cfcf5d2002-01-29 18:41:24 +00003012 }
3013 break;
3014}
3015
drh6b125452002-01-28 15:53:03 +00003016/* Opcode: NotExists P1 P2 *
3017**
3018** Use the top of the stack as a integer key. If a record with that key
3019** does not exist in table of P1, then jump to P2. If the record
3020** does exist, then fall thru. The cursor is left pointing to the
3021** record if it exists. The integer key is popped from the stack.
3022**
3023** The difference between this operation and NotFound is that this
3024** operation assumes the key is an integer and NotFound assumes it
3025** is a string.
3026**
drhd99f7062002-06-08 23:25:08 +00003027** See also: Distinct, Found, MoveTo, NotFound, IsUnique
drh6b125452002-01-28 15:53:03 +00003028*/
3029case OP_NotExists: {
3030 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003031 Cursor *pC;
drh0ca3e242002-01-29 23:07:02 +00003032 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003033 assert( pTos>=p->aStack );
3034 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003035 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197736a3c702004-05-11 06:55:14 +00003036 int res, rx;
3037 u64 iKey;
drh6810ce62004-01-31 19:22:56 +00003038 assert( pTos->flags & MEM_Int );
drhd7556d22004-05-14 21:59:40 +00003039 assert( p->apCsr[i]->intKey );
drh6810ce62004-01-31 19:22:56 +00003040 iKey = intToKey(pTos->i);
danielk197736a3c702004-05-11 06:55:14 +00003041 rx = sqlite3BtreeMoveto(pCrsr, 0, iKey, &res);
drh9188b382004-05-14 21:12:22 +00003042 pC->lastRecno = pTos->i;
3043 pC->recnoIsValid = res==0;
3044 pC->nullRow = 0;
3045 pC->cacheValid = 0;
drh6b125452002-01-28 15:53:03 +00003046 if( rx!=SQLITE_OK || res!=0 ){
drh17f71932002-02-21 12:01:27 +00003047 pc = pOp->p2 - 1;
drh9188b382004-05-14 21:12:22 +00003048 pC->recnoIsValid = 0;
drh6b125452002-01-28 15:53:03 +00003049 }
3050 }
drh6810ce62004-01-31 19:22:56 +00003051 Release(pTos);
3052 pTos--;
drh6b125452002-01-28 15:53:03 +00003053 break;
3054}
3055
drh5e00f6c2001-09-13 13:46:56 +00003056/* Opcode: NewRecno P1 * *
3057**
3058** Get a new integer record number used as the key to a table.
drhb19a2bc2001-09-16 00:13:26 +00003059** The record number is not previously used as a key in the database
drh4a324312001-12-21 14:30:42 +00003060** table that cursor P1 points to. The new record number is pushed
drh5e00f6c2001-09-13 13:46:56 +00003061** onto the stack.
3062*/
3063case OP_NewRecno: {
3064 int i = pOp->p1;
drhf328bc82004-05-10 23:29:49 +00003065 i64 v = 0;
drh80ff32f2001-11-04 18:32:46 +00003066 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00003067 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003068 if( (pC = p->apCsr[i])->pCursor==0 ){
drhf328bc82004-05-10 23:29:49 +00003069 /* The zero initialization above is all that is needed */
drh5e00f6c2001-09-13 13:46:56 +00003070 }else{
drh5cf8e8c2002-02-19 22:42:05 +00003071 /* The next rowid or record number (different terms for the same
3072 ** thing) is obtained in a two-step algorithm.
3073 **
3074 ** First we attempt to find the largest existing rowid and add one
3075 ** to that. But if the largest existing rowid is already the maximum
3076 ** positive integer, we have to fall through to the second
3077 ** probabilistic algorithm
3078 **
3079 ** The second algorithm is to select a rowid at random and see if
3080 ** it already exists in the table. If it does not exist, we have
3081 ** succeeded. If the random rowid does exist, we select a new one
3082 ** and try again, up to 1000 times.
drhdb5ed6d2001-09-18 22:17:44 +00003083 **
3084 ** For a table with less than 2 billion entries, the probability
3085 ** of not finding a unused rowid is about 1.0e-300. This is a
3086 ** non-zero probability, but it is still vanishingly small and should
3087 ** never cause a problem. You are much, much more likely to have a
3088 ** hardware failure than for this algorithm to fail.
3089 **
drhaf9ff332002-01-16 21:00:27 +00003090 ** The analysis in the previous paragraph assumes that you have a good
3091 ** source of random numbers. Is a library function like lrand48()
3092 ** good enough? Maybe. Maybe not. It's hard to know whether there
3093 ** might be subtle bugs is some implementations of lrand48() that
3094 ** could cause problems. To avoid uncertainty, SQLite uses its own
3095 ** random number generator based on the RC4 algorithm.
3096 **
drhdb5ed6d2001-09-18 22:17:44 +00003097 ** To promote locality of reference for repetitive inserts, the
drh5cf8e8c2002-02-19 22:42:05 +00003098 ** first few attempts at chosing a random rowid pick values just a little
drhdb5ed6d2001-09-18 22:17:44 +00003099 ** larger than the previous rowid. This has been shown experimentally
3100 ** to double the speed of the COPY operation.
3101 */
drhf328bc82004-05-10 23:29:49 +00003102 int res, rx, cnt;
3103 i64 x;
drh5e00f6c2001-09-13 13:46:56 +00003104 cnt = 0;
drhf328bc82004-05-10 23:29:49 +00003105 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3106 assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
drh5cf8e8c2002-02-19 22:42:05 +00003107 if( !pC->useRandomRowid ){
drh32fbe342002-10-19 20:16:37 +00003108 if( pC->nextRowidValid ){
3109 v = pC->nextRowid;
drh3fc190c2001-09-14 03:24:23 +00003110 }else{
danielk19774adee202004-05-08 08:23:19 +00003111 rx = sqlite3BtreeLast(pC->pCursor, &res);
drh32fbe342002-10-19 20:16:37 +00003112 if( res ){
3113 v = 1;
drh5cf8e8c2002-02-19 22:42:05 +00003114 }else{
drhf328bc82004-05-10 23:29:49 +00003115 sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
drh32fbe342002-10-19 20:16:37 +00003116 v = keyToInt(v);
drhf328bc82004-05-10 23:29:49 +00003117 if( v==0x7fffffffffffffff ){
drh32fbe342002-10-19 20:16:37 +00003118 pC->useRandomRowid = 1;
3119 }else{
3120 v++;
3121 }
drh5cf8e8c2002-02-19 22:42:05 +00003122 }
drh3fc190c2001-09-14 03:24:23 +00003123 }
drhf328bc82004-05-10 23:29:49 +00003124 if( v<0x7fffffffffffffff ){
drh32fbe342002-10-19 20:16:37 +00003125 pC->nextRowidValid = 1;
3126 pC->nextRowid = v+1;
3127 }else{
3128 pC->nextRowidValid = 0;
3129 }
drh5cf8e8c2002-02-19 22:42:05 +00003130 }
3131 if( pC->useRandomRowid ){
3132 v = db->priorNewRowid;
3133 cnt = 0;
3134 do{
3135 if( v==0 || cnt>2 ){
danielk19774adee202004-05-08 08:23:19 +00003136 sqlite3Randomness(sizeof(v), &v);
drh5cf8e8c2002-02-19 22:42:05 +00003137 if( cnt<5 ) v &= 0xffffff;
3138 }else{
drhbbd82df2004-02-11 09:46:30 +00003139 unsigned char r;
danielk19774adee202004-05-08 08:23:19 +00003140 sqlite3Randomness(1, &r);
drhbbd82df2004-02-11 09:46:30 +00003141 v += r + 1;
drh5cf8e8c2002-02-19 22:42:05 +00003142 }
3143 if( v==0 ) continue;
3144 x = intToKey(v);
drhf328bc82004-05-10 23:29:49 +00003145 rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, &res);
drh5cf8e8c2002-02-19 22:42:05 +00003146 cnt++;
3147 }while( cnt<1000 && rx==SQLITE_OK && res==0 );
3148 db->priorNewRowid = v;
3149 if( rx==SQLITE_OK && res==0 ){
3150 rc = SQLITE_FULL;
3151 goto abort_due_to_error;
3152 }
drh1eaa2692001-09-18 02:02:23 +00003153 }
drh17f71932002-02-21 12:01:27 +00003154 pC->recnoIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003155 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003156 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003157 }
drh6810ce62004-01-31 19:22:56 +00003158 pTos++;
3159 pTos->i = v;
3160 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003161 break;
3162}
3163
drh0ca3e242002-01-29 23:07:02 +00003164/* Opcode: PutIntKey P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003165**
jplyon5a564222003-06-02 06:15:58 +00003166** Write an entry into the table of cursor P1. A new entry is
drhb19a2bc2001-09-16 00:13:26 +00003167** created if it doesn't already exist or the data for an existing
drh5e00f6c2001-09-13 13:46:56 +00003168** entry is overwritten. The data is the value on the top of the
drh6b125452002-01-28 15:53:03 +00003169** stack. The key is the next value down on the stack. The key must
3170** be an integer. The stack is popped twice by this instruction.
drh4a324312001-12-21 14:30:42 +00003171**
rdcb0c374f2004-02-20 22:53:38 +00003172** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3173** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set,
3174** then the current statement change count is incremented (otherwise not).
3175** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
danielk19776f8a5032004-05-10 10:34:51 +00003176** stored for subsequent return by the sqlite3_last_insert_rowid() function
rdcb0c374f2004-02-20 22:53:38 +00003177** (otherwise it's unmodified).
drh5e00f6c2001-09-13 13:46:56 +00003178*/
drhc8d30ac2002-04-12 10:08:59 +00003179/* Opcode: PutStrKey P1 * *
drh6b125452002-01-28 15:53:03 +00003180**
jplyon5a564222003-06-02 06:15:58 +00003181** Write an entry into the table of cursor P1. A new entry is
drh6b125452002-01-28 15:53:03 +00003182** created if it doesn't already exist or the data for an existing
3183** entry is overwritten. The data is the value on the top of the
3184** stack. The key is the next value down on the stack. The key must
3185** be a string. The stack is popped twice by this instruction.
drh70ce3f02003-04-15 19:22:22 +00003186**
3187** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
drh6b125452002-01-28 15:53:03 +00003188*/
3189case OP_PutIntKey:
3190case OP_PutStrKey: {
drh6810ce62004-01-31 19:22:56 +00003191 Mem *pNos = &pTos[-1];
drh5e00f6c2001-09-13 13:46:56 +00003192 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003193 Cursor *pC;
drh6810ce62004-01-31 19:22:56 +00003194 assert( pNos>=p->aStack );
3195 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003196 if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
drh5e00f6c2001-09-13 13:46:56 +00003197 char *zKey;
danielk19775f8d8a82004-05-11 00:28:42 +00003198 i64 nKey;
danielk1977e7c8d582004-05-13 13:38:52 +00003199 i64 iKey;
drh6b125452002-01-28 15:53:03 +00003200 if( pOp->opcode==OP_PutStrKey ){
drh6810ce62004-01-31 19:22:56 +00003201 Stringify(pNos);
3202 nKey = pNos->n;
3203 zKey = pNos->z;
drh5e00f6c2001-09-13 13:46:56 +00003204 }else{
drh6810ce62004-01-31 19:22:56 +00003205 assert( pNos->flags & MEM_Int );
danielk19775f8d8a82004-05-11 00:28:42 +00003206
3207 /* If the table is an INTKEY table, set nKey to the value of
danielk19770dbe72b2004-05-11 04:54:49 +00003208 ** the integer key, and zKey to NULL. Otherwise, set nKey to
3209 ** sizeof(i64) and point zKey at iKey. iKey contains the integer
3210 ** key in the on-disk byte order.
danielk19775f8d8a82004-05-11 00:28:42 +00003211 */
danielk19770dbe72b2004-05-11 04:54:49 +00003212 iKey = intToKey(pNos->i);
danielk19775f8d8a82004-05-11 00:28:42 +00003213 if( pC->intKey ){
danielk197749f737d2004-05-11 02:10:06 +00003214 nKey = intToKey(pNos->i);
danielk19775f8d8a82004-05-11 00:28:42 +00003215 zKey = 0;
3216 }else{
danielk19775f8d8a82004-05-11 00:28:42 +00003217 nKey = sizeof(i64);
3218 zKey = (char*)&iKey;
3219 }
danielk19775f8d8a82004-05-11 00:28:42 +00003220
rdcb0c374f2004-02-20 22:53:38 +00003221 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3222 if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
3223 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
drh6810ce62004-01-31 19:22:56 +00003224 if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
drh32fbe342002-10-19 20:16:37 +00003225 pC->nextRowidValid = 0;
3226 }
drh4a324312001-12-21 14:30:42 +00003227 }
drh78a75832004-02-13 14:07:12 +00003228 if( pTos->flags & MEM_Null ){
3229 pTos->z = 0;
3230 pTos->n = 0;
3231 }else{
3232 assert( pTos->flags & MEM_Str );
3233 }
drh70ce3f02003-04-15 19:22:22 +00003234 if( pC->pseudoTable ){
3235 /* PutStrKey does not work for pseudo-tables.
3236 ** The following assert makes sure we are not trying to use
3237 ** PutStrKey on a pseudo-table
3238 */
3239 assert( pOp->opcode==OP_PutIntKey );
3240 sqliteFree(pC->pData);
3241 pC->iKey = iKey;
drh6810ce62004-01-31 19:22:56 +00003242 pC->nData = pTos->n;
3243 if( pTos->flags & MEM_Dyn ){
3244 pC->pData = pTos->z;
3245 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003246 }else{
3247 pC->pData = sqliteMallocRaw( pC->nData );
3248 if( pC->pData ){
drh6810ce62004-01-31 19:22:56 +00003249 memcpy(pC->pData, pTos->z, pC->nData);
drh70ce3f02003-04-15 19:22:22 +00003250 }
3251 }
3252 pC->nullRow = 0;
3253 }else{
danielk19774adee202004-05-08 08:23:19 +00003254 rc = sqlite3BtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
drh70ce3f02003-04-15 19:22:22 +00003255 }
drh32fbe342002-10-19 20:16:37 +00003256 pC->recnoIsValid = 0;
drha11846b2004-01-07 18:52:56 +00003257 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003258 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003259 }
drh6810ce62004-01-31 19:22:56 +00003260 popStack(&pTos, 2);
drh5e00f6c2001-09-13 13:46:56 +00003261 break;
3262}
3263
drhc8d30ac2002-04-12 10:08:59 +00003264/* Opcode: Delete P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003265**
drh5edc3122001-09-13 21:53:09 +00003266** Delete the record at which the P1 cursor is currently pointing.
3267**
3268** The cursor will be left pointing at either the next or the previous
3269** record in the table. If it is left pointing at the next record, then
drhb19a2bc2001-09-16 00:13:26 +00003270** the next Next instruction will be a no-op. Hence it is OK to delete
3271** a record from within an Next loop.
drhc8d30ac2002-04-12 10:08:59 +00003272**
rdcb0c374f2004-02-20 22:53:38 +00003273** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3274** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set,
3275** then the current statement change count is incremented (otherwise not).
drh70ce3f02003-04-15 19:22:22 +00003276**
3277** If P1 is a pseudo-table, then this instruction is a no-op.
drh5e00f6c2001-09-13 13:46:56 +00003278*/
3279case OP_Delete: {
drh5e00f6c2001-09-13 13:46:56 +00003280 int i = pOp->p1;
drh32fbe342002-10-19 20:16:37 +00003281 Cursor *pC;
drh70ce3f02003-04-15 19:22:22 +00003282 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003283 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003284 if( pC->pCursor!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003285 sqlite3VdbeCursorMoveto(pC);
3286 rc = sqlite3BtreeDelete(pC->pCursor);
drh32fbe342002-10-19 20:16:37 +00003287 pC->nextRowidValid = 0;
drh9188b382004-05-14 21:12:22 +00003288 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003289 }
rdcb0c374f2004-02-20 22:53:38 +00003290 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3291 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
3292 break;
3293}
3294
3295/* Opcode: SetCounts * * *
3296**
3297** Called at end of statement. Updates lsChange (last statement change count)
3298** and resets csChange (current statement change count) to 0.
3299*/
3300case OP_SetCounts: {
3301 db->lsChange=db->csChange;
3302 db->csChange=0;
drh5e00f6c2001-09-13 13:46:56 +00003303 break;
3304}
3305
3306/* Opcode: KeyAsData P1 P2 *
3307**
3308** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
drh5fe2d8c2003-05-10 03:36:53 +00003309** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls
3310** data off of the key rather than the data. This is used for
drh5e00f6c2001-09-13 13:46:56 +00003311** processing compound selects.
3312*/
3313case OP_KeyAsData: {
3314 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003315 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003316 p->apCsr[i]->keyAsData = pOp->p2;
drh70ce3f02003-04-15 19:22:22 +00003317 break;
3318}
3319
3320/* Opcode: RowData P1 * *
3321**
3322** Push onto the stack the complete row data for cursor P1.
3323** There is no interpretation of the data. It is just copied
3324** onto the stack exactly as it is found in the database file.
3325**
3326** If the cursor is not pointing to a valid row, a NULL is pushed
3327** onto the stack.
3328*/
drh143f3c42004-01-07 20:37:52 +00003329/* Opcode: RowKey P1 * *
3330**
3331** Push onto the stack the complete row key for cursor P1.
3332** There is no interpretation of the key. It is just copied
3333** onto the stack exactly as it is found in the database file.
3334**
3335** If the cursor is not pointing to a valid row, a NULL is pushed
3336** onto the stack.
3337*/
3338case OP_RowKey:
drh70ce3f02003-04-15 19:22:22 +00003339case OP_RowData: {
3340 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003341 Cursor *pC;
3342 int n;
3343
drh6810ce62004-01-31 19:22:56 +00003344 pTos++;
drh70ce3f02003-04-15 19:22:22 +00003345 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003346 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003347 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003348 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003349 }else if( pC->pCursor!=0 ){
3350 BtCursor *pCrsr = pC->pCursor;
danielk19774adee202004-05-08 08:23:19 +00003351 sqlite3VdbeCursorMoveto(pC);
drh70ce3f02003-04-15 19:22:22 +00003352 if( pC->nullRow ){
drh6810ce62004-01-31 19:22:56 +00003353 pTos->flags = MEM_Null;
drh70ce3f02003-04-15 19:22:22 +00003354 break;
drh143f3c42004-01-07 20:37:52 +00003355 }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
danielk19776490beb2004-05-11 06:17:21 +00003356 i64 n64;
3357 assert( !pC->intKey );
3358 sqlite3BtreeKeySize(pCrsr, &n64);
3359 n = n64;
drh70ce3f02003-04-15 19:22:22 +00003360 }else{
danielk19774adee202004-05-08 08:23:19 +00003361 sqlite3BtreeDataSize(pCrsr, &n);
drh70ce3f02003-04-15 19:22:22 +00003362 }
drh6810ce62004-01-31 19:22:56 +00003363 pTos->n = n;
drh70ce3f02003-04-15 19:22:22 +00003364 if( n<=NBFS ){
drh6810ce62004-01-31 19:22:56 +00003365 pTos->flags = MEM_Str | MEM_Short;
3366 pTos->z = pTos->zShort;
drh70ce3f02003-04-15 19:22:22 +00003367 }else{
3368 char *z = sqliteMallocRaw( n );
3369 if( z==0 ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +00003370 pTos->flags = MEM_Str | MEM_Dyn;
3371 pTos->z = z;
drh70ce3f02003-04-15 19:22:22 +00003372 }
drh143f3c42004-01-07 20:37:52 +00003373 if( pC->keyAsData || pOp->opcode==OP_RowKey ){
danielk19774adee202004-05-08 08:23:19 +00003374 sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003375 }else{
danielk19774adee202004-05-08 08:23:19 +00003376 sqlite3BtreeData(pCrsr, 0, n, pTos->z);
drh70ce3f02003-04-15 19:22:22 +00003377 }
3378 }else if( pC->pseudoTable ){
drh6810ce62004-01-31 19:22:56 +00003379 pTos->n = pC->nData;
3380 pTos->z = pC->pData;
3381 pTos->flags = MEM_Str|MEM_Ephem;
drh70ce3f02003-04-15 19:22:22 +00003382 }else{
drh6810ce62004-01-31 19:22:56 +00003383 pTos->flags = MEM_Null;
drh5e00f6c2001-09-13 13:46:56 +00003384 }
3385 break;
3386}
3387
drh5e00f6c2001-09-13 13:46:56 +00003388/* Opcode: Recno P1 * *
3389**
3390** Push onto the stack an integer which is the first 4 bytes of the
3391** the key to the current entry in a sequential scan of the database
3392** file P1. The sequential scan should have been started using the
3393** Next opcode.
3394*/
3395case OP_Recno: {
3396 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003397 Cursor *pC;
drhf328bc82004-05-10 23:29:49 +00003398 i64 v;
drh5e00f6c2001-09-13 13:46:56 +00003399
drh70ce3f02003-04-15 19:22:22 +00003400 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003401 pC = p->apCsr[i];
danielk19774adee202004-05-08 08:23:19 +00003402 sqlite3VdbeCursorMoveto(pC);
drh6810ce62004-01-31 19:22:56 +00003403 pTos++;
drha11846b2004-01-07 18:52:56 +00003404 if( pC->recnoIsValid ){
drh70ce3f02003-04-15 19:22:22 +00003405 v = pC->lastRecno;
drh70ce3f02003-04-15 19:22:22 +00003406 }else if( pC->pseudoTable ){
3407 v = keyToInt(pC->iKey);
drhd60ccc62003-06-24 10:39:46 +00003408 }else if( pC->nullRow || pC->pCursor==0 ){
drh6810ce62004-01-31 19:22:56 +00003409 pTos->flags = MEM_Null;
drhd60ccc62003-06-24 10:39:46 +00003410 break;
drh70ce3f02003-04-15 19:22:22 +00003411 }else{
3412 assert( pC->pCursor!=0 );
drhf328bc82004-05-10 23:29:49 +00003413 sqlite3BtreeKeySize(pC->pCursor, (u64*)&v);
drh70ce3f02003-04-15 19:22:22 +00003414 v = keyToInt(v);
drh5e00f6c2001-09-13 13:46:56 +00003415 }
drh6810ce62004-01-31 19:22:56 +00003416 pTos->i = v;
3417 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00003418 break;
3419}
3420
3421/* Opcode: FullKey P1 * *
3422**
drhb19a2bc2001-09-16 00:13:26 +00003423** Extract the complete key from the record that cursor P1 is currently
3424** pointing to and push the key onto the stack as a string.
3425**
3426** Compare this opcode to Recno. The Recno opcode extracts the first
3427** 4 bytes of the key and pushes those bytes onto the stack as an
3428** integer. This instruction pushes the entire key as a string.
drh70ce3f02003-04-15 19:22:22 +00003429**
3430** This opcode may not be used on a pseudo-table.
drh5e00f6c2001-09-13 13:46:56 +00003431*/
3432case OP_FullKey: {
3433 int i = pOp->p1;
drh5e00f6c2001-09-13 13:46:56 +00003434 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003435 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003436
drhd7556d22004-05-14 21:59:40 +00003437 assert( p->apCsr[i]->keyAsData );
3438 assert( !p->apCsr[i]->pseudoTable );
drh6810ce62004-01-31 19:22:56 +00003439 assert( i>=0 && i<p->nCursor );
3440 pTos++;
drhd7556d22004-05-14 21:59:40 +00003441 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk197736a3c702004-05-11 06:55:14 +00003442 u64 amt;
drh5e00f6c2001-09-13 13:46:56 +00003443 char *z;
3444
drhd7556d22004-05-14 21:59:40 +00003445 sqlite3VdbeCursorMoveto(pC);
3446 assert( pC->intKey==0 );
danielk197736a3c702004-05-11 06:55:14 +00003447 sqlite3BtreeKeySize(pCrsr, &amt);
drh5e00f6c2001-09-13 13:46:56 +00003448 if( amt<=0 ){
3449 rc = SQLITE_CORRUPT;
3450 goto abort_due_to_error;
3451 }
drh9bbca4c2001-11-06 04:00:18 +00003452 if( amt>NBFS ){
drh8c1238a2003-01-02 14:43:55 +00003453 z = sqliteMallocRaw( amt );
drhcaec2f12003-01-07 02:47:47 +00003454 if( z==0 ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +00003455 pTos->flags = MEM_Str | MEM_Dyn;
drh9bbca4c2001-11-06 04:00:18 +00003456 }else{
drh6810ce62004-01-31 19:22:56 +00003457 z = pTos->zShort;
3458 pTos->flags = MEM_Str | MEM_Short;
drh9bbca4c2001-11-06 04:00:18 +00003459 }
danielk19774adee202004-05-08 08:23:19 +00003460 sqlite3BtreeKey(pCrsr, 0, amt, z);
drh6810ce62004-01-31 19:22:56 +00003461 pTos->z = z;
3462 pTos->n = amt;
drh5e00f6c2001-09-13 13:46:56 +00003463 }
3464 break;
3465}
3466
drh17f71932002-02-21 12:01:27 +00003467/* Opcode: NullRow P1 * *
3468**
3469** Move the cursor P1 to a null row. Any OP_Column operations
3470** that occur while the cursor is on the null row will always push
3471** a NULL onto the stack.
3472*/
3473case OP_NullRow: {
3474 int i = pOp->p1;
drhd7556d22004-05-14 21:59:40 +00003475 Cursor *pC;
drh17f71932002-02-21 12:01:27 +00003476
drh70ce3f02003-04-15 19:22:22 +00003477 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003478 pC = p->apCsr[i];
3479 pC->nullRow = 1;
3480 pC->recnoIsValid = 0;
drh17f71932002-02-21 12:01:27 +00003481 break;
3482}
3483
drh9562b552002-02-19 15:00:07 +00003484/* Opcode: Last P1 P2 *
3485**
3486** The next use of the Recno or Column or Next instruction for P1
3487** will refer to the last entry in the database table or index.
3488** If the table or index is empty and P2>0, then jump immediately to P2.
3489** If P2 is 0 or if the table or index is not empty, fall through
3490** to the following instruction.
3491*/
3492case OP_Last: {
3493 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003494 Cursor *pC;
drh9562b552002-02-19 15:00:07 +00003495 BtCursor *pCrsr;
3496
drh70ce3f02003-04-15 19:22:22 +00003497 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003498 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003499 if( (pCrsr = pC->pCursor)!=0 ){
drh9562b552002-02-19 15:00:07 +00003500 int res;
danielk19774adee202004-05-08 08:23:19 +00003501 rc = sqlite3BtreeLast(pCrsr, &res);
drha11846b2004-01-07 18:52:56 +00003502 pC->nullRow = res;
3503 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003504 pC->cacheValid = 0;
drh9562b552002-02-19 15:00:07 +00003505 if( res && pOp->p2>0 ){
3506 pc = pOp->p2 - 1;
3507 }
drh70ce3f02003-04-15 19:22:22 +00003508 }else{
3509 pC->nullRow = 0;
drh9562b552002-02-19 15:00:07 +00003510 }
3511 break;
3512}
3513
drh8721ce42001-11-07 14:22:00 +00003514/* Opcode: Rewind P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003515**
3516** The next use of the Recno or Column or Next instruction for P1
drh8721ce42001-11-07 14:22:00 +00003517** will refer to the first entry in the database table or index.
3518** If the table or index is empty and P2>0, then jump immediately to P2.
3519** If P2 is 0 or if the table or index is not empty, fall through
3520** to the following instruction.
drh5e00f6c2001-09-13 13:46:56 +00003521*/
3522case OP_Rewind: {
3523 int i = pOp->p1;
drh70ce3f02003-04-15 19:22:22 +00003524 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003525 BtCursor *pCrsr;
drhf4dada72004-05-11 09:57:35 +00003526 int res;
drh5e00f6c2001-09-13 13:46:56 +00003527
drh70ce3f02003-04-15 19:22:22 +00003528 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003529 pC = p->apCsr[i];
drh70ce3f02003-04-15 19:22:22 +00003530 if( (pCrsr = pC->pCursor)!=0 ){
danielk19774adee202004-05-08 08:23:19 +00003531 rc = sqlite3BtreeFirst(pCrsr, &res);
drh70ce3f02003-04-15 19:22:22 +00003532 pC->atFirst = res==0;
drha11846b2004-01-07 18:52:56 +00003533 pC->deferredMoveto = 0;
drh9188b382004-05-14 21:12:22 +00003534 pC->cacheValid = 0;
drh70ce3f02003-04-15 19:22:22 +00003535 }else{
drhf4dada72004-05-11 09:57:35 +00003536 res = 1;
3537 }
3538 pC->nullRow = res;
3539 if( res && pOp->p2>0 ){
3540 pc = pOp->p2 - 1;
drh5e00f6c2001-09-13 13:46:56 +00003541 }
3542 break;
3543}
3544
3545/* Opcode: Next P1 P2 *
3546**
3547** Advance cursor P1 so that it points to the next key/data pair in its
drh8721ce42001-11-07 14:22:00 +00003548** table or index. If there are no more key/value pairs then fall through
3549** to the following instruction. But if the cursor advance was successful,
3550** jump immediately to P2.
drhc045ec52002-12-04 20:01:06 +00003551**
3552** See also: Prev
drh8721ce42001-11-07 14:22:00 +00003553*/
drhc045ec52002-12-04 20:01:06 +00003554/* Opcode: Prev P1 P2 *
3555**
3556** Back up cursor P1 so that it points to the previous key/data pair in its
3557** table or index. If there is no previous key/value pairs then fall through
3558** to the following instruction. But if the cursor backup was successful,
3559** jump immediately to P2.
3560*/
3561case OP_Prev:
drh6b563442001-11-07 16:48:26 +00003562case OP_Next: {
drh7b396862003-01-01 23:06:20 +00003563 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003564 BtCursor *pCrsr;
3565
drhcaec2f12003-01-07 02:47:47 +00003566 CHECK_FOR_INTERRUPT;
drh70ce3f02003-04-15 19:22:22 +00003567 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003568 pC = p->apCsr[pOp->p1];
drh70ce3f02003-04-15 19:22:22 +00003569 if( (pCrsr = pC->pCursor)!=0 ){
drh8721ce42001-11-07 14:22:00 +00003570 int res;
drh7b396862003-01-01 23:06:20 +00003571 if( pC->nullRow ){
drhad2d8302002-05-24 20:31:36 +00003572 res = 1;
3573 }else{
drha11846b2004-01-07 18:52:56 +00003574 assert( pC->deferredMoveto==0 );
danielk19774adee202004-05-08 08:23:19 +00003575 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3576 sqlite3BtreePrevious(pCrsr, &res);
drh7b396862003-01-01 23:06:20 +00003577 pC->nullRow = res;
drh9188b382004-05-14 21:12:22 +00003578 pC->cacheValid = 0;
drhad2d8302002-05-24 20:31:36 +00003579 }
drh8721ce42001-11-07 14:22:00 +00003580 if( res==0 ){
3581 pc = pOp->p2 - 1;
danielk19776f8a5032004-05-10 10:34:51 +00003582 sqlite3_search_count++;
drh8721ce42001-11-07 14:22:00 +00003583 }
drh70ce3f02003-04-15 19:22:22 +00003584 }else{
3585 pC->nullRow = 1;
drh8721ce42001-11-07 14:22:00 +00003586 }
drh70ce3f02003-04-15 19:22:22 +00003587 pC->recnoIsValid = 0;
drh8721ce42001-11-07 14:22:00 +00003588 break;
3589}
3590
drh8721ce42001-11-07 14:22:00 +00003591/* Opcode: IdxPut P1 P2 P3
drh5e00f6c2001-09-13 13:46:56 +00003592**
jplyon5a564222003-06-02 06:15:58 +00003593** The top of the stack holds a SQL index key made using the
drh5e00f6c2001-09-13 13:46:56 +00003594** MakeIdxKey instruction. This opcode writes that key into the
3595** index P1. Data for the entry is nil.
drh717e6402001-09-27 03:22:32 +00003596**
3597** If P2==1, then the key must be unique. If the key is not unique,
3598** the program aborts with a SQLITE_CONSTRAINT error and the database
jplyon5a564222003-06-02 06:15:58 +00003599** is rolled back. If P3 is not null, then it becomes part of the
drh717e6402001-09-27 03:22:32 +00003600** error message returned with the SQLITE_CONSTRAINT.
drh5e00f6c2001-09-13 13:46:56 +00003601*/
drh8721ce42001-11-07 14:22:00 +00003602case OP_IdxPut: {
drh5e00f6c2001-09-13 13:46:56 +00003603 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003604 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003605 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003606 assert( pTos>=p->aStack );
3607 assert( i>=0 && i<p->nCursor );
3608 assert( pTos->flags & MEM_Str );
drhd7556d22004-05-14 21:59:40 +00003609 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh6810ce62004-01-31 19:22:56 +00003610 int nKey = pTos->n;
3611 const char *zKey = pTos->z;
drh717e6402001-09-27 03:22:32 +00003612 if( pOp->p2 ){
danielk197736a3c702004-05-11 06:55:14 +00003613 int res;
danielk1977452c9892004-05-13 05:16:15 +00003614 int len;
danielk197736a3c702004-05-11 06:55:14 +00003615 u64 n;
danielk1977452c9892004-05-13 05:16:15 +00003616
3617 /* 'len' is the length of the key minus the rowid at the end */
3618 len = nKey-2;
3619 while( zKey[len] && --len );
3620
3621 rc = sqlite3BtreeMoveto(pCrsr, zKey, len, &res);
drh717e6402001-09-27 03:22:32 +00003622 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3623 while( res!=0 ){
3624 int c;
danielk197736a3c702004-05-11 06:55:14 +00003625 sqlite3BtreeKeySize(pCrsr, &n);
danielk19773d1bfea2004-05-14 11:00:53 +00003626 if( n==nKey &&
drh9188b382004-05-14 21:12:22 +00003627 sqlite3VdbeIdxKeyCompare(pC, len, zKey, 0, &c)==SQLITE_OK
danielk1977452c9892004-05-13 05:16:15 +00003628 && c==0
drh717e6402001-09-27 03:22:32 +00003629 ){
3630 rc = SQLITE_CONSTRAINT;
3631 if( pOp->p3 && pOp->p3[0] ){
danielk19774adee202004-05-08 08:23:19 +00003632 sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
drh717e6402001-09-27 03:22:32 +00003633 }
3634 goto abort_due_to_error;
3635 }
3636 if( res<0 ){
danielk19774adee202004-05-08 08:23:19 +00003637 sqlite3BtreeNext(pCrsr, &res);
drh717e6402001-09-27 03:22:32 +00003638 res = +1;
3639 }else{
3640 break;
3641 }
3642 }
3643 }
drh9188b382004-05-14 21:12:22 +00003644 assert( pC->intKey==0 );
danielk19774adee202004-05-08 08:23:19 +00003645 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0);
drh9188b382004-05-14 21:12:22 +00003646 assert( pC->deferredMoveto==0 );
3647 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003648 }
drh6810ce62004-01-31 19:22:56 +00003649 Release(pTos);
3650 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003651 break;
3652}
3653
drh8721ce42001-11-07 14:22:00 +00003654/* Opcode: IdxDelete P1 * *
drh5e00f6c2001-09-13 13:46:56 +00003655**
3656** The top of the stack is an index key built using the MakeIdxKey opcode.
3657** This opcode removes that entry from the index.
3658*/
drh8721ce42001-11-07 14:22:00 +00003659case OP_IdxDelete: {
drh5e00f6c2001-09-13 13:46:56 +00003660 int i = pOp->p1;
drh9188b382004-05-14 21:12:22 +00003661 Cursor *pC;
drh5e00f6c2001-09-13 13:46:56 +00003662 BtCursor *pCrsr;
drh6810ce62004-01-31 19:22:56 +00003663 assert( pTos>=p->aStack );
3664 assert( pTos->flags & MEM_Str );
3665 assert( i>=0 && i<p->nCursor );
drhd7556d22004-05-14 21:59:40 +00003666 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh5e00f6c2001-09-13 13:46:56 +00003667 int rx, res;
danielk19774adee202004-05-08 08:23:19 +00003668 rx = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
drh5e00f6c2001-09-13 13:46:56 +00003669 if( rx==SQLITE_OK && res==0 ){
danielk19774adee202004-05-08 08:23:19 +00003670 rc = sqlite3BtreeDelete(pCrsr);
drh5e00f6c2001-09-13 13:46:56 +00003671 }
drh9188b382004-05-14 21:12:22 +00003672 assert( pC->deferredMoveto==0 );
3673 pC->cacheValid = 0;
drh5e00f6c2001-09-13 13:46:56 +00003674 }
drh6810ce62004-01-31 19:22:56 +00003675 Release(pTos);
3676 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003677 break;
3678}
3679
drh8721ce42001-11-07 14:22:00 +00003680/* Opcode: IdxRecno P1 * *
3681**
danielk1977452c9892004-05-13 05:16:15 +00003682** Push onto the stack an integer which is the varint located at the
3683** end of the index key pointed to by cursor P1. These integer should be
3684** the record number of the table entry to which this index entry points.
drh8721ce42001-11-07 14:22:00 +00003685**
3686** See also: Recno, MakeIdxKey.
3687*/
3688case OP_IdxRecno: {
3689 int i = pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003690 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003691 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003692
drh6810ce62004-01-31 19:22:56 +00003693 assert( i>=0 && i<p->nCursor );
3694 pTos++;
drhd7556d22004-05-14 21:59:40 +00003695 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
danielk19773d1bfea2004-05-14 11:00:53 +00003696 i64 rowid;
danielk1977452c9892004-05-13 05:16:15 +00003697
drhd7556d22004-05-14 21:59:40 +00003698 assert( pC->deferredMoveto==0 );
3699 assert( pC->intKey==0 );
danielk19773d1bfea2004-05-14 11:00:53 +00003700 rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3701 if( rc!=SQLITE_OK ){
3702 goto abort_due_to_error;
3703 }
3704 pTos->flags = MEM_Int;
3705 pTos->i = rowid;
danielk1977452c9892004-05-13 05:16:15 +00003706
danielk19773d1bfea2004-05-14 11:00:53 +00003707#if 0
danielk1977452c9892004-05-13 05:16:15 +00003708 /* Read the final 9 bytes of the key into buf[]. If the whole key is
3709 ** less than 9 bytes then just load the whole thing. Set len to the
3710 ** number of bytes read.
3711 */
danielk19776490beb2004-05-11 06:17:21 +00003712 sqlite3BtreeKeySize(pCrsr, &sz);
danielk19773d1bfea2004-05-14 11:00:53 +00003713 len = ((sz>10)?10:sz);
danielk1977452c9892004-05-13 05:16:15 +00003714 rc = sqlite3BtreeKey(pCrsr, sz-len, len, buf);
3715 if( rc!=SQLITE_OK ){
3716 goto abort_due_to_error;
3717 }
3718
3719 len--;
3720 if( buf[len]&0x80 ){
3721 /* If the last byte read has the 0x80 bit set, then the key does
3722 ** not end with a varint. Push a NULL onto the stack instead.
3723 */
drh6810ce62004-01-31 19:22:56 +00003724 pTos->flags = MEM_Null;
drhd4d595f2003-04-17 12:44:23 +00003725 }else{
danielk1977452c9892004-05-13 05:16:15 +00003726 /* Find the start of the varint by searching backwards for a 0x00
3727 ** byte. If one does not exists, then intepret the whole 9 bytes as a
3728 ** varint.
3729 */
3730 while( len && buf[len-1] ){
3731 len--;
3732 }
3733 sqlite3GetVarint(&buf[len], &sz);
drh6810ce62004-01-31 19:22:56 +00003734 pTos->flags = MEM_Int;
danielk1977452c9892004-05-13 05:16:15 +00003735 pTos->i = sz;
drhd4d595f2003-04-17 12:44:23 +00003736 }
danielk19773d1bfea2004-05-14 11:00:53 +00003737#endif
drh6810ce62004-01-31 19:22:56 +00003738 }else{
3739 pTos->flags = MEM_Null;
drh8721ce42001-11-07 14:22:00 +00003740 }
3741 break;
3742}
3743
3744/* Opcode: IdxGT P1 P2 *
3745**
3746** Compare the top of the stack against the key on the index entry that
3747** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3748** index entry. If the index entry is greater than the top of the stack
3749** then jump to P2. Otherwise fall through to the next instruction.
3750** In either case, the stack is popped once.
3751*/
3752/* Opcode: IdxGE P1 P2 *
3753**
3754** Compare the top of the stack against the key on the index entry that
3755** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3756** index entry. If the index entry is greater than or equal to
3757** the top of the stack
3758** then jump to P2. Otherwise fall through to the next instruction.
3759** In either case, the stack is popped once.
3760*/
drhc045ec52002-12-04 20:01:06 +00003761/* Opcode: IdxLT P1 P2 *
3762**
3763** Compare the top of the stack against the key on the index entry that
3764** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3765** index entry. If the index entry is less than the top of the stack
3766** then jump to P2. Otherwise fall through to the next instruction.
3767** In either case, the stack is popped once.
3768*/
3769case OP_IdxLT:
drh8721ce42001-11-07 14:22:00 +00003770case OP_IdxGT:
3771case OP_IdxGE: {
3772 int i= pOp->p1;
drh8721ce42001-11-07 14:22:00 +00003773 BtCursor *pCrsr;
drhd7556d22004-05-14 21:59:40 +00003774 Cursor *pC;
drh8721ce42001-11-07 14:22:00 +00003775
drh6810ce62004-01-31 19:22:56 +00003776 assert( i>=0 && i<p->nCursor );
3777 assert( pTos>=p->aStack );
drhd7556d22004-05-14 21:59:40 +00003778 if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
drh8721ce42001-11-07 14:22:00 +00003779 int res, rc;
3780
drh6810ce62004-01-31 19:22:56 +00003781 Stringify(pTos);
drhd7556d22004-05-14 21:59:40 +00003782 assert( pC->deferredMoveto==0 );
danielk19773d1bfea2004-05-14 11:00:53 +00003783 if( pOp->p3 ){
3784 pC->incrKey = 1;
3785 }
3786 rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, pTos->z, 0, &res);
3787 pC->incrKey = 0;
drh8721ce42001-11-07 14:22:00 +00003788 if( rc!=SQLITE_OK ){
3789 break;
3790 }
drhc045ec52002-12-04 20:01:06 +00003791 if( pOp->opcode==OP_IdxLT ){
3792 res = -res;
3793 }else if( pOp->opcode==OP_IdxGE ){
drh8721ce42001-11-07 14:22:00 +00003794 res++;
3795 }
3796 if( res>0 ){
3797 pc = pOp->p2 - 1 ;
3798 }
3799 }
drh6810ce62004-01-31 19:22:56 +00003800 Release(pTos);
3801 pTos--;
drh8721ce42001-11-07 14:22:00 +00003802 break;
3803}
3804
drh143f3c42004-01-07 20:37:52 +00003805/* Opcode: IdxIsNull P1 P2 *
3806**
3807** The top of the stack contains an index entry such as might be generated
3808** by the MakeIdxKey opcode. This routine looks at the first P1 fields of
3809** that key. If any of the first P1 fields are NULL, then a jump is made
3810** to address P2. Otherwise we fall straight through.
3811**
3812** The index entry is always popped from the stack.
3813*/
3814case OP_IdxIsNull: {
3815 int i = pOp->p1;
drh143f3c42004-01-07 20:37:52 +00003816 int k, n;
3817 const char *z;
3818
drh6810ce62004-01-31 19:22:56 +00003819 assert( pTos>=p->aStack );
3820 assert( pTos->flags & MEM_Str );
3821 z = pTos->z;
3822 n = pTos->n;
drh143f3c42004-01-07 20:37:52 +00003823 for(k=0; k<n && i>0; i--){
danielk19773d1bfea2004-05-14 11:00:53 +00003824 u64 serial_type;
3825 k += sqlite3GetVarint(&z[k], &serial_type);
3826 if( serial_type==6 ){ /* Serial type 6 is a NULL */
drh143f3c42004-01-07 20:37:52 +00003827 pc = pOp->p2-1;
3828 break;
3829 }
danielk19773d1bfea2004-05-14 11:00:53 +00003830 k += sqlite3VdbeSerialTypeLen(serial_type);
drh143f3c42004-01-07 20:37:52 +00003831 }
drh6810ce62004-01-31 19:22:56 +00003832 Release(pTos);
3833 pTos--;
drh143f3c42004-01-07 20:37:52 +00003834 break;
3835}
3836
drhf57b3392001-10-08 13:22:32 +00003837/* Opcode: Destroy P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003838**
3839** Delete an entire database table or index whose root page in the database
3840** file is given by P1.
drhb19a2bc2001-09-16 00:13:26 +00003841**
drhf57b3392001-10-08 13:22:32 +00003842** The table being destroyed is in the main database file if P2==0. If
3843** P2==1 then the table to be clear is in the auxiliary database file
3844** that is used to store tables create using CREATE TEMPORARY TABLE.
3845**
drhb19a2bc2001-09-16 00:13:26 +00003846** See also: Clear
drh5e00f6c2001-09-13 13:46:56 +00003847*/
3848case OP_Destroy: {
danielk19774adee202004-05-08 08:23:19 +00003849 rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
drh5e00f6c2001-09-13 13:46:56 +00003850 break;
3851}
3852
drhf57b3392001-10-08 13:22:32 +00003853/* Opcode: Clear P1 P2 *
drh5edc3122001-09-13 21:53:09 +00003854**
3855** Delete all contents of the database table or index whose root page
drhb19a2bc2001-09-16 00:13:26 +00003856** in the database file is given by P1. But, unlike Destroy, do not
drh5edc3122001-09-13 21:53:09 +00003857** remove the table or index from the database file.
drhb19a2bc2001-09-16 00:13:26 +00003858**
drhf57b3392001-10-08 13:22:32 +00003859** The table being clear is in the main database file if P2==0. If
3860** P2==1 then the table to be clear is in the auxiliary database file
3861** that is used to store tables create using CREATE TEMPORARY TABLE.
3862**
drhb19a2bc2001-09-16 00:13:26 +00003863** See also: Destroy
drh5edc3122001-09-13 21:53:09 +00003864*/
3865case OP_Clear: {
danielk19774adee202004-05-08 08:23:19 +00003866 rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
drh5edc3122001-09-13 21:53:09 +00003867 break;
3868}
3869
drhf57b3392001-10-08 13:22:32 +00003870/* Opcode: CreateTable * P2 P3
drh5b2fd562001-09-13 15:21:31 +00003871**
drhf57b3392001-10-08 13:22:32 +00003872** Allocate a new table in the main database file if P2==0 or in the
3873** auxiliary database file if P2==1. Push the page number
drh5b2fd562001-09-13 15:21:31 +00003874** for the root page of the new table onto the stack.
3875**
drhadbca9c2001-09-27 15:11:53 +00003876** The root page number is also written to a memory location that P3
3877** points to. This is the mechanism is used to write the root page
3878** number into the parser's internal data structures that describe the
3879** new table.
drhb19a2bc2001-09-16 00:13:26 +00003880**
drhc6b52df2002-01-04 03:09:29 +00003881** The difference between a table and an index is this: A table must
3882** have a 4-byte integer key and can have arbitrary data. An index
3883** has an arbitrary key but no data.
3884**
drhb19a2bc2001-09-16 00:13:26 +00003885** See also: CreateIndex
drh5b2fd562001-09-13 15:21:31 +00003886*/
drhf57b3392001-10-08 13:22:32 +00003887/* Opcode: CreateIndex * P2 P3
3888**
drhc6b52df2002-01-04 03:09:29 +00003889** Allocate a new index in the main database file if P2==0 or in the
3890** auxiliary database file if P2==1. Push the page number of the
3891** root page of the new index onto the stack.
drhf57b3392001-10-08 13:22:32 +00003892**
drhc6b52df2002-01-04 03:09:29 +00003893** See documentation on OP_CreateTable for additional information.
drhf57b3392001-10-08 13:22:32 +00003894*/
3895case OP_CreateIndex:
drh5b2fd562001-09-13 15:21:31 +00003896case OP_CreateTable: {
drh5b2fd562001-09-13 15:21:31 +00003897 int pgno;
drhf328bc82004-05-10 23:29:49 +00003898 int flags;
drh99fcd712001-10-13 01:06:47 +00003899 assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
drh001bbcb2003-03-19 03:14:00 +00003900 assert( pOp->p2>=0 && pOp->p2<db->nDb );
3901 assert( db->aDb[pOp->p2].pBt!=0 );
drhc6b52df2002-01-04 03:09:29 +00003902 if( pOp->opcode==OP_CreateTable ){
danielk197794076252004-05-14 12:16:11 +00003903 /* flags = BTREE_INTKEY; */
3904 flags = BTREE_LEAFDATA|BTREE_INTKEY;
drhc6b52df2002-01-04 03:09:29 +00003905 }else{
drhf328bc82004-05-10 23:29:49 +00003906 flags = BTREE_ZERODATA;
drhc6b52df2002-01-04 03:09:29 +00003907 }
drhf328bc82004-05-10 23:29:49 +00003908 rc = sqlite3BtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno, flags);
drh6810ce62004-01-31 19:22:56 +00003909 pTos++;
drh5b2fd562001-09-13 15:21:31 +00003910 if( rc==SQLITE_OK ){
drh6810ce62004-01-31 19:22:56 +00003911 pTos->i = pgno;
3912 pTos->flags = MEM_Int;
drhadbca9c2001-09-27 15:11:53 +00003913 *(u32*)pOp->p3 = pgno;
3914 pOp->p3 = 0;
drhf1b07b02004-02-08 06:17:19 +00003915 }else{
3916 pTos->flags = MEM_Null;
drh5b2fd562001-09-13 15:21:31 +00003917 }
3918 break;
3919}
3920
drh21504322002-06-25 13:16:02 +00003921/* Opcode: IntegrityCk P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00003922**
drh1dd397f2002-02-03 03:34:07 +00003923** Do an analysis of the currently open database. Push onto the
3924** stack the text of an error message describing any problems.
3925** If there are no errors, push a "ok" onto the stack.
drhb19a2bc2001-09-16 00:13:26 +00003926**
drh1dd397f2002-02-03 03:34:07 +00003927** P1 is the index of a set that contains the root page numbers
drhed717fe2003-06-15 23:42:24 +00003928** for all tables and indices in the main database file. The set
3929** is cleared by this opcode. In other words, after this opcode
3930** has executed, the set will be empty.
drh21504322002-06-25 13:16:02 +00003931**
3932** If P2 is not zero, the check is done on the auxiliary database
3933** file, not the main database file.
drh1dd397f2002-02-03 03:34:07 +00003934**
3935** This opcode is used for testing purposes only.
drh5e00f6c2001-09-13 13:46:56 +00003936*/
drhaaab5722002-02-19 13:39:21 +00003937case OP_IntegrityCk: {
drh1dd397f2002-02-03 03:34:07 +00003938 int nRoot;
3939 int *aRoot;
drh1dd397f2002-02-03 03:34:07 +00003940 int iSet = pOp->p1;
3941 Set *pSet;
3942 int j;
3943 HashElem *i;
3944 char *z;
3945
drh6810ce62004-01-31 19:22:56 +00003946 assert( iSet>=0 && iSet<p->nSet );
3947 pTos++;
drh1dd397f2002-02-03 03:34:07 +00003948 pSet = &p->aSet[iSet];
3949 nRoot = sqliteHashCount(&pSet->hash);
drhcaec2f12003-01-07 02:47:47 +00003950 aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) );
3951 if( aRoot==0 ) goto no_mem;
drh1dd397f2002-02-03 03:34:07 +00003952 for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){
danielk19773d1bfea2004-05-14 11:00:53 +00003953 i64 root64;
3954 toInt((char*)sqliteHashKey(i), &root64);
3955 aRoot[j] = root64;
drh1dd397f2002-02-03 03:34:07 +00003956 }
3957 aRoot[j] = 0;
danielk19774adee202004-05-08 08:23:19 +00003958 sqlite3HashClear(&pSet->hash);
drhed717fe2003-06-15 23:42:24 +00003959 pSet->prev = 0;
danielk19774adee202004-05-08 08:23:19 +00003960 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
drh1dd397f2002-02-03 03:34:07 +00003961 if( z==0 || z[0]==0 ){
drh21504322002-06-25 13:16:02 +00003962 if( z ) sqliteFree(z);
drh6810ce62004-01-31 19:22:56 +00003963 pTos->z = "ok";
3964 pTos->n = 3;
3965 pTos->flags = MEM_Str | MEM_Static;
drh1dd397f2002-02-03 03:34:07 +00003966 }else{
drh6810ce62004-01-31 19:22:56 +00003967 pTos->z = z;
3968 pTos->n = strlen(z) + 1;
3969 pTos->flags = MEM_Str | MEM_Dyn;
drh1dd397f2002-02-03 03:34:07 +00003970 }
drh24e97df2002-02-03 19:06:02 +00003971 sqliteFree(aRoot);
drh5e00f6c2001-09-13 13:46:56 +00003972 break;
3973}
3974
drha8b38d22001-11-01 14:41:34 +00003975/* Opcode: ListWrite * * *
drh5e00f6c2001-09-13 13:46:56 +00003976**
3977** Write the integer on the top of the stack
drha8b38d22001-11-01 14:41:34 +00003978** into the temporary storage list.
drh5e00f6c2001-09-13 13:46:56 +00003979*/
3980case OP_ListWrite: {
drh5e00f6c2001-09-13 13:46:56 +00003981 Keylist *pKeylist;
drh6810ce62004-01-31 19:22:56 +00003982 assert( pTos>=p->aStack );
drha8b38d22001-11-01 14:41:34 +00003983 pKeylist = p->pList;
drh5e00f6c2001-09-13 13:46:56 +00003984 if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
drh8c1238a2003-01-02 14:43:55 +00003985 pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
drh5e00f6c2001-09-13 13:46:56 +00003986 if( pKeylist==0 ) goto no_mem;
3987 pKeylist->nKey = 1000;
3988 pKeylist->nRead = 0;
3989 pKeylist->nUsed = 0;
drha8b38d22001-11-01 14:41:34 +00003990 pKeylist->pNext = p->pList;
3991 p->pList = pKeylist;
drh5e00f6c2001-09-13 13:46:56 +00003992 }
drh6810ce62004-01-31 19:22:56 +00003993 Integerify(pTos);
3994 pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
drh79f14b72004-03-03 01:51:24 +00003995 Release(pTos);
drh6810ce62004-01-31 19:22:56 +00003996 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00003997 break;
3998}
3999
drha8b38d22001-11-01 14:41:34 +00004000/* Opcode: ListRewind * * *
drh5e00f6c2001-09-13 13:46:56 +00004001**
drhfb044c12004-02-10 13:41:52 +00004002** Rewind the temporary buffer back to the beginning.
drh5e00f6c2001-09-13 13:46:56 +00004003*/
4004case OP_ListRewind: {
drhfb044c12004-02-10 13:41:52 +00004005 /* What this opcode codes, really, is reverse the order of the
4006 ** linked list of Keylist structures so that they are read out
4007 ** in the same order that they were read in. */
4008 Keylist *pRev, *pTop;
4009 pRev = 0;
4010 while( p->pList ){
4011 pTop = p->pList;
4012 p->pList = pTop->pNext;
4013 pTop->pNext = pRev;
4014 pRev = pTop;
4015 }
4016 p->pList = pRev;
drh5e00f6c2001-09-13 13:46:56 +00004017 break;
4018}
4019
drha8b38d22001-11-01 14:41:34 +00004020/* Opcode: ListRead * P2 *
drh5e00f6c2001-09-13 13:46:56 +00004021**
drha8b38d22001-11-01 14:41:34 +00004022** Attempt to read an integer from the temporary storage buffer
drh5e00f6c2001-09-13 13:46:56 +00004023** and push it onto the stack. If the storage buffer is empty,
4024** push nothing but instead jump to P2.
4025*/
4026case OP_ListRead: {
drh5e00f6c2001-09-13 13:46:56 +00004027 Keylist *pKeylist;
drhcaec2f12003-01-07 02:47:47 +00004028 CHECK_FOR_INTERRUPT;
drha8b38d22001-11-01 14:41:34 +00004029 pKeylist = p->pList;
drh5e00f6c2001-09-13 13:46:56 +00004030 if( pKeylist!=0 ){
drh6810ce62004-01-31 19:22:56 +00004031 assert( pKeylist->nRead>=0 );
4032 assert( pKeylist->nRead<pKeylist->nUsed );
4033 assert( pKeylist->nRead<pKeylist->nKey );
4034 pTos++;
4035 pTos->i = pKeylist->aKey[pKeylist->nRead++];
4036 pTos->flags = MEM_Int;
drh5e00f6c2001-09-13 13:46:56 +00004037 if( pKeylist->nRead>=pKeylist->nUsed ){
drha8b38d22001-11-01 14:41:34 +00004038 p->pList = pKeylist->pNext;
drh5e00f6c2001-09-13 13:46:56 +00004039 sqliteFree(pKeylist);
4040 }
4041 }else{
4042 pc = pOp->p2 - 1;
4043 }
4044 break;
4045}
4046
drha8b38d22001-11-01 14:41:34 +00004047/* Opcode: ListReset * * *
drh5e00f6c2001-09-13 13:46:56 +00004048**
drha8b38d22001-11-01 14:41:34 +00004049** Reset the temporary storage buffer so that it holds nothing.
drh5e00f6c2001-09-13 13:46:56 +00004050*/
drha8b38d22001-11-01 14:41:34 +00004051case OP_ListReset: {
4052 if( p->pList ){
danielk19774adee202004-05-08 08:23:19 +00004053 sqlite3VdbeKeylistFree(p->pList);
drha8b38d22001-11-01 14:41:34 +00004054 p->pList = 0;
drh5e00f6c2001-09-13 13:46:56 +00004055 }
4056 break;
4057}
4058
drhbd5a4512002-05-23 22:07:02 +00004059/* Opcode: ListPush * * *
4060**
drhad3cab52002-05-24 02:04:32 +00004061** Save the current Vdbe list such that it can be restored by a ListPop
drhbd5a4512002-05-23 22:07:02 +00004062** opcode. The list is empty after this is executed.
4063*/
4064case OP_ListPush: {
4065 p->keylistStackDepth++;
4066 assert(p->keylistStackDepth > 0);
4067 p->keylistStack = sqliteRealloc(p->keylistStack,
4068 sizeof(Keylist *) * p->keylistStackDepth);
drhcaec2f12003-01-07 02:47:47 +00004069 if( p->keylistStack==0 ) goto no_mem;
drhbd5a4512002-05-23 22:07:02 +00004070 p->keylistStack[p->keylistStackDepth - 1] = p->pList;
4071 p->pList = 0;
4072 break;
4073}
4074
4075/* Opcode: ListPop * * *
4076**
drhad3cab52002-05-24 02:04:32 +00004077** Restore the Vdbe list to the state it was in when ListPush was last
drhbd5a4512002-05-23 22:07:02 +00004078** executed.
4079*/
4080case OP_ListPop: {
4081 assert(p->keylistStackDepth > 0);
4082 p->keylistStackDepth--;
danielk19774adee202004-05-08 08:23:19 +00004083 sqlite3VdbeKeylistFree(p->pList);
drhbd5a4512002-05-23 22:07:02 +00004084 p->pList = p->keylistStack[p->keylistStackDepth];
4085 p->keylistStack[p->keylistStackDepth] = 0;
4086 if( p->keylistStackDepth == 0 ){
4087 sqliteFree(p->keylistStack);
4088 p->keylistStack = 0;
4089 }
4090 break;
4091}
4092
rdcb0c374f2004-02-20 22:53:38 +00004093/* Opcode: ContextPush * * *
4094**
4095** Save the current Vdbe context such that it can be restored by a ContextPop
4096** opcode. The context stores the last insert row id, the last statement change
4097** count, and the current statement change count.
4098*/
4099case OP_ContextPush: {
4100 p->contextStackDepth++;
4101 assert(p->contextStackDepth > 0);
4102 p->contextStack = sqliteRealloc(p->contextStack,
4103 sizeof(Context) * p->contextStackDepth);
4104 if( p->contextStack==0 ) goto no_mem;
4105 p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
4106 p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
4107 p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
4108 break;
4109}
4110
4111/* Opcode: ContextPop * * *
4112**
4113** Restore the Vdbe context to the state it was in when contextPush was last
4114** executed. The context stores the last insert row id, the last statement
4115** change count, and the current statement change count.
4116*/
4117case OP_ContextPop: {
4118 assert(p->contextStackDepth > 0);
4119 p->contextStackDepth--;
4120 p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
4121 p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
4122 p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
4123 if( p->contextStackDepth == 0 ){
4124 sqliteFree(p->contextStack);
4125 p->contextStack = 0;
4126 }
4127 break;
4128}
4129
drha8b38d22001-11-01 14:41:34 +00004130/* Opcode: SortPut * * *
drh5e00f6c2001-09-13 13:46:56 +00004131**
4132** The TOS is the key and the NOS is the data. Pop both from the stack
drh9bbca4c2001-11-06 04:00:18 +00004133** and put them on the sorter. The key and data should have been
4134** made using SortMakeKey and SortMakeRec, respectively.
drh5e00f6c2001-09-13 13:46:56 +00004135*/
4136case OP_SortPut: {
drh6810ce62004-01-31 19:22:56 +00004137 Mem *pNos = &pTos[-1];
drh5e00f6c2001-09-13 13:46:56 +00004138 Sorter *pSorter;
drh6810ce62004-01-31 19:22:56 +00004139 assert( pNos>=p->aStack );
4140 if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem;
drh8c1238a2003-01-02 14:43:55 +00004141 pSorter = sqliteMallocRaw( sizeof(Sorter) );
drh5e00f6c2001-09-13 13:46:56 +00004142 if( pSorter==0 ) goto no_mem;
drha8b38d22001-11-01 14:41:34 +00004143 pSorter->pNext = p->pSort;
4144 p->pSort = pSorter;
drh6810ce62004-01-31 19:22:56 +00004145 assert( pTos->flags & MEM_Dyn );
4146 pSorter->nKey = pTos->n;
4147 pSorter->zKey = pTos->z;
4148 assert( pNos->flags & MEM_Dyn );
4149 pSorter->nData = pNos->n;
4150 pSorter->pData = pNos->z;
4151 pTos -= 2;
drh5e00f6c2001-09-13 13:46:56 +00004152 break;
4153}
4154
4155/* Opcode: SortMakeRec P1 * *
4156**
4157** The top P1 elements are the arguments to a callback. Form these
4158** elements into a single data entry that can be stored on a sorter
4159** using SortPut and later fed to a callback using SortCallback.
4160*/
4161case OP_SortMakeRec: {
4162 char *z;
4163 char **azArg;
4164 int nByte;
4165 int nField;
drh6810ce62004-01-31 19:22:56 +00004166 int i;
4167 Mem *pRec;
drh5e00f6c2001-09-13 13:46:56 +00004168
4169 nField = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004170 pRec = &pTos[1-nField];
4171 assert( pRec>=p->aStack );
drh5e00f6c2001-09-13 13:46:56 +00004172 nByte = 0;
drh6810ce62004-01-31 19:22:56 +00004173 for(i=0; i<nField; i++, pRec++){
4174 if( (pRec->flags & MEM_Null)==0 ){
4175 Stringify(pRec);
4176 nByte += pRec->n;
drh5e00f6c2001-09-13 13:46:56 +00004177 }
4178 }
4179 nByte += sizeof(char*)*(nField+1);
drh8c1238a2003-01-02 14:43:55 +00004180 azArg = sqliteMallocRaw( nByte );
drh5e00f6c2001-09-13 13:46:56 +00004181 if( azArg==0 ) goto no_mem;
4182 z = (char*)&azArg[nField+1];
drh6810ce62004-01-31 19:22:56 +00004183 for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
4184 if( pRec->flags & MEM_Null ){
4185 azArg[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004186 }else{
drh6810ce62004-01-31 19:22:56 +00004187 azArg[i] = z;
4188 memcpy(z, pRec->z, pRec->n);
4189 z += pRec->n;
drh5e00f6c2001-09-13 13:46:56 +00004190 }
4191 }
drh6810ce62004-01-31 19:22:56 +00004192 popStack(&pTos, nField);
4193 pTos++;
4194 pTos->n = nByte;
4195 pTos->z = (char*)azArg;
4196 pTos->flags = MEM_Str | MEM_Dyn;
drh5e00f6c2001-09-13 13:46:56 +00004197 break;
4198}
4199
drha8b38d22001-11-01 14:41:34 +00004200/* Opcode: SortMakeKey * * P3
drh5e00f6c2001-09-13 13:46:56 +00004201**
4202** Convert the top few entries of the stack into a sort key. The
4203** number of stack entries consumed is the number of characters in
4204** the string P3. One character from P3 is prepended to each entry.
4205** The first character of P3 is prepended to the element lowest in
drhda30d362002-08-26 19:55:07 +00004206** the stack and the last character of P3 is prepended to the top of
drh5e00f6c2001-09-13 13:46:56 +00004207** the stack. All stack entries are separated by a \000 character
4208** in the result. The whole key is terminated by two \000 characters
4209** in a row.
4210**
drhda30d362002-08-26 19:55:07 +00004211** "N" is substituted in place of the P3 character for NULL values.
4212**
drhb19a2bc2001-09-16 00:13:26 +00004213** See also the MakeKey and MakeIdxKey opcodes.
drh5e00f6c2001-09-13 13:46:56 +00004214*/
4215case OP_SortMakeKey: {
4216 char *zNewKey;
4217 int nByte;
4218 int nField;
4219 int i, j, k;
drh6810ce62004-01-31 19:22:56 +00004220 Mem *pRec;
drh5e00f6c2001-09-13 13:46:56 +00004221
4222 nField = strlen(pOp->p3);
drh6810ce62004-01-31 19:22:56 +00004223 pRec = &pTos[1-nField];
drh5e00f6c2001-09-13 13:46:56 +00004224 nByte = 1;
drh6810ce62004-01-31 19:22:56 +00004225 for(i=0; i<nField; i++, pRec++){
4226 if( pRec->flags & MEM_Null ){
drhda30d362002-08-26 19:55:07 +00004227 nByte += 2;
4228 }else{
drh6810ce62004-01-31 19:22:56 +00004229 Stringify(pRec);
4230 nByte += pRec->n+2;
drhda30d362002-08-26 19:55:07 +00004231 }
drh5e00f6c2001-09-13 13:46:56 +00004232 }
drh8c1238a2003-01-02 14:43:55 +00004233 zNewKey = sqliteMallocRaw( nByte );
drh5e00f6c2001-09-13 13:46:56 +00004234 if( zNewKey==0 ) goto no_mem;
4235 j = 0;
4236 k = 0;
drh6810ce62004-01-31 19:22:56 +00004237 for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
4238 if( pRec->flags & MEM_Null ){
drhda30d362002-08-26 19:55:07 +00004239 zNewKey[j++] = 'N';
4240 zNewKey[j++] = 0;
4241 k++;
4242 }else{
4243 zNewKey[j++] = pOp->p3[k++];
drh6810ce62004-01-31 19:22:56 +00004244 memcpy(&zNewKey[j], pRec->z, pRec->n-1);
4245 j += pRec->n-1;
drhda30d362002-08-26 19:55:07 +00004246 zNewKey[j++] = 0;
4247 }
drh5e00f6c2001-09-13 13:46:56 +00004248 }
4249 zNewKey[j] = 0;
drh6d4abfb2001-10-22 02:58:08 +00004250 assert( j<nByte );
drh6810ce62004-01-31 19:22:56 +00004251 popStack(&pTos, nField);
4252 pTos++;
4253 pTos->n = nByte;
4254 pTos->flags = MEM_Str|MEM_Dyn;
4255 pTos->z = zNewKey;
drh5e00f6c2001-09-13 13:46:56 +00004256 break;
4257}
4258
drha8b38d22001-11-01 14:41:34 +00004259/* Opcode: Sort * * *
drh5e00f6c2001-09-13 13:46:56 +00004260**
drha8b38d22001-11-01 14:41:34 +00004261** Sort all elements on the sorter. The algorithm is a
drh5e00f6c2001-09-13 13:46:56 +00004262** mergesort.
4263*/
4264case OP_Sort: {
drha8b38d22001-11-01 14:41:34 +00004265 int i;
4266 Sorter *pElem;
4267 Sorter *apSorter[NSORT];
4268 for(i=0; i<NSORT; i++){
4269 apSorter[i] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004270 }
drha8b38d22001-11-01 14:41:34 +00004271 while( p->pSort ){
4272 pElem = p->pSort;
4273 p->pSort = pElem->pNext;
4274 pElem->pNext = 0;
4275 for(i=0; i<NSORT-1; i++){
4276 if( apSorter[i]==0 ){
4277 apSorter[i] = pElem;
4278 break;
4279 }else{
4280 pElem = Merge(apSorter[i], pElem);
4281 apSorter[i] = 0;
4282 }
4283 }
4284 if( i>=NSORT-1 ){
4285 apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem);
4286 }
4287 }
4288 pElem = 0;
4289 for(i=0; i<NSORT; i++){
4290 pElem = Merge(apSorter[i], pElem);
4291 }
4292 p->pSort = pElem;
drh5e00f6c2001-09-13 13:46:56 +00004293 break;
4294}
4295
drha8b38d22001-11-01 14:41:34 +00004296/* Opcode: SortNext * P2 *
drh5e00f6c2001-09-13 13:46:56 +00004297**
drha8b38d22001-11-01 14:41:34 +00004298** Push the data for the topmost element in the sorter onto the
4299** stack, then remove the element from the sorter. If the sorter
4300** is empty, push nothing on the stack and instead jump immediately
4301** to instruction P2.
drh5e00f6c2001-09-13 13:46:56 +00004302*/
4303case OP_SortNext: {
drha8b38d22001-11-01 14:41:34 +00004304 Sorter *pSorter = p->pSort;
drhcaec2f12003-01-07 02:47:47 +00004305 CHECK_FOR_INTERRUPT;
drha8b38d22001-11-01 14:41:34 +00004306 if( pSorter!=0 ){
4307 p->pSort = pSorter->pNext;
drh6810ce62004-01-31 19:22:56 +00004308 pTos++;
4309 pTos->z = pSorter->pData;
4310 pTos->n = pSorter->nData;
4311 pTos->flags = MEM_Str|MEM_Dyn;
drh5e00f6c2001-09-13 13:46:56 +00004312 sqliteFree(pSorter->zKey);
4313 sqliteFree(pSorter);
4314 }else{
4315 pc = pOp->p2 - 1;
4316 }
4317 break;
4318}
4319
drhdf199a22002-06-14 22:38:41 +00004320/* Opcode: SortCallback P1 * *
drh5e00f6c2001-09-13 13:46:56 +00004321**
4322** The top of the stack contains a callback record built using
4323** the SortMakeRec operation with the same P1 value as this
4324** instruction. Pop this record from the stack and invoke the
4325** callback on it.
4326*/
4327case OP_SortCallback: {
drh6810ce62004-01-31 19:22:56 +00004328 assert( pTos>=p->aStack );
4329 assert( pTos->flags & MEM_Str );
drha1f9b5e2004-02-14 16:31:02 +00004330 p->nCallback++;
drh826fb5a2004-02-14 23:59:57 +00004331 p->pc = pc+1;
4332 p->azResColumn = (char**)pTos->z;
drhd6502752004-02-16 03:44:01 +00004333 assert( p->nResColumn==pOp->p1 );
drh826fb5a2004-02-14 23:59:57 +00004334 p->popStack = 1;
4335 p->pTos = pTos;
4336 return SQLITE_ROW;
drh5e00f6c2001-09-13 13:46:56 +00004337}
4338
drha8b38d22001-11-01 14:41:34 +00004339/* Opcode: SortReset * * *
drh5e00f6c2001-09-13 13:46:56 +00004340**
drha8b38d22001-11-01 14:41:34 +00004341** Remove any elements that remain on the sorter.
drh5e00f6c2001-09-13 13:46:56 +00004342*/
drha8b38d22001-11-01 14:41:34 +00004343case OP_SortReset: {
danielk19774adee202004-05-08 08:23:19 +00004344 sqlite3VdbeSorterReset(p);
drh5e00f6c2001-09-13 13:46:56 +00004345 break;
4346}
4347
4348/* Opcode: FileOpen * * P3
4349**
4350** Open the file named by P3 for reading using the FileRead opcode.
4351** If P3 is "stdin" then open standard input for reading.
4352*/
4353case OP_FileOpen: {
drh6810ce62004-01-31 19:22:56 +00004354 assert( pOp->p3!=0 );
drh5e00f6c2001-09-13 13:46:56 +00004355 if( p->pFile ){
4356 if( p->pFile!=stdin ) fclose(p->pFile);
4357 p->pFile = 0;
4358 }
danielk19774adee202004-05-08 08:23:19 +00004359 if( sqlite3StrICmp(pOp->p3,"stdin")==0 ){
drh5e00f6c2001-09-13 13:46:56 +00004360 p->pFile = stdin;
4361 }else{
4362 p->pFile = fopen(pOp->p3, "r");
4363 }
4364 if( p->pFile==0 ){
danielk19774adee202004-05-08 08:23:19 +00004365 sqlite3SetString(&p->zErrMsg,"unable to open file: ", pOp->p3, (char*)0);
drh5e00f6c2001-09-13 13:46:56 +00004366 rc = SQLITE_ERROR;
drh5e00f6c2001-09-13 13:46:56 +00004367 }
4368 break;
4369}
4370
drh5e00f6c2001-09-13 13:46:56 +00004371/* Opcode: FileRead P1 P2 P3
4372**
4373** Read a single line of input from the open file (the file opened using
4374** FileOpen). If we reach end-of-file, jump immediately to P2. If
4375** we are able to get another line, split the line apart using P3 as
4376** a delimiter. There should be P1 fields. If the input line contains
4377** more than P1 fields, ignore the excess. If the input line contains
drhb419a922002-01-30 16:17:23 +00004378** fewer than P1 fields, assume the remaining fields contain NULLs.
4379**
4380** Input ends if a line consists of just "\.". A field containing only
4381** "\N" is a null field. The backslash \ character can be used be used
4382** to escape newlines or the delimiter.
drh5e00f6c2001-09-13 13:46:56 +00004383*/
4384case OP_FileRead: {
4385 int n, eol, nField, i, c, nDelim;
4386 char *zDelim, *z;
drhcaec2f12003-01-07 02:47:47 +00004387 CHECK_FOR_INTERRUPT;
drh5e00f6c2001-09-13 13:46:56 +00004388 if( p->pFile==0 ) goto fileread_jump;
4389 nField = pOp->p1;
4390 if( nField<=0 ) goto fileread_jump;
4391 if( nField!=p->nField || p->azField==0 ){
drh6d4abfb2001-10-22 02:58:08 +00004392 char **azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1);
4393 if( azField==0 ){ goto no_mem; }
4394 p->azField = azField;
drh5e00f6c2001-09-13 13:46:56 +00004395 p->nField = nField;
4396 }
4397 n = 0;
4398 eol = 0;
4399 while( eol==0 ){
4400 if( p->zLine==0 || n+200>p->nLineAlloc ){
drh6d4abfb2001-10-22 02:58:08 +00004401 char *zLine;
drh5e00f6c2001-09-13 13:46:56 +00004402 p->nLineAlloc = p->nLineAlloc*2 + 300;
drh6d4abfb2001-10-22 02:58:08 +00004403 zLine = sqliteRealloc(p->zLine, p->nLineAlloc);
4404 if( zLine==0 ){
drh5e00f6c2001-09-13 13:46:56 +00004405 p->nLineAlloc = 0;
drh6d4abfb2001-10-22 02:58:08 +00004406 sqliteFree(p->zLine);
4407 p->zLine = 0;
4408 goto no_mem;
drh5e00f6c2001-09-13 13:46:56 +00004409 }
drh6d4abfb2001-10-22 02:58:08 +00004410 p->zLine = zLine;
drh5e00f6c2001-09-13 13:46:56 +00004411 }
drh62160e72002-07-30 17:20:40 +00004412 if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00004413 eol = 1;
4414 p->zLine[n] = 0;
4415 }else{
drhb419a922002-01-30 16:17:23 +00004416 int c;
4417 while( (c = p->zLine[n])!=0 ){
4418 if( c=='\\' ){
4419 if( p->zLine[n+1]==0 ) break;
4420 n += 2;
4421 }else if( c=='\n' ){
4422 p->zLine[n] = 0;
4423 eol = 1;
4424 break;
4425 }else{
4426 n++;
4427 }
drh5e00f6c2001-09-13 13:46:56 +00004428 }
4429 }
4430 }
4431 if( n==0 ) goto fileread_jump;
4432 z = p->zLine;
4433 if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){
4434 goto fileread_jump;
4435 }
4436 zDelim = pOp->p3;
4437 if( zDelim==0 ) zDelim = "\t";
4438 c = zDelim[0];
4439 nDelim = strlen(zDelim);
4440 p->azField[0] = z;
4441 for(i=1; *z!=0 && i<=nField; i++){
4442 int from, to;
4443 from = to = 0;
drhb419a922002-01-30 16:17:23 +00004444 if( z[0]=='\\' && z[1]=='N'
4445 && (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){
4446 if( i<=nField ) p->azField[i-1] = 0;
4447 z += 2 + nDelim;
4448 if( i<nField ) p->azField[i] = z;
4449 continue;
4450 }
drh5e00f6c2001-09-13 13:46:56 +00004451 while( z[from] ){
4452 if( z[from]=='\\' && z[from+1]!=0 ){
drh37750842003-09-27 00:56:31 +00004453 int tx = z[from+1];
4454 switch( tx ){
4455 case 'b': tx = '\b'; break;
4456 case 'f': tx = '\f'; break;
4457 case 'n': tx = '\n'; break;
4458 case 'r': tx = '\r'; break;
4459 case 't': tx = '\t'; break;
4460 case 'v': tx = '\v'; break;
4461 default: break;
4462 }
4463 z[to++] = tx;
drh5e00f6c2001-09-13 13:46:56 +00004464 from += 2;
4465 continue;
4466 }
4467 if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break;
4468 z[to++] = z[from++];
4469 }
4470 if( z[from] ){
4471 z[to] = 0;
4472 z += from + nDelim;
4473 if( i<nField ) p->azField[i] = z;
4474 }else{
4475 z[to] = 0;
4476 z = "";
4477 }
4478 }
4479 while( i<nField ){
drhb419a922002-01-30 16:17:23 +00004480 p->azField[i++] = 0;
drh5e00f6c2001-09-13 13:46:56 +00004481 }
4482 break;
4483
4484 /* If we reach end-of-file, or if anything goes wrong, jump here.
4485 ** This code will cause a jump to P2 */
4486fileread_jump:
4487 pc = pOp->p2 - 1;
4488 break;
4489}
4490
drhbe0072d2001-09-13 14:46:09 +00004491/* Opcode: FileColumn P1 * *
drh5e00f6c2001-09-13 13:46:56 +00004492**
drhb19a2bc2001-09-16 00:13:26 +00004493** Push onto the stack the P1-th column of the most recently read line
drh5e00f6c2001-09-13 13:46:56 +00004494** from the input file.
4495*/
drhbe0072d2001-09-13 14:46:09 +00004496case OP_FileColumn: {
drh5e00f6c2001-09-13 13:46:56 +00004497 int i = pOp->p1;
4498 char *z;
drh6810ce62004-01-31 19:22:56 +00004499 assert( i>=0 && i<p->nField );
4500 if( p->azField ){
drh5e00f6c2001-09-13 13:46:56 +00004501 z = p->azField[i];
4502 }else{
4503 z = 0;
4504 }
drh6810ce62004-01-31 19:22:56 +00004505 pTos++;
drhb419a922002-01-30 16:17:23 +00004506 if( z ){
drh6810ce62004-01-31 19:22:56 +00004507 pTos->n = strlen(z) + 1;
4508 pTos->z = z;
4509 pTos->flags = MEM_Str | MEM_Ephem;
drhb419a922002-01-30 16:17:23 +00004510 }else{
drh6810ce62004-01-31 19:22:56 +00004511 pTos->flags = MEM_Null;
drhb419a922002-01-30 16:17:23 +00004512 }
drh5e00f6c2001-09-13 13:46:56 +00004513 break;
4514}
4515
drh8721ce42001-11-07 14:22:00 +00004516/* Opcode: MemStore P1 P2 *
drh5e00f6c2001-09-13 13:46:56 +00004517**
drh8721ce42001-11-07 14:22:00 +00004518** Write the top of the stack into memory location P1.
4519** P1 should be a small integer since space is allocated
drh5e00f6c2001-09-13 13:46:56 +00004520** for all memory locations between 0 and P1 inclusive.
drh8721ce42001-11-07 14:22:00 +00004521**
4522** After the data is stored in the memory location, the
4523** stack is popped once if P2 is 1. If P2 is zero, then
4524** the original data remains on the stack.
drh5e00f6c2001-09-13 13:46:56 +00004525*/
4526case OP_MemStore: {
4527 int i = pOp->p1;
drh9bbca4c2001-11-06 04:00:18 +00004528 Mem *pMem;
drh6810ce62004-01-31 19:22:56 +00004529 assert( pTos>=p->aStack );
drh5e00f6c2001-09-13 13:46:56 +00004530 if( i>=p->nMem ){
4531 int nOld = p->nMem;
drh6d4abfb2001-10-22 02:58:08 +00004532 Mem *aMem;
drh5e00f6c2001-09-13 13:46:56 +00004533 p->nMem = i + 5;
drh6d4abfb2001-10-22 02:58:08 +00004534 aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
4535 if( aMem==0 ) goto no_mem;
drh3e56c042002-09-17 03:20:46 +00004536 if( aMem!=p->aMem ){
4537 int j;
4538 for(j=0; j<nOld; j++){
drh6810ce62004-01-31 19:22:56 +00004539 if( aMem[j].flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004540 aMem[j].z = aMem[j].zShort;
drh3e56c042002-09-17 03:20:46 +00004541 }
4542 }
4543 }
drh6d4abfb2001-10-22 02:58:08 +00004544 p->aMem = aMem;
drh5e00f6c2001-09-13 13:46:56 +00004545 if( nOld<p->nMem ){
4546 memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
4547 }
4548 }
drh2c79c672004-01-31 20:20:29 +00004549 Deephemeralize(pTos);
drh5e00f6c2001-09-13 13:46:56 +00004550 pMem = &p->aMem[i];
drh2c79c672004-01-31 20:20:29 +00004551 Release(pMem);
drh6810ce62004-01-31 19:22:56 +00004552 *pMem = *pTos;
drh2c79c672004-01-31 20:20:29 +00004553 if( pMem->flags & MEM_Dyn ){
drh6810ce62004-01-31 19:22:56 +00004554 if( pOp->p2 ){
4555 pTos->flags = MEM_Null;
4556 }else{
drh00706be2004-01-30 14:49:16 +00004557 pMem->z = sqliteMallocRaw( pMem->n );
drh6b563442001-11-07 16:48:26 +00004558 if( pMem->z==0 ) goto no_mem;
drh6810ce62004-01-31 19:22:56 +00004559 memcpy(pMem->z, pTos->z, pMem->n);
drh8721ce42001-11-07 14:22:00 +00004560 }
drh2c79c672004-01-31 20:20:29 +00004561 }else if( pMem->flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004562 pMem->z = pMem->zShort;
drh5e00f6c2001-09-13 13:46:56 +00004563 }
drh8721ce42001-11-07 14:22:00 +00004564 if( pOp->p2 ){
drh6810ce62004-01-31 19:22:56 +00004565 Release(pTos);
4566 pTos--;
drh8721ce42001-11-07 14:22:00 +00004567 }
drh5e00f6c2001-09-13 13:46:56 +00004568 break;
4569}
4570
4571/* Opcode: MemLoad P1 * *
4572**
4573** Push a copy of the value in memory location P1 onto the stack.
drh8721ce42001-11-07 14:22:00 +00004574**
4575** If the value is a string, then the value pushed is a pointer to
4576** the string that is stored in the memory location. If the memory
4577** location is subsequently changed (using OP_MemStore) then the
4578** value pushed onto the stack will change too.
drh5e00f6c2001-09-13 13:46:56 +00004579*/
4580case OP_MemLoad: {
drh5e00f6c2001-09-13 13:46:56 +00004581 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004582 assert( i>=0 && i<p->nMem );
4583 pTos++;
4584 memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
4585 if( pTos->flags & MEM_Str ){
4586 pTos->flags |= MEM_Ephem;
4587 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
drh5e00f6c2001-09-13 13:46:56 +00004588 }
4589 break;
4590}
4591
drhd11d3822002-06-21 23:01:49 +00004592/* Opcode: MemIncr P1 P2 *
4593**
4594** Increment the integer valued memory cell P1 by 1. If P2 is not zero
4595** and the result after the increment is greater than zero, then jump
4596** to P2.
4597**
4598** This instruction throws an error if the memory cell is not initially
4599** an integer.
4600*/
4601case OP_MemIncr: {
4602 int i = pOp->p1;
4603 Mem *pMem;
drh6810ce62004-01-31 19:22:56 +00004604 assert( i>=0 && i<p->nMem );
drhd11d3822002-06-21 23:01:49 +00004605 pMem = &p->aMem[i];
drh6810ce62004-01-31 19:22:56 +00004606 assert( pMem->flags==MEM_Int );
drh00706be2004-01-30 14:49:16 +00004607 pMem->i++;
4608 if( pOp->p2>0 && pMem->i>0 ){
drhd11d3822002-06-21 23:01:49 +00004609 pc = pOp->p2 - 1;
4610 }
4611 break;
4612}
4613
drh5e00f6c2001-09-13 13:46:56 +00004614/* Opcode: AggReset * P2 *
4615**
4616** Reset the aggregator so that it no longer contains any data.
4617** Future aggregator elements will contain P2 values each.
4618*/
4619case OP_AggReset: {
danielk19774adee202004-05-08 08:23:19 +00004620 sqlite3VdbeAggReset(&p->agg);
drh5e00f6c2001-09-13 13:46:56 +00004621 p->agg.nMem = pOp->p2;
drh1350b032002-02-27 19:00:20 +00004622 p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
drhcaec2f12003-01-07 02:47:47 +00004623 if( p->agg.apFunc==0 ) goto no_mem;
drhe5095352002-02-24 03:25:14 +00004624 break;
4625}
4626
drh1350b032002-02-27 19:00:20 +00004627/* Opcode: AggInit * P2 P3
drhe5095352002-02-24 03:25:14 +00004628**
drh0bce8352002-02-28 00:41:10 +00004629** Initialize the function parameters for an aggregate function.
4630** The aggregate will operate out of aggregate column P2.
4631** P3 is a pointer to the FuncDef structure for the function.
drhe5095352002-02-24 03:25:14 +00004632*/
drh1350b032002-02-27 19:00:20 +00004633case OP_AggInit: {
drhe5095352002-02-24 03:25:14 +00004634 int i = pOp->p2;
drh6810ce62004-01-31 19:22:56 +00004635 assert( i>=0 && i<p->agg.nMem );
drh0bce8352002-02-28 00:41:10 +00004636 p->agg.apFunc[i] = (FuncDef*)pOp->p3;
drhe5095352002-02-24 03:25:14 +00004637 break;
4638}
4639
4640/* Opcode: AggFunc * P2 P3
4641**
drh0bce8352002-02-28 00:41:10 +00004642** Execute the step function for an aggregate. The
4643** function has P2 arguments. P3 is a pointer to the FuncDef
4644** structure that specifies the function.
drhe5095352002-02-24 03:25:14 +00004645**
drh1350b032002-02-27 19:00:20 +00004646** The top of the stack must be an integer which is the index of
4647** the aggregate column that corresponds to this aggregate function.
4648** Ideally, this index would be another parameter, but there are
4649** no free parameters left. The integer is popped from the stack.
drhe5095352002-02-24 03:25:14 +00004650*/
4651case OP_AggFunc: {
4652 int n = pOp->p2;
4653 int i;
drh6810ce62004-01-31 19:22:56 +00004654 Mem *pMem, *pRec;
4655 char **azArgv = p->zArgv;
drh1350b032002-02-27 19:00:20 +00004656 sqlite_func ctx;
drhe5095352002-02-24 03:25:14 +00004657
drh6810ce62004-01-31 19:22:56 +00004658 assert( n>=0 );
4659 assert( pTos->flags==MEM_Int );
4660 pRec = &pTos[-n];
4661 assert( pRec>=p->aStack );
4662 for(i=0; i<n; i++, pRec++){
4663 if( pRec->flags & MEM_Null ){
4664 azArgv[i] = 0;
drhf5905aa2002-05-26 20:54:33 +00004665 }else{
drh6810ce62004-01-31 19:22:56 +00004666 Stringify(pRec);
4667 azArgv[i] = pRec->z;
drhe5095352002-02-24 03:25:14 +00004668 }
4669 }
drh6810ce62004-01-31 19:22:56 +00004670 i = pTos->i;
4671 assert( i>=0 && i<p->agg.nMem );
drh0bce8352002-02-28 00:41:10 +00004672 ctx.pFunc = (FuncDef*)pOp->p3;
drh1350b032002-02-27 19:00:20 +00004673 pMem = &p->agg.pCurrent->aMem[i];
drh00706be2004-01-30 14:49:16 +00004674 ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */
drh1350b032002-02-27 19:00:20 +00004675 ctx.pAgg = pMem->z;
drh00706be2004-01-30 14:49:16 +00004676 ctx.cnt = ++pMem->i;
drh1350b032002-02-27 19:00:20 +00004677 ctx.isError = 0;
4678 ctx.isStep = 1;
drh6810ce62004-01-31 19:22:56 +00004679 (ctx.pFunc->xStep)(&ctx, n, (const char**)azArgv);
drh1350b032002-02-27 19:00:20 +00004680 pMem->z = ctx.pAgg;
drh00706be2004-01-30 14:49:16 +00004681 pMem->flags = MEM_AggCtx;
drh6810ce62004-01-31 19:22:56 +00004682 popStack(&pTos, n+1);
drh1350b032002-02-27 19:00:20 +00004683 if( ctx.isError ){
4684 rc = SQLITE_ERROR;
4685 }
drh5e00f6c2001-09-13 13:46:56 +00004686 break;
4687}
4688
4689/* Opcode: AggFocus * P2 *
4690**
4691** Pop the top of the stack and use that as an aggregator key. If
4692** an aggregator with that same key already exists, then make the
4693** aggregator the current aggregator and jump to P2. If no aggregator
4694** with the given key exists, create one and make it current but
4695** do not jump.
4696**
4697** The order of aggregator opcodes is important. The order is:
4698** AggReset AggFocus AggNext. In other words, you must execute
4699** AggReset first, then zero or more AggFocus operations, then
4700** zero or more AggNext operations. You must not execute an AggFocus
4701** in between an AggNext and an AggReset.
4702*/
4703case OP_AggFocus: {
drh5e00f6c2001-09-13 13:46:56 +00004704 AggElem *pElem;
4705 char *zKey;
4706 int nKey;
4707
drh6810ce62004-01-31 19:22:56 +00004708 assert( pTos>=p->aStack );
4709 Stringify(pTos);
4710 zKey = pTos->z;
4711 nKey = pTos->n;
danielk19774adee202004-05-08 08:23:19 +00004712 pElem = sqlite3HashFind(&p->agg.hash, zKey, nKey);
drh5e00f6c2001-09-13 13:46:56 +00004713 if( pElem ){
4714 p->agg.pCurrent = pElem;
4715 pc = pOp->p2 - 1;
4716 }else{
drhdb5ed6d2001-09-18 22:17:44 +00004717 AggInsert(&p->agg, zKey, nKey);
danielk19776f8a5032004-05-10 10:34:51 +00004718 if( sqlite3_malloc_failed ) goto no_mem;
drh5e00f6c2001-09-13 13:46:56 +00004719 }
drh6810ce62004-01-31 19:22:56 +00004720 Release(pTos);
4721 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004722 break;
4723}
4724
drh5e00f6c2001-09-13 13:46:56 +00004725/* Opcode: AggSet * P2 *
4726**
4727** Move the top of the stack into the P2-th field of the current
4728** aggregate. String values are duplicated into new memory.
4729*/
4730case OP_AggSet: {
4731 AggElem *pFocus = AggInFocus(p->agg);
drh2c79c672004-01-31 20:20:29 +00004732 Mem *pMem;
drh5e00f6c2001-09-13 13:46:56 +00004733 int i = pOp->p2;
drh6810ce62004-01-31 19:22:56 +00004734 assert( pTos>=p->aStack );
drh5e00f6c2001-09-13 13:46:56 +00004735 if( pFocus==0 ) goto no_mem;
drh2c79c672004-01-31 20:20:29 +00004736 assert( i>=0 && i<p->agg.nMem );
4737 Deephemeralize(pTos);
4738 pMem = &pFocus->aMem[i];
4739 Release(pMem);
4740 *pMem = *pTos;
4741 if( pMem->flags & MEM_Dyn ){
4742 pTos->flags = MEM_Null;
4743 }else if( pMem->flags & MEM_Short ){
4744 pMem->z = pMem->zShort;
drh5e00f6c2001-09-13 13:46:56 +00004745 }
drh6810ce62004-01-31 19:22:56 +00004746 Release(pTos);
4747 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004748 break;
4749}
4750
4751/* Opcode: AggGet * P2 *
4752**
4753** Push a new entry onto the stack which is a copy of the P2-th field
4754** of the current aggregate. Strings are not duplicated so
4755** string values will be ephemeral.
4756*/
4757case OP_AggGet: {
4758 AggElem *pFocus = AggInFocus(p->agg);
drh2c79c672004-01-31 20:20:29 +00004759 Mem *pMem;
drh5e00f6c2001-09-13 13:46:56 +00004760 int i = pOp->p2;
drh5e00f6c2001-09-13 13:46:56 +00004761 if( pFocus==0 ) goto no_mem;
drh2c79c672004-01-31 20:20:29 +00004762 assert( i>=0 && i<p->agg.nMem );
drh6810ce62004-01-31 19:22:56 +00004763 pTos++;
drh2c79c672004-01-31 20:20:29 +00004764 pMem = &pFocus->aMem[i];
4765 *pTos = *pMem;
drh3914aed2004-01-31 20:40:42 +00004766 if( pTos->flags & MEM_Str ){
4767 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4768 pTos->flags |= MEM_Ephem;
4769 }
drh5e00f6c2001-09-13 13:46:56 +00004770 break;
4771}
4772
4773/* Opcode: AggNext * P2 *
4774**
4775** Make the next aggregate value the current aggregate. The prior
4776** aggregate is deleted. If all aggregate values have been consumed,
4777** jump to P2.
4778**
4779** The order of aggregator opcodes is important. The order is:
4780** AggReset AggFocus AggNext. In other words, you must execute
4781** AggReset first, then zero or more AggFocus operations, then
4782** zero or more AggNext operations. You must not execute an AggFocus
4783** in between an AggNext and an AggReset.
4784*/
4785case OP_AggNext: {
drhcaec2f12003-01-07 02:47:47 +00004786 CHECK_FOR_INTERRUPT;
drhbeae3192001-09-22 18:12:08 +00004787 if( p->agg.pSearch==0 ){
4788 p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
4789 }else{
4790 p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
drh5e00f6c2001-09-13 13:46:56 +00004791 }
drhbeae3192001-09-22 18:12:08 +00004792 if( p->agg.pSearch==0 ){
4793 pc = pOp->p2 - 1;
4794 } else {
drhe5095352002-02-24 03:25:14 +00004795 int i;
drh1350b032002-02-27 19:00:20 +00004796 sqlite_func ctx;
drhe5095352002-02-24 03:25:14 +00004797 Mem *aMem;
drhbeae3192001-09-22 18:12:08 +00004798 p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
drhe5095352002-02-24 03:25:14 +00004799 aMem = p->agg.pCurrent->aMem;
4800 for(i=0; i<p->agg.nMem; i++){
drh0bce8352002-02-28 00:41:10 +00004801 int freeCtx;
drh1350b032002-02-27 19:00:20 +00004802 if( p->agg.apFunc[i]==0 ) continue;
4803 if( p->agg.apFunc[i]->xFinalize==0 ) continue;
drh00706be2004-01-30 14:49:16 +00004804 ctx.s.flags = MEM_Null;
4805 ctx.s.z = aMem[i].zShort;
drh1350b032002-02-27 19:00:20 +00004806 ctx.pAgg = (void*)aMem[i].z;
drh00706be2004-01-30 14:49:16 +00004807 freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
4808 ctx.cnt = aMem[i].i;
drh1350b032002-02-27 19:00:20 +00004809 ctx.isStep = 0;
4810 ctx.pFunc = p->agg.apFunc[i];
4811 (*p->agg.apFunc[i]->xFinalize)(&ctx);
drh0bce8352002-02-28 00:41:10 +00004812 if( freeCtx ){
4813 sqliteFree( aMem[i].z );
4814 }
drh00706be2004-01-30 14:49:16 +00004815 aMem[i] = ctx.s;
drh6810ce62004-01-31 19:22:56 +00004816 if( aMem[i].flags & MEM_Short ){
drh00706be2004-01-30 14:49:16 +00004817 aMem[i].z = aMem[i].zShort;
drhe5095352002-02-24 03:25:14 +00004818 }
drhe5095352002-02-24 03:25:14 +00004819 }
drh5e00f6c2001-09-13 13:46:56 +00004820 }
4821 break;
4822}
4823
drh5e00f6c2001-09-13 13:46:56 +00004824/* Opcode: SetInsert P1 * P3
4825**
4826** If Set P1 does not exist then create it. Then insert value
4827** P3 into that set. If P3 is NULL, then insert the top of the
4828** stack into the set.
4829*/
4830case OP_SetInsert: {
4831 int i = pOp->p1;
4832 if( p->nSet<=i ){
drhbeae3192001-09-22 18:12:08 +00004833 int k;
drh6d4abfb2001-10-22 02:58:08 +00004834 Set *aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) );
4835 if( aSet==0 ) goto no_mem;
4836 p->aSet = aSet;
drhbeae3192001-09-22 18:12:08 +00004837 for(k=p->nSet; k<=i; k++){
danielk19774adee202004-05-08 08:23:19 +00004838 sqlite3HashInit(&p->aSet[k].hash, SQLITE_HASH_BINARY, 1);
drhbeae3192001-09-22 18:12:08 +00004839 }
drh5e00f6c2001-09-13 13:46:56 +00004840 p->nSet = i+1;
4841 }
4842 if( pOp->p3 ){
danielk19774adee202004-05-08 08:23:19 +00004843 sqlite3HashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p);
drh5e00f6c2001-09-13 13:46:56 +00004844 }else{
drh6810ce62004-01-31 19:22:56 +00004845 assert( pTos>=p->aStack );
4846 Stringify(pTos);
danielk19774adee202004-05-08 08:23:19 +00004847 sqlite3HashInsert(&p->aSet[i].hash, pTos->z, pTos->n, p);
drh6810ce62004-01-31 19:22:56 +00004848 Release(pTos);
4849 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004850 }
danielk19776f8a5032004-05-10 10:34:51 +00004851 if( sqlite3_malloc_failed ) goto no_mem;
drh5e00f6c2001-09-13 13:46:56 +00004852 break;
4853}
4854
4855/* Opcode: SetFound P1 P2 *
4856**
4857** Pop the stack once and compare the value popped off with the
4858** contents of set P1. If the element popped exists in set P1,
4859** then jump to P2. Otherwise fall through.
4860*/
4861case OP_SetFound: {
4862 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004863 assert( pTos>=p->aStack );
4864 Stringify(pTos);
danielk19774adee202004-05-08 08:23:19 +00004865 if( i>=0 && i<p->nSet && sqlite3HashFind(&p->aSet[i].hash, pTos->z, pTos->n)){
drh5e00f6c2001-09-13 13:46:56 +00004866 pc = pOp->p2 - 1;
4867 }
drh6810ce62004-01-31 19:22:56 +00004868 Release(pTos);
4869 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004870 break;
4871}
4872
4873/* Opcode: SetNotFound P1 P2 *
4874**
4875** Pop the stack once and compare the value popped off with the
4876** contents of set P1. If the element popped does not exists in
4877** set P1, then jump to P2. Otherwise fall through.
4878*/
4879case OP_SetNotFound: {
4880 int i = pOp->p1;
drh6810ce62004-01-31 19:22:56 +00004881 assert( pTos>=p->aStack );
4882 Stringify(pTos);
drh38dd0b42002-10-30 22:42:58 +00004883 if( i<0 || i>=p->nSet ||
danielk19774adee202004-05-08 08:23:19 +00004884 sqlite3HashFind(&p->aSet[i].hash, pTos->z, pTos->n)==0 ){
drh5e00f6c2001-09-13 13:46:56 +00004885 pc = pOp->p2 - 1;
4886 }
drh6810ce62004-01-31 19:22:56 +00004887 Release(pTos);
4888 pTos--;
drh5e00f6c2001-09-13 13:46:56 +00004889 break;
4890}
4891
drhd99f7062002-06-08 23:25:08 +00004892/* Opcode: SetFirst P1 P2 *
4893**
4894** Read the first element from set P1 and push it onto the stack. If the
4895** set is empty, push nothing and jump immediately to P2. This opcode is
4896** used in combination with OP_SetNext to loop over all elements of a set.
4897*/
4898/* Opcode: SetNext P1 P2 *
4899**
4900** Read the next element from set P1 and push it onto the stack. If there
4901** are no more elements in the set, do not do the push and fall through.
4902** Otherwise, jump to P2 after pushing the next set element.
4903*/
4904case OP_SetFirst:
4905case OP_SetNext: {
4906 Set *pSet;
drhcaec2f12003-01-07 02:47:47 +00004907 CHECK_FOR_INTERRUPT;
drh38dd0b42002-10-30 22:42:58 +00004908 if( pOp->p1<0 || pOp->p1>=p->nSet ){
4909 if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1;
4910 break;
4911 }
drhd99f7062002-06-08 23:25:08 +00004912 pSet = &p->aSet[pOp->p1];
4913 if( pOp->opcode==OP_SetFirst ){
4914 pSet->prev = sqliteHashFirst(&pSet->hash);
4915 if( pSet->prev==0 ){
4916 pc = pOp->p2 - 1;
4917 break;
4918 }
4919 }else{
drh6810ce62004-01-31 19:22:56 +00004920 assert( pSet->prev );
drhd99f7062002-06-08 23:25:08 +00004921 pSet->prev = sqliteHashNext(pSet->prev);
4922 if( pSet->prev==0 ){
4923 break;
4924 }else{
4925 pc = pOp->p2 - 1;
4926 }
4927 }
drh6810ce62004-01-31 19:22:56 +00004928 pTos++;
4929 pTos->z = sqliteHashKey(pSet->prev);
4930 pTos->n = sqliteHashKeysize(pSet->prev);
4931 pTos->flags = MEM_Str | MEM_Ephem;
drhd99f7062002-06-08 23:25:08 +00004932 break;
4933}
drh5e00f6c2001-09-13 13:46:56 +00004934
drh6f8c91c2003-12-07 00:24:35 +00004935/* Opcode: Vacuum * * *
4936**
4937** Vacuum the entire database. This opcode will cause other virtual
4938** machines to be created and run. It may not be called from within
4939** a transaction.
4940*/
4941case OP_Vacuum: {
danielk19774adee202004-05-08 08:23:19 +00004942 if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4943 rc = sqlite3RunVacuum(&p->zErrMsg, db);
4944 if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
drh6f8c91c2003-12-07 00:24:35 +00004945 break;
4946}
4947
drh5e00f6c2001-09-13 13:46:56 +00004948/* An other opcode is illegal...
4949*/
4950default: {
danielk19776f8a5032004-05-10 10:34:51 +00004951 sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
danielk19774adee202004-05-08 08:23:19 +00004952 sqlite3SetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
drh5e00f6c2001-09-13 13:46:56 +00004953 rc = SQLITE_INTERNAL;
4954 break;
4955}
4956
4957/*****************************************************************************
4958** The cases of the switch statement above this line should all be indented
4959** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4960** readability. From this point on down, the normal indentation rules are
4961** restored.
4962*****************************************************************************/
4963 }
drh6e142f52000-06-08 13:36:40 +00004964
drh7b396862003-01-01 23:06:20 +00004965#ifdef VDBE_PROFILE
drh8178a752003-01-05 21:41:40 +00004966 {
4967 long long elapse = hwtime() - start;
4968 pOp->cycles += elapse;
4969 pOp->cnt++;
4970#if 0
4971 fprintf(stdout, "%10lld ", elapse);
danielk19774adee202004-05-08 08:23:19 +00004972 sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
drh8178a752003-01-05 21:41:40 +00004973#endif
4974 }
drh7b396862003-01-01 23:06:20 +00004975#endif
4976
drh6e142f52000-06-08 13:36:40 +00004977 /* The following code adds nothing to the actual functionality
4978 ** of the program. It is only here for testing and debugging.
4979 ** On the other hand, it does burn CPU cycles every time through
4980 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4981 */
4982#ifndef NDEBUG
drh3914aed2004-01-31 20:40:42 +00004983 /* Sanity checking on the top element of the stack */
4984 if( pTos>=p->aStack ){
4985 assert( pTos->flags!=0 ); /* Must define some type */
4986 if( pTos->flags & MEM_Str ){
4987 int x = pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
4988 assert( x!=0 ); /* Strings must define a string subtype */
4989 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
4990 assert( pTos->z!=0 ); /* Strings must have a value */
4991 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
4992 assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort );
4993 assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort );
4994 }else{
4995 /* Cannot define a string subtype for non-string objects */
4996 assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
4997 }
4998 /* MEM_Null excludes all other types */
4999 assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 );
5000 }
drh58b95762000-06-02 01:17:37 +00005001 if( pc<-1 || pc>=p->nOp ){
danielk19774adee202004-05-08 08:23:19 +00005002 sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0);
drh58b95762000-06-02 01:17:37 +00005003 rc = SQLITE_INTERNAL;
5004 }
drh6810ce62004-01-31 19:22:56 +00005005 if( p->trace && pTos>=p->aStack ){
drh75897232000-05-29 14:26:00 +00005006 int i;
5007 fprintf(p->trace, "Stack:");
drh6810ce62004-01-31 19:22:56 +00005008 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
5009 if( pTos[i].flags & MEM_Null ){
drhc61053b2000-06-04 12:58:36 +00005010 fprintf(p->trace, " NULL");
drh6810ce62004-01-31 19:22:56 +00005011 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
drha3b321d2004-05-11 09:31:31 +00005012 fprintf(p->trace, " si:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00005013 }else if( pTos[i].flags & MEM_Int ){
drha3b321d2004-05-11 09:31:31 +00005014 fprintf(p->trace, " i:%lld", pTos[i].i);
drh6810ce62004-01-31 19:22:56 +00005015 }else if( pTos[i].flags & MEM_Real ){
5016 fprintf(p->trace, " r:%g", pTos[i].r);
5017 }else if( pTos[i].flags & MEM_Str ){
drh092d0352001-09-15 13:15:12 +00005018 int j, k;
5019 char zBuf[100];
5020 zBuf[0] = ' ';
drh6810ce62004-01-31 19:22:56 +00005021 if( pTos[i].flags & MEM_Dyn ){
drh84e6b7b2001-11-12 13:10:52 +00005022 zBuf[1] = 'z';
drh6810ce62004-01-31 19:22:56 +00005023 assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 );
5024 }else if( pTos[i].flags & MEM_Static ){
drh84e6b7b2001-11-12 13:10:52 +00005025 zBuf[1] = 't';
drh6810ce62004-01-31 19:22:56 +00005026 assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
5027 }else if( pTos[i].flags & MEM_Ephem ){
drhe958bb42002-10-22 15:04:34 +00005028 zBuf[1] = 'e';
drh6810ce62004-01-31 19:22:56 +00005029 assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 );
drh84e6b7b2001-11-12 13:10:52 +00005030 }else{
5031 zBuf[1] = 's';
5032 }
drh872ff862001-09-15 14:43:39 +00005033 zBuf[2] = '[';
drh092d0352001-09-15 13:15:12 +00005034 k = 3;
drh6810ce62004-01-31 19:22:56 +00005035 for(j=0; j<20 && j<pTos[i].n; j++){
5036 int c = pTos[i].z[j];
5037 if( c==0 && j==pTos[i].n-1 ) break;
drh092d0352001-09-15 13:15:12 +00005038 if( isprint(c) && !isspace(c) ){
5039 zBuf[k++] = c;
5040 }else{
5041 zBuf[k++] = '.';
5042 }
drhc61053b2000-06-04 12:58:36 +00005043 }
drh872ff862001-09-15 14:43:39 +00005044 zBuf[k++] = ']';
drh092d0352001-09-15 13:15:12 +00005045 zBuf[k++] = 0;
5046 fprintf(p->trace, "%s", zBuf);
drh75897232000-05-29 14:26:00 +00005047 }else{
drhc61053b2000-06-04 12:58:36 +00005048 fprintf(p->trace, " ???");
drh75897232000-05-29 14:26:00 +00005049 }
5050 }
drh7bc09d32002-11-01 01:55:36 +00005051 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
drh75897232000-05-29 14:26:00 +00005052 fprintf(p->trace,"\n");
5053 }
drh6e142f52000-06-08 13:36:40 +00005054#endif
drhb86ccfb2003-01-28 23:13:10 +00005055 } /* The end of the for(;;) loop the loops through opcodes */
drh75897232000-05-29 14:26:00 +00005056
drhb86ccfb2003-01-28 23:13:10 +00005057 /* If we reach this point, it means that execution is finished.
5058 */
5059vdbe_halt:
5060 if( rc ){
5061 p->rc = rc;
5062 rc = SQLITE_ERROR;
5063 }else{
5064 rc = SQLITE_DONE;
5065 }
5066 p->magic = VDBE_MAGIC_HALT;
drh6810ce62004-01-31 19:22:56 +00005067 p->pTos = pTos;
drhb86ccfb2003-01-28 23:13:10 +00005068 return rc;
5069
5070 /* Jump to here if a malloc() fails. It's hard to get a malloc()
5071 ** to fail on a modern VM computer, so this code is untested.
5072 */
5073no_mem:
danielk19774adee202004-05-08 08:23:19 +00005074 sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00005075 rc = SQLITE_NOMEM;
5076 goto vdbe_halt;
5077
5078 /* Jump to here for an SQLITE_MISUSE error.
5079 */
5080abort_due_to_misuse:
5081 rc = SQLITE_MISUSE;
5082 /* Fall thru into abort_due_to_error */
5083
5084 /* Jump to here for any other kind of fatal error. The "rc" variable
5085 ** should hold the error number.
5086 */
5087abort_due_to_error:
drh483750b2003-01-29 18:46:51 +00005088 if( p->zErrMsg==0 ){
danielk19776f8a5032004-05-10 10:34:51 +00005089 if( sqlite3_malloc_failed ) rc = SQLITE_NOMEM;
5090 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drh483750b2003-01-29 18:46:51 +00005091 }
drhb86ccfb2003-01-28 23:13:10 +00005092 goto vdbe_halt;
5093
danielk19776f8a5032004-05-10 10:34:51 +00005094 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
drhb86ccfb2003-01-28 23:13:10 +00005095 ** flag.
5096 */
5097abort_due_to_interrupt:
5098 assert( db->flags & SQLITE_Interrupt );
5099 db->flags &= ~SQLITE_Interrupt;
5100 if( db->magic!=SQLITE_MAGIC_BUSY ){
5101 rc = SQLITE_MISUSE;
5102 }else{
5103 rc = SQLITE_INTERRUPT;
5104 }
danielk19776f8a5032004-05-10 10:34:51 +00005105 sqlite3SetString(&p->zErrMsg, sqlite3_error_string(rc), (char*)0);
drhb86ccfb2003-01-28 23:13:10 +00005106 goto vdbe_halt;
drhb86ccfb2003-01-28 23:13:10 +00005107}