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