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 "sqlite3_stmt*" 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 sqlite3VdbeExec()
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$
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_MoveXX, OP_Next, or OP_Prev opcodes. 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 : */
60 : #ifdef SQLITE_TEST
61 : int sqlite3_search_count = 0;
62 : #endif
63 :
64 : /*
65 : ** When this global variable is positive, it gets decremented once before
66 : ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
67 : ** field of the sqlite3 structure is set in order to simulate and interrupt.
68 : **
69 : ** This facility is used for testing purposes only. It does not function
70 : ** in an ordinary build.
71 : */
72 : #ifdef SQLITE_TEST
73 : int sqlite3_interrupt_count = 0;
74 : #endif
75 :
76 : /*
77 : ** The next global variable is incremented each type the OP_Sort opcode
78 : ** is executed. The test procedures use this information to make sure that
79 : ** sorting is occurring or not occuring at appropriate times. This variable
80 : ** has no function other than to help verify the correct operation of the
81 : ** library.
82 : */
83 : #ifdef SQLITE_TEST
84 : int sqlite3_sort_count = 0;
85 : #endif
86 :
87 : /*
88 : ** Release the memory associated with the given stack level. This
89 : ** leaves the Mem.flags field in an inconsistent state.
90 : */
91 : #define Release(P) if((P)->flags&MEM_Dyn){ sqlite3VdbeMemRelease(P); }
92 :
93 : /*
94 : ** Convert the given stack entity into a string if it isn't one
95 : ** already. Return non-zero if a malloc() fails.
96 : */
97 : #define Stringify(P, enc) \
98 : if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
99 : { goto no_mem; }
100 :
101 : /*
102 : ** Convert the given stack entity into a string that has been obtained
103 : ** from sqliteMalloc(). This is different from Stringify() above in that
104 : ** Stringify() will use the NBFS bytes of static string space if the string
105 : ** will fit but this routine always mallocs for space.
106 : ** Return non-zero if we run out of memory.
107 : */
108 : #define Dynamicify(P,enc) sqlite3VdbeMemDynamicify(P)
109 :
110 : /*
111 : ** The header of a record consists of a sequence variable-length integers.
112 : ** These integers are almost always small and are encoded as a single byte.
113 : ** The following macro takes advantage this fact to provide a fast decode
114 : ** of the integers in a record header. It is faster for the common case
115 : ** where the integer is a single byte. It is a little slower when the
116 : ** integer is two or more bytes. But overall it is faster.
117 : **
118 : ** The following expressions are equivalent:
119 : **
120 : ** x = sqlite3GetVarint32( A, &B );
121 : **
122 : ** x = GetVarint( A, B );
123 : **
124 : */
125 : #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
126 :
127 : /*
128 : ** An ephemeral string value (signified by the MEM_Ephem flag) contains
129 : ** a pointer to a dynamically allocated string where some other entity
130 : ** is responsible for deallocating that string. Because the stack entry
131 : ** does not control the string, it might be deleted without the stack
132 : ** entry knowing it.
133 : **
134 : ** This routine converts an ephemeral string into a dynamically allocated
135 : ** string that the stack entry itself controls. In other words, it
136 : ** converts an MEM_Ephem string into an MEM_Dyn string.
137 : */
138 : #define Deephemeralize(P) \
139 : if( ((P)->flags&MEM_Ephem)!=0 \
140 : && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
141 :
142 : /*
143 : ** Argument pMem points at a memory cell that will be passed to a
144 : ** user-defined function or returned to the user as the result of a query.
145 : ** The second argument, 'db_enc' is the text encoding used by the vdbe for
146 : ** stack variables. This routine sets the pMem->enc and pMem->type
147 : ** variables used by the sqlite3_value_*() routines.
148 : */
149 : #define storeTypeInfo(A,B) _storeTypeInfo(A)
150 873 : static void _storeTypeInfo(Mem *pMem){
151 873 : int flags = pMem->flags;
152 873 : if( flags & MEM_Null ){
153 47 : pMem->type = SQLITE_NULL;
154 : }
155 826 : else if( flags & MEM_Int ){
156 320 : pMem->type = SQLITE_INTEGER;
157 : }
158 506 : else if( flags & MEM_Real ){
159 0 : pMem->type = SQLITE_FLOAT;
160 : }
161 506 : else if( flags & MEM_Str ){
162 506 : pMem->type = SQLITE_TEXT;
163 : }else{
164 0 : pMem->type = SQLITE_BLOB;
165 : }
166 873 : }
167 :
168 : /*
169 : ** Pop the stack N times.
170 : */
171 1388 : static void popStack(Mem **ppTos, int N){
172 1388 : Mem *pTos = *ppTos;
173 5784 : while( N>0 ){
174 3008 : N--;
175 3008 : Release(pTos);
176 3008 : pTos--;
177 : }
178 1388 : *ppTos = pTos;
179 1388 : }
180 :
181 : /*
182 : ** Allocate cursor number iCur. Return a pointer to it. Return NULL
183 : ** if we run out of memory.
184 : */
185 730 : static Cursor *allocateCursor(Vdbe *p, int iCur, int iDb){
186 : Cursor *pCx;
187 : assert( iCur<p->nCursor );
188 730 : if( p->apCsr[iCur] ){
189 0 : sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
190 : }
191 730 : p->apCsr[iCur] = pCx = sqliteMalloc( sizeof(Cursor) );
192 730 : if( pCx ){
193 730 : pCx->iDb = iDb;
194 : }
195 730 : return pCx;
196 : }
197 :
198 : /*
199 : ** Try to convert a value into a numeric representation if we can
200 : ** do so without loss of information. In other words, if the string
201 : ** looks like a number, convert it into a number. If it does not
202 : ** look like a number, leave it alone.
203 : */
204 467 : static void applyNumericAffinity(Mem *pRec){
205 467 : if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
206 : int realnum;
207 107 : sqlite3VdbeMemNulTerminate(pRec);
208 107 : if( (pRec->flags&MEM_Str)
209 : && sqlite3IsNumber(pRec->z, &realnum, pRec->enc) ){
210 : i64 value;
211 94 : sqlite3VdbeChangeEncoding(pRec, SQLITE_UTF8);
212 188 : if( !realnum && sqlite3atoi64(pRec->z, &value) ){
213 94 : sqlite3VdbeMemRelease(pRec);
214 94 : pRec->u.i = value;
215 94 : pRec->flags = MEM_Int;
216 : }else{
217 0 : sqlite3VdbeMemRealify(pRec);
218 : }
219 : }
220 : }
221 467 : }
222 :
223 : /*
224 : ** Processing is determine by the affinity parameter:
225 : **
226 : ** SQLITE_AFF_INTEGER:
227 : ** SQLITE_AFF_REAL:
228 : ** SQLITE_AFF_NUMERIC:
229 : ** Try to convert pRec to an integer representation or a
230 : ** floating-point representation if an integer representation
231 : ** is not possible. Note that the integer representation is
232 : ** always preferred, even if the affinity is REAL, because
233 : ** an integer representation is more space efficient on disk.
234 : **
235 : ** SQLITE_AFF_TEXT:
236 : ** Convert pRec to a text representation.
237 : **
238 : ** SQLITE_AFF_NONE:
239 : ** No-op. pRec is unchanged.
240 : */
241 1377 : static void applyAffinity(Mem *pRec, char affinity, u8 enc){
242 1377 : if( affinity==SQLITE_AFF_TEXT ){
243 : /* Only attempt the conversion to TEXT if there is an integer or real
244 : ** representation (blob and NULL do not get converted) but no string
245 : ** representation.
246 : */
247 904 : if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
248 0 : sqlite3VdbeMemStringify(pRec, enc);
249 : }
250 904 : pRec->flags &= ~(MEM_Real|MEM_Int);
251 473 : }else if( affinity!=SQLITE_AFF_NONE ){
252 : assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
253 : || affinity==SQLITE_AFF_NUMERIC );
254 467 : applyNumericAffinity(pRec);
255 467 : if( pRec->flags & MEM_Real ){
256 0 : sqlite3VdbeIntegerAffinity(pRec);
257 : }
258 : }
259 1377 : }
260 :
261 : /*
262 : ** Try to convert the type of a function argument or a result column
263 : ** into a numeric representation. Use either INTEGER or REAL whichever
264 : ** is appropriate. But only do the conversion if it is possible without
265 : ** loss of information and return the revised type of the argument.
266 : **
267 : ** This is an EXPERIMENTAL api and is subject to change or removal.
268 : */
269 0 : int sqlite3_value_numeric_type(sqlite3_value *pVal){
270 0 : Mem *pMem = (Mem*)pVal;
271 0 : applyNumericAffinity(pMem);
272 0 : storeTypeInfo(pMem, 0);
273 0 : return pMem->type;
274 : }
275 :
276 : /*
277 : ** Exported version of applyAffinity(). This one works on sqlite3_value*,
278 : ** not the internal Mem* type.
279 : */
280 0 : void sqlite3ValueApplyAffinity(sqlite3_value *pVal, u8 affinity, u8 enc){
281 0 : applyAffinity((Mem *)pVal, affinity, enc);
282 0 : }
283 :
284 : #ifdef SQLITE_DEBUG
285 : /*
286 : ** Write a nice string representation of the contents of cell pMem
287 : ** into buffer zBuf, length nBuf.
288 : */
289 : void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
290 : char *zCsr = zBuf;
291 : int f = pMem->flags;
292 :
293 : static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
294 :
295 : if( f&MEM_Blob ){
296 : int i;
297 : char c;
298 : if( f & MEM_Dyn ){
299 : c = 'z';
300 : assert( (f & (MEM_Static|MEM_Ephem))==0 );
301 : }else if( f & MEM_Static ){
302 : c = 't';
303 : assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
304 : }else if( f & MEM_Ephem ){
305 : c = 'e';
306 : assert( (f & (MEM_Static|MEM_Dyn))==0 );
307 : }else{
308 : c = 's';
309 : }
310 :
311 : zCsr += sprintf(zCsr, "%c", c);
312 : zCsr += sprintf(zCsr, "%d[", pMem->n);
313 : for(i=0; i<16 && i<pMem->n; i++){
314 : zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
315 : }
316 : for(i=0; i<16 && i<pMem->n; i++){
317 : char z = pMem->z[i];
318 : if( z<32 || z>126 ) *zCsr++ = '.';
319 : else *zCsr++ = z;
320 : }
321 :
322 : zCsr += sprintf(zCsr, "]");
323 : *zCsr = '\0';
324 : }else if( f & MEM_Str ){
325 : int j, k;
326 : zBuf[0] = ' ';
327 : if( f & MEM_Dyn ){
328 : zBuf[1] = 'z';
329 : assert( (f & (MEM_Static|MEM_Ephem))==0 );
330 : }else if( f & MEM_Static ){
331 : zBuf[1] = 't';
332 : assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
333 : }else if( f & MEM_Ephem ){
334 : zBuf[1] = 'e';
335 : assert( (f & (MEM_Static|MEM_Dyn))==0 );
336 : }else{
337 : zBuf[1] = 's';
338 : }
339 : k = 2;
340 : k += sprintf(&zBuf[k], "%d", pMem->n);
341 : zBuf[k++] = '[';
342 : for(j=0; j<15 && j<pMem->n; j++){
343 : u8 c = pMem->z[j];
344 : if( c>=0x20 && c<0x7f ){
345 : zBuf[k++] = c;
346 : }else{
347 : zBuf[k++] = '.';
348 : }
349 : }
350 : zBuf[k++] = ']';
351 : k += sprintf(&zBuf[k], encnames[pMem->enc]);
352 : zBuf[k++] = 0;
353 : }
354 : }
355 : #endif
356 :
357 :
358 : #ifdef VDBE_PROFILE
359 : /*
360 : ** The following routine only works on pentium-class processors.
361 : ** It uses the RDTSC opcode to read the cycle count value out of the
362 : ** processor and returns that value. This can be used for high-res
363 : ** profiling.
364 : */
365 : __inline__ unsigned long long int hwtime(void){
366 : unsigned long long int x;
367 : __asm__("rdtsc\n\t"
368 : "mov %%edx, %%ecx\n\t"
369 : :"=A" (x));
370 : return x;
371 : }
372 : #endif
373 :
374 : /*
375 : ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
376 : ** sqlite3_interrupt() routine has been called. If it has been, then
377 : ** processing of the VDBE program is interrupted.
378 : **
379 : ** This macro added to every instruction that does a jump in order to
380 : ** implement a loop. This test used to be on every single instruction,
381 : ** but that meant we more testing that we needed. By only testing the
382 : ** flag on jump instructions, we get a (small) speed improvement.
383 : */
384 : #define CHECK_FOR_INTERRUPT \
385 : if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
386 :
387 :
388 : /*
389 : ** Execute as much of a VDBE program as we can then return.
390 : **
391 : ** sqlite3VdbeMakeReady() must be called before this routine in order to
392 : ** close the program with a final OP_Halt and to set up the callbacks
393 : ** and the error message pointer.
394 : **
395 : ** Whenever a row or result data is available, this routine will either
396 : ** invoke the result callback (if there is one) or return with
397 : ** SQLITE_ROW.
398 : **
399 : ** If an attempt is made to open a locked database, then this routine
400 : ** will either invoke the busy callback (if there is one) or it will
401 : ** return SQLITE_BUSY.
402 : **
403 : ** If an error occurs, an error message is written to memory obtained
404 : ** from sqliteMalloc() and p->zErrMsg is made to point to that memory.
405 : ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
406 : **
407 : ** If the callback ever returns non-zero, then the program exits
408 : ** immediately. There will be no error message but the p->rc field is
409 : ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
410 : **
411 : ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
412 : ** routine to return SQLITE_ERROR.
413 : **
414 : ** Other fatal errors return SQLITE_ERROR.
415 : **
416 : ** After this routine has finished, sqlite3VdbeFinalize() should be
417 : ** used to clean up the mess that was left behind.
418 : */
419 : int sqlite3VdbeExec(
420 : Vdbe *p /* The VDBE */
421 1000 : ){
422 : int pc; /* The program counter */
423 : Op *pOp; /* Current operation */
424 1000 : int rc = SQLITE_OK; /* Value to return */
425 1000 : sqlite3 *db = p->db; /* The database */
426 1000 : u8 encoding = ENC(db); /* The database encoding */
427 : Mem *pTos; /* Top entry in the operand stack */
428 : #ifdef VDBE_PROFILE
429 : unsigned long long start; /* CPU clock count at start of opcode */
430 : int origPc; /* Program counter at start of opcode */
431 : #endif
432 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
433 1000 : int nProgressOps = 0; /* Opcodes executed since progress callback. */
434 : #endif
435 : #ifndef NDEBUG
436 : Mem *pStackLimit;
437 : #endif
438 :
439 1000 : if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
440 : assert( db->magic==SQLITE_MAGIC_BUSY );
441 1000 : pTos = p->pTos;
442 1000 : if( p->rc==SQLITE_NOMEM ){
443 : /* This happens if a malloc() inside a call to sqlite3_column_text() or
444 : ** sqlite3_column_text16() failed. */
445 0 : goto no_mem;
446 : }
447 : assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
448 1000 : p->rc = SQLITE_OK;
449 : assert( p->explain==0 );
450 1000 : if( p->popStack ){
451 305 : popStack(&pTos, p->popStack);
452 305 : p->popStack = 0;
453 : }
454 1000 : p->resOnStack = 0;
455 1000 : db->busyHandler.nBusy = 0;
456 1000 : CHECK_FOR_INTERRUPT;
457 : sqlite3VdbeIOTraceSql(p);
458 : #ifdef SQLITE_DEBUG
459 : if( (p->db->flags & SQLITE_VdbeListing)!=0
460 : || sqlite3OsFileExists("vdbe_explain")
461 : ){
462 : int i;
463 : printf("VDBE Program Listing:\n");
464 : sqlite3VdbePrintSql(p);
465 : for(i=0; i<p->nOp; i++){
466 : sqlite3VdbePrintOp(stdout, i, &p->aOp[i]);
467 : }
468 : }
469 : if( sqlite3OsFileExists("vdbe_trace") ){
470 : p->trace = stdout;
471 : }
472 : #endif
473 12454 : for(pc=p->pc; rc==SQLITE_OK; pc++){
474 : assert( pc>=0 && pc<p->nOp );
475 : assert( pTos<=&p->aStack[pc] );
476 12453 : if( sqlite3MallocFailed() ) goto no_mem;
477 : #ifdef VDBE_PROFILE
478 : origPc = pc;
479 : start = hwtime();
480 : #endif
481 12453 : pOp = &p->aOp[pc];
482 :
483 : /* Only allow tracing if SQLITE_DEBUG is defined.
484 : */
485 : #ifdef SQLITE_DEBUG
486 : if( p->trace ){
487 : if( pc==0 ){
488 : printf("VDBE Execution Trace:\n");
489 : sqlite3VdbePrintSql(p);
490 : }
491 : sqlite3VdbePrintOp(p->trace, pc, pOp);
492 : }
493 : if( p->trace==0 && pc==0 && sqlite3OsFileExists("vdbe_sqltrace") ){
494 : sqlite3VdbePrintSql(p);
495 : }
496 : #endif
497 :
498 :
499 : /* Check to see if we need to simulate an interrupt. This only happens
500 : ** if we have a special test build.
501 : */
502 : #ifdef SQLITE_TEST
503 : if( sqlite3_interrupt_count>0 ){
504 : sqlite3_interrupt_count--;
505 : if( sqlite3_interrupt_count==0 ){
506 : sqlite3_interrupt(db);
507 : }
508 : }
509 : #endif
510 :
511 : #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
512 : /* Call the progress callback if it is configured and the required number
513 : ** of VDBE ops have been executed (either since this invocation of
514 : ** sqlite3VdbeExec() or since last time the progress callback was called).
515 : ** If the progress callback returns non-zero, exit the virtual machine with
516 : ** a return code SQLITE_ABORT.
517 : */
518 12453 : if( db->xProgress ){
519 0 : if( db->nProgressOps==nProgressOps ){
520 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
521 0 : if( db->xProgress(db->pProgressArg)!=0 ){
522 0 : sqlite3SafetyOn(db);
523 0 : rc = SQLITE_ABORT;
524 0 : continue; /* skip to the next iteration of the for loop */
525 : }
526 0 : nProgressOps = 0;
527 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
528 : }
529 0 : nProgressOps++;
530 : }
531 : #endif
532 :
533 : #ifndef NDEBUG
534 : /* This is to check that the return value of static function
535 : ** opcodeNoPush() (see vdbeaux.c) returns values that match the
536 : ** implementation of the virtual machine in this file. If
537 : ** opcodeNoPush() returns non-zero, then the stack is guarenteed
538 : ** not to grow when the opcode is executed. If it returns zero, then
539 : ** the stack may grow by at most 1.
540 : **
541 : ** The global wrapper function sqlite3VdbeOpcodeUsesStack() is not
542 : ** available if NDEBUG is defined at build time.
543 : */
544 : pStackLimit = pTos;
545 : if( !sqlite3VdbeOpcodeNoPush(pOp->opcode) ){
546 : pStackLimit++;
547 : }
548 : #endif
549 :
550 12453 : switch( pOp->opcode ){
551 :
552 : /*****************************************************************************
553 : ** What follows is a massive switch statement where each case implements a
554 : ** separate instruction in the virtual machine. If we follow the usual
555 : ** indentation conventions, each case should be indented by 6 spaces. But
556 : ** that is a lot of wasted space on the left margin. So the code within
557 : ** the switch statement will break with convention and be flush-left. Another
558 : ** big comment (similar to this one) will mark the point in the code where
559 : ** we transition back to normal indentation.
560 : **
561 : ** The formatting of each case is important. The makefile for SQLite
562 : ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
563 : ** file looking for lines that begin with "case OP_". The opcodes.h files
564 : ** will be filled with #defines that give unique integer values to each
565 : ** opcode and the opcodes.c file is filled with an array of strings where
566 : ** each string is the symbolic name for the corresponding opcode. If the
567 : ** case statement is followed by a comment of the form "/# same as ... #/"
568 : ** that comment is used to determine the particular value of the opcode.
569 : **
570 : ** If a comment on the same line as the "case OP_" construction contains
571 : ** the word "no-push", then the opcode is guarenteed not to grow the
572 : ** vdbe stack when it is executed. See function opcode() in
573 : ** vdbeaux.c for details.
574 : **
575 : ** Documentation about VDBE opcodes is generated by scanning this file
576 : ** for lines of that contain "Opcode:". That line and all subsequent
577 : ** comment lines are used in the generation of the opcode.html documentation
578 : ** file.
579 : **
580 : ** SUMMARY:
581 : **
582 : ** Formatting is important to scripts that scan this file.
583 : ** Do not deviate from the formatting style currently in use.
584 : **
585 : *****************************************************************************/
586 :
587 : /* Opcode: Goto * P2 *
588 : **
589 : ** An unconditional jump to address P2.
590 : ** The next instruction executed will be
591 : ** the one at index P2 from the beginning of
592 : ** the program.
593 : */
594 : case OP_Goto: { /* no-push */
595 894 : CHECK_FOR_INTERRUPT;
596 894 : pc = pOp->p2 - 1;
597 894 : break;
598 : }
599 :
600 : /* Opcode: Gosub * P2 *
601 : **
602 : ** Push the current address plus 1 onto the return address stack
603 : ** and then jump to address P2.
604 : **
605 : ** The return address stack is of limited depth. If too many
606 : ** OP_Gosub operations occur without intervening OP_Returns, then
607 : ** the return address stack will fill up and processing will abort
608 : ** with a fatal error.
609 : */
610 : case OP_Gosub: { /* no-push */
611 : assert( p->returnDepth<sizeof(p->returnStack)/sizeof(p->returnStack[0]) );
612 0 : p->returnStack[p->returnDepth++] = pc+1;
613 0 : pc = pOp->p2 - 1;
614 0 : break;
615 : }
616 :
617 : /* Opcode: Return * * *
618 : **
619 : ** Jump immediately to the next instruction after the last unreturned
620 : ** OP_Gosub. If an OP_Return has occurred for all OP_Gosubs, then
621 : ** processing aborts with a fatal error.
622 : */
623 : case OP_Return: { /* no-push */
624 : assert( p->returnDepth>0 );
625 0 : p->returnDepth--;
626 0 : pc = p->returnStack[p->returnDepth] - 1;
627 0 : break;
628 : }
629 :
630 : /* Opcode: Halt P1 P2 P3
631 : **
632 : ** Exit immediately. All open cursors, Fifos, etc are closed
633 : ** automatically.
634 : **
635 : ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
636 : ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0).
637 : ** For errors, it can be some other value. If P1!=0 then P2 will determine
638 : ** whether or not to rollback the current transaction. Do not rollback
639 : ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort,
640 : ** then back out all changes that have occurred during this execution of the
641 : ** VDBE, but do not rollback the transaction.
642 : **
643 : ** If P3 is not null then it is an error message string.
644 : **
645 : ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
646 : ** every program. So a jump past the last instruction of the program
647 : ** is the same as executing Halt.
648 : */
649 : case OP_Halt: { /* no-push */
650 622 : p->pTos = pTos;
651 622 : p->rc = pOp->p1;
652 622 : p->pc = pc;
653 622 : p->errorAction = pOp->p2;
654 622 : if( pOp->p3 ){
655 0 : sqlite3SetString(&p->zErrMsg, pOp->p3, (char*)0);
656 : }
657 622 : rc = sqlite3VdbeHalt(p);
658 : assert( rc==SQLITE_BUSY || rc==SQLITE_OK );
659 622 : if( rc==SQLITE_BUSY ){
660 0 : p->rc = SQLITE_BUSY;
661 0 : return SQLITE_BUSY;
662 : }
663 622 : return p->rc ? SQLITE_ERROR : SQLITE_DONE;
664 : }
665 :
666 : /* Opcode: Integer P1 * *
667 : **
668 : ** The 32-bit integer value P1 is pushed onto the stack.
669 : */
670 : case OP_Integer: {
671 999 : pTos++;
672 999 : pTos->flags = MEM_Int;
673 999 : pTos->u.i = pOp->p1;
674 999 : break;
675 : }
676 :
677 : /* Opcode: Int64 * * P3
678 : **
679 : ** P3 is a string representation of an integer. Convert that integer
680 : ** to a 64-bit value and push it onto the stack.
681 : */
682 : case OP_Int64: {
683 0 : pTos++;
684 : assert( pOp->p3!=0 );
685 0 : pTos->flags = MEM_Str|MEM_Static|MEM_Term;
686 0 : pTos->z = pOp->p3;
687 0 : pTos->n = strlen(pTos->z);
688 0 : pTos->enc = SQLITE_UTF8;
689 0 : pTos->u.i = sqlite3VdbeIntValue(pTos);
690 0 : pTos->flags |= MEM_Int;
691 0 : break;
692 : }
693 :
694 : /* Opcode: Real * * P3
695 : **
696 : ** The string value P3 is converted to a real and pushed on to the stack.
697 : */
698 : case OP_Real: { /* same as TK_FLOAT, */
699 0 : pTos++;
700 0 : pTos->flags = MEM_Str|MEM_Static|MEM_Term;
701 0 : pTos->z = pOp->p3;
702 0 : pTos->n = strlen(pTos->z);
703 0 : pTos->enc = SQLITE_UTF8;
704 0 : pTos->r = sqlite3VdbeRealValue(pTos);
705 0 : pTos->flags |= MEM_Real;
706 0 : sqlite3VdbeChangeEncoding(pTos, encoding);
707 0 : break;
708 : }
709 :
710 : /* Opcode: String8 * * P3
711 : **
712 : ** P3 points to a nul terminated UTF-8 string. This opcode is transformed
713 : ** into an OP_String before it is executed for the first time.
714 : */
715 : case OP_String8: { /* same as TK_STRING */
716 : assert( pOp->p3!=0 );
717 569 : pOp->opcode = OP_String;
718 569 : pOp->p1 = strlen(pOp->p3);
719 :
720 : #ifndef SQLITE_OMIT_UTF16
721 569 : if( encoding!=SQLITE_UTF8 ){
722 0 : pTos++;
723 0 : sqlite3VdbeMemSetStr(pTos, pOp->p3, -1, SQLITE_UTF8, SQLITE_STATIC);
724 0 : if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pTos, encoding) ) goto no_mem;
725 0 : if( SQLITE_OK!=sqlite3VdbeMemDynamicify(pTos) ) goto no_mem;
726 0 : pTos->flags &= ~(MEM_Dyn);
727 0 : pTos->flags |= MEM_Static;
728 0 : if( pOp->p3type==P3_DYNAMIC ){
729 0 : sqliteFree(pOp->p3);
730 : }
731 0 : pOp->p3type = P3_DYNAMIC;
732 0 : pOp->p3 = pTos->z;
733 0 : pOp->p1 = pTos->n;
734 0 : break;
735 : }
736 : #endif
737 : /* Otherwise fall through to the next case, OP_String */
738 : }
739 :
740 : /* Opcode: String P1 * P3
741 : **
742 : ** The string value P3 of length P1 (bytes) is pushed onto the stack.
743 : */
744 : case OP_String: {
745 617 : pTos++;
746 : assert( pOp->p3!=0 );
747 617 : pTos->flags = MEM_Str|MEM_Static|MEM_Term;
748 617 : pTos->z = pOp->p3;
749 617 : pTos->n = pOp->p1;
750 617 : pTos->enc = encoding;
751 617 : break;
752 : }
753 :
754 : /* Opcode: Null * * *
755 : **
756 : ** Push a NULL onto the stack.
757 : */
758 : case OP_Null: {
759 108 : pTos++;
760 108 : pTos->flags = MEM_Null;
761 108 : pTos->n = 0;
762 108 : break;
763 : }
764 :
765 :
766 : #ifndef SQLITE_OMIT_BLOB_LITERAL
767 : /* Opcode: HexBlob * * P3
768 : **
769 : ** P3 is an UTF-8 SQL hex encoding of a blob. The blob is pushed onto the
770 : ** vdbe stack.
771 : **
772 : ** The first time this instruction executes, in transforms itself into a
773 : ** 'Blob' opcode with a binary blob as P3.
774 : */
775 : case OP_HexBlob: { /* same as TK_BLOB */
776 0 : pOp->opcode = OP_Blob;
777 0 : pOp->p1 = strlen(pOp->p3)/2;
778 0 : if( pOp->p1 ){
779 0 : char *zBlob = sqlite3HexToBlob(pOp->p3);
780 0 : if( !zBlob ) goto no_mem;
781 0 : if( pOp->p3type==P3_DYNAMIC ){
782 0 : sqliteFree(pOp->p3);
783 : }
784 0 : pOp->p3 = zBlob;
785 0 : pOp->p3type = P3_DYNAMIC;
786 : }else{
787 0 : if( pOp->p3type==P3_DYNAMIC ){
788 0 : sqliteFree(pOp->p3);
789 : }
790 0 : pOp->p3type = P3_STATIC;
791 0 : pOp->p3 = "";
792 : }
793 :
794 : /* Fall through to the next case, OP_Blob. */
795 : }
796 :
797 : /* Opcode: Blob P1 * P3
798 : **
799 : ** P3 points to a blob of data P1 bytes long. Push this
800 : ** value onto the stack. This instruction is not coded directly
801 : ** by the compiler. Instead, the compiler layer specifies
802 : ** an OP_HexBlob opcode, with the hex string representation of
803 : ** the blob as P3. This opcode is transformed to an OP_Blob
804 : ** the first time it is executed.
805 : */
806 : case OP_Blob: {
807 0 : pTos++;
808 0 : sqlite3VdbeMemSetStr(pTos, pOp->p3, pOp->p1, 0, 0);
809 0 : break;
810 : }
811 : #endif /* SQLITE_OMIT_BLOB_LITERAL */
812 :
813 : /* Opcode: Variable P1 * *
814 : **
815 : ** Push the value of variable P1 onto the stack. A variable is
816 : ** an unknown in the original SQL string as handed to sqlite3_compile().
817 : ** Any occurance of the '?' character in the original SQL is considered
818 : ** a variable. Variables in the SQL string are number from left to
819 : ** right beginning with 1. The values of variables are set using the
820 : ** sqlite3_bind() API.
821 : */
822 : case OP_Variable: {
823 164 : int j = pOp->p1 - 1;
824 : assert( j>=0 && j<p->nVar );
825 :
826 164 : pTos++;
827 164 : sqlite3VdbeMemShallowCopy(pTos, &p->aVar[j], MEM_Static);
828 164 : break;
829 : }
830 :
831 : /* Opcode: Pop P1 * *
832 : **
833 : ** P1 elements are popped off of the top of stack and discarded.
834 : */
835 : case OP_Pop: { /* no-push */
836 : assert( pOp->p1>=0 );
837 39 : popStack(&pTos, pOp->p1);
838 : assert( pTos>=&p->aStack[-1] );
839 39 : break;
840 : }
841 :
842 : /* Opcode: Dup P1 P2 *
843 : **
844 : ** A copy of the P1-th element of the stack
845 : ** is made and pushed onto the top of the stack.
846 : ** The top of the stack is element 0. So the
847 : ** instruction "Dup 0 0 0" will make a copy of the
848 : ** top of the stack.
849 : **
850 : ** If the content of the P1-th element is a dynamically
851 : ** allocated string, then a new copy of that string
852 : ** is made if P2==0. If P2!=0, then just a pointer
853 : ** to the string is copied.
854 : **
855 : ** Also see the Pull instruction.
856 : */
857 : case OP_Dup: {
858 764 : Mem *pFrom = &pTos[-pOp->p1];
859 : assert( pFrom<=pTos && pFrom>=p->aStack );
860 764 : pTos++;
861 764 : sqlite3VdbeMemShallowCopy(pTos, pFrom, MEM_Ephem);
862 764 : if( pOp->p2 ){
863 488 : Deephemeralize(pTos);
864 : }
865 764 : break;
866 : }
867 :
868 : /* Opcode: Pull P1 * *
869 : **
870 : ** The P1-th element is removed from its current location on
871 : ** the stack and pushed back on top of the stack. The
872 : ** top of the stack is element 0, so "Pull 0 0 0" is
873 : ** a no-op. "Pull 1 0 0" swaps the top two elements of
874 : ** the stack.
875 : **
876 : ** See also the Dup instruction.
877 : */
878 : case OP_Pull: { /* no-push */
879 57 : Mem *pFrom = &pTos[-pOp->p1];
880 : int i;
881 : Mem ts;
882 :
883 57 : ts = *pFrom;
884 57 : Deephemeralize(pTos);
885 114 : for(i=0; i<pOp->p1; i++, pFrom++){
886 57 : Deephemeralize(&pFrom[1]);
887 : assert( (pFrom->flags & MEM_Ephem)==0 );
888 57 : *pFrom = pFrom[1];
889 57 : if( pFrom->flags & MEM_Short ){
890 : assert( pFrom->flags & (MEM_Str|MEM_Blob) );
891 : assert( pFrom->z==pFrom[1].zShort );
892 0 : pFrom->z = pFrom->zShort;
893 : }
894 : }
895 57 : *pTos = ts;
896 57 : if( pTos->flags & MEM_Short ){
897 : assert( pTos->flags & (MEM_Str|MEM_Blob) );
898 : assert( pTos->z==pTos[-pOp->p1].zShort );
899 0 : pTos->z = pTos->zShort;
900 : }
901 57 : break;
902 : }
903 :
904 : /* Opcode: Push P1 * *
905 : **
906 : ** Overwrite the value of the P1-th element down on the
907 : ** stack (P1==0 is the top of the stack) with the value
908 : ** of the top of the stack. Then pop the top of the stack.
909 : */
910 : case OP_Push: { /* no-push */
911 0 : Mem *pTo = &pTos[-pOp->p1];
912 :
913 : assert( pTo>=p->aStack );
914 0 : sqlite3VdbeMemMove(pTo, pTos);
915 0 : pTos--;
916 0 : break;
917 : }
918 :
919 : /* Opcode: Callback P1 * *
920 : **
921 : ** The top P1 values on the stack represent a single result row from
922 : ** a query. This opcode causes the sqlite3_step() call to terminate
923 : ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
924 : ** structure to provide access to the top P1 values as the result
925 : ** row. When the sqlite3_step() function is run again, the top P1
926 : ** values will be automatically popped from the stack before the next
927 : ** instruction executes.
928 : */
929 : case OP_Callback: { /* no-push */
930 : Mem *pMem;
931 : Mem *pFirstColumn;
932 : assert( p->nResColumn==pOp->p1 );
933 :
934 : /* Data in the pager might be moved or changed out from under us
935 : ** in between the return from this sqlite3_step() call and the
936 : ** next call to sqlite3_step(). So deephermeralize everything on
937 : ** the stack. Note that ephemeral data is never stored in memory
938 : ** cells so we do not have to worry about them.
939 : */
940 367 : pFirstColumn = &pTos[0-pOp->p1];
941 367 : for(pMem = p->aStack; pMem<pFirstColumn; pMem++){
942 0 : Deephemeralize(pMem);
943 : }
944 :
945 : /* Invalidate all ephemeral cursor row caches */
946 367 : p->cacheCtr = (p->cacheCtr + 2)|1;
947 :
948 : /* Make sure the results of the current row are \000 terminated
949 : ** and have an assigned type. The results are deephemeralized as
950 : ** as side effect.
951 : */
952 1212 : for(; pMem<=pTos; pMem++ ){
953 845 : sqlite3VdbeMemNulTerminate(pMem);
954 845 : storeTypeInfo(pMem, encoding);
955 : }
956 :
957 : /* Set up the statement structure so that it will pop the current
958 : ** results from the stack when the statement returns.
959 : */
960 367 : p->resOnStack = 1;
961 367 : p->nCallback++;
962 367 : p->popStack = pOp->p1;
963 367 : p->pc = pc + 1;
964 367 : p->pTos = pTos;
965 367 : return SQLITE_ROW;
966 : }
967 :
968 : /* Opcode: Concat P1 P2 *
969 : **
970 : ** Look at the first P1+2 elements of the stack. Append them all
971 : ** together with the lowest element first. The original P1+2 elements
972 : ** are popped from the stack if P2==0 and retained if P2==1. If
973 : ** any element of the stack is NULL, then the result is NULL.
974 : **
975 : ** When P1==1, this routine makes a copy of the top stack element
976 : ** into memory obtained from sqliteMalloc().
977 : */
978 : case OP_Concat: { /* same as TK_CONCAT */
979 : char *zNew;
980 : int nByte;
981 : int nField;
982 : int i, j;
983 : Mem *pTerm;
984 :
985 : /* Loop through the stack elements to see how long the result will be. */
986 0 : nField = pOp->p1 + 2;
987 0 : pTerm = &pTos[1-nField];
988 0 : nByte = 0;
989 0 : for(i=0; i<nField; i++, pTerm++){
990 : assert( pOp->p2==0 || (pTerm->flags&MEM_Str) );
991 0 : if( pTerm->flags&MEM_Null ){
992 0 : nByte = -1;
993 0 : break;
994 : }
995 0 : Stringify(pTerm, encoding);
996 0 : nByte += pTerm->n;
997 : }
998 :
999 0 : if( nByte<0 ){
1000 : /* If nByte is less than zero, then there is a NULL value on the stack.
1001 : ** In this case just pop the values off the stack (if required) and
1002 : ** push on a NULL.
1003 : */
1004 0 : if( pOp->p2==0 ){
1005 0 : popStack(&pTos, nField);
1006 : }
1007 0 : pTos++;
1008 0 : pTos->flags = MEM_Null;
1009 : }else{
1010 : /* Otherwise malloc() space for the result and concatenate all the
1011 : ** stack values.
1012 : */
1013 0 : zNew = sqliteMallocRaw( nByte+2 );
1014 0 : if( zNew==0 ) goto no_mem;
1015 0 : j = 0;
1016 0 : pTerm = &pTos[1-nField];
1017 0 : for(i=j=0; i<nField; i++, pTerm++){
1018 0 : int n = pTerm->n;
1019 : assert( pTerm->flags & (MEM_Str|MEM_Blob) );
1020 0 : memcpy(&zNew[j], pTerm->z, n);
1021 0 : j += n;
1022 : }
1023 0 : zNew[j] = 0;
1024 0 : zNew[j+1] = 0;
1025 : assert( j==nByte );
1026 :
1027 0 : if( pOp->p2==0 ){
1028 0 : popStack(&pTos, nField);
1029 : }
1030 0 : pTos++;
1031 0 : pTos->n = j;
1032 0 : pTos->flags = MEM_Str|MEM_Dyn|MEM_Term;
1033 0 : pTos->xDel = 0;
1034 0 : pTos->enc = encoding;
1035 0 : pTos->z = zNew;
1036 : }
1037 0 : break;
1038 : }
1039 :
1040 : /* Opcode: Add * * *
1041 : **
1042 : ** Pop the top two elements from the stack, add them together,
1043 : ** and push the result back onto the stack. If either element
1044 : ** is a string then it is converted to a double using the atof()
1045 : ** function before the addition.
1046 : ** If either operand is NULL, the result is NULL.
1047 : */
1048 : /* Opcode: Multiply * * *
1049 : **
1050 : ** Pop the top two elements from the stack, multiply them together,
1051 : ** and push the result back onto the stack. If either element
1052 : ** is a string then it is converted to a double using the atof()
1053 : ** function before the multiplication.
1054 : ** If either operand is NULL, the result is NULL.
1055 : */
1056 : /* Opcode: Subtract * * *
1057 : **
1058 : ** Pop the top two elements from the stack, subtract the
1059 : ** first (what was on top of the stack) from the second (the
1060 : ** next on stack)
1061 : ** and push the result back onto the stack. If either element
1062 : ** is a string then it is converted to a double using the atof()
1063 : ** function before the subtraction.
1064 : ** If either operand is NULL, the result is NULL.
1065 : */
1066 : /* Opcode: Divide * * *
1067 : **
1068 : ** Pop the top two elements from the stack, divide the
1069 : ** first (what was on top of the stack) from the second (the
1070 : ** next on stack)
1071 : ** and push the result back onto the stack. If either element
1072 : ** is a string then it is converted to a double using the atof()
1073 : ** function before the division. Division by zero returns NULL.
1074 : ** If either operand is NULL, the result is NULL.
1075 : */
1076 : /* Opcode: Remainder * * *
1077 : **
1078 : ** Pop the top two elements from the stack, divide the
1079 : ** first (what was on top of the stack) from the second (the
1080 : ** next on stack)
1081 : ** and push the remainder after division onto the stack. If either element
1082 : ** is a string then it is converted to a double using the atof()
1083 : ** function before the division. Division by zero returns NULL.
1084 : ** If either operand is NULL, the result is NULL.
1085 : */
1086 : case OP_Add: /* same as TK_PLUS, no-push */
1087 : case OP_Subtract: /* same as TK_MINUS, no-push */
1088 : case OP_Multiply: /* same as TK_STAR, no-push */
1089 : case OP_Divide: /* same as TK_SLASH, no-push */
1090 : case OP_Remainder: { /* same as TK_REM, no-push */
1091 0 : Mem *pNos = &pTos[-1];
1092 : int flags;
1093 : assert( pNos>=p->aStack );
1094 0 : flags = pTos->flags | pNos->flags;
1095 0 : if( (flags & MEM_Null)!=0 ){
1096 0 : Release(pTos);
1097 0 : pTos--;
1098 0 : Release(pTos);
1099 0 : pTos->flags = MEM_Null;
1100 0 : }else if( (pTos->flags & pNos->flags & MEM_Int)==MEM_Int ){
1101 : i64 a, b;
1102 0 : a = pTos->u.i;
1103 0 : b = pNos->u.i;
1104 0 : switch( pOp->opcode ){
1105 0 : case OP_Add: b += a; break;
1106 0 : case OP_Subtract: b -= a; break;
1107 0 : case OP_Multiply: b *= a; break;
1108 : case OP_Divide: {
1109 0 : if( a==0 ) goto divide_by_zero;
1110 0 : b /= a;
1111 0 : break;
1112 : }
1113 : default: {
1114 0 : if( a==0 ) goto divide_by_zero;
1115 0 : b %= a;
1116 : break;
1117 : }
1118 : }
1119 0 : Release(pTos);
1120 0 : pTos--;
1121 0 : Release(pTos);
1122 0 : pTos->u.i = b;
1123 0 : pTos->flags = MEM_Int;
1124 : }else{
1125 : double a, b;
1126 0 : a = sqlite3VdbeRealValue(pTos);
1127 0 : b = sqlite3VdbeRealValue(pNos);
1128 0 : switch( pOp->opcode ){
1129 0 : case OP_Add: b += a; break;
1130 0 : case OP_Subtract: b -= a; break;
1131 0 : case OP_Multiply: b *= a; break;
1132 : case OP_Divide: {
1133 0 : if( a==0.0 ) goto divide_by_zero;
1134 0 : b /= a;
1135 0 : break;
1136 : }
1137 : default: {
1138 0 : int ia = (int)a;
1139 0 : int ib = (int)b;
1140 0 : if( ia==0.0 ) goto divide_by_zero;
1141 0 : b = ib % ia;
1142 : break;
1143 : }
1144 : }
1145 0 : Release(pTos);
1146 0 : pTos--;
1147 0 : Release(pTos);
1148 0 : pTos->r = b;
1149 0 : pTos->flags = MEM_Real;
1150 0 : if( (flags & MEM_Real)==0 ){
1151 0 : sqlite3VdbeIntegerAffinity(pTos);
1152 : }
1153 : }
1154 0 : break;
1155 :
1156 0 : divide_by_zero:
1157 0 : Release(pTos);
1158 0 : pTos--;
1159 0 : Release(pTos);
1160 0 : pTos->flags = MEM_Null;
1161 0 : break;
1162 : }
1163 :
1164 : /* Opcode: CollSeq * * P3
1165 : **
1166 : ** P3 is a pointer to a CollSeq struct. If the next call to a user function
1167 : ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
1168 : ** be returned. This is used by the built-in min(), max() and nullif()
1169 : ** functions.
1170 : **
1171 : ** The interface used by the implementation of the aforementioned functions
1172 : ** to retrieve the collation sequence set by this opcode is not available
1173 : ** publicly, only to user functions defined in func.c.
1174 : */
1175 : case OP_CollSeq: { /* no-push */
1176 : assert( pOp->p3type==P3_COLLSEQ );
1177 0 : break;
1178 : }
1179 :
1180 : /* Opcode: Function P1 P2 P3
1181 : **
1182 : ** Invoke a user function (P3 is a pointer to a Function structure that
1183 : ** defines the function) with P2 arguments taken from the stack. Pop all
1184 : ** arguments from the stack and push back the result.
1185 : **
1186 : ** P1 is a 32-bit bitmask indicating whether or not each argument to the
1187 : ** function was determined to be constant at compile time. If the first
1188 : ** argument was constant then bit 0 of P1 is set. This is used to determine
1189 : ** whether meta data associated with a user function argument using the
1190 : ** sqlite3_set_auxdata() API may be safely retained until the next
1191 : ** invocation of this opcode.
1192 : **
1193 : ** See also: AggStep and AggFinal
1194 : */
1195 : case OP_Function: {
1196 : int i;
1197 : Mem *pArg;
1198 : sqlite3_context ctx;
1199 : sqlite3_value **apVal;
1200 2 : int n = pOp->p2;
1201 :
1202 2 : apVal = p->apArg;
1203 : assert( apVal || n==0 );
1204 :
1205 2 : pArg = &pTos[1-n];
1206 4 : for(i=0; i<n; i++, pArg++){
1207 2 : apVal[i] = pArg;
1208 2 : storeTypeInfo(pArg, encoding);
1209 : }
1210 :
1211 : assert( pOp->p3type==P3_FUNCDEF || pOp->p3type==P3_VDBEFUNC );
1212 2 : if( pOp->p3type==P3_FUNCDEF ){
1213 2 : ctx.pFunc = (FuncDef*)pOp->p3;
1214 2 : ctx.pVdbeFunc = 0;
1215 : }else{
1216 0 : ctx.pVdbeFunc = (VdbeFunc*)pOp->p3;
1217 0 : ctx.pFunc = ctx.pVdbeFunc->pFunc;
1218 : }
1219 :
1220 2 : ctx.s.flags = MEM_Null;
1221 2 : ctx.s.z = 0;
1222 2 : ctx.s.xDel = 0;
1223 2 : ctx.isError = 0;
1224 2 : if( ctx.pFunc->needCollSeq ){
1225 : assert( pOp>p->aOp );
1226 : assert( pOp[-1].p3type==P3_COLLSEQ );
1227 : assert( pOp[-1].opcode==OP_CollSeq );
1228 0 : ctx.pColl = (CollSeq *)pOp[-1].p3;
1229 : }
1230 2 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
1231 2 : (*ctx.pFunc->xFunc)(&ctx, n, apVal);
1232 2 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
1233 2 : if( sqlite3MallocFailed() ) goto no_mem;
1234 2 : popStack(&pTos, n);
1235 :
1236 : /* If any auxilary data functions have been called by this user function,
1237 : ** immediately call the destructor for any non-static values.
1238 : */
1239 2 : if( ctx.pVdbeFunc ){
1240 0 : sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
1241 0 : pOp->p3 = (char *)ctx.pVdbeFunc;
1242 0 : pOp->p3type = P3_VDBEFUNC;
1243 : }
1244 :
1245 : /* If the function returned an error, throw an exception */
1246 2 : if( ctx.isError ){
1247 0 : sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
1248 0 : rc = SQLITE_ERROR;
1249 : }
1250 :
1251 : /* Copy the result of the function to the top of the stack */
1252 2 : sqlite3VdbeChangeEncoding(&ctx.s, encoding);
1253 2 : pTos++;
1254 2 : pTos->flags = 0;
1255 2 : sqlite3VdbeMemMove(pTos, &ctx.s);
1256 2 : break;
1257 : }
1258 :
1259 : /* Opcode: BitAnd * * *
1260 : **
1261 : ** Pop the top two elements from the stack. Convert both elements
1262 : ** to integers. Push back onto the stack the bit-wise AND of the
1263 : ** two elements.
1264 : ** If either operand is NULL, the result is NULL.
1265 : */
1266 : /* Opcode: BitOr * * *
1267 : **
1268 : ** Pop the top two elements from the stack. Convert both elements
1269 : ** to integers. Push back onto the stack the bit-wise OR of the
1270 : ** two elements.
1271 : ** If either operand is NULL, the result is NULL.
1272 : */
1273 : /* Opcode: ShiftLeft * * *
1274 : **
1275 : ** Pop the top two elements from the stack. Convert both elements
1276 : ** to integers. Push back onto the stack the second element shifted
1277 : ** left by N bits where N is the top element on the stack.
1278 : ** If either operand is NULL, the result is NULL.
1279 : */
1280 : /* Opcode: ShiftRight * * *
1281 : **
1282 : ** Pop the top two elements from the stack. Convert both elements
1283 : ** to integers. Push back onto the stack the second element shifted
1284 : ** right by N bits where N is the top element on the stack.
1285 : ** If either operand is NULL, the result is NULL.
1286 : */
1287 : case OP_BitAnd: /* same as TK_BITAND, no-push */
1288 : case OP_BitOr: /* same as TK_BITOR, no-push */
1289 : case OP_ShiftLeft: /* same as TK_LSHIFT, no-push */
1290 : case OP_ShiftRight: { /* same as TK_RSHIFT, no-push */
1291 0 : Mem *pNos = &pTos[-1];
1292 : i64 a, b;
1293 :
1294 : assert( pNos>=p->aStack );
1295 0 : if( (pTos->flags | pNos->flags) & MEM_Null ){
1296 0 : popStack(&pTos, 2);
1297 0 : pTos++;
1298 0 : pTos->flags = MEM_Null;
1299 0 : break;
1300 : }
1301 0 : a = sqlite3VdbeIntValue(pNos);
1302 0 : b = sqlite3VdbeIntValue(pTos);
1303 0 : switch( pOp->opcode ){
1304 0 : case OP_BitAnd: a &= b; break;
1305 0 : case OP_BitOr: a |= b; break;
1306 0 : case OP_ShiftLeft: a <<= b; break;
1307 0 : case OP_ShiftRight: a >>= b; break;
1308 : default: /* CANT HAPPEN */ break;
1309 : }
1310 0 : Release(pTos);
1311 0 : pTos--;
1312 0 : Release(pTos);
1313 0 : pTos->u.i = a;
1314 0 : pTos->flags = MEM_Int;
1315 0 : break;
1316 : }
1317 :
1318 : /* Opcode: AddImm P1 * *
1319 : **
1320 : ** Add the value P1 to whatever is on top of the stack. The result
1321 : ** is always an integer.
1322 : **
1323 : ** To force the top of the stack to be an integer, just add 0.
1324 : */
1325 : case OP_AddImm: { /* no-push */
1326 : assert( pTos>=p->aStack );
1327 0 : sqlite3VdbeMemIntegerify(pTos);
1328 0 : pTos->u.i += pOp->p1;
1329 0 : break;
1330 : }
1331 :
1332 : /* Opcode: ForceInt P1 P2 *
1333 : **
1334 : ** Convert the top of the stack into an integer. If the current top of
1335 : ** the stack is not numeric (meaning that is is a NULL or a string that
1336 : ** does not look like an integer or floating point number) then pop the
1337 : ** stack and jump to P2. If the top of the stack is numeric then
1338 : ** convert it into the least integer that is greater than or equal to its
1339 : ** current value if P1==0, or to the least integer that is strictly
1340 : ** greater than its current value if P1==1.
1341 : */
1342 : case OP_ForceInt: { /* no-push */
1343 : i64 v;
1344 : assert( pTos>=p->aStack );
1345 0 : applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
1346 0 : if( (pTos->flags & (MEM_Int|MEM_Real))==0 ){
1347 0 : Release(pTos);
1348 0 : pTos--;
1349 0 : pc = pOp->p2 - 1;
1350 0 : break;
1351 : }
1352 0 : if( pTos->flags & MEM_Int ){
1353 0 : v = pTos->u.i + (pOp->p1!=0);
1354 : }else{
1355 : /* FIX ME: should this not be assert( pTos->flags & MEM_Real ) ??? */
1356 0 : sqlite3VdbeMemRealify(pTos);
1357 0 : v = (int)pTos->r;
1358 0 : if( pTos->r>(double)v ) v++;
1359 0 : if( pOp->p1 && pTos->r==(double)v ) v++;
1360 : }
1361 0 : Release(pTos);
1362 0 : pTos->u.i = v;
1363 0 : pTos->flags = MEM_Int;
1364 0 : break;
1365 : }
1366 :
1367 : /* Opcode: MustBeInt P1 P2 *
1368 : **
1369 : ** Force the top of the stack to be an integer. If the top of the
1370 : ** stack is not an integer and cannot be converted into an integer
1371 : ** with out data loss, then jump immediately to P2, or if P2==0
1372 : ** raise an SQLITE_MISMATCH exception.
1373 : **
1374 : ** If the top of the stack is not an integer and P2 is not zero and
1375 : ** P1 is 1, then the stack is popped. In all other cases, the depth
1376 : ** of the stack is unchanged.
1377 : */
1378 : case OP_MustBeInt: { /* no-push */
1379 : assert( pTos>=p->aStack );
1380 62 : applyAffinity(pTos, SQLITE_AFF_NUMERIC, encoding);
1381 62 : if( (pTos->flags & MEM_Int)==0 ){
1382 0 : if( pOp->p2==0 ){
1383 0 : rc = SQLITE_MISMATCH;
1384 0 : goto abort_due_to_error;
1385 : }else{
1386 0 : if( pOp->p1 ) popStack(&pTos, 1);
1387 0 : pc = pOp->p2 - 1;
1388 : }
1389 : }else{
1390 62 : Release(pTos);
1391 62 : pTos->flags = MEM_Int;
1392 : }
1393 62 : break;
1394 : }
1395 :
1396 : /* Opcode: RealAffinity * * *
1397 : **
1398 : ** If the top of the stack is an integer, convert it to a real value.
1399 : **
1400 : ** This opcode is used when extracting information from a column that
1401 : ** has REAL affinity. Such column values may still be stored as
1402 : ** integers, for space efficiency, but after extraction we want them
1403 : ** to have only a real value.
1404 : */
1405 : case OP_RealAffinity: { /* no-push */
1406 : assert( pTos>=p->aStack );
1407 0 : if( pTos->flags & MEM_Int ){
1408 0 : sqlite3VdbeMemRealify(pTos);
1409 : }
1410 0 : break;
1411 : }
1412 :
1413 : #ifndef SQLITE_OMIT_CAST
1414 : /* Opcode: ToText * * *
1415 : **
1416 : ** Force the value on the top of the stack to be text.
1417 : ** If the value is numeric, convert it to a string using the
1418 : ** equivalent of printf(). Blob values are unchanged and
1419 : ** are afterwards simply interpreted as text.
1420 : **
1421 : ** A NULL value is not changed by this routine. It remains NULL.
1422 : */
1423 : case OP_ToText: { /* same as TK_TO_TEXT, no-push */
1424 : assert( pTos>=p->aStack );
1425 0 : if( pTos->flags & MEM_Null ) break;
1426 : assert( MEM_Str==(MEM_Blob>>3) );
1427 0 : pTos->flags |= (pTos->flags&MEM_Blob)>>3;
1428 0 : applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
1429 : assert( pTos->flags & MEM_Str );
1430 0 : pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Blob);
1431 0 : break;
1432 : }
1433 :
1434 : /* Opcode: ToBlob * * *
1435 : **
1436 : ** Force the value on the top of the stack to be a BLOB.
1437 : ** If the value is numeric, convert it to a string first.
1438 : ** Strings are simply reinterpreted as blobs with no change
1439 : ** to the underlying data.
1440 : **
1441 : ** A NULL value is not changed by this routine. It remains NULL.
1442 : */
1443 : case OP_ToBlob: { /* same as TK_TO_BLOB, no-push */
1444 : assert( pTos>=p->aStack );
1445 0 : if( pTos->flags & MEM_Null ) break;
1446 0 : if( (pTos->flags & MEM_Blob)==0 ){
1447 0 : applyAffinity(pTos, SQLITE_AFF_TEXT, encoding);
1448 : assert( pTos->flags & MEM_Str );
1449 0 : pTos->flags |= MEM_Blob;
1450 : }
1451 0 : pTos->flags &= ~(MEM_Int|MEM_Real|MEM_Str);
1452 0 : break;
1453 : }
1454 :
1455 : /* Opcode: ToNumeric * * *
1456 : **
1457 : ** Force the value on the top of the stack to be numeric (either an
1458 : ** integer or a floating-point number.)
1459 : ** If the value is text or blob, try to convert it to an using the
1460 : ** equivalent of atoi() or atof() and store 0 if no such conversion
1461 : ** is possible.
1462 : **
1463 : ** A NULL value is not changed by this routine. It remains NULL.
1464 : */
1465 : case OP_ToNumeric: { /* same as TK_TO_NUMERIC, no-push */
1466 : assert( pTos>=p->aStack );
1467 0 : if( (pTos->flags & MEM_Null)==0 ){
1468 0 : sqlite3VdbeMemNumerify(pTos);
1469 : }
1470 0 : break;
1471 : }
1472 : #endif /* SQLITE_OMIT_CAST */
1473 :
1474 : /* Opcode: ToInt * * *
1475 : **
1476 : ** Force the value on the top of the stack to be an integer. If
1477 : ** The value is currently a real number, drop its fractional part.
1478 : ** If the value is text or blob, try to convert it to an integer using the
1479 : ** equivalent of atoi() and store 0 if no such conversion is possible.
1480 : **
1481 : ** A NULL value is not changed by this routine. It remains NULL.
1482 : */
1483 : case OP_ToInt: { /* same as TK_TO_INT, no-push */
1484 : assert( pTos>=p->aStack );
1485 0 : if( (pTos->flags & MEM_Null)==0 ){
1486 0 : sqlite3VdbeMemIntegerify(pTos);
1487 : }
1488 0 : break;
1489 : }
1490 :
1491 : #ifndef SQLITE_OMIT_CAST
1492 : /* Opcode: ToReal * * *
1493 : **
1494 : ** Force the value on the top of the stack to be a floating point number.
1495 : ** If The value is currently an integer, convert it.
1496 : ** If the value is text or blob, try to convert it to an integer using the
1497 : ** equivalent of atoi() and store 0 if no such conversion is possible.
1498 : **
1499 : ** A NULL value is not changed by this routine. It remains NULL.
1500 : */
1501 : case OP_ToReal: { /* same as TK_TO_REAL, no-push */
1502 : assert( pTos>=p->aStack );
1503 0 : if( (pTos->flags & MEM_Null)==0 ){
1504 0 : sqlite3VdbeMemRealify(pTos);
1505 : }
1506 0 : break;
1507 : }
1508 : #endif /* SQLITE_OMIT_CAST */
1509 :
1510 : /* Opcode: Eq P1 P2 P3
1511 : **
1512 : ** Pop the top two elements from the stack. If they are equal, then
1513 : ** jump to instruction P2. Otherwise, continue to the next instruction.
1514 : **
1515 : ** If the 0x100 bit of P1 is true and either operand is NULL then take the
1516 : ** jump. If the 0x100 bit of P1 is clear then fall thru if either operand
1517 : ** is NULL.
1518 : **
1519 : ** If the 0x200 bit of P1 is set and either operand is NULL then
1520 : ** both operands are converted to integers prior to comparison.
1521 : ** NULL operands are converted to zero and non-NULL operands are
1522 : ** converted to 1. Thus, for example, with 0x200 set, NULL==NULL is true
1523 : ** whereas it would normally be NULL. Similarly, NULL==123 is false when
1524 : ** 0x200 is set but is NULL when the 0x200 bit of P1 is clear.
1525 : **
1526 : ** The least significant byte of P1 (mask 0xff) must be an affinity character -
1527 : ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
1528 : ** to coerce both values
1529 : ** according to the affinity before the comparison is made. If the byte is
1530 : ** 0x00, then numeric affinity is used.
1531 : **
1532 : ** Once any conversions have taken place, and neither value is NULL,
1533 : ** the values are compared. If both values are blobs, or both are text,
1534 : ** then memcmp() is used to determine the results of the comparison. If
1535 : ** both values are numeric, then a numeric comparison is used. If the
1536 : ** two values are of different types, then they are inequal.
1537 : **
1538 : ** If P2 is zero, do not jump. Instead, push an integer 1 onto the
1539 : ** stack if the jump would have been taken, or a 0 if not. Push a
1540 : ** NULL if either operand was NULL.
1541 : **
1542 : ** If P3 is not NULL it is a pointer to a collating sequence (a CollSeq
1543 : ** structure) that defines how to compare text.
1544 : */
1545 : /* Opcode: Ne P1 P2 P3
1546 : **
1547 : ** This works just like the Eq opcode except that the jump is taken if
1548 : ** the operands from the stack are not equal. See the Eq opcode for
1549 : ** additional information.
1550 : */
1551 : /* Opcode: Lt P1 P2 P3
1552 : **
1553 : ** This works just like the Eq opcode except that the jump is taken if
1554 : ** the 2nd element down on the stack is less than the top of the stack.
1555 : ** See the Eq opcode for additional information.
1556 : */
1557 : /* Opcode: Le P1 P2 P3
1558 : **
1559 : ** This works just like the Eq opcode except that the jump is taken if
1560 : ** the 2nd element down on the stack is less than or equal to the
1561 : ** top of the stack. See the Eq opcode for additional information.
1562 : */
1563 : /* Opcode: Gt P1 P2 P3
1564 : **
1565 : ** This works just like the Eq opcode except that the jump is taken if
1566 : ** the 2nd element down on the stack is greater than the top of the stack.
1567 : ** See the Eq opcode for additional information.
1568 : */
1569 : /* Opcode: Ge P1 P2 P3
1570 : **
1571 : ** This works just like the Eq opcode except that the jump is taken if
1572 : ** the 2nd element down on the stack is greater than or equal to the
1573 : ** top of the stack. See the Eq opcode for additional information.
1574 : */
1575 : case OP_Eq: /* same as TK_EQ, no-push */
1576 : case OP_Ne: /* same as TK_NE, no-push */
1577 : case OP_Lt: /* same as TK_LT, no-push */
1578 : case OP_Le: /* same as TK_LE, no-push */
1579 : case OP_Gt: /* same as TK_GT, no-push */
1580 : case OP_Ge: { /* same as TK_GE, no-push */
1581 : Mem *pNos;
1582 : int flags;
1583 : int res;
1584 : char affinity;
1585 :
1586 151 : pNos = &pTos[-1];
1587 151 : flags = pTos->flags|pNos->flags;
1588 :
1589 : /* If either value is a NULL P2 is not zero, take the jump if the least
1590 : ** significant byte of P1 is true. If P2 is zero, then push a NULL onto
1591 : ** the stack.
1592 : */
1593 151 : if( flags&MEM_Null ){
1594 0 : if( (pOp->p1 & 0x200)!=0 ){
1595 : /* The 0x200 bit of P1 means, roughly "do not treat NULL as the
1596 : ** magic SQL value it normally is - treat it as if it were another
1597 : ** integer".
1598 : **
1599 : ** With 0x200 set, if either operand is NULL then both operands
1600 : ** are converted to integers prior to being passed down into the
1601 : ** normal comparison logic below. NULL operands are converted to
1602 : ** zero and non-NULL operands are converted to 1. Thus, for example,
1603 : ** with 0x200 set, NULL==NULL is true whereas it would normally
1604 : ** be NULL. Similarly, NULL!=123 is true.
1605 : */
1606 0 : sqlite3VdbeMemSetInt64(pTos, (pTos->flags & MEM_Null)==0);
1607 0 : sqlite3VdbeMemSetInt64(pNos, (pNos->flags & MEM_Null)==0);
1608 : }else{
1609 : /* If the 0x200 bit of P1 is clear and either operand is NULL then
1610 : ** the result is always NULL. The jump is taken if the 0x100 bit
1611 : ** of P1 is set.
1612 : */
1613 0 : popStack(&pTos, 2);
1614 0 : if( pOp->p2 ){
1615 0 : if( pOp->p1 & 0x100 ){
1616 0 : pc = pOp->p2-1;
1617 : }
1618 : }else{
1619 0 : pTos++;
1620 0 : pTos->flags = MEM_Null;
1621 : }
1622 0 : break;
1623 : }
1624 : }
1625 :
1626 151 : affinity = pOp->p1 & 0xFF;
1627 151 : if( affinity ){
1628 151 : applyAffinity(pNos, affinity, encoding);
1629 151 : applyAffinity(pTos, affinity, encoding);
1630 : }
1631 :
1632 : assert( pOp->p3type==P3_COLLSEQ || pOp->p3==0 );
1633 151 : res = sqlite3MemCompare(pNos, pTos, (CollSeq*)pOp->p3);
1634 151 : switch( pOp->opcode ){
1635 5 : case OP_Eq: res = res==0; break;
1636 137 : case OP_Ne: res = res!=0; break;
1637 0 : case OP_Lt: res = res<0; break;
1638 9 : case OP_Le: res = res<=0; break;
1639 0 : case OP_Gt: res = res>0; break;
1640 0 : default: res = res>=0; break;
1641 : }
1642 :
1643 151 : popStack(&pTos, 2);
1644 151 : if( pOp->p2 ){
1645 151 : if( res ){
1646 24 : pc = pOp->p2-1;
1647 : }
1648 : }else{
1649 0 : pTos++;
1650 0 : pTos->flags = MEM_Int;
1651 0 : pTos->u.i = res;
1652 : }
1653 151 : break;
1654 : }
1655 :
1656 : /* Opcode: And * * *
1657 : **
1658 : ** Pop two values off the stack. Take the logical AND of the
1659 : ** two values and push the resulting boolean value back onto the
1660 : ** stack.
1661 : */
1662 : /* Opcode: Or * * *
1663 : **
1664 : ** Pop two values off the stack. Take the logical OR of the
1665 : ** two values and push the resulting boolean value back onto the
1666 : ** stack.
1667 : */
1668 : case OP_And: /* same as TK_AND, no-push */
1669 : case OP_Or: { /* same as TK_OR, no-push */
1670 0 : Mem *pNos = &pTos[-1];
1671 : int v1, v2; /* 0==TRUE, 1==FALSE, 2==UNKNOWN or NULL */
1672 :
1673 : assert( pNos>=p->aStack );
1674 0 : if( pTos->flags & MEM_Null ){
1675 0 : v1 = 2;
1676 : }else{
1677 0 : sqlite3VdbeMemIntegerify(pTos);
1678 0 : v1 = pTos->u.i==0;
1679 : }
1680 0 : if( pNos->flags & MEM_Null ){
1681 0 : v2 = 2;
1682 : }else{
1683 0 : sqlite3VdbeMemIntegerify(pNos);
1684 0 : v2 = pNos->u.i==0;
1685 : }
1686 0 : if( pOp->opcode==OP_And ){
1687 : static const unsigned char and_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
1688 0 : v1 = and_logic[v1*3+v2];
1689 : }else{
1690 : static const unsigned char or_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
1691 0 : v1 = or_logic[v1*3+v2];
1692 : }
1693 0 : popStack(&pTos, 2);
1694 0 : pTos++;
1695 0 : if( v1==2 ){
1696 0 : pTos->flags = MEM_Null;
1697 : }else{
1698 0 : pTos->u.i = v1==0;
1699 0 : pTos->flags = MEM_Int;
1700 : }
1701 0 : break;
1702 : }
1703 :
1704 : /* Opcode: Negative * * *
1705 : **
1706 : ** Treat the top of the stack as a numeric quantity. Replace it
1707 : ** with its additive inverse. If the top of the stack is NULL
1708 : ** its value is unchanged.
1709 : */
1710 : /* Opcode: AbsValue * * *
1711 : **
1712 : ** Treat the top of the stack as a numeric quantity. Replace it
1713 : ** with its absolute value. If the top of the stack is NULL
1714 : ** its value is unchanged.
1715 : */
1716 : case OP_Negative: /* same as TK_UMINUS, no-push */
1717 : case OP_AbsValue: {
1718 : assert( pTos>=p->aStack );
1719 0 : if( pTos->flags & MEM_Real ){
1720 0 : neg_abs_real_case:
1721 0 : Release(pTos);
1722 0 : if( pOp->opcode==OP_Negative || pTos->r<0.0 ){
1723 0 : pTos->r = -pTos->r;
1724 : }
1725 0 : pTos->flags = MEM_Real;
1726 0 : }else if( pTos->flags & MEM_Int ){
1727 0 : Release(pTos);
1728 0 : if( pOp->opcode==OP_Negative || pTos->u.i<0 ){
1729 0 : pTos->u.i = -pTos->u.i;
1730 : }
1731 0 : pTos->flags = MEM_Int;
1732 0 : }else if( pTos->flags & MEM_Null ){
1733 : /* Do nothing */
1734 : }else{
1735 0 : sqlite3VdbeMemNumerify(pTos);
1736 0 : goto neg_abs_real_case;
1737 : }
1738 0 : break;
1739 : }
1740 :
1741 : /* Opcode: Not * * *
1742 : **
1743 : ** Interpret the top of the stack as a boolean value. Replace it
1744 : ** with its complement. If the top of the stack is NULL its value
1745 : ** is unchanged.
1746 : */
1747 : case OP_Not: { /* same as TK_NOT, no-push */
1748 : assert( pTos>=p->aStack );
1749 0 : if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1750 0 : sqlite3VdbeMemIntegerify(pTos);
1751 : assert( (pTos->flags & MEM_Dyn)==0 );
1752 0 : pTos->u.i = !pTos->u.i;
1753 0 : pTos->flags = MEM_Int;
1754 0 : break;
1755 : }
1756 :
1757 : /* Opcode: BitNot * * *
1758 : **
1759 : ** Interpret the top of the stack as an value. Replace it
1760 : ** with its ones-complement. If the top of the stack is NULL its
1761 : ** value is unchanged.
1762 : */
1763 : case OP_BitNot: { /* same as TK_BITNOT, no-push */
1764 : assert( pTos>=p->aStack );
1765 0 : if( pTos->flags & MEM_Null ) break; /* Do nothing to NULLs */
1766 0 : sqlite3VdbeMemIntegerify(pTos);
1767 : assert( (pTos->flags & MEM_Dyn)==0 );
1768 0 : pTos->u.i = ~pTos->u.i;
1769 0 : pTos->flags = MEM_Int;
1770 0 : break;
1771 : }
1772 :
1773 : /* Opcode: Noop * * *
1774 : **
1775 : ** Do nothing. This instruction is often useful as a jump
1776 : ** destination.
1777 : */
1778 : /*
1779 : ** The magic Explain opcode are only inserted when explain==2 (which
1780 : ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
1781 : ** This opcode records information from the optimizer. It is the
1782 : ** the same as a no-op. This opcodesnever appears in a real VM program.
1783 : */
1784 : case OP_Explain:
1785 : case OP_Noop: { /* no-push */
1786 50 : break;
1787 : }
1788 :
1789 : /* Opcode: If P1 P2 *
1790 : **
1791 : ** Pop a single boolean from the stack. If the boolean popped is
1792 : ** true, then jump to p2. Otherwise continue to the next instruction.
1793 : ** An integer is false if zero and true otherwise. A string is
1794 : ** false if it has zero length and true otherwise.
1795 : **
1796 : ** If the value popped of the stack is NULL, then take the jump if P1
1797 : ** is true and fall through if P1 is false.
1798 : */
1799 : /* Opcode: IfNot P1 P2 *
1800 : **
1801 : ** Pop a single boolean from the stack. If the boolean popped is
1802 : ** false, then jump to p2. Otherwise continue to the next instruction.
1803 : ** An integer is false if zero and true otherwise. A string is
1804 : ** false if it has zero length and true otherwise.
1805 : **
1806 : ** If the value popped of the stack is NULL, then take the jump if P1
1807 : ** is true and fall through if P1 is false.
1808 : */
1809 : case OP_If: /* no-push */
1810 : case OP_IfNot: { /* no-push */
1811 : int c;
1812 : assert( pTos>=p->aStack );
1813 57 : if( pTos->flags & MEM_Null ){
1814 0 : c = pOp->p1;
1815 : }else{
1816 : #ifdef SQLITE_OMIT_FLOATING_POINT
1817 : c = sqlite3VdbeIntValue(pTos);
1818 : #else
1819 57 : c = sqlite3VdbeRealValue(pTos)!=0.0;
1820 : #endif
1821 57 : if( pOp->opcode==OP_IfNot ) c = !c;
1822 : }
1823 57 : Release(pTos);
1824 57 : pTos--;
1825 57 : if( c ) pc = pOp->p2-1;
1826 57 : break;
1827 : }
1828 :
1829 : /* Opcode: IsNull P1 P2 *
1830 : **
1831 : ** Check the top of the stack and jump to P2 if the top of the stack
1832 : ** is NULL. If P1 is positive, then pop P1 elements from the stack
1833 : ** regardless of whether or not the jump is taken. If P1 is negative,
1834 : ** pop -P1 elements from the stack only if the jump is taken and leave
1835 : ** the stack unchanged if the jump is not taken.
1836 : */
1837 : case OP_IsNull: { /* same as TK_ISNULL, no-push */
1838 58 : if( pTos->flags & MEM_Null ){
1839 6 : pc = pOp->p2-1;
1840 6 : if( pOp->p1<0 ){
1841 3 : popStack(&pTos, -pOp->p1);
1842 : }
1843 : }
1844 58 : if( pOp->p1>0 ){
1845 12 : popStack(&pTos, pOp->p1);
1846 : }
1847 58 : break;
1848 : }
1849 :
1850 : /* Opcode: NotNull P1 P2 *
1851 : **
1852 : ** Jump to P2 if the top abs(P1) values on the stack are all not NULL.
1853 : ** Regardless of whether or not the jump is taken, pop the stack
1854 : ** P1 times if P1 is greater than zero. But if P1 is negative,
1855 : ** leave the stack unchanged.
1856 : */
1857 : case OP_NotNull: { /* same as TK_NOTNULL, no-push */
1858 : int i, cnt;
1859 125 : cnt = pOp->p1;
1860 125 : if( cnt<0 ) cnt = -cnt;
1861 : assert( &pTos[1-cnt] >= p->aStack );
1862 125 : for(i=0; i<cnt && (pTos[1+i-cnt].flags & MEM_Null)==0; i++){}
1863 125 : if( i>=cnt ) pc = pOp->p2-1;
1864 125 : if( pOp->p1>0 ) popStack(&pTos, cnt);
1865 125 : break;
1866 : }
1867 :
1868 : /* Opcode: SetNumColumns P1 P2 *
1869 : **
1870 : ** Before the OP_Column opcode can be executed on a cursor, this
1871 : ** opcode must be called to set the number of fields in the table.
1872 : **
1873 : ** This opcode sets the number of columns for cursor P1 to P2.
1874 : **
1875 : ** If OP_KeyAsData is to be applied to cursor P1, it must be executed
1876 : ** before this op-code.
1877 : */
1878 : case OP_SetNumColumns: { /* no-push */
1879 : Cursor *pC;
1880 : assert( (pOp->p1)<p->nCursor );
1881 : assert( p->apCsr[pOp->p1]!=0 );
1882 586 : pC = p->apCsr[pOp->p1];
1883 586 : pC->nField = pOp->p2;
1884 586 : break;
1885 : }
1886 :
1887 : /* Opcode: Column P1 P2 P3
1888 : **
1889 : ** Interpret the data that cursor P1 points to as a structure built using
1890 : ** the MakeRecord instruction. (See the MakeRecord opcode for additional
1891 : ** information about the format of the data.) Push onto the stack the value
1892 : ** of the P2-th column contained in the data. If there are less that (P2+1)
1893 : ** values in the record, push a NULL onto the stack.
1894 : **
1895 : ** If the KeyAsData opcode has previously executed on this cursor, then the
1896 : ** field might be extracted from the key rather than the data.
1897 : **
1898 : ** If the column contains fewer than P2 fields, then push a NULL. Or
1899 : ** if P3 is of type P3_MEM, then push the P3 value. The P3 value will
1900 : ** be default value for a column that has been added using the ALTER TABLE
1901 : ** ADD COLUMN command. If P3 is an ordinary string, just push a NULL.
1902 : ** When P3 is a string it is really just a comment describing the value
1903 : ** to be pushed, not a default value.
1904 : */
1905 : case OP_Column: {
1906 : u32 payloadSize; /* Number of bytes in the record */
1907 1029 : int p1 = pOp->p1; /* P1 value of the opcode */
1908 1029 : int p2 = pOp->p2; /* column number to retrieve */
1909 1029 : Cursor *pC = 0; /* The VDBE cursor */
1910 : char *zRec; /* Pointer to complete record-data */
1911 : BtCursor *pCrsr; /* The BTree cursor */
1912 : u32 *aType; /* aType[i] holds the numeric type of the i-th column */
1913 : u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */
1914 : u32 nField; /* number of fields in the record */
1915 : int len; /* The length of the serialized data for the column */
1916 : int i; /* Loop counter */
1917 : char *zData; /* Part of the record being decoded */
1918 : Mem sMem; /* For storing the record being decoded */
1919 :
1920 1029 : sMem.flags = 0;
1921 : assert( p1<p->nCursor );
1922 1029 : pTos++;
1923 1029 : pTos->flags = MEM_Null;
1924 :
1925 : /* This block sets the variable payloadSize to be the total number of
1926 : ** bytes in the record.
1927 : **
1928 : ** zRec is set to be the complete text of the record if it is available.
1929 : ** The complete record text is always available for pseudo-tables
1930 : ** If the record is stored in a cursor, the complete record text
1931 : ** might be available in the pC->aRow cache. Or it might not be.
1932 : ** If the data is unavailable, zRec is set to NULL.
1933 : **
1934 : ** We also compute the number of columns in the record. For cursors,
1935 : ** the number of columns is stored in the Cursor.nField element. For
1936 : ** records on the stack, the next entry down on the stack is an integer
1937 : ** which is the number of records.
1938 : */
1939 1029 : pC = p->apCsr[p1];
1940 : #ifndef SQLITE_OMIT_VIRTUALTABLE
1941 : assert( pC->pVtabCursor==0 );
1942 : #endif
1943 : assert( pC!=0 );
1944 1029 : if( pC->pCursor!=0 ){
1945 : /* The record is stored in a B-Tree */
1946 1029 : rc = sqlite3VdbeCursorMoveto(pC);
1947 1029 : if( rc ) goto abort_due_to_error;
1948 1029 : zRec = 0;
1949 1029 : pCrsr = pC->pCursor;
1950 1029 : if( pC->nullRow ){
1951 9 : payloadSize = 0;
1952 1020 : }else if( pC->cacheStatus==p->cacheCtr ){
1953 596 : payloadSize = pC->payloadSize;
1954 596 : zRec = (char*)pC->aRow;
1955 424 : }else if( pC->isIndex ){
1956 : i64 payloadSize64;
1957 4 : sqlite3BtreeKeySize(pCrsr, &payloadSize64);
1958 4 : payloadSize = payloadSize64;
1959 : }else{
1960 420 : sqlite3BtreeDataSize(pCrsr, &payloadSize);
1961 : }
1962 1029 : nField = pC->nField;
1963 0 : }else if( pC->pseudoTable ){
1964 : /* The record is the sole entry of a pseudo-table */
1965 0 : payloadSize = pC->nData;
1966 0 : zRec = pC->pData;
1967 0 : pC->cacheStatus = CACHE_STALE;
1968 : assert( payloadSize==0 || zRec!=0 );
1969 0 : nField = pC->nField;
1970 0 : pCrsr = 0;
1971 : }else{
1972 0 : zRec = 0;
1973 0 : payloadSize = 0;
1974 0 : pCrsr = 0;
1975 0 : nField = 0;
1976 : }
1977 :
1978 : /* If payloadSize is 0, then just push a NULL onto the stack. */
1979 1029 : if( payloadSize==0 ){
1980 : assert( pTos->flags==MEM_Null );
1981 9 : break;
1982 : }
1983 :
1984 : assert( p2<nField );
1985 :
1986 : /* Read and parse the table header. Store the results of the parse
1987 : ** into the record header cache fields of the cursor.
1988 : */
1989 1616 : if( pC && pC->cacheStatus==p->cacheCtr ){
1990 596 : aType = pC->aType;
1991 596 : aOffset = pC->aOffset;
1992 : }else{
1993 : u8 *zIdx; /* Index into header */
1994 : u8 *zEndHdr; /* Pointer to first byte after the header */
1995 : u32 offset; /* Offset into the data */
1996 : int szHdrSz; /* Size of the header size field at start of record */
1997 : int avail; /* Number of bytes of available data */
1998 :
1999 424 : aType = pC->aType;
2000 424 : if( aType==0 ){
2001 191 : pC->aType = aType = sqliteMallocRaw( 2*nField*sizeof(aType) );
2002 : }
2003 424 : if( aType==0 ){
2004 0 : goto no_mem;
2005 : }
2006 424 : pC->aOffset = aOffset = &aType[nField];
2007 424 : pC->payloadSize = payloadSize;
2008 424 : pC->cacheStatus = p->cacheCtr;
2009 :
2010 : /* Figure out how many bytes are in the header */
2011 424 : if( zRec ){
2012 0 : zData = zRec;
2013 : }else{
2014 424 : if( pC->isIndex ){
2015 4 : zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
2016 : }else{
2017 420 : zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
2018 : }
2019 : /* If KeyFetch()/DataFetch() managed to get the entire payload,
2020 : ** save the payload in the pC->aRow cache. That will save us from
2021 : ** having to make additional calls to fetch the content portion of
2022 : ** the record.
2023 : */
2024 424 : if( avail>=payloadSize ){
2025 424 : zRec = zData;
2026 424 : pC->aRow = (u8*)zData;
2027 : }else{
2028 0 : pC->aRow = 0;
2029 : }
2030 : }
2031 : /* The following assert is true in all cases accept when
2032 : ** the database file has been corrupted externally.
2033 : ** assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
2034 424 : szHdrSz = GetVarint((u8*)zData, offset);
2035 :
2036 : /* The KeyFetch() or DataFetch() above are fast and will get the entire
2037 : ** record header in most cases. But they will fail to get the complete
2038 : ** record header if the record header does not fit on a single page
2039 : ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
2040 : ** acquire the complete header text.
2041 : */
2042 424 : if( !zRec && avail<offset ){
2043 0 : rc = sqlite3VdbeMemFromBtree(pCrsr, 0, offset, pC->isIndex, &sMem);
2044 0 : if( rc!=SQLITE_OK ){
2045 0 : goto op_column_out;
2046 : }
2047 0 : zData = sMem.z;
2048 : }
2049 424 : zEndHdr = (u8 *)&zData[offset];
2050 424 : zIdx = (u8 *)&zData[szHdrSz];
2051 :
2052 : /* Scan the header and use it to fill in the aType[] and aOffset[]
2053 : ** arrays. aType[i] will contain the type integer for the i-th
2054 : ** column and aOffset[i] will contain the offset from the beginning
2055 : ** of the record to the start of the data for the i-th column
2056 : */
2057 1666 : for(i=0; i<nField; i++){
2058 1242 : if( zIdx<zEndHdr ){
2059 1242 : aOffset[i] = offset;
2060 1242 : zIdx += GetVarint(zIdx, aType[i]);
2061 1242 : offset += sqlite3VdbeSerialTypeLen(aType[i]);
2062 : }else{
2063 : /* If i is less that nField, then there are less fields in this
2064 : ** record than SetNumColumns indicated there are columns in the
2065 : ** table. Set the offset for any extra columns not present in
2066 : ** the record to 0. This tells code below to push a NULL onto the
2067 : ** stack instead of deserializing a value from the record.
2068 : */
2069 0 : aOffset[i] = 0;
2070 : }
2071 : }
2072 424 : Release(&sMem);
2073 424 : sMem.flags = MEM_Null;
2074 :
2075 : /* If we have read more header data than was contained in the header,
2076 : ** or if the end of the last field appears to be past the end of the
2077 : ** record, then we must be dealing with a corrupt database.
2078 : */
2079 424 : if( zIdx>zEndHdr || offset>payloadSize ){
2080 0 : rc = SQLITE_CORRUPT_BKPT;
2081 0 : goto op_column_out;
2082 : }
2083 : }
2084 :
2085 : /* Get the column information. If aOffset[p2] is non-zero, then
2086 : ** deserialize the value from the record. If aOffset[p2] is zero,
2087 : ** then there are not enough fields in the record to satisfy the
2088 : ** request. In this case, set the value NULL or to P3 if P3 is
2089 : ** a pointer to a Mem object.
2090 : */
2091 1020 : if( aOffset[p2] ){
2092 : assert( rc==SQLITE_OK );
2093 1020 : if( zRec ){
2094 1020 : zData = &zRec[aOffset[p2]];
2095 : }else{
2096 0 : len = sqlite3VdbeSerialTypeLen(aType[p2]);
2097 0 : rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex,&sMem);
2098 0 : if( rc!=SQLITE_OK ){
2099 0 : goto op_column_out;
2100 : }
2101 0 : zData = sMem.z;
2102 : }
2103 1020 : sqlite3VdbeSerialGet((u8*)zData, aType[p2], pTos);
2104 1020 : pTos->enc = encoding;
2105 : }else{
2106 0 : if( pOp->p3type==P3_MEM ){
2107 0 : sqlite3VdbeMemShallowCopy(pTos, (Mem *)(pOp->p3), MEM_Static);
2108 : }else{
2109 0 : pTos->flags = MEM_Null;
2110 : }
2111 : }
2112 :
2113 : /* If we dynamically allocated space to hold the data (in the
2114 : ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
2115 : ** dynamically allocated space over to the pTos structure.
2116 : ** This prevents a memory copy.
2117 : */
2118 1020 : if( (sMem.flags & MEM_Dyn)!=0 ){
2119 : assert( pTos->flags & MEM_Ephem );
2120 : assert( pTos->flags & (MEM_Str|MEM_Blob) );
2121 : assert( pTos->z==sMem.z );
2122 : assert( sMem.flags & MEM_Term );
2123 0 : pTos->flags &= ~MEM_Ephem;
2124 0 : pTos->flags |= MEM_Dyn|MEM_Term;
2125 : }
2126 :
2127 : /* pTos->z might be pointing to sMem.zShort[]. Fix that so that we
2128 : ** can abandon sMem */
2129 1020 : rc = sqlite3VdbeMemMakeWriteable(pTos);
2130 :
2131 1020 : op_column_out:
2132 1020 : break;
2133 : }
2134 :
2135 : /* Opcode: MakeRecord P1 P2 P3
2136 : **
2137 : ** Convert the top abs(P1) entries of the stack into a single entry
2138 : ** suitable for use as a data record in a database table or as a key
2139 : ** in an index. The details of the format are irrelavant as long as
2140 : ** the OP_Column opcode can decode the record later and as long as the
2141 : ** sqlite3VdbeRecordCompare function will correctly compare two encoded
2142 : ** records. Refer to source code comments for the details of the record
2143 : ** format.
2144 : **
2145 : ** The original stack entries are popped from the stack if P1>0 but
2146 : ** remain on the stack if P1<0.
2147 : **
2148 : ** If P2 is not zero and one or more of the entries are NULL, then jump
2149 : ** to the address given by P2. This feature can be used to skip a
2150 : ** uniqueness test on indices.
2151 : **
2152 : ** P3 may be a string that is P1 characters long. The nth character of the
2153 : ** string indicates the column affinity that should be used for the nth
2154 : ** field of the index key (i.e. the first character of P3 corresponds to the
2155 : ** lowest element on the stack).
2156 : **
2157 : ** The mapping from character to affinity is given by the SQLITE_AFF_
2158 : ** macros defined in sqliteInt.h.
2159 : **
2160 : ** If P3 is NULL then all index fields have the affinity NONE.
2161 : **
2162 : ** See also OP_MakeIdxRec
2163 : */
2164 : /* Opcode: MakeIdxRec P1 P2 P3
2165 : **
2166 : ** This opcode works just OP_MakeRecord except that it reads an extra
2167 : ** integer from the stack (thus reading a total of abs(P1+1) entries)
2168 : ** and appends that extra integer to the end of the record as a varint.
2169 : ** This results in an index key.
2170 : */
2171 : case OP_MakeIdxRec:
2172 : case OP_MakeRecord: {
2173 : /* Assuming the record contains N fields, the record format looks
2174 : ** like this:
2175 : **
2176 : ** ------------------------------------------------------------------------
2177 : ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
2178 : ** ------------------------------------------------------------------------
2179 : **
2180 : ** Data(0) is taken from the lowest element of the stack and data(N-1) is
2181 : ** the top of the stack.
2182 : **
2183 : ** Each type field is a varint representing the serial type of the
2184 : ** corresponding data element (see sqlite3VdbeSerialType()). The
2185 : ** hdr-size field is also a varint which is the offset from the beginning
2186 : ** of the record to data0.
2187 : */
2188 : unsigned char *zNewRecord;
2189 : unsigned char *zCsr;
2190 : Mem *pRec;
2191 408 : Mem *pRowid = 0;
2192 408 : int nData = 0; /* Number of bytes of data space */
2193 408 : int nHdr = 0; /* Number of bytes of header space */
2194 408 : int nByte = 0; /* Space required for this record */
2195 : int nVarint; /* Number of bytes in a varint */
2196 : u32 serial_type; /* Type field */
2197 408 : int containsNull = 0; /* True if any of the data fields are NULL */
2198 : char zTemp[NBFS]; /* Space to hold small records */
2199 : Mem *pData0;
2200 :
2201 : int leaveOnStack; /* If true, leave the entries on the stack */
2202 : int nField; /* Number of fields in the record */
2203 : int jumpIfNull; /* Jump here if non-zero and any entries are NULL. */
2204 : int addRowid; /* True to append a rowid column at the end */
2205 : char *zAffinity; /* The affinity string for the record */
2206 : int file_format; /* File format to use for encoding */
2207 :
2208 408 : leaveOnStack = ((pOp->p1<0)?1:0);
2209 408 : nField = pOp->p1 * (leaveOnStack?-1:1);
2210 408 : jumpIfNull = pOp->p2;
2211 408 : addRowid = pOp->opcode==OP_MakeIdxRec;
2212 408 : zAffinity = pOp->p3;
2213 :
2214 408 : pData0 = &pTos[1-nField];
2215 : assert( pData0>=p->aStack );
2216 408 : containsNull = 0;
2217 408 : file_format = p->minWriteFileFormat;
2218 :
2219 : /* Loop through the elements that will make up the record to figure
2220 : ** out how much space is required for the new record.
2221 : */
2222 1421 : for(pRec=pData0; pRec<=pTos; pRec++){
2223 1013 : if( zAffinity ){
2224 1013 : applyAffinity(pRec, zAffinity[pRec-pData0], encoding);
2225 : }
2226 1013 : if( pRec->flags&MEM_Null ){
2227 53 : containsNull = 1;
2228 : }
2229 1013 : serial_type = sqlite3VdbeSerialType(pRec, file_format);
2230 1013 : nData += sqlite3VdbeSerialTypeLen(serial_type);
2231 1013 : nHdr += sqlite3VarintLen(serial_type);
2232 : }
2233 :
2234 : /* If we have to append a varint rowid to this record, set 'rowid'
2235 : ** to the value of the rowid and increase nByte by the amount of space
2236 : ** required to store it and the 0x00 seperator byte.
2237 : */
2238 408 : if( addRowid ){
2239 121 : pRowid = &pTos[0-nField];
2240 : assert( pRowid>=p->aStack );
2241 121 : sqlite3VdbeMemIntegerify(pRowid);
2242 121 : serial_type = sqlite3VdbeSerialType(pRowid, 0);
2243 121 : nData += sqlite3VdbeSerialTypeLen(serial_type);
2244 121 : nHdr += sqlite3VarintLen(serial_type);
2245 : }
2246 :
2247 : /* Add the initial header varint and total the size */
2248 408 : nHdr += nVarint = sqlite3VarintLen(nHdr);
2249 408 : if( nVarint<sqlite3VarintLen(nHdr) ){
2250 0 : nHdr++;
2251 : }
2252 408 : nByte = nHdr+nData;
2253 :
2254 : /* Allocate space for the new record. */
2255 408 : if( nByte>sizeof(zTemp) ){
2256 100 : zNewRecord = sqliteMallocRaw(nByte);
2257 100 : if( !zNewRecord ){
2258 0 : goto no_mem;
2259 : }
2260 : }else{
2261 308 : zNewRecord = (u8*)zTemp;
2262 : }
2263 :
2264 : /* Write the record */
2265 408 : zCsr = zNewRecord;
2266 408 : zCsr += sqlite3PutVarint(zCsr, nHdr);
2267 1421 : for(pRec=pData0; pRec<=pTos; pRec++){
2268 1013 : serial_type = sqlite3VdbeSerialType(pRec, file_format);
2269 1013 : zCsr += sqlite3PutVarint(zCsr, serial_type); /* serial type */
2270 : }
2271 408 : if( addRowid ){
2272 121 : zCsr += sqlite3PutVarint(zCsr, sqlite3VdbeSerialType(pRowid, 0));
2273 : }
2274 1421 : for(pRec=pData0; pRec<=pTos; pRec++){
2275 1013 : zCsr += sqlite3VdbeSerialPut(zCsr, pRec, file_format); /* serial data */
2276 : }
2277 408 : if( addRowid ){
2278 121 : zCsr += sqlite3VdbeSerialPut(zCsr, pRowid, 0);
2279 : }
2280 : assert( zCsr==(zNewRecord+nByte) );
2281 :
2282 : /* Pop entries off the stack if required. Push the new record on. */
2283 408 : if( !leaveOnStack ){
2284 408 : popStack(&pTos, nField+addRowid);
2285 : }
2286 408 : pTos++;
2287 408 : pTos->n = nByte;
2288 408 : if( nByte<=sizeof(zTemp) ){
2289 : assert( zNewRecord==(unsigned char *)zTemp );
2290 308 : pTos->z = pTos->zShort;
2291 308 : memcpy(pTos->zShort, zTemp, nByte);
2292 308 : pTos->flags = MEM_Blob | MEM_Short;
2293 : }else{
2294 : assert( zNewRecord!=(unsigned char *)zTemp );
2295 100 : pTos->z = (char*)zNewRecord;
2296 100 : pTos->flags = MEM_Blob | MEM_Dyn;
2297 100 : pTos->xDel = 0;
2298 : }
2299 408 : pTos->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
2300 :
2301 : /* If a NULL was encountered and jumpIfNull is non-zero, take the jump. */
2302 408 : if( jumpIfNull && containsNull ){
2303 0 : pc = jumpIfNull - 1;
2304 : }
2305 408 : break;
2306 : }
2307 :
2308 : /* Opcode: Statement P1 * *
2309 : **
2310 : ** Begin an individual statement transaction which is part of a larger
2311 : ** BEGIN..COMMIT transaction. This is needed so that the statement
2312 : ** can be rolled back after an error without having to roll back the
2313 : ** entire transaction. The statement transaction will automatically
2314 : ** commit when the VDBE halts.
2315 : **
2316 : ** The statement is begun on the database file with index P1. The main
2317 : ** database file has an index of 0 and the file used for temporary tables
2318 : ** has an index of 1.
2319 : */
2320 : case OP_Statement: { /* no-push */
2321 0 : int i = pOp->p1;
2322 : Btree *pBt;
2323 0 : if( i>=0 && i<db->nDb && (pBt = db->aDb[i].pBt)!=0 && !(db->autoCommit) ){
2324 : assert( sqlite3BtreeIsInTrans(pBt) );
2325 0 : if( !sqlite3BtreeIsInStmt(pBt) ){
2326 0 : rc = sqlite3BtreeBeginStmt(pBt);
2327 : }
2328 : }
2329 0 : break;
2330 : }
2331 :
2332 : /* Opcode: AutoCommit P1 P2 *
2333 : **
2334 : ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
2335 : ** back any currently active btree transactions. If there are any active
2336 : ** VMs (apart from this one), then the COMMIT or ROLLBACK statement fails.
2337 : **
2338 : ** This instruction causes the VM to halt.
2339 : */
2340 : case OP_AutoCommit: { /* no-push */
2341 10 : u8 i = pOp->p1;
2342 10 : u8 rollback = pOp->p2;
2343 :
2344 : assert( i==1 || i==0 );
2345 : assert( i==1 || rollback==0 );
2346 :
2347 : assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
2348 :
2349 10 : if( db->activeVdbeCnt>1 && i && !db->autoCommit ){
2350 : /* If this instruction implements a COMMIT or ROLLBACK, other VMs are
2351 : ** still running, and a transaction is active, return an error indicating
2352 : ** that the other VMs must complete first.
2353 : */
2354 0 : sqlite3SetString(&p->zErrMsg, "cannot ", rollback?"rollback":"commit",
2355 : " transaction - SQL statements in progress", (char*)0);
2356 0 : rc = SQLITE_ERROR;
2357 10 : }else if( i!=db->autoCommit ){
2358 10 : if( pOp->p2 ){
2359 : assert( i==1 );
2360 3 : sqlite3RollbackAll(db);
2361 3 : db->autoCommit = 1;
2362 : }else{
2363 7 : db->autoCommit = i;
2364 7 : if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
2365 0 : p->pTos = pTos;
2366 0 : p->pc = pc;
2367 0 : db->autoCommit = 1-i;
2368 0 : p->rc = SQLITE_BUSY;
2369 0 : return SQLITE_BUSY;
2370 : }
2371 : }
2372 10 : if( p->rc==SQLITE_OK ){
2373 10 : return SQLITE_DONE;
2374 : }else{
2375 0 : return SQLITE_ERROR;
2376 : }
2377 : }else{
2378 0 : sqlite3SetString(&p->zErrMsg,
2379 : (!i)?"cannot start a transaction within a transaction":(
2380 : (rollback)?"cannot rollback - no transaction is active":
2381 : "cannot commit - no transaction is active"), (char*)0);
2382 :
2383 0 : rc = SQLITE_ERROR;
2384 : }
2385 0 : break;
2386 : }
2387 :
2388 : /* Opcode: Transaction P1 P2 *
2389 : **
2390 : ** Begin a transaction. The transaction ends when a Commit or Rollback
2391 : ** opcode is encountered. Depending on the ON CONFLICT setting, the
2392 : ** transaction might also be rolled back if an error is encountered.
2393 : **
2394 : ** P1 is the index of the database file on which the transaction is
2395 : ** started. Index 0 is the main database file and index 1 is the
2396 : ** file used for temporary tables.
2397 : **
2398 : ** If P2 is non-zero, then a write-transaction is started. A RESERVED lock is
2399 : ** obtained on the database file when a write-transaction is started. No
2400 : ** other process can start another write transaction while this transaction is
2401 : ** underway. Starting a write transaction also creates a rollback journal. A
2402 : ** write transaction must be started before any changes can be made to the
2403 : ** database. If P2 is 2 or greater then an EXCLUSIVE lock is also obtained
2404 : ** on the file.
2405 : **
2406 : ** If P2 is zero, then a read-lock is obtained on the database file.
2407 : */
2408 : case OP_Transaction: { /* no-push */
2409 404 : int i = pOp->p1;
2410 : Btree *pBt;
2411 :
2412 : assert( i>=0 && i<db->nDb );
2413 404 : pBt = db->aDb[i].pBt;
2414 :
2415 404 : if( pBt ){
2416 404 : rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
2417 404 : if( rc==SQLITE_BUSY ){
2418 0 : p->pc = pc;
2419 0 : p->rc = SQLITE_BUSY;
2420 0 : p->pTos = pTos;
2421 0 : return SQLITE_BUSY;
2422 : }
2423 404 : if( rc!=SQLITE_OK && rc!=SQLITE_READONLY /* && rc!=SQLITE_BUSY */ ){
2424 0 : goto abort_due_to_error;
2425 : }
2426 : }
2427 404 : break;
2428 : }
2429 :
2430 : /* Opcode: ReadCookie P1 P2 *
2431 : **
2432 : ** Read cookie number P2 from database P1 and push it onto the stack.
2433 : ** P2==0 is the schema version. P2==1 is the database format.
2434 : ** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2435 : ** the main database file and P1==1 is the database file used to store
2436 : ** temporary tables.
2437 : **
2438 : ** There must be a read-lock on the database (either a transaction
2439 : ** must be started or there must be an open cursor) before
2440 : ** executing this instruction.
2441 : */
2442 : case OP_ReadCookie: {
2443 : int iMeta;
2444 : assert( pOp->p2<SQLITE_N_BTREE_META );
2445 : assert( pOp->p1>=0 && pOp->p1<db->nDb );
2446 : assert( db->aDb[pOp->p1].pBt!=0 );
2447 : /* The indexing of meta values at the schema layer is off by one from
2448 : ** the indexing in the btree layer. The btree considers meta[0] to
2449 : ** be the number of free pages in the database (a read-only value)
2450 : ** and meta[1] to be the schema cookie. The schema layer considers
2451 : ** meta[1] to be the schema cookie. So we have to shift the index
2452 : ** by one in the following statement.
2453 : */
2454 57 : rc = sqlite3BtreeGetMeta(db->aDb[pOp->p1].pBt, 1 + pOp->p2, (u32 *)&iMeta);
2455 57 : pTos++;
2456 57 : pTos->u.i = iMeta;
2457 57 : pTos->flags = MEM_Int;
2458 57 : break;
2459 : }
2460 :
2461 : /* Opcode: SetCookie P1 P2 *
2462 : **
2463 : ** Write the top of the stack into cookie number P2 of database P1.
2464 : ** P2==0 is the schema version. P2==1 is the database format.
2465 : ** P2==2 is the recommended pager cache size, and so forth. P1==0 is
2466 : ** the main database file and P1==1 is the database file used to store
2467 : ** temporary tables.
2468 : **
2469 : ** A transaction must be started before executing this opcode.
2470 : */
2471 : case OP_SetCookie: { /* no-push */
2472 : Db *pDb;
2473 : assert( pOp->p2<SQLITE_N_BTREE_META );
2474 : assert( pOp->p1>=0 && pOp->p1<db->nDb );
2475 161 : pDb = &db->aDb[pOp->p1];
2476 : assert( pDb->pBt!=0 );
2477 : assert( pTos>=p->aStack );
2478 161 : sqlite3VdbeMemIntegerify(pTos);
2479 : /* See note about index shifting on OP_ReadCookie */
2480 161 : rc = sqlite3BtreeUpdateMeta(pDb->pBt, 1+pOp->p2, (int)pTos->u.i);
2481 161 : if( pOp->p2==0 ){
2482 : /* When the schema cookie changes, record the new cookie internally */
2483 61 : pDb->pSchema->schema_cookie = pTos->u.i;
2484 61 : db->flags |= SQLITE_InternChanges;
2485 100 : }else if( pOp->p2==1 ){
2486 : /* Record changes in the file format */
2487 50 : pDb->pSchema->file_format = pTos->u.i;
2488 : }
2489 : assert( (pTos->flags & MEM_Dyn)==0 );
2490 161 : pTos--;
2491 161 : if( pOp->p1==1 ){
2492 : /* Invalidate all prepared statements whenever the TEMP database
2493 : ** schema is changed. Ticket #1644 */
2494 0 : sqlite3ExpirePreparedStatements(db);
2495 : }
2496 161 : break;
2497 : }
2498 :
2499 : /* Opcode: VerifyCookie P1 P2 *
2500 : **
2501 : ** Check the value of global database parameter number 0 (the
2502 : ** schema version) and make sure it is equal to P2.
2503 : ** P1 is the database number which is 0 for the main database file
2504 : ** and 1 for the file holding temporary tables and some higher number
2505 : ** for auxiliary databases.
2506 : **
2507 : ** The cookie changes its value whenever the database schema changes.
2508 : ** This operation is used to detect when that the cookie has changed
2509 : ** and that the current process needs to reread the schema.
2510 : **
2511 : ** Either a transaction needs to have been started or an OP_Open needs
2512 : ** to be executed (to establish a read lock) before this opcode is
2513 : ** invoked.
2514 : */
2515 : case OP_VerifyCookie: { /* no-push */
2516 : int iMeta;
2517 : Btree *pBt;
2518 : assert( pOp->p1>=0 && pOp->p1<db->nDb );
2519 404 : pBt = db->aDb[pOp->p1].pBt;
2520 404 : if( pBt ){
2521 404 : rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&iMeta);
2522 : }else{
2523 0 : rc = SQLITE_OK;
2524 0 : iMeta = 0;
2525 : }
2526 404 : if( rc==SQLITE_OK && iMeta!=pOp->p2 ){
2527 0 : sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0);
2528 : /* If the schema-cookie from the database file matches the cookie
2529 : ** stored with the in-memory representation of the schema, do
2530 : ** not reload the schema from the database file.
2531 : **
2532 : ** If virtual-tables are in use, this is not just an optimisation.
2533 : ** Often, v-tables store their data in other SQLite tables, which
2534 : ** are queried from within xNext() and other v-table methods using
2535 : ** prepared queries. If such a query is out-of-date, we do not want to
2536 : ** discard the database schema, as the user code implementing the
2537 : ** v-table would have to be ready for the sqlite3_vtab structure itself
2538 : ** to be invalidated whenever sqlite3_step() is called from within
2539 : ** a v-table method.
2540 : */
2541 0 : if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
2542 0 : sqlite3ResetInternalSchema(db, pOp->p1);
2543 : }
2544 :
2545 0 : sqlite3ExpirePreparedStatements(db);
2546 0 : rc = SQLITE_SCHEMA;
2547 : }
2548 404 : break;
2549 : }
2550 :
2551 : /* Opcode: OpenRead P1 P2 P3
2552 : **
2553 : ** Open a read-only cursor for the database table whose root page is
2554 : ** P2 in a database file. The database file is determined by an
2555 : ** integer from the top of the stack. 0 means the main database and
2556 : ** 1 means the database used for temporary tables. Give the new
2557 : ** cursor an identifier of P1. The P1 values need not be contiguous
2558 : ** but all P1 values should be small integers. It is an error for
2559 : ** P1 to be negative.
2560 : **
2561 : ** If P2==0 then take the root page number from the next of the stack.
2562 : **
2563 : ** There will be a read lock on the database whenever there is an
2564 : ** open cursor. If the database was unlocked prior to this instruction
2565 : ** then a read lock is acquired as part of this instruction. A read
2566 : ** lock allows other processes to read the database but prohibits
2567 : ** any other process from modifying the database. The read lock is
2568 : ** released when all cursors are closed. If this instruction attempts
2569 : ** to get a read lock but fails, the script terminates with an
2570 : ** SQLITE_BUSY error code.
2571 : **
2572 : ** The P3 value is a pointer to a KeyInfo structure that defines the
2573 : ** content and collating sequence of indices. P3 is NULL for cursors
2574 : ** that are not pointing to indices.
2575 : **
2576 : ** See also OpenWrite.
2577 : */
2578 : /* Opcode: OpenWrite P1 P2 P3
2579 : **
2580 : ** Open a read/write cursor named P1 on the table or index whose root
2581 : ** page is P2. If P2==0 then take the root page number from the stack.
2582 : **
2583 : ** The P3 value is a pointer to a KeyInfo structure that defines the
2584 : ** content and collating sequence of indices. P3 is NULL for cursors
2585 : ** that are not pointing to indices.
2586 : **
2587 : ** This instruction works just like OpenRead except that it opens the cursor
2588 : ** in read/write mode. For a given table, there can be one or more read-only
2589 : ** cursors or a single read/write cursor but not both.
2590 : **
2591 : ** See also OpenRead.
2592 : */
2593 : case OP_OpenRead: /* no-push */
2594 : case OP_OpenWrite: { /* no-push */
2595 730 : int i = pOp->p1;
2596 730 : int p2 = pOp->p2;
2597 : int wrFlag;
2598 : Btree *pX;
2599 : int iDb;
2600 : Cursor *pCur;
2601 : Db *pDb;
2602 :
2603 : assert( pTos>=p->aStack );
2604 730 : sqlite3VdbeMemIntegerify(pTos);
2605 730 : iDb = pTos->u.i;
2606 : assert( (pTos->flags & MEM_Dyn)==0 );
2607 730 : pTos--;
2608 : assert( iDb>=0 && iDb<db->nDb );
2609 730 : pDb = &db->aDb[iDb];
2610 730 : pX = pDb->pBt;
2611 : assert( pX!=0 );
2612 730 : if( pOp->opcode==OP_OpenWrite ){
2613 431 : wrFlag = 1;
2614 431 : if( pDb->pSchema->file_format < p->minWriteFileFormat ){
2615 210 : p->minWriteFileFormat = pDb->pSchema->file_format;
2616 : }
2617 : }else{
2618 299 : wrFlag = 0;
2619 : }
2620 730 : if( p2<=0 ){
2621 : assert( pTos>=p->aStack );
2622 0 : sqlite3VdbeMemIntegerify(pTos);
2623 0 : p2 = pTos->u.i;
2624 : assert( (pTos->flags & MEM_Dyn)==0 );
2625 0 : pTos--;
2626 : assert( p2>=2 );
2627 : }
2628 : assert( i>=0 );
2629 730 : pCur = allocateCursor(p, i, iDb);
2630 730 : if( pCur==0 ) goto no_mem;
2631 730 : pCur->nullRow = 1;
2632 730 : if( pX==0 ) break;
2633 : /* We always provide a key comparison function. If the table being
2634 : ** opened is of type INTKEY, the comparision function will be ignored. */
2635 730 : rc = sqlite3BtreeCursor(pX, p2, wrFlag,
2636 : sqlite3VdbeRecordCompare, pOp->p3,
2637 : &pCur->pCursor);
2638 730 : if( pOp->p3type==P3_KEYINFO ){
2639 155 : pCur->pKeyInfo = (KeyInfo*)pOp->p3;
2640 155 : pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
2641 155 : pCur->pKeyInfo->enc = ENC(p->db);
2642 : }else{
2643 575 : pCur->pKeyInfo = 0;
2644 575 : pCur->pIncrKey = &pCur->bogusIncrKey;
2645 : }
2646 730 : switch( rc ){
2647 : case SQLITE_BUSY: {
2648 0 : p->pc = pc;
2649 0 : p->rc = SQLITE_BUSY;
2650 0 : p->pTos = &pTos[1 + (pOp->p2<=0)]; /* Operands must remain on stack */
2651 0 : return SQLITE_BUSY;
2652 : }
2653 : case SQLITE_OK: {
2654 730 : int flags = sqlite3BtreeFlags(pCur->pCursor);
2655 : /* Sanity checking. Only the lower four bits of the flags byte should
2656 : ** be used. Bit 3 (mask 0x08) is unpreditable. The lower 3 bits
2657 : ** (mask 0x07) should be either 5 (intkey+leafdata for tables) or
2658 : ** 2 (zerodata for indices). If these conditions are not met it can
2659 : ** only mean that we are dealing with a corrupt database file
2660 : */
2661 730 : if( (flags & 0xf0)!=0 || ((flags & 0x07)!=5 && (flags & 0x07)!=2) ){
2662 0 : rc = SQLITE_CORRUPT_BKPT;
2663 0 : goto abort_due_to_error;
2664 : }
2665 730 : pCur->isTable = (flags & BTREE_INTKEY)!=0;
2666 730 : pCur->isIndex = (flags & BTREE_ZERODATA)!=0;
2667 : /* If P3==0 it means we are expected to open a table. If P3!=0 then
2668 : ** we expect to be opening an index. If this is not what happened,
2669 : ** then the database is corrupt
2670 : */
2671 730 : if( (pCur->isTable && pOp->p3type==P3_KEYINFO)
2672 : || (pCur->isIndex && pOp->p3type!=P3_KEYINFO) ){
2673 0 : rc = SQLITE_CORRUPT_BKPT;
2674 0 : goto abort_due_to_error;
2675 : }
2676 730 : break;
2677 : }
2678 : case SQLITE_EMPTY: {
2679 0 : pCur->isTable = pOp->p3type!=P3_KEYINFO;
2680 0 : pCur->isIndex = !pCur->isTable;
2681 0 : rc = SQLITE_OK;
2682 0 : break;
2683 : }
2684 : default: {
2685 0 : goto abort_due_to_error;
2686 : }
2687 : }
2688 730 : break;
2689 : }
2690 :
2691 : /* Opcode: OpenEphemeral P1 P2 P3
2692 : **
2693 : ** Open a new cursor P1 to a transient table.
2694 : ** The cursor is always opened read/write even if
2695 : ** the main database is read-only. The transient or virtual
2696 : ** table is deleted automatically when the cursor is closed.
2697 : **
2698 : ** P2 is the number of columns in the virtual table.
2699 : ** The cursor points to a BTree table if P3==0 and to a BTree index
2700 : ** if P3 is not 0. If P3 is not NULL, it points to a KeyInfo structure
2701 : ** that defines the format of keys in the index.
2702 : **
2703 : ** This opcode was once called OpenTemp. But that created
2704 : ** confusion because the term "temp table", might refer either
2705 : ** to a TEMP table at the SQL level, or to a table opened by
2706 : ** this opcode. Then this opcode was call OpenVirtual. But
2707 : ** that created confusion with the whole virtual-table idea.
2708 : */
2709 : case OP_OpenEphemeral: { /* no-push */
2710 0 : int i = pOp->p1;
2711 : Cursor *pCx;
2712 : assert( i>=0 );
2713 0 : pCx = allocateCursor(p, i, -1);
2714 0 : if( pCx==0 ) goto no_mem;
2715 0 : pCx->nullRow = 1;
2716 0 : rc = sqlite3BtreeFactory(db, 0, 1, TEMP_PAGES, &pCx->pBt);
2717 0 : if( rc==SQLITE_OK ){
2718 0 : rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
2719 : }
2720 0 : if( rc==SQLITE_OK ){
2721 : /* If a transient index is required, create it by calling
2722 : ** sqlite3BtreeCreateTable() with the BTREE_ZERODATA flag before
2723 : ** opening it. If a transient table is required, just use the
2724 : ** automatically created table with root-page 1 (an INTKEY table).
2725 : */
2726 0 : if( pOp->p3 ){
2727 : int pgno;
2728 : assert( pOp->p3type==P3_KEYINFO );
2729 0 : rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA);
2730 0 : if( rc==SQLITE_OK ){
2731 : assert( pgno==MASTER_ROOT+1 );
2732 0 : rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeRecordCompare,
2733 : pOp->p3, &pCx->pCursor);
2734 0 : pCx->pKeyInfo = (KeyInfo*)pOp->p3;
2735 0 : pCx->pKeyInfo->enc = ENC(p->db);
2736 0 : pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
2737 : }
2738 0 : pCx->isTable = 0;
2739 : }else{
2740 0 : rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
2741 0 : pCx->isTable = 1;
2742 0 : pCx->pIncrKey = &pCx->bogusIncrKey;
2743 : }
2744 : }
2745 0 : pCx->nField = pOp->p2;
2746 0 : pCx->isIndex = !pCx->isTable;
2747 0 : break;
2748 : }
2749 :
2750 : /* Opcode: OpenPseudo P1 * *
2751 : **
2752 : ** Open a new cursor that points to a fake table that contains a single
2753 : ** row of data. Any attempt to write a second row of data causes the
2754 : ** first row to be deleted. All data is deleted when the cursor is
2755 : ** closed.
2756 : **
2757 : ** A pseudo-table created by this opcode is useful for holding the
2758 : ** NEW or OLD tables in a trigger. Also used to hold the a single
2759 : ** row output from the sorter so that the row can be decomposed into
2760 : ** individual columns using the OP_Column opcode.
2761 : */
2762 : case OP_OpenPseudo: { /* no-push */
2763 0 : int i = pOp->p1;
2764 : Cursor *pCx;
2765 : assert( i>=0 );
2766 0 : pCx = allocateCursor(p, i, -1);
2767 0 : if( pCx==0 ) goto no_mem;
2768 0 : pCx->nullRow = 1;
2769 0 : pCx->pseudoTable = 1;
2770 0 : pCx->pIncrKey = &pCx->bogusIncrKey;
2771 0 : pCx->isTable = 1;
2772 0 : pCx->isIndex = 0;
2773 0 : break;
2774 : }
2775 :
2776 : /* Opcode: Close P1 * *
2777 : **
2778 : ** Close a cursor previously opened as P1. If P1 is not
2779 : ** currently open, this instruction is a no-op.
2780 : */
2781 : case OP_Close: { /* no-push */
2782 728 : int i = pOp->p1;
2783 728 : if( i>=0 && i<p->nCursor ){
2784 728 : sqlite3VdbeFreeCursor(p, p->apCsr[i]);
2785 728 : p->apCsr[i] = 0;
2786 : }
2787 728 : break;
2788 : }
2789 :
2790 : /* Opcode: MoveGe P1 P2 *
2791 : **
2792 : ** Pop the top of the stack and use its value as a key. Reposition
2793 : ** cursor P1 so that it points to the smallest entry that is greater
2794 : ** than or equal to the key that was popped ffrom the stack.
2795 : ** If there are no records greater than or equal to the key and P2
2796 : ** is not zero, then jump to P2.
2797 : **
2798 : ** See also: Found, NotFound, Distinct, MoveLt, MoveGt, MoveLe
2799 : */
2800 : /* Opcode: MoveGt P1 P2 *
2801 : **
2802 : ** Pop the top of the stack and use its value as a key. Reposition
2803 : ** cursor P1 so that it points to the smallest entry that is greater
2804 : ** than the key from the stack.
2805 : ** If there are no records greater than the key and P2 is not zero,
2806 : ** then jump to P2.
2807 : **
2808 : ** See also: Found, NotFound, Distinct, MoveLt, MoveGe, MoveLe
2809 : */
2810 : /* Opcode: MoveLt P1 P2 *
2811 : **
2812 : ** Pop the top of the stack and use its value as a key. Reposition
2813 : ** cursor P1 so that it points to the largest entry that is less
2814 : ** than the key from the stack.
2815 : ** If there are no records less than the key and P2 is not zero,
2816 : ** then jump to P2.
2817 : **
2818 : ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLe
2819 : */
2820 : /* Opcode: MoveLe P1 P2 *
2821 : **
2822 : ** Pop the top of the stack and use its value as a key. Reposition
2823 : ** cursor P1 so that it points to the largest entry that is less than
2824 : ** or equal to the key that was popped from the stack.
2825 : ** If there are no records less than or eqal to the key and P2 is not zero,
2826 : ** then jump to P2.
2827 : **
2828 : ** See also: Found, NotFound, Distinct, MoveGt, MoveGe, MoveLt
2829 : */
2830 : case OP_MoveLt: /* no-push */
2831 : case OP_MoveLe: /* no-push */
2832 : case OP_MoveGe: /* no-push */
2833 : case OP_MoveGt: { /* no-push */
2834 106 : int i = pOp->p1;
2835 : Cursor *pC;
2836 :
2837 : assert( pTos>=p->aStack );
2838 : assert( i>=0 && i<p->nCursor );
2839 106 : pC = p->apCsr[i];
2840 : assert( pC!=0 );
2841 106 : if( pC->pCursor!=0 ){
2842 : int res, oc;
2843 106 : oc = pOp->opcode;
2844 106 : pC->nullRow = 0;
2845 106 : *pC->pIncrKey = oc==OP_MoveGt || oc==OP_MoveLe;
2846 106 : if( pC->isTable ){
2847 : i64 iKey;
2848 63 : sqlite3VdbeMemIntegerify(pTos);
2849 63 : iKey = intToKey(pTos->u.i);
2850 63 : if( pOp->p2==0 && pOp->opcode==OP_MoveGe ){
2851 63 : pC->movetoTarget = iKey;
2852 63 : pC->deferredMoveto = 1;
2853 : assert( (pTos->flags & MEM_Dyn)==0 );
2854 63 : pTos--;
2855 63 : break;
2856 : }
2857 0 : rc = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)iKey, 0, &res);
2858 0 : if( rc!=SQLITE_OK ){
2859 0 : goto abort_due_to_error;
2860 : }
2861 0 : pC->lastRowid = pTos->u.i;
2862 0 : pC->rowidIsValid = res==0;
2863 : }else{
2864 : assert( pTos->flags & MEM_Blob );
2865 : /* Stringify(pTos, encoding); */
2866 43 : rc = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
2867 43 : if( rc!=SQLITE_OK ){
2868 0 : goto abort_due_to_error;
2869 : }
2870 43 : pC->rowidIsValid = 0;
2871 : }
2872 43 : pC->deferredMoveto = 0;
2873 43 : pC->cacheStatus = CACHE_STALE;
2874 43 : *pC->pIncrKey = 0;
2875 : #ifdef SQLITE_TEST
2876 : sqlite3_search_count++;
2877 : #endif
2878 86 : if( oc==OP_MoveGe || oc==OP_MoveGt ){
2879 43 : if( res<0 ){
2880 16 : rc = sqlite3BtreeNext(pC->pCursor, &res);
2881 16 : if( rc!=SQLITE_OK ) goto abort_due_to_error;
2882 16 : pC->rowidIsValid = 0;
2883 : }else{
2884 27 : res = 0;
2885 : }
2886 : }else{
2887 : assert( oc==OP_MoveLt || oc==OP_MoveLe );
2888 0 : if( res>=0 ){
2889 0 : rc = sqlite3BtreePrevious(pC->pCursor, &res);
2890 0 : if( rc!=SQLITE_OK ) goto abort_due_to_error;
2891 0 : pC->rowidIsValid = 0;
2892 : }else{
2893 : /* res might be negative because the table is empty. Check to
2894 : ** see if this is the case.
2895 : */
2896 0 : res = sqlite3BtreeEof(pC->pCursor);
2897 : }
2898 : }
2899 43 : if( res ){
2900 4 : if( pOp->p2>0 ){
2901 4 : pc = pOp->p2 - 1;
2902 : }else{
2903 0 : pC->nullRow = 1;
2904 : }
2905 : }
2906 : }
2907 43 : Release(pTos);
2908 43 : pTos--;
2909 43 : break;
2910 : }
2911 :
2912 : /* Opcode: Distinct P1 P2 *
2913 : **
2914 : ** Use the top of the stack as a record created using MakeRecord. P1 is a
2915 : ** cursor on a table that declared as an index. If that table contains an
2916 : ** entry that matches the top of the stack fall thru. If the top of the stack
2917 : ** matches no entry in P1 then jump to P2.
2918 : **
2919 : ** The cursor is left pointing at the matching entry if it exists. The
2920 : ** record on the top of the stack is not popped.
2921 : **
2922 : ** This instruction is similar to NotFound except that this operation
2923 : ** does not pop the key from the stack.
2924 : **
2925 : ** The instruction is used to implement the DISTINCT operator on SELECT
2926 : ** statements. The P1 table is not a true index but rather a record of
2927 : ** all results that have produced so far.
2928 : **
2929 : ** See also: Found, NotFound, MoveTo, IsUnique, NotExists
2930 : */
2931 : /* Opcode: Found P1 P2 *
2932 : **
2933 : ** Top of the stack holds a blob constructed by MakeRecord. P1 is an index.
2934 : ** If an entry that matches the top of the stack exists in P1 then
2935 : ** jump to P2. If the top of the stack does not match any entry in P1
2936 : ** then fall thru. The P1 cursor is left pointing at the matching entry
2937 : ** if it exists. The blob is popped off the top of the stack.
2938 : **
2939 : ** This instruction is used to implement the IN operator where the
2940 : ** left-hand side is a SELECT statement. P1 is not a true index but
2941 : ** is instead a temporary index that holds the results of the SELECT
2942 : ** statement. This instruction just checks to see if the left-hand side
2943 : ** of the IN operator (stored on the top of the stack) exists in the
2944 : ** result of the SELECT statement.
2945 : **
2946 : ** See also: Distinct, NotFound, MoveTo, IsUnique, NotExists
2947 : */
2948 : /* Opcode: NotFound P1 P2 *
2949 : **
2950 : ** The top of the stack holds a blob constructed by MakeRecord. P1 is
2951 : ** an index. If no entry exists in P1 that matches the blob then jump
2952 : ** to P2. If an entry does existing, fall through. The cursor is left
2953 : ** pointing to the entry that matches. The blob is popped from the stack.
2954 : **
2955 : ** The difference between this operation and Distinct is that
2956 : ** Distinct does not pop the key from the stack.
2957 : **
2958 : ** See also: Distinct, Found, MoveTo, NotExists, IsUnique
2959 : */
2960 : case OP_Distinct: /* no-push */
2961 : case OP_NotFound: /* no-push */
2962 : case OP_Found: { /* no-push */
2963 0 : int i = pOp->p1;
2964 0 : int alreadyExists = 0;
2965 : Cursor *pC;
2966 : assert( pTos>=p->aStack );
2967 : assert( i>=0 && i<p->nCursor );
2968 : assert( p->apCsr[i]!=0 );
2969 0 : if( (pC = p->apCsr[i])->pCursor!=0 ){
2970 : int res, rx;
2971 : assert( pC->isTable==0 );
2972 0 : Stringify(pTos, encoding);
2973 0 : rx = sqlite3BtreeMoveto(pC->pCursor, pTos->z, pTos->n, 0, &res);
2974 0 : alreadyExists = rx==SQLITE_OK && res==0;
2975 0 : pC->deferredMoveto = 0;
2976 0 : pC->cacheStatus = CACHE_STALE;
2977 : }
2978 0 : if( pOp->opcode==OP_Found ){
2979 0 : if( alreadyExists ) pc = pOp->p2 - 1;
2980 : }else{
2981 0 : if( !alreadyExists ) pc = pOp->p2 - 1;
2982 : }
2983 0 : if( pOp->opcode!=OP_Distinct ){
2984 0 : Release(pTos);
2985 0 : pTos--;
2986 : }
2987 0 : break;
2988 : }
2989 :
2990 : /* Opcode: IsUnique P1 P2 *
2991 : **
2992 : ** The top of the stack is an integer record number. Call this
2993 : ** record number R. The next on the stack is an index key created
2994 : ** using MakeIdxRec. Call it K. This instruction pops R from the
2995 : ** stack but it leaves K unchanged.
2996 : **
2997 : ** P1 is an index. So it has no data and its key consists of a
2998 : ** record generated by OP_MakeRecord where the last field is the
2999 : ** rowid of the entry that the index refers to.
3000 : **
3001 : ** This instruction asks if there is an entry in P1 where the
3002 : ** fields matches K but the rowid is different from R.
3003 : ** If there is no such entry, then there is an immediate
3004 : ** jump to P2. If any entry does exist where the index string
3005 : ** matches K but the record number is not R, then the record
3006 : ** number for that entry is pushed onto the stack and control
3007 : ** falls through to the next instruction.
3008 : **
3009 : ** See also: Distinct, NotFound, NotExists, Found
3010 : */
3011 : case OP_IsUnique: { /* no-push */
3012 121 : int i = pOp->p1;
3013 121 : Mem *pNos = &pTos[-1];
3014 : Cursor *pCx;
3015 : BtCursor *pCrsr;
3016 : i64 R;
3017 :
3018 : /* Pop the value R off the top of the stack
3019 : */
3020 : assert( pNos>=p->aStack );
3021 121 : sqlite3VdbeMemIntegerify(pTos);
3022 121 : R = pTos->u.i;
3023 : assert( (pTos->flags & MEM_Dyn)==0 );
3024 121 : pTos--;
3025 : assert( i>=0 && i<p->nCursor );
3026 121 : pCx = p->apCsr[i];
3027 : assert( pCx!=0 );
3028 121 : pCrsr = pCx->pCursor;
3029 121 : if( pCrsr!=0 ){
3030 : int res;
3031 : i64 v; /* The record number on the P1 entry that matches K */
3032 : char *zKey; /* The value of K */
3033 : int nKey; /* Number of bytes in K */
3034 : int len; /* Number of bytes in K without the rowid at the end */
3035 : int szRowid; /* Size of the rowid column at the end of zKey */
3036 :
3037 : /* Make sure K is a string and make zKey point to K
3038 : */
3039 121 : Stringify(pNos, encoding);
3040 121 : zKey = pNos->z;
3041 121 : nKey = pNos->n;
3042 :
3043 121 : szRowid = sqlite3VdbeIdxRowidLen((u8*)zKey);
3044 121 : len = nKey-szRowid;
3045 :
3046 : /* Search for an entry in P1 where all but the last four bytes match K.
3047 : ** If there is no such entry, jump immediately to P2.
3048 : */
3049 : assert( pCx->deferredMoveto==0 );
3050 121 : pCx->cacheStatus = CACHE_STALE;
3051 121 : rc = sqlite3BtreeMoveto(pCrsr, zKey, len, 0, &res);
3052 121 : if( rc!=SQLITE_OK ){
3053 0 : goto abort_due_to_error;
3054 : }
3055 121 : if( res<0 ){
3056 112 : rc = sqlite3BtreeNext(pCrsr, &res);
3057 112 : if( res ){
3058 110 : pc = pOp->p2 - 1;
3059 110 : break;
3060 : }
3061 : }
3062 11 : rc = sqlite3VdbeIdxKeyCompare(pCx, len, (u8*)zKey, &res);
3063 11 : if( rc!=SQLITE_OK ) goto abort_due_to_error;
3064 11 : if( res>0 ){
3065 11 : pc = pOp->p2 - 1;
3066 11 : break;
3067 : }
3068 :
3069 : /* At this point, pCrsr is pointing to an entry in P1 where all but
3070 : ** the final entry (the rowid) matches K. Check to see if the
3071 : ** final rowid column is different from R. If it equals R then jump
3072 : ** immediately to P2.
3073 : */
3074 0 : rc = sqlite3VdbeIdxRowid(pCrsr, &v);
3075 0 : if( rc!=SQLITE_OK ){
3076 0 : goto abort_due_to_error;
3077 : }
3078 0 : if( v==R ){
3079 0 : pc = pOp->p2 - 1;
3080 0 : break;
3081 : }
3082 :
3083 : /* The final varint of the key is different from R. Push it onto
3084 : ** the stack. (The record number of an entry that violates a UNIQUE
3085 : ** constraint.)
3086 : */
3087 0 : pTos++;
3088 0 : pTos->u.i = v;
3089 0 : pTos->flags = MEM_Int;
3090 : }
3091 0 : break;
3092 : }
3093 :
3094 : /* Opcode: NotExists P1 P2 *
3095 : **
3096 : ** Use the top of the stack as a integer key. If a record with that key
3097 : ** does not exist in table of P1, then jump to P2. If the record
3098 : ** does exist, then fall thru. The cursor is left pointing to the
3099 : ** record if it exists. The integer key is popped from the stack.
3100 : **
3101 : ** The difference between this operation and NotFound is that this
3102 : ** operation assumes the key is an integer and that P1 is a table whereas
3103 : ** NotFound assumes key is a blob constructed from MakeRecord and
3104 : ** P1 is an index.
3105 : **
3106 : ** See also: Distinct, Found, MoveTo, NotFound, IsUnique
3107 : */
3108 : case OP_NotExists: { /* no-push */
3109 125 : int i = pOp->p1;
3110 : Cursor *pC;
3111 : BtCursor *pCrsr;
3112 : assert( pTos>=p->aStack );
3113 : assert( i>=0 && i<p->nCursor );
3114 : assert( p->apCsr[i]!=0 );
3115 125 : if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3116 : int res;
3117 : u64 iKey;
3118 : assert( pTos->flags & MEM_Int );
3119 : assert( p->apCsr[i]->isTable );
3120 125 : iKey = intToKey(pTos->u.i);
3121 125 : rc = sqlite3BtreeMoveto(pCrsr, 0, iKey, 0,&res);
3122 125 : pC->lastRowid = pTos->u.i;
3123 125 : pC->rowidIsValid = res==0;
3124 125 : pC->nullRow = 0;
3125 125 : pC->cacheStatus = CACHE_STALE;
3126 : /* res might be uninitialized if rc!=SQLITE_OK. But if rc!=SQLITE_OK
3127 : ** processing is about to abort so we really do not care whether or not
3128 : ** the following jump is taken. (In other words, do not stress over
3129 : ** the error that valgrind sometimes shows on the next statement when
3130 : ** running ioerr.test and similar failure-recovery test scripts.) */
3131 125 : if( res!=0 ){
3132 3 : pc = pOp->p2 - 1;
3133 3 : pC->rowidIsValid = 0;
3134 : }
3135 : }
3136 125 : Release(pTos);
3137 125 : pTos--;
3138 125 : break;
3139 : }
3140 :
3141 : /* Opcode: Sequence P1 * *
3142 : **
3143 : ** Push an integer onto the stack which is the next available
3144 : ** sequence number for cursor P1. The sequence number on the
3145 : ** cursor is incremented after the push.
3146 : */
3147 : case OP_Sequence: {
3148 0 : int i = pOp->p1;
3149 : assert( pTos>=p->aStack );
3150 : assert( i>=0 && i<p->nCursor );
3151 : assert( p->apCsr[i]!=0 );
3152 0 : pTos++;
3153 0 : pTos->u.i = p->apCsr[i]->seqCount++;
3154 0 : pTos->flags = MEM_Int;
3155 0 : break;
3156 : }
3157 :
3158 :
3159 : /* Opcode: NewRowid P1 P2 *
3160 : **
3161 : ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
3162 : ** The record number is not previously used as a key in the database
3163 : ** table that cursor P1 points to. The new record number is pushed
3164 : ** onto the stack.
3165 : **
3166 : ** If P2>0 then P2 is a memory cell that holds the largest previously
3167 : ** generated record number. No new record numbers are allowed to be less
3168 : ** than this value. When this value reaches its maximum, a SQLITE_FULL
3169 : ** error is generated. The P2 memory cell is updated with the generated
3170 : ** record number. This P2 mechanism is used to help implement the
3171 : ** AUTOINCREMENT feature.
3172 : */
3173 : case OP_NewRowid: {
3174 240 : int i = pOp->p1;
3175 240 : i64 v = 0;
3176 : Cursor *pC;
3177 : assert( i>=0 && i<p->nCursor );
3178 : assert( p->apCsr[i]!=0 );
3179 240 : if( (pC = p->apCsr[i])->pCursor==0 ){
3180 : /* The zero initialization above is all that is needed */
3181 : }else{
3182 : /* The next rowid or record number (different terms for the same
3183 : ** thing) is obtained in a two-step algorithm.
3184 : **
3185 : ** First we attempt to find the largest existing rowid and add one
3186 : ** to that. But if the largest existing rowid is already the maximum
3187 : ** positive integer, we have to fall through to the second
3188 : ** probabilistic algorithm
3189 : **
3190 : ** The second algorithm is to select a rowid at random and see if
3191 : ** it already exists in the table. If it does not exist, we have
3192 : ** succeeded. If the random rowid does exist, we select a new one
3193 : ** and try again, up to 1000 times.
3194 : **
3195 : ** For a table with less than 2 billion entries, the probability
3196 : ** of not finding a unused rowid is about 1.0e-300. This is a
3197 : ** non-zero probability, but it is still vanishingly small and should
3198 : ** never cause a problem. You are much, much more likely to have a
3199 : ** hardware failure than for this algorithm to fail.
3200 : **
3201 : ** The analysis in the previous paragraph assumes that you have a good
3202 : ** source of random numbers. Is a library function like lrand48()
3203 : ** good enough? Maybe. Maybe not. It's hard to know whether there
3204 : ** might be subtle bugs is some implementations of lrand48() that
3205 : ** could cause problems. To avoid uncertainty, SQLite uses its own
3206 : ** random number generator based on the RC4 algorithm.
3207 : **
3208 : ** To promote locality of reference for repetitive inserts, the
3209 : ** first few attempts at chosing a random rowid pick values just a little
3210 : ** larger than the previous rowid. This has been shown experimentally
3211 : ** to double the speed of the COPY operation.
3212 : */
3213 240 : int res, rx=SQLITE_OK, cnt;
3214 : i64 x;
3215 240 : cnt = 0;
3216 240 : if( (sqlite3BtreeFlags(pC->pCursor)&(BTREE_INTKEY|BTREE_ZERODATA)) !=
3217 : BTREE_INTKEY ){
3218 0 : rc = SQLITE_CORRUPT_BKPT;
3219 0 : goto abort_due_to_error;
3220 : }
3221 : assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_INTKEY)!=0 );
3222 : assert( (sqlite3BtreeFlags(pC->pCursor) & BTREE_ZERODATA)==0 );
3223 :
3224 : #ifdef SQLITE_32BIT_ROWID
3225 : # define MAX_ROWID 0x7fffffff
3226 : #else
3227 : /* Some compilers complain about constants of the form 0x7fffffffffffffff.
3228 : ** Others complain about 0x7ffffffffffffffffLL. The following macro seems
3229 : ** to provide the constant while making all compilers happy.
3230 : */
3231 : # define MAX_ROWID ( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
3232 : #endif
3233 :
3234 240 : if( !pC->useRandomRowid ){
3235 240 : if( pC->nextRowidValid ){
3236 0 : v = pC->nextRowid;
3237 : }else{
3238 240 : rc = sqlite3BtreeLast(pC->pCursor, &res);
3239 240 : if( rc!=SQLITE_OK ){
3240 0 : goto abort_due_to_error;
3241 : }
3242 240 : if( res ){
3243 108 : v = 1;
3244 : }else{
3245 132 : sqlite3BtreeKeySize(pC->pCursor, &v);
3246 132 : v = keyToInt(v);
3247 132 : if( v==MAX_ROWID ){
3248 0 : pC->useRandomRowid = 1;
3249 : }else{
3250 132 : v++;
3251 : }
3252 : }
3253 : }
3254 :
3255 : #ifndef SQLITE_OMIT_AUTOINCREMENT
3256 240 : if( pOp->p2 ){
3257 : Mem *pMem;
3258 : assert( pOp->p2>0 && pOp->p2<p->nMem ); /* P2 is a valid memory cell */
3259 0 : pMem = &p->aMem[pOp->p2];
3260 0 : sqlite3VdbeMemIntegerify(pMem);
3261 : assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P2) holds an integer */
3262 0 : if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
3263 0 : rc = SQLITE_FULL;
3264 0 : goto abort_due_to_error;
3265 : }
3266 0 : if( v<pMem->u.i+1 ){
3267 0 : v = pMem->u.i + 1;
3268 : }
3269 0 : pMem->u.i = v;
3270 : }
3271 : #endif
3272 :
3273 240 : if( v<MAX_ROWID ){
3274 240 : pC->nextRowidValid = 1;
3275 240 : pC->nextRowid = v+1;
3276 : }else{
3277 0 : pC->nextRowidValid = 0;
3278 : }
3279 : }
3280 240 : if( pC->useRandomRowid ){
3281 : assert( pOp->p2==0 ); /* SQLITE_FULL must have occurred prior to this */
3282 0 : v = db->priorNewRowid;
3283 0 : cnt = 0;
3284 : do{
3285 0 : if( v==0 || cnt>2 ){
3286 0 : sqlite3Randomness(sizeof(v), &v);
3287 0 : if( cnt<5 ) v &= 0xffffff;
3288 : }else{
3289 : unsigned char r;
3290 0 : sqlite3Randomness(1, &r);
3291 0 : v += r + 1;
3292 : }
3293 0 : if( v==0 ) continue;
3294 0 : x = intToKey(v);
3295 0 : rx = sqlite3BtreeMoveto(pC->pCursor, 0, (u64)x, 0, &res);
3296 0 : cnt++;
3297 0 : }while( cnt<1000 && rx==SQLITE_OK && res==0 );
3298 0 : db->priorNewRowid = v;
3299 0 : if( rx==SQLITE_OK && res==0 ){
3300 0 : rc = SQLITE_FULL;
3301 0 : goto abort_due_to_error;
3302 : }
3303 : }
3304 240 : pC->rowidIsValid = 0;
3305 240 : pC->deferredMoveto = 0;
3306 240 : pC->cacheStatus = CACHE_STALE;
3307 : }
3308 240 : pTos++;
3309 240 : pTos->u.i = v;
3310 240 : pTos->flags = MEM_Int;
3311 240 : break;
3312 : }
3313 :
3314 : /* Opcode: Insert P1 P2 P3
3315 : **
3316 : ** Write an entry into the table of cursor P1. A new entry is
3317 : ** created if it doesn't already exist or the data for an existing
3318 : ** entry is overwritten. The data is the value on the top of the
3319 : ** stack. The key is the next value down on the stack. The key must
3320 : ** be an integer. The stack is popped twice by this instruction.
3321 : **
3322 : ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3323 : ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P2 is set,
3324 : ** then rowid is stored for subsequent return by the
3325 : ** sqlite3_last_insert_rowid() function (otherwise it's unmodified).
3326 : **
3327 : ** Parameter P3 may point to a string containing the table-name, or
3328 : ** may be NULL. If it is not NULL, then the update-hook
3329 : ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
3330 : **
3331 : ** This instruction only works on tables. The equivalent instruction
3332 : ** for indices is OP_IdxInsert.
3333 : */
3334 : case OP_Insert: { /* no-push */
3335 301 : Mem *pNos = &pTos[-1];
3336 301 : int i = pOp->p1;
3337 : Cursor *pC;
3338 : assert( pNos>=p->aStack );
3339 : assert( i>=0 && i<p->nCursor );
3340 : assert( p->apCsr[i]!=0 );
3341 301 : if( ((pC = p->apCsr[i])->pCursor!=0 || pC->pseudoTable) ){
3342 : i64 iKey; /* The integer ROWID or key for the record to be inserted */
3343 :
3344 : assert( pNos->flags & MEM_Int );
3345 : assert( pC->isTable );
3346 301 : iKey = intToKey(pNos->u.i);
3347 :
3348 301 : if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
3349 301 : if( pOp->p2 & OPFLAG_LASTROWID ) db->lastRowid = pNos->u.i;
3350 301 : if( pC->nextRowidValid && pNos->u.i>=pC->nextRowid ){
3351 0 : pC->nextRowidValid = 0;
3352 : }
3353 301 : if( pTos->flags & MEM_Null ){
3354 57 : pTos->z = 0;
3355 57 : pTos->n = 0;
3356 : }else{
3357 : assert( pTos->flags & (MEM_Blob|MEM_Str) );
3358 : }
3359 301 : if( pC->pseudoTable ){
3360 0 : sqliteFree(pC->pData);
3361 0 : pC->iKey = iKey;
3362 0 : pC->nData = pTos->n;
3363 0 : if( pTos->flags & MEM_Dyn ){
3364 0 : pC->pData = pTos->z;
3365 0 : pTos->flags = MEM_Null;
3366 : }else{
3367 0 : pC->pData = sqliteMallocRaw( pC->nData+2 );
3368 0 : if( !pC->pData ) goto no_mem;
3369 0 : memcpy(pC->pData, pTos->z, pC->nData);
3370 0 : pC->pData[pC->nData] = 0;
3371 0 : pC->pData[pC->nData+1] = 0;
3372 : }
3373 0 : pC->nullRow = 0;
3374 : }else{
3375 301 : rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
3376 : pTos->z, pTos->n,
3377 : pOp->p2 & OPFLAG_APPEND);
3378 : }
3379 :
3380 301 : pC->rowidIsValid = 0;
3381 301 : pC->deferredMoveto = 0;
3382 301 : pC->cacheStatus = CACHE_STALE;
3383 :
3384 : /* Invoke the update-hook if required. */
3385 301 : if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3386 0 : const char *zDb = db->aDb[pC->iDb].zName;
3387 0 : const char *zTbl = pOp->p3;
3388 0 : int op = ((pOp->p2 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
3389 : assert( pC->isTable );
3390 0 : db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
3391 : assert( pC->iDb>=0 );
3392 : }
3393 : }
3394 301 : popStack(&pTos, 2);
3395 :
3396 301 : break;
3397 : }
3398 :
3399 : /* Opcode: Delete P1 P2 P3
3400 : **
3401 : ** Delete the record at which the P1 cursor is currently pointing.
3402 : **
3403 : ** The cursor will be left pointing at either the next or the previous
3404 : ** record in the table. If it is left pointing at the next record, then
3405 : ** the next Next instruction will be a no-op. Hence it is OK to delete
3406 : ** a record from within an Next loop.
3407 : **
3408 : ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
3409 : ** incremented (otherwise not).
3410 : **
3411 : ** If P1 is a pseudo-table, then this instruction is a no-op.
3412 : */
3413 : case OP_Delete: { /* no-push */
3414 5 : int i = pOp->p1;
3415 : Cursor *pC;
3416 : assert( i>=0 && i<p->nCursor );
3417 5 : pC = p->apCsr[i];
3418 : assert( pC!=0 );
3419 5 : if( pC->pCursor!=0 ){
3420 : i64 iKey;
3421 :
3422 : /* If the update-hook will be invoked, set iKey to the rowid of the
3423 : ** row being deleted.
3424 : */
3425 5 : if( db->xUpdateCallback && pOp->p3 ){
3426 : assert( pC->isTable );
3427 0 : if( pC->rowidIsValid ){
3428 0 : iKey = pC->lastRowid;
3429 : }else{
3430 0 : rc = sqlite3BtreeKeySize(pC->pCursor, &iKey);
3431 0 : if( rc ){
3432 0 : goto abort_due_to_error;
3433 : }
3434 0 : iKey = keyToInt(iKey);
3435 : }
3436 : }
3437 :
3438 5 : rc = sqlite3VdbeCursorMoveto(pC);
3439 5 : if( rc ) goto abort_due_to_error;
3440 5 : rc = sqlite3BtreeDelete(pC->pCursor);
3441 5 : pC->nextRowidValid = 0;
3442 5 : pC->cacheStatus = CACHE_STALE;
3443 :
3444 : /* Invoke the update-hook if required. */
3445 5 : if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p3 ){
3446 0 : const char *zDb = db->aDb[pC->iDb].zName;
3447 0 : const char *zTbl = pOp->p3;
3448 0 : db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
3449 : assert( pC->iDb>=0 );
3450 : }
3451 : }
3452 5 : if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
3453 5 : break;
3454 : }
3455 :
3456 : /* Opcode: ResetCount P1 * *
3457 : **
3458 : ** This opcode resets the VMs internal change counter to 0. If P1 is true,
3459 : ** then the value of the change counter is copied to the database handle
3460 : ** change counter (returned by subsequent calls to sqlite3_changes())
3461 : ** before it is reset. This is used by trigger programs.
3462 : */
3463 : case OP_ResetCount: { /* no-push */
3464 0 : if( pOp->p1 ){
3465 0 : sqlite3VdbeSetChanges(db, p->nChange);
3466 : }
3467 0 : p->nChange = 0;
3468 0 : break;
3469 : }
3470 :
3471 : /* Opcode: RowData P1 * *
3472 : **
3473 : ** Push onto the stack the complete row data for cursor P1.
3474 : ** There is no interpretation of the data. It is just copied
3475 : ** onto the stack exactly as it is found in the database file.
3476 : **
3477 : ** If the cursor is not pointing to a valid row, a NULL is pushed
3478 : ** onto the stack.
3479 : */
3480 : /* Opcode: RowKey P1 * *
3481 : **
3482 : ** Push onto the stack the complete row key for cursor P1.
3483 : ** There is no interpretation of the key. It is just copied
3484 : ** onto the stack exactly as it is found in the database file.
3485 : **
3486 : ** If the cursor is not pointing to a valid row, a NULL is pushed
3487 : ** onto the stack.
3488 : */
3489 : case OP_RowKey:
3490 : case OP_RowData: {
3491 0 : int i = pOp->p1;
3492 : Cursor *pC;
3493 : u32 n;
3494 :
3495 : /* Note that RowKey and RowData are really exactly the same instruction */
3496 0 : pTos++;
3497 : assert( i>=0 && i<p->nCursor );
3498 0 : pC = p->apCsr[i];
3499 : assert( pC->isTable || pOp->opcode==OP_RowKey );
3500 : assert( pC->isIndex || pOp->opcode==OP_RowData );
3501 : assert( pC!=0 );
3502 0 : if( pC->nullRow ){
3503 0 : pTos->flags = MEM_Null;
3504 0 : }else if( pC->pCursor!=0 ){
3505 0 : BtCursor *pCrsr = pC->pCursor;
3506 0 : rc = sqlite3VdbeCursorMoveto(pC);
3507 0 : if( rc ) goto abort_due_to_error;
3508 0 : if( pC->nullRow ){
3509 0 : pTos->flags = MEM_Null;
3510 0 : break;
3511 0 : }else if( pC->isIndex ){
3512 : i64 n64;
3513 : assert( !pC->isTable );
3514 0 : sqlite3BtreeKeySize(pCrsr, &n64);
3515 0 : n = n64;
3516 : }else{
3517 0 : sqlite3BtreeDataSize(pCrsr, &n);
3518 : }
3519 0 : pTos->n = n;
3520 0 : if( n<=NBFS ){
3521 0 : pTos->flags = MEM_Blob | MEM_Short;
3522 0 : pTos->z = pTos->zShort;
3523 : }else{
3524 0 : char *z = sqliteMallocRaw( n );
3525 0 : if( z==0 ) goto no_mem;
3526 0 : pTos->flags = MEM_Blob | MEM_Dyn;
3527 0 : pTos->xDel = 0;
3528 0 : pTos->z = z;
3529 : }
3530 0 : if( pC->isIndex ){
3531 0 : rc = sqlite3BtreeKey(pCrsr, 0, n, pTos->z);
3532 : }else{
3533 0 : rc = sqlite3BtreeData(pCrsr, 0, n, pTos->z);
3534 : }
3535 0 : }else if( pC->pseudoTable ){
3536 0 : pTos->n = pC->nData;
3537 0 : pTos->z = pC->pData;
3538 0 : pTos->flags = MEM_Blob|MEM_Ephem;
3539 : }else{
3540 0 : pTos->flags = MEM_Null;
3541 : }
3542 0 : pTos->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
3543 0 : break;
3544 : }
3545 :
3546 : /* Opcode: Rowid P1 * *
3547 : **
3548 : ** Push onto the stack an integer which is the key of the table entry that
3549 : ** P1 is currently point to.
3550 : */
3551 : case OP_Rowid: {
3552 68 : int i = pOp->p1;
3553 : Cursor *pC;
3554 : i64 v;
3555 :
3556 : assert( i>=0 && i<p->nCursor );
3557 68 : pC = p->apCsr[i];
3558 : assert( pC!=0 );
3559 68 : rc = sqlite3VdbeCursorMoveto(pC);
3560 68 : if( rc ) goto abort_due_to_error;
3561 68 : pTos++;
3562 68 : if( pC->rowidIsValid ){
3563 59 : v = pC->lastRowid;
3564 9 : }else if( pC->pseudoTable ){
3565 0 : v = keyToInt(pC->iKey);
3566 9 : }else if( pC->nullRow || pC->pCursor==0 ){
3567 0 : pTos->flags = MEM_Null;
3568 0 : break;
3569 : }else{
3570 : assert( pC->pCursor!=0 );
3571 9 : sqlite3BtreeKeySize(pC->pCursor, &v);
3572 9 : v = keyToInt(v);
3573 : }
3574 68 : pTos->u.i = v;
3575 68 : pTos->flags = MEM_Int;
3576 68 : break;
3577 : }
3578 :
3579 : /* Opcode: NullRow P1 * *
3580 : **
3581 : ** Move the cursor P1 to a null row. Any OP_Column operations
3582 : ** that occur while the cursor is on the null row will always push
3583 : ** a NULL onto the stack.
3584 : */
3585 : case OP_NullRow: { /* no-push */
3586 13 : int i = pOp->p1;
3587 : Cursor *pC;
3588 :
3589 : assert( i>=0 && i<p->nCursor );
3590 13 : pC = p->apCsr[i];
3591 : assert( pC!=0 );
3592 13 : pC->nullRow = 1;
3593 13 : pC->rowidIsValid = 0;
3594 13 : break;
3595 : }
3596 :
3597 : /* Opcode: Last P1 P2 *
3598 : **
3599 : ** The next use of the Rowid or Column or Next instruction for P1
3600 : ** will refer to the last entry in the database table or index.
3601 : ** If the table or index is empty and P2>0, then jump immediately to P2.
3602 : ** If P2 is 0 or if the table or index is not empty, fall through
3603 : ** to the following instruction.
3604 : */
3605 : case OP_Last: { /* no-push */
3606 0 : int i = pOp->p1;
3607 : Cursor *pC;
3608 : BtCursor *pCrsr;
3609 :
3610 : assert( i>=0 && i<p->nCursor );
3611 0 : pC = p->apCsr[i];
3612 : assert( pC!=0 );
3613 0 : if( (pCrsr = pC->pCursor)!=0 ){
3614 : int res;
3615 0 : rc = sqlite3BtreeLast(pCrsr, &res);
3616 0 : pC->nullRow = res;
3617 0 : pC->deferredMoveto = 0;
3618 0 : pC->cacheStatus = CACHE_STALE;
3619 0 : if( res && pOp->p2>0 ){
3620 0 : pc = pOp->p2 - 1;
3621 : }
3622 : }else{
3623 0 : pC->nullRow = 0;
3624 : }
3625 0 : break;
3626 : }
3627 :
3628 :
3629 : /* Opcode: Sort P1 P2 *
3630 : **
3631 : ** This opcode does exactly the same thing as OP_Rewind except that
3632 : ** it increments an undocumented global variable used for testing.
3633 : **
3634 : ** Sorting is accomplished by writing records into a sorting index,
3635 : ** then rewinding that index and playing it back from beginning to
3636 : ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
3637 : ** rewinding so that the global variable will be incremented and
3638 : ** regression tests can determine whether or not the optimizer is
3639 : ** correctly optimizing out sorts.
3640 : */
3641 : case OP_Sort: { /* no-push */
3642 : #ifdef SQLITE_TEST
3643 : sqlite3_sort_count++;
3644 : sqlite3_search_count--;
3645 : #endif
3646 : /* Fall through into OP_Rewind */
3647 : }
3648 : /* Opcode: Rewind P1 P2 *
3649 : **
3650 : ** The next use of the Rowid or Column or Next instruction for P1
3651 : ** will refer to the first entry in the database table or index.
3652 : ** If the table or index is empty and P2>0, then jump immediately to P2.
3653 : ** If P2 is 0 or if the table or index is not empty, fall through
3654 : ** to the following instruction.
3655 : */
3656 : case OP_Rewind: { /* no-push */
3657 183 : int i = pOp->p1;
3658 : Cursor *pC;
3659 : BtCursor *pCrsr;
3660 : int res;
3661 :
3662 : assert( i>=0 && i<p->nCursor );
3663 183 : pC = p->apCsr[i];
3664 : assert( pC!=0 );
3665 183 : if( (pCrsr = pC->pCursor)!=0 ){
3666 183 : rc = sqlite3BtreeFirst(pCrsr, &res);
3667 183 : pC->atFirst = res==0;
3668 183 : pC->deferredMoveto = 0;
3669 183 : pC->cacheStatus = CACHE_STALE;
3670 : }else{
3671 0 : res = 1;
3672 : }
3673 183 : pC->nullRow = res;
3674 183 : if( res && pOp->p2>0 ){
3675 12 : pc = pOp->p2 - 1;
3676 : }
3677 183 : break;
3678 : }
3679 :
3680 : /* Opcode: Next P1 P2 *
3681 : **
3682 : ** Advance cursor P1 so that it points to the next key/data pair in its
3683 : ** table or index. If there are no more key/value pairs then fall through
3684 : ** to the following instruction. But if the cursor advance was successful,
3685 : ** jump immediately to P2.
3686 : **
3687 : ** See also: Prev
3688 : */
3689 : /* Opcode: Prev P1 P2 *
3690 : **
3691 : ** Back up cursor P1 so that it points to the previous key/data pair in its
3692 : ** table or index. If there is no previous key/value pairs then fall through
3693 : ** to the following instruction. But if the cursor backup was successful,
3694 : ** jump immediately to P2.
3695 : */
3696 : case OP_Prev: /* no-push */
3697 : case OP_Next: { /* no-push */
3698 : Cursor *pC;
3699 : BtCursor *pCrsr;
3700 :
3701 405 : CHECK_FOR_INTERRUPT;
3702 : assert( pOp->p1>=0 && pOp->p1<p->nCursor );
3703 405 : pC = p->apCsr[pOp->p1];
3704 405 : if( pC==0 ){
3705 0 : break; /* See ticket #2273 */
3706 : }
3707 405 : if( (pCrsr = pC->pCursor)!=0 ){
3708 : int res;
3709 405 : if( pC->nullRow ){
3710 7 : res = 1;
3711 : }else{
3712 : assert( pC->deferredMoveto==0 );
3713 398 : rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
3714 : sqlite3BtreePrevious(pCrsr, &res);
3715 398 : pC->nullRow = res;
3716 398 : pC->cacheStatus = CACHE_STALE;
3717 : }
3718 405 : if( res==0 ){
3719 244 : pc = pOp->p2 - 1;
3720 : #ifdef SQLITE_TEST
3721 : sqlite3_search_count++;
3722 : #endif
3723 : }
3724 : }else{
3725 0 : pC->nullRow = 1;
3726 : }
3727 405 : pC->rowidIsValid = 0;
3728 405 : break;
3729 : }
3730 :
3731 : /* Opcode: IdxInsert P1 P2 *
3732 : **
3733 : ** The top of the stack holds a SQL index key made using either the
3734 : ** MakeIdxRec or MakeRecord instructions. This opcode writes that key
3735 : ** into the index P1. Data for the entry is nil.
3736 : **
3737 : ** P2 is a flag that provides a hint to the b-tree layer that this
3738 : ** insert is likely to be an append.
3739 : **
3740 : ** This instruction only works for indices. The equivalent instruction
3741 : ** for tables is OP_Insert.
3742 : */
3743 : case OP_IdxInsert: { /* no-push */
3744 121 : int i = pOp->p1;
3745 : Cursor *pC;
3746 : BtCursor *pCrsr;
3747 : assert( pTos>=p->aStack );
3748 : assert( i>=0 && i<p->nCursor );
3749 : assert( p->apCsr[i]!=0 );
3750 : assert( pTos->flags & MEM_Blob );
3751 121 : if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3752 121 : int nKey = pTos->n;
3753 121 : const char *zKey = pTos->z;
3754 : assert( pC->isTable==0 );
3755 121 : rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, pOp->p2);
3756 : assert( pC->deferredMoveto==0 );
3757 121 : pC->cacheStatus = CACHE_STALE;
3758 : }
3759 121 : Release(pTos);
3760 121 : pTos--;
3761 121 : break;
3762 : }
3763 :
3764 : /* Opcode: IdxDelete P1 * *
3765 : **
3766 : ** The top of the stack is an index key built using the either the
3767 : ** MakeIdxRec or MakeRecord opcodes.
3768 : ** This opcode removes that entry from the index.
3769 : */
3770 : case OP_IdxDelete: { /* no-push */
3771 0 : int i = pOp->p1;
3772 : Cursor *pC;
3773 : BtCursor *pCrsr;
3774 : assert( pTos>=p->aStack );
3775 : assert( pTos->flags & MEM_Blob );
3776 : assert( i>=0 && i<p->nCursor );
3777 : assert( p->apCsr[i]!=0 );
3778 0 : if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3779 : int res;
3780 0 : rc = sqlite3BtreeMoveto(pCrsr, pTos->z, pTos->n, 0, &res);
3781 0 : if( rc==SQLITE_OK && res==0 ){
3782 0 : rc = sqlite3BtreeDelete(pCrsr);
3783 : }
3784 : assert( pC->deferredMoveto==0 );
3785 0 : pC->cacheStatus = CACHE_STALE;
3786 : }
3787 0 : Release(pTos);
3788 0 : pTos--;
3789 0 : break;
3790 : }
3791 :
3792 : /* Opcode: IdxRowid P1 * *
3793 : **
3794 : ** Push onto the stack an integer which is the last entry in the record at
3795 : ** the end of the index key pointed to by cursor P1. This integer should be
3796 : ** the rowid of the table entry to which this index entry points.
3797 : **
3798 : ** See also: Rowid, MakeIdxRec.
3799 : */
3800 : case OP_IdxRowid: {
3801 63 : int i = pOp->p1;
3802 : BtCursor *pCrsr;
3803 : Cursor *pC;
3804 :
3805 : assert( i>=0 && i<p->nCursor );
3806 : assert( p->apCsr[i]!=0 );
3807 63 : pTos++;
3808 63 : pTos->flags = MEM_Null;
3809 63 : if( (pCrsr = (pC = p->apCsr[i])->pCursor)!=0 ){
3810 : i64 rowid;
3811 :
3812 : assert( pC->deferredMoveto==0 );
3813 : assert( pC->isTable==0 );
3814 63 : if( pC->nullRow ){
3815 0 : pTos->flags = MEM_Null;
3816 : }else{
3817 63 : rc = sqlite3VdbeIdxRowid(pCrsr, &rowid);
3818 63 : if( rc!=SQLITE_OK ){
3819 0 : goto abort_due_to_error;
3820 : }
3821 63 : pTos->flags = MEM_Int;
3822 63 : pTos->u.i = rowid;
3823 : }
3824 : }
3825 63 : break;
3826 : }
3827 :
3828 : /* Opcode: IdxGT P1 P2 *
3829 : **
3830 : ** The top of the stack is an index entry that omits the ROWID. Compare
3831 : ** the top of stack against the index that P1 is currently pointing to.
3832 : ** Ignore the ROWID on the P1 index.
3833 : **
3834 : ** The top of the stack might have fewer columns that P1.
3835 : **
3836 : ** If the P1 index entry is greater than the top of the stack
3837 : ** then jump to P2. Otherwise fall through to the next instruction.
3838 : ** In either case, the stack is popped once.
3839 : */
3840 : /* Opcode: IdxGE P1 P2 P3
3841 : **
3842 : ** The top of the stack is an index entry that omits the ROWID. Compare
3843 : ** the top of stack against the index that P1 is currently pointing to.
3844 : ** Ignore the ROWID on the P1 index.
3845 : **
3846 : ** If the P1 index entry is greater than or equal to the top of the stack
3847 : ** then jump to P2. Otherwise fall through to the next instruction.
3848 : ** In either case, the stack is popped once.
3849 : **
3850 : ** If P3 is the "+" string (or any other non-NULL string) then the
3851 : ** index taken from the top of the stack is temporarily increased by
3852 : ** an epsilon prior to the comparison. This make the opcode work
3853 : ** like IdxGT except that if the key from the stack is a prefix of
3854 : ** the key in the cursor, the result is false whereas it would be
3855 : ** true with IdxGT.
3856 : */
3857 : /* Opcode: IdxLT P1 P2 P3
3858 : **
3859 : ** The top of the stack is an index entry that omits the ROWID. Compare
3860 : ** the top of stack against the index that P1 is currently pointing to.
3861 : ** Ignore the ROWID on the P1 index.
3862 : **
3863 : ** If the P1 index entry is less than the top of the stack
3864 : ** then jump to P2. Otherwise fall through to the next instruction.
3865 : ** In either case, the stack is popped once.
3866 : **
3867 : ** If P3 is the "+" string (or any other non-NULL string) then the
3868 : ** index taken from the top of the stack is temporarily increased by
3869 : ** an epsilon prior to the comparison. This makes the opcode work
3870 : ** like IdxLE.
3871 : */
3872 : case OP_IdxLT: /* no-push */
3873 : case OP_IdxGT: /* no-push */
3874 : case OP_IdxGE: { /* no-push */
3875 53 : int i= pOp->p1;
3876 : Cursor *pC;
3877 :
3878 : assert( i>=0 && i<p->nCursor );
3879 : assert( p->apCsr[i]!=0 );
3880 : assert( pTos>=p->aStack );
3881 53 : if( (pC = p->apCsr[i])->pCursor!=0 ){
3882 : int res;
3883 :
3884 : assert( pTos->flags & MEM_Blob ); /* Created using OP_Make*Key */
3885 53 : Stringify(pTos, encoding);
3886 : assert( pC->deferredMoveto==0 );
3887 53 : *pC->pIncrKey = pOp->p3!=0;
3888 : assert( pOp->p3==0 || pOp->opcode!=OP_IdxGT );
3889 53 : rc = sqlite3VdbeIdxKeyCompare(pC, pTos->n, (u8*)pTos->z, &res);
3890 53 : *pC->pIncrKey = 0;
3891 53 : if( rc!=SQLITE_OK ){
3892 0 : break;
3893 : }
3894 53 : if( pOp->opcode==OP_IdxLT ){
3895 0 : res = -res;
3896 53 : }else if( pOp->opcode==OP_IdxGE ){
3897 53 : res++;
3898 : }
3899 53 : if( res>0 ){
3900 15 : pc = pOp->p2 - 1 ;
3901 : }
3902 : }
3903 53 : Release(pTos);
3904 53 : pTos--;
3905 53 : break;
3906 : }
3907 :
3908 : /* Opcode: Destroy P1 P2 *
3909 : **
3910 : ** Delete an entire database table or index whose root page in the database
3911 : ** file is given by P1.
3912 : **
3913 : ** The table being destroyed is in the main database file if P2==0. If
3914 : ** P2==1 then the table to be clear is in the auxiliary database file
3915 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
3916 : **
3917 : ** If AUTOVACUUM is enabled then it is possible that another root page
3918 : ** might be moved into the newly deleted root page in order to keep all
3919 : ** root pages contiguous at the beginning of the database. The former
3920 : ** value of the root page that moved - its value before the move occurred -
3921 : ** is pushed onto the stack. If no page movement was required (because
3922 : ** the table being dropped was already the last one in the database) then
3923 : ** a zero is pushed onto the stack. If AUTOVACUUM is disabled
3924 : ** then a zero is pushed onto the stack.
3925 : **
3926 : ** See also: Clear
3927 : */
3928 : case OP_Destroy: {
3929 : int iMoved;
3930 : int iCnt;
3931 : #ifndef SQLITE_OMIT_VIRTUALTABLE
3932 : Vdbe *pVdbe;
3933 5 : iCnt = 0;
3934 12 : for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
3935 7 : if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
3936 6 : iCnt++;
3937 : }
3938 : }
3939 : #else
3940 : iCnt = db->activeVdbeCnt;
3941 : #endif
3942 5 : if( iCnt>1 ){
3943 1 : rc = SQLITE_LOCKED;
3944 : }else{
3945 : assert( iCnt==1 );
3946 4 : rc = sqlite3BtreeDropTable(db->aDb[pOp->p2].pBt, pOp->p1, &iMoved);
3947 4 : pTos++;
3948 4 : pTos->flags = MEM_Int;
3949 4 : pTos->u.i = iMoved;
3950 : #ifndef SQLITE_OMIT_AUTOVACUUM
3951 4 : if( rc==SQLITE_OK && iMoved!=0 ){
3952 0 : sqlite3RootPageMoved(&db->aDb[pOp->p2], iMoved, pOp->p1);
3953 : }
3954 : #endif
3955 : }
3956 5 : break;
3957 : }
3958 :
3959 : /* Opcode: Clear P1 P2 *
3960 : **
3961 : ** Delete all contents of the database table or index whose root page
3962 : ** in the database file is given by P1. But, unlike Destroy, do not
3963 : ** remove the table or index from the database file.
3964 : **
3965 : ** The table being clear is in the main database file if P2==0. If
3966 : ** P2==1 then the table to be clear is in the auxiliary database file
3967 : ** that is used to store tables create using CREATE TEMPORARY TABLE.
3968 : **
3969 : ** See also: Destroy
3970 : */
3971 : case OP_Clear: { /* no-push */
3972 :
3973 : /* For consistency with the way other features of SQLite operate
3974 : ** with a truncate, we will also skip the update callback.
3975 : */
3976 : #if 0
3977 : Btree *pBt = db->aDb[pOp->p2].pBt;
3978 : if( db->xUpdateCallback && pOp->p3 ){
3979 : const char *zDb = db->aDb[pOp->p2].zName;
3980 : const char *zTbl = pOp->p3;
3981 : BtCursor *pCur = 0;
3982 : int fin = 0;
3983 :
3984 : rc = sqlite3BtreeCursor(pBt, pOp->p1, 0, 0, 0, &pCur);
3985 : if( rc!=SQLITE_OK ){
3986 : goto abort_due_to_error;
3987 : }
3988 : for(
3989 : rc=sqlite3BtreeFirst(pCur, &fin);
3990 : rc==SQLITE_OK && !fin;
3991 : rc=sqlite3BtreeNext(pCur, &fin)
3992 : ){
3993 : i64 iKey;
3994 : rc = sqlite3BtreeKeySize(pCur, &iKey);
3995 : if( rc ){
3996 : break;
3997 : }
3998 : iKey = keyToInt(iKey);
3999 : db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, iKey);
4000 : }
4001 : sqlite3BtreeCloseCursor(pCur);
4002 : if( rc!=SQLITE_OK ){
4003 : goto abort_due_to_error;
4004 : }
4005 : }
4006 : #endif
4007 4 : rc = sqlite3BtreeClearTable(db->aDb[pOp->p2].pBt, pOp->p1);
4008 4 : break;
4009 : }
4010 :
4011 : /* Opcode: CreateTable P1 * *
4012 : **
4013 : ** Allocate a new table in the main database file if P2==0 or in the
4014 : ** auxiliary database file if P2==1. Push the page number
4015 : ** for the root page of the new table onto the stack.
4016 : **
4017 : ** The difference between a table and an index is this: A table must
4018 : ** have a 4-byte integer key and can have arbitrary data. An index
4019 : ** has an arbitrary key but no data.
4020 : **
4021 : ** See also: CreateIndex
4022 : */
4023 : /* Opcode: CreateIndex P1 * *
4024 : **
4025 : ** Allocate a new index in the main database file if P2==0 or in the
4026 : ** auxiliary database file if P2==1. Push the page number of the
4027 : ** root page of the new index onto the stack.
4028 : **
4029 : ** See documentation on OP_CreateTable for additional information.
4030 : */
4031 : case OP_CreateIndex:
4032 : case OP_CreateTable: {
4033 : int pgno;
4034 : int flags;
4035 : Db *pDb;
4036 : assert( pOp->p1>=0 && pOp->p1<db->nDb );
4037 96 : pDb = &db->aDb[pOp->p1];
4038 : assert( pDb->pBt!=0 );
4039 96 : if( pOp->opcode==OP_CreateTable ){
4040 : /* flags = BTREE_INTKEY; */
4041 57 : flags = BTREE_LEAFDATA|BTREE_INTKEY;
4042 : }else{
4043 39 : flags = BTREE_ZERODATA;
4044 : }
4045 96 : rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
4046 96 : pTos++;
4047 96 : if( rc==SQLITE_OK ){
4048 96 : pTos->u.i = pgno;
4049 96 : pTos->flags = MEM_Int;
4050 : }else{
4051 0 : pTos->flags = MEM_Null;
4052 : }
4053 96 : break;
4054 : }
4055 :
4056 : /* Opcode: ParseSchema P1 P2 P3
4057 : **
4058 : ** Read and parse all entries from the SQLITE_MASTER table of database P1
4059 : ** that match the WHERE clause P3. P2 is the "force" flag. Always do
4060 : ** the parsing if P2 is true. If P2 is false, then this routine is a
4061 : ** no-op if the schema is not currently loaded. In other words, if P2
4062 : ** is false, the SQLITE_MASTER table is only parsed if the rest of the
4063 : ** schema is already loaded into the symbol table.
4064 : **
4065 : ** This opcode invokes the parser to create a new virtual machine,
4066 : ** then runs the new virtual machine. It is thus a reentrant opcode.
4067 : */
4068 : case OP_ParseSchema: { /* no-push */
4069 : char *zSql;
4070 57 : int iDb = pOp->p1;
4071 : const char *zMaster;
4072 : InitData initData;
4073 :
4074 : assert( iDb>=0 && iDb<db->nDb );
4075 57 : if( !pOp->p2 && !DbHasProperty(db, iDb, DB_SchemaLoaded) ){
4076 0 : break;
4077 : }
4078 57 : zMaster = SCHEMA_TABLE(iDb);
4079 57 : initData.db = db;
4080 57 : initData.iDb = pOp->p1;
4081 57 : initData.pzErrMsg = &p->zErrMsg;
4082 57 : zSql = sqlite3MPrintf(
4083 : "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s",
4084 : db->aDb[iDb].zName, zMaster, pOp->p3);
4085 57 : if( zSql==0 ) goto no_mem;
4086 57 : sqlite3SafetyOff(db);
4087 : assert( db->init.busy==0 );
4088 57 : db->init.busy = 1;
4089 : assert( !sqlite3MallocFailed() );
4090 57 : rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
4091 57 : if( rc==SQLITE_ABORT ) rc = initData.rc;
4092 57 : sqliteFree(zSql);
4093 57 : db->init.busy = 0;
4094 57 : sqlite3SafetyOn(db);
4095 57 : if( rc==SQLITE_NOMEM ){
4096 0 : sqlite3FailedMalloc();
4097 0 : goto no_mem;
4098 : }
4099 57 : break;
4100 : }
4101 :
4102 : #if !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER)
4103 : /* Opcode: LoadAnalysis P1 * *
4104 : **
4105 : ** Read the sqlite_stat1 table for database P1 and load the content
4106 : ** of that table into the internal index hash table. This will cause
4107 : ** the analysis to be used when preparing all subsequent queries.
4108 : */
4109 : case OP_LoadAnalysis: { /* no-push */
4110 0 : int iDb = pOp->p1;
4111 : assert( iDb>=0 && iDb<db->nDb );
4112 0 : sqlite3AnalysisLoad(db, iDb);
4113 0 : break;
4114 : }
4115 : #endif /* !defined(SQLITE_OMIT_ANALYZE) && !defined(SQLITE_OMIT_PARSER) */
4116 :
4117 : /* Opcode: DropTable P1 * P3
4118 : **
4119 : ** Remove the internal (in-memory) data structures that describe
4120 : ** the table named P3 in database P1. This is called after a table
4121 : ** is dropped in order to keep the internal representation of the
4122 : ** schema consistent with what is on disk.
4123 : */
4124 : case OP_DropTable: { /* no-push */
4125 4 : sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p3);
4126 4 : break;
4127 : }
4128 :
4129 : /* Opcode: DropIndex P1 * P3
4130 : **
4131 : ** Remove the internal (in-memory) data structures that describe
4132 : ** the index named P3 in database P1. This is called after an index
4133 : ** is dropped in order to keep the internal representation of the
4134 : ** schema consistent with what is on disk.
4135 : */
4136 : case OP_DropIndex: { /* no-push */
4137 0 : sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p3);
4138 0 : break;
4139 : }
4140 :
4141 : /* Opcode: DropTrigger P1 * P3
4142 : **
4143 : ** Remove the internal (in-memory) data structures that describe
4144 : ** the trigger named P3 in database P1. This is called after a trigger
4145 : ** is dropped in order to keep the internal representation of the
4146 : ** schema consistent with what is on disk.
4147 : */
4148 : case OP_DropTrigger: { /* no-push */
4149 0 : sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p3);
4150 0 : break;
4151 : }
4152 :
4153 :
4154 : #ifndef SQLITE_OMIT_INTEGRITY_CHECK
4155 : /* Opcode: IntegrityCk P1 P2 *
4156 : **
4157 : ** Do an analysis of the currently open database. Push onto the
4158 : ** stack the text of an error message describing any problems.
4159 : ** If no problems are found, push a NULL onto the stack.
4160 : **
4161 : ** P1 is the address of a memory cell that contains the maximum
4162 : ** number of allowed errors. At most mem[P1] errors will be reported.
4163 : ** In other words, the analysis stops as soon as mem[P1] errors are
4164 : ** seen. Mem[P1] is updated with the number of errors remaining.
4165 : **
4166 : ** The root page numbers of all tables in the database are integer
4167 : ** values on the stack. This opcode pulls as many integers as it
4168 : ** can off of the stack and uses those numbers as the root pages.
4169 : **
4170 : ** If P2 is not zero, the check is done on the auxiliary database
4171 : ** file, not the main database file.
4172 : **
4173 : ** This opcode is used to implement the integrity_check pragma.
4174 : */
4175 : case OP_IntegrityCk: {
4176 : int nRoot;
4177 : int *aRoot;
4178 : int j;
4179 : int nErr;
4180 : char *z;
4181 : Mem *pnErr;
4182 :
4183 0 : for(nRoot=0; &pTos[-nRoot]>=p->aStack; nRoot++){
4184 0 : if( (pTos[-nRoot].flags & MEM_Int)==0 ) break;
4185 : }
4186 : assert( nRoot>0 );
4187 0 : aRoot = sqliteMallocRaw( sizeof(int*)*(nRoot+1) );
4188 0 : if( aRoot==0 ) goto no_mem;
4189 0 : j = pOp->p1;
4190 : assert( j>=0 && j<p->nMem );
4191 0 : pnErr = &p->aMem[j];
4192 : assert( (pnErr->flags & MEM_Int)!=0 );
4193 0 : for(j=0; j<nRoot; j++){
4194 0 : Mem *pMem = &pTos[-j];
4195 0 : aRoot[j] = pMem->u.i;
4196 : }
4197 0 : aRoot[j] = 0;
4198 0 : popStack(&pTos, nRoot);
4199 0 : pTos++;
4200 0 : z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p2].pBt, aRoot, nRoot,
4201 : pnErr->u.i, &nErr);
4202 0 : pnErr->u.i -= nErr;
4203 0 : if( nErr==0 ){
4204 : assert( z==0 );
4205 0 : pTos->flags = MEM_Null;
4206 : }else{
4207 0 : pTos->z = z;
4208 0 : pTos->n = strlen(z);
4209 0 : pTos->flags = MEM_Str | MEM_Dyn | MEM_Term;
4210 0 : pTos->xDel = 0;
4211 : }
4212 0 : pTos->enc = SQLITE_UTF8;
4213 0 : sqlite3VdbeChangeEncoding(pTos, encoding);
4214 0 : sqliteFree(aRoot);
4215 0 : break;
4216 : }
4217 : #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
4218 :
4219 : /* Opcode: FifoWrite * * *
4220 : **
4221 : ** Write the integer on the top of the stack
4222 : ** into the Fifo.
4223 : */
4224 : case OP_FifoWrite: { /* no-push */
4225 : assert( pTos>=p->aStack );
4226 63 : sqlite3VdbeMemIntegerify(pTos);
4227 63 : sqlite3VdbeFifoPush(&p->sFifo, pTos->u.i);
4228 : assert( (pTos->flags & MEM_Dyn)==0 );
4229 63 : pTos--;
4230 63 : break;
4231 : }
4232 :
4233 : /* Opcode: FifoRead * P2 *
4234 : **
4235 : ** Attempt to read a single integer from the Fifo
4236 : ** and push it onto the stack. If the Fifo is empty
4237 : ** push nothing but instead jump to P2.
4238 : */
4239 : case OP_FifoRead: {
4240 : i64 v;
4241 130 : CHECK_FOR_INTERRUPT;
4242 130 : if( sqlite3VdbeFifoPop(&p->sFifo, &v)==SQLITE_DONE ){
4243 67 : pc = pOp->p2 - 1;
4244 : }else{
4245 63 : pTos++;
4246 63 : pTos->u.i = v;
4247 63 : pTos->flags = MEM_Int;
4248 : }
4249 130 : break;
4250 : }
4251 :
4252 : #ifndef SQLITE_OMIT_TRIGGER
4253 : /* Opcode: ContextPush * * *
4254 : **
4255 : ** Save the current Vdbe context such that it can be restored by a ContextPop
4256 : ** opcode. The context stores the last insert row id, the last statement change
4257 : ** count, and the current statement change count.
4258 : */
4259 : case OP_ContextPush: { /* no-push */
4260 0 : int i = p->contextStackTop++;
4261 : Context *pContext;
4262 :
4263 : assert( i>=0 );
4264 : /* FIX ME: This should be allocated as part of the vdbe at compile-time */
4265 0 : if( i>=p->contextStackDepth ){
4266 0 : p->contextStackDepth = i+1;
4267 0 : p->contextStack = sqliteReallocOrFree(p->contextStack,
4268 : sizeof(Context)*(i+1));
4269 0 : if( p->contextStack==0 ) goto no_mem;
4270 : }
4271 0 : pContext = &p->contextStack[i];
4272 0 : pContext->lastRowid = db->lastRowid;
4273 0 : pContext->nChange = p->nChange;
4274 0 : pContext->sFifo = p->sFifo;
4275 0 : sqlite3VdbeFifoInit(&p->sFifo);
4276 0 : break;
4277 : }
4278 :
4279 : /* Opcode: ContextPop * * *
4280 : **
4281 : ** Restore the Vdbe context to the state it was in when contextPush was last
4282 : ** executed. The context stores the last insert row id, the last statement
4283 : ** change count, and the current statement change count.
4284 : */
4285 : case OP_ContextPop: { /* no-push */
4286 0 : Context *pContext = &p->contextStack[--p->contextStackTop];
4287 : assert( p->contextStackTop>=0 );
4288 0 : db->lastRowid = pContext->lastRowid;
4289 0 : p->nChange = pContext->nChange;
4290 0 : sqlite3VdbeFifoClear(&p->sFifo);
4291 0 : p->sFifo = pContext->sFifo;
4292 0 : break;
4293 : }
4294 : #endif /* #ifndef SQLITE_OMIT_TRIGGER */
4295 :
4296 : /* Opcode: MemStore P1 P2 *
4297 : **
4298 : ** Write the top of the stack into memory location P1.
4299 : ** P1 should be a small integer since space is allocated
4300 : ** for all memory locations between 0 and P1 inclusive.
4301 : **
4302 : ** After the data is stored in the memory location, the
4303 : ** stack is popped once if P2 is 1. If P2 is zero, then
4304 : ** the original data remains on the stack.
4305 : */
4306 : case OP_MemStore: { /* no-push */
4307 : assert( pTos>=p->aStack );
4308 : assert( pOp->p1>=0 && pOp->p1<p->nMem );
4309 243 : rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos);
4310 243 : pTos--;
4311 :
4312 : /* If P2 is 0 then fall thru to the next opcode, OP_MemLoad, that will
4313 : ** restore the top of the stack to its original value.
4314 : */
4315 243 : if( pOp->p2 ){
4316 161 : break;
4317 : }
4318 : }
4319 : /* Opcode: MemLoad P1 * *
4320 : **
4321 : ** Push a copy of the value in memory location P1 onto the stack.
4322 : **
4323 : ** If the value is a string, then the value pushed is a pointer to
4324 : ** the string that is stored in the memory location. If the memory
4325 : ** location is subsequently changed (using OP_MemStore) then the
4326 : ** value pushed onto the stack will change too.
4327 : */
4328 : case OP_MemLoad: {
4329 303 : int i = pOp->p1;
4330 : assert( i>=0 && i<p->nMem );
4331 303 : pTos++;
4332 303 : sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem);
4333 303 : break;
4334 : }
4335 :
4336 : #ifndef SQLITE_OMIT_AUTOINCREMENT
4337 : /* Opcode: MemMax P1 * *
4338 : **
4339 : ** Set the value of memory cell P1 to the maximum of its current value
4340 : ** and the value on the top of the stack. The stack is unchanged.
4341 : **
4342 : ** This instruction throws an error if the memory cell is not initially
4343 : ** an integer.
4344 : */
4345 : case OP_MemMax: { /* no-push */
4346 0 : int i = pOp->p1;
4347 : Mem *pMem;
4348 : assert( pTos>=p->aStack );
4349 : assert( i>=0 && i<p->nMem );
4350 0 : pMem = &p->aMem[i];
4351 0 : sqlite3VdbeMemIntegerify(pMem);
4352 0 : sqlite3VdbeMemIntegerify(pTos);
4353 0 : if( pMem->u.i<pTos->u.i){
4354 0 : pMem->u.i = pTos->u.i;
4355 : }
4356 0 : break;
4357 : }
4358 : #endif /* SQLITE_OMIT_AUTOINCREMENT */
4359 :
4360 : /* Opcode: MemIncr P1 P2 *
4361 : **
4362 : ** Increment the integer valued memory cell P2 by the value in P1.
4363 : **
4364 : ** It is illegal to use this instruction on a memory cell that does
4365 : ** not contain an integer. An assertion fault will result if you try.
4366 : */
4367 : case OP_MemIncr: { /* no-push */
4368 0 : int i = pOp->p2;
4369 : Mem *pMem;
4370 : assert( i>=0 && i<p->nMem );
4371 0 : pMem = &p->aMem[i];
4372 : assert( pMem->flags==MEM_Int );
4373 0 : pMem->u.i += pOp->p1;
4374 0 : break;
4375 : }
4376 :
4377 : /* Opcode: IfMemPos P1 P2 *
4378 : **
4379 : ** If the value of memory cell P1 is 1 or greater, jump to P2.
4380 : **
4381 : ** It is illegal to use this instruction on a memory cell that does
4382 : ** not contain an integer. An assertion fault will result if you try.
4383 : */
4384 : case OP_IfMemPos: { /* no-push */
4385 35 : int i = pOp->p1;
4386 : Mem *pMem;
4387 : assert( i>=0 && i<p->nMem );
4388 35 : pMem = &p->aMem[i];
4389 : assert( pMem->flags==MEM_Int );
4390 35 : if( pMem->u.i>0 ){
4391 28 : pc = pOp->p2 - 1;
4392 : }
4393 35 : break;
4394 : }
4395 :
4396 : /* Opcode: IfMemNeg P1 P2 *
4397 : **
4398 : ** If the value of memory cell P1 is less than zero, jump to P2.
4399 : **
4400 : ** It is illegal to use this instruction on a memory cell that does
4401 : ** not contain an integer. An assertion fault will result if you try.
4402 : */
4403 : case OP_IfMemNeg: { /* no-push */
4404 0 : int i = pOp->p1;
4405 : Mem *pMem;
4406 : assert( i>=0 && i<p->nMem );
4407 0 : pMem = &p->aMem[i];
4408 : assert( pMem->flags==MEM_Int );
4409 0 : if( pMem->u.i<0 ){
4410 0 : pc = pOp->p2 - 1;
4411 : }
4412 0 : break;
4413 : }
4414 :
4415 : /* Opcode: IfMemZero P1 P2 *
4416 : **
4417 : ** If the value of memory cell P1 is exactly 0, jump to P2.
4418 : **
4419 : ** It is illegal to use this instruction on a memory cell that does
4420 : ** not contain an integer. An assertion fault will result if you try.
4421 : */
4422 : case OP_IfMemZero: { /* no-push */
4423 0 : int i = pOp->p1;
4424 : Mem *pMem;
4425 : assert( i>=0 && i<p->nMem );
4426 0 : pMem = &p->aMem[i];
4427 : assert( pMem->flags==MEM_Int );
4428 0 : if( pMem->u.i==0 ){
4429 0 : pc = pOp->p2 - 1;
4430 : }
4431 0 : break;
4432 : }
4433 :
4434 : /* Opcode: MemNull P1 * *
4435 : **
4436 : ** Store a NULL in memory cell P1
4437 : */
4438 : case OP_MemNull: {
4439 : assert( pOp->p1>=0 && pOp->p1<p->nMem );
4440 20 : sqlite3VdbeMemSetNull(&p->aMem[pOp->p1]);
4441 20 : break;
4442 : }
4443 :
4444 : /* Opcode: MemInt P1 P2 *
4445 : **
4446 : ** Store the integer value P1 in memory cell P2.
4447 : */
4448 : case OP_MemInt: {
4449 : assert( pOp->p2>=0 && pOp->p2<p->nMem );
4450 58 : sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1);
4451 58 : break;
4452 : }
4453 :
4454 : /* Opcode: MemMove P1 P2 *
4455 : **
4456 : ** Move the content of memory cell P2 over to memory cell P1.
4457 : ** Any prior content of P1 is erased. Memory cell P2 is left
4458 : ** containing a NULL.
4459 : */
4460 : case OP_MemMove: {
4461 : assert( pOp->p1>=0 && pOp->p1<p->nMem );
4462 : assert( pOp->p2>=0 && pOp->p2<p->nMem );
4463 0 : rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]);
4464 0 : break;
4465 : }
4466 :
4467 : /* Opcode: AggStep P1 P2 P3
4468 : **
4469 : ** Execute the step function for an aggregate. The
4470 : ** function has P2 arguments. P3 is a pointer to the FuncDef
4471 : ** structure that specifies the function. Use memory location
4472 : ** P1 as the accumulator.
4473 : **
4474 : ** The P2 arguments are popped from the stack.
4475 : */
4476 : case OP_AggStep: { /* no-push */
4477 45 : int n = pOp->p2;
4478 : int i;
4479 : Mem *pMem, *pRec;
4480 : sqlite3_context ctx;
4481 : sqlite3_value **apVal;
4482 :
4483 : assert( n>=0 );
4484 45 : pRec = &pTos[1-n];
4485 : assert( pRec>=p->aStack );
4486 45 : apVal = p->apArg;
4487 : assert( apVal || n==0 );
4488 71 : for(i=0; i<n; i++, pRec++){
4489 26 : apVal[i] = pRec;
4490 26 : storeTypeInfo(pRec, encoding);
4491 : }
4492 45 : ctx.pFunc = (FuncDef*)pOp->p3;
4493 : assert( pOp->p1>=0 && pOp->p1<p->nMem );
4494 45 : ctx.pMem = pMem = &p->aMem[pOp->p1];
4495 45 : pMem->n++;
4496 45 : ctx.s.flags = MEM_Null;
4497 45 : ctx.s.z = 0;
4498 45 : ctx.s.xDel = 0;
4499 45 : ctx.isError = 0;
4500 45 : ctx.pColl = 0;
4501 45 : if( ctx.pFunc->needCollSeq ){
4502 : assert( pOp>p->aOp );
4503 : assert( pOp[-1].p3type==P3_COLLSEQ );
4504 : assert( pOp[-1].opcode==OP_CollSeq );
4505 0 : ctx.pColl = (CollSeq *)pOp[-1].p3;
4506 : }
4507 45 : (ctx.pFunc->xStep)(&ctx, n, apVal);
4508 45 : popStack(&pTos, n);
4509 45 : if( ctx.isError ){
4510 0 : sqlite3SetString(&p->zErrMsg, sqlite3_value_text(&ctx.s), (char*)0);
4511 0 : rc = SQLITE_ERROR;
4512 : }
4513 45 : sqlite3VdbeMemRelease(&ctx.s);
4514 45 : break;
4515 : }
4516 :
4517 : /* Opcode: AggFinal P1 P2 P3
4518 : **
4519 : ** Execute the finalizer function for an aggregate. P1 is
4520 : ** the memory location that is the accumulator for the aggregate.
4521 : **
4522 : ** P2 is the number of arguments that the step function takes and
4523 : ** P3 is a pointer to the FuncDef for this function. The P2
4524 : ** argument is not used by this opcode. It is only there to disambiguate
4525 : ** functions that can take varying numbers of arguments. The
4526 : ** P3 argument is only needed for the degenerate case where
4527 : ** the step function was not previously called.
4528 : */
4529 : case OP_AggFinal: { /* no-push */
4530 : Mem *pMem;
4531 : assert( pOp->p1>=0 && pOp->p1<p->nMem );
4532 15 : pMem = &p->aMem[pOp->p1];
4533 : assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
4534 15 : rc = sqlite3VdbeMemFinalize(pMem, (FuncDef*)pOp->p3);
4535 15 : if( rc==SQLITE_ERROR ){
4536 0 : sqlite3SetString(&p->zErrMsg, sqlite3_value_text(pMem), (char*)0);
4537 : }
4538 15 : break;
4539 : }
4540 :
4541 :
4542 : #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
4543 : /* Opcode: Vacuum * * *
4544 : **
4545 : ** Vacuum the entire database. This opcode will cause other virtual
4546 : ** machines to be created and run. It may not be called from within
4547 : ** a transaction.
4548 : */
4549 : case OP_Vacuum: { /* no-push */
4550 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4551 0 : rc = sqlite3RunVacuum(&p->zErrMsg, db);
4552 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4553 0 : break;
4554 : }
4555 : #endif
4556 :
4557 : /* Opcode: Expire P1 * *
4558 : **
4559 : ** Cause precompiled statements to become expired. An expired statement
4560 : ** fails with an error code of SQLITE_SCHEMA if it is ever executed
4561 : ** (via sqlite3_step()).
4562 : **
4563 : ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
4564 : ** then only the currently executing statement is affected.
4565 : */
4566 : case OP_Expire: { /* no-push */
4567 0 : if( !pOp->p1 ){
4568 0 : sqlite3ExpirePreparedStatements(db);
4569 : }else{
4570 0 : p->expired = 1;
4571 : }
4572 0 : break;
4573 : }
4574 :
4575 : #ifndef SQLITE_OMIT_SHARED_CACHE
4576 : /* Opcode: TableLock P1 P2 P3
4577 : **
4578 : ** Obtain a lock on a particular table. This instruction is only used when
4579 : ** the shared-cache feature is enabled.
4580 : **
4581 : ** If P1 is not negative, then it is the index of the database
4582 : ** in sqlite3.aDb[] and a read-lock is required. If P1 is negative, a
4583 : ** write-lock is required. In this case the index of the database is the
4584 : ** absolute value of P1 minus one (iDb = abs(P1) - 1;) and a write-lock is
4585 : ** required.
4586 : **
4587 : ** P2 contains the root-page of the table to lock.
4588 : **
4589 : ** P3 contains a pointer to the name of the table being locked. This is only
4590 : ** used to generate an error message if the lock cannot be obtained.
4591 : */
4592 : case OP_TableLock: { /* no-push */
4593 0 : int p1 = pOp->p1;
4594 0 : u8 isWriteLock = (p1<0);
4595 0 : if( isWriteLock ){
4596 0 : p1 = (-1*p1)-1;
4597 : }
4598 0 : rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
4599 0 : if( rc==SQLITE_LOCKED ){
4600 0 : const char *z = (const char *)pOp->p3;
4601 0 : sqlite3SetString(&p->zErrMsg, "database table is locked: ", z, (char*)0);
4602 : }
4603 0 : break;
4604 : }
4605 : #endif /* SQLITE_OMIT_SHARED_CACHE */
4606 :
4607 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4608 : /* Opcode: VBegin * * P3
4609 : **
4610 : ** P3 a pointer to an sqlite3_vtab structure. Call the xBegin method
4611 : ** for that table.
4612 : */
4613 : case OP_VBegin: { /* no-push */
4614 0 : rc = sqlite3VtabBegin(db, (sqlite3_vtab *)pOp->p3);
4615 0 : break;
4616 : }
4617 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4618 :
4619 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4620 : /* Opcode: VCreate P1 * P3
4621 : **
4622 : ** P3 is the name of a virtual table in database P1. Call the xCreate method
4623 : ** for that table.
4624 : */
4625 : case OP_VCreate: { /* no-push */
4626 0 : rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p3, &p->zErrMsg);
4627 0 : break;
4628 : }
4629 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4630 :
4631 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4632 : /* Opcode: VDestroy P1 * P3
4633 : **
4634 : ** P3 is the name of a virtual table in database P1. Call the xDestroy method
4635 : ** of that table.
4636 : */
4637 : case OP_VDestroy: { /* no-push */
4638 0 : p->inVtabMethod = 2;
4639 0 : rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p3);
4640 0 : p->inVtabMethod = 0;
4641 0 : break;
4642 : }
4643 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4644 :
4645 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4646 : /* Opcode: VOpen P1 * P3
4647 : **
4648 : ** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
4649 : ** P1 is a cursor number. This opcode opens a cursor to the virtual
4650 : ** table and stores that cursor in P1.
4651 : */
4652 : case OP_VOpen: { /* no-push */
4653 0 : Cursor *pCur = 0;
4654 0 : sqlite3_vtab_cursor *pVtabCursor = 0;
4655 :
4656 0 : sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
4657 0 : sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4658 :
4659 : assert(pVtab && pModule);
4660 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4661 0 : rc = pModule->xOpen(pVtab, &pVtabCursor);
4662 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4663 0 : if( SQLITE_OK==rc ){
4664 : /* Initialise sqlite3_vtab_cursor base class */
4665 0 : pVtabCursor->pVtab = pVtab;
4666 :
4667 : /* Initialise vdbe cursor object */
4668 0 : pCur = allocateCursor(p, pOp->p1, -1);
4669 0 : if( pCur ){
4670 0 : pCur->pVtabCursor = pVtabCursor;
4671 0 : pCur->pModule = pVtabCursor->pVtab->pModule;
4672 : }else{
4673 0 : pModule->xClose(pVtabCursor);
4674 : }
4675 : }
4676 0 : break;
4677 : }
4678 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4679 :
4680 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4681 : /* Opcode: VFilter P1 P2 P3
4682 : **
4683 : ** P1 is a cursor opened using VOpen. P2 is an address to jump to if
4684 : ** the filtered result set is empty.
4685 : **
4686 : ** P3 is either NULL or a string that was generated by the xBestIndex
4687 : ** method of the module. The interpretation of the P3 string is left
4688 : ** to the module implementation.
4689 : **
4690 : ** This opcode invokes the xFilter method on the virtual table specified
4691 : ** by P1. The integer query plan parameter to xFilter is the top of the
4692 : ** stack. Next down on the stack is the argc parameter. Beneath the
4693 : ** next of stack are argc additional parameters which are passed to
4694 : ** xFilter as argv. The topmost parameter (i.e. 3rd element popped from
4695 : ** the stack) becomes argv[argc-1] when passed to xFilter.
4696 : **
4697 : ** The integer query plan parameter, argc, and all argv stack values
4698 : ** are popped from the stack before this instruction completes.
4699 : **
4700 : ** A jump is made to P2 if the result set after filtering would be
4701 : ** empty.
4702 : */
4703 : case OP_VFilter: { /* no-push */
4704 : int nArg;
4705 :
4706 : const sqlite3_module *pModule;
4707 :
4708 0 : Cursor *pCur = p->apCsr[pOp->p1];
4709 : assert( pCur->pVtabCursor );
4710 0 : pModule = pCur->pVtabCursor->pVtab->pModule;
4711 :
4712 : /* Grab the index number and argc parameters off the top of the stack. */
4713 : assert( (&pTos[-1])>=p->aStack );
4714 : assert( (pTos[0].flags&MEM_Int)!=0 && pTos[-1].flags==MEM_Int );
4715 0 : nArg = pTos[-1].u.i;
4716 :
4717 : /* Invoke the xFilter method */
4718 : {
4719 0 : int res = 0;
4720 : int i;
4721 0 : Mem **apArg = p->apArg;
4722 0 : for(i = 0; i<nArg; i++){
4723 0 : apArg[i] = &pTos[i+1-2-nArg];
4724 0 : storeTypeInfo(apArg[i], 0);
4725 : }
4726 :
4727 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4728 0 : p->inVtabMethod = 1;
4729 0 : rc = pModule->xFilter(pCur->pVtabCursor, pTos->u.i, pOp->p3, nArg, apArg);
4730 0 : p->inVtabMethod = 0;
4731 0 : if( rc==SQLITE_OK ){
4732 0 : res = pModule->xEof(pCur->pVtabCursor);
4733 : }
4734 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4735 :
4736 0 : if( res ){
4737 0 : pc = pOp->p2 - 1;
4738 : }
4739 : }
4740 :
4741 : /* Pop the index number, argc value and parameters off the stack */
4742 0 : popStack(&pTos, 2+nArg);
4743 0 : break;
4744 : }
4745 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4746 :
4747 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4748 : /* Opcode: VRowid P1 * *
4749 : **
4750 : ** Push an integer onto the stack which is the rowid of
4751 : ** the virtual-table that the P1 cursor is pointing to.
4752 : */
4753 : case OP_VRowid: {
4754 : const sqlite3_module *pModule;
4755 :
4756 0 : Cursor *pCur = p->apCsr[pOp->p1];
4757 : assert( pCur->pVtabCursor );
4758 0 : pModule = pCur->pVtabCursor->pVtab->pModule;
4759 0 : if( pModule->xRowid==0 ){
4760 0 : sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xRowid", 0);
4761 0 : rc = SQLITE_ERROR;
4762 : } else {
4763 : sqlite_int64 iRow;
4764 :
4765 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4766 0 : rc = pModule->xRowid(pCur->pVtabCursor, &iRow);
4767 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4768 :
4769 0 : pTos++;
4770 0 : pTos->flags = MEM_Int;
4771 0 : pTos->u.i = iRow;
4772 : }
4773 :
4774 0 : break;
4775 : }
4776 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4777 :
4778 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4779 : /* Opcode: VColumn P1 P2 *
4780 : **
4781 : ** Push onto the stack the value of the P2-th column of
4782 : ** the row of the virtual-table that the P1 cursor is pointing to.
4783 : */
4784 : case OP_VColumn: {
4785 : const sqlite3_module *pModule;
4786 :
4787 0 : Cursor *pCur = p->apCsr[pOp->p1];
4788 : assert( pCur->pVtabCursor );
4789 0 : pModule = pCur->pVtabCursor->pVtab->pModule;
4790 0 : if( pModule->xColumn==0 ){
4791 0 : sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xColumn", 0);
4792 0 : rc = SQLITE_ERROR;
4793 : } else {
4794 : sqlite3_context sContext;
4795 0 : memset(&sContext, 0, sizeof(sContext));
4796 0 : sContext.s.flags = MEM_Null;
4797 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4798 0 : rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
4799 :
4800 : /* Copy the result of the function to the top of the stack. We
4801 : ** do this regardless of whether or not an error occured to ensure any
4802 : ** dynamic allocation in sContext.s (a Mem struct) is released.
4803 : */
4804 0 : sqlite3VdbeChangeEncoding(&sContext.s, encoding);
4805 0 : pTos++;
4806 0 : pTos->flags = 0;
4807 0 : sqlite3VdbeMemMove(pTos, &sContext.s);
4808 :
4809 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4810 : }
4811 :
4812 0 : break;
4813 : }
4814 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4815 :
4816 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4817 : /* Opcode: VNext P1 P2 *
4818 : **
4819 : ** Advance virtual table P1 to the next row in its result set and
4820 : ** jump to instruction P2. Or, if the virtual table has reached
4821 : ** the end of its result set, then fall through to the next instruction.
4822 : */
4823 : case OP_VNext: { /* no-push */
4824 : const sqlite3_module *pModule;
4825 0 : int res = 0;
4826 :
4827 0 : Cursor *pCur = p->apCsr[pOp->p1];
4828 : assert( pCur->pVtabCursor );
4829 0 : pModule = pCur->pVtabCursor->pVtab->pModule;
4830 0 : if( pModule->xNext==0 ){
4831 0 : sqlite3SetString(&p->zErrMsg, "Unsupported module operation: xNext", 0);
4832 0 : rc = SQLITE_ERROR;
4833 : } else {
4834 : /* Invoke the xNext() method of the module. There is no way for the
4835 : ** underlying implementation to return an error if one occurs during
4836 : ** xNext(). Instead, if an error occurs, true is returned (indicating that
4837 : ** data is available) and the error code returned when xColumn or
4838 : ** some other method is next invoked on the save virtual table cursor.
4839 : */
4840 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4841 0 : p->inVtabMethod = 1;
4842 0 : rc = pModule->xNext(pCur->pVtabCursor);
4843 0 : p->inVtabMethod = 0;
4844 0 : if( rc==SQLITE_OK ){
4845 0 : res = pModule->xEof(pCur->pVtabCursor);
4846 : }
4847 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4848 :
4849 0 : if( !res ){
4850 : /* If there is data, jump to P2 */
4851 0 : pc = pOp->p2 - 1;
4852 : }
4853 : }
4854 :
4855 0 : break;
4856 : }
4857 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4858 :
4859 :
4860 : #ifndef SQLITE_OMIT_VIRTUALTABLE
4861 : /* Opcode: VUpdate P1 P2 P3
4862 : **
4863 : ** P3 is a pointer to a virtual table object, an sqlite3_vtab structure.
4864 : ** This opcode invokes the corresponding xUpdate method. P2 values
4865 : ** are taken from the stack to pass to the xUpdate invocation. The
4866 : ** value on the top of the stack corresponds to the p2th element
4867 : ** of the argv array passed to xUpdate.
4868 : **
4869 : ** The xUpdate method will do a DELETE or an INSERT or both.
4870 : ** The argv[0] element (which corresponds to the P2-th element down
4871 : ** on the stack) is the rowid of a row to delete. If argv[0] is
4872 : ** NULL then no deletion occurs. The argv[1] element is the rowid
4873 : ** of the new row. This can be NULL to have the virtual table
4874 : ** select the new rowid for itself. The higher elements in the
4875 : ** stack are the values of columns in the new row.
4876 : **
4877 : ** If P2==1 then no insert is performed. argv[0] is the rowid of
4878 : ** a row to delete.
4879 : **
4880 : ** P1 is a boolean flag. If it is set to true and the xUpdate call
4881 : ** is successful, then the value returned by sqlite3_last_insert_rowid()
4882 : ** is set to the value of the rowid for the row just inserted.
4883 : */
4884 : case OP_VUpdate: { /* no-push */
4885 0 : sqlite3_vtab *pVtab = (sqlite3_vtab *)(pOp->p3);
4886 0 : sqlite3_module *pModule = (sqlite3_module *)pVtab->pModule;
4887 0 : int nArg = pOp->p2;
4888 : assert( pOp->p3type==P3_VTAB );
4889 0 : if( pModule->xUpdate==0 ){
4890 0 : sqlite3SetString(&p->zErrMsg, "read-only table", 0);
4891 0 : rc = SQLITE_ERROR;
4892 : }else{
4893 : int i;
4894 : sqlite_int64 rowid;
4895 0 : Mem **apArg = p->apArg;
4896 0 : Mem *pX = &pTos[1-nArg];
4897 0 : for(i = 0; i<nArg; i++, pX++){
4898 0 : storeTypeInfo(pX, 0);
4899 0 : apArg[i] = pX;
4900 : }
4901 0 : if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
4902 0 : sqlite3VtabLock(pVtab);
4903 0 : rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
4904 0 : sqlite3VtabUnlock(db, pVtab);
4905 0 : if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
4906 0 : if( pOp->p1 && rc==SQLITE_OK ){
4907 : assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
4908 0 : db->lastRowid = rowid;
4909 : }
4910 : }
4911 0 : popStack(&pTos, nArg);
4912 : break;
4913 : }
4914 : #endif /* SQLITE_OMIT_VIRTUALTABLE */
4915 :
4916 : /* An other opcode is illegal...
4917 : */
4918 : default: {
4919 : assert( 0 );
4920 : break;
4921 : }
4922 :
4923 : /*****************************************************************************
4924 : ** The cases of the switch statement above this line should all be indented
4925 : ** by 6 spaces. But the left-most 6 spaces have been removed to improve the
4926 : ** readability. From this point on down, the normal indentation rules are
4927 : ** restored.
4928 : *****************************************************************************/
4929 : }
4930 :
4931 : /* Make sure the stack limit was not exceeded */
4932 : assert( pTos<=pStackLimit );
4933 :
4934 : #ifdef VDBE_PROFILE
4935 : {
4936 : long long elapse = hwtime() - start;
4937 : pOp->cycles += elapse;
4938 : pOp->cnt++;
4939 : #if 0
4940 : fprintf(stdout, "%10lld ", elapse);
4941 : sqlite3VdbePrintOp(stdout, origPc, &p->aOp[origPc]);
4942 : #endif
4943 : }
4944 : #endif
4945 :
4946 : /* The following code adds nothing to the actual functionality
4947 : ** of the program. It is only here for testing and debugging.
4948 : ** On the other hand, it does burn CPU cycles every time through
4949 : ** the evaluator loop. So we can leave it out when NDEBUG is defined.
4950 : */
4951 : #ifndef NDEBUG
4952 : /* Sanity checking on the top element of the stack. If the previous
4953 : ** instruction was VNoChange, then the flags field of the top
4954 : ** of the stack is set to 0. This is technically invalid for a memory
4955 : ** cell, so avoid calling MemSanity() in this case.
4956 : */
4957 : if( pTos>=p->aStack && pTos->flags ){
4958 : sqlite3VdbeMemSanity(pTos);
4959 : }
4960 : assert( pc>=-1 && pc<p->nOp );
4961 : #ifdef SQLITE_DEBUG
4962 : /* Code for tracing the vdbe stack. */
4963 : if( p->trace && pTos>=p->aStack ){
4964 : int i;
4965 : fprintf(p->trace, "Stack:");
4966 : for(i=0; i>-5 && &pTos[i]>=p->aStack; i--){
4967 : if( pTos[i].flags & MEM_Null ){
4968 : fprintf(p->trace, " NULL");
4969 : }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
4970 : fprintf(p->trace, " si:%lld", pTos[i].u.i);
4971 : }else if( pTos[i].flags & MEM_Int ){
4972 : fprintf(p->trace, " i:%lld", pTos[i].u.i);
4973 : }else if( pTos[i].flags & MEM_Real ){
4974 : fprintf(p->trace, " r:%g", pTos[i].r);
4975 : }else{
4976 : char zBuf[100];
4977 : sqlite3VdbeMemPrettyPrint(&pTos[i], zBuf);
4978 : fprintf(p->trace, " ");
4979 : fprintf(p->trace, "%s", zBuf);
4980 : }
4981 : }
4982 : if( rc!=0 ) fprintf(p->trace," rc=%d",rc);
4983 : fprintf(p->trace,"\n");
4984 : }
4985 : #endif /* SQLITE_DEBUG */
4986 : #endif /* NDEBUG */
4987 : } /* The end of the for(;;) loop the loops through opcodes */
4988 :
4989 : /* If we reach this point, it means that execution is finished.
4990 : */
4991 1 : vdbe_halt:
4992 1 : if( rc ){
4993 1 : p->rc = rc;
4994 1 : rc = SQLITE_ERROR;
4995 : }else{
4996 0 : rc = SQLITE_DONE;
4997 : }
4998 1 : sqlite3VdbeHalt(p);
4999 1 : p->pTos = pTos;
5000 1 : return rc;
5001 :
5002 : /* Jump to here if a malloc() fails. It's hard to get a malloc()
5003 : ** to fail on a modern VM computer, so this code is untested.
5004 : */
5005 0 : no_mem:
5006 0 : sqlite3SetString(&p->zErrMsg, "out of memory", (char*)0);
5007 0 : rc = SQLITE_NOMEM;
5008 0 : goto vdbe_halt;
5009 :
5010 : /* Jump to here for an SQLITE_MISUSE error.
5011 : */
5012 0 : abort_due_to_misuse:
5013 0 : rc = SQLITE_MISUSE;
5014 : /* Fall thru into abort_due_to_error */
5015 :
5016 : /* Jump to here for any other kind of fatal error. The "rc" variable
5017 : ** should hold the error number.
5018 : */
5019 0 : abort_due_to_error:
5020 0 : if( p->zErrMsg==0 ){
5021 0 : if( sqlite3MallocFailed() ) rc = SQLITE_NOMEM;
5022 0 : sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
5023 : }
5024 0 : goto vdbe_halt;
5025 :
5026 : /* Jump to here if the sqlite3_interrupt() API sets the interrupt
5027 : ** flag.
5028 : */
5029 0 : abort_due_to_interrupt:
5030 : assert( db->u1.isInterrupted );
5031 0 : if( db->magic!=SQLITE_MAGIC_BUSY ){
5032 0 : rc = SQLITE_MISUSE;
5033 : }else{
5034 0 : rc = SQLITE_INTERRUPT;
5035 : }
5036 0 : p->rc = rc;
5037 0 : sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(rc), (char*)0);
5038 0 : goto vdbe_halt;
5039 : }
|