1.1 --- a/extensions/sqlite/sqlite-source/vdbeaux.c Fri Mar 28 06:21:34 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,2243 +0,0 @@
1.4 -/*
1.5 -** 2003 September 6
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** This file contains code used for creating, destroying, and populating
1.16 -** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) Prior
1.17 -** to version 2.8.7, all this code was combined into the vdbe.c source file.
1.18 -** But that file was getting too big so this subroutines were split out.
1.19 -*/
1.20 -#include "sqliteInt.h"
1.21 -#include <ctype.h>
1.22 -#include "vdbeInt.h"
1.23 -
1.24 -
1.25 -
1.26 -/*
1.27 -** When debugging the code generator in a symbolic debugger, one can
1.28 -** set the sqlite3_vdbe_addop_trace to 1 and all opcodes will be printed
1.29 -** as they are added to the instruction stream.
1.30 -*/
1.31 -#ifdef SQLITE_DEBUG
1.32 -int sqlite3_vdbe_addop_trace = 0;
1.33 -#endif
1.34 -
1.35 -
1.36 -/*
1.37 -** Create a new virtual database engine.
1.38 -*/
1.39 -Vdbe *sqlite3VdbeCreate(sqlite3 *db){
1.40 - Vdbe *p;
1.41 - p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
1.42 - if( p==0 ) return 0;
1.43 - p->db = db;
1.44 - if( db->pVdbe ){
1.45 - db->pVdbe->pPrev = p;
1.46 - }
1.47 - p->pNext = db->pVdbe;
1.48 - p->pPrev = 0;
1.49 - db->pVdbe = p;
1.50 - p->magic = VDBE_MAGIC_INIT;
1.51 - return p;
1.52 -}
1.53 -
1.54 -/*
1.55 -** Remember the SQL string for a prepared statement.
1.56 -*/
1.57 -void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n){
1.58 - if( p==0 ) return;
1.59 - assert( p->zSql==0 );
1.60 - p->zSql = sqlite3DbStrNDup(p->db, z, n);
1.61 -}
1.62 -
1.63 -/*
1.64 -** Return the SQL associated with a prepared statement
1.65 -*/
1.66 -const char *sqlite3VdbeGetSql(Vdbe *p){
1.67 - return p->zSql;
1.68 -}
1.69 -
1.70 -/*
1.71 -** Swap all content between two VDBE structures.
1.72 -*/
1.73 -void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
1.74 - Vdbe tmp, *pTmp;
1.75 - char *zTmp;
1.76 - int nTmp;
1.77 - tmp = *pA;
1.78 - *pA = *pB;
1.79 - *pB = tmp;
1.80 - pTmp = pA->pNext;
1.81 - pA->pNext = pB->pNext;
1.82 - pB->pNext = pTmp;
1.83 - pTmp = pA->pPrev;
1.84 - pA->pPrev = pB->pPrev;
1.85 - pB->pPrev = pTmp;
1.86 - zTmp = pA->zSql;
1.87 - pA->zSql = pB->zSql;
1.88 - pB->zSql = zTmp;
1.89 - nTmp = pA->nSql;
1.90 - pA->nSql = pB->nSql;
1.91 - pB->nSql = nTmp;
1.92 -}
1.93 -
1.94 -#ifdef SQLITE_DEBUG
1.95 -/*
1.96 -** Turn tracing on or off
1.97 -*/
1.98 -void sqlite3VdbeTrace(Vdbe *p, FILE *trace){
1.99 - p->trace = trace;
1.100 -}
1.101 -#endif
1.102 -
1.103 -/*
1.104 -** Resize the Vdbe.aOp array so that it contains at least N
1.105 -** elements. If the Vdbe is in VDBE_MAGIC_RUN state, then
1.106 -** the Vdbe.aOp array will be sized to contain exactly N
1.107 -** elements. Vdbe.nOpAlloc is set to reflect the new size of
1.108 -** the array.
1.109 -**
1.110 -** If an out-of-memory error occurs while resizing the array,
1.111 -** Vdbe.aOp and Vdbe.nOpAlloc remain unchanged (this is so that
1.112 -** any opcodes already allocated can be correctly deallocated
1.113 -** along with the rest of the Vdbe).
1.114 -*/
1.115 -static void resizeOpArray(Vdbe *p, int N){
1.116 - int runMode = p->magic==VDBE_MAGIC_RUN;
1.117 - if( runMode || p->nOpAlloc<N ){
1.118 - VdbeOp *pNew;
1.119 - int nNew = N + 100*(!runMode);
1.120 - int oldSize = p->nOpAlloc;
1.121 - pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op));
1.122 - if( pNew ){
1.123 - p->nOpAlloc = nNew;
1.124 - p->aOp = pNew;
1.125 - if( nNew>oldSize ){
1.126 - memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op));
1.127 - }
1.128 - }
1.129 - }
1.130 -}
1.131 -
1.132 -/*
1.133 -** Add a new instruction to the list of instructions current in the
1.134 -** VDBE. Return the address of the new instruction.
1.135 -**
1.136 -** Parameters:
1.137 -**
1.138 -** p Pointer to the VDBE
1.139 -**
1.140 -** op The opcode for this instruction
1.141 -**
1.142 -** p1, p2 First two of the three possible operands.
1.143 -**
1.144 -** Use the sqlite3VdbeResolveLabel() function to fix an address and
1.145 -** the sqlite3VdbeChangeP3() function to change the value of the P3
1.146 -** operand.
1.147 -*/
1.148 -int sqlite3VdbeAddOp(Vdbe *p, int op, int p1, int p2){
1.149 - int i;
1.150 - VdbeOp *pOp;
1.151 -
1.152 - i = p->nOp;
1.153 - assert( p->magic==VDBE_MAGIC_INIT );
1.154 - if( p->nOpAlloc<=i ){
1.155 - resizeOpArray(p, i+1);
1.156 - if( p->db->mallocFailed ){
1.157 - return 0;
1.158 - }
1.159 - }
1.160 - p->nOp++;
1.161 - pOp = &p->aOp[i];
1.162 - pOp->opcode = op;
1.163 - pOp->p1 = p1;
1.164 - pOp->p2 = p2;
1.165 - pOp->p3 = 0;
1.166 - pOp->p3type = P3_NOTUSED;
1.167 - p->expired = 0;
1.168 -#ifdef SQLITE_DEBUG
1.169 - if( sqlite3_vdbe_addop_trace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
1.170 -#endif
1.171 - return i;
1.172 -}
1.173 -
1.174 -/*
1.175 -** Add an opcode that includes the p3 value.
1.176 -*/
1.177 -int sqlite3VdbeOp3(Vdbe *p, int op, int p1, int p2, const char *zP3,int p3type){
1.178 - int addr = sqlite3VdbeAddOp(p, op, p1, p2);
1.179 - sqlite3VdbeChangeP3(p, addr, zP3, p3type);
1.180 - return addr;
1.181 -}
1.182 -
1.183 -/*
1.184 -** Create a new symbolic label for an instruction that has yet to be
1.185 -** coded. The symbolic label is really just a negative number. The
1.186 -** label can be used as the P2 value of an operation. Later, when
1.187 -** the label is resolved to a specific address, the VDBE will scan
1.188 -** through its operation list and change all values of P2 which match
1.189 -** the label into the resolved address.
1.190 -**
1.191 -** The VDBE knows that a P2 value is a label because labels are
1.192 -** always negative and P2 values are suppose to be non-negative.
1.193 -** Hence, a negative P2 value is a label that has yet to be resolved.
1.194 -**
1.195 -** Zero is returned if a malloc() fails.
1.196 -*/
1.197 -int sqlite3VdbeMakeLabel(Vdbe *p){
1.198 - int i;
1.199 - i = p->nLabel++;
1.200 - assert( p->magic==VDBE_MAGIC_INIT );
1.201 - if( i>=p->nLabelAlloc ){
1.202 - p->nLabelAlloc = p->nLabelAlloc*2 + 10;
1.203 - p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
1.204 - p->nLabelAlloc*sizeof(p->aLabel[0]));
1.205 - }
1.206 - if( p->aLabel ){
1.207 - p->aLabel[i] = -1;
1.208 - }
1.209 - return -1-i;
1.210 -}
1.211 -
1.212 -/*
1.213 -** Resolve label "x" to be the address of the next instruction to
1.214 -** be inserted. The parameter "x" must have been obtained from
1.215 -** a prior call to sqlite3VdbeMakeLabel().
1.216 -*/
1.217 -void sqlite3VdbeResolveLabel(Vdbe *p, int x){
1.218 - int j = -1-x;
1.219 - assert( p->magic==VDBE_MAGIC_INIT );
1.220 - assert( j>=0 && j<p->nLabel );
1.221 - if( p->aLabel ){
1.222 - p->aLabel[j] = p->nOp;
1.223 - }
1.224 -}
1.225 -
1.226 -/*
1.227 -** Return non-zero if opcode 'op' is guarenteed not to push more values
1.228 -** onto the VDBE stack than it pops off.
1.229 -*/
1.230 -static int opcodeNoPush(u8 op){
1.231 - /* The 10 NOPUSH_MASK_n constants are defined in the automatically
1.232 - ** generated header file opcodes.h. Each is a 16-bit bitmask, one
1.233 - ** bit corresponding to each opcode implemented by the virtual
1.234 - ** machine in vdbe.c. The bit is true if the word "no-push" appears
1.235 - ** in a comment on the same line as the "case OP_XXX:" in
1.236 - ** sqlite3VdbeExec() in vdbe.c.
1.237 - **
1.238 - ** If the bit is true, then the corresponding opcode is guarenteed not
1.239 - ** to grow the stack when it is executed. Otherwise, it may grow the
1.240 - ** stack by at most one entry.
1.241 - **
1.242 - ** NOPUSH_MASK_0 corresponds to opcodes 0 to 15. NOPUSH_MASK_1 contains
1.243 - ** one bit for opcodes 16 to 31, and so on.
1.244 - **
1.245 - ** 16-bit bitmasks (rather than 32-bit) are specified in opcodes.h
1.246 - ** because the file is generated by an awk program. Awk manipulates
1.247 - ** all numbers as floating-point and we don't want to risk a rounding
1.248 - ** error if someone builds with an awk that uses (for example) 32-bit
1.249 - ** IEEE floats.
1.250 - */
1.251 - static const u32 masks[5] = {
1.252 - NOPUSH_MASK_0 + (((unsigned)NOPUSH_MASK_1)<<16),
1.253 - NOPUSH_MASK_2 + (((unsigned)NOPUSH_MASK_3)<<16),
1.254 - NOPUSH_MASK_4 + (((unsigned)NOPUSH_MASK_5)<<16),
1.255 - NOPUSH_MASK_6 + (((unsigned)NOPUSH_MASK_7)<<16),
1.256 - NOPUSH_MASK_8 + (((unsigned)NOPUSH_MASK_9)<<16)
1.257 - };
1.258 - assert( op<32*5 );
1.259 - return (masks[op>>5] & (1<<(op&0x1F)));
1.260 -}
1.261 -
1.262 -#ifndef NDEBUG
1.263 -int sqlite3VdbeOpcodeNoPush(u8 op){
1.264 - return opcodeNoPush(op);
1.265 -}
1.266 -#endif
1.267 -
1.268 -/*
1.269 -** Loop through the program looking for P2 values that are negative.
1.270 -** Each such value is a label. Resolve the label by setting the P2
1.271 -** value to its correct non-zero value.
1.272 -**
1.273 -** This routine is called once after all opcodes have been inserted.
1.274 -**
1.275 -** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
1.276 -** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
1.277 -** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
1.278 -**
1.279 -** The integer *pMaxStack is set to the maximum number of vdbe stack
1.280 -** entries that static analysis reveals this program might need.
1.281 -**
1.282 -** This routine also does the following optimization: It scans for
1.283 -** Halt instructions where P1==SQLITE_CONSTRAINT or P2==OE_Abort or for
1.284 -** IdxInsert instructions where P2!=0. If no such instruction is
1.285 -** found, then every Statement instruction is changed to a Noop. In
1.286 -** this way, we avoid creating the statement journal file unnecessarily.
1.287 -*/
1.288 -static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs, int *pMaxStack){
1.289 - int i;
1.290 - int nMaxArgs = 0;
1.291 - int nMaxStack = p->nOp;
1.292 - Op *pOp;
1.293 - int *aLabel = p->aLabel;
1.294 - int doesStatementRollback = 0;
1.295 - int hasStatementBegin = 0;
1.296 - for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
1.297 - u8 opcode = pOp->opcode;
1.298 -
1.299 - if( opcode==OP_Function || opcode==OP_AggStep
1.300 -#ifndef SQLITE_OMIT_VIRTUALTABLE
1.301 - || opcode==OP_VUpdate
1.302 -#endif
1.303 - ){
1.304 - if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
1.305 - }
1.306 - if( opcode==OP_Halt ){
1.307 - if( pOp->p1==SQLITE_CONSTRAINT && pOp->p2==OE_Abort ){
1.308 - doesStatementRollback = 1;
1.309 - }
1.310 - }else if( opcode==OP_Statement ){
1.311 - hasStatementBegin = 1;
1.312 -#ifndef SQLITE_OMIT_VIRTUALTABLE
1.313 - }else if( opcode==OP_VUpdate || opcode==OP_VRename ){
1.314 - doesStatementRollback = 1;
1.315 - }else if( opcode==OP_VFilter ){
1.316 - int n;
1.317 - assert( p->nOp - i >= 3 );
1.318 - assert( pOp[-2].opcode==OP_Integer );
1.319 - n = pOp[-2].p1;
1.320 - if( n>nMaxArgs ) nMaxArgs = n;
1.321 -#endif
1.322 - }
1.323 - if( opcodeNoPush(opcode) ){
1.324 - nMaxStack--;
1.325 - }
1.326 -
1.327 - if( pOp->p2>=0 ) continue;
1.328 - assert( -1-pOp->p2<p->nLabel );
1.329 - pOp->p2 = aLabel[-1-pOp->p2];
1.330 - }
1.331 - sqlite3_free(p->aLabel);
1.332 - p->aLabel = 0;
1.333 -
1.334 - *pMaxFuncArgs = nMaxArgs;
1.335 - *pMaxStack = nMaxStack;
1.336 -
1.337 - /* If we never rollback a statement transaction, then statement
1.338 - ** transactions are not needed. So change every OP_Statement
1.339 - ** opcode into an OP_Noop. This avoid a call to sqlite3OsOpenExclusive()
1.340 - ** which can be expensive on some platforms.
1.341 - */
1.342 - if( hasStatementBegin && !doesStatementRollback ){
1.343 - for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
1.344 - if( pOp->opcode==OP_Statement ){
1.345 - pOp->opcode = OP_Noop;
1.346 - }
1.347 - }
1.348 - }
1.349 -}
1.350 -
1.351 -/*
1.352 -** Return the address of the next instruction to be inserted.
1.353 -*/
1.354 -int sqlite3VdbeCurrentAddr(Vdbe *p){
1.355 - assert( p->magic==VDBE_MAGIC_INIT );
1.356 - return p->nOp;
1.357 -}
1.358 -
1.359 -/*
1.360 -** Add a whole list of operations to the operation stack. Return the
1.361 -** address of the first operation added.
1.362 -*/
1.363 -int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
1.364 - int addr;
1.365 - assert( p->magic==VDBE_MAGIC_INIT );
1.366 - resizeOpArray(p, p->nOp + nOp);
1.367 - if( p->db->mallocFailed ){
1.368 - return 0;
1.369 - }
1.370 - addr = p->nOp;
1.371 - if( nOp>0 ){
1.372 - int i;
1.373 - VdbeOpList const *pIn = aOp;
1.374 - for(i=0; i<nOp; i++, pIn++){
1.375 - int p2 = pIn->p2;
1.376 - VdbeOp *pOut = &p->aOp[i+addr];
1.377 - pOut->opcode = pIn->opcode;
1.378 - pOut->p1 = pIn->p1;
1.379 - pOut->p2 = p2<0 ? addr + ADDR(p2) : p2;
1.380 - pOut->p3 = pIn->p3;
1.381 - pOut->p3type = pIn->p3 ? P3_STATIC : P3_NOTUSED;
1.382 -#ifdef SQLITE_DEBUG
1.383 - if( sqlite3_vdbe_addop_trace ){
1.384 - sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
1.385 - }
1.386 -#endif
1.387 - }
1.388 - p->nOp += nOp;
1.389 - }
1.390 - return addr;
1.391 -}
1.392 -
1.393 -/*
1.394 -** Change the value of the P1 operand for a specific instruction.
1.395 -** This routine is useful when a large program is loaded from a
1.396 -** static array using sqlite3VdbeAddOpList but we want to make a
1.397 -** few minor changes to the program.
1.398 -*/
1.399 -void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
1.400 - assert( p==0 || p->magic==VDBE_MAGIC_INIT );
1.401 - if( p && addr>=0 && p->nOp>addr && p->aOp ){
1.402 - p->aOp[addr].p1 = val;
1.403 - }
1.404 -}
1.405 -
1.406 -/*
1.407 -** Change the value of the P2 operand for a specific instruction.
1.408 -** This routine is useful for setting a jump destination.
1.409 -*/
1.410 -void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
1.411 - assert( val>=0 );
1.412 - assert( p==0 || p->magic==VDBE_MAGIC_INIT );
1.413 - if( p && addr>=0 && p->nOp>addr && p->aOp ){
1.414 - p->aOp[addr].p2 = val;
1.415 - }
1.416 -}
1.417 -
1.418 -/*
1.419 -** Change the P2 operand of instruction addr so that it points to
1.420 -** the address of the next instruction to be coded.
1.421 -*/
1.422 -void sqlite3VdbeJumpHere(Vdbe *p, int addr){
1.423 - sqlite3VdbeChangeP2(p, addr, p->nOp);
1.424 -}
1.425 -
1.426 -
1.427 -/*
1.428 -** If the input FuncDef structure is ephemeral, then free it. If
1.429 -** the FuncDef is not ephermal, then do nothing.
1.430 -*/
1.431 -static void freeEphemeralFunction(FuncDef *pDef){
1.432 - if( pDef && (pDef->flags & SQLITE_FUNC_EPHEM)!=0 ){
1.433 - sqlite3_free(pDef);
1.434 - }
1.435 -}
1.436 -
1.437 -/*
1.438 -** Delete a P3 value if necessary.
1.439 -*/
1.440 -static void freeP3(int p3type, void *p3){
1.441 - if( p3 ){
1.442 - switch( p3type ){
1.443 - case P3_DYNAMIC:
1.444 - case P3_KEYINFO:
1.445 - case P3_KEYINFO_HANDOFF: {
1.446 - sqlite3_free(p3);
1.447 - break;
1.448 - }
1.449 - case P3_MPRINTF: {
1.450 - sqlite3_free(p3);
1.451 - break;
1.452 - }
1.453 - case P3_VDBEFUNC: {
1.454 - VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
1.455 - freeEphemeralFunction(pVdbeFunc->pFunc);
1.456 - sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
1.457 - sqlite3_free(pVdbeFunc);
1.458 - break;
1.459 - }
1.460 - case P3_FUNCDEF: {
1.461 - freeEphemeralFunction((FuncDef*)p3);
1.462 - break;
1.463 - }
1.464 - case P3_MEM: {
1.465 - sqlite3ValueFree((sqlite3_value*)p3);
1.466 - break;
1.467 - }
1.468 - }
1.469 - }
1.470 -}
1.471 -
1.472 -
1.473 -/*
1.474 -** Change N opcodes starting at addr to No-ops.
1.475 -*/
1.476 -void sqlite3VdbeChangeToNoop(Vdbe *p, int addr, int N){
1.477 - if( p && p->aOp ){
1.478 - VdbeOp *pOp = &p->aOp[addr];
1.479 - while( N-- ){
1.480 - freeP3(pOp->p3type, pOp->p3);
1.481 - memset(pOp, 0, sizeof(pOp[0]));
1.482 - pOp->opcode = OP_Noop;
1.483 - pOp++;
1.484 - }
1.485 - }
1.486 -}
1.487 -
1.488 -/*
1.489 -** Change the value of the P3 operand for a specific instruction.
1.490 -** This routine is useful when a large program is loaded from a
1.491 -** static array using sqlite3VdbeAddOpList but we want to make a
1.492 -** few minor changes to the program.
1.493 -**
1.494 -** If n>=0 then the P3 operand is dynamic, meaning that a copy of
1.495 -** the string is made into memory obtained from sqlite3_malloc().
1.496 -** A value of n==0 means copy bytes of zP3 up to and including the
1.497 -** first null byte. If n>0 then copy n+1 bytes of zP3.
1.498 -**
1.499 -** If n==P3_KEYINFO it means that zP3 is a pointer to a KeyInfo structure.
1.500 -** A copy is made of the KeyInfo structure into memory obtained from
1.501 -** sqlite3_malloc, to be freed when the Vdbe is finalized.
1.502 -** n==P3_KEYINFO_HANDOFF indicates that zP3 points to a KeyInfo structure
1.503 -** stored in memory that the caller has obtained from sqlite3_malloc. The
1.504 -** caller should not free the allocation, it will be freed when the Vdbe is
1.505 -** finalized.
1.506 -**
1.507 -** Other values of n (P3_STATIC, P3_COLLSEQ etc.) indicate that zP3 points
1.508 -** to a string or structure that is guaranteed to exist for the lifetime of
1.509 -** the Vdbe. In these cases we can just copy the pointer.
1.510 -**
1.511 -** If addr<0 then change P3 on the most recently inserted instruction.
1.512 -*/
1.513 -void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
1.514 - Op *pOp;
1.515 - assert( p==0 || p->magic==VDBE_MAGIC_INIT );
1.516 - if( p==0 || p->aOp==0 || p->db->mallocFailed ){
1.517 - if (n != P3_KEYINFO) {
1.518 - freeP3(n, (void*)*(char**)&zP3);
1.519 - }
1.520 - return;
1.521 - }
1.522 - if( addr<0 || addr>=p->nOp ){
1.523 - addr = p->nOp - 1;
1.524 - if( addr<0 ) return;
1.525 - }
1.526 - pOp = &p->aOp[addr];
1.527 - freeP3(pOp->p3type, pOp->p3);
1.528 - pOp->p3 = 0;
1.529 - if( zP3==0 ){
1.530 - pOp->p3 = 0;
1.531 - pOp->p3type = P3_NOTUSED;
1.532 - }else if( n==P3_KEYINFO ){
1.533 - KeyInfo *pKeyInfo;
1.534 - int nField, nByte;
1.535 -
1.536 - nField = ((KeyInfo*)zP3)->nField;
1.537 - nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
1.538 - pKeyInfo = sqlite3_malloc( nByte );
1.539 - pOp->p3 = (char*)pKeyInfo;
1.540 - if( pKeyInfo ){
1.541 - unsigned char *aSortOrder;
1.542 - memcpy(pKeyInfo, zP3, nByte);
1.543 - aSortOrder = pKeyInfo->aSortOrder;
1.544 - if( aSortOrder ){
1.545 - pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
1.546 - memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
1.547 - }
1.548 - pOp->p3type = P3_KEYINFO;
1.549 - }else{
1.550 - p->db->mallocFailed = 1;
1.551 - pOp->p3type = P3_NOTUSED;
1.552 - }
1.553 - }else if( n==P3_KEYINFO_HANDOFF ){
1.554 - pOp->p3 = (char*)zP3;
1.555 - pOp->p3type = P3_KEYINFO;
1.556 - }else if( n<0 ){
1.557 - pOp->p3 = (char*)zP3;
1.558 - pOp->p3type = n;
1.559 - }else{
1.560 - if( n==0 ) n = strlen(zP3);
1.561 - pOp->p3 = sqlite3DbStrNDup(p->db, zP3, n);
1.562 - pOp->p3type = P3_DYNAMIC;
1.563 - }
1.564 -}
1.565 -
1.566 -#ifndef NDEBUG
1.567 -/*
1.568 -** Replace the P3 field of the most recently coded instruction with
1.569 -** comment text.
1.570 -*/
1.571 -void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
1.572 - va_list ap;
1.573 - assert( p->nOp>0 || p->aOp==0 );
1.574 - assert( p->aOp==0 || p->aOp[p->nOp-1].p3==0 || p->db->mallocFailed );
1.575 - va_start(ap, zFormat);
1.576 - sqlite3VdbeChangeP3(p, -1, sqlite3VMPrintf(p->db, zFormat, ap), P3_DYNAMIC);
1.577 - va_end(ap);
1.578 -}
1.579 -#endif
1.580 -
1.581 -/*
1.582 -** Return the opcode for a given address.
1.583 -*/
1.584 -VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
1.585 - assert( p->magic==VDBE_MAGIC_INIT );
1.586 - assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
1.587 - return ((addr>=0 && addr<p->nOp)?(&p->aOp[addr]):0);
1.588 -}
1.589 -
1.590 -#if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
1.591 - || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1.592 -/*
1.593 -** Compute a string that describes the P3 parameter for an opcode.
1.594 -** Use zTemp for any required temporary buffer space.
1.595 -*/
1.596 -static char *displayP3(Op *pOp, char *zTemp, int nTemp){
1.597 - char *zP3;
1.598 - assert( nTemp>=20 );
1.599 - switch( pOp->p3type ){
1.600 - case P3_KEYINFO: {
1.601 - int i, j;
1.602 - KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
1.603 - sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
1.604 - i = strlen(zTemp);
1.605 - for(j=0; j<pKeyInfo->nField; j++){
1.606 - CollSeq *pColl = pKeyInfo->aColl[j];
1.607 - if( pColl ){
1.608 - int n = strlen(pColl->zName);
1.609 - if( i+n>nTemp-6 ){
1.610 - memcpy(&zTemp[i],",...",4);
1.611 - break;
1.612 - }
1.613 - zTemp[i++] = ',';
1.614 - if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
1.615 - zTemp[i++] = '-';
1.616 - }
1.617 - memcpy(&zTemp[i], pColl->zName,n+1);
1.618 - i += n;
1.619 - }else if( i+4<nTemp-6 ){
1.620 - memcpy(&zTemp[i],",nil",4);
1.621 - i += 4;
1.622 - }
1.623 - }
1.624 - zTemp[i++] = ')';
1.625 - zTemp[i] = 0;
1.626 - assert( i<nTemp );
1.627 - zP3 = zTemp;
1.628 - break;
1.629 - }
1.630 - case P3_COLLSEQ: {
1.631 - CollSeq *pColl = (CollSeq*)pOp->p3;
1.632 - sqlite3_snprintf(nTemp, zTemp, "collseq(%.20s)", pColl->zName);
1.633 - zP3 = zTemp;
1.634 - break;
1.635 - }
1.636 - case P3_FUNCDEF: {
1.637 - FuncDef *pDef = (FuncDef*)pOp->p3;
1.638 - sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
1.639 - zP3 = zTemp;
1.640 - break;
1.641 - }
1.642 -#ifndef SQLITE_OMIT_VIRTUALTABLE
1.643 - case P3_VTAB: {
1.644 - sqlite3_vtab *pVtab = (sqlite3_vtab*)pOp->p3;
1.645 - sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
1.646 - zP3 = zTemp;
1.647 - break;
1.648 - }
1.649 -#endif
1.650 - default: {
1.651 - zP3 = pOp->p3;
1.652 - if( zP3==0 || pOp->opcode==OP_Noop ){
1.653 - zP3 = "";
1.654 - }
1.655 - }
1.656 - }
1.657 - assert( zP3!=0 );
1.658 - return zP3;
1.659 -}
1.660 -#endif
1.661 -
1.662 -/*
1.663 -** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
1.664 -**
1.665 -*/
1.666 -void sqlite3VdbeUsesBtree(Vdbe *p, int i){
1.667 - int mask;
1.668 - assert( i>=0 && i<p->db->nDb );
1.669 - assert( i<sizeof(p->btreeMask)*8 );
1.670 - mask = 1<<i;
1.671 - if( (p->btreeMask & mask)==0 ){
1.672 - p->btreeMask |= mask;
1.673 - sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);
1.674 - }
1.675 -}
1.676 -
1.677 -
1.678 -#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
1.679 -/*
1.680 -** Print a single opcode. This routine is used for debugging only.
1.681 -*/
1.682 -void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
1.683 - char *zP3;
1.684 - char zPtr[50];
1.685 - static const char *zFormat1 = "%4d %-13s %4d %4d %s\n";
1.686 - if( pOut==0 ) pOut = stdout;
1.687 - zP3 = displayP3(pOp, zPtr, sizeof(zPtr));
1.688 - fprintf(pOut, zFormat1,
1.689 - pc, sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, zP3);
1.690 - fflush(pOut);
1.691 -}
1.692 -#endif
1.693 -
1.694 -/*
1.695 -** Release an array of N Mem elements
1.696 -*/
1.697 -static void releaseMemArray(Mem *p, int N){
1.698 - if( p ){
1.699 - while( N-->0 ){
1.700 - assert( N<2 || p[0].db==p[1].db );
1.701 - sqlite3VdbeMemRelease(p++);
1.702 - }
1.703 - }
1.704 -}
1.705 -
1.706 -#ifndef SQLITE_OMIT_EXPLAIN
1.707 -/*
1.708 -** Give a listing of the program in the virtual machine.
1.709 -**
1.710 -** The interface is the same as sqlite3VdbeExec(). But instead of
1.711 -** running the code, it invokes the callback once for each instruction.
1.712 -** This feature is used to implement "EXPLAIN".
1.713 -*/
1.714 -int sqlite3VdbeList(
1.715 - Vdbe *p /* The VDBE */
1.716 -){
1.717 - sqlite3 *db = p->db;
1.718 - int i;
1.719 - int rc = SQLITE_OK;
1.720 -
1.721 - assert( p->explain );
1.722 - if( p->magic!=VDBE_MAGIC_RUN ) return SQLITE_MISUSE;
1.723 - assert( db->magic==SQLITE_MAGIC_BUSY );
1.724 - assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
1.725 -
1.726 - /* Even though this opcode does not put dynamic strings onto the
1.727 - ** the stack, they may become dynamic if the user calls
1.728 - ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
1.729 - */
1.730 - if( p->pTos==&p->aStack[4] ){
1.731 - releaseMemArray(p->aStack, 5);
1.732 - }
1.733 - p->resOnStack = 0;
1.734 -
1.735 - do{
1.736 - i = p->pc++;
1.737 - }while( i<p->nOp && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
1.738 - if( i>=p->nOp ){
1.739 - p->rc = SQLITE_OK;
1.740 - rc = SQLITE_DONE;
1.741 - }else if( db->u1.isInterrupted ){
1.742 - p->rc = SQLITE_INTERRUPT;
1.743 - rc = SQLITE_ERROR;
1.744 - sqlite3SetString(&p->zErrMsg, sqlite3ErrStr(p->rc), (char*)0);
1.745 - }else{
1.746 - Op *pOp = &p->aOp[i];
1.747 - Mem *pMem = p->aStack;
1.748 - pMem->flags = MEM_Int;
1.749 - pMem->type = SQLITE_INTEGER;
1.750 - pMem->u.i = i; /* Program counter */
1.751 - pMem++;
1.752 -
1.753 - pMem->flags = MEM_Static|MEM_Str|MEM_Term;
1.754 - pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
1.755 - assert( pMem->z!=0 );
1.756 - pMem->n = strlen(pMem->z);
1.757 - pMem->type = SQLITE_TEXT;
1.758 - pMem->enc = SQLITE_UTF8;
1.759 - pMem++;
1.760 -
1.761 - pMem->flags = MEM_Int;
1.762 - pMem->u.i = pOp->p1; /* P1 */
1.763 - pMem->type = SQLITE_INTEGER;
1.764 - pMem++;
1.765 -
1.766 - pMem->flags = MEM_Int;
1.767 - pMem->u.i = pOp->p2; /* P2 */
1.768 - pMem->type = SQLITE_INTEGER;
1.769 - pMem++;
1.770 -
1.771 - pMem->flags = MEM_Ephem|MEM_Str|MEM_Term; /* P3 */
1.772 - pMem->z = displayP3(pOp, pMem->zShort, sizeof(pMem->zShort));
1.773 - assert( pMem->z!=0 );
1.774 - pMem->n = strlen(pMem->z);
1.775 - pMem->type = SQLITE_TEXT;
1.776 - pMem->enc = SQLITE_UTF8;
1.777 -
1.778 - p->nResColumn = 5 - 2*(p->explain-1);
1.779 - p->pTos = pMem;
1.780 - p->rc = SQLITE_OK;
1.781 - p->resOnStack = 1;
1.782 - rc = SQLITE_ROW;
1.783 - }
1.784 - return rc;
1.785 -}
1.786 -#endif /* SQLITE_OMIT_EXPLAIN */
1.787 -
1.788 -#ifdef SQLITE_DEBUG
1.789 -/*
1.790 -** Print the SQL that was used to generate a VDBE program.
1.791 -*/
1.792 -void sqlite3VdbePrintSql(Vdbe *p){
1.793 - int nOp = p->nOp;
1.794 - VdbeOp *pOp;
1.795 - if( nOp<1 ) return;
1.796 - pOp = &p->aOp[nOp-1];
1.797 - if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
1.798 - const char *z = pOp->p3;
1.799 - while( isspace(*(u8*)z) ) z++;
1.800 - printf("SQL: [%s]\n", z);
1.801 - }
1.802 -}
1.803 -#endif
1.804 -
1.805 -#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
1.806 -/*
1.807 -** Print an IOTRACE message showing SQL content.
1.808 -*/
1.809 -void sqlite3VdbeIOTraceSql(Vdbe *p){
1.810 - int nOp = p->nOp;
1.811 - VdbeOp *pOp;
1.812 - if( sqlite3_io_trace==0 ) return;
1.813 - if( nOp<1 ) return;
1.814 - pOp = &p->aOp[nOp-1];
1.815 - if( pOp->opcode==OP_Noop && pOp->p3!=0 ){
1.816 - int i, j;
1.817 - char z[1000];
1.818 - sqlite3_snprintf(sizeof(z), z, "%s", pOp->p3);
1.819 - for(i=0; isspace((unsigned char)z[i]); i++){}
1.820 - for(j=0; z[i]; i++){
1.821 - if( isspace((unsigned char)z[i]) ){
1.822 - if( z[i-1]!=' ' ){
1.823 - z[j++] = ' ';
1.824 - }
1.825 - }else{
1.826 - z[j++] = z[i];
1.827 - }
1.828 - }
1.829 - z[j] = 0;
1.830 - sqlite3_io_trace("SQL %s\n", z);
1.831 - }
1.832 -}
1.833 -#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
1.834 -
1.835 -
1.836 -/*
1.837 -** Prepare a virtual machine for execution. This involves things such
1.838 -** as allocating stack space and initializing the program counter.
1.839 -** After the VDBE has be prepped, it can be executed by one or more
1.840 -** calls to sqlite3VdbeExec().
1.841 -**
1.842 -** This is the only way to move a VDBE from VDBE_MAGIC_INIT to
1.843 -** VDBE_MAGIC_RUN.
1.844 -*/
1.845 -void sqlite3VdbeMakeReady(
1.846 - Vdbe *p, /* The VDBE */
1.847 - int nVar, /* Number of '?' see in the SQL statement */
1.848 - int nMem, /* Number of memory cells to allocate */
1.849 - int nCursor, /* Number of cursors to allocate */
1.850 - int isExplain /* True if the EXPLAIN keywords is present */
1.851 -){
1.852 - int n;
1.853 - sqlite3 *db = p->db;
1.854 -
1.855 - assert( p!=0 );
1.856 - assert( p->magic==VDBE_MAGIC_INIT );
1.857 -
1.858 - /* There should be at least one opcode.
1.859 - */
1.860 - assert( p->nOp>0 );
1.861 -
1.862 - /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. This
1.863 - * is because the call to resizeOpArray() below may shrink the
1.864 - * p->aOp[] array to save memory if called when in VDBE_MAGIC_RUN
1.865 - * state.
1.866 - */
1.867 - p->magic = VDBE_MAGIC_RUN;
1.868 -
1.869 - /* No instruction ever pushes more than a single element onto the
1.870 - ** stack. And the stack never grows on successive executions of the
1.871 - ** same loop. So the total number of instructions is an upper bound
1.872 - ** on the maximum stack depth required. (Added later:) The
1.873 - ** resolveP2Values() call computes a tighter upper bound on the
1.874 - ** stack size.
1.875 - **
1.876 - ** Allocation all the stack space we will ever need.
1.877 - */
1.878 - if( p->aStack==0 ){
1.879 - int nArg; /* Maximum number of args passed to a user function. */
1.880 - int nStack; /* Maximum number of stack entries required */
1.881 - resolveP2Values(p, &nArg, &nStack);
1.882 - resizeOpArray(p, p->nOp);
1.883 - assert( nVar>=0 );
1.884 - assert( nStack<p->nOp );
1.885 - if( isExplain ){
1.886 - nStack = 10;
1.887 - }
1.888 - p->aStack = sqlite3DbMallocZero(db,
1.889 - nStack*sizeof(p->aStack[0]) /* aStack */
1.890 - + nArg*sizeof(Mem*) /* apArg */
1.891 - + nVar*sizeof(Mem) /* aVar */
1.892 - + nVar*sizeof(char*) /* azVar */
1.893 - + nMem*sizeof(Mem) /* aMem */
1.894 - + nCursor*sizeof(Cursor*) /* apCsr */
1.895 - );
1.896 - if( !db->mallocFailed ){
1.897 - p->aMem = &p->aStack[nStack];
1.898 - p->nMem = nMem;
1.899 - p->aVar = &p->aMem[nMem];
1.900 - p->nVar = nVar;
1.901 - p->okVar = 0;
1.902 - p->apArg = (Mem**)&p->aVar[nVar];
1.903 - p->azVar = (char**)&p->apArg[nArg];
1.904 - p->apCsr = (Cursor**)&p->azVar[nVar];
1.905 - p->nCursor = nCursor;
1.906 - for(n=0; n<nVar; n++){
1.907 - p->aVar[n].flags = MEM_Null;
1.908 - p->aVar[n].db = db;
1.909 - }
1.910 - for(n=0; n<nStack; n++){
1.911 - p->aStack[n].db = db;
1.912 - }
1.913 - }
1.914 - }
1.915 - for(n=0; n<p->nMem; n++){
1.916 - p->aMem[n].flags = MEM_Null;
1.917 - p->aMem[n].db = db;
1.918 - }
1.919 -
1.920 - p->pTos = &p->aStack[-1];
1.921 - p->pc = -1;
1.922 - p->rc = SQLITE_OK;
1.923 - p->uniqueCnt = 0;
1.924 - p->returnDepth = 0;
1.925 - p->errorAction = OE_Abort;
1.926 - p->popStack = 0;
1.927 - p->explain |= isExplain;
1.928 - p->magic = VDBE_MAGIC_RUN;
1.929 - p->nChange = 0;
1.930 - p->cacheCtr = 1;
1.931 - p->minWriteFileFormat = 255;
1.932 - p->openedStatement = 0;
1.933 -#ifdef VDBE_PROFILE
1.934 - {
1.935 - int i;
1.936 - for(i=0; i<p->nOp; i++){
1.937 - p->aOp[i].cnt = 0;
1.938 - p->aOp[i].cycles = 0;
1.939 - }
1.940 - }
1.941 -#endif
1.942 -}
1.943 -
1.944 -/*
1.945 -** Close a VDBE cursor and release all the resources that cursor happens
1.946 -** to hold.
1.947 -*/
1.948 -void sqlite3VdbeFreeCursor(Vdbe *p, Cursor *pCx){
1.949 - if( pCx==0 ){
1.950 - return;
1.951 - }
1.952 - if( pCx->pCursor ){
1.953 - sqlite3BtreeCloseCursor(pCx->pCursor);
1.954 - }
1.955 - if( pCx->pBt ){
1.956 - sqlite3BtreeClose(pCx->pBt);
1.957 - }
1.958 -#ifndef SQLITE_OMIT_VIRTUALTABLE
1.959 - if( pCx->pVtabCursor ){
1.960 - sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
1.961 - const sqlite3_module *pModule = pCx->pModule;
1.962 - p->inVtabMethod = 1;
1.963 - sqlite3SafetyOff(p->db);
1.964 - pModule->xClose(pVtabCursor);
1.965 - sqlite3SafetyOn(p->db);
1.966 - p->inVtabMethod = 0;
1.967 - }
1.968 -#endif
1.969 - sqlite3_free(pCx->pData);
1.970 - sqlite3_free(pCx->aType);
1.971 - sqlite3_free(pCx);
1.972 -}
1.973 -
1.974 -/*
1.975 -** Close all cursors except for VTab cursors that are currently
1.976 -** in use.
1.977 -*/
1.978 -static void closeAllCursorsExceptActiveVtabs(Vdbe *p){
1.979 - int i;
1.980 - if( p->apCsr==0 ) return;
1.981 - for(i=0; i<p->nCursor; i++){
1.982 - Cursor *pC = p->apCsr[i];
1.983 - if( pC && (!p->inVtabMethod || !pC->pVtabCursor) ){
1.984 - sqlite3VdbeFreeCursor(p, pC);
1.985 - p->apCsr[i] = 0;
1.986 - }
1.987 - }
1.988 -}
1.989 -
1.990 -/*
1.991 -** Clean up the VM after execution.
1.992 -**
1.993 -** This routine will automatically close any cursors, lists, and/or
1.994 -** sorters that were left open. It also deletes the values of
1.995 -** variables in the aVar[] array.
1.996 -*/
1.997 -static void Cleanup(Vdbe *p){
1.998 - int i;
1.999 - if( p->aStack ){
1.1000 - releaseMemArray(p->aStack, 1 + (p->pTos - p->aStack));
1.1001 - p->pTos = &p->aStack[-1];
1.1002 - }
1.1003 - closeAllCursorsExceptActiveVtabs(p);
1.1004 - releaseMemArray(p->aMem, p->nMem);
1.1005 - sqlite3VdbeFifoClear(&p->sFifo);
1.1006 - if( p->contextStack ){
1.1007 - for(i=0; i<p->contextStackTop; i++){
1.1008 - sqlite3VdbeFifoClear(&p->contextStack[i].sFifo);
1.1009 - }
1.1010 - sqlite3_free(p->contextStack);
1.1011 - }
1.1012 - p->contextStack = 0;
1.1013 - p->contextStackDepth = 0;
1.1014 - p->contextStackTop = 0;
1.1015 - sqlite3_free(p->zErrMsg);
1.1016 - p->zErrMsg = 0;
1.1017 - p->resOnStack = 0;
1.1018 -}
1.1019 -
1.1020 -/*
1.1021 -** Set the number of result columns that will be returned by this SQL
1.1022 -** statement. This is now set at compile time, rather than during
1.1023 -** execution of the vdbe program so that sqlite3_column_count() can
1.1024 -** be called on an SQL statement before sqlite3_step().
1.1025 -*/
1.1026 -void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
1.1027 - Mem *pColName;
1.1028 - int n;
1.1029 -
1.1030 - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1.1031 - sqlite3_free(p->aColName);
1.1032 - n = nResColumn*COLNAME_N;
1.1033 - p->nResColumn = nResColumn;
1.1034 - p->aColName = pColName = (Mem*)sqlite3DbMallocZero(p->db, sizeof(Mem)*n );
1.1035 - if( p->aColName==0 ) return;
1.1036 - while( n-- > 0 ){
1.1037 - pColName->flags = MEM_Null;
1.1038 - pColName->db = p->db;
1.1039 - pColName++;
1.1040 - }
1.1041 -}
1.1042 -
1.1043 -/*
1.1044 -** Set the name of the idx'th column to be returned by the SQL statement.
1.1045 -** zName must be a pointer to a nul terminated string.
1.1046 -**
1.1047 -** This call must be made after a call to sqlite3VdbeSetNumCols().
1.1048 -**
1.1049 -** If N==P3_STATIC it means that zName is a pointer to a constant static
1.1050 -** string and we can just copy the pointer. If it is P3_DYNAMIC, then
1.1051 -** the string is freed using sqlite3_free() when the vdbe is finished with
1.1052 -** it. Otherwise, N bytes of zName are copied.
1.1053 -*/
1.1054 -int sqlite3VdbeSetColName(Vdbe *p, int idx, int var, const char *zName, int N){
1.1055 - int rc;
1.1056 - Mem *pColName;
1.1057 - assert( idx<p->nResColumn );
1.1058 - assert( var<COLNAME_N );
1.1059 - if( p->db->mallocFailed ) return SQLITE_NOMEM;
1.1060 - assert( p->aColName!=0 );
1.1061 - pColName = &(p->aColName[idx+var*p->nResColumn]);
1.1062 - if( N==P3_DYNAMIC || N==P3_STATIC ){
1.1063 - rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, SQLITE_STATIC);
1.1064 - }else{
1.1065 - rc = sqlite3VdbeMemSetStr(pColName, zName, N, SQLITE_UTF8,SQLITE_TRANSIENT);
1.1066 - }
1.1067 - if( rc==SQLITE_OK && N==P3_DYNAMIC ){
1.1068 - pColName->flags = (pColName->flags&(~MEM_Static))|MEM_Dyn;
1.1069 - pColName->xDel = 0;
1.1070 - }
1.1071 - return rc;
1.1072 -}
1.1073 -
1.1074 -/*
1.1075 -** A read or write transaction may or may not be active on database handle
1.1076 -** db. If a transaction is active, commit it. If there is a
1.1077 -** write-transaction spanning more than one database file, this routine
1.1078 -** takes care of the master journal trickery.
1.1079 -*/
1.1080 -static int vdbeCommit(sqlite3 *db){
1.1081 - int i;
1.1082 - int nTrans = 0; /* Number of databases with an active write-transaction */
1.1083 - int rc = SQLITE_OK;
1.1084 - int needXcommit = 0;
1.1085 -
1.1086 - /* Before doing anything else, call the xSync() callback for any
1.1087 - ** virtual module tables written in this transaction. This has to
1.1088 - ** be done before determining whether a master journal file is
1.1089 - ** required, as an xSync() callback may add an attached database
1.1090 - ** to the transaction.
1.1091 - */
1.1092 - rc = sqlite3VtabSync(db, rc);
1.1093 - if( rc!=SQLITE_OK ){
1.1094 - return rc;
1.1095 - }
1.1096 -
1.1097 - /* This loop determines (a) if the commit hook should be invoked and
1.1098 - ** (b) how many database files have open write transactions, not
1.1099 - ** including the temp database. (b) is important because if more than
1.1100 - ** one database file has an open write transaction, a master journal
1.1101 - ** file is required for an atomic commit.
1.1102 - */
1.1103 - for(i=0; i<db->nDb; i++){
1.1104 - Btree *pBt = db->aDb[i].pBt;
1.1105 - if( sqlite3BtreeIsInTrans(pBt) ){
1.1106 - needXcommit = 1;
1.1107 - if( i!=1 ) nTrans++;
1.1108 - }
1.1109 - }
1.1110 -
1.1111 - /* If there are any write-transactions at all, invoke the commit hook */
1.1112 - if( needXcommit && db->xCommitCallback ){
1.1113 - sqlite3SafetyOff(db);
1.1114 - rc = db->xCommitCallback(db->pCommitArg);
1.1115 - sqlite3SafetyOn(db);
1.1116 - if( rc ){
1.1117 - return SQLITE_CONSTRAINT;
1.1118 - }
1.1119 - }
1.1120 -
1.1121 - /* The simple case - no more than one database file (not counting the
1.1122 - ** TEMP database) has a transaction active. There is no need for the
1.1123 - ** master-journal.
1.1124 - **
1.1125 - ** If the return value of sqlite3BtreeGetFilename() is a zero length
1.1126 - ** string, it means the main database is :memory:. In that case we do
1.1127 - ** not support atomic multi-file commits, so use the simple case then
1.1128 - ** too.
1.1129 - */
1.1130 - if( 0==strlen(sqlite3BtreeGetFilename(db->aDb[0].pBt)) || nTrans<=1 ){
1.1131 - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1.1132 - Btree *pBt = db->aDb[i].pBt;
1.1133 - if( pBt ){
1.1134 - rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
1.1135 - }
1.1136 - }
1.1137 -
1.1138 - /* Do the commit only if all databases successfully complete phase 1.
1.1139 - ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
1.1140 - ** IO error while deleting or truncating a journal file. It is unlikely,
1.1141 - ** but could happen. In this case abandon processing and return the error.
1.1142 - */
1.1143 - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1.1144 - Btree *pBt = db->aDb[i].pBt;
1.1145 - if( pBt ){
1.1146 - rc = sqlite3BtreeCommitPhaseTwo(pBt);
1.1147 - }
1.1148 - }
1.1149 - if( rc==SQLITE_OK ){
1.1150 - sqlite3VtabCommit(db);
1.1151 - }
1.1152 - }
1.1153 -
1.1154 - /* The complex case - There is a multi-file write-transaction active.
1.1155 - ** This requires a master journal file to ensure the transaction is
1.1156 - ** committed atomicly.
1.1157 - */
1.1158 -#ifndef SQLITE_OMIT_DISKIO
1.1159 - else{
1.1160 - sqlite3_vfs *pVfs = db->pVfs;
1.1161 - int needSync = 0;
1.1162 - char *zMaster = 0; /* File-name for the master journal */
1.1163 - char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
1.1164 - sqlite3_file *pMaster = 0;
1.1165 - i64 offset = 0;
1.1166 -
1.1167 - /* Select a master journal file name */
1.1168 - do {
1.1169 - u32 random;
1.1170 - sqlite3_free(zMaster);
1.1171 - sqlite3Randomness(sizeof(random), &random);
1.1172 - zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, random&0x7fffffff);
1.1173 - if( !zMaster ){
1.1174 - return SQLITE_NOMEM;
1.1175 - }
1.1176 - }while( sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS) );
1.1177 -
1.1178 - /* Open the master journal. */
1.1179 - rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
1.1180 - SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1.1181 - SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
1.1182 - );
1.1183 - if( rc!=SQLITE_OK ){
1.1184 - sqlite3_free(zMaster);
1.1185 - return rc;
1.1186 - }
1.1187 -
1.1188 - /* Write the name of each database file in the transaction into the new
1.1189 - ** master journal file. If an error occurs at this point close
1.1190 - ** and delete the master journal file. All the individual journal files
1.1191 - ** still have 'null' as the master journal pointer, so they will roll
1.1192 - ** back independently if a failure occurs.
1.1193 - */
1.1194 - for(i=0; i<db->nDb; i++){
1.1195 - Btree *pBt = db->aDb[i].pBt;
1.1196 - if( i==1 ) continue; /* Ignore the TEMP database */
1.1197 - if( sqlite3BtreeIsInTrans(pBt) ){
1.1198 - char const *zFile = sqlite3BtreeGetJournalname(pBt);
1.1199 - if( zFile[0]==0 ) continue; /* Ignore :memory: databases */
1.1200 - if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
1.1201 - needSync = 1;
1.1202 - }
1.1203 - rc = sqlite3OsWrite(pMaster, zFile, strlen(zFile)+1, offset);
1.1204 - offset += strlen(zFile)+1;
1.1205 - if( rc!=SQLITE_OK ){
1.1206 - sqlite3OsCloseFree(pMaster);
1.1207 - sqlite3OsDelete(pVfs, zMaster, 0);
1.1208 - sqlite3_free(zMaster);
1.1209 - return rc;
1.1210 - }
1.1211 - }
1.1212 - }
1.1213 -
1.1214 - /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
1.1215 - ** flag is set this is not required.
1.1216 - */
1.1217 - zMainFile = sqlite3BtreeGetDirname(db->aDb[0].pBt);
1.1218 - if( (needSync
1.1219 - && (0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL))
1.1220 - && (rc=sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))!=SQLITE_OK) ){
1.1221 - sqlite3OsCloseFree(pMaster);
1.1222 - sqlite3OsDelete(pVfs, zMaster, 0);
1.1223 - sqlite3_free(zMaster);
1.1224 - return rc;
1.1225 - }
1.1226 -
1.1227 - /* Sync all the db files involved in the transaction. The same call
1.1228 - ** sets the master journal pointer in each individual journal. If
1.1229 - ** an error occurs here, do not delete the master journal file.
1.1230 - **
1.1231 - ** If the error occurs during the first call to
1.1232 - ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
1.1233 - ** master journal file will be orphaned. But we cannot delete it,
1.1234 - ** in case the master journal file name was written into the journal
1.1235 - ** file before the failure occured.
1.1236 - */
1.1237 - for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
1.1238 - Btree *pBt = db->aDb[i].pBt;
1.1239 - if( pBt ){
1.1240 - rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
1.1241 - }
1.1242 - }
1.1243 - sqlite3OsCloseFree(pMaster);
1.1244 - if( rc!=SQLITE_OK ){
1.1245 - sqlite3_free(zMaster);
1.1246 - return rc;
1.1247 - }
1.1248 -
1.1249 - /* Delete the master journal file. This commits the transaction. After
1.1250 - ** doing this the directory is synced again before any individual
1.1251 - ** transaction files are deleted.
1.1252 - */
1.1253 - rc = sqlite3OsDelete(pVfs, zMaster, 1);
1.1254 - sqlite3_free(zMaster);
1.1255 - zMaster = 0;
1.1256 - if( rc ){
1.1257 - return rc;
1.1258 - }
1.1259 -
1.1260 - /* All files and directories have already been synced, so the following
1.1261 - ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
1.1262 - ** deleting or truncating journals. If something goes wrong while
1.1263 - ** this is happening we don't really care. The integrity of the
1.1264 - ** transaction is already guaranteed, but some stray 'cold' journals
1.1265 - ** may be lying around. Returning an error code won't help matters.
1.1266 - */
1.1267 - disable_simulated_io_errors();
1.1268 - for(i=0; i<db->nDb; i++){
1.1269 - Btree *pBt = db->aDb[i].pBt;
1.1270 - if( pBt ){
1.1271 - sqlite3BtreeCommitPhaseTwo(pBt);
1.1272 - }
1.1273 - }
1.1274 - enable_simulated_io_errors();
1.1275 -
1.1276 - sqlite3VtabCommit(db);
1.1277 - }
1.1278 -#endif
1.1279 -
1.1280 - return rc;
1.1281 -}
1.1282 -
1.1283 -/*
1.1284 -** This routine checks that the sqlite3.activeVdbeCnt count variable
1.1285 -** matches the number of vdbe's in the list sqlite3.pVdbe that are
1.1286 -** currently active. An assertion fails if the two counts do not match.
1.1287 -** This is an internal self-check only - it is not an essential processing
1.1288 -** step.
1.1289 -**
1.1290 -** This is a no-op if NDEBUG is defined.
1.1291 -*/
1.1292 -#ifndef NDEBUG
1.1293 -static void checkActiveVdbeCnt(sqlite3 *db){
1.1294 - Vdbe *p;
1.1295 - int cnt = 0;
1.1296 - p = db->pVdbe;
1.1297 - while( p ){
1.1298 - if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
1.1299 - cnt++;
1.1300 - }
1.1301 - p = p->pNext;
1.1302 - }
1.1303 - assert( cnt==db->activeVdbeCnt );
1.1304 -}
1.1305 -#else
1.1306 -#define checkActiveVdbeCnt(x)
1.1307 -#endif
1.1308 -
1.1309 -/*
1.1310 -** For every Btree that in database connection db which
1.1311 -** has been modified, "trip" or invalidate each cursor in
1.1312 -** that Btree might have been modified so that the cursor
1.1313 -** can never be used again. This happens when a rollback
1.1314 -*** occurs. We have to trip all the other cursors, even
1.1315 -** cursor from other VMs in different database connections,
1.1316 -** so that none of them try to use the data at which they
1.1317 -** were pointing and which now may have been changed due
1.1318 -** to the rollback.
1.1319 -**
1.1320 -** Remember that a rollback can delete tables complete and
1.1321 -** reorder rootpages. So it is not sufficient just to save
1.1322 -** the state of the cursor. We have to invalidate the cursor
1.1323 -** so that it is never used again.
1.1324 -*/
1.1325 -void invalidateCursorsOnModifiedBtrees(sqlite3 *db){
1.1326 - int i;
1.1327 - for(i=0; i<db->nDb; i++){
1.1328 - Btree *p = db->aDb[i].pBt;
1.1329 - if( p && sqlite3BtreeIsInTrans(p) ){
1.1330 - sqlite3BtreeTripAllCursors(p, SQLITE_ABORT);
1.1331 - }
1.1332 - }
1.1333 -}
1.1334 -
1.1335 -/*
1.1336 -** This routine is called the when a VDBE tries to halt. If the VDBE
1.1337 -** has made changes and is in autocommit mode, then commit those
1.1338 -** changes. If a rollback is needed, then do the rollback.
1.1339 -**
1.1340 -** This routine is the only way to move the state of a VM from
1.1341 -** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to
1.1342 -** call this on a VM that is in the SQLITE_MAGIC_HALT state.
1.1343 -**
1.1344 -** Return an error code. If the commit could not complete because of
1.1345 -** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it
1.1346 -** means the close did not happen and needs to be repeated.
1.1347 -*/
1.1348 -int sqlite3VdbeHalt(Vdbe *p){
1.1349 - sqlite3 *db = p->db;
1.1350 - int i;
1.1351 - int (*xFunc)(Btree *pBt) = 0; /* Function to call on each btree backend */
1.1352 - int isSpecialError; /* Set to true if SQLITE_NOMEM or IOERR */
1.1353 -
1.1354 - /* This function contains the logic that determines if a statement or
1.1355 - ** transaction will be committed or rolled back as a result of the
1.1356 - ** execution of this virtual machine.
1.1357 - **
1.1358 - ** If any of the following errors occur:
1.1359 - **
1.1360 - ** SQLITE_NOMEM
1.1361 - ** SQLITE_IOERR
1.1362 - ** SQLITE_FULL
1.1363 - ** SQLITE_INTERRUPT
1.1364 - **
1.1365 - ** Then the internal cache might have been left in an inconsistent
1.1366 - ** state. We need to rollback the statement transaction, if there is
1.1367 - ** one, or the complete transaction if there is no statement transaction.
1.1368 - */
1.1369 -
1.1370 - if( p->db->mallocFailed ){
1.1371 - p->rc = SQLITE_NOMEM;
1.1372 - }
1.1373 - closeAllCursorsExceptActiveVtabs(p);
1.1374 - if( p->magic!=VDBE_MAGIC_RUN ){
1.1375 - return SQLITE_OK;
1.1376 - }
1.1377 - checkActiveVdbeCnt(db);
1.1378 -
1.1379 - /* No commit or rollback needed if the program never started */
1.1380 - if( p->pc>=0 ){
1.1381 - int mrc; /* Primary error code from p->rc */
1.1382 -
1.1383 - /* Lock all btrees used by the statement */
1.1384 - sqlite3BtreeMutexArrayEnter(&p->aMutex);
1.1385 -
1.1386 - /* Check for one of the special errors */
1.1387 - mrc = p->rc & 0xff;
1.1388 - isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
1.1389 - || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL ;
1.1390 - if( isSpecialError ){
1.1391 - /* This loop does static analysis of the query to see which of the
1.1392 - ** following three categories it falls into:
1.1393 - **
1.1394 - ** Read-only
1.1395 - ** Query with statement journal
1.1396 - ** Query without statement journal
1.1397 - **
1.1398 - ** We could do something more elegant than this static analysis (i.e.
1.1399 - ** store the type of query as part of the compliation phase), but
1.1400 - ** handling malloc() or IO failure is a fairly obscure edge case so
1.1401 - ** this is probably easier. Todo: Might be an opportunity to reduce
1.1402 - ** code size a very small amount though...
1.1403 - */
1.1404 - int notReadOnly = 0;
1.1405 - int isStatement = 0;
1.1406 - assert(p->aOp || p->nOp==0);
1.1407 - for(i=0; i<p->nOp; i++){
1.1408 - switch( p->aOp[i].opcode ){
1.1409 - case OP_Transaction:
1.1410 - notReadOnly |= p->aOp[i].p2;
1.1411 - break;
1.1412 - case OP_Statement:
1.1413 - isStatement = 1;
1.1414 - break;
1.1415 - }
1.1416 - }
1.1417 -
1.1418 -
1.1419 - /* If the query was read-only, we need do no rollback at all. Otherwise,
1.1420 - ** proceed with the special handling.
1.1421 - */
1.1422 - if( notReadOnly || mrc!=SQLITE_INTERRUPT ){
1.1423 - if( p->rc==SQLITE_IOERR_BLOCKED && isStatement ){
1.1424 - xFunc = sqlite3BtreeRollbackStmt;
1.1425 - p->rc = SQLITE_BUSY;
1.1426 - } else if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && isStatement ){
1.1427 - xFunc = sqlite3BtreeRollbackStmt;
1.1428 - }else{
1.1429 - /* We are forced to roll back the active transaction. Before doing
1.1430 - ** so, abort any other statements this handle currently has active.
1.1431 - */
1.1432 - invalidateCursorsOnModifiedBtrees(db);
1.1433 - sqlite3RollbackAll(db);
1.1434 - db->autoCommit = 1;
1.1435 - }
1.1436 - }
1.1437 - }
1.1438 -
1.1439 - /* If the auto-commit flag is set and this is the only active vdbe, then
1.1440 - ** we do either a commit or rollback of the current transaction.
1.1441 - **
1.1442 - ** Note: This block also runs if one of the special errors handled
1.1443 - ** above has occured.
1.1444 - */
1.1445 - if( db->autoCommit && db->activeVdbeCnt==1 ){
1.1446 - if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
1.1447 - /* The auto-commit flag is true, and the vdbe program was
1.1448 - ** successful or hit an 'OR FAIL' constraint. This means a commit
1.1449 - ** is required.
1.1450 - */
1.1451 - int rc = vdbeCommit(db);
1.1452 - if( rc==SQLITE_BUSY ){
1.1453 - sqlite3BtreeMutexArrayLeave(&p->aMutex);
1.1454 - return SQLITE_BUSY;
1.1455 - }else if( rc!=SQLITE_OK ){
1.1456 - p->rc = rc;
1.1457 - sqlite3RollbackAll(db);
1.1458 - }else{
1.1459 - sqlite3CommitInternalChanges(db);
1.1460 - }
1.1461 - }else{
1.1462 - sqlite3RollbackAll(db);
1.1463 - }
1.1464 - }else if( !xFunc ){
1.1465 - if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
1.1466 - if( p->openedStatement ){
1.1467 - xFunc = sqlite3BtreeCommitStmt;
1.1468 - }
1.1469 - }else if( p->errorAction==OE_Abort ){
1.1470 - xFunc = sqlite3BtreeRollbackStmt;
1.1471 - }else{
1.1472 - invalidateCursorsOnModifiedBtrees(db);
1.1473 - sqlite3RollbackAll(db);
1.1474 - db->autoCommit = 1;
1.1475 - }
1.1476 - }
1.1477 -
1.1478 - /* If xFunc is not NULL, then it is one of sqlite3BtreeRollbackStmt or
1.1479 - ** sqlite3BtreeCommitStmt. Call it once on each backend. If an error occurs
1.1480 - ** and the return code is still SQLITE_OK, set the return code to the new
1.1481 - ** error value.
1.1482 - */
1.1483 - assert(!xFunc ||
1.1484 - xFunc==sqlite3BtreeCommitStmt ||
1.1485 - xFunc==sqlite3BtreeRollbackStmt
1.1486 - );
1.1487 - for(i=0; xFunc && i<db->nDb; i++){
1.1488 - int rc;
1.1489 - Btree *pBt = db->aDb[i].pBt;
1.1490 - if( pBt ){
1.1491 - rc = xFunc(pBt);
1.1492 - if( rc && (p->rc==SQLITE_OK || p->rc==SQLITE_CONSTRAINT) ){
1.1493 - p->rc = rc;
1.1494 - sqlite3SetString(&p->zErrMsg, 0);
1.1495 - }
1.1496 - }
1.1497 - }
1.1498 -
1.1499 - /* If this was an INSERT, UPDATE or DELETE and the statement was committed,
1.1500 - ** set the change counter.
1.1501 - */
1.1502 - if( p->changeCntOn && p->pc>=0 ){
1.1503 - if( !xFunc || xFunc==sqlite3BtreeCommitStmt ){
1.1504 - sqlite3VdbeSetChanges(db, p->nChange);
1.1505 - }else{
1.1506 - sqlite3VdbeSetChanges(db, 0);
1.1507 - }
1.1508 - p->nChange = 0;
1.1509 - }
1.1510 -
1.1511 - /* Rollback or commit any schema changes that occurred. */
1.1512 - if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
1.1513 - sqlite3ResetInternalSchema(db, 0);
1.1514 - db->flags = (db->flags | SQLITE_InternChanges);
1.1515 - }
1.1516 -
1.1517 - /* Release the locks */
1.1518 - sqlite3BtreeMutexArrayLeave(&p->aMutex);
1.1519 - }
1.1520 -
1.1521 - /* We have successfully halted and closed the VM. Record this fact. */
1.1522 - if( p->pc>=0 ){
1.1523 - db->activeVdbeCnt--;
1.1524 - }
1.1525 - p->magic = VDBE_MAGIC_HALT;
1.1526 - checkActiveVdbeCnt(db);
1.1527 - if( p->db->mallocFailed ){
1.1528 - p->rc = SQLITE_NOMEM;
1.1529 - }
1.1530 - checkActiveVdbeCnt(db);
1.1531 -
1.1532 - return SQLITE_OK;
1.1533 -}
1.1534 -
1.1535 -
1.1536 -/*
1.1537 -** Each VDBE holds the result of the most recent sqlite3_step() call
1.1538 -** in p->rc. This routine sets that result back to SQLITE_OK.
1.1539 -*/
1.1540 -void sqlite3VdbeResetStepResult(Vdbe *p){
1.1541 - p->rc = SQLITE_OK;
1.1542 -}
1.1543 -
1.1544 -/*
1.1545 -** Clean up a VDBE after execution but do not delete the VDBE just yet.
1.1546 -** Write any error messages into *pzErrMsg. Return the result code.
1.1547 -**
1.1548 -** After this routine is run, the VDBE should be ready to be executed
1.1549 -** again.
1.1550 -**
1.1551 -** To look at it another way, this routine resets the state of the
1.1552 -** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
1.1553 -** VDBE_MAGIC_INIT.
1.1554 -*/
1.1555 -int sqlite3VdbeReset(Vdbe *p){
1.1556 - sqlite3 *db;
1.1557 - db = p->db;
1.1558 -
1.1559 - /* If the VM did not run to completion or if it encountered an
1.1560 - ** error, then it might not have been halted properly. So halt
1.1561 - ** it now.
1.1562 - */
1.1563 - sqlite3SafetyOn(db);
1.1564 - sqlite3VdbeHalt(p);
1.1565 - sqlite3SafetyOff(db);
1.1566 -
1.1567 - /* If the VDBE has be run even partially, then transfer the error code
1.1568 - ** and error message from the VDBE into the main database structure. But
1.1569 - ** if the VDBE has just been set to run but has not actually executed any
1.1570 - ** instructions yet, leave the main database error information unchanged.
1.1571 - */
1.1572 - if( p->pc>=0 ){
1.1573 - if( p->zErrMsg ){
1.1574 - sqlite3ValueSetStr(db->pErr,-1,p->zErrMsg,SQLITE_UTF8,sqlite3_free);
1.1575 - db->errCode = p->rc;
1.1576 - p->zErrMsg = 0;
1.1577 - }else if( p->rc ){
1.1578 - sqlite3Error(db, p->rc, 0);
1.1579 - }else{
1.1580 - sqlite3Error(db, SQLITE_OK, 0);
1.1581 - }
1.1582 - }else if( p->rc && p->expired ){
1.1583 - /* The expired flag was set on the VDBE before the first call
1.1584 - ** to sqlite3_step(). For consistency (since sqlite3_step() was
1.1585 - ** called), set the database error in this case as well.
1.1586 - */
1.1587 - sqlite3Error(db, p->rc, 0);
1.1588 - }
1.1589 -
1.1590 - /* Reclaim all memory used by the VDBE
1.1591 - */
1.1592 - Cleanup(p);
1.1593 -
1.1594 - /* Save profiling information from this VDBE run.
1.1595 - */
1.1596 - assert( p->pTos<&p->aStack[p->pc<0?0:p->pc] || !p->aStack );
1.1597 -#ifdef VDBE_PROFILE
1.1598 - {
1.1599 - FILE *out = fopen("vdbe_profile.out", "a");
1.1600 - if( out ){
1.1601 - int i;
1.1602 - fprintf(out, "---- ");
1.1603 - for(i=0; i<p->nOp; i++){
1.1604 - fprintf(out, "%02x", p->aOp[i].opcode);
1.1605 - }
1.1606 - fprintf(out, "\n");
1.1607 - for(i=0; i<p->nOp; i++){
1.1608 - fprintf(out, "%6d %10lld %8lld ",
1.1609 - p->aOp[i].cnt,
1.1610 - p->aOp[i].cycles,
1.1611 - p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
1.1612 - );
1.1613 - sqlite3VdbePrintOp(out, i, &p->aOp[i]);
1.1614 - }
1.1615 - fclose(out);
1.1616 - }
1.1617 - }
1.1618 -#endif
1.1619 - p->magic = VDBE_MAGIC_INIT;
1.1620 - p->aborted = 0;
1.1621 - return p->rc & db->errMask;
1.1622 -}
1.1623 -
1.1624 -/*
1.1625 -** Clean up and delete a VDBE after execution. Return an integer which is
1.1626 -** the result code. Write any error message text into *pzErrMsg.
1.1627 -*/
1.1628 -int sqlite3VdbeFinalize(Vdbe *p){
1.1629 - int rc = SQLITE_OK;
1.1630 - if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
1.1631 - rc = sqlite3VdbeReset(p);
1.1632 - assert( (rc & p->db->errMask)==rc );
1.1633 - }else if( p->magic!=VDBE_MAGIC_INIT ){
1.1634 - return SQLITE_MISUSE;
1.1635 - }
1.1636 - sqlite3VdbeDelete(p);
1.1637 - return rc;
1.1638 -}
1.1639 -
1.1640 -/*
1.1641 -** Call the destructor for each auxdata entry in pVdbeFunc for which
1.1642 -** the corresponding bit in mask is clear. Auxdata entries beyond 31
1.1643 -** are always destroyed. To destroy all auxdata entries, call this
1.1644 -** routine with mask==0.
1.1645 -*/
1.1646 -void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
1.1647 - int i;
1.1648 - for(i=0; i<pVdbeFunc->nAux; i++){
1.1649 - struct AuxData *pAux = &pVdbeFunc->apAux[i];
1.1650 - if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
1.1651 - if( pAux->xDelete ){
1.1652 - pAux->xDelete(pAux->pAux);
1.1653 - }
1.1654 - pAux->pAux = 0;
1.1655 - }
1.1656 - }
1.1657 -}
1.1658 -
1.1659 -/*
1.1660 -** Delete an entire VDBE.
1.1661 -*/
1.1662 -void sqlite3VdbeDelete(Vdbe *p){
1.1663 - int i;
1.1664 - if( p==0 ) return;
1.1665 - Cleanup(p);
1.1666 - if( p->pPrev ){
1.1667 - p->pPrev->pNext = p->pNext;
1.1668 - }else{
1.1669 - assert( p->db->pVdbe==p );
1.1670 - p->db->pVdbe = p->pNext;
1.1671 - }
1.1672 - if( p->pNext ){
1.1673 - p->pNext->pPrev = p->pPrev;
1.1674 - }
1.1675 - if( p->aOp ){
1.1676 - for(i=0; i<p->nOp; i++){
1.1677 - Op *pOp = &p->aOp[i];
1.1678 - freeP3(pOp->p3type, pOp->p3);
1.1679 - }
1.1680 - sqlite3_free(p->aOp);
1.1681 - }
1.1682 - releaseMemArray(p->aVar, p->nVar);
1.1683 - sqlite3_free(p->aLabel);
1.1684 - sqlite3_free(p->aStack);
1.1685 - releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
1.1686 - sqlite3_free(p->aColName);
1.1687 - sqlite3_free(p->zSql);
1.1688 - p->magic = VDBE_MAGIC_DEAD;
1.1689 - sqlite3_free(p);
1.1690 -}
1.1691 -
1.1692 -/*
1.1693 -** If a MoveTo operation is pending on the given cursor, then do that
1.1694 -** MoveTo now. Return an error code. If no MoveTo is pending, this
1.1695 -** routine does nothing and returns SQLITE_OK.
1.1696 -*/
1.1697 -int sqlite3VdbeCursorMoveto(Cursor *p){
1.1698 - if( p->deferredMoveto ){
1.1699 - int res, rc;
1.1700 -#ifdef SQLITE_TEST
1.1701 - extern int sqlite3_search_count;
1.1702 -#endif
1.1703 - assert( p->isTable );
1.1704 - rc = sqlite3BtreeMoveto(p->pCursor, 0, p->movetoTarget, 0, &res);
1.1705 - if( rc ) return rc;
1.1706 - *p->pIncrKey = 0;
1.1707 - p->lastRowid = keyToInt(p->movetoTarget);
1.1708 - p->rowidIsValid = res==0;
1.1709 - if( res<0 ){
1.1710 - rc = sqlite3BtreeNext(p->pCursor, &res);
1.1711 - if( rc ) return rc;
1.1712 - }
1.1713 -#ifdef SQLITE_TEST
1.1714 - sqlite3_search_count++;
1.1715 -#endif
1.1716 - p->deferredMoveto = 0;
1.1717 - p->cacheStatus = CACHE_STALE;
1.1718 - }
1.1719 - return SQLITE_OK;
1.1720 -}
1.1721 -
1.1722 -/*
1.1723 -** The following functions:
1.1724 -**
1.1725 -** sqlite3VdbeSerialType()
1.1726 -** sqlite3VdbeSerialTypeLen()
1.1727 -** sqlite3VdbeSerialRead()
1.1728 -** sqlite3VdbeSerialLen()
1.1729 -** sqlite3VdbeSerialWrite()
1.1730 -**
1.1731 -** encapsulate the code that serializes values for storage in SQLite
1.1732 -** data and index records. Each serialized value consists of a
1.1733 -** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
1.1734 -** integer, stored as a varint.
1.1735 -**
1.1736 -** In an SQLite index record, the serial type is stored directly before
1.1737 -** the blob of data that it corresponds to. In a table record, all serial
1.1738 -** types are stored at the start of the record, and the blobs of data at
1.1739 -** the end. Hence these functions allow the caller to handle the
1.1740 -** serial-type and data blob seperately.
1.1741 -**
1.1742 -** The following table describes the various storage classes for data:
1.1743 -**
1.1744 -** serial type bytes of data type
1.1745 -** -------------- --------------- ---------------
1.1746 -** 0 0 NULL
1.1747 -** 1 1 signed integer
1.1748 -** 2 2 signed integer
1.1749 -** 3 3 signed integer
1.1750 -** 4 4 signed integer
1.1751 -** 5 6 signed integer
1.1752 -** 6 8 signed integer
1.1753 -** 7 8 IEEE float
1.1754 -** 8 0 Integer constant 0
1.1755 -** 9 0 Integer constant 1
1.1756 -** 10,11 reserved for expansion
1.1757 -** N>=12 and even (N-12)/2 BLOB
1.1758 -** N>=13 and odd (N-13)/2 text
1.1759 -**
1.1760 -** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions
1.1761 -** of SQLite will not understand those serial types.
1.1762 -*/
1.1763 -
1.1764 -/*
1.1765 -** Return the serial-type for the value stored in pMem.
1.1766 -*/
1.1767 -u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
1.1768 - int flags = pMem->flags;
1.1769 - int n;
1.1770 -
1.1771 - if( flags&MEM_Null ){
1.1772 - return 0;
1.1773 - }
1.1774 - if( flags&MEM_Int ){
1.1775 - /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
1.1776 -# define MAX_6BYTE ((((i64)0x00001000)<<32)-1)
1.1777 - i64 i = pMem->u.i;
1.1778 - u64 u;
1.1779 - if( file_format>=4 && (i&1)==i ){
1.1780 - return 8+i;
1.1781 - }
1.1782 - u = i<0 ? -i : i;
1.1783 - if( u<=127 ) return 1;
1.1784 - if( u<=32767 ) return 2;
1.1785 - if( u<=8388607 ) return 3;
1.1786 - if( u<=2147483647 ) return 4;
1.1787 - if( u<=MAX_6BYTE ) return 5;
1.1788 - return 6;
1.1789 - }
1.1790 - if( flags&MEM_Real ){
1.1791 - return 7;
1.1792 - }
1.1793 - assert( flags&(MEM_Str|MEM_Blob) );
1.1794 - n = pMem->n;
1.1795 - if( flags & MEM_Zero ){
1.1796 - n += pMem->u.i;
1.1797 - }
1.1798 - assert( n>=0 );
1.1799 - return ((n*2) + 12 + ((flags&MEM_Str)!=0));
1.1800 -}
1.1801 -
1.1802 -/*
1.1803 -** Return the length of the data corresponding to the supplied serial-type.
1.1804 -*/
1.1805 -int sqlite3VdbeSerialTypeLen(u32 serial_type){
1.1806 - if( serial_type>=12 ){
1.1807 - return (serial_type-12)/2;
1.1808 - }else{
1.1809 - static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
1.1810 - return aSize[serial_type];
1.1811 - }
1.1812 -}
1.1813 -
1.1814 -/*
1.1815 -** If we are on an architecture with mixed-endian floating
1.1816 -** points (ex: ARM7) then swap the lower 4 bytes with the
1.1817 -** upper 4 bytes. Return the result.
1.1818 -**
1.1819 -** For most architectures, this is a no-op.
1.1820 -**
1.1821 -** (later): It is reported to me that the mixed-endian problem
1.1822 -** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems
1.1823 -** that early versions of GCC stored the two words of a 64-bit
1.1824 -** float in the wrong order. And that error has been propagated
1.1825 -** ever since. The blame is not necessarily with GCC, though.
1.1826 -** GCC might have just copying the problem from a prior compiler.
1.1827 -** I am also told that newer versions of GCC that follow a different
1.1828 -** ABI get the byte order right.
1.1829 -**
1.1830 -** Developers using SQLite on an ARM7 should compile and run their
1.1831 -** application using -DSQLITE_DEBUG=1 at least once. With DEBUG
1.1832 -** enabled, some asserts below will ensure that the byte order of
1.1833 -** floating point values is correct.
1.1834 -**
1.1835 -** (2007-08-30) Frank van Vugt has studied this problem closely
1.1836 -** and has send his findings to the SQLite developers. Frank
1.1837 -** writes that some Linux kernels offer floating point hardware
1.1838 -** emulation that uses only 32-bit mantissas instead of a full
1.1839 -** 48-bits as required by the IEEE standard. (This is the
1.1840 -** CONFIG_FPE_FASTFPE option.) On such systems, floating point
1.1841 -** byte swapping becomes very complicated. To avoid problems,
1.1842 -** the necessary byte swapping is carried out using a 64-bit integer
1.1843 -** rather than a 64-bit float. Frank assures us that the code here
1.1844 -** works for him. We, the developers, have no way to independently
1.1845 -** verify this, but Frank seems to know what he is talking about
1.1846 -** so we trust him.
1.1847 -*/
1.1848 -#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
1.1849 -static u64 floatSwap(u64 in){
1.1850 - union {
1.1851 - u64 r;
1.1852 - u32 i[2];
1.1853 - } u;
1.1854 - u32 t;
1.1855 -
1.1856 - u.r = in;
1.1857 - t = u.i[0];
1.1858 - u.i[0] = u.i[1];
1.1859 - u.i[1] = t;
1.1860 - return u.r;
1.1861 -}
1.1862 -# define swapMixedEndianFloat(X) X = floatSwap(X)
1.1863 -#else
1.1864 -# define swapMixedEndianFloat(X)
1.1865 -#endif
1.1866 -
1.1867 -/*
1.1868 -** Write the serialized data blob for the value stored in pMem into
1.1869 -** buf. It is assumed that the caller has allocated sufficient space.
1.1870 -** Return the number of bytes written.
1.1871 -**
1.1872 -** nBuf is the amount of space left in buf[]. nBuf must always be
1.1873 -** large enough to hold the entire field. Except, if the field is
1.1874 -** a blob with a zero-filled tail, then buf[] might be just the right
1.1875 -** size to hold everything except for the zero-filled tail. If buf[]
1.1876 -** is only big enough to hold the non-zero prefix, then only write that
1.1877 -** prefix into buf[]. But if buf[] is large enough to hold both the
1.1878 -** prefix and the tail then write the prefix and set the tail to all
1.1879 -** zeros.
1.1880 -**
1.1881 -** Return the number of bytes actually written into buf[]. The number
1.1882 -** of bytes in the zero-filled tail is included in the return value only
1.1883 -** if those bytes were zeroed in buf[].
1.1884 -*/
1.1885 -int sqlite3VdbeSerialPut(u8 *buf, int nBuf, Mem *pMem, int file_format){
1.1886 - u32 serial_type = sqlite3VdbeSerialType(pMem, file_format);
1.1887 - int len;
1.1888 -
1.1889 - /* Integer and Real */
1.1890 - if( serial_type<=7 && serial_type>0 ){
1.1891 - u64 v;
1.1892 - int i;
1.1893 - if( serial_type==7 ){
1.1894 - assert( sizeof(v)==sizeof(pMem->r) );
1.1895 - memcpy(&v, &pMem->r, sizeof(v));
1.1896 - swapMixedEndianFloat(v);
1.1897 - }else{
1.1898 - v = pMem->u.i;
1.1899 - }
1.1900 - len = i = sqlite3VdbeSerialTypeLen(serial_type);
1.1901 - assert( len<=nBuf );
1.1902 - while( i-- ){
1.1903 - buf[i] = (v&0xFF);
1.1904 - v >>= 8;
1.1905 - }
1.1906 - return len;
1.1907 - }
1.1908 -
1.1909 - /* String or blob */
1.1910 - if( serial_type>=12 ){
1.1911 - assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.i:0)
1.1912 - == sqlite3VdbeSerialTypeLen(serial_type) );
1.1913 - assert( pMem->n<=nBuf );
1.1914 - len = pMem->n;
1.1915 - memcpy(buf, pMem->z, len);
1.1916 - if( pMem->flags & MEM_Zero ){
1.1917 - len += pMem->u.i;
1.1918 - if( len>nBuf ){
1.1919 - len = nBuf;
1.1920 - }
1.1921 - memset(&buf[pMem->n], 0, len-pMem->n);
1.1922 - }
1.1923 - return len;
1.1924 - }
1.1925 -
1.1926 - /* NULL or constants 0 or 1 */
1.1927 - return 0;
1.1928 -}
1.1929 -
1.1930 -/*
1.1931 -** Deserialize the data blob pointed to by buf as serial type serial_type
1.1932 -** and store the result in pMem. Return the number of bytes read.
1.1933 -*/
1.1934 -int sqlite3VdbeSerialGet(
1.1935 - const unsigned char *buf, /* Buffer to deserialize from */
1.1936 - u32 serial_type, /* Serial type to deserialize */
1.1937 - Mem *pMem /* Memory cell to write value into */
1.1938 -){
1.1939 - switch( serial_type ){
1.1940 - case 10: /* Reserved for future use */
1.1941 - case 11: /* Reserved for future use */
1.1942 - case 0: { /* NULL */
1.1943 - pMem->flags = MEM_Null;
1.1944 - break;
1.1945 - }
1.1946 - case 1: { /* 1-byte signed integer */
1.1947 - pMem->u.i = (signed char)buf[0];
1.1948 - pMem->flags = MEM_Int;
1.1949 - return 1;
1.1950 - }
1.1951 - case 2: { /* 2-byte signed integer */
1.1952 - pMem->u.i = (((signed char)buf[0])<<8) | buf[1];
1.1953 - pMem->flags = MEM_Int;
1.1954 - return 2;
1.1955 - }
1.1956 - case 3: { /* 3-byte signed integer */
1.1957 - pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2];
1.1958 - pMem->flags = MEM_Int;
1.1959 - return 3;
1.1960 - }
1.1961 - case 4: { /* 4-byte signed integer */
1.1962 - pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1.1963 - pMem->flags = MEM_Int;
1.1964 - return 4;
1.1965 - }
1.1966 - case 5: { /* 6-byte signed integer */
1.1967 - u64 x = (((signed char)buf[0])<<8) | buf[1];
1.1968 - u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
1.1969 - x = (x<<32) | y;
1.1970 - pMem->u.i = *(i64*)&x;
1.1971 - pMem->flags = MEM_Int;
1.1972 - return 6;
1.1973 - }
1.1974 - case 6: /* 8-byte signed integer */
1.1975 - case 7: { /* IEEE floating point */
1.1976 - u64 x;
1.1977 - u32 y;
1.1978 -#if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
1.1979 - /* Verify that integers and floating point values use the same
1.1980 - ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
1.1981 - ** defined that 64-bit floating point values really are mixed
1.1982 - ** endian.
1.1983 - */
1.1984 - static const u64 t1 = ((u64)0x3ff00000)<<32;
1.1985 - static const double r1 = 1.0;
1.1986 - u64 t2 = t1;
1.1987 - swapMixedEndianFloat(t2);
1.1988 - assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
1.1989 -#endif
1.1990 -
1.1991 - x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
1.1992 - y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
1.1993 - x = (x<<32) | y;
1.1994 - if( serial_type==6 ){
1.1995 - pMem->u.i = *(i64*)&x;
1.1996 - pMem->flags = MEM_Int;
1.1997 - }else{
1.1998 - assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
1.1999 - swapMixedEndianFloat(x);
1.2000 - memcpy(&pMem->r, &x, sizeof(x));
1.2001 - pMem->flags = MEM_Real;
1.2002 - }
1.2003 - return 8;
1.2004 - }
1.2005 - case 8: /* Integer 0 */
1.2006 - case 9: { /* Integer 1 */
1.2007 - pMem->u.i = serial_type-8;
1.2008 - pMem->flags = MEM_Int;
1.2009 - return 0;
1.2010 - }
1.2011 - default: {
1.2012 - int len = (serial_type-12)/2;
1.2013 - pMem->z = (char *)buf;
1.2014 - pMem->n = len;
1.2015 - pMem->xDel = 0;
1.2016 - if( serial_type&0x01 ){
1.2017 - pMem->flags = MEM_Str | MEM_Ephem;
1.2018 - }else{
1.2019 - pMem->flags = MEM_Blob | MEM_Ephem;
1.2020 - }
1.2021 - return len;
1.2022 - }
1.2023 - }
1.2024 - return 0;
1.2025 -}
1.2026 -
1.2027 -/*
1.2028 -** The header of a record consists of a sequence variable-length integers.
1.2029 -** These integers are almost always small and are encoded as a single byte.
1.2030 -** The following macro takes advantage this fact to provide a fast decode
1.2031 -** of the integers in a record header. It is faster for the common case
1.2032 -** where the integer is a single byte. It is a little slower when the
1.2033 -** integer is two or more bytes. But overall it is faster.
1.2034 -**
1.2035 -** The following expressions are equivalent:
1.2036 -**
1.2037 -** x = sqlite3GetVarint32( A, &B );
1.2038 -**
1.2039 -** x = GetVarint( A, B );
1.2040 -**
1.2041 -*/
1.2042 -#define GetVarint(A,B) ((B = *(A))<=0x7f ? 1 : sqlite3GetVarint32(A, &B))
1.2043 -
1.2044 -/*
1.2045 -** This function compares the two table rows or index records specified by
1.2046 -** {nKey1, pKey1} and {nKey2, pKey2}, returning a negative, zero
1.2047 -** or positive integer if {nKey1, pKey1} is less than, equal to or
1.2048 -** greater than {nKey2, pKey2}. Both Key1 and Key2 must be byte strings
1.2049 -** composed by the OP_MakeRecord opcode of the VDBE.
1.2050 -*/
1.2051 -int sqlite3VdbeRecordCompare(
1.2052 - void *userData,
1.2053 - int nKey1, const void *pKey1,
1.2054 - int nKey2, const void *pKey2
1.2055 -){
1.2056 - KeyInfo *pKeyInfo = (KeyInfo*)userData;
1.2057 - u32 d1, d2; /* Offset into aKey[] of next data element */
1.2058 - u32 idx1, idx2; /* Offset into aKey[] of next header element */
1.2059 - u32 szHdr1, szHdr2; /* Number of bytes in header */
1.2060 - int i = 0;
1.2061 - int nField;
1.2062 - int rc = 0;
1.2063 - const unsigned char *aKey1 = (const unsigned char *)pKey1;
1.2064 - const unsigned char *aKey2 = (const unsigned char *)pKey2;
1.2065 -
1.2066 - Mem mem1;
1.2067 - Mem mem2;
1.2068 - mem1.enc = pKeyInfo->enc;
1.2069 - mem1.db = pKeyInfo->db;
1.2070 - mem2.enc = pKeyInfo->enc;
1.2071 - mem2.db = pKeyInfo->db;
1.2072 -
1.2073 - idx1 = GetVarint(aKey1, szHdr1);
1.2074 - d1 = szHdr1;
1.2075 - idx2 = GetVarint(aKey2, szHdr2);
1.2076 - d2 = szHdr2;
1.2077 - nField = pKeyInfo->nField;
1.2078 - while( idx1<szHdr1 && idx2<szHdr2 ){
1.2079 - u32 serial_type1;
1.2080 - u32 serial_type2;
1.2081 -
1.2082 - /* Read the serial types for the next element in each key. */
1.2083 - idx1 += GetVarint( aKey1+idx1, serial_type1 );
1.2084 - if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
1.2085 - idx2 += GetVarint( aKey2+idx2, serial_type2 );
1.2086 - if( d2>=nKey2 && sqlite3VdbeSerialTypeLen(serial_type2)>0 ) break;
1.2087 -
1.2088 - /* Extract the values to be compared.
1.2089 - */
1.2090 - d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
1.2091 - d2 += sqlite3VdbeSerialGet(&aKey2[d2], serial_type2, &mem2);
1.2092 -
1.2093 - /* Do the comparison
1.2094 - */
1.2095 - rc = sqlite3MemCompare(&mem1, &mem2, i<nField ? pKeyInfo->aColl[i] : 0);
1.2096 - if( mem1.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem1);
1.2097 - if( mem2.flags & MEM_Dyn ) sqlite3VdbeMemRelease(&mem2);
1.2098 - if( rc!=0 ){
1.2099 - break;
1.2100 - }
1.2101 - i++;
1.2102 - }
1.2103 -
1.2104 - /* One of the keys ran out of fields, but all the fields up to that point
1.2105 - ** were equal. If the incrKey flag is true, then the second key is
1.2106 - ** treated as larger.
1.2107 - */
1.2108 - if( rc==0 ){
1.2109 - if( pKeyInfo->incrKey ){
1.2110 - rc = -1;
1.2111 - }else if( d1<nKey1 ){
1.2112 - rc = 1;
1.2113 - }else if( d2<nKey2 ){
1.2114 - rc = -1;
1.2115 - }
1.2116 - }else if( pKeyInfo->aSortOrder && i<pKeyInfo->nField
1.2117 - && pKeyInfo->aSortOrder[i] ){
1.2118 - rc = -rc;
1.2119 - }
1.2120 -
1.2121 - return rc;
1.2122 -}
1.2123 -
1.2124 -/*
1.2125 -** The argument is an index entry composed using the OP_MakeRecord opcode.
1.2126 -** The last entry in this record should be an integer (specifically
1.2127 -** an integer rowid). This routine returns the number of bytes in
1.2128 -** that integer.
1.2129 -*/
1.2130 -int sqlite3VdbeIdxRowidLen(const u8 *aKey){
1.2131 - u32 szHdr; /* Size of the header */
1.2132 - u32 typeRowid; /* Serial type of the rowid */
1.2133 -
1.2134 - sqlite3GetVarint32(aKey, &szHdr);
1.2135 - sqlite3GetVarint32(&aKey[szHdr-1], &typeRowid);
1.2136 - return sqlite3VdbeSerialTypeLen(typeRowid);
1.2137 -}
1.2138 -
1.2139 -
1.2140 -/*
1.2141 -** pCur points at an index entry created using the OP_MakeRecord opcode.
1.2142 -** Read the rowid (the last field in the record) and store it in *rowid.
1.2143 -** Return SQLITE_OK if everything works, or an error code otherwise.
1.2144 -*/
1.2145 -int sqlite3VdbeIdxRowid(BtCursor *pCur, i64 *rowid){
1.2146 - i64 nCellKey = 0;
1.2147 - int rc;
1.2148 - u32 szHdr; /* Size of the header */
1.2149 - u32 typeRowid; /* Serial type of the rowid */
1.2150 - u32 lenRowid; /* Size of the rowid */
1.2151 - Mem m, v;
1.2152 -
1.2153 - sqlite3BtreeKeySize(pCur, &nCellKey);
1.2154 - if( nCellKey<=0 ){
1.2155 - return SQLITE_CORRUPT_BKPT;
1.2156 - }
1.2157 - rc = sqlite3VdbeMemFromBtree(pCur, 0, nCellKey, 1, &m);
1.2158 - if( rc ){
1.2159 - return rc;
1.2160 - }
1.2161 - sqlite3GetVarint32((u8*)m.z, &szHdr);
1.2162 - sqlite3GetVarint32((u8*)&m.z[szHdr-1], &typeRowid);
1.2163 - lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
1.2164 - sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
1.2165 - *rowid = v.u.i;
1.2166 - sqlite3VdbeMemRelease(&m);
1.2167 - return SQLITE_OK;
1.2168 -}
1.2169 -
1.2170 -/*
1.2171 -** Compare the key of the index entry that cursor pC is point to against
1.2172 -** the key string in pKey (of length nKey). Write into *pRes a number
1.2173 -** that is negative, zero, or positive if pC is less than, equal to,
1.2174 -** or greater than pKey. Return SQLITE_OK on success.
1.2175 -**
1.2176 -** pKey is either created without a rowid or is truncated so that it
1.2177 -** omits the rowid at the end. The rowid at the end of the index entry
1.2178 -** is ignored as well.
1.2179 -*/
1.2180 -int sqlite3VdbeIdxKeyCompare(
1.2181 - Cursor *pC, /* The cursor to compare against */
1.2182 - int nKey, const u8 *pKey, /* The key to compare */
1.2183 - int *res /* Write the comparison result here */
1.2184 -){
1.2185 - i64 nCellKey = 0;
1.2186 - int rc;
1.2187 - BtCursor *pCur = pC->pCursor;
1.2188 - int lenRowid;
1.2189 - Mem m;
1.2190 -
1.2191 - sqlite3BtreeKeySize(pCur, &nCellKey);
1.2192 - if( nCellKey<=0 ){
1.2193 - *res = 0;
1.2194 - return SQLITE_OK;
1.2195 - }
1.2196 - rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, nCellKey, 1, &m);
1.2197 - if( rc ){
1.2198 - return rc;
1.2199 - }
1.2200 - lenRowid = sqlite3VdbeIdxRowidLen((u8*)m.z);
1.2201 - *res = sqlite3VdbeRecordCompare(pC->pKeyInfo, m.n-lenRowid, m.z, nKey, pKey);
1.2202 - sqlite3VdbeMemRelease(&m);
1.2203 - return SQLITE_OK;
1.2204 -}
1.2205 -
1.2206 -/*
1.2207 -** This routine sets the value to be returned by subsequent calls to
1.2208 -** sqlite3_changes() on the database handle 'db'.
1.2209 -*/
1.2210 -void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
1.2211 - assert( sqlite3_mutex_held(db->mutex) );
1.2212 - db->nChange = nChange;
1.2213 - db->nTotalChange += nChange;
1.2214 -}
1.2215 -
1.2216 -/*
1.2217 -** Set a flag in the vdbe to update the change counter when it is finalised
1.2218 -** or reset.
1.2219 -*/
1.2220 -void sqlite3VdbeCountChanges(Vdbe *v){
1.2221 - v->changeCntOn = 1;
1.2222 -}
1.2223 -
1.2224 -/*
1.2225 -** Mark every prepared statement associated with a database connection
1.2226 -** as expired.
1.2227 -**
1.2228 -** An expired statement means that recompilation of the statement is
1.2229 -** recommend. Statements expire when things happen that make their
1.2230 -** programs obsolete. Removing user-defined functions or collating
1.2231 -** sequences, or changing an authorization function are the types of
1.2232 -** things that make prepared statements obsolete.
1.2233 -*/
1.2234 -void sqlite3ExpirePreparedStatements(sqlite3 *db){
1.2235 - Vdbe *p;
1.2236 - for(p = db->pVdbe; p; p=p->pNext){
1.2237 - p->expired = 1;
1.2238 - }
1.2239 -}
1.2240 -
1.2241 -/*
1.2242 -** Return the database associated with the Vdbe.
1.2243 -*/
1.2244 -sqlite3 *sqlite3VdbeDb(Vdbe *v){
1.2245 - return v->db;
1.2246 -}