4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
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.)
15 #include "sqliteInt.h"
19 ** Create a new virtual database engine.
21 Vdbe
*sqlite3VdbeCreate(Parse
*pParse
){
22 sqlite3
*db
= pParse
->db
;
24 p
= sqlite3DbMallocZero(db
, sizeof(Vdbe
) );
33 p
->magic
= VDBE_MAGIC_INIT
;
35 assert( pParse
->aLabel
==0 );
36 assert( pParse
->nLabel
==0 );
37 assert( pParse
->nOpAlloc
==0 );
42 ** Remember the SQL string for a prepared statement.
44 void sqlite3VdbeSetSql(Vdbe
*p
, const char *z
, int n
, int isPrepareV2
){
45 assert( isPrepareV2
==1 || isPrepareV2
==0 );
47 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
48 if( !isPrepareV2
) return;
51 p
->zSql
= sqlite3DbStrNDup(p
->db
, z
, n
);
52 p
->isPrepareV2
= (u8
)isPrepareV2
;
56 ** Return the SQL associated with a prepared statement
58 const char *sqlite3_sql(sqlite3_stmt
*pStmt
){
59 Vdbe
*p
= (Vdbe
*)pStmt
;
60 return (p
&& p
->isPrepareV2
) ? p
->zSql
: 0;
64 ** Swap all content between two VDBE structures.
66 void sqlite3VdbeSwap(Vdbe
*pA
, Vdbe
*pB
){
73 pA
->pNext
= pB
->pNext
;
76 pA
->pPrev
= pB
->pPrev
;
81 pB
->isPrepareV2
= pA
->isPrepareV2
;
85 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger
86 ** than its current size. nOp is guaranteed to be less than or equal
87 ** to 1024/sizeof(Op).
89 ** If an out-of-memory error occurs while resizing the array, return
90 ** SQLITE_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
91 ** unchanged (this is so that any opcodes already allocated can be
92 ** correctly deallocated along with the rest of the Vdbe).
94 static int growOpArray(Vdbe
*v
, int nOp
){
98 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force
99 ** more frequent reallocs and hence provide more opportunities for
100 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used
101 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array
102 ** by the minimum* amount required until the size reaches 512. Normal
103 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current
104 ** size of the op array or add 1KB of space, whichever is smaller. */
105 #ifdef SQLITE_TEST_REALLOC_STRESS
106 int nNew
= (p
->nOpAlloc
>=512 ? p
->nOpAlloc
*2 : p
->nOpAlloc
+nOp
);
108 int nNew
= (p
->nOpAlloc
? p
->nOpAlloc
*2 : (int)(1024/sizeof(Op
)));
109 UNUSED_PARAMETER(nOp
);
112 assert( nOp
<=(1024/sizeof(Op
)) );
113 assert( nNew
>=(p
->nOpAlloc
+nOp
) );
114 pNew
= sqlite3DbRealloc(p
->db
, v
->aOp
, nNew
*sizeof(Op
));
116 p
->nOpAlloc
= sqlite3DbMallocSize(p
->db
, pNew
)/sizeof(Op
);
119 return (pNew
? SQLITE_OK
: SQLITE_NOMEM
);
123 /* This routine is just a convenient place to set a breakpoint that will
124 ** fire after each opcode is inserted and displayed using
125 ** "PRAGMA vdbe_addoptrace=on".
127 static void test_addop_breakpoint(void){
134 ** Add a new instruction to the list of instructions current in the
135 ** VDBE. Return the address of the new instruction.
139 ** p Pointer to the VDBE
141 ** op The opcode for this instruction
143 ** p1, p2, p3 Operands
145 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
146 ** the sqlite3VdbeChangeP4() function to change the value of the P4
149 int sqlite3VdbeAddOp3(Vdbe
*p
, int op
, int p1
, int p2
, int p3
){
154 assert( p
->magic
==VDBE_MAGIC_INIT
);
155 assert( op
>0 && op
<0xff );
156 if( p
->pParse
->nOpAlloc
<=i
){
157 if( growOpArray(p
, 1) ){
163 pOp
->opcode
= (u8
)op
;
169 pOp
->p4type
= P4_NOTUSED
;
170 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
174 if( p
->db
->flags
& SQLITE_VdbeAddopTrace
){
176 Parse
*pParse
= p
->pParse
;
177 for(jj
=kk
=0; jj
<SQLITE_N_COLCACHE
; jj
++){
178 struct yColCache
*x
= pParse
->aColCache
+ jj
;
179 if( x
->iLevel
>pParse
->iCacheLevel
|| x
->iReg
==0 ) continue;
180 printf(" r[%d]={%d:%d}", x
->iReg
, x
->iTable
, x
->iColumn
);
183 if( kk
) printf("\n");
184 sqlite3VdbePrintOp(0, i
, &p
->aOp
[i
]);
185 test_addop_breakpoint();
192 #ifdef SQLITE_VDBE_COVERAGE
197 int sqlite3VdbeAddOp0(Vdbe
*p
, int op
){
198 return sqlite3VdbeAddOp3(p
, op
, 0, 0, 0);
200 int sqlite3VdbeAddOp1(Vdbe
*p
, int op
, int p1
){
201 return sqlite3VdbeAddOp3(p
, op
, p1
, 0, 0);
203 int sqlite3VdbeAddOp2(Vdbe
*p
, int op
, int p1
, int p2
){
204 return sqlite3VdbeAddOp3(p
, op
, p1
, p2
, 0);
209 ** Add an opcode that includes the p4 value as a pointer.
211 int sqlite3VdbeAddOp4(
212 Vdbe
*p
, /* Add the opcode to this VM */
213 int op
, /* The new opcode */
214 int p1
, /* The P1 operand */
215 int p2
, /* The P2 operand */
216 int p3
, /* The P3 operand */
217 const char *zP4
, /* The P4 operand */
218 int p4type
/* P4 operand type */
220 int addr
= sqlite3VdbeAddOp3(p
, op
, p1
, p2
, p3
);
221 sqlite3VdbeChangeP4(p
, addr
, zP4
, p4type
);
226 ** Add an OP_ParseSchema opcode. This routine is broken out from
227 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
228 ** as having been used.
230 ** The zWhere string must have been obtained from sqlite3_malloc().
231 ** This routine will take ownership of the allocated memory.
233 void sqlite3VdbeAddParseSchemaOp(Vdbe
*p
, int iDb
, char *zWhere
){
235 int addr
= sqlite3VdbeAddOp3(p
, OP_ParseSchema
, iDb
, 0, 0);
236 sqlite3VdbeChangeP4(p
, addr
, zWhere
, P4_DYNAMIC
);
237 for(j
=0; j
<p
->db
->nDb
; j
++) sqlite3VdbeUsesBtree(p
, j
);
241 ** Add an opcode that includes the p4 value as an integer.
243 int sqlite3VdbeAddOp4Int(
244 Vdbe
*p
, /* Add the opcode to this VM */
245 int op
, /* The new opcode */
246 int p1
, /* The P1 operand */
247 int p2
, /* The P2 operand */
248 int p3
, /* The P3 operand */
249 int p4
/* The P4 operand as an integer */
251 int addr
= sqlite3VdbeAddOp3(p
, op
, p1
, p2
, p3
);
252 sqlite3VdbeChangeP4(p
, addr
, SQLITE_INT_TO_PTR(p4
), P4_INT32
);
257 ** Create a new symbolic label for an instruction that has yet to be
258 ** coded. The symbolic label is really just a negative number. The
259 ** label can be used as the P2 value of an operation. Later, when
260 ** the label is resolved to a specific address, the VDBE will scan
261 ** through its operation list and change all values of P2 which match
262 ** the label into the resolved address.
264 ** The VDBE knows that a P2 value is a label because labels are
265 ** always negative and P2 values are suppose to be non-negative.
266 ** Hence, a negative P2 value is a label that has yet to be resolved.
268 ** Zero is returned if a malloc() fails.
270 int sqlite3VdbeMakeLabel(Vdbe
*v
){
271 Parse
*p
= v
->pParse
;
273 assert( v
->magic
==VDBE_MAGIC_INIT
);
274 if( (i
& (i
-1))==0 ){
275 p
->aLabel
= sqlite3DbReallocOrFree(p
->db
, p
->aLabel
,
276 (i
*2+1)*sizeof(p
->aLabel
[0]));
285 ** Resolve label "x" to be the address of the next instruction to
286 ** be inserted. The parameter "x" must have been obtained from
287 ** a prior call to sqlite3VdbeMakeLabel().
289 void sqlite3VdbeResolveLabel(Vdbe
*v
, int x
){
290 Parse
*p
= v
->pParse
;
292 assert( v
->magic
==VDBE_MAGIC_INIT
);
293 assert( j
<p
->nLabel
);
294 if( ALWAYS(j
>=0) && p
->aLabel
){
295 p
->aLabel
[j
] = v
->nOp
;
297 p
->iFixedOp
= v
->nOp
- 1;
301 ** Mark the VDBE as one that can only be run one time.
303 void sqlite3VdbeRunOnlyOnce(Vdbe
*p
){
307 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
310 ** The following type and function are used to iterate through all opcodes
311 ** in a Vdbe main program and each of the sub-programs (triggers) it may
312 ** invoke directly or indirectly. It should be used as follows:
317 ** memset(&sIter, 0, sizeof(sIter));
318 ** sIter.v = v; // v is of type Vdbe*
319 ** while( (pOp = opIterNext(&sIter)) ){
320 ** // Do something with pOp
322 ** sqlite3DbFree(v->db, sIter.apSub);
325 typedef struct VdbeOpIter VdbeOpIter
;
327 Vdbe
*v
; /* Vdbe to iterate through the opcodes of */
328 SubProgram
**apSub
; /* Array of subprograms */
329 int nSub
; /* Number of entries in apSub */
330 int iAddr
; /* Address of next instruction to return */
331 int iSub
; /* 0 = main program, 1 = first sub-program etc. */
333 static Op
*opIterNext(VdbeOpIter
*p
){
339 if( p
->iSub
<=p
->nSub
){
345 aOp
= p
->apSub
[p
->iSub
-1]->aOp
;
346 nOp
= p
->apSub
[p
->iSub
-1]->nOp
;
348 assert( p
->iAddr
<nOp
);
350 pRet
= &aOp
[p
->iAddr
];
357 if( pRet
->p4type
==P4_SUBPROGRAM
){
358 int nByte
= (p
->nSub
+1)*sizeof(SubProgram
*);
360 for(j
=0; j
<p
->nSub
; j
++){
361 if( p
->apSub
[j
]==pRet
->p4
.pProgram
) break;
364 p
->apSub
= sqlite3DbReallocOrFree(v
->db
, p
->apSub
, nByte
);
368 p
->apSub
[p
->nSub
++] = pRet
->p4
.pProgram
;
378 ** Check if the program stored in the VM associated with pParse may
379 ** throw an ABORT exception (causing the statement, but not entire transaction
380 ** to be rolled back). This condition is true if the main program or any
381 ** sub-programs contains any of the following:
383 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
384 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
388 ** * OP_FkCounter with P2==0 (immediate foreign key constraint)
390 ** Then check that the value of Parse.mayAbort is true if an
391 ** ABORT may be thrown, or false otherwise. Return true if it does
392 ** match, or false otherwise. This function is intended to be used as
393 ** part of an assert statement in the compiler. Similar to:
395 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
397 int sqlite3VdbeAssertMayAbort(Vdbe
*v
, int mayAbort
){
401 memset(&sIter
, 0, sizeof(sIter
));
404 while( (pOp
= opIterNext(&sIter
))!=0 ){
405 int opcode
= pOp
->opcode
;
406 if( opcode
==OP_Destroy
|| opcode
==OP_VUpdate
|| opcode
==OP_VRename
407 #ifndef SQLITE_OMIT_FOREIGN_KEY
408 || (opcode
==OP_FkCounter
&& pOp
->p1
==0 && pOp
->p2
==1)
410 || ((opcode
==OP_Halt
|| opcode
==OP_HaltIfNull
)
411 && ((pOp
->p1
&0xff)==SQLITE_CONSTRAINT
&& pOp
->p2
==OE_Abort
))
417 sqlite3DbFree(v
->db
, sIter
.apSub
);
419 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
420 ** If malloc failed, then the while() loop above may not have iterated
421 ** through all opcodes and hasAbort may be set incorrectly. Return
422 ** true for this case to prevent the assert() in the callers frame
424 return ( v
->db
->mallocFailed
|| hasAbort
==mayAbort
);
426 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
429 ** Loop through the program looking for P2 values that are negative
430 ** on jump instructions. Each such value is a label. Resolve the
431 ** label by setting the P2 value to its correct non-zero value.
433 ** This routine is called once after all opcodes have been inserted.
435 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
436 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
437 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
439 ** The Op.opflags field is set on all opcodes.
441 static void resolveP2Values(Vdbe
*p
, int *pMaxFuncArgs
){
443 int nMaxArgs
= *pMaxFuncArgs
;
445 Parse
*pParse
= p
->pParse
;
446 int *aLabel
= pParse
->aLabel
;
449 for(pOp
=p
->aOp
, i
=p
->nOp
-1; i
>=0; i
--, pOp
++){
450 u8 opcode
= pOp
->opcode
;
452 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
453 ** cases from this switch! */
457 if( pOp
->p5
>nMaxArgs
) nMaxArgs
= pOp
->p5
;
460 case OP_Transaction
: {
461 if( pOp
->p2
!=0 ) p
->readOnly
= 0;
469 #ifndef SQLITE_OMIT_WAL
473 case OP_JournalMode
: {
478 #ifndef SQLITE_OMIT_VIRTUALTABLE
480 if( pOp
->p2
>nMaxArgs
) nMaxArgs
= pOp
->p2
;
485 assert( p
->nOp
- i
>= 3 );
486 assert( pOp
[-1].opcode
==OP_Integer
);
488 if( n
>nMaxArgs
) nMaxArgs
= n
;
494 case OP_SorterNext
: {
495 pOp
->p4
.xAdvance
= sqlite3BtreeNext
;
496 pOp
->p4type
= P4_ADVANCE
;
500 case OP_PrevIfOpen
: {
501 pOp
->p4
.xAdvance
= sqlite3BtreePrevious
;
502 pOp
->p4type
= P4_ADVANCE
;
507 pOp
->opflags
= sqlite3OpcodeProperty
[opcode
];
508 if( (pOp
->opflags
& OPFLG_JUMP
)!=0 && pOp
->p2
<0 ){
509 assert( -1-pOp
->p2
<pParse
->nLabel
);
510 pOp
->p2
= aLabel
[-1-pOp
->p2
];
513 sqlite3DbFree(p
->db
, pParse
->aLabel
);
516 *pMaxFuncArgs
= nMaxArgs
;
517 assert( p
->bIsReader
!=0 || DbMaskAllZero(p
->btreeMask
) );
521 ** Return the address of the next instruction to be inserted.
523 int sqlite3VdbeCurrentAddr(Vdbe
*p
){
524 assert( p
->magic
==VDBE_MAGIC_INIT
);
529 ** This function returns a pointer to the array of opcodes associated with
530 ** the Vdbe passed as the first argument. It is the callers responsibility
531 ** to arrange for the returned array to be eventually freed using the
532 ** vdbeFreeOpArray() function.
534 ** Before returning, *pnOp is set to the number of entries in the returned
535 ** array. Also, *pnMaxArg is set to the larger of its current value and
536 ** the number of entries in the Vdbe.apArg[] array required to execute the
539 VdbeOp
*sqlite3VdbeTakeOpArray(Vdbe
*p
, int *pnOp
, int *pnMaxArg
){
540 VdbeOp
*aOp
= p
->aOp
;
541 assert( aOp
&& !p
->db
->mallocFailed
);
543 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
544 assert( DbMaskAllZero(p
->btreeMask
) );
546 resolveP2Values(p
, pnMaxArg
);
553 ** Add a whole list of operations to the operation stack. Return the
554 ** address of the first operation added.
556 int sqlite3VdbeAddOpList(Vdbe
*p
, int nOp
, VdbeOpList
const *aOp
, int iLineno
){
558 assert( p
->magic
==VDBE_MAGIC_INIT
);
559 if( p
->nOp
+ nOp
> p
->pParse
->nOpAlloc
&& growOpArray(p
, nOp
) ){
565 VdbeOpList
const *pIn
= aOp
;
566 for(i
=0; i
<nOp
; i
++, pIn
++){
568 VdbeOp
*pOut
= &p
->aOp
[i
+addr
];
569 pOut
->opcode
= pIn
->opcode
;
572 assert( sqlite3OpcodeProperty
[pOut
->opcode
] & OPFLG_JUMP
);
573 pOut
->p2
= addr
+ ADDR(p2
);
578 pOut
->p4type
= P4_NOTUSED
;
581 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
584 #ifdef SQLITE_VDBE_COVERAGE
585 pOut
->iSrcLine
= iLineno
+i
;
590 if( p
->db
->flags
& SQLITE_VdbeAddopTrace
){
591 sqlite3VdbePrintOp(0, i
+addr
, &p
->aOp
[i
+addr
]);
601 ** Change the value of the P1 operand for a specific instruction.
602 ** This routine is useful when a large program is loaded from a
603 ** static array using sqlite3VdbeAddOpList but we want to make a
604 ** few minor changes to the program.
606 void sqlite3VdbeChangeP1(Vdbe
*p
, u32 addr
, int val
){
608 if( ((u32
)p
->nOp
)>addr
){
609 p
->aOp
[addr
].p1
= val
;
614 ** Change the value of the P2 operand for a specific instruction.
615 ** This routine is useful for setting a jump destination.
617 void sqlite3VdbeChangeP2(Vdbe
*p
, u32 addr
, int val
){
619 if( ((u32
)p
->nOp
)>addr
){
620 p
->aOp
[addr
].p2
= val
;
625 ** Change the value of the P3 operand for a specific instruction.
627 void sqlite3VdbeChangeP3(Vdbe
*p
, u32 addr
, int val
){
629 if( ((u32
)p
->nOp
)>addr
){
630 p
->aOp
[addr
].p3
= val
;
635 ** Change the value of the P5 operand for the most recently
638 void sqlite3VdbeChangeP5(Vdbe
*p
, u8 val
){
642 p
->aOp
[p
->nOp
-1].p5
= val
;
647 ** Change the P2 operand of instruction addr so that it points to
648 ** the address of the next instruction to be coded.
650 void sqlite3VdbeJumpHere(Vdbe
*p
, int addr
){
651 sqlite3VdbeChangeP2(p
, addr
, p
->nOp
);
652 p
->pParse
->iFixedOp
= p
->nOp
- 1;
657 ** If the input FuncDef structure is ephemeral, then free it. If
658 ** the FuncDef is not ephermal, then do nothing.
660 static void freeEphemeralFunction(sqlite3
*db
, FuncDef
*pDef
){
661 if( ALWAYS(pDef
) && (pDef
->funcFlags
& SQLITE_FUNC_EPHEM
)!=0 ){
662 sqlite3DbFree(db
, pDef
);
666 static void vdbeFreeOpArray(sqlite3
*, Op
*, int);
669 ** Delete a P4 value if necessary.
671 static void freeP4(sqlite3
*db
, int p4type
, void *p4
){
679 sqlite3DbFree(db
, p4
);
683 if( db
->pnBytesFreed
==0 ) sqlite3KeyInfoUnref((KeyInfo
*)p4
);
687 if( db
->pnBytesFreed
==0 ) sqlite3_free(p4
);
691 freeEphemeralFunction(db
, (FuncDef
*)p4
);
695 if( db
->pnBytesFreed
==0 ){
696 sqlite3ValueFree((sqlite3_value
*)p4
);
699 if( p
->szMalloc
) sqlite3DbFree(db
, p
->zMalloc
);
700 sqlite3DbFree(db
, p
);
705 if( db
->pnBytesFreed
==0 ) sqlite3VtabUnlock((VTable
*)p4
);
713 ** Free the space allocated for aOp and any p4 values allocated for the
714 ** opcodes contained within. If aOp is not NULL it is assumed to contain
717 static void vdbeFreeOpArray(sqlite3
*db
, Op
*aOp
, int nOp
){
720 for(pOp
=aOp
; pOp
<&aOp
[nOp
]; pOp
++){
721 freeP4(db
, pOp
->p4type
, pOp
->p4
.p
);
722 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
723 sqlite3DbFree(db
, pOp
->zComment
);
727 sqlite3DbFree(db
, aOp
);
731 ** Link the SubProgram object passed as the second argument into the linked
732 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
733 ** objects when the VM is no longer required.
735 void sqlite3VdbeLinkSubProgram(Vdbe
*pVdbe
, SubProgram
*p
){
736 p
->pNext
= pVdbe
->pProgram
;
741 ** Change the opcode at addr into OP_Noop
743 void sqlite3VdbeChangeToNoop(Vdbe
*p
, int addr
){
745 VdbeOp
*pOp
= &p
->aOp
[addr
];
747 freeP4(db
, pOp
->p4type
, pOp
->p4
.p
);
748 memset(pOp
, 0, sizeof(pOp
[0]));
749 pOp
->opcode
= OP_Noop
;
750 if( addr
==p
->nOp
-1 ) p
->nOp
--;
755 ** If the last opcode is "op" and it is not a jump destination,
756 ** then remove it. Return true if and only if an opcode was removed.
758 int sqlite3VdbeDeletePriorOpcode(Vdbe
*p
, u8 op
){
759 if( (p
->nOp
-1)>(p
->pParse
->iFixedOp
) && p
->aOp
[p
->nOp
-1].opcode
==op
){
760 sqlite3VdbeChangeToNoop(p
, p
->nOp
-1);
768 ** Change the value of the P4 operand for a specific instruction.
769 ** This routine is useful when a large program is loaded from a
770 ** static array using sqlite3VdbeAddOpList but we want to make a
771 ** few minor changes to the program.
773 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
774 ** the string is made into memory obtained from sqlite3_malloc().
775 ** A value of n==0 means copy bytes of zP4 up to and including the
776 ** first null byte. If n>0 then copy n+1 bytes of zP4.
778 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
779 ** to a string or structure that is guaranteed to exist for the lifetime of
780 ** the Vdbe. In these cases we can just copy the pointer.
782 ** If addr<0 then change P4 on the most recently inserted instruction.
784 void sqlite3VdbeChangeP4(Vdbe
*p
, int addr
, const char *zP4
, int n
){
789 assert( p
->magic
==VDBE_MAGIC_INIT
);
790 if( p
->aOp
==0 || db
->mallocFailed
){
792 freeP4(db
, n
, (void*)*(char**)&zP4
);
797 assert( addr
<p
->nOp
);
802 assert( pOp
->p4type
==P4_NOTUSED
803 || pOp
->p4type
==P4_INT32
804 || pOp
->p4type
==P4_KEYINFO
);
805 freeP4(db
, pOp
->p4type
, pOp
->p4
.p
);
808 /* Note: this cast is safe, because the origin data point was an int
809 ** that was cast to a (const char *). */
810 pOp
->p4
.i
= SQLITE_PTR_TO_INT(zP4
);
811 pOp
->p4type
= P4_INT32
;
814 pOp
->p4type
= P4_NOTUSED
;
815 }else if( n
==P4_KEYINFO
){
816 pOp
->p4
.p
= (void*)zP4
;
817 pOp
->p4type
= P4_KEYINFO
;
818 }else if( n
==P4_VTAB
){
819 pOp
->p4
.p
= (void*)zP4
;
820 pOp
->p4type
= P4_VTAB
;
821 sqlite3VtabLock((VTable
*)zP4
);
822 assert( ((VTable
*)zP4
)->db
==p
->db
);
824 pOp
->p4
.p
= (void*)zP4
;
825 pOp
->p4type
= (signed char)n
;
827 if( n
==0 ) n
= sqlite3Strlen30(zP4
);
828 pOp
->p4
.z
= sqlite3DbStrNDup(p
->db
, zP4
, n
);
829 pOp
->p4type
= P4_DYNAMIC
;
834 ** Set the P4 on the most recently added opcode to the KeyInfo for the
837 void sqlite3VdbeSetP4KeyInfo(Parse
*pParse
, Index
*pIdx
){
838 Vdbe
*v
= pParse
->pVdbe
;
841 sqlite3VdbeChangeP4(v
, -1, (char*)sqlite3KeyInfoOfIndex(pParse
, pIdx
),
845 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
847 ** Change the comment on the most recently coded instruction. Or
848 ** insert a No-op and add the comment to that new instruction. This
849 ** makes the code easier to read during debugging. None of this happens
850 ** in a production build.
852 static void vdbeVComment(Vdbe
*p
, const char *zFormat
, va_list ap
){
853 assert( p
->nOp
>0 || p
->aOp
==0 );
854 assert( p
->aOp
==0 || p
->aOp
[p
->nOp
-1].zComment
==0 || p
->db
->mallocFailed
);
857 sqlite3DbFree(p
->db
, p
->aOp
[p
->nOp
-1].zComment
);
858 p
->aOp
[p
->nOp
-1].zComment
= sqlite3VMPrintf(p
->db
, zFormat
, ap
);
861 void sqlite3VdbeComment(Vdbe
*p
, const char *zFormat
, ...){
864 va_start(ap
, zFormat
);
865 vdbeVComment(p
, zFormat
, ap
);
869 void sqlite3VdbeNoopComment(Vdbe
*p
, const char *zFormat
, ...){
872 sqlite3VdbeAddOp0(p
, OP_Noop
);
873 va_start(ap
, zFormat
);
874 vdbeVComment(p
, zFormat
, ap
);
880 #ifdef SQLITE_VDBE_COVERAGE
882 ** Set the value if the iSrcLine field for the previously coded instruction.
884 void sqlite3VdbeSetLineNumber(Vdbe
*v
, int iLine
){
885 sqlite3VdbeGetOp(v
,-1)->iSrcLine
= iLine
;
887 #endif /* SQLITE_VDBE_COVERAGE */
890 ** Return the opcode for a given address. If the address is -1, then
891 ** return the most recently inserted opcode.
893 ** If a memory allocation error has occurred prior to the calling of this
894 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode
895 ** is readable but not writable, though it is cast to a writable value.
896 ** The return of a dummy opcode allows the call to continue functioning
897 ** after an OOM fault without having to check to see if the return from
898 ** this routine is a valid pointer. But because the dummy.opcode is 0,
899 ** dummy will never be written to. This is verified by code inspection and
900 ** by running with Valgrind.
902 VdbeOp
*sqlite3VdbeGetOp(Vdbe
*p
, int addr
){
903 /* C89 specifies that the constant "dummy" will be initialized to all
904 ** zeros, which is correct. MSVC generates a warning, nevertheless. */
905 static VdbeOp dummy
; /* Ignore the MSVC warning about no initializer */
906 assert( p
->magic
==VDBE_MAGIC_INIT
);
910 assert( (addr
>=0 && addr
<p
->nOp
) || p
->db
->mallocFailed
);
911 if( p
->db
->mallocFailed
){
912 return (VdbeOp
*)&dummy
;
914 return &p
->aOp
[addr
];
918 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
920 ** Return an integer value for one of the parameters to the opcode pOp
921 ** determined by character c.
923 static int translateP(char c
, const Op
*pOp
){
924 if( c
=='1' ) return pOp
->p1
;
925 if( c
=='2' ) return pOp
->p2
;
926 if( c
=='3' ) return pOp
->p3
;
927 if( c
=='4' ) return pOp
->p4
.i
;
932 ** Compute a string for the "comment" field of a VDBE opcode listing.
934 ** The Synopsis: field in comments in the vdbe.c source file gets converted
935 ** to an extra string that is appended to the sqlite3OpcodeName(). In the
936 ** absence of other comments, this synopsis becomes the comment on the opcode.
937 ** Some translation occurs:
940 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1
941 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0
942 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x
944 static int displayComment(
945 const Op
*pOp
, /* The opcode to be commented */
946 const char *zP4
, /* Previously obtained value for P4 */
947 char *zTemp
, /* Write result here */
948 int nTemp
/* Space available in zTemp[] */
951 const char *zSynopsis
;
954 zOpName
= sqlite3OpcodeName(pOp
->opcode
);
955 nOpName
= sqlite3Strlen30(zOpName
);
956 if( zOpName
[nOpName
+1] ){
959 zSynopsis
= zOpName
+= nOpName
+ 1;
960 for(ii
=jj
=0; jj
<nTemp
-1 && (c
= zSynopsis
[ii
])!=0; ii
++){
964 sqlite3_snprintf(nTemp
-jj
, zTemp
+jj
, "%s", zP4
);
966 sqlite3_snprintf(nTemp
-jj
, zTemp
+jj
, "%s", pOp
->zComment
);
969 int v1
= translateP(c
, pOp
);
971 sqlite3_snprintf(nTemp
-jj
, zTemp
+jj
, "%d", v1
);
972 if( strncmp(zSynopsis
+ii
+1, "@P", 2)==0 ){
974 jj
+= sqlite3Strlen30(zTemp
+jj
);
975 v2
= translateP(zSynopsis
[ii
], pOp
);
976 if( strncmp(zSynopsis
+ii
+1,"+1",2)==0 ){
981 sqlite3_snprintf(nTemp
-jj
, zTemp
+jj
, "..%d", v1
+v2
-1);
983 }else if( strncmp(zSynopsis
+ii
+1, "..P3", 4)==0 && pOp
->p3
==0 ){
987 jj
+= sqlite3Strlen30(zTemp
+jj
);
992 if( !seenCom
&& jj
<nTemp
-5 && pOp
->zComment
){
993 sqlite3_snprintf(nTemp
-jj
, zTemp
+jj
, "; %s", pOp
->zComment
);
994 jj
+= sqlite3Strlen30(zTemp
+jj
);
996 if( jj
<nTemp
) zTemp
[jj
] = 0;
997 }else if( pOp
->zComment
){
998 sqlite3_snprintf(nTemp
, zTemp
, "%s", pOp
->zComment
);
999 jj
= sqlite3Strlen30(zTemp
);
1006 #endif /* SQLITE_DEBUG */
1009 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
1010 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1012 ** Compute a string that describes the P4 parameter for an opcode.
1013 ** Use zTemp for any required temporary buffer space.
1015 static char *displayP4(Op
*pOp
, char *zTemp
, int nTemp
){
1017 assert( nTemp
>=20 );
1018 switch( pOp
->p4type
){
1021 KeyInfo
*pKeyInfo
= pOp
->p4
.pKeyInfo
;
1022 assert( pKeyInfo
->aSortOrder
!=0 );
1023 sqlite3_snprintf(nTemp
, zTemp
, "k(%d", pKeyInfo
->nField
);
1024 i
= sqlite3Strlen30(zTemp
);
1025 for(j
=0; j
<pKeyInfo
->nField
; j
++){
1026 CollSeq
*pColl
= pKeyInfo
->aColl
[j
];
1027 const char *zColl
= pColl
? pColl
->zName
: "nil";
1028 int n
= sqlite3Strlen30(zColl
);
1029 if( n
==6 && memcmp(zColl
,"BINARY",6)==0 ){
1034 memcpy(&zTemp
[i
],",...",4);
1038 if( pKeyInfo
->aSortOrder
[j
] ){
1041 memcpy(&zTemp
[i
], zColl
, n
+1);
1050 CollSeq
*pColl
= pOp
->p4
.pColl
;
1051 sqlite3_snprintf(nTemp
, zTemp
, "(%.20s)", pColl
->zName
);
1055 FuncDef
*pDef
= pOp
->p4
.pFunc
;
1056 sqlite3_snprintf(nTemp
, zTemp
, "%s(%d)", pDef
->zName
, pDef
->nArg
);
1060 sqlite3_snprintf(nTemp
, zTemp
, "%lld", *pOp
->p4
.pI64
);
1064 sqlite3_snprintf(nTemp
, zTemp
, "%d", pOp
->p4
.i
);
1068 sqlite3_snprintf(nTemp
, zTemp
, "%.16g", *pOp
->p4
.pReal
);
1072 Mem
*pMem
= pOp
->p4
.pMem
;
1073 if( pMem
->flags
& MEM_Str
){
1075 }else if( pMem
->flags
& MEM_Int
){
1076 sqlite3_snprintf(nTemp
, zTemp
, "%lld", pMem
->u
.i
);
1077 }else if( pMem
->flags
& MEM_Real
){
1078 sqlite3_snprintf(nTemp
, zTemp
, "%.16g", pMem
->u
.r
);
1079 }else if( pMem
->flags
& MEM_Null
){
1080 sqlite3_snprintf(nTemp
, zTemp
, "NULL");
1082 assert( pMem
->flags
& MEM_Blob
);
1087 #ifndef SQLITE_OMIT_VIRTUALTABLE
1089 sqlite3_vtab
*pVtab
= pOp
->p4
.pVtab
->pVtab
;
1090 sqlite3_snprintf(nTemp
, zTemp
, "vtab:%p:%p", pVtab
, pVtab
->pModule
);
1095 sqlite3_snprintf(nTemp
, zTemp
, "intarray");
1098 case P4_SUBPROGRAM
: {
1099 sqlite3_snprintf(nTemp
, zTemp
, "program");
1120 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
1122 ** The prepared statements need to know in advance the complete set of
1123 ** attached databases that will be use. A mask of these databases
1124 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
1125 ** p->btreeMask of databases that will require a lock.
1127 void sqlite3VdbeUsesBtree(Vdbe
*p
, int i
){
1128 assert( i
>=0 && i
<p
->db
->nDb
&& i
<(int)sizeof(yDbMask
)*8 );
1129 assert( i
<(int)sizeof(p
->btreeMask
)*8 );
1130 DbMaskSet(p
->btreeMask
, i
);
1131 if( i
!=1 && sqlite3BtreeSharable(p
->db
->aDb
[i
].pBt
) ){
1132 DbMaskSet(p
->lockMask
, i
);
1136 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1138 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
1139 ** this routine obtains the mutex associated with each BtShared structure
1140 ** that may be accessed by the VM passed as an argument. In doing so it also
1141 ** sets the BtShared.db member of each of the BtShared structures, ensuring
1142 ** that the correct busy-handler callback is invoked if required.
1144 ** If SQLite is not threadsafe but does support shared-cache mode, then
1145 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
1146 ** of all of BtShared structures accessible via the database handle
1147 ** associated with the VM.
1149 ** If SQLite is not threadsafe and does not support shared-cache mode, this
1150 ** function is a no-op.
1152 ** The p->btreeMask field is a bitmask of all btrees that the prepared
1153 ** statement p will ever use. Let N be the number of bits in p->btreeMask
1154 ** corresponding to btrees that use shared cache. Then the runtime of
1155 ** this routine is N*N. But as N is rarely more than 1, this should not
1158 void sqlite3VdbeEnter(Vdbe
*p
){
1163 if( DbMaskAllZero(p
->lockMask
) ) return; /* The common case */
1167 for(i
=0; i
<nDb
; i
++){
1168 if( i
!=1 && DbMaskTest(p
->lockMask
,i
) && ALWAYS(aDb
[i
].pBt
!=0) ){
1169 sqlite3BtreeEnter(aDb
[i
].pBt
);
1175 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1177 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
1179 void sqlite3VdbeLeave(Vdbe
*p
){
1184 if( DbMaskAllZero(p
->lockMask
) ) return; /* The common case */
1188 for(i
=0; i
<nDb
; i
++){
1189 if( i
!=1 && DbMaskTest(p
->lockMask
,i
) && ALWAYS(aDb
[i
].pBt
!=0) ){
1190 sqlite3BtreeLeave(aDb
[i
].pBt
);
1196 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1198 ** Print a single opcode. This routine is used for debugging only.
1200 void sqlite3VdbePrintOp(FILE *pOut
, int pc
, Op
*pOp
){
1204 static const char *zFormat1
= "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
1205 if( pOut
==0 ) pOut
= stdout
;
1206 zP4
= displayP4(pOp
, zPtr
, sizeof(zPtr
));
1207 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1208 displayComment(pOp
, zP4
, zCom
, sizeof(zCom
));
1212 /* NB: The sqlite3OpcodeName() function is implemented by code created
1213 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
1214 ** information from the vdbe.c source text */
1215 fprintf(pOut
, zFormat1
, pc
,
1216 sqlite3OpcodeName(pOp
->opcode
), pOp
->p1
, pOp
->p2
, pOp
->p3
, zP4
, pOp
->p5
,
1224 ** Release an array of N Mem elements
1226 static void releaseMemArray(Mem
*p
, int N
){
1229 sqlite3
*db
= p
->db
;
1230 u8 malloc_failed
= db
->mallocFailed
;
1231 if( db
->pnBytesFreed
){
1233 if( p
->szMalloc
) sqlite3DbFree(db
, p
->zMalloc
);
1234 }while( (++p
)<pEnd
);
1238 assert( (&p
[1])==pEnd
|| p
[0].db
==p
[1].db
);
1239 assert( sqlite3VdbeCheckMemInvariants(p
) );
1241 /* This block is really an inlined version of sqlite3VdbeMemRelease()
1242 ** that takes advantage of the fact that the memory cell value is
1243 ** being set to NULL after releasing any dynamic resources.
1245 ** The justification for duplicating code is that according to
1246 ** callgrind, this causes a certain test case to hit the CPU 4.7
1247 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
1248 ** sqlite3MemRelease() were called from here. With -O2, this jumps
1249 ** to 6.6 percent. The test case is inserting 1000 rows into a table
1250 ** with no indexes using a single prepared INSERT statement, bind()
1251 ** and reset(). Inserts are grouped into a transaction.
1253 testcase( p
->flags
& MEM_Agg
);
1254 testcase( p
->flags
& MEM_Dyn
);
1255 testcase( p
->flags
& MEM_Frame
);
1256 testcase( p
->flags
& MEM_RowSet
);
1257 if( p
->flags
&(MEM_Agg
|MEM_Dyn
|MEM_Frame
|MEM_RowSet
) ){
1258 sqlite3VdbeMemRelease(p
);
1259 }else if( p
->szMalloc
){
1260 sqlite3DbFree(db
, p
->zMalloc
);
1264 p
->flags
= MEM_Undefined
;
1265 }while( (++p
)<pEnd
);
1266 db
->mallocFailed
= malloc_failed
;
1271 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
1272 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
1274 void sqlite3VdbeFrameDelete(VdbeFrame
*p
){
1276 Mem
*aMem
= VdbeFrameMem(p
);
1277 VdbeCursor
**apCsr
= (VdbeCursor
**)&aMem
[p
->nChildMem
];
1278 for(i
=0; i
<p
->nChildCsr
; i
++){
1279 sqlite3VdbeFreeCursor(p
->v
, apCsr
[i
]);
1281 releaseMemArray(aMem
, p
->nChildMem
);
1282 sqlite3DbFree(p
->v
->db
, p
);
1285 #ifndef SQLITE_OMIT_EXPLAIN
1287 ** Give a listing of the program in the virtual machine.
1289 ** The interface is the same as sqlite3VdbeExec(). But instead of
1290 ** running the code, it invokes the callback once for each instruction.
1291 ** This feature is used to implement "EXPLAIN".
1293 ** When p->explain==1, each instruction is listed. When
1294 ** p->explain==2, only OP_Explain instructions are listed and these
1295 ** are shown in a different format. p->explain==2 is used to implement
1296 ** EXPLAIN QUERY PLAN.
1298 ** When p->explain==1, first the main program is listed, then each of
1299 ** the trigger subprograms are listed one by one.
1301 int sqlite3VdbeList(
1302 Vdbe
*p
/* The VDBE */
1304 int nRow
; /* Stop when row count reaches this */
1305 int nSub
= 0; /* Number of sub-vdbes seen so far */
1306 SubProgram
**apSub
= 0; /* Array of sub-vdbes */
1307 Mem
*pSub
= 0; /* Memory cell hold array of subprogs */
1308 sqlite3
*db
= p
->db
; /* The database connection */
1309 int i
; /* Loop counter */
1310 int rc
= SQLITE_OK
; /* Return code */
1311 Mem
*pMem
= &p
->aMem
[1]; /* First Mem of result set */
1313 assert( p
->explain
);
1314 assert( p
->magic
==VDBE_MAGIC_RUN
);
1315 assert( p
->rc
==SQLITE_OK
|| p
->rc
==SQLITE_BUSY
|| p
->rc
==SQLITE_NOMEM
);
1317 /* Even though this opcode does not use dynamic strings for
1318 ** the result, result columns may become dynamic if the user calls
1319 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
1321 releaseMemArray(pMem
, 8);
1324 if( p
->rc
==SQLITE_NOMEM
){
1325 /* This happens if a malloc() inside a call to sqlite3_column_text() or
1326 ** sqlite3_column_text16() failed. */
1327 db
->mallocFailed
= 1;
1328 return SQLITE_ERROR
;
1331 /* When the number of output rows reaches nRow, that means the
1332 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
1333 ** nRow is the sum of the number of rows in the main program, plus
1334 ** the sum of the number of rows in all trigger subprograms encountered
1335 ** so far. The nRow value will increase as new trigger subprograms are
1336 ** encountered, but p->pc will eventually catch up to nRow.
1339 if( p
->explain
==1 ){
1340 /* The first 8 memory cells are used for the result set. So we will
1341 ** commandeer the 9th cell to use as storage for an array of pointers
1342 ** to trigger subprograms. The VDBE is guaranteed to have at least 9
1344 assert( p
->nMem
>9 );
1346 if( pSub
->flags
&MEM_Blob
){
1347 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is
1348 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
1349 nSub
= pSub
->n
/sizeof(Vdbe
*);
1350 apSub
= (SubProgram
**)pSub
->z
;
1352 for(i
=0; i
<nSub
; i
++){
1353 nRow
+= apSub
[i
]->nOp
;
1359 }while( i
<nRow
&& p
->explain
==2 && p
->aOp
[i
].opcode
!=OP_Explain
);
1363 }else if( db
->u1
.isInterrupted
){
1364 p
->rc
= SQLITE_INTERRUPT
;
1366 sqlite3SetString(&p
->zErrMsg
, db
, "%s", sqlite3ErrStr(p
->rc
));
1371 /* The output line number is small enough that we are still in the
1375 /* We are currently listing subprograms. Figure out which one and
1376 ** pick up the appropriate opcode. */
1379 for(j
=0; i
>=apSub
[j
]->nOp
; j
++){
1382 pOp
= &apSub
[j
]->aOp
[i
];
1384 if( p
->explain
==1 ){
1385 pMem
->flags
= MEM_Int
;
1386 pMem
->u
.i
= i
; /* Program counter */
1389 pMem
->flags
= MEM_Static
|MEM_Str
|MEM_Term
;
1390 pMem
->z
= (char*)sqlite3OpcodeName(pOp
->opcode
); /* Opcode */
1391 assert( pMem
->z
!=0 );
1392 pMem
->n
= sqlite3Strlen30(pMem
->z
);
1393 pMem
->enc
= SQLITE_UTF8
;
1396 /* When an OP_Program opcode is encounter (the only opcode that has
1397 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
1398 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
1399 ** has not already been seen.
1401 if( pOp
->p4type
==P4_SUBPROGRAM
){
1402 int nByte
= (nSub
+1)*sizeof(SubProgram
*);
1404 for(j
=0; j
<nSub
; j
++){
1405 if( apSub
[j
]==pOp
->p4
.pProgram
) break;
1407 if( j
==nSub
&& SQLITE_OK
==sqlite3VdbeMemGrow(pSub
, nByte
, nSub
!=0) ){
1408 apSub
= (SubProgram
**)pSub
->z
;
1409 apSub
[nSub
++] = pOp
->p4
.pProgram
;
1410 pSub
->flags
|= MEM_Blob
;
1411 pSub
->n
= nSub
*sizeof(SubProgram
*);
1416 pMem
->flags
= MEM_Int
;
1417 pMem
->u
.i
= pOp
->p1
; /* P1 */
1420 pMem
->flags
= MEM_Int
;
1421 pMem
->u
.i
= pOp
->p2
; /* P2 */
1424 pMem
->flags
= MEM_Int
;
1425 pMem
->u
.i
= pOp
->p3
; /* P3 */
1428 if( sqlite3VdbeMemClearAndResize(pMem
, 32) ){ /* P4 */
1429 assert( p
->db
->mallocFailed
);
1430 return SQLITE_ERROR
;
1432 pMem
->flags
= MEM_Str
|MEM_Term
;
1433 zP4
= displayP4(pOp
, pMem
->z
, 32);
1435 sqlite3VdbeMemSetStr(pMem
, zP4
, -1, SQLITE_UTF8
, 0);
1437 assert( pMem
->z
!=0 );
1438 pMem
->n
= sqlite3Strlen30(pMem
->z
);
1439 pMem
->enc
= SQLITE_UTF8
;
1443 if( p
->explain
==1 ){
1444 if( sqlite3VdbeMemClearAndResize(pMem
, 4) ){
1445 assert( p
->db
->mallocFailed
);
1446 return SQLITE_ERROR
;
1448 pMem
->flags
= MEM_Str
|MEM_Term
;
1450 sqlite3_snprintf(3, pMem
->z
, "%.2x", pOp
->p5
); /* P5 */
1451 pMem
->enc
= SQLITE_UTF8
;
1454 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
1455 if( sqlite3VdbeMemClearAndResize(pMem
, 500) ){
1456 assert( p
->db
->mallocFailed
);
1457 return SQLITE_ERROR
;
1459 pMem
->flags
= MEM_Str
|MEM_Term
;
1460 pMem
->n
= displayComment(pOp
, zP4
, pMem
->z
, 500);
1461 pMem
->enc
= SQLITE_UTF8
;
1463 pMem
->flags
= MEM_Null
; /* Comment */
1467 p
->nResColumn
= 8 - 4*(p
->explain
-1);
1468 p
->pResultSet
= &p
->aMem
[1];
1474 #endif /* SQLITE_OMIT_EXPLAIN */
1478 ** Print the SQL that was used to generate a VDBE program.
1480 void sqlite3VdbePrintSql(Vdbe
*p
){
1484 }else if( p
->nOp
>=1 ){
1485 const VdbeOp
*pOp
= &p
->aOp
[0];
1486 if( pOp
->opcode
==OP_Init
&& pOp
->p4
.z
!=0 ){
1488 while( sqlite3Isspace(*z
) ) z
++;
1491 if( z
) printf("SQL: [%s]\n", z
);
1495 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1497 ** Print an IOTRACE message showing SQL content.
1499 void sqlite3VdbeIOTraceSql(Vdbe
*p
){
1502 if( sqlite3IoTrace
==0 ) return;
1505 if( pOp
->opcode
==OP_Init
&& pOp
->p4
.z
!=0 ){
1508 sqlite3_snprintf(sizeof(z
), z
, "%s", pOp
->p4
.z
);
1509 for(i
=0; sqlite3Isspace(z
[i
]); i
++){}
1510 for(j
=0; z
[i
]; i
++){
1511 if( sqlite3Isspace(z
[i
]) ){
1520 sqlite3IoTrace("SQL %s\n", z
);
1523 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1526 ** Allocate space from a fixed size buffer and return a pointer to
1527 ** that space. If insufficient space is available, return NULL.
1529 ** The pBuf parameter is the initial value of a pointer which will
1530 ** receive the new memory. pBuf is normally NULL. If pBuf is not
1531 ** NULL, it means that memory space has already been allocated and that
1532 ** this routine should not allocate any new memory. When pBuf is not
1533 ** NULL simply return pBuf. Only allocate new memory space when pBuf
1536 ** nByte is the number of bytes of space needed.
1538 ** *ppFrom points to available space and pEnd points to the end of the
1539 ** available space. When space is allocated, *ppFrom is advanced past
1540 ** the end of the allocated space.
1542 ** *pnByte is a counter of the number of bytes of space that have failed
1543 ** to allocate. If there is insufficient space in *ppFrom to satisfy the
1544 ** request, then increment *pnByte by the amount of the request.
1546 static void *allocSpace(
1547 void *pBuf
, /* Where return pointer will be stored */
1548 int nByte
, /* Number of bytes to allocate */
1549 u8
**ppFrom
, /* IN/OUT: Allocate from *ppFrom */
1550 u8
*pEnd
, /* Pointer to 1 byte past the end of *ppFrom buffer */
1551 int *pnByte
/* If allocation cannot be made, increment *pnByte */
1553 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom
) );
1554 if( pBuf
) return pBuf
;
1555 nByte
= ROUND8(nByte
);
1556 if( &(*ppFrom
)[nByte
] <= pEnd
){
1557 pBuf
= (void*)*ppFrom
;
1566 ** Rewind the VDBE back to the beginning in preparation for
1569 void sqlite3VdbeRewind(Vdbe
*p
){
1570 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1574 assert( p
->magic
==VDBE_MAGIC_INIT
);
1576 /* There should be at least one opcode.
1580 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
1581 p
->magic
= VDBE_MAGIC_RUN
;
1584 for(i
=1; i
<p
->nMem
; i
++){
1585 assert( p
->aMem
[i
].db
==p
->db
);
1590 p
->errorAction
= OE_Abort
;
1591 p
->magic
= VDBE_MAGIC_RUN
;
1594 p
->minWriteFileFormat
= 255;
1596 p
->nFkConstraint
= 0;
1598 for(i
=0; i
<p
->nOp
; i
++){
1600 p
->aOp
[i
].cycles
= 0;
1606 ** Prepare a virtual machine for execution for the first time after
1607 ** creating the virtual machine. This involves things such
1608 ** as allocating registers and initializing the program counter.
1609 ** After the VDBE has be prepped, it can be executed by one or more
1610 ** calls to sqlite3VdbeExec().
1612 ** This function may be called exactly once on each virtual machine.
1613 ** After this routine is called the VM has been "packaged" and is ready
1614 ** to run. After this routine is called, further calls to
1615 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects
1616 ** the Vdbe from the Parse object that helped generate it so that the
1617 ** the Vdbe becomes an independent entity and the Parse object can be
1620 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
1621 ** to its initial state after it has been run.
1623 void sqlite3VdbeMakeReady(
1624 Vdbe
*p
, /* The VDBE */
1625 Parse
*pParse
/* Parsing context */
1627 sqlite3
*db
; /* The database connection */
1628 int nVar
; /* Number of parameters */
1629 int nMem
; /* Number of VM memory registers */
1630 int nCursor
; /* Number of cursors required */
1631 int nArg
; /* Number of arguments in subprograms */
1632 int nOnce
; /* Number of OP_Once instructions */
1633 int n
; /* Loop counter */
1634 u8
*zCsr
; /* Memory available for allocation */
1635 u8
*zEnd
; /* First byte past allocated memory */
1636 int nByte
; /* How much extra memory is needed */
1640 assert( pParse
!=0 );
1641 assert( p
->magic
==VDBE_MAGIC_INIT
);
1642 assert( pParse
==p
->pParse
);
1644 assert( db
->mallocFailed
==0 );
1645 nVar
= pParse
->nVar
;
1646 nMem
= pParse
->nMem
;
1647 nCursor
= pParse
->nTab
;
1648 nArg
= pParse
->nMaxArg
;
1649 nOnce
= pParse
->nOnce
;
1650 if( nOnce
==0 ) nOnce
= 1; /* Ensure at least one byte in p->aOnceFlag[] */
1652 /* For each cursor required, also allocate a memory cell. Memory
1653 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
1654 ** the vdbe program. Instead they are used to allocate space for
1655 ** VdbeCursor/BtCursor structures. The blob of memory associated with
1656 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
1657 ** stores the blob of memory associated with cursor 1, etc.
1659 ** See also: allocateCursor().
1663 /* Allocate space for memory registers, SQL variables, VDBE cursors and
1664 ** an array to marshal SQL function arguments in.
1666 zCsr
= (u8
*)&p
->aOp
[p
->nOp
]; /* Memory avaliable for allocation */
1667 zEnd
= (u8
*)&p
->aOp
[pParse
->nOpAlloc
]; /* First byte past end of zCsr[] */
1669 resolveP2Values(p
, &nArg
);
1670 p
->usesStmtJournal
= (u8
)(pParse
->isMultiWrite
&& pParse
->mayAbort
);
1671 if( pParse
->explain
&& nMem
<10 ){
1674 memset(zCsr
, 0, zEnd
-zCsr
);
1675 zCsr
+= (zCsr
- (u8
*)0)&7;
1676 assert( EIGHT_BYTE_ALIGNMENT(zCsr
) );
1679 /* Memory for registers, parameters, cursor, etc, is allocated in two
1680 ** passes. On the first pass, we try to reuse unused space at the
1681 ** end of the opcode array. If we are unable to satisfy all memory
1682 ** requirements by reusing the opcode array tail, then the second
1683 ** pass will fill in the rest using a fresh allocation.
1685 ** This two-pass approach that reuses as much memory as possible from
1686 ** the leftover space at the end of the opcode array can significantly
1687 ** reduce the amount of memory held by a prepared statement.
1691 p
->aMem
= allocSpace(p
->aMem
, nMem
*sizeof(Mem
), &zCsr
, zEnd
, &nByte
);
1692 p
->aVar
= allocSpace(p
->aVar
, nVar
*sizeof(Mem
), &zCsr
, zEnd
, &nByte
);
1693 p
->apArg
= allocSpace(p
->apArg
, nArg
*sizeof(Mem
*), &zCsr
, zEnd
, &nByte
);
1694 p
->azVar
= allocSpace(p
->azVar
, nVar
*sizeof(char*), &zCsr
, zEnd
, &nByte
);
1695 p
->apCsr
= allocSpace(p
->apCsr
, nCursor
*sizeof(VdbeCursor
*),
1696 &zCsr
, zEnd
, &nByte
);
1697 p
->aOnceFlag
= allocSpace(p
->aOnceFlag
, nOnce
, &zCsr
, zEnd
, &nByte
);
1699 p
->pFree
= sqlite3DbMallocZero(db
, nByte
);
1702 zEnd
= &zCsr
[nByte
];
1703 }while( nByte
&& !db
->mallocFailed
);
1705 p
->nCursor
= nCursor
;
1706 p
->nOnceFlag
= nOnce
;
1708 p
->nVar
= (ynVar
)nVar
;
1709 for(n
=0; n
<nVar
; n
++){
1710 p
->aVar
[n
].flags
= MEM_Null
;
1715 p
->nzVar
= pParse
->nzVar
;
1716 memcpy(p
->azVar
, pParse
->azVar
, p
->nzVar
*sizeof(p
->azVar
[0]));
1717 memset(pParse
->azVar
, 0, pParse
->nzVar
*sizeof(pParse
->azVar
[0]));
1720 p
->aMem
--; /* aMem[] goes from 1..nMem */
1721 p
->nMem
= nMem
; /* not from 0..nMem-1 */
1722 for(n
=1; n
<=nMem
; n
++){
1723 p
->aMem
[n
].flags
= MEM_Undefined
;
1727 p
->explain
= pParse
->explain
;
1728 sqlite3VdbeRewind(p
);
1732 ** Close a VDBE cursor and release all the resources that cursor
1735 void sqlite3VdbeFreeCursor(Vdbe
*p
, VdbeCursor
*pCx
){
1739 sqlite3VdbeSorterClose(p
->db
, pCx
);
1741 sqlite3BtreeClose(pCx
->pBt
);
1742 /* The pCx->pCursor will be close automatically, if it exists, by
1743 ** the call above. */
1744 }else if( pCx
->pCursor
){
1745 sqlite3BtreeCloseCursor(pCx
->pCursor
);
1747 #ifndef SQLITE_OMIT_VIRTUALTABLE
1748 else if( pCx
->pVtabCursor
){
1749 sqlite3_vtab_cursor
*pVtabCursor
= pCx
->pVtabCursor
;
1750 const sqlite3_module
*pModule
= pVtabCursor
->pVtab
->pModule
;
1751 p
->inVtabMethod
= 1;
1752 pModule
->xClose(pVtabCursor
);
1753 p
->inVtabMethod
= 0;
1759 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
1760 ** is used, for example, when a trigger sub-program is halted to restore
1761 ** control to the main program.
1763 int sqlite3VdbeFrameRestore(VdbeFrame
*pFrame
){
1764 Vdbe
*v
= pFrame
->v
;
1765 v
->aOnceFlag
= pFrame
->aOnceFlag
;
1766 v
->nOnceFlag
= pFrame
->nOnceFlag
;
1767 v
->aOp
= pFrame
->aOp
;
1768 v
->nOp
= pFrame
->nOp
;
1769 v
->aMem
= pFrame
->aMem
;
1770 v
->nMem
= pFrame
->nMem
;
1771 v
->apCsr
= pFrame
->apCsr
;
1772 v
->nCursor
= pFrame
->nCursor
;
1773 v
->db
->lastRowid
= pFrame
->lastRowid
;
1774 v
->nChange
= pFrame
->nChange
;
1779 ** Close all cursors.
1781 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
1782 ** cell array. This is necessary as the memory cell array may contain
1783 ** pointers to VdbeFrame objects, which may in turn contain pointers to
1786 static void closeAllCursors(Vdbe
*p
){
1789 for(pFrame
=p
->pFrame
; pFrame
->pParent
; pFrame
=pFrame
->pParent
);
1790 sqlite3VdbeFrameRestore(pFrame
);
1794 assert( p
->nFrame
==0 );
1798 for(i
=0; i
<p
->nCursor
; i
++){
1799 VdbeCursor
*pC
= p
->apCsr
[i
];
1801 sqlite3VdbeFreeCursor(p
, pC
);
1807 releaseMemArray(&p
->aMem
[1], p
->nMem
);
1809 while( p
->pDelFrame
){
1810 VdbeFrame
*pDel
= p
->pDelFrame
;
1811 p
->pDelFrame
= pDel
->pParent
;
1812 sqlite3VdbeFrameDelete(pDel
);
1815 /* Delete any auxdata allocations made by the VM */
1816 if( p
->pAuxData
) sqlite3VdbeDeleteAuxData(p
, -1, 0);
1817 assert( p
->pAuxData
==0 );
1821 ** Clean up the VM after a single run.
1823 static void Cleanup(Vdbe
*p
){
1824 sqlite3
*db
= p
->db
;
1827 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
1828 ** Vdbe.aMem[] arrays have already been cleaned up. */
1830 if( p
->apCsr
) for(i
=0; i
<p
->nCursor
; i
++) assert( p
->apCsr
[i
]==0 );
1832 for(i
=1; i
<=p
->nMem
; i
++) assert( p
->aMem
[i
].flags
==MEM_Undefined
);
1836 sqlite3DbFree(db
, p
->zErrMsg
);
1842 ** Set the number of result columns that will be returned by this SQL
1843 ** statement. This is now set at compile time, rather than during
1844 ** execution of the vdbe program so that sqlite3_column_count() can
1845 ** be called on an SQL statement before sqlite3_step().
1847 void sqlite3VdbeSetNumCols(Vdbe
*p
, int nResColumn
){
1850 sqlite3
*db
= p
->db
;
1852 releaseMemArray(p
->aColName
, p
->nResColumn
*COLNAME_N
);
1853 sqlite3DbFree(db
, p
->aColName
);
1854 n
= nResColumn
*COLNAME_N
;
1855 p
->nResColumn
= (u16
)nResColumn
;
1856 p
->aColName
= pColName
= (Mem
*)sqlite3DbMallocZero(db
, sizeof(Mem
)*n
);
1857 if( p
->aColName
==0 ) return;
1859 pColName
->flags
= MEM_Null
;
1860 pColName
->db
= p
->db
;
1866 ** Set the name of the idx'th column to be returned by the SQL statement.
1867 ** zName must be a pointer to a nul terminated string.
1869 ** This call must be made after a call to sqlite3VdbeSetNumCols().
1871 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
1872 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
1873 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
1875 int sqlite3VdbeSetColName(
1876 Vdbe
*p
, /* Vdbe being configured */
1877 int idx
, /* Index of column zName applies to */
1878 int var
, /* One of the COLNAME_* constants */
1879 const char *zName
, /* Pointer to buffer containing name */
1880 void (*xDel
)(void*) /* Memory management strategy for zName */
1884 assert( idx
<p
->nResColumn
);
1885 assert( var
<COLNAME_N
);
1886 if( p
->db
->mallocFailed
){
1887 assert( !zName
|| xDel
!=SQLITE_DYNAMIC
);
1888 return SQLITE_NOMEM
;
1890 assert( p
->aColName
!=0 );
1891 pColName
= &(p
->aColName
[idx
+var
*p
->nResColumn
]);
1892 rc
= sqlite3VdbeMemSetStr(pColName
, zName
, -1, SQLITE_UTF8
, xDel
);
1893 assert( rc
!=0 || !zName
|| (pColName
->flags
&MEM_Term
)!=0 );
1898 ** A read or write transaction may or may not be active on database handle
1899 ** db. If a transaction is active, commit it. If there is a
1900 ** write-transaction spanning more than one database file, this routine
1901 ** takes care of the master journal trickery.
1903 static int vdbeCommit(sqlite3
*db
, Vdbe
*p
){
1905 int nTrans
= 0; /* Number of databases with an active write-transaction */
1907 int needXcommit
= 0;
1909 #ifdef SQLITE_OMIT_VIRTUALTABLE
1910 /* With this option, sqlite3VtabSync() is defined to be simply
1911 ** SQLITE_OK so p is not used.
1913 UNUSED_PARAMETER(p
);
1916 /* Before doing anything else, call the xSync() callback for any
1917 ** virtual module tables written in this transaction. This has to
1918 ** be done before determining whether a master journal file is
1919 ** required, as an xSync() callback may add an attached database
1920 ** to the transaction.
1922 rc
= sqlite3VtabSync(db
, p
);
1924 /* This loop determines (a) if the commit hook should be invoked and
1925 ** (b) how many database files have open write transactions, not
1926 ** including the temp database. (b) is important because if more than
1927 ** one database file has an open write transaction, a master journal
1928 ** file is required for an atomic commit.
1930 for(i
=0; rc
==SQLITE_OK
&& i
<db
->nDb
; i
++){
1931 Btree
*pBt
= db
->aDb
[i
].pBt
;
1932 if( sqlite3BtreeIsInTrans(pBt
) ){
1934 if( i
!=1 ) nTrans
++;
1935 sqlite3BtreeEnter(pBt
);
1936 rc
= sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt
));
1937 sqlite3BtreeLeave(pBt
);
1940 if( rc
!=SQLITE_OK
){
1944 /* If there are any write-transactions at all, invoke the commit hook */
1945 if( needXcommit
&& db
->xCommitCallback
){
1946 rc
= db
->xCommitCallback(db
->pCommitArg
);
1948 return SQLITE_CONSTRAINT_COMMITHOOK
;
1952 /* The simple case - no more than one database file (not counting the
1953 ** TEMP database) has a transaction active. There is no need for the
1956 ** If the return value of sqlite3BtreeGetFilename() is a zero length
1957 ** string, it means the main database is :memory: or a temp file. In
1958 ** that case we do not support atomic multi-file commits, so use the
1959 ** simple case then too.
1961 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db
->aDb
[0].pBt
))
1964 for(i
=0; rc
==SQLITE_OK
&& i
<db
->nDb
; i
++){
1965 Btree
*pBt
= db
->aDb
[i
].pBt
;
1967 rc
= sqlite3BtreeCommitPhaseOne(pBt
, 0);
1971 /* Do the commit only if all databases successfully complete phase 1.
1972 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1973 ** IO error while deleting or truncating a journal file. It is unlikely,
1974 ** but could happen. In this case abandon processing and return the error.
1976 for(i
=0; rc
==SQLITE_OK
&& i
<db
->nDb
; i
++){
1977 Btree
*pBt
= db
->aDb
[i
].pBt
;
1979 rc
= sqlite3BtreeCommitPhaseTwo(pBt
, 0);
1982 if( rc
==SQLITE_OK
){
1983 sqlite3VtabCommit(db
);
1987 /* The complex case - There is a multi-file write-transaction active.
1988 ** This requires a master journal file to ensure the transaction is
1989 ** committed atomically.
1991 #ifndef SQLITE_OMIT_DISKIO
1993 sqlite3_vfs
*pVfs
= db
->pVfs
;
1995 char *zMaster
= 0; /* File-name for the master journal */
1996 char const *zMainFile
= sqlite3BtreeGetFilename(db
->aDb
[0].pBt
);
1997 sqlite3_file
*pMaster
= 0;
2003 /* Select a master journal file name */
2004 nMainFile
= sqlite3Strlen30(zMainFile
);
2005 zMaster
= sqlite3MPrintf(db
, "%s-mjXXXXXX9XXz", zMainFile
);
2006 if( zMaster
==0 ) return SQLITE_NOMEM
;
2010 if( retryCount
>100 ){
2011 sqlite3_log(SQLITE_FULL
, "MJ delete: %s", zMaster
);
2012 sqlite3OsDelete(pVfs
, zMaster
, 0);
2014 }else if( retryCount
==1 ){
2015 sqlite3_log(SQLITE_FULL
, "MJ collide: %s", zMaster
);
2019 sqlite3_randomness(sizeof(iRandom
), &iRandom
);
2020 sqlite3_snprintf(13, &zMaster
[nMainFile
], "-mj%06X9%02X",
2021 (iRandom
>>8)&0xffffff, iRandom
&0xff);
2022 /* The antipenultimate character of the master journal name must
2023 ** be "9" to avoid name collisions when using 8+3 filenames. */
2024 assert( zMaster
[sqlite3Strlen30(zMaster
)-3]=='9' );
2025 sqlite3FileSuffix3(zMainFile
, zMaster
);
2026 rc
= sqlite3OsAccess(pVfs
, zMaster
, SQLITE_ACCESS_EXISTS
, &res
);
2027 }while( rc
==SQLITE_OK
&& res
);
2028 if( rc
==SQLITE_OK
){
2029 /* Open the master journal. */
2030 rc
= sqlite3OsOpenMalloc(pVfs
, zMaster
, &pMaster
,
2031 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|
2032 SQLITE_OPEN_EXCLUSIVE
|SQLITE_OPEN_MASTER_JOURNAL
, 0
2035 if( rc
!=SQLITE_OK
){
2036 sqlite3DbFree(db
, zMaster
);
2040 /* Write the name of each database file in the transaction into the new
2041 ** master journal file. If an error occurs at this point close
2042 ** and delete the master journal file. All the individual journal files
2043 ** still have 'null' as the master journal pointer, so they will roll
2044 ** back independently if a failure occurs.
2046 for(i
=0; i
<db
->nDb
; i
++){
2047 Btree
*pBt
= db
->aDb
[i
].pBt
;
2048 if( sqlite3BtreeIsInTrans(pBt
) ){
2049 char const *zFile
= sqlite3BtreeGetJournalname(pBt
);
2051 continue; /* Ignore TEMP and :memory: databases */
2053 assert( zFile
[0]!=0 );
2054 if( !needSync
&& !sqlite3BtreeSyncDisabled(pBt
) ){
2057 rc
= sqlite3OsWrite(pMaster
, zFile
, sqlite3Strlen30(zFile
)+1, offset
);
2058 offset
+= sqlite3Strlen30(zFile
)+1;
2059 if( rc
!=SQLITE_OK
){
2060 sqlite3OsCloseFree(pMaster
);
2061 sqlite3OsDelete(pVfs
, zMaster
, 0);
2062 sqlite3DbFree(db
, zMaster
);
2068 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
2069 ** flag is set this is not required.
2072 && 0==(sqlite3OsDeviceCharacteristics(pMaster
)&SQLITE_IOCAP_SEQUENTIAL
)
2073 && SQLITE_OK
!=(rc
= sqlite3OsSync(pMaster
, SQLITE_SYNC_NORMAL
))
2075 sqlite3OsCloseFree(pMaster
);
2076 sqlite3OsDelete(pVfs
, zMaster
, 0);
2077 sqlite3DbFree(db
, zMaster
);
2081 /* Sync all the db files involved in the transaction. The same call
2082 ** sets the master journal pointer in each individual journal. If
2083 ** an error occurs here, do not delete the master journal file.
2085 ** If the error occurs during the first call to
2086 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
2087 ** master journal file will be orphaned. But we cannot delete it,
2088 ** in case the master journal file name was written into the journal
2089 ** file before the failure occurred.
2091 for(i
=0; rc
==SQLITE_OK
&& i
<db
->nDb
; i
++){
2092 Btree
*pBt
= db
->aDb
[i
].pBt
;
2094 rc
= sqlite3BtreeCommitPhaseOne(pBt
, zMaster
);
2097 sqlite3OsCloseFree(pMaster
);
2098 assert( rc
!=SQLITE_BUSY
);
2099 if( rc
!=SQLITE_OK
){
2100 sqlite3DbFree(db
, zMaster
);
2104 /* Delete the master journal file. This commits the transaction. After
2105 ** doing this the directory is synced again before any individual
2106 ** transaction files are deleted.
2108 rc
= sqlite3OsDelete(pVfs
, zMaster
, 1);
2109 sqlite3DbFree(db
, zMaster
);
2115 /* All files and directories have already been synced, so the following
2116 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
2117 ** deleting or truncating journals. If something goes wrong while
2118 ** this is happening we don't really care. The integrity of the
2119 ** transaction is already guaranteed, but some stray 'cold' journals
2120 ** may be lying around. Returning an error code won't help matters.
2122 disable_simulated_io_errors();
2123 sqlite3BeginBenignMalloc();
2124 for(i
=0; i
<db
->nDb
; i
++){
2125 Btree
*pBt
= db
->aDb
[i
].pBt
;
2127 sqlite3BtreeCommitPhaseTwo(pBt
, 1);
2130 sqlite3EndBenignMalloc();
2131 enable_simulated_io_errors();
2133 sqlite3VtabCommit(db
);
2141 ** This routine checks that the sqlite3.nVdbeActive count variable
2142 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
2143 ** currently active. An assertion fails if the two counts do not match.
2144 ** This is an internal self-check only - it is not an essential processing
2147 ** This is a no-op if NDEBUG is defined.
2150 static void checkActiveVdbeCnt(sqlite3
*db
){
2157 if( sqlite3_stmt_busy((sqlite3_stmt
*)p
) ){
2159 if( p
->readOnly
==0 ) nWrite
++;
2160 if( p
->bIsReader
) nRead
++;
2164 assert( cnt
==db
->nVdbeActive
);
2165 assert( nWrite
==db
->nVdbeWrite
);
2166 assert( nRead
==db
->nVdbeRead
);
2169 #define checkActiveVdbeCnt(x)
2173 ** If the Vdbe passed as the first argument opened a statement-transaction,
2174 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
2175 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
2176 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
2177 ** statement transaction is committed.
2179 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
2180 ** Otherwise SQLITE_OK.
2182 int sqlite3VdbeCloseStatement(Vdbe
*p
, int eOp
){
2183 sqlite3
*const db
= p
->db
;
2186 /* If p->iStatement is greater than zero, then this Vdbe opened a
2187 ** statement transaction that should be closed here. The only exception
2188 ** is that an IO error may have occurred, causing an emergency rollback.
2189 ** In this case (db->nStatement==0), and there is nothing to do.
2191 if( db
->nStatement
&& p
->iStatement
){
2193 const int iSavepoint
= p
->iStatement
-1;
2195 assert( eOp
==SAVEPOINT_ROLLBACK
|| eOp
==SAVEPOINT_RELEASE
);
2196 assert( db
->nStatement
>0 );
2197 assert( p
->iStatement
==(db
->nStatement
+db
->nSavepoint
) );
2199 for(i
=0; i
<db
->nDb
; i
++){
2200 int rc2
= SQLITE_OK
;
2201 Btree
*pBt
= db
->aDb
[i
].pBt
;
2203 if( eOp
==SAVEPOINT_ROLLBACK
){
2204 rc2
= sqlite3BtreeSavepoint(pBt
, SAVEPOINT_ROLLBACK
, iSavepoint
);
2206 if( rc2
==SQLITE_OK
){
2207 rc2
= sqlite3BtreeSavepoint(pBt
, SAVEPOINT_RELEASE
, iSavepoint
);
2209 if( rc
==SQLITE_OK
){
2217 if( rc
==SQLITE_OK
){
2218 if( eOp
==SAVEPOINT_ROLLBACK
){
2219 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_ROLLBACK
, iSavepoint
);
2221 if( rc
==SQLITE_OK
){
2222 rc
= sqlite3VtabSavepoint(db
, SAVEPOINT_RELEASE
, iSavepoint
);
2226 /* If the statement transaction is being rolled back, also restore the
2227 ** database handles deferred constraint counter to the value it had when
2228 ** the statement transaction was opened. */
2229 if( eOp
==SAVEPOINT_ROLLBACK
){
2230 db
->nDeferredCons
= p
->nStmtDefCons
;
2231 db
->nDeferredImmCons
= p
->nStmtDefImmCons
;
2238 ** This function is called when a transaction opened by the database
2239 ** handle associated with the VM passed as an argument is about to be
2240 ** committed. If there are outstanding deferred foreign key constraint
2241 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
2243 ** If there are outstanding FK violations and this function returns
2244 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
2245 ** and write an error message to it. Then return SQLITE_ERROR.
2247 #ifndef SQLITE_OMIT_FOREIGN_KEY
2248 int sqlite3VdbeCheckFk(Vdbe
*p
, int deferred
){
2249 sqlite3
*db
= p
->db
;
2250 if( (deferred
&& (db
->nDeferredCons
+db
->nDeferredImmCons
)>0)
2251 || (!deferred
&& p
->nFkConstraint
>0)
2253 p
->rc
= SQLITE_CONSTRAINT_FOREIGNKEY
;
2254 p
->errorAction
= OE_Abort
;
2255 sqlite3SetString(&p
->zErrMsg
, db
, "FOREIGN KEY constraint failed");
2256 return SQLITE_ERROR
;
2263 ** This routine is called the when a VDBE tries to halt. If the VDBE
2264 ** has made changes and is in autocommit mode, then commit those
2265 ** changes. If a rollback is needed, then do the rollback.
2267 ** This routine is the only way to move the state of a VM from
2268 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
2269 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
2271 ** Return an error code. If the commit could not complete because of
2272 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
2273 ** means the close did not happen and needs to be repeated.
2275 int sqlite3VdbeHalt(Vdbe
*p
){
2276 int rc
; /* Used to store transient return codes */
2277 sqlite3
*db
= p
->db
;
2279 /* This function contains the logic that determines if a statement or
2280 ** transaction will be committed or rolled back as a result of the
2281 ** execution of this virtual machine.
2283 ** If any of the following errors occur:
2290 ** Then the internal cache might have been left in an inconsistent
2291 ** state. We need to rollback the statement transaction, if there is
2292 ** one, or the complete transaction if there is no statement transaction.
2295 if( p
->db
->mallocFailed
){
2296 p
->rc
= SQLITE_NOMEM
;
2298 if( p
->aOnceFlag
) memset(p
->aOnceFlag
, 0, p
->nOnceFlag
);
2300 if( p
->magic
!=VDBE_MAGIC_RUN
){
2303 checkActiveVdbeCnt(db
);
2305 /* No commit or rollback needed if the program never started or if the
2306 ** SQL statement does not read or write a database file. */
2307 if( p
->pc
>=0 && p
->bIsReader
){
2308 int mrc
; /* Primary error code from p->rc */
2309 int eStatementOp
= 0;
2310 int isSpecialError
; /* Set to true if a 'special' error */
2312 /* Lock all btrees used by the statement */
2313 sqlite3VdbeEnter(p
);
2315 /* Check for one of the special errors */
2317 isSpecialError
= mrc
==SQLITE_NOMEM
|| mrc
==SQLITE_IOERR
2318 || mrc
==SQLITE_INTERRUPT
|| mrc
==SQLITE_FULL
;
2319 if( isSpecialError
){
2320 /* If the query was read-only and the error code is SQLITE_INTERRUPT,
2321 ** no rollback is necessary. Otherwise, at least a savepoint
2322 ** transaction must be rolled back to restore the database to a
2323 ** consistent state.
2325 ** Even if the statement is read-only, it is important to perform
2326 ** a statement or transaction rollback operation. If the error
2327 ** occurred while writing to the journal, sub-journal or database
2328 ** file as part of an effort to free up cache space (see function
2329 ** pagerStress() in pager.c), the rollback is required to restore
2330 ** the pager to a consistent state.
2332 if( !p
->readOnly
|| mrc
!=SQLITE_INTERRUPT
){
2333 if( (mrc
==SQLITE_NOMEM
|| mrc
==SQLITE_FULL
) && p
->usesStmtJournal
){
2334 eStatementOp
= SAVEPOINT_ROLLBACK
;
2336 /* We are forced to roll back the active transaction. Before doing
2337 ** so, abort any other statements this handle currently has active.
2339 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
2340 sqlite3CloseSavepoints(db
);
2346 /* Check for immediate foreign key violations. */
2347 if( p
->rc
==SQLITE_OK
){
2348 sqlite3VdbeCheckFk(p
, 0);
2351 /* If the auto-commit flag is set and this is the only active writer
2352 ** VM, then we do either a commit or rollback of the current transaction.
2354 ** Note: This block also runs if one of the special errors handled
2355 ** above has occurred.
2357 if( !sqlite3VtabInSync(db
)
2359 && db
->nVdbeWrite
==(p
->readOnly
==0)
2361 if( p
->rc
==SQLITE_OK
|| (p
->errorAction
==OE_Fail
&& !isSpecialError
) ){
2362 rc
= sqlite3VdbeCheckFk(p
, 1);
2363 if( rc
!=SQLITE_OK
){
2364 if( NEVER(p
->readOnly
) ){
2365 sqlite3VdbeLeave(p
);
2366 return SQLITE_ERROR
;
2368 rc
= SQLITE_CONSTRAINT_FOREIGNKEY
;
2370 /* The auto-commit flag is true, the vdbe program was successful
2371 ** or hit an 'OR FAIL' constraint and there are no deferred foreign
2372 ** key constraints to hold up the transaction. This means a commit
2374 rc
= vdbeCommit(db
, p
);
2376 if( rc
==SQLITE_BUSY
&& p
->readOnly
){
2377 sqlite3VdbeLeave(p
);
2379 }else if( rc
!=SQLITE_OK
){
2381 sqlite3RollbackAll(db
, SQLITE_OK
);
2383 db
->nDeferredCons
= 0;
2384 db
->nDeferredImmCons
= 0;
2385 db
->flags
&= ~SQLITE_DeferFKs
;
2386 sqlite3CommitInternalChanges(db
);
2389 sqlite3RollbackAll(db
, SQLITE_OK
);
2392 }else if( eStatementOp
==0 ){
2393 if( p
->rc
==SQLITE_OK
|| p
->errorAction
==OE_Fail
){
2394 eStatementOp
= SAVEPOINT_RELEASE
;
2395 }else if( p
->errorAction
==OE_Abort
){
2396 eStatementOp
= SAVEPOINT_ROLLBACK
;
2398 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
2399 sqlite3CloseSavepoints(db
);
2404 /* If eStatementOp is non-zero, then a statement transaction needs to
2405 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
2406 ** do so. If this operation returns an error, and the current statement
2407 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
2408 ** current statement error code.
2411 rc
= sqlite3VdbeCloseStatement(p
, eStatementOp
);
2413 if( p
->rc
==SQLITE_OK
|| (p
->rc
&0xff)==SQLITE_CONSTRAINT
){
2415 sqlite3DbFree(db
, p
->zErrMsg
);
2418 sqlite3RollbackAll(db
, SQLITE_ABORT_ROLLBACK
);
2419 sqlite3CloseSavepoints(db
);
2424 /* If this was an INSERT, UPDATE or DELETE and no statement transaction
2425 ** has been rolled back, update the database connection change-counter.
2427 if( p
->changeCntOn
){
2428 if( eStatementOp
!=SAVEPOINT_ROLLBACK
){
2429 sqlite3VdbeSetChanges(db
, p
->nChange
);
2431 sqlite3VdbeSetChanges(db
, 0);
2436 /* Release the locks */
2437 sqlite3VdbeLeave(p
);
2440 /* We have successfully halted and closed the VM. Record this fact. */
2443 if( !p
->readOnly
) db
->nVdbeWrite
--;
2444 if( p
->bIsReader
) db
->nVdbeRead
--;
2445 assert( db
->nVdbeActive
>=db
->nVdbeRead
);
2446 assert( db
->nVdbeRead
>=db
->nVdbeWrite
);
2447 assert( db
->nVdbeWrite
>=0 );
2449 p
->magic
= VDBE_MAGIC_HALT
;
2450 checkActiveVdbeCnt(db
);
2451 if( p
->db
->mallocFailed
){
2452 p
->rc
= SQLITE_NOMEM
;
2455 /* If the auto-commit flag is set to true, then any locks that were held
2456 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
2457 ** to invoke any required unlock-notify callbacks.
2459 if( db
->autoCommit
){
2460 sqlite3ConnectionUnlocked(db
);
2463 assert( db
->nVdbeActive
>0 || db
->autoCommit
==0 || db
->nStatement
==0 );
2464 return (p
->rc
==SQLITE_BUSY
? SQLITE_BUSY
: SQLITE_OK
);
2469 ** Each VDBE holds the result of the most recent sqlite3_step() call
2470 ** in p->rc. This routine sets that result back to SQLITE_OK.
2472 void sqlite3VdbeResetStepResult(Vdbe
*p
){
2477 ** Copy the error code and error message belonging to the VDBE passed
2478 ** as the first argument to its database handle (so that they will be
2479 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
2481 ** This function does not clear the VDBE error code or message, just
2482 ** copies them to the database handle.
2484 int sqlite3VdbeTransferError(Vdbe
*p
){
2485 sqlite3
*db
= p
->db
;
2488 u8 mallocFailed
= db
->mallocFailed
;
2489 sqlite3BeginBenignMalloc();
2490 if( db
->pErr
==0 ) db
->pErr
= sqlite3ValueNew(db
);
2491 sqlite3ValueSetStr(db
->pErr
, -1, p
->zErrMsg
, SQLITE_UTF8
, SQLITE_TRANSIENT
);
2492 sqlite3EndBenignMalloc();
2493 db
->mallocFailed
= mallocFailed
;
2496 sqlite3Error(db
, rc
);
2501 #ifdef SQLITE_ENABLE_SQLLOG
2503 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
2506 static void vdbeInvokeSqllog(Vdbe
*v
){
2507 if( sqlite3GlobalConfig
.xSqllog
&& v
->rc
==SQLITE_OK
&& v
->zSql
&& v
->pc
>=0 ){
2508 char *zExpanded
= sqlite3VdbeExpandSql(v
, v
->zSql
);
2509 assert( v
->db
->init
.busy
==0 );
2511 sqlite3GlobalConfig
.xSqllog(
2512 sqlite3GlobalConfig
.pSqllogArg
, v
->db
, zExpanded
, 1
2514 sqlite3DbFree(v
->db
, zExpanded
);
2519 # define vdbeInvokeSqllog(x)
2523 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
2524 ** Write any error messages into *pzErrMsg. Return the result code.
2526 ** After this routine is run, the VDBE should be ready to be executed
2529 ** To look at it another way, this routine resets the state of the
2530 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
2533 int sqlite3VdbeReset(Vdbe
*p
){
2537 /* If the VM did not run to completion or if it encountered an
2538 ** error, then it might not have been halted properly. So halt
2543 /* If the VDBE has be run even partially, then transfer the error code
2544 ** and error message from the VDBE into the main database structure. But
2545 ** if the VDBE has just been set to run but has not actually executed any
2546 ** instructions yet, leave the main database error information unchanged.
2549 vdbeInvokeSqllog(p
);
2550 sqlite3VdbeTransferError(p
);
2551 sqlite3DbFree(db
, p
->zErrMsg
);
2553 if( p
->runOnlyOnce
) p
->expired
= 1;
2554 }else if( p
->rc
&& p
->expired
){
2555 /* The expired flag was set on the VDBE before the first call
2556 ** to sqlite3_step(). For consistency (since sqlite3_step() was
2557 ** called), set the database error in this case as well.
2559 sqlite3ErrorWithMsg(db
, p
->rc
, p
->zErrMsg
? "%s" : 0, p
->zErrMsg
);
2560 sqlite3DbFree(db
, p
->zErrMsg
);
2564 /* Reclaim all memory used by the VDBE
2568 /* Save profiling information from this VDBE run.
2572 FILE *out
= fopen("vdbe_profile.out", "a");
2575 fprintf(out
, "---- ");
2576 for(i
=0; i
<p
->nOp
; i
++){
2577 fprintf(out
, "%02x", p
->aOp
[i
].opcode
);
2582 fprintf(out
, "-- ");
2583 for(i
=0; (c
= p
->zSql
[i
])!=0; i
++){
2584 if( pc
=='\n' ) fprintf(out
, "-- ");
2588 if( pc
!='\n' ) fprintf(out
, "\n");
2590 for(i
=0; i
<p
->nOp
; i
++){
2592 sqlite3_snprintf(sizeof(zHdr
), zHdr
, "%6u %12llu %8llu ",
2595 p
->aOp
[i
].cnt
>0 ? p
->aOp
[i
].cycles
/p
->aOp
[i
].cnt
: 0
2597 fprintf(out
, "%s", zHdr
);
2598 sqlite3VdbePrintOp(out
, i
, &p
->aOp
[i
]);
2604 p
->iCurrentTime
= 0;
2605 p
->magic
= VDBE_MAGIC_INIT
;
2606 return p
->rc
& db
->errMask
;
2610 ** Clean up and delete a VDBE after execution. Return an integer which is
2611 ** the result code. Write any error message text into *pzErrMsg.
2613 int sqlite3VdbeFinalize(Vdbe
*p
){
2615 if( p
->magic
==VDBE_MAGIC_RUN
|| p
->magic
==VDBE_MAGIC_HALT
){
2616 rc
= sqlite3VdbeReset(p
);
2617 assert( (rc
& p
->db
->errMask
)==rc
);
2619 sqlite3VdbeDelete(p
);
2624 ** If parameter iOp is less than zero, then invoke the destructor for
2625 ** all auxiliary data pointers currently cached by the VM passed as
2626 ** the first argument.
2628 ** Or, if iOp is greater than or equal to zero, then the destructor is
2629 ** only invoked for those auxiliary data pointers created by the user
2630 ** function invoked by the OP_Function opcode at instruction iOp of
2631 ** VM pVdbe, and only then if:
2633 ** * the associated function parameter is the 32nd or later (counting
2634 ** from left to right), or
2636 ** * the corresponding bit in argument mask is clear (where the first
2637 ** function parameter corresponds to bit 0 etc.).
2639 void sqlite3VdbeDeleteAuxData(Vdbe
*pVdbe
, int iOp
, int mask
){
2640 AuxData
**pp
= &pVdbe
->pAuxData
;
2642 AuxData
*pAux
= *pp
;
2644 || (pAux
->iOp
==iOp
&& (pAux
->iArg
>31 || !(mask
& MASKBIT32(pAux
->iArg
))))
2646 testcase( pAux
->iArg
==31 );
2647 if( pAux
->xDelete
){
2648 pAux
->xDelete(pAux
->pAux
);
2651 sqlite3DbFree(pVdbe
->db
, pAux
);
2659 ** Free all memory associated with the Vdbe passed as the second argument,
2660 ** except for object itself, which is preserved.
2662 ** The difference between this function and sqlite3VdbeDelete() is that
2663 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
2664 ** the database connection and frees the object itself.
2666 void sqlite3VdbeClearObject(sqlite3
*db
, Vdbe
*p
){
2667 SubProgram
*pSub
, *pNext
;
2669 assert( p
->db
==0 || p
->db
==db
);
2670 releaseMemArray(p
->aVar
, p
->nVar
);
2671 releaseMemArray(p
->aColName
, p
->nResColumn
*COLNAME_N
);
2672 for(pSub
=p
->pProgram
; pSub
; pSub
=pNext
){
2673 pNext
= pSub
->pNext
;
2674 vdbeFreeOpArray(db
, pSub
->aOp
, pSub
->nOp
);
2675 sqlite3DbFree(db
, pSub
);
2677 for(i
=p
->nzVar
-1; i
>=0; i
--) sqlite3DbFree(db
, p
->azVar
[i
]);
2678 vdbeFreeOpArray(db
, p
->aOp
, p
->nOp
);
2679 sqlite3DbFree(db
, p
->aColName
);
2680 sqlite3DbFree(db
, p
->zSql
);
2681 sqlite3DbFree(db
, p
->pFree
);
2685 ** Delete an entire VDBE.
2687 void sqlite3VdbeDelete(Vdbe
*p
){
2690 if( NEVER(p
==0) ) return;
2692 assert( sqlite3_mutex_held(db
->mutex
) );
2693 sqlite3VdbeClearObject(db
, p
);
2695 p
->pPrev
->pNext
= p
->pNext
;
2697 assert( db
->pVdbe
==p
);
2698 db
->pVdbe
= p
->pNext
;
2701 p
->pNext
->pPrev
= p
->pPrev
;
2703 p
->magic
= VDBE_MAGIC_DEAD
;
2705 sqlite3DbFree(db
, p
);
2709 ** The cursor "p" has a pending seek operation that has not yet been
2710 ** carried out. Seek the cursor now. If an error occurs, return
2711 ** the appropriate error code.
2713 static int SQLITE_NOINLINE
handleDeferredMoveto(VdbeCursor
*p
){
2716 extern int sqlite3_search_count
;
2718 assert( p
->deferredMoveto
);
2719 assert( p
->isTable
);
2720 rc
= sqlite3BtreeMovetoUnpacked(p
->pCursor
, 0, p
->movetoTarget
, 0, &res
);
2722 if( res
!=0 ) return SQLITE_CORRUPT_BKPT
;
2724 sqlite3_search_count
++;
2726 p
->deferredMoveto
= 0;
2727 p
->cacheStatus
= CACHE_STALE
;
2732 ** Something has moved cursor "p" out of place. Maybe the row it was
2733 ** pointed to was deleted out from under it. Or maybe the btree was
2734 ** rebalanced. Whatever the cause, try to restore "p" to the place it
2735 ** is supposed to be pointing. If the row was deleted out from under the
2736 ** cursor, set the cursor to point to a NULL row.
2738 static int SQLITE_NOINLINE
handleMovedCursor(VdbeCursor
*p
){
2739 int isDifferentRow
, rc
;
2740 assert( p
->pCursor
!=0 );
2741 assert( sqlite3BtreeCursorHasMoved(p
->pCursor
) );
2742 rc
= sqlite3BtreeCursorRestore(p
->pCursor
, &isDifferentRow
);
2743 p
->cacheStatus
= CACHE_STALE
;
2744 if( isDifferentRow
) p
->nullRow
= 1;
2749 ** Check to ensure that the cursor is valid. Restore the cursor
2750 ** if need be. Return any I/O error from the restore operation.
2752 int sqlite3VdbeCursorRestore(VdbeCursor
*p
){
2753 if( sqlite3BtreeCursorHasMoved(p
->pCursor
) ){
2754 return handleMovedCursor(p
);
2760 ** Make sure the cursor p is ready to read or write the row to which it
2761 ** was last positioned. Return an error code if an OOM fault or I/O error
2762 ** prevents us from positioning the cursor to its correct position.
2764 ** If a MoveTo operation is pending on the given cursor, then do that
2765 ** MoveTo now. If no move is pending, check to see if the row has been
2766 ** deleted out from under the cursor and if it has, mark the row as
2769 ** If the cursor is already pointing to the correct row and that row has
2770 ** not been deleted out from under the cursor, then this routine is a no-op.
2772 int sqlite3VdbeCursorMoveto(VdbeCursor
*p
){
2773 if( p
->deferredMoveto
){
2774 return handleDeferredMoveto(p
);
2776 if( p
->pCursor
&& sqlite3BtreeCursorHasMoved(p
->pCursor
) ){
2777 return handleMovedCursor(p
);
2783 ** The following functions:
2785 ** sqlite3VdbeSerialType()
2786 ** sqlite3VdbeSerialTypeLen()
2787 ** sqlite3VdbeSerialLen()
2788 ** sqlite3VdbeSerialPut()
2789 ** sqlite3VdbeSerialGet()
2791 ** encapsulate the code that serializes values for storage in SQLite
2792 ** data and index records. Each serialized value consists of a
2793 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
2794 ** integer, stored as a varint.
2796 ** In an SQLite index record, the serial type is stored directly before
2797 ** the blob of data that it corresponds to. In a table record, all serial
2798 ** types are stored at the start of the record, and the blobs of data at
2799 ** the end. Hence these functions allow the caller to handle the
2800 ** serial-type and data blob separately.
2802 ** The following table describes the various storage classes for data:
2804 ** serial type bytes of data type
2805 ** -------------- --------------- ---------------
2807 ** 1 1 signed integer
2808 ** 2 2 signed integer
2809 ** 3 3 signed integer
2810 ** 4 4 signed integer
2811 ** 5 6 signed integer
2812 ** 6 8 signed integer
2814 ** 8 0 Integer constant 0
2815 ** 9 0 Integer constant 1
2816 ** 10,11 reserved for expansion
2817 ** N>=12 and even (N-12)/2 BLOB
2818 ** N>=13 and odd (N-13)/2 text
2820 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
2821 ** of SQLite will not understand those serial types.
2825 ** Return the serial-type for the value stored in pMem.
2827 u32
sqlite3VdbeSerialType(Mem
*pMem
, int file_format
){
2828 int flags
= pMem
->flags
;
2831 if( flags
&MEM_Null
){
2834 if( flags
&MEM_Int
){
2835 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
2836 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
2840 if( i
<(-MAX_6BYTE
) ) return 6;
2841 /* Previous test prevents: u = -(-9223372036854775808) */
2847 return ((i
&1)==i
&& file_format
>=4) ? 8+(u32
)u
: 1;
2849 if( u
<=32767 ) return 2;
2850 if( u
<=8388607 ) return 3;
2851 if( u
<=2147483647 ) return 4;
2852 if( u
<=MAX_6BYTE
) return 5;
2855 if( flags
&MEM_Real
){
2858 assert( pMem
->db
->mallocFailed
|| flags
&(MEM_Str
|MEM_Blob
) );
2859 assert( pMem
->n
>=0 );
2861 if( flags
& MEM_Zero
){
2864 return ((n
*2) + 12 + ((flags
&MEM_Str
)!=0));
2868 ** Return the length of the data corresponding to the supplied serial-type.
2870 u32
sqlite3VdbeSerialTypeLen(u32 serial_type
){
2871 if( serial_type
>=12 ){
2872 return (serial_type
-12)/2;
2874 static const u8 aSize
[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
2875 return aSize
[serial_type
];
2880 ** If we are on an architecture with mixed-endian floating
2881 ** points (ex: ARM7) then swap the lower 4 bytes with the
2882 ** upper 4 bytes. Return the result.
2884 ** For most architectures, this is a no-op.
2886 ** (later): It is reported to me that the mixed-endian problem
2887 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
2888 ** that early versions of GCC stored the two words of a 64-bit
2889 ** float in the wrong order. And that error has been propagated
2890 ** ever since. The blame is not necessarily with GCC, though.
2891 ** GCC might have just copying the problem from a prior compiler.
2892 ** I am also told that newer versions of GCC that follow a different
2893 ** ABI get the byte order right.
2895 ** Developers using SQLite on an ARM7 should compile and run their
2896 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
2897 ** enabled, some asserts below will ensure that the byte order of
2898 ** floating point values is correct.
2900 ** (2007-08-30) Frank van Vugt has studied this problem closely
2901 ** and has send his findings to the SQLite developers. Frank
2902 ** writes that some Linux kernels offer floating point hardware
2903 ** emulation that uses only 32-bit mantissas instead of a full
2904 ** 48-bits as required by the IEEE standard. (This is the
2905 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point
2906 ** byte swapping becomes very complicated. To avoid problems,
2907 ** the necessary byte swapping is carried out using a 64-bit integer
2908 ** rather than a 64-bit float. Frank assures us that the code here
2909 ** works for him. We, the developers, have no way to independently
2910 ** verify this, but Frank seems to know what he is talking about
2913 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
2914 static u64
floatSwap(u64 in
){
2927 # define swapMixedEndianFloat(X) X = floatSwap(X)
2929 # define swapMixedEndianFloat(X)
2933 ** Write the serialized data blob for the value stored in pMem into
2934 ** buf. It is assumed that the caller has allocated sufficient space.
2935 ** Return the number of bytes written.
2937 ** nBuf is the amount of space left in buf[]. The caller is responsible
2938 ** for allocating enough space to buf[] to hold the entire field, exclusive
2939 ** of the pMem->u.nZero bytes for a MEM_Zero value.
2941 ** Return the number of bytes actually written into buf[]. The number
2942 ** of bytes in the zero-filled tail is included in the return value only
2943 ** if those bytes were zeroed in buf[].
2945 u32
sqlite3VdbeSerialPut(u8
*buf
, Mem
*pMem
, u32 serial_type
){
2948 /* Integer and Real */
2949 if( serial_type
<=7 && serial_type
>0 ){
2952 if( serial_type
==7 ){
2953 assert( sizeof(v
)==sizeof(pMem
->u
.r
) );
2954 memcpy(&v
, &pMem
->u
.r
, sizeof(v
));
2955 swapMixedEndianFloat(v
);
2959 len
= i
= sqlite3VdbeSerialTypeLen(serial_type
);
2962 buf
[--i
] = (u8
)(v
&0xFF);
2968 /* String or blob */
2969 if( serial_type
>=12 ){
2970 assert( pMem
->n
+ ((pMem
->flags
& MEM_Zero
)?pMem
->u
.nZero
:0)
2971 == (int)sqlite3VdbeSerialTypeLen(serial_type
) );
2973 memcpy(buf
, pMem
->z
, len
);
2977 /* NULL or constants 0 or 1 */
2981 /* Input "x" is a sequence of unsigned characters that represent a
2982 ** big-endian integer. Return the equivalent native integer
2984 #define ONE_BYTE_INT(x) ((i8)(x)[0])
2985 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1])
2986 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
2987 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
2988 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
2991 ** Deserialize the data blob pointed to by buf as serial type serial_type
2992 ** and store the result in pMem. Return the number of bytes read.
2994 ** This function is implemented as two separate routines for performance.
2995 ** The few cases that require local variables are broken out into a separate
2996 ** routine so that in most cases the overhead of moving the stack pointer
2999 static u32 SQLITE_NOINLINE
serialGet(
3000 const unsigned char *buf
, /* Buffer to deserialize from */
3001 u32 serial_type
, /* Serial type to deserialize */
3002 Mem
*pMem
/* Memory cell to write value into */
3004 u64 x
= FOUR_BYTE_UINT(buf
);
3005 u32 y
= FOUR_BYTE_UINT(buf
+4);
3007 if( serial_type
==6 ){
3008 pMem
->u
.i
= *(i64
*)&x
;
3009 pMem
->flags
= MEM_Int
;
3010 testcase( pMem
->u
.i
<0 );
3012 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
3013 /* Verify that integers and floating point values use the same
3014 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
3015 ** defined that 64-bit floating point values really are mixed
3018 static const u64 t1
= ((u64
)0x3ff00000)<<32;
3019 static const double r1
= 1.0;
3021 swapMixedEndianFloat(t2
);
3022 assert( sizeof(r1
)==sizeof(t2
) && memcmp(&r1
, &t2
, sizeof(r1
))==0 );
3024 assert( sizeof(x
)==8 && sizeof(pMem
->u
.r
)==8 );
3025 swapMixedEndianFloat(x
);
3026 memcpy(&pMem
->u
.r
, &x
, sizeof(x
));
3027 pMem
->flags
= sqlite3IsNaN(pMem
->u
.r
) ? MEM_Null
: MEM_Real
;
3031 u32
sqlite3VdbeSerialGet(
3032 const unsigned char *buf
, /* Buffer to deserialize from */
3033 u32 serial_type
, /* Serial type to deserialize */
3034 Mem
*pMem
/* Memory cell to write value into */
3036 switch( serial_type
){
3037 case 10: /* Reserved for future use */
3038 case 11: /* Reserved for future use */
3039 case 0: { /* NULL */
3040 pMem
->flags
= MEM_Null
;
3043 case 1: { /* 1-byte signed integer */
3044 pMem
->u
.i
= ONE_BYTE_INT(buf
);
3045 pMem
->flags
= MEM_Int
;
3046 testcase( pMem
->u
.i
<0 );
3049 case 2: { /* 2-byte signed integer */
3050 pMem
->u
.i
= TWO_BYTE_INT(buf
);
3051 pMem
->flags
= MEM_Int
;
3052 testcase( pMem
->u
.i
<0 );
3055 case 3: { /* 3-byte signed integer */
3056 pMem
->u
.i
= THREE_BYTE_INT(buf
);
3057 pMem
->flags
= MEM_Int
;
3058 testcase( pMem
->u
.i
<0 );
3061 case 4: { /* 4-byte signed integer */
3062 pMem
->u
.i
= FOUR_BYTE_INT(buf
);
3063 pMem
->flags
= MEM_Int
;
3064 testcase( pMem
->u
.i
<0 );
3067 case 5: { /* 6-byte signed integer */
3068 pMem
->u
.i
= FOUR_BYTE_UINT(buf
+2) + (((i64
)1)<<32)*TWO_BYTE_INT(buf
);
3069 pMem
->flags
= MEM_Int
;
3070 testcase( pMem
->u
.i
<0 );
3073 case 6: /* 8-byte signed integer */
3074 case 7: { /* IEEE floating point */
3075 /* These use local variables, so do them in a separate routine
3076 ** to avoid having to move the frame pointer in the common case */
3077 return serialGet(buf
,serial_type
,pMem
);
3079 case 8: /* Integer 0 */
3080 case 9: { /* Integer 1 */
3081 pMem
->u
.i
= serial_type
-8;
3082 pMem
->flags
= MEM_Int
;
3086 static const u16 aFlag
[] = { MEM_Blob
|MEM_Ephem
, MEM_Str
|MEM_Ephem
};
3087 pMem
->z
= (char *)buf
;
3088 pMem
->n
= (serial_type
-12)/2;
3089 pMem
->flags
= aFlag
[serial_type
&1];
3096 ** This routine is used to allocate sufficient space for an UnpackedRecord
3097 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
3098 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
3100 ** The space is either allocated using sqlite3DbMallocRaw() or from within
3101 ** the unaligned buffer passed via the second and third arguments (presumably
3102 ** stack space). If the former, then *ppFree is set to a pointer that should
3103 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
3104 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
3105 ** before returning.
3107 ** If an OOM error occurs, NULL is returned.
3109 UnpackedRecord
*sqlite3VdbeAllocUnpackedRecord(
3110 KeyInfo
*pKeyInfo
, /* Description of the record */
3111 char *pSpace
, /* Unaligned space available */
3112 int szSpace
, /* Size of pSpace[] in bytes */
3113 char **ppFree
/* OUT: Caller should free this pointer */
3115 UnpackedRecord
*p
; /* Unpacked record to return */
3116 int nOff
; /* Increment pSpace by nOff to align it */
3117 int nByte
; /* Number of bytes required for *p */
3119 /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
3120 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
3121 ** it by. If pSpace is already 8-byte aligned, nOff should be zero.
3123 nOff
= (8 - (SQLITE_PTR_TO_INT(pSpace
) & 7)) & 7;
3124 nByte
= ROUND8(sizeof(UnpackedRecord
)) + sizeof(Mem
)*(pKeyInfo
->nField
+1);
3125 if( nByte
>szSpace
+nOff
){
3126 p
= (UnpackedRecord
*)sqlite3DbMallocRaw(pKeyInfo
->db
, nByte
);
3127 *ppFree
= (char *)p
;
3130 p
= (UnpackedRecord
*)&pSpace
[nOff
];
3134 p
->aMem
= (Mem
*)&((char*)p
)[ROUND8(sizeof(UnpackedRecord
))];
3135 assert( pKeyInfo
->aSortOrder
!=0 );
3136 p
->pKeyInfo
= pKeyInfo
;
3137 p
->nField
= pKeyInfo
->nField
+ 1;
3142 ** Given the nKey-byte encoding of a record in pKey[], populate the
3143 ** UnpackedRecord structure indicated by the fourth argument with the
3144 ** contents of the decoded record.
3146 void sqlite3VdbeRecordUnpack(
3147 KeyInfo
*pKeyInfo
, /* Information about the record format */
3148 int nKey
, /* Size of the binary record */
3149 const void *pKey
, /* The binary record */
3150 UnpackedRecord
*p
/* Populate this structure before returning. */
3152 const unsigned char *aKey
= (const unsigned char *)pKey
;
3154 u32 idx
; /* Offset in aKey[] to read from */
3155 u16 u
; /* Unsigned loop counter */
3157 Mem
*pMem
= p
->aMem
;
3160 assert( EIGHT_BYTE_ALIGNMENT(pMem
) );
3161 idx
= getVarint32(aKey
, szHdr
);
3164 while( idx
<szHdr
&& d
<=nKey
){
3167 idx
+= getVarint32(&aKey
[idx
], serial_type
);
3168 pMem
->enc
= pKeyInfo
->enc
;
3169 pMem
->db
= pKeyInfo
->db
;
3170 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
3172 d
+= sqlite3VdbeSerialGet(&aKey
[d
], serial_type
, pMem
);
3174 if( (++u
)>=p
->nField
) break;
3176 assert( u
<=pKeyInfo
->nField
+ 1 );
3182 ** This function compares two index or table record keys in the same way
3183 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
3184 ** this function deserializes and compares values using the
3185 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
3186 ** in assert() statements to ensure that the optimized code in
3187 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
3189 ** Return true if the result of comparison is equivalent to desiredResult.
3190 ** Return false if there is a disagreement.
3192 static int vdbeRecordCompareDebug(
3193 int nKey1
, const void *pKey1
, /* Left key */
3194 const UnpackedRecord
*pPKey2
, /* Right key */
3195 int desiredResult
/* Correct answer */
3197 u32 d1
; /* Offset into aKey[] of next data element */
3198 u32 idx1
; /* Offset into aKey[] of next header element */
3199 u32 szHdr1
; /* Number of bytes in header */
3202 const unsigned char *aKey1
= (const unsigned char *)pKey1
;
3206 pKeyInfo
= pPKey2
->pKeyInfo
;
3207 if( pKeyInfo
->db
==0 ) return 1;
3208 mem1
.enc
= pKeyInfo
->enc
;
3209 mem1
.db
= pKeyInfo
->db
;
3210 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */
3211 VVA_ONLY( mem1
.szMalloc
= 0; ) /* Only needed by assert() statements */
3213 /* Compilers may complain that mem1.u.i is potentially uninitialized.
3214 ** We could initialize it, as shown here, to silence those complaints.
3215 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
3216 ** the unnecessary initialization has a measurable negative performance
3217 ** impact, since this routine is a very high runner. And so, we choose
3218 ** to ignore the compiler warnings and leave this variable uninitialized.
3220 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
3222 idx1
= getVarint32(aKey1
, szHdr1
);
3224 assert( pKeyInfo
->nField
+pKeyInfo
->nXField
>=pPKey2
->nField
|| CORRUPT_DB
);
3225 assert( pKeyInfo
->aSortOrder
!=0 );
3226 assert( pKeyInfo
->nField
>0 );
3227 assert( idx1
<=szHdr1
|| CORRUPT_DB
);
3231 /* Read the serial types for the next element in each key. */
3232 idx1
+= getVarint32( aKey1
+idx1
, serial_type1
);
3234 /* Verify that there is enough key space remaining to avoid
3235 ** a buffer overread. The "d1+serial_type1+2" subexpression will
3236 ** always be greater than or equal to the amount of required key space.
3237 ** Use that approximation to avoid the more expensive call to
3238 ** sqlite3VdbeSerialTypeLen() in the common case.
3240 if( d1
+serial_type1
+2>(u32
)nKey1
3241 && d1
+sqlite3VdbeSerialTypeLen(serial_type1
)>(u32
)nKey1
3246 /* Extract the values to be compared.
3248 d1
+= sqlite3VdbeSerialGet(&aKey1
[d1
], serial_type1
, &mem1
);
3250 /* Do the comparison
3252 rc
= sqlite3MemCompare(&mem1
, &pPKey2
->aMem
[i
], pKeyInfo
->aColl
[i
]);
3254 assert( mem1
.szMalloc
==0 ); /* See comment below */
3255 if( pKeyInfo
->aSortOrder
[i
] ){
3256 rc
= -rc
; /* Invert the result for DESC sort order. */
3258 goto debugCompareEnd
;
3261 }while( idx1
<szHdr1
&& i
<pPKey2
->nField
);
3263 /* No memory allocation is ever used on mem1. Prove this using
3264 ** the following assert(). If the assert() fails, it indicates a
3265 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
3267 assert( mem1
.szMalloc
==0 );
3269 /* rc==0 here means that one of the keys ran out of fields and
3270 ** all the fields up to that point were equal. Return the default_rc
3272 rc
= pPKey2
->default_rc
;
3275 if( desiredResult
==0 && rc
==0 ) return 1;
3276 if( desiredResult
<0 && rc
<0 ) return 1;
3277 if( desiredResult
>0 && rc
>0 ) return 1;
3278 if( CORRUPT_DB
) return 1;
3279 if( pKeyInfo
->db
->mallocFailed
) return 1;
3285 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
3286 ** using the collation sequence pColl. As usual, return a negative , zero
3287 ** or positive value if *pMem1 is less than, equal to or greater than
3288 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
3290 static int vdbeCompareMemString(
3293 const CollSeq
*pColl
,
3294 u8
*prcErr
/* If an OOM occurs, set to SQLITE_NOMEM */
3296 if( pMem1
->enc
==pColl
->enc
){
3297 /* The strings are already in the correct encoding. Call the
3298 ** comparison function directly */
3299 return pColl
->xCmp(pColl
->pUser
,pMem1
->n
,pMem1
->z
,pMem2
->n
,pMem2
->z
);
3302 const void *v1
, *v2
;
3306 sqlite3VdbeMemInit(&c1
, pMem1
->db
, MEM_Null
);
3307 sqlite3VdbeMemInit(&c2
, pMem1
->db
, MEM_Null
);
3308 sqlite3VdbeMemShallowCopy(&c1
, pMem1
, MEM_Ephem
);
3309 sqlite3VdbeMemShallowCopy(&c2
, pMem2
, MEM_Ephem
);
3310 v1
= sqlite3ValueText((sqlite3_value
*)&c1
, pColl
->enc
);
3311 n1
= v1
==0 ? 0 : c1
.n
;
3312 v2
= sqlite3ValueText((sqlite3_value
*)&c2
, pColl
->enc
);
3313 n2
= v2
==0 ? 0 : c2
.n
;
3314 rc
= pColl
->xCmp(pColl
->pUser
, n1
, v1
, n2
, v2
);
3315 sqlite3VdbeMemRelease(&c1
);
3316 sqlite3VdbeMemRelease(&c2
);
3317 if( (v1
==0 || v2
==0) && prcErr
) *prcErr
= SQLITE_NOMEM
;
3323 ** Compare two blobs. Return negative, zero, or positive if the first
3324 ** is less than, equal to, or greater than the second, respectively.
3325 ** If one blob is a prefix of the other, then the shorter is the lessor.
3327 static SQLITE_NOINLINE
int sqlite3BlobCompare(const Mem
*pB1
, const Mem
*pB2
){
3328 int c
= memcmp(pB1
->z
, pB2
->z
, pB1
->n
>pB2
->n
? pB2
->n
: pB1
->n
);
3330 return pB1
->n
- pB2
->n
;
3335 ** Compare the values contained by the two memory cells, returning
3336 ** negative, zero or positive if pMem1 is less than, equal to, or greater
3337 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
3338 ** and reals) sorted numerically, followed by text ordered by the collating
3339 ** sequence pColl and finally blob's ordered by memcmp().
3341 ** Two NULL values are considered equal by this function.
3343 int sqlite3MemCompare(const Mem
*pMem1
, const Mem
*pMem2
, const CollSeq
*pColl
){
3349 combined_flags
= f1
|f2
;
3350 assert( (combined_flags
& MEM_RowSet
)==0 );
3352 /* If one value is NULL, it is less than the other. If both values
3353 ** are NULL, return 0.
3355 if( combined_flags
&MEM_Null
){
3356 return (f2
&MEM_Null
) - (f1
&MEM_Null
);
3359 /* If one value is a number and the other is not, the number is less.
3360 ** If both are numbers, compare as reals if one is a real, or as integers
3361 ** if both values are integers.
3363 if( combined_flags
&(MEM_Int
|MEM_Real
) ){
3365 if( (f1
& f2
& MEM_Int
)!=0 ){
3366 if( pMem1
->u
.i
< pMem2
->u
.i
) return -1;
3367 if( pMem1
->u
.i
> pMem2
->u
.i
) return 1;
3370 if( (f1
&MEM_Real
)!=0 ){
3372 }else if( (f1
&MEM_Int
)!=0 ){
3373 r1
= (double)pMem1
->u
.i
;
3377 if( (f2
&MEM_Real
)!=0 ){
3379 }else if( (f2
&MEM_Int
)!=0 ){
3380 r2
= (double)pMem2
->u
.i
;
3384 if( r1
<r2
) return -1;
3385 if( r1
>r2
) return 1;
3389 /* If one value is a string and the other is a blob, the string is less.
3390 ** If both are strings, compare using the collating functions.
3392 if( combined_flags
&MEM_Str
){
3393 if( (f1
& MEM_Str
)==0 ){
3396 if( (f2
& MEM_Str
)==0 ){
3400 assert( pMem1
->enc
==pMem2
->enc
);
3401 assert( pMem1
->enc
==SQLITE_UTF8
||
3402 pMem1
->enc
==SQLITE_UTF16LE
|| pMem1
->enc
==SQLITE_UTF16BE
);
3404 /* The collation sequence must be defined at this point, even if
3405 ** the user deletes the collation sequence after the vdbe program is
3406 ** compiled (this was not always the case).
3408 assert( !pColl
|| pColl
->xCmp
);
3411 return vdbeCompareMemString(pMem1
, pMem2
, pColl
, 0);
3413 /* If a NULL pointer was passed as the collate function, fall through
3414 ** to the blob case and use memcmp(). */
3417 /* Both values must be blobs. Compare using memcmp(). */
3418 return sqlite3BlobCompare(pMem1
, pMem2
);
3423 ** The first argument passed to this function is a serial-type that
3424 ** corresponds to an integer - all values between 1 and 9 inclusive
3425 ** except 7. The second points to a buffer containing an integer value
3426 ** serialized according to serial_type. This function deserializes
3427 ** and returns the value.
3429 static i64
vdbeRecordDecodeInt(u32 serial_type
, const u8
*aKey
){
3431 assert( CORRUPT_DB
|| (serial_type
>=1 && serial_type
<=9 && serial_type
!=7) );
3432 switch( serial_type
){
3435 testcase( aKey
[0]&0x80 );
3436 return ONE_BYTE_INT(aKey
);
3438 testcase( aKey
[0]&0x80 );
3439 return TWO_BYTE_INT(aKey
);
3441 testcase( aKey
[0]&0x80 );
3442 return THREE_BYTE_INT(aKey
);
3444 testcase( aKey
[0]&0x80 );
3445 y
= FOUR_BYTE_UINT(aKey
);
3446 return (i64
)*(int*)&y
;
3449 testcase( aKey
[0]&0x80 );
3450 return FOUR_BYTE_UINT(aKey
+2) + (((i64
)1)<<32)*TWO_BYTE_INT(aKey
);
3453 u64 x
= FOUR_BYTE_UINT(aKey
);
3454 testcase( aKey
[0]&0x80 );
3455 x
= (x
<<32) | FOUR_BYTE_UINT(aKey
+4);
3456 return (i64
)*(i64
*)&x
;
3460 return (serial_type
- 8);
3464 ** This function compares the two table rows or index records
3465 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero
3466 ** or positive integer if key1 is less than, equal to or
3467 ** greater than key2. The {nKey1, pKey1} key must be a blob
3468 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2
3469 ** key must be a parsed key such as obtained from
3470 ** sqlite3VdbeParseRecord.
3472 ** If argument bSkip is non-zero, it is assumed that the caller has already
3473 ** determined that the first fields of the keys are equal.
3475 ** Key1 and Key2 do not have to contain the same number of fields. If all
3476 ** fields that appear in both keys are equal, then pPKey2->default_rc is
3479 ** If database corruption is discovered, set pPKey2->errCode to
3480 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered,
3481 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the
3482 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db).
3484 static int vdbeRecordCompareWithSkip(
3485 int nKey1
, const void *pKey1
, /* Left key */
3486 UnpackedRecord
*pPKey2
, /* Right key */
3487 int bSkip
/* If true, skip the first field */
3489 u32 d1
; /* Offset into aKey[] of next data element */
3490 int i
; /* Index of next field to compare */
3491 u32 szHdr1
; /* Size of record header in bytes */
3492 u32 idx1
; /* Offset of first type in header */
3493 int rc
= 0; /* Return value */
3494 Mem
*pRhs
= pPKey2
->aMem
; /* Next field of pPKey2 to compare */
3495 KeyInfo
*pKeyInfo
= pPKey2
->pKeyInfo
;
3496 const unsigned char *aKey1
= (const unsigned char *)pKey1
;
3499 /* If bSkip is true, then the caller has already determined that the first
3500 ** two elements in the keys are equal. Fix the various stack variables so
3501 ** that this routine begins comparing at the second field. */
3504 idx1
= 1 + getVarint32(&aKey1
[1], s1
);
3506 d1
= szHdr1
+ sqlite3VdbeSerialTypeLen(s1
);
3510 idx1
= getVarint32(aKey1
, szHdr1
);
3512 if( d1
>(unsigned)nKey1
){
3513 pPKey2
->errCode
= (u8
)SQLITE_CORRUPT_BKPT
;
3514 return 0; /* Corruption */
3519 VVA_ONLY( mem1
.szMalloc
= 0; ) /* Only needed by assert() statements */
3520 assert( pPKey2
->pKeyInfo
->nField
+pPKey2
->pKeyInfo
->nXField
>=pPKey2
->nField
3522 assert( pPKey2
->pKeyInfo
->aSortOrder
!=0 );
3523 assert( pPKey2
->pKeyInfo
->nField
>0 );
3524 assert( idx1
<=szHdr1
|| CORRUPT_DB
);
3528 /* RHS is an integer */
3529 if( pRhs
->flags
& MEM_Int
){
3530 serial_type
= aKey1
[idx1
];
3531 testcase( serial_type
==12 );
3532 if( serial_type
>=12 ){
3534 }else if( serial_type
==0 ){
3536 }else if( serial_type
==7 ){
3537 double rhs
= (double)pRhs
->u
.i
;
3538 sqlite3VdbeSerialGet(&aKey1
[d1
], serial_type
, &mem1
);
3541 }else if( mem1
.u
.r
>rhs
){
3545 i64 lhs
= vdbeRecordDecodeInt(serial_type
, &aKey1
[d1
]);
3546 i64 rhs
= pRhs
->u
.i
;
3549 }else if( lhs
>rhs
){
3556 else if( pRhs
->flags
& MEM_Real
){
3557 serial_type
= aKey1
[idx1
];
3558 if( serial_type
>=12 ){
3560 }else if( serial_type
==0 ){
3563 double rhs
= pRhs
->u
.r
;
3565 sqlite3VdbeSerialGet(&aKey1
[d1
], serial_type
, &mem1
);
3566 if( serial_type
==7 ){
3569 lhs
= (double)mem1
.u
.i
;
3573 }else if( lhs
>rhs
){
3579 /* RHS is a string */
3580 else if( pRhs
->flags
& MEM_Str
){
3581 getVarint32(&aKey1
[idx1
], serial_type
);
3582 testcase( serial_type
==12 );
3583 if( serial_type
<12 ){
3585 }else if( !(serial_type
& 0x01) ){
3588 mem1
.n
= (serial_type
- 12) / 2;
3589 testcase( (d1
+mem1
.n
)==(unsigned)nKey1
);
3590 testcase( (d1
+mem1
.n
+1)==(unsigned)nKey1
);
3591 if( (d1
+mem1
.n
) > (unsigned)nKey1
){
3592 pPKey2
->errCode
= (u8
)SQLITE_CORRUPT_BKPT
;
3593 return 0; /* Corruption */
3594 }else if( pKeyInfo
->aColl
[i
] ){
3595 mem1
.enc
= pKeyInfo
->enc
;
3596 mem1
.db
= pKeyInfo
->db
;
3597 mem1
.flags
= MEM_Str
;
3598 mem1
.z
= (char*)&aKey1
[d1
];
3599 rc
= vdbeCompareMemString(
3600 &mem1
, pRhs
, pKeyInfo
->aColl
[i
], &pPKey2
->errCode
3603 int nCmp
= MIN(mem1
.n
, pRhs
->n
);
3604 rc
= memcmp(&aKey1
[d1
], pRhs
->z
, nCmp
);
3605 if( rc
==0 ) rc
= mem1
.n
- pRhs
->n
;
3611 else if( pRhs
->flags
& MEM_Blob
){
3612 getVarint32(&aKey1
[idx1
], serial_type
);
3613 testcase( serial_type
==12 );
3614 if( serial_type
<12 || (serial_type
& 0x01) ){
3617 int nStr
= (serial_type
- 12) / 2;
3618 testcase( (d1
+nStr
)==(unsigned)nKey1
);
3619 testcase( (d1
+nStr
+1)==(unsigned)nKey1
);
3620 if( (d1
+nStr
) > (unsigned)nKey1
){
3621 pPKey2
->errCode
= (u8
)SQLITE_CORRUPT_BKPT
;
3622 return 0; /* Corruption */
3624 int nCmp
= MIN(nStr
, pRhs
->n
);
3625 rc
= memcmp(&aKey1
[d1
], pRhs
->z
, nCmp
);
3626 if( rc
==0 ) rc
= nStr
- pRhs
->n
;
3633 serial_type
= aKey1
[idx1
];
3634 rc
= (serial_type
!=0);
3638 if( pKeyInfo
->aSortOrder
[i
] ){
3641 assert( vdbeRecordCompareDebug(nKey1
, pKey1
, pPKey2
, rc
) );
3642 assert( mem1
.szMalloc
==0 ); /* See comment below */
3648 d1
+= sqlite3VdbeSerialTypeLen(serial_type
);
3649 idx1
+= sqlite3VarintLen(serial_type
);
3650 }while( idx1
<(unsigned)szHdr1
&& i
<pPKey2
->nField
&& d1
<=(unsigned)nKey1
);
3652 /* No memory allocation is ever used on mem1. Prove this using
3653 ** the following assert(). If the assert() fails, it indicates a
3654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */
3655 assert( mem1
.szMalloc
==0 );
3657 /* rc==0 here means that one or both of the keys ran out of fields and
3658 ** all the fields up to that point were equal. Return the default_rc
3661 || vdbeRecordCompareDebug(nKey1
, pKey1
, pPKey2
, pPKey2
->default_rc
)
3662 || pKeyInfo
->db
->mallocFailed
3664 return pPKey2
->default_rc
;
3666 int sqlite3VdbeRecordCompare(
3667 int nKey1
, const void *pKey1
, /* Left key */
3668 UnpackedRecord
*pPKey2
/* Right key */
3670 return vdbeRecordCompareWithSkip(nKey1
, pKey1
, pPKey2
, 0);
3675 ** This function is an optimized version of sqlite3VdbeRecordCompare()
3676 ** that (a) the first field of pPKey2 is an integer, and (b) the
3677 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
3678 ** byte (i.e. is less than 128).
3680 ** To avoid concerns about buffer overreads, this routine is only used
3681 ** on schemas where the maximum valid header size is 63 bytes or less.
3683 static int vdbeRecordCompareInt(
3684 int nKey1
, const void *pKey1
, /* Left key */
3685 UnpackedRecord
*pPKey2
/* Right key */
3687 const u8
*aKey
= &((const u8
*)pKey1
)[*(const u8
*)pKey1
& 0x3F];
3688 int serial_type
= ((const u8
*)pKey1
)[1];
3692 i64 v
= pPKey2
->aMem
[0].u
.i
;
3695 assert( (*(u8
*)pKey1
)<=0x3F || CORRUPT_DB
);
3696 switch( serial_type
){
3697 case 1: { /* 1-byte signed integer */
3698 lhs
= ONE_BYTE_INT(aKey
);
3702 case 2: { /* 2-byte signed integer */
3703 lhs
= TWO_BYTE_INT(aKey
);
3707 case 3: { /* 3-byte signed integer */
3708 lhs
= THREE_BYTE_INT(aKey
);
3712 case 4: { /* 4-byte signed integer */
3713 y
= FOUR_BYTE_UINT(aKey
);
3714 lhs
= (i64
)*(int*)&y
;
3718 case 5: { /* 6-byte signed integer */
3719 lhs
= FOUR_BYTE_UINT(aKey
+2) + (((i64
)1)<<32)*TWO_BYTE_INT(aKey
);
3723 case 6: { /* 8-byte signed integer */
3724 x
= FOUR_BYTE_UINT(aKey
);
3725 x
= (x
<<32) | FOUR_BYTE_UINT(aKey
+4);
3737 /* This case could be removed without changing the results of running
3738 ** this code. Including it causes gcc to generate a faster switch
3739 ** statement (since the range of switch targets now starts at zero and
3740 ** is contiguous) but does not cause any duplicate code to be generated
3741 ** (as gcc is clever enough to combine the two like cases). Other
3742 ** compilers might be similar. */
3744 return sqlite3VdbeRecordCompare(nKey1
, pKey1
, pPKey2
);
3747 return sqlite3VdbeRecordCompare(nKey1
, pKey1
, pPKey2
);
3754 }else if( pPKey2
->nField
>1 ){
3755 /* The first fields of the two keys are equal. Compare the trailing
3757 res
= vdbeRecordCompareWithSkip(nKey1
, pKey1
, pPKey2
, 1);
3759 /* The first fields of the two keys are equal and there are no trailing
3760 ** fields. Return pPKey2->default_rc in this case. */
3761 res
= pPKey2
->default_rc
;
3764 assert( vdbeRecordCompareDebug(nKey1
, pKey1
, pPKey2
, res
) );
3769 ** This function is an optimized version of sqlite3VdbeRecordCompare()
3770 ** that (a) the first field of pPKey2 is a string, that (b) the first field
3771 ** uses the collation sequence BINARY and (c) that the size-of-header varint
3772 ** at the start of (pKey1/nKey1) fits in a single byte.
3774 static int vdbeRecordCompareString(
3775 int nKey1
, const void *pKey1
, /* Left key */
3776 UnpackedRecord
*pPKey2
/* Right key */
3778 const u8
*aKey1
= (const u8
*)pKey1
;
3782 getVarint32(&aKey1
[1], serial_type
);
3783 if( serial_type
<12 ){
3784 res
= pPKey2
->r1
; /* (pKey1/nKey1) is a number or a null */
3785 }else if( !(serial_type
& 0x01) ){
3786 res
= pPKey2
->r2
; /* (pKey1/nKey1) is a blob */
3790 int szHdr
= aKey1
[0];
3792 nStr
= (serial_type
-12) / 2;
3793 if( (szHdr
+ nStr
) > nKey1
){
3794 pPKey2
->errCode
= (u8
)SQLITE_CORRUPT_BKPT
;
3795 return 0; /* Corruption */
3797 nCmp
= MIN( pPKey2
->aMem
[0].n
, nStr
);
3798 res
= memcmp(&aKey1
[szHdr
], pPKey2
->aMem
[0].z
, nCmp
);
3801 res
= nStr
- pPKey2
->aMem
[0].n
;
3803 if( pPKey2
->nField
>1 ){
3804 res
= vdbeRecordCompareWithSkip(nKey1
, pKey1
, pPKey2
, 1);
3806 res
= pPKey2
->default_rc
;
3820 assert( vdbeRecordCompareDebug(nKey1
, pKey1
, pPKey2
, res
)
3822 || pPKey2
->pKeyInfo
->db
->mallocFailed
3828 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
3829 ** suitable for comparing serialized records to the unpacked record passed
3830 ** as the only argument.
3832 RecordCompare
sqlite3VdbeFindCompare(UnpackedRecord
*p
){
3833 /* varintRecordCompareInt() and varintRecordCompareString() both assume
3834 ** that the size-of-header varint that occurs at the start of each record
3835 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
3836 ** also assumes that it is safe to overread a buffer by at least the
3837 ** maximum possible legal header size plus 8 bytes. Because there is
3838 ** guaranteed to be at least 74 (but not 136) bytes of padding following each
3839 ** buffer passed to varintRecordCompareInt() this makes it convenient to
3840 ** limit the size of the header to 64 bytes in cases where the first field
3843 ** The easiest way to enforce this limit is to consider only records with
3844 ** 13 fields or less. If the first field is an integer, the maximum legal
3845 ** header size is (12*5 + 1 + 1) bytes. */
3846 if( (p
->pKeyInfo
->nField
+ p
->pKeyInfo
->nXField
)<=13 ){
3847 int flags
= p
->aMem
[0].flags
;
3848 if( p
->pKeyInfo
->aSortOrder
[0] ){
3855 if( (flags
& MEM_Int
) ){
3856 return vdbeRecordCompareInt
;
3858 testcase( flags
& MEM_Real
);
3859 testcase( flags
& MEM_Null
);
3860 testcase( flags
& MEM_Blob
);
3861 if( (flags
& (MEM_Real
|MEM_Null
|MEM_Blob
))==0 && p
->pKeyInfo
->aColl
[0]==0 ){
3862 assert( flags
& MEM_Str
);
3863 return vdbeRecordCompareString
;
3867 return sqlite3VdbeRecordCompare
;
3871 ** pCur points at an index entry created using the OP_MakeRecord opcode.
3872 ** Read the rowid (the last field in the record) and store it in *rowid.
3873 ** Return SQLITE_OK if everything works, or an error code otherwise.
3875 ** pCur might be pointing to text obtained from a corrupt database file.
3876 ** So the content cannot be trusted. Do appropriate checks on the content.
3878 int sqlite3VdbeIdxRowid(sqlite3
*db
, BtCursor
*pCur
, i64
*rowid
){
3881 u32 szHdr
; /* Size of the header */
3882 u32 typeRowid
; /* Serial type of the rowid */
3883 u32 lenRowid
; /* Size of the rowid */
3886 /* Get the size of the index entry. Only indices entries of less
3887 ** than 2GiB are support - anything large must be database corruption.
3888 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
3889 ** this code can safely assume that nCellKey is 32-bits
3891 assert( sqlite3BtreeCursorIsValid(pCur
) );
3892 VVA_ONLY(rc
=) sqlite3BtreeKeySize(pCur
, &nCellKey
);
3893 assert( rc
==SQLITE_OK
); /* pCur is always valid so KeySize cannot fail */
3894 assert( (nCellKey
& SQLITE_MAX_U32
)==(u64
)nCellKey
);
3896 /* Read in the complete content of the index entry */
3897 sqlite3VdbeMemInit(&m
, db
, 0);
3898 rc
= sqlite3VdbeMemFromBtree(pCur
, 0, (u32
)nCellKey
, 1, &m
);
3903 /* The index entry must begin with a header size */
3904 (void)getVarint32((u8
*)m
.z
, szHdr
);
3905 testcase( szHdr
==3 );
3906 testcase( szHdr
==m
.n
);
3907 if( unlikely(szHdr
<3 || (int)szHdr
>m
.n
) ){
3908 goto idx_rowid_corruption
;
3911 /* The last field of the index should be an integer - the ROWID.
3912 ** Verify that the last entry really is an integer. */
3913 (void)getVarint32((u8
*)&m
.z
[szHdr
-1], typeRowid
);
3914 testcase( typeRowid
==1 );
3915 testcase( typeRowid
==2 );
3916 testcase( typeRowid
==3 );
3917 testcase( typeRowid
==4 );
3918 testcase( typeRowid
==5 );
3919 testcase( typeRowid
==6 );
3920 testcase( typeRowid
==8 );
3921 testcase( typeRowid
==9 );
3922 if( unlikely(typeRowid
<1 || typeRowid
>9 || typeRowid
==7) ){
3923 goto idx_rowid_corruption
;
3925 lenRowid
= sqlite3VdbeSerialTypeLen(typeRowid
);
3926 testcase( (u32
)m
.n
==szHdr
+lenRowid
);
3927 if( unlikely((u32
)m
.n
<szHdr
+lenRowid
) ){
3928 goto idx_rowid_corruption
;
3931 /* Fetch the integer off the end of the index record */
3932 sqlite3VdbeSerialGet((u8
*)&m
.z
[m
.n
-lenRowid
], typeRowid
, &v
);
3934 sqlite3VdbeMemRelease(&m
);
3937 /* Jump here if database corruption is detected after m has been
3938 ** allocated. Free the m object and return SQLITE_CORRUPT. */
3939 idx_rowid_corruption
:
3940 testcase( m
.szMalloc
!=0 );
3941 sqlite3VdbeMemRelease(&m
);
3942 return SQLITE_CORRUPT_BKPT
;
3946 ** Compare the key of the index entry that cursor pC is pointing to against
3947 ** the key string in pUnpacked. Write into *pRes a number
3948 ** that is negative, zero, or positive if pC is less than, equal to,
3949 ** or greater than pUnpacked. Return SQLITE_OK on success.
3951 ** pUnpacked is either created without a rowid or is truncated so that it
3952 ** omits the rowid at the end. The rowid at the end of the index entry
3953 ** is ignored as well. Hence, this routine only compares the prefixes
3954 ** of the keys prior to the final rowid, not the entire key.
3956 int sqlite3VdbeIdxKeyCompare(
3957 sqlite3
*db
, /* Database connection */
3958 VdbeCursor
*pC
, /* The cursor to compare against */
3959 UnpackedRecord
*pUnpacked
, /* Unpacked version of key */
3960 int *res
/* Write the comparison result here */
3964 BtCursor
*pCur
= pC
->pCursor
;
3967 assert( sqlite3BtreeCursorIsValid(pCur
) );
3968 VVA_ONLY(rc
=) sqlite3BtreeKeySize(pCur
, &nCellKey
);
3969 assert( rc
==SQLITE_OK
); /* pCur is always valid so KeySize cannot fail */
3970 /* nCellKey will always be between 0 and 0xffffffff because of the way
3971 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
3972 if( nCellKey
<=0 || nCellKey
>0x7fffffff ){
3974 return SQLITE_CORRUPT_BKPT
;
3976 sqlite3VdbeMemInit(&m
, db
, 0);
3977 rc
= sqlite3VdbeMemFromBtree(pC
->pCursor
, 0, (u32
)nCellKey
, 1, &m
);
3981 *res
= sqlite3VdbeRecordCompare(m
.n
, m
.z
, pUnpacked
);
3982 sqlite3VdbeMemRelease(&m
);
3987 ** This routine sets the value to be returned by subsequent calls to
3988 ** sqlite3_changes() on the database handle 'db'.
3990 void sqlite3VdbeSetChanges(sqlite3
*db
, int nChange
){
3991 assert( sqlite3_mutex_held(db
->mutex
) );
3992 db
->nChange
= nChange
;
3993 db
->nTotalChange
+= nChange
;
3997 ** Set a flag in the vdbe to update the change counter when it is finalised
4000 void sqlite3VdbeCountChanges(Vdbe
*v
){
4005 ** Mark every prepared statement associated with a database connection
4008 ** An expired statement means that recompilation of the statement is
4009 ** recommend. Statements expire when things happen that make their
4010 ** programs obsolete. Removing user-defined functions or collating
4011 ** sequences, or changing an authorization function are the types of
4012 ** things that make prepared statements obsolete.
4014 void sqlite3ExpirePreparedStatements(sqlite3
*db
){
4016 for(p
= db
->pVdbe
; p
; p
=p
->pNext
){
4022 ** Return the database associated with the Vdbe.
4024 sqlite3
*sqlite3VdbeDb(Vdbe
*v
){
4029 ** Return a pointer to an sqlite3_value structure containing the value bound
4030 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
4031 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
4032 ** constants) to the value before returning it.
4034 ** The returned value must be freed by the caller using sqlite3ValueFree().
4036 sqlite3_value
*sqlite3VdbeGetBoundValue(Vdbe
*v
, int iVar
, u8 aff
){
4039 Mem
*pMem
= &v
->aVar
[iVar
-1];
4040 if( 0==(pMem
->flags
& MEM_Null
) ){
4041 sqlite3_value
*pRet
= sqlite3ValueNew(v
->db
);
4043 sqlite3VdbeMemCopy((Mem
*)pRet
, pMem
);
4044 sqlite3ValueApplyAffinity(pRet
, aff
, SQLITE_UTF8
);
4053 ** Configure SQL variable iVar so that binding a new value to it signals
4054 ** to sqlite3_reoptimize() that re-preparing the statement may result
4055 ** in a better query plan.
4057 void sqlite3VdbeSetVarmask(Vdbe
*v
, int iVar
){
4060 v
->expmask
= 0xffffffff;
4062 v
->expmask
|= ((u32
)1 << (iVar
-1));
4066 #ifndef SQLITE_OMIT_VIRTUALTABLE
4068 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
4069 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
4070 ** in memory obtained from sqlite3DbMalloc).
4072 void sqlite3VtabImportErrmsg(Vdbe
*p
, sqlite3_vtab
*pVtab
){
4073 sqlite3
*db
= p
->db
;
4074 sqlite3DbFree(db
, p
->zErrMsg
);
4075 p
->zErrMsg
= sqlite3DbStrDup(db
, pVtab
->zErrMsg
);
4076 sqlite3_free(pVtab
->zErrMsg
);
4079 #endif /* SQLITE_OMIT_VIRTUALTABLE */