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