OLD | NEW |
| (Empty) |
1 /* | |
2 ** 2003 September 6 | |
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 ** This file contains code used for creating, destroying, and populating | |
13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) | |
14 */ | |
15 #include "sqliteInt.h" | |
16 #include "vdbeInt.h" | |
17 | |
18 /* | |
19 ** Create a new virtual database engine. | |
20 */ | |
21 Vdbe *sqlite3VdbeCreate(Parse *pParse){ | |
22 sqlite3 *db = pParse->db; | |
23 Vdbe *p; | |
24 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); | |
25 if( p==0 ) return 0; | |
26 p->db = db; | |
27 if( db->pVdbe ){ | |
28 db->pVdbe->pPrev = p; | |
29 } | |
30 p->pNext = db->pVdbe; | |
31 p->pPrev = 0; | |
32 db->pVdbe = p; | |
33 p->magic = VDBE_MAGIC_INIT; | |
34 p->pParse = pParse; | |
35 assert( pParse->aLabel==0 ); | |
36 assert( pParse->nLabel==0 ); | |
37 assert( pParse->nOpAlloc==0 ); | |
38 return p; | |
39 } | |
40 | |
41 /* | |
42 ** Remember the SQL string for a prepared statement. | |
43 */ | |
44 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ | |
45 assert( isPrepareV2==1 || isPrepareV2==0 ); | |
46 if( p==0 ) return; | |
47 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) | |
48 if( !isPrepareV2 ) return; | |
49 #endif | |
50 assert( p->zSql==0 ); | |
51 p->zSql = sqlite3DbStrNDup(p->db, z, n); | |
52 p->isPrepareV2 = (u8)isPrepareV2; | |
53 } | |
54 | |
55 /* | |
56 ** Return the SQL associated with a prepared statement | |
57 */ | |
58 const char *sqlite3_sql(sqlite3_stmt *pStmt){ | |
59 Vdbe *p = (Vdbe *)pStmt; | |
60 return (p && p->isPrepareV2) ? p->zSql : 0; | |
61 } | |
62 | |
63 /* | |
64 ** Swap all content between two VDBE structures. | |
65 */ | |
66 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ | |
67 Vdbe tmp, *pTmp; | |
68 char *zTmp; | |
69 tmp = *pA; | |
70 *pA = *pB; | |
71 *pB = tmp; | |
72 pTmp = pA->pNext; | |
73 pA->pNext = pB->pNext; | |
74 pB->pNext = pTmp; | |
75 pTmp = pA->pPrev; | |
76 pA->pPrev = pB->pPrev; | |
77 pB->pPrev = pTmp; | |
78 zTmp = pA->zSql; | |
79 pA->zSql = pB->zSql; | |
80 pB->zSql = zTmp; | |
81 pB->isPrepareV2 = pA->isPrepareV2; | |
82 } | |
83 | |
84 /* | |
85 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger | |
86 ** than its current size. nOp is guaranteed to be less than or equal | |
87 ** to 1024/sizeof(Op). | |
88 ** | |
89 ** If an out-of-memory error occurs while resizing the array, return | |
90 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain | |
91 ** unchanged (this is so that any opcodes already allocated can be | |
92 ** correctly deallocated along with the rest of the Vdbe). | |
93 */ | |
94 static int growOpArray(Vdbe *v, int nOp){ | |
95 VdbeOp *pNew; | |
96 Parse *p = v->pParse; | |
97 | |
98 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force | |
99 ** more frequent reallocs and hence provide more opportunities for | |
100 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used | |
101 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array | |
102 ** by the minimum* amount required until the size reaches 512. Normal | |
103 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current | |
104 ** size of the op array or add 1KB of space, whichever is smaller. */ | |
105 #ifdef SQLITE_TEST_REALLOC_STRESS | |
106 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); | |
107 #else | |
108 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); | |
109 UNUSED_PARAMETER(nOp); | |
110 #endif | |
111 | |
112 assert( nOp<=(1024/sizeof(Op)) ); | |
113 assert( nNew>=(p->nOpAlloc+nOp) ); | |
114 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); | |
115 if( pNew ){ | |
116 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); | |
117 v->aOp = pNew; | |
118 } | |
119 return (pNew ? SQLITE_OK : SQLITE_NOMEM); | |
120 } | |
121 | |
122 #ifdef SQLITE_DEBUG | |
123 /* This routine is just a convenient place to set a breakpoint that will | |
124 ** fire after each opcode is inserted and displayed using | |
125 ** "PRAGMA vdbe_addoptrace=on". | |
126 */ | |
127 static void test_addop_breakpoint(void){ | |
128 static int n = 0; | |
129 n++; | |
130 } | |
131 #endif | |
132 | |
133 /* | |
134 ** Add a new instruction to the list of instructions current in the | |
135 ** VDBE. Return the address of the new instruction. | |
136 ** | |
137 ** Parameters: | |
138 ** | |
139 ** p Pointer to the VDBE | |
140 ** | |
141 ** op The opcode for this instruction | |
142 ** | |
143 ** p1, p2, p3 Operands | |
144 ** | |
145 ** Use the sqlite3VdbeResolveLabel() function to fix an address and | |
146 ** the sqlite3VdbeChangeP4() function to change the value of the P4 | |
147 ** operand. | |
148 */ | |
149 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ | |
150 int i; | |
151 VdbeOp *pOp; | |
152 | |
153 i = p->nOp; | |
154 assert( p->magic==VDBE_MAGIC_INIT ); | |
155 assert( op>0 && op<0xff ); | |
156 if( p->pParse->nOpAlloc<=i ){ | |
157 if( growOpArray(p, 1) ){ | |
158 return 1; | |
159 } | |
160 } | |
161 p->nOp++; | |
162 pOp = &p->aOp[i]; | |
163 pOp->opcode = (u8)op; | |
164 pOp->p5 = 0; | |
165 pOp->p1 = p1; | |
166 pOp->p2 = p2; | |
167 pOp->p3 = p3; | |
168 pOp->p4.p = 0; | |
169 pOp->p4type = P4_NOTUSED; | |
170 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
171 pOp->zComment = 0; | |
172 #endif | |
173 #ifdef SQLITE_DEBUG | |
174 if( p->db->flags & SQLITE_VdbeAddopTrace ){ | |
175 int jj, kk; | |
176 Parse *pParse = p->pParse; | |
177 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){ | |
178 struct yColCache *x = pParse->aColCache + jj; | |
179 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; | |
180 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); | |
181 kk++; | |
182 } | |
183 if( kk ) printf("\n"); | |
184 sqlite3VdbePrintOp(0, i, &p->aOp[i]); | |
185 test_addop_breakpoint(); | |
186 } | |
187 #endif | |
188 #ifdef VDBE_PROFILE | |
189 pOp->cycles = 0; | |
190 pOp->cnt = 0; | |
191 #endif | |
192 #ifdef SQLITE_VDBE_COVERAGE | |
193 pOp->iSrcLine = 0; | |
194 #endif | |
195 return i; | |
196 } | |
197 int sqlite3VdbeAddOp0(Vdbe *p, int op){ | |
198 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); | |
199 } | |
200 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ | |
201 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); | |
202 } | |
203 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ | |
204 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); | |
205 } | |
206 | |
207 | |
208 /* | |
209 ** Add an opcode that includes the p4 value as a pointer. | |
210 */ | |
211 int sqlite3VdbeAddOp4( | |
212 Vdbe *p, /* Add the opcode to this VM */ | |
213 int op, /* The new opcode */ | |
214 int p1, /* The P1 operand */ | |
215 int p2, /* The P2 operand */ | |
216 int p3, /* The P3 operand */ | |
217 const char *zP4, /* The P4 operand */ | |
218 int p4type /* P4 operand type */ | |
219 ){ | |
220 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | |
221 sqlite3VdbeChangeP4(p, addr, zP4, p4type); | |
222 return addr; | |
223 } | |
224 | |
225 /* | |
226 ** Add an OP_ParseSchema opcode. This routine is broken out from | |
227 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees | |
228 ** as having been used. | |
229 ** | |
230 ** The zWhere string must have been obtained from sqlite3_malloc(). | |
231 ** This routine will take ownership of the allocated memory. | |
232 */ | |
233 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ | |
234 int j; | |
235 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); | |
236 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC); | |
237 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); | |
238 } | |
239 | |
240 /* | |
241 ** Add an opcode that includes the p4 value as an integer. | |
242 */ | |
243 int sqlite3VdbeAddOp4Int( | |
244 Vdbe *p, /* Add the opcode to this VM */ | |
245 int op, /* The new opcode */ | |
246 int p1, /* The P1 operand */ | |
247 int p2, /* The P2 operand */ | |
248 int p3, /* The P3 operand */ | |
249 int p4 /* The P4 operand as an integer */ | |
250 ){ | |
251 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | |
252 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); | |
253 return addr; | |
254 } | |
255 | |
256 /* | |
257 ** Create a new symbolic label for an instruction that has yet to be | |
258 ** coded. The symbolic label is really just a negative number. The | |
259 ** label can be used as the P2 value of an operation. Later, when | |
260 ** the label is resolved to a specific address, the VDBE will scan | |
261 ** through its operation list and change all values of P2 which match | |
262 ** the label into the resolved address. | |
263 ** | |
264 ** The VDBE knows that a P2 value is a label because labels are | |
265 ** always negative and P2 values are suppose to be non-negative. | |
266 ** Hence, a negative P2 value is a label that has yet to be resolved. | |
267 ** | |
268 ** Zero is returned if a malloc() fails. | |
269 */ | |
270 int sqlite3VdbeMakeLabel(Vdbe *v){ | |
271 Parse *p = v->pParse; | |
272 int i = p->nLabel++; | |
273 assert( v->magic==VDBE_MAGIC_INIT ); | |
274 if( (i & (i-1))==0 ){ | |
275 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, | |
276 (i*2+1)*sizeof(p->aLabel[0])); | |
277 } | |
278 if( p->aLabel ){ | |
279 p->aLabel[i] = -1; | |
280 } | |
281 return -1-i; | |
282 } | |
283 | |
284 /* | |
285 ** Resolve label "x" to be the address of the next instruction to | |
286 ** be inserted. The parameter "x" must have been obtained from | |
287 ** a prior call to sqlite3VdbeMakeLabel(). | |
288 */ | |
289 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ | |
290 Parse *p = v->pParse; | |
291 int j = -1-x; | |
292 assert( v->magic==VDBE_MAGIC_INIT ); | |
293 assert( j<p->nLabel ); | |
294 if( ALWAYS(j>=0) && p->aLabel ){ | |
295 p->aLabel[j] = v->nOp; | |
296 } | |
297 p->iFixedOp = v->nOp - 1; | |
298 } | |
299 | |
300 /* | |
301 ** Mark the VDBE as one that can only be run one time. | |
302 */ | |
303 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ | |
304 p->runOnlyOnce = 1; | |
305 } | |
306 | |
307 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ | |
308 | |
309 /* | |
310 ** The following type and function are used to iterate through all opcodes | |
311 ** in a Vdbe main program and each of the sub-programs (triggers) it may | |
312 ** invoke directly or indirectly. It should be used as follows: | |
313 ** | |
314 ** Op *pOp; | |
315 ** VdbeOpIter sIter; | |
316 ** | |
317 ** memset(&sIter, 0, sizeof(sIter)); | |
318 ** sIter.v = v; // v is of type Vdbe* | |
319 ** while( (pOp = opIterNext(&sIter)) ){ | |
320 ** // Do something with pOp | |
321 ** } | |
322 ** sqlite3DbFree(v->db, sIter.apSub); | |
323 ** | |
324 */ | |
325 typedef struct VdbeOpIter VdbeOpIter; | |
326 struct VdbeOpIter { | |
327 Vdbe *v; /* Vdbe to iterate through the opcodes of */ | |
328 SubProgram **apSub; /* Array of subprograms */ | |
329 int nSub; /* Number of entries in apSub */ | |
330 int iAddr; /* Address of next instruction to return */ | |
331 int iSub; /* 0 = main program, 1 = first sub-program etc. */ | |
332 }; | |
333 static Op *opIterNext(VdbeOpIter *p){ | |
334 Vdbe *v = p->v; | |
335 Op *pRet = 0; | |
336 Op *aOp; | |
337 int nOp; | |
338 | |
339 if( p->iSub<=p->nSub ){ | |
340 | |
341 if( p->iSub==0 ){ | |
342 aOp = v->aOp; | |
343 nOp = v->nOp; | |
344 }else{ | |
345 aOp = p->apSub[p->iSub-1]->aOp; | |
346 nOp = p->apSub[p->iSub-1]->nOp; | |
347 } | |
348 assert( p->iAddr<nOp ); | |
349 | |
350 pRet = &aOp[p->iAddr]; | |
351 p->iAddr++; | |
352 if( p->iAddr==nOp ){ | |
353 p->iSub++; | |
354 p->iAddr = 0; | |
355 } | |
356 | |
357 if( pRet->p4type==P4_SUBPROGRAM ){ | |
358 int nByte = (p->nSub+1)*sizeof(SubProgram*); | |
359 int j; | |
360 for(j=0; j<p->nSub; j++){ | |
361 if( p->apSub[j]==pRet->p4.pProgram ) break; | |
362 } | |
363 if( j==p->nSub ){ | |
364 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte); | |
365 if( !p->apSub ){ | |
366 pRet = 0; | |
367 }else{ | |
368 p->apSub[p->nSub++] = pRet->p4.pProgram; | |
369 } | |
370 } | |
371 } | |
372 } | |
373 | |
374 return pRet; | |
375 } | |
376 | |
377 /* | |
378 ** Check if the program stored in the VM associated with pParse may | |
379 ** throw an ABORT exception (causing the statement, but not entire transaction | |
380 ** to be rolled back). This condition is true if the main program or any | |
381 ** sub-programs contains any of the following: | |
382 ** | |
383 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | |
384 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | |
385 ** * OP_Destroy | |
386 ** * OP_VUpdate | |
387 ** * OP_VRename | |
388 ** * OP_FkCounter with P2==0 (immediate foreign key constraint) | |
389 ** | |
390 ** Then check that the value of Parse.mayAbort is true if an | |
391 ** ABORT may be thrown, or false otherwise. Return true if it does | |
392 ** match, or false otherwise. This function is intended to be used as | |
393 ** part of an assert statement in the compiler. Similar to: | |
394 ** | |
395 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); | |
396 */ | |
397 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ | |
398 int hasAbort = 0; | |
399 Op *pOp; | |
400 VdbeOpIter sIter; | |
401 memset(&sIter, 0, sizeof(sIter)); | |
402 sIter.v = v; | |
403 | |
404 while( (pOp = opIterNext(&sIter))!=0 ){ | |
405 int opcode = pOp->opcode; | |
406 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename | |
407 #ifndef SQLITE_OMIT_FOREIGN_KEY | |
408 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) | |
409 #endif | |
410 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) | |
411 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) | |
412 ){ | |
413 hasAbort = 1; | |
414 break; | |
415 } | |
416 } | |
417 sqlite3DbFree(v->db, sIter.apSub); | |
418 | |
419 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. | |
420 ** If malloc failed, then the while() loop above may not have iterated | |
421 ** through all opcodes and hasAbort may be set incorrectly. Return | |
422 ** true for this case to prevent the assert() in the callers frame | |
423 ** from failing. */ | |
424 return ( v->db->mallocFailed || hasAbort==mayAbort ); | |
425 } | |
426 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ | |
427 | |
428 /* | |
429 ** Loop through the program looking for P2 values that are negative | |
430 ** on jump instructions. Each such value is a label. Resolve the | |
431 ** label by setting the P2 value to its correct non-zero value. | |
432 ** | |
433 ** This routine is called once after all opcodes have been inserted. | |
434 ** | |
435 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument | |
436 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by | |
437 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. | |
438 ** | |
439 ** The Op.opflags field is set on all opcodes. | |
440 */ | |
441 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ | |
442 int i; | |
443 int nMaxArgs = *pMaxFuncArgs; | |
444 Op *pOp; | |
445 Parse *pParse = p->pParse; | |
446 int *aLabel = pParse->aLabel; | |
447 p->readOnly = 1; | |
448 p->bIsReader = 0; | |
449 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ | |
450 u8 opcode = pOp->opcode; | |
451 | |
452 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing | |
453 ** cases from this switch! */ | |
454 switch( opcode ){ | |
455 case OP_Function: | |
456 case OP_AggStep: { | |
457 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; | |
458 break; | |
459 } | |
460 case OP_Transaction: { | |
461 if( pOp->p2!=0 ) p->readOnly = 0; | |
462 /* fall thru */ | |
463 } | |
464 case OP_AutoCommit: | |
465 case OP_Savepoint: { | |
466 p->bIsReader = 1; | |
467 break; | |
468 } | |
469 #ifndef SQLITE_OMIT_WAL | |
470 case OP_Checkpoint: | |
471 #endif | |
472 case OP_Vacuum: | |
473 case OP_JournalMode: { | |
474 p->readOnly = 0; | |
475 p->bIsReader = 1; | |
476 break; | |
477 } | |
478 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
479 case OP_VUpdate: { | |
480 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; | |
481 break; | |
482 } | |
483 case OP_VFilter: { | |
484 int n; | |
485 assert( p->nOp - i >= 3 ); | |
486 assert( pOp[-1].opcode==OP_Integer ); | |
487 n = pOp[-1].p1; | |
488 if( n>nMaxArgs ) nMaxArgs = n; | |
489 break; | |
490 } | |
491 #endif | |
492 case OP_Next: | |
493 case OP_NextIfOpen: | |
494 case OP_SorterNext: { | |
495 pOp->p4.xAdvance = sqlite3BtreeNext; | |
496 pOp->p4type = P4_ADVANCE; | |
497 break; | |
498 } | |
499 case OP_Prev: | |
500 case OP_PrevIfOpen: { | |
501 pOp->p4.xAdvance = sqlite3BtreePrevious; | |
502 pOp->p4type = P4_ADVANCE; | |
503 break; | |
504 } | |
505 } | |
506 | |
507 pOp->opflags = sqlite3OpcodeProperty[opcode]; | |
508 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ | |
509 assert( -1-pOp->p2<pParse->nLabel ); | |
510 pOp->p2 = aLabel[-1-pOp->p2]; | |
511 } | |
512 } | |
513 sqlite3DbFree(p->db, pParse->aLabel); | |
514 pParse->aLabel = 0; | |
515 pParse->nLabel = 0; | |
516 *pMaxFuncArgs = nMaxArgs; | |
517 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); | |
518 } | |
519 | |
520 /* | |
521 ** Return the address of the next instruction to be inserted. | |
522 */ | |
523 int sqlite3VdbeCurrentAddr(Vdbe *p){ | |
524 assert( p->magic==VDBE_MAGIC_INIT ); | |
525 return p->nOp; | |
526 } | |
527 | |
528 /* | |
529 ** This function returns a pointer to the array of opcodes associated with | |
530 ** the Vdbe passed as the first argument. It is the callers responsibility | |
531 ** to arrange for the returned array to be eventually freed using the | |
532 ** vdbeFreeOpArray() function. | |
533 ** | |
534 ** Before returning, *pnOp is set to the number of entries in the returned | |
535 ** array. Also, *pnMaxArg is set to the larger of its current value and | |
536 ** the number of entries in the Vdbe.apArg[] array required to execute the | |
537 ** returned program. | |
538 */ | |
539 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ | |
540 VdbeOp *aOp = p->aOp; | |
541 assert( aOp && !p->db->mallocFailed ); | |
542 | |
543 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ | |
544 assert( DbMaskAllZero(p->btreeMask) ); | |
545 | |
546 resolveP2Values(p, pnMaxArg); | |
547 *pnOp = p->nOp; | |
548 p->aOp = 0; | |
549 return aOp; | |
550 } | |
551 | |
552 /* | |
553 ** Add a whole list of operations to the operation stack. Return the | |
554 ** address of the first operation added. | |
555 */ | |
556 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ | |
557 int addr; | |
558 assert( p->magic==VDBE_MAGIC_INIT ); | |
559 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ | |
560 return 0; | |
561 } | |
562 addr = p->nOp; | |
563 if( ALWAYS(nOp>0) ){ | |
564 int i; | |
565 VdbeOpList const *pIn = aOp; | |
566 for(i=0; i<nOp; i++, pIn++){ | |
567 int p2 = pIn->p2; | |
568 VdbeOp *pOut = &p->aOp[i+addr]; | |
569 pOut->opcode = pIn->opcode; | |
570 pOut->p1 = pIn->p1; | |
571 if( p2<0 ){ | |
572 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); | |
573 pOut->p2 = addr + ADDR(p2); | |
574 }else{ | |
575 pOut->p2 = p2; | |
576 } | |
577 pOut->p3 = pIn->p3; | |
578 pOut->p4type = P4_NOTUSED; | |
579 pOut->p4.p = 0; | |
580 pOut->p5 = 0; | |
581 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
582 pOut->zComment = 0; | |
583 #endif | |
584 #ifdef SQLITE_VDBE_COVERAGE | |
585 pOut->iSrcLine = iLineno+i; | |
586 #else | |
587 (void)iLineno; | |
588 #endif | |
589 #ifdef SQLITE_DEBUG | |
590 if( p->db->flags & SQLITE_VdbeAddopTrace ){ | |
591 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); | |
592 } | |
593 #endif | |
594 } | |
595 p->nOp += nOp; | |
596 } | |
597 return addr; | |
598 } | |
599 | |
600 /* | |
601 ** Change the value of the P1 operand for a specific instruction. | |
602 ** This routine is useful when a large program is loaded from a | |
603 ** static array using sqlite3VdbeAddOpList but we want to make a | |
604 ** few minor changes to the program. | |
605 */ | |
606 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ | |
607 assert( p!=0 ); | |
608 if( ((u32)p->nOp)>addr ){ | |
609 p->aOp[addr].p1 = val; | |
610 } | |
611 } | |
612 | |
613 /* | |
614 ** Change the value of the P2 operand for a specific instruction. | |
615 ** This routine is useful for setting a jump destination. | |
616 */ | |
617 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ | |
618 assert( p!=0 ); | |
619 if( ((u32)p->nOp)>addr ){ | |
620 p->aOp[addr].p2 = val; | |
621 } | |
622 } | |
623 | |
624 /* | |
625 ** Change the value of the P3 operand for a specific instruction. | |
626 */ | |
627 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ | |
628 assert( p!=0 ); | |
629 if( ((u32)p->nOp)>addr ){ | |
630 p->aOp[addr].p3 = val; | |
631 } | |
632 } | |
633 | |
634 /* | |
635 ** Change the value of the P5 operand for the most recently | |
636 ** added operation. | |
637 */ | |
638 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ | |
639 assert( p!=0 ); | |
640 if( p->aOp ){ | |
641 assert( p->nOp>0 ); | |
642 p->aOp[p->nOp-1].p5 = val; | |
643 } | |
644 } | |
645 | |
646 /* | |
647 ** Change the P2 operand of instruction addr so that it points to | |
648 ** the address of the next instruction to be coded. | |
649 */ | |
650 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ | |
651 sqlite3VdbeChangeP2(p, addr, p->nOp); | |
652 p->pParse->iFixedOp = p->nOp - 1; | |
653 } | |
654 | |
655 | |
656 /* | |
657 ** If the input FuncDef structure is ephemeral, then free it. If | |
658 ** the FuncDef is not ephermal, then do nothing. | |
659 */ | |
660 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ | |
661 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ | |
662 sqlite3DbFree(db, pDef); | |
663 } | |
664 } | |
665 | |
666 static void vdbeFreeOpArray(sqlite3 *, Op *, int); | |
667 | |
668 /* | |
669 ** Delete a P4 value if necessary. | |
670 */ | |
671 static void freeP4(sqlite3 *db, int p4type, void *p4){ | |
672 if( p4 ){ | |
673 assert( db ); | |
674 switch( p4type ){ | |
675 case P4_REAL: | |
676 case P4_INT64: | |
677 case P4_DYNAMIC: | |
678 case P4_INTARRAY: { | |
679 sqlite3DbFree(db, p4); | |
680 break; | |
681 } | |
682 case P4_KEYINFO: { | |
683 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); | |
684 break; | |
685 } | |
686 case P4_MPRINTF: { | |
687 if( db->pnBytesFreed==0 ) sqlite3_free(p4); | |
688 break; | |
689 } | |
690 case P4_FUNCDEF: { | |
691 freeEphemeralFunction(db, (FuncDef*)p4); | |
692 break; | |
693 } | |
694 case P4_MEM: { | |
695 if( db->pnBytesFreed==0 ){ | |
696 sqlite3ValueFree((sqlite3_value*)p4); | |
697 }else{ | |
698 Mem *p = (Mem*)p4; | |
699 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); | |
700 sqlite3DbFree(db, p); | |
701 } | |
702 break; | |
703 } | |
704 case P4_VTAB : { | |
705 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); | |
706 break; | |
707 } | |
708 } | |
709 } | |
710 } | |
711 | |
712 /* | |
713 ** Free the space allocated for aOp and any p4 values allocated for the | |
714 ** opcodes contained within. If aOp is not NULL it is assumed to contain | |
715 ** nOp entries. | |
716 */ | |
717 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ | |
718 if( aOp ){ | |
719 Op *pOp; | |
720 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ | |
721 freeP4(db, pOp->p4type, pOp->p4.p); | |
722 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
723 sqlite3DbFree(db, pOp->zComment); | |
724 #endif | |
725 } | |
726 } | |
727 sqlite3DbFree(db, aOp); | |
728 } | |
729 | |
730 /* | |
731 ** Link the SubProgram object passed as the second argument into the linked | |
732 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program | |
733 ** objects when the VM is no longer required. | |
734 */ | |
735 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ | |
736 p->pNext = pVdbe->pProgram; | |
737 pVdbe->pProgram = p; | |
738 } | |
739 | |
740 /* | |
741 ** Change the opcode at addr into OP_Noop | |
742 */ | |
743 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ | |
744 if( addr<p->nOp ){ | |
745 VdbeOp *pOp = &p->aOp[addr]; | |
746 sqlite3 *db = p->db; | |
747 freeP4(db, pOp->p4type, pOp->p4.p); | |
748 memset(pOp, 0, sizeof(pOp[0])); | |
749 pOp->opcode = OP_Noop; | |
750 if( addr==p->nOp-1 ) p->nOp--; | |
751 } | |
752 } | |
753 | |
754 /* | |
755 ** If the last opcode is "op" and it is not a jump destination, | |
756 ** then remove it. Return true if and only if an opcode was removed. | |
757 */ | |
758 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ | |
759 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ | |
760 sqlite3VdbeChangeToNoop(p, p->nOp-1); | |
761 return 1; | |
762 }else{ | |
763 return 0; | |
764 } | |
765 } | |
766 | |
767 /* | |
768 ** Change the value of the P4 operand for a specific instruction. | |
769 ** This routine is useful when a large program is loaded from a | |
770 ** static array using sqlite3VdbeAddOpList but we want to make a | |
771 ** few minor changes to the program. | |
772 ** | |
773 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of | |
774 ** the string is made into memory obtained from sqlite3_malloc(). | |
775 ** A value of n==0 means copy bytes of zP4 up to and including the | |
776 ** first null byte. If n>0 then copy n+1 bytes of zP4. | |
777 ** | |
778 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points | |
779 ** to a string or structure that is guaranteed to exist for the lifetime of | |
780 ** the Vdbe. In these cases we can just copy the pointer. | |
781 ** | |
782 ** If addr<0 then change P4 on the most recently inserted instruction. | |
783 */ | |
784 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ | |
785 Op *pOp; | |
786 sqlite3 *db; | |
787 assert( p!=0 ); | |
788 db = p->db; | |
789 assert( p->magic==VDBE_MAGIC_INIT ); | |
790 if( p->aOp==0 || db->mallocFailed ){ | |
791 if( n!=P4_VTAB ){ | |
792 freeP4(db, n, (void*)*(char**)&zP4); | |
793 } | |
794 return; | |
795 } | |
796 assert( p->nOp>0 ); | |
797 assert( addr<p->nOp ); | |
798 if( addr<0 ){ | |
799 addr = p->nOp - 1; | |
800 } | |
801 pOp = &p->aOp[addr]; | |
802 assert( pOp->p4type==P4_NOTUSED | |
803 || pOp->p4type==P4_INT32 | |
804 || pOp->p4type==P4_KEYINFO ); | |
805 freeP4(db, pOp->p4type, pOp->p4.p); | |
806 pOp->p4.p = 0; | |
807 if( n==P4_INT32 ){ | |
808 /* Note: this cast is safe, because the origin data point was an int | |
809 ** that was cast to a (const char *). */ | |
810 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); | |
811 pOp->p4type = P4_INT32; | |
812 }else if( zP4==0 ){ | |
813 pOp->p4.p = 0; | |
814 pOp->p4type = P4_NOTUSED; | |
815 }else if( n==P4_KEYINFO ){ | |
816 pOp->p4.p = (void*)zP4; | |
817 pOp->p4type = P4_KEYINFO; | |
818 }else if( n==P4_VTAB ){ | |
819 pOp->p4.p = (void*)zP4; | |
820 pOp->p4type = P4_VTAB; | |
821 sqlite3VtabLock((VTable *)zP4); | |
822 assert( ((VTable *)zP4)->db==p->db ); | |
823 }else if( n<0 ){ | |
824 pOp->p4.p = (void*)zP4; | |
825 pOp->p4type = (signed char)n; | |
826 }else{ | |
827 if( n==0 ) n = sqlite3Strlen30(zP4); | |
828 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); | |
829 pOp->p4type = P4_DYNAMIC; | |
830 } | |
831 } | |
832 | |
833 /* | |
834 ** Set the P4 on the most recently added opcode to the KeyInfo for the | |
835 ** index given. | |
836 */ | |
837 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ | |
838 Vdbe *v = pParse->pVdbe; | |
839 assert( v!=0 ); | |
840 assert( pIdx!=0 ); | |
841 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), | |
842 P4_KEYINFO); | |
843 } | |
844 | |
845 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
846 /* | |
847 ** Change the comment on the most recently coded instruction. Or | |
848 ** insert a No-op and add the comment to that new instruction. This | |
849 ** makes the code easier to read during debugging. None of this happens | |
850 ** in a production build. | |
851 */ | |
852 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ | |
853 assert( p->nOp>0 || p->aOp==0 ); | |
854 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); | |
855 if( p->nOp ){ | |
856 assert( p->aOp ); | |
857 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); | |
858 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); | |
859 } | |
860 } | |
861 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ | |
862 va_list ap; | |
863 if( p ){ | |
864 va_start(ap, zFormat); | |
865 vdbeVComment(p, zFormat, ap); | |
866 va_end(ap); | |
867 } | |
868 } | |
869 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ | |
870 va_list ap; | |
871 if( p ){ | |
872 sqlite3VdbeAddOp0(p, OP_Noop); | |
873 va_start(ap, zFormat); | |
874 vdbeVComment(p, zFormat, ap); | |
875 va_end(ap); | |
876 } | |
877 } | |
878 #endif /* NDEBUG */ | |
879 | |
880 #ifdef SQLITE_VDBE_COVERAGE | |
881 /* | |
882 ** Set the value if the iSrcLine field for the previously coded instruction. | |
883 */ | |
884 void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ | |
885 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; | |
886 } | |
887 #endif /* SQLITE_VDBE_COVERAGE */ | |
888 | |
889 /* | |
890 ** Return the opcode for a given address. If the address is -1, then | |
891 ** return the most recently inserted opcode. | |
892 ** | |
893 ** If a memory allocation error has occurred prior to the calling of this | |
894 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode | |
895 ** is readable but not writable, though it is cast to a writable value. | |
896 ** The return of a dummy opcode allows the call to continue functioning | |
897 ** after an OOM fault without having to check to see if the return from | |
898 ** this routine is a valid pointer. But because the dummy.opcode is 0, | |
899 ** dummy will never be written to. This is verified by code inspection and | |
900 ** by running with Valgrind. | |
901 */ | |
902 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ | |
903 /* C89 specifies that the constant "dummy" will be initialized to all | |
904 ** zeros, which is correct. MSVC generates a warning, nevertheless. */ | |
905 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ | |
906 assert( p->magic==VDBE_MAGIC_INIT ); | |
907 if( addr<0 ){ | |
908 addr = p->nOp - 1; | |
909 } | |
910 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); | |
911 if( p->db->mallocFailed ){ | |
912 return (VdbeOp*)&dummy; | |
913 }else{ | |
914 return &p->aOp[addr]; | |
915 } | |
916 } | |
917 | |
918 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) | |
919 /* | |
920 ** Return an integer value for one of the parameters to the opcode pOp | |
921 ** determined by character c. | |
922 */ | |
923 static int translateP(char c, const Op *pOp){ | |
924 if( c=='1' ) return pOp->p1; | |
925 if( c=='2' ) return pOp->p2; | |
926 if( c=='3' ) return pOp->p3; | |
927 if( c=='4' ) return pOp->p4.i; | |
928 return pOp->p5; | |
929 } | |
930 | |
931 /* | |
932 ** Compute a string for the "comment" field of a VDBE opcode listing. | |
933 ** | |
934 ** The Synopsis: field in comments in the vdbe.c source file gets converted | |
935 ** to an extra string that is appended to the sqlite3OpcodeName(). In the | |
936 ** absence of other comments, this synopsis becomes the comment on the opcode. | |
937 ** Some translation occurs: | |
938 ** | |
939 ** "PX" -> "r[X]" | |
940 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 | |
941 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 | |
942 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x | |
943 */ | |
944 static int displayComment( | |
945 const Op *pOp, /* The opcode to be commented */ | |
946 const char *zP4, /* Previously obtained value for P4 */ | |
947 char *zTemp, /* Write result here */ | |
948 int nTemp /* Space available in zTemp[] */ | |
949 ){ | |
950 const char *zOpName; | |
951 const char *zSynopsis; | |
952 int nOpName; | |
953 int ii, jj; | |
954 zOpName = sqlite3OpcodeName(pOp->opcode); | |
955 nOpName = sqlite3Strlen30(zOpName); | |
956 if( zOpName[nOpName+1] ){ | |
957 int seenCom = 0; | |
958 char c; | |
959 zSynopsis = zOpName += nOpName + 1; | |
960 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ | |
961 if( c=='P' ){ | |
962 c = zSynopsis[++ii]; | |
963 if( c=='4' ){ | |
964 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); | |
965 }else if( c=='X' ){ | |
966 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); | |
967 seenCom = 1; | |
968 }else{ | |
969 int v1 = translateP(c, pOp); | |
970 int v2; | |
971 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); | |
972 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ | |
973 ii += 3; | |
974 jj += sqlite3Strlen30(zTemp+jj); | |
975 v2 = translateP(zSynopsis[ii], pOp); | |
976 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ | |
977 ii += 2; | |
978 v2++; | |
979 } | |
980 if( v2>1 ){ | |
981 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); | |
982 } | |
983 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ | |
984 ii += 4; | |
985 } | |
986 } | |
987 jj += sqlite3Strlen30(zTemp+jj); | |
988 }else{ | |
989 zTemp[jj++] = c; | |
990 } | |
991 } | |
992 if( !seenCom && jj<nTemp-5 && pOp->zComment ){ | |
993 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); | |
994 jj += sqlite3Strlen30(zTemp+jj); | |
995 } | |
996 if( jj<nTemp ) zTemp[jj] = 0; | |
997 }else if( pOp->zComment ){ | |
998 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); | |
999 jj = sqlite3Strlen30(zTemp); | |
1000 }else{ | |
1001 zTemp[0] = 0; | |
1002 jj = 0; | |
1003 } | |
1004 return jj; | |
1005 } | |
1006 #endif /* SQLITE_DEBUG */ | |
1007 | |
1008 | |
1009 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ | |
1010 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | |
1011 /* | |
1012 ** Compute a string that describes the P4 parameter for an opcode. | |
1013 ** Use zTemp for any required temporary buffer space. | |
1014 */ | |
1015 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ | |
1016 char *zP4 = zTemp; | |
1017 assert( nTemp>=20 ); | |
1018 switch( pOp->p4type ){ | |
1019 case P4_KEYINFO: { | |
1020 int i, j; | |
1021 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; | |
1022 assert( pKeyInfo->aSortOrder!=0 ); | |
1023 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); | |
1024 i = sqlite3Strlen30(zTemp); | |
1025 for(j=0; j<pKeyInfo->nField; j++){ | |
1026 CollSeq *pColl = pKeyInfo->aColl[j]; | |
1027 const char *zColl = pColl ? pColl->zName : "nil"; | |
1028 int n = sqlite3Strlen30(zColl); | |
1029 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ | |
1030 zColl = "B"; | |
1031 n = 1; | |
1032 } | |
1033 if( i+n>nTemp-6 ){ | |
1034 memcpy(&zTemp[i],",...",4); | |
1035 break; | |
1036 } | |
1037 zTemp[i++] = ','; | |
1038 if( pKeyInfo->aSortOrder[j] ){ | |
1039 zTemp[i++] = '-'; | |
1040 } | |
1041 memcpy(&zTemp[i], zColl, n+1); | |
1042 i += n; | |
1043 } | |
1044 zTemp[i++] = ')'; | |
1045 zTemp[i] = 0; | |
1046 assert( i<nTemp ); | |
1047 break; | |
1048 } | |
1049 case P4_COLLSEQ: { | |
1050 CollSeq *pColl = pOp->p4.pColl; | |
1051 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); | |
1052 break; | |
1053 } | |
1054 case P4_FUNCDEF: { | |
1055 FuncDef *pDef = pOp->p4.pFunc; | |
1056 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | |
1057 break; | |
1058 } | |
1059 case P4_INT64: { | |
1060 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); | |
1061 break; | |
1062 } | |
1063 case P4_INT32: { | |
1064 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); | |
1065 break; | |
1066 } | |
1067 case P4_REAL: { | |
1068 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); | |
1069 break; | |
1070 } | |
1071 case P4_MEM: { | |
1072 Mem *pMem = pOp->p4.pMem; | |
1073 if( pMem->flags & MEM_Str ){ | |
1074 zP4 = pMem->z; | |
1075 }else if( pMem->flags & MEM_Int ){ | |
1076 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); | |
1077 }else if( pMem->flags & MEM_Real ){ | |
1078 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r); | |
1079 }else if( pMem->flags & MEM_Null ){ | |
1080 sqlite3_snprintf(nTemp, zTemp, "NULL"); | |
1081 }else{ | |
1082 assert( pMem->flags & MEM_Blob ); | |
1083 zP4 = "(blob)"; | |
1084 } | |
1085 break; | |
1086 } | |
1087 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
1088 case P4_VTAB: { | |
1089 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; | |
1090 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); | |
1091 break; | |
1092 } | |
1093 #endif | |
1094 case P4_INTARRAY: { | |
1095 sqlite3_snprintf(nTemp, zTemp, "intarray"); | |
1096 break; | |
1097 } | |
1098 case P4_SUBPROGRAM: { | |
1099 sqlite3_snprintf(nTemp, zTemp, "program"); | |
1100 break; | |
1101 } | |
1102 case P4_ADVANCE: { | |
1103 zTemp[0] = 0; | |
1104 break; | |
1105 } | |
1106 default: { | |
1107 zP4 = pOp->p4.z; | |
1108 if( zP4==0 ){ | |
1109 zP4 = zTemp; | |
1110 zTemp[0] = 0; | |
1111 } | |
1112 } | |
1113 } | |
1114 assert( zP4!=0 ); | |
1115 return zP4; | |
1116 } | |
1117 #endif | |
1118 | |
1119 /* | |
1120 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. | |
1121 ** | |
1122 ** The prepared statements need to know in advance the complete set of | |
1123 ** attached databases that will be use. A mask of these databases | |
1124 ** is maintained in p->btreeMask. The p->lockMask value is the subset of | |
1125 ** p->btreeMask of databases that will require a lock. | |
1126 */ | |
1127 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ | |
1128 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); | |
1129 assert( i<(int)sizeof(p->btreeMask)*8 ); | |
1130 DbMaskSet(p->btreeMask, i); | |
1131 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ | |
1132 DbMaskSet(p->lockMask, i); | |
1133 } | |
1134 } | |
1135 | |
1136 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | |
1137 /* | |
1138 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, | |
1139 ** this routine obtains the mutex associated with each BtShared structure | |
1140 ** that may be accessed by the VM passed as an argument. In doing so it also | |
1141 ** sets the BtShared.db member of each of the BtShared structures, ensuring | |
1142 ** that the correct busy-handler callback is invoked if required. | |
1143 ** | |
1144 ** If SQLite is not threadsafe but does support shared-cache mode, then | |
1145 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables | |
1146 ** of all of BtShared structures accessible via the database handle | |
1147 ** associated with the VM. | |
1148 ** | |
1149 ** If SQLite is not threadsafe and does not support shared-cache mode, this | |
1150 ** function is a no-op. | |
1151 ** | |
1152 ** The p->btreeMask field is a bitmask of all btrees that the prepared | |
1153 ** statement p will ever use. Let N be the number of bits in p->btreeMask | |
1154 ** corresponding to btrees that use shared cache. Then the runtime of | |
1155 ** this routine is N*N. But as N is rarely more than 1, this should not | |
1156 ** be a problem. | |
1157 */ | |
1158 void sqlite3VdbeEnter(Vdbe *p){ | |
1159 int i; | |
1160 sqlite3 *db; | |
1161 Db *aDb; | |
1162 int nDb; | |
1163 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ | |
1164 db = p->db; | |
1165 aDb = db->aDb; | |
1166 nDb = db->nDb; | |
1167 for(i=0; i<nDb; i++){ | |
1168 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ | |
1169 sqlite3BtreeEnter(aDb[i].pBt); | |
1170 } | |
1171 } | |
1172 } | |
1173 #endif | |
1174 | |
1175 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | |
1176 /* | |
1177 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). | |
1178 */ | |
1179 void sqlite3VdbeLeave(Vdbe *p){ | |
1180 int i; | |
1181 sqlite3 *db; | |
1182 Db *aDb; | |
1183 int nDb; | |
1184 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ | |
1185 db = p->db; | |
1186 aDb = db->aDb; | |
1187 nDb = db->nDb; | |
1188 for(i=0; i<nDb; i++){ | |
1189 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ | |
1190 sqlite3BtreeLeave(aDb[i].pBt); | |
1191 } | |
1192 } | |
1193 } | |
1194 #endif | |
1195 | |
1196 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | |
1197 /* | |
1198 ** Print a single opcode. This routine is used for debugging only. | |
1199 */ | |
1200 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ | |
1201 char *zP4; | |
1202 char zPtr[50]; | |
1203 char zCom[100]; | |
1204 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; | |
1205 if( pOut==0 ) pOut = stdout; | |
1206 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); | |
1207 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
1208 displayComment(pOp, zP4, zCom, sizeof(zCom)); | |
1209 #else | |
1210 zCom[0] = 0; | |
1211 #endif | |
1212 /* NB: The sqlite3OpcodeName() function is implemented by code created | |
1213 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the | |
1214 ** information from the vdbe.c source text */ | |
1215 fprintf(pOut, zFormat1, pc, | |
1216 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, | |
1217 zCom | |
1218 ); | |
1219 fflush(pOut); | |
1220 } | |
1221 #endif | |
1222 | |
1223 /* | |
1224 ** Release an array of N Mem elements | |
1225 */ | |
1226 static void releaseMemArray(Mem *p, int N){ | |
1227 if( p && N ){ | |
1228 Mem *pEnd = &p[N]; | |
1229 sqlite3 *db = p->db; | |
1230 u8 malloc_failed = db->mallocFailed; | |
1231 if( db->pnBytesFreed ){ | |
1232 do{ | |
1233 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); | |
1234 }while( (++p)<pEnd ); | |
1235 return; | |
1236 } | |
1237 do{ | |
1238 assert( (&p[1])==pEnd || p[0].db==p[1].db ); | |
1239 assert( sqlite3VdbeCheckMemInvariants(p) ); | |
1240 | |
1241 /* This block is really an inlined version of sqlite3VdbeMemRelease() | |
1242 ** that takes advantage of the fact that the memory cell value is | |
1243 ** being set to NULL after releasing any dynamic resources. | |
1244 ** | |
1245 ** The justification for duplicating code is that according to | |
1246 ** callgrind, this causes a certain test case to hit the CPU 4.7 | |
1247 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if | |
1248 ** sqlite3MemRelease() were called from here. With -O2, this jumps | |
1249 ** to 6.6 percent. The test case is inserting 1000 rows into a table | |
1250 ** with no indexes using a single prepared INSERT statement, bind() | |
1251 ** and reset(). Inserts are grouped into a transaction. | |
1252 */ | |
1253 testcase( p->flags & MEM_Agg ); | |
1254 testcase( p->flags & MEM_Dyn ); | |
1255 testcase( p->flags & MEM_Frame ); | |
1256 testcase( p->flags & MEM_RowSet ); | |
1257 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ | |
1258 sqlite3VdbeMemRelease(p); | |
1259 }else if( p->szMalloc ){ | |
1260 sqlite3DbFree(db, p->zMalloc); | |
1261 p->szMalloc = 0; | |
1262 } | |
1263 | |
1264 p->flags = MEM_Undefined; | |
1265 }while( (++p)<pEnd ); | |
1266 db->mallocFailed = malloc_failed; | |
1267 } | |
1268 } | |
1269 | |
1270 /* | |
1271 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are | |
1272 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). | |
1273 */ | |
1274 void sqlite3VdbeFrameDelete(VdbeFrame *p){ | |
1275 int i; | |
1276 Mem *aMem = VdbeFrameMem(p); | |
1277 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; | |
1278 for(i=0; i<p->nChildCsr; i++){ | |
1279 sqlite3VdbeFreeCursor(p->v, apCsr[i]); | |
1280 } | |
1281 releaseMemArray(aMem, p->nChildMem); | |
1282 sqlite3DbFree(p->v->db, p); | |
1283 } | |
1284 | |
1285 #ifndef SQLITE_OMIT_EXPLAIN | |
1286 /* | |
1287 ** Give a listing of the program in the virtual machine. | |
1288 ** | |
1289 ** The interface is the same as sqlite3VdbeExec(). But instead of | |
1290 ** running the code, it invokes the callback once for each instruction. | |
1291 ** This feature is used to implement "EXPLAIN". | |
1292 ** | |
1293 ** When p->explain==1, each instruction is listed. When | |
1294 ** p->explain==2, only OP_Explain instructions are listed and these | |
1295 ** are shown in a different format. p->explain==2 is used to implement | |
1296 ** EXPLAIN QUERY PLAN. | |
1297 ** | |
1298 ** When p->explain==1, first the main program is listed, then each of | |
1299 ** the trigger subprograms are listed one by one. | |
1300 */ | |
1301 int sqlite3VdbeList( | |
1302 Vdbe *p /* The VDBE */ | |
1303 ){ | |
1304 int nRow; /* Stop when row count reaches this */ | |
1305 int nSub = 0; /* Number of sub-vdbes seen so far */ | |
1306 SubProgram **apSub = 0; /* Array of sub-vdbes */ | |
1307 Mem *pSub = 0; /* Memory cell hold array of subprogs */ | |
1308 sqlite3 *db = p->db; /* The database connection */ | |
1309 int i; /* Loop counter */ | |
1310 int rc = SQLITE_OK; /* Return code */ | |
1311 Mem *pMem = &p->aMem[1]; /* First Mem of result set */ | |
1312 | |
1313 assert( p->explain ); | |
1314 assert( p->magic==VDBE_MAGIC_RUN ); | |
1315 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); | |
1316 | |
1317 /* Even though this opcode does not use dynamic strings for | |
1318 ** the result, result columns may become dynamic if the user calls | |
1319 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. | |
1320 */ | |
1321 releaseMemArray(pMem, 8); | |
1322 p->pResultSet = 0; | |
1323 | |
1324 if( p->rc==SQLITE_NOMEM ){ | |
1325 /* This happens if a malloc() inside a call to sqlite3_column_text() or | |
1326 ** sqlite3_column_text16() failed. */ | |
1327 db->mallocFailed = 1; | |
1328 return SQLITE_ERROR; | |
1329 } | |
1330 | |
1331 /* When the number of output rows reaches nRow, that means the | |
1332 ** listing has finished and sqlite3_step() should return SQLITE_DONE. | |
1333 ** nRow is the sum of the number of rows in the main program, plus | |
1334 ** the sum of the number of rows in all trigger subprograms encountered | |
1335 ** so far. The nRow value will increase as new trigger subprograms are | |
1336 ** encountered, but p->pc will eventually catch up to nRow. | |
1337 */ | |
1338 nRow = p->nOp; | |
1339 if( p->explain==1 ){ | |
1340 /* The first 8 memory cells are used for the result set. So we will | |
1341 ** commandeer the 9th cell to use as storage for an array of pointers | |
1342 ** to trigger subprograms. The VDBE is guaranteed to have at least 9 | |
1343 ** cells. */ | |
1344 assert( p->nMem>9 ); | |
1345 pSub = &p->aMem[9]; | |
1346 if( pSub->flags&MEM_Blob ){ | |
1347 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is | |
1348 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ | |
1349 nSub = pSub->n/sizeof(Vdbe*); | |
1350 apSub = (SubProgram **)pSub->z; | |
1351 } | |
1352 for(i=0; i<nSub; i++){ | |
1353 nRow += apSub[i]->nOp; | |
1354 } | |
1355 } | |
1356 | |
1357 do{ | |
1358 i = p->pc++; | |
1359 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); | |
1360 if( i>=nRow ){ | |
1361 p->rc = SQLITE_OK; | |
1362 rc = SQLITE_DONE; | |
1363 }else if( db->u1.isInterrupted ){ | |
1364 p->rc = SQLITE_INTERRUPT; | |
1365 rc = SQLITE_ERROR; | |
1366 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); | |
1367 }else{ | |
1368 char *zP4; | |
1369 Op *pOp; | |
1370 if( i<p->nOp ){ | |
1371 /* The output line number is small enough that we are still in the | |
1372 ** main program. */ | |
1373 pOp = &p->aOp[i]; | |
1374 }else{ | |
1375 /* We are currently listing subprograms. Figure out which one and | |
1376 ** pick up the appropriate opcode. */ | |
1377 int j; | |
1378 i -= p->nOp; | |
1379 for(j=0; i>=apSub[j]->nOp; j++){ | |
1380 i -= apSub[j]->nOp; | |
1381 } | |
1382 pOp = &apSub[j]->aOp[i]; | |
1383 } | |
1384 if( p->explain==1 ){ | |
1385 pMem->flags = MEM_Int; | |
1386 pMem->u.i = i; /* Program counter */ | |
1387 pMem++; | |
1388 | |
1389 pMem->flags = MEM_Static|MEM_Str|MEM_Term; | |
1390 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ | |
1391 assert( pMem->z!=0 ); | |
1392 pMem->n = sqlite3Strlen30(pMem->z); | |
1393 pMem->enc = SQLITE_UTF8; | |
1394 pMem++; | |
1395 | |
1396 /* When an OP_Program opcode is encounter (the only opcode that has | |
1397 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms | |
1398 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram | |
1399 ** has not already been seen. | |
1400 */ | |
1401 if( pOp->p4type==P4_SUBPROGRAM ){ | |
1402 int nByte = (nSub+1)*sizeof(SubProgram*); | |
1403 int j; | |
1404 for(j=0; j<nSub; j++){ | |
1405 if( apSub[j]==pOp->p4.pProgram ) break; | |
1406 } | |
1407 if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){ | |
1408 apSub = (SubProgram **)pSub->z; | |
1409 apSub[nSub++] = pOp->p4.pProgram; | |
1410 pSub->flags |= MEM_Blob; | |
1411 pSub->n = nSub*sizeof(SubProgram*); | |
1412 } | |
1413 } | |
1414 } | |
1415 | |
1416 pMem->flags = MEM_Int; | |
1417 pMem->u.i = pOp->p1; /* P1 */ | |
1418 pMem++; | |
1419 | |
1420 pMem->flags = MEM_Int; | |
1421 pMem->u.i = pOp->p2; /* P2 */ | |
1422 pMem++; | |
1423 | |
1424 pMem->flags = MEM_Int; | |
1425 pMem->u.i = pOp->p3; /* P3 */ | |
1426 pMem++; | |
1427 | |
1428 if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */ | |
1429 assert( p->db->mallocFailed ); | |
1430 return SQLITE_ERROR; | |
1431 } | |
1432 pMem->flags = MEM_Str|MEM_Term; | |
1433 zP4 = displayP4(pOp, pMem->z, 32); | |
1434 if( zP4!=pMem->z ){ | |
1435 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); | |
1436 }else{ | |
1437 assert( pMem->z!=0 ); | |
1438 pMem->n = sqlite3Strlen30(pMem->z); | |
1439 pMem->enc = SQLITE_UTF8; | |
1440 } | |
1441 pMem++; | |
1442 | |
1443 if( p->explain==1 ){ | |
1444 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ | |
1445 assert( p->db->mallocFailed ); | |
1446 return SQLITE_ERROR; | |
1447 } | |
1448 pMem->flags = MEM_Str|MEM_Term; | |
1449 pMem->n = 2; | |
1450 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ | |
1451 pMem->enc = SQLITE_UTF8; | |
1452 pMem++; | |
1453 | |
1454 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | |
1455 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ | |
1456 assert( p->db->mallocFailed ); | |
1457 return SQLITE_ERROR; | |
1458 } | |
1459 pMem->flags = MEM_Str|MEM_Term; | |
1460 pMem->n = displayComment(pOp, zP4, pMem->z, 500); | |
1461 pMem->enc = SQLITE_UTF8; | |
1462 #else | |
1463 pMem->flags = MEM_Null; /* Comment */ | |
1464 #endif | |
1465 } | |
1466 | |
1467 p->nResColumn = 8 - 4*(p->explain-1); | |
1468 p->pResultSet = &p->aMem[1]; | |
1469 p->rc = SQLITE_OK; | |
1470 rc = SQLITE_ROW; | |
1471 } | |
1472 return rc; | |
1473 } | |
1474 #endif /* SQLITE_OMIT_EXPLAIN */ | |
1475 | |
1476 #ifdef SQLITE_DEBUG | |
1477 /* | |
1478 ** Print the SQL that was used to generate a VDBE program. | |
1479 */ | |
1480 void sqlite3VdbePrintSql(Vdbe *p){ | |
1481 const char *z = 0; | |
1482 if( p->zSql ){ | |
1483 z = p->zSql; | |
1484 }else if( p->nOp>=1 ){ | |
1485 const VdbeOp *pOp = &p->aOp[0]; | |
1486 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ | |
1487 z = pOp->p4.z; | |
1488 while( sqlite3Isspace(*z) ) z++; | |
1489 } | |
1490 } | |
1491 if( z ) printf("SQL: [%s]\n", z); | |
1492 } | |
1493 #endif | |
1494 | |
1495 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) | |
1496 /* | |
1497 ** Print an IOTRACE message showing SQL content. | |
1498 */ | |
1499 void sqlite3VdbeIOTraceSql(Vdbe *p){ | |
1500 int nOp = p->nOp; | |
1501 VdbeOp *pOp; | |
1502 if( sqlite3IoTrace==0 ) return; | |
1503 if( nOp<1 ) return; | |
1504 pOp = &p->aOp[0]; | |
1505 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ | |
1506 int i, j; | |
1507 char z[1000]; | |
1508 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); | |
1509 for(i=0; sqlite3Isspace(z[i]); i++){} | |
1510 for(j=0; z[i]; i++){ | |
1511 if( sqlite3Isspace(z[i]) ){ | |
1512 if( z[i-1]!=' ' ){ | |
1513 z[j++] = ' '; | |
1514 } | |
1515 }else{ | |
1516 z[j++] = z[i]; | |
1517 } | |
1518 } | |
1519 z[j] = 0; | |
1520 sqlite3IoTrace("SQL %s\n", z); | |
1521 } | |
1522 } | |
1523 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ | |
1524 | |
1525 /* | |
1526 ** Allocate space from a fixed size buffer and return a pointer to | |
1527 ** that space. If insufficient space is available, return NULL. | |
1528 ** | |
1529 ** The pBuf parameter is the initial value of a pointer which will | |
1530 ** receive the new memory. pBuf is normally NULL. If pBuf is not | |
1531 ** NULL, it means that memory space has already been allocated and that | |
1532 ** this routine should not allocate any new memory. When pBuf is not | |
1533 ** NULL simply return pBuf. Only allocate new memory space when pBuf | |
1534 ** is NULL. | |
1535 ** | |
1536 ** nByte is the number of bytes of space needed. | |
1537 ** | |
1538 ** *ppFrom points to available space and pEnd points to the end of the | |
1539 ** available space. When space is allocated, *ppFrom is advanced past | |
1540 ** the end of the allocated space. | |
1541 ** | |
1542 ** *pnByte is a counter of the number of bytes of space that have failed | |
1543 ** to allocate. If there is insufficient space in *ppFrom to satisfy the | |
1544 ** request, then increment *pnByte by the amount of the request. | |
1545 */ | |
1546 static void *allocSpace( | |
1547 void *pBuf, /* Where return pointer will be stored */ | |
1548 int nByte, /* Number of bytes to allocate */ | |
1549 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */ | |
1550 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */ | |
1551 int *pnByte /* If allocation cannot be made, increment *pnByte */ | |
1552 ){ | |
1553 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) ); | |
1554 if( pBuf ) return pBuf; | |
1555 nByte = ROUND8(nByte); | |
1556 if( &(*ppFrom)[nByte] <= pEnd ){ | |
1557 pBuf = (void*)*ppFrom; | |
1558 *ppFrom += nByte; | |
1559 }else{ | |
1560 *pnByte += nByte; | |
1561 } | |
1562 return pBuf; | |
1563 } | |
1564 | |
1565 /* | |
1566 ** Rewind the VDBE back to the beginning in preparation for | |
1567 ** running it. | |
1568 */ | |
1569 void sqlite3VdbeRewind(Vdbe *p){ | |
1570 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) | |
1571 int i; | |
1572 #endif | |
1573 assert( p!=0 ); | |
1574 assert( p->magic==VDBE_MAGIC_INIT ); | |
1575 | |
1576 /* There should be at least one opcode. | |
1577 */ | |
1578 assert( p->nOp>0 ); | |
1579 | |
1580 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ | |
1581 p->magic = VDBE_MAGIC_RUN; | |
1582 | |
1583 #ifdef SQLITE_DEBUG | |
1584 for(i=1; i<p->nMem; i++){ | |
1585 assert( p->aMem[i].db==p->db ); | |
1586 } | |
1587 #endif | |
1588 p->pc = -1; | |
1589 p->rc = SQLITE_OK; | |
1590 p->errorAction = OE_Abort; | |
1591 p->magic = VDBE_MAGIC_RUN; | |
1592 p->nChange = 0; | |
1593 p->cacheCtr = 1; | |
1594 p->minWriteFileFormat = 255; | |
1595 p->iStatement = 0; | |
1596 p->nFkConstraint = 0; | |
1597 #ifdef VDBE_PROFILE | |
1598 for(i=0; i<p->nOp; i++){ | |
1599 p->aOp[i].cnt = 0; | |
1600 p->aOp[i].cycles = 0; | |
1601 } | |
1602 #endif | |
1603 } | |
1604 | |
1605 /* | |
1606 ** Prepare a virtual machine for execution for the first time after | |
1607 ** creating the virtual machine. This involves things such | |
1608 ** as allocating registers and initializing the program counter. | |
1609 ** After the VDBE has be prepped, it can be executed by one or more | |
1610 ** calls to sqlite3VdbeExec(). | |
1611 ** | |
1612 ** This function may be called exactly once on each virtual machine. | |
1613 ** After this routine is called the VM has been "packaged" and is ready | |
1614 ** to run. After this routine is called, further calls to | |
1615 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects | |
1616 ** the Vdbe from the Parse object that helped generate it so that the | |
1617 ** the Vdbe becomes an independent entity and the Parse object can be | |
1618 ** destroyed. | |
1619 ** | |
1620 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back | |
1621 ** to its initial state after it has been run. | |
1622 */ | |
1623 void sqlite3VdbeMakeReady( | |
1624 Vdbe *p, /* The VDBE */ | |
1625 Parse *pParse /* Parsing context */ | |
1626 ){ | |
1627 sqlite3 *db; /* The database connection */ | |
1628 int nVar; /* Number of parameters */ | |
1629 int nMem; /* Number of VM memory registers */ | |
1630 int nCursor; /* Number of cursors required */ | |
1631 int nArg; /* Number of arguments in subprograms */ | |
1632 int nOnce; /* Number of OP_Once instructions */ | |
1633 int n; /* Loop counter */ | |
1634 u8 *zCsr; /* Memory available for allocation */ | |
1635 u8 *zEnd; /* First byte past allocated memory */ | |
1636 int nByte; /* How much extra memory is needed */ | |
1637 | |
1638 assert( p!=0 ); | |
1639 assert( p->nOp>0 ); | |
1640 assert( pParse!=0 ); | |
1641 assert( p->magic==VDBE_MAGIC_INIT ); | |
1642 assert( pParse==p->pParse ); | |
1643 db = p->db; | |
1644 assert( db->mallocFailed==0 ); | |
1645 nVar = pParse->nVar; | |
1646 nMem = pParse->nMem; | |
1647 nCursor = pParse->nTab; | |
1648 nArg = pParse->nMaxArg; | |
1649 nOnce = pParse->nOnce; | |
1650 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ | |
1651 | |
1652 /* For each cursor required, also allocate a memory cell. Memory | |
1653 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by | |
1654 ** the vdbe program. Instead they are used to allocate space for | |
1655 ** VdbeCursor/BtCursor structures. The blob of memory associated with | |
1656 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) | |
1657 ** stores the blob of memory associated with cursor 1, etc. | |
1658 ** | |
1659 ** See also: allocateCursor(). | |
1660 */ | |
1661 nMem += nCursor; | |
1662 | |
1663 /* Allocate space for memory registers, SQL variables, VDBE cursors and | |
1664 ** an array to marshal SQL function arguments in. | |
1665 */ | |
1666 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */ | |
1667 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */ | |
1668 | |
1669 resolveP2Values(p, &nArg); | |
1670 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); | |
1671 if( pParse->explain && nMem<10 ){ | |
1672 nMem = 10; | |
1673 } | |
1674 memset(zCsr, 0, zEnd-zCsr); | |
1675 zCsr += (zCsr - (u8*)0)&7; | |
1676 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | |
1677 p->expired = 0; | |
1678 | |
1679 /* Memory for registers, parameters, cursor, etc, is allocated in two | |
1680 ** passes. On the first pass, we try to reuse unused space at the | |
1681 ** end of the opcode array. If we are unable to satisfy all memory | |
1682 ** requirements by reusing the opcode array tail, then the second | |
1683 ** pass will fill in the rest using a fresh allocation. | |
1684 ** | |
1685 ** This two-pass approach that reuses as much memory as possible from | |
1686 ** the leftover space at the end of the opcode array can significantly | |
1687 ** reduce the amount of memory held by a prepared statement. | |
1688 */ | |
1689 do { | |
1690 nByte = 0; | |
1691 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); | |
1692 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); | |
1693 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); | |
1694 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); | |
1695 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), | |
1696 &zCsr, zEnd, &nByte); | |
1697 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); | |
1698 if( nByte ){ | |
1699 p->pFree = sqlite3DbMallocZero(db, nByte); | |
1700 } | |
1701 zCsr = p->pFree; | |
1702 zEnd = &zCsr[nByte]; | |
1703 }while( nByte && !db->mallocFailed ); | |
1704 | |
1705 p->nCursor = nCursor; | |
1706 p->nOnceFlag = nOnce; | |
1707 if( p->aVar ){ | |
1708 p->nVar = (ynVar)nVar; | |
1709 for(n=0; n<nVar; n++){ | |
1710 p->aVar[n].flags = MEM_Null; | |
1711 p->aVar[n].db = db; | |
1712 } | |
1713 } | |
1714 if( p->azVar ){ | |
1715 p->nzVar = pParse->nzVar; | |
1716 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); | |
1717 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); | |
1718 } | |
1719 if( p->aMem ){ | |
1720 p->aMem--; /* aMem[] goes from 1..nMem */ | |
1721 p->nMem = nMem; /* not from 0..nMem-1 */ | |
1722 for(n=1; n<=nMem; n++){ | |
1723 p->aMem[n].flags = MEM_Undefined; | |
1724 p->aMem[n].db = db; | |
1725 } | |
1726 } | |
1727 p->explain = pParse->explain; | |
1728 sqlite3VdbeRewind(p); | |
1729 } | |
1730 | |
1731 /* | |
1732 ** Close a VDBE cursor and release all the resources that cursor | |
1733 ** happens to hold. | |
1734 */ | |
1735 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ | |
1736 if( pCx==0 ){ | |
1737 return; | |
1738 } | |
1739 sqlite3VdbeSorterClose(p->db, pCx); | |
1740 if( pCx->pBt ){ | |
1741 sqlite3BtreeClose(pCx->pBt); | |
1742 /* The pCx->pCursor will be close automatically, if it exists, by | |
1743 ** the call above. */ | |
1744 }else if( pCx->pCursor ){ | |
1745 sqlite3BtreeCloseCursor(pCx->pCursor); | |
1746 } | |
1747 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
1748 else if( pCx->pVtabCursor ){ | |
1749 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; | |
1750 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; | |
1751 p->inVtabMethod = 1; | |
1752 pModule->xClose(pVtabCursor); | |
1753 p->inVtabMethod = 0; | |
1754 } | |
1755 #endif | |
1756 } | |
1757 | |
1758 /* | |
1759 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This | |
1760 ** is used, for example, when a trigger sub-program is halted to restore | |
1761 ** control to the main program. | |
1762 */ | |
1763 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ | |
1764 Vdbe *v = pFrame->v; | |
1765 v->aOnceFlag = pFrame->aOnceFlag; | |
1766 v->nOnceFlag = pFrame->nOnceFlag; | |
1767 v->aOp = pFrame->aOp; | |
1768 v->nOp = pFrame->nOp; | |
1769 v->aMem = pFrame->aMem; | |
1770 v->nMem = pFrame->nMem; | |
1771 v->apCsr = pFrame->apCsr; | |
1772 v->nCursor = pFrame->nCursor; | |
1773 v->db->lastRowid = pFrame->lastRowid; | |
1774 v->nChange = pFrame->nChange; | |
1775 return pFrame->pc; | |
1776 } | |
1777 | |
1778 /* | |
1779 ** Close all cursors. | |
1780 ** | |
1781 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | |
1782 ** cell array. This is necessary as the memory cell array may contain | |
1783 ** pointers to VdbeFrame objects, which may in turn contain pointers to | |
1784 ** open cursors. | |
1785 */ | |
1786 static void closeAllCursors(Vdbe *p){ | |
1787 if( p->pFrame ){ | |
1788 VdbeFrame *pFrame; | |
1789 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | |
1790 sqlite3VdbeFrameRestore(pFrame); | |
1791 p->pFrame = 0; | |
1792 p->nFrame = 0; | |
1793 } | |
1794 assert( p->nFrame==0 ); | |
1795 | |
1796 if( p->apCsr ){ | |
1797 int i; | |
1798 for(i=0; i<p->nCursor; i++){ | |
1799 VdbeCursor *pC = p->apCsr[i]; | |
1800 if( pC ){ | |
1801 sqlite3VdbeFreeCursor(p, pC); | |
1802 p->apCsr[i] = 0; | |
1803 } | |
1804 } | |
1805 } | |
1806 if( p->aMem ){ | |
1807 releaseMemArray(&p->aMem[1], p->nMem); | |
1808 } | |
1809 while( p->pDelFrame ){ | |
1810 VdbeFrame *pDel = p->pDelFrame; | |
1811 p->pDelFrame = pDel->pParent; | |
1812 sqlite3VdbeFrameDelete(pDel); | |
1813 } | |
1814 | |
1815 /* Delete any auxdata allocations made by the VM */ | |
1816 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); | |
1817 assert( p->pAuxData==0 ); | |
1818 } | |
1819 | |
1820 /* | |
1821 ** Clean up the VM after a single run. | |
1822 */ | |
1823 static void Cleanup(Vdbe *p){ | |
1824 sqlite3 *db = p->db; | |
1825 | |
1826 #ifdef SQLITE_DEBUG | |
1827 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and | |
1828 ** Vdbe.aMem[] arrays have already been cleaned up. */ | |
1829 int i; | |
1830 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); | |
1831 if( p->aMem ){ | |
1832 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); | |
1833 } | |
1834 #endif | |
1835 | |
1836 sqlite3DbFree(db, p->zErrMsg); | |
1837 p->zErrMsg = 0; | |
1838 p->pResultSet = 0; | |
1839 } | |
1840 | |
1841 /* | |
1842 ** Set the number of result columns that will be returned by this SQL | |
1843 ** statement. This is now set at compile time, rather than during | |
1844 ** execution of the vdbe program so that sqlite3_column_count() can | |
1845 ** be called on an SQL statement before sqlite3_step(). | |
1846 */ | |
1847 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ | |
1848 Mem *pColName; | |
1849 int n; | |
1850 sqlite3 *db = p->db; | |
1851 | |
1852 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | |
1853 sqlite3DbFree(db, p->aColName); | |
1854 n = nResColumn*COLNAME_N; | |
1855 p->nResColumn = (u16)nResColumn; | |
1856 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); | |
1857 if( p->aColName==0 ) return; | |
1858 while( n-- > 0 ){ | |
1859 pColName->flags = MEM_Null; | |
1860 pColName->db = p->db; | |
1861 pColName++; | |
1862 } | |
1863 } | |
1864 | |
1865 /* | |
1866 ** Set the name of the idx'th column to be returned by the SQL statement. | |
1867 ** zName must be a pointer to a nul terminated string. | |
1868 ** | |
1869 ** This call must be made after a call to sqlite3VdbeSetNumCols(). | |
1870 ** | |
1871 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC | |
1872 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed | |
1873 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. | |
1874 */ | |
1875 int sqlite3VdbeSetColName( | |
1876 Vdbe *p, /* Vdbe being configured */ | |
1877 int idx, /* Index of column zName applies to */ | |
1878 int var, /* One of the COLNAME_* constants */ | |
1879 const char *zName, /* Pointer to buffer containing name */ | |
1880 void (*xDel)(void*) /* Memory management strategy for zName */ | |
1881 ){ | |
1882 int rc; | |
1883 Mem *pColName; | |
1884 assert( idx<p->nResColumn ); | |
1885 assert( var<COLNAME_N ); | |
1886 if( p->db->mallocFailed ){ | |
1887 assert( !zName || xDel!=SQLITE_DYNAMIC ); | |
1888 return SQLITE_NOMEM; | |
1889 } | |
1890 assert( p->aColName!=0 ); | |
1891 pColName = &(p->aColName[idx+var*p->nResColumn]); | |
1892 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); | |
1893 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); | |
1894 return rc; | |
1895 } | |
1896 | |
1897 /* | |
1898 ** A read or write transaction may or may not be active on database handle | |
1899 ** db. If a transaction is active, commit it. If there is a | |
1900 ** write-transaction spanning more than one database file, this routine | |
1901 ** takes care of the master journal trickery. | |
1902 */ | |
1903 static int vdbeCommit(sqlite3 *db, Vdbe *p){ | |
1904 int i; | |
1905 int nTrans = 0; /* Number of databases with an active write-transaction */ | |
1906 int rc = SQLITE_OK; | |
1907 int needXcommit = 0; | |
1908 | |
1909 #ifdef SQLITE_OMIT_VIRTUALTABLE | |
1910 /* With this option, sqlite3VtabSync() is defined to be simply | |
1911 ** SQLITE_OK so p is not used. | |
1912 */ | |
1913 UNUSED_PARAMETER(p); | |
1914 #endif | |
1915 | |
1916 /* Before doing anything else, call the xSync() callback for any | |
1917 ** virtual module tables written in this transaction. This has to | |
1918 ** be done before determining whether a master journal file is | |
1919 ** required, as an xSync() callback may add an attached database | |
1920 ** to the transaction. | |
1921 */ | |
1922 rc = sqlite3VtabSync(db, p); | |
1923 | |
1924 /* This loop determines (a) if the commit hook should be invoked and | |
1925 ** (b) how many database files have open write transactions, not | |
1926 ** including the temp database. (b) is important because if more than | |
1927 ** one database file has an open write transaction, a master journal | |
1928 ** file is required for an atomic commit. | |
1929 */ | |
1930 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
1931 Btree *pBt = db->aDb[i].pBt; | |
1932 if( sqlite3BtreeIsInTrans(pBt) ){ | |
1933 needXcommit = 1; | |
1934 if( i!=1 ) nTrans++; | |
1935 sqlite3BtreeEnter(pBt); | |
1936 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); | |
1937 sqlite3BtreeLeave(pBt); | |
1938 } | |
1939 } | |
1940 if( rc!=SQLITE_OK ){ | |
1941 return rc; | |
1942 } | |
1943 | |
1944 /* If there are any write-transactions at all, invoke the commit hook */ | |
1945 if( needXcommit && db->xCommitCallback ){ | |
1946 rc = db->xCommitCallback(db->pCommitArg); | |
1947 if( rc ){ | |
1948 return SQLITE_CONSTRAINT_COMMITHOOK; | |
1949 } | |
1950 } | |
1951 | |
1952 /* The simple case - no more than one database file (not counting the | |
1953 ** TEMP database) has a transaction active. There is no need for the | |
1954 ** master-journal. | |
1955 ** | |
1956 ** If the return value of sqlite3BtreeGetFilename() is a zero length | |
1957 ** string, it means the main database is :memory: or a temp file. In | |
1958 ** that case we do not support atomic multi-file commits, so use the | |
1959 ** simple case then too. | |
1960 */ | |
1961 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) | |
1962 || nTrans<=1 | |
1963 ){ | |
1964 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
1965 Btree *pBt = db->aDb[i].pBt; | |
1966 if( pBt ){ | |
1967 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); | |
1968 } | |
1969 } | |
1970 | |
1971 /* Do the commit only if all databases successfully complete phase 1. | |
1972 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an | |
1973 ** IO error while deleting or truncating a journal file. It is unlikely, | |
1974 ** but could happen. In this case abandon processing and return the error. | |
1975 */ | |
1976 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
1977 Btree *pBt = db->aDb[i].pBt; | |
1978 if( pBt ){ | |
1979 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); | |
1980 } | |
1981 } | |
1982 if( rc==SQLITE_OK ){ | |
1983 sqlite3VtabCommit(db); | |
1984 } | |
1985 } | |
1986 | |
1987 /* The complex case - There is a multi-file write-transaction active. | |
1988 ** This requires a master journal file to ensure the transaction is | |
1989 ** committed atomically. | |
1990 */ | |
1991 #ifndef SQLITE_OMIT_DISKIO | |
1992 else{ | |
1993 sqlite3_vfs *pVfs = db->pVfs; | |
1994 int needSync = 0; | |
1995 char *zMaster = 0; /* File-name for the master journal */ | |
1996 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); | |
1997 sqlite3_file *pMaster = 0; | |
1998 i64 offset = 0; | |
1999 int res; | |
2000 int retryCount = 0; | |
2001 int nMainFile; | |
2002 | |
2003 /* Select a master journal file name */ | |
2004 nMainFile = sqlite3Strlen30(zMainFile); | |
2005 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); | |
2006 if( zMaster==0 ) return SQLITE_NOMEM; | |
2007 do { | |
2008 u32 iRandom; | |
2009 if( retryCount ){ | |
2010 if( retryCount>100 ){ | |
2011 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); | |
2012 sqlite3OsDelete(pVfs, zMaster, 0); | |
2013 break; | |
2014 }else if( retryCount==1 ){ | |
2015 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); | |
2016 } | |
2017 } | |
2018 retryCount++; | |
2019 sqlite3_randomness(sizeof(iRandom), &iRandom); | |
2020 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", | |
2021 (iRandom>>8)&0xffffff, iRandom&0xff); | |
2022 /* The antipenultimate character of the master journal name must | |
2023 ** be "9" to avoid name collisions when using 8+3 filenames. */ | |
2024 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); | |
2025 sqlite3FileSuffix3(zMainFile, zMaster); | |
2026 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); | |
2027 }while( rc==SQLITE_OK && res ); | |
2028 if( rc==SQLITE_OK ){ | |
2029 /* Open the master journal. */ | |
2030 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, | |
2031 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| | |
2032 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 | |
2033 ); | |
2034 } | |
2035 if( rc!=SQLITE_OK ){ | |
2036 sqlite3DbFree(db, zMaster); | |
2037 return rc; | |
2038 } | |
2039 | |
2040 /* Write the name of each database file in the transaction into the new | |
2041 ** master journal file. If an error occurs at this point close | |
2042 ** and delete the master journal file. All the individual journal files | |
2043 ** still have 'null' as the master journal pointer, so they will roll | |
2044 ** back independently if a failure occurs. | |
2045 */ | |
2046 for(i=0; i<db->nDb; i++){ | |
2047 Btree *pBt = db->aDb[i].pBt; | |
2048 if( sqlite3BtreeIsInTrans(pBt) ){ | |
2049 char const *zFile = sqlite3BtreeGetJournalname(pBt); | |
2050 if( zFile==0 ){ | |
2051 continue; /* Ignore TEMP and :memory: databases */ | |
2052 } | |
2053 assert( zFile[0]!=0 ); | |
2054 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ | |
2055 needSync = 1; | |
2056 } | |
2057 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); | |
2058 offset += sqlite3Strlen30(zFile)+1; | |
2059 if( rc!=SQLITE_OK ){ | |
2060 sqlite3OsCloseFree(pMaster); | |
2061 sqlite3OsDelete(pVfs, zMaster, 0); | |
2062 sqlite3DbFree(db, zMaster); | |
2063 return rc; | |
2064 } | |
2065 } | |
2066 } | |
2067 | |
2068 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device | |
2069 ** flag is set this is not required. | |
2070 */ | |
2071 if( needSync | |
2072 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) | |
2073 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) | |
2074 ){ | |
2075 sqlite3OsCloseFree(pMaster); | |
2076 sqlite3OsDelete(pVfs, zMaster, 0); | |
2077 sqlite3DbFree(db, zMaster); | |
2078 return rc; | |
2079 } | |
2080 | |
2081 /* Sync all the db files involved in the transaction. The same call | |
2082 ** sets the master journal pointer in each individual journal. If | |
2083 ** an error occurs here, do not delete the master journal file. | |
2084 ** | |
2085 ** If the error occurs during the first call to | |
2086 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the | |
2087 ** master journal file will be orphaned. But we cannot delete it, | |
2088 ** in case the master journal file name was written into the journal | |
2089 ** file before the failure occurred. | |
2090 */ | |
2091 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | |
2092 Btree *pBt = db->aDb[i].pBt; | |
2093 if( pBt ){ | |
2094 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); | |
2095 } | |
2096 } | |
2097 sqlite3OsCloseFree(pMaster); | |
2098 assert( rc!=SQLITE_BUSY ); | |
2099 if( rc!=SQLITE_OK ){ | |
2100 sqlite3DbFree(db, zMaster); | |
2101 return rc; | |
2102 } | |
2103 | |
2104 /* Delete the master journal file. This commits the transaction. After | |
2105 ** doing this the directory is synced again before any individual | |
2106 ** transaction files are deleted. | |
2107 */ | |
2108 rc = sqlite3OsDelete(pVfs, zMaster, 1); | |
2109 sqlite3DbFree(db, zMaster); | |
2110 zMaster = 0; | |
2111 if( rc ){ | |
2112 return rc; | |
2113 } | |
2114 | |
2115 /* All files and directories have already been synced, so the following | |
2116 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and | |
2117 ** deleting or truncating journals. If something goes wrong while | |
2118 ** this is happening we don't really care. The integrity of the | |
2119 ** transaction is already guaranteed, but some stray 'cold' journals | |
2120 ** may be lying around. Returning an error code won't help matters. | |
2121 */ | |
2122 disable_simulated_io_errors(); | |
2123 sqlite3BeginBenignMalloc(); | |
2124 for(i=0; i<db->nDb; i++){ | |
2125 Btree *pBt = db->aDb[i].pBt; | |
2126 if( pBt ){ | |
2127 sqlite3BtreeCommitPhaseTwo(pBt, 1); | |
2128 } | |
2129 } | |
2130 sqlite3EndBenignMalloc(); | |
2131 enable_simulated_io_errors(); | |
2132 | |
2133 sqlite3VtabCommit(db); | |
2134 } | |
2135 #endif | |
2136 | |
2137 return rc; | |
2138 } | |
2139 | |
2140 /* | |
2141 ** This routine checks that the sqlite3.nVdbeActive count variable | |
2142 ** matches the number of vdbe's in the list sqlite3.pVdbe that are | |
2143 ** currently active. An assertion fails if the two counts do not match. | |
2144 ** This is an internal self-check only - it is not an essential processing | |
2145 ** step. | |
2146 ** | |
2147 ** This is a no-op if NDEBUG is defined. | |
2148 */ | |
2149 #ifndef NDEBUG | |
2150 static void checkActiveVdbeCnt(sqlite3 *db){ | |
2151 Vdbe *p; | |
2152 int cnt = 0; | |
2153 int nWrite = 0; | |
2154 int nRead = 0; | |
2155 p = db->pVdbe; | |
2156 while( p ){ | |
2157 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ | |
2158 cnt++; | |
2159 if( p->readOnly==0 ) nWrite++; | |
2160 if( p->bIsReader ) nRead++; | |
2161 } | |
2162 p = p->pNext; | |
2163 } | |
2164 assert( cnt==db->nVdbeActive ); | |
2165 assert( nWrite==db->nVdbeWrite ); | |
2166 assert( nRead==db->nVdbeRead ); | |
2167 } | |
2168 #else | |
2169 #define checkActiveVdbeCnt(x) | |
2170 #endif | |
2171 | |
2172 /* | |
2173 ** If the Vdbe passed as the first argument opened a statement-transaction, | |
2174 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or | |
2175 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement | |
2176 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the | |
2177 ** statement transaction is committed. | |
2178 ** | |
2179 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. | |
2180 ** Otherwise SQLITE_OK. | |
2181 */ | |
2182 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ | |
2183 sqlite3 *const db = p->db; | |
2184 int rc = SQLITE_OK; | |
2185 | |
2186 /* If p->iStatement is greater than zero, then this Vdbe opened a | |
2187 ** statement transaction that should be closed here. The only exception | |
2188 ** is that an IO error may have occurred, causing an emergency rollback. | |
2189 ** In this case (db->nStatement==0), and there is nothing to do. | |
2190 */ | |
2191 if( db->nStatement && p->iStatement ){ | |
2192 int i; | |
2193 const int iSavepoint = p->iStatement-1; | |
2194 | |
2195 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); | |
2196 assert( db->nStatement>0 ); | |
2197 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); | |
2198 | |
2199 for(i=0; i<db->nDb; i++){ | |
2200 int rc2 = SQLITE_OK; | |
2201 Btree *pBt = db->aDb[i].pBt; | |
2202 if( pBt ){ | |
2203 if( eOp==SAVEPOINT_ROLLBACK ){ | |
2204 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); | |
2205 } | |
2206 if( rc2==SQLITE_OK ){ | |
2207 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); | |
2208 } | |
2209 if( rc==SQLITE_OK ){ | |
2210 rc = rc2; | |
2211 } | |
2212 } | |
2213 } | |
2214 db->nStatement--; | |
2215 p->iStatement = 0; | |
2216 | |
2217 if( rc==SQLITE_OK ){ | |
2218 if( eOp==SAVEPOINT_ROLLBACK ){ | |
2219 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); | |
2220 } | |
2221 if( rc==SQLITE_OK ){ | |
2222 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); | |
2223 } | |
2224 } | |
2225 | |
2226 /* If the statement transaction is being rolled back, also restore the | |
2227 ** database handles deferred constraint counter to the value it had when | |
2228 ** the statement transaction was opened. */ | |
2229 if( eOp==SAVEPOINT_ROLLBACK ){ | |
2230 db->nDeferredCons = p->nStmtDefCons; | |
2231 db->nDeferredImmCons = p->nStmtDefImmCons; | |
2232 } | |
2233 } | |
2234 return rc; | |
2235 } | |
2236 | |
2237 /* | |
2238 ** This function is called when a transaction opened by the database | |
2239 ** handle associated with the VM passed as an argument is about to be | |
2240 ** committed. If there are outstanding deferred foreign key constraint | |
2241 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. | |
2242 ** | |
2243 ** If there are outstanding FK violations and this function returns | |
2244 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY | |
2245 ** and write an error message to it. Then return SQLITE_ERROR. | |
2246 */ | |
2247 #ifndef SQLITE_OMIT_FOREIGN_KEY | |
2248 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ | |
2249 sqlite3 *db = p->db; | |
2250 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) | |
2251 || (!deferred && p->nFkConstraint>0) | |
2252 ){ | |
2253 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; | |
2254 p->errorAction = OE_Abort; | |
2255 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed"); | |
2256 return SQLITE_ERROR; | |
2257 } | |
2258 return SQLITE_OK; | |
2259 } | |
2260 #endif | |
2261 | |
2262 /* | |
2263 ** This routine is called the when a VDBE tries to halt. If the VDBE | |
2264 ** has made changes and is in autocommit mode, then commit those | |
2265 ** changes. If a rollback is needed, then do the rollback. | |
2266 ** | |
2267 ** This routine is the only way to move the state of a VM from | |
2268 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to | |
2269 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. | |
2270 ** | |
2271 ** Return an error code. If the commit could not complete because of | |
2272 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it | |
2273 ** means the close did not happen and needs to be repeated. | |
2274 */ | |
2275 int sqlite3VdbeHalt(Vdbe *p){ | |
2276 int rc; /* Used to store transient return codes */ | |
2277 sqlite3 *db = p->db; | |
2278 | |
2279 /* This function contains the logic that determines if a statement or | |
2280 ** transaction will be committed or rolled back as a result of the | |
2281 ** execution of this virtual machine. | |
2282 ** | |
2283 ** If any of the following errors occur: | |
2284 ** | |
2285 ** SQLITE_NOMEM | |
2286 ** SQLITE_IOERR | |
2287 ** SQLITE_FULL | |
2288 ** SQLITE_INTERRUPT | |
2289 ** | |
2290 ** Then the internal cache might have been left in an inconsistent | |
2291 ** state. We need to rollback the statement transaction, if there is | |
2292 ** one, or the complete transaction if there is no statement transaction. | |
2293 */ | |
2294 | |
2295 if( p->db->mallocFailed ){ | |
2296 p->rc = SQLITE_NOMEM; | |
2297 } | |
2298 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); | |
2299 closeAllCursors(p); | |
2300 if( p->magic!=VDBE_MAGIC_RUN ){ | |
2301 return SQLITE_OK; | |
2302 } | |
2303 checkActiveVdbeCnt(db); | |
2304 | |
2305 /* No commit or rollback needed if the program never started or if the | |
2306 ** SQL statement does not read or write a database file. */ | |
2307 if( p->pc>=0 && p->bIsReader ){ | |
2308 int mrc; /* Primary error code from p->rc */ | |
2309 int eStatementOp = 0; | |
2310 int isSpecialError; /* Set to true if a 'special' error */ | |
2311 | |
2312 /* Lock all btrees used by the statement */ | |
2313 sqlite3VdbeEnter(p); | |
2314 | |
2315 /* Check for one of the special errors */ | |
2316 mrc = p->rc & 0xff; | |
2317 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR | |
2318 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; | |
2319 if( isSpecialError ){ | |
2320 /* If the query was read-only and the error code is SQLITE_INTERRUPT, | |
2321 ** no rollback is necessary. Otherwise, at least a savepoint | |
2322 ** transaction must be rolled back to restore the database to a | |
2323 ** consistent state. | |
2324 ** | |
2325 ** Even if the statement is read-only, it is important to perform | |
2326 ** a statement or transaction rollback operation. If the error | |
2327 ** occurred while writing to the journal, sub-journal or database | |
2328 ** file as part of an effort to free up cache space (see function | |
2329 ** pagerStress() in pager.c), the rollback is required to restore | |
2330 ** the pager to a consistent state. | |
2331 */ | |
2332 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ | |
2333 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ | |
2334 eStatementOp = SAVEPOINT_ROLLBACK; | |
2335 }else{ | |
2336 /* We are forced to roll back the active transaction. Before doing | |
2337 ** so, abort any other statements this handle currently has active. | |
2338 */ | |
2339 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | |
2340 sqlite3CloseSavepoints(db); | |
2341 db->autoCommit = 1; | |
2342 } | |
2343 } | |
2344 } | |
2345 | |
2346 /* Check for immediate foreign key violations. */ | |
2347 if( p->rc==SQLITE_OK ){ | |
2348 sqlite3VdbeCheckFk(p, 0); | |
2349 } | |
2350 | |
2351 /* If the auto-commit flag is set and this is the only active writer | |
2352 ** VM, then we do either a commit or rollback of the current transaction. | |
2353 ** | |
2354 ** Note: This block also runs if one of the special errors handled | |
2355 ** above has occurred. | |
2356 */ | |
2357 if( !sqlite3VtabInSync(db) | |
2358 && db->autoCommit | |
2359 && db->nVdbeWrite==(p->readOnly==0) | |
2360 ){ | |
2361 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ | |
2362 rc = sqlite3VdbeCheckFk(p, 1); | |
2363 if( rc!=SQLITE_OK ){ | |
2364 if( NEVER(p->readOnly) ){ | |
2365 sqlite3VdbeLeave(p); | |
2366 return SQLITE_ERROR; | |
2367 } | |
2368 rc = SQLITE_CONSTRAINT_FOREIGNKEY; | |
2369 }else{ | |
2370 /* The auto-commit flag is true, the vdbe program was successful | |
2371 ** or hit an 'OR FAIL' constraint and there are no deferred foreign | |
2372 ** key constraints to hold up the transaction. This means a commit | |
2373 ** is required. */ | |
2374 rc = vdbeCommit(db, p); | |
2375 } | |
2376 if( rc==SQLITE_BUSY && p->readOnly ){ | |
2377 sqlite3VdbeLeave(p); | |
2378 return SQLITE_BUSY; | |
2379 }else if( rc!=SQLITE_OK ){ | |
2380 p->rc = rc; | |
2381 sqlite3RollbackAll(db, SQLITE_OK); | |
2382 }else{ | |
2383 db->nDeferredCons = 0; | |
2384 db->nDeferredImmCons = 0; | |
2385 db->flags &= ~SQLITE_DeferFKs; | |
2386 sqlite3CommitInternalChanges(db); | |
2387 } | |
2388 }else{ | |
2389 sqlite3RollbackAll(db, SQLITE_OK); | |
2390 } | |
2391 db->nStatement = 0; | |
2392 }else if( eStatementOp==0 ){ | |
2393 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ | |
2394 eStatementOp = SAVEPOINT_RELEASE; | |
2395 }else if( p->errorAction==OE_Abort ){ | |
2396 eStatementOp = SAVEPOINT_ROLLBACK; | |
2397 }else{ | |
2398 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | |
2399 sqlite3CloseSavepoints(db); | |
2400 db->autoCommit = 1; | |
2401 } | |
2402 } | |
2403 | |
2404 /* If eStatementOp is non-zero, then a statement transaction needs to | |
2405 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to | |
2406 ** do so. If this operation returns an error, and the current statement | |
2407 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the | |
2408 ** current statement error code. | |
2409 */ | |
2410 if( eStatementOp ){ | |
2411 rc = sqlite3VdbeCloseStatement(p, eStatementOp); | |
2412 if( rc ){ | |
2413 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ | |
2414 p->rc = rc; | |
2415 sqlite3DbFree(db, p->zErrMsg); | |
2416 p->zErrMsg = 0; | |
2417 } | |
2418 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | |
2419 sqlite3CloseSavepoints(db); | |
2420 db->autoCommit = 1; | |
2421 } | |
2422 } | |
2423 | |
2424 /* If this was an INSERT, UPDATE or DELETE and no statement transaction | |
2425 ** has been rolled back, update the database connection change-counter. | |
2426 */ | |
2427 if( p->changeCntOn ){ | |
2428 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ | |
2429 sqlite3VdbeSetChanges(db, p->nChange); | |
2430 }else{ | |
2431 sqlite3VdbeSetChanges(db, 0); | |
2432 } | |
2433 p->nChange = 0; | |
2434 } | |
2435 | |
2436 /* Release the locks */ | |
2437 sqlite3VdbeLeave(p); | |
2438 } | |
2439 | |
2440 /* We have successfully halted and closed the VM. Record this fact. */ | |
2441 if( p->pc>=0 ){ | |
2442 db->nVdbeActive--; | |
2443 if( !p->readOnly ) db->nVdbeWrite--; | |
2444 if( p->bIsReader ) db->nVdbeRead--; | |
2445 assert( db->nVdbeActive>=db->nVdbeRead ); | |
2446 assert( db->nVdbeRead>=db->nVdbeWrite ); | |
2447 assert( db->nVdbeWrite>=0 ); | |
2448 } | |
2449 p->magic = VDBE_MAGIC_HALT; | |
2450 checkActiveVdbeCnt(db); | |
2451 if( p->db->mallocFailed ){ | |
2452 p->rc = SQLITE_NOMEM; | |
2453 } | |
2454 | |
2455 /* If the auto-commit flag is set to true, then any locks that were held | |
2456 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() | |
2457 ** to invoke any required unlock-notify callbacks. | |
2458 */ | |
2459 if( db->autoCommit ){ | |
2460 sqlite3ConnectionUnlocked(db); | |
2461 } | |
2462 | |
2463 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); | |
2464 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); | |
2465 } | |
2466 | |
2467 | |
2468 /* | |
2469 ** Each VDBE holds the result of the most recent sqlite3_step() call | |
2470 ** in p->rc. This routine sets that result back to SQLITE_OK. | |
2471 */ | |
2472 void sqlite3VdbeResetStepResult(Vdbe *p){ | |
2473 p->rc = SQLITE_OK; | |
2474 } | |
2475 | |
2476 /* | |
2477 ** Copy the error code and error message belonging to the VDBE passed | |
2478 ** as the first argument to its database handle (so that they will be | |
2479 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). | |
2480 ** | |
2481 ** This function does not clear the VDBE error code or message, just | |
2482 ** copies them to the database handle. | |
2483 */ | |
2484 int sqlite3VdbeTransferError(Vdbe *p){ | |
2485 sqlite3 *db = p->db; | |
2486 int rc = p->rc; | |
2487 if( p->zErrMsg ){ | |
2488 u8 mallocFailed = db->mallocFailed; | |
2489 sqlite3BeginBenignMalloc(); | |
2490 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); | |
2491 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); | |
2492 sqlite3EndBenignMalloc(); | |
2493 db->mallocFailed = mallocFailed; | |
2494 db->errCode = rc; | |
2495 }else{ | |
2496 sqlite3Error(db, rc); | |
2497 } | |
2498 return rc; | |
2499 } | |
2500 | |
2501 #ifdef SQLITE_ENABLE_SQLLOG | |
2502 /* | |
2503 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, | |
2504 ** invoke it. | |
2505 */ | |
2506 static void vdbeInvokeSqllog(Vdbe *v){ | |
2507 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){ | |
2508 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql); | |
2509 assert( v->db->init.busy==0 ); | |
2510 if( zExpanded ){ | |
2511 sqlite3GlobalConfig.xSqllog( | |
2512 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1 | |
2513 ); | |
2514 sqlite3DbFree(v->db, zExpanded); | |
2515 } | |
2516 } | |
2517 } | |
2518 #else | |
2519 # define vdbeInvokeSqllog(x) | |
2520 #endif | |
2521 | |
2522 /* | |
2523 ** Clean up a VDBE after execution but do not delete the VDBE just yet. | |
2524 ** Write any error messages into *pzErrMsg. Return the result code. | |
2525 ** | |
2526 ** After this routine is run, the VDBE should be ready to be executed | |
2527 ** again. | |
2528 ** | |
2529 ** To look at it another way, this routine resets the state of the | |
2530 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to | |
2531 ** VDBE_MAGIC_INIT. | |
2532 */ | |
2533 int sqlite3VdbeReset(Vdbe *p){ | |
2534 sqlite3 *db; | |
2535 db = p->db; | |
2536 | |
2537 /* If the VM did not run to completion or if it encountered an | |
2538 ** error, then it might not have been halted properly. So halt | |
2539 ** it now. | |
2540 */ | |
2541 sqlite3VdbeHalt(p); | |
2542 | |
2543 /* If the VDBE has be run even partially, then transfer the error code | |
2544 ** and error message from the VDBE into the main database structure. But | |
2545 ** if the VDBE has just been set to run but has not actually executed any | |
2546 ** instructions yet, leave the main database error information unchanged. | |
2547 */ | |
2548 if( p->pc>=0 ){ | |
2549 vdbeInvokeSqllog(p); | |
2550 sqlite3VdbeTransferError(p); | |
2551 sqlite3DbFree(db, p->zErrMsg); | |
2552 p->zErrMsg = 0; | |
2553 if( p->runOnlyOnce ) p->expired = 1; | |
2554 }else if( p->rc && p->expired ){ | |
2555 /* The expired flag was set on the VDBE before the first call | |
2556 ** to sqlite3_step(). For consistency (since sqlite3_step() was | |
2557 ** called), set the database error in this case as well. | |
2558 */ | |
2559 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); | |
2560 sqlite3DbFree(db, p->zErrMsg); | |
2561 p->zErrMsg = 0; | |
2562 } | |
2563 | |
2564 /* Reclaim all memory used by the VDBE | |
2565 */ | |
2566 Cleanup(p); | |
2567 | |
2568 /* Save profiling information from this VDBE run. | |
2569 */ | |
2570 #ifdef VDBE_PROFILE | |
2571 { | |
2572 FILE *out = fopen("vdbe_profile.out", "a"); | |
2573 if( out ){ | |
2574 int i; | |
2575 fprintf(out, "---- "); | |
2576 for(i=0; i<p->nOp; i++){ | |
2577 fprintf(out, "%02x", p->aOp[i].opcode); | |
2578 } | |
2579 fprintf(out, "\n"); | |
2580 if( p->zSql ){ | |
2581 char c, pc = 0; | |
2582 fprintf(out, "-- "); | |
2583 for(i=0; (c = p->zSql[i])!=0; i++){ | |
2584 if( pc=='\n' ) fprintf(out, "-- "); | |
2585 putc(c, out); | |
2586 pc = c; | |
2587 } | |
2588 if( pc!='\n' ) fprintf(out, "\n"); | |
2589 } | |
2590 for(i=0; i<p->nOp; i++){ | |
2591 char zHdr[100]; | |
2592 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", | |
2593 p->aOp[i].cnt, | |
2594 p->aOp[i].cycles, | |
2595 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 | |
2596 ); | |
2597 fprintf(out, "%s", zHdr); | |
2598 sqlite3VdbePrintOp(out, i, &p->aOp[i]); | |
2599 } | |
2600 fclose(out); | |
2601 } | |
2602 } | |
2603 #endif | |
2604 p->iCurrentTime = 0; | |
2605 p->magic = VDBE_MAGIC_INIT; | |
2606 return p->rc & db->errMask; | |
2607 } | |
2608 | |
2609 /* | |
2610 ** Clean up and delete a VDBE after execution. Return an integer which is | |
2611 ** the result code. Write any error message text into *pzErrMsg. | |
2612 */ | |
2613 int sqlite3VdbeFinalize(Vdbe *p){ | |
2614 int rc = SQLITE_OK; | |
2615 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ | |
2616 rc = sqlite3VdbeReset(p); | |
2617 assert( (rc & p->db->errMask)==rc ); | |
2618 } | |
2619 sqlite3VdbeDelete(p); | |
2620 return rc; | |
2621 } | |
2622 | |
2623 /* | |
2624 ** If parameter iOp is less than zero, then invoke the destructor for | |
2625 ** all auxiliary data pointers currently cached by the VM passed as | |
2626 ** the first argument. | |
2627 ** | |
2628 ** Or, if iOp is greater than or equal to zero, then the destructor is | |
2629 ** only invoked for those auxiliary data pointers created by the user | |
2630 ** function invoked by the OP_Function opcode at instruction iOp of | |
2631 ** VM pVdbe, and only then if: | |
2632 ** | |
2633 ** * the associated function parameter is the 32nd or later (counting | |
2634 ** from left to right), or | |
2635 ** | |
2636 ** * the corresponding bit in argument mask is clear (where the first | |
2637 ** function parameter corresponds to bit 0 etc.). | |
2638 */ | |
2639 void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ | |
2640 AuxData **pp = &pVdbe->pAuxData; | |
2641 while( *pp ){ | |
2642 AuxData *pAux = *pp; | |
2643 if( (iOp<0) | |
2644 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) | |
2645 ){ | |
2646 testcase( pAux->iArg==31 ); | |
2647 if( pAux->xDelete ){ | |
2648 pAux->xDelete(pAux->pAux); | |
2649 } | |
2650 *pp = pAux->pNext; | |
2651 sqlite3DbFree(pVdbe->db, pAux); | |
2652 }else{ | |
2653 pp= &pAux->pNext; | |
2654 } | |
2655 } | |
2656 } | |
2657 | |
2658 /* | |
2659 ** Free all memory associated with the Vdbe passed as the second argument, | |
2660 ** except for object itself, which is preserved. | |
2661 ** | |
2662 ** The difference between this function and sqlite3VdbeDelete() is that | |
2663 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with | |
2664 ** the database connection and frees the object itself. | |
2665 */ | |
2666 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ | |
2667 SubProgram *pSub, *pNext; | |
2668 int i; | |
2669 assert( p->db==0 || p->db==db ); | |
2670 releaseMemArray(p->aVar, p->nVar); | |
2671 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | |
2672 for(pSub=p->pProgram; pSub; pSub=pNext){ | |
2673 pNext = pSub->pNext; | |
2674 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); | |
2675 sqlite3DbFree(db, pSub); | |
2676 } | |
2677 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); | |
2678 vdbeFreeOpArray(db, p->aOp, p->nOp); | |
2679 sqlite3DbFree(db, p->aColName); | |
2680 sqlite3DbFree(db, p->zSql); | |
2681 sqlite3DbFree(db, p->pFree); | |
2682 } | |
2683 | |
2684 /* | |
2685 ** Delete an entire VDBE. | |
2686 */ | |
2687 void sqlite3VdbeDelete(Vdbe *p){ | |
2688 sqlite3 *db; | |
2689 | |
2690 if( NEVER(p==0) ) return; | |
2691 db = p->db; | |
2692 assert( sqlite3_mutex_held(db->mutex) ); | |
2693 sqlite3VdbeClearObject(db, p); | |
2694 if( p->pPrev ){ | |
2695 p->pPrev->pNext = p->pNext; | |
2696 }else{ | |
2697 assert( db->pVdbe==p ); | |
2698 db->pVdbe = p->pNext; | |
2699 } | |
2700 if( p->pNext ){ | |
2701 p->pNext->pPrev = p->pPrev; | |
2702 } | |
2703 p->magic = VDBE_MAGIC_DEAD; | |
2704 p->db = 0; | |
2705 sqlite3DbFree(db, p); | |
2706 } | |
2707 | |
2708 /* | |
2709 ** The cursor "p" has a pending seek operation that has not yet been | |
2710 ** carried out. Seek the cursor now. If an error occurs, return | |
2711 ** the appropriate error code. | |
2712 */ | |
2713 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ | |
2714 int res, rc; | |
2715 #ifdef SQLITE_TEST | |
2716 extern int sqlite3_search_count; | |
2717 #endif | |
2718 assert( p->deferredMoveto ); | |
2719 assert( p->isTable ); | |
2720 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); | |
2721 if( rc ) return rc; | |
2722 if( res!=0 ) return SQLITE_CORRUPT_BKPT; | |
2723 #ifdef SQLITE_TEST | |
2724 sqlite3_search_count++; | |
2725 #endif | |
2726 p->deferredMoveto = 0; | |
2727 p->cacheStatus = CACHE_STALE; | |
2728 return SQLITE_OK; | |
2729 } | |
2730 | |
2731 /* | |
2732 ** Something has moved cursor "p" out of place. Maybe the row it was | |
2733 ** pointed to was deleted out from under it. Or maybe the btree was | |
2734 ** rebalanced. Whatever the cause, try to restore "p" to the place it | |
2735 ** is supposed to be pointing. If the row was deleted out from under the | |
2736 ** cursor, set the cursor to point to a NULL row. | |
2737 */ | |
2738 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ | |
2739 int isDifferentRow, rc; | |
2740 assert( p->pCursor!=0 ); | |
2741 assert( sqlite3BtreeCursorHasMoved(p->pCursor) ); | |
2742 rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); | |
2743 p->cacheStatus = CACHE_STALE; | |
2744 if( isDifferentRow ) p->nullRow = 1; | |
2745 return rc; | |
2746 } | |
2747 | |
2748 /* | |
2749 ** Check to ensure that the cursor is valid. Restore the cursor | |
2750 ** if need be. Return any I/O error from the restore operation. | |
2751 */ | |
2752 int sqlite3VdbeCursorRestore(VdbeCursor *p){ | |
2753 if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ | |
2754 return handleMovedCursor(p); | |
2755 } | |
2756 return SQLITE_OK; | |
2757 } | |
2758 | |
2759 /* | |
2760 ** Make sure the cursor p is ready to read or write the row to which it | |
2761 ** was last positioned. Return an error code if an OOM fault or I/O error | |
2762 ** prevents us from positioning the cursor to its correct position. | |
2763 ** | |
2764 ** If a MoveTo operation is pending on the given cursor, then do that | |
2765 ** MoveTo now. If no move is pending, check to see if the row has been | |
2766 ** deleted out from under the cursor and if it has, mark the row as | |
2767 ** a NULL row. | |
2768 ** | |
2769 ** If the cursor is already pointing to the correct row and that row has | |
2770 ** not been deleted out from under the cursor, then this routine is a no-op. | |
2771 */ | |
2772 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ | |
2773 if( p->deferredMoveto ){ | |
2774 return handleDeferredMoveto(p); | |
2775 } | |
2776 if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ | |
2777 return handleMovedCursor(p); | |
2778 } | |
2779 return SQLITE_OK; | |
2780 } | |
2781 | |
2782 /* | |
2783 ** The following functions: | |
2784 ** | |
2785 ** sqlite3VdbeSerialType() | |
2786 ** sqlite3VdbeSerialTypeLen() | |
2787 ** sqlite3VdbeSerialLen() | |
2788 ** sqlite3VdbeSerialPut() | |
2789 ** sqlite3VdbeSerialGet() | |
2790 ** | |
2791 ** encapsulate the code that serializes values for storage in SQLite | |
2792 ** data and index records. Each serialized value consists of a | |
2793 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned | |
2794 ** integer, stored as a varint. | |
2795 ** | |
2796 ** In an SQLite index record, the serial type is stored directly before | |
2797 ** the blob of data that it corresponds to. In a table record, all serial | |
2798 ** types are stored at the start of the record, and the blobs of data at | |
2799 ** the end. Hence these functions allow the caller to handle the | |
2800 ** serial-type and data blob separately. | |
2801 ** | |
2802 ** The following table describes the various storage classes for data: | |
2803 ** | |
2804 ** serial type bytes of data type | |
2805 ** -------------- --------------- --------------- | |
2806 ** 0 0 NULL | |
2807 ** 1 1 signed integer | |
2808 ** 2 2 signed integer | |
2809 ** 3 3 signed integer | |
2810 ** 4 4 signed integer | |
2811 ** 5 6 signed integer | |
2812 ** 6 8 signed integer | |
2813 ** 7 8 IEEE float | |
2814 ** 8 0 Integer constant 0 | |
2815 ** 9 0 Integer constant 1 | |
2816 ** 10,11 reserved for expansion | |
2817 ** N>=12 and even (N-12)/2 BLOB | |
2818 ** N>=13 and odd (N-13)/2 text | |
2819 ** | |
2820 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions | |
2821 ** of SQLite will not understand those serial types. | |
2822 */ | |
2823 | |
2824 /* | |
2825 ** Return the serial-type for the value stored in pMem. | |
2826 */ | |
2827 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ | |
2828 int flags = pMem->flags; | |
2829 u32 n; | |
2830 | |
2831 if( flags&MEM_Null ){ | |
2832 return 0; | |
2833 } | |
2834 if( flags&MEM_Int ){ | |
2835 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ | |
2836 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) | |
2837 i64 i = pMem->u.i; | |
2838 u64 u; | |
2839 if( i<0 ){ | |
2840 if( i<(-MAX_6BYTE) ) return 6; | |
2841 /* Previous test prevents: u = -(-9223372036854775808) */ | |
2842 u = -i; | |
2843 }else{ | |
2844 u = i; | |
2845 } | |
2846 if( u<=127 ){ | |
2847 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; | |
2848 } | |
2849 if( u<=32767 ) return 2; | |
2850 if( u<=8388607 ) return 3; | |
2851 if( u<=2147483647 ) return 4; | |
2852 if( u<=MAX_6BYTE ) return 5; | |
2853 return 6; | |
2854 } | |
2855 if( flags&MEM_Real ){ | |
2856 return 7; | |
2857 } | |
2858 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); | |
2859 assert( pMem->n>=0 ); | |
2860 n = (u32)pMem->n; | |
2861 if( flags & MEM_Zero ){ | |
2862 n += pMem->u.nZero; | |
2863 } | |
2864 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); | |
2865 } | |
2866 | |
2867 /* | |
2868 ** Return the length of the data corresponding to the supplied serial-type. | |
2869 */ | |
2870 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ | |
2871 if( serial_type>=12 ){ | |
2872 return (serial_type-12)/2; | |
2873 }else{ | |
2874 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; | |
2875 return aSize[serial_type]; | |
2876 } | |
2877 } | |
2878 | |
2879 /* | |
2880 ** If we are on an architecture with mixed-endian floating | |
2881 ** points (ex: ARM7) then swap the lower 4 bytes with the | |
2882 ** upper 4 bytes. Return the result. | |
2883 ** | |
2884 ** For most architectures, this is a no-op. | |
2885 ** | |
2886 ** (later): It is reported to me that the mixed-endian problem | |
2887 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems | |
2888 ** that early versions of GCC stored the two words of a 64-bit | |
2889 ** float in the wrong order. And that error has been propagated | |
2890 ** ever since. The blame is not necessarily with GCC, though. | |
2891 ** GCC might have just copying the problem from a prior compiler. | |
2892 ** I am also told that newer versions of GCC that follow a different | |
2893 ** ABI get the byte order right. | |
2894 ** | |
2895 ** Developers using SQLite on an ARM7 should compile and run their | |
2896 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG | |
2897 ** enabled, some asserts below will ensure that the byte order of | |
2898 ** floating point values is correct. | |
2899 ** | |
2900 ** (2007-08-30) Frank van Vugt has studied this problem closely | |
2901 ** and has send his findings to the SQLite developers. Frank | |
2902 ** writes that some Linux kernels offer floating point hardware | |
2903 ** emulation that uses only 32-bit mantissas instead of a full | |
2904 ** 48-bits as required by the IEEE standard. (This is the | |
2905 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point | |
2906 ** byte swapping becomes very complicated. To avoid problems, | |
2907 ** the necessary byte swapping is carried out using a 64-bit integer | |
2908 ** rather than a 64-bit float. Frank assures us that the code here | |
2909 ** works for him. We, the developers, have no way to independently | |
2910 ** verify this, but Frank seems to know what he is talking about | |
2911 ** so we trust him. | |
2912 */ | |
2913 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT | |
2914 static u64 floatSwap(u64 in){ | |
2915 union { | |
2916 u64 r; | |
2917 u32 i[2]; | |
2918 } u; | |
2919 u32 t; | |
2920 | |
2921 u.r = in; | |
2922 t = u.i[0]; | |
2923 u.i[0] = u.i[1]; | |
2924 u.i[1] = t; | |
2925 return u.r; | |
2926 } | |
2927 # define swapMixedEndianFloat(X) X = floatSwap(X) | |
2928 #else | |
2929 # define swapMixedEndianFloat(X) | |
2930 #endif | |
2931 | |
2932 /* | |
2933 ** Write the serialized data blob for the value stored in pMem into | |
2934 ** buf. It is assumed that the caller has allocated sufficient space. | |
2935 ** Return the number of bytes written. | |
2936 ** | |
2937 ** nBuf is the amount of space left in buf[]. The caller is responsible | |
2938 ** for allocating enough space to buf[] to hold the entire field, exclusive | |
2939 ** of the pMem->u.nZero bytes for a MEM_Zero value. | |
2940 ** | |
2941 ** Return the number of bytes actually written into buf[]. The number | |
2942 ** of bytes in the zero-filled tail is included in the return value only | |
2943 ** if those bytes were zeroed in buf[]. | |
2944 */ | |
2945 u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ | |
2946 u32 len; | |
2947 | |
2948 /* Integer and Real */ | |
2949 if( serial_type<=7 && serial_type>0 ){ | |
2950 u64 v; | |
2951 u32 i; | |
2952 if( serial_type==7 ){ | |
2953 assert( sizeof(v)==sizeof(pMem->u.r) ); | |
2954 memcpy(&v, &pMem->u.r, sizeof(v)); | |
2955 swapMixedEndianFloat(v); | |
2956 }else{ | |
2957 v = pMem->u.i; | |
2958 } | |
2959 len = i = sqlite3VdbeSerialTypeLen(serial_type); | |
2960 assert( i>0 ); | |
2961 do{ | |
2962 buf[--i] = (u8)(v&0xFF); | |
2963 v >>= 8; | |
2964 }while( i ); | |
2965 return len; | |
2966 } | |
2967 | |
2968 /* String or blob */ | |
2969 if( serial_type>=12 ){ | |
2970 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) | |
2971 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); | |
2972 len = pMem->n; | |
2973 memcpy(buf, pMem->z, len); | |
2974 return len; | |
2975 } | |
2976 | |
2977 /* NULL or constants 0 or 1 */ | |
2978 return 0; | |
2979 } | |
2980 | |
2981 /* Input "x" is a sequence of unsigned characters that represent a | |
2982 ** big-endian integer. Return the equivalent native integer | |
2983 */ | |
2984 #define ONE_BYTE_INT(x) ((i8)(x)[0]) | |
2985 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) | |
2986 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) | |
2987 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) | |
2988 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) | |
2989 | |
2990 /* | |
2991 ** Deserialize the data blob pointed to by buf as serial type serial_type | |
2992 ** and store the result in pMem. Return the number of bytes read. | |
2993 ** | |
2994 ** This function is implemented as two separate routines for performance. | |
2995 ** The few cases that require local variables are broken out into a separate | |
2996 ** routine so that in most cases the overhead of moving the stack pointer | |
2997 ** is avoided. | |
2998 */ | |
2999 static u32 SQLITE_NOINLINE serialGet( | |
3000 const unsigned char *buf, /* Buffer to deserialize from */ | |
3001 u32 serial_type, /* Serial type to deserialize */ | |
3002 Mem *pMem /* Memory cell to write value into */ | |
3003 ){ | |
3004 u64 x = FOUR_BYTE_UINT(buf); | |
3005 u32 y = FOUR_BYTE_UINT(buf+4); | |
3006 x = (x<<32) + y; | |
3007 if( serial_type==6 ){ | |
3008 pMem->u.i = *(i64*)&x; | |
3009 pMem->flags = MEM_Int; | |
3010 testcase( pMem->u.i<0 ); | |
3011 }else{ | |
3012 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) | |
3013 /* Verify that integers and floating point values use the same | |
3014 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is | |
3015 ** defined that 64-bit floating point values really are mixed | |
3016 ** endian. | |
3017 */ | |
3018 static const u64 t1 = ((u64)0x3ff00000)<<32; | |
3019 static const double r1 = 1.0; | |
3020 u64 t2 = t1; | |
3021 swapMixedEndianFloat(t2); | |
3022 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); | |
3023 #endif | |
3024 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); | |
3025 swapMixedEndianFloat(x); | |
3026 memcpy(&pMem->u.r, &x, sizeof(x)); | |
3027 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; | |
3028 } | |
3029 return 8; | |
3030 } | |
3031 u32 sqlite3VdbeSerialGet( | |
3032 const unsigned char *buf, /* Buffer to deserialize from */ | |
3033 u32 serial_type, /* Serial type to deserialize */ | |
3034 Mem *pMem /* Memory cell to write value into */ | |
3035 ){ | |
3036 switch( serial_type ){ | |
3037 case 10: /* Reserved for future use */ | |
3038 case 11: /* Reserved for future use */ | |
3039 case 0: { /* NULL */ | |
3040 pMem->flags = MEM_Null; | |
3041 break; | |
3042 } | |
3043 case 1: { /* 1-byte signed integer */ | |
3044 pMem->u.i = ONE_BYTE_INT(buf); | |
3045 pMem->flags = MEM_Int; | |
3046 testcase( pMem->u.i<0 ); | |
3047 return 1; | |
3048 } | |
3049 case 2: { /* 2-byte signed integer */ | |
3050 pMem->u.i = TWO_BYTE_INT(buf); | |
3051 pMem->flags = MEM_Int; | |
3052 testcase( pMem->u.i<0 ); | |
3053 return 2; | |
3054 } | |
3055 case 3: { /* 3-byte signed integer */ | |
3056 pMem->u.i = THREE_BYTE_INT(buf); | |
3057 pMem->flags = MEM_Int; | |
3058 testcase( pMem->u.i<0 ); | |
3059 return 3; | |
3060 } | |
3061 case 4: { /* 4-byte signed integer */ | |
3062 pMem->u.i = FOUR_BYTE_INT(buf); | |
3063 pMem->flags = MEM_Int; | |
3064 testcase( pMem->u.i<0 ); | |
3065 return 4; | |
3066 } | |
3067 case 5: { /* 6-byte signed integer */ | |
3068 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); | |
3069 pMem->flags = MEM_Int; | |
3070 testcase( pMem->u.i<0 ); | |
3071 return 6; | |
3072 } | |
3073 case 6: /* 8-byte signed integer */ | |
3074 case 7: { /* IEEE floating point */ | |
3075 /* These use local variables, so do them in a separate routine | |
3076 ** to avoid having to move the frame pointer in the common case */ | |
3077 return serialGet(buf,serial_type,pMem); | |
3078 } | |
3079 case 8: /* Integer 0 */ | |
3080 case 9: { /* Integer 1 */ | |
3081 pMem->u.i = serial_type-8; | |
3082 pMem->flags = MEM_Int; | |
3083 return 0; | |
3084 } | |
3085 default: { | |
3086 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; | |
3087 pMem->z = (char *)buf; | |
3088 pMem->n = (serial_type-12)/2; | |
3089 pMem->flags = aFlag[serial_type&1]; | |
3090 return pMem->n; | |
3091 } | |
3092 } | |
3093 return 0; | |
3094 } | |
3095 /* | |
3096 ** This routine is used to allocate sufficient space for an UnpackedRecord | |
3097 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if | |
3098 ** the first argument is a pointer to KeyInfo structure pKeyInfo. | |
3099 ** | |
3100 ** The space is either allocated using sqlite3DbMallocRaw() or from within | |
3101 ** the unaligned buffer passed via the second and third arguments (presumably | |
3102 ** stack space). If the former, then *ppFree is set to a pointer that should | |
3103 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the | |
3104 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL | |
3105 ** before returning. | |
3106 ** | |
3107 ** If an OOM error occurs, NULL is returned. | |
3108 */ | |
3109 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( | |
3110 KeyInfo *pKeyInfo, /* Description of the record */ | |
3111 char *pSpace, /* Unaligned space available */ | |
3112 int szSpace, /* Size of pSpace[] in bytes */ | |
3113 char **ppFree /* OUT: Caller should free this pointer */ | |
3114 ){ | |
3115 UnpackedRecord *p; /* Unpacked record to return */ | |
3116 int nOff; /* Increment pSpace by nOff to align it */ | |
3117 int nByte; /* Number of bytes required for *p */ | |
3118 | |
3119 /* We want to shift the pointer pSpace up such that it is 8-byte aligned. | |
3120 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift | |
3121 ** it by. If pSpace is already 8-byte aligned, nOff should be zero. | |
3122 */ | |
3123 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; | |
3124 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); | |
3125 if( nByte>szSpace+nOff ){ | |
3126 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); | |
3127 *ppFree = (char *)p; | |
3128 if( !p ) return 0; | |
3129 }else{ | |
3130 p = (UnpackedRecord*)&pSpace[nOff]; | |
3131 *ppFree = 0; | |
3132 } | |
3133 | |
3134 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; | |
3135 assert( pKeyInfo->aSortOrder!=0 ); | |
3136 p->pKeyInfo = pKeyInfo; | |
3137 p->nField = pKeyInfo->nField + 1; | |
3138 return p; | |
3139 } | |
3140 | |
3141 /* | |
3142 ** Given the nKey-byte encoding of a record in pKey[], populate the | |
3143 ** UnpackedRecord structure indicated by the fourth argument with the | |
3144 ** contents of the decoded record. | |
3145 */ | |
3146 void sqlite3VdbeRecordUnpack( | |
3147 KeyInfo *pKeyInfo, /* Information about the record format */ | |
3148 int nKey, /* Size of the binary record */ | |
3149 const void *pKey, /* The binary record */ | |
3150 UnpackedRecord *p /* Populate this structure before returning. */ | |
3151 ){ | |
3152 const unsigned char *aKey = (const unsigned char *)pKey; | |
3153 int d; | |
3154 u32 idx; /* Offset in aKey[] to read from */ | |
3155 u16 u; /* Unsigned loop counter */ | |
3156 u32 szHdr; | |
3157 Mem *pMem = p->aMem; | |
3158 | |
3159 p->default_rc = 0; | |
3160 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); | |
3161 idx = getVarint32(aKey, szHdr); | |
3162 d = szHdr; | |
3163 u = 0; | |
3164 while( idx<szHdr && d<=nKey ){ | |
3165 u32 serial_type; | |
3166 | |
3167 idx += getVarint32(&aKey[idx], serial_type); | |
3168 pMem->enc = pKeyInfo->enc; | |
3169 pMem->db = pKeyInfo->db; | |
3170 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ | |
3171 pMem->szMalloc = 0; | |
3172 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); | |
3173 pMem++; | |
3174 if( (++u)>=p->nField ) break; | |
3175 } | |
3176 assert( u<=pKeyInfo->nField + 1 ); | |
3177 p->nField = u; | |
3178 } | |
3179 | |
3180 #if SQLITE_DEBUG | |
3181 /* | |
3182 ** This function compares two index or table record keys in the same way | |
3183 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), | |
3184 ** this function deserializes and compares values using the | |
3185 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used | |
3186 ** in assert() statements to ensure that the optimized code in | |
3187 ** sqlite3VdbeRecordCompare() returns results with these two primitives. | |
3188 ** | |
3189 ** Return true if the result of comparison is equivalent to desiredResult. | |
3190 ** Return false if there is a disagreement. | |
3191 */ | |
3192 static int vdbeRecordCompareDebug( | |
3193 int nKey1, const void *pKey1, /* Left key */ | |
3194 const UnpackedRecord *pPKey2, /* Right key */ | |
3195 int desiredResult /* Correct answer */ | |
3196 ){ | |
3197 u32 d1; /* Offset into aKey[] of next data element */ | |
3198 u32 idx1; /* Offset into aKey[] of next header element */ | |
3199 u32 szHdr1; /* Number of bytes in header */ | |
3200 int i = 0; | |
3201 int rc = 0; | |
3202 const unsigned char *aKey1 = (const unsigned char *)pKey1; | |
3203 KeyInfo *pKeyInfo; | |
3204 Mem mem1; | |
3205 | |
3206 pKeyInfo = pPKey2->pKeyInfo; | |
3207 if( pKeyInfo->db==0 ) return 1; | |
3208 mem1.enc = pKeyInfo->enc; | |
3209 mem1.db = pKeyInfo->db; | |
3210 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ | |
3211 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ | |
3212 | |
3213 /* Compilers may complain that mem1.u.i is potentially uninitialized. | |
3214 ** We could initialize it, as shown here, to silence those complaints. | |
3215 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing | |
3216 ** the unnecessary initialization has a measurable negative performance | |
3217 ** impact, since this routine is a very high runner. And so, we choose | |
3218 ** to ignore the compiler warnings and leave this variable uninitialized. | |
3219 */ | |
3220 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ | |
3221 | |
3222 idx1 = getVarint32(aKey1, szHdr1); | |
3223 d1 = szHdr1; | |
3224 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); | |
3225 assert( pKeyInfo->aSortOrder!=0 ); | |
3226 assert( pKeyInfo->nField>0 ); | |
3227 assert( idx1<=szHdr1 || CORRUPT_DB ); | |
3228 do{ | |
3229 u32 serial_type1; | |
3230 | |
3231 /* Read the serial types for the next element in each key. */ | |
3232 idx1 += getVarint32( aKey1+idx1, serial_type1 ); | |
3233 | |
3234 /* Verify that there is enough key space remaining to avoid | |
3235 ** a buffer overread. The "d1+serial_type1+2" subexpression will | |
3236 ** always be greater than or equal to the amount of required key space. | |
3237 ** Use that approximation to avoid the more expensive call to | |
3238 ** sqlite3VdbeSerialTypeLen() in the common case. | |
3239 */ | |
3240 if( d1+serial_type1+2>(u32)nKey1 | |
3241 && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 | |
3242 ){ | |
3243 break; | |
3244 } | |
3245 | |
3246 /* Extract the values to be compared. | |
3247 */ | |
3248 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); | |
3249 | |
3250 /* Do the comparison | |
3251 */ | |
3252 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]); | |
3253 if( rc!=0 ){ | |
3254 assert( mem1.szMalloc==0 ); /* See comment below */ | |
3255 if( pKeyInfo->aSortOrder[i] ){ | |
3256 rc = -rc; /* Invert the result for DESC sort order. */ | |
3257 } | |
3258 goto debugCompareEnd; | |
3259 } | |
3260 i++; | |
3261 }while( idx1<szHdr1 && i<pPKey2->nField ); | |
3262 | |
3263 /* No memory allocation is ever used on mem1. Prove this using | |
3264 ** the following assert(). If the assert() fails, it indicates a | |
3265 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). | |
3266 */ | |
3267 assert( mem1.szMalloc==0 ); | |
3268 | |
3269 /* rc==0 here means that one of the keys ran out of fields and | |
3270 ** all the fields up to that point were equal. Return the default_rc | |
3271 ** value. */ | |
3272 rc = pPKey2->default_rc; | |
3273 | |
3274 debugCompareEnd: | |
3275 if( desiredResult==0 && rc==0 ) return 1; | |
3276 if( desiredResult<0 && rc<0 ) return 1; | |
3277 if( desiredResult>0 && rc>0 ) return 1; | |
3278 if( CORRUPT_DB ) return 1; | |
3279 if( pKeyInfo->db->mallocFailed ) return 1; | |
3280 return 0; | |
3281 } | |
3282 #endif | |
3283 | |
3284 /* | |
3285 ** Both *pMem1 and *pMem2 contain string values. Compare the two values | |
3286 ** using the collation sequence pColl. As usual, return a negative , zero | |
3287 ** or positive value if *pMem1 is less than, equal to or greater than | |
3288 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". | |
3289 */ | |
3290 static int vdbeCompareMemString( | |
3291 const Mem *pMem1, | |
3292 const Mem *pMem2, | |
3293 const CollSeq *pColl, | |
3294 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ | |
3295 ){ | |
3296 if( pMem1->enc==pColl->enc ){ | |
3297 /* The strings are already in the correct encoding. Call the | |
3298 ** comparison function directly */ | |
3299 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); | |
3300 }else{ | |
3301 int rc; | |
3302 const void *v1, *v2; | |
3303 int n1, n2; | |
3304 Mem c1; | |
3305 Mem c2; | |
3306 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); | |
3307 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); | |
3308 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); | |
3309 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); | |
3310 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); | |
3311 n1 = v1==0 ? 0 : c1.n; | |
3312 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); | |
3313 n2 = v2==0 ? 0 : c2.n; | |
3314 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); | |
3315 sqlite3VdbeMemRelease(&c1); | |
3316 sqlite3VdbeMemRelease(&c2); | |
3317 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM; | |
3318 return rc; | |
3319 } | |
3320 } | |
3321 | |
3322 /* | |
3323 ** Compare two blobs. Return negative, zero, or positive if the first | |
3324 ** is less than, equal to, or greater than the second, respectively. | |
3325 ** If one blob is a prefix of the other, then the shorter is the lessor. | |
3326 */ | |
3327 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ | |
3328 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); | |
3329 if( c ) return c; | |
3330 return pB1->n - pB2->n; | |
3331 } | |
3332 | |
3333 | |
3334 /* | |
3335 ** Compare the values contained by the two memory cells, returning | |
3336 ** negative, zero or positive if pMem1 is less than, equal to, or greater | |
3337 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers | |
3338 ** and reals) sorted numerically, followed by text ordered by the collating | |
3339 ** sequence pColl and finally blob's ordered by memcmp(). | |
3340 ** | |
3341 ** Two NULL values are considered equal by this function. | |
3342 */ | |
3343 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ | |
3344 int f1, f2; | |
3345 int combined_flags; | |
3346 | |
3347 f1 = pMem1->flags; | |
3348 f2 = pMem2->flags; | |
3349 combined_flags = f1|f2; | |
3350 assert( (combined_flags & MEM_RowSet)==0 ); | |
3351 | |
3352 /* If one value is NULL, it is less than the other. If both values | |
3353 ** are NULL, return 0. | |
3354 */ | |
3355 if( combined_flags&MEM_Null ){ | |
3356 return (f2&MEM_Null) - (f1&MEM_Null); | |
3357 } | |
3358 | |
3359 /* If one value is a number and the other is not, the number is less. | |
3360 ** If both are numbers, compare as reals if one is a real, or as integers | |
3361 ** if both values are integers. | |
3362 */ | |
3363 if( combined_flags&(MEM_Int|MEM_Real) ){ | |
3364 double r1, r2; | |
3365 if( (f1 & f2 & MEM_Int)!=0 ){ | |
3366 if( pMem1->u.i < pMem2->u.i ) return -1; | |
3367 if( pMem1->u.i > pMem2->u.i ) return 1; | |
3368 return 0; | |
3369 } | |
3370 if( (f1&MEM_Real)!=0 ){ | |
3371 r1 = pMem1->u.r; | |
3372 }else if( (f1&MEM_Int)!=0 ){ | |
3373 r1 = (double)pMem1->u.i; | |
3374 }else{ | |
3375 return 1; | |
3376 } | |
3377 if( (f2&MEM_Real)!=0 ){ | |
3378 r2 = pMem2->u.r; | |
3379 }else if( (f2&MEM_Int)!=0 ){ | |
3380 r2 = (double)pMem2->u.i; | |
3381 }else{ | |
3382 return -1; | |
3383 } | |
3384 if( r1<r2 ) return -1; | |
3385 if( r1>r2 ) return 1; | |
3386 return 0; | |
3387 } | |
3388 | |
3389 /* If one value is a string and the other is a blob, the string is less. | |
3390 ** If both are strings, compare using the collating functions. | |
3391 */ | |
3392 if( combined_flags&MEM_Str ){ | |
3393 if( (f1 & MEM_Str)==0 ){ | |
3394 return 1; | |
3395 } | |
3396 if( (f2 & MEM_Str)==0 ){ | |
3397 return -1; | |
3398 } | |
3399 | |
3400 assert( pMem1->enc==pMem2->enc ); | |
3401 assert( pMem1->enc==SQLITE_UTF8 || | |
3402 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); | |
3403 | |
3404 /* The collation sequence must be defined at this point, even if | |
3405 ** the user deletes the collation sequence after the vdbe program is | |
3406 ** compiled (this was not always the case). | |
3407 */ | |
3408 assert( !pColl || pColl->xCmp ); | |
3409 | |
3410 if( pColl ){ | |
3411 return vdbeCompareMemString(pMem1, pMem2, pColl, 0); | |
3412 } | |
3413 /* If a NULL pointer was passed as the collate function, fall through | |
3414 ** to the blob case and use memcmp(). */ | |
3415 } | |
3416 | |
3417 /* Both values must be blobs. Compare using memcmp(). */ | |
3418 return sqlite3BlobCompare(pMem1, pMem2); | |
3419 } | |
3420 | |
3421 | |
3422 /* | |
3423 ** The first argument passed to this function is a serial-type that | |
3424 ** corresponds to an integer - all values between 1 and 9 inclusive | |
3425 ** except 7. The second points to a buffer containing an integer value | |
3426 ** serialized according to serial_type. This function deserializes | |
3427 ** and returns the value. | |
3428 */ | |
3429 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ | |
3430 u32 y; | |
3431 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); | |
3432 switch( serial_type ){ | |
3433 case 0: | |
3434 case 1: | |
3435 testcase( aKey[0]&0x80 ); | |
3436 return ONE_BYTE_INT(aKey); | |
3437 case 2: | |
3438 testcase( aKey[0]&0x80 ); | |
3439 return TWO_BYTE_INT(aKey); | |
3440 case 3: | |
3441 testcase( aKey[0]&0x80 ); | |
3442 return THREE_BYTE_INT(aKey); | |
3443 case 4: { | |
3444 testcase( aKey[0]&0x80 ); | |
3445 y = FOUR_BYTE_UINT(aKey); | |
3446 return (i64)*(int*)&y; | |
3447 } | |
3448 case 5: { | |
3449 testcase( aKey[0]&0x80 ); | |
3450 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); | |
3451 } | |
3452 case 6: { | |
3453 u64 x = FOUR_BYTE_UINT(aKey); | |
3454 testcase( aKey[0]&0x80 ); | |
3455 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); | |
3456 return (i64)*(i64*)&x; | |
3457 } | |
3458 } | |
3459 | |
3460 return (serial_type - 8); | |
3461 } | |
3462 | |
3463 /* | |
3464 ** This function compares the two table rows or index records | |
3465 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero | |
3466 ** or positive integer if key1 is less than, equal to or | |
3467 ** greater than key2. The {nKey1, pKey1} key must be a blob | |
3468 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 | |
3469 ** key must be a parsed key such as obtained from | |
3470 ** sqlite3VdbeParseRecord. | |
3471 ** | |
3472 ** If argument bSkip is non-zero, it is assumed that the caller has already | |
3473 ** determined that the first fields of the keys are equal. | |
3474 ** | |
3475 ** Key1 and Key2 do not have to contain the same number of fields. If all | |
3476 ** fields that appear in both keys are equal, then pPKey2->default_rc is | |
3477 ** returned. | |
3478 ** | |
3479 ** If database corruption is discovered, set pPKey2->errCode to | |
3480 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, | |
3481 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the | |
3482 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). | |
3483 */ | |
3484 static int vdbeRecordCompareWithSkip( | |
3485 int nKey1, const void *pKey1, /* Left key */ | |
3486 UnpackedRecord *pPKey2, /* Right key */ | |
3487 int bSkip /* If true, skip the first field */ | |
3488 ){ | |
3489 u32 d1; /* Offset into aKey[] of next data element */ | |
3490 int i; /* Index of next field to compare */ | |
3491 u32 szHdr1; /* Size of record header in bytes */ | |
3492 u32 idx1; /* Offset of first type in header */ | |
3493 int rc = 0; /* Return value */ | |
3494 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ | |
3495 KeyInfo *pKeyInfo = pPKey2->pKeyInfo; | |
3496 const unsigned char *aKey1 = (const unsigned char *)pKey1; | |
3497 Mem mem1; | |
3498 | |
3499 /* If bSkip is true, then the caller has already determined that the first | |
3500 ** two elements in the keys are equal. Fix the various stack variables so | |
3501 ** that this routine begins comparing at the second field. */ | |
3502 if( bSkip ){ | |
3503 u32 s1; | |
3504 idx1 = 1 + getVarint32(&aKey1[1], s1); | |
3505 szHdr1 = aKey1[0]; | |
3506 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); | |
3507 i = 1; | |
3508 pRhs++; | |
3509 }else{ | |
3510 idx1 = getVarint32(aKey1, szHdr1); | |
3511 d1 = szHdr1; | |
3512 if( d1>(unsigned)nKey1 ){ | |
3513 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | |
3514 return 0; /* Corruption */ | |
3515 } | |
3516 i = 0; | |
3517 } | |
3518 | |
3519 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ | |
3520 assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField | |
3521 || CORRUPT_DB ); | |
3522 assert( pPKey2->pKeyInfo->aSortOrder!=0 ); | |
3523 assert( pPKey2->pKeyInfo->nField>0 ); | |
3524 assert( idx1<=szHdr1 || CORRUPT_DB ); | |
3525 do{ | |
3526 u32 serial_type; | |
3527 | |
3528 /* RHS is an integer */ | |
3529 if( pRhs->flags & MEM_Int ){ | |
3530 serial_type = aKey1[idx1]; | |
3531 testcase( serial_type==12 ); | |
3532 if( serial_type>=12 ){ | |
3533 rc = +1; | |
3534 }else if( serial_type==0 ){ | |
3535 rc = -1; | |
3536 }else if( serial_type==7 ){ | |
3537 double rhs = (double)pRhs->u.i; | |
3538 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); | |
3539 if( mem1.u.r<rhs ){ | |
3540 rc = -1; | |
3541 }else if( mem1.u.r>rhs ){ | |
3542 rc = +1; | |
3543 } | |
3544 }else{ | |
3545 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); | |
3546 i64 rhs = pRhs->u.i; | |
3547 if( lhs<rhs ){ | |
3548 rc = -1; | |
3549 }else if( lhs>rhs ){ | |
3550 rc = +1; | |
3551 } | |
3552 } | |
3553 } | |
3554 | |
3555 /* RHS is real */ | |
3556 else if( pRhs->flags & MEM_Real ){ | |
3557 serial_type = aKey1[idx1]; | |
3558 if( serial_type>=12 ){ | |
3559 rc = +1; | |
3560 }else if( serial_type==0 ){ | |
3561 rc = -1; | |
3562 }else{ | |
3563 double rhs = pRhs->u.r; | |
3564 double lhs; | |
3565 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); | |
3566 if( serial_type==7 ){ | |
3567 lhs = mem1.u.r; | |
3568 }else{ | |
3569 lhs = (double)mem1.u.i; | |
3570 } | |
3571 if( lhs<rhs ){ | |
3572 rc = -1; | |
3573 }else if( lhs>rhs ){ | |
3574 rc = +1; | |
3575 } | |
3576 } | |
3577 } | |
3578 | |
3579 /* RHS is a string */ | |
3580 else if( pRhs->flags & MEM_Str ){ | |
3581 getVarint32(&aKey1[idx1], serial_type); | |
3582 testcase( serial_type==12 ); | |
3583 if( serial_type<12 ){ | |
3584 rc = -1; | |
3585 }else if( !(serial_type & 0x01) ){ | |
3586 rc = +1; | |
3587 }else{ | |
3588 mem1.n = (serial_type - 12) / 2; | |
3589 testcase( (d1+mem1.n)==(unsigned)nKey1 ); | |
3590 testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); | |
3591 if( (d1+mem1.n) > (unsigned)nKey1 ){ | |
3592 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | |
3593 return 0; /* Corruption */ | |
3594 }else if( pKeyInfo->aColl[i] ){ | |
3595 mem1.enc = pKeyInfo->enc; | |
3596 mem1.db = pKeyInfo->db; | |
3597 mem1.flags = MEM_Str; | |
3598 mem1.z = (char*)&aKey1[d1]; | |
3599 rc = vdbeCompareMemString( | |
3600 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode | |
3601 ); | |
3602 }else{ | |
3603 int nCmp = MIN(mem1.n, pRhs->n); | |
3604 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); | |
3605 if( rc==0 ) rc = mem1.n - pRhs->n; | |
3606 } | |
3607 } | |
3608 } | |
3609 | |
3610 /* RHS is a blob */ | |
3611 else if( pRhs->flags & MEM_Blob ){ | |
3612 getVarint32(&aKey1[idx1], serial_type); | |
3613 testcase( serial_type==12 ); | |
3614 if( serial_type<12 || (serial_type & 0x01) ){ | |
3615 rc = -1; | |
3616 }else{ | |
3617 int nStr = (serial_type - 12) / 2; | |
3618 testcase( (d1+nStr)==(unsigned)nKey1 ); | |
3619 testcase( (d1+nStr+1)==(unsigned)nKey1 ); | |
3620 if( (d1+nStr) > (unsigned)nKey1 ){ | |
3621 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | |
3622 return 0; /* Corruption */ | |
3623 }else{ | |
3624 int nCmp = MIN(nStr, pRhs->n); | |
3625 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); | |
3626 if( rc==0 ) rc = nStr - pRhs->n; | |
3627 } | |
3628 } | |
3629 } | |
3630 | |
3631 /* RHS is null */ | |
3632 else{ | |
3633 serial_type = aKey1[idx1]; | |
3634 rc = (serial_type!=0); | |
3635 } | |
3636 | |
3637 if( rc!=0 ){ | |
3638 if( pKeyInfo->aSortOrder[i] ){ | |
3639 rc = -rc; | |
3640 } | |
3641 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); | |
3642 assert( mem1.szMalloc==0 ); /* See comment below */ | |
3643 return rc; | |
3644 } | |
3645 | |
3646 i++; | |
3647 pRhs++; | |
3648 d1 += sqlite3VdbeSerialTypeLen(serial_type); | |
3649 idx1 += sqlite3VarintLen(serial_type); | |
3650 }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 ); | |
3651 | |
3652 /* No memory allocation is ever used on mem1. Prove this using | |
3653 ** the following assert(). If the assert() fails, it indicates a | |
3654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ | |
3655 assert( mem1.szMalloc==0 ); | |
3656 | |
3657 /* rc==0 here means that one or both of the keys ran out of fields and | |
3658 ** all the fields up to that point were equal. Return the default_rc | |
3659 ** value. */ | |
3660 assert( CORRUPT_DB | |
3661 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) | |
3662 || pKeyInfo->db->mallocFailed | |
3663 ); | |
3664 return pPKey2->default_rc; | |
3665 } | |
3666 int sqlite3VdbeRecordCompare( | |
3667 int nKey1, const void *pKey1, /* Left key */ | |
3668 UnpackedRecord *pPKey2 /* Right key */ | |
3669 ){ | |
3670 return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); | |
3671 } | |
3672 | |
3673 | |
3674 /* | |
3675 ** This function is an optimized version of sqlite3VdbeRecordCompare() | |
3676 ** that (a) the first field of pPKey2 is an integer, and (b) the | |
3677 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single | |
3678 ** byte (i.e. is less than 128). | |
3679 ** | |
3680 ** To avoid concerns about buffer overreads, this routine is only used | |
3681 ** on schemas where the maximum valid header size is 63 bytes or less. | |
3682 */ | |
3683 static int vdbeRecordCompareInt( | |
3684 int nKey1, const void *pKey1, /* Left key */ | |
3685 UnpackedRecord *pPKey2 /* Right key */ | |
3686 ){ | |
3687 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; | |
3688 int serial_type = ((const u8*)pKey1)[1]; | |
3689 int res; | |
3690 u32 y; | |
3691 u64 x; | |
3692 i64 v = pPKey2->aMem[0].u.i; | |
3693 i64 lhs; | |
3694 | |
3695 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); | |
3696 switch( serial_type ){ | |
3697 case 1: { /* 1-byte signed integer */ | |
3698 lhs = ONE_BYTE_INT(aKey); | |
3699 testcase( lhs<0 ); | |
3700 break; | |
3701 } | |
3702 case 2: { /* 2-byte signed integer */ | |
3703 lhs = TWO_BYTE_INT(aKey); | |
3704 testcase( lhs<0 ); | |
3705 break; | |
3706 } | |
3707 case 3: { /* 3-byte signed integer */ | |
3708 lhs = THREE_BYTE_INT(aKey); | |
3709 testcase( lhs<0 ); | |
3710 break; | |
3711 } | |
3712 case 4: { /* 4-byte signed integer */ | |
3713 y = FOUR_BYTE_UINT(aKey); | |
3714 lhs = (i64)*(int*)&y; | |
3715 testcase( lhs<0 ); | |
3716 break; | |
3717 } | |
3718 case 5: { /* 6-byte signed integer */ | |
3719 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); | |
3720 testcase( lhs<0 ); | |
3721 break; | |
3722 } | |
3723 case 6: { /* 8-byte signed integer */ | |
3724 x = FOUR_BYTE_UINT(aKey); | |
3725 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); | |
3726 lhs = *(i64*)&x; | |
3727 testcase( lhs<0 ); | |
3728 break; | |
3729 } | |
3730 case 8: | |
3731 lhs = 0; | |
3732 break; | |
3733 case 9: | |
3734 lhs = 1; | |
3735 break; | |
3736 | |
3737 /* This case could be removed without changing the results of running | |
3738 ** this code. Including it causes gcc to generate a faster switch | |
3739 ** statement (since the range of switch targets now starts at zero and | |
3740 ** is contiguous) but does not cause any duplicate code to be generated | |
3741 ** (as gcc is clever enough to combine the two like cases). Other | |
3742 ** compilers might be similar. */ | |
3743 case 0: case 7: | |
3744 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); | |
3745 | |
3746 default: | |
3747 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); | |
3748 } | |
3749 | |
3750 if( v>lhs ){ | |
3751 res = pPKey2->r1; | |
3752 }else if( v<lhs ){ | |
3753 res = pPKey2->r2; | |
3754 }else if( pPKey2->nField>1 ){ | |
3755 /* The first fields of the two keys are equal. Compare the trailing | |
3756 ** fields. */ | |
3757 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); | |
3758 }else{ | |
3759 /* The first fields of the two keys are equal and there are no trailing | |
3760 ** fields. Return pPKey2->default_rc in this case. */ | |
3761 res = pPKey2->default_rc; | |
3762 } | |
3763 | |
3764 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); | |
3765 return res; | |
3766 } | |
3767 | |
3768 /* | |
3769 ** This function is an optimized version of sqlite3VdbeRecordCompare() | |
3770 ** that (a) the first field of pPKey2 is a string, that (b) the first field | |
3771 ** uses the collation sequence BINARY and (c) that the size-of-header varint | |
3772 ** at the start of (pKey1/nKey1) fits in a single byte. | |
3773 */ | |
3774 static int vdbeRecordCompareString( | |
3775 int nKey1, const void *pKey1, /* Left key */ | |
3776 UnpackedRecord *pPKey2 /* Right key */ | |
3777 ){ | |
3778 const u8 *aKey1 = (const u8*)pKey1; | |
3779 int serial_type; | |
3780 int res; | |
3781 | |
3782 getVarint32(&aKey1[1], serial_type); | |
3783 if( serial_type<12 ){ | |
3784 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ | |
3785 }else if( !(serial_type & 0x01) ){ | |
3786 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ | |
3787 }else{ | |
3788 int nCmp; | |
3789 int nStr; | |
3790 int szHdr = aKey1[0]; | |
3791 | |
3792 nStr = (serial_type-12) / 2; | |
3793 if( (szHdr + nStr) > nKey1 ){ | |
3794 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | |
3795 return 0; /* Corruption */ | |
3796 } | |
3797 nCmp = MIN( pPKey2->aMem[0].n, nStr ); | |
3798 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); | |
3799 | |
3800 if( res==0 ){ | |
3801 res = nStr - pPKey2->aMem[0].n; | |
3802 if( res==0 ){ | |
3803 if( pPKey2->nField>1 ){ | |
3804 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); | |
3805 }else{ | |
3806 res = pPKey2->default_rc; | |
3807 } | |
3808 }else if( res>0 ){ | |
3809 res = pPKey2->r2; | |
3810 }else{ | |
3811 res = pPKey2->r1; | |
3812 } | |
3813 }else if( res>0 ){ | |
3814 res = pPKey2->r2; | |
3815 }else{ | |
3816 res = pPKey2->r1; | |
3817 } | |
3818 } | |
3819 | |
3820 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) | |
3821 || CORRUPT_DB | |
3822 || pPKey2->pKeyInfo->db->mallocFailed | |
3823 ); | |
3824 return res; | |
3825 } | |
3826 | |
3827 /* | |
3828 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function | |
3829 ** suitable for comparing serialized records to the unpacked record passed | |
3830 ** as the only argument. | |
3831 */ | |
3832 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ | |
3833 /* varintRecordCompareInt() and varintRecordCompareString() both assume | |
3834 ** that the size-of-header varint that occurs at the start of each record | |
3835 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() | |
3836 ** also assumes that it is safe to overread a buffer by at least the | |
3837 ** maximum possible legal header size plus 8 bytes. Because there is | |
3838 ** guaranteed to be at least 74 (but not 136) bytes of padding following each | |
3839 ** buffer passed to varintRecordCompareInt() this makes it convenient to | |
3840 ** limit the size of the header to 64 bytes in cases where the first field | |
3841 ** is an integer. | |
3842 ** | |
3843 ** The easiest way to enforce this limit is to consider only records with | |
3844 ** 13 fields or less. If the first field is an integer, the maximum legal | |
3845 ** header size is (12*5 + 1 + 1) bytes. */ | |
3846 if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){ | |
3847 int flags = p->aMem[0].flags; | |
3848 if( p->pKeyInfo->aSortOrder[0] ){ | |
3849 p->r1 = 1; | |
3850 p->r2 = -1; | |
3851 }else{ | |
3852 p->r1 = -1; | |
3853 p->r2 = 1; | |
3854 } | |
3855 if( (flags & MEM_Int) ){ | |
3856 return vdbeRecordCompareInt; | |
3857 } | |
3858 testcase( flags & MEM_Real ); | |
3859 testcase( flags & MEM_Null ); | |
3860 testcase( flags & MEM_Blob ); | |
3861 if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){ | |
3862 assert( flags & MEM_Str ); | |
3863 return vdbeRecordCompareString; | |
3864 } | |
3865 } | |
3866 | |
3867 return sqlite3VdbeRecordCompare; | |
3868 } | |
3869 | |
3870 /* | |
3871 ** pCur points at an index entry created using the OP_MakeRecord opcode. | |
3872 ** Read the rowid (the last field in the record) and store it in *rowid. | |
3873 ** Return SQLITE_OK if everything works, or an error code otherwise. | |
3874 ** | |
3875 ** pCur might be pointing to text obtained from a corrupt database file. | |
3876 ** So the content cannot be trusted. Do appropriate checks on the content. | |
3877 */ | |
3878 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ | |
3879 i64 nCellKey = 0; | |
3880 int rc; | |
3881 u32 szHdr; /* Size of the header */ | |
3882 u32 typeRowid; /* Serial type of the rowid */ | |
3883 u32 lenRowid; /* Size of the rowid */ | |
3884 Mem m, v; | |
3885 | |
3886 /* Get the size of the index entry. Only indices entries of less | |
3887 ** than 2GiB are support - anything large must be database corruption. | |
3888 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so | |
3889 ** this code can safely assume that nCellKey is 32-bits | |
3890 */ | |
3891 assert( sqlite3BtreeCursorIsValid(pCur) ); | |
3892 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); | |
3893 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
3894 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); | |
3895 | |
3896 /* Read in the complete content of the index entry */ | |
3897 sqlite3VdbeMemInit(&m, db, 0); | |
3898 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); | |
3899 if( rc ){ | |
3900 return rc; | |
3901 } | |
3902 | |
3903 /* The index entry must begin with a header size */ | |
3904 (void)getVarint32((u8*)m.z, szHdr); | |
3905 testcase( szHdr==3 ); | |
3906 testcase( szHdr==m.n ); | |
3907 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ | |
3908 goto idx_rowid_corruption; | |
3909 } | |
3910 | |
3911 /* The last field of the index should be an integer - the ROWID. | |
3912 ** Verify that the last entry really is an integer. */ | |
3913 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); | |
3914 testcase( typeRowid==1 ); | |
3915 testcase( typeRowid==2 ); | |
3916 testcase( typeRowid==3 ); | |
3917 testcase( typeRowid==4 ); | |
3918 testcase( typeRowid==5 ); | |
3919 testcase( typeRowid==6 ); | |
3920 testcase( typeRowid==8 ); | |
3921 testcase( typeRowid==9 ); | |
3922 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ | |
3923 goto idx_rowid_corruption; | |
3924 } | |
3925 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); | |
3926 testcase( (u32)m.n==szHdr+lenRowid ); | |
3927 if( unlikely((u32)m.n<szHdr+lenRowid) ){ | |
3928 goto idx_rowid_corruption; | |
3929 } | |
3930 | |
3931 /* Fetch the integer off the end of the index record */ | |
3932 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); | |
3933 *rowid = v.u.i; | |
3934 sqlite3VdbeMemRelease(&m); | |
3935 return SQLITE_OK; | |
3936 | |
3937 /* Jump here if database corruption is detected after m has been | |
3938 ** allocated. Free the m object and return SQLITE_CORRUPT. */ | |
3939 idx_rowid_corruption: | |
3940 testcase( m.szMalloc!=0 ); | |
3941 sqlite3VdbeMemRelease(&m); | |
3942 return SQLITE_CORRUPT_BKPT; | |
3943 } | |
3944 | |
3945 /* | |
3946 ** Compare the key of the index entry that cursor pC is pointing to against | |
3947 ** the key string in pUnpacked. Write into *pRes a number | |
3948 ** that is negative, zero, or positive if pC is less than, equal to, | |
3949 ** or greater than pUnpacked. Return SQLITE_OK on success. | |
3950 ** | |
3951 ** pUnpacked is either created without a rowid or is truncated so that it | |
3952 ** omits the rowid at the end. The rowid at the end of the index entry | |
3953 ** is ignored as well. Hence, this routine only compares the prefixes | |
3954 ** of the keys prior to the final rowid, not the entire key. | |
3955 */ | |
3956 int sqlite3VdbeIdxKeyCompare( | |
3957 sqlite3 *db, /* Database connection */ | |
3958 VdbeCursor *pC, /* The cursor to compare against */ | |
3959 UnpackedRecord *pUnpacked, /* Unpacked version of key */ | |
3960 int *res /* Write the comparison result here */ | |
3961 ){ | |
3962 i64 nCellKey = 0; | |
3963 int rc; | |
3964 BtCursor *pCur = pC->pCursor; | |
3965 Mem m; | |
3966 | |
3967 assert( sqlite3BtreeCursorIsValid(pCur) ); | |
3968 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); | |
3969 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
3970 /* nCellKey will always be between 0 and 0xffffffff because of the way | |
3971 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ | |
3972 if( nCellKey<=0 || nCellKey>0x7fffffff ){ | |
3973 *res = 0; | |
3974 return SQLITE_CORRUPT_BKPT; | |
3975 } | |
3976 sqlite3VdbeMemInit(&m, db, 0); | |
3977 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); | |
3978 if( rc ){ | |
3979 return rc; | |
3980 } | |
3981 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); | |
3982 sqlite3VdbeMemRelease(&m); | |
3983 return SQLITE_OK; | |
3984 } | |
3985 | |
3986 /* | |
3987 ** This routine sets the value to be returned by subsequent calls to | |
3988 ** sqlite3_changes() on the database handle 'db'. | |
3989 */ | |
3990 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ | |
3991 assert( sqlite3_mutex_held(db->mutex) ); | |
3992 db->nChange = nChange; | |
3993 db->nTotalChange += nChange; | |
3994 } | |
3995 | |
3996 /* | |
3997 ** Set a flag in the vdbe to update the change counter when it is finalised | |
3998 ** or reset. | |
3999 */ | |
4000 void sqlite3VdbeCountChanges(Vdbe *v){ | |
4001 v->changeCntOn = 1; | |
4002 } | |
4003 | |
4004 /* | |
4005 ** Mark every prepared statement associated with a database connection | |
4006 ** as expired. | |
4007 ** | |
4008 ** An expired statement means that recompilation of the statement is | |
4009 ** recommend. Statements expire when things happen that make their | |
4010 ** programs obsolete. Removing user-defined functions or collating | |
4011 ** sequences, or changing an authorization function are the types of | |
4012 ** things that make prepared statements obsolete. | |
4013 */ | |
4014 void sqlite3ExpirePreparedStatements(sqlite3 *db){ | |
4015 Vdbe *p; | |
4016 for(p = db->pVdbe; p; p=p->pNext){ | |
4017 p->expired = 1; | |
4018 } | |
4019 } | |
4020 | |
4021 /* | |
4022 ** Return the database associated with the Vdbe. | |
4023 */ | |
4024 sqlite3 *sqlite3VdbeDb(Vdbe *v){ | |
4025 return v->db; | |
4026 } | |
4027 | |
4028 /* | |
4029 ** Return a pointer to an sqlite3_value structure containing the value bound | |
4030 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return | |
4031 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* | |
4032 ** constants) to the value before returning it. | |
4033 ** | |
4034 ** The returned value must be freed by the caller using sqlite3ValueFree(). | |
4035 */ | |
4036 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ | |
4037 assert( iVar>0 ); | |
4038 if( v ){ | |
4039 Mem *pMem = &v->aVar[iVar-1]; | |
4040 if( 0==(pMem->flags & MEM_Null) ){ | |
4041 sqlite3_value *pRet = sqlite3ValueNew(v->db); | |
4042 if( pRet ){ | |
4043 sqlite3VdbeMemCopy((Mem *)pRet, pMem); | |
4044 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); | |
4045 } | |
4046 return pRet; | |
4047 } | |
4048 } | |
4049 return 0; | |
4050 } | |
4051 | |
4052 /* | |
4053 ** Configure SQL variable iVar so that binding a new value to it signals | |
4054 ** to sqlite3_reoptimize() that re-preparing the statement may result | |
4055 ** in a better query plan. | |
4056 */ | |
4057 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ | |
4058 assert( iVar>0 ); | |
4059 if( iVar>32 ){ | |
4060 v->expmask = 0xffffffff; | |
4061 }else{ | |
4062 v->expmask |= ((u32)1 << (iVar-1)); | |
4063 } | |
4064 } | |
4065 | |
4066 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
4067 /* | |
4068 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored | |
4069 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored | |
4070 ** in memory obtained from sqlite3DbMalloc). | |
4071 */ | |
4072 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ | |
4073 sqlite3 *db = p->db; | |
4074 sqlite3DbFree(db, p->zErrMsg); | |
4075 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); | |
4076 sqlite3_free(pVtab->zErrMsg); | |
4077 pVtab->zErrMsg = 0; | |
4078 } | |
4079 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | |
OLD | NEW |