source: trunk/src/3rdparty/sqlite/vdbe.c

Last change on this file was 205, checked in by rudi, 14 years ago

Added SQLite 2.8.17 sources. This allows to build at least one of the sql drivers / plugins.

File size: 140.2 KB
Line 
1/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** 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.
10**
11*************************************************************************
12** 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.
20**
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
31** 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.
34**
35** Most of the code in this file is taken up by the sqliteVdbeExec()
36** 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**
40** 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**
46** $Id: vdbe.c,v 1.268.2.4 2004/10/01 15:11:13 drh Exp $
47*/
48#include "sqliteInt.h"
49#include "os.h"
50#include <ctype.h>
51#include "vdbeInt.h"
52
53/*
54** 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
57** working correctly. This variable has no function other than to
58** help verify the correct operation of the library.
59*/
60int sqlite_search_count = 0;
61
62/*
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 an interrupt.
66**
67** This facility is used for testing purposes only. It does not function
68** in an ordinary build.
69*/
70int sqlite_interrupt_count = 0;
71
72/*
73** Advance the virtual machine to the next output row.
74**
75** The return vale will be either SQLITE_BUSY, SQLITE_DONE,
76** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
77**
78** SQLITE_BUSY means that the virtual machine attempted to open
79** a locked database and there is no busy callback registered.
80** Call sqlite_step() again to retry the open. *pN is set to 0
81** and *pazColName and *pazValue are both set to NULL.
82**
83** SQLITE_DONE means that the virtual machine has finished
84** executing. sqlite_step() should not be called again on this
85** 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**
96** SQLITE_ERROR means that a run-time error (such as a constraint
97** violation) has occurred. The details of the error will be returned
98** by the next call to sqlite_finalize(). sqlite_step() should not
99** 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.
106*/
107int sqlite_step(
108 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;
114 sqlite *db;
115 int rc;
116
117 if( p->magic!=VDBE_MAGIC_RUN ){
118 return SQLITE_MISUSE;
119 }
120 db = p->db;
121 if( sqliteSafetyOn(db) ){
122 p->rc = SQLITE_MISUSE;
123 return SQLITE_MISUSE;
124 }
125 if( p->explain ){
126 rc = sqliteVdbeList(p);
127 }else{
128 rc = sqliteVdbeExec(p);
129 }
130 if( rc==SQLITE_DONE || rc==SQLITE_ROW ){
131 if( pazColName ) *pazColName = (const char**)p->azColName;
132 if( pN ) *pN = p->nResColumn;
133 }else{
134 if( pazColName) *pazColName = 0;
135 if( pN ) *pN = 0;
136 }
137 if( pazValue ){
138 if( rc==SQLITE_ROW ){
139 *pazValue = (const char**)p->azResColumn;
140 }else{
141 *pazValue = 0;
142 }
143 }
144 if( sqliteSafetyOff(db) ){
145 return SQLITE_MISUSE;
146 }
147 return rc;
148}
149
150/*
151** Insert a new aggregate element and make it the element that
152** has focus.
153**
154** Return 0 on success and 1 if memory is exhausted.
155*/
156static int AggInsert(Agg *p, char *zKey, int nKey){
157 AggElem *pElem, *pOld;
158 int i;
159 Mem *pMem;
160 pElem = sqliteMalloc( sizeof(AggElem) + nKey +
161 (p->nMem-1)*sizeof(pElem->aMem[0]) );
162 if( pElem==0 ) return 1;
163 pElem->zKey = (char*)&pElem->aMem[p->nMem];
164 memcpy(pElem->zKey, zKey, nKey);
165 pElem->nKey = nKey;
166 pOld = sqliteHashInsert(&p->hash, pElem->zKey, pElem->nKey, pElem);
167 if( pOld!=0 ){
168 assert( pOld==pElem ); /* Malloc failed on insert */
169 sqliteFree(pOld);
170 return 0;
171 }
172 for(i=0, pMem=pElem->aMem; i<p->nMem; i++, pMem++){
173 pMem->flags = MEM_Null;
174 }
175 p->pCurrent = pElem;
176 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){
184 HashElem *pElem = sqliteHashFirst(&p->hash);
185 if( pElem==0 ){
186 AggInsert(p,"",1);
187 pElem = sqliteHashFirst(&p->hash);
188 }
189 return pElem ? sqliteHashData(pElem) : 0;
190}
191
192/*
193** Convert the given stack entity into a string if it isn't one
194** already.
195*/
196#define Stringify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);}
197static int hardStringify(Mem *pStack){
198 int fg = pStack->flags;
199 if( fg & MEM_Real ){
200 sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%.15g",pStack->r);
201 }else if( fg & MEM_Int ){
202 sqlite_snprintf(sizeof(pStack->zShort),pStack->zShort,"%d",pStack->i);
203 }else{
204 pStack->zShort[0] = 0;
205 }
206 pStack->z = pStack->zShort;
207 pStack->n = strlen(pStack->zShort)+1;
208 pStack->flags = MEM_Str | MEM_Short;
209 return 0;
210}
211
212/*
213** 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*/
219#define Dynamicify(P) (((P)->flags & MEM_Dyn)==0 ? hardDynamicify(P):0)
220static int hardDynamicify(Mem *pStack){
221 int fg = pStack->flags;
222 char *z;
223 if( (fg & MEM_Str)==0 ){
224 hardStringify(pStack);
225 }
226 assert( (fg & MEM_Dyn)==0 );
227 z = sqliteMallocRaw( pStack->n );
228 if( z==0 ) return 1;
229 memcpy(z, pStack->z, pStack->n);
230 pStack->z = z;
231 pStack->flags |= MEM_Dyn;
232 return 0;
233}
234
235/*
236** An ephemeral string value (signified by the MEM_Ephem flag) contains
237** 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
244** converts an MEM_Ephem string into an MEM_Dyn string.
245*/
246#define Deephemeralize(P) \
247 if( ((P)->flags&MEM_Ephem)!=0 && hardDeephem(P) ){ goto no_mem;}
248static int hardDeephem(Mem *pStack){
249 char *z;
250 assert( (pStack->flags & MEM_Ephem)!=0 );
251 z = sqliteMallocRaw( pStack->n );
252 if( z==0 ) return 1;
253 memcpy(z, pStack->z, pStack->n);
254 pStack->z = z;
255 pStack->flags &= ~MEM_Ephem;
256 pStack->flags |= MEM_Dyn;
257 return 0;
258}
259
260/*
261** Release the memory associated with the given stack level. This
262** leaves the Mem.flags field in an inconsistent state.
263*/
264#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;
277}
278
279/*
280** 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.
284**
285** Under Linux (RedHat 7.2) this routine is much faster than atoi()
286** for converting strings into integers.
287*/
288static int toInt(const char *zNum, int *pNum){
289 int v = 0;
290 int neg;
291 int i, c;
292 if( *zNum=='-' ){
293 neg = 1;
294 zNum++;
295 }else if( *zNum=='+' ){
296 neg = 0;
297 zNum++;
298 }else{
299 neg = 0;
300 }
301 for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
302 v = v*10 + c - '0';
303 }
304 *pNum = neg ? -v : v;
305 return c==0 && i>0 && (i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0));
306}
307
308/*
309** Convert the given stack entity into a integer if it isn't one
310** already.
311**
312** Any prior string or real representation is invalidated.
313** NULLs are converted into 0.
314*/
315#define Integerify(P) if(((P)->flags&MEM_Int)==0){ hardIntegerify(P); }
316static void hardIntegerify(Mem *pStack){
317 if( pStack->flags & MEM_Real ){
318 pStack->i = (int)pStack->r;
319 Release(pStack);
320 }else if( pStack->flags & MEM_Str ){
321 toInt(pStack->z, &pStack->i);
322 Release(pStack);
323 }else{
324 pStack->i = 0;
325 }
326 pStack->flags = MEM_Int;
327}
328
329/*
330** Get a valid Real representation for the given stack element.
331**
332** Any prior string or integer representation is retained.
333** NULLs are converted into 0.0.
334*/
335#define Realify(P) if(((P)->flags&MEM_Real)==0){ hardRealify(P); }
336static void hardRealify(Mem *pStack){
337 if( pStack->flags & MEM_Str ){
338 pStack->r = sqliteAtoF(pStack->z, 0);
339 }else if( pStack->flags & MEM_Int ){
340 pStack->r = pStack->i;
341 }else{
342 pStack->r = 0.0;
343 }
344 pStack->flags |= MEM_Real;
345}
346
347/*
348** The parameters are pointers to the head of two sorted lists
349** of Sorter structures. Merge these two lists together and return
350** a single sorted list. This routine forms the core of the merge-sort
351** algorithm.
352**
353** In the case of a tie, left sorts in front of right.
354*/
355static Sorter *Merge(Sorter *pLeft, Sorter *pRight){
356 Sorter sHead;
357 Sorter *pTail;
358 pTail = &sHead;
359 pTail->pNext = 0;
360 while( pLeft && pRight ){
361 int c = sqliteSortCompare(pLeft->zKey, pRight->zKey);
362 if( c<=0 ){
363 pTail->pNext = pLeft;
364 pLeft = pLeft->pNext;
365 }else{
366 pTail->pNext = pRight;
367 pRight = pRight->pNext;
368 }
369 pTail = pTail->pNext;
370 }
371 if( pLeft ){
372 pTail->pNext = pLeft;
373 }else if( pRight ){
374 pTail->pNext = pRight;
375 }
376 return sHead.pNext;
377}
378
379/*
380** The following routine works like a replacement for the standard
381** library routine fgets(). The difference is in how end-of-line (EOL)
382** is handled. Standard fgets() uses LF for EOL under unix, CRLF
383** under windows, and CR under mac. This routine accepts any of these
384** character sequences as an EOL mark. The EOL mark is replaced by
385** a single LF character in zBuf.
386*/
387static char *vdbe_fgets(char *zBuf, int nBuf, FILE *in){
388 int i, c;
389 for(i=0; i<nBuf-1 && (c=getc(in))!=EOF; i++){
390 zBuf[i] = c;
391 if( c=='\r' || c=='\n' ){
392 if( c=='\r' ){
393 zBuf[i] = '\n';
394 c = getc(in);
395 if( c!=EOF && c!='\n' ) ungetc(c, in);
396 }
397 i++;
398 break;
399 }
400 }
401 zBuf[i] = 0;
402 return i>0 ? zBuf : 0;
403}
404
405/*
406** Make sure there is space in the Vdbe structure to hold at least
407** mxCursor cursors. If there is not currently enough space, then
408** allocate more.
409**
410** If a memory allocation error occurs, return 1. Return 0 if
411** everything works.
412*/
413static int expandCursorArraySize(Vdbe *p, int mxCursor){
414 if( mxCursor>=p->nCursor ){
415 Cursor *aCsr = sqliteRealloc( p->aCsr, (mxCursor+1)*sizeof(Cursor) );
416 if( aCsr==0 ) return 1;
417 p->aCsr = aCsr;
418 memset(&p->aCsr[p->nCursor], 0, sizeof(Cursor)*(mxCursor+1-p->nCursor));
419 p->nCursor = mxCursor+1;
420 }
421 return 0;
422}
423
424#ifdef VDBE_PROFILE
425/*
426** The following routine only works on pentium-class processors.
427** It uses the RDTSC opcode to read cycle count value out of the
428** processor and returns that value. This can be used for high-res
429** profiling.
430*/
431__inline__ unsigned long long int hwtime(void){
432 unsigned long long int x;
433 __asm__("rdtsc\n\t"
434 "mov %%edx, %%ecx\n\t"
435 :"=A" (x));
436 return x;
437}
438#endif
439
440/*
441** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
442** sqlite_interrupt() routine has been called. If it has been, then
443** processing of the VDBE program is interrupted.
444**
445** This macro added to every instruction that does a jump in order to
446** implement a loop. This test used to be on every single instruction,
447** but that meant we more testing that we needed. By only testing the
448** flag on jump instructions, we get a (small) speed improvement.
449*/
450#define CHECK_FOR_INTERRUPT \
451 if( db->flags & SQLITE_Interrupt ) goto abort_due_to_interrupt;
452
453
454/*
455** Execute as much of a VDBE program as we can then return.
456**
457** sqliteVdbeMakeReady() must be called before this routine in order to
458** close the program with a final OP_Halt and to set up the callbacks
459** and the error message pointer.
460**
461** Whenever a row or result data is available, this routine will either
462** invoke the result callback (if there is one) or return with
463** SQLITE_ROW.
464**
465** If an attempt is made to open a locked database, then this routine
466** will either invoke the busy callback (if there is one) or it will
467** return SQLITE_BUSY.
468**
469** If an error occurs, an error message is written to memory obtained
470** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
471** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
472**
473** If the callback ever returns non-zero, then the program exits
474** immediately. There will be no error message but the p->rc field is
475** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
476**
477** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
478** routine to return SQLITE_ERROR.
479**
480** Other fatal errors return SQLITE_ERROR.
481**
482** After this routine has finished, sqliteVdbeFinalize() should be
483** used to clean up the mess that was left behind.
484*/
485int sqliteVdbeExec(
486 Vdbe *p /* The VDBE */
487){
488 int pc; /* The program counter */
489 Op *pOp; /* Current operation */
490 int rc = SQLITE_OK; /* Value to return */
491 sqlite *db = p->db; /* The database */
492 Mem *pTos; /* Top entry in the operand stack */
493 char zBuf[100]; /* Space to sprintf() an integer */
494#ifdef VDBE_PROFILE
495 unsigned long long start; /* CPU clock count at start of opcode */
496 int origPc; /* Program counter at start of opcode */
497#endif
498#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
499 int nProgressOps = 0; /* Opcodes executed since progress callback. */
500#endif
501
502 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
503 assert( db->magic==SQLITE_MAGIC_BUSY );
504 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
505 p->rc = SQLITE_OK;
506 assert( p->explain==0 );
507 if( sqlite_malloc_failed ) goto no_mem;
508 pTos = p->pTos;
509 if( p->popStack ){
510 popStack(&pTos, p->popStack);
511 p->popStack = 0;
512 }
513 CHECK_FOR_INTERRUPT;
514 for(pc=p->pc; rc==SQLITE_OK; pc++){
515 assert( pc>=0 && pc<p->nOp );
516 assert( pTos<=&p->aStack[pc] );
517#ifdef VDBE_PROFILE
518 origPc = pc;
519 start = hwtime();
520#endif
521 pOp = &p->aOp[pc];
522
523 /* Only allow tracing if NDEBUG is not defined.
524 */
525#ifndef NDEBUG
526 if( p->trace ){
527 sqliteVdbePrintOp(p->trace, pc, pOp);
528 }
529#endif
530
531 /* Check to see if we need to simulate an interrupt. This only happens
532 ** if we have a special test build.
533 */
534#ifdef SQLITE_TEST
535 if( sqlite_interrupt_count>0 ){
536 sqlite_interrupt_count--;
537 if( sqlite_interrupt_count==0 ){
538 sqlite_interrupt(db);
539 }
540 }
541#endif
542
543#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
544 /* Call the progress callback if it is configured and the required number
545 ** of VDBE ops have been executed (either since this invocation of
546 ** sqliteVdbeExec() or since last time the progress callback was called).
547 ** If the progress callback returns non-zero, exit the virtual machine with
548 ** a return code SQLITE_ABORT.
549 */
550 if( db->xProgress ){
551 if( db->nProgressOps==nProgressOps ){
552 if( db->xProgress(db->pProgressArg)!=0 ){
553 rc = SQLITE_ABORT;
554 continue; /* skip to the next iteration of the for loop */
555 }
556 nProgressOps = 0;
557 }
558 nProgressOps++;
559 }
560#endif
561
562 switch( pOp->opcode ){
563
564/*****************************************************************************
565** What follows is a massive switch statement where each case implements a
566** separate instruction in the virtual machine. If we follow the usual
567** indentation conventions, each case should be indented by 6 spaces. But
568** that is a lot of wasted space on the left margin. So the code within
569** the switch statement will break with convention and be flush-left. Another
570** big comment (similar to this one) will mark the point in the code where
571** we transition back to normal indentation.
572**
573** The formatting of each case is important. The makefile for SQLite
574** generates two C files "opcodes.h" and "opcodes.c" by scanning this
575** file looking for lines that begin with "case OP_". The opcodes.h files
576** will be filled with #defines that give unique integer values to each
577** opcode and the opcodes.c file is filled with an array of strings where
578** each string is the symbolic name for the corresponding opcode.
579**
580** Documentation about VDBE opcodes is generated by scanning this file
581** for lines of that contain "Opcode:". That line and all subsequent
582** comment lines are used in the generation of the opcode.html documentation
583** file.
584**
585** SUMMARY:
586**
587** Formatting is important to scripts that scan this file.
588** Do not deviate from the formatting style currently in use.
589**
590*****************************************************************************/
591
592/* Opcode: Goto * P2 *
593**
594** An unconditional jump to address P2.
595** The next instruction executed will be
596** the one at index P2 from the beginning of
597** the program.
598*/
599case OP_Goto: {
600 CHECK_FOR_INTERRUPT;
601 pc = pOp->p2 - 1;
602 break;
603}
604
605/* Opcode: Gosub * P2 *
606**
607** Push the current address plus 1 onto the return address stack
608** and then jump to address P2.
609**
610** The return address stack is of limited depth. If too many
611** OP_Gosub operations occur without intervening OP_Returns, then
612** the return address stack will fill up and processing will abort
613** with a fatal error.
614*/
615case OP_Gosub: {
616 if( p->returnDepth>=sizeof(p->returnStack)/sizeof(p->returnStack[0]) ){
617 sqliteSetString(&p->zErrMsg, "return address stack overflow", (char*)0);
618 p->rc = SQLITE_INTERNAL;
619 return SQLITE_ERROR;
620 }
621 p->returnStack[p->returnDepth++] = pc+1;
622 pc = pOp->p2 - 1;
623 break;
624}
625
626/* Opcode: Return * * *
627**
628** Jump immediately to the next instruction after the last unreturned
629** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
630** processing aborts with a fatal error.
631*/
632case OP_Return: {
633 if( p->returnDepth<=0 ){
634 sqliteSetString(&p->zErrMsg, "return address stack underflow", (char*)0);
635 p->rc = SQLITE_INTERNAL;
636 return SQLITE_ERROR;
637 }
638 p->returnDepth--;
639 pc = p->returnStack[p->returnDepth] - 1;
640 break;
641}
642
643/* Opcode: Halt P1 P2 *
644**
645** Exit immediately. All open cursors, Lists, Sorts, etc are closed
646** automatically.
647**
648** P1 is the result code returned by sqlite_exec(). For a normal
649** halt, this should be SQLITE_OK (0). For errors, it can be some
650** other value. If P1!=0 then P2 will determine whether or not to
651** rollback the current transaction. Do not rollback if P2==OE_Fail.
652** Do the rollback if P2==OE_Rollback. If P2==OE_Abort, then back
653** out all changes that have occurred during this execution of the
654** VDBE, but do not rollback the transaction.
655**
656** There is an implied "Halt 0 0 0" instruction inserted at the very end of
657** every program. So a jump past the last instruction of the program
658** is the same as executing Halt.
659*/
660case OP_Halt: {
661 p->magic = VDBE_MAGIC_HALT;
662 p->pTos = pTos;
663 if( pOp->p1!=SQLITE_OK ){
664 p->rc = pOp->p1;
665 p->errorAction = pOp->p2;
666 if( pOp->p3 ){
667 sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
668 }
669 return SQLITE_ERROR;
670 }else{
671 p->rc = SQLITE_OK;
672 return SQLITE_DONE;
673 }
674}
675
676/* Opcode: Integer P1 * P3
677**
678** The integer value P1 is pushed onto the stack. If P3 is not zero
679** then it is assumed to be a string representation of the same integer.
680*/
681case OP_Integer: {
682 pTos++;
683 pTos->i = pOp->p1;
684 pTos->flags = MEM_Int;
685 if( pOp->p3 ){
686 pTos->z = pOp->p3;
687 pTos->flags |= MEM_Str | MEM_Static;
688 pTos->n = strlen(pOp->p3)+1;
689 }
690 break;
691}
692
693/* Opcode: String * * P3
694**
695** The string value P3 is pushed onto the stack. If P3==0 then a
696** NULL is pushed onto the stack.
697*/
698case OP_String: {
699 char *z = pOp->p3;
700 pTos++;
701 if( z==0 ){
702 pTos->flags = MEM_Null;
703 }else{
704 pTos->z = z;
705 pTos->n = strlen(z) + 1;
706 pTos->flags = MEM_Str | MEM_Static;
707 }
708 break;
709}
710
711/* Opcode: Variable P1 * *
712**
713** Push the value of variable P1 onto the stack. A variable is
714** an unknown in the original SQL string as handed to sqlite_compile().
715** Any occurance of the '?' character in the original SQL is considered
716** a variable. Variables in the SQL string are number from left to
717** right beginning with 1. The values of variables are set using the
718** sqlite_bind() API.
719*/
720case OP_Variable: {
721 int j = pOp->p1 - 1;
722 pTos++;
723 if( j>=0 && j<p->nVar && p->azVar[j]!=0 ){
724 pTos->z = p->azVar[j];
725 pTos->n = p->anVar[j];
726 pTos->flags = MEM_Str | MEM_Static;
727 }else{
728 pTos->flags = MEM_Null;
729 }
730 break;
731}
732
733/* Opcode: Pop P1 * *
734**
735** P1 elements are popped off of the top of stack and discarded.
736*/
737case OP_Pop: {
738 assert( pOp->p1>=0 );
739 popStack(&pTos, pOp->p1);
740 assert( pTos>=&p->aStack[-1] );
741 break;
742}
743
744/* Opcode: Dup P1 P2 *
745**
746** A copy of the P1-th element of the stack
747** is made and pushed onto the top of the stack.
748** The top of the stack is element 0. So the
749** instruction "Dup 0 0 0" will make a copy of the
750** top of the stack.
751**
752** If the content of the P1-th element is a dynamically
753** allocated string, then a new copy of that string
754** is made if P2==0. If P2!=0, then just a pointer
755** to the string is copied.
756**
757** Also see the Pull instruction.
758*/
759case OP_Dup: {
760 Mem *pFrom = &pTos[-pOp->p1];
761 assert( pFrom<=pTos && pFrom>=p->aStack );
762 pTos++;
763 memcpy(pTos, pFrom, sizeof(*pFrom)-NBFS);
764 if( pTos->flags & MEM_Str ){
765 if( pOp->p2 && (pTos->flags & (MEM_Dyn|MEM_Ephem)) ){
766 pTos->flags &= ~MEM_Dyn;
767 pTos->flags |= MEM_Ephem;
768 }else if( pTos->flags & MEM_Short ){
769 memcpy(pTos->zShort, pFrom->zShort, pTos->n);
770 pTos->z = pTos->zShort;
771 }else if( (pTos->flags & MEM_Static)==0 ){
772 pTos->z = sqliteMallocRaw(pFrom->n);
773 if( sqlite_malloc_failed ) goto no_mem;
774 memcpy(pTos->z, pFrom->z, pFrom->n);
775 pTos->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
776 pTos->flags |= MEM_Dyn;
777 }
778 }
779 break;
780}
781
782/* Opcode: Pull P1 * *
783**
784** The P1-th element is removed from its current location on
785** the stack and pushed back on top of the stack. The
786** top of the stack is element 0, so "Pull 0 0 0" is
787** a no-op. "Pull 1 0 0" swaps the top two elements of
788** the stack.
789**
790** See also the Dup instruction.
791*/
792case OP_Pull: {
793 Mem *pFrom = &pTos[-pOp->p1];
794 int i;
795 Mem ts;
796
797 ts = *pFrom;
798 Deephemeralize(pTos);
799 for(i=0; i<pOp->p1; i++, pFrom++){
800 Deephemeralize(&pFrom[1]);
801 *pFrom = pFrom[1];
802 assert( (pFrom->flags & MEM_Ephem)==0 );
803 if( pFrom->flags & MEM_Short ){
804 assert( pFrom->flags & MEM_Str );
805 assert( pFrom->z==pFrom[1].zShort );
806 pFrom->z = pFrom->zShort;
807 }
808 }
809 *pTos = ts;
810 if( pTos->flags & MEM_Short ){
811 assert( pTos->flags & MEM_Str );
812 assert( pTos->z==pTos[-pOp->p1].zShort );
813 pTos->z = pTos->zShort;
814 }
815 break;
816}
817
818/* Opcode: Push P1 * *
819**
820** Overwrite the value of the P1-th element down on the
821** stack (P1==0 is the top of the stack) with the value
822** of the top of the stack. Then pop the top of the stack.
823*/
824case OP_Push: {
825 Mem *pTo = &pTos[-pOp->p1];
826
827 assert( pTo>=p->aStack );
828 Deephemeralize(pTos);
829 Release(pTo);
830 *pTo = *pTos;
831 if( pTo->flags & MEM_Short ){
832 assert( pTo->z==pTos->zShort );
833 pTo->z = pTo->zShort;
834 }
835 pTos--;
836 break;
837}
838
839
840/* Opcode: ColumnName P1 P2 P3
841**
842** P3 becomes the P1-th column name (first is 0). An array of pointers
843** to all column names is passed as the 4th parameter to the callback.
844** If P2==1 then this is the last column in the result set and thus the
845** number of columns in the result set will be P1. There must be at least
846** one OP_ColumnName with a P2==1 before invoking OP_Callback and the
847** number of columns specified in OP_Callback must one more than the P1
848** value of the OP_ColumnName that has P2==1.
849*/
850case OP_ColumnName: {
851 assert( pOp->p1>=0 && pOp->p1<p->nOp );
852 p->azColName[pOp->p1] = pOp->p3;
853 p->nCallback = 0;
854 if( pOp->p2 ) p->nResColumn = pOp->p1+1;
855 break;
856}
857
858/* Opcode: Callback P1 * *
859**
860** Pop P1 values off the stack and form them into an array. Then
861** invoke the callback function using the newly formed array as the
862** 3rd parameter.
863*/
864case OP_Callback: {
865 int i;
866 char **azArgv = p->zArgv;
867 Mem *pCol;
868
869 pCol = &pTos[1-pOp->p1];
870 assert( pCol>=p->aStack );
871 for(i=0; i<pOp->p1; i++, pCol++){
872 if( pCol->flags & MEM_Null ){
873 azArgv[i] = 0;
874 }else{
875 Stringify(pCol);
876 azArgv[i] = pCol->z;
877 }
878 }
879 azArgv[i] = 0;
880 p->nCallback++;
881 p->azResColumn = azArgv;
882 assert( p->nResColumn==pOp->p1 );
883 p->popStack = pOp->p1;
884 p->pc = pc + 1;
885 p->pTos = pTos;
886 return SQLITE_ROW;
887}
888
889/* Opcode: Concat P1 P2 P3
890**
891** Look at the first P1 elements of the stack. Append them all
892** together with the lowest element first. Use P3 as a separator.
893** Put the result on the top of the stack. The original P1 elements
894** are popped from the stack if P2==0 and retained if P2==1. If
895** any element of the stack is NULL, then the result is NULL.
896**
897** If P3 is NULL, then use no separator. When P1==1, this routine
898** makes a copy of the top stack element into memory obtained
899** from sqliteMalloc().
900*/
901case OP_Concat: {
902 char *zNew;
903 int nByte;
904 int nField;
905 int i, j;
906 char *zSep;
907 int nSep;
908 Mem *pTerm;
909
910 nField = pOp->p1;
911 zSep = pOp->p3;
912 if( zSep==0 ) zSep = "";
913 nSep = strlen(zSep);
914 assert( &pTos[1-nField] >= p->aStack );
915 nByte = 1 - nSep;
916 pTerm = &pTos[1-nField];
917 for(i=0; i<nField; i++, pTerm++){
918 if( pTerm->flags & MEM_Null ){
919 nByte = -1;
920 break;
921 }else{
922 Stringify(pTerm);
923 nByte += pTerm->n - 1 + nSep;
924 }
925 }
926 if( nByte<0 ){
927 if( pOp->p2==0 ){
928 popStack(&pTos, nField);
929 }
930 pTos++;
931 pTos->flags = MEM_Null;
932 break;
933 }
934 zNew = sqliteMallocRaw( nByte );
935 if( zNew==0 ) goto no_mem;
936 j = 0;
937 pTerm = &pTos[1-nField];
938 for(i=j=0; i<nField; i++, pTerm++){
939 assert( pTerm->flags & MEM_Str );
940 memcpy(&zNew[j], pTerm->z, pTerm->n-1);
941 j += pTerm->n-1;
942 if( nSep>0 && i<nField-1 ){
943 memcpy(&zNew[j], zSep, nSep);
944 j += nSep;
945 }
946 }
947 zNew[j] = 0;
948 if( pOp->p2==0 ){
949 popStack(&pTos, nField);
950 }
951 pTos++;
952 pTos->n = nByte;
953 pTos->flags = MEM_Str|MEM_Dyn;
954 pTos->z = zNew;
955 break;
956}
957
958/* Opcode: Add * * *
959**
960** Pop the top two elements from the stack, add them together,
961** and push the result back onto the stack. If either element
962** is a string then it is converted to a double using the atof()
963** function before the addition.
964** If either operand is NULL, the result is NULL.
965*/
966/* Opcode: Multiply * * *
967**
968** Pop the top two elements from the stack, multiply them together,
969** and push the result back onto the stack. If either element
970** is a string then it is converted to a double using the atof()
971** function before the multiplication.
972** If either operand is NULL, the result is NULL.
973*/
974/* Opcode: Subtract * * *
975**
976** Pop the top two elements from the stack, subtract the
977** first (what was on top of the stack) from the second (the
978** next on stack)
979** and push the result back onto the stack. If either element
980** is a string then it is converted to a double using the atof()
981** function before the subtraction.
982** If either operand is NULL, the result is NULL.
983*/
984/* Opcode: Divide * * *
985**
986** Pop the top two elements from the stack, divide the
987** first (what was on top of the stack) from the second (the
988** next on stack)
989** and push the result back onto the stack. If either element
990** is a string then it is converted to a double using the atof()
991** function before the division. Division by zero returns NULL.
992** If either operand is NULL, the result is NULL.
993*/
994/* Opcode: Remainder * * *
995**
996** Pop the top two elements from the stack, divide the
997** first (what was on top of the stack) from the second (the
998** next on stack)
999** and push the remainder after division onto the stack. If either element
1000** is a string then it is converted to a double using the atof()
1001** function before the division. Division by zero returns NULL.
1002** If either operand is NULL, the result is NULL.
1003*/
1004case OP_Add:
1005case OP_Subtract:
1006case OP_Multiply:
1007case OP_Divide:
1008case OP_Remainder: {
1009 Mem *pNos = &pTos[-1];
1010 assert( pNos>=p->aStack );
1011 if( ((pTos->flags | pNos->flags) & MEM_Null)!=0 ){
1012 Release(pTos);
1013 pTos--;
1014 Release(pTos);
1015 pTos->flags = MEM_Null;
1016 }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
1017 int a, b;
1018 a = pTos->i;
1019 b = pNos->i;
1020 switch( pOp->opcode ){
1021 case OP_Add: b += a; break;
1022 case OP_Subtract: b -= a; break;
1023 case OP_Multiply: b *= a; break;
1024 case OP_Divide: {
1025 if( a==0 ) goto divide_by_zero;
1026 b /= a;
1027 break;
1028 }
1029 default: {
1030 if( a==0 ) goto divide_by_zero;
1031 b %= a;
1032 break;
1033 }
1034 }
1035 Release(pTos);
1036 pTos--;
1037 Release(pTos);
1038 pTos->i = b;
1039 pTos->flags = MEM_Int;
1040 }else{
1041 double a, b;
1042 Realify(pTos);
1043 Realify(pNos);
1044 a = pTos->r;
1045 b = pNos->r;
1046 switch( pOp->opcode ){
1047 case OP_Add: b += a; break;
1048 case OP_Subtract: b -= a; break;
1049 case OP_Multiply: b *= a; break;
1050 case OP_Divide: {
1051 if( a==0.0 ) goto divide_by_zero;
1052 b /= a;
1053 break;
1054 }
1055 default: {
1056 int ia = (int)a;
1057 int ib = (int)b;
1058 if( ia==0.0 ) goto divide_by_zero;
1059 b = ib % ia;
1060 break;
1061 }
1062 }
1063 Release(pTos);
1064 pTos--;
1065 Release(pTos);
1066 pTos->r = b;
1067 pTos->flags = MEM_Real;
1068 }
1069 break;
1070
1071divide_by_zero:
1072 Release(pTos);
1073 pTos--;
1074 Release(pTos);
1075 pTos->flags = MEM_Null;
1076 break;
1077}
1078
1079/* Opcode: Function P1 * P3
1080**
1081** Invoke a user function (P3 is a pointer to a Function structure that
1082** defines the function) with P1 string arguments taken from the stack.
1083** Pop all arguments from the stack and push back the result.
1084**
1085** See also: AggFunc
1086*/
1087case OP_Function: {
1088 int n, i;
1089 Mem *pArg;
1090 char **azArgv;
1091 sqlite_func ctx;
1092
1093 n = pOp->p1;
1094 pArg = &pTos[1-n];
1095 azArgv = p->zArgv;
1096 for(i=0; i<n; i++, pArg++){
1097 if( pArg->flags & MEM_Null ){
1098 azArgv[i] = 0;
1099 }else{
1100 Stringify(pArg);
1101 azArgv[i] = pArg->z;
1102 }
1103 }
1104 ctx.pFunc = (FuncDef*)pOp->p3;
1105 ctx.s.flags = MEM_Null;
1106 ctx.s.z = 0;
1107 ctx.isError = 0;
1108 ctx.isStep = 0;
1109 if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
1110 (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
1111 if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
1112 popStack(&pTos, n);
1113 pTos++;
1114 *pTos = ctx.s;
1115 if( pTos->flags & MEM_Short ){
1116 pTos->z = pTos->zShort;
1117 }
1118 if( ctx.isError ){
1119 sqliteSetString(&p->zErrMsg,
1120 (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
1121 rc = SQLITE_ERROR;
1122 }
1123 break;
1124}
1125
1126/* Opcode: BitAnd * * *
1127**
1128** Pop the top two elements from the stack. Convert both elements
1129** to integers. Push back onto the stack the bit-wise AND of the
1130** two elements.
1131** If either operand is NULL, the result is NULL.
1132*/
1133/* Opcode: BitOr * * *
1134**
1135** Pop the top two elements from the stack. Convert both elements
1136** to integers. Push back onto the stack the bit-wise OR of the
1137** two elements.
1138** If either operand is NULL, the result is NULL.
1139*/
1140/* Opcode: ShiftLeft * * *
1141**
1142** Pop the top two elements from the stack. Convert both elements
1143** to integers. Push back onto the stack the top element shifted
1144** left by N bits where N is the second element on the stack.
1145** If either operand is NULL, the result is NULL.
1146*/
1147/* Opcode: ShiftRight * * *
1148**
1149** Pop the top two elements from the stack. Convert both elements
1150** to integers. Push back onto the stack the top element shifted
1151** right by N bits where N is the second element on the stack.
1152** If either operand is NULL, the result is NULL.
1153*/
1154case OP_BitAnd:
1155case OP_BitOr:
1156case OP_ShiftLeft:
1157case OP_ShiftRight: {
1158 Mem *pNos = &pTos[-1];
1159 int a, b;
1160
1161 assert( pNos>=p->aStack );
1162 if( (pTos->flags | pNos->flags) & MEM_Null ){
1163 popStack(&pTos, 2);
1164 pTos++;
1165 pTos->flags = MEM_Null;
1166 break;
1167 }
1168 Integerify(pTos);
1169 Integerify(pNos);
1170 a = pTos->i;
1171 b = pNos->i;
1172 switch( pOp->opcode ){
1173 case OP_BitAnd: a &= b; break;
1174 case OP_BitOr: a |= b; break;
1175 case OP_ShiftLeft: a <<= b; break;
1176 case OP_ShiftRight: a >>= b; break;
1177 default: /* CANT HAPPEN */ break;
1178 }
1179 assert( (pTos->flags & MEM_Dyn)==0 );
1180 assert( (pNos->flags & MEM_Dyn)==0 );
1181 pTos--;
1182 Release(pTos);
1183 pTos->i = a;
1184 pTos->flags = MEM_Int;
1185 break;
1186}
1187
1188/* Opcode: AddImm P1 * *
1189**
1190** Add the value P1 to whatever is on top of the stack. The result
1191** is always an integer.
1192**
1193** To force the top of the stack to be an integer, just add 0.
1194*/
1195case OP_AddImm: {
1196 assert( pTos>=p->aStack );
1197 Integerify(pTos);
1198 pTos->i += pOp->p1;
1199 break;
1200}
1201
1202/* Opcode: ForceInt P1 P2 *
1203**
1204** Convert the top of the stack into an integer. If the current top of
1205** the stack is not numeric (meaning that is is a NULL or a string that
1206** does not look like an integer or floating point number) then pop the
1207** stack and jump to P2. If the top of the stack is numeric then
1208** convert it into the least integer that is greater than or equal to its
1209** current value if P1==0, or to the least integer that is strictly
1210** greater than its current value if P1==1.
1211*/
1212case OP_ForceInt: {
1213 int v;
1214 assert( pTos>=p->aStack );
1215 if( (pTos->flags & (MEM_Int|MEM_Real))==0
1216 && ((pTos->flags & MEM_Str)==0 || sqliteIsNumber(pTos->z)==0) ){
1217 Release(pTos);
1218 pTos--;
1219 pc = pOp->p2 - 1;
1220 break;
1221 }
1222 if( pTos->flags & MEM_Int ){
1223 v = pTos->i + (pOp->p1!=0);
1224 }else{
1225 Realify(pTos);
1226 v = (int)pTos->r;
1227 if( pTos->r>(double)v ) v++;
1228 if( pOp->p1 && pTos->r==(double)v ) v++;
1229 }
1230 Release(pTos);
1231 pTos->i = v;
1232 pTos->flags = MEM_Int;
1233 break;
1234}
1235
1236/* Opcode: MustBeInt P1 P2 *
1237**
1238** Force the top of the stack to be an integer. If the top of the
1239** stack is not an integer and cannot be converted into an integer
1240** with out data loss, then jump immediately to P2, or if P2==0
1241** raise an SQLITE_MISMATCH exception.
1242**
1243** If the top of the stack is not an integer and P2 is not zero and
1244** P1 is 1, then the stack is popped. In all other cases, the depth
1245** of the stack is unchanged.
1246*/
1247case OP_MustBeInt: {
1248 assert( pTos>=p->aStack );
1249 if( pTos->flags & MEM_Int ){
1250 /* Do nothing */
1251 }else if( pTos->flags & MEM_Real ){
1252 int i = (int)pTos->r;
1253 double r = (double)i;
1254 if( r!=pTos->r ){
1255 goto mismatch;
1256 }
1257 pTos->i = i;
1258 }else if( pTos->flags & MEM_Str ){
1259 int v;
1260 if( !toInt(pTos->z, &v) ){
1261 double r;
1262 if( !sqliteIsNumber(pTos->z) ){
1263 goto mismatch;
1264 }
1265 Realify(pTos);
1266 v = (int)pTos->r;
1267 r = (double)v;
1268 if( r!=pTos->r ){
1269 goto mismatch;
1270 }
1271 }
1272 pTos->i = v;
1273 }else{
1274 goto mismatch;
1275 }
1276 Release(pTos);
1277 pTos->flags = MEM_Int;
1278 break;
1279
1280mismatch:
1281 if( pOp->p2==0 ){
1282 rc = SQLITE_MISMATCH;
1283 goto abort_due_to_error;
1284 }else{
1285 if( pOp->p1 ) popStack(&pTos, 1);
1286 pc = pOp->p2 - 1;
1287 }
1288 break;
1289}
1290
1291/* Opcode: Eq P1 P2 *
1292**
1293** Pop the top two elements from the stack. If they are equal, then
1294** jump to instruction P2. Otherwise, continue to the next instruction.
1295**
1296** If either operand is NULL (and thus if the result is unknown) then
1297** take the jump if P1 is true.
1298**
1299** If both values are numeric, they are converted to doubles using atof()
1300** and compared for equality that way. Otherwise the strcmp() library
1301** routine is used for the comparison. For a pure text comparison
1302** use OP_StrEq.
1303**
1304** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1305** stack if the jump would have been taken, or a 0 if not. Push a
1306** NULL if either operand was NULL.
1307*/
1308/* Opcode: Ne P1 P2 *
1309**
1310** Pop the top two elements from the stack. If they are not equal, then
1311** jump to instruction P2. Otherwise, continue to the next instruction.
1312**
1313** If either operand is NULL (and thus if the result is unknown) then
1314** take the jump if P1 is true.
1315**
1316** If both values are numeric, they are converted to doubles using atof()
1317** and compared in that format. Otherwise the strcmp() library
1318** routine is used for the comparison. For a pure text comparison
1319** use OP_StrNe.
1320**
1321** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1322** stack if the jump would have been taken, or a 0 if not. Push a
1323** NULL if either operand was NULL.
1324*/
1325/* Opcode: Lt P1 P2 *
1326**
1327** Pop the top two elements from the stack. If second element (the
1328** next on stack) is less than the first (the top of stack), then
1329** jump to instruction P2. Otherwise, continue to the next instruction.
1330** In other words, jump if NOS<TOS.
1331**
1332** If either operand is NULL (and thus if the result is unknown) then
1333** take the jump if P1 is true.
1334**
1335** If both values are numeric, they are converted to doubles using atof()
1336** and compared in that format. Numeric values are always less than
1337** non-numeric values. If both operands are non-numeric, the strcmp() library
1338** routine is used for the comparison. For a pure text comparison
1339** use OP_StrLt.
1340**
1341** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1342** stack if the jump would have been taken, or a 0 if not. Push a
1343** NULL if either operand was NULL.
1344*/
1345/* Opcode: Le P1 P2 *
1346**
1347** Pop the top two elements from the stack. If second element (the
1348** next on stack) is less than or equal to the first (the top of stack),
1349** then jump to instruction P2. In other words, jump if NOS<=TOS.
1350**
1351** If either operand is NULL (and thus if the result is unknown) then
1352** take the jump if P1 is true.
1353**
1354** If both values are numeric, they are converted to doubles using atof()
1355** and compared in that format. Numeric values are always less than
1356** non-numeric values. If both operands are non-numeric, the strcmp() library
1357** routine is used for the comparison. For a pure text comparison
1358** use OP_StrLe.
1359**
1360** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1361** stack if the jump would have been taken, or a 0 if not. Push a
1362** NULL if either operand was NULL.
1363*/
1364/* Opcode: Gt P1 P2 *
1365**
1366** Pop the top two elements from the stack. If second element (the
1367** next on stack) is greater than the first (the top of stack),
1368** then jump to instruction P2. In other words, jump if NOS>TOS.
1369**
1370** If either operand is NULL (and thus if the result is unknown) then
1371** take the jump if P1 is true.
1372**
1373** If both values are numeric, they are converted to doubles using atof()
1374** and compared in that format. Numeric values are always less than
1375** non-numeric values. If both operands are non-numeric, the strcmp() library
1376** routine is used for the comparison. For a pure text comparison
1377** use OP_StrGt.
1378**
1379** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1380** stack if the jump would have been taken, or a 0 if not. Push a
1381** NULL if either operand was NULL.
1382*/
1383/* Opcode: Ge P1 P2 *
1384**
1385** Pop the top two elements from the stack. If second element (the next
1386** on stack) is greater than or equal to the first (the top of stack),
1387** then jump to instruction P2. In other words, jump if NOS>=TOS.
1388**
1389** If either operand is NULL (and thus if the result is unknown) then
1390** take the jump if P1 is true.
1391**
1392** If both values are numeric, they are converted to doubles using atof()
1393** and compared in that format. Numeric values are always less than
1394** non-numeric values. If both operands are non-numeric, the strcmp() library
1395** routine is used for the comparison. For a pure text comparison
1396** use OP_StrGe.
1397**
1398** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1399** stack if the jump would have been taken, or a 0 if not. Push a
1400** NULL if either operand was NULL.
1401*/
1402case OP_Eq:
1403case OP_Ne:
1404case OP_Lt:
1405case OP_Le:
1406case OP_Gt:
1407case OP_Ge: {
1408 Mem *pNos = &pTos[-1];
1409 int c, v;
1410 int ft, fn;
1411 assert( pNos>=p->aStack );
1412 ft = pTos->flags;
1413 fn = pNos->flags;
1414 if( (ft | fn) & MEM_Null ){
1415 popStack(&pTos, 2);
1416 if( pOp->p2 ){
1417 if( pOp->p1 ) pc = pOp->p2-1;
1418 }else{
1419 pTos++;
1420 pTos->flags = MEM_Null;
1421 }
1422 break;
1423 }else if( (ft & fn & MEM_Int)==MEM_Int ){
1424 c = pNos->i - pTos->i;
1425 }else if( (ft & MEM_Int)!=0 && (fn & MEM_Str)!=0 && toInt(pNos->z,&v) ){
1426 c = v - pTos->i;
1427 }else if( (fn & MEM_Int)!=0 && (ft & MEM_Str)!=0 && toInt(pTos->z,&v) ){
1428 c = pNos->i - v;
1429 }else{
1430 Stringify(pTos);
1431 Stringify(pNos);
1432 c = sqliteCompare(pNos->z, pTos->z);
1433 }
1434 switch( pOp->opcode ){
1435 case OP_Eq: c = c==0; break;
1436 case OP_Ne: c = c!=0; break;
1437 case OP_Lt: c = c<0; break;
1438 case OP_Le: c = c<=0; break;
1439 case OP_Gt: c = c>0; break;
1440 default: c = c>=0; break;
1441 }
1442 popStack(&pTos, 2);
1443 if( pOp->p2 ){
1444 if( c ) pc = pOp->p2-1;
1445 }else{
1446 pTos++;
1447 pTos->i = c;
1448 pTos->flags = MEM_Int;
1449 }
1450 break;
1451}
1452/* INSERT NO CODE HERE!
1453**
1454** The opcode numbers are extracted from this source file by doing
1455**
1456** grep '^case OP_' vdbe.c | ... >opcodes.h
1457**
1458** The opcodes are numbered in the order that they appear in this file.
1459** But in order for the expression generating code to work right, the
1460** string comparison operators that follow must be numbered exactly 6
1461** greater than the numeric comparison opcodes above. So no other
1462** cases can appear between the two.
1463*/
1464/* Opcode: StrEq P1 P2 *
1465**
1466** Pop the top two elements from the stack. If they are equal, then
1467** jump to instruction P2. Otherwise, continue to the next instruction.
1468**
1469** If either operand is NULL (and thus if the result is unknown) then
1470** take the jump if P1 is true.
1471**
1472** The strcmp() library routine is used for the comparison. For a
1473** numeric comparison, use OP_Eq.
1474**
1475** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1476** stack if the jump would have been taken, or a 0 if not. Push a
1477** NULL if either operand was NULL.
1478*/
1479/* Opcode: StrNe P1 P2 *
1480**
1481** Pop the top two elements from the stack. If they are not equal, then
1482** jump to instruction P2. Otherwise, continue to the next instruction.
1483**
1484** If either operand is NULL (and thus if the result is unknown) then
1485** take the jump if P1 is true.
1486**
1487** The strcmp() library routine is used for the comparison. For a
1488** numeric comparison, use OP_Ne.
1489**
1490** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1491** stack if the jump would have been taken, or a 0 if not. Push a
1492** NULL if either operand was NULL.
1493*/
1494/* Opcode: StrLt P1 P2 *
1495**
1496** Pop the top two elements from the stack. If second element (the
1497** next on stack) is less than the first (the top of stack), then
1498** jump to instruction P2. Otherwise, continue to the next instruction.
1499** In other words, jump if NOS<TOS.
1500**
1501** If either operand is NULL (and thus if the result is unknown) then
1502** take the jump if P1 is true.
1503**
1504** The strcmp() library routine is used for the comparison. For a
1505** numeric comparison, use OP_Lt.
1506**
1507** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1508** stack if the jump would have been taken, or a 0 if not. Push a
1509** NULL if either operand was NULL.
1510*/
1511/* Opcode: StrLe P1 P2 *
1512**
1513** Pop the top two elements from the stack. If second element (the
1514** next on stack) is less than or equal to the first (the top of stack),
1515** then jump to instruction P2. In other words, jump if NOS<=TOS.
1516**
1517** If either operand is NULL (and thus if the result is unknown) then
1518** take the jump if P1 is true.
1519**
1520** The strcmp() library routine is used for the comparison. For a
1521** numeric comparison, use OP_Le.
1522**
1523** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1524** stack if the jump would have been taken, or a 0 if not. Push a
1525** NULL if either operand was NULL.
1526*/
1527/* Opcode: StrGt P1 P2 *
1528**
1529** Pop the top two elements from the stack. If second element (the
1530** next on stack) is greater than the first (the top of stack),
1531** then jump to instruction P2. In other words, jump if NOS>TOS.
1532**
1533** If either operand is NULL (and thus if the result is unknown) then
1534** take the jump if P1 is true.
1535**
1536** The strcmp() library routine is used for the comparison. For a
1537** numeric comparison, use OP_Gt.
1538**
1539** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1540** stack if the jump would have been taken, or a 0 if not. Push a
1541** NULL if either operand was NULL.
1542*/
1543/* Opcode: StrGe P1 P2 *
1544**
1545** Pop the top two elements from the stack. If second element (the next
1546** on stack) is greater than or equal to the first (the top of stack),
1547** then jump to instruction P2. In other words, jump if NOS>=TOS.
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_Ge.
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*/
1559case OP_StrEq:
1560case OP_StrNe:
1561case OP_StrLt:
1562case OP_StrLe:
1563case OP_StrGt:
1564case OP_StrGe: {
1565 Mem *pNos = &pTos[-1];
1566 int c;
1567 assert( pNos>=p->aStack );
1568 if( (pNos->flags | pTos->flags) & MEM_Null ){
1569 popStack(&pTos, 2);
1570 if( pOp->p2 ){
1571 if( pOp->p1 ) pc = pOp->p2-1;
1572 }else{
1573 pTos++;
1574 pTos->flags = MEM_Null;
1575 }
1576 break;
1577 }else{
1578 Stringify(pTos);
1579 Stringify(pNos);
1580 c = strcmp(pNos->z, pTos->z);
1581 }
1582 /* The asserts on each case of the following switch are there to verify
1583 ** that string comparison opcodes are always exactly 6 greater than the
1584 ** corresponding numeric comparison opcodes. The code generator depends
1585 ** on this fact.
1586 */
1587 switch( pOp->opcode ){
1588 case OP_StrEq: c = c==0; assert( pOp->opcode-6==OP_Eq ); break;
1589 case OP_StrNe: c = c!=0; assert( pOp->opcode-6==OP_Ne ); break;
1590 case OP_StrLt: c = c<0; assert( pOp->opcode-6==OP_Lt ); break;
1591 case OP_StrLe: c = c<=0; assert( pOp->opcode-6==OP_Le ); break;
1592 case OP_StrGt: c = c>0; assert( pOp->opcode-6==OP_Gt ); break;
1593 default: c = c>=0; assert( pOp->opcode-6==OP_Ge ); break;
1594 }
1595 popStack(&pTos, 2);
1596 if( pOp->p2 ){
1597 if( c ) pc = pOp->p2-1;
1598 }else{
1599 pTos++;
1600 pTos->flags = MEM_Int;
1601 pTos->i = c;
1602 }
1603 break;
1604}
1605
1606/* Opcode: And * * *
1607**
1608** Pop two values off the stack. Take the logical AND of the
1609** two values and push the resulting boolean value back onto the
1610** stack.
1611*/
1612/* Opcode: Or * * *
1613**
1614** Pop two values off the stack. Take the logical OR of the
1615** two values and push the resulting boolean value back onto the
1616** stack.
1617*/
1618case OP_And:
1619case OP_Or: {
1620 Mem *pNos = &pTos[-1];
1621 int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1622
1623 assert( pNos>=p->aStack );
1624 if( pTos->flags & MEM_Null ){
1625 v1 = 2;
1626 }else{
1627 Integerify(pTos);
1628 v1 = pTos->i==0;
1629 }
1630 if( pNos->flags & MEM_Null ){
1631 v2 = 2;
1632 }else{
1633 Integerify(pNos);
1634 v2 = pNos->i==0;
1635 }
1636 if( pOp->opcode==OP_And ){
1637 static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1638 v1 = and_logic[v1*3+v2];
1639 }else{
1640 static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1641 v1 = or_logic[v1*3+v2];
1642 }
1643 popStack(&pTos, 2);
1644 pTos++;
1645 if( v1==2 ){
1646 pTos->flags = MEM_Null;
1647 }else{
1648 pTos->i = v1==0;
1649 pTos->flags = MEM_Int;
1650 }
1651 break;
1652}
1653
1654/* Opcode: Negative * * *
1655**
1656** Treat the top of the stack as a numeric quantity. Replace it
1657** with its additive inverse. If the top of the stack is NULL
1658** its value is unchanged.
1659*/
1660/* Opcode: AbsValue * * *
1661**
1662** Treat the top of the stack as a numeric quantity. Replace it
1663** with its absolute value. If the top of the stack is NULL
1664** its value is unchanged.
1665*/
1666case OP_Negative:
1667case OP_AbsValue: {
1668 assert( pTos>=p->aStack );
1669 if( pTos->flags & MEM_Real ){
1670 Release(pTos);
1671 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1672 pTos->r = -pTos->r;
1673 }
1674 pTos->flags = MEM_Real;
1675 }else if( pTos->flags & MEM_Int ){
1676 Release(pTos);
1677 if( pOp->opcode==OP_Negative || pTos->i<0 ){
1678 pTos->i = -pTos->i;
1679 }
1680 pTos->flags = MEM_Int;
1681 }else if( pTos->flags & MEM_Null ){
1682 /* Do nothing */
1683 }else{
1684 Realify(pTos);
1685 Release(pTos);
1686 if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1687 pTos->r = -pTos->r;
1688 }
1689 pTos->flags = MEM_Real;
1690 }
1691 break;
1692}
1693
1694/* Opcode: Not * * *
1695**
1696** Interpret the top of the stack as a boolean value. Replace it
1697** with its complement. If the top of the stack is NULL its value
1698** is unchanged.
1699*/
1700case OP_Not: {
1701 assert( pTos>=p->aStack );
1702 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1703 Integerify(pTos);
1704 Release(pTos);
1705 pTos->i = !pTos->i;
1706 pTos->flags = MEM_Int;
1707 break;
1708}
1709
1710/* Opcode: BitNot * * *
1711**
1712** Interpret the top of the stack as an value. Replace it
1713** with its ones-complement. If the top of the stack is NULL its
1714** value is unchanged.
1715*/
1716case OP_BitNot: {
1717 assert( pTos>=p->aStack );
1718 if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1719 Integerify(pTos);
1720 Release(pTos);
1721 pTos->i = ~pTos->i;
1722 pTos->flags = MEM_Int;
1723 break;
1724}
1725
1726/* Opcode: Noop * * *
1727**
1728** Do nothing. This instruction is often useful as a jump
1729** destination.
1730*/
1731case OP_Noop: {
1732 break;
1733}
1734
1735/* Opcode: If P1 P2 *
1736**
1737** Pop a single boolean from the stack. If the boolean popped is
1738** true, then jump to p2. Otherwise continue to the next instruction.
1739** An integer is false if zero and true otherwise. A string is
1740** false if it has zero length and true otherwise.
1741**
1742** If the value popped of the stack is NULL, then take the jump if P1
1743** is true and fall through if P1 is false.
1744*/
1745/* Opcode: IfNot P1 P2 *
1746**
1747** Pop a single boolean from the stack. If the boolean popped is
1748** false, then jump to p2. Otherwise continue to the next instruction.
1749** An integer is false if zero and true otherwise. A string is
1750** false if it has zero length and true otherwise.
1751**
1752** If the value popped of the stack is NULL, then take the jump if P1
1753** is true and fall through if P1 is false.
1754*/
1755case OP_If:
1756case OP_IfNot: {
1757 int c;
1758 assert( pTos>=p->aStack );
1759 if( pTos->flags & MEM_Null ){
1760 c = pOp->p1;
1761 }else{
1762 Integerify(pTos);
1763 c = pTos->i;
1764 if( pOp->opcode==OP_IfNot ) c = !c;
1765 }
1766 assert( (pTos->flags & MEM_Dyn)==0 );
1767 pTos--;
1768 if( c ) pc = pOp->p2-1;
1769 break;
1770}
1771
1772/* Opcode: IsNull P1 P2 *
1773**
1774** If any of the top abs(P1) values on the stack are NULL, then jump
1775** to P2. Pop the stack P1 times if P1>0. If P1<0 leave the stack
1776** unchanged.
1777*/
1778case OP_IsNull: {
1779 int i, cnt;
1780 Mem *pTerm;
1781 cnt = pOp->p1;
1782 if( cnt<0 ) cnt = -cnt;
1783 pTerm = &pTos[1-cnt];
1784 assert( pTerm>=p->aStack );
1785 for(i=0; i<cnt; i++, pTerm++){
1786 if( pTerm->flags & MEM_Null ){
1787 pc = pOp->p2-1;
1788 break;
1789 }
1790 }
1791 if( pOp->p1>0 ) popStack(&pTos, cnt);
1792 break;
1793}
1794
1795/* Opcode: NotNull P1 P2 *
1796**
1797** Jump to P2 if the top P1 values on the stack are all not NULL. Pop the
1798** stack if P1 times if P1 is greater than zero. If P1 is less than
1799** zero then leave the stack unchanged.
1800*/
1801case OP_NotNull: {
1802 int i, cnt;
1803 cnt = pOp->p1;
1804 if( cnt<0 ) cnt = -cnt;
1805 assert( &pTos[1-cnt] >= p->aStack );
1806 for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
1807 if( i>=cnt ) pc = pOp->p2-1;
1808 if( pOp->p1>0 ) popStack(&pTos, cnt);
1809 break;
1810}
1811
1812/* Opcode: MakeRecord P1 P2 *
1813**
1814** Convert the top P1 entries of the stack into a single entry
1815** suitable for use as a data record in a database table. The
1816** details of the format are irrelavant as long as the OP_Column
1817** opcode can decode the record later. Refer to source code
1818** comments for the details of the record format.
1819**
1820** If P2 is true (non-zero) and one or more of the P1 entries
1821** that go into building the record is NULL, then add some extra
1822** bytes to the record to make it distinct for other entries created
1823** during the same run of the VDBE. The extra bytes added are a
1824** counter that is reset with each run of the VDBE, so records
1825** created this way will not necessarily be distinct across runs.
1826** But they should be distinct for transient tables (created using
1827** OP_OpenTemp) which is what they are intended for.
1828**
1829** (Later:) The P2==1 option was intended to make NULLs distinct
1830** for the UNION operator. But I have since discovered that NULLs
1831** are indistinct for UNION. So this option is never used.
1832*/
1833case OP_MakeRecord: {
1834 char *zNewRecord;
1835 int nByte;
1836 int nField;
1837 int i, j;
1838 int idxWidth;
1839 u32 addr;
1840 Mem *pRec;
1841 int addUnique = 0; /* True to cause bytes to be added to make the
1842 ** generated record distinct */
1843 char zTemp[NBFS]; /* Temp space for small records */
1844
1845 /* Assuming the record contains N fields, the record format looks
1846 ** like this:
1847 **
1848 ** -------------------------------------------------------------------
1849 ** | idx0 | idx1 | ... | idx(N-1) | idx(N) | data0 | ... | data(N-1) |
1850 ** -------------------------------------------------------------------
1851 **
1852 ** All data fields are converted to strings before being stored and
1853 ** are stored with their null terminators. NULL entries omit the
1854 ** null terminator. Thus an empty string uses 1 byte and a NULL uses
1855 ** zero bytes. Data(0) is taken from the lowest element of the stack
1856 ** and data(N-1) is the top of the stack.
1857 **
1858 ** Each of the idx() entries is either 1, 2, or 3 bytes depending on
1859 ** how big the total record is. Idx(0) contains the offset to the start
1860 ** of data(0). Idx(k) contains the offset to the start of data(k).
1861 ** Idx(N) contains the total number of bytes in the record.
1862 */
1863 nField = pOp->p1;
1864 pRec = &pTos[1-nField];
1865 assert( pRec>=p->aStack );
1866 nByte = 0;
1867 for(i=0; i<nField; i++, pRec++){
1868 if( pRec->flags & MEM_Null ){
1869 addUnique = pOp->p2;
1870 }else{
1871 Stringify(pRec);
1872 nByte += pRec->n;
1873 }
1874 }
1875 if( addUnique ) nByte += sizeof(p->uniqueCnt);
1876 if( nByte + nField + 1 < 256 ){
1877 idxWidth = 1;
1878 }else if( nByte + 2*nField + 2 < 65536 ){
1879 idxWidth = 2;
1880 }else{
1881 idxWidth = 3;
1882 }
1883 nByte += idxWidth*(nField + 1);
1884 if( nByte>MAX_BYTES_PER_ROW ){
1885 rc = SQLITE_TOOBIG;
1886 goto abort_due_to_error;
1887 }
1888 if( nByte<=NBFS ){
1889 zNewRecord = zTemp;
1890 }else{
1891 zNewRecord = sqliteMallocRaw( nByte );
1892 if( zNewRecord==0 ) goto no_mem;
1893 }
1894 j = 0;
1895 addr = idxWidth*(nField+1) + addUnique*sizeof(p->uniqueCnt);
1896 for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
1897 zNewRecord[j++] = addr & 0xff;
1898 if( idxWidth>1 ){
1899 zNewRecord[j++] = (addr>>8)&0xff;
1900 if( idxWidth>2 ){
1901 zNewRecord[j++] = (addr>>16)&0xff;
1902 }
1903 }
1904 if( (pRec->flags & MEM_Null)==0 ){
1905 addr += pRec->n;
1906 }
1907 }
1908 zNewRecord[j++] = addr & 0xff;
1909 if( idxWidth>1 ){
1910 zNewRecord[j++] = (addr>>8)&0xff;
1911 if( idxWidth>2 ){
1912 zNewRecord[j++] = (addr>>16)&0xff;
1913 }
1914 }
1915 if( addUnique ){
1916 memcpy(&zNewRecord[j], &p->uniqueCnt, sizeof(p->uniqueCnt));
1917 p->uniqueCnt++;
1918 j += sizeof(p->uniqueCnt);
1919 }
1920 for(i=0, pRec=&pTos[1-nField]; i<nField; i++, pRec++){
1921 if( (pRec->flags & MEM_Null)==0 ){
1922 memcpy(&zNewRecord[j], pRec->z, pRec->n);
1923 j += pRec->n;
1924 }
1925 }
1926 popStack(&pTos, nField);
1927 pTos++;
1928 pTos->n = nByte;
1929 if( nByte<=NBFS ){
1930 assert( zNewRecord==zTemp );
1931 memcpy(pTos->zShort, zTemp, nByte);
1932 pTos->z = pTos->zShort;
1933 pTos->flags = MEM_Str | MEM_Short;
1934 }else{
1935 assert( zNewRecord!=zTemp );
1936 pTos->z = zNewRecord;
1937 pTos->flags = MEM_Str | MEM_Dyn;
1938 }
1939 break;
1940}
1941
1942/* Opcode: MakeKey P1 P2 P3
1943**
1944** Convert the top P1 entries of the stack into a single entry suitable
1945** for use as the key in an index. The top P1 records are
1946** converted to strings and merged. The null-terminators
1947** are retained and used as separators.
1948** The lowest entry in the stack is the first field and the top of the
1949** stack becomes the last.
1950**
1951** If P2 is not zero, then the original entries remain on the stack
1952** and the new key is pushed on top. If P2 is zero, the original
1953** data is popped off the stack first then the new key is pushed
1954** back in its place.
1955**
1956** P3 is a string that is P1 characters long. Each character is either
1957** an 'n' or a 't' to indicates if the argument should be intepreted as
1958** numeric or text type. The first character of P3 corresponds to the
1959** lowest element on the stack. If P3 is NULL then all arguments are
1960** assumed to be of the numeric type.
1961**
1962** The type makes a difference in that text-type fields may not be
1963** introduced by 'b' (as described in the next paragraph). The
1964** first character of a text-type field must be either 'a' (if it is NULL)
1965** or 'c'. Numeric fields will be introduced by 'b' if their content
1966** looks like a well-formed number. Otherwise the 'a' or 'c' will be
1967** used.
1968**
1969** The key is a concatenation of fields. Each field is terminated by
1970** a single 0x00 character. A NULL field is introduced by an 'a' and
1971** is followed immediately by its 0x00 terminator. A numeric field is
1972** introduced by a single character 'b' and is followed by a sequence
1973** of characters that represent the number such that a comparison of
1974** the character string using memcpy() sorts the numbers in numerical
1975** order. The character strings for numbers are generated using the
1976** sqliteRealToSortable() function. A text field is introduced by a
1977** 'c' character and is followed by the exact text of the field. The
1978** use of an 'a', 'b', or 'c' character at the beginning of each field
1979** guarantees that NULLs sort before numbers and that numbers sort
1980** before text. 0x00 characters do not occur except as separators
1981** between fields.
1982**
1983** See also: MakeIdxKey, SortMakeKey
1984*/
1985/* Opcode: MakeIdxKey P1 P2 P3
1986**
1987** Convert the top P1 entries of the stack into a single entry suitable
1988** for use as the key in an index. In addition, take one additional integer
1989** off of the stack, treat that integer as a four-byte record number, and
1990** append the four bytes to the key. Thus a total of P1+1 entries are
1991** popped from the stack for this instruction and a single entry is pushed
1992** back. The first P1 entries that are popped are strings and the last
1993** entry (the lowest on the stack) is an integer record number.
1994**
1995** The converstion of the first P1 string entries occurs just like in
1996** MakeKey. Each entry is separated from the others by a null.
1997** The entire concatenation is null-terminated. The lowest entry
1998** in the stack is the first field and the top of the stack becomes the
1999** last.
2000**
2001** If P2 is not zero and one or more of the P1 entries that go into the
2002** generated key is NULL, then jump to P2 after the new key has been
2003** pushed on the stack. In other words, jump to P2 if the key is
2004** guaranteed to be unique. This jump can be used to skip a subsequent
2005** uniqueness test.
2006**
2007** P3 is a string that is P1 characters long. Each character is either
2008** an 'n' or a 't' to indicates if the argument should be numeric or
2009** text. The first character corresponds to the lowest element on the
2010** stack. If P3 is null then all arguments are assumed to be numeric.
2011**
2012** See also: MakeKey, SortMakeKey
2013*/
2014case OP_MakeIdxKey:
2015case OP_MakeKey: {
2016 char *zNewKey;
2017 int nByte;
2018 int nField;
2019 int addRowid;
2020 int i, j;
2021 int containsNull = 0;
2022 Mem *pRec;
2023 char zTemp[NBFS];
2024
2025 addRowid = pOp->opcode==OP_MakeIdxKey;
2026 nField = pOp->p1;
2027 pRec = &pTos[1-nField];
2028 assert( pRec>=p->aStack );
2029 nByte = 0;
2030 for(j=0, i=0; i<nField; i++, j++, pRec++){
2031 int flags = pRec->flags;
2032 int len;
2033 char *z;
2034 if( flags & MEM_Null ){
2035 nByte += 2;
2036 containsNull = 1;
2037 }else if( pOp->p3 && pOp->p3[j]=='t' ){
2038 Stringify(pRec);
2039 pRec->flags &= ~(MEM_Int|MEM_Real);
2040 nByte += pRec->n+1;
2041 }else if( (flags & (MEM_Real|MEM_Int))!=0 || sqliteIsNumber(pRec->z) ){
2042 if( (flags & (MEM_Real|MEM_Int))==MEM_Int ){
2043 pRec->r = pRec->i;
2044 }else if( (flags & (MEM_Real|MEM_Int))==0 ){
2045 pRec->r = sqliteAtoF(pRec->z, 0);
2046 }
2047 Release(pRec);
2048 z = pRec->zShort;
2049 sqliteRealToSortable(pRec->r, z);
2050 len = strlen(z);
2051 pRec->z = 0;
2052 pRec->flags = MEM_Real;
2053 pRec->n = len+1;
2054 nByte += pRec->n+1;
2055 }else{
2056 nByte += pRec->n+1;
2057 }
2058 }
2059 if( nByte+sizeof(u32)>MAX_BYTES_PER_ROW ){
2060 rc = SQLITE_TOOBIG;
2061 goto abort_due_to_error;
2062 }
2063 if( addRowid ) nByte += sizeof(u32);
2064 if( nByte<=NBFS ){
2065 zNewKey = zTemp;
2066 }else{
2067 zNewKey = sqliteMallocRaw( nByte );
2068 if( zNewKey==0 ) goto no_mem;
2069 }
2070 j = 0;
2071 pRec = &pTos[1-nField];
2072 for(i=0; i<nField; i++, pRec++){
2073 if( pRec->flags & MEM_Null ){
2074 zNewKey[j++] = 'a';
2075 zNewKey[j++] = 0;
2076 }else if( pRec->flags==MEM_Real ){
2077 zNewKey[j++] = 'b';
2078 memcpy(&zNewKey[j], pRec->zShort, pRec->n);
2079 j += pRec->n;
2080 }else{
2081 assert( pRec->flags & MEM_Str );
2082 zNewKey[j++] = 'c';
2083 memcpy(&zNewKey[j], pRec->z, pRec->n);
2084 j += pRec->n;
2085 }
2086 }
2087 if( addRowid ){
2088 u32 iKey;
2089 pRec = &pTos[-nField];
2090 assert( pRec>=p->aStack );
2091 Integerify(pRec);
2092 iKey = intToKey(pRec->i);
2093 memcpy(&zNewKey[j], &iKey, sizeof(u32));
2094 popStack(&pTos, nField+1);
2095 if( pOp->p2 && containsNull ) pc = pOp->p2 - 1;
2096 }else{
2097 if( pOp->p2==0 ) popStack(&pTos, nField);
2098 }
2099 pTos++;
2100 pTos->n = nByte;
2101 if( nByte<=NBFS ){
2102 assert( zNewKey==zTemp );
2103 pTos->z = pTos->zShort;
2104 memcpy(pTos->zShort, zTemp, nByte);
2105 pTos->flags = MEM_Str | MEM_Short;
2106 }else{
2107 pTos->z = zNewKey;
2108 pTos->flags = MEM_Str | MEM_Dyn;
2109 }
2110 break;
2111}
2112
2113/* Opcode: IncrKey * * *
2114**
2115** The top of the stack should contain an index key generated by
2116** The MakeKey opcode. This routine increases the least significant
2117** byte of that key by one. This is used so that the MoveTo opcode
2118** will move to the first entry greater than the key rather than to
2119** the key itself.
2120*/
2121case OP_IncrKey: {
2122 assert( pTos>=p->aStack );
2123 /* The IncrKey opcode is only applied to keys generated by
2124 ** MakeKey or MakeIdxKey and the results of those operands
2125 ** are always dynamic strings or zShort[] strings. So we
2126 ** are always free to modify the string in place.
2127 */
2128 assert( pTos->flags & (MEM_Dyn|MEM_Short) );
2129 pTos->z[pTos->n-1]++;
2130 break;
2131}
2132
2133/* Opcode: Checkpoint P1 * *
2134**
2135** Begin a checkpoint. A checkpoint is the beginning of a operation that
2136** is part of a larger transaction but which might need to be rolled back
2137** itself without effecting the containing transaction. A checkpoint will
2138** be automatically committed or rollback when the VDBE halts.
2139**
2140** The checkpoint is begun on the database file with index P1. The main
2141** database file has an index of 0 and the file used for temporary tables
2142** has an index of 1.
2143*/
2144case OP_Checkpoint: {
2145 int i = pOp->p1;
2146 if( i>=0 && i<db->nDb && db->aDb[i].pBt && db->aDb[i].inTrans==1 ){
2147 rc = sqliteBtreeBeginCkpt(db->aDb[i].pBt);
2148 if( rc==SQLITE_OK ) db->aDb[i].inTrans = 2;
2149 }
2150 break;
2151}
2152
2153/* Opcode: Transaction P1 * *
2154**
2155** Begin a transaction. The transaction ends when a Commit or Rollback
2156** opcode is encountered. Depending on the ON CONFLICT setting, the
2157** transaction might also be rolled back if an error is encountered.
2158**
2159** P1 is the index of the database file on which the transaction is
2160** started. Index 0 is the main database file and index 1 is the
2161** file used for temporary tables.
2162**
2163** A write lock is obtained on the database file when a transaction is
2164** started. No other process can read or write the file while the
2165** transaction is underway. Starting a transaction also creates a
2166** rollback journal. A transaction must be started before any changes
2167** can be made to the database.
2168*/
2169case OP_Transaction: {
2170 int busy = 1;
2171 int i = pOp->p1;
2172 assert( i>=0 && i<db->nDb );
2173 if( db->aDb[i].inTrans ) break;
2174 while( db->aDb[i].pBt!=0 && busy ){
2175 rc = sqliteBtreeBeginTrans(db->aDb[i].pBt);
2176 switch( rc ){
2177 case SQLITE_BUSY: {
2178 if( db->xBusyCallback==0 ){
2179 p->pc = pc;
2180 p->undoTransOnError = 1;
2181 p->rc = SQLITE_BUSY;
2182 p->pTos = pTos;
2183 return SQLITE_BUSY;
2184 }else if( (*db->xBusyCallback)(db->pBusyArg, "", busy++)==0 ){
2185 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
2186 busy = 0;
2187 }
2188 break;
2189 }
2190 case SQLITE_READONLY: {
2191 rc = SQLITE_OK;
2192 /* Fall thru into the next case */
2193 }
2194 case SQLITE_OK: {
2195 p->inTempTrans = 0;
2196 busy = 0;
2197 break;
2198 }
2199 default: {
2200 goto abort_due_to_error;
2201 }
2202 }
2203 }
2204 db->aDb[i].inTrans = 1;
2205 p->undoTransOnError = 1;
2206 break;
2207}
2208
2209/* Opcode: Commit * * *
2210**
2211** Cause all modifications to the database that have been made since the
2212** last Transaction to actually take effect. No additional modifications
2213** are allowed until another transaction is started. The Commit instruction
2214** deletes the journal file and releases the write lock on the database.
2215** A read lock continues to be held if there are still cursors open.
2216*/
2217case OP_Commit: {
2218 int i;
2219 if( db->xCommitCallback!=0 ){
2220 if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
2221 if( db->xCommitCallback(db->pCommitArg)!=0 ){
2222 rc = SQLITE_CONSTRAINT;
2223 }
2224 if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
2225 }
2226 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
2227 if( db->aDb[i].inTrans ){
2228 rc = sqliteBtreeCommit(db->aDb[i].pBt);
2229 db->aDb[i].inTrans = 0;
2230 }
2231 }
2232 if( rc==SQLITE_OK ){
2233 sqliteCommitInternalChanges(db);
2234 }else{
2235 sqliteRollbackAll(db);
2236 }
2237 break;
2238}
2239
2240/* Opcode: Rollback P1 * *
2241**
2242** Cause all modifications to the database that have been made since the
2243** last Transaction to be undone. The database is restored to its state
2244** before the Transaction opcode was executed. No additional modifications
2245** are allowed until another transaction is started.
2246**
2247** P1 is the index of the database file that is committed. An index of 0
2248** is used for the main database and an index of 1 is used for the file used
2249** to hold temporary tables.
2250**
2251** This instruction automatically closes all cursors and releases both
2252** the read and write locks on the indicated database.
2253*/
2254case OP_Rollback: {
2255 sqliteRollbackAll(db);
2256 break;
2257}
2258
2259/* Opcode: ReadCookie P1 P2 *
2260**
2261** Read cookie number P2 from database P1 and push it onto the stack.
2262** P2==0 is the schema version. P2==1 is the database format.
2263** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2264** the main database file and P1==1 is the database file used to store
2265** temporary tables.
2266**
2267** There must be a read-lock on the database (either a transaction
2268** must be started or there must be an open cursor) before
2269** executing this instruction.
2270*/
2271case OP_ReadCookie: {
2272 int aMeta[SQLITE_N_BTREE_META];
2273 assert( pOp->p2<SQLITE_N_BTREE_META );
2274 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2275 assert( db->aDb[pOp->p1].pBt!=0 );
2276 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2277 pTos++;
2278 pTos->i = aMeta[1+pOp->p2];
2279 pTos->flags = MEM_Int;
2280 break;
2281}
2282
2283/* Opcode: SetCookie P1 P2 *
2284**
2285** Write the top of the stack into cookie number P2 of database P1.
2286** P2==0 is the schema version. P2==1 is the database format.
2287** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2288** the main database file and P1==1 is the database file used to store
2289** temporary tables.
2290**
2291** A transaction must be started before executing this opcode.
2292*/
2293case OP_SetCookie: {
2294 int aMeta[SQLITE_N_BTREE_META];
2295 assert( pOp->p2<SQLITE_N_BTREE_META );
2296 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2297 assert( db->aDb[pOp->p1].pBt!=0 );
2298 assert( pTos>=p->aStack );
2299 Integerify(pTos)
2300 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2301 if( rc==SQLITE_OK ){
2302 aMeta[1+pOp->p2] = pTos->i;
2303 rc = sqliteBtreeUpdateMeta(db->aDb[pOp->p1].pBt, aMeta);
2304 }
2305 Release(pTos);
2306 pTos--;
2307 break;
2308}
2309
2310/* Opcode: VerifyCookie P1 P2 *
2311**
2312** Check the value of global database parameter number 0 (the
2313** schema version) and make sure it is equal to P2.
2314** P1 is the database number which is 0 for the main database file
2315** and 1 for the file holding temporary tables and some higher number
2316** for auxiliary databases.
2317**
2318** The cookie changes its value whenever the database schema changes.
2319** This operation is used to detect when that the cookie has changed
2320** and that the current process needs to reread the schema.
2321**
2322** Either a transaction needs to have been started or an OP_Open needs
2323** to be executed (to establish a read lock) before this opcode is
2324** invoked.
2325*/
2326case OP_VerifyCookie: {
2327 int aMeta[SQLITE_N_BTREE_META];
2328 assert( pOp->p1>=0 && pOp->p1<db->nDb );
2329 rc = sqliteBtreeGetMeta(db->aDb[pOp->p1].pBt, aMeta);
2330 if( rc==SQLITE_OK && aMeta[1]!=pOp->p2 ){
2331 sqliteSetString(&p->zErrMsg, "database schema has changed", (char*)0);
2332 rc = SQLITE_SCHEMA;
2333 }
2334 break;
2335}
2336
2337/* Opcode: OpenRead P1 P2 P3
2338**
2339** Open a read-only cursor for the database table whose root page is
2340** P2 in a database file. The database file is determined by an
2341** integer from the top of the stack. 0 means the main database and
2342** 1 means the database used for temporary tables. Give the new
2343** cursor an identifier of P1. The P1 values need not be contiguous
2344** but all P1 values should be small integers. It is an error for
2345** P1 to be negative.
2346**
2347** If P2==0 then take the root page number from the next of the stack.
2348**
2349** There will be a read lock on the database whenever there is an
2350** open cursor. If the database was unlocked prior to this instruction
2351** then a read lock is acquired as part of this instruction. A read
2352** lock allows other processes to read the database but prohibits
2353** any other process from modifying the database. The read lock is
2354** released when all cursors are closed. If this instruction attempts
2355** to get a read lock but fails, the script terminates with an
2356** SQLITE_BUSY error code.
2357**
2358** The P3 value is the name of the table or index being opened.
2359** The P3 value is not actually used by this opcode and may be
2360** omitted. But the code generator usually inserts the index or
2361** table name into P3 to make the code easier to read.
2362**
2363** See also OpenWrite.
2364*/
2365/* Opcode: OpenWrite P1 P2 P3
2366**
2367** Open a read/write cursor named P1 on the table or index whose root
2368** page is P2. If P2==0 then take the root page number from the stack.
2369**
2370** The P3 value is the name of the table or index being opened.
2371** The P3 value is not actually used by this opcode and may be
2372** omitted. But the code generator usually inserts the index or
2373** table name into P3 to make the code easier to read.
2374**
2375** This instruction works just like OpenRead except that it opens the cursor
2376** in read/write mode. For a given table, there can be one or more read-only
2377** cursors or a single read/write cursor but not both.
2378**
2379** See also OpenRead.
2380*/
2381case OP_OpenRead:
2382case OP_OpenWrite: {
2383 int busy = 0;
2384 int i = pOp->p1;
2385 int p2 = pOp->p2;
2386 int wrFlag;
2387 Btree *pX;
2388 int iDb;
2389
2390 assert( pTos>=p->aStack );
2391 Integerify(pTos);
2392 iDb = pTos->i;
2393 pTos--;
2394 assert( iDb>=0 && iDb<db->nDb );
2395 pX = db->aDb[iDb].pBt;
2396 assert( pX!=0 );
2397 wrFlag = pOp->opcode==OP_OpenWrite;
2398 if( p2<=0 ){
2399 assert( pTos>=p->aStack );
2400 Integerify(pTos);
2401 p2 = pTos->i;
2402 pTos--;
2403 if( p2<2 ){
2404 sqliteSetString(&p->zErrMsg, "root page number less than 2", (char*)0);
2405 rc = SQLITE_INTERNAL;
2406 break;
2407 }
2408 }
2409 assert( i>=0 );
2410 if( expandCursorArraySize(p, i) ) goto no_mem;
2411 sqliteVdbeCleanupCursor(&p->aCsr[i]);
2412 memset(&p->aCsr[i], 0, sizeof(Cursor));
2413 p->aCsr[i].nullRow = 1;
2414 if( pX==0 ) break;
2415 do{
2416 rc = sqliteBtreeCursor(pX, p2, wrFlag, &p->aCsr[i].pCursor);
2417 switch( rc ){
2418 case SQLITE_BUSY: {
2419 if( db->xBusyCallback==0 ){
2420 p->pc = pc;
2421 p->rc = SQLITE_BUSY;
2422 p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
2423 return SQLITE_BUSY;
2424 }else if( (*db->xBusyCallback)(db->pBusyArg, pOp->p3, ++busy)==0 ){
2425 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
2426 busy = 0;
2427 }
2428 break;
2429 }
2430 case SQLITE_OK: {
2431 busy = 0;
2432 break;
2433 }
2434 default: {
2435 goto abort_due_to_error;
2436 }
2437 }
2438 }while( busy );
2439 break;
2440}
2441
2442/* Opcode: OpenTemp P1 P2 *
2443**
2444** Open a new cursor to a transient table.
2445** The transient cursor is always opened read/write even if
2446** the main database is read-only. The transient table is deleted
2447** automatically when the cursor is closed.
2448**
2449** The cursor points to a BTree table if P2==0 and to a BTree index
2450** if P2==1. A BTree table must have an integer key and can have arbitrary
2451** data. A BTree index has no data but can have an arbitrary key.
2452**
2453** This opcode is used for tables that exist for the duration of a single
2454** SQL statement only. Tables created using CREATE TEMPORARY TABLE
2455** are opened using OP_OpenRead or OP_OpenWrite. "Temporary" in the
2456** context of this opcode means for the duration of a single SQL statement
2457** whereas "Temporary" in the context of CREATE TABLE means for the duration
2458** of the connection to the database. Same word; different meanings.
2459*/
2460case OP_OpenTemp: {
2461 int i = pOp->p1;
2462 Cursor *pCx;
2463 assert( i>=0 );
2464 if( expandCursorArraySize(p, i) ) goto no_mem;
2465 pCx = &p->aCsr[i];
2466 sqliteVdbeCleanupCursor(pCx);
2467 memset(pCx, 0, sizeof(*pCx));
2468 pCx->nullRow = 1;
2469 rc = sqliteBtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
2470
2471 if( rc==SQLITE_OK ){
2472 rc = sqliteBtreeBeginTrans(pCx->pBt);
2473 }
2474 if( rc==SQLITE_OK ){
2475 if( pOp->p2 ){
2476 int pgno;
2477 rc = sqliteBtreeCreateIndex(pCx->pBt, &pgno);
2478 if( rc==SQLITE_OK ){
2479 rc = sqliteBtreeCursor(pCx->pBt, pgno, 1, &pCx->pCursor);
2480 }
2481 }else{
2482 rc = sqliteBtreeCursor(pCx->pBt, 2, 1, &pCx->pCursor);
2483 }
2484 }
2485 break;
2486}
2487
2488/* Opcode: OpenPseudo P1 * *
2489**
2490** Open a new cursor that points to a fake table that contains a single
2491** row of data. Any attempt to write a second row of data causes the
2492** first row to be deleted. All data is deleted when the cursor is
2493** closed.
2494**
2495** A pseudo-table created by this opcode is useful for holding the
2496** NEW or OLD tables in a trigger.
2497*/
2498case OP_OpenPseudo: {
2499 int i = pOp->p1;
2500 Cursor *pCx;
2501 assert( i>=0 );
2502 if( expandCursorArraySize(p, i) ) goto no_mem;
2503 pCx = &p->aCsr[i];
2504 sqliteVdbeCleanupCursor(pCx);
2505 memset(pCx, 0, sizeof(*pCx));
2506 pCx->nullRow = 1;
2507 pCx->pseudoTable = 1;
2508 break;
2509}
2510
2511/* Opcode: Close P1 * *
2512**
2513** Close a cursor previously opened as P1. If P1 is not
2514** currently open, this instruction is a no-op.
2515*/
2516case OP_Close: {
2517 int i = pOp->p1;
2518 if( i>=0 && i<p->nCursor ){
2519 sqliteVdbeCleanupCursor(&p->aCsr[i]);
2520 }
2521 break;
2522}
2523
2524/* Opcode: MoveTo P1 P2 *
2525**
2526** Pop the top of the stack and use its value as a key. Reposition
2527** cursor P1 so that it points to an entry with a matching key. If
2528** the table contains no record with a matching key, then the cursor
2529** is left pointing at the first record that is greater than the key.
2530** If there are no records greater than the key and P2 is not zero,
2531** then an immediate jump to P2 is made.
2532**
2533** See also: Found, NotFound, Distinct, MoveLt
2534*/
2535/* Opcode: MoveLt P1 P2 *
2536**
2537** Pop the top of the stack and use its value as a key. Reposition
2538** cursor P1 so that it points to the entry with the largest key that is
2539** less than the key popped from the stack.
2540** If there are no records less than than the key and P2
2541** is not zero then an immediate jump to P2 is made.
2542**
2543** See also: MoveTo
2544*/
2545case OP_MoveLt:
2546case OP_MoveTo: {
2547 int i = pOp->p1;
2548 Cursor *pC;
2549
2550 assert( pTos>=p->aStack );
2551 assert( i>=0 && i<p->nCursor );
2552 pC = &p->aCsr[i];
2553 if( pC->pCursor!=0 ){
2554 int res, oc;
2555 pC->nullRow = 0;
2556 if( pTos->flags & MEM_Int ){
2557 int iKey = intToKey(pTos->i);
2558 if( pOp->p2==0 && pOp->opcode==OP_MoveTo ){
2559 pC->movetoTarget = iKey;
2560 pC->deferredMoveto = 1;
2561 Release(pTos);
2562 pTos--;
2563 break;
2564 }
2565 sqliteBtreeMoveto(pC->pCursor, (char*)&iKey, sizeof(int), &res);
2566 pC->lastRecno = pTos->i;
2567 pC->recnoIsValid = res==0;
2568 }else{
2569 Stringify(pTos);
2570 sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2571 pC->recnoIsValid = 0;
2572 }
2573 pC->deferredMoveto = 0;
2574 sqlite_search_count++;
2575 oc = pOp->opcode;
2576 if( oc==OP_MoveTo && res<0 ){
2577 sqliteBtreeNext(pC->pCursor, &res);
2578 pC->recnoIsValid = 0;
2579 if( res && pOp->p2>0 ){
2580 pc = pOp->p2 - 1;
2581 }
2582 }else if( oc==OP_MoveLt ){
2583 if( res>=0 ){
2584 sqliteBtreePrevious(pC->pCursor, &res);
2585 pC->recnoIsValid = 0;
2586 }else{
2587 /* res might be negative because the table is empty. Check to
2588 ** see if this is the case.
2589 */
2590 int keysize;
2591 res = sqliteBtreeKeySize(pC->pCursor,&keysize)!=0 || keysize==0;
2592 }
2593 if( res && pOp->p2>0 ){
2594 pc = pOp->p2 - 1;
2595 }
2596 }
2597 }
2598 Release(pTos);
2599 pTos--;
2600 break;
2601}
2602
2603/* Opcode: Distinct P1 P2 *
2604**
2605** Use the top of the stack as a string key. If a record with that key does
2606** not exist in the table of cursor P1, then jump to P2. If the record
2607** does already exist, then fall thru. The cursor is left pointing
2608** at the record if it exists. The key is not popped from the stack.
2609**
2610** This operation is similar to NotFound except that this operation
2611** does not pop the key from the stack.
2612**
2613** See also: Found, NotFound, MoveTo, IsUnique, NotExists
2614*/
2615/* Opcode: Found P1 P2 *
2616**
2617** Use the top of the stack as a string key. If a record with that key
2618** does exist in table of P1, then jump to P2. If the record
2619** does not exist, then fall thru. The cursor is left pointing
2620** to the record if it exists. The key is popped from the stack.
2621**
2622** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
2623*/
2624/* Opcode: NotFound P1 P2 *
2625**
2626** Use the top of the stack as a string key. If a record with that key
2627** does not exist in table of P1, then jump to P2. If the record
2628** does exist, then fall thru. The cursor is left pointing to the
2629** record if it exists. The key is popped from the stack.
2630**
2631** The difference between this operation and Distinct is that
2632** Distinct does not pop the key from the stack.
2633**
2634** See also: Distinct, Found, MoveTo, NotExists, IsUnique
2635*/
2636case OP_Distinct:
2637case OP_NotFound:
2638case OP_Found: {
2639 int i = pOp->p1;
2640 int alreadyExists = 0;
2641 Cursor *pC;
2642 assert( pTos>=p->aStack );
2643 assert( i>=0 && i<p->nCursor );
2644 if( (pC = &p->aCsr[i])->pCursor!=0 ){
2645 int res, rx;
2646 Stringify(pTos);
2647 rx = sqliteBtreeMoveto(pC->pCursor, pTos->z, pTos->n, &res);
2648 alreadyExists = rx==SQLITE_OK && res==0;
2649 pC->deferredMoveto = 0;
2650 }
2651 if( pOp->opcode==OP_Found ){
2652 if( alreadyExists ) pc = pOp->p2 - 1;
2653 }else{
2654 if( !alreadyExists ) pc = pOp->p2 - 1;
2655 }
2656 if( pOp->opcode!=OP_Distinct ){
2657 Release(pTos);
2658 pTos--;
2659 }
2660 break;
2661}
2662
2663/* Opcode: IsUnique P1 P2 *
2664**
2665** The top of the stack is an integer record number. Call this
2666** record number R. The next on the stack is an index key created
2667** using MakeIdxKey. Call it K. This instruction pops R from the
2668** stack but it leaves K unchanged.
2669**
2670** P1 is an index. So all but the last four bytes of K are an
2671** index string. The last four bytes of K are a record number.
2672**
2673** This instruction asks if there is an entry in P1 where the
2674** index string matches K but the record number is different
2675** from R. If there is no such entry, then there is an immediate
2676** jump to P2. If any entry does exist where the index string
2677** matches K but the record number is not R, then the record
2678** number for that entry is pushed onto the stack and control
2679** falls through to the next instruction.
2680**
2681** See also: Distinct, NotFound, NotExists, Found
2682*/
2683case OP_IsUnique: {
2684 int i = pOp->p1;
2685 Mem *pNos = &pTos[-1];
2686 BtCursor *pCrsr;
2687 int R;
2688
2689 /* Pop the value R off the top of the stack
2690 */
2691 assert( pNos>=p->aStack );
2692 Integerify(pTos);
2693 R = pTos->i;
2694 pTos--;
2695 assert( i>=0 && i<=p->nCursor );
2696 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
2697 int res, rc;
2698 int v; /* The record number on the P1 entry that matches K */
2699 char *zKey; /* The value of K */
2700 int nKey; /* Number of bytes in K */
2701
2702 /* Make sure K is a string and make zKey point to K
2703 */
2704 Stringify(pNos);
2705 zKey = pNos->z;
2706 nKey = pNos->n;
2707 assert( nKey >= 4 );
2708
2709 /* Search for an entry in P1 where all but the last four bytes match K.
2710 ** If there is no such entry, jump immediately to P2.
2711 */
2712 assert( p->aCsr[i].deferredMoveto==0 );
2713 rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
2714 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2715 if( res<0 ){
2716 rc = sqliteBtreeNext(pCrsr, &res);
2717 if( res ){
2718 pc = pOp->p2 - 1;
2719 break;
2720 }
2721 }
2722 rc = sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &res);
2723 if( rc!=SQLITE_OK ) goto abort_due_to_error;
2724 if( res>0 ){
2725 pc = pOp->p2 - 1;
2726 break;
2727 }
2728
2729 /* At this point, pCrsr is pointing to an entry in P1 where all but
2730 ** the last for bytes of the key match K. Check to see if the last
2731 ** four bytes of the key are different from R. If the last four
2732 ** bytes equal R then jump immediately to P2.
2733 */
2734 sqliteBtreeKey(pCrsr, nKey - 4, 4, (char*)&v);
2735 v = keyToInt(v);
2736 if( v==R ){
2737 pc = pOp->p2 - 1;
2738 break;
2739 }
2740
2741 /* The last four bytes of the key are different from R. Convert the
2742 ** last four bytes of the key into an integer and push it onto the
2743 ** stack. (These bytes are the record number of an entry that
2744 ** violates a UNIQUE constraint.)
2745 */
2746 pTos++;
2747 pTos->i = v;
2748 pTos->flags = MEM_Int;
2749 }
2750 break;
2751}
2752
2753/* Opcode: NotExists P1 P2 *
2754**
2755** Use the top of the stack as a integer key. If a record with that key
2756** does not exist in table of P1, then jump to P2. If the record
2757** does exist, then fall thru. The cursor is left pointing to the
2758** record if it exists. The integer key is popped from the stack.
2759**
2760** The difference between this operation and NotFound is that this
2761** operation assumes the key is an integer and NotFound assumes it
2762** is a string.
2763**
2764** See also: Distinct, Found, MoveTo, NotFound, IsUnique
2765*/
2766case OP_NotExists: {
2767 int i = pOp->p1;
2768 BtCursor *pCrsr;
2769 assert( pTos>=p->aStack );
2770 assert( i>=0 && i<p->nCursor );
2771 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
2772 int res, rx, iKey;
2773 assert( pTos->flags & MEM_Int );
2774 iKey = intToKey(pTos->i);
2775 rx = sqliteBtreeMoveto(pCrsr, (char*)&iKey, sizeof(int), &res);
2776 p->aCsr[i].lastRecno = pTos->i;
2777 p->aCsr[i].recnoIsValid = res==0;
2778 p->aCsr[i].nullRow = 0;
2779 if( rx!=SQLITE_OK || res!=0 ){
2780 pc = pOp->p2 - 1;
2781 p->aCsr[i].recnoIsValid = 0;
2782 }
2783 }
2784 Release(pTos);
2785 pTos--;
2786 break;
2787}
2788
2789/* Opcode: NewRecno P1 * *
2790**
2791** Get a new integer record number used as the key to a table.
2792** The record number is not previously used as a key in the database
2793** table that cursor P1 points to. The new record number is pushed
2794** onto the stack.
2795*/
2796case OP_NewRecno: {
2797 int i = pOp->p1;
2798 int v = 0;
2799 Cursor *pC;
2800 assert( i>=0 && i<p->nCursor );
2801 if( (pC = &p->aCsr[i])->pCursor==0 ){
2802 v = 0;
2803 }else{
2804 /* The next rowid or record number (different terms for the same
2805 ** thing) is obtained in a two-step algorithm.
2806 **
2807 ** First we attempt to find the largest existing rowid and add one
2808 ** to that. But if the largest existing rowid is already the maximum
2809 ** positive integer, we have to fall through to the second
2810 ** probabilistic algorithm
2811 **
2812 ** The second algorithm is to select a rowid at random and see if
2813 ** it already exists in the table. If it does not exist, we have
2814 ** succeeded. If the random rowid does exist, we select a new one
2815 ** and try again, up to 1000 times.
2816 **
2817 ** For a table with less than 2 billion entries, the probability
2818 ** of not finding a unused rowid is about 1.0e-300. This is a
2819 ** non-zero probability, but it is still vanishingly small and should
2820 ** never cause a problem. You are much, much more likely to have a
2821 ** hardware failure than for this algorithm to fail.
2822 **
2823 ** The analysis in the previous paragraph assumes that you have a good
2824 ** source of random numbers. Is a library function like lrand48()
2825 ** good enough? Maybe. Maybe not. It's hard to know whether there
2826 ** might be subtle bugs is some implementations of lrand48() that
2827 ** could cause problems. To avoid uncertainty, SQLite uses its own
2828 ** random number generator based on the RC4 algorithm.
2829 **
2830 ** To promote locality of reference for repetitive inserts, the
2831 ** first few attempts at chosing a random rowid pick values just a little
2832 ** larger than the previous rowid. This has been shown experimentally
2833 ** to double the speed of the COPY operation.
2834 */
2835 int res, rx, cnt, x;
2836 cnt = 0;
2837 if( !pC->useRandomRowid ){
2838 if( pC->nextRowidValid ){
2839 v = pC->nextRowid;
2840 }else{
2841 rx = sqliteBtreeLast(pC->pCursor, &res);
2842 if( res ){
2843 v = 1;
2844 }else{
2845 sqliteBtreeKey(pC->pCursor, 0, sizeof(v), (void*)&v);
2846 v = keyToInt(v);
2847 if( v==0x7fffffff ){
2848 pC->useRandomRowid = 1;
2849 }else{
2850 v++;
2851 }
2852 }
2853 }
2854 if( v<0x7fffffff ){
2855 pC->nextRowidValid = 1;
2856 pC->nextRowid = v+1;
2857 }else{
2858 pC->nextRowidValid = 0;
2859 }
2860 }
2861 if( pC->useRandomRowid ){
2862 v = db->priorNewRowid;
2863 cnt = 0;
2864 do{
2865 if( v==0 || cnt>2 ){
2866 sqliteRandomness(sizeof(v), &v);
2867 if( cnt<5 ) v &= 0xffffff;
2868 }else{
2869 unsigned char r;
2870 sqliteRandomness(1, &r);
2871 v += r + 1;
2872 }
2873 if( v==0 ) continue;
2874 x = intToKey(v);
2875 rx = sqliteBtreeMoveto(pC->pCursor, &x, sizeof(int), &res);
2876 cnt++;
2877 }while( cnt<1000 && rx==SQLITE_OK && res==0 );
2878 db->priorNewRowid = v;
2879 if( rx==SQLITE_OK && res==0 ){
2880 rc = SQLITE_FULL;
2881 goto abort_due_to_error;
2882 }
2883 }
2884 pC->recnoIsValid = 0;
2885 pC->deferredMoveto = 0;
2886 }
2887 pTos++;
2888 pTos->i = v;
2889 pTos->flags = MEM_Int;
2890 break;
2891}
2892
2893/* Opcode: PutIntKey P1 P2 *
2894**
2895** Write an entry into the table of cursor P1. A new entry is
2896** created if it doesn't already exist or the data for an existing
2897** entry is overwritten. The data is the value on the top of the
2898** stack. The key is the next value down on the stack. The key must
2899** be an integer. The stack is popped twice by this instruction.
2900**
2901** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
2902** incremented (otherwise not). If the OPFLAG_CSCHANGE flag is set,
2903** then the current statement change count is incremented (otherwise not).
2904** If the OPFLAG_LASTROWID flag of P2 is set, then rowid is
2905** stored for subsequent return by the sqlite_last_insert_rowid() function
2906** (otherwise it's unmodified).
2907*/
2908/* Opcode: PutStrKey P1 * *
2909**
2910** Write an entry into the table of cursor P1. A new entry is
2911** created if it doesn't already exist or the data for an existing
2912** entry is overwritten. The data is the value on the top of the
2913** stack. The key is the next value down on the stack. The key must
2914** be a string. The stack is popped twice by this instruction.
2915**
2916** P1 may not be a pseudo-table opened using the OpenPseudo opcode.
2917*/
2918case OP_PutIntKey:
2919case OP_PutStrKey: {
2920 Mem *pNos = &pTos[-1];
2921 int i = pOp->p1;
2922 Cursor *pC;
2923 assert( pNos>=p->aStack );
2924 assert( i>=0 && i<p->nCursor );
2925 if( ((pC = &p->aCsr[i])->pCursor!=0 || pC->pseudoTable) ){
2926 char *zKey;
2927 int nKey, iKey;
2928 if( pOp->opcode==OP_PutStrKey ){
2929 Stringify(pNos);
2930 nKey = pNos->n;
2931 zKey = pNos->z;
2932 }else{
2933 assert( pNos->flags & MEM_Int );
2934 nKey = sizeof(int);
2935 iKey = intToKey(pNos->i);
2936 zKey = (char*)&iKey;
2937 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
2938 if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->i;
2939 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
2940 if( pC->nextRowidValid && pTos->i>=pC->nextRowid ){
2941 pC->nextRowidValid = 0;
2942 }
2943 }
2944 if( pTos->flags & MEM_Null ){
2945 pTos->z = 0;
2946 pTos->n = 0;
2947 }else{
2948 assert( pTos->flags & MEM_Str );
2949 }
2950 if( pC->pseudoTable ){
2951 /* PutStrKey does not work for pseudo-tables.
2952 ** The following assert makes sure we are not trying to use
2953 ** PutStrKey on a pseudo-table
2954 */
2955 assert( pOp->opcode==OP_PutIntKey );
2956 sqliteFree(pC->pData);
2957 pC->iKey = iKey;
2958 pC->nData = pTos->n;
2959 if( pTos->flags & MEM_Dyn ){
2960 pC->pData = pTos->z;
2961 pTos->flags = MEM_Null;
2962 }else{
2963 pC->pData = sqliteMallocRaw( pC->nData );
2964 if( pC->pData ){
2965 memcpy(pC->pData, pTos->z, pC->nData);
2966 }
2967 }
2968 pC->nullRow = 0;
2969 }else{
2970 rc = sqliteBtreeInsert(pC->pCursor, zKey, nKey, pTos->z, pTos->n);
2971 }
2972 pC->recnoIsValid = 0;
2973 pC->deferredMoveto = 0;
2974 }
2975 popStack(&pTos, 2);
2976 break;
2977}
2978
2979/* Opcode: Delete P1 P2 *
2980**
2981** Delete the record at which the P1 cursor is currently pointing.
2982**
2983** The cursor will be left pointing at either the next or the previous
2984** record in the table. If it is left pointing at the next record, then
2985** the next Next instruction will be a no-op. Hence it is OK to delete
2986** a record from within an Next loop.
2987**
2988** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
2989** incremented (otherwise not). If OPFLAG_CSCHANGE flag is set,
2990** then the current statement change count is incremented (otherwise not).
2991**
2992** If P1 is a pseudo-table, then this instruction is a no-op.
2993*/
2994case OP_Delete: {
2995 int i = pOp->p1;
2996 Cursor *pC;
2997 assert( i>=0 && i<p->nCursor );
2998 pC = &p->aCsr[i];
2999 if( pC->pCursor!=0 ){
3000 sqliteVdbeCursorMoveto(pC);
3001 rc = sqliteBtreeDelete(pC->pCursor);
3002 pC->nextRowidValid = 0;
3003 }
3004 if( pOp->p2 & OPFLAG_NCHANGE ) db->nChange++;
3005 if( pOp->p2 & OPFLAG_CSCHANGE ) db->csChange++;
3006 break;
3007}
3008
3009/* Opcode: SetCounts * * *
3010**
3011** Called at end of statement. Updates lsChange (last statement change count)
3012** and resets csChange (current statement change count) to 0.
3013*/
3014case OP_SetCounts: {
3015 db->lsChange=db->csChange;
3016 db->csChange=0;
3017 break;
3018}
3019
3020/* Opcode: KeyAsData P1 P2 *
3021**
3022** Turn the key-as-data mode for cursor P1 either on (if P2==1) or
3023** off (if P2==0). In key-as-data mode, the OP_Column opcode pulls
3024** data off of the key rather than the data. This is used for
3025** processing compound selects.
3026*/
3027case OP_KeyAsData: {
3028 int i = pOp->p1;
3029 assert( i>=0 && i<p->nCursor );
3030 p->aCsr[i].keyAsData = pOp->p2;
3031 break;
3032}
3033
3034/* Opcode: RowData P1 * *
3035**
3036** Push onto the stack the complete row data for cursor P1.
3037** There is no interpretation of the data. It is just copied
3038** onto the stack exactly as it is found in the database file.
3039**
3040** If the cursor is not pointing to a valid row, a NULL is pushed
3041** onto the stack.
3042*/
3043/* Opcode: RowKey P1 * *
3044**
3045** Push onto the stack the complete row key for cursor P1.
3046** There is no interpretation of the key. It is just copied
3047** onto the stack exactly as it is found in the database file.
3048**
3049** If the cursor is not pointing to a valid row, a NULL is pushed
3050** onto the stack.
3051*/
3052case OP_RowKey:
3053case OP_RowData: {
3054 int i = pOp->p1;
3055 Cursor *pC;
3056 int n;
3057
3058 pTos++;
3059 assert( i>=0 && i<p->nCursor );
3060 pC = &p->aCsr[i];
3061 if( pC->nullRow ){
3062 pTos->flags = MEM_Null;
3063 }else if( pC->pCursor!=0 ){
3064 BtCursor *pCrsr = pC->pCursor;
3065 sqliteVdbeCursorMoveto(pC);
3066 if( pC->nullRow ){
3067 pTos->flags = MEM_Null;
3068 break;
3069 }else if( pC->keyAsData || pOp->opcode==OP_RowKey ){
3070 sqliteBtreeKeySize(pCrsr, &n);
3071 }else{
3072 sqliteBtreeDataSize(pCrsr, &n);
3073 }
3074 pTos->n = n;
3075 if( n<=NBFS ){
3076 pTos->flags = MEM_Str | MEM_Short;
3077 pTos->z = pTos->zShort;
3078 }else{
3079 char *z = sqliteMallocRaw( n );
3080 if( z==0 ) goto no_mem;
3081 pTos->flags = MEM_Str | MEM_Dyn;
3082 pTos->z = z;
3083 }
3084 if( pC->keyAsData || pOp->opcode==OP_RowKey ){
3085 sqliteBtreeKey(pCrsr, 0, n, pTos->z);
3086 }else{
3087 sqliteBtreeData(pCrsr, 0, n, pTos->z);
3088 }
3089 }else if( pC->pseudoTable ){
3090 pTos->n = pC->nData;
3091 pTos->z = pC->pData;
3092 pTos->flags = MEM_Str|MEM_Ephem;
3093 }else{
3094 pTos->flags = MEM_Null;
3095 }
3096 break;
3097}
3098
3099/* Opcode: Column P1 P2 *
3100**
3101** Interpret the data that cursor P1 points to as
3102** a structure built using the MakeRecord instruction.
3103** (See the MakeRecord opcode for additional information about
3104** the format of the data.)
3105** Push onto the stack the value of the P2-th column contained
3106** in the data.
3107**
3108** If the KeyAsData opcode has previously executed on this cursor,
3109** then the field might be extracted from the key rather than the
3110** data.
3111**
3112** If P1 is negative, then the record is stored on the stack rather
3113** than in a table. For P1==-1, the top of the stack is used.
3114** For P1==-2, the next on the stack is used. And so forth. The
3115** value pushed is always just a pointer into the record which is
3116** stored further down on the stack. The column value is not copied.
3117*/
3118case OP_Column: {
3119 int amt, offset, end, payloadSize;
3120 int i = pOp->p1;
3121 int p2 = pOp->p2;
3122 Cursor *pC;
3123 char *zRec;
3124 BtCursor *pCrsr;
3125 int idxWidth;
3126 unsigned char aHdr[10];
3127
3128 assert( i<p->nCursor );
3129 pTos++;
3130 if( i<0 ){
3131 assert( &pTos[i]>=p->aStack );
3132 assert( pTos[i].flags & MEM_Str );
3133 zRec = pTos[i].z;
3134 payloadSize = pTos[i].n;
3135 }else if( (pC = &p->aCsr[i])->pCursor!=0 ){
3136 sqliteVdbeCursorMoveto(pC);
3137 zRec = 0;
3138 pCrsr = pC->pCursor;
3139 if( pC->nullRow ){
3140 payloadSize = 0;
3141 }else if( pC->keyAsData ){
3142 sqliteBtreeKeySize(pCrsr, &payloadSize);
3143 }else{
3144 sqliteBtreeDataSize(pCrsr, &payloadSize);
3145 }
3146 }else if( pC->pseudoTable ){
3147 payloadSize = pC->nData;
3148 zRec = pC->pData;
3149 assert( payloadSize==0 || zRec!=0 );
3150 }else{
3151 payloadSize = 0;
3152 }
3153
3154 /* Figure out how many bytes in the column data and where the column
3155 ** data begins.
3156 */
3157 if( payloadSize==0 ){
3158 pTos->flags = MEM_Null;
3159 break;
3160 }else if( payloadSize<256 ){
3161 idxWidth = 1;
3162 }else if( payloadSize<65536 ){
3163 idxWidth = 2;
3164 }else{
3165 idxWidth = 3;
3166 }
3167
3168 /* Figure out where the requested column is stored and how big it is.
3169 */
3170 if( payloadSize < idxWidth*(p2+1) ){
3171 rc = SQLITE_CORRUPT;
3172 goto abort_due_to_error;
3173 }
3174 if( zRec ){
3175 memcpy(aHdr, &zRec[idxWidth*p2], idxWidth*2);
3176 }else if( pC->keyAsData ){
3177 sqliteBtreeKey(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
3178 }else{
3179 sqliteBtreeData(pCrsr, idxWidth*p2, idxWidth*2, (char*)aHdr);
3180 }
3181 offset = aHdr[0];
3182 end = aHdr[idxWidth];
3183 if( idxWidth>1 ){
3184 offset |= aHdr[1]<<8;
3185 end |= aHdr[idxWidth+1]<<8;
3186 if( idxWidth>2 ){
3187 offset |= aHdr[2]<<16;
3188 end |= aHdr[idxWidth+2]<<16;
3189 }
3190 }
3191 amt = end - offset;
3192 if( amt<0 || offset<0 || end>payloadSize ){
3193 rc = SQLITE_CORRUPT;
3194 goto abort_due_to_error;
3195 }
3196
3197 /* amt and offset now hold the offset to the start of data and the
3198 ** amount of data. Go get the data and put it on the stack.
3199 */
3200 pTos->n = amt;
3201 if( amt==0 ){
3202 pTos->flags = MEM_Null;
3203 }else if( zRec ){
3204 pTos->flags = MEM_Str | MEM_Ephem;
3205 pTos->z = &zRec[offset];
3206 }else{
3207 if( amt<=NBFS ){
3208 pTos->flags = MEM_Str | MEM_Short;
3209 pTos->z = pTos->zShort;
3210 }else{
3211 char *z = sqliteMallocRaw( amt );
3212 if( z==0 ) goto no_mem;
3213 pTos->flags = MEM_Str | MEM_Dyn;
3214 pTos->z = z;
3215 }
3216 if( pC->keyAsData ){
3217 sqliteBtreeKey(pCrsr, offset, amt, pTos->z);
3218 }else{
3219 sqliteBtreeData(pCrsr, offset, amt, pTos->z);
3220 }
3221 }
3222 break;
3223}
3224
3225/* Opcode: Recno P1 * *
3226**
3227** Push onto the stack an integer which is the first 4 bytes of the
3228** the key to the current entry in a sequential scan of the database
3229** file P1. The sequential scan should have been started using the
3230** Next opcode.
3231*/
3232case OP_Recno: {
3233 int i = pOp->p1;
3234 Cursor *pC;
3235 int v;
3236
3237 assert( i>=0 && i<p->nCursor );
3238 pC = &p->aCsr[i];
3239 sqliteVdbeCursorMoveto(pC);
3240 pTos++;
3241 if( pC->recnoIsValid ){
3242 v = pC->lastRecno;
3243 }else if( pC->pseudoTable ){
3244 v = keyToInt(pC->iKey);
3245 }else if( pC->nullRow || pC->pCursor==0 ){
3246 pTos->flags = MEM_Null;
3247 break;
3248 }else{
3249 assert( pC->pCursor!=0 );
3250 sqliteBtreeKey(pC->pCursor, 0, sizeof(u32), (char*)&v);
3251 v = keyToInt(v);
3252 }
3253 pTos->i = v;
3254 pTos->flags = MEM_Int;
3255 break;
3256}
3257
3258/* Opcode: FullKey P1 * *
3259**
3260** Extract the complete key from the record that cursor P1 is currently
3261** pointing to and push the key onto the stack as a string.
3262**
3263** Compare this opcode to Recno. The Recno opcode extracts the first
3264** 4 bytes of the key and pushes those bytes onto the stack as an
3265** integer. This instruction pushes the entire key as a string.
3266**
3267** This opcode may not be used on a pseudo-table.
3268*/
3269case OP_FullKey: {
3270 int i = pOp->p1;
3271 BtCursor *pCrsr;
3272
3273 assert( p->aCsr[i].keyAsData );
3274 assert( !p->aCsr[i].pseudoTable );
3275 assert( i>=0 && i<p->nCursor );
3276 pTos++;
3277 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
3278 int amt;
3279 char *z;
3280
3281 sqliteVdbeCursorMoveto(&p->aCsr[i]);
3282 sqliteBtreeKeySize(pCrsr, &amt);
3283 if( amt<=0 ){
3284 rc = SQLITE_CORRUPT;
3285 goto abort_due_to_error;
3286 }
3287 if( amt>NBFS ){
3288 z = sqliteMallocRaw( amt );
3289 if( z==0 ) goto no_mem;
3290 pTos->flags = MEM_Str | MEM_Dyn;
3291 }else{
3292 z = pTos->zShort;
3293 pTos->flags = MEM_Str | MEM_Short;
3294 }
3295 sqliteBtreeKey(pCrsr, 0, amt, z);
3296 pTos->z = z;
3297 pTos->n = amt;
3298 }
3299 break;
3300}
3301
3302/* Opcode: NullRow P1 * *
3303**
3304** Move the cursor P1 to a null row. Any OP_Column operations
3305** that occur while the cursor is on the null row will always push
3306** a NULL onto the stack.
3307*/
3308case OP_NullRow: {
3309 int i = pOp->p1;
3310
3311 assert( i>=0 && i<p->nCursor );
3312 p->aCsr[i].nullRow = 1;
3313 p->aCsr[i].recnoIsValid = 0;
3314 break;
3315}
3316
3317/* Opcode: Last P1 P2 *
3318**
3319** The next use of the Recno or Column or Next instruction for P1
3320** will refer to the last entry in the database table or index.
3321** If the table or index is empty and P2>0, then jump immediately to P2.
3322** If P2 is 0 or if the table or index is not empty, fall through
3323** to the following instruction.
3324*/
3325case OP_Last: {
3326 int i = pOp->p1;
3327 Cursor *pC;
3328 BtCursor *pCrsr;
3329
3330 assert( i>=0 && i<p->nCursor );
3331 pC = &p->aCsr[i];
3332 if( (pCrsr = pC->pCursor)!=0 ){
3333 int res;
3334 rc = sqliteBtreeLast(pCrsr, &res);
3335 pC->nullRow = res;
3336 pC->deferredMoveto = 0;
3337 if( res && pOp->p2>0 ){
3338 pc = pOp->p2 - 1;
3339 }
3340 }else{
3341 pC->nullRow = 0;
3342 }
3343 break;
3344}
3345
3346/* Opcode: Rewind P1 P2 *
3347**
3348** The next use of the Recno or Column or Next instruction for P1
3349** will refer to the first entry in the database table or index.
3350** If the table or index is empty and P2>0, then jump immediately to P2.
3351** If P2 is 0 or if the table or index is not empty, fall through
3352** to the following instruction.
3353*/
3354case OP_Rewind: {
3355 int i = pOp->p1;
3356 Cursor *pC;
3357 BtCursor *pCrsr;
3358
3359 assert( i>=0 && i<p->nCursor );
3360 pC = &p->aCsr[i];
3361 if( (pCrsr = pC->pCursor)!=0 ){
3362 int res;
3363 rc = sqliteBtreeFirst(pCrsr, &res);
3364 pC->atFirst = res==0;
3365 pC->nullRow = res;
3366 pC->deferredMoveto = 0;
3367 if( res && pOp->p2>0 ){
3368 pc = pOp->p2 - 1;
3369 }
3370 }else{
3371 pC->nullRow = 0;
3372 }
3373 break;
3374}
3375
3376/* Opcode: Next P1 P2 *
3377**
3378** Advance cursor P1 so that it points to the next key/data pair in its
3379** table or index. If there are no more key/value pairs then fall through
3380** to the following instruction. But if the cursor advance was successful,
3381** jump immediately to P2.
3382**
3383** See also: Prev
3384*/
3385/* Opcode: Prev P1 P2 *
3386**
3387** Back up cursor P1 so that it points to the previous key/data pair in its
3388** table or index. If there is no previous key/value pairs then fall through
3389** to the following instruction. But if the cursor backup was successful,
3390** jump immediately to P2.
3391*/
3392case OP_Prev:
3393case OP_Next: {
3394 Cursor *pC;
3395 BtCursor *pCrsr;
3396
3397 CHECK_FOR_INTERRUPT;
3398 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3399 pC = &p->aCsr[pOp->p1];
3400 if( (pCrsr = pC->pCursor)!=0 ){
3401 int res;
3402 if( pC->nullRow ){
3403 res = 1;
3404 }else{
3405 assert( pC->deferredMoveto==0 );
3406 rc = pOp->opcode==OP_Next ? sqliteBtreeNext(pCrsr, &res) :
3407 sqliteBtreePrevious(pCrsr, &res);
3408 pC->nullRow = res;
3409 }
3410 if( res==0 ){
3411 pc = pOp->p2 - 1;
3412 sqlite_search_count++;
3413 }
3414 }else{
3415 pC->nullRow = 1;
3416 }
3417 pC->recnoIsValid = 0;
3418 break;
3419}
3420
3421/* Opcode: IdxPut P1 P2 P3
3422**
3423** The top of the stack holds a SQL index key made using the
3424** MakeIdxKey instruction. This opcode writes that key into the
3425** index P1. Data for the entry is nil.
3426**
3427** If P2==1, then the key must be unique. If the key is not unique,
3428** the program aborts with a SQLITE_CONSTRAINT error and the database
3429** is rolled back. If P3 is not null, then it becomes part of the
3430** error message returned with the SQLITE_CONSTRAINT.
3431*/
3432case OP_IdxPut: {
3433 int i = pOp->p1;
3434 BtCursor *pCrsr;
3435 assert( pTos>=p->aStack );
3436 assert( i>=0 && i<p->nCursor );
3437 assert( pTos->flags & MEM_Str );
3438 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
3439 int nKey = pTos->n;
3440 const char *zKey = pTos->z;
3441 if( pOp->p2 ){
3442 int res, n;
3443 assert( nKey >= 4 );
3444 rc = sqliteBtreeMoveto(pCrsr, zKey, nKey-4, &res);
3445 if( rc!=SQLITE_OK ) goto abort_due_to_error;
3446 while( res!=0 ){
3447 int c;
3448 sqliteBtreeKeySize(pCrsr, &n);
3449 if( n==nKey
3450 && sqliteBtreeKeyCompare(pCrsr, zKey, nKey-4, 4, &c)==SQLITE_OK
3451 && c==0
3452 ){
3453 rc = SQLITE_CONSTRAINT;
3454 if( pOp->p3 && pOp->p3[0] ){
3455 sqliteSetString(&p->zErrMsg, pOp->p3, (char*)0);
3456 }
3457 goto abort_due_to_error;
3458 }
3459 if( res<0 ){
3460 sqliteBtreeNext(pCrsr, &res);
3461 res = +1;
3462 }else{
3463 break;
3464 }
3465 }
3466 }
3467 rc = sqliteBtreeInsert(pCrsr, zKey, nKey, "", 0);
3468 assert( p->aCsr[i].deferredMoveto==0 );
3469 }
3470 Release(pTos);
3471 pTos--;
3472 break;
3473}
3474
3475/* Opcode: IdxDelete P1 * *
3476**
3477** The top of the stack is an index key built using the MakeIdxKey opcode.
3478** This opcode removes that entry from the index.
3479*/
3480case OP_IdxDelete: {
3481 int i = pOp->p1;
3482 BtCursor *pCrsr;
3483 assert( pTos>=p->aStack );
3484 assert( pTos->flags & MEM_Str );
3485 assert( i>=0 && i<p->nCursor );
3486 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
3487 int rx, res;
3488 rx = sqliteBtreeMoveto(pCrsr, pTos->z, pTos->n, &res);
3489 if( rx==SQLITE_OK && res==0 ){
3490 rc = sqliteBtreeDelete(pCrsr);
3491 }
3492 assert( p->aCsr[i].deferredMoveto==0 );
3493 }
3494 Release(pTos);
3495 pTos--;
3496 break;
3497}
3498
3499/* Opcode: IdxRecno P1 * *
3500**
3501** Push onto the stack an integer which is the last 4 bytes of the
3502** the key to the current entry in index P1. These 4 bytes should
3503** be the record number of the table entry to which this index entry
3504** points.
3505**
3506** See also: Recno, MakeIdxKey.
3507*/
3508case OP_IdxRecno: {
3509 int i = pOp->p1;
3510 BtCursor *pCrsr;
3511
3512 assert( i>=0 && i<p->nCursor );
3513 pTos++;
3514 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
3515 int v;
3516 int sz;
3517 assert( p->aCsr[i].deferredMoveto==0 );
3518 sqliteBtreeKeySize(pCrsr, &sz);
3519 if( sz<sizeof(u32) ){
3520 pTos->flags = MEM_Null;
3521 }else{
3522 sqliteBtreeKey(pCrsr, sz - sizeof(u32), sizeof(u32), (char*)&v);
3523 v = keyToInt(v);
3524 pTos->i = v;
3525 pTos->flags = MEM_Int;
3526 }
3527 }else{
3528 pTos->flags = MEM_Null;
3529 }
3530 break;
3531}
3532
3533/* Opcode: IdxGT P1 P2 *
3534**
3535** Compare the top of the stack against the key on the index entry that
3536** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3537** index entry. If the index entry is greater than the top of the stack
3538** then jump to P2. Otherwise fall through to the next instruction.
3539** In either case, the stack is popped once.
3540*/
3541/* Opcode: IdxGE P1 P2 *
3542**
3543** Compare the top of the stack against the key on the index entry that
3544** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3545** index entry. If the index entry is greater than or equal to
3546** the top of the stack
3547** then jump to P2. Otherwise fall through to the next instruction.
3548** In either case, the stack is popped once.
3549*/
3550/* Opcode: IdxLT P1 P2 *
3551**
3552** Compare the top of the stack against the key on the index entry that
3553** cursor P1 is currently pointing to. Ignore the last 4 bytes of the
3554** index entry. If the index entry is less than the top of the stack
3555** then jump to P2. Otherwise fall through to the next instruction.
3556** In either case, the stack is popped once.
3557*/
3558case OP_IdxLT:
3559case OP_IdxGT:
3560case OP_IdxGE: {
3561 int i= pOp->p1;
3562 BtCursor *pCrsr;
3563
3564 assert( i>=0 && i<p->nCursor );
3565 assert( pTos>=p->aStack );
3566 if( (pCrsr = p->aCsr[i].pCursor)!=0 ){
3567 int res, rc;
3568
3569 Stringify(pTos);
3570 assert( p->aCsr[i].deferredMoveto==0 );
3571 rc = sqliteBtreeKeyCompare(pCrsr, pTos->z, pTos->n, 4, &res);
3572 if( rc!=SQLITE_OK ){
3573 break;
3574 }
3575 if( pOp->opcode==OP_IdxLT ){
3576 res = -res;
3577 }else if( pOp->opcode==OP_IdxGE ){
3578 res++;
3579 }
3580 if( res>0 ){
3581 pc = pOp->p2 - 1 ;
3582 }
3583 }
3584 Release(pTos);
3585 pTos--;
3586 break;
3587}
3588
3589/* Opcode: IdxIsNull P1 P2 *
3590**
3591** The top of the stack contains an index entry such as might be generated
3592** by the MakeIdxKey opcode. This routine looks at the first P1 fields of
3593** that key. If any of the first P1 fields are NULL, then a jump is made
3594** to address P2. Otherwise we fall straight through.
3595**
3596** The index entry is always popped from the stack.
3597*/
3598case OP_IdxIsNull: {
3599 int i = pOp->p1;
3600 int k, n;
3601 const char *z;
3602
3603 assert( pTos>=p->aStack );
3604 assert( pTos->flags & MEM_Str );
3605 z = pTos->z;
3606 n = pTos->n;
3607 for(k=0; k<n && i>0; i--){
3608 if( z[k]=='a' ){
3609 pc = pOp->p2-1;
3610 break;
3611 }
3612 while( k<n && z[k] ){ k++; }
3613 k++;
3614 }
3615 Release(pTos);
3616 pTos--;
3617 break;
3618}
3619
3620/* Opcode: Destroy P1 P2 *
3621**
3622** Delete an entire database table or index whose root page in the database
3623** file is given by P1.
3624**
3625** The table being destroyed is in the main database file if P2==0. If
3626** P2==1 then the table to be clear is in the auxiliary database file
3627** that is used to store tables create using CREATE TEMPORARY TABLE.
3628**
3629** See also: Clear
3630*/
3631case OP_Destroy: {
3632 rc = sqliteBtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1);
3633 break;
3634}
3635
3636/* Opcode: Clear P1 P2 *
3637**
3638** Delete all contents of the database table or index whose root page
3639** in the database file is given by P1. But, unlike Destroy, do not
3640** remove the table or index from the database file.
3641**
3642** The table being clear is in the main database file if P2==0. If
3643** P2==1 then the table to be clear is in the auxiliary database file
3644** that is used to store tables create using CREATE TEMPORARY TABLE.
3645**
3646** See also: Destroy
3647*/
3648case OP_Clear: {
3649 rc = sqliteBtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
3650 break;
3651}
3652
3653/* Opcode: CreateTable * P2 P3
3654**
3655** Allocate a new table in the main database file if P2==0 or in the
3656** auxiliary database file if P2==1. Push the page number
3657** for the root page of the new table onto the stack.
3658**
3659** The root page number is also written to a memory location that P3
3660** points to. This is the mechanism is used to write the root page
3661** number into the parser's internal data structures that describe the
3662** new table.
3663**
3664** The difference between a table and an index is this: A table must
3665** have a 4-byte integer key and can have arbitrary data. An index
3666** has an arbitrary key but no data.
3667**
3668** See also: CreateIndex
3669*/
3670/* Opcode: CreateIndex * P2 P3
3671**
3672** Allocate a new index in the main database file if P2==0 or in the
3673** auxiliary database file if P2==1. Push the page number of the
3674** root page of the new index onto the stack.
3675**
3676** See documentation on OP_CreateTable for additional information.
3677*/
3678case OP_CreateIndex:
3679case OP_CreateTable: {
3680 int pgno;
3681 assert( pOp->p3!=0 && pOp->p3type==P3_POINTER );
3682 assert( pOp->p2>=0 && pOp->p2<db->nDb );
3683 assert( db->aDb[pOp->p2].pBt!=0 );
3684 if( pOp->opcode==OP_CreateTable ){
3685 rc = sqliteBtreeCreateTable(db->aDb[pOp->p2].pBt, &pgno);
3686 }else{
3687 rc = sqliteBtreeCreateIndex(db->aDb[pOp->p2].pBt, &pgno);
3688 }
3689 pTos++;
3690 if( rc==SQLITE_OK ){
3691 pTos->i = pgno;
3692 pTos->flags = MEM_Int;
3693 *(u32*)pOp->p3 = pgno;
3694 pOp->p3 = 0;
3695 }else{
3696 pTos->flags = MEM_Null;
3697 }
3698 break;
3699}
3700
3701/* Opcode: IntegrityCk P1 P2 *
3702**
3703** Do an analysis of the currently open database. Push onto the
3704** stack the text of an error message describing any problems.
3705** If there are no errors, push a "ok" onto the stack.
3706**
3707** P1 is the index of a set that contains the root page numbers
3708** for all tables and indices in the main database file. The set
3709** is cleared by this opcode. In other words, after this opcode
3710** has executed, the set will be empty.
3711**
3712** If P2 is not zero, the check is done on the auxiliary database
3713** file, not the main database file.
3714**
3715** This opcode is used for testing purposes only.
3716*/
3717case OP_IntegrityCk: {
3718 int nRoot;
3719 int *aRoot;
3720 int iSet = pOp->p1;
3721 Set *pSet;
3722 int j;
3723 HashElem *i;
3724 char *z;
3725
3726 assert( iSet>=0 && iSet<p->nSet );
3727 pTos++;
3728 pSet = &p->aSet[iSet];
3729 nRoot = sqliteHashCount(&pSet->hash);
3730 aRoot = sqliteMallocRaw( sizeof(int)*(nRoot+1) );
3731 if( aRoot==0 ) goto no_mem;
3732 for(j=0, i=sqliteHashFirst(&pSet->hash); i; i=sqliteHashNext(i), j++){
3733 toInt((char*)sqliteHashKey(i), &aRoot[j]);
3734 }
3735 aRoot[j] = 0;
3736 sqliteHashClear(&pSet->hash);
3737 pSet->prev = 0;
3738 z = sqliteBtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot);
3739 if( z==0 || z[0]==0 ){
3740 if( z ) sqliteFree(z);
3741 pTos->z = "ok";
3742 pTos->n = 3;
3743 pTos->flags = MEM_Str | MEM_Static;
3744 }else{
3745 pTos->z = z;
3746 pTos->n = strlen(z) + 1;
3747 pTos->flags = MEM_Str | MEM_Dyn;
3748 }
3749 sqliteFree(aRoot);
3750 break;
3751}
3752
3753/* Opcode: ListWrite * * *
3754**
3755** Write the integer on the top of the stack
3756** into the temporary storage list.
3757*/
3758case OP_ListWrite: {
3759 Keylist *pKeylist;
3760 assert( pTos>=p->aStack );
3761 pKeylist = p->pList;
3762 if( pKeylist==0 || pKeylist->nUsed>=pKeylist->nKey ){
3763 pKeylist = sqliteMallocRaw( sizeof(Keylist)+999*sizeof(pKeylist->aKey[0]) );
3764 if( pKeylist==0 ) goto no_mem;
3765 pKeylist->nKey = 1000;
3766 pKeylist->nRead = 0;
3767 pKeylist->nUsed = 0;
3768 pKeylist->pNext = p->pList;
3769 p->pList = pKeylist;
3770 }
3771 Integerify(pTos);
3772 pKeylist->aKey[pKeylist->nUsed++] = pTos->i;
3773 Release(pTos);
3774 pTos--;
3775 break;
3776}
3777
3778/* Opcode: ListRewind * * *
3779**
3780** Rewind the temporary buffer back to the beginning.
3781*/
3782case OP_ListRewind: {
3783 /* What this opcode codes, really, is reverse the order of the
3784 ** linked list of Keylist structures so that they are read out
3785 ** in the same order that they were read in. */
3786 Keylist *pRev, *pTop;
3787 pRev = 0;
3788 while( p->pList ){
3789 pTop = p->pList;
3790 p->pList = pTop->pNext;
3791 pTop->pNext = pRev;
3792 pRev = pTop;
3793 }
3794 p->pList = pRev;
3795 break;
3796}
3797
3798/* Opcode: ListRead * P2 *
3799**
3800** Attempt to read an integer from the temporary storage buffer
3801** and push it onto the stack. If the storage buffer is empty,
3802** push nothing but instead jump to P2.
3803*/
3804case OP_ListRead: {
3805 Keylist *pKeylist;
3806 CHECK_FOR_INTERRUPT;
3807 pKeylist = p->pList;
3808 if( pKeylist!=0 ){
3809 assert( pKeylist->nRead>=0 );
3810 assert( pKeylist->nRead<pKeylist->nUsed );
3811 assert( pKeylist->nRead<pKeylist->nKey );
3812 pTos++;
3813 pTos->i = pKeylist->aKey[pKeylist->nRead++];
3814 pTos->flags = MEM_Int;
3815 if( pKeylist->nRead>=pKeylist->nUsed ){
3816 p->pList = pKeylist->pNext;
3817 sqliteFree(pKeylist);
3818 }
3819 }else{
3820 pc = pOp->p2 - 1;
3821 }
3822 break;
3823}
3824
3825/* Opcode: ListReset * * *
3826**
3827** Reset the temporary storage buffer so that it holds nothing.
3828*/
3829case OP_ListReset: {
3830 if( p->pList ){
3831 sqliteVdbeKeylistFree(p->pList);
3832 p->pList = 0;
3833 }
3834 break;
3835}
3836
3837/* Opcode: ListPush * * *
3838**
3839** Save the current Vdbe list such that it can be restored by a ListPop
3840** opcode. The list is empty after this is executed.
3841*/
3842case OP_ListPush: {
3843 p->keylistStackDepth++;
3844 assert(p->keylistStackDepth > 0);
3845 p->keylistStack = sqliteRealloc(p->keylistStack,
3846 sizeof(Keylist *) * p->keylistStackDepth);
3847 if( p->keylistStack==0 ) goto no_mem;
3848 p->keylistStack[p->keylistStackDepth - 1] = p->pList;
3849 p->pList = 0;
3850 break;
3851}
3852
3853/* Opcode: ListPop * * *
3854**
3855** Restore the Vdbe list to the state it was in when ListPush was last
3856** executed.
3857*/
3858case OP_ListPop: {
3859 assert(p->keylistStackDepth > 0);
3860 p->keylistStackDepth--;
3861 sqliteVdbeKeylistFree(p->pList);
3862 p->pList = p->keylistStack[p->keylistStackDepth];
3863 p->keylistStack[p->keylistStackDepth] = 0;
3864 if( p->keylistStackDepth == 0 ){
3865 sqliteFree(p->keylistStack);
3866 p->keylistStack = 0;
3867 }
3868 break;
3869}
3870
3871/* Opcode: ContextPush * * *
3872**
3873** Save the current Vdbe context such that it can be restored by a ContextPop
3874** opcode. The context stores the last insert row id, the last statement change
3875** count, and the current statement change count.
3876*/
3877case OP_ContextPush: {
3878 p->contextStackDepth++;
3879 assert(p->contextStackDepth > 0);
3880 p->contextStack = sqliteRealloc(p->contextStack,
3881 sizeof(Context) * p->contextStackDepth);
3882 if( p->contextStack==0 ) goto no_mem;
3883 p->contextStack[p->contextStackDepth - 1].lastRowid = p->db->lastRowid;
3884 p->contextStack[p->contextStackDepth - 1].lsChange = p->db->lsChange;
3885 p->contextStack[p->contextStackDepth - 1].csChange = p->db->csChange;
3886 break;
3887}
3888
3889/* Opcode: ContextPop * * *
3890**
3891** Restore the Vdbe context to the state it was in when contextPush was last
3892** executed. The context stores the last insert row id, the last statement
3893** change count, and the current statement change count.
3894*/
3895case OP_ContextPop: {
3896 assert(p->contextStackDepth > 0);
3897 p->contextStackDepth--;
3898 p->db->lastRowid = p->contextStack[p->contextStackDepth].lastRowid;
3899 p->db->lsChange = p->contextStack[p->contextStackDepth].lsChange;
3900 p->db->csChange = p->contextStack[p->contextStackDepth].csChange;
3901 if( p->contextStackDepth == 0 ){
3902 sqliteFree(p->contextStack);
3903 p->contextStack = 0;
3904 }
3905 break;
3906}
3907
3908/* Opcode: SortPut * * *
3909**
3910** The TOS is the key and the NOS is the data. Pop both from the stack
3911** and put them on the sorter. The key and data should have been
3912** made using SortMakeKey and SortMakeRec, respectively.
3913*/
3914case OP_SortPut: {
3915 Mem *pNos = &pTos[-1];
3916 Sorter *pSorter;
3917 assert( pNos>=p->aStack );
3918 if( Dynamicify(pTos) || Dynamicify(pNos) ) goto no_mem;
3919 pSorter = sqliteMallocRaw( sizeof(Sorter) );
3920 if( pSorter==0 ) goto no_mem;
3921 pSorter->pNext = p->pSort;
3922 p->pSort = pSorter;
3923 assert( pTos->flags & MEM_Dyn );
3924 pSorter->nKey = pTos->n;
3925 pSorter->zKey = pTos->z;
3926 assert( pNos->flags & MEM_Dyn );
3927 pSorter->nData = pNos->n;
3928 pSorter->pData = pNos->z;
3929 pTos -= 2;
3930 break;
3931}
3932
3933/* Opcode: SortMakeRec P1 * *
3934**
3935** The top P1 elements are the arguments to a callback. Form these
3936** elements into a single data entry that can be stored on a sorter
3937** using SortPut and later fed to a callback using SortCallback.
3938*/
3939case OP_SortMakeRec: {
3940 char *z;
3941 char **azArg;
3942 int nByte;
3943 int nField;
3944 int i;
3945 Mem *pRec;
3946
3947 nField = pOp->p1;
3948 pRec = &pTos[1-nField];
3949 assert( pRec>=p->aStack );
3950 nByte = 0;
3951 for(i=0; i<nField; i++, pRec++){
3952 if( (pRec->flags & MEM_Null)==0 ){
3953 Stringify(pRec);
3954 nByte += pRec->n;
3955 }
3956 }
3957 nByte += sizeof(char*)*(nField+1);
3958 azArg = sqliteMallocRaw( nByte );
3959 if( azArg==0 ) goto no_mem;
3960 z = (char*)&azArg[nField+1];
3961 for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
3962 if( pRec->flags & MEM_Null ){
3963 azArg[i] = 0;
3964 }else{
3965 azArg[i] = z;
3966 memcpy(z, pRec->z, pRec->n);
3967 z += pRec->n;
3968 }
3969 }
3970 popStack(&pTos, nField);
3971 pTos++;
3972 pTos->n = nByte;
3973 pTos->z = (char*)azArg;
3974 pTos->flags = MEM_Str | MEM_Dyn;
3975 break;
3976}
3977
3978/* Opcode: SortMakeKey * * P3
3979**
3980** Convert the top few entries of the stack into a sort key. The
3981** number of stack entries consumed is the number of characters in
3982** the string P3. One character from P3 is prepended to each entry.
3983** The first character of P3 is prepended to the element lowest in
3984** the stack and the last character of P3 is prepended to the top of
3985** the stack. All stack entries are separated by a \000 character
3986** in the result. The whole key is terminated by two \000 characters
3987** in a row.
3988**
3989** "N" is substituted in place of the P3 character for NULL values.
3990**
3991** See also the MakeKey and MakeIdxKey opcodes.
3992*/
3993case OP_SortMakeKey: {
3994 char *zNewKey;
3995 int nByte;
3996 int nField;
3997 int i, j, k;
3998 Mem *pRec;
3999
4000 nField = strlen(pOp->p3);
4001 pRec = &pTos[1-nField];
4002 nByte = 1;
4003 for(i=0; i<nField; i++, pRec++){
4004 if( pRec->flags & MEM_Null ){
4005 nByte += 2;
4006 }else{
4007 Stringify(pRec);
4008 nByte += pRec->n+2;
4009 }
4010 }
4011 zNewKey = sqliteMallocRaw( nByte );
4012 if( zNewKey==0 ) goto no_mem;
4013 j = 0;
4014 k = 0;
4015 for(pRec=&pTos[1-nField], i=0; i<nField; i++, pRec++){
4016 if( pRec->flags & MEM_Null ){
4017 zNewKey[j++] = 'N';
4018 zNewKey[j++] = 0;
4019 k++;
4020 }else{
4021 zNewKey[j++] = pOp->p3[k++];
4022 memcpy(&zNewKey[j], pRec->z, pRec->n-1);
4023 j += pRec->n-1;
4024 zNewKey[j++] = 0;
4025 }
4026 }
4027 zNewKey[j] = 0;
4028 assert( j<nByte );
4029 popStack(&pTos, nField);
4030 pTos++;
4031 pTos->n = nByte;
4032 pTos->flags = MEM_Str|MEM_Dyn;
4033 pTos->z = zNewKey;
4034 break;
4035}
4036
4037/* Opcode: Sort * * *
4038**
4039** Sort all elements on the sorter. The algorithm is a
4040** mergesort.
4041*/
4042case OP_Sort: {
4043 int i;
4044 Sorter *pElem;
4045 Sorter *apSorter[NSORT];
4046 for(i=0; i<NSORT; i++){
4047 apSorter[i] = 0;
4048 }
4049 while( p->pSort ){
4050 pElem = p->pSort;
4051 p->pSort = pElem->pNext;
4052 pElem->pNext = 0;
4053 for(i=0; i<NSORT-1; i++){
4054 if( apSorter[i]==0 ){
4055 apSorter[i] = pElem;
4056 break;
4057 }else{
4058 pElem = Merge(apSorter[i], pElem);
4059 apSorter[i] = 0;
4060 }
4061 }
4062 if( i>=NSORT-1 ){
4063 apSorter[NSORT-1] = Merge(apSorter[NSORT-1],pElem);
4064 }
4065 }
4066 pElem = 0;
4067 for(i=0; i<NSORT; i++){
4068 pElem = Merge(apSorter[i], pElem);
4069 }
4070 p->pSort = pElem;
4071 break;
4072}
4073
4074/* Opcode: SortNext * P2 *
4075**
4076** Push the data for the topmost element in the sorter onto the
4077** stack, then remove the element from the sorter. If the sorter
4078** is empty, push nothing on the stack and instead jump immediately
4079** to instruction P2.
4080*/
4081case OP_SortNext: {
4082 Sorter *pSorter = p->pSort;
4083 CHECK_FOR_INTERRUPT;
4084 if( pSorter!=0 ){
4085 p->pSort = pSorter->pNext;
4086 pTos++;
4087 pTos->z = pSorter->pData;
4088 pTos->n = pSorter->nData;
4089 pTos->flags = MEM_Str|MEM_Dyn;
4090 sqliteFree(pSorter->zKey);
4091 sqliteFree(pSorter);
4092 }else{
4093 pc = pOp->p2 - 1;
4094 }
4095 break;
4096}
4097
4098/* Opcode: SortCallback P1 * *
4099**
4100** The top of the stack contains a callback record built using
4101** the SortMakeRec operation with the same P1 value as this
4102** instruction. Pop this record from the stack and invoke the
4103** callback on it.
4104*/
4105case OP_SortCallback: {
4106 assert( pTos>=p->aStack );
4107 assert( pTos->flags & MEM_Str );
4108 p->nCallback++;
4109 p->pc = pc+1;
4110 p->azResColumn = (char**)pTos->z;
4111 assert( p->nResColumn==pOp->p1 );
4112 p->popStack = 1;
4113 p->pTos = pTos;
4114 return SQLITE_ROW;
4115}
4116
4117/* Opcode: SortReset * * *
4118**
4119** Remove any elements that remain on the sorter.
4120*/
4121case OP_SortReset: {
4122 sqliteVdbeSorterReset(p);
4123 break;
4124}
4125
4126/* Opcode: FileOpen * * P3
4127**
4128** Open the file named by P3 for reading using the FileRead opcode.
4129** If P3 is "stdin" then open standard input for reading.
4130*/
4131case OP_FileOpen: {
4132 assert( pOp->p3!=0 );
4133 if( p->pFile ){
4134 if( p->pFile!=stdin ) fclose(p->pFile);
4135 p->pFile = 0;
4136 }
4137 if( sqliteStrICmp(pOp->p3,"stdin")==0 ){
4138 p->pFile = stdin;
4139 }else{
4140 p->pFile = fopen(pOp->p3, "r");
4141 }
4142 if( p->pFile==0 ){
4143 sqliteSetString(&p->zErrMsg,"unable to open file: ", pOp->p3, (char*)0);
4144 rc = SQLITE_ERROR;
4145 }
4146 break;
4147}
4148
4149/* Opcode: FileRead P1 P2 P3
4150**
4151** Read a single line of input from the open file (the file opened using
4152** FileOpen). If we reach end-of-file, jump immediately to P2. If
4153** we are able to get another line, split the line apart using P3 as
4154** a delimiter. There should be P1 fields. If the input line contains
4155** more than P1 fields, ignore the excess. If the input line contains
4156** fewer than P1 fields, assume the remaining fields contain NULLs.
4157**
4158** Input ends if a line consists of just "\.". A field containing only
4159** "\N" is a null field. The backslash \ character can be used be used
4160** to escape newlines or the delimiter.
4161*/
4162case OP_FileRead: {
4163 int n, eol, nField, i, c, nDelim;
4164 char *zDelim, *z;
4165 CHECK_FOR_INTERRUPT;
4166 if( p->pFile==0 ) goto fileread_jump;
4167 nField = pOp->p1;
4168 if( nField<=0 ) goto fileread_jump;
4169 if( nField!=p->nField || p->azField==0 ){
4170 char **azField = sqliteRealloc(p->azField, sizeof(char*)*nField+1);
4171 if( azField==0 ){ goto no_mem; }
4172 p->azField = azField;
4173 p->nField = nField;
4174 }
4175 n = 0;
4176 eol = 0;
4177 while( eol==0 ){
4178 if( p->zLine==0 || n+200>p->nLineAlloc ){
4179 char *zLine;
4180 p->nLineAlloc = p->nLineAlloc*2 + 300;
4181 zLine = sqliteRealloc(p->zLine, p->nLineAlloc);
4182 if( zLine==0 ){
4183 p->nLineAlloc = 0;
4184 sqliteFree(p->zLine);
4185 p->zLine = 0;
4186 goto no_mem;
4187 }
4188 p->zLine = zLine;
4189 }
4190 if( vdbe_fgets(&p->zLine[n], p->nLineAlloc-n, p->pFile)==0 ){
4191 eol = 1;
4192 p->zLine[n] = 0;
4193 }else{
4194 int c;
4195 while( (c = p->zLine[n])!=0 ){
4196 if( c=='\\' ){
4197 if( p->zLine[n+1]==0 ) break;
4198 n += 2;
4199 }else if( c=='\n' ){
4200 p->zLine[n] = 0;
4201 eol = 1;
4202 break;
4203 }else{
4204 n++;
4205 }
4206 }
4207 }
4208 }
4209 if( n==0 ) goto fileread_jump;
4210 z = p->zLine;
4211 if( z[0]=='\\' && z[1]=='.' && z[2]==0 ){
4212 goto fileread_jump;
4213 }
4214 zDelim = pOp->p3;
4215 if( zDelim==0 ) zDelim = "\t";
4216 c = zDelim[0];
4217 nDelim = strlen(zDelim);
4218 p->azField[0] = z;
4219 for(i=1; *z!=0 && i<=nField; i++){
4220 int from, to;
4221 from = to = 0;
4222 if( z[0]=='\\' && z[1]=='N'
4223 && (z[2]==0 || strncmp(&z[2],zDelim,nDelim)==0) ){
4224 if( i<=nField ) p->azField[i-1] = 0;
4225 z += 2 + nDelim;
4226 if( i<nField ) p->azField[i] = z;
4227 continue;
4228 }
4229 while( z[from] ){
4230 if( z[from]=='\\' && z[from+1]!=0 ){
4231 int tx = z[from+1];
4232 switch( tx ){
4233 case 'b': tx = '\b'; break;
4234 case 'f': tx = '\f'; break;
4235 case 'n': tx = '\n'; break;
4236 case 'r': tx = '\r'; break;
4237 case 't': tx = '\t'; break;
4238 case 'v': tx = '\v'; break;
4239 default: break;
4240 }
4241 z[to++] = tx;
4242 from += 2;
4243 continue;
4244 }
4245 if( z[from]==c && strncmp(&z[from],zDelim,nDelim)==0 ) break;
4246 z[to++] = z[from++];
4247 }
4248 if( z[from] ){
4249 z[to] = 0;
4250 z += from + nDelim;
4251 if( i<nField ) p->azField[i] = z;
4252 }else{
4253 z[to] = 0;
4254 z = "";
4255 }
4256 }
4257 while( i<nField ){
4258 p->azField[i++] = 0;
4259 }
4260 break;
4261
4262 /* If we reach end-of-file, or if anything goes wrong, jump here.
4263 ** This code will cause a jump to P2 */
4264fileread_jump:
4265 pc = pOp->p2 - 1;
4266 break;
4267}
4268
4269/* Opcode: FileColumn P1 * *
4270**
4271** Push onto the stack the P1-th column of the most recently read line
4272** from the input file.
4273*/
4274case OP_FileColumn: {
4275 int i = pOp->p1;
4276 char *z;
4277 assert( i>=0 && i<p->nField );
4278 if( p->azField ){
4279 z = p->azField[i];
4280 }else{
4281 z = 0;
4282 }
4283 pTos++;
4284 if( z ){
4285 pTos->n = strlen(z) + 1;
4286 pTos->z = z;
4287 pTos->flags = MEM_Str | MEM_Ephem;
4288 }else{
4289 pTos->flags = MEM_Null;
4290 }
4291 break;
4292}
4293
4294/* Opcode: MemStore P1 P2 *
4295**
4296** Write the top of the stack into memory location P1.
4297** P1 should be a small integer since space is allocated
4298** for all memory locations between 0 and P1 inclusive.
4299**
4300** After the data is stored in the memory location, the
4301** stack is popped once if P2 is 1. If P2 is zero, then
4302** the original data remains on the stack.
4303*/
4304case OP_MemStore: {
4305 int i = pOp->p1;
4306 Mem *pMem;
4307 assert( pTos>=p->aStack );
4308 if( i>=p->nMem ){
4309 int nOld = p->nMem;
4310 Mem *aMem;
4311 p->nMem = i + 5;
4312 aMem = sqliteRealloc(p->aMem, p->nMem*sizeof(p->aMem[0]));
4313 if( aMem==0 ) goto no_mem;
4314 if( aMem!=p->aMem ){
4315 int j;
4316 for(j=0; j<nOld; j++){
4317 if( aMem[j].flags & MEM_Short ){
4318 aMem[j].z = aMem[j].zShort;
4319 }
4320 }
4321 }
4322 p->aMem = aMem;
4323 if( nOld<p->nMem ){
4324 memset(&p->aMem[nOld], 0, sizeof(p->aMem[0])*(p->nMem-nOld));
4325 }
4326 }
4327 Deephemeralize(pTos);
4328 pMem = &p->aMem[i];
4329 Release(pMem);
4330 *pMem = *pTos;
4331 if( pMem->flags & MEM_Dyn ){
4332 if( pOp->p2 ){
4333 pTos->flags = MEM_Null;
4334 }else{
4335 pMem->z = sqliteMallocRaw( pMem->n );
4336 if( pMem->z==0 ) goto no_mem;
4337 memcpy(pMem->z, pTos->z, pMem->n);
4338 }
4339 }else if( pMem->flags & MEM_Short ){
4340 pMem->z = pMem->zShort;
4341 }
4342 if( pOp->p2 ){
4343 Release(pTos);
4344 pTos--;
4345 }
4346 break;
4347}
4348
4349/* Opcode: MemLoad P1 * *
4350**
4351** Push a copy of the value in memory location P1 onto the stack.
4352**
4353** If the value is a string, then the value pushed is a pointer to
4354** the string that is stored in the memory location. If the memory
4355** location is subsequently changed (using OP_MemStore) then the
4356** value pushed onto the stack will change too.
4357*/
4358case OP_MemLoad: {
4359 int i = pOp->p1;
4360 assert( i>=0 && i<p->nMem );
4361 pTos++;
4362 memcpy(pTos, &p->aMem[i], sizeof(pTos[0])-NBFS);;
4363 if( pTos->flags & MEM_Str ){
4364 pTos->flags |= MEM_Ephem;
4365 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4366 }
4367 break;
4368}
4369
4370/* Opcode: MemIncr P1 P2 *
4371**
4372** Increment the integer valued memory cell P1 by 1. If P2 is not zero
4373** and the result after the increment is greater than zero, then jump
4374** to P2.
4375**
4376** This instruction throws an error if the memory cell is not initially
4377** an integer.
4378*/
4379case OP_MemIncr: {
4380 int i = pOp->p1;
4381 Mem *pMem;
4382 assert( i>=0 && i<p->nMem );
4383 pMem = &p->aMem[i];
4384 assert( pMem->flags==MEM_Int );
4385 pMem->i++;
4386 if( pOp->p2>0 && pMem->i>0 ){
4387 pc = pOp->p2 - 1;
4388 }
4389 break;
4390}
4391
4392/* Opcode: AggReset * P2 *
4393**
4394** Reset the aggregator so that it no longer contains any data.
4395** Future aggregator elements will contain P2 values each.
4396*/
4397case OP_AggReset: {
4398 sqliteVdbeAggReset(&p->agg);
4399 p->agg.nMem = pOp->p2;
4400 p->agg.apFunc = sqliteMalloc( p->agg.nMem*sizeof(p->agg.apFunc[0]) );
4401 if( p->agg.apFunc==0 ) goto no_mem;
4402 break;
4403}
4404
4405/* Opcode: AggInit * P2 P3
4406**
4407** Initialize the function parameters for an aggregate function.
4408** The aggregate will operate out of aggregate column P2.
4409** P3 is a pointer to the FuncDef structure for the function.
4410*/
4411case OP_AggInit: {
4412 int i = pOp->p2;
4413 assert( i>=0 && i<p->agg.nMem );
4414 p->agg.apFunc[i] = (FuncDef*)pOp->p3;
4415 break;
4416}
4417
4418/* Opcode: AggFunc * P2 P3
4419**
4420** Execute the step function for an aggregate. The
4421** function has P2 arguments. P3 is a pointer to the FuncDef
4422** structure that specifies the function.
4423**
4424** The top of the stack must be an integer which is the index of
4425** the aggregate column that corresponds to this aggregate function.
4426** Ideally, this index would be another parameter, but there are
4427** no free parameters left. The integer is popped from the stack.
4428*/
4429case OP_AggFunc: {
4430 int n = pOp->p2;
4431 int i;
4432 Mem *pMem, *pRec;
4433 char **azArgv = p->zArgv;
4434 sqlite_func ctx;
4435
4436 assert( n>=0 );
4437 assert( pTos->flags==MEM_Int );
4438 pRec = &pTos[-n];
4439 assert( pRec>=p->aStack );
4440 for(i=0; i<n; i++, pRec++){
4441 if( pRec->flags & MEM_Null ){
4442 azArgv[i] = 0;
4443 }else{
4444 Stringify(pRec);
4445 azArgv[i] = pRec->z;
4446 }
4447 }
4448 i = pTos->i;
4449 assert( i>=0 && i<p->agg.nMem );
4450 ctx.pFunc = (FuncDef*)pOp->p3;
4451 pMem = &p->agg.pCurrent->aMem[i];
4452 ctx.s.z = pMem->zShort; /* Space used for small aggregate contexts */
4453 ctx.pAgg = pMem->z;
4454 ctx.cnt = ++pMem->i;
4455 ctx.isError = 0;
4456 ctx.isStep = 1;
4457 (ctx.pFunc->xStep)(&ctx, n, (const char**)azArgv);
4458 pMem->z = ctx.pAgg;
4459 pMem->flags = MEM_AggCtx;
4460 popStack(&pTos, n+1);
4461 if( ctx.isError ){
4462 rc = SQLITE_ERROR;
4463 }
4464 break;
4465}
4466
4467/* Opcode: AggFocus * P2 *
4468**
4469** Pop the top of the stack and use that as an aggregator key. If
4470** an aggregator with that same key already exists, then make the
4471** aggregator the current aggregator and jump to P2. If no aggregator
4472** with the given key exists, create one and make it current but
4473** do not jump.
4474**
4475** The order of aggregator opcodes is important. The order is:
4476** AggReset AggFocus AggNext. In other words, you must execute
4477** AggReset first, then zero or more AggFocus operations, then
4478** zero or more AggNext operations. You must not execute an AggFocus
4479** in between an AggNext and an AggReset.
4480*/
4481case OP_AggFocus: {
4482 AggElem *pElem;
4483 char *zKey;
4484 int nKey;
4485
4486 assert( pTos>=p->aStack );
4487 Stringify(pTos);
4488 zKey = pTos->z;
4489 nKey = pTos->n;
4490 pElem = sqliteHashFind(&p->agg.hash, zKey, nKey);
4491 if( pElem ){
4492 p->agg.pCurrent = pElem;
4493 pc = pOp->p2 - 1;
4494 }else{
4495 AggInsert(&p->agg, zKey, nKey);
4496 if( sqlite_malloc_failed ) goto no_mem;
4497 }
4498 Release(pTos);
4499 pTos--;
4500 break;
4501}
4502
4503/* Opcode: AggSet * P2 *
4504**
4505** Move the top of the stack into the P2-th field of the current
4506** aggregate. String values are duplicated into new memory.
4507*/
4508case OP_AggSet: {
4509 AggElem *pFocus = AggInFocus(p->agg);
4510 Mem *pMem;
4511 int i = pOp->p2;
4512 assert( pTos>=p->aStack );
4513 if( pFocus==0 ) goto no_mem;
4514 assert( i>=0 && i<p->agg.nMem );
4515 Deephemeralize(pTos);
4516 pMem = &pFocus->aMem[i];
4517 Release(pMem);
4518 *pMem = *pTos;
4519 if( pMem->flags & MEM_Dyn ){
4520 pTos->flags = MEM_Null;
4521 }else if( pMem->flags & MEM_Short ){
4522 pMem->z = pMem->zShort;
4523 }
4524 Release(pTos);
4525 pTos--;
4526 break;
4527}
4528
4529/* Opcode: AggGet * P2 *
4530**
4531** Push a new entry onto the stack which is a copy of the P2-th field
4532** of the current aggregate. Strings are not duplicated so
4533** string values will be ephemeral.
4534*/
4535case OP_AggGet: {
4536 AggElem *pFocus = AggInFocus(p->agg);
4537 Mem *pMem;
4538 int i = pOp->p2;
4539 if( pFocus==0 ) goto no_mem;
4540 assert( i>=0 && i<p->agg.nMem );
4541 pTos++;
4542 pMem = &pFocus->aMem[i];
4543 *pTos = *pMem;
4544 if( pTos->flags & MEM_Str ){
4545 pTos->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short);
4546 pTos->flags |= MEM_Ephem;
4547 }
4548 if( pTos->flags & MEM_AggCtx ){
4549 Release(pTos);
4550 pTos->flags = MEM_Null;
4551 }
4552 break;
4553}
4554
4555/* Opcode: AggNext * P2 *
4556**
4557** Make the next aggregate value the current aggregate. The prior
4558** aggregate is deleted. If all aggregate values have been consumed,
4559** jump to P2.
4560**
4561** The order of aggregator opcodes is important. The order is:
4562** AggReset AggFocus AggNext. In other words, you must execute
4563** AggReset first, then zero or more AggFocus operations, then
4564** zero or more AggNext operations. You must not execute an AggFocus
4565** in between an AggNext and an AggReset.
4566*/
4567case OP_AggNext: {
4568 CHECK_FOR_INTERRUPT;
4569 if( p->agg.pSearch==0 ){
4570 p->agg.pSearch = sqliteHashFirst(&p->agg.hash);
4571 }else{
4572 p->agg.pSearch = sqliteHashNext(p->agg.pSearch);
4573 }
4574 if( p->agg.pSearch==0 ){
4575 pc = pOp->p2 - 1;
4576 } else {
4577 int i;
4578 sqlite_func ctx;
4579 Mem *aMem;
4580 p->agg.pCurrent = sqliteHashData(p->agg.pSearch);
4581 aMem = p->agg.pCurrent->aMem;
4582 for(i=0; i<p->agg.nMem; i++){
4583 int freeCtx;
4584 if( p->agg.apFunc[i]==0 ) continue;
4585 if( p->agg.apFunc[i]->xFinalize==0 ) continue;
4586 ctx.s.flags = MEM_Null;
4587 ctx.s.z = aMem[i].zShort;
4588 ctx.pAgg = (void*)aMem[i].z;
4589 freeCtx = aMem[i].z && aMem[i].z!=aMem[i].zShort;
4590 ctx.cnt = aMem[i].i;
4591 ctx.isStep = 0;
4592 ctx.pFunc = p->agg.apFunc[i];
4593 (*p->agg.apFunc[i]->xFinalize)(&ctx);
4594 if( freeCtx ){
4595 sqliteFree( aMem[i].z );
4596 }
4597 aMem[i] = ctx.s;
4598 if( aMem[i].flags & MEM_Short ){
4599 aMem[i].z = aMem[i].zShort;
4600 }
4601 }
4602 }
4603 break;
4604}
4605
4606/* Opcode: SetInsert P1 * P3
4607**
4608** If Set P1 does not exist then create it. Then insert value
4609** P3 into that set. If P3 is NULL, then insert the top of the
4610** stack into the set.
4611*/
4612case OP_SetInsert: {
4613 int i = pOp->p1;
4614 if( p->nSet<=i ){
4615 int k;
4616 Set *aSet = sqliteRealloc(p->aSet, (i+1)*sizeof(p->aSet[0]) );
4617 if( aSet==0 ) goto no_mem;
4618 p->aSet = aSet;
4619 for(k=p->nSet; k<=i; k++){
4620 sqliteHashInit(&p->aSet[k].hash, SQLITE_HASH_BINARY, 1);
4621 }
4622 p->nSet = i+1;
4623 }
4624 if( pOp->p3 ){
4625 sqliteHashInsert(&p->aSet[i].hash, pOp->p3, strlen(pOp->p3)+1, p);
4626 }else{
4627 assert( pTos>=p->aStack );
4628 Stringify(pTos);
4629 sqliteHashInsert(&p->aSet[i].hash, pTos->z, pTos->n, p);
4630 Release(pTos);
4631 pTos--;
4632 }
4633 if( sqlite_malloc_failed ) goto no_mem;
4634 break;
4635}
4636
4637/* Opcode: SetFound P1 P2 *
4638**
4639** Pop the stack once and compare the value popped off with the
4640** contents of set P1. If the element popped exists in set P1,
4641** then jump to P2. Otherwise fall through.
4642*/
4643case OP_SetFound: {
4644 int i = pOp->p1;
4645 assert( pTos>=p->aStack );
4646 Stringify(pTos);
4647 if( i>=0 && i<p->nSet && sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)){
4648 pc = pOp->p2 - 1;
4649 }
4650 Release(pTos);
4651 pTos--;
4652 break;
4653}
4654
4655/* Opcode: SetNotFound P1 P2 *
4656**
4657** Pop the stack once and compare the value popped off with the
4658** contents of set P1. If the element popped does not exists in
4659** set P1, then jump to P2. Otherwise fall through.
4660*/
4661case OP_SetNotFound: {
4662 int i = pOp->p1;
4663 assert( pTos>=p->aStack );
4664 Stringify(pTos);
4665 if( i<0 || i>=p->nSet ||
4666 sqliteHashFind(&p->aSet[i].hash, pTos->z, pTos->n)==0 ){
4667 pc = pOp->p2 - 1;
4668 }
4669 Release(pTos);
4670 pTos--;
4671 break;
4672}
4673
4674/* Opcode: SetFirst P1 P2 *
4675**
4676** Read the first element from set P1 and push it onto the stack. If the
4677** set is empty, push nothing and jump immediately to P2. This opcode is
4678** used in combination with OP_SetNext to loop over all elements of a set.
4679*/
4680/* Opcode: SetNext P1 P2 *
4681**
4682** Read the next element from set P1 and push it onto the stack. If there
4683** are no more elements in the set, do not do the push and fall through.
4684** Otherwise, jump to P2 after pushing the next set element.
4685*/
4686case OP_SetFirst:
4687case OP_SetNext: {
4688 Set *pSet;
4689 CHECK_FOR_INTERRUPT;
4690 if( pOp->p1<0 || pOp->p1>=p->nSet ){
4691 if( pOp->opcode==OP_SetFirst ) pc = pOp->p2 - 1;
4692 break;
4693 }
4694 pSet = &p->aSet[pOp->p1];
4695 if( pOp->opcode==OP_SetFirst ){
4696 pSet->prev = sqliteHashFirst(&pSet->hash);
4697 if( pSet->prev==0 ){
4698 pc = pOp->p2 - 1;
4699 break;
4700 }
4701 }else{
4702 if( pSet->prev ){
4703 pSet->prev = sqliteHashNext(pSet->prev);
4704 }
4705 if( pSet->prev==0 ){
4706 break;
4707 }else{
4708 pc = pOp->p2 - 1;
4709 }
4710 }
4711 pTos++;
4712 pTos->z = sqliteHashKey(pSet->prev);
4713 pTos->n = sqliteHashKeysize(pSet->prev);
4714 pTos->flags = MEM_Str | MEM_Ephem;
4715 break;
4716}
4717
4718/* Opcode: Vacuum * * *
4719**
4720** Vacuum the entire database. This opcode will cause other virtual
4721** machines to be created and run. It may not be called from within
4722** a transaction.
4723*/
4724case OP_Vacuum: {
4725 if( sqliteSafetyOff(db) ) goto abort_due_to_misuse;
4726 rc = sqliteRunVacuum(&p->zErrMsg, db);
4727 if( sqliteSafetyOn(db) ) goto abort_due_to_misuse;
4728 break;
4729}
4730
4731/* Opcode: StackDepth * * *
4732**
4733** Push an integer onto the stack which is the depth of the stack prior
4734** to that integer being pushed.
4735*/
4736case OP_StackDepth: {
4737 int depth = (&pTos[1]) - p->aStack;
4738 pTos++;
4739 pTos->i = depth;
4740 pTos->flags = MEM_Int;
4741 break;
4742}
4743
4744/* Opcode: StackReset * * *
4745**
4746** Pop a single integer off of the stack. Then pop the stack
4747** as many times as necessary to get the depth of the stack down
4748** to the value of the integer that was popped.
4749*/
4750case OP_StackReset: {
4751 int depth, goal;
4752 assert( pTos>=p->aStack );
4753 Integerify(pTos);
4754 goal = pTos->i;
4755 depth = (&pTos[1]) - p->aStack;
4756 assert( goal<depth );
4757 popStack(&pTos, depth-goal);
4758 break;
4759}
4760
4761/* An other opcode is illegal...
4762*/
4763default: {
4764 sqlite_snprintf(sizeof(zBuf),zBuf,"%d",pOp->opcode);
4765 sqliteSetString(&p->zErrMsg, "unknown opcode ", zBuf, (char*)0);
4766 rc = SQLITE_INTERNAL;
4767 break;
4768}
4769
4770/*****************************************************************************
4771** The cases of the switch statement above this line should all be indented
4772** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4773** readability. From this point on down, the normal indentation rules are
4774** restored.
4775*****************************************************************************/
4776 }
4777
4778#ifdef VDBE_PROFILE
4779 {
4780 long long elapse = hwtime() - start;
4781 pOp->cycles += elapse;
4782 pOp->cnt++;
4783#if 0
4784 fprintf(stdout, "%10lld ", elapse);
4785 sqliteVdbePrintOp(stdout, origPc, &p->aOp[origPc]);
4786#endif
4787 }
4788#endif
4789
4790 /* The following code adds nothing to the actual functionality
4791 ** of the program. It is only here for testing and debugging.
4792 ** On the other hand, it does burn CPU cycles every time through
4793 ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4794 */
4795#ifndef NDEBUG
4796 /* Sanity checking on the top element of the stack */
4797 if( pTos>=p->aStack ){
4798 assert( pTos->flags!=0 ); /* Must define some type */
4799 if( pTos->flags & MEM_Str ){
4800 int x = pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
4801 assert( x!=0 ); /* Strings must define a string subtype */
4802 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
4803 assert( pTos->z!=0 ); /* Strings must have a value */
4804 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
4805 assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort );
4806 assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort );
4807 }else{
4808 /* Cannot define a string subtype for non-string objects */
4809 assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
4810 }
4811 /* MEM_Null excludes all other types */
4812 assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 );
4813 }
4814 if( pc<-1 || pc>=p->nOp ){
4815 sqliteSetString(&p->zErrMsg, "jump destination out of range", (char*)0);
4816 rc = SQLITE_INTERNAL;
4817 }
4818 if( p->trace && pTos>=p->aStack ){
4819 int i;
4820 fprintf(p->trace, "Stack:");
4821 for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4822 if( pTos[i].flags & MEM_Null ){
4823 fprintf(p->trace, " NULL");
4824 }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
4825 fprintf(p->trace, " si:%d", pTos[i].i);
4826 }else if( pTos[i].flags & MEM_Int ){
4827 fprintf(p->trace, " i:%d", pTos[i].i);
4828 }else if( pTos[i].flags & MEM_Real ){
4829 fprintf(p->trace, " r:%g", pTos[i].r);
4830 }else if( pTos[i].flags & MEM_Str ){
4831 int j, k;
4832 char zBuf[100];
4833 zBuf[0] = ' ';
4834 if( pTos[i].flags & MEM_Dyn ){
4835 zBuf[1] = 'z';
4836 assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 );
4837 }else if( pTos[i].flags & MEM_Static ){
4838 zBuf[1] = 't';
4839 assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
4840 }else if( pTos[i].flags & MEM_Ephem ){
4841 zBuf[1] = 'e';
4842 assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 );
4843 }else{
4844 zBuf[1] = 's';
4845 }
4846 zBuf[2] = '[';
4847 k = 3;
4848 for(j=0; j<20 && j<pTos[i].n; j++){
4849 int c = pTos[i].z[j];
4850 if( c==0 && j==pTos[i].n-1 ) break;
4851 if( isprint(c) && !isspace(c) ){
4852 zBuf[k++] = c;
4853 }else{
4854 zBuf[k++] = '.';
4855 }
4856 }
4857 zBuf[k++] = ']';
4858 zBuf[k++] = 0;
4859 fprintf(p->trace, "%s", zBuf);
4860 }else{
4861 fprintf(p->trace, " ???");
4862 }
4863 }
4864 if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
4865 fprintf(p->trace,"\n");
4866 }
4867#endif
4868 } /* The end of the for(;;) loop the loops through opcodes */
4869
4870 /* If we reach this point, it means that execution is finished.
4871 */
4872vdbe_halt:
4873 CHECK_FOR_INTERRUPT
4874 if( rc ){
4875 p->rc = rc;
4876 rc = SQLITE_ERROR;
4877 }else{
4878 rc = SQLITE_DONE;
4879 }
4880 p->magic = VDBE_MAGIC_HALT;
4881 p->pTos = pTos;
4882 return rc;
4883
4884 /* Jump to here if a malloc() fails. It's hard to get a malloc()
4885 ** to fail on a modern VM computer, so this code is untested.
4886 */
4887no_mem:
4888 sqliteSetString(&p->zErrMsg, "out of memory", (char*)0);
4889 rc = SQLITE_NOMEM;
4890 goto vdbe_halt;
4891
4892 /* Jump to here for an SQLITE_MISUSE error.
4893 */
4894abort_due_to_misuse:
4895 rc = SQLITE_MISUSE;
4896 /* Fall thru into abort_due_to_error */
4897
4898 /* Jump to here for any other kind of fatal error. The "rc" variable
4899 ** should hold the error number.
4900 */
4901abort_due_to_error:
4902 if( p->zErrMsg==0 ){
4903 if( sqlite_malloc_failed ) rc = SQLITE_NOMEM;
4904 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
4905 }
4906 goto vdbe_halt;
4907
4908 /* Jump to here if the sqlite_interrupt() API sets the interrupt
4909 ** flag.
4910 */
4911abort_due_to_interrupt:
4912 assert( db->flags & SQLITE_Interrupt );
4913 db->flags &= ~SQLITE_Interrupt;
4914 if( db->magic!=SQLITE_MAGIC_BUSY ){
4915 rc = SQLITE_MISUSE;
4916 }else{
4917 rc = SQLITE_INTERRUPT;
4918 }
4919 sqliteSetString(&p->zErrMsg, sqlite_error_string(rc), (char*)0);
4920 goto vdbe_halt;
4921}
Note: See TracBrowser for help on using the repository browser.