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