|
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 ** $Id: vdbeaux.c,v 1.405 2008/08/02 03:50:39 drh Exp $ |
|
18 */ |
|
19 #include "sqliteInt.h" |
|
20 #include <ctype.h> |
|
21 #include "vdbeInt.h" |
|
22 |
|
23 |
|
24 |
|
25 /* |
|
26 ** When debugging the code generator in a symbolic debugger, one can |
|
27 ** set the sqlite3VdbeAddopTrace to 1 and all opcodes will be printed |
|
28 ** as they are added to the instruction stream. |
|
29 */ |
|
30 #ifdef SQLITE_DEBUG |
|
31 int sqlite3VdbeAddopTrace = 0; |
|
32 #endif |
|
33 |
|
34 |
|
35 /* |
|
36 ** Create a new virtual database engine. |
|
37 */ |
|
38 Vdbe *sqlite3VdbeCreate(sqlite3 *db){ |
|
39 Vdbe *p; |
|
40 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); |
|
41 if( p==0 ) return 0; |
|
42 p->db = db; |
|
43 if( db->pVdbe ){ |
|
44 db->pVdbe->pPrev = p; |
|
45 } |
|
46 p->pNext = db->pVdbe; |
|
47 p->pPrev = 0; |
|
48 db->pVdbe = p; |
|
49 p->magic = VDBE_MAGIC_INIT; |
|
50 return p; |
|
51 } |
|
52 |
|
53 /* |
|
54 ** Remember the SQL string for a prepared statement. |
|
55 */ |
|
56 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){ |
|
57 if( p==0 ) return; |
|
58 assert( p->zSql==0 ); |
|
59 p->zSql = sqlite3DbStrNDup(p->db, z, n); |
|
60 } |
|
61 |
|
62 /* |
|
63 ** Return the SQL associated with a prepared statement |
|
64 */ |
|
65 const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
|
66 return ((Vdbe *)pStmt)->zSql; |
|
67 } |
|
68 |
|
69 /* |
|
70 ** Swap all content between two VDBE structures. |
|
71 */ |
|
72 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
|
73 Vdbe tmp, *pTmp; |
|
74 char *zTmp; |
|
75 int nTmp; |
|
76 tmp = *pA; |
|
77 *pA = *pB; |
|
78 *pB = tmp; |
|
79 pTmp = pA->pNext; |
|
80 pA->pNext = pB->pNext; |
|
81 pB->pNext = pTmp; |
|
82 pTmp = pA->pPrev; |
|
83 pA->pPrev = pB->pPrev; |
|
84 pB->pPrev = pTmp; |
|
85 zTmp = pA->zSql; |
|
86 pA->zSql = pB->zSql; |
|
87 pB->zSql = zTmp; |
|
88 nTmp = pA->nSql; |
|
89 pA->nSql = pB->nSql; |
|
90 pB->nSql = nTmp; |
|
91 } |
|
92 |
|
93 #ifdef SQLITE_DEBUG |
|
94 /* |
|
95 ** Turn tracing on or off |
|
96 */ |
|
97 void sqlite3VdbeTrace(Vdbe *p, FILE *trace){ |
|
98 p->trace = trace; |
|
99 } |
|
100 #endif |
|
101 |
|
102 /* |
|
103 ** Resize the Vdbe.aOp array so that it contains at least N |
|
104 ** elements. |
|
105 ** |
|
106 ** If an out-of-memory error occurs while resizing the array, |
|
107 ** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that |
|
108 ** any opcodes already allocated can be correctly deallocated |
|
109 ** along with the rest of the Vdbe). |
|
110 */ |
|
111 static void resizeOpArray(Vdbe *p, int N){ |
|
112 VdbeOp *pNew; |
|
113 pNew = sqlite3DbRealloc(p->db, p->aOp, N*sizeof(Op)); |
|
114 if( pNew ){ |
|
115 p->nOpAlloc = N; |
|
116 p->aOp = pNew; |
|
117 } |
|
118 } |
|
119 |
|
120 /* |
|
121 ** Add a new instruction to the list of instructions current in the |
|
122 ** VDBE. Return the address of the new instruction. |
|
123 ** |
|
124 ** Parameters: |
|
125 ** |
|
126 ** p Pointer to the VDBE |
|
127 ** |
|
128 ** op The opcode for this instruction |
|
129 ** |
|
130 ** p1, p2, p3 Operands |
|
131 ** |
|
132 ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
|
133 ** the sqlite3VdbeChangeP4() function to change the value of the P4 |
|
134 ** operand. |
|
135 */ |
|
136 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
|
137 int i; |
|
138 VdbeOp *pOp; |
|
139 |
|
140 i = p->nOp; |
|
141 assert( p->magic==VDBE_MAGIC_INIT ); |
|
142 if( p->nOpAlloc<=i ){ |
|
143 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); |
|
144 if( p->db->mallocFailed ){ |
|
145 return 0; |
|
146 } |
|
147 } |
|
148 p->nOp++; |
|
149 pOp = &p->aOp[i]; |
|
150 pOp->opcode = op; |
|
151 pOp->p5 = 0; |
|
152 pOp->p1 = p1; |
|
153 pOp->p2 = p2; |
|
154 pOp->p3 = p3; |
|
155 pOp->p4.p = 0; |
|
156 pOp->p4type = P4_NOTUSED; |
|
157 p->expired = 0; |
|
158 #ifdef SQLITE_DEBUG |
|
159 pOp->zComment = 0; |
|
160 if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
|
161 #endif |
|
162 #ifdef VDBE_PROFILE |
|
163 pOp->cycles = 0; |
|
164 pOp->cnt = 0; |
|
165 #endif |
|
166 return i; |
|
167 } |
|
168 int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
|
169 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); |
|
170 } |
|
171 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ |
|
172 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); |
|
173 } |
|
174 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ |
|
175 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); |
|
176 } |
|
177 |
|
178 |
|
179 /* |
|
180 ** Add an opcode that includes the p4 value as a pointer. |
|
181 */ |
|
182 int sqlite3VdbeAddOp4( |
|
183 Vdbe *p, /* Add the opcode to this VM */ |
|
184 int op, /* The new opcode */ |
|
185 int p1, /* The P1 operand */ |
|
186 int p2, /* The P2 operand */ |
|
187 int p3, /* The P3 operand */ |
|
188 const char *zP4, /* The P4 operand */ |
|
189 int p4type /* P4 operand type */ |
|
190 ){ |
|
191 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
|
192 sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
|
193 return addr; |
|
194 } |
|
195 |
|
196 /* |
|
197 ** Create a new symbolic label for an instruction that has yet to be |
|
198 ** coded. The symbolic label is really just a negative number. The |
|
199 ** label can be used as the P2 value of an operation. Later, when |
|
200 ** the label is resolved to a specific address, the VDBE will scan |
|
201 ** through its operation list and change all values of P2 which match |
|
202 ** the label into the resolved address. |
|
203 ** |
|
204 ** The VDBE knows that a P2 value is a label because labels are |
|
205 ** always negative and P2 values are suppose to be non-negative. |
|
206 ** Hence, a negative P2 value is a label that has yet to be resolved. |
|
207 ** |
|
208 ** Zero is returned if a malloc() fails. |
|
209 */ |
|
210 int sqlite3VdbeMakeLabel(Vdbe *p){ |
|
211 int i; |
|
212 i = p->nLabel++; |
|
213 assert( p->magic==VDBE_MAGIC_INIT ); |
|
214 if( i>=p->nLabelAlloc ){ |
|
215 p->nLabelAlloc = p->nLabelAlloc*2 + 10; |
|
216 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
|
217 p->nLabelAlloc*sizeof(p->aLabel[0])); |
|
218 } |
|
219 if( p->aLabel ){ |
|
220 p->aLabel[i] = -1; |
|
221 } |
|
222 return -1-i; |
|
223 } |
|
224 |
|
225 /* |
|
226 ** Resolve label "x" to be the address of the next instruction to |
|
227 ** be inserted. The parameter "x" must have been obtained from |
|
228 ** a prior call to sqlite3VdbeMakeLabel(). |
|
229 */ |
|
230 void sqlite3VdbeResolveLabel(Vdbe *p, int x){ |
|
231 int j = -1-x; |
|
232 assert( p->magic==VDBE_MAGIC_INIT ); |
|
233 assert( j>=0 && j<p->nLabel ); |
|
234 if( p->aLabel ){ |
|
235 p->aLabel[j] = p->nOp; |
|
236 } |
|
237 } |
|
238 |
|
239 /* |
|
240 ** Loop through the program looking for P2 values that are negative |
|
241 ** on jump instructions. Each such value is a label. Resolve the |
|
242 ** label by setting the P2 value to its correct non-zero value. |
|
243 ** |
|
244 ** This routine is called once after all opcodes have been inserted. |
|
245 ** |
|
246 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument |
|
247 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by |
|
248 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. |
|
249 ** |
|
250 ** This routine also does the following optimization: It scans for |
|
251 ** instructions that might cause a statement rollback. Such instructions |
|
252 ** are: |
|
253 ** |
|
254 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
|
255 ** * OP_Destroy |
|
256 ** * OP_VUpdate |
|
257 ** * OP_VRename |
|
258 ** |
|
259 ** If no such instruction is found, then every Statement instruction |
|
260 ** is changed to a Noop. In this way, we avoid creating the statement |
|
261 ** journal file unnecessarily. |
|
262 */ |
|
263 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
|
264 int i; |
|
265 int nMaxArgs = 0; |
|
266 Op *pOp; |
|
267 int *aLabel = p->aLabel; |
|
268 int doesStatementRollback = 0; |
|
269 int hasStatementBegin = 0; |
|
270 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
|
271 u8 opcode = pOp->opcode; |
|
272 |
|
273 if( opcode==OP_Function || opcode==OP_AggStep ){ |
|
274 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; |
|
275 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
276 }else if( opcode==OP_VUpdate ){ |
|
277 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
|
278 #endif |
|
279 } |
|
280 if( opcode==OP_Halt ){ |
|
281 if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){ |
|
282 doesStatementRollback = 1; |
|
283 } |
|
284 }else if( opcode==OP_Statement ){ |
|
285 hasStatementBegin = 1; |
|
286 }else if( opcode==OP_Destroy ){ |
|
287 doesStatementRollback = 1; |
|
288 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
289 }else if( opcode==OP_VUpdate || opcode==OP_VRename ){ |
|
290 doesStatementRollback = 1; |
|
291 }else if( opcode==OP_VFilter ){ |
|
292 int n; |
|
293 assert( p->nOp - i >= 3 ); |
|
294 assert( pOp[-1].opcode==OP_Integer ); |
|
295 n = pOp[-1].p1; |
|
296 if( n>nMaxArgs ) nMaxArgs = n; |
|
297 #endif |
|
298 } |
|
299 |
|
300 if( sqlite3VdbeOpcodeHasProperty(opcode, OPFLG_JUMP) && pOp->p2<0 ){ |
|
301 assert( -1-pOp->p2<p->nLabel ); |
|
302 pOp->p2 = aLabel[-1-pOp->p2]; |
|
303 } |
|
304 } |
|
305 sqlite3DbFree(p->db, p->aLabel); |
|
306 p->aLabel = 0; |
|
307 |
|
308 *pMaxFuncArgs = nMaxArgs; |
|
309 |
|
310 /* If we never rollback a statement transaction, then statement |
|
311 ** transactions are not needed. So change every OP_Statement |
|
312 ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive() |
|
313 ** which can be expensive on some platforms. |
|
314 */ |
|
315 if( hasStatementBegin && !doesStatementRollback ){ |
|
316 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
|
317 if( pOp->opcode==OP_Statement ){ |
|
318 pOp->opcode = OP_Noop; |
|
319 } |
|
320 } |
|
321 } |
|
322 } |
|
323 |
|
324 /* |
|
325 ** Return the address of the next instruction to be inserted. |
|
326 */ |
|
327 int sqlite3VdbeCurrentAddr(Vdbe *p){ |
|
328 assert( p->magic==VDBE_MAGIC_INIT ); |
|
329 return p->nOp; |
|
330 } |
|
331 |
|
332 /* |
|
333 ** Add a whole list of operations to the operation stack. Return the |
|
334 ** address of the first operation added. |
|
335 */ |
|
336 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){ |
|
337 int addr; |
|
338 assert( p->magic==VDBE_MAGIC_INIT ); |
|
339 if( p->nOp + nOp > p->nOpAlloc ){ |
|
340 resizeOpArray(p, p->nOpAlloc ? p->nOpAlloc*2 : 1024/sizeof(Op)); |
|
341 assert( p->nOp+nOp<=p->nOpAlloc || p->db->mallocFailed ); |
|
342 } |
|
343 if( p->db->mallocFailed ){ |
|
344 return 0; |
|
345 } |
|
346 addr = p->nOp; |
|
347 if( nOp>0 ){ |
|
348 int i; |
|
349 VdbeOpList const *pIn = aOp; |
|
350 for(i=0; i<nOp; i++, pIn++){ |
|
351 int p2 = pIn->p2; |
|
352 VdbeOp *pOut = &p->aOp[i+addr]; |
|
353 pOut->opcode = pIn->opcode; |
|
354 pOut->p1 = pIn->p1; |
|
355 if( p2<0 && sqlite3VdbeOpcodeHasProperty(pOut->opcode, OPFLG_JUMP) ){ |
|
356 pOut->p2 = addr + ADDR(p2); |
|
357 }else{ |
|
358 pOut->p2 = p2; |
|
359 } |
|
360 pOut->p3 = pIn->p3; |
|
361 pOut->p4type = P4_NOTUSED; |
|
362 pOut->p4.p = 0; |
|
363 pOut->p5 = 0; |
|
364 #ifdef SQLITE_DEBUG |
|
365 pOut->zComment = 0; |
|
366 if( sqlite3VdbeAddopTrace ){ |
|
367 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
|
368 } |
|
369 #endif |
|
370 } |
|
371 p->nOp += nOp; |
|
372 } |
|
373 return addr; |
|
374 } |
|
375 |
|
376 /* |
|
377 ** Change the value of the P1 operand for a specific instruction. |
|
378 ** This routine is useful when a large program is loaded from a |
|
379 ** static array using sqlite3VdbeAddOpList but we want to make a |
|
380 ** few minor changes to the program. |
|
381 */ |
|
382 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ |
|
383 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
384 if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
385 p->aOp[addr].p1 = val; |
|
386 } |
|
387 } |
|
388 |
|
389 /* |
|
390 ** Change the value of the P2 operand for a specific instruction. |
|
391 ** This routine is useful for setting a jump destination. |
|
392 */ |
|
393 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ |
|
394 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
395 if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
396 p->aOp[addr].p2 = val; |
|
397 } |
|
398 } |
|
399 |
|
400 /* |
|
401 ** Change the value of the P3 operand for a specific instruction. |
|
402 */ |
|
403 void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ |
|
404 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
405 if( p && addr>=0 && p->nOp>addr && p->aOp ){ |
|
406 p->aOp[addr].p3 = val; |
|
407 } |
|
408 } |
|
409 |
|
410 /* |
|
411 ** Change the value of the P5 operand for the most recently |
|
412 ** added operation. |
|
413 */ |
|
414 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ |
|
415 assert( p==0 || p->magic==VDBE_MAGIC_INIT ); |
|
416 if( p && p->aOp ){ |
|
417 assert( p->nOp>0 ); |
|
418 p->aOp[p->nOp-1].p5 = val; |
|
419 } |
|
420 } |
|
421 |
|
422 /* |
|
423 ** Change the P2 operand of instruction addr so that it points to |
|
424 ** the address of the next instruction to be coded. |
|
425 */ |
|
426 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
|
427 sqlite3VdbeChangeP2(p, addr, p->nOp); |
|
428 } |
|
429 |
|
430 |
|
431 /* |
|
432 ** If the input FuncDef structure is ephemeral, then free it. If |
|
433 ** the FuncDef is not ephermal, then do nothing. |
|
434 */ |
|
435 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
|
436 if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){ |
|
437 sqlite3DbFree(db, pDef); |
|
438 } |
|
439 } |
|
440 |
|
441 /* |
|
442 ** Delete a P4 value if necessary. |
|
443 */ |
|
444 static void freeP4(sqlite3 *db, int p4type, void *p4){ |
|
445 if( p4 ){ |
|
446 switch( p4type ){ |
|
447 case P4_REAL: |
|
448 case P4_INT64: |
|
449 case P4_MPRINTF: |
|
450 case P4_DYNAMIC: |
|
451 case P4_KEYINFO: |
|
452 case P4_INTARRAY: |
|
453 case P4_KEYINFO_HANDOFF: { |
|
454 sqlite3DbFree(db, p4); |
|
455 break; |
|
456 } |
|
457 case P4_VDBEFUNC: { |
|
458 VdbeFunc *pVdbeFunc = (VdbeFunc *)p4; |
|
459 freeEphemeralFunction(db, pVdbeFunc->pFunc); |
|
460 sqlite3VdbeDeleteAuxData(pVdbeFunc, 0); |
|
461 sqlite3DbFree(db, pVdbeFunc); |
|
462 break; |
|
463 } |
|
464 case P4_FUNCDEF: { |
|
465 freeEphemeralFunction(db, (FuncDef*)p4); |
|
466 break; |
|
467 } |
|
468 case P4_MEM: { |
|
469 sqlite3ValueFree((sqlite3_value*)p4); |
|
470 break; |
|
471 } |
|
472 } |
|
473 } |
|
474 } |
|
475 |
|
476 |
|
477 /* |
|
478 ** Change N opcodes starting at addr to No-ops. |
|
479 */ |
|
480 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){ |
|
481 if( p && p->aOp ){ |
|
482 VdbeOp *pOp = &p->aOp[addr]; |
|
483 sqlite3 *db = p->db; |
|
484 while( N-- ){ |
|
485 freeP4(db, pOp->p4type, pOp->p4.p); |
|
486 memset(pOp, 0, sizeof(pOp[0])); |
|
487 pOp->opcode = OP_Noop; |
|
488 pOp++; |
|
489 } |
|
490 } |
|
491 } |
|
492 |
|
493 /* |
|
494 ** Change the value of the P4 operand for a specific instruction. |
|
495 ** This routine is useful when a large program is loaded from a |
|
496 ** static array using sqlite3VdbeAddOpList but we want to make a |
|
497 ** few minor changes to the program. |
|
498 ** |
|
499 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of |
|
500 ** the string is made into memory obtained from sqlite3_malloc(). |
|
501 ** A value of n==0 means copy bytes of zP4 up to and including the |
|
502 ** first null byte. If n>0 then copy n+1 bytes of zP4. |
|
503 ** |
|
504 ** If n==P4_KEYINFO it means that zP4 is a pointer to a KeyInfo structure. |
|
505 ** A copy is made of the KeyInfo structure into memory obtained from |
|
506 ** sqlite3_malloc, to be freed when the Vdbe is finalized. |
|
507 ** n==P4_KEYINFO_HANDOFF indicates that zP4 points to a KeyInfo structure |
|
508 ** stored in memory that the caller has obtained from sqlite3_malloc. The |
|
509 ** caller should not free the allocation, it will be freed when the Vdbe is |
|
510 ** finalized. |
|
511 ** |
|
512 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
|
513 ** to a string or structure that is guaranteed to exist for the lifetime of |
|
514 ** the Vdbe. In these cases we can just copy the pointer. |
|
515 ** |
|
516 ** If addr<0 then change P4 on the most recently inserted instruction. |
|
517 */ |
|
518 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
|
519 Op *pOp; |
|
520 sqlite3 *db; |
|
521 assert( p!=0 ); |
|
522 db = p->db; |
|
523 assert( p->magic==VDBE_MAGIC_INIT ); |
|
524 if( p->aOp==0 || db->mallocFailed ){ |
|
525 if (n != P4_KEYINFO) { |
|
526 freeP4(db, n, (void*)*(char**)&zP4); |
|
527 } |
|
528 return; |
|
529 } |
|
530 assert( addr<p->nOp ); |
|
531 if( addr<0 ){ |
|
532 addr = p->nOp - 1; |
|
533 if( addr<0 ) return; |
|
534 } |
|
535 pOp = &p->aOp[addr]; |
|
536 freeP4(db, pOp->p4type, pOp->p4.p); |
|
537 pOp->p4.p = 0; |
|
538 if( n==P4_INT32 ){ |
|
539 /* Note: this cast is safe, because the origin data point was an int |
|
540 ** that was cast to a (const char *). */ |
|
541 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); |
|
542 pOp->p4type = n; |
|
543 }else if( zP4==0 ){ |
|
544 pOp->p4.p = 0; |
|
545 pOp->p4type = P4_NOTUSED; |
|
546 }else if( n==P4_KEYINFO ){ |
|
547 KeyInfo *pKeyInfo; |
|
548 int nField, nByte; |
|
549 |
|
550 nField = ((KeyInfo*)zP4)->nField; |
|
551 nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField; |
|
552 pKeyInfo = sqlite3Malloc( nByte ); |
|
553 pOp->p4.pKeyInfo = pKeyInfo; |
|
554 if( pKeyInfo ){ |
|
555 u8 *aSortOrder; |
|
556 memcpy(pKeyInfo, zP4, nByte); |
|
557 aSortOrder = pKeyInfo->aSortOrder; |
|
558 if( aSortOrder ){ |
|
559 pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField]; |
|
560 memcpy(pKeyInfo->aSortOrder, aSortOrder, nField); |
|
561 } |
|
562 pOp->p4type = P4_KEYINFO; |
|
563 }else{ |
|
564 p->db->mallocFailed = 1; |
|
565 pOp->p4type = P4_NOTUSED; |
|
566 } |
|
567 }else if( n==P4_KEYINFO_HANDOFF ){ |
|
568 pOp->p4.p = (void*)zP4; |
|
569 pOp->p4type = P4_KEYINFO; |
|
570 }else if( n<0 ){ |
|
571 pOp->p4.p = (void*)zP4; |
|
572 pOp->p4type = n; |
|
573 }else{ |
|
574 if( n==0 ) n = strlen(zP4); |
|
575 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); |
|
576 pOp->p4type = P4_DYNAMIC; |
|
577 } |
|
578 } |
|
579 |
|
580 #ifndef NDEBUG |
|
581 /* |
|
582 ** Change the comment on the the most recently coded instruction. Or |
|
583 ** insert a No-op and add the comment to that new instruction. This |
|
584 ** makes the code easier to read during debugging. None of this happens |
|
585 ** in a production build. |
|
586 */ |
|
587 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ |
|
588 va_list ap; |
|
589 assert( p->nOp>0 || p->aOp==0 ); |
|
590 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
|
591 if( p->nOp ){ |
|
592 char **pz = &p->aOp[p->nOp-1].zComment; |
|
593 va_start(ap, zFormat); |
|
594 sqlite3DbFree(p->db, *pz); |
|
595 *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
|
596 va_end(ap); |
|
597 } |
|
598 } |
|
599 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ |
|
600 va_list ap; |
|
601 sqlite3VdbeAddOp0(p, OP_Noop); |
|
602 assert( p->nOp>0 || p->aOp==0 ); |
|
603 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed ); |
|
604 if( p->nOp ){ |
|
605 char **pz = &p->aOp[p->nOp-1].zComment; |
|
606 va_start(ap, zFormat); |
|
607 sqlite3DbFree(p->db, *pz); |
|
608 *pz = sqlite3VMPrintf(p->db, zFormat, ap); |
|
609 va_end(ap); |
|
610 } |
|
611 } |
|
612 #endif /* NDEBUG */ |
|
613 |
|
614 /* |
|
615 ** Return the opcode for a given address. |
|
616 */ |
|
617 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ |
|
618 assert( p->magic==VDBE_MAGIC_INIT ); |
|
619 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); |
|
620 return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0); |
|
621 } |
|
622 |
|
623 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ |
|
624 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
|
625 /* |
|
626 ** Compute a string that describes the P4 parameter for an opcode. |
|
627 ** Use zTemp for any required temporary buffer space. |
|
628 */ |
|
629 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
|
630 char *zP4 = zTemp; |
|
631 assert( nTemp>=20 ); |
|
632 switch( pOp->p4type ){ |
|
633 case P4_KEYINFO_STATIC: |
|
634 case P4_KEYINFO: { |
|
635 int i, j; |
|
636 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
|
637 sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField); |
|
638 i = strlen(zTemp); |
|
639 for(j=0; j<pKeyInfo->nField; j++){ |
|
640 CollSeq *pColl = pKeyInfo->aColl[j]; |
|
641 if( pColl ){ |
|
642 int n = strlen(pColl->zName); |
|
643 if( i+n>nTemp-6 ){ |
|
644 memcpy(&zTemp[i],",...",4); |
|
645 break; |
|
646 } |
|
647 zTemp[i++] = ','; |
|
648 if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){ |
|
649 zTemp[i++] = '-'; |
|
650 } |
|
651 memcpy(&zTemp[i], pColl->zName,n+1); |
|
652 i += n; |
|
653 }else if( i+4<nTemp-6 ){ |
|
654 memcpy(&zTemp[i],",nil",4); |
|
655 i += 4; |
|
656 } |
|
657 } |
|
658 zTemp[i++] = ')'; |
|
659 zTemp[i] = 0; |
|
660 assert( i<nTemp ); |
|
661 break; |
|
662 } |
|
663 case P4_COLLSEQ: { |
|
664 CollSeq *pColl = pOp->p4.pColl; |
|
665 sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName); |
|
666 break; |
|
667 } |
|
668 case P4_FUNCDEF: { |
|
669 FuncDef *pDef = pOp->p4.pFunc; |
|
670 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
|
671 break; |
|
672 } |
|
673 case P4_INT64: { |
|
674 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
|
675 break; |
|
676 } |
|
677 case P4_INT32: { |
|
678 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); |
|
679 break; |
|
680 } |
|
681 case P4_REAL: { |
|
682 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); |
|
683 break; |
|
684 } |
|
685 case P4_MEM: { |
|
686 Mem *pMem = pOp->p4.pMem; |
|
687 assert( (pMem->flags & MEM_Null)==0 ); |
|
688 if( pMem->flags & MEM_Str ){ |
|
689 zP4 = pMem->z; |
|
690 }else if( pMem->flags & MEM_Int ){ |
|
691 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); |
|
692 }else if( pMem->flags & MEM_Real ){ |
|
693 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r); |
|
694 } |
|
695 break; |
|
696 } |
|
697 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
698 case P4_VTAB: { |
|
699 sqlite3_vtab *pVtab = pOp->p4.pVtab; |
|
700 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); |
|
701 break; |
|
702 } |
|
703 #endif |
|
704 case P4_INTARRAY: { |
|
705 sqlite3_snprintf(nTemp, zTemp, "intarray"); |
|
706 break; |
|
707 } |
|
708 default: { |
|
709 zP4 = pOp->p4.z; |
|
710 if( zP4==0 ){ |
|
711 zP4 = zTemp; |
|
712 zTemp[0] = 0; |
|
713 } |
|
714 } |
|
715 } |
|
716 assert( zP4!=0 ); |
|
717 return zP4; |
|
718 } |
|
719 #endif |
|
720 |
|
721 /* |
|
722 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
|
723 ** |
|
724 */ |
|
725 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
|
726 int mask; |
|
727 assert( i>=0 && i<p->db->nDb ); |
|
728 assert( i<sizeof(p->btreeMask)*8 ); |
|
729 mask = 1<<i; |
|
730 if( (p->btreeMask & mask)==0 ){ |
|
731 p->btreeMask |= mask; |
|
732 sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt); |
|
733 } |
|
734 } |
|
735 |
|
736 |
|
737 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
|
738 /* |
|
739 ** Print a single opcode. This routine is used for debugging only. |
|
740 */ |
|
741 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
|
742 char *zP4; |
|
743 char zPtr[50]; |
|
744 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-4s %.2X %s\n"; |
|
745 if( pOut==0 ) pOut = stdout; |
|
746 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); |
|
747 fprintf(pOut, zFormat1, pc, |
|
748 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
|
749 #ifdef SQLITE_DEBUG |
|
750 pOp->zComment ? pOp->zComment : "" |
|
751 #else |
|
752 "" |
|
753 #endif |
|
754 ); |
|
755 fflush(pOut); |
|
756 } |
|
757 #endif |
|
758 |
|
759 /* |
|
760 ** Release an array of N Mem elements |
|
761 */ |
|
762 static void releaseMemArray(Mem *p, int N){ |
|
763 if( p && N ){ |
|
764 sqlite3 *db = p->db; |
|
765 int malloc_failed = db->mallocFailed; |
|
766 while( N-->0 ){ |
|
767 assert( N<2 || p[0].db==p[1].db ); |
|
768 sqlite3VdbeMemRelease(p); |
|
769 p->flags = MEM_Null; |
|
770 p++; |
|
771 } |
|
772 db->mallocFailed = malloc_failed; |
|
773 } |
|
774 } |
|
775 |
|
776 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
|
777 int sqlite3VdbeReleaseBuffers(Vdbe *p){ |
|
778 int ii; |
|
779 int nFree = 0; |
|
780 assert( sqlite3_mutex_held(p->db->mutex) ); |
|
781 for(ii=1; ii<=p->nMem; ii++){ |
|
782 Mem *pMem = &p->aMem[ii]; |
|
783 if( pMem->z && pMem->flags&MEM_Dyn ){ |
|
784 assert( !pMem->xDel ); |
|
785 nFree += sqlite3DbMallocSize(pMem->db, pMem->z); |
|
786 sqlite3VdbeMemRelease(pMem); |
|
787 } |
|
788 } |
|
789 return nFree; |
|
790 } |
|
791 #endif |
|
792 |
|
793 #ifndef SQLITE_OMIT_EXPLAIN |
|
794 /* |
|
795 ** Give a listing of the program in the virtual machine. |
|
796 ** |
|
797 ** The interface is the same as sqlite3VdbeExec(). But instead of |
|
798 ** running the code, it invokes the callback once for each instruction. |
|
799 ** This feature is used to implement "EXPLAIN". |
|
800 ** |
|
801 ** When p->explain==1, each instruction is listed. When |
|
802 ** p->explain==2, only OP_Explain instructions are listed and these |
|
803 ** are shown in a different format. p->explain==2 is used to implement |
|
804 ** EXPLAIN QUERY PLAN. |
|
805 */ |
|
806 int sqlite3VdbeList( |
|
807 Vdbe *p /* The VDBE */ |
|
808 ){ |
|
809 sqlite3 *db = p->db; |
|
810 int i; |
|
811 int rc = SQLITE_OK; |
|
812 Mem *pMem = p->pResultSet = &p->aMem[1]; |
|
813 |
|
814 assert( p->explain ); |
|
815 if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE; |
|
816 assert( db->magic==SQLITE_MAGIC_BUSY ); |
|
817 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY ); |
|
818 |
|
819 /* Even though this opcode does not use dynamic strings for |
|
820 ** the result, result columns may become dynamic if the user calls |
|
821 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
|
822 */ |
|
823 releaseMemArray(pMem, p->nMem); |
|
824 |
|
825 do{ |
|
826 i = p->pc++; |
|
827 }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
|
828 if( i>=p->nOp ){ |
|
829 p->rc = SQLITE_OK; |
|
830 rc = SQLITE_DONE; |
|
831 }else if( db->u1.isInterrupted ){ |
|
832 p->rc = SQLITE_INTERRUPT; |
|
833 rc = SQLITE_ERROR; |
|
834 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); |
|
835 }else{ |
|
836 char *z; |
|
837 Op *pOp = &p->aOp[i]; |
|
838 if( p->explain==1 ){ |
|
839 pMem->flags = MEM_Int; |
|
840 pMem->type = SQLITE_INTEGER; |
|
841 pMem->u.i = i; /* Program counter */ |
|
842 pMem++; |
|
843 |
|
844 pMem->flags = MEM_Static|MEM_Str|MEM_Term; |
|
845 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ |
|
846 assert( pMem->z!=0 ); |
|
847 pMem->n = strlen(pMem->z); |
|
848 pMem->type = SQLITE_TEXT; |
|
849 pMem->enc = SQLITE_UTF8; |
|
850 pMem++; |
|
851 } |
|
852 |
|
853 pMem->flags = MEM_Int; |
|
854 pMem->u.i = pOp->p1; /* P1 */ |
|
855 pMem->type = SQLITE_INTEGER; |
|
856 pMem++; |
|
857 |
|
858 pMem->flags = MEM_Int; |
|
859 pMem->u.i = pOp->p2; /* P2 */ |
|
860 pMem->type = SQLITE_INTEGER; |
|
861 pMem++; |
|
862 |
|
863 if( p->explain==1 ){ |
|
864 pMem->flags = MEM_Int; |
|
865 pMem->u.i = pOp->p3; /* P3 */ |
|
866 pMem->type = SQLITE_INTEGER; |
|
867 pMem++; |
|
868 } |
|
869 |
|
870 if( sqlite3VdbeMemGrow(pMem, 32, 0) ){ /* P4 */ |
|
871 p->db->mallocFailed = 1; |
|
872 return SQLITE_NOMEM; |
|
873 } |
|
874 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
|
875 z = displayP4(pOp, pMem->z, 32); |
|
876 if( z!=pMem->z ){ |
|
877 sqlite3VdbeMemSetStr(pMem, z, -1, SQLITE_UTF8, 0); |
|
878 }else{ |
|
879 assert( pMem->z!=0 ); |
|
880 pMem->n = strlen(pMem->z); |
|
881 pMem->enc = SQLITE_UTF8; |
|
882 } |
|
883 pMem->type = SQLITE_TEXT; |
|
884 pMem++; |
|
885 |
|
886 if( p->explain==1 ){ |
|
887 if( sqlite3VdbeMemGrow(pMem, 4, 0) ){ |
|
888 p->db->mallocFailed = 1; |
|
889 return SQLITE_NOMEM; |
|
890 } |
|
891 pMem->flags = MEM_Dyn|MEM_Str|MEM_Term; |
|
892 pMem->n = 2; |
|
893 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ |
|
894 pMem->type = SQLITE_TEXT; |
|
895 pMem->enc = SQLITE_UTF8; |
|
896 pMem++; |
|
897 |
|
898 #ifdef SQLITE_DEBUG |
|
899 if( pOp->zComment ){ |
|
900 pMem->flags = MEM_Str|MEM_Term; |
|
901 pMem->z = pOp->zComment; |
|
902 pMem->n = strlen(pMem->z); |
|
903 pMem->enc = SQLITE_UTF8; |
|
904 }else |
|
905 #endif |
|
906 { |
|
907 pMem->flags = MEM_Null; /* Comment */ |
|
908 pMem->type = SQLITE_NULL; |
|
909 } |
|
910 } |
|
911 |
|
912 p->nResColumn = 8 - 5*(p->explain-1); |
|
913 p->rc = SQLITE_OK; |
|
914 rc = SQLITE_ROW; |
|
915 } |
|
916 return rc; |
|
917 } |
|
918 #endif /* SQLITE_OMIT_EXPLAIN */ |
|
919 |
|
920 #ifdef SQLITE_DEBUG |
|
921 /* |
|
922 ** Print the SQL that was used to generate a VDBE program. |
|
923 */ |
|
924 void sqlite3VdbePrintSql(Vdbe *p){ |
|
925 int nOp = p->nOp; |
|
926 VdbeOp *pOp; |
|
927 if( nOp<1 ) return; |
|
928 pOp = &p->aOp[0]; |
|
929 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
|
930 const char *z = pOp->p4.z; |
|
931 while( isspace(*(u8*)z) ) z++; |
|
932 printf("SQL: [%s]\n", z); |
|
933 } |
|
934 } |
|
935 #endif |
|
936 |
|
937 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
|
938 /* |
|
939 ** Print an IOTRACE message showing SQL content. |
|
940 */ |
|
941 void sqlite3VdbeIOTraceSql(Vdbe *p){ |
|
942 int nOp = p->nOp; |
|
943 VdbeOp *pOp; |
|
944 if( sqlite3IoTrace==0 ) return; |
|
945 if( nOp<1 ) return; |
|
946 pOp = &p->aOp[0]; |
|
947 if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){ |
|
948 int i, j; |
|
949 char z[1000]; |
|
950 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); |
|
951 for(i=0; isspace((unsigned char)z[i]); i++){} |
|
952 for(j=0; z[i]; i++){ |
|
953 if( isspace((unsigned char)z[i]) ){ |
|
954 if( z[i-1]!=' ' ){ |
|
955 z[j++] = ' '; |
|
956 } |
|
957 }else{ |
|
958 z[j++] = z[i]; |
|
959 } |
|
960 } |
|
961 z[j] = 0; |
|
962 sqlite3IoTrace("SQL %s\n", z); |
|
963 } |
|
964 } |
|
965 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ |
|
966 |
|
967 |
|
968 /* |
|
969 ** Prepare a virtual machine for execution. This involves things such |
|
970 ** as allocating stack space and initializing the program counter. |
|
971 ** After the VDBE has be prepped, it can be executed by one or more |
|
972 ** calls to sqlite3VdbeExec(). |
|
973 ** |
|
974 ** This is the only way to move a VDBE from VDBE_MAGIC_INIT to |
|
975 ** VDBE_MAGIC_RUN. |
|
976 */ |
|
977 void sqlite3VdbeMakeReady( |
|
978 Vdbe *p, /* The VDBE */ |
|
979 int nVar, /* Number of '?' see in the SQL statement */ |
|
980 int nMem, /* Number of memory cells to allocate */ |
|
981 int nCursor, /* Number of cursors to allocate */ |
|
982 int isExplain /* True if the EXPLAIN keywords is present */ |
|
983 ){ |
|
984 int n; |
|
985 sqlite3 *db = p->db; |
|
986 |
|
987 assert( p!=0 ); |
|
988 assert( p->magic==VDBE_MAGIC_INIT ); |
|
989 |
|
990 /* There should be at least one opcode. |
|
991 */ |
|
992 assert( p->nOp>0 ); |
|
993 |
|
994 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This |
|
995 * is because the call to resizeOpArray() below may shrink the |
|
996 * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN |
|
997 * state. |
|
998 */ |
|
999 p->magic = VDBE_MAGIC_RUN; |
|
1000 |
|
1001 /* For each cursor required, also allocate a memory cell. Memory |
|
1002 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
|
1003 ** the vdbe program. Instead they are used to allocate space for |
|
1004 ** Cursor/BtCursor structures. The blob of memory associated with |
|
1005 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) |
|
1006 ** stores the blob of memory associated with cursor 1, etc. |
|
1007 ** |
|
1008 ** See also: allocateCursor(). |
|
1009 */ |
|
1010 nMem += nCursor; |
|
1011 |
|
1012 /* |
|
1013 ** Allocation space for registers. |
|
1014 */ |
|
1015 if( p->aMem==0 ){ |
|
1016 int nArg; /* Maximum number of args passed to a user function. */ |
|
1017 resolveP2Values(p, &nArg); |
|
1018 /*resizeOpArray(p, p->nOp);*/ |
|
1019 assert( nVar>=0 ); |
|
1020 if( isExplain && nMem<10 ){ |
|
1021 p->nMem = nMem = 10; |
|
1022 } |
|
1023 p->aMem = sqlite3DbMallocZero(db, |
|
1024 nMem*sizeof(Mem) /* aMem */ |
|
1025 + nVar*sizeof(Mem) /* aVar */ |
|
1026 + nArg*sizeof(Mem*) /* apArg */ |
|
1027 + nVar*sizeof(char*) /* azVar */ |
|
1028 + nCursor*sizeof(Cursor*) + 1 /* apCsr */ |
|
1029 ); |
|
1030 if( !db->mallocFailed ){ |
|
1031 p->aMem--; /* aMem[] goes from 1..nMem */ |
|
1032 p->nMem = nMem; /* not from 0..nMem-1 */ |
|
1033 p->aVar = &p->aMem[nMem+1]; |
|
1034 p->nVar = nVar; |
|
1035 p->okVar = 0; |
|
1036 p->apArg = (Mem**)&p->aVar[nVar]; |
|
1037 p->azVar = (char**)&p->apArg[nArg]; |
|
1038 p->apCsr = (Cursor**)&p->azVar[nVar]; |
|
1039 p->nCursor = nCursor; |
|
1040 for(n=0; n<nVar; n++){ |
|
1041 p->aVar[n].flags = MEM_Null; |
|
1042 p->aVar[n].db = db; |
|
1043 } |
|
1044 for(n=1; n<=nMem; n++){ |
|
1045 p->aMem[n].flags = MEM_Null; |
|
1046 p->aMem[n].db = db; |
|
1047 } |
|
1048 } |
|
1049 } |
|
1050 #ifdef SQLITE_DEBUG |
|
1051 for(n=1; n<p->nMem; n++){ |
|
1052 assert( p->aMem[n].db==db ); |
|
1053 } |
|
1054 #endif |
|
1055 |
|
1056 p->pc = -1; |
|
1057 p->rc = SQLITE_OK; |
|
1058 p->uniqueCnt = 0; |
|
1059 p->errorAction = OE_Abort; |
|
1060 p->explain |= isExplain; |
|
1061 p->magic = VDBE_MAGIC_RUN; |
|
1062 p->nChange = 0; |
|
1063 p->cacheCtr = 1; |
|
1064 p->minWriteFileFormat = 255; |
|
1065 p->openedStatement = 0; |
|
1066 #ifdef VDBE_PROFILE |
|
1067 { |
|
1068 int i; |
|
1069 for(i=0; i<p->nOp; i++){ |
|
1070 p->aOp[i].cnt = 0; |
|
1071 p->aOp[i].cycles = 0; |
|
1072 } |
|
1073 } |
|
1074 #endif |
|
1075 } |
|
1076 |
|
1077 /* |
|
1078 ** Close a VDBE cursor and release all the resources that cursor |
|
1079 ** happens to hold. |
|
1080 */ |
|
1081 void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){ |
|
1082 if( pCx==0 ){ |
|
1083 return; |
|
1084 } |
|
1085 if( pCx->pBt ){ |
|
1086 sqlite3BtreeClose(pCx->pBt); |
|
1087 /* The pCx->pCursor will be close automatically, if it exists, by |
|
1088 ** the call above. */ |
|
1089 }else if( pCx->pCursor ){ |
|
1090 sqlite3BtreeCloseCursor(pCx->pCursor); |
|
1091 } |
|
1092 #ifndef SQLITE_OMIT_VIRTUALTABLE |
|
1093 if( pCx->pVtabCursor ){ |
|
1094 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; |
|
1095 const sqlite3_module *pModule = pCx->pModule; |
|
1096 p->inVtabMethod = 1; |
|
1097 (void)sqlite3SafetyOff(p->db); |
|
1098 pModule->xClose(pVtabCursor); |
|
1099 (void)sqlite3SafetyOn(p->db); |
|
1100 p->inVtabMethod = 0; |
|
1101 } |
|
1102 #endif |
|
1103 if( !pCx->ephemPseudoTable ){ |
|
1104 sqlite3DbFree(p->db, pCx->pData); |
|
1105 } |
|
1106 } |
|
1107 |
|
1108 /* |
|
1109 ** Close all cursors except for VTab cursors that are currently |
|
1110 ** in use. |
|
1111 */ |
|
1112 static void closeAllCursorsExceptActiveVtabs(Vdbe *p){ |
|
1113 int i; |
|
1114 if( p->apCsr==0 ) return; |
|
1115 for(i=0; i<p->nCursor; i++){ |
|
1116 Cursor *pC = p->apCsr[i]; |
|
1117 if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){ |
|
1118 sqlite3VdbeFreeCursor(p, pC); |
|
1119 p->apCsr[i] = 0; |
|
1120 } |
|
1121 } |
|
1122 } |
|
1123 |
|
1124 /* |
|
1125 ** Clean up the VM after execution. |
|
1126 ** |
|
1127 ** This routine will automatically close any cursors, lists, and/or |
|
1128 ** sorters that were left open. It also deletes the values of |
|
1129 ** variables in the aVar[] array. |
|
1130 */ |
|
1131 static void Cleanup(Vdbe *p){ |
|
1132 int i; |
|
1133 sqlite3 *db = p->db; |
|
1134 closeAllCursorsExceptActiveVtabs(p); |
|
1135 for(i=1; i<=p->nMem; i++){ |
|
1136 MemSetTypeFlag(&p->aMem[i], MEM_Null); |
|
1137 } |
|
1138 releaseMemArray(&p->aMem[1], p->nMem); |
|
1139 sqlite3VdbeFifoClear(&p->sFifo); |
|
1140 if( p->contextStack ){ |
|
1141 for(i=0; i<p->contextStackTop; i++){ |
|
1142 sqlite3VdbeFifoClear(&p->contextStack[i].sFifo); |
|
1143 } |
|
1144 sqlite3DbFree(db, p->contextStack); |
|
1145 } |
|
1146 p->contextStack = 0; |
|
1147 p->contextStackDepth = 0; |
|
1148 p->contextStackTop = 0; |
|
1149 sqlite3DbFree(db, p->zErrMsg); |
|
1150 p->zErrMsg = 0; |
|
1151 p->pResultSet = 0; |
|
1152 } |
|
1153 |
|
1154 /* |
|
1155 ** Set the number of result columns that will be returned by this SQL |
|
1156 ** statement. This is now set at compile time, rather than during |
|
1157 ** execution of the vdbe program so that sqlite3_column_count() can |
|
1158 ** be called on an SQL statement before sqlite3_step(). |
|
1159 */ |
|
1160 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
|
1161 Mem *pColName; |
|
1162 int n; |
|
1163 sqlite3 *db = p->db; |
|
1164 |
|
1165 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
|
1166 sqlite3DbFree(db, p->aColName); |
|
1167 n = nResColumn*COLNAME_N; |
|
1168 p->nResColumn = nResColumn; |
|
1169 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); |
|
1170 if( p->aColName==0 ) return; |
|
1171 while( n-- > 0 ){ |
|
1172 pColName->flags = MEM_Null; |
|
1173 pColName->db = p->db; |
|
1174 pColName++; |
|
1175 } |
|
1176 } |
|
1177 |
|
1178 /* |
|
1179 ** Set the name of the idx'th column to be returned by the SQL statement. |
|
1180 ** zName must be a pointer to a nul terminated string. |
|
1181 ** |
|
1182 ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
|
1183 ** |
|
1184 ** If N==P4_STATIC it means that zName is a pointer to a constant static |
|
1185 ** string and we can just copy the pointer. If it is P4_DYNAMIC, then |
|
1186 ** the string is freed using sqlite3DbFree(db, ) when the vdbe is finished with |
|
1187 ** it. Otherwise, N bytes of zName are copied. |
|
1188 */ |
|
1189 int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){ |
|
1190 int rc; |
|
1191 Mem *pColName; |
|
1192 assert( idx<p->nResColumn ); |
|
1193 assert( var<COLNAME_N ); |
|
1194 if( p->db->mallocFailed ) return SQLITE_NOMEM; |
|
1195 assert( p->aColName!=0 ); |
|
1196 pColName = &(p->aColName[idx+var*p->nResColumn]); |
|
1197 if( N==P4_DYNAMIC || N==P4_STATIC ){ |
|
1198 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC); |
|
1199 }else{ |
|
1200 rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT); |
|
1201 } |
|
1202 if( rc==SQLITE_OK && N==P4_DYNAMIC ){ |
|
1203 pColName->flags &= (~MEM_Static); |
|
1204 pColName->zMalloc = pColName->z; |
|
1205 } |
|
1206 return rc; |
|
1207 } |
|
1208 |
|
1209 /* |
|
1210 ** A read or write transaction may or may not be active on database handle |
|
1211 ** db. If a transaction is active, commit it. If there is a |
|
1212 ** write-transaction spanning more than one database file, this routine |
|
1213 ** takes care of the master journal trickery. |
|
1214 */ |
|
1215 static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
|
1216 int i; |
|
1217 int nTrans = 0; /* Number of databases with an active write-transaction */ |
|
1218 int rc = SQLITE_OK; |
|
1219 int needXcommit = 0; |
|
1220 |
|
1221 /* Before doing anything else, call the xSync() callback for any |
|
1222 ** virtual module tables written in this transaction. This has to |
|
1223 ** be done before determining whether a master journal file is |
|
1224 ** required, as an xSync() callback may add an attached database |
|
1225 ** to the transaction. |
|
1226 */ |
|
1227 rc = sqlite3VtabSync(db, &p->zErrMsg); |
|
1228 if( rc!=SQLITE_OK ){ |
|
1229 return rc; |
|
1230 } |
|
1231 |
|
1232 /* This loop determines (a) if the commit hook should be invoked and |
|
1233 ** (b) how many database files have open write transactions, not |
|
1234 ** including the temp database. (b) is important because if more than |
|
1235 ** one database file has an open write transaction, a master journal |
|
1236 ** file is required for an atomic commit. |
|
1237 */ |
|
1238 for(i=0; i<db->nDb; i++){ |
|
1239 Btree *pBt = db->aDb[i].pBt; |
|
1240 if( sqlite3BtreeIsInTrans(pBt) ){ |
|
1241 needXcommit = 1; |
|
1242 if( i!=1 ) nTrans++; |
|
1243 } |
|
1244 } |
|
1245 |
|
1246 /* If there are any write-transactions at all, invoke the commit hook */ |
|
1247 if( needXcommit && db->xCommitCallback ){ |
|
1248 (void)sqlite3SafetyOff(db); |
|
1249 rc = db->xCommitCallback(db->pCommitArg); |
|
1250 (void)sqlite3SafetyOn(db); |
|
1251 if( rc ){ |
|
1252 return SQLITE_CONSTRAINT; |
|
1253 } |
|
1254 } |
|
1255 |
|
1256 /* The simple case - no more than one database file (not counting the |
|
1257 ** TEMP database) has a transaction active. There is no need for the |
|
1258 ** master-journal. |
|
1259 ** |
|
1260 ** If the return value of sqlite3BtreeGetFilename() is a zero length |
|
1261 ** string, it means the main database is :memory: or a temp file. In |
|
1262 ** that case we do not support atomic multi-file commits, so use the |
|
1263 ** simple case then too. |
|
1264 */ |
|
1265 if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){ |
|
1266 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
|
1267 Btree *pBt = db->aDb[i].pBt; |
|
1268 if( pBt ){ |
|
1269 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); |
|
1270 } |
|
1271 } |
|
1272 |
|
1273 /* Do the commit only if all databases successfully complete phase 1. |
|
1274 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an |
|
1275 ** IO error while deleting or truncating a journal file. It is unlikely, |
|
1276 ** but could happen. In this case abandon processing and return the error. |
|
1277 */ |
|
1278 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
|
1279 Btree *pBt = db->aDb[i].pBt; |
|
1280 if( pBt ){ |
|
1281 rc = sqlite3BtreeCommitPhaseTwo(pBt); |
|
1282 } |
|
1283 } |
|
1284 if( rc==SQLITE_OK ){ |
|
1285 sqlite3VtabCommit(db); |
|
1286 } |
|
1287 } |
|
1288 |
|
1289 /* The complex case - There is a multi-file write-transaction active. |
|
1290 ** This requires a master journal file to ensure the transaction is |
|
1291 ** committed atomicly. |
|
1292 */ |
|
1293 #ifndef SQLITE_OMIT_DISKIO |
|
1294 else{ |
|
1295 sqlite3_vfs *pVfs = db->pVfs; |
|
1296 int needSync = 0; |
|
1297 char *zMaster = 0; /* File-name for the master journal */ |
|
1298 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
|
1299 sqlite3_file *pMaster = 0; |
|
1300 i64 offset = 0; |
|
1301 int res; |
|
1302 |
|
1303 /* Select a master journal file name */ |
|
1304 do { |
|
1305 u32 random; |
|
1306 sqlite3DbFree(db, zMaster); |
|
1307 sqlite3_randomness(sizeof(random), &random); |
|
1308 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff); |
|
1309 if( !zMaster ){ |
|
1310 return SQLITE_NOMEM; |
|
1311 } |
|
1312 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); |
|
1313 }while( rc==SQLITE_OK && res ); |
|
1314 if( rc==SQLITE_OK ){ |
|
1315 /* Open the master journal. */ |
|
1316 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, |
|
1317 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| |
|
1318 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 |
|
1319 ); |
|
1320 } |
|
1321 if( rc!=SQLITE_OK ){ |
|
1322 sqlite3DbFree(db, zMaster); |
|
1323 return rc; |
|
1324 } |
|
1325 |
|
1326 /* Write the name of each database file in the transaction into the new |
|
1327 ** master journal file. If an error occurs at this point close |
|
1328 ** and delete the master journal file. All the individual journal files |
|
1329 ** still have 'null' as the master journal pointer, so they will roll |
|
1330 ** back independently if a failure occurs. |
|
1331 */ |
|
1332 for(i=0; i<db->nDb; i++){ |
|
1333 Btree *pBt = db->aDb[i].pBt; |
|
1334 if( i==1 ) continue; /* Ignore the TEMP database */ |
|
1335 if( sqlite3BtreeIsInTrans(pBt) ){ |
|
1336 char const *zFile = sqlite3BtreeGetJournalname(pBt); |
|
1337 if( zFile[0]==0 ) continue; /* Ignore :memory: databases */ |
|
1338 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ |
|
1339 needSync = 1; |
|
1340 } |
|
1341 rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset); |
|
1342 offset += strlen(zFile)+1; |
|
1343 if( rc!=SQLITE_OK ){ |
|
1344 sqlite3OsCloseFree(pMaster); |
|
1345 sqlite3OsDelete(pVfs, zMaster, 0); |
|
1346 sqlite3DbFree(db, zMaster); |
|
1347 return rc; |
|
1348 } |
|
1349 } |
|
1350 } |
|
1351 |
|
1352 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device |
|
1353 ** flag is set this is not required. |
|
1354 */ |
|
1355 zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt); |
|
1356 if( (needSync |
|
1357 && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)) |
|
1358 && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){ |
|
1359 sqlite3OsCloseFree(pMaster); |
|
1360 sqlite3OsDelete(pVfs, zMaster, 0); |
|
1361 sqlite3DbFree(db, zMaster); |
|
1362 return rc; |
|
1363 } |
|
1364 |
|
1365 /* Sync all the db files involved in the transaction. The same call |
|
1366 ** sets the master journal pointer in each individual journal. If |
|
1367 ** an error occurs here, do not delete the master journal file. |
|
1368 ** |
|
1369 ** If the error occurs during the first call to |
|
1370 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the |
|
1371 ** master journal file will be orphaned. But we cannot delete it, |
|
1372 ** in case the master journal file name was written into the journal |
|
1373 ** file before the failure occured. |
|
1374 */ |
|
1375 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
|
1376 Btree *pBt = db->aDb[i].pBt; |
|
1377 if( pBt ){ |
|
1378 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); |
|
1379 } |
|
1380 } |
|
1381 sqlite3OsCloseFree(pMaster); |
|
1382 if( rc!=SQLITE_OK ){ |
|
1383 sqlite3DbFree(db, zMaster); |
|
1384 return rc; |
|
1385 } |
|
1386 |
|
1387 /* Delete the master journal file. This commits the transaction. After |
|
1388 ** doing this the directory is synced again before any individual |
|
1389 ** transaction files are deleted. |
|
1390 */ |
|
1391 rc = sqlite3OsDelete(pVfs, zMaster, 1); |
|
1392 sqlite3DbFree(db, zMaster); |
|
1393 zMaster = 0; |
|
1394 if( rc ){ |
|
1395 return rc; |
|
1396 } |
|
1397 |
|
1398 /* All files and directories have already been synced, so the following |
|
1399 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and |
|
1400 ** deleting or truncating journals. If something goes wrong while |
|
1401 ** this is happening we don't really care. The integrity of the |
|
1402 ** transaction is already guaranteed, but some stray 'cold' journals |
|
1403 ** may be lying around. Returning an error code won't help matters. |
|
1404 */ |
|
1405 disable_simulated_io_errors(); |
|
1406 sqlite3BeginBenignMalloc(); |
|
1407 for(i=0; i<db->nDb; i++){ |
|
1408 Btree *pBt = db->aDb[i].pBt; |
|
1409 if( pBt ){ |
|
1410 sqlite3BtreeCommitPhaseTwo(pBt); |
|
1411 } |
|
1412 } |
|
1413 sqlite3EndBenignMalloc(); |
|
1414 enable_simulated_io_errors(); |
|
1415 |
|
1416 sqlite3VtabCommit(db); |
|
1417 } |
|
1418 #endif |
|
1419 |
|
1420 return rc; |
|
1421 } |
|
1422 |
|
1423 /* |
|
1424 ** This routine checks that the sqlite3.activeVdbeCnt count variable |
|
1425 ** matches the number of vdbe's in the list sqlite3.pVdbe that are |
|
1426 ** currently active. An assertion fails if the two counts do not match. |
|
1427 ** This is an internal self-check only - it is not an essential processing |
|
1428 ** step. |
|
1429 ** |
|
1430 ** This is a no-op if NDEBUG is defined. |
|
1431 */ |
|
1432 #ifndef NDEBUG |
|
1433 static void checkActiveVdbeCnt(sqlite3 *db){ |
|
1434 Vdbe *p; |
|
1435 int cnt = 0; |
|
1436 p = db->pVdbe; |
|
1437 while( p ){ |
|
1438 if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){ |
|
1439 cnt++; |
|
1440 } |
|
1441 p = p->pNext; |
|
1442 } |
|
1443 assert( cnt==db->activeVdbeCnt ); |
|
1444 } |
|
1445 #else |
|
1446 #define checkActiveVdbeCnt(x) |
|
1447 #endif |
|
1448 |
|
1449 /* |
|
1450 ** For every Btree that in database connection db which |
|
1451 ** has been modified, "trip" or invalidate each cursor in |
|
1452 ** that Btree might have been modified so that the cursor |
|
1453 ** can never be used again. This happens when a rollback |
|
1454 *** occurs. We have to trip all the other cursors, even |
|
1455 ** cursor from other VMs in different database connections, |
|
1456 ** so that none of them try to use the data at which they |
|
1457 ** were pointing and which now may have been changed due |
|
1458 ** to the rollback. |
|
1459 ** |
|
1460 ** Remember that a rollback can delete tables complete and |
|
1461 ** reorder rootpages. So it is not sufficient just to save |
|
1462 ** the state of the cursor. We have to invalidate the cursor |
|
1463 ** so that it is never used again. |
|
1464 */ |
|
1465 static void invalidateCursorsOnModifiedBtrees(sqlite3 *db){ |
|
1466 int i; |
|
1467 for(i=0; i<db->nDb; i++){ |
|
1468 Btree *p = db->aDb[i].pBt; |
|
1469 if( p && sqlite3BtreeIsInTrans(p) ){ |
|
1470 sqlite3BtreeTripAllCursors(p, SQLITE_ABORT); |
|
1471 } |
|
1472 } |
|
1473 } |
|
1474 |
|
1475 /* |
|
1476 ** This routine is called the when a VDBE tries to halt. If the VDBE |
|
1477 ** has made changes and is in autocommit mode, then commit those |
|
1478 ** changes. If a rollback is needed, then do the rollback. |
|
1479 ** |
|
1480 ** This routine is the only way to move the state of a VM from |
|
1481 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to |
|
1482 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. |
|
1483 ** |
|
1484 ** Return an error code. If the commit could not complete because of |
|
1485 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it |
|
1486 ** means the close did not happen and needs to be repeated. |
|
1487 */ |
|
1488 int sqlite3VdbeHalt(Vdbe *p){ |
|
1489 sqlite3 *db = p->db; |
|
1490 int i; |
|
1491 int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */ |
|
1492 int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */ |
|
1493 |
|
1494 /* This function contains the logic that determines if a statement or |
|
1495 ** transaction will be committed or rolled back as a result of the |
|
1496 ** execution of this virtual machine. |
|
1497 ** |
|
1498 ** If any of the following errors occur: |
|
1499 ** |
|
1500 ** SQLITE_NOMEM |
|
1501 ** SQLITE_IOERR |
|
1502 ** SQLITE_FULL |
|
1503 ** SQLITE_INTERRUPT |
|
1504 ** |
|
1505 ** Then the internal cache might have been left in an inconsistent |
|
1506 ** state. We need to rollback the statement transaction, if there is |
|
1507 ** one, or the complete transaction if there is no statement transaction. |
|
1508 */ |
|
1509 |
|
1510 if( p->db->mallocFailed ){ |
|
1511 p->rc = SQLITE_NOMEM; |
|
1512 } |
|
1513 closeAllCursorsExceptActiveVtabs(p); |
|
1514 if( p->magic!=VDBE_MAGIC_RUN ){ |
|
1515 return SQLITE_OK; |
|
1516 } |
|
1517 checkActiveVdbeCnt(db); |
|
1518 |
|
1519 /* No commit or rollback needed if the program never started */ |
|
1520 if( p->pc>=0 ){ |
|
1521 int mrc; /* Primary error code from p->rc */ |
|
1522 |
|
1523 /* Lock all btrees used by the statement */ |
|
1524 sqlite3BtreeMutexArrayEnter(&p->aMutex); |
|
1525 |
|
1526 /* Check for one of the special errors */ |
|
1527 mrc = p->rc & 0xff; |
|
1528 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR |
|
1529 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; |
|
1530 if( isSpecialError ){ |
|
1531 /* This loop does static analysis of the query to see which of the |
|
1532 ** following three categories it falls into: |
|
1533 ** |
|
1534 ** Read-only |
|
1535 ** Query with statement journal |
|
1536 ** Query without statement journal |
|
1537 ** |
|
1538 ** We could do something more elegant than this static analysis (i.e. |
|
1539 ** store the type of query as part of the compliation phase), but |
|
1540 ** handling malloc() or IO failure is a fairly obscure edge case so |
|
1541 ** this is probably easier. Todo: Might be an opportunity to reduce |
|
1542 ** code size a very small amount though... |
|
1543 */ |
|
1544 int notReadOnly = 0; |
|
1545 int isStatement = 0; |
|
1546 assert(p->aOp || p->nOp==0); |
|
1547 for(i=0; i<p->nOp; i++){ |
|
1548 switch( p->aOp[i].opcode ){ |
|
1549 case OP_Transaction: |
|
1550 notReadOnly |= p->aOp[i].p2; |
|
1551 break; |
|
1552 case OP_Statement: |
|
1553 isStatement = 1; |
|
1554 break; |
|
1555 } |
|
1556 } |
|
1557 |
|
1558 |
|
1559 /* If the query was read-only, we need do no rollback at all. Otherwise, |
|
1560 ** proceed with the special handling. |
|
1561 */ |
|
1562 if( notReadOnly || mrc!=SQLITE_INTERRUPT ){ |
|
1563 if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){ |
|
1564 xFunc = sqlite3BtreeRollbackStmt; |
|
1565 p->rc = SQLITE_BUSY; |
|
1566 } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){ |
|
1567 xFunc = sqlite3BtreeRollbackStmt; |
|
1568 }else{ |
|
1569 /* We are forced to roll back the active transaction. Before doing |
|
1570 ** so, abort any other statements this handle currently has active. |
|
1571 */ |
|
1572 invalidateCursorsOnModifiedBtrees(db); |
|
1573 sqlite3RollbackAll(db); |
|
1574 db->autoCommit = 1; |
|
1575 } |
|
1576 } |
|
1577 } |
|
1578 |
|
1579 /* If the auto-commit flag is set and this is the only active vdbe, then |
|
1580 ** we do either a commit or rollback of the current transaction. |
|
1581 ** |
|
1582 ** Note: This block also runs if one of the special errors handled |
|
1583 ** above has occured. |
|
1584 */ |
|
1585 if( db->autoCommit && db->activeVdbeCnt==1 ){ |
|
1586 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ |
|
1587 /* The auto-commit flag is true, and the vdbe program was |
|
1588 ** successful or hit an 'OR FAIL' constraint. This means a commit |
|
1589 ** is required. |
|
1590 */ |
|
1591 int rc = vdbeCommit(db, p); |
|
1592 if( rc==SQLITE_BUSY ){ |
|
1593 sqlite3BtreeMutexArrayLeave(&p->aMutex); |
|
1594 return SQLITE_BUSY; |
|
1595 }else if( rc!=SQLITE_OK ){ |
|
1596 p->rc = rc; |
|
1597 sqlite3RollbackAll(db); |
|
1598 }else{ |
|
1599 sqlite3CommitInternalChanges(db); |
|
1600 } |
|
1601 }else{ |
|
1602 sqlite3RollbackAll(db); |
|
1603 } |
|
1604 }else if( !xFunc ){ |
|
1605 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
|
1606 if( p->openedStatement ){ |
|
1607 xFunc = sqlite3BtreeCommitStmt; |
|
1608 } |
|
1609 }else if( p->errorAction==OE_Abort ){ |
|
1610 xFunc = sqlite3BtreeRollbackStmt; |
|
1611 }else{ |
|
1612 invalidateCursorsOnModifiedBtrees(db); |
|
1613 sqlite3RollbackAll(db); |
|
1614 db->autoCommit = 1; |
|
1615 } |
|
1616 } |
|
1617 |
|
1618 /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or |
|
1619 ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs |
|
1620 ** and the return code is still SQLITE_OK, set the return code to the new |
|
1621 ** error value. |
|
1622 */ |
|
1623 assert(!xFunc || |
|
1624 xFunc==sqlite3BtreeCommitStmt || |
|
1625 xFunc==sqlite3BtreeRollbackStmt |
|
1626 ); |
|
1627 for(i=0; xFunc && i<db->nDb; i++){ |
|
1628 int rc; |
|
1629 Btree *pBt = db->aDb[i].pBt; |
|
1630 if( pBt ){ |
|
1631 rc = xFunc(pBt); |
|
1632 if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){ |
|
1633 p->rc = rc; |
|
1634 sqlite3DbFree(db, p->zErrMsg); |
|
1635 p->zErrMsg = 0; |
|
1636 } |
|
1637 } |
|
1638 } |
|
1639 |
|
1640 /* If this was an INSERT, UPDATE or DELETE and the statement was committed, |
|
1641 ** set the change counter. |
|
1642 */ |
|
1643 if( p->changeCntOn && p->pc>=0 ){ |
|
1644 if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){ |
|
1645 sqlite3VdbeSetChanges(db, p->nChange); |
|
1646 }else{ |
|
1647 sqlite3VdbeSetChanges(db, 0); |
|
1648 } |
|
1649 p->nChange = 0; |
|
1650 } |
|
1651 |
|
1652 /* Rollback or commit any schema changes that occurred. */ |
|
1653 if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){ |
|
1654 sqlite3ResetInternalSchema(db, 0); |
|
1655 db->flags = (db->flags | SQLITE_InternChanges); |
|
1656 } |
|
1657 |
|
1658 /* Release the locks */ |
|
1659 sqlite3BtreeMutexArrayLeave(&p->aMutex); |
|
1660 } |
|
1661 |
|
1662 /* We have successfully halted and closed the VM. Record this fact. */ |
|
1663 if( p->pc>=0 ){ |
|
1664 db->activeVdbeCnt--; |
|
1665 } |
|
1666 p->magic = VDBE_MAGIC_HALT; |
|
1667 checkActiveVdbeCnt(db); |
|
1668 if( p->db->mallocFailed ){ |
|
1669 p->rc = SQLITE_NOMEM; |
|
1670 } |
|
1671 |
|
1672 return SQLITE_OK; |
|
1673 } |
|
1674 |
|
1675 |
|
1676 /* |
|
1677 ** Each VDBE holds the result of the most recent sqlite3_step() call |
|
1678 ** in p->rc. This routine sets that result back to SQLITE_OK. |
|
1679 */ |
|
1680 void sqlite3VdbeResetStepResult(Vdbe *p){ |
|
1681 p->rc = SQLITE_OK; |
|
1682 } |
|
1683 |
|
1684 /* |
|
1685 ** Clean up a VDBE after execution but do not delete the VDBE just yet. |
|
1686 ** Write any error messages into *pzErrMsg. Return the result code. |
|
1687 ** |
|
1688 ** After this routine is run, the VDBE should be ready to be executed |
|
1689 ** again. |
|
1690 ** |
|
1691 ** To look at it another way, this routine resets the state of the |
|
1692 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to |
|
1693 ** VDBE_MAGIC_INIT. |
|
1694 */ |
|
1695 int sqlite3VdbeReset(Vdbe *p){ |
|
1696 sqlite3 *db; |
|
1697 db = p->db; |
|
1698 |
|
1699 /* If the VM did not run to completion or if it encountered an |
|
1700 ** error, then it might not have been halted properly. So halt |
|
1701 ** it now. |
|
1702 */ |
|
1703 (void)sqlite3SafetyOn(db); |
|
1704 sqlite3VdbeHalt(p); |
|
1705 (void)sqlite3SafetyOff(db); |
|
1706 |
|
1707 /* If the VDBE has be run even partially, then transfer the error code |
|
1708 ** and error message from the VDBE into the main database structure. But |
|
1709 ** if the VDBE has just been set to run but has not actually executed any |
|
1710 ** instructions yet, leave the main database error information unchanged. |
|
1711 */ |
|
1712 if( p->pc>=0 ){ |
|
1713 if( p->zErrMsg ){ |
|
1714 sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,SQLITE_TRANSIENT); |
|
1715 db->errCode = p->rc; |
|
1716 sqlite3DbFree(db, p->zErrMsg); |
|
1717 p->zErrMsg = 0; |
|
1718 }else if( p->rc ){ |
|
1719 sqlite3Error(db, p->rc, 0); |
|
1720 }else{ |
|
1721 sqlite3Error(db, SQLITE_OK, 0); |
|
1722 } |
|
1723 }else if( p->rc && p->expired ){ |
|
1724 /* The expired flag was set on the VDBE before the first call |
|
1725 ** to sqlite3_step(). For consistency (since sqlite3_step() was |
|
1726 ** called), set the database error in this case as well. |
|
1727 */ |
|
1728 sqlite3Error(db, p->rc, 0); |
|
1729 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
|
1730 sqlite3DbFree(db, p->zErrMsg); |
|
1731 p->zErrMsg = 0; |
|
1732 } |
|
1733 |
|
1734 /* Reclaim all memory used by the VDBE |
|
1735 */ |
|
1736 Cleanup(p); |
|
1737 |
|
1738 /* Save profiling information from this VDBE run. |
|
1739 */ |
|
1740 #ifdef VDBE_PROFILE |
|
1741 { |
|
1742 FILE *out = fopen("vdbe_profile.out", "a"); |
|
1743 if( out ){ |
|
1744 int i; |
|
1745 fprintf(out, "---- "); |
|
1746 for(i=0; i<p->nOp; i++){ |
|
1747 fprintf(out, "%02x", p->aOp[i].opcode); |
|
1748 } |
|
1749 fprintf(out, "\n"); |
|
1750 for(i=0; i<p->nOp; i++){ |
|
1751 fprintf(out, "%6d %10lld %8lld ", |
|
1752 p->aOp[i].cnt, |
|
1753 p->aOp[i].cycles, |
|
1754 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
|
1755 ); |
|
1756 sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
|
1757 } |
|
1758 fclose(out); |
|
1759 } |
|
1760 } |
|
1761 #endif |
|
1762 p->magic = VDBE_MAGIC_INIT; |
|
1763 return p->rc & db->errMask; |
|
1764 } |
|
1765 |
|
1766 /* |
|
1767 ** Clean up and delete a VDBE after execution. Return an integer which is |
|
1768 ** the result code. Write any error message text into *pzErrMsg. |
|
1769 */ |
|
1770 int sqlite3VdbeFinalize(Vdbe *p){ |
|
1771 int rc = SQLITE_OK; |
|
1772 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
|
1773 rc = sqlite3VdbeReset(p); |
|
1774 assert( (rc & p->db->errMask)==rc ); |
|
1775 }else if( p->magic!=VDBE_MAGIC_INIT ){ |
|
1776 return SQLITE_MISUSE; |
|
1777 } |
|
1778 sqlite3VdbeDelete(p); |
|
1779 return rc; |
|
1780 } |
|
1781 |
|
1782 /* |
|
1783 ** Call the destructor for each auxdata entry in pVdbeFunc for which |
|
1784 ** the corresponding bit in mask is clear. Auxdata entries beyond 31 |
|
1785 ** are always destroyed. To destroy all auxdata entries, call this |
|
1786 ** routine with mask==0. |
|
1787 */ |
|
1788 void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){ |
|
1789 int i; |
|
1790 for(i=0; i<pVdbeFunc->nAux; i++){ |
|
1791 struct AuxData *pAux = &pVdbeFunc->apAux[i]; |
|
1792 if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){ |
|
1793 if( pAux->xDelete ){ |
|
1794 pAux->xDelete(pAux->pAux); |
|
1795 } |
|
1796 pAux->pAux = 0; |
|
1797 } |
|
1798 } |
|
1799 } |
|
1800 |
|
1801 /* |
|
1802 ** Delete an entire VDBE. |
|
1803 */ |
|
1804 void sqlite3VdbeDelete(Vdbe *p){ |
|
1805 int i; |
|
1806 sqlite3 *db; |
|
1807 |
|
1808 if( p==0 ) return; |
|
1809 db = p->db; |
|
1810 if( p->pPrev ){ |
|
1811 p->pPrev->pNext = p->pNext; |
|
1812 }else{ |
|
1813 assert( db->pVdbe==p ); |
|
1814 db->pVdbe = p->pNext; |
|
1815 } |
|
1816 if( p->pNext ){ |
|
1817 p->pNext->pPrev = p->pPrev; |
|
1818 } |
|
1819 if( p->aOp ){ |
|
1820 Op *pOp = p->aOp; |
|
1821 for(i=0; i<p->nOp; i++, pOp++){ |
|
1822 freeP4(db, pOp->p4type, pOp->p4.p); |
|
1823 #ifdef SQLITE_DEBUG |
|
1824 sqlite3DbFree(db, pOp->zComment); |
|
1825 #endif |
|
1826 } |
|
1827 sqlite3DbFree(db, p->aOp); |
|
1828 } |
|
1829 releaseMemArray(p->aVar, p->nVar); |
|
1830 sqlite3DbFree(db, p->aLabel); |
|
1831 if( p->aMem ){ |
|
1832 sqlite3DbFree(db, &p->aMem[1]); |
|
1833 } |
|
1834 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
|
1835 sqlite3DbFree(db, p->aColName); |
|
1836 sqlite3DbFree(db, p->zSql); |
|
1837 p->magic = VDBE_MAGIC_DEAD; |
|
1838 sqlite3DbFree(db, p); |
|
1839 } |
|
1840 |
|
1841 /* |
|
1842 ** If a MoveTo operation is pending on the given cursor, then do that |
|
1843 ** MoveTo now. Return an error code. If no MoveTo is pending, this |
|
1844 ** routine does nothing and returns SQLITE_OK. |
|
1845 */ |
|
1846 int sqlite3VdbeCursorMoveto(Cursor *p){ |
|
1847 if( p->deferredMoveto ){ |
|
1848 int res, rc; |
|
1849 #ifdef SQLITE_TEST |
|
1850 extern int sqlite3_search_count; |
|
1851 #endif |
|
1852 assert( p->isTable ); |
|
1853 rc = sqlite3BtreeMoveto(p->pCursor, 0, 0, p->movetoTarget, 0, &res); |
|
1854 if( rc ) return rc; |
|
1855 *p->pIncrKey = 0; |
|
1856 p->lastRowid = keyToInt(p->movetoTarget); |
|
1857 p->rowidIsValid = res==0; |
|
1858 if( res<0 ){ |
|
1859 rc = sqlite3BtreeNext(p->pCursor, &res); |
|
1860 if( rc ) return rc; |
|
1861 } |
|
1862 #ifdef SQLITE_TEST |
|
1863 sqlite3_search_count++; |
|
1864 #endif |
|
1865 p->deferredMoveto = 0; |
|
1866 p->cacheStatus = CACHE_STALE; |
|
1867 }else if( p->pCursor ){ |
|
1868 int hasMoved; |
|
1869 int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved); |
|
1870 if( rc ) return rc; |
|
1871 if( hasMoved ){ |
|
1872 p->cacheStatus = CACHE_STALE; |
|
1873 p->nullRow = 1; |
|
1874 } |
|
1875 } |
|
1876 return SQLITE_OK; |
|
1877 } |
|
1878 |
|
1879 /* |
|
1880 ** The following functions: |
|
1881 ** |
|
1882 ** sqlite3VdbeSerialType() |
|
1883 ** sqlite3VdbeSerialTypeLen() |
|
1884 ** sqlite3VdbeSerialLen() |
|
1885 ** sqlite3VdbeSerialPut() |
|
1886 ** sqlite3VdbeSerialGet() |
|
1887 ** |
|
1888 ** encapsulate the code that serializes values for storage in SQLite |
|
1889 ** data and index records. Each serialized value consists of a |
|
1890 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned |
|
1891 ** integer, stored as a varint. |
|
1892 ** |
|
1893 ** In an SQLite index record, the serial type is stored directly before |
|
1894 ** the blob of data that it corresponds to. In a table record, all serial |
|
1895 ** types are stored at the start of the record, and the blobs of data at |
|
1896 ** the end. Hence these functions allow the caller to handle the |
|
1897 ** serial-type and data blob seperately. |
|
1898 ** |
|
1899 ** The following table describes the various storage classes for data: |
|
1900 ** |
|
1901 ** serial type bytes of data type |
|
1902 ** -------------- --------------- --------------- |
|
1903 ** 0 0 NULL |
|
1904 ** 1 1 signed integer |
|
1905 ** 2 2 signed integer |
|
1906 ** 3 3 signed integer |
|
1907 ** 4 4 signed integer |
|
1908 ** 5 6 signed integer |
|
1909 ** 6 8 signed integer |
|
1910 ** 7 8 IEEE float |
|
1911 ** 8 0 Integer constant 0 |
|
1912 ** 9 0 Integer constant 1 |
|
1913 ** 10,11 reserved for expansion |
|
1914 ** N>=12 and even (N-12)/2 BLOB |
|
1915 ** N>=13 and odd (N-13)/2 text |
|
1916 ** |
|
1917 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
|
1918 ** of SQLite will not understand those serial types. |
|
1919 */ |
|
1920 |
|
1921 /* |
|
1922 ** Return the serial-type for the value stored in pMem. |
|
1923 */ |
|
1924 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ |
|
1925 int flags = pMem->flags; |
|
1926 int n; |
|
1927 |
|
1928 if( flags&MEM_Null ){ |
|
1929 return 0; |
|
1930 } |
|
1931 if( flags&MEM_Int ){ |
|
1932 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
|
1933 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
|
1934 i64 i = pMem->u.i; |
|
1935 u64 u; |
|
1936 if( file_format>=4 && (i&1)==i ){ |
|
1937 return 8+i; |
|
1938 } |
|
1939 u = i<0 ? -i : i; |
|
1940 if( u<=127 ) return 1; |
|
1941 if( u<=32767 ) return 2; |
|
1942 if( u<=8388607 ) return 3; |
|
1943 if( u<=2147483647 ) return 4; |
|
1944 if( u<=MAX_6BYTE ) return 5; |
|
1945 return 6; |
|
1946 } |
|
1947 if( flags&MEM_Real ){ |
|
1948 return 7; |
|
1949 } |
|
1950 assert( flags&(MEM_Str|MEM_Blob) ); |
|
1951 n = pMem->n; |
|
1952 if( flags & MEM_Zero ){ |
|
1953 n += pMem->u.i; |
|
1954 } |
|
1955 assert( n>=0 ); |
|
1956 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
|
1957 } |
|
1958 |
|
1959 /* |
|
1960 ** Return the length of the data corresponding to the supplied serial-type. |
|
1961 */ |
|
1962 int sqlite3VdbeSerialTypeLen(u32 serial_type){ |
|
1963 if( serial_type>=12 ){ |
|
1964 return (serial_type-12)/2; |
|
1965 }else{ |
|
1966 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; |
|
1967 return aSize[serial_type]; |
|
1968 } |
|
1969 } |
|
1970 |
|
1971 /* |
|
1972 ** If we are on an architecture with mixed-endian floating |
|
1973 ** points (ex: ARM7) then swap the lower 4 bytes with the |
|
1974 ** upper 4 bytes. Return the result. |
|
1975 ** |
|
1976 ** For most architectures, this is a no-op. |
|
1977 ** |
|
1978 ** (later): It is reported to me that the mixed-endian problem |
|
1979 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems |
|
1980 ** that early versions of GCC stored the two words of a 64-bit |
|
1981 ** float in the wrong order. And that error has been propagated |
|
1982 ** ever since. The blame is not necessarily with GCC, though. |
|
1983 ** GCC might have just copying the problem from a prior compiler. |
|
1984 ** I am also told that newer versions of GCC that follow a different |
|
1985 ** ABI get the byte order right. |
|
1986 ** |
|
1987 ** Developers using SQLite on an ARM7 should compile and run their |
|
1988 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG |
|
1989 ** enabled, some asserts below will ensure that the byte order of |
|
1990 ** floating point values is correct. |
|
1991 ** |
|
1992 ** (2007-08-30) Frank van Vugt has studied this problem closely |
|
1993 ** and has send his findings to the SQLite developers. Frank |
|
1994 ** writes that some Linux kernels offer floating point hardware |
|
1995 ** emulation that uses only 32-bit mantissas instead of a full |
|
1996 ** 48-bits as required by the IEEE standard. (This is the |
|
1997 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point |
|
1998 ** byte swapping becomes very complicated. To avoid problems, |
|
1999 ** the necessary byte swapping is carried out using a 64-bit integer |
|
2000 ** rather than a 64-bit float. Frank assures us that the code here |
|
2001 ** works for him. We, the developers, have no way to independently |
|
2002 ** verify this, but Frank seems to know what he is talking about |
|
2003 ** so we trust him. |
|
2004 */ |
|
2005 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT |
|
2006 static u64 floatSwap(u64 in){ |
|
2007 union { |
|
2008 u64 r; |
|
2009 u32 i[2]; |
|
2010 } u; |
|
2011 u32 t; |
|
2012 |
|
2013 u.r = in; |
|
2014 t = u.i[0]; |
|
2015 u.i[0] = u.i[1]; |
|
2016 u.i[1] = t; |
|
2017 return u.r; |
|
2018 } |
|
2019 # define swapMixedEndianFloat(X) X = floatSwap(X) |
|
2020 #else |
|
2021 # define swapMixedEndianFloat(X) |
|
2022 #endif |
|
2023 |
|
2024 /* |
|
2025 ** Write the serialized data blob for the value stored in pMem into |
|
2026 ** buf. It is assumed that the caller has allocated sufficient space. |
|
2027 ** Return the number of bytes written. |
|
2028 ** |
|
2029 ** nBuf is the amount of space left in buf[]. nBuf must always be |
|
2030 ** large enough to hold the entire field. Except, if the field is |
|
2031 ** a blob with a zero-filled tail, then buf[] might be just the right |
|
2032 ** size to hold everything except for the zero-filled tail. If buf[] |
|
2033 ** is only big enough to hold the non-zero prefix, then only write that |
|
2034 ** prefix into buf[]. But if buf[] is large enough to hold both the |
|
2035 ** prefix and the tail then write the prefix and set the tail to all |
|
2036 ** zeros. |
|
2037 ** |
|
2038 ** Return the number of bytes actually written into buf[]. The number |
|
2039 ** of bytes in the zero-filled tail is included in the return value only |
|
2040 ** if those bytes were zeroed in buf[]. |
|
2041 */ |
|
2042 int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){ |
|
2043 u32 serial_type = sqlite3VdbeSerialType(pMem, file_format); |
|
2044 int len; |
|
2045 |
|
2046 /* Integer and Real */ |
|
2047 if( serial_type<=7 && serial_type>0 ){ |
|
2048 u64 v; |
|
2049 int i; |
|
2050 if( serial_type==7 ){ |
|
2051 assert( sizeof(v)==sizeof(pMem->r) ); |
|
2052 memcpy(&v, &pMem->r, sizeof(v)); |
|
2053 swapMixedEndianFloat(v); |
|
2054 }else{ |
|
2055 v = pMem->u.i; |
|
2056 } |
|
2057 len = i = sqlite3VdbeSerialTypeLen(serial_type); |
|
2058 assert( len<=nBuf ); |
|
2059 while( i-- ){ |
|
2060 buf[i] = (v&0xFF); |
|
2061 v >>= 8; |
|
2062 } |
|
2063 return len; |
|
2064 } |
|
2065 |
|
2066 /* String or blob */ |
|
2067 if( serial_type>=12 ){ |
|
2068 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0) |
|
2069 == sqlite3VdbeSerialTypeLen(serial_type) ); |
|
2070 assert( pMem->n<=nBuf ); |
|
2071 len = pMem->n; |
|
2072 memcpy(buf, pMem->z, len); |
|
2073 if( pMem->flags & MEM_Zero ){ |
|
2074 len += pMem->u.i; |
|
2075 if( len>nBuf ){ |
|
2076 len = nBuf; |
|
2077 } |
|
2078 memset(&buf[pMem->n], 0, len-pMem->n); |
|
2079 } |
|
2080 return len; |
|
2081 } |
|
2082 |
|
2083 /* NULL or constants 0 or 1 */ |
|
2084 return 0; |
|
2085 } |
|
2086 |
|
2087 /* |
|
2088 ** Deserialize the data blob pointed to by buf as serial type serial_type |
|
2089 ** and store the result in pMem. Return the number of bytes read. |
|
2090 */ |
|
2091 int sqlite3VdbeSerialGet( |
|
2092 const unsigned char *buf, /* Buffer to deserialize from */ |
|
2093 u32 serial_type, /* Serial type to deserialize */ |
|
2094 Mem *pMem /* Memory cell to write value into */ |
|
2095 ){ |
|
2096 switch( serial_type ){ |
|
2097 case 10: /* Reserved for future use */ |
|
2098 case 11: /* Reserved for future use */ |
|
2099 case 0: { /* NULL */ |
|
2100 pMem->flags = MEM_Null; |
|
2101 break; |
|
2102 } |
|
2103 case 1: { /* 1-byte signed integer */ |
|
2104 pMem->u.i = (signed char)buf[0]; |
|
2105 pMem->flags = MEM_Int; |
|
2106 return 1; |
|
2107 } |
|
2108 case 2: { /* 2-byte signed integer */ |
|
2109 pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; |
|
2110 pMem->flags = MEM_Int; |
|
2111 return 2; |
|
2112 } |
|
2113 case 3: { /* 3-byte signed integer */ |
|
2114 pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; |
|
2115 pMem->flags = MEM_Int; |
|
2116 return 3; |
|
2117 } |
|
2118 case 4: { /* 4-byte signed integer */ |
|
2119 pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
|
2120 pMem->flags = MEM_Int; |
|
2121 return 4; |
|
2122 } |
|
2123 case 5: { /* 6-byte signed integer */ |
|
2124 u64 x = (((signed char)buf[0])<<8) | buf[1]; |
|
2125 u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; |
|
2126 x = (x<<32) | y; |
|
2127 pMem->u.i = *(i64*)&x; |
|
2128 pMem->flags = MEM_Int; |
|
2129 return 6; |
|
2130 } |
|
2131 case 6: /* 8-byte signed integer */ |
|
2132 case 7: { /* IEEE floating point */ |
|
2133 u64 x; |
|
2134 u32 y; |
|
2135 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
|
2136 /* Verify that integers and floating point values use the same |
|
2137 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
|
2138 ** defined that 64-bit floating point values really are mixed |
|
2139 ** endian. |
|
2140 */ |
|
2141 static const u64 t1 = ((u64)0x3ff00000)<<32; |
|
2142 static const double r1 = 1.0; |
|
2143 u64 t2 = t1; |
|
2144 swapMixedEndianFloat(t2); |
|
2145 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
|
2146 #endif |
|
2147 |
|
2148 x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; |
|
2149 y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; |
|
2150 x = (x<<32) | y; |
|
2151 if( serial_type==6 ){ |
|
2152 pMem->u.i = *(i64*)&x; |
|
2153 pMem->flags = MEM_Int; |
|
2154 }else{ |
|
2155 assert( sizeof(x)==8 && sizeof(pMem->r)==8 ); |
|
2156 swapMixedEndianFloat(x); |
|
2157 memcpy(&pMem->r, &x, sizeof(x)); |
|
2158 pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real; |
|
2159 } |
|
2160 return 8; |
|
2161 } |
|
2162 case 8: /* Integer 0 */ |
|
2163 case 9: { /* Integer 1 */ |
|
2164 pMem->u.i = serial_type-8; |
|
2165 pMem->flags = MEM_Int; |
|
2166 return 0; |
|
2167 } |
|
2168 default: { |
|
2169 int len = (serial_type-12)/2; |
|
2170 pMem->z = (char *)buf; |
|
2171 pMem->n = len; |
|
2172 pMem->xDel = 0; |
|
2173 if( serial_type&0x01 ){ |
|
2174 pMem->flags = MEM_Str | MEM_Ephem; |
|
2175 }else{ |
|
2176 pMem->flags = MEM_Blob | MEM_Ephem; |
|
2177 } |
|
2178 return len; |
|
2179 } |
|
2180 } |
|
2181 return 0; |
|
2182 } |
|
2183 |
|
2184 |
|
2185 /* |
|
2186 ** Given the nKey-byte encoding of a record in pKey[], parse the |
|
2187 ** record into a UnpackedRecord structure. Return a pointer to |
|
2188 ** that structure. |
|
2189 ** |
|
2190 ** The calling function might provide szSpace bytes of memory |
|
2191 ** space at pSpace. This space can be used to hold the returned |
|
2192 ** VDbeParsedRecord structure if it is large enough. If it is |
|
2193 ** not big enough, space is obtained from sqlite3_malloc(). |
|
2194 ** |
|
2195 ** The returned structure should be closed by a call to |
|
2196 ** sqlite3VdbeDeleteUnpackedRecord(). |
|
2197 */ |
|
2198 UnpackedRecord *sqlite3VdbeRecordUnpack( |
|
2199 KeyInfo *pKeyInfo, /* Information about the record format */ |
|
2200 int nKey, /* Size of the binary record */ |
|
2201 const void *pKey, /* The binary record */ |
|
2202 void *pSpace, /* Space available to hold resulting object */ |
|
2203 int szSpace /* Size of pSpace[] in bytes */ |
|
2204 ){ |
|
2205 const unsigned char *aKey = (const unsigned char *)pKey; |
|
2206 UnpackedRecord *p; |
|
2207 int nByte; |
|
2208 int idx, d; |
|
2209 u16 u; /* Unsigned loop counter */ |
|
2210 u32 szHdr; |
|
2211 Mem *pMem; |
|
2212 |
|
2213 assert( sizeof(Mem)>sizeof(*p) ); |
|
2214 nByte = sizeof(Mem)*(pKeyInfo->nField+2); |
|
2215 if( nByte>szSpace ){ |
|
2216 p = sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
|
2217 if( p==0 ) return 0; |
|
2218 p->needFree = 1; |
|
2219 }else{ |
|
2220 p = pSpace; |
|
2221 p->needFree = 0; |
|
2222 } |
|
2223 p->pKeyInfo = pKeyInfo; |
|
2224 p->nField = pKeyInfo->nField + 1; |
|
2225 p->needDestroy = 1; |
|
2226 p->aMem = pMem = &((Mem*)p)[1]; |
|
2227 idx = getVarint32(aKey, szHdr); |
|
2228 d = szHdr; |
|
2229 u = 0; |
|
2230 while( idx<szHdr && u<p->nField ){ |
|
2231 u32 serial_type; |
|
2232 |
|
2233 idx += getVarint32( aKey+idx, serial_type); |
|
2234 if( d>=nKey && sqlite3VdbeSerialTypeLen(serial_type)>0 ) break; |
|
2235 pMem->enc = pKeyInfo->enc; |
|
2236 pMem->db = pKeyInfo->db; |
|
2237 pMem->flags = 0; |
|
2238 pMem->zMalloc = 0; |
|
2239 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
|
2240 pMem++; |
|
2241 u++; |
|
2242 } |
|
2243 p->nField = u; |
|
2244 return (void*)p; |
|
2245 } |
|
2246 |
|
2247 /* |
|
2248 ** This routine destroys a UnpackedRecord object |
|
2249 */ |
|
2250 void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){ |
|
2251 if( p ){ |
|
2252 if( p->needDestroy ){ |
|
2253 int i; |
|
2254 Mem *pMem; |
|
2255 for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){ |
|
2256 if( pMem->zMalloc ){ |
|
2257 sqlite3VdbeMemRelease(pMem); |
|
2258 } |
|
2259 } |
|
2260 } |
|
2261 if( p->needFree ){ |
|
2262 sqlite3DbFree(p->pKeyInfo->db, p); |
|
2263 } |
|
2264 } |
|
2265 } |
|
2266 |
|
2267 /* |
|
2268 ** This function compares the two table rows or index records |
|
2269 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero |
|
2270 ** or positive integer if {nKey1, pKey1} is less than, equal to or |
|
2271 ** greater than pPKey2. The {nKey1, pKey1} key must be a blob |
|
2272 ** created by th OP_MakeRecord opcode of the VDBE. The pPKey2 |
|
2273 ** key must be a parsed key such as obtained from |
|
2274 ** sqlite3VdbeParseRecord. |
|
2275 ** |
|
2276 ** Key1 and Key2 do not have to contain the same number of fields. |
|
2277 ** But if the lengths differ, Key2 must be the shorter of the two. |
|
2278 ** |
|
2279 ** Historical note: In earlier versions of this routine both Key1 |
|
2280 ** and Key2 were blobs obtained from OP_MakeRecord. But we found |
|
2281 ** that in typical use the same Key2 would be submitted multiple times |
|
2282 ** in a row. So an optimization was added to parse the Key2 key |
|
2283 ** separately and submit the parsed version. In this way, we avoid |
|
2284 ** parsing the same Key2 multiple times in a row. |
|
2285 */ |
|
2286 int sqlite3VdbeRecordCompare( |
|
2287 int nKey1, const void *pKey1, |
|
2288 UnpackedRecord *pPKey2 |
|
2289 ){ |
|
2290 u32 d1; /* Offset into aKey[] of next data element */ |
|
2291 u32 idx1; /* Offset into aKey[] of next header element */ |
|
2292 u32 szHdr1; /* Number of bytes in header */ |
|
2293 int i = 0; |
|
2294 int nField; |
|
2295 int rc = 0; |
|
2296 const unsigned char *aKey1 = (const unsigned char *)pKey1; |
|
2297 KeyInfo *pKeyInfo; |
|
2298 Mem mem1; |
|
2299 |
|
2300 pKeyInfo = pPKey2->pKeyInfo; |
|
2301 mem1.enc = pKeyInfo->enc; |
|
2302 mem1.db = pKeyInfo->db; |
|
2303 mem1.flags = 0; |
|
2304 mem1.zMalloc = 0; |
|
2305 |
|
2306 idx1 = getVarint32(aKey1, szHdr1); |
|
2307 d1 = szHdr1; |
|
2308 nField = pKeyInfo->nField; |
|
2309 while( idx1<szHdr1 && i<pPKey2->nField ){ |
|
2310 u32 serial_type1; |
|
2311 |
|
2312 /* Read the serial types for the next element in each key. */ |
|
2313 idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
|
2314 if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break; |
|
2315 |
|
2316 /* Extract the values to be compared. |
|
2317 */ |
|
2318 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); |
|
2319 |
|
2320 /* Do the comparison |
|
2321 */ |
|
2322 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], |
|
2323 i<nField ? pKeyInfo->aColl[i] : 0); |
|
2324 if( rc!=0 ){ |
|
2325 break; |
|
2326 } |
|
2327 i++; |
|
2328 } |
|
2329 if( mem1.zMalloc ) sqlite3VdbeMemRelease(&mem1); |
|
2330 |
|
2331 /* One of the keys ran out of fields, but all the fields up to that point |
|
2332 ** were equal. If the incrKey flag is true, then the second key is |
|
2333 ** treated as larger. |
|
2334 */ |
|
2335 if( rc==0 ){ |
|
2336 if( pKeyInfo->incrKey ){ |
|
2337 rc = -1; |
|
2338 }else if( !pKeyInfo->prefixIsEqual ){ |
|
2339 if( d1<nKey1 ){ |
|
2340 rc = 1; |
|
2341 } |
|
2342 } |
|
2343 }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField |
|
2344 && pKeyInfo->aSortOrder[i] ){ |
|
2345 rc = -rc; |
|
2346 } |
|
2347 |
|
2348 return rc; |
|
2349 } |
|
2350 |
|
2351 /* |
|
2352 ** The argument is an index entry composed using the OP_MakeRecord opcode. |
|
2353 ** The last entry in this record should be an integer (specifically |
|
2354 ** an integer rowid). This routine returns the number of bytes in |
|
2355 ** that integer. |
|
2356 */ |
|
2357 int sqlite3VdbeIdxRowidLen(const u8 *aKey, int nKey, int *pRowidLen){ |
|
2358 u32 szHdr; /* Size of the header */ |
|
2359 u32 typeRowid; /* Serial type of the rowid */ |
|
2360 |
|
2361 (void)getVarint32(aKey, szHdr); |
|
2362 if( szHdr>nKey ){ |
|
2363 return SQLITE_CORRUPT_BKPT; |
|
2364 } |
|
2365 (void)getVarint32(&aKey[szHdr-1], typeRowid); |
|
2366 *pRowidLen = sqlite3VdbeSerialTypeLen(typeRowid); |
|
2367 return SQLITE_OK; |
|
2368 } |
|
2369 |
|
2370 |
|
2371 /* |
|
2372 ** pCur points at an index entry created using the OP_MakeRecord opcode. |
|
2373 ** Read the rowid (the last field in the record) and store it in *rowid. |
|
2374 ** Return SQLITE_OK if everything works, or an error code otherwise. |
|
2375 */ |
|
2376 int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){ |
|
2377 i64 nCellKey = 0; |
|
2378 int rc; |
|
2379 u32 szHdr; /* Size of the header */ |
|
2380 u32 typeRowid; /* Serial type of the rowid */ |
|
2381 u32 lenRowid; /* Size of the rowid */ |
|
2382 Mem m, v; |
|
2383 |
|
2384 sqlite3BtreeKeySize(pCur, &nCellKey); |
|
2385 if( nCellKey<=0 ){ |
|
2386 return SQLITE_CORRUPT_BKPT; |
|
2387 } |
|
2388 m.flags = 0; |
|
2389 m.db = 0; |
|
2390 m.zMalloc = 0; |
|
2391 rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m); |
|
2392 if( rc ){ |
|
2393 return rc; |
|
2394 } |
|
2395 (void)getVarint32((u8*)m.z, szHdr); |
|
2396 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); |
|
2397 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); |
|
2398 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
|
2399 *rowid = v.u.i; |
|
2400 sqlite3VdbeMemRelease(&m); |
|
2401 return SQLITE_OK; |
|
2402 } |
|
2403 |
|
2404 /* |
|
2405 ** Compare the key of the index entry that cursor pC is point to against |
|
2406 ** the key string in pKey (of length nKey). Write into *pRes a number |
|
2407 ** that is negative, zero, or positive if pC is less than, equal to, |
|
2408 ** or greater than pKey. Return SQLITE_OK on success. |
|
2409 ** |
|
2410 ** pKey is either created without a rowid or is truncated so that it |
|
2411 ** omits the rowid at the end. The rowid at the end of the index entry |
|
2412 ** is ignored as well. |
|
2413 */ |
|
2414 int sqlite3VdbeIdxKeyCompare( |
|
2415 Cursor *pC, /* The cursor to compare against */ |
|
2416 UnpackedRecord *pUnpacked, |
|
2417 int nKey, const u8 *pKey, /* The key to compare */ |
|
2418 int *res /* Write the comparison result here */ |
|
2419 ){ |
|
2420 i64 nCellKey = 0; |
|
2421 int rc; |
|
2422 BtCursor *pCur = pC->pCursor; |
|
2423 int lenRowid; |
|
2424 Mem m; |
|
2425 UnpackedRecord *pRec; |
|
2426 char zSpace[200]; |
|
2427 |
|
2428 sqlite3BtreeKeySize(pCur, &nCellKey); |
|
2429 if( nCellKey<=0 ){ |
|
2430 *res = 0; |
|
2431 return SQLITE_OK; |
|
2432 } |
|
2433 m.db = 0; |
|
2434 m.flags = 0; |
|
2435 m.zMalloc = 0; |
|
2436 if( (rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m)) |
|
2437 || (rc = sqlite3VdbeIdxRowidLen((u8*)m.z, m.n, &lenRowid)) |
|
2438 ){ |
|
2439 return rc; |
|
2440 } |
|
2441 if( !pUnpacked ){ |
|
2442 pRec = sqlite3VdbeRecordUnpack(pC->pKeyInfo, nKey, pKey, |
|
2443 zSpace, sizeof(zSpace)); |
|
2444 }else{ |
|
2445 pRec = pUnpacked; |
|
2446 } |
|
2447 if( pRec==0 ){ |
|
2448 return SQLITE_NOMEM; |
|
2449 } |
|
2450 *res = sqlite3VdbeRecordCompare(m.n-lenRowid, m.z, pRec); |
|
2451 if( !pUnpacked ){ |
|
2452 sqlite3VdbeDeleteUnpackedRecord(pRec); |
|
2453 } |
|
2454 sqlite3VdbeMemRelease(&m); |
|
2455 return SQLITE_OK; |
|
2456 } |
|
2457 |
|
2458 /* |
|
2459 ** This routine sets the value to be returned by subsequent calls to |
|
2460 ** sqlite3_changes() on the database handle 'db'. |
|
2461 */ |
|
2462 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ |
|
2463 assert( sqlite3_mutex_held(db->mutex) ); |
|
2464 db->nChange = nChange; |
|
2465 db->nTotalChange += nChange; |
|
2466 } |
|
2467 |
|
2468 /* |
|
2469 ** Set a flag in the vdbe to update the change counter when it is finalised |
|
2470 ** or reset. |
|
2471 */ |
|
2472 void sqlite3VdbeCountChanges(Vdbe *v){ |
|
2473 v->changeCntOn = 1; |
|
2474 } |
|
2475 |
|
2476 /* |
|
2477 ** Mark every prepared statement associated with a database connection |
|
2478 ** as expired. |
|
2479 ** |
|
2480 ** An expired statement means that recompilation of the statement is |
|
2481 ** recommend. Statements expire when things happen that make their |
|
2482 ** programs obsolete. Removing user-defined functions or collating |
|
2483 ** sequences, or changing an authorization function are the types of |
|
2484 ** things that make prepared statements obsolete. |
|
2485 */ |
|
2486 void sqlite3ExpirePreparedStatements(sqlite3 *db){ |
|
2487 Vdbe *p; |
|
2488 for(p = db->pVdbe; p; p=p->pNext){ |
|
2489 p->expired = 1; |
|
2490 } |
|
2491 } |
|
2492 |
|
2493 /* |
|
2494 ** Return the database associated with the Vdbe. |
|
2495 */ |
|
2496 sqlite3 *sqlite3VdbeDb(Vdbe *v){ |
|
2497 return v->db; |
|
2498 } |