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