000001 /* 000002 ** 2003 September 6 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** This file contains code used for creating, destroying, and populating 000013 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) 000014 */ 000015 #include "sqliteInt.h" 000016 #include "vdbeInt.h" 000017 000018 /* Forward references */ 000019 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef); 000020 static void vdbeFreeOpArray(sqlite3 *, Op *, int); 000021 000022 /* 000023 ** Create a new virtual database engine. 000024 */ 000025 Vdbe *sqlite3VdbeCreate(Parse *pParse){ 000026 sqlite3 *db = pParse->db; 000027 Vdbe *p; 000028 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) ); 000029 if( p==0 ) return 0; 000030 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)); 000031 p->db = db; 000032 if( db->pVdbe ){ 000033 db->pVdbe->pPrev = p; 000034 } 000035 p->pNext = db->pVdbe; 000036 p->pPrev = 0; 000037 db->pVdbe = p; 000038 p->magic = VDBE_MAGIC_INIT; 000039 p->pParse = pParse; 000040 pParse->pVdbe = p; 000041 assert( pParse->aLabel==0 ); 000042 assert( pParse->nLabel==0 ); 000043 assert( p->nOpAlloc==0 ); 000044 assert( pParse->szOpAlloc==0 ); 000045 sqlite3VdbeAddOp2(p, OP_Init, 0, 1); 000046 return p; 000047 } 000048 000049 /* 000050 ** Return the Parse object that owns a Vdbe object. 000051 */ 000052 Parse *sqlite3VdbeParser(Vdbe *p){ 000053 return p->pParse; 000054 } 000055 000056 /* 000057 ** Change the error string stored in Vdbe.zErrMsg 000058 */ 000059 void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){ 000060 va_list ap; 000061 sqlite3DbFree(p->db, p->zErrMsg); 000062 va_start(ap, zFormat); 000063 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap); 000064 va_end(ap); 000065 } 000066 000067 /* 000068 ** Remember the SQL string for a prepared statement. 000069 */ 000070 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, u8 prepFlags){ 000071 if( p==0 ) return; 000072 p->prepFlags = prepFlags; 000073 if( (prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ 000074 p->expmask = 0; 000075 } 000076 assert( p->zSql==0 ); 000077 p->zSql = sqlite3DbStrNDup(p->db, z, n); 000078 } 000079 000080 #ifdef SQLITE_ENABLE_NORMALIZE 000081 /* 000082 ** Add a new element to the Vdbe->pDblStr list. 000083 */ 000084 void sqlite3VdbeAddDblquoteStr(sqlite3 *db, Vdbe *p, const char *z){ 000085 if( p ){ 000086 int n = sqlite3Strlen30(z); 000087 DblquoteStr *pStr = sqlite3DbMallocRawNN(db, 000088 sizeof(*pStr)+n+1-sizeof(pStr->z)); 000089 if( pStr ){ 000090 pStr->pNextStr = p->pDblStr; 000091 p->pDblStr = pStr; 000092 memcpy(pStr->z, z, n+1); 000093 } 000094 } 000095 } 000096 #endif 000097 000098 #ifdef SQLITE_ENABLE_NORMALIZE 000099 /* 000100 ** zId of length nId is a double-quoted identifier. Check to see if 000101 ** that identifier is really used as a string literal. 000102 */ 000103 int sqlite3VdbeUsesDoubleQuotedString( 000104 Vdbe *pVdbe, /* The prepared statement */ 000105 const char *zId /* The double-quoted identifier, already dequoted */ 000106 ){ 000107 DblquoteStr *pStr; 000108 assert( zId!=0 ); 000109 if( pVdbe->pDblStr==0 ) return 0; 000110 for(pStr=pVdbe->pDblStr; pStr; pStr=pStr->pNextStr){ 000111 if( strcmp(zId, pStr->z)==0 ) return 1; 000112 } 000113 return 0; 000114 } 000115 #endif 000116 000117 /* 000118 ** Swap all content between two VDBE structures. 000119 */ 000120 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ 000121 Vdbe tmp, *pTmp; 000122 char *zTmp; 000123 assert( pA->db==pB->db ); 000124 tmp = *pA; 000125 *pA = *pB; 000126 *pB = tmp; 000127 pTmp = pA->pNext; 000128 pA->pNext = pB->pNext; 000129 pB->pNext = pTmp; 000130 pTmp = pA->pPrev; 000131 pA->pPrev = pB->pPrev; 000132 pB->pPrev = pTmp; 000133 zTmp = pA->zSql; 000134 pA->zSql = pB->zSql; 000135 pB->zSql = zTmp; 000136 #ifdef SQLITE_ENABLE_NORMALIZE 000137 zTmp = pA->zNormSql; 000138 pA->zNormSql = pB->zNormSql; 000139 pB->zNormSql = zTmp; 000140 #endif 000141 pB->expmask = pA->expmask; 000142 pB->prepFlags = pA->prepFlags; 000143 memcpy(pB->aCounter, pA->aCounter, sizeof(pB->aCounter)); 000144 pB->aCounter[SQLITE_STMTSTATUS_REPREPARE]++; 000145 } 000146 000147 /* 000148 ** Resize the Vdbe.aOp array so that it is at least nOp elements larger 000149 ** than its current size. nOp is guaranteed to be less than or equal 000150 ** to 1024/sizeof(Op). 000151 ** 000152 ** If an out-of-memory error occurs while resizing the array, return 000153 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain 000154 ** unchanged (this is so that any opcodes already allocated can be 000155 ** correctly deallocated along with the rest of the Vdbe). 000156 */ 000157 static int growOpArray(Vdbe *v, int nOp){ 000158 VdbeOp *pNew; 000159 Parse *p = v->pParse; 000160 000161 /* The SQLITE_TEST_REALLOC_STRESS compile-time option is designed to force 000162 ** more frequent reallocs and hence provide more opportunities for 000163 ** simulated OOM faults. SQLITE_TEST_REALLOC_STRESS is generally used 000164 ** during testing only. With SQLITE_TEST_REALLOC_STRESS grow the op array 000165 ** by the minimum* amount required until the size reaches 512. Normal 000166 ** operation (without SQLITE_TEST_REALLOC_STRESS) is to double the current 000167 ** size of the op array or add 1KB of space, whichever is smaller. */ 000168 #ifdef SQLITE_TEST_REALLOC_STRESS 000169 sqlite3_int64 nNew = (v->nOpAlloc>=512 ? 2*(sqlite3_int64)v->nOpAlloc 000170 : (sqlite3_int64)v->nOpAlloc+nOp); 000171 #else 000172 sqlite3_int64 nNew = (v->nOpAlloc ? 2*(sqlite3_int64)v->nOpAlloc 000173 : (sqlite3_int64)(1024/sizeof(Op))); 000174 UNUSED_PARAMETER(nOp); 000175 #endif 000176 000177 /* Ensure that the size of a VDBE does not grow too large */ 000178 if( nNew > p->db->aLimit[SQLITE_LIMIT_VDBE_OP] ){ 000179 sqlite3OomFault(p->db); 000180 return SQLITE_NOMEM; 000181 } 000182 000183 assert( nOp<=(1024/sizeof(Op)) ); 000184 assert( nNew>=(v->nOpAlloc+nOp) ); 000185 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); 000186 if( pNew ){ 000187 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); 000188 v->nOpAlloc = p->szOpAlloc/sizeof(Op); 000189 v->aOp = pNew; 000190 } 000191 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT); 000192 } 000193 000194 #ifdef SQLITE_DEBUG 000195 /* This routine is just a convenient place to set a breakpoint that will 000196 ** fire after each opcode is inserted and displayed using 000197 ** "PRAGMA vdbe_addoptrace=on". 000198 */ 000199 static void test_addop_breakpoint(void){ 000200 static int n = 0; 000201 n++; 000202 } 000203 #endif 000204 000205 /* 000206 ** Add a new instruction to the list of instructions current in the 000207 ** VDBE. Return the address of the new instruction. 000208 ** 000209 ** Parameters: 000210 ** 000211 ** p Pointer to the VDBE 000212 ** 000213 ** op The opcode for this instruction 000214 ** 000215 ** p1, p2, p3 Operands 000216 ** 000217 ** Use the sqlite3VdbeResolveLabel() function to fix an address and 000218 ** the sqlite3VdbeChangeP4() function to change the value of the P4 000219 ** operand. 000220 */ 000221 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){ 000222 assert( p->nOpAlloc<=p->nOp ); 000223 if( growOpArray(p, 1) ) return 1; 000224 assert( p->nOpAlloc>p->nOp ); 000225 return sqlite3VdbeAddOp3(p, op, p1, p2, p3); 000226 } 000227 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ 000228 int i; 000229 VdbeOp *pOp; 000230 000231 i = p->nOp; 000232 assert( p->magic==VDBE_MAGIC_INIT ); 000233 assert( op>=0 && op<0xff ); 000234 if( p->nOpAlloc<=i ){ 000235 return growOp3(p, op, p1, p2, p3); 000236 } 000237 p->nOp++; 000238 pOp = &p->aOp[i]; 000239 pOp->opcode = (u8)op; 000240 pOp->p5 = 0; 000241 pOp->p1 = p1; 000242 pOp->p2 = p2; 000243 pOp->p3 = p3; 000244 pOp->p4.p = 0; 000245 pOp->p4type = P4_NOTUSED; 000246 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 000247 pOp->zComment = 0; 000248 #endif 000249 #ifdef SQLITE_DEBUG 000250 if( p->db->flags & SQLITE_VdbeAddopTrace ){ 000251 sqlite3VdbePrintOp(0, i, &p->aOp[i]); 000252 test_addop_breakpoint(); 000253 } 000254 #endif 000255 #ifdef VDBE_PROFILE 000256 pOp->cycles = 0; 000257 pOp->cnt = 0; 000258 #endif 000259 #ifdef SQLITE_VDBE_COVERAGE 000260 pOp->iSrcLine = 0; 000261 #endif 000262 return i; 000263 } 000264 int sqlite3VdbeAddOp0(Vdbe *p, int op){ 000265 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); 000266 } 000267 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ 000268 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); 000269 } 000270 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ 000271 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); 000272 } 000273 000274 /* Generate code for an unconditional jump to instruction iDest 000275 */ 000276 int sqlite3VdbeGoto(Vdbe *p, int iDest){ 000277 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0); 000278 } 000279 000280 /* Generate code to cause the string zStr to be loaded into 000281 ** register iDest 000282 */ 000283 int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){ 000284 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0); 000285 } 000286 000287 /* 000288 ** Generate code that initializes multiple registers to string or integer 000289 ** constants. The registers begin with iDest and increase consecutively. 000290 ** One register is initialized for each characgter in zTypes[]. For each 000291 ** "s" character in zTypes[], the register is a string if the argument is 000292 ** not NULL, or OP_Null if the value is a null pointer. For each "i" character 000293 ** in zTypes[], the register is initialized to an integer. 000294 ** 000295 ** If the input string does not end with "X" then an OP_ResultRow instruction 000296 ** is generated for the values inserted. 000297 */ 000298 void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ 000299 va_list ap; 000300 int i; 000301 char c; 000302 va_start(ap, zTypes); 000303 for(i=0; (c = zTypes[i])!=0; i++){ 000304 if( c=='s' ){ 000305 const char *z = va_arg(ap, const char*); 000306 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest+i, 0, z, 0); 000307 }else if( c=='i' ){ 000308 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest+i); 000309 }else{ 000310 goto skip_op_resultrow; 000311 } 000312 } 000313 sqlite3VdbeAddOp2(p, OP_ResultRow, iDest, i); 000314 skip_op_resultrow: 000315 va_end(ap); 000316 } 000317 000318 /* 000319 ** Add an opcode that includes the p4 value as a pointer. 000320 */ 000321 int sqlite3VdbeAddOp4( 000322 Vdbe *p, /* Add the opcode to this VM */ 000323 int op, /* The new opcode */ 000324 int p1, /* The P1 operand */ 000325 int p2, /* The P2 operand */ 000326 int p3, /* The P3 operand */ 000327 const char *zP4, /* The P4 operand */ 000328 int p4type /* P4 operand type */ 000329 ){ 000330 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); 000331 sqlite3VdbeChangeP4(p, addr, zP4, p4type); 000332 return addr; 000333 } 000334 000335 /* 000336 ** Add an OP_Function or OP_PureFunc opcode. 000337 ** 000338 ** The eCallCtx argument is information (typically taken from Expr.op2) 000339 ** that describes the calling context of the function. 0 means a general 000340 ** function call. NC_IsCheck means called by a check constraint, 000341 ** NC_IdxExpr means called as part of an index expression. NC_PartIdx 000342 ** means in the WHERE clause of a partial index. NC_GenCol means called 000343 ** while computing a generated column value. 0 is the usual case. 000344 */ 000345 int sqlite3VdbeAddFunctionCall( 000346 Parse *pParse, /* Parsing context */ 000347 int p1, /* Constant argument mask */ 000348 int p2, /* First argument register */ 000349 int p3, /* Register into which results are written */ 000350 int nArg, /* Number of argument */ 000351 const FuncDef *pFunc, /* The function to be invoked */ 000352 int eCallCtx /* Calling context */ 000353 ){ 000354 Vdbe *v = pParse->pVdbe; 000355 int nByte; 000356 int addr; 000357 sqlite3_context *pCtx; 000358 assert( v ); 000359 nByte = sizeof(*pCtx) + (nArg-1)*sizeof(sqlite3_value*); 000360 pCtx = sqlite3DbMallocRawNN(pParse->db, nByte); 000361 if( pCtx==0 ){ 000362 assert( pParse->db->mallocFailed ); 000363 freeEphemeralFunction(pParse->db, (FuncDef*)pFunc); 000364 return 0; 000365 } 000366 pCtx->pOut = 0; 000367 pCtx->pFunc = (FuncDef*)pFunc; 000368 pCtx->pVdbe = 0; 000369 pCtx->isError = 0; 000370 pCtx->argc = nArg; 000371 pCtx->iOp = sqlite3VdbeCurrentAddr(v); 000372 addr = sqlite3VdbeAddOp4(v, eCallCtx ? OP_PureFunc : OP_Function, 000373 p1, p2, p3, (char*)pCtx, P4_FUNCCTX); 000374 sqlite3VdbeChangeP5(v, eCallCtx & NC_SelfRef); 000375 return addr; 000376 } 000377 000378 /* 000379 ** Add an opcode that includes the p4 value with a P4_INT64 or 000380 ** P4_REAL type. 000381 */ 000382 int sqlite3VdbeAddOp4Dup8( 000383 Vdbe *p, /* Add the opcode to this VM */ 000384 int op, /* The new opcode */ 000385 int p1, /* The P1 operand */ 000386 int p2, /* The P2 operand */ 000387 int p3, /* The P3 operand */ 000388 const u8 *zP4, /* The P4 operand */ 000389 int p4type /* P4 operand type */ 000390 ){ 000391 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8); 000392 if( p4copy ) memcpy(p4copy, zP4, 8); 000393 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); 000394 } 000395 000396 #ifndef SQLITE_OMIT_EXPLAIN 000397 /* 000398 ** Return the address of the current EXPLAIN QUERY PLAN baseline. 000399 ** 0 means "none". 000400 */ 000401 int sqlite3VdbeExplainParent(Parse *pParse){ 000402 VdbeOp *pOp; 000403 if( pParse->addrExplain==0 ) return 0; 000404 pOp = sqlite3VdbeGetOp(pParse->pVdbe, pParse->addrExplain); 000405 return pOp->p2; 000406 } 000407 000408 /* 000409 ** Set a debugger breakpoint on the following routine in order to 000410 ** monitor the EXPLAIN QUERY PLAN code generation. 000411 */ 000412 #if defined(SQLITE_DEBUG) 000413 void sqlite3ExplainBreakpoint(const char *z1, const char *z2){ 000414 (void)z1; 000415 (void)z2; 000416 } 000417 #endif 000418 000419 /* 000420 ** Add a new OP_ opcode. 000421 ** 000422 ** If the bPush flag is true, then make this opcode the parent for 000423 ** subsequent Explains until sqlite3VdbeExplainPop() is called. 000424 */ 000425 void sqlite3VdbeExplain(Parse *pParse, u8 bPush, const char *zFmt, ...){ 000426 #ifndef SQLITE_DEBUG 000427 /* Always include the OP_Explain opcodes if SQLITE_DEBUG is defined. 000428 ** But omit them (for performance) during production builds */ 000429 if( pParse->explain==2 ) 000430 #endif 000431 { 000432 char *zMsg; 000433 Vdbe *v; 000434 va_list ap; 000435 int iThis; 000436 va_start(ap, zFmt); 000437 zMsg = sqlite3VMPrintf(pParse->db, zFmt, ap); 000438 va_end(ap); 000439 v = pParse->pVdbe; 000440 iThis = v->nOp; 000441 sqlite3VdbeAddOp4(v, OP_Explain, iThis, pParse->addrExplain, 0, 000442 zMsg, P4_DYNAMIC); 000443 sqlite3ExplainBreakpoint(bPush?"PUSH":"", sqlite3VdbeGetOp(v,-1)->p4.z); 000444 if( bPush){ 000445 pParse->addrExplain = iThis; 000446 } 000447 } 000448 } 000449 000450 /* 000451 ** Pop the EXPLAIN QUERY PLAN stack one level. 000452 */ 000453 void sqlite3VdbeExplainPop(Parse *pParse){ 000454 sqlite3ExplainBreakpoint("POP", 0); 000455 pParse->addrExplain = sqlite3VdbeExplainParent(pParse); 000456 } 000457 #endif /* SQLITE_OMIT_EXPLAIN */ 000458 000459 /* 000460 ** Add an OP_ParseSchema opcode. This routine is broken out from 000461 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees 000462 ** as having been used. 000463 ** 000464 ** The zWhere string must have been obtained from sqlite3_malloc(). 000465 ** This routine will take ownership of the allocated memory. 000466 */ 000467 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ 000468 int j; 000469 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); 000470 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); 000471 } 000472 000473 /* 000474 ** Add an opcode that includes the p4 value as an integer. 000475 */ 000476 int sqlite3VdbeAddOp4Int( 000477 Vdbe *p, /* Add the opcode to this VM */ 000478 int op, /* The new opcode */ 000479 int p1, /* The P1 operand */ 000480 int p2, /* The P2 operand */ 000481 int p3, /* The P3 operand */ 000482 int p4 /* The P4 operand as an integer */ 000483 ){ 000484 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); 000485 if( p->db->mallocFailed==0 ){ 000486 VdbeOp *pOp = &p->aOp[addr]; 000487 pOp->p4type = P4_INT32; 000488 pOp->p4.i = p4; 000489 } 000490 return addr; 000491 } 000492 000493 /* Insert the end of a co-routine 000494 */ 000495 void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){ 000496 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); 000497 000498 /* Clear the temporary register cache, thereby ensuring that each 000499 ** co-routine has its own independent set of registers, because co-routines 000500 ** might expect their registers to be preserved across an OP_Yield, and 000501 ** that could cause problems if two or more co-routines are using the same 000502 ** temporary register. 000503 */ 000504 v->pParse->nTempReg = 0; 000505 v->pParse->nRangeReg = 0; 000506 } 000507 000508 /* 000509 ** Create a new symbolic label for an instruction that has yet to be 000510 ** coded. The symbolic label is really just a negative number. The 000511 ** label can be used as the P2 value of an operation. Later, when 000512 ** the label is resolved to a specific address, the VDBE will scan 000513 ** through its operation list and change all values of P2 which match 000514 ** the label into the resolved address. 000515 ** 000516 ** The VDBE knows that a P2 value is a label because labels are 000517 ** always negative and P2 values are suppose to be non-negative. 000518 ** Hence, a negative P2 value is a label that has yet to be resolved. 000519 ** (Later:) This is only true for opcodes that have the OPFLG_JUMP 000520 ** property. 000521 ** 000522 ** Variable usage notes: 000523 ** 000524 ** Parse.aLabel[x] Stores the address that the x-th label resolves 000525 ** into. For testing (SQLITE_DEBUG), unresolved 000526 ** labels stores -1, but that is not required. 000527 ** Parse.nLabelAlloc Number of slots allocated to Parse.aLabel[] 000528 ** Parse.nLabel The *negative* of the number of labels that have 000529 ** been issued. The negative is stored because 000530 ** that gives a performance improvement over storing 000531 ** the equivalent positive value. 000532 */ 000533 int sqlite3VdbeMakeLabel(Parse *pParse){ 000534 return --pParse->nLabel; 000535 } 000536 000537 /* 000538 ** Resolve label "x" to be the address of the next instruction to 000539 ** be inserted. The parameter "x" must have been obtained from 000540 ** a prior call to sqlite3VdbeMakeLabel(). 000541 */ 000542 static SQLITE_NOINLINE void resizeResolveLabel(Parse *p, Vdbe *v, int j){ 000543 int nNewSize = 10 - p->nLabel; 000544 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, 000545 nNewSize*sizeof(p->aLabel[0])); 000546 if( p->aLabel==0 ){ 000547 p->nLabelAlloc = 0; 000548 }else{ 000549 #ifdef SQLITE_DEBUG 000550 int i; 000551 for(i=p->nLabelAlloc; i<nNewSize; i++) p->aLabel[i] = -1; 000552 #endif 000553 p->nLabelAlloc = nNewSize; 000554 p->aLabel[j] = v->nOp; 000555 } 000556 } 000557 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ 000558 Parse *p = v->pParse; 000559 int j = ADDR(x); 000560 assert( v->magic==VDBE_MAGIC_INIT ); 000561 assert( j<-p->nLabel ); 000562 assert( j>=0 ); 000563 #ifdef SQLITE_DEBUG 000564 if( p->db->flags & SQLITE_VdbeAddopTrace ){ 000565 printf("RESOLVE LABEL %d to %d\n", x, v->nOp); 000566 } 000567 #endif 000568 if( p->nLabelAlloc + p->nLabel < 0 ){ 000569 resizeResolveLabel(p,v,j); 000570 }else{ 000571 assert( p->aLabel[j]==(-1) ); /* Labels may only be resolved once */ 000572 p->aLabel[j] = v->nOp; 000573 } 000574 } 000575 000576 /* 000577 ** Mark the VDBE as one that can only be run one time. 000578 */ 000579 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ 000580 p->runOnlyOnce = 1; 000581 } 000582 000583 /* 000584 ** Mark the VDBE as one that can only be run multiple times. 000585 */ 000586 void sqlite3VdbeReusable(Vdbe *p){ 000587 p->runOnlyOnce = 0; 000588 } 000589 000590 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ 000591 000592 /* 000593 ** The following type and function are used to iterate through all opcodes 000594 ** in a Vdbe main program and each of the sub-programs (triggers) it may 000595 ** invoke directly or indirectly. It should be used as follows: 000596 ** 000597 ** Op *pOp; 000598 ** VdbeOpIter sIter; 000599 ** 000600 ** memset(&sIter, 0, sizeof(sIter)); 000601 ** sIter.v = v; // v is of type Vdbe* 000602 ** while( (pOp = opIterNext(&sIter)) ){ 000603 ** // Do something with pOp 000604 ** } 000605 ** sqlite3DbFree(v->db, sIter.apSub); 000606 ** 000607 */ 000608 typedef struct VdbeOpIter VdbeOpIter; 000609 struct VdbeOpIter { 000610 Vdbe *v; /* Vdbe to iterate through the opcodes of */ 000611 SubProgram **apSub; /* Array of subprograms */ 000612 int nSub; /* Number of entries in apSub */ 000613 int iAddr; /* Address of next instruction to return */ 000614 int iSub; /* 0 = main program, 1 = first sub-program etc. */ 000615 }; 000616 static Op *opIterNext(VdbeOpIter *p){ 000617 Vdbe *v = p->v; 000618 Op *pRet = 0; 000619 Op *aOp; 000620 int nOp; 000621 000622 if( p->iSub<=p->nSub ){ 000623 000624 if( p->iSub==0 ){ 000625 aOp = v->aOp; 000626 nOp = v->nOp; 000627 }else{ 000628 aOp = p->apSub[p->iSub-1]->aOp; 000629 nOp = p->apSub[p->iSub-1]->nOp; 000630 } 000631 assert( p->iAddr<nOp ); 000632 000633 pRet = &aOp[p->iAddr]; 000634 p->iAddr++; 000635 if( p->iAddr==nOp ){ 000636 p->iSub++; 000637 p->iAddr = 0; 000638 } 000639 000640 if( pRet->p4type==P4_SUBPROGRAM ){ 000641 int nByte = (p->nSub+1)*sizeof(SubProgram*); 000642 int j; 000643 for(j=0; j<p->nSub; j++){ 000644 if( p->apSub[j]==pRet->p4.pProgram ) break; 000645 } 000646 if( j==p->nSub ){ 000647 p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte); 000648 if( !p->apSub ){ 000649 pRet = 0; 000650 }else{ 000651 p->apSub[p->nSub++] = pRet->p4.pProgram; 000652 } 000653 } 000654 } 000655 } 000656 000657 return pRet; 000658 } 000659 000660 /* 000661 ** Check if the program stored in the VM associated with pParse may 000662 ** throw an ABORT exception (causing the statement, but not entire transaction 000663 ** to be rolled back). This condition is true if the main program or any 000664 ** sub-programs contains any of the following: 000665 ** 000666 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. 000667 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. 000668 ** * OP_Destroy 000669 ** * OP_VUpdate 000670 ** * OP_VCreate 000671 ** * OP_VRename 000672 ** * OP_FkCounter with P2==0 (immediate foreign key constraint) 000673 ** * OP_CreateBtree/BTREE_INTKEY and OP_InitCoroutine 000674 ** (for CREATE TABLE AS SELECT ...) 000675 ** 000676 ** Then check that the value of Parse.mayAbort is true if an 000677 ** ABORT may be thrown, or false otherwise. Return true if it does 000678 ** match, or false otherwise. This function is intended to be used as 000679 ** part of an assert statement in the compiler. Similar to: 000680 ** 000681 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); 000682 */ 000683 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ 000684 int hasAbort = 0; 000685 int hasFkCounter = 0; 000686 int hasCreateTable = 0; 000687 int hasCreateIndex = 0; 000688 int hasInitCoroutine = 0; 000689 Op *pOp; 000690 VdbeOpIter sIter; 000691 memset(&sIter, 0, sizeof(sIter)); 000692 sIter.v = v; 000693 000694 while( (pOp = opIterNext(&sIter))!=0 ){ 000695 int opcode = pOp->opcode; 000696 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename 000697 || opcode==OP_VDestroy 000698 || opcode==OP_VCreate 000699 || (opcode==OP_ParseSchema && pOp->p4.z==0) 000700 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) 000701 && ((pOp->p1)!=SQLITE_OK && pOp->p2==OE_Abort)) 000702 ){ 000703 hasAbort = 1; 000704 break; 000705 } 000706 if( opcode==OP_CreateBtree && pOp->p3==BTREE_INTKEY ) hasCreateTable = 1; 000707 if( mayAbort ){ 000708 /* hasCreateIndex may also be set for some DELETE statements that use 000709 ** OP_Clear. So this routine may end up returning true in the case 000710 ** where a "DELETE FROM tbl" has a statement-journal but does not 000711 ** require one. This is not so bad - it is an inefficiency, not a bug. */ 000712 if( opcode==OP_CreateBtree && pOp->p3==BTREE_BLOBKEY ) hasCreateIndex = 1; 000713 if( opcode==OP_Clear ) hasCreateIndex = 1; 000714 } 000715 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1; 000716 #ifndef SQLITE_OMIT_FOREIGN_KEY 000717 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){ 000718 hasFkCounter = 1; 000719 } 000720 #endif 000721 } 000722 sqlite3DbFree(v->db, sIter.apSub); 000723 000724 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. 000725 ** If malloc failed, then the while() loop above may not have iterated 000726 ** through all opcodes and hasAbort may be set incorrectly. Return 000727 ** true for this case to prevent the assert() in the callers frame 000728 ** from failing. */ 000729 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter 000730 || (hasCreateTable && hasInitCoroutine) || hasCreateIndex 000731 ); 000732 } 000733 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ 000734 000735 #ifdef SQLITE_DEBUG 000736 /* 000737 ** Increment the nWrite counter in the VDBE if the cursor is not an 000738 ** ephemeral cursor, or if the cursor argument is NULL. 000739 */ 000740 void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){ 000741 if( pC==0 000742 || (pC->eCurType!=CURTYPE_SORTER 000743 && pC->eCurType!=CURTYPE_PSEUDO 000744 && !pC->isEphemeral) 000745 ){ 000746 p->nWrite++; 000747 } 000748 } 000749 #endif 000750 000751 #ifdef SQLITE_DEBUG 000752 /* 000753 ** Assert if an Abort at this point in time might result in a corrupt 000754 ** database. 000755 */ 000756 void sqlite3VdbeAssertAbortable(Vdbe *p){ 000757 assert( p->nWrite==0 || p->usesStmtJournal ); 000758 } 000759 #endif 000760 000761 /* 000762 ** This routine is called after all opcodes have been inserted. It loops 000763 ** through all the opcodes and fixes up some details. 000764 ** 000765 ** (1) For each jump instruction with a negative P2 value (a label) 000766 ** resolve the P2 value to an actual address. 000767 ** 000768 ** (2) Compute the maximum number of arguments used by any SQL function 000769 ** and store that value in *pMaxFuncArgs. 000770 ** 000771 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately 000772 ** indicate what the prepared statement actually does. 000773 ** 000774 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it. 000775 ** 000776 ** (5) Reclaim the memory allocated for storing labels. 000777 ** 000778 ** This routine will only function correctly if the mkopcodeh.tcl generator 000779 ** script numbers the opcodes correctly. Changes to this routine must be 000780 ** coordinated with changes to mkopcodeh.tcl. 000781 */ 000782 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ 000783 int nMaxArgs = *pMaxFuncArgs; 000784 Op *pOp; 000785 Parse *pParse = p->pParse; 000786 int *aLabel = pParse->aLabel; 000787 p->readOnly = 1; 000788 p->bIsReader = 0; 000789 pOp = &p->aOp[p->nOp-1]; 000790 while(1){ 000791 000792 /* Only JUMP opcodes and the short list of special opcodes in the switch 000793 ** below need to be considered. The mkopcodeh.tcl generator script groups 000794 ** all these opcodes together near the front of the opcode list. Skip 000795 ** any opcode that does not need processing by virtual of the fact that 000796 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. 000797 */ 000798 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){ 000799 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing 000800 ** cases from this switch! */ 000801 switch( pOp->opcode ){ 000802 case OP_Transaction: { 000803 if( pOp->p2!=0 ) p->readOnly = 0; 000804 /* fall thru */ 000805 } 000806 case OP_AutoCommit: 000807 case OP_Savepoint: { 000808 p->bIsReader = 1; 000809 break; 000810 } 000811 #ifndef SQLITE_OMIT_WAL 000812 case OP_Checkpoint: 000813 #endif 000814 case OP_Vacuum: 000815 case OP_JournalMode: { 000816 p->readOnly = 0; 000817 p->bIsReader = 1; 000818 break; 000819 } 000820 case OP_Next: 000821 case OP_SorterNext: { 000822 pOp->p4.xAdvance = sqlite3BtreeNext; 000823 pOp->p4type = P4_ADVANCE; 000824 /* The code generator never codes any of these opcodes as a jump 000825 ** to a label. They are always coded as a jump backwards to a 000826 ** known address */ 000827 assert( pOp->p2>=0 ); 000828 break; 000829 } 000830 case OP_Prev: { 000831 pOp->p4.xAdvance = sqlite3BtreePrevious; 000832 pOp->p4type = P4_ADVANCE; 000833 /* The code generator never codes any of these opcodes as a jump 000834 ** to a label. They are always coded as a jump backwards to a 000835 ** known address */ 000836 assert( pOp->p2>=0 ); 000837 break; 000838 } 000839 #ifndef SQLITE_OMIT_VIRTUALTABLE 000840 case OP_VUpdate: { 000841 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; 000842 break; 000843 } 000844 case OP_VFilter: { 000845 int n; 000846 assert( (pOp - p->aOp) >= 3 ); 000847 assert( pOp[-1].opcode==OP_Integer ); 000848 n = pOp[-1].p1; 000849 if( n>nMaxArgs ) nMaxArgs = n; 000850 /* Fall through into the default case */ 000851 } 000852 #endif 000853 default: { 000854 if( pOp->p2<0 ){ 000855 /* The mkopcodeh.tcl script has so arranged things that the only 000856 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to 000857 ** have non-negative values for P2. */ 000858 assert( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 ); 000859 assert( ADDR(pOp->p2)<-pParse->nLabel ); 000860 pOp->p2 = aLabel[ADDR(pOp->p2)]; 000861 } 000862 break; 000863 } 000864 } 000865 /* The mkopcodeh.tcl script has so arranged things that the only 000866 ** non-jump opcodes less than SQLITE_MX_JUMP_CODE are guaranteed to 000867 ** have non-negative values for P2. */ 000868 assert( (sqlite3OpcodeProperty[pOp->opcode]&OPFLG_JUMP)==0 || pOp->p2>=0); 000869 } 000870 if( pOp==p->aOp ) break; 000871 pOp--; 000872 } 000873 sqlite3DbFree(p->db, pParse->aLabel); 000874 pParse->aLabel = 0; 000875 pParse->nLabel = 0; 000876 *pMaxFuncArgs = nMaxArgs; 000877 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); 000878 } 000879 000880 /* 000881 ** Return the address of the next instruction to be inserted. 000882 */ 000883 int sqlite3VdbeCurrentAddr(Vdbe *p){ 000884 assert( p->magic==VDBE_MAGIC_INIT ); 000885 return p->nOp; 000886 } 000887 000888 /* 000889 ** Verify that at least N opcode slots are available in p without 000890 ** having to malloc for more space (except when compiled using 000891 ** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing 000892 ** to verify that certain calls to sqlite3VdbeAddOpList() can never 000893 ** fail due to a OOM fault and hence that the return value from 000894 ** sqlite3VdbeAddOpList() will always be non-NULL. 000895 */ 000896 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) 000897 void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){ 000898 assert( p->nOp + N <= p->nOpAlloc ); 000899 } 000900 #endif 000901 000902 /* 000903 ** Verify that the VM passed as the only argument does not contain 000904 ** an OP_ResultRow opcode. Fail an assert() if it does. This is used 000905 ** by code in pragma.c to ensure that the implementation of certain 000906 ** pragmas comports with the flags specified in the mkpragmatab.tcl 000907 ** script. 000908 */ 000909 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) 000910 void sqlite3VdbeVerifyNoResultRow(Vdbe *p){ 000911 int i; 000912 for(i=0; i<p->nOp; i++){ 000913 assert( p->aOp[i].opcode!=OP_ResultRow ); 000914 } 000915 } 000916 #endif 000917 000918 /* 000919 ** Generate code (a single OP_Abortable opcode) that will 000920 ** verify that the VDBE program can safely call Abort in the current 000921 ** context. 000922 */ 000923 #if defined(SQLITE_DEBUG) 000924 void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){ 000925 if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable); 000926 } 000927 #endif 000928 000929 /* 000930 ** This function returns a pointer to the array of opcodes associated with 000931 ** the Vdbe passed as the first argument. It is the callers responsibility 000932 ** to arrange for the returned array to be eventually freed using the 000933 ** vdbeFreeOpArray() function. 000934 ** 000935 ** Before returning, *pnOp is set to the number of entries in the returned 000936 ** array. Also, *pnMaxArg is set to the larger of its current value and 000937 ** the number of entries in the Vdbe.apArg[] array required to execute the 000938 ** returned program. 000939 */ 000940 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ 000941 VdbeOp *aOp = p->aOp; 000942 assert( aOp && !p->db->mallocFailed ); 000943 000944 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ 000945 assert( DbMaskAllZero(p->btreeMask) ); 000946 000947 resolveP2Values(p, pnMaxArg); 000948 *pnOp = p->nOp; 000949 p->aOp = 0; 000950 return aOp; 000951 } 000952 000953 /* 000954 ** Add a whole list of operations to the operation stack. Return a 000955 ** pointer to the first operation inserted. 000956 ** 000957 ** Non-zero P2 arguments to jump instructions are automatically adjusted 000958 ** so that the jump target is relative to the first operation inserted. 000959 */ 000960 VdbeOp *sqlite3VdbeAddOpList( 000961 Vdbe *p, /* Add opcodes to the prepared statement */ 000962 int nOp, /* Number of opcodes to add */ 000963 VdbeOpList const *aOp, /* The opcodes to be added */ 000964 int iLineno /* Source-file line number of first opcode */ 000965 ){ 000966 int i; 000967 VdbeOp *pOut, *pFirst; 000968 assert( nOp>0 ); 000969 assert( p->magic==VDBE_MAGIC_INIT ); 000970 if( p->nOp + nOp > p->nOpAlloc && growOpArray(p, nOp) ){ 000971 return 0; 000972 } 000973 pFirst = pOut = &p->aOp[p->nOp]; 000974 for(i=0; i<nOp; i++, aOp++, pOut++){ 000975 pOut->opcode = aOp->opcode; 000976 pOut->p1 = aOp->p1; 000977 pOut->p2 = aOp->p2; 000978 assert( aOp->p2>=0 ); 000979 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){ 000980 pOut->p2 += p->nOp; 000981 } 000982 pOut->p3 = aOp->p3; 000983 pOut->p4type = P4_NOTUSED; 000984 pOut->p4.p = 0; 000985 pOut->p5 = 0; 000986 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 000987 pOut->zComment = 0; 000988 #endif 000989 #ifdef SQLITE_VDBE_COVERAGE 000990 pOut->iSrcLine = iLineno+i; 000991 #else 000992 (void)iLineno; 000993 #endif 000994 #ifdef SQLITE_DEBUG 000995 if( p->db->flags & SQLITE_VdbeAddopTrace ){ 000996 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]); 000997 } 000998 #endif 000999 } 001000 p->nOp += nOp; 001001 return pFirst; 001002 } 001003 001004 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) 001005 /* 001006 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). 001007 */ 001008 void sqlite3VdbeScanStatus( 001009 Vdbe *p, /* VM to add scanstatus() to */ 001010 int addrExplain, /* Address of OP_Explain (or 0) */ 001011 int addrLoop, /* Address of loop counter */ 001012 int addrVisit, /* Address of rows visited counter */ 001013 LogEst nEst, /* Estimated number of output rows */ 001014 const char *zName /* Name of table or index being scanned */ 001015 ){ 001016 sqlite3_int64 nByte = (p->nScan+1) * sizeof(ScanStatus); 001017 ScanStatus *aNew; 001018 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); 001019 if( aNew ){ 001020 ScanStatus *pNew = &aNew[p->nScan++]; 001021 pNew->addrExplain = addrExplain; 001022 pNew->addrLoop = addrLoop; 001023 pNew->addrVisit = addrVisit; 001024 pNew->nEst = nEst; 001025 pNew->zName = sqlite3DbStrDup(p->db, zName); 001026 p->aScan = aNew; 001027 } 001028 } 001029 #endif 001030 001031 001032 /* 001033 ** Change the value of the opcode, or P1, P2, P3, or P5 operands 001034 ** for a specific instruction. 001035 */ 001036 void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){ 001037 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode; 001038 } 001039 void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){ 001040 sqlite3VdbeGetOp(p,addr)->p1 = val; 001041 } 001042 void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){ 001043 sqlite3VdbeGetOp(p,addr)->p2 = val; 001044 } 001045 void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){ 001046 sqlite3VdbeGetOp(p,addr)->p3 = val; 001047 } 001048 void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ 001049 assert( p->nOp>0 || p->db->mallocFailed ); 001050 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; 001051 } 001052 001053 /* 001054 ** Change the P2 operand of instruction addr so that it points to 001055 ** the address of the next instruction to be coded. 001056 */ 001057 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ 001058 sqlite3VdbeChangeP2(p, addr, p->nOp); 001059 } 001060 001061 001062 /* 001063 ** If the input FuncDef structure is ephemeral, then free it. If 001064 ** the FuncDef is not ephermal, then do nothing. 001065 */ 001066 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ 001067 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ 001068 sqlite3DbFreeNN(db, pDef); 001069 } 001070 } 001071 001072 /* 001073 ** Delete a P4 value if necessary. 001074 */ 001075 static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){ 001076 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); 001077 sqlite3DbFreeNN(db, p); 001078 } 001079 static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){ 001080 freeEphemeralFunction(db, p->pFunc); 001081 sqlite3DbFreeNN(db, p); 001082 } 001083 static void freeP4(sqlite3 *db, int p4type, void *p4){ 001084 assert( db ); 001085 switch( p4type ){ 001086 case P4_FUNCCTX: { 001087 freeP4FuncCtx(db, (sqlite3_context*)p4); 001088 break; 001089 } 001090 case P4_REAL: 001091 case P4_INT64: 001092 case P4_DYNAMIC: 001093 case P4_DYNBLOB: 001094 case P4_INTARRAY: { 001095 sqlite3DbFree(db, p4); 001096 break; 001097 } 001098 case P4_KEYINFO: { 001099 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); 001100 break; 001101 } 001102 #ifdef SQLITE_ENABLE_CURSOR_HINTS 001103 case P4_EXPR: { 001104 sqlite3ExprDelete(db, (Expr*)p4); 001105 break; 001106 } 001107 #endif 001108 case P4_FUNCDEF: { 001109 freeEphemeralFunction(db, (FuncDef*)p4); 001110 break; 001111 } 001112 case P4_MEM: { 001113 if( db->pnBytesFreed==0 ){ 001114 sqlite3ValueFree((sqlite3_value*)p4); 001115 }else{ 001116 freeP4Mem(db, (Mem*)p4); 001117 } 001118 break; 001119 } 001120 case P4_VTAB : { 001121 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); 001122 break; 001123 } 001124 } 001125 } 001126 001127 /* 001128 ** Free the space allocated for aOp and any p4 values allocated for the 001129 ** opcodes contained within. If aOp is not NULL it is assumed to contain 001130 ** nOp entries. 001131 */ 001132 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ 001133 if( aOp ){ 001134 Op *pOp; 001135 for(pOp=&aOp[nOp-1]; pOp>=aOp; pOp--){ 001136 if( pOp->p4type <= P4_FREE_IF_LE ) freeP4(db, pOp->p4type, pOp->p4.p); 001137 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 001138 sqlite3DbFree(db, pOp->zComment); 001139 #endif 001140 } 001141 sqlite3DbFreeNN(db, aOp); 001142 } 001143 } 001144 001145 /* 001146 ** Link the SubProgram object passed as the second argument into the linked 001147 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program 001148 ** objects when the VM is no longer required. 001149 */ 001150 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ 001151 p->pNext = pVdbe->pProgram; 001152 pVdbe->pProgram = p; 001153 } 001154 001155 /* 001156 ** Return true if the given Vdbe has any SubPrograms. 001157 */ 001158 int sqlite3VdbeHasSubProgram(Vdbe *pVdbe){ 001159 return pVdbe->pProgram!=0; 001160 } 001161 001162 /* 001163 ** Change the opcode at addr into OP_Noop 001164 */ 001165 int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ 001166 VdbeOp *pOp; 001167 if( p->db->mallocFailed ) return 0; 001168 assert( addr>=0 && addr<p->nOp ); 001169 pOp = &p->aOp[addr]; 001170 freeP4(p->db, pOp->p4type, pOp->p4.p); 001171 pOp->p4type = P4_NOTUSED; 001172 pOp->p4.z = 0; 001173 pOp->opcode = OP_Noop; 001174 return 1; 001175 } 001176 001177 /* 001178 ** If the last opcode is "op" and it is not a jump destination, 001179 ** then remove it. Return true if and only if an opcode was removed. 001180 */ 001181 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ 001182 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){ 001183 return sqlite3VdbeChangeToNoop(p, p->nOp-1); 001184 }else{ 001185 return 0; 001186 } 001187 } 001188 001189 #ifdef SQLITE_DEBUG 001190 /* 001191 ** Generate an OP_ReleaseReg opcode to indicate that a range of 001192 ** registers, except any identified by mask, are no longer in use. 001193 */ 001194 void sqlite3VdbeReleaseRegisters(Parse *pParse, int iFirst, int N, u32 mask){ 001195 assert( pParse->pVdbe ); 001196 while( N>0 && (mask&1)!=0 ){ 001197 mask >>= 1; 001198 iFirst++; 001199 N--; 001200 } 001201 while( N>0 && N<=32 && (mask & MASKBIT32(N-1))!=0 ){ 001202 mask &= ~MASKBIT32(N-1); 001203 N--; 001204 } 001205 if( N>0 ){ 001206 sqlite3VdbeAddOp3(pParse->pVdbe, OP_ReleaseReg, iFirst, N, *(int*)&mask); 001207 } 001208 } 001209 #endif /* SQLITE_DEBUG */ 001210 001211 001212 /* 001213 ** Change the value of the P4 operand for a specific instruction. 001214 ** This routine is useful when a large program is loaded from a 001215 ** static array using sqlite3VdbeAddOpList but we want to make a 001216 ** few minor changes to the program. 001217 ** 001218 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of 001219 ** the string is made into memory obtained from sqlite3_malloc(). 001220 ** A value of n==0 means copy bytes of zP4 up to and including the 001221 ** first null byte. If n>0 then copy n+1 bytes of zP4. 001222 ** 001223 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points 001224 ** to a string or structure that is guaranteed to exist for the lifetime of 001225 ** the Vdbe. In these cases we can just copy the pointer. 001226 ** 001227 ** If addr<0 then change P4 on the most recently inserted instruction. 001228 */ 001229 static void SQLITE_NOINLINE vdbeChangeP4Full( 001230 Vdbe *p, 001231 Op *pOp, 001232 const char *zP4, 001233 int n 001234 ){ 001235 if( pOp->p4type ){ 001236 freeP4(p->db, pOp->p4type, pOp->p4.p); 001237 pOp->p4type = 0; 001238 pOp->p4.p = 0; 001239 } 001240 if( n<0 ){ 001241 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n); 001242 }else{ 001243 if( n==0 ) n = sqlite3Strlen30(zP4); 001244 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); 001245 pOp->p4type = P4_DYNAMIC; 001246 } 001247 } 001248 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ 001249 Op *pOp; 001250 sqlite3 *db; 001251 assert( p!=0 ); 001252 db = p->db; 001253 assert( p->magic==VDBE_MAGIC_INIT ); 001254 assert( p->aOp!=0 || db->mallocFailed ); 001255 if( db->mallocFailed ){ 001256 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4); 001257 return; 001258 } 001259 assert( p->nOp>0 ); 001260 assert( addr<p->nOp ); 001261 if( addr<0 ){ 001262 addr = p->nOp - 1; 001263 } 001264 pOp = &p->aOp[addr]; 001265 if( n>=0 || pOp->p4type ){ 001266 vdbeChangeP4Full(p, pOp, zP4, n); 001267 return; 001268 } 001269 if( n==P4_INT32 ){ 001270 /* Note: this cast is safe, because the origin data point was an int 001271 ** that was cast to a (const char *). */ 001272 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); 001273 pOp->p4type = P4_INT32; 001274 }else if( zP4!=0 ){ 001275 assert( n<0 ); 001276 pOp->p4.p = (void*)zP4; 001277 pOp->p4type = (signed char)n; 001278 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4); 001279 } 001280 } 001281 001282 /* 001283 ** Change the P4 operand of the most recently coded instruction 001284 ** to the value defined by the arguments. This is a high-speed 001285 ** version of sqlite3VdbeChangeP4(). 001286 ** 001287 ** The P4 operand must not have been previously defined. And the new 001288 ** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of 001289 ** those cases. 001290 */ 001291 void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){ 001292 VdbeOp *pOp; 001293 assert( n!=P4_INT32 && n!=P4_VTAB ); 001294 assert( n<=0 ); 001295 if( p->db->mallocFailed ){ 001296 freeP4(p->db, n, pP4); 001297 }else{ 001298 assert( pP4!=0 ); 001299 assert( p->nOp>0 ); 001300 pOp = &p->aOp[p->nOp-1]; 001301 assert( pOp->p4type==P4_NOTUSED ); 001302 pOp->p4type = n; 001303 pOp->p4.p = pP4; 001304 } 001305 } 001306 001307 /* 001308 ** Set the P4 on the most recently added opcode to the KeyInfo for the 001309 ** index given. 001310 */ 001311 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ 001312 Vdbe *v = pParse->pVdbe; 001313 KeyInfo *pKeyInfo; 001314 assert( v!=0 ); 001315 assert( pIdx!=0 ); 001316 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx); 001317 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); 001318 } 001319 001320 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 001321 /* 001322 ** Change the comment on the most recently coded instruction. Or 001323 ** insert a No-op and add the comment to that new instruction. This 001324 ** makes the code easier to read during debugging. None of this happens 001325 ** in a production build. 001326 */ 001327 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ 001328 assert( p->nOp>0 || p->aOp==0 ); 001329 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed 001330 || p->pParse->nErr>0 ); 001331 if( p->nOp ){ 001332 assert( p->aOp ); 001333 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment); 001334 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap); 001335 } 001336 } 001337 void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){ 001338 va_list ap; 001339 if( p ){ 001340 va_start(ap, zFormat); 001341 vdbeVComment(p, zFormat, ap); 001342 va_end(ap); 001343 } 001344 } 001345 void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){ 001346 va_list ap; 001347 if( p ){ 001348 sqlite3VdbeAddOp0(p, OP_Noop); 001349 va_start(ap, zFormat); 001350 vdbeVComment(p, zFormat, ap); 001351 va_end(ap); 001352 } 001353 } 001354 #endif /* NDEBUG */ 001355 001356 #ifdef SQLITE_VDBE_COVERAGE 001357 /* 001358 ** Set the value if the iSrcLine field for the previously coded instruction. 001359 */ 001360 void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){ 001361 sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine; 001362 } 001363 #endif /* SQLITE_VDBE_COVERAGE */ 001364 001365 /* 001366 ** Return the opcode for a given address. If the address is -1, then 001367 ** return the most recently inserted opcode. 001368 ** 001369 ** If a memory allocation error has occurred prior to the calling of this 001370 ** routine, then a pointer to a dummy VdbeOp will be returned. That opcode 001371 ** is readable but not writable, though it is cast to a writable value. 001372 ** The return of a dummy opcode allows the call to continue functioning 001373 ** after an OOM fault without having to check to see if the return from 001374 ** this routine is a valid pointer. But because the dummy.opcode is 0, 001375 ** dummy will never be written to. This is verified by code inspection and 001376 ** by running with Valgrind. 001377 */ 001378 VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){ 001379 /* C89 specifies that the constant "dummy" will be initialized to all 001380 ** zeros, which is correct. MSVC generates a warning, nevertheless. */ 001381 static VdbeOp dummy; /* Ignore the MSVC warning about no initializer */ 001382 assert( p->magic==VDBE_MAGIC_INIT ); 001383 if( addr<0 ){ 001384 addr = p->nOp - 1; 001385 } 001386 assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed ); 001387 if( p->db->mallocFailed ){ 001388 return (VdbeOp*)&dummy; 001389 }else{ 001390 return &p->aOp[addr]; 001391 } 001392 } 001393 001394 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) 001395 /* 001396 ** Return an integer value for one of the parameters to the opcode pOp 001397 ** determined by character c. 001398 */ 001399 static int translateP(char c, const Op *pOp){ 001400 if( c=='1' ) return pOp->p1; 001401 if( c=='2' ) return pOp->p2; 001402 if( c=='3' ) return pOp->p3; 001403 if( c=='4' ) return pOp->p4.i; 001404 return pOp->p5; 001405 } 001406 001407 /* 001408 ** Compute a string for the "comment" field of a VDBE opcode listing. 001409 ** 001410 ** The Synopsis: field in comments in the vdbe.c source file gets converted 001411 ** to an extra string that is appended to the sqlite3OpcodeName(). In the 001412 ** absence of other comments, this synopsis becomes the comment on the opcode. 001413 ** Some translation occurs: 001414 ** 001415 ** "PX" -> "r[X]" 001416 ** "PX@PY" -> "r[X..X+Y-1]" or "r[x]" if y is 0 or 1 001417 ** "PX@PY+1" -> "r[X..X+Y]" or "r[x]" if y is 0 001418 ** "PY..PY" -> "r[X..Y]" or "r[x]" if y<=x 001419 */ 001420 static int displayComment( 001421 const Op *pOp, /* The opcode to be commented */ 001422 const char *zP4, /* Previously obtained value for P4 */ 001423 char *zTemp, /* Write result here */ 001424 int nTemp /* Space available in zTemp[] */ 001425 ){ 001426 const char *zOpName; 001427 const char *zSynopsis; 001428 int nOpName; 001429 int ii, jj; 001430 char zAlt[50]; 001431 zOpName = sqlite3OpcodeName(pOp->opcode); 001432 nOpName = sqlite3Strlen30(zOpName); 001433 if( zOpName[nOpName+1] ){ 001434 int seenCom = 0; 001435 char c; 001436 zSynopsis = zOpName += nOpName + 1; 001437 if( strncmp(zSynopsis,"IF ",3)==0 ){ 001438 if( pOp->p5 & SQLITE_STOREP2 ){ 001439 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3); 001440 }else{ 001441 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3); 001442 } 001443 zSynopsis = zAlt; 001444 } 001445 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ 001446 if( c=='P' ){ 001447 c = zSynopsis[++ii]; 001448 if( c=='4' ){ 001449 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); 001450 }else if( c=='X' ){ 001451 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); 001452 seenCom = 1; 001453 }else{ 001454 int v1 = translateP(c, pOp); 001455 int v2; 001456 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1); 001457 if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){ 001458 ii += 3; 001459 jj += sqlite3Strlen30(zTemp+jj); 001460 v2 = translateP(zSynopsis[ii], pOp); 001461 if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){ 001462 ii += 2; 001463 v2++; 001464 } 001465 if( v2>1 ){ 001466 sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1); 001467 } 001468 }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){ 001469 ii += 4; 001470 } 001471 } 001472 jj += sqlite3Strlen30(zTemp+jj); 001473 }else{ 001474 zTemp[jj++] = c; 001475 } 001476 } 001477 if( !seenCom && jj<nTemp-5 && pOp->zComment ){ 001478 sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment); 001479 jj += sqlite3Strlen30(zTemp+jj); 001480 } 001481 if( jj<nTemp ) zTemp[jj] = 0; 001482 }else if( pOp->zComment ){ 001483 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); 001484 jj = sqlite3Strlen30(zTemp); 001485 }else{ 001486 zTemp[0] = 0; 001487 jj = 0; 001488 } 001489 return jj; 001490 } 001491 #endif /* SQLITE_DEBUG */ 001492 001493 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) 001494 /* 001495 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text 001496 ** that can be displayed in the P4 column of EXPLAIN output. 001497 */ 001498 static void displayP4Expr(StrAccum *p, Expr *pExpr){ 001499 const char *zOp = 0; 001500 switch( pExpr->op ){ 001501 case TK_STRING: 001502 sqlite3_str_appendf(p, "%Q", pExpr->u.zToken); 001503 break; 001504 case TK_INTEGER: 001505 sqlite3_str_appendf(p, "%d", pExpr->u.iValue); 001506 break; 001507 case TK_NULL: 001508 sqlite3_str_appendf(p, "NULL"); 001509 break; 001510 case TK_REGISTER: { 001511 sqlite3_str_appendf(p, "r[%d]", pExpr->iTable); 001512 break; 001513 } 001514 case TK_COLUMN: { 001515 if( pExpr->iColumn<0 ){ 001516 sqlite3_str_appendf(p, "rowid"); 001517 }else{ 001518 sqlite3_str_appendf(p, "c%d", (int)pExpr->iColumn); 001519 } 001520 break; 001521 } 001522 case TK_LT: zOp = "LT"; break; 001523 case TK_LE: zOp = "LE"; break; 001524 case TK_GT: zOp = "GT"; break; 001525 case TK_GE: zOp = "GE"; break; 001526 case TK_NE: zOp = "NE"; break; 001527 case TK_EQ: zOp = "EQ"; break; 001528 case TK_IS: zOp = "IS"; break; 001529 case TK_ISNOT: zOp = "ISNOT"; break; 001530 case TK_AND: zOp = "AND"; break; 001531 case TK_OR: zOp = "OR"; break; 001532 case TK_PLUS: zOp = "ADD"; break; 001533 case TK_STAR: zOp = "MUL"; break; 001534 case TK_MINUS: zOp = "SUB"; break; 001535 case TK_REM: zOp = "REM"; break; 001536 case TK_BITAND: zOp = "BITAND"; break; 001537 case TK_BITOR: zOp = "BITOR"; break; 001538 case TK_SLASH: zOp = "DIV"; break; 001539 case TK_LSHIFT: zOp = "LSHIFT"; break; 001540 case TK_RSHIFT: zOp = "RSHIFT"; break; 001541 case TK_CONCAT: zOp = "CONCAT"; break; 001542 case TK_UMINUS: zOp = "MINUS"; break; 001543 case TK_UPLUS: zOp = "PLUS"; break; 001544 case TK_BITNOT: zOp = "BITNOT"; break; 001545 case TK_NOT: zOp = "NOT"; break; 001546 case TK_ISNULL: zOp = "ISNULL"; break; 001547 case TK_NOTNULL: zOp = "NOTNULL"; break; 001548 001549 default: 001550 sqlite3_str_appendf(p, "%s", "expr"); 001551 break; 001552 } 001553 001554 if( zOp ){ 001555 sqlite3_str_appendf(p, "%s(", zOp); 001556 displayP4Expr(p, pExpr->pLeft); 001557 if( pExpr->pRight ){ 001558 sqlite3_str_append(p, ",", 1); 001559 displayP4Expr(p, pExpr->pRight); 001560 } 001561 sqlite3_str_append(p, ")", 1); 001562 } 001563 } 001564 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ 001565 001566 001567 #if VDBE_DISPLAY_P4 001568 /* 001569 ** Compute a string that describes the P4 parameter for an opcode. 001570 ** Use zTemp for any required temporary buffer space. 001571 */ 001572 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ 001573 char *zP4 = zTemp; 001574 StrAccum x; 001575 assert( nTemp>=20 ); 001576 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); 001577 switch( pOp->p4type ){ 001578 case P4_KEYINFO: { 001579 int j; 001580 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; 001581 assert( pKeyInfo->aSortFlags!=0 ); 001582 sqlite3_str_appendf(&x, "k(%d", pKeyInfo->nKeyField); 001583 for(j=0; j<pKeyInfo->nKeyField; j++){ 001584 CollSeq *pColl = pKeyInfo->aColl[j]; 001585 const char *zColl = pColl ? pColl->zName : ""; 001586 if( strcmp(zColl, "BINARY")==0 ) zColl = "B"; 001587 sqlite3_str_appendf(&x, ",%s%s%s", 001588 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_DESC) ? "-" : "", 001589 (pKeyInfo->aSortFlags[j] & KEYINFO_ORDER_BIGNULL)? "N." : "", 001590 zColl); 001591 } 001592 sqlite3_str_append(&x, ")", 1); 001593 break; 001594 } 001595 #ifdef SQLITE_ENABLE_CURSOR_HINTS 001596 case P4_EXPR: { 001597 displayP4Expr(&x, pOp->p4.pExpr); 001598 break; 001599 } 001600 #endif 001601 case P4_COLLSEQ: { 001602 CollSeq *pColl = pOp->p4.pColl; 001603 sqlite3_str_appendf(&x, "(%.20s)", pColl->zName); 001604 break; 001605 } 001606 case P4_FUNCDEF: { 001607 FuncDef *pDef = pOp->p4.pFunc; 001608 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg); 001609 break; 001610 } 001611 case P4_FUNCCTX: { 001612 FuncDef *pDef = pOp->p4.pCtx->pFunc; 001613 sqlite3_str_appendf(&x, "%s(%d)", pDef->zName, pDef->nArg); 001614 break; 001615 } 001616 case P4_INT64: { 001617 sqlite3_str_appendf(&x, "%lld", *pOp->p4.pI64); 001618 break; 001619 } 001620 case P4_INT32: { 001621 sqlite3_str_appendf(&x, "%d", pOp->p4.i); 001622 break; 001623 } 001624 case P4_REAL: { 001625 sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal); 001626 break; 001627 } 001628 case P4_MEM: { 001629 Mem *pMem = pOp->p4.pMem; 001630 if( pMem->flags & MEM_Str ){ 001631 zP4 = pMem->z; 001632 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 001633 sqlite3_str_appendf(&x, "%lld", pMem->u.i); 001634 }else if( pMem->flags & MEM_Real ){ 001635 sqlite3_str_appendf(&x, "%.16g", pMem->u.r); 001636 }else if( pMem->flags & MEM_Null ){ 001637 zP4 = "NULL"; 001638 }else{ 001639 assert( pMem->flags & MEM_Blob ); 001640 zP4 = "(blob)"; 001641 } 001642 break; 001643 } 001644 #ifndef SQLITE_OMIT_VIRTUALTABLE 001645 case P4_VTAB: { 001646 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; 001647 sqlite3_str_appendf(&x, "vtab:%p", pVtab); 001648 break; 001649 } 001650 #endif 001651 case P4_INTARRAY: { 001652 int i; 001653 int *ai = pOp->p4.ai; 001654 int n = ai[0]; /* The first element of an INTARRAY is always the 001655 ** count of the number of elements to follow */ 001656 for(i=1; i<=n; i++){ 001657 sqlite3_str_appendf(&x, ",%d", ai[i]); 001658 } 001659 zTemp[0] = '['; 001660 sqlite3_str_append(&x, "]", 1); 001661 break; 001662 } 001663 case P4_SUBPROGRAM: { 001664 sqlite3_str_appendf(&x, "program"); 001665 break; 001666 } 001667 case P4_DYNBLOB: 001668 case P4_ADVANCE: { 001669 zTemp[0] = 0; 001670 break; 001671 } 001672 case P4_TABLE: { 001673 sqlite3_str_appendf(&x, "%s", pOp->p4.pTab->zName); 001674 break; 001675 } 001676 default: { 001677 zP4 = pOp->p4.z; 001678 if( zP4==0 ){ 001679 zP4 = zTemp; 001680 zTemp[0] = 0; 001681 } 001682 } 001683 } 001684 sqlite3StrAccumFinish(&x); 001685 assert( zP4!=0 ); 001686 return zP4; 001687 } 001688 #endif /* VDBE_DISPLAY_P4 */ 001689 001690 /* 001691 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. 001692 ** 001693 ** The prepared statements need to know in advance the complete set of 001694 ** attached databases that will be use. A mask of these databases 001695 ** is maintained in p->btreeMask. The p->lockMask value is the subset of 001696 ** p->btreeMask of databases that will require a lock. 001697 */ 001698 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ 001699 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); 001700 assert( i<(int)sizeof(p->btreeMask)*8 ); 001701 DbMaskSet(p->btreeMask, i); 001702 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ 001703 DbMaskSet(p->lockMask, i); 001704 } 001705 } 001706 001707 #if !defined(SQLITE_OMIT_SHARED_CACHE) 001708 /* 001709 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, 001710 ** this routine obtains the mutex associated with each BtShared structure 001711 ** that may be accessed by the VM passed as an argument. In doing so it also 001712 ** sets the BtShared.db member of each of the BtShared structures, ensuring 001713 ** that the correct busy-handler callback is invoked if required. 001714 ** 001715 ** If SQLite is not threadsafe but does support shared-cache mode, then 001716 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables 001717 ** of all of BtShared structures accessible via the database handle 001718 ** associated with the VM. 001719 ** 001720 ** If SQLite is not threadsafe and does not support shared-cache mode, this 001721 ** function is a no-op. 001722 ** 001723 ** The p->btreeMask field is a bitmask of all btrees that the prepared 001724 ** statement p will ever use. Let N be the number of bits in p->btreeMask 001725 ** corresponding to btrees that use shared cache. Then the runtime of 001726 ** this routine is N*N. But as N is rarely more than 1, this should not 001727 ** be a problem. 001728 */ 001729 void sqlite3VdbeEnter(Vdbe *p){ 001730 int i; 001731 sqlite3 *db; 001732 Db *aDb; 001733 int nDb; 001734 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ 001735 db = p->db; 001736 aDb = db->aDb; 001737 nDb = db->nDb; 001738 for(i=0; i<nDb; i++){ 001739 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ 001740 sqlite3BtreeEnter(aDb[i].pBt); 001741 } 001742 } 001743 } 001744 #endif 001745 001746 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 001747 /* 001748 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). 001749 */ 001750 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){ 001751 int i; 001752 sqlite3 *db; 001753 Db *aDb; 001754 int nDb; 001755 db = p->db; 001756 aDb = db->aDb; 001757 nDb = db->nDb; 001758 for(i=0; i<nDb; i++){ 001759 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ 001760 sqlite3BtreeLeave(aDb[i].pBt); 001761 } 001762 } 001763 } 001764 void sqlite3VdbeLeave(Vdbe *p){ 001765 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ 001766 vdbeLeave(p); 001767 } 001768 #endif 001769 001770 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) 001771 /* 001772 ** Print a single opcode. This routine is used for debugging only. 001773 */ 001774 void sqlite3VdbePrintOp(FILE *pOut, int pc, VdbeOp *pOp){ 001775 char *zP4; 001776 char zPtr[50]; 001777 char zCom[100]; 001778 static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n"; 001779 if( pOut==0 ) pOut = stdout; 001780 zP4 = displayP4(pOp, zPtr, sizeof(zPtr)); 001781 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 001782 displayComment(pOp, zP4, zCom, sizeof(zCom)); 001783 #else 001784 zCom[0] = 0; 001785 #endif 001786 /* NB: The sqlite3OpcodeName() function is implemented by code created 001787 ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the 001788 ** information from the vdbe.c source text */ 001789 fprintf(pOut, zFormat1, pc, 001790 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, 001791 zCom 001792 ); 001793 fflush(pOut); 001794 } 001795 #endif 001796 001797 /* 001798 ** Initialize an array of N Mem element. 001799 */ 001800 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){ 001801 while( (N--)>0 ){ 001802 p->db = db; 001803 p->flags = flags; 001804 p->szMalloc = 0; 001805 #ifdef SQLITE_DEBUG 001806 p->pScopyFrom = 0; 001807 #endif 001808 p++; 001809 } 001810 } 001811 001812 /* 001813 ** Release an array of N Mem elements 001814 */ 001815 static void releaseMemArray(Mem *p, int N){ 001816 if( p && N ){ 001817 Mem *pEnd = &p[N]; 001818 sqlite3 *db = p->db; 001819 if( db->pnBytesFreed ){ 001820 do{ 001821 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); 001822 }while( (++p)<pEnd ); 001823 return; 001824 } 001825 do{ 001826 assert( (&p[1])==pEnd || p[0].db==p[1].db ); 001827 assert( sqlite3VdbeCheckMemInvariants(p) ); 001828 001829 /* This block is really an inlined version of sqlite3VdbeMemRelease() 001830 ** that takes advantage of the fact that the memory cell value is 001831 ** being set to NULL after releasing any dynamic resources. 001832 ** 001833 ** The justification for duplicating code is that according to 001834 ** callgrind, this causes a certain test case to hit the CPU 4.7 001835 ** percent less (x86 linux, gcc version 4.1.2, -O6) than if 001836 ** sqlite3MemRelease() were called from here. With -O2, this jumps 001837 ** to 6.6 percent. The test case is inserting 1000 rows into a table 001838 ** with no indexes using a single prepared INSERT statement, bind() 001839 ** and reset(). Inserts are grouped into a transaction. 001840 */ 001841 testcase( p->flags & MEM_Agg ); 001842 testcase( p->flags & MEM_Dyn ); 001843 testcase( p->xDel==sqlite3VdbeFrameMemDel ); 001844 if( p->flags&(MEM_Agg|MEM_Dyn) ){ 001845 sqlite3VdbeMemRelease(p); 001846 }else if( p->szMalloc ){ 001847 sqlite3DbFreeNN(db, p->zMalloc); 001848 p->szMalloc = 0; 001849 } 001850 001851 p->flags = MEM_Undefined; 001852 }while( (++p)<pEnd ); 001853 } 001854 } 001855 001856 #ifdef SQLITE_DEBUG 001857 /* 001858 ** Verify that pFrame is a valid VdbeFrame pointer. Return true if it is 001859 ** and false if something is wrong. 001860 ** 001861 ** This routine is intended for use inside of assert() statements only. 001862 */ 001863 int sqlite3VdbeFrameIsValid(VdbeFrame *pFrame){ 001864 if( pFrame->iFrameMagic!=SQLITE_FRAME_MAGIC ) return 0; 001865 return 1; 001866 } 001867 #endif 001868 001869 001870 /* 001871 ** This is a destructor on a Mem object (which is really an sqlite3_value) 001872 ** that deletes the Frame object that is attached to it as a blob. 001873 ** 001874 ** This routine does not delete the Frame right away. It merely adds the 001875 ** frame to a list of frames to be deleted when the Vdbe halts. 001876 */ 001877 void sqlite3VdbeFrameMemDel(void *pArg){ 001878 VdbeFrame *pFrame = (VdbeFrame*)pArg; 001879 assert( sqlite3VdbeFrameIsValid(pFrame) ); 001880 pFrame->pParent = pFrame->v->pDelFrame; 001881 pFrame->v->pDelFrame = pFrame; 001882 } 001883 001884 001885 /* 001886 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are 001887 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). 001888 */ 001889 void sqlite3VdbeFrameDelete(VdbeFrame *p){ 001890 int i; 001891 Mem *aMem = VdbeFrameMem(p); 001892 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; 001893 assert( sqlite3VdbeFrameIsValid(p) ); 001894 for(i=0; i<p->nChildCsr; i++){ 001895 sqlite3VdbeFreeCursor(p->v, apCsr[i]); 001896 } 001897 releaseMemArray(aMem, p->nChildMem); 001898 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0); 001899 sqlite3DbFree(p->v->db, p); 001900 } 001901 001902 #ifndef SQLITE_OMIT_EXPLAIN 001903 /* 001904 ** Give a listing of the program in the virtual machine. 001905 ** 001906 ** The interface is the same as sqlite3VdbeExec(). But instead of 001907 ** running the code, it invokes the callback once for each instruction. 001908 ** This feature is used to implement "EXPLAIN". 001909 ** 001910 ** When p->explain==1, each instruction is listed. When 001911 ** p->explain==2, only OP_Explain instructions are listed and these 001912 ** are shown in a different format. p->explain==2 is used to implement 001913 ** EXPLAIN QUERY PLAN. 001914 ** 2018-04-24: In p->explain==2 mode, the OP_Init opcodes of triggers 001915 ** are also shown, so that the boundaries between the main program and 001916 ** each trigger are clear. 001917 ** 001918 ** When p->explain==1, first the main program is listed, then each of 001919 ** the trigger subprograms are listed one by one. 001920 */ 001921 int sqlite3VdbeList( 001922 Vdbe *p /* The VDBE */ 001923 ){ 001924 int nRow; /* Stop when row count reaches this */ 001925 int nSub = 0; /* Number of sub-vdbes seen so far */ 001926 SubProgram **apSub = 0; /* Array of sub-vdbes */ 001927 Mem *pSub = 0; /* Memory cell hold array of subprogs */ 001928 sqlite3 *db = p->db; /* The database connection */ 001929 int i; /* Loop counter */ 001930 int rc = SQLITE_OK; /* Return code */ 001931 Mem *pMem = &p->aMem[1]; /* First Mem of result set */ 001932 int bListSubprogs = (p->explain==1 || (db->flags & SQLITE_TriggerEQP)!=0); 001933 Op *pOp = 0; 001934 001935 assert( p->explain ); 001936 assert( p->magic==VDBE_MAGIC_RUN ); 001937 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); 001938 001939 /* Even though this opcode does not use dynamic strings for 001940 ** the result, result columns may become dynamic if the user calls 001941 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. 001942 */ 001943 releaseMemArray(pMem, 8); 001944 p->pResultSet = 0; 001945 001946 if( p->rc==SQLITE_NOMEM ){ 001947 /* This happens if a malloc() inside a call to sqlite3_column_text() or 001948 ** sqlite3_column_text16() failed. */ 001949 sqlite3OomFault(db); 001950 return SQLITE_ERROR; 001951 } 001952 001953 /* When the number of output rows reaches nRow, that means the 001954 ** listing has finished and sqlite3_step() should return SQLITE_DONE. 001955 ** nRow is the sum of the number of rows in the main program, plus 001956 ** the sum of the number of rows in all trigger subprograms encountered 001957 ** so far. The nRow value will increase as new trigger subprograms are 001958 ** encountered, but p->pc will eventually catch up to nRow. 001959 */ 001960 nRow = p->nOp; 001961 if( bListSubprogs ){ 001962 /* The first 8 memory cells are used for the result set. So we will 001963 ** commandeer the 9th cell to use as storage for an array of pointers 001964 ** to trigger subprograms. The VDBE is guaranteed to have at least 9 001965 ** cells. */ 001966 assert( p->nMem>9 ); 001967 pSub = &p->aMem[9]; 001968 if( pSub->flags&MEM_Blob ){ 001969 /* On the first call to sqlite3_step(), pSub will hold a NULL. It is 001970 ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */ 001971 nSub = pSub->n/sizeof(Vdbe*); 001972 apSub = (SubProgram **)pSub->z; 001973 } 001974 for(i=0; i<nSub; i++){ 001975 nRow += apSub[i]->nOp; 001976 } 001977 } 001978 001979 while(1){ /* Loop exits via break */ 001980 i = p->pc++; 001981 if( i>=nRow ){ 001982 p->rc = SQLITE_OK; 001983 rc = SQLITE_DONE; 001984 break; 001985 } 001986 if( i<p->nOp ){ 001987 /* The output line number is small enough that we are still in the 001988 ** main program. */ 001989 pOp = &p->aOp[i]; 001990 }else{ 001991 /* We are currently listing subprograms. Figure out which one and 001992 ** pick up the appropriate opcode. */ 001993 int j; 001994 i -= p->nOp; 001995 assert( apSub!=0 ); 001996 assert( nSub>0 ); 001997 for(j=0; i>=apSub[j]->nOp; j++){ 001998 i -= apSub[j]->nOp; 001999 assert( i<apSub[j]->nOp || j+1<nSub ); 002000 } 002001 pOp = &apSub[j]->aOp[i]; 002002 } 002003 002004 /* When an OP_Program opcode is encounter (the only opcode that has 002005 ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms 002006 ** kept in p->aMem[9].z to hold the new program - assuming this subprogram 002007 ** has not already been seen. 002008 */ 002009 if( bListSubprogs && pOp->p4type==P4_SUBPROGRAM ){ 002010 int nByte = (nSub+1)*sizeof(SubProgram*); 002011 int j; 002012 for(j=0; j<nSub; j++){ 002013 if( apSub[j]==pOp->p4.pProgram ) break; 002014 } 002015 if( j==nSub ){ 002016 p->rc = sqlite3VdbeMemGrow(pSub, nByte, nSub!=0); 002017 if( p->rc!=SQLITE_OK ){ 002018 rc = SQLITE_ERROR; 002019 break; 002020 } 002021 apSub = (SubProgram **)pSub->z; 002022 apSub[nSub++] = pOp->p4.pProgram; 002023 pSub->flags |= MEM_Blob; 002024 pSub->n = nSub*sizeof(SubProgram*); 002025 nRow += pOp->p4.pProgram->nOp; 002026 } 002027 } 002028 if( p->explain<2 ) break; 002029 if( pOp->opcode==OP_Explain ) break; 002030 if( pOp->opcode==OP_Init && p->pc>1 ) break; 002031 } 002032 002033 if( rc==SQLITE_OK ){ 002034 if( db->u1.isInterrupted ){ 002035 p->rc = SQLITE_INTERRUPT; 002036 rc = SQLITE_ERROR; 002037 sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); 002038 }else{ 002039 char *zP4; 002040 if( p->explain==1 ){ 002041 pMem->flags = MEM_Int; 002042 pMem->u.i = i; /* Program counter */ 002043 pMem++; 002044 002045 pMem->flags = MEM_Static|MEM_Str|MEM_Term; 002046 pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */ 002047 assert( pMem->z!=0 ); 002048 pMem->n = sqlite3Strlen30(pMem->z); 002049 pMem->enc = SQLITE_UTF8; 002050 pMem++; 002051 } 002052 002053 pMem->flags = MEM_Int; 002054 pMem->u.i = pOp->p1; /* P1 */ 002055 pMem++; 002056 002057 pMem->flags = MEM_Int; 002058 pMem->u.i = pOp->p2; /* P2 */ 002059 pMem++; 002060 002061 pMem->flags = MEM_Int; 002062 pMem->u.i = pOp->p3; /* P3 */ 002063 pMem++; 002064 002065 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ 002066 assert( p->db->mallocFailed ); 002067 return SQLITE_ERROR; 002068 } 002069 pMem->flags = MEM_Str|MEM_Term; 002070 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); 002071 if( zP4!=pMem->z ){ 002072 pMem->n = 0; 002073 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); 002074 }else{ 002075 assert( pMem->z!=0 ); 002076 pMem->n = sqlite3Strlen30(pMem->z); 002077 pMem->enc = SQLITE_UTF8; 002078 } 002079 pMem++; 002080 002081 if( p->explain==1 ){ 002082 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ 002083 assert( p->db->mallocFailed ); 002084 return SQLITE_ERROR; 002085 } 002086 pMem->flags = MEM_Str|MEM_Term; 002087 pMem->n = 2; 002088 sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5); /* P5 */ 002089 pMem->enc = SQLITE_UTF8; 002090 pMem++; 002091 002092 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS 002093 if( sqlite3VdbeMemClearAndResize(pMem, 500) ){ 002094 assert( p->db->mallocFailed ); 002095 return SQLITE_ERROR; 002096 } 002097 pMem->flags = MEM_Str|MEM_Term; 002098 pMem->n = displayComment(pOp, zP4, pMem->z, 500); 002099 pMem->enc = SQLITE_UTF8; 002100 #else 002101 pMem->flags = MEM_Null; /* Comment */ 002102 #endif 002103 } 002104 002105 p->nResColumn = 8 - 4*(p->explain-1); 002106 p->pResultSet = &p->aMem[1]; 002107 p->rc = SQLITE_OK; 002108 rc = SQLITE_ROW; 002109 } 002110 } 002111 return rc; 002112 } 002113 #endif /* SQLITE_OMIT_EXPLAIN */ 002114 002115 #ifdef SQLITE_DEBUG 002116 /* 002117 ** Print the SQL that was used to generate a VDBE program. 002118 */ 002119 void sqlite3VdbePrintSql(Vdbe *p){ 002120 const char *z = 0; 002121 if( p->zSql ){ 002122 z = p->zSql; 002123 }else if( p->nOp>=1 ){ 002124 const VdbeOp *pOp = &p->aOp[0]; 002125 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ 002126 z = pOp->p4.z; 002127 while( sqlite3Isspace(*z) ) z++; 002128 } 002129 } 002130 if( z ) printf("SQL: [%s]\n", z); 002131 } 002132 #endif 002133 002134 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) 002135 /* 002136 ** Print an IOTRACE message showing SQL content. 002137 */ 002138 void sqlite3VdbeIOTraceSql(Vdbe *p){ 002139 int nOp = p->nOp; 002140 VdbeOp *pOp; 002141 if( sqlite3IoTrace==0 ) return; 002142 if( nOp<1 ) return; 002143 pOp = &p->aOp[0]; 002144 if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){ 002145 int i, j; 002146 char z[1000]; 002147 sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z); 002148 for(i=0; sqlite3Isspace(z[i]); i++){} 002149 for(j=0; z[i]; i++){ 002150 if( sqlite3Isspace(z[i]) ){ 002151 if( z[i-1]!=' ' ){ 002152 z[j++] = ' '; 002153 } 002154 }else{ 002155 z[j++] = z[i]; 002156 } 002157 } 002158 z[j] = 0; 002159 sqlite3IoTrace("SQL %s\n", z); 002160 } 002161 } 002162 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ 002163 002164 /* An instance of this object describes bulk memory available for use 002165 ** by subcomponents of a prepared statement. Space is allocated out 002166 ** of a ReusableSpace object by the allocSpace() routine below. 002167 */ 002168 struct ReusableSpace { 002169 u8 *pSpace; /* Available memory */ 002170 sqlite3_int64 nFree; /* Bytes of available memory */ 002171 sqlite3_int64 nNeeded; /* Total bytes that could not be allocated */ 002172 }; 002173 002174 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf 002175 ** from the ReusableSpace object. Return a pointer to the allocated 002176 ** memory on success. If insufficient memory is available in the 002177 ** ReusableSpace object, increase the ReusableSpace.nNeeded 002178 ** value by the amount needed and return NULL. 002179 ** 002180 ** If pBuf is not initially NULL, that means that the memory has already 002181 ** been allocated by a prior call to this routine, so just return a copy 002182 ** of pBuf and leave ReusableSpace unchanged. 002183 ** 002184 ** This allocator is employed to repurpose unused slots at the end of the 002185 ** opcode array of prepared state for other memory needs of the prepared 002186 ** statement. 002187 */ 002188 static void *allocSpace( 002189 struct ReusableSpace *p, /* Bulk memory available for allocation */ 002190 void *pBuf, /* Pointer to a prior allocation */ 002191 sqlite3_int64 nByte /* Bytes of memory needed */ 002192 ){ 002193 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) ); 002194 if( pBuf==0 ){ 002195 nByte = ROUND8(nByte); 002196 if( nByte <= p->nFree ){ 002197 p->nFree -= nByte; 002198 pBuf = &p->pSpace[p->nFree]; 002199 }else{ 002200 p->nNeeded += nByte; 002201 } 002202 } 002203 assert( EIGHT_BYTE_ALIGNMENT(pBuf) ); 002204 return pBuf; 002205 } 002206 002207 /* 002208 ** Rewind the VDBE back to the beginning in preparation for 002209 ** running it. 002210 */ 002211 void sqlite3VdbeRewind(Vdbe *p){ 002212 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 002213 int i; 002214 #endif 002215 assert( p!=0 ); 002216 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET ); 002217 002218 /* There should be at least one opcode. 002219 */ 002220 assert( p->nOp>0 ); 002221 002222 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ 002223 p->magic = VDBE_MAGIC_RUN; 002224 002225 #ifdef SQLITE_DEBUG 002226 for(i=0; i<p->nMem; i++){ 002227 assert( p->aMem[i].db==p->db ); 002228 } 002229 #endif 002230 p->pc = -1; 002231 p->rc = SQLITE_OK; 002232 p->errorAction = OE_Abort; 002233 p->nChange = 0; 002234 p->cacheCtr = 1; 002235 p->minWriteFileFormat = 255; 002236 p->iStatement = 0; 002237 p->nFkConstraint = 0; 002238 #ifdef VDBE_PROFILE 002239 for(i=0; i<p->nOp; i++){ 002240 p->aOp[i].cnt = 0; 002241 p->aOp[i].cycles = 0; 002242 } 002243 #endif 002244 } 002245 002246 /* 002247 ** Prepare a virtual machine for execution for the first time after 002248 ** creating the virtual machine. This involves things such 002249 ** as allocating registers and initializing the program counter. 002250 ** After the VDBE has be prepped, it can be executed by one or more 002251 ** calls to sqlite3VdbeExec(). 002252 ** 002253 ** This function may be called exactly once on each virtual machine. 002254 ** After this routine is called the VM has been "packaged" and is ready 002255 ** to run. After this routine is called, further calls to 002256 ** sqlite3VdbeAddOp() functions are prohibited. This routine disconnects 002257 ** the Vdbe from the Parse object that helped generate it so that the 002258 ** the Vdbe becomes an independent entity and the Parse object can be 002259 ** destroyed. 002260 ** 002261 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back 002262 ** to its initial state after it has been run. 002263 */ 002264 void sqlite3VdbeMakeReady( 002265 Vdbe *p, /* The VDBE */ 002266 Parse *pParse /* Parsing context */ 002267 ){ 002268 sqlite3 *db; /* The database connection */ 002269 int nVar; /* Number of parameters */ 002270 int nMem; /* Number of VM memory registers */ 002271 int nCursor; /* Number of cursors required */ 002272 int nArg; /* Number of arguments in subprograms */ 002273 int n; /* Loop counter */ 002274 struct ReusableSpace x; /* Reusable bulk memory */ 002275 002276 assert( p!=0 ); 002277 assert( p->nOp>0 ); 002278 assert( pParse!=0 ); 002279 assert( p->magic==VDBE_MAGIC_INIT ); 002280 assert( pParse==p->pParse ); 002281 db = p->db; 002282 assert( db->mallocFailed==0 ); 002283 nVar = pParse->nVar; 002284 nMem = pParse->nMem; 002285 nCursor = pParse->nTab; 002286 nArg = pParse->nMaxArg; 002287 002288 /* Each cursor uses a memory cell. The first cursor (cursor 0) can 002289 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate 002290 ** space at the end of aMem[] for cursors 1 and greater. 002291 ** See also: allocateCursor(). 002292 */ 002293 nMem += nCursor; 002294 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */ 002295 002296 /* Figure out how much reusable memory is available at the end of the 002297 ** opcode array. This extra memory will be reallocated for other elements 002298 ** of the prepared statement. 002299 */ 002300 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */ 002301 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */ 002302 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) ); 002303 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */ 002304 assert( x.nFree>=0 ); 002305 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) ); 002306 002307 resolveP2Values(p, &nArg); 002308 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); 002309 if( pParse->explain ){ 002310 static const char * const azColName[] = { 002311 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", 002312 "id", "parent", "notused", "detail" 002313 }; 002314 int iFirst, mx, i; 002315 if( nMem<10 ) nMem = 10; 002316 if( pParse->explain==2 ){ 002317 sqlite3VdbeSetNumCols(p, 4); 002318 iFirst = 8; 002319 mx = 12; 002320 }else{ 002321 sqlite3VdbeSetNumCols(p, 8); 002322 iFirst = 0; 002323 mx = 8; 002324 } 002325 for(i=iFirst; i<mx; i++){ 002326 sqlite3VdbeSetColName(p, i-iFirst, COLNAME_NAME, 002327 azColName[i], SQLITE_STATIC); 002328 } 002329 } 002330 p->expired = 0; 002331 002332 /* Memory for registers, parameters, cursor, etc, is allocated in one or two 002333 ** passes. On the first pass, we try to reuse unused memory at the 002334 ** end of the opcode array. If we are unable to satisfy all memory 002335 ** requirements by reusing the opcode array tail, then the second 002336 ** pass will fill in the remainder using a fresh memory allocation. 002337 ** 002338 ** This two-pass approach that reuses as much memory as possible from 002339 ** the leftover memory at the end of the opcode array. This can significantly 002340 ** reduce the amount of memory held by a prepared statement. 002341 */ 002342 x.nNeeded = 0; 002343 p->aMem = allocSpace(&x, 0, nMem*sizeof(Mem)); 002344 p->aVar = allocSpace(&x, 0, nVar*sizeof(Mem)); 002345 p->apArg = allocSpace(&x, 0, nArg*sizeof(Mem*)); 002346 p->apCsr = allocSpace(&x, 0, nCursor*sizeof(VdbeCursor*)); 002347 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002348 p->anExec = allocSpace(&x, 0, p->nOp*sizeof(i64)); 002349 #endif 002350 if( x.nNeeded ){ 002351 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded); 002352 x.nFree = x.nNeeded; 002353 if( !db->mallocFailed ){ 002354 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem)); 002355 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); 002356 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); 002357 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); 002358 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002359 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64)); 002360 #endif 002361 } 002362 } 002363 002364 p->pVList = pParse->pVList; 002365 pParse->pVList = 0; 002366 p->explain = pParse->explain; 002367 if( db->mallocFailed ){ 002368 p->nVar = 0; 002369 p->nCursor = 0; 002370 p->nMem = 0; 002371 }else{ 002372 p->nCursor = nCursor; 002373 p->nVar = (ynVar)nVar; 002374 initMemArray(p->aVar, nVar, db, MEM_Null); 002375 p->nMem = nMem; 002376 initMemArray(p->aMem, nMem, db, MEM_Undefined); 002377 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*)); 002378 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002379 memset(p->anExec, 0, p->nOp*sizeof(i64)); 002380 #endif 002381 } 002382 sqlite3VdbeRewind(p); 002383 } 002384 002385 /* 002386 ** Close a VDBE cursor and release all the resources that cursor 002387 ** happens to hold. 002388 */ 002389 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ 002390 if( pCx==0 ){ 002391 return; 002392 } 002393 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE ); 002394 switch( pCx->eCurType ){ 002395 case CURTYPE_SORTER: { 002396 sqlite3VdbeSorterClose(p->db, pCx); 002397 break; 002398 } 002399 case CURTYPE_BTREE: { 002400 if( pCx->isEphemeral ){ 002401 if( pCx->pBtx ) sqlite3BtreeClose(pCx->pBtx); 002402 /* The pCx->pCursor will be close automatically, if it exists, by 002403 ** the call above. */ 002404 }else{ 002405 assert( pCx->uc.pCursor!=0 ); 002406 sqlite3BtreeCloseCursor(pCx->uc.pCursor); 002407 } 002408 break; 002409 } 002410 #ifndef SQLITE_OMIT_VIRTUALTABLE 002411 case CURTYPE_VTAB: { 002412 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur; 002413 const sqlite3_module *pModule = pVCur->pVtab->pModule; 002414 assert( pVCur->pVtab->nRef>0 ); 002415 pVCur->pVtab->nRef--; 002416 pModule->xClose(pVCur); 002417 break; 002418 } 002419 #endif 002420 } 002421 } 002422 002423 /* 002424 ** Close all cursors in the current frame. 002425 */ 002426 static void closeCursorsInFrame(Vdbe *p){ 002427 if( p->apCsr ){ 002428 int i; 002429 for(i=0; i<p->nCursor; i++){ 002430 VdbeCursor *pC = p->apCsr[i]; 002431 if( pC ){ 002432 sqlite3VdbeFreeCursor(p, pC); 002433 p->apCsr[i] = 0; 002434 } 002435 } 002436 } 002437 } 002438 002439 /* 002440 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This 002441 ** is used, for example, when a trigger sub-program is halted to restore 002442 ** control to the main program. 002443 */ 002444 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ 002445 Vdbe *v = pFrame->v; 002446 closeCursorsInFrame(v); 002447 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002448 v->anExec = pFrame->anExec; 002449 #endif 002450 v->aOp = pFrame->aOp; 002451 v->nOp = pFrame->nOp; 002452 v->aMem = pFrame->aMem; 002453 v->nMem = pFrame->nMem; 002454 v->apCsr = pFrame->apCsr; 002455 v->nCursor = pFrame->nCursor; 002456 v->db->lastRowid = pFrame->lastRowid; 002457 v->nChange = pFrame->nChange; 002458 v->db->nChange = pFrame->nDbChange; 002459 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0); 002460 v->pAuxData = pFrame->pAuxData; 002461 pFrame->pAuxData = 0; 002462 return pFrame->pc; 002463 } 002464 002465 /* 002466 ** Close all cursors. 002467 ** 002468 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory 002469 ** cell array. This is necessary as the memory cell array may contain 002470 ** pointers to VdbeFrame objects, which may in turn contain pointers to 002471 ** open cursors. 002472 */ 002473 static void closeAllCursors(Vdbe *p){ 002474 if( p->pFrame ){ 002475 VdbeFrame *pFrame; 002476 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 002477 sqlite3VdbeFrameRestore(pFrame); 002478 p->pFrame = 0; 002479 p->nFrame = 0; 002480 } 002481 assert( p->nFrame==0 ); 002482 closeCursorsInFrame(p); 002483 if( p->aMem ){ 002484 releaseMemArray(p->aMem, p->nMem); 002485 } 002486 while( p->pDelFrame ){ 002487 VdbeFrame *pDel = p->pDelFrame; 002488 p->pDelFrame = pDel->pParent; 002489 sqlite3VdbeFrameDelete(pDel); 002490 } 002491 002492 /* Delete any auxdata allocations made by the VM */ 002493 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0); 002494 assert( p->pAuxData==0 ); 002495 } 002496 002497 /* 002498 ** Set the number of result columns that will be returned by this SQL 002499 ** statement. This is now set at compile time, rather than during 002500 ** execution of the vdbe program so that sqlite3_column_count() can 002501 ** be called on an SQL statement before sqlite3_step(). 002502 */ 002503 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ 002504 int n; 002505 sqlite3 *db = p->db; 002506 002507 if( p->nResColumn ){ 002508 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 002509 sqlite3DbFree(db, p->aColName); 002510 } 002511 n = nResColumn*COLNAME_N; 002512 p->nResColumn = (u16)nResColumn; 002513 p->aColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n ); 002514 if( p->aColName==0 ) return; 002515 initMemArray(p->aColName, n, db, MEM_Null); 002516 } 002517 002518 /* 002519 ** Set the name of the idx'th column to be returned by the SQL statement. 002520 ** zName must be a pointer to a nul terminated string. 002521 ** 002522 ** This call must be made after a call to sqlite3VdbeSetNumCols(). 002523 ** 002524 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC 002525 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed 002526 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. 002527 */ 002528 int sqlite3VdbeSetColName( 002529 Vdbe *p, /* Vdbe being configured */ 002530 int idx, /* Index of column zName applies to */ 002531 int var, /* One of the COLNAME_* constants */ 002532 const char *zName, /* Pointer to buffer containing name */ 002533 void (*xDel)(void*) /* Memory management strategy for zName */ 002534 ){ 002535 int rc; 002536 Mem *pColName; 002537 assert( idx<p->nResColumn ); 002538 assert( var<COLNAME_N ); 002539 if( p->db->mallocFailed ){ 002540 assert( !zName || xDel!=SQLITE_DYNAMIC ); 002541 return SQLITE_NOMEM_BKPT; 002542 } 002543 assert( p->aColName!=0 ); 002544 pColName = &(p->aColName[idx+var*p->nResColumn]); 002545 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); 002546 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); 002547 return rc; 002548 } 002549 002550 /* 002551 ** A read or write transaction may or may not be active on database handle 002552 ** db. If a transaction is active, commit it. If there is a 002553 ** write-transaction spanning more than one database file, this routine 002554 ** takes care of the master journal trickery. 002555 */ 002556 static int vdbeCommit(sqlite3 *db, Vdbe *p){ 002557 int i; 002558 int nTrans = 0; /* Number of databases with an active write-transaction 002559 ** that are candidates for a two-phase commit using a 002560 ** master-journal */ 002561 int rc = SQLITE_OK; 002562 int needXcommit = 0; 002563 002564 #ifdef SQLITE_OMIT_VIRTUALTABLE 002565 /* With this option, sqlite3VtabSync() is defined to be simply 002566 ** SQLITE_OK so p is not used. 002567 */ 002568 UNUSED_PARAMETER(p); 002569 #endif 002570 002571 /* Before doing anything else, call the xSync() callback for any 002572 ** virtual module tables written in this transaction. This has to 002573 ** be done before determining whether a master journal file is 002574 ** required, as an xSync() callback may add an attached database 002575 ** to the transaction. 002576 */ 002577 rc = sqlite3VtabSync(db, p); 002578 002579 /* This loop determines (a) if the commit hook should be invoked and 002580 ** (b) how many database files have open write transactions, not 002581 ** including the temp database. (b) is important because if more than 002582 ** one database file has an open write transaction, a master journal 002583 ** file is required for an atomic commit. 002584 */ 002585 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 002586 Btree *pBt = db->aDb[i].pBt; 002587 if( sqlite3BtreeIsInTrans(pBt) ){ 002588 /* Whether or not a database might need a master journal depends upon 002589 ** its journal mode (among other things). This matrix determines which 002590 ** journal modes use a master journal and which do not */ 002591 static const u8 aMJNeeded[] = { 002592 /* DELETE */ 1, 002593 /* PERSIST */ 1, 002594 /* OFF */ 0, 002595 /* TRUNCATE */ 1, 002596 /* MEMORY */ 0, 002597 /* WAL */ 0 002598 }; 002599 Pager *pPager; /* Pager associated with pBt */ 002600 needXcommit = 1; 002601 sqlite3BtreeEnter(pBt); 002602 pPager = sqlite3BtreePager(pBt); 002603 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF 002604 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)] 002605 && sqlite3PagerIsMemdb(pPager)==0 002606 ){ 002607 assert( i!=1 ); 002608 nTrans++; 002609 } 002610 rc = sqlite3PagerExclusiveLock(pPager); 002611 sqlite3BtreeLeave(pBt); 002612 } 002613 } 002614 if( rc!=SQLITE_OK ){ 002615 return rc; 002616 } 002617 002618 /* If there are any write-transactions at all, invoke the commit hook */ 002619 if( needXcommit && db->xCommitCallback ){ 002620 rc = db->xCommitCallback(db->pCommitArg); 002621 if( rc ){ 002622 return SQLITE_CONSTRAINT_COMMITHOOK; 002623 } 002624 } 002625 002626 /* The simple case - no more than one database file (not counting the 002627 ** TEMP database) has a transaction active. There is no need for the 002628 ** master-journal. 002629 ** 002630 ** If the return value of sqlite3BtreeGetFilename() is a zero length 002631 ** string, it means the main database is :memory: or a temp file. In 002632 ** that case we do not support atomic multi-file commits, so use the 002633 ** simple case then too. 002634 */ 002635 if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt)) 002636 || nTrans<=1 002637 ){ 002638 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 002639 Btree *pBt = db->aDb[i].pBt; 002640 if( pBt ){ 002641 rc = sqlite3BtreeCommitPhaseOne(pBt, 0); 002642 } 002643 } 002644 002645 /* Do the commit only if all databases successfully complete phase 1. 002646 ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an 002647 ** IO error while deleting or truncating a journal file. It is unlikely, 002648 ** but could happen. In this case abandon processing and return the error. 002649 */ 002650 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 002651 Btree *pBt = db->aDb[i].pBt; 002652 if( pBt ){ 002653 rc = sqlite3BtreeCommitPhaseTwo(pBt, 0); 002654 } 002655 } 002656 if( rc==SQLITE_OK ){ 002657 sqlite3VtabCommit(db); 002658 } 002659 } 002660 002661 /* The complex case - There is a multi-file write-transaction active. 002662 ** This requires a master journal file to ensure the transaction is 002663 ** committed atomically. 002664 */ 002665 #ifndef SQLITE_OMIT_DISKIO 002666 else{ 002667 sqlite3_vfs *pVfs = db->pVfs; 002668 char *zMaster = 0; /* File-name for the master journal */ 002669 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); 002670 sqlite3_file *pMaster = 0; 002671 i64 offset = 0; 002672 int res; 002673 int retryCount = 0; 002674 int nMainFile; 002675 002676 /* Select a master journal file name */ 002677 nMainFile = sqlite3Strlen30(zMainFile); 002678 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz%c%c", zMainFile, 0, 0); 002679 if( zMaster==0 ) return SQLITE_NOMEM_BKPT; 002680 do { 002681 u32 iRandom; 002682 if( retryCount ){ 002683 if( retryCount>100 ){ 002684 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); 002685 sqlite3OsDelete(pVfs, zMaster, 0); 002686 break; 002687 }else if( retryCount==1 ){ 002688 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); 002689 } 002690 } 002691 retryCount++; 002692 sqlite3_randomness(sizeof(iRandom), &iRandom); 002693 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X", 002694 (iRandom>>8)&0xffffff, iRandom&0xff); 002695 /* The antipenultimate character of the master journal name must 002696 ** be "9" to avoid name collisions when using 8+3 filenames. */ 002697 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' ); 002698 sqlite3FileSuffix3(zMainFile, zMaster); 002699 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res); 002700 }while( rc==SQLITE_OK && res ); 002701 if( rc==SQLITE_OK ){ 002702 /* Open the master journal. */ 002703 rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster, 002704 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE| 002705 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0 002706 ); 002707 } 002708 if( rc!=SQLITE_OK ){ 002709 sqlite3DbFree(db, zMaster); 002710 return rc; 002711 } 002712 002713 /* Write the name of each database file in the transaction into the new 002714 ** master journal file. If an error occurs at this point close 002715 ** and delete the master journal file. All the individual journal files 002716 ** still have 'null' as the master journal pointer, so they will roll 002717 ** back independently if a failure occurs. 002718 */ 002719 for(i=0; i<db->nDb; i++){ 002720 Btree *pBt = db->aDb[i].pBt; 002721 if( sqlite3BtreeIsInTrans(pBt) ){ 002722 char const *zFile = sqlite3BtreeGetJournalname(pBt); 002723 if( zFile==0 ){ 002724 continue; /* Ignore TEMP and :memory: databases */ 002725 } 002726 assert( zFile[0]!=0 ); 002727 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); 002728 offset += sqlite3Strlen30(zFile)+1; 002729 if( rc!=SQLITE_OK ){ 002730 sqlite3OsCloseFree(pMaster); 002731 sqlite3OsDelete(pVfs, zMaster, 0); 002732 sqlite3DbFree(db, zMaster); 002733 return rc; 002734 } 002735 } 002736 } 002737 002738 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device 002739 ** flag is set this is not required. 002740 */ 002741 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) 002742 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) 002743 ){ 002744 sqlite3OsCloseFree(pMaster); 002745 sqlite3OsDelete(pVfs, zMaster, 0); 002746 sqlite3DbFree(db, zMaster); 002747 return rc; 002748 } 002749 002750 /* Sync all the db files involved in the transaction. The same call 002751 ** sets the master journal pointer in each individual journal. If 002752 ** an error occurs here, do not delete the master journal file. 002753 ** 002754 ** If the error occurs during the first call to 002755 ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the 002756 ** master journal file will be orphaned. But we cannot delete it, 002757 ** in case the master journal file name was written into the journal 002758 ** file before the failure occurred. 002759 */ 002760 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ 002761 Btree *pBt = db->aDb[i].pBt; 002762 if( pBt ){ 002763 rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster); 002764 } 002765 } 002766 sqlite3OsCloseFree(pMaster); 002767 assert( rc!=SQLITE_BUSY ); 002768 if( rc!=SQLITE_OK ){ 002769 sqlite3DbFree(db, zMaster); 002770 return rc; 002771 } 002772 002773 /* Delete the master journal file. This commits the transaction. After 002774 ** doing this the directory is synced again before any individual 002775 ** transaction files are deleted. 002776 */ 002777 rc = sqlite3OsDelete(pVfs, zMaster, 1); 002778 sqlite3DbFree(db, zMaster); 002779 zMaster = 0; 002780 if( rc ){ 002781 return rc; 002782 } 002783 002784 /* All files and directories have already been synced, so the following 002785 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and 002786 ** deleting or truncating journals. If something goes wrong while 002787 ** this is happening we don't really care. The integrity of the 002788 ** transaction is already guaranteed, but some stray 'cold' journals 002789 ** may be lying around. Returning an error code won't help matters. 002790 */ 002791 disable_simulated_io_errors(); 002792 sqlite3BeginBenignMalloc(); 002793 for(i=0; i<db->nDb; i++){ 002794 Btree *pBt = db->aDb[i].pBt; 002795 if( pBt ){ 002796 sqlite3BtreeCommitPhaseTwo(pBt, 1); 002797 } 002798 } 002799 sqlite3EndBenignMalloc(); 002800 enable_simulated_io_errors(); 002801 002802 sqlite3VtabCommit(db); 002803 } 002804 #endif 002805 002806 return rc; 002807 } 002808 002809 /* 002810 ** This routine checks that the sqlite3.nVdbeActive count variable 002811 ** matches the number of vdbe's in the list sqlite3.pVdbe that are 002812 ** currently active. An assertion fails if the two counts do not match. 002813 ** This is an internal self-check only - it is not an essential processing 002814 ** step. 002815 ** 002816 ** This is a no-op if NDEBUG is defined. 002817 */ 002818 #ifndef NDEBUG 002819 static void checkActiveVdbeCnt(sqlite3 *db){ 002820 Vdbe *p; 002821 int cnt = 0; 002822 int nWrite = 0; 002823 int nRead = 0; 002824 p = db->pVdbe; 002825 while( p ){ 002826 if( sqlite3_stmt_busy((sqlite3_stmt*)p) ){ 002827 cnt++; 002828 if( p->readOnly==0 ) nWrite++; 002829 if( p->bIsReader ) nRead++; 002830 } 002831 p = p->pNext; 002832 } 002833 assert( cnt==db->nVdbeActive ); 002834 assert( nWrite==db->nVdbeWrite ); 002835 assert( nRead==db->nVdbeRead ); 002836 } 002837 #else 002838 #define checkActiveVdbeCnt(x) 002839 #endif 002840 002841 /* 002842 ** If the Vdbe passed as the first argument opened a statement-transaction, 002843 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or 002844 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement 002845 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the 002846 ** statement transaction is committed. 002847 ** 002848 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. 002849 ** Otherwise SQLITE_OK. 002850 */ 002851 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){ 002852 sqlite3 *const db = p->db; 002853 int rc = SQLITE_OK; 002854 int i; 002855 const int iSavepoint = p->iStatement-1; 002856 002857 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); 002858 assert( db->nStatement>0 ); 002859 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); 002860 002861 for(i=0; i<db->nDb; i++){ 002862 int rc2 = SQLITE_OK; 002863 Btree *pBt = db->aDb[i].pBt; 002864 if( pBt ){ 002865 if( eOp==SAVEPOINT_ROLLBACK ){ 002866 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); 002867 } 002868 if( rc2==SQLITE_OK ){ 002869 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); 002870 } 002871 if( rc==SQLITE_OK ){ 002872 rc = rc2; 002873 } 002874 } 002875 } 002876 db->nStatement--; 002877 p->iStatement = 0; 002878 002879 if( rc==SQLITE_OK ){ 002880 if( eOp==SAVEPOINT_ROLLBACK ){ 002881 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); 002882 } 002883 if( rc==SQLITE_OK ){ 002884 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); 002885 } 002886 } 002887 002888 /* If the statement transaction is being rolled back, also restore the 002889 ** database handles deferred constraint counter to the value it had when 002890 ** the statement transaction was opened. */ 002891 if( eOp==SAVEPOINT_ROLLBACK ){ 002892 db->nDeferredCons = p->nStmtDefCons; 002893 db->nDeferredImmCons = p->nStmtDefImmCons; 002894 } 002895 return rc; 002896 } 002897 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ 002898 if( p->db->nStatement && p->iStatement ){ 002899 return vdbeCloseStatement(p, eOp); 002900 } 002901 return SQLITE_OK; 002902 } 002903 002904 002905 /* 002906 ** This function is called when a transaction opened by the database 002907 ** handle associated with the VM passed as an argument is about to be 002908 ** committed. If there are outstanding deferred foreign key constraint 002909 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. 002910 ** 002911 ** If there are outstanding FK violations and this function returns 002912 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY 002913 ** and write an error message to it. Then return SQLITE_ERROR. 002914 */ 002915 #ifndef SQLITE_OMIT_FOREIGN_KEY 002916 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ 002917 sqlite3 *db = p->db; 002918 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) 002919 || (!deferred && p->nFkConstraint>0) 002920 ){ 002921 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; 002922 p->errorAction = OE_Abort; 002923 sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); 002924 return SQLITE_ERROR; 002925 } 002926 return SQLITE_OK; 002927 } 002928 #endif 002929 002930 /* 002931 ** This routine is called the when a VDBE tries to halt. If the VDBE 002932 ** has made changes and is in autocommit mode, then commit those 002933 ** changes. If a rollback is needed, then do the rollback. 002934 ** 002935 ** This routine is the only way to move the state of a VM from 002936 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT. It is harmless to 002937 ** call this on a VM that is in the SQLITE_MAGIC_HALT state. 002938 ** 002939 ** Return an error code. If the commit could not complete because of 002940 ** lock contention, return SQLITE_BUSY. If SQLITE_BUSY is returned, it 002941 ** means the close did not happen and needs to be repeated. 002942 */ 002943 int sqlite3VdbeHalt(Vdbe *p){ 002944 int rc; /* Used to store transient return codes */ 002945 sqlite3 *db = p->db; 002946 002947 /* This function contains the logic that determines if a statement or 002948 ** transaction will be committed or rolled back as a result of the 002949 ** execution of this virtual machine. 002950 ** 002951 ** If any of the following errors occur: 002952 ** 002953 ** SQLITE_NOMEM 002954 ** SQLITE_IOERR 002955 ** SQLITE_FULL 002956 ** SQLITE_INTERRUPT 002957 ** 002958 ** Then the internal cache might have been left in an inconsistent 002959 ** state. We need to rollback the statement transaction, if there is 002960 ** one, or the complete transaction if there is no statement transaction. 002961 */ 002962 002963 if( p->magic!=VDBE_MAGIC_RUN ){ 002964 return SQLITE_OK; 002965 } 002966 if( db->mallocFailed ){ 002967 p->rc = SQLITE_NOMEM_BKPT; 002968 } 002969 closeAllCursors(p); 002970 checkActiveVdbeCnt(db); 002971 002972 /* No commit or rollback needed if the program never started or if the 002973 ** SQL statement does not read or write a database file. */ 002974 if( p->pc>=0 && p->bIsReader ){ 002975 int mrc; /* Primary error code from p->rc */ 002976 int eStatementOp = 0; 002977 int isSpecialError; /* Set to true if a 'special' error */ 002978 002979 /* Lock all btrees used by the statement */ 002980 sqlite3VdbeEnter(p); 002981 002982 /* Check for one of the special errors */ 002983 mrc = p->rc & 0xff; 002984 isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR 002985 || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL; 002986 if( isSpecialError ){ 002987 /* If the query was read-only and the error code is SQLITE_INTERRUPT, 002988 ** no rollback is necessary. Otherwise, at least a savepoint 002989 ** transaction must be rolled back to restore the database to a 002990 ** consistent state. 002991 ** 002992 ** Even if the statement is read-only, it is important to perform 002993 ** a statement or transaction rollback operation. If the error 002994 ** occurred while writing to the journal, sub-journal or database 002995 ** file as part of an effort to free up cache space (see function 002996 ** pagerStress() in pager.c), the rollback is required to restore 002997 ** the pager to a consistent state. 002998 */ 002999 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ 003000 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ 003001 eStatementOp = SAVEPOINT_ROLLBACK; 003002 }else{ 003003 /* We are forced to roll back the active transaction. Before doing 003004 ** so, abort any other statements this handle currently has active. 003005 */ 003006 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 003007 sqlite3CloseSavepoints(db); 003008 db->autoCommit = 1; 003009 p->nChange = 0; 003010 } 003011 } 003012 } 003013 003014 /* Check for immediate foreign key violations. */ 003015 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ 003016 sqlite3VdbeCheckFk(p, 0); 003017 } 003018 003019 /* If the auto-commit flag is set and this is the only active writer 003020 ** VM, then we do either a commit or rollback of the current transaction. 003021 ** 003022 ** Note: This block also runs if one of the special errors handled 003023 ** above has occurred. 003024 */ 003025 if( !sqlite3VtabInSync(db) 003026 && db->autoCommit 003027 && db->nVdbeWrite==(p->readOnly==0) 003028 ){ 003029 if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){ 003030 rc = sqlite3VdbeCheckFk(p, 1); 003031 if( rc!=SQLITE_OK ){ 003032 if( NEVER(p->readOnly) ){ 003033 sqlite3VdbeLeave(p); 003034 return SQLITE_ERROR; 003035 } 003036 rc = SQLITE_CONSTRAINT_FOREIGNKEY; 003037 }else{ 003038 /* The auto-commit flag is true, the vdbe program was successful 003039 ** or hit an 'OR FAIL' constraint and there are no deferred foreign 003040 ** key constraints to hold up the transaction. This means a commit 003041 ** is required. */ 003042 rc = vdbeCommit(db, p); 003043 } 003044 if( rc==SQLITE_BUSY && p->readOnly ){ 003045 sqlite3VdbeLeave(p); 003046 return SQLITE_BUSY; 003047 }else if( rc!=SQLITE_OK ){ 003048 p->rc = rc; 003049 sqlite3RollbackAll(db, SQLITE_OK); 003050 p->nChange = 0; 003051 }else{ 003052 db->nDeferredCons = 0; 003053 db->nDeferredImmCons = 0; 003054 db->flags &= ~(u64)SQLITE_DeferFKs; 003055 sqlite3CommitInternalChanges(db); 003056 } 003057 }else{ 003058 sqlite3RollbackAll(db, SQLITE_OK); 003059 p->nChange = 0; 003060 } 003061 db->nStatement = 0; 003062 }else if( eStatementOp==0 ){ 003063 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ 003064 eStatementOp = SAVEPOINT_RELEASE; 003065 }else if( p->errorAction==OE_Abort ){ 003066 eStatementOp = SAVEPOINT_ROLLBACK; 003067 }else{ 003068 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 003069 sqlite3CloseSavepoints(db); 003070 db->autoCommit = 1; 003071 p->nChange = 0; 003072 } 003073 } 003074 003075 /* If eStatementOp is non-zero, then a statement transaction needs to 003076 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to 003077 ** do so. If this operation returns an error, and the current statement 003078 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the 003079 ** current statement error code. 003080 */ 003081 if( eStatementOp ){ 003082 rc = sqlite3VdbeCloseStatement(p, eStatementOp); 003083 if( rc ){ 003084 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ 003085 p->rc = rc; 003086 sqlite3DbFree(db, p->zErrMsg); 003087 p->zErrMsg = 0; 003088 } 003089 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); 003090 sqlite3CloseSavepoints(db); 003091 db->autoCommit = 1; 003092 p->nChange = 0; 003093 } 003094 } 003095 003096 /* If this was an INSERT, UPDATE or DELETE and no statement transaction 003097 ** has been rolled back, update the database connection change-counter. 003098 */ 003099 if( p->changeCntOn ){ 003100 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ 003101 sqlite3VdbeSetChanges(db, p->nChange); 003102 }else{ 003103 sqlite3VdbeSetChanges(db, 0); 003104 } 003105 p->nChange = 0; 003106 } 003107 003108 /* Release the locks */ 003109 sqlite3VdbeLeave(p); 003110 } 003111 003112 /* We have successfully halted and closed the VM. Record this fact. */ 003113 if( p->pc>=0 ){ 003114 db->nVdbeActive--; 003115 if( !p->readOnly ) db->nVdbeWrite--; 003116 if( p->bIsReader ) db->nVdbeRead--; 003117 assert( db->nVdbeActive>=db->nVdbeRead ); 003118 assert( db->nVdbeRead>=db->nVdbeWrite ); 003119 assert( db->nVdbeWrite>=0 ); 003120 } 003121 p->magic = VDBE_MAGIC_HALT; 003122 checkActiveVdbeCnt(db); 003123 if( db->mallocFailed ){ 003124 p->rc = SQLITE_NOMEM_BKPT; 003125 } 003126 003127 /* If the auto-commit flag is set to true, then any locks that were held 003128 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() 003129 ** to invoke any required unlock-notify callbacks. 003130 */ 003131 if( db->autoCommit ){ 003132 sqlite3ConnectionUnlocked(db); 003133 } 003134 003135 assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 ); 003136 return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK); 003137 } 003138 003139 003140 /* 003141 ** Each VDBE holds the result of the most recent sqlite3_step() call 003142 ** in p->rc. This routine sets that result back to SQLITE_OK. 003143 */ 003144 void sqlite3VdbeResetStepResult(Vdbe *p){ 003145 p->rc = SQLITE_OK; 003146 } 003147 003148 /* 003149 ** Copy the error code and error message belonging to the VDBE passed 003150 ** as the first argument to its database handle (so that they will be 003151 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). 003152 ** 003153 ** This function does not clear the VDBE error code or message, just 003154 ** copies them to the database handle. 003155 */ 003156 int sqlite3VdbeTransferError(Vdbe *p){ 003157 sqlite3 *db = p->db; 003158 int rc = p->rc; 003159 if( p->zErrMsg ){ 003160 db->bBenignMalloc++; 003161 sqlite3BeginBenignMalloc(); 003162 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); 003163 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); 003164 sqlite3EndBenignMalloc(); 003165 db->bBenignMalloc--; 003166 }else if( db->pErr ){ 003167 sqlite3ValueSetNull(db->pErr); 003168 } 003169 db->errCode = rc; 003170 return rc; 003171 } 003172 003173 #ifdef SQLITE_ENABLE_SQLLOG 003174 /* 003175 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, 003176 ** invoke it. 003177 */ 003178 static void vdbeInvokeSqllog(Vdbe *v){ 003179 if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){ 003180 char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql); 003181 assert( v->db->init.busy==0 ); 003182 if( zExpanded ){ 003183 sqlite3GlobalConfig.xSqllog( 003184 sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1 003185 ); 003186 sqlite3DbFree(v->db, zExpanded); 003187 } 003188 } 003189 } 003190 #else 003191 # define vdbeInvokeSqllog(x) 003192 #endif 003193 003194 /* 003195 ** Clean up a VDBE after execution but do not delete the VDBE just yet. 003196 ** Write any error messages into *pzErrMsg. Return the result code. 003197 ** 003198 ** After this routine is run, the VDBE should be ready to be executed 003199 ** again. 003200 ** 003201 ** To look at it another way, this routine resets the state of the 003202 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to 003203 ** VDBE_MAGIC_INIT. 003204 */ 003205 int sqlite3VdbeReset(Vdbe *p){ 003206 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) 003207 int i; 003208 #endif 003209 003210 sqlite3 *db; 003211 db = p->db; 003212 003213 /* If the VM did not run to completion or if it encountered an 003214 ** error, then it might not have been halted properly. So halt 003215 ** it now. 003216 */ 003217 sqlite3VdbeHalt(p); 003218 003219 /* If the VDBE has been run even partially, then transfer the error code 003220 ** and error message from the VDBE into the main database structure. But 003221 ** if the VDBE has just been set to run but has not actually executed any 003222 ** instructions yet, leave the main database error information unchanged. 003223 */ 003224 if( p->pc>=0 ){ 003225 vdbeInvokeSqllog(p); 003226 sqlite3VdbeTransferError(p); 003227 if( p->runOnlyOnce ) p->expired = 1; 003228 }else if( p->rc && p->expired ){ 003229 /* The expired flag was set on the VDBE before the first call 003230 ** to sqlite3_step(). For consistency (since sqlite3_step() was 003231 ** called), set the database error in this case as well. 003232 */ 003233 sqlite3ErrorWithMsg(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); 003234 } 003235 003236 /* Reset register contents and reclaim error message memory. 003237 */ 003238 #ifdef SQLITE_DEBUG 003239 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and 003240 ** Vdbe.aMem[] arrays have already been cleaned up. */ 003241 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); 003242 if( p->aMem ){ 003243 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); 003244 } 003245 #endif 003246 sqlite3DbFree(db, p->zErrMsg); 003247 p->zErrMsg = 0; 003248 p->pResultSet = 0; 003249 #ifdef SQLITE_DEBUG 003250 p->nWrite = 0; 003251 #endif 003252 003253 /* Save profiling information from this VDBE run. 003254 */ 003255 #ifdef VDBE_PROFILE 003256 { 003257 FILE *out = fopen("vdbe_profile.out", "a"); 003258 if( out ){ 003259 fprintf(out, "---- "); 003260 for(i=0; i<p->nOp; i++){ 003261 fprintf(out, "%02x", p->aOp[i].opcode); 003262 } 003263 fprintf(out, "\n"); 003264 if( p->zSql ){ 003265 char c, pc = 0; 003266 fprintf(out, "-- "); 003267 for(i=0; (c = p->zSql[i])!=0; i++){ 003268 if( pc=='\n' ) fprintf(out, "-- "); 003269 putc(c, out); 003270 pc = c; 003271 } 003272 if( pc!='\n' ) fprintf(out, "\n"); 003273 } 003274 for(i=0; i<p->nOp; i++){ 003275 char zHdr[100]; 003276 sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ", 003277 p->aOp[i].cnt, 003278 p->aOp[i].cycles, 003279 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 003280 ); 003281 fprintf(out, "%s", zHdr); 003282 sqlite3VdbePrintOp(out, i, &p->aOp[i]); 003283 } 003284 fclose(out); 003285 } 003286 } 003287 #endif 003288 p->magic = VDBE_MAGIC_RESET; 003289 return p->rc & db->errMask; 003290 } 003291 003292 /* 003293 ** Clean up and delete a VDBE after execution. Return an integer which is 003294 ** the result code. Write any error message text into *pzErrMsg. 003295 */ 003296 int sqlite3VdbeFinalize(Vdbe *p){ 003297 int rc = SQLITE_OK; 003298 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ 003299 rc = sqlite3VdbeReset(p); 003300 assert( (rc & p->db->errMask)==rc ); 003301 } 003302 sqlite3VdbeDelete(p); 003303 return rc; 003304 } 003305 003306 /* 003307 ** If parameter iOp is less than zero, then invoke the destructor for 003308 ** all auxiliary data pointers currently cached by the VM passed as 003309 ** the first argument. 003310 ** 003311 ** Or, if iOp is greater than or equal to zero, then the destructor is 003312 ** only invoked for those auxiliary data pointers created by the user 003313 ** function invoked by the OP_Function opcode at instruction iOp of 003314 ** VM pVdbe, and only then if: 003315 ** 003316 ** * the associated function parameter is the 32nd or later (counting 003317 ** from left to right), or 003318 ** 003319 ** * the corresponding bit in argument mask is clear (where the first 003320 ** function parameter corresponds to bit 0 etc.). 003321 */ 003322 void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){ 003323 while( *pp ){ 003324 AuxData *pAux = *pp; 003325 if( (iOp<0) 003326 || (pAux->iAuxOp==iOp 003327 && pAux->iAuxArg>=0 003328 && (pAux->iAuxArg>31 || !(mask & MASKBIT32(pAux->iAuxArg)))) 003329 ){ 003330 testcase( pAux->iAuxArg==31 ); 003331 if( pAux->xDeleteAux ){ 003332 pAux->xDeleteAux(pAux->pAux); 003333 } 003334 *pp = pAux->pNextAux; 003335 sqlite3DbFree(db, pAux); 003336 }else{ 003337 pp= &pAux->pNextAux; 003338 } 003339 } 003340 } 003341 003342 /* 003343 ** Free all memory associated with the Vdbe passed as the second argument, 003344 ** except for object itself, which is preserved. 003345 ** 003346 ** The difference between this function and sqlite3VdbeDelete() is that 003347 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with 003348 ** the database connection and frees the object itself. 003349 */ 003350 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ 003351 SubProgram *pSub, *pNext; 003352 assert( p->db==0 || p->db==db ); 003353 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); 003354 for(pSub=p->pProgram; pSub; pSub=pNext){ 003355 pNext = pSub->pNext; 003356 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); 003357 sqlite3DbFree(db, pSub); 003358 } 003359 if( p->magic!=VDBE_MAGIC_INIT ){ 003360 releaseMemArray(p->aVar, p->nVar); 003361 sqlite3DbFree(db, p->pVList); 003362 sqlite3DbFree(db, p->pFree); 003363 } 003364 vdbeFreeOpArray(db, p->aOp, p->nOp); 003365 sqlite3DbFree(db, p->aColName); 003366 sqlite3DbFree(db, p->zSql); 003367 #ifdef SQLITE_ENABLE_NORMALIZE 003368 sqlite3DbFree(db, p->zNormSql); 003369 { 003370 DblquoteStr *pThis, *pNext; 003371 for(pThis=p->pDblStr; pThis; pThis=pNext){ 003372 pNext = pThis->pNextStr; 003373 sqlite3DbFree(db, pThis); 003374 } 003375 } 003376 #endif 003377 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 003378 { 003379 int i; 003380 for(i=0; i<p->nScan; i++){ 003381 sqlite3DbFree(db, p->aScan[i].zName); 003382 } 003383 sqlite3DbFree(db, p->aScan); 003384 } 003385 #endif 003386 } 003387 003388 /* 003389 ** Delete an entire VDBE. 003390 */ 003391 void sqlite3VdbeDelete(Vdbe *p){ 003392 sqlite3 *db; 003393 003394 assert( p!=0 ); 003395 db = p->db; 003396 assert( sqlite3_mutex_held(db->mutex) ); 003397 sqlite3VdbeClearObject(db, p); 003398 if( p->pPrev ){ 003399 p->pPrev->pNext = p->pNext; 003400 }else{ 003401 assert( db->pVdbe==p ); 003402 db->pVdbe = p->pNext; 003403 } 003404 if( p->pNext ){ 003405 p->pNext->pPrev = p->pPrev; 003406 } 003407 p->magic = VDBE_MAGIC_DEAD; 003408 p->db = 0; 003409 sqlite3DbFreeNN(db, p); 003410 } 003411 003412 /* 003413 ** The cursor "p" has a pending seek operation that has not yet been 003414 ** carried out. Seek the cursor now. If an error occurs, return 003415 ** the appropriate error code. 003416 */ 003417 int SQLITE_NOINLINE sqlite3VdbeFinishMoveto(VdbeCursor *p){ 003418 int res, rc; 003419 #ifdef SQLITE_TEST 003420 extern int sqlite3_search_count; 003421 #endif 003422 assert( p->deferredMoveto ); 003423 assert( p->isTable ); 003424 assert( p->eCurType==CURTYPE_BTREE ); 003425 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res); 003426 if( rc ) return rc; 003427 if( res!=0 ) return SQLITE_CORRUPT_BKPT; 003428 #ifdef SQLITE_TEST 003429 sqlite3_search_count++; 003430 #endif 003431 p->deferredMoveto = 0; 003432 p->cacheStatus = CACHE_STALE; 003433 return SQLITE_OK; 003434 } 003435 003436 /* 003437 ** Something has moved cursor "p" out of place. Maybe the row it was 003438 ** pointed to was deleted out from under it. Or maybe the btree was 003439 ** rebalanced. Whatever the cause, try to restore "p" to the place it 003440 ** is supposed to be pointing. If the row was deleted out from under the 003441 ** cursor, set the cursor to point to a NULL row. 003442 */ 003443 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ 003444 int isDifferentRow, rc; 003445 assert( p->eCurType==CURTYPE_BTREE ); 003446 assert( p->uc.pCursor!=0 ); 003447 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ); 003448 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow); 003449 p->cacheStatus = CACHE_STALE; 003450 if( isDifferentRow ) p->nullRow = 1; 003451 return rc; 003452 } 003453 003454 /* 003455 ** Check to ensure that the cursor is valid. Restore the cursor 003456 ** if need be. Return any I/O error from the restore operation. 003457 */ 003458 int sqlite3VdbeCursorRestore(VdbeCursor *p){ 003459 assert( p->eCurType==CURTYPE_BTREE ); 003460 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ 003461 return handleMovedCursor(p); 003462 } 003463 return SQLITE_OK; 003464 } 003465 003466 /* 003467 ** Make sure the cursor p is ready to read or write the row to which it 003468 ** was last positioned. Return an error code if an OOM fault or I/O error 003469 ** prevents us from positioning the cursor to its correct position. 003470 ** 003471 ** If a MoveTo operation is pending on the given cursor, then do that 003472 ** MoveTo now. If no move is pending, check to see if the row has been 003473 ** deleted out from under the cursor and if it has, mark the row as 003474 ** a NULL row. 003475 ** 003476 ** If the cursor is already pointing to the correct row and that row has 003477 ** not been deleted out from under the cursor, then this routine is a no-op. 003478 */ 003479 int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ 003480 VdbeCursor *p = *pp; 003481 assert( p->eCurType==CURTYPE_BTREE || p->eCurType==CURTYPE_PSEUDO ); 003482 if( p->deferredMoveto ){ 003483 int iMap; 003484 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){ 003485 *pp = p->pAltCursor; 003486 *piCol = iMap - 1; 003487 return SQLITE_OK; 003488 } 003489 return sqlite3VdbeFinishMoveto(p); 003490 } 003491 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ 003492 return handleMovedCursor(p); 003493 } 003494 return SQLITE_OK; 003495 } 003496 003497 /* 003498 ** The following functions: 003499 ** 003500 ** sqlite3VdbeSerialType() 003501 ** sqlite3VdbeSerialTypeLen() 003502 ** sqlite3VdbeSerialLen() 003503 ** sqlite3VdbeSerialPut() 003504 ** sqlite3VdbeSerialGet() 003505 ** 003506 ** encapsulate the code that serializes values for storage in SQLite 003507 ** data and index records. Each serialized value consists of a 003508 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned 003509 ** integer, stored as a varint. 003510 ** 003511 ** In an SQLite index record, the serial type is stored directly before 003512 ** the blob of data that it corresponds to. In a table record, all serial 003513 ** types are stored at the start of the record, and the blobs of data at 003514 ** the end. Hence these functions allow the caller to handle the 003515 ** serial-type and data blob separately. 003516 ** 003517 ** The following table describes the various storage classes for data: 003518 ** 003519 ** serial type bytes of data type 003520 ** -------------- --------------- --------------- 003521 ** 0 0 NULL 003522 ** 1 1 signed integer 003523 ** 2 2 signed integer 003524 ** 3 3 signed integer 003525 ** 4 4 signed integer 003526 ** 5 6 signed integer 003527 ** 6 8 signed integer 003528 ** 7 8 IEEE float 003529 ** 8 0 Integer constant 0 003530 ** 9 0 Integer constant 1 003531 ** 10,11 reserved for expansion 003532 ** N>=12 and even (N-12)/2 BLOB 003533 ** N>=13 and odd (N-13)/2 text 003534 ** 003535 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions 003536 ** of SQLite will not understand those serial types. 003537 */ 003538 003539 #if 0 /* Inlined into the OP_MakeRecord opcode */ 003540 /* 003541 ** Return the serial-type for the value stored in pMem. 003542 ** 003543 ** This routine might convert a large MEM_IntReal value into MEM_Real. 003544 ** 003545 ** 2019-07-11: The primary user of this subroutine was the OP_MakeRecord 003546 ** opcode in the byte-code engine. But by moving this routine in-line, we 003547 ** can omit some redundant tests and make that opcode a lot faster. So 003548 ** this routine is now only used by the STAT3 logic and STAT3 support has 003549 ** ended. The code is kept here for historical reference only. 003550 */ 003551 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ 003552 int flags = pMem->flags; 003553 u32 n; 003554 003555 assert( pLen!=0 ); 003556 if( flags&MEM_Null ){ 003557 *pLen = 0; 003558 return 0; 003559 } 003560 if( flags&(MEM_Int|MEM_IntReal) ){ 003561 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ 003562 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) 003563 i64 i = pMem->u.i; 003564 u64 u; 003565 testcase( flags & MEM_Int ); 003566 testcase( flags & MEM_IntReal ); 003567 if( i<0 ){ 003568 u = ~i; 003569 }else{ 003570 u = i; 003571 } 003572 if( u<=127 ){ 003573 if( (i&1)==i && file_format>=4 ){ 003574 *pLen = 0; 003575 return 8+(u32)u; 003576 }else{ 003577 *pLen = 1; 003578 return 1; 003579 } 003580 } 003581 if( u<=32767 ){ *pLen = 2; return 2; } 003582 if( u<=8388607 ){ *pLen = 3; return 3; } 003583 if( u<=2147483647 ){ *pLen = 4; return 4; } 003584 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; } 003585 *pLen = 8; 003586 if( flags&MEM_IntReal ){ 003587 /* If the value is IntReal and is going to take up 8 bytes to store 003588 ** as an integer, then we might as well make it an 8-byte floating 003589 ** point value */ 003590 pMem->u.r = (double)pMem->u.i; 003591 pMem->flags &= ~MEM_IntReal; 003592 pMem->flags |= MEM_Real; 003593 return 7; 003594 } 003595 return 6; 003596 } 003597 if( flags&MEM_Real ){ 003598 *pLen = 8; 003599 return 7; 003600 } 003601 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); 003602 assert( pMem->n>=0 ); 003603 n = (u32)pMem->n; 003604 if( flags & MEM_Zero ){ 003605 n += pMem->u.nZero; 003606 } 003607 *pLen = n; 003608 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); 003609 } 003610 #endif /* inlined into OP_MakeRecord */ 003611 003612 /* 003613 ** The sizes for serial types less than 128 003614 */ 003615 static const u8 sqlite3SmallTypeSizes[] = { 003616 /* 0 1 2 3 4 5 6 7 8 9 */ 003617 /* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 003618 /* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 003619 /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 003620 /* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 003621 /* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 003622 /* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 003623 /* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 003624 /* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 003625 /* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 003626 /* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 003627 /* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, 003628 /* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 003629 /* 120 */ 54, 54, 55, 55, 56, 56, 57, 57 003630 }; 003631 003632 /* 003633 ** Return the length of the data corresponding to the supplied serial-type. 003634 */ 003635 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ 003636 if( serial_type>=128 ){ 003637 return (serial_type-12)/2; 003638 }else{ 003639 assert( serial_type<12 003640 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 ); 003641 return sqlite3SmallTypeSizes[serial_type]; 003642 } 003643 } 003644 u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ 003645 assert( serial_type<128 ); 003646 return sqlite3SmallTypeSizes[serial_type]; 003647 } 003648 003649 /* 003650 ** If we are on an architecture with mixed-endian floating 003651 ** points (ex: ARM7) then swap the lower 4 bytes with the 003652 ** upper 4 bytes. Return the result. 003653 ** 003654 ** For most architectures, this is a no-op. 003655 ** 003656 ** (later): It is reported to me that the mixed-endian problem 003657 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems 003658 ** that early versions of GCC stored the two words of a 64-bit 003659 ** float in the wrong order. And that error has been propagated 003660 ** ever since. The blame is not necessarily with GCC, though. 003661 ** GCC might have just copying the problem from a prior compiler. 003662 ** I am also told that newer versions of GCC that follow a different 003663 ** ABI get the byte order right. 003664 ** 003665 ** Developers using SQLite on an ARM7 should compile and run their 003666 ** application using -DSQLITE_DEBUG=1 at least once. With DEBUG 003667 ** enabled, some asserts below will ensure that the byte order of 003668 ** floating point values is correct. 003669 ** 003670 ** (2007-08-30) Frank van Vugt has studied this problem closely 003671 ** and has send his findings to the SQLite developers. Frank 003672 ** writes that some Linux kernels offer floating point hardware 003673 ** emulation that uses only 32-bit mantissas instead of a full 003674 ** 48-bits as required by the IEEE standard. (This is the 003675 ** CONFIG_FPE_FASTFPE option.) On such systems, floating point 003676 ** byte swapping becomes very complicated. To avoid problems, 003677 ** the necessary byte swapping is carried out using a 64-bit integer 003678 ** rather than a 64-bit float. Frank assures us that the code here 003679 ** works for him. We, the developers, have no way to independently 003680 ** verify this, but Frank seems to know what he is talking about 003681 ** so we trust him. 003682 */ 003683 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT 003684 static u64 floatSwap(u64 in){ 003685 union { 003686 u64 r; 003687 u32 i[2]; 003688 } u; 003689 u32 t; 003690 003691 u.r = in; 003692 t = u.i[0]; 003693 u.i[0] = u.i[1]; 003694 u.i[1] = t; 003695 return u.r; 003696 } 003697 # define swapMixedEndianFloat(X) X = floatSwap(X) 003698 #else 003699 # define swapMixedEndianFloat(X) 003700 #endif 003701 003702 /* 003703 ** Write the serialized data blob for the value stored in pMem into 003704 ** buf. It is assumed that the caller has allocated sufficient space. 003705 ** Return the number of bytes written. 003706 ** 003707 ** nBuf is the amount of space left in buf[]. The caller is responsible 003708 ** for allocating enough space to buf[] to hold the entire field, exclusive 003709 ** of the pMem->u.nZero bytes for a MEM_Zero value. 003710 ** 003711 ** Return the number of bytes actually written into buf[]. The number 003712 ** of bytes in the zero-filled tail is included in the return value only 003713 ** if those bytes were zeroed in buf[]. 003714 */ 003715 u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ 003716 u32 len; 003717 003718 /* Integer and Real */ 003719 if( serial_type<=7 && serial_type>0 ){ 003720 u64 v; 003721 u32 i; 003722 if( serial_type==7 ){ 003723 assert( sizeof(v)==sizeof(pMem->u.r) ); 003724 memcpy(&v, &pMem->u.r, sizeof(v)); 003725 swapMixedEndianFloat(v); 003726 }else{ 003727 v = pMem->u.i; 003728 } 003729 len = i = sqlite3SmallTypeSizes[serial_type]; 003730 assert( i>0 ); 003731 do{ 003732 buf[--i] = (u8)(v&0xFF); 003733 v >>= 8; 003734 }while( i ); 003735 return len; 003736 } 003737 003738 /* String or blob */ 003739 if( serial_type>=12 ){ 003740 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) 003741 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); 003742 len = pMem->n; 003743 if( len>0 ) memcpy(buf, pMem->z, len); 003744 return len; 003745 } 003746 003747 /* NULL or constants 0 or 1 */ 003748 return 0; 003749 } 003750 003751 /* Input "x" is a sequence of unsigned characters that represent a 003752 ** big-endian integer. Return the equivalent native integer 003753 */ 003754 #define ONE_BYTE_INT(x) ((i8)(x)[0]) 003755 #define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) 003756 #define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) 003757 #define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) 003758 #define FOUR_BYTE_INT(x) (16777216*(i8)((x)[0])|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) 003759 003760 /* 003761 ** Deserialize the data blob pointed to by buf as serial type serial_type 003762 ** and store the result in pMem. Return the number of bytes read. 003763 ** 003764 ** This function is implemented as two separate routines for performance. 003765 ** The few cases that require local variables are broken out into a separate 003766 ** routine so that in most cases the overhead of moving the stack pointer 003767 ** is avoided. 003768 */ 003769 static u32 serialGet( 003770 const unsigned char *buf, /* Buffer to deserialize from */ 003771 u32 serial_type, /* Serial type to deserialize */ 003772 Mem *pMem /* Memory cell to write value into */ 003773 ){ 003774 u64 x = FOUR_BYTE_UINT(buf); 003775 u32 y = FOUR_BYTE_UINT(buf+4); 003776 x = (x<<32) + y; 003777 if( serial_type==6 ){ 003778 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit 003779 ** twos-complement integer. */ 003780 pMem->u.i = *(i64*)&x; 003781 pMem->flags = MEM_Int; 003782 testcase( pMem->u.i<0 ); 003783 }else{ 003784 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit 003785 ** floating point number. */ 003786 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) 003787 /* Verify that integers and floating point values use the same 003788 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is 003789 ** defined that 64-bit floating point values really are mixed 003790 ** endian. 003791 */ 003792 static const u64 t1 = ((u64)0x3ff00000)<<32; 003793 static const double r1 = 1.0; 003794 u64 t2 = t1; 003795 swapMixedEndianFloat(t2); 003796 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); 003797 #endif 003798 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); 003799 swapMixedEndianFloat(x); 003800 memcpy(&pMem->u.r, &x, sizeof(x)); 003801 pMem->flags = IsNaN(x) ? MEM_Null : MEM_Real; 003802 } 003803 return 8; 003804 } 003805 u32 sqlite3VdbeSerialGet( 003806 const unsigned char *buf, /* Buffer to deserialize from */ 003807 u32 serial_type, /* Serial type to deserialize */ 003808 Mem *pMem /* Memory cell to write value into */ 003809 ){ 003810 switch( serial_type ){ 003811 case 10: { /* Internal use only: NULL with virtual table 003812 ** UPDATE no-change flag set */ 003813 pMem->flags = MEM_Null|MEM_Zero; 003814 pMem->n = 0; 003815 pMem->u.nZero = 0; 003816 break; 003817 } 003818 case 11: /* Reserved for future use */ 003819 case 0: { /* Null */ 003820 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ 003821 pMem->flags = MEM_Null; 003822 break; 003823 } 003824 case 1: { 003825 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement 003826 ** integer. */ 003827 pMem->u.i = ONE_BYTE_INT(buf); 003828 pMem->flags = MEM_Int; 003829 testcase( pMem->u.i<0 ); 003830 return 1; 003831 } 003832 case 2: { /* 2-byte signed integer */ 003833 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit 003834 ** twos-complement integer. */ 003835 pMem->u.i = TWO_BYTE_INT(buf); 003836 pMem->flags = MEM_Int; 003837 testcase( pMem->u.i<0 ); 003838 return 2; 003839 } 003840 case 3: { /* 3-byte signed integer */ 003841 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit 003842 ** twos-complement integer. */ 003843 pMem->u.i = THREE_BYTE_INT(buf); 003844 pMem->flags = MEM_Int; 003845 testcase( pMem->u.i<0 ); 003846 return 3; 003847 } 003848 case 4: { /* 4-byte signed integer */ 003849 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit 003850 ** twos-complement integer. */ 003851 pMem->u.i = FOUR_BYTE_INT(buf); 003852 #ifdef __HP_cc 003853 /* Work around a sign-extension bug in the HP compiler for HP/UX */ 003854 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL; 003855 #endif 003856 pMem->flags = MEM_Int; 003857 testcase( pMem->u.i<0 ); 003858 return 4; 003859 } 003860 case 5: { /* 6-byte signed integer */ 003861 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit 003862 ** twos-complement integer. */ 003863 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); 003864 pMem->flags = MEM_Int; 003865 testcase( pMem->u.i<0 ); 003866 return 6; 003867 } 003868 case 6: /* 8-byte signed integer */ 003869 case 7: { /* IEEE floating point */ 003870 /* These use local variables, so do them in a separate routine 003871 ** to avoid having to move the frame pointer in the common case */ 003872 return serialGet(buf,serial_type,pMem); 003873 } 003874 case 8: /* Integer 0 */ 003875 case 9: { /* Integer 1 */ 003876 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ 003877 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ 003878 pMem->u.i = serial_type-8; 003879 pMem->flags = MEM_Int; 003880 return 0; 003881 } 003882 default: { 003883 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in 003884 ** length. 003885 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and 003886 ** (N-13)/2 bytes in length. */ 003887 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; 003888 pMem->z = (char *)buf; 003889 pMem->n = (serial_type-12)/2; 003890 pMem->flags = aFlag[serial_type&1]; 003891 return pMem->n; 003892 } 003893 } 003894 return 0; 003895 } 003896 /* 003897 ** This routine is used to allocate sufficient space for an UnpackedRecord 003898 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if 003899 ** the first argument is a pointer to KeyInfo structure pKeyInfo. 003900 ** 003901 ** The space is either allocated using sqlite3DbMallocRaw() or from within 003902 ** the unaligned buffer passed via the second and third arguments (presumably 003903 ** stack space). If the former, then *ppFree is set to a pointer that should 003904 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the 003905 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL 003906 ** before returning. 003907 ** 003908 ** If an OOM error occurs, NULL is returned. 003909 */ 003910 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( 003911 KeyInfo *pKeyInfo /* Description of the record */ 003912 ){ 003913 UnpackedRecord *p; /* Unpacked record to return */ 003914 int nByte; /* Number of bytes required for *p */ 003915 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nKeyField+1); 003916 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); 003917 if( !p ) return 0; 003918 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; 003919 assert( pKeyInfo->aSortFlags!=0 ); 003920 p->pKeyInfo = pKeyInfo; 003921 p->nField = pKeyInfo->nKeyField + 1; 003922 return p; 003923 } 003924 003925 /* 003926 ** Given the nKey-byte encoding of a record in pKey[], populate the 003927 ** UnpackedRecord structure indicated by the fourth argument with the 003928 ** contents of the decoded record. 003929 */ 003930 void sqlite3VdbeRecordUnpack( 003931 KeyInfo *pKeyInfo, /* Information about the record format */ 003932 int nKey, /* Size of the binary record */ 003933 const void *pKey, /* The binary record */ 003934 UnpackedRecord *p /* Populate this structure before returning. */ 003935 ){ 003936 const unsigned char *aKey = (const unsigned char *)pKey; 003937 u32 d; 003938 u32 idx; /* Offset in aKey[] to read from */ 003939 u16 u; /* Unsigned loop counter */ 003940 u32 szHdr; 003941 Mem *pMem = p->aMem; 003942 003943 p->default_rc = 0; 003944 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 003945 idx = getVarint32(aKey, szHdr); 003946 d = szHdr; 003947 u = 0; 003948 while( idx<szHdr && d<=(u32)nKey ){ 003949 u32 serial_type; 003950 003951 idx += getVarint32(&aKey[idx], serial_type); 003952 pMem->enc = pKeyInfo->enc; 003953 pMem->db = pKeyInfo->db; 003954 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ 003955 pMem->szMalloc = 0; 003956 pMem->z = 0; 003957 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); 003958 pMem++; 003959 if( (++u)>=p->nField ) break; 003960 } 003961 if( d>(u32)nKey && u ){ 003962 assert( CORRUPT_DB ); 003963 /* In a corrupt record entry, the last pMem might have been set up using 003964 ** uninitialized memory. Overwrite its value with NULL, to prevent 003965 ** warnings from MSAN. */ 003966 sqlite3VdbeMemSetNull(pMem-1); 003967 } 003968 assert( u<=pKeyInfo->nKeyField + 1 ); 003969 p->nField = u; 003970 } 003971 003972 #ifdef SQLITE_DEBUG 003973 /* 003974 ** This function compares two index or table record keys in the same way 003975 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(), 003976 ** this function deserializes and compares values using the 003977 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used 003978 ** in assert() statements to ensure that the optimized code in 003979 ** sqlite3VdbeRecordCompare() returns results with these two primitives. 003980 ** 003981 ** Return true if the result of comparison is equivalent to desiredResult. 003982 ** Return false if there is a disagreement. 003983 */ 003984 static int vdbeRecordCompareDebug( 003985 int nKey1, const void *pKey1, /* Left key */ 003986 const UnpackedRecord *pPKey2, /* Right key */ 003987 int desiredResult /* Correct answer */ 003988 ){ 003989 u32 d1; /* Offset into aKey[] of next data element */ 003990 u32 idx1; /* Offset into aKey[] of next header element */ 003991 u32 szHdr1; /* Number of bytes in header */ 003992 int i = 0; 003993 int rc = 0; 003994 const unsigned char *aKey1 = (const unsigned char *)pKey1; 003995 KeyInfo *pKeyInfo; 003996 Mem mem1; 003997 003998 pKeyInfo = pPKey2->pKeyInfo; 003999 if( pKeyInfo->db==0 ) return 1; 004000 mem1.enc = pKeyInfo->enc; 004001 mem1.db = pKeyInfo->db; 004002 /* mem1.flags = 0; // Will be initialized by sqlite3VdbeSerialGet() */ 004003 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ 004004 004005 /* Compilers may complain that mem1.u.i is potentially uninitialized. 004006 ** We could initialize it, as shown here, to silence those complaints. 004007 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing 004008 ** the unnecessary initialization has a measurable negative performance 004009 ** impact, since this routine is a very high runner. And so, we choose 004010 ** to ignore the compiler warnings and leave this variable uninitialized. 004011 */ 004012 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ 004013 004014 idx1 = getVarint32(aKey1, szHdr1); 004015 if( szHdr1>98307 ) return SQLITE_CORRUPT; 004016 d1 = szHdr1; 004017 assert( pKeyInfo->nAllField>=pPKey2->nField || CORRUPT_DB ); 004018 assert( pKeyInfo->aSortFlags!=0 ); 004019 assert( pKeyInfo->nKeyField>0 ); 004020 assert( idx1<=szHdr1 || CORRUPT_DB ); 004021 do{ 004022 u32 serial_type1; 004023 004024 /* Read the serial types for the next element in each key. */ 004025 idx1 += getVarint32( aKey1+idx1, serial_type1 ); 004026 004027 /* Verify that there is enough key space remaining to avoid 004028 ** a buffer overread. The "d1+serial_type1+2" subexpression will 004029 ** always be greater than or equal to the amount of required key space. 004030 ** Use that approximation to avoid the more expensive call to 004031 ** sqlite3VdbeSerialTypeLen() in the common case. 004032 */ 004033 if( d1+(u64)serial_type1+2>(u64)nKey1 004034 && d1+(u64)sqlite3VdbeSerialTypeLen(serial_type1)>(u64)nKey1 004035 ){ 004036 break; 004037 } 004038 004039 /* Extract the values to be compared. 004040 */ 004041 d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1); 004042 004043 /* Do the comparison 004044 */ 004045 rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], 004046 pKeyInfo->nAllField>i ? pKeyInfo->aColl[i] : 0); 004047 if( rc!=0 ){ 004048 assert( mem1.szMalloc==0 ); /* See comment below */ 004049 if( (pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) 004050 && ((mem1.flags & MEM_Null) || (pPKey2->aMem[i].flags & MEM_Null)) 004051 ){ 004052 rc = -rc; 004053 } 004054 if( pKeyInfo->aSortFlags[i] & KEYINFO_ORDER_DESC ){ 004055 rc = -rc; /* Invert the result for DESC sort order. */ 004056 } 004057 goto debugCompareEnd; 004058 } 004059 i++; 004060 }while( idx1<szHdr1 && i<pPKey2->nField ); 004061 004062 /* No memory allocation is ever used on mem1. Prove this using 004063 ** the following assert(). If the assert() fails, it indicates a 004064 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). 004065 */ 004066 assert( mem1.szMalloc==0 ); 004067 004068 /* rc==0 here means that one of the keys ran out of fields and 004069 ** all the fields up to that point were equal. Return the default_rc 004070 ** value. */ 004071 rc = pPKey2->default_rc; 004072 004073 debugCompareEnd: 004074 if( desiredResult==0 && rc==0 ) return 1; 004075 if( desiredResult<0 && rc<0 ) return 1; 004076 if( desiredResult>0 && rc>0 ) return 1; 004077 if( CORRUPT_DB ) return 1; 004078 if( pKeyInfo->db->mallocFailed ) return 1; 004079 return 0; 004080 } 004081 #endif 004082 004083 #ifdef SQLITE_DEBUG 004084 /* 004085 ** Count the number of fields (a.k.a. columns) in the record given by 004086 ** pKey,nKey. The verify that this count is less than or equal to the 004087 ** limit given by pKeyInfo->nAllField. 004088 ** 004089 ** If this constraint is not satisfied, it means that the high-speed 004090 ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will 004091 ** not work correctly. If this assert() ever fires, it probably means 004092 ** that the KeyInfo.nKeyField or KeyInfo.nAllField values were computed 004093 ** incorrectly. 004094 */ 004095 static void vdbeAssertFieldCountWithinLimits( 004096 int nKey, const void *pKey, /* The record to verify */ 004097 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */ 004098 ){ 004099 int nField = 0; 004100 u32 szHdr; 004101 u32 idx; 004102 u32 notUsed; 004103 const unsigned char *aKey = (const unsigned char*)pKey; 004104 004105 if( CORRUPT_DB ) return; 004106 idx = getVarint32(aKey, szHdr); 004107 assert( nKey>=0 ); 004108 assert( szHdr<=(u32)nKey ); 004109 while( idx<szHdr ){ 004110 idx += getVarint32(aKey+idx, notUsed); 004111 nField++; 004112 } 004113 assert( nField <= pKeyInfo->nAllField ); 004114 } 004115 #else 004116 # define vdbeAssertFieldCountWithinLimits(A,B,C) 004117 #endif 004118 004119 /* 004120 ** Both *pMem1 and *pMem2 contain string values. Compare the two values 004121 ** using the collation sequence pColl. As usual, return a negative , zero 004122 ** or positive value if *pMem1 is less than, equal to or greater than 004123 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". 004124 */ 004125 static int vdbeCompareMemString( 004126 const Mem *pMem1, 004127 const Mem *pMem2, 004128 const CollSeq *pColl, 004129 u8 *prcErr /* If an OOM occurs, set to SQLITE_NOMEM */ 004130 ){ 004131 if( pMem1->enc==pColl->enc ){ 004132 /* The strings are already in the correct encoding. Call the 004133 ** comparison function directly */ 004134 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); 004135 }else{ 004136 int rc; 004137 const void *v1, *v2; 004138 Mem c1; 004139 Mem c2; 004140 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); 004141 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); 004142 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); 004143 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); 004144 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); 004145 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); 004146 if( (v1==0 || v2==0) ){ 004147 if( prcErr ) *prcErr = SQLITE_NOMEM_BKPT; 004148 rc = 0; 004149 }else{ 004150 rc = pColl->xCmp(pColl->pUser, c1.n, v1, c2.n, v2); 004151 } 004152 sqlite3VdbeMemRelease(&c1); 004153 sqlite3VdbeMemRelease(&c2); 004154 return rc; 004155 } 004156 } 004157 004158 /* 004159 ** The input pBlob is guaranteed to be a Blob that is not marked 004160 ** with MEM_Zero. Return true if it could be a zero-blob. 004161 */ 004162 static int isAllZero(const char *z, int n){ 004163 int i; 004164 for(i=0; i<n; i++){ 004165 if( z[i] ) return 0; 004166 } 004167 return 1; 004168 } 004169 004170 /* 004171 ** Compare two blobs. Return negative, zero, or positive if the first 004172 ** is less than, equal to, or greater than the second, respectively. 004173 ** If one blob is a prefix of the other, then the shorter is the lessor. 004174 */ 004175 SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ 004176 int c; 004177 int n1 = pB1->n; 004178 int n2 = pB2->n; 004179 004180 /* It is possible to have a Blob value that has some non-zero content 004181 ** followed by zero content. But that only comes up for Blobs formed 004182 ** by the OP_MakeRecord opcode, and such Blobs never get passed into 004183 ** sqlite3MemCompare(). */ 004184 assert( (pB1->flags & MEM_Zero)==0 || n1==0 ); 004185 assert( (pB2->flags & MEM_Zero)==0 || n2==0 ); 004186 004187 if( (pB1->flags|pB2->flags) & MEM_Zero ){ 004188 if( pB1->flags & pB2->flags & MEM_Zero ){ 004189 return pB1->u.nZero - pB2->u.nZero; 004190 }else if( pB1->flags & MEM_Zero ){ 004191 if( !isAllZero(pB2->z, pB2->n) ) return -1; 004192 return pB1->u.nZero - n2; 004193 }else{ 004194 if( !isAllZero(pB1->z, pB1->n) ) return +1; 004195 return n1 - pB2->u.nZero; 004196 } 004197 } 004198 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1); 004199 if( c ) return c; 004200 return n1 - n2; 004201 } 004202 004203 /* 004204 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point 004205 ** number. Return negative, zero, or positive if the first (i64) is less than, 004206 ** equal to, or greater than the second (double). 004207 */ 004208 static int sqlite3IntFloatCompare(i64 i, double r){ 004209 if( sizeof(LONGDOUBLE_TYPE)>8 ){ 004210 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; 004211 if( x<r ) return -1; 004212 if( x>r ) return +1; 004213 return 0; 004214 }else{ 004215 i64 y; 004216 double s; 004217 if( r<-9223372036854775808.0 ) return +1; 004218 if( r>=9223372036854775808.0 ) return -1; 004219 y = (i64)r; 004220 if( i<y ) return -1; 004221 if( i>y ) return +1; 004222 s = (double)i; 004223 if( s<r ) return -1; 004224 if( s>r ) return +1; 004225 return 0; 004226 } 004227 } 004228 004229 /* 004230 ** Compare the values contained by the two memory cells, returning 004231 ** negative, zero or positive if pMem1 is less than, equal to, or greater 004232 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers 004233 ** and reals) sorted numerically, followed by text ordered by the collating 004234 ** sequence pColl and finally blob's ordered by memcmp(). 004235 ** 004236 ** Two NULL values are considered equal by this function. 004237 */ 004238 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ 004239 int f1, f2; 004240 int combined_flags; 004241 004242 f1 = pMem1->flags; 004243 f2 = pMem2->flags; 004244 combined_flags = f1|f2; 004245 assert( !sqlite3VdbeMemIsRowSet(pMem1) && !sqlite3VdbeMemIsRowSet(pMem2) ); 004246 004247 /* If one value is NULL, it is less than the other. If both values 004248 ** are NULL, return 0. 004249 */ 004250 if( combined_flags&MEM_Null ){ 004251 return (f2&MEM_Null) - (f1&MEM_Null); 004252 } 004253 004254 /* At least one of the two values is a number 004255 */ 004256 if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){ 004257 testcase( combined_flags & MEM_Int ); 004258 testcase( combined_flags & MEM_Real ); 004259 testcase( combined_flags & MEM_IntReal ); 004260 if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){ 004261 testcase( f1 & f2 & MEM_Int ); 004262 testcase( f1 & f2 & MEM_IntReal ); 004263 if( pMem1->u.i < pMem2->u.i ) return -1; 004264 if( pMem1->u.i > pMem2->u.i ) return +1; 004265 return 0; 004266 } 004267 if( (f1 & f2 & MEM_Real)!=0 ){ 004268 if( pMem1->u.r < pMem2->u.r ) return -1; 004269 if( pMem1->u.r > pMem2->u.r ) return +1; 004270 return 0; 004271 } 004272 if( (f1&(MEM_Int|MEM_IntReal))!=0 ){ 004273 testcase( f1 & MEM_Int ); 004274 testcase( f1 & MEM_IntReal ); 004275 if( (f2&MEM_Real)!=0 ){ 004276 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r); 004277 }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ 004278 if( pMem1->u.i < pMem2->u.i ) return -1; 004279 if( pMem1->u.i > pMem2->u.i ) return +1; 004280 return 0; 004281 }else{ 004282 return -1; 004283 } 004284 } 004285 if( (f1&MEM_Real)!=0 ){ 004286 if( (f2&(MEM_Int|MEM_IntReal))!=0 ){ 004287 testcase( f2 & MEM_Int ); 004288 testcase( f2 & MEM_IntReal ); 004289 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r); 004290 }else{ 004291 return -1; 004292 } 004293 } 004294 return +1; 004295 } 004296 004297 /* If one value is a string and the other is a blob, the string is less. 004298 ** If both are strings, compare using the collating functions. 004299 */ 004300 if( combined_flags&MEM_Str ){ 004301 if( (f1 & MEM_Str)==0 ){ 004302 return 1; 004303 } 004304 if( (f2 & MEM_Str)==0 ){ 004305 return -1; 004306 } 004307 004308 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed ); 004309 assert( pMem1->enc==SQLITE_UTF8 || 004310 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); 004311 004312 /* The collation sequence must be defined at this point, even if 004313 ** the user deletes the collation sequence after the vdbe program is 004314 ** compiled (this was not always the case). 004315 */ 004316 assert( !pColl || pColl->xCmp ); 004317 004318 if( pColl ){ 004319 return vdbeCompareMemString(pMem1, pMem2, pColl, 0); 004320 } 004321 /* If a NULL pointer was passed as the collate function, fall through 004322 ** to the blob case and use memcmp(). */ 004323 } 004324 004325 /* Both values must be blobs. Compare using memcmp(). */ 004326 return sqlite3BlobCompare(pMem1, pMem2); 004327 } 004328 004329 004330 /* 004331 ** The first argument passed to this function is a serial-type that 004332 ** corresponds to an integer - all values between 1 and 9 inclusive 004333 ** except 7. The second points to a buffer containing an integer value 004334 ** serialized according to serial_type. This function deserializes 004335 ** and returns the value. 004336 */ 004337 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ 004338 u32 y; 004339 assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); 004340 switch( serial_type ){ 004341 case 0: 004342 case 1: 004343 testcase( aKey[0]&0x80 ); 004344 return ONE_BYTE_INT(aKey); 004345 case 2: 004346 testcase( aKey[0]&0x80 ); 004347 return TWO_BYTE_INT(aKey); 004348 case 3: 004349 testcase( aKey[0]&0x80 ); 004350 return THREE_BYTE_INT(aKey); 004351 case 4: { 004352 testcase( aKey[0]&0x80 ); 004353 y = FOUR_BYTE_UINT(aKey); 004354 return (i64)*(int*)&y; 004355 } 004356 case 5: { 004357 testcase( aKey[0]&0x80 ); 004358 return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); 004359 } 004360 case 6: { 004361 u64 x = FOUR_BYTE_UINT(aKey); 004362 testcase( aKey[0]&0x80 ); 004363 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); 004364 return (i64)*(i64*)&x; 004365 } 004366 } 004367 004368 return (serial_type - 8); 004369 } 004370 004371 /* 004372 ** This function compares the two table rows or index records 004373 ** specified by {nKey1, pKey1} and pPKey2. It returns a negative, zero 004374 ** or positive integer if key1 is less than, equal to or 004375 ** greater than key2. The {nKey1, pKey1} key must be a blob 004376 ** created by the OP_MakeRecord opcode of the VDBE. The pPKey2 004377 ** key must be a parsed key such as obtained from 004378 ** sqlite3VdbeParseRecord. 004379 ** 004380 ** If argument bSkip is non-zero, it is assumed that the caller has already 004381 ** determined that the first fields of the keys are equal. 004382 ** 004383 ** Key1 and Key2 do not have to contain the same number of fields. If all 004384 ** fields that appear in both keys are equal, then pPKey2->default_rc is 004385 ** returned. 004386 ** 004387 ** If database corruption is discovered, set pPKey2->errCode to 004388 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, 004389 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the 004390 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). 004391 */ 004392 int sqlite3VdbeRecordCompareWithSkip( 004393 int nKey1, const void *pKey1, /* Left key */ 004394 UnpackedRecord *pPKey2, /* Right key */ 004395 int bSkip /* If true, skip the first field */ 004396 ){ 004397 u32 d1; /* Offset into aKey[] of next data element */ 004398 int i; /* Index of next field to compare */ 004399 u32 szHdr1; /* Size of record header in bytes */ 004400 u32 idx1; /* Offset of first type in header */ 004401 int rc = 0; /* Return value */ 004402 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ 004403 KeyInfo *pKeyInfo; 004404 const unsigned char *aKey1 = (const unsigned char *)pKey1; 004405 Mem mem1; 004406 004407 /* If bSkip is true, then the caller has already determined that the first 004408 ** two elements in the keys are equal. Fix the various stack variables so 004409 ** that this routine begins comparing at the second field. */ 004410 if( bSkip ){ 004411 u32 s1; 004412 idx1 = 1 + getVarint32(&aKey1[1], s1); 004413 szHdr1 = aKey1[0]; 004414 d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1); 004415 i = 1; 004416 pRhs++; 004417 }else{ 004418 idx1 = getVarint32(aKey1, szHdr1); 004419 d1 = szHdr1; 004420 i = 0; 004421 } 004422 if( d1>(unsigned)nKey1 ){ 004423 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; 004424 return 0; /* Corruption */ 004425 } 004426 004427 VVA_ONLY( mem1.szMalloc = 0; ) /* Only needed by assert() statements */ 004428 assert( pPKey2->pKeyInfo->nAllField>=pPKey2->nField 004429 || CORRUPT_DB ); 004430 assert( pPKey2->pKeyInfo->aSortFlags!=0 ); 004431 assert( pPKey2->pKeyInfo->nKeyField>0 ); 004432 assert( idx1<=szHdr1 || CORRUPT_DB ); 004433 do{ 004434 u32 serial_type; 004435 004436 /* RHS is an integer */ 004437 if( pRhs->flags & (MEM_Int|MEM_IntReal) ){ 004438 testcase( pRhs->flags & MEM_Int ); 004439 testcase( pRhs->flags & MEM_IntReal ); 004440 serial_type = aKey1[idx1]; 004441 testcase( serial_type==12 ); 004442 if( serial_type>=10 ){ 004443 rc = +1; 004444 }else if( serial_type==0 ){ 004445 rc = -1; 004446 }else if( serial_type==7 ){ 004447 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); 004448 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r); 004449 }else{ 004450 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); 004451 i64 rhs = pRhs->u.i; 004452 if( lhs<rhs ){ 004453 rc = -1; 004454 }else if( lhs>rhs ){ 004455 rc = +1; 004456 } 004457 } 004458 } 004459 004460 /* RHS is real */ 004461 else if( pRhs->flags & MEM_Real ){ 004462 serial_type = aKey1[idx1]; 004463 if( serial_type>=10 ){ 004464 /* Serial types 12 or greater are strings and blobs (greater than 004465 ** numbers). Types 10 and 11 are currently "reserved for future 004466 ** use", so it doesn't really matter what the results of comparing 004467 ** them to numberic values are. */ 004468 rc = +1; 004469 }else if( serial_type==0 ){ 004470 rc = -1; 004471 }else{ 004472 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); 004473 if( serial_type==7 ){ 004474 if( mem1.u.r<pRhs->u.r ){ 004475 rc = -1; 004476 }else if( mem1.u.r>pRhs->u.r ){ 004477 rc = +1; 004478 } 004479 }else{ 004480 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r); 004481 } 004482 } 004483 } 004484 004485 /* RHS is a string */ 004486 else if( pRhs->flags & MEM_Str ){ 004487 getVarint32(&aKey1[idx1], serial_type); 004488 testcase( serial_type==12 ); 004489 if( serial_type<12 ){ 004490 rc = -1; 004491 }else if( !(serial_type & 0x01) ){ 004492 rc = +1; 004493 }else{ 004494 mem1.n = (serial_type - 12) / 2; 004495 testcase( (d1+mem1.n)==(unsigned)nKey1 ); 004496 testcase( (d1+mem1.n+1)==(unsigned)nKey1 ); 004497 if( (d1+mem1.n) > (unsigned)nKey1 004498 || (pKeyInfo = pPKey2->pKeyInfo)->nAllField<=i 004499 ){ 004500 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; 004501 return 0; /* Corruption */ 004502 }else if( pKeyInfo->aColl[i] ){ 004503 mem1.enc = pKeyInfo->enc; 004504 mem1.db = pKeyInfo->db; 004505 mem1.flags = MEM_Str; 004506 mem1.z = (char*)&aKey1[d1]; 004507 rc = vdbeCompareMemString( 004508 &mem1, pRhs, pKeyInfo->aColl[i], &pPKey2->errCode 004509 ); 004510 }else{ 004511 int nCmp = MIN(mem1.n, pRhs->n); 004512 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); 004513 if( rc==0 ) rc = mem1.n - pRhs->n; 004514 } 004515 } 004516 } 004517 004518 /* RHS is a blob */ 004519 else if( pRhs->flags & MEM_Blob ){ 004520 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 ); 004521 getVarint32(&aKey1[idx1], serial_type); 004522 testcase( serial_type==12 ); 004523 if( serial_type<12 || (serial_type & 0x01) ){ 004524 rc = -1; 004525 }else{ 004526 int nStr = (serial_type - 12) / 2; 004527 testcase( (d1+nStr)==(unsigned)nKey1 ); 004528 testcase( (d1+nStr+1)==(unsigned)nKey1 ); 004529 if( (d1+nStr) > (unsigned)nKey1 ){ 004530 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; 004531 return 0; /* Corruption */ 004532 }else if( pRhs->flags & MEM_Zero ){ 004533 if( !isAllZero((const char*)&aKey1[d1],nStr) ){ 004534 rc = 1; 004535 }else{ 004536 rc = nStr - pRhs->u.nZero; 004537 } 004538 }else{ 004539 int nCmp = MIN(nStr, pRhs->n); 004540 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); 004541 if( rc==0 ) rc = nStr - pRhs->n; 004542 } 004543 } 004544 } 004545 004546 /* RHS is null */ 004547 else{ 004548 serial_type = aKey1[idx1]; 004549 rc = (serial_type!=0); 004550 } 004551 004552 if( rc!=0 ){ 004553 int sortFlags = pPKey2->pKeyInfo->aSortFlags[i]; 004554 if( sortFlags ){ 004555 if( (sortFlags & KEYINFO_ORDER_BIGNULL)==0 004556 || ((sortFlags & KEYINFO_ORDER_DESC) 004557 !=(serial_type==0 || (pRhs->flags&MEM_Null))) 004558 ){ 004559 rc = -rc; 004560 } 004561 } 004562 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, rc) ); 004563 assert( mem1.szMalloc==0 ); /* See comment below */ 004564 return rc; 004565 } 004566 004567 i++; 004568 if( i==pPKey2->nField ) break; 004569 pRhs++; 004570 d1 += sqlite3VdbeSerialTypeLen(serial_type); 004571 idx1 += sqlite3VarintLen(serial_type); 004572 }while( idx1<(unsigned)szHdr1 && d1<=(unsigned)nKey1 ); 004573 004574 /* No memory allocation is ever used on mem1. Prove this using 004575 ** the following assert(). If the assert() fails, it indicates a 004576 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ 004577 assert( mem1.szMalloc==0 ); 004578 004579 /* rc==0 here means that one or both of the keys ran out of fields and 004580 ** all the fields up to that point were equal. Return the default_rc 004581 ** value. */ 004582 assert( CORRUPT_DB 004583 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) 004584 || pPKey2->pKeyInfo->db->mallocFailed 004585 ); 004586 pPKey2->eqSeen = 1; 004587 return pPKey2->default_rc; 004588 } 004589 int sqlite3VdbeRecordCompare( 004590 int nKey1, const void *pKey1, /* Left key */ 004591 UnpackedRecord *pPKey2 /* Right key */ 004592 ){ 004593 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); 004594 } 004595 004596 004597 /* 004598 ** This function is an optimized version of sqlite3VdbeRecordCompare() 004599 ** that (a) the first field of pPKey2 is an integer, and (b) the 004600 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single 004601 ** byte (i.e. is less than 128). 004602 ** 004603 ** To avoid concerns about buffer overreads, this routine is only used 004604 ** on schemas where the maximum valid header size is 63 bytes or less. 004605 */ 004606 static int vdbeRecordCompareInt( 004607 int nKey1, const void *pKey1, /* Left key */ 004608 UnpackedRecord *pPKey2 /* Right key */ 004609 ){ 004610 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; 004611 int serial_type = ((const u8*)pKey1)[1]; 004612 int res; 004613 u32 y; 004614 u64 x; 004615 i64 v; 004616 i64 lhs; 004617 004618 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); 004619 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); 004620 switch( serial_type ){ 004621 case 1: { /* 1-byte signed integer */ 004622 lhs = ONE_BYTE_INT(aKey); 004623 testcase( lhs<0 ); 004624 break; 004625 } 004626 case 2: { /* 2-byte signed integer */ 004627 lhs = TWO_BYTE_INT(aKey); 004628 testcase( lhs<0 ); 004629 break; 004630 } 004631 case 3: { /* 3-byte signed integer */ 004632 lhs = THREE_BYTE_INT(aKey); 004633 testcase( lhs<0 ); 004634 break; 004635 } 004636 case 4: { /* 4-byte signed integer */ 004637 y = FOUR_BYTE_UINT(aKey); 004638 lhs = (i64)*(int*)&y; 004639 testcase( lhs<0 ); 004640 break; 004641 } 004642 case 5: { /* 6-byte signed integer */ 004643 lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); 004644 testcase( lhs<0 ); 004645 break; 004646 } 004647 case 6: { /* 8-byte signed integer */ 004648 x = FOUR_BYTE_UINT(aKey); 004649 x = (x<<32) | FOUR_BYTE_UINT(aKey+4); 004650 lhs = *(i64*)&x; 004651 testcase( lhs<0 ); 004652 break; 004653 } 004654 case 8: 004655 lhs = 0; 004656 break; 004657 case 9: 004658 lhs = 1; 004659 break; 004660 004661 /* This case could be removed without changing the results of running 004662 ** this code. Including it causes gcc to generate a faster switch 004663 ** statement (since the range of switch targets now starts at zero and 004664 ** is contiguous) but does not cause any duplicate code to be generated 004665 ** (as gcc is clever enough to combine the two like cases). Other 004666 ** compilers might be similar. */ 004667 case 0: case 7: 004668 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); 004669 004670 default: 004671 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); 004672 } 004673 004674 v = pPKey2->aMem[0].u.i; 004675 if( v>lhs ){ 004676 res = pPKey2->r1; 004677 }else if( v<lhs ){ 004678 res = pPKey2->r2; 004679 }else if( pPKey2->nField>1 ){ 004680 /* The first fields of the two keys are equal. Compare the trailing 004681 ** fields. */ 004682 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); 004683 }else{ 004684 /* The first fields of the two keys are equal and there are no trailing 004685 ** fields. Return pPKey2->default_rc in this case. */ 004686 res = pPKey2->default_rc; 004687 pPKey2->eqSeen = 1; 004688 } 004689 004690 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); 004691 return res; 004692 } 004693 004694 /* 004695 ** This function is an optimized version of sqlite3VdbeRecordCompare() 004696 ** that (a) the first field of pPKey2 is a string, that (b) the first field 004697 ** uses the collation sequence BINARY and (c) that the size-of-header varint 004698 ** at the start of (pKey1/nKey1) fits in a single byte. 004699 */ 004700 static int vdbeRecordCompareString( 004701 int nKey1, const void *pKey1, /* Left key */ 004702 UnpackedRecord *pPKey2 /* Right key */ 004703 ){ 004704 const u8 *aKey1 = (const u8*)pKey1; 004705 int serial_type; 004706 int res; 004707 004708 assert( pPKey2->aMem[0].flags & MEM_Str ); 004709 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); 004710 getVarint32(&aKey1[1], serial_type); 004711 if( serial_type<12 ){ 004712 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ 004713 }else if( !(serial_type & 0x01) ){ 004714 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ 004715 }else{ 004716 int nCmp; 004717 int nStr; 004718 int szHdr = aKey1[0]; 004719 004720 nStr = (serial_type-12) / 2; 004721 if( (szHdr + nStr) > nKey1 ){ 004722 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; 004723 return 0; /* Corruption */ 004724 } 004725 nCmp = MIN( pPKey2->aMem[0].n, nStr ); 004726 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); 004727 004728 if( res>0 ){ 004729 res = pPKey2->r2; 004730 }else if( res<0 ){ 004731 res = pPKey2->r1; 004732 }else{ 004733 res = nStr - pPKey2->aMem[0].n; 004734 if( res==0 ){ 004735 if( pPKey2->nField>1 ){ 004736 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); 004737 }else{ 004738 res = pPKey2->default_rc; 004739 pPKey2->eqSeen = 1; 004740 } 004741 }else if( res>0 ){ 004742 res = pPKey2->r2; 004743 }else{ 004744 res = pPKey2->r1; 004745 } 004746 } 004747 } 004748 004749 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) 004750 || CORRUPT_DB 004751 || pPKey2->pKeyInfo->db->mallocFailed 004752 ); 004753 return res; 004754 } 004755 004756 /* 004757 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function 004758 ** suitable for comparing serialized records to the unpacked record passed 004759 ** as the only argument. 004760 */ 004761 RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){ 004762 /* varintRecordCompareInt() and varintRecordCompareString() both assume 004763 ** that the size-of-header varint that occurs at the start of each record 004764 ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt() 004765 ** also assumes that it is safe to overread a buffer by at least the 004766 ** maximum possible legal header size plus 8 bytes. Because there is 004767 ** guaranteed to be at least 74 (but not 136) bytes of padding following each 004768 ** buffer passed to varintRecordCompareInt() this makes it convenient to 004769 ** limit the size of the header to 64 bytes in cases where the first field 004770 ** is an integer. 004771 ** 004772 ** The easiest way to enforce this limit is to consider only records with 004773 ** 13 fields or less. If the first field is an integer, the maximum legal 004774 ** header size is (12*5 + 1 + 1) bytes. */ 004775 if( p->pKeyInfo->nAllField<=13 ){ 004776 int flags = p->aMem[0].flags; 004777 if( p->pKeyInfo->aSortFlags[0] ){ 004778 if( p->pKeyInfo->aSortFlags[0] & KEYINFO_ORDER_BIGNULL ){ 004779 return sqlite3VdbeRecordCompare; 004780 } 004781 p->r1 = 1; 004782 p->r2 = -1; 004783 }else{ 004784 p->r1 = -1; 004785 p->r2 = 1; 004786 } 004787 if( (flags & MEM_Int) ){ 004788 return vdbeRecordCompareInt; 004789 } 004790 testcase( flags & MEM_Real ); 004791 testcase( flags & MEM_Null ); 004792 testcase( flags & MEM_Blob ); 004793 if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0 004794 && p->pKeyInfo->aColl[0]==0 004795 ){ 004796 assert( flags & MEM_Str ); 004797 return vdbeRecordCompareString; 004798 } 004799 } 004800 004801 return sqlite3VdbeRecordCompare; 004802 } 004803 004804 /* 004805 ** pCur points at an index entry created using the OP_MakeRecord opcode. 004806 ** Read the rowid (the last field in the record) and store it in *rowid. 004807 ** Return SQLITE_OK if everything works, or an error code otherwise. 004808 ** 004809 ** pCur might be pointing to text obtained from a corrupt database file. 004810 ** So the content cannot be trusted. Do appropriate checks on the content. 004811 */ 004812 int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){ 004813 i64 nCellKey = 0; 004814 int rc; 004815 u32 szHdr; /* Size of the header */ 004816 u32 typeRowid; /* Serial type of the rowid */ 004817 u32 lenRowid; /* Size of the rowid */ 004818 Mem m, v; 004819 004820 /* Get the size of the index entry. Only indices entries of less 004821 ** than 2GiB are support - anything large must be database corruption. 004822 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so 004823 ** this code can safely assume that nCellKey is 32-bits 004824 */ 004825 assert( sqlite3BtreeCursorIsValid(pCur) ); 004826 nCellKey = sqlite3BtreePayloadSize(pCur); 004827 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); 004828 004829 /* Read in the complete content of the index entry */ 004830 sqlite3VdbeMemInit(&m, db, 0); 004831 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m); 004832 if( rc ){ 004833 return rc; 004834 } 004835 004836 /* The index entry must begin with a header size */ 004837 (void)getVarint32((u8*)m.z, szHdr); 004838 testcase( szHdr==3 ); 004839 testcase( szHdr==m.n ); 004840 testcase( szHdr>0x7fffffff ); 004841 assert( m.n>=0 ); 004842 if( unlikely(szHdr<3 || szHdr>(unsigned)m.n) ){ 004843 goto idx_rowid_corruption; 004844 } 004845 004846 /* The last field of the index should be an integer - the ROWID. 004847 ** Verify that the last entry really is an integer. */ 004848 (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid); 004849 testcase( typeRowid==1 ); 004850 testcase( typeRowid==2 ); 004851 testcase( typeRowid==3 ); 004852 testcase( typeRowid==4 ); 004853 testcase( typeRowid==5 ); 004854 testcase( typeRowid==6 ); 004855 testcase( typeRowid==8 ); 004856 testcase( typeRowid==9 ); 004857 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ 004858 goto idx_rowid_corruption; 004859 } 004860 lenRowid = sqlite3SmallTypeSizes[typeRowid]; 004861 testcase( (u32)m.n==szHdr+lenRowid ); 004862 if( unlikely((u32)m.n<szHdr+lenRowid) ){ 004863 goto idx_rowid_corruption; 004864 } 004865 004866 /* Fetch the integer off the end of the index record */ 004867 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); 004868 *rowid = v.u.i; 004869 sqlite3VdbeMemRelease(&m); 004870 return SQLITE_OK; 004871 004872 /* Jump here if database corruption is detected after m has been 004873 ** allocated. Free the m object and return SQLITE_CORRUPT. */ 004874 idx_rowid_corruption: 004875 testcase( m.szMalloc!=0 ); 004876 sqlite3VdbeMemRelease(&m); 004877 return SQLITE_CORRUPT_BKPT; 004878 } 004879 004880 /* 004881 ** Compare the key of the index entry that cursor pC is pointing to against 004882 ** the key string in pUnpacked. Write into *pRes a number 004883 ** that is negative, zero, or positive if pC is less than, equal to, 004884 ** or greater than pUnpacked. Return SQLITE_OK on success. 004885 ** 004886 ** pUnpacked is either created without a rowid or is truncated so that it 004887 ** omits the rowid at the end. The rowid at the end of the index entry 004888 ** is ignored as well. Hence, this routine only compares the prefixes 004889 ** of the keys prior to the final rowid, not the entire key. 004890 */ 004891 int sqlite3VdbeIdxKeyCompare( 004892 sqlite3 *db, /* Database connection */ 004893 VdbeCursor *pC, /* The cursor to compare against */ 004894 UnpackedRecord *pUnpacked, /* Unpacked version of key */ 004895 int *res /* Write the comparison result here */ 004896 ){ 004897 i64 nCellKey = 0; 004898 int rc; 004899 BtCursor *pCur; 004900 Mem m; 004901 004902 assert( pC->eCurType==CURTYPE_BTREE ); 004903 pCur = pC->uc.pCursor; 004904 assert( sqlite3BtreeCursorIsValid(pCur) ); 004905 nCellKey = sqlite3BtreePayloadSize(pCur); 004906 /* nCellKey will always be between 0 and 0xffffffff because of the way 004907 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ 004908 if( nCellKey<=0 || nCellKey>0x7fffffff ){ 004909 *res = 0; 004910 return SQLITE_CORRUPT_BKPT; 004911 } 004912 sqlite3VdbeMemInit(&m, db, 0); 004913 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m); 004914 if( rc ){ 004915 return rc; 004916 } 004917 *res = sqlite3VdbeRecordCompareWithSkip(m.n, m.z, pUnpacked, 0); 004918 sqlite3VdbeMemRelease(&m); 004919 return SQLITE_OK; 004920 } 004921 004922 /* 004923 ** This routine sets the value to be returned by subsequent calls to 004924 ** sqlite3_changes() on the database handle 'db'. 004925 */ 004926 void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){ 004927 assert( sqlite3_mutex_held(db->mutex) ); 004928 db->nChange = nChange; 004929 db->nTotalChange += nChange; 004930 } 004931 004932 /* 004933 ** Set a flag in the vdbe to update the change counter when it is finalised 004934 ** or reset. 004935 */ 004936 void sqlite3VdbeCountChanges(Vdbe *v){ 004937 v->changeCntOn = 1; 004938 } 004939 004940 /* 004941 ** Mark every prepared statement associated with a database connection 004942 ** as expired. 004943 ** 004944 ** An expired statement means that recompilation of the statement is 004945 ** recommend. Statements expire when things happen that make their 004946 ** programs obsolete. Removing user-defined functions or collating 004947 ** sequences, or changing an authorization function are the types of 004948 ** things that make prepared statements obsolete. 004949 ** 004950 ** If iCode is 1, then expiration is advisory. The statement should 004951 ** be reprepared before being restarted, but if it is already running 004952 ** it is allowed to run to completion. 004953 ** 004954 ** Internally, this function just sets the Vdbe.expired flag on all 004955 ** prepared statements. The flag is set to 1 for an immediate expiration 004956 ** and set to 2 for an advisory expiration. 004957 */ 004958 void sqlite3ExpirePreparedStatements(sqlite3 *db, int iCode){ 004959 Vdbe *p; 004960 for(p = db->pVdbe; p; p=p->pNext){ 004961 p->expired = iCode+1; 004962 } 004963 } 004964 004965 /* 004966 ** Return the database associated with the Vdbe. 004967 */ 004968 sqlite3 *sqlite3VdbeDb(Vdbe *v){ 004969 return v->db; 004970 } 004971 004972 /* 004973 ** Return the SQLITE_PREPARE flags for a Vdbe. 004974 */ 004975 u8 sqlite3VdbePrepareFlags(Vdbe *v){ 004976 return v->prepFlags; 004977 } 004978 004979 /* 004980 ** Return a pointer to an sqlite3_value structure containing the value bound 004981 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 004982 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_* 004983 ** constants) to the value before returning it. 004984 ** 004985 ** The returned value must be freed by the caller using sqlite3ValueFree(). 004986 */ 004987 sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){ 004988 assert( iVar>0 ); 004989 if( v ){ 004990 Mem *pMem = &v->aVar[iVar-1]; 004991 assert( (v->db->flags & SQLITE_EnableQPSG)==0 ); 004992 if( 0==(pMem->flags & MEM_Null) ){ 004993 sqlite3_value *pRet = sqlite3ValueNew(v->db); 004994 if( pRet ){ 004995 sqlite3VdbeMemCopy((Mem *)pRet, pMem); 004996 sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8); 004997 } 004998 return pRet; 004999 } 005000 } 005001 return 0; 005002 } 005003 005004 /* 005005 ** Configure SQL variable iVar so that binding a new value to it signals 005006 ** to sqlite3_reoptimize() that re-preparing the statement may result 005007 ** in a better query plan. 005008 */ 005009 void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){ 005010 assert( iVar>0 ); 005011 assert( (v->db->flags & SQLITE_EnableQPSG)==0 ); 005012 if( iVar>=32 ){ 005013 v->expmask |= 0x80000000; 005014 }else{ 005015 v->expmask |= ((u32)1 << (iVar-1)); 005016 } 005017 } 005018 005019 /* 005020 ** Cause a function to throw an error if it was call from OP_PureFunc 005021 ** rather than OP_Function. 005022 ** 005023 ** OP_PureFunc means that the function must be deterministic, and should 005024 ** throw an error if it is given inputs that would make it non-deterministic. 005025 ** This routine is invoked by date/time functions that use non-deterministic 005026 ** features such as 'now'. 005027 */ 005028 int sqlite3NotPureFunc(sqlite3_context *pCtx){ 005029 const VdbeOp *pOp; 005030 #ifdef SQLITE_ENABLE_STAT4 005031 if( pCtx->pVdbe==0 ) return 1; 005032 #endif 005033 pOp = pCtx->pVdbe->aOp + pCtx->iOp; 005034 if( pOp->opcode==OP_PureFunc ){ 005035 const char *zContext; 005036 char *zMsg; 005037 if( pOp->p5 & NC_IsCheck ){ 005038 zContext = "a CHECK constraint"; 005039 }else if( pOp->p5 & NC_GenCol ){ 005040 zContext = "a generated column"; 005041 }else{ 005042 zContext = "an index"; 005043 } 005044 zMsg = sqlite3_mprintf("non-deterministic use of %s() in %s", 005045 pCtx->pFunc->zName, zContext); 005046 sqlite3_result_error(pCtx, zMsg, -1); 005047 sqlite3_free(zMsg); 005048 return 0; 005049 } 005050 return 1; 005051 } 005052 005053 #ifndef SQLITE_OMIT_VIRTUALTABLE 005054 /* 005055 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored 005056 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored 005057 ** in memory obtained from sqlite3DbMalloc). 005058 */ 005059 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ 005060 if( pVtab->zErrMsg ){ 005061 sqlite3 *db = p->db; 005062 sqlite3DbFree(db, p->zErrMsg); 005063 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); 005064 sqlite3_free(pVtab->zErrMsg); 005065 pVtab->zErrMsg = 0; 005066 } 005067 } 005068 #endif /* SQLITE_OMIT_VIRTUALTABLE */ 005069 005070 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 005071 005072 /* 005073 ** If the second argument is not NULL, release any allocations associated 005074 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord 005075 ** structure itself, using sqlite3DbFree(). 005076 ** 005077 ** This function is used to free UnpackedRecord structures allocated by 005078 ** the vdbeUnpackRecord() function found in vdbeapi.c. 005079 */ 005080 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ 005081 if( p ){ 005082 int i; 005083 for(i=0; i<nField; i++){ 005084 Mem *pMem = &p->aMem[i]; 005085 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem); 005086 } 005087 sqlite3DbFreeNN(db, p); 005088 } 005089 } 005090 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 005091 005092 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 005093 /* 005094 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call, 005095 ** then cursor passed as the second argument should point to the row about 005096 ** to be update or deleted. If the application calls sqlite3_preupdate_old(), 005097 ** the required value will be read from the row the cursor points to. 005098 */ 005099 void sqlite3VdbePreUpdateHook( 005100 Vdbe *v, /* Vdbe pre-update hook is invoked by */ 005101 VdbeCursor *pCsr, /* Cursor to grab old.* values from */ 005102 int op, /* SQLITE_INSERT, UPDATE or DELETE */ 005103 const char *zDb, /* Database name */ 005104 Table *pTab, /* Modified table */ 005105 i64 iKey1, /* Initial key value */ 005106 int iReg /* Register for new.* record */ 005107 ){ 005108 sqlite3 *db = v->db; 005109 i64 iKey2; 005110 PreUpdate preupdate; 005111 const char *zTbl = pTab->zName; 005112 static const u8 fakeSortOrder = 0; 005113 005114 assert( db->pPreUpdate==0 ); 005115 memset(&preupdate, 0, sizeof(PreUpdate)); 005116 if( HasRowid(pTab)==0 ){ 005117 iKey1 = iKey2 = 0; 005118 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab); 005119 }else{ 005120 if( op==SQLITE_UPDATE ){ 005121 iKey2 = v->aMem[iReg].u.i; 005122 }else{ 005123 iKey2 = iKey1; 005124 } 005125 } 005126 005127 assert( pCsr->nField==pTab->nCol 005128 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1) 005129 ); 005130 005131 preupdate.v = v; 005132 preupdate.pCsr = pCsr; 005133 preupdate.op = op; 005134 preupdate.iNewReg = iReg; 005135 preupdate.keyinfo.db = db; 005136 preupdate.keyinfo.enc = ENC(db); 005137 preupdate.keyinfo.nKeyField = pTab->nCol; 005138 preupdate.keyinfo.aSortFlags = (u8*)&fakeSortOrder; 005139 preupdate.iKey1 = iKey1; 005140 preupdate.iKey2 = iKey2; 005141 preupdate.pTab = pTab; 005142 005143 db->pPreUpdate = &preupdate; 005144 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); 005145 db->pPreUpdate = 0; 005146 sqlite3DbFree(db, preupdate.aRecord); 005147 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pUnpacked); 005148 vdbeFreeUnpacked(db, preupdate.keyinfo.nKeyField+1, preupdate.pNewUnpacked); 005149 if( preupdate.aNew ){ 005150 int i; 005151 for(i=0; i<pCsr->nField; i++){ 005152 sqlite3VdbeMemRelease(&preupdate.aNew[i]); 005153 } 005154 sqlite3DbFreeNN(db, preupdate.aNew); 005155 } 005156 } 005157 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */