|
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.) Prior |
|
14 ** to version 2.8.7, all this code was combined into the vdbe.c source file. |
|
15 ** But that file was getting too big so this subroutines were split out. |
|
16 */ |
|
17 #include "sqliteInt.h" |
|
18 #include "os.h" |
|
19 #include <ctype.h> |
|
20 #include "vdbeInt.h" |
|
21 |
|
22 |
|
23 /* |
|
24 ** When debugging the code generator in a symbolic debugger, one can |
|
25 ** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed |
|
26 ** as they are added to the instruction stream. |
|
27 */ |
|
28 #ifdef SQLITE_DEBUG |
|
29 int sqlite3_vdbe_addop_trace = 0; |
|
30 #endif |
|
31 |
|
32 |
|
33 /* |
|
34 ** Create a new virtual database engine. |
|
35 */ |
|
36 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
|
37 Vdbe *p; |
|
38 p = sqliteMalloc( sizeof(Vdbe) ); |
|
39 if( p==0 ) return 0; |
|
40 p->db = db; |
|
41 if( db->pVdbe ){ |
|
42 db->pVdbe->pPrev = p; |
|
43 } |
|
44 p->pNext = db->pVdbe; |
|
45 p->pPrev = 0; |
|
46 db->pVdbe = p; |
|
47 p->magic = VDBE_MAGIC_INIT; |
|
48 return p; |
|
49 } |
|
50 |
|
51 /* |
|
52 ** Turn tracing on or off |
|
53 */ |
|
54 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
|
55 p->trace = trace; |
|
56 } |
|
57 |
|
58 /* |
|
59 ** Resize the Vdbe.aOp array so that it contains at least N |
|
60 ** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then |
|
61 ** the Vdbe.aOp array will be sized to contain exactly N |
|
62 ** elements. Vdbe.nOpAlloc is set to reflect the new size of |
|
63 ** the array. |
|
64 ** |
|
65 ** If an out-of-memory error occurs while resizing the array, |
|
66 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that |
|
67 ** any opcodes already allocated can be correctly deallocated |
|
68 ** along with the rest of the Vdbe). |
|
69 */ |
|
70 static void resizeOpArray(Vdbe *p, int N){ |
|
71 int runMode = p->magic==VDBE_MAGIC_RUN; |
|
72 if( runMode || p->nOpAlloc<N ){ |
|
73 VdbeOp *pNew; |
|
74 int nNew = N + 100*(!runMode); |
|
75 int oldSize = p->nOpAlloc; |
|
76 pNew = sqliteRealloc(p->aOp, nNew*sizeof(Op)); |
|
77 if( pNew ){ |
|
78 p->nOpAlloc = nNew; |
|
79 p->aOp = pNew; |
|
80 if( nNew>oldSize ){ |
|
81 memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op)); |
|
82 } |
|
83 } |
|
84 } |
|
85 } |
|
86 |
|
87 /* |
|
88 ** Add a new instruction to the list of instructions current in the |
|
89 ** VDBE. Return the address of the new instruction. |
|
90 ** |
|
91 ** Parameters: |
|
92 ** |
|
93 ** p Pointer to the VDBE |
|
94 ** |
|
95 ** op The opcode for this instruction |
|
96 ** |
|
97 ** p1, p2 First two of the three possible operands. |
|
98 ** |
|
99 ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
|
100 ** the sqlite3VdbeChangeP3() function to change the value of the P3 |
|
101 ** operand. |
|
102 */ |
|
103 int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){ |
|
104 int i; |
|
105 VdbeOp *pOp; |
|
106 |
|
107 i = p->nOp; |
|
108 p->nOp++; |
|
109 assert( p->magic==VDBE_MAGIC_INIT ); |
|
110 if( p->nOpAlloc<=i ){ |
|
111 resizeOpArray(p, i+1); |
|
112 if( sqlite3MallocFailed() ){ |
|
113 return 0; |
|
114 } |
|
115 } |
|
116 pOp = &p->aOp[i]; |
|
117 pOp->opcode = op; |
|
118 pOp->p1 = p1; |
|
119 pOp->p2 = p2; |
|
120 pOp->p3 = 0; |
|
121 pOp->p3type = P3_NOTUSED; |
|
122 p->expired = 0; |
|
123 #ifdef SQLITE_DEBUG |
|
124 if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
|
125 #endif |
|
126 return i; |
|
127 } |
|
128 |
|
129 /* |
|
130 ** Add an opcode that includes the p3 value. |
|
131 */ |
|
132 int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){ |
|
133 int addr = sqlite3VdbeAddOp(p, op, p1, p2); |
|
134 sqlite3VdbeChangeP3(p, addr, zP3, p3type); |
|
135 return addr; |
|
136 } |
|
137 |
|
138 /* |
|
139 ** Create a new symbolic label for an instruction that has yet to be |
|
140 ** coded. The symbolic label is really just a negative number. The |
|
141 ** label can be used as the P2 value of an operation. Later, when |
|
142 ** the label is resolved to a specific address, the VDBE will scan |
|
143 ** through its operation list and change all values of P2 which match |
|
144 ** the label into the resolved address. |
|
145 ** |
|
146 ** The VDBE knows that a P2 value is a label because labels are |
|
147 ** always negative and P2 values are suppose to be non-negative. |
|
148 ** Hence, a negative P2 value is a label that has yet to be resolved. |
|
149 ** |
|
150 ** Zero is returned if a malloc() fails. |
|
151 */ |
|
152 int sqlite3VdbeMakeLabel(Vdbe *p){ |
|
153 int i; |
|
154 i = p->nLabel++; |
|
155 assert( p->magic==VDBE_MAGIC_INIT ); |
|
156 if( i>=p->nLabelAlloc ){ |
|
157 p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
|
158 sqliteReallocOrFree((void**)&p->aLabel, |
|
159 p->nLabelAlloc*sizeof(p->aLabel[0])); |
|
160 } |
|
161 if( p->aLabel ){ |
|
162 p->aLabel[i] = -1; |
|
163 } |
|
164 return -1-i; |
|
165 } |
|
166 |
|
167 /* |
|
168 ** Resolve label "x" to be the address of the next instruction to |
|
169 ** be inserted. The parameter "x" must have been obtained from |
|
170 ** a prior call to sqlite3VdbeMakeLabel(). |
|
171 */ |
|
172 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
|
173 int j = -1-x; |
|
174 assert( p->magic==VDBE_MAGIC_INIT ); |
|
175 assert( j>=0 && j<p->nLabel ); |
|
176 if( p->aLabel ){ |
|
177 p->aLabel[j] = p->nOp; |
|
178 } |
|
179 } |
|
180 |
|
181 /* |
|
182 ** Return non-zero if opcode 'op' is guarenteed not to push more values |
|
183 ** onto the VDBE stack than it pops off. |
|
184 */ |
|
185 static int opcodeNoPush(u8 op){ |
|
186 /* The 10 NOPUSH_MASK_n constants are defined in the automatically |
|
187 ** generated header file opcodes.h. Each is a 16-bit bitmask, one |
|
188 ** bit corresponding to each opcode implemented by the virtual |
|
189 ** machine in vdbe.c. The bit is true if the word "no-push" appears |
|
190 ** in a comment on the same line as the "case OP_XXX:" in |
|
191 ** sqlite3VdbeExec() in vdbe.c. |
|
192 ** |
|
193 ** If the bit is true, then the corresponding opcode is guarenteed not |
|
194 ** to grow the stack when it is executed. Otherwise, it may grow the |
|
195 ** stack by at most one entry. |
|
196 ** |
|
197 ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains |
|
198 ** one bit for opcodes 16 to 31, and so on. |
|
199 ** |
|
200 ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h |
|
201 ** because the file is generated by an awk program. Awk manipulates |
|
202 ** all numbers as floating-point and we don't want to risk a rounding |
|
203 ** error if someone builds with an awk that uses (for example) 32-bit |
|
204 ** IEEE floats. |
|
205 */ |
|
206 static const u32 masks[5] = { |
|
207 NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16), |
|
208 NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16), |
|
209 NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16), |
|
210 NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16), |
|
211 NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16) |
|
212 }; |
|
213 assert( op<32*5 ); |
|
214 return (masks[op>>5] & (1<<(op&0x1F))); |
|
215 } |
|
216 |
|
217 #ifndef NDEBUG |
|
218 int sqlite3VdbeOpcodeNoPush(u8 op){ |
|
219 return opcodeNoPush(op); |
|
220 } |
|
221 #endif |
|
222 |
|
223 /* |
|
224 ** Loop through the program looking for P2 values that are negative. |
|
225 ** Each such value is a label. Resolve the label by setting the P2 |
|
226 ** value to its correct non-zero value. |
|
227 ** |
|
228 ** This routine is called once after all opcodes have been inserted. |
|
229 ** |
|
230 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument |
|
231 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by |
|
232 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. |
|
233 ** |
|
234 ** The integer *pMaxStack is set to the maximum number of vdbe stack |
|
235 ** entries that static analysis reveals this program might need. |
|
236 ** |
|
237 ** This routine also does the following optimization: It scans for |
|
238 ** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for |
|
239 ** IdxInsert instructions where P2!=0. If no such instruction is |
|
240 ** found, then every Statement instruction is changed to a Noop. In |
|
241 ** this way, we avoid creating the statement journal file unnecessarily. |
|
242 */ |
|
243 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){ |
|
244 int i; |
|
245 int nMaxArgs = 0; |
|
246 int nMaxStack = p->nOp; |
|
247 Op *pOp; |
|
248 int *aLabel = p->aLabel; |
|
249 int doesStatementRollback = 0; |
|
250 int hasStatementBegin = 0; |
|
251 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
|
252 u8 opcode = pOp->opcode; |
|
253 |
|
254 if( opcode==OP_Function || opcode==OP_AggStep |
|
255 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
256 || opcode==OP_VUpdate |
|
257 #endif |
|
258 ){ |
|
259 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
|
260 }else if( opcode==OP_Halt ){ |
|
261 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ |
|
262 doesStatementRollback = 1; |
|
263 } |
|
264 }else if( opcode==OP_Statement ){ |
|
265 hasStatementBegin = 1; |
|
266 }else if( opcode==OP_VFilter ){ |
|
267 int n; |
|
268 assert( p->nOp - i >= 3 ); |
|
269 assert( pOp[-2].opcode==OP_Integer ); |
|
270 n = pOp[-2].p1; |
|
271 if( n>nMaxArgs ) nMaxArgs = n; |
|
272 } |
|
273 if( opcodeNoPush(opcode) ){ |
|
274 nMaxStack--; |
|
275 } |
|
276 |
|
277 if( pOp->p2>=0 ) continue; |
|
278 assert( -1-pOp->p2<p->nLabel ); |
|
279 pOp->p2 = aLabel[-1-pOp->p2]; |
|
280 } |
|
281 sqliteFree(p->aLabel); |
|
282 p->aLabel = 0; |
|
283 |
|
284 *pMaxFuncArgs = nMaxArgs; |
|
285 *pMaxStack = nMaxStack; |
|
286 |
|
287 /* If we never rollback a statement transaction, then statement |
|
288 ** transactions are not needed. So change every OP_Statement |
|
289 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() |
|
290 ** which can be expensive on some platforms. |
|
291 */ |
|
292 if( hasStatementBegin && !doesStatementRollback ){ |
|
293 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
|
294 if( pOp->opcode==OP_Statement ){ |
|
295 pOp->opcode = OP_Noop; |
|
296 } |
|
297 } |
|
298 } |
|
299 } |
|
300 |
|
301 /* |
|
302 ** Return the address of the next instruction to be inserted. |
|
303 */ |
|
304 int sqlite3VdbeCurrentAddr(Vdbe *p){ |
|
305 assert( p->magic==VDBE_MAGIC_INIT ); |
|
306 return p->nOp; |
|
307 } |
|
308 |
|
309 /* |
|
310 ** Add a whole list of operations to the operation stack. Return the |
|
311 ** address of the first operation added. |
|
312 */ |
|
313 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
|
314 int addr; |
|
315 assert( p->magic==VDBE_MAGIC_INIT ); |
|
316 resizeOpArray(p, p->nOp + nOp); |
|
317 if( sqlite3MallocFailed() ){ |
|
318 return 0; |
|
319 } |
|
320 addr = p->nOp; |
|
321 if( nOp>0 ){ |
|
322 int i; |
|
323 VdbeOpList const *pIn = aOp; |
|
324 for(i=0; i<nOp; i++, pIn++){ |
|
325 int p2 = pIn->p2; |
|
326 VdbeOp *pOut = &p->aOp[i+addr]; |
|
327 pOut->opcode = pIn->opcode; |
|
328 pOut->p1 = pIn->p1; |
|
329 pOut->p2 = p2<0 ? addr + ADDR(p2) : p2; |
|
330 pOut->p3 = pIn->p3; |
|
331 pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED; |
|
332 #ifdef SQLITE_DEBUG |
|
333 if( sqlite3_vdbe_addop_trace ){ |
|
334 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
|
335 } |
|
336 #endif |
|
337 } |
|
338 p->nOp += nOp; |
|
339 } |
|
340 return addr; |
|
341 } |
|
342 |
|
343 /* |
|
344 ** Change the value of the P1 operand for a specific instruction. |
|
345 ** This routine is useful when a large program is loaded from a |
|
346 ** static array using sqlite3VdbeAddOpList but we want to make a |
|
347 ** few minor changes to the program. |
|
348 */ |
|
349 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
|
350 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
351 if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
352 p->aOp[addr].p1 = val; |
|
353 } |
|
354 } |
|
355 |
|
356 /* |
|
357 ** Change the value of the P2 operand for a specific instruction. |
|
358 ** This routine is useful for setting a jump destination. |
|
359 */ |
|
360 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
|
361 assert( val>=0 ); |
|
362 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
363 if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
364 p->aOp[addr].p2 = val; |
|
365 } |
|
366 } |
|
367 |
|
368 /* |
|
369 ** Change the P2 operand of instruction addr so that it points to |
|
370 ** the address of the next instruction to be coded. |
|
371 */ |
|
372 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
|
373 sqlite3VdbeChangeP2(p, addr, p->nOp); |
|
374 } |
|
375 |
|
376 |
|
377 /* |
|
378 ** If the input FuncDef structure is ephemeral, then free it. If |
|
379 ** the FuncDef is not ephermal, then do nothing. |
|
380 */ |
|
381 static void freeEphemeralFunction(FuncDef *pDef){ |
|
382 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
|
383 sqliteFree(pDef); |
|
384 } |
|
385 } |
|
386 |
|
387 /* |
|
388 ** Delete a P3 value if necessary. |
|
389 */ |
|
390 static void freeP3(int p3type, void *p3){ |
|
391 if( p3 ){ |
|
392 switch( p3type ){ |
|
393 case P3_DYNAMIC: |
|
394 case P3_KEYINFO: |
|
395 case P3_KEYINFO_HANDOFF: { |
|
396 sqliteFree(p3); |
|
397 break; |
|
398 } |
|
399 case P3_MPRINTF: { |
|
400 sqlite3_free(p3); |
|
401 break; |
|
402 } |
|
403 case P3_VDBEFUNC: { |
|
404 VdbeFunc *pVdbeFunc = (VdbeFunc *)p3; |
|
405 freeEphemeralFunction(pVdbeFunc->pFunc); |
|
406 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
|
407 sqliteFree(pVdbeFunc); |
|
408 break; |
|
409 } |
|
410 case P3_FUNCDEF: { |
|
411 freeEphemeralFunction((FuncDef*)p3); |
|
412 break; |
|
413 } |
|
414 case P3_MEM: { |
|
415 sqlite3ValueFree((sqlite3_value*)p3); |
|
416 break; |
|
417 } |
|
418 } |
|
419 } |
|
420 } |
|
421 |
|
422 |
|
423 /* |
|
424 ** Change N opcodes starting at addr to No-ops. |
|
425 */ |
|
426 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
|
427 VdbeOp *pOp = &p->aOp[addr]; |
|
428 while( N-- ){ |
|
429 freeP3(pOp->p3type, pOp->p3); |
|
430 memset(pOp, 0, sizeof(pOp[0])); |
|
431 pOp->opcode = OP_Noop; |
|
432 pOp++; |
|
433 } |
|
434 } |
|
435 |
|
436 /* |
|
437 ** Change the value of the P3 operand for a specific instruction. |
|
438 ** This routine is useful when a large program is loaded from a |
|
439 ** static array using sqlite3VdbeAddOpList but we want to make a |
|
440 ** few minor changes to the program. |
|
441 ** |
|
442 ** If n>=0 then the P3 operand is dynamic, meaning that a copy of |
|
443 ** the string is made into memory obtained from sqliteMalloc(). |
|
444 ** A value of n==0 means copy bytes of zP3 up to and including the |
|
445 ** first null byte. If n>0 then copy n+1 bytes of zP3. |
|
446 ** |
|
447 ** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure. |
|
448 ** A copy is made of the KeyInfo structure into memory obtained from |
|
449 ** sqliteMalloc, to be freed when the Vdbe is finalized. |
|
450 ** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure |
|
451 ** stored in memory that the caller has obtained from sqliteMalloc. The |
|
452 ** caller should not free the allocation, it will be freed when the Vdbe is |
|
453 ** finalized. |
|
454 ** |
|
455 ** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points |
|
456 ** to a string or structure that is guaranteed to exist for the lifetime of |
|
457 ** the Vdbe. In these cases we can just copy the pointer. |
|
458 ** |
|
459 ** If addr<0 then change P3 on the most recently inserted instruction. |
|
460 */ |
|
461 void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){ |
|
462 Op *pOp; |
|
463 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
464 if( p==0 || p->aOp==0 || sqlite3MallocFailed() ){ |
|
465 if (n != P3_KEYINFO) { |
|
466 freeP3(n, (void*)*(char**)&zP3); |
|
467 } |
|
468 return; |
|
469 } |
|
470 if( addr<0 || addr>=p->nOp ){ |
|
471 addr = p->nOp - 1; |
|
472 if( addr<0 ) return; |
|
473 } |
|
474 pOp = &p->aOp[addr]; |
|
475 freeP3(pOp->p3type, pOp->p3); |
|
476 pOp->p3 = 0; |
|
477 if( zP3==0 ){ |
|
478 pOp->p3 = 0; |
|
479 pOp->p3type = P3_NOTUSED; |
|
480 }else if( n==P3_KEYINFO ){ |
|
481 KeyInfo *pKeyInfo; |
|
482 int nField, nByte; |
|
483 |
|
484 nField = ((KeyInfo*)zP3)->nField; |
|
485 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
|
486 pKeyInfo = sqliteMallocRaw( nByte ); |
|
487 pOp->p3 = (char*)pKeyInfo; |
|
488 if( pKeyInfo ){ |
|
489 unsigned char *aSortOrder; |
|
490 memcpy(pKeyInfo, zP3, nByte); |
|
491 aSortOrder = pKeyInfo->aSortOrder; |
|
492 if( aSortOrder ){ |
|
493 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
|
494 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
|
495 } |
|
496 pOp->p3type = P3_KEYINFO; |
|
497 }else{ |
|
498 pOp->p3type = P3_NOTUSED; |
|
499 } |
|
500 }else if( n==P3_KEYINFO_HANDOFF ){ |
|
501 pOp->p3 = (char*)zP3; |
|
502 pOp->p3type = P3_KEYINFO; |
|
503 }else if( n<0 ){ |
|
504 pOp->p3 = (char*)zP3; |
|
505 pOp->p3type = n; |
|
506 }else{ |
|
507 if( n==0 ) n = strlen(zP3); |
|
508 pOp->p3 = sqliteStrNDup(zP3, n); |
|
509 pOp->p3type = P3_DYNAMIC; |
|
510 } |
|
511 } |
|
512 |
|
513 #ifndef NDEBUG |
|
514 /* |
|
515 ** Replace the P3 field of the most recently coded instruction with |
|
516 ** comment text. |
|
517 */ |
|
518 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
|
519 va_list ap; |
|
520 assert( p->nOp>0 ); |
|
521 assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 |
|
522 || sqlite3MallocFailed() ); |
|
523 va_start(ap, zFormat); |
|
524 sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(zFormat, ap), P3_DYNAMIC); |
|
525 va_end(ap); |
|
526 } |
|
527 #endif |
|
528 |
|
529 /* |
|
530 ** Return the opcode for a given address. |
|
531 */ |
|
532 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
|
533 assert( p->magic==VDBE_MAGIC_INIT ); |
|
534 assert( addr>=0 && addr<p->nOp ); |
|
535 return &p->aOp[addr]; |
|
536 } |
|
537 |
|
538 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
|
539 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
|
540 /* |
|
541 ** Compute a string that describes the P3 parameter for an opcode. |
|
542 ** Use zTemp for any required temporary buffer space. |
|
543 */ |
|
544 static char *displayP3(Op *pOp, char *zTemp, int nTemp){ |
|
545 char *zP3; |
|
546 assert( nTemp>=20 ); |
|
547 switch( pOp->p3type ){ |
|
548 case P3_KEYINFO: { |
|
549 int i, j; |
|
550 KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3; |
|
551 sprintf(zTemp, "keyinfo(%d", pKeyInfo->nField); |
|
552 i = strlen(zTemp); |
|
553 for(j=0; j<pKeyInfo->nField; j++){ |
|
554 CollSeq *pColl = pKeyInfo->aColl[j]; |
|
555 if( pColl ){ |
|
556 int n = strlen(pColl->zName); |
|
557 if( i+n>nTemp-6 ){ |
|
558 strcpy(&zTemp[i],",..."); |
|
559 break; |
|
560 } |
|
561 zTemp[i++] = ','; |
|
562 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
|
563 zTemp[i++] = '-'; |
|
564 } |
|
565 strcpy(&zTemp[i], pColl->zName); |
|
566 i += n; |
|
567 }else if( i+4<nTemp-6 ){ |
|
568 strcpy(&zTemp[i],",nil"); |
|
569 i += 4; |
|
570 } |
|
571 } |
|
572 zTemp[i++] = ')'; |
|
573 zTemp[i] = 0; |
|
574 assert( i<nTemp ); |
|
575 zP3 = zTemp; |
|
576 break; |
|
577 } |
|
578 case P3_COLLSEQ: { |
|
579 CollSeq *pColl = (CollSeq*)pOp->p3; |
|
580 sprintf(zTemp, "collseq(%.20s)", pColl->zName); |
|
581 zP3 = zTemp; |
|
582 break; |
|
583 } |
|
584 case P3_FUNCDEF: { |
|
585 FuncDef *pDef = (FuncDef*)pOp->p3; |
|
586 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
|
587 zP3 = zTemp; |
|
588 break; |
|
589 } |
|
590 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
591 case P3_VTAB: { |
|
592 sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3; |
|
593 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
|
594 zP3 = zTemp; |
|
595 break; |
|
596 } |
|
597 #endif |
|
598 default: { |
|
599 zP3 = pOp->p3; |
|
600 if( zP3==0 || pOp->opcode==OP_Noop ){ |
|
601 zP3 = ""; |
|
602 } |
|
603 } |
|
604 } |
|
605 assert( zP3!=0 ); |
|
606 return zP3; |
|
607 } |
|
608 #endif |
|
609 |
|
610 |
|
611 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
|
612 /* |
|
613 ** Print a single opcode. This routine is used for debugging only. |
|
614 */ |
|
615 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
|
616 char *zP3; |
|
617 char zPtr[50]; |
|
618 static const char *zFormat1 = "%4d %-13s %4d %4d %s\n"; |
|
619 if( pOut==0 ) pOut = stdout; |
|
620 zP3 = displayP3(pOp, zPtr, sizeof(zPtr)); |
|
621 fprintf(pOut, zFormat1, |
|
622 pc, sqlite3OpcodeNames[pOp->opcode], pOp->p1, pOp->p2, zP3); |
|
623 fflush(pOut); |
|
624 } |
|
625 #endif |
|
626 |
|
627 /* |
|
628 ** Release an array of N Mem elements |
|
629 */ |
|
630 static void releaseMemArray(Mem *p, int N){ |
|
631 if( p ){ |
|
632 while( N-->0 ){ |
|
633 sqlite3VdbeMemRelease(p++); |
|
634 } |
|
635 } |
|
636 } |
|
637 |
|
638 #ifndef SQLITE_OMIT_EXPLAIN |
|
639 /* |
|
640 ** Give a listing of the program in the virtual machine. |
|
641 ** |
|
642 ** The interface is the same as sqlite3VdbeExec(). But instead of |
|
643 ** running the code, it invokes the callback once for each instruction. |
|
644 ** This feature is used to implement "EXPLAIN". |
|
645 */ |
|
646 int sqlite3VdbeList( |
|
647 Vdbe *p /* The VDBE */ |
|
648 ){ |
|
649 sqlite3 *db = p->db; |
|
650 int i; |
|
651 int rc = SQLITE_OK; |
|
652 |
|
653 assert( p->explain ); |
|
654 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
|
655 assert( db->magic==SQLITE_MAGIC_BUSY ); |
|
656 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
|
657 |
|
658 /* Even though this opcode does not put dynamic strings onto the |
|
659 ** the stack, they may become dynamic if the user calls |
|
660 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
|
661 */ |
|
662 if( p->pTos==&p->aStack[4] ){ |
|
663 releaseMemArray(p->aStack, 5); |
|
664 } |
|
665 p->resOnStack = 0; |
|
666 |
|
667 do{ |
|
668 i = p->pc++; |
|
669 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
|
670 if( i>=p->nOp ){ |
|
671 p->rc = SQLITE_OK; |
|
672 rc = SQLITE_DONE; |
|
673 }else if( db->u1.isInterrupted ){ |
|
674 p->rc = SQLITE_INTERRUPT; |
|
675 rc = SQLITE_ERROR; |
|
676 sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0); |
|
677 }else{ |
|
678 Op *pOp = &p->aOp[i]; |
|
679 Mem *pMem = p->aStack; |
|
680 pMem->flags = MEM_Int; |
|
681 pMem->type = SQLITE_INTEGER; |
|
682 pMem->i = i; /* Program counter */ |
|
683 pMem++; |
|
684 |
|
685 pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
|
686 pMem->z = sqlite3OpcodeNames[pOp->opcode]; /* Opcode */ |
|
687 assert( pMem->z!=0 ); |
|
688 pMem->n = strlen(pMem->z); |
|
689 pMem->type = SQLITE_TEXT; |
|
690 pMem->enc = SQLITE_UTF8; |
|
691 pMem++; |
|
692 |
|
693 pMem->flags = MEM_Int; |
|
694 pMem->i = pOp->p1; /* P1 */ |
|
695 pMem->type = SQLITE_INTEGER; |
|
696 pMem++; |
|
697 |
|
698 pMem->flags = MEM_Int; |
|
699 pMem->i = pOp->p2; /* P2 */ |
|
700 pMem->type = SQLITE_INTEGER; |
|
701 pMem++; |
|
702 |
|
703 pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */ |
|
704 pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort)); |
|
705 assert( pMem->z!=0 ); |
|
706 pMem->n = strlen(pMem->z); |
|
707 pMem->type = SQLITE_TEXT; |
|
708 pMem->enc = SQLITE_UTF8; |
|
709 |
|
710 p->nResColumn = 5 - 2*(p->explain-1); |
|
711 p->pTos = pMem; |
|
712 p->rc = SQLITE_OK; |
|
713 p->resOnStack = 1; |
|
714 rc = SQLITE_ROW; |
|
715 } |
|
716 return rc; |
|
717 } |
|
718 #endif /* SQLITE_OMIT_EXPLAIN */ |
|
719 |
|
720 /* |
|
721 ** Print the SQL that was used to generate a VDBE program. |
|
722 */ |
|
723 void sqlite3VdbePrintSql(Vdbe *p){ |
|
724 #ifdef SQLITE_DEBUG |
|
725 int nOp = p->nOp; |
|
726 VdbeOp *pOp; |
|
727 if( nOp<1 ) return; |
|
728 pOp = &p->aOp[nOp-1]; |
|
729 if( pOp->opcode==OP_Noop && pOp->p3!=0 ){ |
|
730 const char *z = pOp->p3; |
|
731 while( isspace(*(u8*)z) ) z++; |
|
732 printf("SQL: [%s]\n", z); |
|
733 } |
|
734 #endif |
|
735 } |
|
736 |
|
737 /* |
|
738 ** Prepare a virtual machine for execution. This involves things such |
|
739 ** as allocating stack space and initializing the program counter. |
|
740 ** After the VDBE has be prepped, it can be executed by one or more |
|
741 ** calls to sqlite3VdbeExec(). |
|
742 ** |
|
743 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to |
|
744 ** VDBE_MAGIC_RUN. |
|
745 */ |
|
746 void sqlite3VdbeMakeReady( |
|
747 Vdbe *p, /* The VDBE */ |
|
748 int nVar, /* Number of '?' see in the SQL statement */ |
|
749 int nMem, /* Number of memory cells to allocate */ |
|
750 int nCursor, /* Number of cursors to allocate */ |
|
751 int isExplain /* True if the EXPLAIN keywords is present */ |
|
752 ){ |
|
753 int n; |
|
754 |
|
755 assert( p!=0 ); |
|
756 assert( p->magic==VDBE_MAGIC_INIT ); |
|
757 |
|
758 /* There should be at least one opcode. |
|
759 */ |
|
760 assert( p->nOp>0 ); |
|
761 |
|
762 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This |
|
763 * is because the call to resizeOpArray() below may shrink the |
|
764 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN |
|
765 * state. |
|
766 */ |
|
767 p->magic = VDBE_MAGIC_RUN; |
|
768 |
|
769 /* No instruction ever pushes more than a single element onto the |
|
770 ** stack. And the stack never grows on successive executions of the |
|
771 ** same loop. So the total number of instructions is an upper bound |
|
772 ** on the maximum stack depth required. (Added later:) The |
|
773 ** resolveP2Values() call computes a tighter upper bound on the |
|
774 ** stack size. |
|
775 ** |
|
776 ** Allocation all the stack space we will ever need. |
|
777 */ |
|
778 if( p->aStack==0 ){ |
|
779 int nArg; /* Maximum number of args passed to a user function. */ |
|
780 int nStack; /* Maximum number of stack entries required */ |
|
781 resolveP2Values(p, &nArg, &nStack); |
|
782 resizeOpArray(p, p->nOp); |
|
783 assert( nVar>=0 ); |
|
784 assert( nStack<p->nOp ); |
|
785 if( isExplain ){ |
|
786 nStack = 10; |
|
787 } |
|
788 p->aStack = sqliteMalloc( |
|
789 nStack*sizeof(p->aStack[0]) /* aStack */ |
|
790 + nArg*sizeof(Mem*) /* apArg */ |
|
791 + nVar*sizeof(Mem) /* aVar */ |
|
792 + nVar*sizeof(char*) /* azVar */ |
|
793 + nMem*sizeof(Mem) /* aMem */ |
|
794 + nCursor*sizeof(Cursor*) /* apCsr */ |
|
795 ); |
|
796 if( !sqlite3MallocFailed() ){ |
|
797 p->aMem = &p->aStack[nStack]; |
|
798 p->nMem = nMem; |
|
799 p->aVar = &p->aMem[nMem]; |
|
800 p->nVar = nVar; |
|
801 p->okVar = 0; |
|
802 p->apArg = (Mem**)&p->aVar[nVar]; |
|
803 p->azVar = (char**)&p->apArg[nArg]; |
|
804 p->apCsr = (Cursor**)&p->azVar[nVar]; |
|
805 p->nCursor = nCursor; |
|
806 for(n=0; n<nVar; n++){ |
|
807 p->aVar[n].flags = MEM_Null; |
|
808 } |
|
809 } |
|
810 } |
|
811 for(n=0; n<p->nMem; n++){ |
|
812 p->aMem[n].flags = MEM_Null; |
|
813 } |
|
814 |
|
815 #ifdef SQLITE_DEBUG |
|
816 if( (p->db->flags & SQLITE_VdbeListing)!=0 |
|
817 || sqlite3OsFileExists("vdbe_explain") |
|
818 ){ |
|
819 int i; |
|
820 printf("VDBE Program Listing:\n"); |
|
821 sqlite3VdbePrintSql(p); |
|
822 for(i=0; i<p->nOp; i++){ |
|
823 sqlite3VdbePrintOp(stdout, i, &p->aOp[i]); |
|
824 } |
|
825 } |
|
826 if( sqlite3OsFileExists("vdbe_trace") ){ |
|
827 p->trace = stdout; |
|
828 } |
|
829 #endif |
|
830 p->pTos = &p->aStack[-1]; |
|
831 p->pc = -1; |
|
832 p->rc = SQLITE_OK; |
|
833 p->uniqueCnt = 0; |
|
834 p->returnDepth = 0; |
|
835 p->errorAction = OE_Abort; |
|
836 p->popStack = 0; |
|
837 p->explain |= isExplain; |
|
838 p->magic = VDBE_MAGIC_RUN; |
|
839 p->nChange = 0; |
|
840 p->cacheCtr = 1; |
|
841 p->minWriteFileFormat = 255; |
|
842 #ifdef VDBE_PROFILE |
|
843 { |
|
844 int i; |
|
845 for(i=0; i<p->nOp; i++){ |
|
846 p->aOp[i].cnt = 0; |
|
847 p->aOp[i].cycles = 0; |
|
848 } |
|
849 } |
|
850 #endif |
|
851 } |
|
852 |
|
853 /* |
|
854 ** Close a cursor and release all the resources that cursor happens |
|
855 ** to hold. |
|
856 */ |
|
857 void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ |
|
858 if( pCx==0 ){ |
|
859 return; |
|
860 } |
|
861 if( pCx->pCursor ){ |
|
862 sqlite3BtreeCloseCursor(pCx->pCursor); |
|
863 } |
|
864 if( pCx->pBt ){ |
|
865 sqlite3BtreeClose(pCx->pBt); |
|
866 } |
|
867 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
868 if( pCx->pVtabCursor ){ |
|
869 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
|
870 const sqlite3_module *pModule = pCx->pModule; |
|
871 p->inVtabMethod = 1; |
|
872 sqlite3SafetyOff(p->db); |
|
873 pModule->xClose(pVtabCursor); |
|
874 sqlite3SafetyOn(p->db); |
|
875 p->inVtabMethod = 0; |
|
876 } |
|
877 #endif |
|
878 sqliteFree(pCx->pData); |
|
879 sqliteFree(pCx->aType); |
|
880 sqliteFree(pCx); |
|
881 } |
|
882 |
|
883 /* |
|
884 ** Close all cursors |
|
885 */ |
|
886 static void closeAllCursors(Vdbe *p){ |
|
887 int i; |
|
888 if( p->apCsr==0 ) return; |
|
889 for(i=0; i<p->nCursor; i++){ |
|
890 if( !p->inVtabMethod || (p->apCsr[i] && !p->apCsr[i]->pVtabCursor) ){ |
|
891 sqlite3VdbeFreeCursor(p, p->apCsr[i]); |
|
892 p->apCsr[i] = 0; |
|
893 } |
|
894 } |
|
895 } |
|
896 |
|
897 /* |
|
898 ** Clean up the VM after execution. |
|
899 ** |
|
900 ** This routine will automatically close any cursors, lists, and/or |
|
901 ** sorters that were left open. It also deletes the values of |
|
902 ** variables in the aVar[] array. |
|
903 */ |
|
904 static void Cleanup(Vdbe *p){ |
|
905 int i; |
|
906 if( p->aStack ){ |
|
907 releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack)); |
|
908 p->pTos = &p->aStack[-1]; |
|
909 } |
|
910 closeAllCursors(p); |
|
911 releaseMemArray(p->aMem, p->nMem); |
|
912 sqlite3VdbeFifoClear(&p->sFifo); |
|
913 if( p->contextStack ){ |
|
914 for(i=0; i<p->contextStackTop; i++){ |
|
915 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); |
|
916 } |
|
917 sqliteFree(p->contextStack); |
|
918 } |
|
919 p->contextStack = 0; |
|
920 p->contextStackDepth = 0; |
|
921 p->contextStackTop = 0; |
|
922 sqliteFree(p->zErrMsg); |
|
923 p->zErrMsg = 0; |
|
924 } |
|
925 |
|
926 /* |
|
927 ** Set the number of result columns that will be returned by this SQL |
|
928 ** statement. This is now set at compile time, rather than during |
|
929 ** execution of the vdbe program so that sqlite3_column_count() can |
|
930 ** be called on an SQL statement before sqlite3_step(). |
|
931 */ |
|
932 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
|
933 Mem *pColName; |
|
934 int n; |
|
935 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
|
936 sqliteFree(p->aColName); |
|
937 n = nResColumn*COLNAME_N; |
|
938 p->nResColumn = nResColumn; |
|
939 p->aColName = pColName = (Mem*)sqliteMalloc( sizeof(Mem)*n ); |
|
940 if( p->aColName==0 ) return; |
|
941 while( n-- > 0 ){ |
|
942 (pColName++)->flags = MEM_Null; |
|
943 } |
|
944 } |
|
945 |
|
946 /* |
|
947 ** Set the name of the idx'th column to be returned by the SQL statement. |
|
948 ** zName must be a pointer to a nul terminated string. |
|
949 ** |
|
950 ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
|
951 ** |
|
952 ** If N==P3_STATIC it means that zName is a pointer to a constant static |
|
953 ** string and we can just copy the pointer. If it is P3_DYNAMIC, then |
|
954 ** the string is freed using sqliteFree() when the vdbe is finished with |
|
955 ** it. Otherwise, N bytes of zName are copied. |
|
956 */ |
|
957 int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ |
|
958 int rc; |
|
959 Mem *pColName; |
|
960 assert( idx<p->nResColumn ); |
|
961 assert( var<COLNAME_N ); |
|
962 if( sqlite3MallocFailed() ) return SQLITE_NOMEM; |
|
963 assert( p->aColName!=0 ); |
|
964 pColName = &(p->aColName[idx+var*p->nResColumn]); |
|
965 if( N==P3_DYNAMIC || N==P3_STATIC ){ |
|
966 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); |
|
967 }else{ |
|
968 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); |
|
969 } |
|
970 if( rc==SQLITE_OK && N==P3_DYNAMIC ){ |
|
971 pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn; |
|
972 pColName->xDel = 0; |
|
973 } |
|
974 return rc; |
|
975 } |
|
976 |
|
977 /* |
|
978 ** A read or write transaction may or may not be active on database handle |
|
979 ** db. If a transaction is active, commit it. If there is a |
|
980 ** write-transaction spanning more than one database file, this routine |
|
981 ** takes care of the master journal trickery. |
|
982 */ |
|
983 static int vdbeCommit(sqlite3 *db){ |
|
984 int i; |
|
985 int nTrans = 0; /* Number of databases with an active write-transaction */ |
|
986 int rc = SQLITE_OK; |
|
987 int needXcommit = 0; |
|
988 |
|
989 /* Before doing anything else, call the xSync() callback for any |
|
990 ** virtual module tables written in this transaction. This has to |
|
991 ** be done before determining whether a master journal file is |
|
992 ** required, as an xSync() callback may add an attached database |
|
993 ** to the transaction. |
|
994 */ |
|
995 rc = sqlite3VtabSync(db, rc); |
|
996 if( rc!=SQLITE_OK ){ |
|
997 return rc; |
|
998 } |
|
999 |
|
1000 /* This loop determines (a) if the commit hook should be invoked and |
|
1001 ** (b) how many database files have open write transactions, not |
|
1002 ** including the temp database. (b) is important because if more than |
|
1003 ** one database file has an open write transaction, a master journal |
|
1004 ** file is required for an atomic commit. |
|
1005 */ |
|
1006 for(i=0; i<db->nDb; i++){ |
|
1007 Btree *pBt = db->aDb[i].pBt; |
|
1008 if( pBt && sqlite3BtreeIsInTrans(pBt) ){ |
|
1009 needXcommit = 1; |
|
1010 if( i!=1 ) nTrans++; |
|
1011 } |
|
1012 } |
|
1013 |
|
1014 /* If there are any write-transactions at all, invoke the commit hook */ |
|
1015 if( needXcommit && db->xCommitCallback ){ |
|
1016 sqlite3SafetyOff(db); |
|
1017 rc = db->xCommitCallback(db->pCommitArg); |
|
1018 sqlite3SafetyOn(db); |
|
1019 if( rc ){ |
|
1020 return SQLITE_CONSTRAINT; |
|
1021 } |
|
1022 } |
|
1023 |
|
1024 /* The simple case - no more than one database file (not counting the |
|
1025 ** TEMP database) has a transaction active. There is no need for the |
|
1026 ** master-journal. |
|
1027 ** |
|
1028 ** If the return value of sqlite3BtreeGetFilename() is a zero length |
|
1029 ** string, it means the main database is :memory:. In that case we do |
|
1030 ** not support atomic multi-file commits, so use the simple case then |
|
1031 ** too. |
|
1032 */ |
|
1033 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ |
|
1034 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
|
1035 Btree *pBt = db->aDb[i].pBt; |
|
1036 if( pBt ){ |
|
1037 rc = sqlite3BtreeSync(pBt, 0); |
|
1038 } |
|
1039 } |
|
1040 |
|
1041 /* Do the commit only if all databases successfully synced */ |
|
1042 if( rc==SQLITE_OK ){ |
|
1043 for(i=0; i<db->nDb; i++){ |
|
1044 Btree *pBt = db->aDb[i].pBt; |
|
1045 if( pBt ){ |
|
1046 sqlite3BtreeCommit(pBt); |
|
1047 } |
|
1048 } |
|
1049 sqlite3VtabCommit(db); |
|
1050 } |
|
1051 } |
|
1052 |
|
1053 /* The complex case - There is a multi-file write-transaction active. |
|
1054 ** This requires a master journal file to ensure the transaction is |
|
1055 ** committed atomicly. |
|
1056 */ |
|
1057 #ifndef SQLITE_OMIT_DISKIO |
|
1058 else{ |
|
1059 int needSync = 0; |
|
1060 char *zMaster = 0; /* File-name for the master journal */ |
|
1061 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
|
1062 OsFile *master = 0; |
|
1063 |
|
1064 /* Select a master journal file name */ |
|
1065 do { |
|
1066 u32 random; |
|
1067 sqliteFree(zMaster); |
|
1068 sqlite3Randomness(sizeof(random), &random); |
|
1069 zMaster = sqlite3MPrintf("%s-mj%08X", zMainFile, random&0x7fffffff); |
|
1070 if( !zMaster ){ |
|
1071 return SQLITE_NOMEM; |
|
1072 } |
|
1073 }while( sqlite3OsFileExists(zMaster) ); |
|
1074 |
|
1075 /* Open the master journal. */ |
|
1076 rc = sqlite3OsOpenExclusive(zMaster, &master, 0); |
|
1077 if( rc!=SQLITE_OK ){ |
|
1078 sqliteFree(zMaster); |
|
1079 return rc; |
|
1080 } |
|
1081 |
|
1082 /* Write the name of each database file in the transaction into the new |
|
1083 ** master journal file. If an error occurs at this point close |
|
1084 ** and delete the master journal file. All the individual journal files |
|
1085 ** still have 'null' as the master journal pointer, so they will roll |
|
1086 ** back independently if a failure occurs. |
|
1087 */ |
|
1088 for(i=0; i<db->nDb; i++){ |
|
1089 Btree *pBt = db->aDb[i].pBt; |
|
1090 if( i==1 ) continue; /* Ignore the TEMP database */ |
|
1091 if( pBt && sqlite3BtreeIsInTrans(pBt) ){ |
|
1092 char const *zFile = sqlite3BtreeGetJournalname(pBt); |
|
1093 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ |
|
1094 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
|
1095 needSync = 1; |
|
1096 } |
|
1097 rc = sqlite3OsWrite(master, zFile, strlen(zFile)+1); |
|
1098 if( rc!=SQLITE_OK ){ |
|
1099 sqlite3OsClose(&master); |
|
1100 sqlite3OsDelete(zMaster); |
|
1101 sqliteFree(zMaster); |
|
1102 return rc; |
|
1103 } |
|
1104 } |
|
1105 } |
|
1106 |
|
1107 |
|
1108 /* Sync the master journal file. Before doing this, open the directory |
|
1109 ** the master journal file is store in so that it gets synced too. |
|
1110 */ |
|
1111 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); |
|
1112 rc = sqlite3OsOpenDirectory(master, zMainFile); |
|
1113 if( rc!=SQLITE_OK || |
|
1114 (needSync && (rc=sqlite3OsSync(master,0))!=SQLITE_OK) ){ |
|
1115 sqlite3OsClose(&master); |
|
1116 sqlite3OsDelete(zMaster); |
|
1117 sqliteFree(zMaster); |
|
1118 return rc; |
|
1119 } |
|
1120 |
|
1121 /* Sync all the db files involved in the transaction. The same call |
|
1122 ** sets the master journal pointer in each individual journal. If |
|
1123 ** an error occurs here, do not delete the master journal file. |
|
1124 ** |
|
1125 ** If the error occurs during the first call to sqlite3BtreeSync(), |
|
1126 ** then there is a chance that the master journal file will be |
|
1127 ** orphaned. But we cannot delete it, in case the master journal |
|
1128 ** file name was written into the journal file before the failure |
|
1129 ** occured. |
|
1130 */ |
|
1131 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
|
1132 Btree *pBt = db->aDb[i].pBt; |
|
1133 if( pBt && sqlite3BtreeIsInTrans(pBt) ){ |
|
1134 rc = sqlite3BtreeSync(pBt, zMaster); |
|
1135 } |
|
1136 } |
|
1137 sqlite3OsClose(&master); |
|
1138 if( rc!=SQLITE_OK ){ |
|
1139 sqliteFree(zMaster); |
|
1140 return rc; |
|
1141 } |
|
1142 |
|
1143 /* Delete the master journal file. This commits the transaction. After |
|
1144 ** doing this the directory is synced again before any individual |
|
1145 ** transaction files are deleted. |
|
1146 */ |
|
1147 rc = sqlite3OsDelete(zMaster); |
|
1148 if( rc ){ |
|
1149 return rc; |
|
1150 } |
|
1151 sqliteFree(zMaster); |
|
1152 zMaster = 0; |
|
1153 rc = sqlite3OsSyncDirectory(zMainFile); |
|
1154 if( rc!=SQLITE_OK ){ |
|
1155 /* This is not good. The master journal file has been deleted, but |
|
1156 ** the directory sync failed. There is no completely safe course of |
|
1157 ** action from here. The individual journals contain the name of the |
|
1158 ** master journal file, but there is no way of knowing if that |
|
1159 ** master journal exists now or if it will exist after the operating |
|
1160 ** system crash that may follow the fsync() failure. |
|
1161 */ |
|
1162 return rc; |
|
1163 } |
|
1164 |
|
1165 /* All files and directories have already been synced, so the following |
|
1166 ** calls to sqlite3BtreeCommit() are only closing files and deleting |
|
1167 ** journals. If something goes wrong while this is happening we don't |
|
1168 ** really care. The integrity of the transaction is already guaranteed, |
|
1169 ** but some stray 'cold' journals may be lying around. Returning an |
|
1170 ** error code won't help matters. |
|
1171 */ |
|
1172 for(i=0; i<db->nDb; i++){ |
|
1173 Btree *pBt = db->aDb[i].pBt; |
|
1174 if( pBt ){ |
|
1175 sqlite3BtreeCommit(pBt); |
|
1176 } |
|
1177 } |
|
1178 sqlite3VtabCommit(db); |
|
1179 } |
|
1180 #endif |
|
1181 |
|
1182 return rc; |
|
1183 } |
|
1184 |
|
1185 /* |
|
1186 ** This routine checks that the sqlite3.activeVdbeCnt count variable |
|
1187 ** matches the number of vdbe's in the list sqlite3.pVdbe that are |
|
1188 ** currently active. An assertion fails if the two counts do not match. |
|
1189 ** This is an internal self-check only - it is not an essential processing |
|
1190 ** step. |
|
1191 ** |
|
1192 ** This is a no-op if NDEBUG is defined. |
|
1193 */ |
|
1194 #ifndef NDEBUG |
|
1195 static void checkActiveVdbeCnt(sqlite3 *db){ |
|
1196 Vdbe *p; |
|
1197 int cnt = 0; |
|
1198 p = db->pVdbe; |
|
1199 while( p ){ |
|
1200 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ |
|
1201 cnt++; |
|
1202 } |
|
1203 p = p->pNext; |
|
1204 } |
|
1205 assert( cnt==db->activeVdbeCnt ); |
|
1206 } |
|
1207 #else |
|
1208 #define checkActiveVdbeCnt(x) |
|
1209 #endif |
|
1210 |
|
1211 /* |
|
1212 ** Find every active VM other than pVdbe and change its status to |
|
1213 ** aborted. This happens when one VM causes a rollback due to an |
|
1214 ** ON CONFLICT ROLLBACK clause (for example). The other VMs must be |
|
1215 ** aborted so that they do not have data rolled out from underneath |
|
1216 ** them leading to a segfault. |
|
1217 */ |
|
1218 void sqlite3AbortOtherActiveVdbes(sqlite3 *db, Vdbe *pExcept){ |
|
1219 Vdbe *pOther; |
|
1220 for(pOther=db->pVdbe; pOther; pOther=pOther->pNext){ |
|
1221 if( pOther==pExcept ) continue; |
|
1222 if( pOther->magic!=VDBE_MAGIC_RUN || pOther->pc<0 ) continue; |
|
1223 checkActiveVdbeCnt(db); |
|
1224 closeAllCursors(pOther); |
|
1225 checkActiveVdbeCnt(db); |
|
1226 pOther->aborted = 1; |
|
1227 } |
|
1228 } |
|
1229 |
|
1230 /* |
|
1231 ** This routine is called the when a VDBE tries to halt. If the VDBE |
|
1232 ** has made changes and is in autocommit mode, then commit those |
|
1233 ** changes. If a rollback is needed, then do the rollback. |
|
1234 ** |
|
1235 ** This routine is the only way to move the state of a VM from |
|
1236 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. |
|
1237 ** |
|
1238 ** Return an error code. If the commit could not complete because of |
|
1239 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it |
|
1240 ** means the close did not happen and needs to be repeated. |
|
1241 */ |
|
1242 int sqlite3VdbeHalt(Vdbe *p){ |
|
1243 sqlite3 *db = p->db; |
|
1244 int i; |
|
1245 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ |
|
1246 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ |
|
1247 |
|
1248 /* This function contains the logic that determines if a statement or |
|
1249 ** transaction will be committed or rolled back as a result of the |
|
1250 ** execution of this virtual machine. |
|
1251 ** |
|
1252 ** Special errors: |
|
1253 ** |
|
1254 ** If an SQLITE_NOMEM error has occured in a statement that writes to |
|
1255 ** the database, then either a statement or transaction must be rolled |
|
1256 ** back to ensure the tree-structures are in a consistent state. A |
|
1257 ** statement transaction is rolled back if one is open, otherwise the |
|
1258 ** entire transaction must be rolled back. |
|
1259 ** |
|
1260 ** If an SQLITE_IOERR error has occured in a statement that writes to |
|
1261 ** the database, then the entire transaction must be rolled back. The |
|
1262 ** I/O error may have caused garbage to be written to the journal |
|
1263 ** file. Were the transaction to continue and eventually be rolled |
|
1264 ** back that garbage might end up in the database file. |
|
1265 ** |
|
1266 ** In both of the above cases, the Vdbe.errorAction variable is |
|
1267 ** ignored. If the sqlite3.autoCommit flag is false and a transaction |
|
1268 ** is rolled back, it will be set to true. |
|
1269 ** |
|
1270 ** Other errors: |
|
1271 ** |
|
1272 ** No error: |
|
1273 ** |
|
1274 */ |
|
1275 |
|
1276 if( sqlite3MallocFailed() ){ |
|
1277 p->rc = SQLITE_NOMEM; |
|
1278 } |
|
1279 if( p->magic!=VDBE_MAGIC_RUN ){ |
|
1280 /* Already halted. Nothing to do. */ |
|
1281 assert( p->magic==VDBE_MAGIC_HALT ); |
|
1282 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1283 closeAllCursors(p); |
|
1284 #endif |
|
1285 return SQLITE_OK; |
|
1286 } |
|
1287 closeAllCursors(p); |
|
1288 checkActiveVdbeCnt(db); |
|
1289 |
|
1290 /* No commit or rollback needed if the program never started */ |
|
1291 if( p->pc>=0 ){ |
|
1292 |
|
1293 /* Check for one of the special errors - SQLITE_NOMEM or SQLITE_IOERR */ |
|
1294 isSpecialError = ((p->rc==SQLITE_NOMEM || p->rc==SQLITE_IOERR)?1:0); |
|
1295 if( isSpecialError ){ |
|
1296 /* This loop does static analysis of the query to see which of the |
|
1297 ** following three categories it falls into: |
|
1298 ** |
|
1299 ** Read-only |
|
1300 ** Query with statement journal |
|
1301 ** Query without statement journal |
|
1302 ** |
|
1303 ** We could do something more elegant than this static analysis (i.e. |
|
1304 ** store the type of query as part of the compliation phase), but |
|
1305 ** handling malloc() or IO failure is a fairly obscure edge case so |
|
1306 ** this is probably easier. Todo: Might be an opportunity to reduce |
|
1307 ** code size a very small amount though... |
|
1308 */ |
|
1309 int isReadOnly = 1; |
|
1310 int isStatement = 0; |
|
1311 assert(p->aOp || p->nOp==0); |
|
1312 for(i=0; i<p->nOp; i++){ |
|
1313 switch( p->aOp[i].opcode ){ |
|
1314 case OP_Transaction: |
|
1315 isReadOnly = 0; |
|
1316 break; |
|
1317 case OP_Statement: |
|
1318 isStatement = 1; |
|
1319 break; |
|
1320 } |
|
1321 } |
|
1322 |
|
1323 /* If the query was read-only, we need do no rollback at all. Otherwise, |
|
1324 ** proceed with the special handling. |
|
1325 */ |
|
1326 if( !isReadOnly ){ |
|
1327 if( p->rc==SQLITE_NOMEM && isStatement ){ |
|
1328 xFunc = sqlite3BtreeRollbackStmt; |
|
1329 }else{ |
|
1330 /* We are forced to roll back the active transaction. Before doing |
|
1331 ** so, abort any other statements this handle currently has active. |
|
1332 */ |
|
1333 sqlite3AbortOtherActiveVdbes(db, p); |
|
1334 sqlite3RollbackAll(db); |
|
1335 db->autoCommit = 1; |
|
1336 } |
|
1337 } |
|
1338 } |
|
1339 |
|
1340 /* If the auto-commit flag is set and this is the only active vdbe, then |
|
1341 ** we do either a commit or rollback of the current transaction. |
|
1342 ** |
|
1343 ** Note: This block also runs if one of the special errors handled |
|
1344 ** above has occured. |
|
1345 */ |
|
1346 if( db->autoCommit && db->activeVdbeCnt==1 ){ |
|
1347 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
|
1348 /* The auto-commit flag is true, and the vdbe program was |
|
1349 ** successful or hit an 'OR FAIL' constraint. This means a commit |
|
1350 ** is required. |
|
1351 */ |
|
1352 int rc = vdbeCommit(db); |
|
1353 if( rc==SQLITE_BUSY ){ |
|
1354 return SQLITE_BUSY; |
|
1355 }else if( rc!=SQLITE_OK ){ |
|
1356 p->rc = rc; |
|
1357 sqlite3RollbackAll(db); |
|
1358 }else{ |
|
1359 sqlite3CommitInternalChanges(db); |
|
1360 } |
|
1361 }else{ |
|
1362 sqlite3RollbackAll(db); |
|
1363 } |
|
1364 }else if( !xFunc ){ |
|
1365 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
|
1366 xFunc = sqlite3BtreeCommitStmt; |
|
1367 }else if( p->errorAction==OE_Abort ){ |
|
1368 xFunc = sqlite3BtreeRollbackStmt; |
|
1369 }else{ |
|
1370 sqlite3AbortOtherActiveVdbes(db, p); |
|
1371 sqlite3RollbackAll(db); |
|
1372 db->autoCommit = 1; |
|
1373 } |
|
1374 } |
|
1375 |
|
1376 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or |
|
1377 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs |
|
1378 ** and the return code is still SQLITE_OK, set the return code to the new |
|
1379 ** error value. |
|
1380 */ |
|
1381 assert(!xFunc || |
|
1382 xFunc==sqlite3BtreeCommitStmt || |
|
1383 xFunc==sqlite3BtreeRollbackStmt |
|
1384 ); |
|
1385 for(i=0; xFunc && i<db->nDb; i++){ |
|
1386 int rc; |
|
1387 Btree *pBt = db->aDb[i].pBt; |
|
1388 if( pBt ){ |
|
1389 rc = xFunc(pBt); |
|
1390 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ |
|
1391 p->rc = rc; |
|
1392 sqlite3SetString(&p->zErrMsg, 0); |
|
1393 } |
|
1394 } |
|
1395 } |
|
1396 |
|
1397 /* If this was an INSERT, UPDATE or DELETE and the statement was committed, |
|
1398 ** set the change counter. |
|
1399 */ |
|
1400 if( p->changeCntOn && p->pc>=0 ){ |
|
1401 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ |
|
1402 sqlite3VdbeSetChanges(db, p->nChange); |
|
1403 }else{ |
|
1404 sqlite3VdbeSetChanges(db, 0); |
|
1405 } |
|
1406 p->nChange = 0; |
|
1407 } |
|
1408 |
|
1409 /* Rollback or commit any schema changes that occurred. */ |
|
1410 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ |
|
1411 sqlite3ResetInternalSchema(db, 0); |
|
1412 db->flags = (db->flags | SQLITE_InternChanges); |
|
1413 } |
|
1414 } |
|
1415 |
|
1416 /* We have successfully halted and closed the VM. Record this fact. */ |
|
1417 if( p->pc>=0 ){ |
|
1418 db->activeVdbeCnt--; |
|
1419 } |
|
1420 p->magic = VDBE_MAGIC_HALT; |
|
1421 checkActiveVdbeCnt(db); |
|
1422 |
|
1423 return SQLITE_OK; |
|
1424 } |
|
1425 |
|
1426 /* |
|
1427 ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
|
1428 ** Write any error messages into *pzErrMsg. Return the result code. |
|
1429 ** |
|
1430 ** After this routine is run, the VDBE should be ready to be executed |
|
1431 ** again. |
|
1432 ** |
|
1433 ** To look at it another way, this routine resets the state of the |
|
1434 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
|
1435 ** VDBE_MAGIC_INIT. |
|
1436 */ |
|
1437 int sqlite3VdbeReset(Vdbe *p){ |
|
1438 if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){ |
|
1439 sqlite3Error(p->db, SQLITE_MISUSE, 0); |
|
1440 return SQLITE_MISUSE; |
|
1441 } |
|
1442 |
|
1443 /* If the VM did not run to completion or if it encountered an |
|
1444 ** error, then it might not have been halted properly. So halt |
|
1445 ** it now. |
|
1446 */ |
|
1447 sqlite3SafetyOn(p->db); |
|
1448 sqlite3VdbeHalt(p); |
|
1449 sqlite3SafetyOff(p->db); |
|
1450 |
|
1451 /* If the VDBE has be run even partially, then transfer the error code |
|
1452 ** and error message from the VDBE into the main database structure. But |
|
1453 ** if the VDBE has just been set to run but has not actually executed any |
|
1454 ** instructions yet, leave the main database error information unchanged. |
|
1455 */ |
|
1456 if( p->pc>=0 ){ |
|
1457 if( p->zErrMsg ){ |
|
1458 sqlite3* db = p->db; |
|
1459 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, sqlite3FreeX); |
|
1460 db->errCode = p->rc; |
|
1461 p->zErrMsg = 0; |
|
1462 }else if( p->rc ){ |
|
1463 sqlite3Error(p->db, p->rc, 0); |
|
1464 }else{ |
|
1465 sqlite3Error(p->db, SQLITE_OK, 0); |
|
1466 } |
|
1467 }else if( p->rc && p->expired ){ |
|
1468 /* The expired flag was set on the VDBE before the first call |
|
1469 ** to sqlite3_step(). For consistency (since sqlite3_step() was |
|
1470 ** called), set the database error in this case as well. |
|
1471 */ |
|
1472 sqlite3Error(p->db, p->rc, 0); |
|
1473 } |
|
1474 |
|
1475 /* Reclaim all memory used by the VDBE |
|
1476 */ |
|
1477 Cleanup(p); |
|
1478 |
|
1479 /* Save profiling information from this VDBE run. |
|
1480 */ |
|
1481 assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack ); |
|
1482 #ifdef VDBE_PROFILE |
|
1483 { |
|
1484 FILE *out = fopen("vdbe_profile.out", "a"); |
|
1485 if( out ){ |
|
1486 int i; |
|
1487 fprintf(out, "---- "); |
|
1488 for(i=0; i<p->nOp; i++){ |
|
1489 fprintf(out, "%02x", p->aOp[i].opcode); |
|
1490 } |
|
1491 fprintf(out, "\n"); |
|
1492 for(i=0; i<p->nOp; i++){ |
|
1493 fprintf(out, "%6d %10lld %8lld ", |
|
1494 p->aOp[i].cnt, |
|
1495 p->aOp[i].cycles, |
|
1496 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
|
1497 ); |
|
1498 sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
|
1499 } |
|
1500 fclose(out); |
|
1501 } |
|
1502 } |
|
1503 #endif |
|
1504 p->magic = VDBE_MAGIC_INIT; |
|
1505 p->aborted = 0; |
|
1506 if( p->rc==SQLITE_SCHEMA ){ |
|
1507 sqlite3ResetInternalSchema(p->db, 0); |
|
1508 } |
|
1509 return p->rc; |
|
1510 } |
|
1511 |
|
1512 /* |
|
1513 ** Clean up and delete a VDBE after execution. Return an integer which is |
|
1514 ** the result code. Write any error message text into *pzErrMsg. |
|
1515 */ |
|
1516 int sqlite3VdbeFinalize(Vdbe *p){ |
|
1517 int rc = SQLITE_OK; |
|
1518 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
|
1519 rc = sqlite3VdbeReset(p); |
|
1520 }else if( p->magic!=VDBE_MAGIC_INIT ){ |
|
1521 return SQLITE_MISUSE; |
|
1522 } |
|
1523 sqlite3VdbeDelete(p); |
|
1524 return rc; |
|
1525 } |
|
1526 |
|
1527 /* |
|
1528 ** Call the destructor for each auxdata entry in pVdbeFunc for which |
|
1529 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 |
|
1530 ** are always destroyed. To destroy all auxdata entries, call this |
|
1531 ** routine with mask==0. |
|
1532 */ |
|
1533 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ |
|
1534 int i; |
|
1535 for(i=0; i<pVdbeFunc->nAux; i++){ |
|
1536 struct AuxData *pAux = &pVdbeFunc->apAux[i]; |
|
1537 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){ |
|
1538 if( pAux->xDelete ){ |
|
1539 pAux->xDelete(pAux->pAux); |
|
1540 } |
|
1541 pAux->pAux = 0; |
|
1542 } |
|
1543 } |
|
1544 } |
|
1545 |
|
1546 /* |
|
1547 ** Delete an entire VDBE. |
|
1548 */ |
|
1549 void sqlite3VdbeDelete(Vdbe *p){ |
|
1550 int i; |
|
1551 if( p==0 ) return; |
|
1552 Cleanup(p); |
|
1553 if( p->pPrev ){ |
|
1554 p->pPrev->pNext = p->pNext; |
|
1555 }else{ |
|
1556 assert( p->db->pVdbe==p ); |
|
1557 p->db->pVdbe = p->pNext; |
|
1558 } |
|
1559 if( p->pNext ){ |
|
1560 p->pNext->pPrev = p->pPrev; |
|
1561 } |
|
1562 if( p->aOp ){ |
|
1563 for(i=0; i<p->nOp; i++){ |
|
1564 Op *pOp = &p->aOp[i]; |
|
1565 freeP3(pOp->p3type, pOp->p3); |
|
1566 } |
|
1567 sqliteFree(p->aOp); |
|
1568 } |
|
1569 releaseMemArray(p->aVar, p->nVar); |
|
1570 sqliteFree(p->aLabel); |
|
1571 sqliteFree(p->aStack); |
|
1572 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
|
1573 sqliteFree(p->aColName); |
|
1574 p->magic = VDBE_MAGIC_DEAD; |
|
1575 sqliteFree(p); |
|
1576 } |
|
1577 |
|
1578 /* |
|
1579 ** If a MoveTo operation is pending on the given cursor, then do that |
|
1580 ** MoveTo now. Return an error code. If no MoveTo is pending, this |
|
1581 ** routine does nothing and returns SQLITE_OK. |
|
1582 */ |
|
1583 int sqlite3VdbeCursorMoveto(Cursor *p){ |
|
1584 if( p->deferredMoveto ){ |
|
1585 int res, rc; |
|
1586 extern int sqlite3_search_count; |
|
1587 assert( p->isTable ); |
|
1588 if( p->isTable ){ |
|
1589 rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, &res); |
|
1590 }else{ |
|
1591 rc = sqlite3BtreeMoveto(p->pCursor,(char*)&p->movetoTarget, |
|
1592 sizeof(i64),&res); |
|
1593 } |
|
1594 if( rc ) return rc; |
|
1595 *p->pIncrKey = 0; |
|
1596 p->lastRowid = keyToInt(p->movetoTarget); |
|
1597 p->rowidIsValid = res==0; |
|
1598 if( res<0 ){ |
|
1599 rc = sqlite3BtreeNext(p->pCursor, &res); |
|
1600 if( rc ) return rc; |
|
1601 } |
|
1602 #ifdef SQLITE_TEST |
|
1603 sqlite3_search_count++; |
|
1604 #endif |
|
1605 p->deferredMoveto = 0; |
|
1606 p->cacheStatus = CACHE_STALE; |
|
1607 } |
|
1608 return SQLITE_OK; |
|
1609 } |
|
1610 |
|
1611 /* |
|
1612 ** The following functions: |
|
1613 ** |
|
1614 ** sqlite3VdbeSerialType() |
|
1615 ** sqlite3VdbeSerialTypeLen() |
|
1616 ** sqlite3VdbeSerialRead() |
|
1617 ** sqlite3VdbeSerialLen() |
|
1618 ** sqlite3VdbeSerialWrite() |
|
1619 ** |
|
1620 ** encapsulate the code that serializes values for storage in SQLite |
|
1621 ** data and index records. Each serialized value consists of a |
|
1622 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
|
1623 ** integer, stored as a varint. |
|
1624 ** |
|
1625 ** In an SQLite index record, the serial type is stored directly before |
|
1626 ** the blob of data that it corresponds to. In a table record, all serial |
|
1627 ** types are stored at the start of the record, and the blobs of data at |
|
1628 ** the end. Hence these functions allow the caller to handle the |
|
1629 ** serial-type and data blob seperately. |
|
1630 ** |
|
1631 ** The following table describes the various storage classes for data: |
|
1632 ** |
|
1633 ** serial type bytes of data type |
|
1634 ** -------------- --------------- --------------- |
|
1635 ** 0 0 NULL |
|
1636 ** 1 1 signed integer |
|
1637 ** 2 2 signed integer |
|
1638 ** 3 3 signed integer |
|
1639 ** 4 4 signed integer |
|
1640 ** 5 6 signed integer |
|
1641 ** 6 8 signed integer |
|
1642 ** 7 8 IEEE float |
|
1643 ** 8 0 Integer constant 0 |
|
1644 ** 9 0 Integer constant 1 |
|
1645 ** 10,11 reserved for expansion |
|
1646 ** N>=12 and even (N-12)/2 BLOB |
|
1647 ** N>=13 and odd (N-13)/2 text |
|
1648 ** |
|
1649 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
|
1650 ** of SQLite will not understand those serial types. |
|
1651 */ |
|
1652 |
|
1653 /* |
|
1654 ** Return the serial-type for the value stored in pMem. |
|
1655 */ |
|
1656 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
|
1657 int flags = pMem->flags; |
|
1658 |
|
1659 if( flags&MEM_Null ){ |
|
1660 return 0; |
|
1661 } |
|
1662 if( flags&MEM_Int ){ |
|
1663 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
|
1664 # define MAX_6BYTE ((((i64)0x00001000)<<32)-1) |
|
1665 i64 i = pMem->i; |
|
1666 u64 u; |
|
1667 if( file_format>=4 && (i&1)==i ){ |
|
1668 return 8+i; |
|
1669 } |
|
1670 u = i<0 ? -i : i; |
|
1671 if( u<=127 ) return 1; |
|
1672 if( u<=32767 ) return 2; |
|
1673 if( u<=8388607 ) return 3; |
|
1674 if( u<=2147483647 ) return 4; |
|
1675 if( u<=MAX_6BYTE ) return 5; |
|
1676 return 6; |
|
1677 } |
|
1678 if( flags&MEM_Real ){ |
|
1679 return 7; |
|
1680 } |
|
1681 if( flags&MEM_Str ){ |
|
1682 int n = pMem->n; |
|
1683 assert( n>=0 ); |
|
1684 return ((n*2) + 13); |
|
1685 } |
|
1686 if( flags&MEM_Blob ){ |
|
1687 return (pMem->n*2 + 12); |
|
1688 } |
|
1689 return 0; |
|
1690 } |
|
1691 |
|
1692 /* |
|
1693 ** Return the length of the data corresponding to the supplied serial-type. |
|
1694 */ |
|
1695 int sqlite3VdbeSerialTypeLen(u32 serial_type){ |
|
1696 if( serial_type>=12 ){ |
|
1697 return (serial_type-12)/2; |
|
1698 }else{ |
|
1699 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; |
|
1700 return aSize[serial_type]; |
|
1701 } |
|
1702 } |
|
1703 |
|
1704 /* |
|
1705 ** Write the serialized data blob for the value stored in pMem into |
|
1706 ** buf. It is assumed that the caller has allocated sufficient space. |
|
1707 ** Return the number of bytes written. |
|
1708 */ |
|
1709 int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem, int file_format){ |
|
1710 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); |
|
1711 int len; |
|
1712 |
|
1713 /* Integer and Real */ |
|
1714 if( serial_type<=7 && serial_type>0 ){ |
|
1715 u64 v; |
|
1716 int i; |
|
1717 if( serial_type==7 ){ |
|
1718 v = *(u64*)&pMem->r; |
|
1719 }else{ |
|
1720 v = *(u64*)&pMem->i; |
|
1721 } |
|
1722 len = i = sqlite3VdbeSerialTypeLen(serial_type); |
|
1723 while( i-- ){ |
|
1724 buf[i] = (v&0xFF); |
|
1725 v >>= 8; |
|
1726 } |
|
1727 return len; |
|
1728 } |
|
1729 |
|
1730 /* String or blob */ |
|
1731 if( serial_type>=12 ){ |
|
1732 len = sqlite3VdbeSerialTypeLen(serial_type); |
|
1733 memcpy(buf, pMem->z, len); |
|
1734 return len; |
|
1735 } |
|
1736 |
|
1737 /* NULL or constants 0 or 1 */ |
|
1738 return 0; |
|
1739 } |
|
1740 |
|
1741 /* |
|
1742 ** Deserialize the data blob pointed to by buf as serial type serial_type |
|
1743 ** and store the result in pMem. Return the number of bytes read. |
|
1744 */ |
|
1745 int sqlite3VdbeSerialGet( |
|
1746 const unsigned char *buf, /* Buffer to deserialize from */ |
|
1747 u32 serial_type, /* Serial type to deserialize */ |
|
1748 Mem *pMem /* Memory cell to write value into */ |
|
1749 ){ |
|
1750 switch( serial_type ){ |
|
1751 case 10: /* Reserved for future use */ |
|
1752 case 11: /* Reserved for future use */ |
|
1753 case 0: { /* NULL */ |
|
1754 pMem->flags = MEM_Null; |
|
1755 break; |
|
1756 } |
|
1757 case 1: { /* 1-byte signed integer */ |
|
1758 pMem->i = (signed char)buf[0]; |
|
1759 pMem->flags = MEM_Int; |
|
1760 return 1; |
|
1761 } |
|
1762 case 2: { /* 2-byte signed integer */ |
|
1763 pMem->i = (((signed char)buf[0])<<8) | buf[1]; |
|
1764 pMem->flags = MEM_Int; |
|
1765 return 2; |
|
1766 } |
|
1767 case 3: { /* 3-byte signed integer */ |
|
1768 pMem->i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; |
|
1769 pMem->flags = MEM_Int; |
|
1770 return 3; |
|
1771 } |
|
1772 case 4: { /* 4-byte signed integer */ |
|
1773 pMem->i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
|
1774 pMem->flags = MEM_Int; |
|
1775 return 4; |
|
1776 } |
|
1777 case 5: { /* 6-byte signed integer */ |
|
1778 u64 x = (((signed char)buf[0])<<8) | buf[1]; |
|
1779 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
|
1780 x = (x<<32) | y; |
|
1781 pMem->i = *(i64*)&x; |
|
1782 pMem->flags = MEM_Int; |
|
1783 return 6; |
|
1784 } |
|
1785 case 6: /* 8-byte signed integer */ |
|
1786 case 7: { /* IEEE floating point */ |
|
1787 u64 x; |
|
1788 u32 y; |
|
1789 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
|
1790 /* Verify that integers and floating point values use the same |
|
1791 ** byte order. The byte order differs on some (broken) architectures. |
|
1792 */ |
|
1793 static const u64 t1 = ((u64)0x3ff00000)<<32; |
|
1794 assert( 1.0==*(double*)&t1 ); |
|
1795 #endif |
|
1796 |
|
1797 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
|
1798 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
|
1799 x = (x<<32) | y; |
|
1800 if( serial_type==6 ){ |
|
1801 pMem->i = *(i64*)&x; |
|
1802 pMem->flags = MEM_Int; |
|
1803 }else{ |
|
1804 pMem->r = *(double*)&x; |
|
1805 pMem->flags = MEM_Real; |
|
1806 } |
|
1807 return 8; |
|
1808 } |
|
1809 case 8: /* Integer 0 */ |
|
1810 case 9: { /* Integer 1 */ |
|
1811 pMem->i = serial_type-8; |
|
1812 pMem->flags = MEM_Int; |
|
1813 return 0; |
|
1814 } |
|
1815 default: { |
|
1816 int len = (serial_type-12)/2; |
|
1817 pMem->z = (char *)buf; |
|
1818 pMem->n = len; |
|
1819 pMem->xDel = 0; |
|
1820 if( serial_type&0x01 ){ |
|
1821 pMem->flags = MEM_Str | MEM_Ephem; |
|
1822 }else{ |
|
1823 pMem->flags = MEM_Blob | MEM_Ephem; |
|
1824 } |
|
1825 return len; |
|
1826 } |
|
1827 } |
|
1828 return 0; |
|
1829 } |
|
1830 |
|
1831 /* |
|
1832 ** The header of a record consists of a sequence variable-length integers. |
|
1833 ** These integers are almost always small and are encoded as a single byte. |
|
1834 ** The following macro takes advantage this fact to provide a fast decode |
|
1835 ** of the integers in a record header. It is faster for the common case |
|
1836 ** where the integer is a single byte. It is a little slower when the |
|
1837 ** integer is two or more bytes. But overall it is faster. |
|
1838 ** |
|
1839 ** The following expressions are equivalent: |
|
1840 ** |
|
1841 ** x = sqlite3GetVarint32( A, &B ); |
|
1842 ** |
|
1843 ** x = GetVarint( A, B ); |
|
1844 ** |
|
1845 */ |
|
1846 #define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B)) |
|
1847 |
|
1848 /* |
|
1849 ** This function compares the two table rows or index records specified by |
|
1850 ** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero |
|
1851 ** or positive integer if {nKey1, pKey1} is less than, equal to or |
|
1852 ** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings |
|
1853 ** composed by the OP_MakeRecord opcode of the VDBE. |
|
1854 */ |
|
1855 int sqlite3VdbeRecordCompare( |
|
1856 void *userData, |
|
1857 int nKey1, const void *pKey1, |
|
1858 int nKey2, const void *pKey2 |
|
1859 ){ |
|
1860 KeyInfo *pKeyInfo = (KeyInfo*)userData; |
|
1861 u32 d1, d2; /* Offset into aKey[] of next data element */ |
|
1862 u32 idx1, idx2; /* Offset into aKey[] of next header element */ |
|
1863 u32 szHdr1, szHdr2; /* Number of bytes in header */ |
|
1864 int i = 0; |
|
1865 int nField; |
|
1866 int rc = 0; |
|
1867 const unsigned char *aKey1 = (const unsigned char *)pKey1; |
|
1868 const unsigned char *aKey2 = (const unsigned char *)pKey2; |
|
1869 |
|
1870 Mem mem1; |
|
1871 Mem mem2; |
|
1872 mem1.enc = pKeyInfo->enc; |
|
1873 mem2.enc = pKeyInfo->enc; |
|
1874 |
|
1875 idx1 = GetVarint(aKey1, szHdr1); |
|
1876 d1 = szHdr1; |
|
1877 idx2 = GetVarint(aKey2, szHdr2); |
|
1878 d2 = szHdr2; |
|
1879 nField = pKeyInfo->nField; |
|
1880 while( idx1<szHdr1 && idx2<szHdr2 ){ |
|
1881 u32 serial_type1; |
|
1882 u32 serial_type2; |
|
1883 |
|
1884 /* Read the serial types for the next element in each key. */ |
|
1885 idx1 += GetVarint( aKey1+idx1, serial_type1 ); |
|
1886 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; |
|
1887 idx2 += GetVarint( aKey2+idx2, serial_type2 ); |
|
1888 if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break; |
|
1889 |
|
1890 /* Assert that there is enough space left in each key for the blob of |
|
1891 ** data to go with the serial type just read. This assert may fail if |
|
1892 ** the file is corrupted. Then read the value from each key into mem1 |
|
1893 ** and mem2 respectively. |
|
1894 */ |
|
1895 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
|
1896 d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2); |
|
1897 |
|
1898 rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0); |
|
1899 if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1); |
|
1900 if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2); |
|
1901 if( rc!=0 ){ |
|
1902 break; |
|
1903 } |
|
1904 i++; |
|
1905 } |
|
1906 |
|
1907 /* One of the keys ran out of fields, but all the fields up to that point |
|
1908 ** were equal. If the incrKey flag is true, then the second key is |
|
1909 ** treated as larger. |
|
1910 */ |
|
1911 if( rc==0 ){ |
|
1912 if( pKeyInfo->incrKey ){ |
|
1913 rc = -1; |
|
1914 }else if( d1<nKey1 ){ |
|
1915 rc = 1; |
|
1916 }else if( d2<nKey2 ){ |
|
1917 rc = -1; |
|
1918 } |
|
1919 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField |
|
1920 && pKeyInfo->aSortOrder[i] ){ |
|
1921 rc = -rc; |
|
1922 } |
|
1923 |
|
1924 return rc; |
|
1925 } |
|
1926 |
|
1927 /* |
|
1928 ** The argument is an index entry composed using the OP_MakeRecord opcode. |
|
1929 ** The last entry in this record should be an integer (specifically |
|
1930 ** an integer rowid). This routine returns the number of bytes in |
|
1931 ** that integer. |
|
1932 */ |
|
1933 int sqlite3VdbeIdxRowidLen(const u8 *aKey){ |
|
1934 u32 szHdr; /* Size of the header */ |
|
1935 u32 typeRowid; /* Serial type of the rowid */ |
|
1936 |
|
1937 sqlite3GetVarint32(aKey, &szHdr); |
|
1938 sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid); |
|
1939 return sqlite3VdbeSerialTypeLen(typeRowid); |
|
1940 } |
|
1941 |
|
1942 |
|
1943 /* |
|
1944 ** pCur points at an index entry created using the OP_MakeRecord opcode. |
|
1945 ** Read the rowid (the last field in the record) and store it in *rowid. |
|
1946 ** Return SQLITE_OK if everything works, or an error code otherwise. |
|
1947 */ |
|
1948 int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
|
1949 i64 nCellKey; |
|
1950 int rc; |
|
1951 u32 szHdr; /* Size of the header */ |
|
1952 u32 typeRowid; /* Serial type of the rowid */ |
|
1953 u32 lenRowid; /* Size of the rowid */ |
|
1954 Mem m, v; |
|
1955 |
|
1956 sqlite3BtreeKeySize(pCur, &nCellKey); |
|
1957 if( nCellKey<=0 ){ |
|
1958 return SQLITE_CORRUPT_BKPT; |
|
1959 } |
|
1960 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); |
|
1961 if( rc ){ |
|
1962 return rc; |
|
1963 } |
|
1964 sqlite3GetVarint32((u8*)m.z, &szHdr); |
|
1965 sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid); |
|
1966 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); |
|
1967 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
|
1968 *rowid = v.i; |
|
1969 sqlite3VdbeMemRelease(&m); |
|
1970 return SQLITE_OK; |
|
1971 } |
|
1972 |
|
1973 /* |
|
1974 ** Compare the key of the index entry that cursor pC is point to against |
|
1975 ** the key string in pKey (of length nKey). Write into *pRes a number |
|
1976 ** that is negative, zero, or positive if pC is less than, equal to, |
|
1977 ** or greater than pKey. Return SQLITE_OK on success. |
|
1978 ** |
|
1979 ** pKey is either created without a rowid or is truncated so that it |
|
1980 ** omits the rowid at the end. The rowid at the end of the index entry |
|
1981 ** is ignored as well. |
|
1982 */ |
|
1983 int sqlite3VdbeIdxKeyCompare( |
|
1984 Cursor *pC, /* The cursor to compare against */ |
|
1985 int nKey, const u8 *pKey, /* The key to compare */ |
|
1986 int *res /* Write the comparison result here */ |
|
1987 ){ |
|
1988 i64 nCellKey; |
|
1989 int rc; |
|
1990 BtCursor *pCur = pC->pCursor; |
|
1991 int lenRowid; |
|
1992 Mem m; |
|
1993 |
|
1994 sqlite3BtreeKeySize(pCur, &nCellKey); |
|
1995 if( nCellKey<=0 ){ |
|
1996 *res = 0; |
|
1997 return SQLITE_OK; |
|
1998 } |
|
1999 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m); |
|
2000 if( rc ){ |
|
2001 return rc; |
|
2002 } |
|
2003 lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z); |
|
2004 *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey); |
|
2005 sqlite3VdbeMemRelease(&m); |
|
2006 return SQLITE_OK; |
|
2007 } |
|
2008 |
|
2009 /* |
|
2010 ** This routine sets the value to be returned by subsequent calls to |
|
2011 ** sqlite3_changes() on the database handle 'db'. |
|
2012 */ |
|
2013 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ |
|
2014 db->nChange = nChange; |
|
2015 db->nTotalChange += nChange; |
|
2016 } |
|
2017 |
|
2018 /* |
|
2019 ** Set a flag in the vdbe to update the change counter when it is finalised |
|
2020 ** or reset. |
|
2021 */ |
|
2022 void sqlite3VdbeCountChanges(Vdbe *v){ |
|
2023 v->changeCntOn = 1; |
|
2024 } |
|
2025 |
|
2026 /* |
|
2027 ** Mark every prepared statement associated with a database connection |
|
2028 ** as expired. |
|
2029 ** |
|
2030 ** An expired statement means that recompilation of the statement is |
|
2031 ** recommend. Statements expire when things happen that make their |
|
2032 ** programs obsolete. Removing user-defined functions or collating |
|
2033 ** sequences, or changing an authorization function are the types of |
|
2034 ** things that make prepared statements obsolete. |
|
2035 */ |
|
2036 void sqlite3ExpirePreparedStatements(sqlite3 *db){ |
|
2037 Vdbe *p; |
|
2038 for(p = db->pVdbe; p; p=p->pNext){ |
|
2039 p->expired = 1; |
|
2040 } |
|
2041 } |
|
2042 |
|
2043 /* |
|
2044 ** Return the database associated with the Vdbe. |
|
2045 */ |
|
2046 sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
|
2047 return v->db; |
|
2048 } |