OLD | NEW |
| (Empty) |
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 the function that runs the | |
13 ** bytecode of a prepared statement. | |
14 ** | |
15 ** Various scripts scan this source file in order to generate HTML | |
16 ** documentation, headers files, or other derived files. The formatting | |
17 ** of the code in this file is, therefore, important. See other comments | |
18 ** in this file for details. If in doubt, do not deviate from existing | |
19 ** commenting and indentation practices when changing or adding code. | |
20 */ | |
21 #include "sqliteInt.h" | |
22 #include "vdbeInt.h" | |
23 | |
24 /* | |
25 ** Invoke this macro on memory cells just prior to changing the | |
26 ** value of the cell. This macro verifies that shallow copies are | |
27 ** not misused. A shallow copy of a string or blob just copies a | |
28 ** pointer to the string or blob, not the content. If the original | |
29 ** is changed while the copy is still in use, the string or blob might | |
30 ** be changed out from under the copy. This macro verifies that nothing | |
31 ** like that ever happens. | |
32 */ | |
33 #ifdef SQLITE_DEBUG | |
34 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M) | |
35 #else | |
36 # define memAboutToChange(P,M) | |
37 #endif | |
38 | |
39 /* | |
40 ** The following global variable is incremented every time a cursor | |
41 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes. The test | |
42 ** procedures use this information to make sure that indices are | |
43 ** working correctly. This variable has no function other than to | |
44 ** help verify the correct operation of the library. | |
45 */ | |
46 #ifdef SQLITE_TEST | |
47 int sqlite3_search_count = 0; | |
48 #endif | |
49 | |
50 /* | |
51 ** When this global variable is positive, it gets decremented once before | |
52 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted | |
53 ** field of the sqlite3 structure is set in order to simulate an interrupt. | |
54 ** | |
55 ** This facility is used for testing purposes only. It does not function | |
56 ** in an ordinary build. | |
57 */ | |
58 #ifdef SQLITE_TEST | |
59 int sqlite3_interrupt_count = 0; | |
60 #endif | |
61 | |
62 /* | |
63 ** The next global variable is incremented each type the OP_Sort opcode | |
64 ** is executed. The test procedures use this information to make sure that | |
65 ** sorting is occurring or not occurring at appropriate times. This variable | |
66 ** has no function other than to help verify the correct operation of the | |
67 ** library. | |
68 */ | |
69 #ifdef SQLITE_TEST | |
70 int sqlite3_sort_count = 0; | |
71 #endif | |
72 | |
73 /* | |
74 ** The next global variable records the size of the largest MEM_Blob | |
75 ** or MEM_Str that has been used by a VDBE opcode. The test procedures | |
76 ** use this information to make sure that the zero-blob functionality | |
77 ** is working correctly. This variable has no function other than to | |
78 ** help verify the correct operation of the library. | |
79 */ | |
80 #ifdef SQLITE_TEST | |
81 int sqlite3_max_blobsize = 0; | |
82 static void updateMaxBlobsize(Mem *p){ | |
83 if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){ | |
84 sqlite3_max_blobsize = p->n; | |
85 } | |
86 } | |
87 #endif | |
88 | |
89 /* | |
90 ** The next global variable is incremented each time the OP_Found opcode | |
91 ** is executed. This is used to test whether or not the foreign key | |
92 ** operation implemented using OP_FkIsZero is working. This variable | |
93 ** has no function other than to help verify the correct operation of the | |
94 ** library. | |
95 */ | |
96 #ifdef SQLITE_TEST | |
97 int sqlite3_found_count = 0; | |
98 #endif | |
99 | |
100 /* | |
101 ** Test a register to see if it exceeds the current maximum blob size. | |
102 ** If it does, record the new maximum blob size. | |
103 */ | |
104 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST) | |
105 # define UPDATE_MAX_BLOBSIZE(P) updateMaxBlobsize(P) | |
106 #else | |
107 # define UPDATE_MAX_BLOBSIZE(P) | |
108 #endif | |
109 | |
110 /* | |
111 ** Invoke the VDBE coverage callback, if that callback is defined. This | |
112 ** feature is used for test suite validation only and does not appear an | |
113 ** production builds. | |
114 ** | |
115 ** M is an integer, 2 or 3, that indices how many different ways the | |
116 ** branch can go. It is usually 2. "I" is the direction the branch | |
117 ** goes. 0 means falls through. 1 means branch is taken. 2 means the | |
118 ** second alternative branch is taken. | |
119 ** | |
120 ** iSrcLine is the source code line (from the __LINE__ macro) that | |
121 ** generated the VDBE instruction. This instrumentation assumes that all | |
122 ** source code is in a single file (the amalgamation). Special values 1 | |
123 ** and 2 for the iSrcLine parameter mean that this particular branch is | |
124 ** always taken or never taken, respectively. | |
125 */ | |
126 #if !defined(SQLITE_VDBE_COVERAGE) | |
127 # define VdbeBranchTaken(I,M) | |
128 #else | |
129 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M) | |
130 static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){ | |
131 if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){ | |
132 M = iSrcLine; | |
133 /* Assert the truth of VdbeCoverageAlwaysTaken() and | |
134 ** VdbeCoverageNeverTaken() */ | |
135 assert( (M & I)==I ); | |
136 }else{ | |
137 if( sqlite3GlobalConfig.xVdbeBranch==0 ) return; /*NO_TEST*/ | |
138 sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, | |
139 iSrcLine,I,M); | |
140 } | |
141 } | |
142 #endif | |
143 | |
144 /* | |
145 ** Convert the given register into a string if it isn't one | |
146 ** already. Return non-zero if a malloc() fails. | |
147 */ | |
148 #define Stringify(P, enc) \ | |
149 if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \ | |
150 { goto no_mem; } | |
151 | |
152 /* | |
153 ** An ephemeral string value (signified by the MEM_Ephem flag) contains | |
154 ** a pointer to a dynamically allocated string where some other entity | |
155 ** is responsible for deallocating that string. Because the register | |
156 ** does not control the string, it might be deleted without the register | |
157 ** knowing it. | |
158 ** | |
159 ** This routine converts an ephemeral string into a dynamically allocated | |
160 ** string that the register itself controls. In other words, it | |
161 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc. | |
162 */ | |
163 #define Deephemeralize(P) \ | |
164 if( ((P)->flags&MEM_Ephem)!=0 \ | |
165 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;} | |
166 | |
167 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */ | |
168 #define isSorter(x) ((x)->pSorter!=0) | |
169 | |
170 /* | |
171 ** Allocate VdbeCursor number iCur. Return a pointer to it. Return NULL | |
172 ** if we run out of memory. | |
173 */ | |
174 static VdbeCursor *allocateCursor( | |
175 Vdbe *p, /* The virtual machine */ | |
176 int iCur, /* Index of the new VdbeCursor */ | |
177 int nField, /* Number of fields in the table or index */ | |
178 int iDb, /* Database the cursor belongs to, or -1 */ | |
179 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */ | |
180 ){ | |
181 /* Find the memory cell that will be used to store the blob of memory | |
182 ** required for this VdbeCursor structure. It is convenient to use a | |
183 ** vdbe memory cell to manage the memory allocation required for a | |
184 ** VdbeCursor structure for the following reasons: | |
185 ** | |
186 ** * Sometimes cursor numbers are used for a couple of different | |
187 ** purposes in a vdbe program. The different uses might require | |
188 ** different sized allocations. Memory cells provide growable | |
189 ** allocations. | |
190 ** | |
191 ** * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can | |
192 ** be freed lazily via the sqlite3_release_memory() API. This | |
193 ** minimizes the number of malloc calls made by the system. | |
194 ** | |
195 ** Memory cells for cursors are allocated at the top of the address | |
196 ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for | |
197 ** cursor 1 is managed by memory cell (p->nMem-1), etc. | |
198 */ | |
199 Mem *pMem = &p->aMem[p->nMem-iCur]; | |
200 | |
201 int nByte; | |
202 VdbeCursor *pCx = 0; | |
203 nByte = | |
204 ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + | |
205 (isBtreeCursor?sqlite3BtreeCursorSize():0); | |
206 | |
207 assert( iCur<p->nCursor ); | |
208 if( p->apCsr[iCur] ){ | |
209 sqlite3VdbeFreeCursor(p, p->apCsr[iCur]); | |
210 p->apCsr[iCur] = 0; | |
211 } | |
212 if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){ | |
213 p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z; | |
214 memset(pCx, 0, sizeof(VdbeCursor)); | |
215 pCx->iDb = iDb; | |
216 pCx->nField = nField; | |
217 pCx->aOffset = &pCx->aType[nField]; | |
218 if( isBtreeCursor ){ | |
219 pCx->pCursor = (BtCursor*) | |
220 &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField]; | |
221 sqlite3BtreeCursorZero(pCx->pCursor); | |
222 } | |
223 } | |
224 return pCx; | |
225 } | |
226 | |
227 /* | |
228 ** Try to convert a value into a numeric representation if we can | |
229 ** do so without loss of information. In other words, if the string | |
230 ** looks like a number, convert it into a number. If it does not | |
231 ** look like a number, leave it alone. | |
232 ** | |
233 ** If the bTryForInt flag is true, then extra effort is made to give | |
234 ** an integer representation. Strings that look like floating point | |
235 ** values but which have no fractional component (example: '48.00') | |
236 ** will have a MEM_Int representation when bTryForInt is true. | |
237 ** | |
238 ** If bTryForInt is false, then if the input string contains a decimal | |
239 ** point or exponential notation, the result is only MEM_Real, even | |
240 ** if there is an exact integer representation of the quantity. | |
241 */ | |
242 static void applyNumericAffinity(Mem *pRec, int bTryForInt){ | |
243 double rValue; | |
244 i64 iValue; | |
245 u8 enc = pRec->enc; | |
246 assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str ); | |
247 if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return; | |
248 if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){ | |
249 pRec->u.i = iValue; | |
250 pRec->flags |= MEM_Int; | |
251 }else{ | |
252 pRec->u.r = rValue; | |
253 pRec->flags |= MEM_Real; | |
254 if( bTryForInt ) sqlite3VdbeIntegerAffinity(pRec); | |
255 } | |
256 } | |
257 | |
258 /* | |
259 ** Processing is determine by the affinity parameter: | |
260 ** | |
261 ** SQLITE_AFF_INTEGER: | |
262 ** SQLITE_AFF_REAL: | |
263 ** SQLITE_AFF_NUMERIC: | |
264 ** Try to convert pRec to an integer representation or a | |
265 ** floating-point representation if an integer representation | |
266 ** is not possible. Note that the integer representation is | |
267 ** always preferred, even if the affinity is REAL, because | |
268 ** an integer representation is more space efficient on disk. | |
269 ** | |
270 ** SQLITE_AFF_TEXT: | |
271 ** Convert pRec to a text representation. | |
272 ** | |
273 ** SQLITE_AFF_NONE: | |
274 ** No-op. pRec is unchanged. | |
275 */ | |
276 static void applyAffinity( | |
277 Mem *pRec, /* The value to apply affinity to */ | |
278 char affinity, /* The affinity to be applied */ | |
279 u8 enc /* Use this text encoding */ | |
280 ){ | |
281 if( affinity>=SQLITE_AFF_NUMERIC ){ | |
282 assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL | |
283 || affinity==SQLITE_AFF_NUMERIC ); | |
284 if( (pRec->flags & MEM_Int)==0 ){ | |
285 if( (pRec->flags & MEM_Real)==0 ){ | |
286 if( pRec->flags & MEM_Str ) applyNumericAffinity(pRec,1); | |
287 }else{ | |
288 sqlite3VdbeIntegerAffinity(pRec); | |
289 } | |
290 } | |
291 }else if( affinity==SQLITE_AFF_TEXT ){ | |
292 /* Only attempt the conversion to TEXT if there is an integer or real | |
293 ** representation (blob and NULL do not get converted) but no string | |
294 ** representation. | |
295 */ | |
296 if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ | |
297 sqlite3VdbeMemStringify(pRec, enc, 1); | |
298 } | |
299 } | |
300 } | |
301 | |
302 /* | |
303 ** Try to convert the type of a function argument or a result column | |
304 ** into a numeric representation. Use either INTEGER or REAL whichever | |
305 ** is appropriate. But only do the conversion if it is possible without | |
306 ** loss of information and return the revised type of the argument. | |
307 */ | |
308 int sqlite3_value_numeric_type(sqlite3_value *pVal){ | |
309 int eType = sqlite3_value_type(pVal); | |
310 if( eType==SQLITE_TEXT ){ | |
311 Mem *pMem = (Mem*)pVal; | |
312 applyNumericAffinity(pMem, 0); | |
313 eType = sqlite3_value_type(pVal); | |
314 } | |
315 return eType; | |
316 } | |
317 | |
318 /* | |
319 ** Exported version of applyAffinity(). This one works on sqlite3_value*, | |
320 ** not the internal Mem* type. | |
321 */ | |
322 void sqlite3ValueApplyAffinity( | |
323 sqlite3_value *pVal, | |
324 u8 affinity, | |
325 u8 enc | |
326 ){ | |
327 applyAffinity((Mem *)pVal, affinity, enc); | |
328 } | |
329 | |
330 /* | |
331 ** pMem currently only holds a string type (or maybe a BLOB that we can | |
332 ** interpret as a string if we want to). Compute its corresponding | |
333 ** numeric type, if has one. Set the pMem->u.r and pMem->u.i fields | |
334 ** accordingly. | |
335 */ | |
336 static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){ | |
337 assert( (pMem->flags & (MEM_Int|MEM_Real))==0 ); | |
338 assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ); | |
339 if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){ | |
340 return 0; | |
341 } | |
342 if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==SQLITE_OK ){ | |
343 return MEM_Int; | |
344 } | |
345 return MEM_Real; | |
346 } | |
347 | |
348 /* | |
349 ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or | |
350 ** none. | |
351 ** | |
352 ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags. | |
353 ** But it does set pMem->u.r and pMem->u.i appropriately. | |
354 */ | |
355 static u16 numericType(Mem *pMem){ | |
356 if( pMem->flags & (MEM_Int|MEM_Real) ){ | |
357 return pMem->flags & (MEM_Int|MEM_Real); | |
358 } | |
359 if( pMem->flags & (MEM_Str|MEM_Blob) ){ | |
360 return computeNumericType(pMem); | |
361 } | |
362 return 0; | |
363 } | |
364 | |
365 #ifdef SQLITE_DEBUG | |
366 /* | |
367 ** Write a nice string representation of the contents of cell pMem | |
368 ** into buffer zBuf, length nBuf. | |
369 */ | |
370 void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){ | |
371 char *zCsr = zBuf; | |
372 int f = pMem->flags; | |
373 | |
374 static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"}; | |
375 | |
376 if( f&MEM_Blob ){ | |
377 int i; | |
378 char c; | |
379 if( f & MEM_Dyn ){ | |
380 c = 'z'; | |
381 assert( (f & (MEM_Static|MEM_Ephem))==0 ); | |
382 }else if( f & MEM_Static ){ | |
383 c = 't'; | |
384 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); | |
385 }else if( f & MEM_Ephem ){ | |
386 c = 'e'; | |
387 assert( (f & (MEM_Static|MEM_Dyn))==0 ); | |
388 }else{ | |
389 c = 's'; | |
390 } | |
391 | |
392 sqlite3_snprintf(100, zCsr, "%c", c); | |
393 zCsr += sqlite3Strlen30(zCsr); | |
394 sqlite3_snprintf(100, zCsr, "%d[", pMem->n); | |
395 zCsr += sqlite3Strlen30(zCsr); | |
396 for(i=0; i<16 && i<pMem->n; i++){ | |
397 sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF)); | |
398 zCsr += sqlite3Strlen30(zCsr); | |
399 } | |
400 for(i=0; i<16 && i<pMem->n; i++){ | |
401 char z = pMem->z[i]; | |
402 if( z<32 || z>126 ) *zCsr++ = '.'; | |
403 else *zCsr++ = z; | |
404 } | |
405 | |
406 sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]); | |
407 zCsr += sqlite3Strlen30(zCsr); | |
408 if( f & MEM_Zero ){ | |
409 sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero); | |
410 zCsr += sqlite3Strlen30(zCsr); | |
411 } | |
412 *zCsr = '\0'; | |
413 }else if( f & MEM_Str ){ | |
414 int j, k; | |
415 zBuf[0] = ' '; | |
416 if( f & MEM_Dyn ){ | |
417 zBuf[1] = 'z'; | |
418 assert( (f & (MEM_Static|MEM_Ephem))==0 ); | |
419 }else if( f & MEM_Static ){ | |
420 zBuf[1] = 't'; | |
421 assert( (f & (MEM_Dyn|MEM_Ephem))==0 ); | |
422 }else if( f & MEM_Ephem ){ | |
423 zBuf[1] = 'e'; | |
424 assert( (f & (MEM_Static|MEM_Dyn))==0 ); | |
425 }else{ | |
426 zBuf[1] = 's'; | |
427 } | |
428 k = 2; | |
429 sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n); | |
430 k += sqlite3Strlen30(&zBuf[k]); | |
431 zBuf[k++] = '['; | |
432 for(j=0; j<15 && j<pMem->n; j++){ | |
433 u8 c = pMem->z[j]; | |
434 if( c>=0x20 && c<0x7f ){ | |
435 zBuf[k++] = c; | |
436 }else{ | |
437 zBuf[k++] = '.'; | |
438 } | |
439 } | |
440 zBuf[k++] = ']'; | |
441 sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]); | |
442 k += sqlite3Strlen30(&zBuf[k]); | |
443 zBuf[k++] = 0; | |
444 } | |
445 } | |
446 #endif | |
447 | |
448 #ifdef SQLITE_DEBUG | |
449 /* | |
450 ** Print the value of a register for tracing purposes: | |
451 */ | |
452 static void memTracePrint(Mem *p){ | |
453 if( p->flags & MEM_Undefined ){ | |
454 printf(" undefined"); | |
455 }else if( p->flags & MEM_Null ){ | |
456 printf(" NULL"); | |
457 }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){ | |
458 printf(" si:%lld", p->u.i); | |
459 }else if( p->flags & MEM_Int ){ | |
460 printf(" i:%lld", p->u.i); | |
461 #ifndef SQLITE_OMIT_FLOATING_POINT | |
462 }else if( p->flags & MEM_Real ){ | |
463 printf(" r:%g", p->u.r); | |
464 #endif | |
465 }else if( p->flags & MEM_RowSet ){ | |
466 printf(" (rowset)"); | |
467 }else{ | |
468 char zBuf[200]; | |
469 sqlite3VdbeMemPrettyPrint(p, zBuf); | |
470 printf(" %s", zBuf); | |
471 } | |
472 } | |
473 static void registerTrace(int iReg, Mem *p){ | |
474 printf("REG[%d] = ", iReg); | |
475 memTracePrint(p); | |
476 printf("\n"); | |
477 } | |
478 #endif | |
479 | |
480 #ifdef SQLITE_DEBUG | |
481 # define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M) | |
482 #else | |
483 # define REGISTER_TRACE(R,M) | |
484 #endif | |
485 | |
486 | |
487 #ifdef VDBE_PROFILE | |
488 | |
489 /* | |
490 ** hwtime.h contains inline assembler code for implementing | |
491 ** high-performance timing routines. | |
492 */ | |
493 #include "hwtime.h" | |
494 | |
495 #endif | |
496 | |
497 #ifndef NDEBUG | |
498 /* | |
499 ** This function is only called from within an assert() expression. It | |
500 ** checks that the sqlite3.nTransaction variable is correctly set to | |
501 ** the number of non-transaction savepoints currently in the | |
502 ** linked list starting at sqlite3.pSavepoint. | |
503 ** | |
504 ** Usage: | |
505 ** | |
506 ** assert( checkSavepointCount(db) ); | |
507 */ | |
508 static int checkSavepointCount(sqlite3 *db){ | |
509 int n = 0; | |
510 Savepoint *p; | |
511 for(p=db->pSavepoint; p; p=p->pNext) n++; | |
512 assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); | |
513 return 1; | |
514 } | |
515 #endif | |
516 | |
517 | |
518 /* | |
519 ** Execute as much of a VDBE program as we can. | |
520 ** This is the core of sqlite3_step(). | |
521 */ | |
522 int sqlite3VdbeExec( | |
523 Vdbe *p /* The VDBE */ | |
524 ){ | |
525 int pc=0; /* The program counter */ | |
526 Op *aOp = p->aOp; /* Copy of p->aOp */ | |
527 Op *pOp; /* Current operation */ | |
528 int rc = SQLITE_OK; /* Value to return */ | |
529 sqlite3 *db = p->db; /* The database */ | |
530 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ | |
531 u8 encoding = ENC(db); /* The database encoding */ | |
532 int iCompare = 0; /* Result of last OP_Compare operation */ | |
533 unsigned nVmStep = 0; /* Number of virtual machine steps */ | |
534 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
535 unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */ | |
536 #endif | |
537 Mem *aMem = p->aMem; /* Copy of p->aMem */ | |
538 Mem *pIn1 = 0; /* 1st input operand */ | |
539 Mem *pIn2 = 0; /* 2nd input operand */ | |
540 Mem *pIn3 = 0; /* 3rd input operand */ | |
541 Mem *pOut = 0; /* Output operand */ | |
542 int *aPermute = 0; /* Permutation of columns for OP_Compare */ | |
543 i64 lastRowid = db->lastRowid; /* Saved value of the last insert ROWID */ | |
544 #ifdef VDBE_PROFILE | |
545 u64 start; /* CPU clock count at start of opcode */ | |
546 #endif | |
547 /*** INSERT STACK UNION HERE ***/ | |
548 | |
549 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */ | |
550 sqlite3VdbeEnter(p); | |
551 if( p->rc==SQLITE_NOMEM ){ | |
552 /* This happens if a malloc() inside a call to sqlite3_column_text() or | |
553 ** sqlite3_column_text16() failed. */ | |
554 goto no_mem; | |
555 } | |
556 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); | |
557 assert( p->bIsReader || p->readOnly!=0 ); | |
558 p->rc = SQLITE_OK; | |
559 p->iCurrentTime = 0; | |
560 assert( p->explain==0 ); | |
561 p->pResultSet = 0; | |
562 db->busyHandler.nBusy = 0; | |
563 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; | |
564 sqlite3VdbeIOTraceSql(p); | |
565 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
566 if( db->xProgress ){ | |
567 assert( 0 < db->nProgressOps ); | |
568 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP]; | |
569 if( nProgressLimit==0 ){ | |
570 nProgressLimit = db->nProgressOps; | |
571 }else{ | |
572 nProgressLimit %= (unsigned)db->nProgressOps; | |
573 } | |
574 } | |
575 #endif | |
576 #ifdef SQLITE_DEBUG | |
577 sqlite3BeginBenignMalloc(); | |
578 if( p->pc==0 | |
579 && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0 | |
580 ){ | |
581 int i; | |
582 int once = 1; | |
583 sqlite3VdbePrintSql(p); | |
584 if( p->db->flags & SQLITE_VdbeListing ){ | |
585 printf("VDBE Program Listing:\n"); | |
586 for(i=0; i<p->nOp; i++){ | |
587 sqlite3VdbePrintOp(stdout, i, &aOp[i]); | |
588 } | |
589 } | |
590 if( p->db->flags & SQLITE_VdbeEQP ){ | |
591 for(i=0; i<p->nOp; i++){ | |
592 if( aOp[i].opcode==OP_Explain ){ | |
593 if( once ) printf("VDBE Query Plan:\n"); | |
594 printf("%s\n", aOp[i].p4.z); | |
595 once = 0; | |
596 } | |
597 } | |
598 } | |
599 if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); | |
600 } | |
601 sqlite3EndBenignMalloc(); | |
602 #endif | |
603 for(pc=p->pc; rc==SQLITE_OK; pc++){ | |
604 assert( pc>=0 && pc<p->nOp ); | |
605 if( db->mallocFailed ) goto no_mem; | |
606 #ifdef VDBE_PROFILE | |
607 start = sqlite3Hwtime(); | |
608 #endif | |
609 nVmStep++; | |
610 pOp = &aOp[pc]; | |
611 | |
612 /* Only allow tracing if SQLITE_DEBUG is defined. | |
613 */ | |
614 #ifdef SQLITE_DEBUG | |
615 if( db->flags & SQLITE_VdbeTrace ){ | |
616 sqlite3VdbePrintOp(stdout, pc, pOp); | |
617 } | |
618 #endif | |
619 | |
620 | |
621 /* Check to see if we need to simulate an interrupt. This only happens | |
622 ** if we have a special test build. | |
623 */ | |
624 #ifdef SQLITE_TEST | |
625 if( sqlite3_interrupt_count>0 ){ | |
626 sqlite3_interrupt_count--; | |
627 if( sqlite3_interrupt_count==0 ){ | |
628 sqlite3_interrupt(db); | |
629 } | |
630 } | |
631 #endif | |
632 | |
633 /* On any opcode with the "out2-prerelease" tag, free any | |
634 ** external allocations out of mem[p2] and set mem[p2] to be | |
635 ** an undefined integer. Opcodes will either fill in the integer | |
636 ** value or convert mem[p2] to a different type. | |
637 */ | |
638 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); | |
639 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ | |
640 assert( pOp->p2>0 ); | |
641 assert( pOp->p2<=(p->nMem-p->nCursor) ); | |
642 pOut = &aMem[pOp->p2]; | |
643 memAboutToChange(p, pOut); | |
644 if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); | |
645 pOut->flags = MEM_Int; | |
646 } | |
647 | |
648 /* Sanity checking on other operands */ | |
649 #ifdef SQLITE_DEBUG | |
650 if( (pOp->opflags & OPFLG_IN1)!=0 ){ | |
651 assert( pOp->p1>0 ); | |
652 assert( pOp->p1<=(p->nMem-p->nCursor) ); | |
653 assert( memIsValid(&aMem[pOp->p1]) ); | |
654 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); | |
655 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]); | |
656 } | |
657 if( (pOp->opflags & OPFLG_IN2)!=0 ){ | |
658 assert( pOp->p2>0 ); | |
659 assert( pOp->p2<=(p->nMem-p->nCursor) ); | |
660 assert( memIsValid(&aMem[pOp->p2]) ); | |
661 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) ); | |
662 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]); | |
663 } | |
664 if( (pOp->opflags & OPFLG_IN3)!=0 ){ | |
665 assert( pOp->p3>0 ); | |
666 assert( pOp->p3<=(p->nMem-p->nCursor) ); | |
667 assert( memIsValid(&aMem[pOp->p3]) ); | |
668 assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) ); | |
669 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]); | |
670 } | |
671 if( (pOp->opflags & OPFLG_OUT2)!=0 ){ | |
672 assert( pOp->p2>0 ); | |
673 assert( pOp->p2<=(p->nMem-p->nCursor) ); | |
674 memAboutToChange(p, &aMem[pOp->p2]); | |
675 } | |
676 if( (pOp->opflags & OPFLG_OUT3)!=0 ){ | |
677 assert( pOp->p3>0 ); | |
678 assert( pOp->p3<=(p->nMem-p->nCursor) ); | |
679 memAboutToChange(p, &aMem[pOp->p3]); | |
680 } | |
681 #endif | |
682 | |
683 switch( pOp->opcode ){ | |
684 | |
685 /***************************************************************************** | |
686 ** What follows is a massive switch statement where each case implements a | |
687 ** separate instruction in the virtual machine. If we follow the usual | |
688 ** indentation conventions, each case should be indented by 6 spaces. But | |
689 ** that is a lot of wasted space on the left margin. So the code within | |
690 ** the switch statement will break with convention and be flush-left. Another | |
691 ** big comment (similar to this one) will mark the point in the code where | |
692 ** we transition back to normal indentation. | |
693 ** | |
694 ** The formatting of each case is important. The makefile for SQLite | |
695 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this | |
696 ** file looking for lines that begin with "case OP_". The opcodes.h files | |
697 ** will be filled with #defines that give unique integer values to each | |
698 ** opcode and the opcodes.c file is filled with an array of strings where | |
699 ** each string is the symbolic name for the corresponding opcode. If the | |
700 ** case statement is followed by a comment of the form "/# same as ... #/" | |
701 ** that comment is used to determine the particular value of the opcode. | |
702 ** | |
703 ** Other keywords in the comment that follows each case are used to | |
704 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. | |
705 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See | |
706 ** the mkopcodeh.awk script for additional information. | |
707 ** | |
708 ** Documentation about VDBE opcodes is generated by scanning this file | |
709 ** for lines of that contain "Opcode:". That line and all subsequent | |
710 ** comment lines are used in the generation of the opcode.html documentation | |
711 ** file. | |
712 ** | |
713 ** SUMMARY: | |
714 ** | |
715 ** Formatting is important to scripts that scan this file. | |
716 ** Do not deviate from the formatting style currently in use. | |
717 ** | |
718 *****************************************************************************/ | |
719 | |
720 /* Opcode: Goto * P2 * * * | |
721 ** | |
722 ** An unconditional jump to address P2. | |
723 ** The next instruction executed will be | |
724 ** the one at index P2 from the beginning of | |
725 ** the program. | |
726 ** | |
727 ** The P1 parameter is not actually used by this opcode. However, it | |
728 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell | |
729 ** that this Goto is the bottom of a loop and that the lines from P2 down | |
730 ** to the current line should be indented for EXPLAIN output. | |
731 */ | |
732 case OP_Goto: { /* jump */ | |
733 pc = pOp->p2 - 1; | |
734 | |
735 /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, | |
736 ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon | |
737 ** completion. Check to see if sqlite3_interrupt() has been called | |
738 ** or if the progress callback needs to be invoked. | |
739 ** | |
740 ** This code uses unstructured "goto" statements and does not look clean. | |
741 ** But that is not due to sloppy coding habits. The code is written this | |
742 ** way for performance, to avoid having to run the interrupt and progress | |
743 ** checks on every opcode. This helps sqlite3_step() to run about 1.5% | |
744 ** faster according to "valgrind --tool=cachegrind" */ | |
745 check_for_interrupt: | |
746 if( db->u1.isInterrupted ) goto abort_due_to_interrupt; | |
747 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
748 /* Call the progress callback if it is configured and the required number | |
749 ** of VDBE ops have been executed (either since this invocation of | |
750 ** sqlite3VdbeExec() or since last time the progress callback was called). | |
751 ** If the progress callback returns non-zero, exit the virtual machine with | |
752 ** a return code SQLITE_ABORT. | |
753 */ | |
754 if( db->xProgress!=0 && nVmStep>=nProgressLimit ){ | |
755 assert( db->nProgressOps!=0 ); | |
756 nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps); | |
757 if( db->xProgress(db->pProgressArg) ){ | |
758 rc = SQLITE_INTERRUPT; | |
759 goto vdbe_error_halt; | |
760 } | |
761 } | |
762 #endif | |
763 | |
764 break; | |
765 } | |
766 | |
767 /* Opcode: Gosub P1 P2 * * * | |
768 ** | |
769 ** Write the current address onto register P1 | |
770 ** and then jump to address P2. | |
771 */ | |
772 case OP_Gosub: { /* jump */ | |
773 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); | |
774 pIn1 = &aMem[pOp->p1]; | |
775 assert( VdbeMemDynamic(pIn1)==0 ); | |
776 memAboutToChange(p, pIn1); | |
777 pIn1->flags = MEM_Int; | |
778 pIn1->u.i = pc; | |
779 REGISTER_TRACE(pOp->p1, pIn1); | |
780 pc = pOp->p2 - 1; | |
781 break; | |
782 } | |
783 | |
784 /* Opcode: Return P1 * * * * | |
785 ** | |
786 ** Jump to the next instruction after the address in register P1. After | |
787 ** the jump, register P1 becomes undefined. | |
788 */ | |
789 case OP_Return: { /* in1 */ | |
790 pIn1 = &aMem[pOp->p1]; | |
791 assert( pIn1->flags==MEM_Int ); | |
792 pc = (int)pIn1->u.i; | |
793 pIn1->flags = MEM_Undefined; | |
794 break; | |
795 } | |
796 | |
797 /* Opcode: InitCoroutine P1 P2 P3 * * | |
798 ** | |
799 ** Set up register P1 so that it will Yield to the coroutine | |
800 ** located at address P3. | |
801 ** | |
802 ** If P2!=0 then the coroutine implementation immediately follows | |
803 ** this opcode. So jump over the coroutine implementation to | |
804 ** address P2. | |
805 ** | |
806 ** See also: EndCoroutine | |
807 */ | |
808 case OP_InitCoroutine: { /* jump */ | |
809 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); | |
810 assert( pOp->p2>=0 && pOp->p2<p->nOp ); | |
811 assert( pOp->p3>=0 && pOp->p3<p->nOp ); | |
812 pOut = &aMem[pOp->p1]; | |
813 assert( !VdbeMemDynamic(pOut) ); | |
814 pOut->u.i = pOp->p3 - 1; | |
815 pOut->flags = MEM_Int; | |
816 if( pOp->p2 ) pc = pOp->p2 - 1; | |
817 break; | |
818 } | |
819 | |
820 /* Opcode: EndCoroutine P1 * * * * | |
821 ** | |
822 ** The instruction at the address in register P1 is a Yield. | |
823 ** Jump to the P2 parameter of that Yield. | |
824 ** After the jump, register P1 becomes undefined. | |
825 ** | |
826 ** See also: InitCoroutine | |
827 */ | |
828 case OP_EndCoroutine: { /* in1 */ | |
829 VdbeOp *pCaller; | |
830 pIn1 = &aMem[pOp->p1]; | |
831 assert( pIn1->flags==MEM_Int ); | |
832 assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); | |
833 pCaller = &aOp[pIn1->u.i]; | |
834 assert( pCaller->opcode==OP_Yield ); | |
835 assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); | |
836 pc = pCaller->p2 - 1; | |
837 pIn1->flags = MEM_Undefined; | |
838 break; | |
839 } | |
840 | |
841 /* Opcode: Yield P1 P2 * * * | |
842 ** | |
843 ** Swap the program counter with the value in register P1. This | |
844 ** has the effect of yielding to a coroutine. | |
845 ** | |
846 ** If the coroutine that is launched by this instruction ends with | |
847 ** Yield or Return then continue to the next instruction. But if | |
848 ** the coroutine launched by this instruction ends with | |
849 ** EndCoroutine, then jump to P2 rather than continuing with the | |
850 ** next instruction. | |
851 ** | |
852 ** See also: InitCoroutine | |
853 */ | |
854 case OP_Yield: { /* in1, jump */ | |
855 int pcDest; | |
856 pIn1 = &aMem[pOp->p1]; | |
857 assert( VdbeMemDynamic(pIn1)==0 ); | |
858 pIn1->flags = MEM_Int; | |
859 pcDest = (int)pIn1->u.i; | |
860 pIn1->u.i = pc; | |
861 REGISTER_TRACE(pOp->p1, pIn1); | |
862 pc = pcDest; | |
863 break; | |
864 } | |
865 | |
866 /* Opcode: HaltIfNull P1 P2 P3 P4 P5 | |
867 ** Synopsis: if r[P3]=null halt | |
868 ** | |
869 ** Check the value in register P3. If it is NULL then Halt using | |
870 ** parameter P1, P2, and P4 as if this were a Halt instruction. If the | |
871 ** value in register P3 is not NULL, then this routine is a no-op. | |
872 ** The P5 parameter should be 1. | |
873 */ | |
874 case OP_HaltIfNull: { /* in3 */ | |
875 pIn3 = &aMem[pOp->p3]; | |
876 if( (pIn3->flags & MEM_Null)==0 ) break; | |
877 /* Fall through into OP_Halt */ | |
878 } | |
879 | |
880 /* Opcode: Halt P1 P2 * P4 P5 | |
881 ** | |
882 ** Exit immediately. All open cursors, etc are closed | |
883 ** automatically. | |
884 ** | |
885 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(), | |
886 ** or sqlite3_finalize(). For a normal halt, this should be SQLITE_OK (0). | |
887 ** For errors, it can be some other value. If P1!=0 then P2 will determine | |
888 ** whether or not to rollback the current transaction. Do not rollback | |
889 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback. If P2==OE_Abort, | |
890 ** then back out all changes that have occurred during this execution of the | |
891 ** VDBE, but do not rollback the transaction. | |
892 ** | |
893 ** If P4 is not null then it is an error message string. | |
894 ** | |
895 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string. | |
896 ** | |
897 ** 0: (no change) | |
898 ** 1: NOT NULL contraint failed: P4 | |
899 ** 2: UNIQUE constraint failed: P4 | |
900 ** 3: CHECK constraint failed: P4 | |
901 ** 4: FOREIGN KEY constraint failed: P4 | |
902 ** | |
903 ** If P5 is not zero and P4 is NULL, then everything after the ":" is | |
904 ** omitted. | |
905 ** | |
906 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of | |
907 ** every program. So a jump past the last instruction of the program | |
908 ** is the same as executing Halt. | |
909 */ | |
910 case OP_Halt: { | |
911 const char *zType; | |
912 const char *zLogFmt; | |
913 | |
914 if( pOp->p1==SQLITE_OK && p->pFrame ){ | |
915 /* Halt the sub-program. Return control to the parent frame. */ | |
916 VdbeFrame *pFrame = p->pFrame; | |
917 p->pFrame = pFrame->pParent; | |
918 p->nFrame--; | |
919 sqlite3VdbeSetChanges(db, p->nChange); | |
920 pc = sqlite3VdbeFrameRestore(pFrame); | |
921 lastRowid = db->lastRowid; | |
922 if( pOp->p2==OE_Ignore ){ | |
923 /* Instruction pc is the OP_Program that invoked the sub-program | |
924 ** currently being halted. If the p2 instruction of this OP_Halt | |
925 ** instruction is set to OE_Ignore, then the sub-program is throwing | |
926 ** an IGNORE exception. In this case jump to the address specified | |
927 ** as the p2 of the calling OP_Program. */ | |
928 pc = p->aOp[pc].p2-1; | |
929 } | |
930 aOp = p->aOp; | |
931 aMem = p->aMem; | |
932 break; | |
933 } | |
934 p->rc = pOp->p1; | |
935 p->errorAction = (u8)pOp->p2; | |
936 p->pc = pc; | |
937 if( p->rc ){ | |
938 if( pOp->p5 ){ | |
939 static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", | |
940 "FOREIGN KEY" }; | |
941 assert( pOp->p5>=1 && pOp->p5<=4 ); | |
942 testcase( pOp->p5==1 ); | |
943 testcase( pOp->p5==2 ); | |
944 testcase( pOp->p5==3 ); | |
945 testcase( pOp->p5==4 ); | |
946 zType = azType[pOp->p5-1]; | |
947 }else{ | |
948 zType = 0; | |
949 } | |
950 assert( zType!=0 || pOp->p4.z!=0 ); | |
951 zLogFmt = "abort at %d in [%s]: %s"; | |
952 if( zType && pOp->p4.z ){ | |
953 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s", | |
954 zType, pOp->p4.z); | |
955 }else if( pOp->p4.z ){ | |
956 sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); | |
957 }else{ | |
958 sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType); | |
959 } | |
960 sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg); | |
961 } | |
962 rc = sqlite3VdbeHalt(p); | |
963 assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); | |
964 if( rc==SQLITE_BUSY ){ | |
965 p->rc = rc = SQLITE_BUSY; | |
966 }else{ | |
967 assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ); | |
968 assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 ); | |
969 rc = p->rc ? SQLITE_ERROR : SQLITE_DONE; | |
970 } | |
971 goto vdbe_return; | |
972 } | |
973 | |
974 /* Opcode: Integer P1 P2 * * * | |
975 ** Synopsis: r[P2]=P1 | |
976 ** | |
977 ** The 32-bit integer value P1 is written into register P2. | |
978 */ | |
979 case OP_Integer: { /* out2-prerelease */ | |
980 pOut->u.i = pOp->p1; | |
981 break; | |
982 } | |
983 | |
984 /* Opcode: Int64 * P2 * P4 * | |
985 ** Synopsis: r[P2]=P4 | |
986 ** | |
987 ** P4 is a pointer to a 64-bit integer value. | |
988 ** Write that value into register P2. | |
989 */ | |
990 case OP_Int64: { /* out2-prerelease */ | |
991 assert( pOp->p4.pI64!=0 ); | |
992 pOut->u.i = *pOp->p4.pI64; | |
993 break; | |
994 } | |
995 | |
996 #ifndef SQLITE_OMIT_FLOATING_POINT | |
997 /* Opcode: Real * P2 * P4 * | |
998 ** Synopsis: r[P2]=P4 | |
999 ** | |
1000 ** P4 is a pointer to a 64-bit floating point value. | |
1001 ** Write that value into register P2. | |
1002 */ | |
1003 case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ | |
1004 pOut->flags = MEM_Real; | |
1005 assert( !sqlite3IsNaN(*pOp->p4.pReal) ); | |
1006 pOut->u.r = *pOp->p4.pReal; | |
1007 break; | |
1008 } | |
1009 #endif | |
1010 | |
1011 /* Opcode: String8 * P2 * P4 * | |
1012 ** Synopsis: r[P2]='P4' | |
1013 ** | |
1014 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed | |
1015 ** into a String before it is executed for the first time. During | |
1016 ** this transformation, the length of string P4 is computed and stored | |
1017 ** as the P1 parameter. | |
1018 */ | |
1019 case OP_String8: { /* same as TK_STRING, out2-prerelease */ | |
1020 assert( pOp->p4.z!=0 ); | |
1021 pOp->opcode = OP_String; | |
1022 pOp->p1 = sqlite3Strlen30(pOp->p4.z); | |
1023 | |
1024 #ifndef SQLITE_OMIT_UTF16 | |
1025 if( encoding!=SQLITE_UTF8 ){ | |
1026 rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC); | |
1027 if( rc==SQLITE_TOOBIG ) goto too_big; | |
1028 if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem; | |
1029 assert( pOut->szMalloc>0 && pOut->zMalloc==pOut->z ); | |
1030 assert( VdbeMemDynamic(pOut)==0 ); | |
1031 pOut->szMalloc = 0; | |
1032 pOut->flags |= MEM_Static; | |
1033 if( pOp->p4type==P4_DYNAMIC ){ | |
1034 sqlite3DbFree(db, pOp->p4.z); | |
1035 } | |
1036 pOp->p4type = P4_DYNAMIC; | |
1037 pOp->p4.z = pOut->z; | |
1038 pOp->p1 = pOut->n; | |
1039 } | |
1040 #endif | |
1041 if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
1042 goto too_big; | |
1043 } | |
1044 /* Fall through to the next case, OP_String */ | |
1045 } | |
1046 | |
1047 /* Opcode: String P1 P2 * P4 * | |
1048 ** Synopsis: r[P2]='P4' (len=P1) | |
1049 ** | |
1050 ** The string value P4 of length P1 (bytes) is stored in register P2. | |
1051 */ | |
1052 case OP_String: { /* out2-prerelease */ | |
1053 assert( pOp->p4.z!=0 ); | |
1054 pOut->flags = MEM_Str|MEM_Static|MEM_Term; | |
1055 pOut->z = pOp->p4.z; | |
1056 pOut->n = pOp->p1; | |
1057 pOut->enc = encoding; | |
1058 UPDATE_MAX_BLOBSIZE(pOut); | |
1059 break; | |
1060 } | |
1061 | |
1062 /* Opcode: Null P1 P2 P3 * * | |
1063 ** Synopsis: r[P2..P3]=NULL | |
1064 ** | |
1065 ** Write a NULL into registers P2. If P3 greater than P2, then also write | |
1066 ** NULL into register P3 and every register in between P2 and P3. If P3 | |
1067 ** is less than P2 (typically P3 is zero) then only register P2 is | |
1068 ** set to NULL. | |
1069 ** | |
1070 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that | |
1071 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on | |
1072 ** OP_Ne or OP_Eq. | |
1073 */ | |
1074 case OP_Null: { /* out2-prerelease */ | |
1075 int cnt; | |
1076 u16 nullFlag; | |
1077 cnt = pOp->p3-pOp->p2; | |
1078 assert( pOp->p3<=(p->nMem-p->nCursor) ); | |
1079 pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; | |
1080 while( cnt>0 ){ | |
1081 pOut++; | |
1082 memAboutToChange(p, pOut); | |
1083 sqlite3VdbeMemSetNull(pOut); | |
1084 pOut->flags = nullFlag; | |
1085 cnt--; | |
1086 } | |
1087 break; | |
1088 } | |
1089 | |
1090 /* Opcode: SoftNull P1 * * * * | |
1091 ** Synopsis: r[P1]=NULL | |
1092 ** | |
1093 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord | |
1094 ** instruction, but do not free any string or blob memory associated with | |
1095 ** the register, so that if the value was a string or blob that was | |
1096 ** previously copied using OP_SCopy, the copies will continue to be valid. | |
1097 */ | |
1098 case OP_SoftNull: { | |
1099 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); | |
1100 pOut = &aMem[pOp->p1]; | |
1101 pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined; | |
1102 break; | |
1103 } | |
1104 | |
1105 /* Opcode: Blob P1 P2 * P4 * | |
1106 ** Synopsis: r[P2]=P4 (len=P1) | |
1107 ** | |
1108 ** P4 points to a blob of data P1 bytes long. Store this | |
1109 ** blob in register P2. | |
1110 */ | |
1111 case OP_Blob: { /* out2-prerelease */ | |
1112 assert( pOp->p1 <= SQLITE_MAX_LENGTH ); | |
1113 sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); | |
1114 pOut->enc = encoding; | |
1115 UPDATE_MAX_BLOBSIZE(pOut); | |
1116 break; | |
1117 } | |
1118 | |
1119 /* Opcode: Variable P1 P2 * P4 * | |
1120 ** Synopsis: r[P2]=parameter(P1,P4) | |
1121 ** | |
1122 ** Transfer the values of bound parameter P1 into register P2 | |
1123 ** | |
1124 ** If the parameter is named, then its name appears in P4. | |
1125 ** The P4 value is used by sqlite3_bind_parameter_name(). | |
1126 */ | |
1127 case OP_Variable: { /* out2-prerelease */ | |
1128 Mem *pVar; /* Value being transferred */ | |
1129 | |
1130 assert( pOp->p1>0 && pOp->p1<=p->nVar ); | |
1131 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); | |
1132 pVar = &p->aVar[pOp->p1 - 1]; | |
1133 if( sqlite3VdbeMemTooBig(pVar) ){ | |
1134 goto too_big; | |
1135 } | |
1136 sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); | |
1137 UPDATE_MAX_BLOBSIZE(pOut); | |
1138 break; | |
1139 } | |
1140 | |
1141 /* Opcode: Move P1 P2 P3 * * | |
1142 ** Synopsis: r[P2@P3]=r[P1@P3] | |
1143 ** | |
1144 ** Move the P3 values in register P1..P1+P3-1 over into | |
1145 ** registers P2..P2+P3-1. Registers P1..P1+P3-1 are | |
1146 ** left holding a NULL. It is an error for register ranges | |
1147 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap. It is an error | |
1148 ** for P3 to be less than 1. | |
1149 */ | |
1150 case OP_Move: { | |
1151 int n; /* Number of registers left to copy */ | |
1152 int p1; /* Register to copy from */ | |
1153 int p2; /* Register to copy to */ | |
1154 | |
1155 n = pOp->p3; | |
1156 p1 = pOp->p1; | |
1157 p2 = pOp->p2; | |
1158 assert( n>0 && p1>0 && p2>0 ); | |
1159 assert( p1+n<=p2 || p2+n<=p1 ); | |
1160 | |
1161 pIn1 = &aMem[p1]; | |
1162 pOut = &aMem[p2]; | |
1163 do{ | |
1164 assert( pOut<=&aMem[(p->nMem-p->nCursor)] ); | |
1165 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); | |
1166 assert( memIsValid(pIn1) ); | |
1167 memAboutToChange(p, pOut); | |
1168 sqlite3VdbeMemMove(pOut, pIn1); | |
1169 #ifdef SQLITE_DEBUG | |
1170 if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){ | |
1171 pOut->pScopyFrom += p1 - pOp->p2; | |
1172 } | |
1173 #endif | |
1174 REGISTER_TRACE(p2++, pOut); | |
1175 pIn1++; | |
1176 pOut++; | |
1177 }while( --n ); | |
1178 break; | |
1179 } | |
1180 | |
1181 /* Opcode: Copy P1 P2 P3 * * | |
1182 ** Synopsis: r[P2@P3+1]=r[P1@P3+1] | |
1183 ** | |
1184 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3. | |
1185 ** | |
1186 ** This instruction makes a deep copy of the value. A duplicate | |
1187 ** is made of any string or blob constant. See also OP_SCopy. | |
1188 */ | |
1189 case OP_Copy: { | |
1190 int n; | |
1191 | |
1192 n = pOp->p3; | |
1193 pIn1 = &aMem[pOp->p1]; | |
1194 pOut = &aMem[pOp->p2]; | |
1195 assert( pOut!=pIn1 ); | |
1196 while( 1 ){ | |
1197 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); | |
1198 Deephemeralize(pOut); | |
1199 #ifdef SQLITE_DEBUG | |
1200 pOut->pScopyFrom = 0; | |
1201 #endif | |
1202 REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut); | |
1203 if( (n--)==0 ) break; | |
1204 pOut++; | |
1205 pIn1++; | |
1206 } | |
1207 break; | |
1208 } | |
1209 | |
1210 /* Opcode: SCopy P1 P2 * * * | |
1211 ** Synopsis: r[P2]=r[P1] | |
1212 ** | |
1213 ** Make a shallow copy of register P1 into register P2. | |
1214 ** | |
1215 ** This instruction makes a shallow copy of the value. If the value | |
1216 ** is a string or blob, then the copy is only a pointer to the | |
1217 ** original and hence if the original changes so will the copy. | |
1218 ** Worse, if the original is deallocated, the copy becomes invalid. | |
1219 ** Thus the program must guarantee that the original will not change | |
1220 ** during the lifetime of the copy. Use OP_Copy to make a complete | |
1221 ** copy. | |
1222 */ | |
1223 case OP_SCopy: { /* out2 */ | |
1224 pIn1 = &aMem[pOp->p1]; | |
1225 pOut = &aMem[pOp->p2]; | |
1226 assert( pOut!=pIn1 ); | |
1227 sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem); | |
1228 #ifdef SQLITE_DEBUG | |
1229 if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1; | |
1230 #endif | |
1231 break; | |
1232 } | |
1233 | |
1234 /* Opcode: ResultRow P1 P2 * * * | |
1235 ** Synopsis: output=r[P1@P2] | |
1236 ** | |
1237 ** The registers P1 through P1+P2-1 contain a single row of | |
1238 ** results. This opcode causes the sqlite3_step() call to terminate | |
1239 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt | |
1240 ** structure to provide access to the r(P1)..r(P1+P2-1) values as | |
1241 ** the result row. | |
1242 */ | |
1243 case OP_ResultRow: { | |
1244 Mem *pMem; | |
1245 int i; | |
1246 assert( p->nResColumn==pOp->p2 ); | |
1247 assert( pOp->p1>0 ); | |
1248 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 ); | |
1249 | |
1250 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
1251 /* Run the progress counter just before returning. | |
1252 */ | |
1253 if( db->xProgress!=0 | |
1254 && nVmStep>=nProgressLimit | |
1255 && db->xProgress(db->pProgressArg)!=0 | |
1256 ){ | |
1257 rc = SQLITE_INTERRUPT; | |
1258 goto vdbe_error_halt; | |
1259 } | |
1260 #endif | |
1261 | |
1262 /* If this statement has violated immediate foreign key constraints, do | |
1263 ** not return the number of rows modified. And do not RELEASE the statement | |
1264 ** transaction. It needs to be rolled back. */ | |
1265 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){ | |
1266 assert( db->flags&SQLITE_CountRows ); | |
1267 assert( p->usesStmtJournal ); | |
1268 break; | |
1269 } | |
1270 | |
1271 /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then | |
1272 ** DML statements invoke this opcode to return the number of rows | |
1273 ** modified to the user. This is the only way that a VM that | |
1274 ** opens a statement transaction may invoke this opcode. | |
1275 ** | |
1276 ** In case this is such a statement, close any statement transaction | |
1277 ** opened by this VM before returning control to the user. This is to | |
1278 ** ensure that statement-transactions are always nested, not overlapping. | |
1279 ** If the open statement-transaction is not closed here, then the user | |
1280 ** may step another VM that opens its own statement transaction. This | |
1281 ** may lead to overlapping statement transactions. | |
1282 ** | |
1283 ** The statement transaction is never a top-level transaction. Hence | |
1284 ** the RELEASE call below can never fail. | |
1285 */ | |
1286 assert( p->iStatement==0 || db->flags&SQLITE_CountRows ); | |
1287 rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE); | |
1288 if( NEVER(rc!=SQLITE_OK) ){ | |
1289 break; | |
1290 } | |
1291 | |
1292 /* Invalidate all ephemeral cursor row caches */ | |
1293 p->cacheCtr = (p->cacheCtr + 2)|1; | |
1294 | |
1295 /* Make sure the results of the current row are \000 terminated | |
1296 ** and have an assigned type. The results are de-ephemeralized as | |
1297 ** a side effect. | |
1298 */ | |
1299 pMem = p->pResultSet = &aMem[pOp->p1]; | |
1300 for(i=0; i<pOp->p2; i++){ | |
1301 assert( memIsValid(&pMem[i]) ); | |
1302 Deephemeralize(&pMem[i]); | |
1303 assert( (pMem[i].flags & MEM_Ephem)==0 | |
1304 || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 ); | |
1305 sqlite3VdbeMemNulTerminate(&pMem[i]); | |
1306 REGISTER_TRACE(pOp->p1+i, &pMem[i]); | |
1307 } | |
1308 if( db->mallocFailed ) goto no_mem; | |
1309 | |
1310 /* Return SQLITE_ROW | |
1311 */ | |
1312 p->pc = pc + 1; | |
1313 rc = SQLITE_ROW; | |
1314 goto vdbe_return; | |
1315 } | |
1316 | |
1317 /* Opcode: Concat P1 P2 P3 * * | |
1318 ** Synopsis: r[P3]=r[P2]+r[P1] | |
1319 ** | |
1320 ** Add the text in register P1 onto the end of the text in | |
1321 ** register P2 and store the result in register P3. | |
1322 ** If either the P1 or P2 text are NULL then store NULL in P3. | |
1323 ** | |
1324 ** P3 = P2 || P1 | |
1325 ** | |
1326 ** It is illegal for P1 and P3 to be the same register. Sometimes, | |
1327 ** if P3 is the same register as P2, the implementation is able | |
1328 ** to avoid a memcpy(). | |
1329 */ | |
1330 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ | |
1331 i64 nByte; | |
1332 | |
1333 pIn1 = &aMem[pOp->p1]; | |
1334 pIn2 = &aMem[pOp->p2]; | |
1335 pOut = &aMem[pOp->p3]; | |
1336 assert( pIn1!=pOut ); | |
1337 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ | |
1338 sqlite3VdbeMemSetNull(pOut); | |
1339 break; | |
1340 } | |
1341 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem; | |
1342 Stringify(pIn1, encoding); | |
1343 Stringify(pIn2, encoding); | |
1344 nByte = pIn1->n + pIn2->n; | |
1345 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
1346 goto too_big; | |
1347 } | |
1348 if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){ | |
1349 goto no_mem; | |
1350 } | |
1351 MemSetTypeFlag(pOut, MEM_Str); | |
1352 if( pOut!=pIn2 ){ | |
1353 memcpy(pOut->z, pIn2->z, pIn2->n); | |
1354 } | |
1355 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); | |
1356 pOut->z[nByte]=0; | |
1357 pOut->z[nByte+1] = 0; | |
1358 pOut->flags |= MEM_Term; | |
1359 pOut->n = (int)nByte; | |
1360 pOut->enc = encoding; | |
1361 UPDATE_MAX_BLOBSIZE(pOut); | |
1362 break; | |
1363 } | |
1364 | |
1365 /* Opcode: Add P1 P2 P3 * * | |
1366 ** Synopsis: r[P3]=r[P1]+r[P2] | |
1367 ** | |
1368 ** Add the value in register P1 to the value in register P2 | |
1369 ** and store the result in register P3. | |
1370 ** If either input is NULL, the result is NULL. | |
1371 */ | |
1372 /* Opcode: Multiply P1 P2 P3 * * | |
1373 ** Synopsis: r[P3]=r[P1]*r[P2] | |
1374 ** | |
1375 ** | |
1376 ** Multiply the value in register P1 by the value in register P2 | |
1377 ** and store the result in register P3. | |
1378 ** If either input is NULL, the result is NULL. | |
1379 */ | |
1380 /* Opcode: Subtract P1 P2 P3 * * | |
1381 ** Synopsis: r[P3]=r[P2]-r[P1] | |
1382 ** | |
1383 ** Subtract the value in register P1 from the value in register P2 | |
1384 ** and store the result in register P3. | |
1385 ** If either input is NULL, the result is NULL. | |
1386 */ | |
1387 /* Opcode: Divide P1 P2 P3 * * | |
1388 ** Synopsis: r[P3]=r[P2]/r[P1] | |
1389 ** | |
1390 ** Divide the value in register P1 by the value in register P2 | |
1391 ** and store the result in register P3 (P3=P2/P1). If the value in | |
1392 ** register P1 is zero, then the result is NULL. If either input is | |
1393 ** NULL, the result is NULL. | |
1394 */ | |
1395 /* Opcode: Remainder P1 P2 P3 * * | |
1396 ** Synopsis: r[P3]=r[P2]%r[P1] | |
1397 ** | |
1398 ** Compute the remainder after integer register P2 is divided by | |
1399 ** register P1 and store the result in register P3. | |
1400 ** If the value in register P1 is zero the result is NULL. | |
1401 ** If either operand is NULL, the result is NULL. | |
1402 */ | |
1403 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */ | |
1404 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */ | |
1405 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */ | |
1406 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */ | |
1407 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */ | |
1408 char bIntint; /* Started out as two integer operands */ | |
1409 u16 flags; /* Combined MEM_* flags from both inputs */ | |
1410 u16 type1; /* Numeric type of left operand */ | |
1411 u16 type2; /* Numeric type of right operand */ | |
1412 i64 iA; /* Integer value of left operand */ | |
1413 i64 iB; /* Integer value of right operand */ | |
1414 double rA; /* Real value of left operand */ | |
1415 double rB; /* Real value of right operand */ | |
1416 | |
1417 pIn1 = &aMem[pOp->p1]; | |
1418 type1 = numericType(pIn1); | |
1419 pIn2 = &aMem[pOp->p2]; | |
1420 type2 = numericType(pIn2); | |
1421 pOut = &aMem[pOp->p3]; | |
1422 flags = pIn1->flags | pIn2->flags; | |
1423 if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null; | |
1424 if( (type1 & type2 & MEM_Int)!=0 ){ | |
1425 iA = pIn1->u.i; | |
1426 iB = pIn2->u.i; | |
1427 bIntint = 1; | |
1428 switch( pOp->opcode ){ | |
1429 case OP_Add: if( sqlite3AddInt64(&iB,iA) ) goto fp_math; break; | |
1430 case OP_Subtract: if( sqlite3SubInt64(&iB,iA) ) goto fp_math; break; | |
1431 case OP_Multiply: if( sqlite3MulInt64(&iB,iA) ) goto fp_math; break; | |
1432 case OP_Divide: { | |
1433 if( iA==0 ) goto arithmetic_result_is_null; | |
1434 if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math; | |
1435 iB /= iA; | |
1436 break; | |
1437 } | |
1438 default: { | |
1439 if( iA==0 ) goto arithmetic_result_is_null; | |
1440 if( iA==-1 ) iA = 1; | |
1441 iB %= iA; | |
1442 break; | |
1443 } | |
1444 } | |
1445 pOut->u.i = iB; | |
1446 MemSetTypeFlag(pOut, MEM_Int); | |
1447 }else{ | |
1448 bIntint = 0; | |
1449 fp_math: | |
1450 rA = sqlite3VdbeRealValue(pIn1); | |
1451 rB = sqlite3VdbeRealValue(pIn2); | |
1452 switch( pOp->opcode ){ | |
1453 case OP_Add: rB += rA; break; | |
1454 case OP_Subtract: rB -= rA; break; | |
1455 case OP_Multiply: rB *= rA; break; | |
1456 case OP_Divide: { | |
1457 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ | |
1458 if( rA==(double)0 ) goto arithmetic_result_is_null; | |
1459 rB /= rA; | |
1460 break; | |
1461 } | |
1462 default: { | |
1463 iA = (i64)rA; | |
1464 iB = (i64)rB; | |
1465 if( iA==0 ) goto arithmetic_result_is_null; | |
1466 if( iA==-1 ) iA = 1; | |
1467 rB = (double)(iB % iA); | |
1468 break; | |
1469 } | |
1470 } | |
1471 #ifdef SQLITE_OMIT_FLOATING_POINT | |
1472 pOut->u.i = rB; | |
1473 MemSetTypeFlag(pOut, MEM_Int); | |
1474 #else | |
1475 if( sqlite3IsNaN(rB) ){ | |
1476 goto arithmetic_result_is_null; | |
1477 } | |
1478 pOut->u.r = rB; | |
1479 MemSetTypeFlag(pOut, MEM_Real); | |
1480 if( ((type1|type2)&MEM_Real)==0 && !bIntint ){ | |
1481 sqlite3VdbeIntegerAffinity(pOut); | |
1482 } | |
1483 #endif | |
1484 } | |
1485 break; | |
1486 | |
1487 arithmetic_result_is_null: | |
1488 sqlite3VdbeMemSetNull(pOut); | |
1489 break; | |
1490 } | |
1491 | |
1492 /* Opcode: CollSeq P1 * * P4 | |
1493 ** | |
1494 ** P4 is a pointer to a CollSeq struct. If the next call to a user function | |
1495 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will | |
1496 ** be returned. This is used by the built-in min(), max() and nullif() | |
1497 ** functions. | |
1498 ** | |
1499 ** If P1 is not zero, then it is a register that a subsequent min() or | |
1500 ** max() aggregate will set to 1 if the current row is not the minimum or | |
1501 ** maximum. The P1 register is initialized to 0 by this instruction. | |
1502 ** | |
1503 ** The interface used by the implementation of the aforementioned functions | |
1504 ** to retrieve the collation sequence set by this opcode is not available | |
1505 ** publicly, only to user functions defined in func.c. | |
1506 */ | |
1507 case OP_CollSeq: { | |
1508 assert( pOp->p4type==P4_COLLSEQ ); | |
1509 if( pOp->p1 ){ | |
1510 sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0); | |
1511 } | |
1512 break; | |
1513 } | |
1514 | |
1515 /* Opcode: Function P1 P2 P3 P4 P5 | |
1516 ** Synopsis: r[P3]=func(r[P2@P5]) | |
1517 ** | |
1518 ** Invoke a user function (P4 is a pointer to a Function structure that | |
1519 ** defines the function) with P5 arguments taken from register P2 and | |
1520 ** successors. The result of the function is stored in register P3. | |
1521 ** Register P3 must not be one of the function inputs. | |
1522 ** | |
1523 ** P1 is a 32-bit bitmask indicating whether or not each argument to the | |
1524 ** function was determined to be constant at compile time. If the first | |
1525 ** argument was constant then bit 0 of P1 is set. This is used to determine | |
1526 ** whether meta data associated with a user function argument using the | |
1527 ** sqlite3_set_auxdata() API may be safely retained until the next | |
1528 ** invocation of this opcode. | |
1529 ** | |
1530 ** See also: AggStep and AggFinal | |
1531 */ | |
1532 case OP_Function: { | |
1533 int i; | |
1534 Mem *pArg; | |
1535 sqlite3_context ctx; | |
1536 sqlite3_value **apVal; | |
1537 int n; | |
1538 | |
1539 n = pOp->p5; | |
1540 apVal = p->apArg; | |
1541 assert( apVal || n==0 ); | |
1542 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
1543 ctx.pOut = &aMem[pOp->p3]; | |
1544 memAboutToChange(p, ctx.pOut); | |
1545 | |
1546 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) ); | |
1547 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n ); | |
1548 pArg = &aMem[pOp->p2]; | |
1549 for(i=0; i<n; i++, pArg++){ | |
1550 assert( memIsValid(pArg) ); | |
1551 apVal[i] = pArg; | |
1552 Deephemeralize(pArg); | |
1553 REGISTER_TRACE(pOp->p2+i, pArg); | |
1554 } | |
1555 | |
1556 assert( pOp->p4type==P4_FUNCDEF ); | |
1557 ctx.pFunc = pOp->p4.pFunc; | |
1558 ctx.iOp = pc; | |
1559 ctx.pVdbe = p; | |
1560 MemSetTypeFlag(ctx.pOut, MEM_Null); | |
1561 ctx.fErrorOrAux = 0; | |
1562 db->lastRowid = lastRowid; | |
1563 (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ | |
1564 lastRowid = db->lastRowid; /* Remember rowid changes made by xFunc */ | |
1565 | |
1566 /* If the function returned an error, throw an exception */ | |
1567 if( ctx.fErrorOrAux ){ | |
1568 if( ctx.isError ){ | |
1569 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut)); | |
1570 rc = ctx.isError; | |
1571 } | |
1572 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1); | |
1573 } | |
1574 | |
1575 /* Copy the result of the function into register P3 */ | |
1576 sqlite3VdbeChangeEncoding(ctx.pOut, encoding); | |
1577 if( sqlite3VdbeMemTooBig(ctx.pOut) ){ | |
1578 goto too_big; | |
1579 } | |
1580 | |
1581 REGISTER_TRACE(pOp->p3, ctx.pOut); | |
1582 UPDATE_MAX_BLOBSIZE(ctx.pOut); | |
1583 break; | |
1584 } | |
1585 | |
1586 /* Opcode: BitAnd P1 P2 P3 * * | |
1587 ** Synopsis: r[P3]=r[P1]&r[P2] | |
1588 ** | |
1589 ** Take the bit-wise AND of the values in register P1 and P2 and | |
1590 ** store the result in register P3. | |
1591 ** If either input is NULL, the result is NULL. | |
1592 */ | |
1593 /* Opcode: BitOr P1 P2 P3 * * | |
1594 ** Synopsis: r[P3]=r[P1]|r[P2] | |
1595 ** | |
1596 ** Take the bit-wise OR of the values in register P1 and P2 and | |
1597 ** store the result in register P3. | |
1598 ** If either input is NULL, the result is NULL. | |
1599 */ | |
1600 /* Opcode: ShiftLeft P1 P2 P3 * * | |
1601 ** Synopsis: r[P3]=r[P2]<<r[P1] | |
1602 ** | |
1603 ** Shift the integer value in register P2 to the left by the | |
1604 ** number of bits specified by the integer in register P1. | |
1605 ** Store the result in register P3. | |
1606 ** If either input is NULL, the result is NULL. | |
1607 */ | |
1608 /* Opcode: ShiftRight P1 P2 P3 * * | |
1609 ** Synopsis: r[P3]=r[P2]>>r[P1] | |
1610 ** | |
1611 ** Shift the integer value in register P2 to the right by the | |
1612 ** number of bits specified by the integer in register P1. | |
1613 ** Store the result in register P3. | |
1614 ** If either input is NULL, the result is NULL. | |
1615 */ | |
1616 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */ | |
1617 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */ | |
1618 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */ | |
1619 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */ | |
1620 i64 iA; | |
1621 u64 uA; | |
1622 i64 iB; | |
1623 u8 op; | |
1624 | |
1625 pIn1 = &aMem[pOp->p1]; | |
1626 pIn2 = &aMem[pOp->p2]; | |
1627 pOut = &aMem[pOp->p3]; | |
1628 if( (pIn1->flags | pIn2->flags) & MEM_Null ){ | |
1629 sqlite3VdbeMemSetNull(pOut); | |
1630 break; | |
1631 } | |
1632 iA = sqlite3VdbeIntValue(pIn2); | |
1633 iB = sqlite3VdbeIntValue(pIn1); | |
1634 op = pOp->opcode; | |
1635 if( op==OP_BitAnd ){ | |
1636 iA &= iB; | |
1637 }else if( op==OP_BitOr ){ | |
1638 iA |= iB; | |
1639 }else if( iB!=0 ){ | |
1640 assert( op==OP_ShiftRight || op==OP_ShiftLeft ); | |
1641 | |
1642 /* If shifting by a negative amount, shift in the other direction */ | |
1643 if( iB<0 ){ | |
1644 assert( OP_ShiftRight==OP_ShiftLeft+1 ); | |
1645 op = 2*OP_ShiftLeft + 1 - op; | |
1646 iB = iB>(-64) ? -iB : 64; | |
1647 } | |
1648 | |
1649 if( iB>=64 ){ | |
1650 iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1; | |
1651 }else{ | |
1652 memcpy(&uA, &iA, sizeof(uA)); | |
1653 if( op==OP_ShiftLeft ){ | |
1654 uA <<= iB; | |
1655 }else{ | |
1656 uA >>= iB; | |
1657 /* Sign-extend on a right shift of a negative number */ | |
1658 if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB); | |
1659 } | |
1660 memcpy(&iA, &uA, sizeof(iA)); | |
1661 } | |
1662 } | |
1663 pOut->u.i = iA; | |
1664 MemSetTypeFlag(pOut, MEM_Int); | |
1665 break; | |
1666 } | |
1667 | |
1668 /* Opcode: AddImm P1 P2 * * * | |
1669 ** Synopsis: r[P1]=r[P1]+P2 | |
1670 ** | |
1671 ** Add the constant P2 to the value in register P1. | |
1672 ** The result is always an integer. | |
1673 ** | |
1674 ** To force any register to be an integer, just add 0. | |
1675 */ | |
1676 case OP_AddImm: { /* in1 */ | |
1677 pIn1 = &aMem[pOp->p1]; | |
1678 memAboutToChange(p, pIn1); | |
1679 sqlite3VdbeMemIntegerify(pIn1); | |
1680 pIn1->u.i += pOp->p2; | |
1681 break; | |
1682 } | |
1683 | |
1684 /* Opcode: MustBeInt P1 P2 * * * | |
1685 ** | |
1686 ** Force the value in register P1 to be an integer. If the value | |
1687 ** in P1 is not an integer and cannot be converted into an integer | |
1688 ** without data loss, then jump immediately to P2, or if P2==0 | |
1689 ** raise an SQLITE_MISMATCH exception. | |
1690 */ | |
1691 case OP_MustBeInt: { /* jump, in1 */ | |
1692 pIn1 = &aMem[pOp->p1]; | |
1693 if( (pIn1->flags & MEM_Int)==0 ){ | |
1694 applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding); | |
1695 VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2); | |
1696 if( (pIn1->flags & MEM_Int)==0 ){ | |
1697 if( pOp->p2==0 ){ | |
1698 rc = SQLITE_MISMATCH; | |
1699 goto abort_due_to_error; | |
1700 }else{ | |
1701 pc = pOp->p2 - 1; | |
1702 break; | |
1703 } | |
1704 } | |
1705 } | |
1706 MemSetTypeFlag(pIn1, MEM_Int); | |
1707 break; | |
1708 } | |
1709 | |
1710 #ifndef SQLITE_OMIT_FLOATING_POINT | |
1711 /* Opcode: RealAffinity P1 * * * * | |
1712 ** | |
1713 ** If register P1 holds an integer convert it to a real value. | |
1714 ** | |
1715 ** This opcode is used when extracting information from a column that | |
1716 ** has REAL affinity. Such column values may still be stored as | |
1717 ** integers, for space efficiency, but after extraction we want them | |
1718 ** to have only a real value. | |
1719 */ | |
1720 case OP_RealAffinity: { /* in1 */ | |
1721 pIn1 = &aMem[pOp->p1]; | |
1722 if( pIn1->flags & MEM_Int ){ | |
1723 sqlite3VdbeMemRealify(pIn1); | |
1724 } | |
1725 break; | |
1726 } | |
1727 #endif | |
1728 | |
1729 #ifndef SQLITE_OMIT_CAST | |
1730 /* Opcode: Cast P1 P2 * * * | |
1731 ** Synopsis: affinity(r[P1]) | |
1732 ** | |
1733 ** Force the value in register P1 to be the type defined by P2. | |
1734 ** | |
1735 ** <ul> | |
1736 ** <li value="97"> TEXT | |
1737 ** <li value="98"> BLOB | |
1738 ** <li value="99"> NUMERIC | |
1739 ** <li value="100"> INTEGER | |
1740 ** <li value="101"> REAL | |
1741 ** </ul> | |
1742 ** | |
1743 ** A NULL value is not changed by this routine. It remains NULL. | |
1744 */ | |
1745 case OP_Cast: { /* in1 */ | |
1746 assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL ); | |
1747 testcase( pOp->p2==SQLITE_AFF_TEXT ); | |
1748 testcase( pOp->p2==SQLITE_AFF_NONE ); | |
1749 testcase( pOp->p2==SQLITE_AFF_NUMERIC ); | |
1750 testcase( pOp->p2==SQLITE_AFF_INTEGER ); | |
1751 testcase( pOp->p2==SQLITE_AFF_REAL ); | |
1752 pIn1 = &aMem[pOp->p1]; | |
1753 memAboutToChange(p, pIn1); | |
1754 rc = ExpandBlob(pIn1); | |
1755 sqlite3VdbeMemCast(pIn1, pOp->p2, encoding); | |
1756 UPDATE_MAX_BLOBSIZE(pIn1); | |
1757 break; | |
1758 } | |
1759 #endif /* SQLITE_OMIT_CAST */ | |
1760 | |
1761 /* Opcode: Lt P1 P2 P3 P4 P5 | |
1762 ** Synopsis: if r[P1]<r[P3] goto P2 | |
1763 ** | |
1764 ** Compare the values in register P1 and P3. If reg(P3)<reg(P1) then | |
1765 ** jump to address P2. | |
1766 ** | |
1767 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or | |
1768 ** reg(P3) is NULL then take the jump. If the SQLITE_JUMPIFNULL | |
1769 ** bit is clear then fall through if either operand is NULL. | |
1770 ** | |
1771 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character - | |
1772 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made | |
1773 ** to coerce both inputs according to this affinity before the | |
1774 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric | |
1775 ** affinity is used. Note that the affinity conversions are stored | |
1776 ** back into the input registers P1 and P3. So this opcode can cause | |
1777 ** persistent changes to registers P1 and P3. | |
1778 ** | |
1779 ** Once any conversions have taken place, and neither value is NULL, | |
1780 ** the values are compared. If both values are blobs then memcmp() is | |
1781 ** used to determine the results of the comparison. If both values | |
1782 ** are text, then the appropriate collating function specified in | |
1783 ** P4 is used to do the comparison. If P4 is not specified then | |
1784 ** memcmp() is used to compare text string. If both values are | |
1785 ** numeric, then a numeric comparison is used. If the two values | |
1786 ** are of different types, then numbers are considered less than | |
1787 ** strings and strings are considered less than blobs. | |
1788 ** | |
1789 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump. Instead, | |
1790 ** store a boolean result (either 0, or 1, or NULL) in register P2. | |
1791 ** | |
1792 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered | |
1793 ** equal to one another, provided that they do not have their MEM_Cleared | |
1794 ** bit set. | |
1795 */ | |
1796 /* Opcode: Ne P1 P2 P3 P4 P5 | |
1797 ** Synopsis: if r[P1]!=r[P3] goto P2 | |
1798 ** | |
1799 ** This works just like the Lt opcode except that the jump is taken if | |
1800 ** the operands in registers P1 and P3 are not equal. See the Lt opcode for | |
1801 ** additional information. | |
1802 ** | |
1803 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either | |
1804 ** true or false and is never NULL. If both operands are NULL then the result | |
1805 ** of comparison is false. If either operand is NULL then the result is true. | |
1806 ** If neither operand is NULL the result is the same as it would be if | |
1807 ** the SQLITE_NULLEQ flag were omitted from P5. | |
1808 */ | |
1809 /* Opcode: Eq P1 P2 P3 P4 P5 | |
1810 ** Synopsis: if r[P1]==r[P3] goto P2 | |
1811 ** | |
1812 ** This works just like the Lt opcode except that the jump is taken if | |
1813 ** the operands in registers P1 and P3 are equal. | |
1814 ** See the Lt opcode for additional information. | |
1815 ** | |
1816 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either | |
1817 ** true or false and is never NULL. If both operands are NULL then the result | |
1818 ** of comparison is true. If either operand is NULL then the result is false. | |
1819 ** If neither operand is NULL the result is the same as it would be if | |
1820 ** the SQLITE_NULLEQ flag were omitted from P5. | |
1821 */ | |
1822 /* Opcode: Le P1 P2 P3 P4 P5 | |
1823 ** Synopsis: if r[P1]<=r[P3] goto P2 | |
1824 ** | |
1825 ** This works just like the Lt opcode except that the jump is taken if | |
1826 ** the content of register P3 is less than or equal to the content of | |
1827 ** register P1. See the Lt opcode for additional information. | |
1828 */ | |
1829 /* Opcode: Gt P1 P2 P3 P4 P5 | |
1830 ** Synopsis: if r[P1]>r[P3] goto P2 | |
1831 ** | |
1832 ** This works just like the Lt opcode except that the jump is taken if | |
1833 ** the content of register P3 is greater than the content of | |
1834 ** register P1. See the Lt opcode for additional information. | |
1835 */ | |
1836 /* Opcode: Ge P1 P2 P3 P4 P5 | |
1837 ** Synopsis: if r[P1]>=r[P3] goto P2 | |
1838 ** | |
1839 ** This works just like the Lt opcode except that the jump is taken if | |
1840 ** the content of register P3 is greater than or equal to the content of | |
1841 ** register P1. See the Lt opcode for additional information. | |
1842 */ | |
1843 case OP_Eq: /* same as TK_EQ, jump, in1, in3 */ | |
1844 case OP_Ne: /* same as TK_NE, jump, in1, in3 */ | |
1845 case OP_Lt: /* same as TK_LT, jump, in1, in3 */ | |
1846 case OP_Le: /* same as TK_LE, jump, in1, in3 */ | |
1847 case OP_Gt: /* same as TK_GT, jump, in1, in3 */ | |
1848 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */ | |
1849 int res; /* Result of the comparison of pIn1 against pIn3 */ | |
1850 char affinity; /* Affinity to use for comparison */ | |
1851 u16 flags1; /* Copy of initial value of pIn1->flags */ | |
1852 u16 flags3; /* Copy of initial value of pIn3->flags */ | |
1853 | |
1854 pIn1 = &aMem[pOp->p1]; | |
1855 pIn3 = &aMem[pOp->p3]; | |
1856 flags1 = pIn1->flags; | |
1857 flags3 = pIn3->flags; | |
1858 if( (flags1 | flags3)&MEM_Null ){ | |
1859 /* One or both operands are NULL */ | |
1860 if( pOp->p5 & SQLITE_NULLEQ ){ | |
1861 /* If SQLITE_NULLEQ is set (which will only happen if the operator is | |
1862 ** OP_Eq or OP_Ne) then take the jump or not depending on whether | |
1863 ** or not both operands are null. | |
1864 */ | |
1865 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne ); | |
1866 assert( (flags1 & MEM_Cleared)==0 ); | |
1867 assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 ); | |
1868 if( (flags1&MEM_Null)!=0 | |
1869 && (flags3&MEM_Null)!=0 | |
1870 && (flags3&MEM_Cleared)==0 | |
1871 ){ | |
1872 res = 0; /* Results are equal */ | |
1873 }else{ | |
1874 res = 1; /* Results are not equal */ | |
1875 } | |
1876 }else{ | |
1877 /* SQLITE_NULLEQ is clear and at least one operand is NULL, | |
1878 ** then the result is always NULL. | |
1879 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set. | |
1880 */ | |
1881 if( pOp->p5 & SQLITE_STOREP2 ){ | |
1882 pOut = &aMem[pOp->p2]; | |
1883 MemSetTypeFlag(pOut, MEM_Null); | |
1884 REGISTER_TRACE(pOp->p2, pOut); | |
1885 }else{ | |
1886 VdbeBranchTaken(2,3); | |
1887 if( pOp->p5 & SQLITE_JUMPIFNULL ){ | |
1888 pc = pOp->p2-1; | |
1889 } | |
1890 } | |
1891 break; | |
1892 } | |
1893 }else{ | |
1894 /* Neither operand is NULL. Do a comparison. */ | |
1895 affinity = pOp->p5 & SQLITE_AFF_MASK; | |
1896 if( affinity>=SQLITE_AFF_NUMERIC ){ | |
1897 if( (pIn1->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ | |
1898 applyNumericAffinity(pIn1,0); | |
1899 } | |
1900 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ | |
1901 applyNumericAffinity(pIn3,0); | |
1902 } | |
1903 }else if( affinity==SQLITE_AFF_TEXT ){ | |
1904 if( (pIn1->flags & MEM_Str)==0 && (pIn1->flags & (MEM_Int|MEM_Real))!=0 ){ | |
1905 testcase( pIn1->flags & MEM_Int ); | |
1906 testcase( pIn1->flags & MEM_Real ); | |
1907 sqlite3VdbeMemStringify(pIn1, encoding, 1); | |
1908 } | |
1909 if( (pIn3->flags & MEM_Str)==0 && (pIn3->flags & (MEM_Int|MEM_Real))!=0 ){ | |
1910 testcase( pIn3->flags & MEM_Int ); | |
1911 testcase( pIn3->flags & MEM_Real ); | |
1912 sqlite3VdbeMemStringify(pIn3, encoding, 1); | |
1913 } | |
1914 } | |
1915 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 ); | |
1916 if( pIn1->flags & MEM_Zero ){ | |
1917 sqlite3VdbeMemExpandBlob(pIn1); | |
1918 flags1 &= ~MEM_Zero; | |
1919 } | |
1920 if( pIn3->flags & MEM_Zero ){ | |
1921 sqlite3VdbeMemExpandBlob(pIn3); | |
1922 flags3 &= ~MEM_Zero; | |
1923 } | |
1924 if( db->mallocFailed ) goto no_mem; | |
1925 res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl); | |
1926 } | |
1927 switch( pOp->opcode ){ | |
1928 case OP_Eq: res = res==0; break; | |
1929 case OP_Ne: res = res!=0; break; | |
1930 case OP_Lt: res = res<0; break; | |
1931 case OP_Le: res = res<=0; break; | |
1932 case OP_Gt: res = res>0; break; | |
1933 default: res = res>=0; break; | |
1934 } | |
1935 | |
1936 if( pOp->p5 & SQLITE_STOREP2 ){ | |
1937 pOut = &aMem[pOp->p2]; | |
1938 memAboutToChange(p, pOut); | |
1939 MemSetTypeFlag(pOut, MEM_Int); | |
1940 pOut->u.i = res; | |
1941 REGISTER_TRACE(pOp->p2, pOut); | |
1942 }else{ | |
1943 VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); | |
1944 if( res ){ | |
1945 pc = pOp->p2-1; | |
1946 } | |
1947 } | |
1948 /* Undo any changes made by applyAffinity() to the input registers. */ | |
1949 pIn1->flags = flags1; | |
1950 pIn3->flags = flags3; | |
1951 break; | |
1952 } | |
1953 | |
1954 /* Opcode: Permutation * * * P4 * | |
1955 ** | |
1956 ** Set the permutation used by the OP_Compare operator to be the array | |
1957 ** of integers in P4. | |
1958 ** | |
1959 ** The permutation is only valid until the next OP_Compare that has | |
1960 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should | |
1961 ** occur immediately prior to the OP_Compare. | |
1962 */ | |
1963 case OP_Permutation: { | |
1964 assert( pOp->p4type==P4_INTARRAY ); | |
1965 assert( pOp->p4.ai ); | |
1966 aPermute = pOp->p4.ai; | |
1967 break; | |
1968 } | |
1969 | |
1970 /* Opcode: Compare P1 P2 P3 P4 P5 | |
1971 ** Synopsis: r[P1@P3] <-> r[P2@P3] | |
1972 ** | |
1973 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this | |
1974 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B"). Save the result of | |
1975 ** the comparison for use by the next OP_Jump instruct. | |
1976 ** | |
1977 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is | |
1978 ** determined by the most recent OP_Permutation operator. If the | |
1979 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential | |
1980 ** order. | |
1981 ** | |
1982 ** P4 is a KeyInfo structure that defines collating sequences and sort | |
1983 ** orders for the comparison. The permutation applies to registers | |
1984 ** only. The KeyInfo elements are used sequentially. | |
1985 ** | |
1986 ** The comparison is a sort comparison, so NULLs compare equal, | |
1987 ** NULLs are less than numbers, numbers are less than strings, | |
1988 ** and strings are less than blobs. | |
1989 */ | |
1990 case OP_Compare: { | |
1991 int n; | |
1992 int i; | |
1993 int p1; | |
1994 int p2; | |
1995 const KeyInfo *pKeyInfo; | |
1996 int idx; | |
1997 CollSeq *pColl; /* Collating sequence to use on this term */ | |
1998 int bRev; /* True for DESCENDING sort order */ | |
1999 | |
2000 if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0; | |
2001 n = pOp->p3; | |
2002 pKeyInfo = pOp->p4.pKeyInfo; | |
2003 assert( n>0 ); | |
2004 assert( pKeyInfo!=0 ); | |
2005 p1 = pOp->p1; | |
2006 p2 = pOp->p2; | |
2007 #if SQLITE_DEBUG | |
2008 if( aPermute ){ | |
2009 int k, mx = 0; | |
2010 for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k]; | |
2011 assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 ); | |
2012 assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 ); | |
2013 }else{ | |
2014 assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 ); | |
2015 assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 ); | |
2016 } | |
2017 #endif /* SQLITE_DEBUG */ | |
2018 for(i=0; i<n; i++){ | |
2019 idx = aPermute ? aPermute[i] : i; | |
2020 assert( memIsValid(&aMem[p1+idx]) ); | |
2021 assert( memIsValid(&aMem[p2+idx]) ); | |
2022 REGISTER_TRACE(p1+idx, &aMem[p1+idx]); | |
2023 REGISTER_TRACE(p2+idx, &aMem[p2+idx]); | |
2024 assert( i<pKeyInfo->nField ); | |
2025 pColl = pKeyInfo->aColl[i]; | |
2026 bRev = pKeyInfo->aSortOrder[i]; | |
2027 iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl); | |
2028 if( iCompare ){ | |
2029 if( bRev ) iCompare = -iCompare; | |
2030 break; | |
2031 } | |
2032 } | |
2033 aPermute = 0; | |
2034 break; | |
2035 } | |
2036 | |
2037 /* Opcode: Jump P1 P2 P3 * * | |
2038 ** | |
2039 ** Jump to the instruction at address P1, P2, or P3 depending on whether | |
2040 ** in the most recent OP_Compare instruction the P1 vector was less than | |
2041 ** equal to, or greater than the P2 vector, respectively. | |
2042 */ | |
2043 case OP_Jump: { /* jump */ | |
2044 if( iCompare<0 ){ | |
2045 pc = pOp->p1 - 1; VdbeBranchTaken(0,3); | |
2046 }else if( iCompare==0 ){ | |
2047 pc = pOp->p2 - 1; VdbeBranchTaken(1,3); | |
2048 }else{ | |
2049 pc = pOp->p3 - 1; VdbeBranchTaken(2,3); | |
2050 } | |
2051 break; | |
2052 } | |
2053 | |
2054 /* Opcode: And P1 P2 P3 * * | |
2055 ** Synopsis: r[P3]=(r[P1] && r[P2]) | |
2056 ** | |
2057 ** Take the logical AND of the values in registers P1 and P2 and | |
2058 ** write the result into register P3. | |
2059 ** | |
2060 ** If either P1 or P2 is 0 (false) then the result is 0 even if | |
2061 ** the other input is NULL. A NULL and true or two NULLs give | |
2062 ** a NULL output. | |
2063 */ | |
2064 /* Opcode: Or P1 P2 P3 * * | |
2065 ** Synopsis: r[P3]=(r[P1] || r[P2]) | |
2066 ** | |
2067 ** Take the logical OR of the values in register P1 and P2 and | |
2068 ** store the answer in register P3. | |
2069 ** | |
2070 ** If either P1 or P2 is nonzero (true) then the result is 1 (true) | |
2071 ** even if the other input is NULL. A NULL and false or two NULLs | |
2072 ** give a NULL output. | |
2073 */ | |
2074 case OP_And: /* same as TK_AND, in1, in2, out3 */ | |
2075 case OP_Or: { /* same as TK_OR, in1, in2, out3 */ | |
2076 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ | |
2077 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */ | |
2078 | |
2079 pIn1 = &aMem[pOp->p1]; | |
2080 if( pIn1->flags & MEM_Null ){ | |
2081 v1 = 2; | |
2082 }else{ | |
2083 v1 = sqlite3VdbeIntValue(pIn1)!=0; | |
2084 } | |
2085 pIn2 = &aMem[pOp->p2]; | |
2086 if( pIn2->flags & MEM_Null ){ | |
2087 v2 = 2; | |
2088 }else{ | |
2089 v2 = sqlite3VdbeIntValue(pIn2)!=0; | |
2090 } | |
2091 if( pOp->opcode==OP_And ){ | |
2092 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 }; | |
2093 v1 = and_logic[v1*3+v2]; | |
2094 }else{ | |
2095 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 }; | |
2096 v1 = or_logic[v1*3+v2]; | |
2097 } | |
2098 pOut = &aMem[pOp->p3]; | |
2099 if( v1==2 ){ | |
2100 MemSetTypeFlag(pOut, MEM_Null); | |
2101 }else{ | |
2102 pOut->u.i = v1; | |
2103 MemSetTypeFlag(pOut, MEM_Int); | |
2104 } | |
2105 break; | |
2106 } | |
2107 | |
2108 /* Opcode: Not P1 P2 * * * | |
2109 ** Synopsis: r[P2]= !r[P1] | |
2110 ** | |
2111 ** Interpret the value in register P1 as a boolean value. Store the | |
2112 ** boolean complement in register P2. If the value in register P1 is | |
2113 ** NULL, then a NULL is stored in P2. | |
2114 */ | |
2115 case OP_Not: { /* same as TK_NOT, in1, out2 */ | |
2116 pIn1 = &aMem[pOp->p1]; | |
2117 pOut = &aMem[pOp->p2]; | |
2118 sqlite3VdbeMemSetNull(pOut); | |
2119 if( (pIn1->flags & MEM_Null)==0 ){ | |
2120 pOut->flags = MEM_Int; | |
2121 pOut->u.i = !sqlite3VdbeIntValue(pIn1); | |
2122 } | |
2123 break; | |
2124 } | |
2125 | |
2126 /* Opcode: BitNot P1 P2 * * * | |
2127 ** Synopsis: r[P1]= ~r[P1] | |
2128 ** | |
2129 ** Interpret the content of register P1 as an integer. Store the | |
2130 ** ones-complement of the P1 value into register P2. If P1 holds | |
2131 ** a NULL then store a NULL in P2. | |
2132 */ | |
2133 case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */ | |
2134 pIn1 = &aMem[pOp->p1]; | |
2135 pOut = &aMem[pOp->p2]; | |
2136 sqlite3VdbeMemSetNull(pOut); | |
2137 if( (pIn1->flags & MEM_Null)==0 ){ | |
2138 pOut->flags = MEM_Int; | |
2139 pOut->u.i = ~sqlite3VdbeIntValue(pIn1); | |
2140 } | |
2141 break; | |
2142 } | |
2143 | |
2144 /* Opcode: Once P1 P2 * * * | |
2145 ** | |
2146 ** Check the "once" flag number P1. If it is set, jump to instruction P2. | |
2147 ** Otherwise, set the flag and fall through to the next instruction. | |
2148 ** In other words, this opcode causes all following opcodes up through P2 | |
2149 ** (but not including P2) to run just once and to be skipped on subsequent | |
2150 ** times through the loop. | |
2151 ** | |
2152 ** All "once" flags are initially cleared whenever a prepared statement | |
2153 ** first begins to run. | |
2154 */ | |
2155 case OP_Once: { /* jump */ | |
2156 assert( pOp->p1<p->nOnceFlag ); | |
2157 VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); | |
2158 if( p->aOnceFlag[pOp->p1] ){ | |
2159 pc = pOp->p2-1; | |
2160 }else{ | |
2161 p->aOnceFlag[pOp->p1] = 1; | |
2162 } | |
2163 break; | |
2164 } | |
2165 | |
2166 /* Opcode: If P1 P2 P3 * * | |
2167 ** | |
2168 ** Jump to P2 if the value in register P1 is true. The value | |
2169 ** is considered true if it is numeric and non-zero. If the value | |
2170 ** in P1 is NULL then take the jump if and only if P3 is non-zero. | |
2171 */ | |
2172 /* Opcode: IfNot P1 P2 P3 * * | |
2173 ** | |
2174 ** Jump to P2 if the value in register P1 is False. The value | |
2175 ** is considered false if it has a numeric value of zero. If the value | |
2176 ** in P1 is NULL then take the jump if and only if P3 is non-zero. | |
2177 */ | |
2178 case OP_If: /* jump, in1 */ | |
2179 case OP_IfNot: { /* jump, in1 */ | |
2180 int c; | |
2181 pIn1 = &aMem[pOp->p1]; | |
2182 if( pIn1->flags & MEM_Null ){ | |
2183 c = pOp->p3; | |
2184 }else{ | |
2185 #ifdef SQLITE_OMIT_FLOATING_POINT | |
2186 c = sqlite3VdbeIntValue(pIn1)!=0; | |
2187 #else | |
2188 c = sqlite3VdbeRealValue(pIn1)!=0.0; | |
2189 #endif | |
2190 if( pOp->opcode==OP_IfNot ) c = !c; | |
2191 } | |
2192 VdbeBranchTaken(c!=0, 2); | |
2193 if( c ){ | |
2194 pc = pOp->p2-1; | |
2195 } | |
2196 break; | |
2197 } | |
2198 | |
2199 /* Opcode: IsNull P1 P2 * * * | |
2200 ** Synopsis: if r[P1]==NULL goto P2 | |
2201 ** | |
2202 ** Jump to P2 if the value in register P1 is NULL. | |
2203 */ | |
2204 case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ | |
2205 pIn1 = &aMem[pOp->p1]; | |
2206 VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); | |
2207 if( (pIn1->flags & MEM_Null)!=0 ){ | |
2208 pc = pOp->p2 - 1; | |
2209 } | |
2210 break; | |
2211 } | |
2212 | |
2213 /* Opcode: NotNull P1 P2 * * * | |
2214 ** Synopsis: if r[P1]!=NULL goto P2 | |
2215 ** | |
2216 ** Jump to P2 if the value in register P1 is not NULL. | |
2217 */ | |
2218 case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ | |
2219 pIn1 = &aMem[pOp->p1]; | |
2220 VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); | |
2221 if( (pIn1->flags & MEM_Null)==0 ){ | |
2222 pc = pOp->p2 - 1; | |
2223 } | |
2224 break; | |
2225 } | |
2226 | |
2227 /* Opcode: Column P1 P2 P3 P4 P5 | |
2228 ** Synopsis: r[P3]=PX | |
2229 ** | |
2230 ** Interpret the data that cursor P1 points to as a structure built using | |
2231 ** the MakeRecord instruction. (See the MakeRecord opcode for additional | |
2232 ** information about the format of the data.) Extract the P2-th column | |
2233 ** from this record. If there are less that (P2+1) | |
2234 ** values in the record, extract a NULL. | |
2235 ** | |
2236 ** The value extracted is stored in register P3. | |
2237 ** | |
2238 ** If the column contains fewer than P2 fields, then extract a NULL. Or, | |
2239 ** if the P4 argument is a P4_MEM use the value of the P4 argument as | |
2240 ** the result. | |
2241 ** | |
2242 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor, | |
2243 ** then the cache of the cursor is reset prior to extracting the column. | |
2244 ** The first OP_Column against a pseudo-table after the value of the content | |
2245 ** register has changed should have this bit set. | |
2246 ** | |
2247 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when | |
2248 ** the result is guaranteed to only be used as the argument of a length() | |
2249 ** or typeof() function, respectively. The loading of large blobs can be | |
2250 ** skipped for length() and all content loading can be skipped for typeof(). | |
2251 */ | |
2252 case OP_Column: { | |
2253 i64 payloadSize64; /* Number of bytes in the record */ | |
2254 int p2; /* column number to retrieve */ | |
2255 VdbeCursor *pC; /* The VDBE cursor */ | |
2256 BtCursor *pCrsr; /* The BTree cursor */ | |
2257 u32 *aOffset; /* aOffset[i] is offset to start of data for i-th column */ | |
2258 int len; /* The length of the serialized data for the column */ | |
2259 int i; /* Loop counter */ | |
2260 Mem *pDest; /* Where to write the extracted value */ | |
2261 Mem sMem; /* For storing the record being decoded */ | |
2262 const u8 *zData; /* Part of the record being decoded */ | |
2263 const u8 *zHdr; /* Next unparsed byte of the header */ | |
2264 const u8 *zEndHdr; /* Pointer to first byte after the header */ | |
2265 u32 offset; /* Offset into the data */ | |
2266 u32 szField; /* Number of bytes in the content of a field */ | |
2267 u32 avail; /* Number of bytes of available data */ | |
2268 u32 t; /* A type code from the record header */ | |
2269 u16 fx; /* pDest->flags value */ | |
2270 Mem *pReg; /* PseudoTable input register */ | |
2271 | |
2272 p2 = pOp->p2; | |
2273 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
2274 pDest = &aMem[pOp->p3]; | |
2275 memAboutToChange(p, pDest); | |
2276 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
2277 pC = p->apCsr[pOp->p1]; | |
2278 assert( pC!=0 ); | |
2279 assert( p2<pC->nField ); | |
2280 aOffset = pC->aOffset; | |
2281 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
2282 assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */ | |
2283 #endif | |
2284 pCrsr = pC->pCursor; | |
2285 assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */ | |
2286 assert( pCrsr!=0 || pC->nullRow ); /* pC->nullRow on PseudoTables */ | |
2287 | |
2288 /* If the cursor cache is stale, bring it up-to-date */ | |
2289 rc = sqlite3VdbeCursorMoveto(pC); | |
2290 if( rc ) goto abort_due_to_error; | |
2291 if( pC->cacheStatus!=p->cacheCtr ){ | |
2292 if( pC->nullRow ){ | |
2293 if( pCrsr==0 ){ | |
2294 assert( pC->pseudoTableReg>0 ); | |
2295 pReg = &aMem[pC->pseudoTableReg]; | |
2296 assert( pReg->flags & MEM_Blob ); | |
2297 assert( memIsValid(pReg) ); | |
2298 pC->payloadSize = pC->szRow = avail = pReg->n; | |
2299 pC->aRow = (u8*)pReg->z; | |
2300 }else{ | |
2301 sqlite3VdbeMemSetNull(pDest); | |
2302 goto op_column_out; | |
2303 } | |
2304 }else{ | |
2305 assert( pCrsr ); | |
2306 if( pC->isTable==0 ){ | |
2307 assert( sqlite3BtreeCursorIsValid(pCrsr) ); | |
2308 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64); | |
2309 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ | |
2310 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the | |
2311 ** payload size, so it is impossible for payloadSize64 to be | |
2312 ** larger than 32 bits. */ | |
2313 assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 ); | |
2314 pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail); | |
2315 pC->payloadSize = (u32)payloadSize64; | |
2316 }else{ | |
2317 assert( sqlite3BtreeCursorIsValid(pCrsr) ); | |
2318 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize); | |
2319 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ | |
2320 pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail); | |
2321 } | |
2322 assert( avail<=65536 ); /* Maximum page size is 64KiB */ | |
2323 if( pC->payloadSize <= (u32)avail ){ | |
2324 pC->szRow = pC->payloadSize; | |
2325 }else{ | |
2326 pC->szRow = avail; | |
2327 } | |
2328 if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
2329 goto too_big; | |
2330 } | |
2331 } | |
2332 pC->cacheStatus = p->cacheCtr; | |
2333 pC->iHdrOffset = getVarint32(pC->aRow, offset); | |
2334 pC->nHdrParsed = 0; | |
2335 aOffset[0] = offset; | |
2336 | |
2337 /* Make sure a corrupt database has not given us an oversize header. | |
2338 ** Do this now to avoid an oversize memory allocation. | |
2339 ** | |
2340 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte | |
2341 ** types use so much data space that there can only be 4096 and 32 of | |
2342 ** them, respectively. So the maximum header length results from a | |
2343 ** 3-byte type for each of the maximum of 32768 columns plus three | |
2344 ** extra bytes for the header length itself. 32768*3 + 3 = 98307. | |
2345 */ | |
2346 if( offset > 98307 || offset > pC->payloadSize ){ | |
2347 rc = SQLITE_CORRUPT_BKPT; | |
2348 goto op_column_error; | |
2349 } | |
2350 | |
2351 if( avail<offset ){ | |
2352 /* pC->aRow does not have to hold the entire row, but it does at least | |
2353 ** need to cover the header of the record. If pC->aRow does not contain | |
2354 ** the complete header, then set it to zero, forcing the header to be | |
2355 ** dynamically allocated. */ | |
2356 pC->aRow = 0; | |
2357 pC->szRow = 0; | |
2358 } | |
2359 | |
2360 /* The following goto is an optimization. It can be omitted and | |
2361 ** everything will still work. But OP_Column is measurably faster | |
2362 ** by skipping the subsequent conditional, which is always true. | |
2363 */ | |
2364 assert( pC->nHdrParsed<=p2 ); /* Conditional skipped */ | |
2365 goto op_column_read_header; | |
2366 } | |
2367 | |
2368 /* Make sure at least the first p2+1 entries of the header have been | |
2369 ** parsed and valid information is in aOffset[] and pC->aType[]. | |
2370 */ | |
2371 if( pC->nHdrParsed<=p2 ){ | |
2372 /* If there is more header available for parsing in the record, try | |
2373 ** to extract additional fields up through the p2+1-th field | |
2374 */ | |
2375 op_column_read_header: | |
2376 if( pC->iHdrOffset<aOffset[0] ){ | |
2377 /* Make sure zData points to enough of the record to cover the header. */ | |
2378 if( pC->aRow==0 ){ | |
2379 memset(&sMem, 0, sizeof(sMem)); | |
2380 rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], | |
2381 !pC->isTable, &sMem); | |
2382 if( rc!=SQLITE_OK ){ | |
2383 goto op_column_error; | |
2384 } | |
2385 zData = (u8*)sMem.z; | |
2386 }else{ | |
2387 zData = pC->aRow; | |
2388 } | |
2389 | |
2390 /* Fill in pC->aType[i] and aOffset[i] values through the p2-th field. */ | |
2391 i = pC->nHdrParsed; | |
2392 offset = aOffset[i]; | |
2393 zHdr = zData + pC->iHdrOffset; | |
2394 zEndHdr = zData + aOffset[0]; | |
2395 assert( i<=p2 && zHdr<zEndHdr ); | |
2396 do{ | |
2397 if( zHdr[0]<0x80 ){ | |
2398 t = zHdr[0]; | |
2399 zHdr++; | |
2400 }else{ | |
2401 zHdr += sqlite3GetVarint32(zHdr, &t); | |
2402 } | |
2403 pC->aType[i] = t; | |
2404 szField = sqlite3VdbeSerialTypeLen(t); | |
2405 offset += szField; | |
2406 if( offset<szField ){ /* True if offset overflows */ | |
2407 zHdr = &zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */ | |
2408 break; | |
2409 } | |
2410 i++; | |
2411 aOffset[i] = offset; | |
2412 }while( i<=p2 && zHdr<zEndHdr ); | |
2413 pC->nHdrParsed = i; | |
2414 pC->iHdrOffset = (u32)(zHdr - zData); | |
2415 if( pC->aRow==0 ){ | |
2416 sqlite3VdbeMemRelease(&sMem); | |
2417 sMem.flags = MEM_Null; | |
2418 } | |
2419 | |
2420 /* The record is corrupt if any of the following are true: | |
2421 ** (1) the bytes of the header extend past the declared header size | |
2422 ** (zHdr>zEndHdr) | |
2423 ** (2) the entire header was used but not all data was used | |
2424 ** (zHdr==zEndHdr && offset!=pC->payloadSize) | |
2425 ** (3) the end of the data extends beyond the end of the record. | |
2426 ** (offset > pC->payloadSize) | |
2427 */ | |
2428 if( (zHdr>=zEndHdr && (zHdr>zEndHdr || offset!=pC->payloadSize)) | |
2429 || (offset > pC->payloadSize) | |
2430 ){ | |
2431 rc = SQLITE_CORRUPT_BKPT; | |
2432 goto op_column_error; | |
2433 } | |
2434 } | |
2435 | |
2436 /* If after trying to extra new entries from the header, nHdrParsed is | |
2437 ** still not up to p2, that means that the record has fewer than p2 | |
2438 ** columns. So the result will be either the default value or a NULL. | |
2439 */ | |
2440 if( pC->nHdrParsed<=p2 ){ | |
2441 if( pOp->p4type==P4_MEM ){ | |
2442 sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static); | |
2443 }else{ | |
2444 sqlite3VdbeMemSetNull(pDest); | |
2445 } | |
2446 goto op_column_out; | |
2447 } | |
2448 } | |
2449 | |
2450 /* Extract the content for the p2+1-th column. Control can only | |
2451 ** reach this point if aOffset[p2], aOffset[p2+1], and pC->aType[p2] are | |
2452 ** all valid. | |
2453 */ | |
2454 assert( p2<pC->nHdrParsed ); | |
2455 assert( rc==SQLITE_OK ); | |
2456 assert( sqlite3VdbeCheckMemInvariants(pDest) ); | |
2457 if( VdbeMemDynamic(pDest) ) sqlite3VdbeMemSetNull(pDest); | |
2458 t = pC->aType[p2]; | |
2459 if( pC->szRow>=aOffset[p2+1] ){ | |
2460 /* This is the common case where the desired content fits on the original | |
2461 ** page - where the content is not on an overflow page */ | |
2462 sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], t, pDest); | |
2463 }else{ | |
2464 /* This branch happens only when content is on overflow pages */ | |
2465 if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0 | |
2466 && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)) | |
2467 || (len = sqlite3VdbeSerialTypeLen(t))==0 | |
2468 ){ | |
2469 /* Content is irrelevant for | |
2470 ** 1. the typeof() function, | |
2471 ** 2. the length(X) function if X is a blob, and | |
2472 ** 3. if the content length is zero. | |
2473 ** So we might as well use bogus content rather than reading | |
2474 ** content from disk. NULL will work for the value for strings | |
2475 ** and blobs and whatever is in the payloadSize64 variable | |
2476 ** will work for everything else. */ | |
2477 sqlite3VdbeSerialGet(t<=13 ? (u8*)&payloadSize64 : 0, t, pDest); | |
2478 }else{ | |
2479 rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable, | |
2480 pDest); | |
2481 if( rc!=SQLITE_OK ){ | |
2482 goto op_column_error; | |
2483 } | |
2484 sqlite3VdbeSerialGet((const u8*)pDest->z, t, pDest); | |
2485 pDest->flags &= ~MEM_Ephem; | |
2486 } | |
2487 } | |
2488 pDest->enc = encoding; | |
2489 | |
2490 op_column_out: | |
2491 /* If the column value is an ephemeral string, go ahead and persist | |
2492 ** that string in case the cursor moves before the column value is | |
2493 ** used. The following code does the equivalent of Deephemeralize() | |
2494 ** but does it faster. */ | |
2495 if( (pDest->flags & MEM_Ephem)!=0 && pDest->z ){ | |
2496 fx = pDest->flags & (MEM_Str|MEM_Blob); | |
2497 assert( fx!=0 ); | |
2498 zData = (const u8*)pDest->z; | |
2499 len = pDest->n; | |
2500 if( sqlite3VdbeMemClearAndResize(pDest, len+2) ) goto no_mem; | |
2501 memcpy(pDest->z, zData, len); | |
2502 pDest->z[len] = 0; | |
2503 pDest->z[len+1] = 0; | |
2504 pDest->flags = fx|MEM_Term; | |
2505 } | |
2506 op_column_error: | |
2507 UPDATE_MAX_BLOBSIZE(pDest); | |
2508 REGISTER_TRACE(pOp->p3, pDest); | |
2509 break; | |
2510 } | |
2511 | |
2512 /* Opcode: Affinity P1 P2 * P4 * | |
2513 ** Synopsis: affinity(r[P1@P2]) | |
2514 ** | |
2515 ** Apply affinities to a range of P2 registers starting with P1. | |
2516 ** | |
2517 ** P4 is a string that is P2 characters long. The nth character of the | |
2518 ** string indicates the column affinity that should be used for the nth | |
2519 ** memory cell in the range. | |
2520 */ | |
2521 case OP_Affinity: { | |
2522 const char *zAffinity; /* The affinity to be applied */ | |
2523 char cAff; /* A single character of affinity */ | |
2524 | |
2525 zAffinity = pOp->p4.z; | |
2526 assert( zAffinity!=0 ); | |
2527 assert( zAffinity[pOp->p2]==0 ); | |
2528 pIn1 = &aMem[pOp->p1]; | |
2529 while( (cAff = *(zAffinity++))!=0 ){ | |
2530 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] ); | |
2531 assert( memIsValid(pIn1) ); | |
2532 applyAffinity(pIn1, cAff, encoding); | |
2533 pIn1++; | |
2534 } | |
2535 break; | |
2536 } | |
2537 | |
2538 /* Opcode: MakeRecord P1 P2 P3 P4 * | |
2539 ** Synopsis: r[P3]=mkrec(r[P1@P2]) | |
2540 ** | |
2541 ** Convert P2 registers beginning with P1 into the [record format] | |
2542 ** use as a data record in a database table or as a key | |
2543 ** in an index. The OP_Column opcode can decode the record later. | |
2544 ** | |
2545 ** P4 may be a string that is P2 characters long. The nth character of the | |
2546 ** string indicates the column affinity that should be used for the nth | |
2547 ** field of the index key. | |
2548 ** | |
2549 ** The mapping from character to affinity is given by the SQLITE_AFF_ | |
2550 ** macros defined in sqliteInt.h. | |
2551 ** | |
2552 ** If P4 is NULL then all index fields have the affinity NONE. | |
2553 */ | |
2554 case OP_MakeRecord: { | |
2555 u8 *zNewRecord; /* A buffer to hold the data for the new record */ | |
2556 Mem *pRec; /* The new record */ | |
2557 u64 nData; /* Number of bytes of data space */ | |
2558 int nHdr; /* Number of bytes of header space */ | |
2559 i64 nByte; /* Data space required for this record */ | |
2560 int nZero; /* Number of zero bytes at the end of the record */ | |
2561 int nVarint; /* Number of bytes in a varint */ | |
2562 u32 serial_type; /* Type field */ | |
2563 Mem *pData0; /* First field to be combined into the record */ | |
2564 Mem *pLast; /* Last field of the record */ | |
2565 int nField; /* Number of fields in the record */ | |
2566 char *zAffinity; /* The affinity string for the record */ | |
2567 int file_format; /* File format to use for encoding */ | |
2568 int i; /* Space used in zNewRecord[] header */ | |
2569 int j; /* Space used in zNewRecord[] content */ | |
2570 int len; /* Length of a field */ | |
2571 | |
2572 /* Assuming the record contains N fields, the record format looks | |
2573 ** like this: | |
2574 ** | |
2575 ** ------------------------------------------------------------------------ | |
2576 ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | | |
2577 ** ------------------------------------------------------------------------ | |
2578 ** | |
2579 ** Data(0) is taken from register P1. Data(1) comes from register P1+1 | |
2580 ** and so forth. | |
2581 ** | |
2582 ** Each type field is a varint representing the serial type of the | |
2583 ** corresponding data element (see sqlite3VdbeSerialType()). The | |
2584 ** hdr-size field is also a varint which is the offset from the beginning | |
2585 ** of the record to data0. | |
2586 */ | |
2587 nData = 0; /* Number of bytes of data space */ | |
2588 nHdr = 0; /* Number of bytes of header space */ | |
2589 nZero = 0; /* Number of zero bytes at the end of the record */ | |
2590 nField = pOp->p1; | |
2591 zAffinity = pOp->p4.z; | |
2592 assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 ); | |
2593 pData0 = &aMem[nField]; | |
2594 nField = pOp->p2; | |
2595 pLast = &pData0[nField-1]; | |
2596 file_format = p->minWriteFileFormat; | |
2597 | |
2598 /* Identify the output register */ | |
2599 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 ); | |
2600 pOut = &aMem[pOp->p3]; | |
2601 memAboutToChange(p, pOut); | |
2602 | |
2603 /* Apply the requested affinity to all inputs | |
2604 */ | |
2605 assert( pData0<=pLast ); | |
2606 if( zAffinity ){ | |
2607 pRec = pData0; | |
2608 do{ | |
2609 applyAffinity(pRec++, *(zAffinity++), encoding); | |
2610 assert( zAffinity[0]==0 || pRec<=pLast ); | |
2611 }while( zAffinity[0] ); | |
2612 } | |
2613 | |
2614 /* Loop through the elements that will make up the record to figure | |
2615 ** out how much space is required for the new record. | |
2616 */ | |
2617 pRec = pLast; | |
2618 do{ | |
2619 assert( memIsValid(pRec) ); | |
2620 pRec->uTemp = serial_type = sqlite3VdbeSerialType(pRec, file_format); | |
2621 len = sqlite3VdbeSerialTypeLen(serial_type); | |
2622 if( pRec->flags & MEM_Zero ){ | |
2623 if( nData ){ | |
2624 sqlite3VdbeMemExpandBlob(pRec); | |
2625 }else{ | |
2626 nZero += pRec->u.nZero; | |
2627 len -= pRec->u.nZero; | |
2628 } | |
2629 } | |
2630 nData += len; | |
2631 testcase( serial_type==127 ); | |
2632 testcase( serial_type==128 ); | |
2633 nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type); | |
2634 }while( (--pRec)>=pData0 ); | |
2635 | |
2636 /* Add the initial header varint and total the size */ | |
2637 testcase( nHdr==126 ); | |
2638 testcase( nHdr==127 ); | |
2639 if( nHdr<=126 ){ | |
2640 /* The common case */ | |
2641 nHdr += 1; | |
2642 }else{ | |
2643 /* Rare case of a really large header */ | |
2644 nVarint = sqlite3VarintLen(nHdr); | |
2645 nHdr += nVarint; | |
2646 if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; | |
2647 } | |
2648 nByte = nHdr+nData; | |
2649 if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
2650 goto too_big; | |
2651 } | |
2652 | |
2653 /* Make sure the output register has a buffer large enough to store | |
2654 ** the new record. The output register (pOp->p3) is not allowed to | |
2655 ** be one of the input registers (because the following call to | |
2656 ** sqlite3VdbeMemClearAndResize() could clobber the value before it is used). | |
2657 */ | |
2658 if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){ | |
2659 goto no_mem; | |
2660 } | |
2661 zNewRecord = (u8 *)pOut->z; | |
2662 | |
2663 /* Write the record */ | |
2664 i = putVarint32(zNewRecord, nHdr); | |
2665 j = nHdr; | |
2666 assert( pData0<=pLast ); | |
2667 pRec = pData0; | |
2668 do{ | |
2669 serial_type = pRec->uTemp; | |
2670 i += putVarint32(&zNewRecord[i], serial_type); /* serial type */ | |
2671 j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */ | |
2672 }while( (++pRec)<=pLast ); | |
2673 assert( i==nHdr ); | |
2674 assert( j==nByte ); | |
2675 | |
2676 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
2677 pOut->n = (int)nByte; | |
2678 pOut->flags = MEM_Blob; | |
2679 if( nZero ){ | |
2680 pOut->u.nZero = nZero; | |
2681 pOut->flags |= MEM_Zero; | |
2682 } | |
2683 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */ | |
2684 REGISTER_TRACE(pOp->p3, pOut); | |
2685 UPDATE_MAX_BLOBSIZE(pOut); | |
2686 break; | |
2687 } | |
2688 | |
2689 /* Opcode: Count P1 P2 * * * | |
2690 ** Synopsis: r[P2]=count() | |
2691 ** | |
2692 ** Store the number of entries (an integer value) in the table or index | |
2693 ** opened by cursor P1 in register P2 | |
2694 */ | |
2695 #ifndef SQLITE_OMIT_BTREECOUNT | |
2696 case OP_Count: { /* out2-prerelease */ | |
2697 i64 nEntry; | |
2698 BtCursor *pCrsr; | |
2699 | |
2700 pCrsr = p->apCsr[pOp->p1]->pCursor; | |
2701 assert( pCrsr ); | |
2702 nEntry = 0; /* Not needed. Only used to silence a warning. */ | |
2703 rc = sqlite3BtreeCount(pCrsr, &nEntry); | |
2704 pOut->u.i = nEntry; | |
2705 break; | |
2706 } | |
2707 #endif | |
2708 | |
2709 /* Opcode: Savepoint P1 * * P4 * | |
2710 ** | |
2711 ** Open, release or rollback the savepoint named by parameter P4, depending | |
2712 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an | |
2713 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2. | |
2714 */ | |
2715 case OP_Savepoint: { | |
2716 int p1; /* Value of P1 operand */ | |
2717 char *zName; /* Name of savepoint */ | |
2718 int nName; | |
2719 Savepoint *pNew; | |
2720 Savepoint *pSavepoint; | |
2721 Savepoint *pTmp; | |
2722 int iSavepoint; | |
2723 int ii; | |
2724 | |
2725 p1 = pOp->p1; | |
2726 zName = pOp->p4.z; | |
2727 | |
2728 /* Assert that the p1 parameter is valid. Also that if there is no open | |
2729 ** transaction, then there cannot be any savepoints. | |
2730 */ | |
2731 assert( db->pSavepoint==0 || db->autoCommit==0 ); | |
2732 assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK ); | |
2733 assert( db->pSavepoint || db->isTransactionSavepoint==0 ); | |
2734 assert( checkSavepointCount(db) ); | |
2735 assert( p->bIsReader ); | |
2736 | |
2737 if( p1==SAVEPOINT_BEGIN ){ | |
2738 if( db->nVdbeWrite>0 ){ | |
2739 /* A new savepoint cannot be created if there are active write | |
2740 ** statements (i.e. open read/write incremental blob handles). | |
2741 */ | |
2742 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - " | |
2743 "SQL statements in progress"); | |
2744 rc = SQLITE_BUSY; | |
2745 }else{ | |
2746 nName = sqlite3Strlen30(zName); | |
2747 | |
2748 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
2749 /* This call is Ok even if this savepoint is actually a transaction | |
2750 ** savepoint (and therefore should not prompt xSavepoint()) callbacks. | |
2751 ** If this is a transaction savepoint being opened, it is guaranteed | |
2752 ** that the db->aVTrans[] array is empty. */ | |
2753 assert( db->autoCommit==0 || db->nVTrans==0 ); | |
2754 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, | |
2755 db->nStatement+db->nSavepoint); | |
2756 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
2757 #endif | |
2758 | |
2759 /* Create a new savepoint structure. */ | |
2760 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1); | |
2761 if( pNew ){ | |
2762 pNew->zName = (char *)&pNew[1]; | |
2763 memcpy(pNew->zName, zName, nName+1); | |
2764 | |
2765 /* If there is no open transaction, then mark this as a special | |
2766 ** "transaction savepoint". */ | |
2767 if( db->autoCommit ){ | |
2768 db->autoCommit = 0; | |
2769 db->isTransactionSavepoint = 1; | |
2770 }else{ | |
2771 db->nSavepoint++; | |
2772 } | |
2773 | |
2774 /* Link the new savepoint into the database handle's list. */ | |
2775 pNew->pNext = db->pSavepoint; | |
2776 db->pSavepoint = pNew; | |
2777 pNew->nDeferredCons = db->nDeferredCons; | |
2778 pNew->nDeferredImmCons = db->nDeferredImmCons; | |
2779 } | |
2780 } | |
2781 }else{ | |
2782 iSavepoint = 0; | |
2783 | |
2784 /* Find the named savepoint. If there is no such savepoint, then an | |
2785 ** an error is returned to the user. */ | |
2786 for( | |
2787 pSavepoint = db->pSavepoint; | |
2788 pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName); | |
2789 pSavepoint = pSavepoint->pNext | |
2790 ){ | |
2791 iSavepoint++; | |
2792 } | |
2793 if( !pSavepoint ){ | |
2794 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName); | |
2795 rc = SQLITE_ERROR; | |
2796 }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){ | |
2797 /* It is not possible to release (commit) a savepoint if there are | |
2798 ** active write statements. | |
2799 */ | |
2800 sqlite3SetString(&p->zErrMsg, db, | |
2801 "cannot release savepoint - SQL statements in progress" | |
2802 ); | |
2803 rc = SQLITE_BUSY; | |
2804 }else{ | |
2805 | |
2806 /* Determine whether or not this is a transaction savepoint. If so, | |
2807 ** and this is a RELEASE command, then the current transaction | |
2808 ** is committed. | |
2809 */ | |
2810 int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint; | |
2811 if( isTransaction && p1==SAVEPOINT_RELEASE ){ | |
2812 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ | |
2813 goto vdbe_return; | |
2814 } | |
2815 db->autoCommit = 1; | |
2816 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ | |
2817 p->pc = pc; | |
2818 db->autoCommit = 0; | |
2819 p->rc = rc = SQLITE_BUSY; | |
2820 goto vdbe_return; | |
2821 } | |
2822 db->isTransactionSavepoint = 0; | |
2823 rc = p->rc; | |
2824 }else{ | |
2825 int isSchemaChange; | |
2826 iSavepoint = db->nSavepoint - iSavepoint - 1; | |
2827 if( p1==SAVEPOINT_ROLLBACK ){ | |
2828 isSchemaChange = (db->flags & SQLITE_InternChanges)!=0; | |
2829 for(ii=0; ii<db->nDb; ii++){ | |
2830 rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, | |
2831 SQLITE_ABORT_ROLLBACK, | |
2832 isSchemaChange==0); | |
2833 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
2834 } | |
2835 }else{ | |
2836 isSchemaChange = 0; | |
2837 } | |
2838 for(ii=0; ii<db->nDb; ii++){ | |
2839 rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint); | |
2840 if( rc!=SQLITE_OK ){ | |
2841 goto abort_due_to_error; | |
2842 } | |
2843 } | |
2844 if( isSchemaChange ){ | |
2845 sqlite3ExpirePreparedStatements(db); | |
2846 sqlite3ResetAllSchemasOfConnection(db); | |
2847 db->flags = (db->flags | SQLITE_InternChanges); | |
2848 } | |
2849 } | |
2850 | |
2851 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all | |
2852 ** savepoints nested inside of the savepoint being operated on. */ | |
2853 while( db->pSavepoint!=pSavepoint ){ | |
2854 pTmp = db->pSavepoint; | |
2855 db->pSavepoint = pTmp->pNext; | |
2856 sqlite3DbFree(db, pTmp); | |
2857 db->nSavepoint--; | |
2858 } | |
2859 | |
2860 /* If it is a RELEASE, then destroy the savepoint being operated on | |
2861 ** too. If it is a ROLLBACK TO, then set the number of deferred | |
2862 ** constraint violations present in the database to the value stored | |
2863 ** when the savepoint was created. */ | |
2864 if( p1==SAVEPOINT_RELEASE ){ | |
2865 assert( pSavepoint==db->pSavepoint ); | |
2866 db->pSavepoint = pSavepoint->pNext; | |
2867 sqlite3DbFree(db, pSavepoint); | |
2868 if( !isTransaction ){ | |
2869 db->nSavepoint--; | |
2870 } | |
2871 }else{ | |
2872 db->nDeferredCons = pSavepoint->nDeferredCons; | |
2873 db->nDeferredImmCons = pSavepoint->nDeferredImmCons; | |
2874 } | |
2875 | |
2876 if( !isTransaction ){ | |
2877 rc = sqlite3VtabSavepoint(db, p1, iSavepoint); | |
2878 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
2879 } | |
2880 } | |
2881 } | |
2882 | |
2883 break; | |
2884 } | |
2885 | |
2886 /* Opcode: AutoCommit P1 P2 * * * | |
2887 ** | |
2888 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll | |
2889 ** back any currently active btree transactions. If there are any active | |
2890 ** VMs (apart from this one), then a ROLLBACK fails. A COMMIT fails if | |
2891 ** there are active writing VMs or active VMs that use shared cache. | |
2892 ** | |
2893 ** This instruction causes the VM to halt. | |
2894 */ | |
2895 case OP_AutoCommit: { | |
2896 int desiredAutoCommit; | |
2897 int iRollback; | |
2898 int turnOnAC; | |
2899 | |
2900 desiredAutoCommit = pOp->p1; | |
2901 iRollback = pOp->p2; | |
2902 turnOnAC = desiredAutoCommit && !db->autoCommit; | |
2903 assert( desiredAutoCommit==1 || desiredAutoCommit==0 ); | |
2904 assert( desiredAutoCommit==1 || iRollback==0 ); | |
2905 assert( db->nVdbeActive>0 ); /* At least this one VM is active */ | |
2906 assert( p->bIsReader ); | |
2907 | |
2908 #if 0 | |
2909 if( turnOnAC && iRollback && db->nVdbeActive>1 ){ | |
2910 /* If this instruction implements a ROLLBACK and other VMs are | |
2911 ** still running, and a transaction is active, return an error indicating | |
2912 ** that the other VMs must complete first. | |
2913 */ | |
2914 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - " | |
2915 "SQL statements in progress"); | |
2916 rc = SQLITE_BUSY; | |
2917 }else | |
2918 #endif | |
2919 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){ | |
2920 /* If this instruction implements a COMMIT and other VMs are writing | |
2921 ** return an error indicating that the other VMs must complete first. | |
2922 */ | |
2923 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - " | |
2924 "SQL statements in progress"); | |
2925 rc = SQLITE_BUSY; | |
2926 }else if( desiredAutoCommit!=db->autoCommit ){ | |
2927 if( iRollback ){ | |
2928 assert( desiredAutoCommit==1 ); | |
2929 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | |
2930 db->autoCommit = 1; | |
2931 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ | |
2932 goto vdbe_return; | |
2933 }else{ | |
2934 db->autoCommit = (u8)desiredAutoCommit; | |
2935 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ | |
2936 p->pc = pc; | |
2937 db->autoCommit = (u8)(1-desiredAutoCommit); | |
2938 p->rc = rc = SQLITE_BUSY; | |
2939 goto vdbe_return; | |
2940 } | |
2941 } | |
2942 assert( db->nStatement==0 ); | |
2943 sqlite3CloseSavepoints(db); | |
2944 if( p->rc==SQLITE_OK ){ | |
2945 rc = SQLITE_DONE; | |
2946 }else{ | |
2947 rc = SQLITE_ERROR; | |
2948 } | |
2949 goto vdbe_return; | |
2950 }else{ | |
2951 sqlite3SetString(&p->zErrMsg, db, | |
2952 (!desiredAutoCommit)?"cannot start a transaction within a transaction":( | |
2953 (iRollback)?"cannot rollback - no transaction is active": | |
2954 "cannot commit - no transaction is active")); | |
2955 | |
2956 rc = SQLITE_ERROR; | |
2957 } | |
2958 break; | |
2959 } | |
2960 | |
2961 /* Opcode: Transaction P1 P2 P3 P4 P5 | |
2962 ** | |
2963 ** Begin a transaction on database P1 if a transaction is not already | |
2964 ** active. | |
2965 ** If P2 is non-zero, then a write-transaction is started, or if a | |
2966 ** read-transaction is already active, it is upgraded to a write-transaction. | |
2967 ** If P2 is zero, then a read-transaction is started. | |
2968 ** | |
2969 ** P1 is the index of the database file on which the transaction is | |
2970 ** started. Index 0 is the main database file and index 1 is the | |
2971 ** file used for temporary tables. Indices of 2 or more are used for | |
2972 ** attached databases. | |
2973 ** | |
2974 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is | |
2975 ** true (this flag is set if the Vdbe may modify more than one row and may | |
2976 ** throw an ABORT exception), a statement transaction may also be opened. | |
2977 ** More specifically, a statement transaction is opened iff the database | |
2978 ** connection is currently not in autocommit mode, or if there are other | |
2979 ** active statements. A statement transaction allows the changes made by this | |
2980 ** VDBE to be rolled back after an error without having to roll back the | |
2981 ** entire transaction. If no error is encountered, the statement transaction | |
2982 ** will automatically commit when the VDBE halts. | |
2983 ** | |
2984 ** If P5!=0 then this opcode also checks the schema cookie against P3 | |
2985 ** and the schema generation counter against P4. | |
2986 ** The cookie changes its value whenever the database schema changes. | |
2987 ** This operation is used to detect when that the cookie has changed | |
2988 ** and that the current process needs to reread the schema. If the schema | |
2989 ** cookie in P3 differs from the schema cookie in the database header or | |
2990 ** if the schema generation counter in P4 differs from the current | |
2991 ** generation counter, then an SQLITE_SCHEMA error is raised and execution | |
2992 ** halts. The sqlite3_step() wrapper function might then reprepare the | |
2993 ** statement and rerun it from the beginning. | |
2994 */ | |
2995 case OP_Transaction: { | |
2996 Btree *pBt; | |
2997 int iMeta; | |
2998 int iGen; | |
2999 | |
3000 assert( p->bIsReader ); | |
3001 assert( p->readOnly==0 || pOp->p2==0 ); | |
3002 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
3003 assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
3004 if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){ | |
3005 rc = SQLITE_READONLY; | |
3006 goto abort_due_to_error; | |
3007 } | |
3008 pBt = db->aDb[pOp->p1].pBt; | |
3009 | |
3010 if( pBt ){ | |
3011 rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); | |
3012 if( rc==SQLITE_BUSY ){ | |
3013 p->pc = pc; | |
3014 p->rc = rc = SQLITE_BUSY; | |
3015 goto vdbe_return; | |
3016 } | |
3017 if( rc!=SQLITE_OK ){ | |
3018 goto abort_due_to_error; | |
3019 } | |
3020 | |
3021 if( pOp->p2 && p->usesStmtJournal | |
3022 && (db->autoCommit==0 || db->nVdbeRead>1) | |
3023 ){ | |
3024 assert( sqlite3BtreeIsInTrans(pBt) ); | |
3025 if( p->iStatement==0 ){ | |
3026 assert( db->nStatement>=0 && db->nSavepoint>=0 ); | |
3027 db->nStatement++; | |
3028 p->iStatement = db->nSavepoint + db->nStatement; | |
3029 } | |
3030 | |
3031 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1); | |
3032 if( rc==SQLITE_OK ){ | |
3033 rc = sqlite3BtreeBeginStmt(pBt, p->iStatement); | |
3034 } | |
3035 | |
3036 /* Store the current value of the database handles deferred constraint | |
3037 ** counter. If the statement transaction needs to be rolled back, | |
3038 ** the value of this counter needs to be restored too. */ | |
3039 p->nStmtDefCons = db->nDeferredCons; | |
3040 p->nStmtDefImmCons = db->nDeferredImmCons; | |
3041 } | |
3042 | |
3043 /* Gather the schema version number for checking */ | |
3044 sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta); | |
3045 iGen = db->aDb[pOp->p1].pSchema->iGeneration; | |
3046 }else{ | |
3047 iGen = iMeta = 0; | |
3048 } | |
3049 assert( pOp->p5==0 || pOp->p4type==P4_INT32 ); | |
3050 if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){ | |
3051 sqlite3DbFree(db, p->zErrMsg); | |
3052 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); | |
3053 /* If the schema-cookie from the database file matches the cookie | |
3054 ** stored with the in-memory representation of the schema, do | |
3055 ** not reload the schema from the database file. | |
3056 ** | |
3057 ** If virtual-tables are in use, this is not just an optimization. | |
3058 ** Often, v-tables store their data in other SQLite tables, which | |
3059 ** are queried from within xNext() and other v-table methods using | |
3060 ** prepared queries. If such a query is out-of-date, we do not want to | |
3061 ** discard the database schema, as the user code implementing the | |
3062 ** v-table would have to be ready for the sqlite3_vtab structure itself | |
3063 ** to be invalidated whenever sqlite3_step() is called from within | |
3064 ** a v-table method. | |
3065 */ | |
3066 if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){ | |
3067 sqlite3ResetOneSchema(db, pOp->p1); | |
3068 } | |
3069 p->expired = 1; | |
3070 rc = SQLITE_SCHEMA; | |
3071 } | |
3072 break; | |
3073 } | |
3074 | |
3075 /* Opcode: ReadCookie P1 P2 P3 * * | |
3076 ** | |
3077 ** Read cookie number P3 from database P1 and write it into register P2. | |
3078 ** P3==1 is the schema version. P3==2 is the database format. | |
3079 ** P3==3 is the recommended pager cache size, and so forth. P1==0 is | |
3080 ** the main database file and P1==1 is the database file used to store | |
3081 ** temporary tables. | |
3082 ** | |
3083 ** There must be a read-lock on the database (either a transaction | |
3084 ** must be started or there must be an open cursor) before | |
3085 ** executing this instruction. | |
3086 */ | |
3087 case OP_ReadCookie: { /* out2-prerelease */ | |
3088 int iMeta; | |
3089 int iDb; | |
3090 int iCookie; | |
3091 | |
3092 assert( p->bIsReader ); | |
3093 iDb = pOp->p1; | |
3094 iCookie = pOp->p3; | |
3095 assert( pOp->p3<SQLITE_N_BTREE_META ); | |
3096 assert( iDb>=0 && iDb<db->nDb ); | |
3097 assert( db->aDb[iDb].pBt!=0 ); | |
3098 assert( DbMaskTest(p->btreeMask, iDb) ); | |
3099 | |
3100 sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); | |
3101 pOut->u.i = iMeta; | |
3102 break; | |
3103 } | |
3104 | |
3105 /* Opcode: SetCookie P1 P2 P3 * * | |
3106 ** | |
3107 ** Write the content of register P3 (interpreted as an integer) | |
3108 ** into cookie number P2 of database P1. P2==1 is the schema version. | |
3109 ** P2==2 is the database format. P2==3 is the recommended pager cache | |
3110 ** size, and so forth. P1==0 is the main database file and P1==1 is the | |
3111 ** database file used to store temporary tables. | |
3112 ** | |
3113 ** A transaction must be started before executing this opcode. | |
3114 */ | |
3115 case OP_SetCookie: { /* in3 */ | |
3116 Db *pDb; | |
3117 assert( pOp->p2<SQLITE_N_BTREE_META ); | |
3118 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
3119 assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
3120 assert( p->readOnly==0 ); | |
3121 pDb = &db->aDb[pOp->p1]; | |
3122 assert( pDb->pBt!=0 ); | |
3123 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) ); | |
3124 pIn3 = &aMem[pOp->p3]; | |
3125 sqlite3VdbeMemIntegerify(pIn3); | |
3126 /* See note about index shifting on OP_ReadCookie */ | |
3127 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i); | |
3128 if( pOp->p2==BTREE_SCHEMA_VERSION ){ | |
3129 /* When the schema cookie changes, record the new cookie internally */ | |
3130 pDb->pSchema->schema_cookie = (int)pIn3->u.i; | |
3131 db->flags |= SQLITE_InternChanges; | |
3132 }else if( pOp->p2==BTREE_FILE_FORMAT ){ | |
3133 /* Record changes in the file format */ | |
3134 pDb->pSchema->file_format = (u8)pIn3->u.i; | |
3135 } | |
3136 if( pOp->p1==1 ){ | |
3137 /* Invalidate all prepared statements whenever the TEMP database | |
3138 ** schema is changed. Ticket #1644 */ | |
3139 sqlite3ExpirePreparedStatements(db); | |
3140 p->expired = 0; | |
3141 } | |
3142 break; | |
3143 } | |
3144 | |
3145 /* Opcode: OpenRead P1 P2 P3 P4 P5 | |
3146 ** Synopsis: root=P2 iDb=P3 | |
3147 ** | |
3148 ** Open a read-only cursor for the database table whose root page is | |
3149 ** P2 in a database file. The database file is determined by P3. | |
3150 ** P3==0 means the main database, P3==1 means the database used for | |
3151 ** temporary tables, and P3>1 means used the corresponding attached | |
3152 ** database. Give the new cursor an identifier of P1. The P1 | |
3153 ** values need not be contiguous but all P1 values should be small integers. | |
3154 ** It is an error for P1 to be negative. | |
3155 ** | |
3156 ** If P5!=0 then use the content of register P2 as the root page, not | |
3157 ** the value of P2 itself. | |
3158 ** | |
3159 ** There will be a read lock on the database whenever there is an | |
3160 ** open cursor. If the database was unlocked prior to this instruction | |
3161 ** then a read lock is acquired as part of this instruction. A read | |
3162 ** lock allows other processes to read the database but prohibits | |
3163 ** any other process from modifying the database. The read lock is | |
3164 ** released when all cursors are closed. If this instruction attempts | |
3165 ** to get a read lock but fails, the script terminates with an | |
3166 ** SQLITE_BUSY error code. | |
3167 ** | |
3168 ** The P4 value may be either an integer (P4_INT32) or a pointer to | |
3169 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo | |
3170 ** structure, then said structure defines the content and collating | |
3171 ** sequence of the index being opened. Otherwise, if P4 is an integer | |
3172 ** value, it is set to the number of columns in the table. | |
3173 ** | |
3174 ** See also: OpenWrite, ReopenIdx | |
3175 */ | |
3176 /* Opcode: ReopenIdx P1 P2 P3 P4 P5 | |
3177 ** Synopsis: root=P2 iDb=P3 | |
3178 ** | |
3179 ** The ReopenIdx opcode works exactly like ReadOpen except that it first | |
3180 ** checks to see if the cursor on P1 is already open with a root page | |
3181 ** number of P2 and if it is this opcode becomes a no-op. In other words, | |
3182 ** if the cursor is already open, do not reopen it. | |
3183 ** | |
3184 ** The ReopenIdx opcode may only be used with P5==0 and with P4 being | |
3185 ** a P4_KEYINFO object. Furthermore, the P3 value must be the same as | |
3186 ** every other ReopenIdx or OpenRead for the same cursor number. | |
3187 ** | |
3188 ** See the OpenRead opcode documentation for additional information. | |
3189 */ | |
3190 /* Opcode: OpenWrite P1 P2 P3 P4 P5 | |
3191 ** Synopsis: root=P2 iDb=P3 | |
3192 ** | |
3193 ** Open a read/write cursor named P1 on the table or index whose root | |
3194 ** page is P2. Or if P5!=0 use the content of register P2 to find the | |
3195 ** root page. | |
3196 ** | |
3197 ** The P4 value may be either an integer (P4_INT32) or a pointer to | |
3198 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo | |
3199 ** structure, then said structure defines the content and collating | |
3200 ** sequence of the index being opened. Otherwise, if P4 is an integer | |
3201 ** value, it is set to the number of columns in the table, or to the | |
3202 ** largest index of any column of the table that is actually used. | |
3203 ** | |
3204 ** This instruction works just like OpenRead except that it opens the cursor | |
3205 ** in read/write mode. For a given table, there can be one or more read-only | |
3206 ** cursors or a single read/write cursor but not both. | |
3207 ** | |
3208 ** See also OpenRead. | |
3209 */ | |
3210 case OP_ReopenIdx: { | |
3211 VdbeCursor *pCur; | |
3212 | |
3213 assert( pOp->p5==0 ); | |
3214 assert( pOp->p4type==P4_KEYINFO ); | |
3215 pCur = p->apCsr[pOp->p1]; | |
3216 if( pCur && pCur->pgnoRoot==(u32)pOp->p2 ){ | |
3217 assert( pCur->iDb==pOp->p3 ); /* Guaranteed by the code generator */ | |
3218 break; | |
3219 } | |
3220 /* If the cursor is not currently open or is open on a different | |
3221 ** index, then fall through into OP_OpenRead to force a reopen */ | |
3222 } | |
3223 case OP_OpenRead: | |
3224 case OP_OpenWrite: { | |
3225 int nField; | |
3226 KeyInfo *pKeyInfo; | |
3227 int p2; | |
3228 int iDb; | |
3229 int wrFlag; | |
3230 Btree *pX; | |
3231 VdbeCursor *pCur; | |
3232 Db *pDb; | |
3233 | |
3234 assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 ); | |
3235 assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 ); | |
3236 assert( p->bIsReader ); | |
3237 assert( pOp->opcode==OP_OpenRead || pOp->opcode==OP_ReopenIdx | |
3238 || p->readOnly==0 ); | |
3239 | |
3240 if( p->expired ){ | |
3241 rc = SQLITE_ABORT_ROLLBACK; | |
3242 break; | |
3243 } | |
3244 | |
3245 nField = 0; | |
3246 pKeyInfo = 0; | |
3247 p2 = pOp->p2; | |
3248 iDb = pOp->p3; | |
3249 assert( iDb>=0 && iDb<db->nDb ); | |
3250 assert( DbMaskTest(p->btreeMask, iDb) ); | |
3251 pDb = &db->aDb[iDb]; | |
3252 pX = pDb->pBt; | |
3253 assert( pX!=0 ); | |
3254 if( pOp->opcode==OP_OpenWrite ){ | |
3255 wrFlag = 1; | |
3256 assert( sqlite3SchemaMutexHeld(db, iDb, 0) ); | |
3257 if( pDb->pSchema->file_format < p->minWriteFileFormat ){ | |
3258 p->minWriteFileFormat = pDb->pSchema->file_format; | |
3259 } | |
3260 }else{ | |
3261 wrFlag = 0; | |
3262 } | |
3263 if( pOp->p5 & OPFLAG_P2ISREG ){ | |
3264 assert( p2>0 ); | |
3265 assert( p2<=(p->nMem-p->nCursor) ); | |
3266 pIn2 = &aMem[p2]; | |
3267 assert( memIsValid(pIn2) ); | |
3268 assert( (pIn2->flags & MEM_Int)!=0 ); | |
3269 sqlite3VdbeMemIntegerify(pIn2); | |
3270 p2 = (int)pIn2->u.i; | |
3271 /* The p2 value always comes from a prior OP_CreateTable opcode and | |
3272 ** that opcode will always set the p2 value to 2 or more or else fail. | |
3273 ** If there were a failure, the prepared statement would have halted | |
3274 ** before reaching this instruction. */ | |
3275 if( NEVER(p2<2) ) { | |
3276 rc = SQLITE_CORRUPT_BKPT; | |
3277 goto abort_due_to_error; | |
3278 } | |
3279 } | |
3280 if( pOp->p4type==P4_KEYINFO ){ | |
3281 pKeyInfo = pOp->p4.pKeyInfo; | |
3282 assert( pKeyInfo->enc==ENC(db) ); | |
3283 assert( pKeyInfo->db==db ); | |
3284 nField = pKeyInfo->nField+pKeyInfo->nXField; | |
3285 }else if( pOp->p4type==P4_INT32 ){ | |
3286 nField = pOp->p4.i; | |
3287 } | |
3288 assert( pOp->p1>=0 ); | |
3289 assert( nField>=0 ); | |
3290 testcase( nField==0 ); /* Table with INTEGER PRIMARY KEY and nothing else */ | |
3291 pCur = allocateCursor(p, pOp->p1, nField, iDb, 1); | |
3292 if( pCur==0 ) goto no_mem; | |
3293 pCur->nullRow = 1; | |
3294 pCur->isOrdered = 1; | |
3295 pCur->pgnoRoot = p2; | |
3296 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor); | |
3297 pCur->pKeyInfo = pKeyInfo; | |
3298 assert( OPFLAG_BULKCSR==BTREE_BULKLOAD ); | |
3299 sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR)); | |
3300 | |
3301 /* Set the VdbeCursor.isTable variable. Previous versions of | |
3302 ** SQLite used to check if the root-page flags were sane at this point | |
3303 ** and report database corruption if they were not, but this check has | |
3304 ** since moved into the btree layer. */ | |
3305 pCur->isTable = pOp->p4type!=P4_KEYINFO; | |
3306 break; | |
3307 } | |
3308 | |
3309 /* Opcode: OpenEphemeral P1 P2 * P4 P5 | |
3310 ** Synopsis: nColumn=P2 | |
3311 ** | |
3312 ** Open a new cursor P1 to a transient table. | |
3313 ** The cursor is always opened read/write even if | |
3314 ** the main database is read-only. The ephemeral | |
3315 ** table is deleted automatically when the cursor is closed. | |
3316 ** | |
3317 ** P2 is the number of columns in the ephemeral table. | |
3318 ** The cursor points to a BTree table if P4==0 and to a BTree index | |
3319 ** if P4 is not 0. If P4 is not NULL, it points to a KeyInfo structure | |
3320 ** that defines the format of keys in the index. | |
3321 ** | |
3322 ** The P5 parameter can be a mask of the BTREE_* flags defined | |
3323 ** in btree.h. These flags control aspects of the operation of | |
3324 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are | |
3325 ** added automatically. | |
3326 */ | |
3327 /* Opcode: OpenAutoindex P1 P2 * P4 * | |
3328 ** Synopsis: nColumn=P2 | |
3329 ** | |
3330 ** This opcode works the same as OP_OpenEphemeral. It has a | |
3331 ** different name to distinguish its use. Tables created using | |
3332 ** by this opcode will be used for automatically created transient | |
3333 ** indices in joins. | |
3334 */ | |
3335 case OP_OpenAutoindex: | |
3336 case OP_OpenEphemeral: { | |
3337 VdbeCursor *pCx; | |
3338 KeyInfo *pKeyInfo; | |
3339 | |
3340 static const int vfsFlags = | |
3341 SQLITE_OPEN_READWRITE | | |
3342 SQLITE_OPEN_CREATE | | |
3343 SQLITE_OPEN_EXCLUSIVE | | |
3344 SQLITE_OPEN_DELETEONCLOSE | | |
3345 SQLITE_OPEN_TRANSIENT_DB; | |
3346 assert( pOp->p1>=0 ); | |
3347 assert( pOp->p2>=0 ); | |
3348 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); | |
3349 if( pCx==0 ) goto no_mem; | |
3350 pCx->nullRow = 1; | |
3351 pCx->isEphemeral = 1; | |
3352 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, | |
3353 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags); | |
3354 if( rc==SQLITE_OK ){ | |
3355 rc = sqlite3BtreeBeginTrans(pCx->pBt, 1); | |
3356 } | |
3357 if( rc==SQLITE_OK ){ | |
3358 /* If a transient index is required, create it by calling | |
3359 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before | |
3360 ** opening it. If a transient table is required, just use the | |
3361 ** automatically created table with root-page 1 (an BLOB_INTKEY table). | |
3362 */ | |
3363 if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){ | |
3364 int pgno; | |
3365 assert( pOp->p4type==P4_KEYINFO ); | |
3366 rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); | |
3367 if( rc==SQLITE_OK ){ | |
3368 assert( pgno==MASTER_ROOT+1 ); | |
3369 assert( pKeyInfo->db==db ); | |
3370 assert( pKeyInfo->enc==ENC(db) ); | |
3371 pCx->pKeyInfo = pKeyInfo; | |
3372 rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor); | |
3373 } | |
3374 pCx->isTable = 0; | |
3375 }else{ | |
3376 rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor); | |
3377 pCx->isTable = 1; | |
3378 } | |
3379 } | |
3380 pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED); | |
3381 break; | |
3382 } | |
3383 | |
3384 /* Opcode: SorterOpen P1 P2 P3 P4 * | |
3385 ** | |
3386 ** This opcode works like OP_OpenEphemeral except that it opens | |
3387 ** a transient index that is specifically designed to sort large | |
3388 ** tables using an external merge-sort algorithm. | |
3389 ** | |
3390 ** If argument P3 is non-zero, then it indicates that the sorter may | |
3391 ** assume that a stable sort considering the first P3 fields of each | |
3392 ** key is sufficient to produce the required results. | |
3393 */ | |
3394 case OP_SorterOpen: { | |
3395 VdbeCursor *pCx; | |
3396 | |
3397 assert( pOp->p1>=0 ); | |
3398 assert( pOp->p2>=0 ); | |
3399 pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1); | |
3400 if( pCx==0 ) goto no_mem; | |
3401 pCx->pKeyInfo = pOp->p4.pKeyInfo; | |
3402 assert( pCx->pKeyInfo->db==db ); | |
3403 assert( pCx->pKeyInfo->enc==ENC(db) ); | |
3404 rc = sqlite3VdbeSorterInit(db, pOp->p3, pCx); | |
3405 break; | |
3406 } | |
3407 | |
3408 /* Opcode: SequenceTest P1 P2 * * * | |
3409 ** Synopsis: if( cursor[P1].ctr++ ) pc = P2 | |
3410 ** | |
3411 ** P1 is a sorter cursor. If the sequence counter is currently zero, jump | |
3412 ** to P2. Regardless of whether or not the jump is taken, increment the | |
3413 ** the sequence value. | |
3414 */ | |
3415 case OP_SequenceTest: { | |
3416 VdbeCursor *pC; | |
3417 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3418 pC = p->apCsr[pOp->p1]; | |
3419 assert( pC->pSorter ); | |
3420 if( (pC->seqCount++)==0 ){ | |
3421 pc = pOp->p2 - 1; | |
3422 } | |
3423 break; | |
3424 } | |
3425 | |
3426 /* Opcode: OpenPseudo P1 P2 P3 * * | |
3427 ** Synopsis: P3 columns in r[P2] | |
3428 ** | |
3429 ** Open a new cursor that points to a fake table that contains a single | |
3430 ** row of data. The content of that one row is the content of memory | |
3431 ** register P2. In other words, cursor P1 becomes an alias for the | |
3432 ** MEM_Blob content contained in register P2. | |
3433 ** | |
3434 ** A pseudo-table created by this opcode is used to hold a single | |
3435 ** row output from the sorter so that the row can be decomposed into | |
3436 ** individual columns using the OP_Column opcode. The OP_Column opcode | |
3437 ** is the only cursor opcode that works with a pseudo-table. | |
3438 ** | |
3439 ** P3 is the number of fields in the records that will be stored by | |
3440 ** the pseudo-table. | |
3441 */ | |
3442 case OP_OpenPseudo: { | |
3443 VdbeCursor *pCx; | |
3444 | |
3445 assert( pOp->p1>=0 ); | |
3446 assert( pOp->p3>=0 ); | |
3447 pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0); | |
3448 if( pCx==0 ) goto no_mem; | |
3449 pCx->nullRow = 1; | |
3450 pCx->pseudoTableReg = pOp->p2; | |
3451 pCx->isTable = 1; | |
3452 assert( pOp->p5==0 ); | |
3453 break; | |
3454 } | |
3455 | |
3456 /* Opcode: Close P1 * * * * | |
3457 ** | |
3458 ** Close a cursor previously opened as P1. If P1 is not | |
3459 ** currently open, this instruction is a no-op. | |
3460 */ | |
3461 case OP_Close: { | |
3462 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3463 sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]); | |
3464 p->apCsr[pOp->p1] = 0; | |
3465 break; | |
3466 } | |
3467 | |
3468 /* Opcode: SeekGE P1 P2 P3 P4 * | |
3469 ** Synopsis: key=r[P3@P4] | |
3470 ** | |
3471 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), | |
3472 ** use the value in register P3 as the key. If cursor P1 refers | |
3473 ** to an SQL index, then P3 is the first in an array of P4 registers | |
3474 ** that are used as an unpacked index key. | |
3475 ** | |
3476 ** Reposition cursor P1 so that it points to the smallest entry that | |
3477 ** is greater than or equal to the key value. If there are no records | |
3478 ** greater than or equal to the key and P2 is not zero, then jump to P2. | |
3479 ** | |
3480 ** This opcode leaves the cursor configured to move in forward order, | |
3481 ** from the beginning toward the end. In other words, the cursor is | |
3482 ** configured to use Next, not Prev. | |
3483 ** | |
3484 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe | |
3485 */ | |
3486 /* Opcode: SeekGT P1 P2 P3 P4 * | |
3487 ** Synopsis: key=r[P3@P4] | |
3488 ** | |
3489 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), | |
3490 ** use the value in register P3 as a key. If cursor P1 refers | |
3491 ** to an SQL index, then P3 is the first in an array of P4 registers | |
3492 ** that are used as an unpacked index key. | |
3493 ** | |
3494 ** Reposition cursor P1 so that it points to the smallest entry that | |
3495 ** is greater than the key value. If there are no records greater than | |
3496 ** the key and P2 is not zero, then jump to P2. | |
3497 ** | |
3498 ** This opcode leaves the cursor configured to move in forward order, | |
3499 ** from the beginning toward the end. In other words, the cursor is | |
3500 ** configured to use Next, not Prev. | |
3501 ** | |
3502 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe | |
3503 */ | |
3504 /* Opcode: SeekLT P1 P2 P3 P4 * | |
3505 ** Synopsis: key=r[P3@P4] | |
3506 ** | |
3507 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), | |
3508 ** use the value in register P3 as a key. If cursor P1 refers | |
3509 ** to an SQL index, then P3 is the first in an array of P4 registers | |
3510 ** that are used as an unpacked index key. | |
3511 ** | |
3512 ** Reposition cursor P1 so that it points to the largest entry that | |
3513 ** is less than the key value. If there are no records less than | |
3514 ** the key and P2 is not zero, then jump to P2. | |
3515 ** | |
3516 ** This opcode leaves the cursor configured to move in reverse order, | |
3517 ** from the end toward the beginning. In other words, the cursor is | |
3518 ** configured to use Prev, not Next. | |
3519 ** | |
3520 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe | |
3521 */ | |
3522 /* Opcode: SeekLE P1 P2 P3 P4 * | |
3523 ** Synopsis: key=r[P3@P4] | |
3524 ** | |
3525 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys), | |
3526 ** use the value in register P3 as a key. If cursor P1 refers | |
3527 ** to an SQL index, then P3 is the first in an array of P4 registers | |
3528 ** that are used as an unpacked index key. | |
3529 ** | |
3530 ** Reposition cursor P1 so that it points to the largest entry that | |
3531 ** is less than or equal to the key value. If there are no records | |
3532 ** less than or equal to the key and P2 is not zero, then jump to P2. | |
3533 ** | |
3534 ** This opcode leaves the cursor configured to move in reverse order, | |
3535 ** from the end toward the beginning. In other words, the cursor is | |
3536 ** configured to use Prev, not Next. | |
3537 ** | |
3538 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt | |
3539 */ | |
3540 case OP_SeekLT: /* jump, in3 */ | |
3541 case OP_SeekLE: /* jump, in3 */ | |
3542 case OP_SeekGE: /* jump, in3 */ | |
3543 case OP_SeekGT: { /* jump, in3 */ | |
3544 int res; | |
3545 int oc; | |
3546 VdbeCursor *pC; | |
3547 UnpackedRecord r; | |
3548 int nField; | |
3549 i64 iKey; /* The rowid we are to seek to */ | |
3550 | |
3551 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3552 assert( pOp->p2!=0 ); | |
3553 pC = p->apCsr[pOp->p1]; | |
3554 assert( pC!=0 ); | |
3555 assert( pC->pseudoTableReg==0 ); | |
3556 assert( OP_SeekLE == OP_SeekLT+1 ); | |
3557 assert( OP_SeekGE == OP_SeekLT+2 ); | |
3558 assert( OP_SeekGT == OP_SeekLT+3 ); | |
3559 assert( pC->isOrdered ); | |
3560 assert( pC->pCursor!=0 ); | |
3561 oc = pOp->opcode; | |
3562 pC->nullRow = 0; | |
3563 #ifdef SQLITE_DEBUG | |
3564 pC->seekOp = pOp->opcode; | |
3565 #endif | |
3566 if( pC->isTable ){ | |
3567 /* The input value in P3 might be of any type: integer, real, string, | |
3568 ** blob, or NULL. But it needs to be an integer before we can do | |
3569 ** the seek, so convert it. */ | |
3570 pIn3 = &aMem[pOp->p3]; | |
3571 if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){ | |
3572 applyNumericAffinity(pIn3, 0); | |
3573 } | |
3574 iKey = sqlite3VdbeIntValue(pIn3); | |
3575 | |
3576 /* If the P3 value could not be converted into an integer without | |
3577 ** loss of information, then special processing is required... */ | |
3578 if( (pIn3->flags & MEM_Int)==0 ){ | |
3579 if( (pIn3->flags & MEM_Real)==0 ){ | |
3580 /* If the P3 value cannot be converted into any kind of a number, | |
3581 ** then the seek is not possible, so jump to P2 */ | |
3582 pc = pOp->p2 - 1; VdbeBranchTaken(1,2); | |
3583 break; | |
3584 } | |
3585 | |
3586 /* If the approximation iKey is larger than the actual real search | |
3587 ** term, substitute >= for > and < for <=. e.g. if the search term | |
3588 ** is 4.9 and the integer approximation 5: | |
3589 ** | |
3590 ** (x > 4.9) -> (x >= 5) | |
3591 ** (x <= 4.9) -> (x < 5) | |
3592 */ | |
3593 if( pIn3->u.r<(double)iKey ){ | |
3594 assert( OP_SeekGE==(OP_SeekGT-1) ); | |
3595 assert( OP_SeekLT==(OP_SeekLE-1) ); | |
3596 assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) ); | |
3597 if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--; | |
3598 } | |
3599 | |
3600 /* If the approximation iKey is smaller than the actual real search | |
3601 ** term, substitute <= for < and > for >=. */ | |
3602 else if( pIn3->u.r>(double)iKey ){ | |
3603 assert( OP_SeekLE==(OP_SeekLT+1) ); | |
3604 assert( OP_SeekGT==(OP_SeekGE+1) ); | |
3605 assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) ); | |
3606 if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++; | |
3607 } | |
3608 } | |
3609 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res); | |
3610 pC->movetoTarget = iKey; /* Used by OP_Delete */ | |
3611 if( rc!=SQLITE_OK ){ | |
3612 goto abort_due_to_error; | |
3613 } | |
3614 }else{ | |
3615 nField = pOp->p4.i; | |
3616 assert( pOp->p4type==P4_INT32 ); | |
3617 assert( nField>0 ); | |
3618 r.pKeyInfo = pC->pKeyInfo; | |
3619 r.nField = (u16)nField; | |
3620 | |
3621 /* The next line of code computes as follows, only faster: | |
3622 ** if( oc==OP_SeekGT || oc==OP_SeekLE ){ | |
3623 ** r.default_rc = -1; | |
3624 ** }else{ | |
3625 ** r.default_rc = +1; | |
3626 ** } | |
3627 */ | |
3628 r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1); | |
3629 assert( oc!=OP_SeekGT || r.default_rc==-1 ); | |
3630 assert( oc!=OP_SeekLE || r.default_rc==-1 ); | |
3631 assert( oc!=OP_SeekGE || r.default_rc==+1 ); | |
3632 assert( oc!=OP_SeekLT || r.default_rc==+1 ); | |
3633 | |
3634 r.aMem = &aMem[pOp->p3]; | |
3635 #ifdef SQLITE_DEBUG | |
3636 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } | |
3637 #endif | |
3638 ExpandBlob(r.aMem); | |
3639 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res); | |
3640 if( rc!=SQLITE_OK ){ | |
3641 goto abort_due_to_error; | |
3642 } | |
3643 } | |
3644 pC->deferredMoveto = 0; | |
3645 pC->cacheStatus = CACHE_STALE; | |
3646 #ifdef SQLITE_TEST | |
3647 sqlite3_search_count++; | |
3648 #endif | |
3649 if( oc>=OP_SeekGE ){ assert( oc==OP_SeekGE || oc==OP_SeekGT ); | |
3650 if( res<0 || (res==0 && oc==OP_SeekGT) ){ | |
3651 res = 0; | |
3652 rc = sqlite3BtreeNext(pC->pCursor, &res); | |
3653 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
3654 }else{ | |
3655 res = 0; | |
3656 } | |
3657 }else{ | |
3658 assert( oc==OP_SeekLT || oc==OP_SeekLE ); | |
3659 if( res>0 || (res==0 && oc==OP_SeekLT) ){ | |
3660 res = 0; | |
3661 rc = sqlite3BtreePrevious(pC->pCursor, &res); | |
3662 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
3663 }else{ | |
3664 /* res might be negative because the table is empty. Check to | |
3665 ** see if this is the case. | |
3666 */ | |
3667 res = sqlite3BtreeEof(pC->pCursor); | |
3668 } | |
3669 } | |
3670 assert( pOp->p2>0 ); | |
3671 VdbeBranchTaken(res!=0,2); | |
3672 if( res ){ | |
3673 pc = pOp->p2 - 1; | |
3674 } | |
3675 break; | |
3676 } | |
3677 | |
3678 /* Opcode: Seek P1 P2 * * * | |
3679 ** Synopsis: intkey=r[P2] | |
3680 ** | |
3681 ** P1 is an open table cursor and P2 is a rowid integer. Arrange | |
3682 ** for P1 to move so that it points to the rowid given by P2. | |
3683 ** | |
3684 ** This is actually a deferred seek. Nothing actually happens until | |
3685 ** the cursor is used to read a record. That way, if no reads | |
3686 ** occur, no unnecessary I/O happens. | |
3687 */ | |
3688 case OP_Seek: { /* in2 */ | |
3689 VdbeCursor *pC; | |
3690 | |
3691 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3692 pC = p->apCsr[pOp->p1]; | |
3693 assert( pC!=0 ); | |
3694 assert( pC->pCursor!=0 ); | |
3695 assert( pC->isTable ); | |
3696 pC->nullRow = 0; | |
3697 pIn2 = &aMem[pOp->p2]; | |
3698 pC->movetoTarget = sqlite3VdbeIntValue(pIn2); | |
3699 pC->deferredMoveto = 1; | |
3700 break; | |
3701 } | |
3702 | |
3703 | |
3704 /* Opcode: Found P1 P2 P3 P4 * | |
3705 ** Synopsis: key=r[P3@P4] | |
3706 ** | |
3707 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If | |
3708 ** P4>0 then register P3 is the first of P4 registers that form an unpacked | |
3709 ** record. | |
3710 ** | |
3711 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 | |
3712 ** is a prefix of any entry in P1 then a jump is made to P2 and | |
3713 ** P1 is left pointing at the matching entry. | |
3714 ** | |
3715 ** This operation leaves the cursor in a state where it can be | |
3716 ** advanced in the forward direction. The Next instruction will work, | |
3717 ** but not the Prev instruction. | |
3718 ** | |
3719 ** See also: NotFound, NoConflict, NotExists. SeekGe | |
3720 */ | |
3721 /* Opcode: NotFound P1 P2 P3 P4 * | |
3722 ** Synopsis: key=r[P3@P4] | |
3723 ** | |
3724 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If | |
3725 ** P4>0 then register P3 is the first of P4 registers that form an unpacked | |
3726 ** record. | |
3727 ** | |
3728 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 | |
3729 ** is not the prefix of any entry in P1 then a jump is made to P2. If P1 | |
3730 ** does contain an entry whose prefix matches the P3/P4 record then control | |
3731 ** falls through to the next instruction and P1 is left pointing at the | |
3732 ** matching entry. | |
3733 ** | |
3734 ** This operation leaves the cursor in a state where it cannot be | |
3735 ** advanced in either direction. In other words, the Next and Prev | |
3736 ** opcodes do not work after this operation. | |
3737 ** | |
3738 ** See also: Found, NotExists, NoConflict | |
3739 */ | |
3740 /* Opcode: NoConflict P1 P2 P3 P4 * | |
3741 ** Synopsis: key=r[P3@P4] | |
3742 ** | |
3743 ** If P4==0 then register P3 holds a blob constructed by MakeRecord. If | |
3744 ** P4>0 then register P3 is the first of P4 registers that form an unpacked | |
3745 ** record. | |
3746 ** | |
3747 ** Cursor P1 is on an index btree. If the record identified by P3 and P4 | |
3748 ** contains any NULL value, jump immediately to P2. If all terms of the | |
3749 ** record are not-NULL then a check is done to determine if any row in the | |
3750 ** P1 index btree has a matching key prefix. If there are no matches, jump | |
3751 ** immediately to P2. If there is a match, fall through and leave the P1 | |
3752 ** cursor pointing to the matching row. | |
3753 ** | |
3754 ** This opcode is similar to OP_NotFound with the exceptions that the | |
3755 ** branch is always taken if any part of the search key input is NULL. | |
3756 ** | |
3757 ** This operation leaves the cursor in a state where it cannot be | |
3758 ** advanced in either direction. In other words, the Next and Prev | |
3759 ** opcodes do not work after this operation. | |
3760 ** | |
3761 ** See also: NotFound, Found, NotExists | |
3762 */ | |
3763 case OP_NoConflict: /* jump, in3 */ | |
3764 case OP_NotFound: /* jump, in3 */ | |
3765 case OP_Found: { /* jump, in3 */ | |
3766 int alreadyExists; | |
3767 int ii; | |
3768 VdbeCursor *pC; | |
3769 int res; | |
3770 char *pFree; | |
3771 UnpackedRecord *pIdxKey; | |
3772 UnpackedRecord r; | |
3773 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7]; | |
3774 | |
3775 #ifdef SQLITE_TEST | |
3776 if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++; | |
3777 #endif | |
3778 | |
3779 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3780 assert( pOp->p4type==P4_INT32 ); | |
3781 pC = p->apCsr[pOp->p1]; | |
3782 assert( pC!=0 ); | |
3783 #ifdef SQLITE_DEBUG | |
3784 pC->seekOp = pOp->opcode; | |
3785 #endif | |
3786 pIn3 = &aMem[pOp->p3]; | |
3787 assert( pC->pCursor!=0 ); | |
3788 assert( pC->isTable==0 ); | |
3789 pFree = 0; /* Not needed. Only used to suppress a compiler warning. */ | |
3790 if( pOp->p4.i>0 ){ | |
3791 r.pKeyInfo = pC->pKeyInfo; | |
3792 r.nField = (u16)pOp->p4.i; | |
3793 r.aMem = pIn3; | |
3794 for(ii=0; ii<r.nField; ii++){ | |
3795 assert( memIsValid(&r.aMem[ii]) ); | |
3796 ExpandBlob(&r.aMem[ii]); | |
3797 #ifdef SQLITE_DEBUG | |
3798 if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]); | |
3799 #endif | |
3800 } | |
3801 pIdxKey = &r; | |
3802 }else{ | |
3803 pIdxKey = sqlite3VdbeAllocUnpackedRecord( | |
3804 pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree | |
3805 ); | |
3806 if( pIdxKey==0 ) goto no_mem; | |
3807 assert( pIn3->flags & MEM_Blob ); | |
3808 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */ | |
3809 sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); | |
3810 } | |
3811 pIdxKey->default_rc = 0; | |
3812 if( pOp->opcode==OP_NoConflict ){ | |
3813 /* For the OP_NoConflict opcode, take the jump if any of the | |
3814 ** input fields are NULL, since any key with a NULL will not | |
3815 ** conflict */ | |
3816 for(ii=0; ii<r.nField; ii++){ | |
3817 if( r.aMem[ii].flags & MEM_Null ){ | |
3818 pc = pOp->p2 - 1; VdbeBranchTaken(1,2); | |
3819 break; | |
3820 } | |
3821 } | |
3822 } | |
3823 rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); | |
3824 if( pOp->p4.i==0 ){ | |
3825 sqlite3DbFree(db, pFree); | |
3826 } | |
3827 if( rc!=SQLITE_OK ){ | |
3828 break; | |
3829 } | |
3830 pC->seekResult = res; | |
3831 alreadyExists = (res==0); | |
3832 pC->nullRow = 1-alreadyExists; | |
3833 pC->deferredMoveto = 0; | |
3834 pC->cacheStatus = CACHE_STALE; | |
3835 if( pOp->opcode==OP_Found ){ | |
3836 VdbeBranchTaken(alreadyExists!=0,2); | |
3837 if( alreadyExists ) pc = pOp->p2 - 1; | |
3838 }else{ | |
3839 VdbeBranchTaken(alreadyExists==0,2); | |
3840 if( !alreadyExists ) pc = pOp->p2 - 1; | |
3841 } | |
3842 break; | |
3843 } | |
3844 | |
3845 /* Opcode: NotExists P1 P2 P3 * * | |
3846 ** Synopsis: intkey=r[P3] | |
3847 ** | |
3848 ** P1 is the index of a cursor open on an SQL table btree (with integer | |
3849 ** keys). P3 is an integer rowid. If P1 does not contain a record with | |
3850 ** rowid P3 then jump immediately to P2. If P1 does contain a record | |
3851 ** with rowid P3 then leave the cursor pointing at that record and fall | |
3852 ** through to the next instruction. | |
3853 ** | |
3854 ** The OP_NotFound opcode performs the same operation on index btrees | |
3855 ** (with arbitrary multi-value keys). | |
3856 ** | |
3857 ** This opcode leaves the cursor in a state where it cannot be advanced | |
3858 ** in either direction. In other words, the Next and Prev opcodes will | |
3859 ** not work following this opcode. | |
3860 ** | |
3861 ** See also: Found, NotFound, NoConflict | |
3862 */ | |
3863 case OP_NotExists: { /* jump, in3 */ | |
3864 VdbeCursor *pC; | |
3865 BtCursor *pCrsr; | |
3866 int res; | |
3867 u64 iKey; | |
3868 | |
3869 pIn3 = &aMem[pOp->p3]; | |
3870 assert( pIn3->flags & MEM_Int ); | |
3871 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3872 pC = p->apCsr[pOp->p1]; | |
3873 assert( pC!=0 ); | |
3874 #ifdef SQLITE_DEBUG | |
3875 pC->seekOp = 0; | |
3876 #endif | |
3877 assert( pC->isTable ); | |
3878 assert( pC->pseudoTableReg==0 ); | |
3879 pCrsr = pC->pCursor; | |
3880 assert( pCrsr!=0 ); | |
3881 res = 0; | |
3882 iKey = pIn3->u.i; | |
3883 rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res); | |
3884 pC->movetoTarget = iKey; /* Used by OP_Delete */ | |
3885 pC->nullRow = 0; | |
3886 pC->cacheStatus = CACHE_STALE; | |
3887 pC->deferredMoveto = 0; | |
3888 VdbeBranchTaken(res!=0,2); | |
3889 if( res!=0 ){ | |
3890 pc = pOp->p2 - 1; | |
3891 } | |
3892 pC->seekResult = res; | |
3893 break; | |
3894 } | |
3895 | |
3896 /* Opcode: Sequence P1 P2 * * * | |
3897 ** Synopsis: r[P2]=cursor[P1].ctr++ | |
3898 ** | |
3899 ** Find the next available sequence number for cursor P1. | |
3900 ** Write the sequence number into register P2. | |
3901 ** The sequence number on the cursor is incremented after this | |
3902 ** instruction. | |
3903 */ | |
3904 case OP_Sequence: { /* out2-prerelease */ | |
3905 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3906 assert( p->apCsr[pOp->p1]!=0 ); | |
3907 pOut->u.i = p->apCsr[pOp->p1]->seqCount++; | |
3908 break; | |
3909 } | |
3910 | |
3911 | |
3912 /* Opcode: NewRowid P1 P2 P3 * * | |
3913 ** Synopsis: r[P2]=rowid | |
3914 ** | |
3915 ** Get a new integer record number (a.k.a "rowid") used as the key to a table. | |
3916 ** The record number is not previously used as a key in the database | |
3917 ** table that cursor P1 points to. The new record number is written | |
3918 ** written to register P2. | |
3919 ** | |
3920 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds | |
3921 ** the largest previously generated record number. No new record numbers are | |
3922 ** allowed to be less than this value. When this value reaches its maximum, | |
3923 ** an SQLITE_FULL error is generated. The P3 register is updated with the ' | |
3924 ** generated record number. This P3 mechanism is used to help implement the | |
3925 ** AUTOINCREMENT feature. | |
3926 */ | |
3927 case OP_NewRowid: { /* out2-prerelease */ | |
3928 i64 v; /* The new rowid */ | |
3929 VdbeCursor *pC; /* Cursor of table to get the new rowid */ | |
3930 int res; /* Result of an sqlite3BtreeLast() */ | |
3931 int cnt; /* Counter to limit the number of searches */ | |
3932 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ | |
3933 VdbeFrame *pFrame; /* Root frame of VDBE */ | |
3934 | |
3935 v = 0; | |
3936 res = 0; | |
3937 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
3938 pC = p->apCsr[pOp->p1]; | |
3939 assert( pC!=0 ); | |
3940 if( NEVER(pC->pCursor==0) ){ | |
3941 /* The zero initialization above is all that is needed */ | |
3942 }else{ | |
3943 /* The next rowid or record number (different terms for the same | |
3944 ** thing) is obtained in a two-step algorithm. | |
3945 ** | |
3946 ** First we attempt to find the largest existing rowid and add one | |
3947 ** to that. But if the largest existing rowid is already the maximum | |
3948 ** positive integer, we have to fall through to the second | |
3949 ** probabilistic algorithm | |
3950 ** | |
3951 ** The second algorithm is to select a rowid at random and see if | |
3952 ** it already exists in the table. If it does not exist, we have | |
3953 ** succeeded. If the random rowid does exist, we select a new one | |
3954 ** and try again, up to 100 times. | |
3955 */ | |
3956 assert( pC->isTable ); | |
3957 | |
3958 #ifdef SQLITE_32BIT_ROWID | |
3959 # define MAX_ROWID 0x7fffffff | |
3960 #else | |
3961 /* Some compilers complain about constants of the form 0x7fffffffffffffff. | |
3962 ** Others complain about 0x7ffffffffffffffffLL. The following macro seems | |
3963 ** to provide the constant while making all compilers happy. | |
3964 */ | |
3965 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff ) | |
3966 #endif | |
3967 | |
3968 if( !pC->useRandomRowid ){ | |
3969 rc = sqlite3BtreeLast(pC->pCursor, &res); | |
3970 if( rc!=SQLITE_OK ){ | |
3971 goto abort_due_to_error; | |
3972 } | |
3973 if( res ){ | |
3974 v = 1; /* IMP: R-61914-48074 */ | |
3975 }else{ | |
3976 assert( sqlite3BtreeCursorIsValid(pC->pCursor) ); | |
3977 rc = sqlite3BtreeKeySize(pC->pCursor, &v); | |
3978 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */ | |
3979 if( v>=MAX_ROWID ){ | |
3980 pC->useRandomRowid = 1; | |
3981 }else{ | |
3982 v++; /* IMP: R-29538-34987 */ | |
3983 } | |
3984 } | |
3985 } | |
3986 | |
3987 #ifndef SQLITE_OMIT_AUTOINCREMENT | |
3988 if( pOp->p3 ){ | |
3989 /* Assert that P3 is a valid memory cell. */ | |
3990 assert( pOp->p3>0 ); | |
3991 if( p->pFrame ){ | |
3992 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | |
3993 /* Assert that P3 is a valid memory cell. */ | |
3994 assert( pOp->p3<=pFrame->nMem ); | |
3995 pMem = &pFrame->aMem[pOp->p3]; | |
3996 }else{ | |
3997 /* Assert that P3 is a valid memory cell. */ | |
3998 assert( pOp->p3<=(p->nMem-p->nCursor) ); | |
3999 pMem = &aMem[pOp->p3]; | |
4000 memAboutToChange(p, pMem); | |
4001 } | |
4002 assert( memIsValid(pMem) ); | |
4003 | |
4004 REGISTER_TRACE(pOp->p3, pMem); | |
4005 sqlite3VdbeMemIntegerify(pMem); | |
4006 assert( (pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */ | |
4007 if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){ | |
4008 rc = SQLITE_FULL; /* IMP: R-12275-61338 */ | |
4009 goto abort_due_to_error; | |
4010 } | |
4011 if( v<pMem->u.i+1 ){ | |
4012 v = pMem->u.i + 1; | |
4013 } | |
4014 pMem->u.i = v; | |
4015 } | |
4016 #endif | |
4017 if( pC->useRandomRowid ){ | |
4018 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the | |
4019 ** largest possible integer (9223372036854775807) then the database | |
4020 ** engine starts picking positive candidate ROWIDs at random until | |
4021 ** it finds one that is not previously used. */ | |
4022 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is | |
4023 ** an AUTOINCREMENT table. */ | |
4024 cnt = 0; | |
4025 do{ | |
4026 sqlite3_randomness(sizeof(v), &v); | |
4027 v &= (MAX_ROWID>>1); v++; /* Ensure that v is greater than zero */ | |
4028 }while( ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v, | |
4029 0, &res))==SQLITE_OK) | |
4030 && (res==0) | |
4031 && (++cnt<100)); | |
4032 if( rc==SQLITE_OK && res==0 ){ | |
4033 rc = SQLITE_FULL; /* IMP: R-38219-53002 */ | |
4034 goto abort_due_to_error; | |
4035 } | |
4036 assert( v>0 ); /* EV: R-40812-03570 */ | |
4037 } | |
4038 pC->deferredMoveto = 0; | |
4039 pC->cacheStatus = CACHE_STALE; | |
4040 } | |
4041 pOut->u.i = v; | |
4042 break; | |
4043 } | |
4044 | |
4045 /* Opcode: Insert P1 P2 P3 P4 P5 | |
4046 ** Synopsis: intkey=r[P3] data=r[P2] | |
4047 ** | |
4048 ** Write an entry into the table of cursor P1. A new entry is | |
4049 ** created if it doesn't already exist or the data for an existing | |
4050 ** entry is overwritten. The data is the value MEM_Blob stored in register | |
4051 ** number P2. The key is stored in register P3. The key must | |
4052 ** be a MEM_Int. | |
4053 ** | |
4054 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is | |
4055 ** incremented (otherwise not). If the OPFLAG_LASTROWID flag of P5 is set, | |
4056 ** then rowid is stored for subsequent return by the | |
4057 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified). | |
4058 ** | |
4059 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of | |
4060 ** the last seek operation (OP_NotExists) was a success, then this | |
4061 ** operation will not attempt to find the appropriate row before doing | |
4062 ** the insert but will instead overwrite the row that the cursor is | |
4063 ** currently pointing to. Presumably, the prior OP_NotExists opcode | |
4064 ** has already positioned the cursor correctly. This is an optimization | |
4065 ** that boosts performance by avoiding redundant seeks. | |
4066 ** | |
4067 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an | |
4068 ** UPDATE operation. Otherwise (if the flag is clear) then this opcode | |
4069 ** is part of an INSERT operation. The difference is only important to | |
4070 ** the update hook. | |
4071 ** | |
4072 ** Parameter P4 may point to a string containing the table-name, or | |
4073 ** may be NULL. If it is not NULL, then the update-hook | |
4074 ** (sqlite3.xUpdateCallback) is invoked following a successful insert. | |
4075 ** | |
4076 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically | |
4077 ** allocated, then ownership of P2 is transferred to the pseudo-cursor | |
4078 ** and register P2 becomes ephemeral. If the cursor is changed, the | |
4079 ** value of register P2 will then change. Make sure this does not | |
4080 ** cause any problems.) | |
4081 ** | |
4082 ** This instruction only works on tables. The equivalent instruction | |
4083 ** for indices is OP_IdxInsert. | |
4084 */ | |
4085 /* Opcode: InsertInt P1 P2 P3 P4 P5 | |
4086 ** Synopsis: intkey=P3 data=r[P2] | |
4087 ** | |
4088 ** This works exactly like OP_Insert except that the key is the | |
4089 ** integer value P3, not the value of the integer stored in register P3. | |
4090 */ | |
4091 case OP_Insert: | |
4092 case OP_InsertInt: { | |
4093 Mem *pData; /* MEM cell holding data for the record to be inserted */ | |
4094 Mem *pKey; /* MEM cell holding key for the record */ | |
4095 i64 iKey; /* The integer ROWID or key for the record to be inserted */ | |
4096 VdbeCursor *pC; /* Cursor to table into which insert is written */ | |
4097 int nZero; /* Number of zero-bytes to append */ | |
4098 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */ | |
4099 const char *zDb; /* database name - used by the update hook */ | |
4100 const char *zTbl; /* Table name - used by the opdate hook */ | |
4101 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */ | |
4102 | |
4103 pData = &aMem[pOp->p2]; | |
4104 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4105 assert( memIsValid(pData) ); | |
4106 pC = p->apCsr[pOp->p1]; | |
4107 assert( pC!=0 ); | |
4108 assert( pC->pCursor!=0 ); | |
4109 assert( pC->pseudoTableReg==0 ); | |
4110 assert( pC->isTable ); | |
4111 REGISTER_TRACE(pOp->p2, pData); | |
4112 | |
4113 if( pOp->opcode==OP_Insert ){ | |
4114 pKey = &aMem[pOp->p3]; | |
4115 assert( pKey->flags & MEM_Int ); | |
4116 assert( memIsValid(pKey) ); | |
4117 REGISTER_TRACE(pOp->p3, pKey); | |
4118 iKey = pKey->u.i; | |
4119 }else{ | |
4120 assert( pOp->opcode==OP_InsertInt ); | |
4121 iKey = pOp->p3; | |
4122 } | |
4123 | |
4124 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; | |
4125 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey; | |
4126 if( pData->flags & MEM_Null ){ | |
4127 pData->z = 0; | |
4128 pData->n = 0; | |
4129 }else{ | |
4130 assert( pData->flags & (MEM_Blob|MEM_Str) ); | |
4131 } | |
4132 seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0); | |
4133 if( pData->flags & MEM_Zero ){ | |
4134 nZero = pData->u.nZero; | |
4135 }else{ | |
4136 nZero = 0; | |
4137 } | |
4138 rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey, | |
4139 pData->z, pData->n, nZero, | |
4140 (pOp->p5 & OPFLAG_APPEND)!=0, seekResult | |
4141 ); | |
4142 pC->deferredMoveto = 0; | |
4143 pC->cacheStatus = CACHE_STALE; | |
4144 | |
4145 /* Invoke the update-hook if required. */ | |
4146 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){ | |
4147 zDb = db->aDb[pC->iDb].zName; | |
4148 zTbl = pOp->p4.z; | |
4149 op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT); | |
4150 assert( pC->isTable ); | |
4151 db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey); | |
4152 assert( pC->iDb>=0 ); | |
4153 } | |
4154 break; | |
4155 } | |
4156 | |
4157 /* Opcode: Delete P1 P2 * P4 * | |
4158 ** | |
4159 ** Delete the record at which the P1 cursor is currently pointing. | |
4160 ** | |
4161 ** The cursor will be left pointing at either the next or the previous | |
4162 ** record in the table. If it is left pointing at the next record, then | |
4163 ** the next Next instruction will be a no-op. Hence it is OK to delete | |
4164 ** a record from within a Next loop. | |
4165 ** | |
4166 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is | |
4167 ** incremented (otherwise not). | |
4168 ** | |
4169 ** P1 must not be pseudo-table. It has to be a real table with | |
4170 ** multiple rows. | |
4171 ** | |
4172 ** If P4 is not NULL, then it is the name of the table that P1 is | |
4173 ** pointing to. The update hook will be invoked, if it exists. | |
4174 ** If P4 is not NULL then the P1 cursor must have been positioned | |
4175 ** using OP_NotFound prior to invoking this opcode. | |
4176 */ | |
4177 case OP_Delete: { | |
4178 VdbeCursor *pC; | |
4179 | |
4180 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4181 pC = p->apCsr[pOp->p1]; | |
4182 assert( pC!=0 ); | |
4183 assert( pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */ | |
4184 assert( pC->deferredMoveto==0 ); | |
4185 | |
4186 #ifdef SQLITE_DEBUG | |
4187 /* The seek operation that positioned the cursor prior to OP_Delete will | |
4188 ** have also set the pC->movetoTarget field to the rowid of the row that | |
4189 ** is being deleted */ | |
4190 if( pOp->p4.z && pC->isTable ){ | |
4191 i64 iKey = 0; | |
4192 sqlite3BtreeKeySize(pC->pCursor, &iKey); | |
4193 assert( pC->movetoTarget==iKey ); | |
4194 } | |
4195 #endif | |
4196 | |
4197 rc = sqlite3BtreeDelete(pC->pCursor); | |
4198 pC->cacheStatus = CACHE_STALE; | |
4199 | |
4200 /* Invoke the update-hook if required. */ | |
4201 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){ | |
4202 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, | |
4203 db->aDb[pC->iDb].zName, pOp->p4.z, pC->movetoTarget); | |
4204 assert( pC->iDb>=0 ); | |
4205 } | |
4206 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++; | |
4207 break; | |
4208 } | |
4209 /* Opcode: ResetCount * * * * * | |
4210 ** | |
4211 ** The value of the change counter is copied to the database handle | |
4212 ** change counter (returned by subsequent calls to sqlite3_changes()). | |
4213 ** Then the VMs internal change counter resets to 0. | |
4214 ** This is used by trigger programs. | |
4215 */ | |
4216 case OP_ResetCount: { | |
4217 sqlite3VdbeSetChanges(db, p->nChange); | |
4218 p->nChange = 0; | |
4219 break; | |
4220 } | |
4221 | |
4222 /* Opcode: SorterCompare P1 P2 P3 P4 | |
4223 ** Synopsis: if key(P1)!=trim(r[P3],P4) goto P2 | |
4224 ** | |
4225 ** P1 is a sorter cursor. This instruction compares a prefix of the | |
4226 ** record blob in register P3 against a prefix of the entry that | |
4227 ** the sorter cursor currently points to. Only the first P4 fields | |
4228 ** of r[P3] and the sorter record are compared. | |
4229 ** | |
4230 ** If either P3 or the sorter contains a NULL in one of their significant | |
4231 ** fields (not counting the P4 fields at the end which are ignored) then | |
4232 ** the comparison is assumed to be equal. | |
4233 ** | |
4234 ** Fall through to next instruction if the two records compare equal to | |
4235 ** each other. Jump to P2 if they are different. | |
4236 */ | |
4237 case OP_SorterCompare: { | |
4238 VdbeCursor *pC; | |
4239 int res; | |
4240 int nKeyCol; | |
4241 | |
4242 pC = p->apCsr[pOp->p1]; | |
4243 assert( isSorter(pC) ); | |
4244 assert( pOp->p4type==P4_INT32 ); | |
4245 pIn3 = &aMem[pOp->p3]; | |
4246 nKeyCol = pOp->p4.i; | |
4247 res = 0; | |
4248 rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); | |
4249 VdbeBranchTaken(res!=0,2); | |
4250 if( res ){ | |
4251 pc = pOp->p2-1; | |
4252 } | |
4253 break; | |
4254 }; | |
4255 | |
4256 /* Opcode: SorterData P1 P2 P3 * * | |
4257 ** Synopsis: r[P2]=data | |
4258 ** | |
4259 ** Write into register P2 the current sorter data for sorter cursor P1. | |
4260 ** Then clear the column header cache on cursor P3. | |
4261 ** | |
4262 ** This opcode is normally use to move a record out of the sorter and into | |
4263 ** a register that is the source for a pseudo-table cursor created using | |
4264 ** OpenPseudo. That pseudo-table cursor is the one that is identified by | |
4265 ** parameter P3. Clearing the P3 column cache as part of this opcode saves | |
4266 ** us from having to issue a separate NullRow instruction to clear that cache. | |
4267 */ | |
4268 case OP_SorterData: { | |
4269 VdbeCursor *pC; | |
4270 | |
4271 pOut = &aMem[pOp->p2]; | |
4272 pC = p->apCsr[pOp->p1]; | |
4273 assert( isSorter(pC) ); | |
4274 rc = sqlite3VdbeSorterRowkey(pC, pOut); | |
4275 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) ); | |
4276 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4277 p->apCsr[pOp->p3]->cacheStatus = CACHE_STALE; | |
4278 break; | |
4279 } | |
4280 | |
4281 /* Opcode: RowData P1 P2 * * * | |
4282 ** Synopsis: r[P2]=data | |
4283 ** | |
4284 ** Write into register P2 the complete row data for cursor P1. | |
4285 ** There is no interpretation of the data. | |
4286 ** It is just copied onto the P2 register exactly as | |
4287 ** it is found in the database file. | |
4288 ** | |
4289 ** If the P1 cursor must be pointing to a valid row (not a NULL row) | |
4290 ** of a real table, not a pseudo-table. | |
4291 */ | |
4292 /* Opcode: RowKey P1 P2 * * * | |
4293 ** Synopsis: r[P2]=key | |
4294 ** | |
4295 ** Write into register P2 the complete row key for cursor P1. | |
4296 ** There is no interpretation of the data. | |
4297 ** The key is copied onto the P2 register exactly as | |
4298 ** it is found in the database file. | |
4299 ** | |
4300 ** If the P1 cursor must be pointing to a valid row (not a NULL row) | |
4301 ** of a real table, not a pseudo-table. | |
4302 */ | |
4303 case OP_RowKey: | |
4304 case OP_RowData: { | |
4305 VdbeCursor *pC; | |
4306 BtCursor *pCrsr; | |
4307 u32 n; | |
4308 i64 n64; | |
4309 | |
4310 pOut = &aMem[pOp->p2]; | |
4311 memAboutToChange(p, pOut); | |
4312 | |
4313 /* Note that RowKey and RowData are really exactly the same instruction */ | |
4314 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4315 pC = p->apCsr[pOp->p1]; | |
4316 assert( isSorter(pC)==0 ); | |
4317 assert( pC->isTable || pOp->opcode!=OP_RowData ); | |
4318 assert( pC->isTable==0 || pOp->opcode==OP_RowData ); | |
4319 assert( pC!=0 ); | |
4320 assert( pC->nullRow==0 ); | |
4321 assert( pC->pseudoTableReg==0 ); | |
4322 assert( pC->pCursor!=0 ); | |
4323 pCrsr = pC->pCursor; | |
4324 | |
4325 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or | |
4326 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate | |
4327 ** the cursor. If this where not the case, on of the following assert()s | |
4328 ** would fail. Should this ever change (because of changes in the code | |
4329 ** generator) then the fix would be to insert a call to | |
4330 ** sqlite3VdbeCursorMoveto(). | |
4331 */ | |
4332 assert( pC->deferredMoveto==0 ); | |
4333 assert( sqlite3BtreeCursorIsValid(pCrsr) ); | |
4334 #if 0 /* Not required due to the previous to assert() statements */ | |
4335 rc = sqlite3VdbeCursorMoveto(pC); | |
4336 if( rc!=SQLITE_OK ) goto abort_due_to_error; | |
4337 #endif | |
4338 | |
4339 if( pC->isTable==0 ){ | |
4340 assert( !pC->isTable ); | |
4341 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64); | |
4342 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */ | |
4343 if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
4344 goto too_big; | |
4345 } | |
4346 n = (u32)n64; | |
4347 }else{ | |
4348 VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n); | |
4349 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */ | |
4350 if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){ | |
4351 goto too_big; | |
4352 } | |
4353 } | |
4354 testcase( n==0 ); | |
4355 if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){ | |
4356 goto no_mem; | |
4357 } | |
4358 pOut->n = n; | |
4359 MemSetTypeFlag(pOut, MEM_Blob); | |
4360 if( pC->isTable==0 ){ | |
4361 rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z); | |
4362 }else{ | |
4363 rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z); | |
4364 } | |
4365 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */ | |
4366 UPDATE_MAX_BLOBSIZE(pOut); | |
4367 REGISTER_TRACE(pOp->p2, pOut); | |
4368 break; | |
4369 } | |
4370 | |
4371 /* Opcode: Rowid P1 P2 * * * | |
4372 ** Synopsis: r[P2]=rowid | |
4373 ** | |
4374 ** Store in register P2 an integer which is the key of the table entry that | |
4375 ** P1 is currently point to. | |
4376 ** | |
4377 ** P1 can be either an ordinary table or a virtual table. There used to | |
4378 ** be a separate OP_VRowid opcode for use with virtual tables, but this | |
4379 ** one opcode now works for both table types. | |
4380 */ | |
4381 case OP_Rowid: { /* out2-prerelease */ | |
4382 VdbeCursor *pC; | |
4383 i64 v; | |
4384 sqlite3_vtab *pVtab; | |
4385 const sqlite3_module *pModule; | |
4386 | |
4387 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4388 pC = p->apCsr[pOp->p1]; | |
4389 assert( pC!=0 ); | |
4390 assert( pC->pseudoTableReg==0 || pC->nullRow ); | |
4391 if( pC->nullRow ){ | |
4392 pOut->flags = MEM_Null; | |
4393 break; | |
4394 }else if( pC->deferredMoveto ){ | |
4395 v = pC->movetoTarget; | |
4396 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
4397 }else if( pC->pVtabCursor ){ | |
4398 pVtab = pC->pVtabCursor->pVtab; | |
4399 pModule = pVtab->pModule; | |
4400 assert( pModule->xRowid ); | |
4401 rc = pModule->xRowid(pC->pVtabCursor, &v); | |
4402 sqlite3VtabImportErrmsg(p, pVtab); | |
4403 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
4404 }else{ | |
4405 assert( pC->pCursor!=0 ); | |
4406 rc = sqlite3VdbeCursorRestore(pC); | |
4407 if( rc ) goto abort_due_to_error; | |
4408 if( pC->nullRow ){ | |
4409 pOut->flags = MEM_Null; | |
4410 break; | |
4411 } | |
4412 rc = sqlite3BtreeKeySize(pC->pCursor, &v); | |
4413 assert( rc==SQLITE_OK ); /* Always so because of CursorRestore() above */ | |
4414 } | |
4415 pOut->u.i = v; | |
4416 break; | |
4417 } | |
4418 | |
4419 /* Opcode: NullRow P1 * * * * | |
4420 ** | |
4421 ** Move the cursor P1 to a null row. Any OP_Column operations | |
4422 ** that occur while the cursor is on the null row will always | |
4423 ** write a NULL. | |
4424 */ | |
4425 case OP_NullRow: { | |
4426 VdbeCursor *pC; | |
4427 | |
4428 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4429 pC = p->apCsr[pOp->p1]; | |
4430 assert( pC!=0 ); | |
4431 pC->nullRow = 1; | |
4432 pC->cacheStatus = CACHE_STALE; | |
4433 if( pC->pCursor ){ | |
4434 sqlite3BtreeClearCursor(pC->pCursor); | |
4435 } | |
4436 break; | |
4437 } | |
4438 | |
4439 /* Opcode: Last P1 P2 * * * | |
4440 ** | |
4441 ** The next use of the Rowid or Column or Prev instruction for P1 | |
4442 ** will refer to the last entry in the database table or index. | |
4443 ** If the table or index is empty and P2>0, then jump immediately to P2. | |
4444 ** If P2 is 0 or if the table or index is not empty, fall through | |
4445 ** to the following instruction. | |
4446 ** | |
4447 ** This opcode leaves the cursor configured to move in reverse order, | |
4448 ** from the end toward the beginning. In other words, the cursor is | |
4449 ** configured to use Prev, not Next. | |
4450 */ | |
4451 case OP_Last: { /* jump */ | |
4452 VdbeCursor *pC; | |
4453 BtCursor *pCrsr; | |
4454 int res; | |
4455 | |
4456 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4457 pC = p->apCsr[pOp->p1]; | |
4458 assert( pC!=0 ); | |
4459 pCrsr = pC->pCursor; | |
4460 res = 0; | |
4461 assert( pCrsr!=0 ); | |
4462 rc = sqlite3BtreeLast(pCrsr, &res); | |
4463 pC->nullRow = (u8)res; | |
4464 pC->deferredMoveto = 0; | |
4465 pC->cacheStatus = CACHE_STALE; | |
4466 #ifdef SQLITE_DEBUG | |
4467 pC->seekOp = OP_Last; | |
4468 #endif | |
4469 if( pOp->p2>0 ){ | |
4470 VdbeBranchTaken(res!=0,2); | |
4471 if( res ) pc = pOp->p2 - 1; | |
4472 } | |
4473 break; | |
4474 } | |
4475 | |
4476 | |
4477 /* Opcode: Sort P1 P2 * * * | |
4478 ** | |
4479 ** This opcode does exactly the same thing as OP_Rewind except that | |
4480 ** it increments an undocumented global variable used for testing. | |
4481 ** | |
4482 ** Sorting is accomplished by writing records into a sorting index, | |
4483 ** then rewinding that index and playing it back from beginning to | |
4484 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the | |
4485 ** rewinding so that the global variable will be incremented and | |
4486 ** regression tests can determine whether or not the optimizer is | |
4487 ** correctly optimizing out sorts. | |
4488 */ | |
4489 case OP_SorterSort: /* jump */ | |
4490 case OP_Sort: { /* jump */ | |
4491 #ifdef SQLITE_TEST | |
4492 sqlite3_sort_count++; | |
4493 sqlite3_search_count--; | |
4494 #endif | |
4495 p->aCounter[SQLITE_STMTSTATUS_SORT]++; | |
4496 /* Fall through into OP_Rewind */ | |
4497 } | |
4498 /* Opcode: Rewind P1 P2 * * * | |
4499 ** | |
4500 ** The next use of the Rowid or Column or Next instruction for P1 | |
4501 ** will refer to the first entry in the database table or index. | |
4502 ** If the table or index is empty and P2>0, then jump immediately to P2. | |
4503 ** If P2 is 0 or if the table or index is not empty, fall through | |
4504 ** to the following instruction. | |
4505 ** | |
4506 ** This opcode leaves the cursor configured to move in forward order, | |
4507 ** from the beginning toward the end. In other words, the cursor is | |
4508 ** configured to use Next, not Prev. | |
4509 */ | |
4510 case OP_Rewind: { /* jump */ | |
4511 VdbeCursor *pC; | |
4512 BtCursor *pCrsr; | |
4513 int res; | |
4514 | |
4515 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4516 pC = p->apCsr[pOp->p1]; | |
4517 assert( pC!=0 ); | |
4518 assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) ); | |
4519 res = 1; | |
4520 #ifdef SQLITE_DEBUG | |
4521 pC->seekOp = OP_Rewind; | |
4522 #endif | |
4523 if( isSorter(pC) ){ | |
4524 rc = sqlite3VdbeSorterRewind(pC, &res); | |
4525 }else{ | |
4526 pCrsr = pC->pCursor; | |
4527 assert( pCrsr ); | |
4528 rc = sqlite3BtreeFirst(pCrsr, &res); | |
4529 pC->deferredMoveto = 0; | |
4530 pC->cacheStatus = CACHE_STALE; | |
4531 } | |
4532 pC->nullRow = (u8)res; | |
4533 assert( pOp->p2>0 && pOp->p2<p->nOp ); | |
4534 VdbeBranchTaken(res!=0,2); | |
4535 if( res ){ | |
4536 pc = pOp->p2 - 1; | |
4537 } | |
4538 break; | |
4539 } | |
4540 | |
4541 /* Opcode: Next P1 P2 P3 P4 P5 | |
4542 ** | |
4543 ** Advance cursor P1 so that it points to the next key/data pair in its | |
4544 ** table or index. If there are no more key/value pairs then fall through | |
4545 ** to the following instruction. But if the cursor advance was successful, | |
4546 ** jump immediately to P2. | |
4547 ** | |
4548 ** The Next opcode is only valid following an SeekGT, SeekGE, or | |
4549 ** OP_Rewind opcode used to position the cursor. Next is not allowed | |
4550 ** to follow SeekLT, SeekLE, or OP_Last. | |
4551 ** | |
4552 ** The P1 cursor must be for a real table, not a pseudo-table. P1 must have | |
4553 ** been opened prior to this opcode or the program will segfault. | |
4554 ** | |
4555 ** The P3 value is a hint to the btree implementation. If P3==1, that | |
4556 ** means P1 is an SQL index and that this instruction could have been | |
4557 ** omitted if that index had been unique. P3 is usually 0. P3 is | |
4558 ** always either 0 or 1. | |
4559 ** | |
4560 ** P4 is always of type P4_ADVANCE. The function pointer points to | |
4561 ** sqlite3BtreeNext(). | |
4562 ** | |
4563 ** If P5 is positive and the jump is taken, then event counter | |
4564 ** number P5-1 in the prepared statement is incremented. | |
4565 ** | |
4566 ** See also: Prev, NextIfOpen | |
4567 */ | |
4568 /* Opcode: NextIfOpen P1 P2 P3 P4 P5 | |
4569 ** | |
4570 ** This opcode works just like Next except that if cursor P1 is not | |
4571 ** open it behaves a no-op. | |
4572 */ | |
4573 /* Opcode: Prev P1 P2 P3 P4 P5 | |
4574 ** | |
4575 ** Back up cursor P1 so that it points to the previous key/data pair in its | |
4576 ** table or index. If there is no previous key/value pairs then fall through | |
4577 ** to the following instruction. But if the cursor backup was successful, | |
4578 ** jump immediately to P2. | |
4579 ** | |
4580 ** | |
4581 ** The Prev opcode is only valid following an SeekLT, SeekLE, or | |
4582 ** OP_Last opcode used to position the cursor. Prev is not allowed | |
4583 ** to follow SeekGT, SeekGE, or OP_Rewind. | |
4584 ** | |
4585 ** The P1 cursor must be for a real table, not a pseudo-table. If P1 is | |
4586 ** not open then the behavior is undefined. | |
4587 ** | |
4588 ** The P3 value is a hint to the btree implementation. If P3==1, that | |
4589 ** means P1 is an SQL index and that this instruction could have been | |
4590 ** omitted if that index had been unique. P3 is usually 0. P3 is | |
4591 ** always either 0 or 1. | |
4592 ** | |
4593 ** P4 is always of type P4_ADVANCE. The function pointer points to | |
4594 ** sqlite3BtreePrevious(). | |
4595 ** | |
4596 ** If P5 is positive and the jump is taken, then event counter | |
4597 ** number P5-1 in the prepared statement is incremented. | |
4598 */ | |
4599 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5 | |
4600 ** | |
4601 ** This opcode works just like Prev except that if cursor P1 is not | |
4602 ** open it behaves a no-op. | |
4603 */ | |
4604 case OP_SorterNext: { /* jump */ | |
4605 VdbeCursor *pC; | |
4606 int res; | |
4607 | |
4608 pC = p->apCsr[pOp->p1]; | |
4609 assert( isSorter(pC) ); | |
4610 res = 0; | |
4611 rc = sqlite3VdbeSorterNext(db, pC, &res); | |
4612 goto next_tail; | |
4613 case OP_PrevIfOpen: /* jump */ | |
4614 case OP_NextIfOpen: /* jump */ | |
4615 if( p->apCsr[pOp->p1]==0 ) break; | |
4616 /* Fall through */ | |
4617 case OP_Prev: /* jump */ | |
4618 case OP_Next: /* jump */ | |
4619 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4620 assert( pOp->p5<ArraySize(p->aCounter) ); | |
4621 pC = p->apCsr[pOp->p1]; | |
4622 res = pOp->p3; | |
4623 assert( pC!=0 ); | |
4624 assert( pC->deferredMoveto==0 ); | |
4625 assert( pC->pCursor ); | |
4626 assert( res==0 || (res==1 && pC->isTable==0) ); | |
4627 testcase( res==1 ); | |
4628 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext ); | |
4629 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious ); | |
4630 assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext ); | |
4631 assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious); | |
4632 | |
4633 /* The Next opcode is only used after SeekGT, SeekGE, and Rewind. | |
4634 ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */ | |
4635 assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen | |
4636 || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE | |
4637 || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found); | |
4638 assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen | |
4639 || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE | |
4640 || pC->seekOp==OP_Last ); | |
4641 | |
4642 rc = pOp->p4.xAdvance(pC->pCursor, &res); | |
4643 next_tail: | |
4644 pC->cacheStatus = CACHE_STALE; | |
4645 VdbeBranchTaken(res==0,2); | |
4646 if( res==0 ){ | |
4647 pC->nullRow = 0; | |
4648 pc = pOp->p2 - 1; | |
4649 p->aCounter[pOp->p5]++; | |
4650 #ifdef SQLITE_TEST | |
4651 sqlite3_search_count++; | |
4652 #endif | |
4653 }else{ | |
4654 pC->nullRow = 1; | |
4655 } | |
4656 goto check_for_interrupt; | |
4657 } | |
4658 | |
4659 /* Opcode: IdxInsert P1 P2 P3 * P5 | |
4660 ** Synopsis: key=r[P2] | |
4661 ** | |
4662 ** Register P2 holds an SQL index key made using the | |
4663 ** MakeRecord instructions. This opcode writes that key | |
4664 ** into the index P1. Data for the entry is nil. | |
4665 ** | |
4666 ** P3 is a flag that provides a hint to the b-tree layer that this | |
4667 ** insert is likely to be an append. | |
4668 ** | |
4669 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is | |
4670 ** incremented by this instruction. If the OPFLAG_NCHANGE bit is clear, | |
4671 ** then the change counter is unchanged. | |
4672 ** | |
4673 ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have | |
4674 ** just done a seek to the spot where the new entry is to be inserted. | |
4675 ** This flag avoids doing an extra seek. | |
4676 ** | |
4677 ** This instruction only works for indices. The equivalent instruction | |
4678 ** for tables is OP_Insert. | |
4679 */ | |
4680 case OP_SorterInsert: /* in2 */ | |
4681 case OP_IdxInsert: { /* in2 */ | |
4682 VdbeCursor *pC; | |
4683 BtCursor *pCrsr; | |
4684 int nKey; | |
4685 const char *zKey; | |
4686 | |
4687 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4688 pC = p->apCsr[pOp->p1]; | |
4689 assert( pC!=0 ); | |
4690 assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) ); | |
4691 pIn2 = &aMem[pOp->p2]; | |
4692 assert( pIn2->flags & MEM_Blob ); | |
4693 pCrsr = pC->pCursor; | |
4694 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++; | |
4695 assert( pCrsr!=0 ); | |
4696 assert( pC->isTable==0 ); | |
4697 rc = ExpandBlob(pIn2); | |
4698 if( rc==SQLITE_OK ){ | |
4699 if( isSorter(pC) ){ | |
4700 rc = sqlite3VdbeSorterWrite(pC, pIn2); | |
4701 }else{ | |
4702 nKey = pIn2->n; | |
4703 zKey = pIn2->z; | |
4704 rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, | |
4705 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0) | |
4706 ); | |
4707 assert( pC->deferredMoveto==0 ); | |
4708 pC->cacheStatus = CACHE_STALE; | |
4709 } | |
4710 } | |
4711 break; | |
4712 } | |
4713 | |
4714 /* Opcode: IdxDelete P1 P2 P3 * * | |
4715 ** Synopsis: key=r[P2@P3] | |
4716 ** | |
4717 ** The content of P3 registers starting at register P2 form | |
4718 ** an unpacked index key. This opcode removes that entry from the | |
4719 ** index opened by cursor P1. | |
4720 */ | |
4721 case OP_IdxDelete: { | |
4722 VdbeCursor *pC; | |
4723 BtCursor *pCrsr; | |
4724 int res; | |
4725 UnpackedRecord r; | |
4726 | |
4727 assert( pOp->p3>0 ); | |
4728 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 ); | |
4729 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4730 pC = p->apCsr[pOp->p1]; | |
4731 assert( pC!=0 ); | |
4732 pCrsr = pC->pCursor; | |
4733 assert( pCrsr!=0 ); | |
4734 assert( pOp->p5==0 ); | |
4735 r.pKeyInfo = pC->pKeyInfo; | |
4736 r.nField = (u16)pOp->p3; | |
4737 r.default_rc = 0; | |
4738 r.aMem = &aMem[pOp->p2]; | |
4739 #ifdef SQLITE_DEBUG | |
4740 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } | |
4741 #endif | |
4742 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res); | |
4743 if( rc==SQLITE_OK && res==0 ){ | |
4744 rc = sqlite3BtreeDelete(pCrsr); | |
4745 } | |
4746 assert( pC->deferredMoveto==0 ); | |
4747 pC->cacheStatus = CACHE_STALE; | |
4748 break; | |
4749 } | |
4750 | |
4751 /* Opcode: IdxRowid P1 P2 * * * | |
4752 ** Synopsis: r[P2]=rowid | |
4753 ** | |
4754 ** Write into register P2 an integer which is the last entry in the record at | |
4755 ** the end of the index key pointed to by cursor P1. This integer should be | |
4756 ** the rowid of the table entry to which this index entry points. | |
4757 ** | |
4758 ** See also: Rowid, MakeRecord. | |
4759 */ | |
4760 case OP_IdxRowid: { /* out2-prerelease */ | |
4761 BtCursor *pCrsr; | |
4762 VdbeCursor *pC; | |
4763 i64 rowid; | |
4764 | |
4765 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4766 pC = p->apCsr[pOp->p1]; | |
4767 assert( pC!=0 ); | |
4768 pCrsr = pC->pCursor; | |
4769 assert( pCrsr!=0 ); | |
4770 pOut->flags = MEM_Null; | |
4771 assert( pC->isTable==0 ); | |
4772 assert( pC->deferredMoveto==0 ); | |
4773 | |
4774 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted | |
4775 ** out from under the cursor. That will never happend for an IdxRowid | |
4776 ** opcode, hence the NEVER() arround the check of the return value. | |
4777 */ | |
4778 rc = sqlite3VdbeCursorRestore(pC); | |
4779 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error; | |
4780 | |
4781 if( !pC->nullRow ){ | |
4782 rowid = 0; /* Not needed. Only used to silence a warning. */ | |
4783 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid); | |
4784 if( rc!=SQLITE_OK ){ | |
4785 goto abort_due_to_error; | |
4786 } | |
4787 pOut->u.i = rowid; | |
4788 pOut->flags = MEM_Int; | |
4789 } | |
4790 break; | |
4791 } | |
4792 | |
4793 /* Opcode: IdxGE P1 P2 P3 P4 P5 | |
4794 ** Synopsis: key=r[P3@P4] | |
4795 ** | |
4796 ** The P4 register values beginning with P3 form an unpacked index | |
4797 ** key that omits the PRIMARY KEY. Compare this key value against the index | |
4798 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID | |
4799 ** fields at the end. | |
4800 ** | |
4801 ** If the P1 index entry is greater than or equal to the key value | |
4802 ** then jump to P2. Otherwise fall through to the next instruction. | |
4803 */ | |
4804 /* Opcode: IdxGT P1 P2 P3 P4 P5 | |
4805 ** Synopsis: key=r[P3@P4] | |
4806 ** | |
4807 ** The P4 register values beginning with P3 form an unpacked index | |
4808 ** key that omits the PRIMARY KEY. Compare this key value against the index | |
4809 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID | |
4810 ** fields at the end. | |
4811 ** | |
4812 ** If the P1 index entry is greater than the key value | |
4813 ** then jump to P2. Otherwise fall through to the next instruction. | |
4814 */ | |
4815 /* Opcode: IdxLT P1 P2 P3 P4 P5 | |
4816 ** Synopsis: key=r[P3@P4] | |
4817 ** | |
4818 ** The P4 register values beginning with P3 form an unpacked index | |
4819 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against | |
4820 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or | |
4821 ** ROWID on the P1 index. | |
4822 ** | |
4823 ** If the P1 index entry is less than the key value then jump to P2. | |
4824 ** Otherwise fall through to the next instruction. | |
4825 */ | |
4826 /* Opcode: IdxLE P1 P2 P3 P4 P5 | |
4827 ** Synopsis: key=r[P3@P4] | |
4828 ** | |
4829 ** The P4 register values beginning with P3 form an unpacked index | |
4830 ** key that omits the PRIMARY KEY or ROWID. Compare this key value against | |
4831 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or | |
4832 ** ROWID on the P1 index. | |
4833 ** | |
4834 ** If the P1 index entry is less than or equal to the key value then jump | |
4835 ** to P2. Otherwise fall through to the next instruction. | |
4836 */ | |
4837 case OP_IdxLE: /* jump */ | |
4838 case OP_IdxGT: /* jump */ | |
4839 case OP_IdxLT: /* jump */ | |
4840 case OP_IdxGE: { /* jump */ | |
4841 VdbeCursor *pC; | |
4842 int res; | |
4843 UnpackedRecord r; | |
4844 | |
4845 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4846 pC = p->apCsr[pOp->p1]; | |
4847 assert( pC!=0 ); | |
4848 assert( pC->isOrdered ); | |
4849 assert( pC->pCursor!=0); | |
4850 assert( pC->deferredMoveto==0 ); | |
4851 assert( pOp->p5==0 || pOp->p5==1 ); | |
4852 assert( pOp->p4type==P4_INT32 ); | |
4853 r.pKeyInfo = pC->pKeyInfo; | |
4854 r.nField = (u16)pOp->p4.i; | |
4855 if( pOp->opcode<OP_IdxLT ){ | |
4856 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT ); | |
4857 r.default_rc = -1; | |
4858 }else{ | |
4859 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT ); | |
4860 r.default_rc = 0; | |
4861 } | |
4862 r.aMem = &aMem[pOp->p3]; | |
4863 #ifdef SQLITE_DEBUG | |
4864 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); } | |
4865 #endif | |
4866 res = 0; /* Not needed. Only used to silence a warning. */ | |
4867 rc = sqlite3VdbeIdxKeyCompare(db, pC, &r, &res); | |
4868 assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) ); | |
4869 if( (pOp->opcode&1)==(OP_IdxLT&1) ){ | |
4870 assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT ); | |
4871 res = -res; | |
4872 }else{ | |
4873 assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); | |
4874 res++; | |
4875 } | |
4876 VdbeBranchTaken(res>0,2); | |
4877 if( res>0 ){ | |
4878 pc = pOp->p2 - 1 ; | |
4879 } | |
4880 break; | |
4881 } | |
4882 | |
4883 /* Opcode: Destroy P1 P2 P3 * * | |
4884 ** | |
4885 ** Delete an entire database table or index whose root page in the database | |
4886 ** file is given by P1. | |
4887 ** | |
4888 ** The table being destroyed is in the main database file if P3==0. If | |
4889 ** P3==1 then the table to be clear is in the auxiliary database file | |
4890 ** that is used to store tables create using CREATE TEMPORARY TABLE. | |
4891 ** | |
4892 ** If AUTOVACUUM is enabled then it is possible that another root page | |
4893 ** might be moved into the newly deleted root page in order to keep all | |
4894 ** root pages contiguous at the beginning of the database. The former | |
4895 ** value of the root page that moved - its value before the move occurred - | |
4896 ** is stored in register P2. If no page | |
4897 ** movement was required (because the table being dropped was already | |
4898 ** the last one in the database) then a zero is stored in register P2. | |
4899 ** If AUTOVACUUM is disabled then a zero is stored in register P2. | |
4900 ** | |
4901 ** See also: Clear | |
4902 */ | |
4903 case OP_Destroy: { /* out2-prerelease */ | |
4904 int iMoved; | |
4905 int iCnt; | |
4906 Vdbe *pVdbe; | |
4907 int iDb; | |
4908 | |
4909 assert( p->readOnly==0 ); | |
4910 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
4911 iCnt = 0; | |
4912 for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){ | |
4913 if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader | |
4914 && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 | |
4915 ){ | |
4916 iCnt++; | |
4917 } | |
4918 } | |
4919 #else | |
4920 iCnt = db->nVdbeRead; | |
4921 #endif | |
4922 pOut->flags = MEM_Null; | |
4923 if( iCnt>1 ){ | |
4924 rc = SQLITE_LOCKED; | |
4925 p->errorAction = OE_Abort; | |
4926 }else{ | |
4927 iDb = pOp->p3; | |
4928 assert( iCnt==1 ); | |
4929 assert( DbMaskTest(p->btreeMask, iDb) ); | |
4930 iMoved = 0; /* Not needed. Only to silence a warning. */ | |
4931 rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved); | |
4932 pOut->flags = MEM_Int; | |
4933 pOut->u.i = iMoved; | |
4934 #ifndef SQLITE_OMIT_AUTOVACUUM | |
4935 if( rc==SQLITE_OK && iMoved!=0 ){ | |
4936 sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1); | |
4937 /* All OP_Destroy operations occur on the same btree */ | |
4938 assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 ); | |
4939 resetSchemaOnFault = iDb+1; | |
4940 } | |
4941 #endif | |
4942 } | |
4943 break; | |
4944 } | |
4945 | |
4946 /* Opcode: Clear P1 P2 P3 | |
4947 ** | |
4948 ** Delete all contents of the database table or index whose root page | |
4949 ** in the database file is given by P1. But, unlike Destroy, do not | |
4950 ** remove the table or index from the database file. | |
4951 ** | |
4952 ** The table being clear is in the main database file if P2==0. If | |
4953 ** P2==1 then the table to be clear is in the auxiliary database file | |
4954 ** that is used to store tables create using CREATE TEMPORARY TABLE. | |
4955 ** | |
4956 ** If the P3 value is non-zero, then the table referred to must be an | |
4957 ** intkey table (an SQL table, not an index). In this case the row change | |
4958 ** count is incremented by the number of rows in the table being cleared. | |
4959 ** If P3 is greater than zero, then the value stored in register P3 is | |
4960 ** also incremented by the number of rows in the table being cleared. | |
4961 ** | |
4962 ** See also: Destroy | |
4963 */ | |
4964 case OP_Clear: { | |
4965 int nChange; | |
4966 | |
4967 nChange = 0; | |
4968 assert( p->readOnly==0 ); | |
4969 assert( DbMaskTest(p->btreeMask, pOp->p2) ); | |
4970 rc = sqlite3BtreeClearTable( | |
4971 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0) | |
4972 ); | |
4973 if( pOp->p3 ){ | |
4974 p->nChange += nChange; | |
4975 if( pOp->p3>0 ){ | |
4976 assert( memIsValid(&aMem[pOp->p3]) ); | |
4977 memAboutToChange(p, &aMem[pOp->p3]); | |
4978 aMem[pOp->p3].u.i += nChange; | |
4979 } | |
4980 } | |
4981 break; | |
4982 } | |
4983 | |
4984 /* Opcode: ResetSorter P1 * * * * | |
4985 ** | |
4986 ** Delete all contents from the ephemeral table or sorter | |
4987 ** that is open on cursor P1. | |
4988 ** | |
4989 ** This opcode only works for cursors used for sorting and | |
4990 ** opened with OP_OpenEphemeral or OP_SorterOpen. | |
4991 */ | |
4992 case OP_ResetSorter: { | |
4993 VdbeCursor *pC; | |
4994 | |
4995 assert( pOp->p1>=0 && pOp->p1<p->nCursor ); | |
4996 pC = p->apCsr[pOp->p1]; | |
4997 assert( pC!=0 ); | |
4998 if( pC->pSorter ){ | |
4999 sqlite3VdbeSorterReset(db, pC->pSorter); | |
5000 }else{ | |
5001 assert( pC->isEphemeral ); | |
5002 rc = sqlite3BtreeClearTableOfCursor(pC->pCursor); | |
5003 } | |
5004 break; | |
5005 } | |
5006 | |
5007 /* Opcode: CreateTable P1 P2 * * * | |
5008 ** Synopsis: r[P2]=root iDb=P1 | |
5009 ** | |
5010 ** Allocate a new table in the main database file if P1==0 or in the | |
5011 ** auxiliary database file if P1==1 or in an attached database if | |
5012 ** P1>1. Write the root page number of the new table into | |
5013 ** register P2 | |
5014 ** | |
5015 ** The difference between a table and an index is this: A table must | |
5016 ** have a 4-byte integer key and can have arbitrary data. An index | |
5017 ** has an arbitrary key but no data. | |
5018 ** | |
5019 ** See also: CreateIndex | |
5020 */ | |
5021 /* Opcode: CreateIndex P1 P2 * * * | |
5022 ** Synopsis: r[P2]=root iDb=P1 | |
5023 ** | |
5024 ** Allocate a new index in the main database file if P1==0 or in the | |
5025 ** auxiliary database file if P1==1 or in an attached database if | |
5026 ** P1>1. Write the root page number of the new table into | |
5027 ** register P2. | |
5028 ** | |
5029 ** See documentation on OP_CreateTable for additional information. | |
5030 */ | |
5031 case OP_CreateIndex: /* out2-prerelease */ | |
5032 case OP_CreateTable: { /* out2-prerelease */ | |
5033 int pgno; | |
5034 int flags; | |
5035 Db *pDb; | |
5036 | |
5037 pgno = 0; | |
5038 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
5039 assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
5040 assert( p->readOnly==0 ); | |
5041 pDb = &db->aDb[pOp->p1]; | |
5042 assert( pDb->pBt!=0 ); | |
5043 if( pOp->opcode==OP_CreateTable ){ | |
5044 /* flags = BTREE_INTKEY; */ | |
5045 flags = BTREE_INTKEY; | |
5046 }else{ | |
5047 flags = BTREE_BLOBKEY; | |
5048 } | |
5049 rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags); | |
5050 pOut->u.i = pgno; | |
5051 break; | |
5052 } | |
5053 | |
5054 /* Opcode: ParseSchema P1 * * P4 * | |
5055 ** | |
5056 ** Read and parse all entries from the SQLITE_MASTER table of database P1 | |
5057 ** that match the WHERE clause P4. | |
5058 ** | |
5059 ** This opcode invokes the parser to create a new virtual machine, | |
5060 ** then runs the new virtual machine. It is thus a re-entrant opcode. | |
5061 */ | |
5062 case OP_ParseSchema: { | |
5063 int iDb; | |
5064 const char *zMaster; | |
5065 char *zSql; | |
5066 InitData initData; | |
5067 | |
5068 /* Any prepared statement that invokes this opcode will hold mutexes | |
5069 ** on every btree. This is a prerequisite for invoking | |
5070 ** sqlite3InitCallback(). | |
5071 */ | |
5072 #ifdef SQLITE_DEBUG | |
5073 for(iDb=0; iDb<db->nDb; iDb++){ | |
5074 assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); | |
5075 } | |
5076 #endif | |
5077 | |
5078 iDb = pOp->p1; | |
5079 assert( iDb>=0 && iDb<db->nDb ); | |
5080 assert( DbHasProperty(db, iDb, DB_SchemaLoaded) ); | |
5081 /* Used to be a conditional */ { | |
5082 zMaster = SCHEMA_TABLE(iDb); | |
5083 initData.db = db; | |
5084 initData.iDb = pOp->p1; | |
5085 initData.pzErrMsg = &p->zErrMsg; | |
5086 zSql = sqlite3MPrintf(db, | |
5087 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid", | |
5088 db->aDb[iDb].zName, zMaster, pOp->p4.z); | |
5089 if( zSql==0 ){ | |
5090 rc = SQLITE_NOMEM; | |
5091 }else{ | |
5092 assert( db->init.busy==0 ); | |
5093 db->init.busy = 1; | |
5094 initData.rc = SQLITE_OK; | |
5095 assert( !db->mallocFailed ); | |
5096 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); | |
5097 if( rc==SQLITE_OK ) rc = initData.rc; | |
5098 sqlite3DbFree(db, zSql); | |
5099 db->init.busy = 0; | |
5100 } | |
5101 } | |
5102 if( rc ) sqlite3ResetAllSchemasOfConnection(db); | |
5103 if( rc==SQLITE_NOMEM ){ | |
5104 goto no_mem; | |
5105 } | |
5106 break; | |
5107 } | |
5108 | |
5109 #if !defined(SQLITE_OMIT_ANALYZE) | |
5110 /* Opcode: LoadAnalysis P1 * * * * | |
5111 ** | |
5112 ** Read the sqlite_stat1 table for database P1 and load the content | |
5113 ** of that table into the internal index hash table. This will cause | |
5114 ** the analysis to be used when preparing all subsequent queries. | |
5115 */ | |
5116 case OP_LoadAnalysis: { | |
5117 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
5118 rc = sqlite3AnalysisLoad(db, pOp->p1); | |
5119 break; | |
5120 } | |
5121 #endif /* !defined(SQLITE_OMIT_ANALYZE) */ | |
5122 | |
5123 /* Opcode: DropTable P1 * * P4 * | |
5124 ** | |
5125 ** Remove the internal (in-memory) data structures that describe | |
5126 ** the table named P4 in database P1. This is called after a table | |
5127 ** is dropped from disk (using the Destroy opcode) in order to keep | |
5128 ** the internal representation of the | |
5129 ** schema consistent with what is on disk. | |
5130 */ | |
5131 case OP_DropTable: { | |
5132 sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z); | |
5133 break; | |
5134 } | |
5135 | |
5136 /* Opcode: DropIndex P1 * * P4 * | |
5137 ** | |
5138 ** Remove the internal (in-memory) data structures that describe | |
5139 ** the index named P4 in database P1. This is called after an index | |
5140 ** is dropped from disk (using the Destroy opcode) | |
5141 ** in order to keep the internal representation of the | |
5142 ** schema consistent with what is on disk. | |
5143 */ | |
5144 case OP_DropIndex: { | |
5145 sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z); | |
5146 break; | |
5147 } | |
5148 | |
5149 /* Opcode: DropTrigger P1 * * P4 * | |
5150 ** | |
5151 ** Remove the internal (in-memory) data structures that describe | |
5152 ** the trigger named P4 in database P1. This is called after a trigger | |
5153 ** is dropped from disk (using the Destroy opcode) in order to keep | |
5154 ** the internal representation of the | |
5155 ** schema consistent with what is on disk. | |
5156 */ | |
5157 case OP_DropTrigger: { | |
5158 sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z); | |
5159 break; | |
5160 } | |
5161 | |
5162 | |
5163 #ifndef SQLITE_OMIT_INTEGRITY_CHECK | |
5164 /* Opcode: IntegrityCk P1 P2 P3 * P5 | |
5165 ** | |
5166 ** Do an analysis of the currently open database. Store in | |
5167 ** register P1 the text of an error message describing any problems. | |
5168 ** If no problems are found, store a NULL in register P1. | |
5169 ** | |
5170 ** The register P3 contains the maximum number of allowed errors. | |
5171 ** At most reg(P3) errors will be reported. | |
5172 ** In other words, the analysis stops as soon as reg(P1) errors are | |
5173 ** seen. Reg(P1) is updated with the number of errors remaining. | |
5174 ** | |
5175 ** The root page numbers of all tables in the database are integer | |
5176 ** stored in reg(P1), reg(P1+1), reg(P1+2), .... There are P2 tables | |
5177 ** total. | |
5178 ** | |
5179 ** If P5 is not zero, the check is done on the auxiliary database | |
5180 ** file, not the main database file. | |
5181 ** | |
5182 ** This opcode is used to implement the integrity_check pragma. | |
5183 */ | |
5184 case OP_IntegrityCk: { | |
5185 int nRoot; /* Number of tables to check. (Number of root pages.) */ | |
5186 int *aRoot; /* Array of rootpage numbers for tables to be checked */ | |
5187 int j; /* Loop counter */ | |
5188 int nErr; /* Number of errors reported */ | |
5189 char *z; /* Text of the error report */ | |
5190 Mem *pnErr; /* Register keeping track of errors remaining */ | |
5191 | |
5192 assert( p->bIsReader ); | |
5193 nRoot = pOp->p2; | |
5194 assert( nRoot>0 ); | |
5195 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) ); | |
5196 if( aRoot==0 ) goto no_mem; | |
5197 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
5198 pnErr = &aMem[pOp->p3]; | |
5199 assert( (pnErr->flags & MEM_Int)!=0 ); | |
5200 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 ); | |
5201 pIn1 = &aMem[pOp->p1]; | |
5202 for(j=0; j<nRoot; j++){ | |
5203 aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]); | |
5204 } | |
5205 aRoot[j] = 0; | |
5206 assert( pOp->p5<db->nDb ); | |
5207 assert( DbMaskTest(p->btreeMask, pOp->p5) ); | |
5208 z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot, | |
5209 (int)pnErr->u.i, &nErr); | |
5210 sqlite3DbFree(db, aRoot); | |
5211 pnErr->u.i -= nErr; | |
5212 sqlite3VdbeMemSetNull(pIn1); | |
5213 if( nErr==0 ){ | |
5214 assert( z==0 ); | |
5215 }else if( z==0 ){ | |
5216 goto no_mem; | |
5217 }else{ | |
5218 sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free); | |
5219 } | |
5220 UPDATE_MAX_BLOBSIZE(pIn1); | |
5221 sqlite3VdbeChangeEncoding(pIn1, encoding); | |
5222 break; | |
5223 } | |
5224 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ | |
5225 | |
5226 /* Opcode: RowSetAdd P1 P2 * * * | |
5227 ** Synopsis: rowset(P1)=r[P2] | |
5228 ** | |
5229 ** Insert the integer value held by register P2 into a boolean index | |
5230 ** held in register P1. | |
5231 ** | |
5232 ** An assertion fails if P2 is not an integer. | |
5233 */ | |
5234 case OP_RowSetAdd: { /* in1, in2 */ | |
5235 pIn1 = &aMem[pOp->p1]; | |
5236 pIn2 = &aMem[pOp->p2]; | |
5237 assert( (pIn2->flags & MEM_Int)!=0 ); | |
5238 if( (pIn1->flags & MEM_RowSet)==0 ){ | |
5239 sqlite3VdbeMemSetRowSet(pIn1); | |
5240 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; | |
5241 } | |
5242 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i); | |
5243 break; | |
5244 } | |
5245 | |
5246 /* Opcode: RowSetRead P1 P2 P3 * * | |
5247 ** Synopsis: r[P3]=rowset(P1) | |
5248 ** | |
5249 ** Extract the smallest value from boolean index P1 and put that value into | |
5250 ** register P3. Or, if boolean index P1 is initially empty, leave P3 | |
5251 ** unchanged and jump to instruction P2. | |
5252 */ | |
5253 case OP_RowSetRead: { /* jump, in1, out3 */ | |
5254 i64 val; | |
5255 | |
5256 pIn1 = &aMem[pOp->p1]; | |
5257 if( (pIn1->flags & MEM_RowSet)==0 | |
5258 || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 | |
5259 ){ | |
5260 /* The boolean index is empty */ | |
5261 sqlite3VdbeMemSetNull(pIn1); | |
5262 pc = pOp->p2 - 1; | |
5263 VdbeBranchTaken(1,2); | |
5264 }else{ | |
5265 /* A value was pulled from the index */ | |
5266 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); | |
5267 VdbeBranchTaken(0,2); | |
5268 } | |
5269 goto check_for_interrupt; | |
5270 } | |
5271 | |
5272 /* Opcode: RowSetTest P1 P2 P3 P4 | |
5273 ** Synopsis: if r[P3] in rowset(P1) goto P2 | |
5274 ** | |
5275 ** Register P3 is assumed to hold a 64-bit integer value. If register P1 | |
5276 ** contains a RowSet object and that RowSet object contains | |
5277 ** the value held in P3, jump to register P2. Otherwise, insert the | |
5278 ** integer in P3 into the RowSet and continue on to the | |
5279 ** next opcode. | |
5280 ** | |
5281 ** The RowSet object is optimized for the case where successive sets | |
5282 ** of integers, where each set contains no duplicates. Each set | |
5283 ** of values is identified by a unique P4 value. The first set | |
5284 ** must have P4==0, the final set P4=-1. P4 must be either -1 or | |
5285 ** non-negative. For non-negative values of P4 only the lower 4 | |
5286 ** bits are significant. | |
5287 ** | |
5288 ** This allows optimizations: (a) when P4==0 there is no need to test | |
5289 ** the rowset object for P3, as it is guaranteed not to contain it, | |
5290 ** (b) when P4==-1 there is no need to insert the value, as it will | |
5291 ** never be tested for, and (c) when a value that is part of set X is | |
5292 ** inserted, there is no need to search to see if the same value was | |
5293 ** previously inserted as part of set X (only if it was previously | |
5294 ** inserted as part of some other set). | |
5295 */ | |
5296 case OP_RowSetTest: { /* jump, in1, in3 */ | |
5297 int iSet; | |
5298 int exists; | |
5299 | |
5300 pIn1 = &aMem[pOp->p1]; | |
5301 pIn3 = &aMem[pOp->p3]; | |
5302 iSet = pOp->p4.i; | |
5303 assert( pIn3->flags&MEM_Int ); | |
5304 | |
5305 /* If there is anything other than a rowset object in memory cell P1, | |
5306 ** delete it now and initialize P1 with an empty rowset | |
5307 */ | |
5308 if( (pIn1->flags & MEM_RowSet)==0 ){ | |
5309 sqlite3VdbeMemSetRowSet(pIn1); | |
5310 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem; | |
5311 } | |
5312 | |
5313 assert( pOp->p4type==P4_INT32 ); | |
5314 assert( iSet==-1 || iSet>=0 ); | |
5315 if( iSet ){ | |
5316 exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); | |
5317 VdbeBranchTaken(exists!=0,2); | |
5318 if( exists ){ | |
5319 pc = pOp->p2 - 1; | |
5320 break; | |
5321 } | |
5322 } | |
5323 if( iSet>=0 ){ | |
5324 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); | |
5325 } | |
5326 break; | |
5327 } | |
5328 | |
5329 | |
5330 #ifndef SQLITE_OMIT_TRIGGER | |
5331 | |
5332 /* Opcode: Program P1 P2 P3 P4 P5 | |
5333 ** | |
5334 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM). | |
5335 ** | |
5336 ** P1 contains the address of the memory cell that contains the first memory | |
5337 ** cell in an array of values used as arguments to the sub-program. P2 | |
5338 ** contains the address to jump to if the sub-program throws an IGNORE | |
5339 ** exception using the RAISE() function. Register P3 contains the address | |
5340 ** of a memory cell in this (the parent) VM that is used to allocate the | |
5341 ** memory required by the sub-vdbe at runtime. | |
5342 ** | |
5343 ** P4 is a pointer to the VM containing the trigger program. | |
5344 ** | |
5345 ** If P5 is non-zero, then recursive program invocation is enabled. | |
5346 */ | |
5347 case OP_Program: { /* jump */ | |
5348 int nMem; /* Number of memory registers for sub-program */ | |
5349 int nByte; /* Bytes of runtime space required for sub-program */ | |
5350 Mem *pRt; /* Register to allocate runtime space */ | |
5351 Mem *pMem; /* Used to iterate through memory cells */ | |
5352 Mem *pEnd; /* Last memory cell in new array */ | |
5353 VdbeFrame *pFrame; /* New vdbe frame to execute in */ | |
5354 SubProgram *pProgram; /* Sub-program to execute */ | |
5355 void *t; /* Token identifying trigger */ | |
5356 | |
5357 pProgram = pOp->p4.pProgram; | |
5358 pRt = &aMem[pOp->p3]; | |
5359 assert( pProgram->nOp>0 ); | |
5360 | |
5361 /* If the p5 flag is clear, then recursive invocation of triggers is | |
5362 ** disabled for backwards compatibility (p5 is set if this sub-program | |
5363 ** is really a trigger, not a foreign key action, and the flag set | |
5364 ** and cleared by the "PRAGMA recursive_triggers" command is clear). | |
5365 ** | |
5366 ** It is recursive invocation of triggers, at the SQL level, that is | |
5367 ** disabled. In some cases a single trigger may generate more than one | |
5368 ** SubProgram (if the trigger may be executed with more than one different | |
5369 ** ON CONFLICT algorithm). SubProgram structures associated with a | |
5370 ** single trigger all have the same value for the SubProgram.token | |
5371 ** variable. */ | |
5372 if( pOp->p5 ){ | |
5373 t = pProgram->token; | |
5374 for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent); | |
5375 if( pFrame ) break; | |
5376 } | |
5377 | |
5378 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){ | |
5379 rc = SQLITE_ERROR; | |
5380 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion"); | |
5381 break; | |
5382 } | |
5383 | |
5384 /* Register pRt is used to store the memory required to save the state | |
5385 ** of the current program, and the memory required at runtime to execute | |
5386 ** the trigger program. If this trigger has been fired before, then pRt | |
5387 ** is already allocated. Otherwise, it must be initialized. */ | |
5388 if( (pRt->flags&MEM_Frame)==0 ){ | |
5389 /* SubProgram.nMem is set to the number of memory cells used by the | |
5390 ** program stored in SubProgram.aOp. As well as these, one memory | |
5391 ** cell is required for each cursor used by the program. Set local | |
5392 ** variable nMem (and later, VdbeFrame.nChildMem) to this value. | |
5393 */ | |
5394 nMem = pProgram->nMem + pProgram->nCsr; | |
5395 nByte = ROUND8(sizeof(VdbeFrame)) | |
5396 + nMem * sizeof(Mem) | |
5397 + pProgram->nCsr * sizeof(VdbeCursor *) | |
5398 + pProgram->nOnce * sizeof(u8); | |
5399 pFrame = sqlite3DbMallocZero(db, nByte); | |
5400 if( !pFrame ){ | |
5401 goto no_mem; | |
5402 } | |
5403 sqlite3VdbeMemRelease(pRt); | |
5404 pRt->flags = MEM_Frame; | |
5405 pRt->u.pFrame = pFrame; | |
5406 | |
5407 pFrame->v = p; | |
5408 pFrame->nChildMem = nMem; | |
5409 pFrame->nChildCsr = pProgram->nCsr; | |
5410 pFrame->pc = pc; | |
5411 pFrame->aMem = p->aMem; | |
5412 pFrame->nMem = p->nMem; | |
5413 pFrame->apCsr = p->apCsr; | |
5414 pFrame->nCursor = p->nCursor; | |
5415 pFrame->aOp = p->aOp; | |
5416 pFrame->nOp = p->nOp; | |
5417 pFrame->token = pProgram->token; | |
5418 pFrame->aOnceFlag = p->aOnceFlag; | |
5419 pFrame->nOnceFlag = p->nOnceFlag; | |
5420 | |
5421 pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem]; | |
5422 for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){ | |
5423 pMem->flags = MEM_Undefined; | |
5424 pMem->db = db; | |
5425 } | |
5426 }else{ | |
5427 pFrame = pRt->u.pFrame; | |
5428 assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); | |
5429 assert( pProgram->nCsr==pFrame->nChildCsr ); | |
5430 assert( pc==pFrame->pc ); | |
5431 } | |
5432 | |
5433 p->nFrame++; | |
5434 pFrame->pParent = p->pFrame; | |
5435 pFrame->lastRowid = lastRowid; | |
5436 pFrame->nChange = p->nChange; | |
5437 p->nChange = 0; | |
5438 p->pFrame = pFrame; | |
5439 p->aMem = aMem = &VdbeFrameMem(pFrame)[-1]; | |
5440 p->nMem = pFrame->nChildMem; | |
5441 p->nCursor = (u16)pFrame->nChildCsr; | |
5442 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1]; | |
5443 p->aOp = aOp = pProgram->aOp; | |
5444 p->nOp = pProgram->nOp; | |
5445 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; | |
5446 p->nOnceFlag = pProgram->nOnce; | |
5447 pc = -1; | |
5448 memset(p->aOnceFlag, 0, p->nOnceFlag); | |
5449 | |
5450 break; | |
5451 } | |
5452 | |
5453 /* Opcode: Param P1 P2 * * * | |
5454 ** | |
5455 ** This opcode is only ever present in sub-programs called via the | |
5456 ** OP_Program instruction. Copy a value currently stored in a memory | |
5457 ** cell of the calling (parent) frame to cell P2 in the current frames | |
5458 ** address space. This is used by trigger programs to access the new.* | |
5459 ** and old.* values. | |
5460 ** | |
5461 ** The address of the cell in the parent frame is determined by adding | |
5462 ** the value of the P1 argument to the value of the P1 argument to the | |
5463 ** calling OP_Program instruction. | |
5464 */ | |
5465 case OP_Param: { /* out2-prerelease */ | |
5466 VdbeFrame *pFrame; | |
5467 Mem *pIn; | |
5468 pFrame = p->pFrame; | |
5469 pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; | |
5470 sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); | |
5471 break; | |
5472 } | |
5473 | |
5474 #endif /* #ifndef SQLITE_OMIT_TRIGGER */ | |
5475 | |
5476 #ifndef SQLITE_OMIT_FOREIGN_KEY | |
5477 /* Opcode: FkCounter P1 P2 * * * | |
5478 ** Synopsis: fkctr[P1]+=P2 | |
5479 ** | |
5480 ** Increment a "constraint counter" by P2 (P2 may be negative or positive). | |
5481 ** If P1 is non-zero, the database constraint counter is incremented | |
5482 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the | |
5483 ** statement counter is incremented (immediate foreign key constraints). | |
5484 */ | |
5485 case OP_FkCounter: { | |
5486 if( db->flags & SQLITE_DeferFKs ){ | |
5487 db->nDeferredImmCons += pOp->p2; | |
5488 }else if( pOp->p1 ){ | |
5489 db->nDeferredCons += pOp->p2; | |
5490 }else{ | |
5491 p->nFkConstraint += pOp->p2; | |
5492 } | |
5493 break; | |
5494 } | |
5495 | |
5496 /* Opcode: FkIfZero P1 P2 * * * | |
5497 ** Synopsis: if fkctr[P1]==0 goto P2 | |
5498 ** | |
5499 ** This opcode tests if a foreign key constraint-counter is currently zero. | |
5500 ** If so, jump to instruction P2. Otherwise, fall through to the next | |
5501 ** instruction. | |
5502 ** | |
5503 ** If P1 is non-zero, then the jump is taken if the database constraint-counter | |
5504 ** is zero (the one that counts deferred constraint violations). If P1 is | |
5505 ** zero, the jump is taken if the statement constraint-counter is zero | |
5506 ** (immediate foreign key constraint violations). | |
5507 */ | |
5508 case OP_FkIfZero: { /* jump */ | |
5509 if( pOp->p1 ){ | |
5510 VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); | |
5511 if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; | |
5512 }else{ | |
5513 VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); | |
5514 if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; | |
5515 } | |
5516 break; | |
5517 } | |
5518 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ | |
5519 | |
5520 #ifndef SQLITE_OMIT_AUTOINCREMENT | |
5521 /* Opcode: MemMax P1 P2 * * * | |
5522 ** Synopsis: r[P1]=max(r[P1],r[P2]) | |
5523 ** | |
5524 ** P1 is a register in the root frame of this VM (the root frame is | |
5525 ** different from the current frame if this instruction is being executed | |
5526 ** within a sub-program). Set the value of register P1 to the maximum of | |
5527 ** its current value and the value in register P2. | |
5528 ** | |
5529 ** This instruction throws an error if the memory cell is not initially | |
5530 ** an integer. | |
5531 */ | |
5532 case OP_MemMax: { /* in2 */ | |
5533 VdbeFrame *pFrame; | |
5534 if( p->pFrame ){ | |
5535 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | |
5536 pIn1 = &pFrame->aMem[pOp->p1]; | |
5537 }else{ | |
5538 pIn1 = &aMem[pOp->p1]; | |
5539 } | |
5540 assert( memIsValid(pIn1) ); | |
5541 sqlite3VdbeMemIntegerify(pIn1); | |
5542 pIn2 = &aMem[pOp->p2]; | |
5543 sqlite3VdbeMemIntegerify(pIn2); | |
5544 if( pIn1->u.i<pIn2->u.i){ | |
5545 pIn1->u.i = pIn2->u.i; | |
5546 } | |
5547 break; | |
5548 } | |
5549 #endif /* SQLITE_OMIT_AUTOINCREMENT */ | |
5550 | |
5551 /* Opcode: IfPos P1 P2 * * * | |
5552 ** Synopsis: if r[P1]>0 goto P2 | |
5553 ** | |
5554 ** If the value of register P1 is 1 or greater, jump to P2. | |
5555 ** | |
5556 ** It is illegal to use this instruction on a register that does | |
5557 ** not contain an integer. An assertion fault will result if you try. | |
5558 */ | |
5559 case OP_IfPos: { /* jump, in1 */ | |
5560 pIn1 = &aMem[pOp->p1]; | |
5561 assert( pIn1->flags&MEM_Int ); | |
5562 VdbeBranchTaken( pIn1->u.i>0, 2); | |
5563 if( pIn1->u.i>0 ){ | |
5564 pc = pOp->p2 - 1; | |
5565 } | |
5566 break; | |
5567 } | |
5568 | |
5569 /* Opcode: IfNeg P1 P2 P3 * * | |
5570 ** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2 | |
5571 ** | |
5572 ** Register P1 must contain an integer. Add literal P3 to the value in | |
5573 ** register P1 then if the value of register P1 is less than zero, jump to P2. | |
5574 */ | |
5575 case OP_IfNeg: { /* jump, in1 */ | |
5576 pIn1 = &aMem[pOp->p1]; | |
5577 assert( pIn1->flags&MEM_Int ); | |
5578 pIn1->u.i += pOp->p3; | |
5579 VdbeBranchTaken(pIn1->u.i<0, 2); | |
5580 if( pIn1->u.i<0 ){ | |
5581 pc = pOp->p2 - 1; | |
5582 } | |
5583 break; | |
5584 } | |
5585 | |
5586 /* Opcode: IfZero P1 P2 P3 * * | |
5587 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2 | |
5588 ** | |
5589 ** The register P1 must contain an integer. Add literal P3 to the | |
5590 ** value in register P1. If the result is exactly 0, jump to P2. | |
5591 */ | |
5592 case OP_IfZero: { /* jump, in1 */ | |
5593 pIn1 = &aMem[pOp->p1]; | |
5594 assert( pIn1->flags&MEM_Int ); | |
5595 pIn1->u.i += pOp->p3; | |
5596 VdbeBranchTaken(pIn1->u.i==0, 2); | |
5597 if( pIn1->u.i==0 ){ | |
5598 pc = pOp->p2 - 1; | |
5599 } | |
5600 break; | |
5601 } | |
5602 | |
5603 /* Opcode: AggStep * P2 P3 P4 P5 | |
5604 ** Synopsis: accum=r[P3] step(r[P2@P5]) | |
5605 ** | |
5606 ** Execute the step function for an aggregate. The | |
5607 ** function has P5 arguments. P4 is a pointer to the FuncDef | |
5608 ** structure that specifies the function. Use register | |
5609 ** P3 as the accumulator. | |
5610 ** | |
5611 ** The P5 arguments are taken from register P2 and its | |
5612 ** successors. | |
5613 */ | |
5614 case OP_AggStep: { | |
5615 int n; | |
5616 int i; | |
5617 Mem *pMem; | |
5618 Mem *pRec; | |
5619 Mem t; | |
5620 sqlite3_context ctx; | |
5621 sqlite3_value **apVal; | |
5622 | |
5623 n = pOp->p5; | |
5624 assert( n>=0 ); | |
5625 pRec = &aMem[pOp->p2]; | |
5626 apVal = p->apArg; | |
5627 assert( apVal || n==0 ); | |
5628 for(i=0; i<n; i++, pRec++){ | |
5629 assert( memIsValid(pRec) ); | |
5630 apVal[i] = pRec; | |
5631 memAboutToChange(p, pRec); | |
5632 } | |
5633 ctx.pFunc = pOp->p4.pFunc; | |
5634 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
5635 ctx.pMem = pMem = &aMem[pOp->p3]; | |
5636 pMem->n++; | |
5637 sqlite3VdbeMemInit(&t, db, MEM_Null); | |
5638 ctx.pOut = &t; | |
5639 ctx.isError = 0; | |
5640 ctx.pVdbe = p; | |
5641 ctx.iOp = pc; | |
5642 ctx.skipFlag = 0; | |
5643 (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ | |
5644 if( ctx.isError ){ | |
5645 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t)); | |
5646 rc = ctx.isError; | |
5647 } | |
5648 if( ctx.skipFlag ){ | |
5649 assert( pOp[-1].opcode==OP_CollSeq ); | |
5650 i = pOp[-1].p1; | |
5651 if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1); | |
5652 } | |
5653 sqlite3VdbeMemRelease(&t); | |
5654 break; | |
5655 } | |
5656 | |
5657 /* Opcode: AggFinal P1 P2 * P4 * | |
5658 ** Synopsis: accum=r[P1] N=P2 | |
5659 ** | |
5660 ** Execute the finalizer function for an aggregate. P1 is | |
5661 ** the memory location that is the accumulator for the aggregate. | |
5662 ** | |
5663 ** P2 is the number of arguments that the step function takes and | |
5664 ** P4 is a pointer to the FuncDef for this function. The P2 | |
5665 ** argument is not used by this opcode. It is only there to disambiguate | |
5666 ** functions that can take varying numbers of arguments. The | |
5667 ** P4 argument is only needed for the degenerate case where | |
5668 ** the step function was not previously called. | |
5669 */ | |
5670 case OP_AggFinal: { | |
5671 Mem *pMem; | |
5672 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); | |
5673 pMem = &aMem[pOp->p1]; | |
5674 assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 ); | |
5675 rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc); | |
5676 if( rc ){ | |
5677 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem)); | |
5678 } | |
5679 sqlite3VdbeChangeEncoding(pMem, encoding); | |
5680 UPDATE_MAX_BLOBSIZE(pMem); | |
5681 if( sqlite3VdbeMemTooBig(pMem) ){ | |
5682 goto too_big; | |
5683 } | |
5684 break; | |
5685 } | |
5686 | |
5687 #ifndef SQLITE_OMIT_WAL | |
5688 /* Opcode: Checkpoint P1 P2 P3 * * | |
5689 ** | |
5690 ** Checkpoint database P1. This is a no-op if P1 is not currently in | |
5691 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL | |
5692 ** or RESTART. Write 1 or 0 into mem[P3] if the checkpoint returns | |
5693 ** SQLITE_BUSY or not, respectively. Write the number of pages in the | |
5694 ** WAL after the checkpoint into mem[P3+1] and the number of pages | |
5695 ** in the WAL that have been checkpointed after the checkpoint | |
5696 ** completes into mem[P3+2]. However on an error, mem[P3+1] and | |
5697 ** mem[P3+2] are initialized to -1. | |
5698 */ | |
5699 case OP_Checkpoint: { | |
5700 int i; /* Loop counter */ | |
5701 int aRes[3]; /* Results */ | |
5702 Mem *pMem; /* Write results here */ | |
5703 | |
5704 assert( p->readOnly==0 ); | |
5705 aRes[0] = 0; | |
5706 aRes[1] = aRes[2] = -1; | |
5707 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE | |
5708 || pOp->p2==SQLITE_CHECKPOINT_FULL | |
5709 || pOp->p2==SQLITE_CHECKPOINT_RESTART | |
5710 ); | |
5711 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]); | |
5712 if( rc==SQLITE_BUSY ){ | |
5713 rc = SQLITE_OK; | |
5714 aRes[0] = 1; | |
5715 } | |
5716 for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){ | |
5717 sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]); | |
5718 } | |
5719 break; | |
5720 }; | |
5721 #endif | |
5722 | |
5723 #ifndef SQLITE_OMIT_PRAGMA | |
5724 /* Opcode: JournalMode P1 P2 P3 * * | |
5725 ** | |
5726 ** Change the journal mode of database P1 to P3. P3 must be one of the | |
5727 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback | |
5728 ** modes (delete, truncate, persist, off and memory), this is a simple | |
5729 ** operation. No IO is required. | |
5730 ** | |
5731 ** If changing into or out of WAL mode the procedure is more complicated. | |
5732 ** | |
5733 ** Write a string containing the final journal-mode to register P2. | |
5734 */ | |
5735 case OP_JournalMode: { /* out2-prerelease */ | |
5736 Btree *pBt; /* Btree to change journal mode of */ | |
5737 Pager *pPager; /* Pager associated with pBt */ | |
5738 int eNew; /* New journal mode */ | |
5739 int eOld; /* The old journal mode */ | |
5740 #ifndef SQLITE_OMIT_WAL | |
5741 const char *zFilename; /* Name of database file for pPager */ | |
5742 #endif | |
5743 | |
5744 eNew = pOp->p3; | |
5745 assert( eNew==PAGER_JOURNALMODE_DELETE | |
5746 || eNew==PAGER_JOURNALMODE_TRUNCATE | |
5747 || eNew==PAGER_JOURNALMODE_PERSIST | |
5748 || eNew==PAGER_JOURNALMODE_OFF | |
5749 || eNew==PAGER_JOURNALMODE_MEMORY | |
5750 || eNew==PAGER_JOURNALMODE_WAL | |
5751 || eNew==PAGER_JOURNALMODE_QUERY | |
5752 ); | |
5753 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
5754 assert( p->readOnly==0 ); | |
5755 | |
5756 pBt = db->aDb[pOp->p1].pBt; | |
5757 pPager = sqlite3BtreePager(pBt); | |
5758 eOld = sqlite3PagerGetJournalMode(pPager); | |
5759 if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld; | |
5760 if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld; | |
5761 | |
5762 #ifndef SQLITE_OMIT_WAL | |
5763 zFilename = sqlite3PagerFilename(pPager, 1); | |
5764 | |
5765 /* Do not allow a transition to journal_mode=WAL for a database | |
5766 ** in temporary storage or if the VFS does not support shared memory | |
5767 */ | |
5768 if( eNew==PAGER_JOURNALMODE_WAL | |
5769 && (sqlite3Strlen30(zFilename)==0 /* Temp file */ | |
5770 || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ | |
5771 ){ | |
5772 eNew = eOld; | |
5773 } | |
5774 | |
5775 if( (eNew!=eOld) | |
5776 && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) | |
5777 ){ | |
5778 if( !db->autoCommit || db->nVdbeRead>1 ){ | |
5779 rc = SQLITE_ERROR; | |
5780 sqlite3SetString(&p->zErrMsg, db, | |
5781 "cannot change %s wal mode from within a transaction", | |
5782 (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") | |
5783 ); | |
5784 break; | |
5785 }else{ | |
5786 | |
5787 if( eOld==PAGER_JOURNALMODE_WAL ){ | |
5788 /* If leaving WAL mode, close the log file. If successful, the call | |
5789 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log | |
5790 ** file. An EXCLUSIVE lock may still be held on the database file | |
5791 ** after a successful return. | |
5792 */ | |
5793 rc = sqlite3PagerCloseWal(pPager); | |
5794 if( rc==SQLITE_OK ){ | |
5795 sqlite3PagerSetJournalMode(pPager, eNew); | |
5796 } | |
5797 }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ | |
5798 /* Cannot transition directly from MEMORY to WAL. Use mode OFF | |
5799 ** as an intermediate */ | |
5800 sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); | |
5801 } | |
5802 | |
5803 /* Open a transaction on the database file. Regardless of the journal | |
5804 ** mode, this transaction always uses a rollback journal. | |
5805 */ | |
5806 assert( sqlite3BtreeIsInTrans(pBt)==0 ); | |
5807 if( rc==SQLITE_OK ){ | |
5808 rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); | |
5809 } | |
5810 } | |
5811 } | |
5812 #endif /* ifndef SQLITE_OMIT_WAL */ | |
5813 | |
5814 if( rc ){ | |
5815 eNew = eOld; | |
5816 } | |
5817 eNew = sqlite3PagerSetJournalMode(pPager, eNew); | |
5818 | |
5819 pOut = &aMem[pOp->p2]; | |
5820 pOut->flags = MEM_Str|MEM_Static|MEM_Term; | |
5821 pOut->z = (char *)sqlite3JournalModename(eNew); | |
5822 pOut->n = sqlite3Strlen30(pOut->z); | |
5823 pOut->enc = SQLITE_UTF8; | |
5824 sqlite3VdbeChangeEncoding(pOut, encoding); | |
5825 break; | |
5826 }; | |
5827 #endif /* SQLITE_OMIT_PRAGMA */ | |
5828 | |
5829 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) | |
5830 /* Opcode: Vacuum * * * * * | |
5831 ** | |
5832 ** Vacuum the entire database. This opcode will cause other virtual | |
5833 ** machines to be created and run. It may not be called from within | |
5834 ** a transaction. | |
5835 */ | |
5836 case OP_Vacuum: { | |
5837 assert( p->readOnly==0 ); | |
5838 rc = sqlite3RunVacuum(&p->zErrMsg, db); | |
5839 break; | |
5840 } | |
5841 #endif | |
5842 | |
5843 #if !defined(SQLITE_OMIT_AUTOVACUUM) | |
5844 /* Opcode: IncrVacuum P1 P2 * * * | |
5845 ** | |
5846 ** Perform a single step of the incremental vacuum procedure on | |
5847 ** the P1 database. If the vacuum has finished, jump to instruction | |
5848 ** P2. Otherwise, fall through to the next instruction. | |
5849 */ | |
5850 case OP_IncrVacuum: { /* jump */ | |
5851 Btree *pBt; | |
5852 | |
5853 assert( pOp->p1>=0 && pOp->p1<db->nDb ); | |
5854 assert( DbMaskTest(p->btreeMask, pOp->p1) ); | |
5855 assert( p->readOnly==0 ); | |
5856 pBt = db->aDb[pOp->p1].pBt; | |
5857 rc = sqlite3BtreeIncrVacuum(pBt); | |
5858 VdbeBranchTaken(rc==SQLITE_DONE,2); | |
5859 if( rc==SQLITE_DONE ){ | |
5860 pc = pOp->p2 - 1; | |
5861 rc = SQLITE_OK; | |
5862 } | |
5863 break; | |
5864 } | |
5865 #endif | |
5866 | |
5867 /* Opcode: Expire P1 * * * * | |
5868 ** | |
5869 ** Cause precompiled statements to expire. When an expired statement | |
5870 ** is executed using sqlite3_step() it will either automatically | |
5871 ** reprepare itself (if it was originally created using sqlite3_prepare_v2()) | |
5872 ** or it will fail with SQLITE_SCHEMA. | |
5873 ** | |
5874 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero, | |
5875 ** then only the currently executing statement is expired. | |
5876 */ | |
5877 case OP_Expire: { | |
5878 if( !pOp->p1 ){ | |
5879 sqlite3ExpirePreparedStatements(db); | |
5880 }else{ | |
5881 p->expired = 1; | |
5882 } | |
5883 break; | |
5884 } | |
5885 | |
5886 #ifndef SQLITE_OMIT_SHARED_CACHE | |
5887 /* Opcode: TableLock P1 P2 P3 P4 * | |
5888 ** Synopsis: iDb=P1 root=P2 write=P3 | |
5889 ** | |
5890 ** Obtain a lock on a particular table. This instruction is only used when | |
5891 ** the shared-cache feature is enabled. | |
5892 ** | |
5893 ** P1 is the index of the database in sqlite3.aDb[] of the database | |
5894 ** on which the lock is acquired. A readlock is obtained if P3==0 or | |
5895 ** a write lock if P3==1. | |
5896 ** | |
5897 ** P2 contains the root-page of the table to lock. | |
5898 ** | |
5899 ** P4 contains a pointer to the name of the table being locked. This is only | |
5900 ** used to generate an error message if the lock cannot be obtained. | |
5901 */ | |
5902 case OP_TableLock: { | |
5903 u8 isWriteLock = (u8)pOp->p3; | |
5904 if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){ | |
5905 int p1 = pOp->p1; | |
5906 assert( p1>=0 && p1<db->nDb ); | |
5907 assert( DbMaskTest(p->btreeMask, p1) ); | |
5908 assert( isWriteLock==0 || isWriteLock==1 ); | |
5909 rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock); | |
5910 if( (rc&0xFF)==SQLITE_LOCKED ){ | |
5911 const char *z = pOp->p4.z; | |
5912 sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z); | |
5913 } | |
5914 } | |
5915 break; | |
5916 } | |
5917 #endif /* SQLITE_OMIT_SHARED_CACHE */ | |
5918 | |
5919 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
5920 /* Opcode: VBegin * * * P4 * | |
5921 ** | |
5922 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the | |
5923 ** xBegin method for that table. | |
5924 ** | |
5925 ** Also, whether or not P4 is set, check that this is not being called from | |
5926 ** within a callback to a virtual table xSync() method. If it is, the error | |
5927 ** code will be set to SQLITE_LOCKED. | |
5928 */ | |
5929 case OP_VBegin: { | |
5930 VTable *pVTab; | |
5931 pVTab = pOp->p4.pVtab; | |
5932 rc = sqlite3VtabBegin(db, pVTab); | |
5933 if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab); | |
5934 break; | |
5935 } | |
5936 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
5937 | |
5938 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
5939 /* Opcode: VCreate P1 * * P4 * | |
5940 ** | |
5941 ** P4 is the name of a virtual table in database P1. Call the xCreate method | |
5942 ** for that table. | |
5943 */ | |
5944 case OP_VCreate: { | |
5945 rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg); | |
5946 break; | |
5947 } | |
5948 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
5949 | |
5950 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
5951 /* Opcode: VDestroy P1 * * P4 * | |
5952 ** | |
5953 ** P4 is the name of a virtual table in database P1. Call the xDestroy method | |
5954 ** of that table. | |
5955 */ | |
5956 case OP_VDestroy: { | |
5957 p->inVtabMethod = 2; | |
5958 rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z); | |
5959 p->inVtabMethod = 0; | |
5960 break; | |
5961 } | |
5962 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
5963 | |
5964 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
5965 /* Opcode: VOpen P1 * * P4 * | |
5966 ** | |
5967 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. | |
5968 ** P1 is a cursor number. This opcode opens a cursor to the virtual | |
5969 ** table and stores that cursor in P1. | |
5970 */ | |
5971 case OP_VOpen: { | |
5972 VdbeCursor *pCur; | |
5973 sqlite3_vtab_cursor *pVtabCursor; | |
5974 sqlite3_vtab *pVtab; | |
5975 sqlite3_module *pModule; | |
5976 | |
5977 assert( p->bIsReader ); | |
5978 pCur = 0; | |
5979 pVtabCursor = 0; | |
5980 pVtab = pOp->p4.pVtab->pVtab; | |
5981 pModule = (sqlite3_module *)pVtab->pModule; | |
5982 assert(pVtab && pModule); | |
5983 rc = pModule->xOpen(pVtab, &pVtabCursor); | |
5984 sqlite3VtabImportErrmsg(p, pVtab); | |
5985 if( SQLITE_OK==rc ){ | |
5986 /* Initialize sqlite3_vtab_cursor base class */ | |
5987 pVtabCursor->pVtab = pVtab; | |
5988 | |
5989 /* Initialize vdbe cursor object */ | |
5990 pCur = allocateCursor(p, pOp->p1, 0, -1, 0); | |
5991 if( pCur ){ | |
5992 pCur->pVtabCursor = pVtabCursor; | |
5993 }else{ | |
5994 db->mallocFailed = 1; | |
5995 pModule->xClose(pVtabCursor); | |
5996 } | |
5997 } | |
5998 break; | |
5999 } | |
6000 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
6001 | |
6002 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
6003 /* Opcode: VFilter P1 P2 P3 P4 * | |
6004 ** Synopsis: iplan=r[P3] zplan='P4' | |
6005 ** | |
6006 ** P1 is a cursor opened using VOpen. P2 is an address to jump to if | |
6007 ** the filtered result set is empty. | |
6008 ** | |
6009 ** P4 is either NULL or a string that was generated by the xBestIndex | |
6010 ** method of the module. The interpretation of the P4 string is left | |
6011 ** to the module implementation. | |
6012 ** | |
6013 ** This opcode invokes the xFilter method on the virtual table specified | |
6014 ** by P1. The integer query plan parameter to xFilter is stored in register | |
6015 ** P3. Register P3+1 stores the argc parameter to be passed to the | |
6016 ** xFilter method. Registers P3+2..P3+1+argc are the argc | |
6017 ** additional parameters which are passed to | |
6018 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter. | |
6019 ** | |
6020 ** A jump is made to P2 if the result set after filtering would be empty. | |
6021 */ | |
6022 case OP_VFilter: { /* jump */ | |
6023 int nArg; | |
6024 int iQuery; | |
6025 const sqlite3_module *pModule; | |
6026 Mem *pQuery; | |
6027 Mem *pArgc; | |
6028 sqlite3_vtab_cursor *pVtabCursor; | |
6029 sqlite3_vtab *pVtab; | |
6030 VdbeCursor *pCur; | |
6031 int res; | |
6032 int i; | |
6033 Mem **apArg; | |
6034 | |
6035 pQuery = &aMem[pOp->p3]; | |
6036 pArgc = &pQuery[1]; | |
6037 pCur = p->apCsr[pOp->p1]; | |
6038 assert( memIsValid(pQuery) ); | |
6039 REGISTER_TRACE(pOp->p3, pQuery); | |
6040 assert( pCur->pVtabCursor ); | |
6041 pVtabCursor = pCur->pVtabCursor; | |
6042 pVtab = pVtabCursor->pVtab; | |
6043 pModule = pVtab->pModule; | |
6044 | |
6045 /* Grab the index number and argc parameters */ | |
6046 assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); | |
6047 nArg = (int)pArgc->u.i; | |
6048 iQuery = (int)pQuery->u.i; | |
6049 | |
6050 /* Invoke the xFilter method */ | |
6051 { | |
6052 res = 0; | |
6053 apArg = p->apArg; | |
6054 for(i = 0; i<nArg; i++){ | |
6055 apArg[i] = &pArgc[i+1]; | |
6056 } | |
6057 | |
6058 p->inVtabMethod = 1; | |
6059 rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); | |
6060 p->inVtabMethod = 0; | |
6061 sqlite3VtabImportErrmsg(p, pVtab); | |
6062 if( rc==SQLITE_OK ){ | |
6063 res = pModule->xEof(pVtabCursor); | |
6064 } | |
6065 VdbeBranchTaken(res!=0,2); | |
6066 if( res ){ | |
6067 pc = pOp->p2 - 1; | |
6068 } | |
6069 } | |
6070 pCur->nullRow = 0; | |
6071 | |
6072 break; | |
6073 } | |
6074 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
6075 | |
6076 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
6077 /* Opcode: VColumn P1 P2 P3 * * | |
6078 ** Synopsis: r[P3]=vcolumn(P2) | |
6079 ** | |
6080 ** Store the value of the P2-th column of | |
6081 ** the row of the virtual-table that the | |
6082 ** P1 cursor is pointing to into register P3. | |
6083 */ | |
6084 case OP_VColumn: { | |
6085 sqlite3_vtab *pVtab; | |
6086 const sqlite3_module *pModule; | |
6087 Mem *pDest; | |
6088 sqlite3_context sContext; | |
6089 | |
6090 VdbeCursor *pCur = p->apCsr[pOp->p1]; | |
6091 assert( pCur->pVtabCursor ); | |
6092 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) ); | |
6093 pDest = &aMem[pOp->p3]; | |
6094 memAboutToChange(p, pDest); | |
6095 if( pCur->nullRow ){ | |
6096 sqlite3VdbeMemSetNull(pDest); | |
6097 break; | |
6098 } | |
6099 pVtab = pCur->pVtabCursor->pVtab; | |
6100 pModule = pVtab->pModule; | |
6101 assert( pModule->xColumn ); | |
6102 memset(&sContext, 0, sizeof(sContext)); | |
6103 sContext.pOut = pDest; | |
6104 MemSetTypeFlag(pDest, MEM_Null); | |
6105 rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2); | |
6106 sqlite3VtabImportErrmsg(p, pVtab); | |
6107 if( sContext.isError ){ | |
6108 rc = sContext.isError; | |
6109 } | |
6110 sqlite3VdbeChangeEncoding(pDest, encoding); | |
6111 REGISTER_TRACE(pOp->p3, pDest); | |
6112 UPDATE_MAX_BLOBSIZE(pDest); | |
6113 | |
6114 if( sqlite3VdbeMemTooBig(pDest) ){ | |
6115 goto too_big; | |
6116 } | |
6117 break; | |
6118 } | |
6119 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
6120 | |
6121 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
6122 /* Opcode: VNext P1 P2 * * * | |
6123 ** | |
6124 ** Advance virtual table P1 to the next row in its result set and | |
6125 ** jump to instruction P2. Or, if the virtual table has reached | |
6126 ** the end of its result set, then fall through to the next instruction. | |
6127 */ | |
6128 case OP_VNext: { /* jump */ | |
6129 sqlite3_vtab *pVtab; | |
6130 const sqlite3_module *pModule; | |
6131 int res; | |
6132 VdbeCursor *pCur; | |
6133 | |
6134 res = 0; | |
6135 pCur = p->apCsr[pOp->p1]; | |
6136 assert( pCur->pVtabCursor ); | |
6137 if( pCur->nullRow ){ | |
6138 break; | |
6139 } | |
6140 pVtab = pCur->pVtabCursor->pVtab; | |
6141 pModule = pVtab->pModule; | |
6142 assert( pModule->xNext ); | |
6143 | |
6144 /* Invoke the xNext() method of the module. There is no way for the | |
6145 ** underlying implementation to return an error if one occurs during | |
6146 ** xNext(). Instead, if an error occurs, true is returned (indicating that | |
6147 ** data is available) and the error code returned when xColumn or | |
6148 ** some other method is next invoked on the save virtual table cursor. | |
6149 */ | |
6150 p->inVtabMethod = 1; | |
6151 rc = pModule->xNext(pCur->pVtabCursor); | |
6152 p->inVtabMethod = 0; | |
6153 sqlite3VtabImportErrmsg(p, pVtab); | |
6154 if( rc==SQLITE_OK ){ | |
6155 res = pModule->xEof(pCur->pVtabCursor); | |
6156 } | |
6157 VdbeBranchTaken(!res,2); | |
6158 if( !res ){ | |
6159 /* If there is data, jump to P2 */ | |
6160 pc = pOp->p2 - 1; | |
6161 } | |
6162 goto check_for_interrupt; | |
6163 } | |
6164 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
6165 | |
6166 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
6167 /* Opcode: VRename P1 * * P4 * | |
6168 ** | |
6169 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. | |
6170 ** This opcode invokes the corresponding xRename method. The value | |
6171 ** in register P1 is passed as the zName argument to the xRename method. | |
6172 */ | |
6173 case OP_VRename: { | |
6174 sqlite3_vtab *pVtab; | |
6175 Mem *pName; | |
6176 | |
6177 pVtab = pOp->p4.pVtab->pVtab; | |
6178 pName = &aMem[pOp->p1]; | |
6179 assert( pVtab->pModule->xRename ); | |
6180 assert( memIsValid(pName) ); | |
6181 assert( p->readOnly==0 ); | |
6182 REGISTER_TRACE(pOp->p1, pName); | |
6183 assert( pName->flags & MEM_Str ); | |
6184 testcase( pName->enc==SQLITE_UTF8 ); | |
6185 testcase( pName->enc==SQLITE_UTF16BE ); | |
6186 testcase( pName->enc==SQLITE_UTF16LE ); | |
6187 rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8); | |
6188 if( rc==SQLITE_OK ){ | |
6189 rc = pVtab->pModule->xRename(pVtab, pName->z); | |
6190 sqlite3VtabImportErrmsg(p, pVtab); | |
6191 p->expired = 0; | |
6192 } | |
6193 break; | |
6194 } | |
6195 #endif | |
6196 | |
6197 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
6198 /* Opcode: VUpdate P1 P2 P3 P4 P5 | |
6199 ** Synopsis: data=r[P3@P2] | |
6200 ** | |
6201 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure. | |
6202 ** This opcode invokes the corresponding xUpdate method. P2 values | |
6203 ** are contiguous memory cells starting at P3 to pass to the xUpdate | |
6204 ** invocation. The value in register (P3+P2-1) corresponds to the | |
6205 ** p2th element of the argv array passed to xUpdate. | |
6206 ** | |
6207 ** The xUpdate method will do a DELETE or an INSERT or both. | |
6208 ** The argv[0] element (which corresponds to memory cell P3) | |
6209 ** is the rowid of a row to delete. If argv[0] is NULL then no | |
6210 ** deletion occurs. The argv[1] element is the rowid of the new | |
6211 ** row. This can be NULL to have the virtual table select the new | |
6212 ** rowid for itself. The subsequent elements in the array are | |
6213 ** the values of columns in the new row. | |
6214 ** | |
6215 ** If P2==1 then no insert is performed. argv[0] is the rowid of | |
6216 ** a row to delete. | |
6217 ** | |
6218 ** P1 is a boolean flag. If it is set to true and the xUpdate call | |
6219 ** is successful, then the value returned by sqlite3_last_insert_rowid() | |
6220 ** is set to the value of the rowid for the row just inserted. | |
6221 ** | |
6222 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to | |
6223 ** apply in the case of a constraint failure on an insert or update. | |
6224 */ | |
6225 case OP_VUpdate: { | |
6226 sqlite3_vtab *pVtab; | |
6227 sqlite3_module *pModule; | |
6228 int nArg; | |
6229 int i; | |
6230 sqlite_int64 rowid; | |
6231 Mem **apArg; | |
6232 Mem *pX; | |
6233 | |
6234 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback | |
6235 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace | |
6236 ); | |
6237 assert( p->readOnly==0 ); | |
6238 pVtab = pOp->p4.pVtab->pVtab; | |
6239 pModule = (sqlite3_module *)pVtab->pModule; | |
6240 nArg = pOp->p2; | |
6241 assert( pOp->p4type==P4_VTAB ); | |
6242 if( ALWAYS(pModule->xUpdate) ){ | |
6243 u8 vtabOnConflict = db->vtabOnConflict; | |
6244 apArg = p->apArg; | |
6245 pX = &aMem[pOp->p3]; | |
6246 for(i=0; i<nArg; i++){ | |
6247 assert( memIsValid(pX) ); | |
6248 memAboutToChange(p, pX); | |
6249 apArg[i] = pX; | |
6250 pX++; | |
6251 } | |
6252 db->vtabOnConflict = pOp->p5; | |
6253 rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid); | |
6254 db->vtabOnConflict = vtabOnConflict; | |
6255 sqlite3VtabImportErrmsg(p, pVtab); | |
6256 if( rc==SQLITE_OK && pOp->p1 ){ | |
6257 assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) ); | |
6258 db->lastRowid = lastRowid = rowid; | |
6259 } | |
6260 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){ | |
6261 if( pOp->p5==OE_Ignore ){ | |
6262 rc = SQLITE_OK; | |
6263 }else{ | |
6264 p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5); | |
6265 } | |
6266 }else{ | |
6267 p->nChange++; | |
6268 } | |
6269 } | |
6270 break; | |
6271 } | |
6272 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
6273 | |
6274 #ifndef SQLITE_OMIT_PAGER_PRAGMAS | |
6275 /* Opcode: Pagecount P1 P2 * * * | |
6276 ** | |
6277 ** Write the current number of pages in database P1 to memory cell P2. | |
6278 */ | |
6279 case OP_Pagecount: { /* out2-prerelease */ | |
6280 pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); | |
6281 break; | |
6282 } | |
6283 #endif | |
6284 | |
6285 | |
6286 #ifndef SQLITE_OMIT_PAGER_PRAGMAS | |
6287 /* Opcode: MaxPgcnt P1 P2 P3 * * | |
6288 ** | |
6289 ** Try to set the maximum page count for database P1 to the value in P3. | |
6290 ** Do not let the maximum page count fall below the current page count and | |
6291 ** do not change the maximum page count value if P3==0. | |
6292 ** | |
6293 ** Store the maximum page count after the change in register P2. | |
6294 */ | |
6295 case OP_MaxPgcnt: { /* out2-prerelease */ | |
6296 unsigned int newMax; | |
6297 Btree *pBt; | |
6298 | |
6299 pBt = db->aDb[pOp->p1].pBt; | |
6300 newMax = 0; | |
6301 if( pOp->p3 ){ | |
6302 newMax = sqlite3BtreeLastPage(pBt); | |
6303 if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; | |
6304 } | |
6305 pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax); | |
6306 break; | |
6307 } | |
6308 #endif | |
6309 | |
6310 | |
6311 /* Opcode: Init * P2 * P4 * | |
6312 ** Synopsis: Start at P2 | |
6313 ** | |
6314 ** Programs contain a single instance of this opcode as the very first | |
6315 ** opcode. | |
6316 ** | |
6317 ** If tracing is enabled (by the sqlite3_trace()) interface, then | |
6318 ** the UTF-8 string contained in P4 is emitted on the trace callback. | |
6319 ** Or if P4 is blank, use the string returned by sqlite3_sql(). | |
6320 ** | |
6321 ** If P2 is not zero, jump to instruction P2. | |
6322 */ | |
6323 case OP_Init: { /* jump */ | |
6324 char *zTrace; | |
6325 char *z; | |
6326 | |
6327 if( pOp->p2 ){ | |
6328 pc = pOp->p2 - 1; | |
6329 } | |
6330 #ifndef SQLITE_OMIT_TRACE | |
6331 if( db->xTrace | |
6332 && !p->doingRerun | |
6333 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 | |
6334 ){ | |
6335 z = sqlite3VdbeExpandSql(p, zTrace); | |
6336 db->xTrace(db->pTraceArg, z); | |
6337 sqlite3DbFree(db, z); | |
6338 } | |
6339 #ifdef SQLITE_USE_FCNTL_TRACE | |
6340 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); | |
6341 if( zTrace ){ | |
6342 int i; | |
6343 for(i=0; i<db->nDb; i++){ | |
6344 if( DbMaskTest(p->btreeMask, i)==0 ) continue; | |
6345 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); | |
6346 } | |
6347 } | |
6348 #endif /* SQLITE_USE_FCNTL_TRACE */ | |
6349 #ifdef SQLITE_DEBUG | |
6350 if( (db->flags & SQLITE_SqlTrace)!=0 | |
6351 && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 | |
6352 ){ | |
6353 sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); | |
6354 } | |
6355 #endif /* SQLITE_DEBUG */ | |
6356 #endif /* SQLITE_OMIT_TRACE */ | |
6357 break; | |
6358 } | |
6359 | |
6360 | |
6361 /* Opcode: Noop * * * * * | |
6362 ** | |
6363 ** Do nothing. This instruction is often useful as a jump | |
6364 ** destination. | |
6365 */ | |
6366 /* | |
6367 ** The magic Explain opcode are only inserted when explain==2 (which | |
6368 ** is to say when the EXPLAIN QUERY PLAN syntax is used.) | |
6369 ** This opcode records information from the optimizer. It is the | |
6370 ** the same as a no-op. This opcodesnever appears in a real VM program. | |
6371 */ | |
6372 default: { /* This is really OP_Noop and OP_Explain */ | |
6373 assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain ); | |
6374 break; | |
6375 } | |
6376 | |
6377 /***************************************************************************** | |
6378 ** The cases of the switch statement above this line should all be indented | |
6379 ** by 6 spaces. But the left-most 6 spaces have been removed to improve the | |
6380 ** readability. From this point on down, the normal indentation rules are | |
6381 ** restored. | |
6382 *****************************************************************************/ | |
6383 } | |
6384 | |
6385 #ifdef VDBE_PROFILE | |
6386 { | |
6387 u64 endTime = sqlite3Hwtime(); | |
6388 if( endTime>start ) pOp->cycles += endTime - start; | |
6389 pOp->cnt++; | |
6390 } | |
6391 #endif | |
6392 | |
6393 /* The following code adds nothing to the actual functionality | |
6394 ** of the program. It is only here for testing and debugging. | |
6395 ** On the other hand, it does burn CPU cycles every time through | |
6396 ** the evaluator loop. So we can leave it out when NDEBUG is defined. | |
6397 */ | |
6398 #ifndef NDEBUG | |
6399 assert( pc>=-1 && pc<p->nOp ); | |
6400 | |
6401 #ifdef SQLITE_DEBUG | |
6402 if( db->flags & SQLITE_VdbeTrace ){ | |
6403 if( rc!=0 ) printf("rc=%d\n",rc); | |
6404 if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){ | |
6405 registerTrace(pOp->p2, &aMem[pOp->p2]); | |
6406 } | |
6407 if( pOp->opflags & OPFLG_OUT3 ){ | |
6408 registerTrace(pOp->p3, &aMem[pOp->p3]); | |
6409 } | |
6410 } | |
6411 #endif /* SQLITE_DEBUG */ | |
6412 #endif /* NDEBUG */ | |
6413 } /* The end of the for(;;) loop the loops through opcodes */ | |
6414 | |
6415 /* If we reach this point, it means that execution is finished with | |
6416 ** an error of some kind. | |
6417 */ | |
6418 vdbe_error_halt: | |
6419 assert( rc ); | |
6420 p->rc = rc; | |
6421 testcase( sqlite3GlobalConfig.xLog!=0 ); | |
6422 sqlite3_log(rc, "statement aborts at %d: [%s] %s", | |
6423 pc, p->zSql, p->zErrMsg); | |
6424 sqlite3VdbeHalt(p); | |
6425 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; | |
6426 rc = SQLITE_ERROR; | |
6427 if( resetSchemaOnFault>0 ){ | |
6428 sqlite3ResetOneSchema(db, resetSchemaOnFault-1); | |
6429 } | |
6430 | |
6431 /* This is the only way out of this procedure. We have to | |
6432 ** release the mutexes on btrees that were acquired at the | |
6433 ** top. */ | |
6434 vdbe_return: | |
6435 db->lastRowid = lastRowid; | |
6436 testcase( nVmStep>0 ); | |
6437 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep; | |
6438 sqlite3VdbeLeave(p); | |
6439 return rc; | |
6440 | |
6441 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH | |
6442 ** is encountered. | |
6443 */ | |
6444 too_big: | |
6445 sqlite3SetString(&p->zErrMsg, db, "string or blob too big"); | |
6446 rc = SQLITE_TOOBIG; | |
6447 goto vdbe_error_halt; | |
6448 | |
6449 /* Jump to here if a malloc() fails. | |
6450 */ | |
6451 no_mem: | |
6452 db->mallocFailed = 1; | |
6453 sqlite3SetString(&p->zErrMsg, db, "out of memory"); | |
6454 rc = SQLITE_NOMEM; | |
6455 goto vdbe_error_halt; | |
6456 | |
6457 /* Jump to here for any other kind of fatal error. The "rc" variable | |
6458 ** should hold the error number. | |
6459 */ | |
6460 abort_due_to_error: | |
6461 assert( p->zErrMsg==0 ); | |
6462 if( db->mallocFailed ) rc = SQLITE_NOMEM; | |
6463 if( rc!=SQLITE_IOERR_NOMEM ){ | |
6464 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); | |
6465 } | |
6466 goto vdbe_error_halt; | |
6467 | |
6468 /* Jump to here if the sqlite3_interrupt() API sets the interrupt | |
6469 ** flag. | |
6470 */ | |
6471 abort_due_to_interrupt: | |
6472 assert( db->u1.isInterrupted ); | |
6473 rc = SQLITE_INTERRUPT; | |
6474 p->rc = rc; | |
6475 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc)); | |
6476 goto vdbe_error_halt; | |
6477 } | |
OLD | NEW |