| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2003 September 6 | 2 ** 2003 September 6 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains code used for creating, destroying, and populating | 12 ** This file contains code used for creating, destroying, and populating |
| 13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) | 13 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.) |
| 14 */ | 14 */ |
| 15 #include "sqliteInt.h" | 15 #include "sqliteInt.h" |
| 16 #include "vdbeInt.h" | 16 #include "vdbeInt.h" |
| 17 | 17 |
| 18 /* | 18 /* |
| 19 ** Create a new virtual database engine. | 19 ** Create a new virtual database engine. |
| 20 */ | 20 */ |
| 21 Vdbe *sqlite3VdbeCreate(Parse *pParse){ | 21 Vdbe *sqlite3VdbeCreate(Parse *pParse){ |
| 22 sqlite3 *db = pParse->db; | 22 sqlite3 *db = pParse->db; |
| 23 Vdbe *p; | 23 Vdbe *p; |
| 24 p = sqlite3DbMallocZero(db, sizeof(Vdbe) ); | 24 p = sqlite3DbMallocRawNN(db, sizeof(Vdbe) ); |
| 25 if( p==0 ) return 0; | 25 if( p==0 ) return 0; |
| 26 memset(&p->aOp, 0, sizeof(Vdbe)-offsetof(Vdbe,aOp)); |
| 26 p->db = db; | 27 p->db = db; |
| 27 if( db->pVdbe ){ | 28 if( db->pVdbe ){ |
| 28 db->pVdbe->pPrev = p; | 29 db->pVdbe->pPrev = p; |
| 29 } | 30 } |
| 30 p->pNext = db->pVdbe; | 31 p->pNext = db->pVdbe; |
| 31 p->pPrev = 0; | 32 p->pPrev = 0; |
| 32 db->pVdbe = p; | 33 db->pVdbe = p; |
| 33 p->magic = VDBE_MAGIC_INIT; | 34 p->magic = VDBE_MAGIC_INIT; |
| 34 p->pParse = pParse; | 35 p->pParse = pParse; |
| 35 assert( pParse->aLabel==0 ); | 36 assert( pParse->aLabel==0 ); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 58 if( p==0 ) return; | 59 if( p==0 ) return; |
| 59 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) | 60 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) |
| 60 if( !isPrepareV2 ) return; | 61 if( !isPrepareV2 ) return; |
| 61 #endif | 62 #endif |
| 62 assert( p->zSql==0 ); | 63 assert( p->zSql==0 ); |
| 63 p->zSql = sqlite3DbStrNDup(p->db, z, n); | 64 p->zSql = sqlite3DbStrNDup(p->db, z, n); |
| 64 p->isPrepareV2 = (u8)isPrepareV2; | 65 p->isPrepareV2 = (u8)isPrepareV2; |
| 65 } | 66 } |
| 66 | 67 |
| 67 /* | 68 /* |
| 68 ** Return the SQL associated with a prepared statement | |
| 69 */ | |
| 70 const char *sqlite3_sql(sqlite3_stmt *pStmt){ | |
| 71 Vdbe *p = (Vdbe *)pStmt; | |
| 72 return p ? p->zSql : 0; | |
| 73 } | |
| 74 | |
| 75 /* | |
| 76 ** Swap all content between two VDBE structures. | 69 ** Swap all content between two VDBE structures. |
| 77 */ | 70 */ |
| 78 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ | 71 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
| 79 Vdbe tmp, *pTmp; | 72 Vdbe tmp, *pTmp; |
| 80 char *zTmp; | 73 char *zTmp; |
| 74 assert( pA->db==pB->db ); |
| 81 tmp = *pA; | 75 tmp = *pA; |
| 82 *pA = *pB; | 76 *pA = *pB; |
| 83 *pB = tmp; | 77 *pB = tmp; |
| 84 pTmp = pA->pNext; | 78 pTmp = pA->pNext; |
| 85 pA->pNext = pB->pNext; | 79 pA->pNext = pB->pNext; |
| 86 pB->pNext = pTmp; | 80 pB->pNext = pTmp; |
| 87 pTmp = pA->pPrev; | 81 pTmp = pA->pPrev; |
| 88 pA->pPrev = pB->pPrev; | 82 pA->pPrev = pB->pPrev; |
| 89 pB->pPrev = pTmp; | 83 pB->pPrev = pTmp; |
| 90 zTmp = pA->zSql; | 84 zTmp = pA->zSql; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 #endif | 116 #endif |
| 123 | 117 |
| 124 assert( nOp<=(1024/sizeof(Op)) ); | 118 assert( nOp<=(1024/sizeof(Op)) ); |
| 125 assert( nNew>=(p->nOpAlloc+nOp) ); | 119 assert( nNew>=(p->nOpAlloc+nOp) ); |
| 126 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); | 120 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); |
| 127 if( pNew ){ | 121 if( pNew ){ |
| 128 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); | 122 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); |
| 129 p->nOpAlloc = p->szOpAlloc/sizeof(Op); | 123 p->nOpAlloc = p->szOpAlloc/sizeof(Op); |
| 130 v->aOp = pNew; | 124 v->aOp = pNew; |
| 131 } | 125 } |
| 132 return (pNew ? SQLITE_OK : SQLITE_NOMEM); | 126 return (pNew ? SQLITE_OK : SQLITE_NOMEM_BKPT); |
| 133 } | 127 } |
| 134 | 128 |
| 135 #ifdef SQLITE_DEBUG | 129 #ifdef SQLITE_DEBUG |
| 136 /* This routine is just a convenient place to set a breakpoint that will | 130 /* This routine is just a convenient place to set a breakpoint that will |
| 137 ** fire after each opcode is inserted and displayed using | 131 ** fire after each opcode is inserted and displayed using |
| 138 ** "PRAGMA vdbe_addoptrace=on". | 132 ** "PRAGMA vdbe_addoptrace=on". |
| 139 */ | 133 */ |
| 140 static void test_addop_breakpoint(void){ | 134 static void test_addop_breakpoint(void){ |
| 141 static int n = 0; | 135 static int n = 0; |
| 142 n++; | 136 n++; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 164 if( growOpArray(p, 1) ) return 1; | 158 if( growOpArray(p, 1) ) return 1; |
| 165 assert( p->pParse->nOpAlloc>p->nOp ); | 159 assert( p->pParse->nOpAlloc>p->nOp ); |
| 166 return sqlite3VdbeAddOp3(p, op, p1, p2, p3); | 160 return sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 167 } | 161 } |
| 168 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ | 162 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
| 169 int i; | 163 int i; |
| 170 VdbeOp *pOp; | 164 VdbeOp *pOp; |
| 171 | 165 |
| 172 i = p->nOp; | 166 i = p->nOp; |
| 173 assert( p->magic==VDBE_MAGIC_INIT ); | 167 assert( p->magic==VDBE_MAGIC_INIT ); |
| 174 assert( op>0 && op<0xff ); | 168 assert( op>=0 && op<0xff ); |
| 175 if( p->pParse->nOpAlloc<=i ){ | 169 if( p->pParse->nOpAlloc<=i ){ |
| 176 return growOp3(p, op, p1, p2, p3); | 170 return growOp3(p, op, p1, p2, p3); |
| 177 } | 171 } |
| 178 p->nOp++; | 172 p->nOp++; |
| 179 pOp = &p->aOp[i]; | 173 pOp = &p->aOp[i]; |
| 180 pOp->opcode = (u8)op; | 174 pOp->opcode = (u8)op; |
| 181 pOp->p5 = 0; | 175 pOp->p5 = 0; |
| 182 pOp->p1 = p1; | 176 pOp->p1 = p1; |
| 183 pOp->p2 = p2; | 177 pOp->p2 = p2; |
| 184 pOp->p3 = p3; | 178 pOp->p3 = p3; |
| 185 pOp->p4.p = 0; | 179 pOp->p4.p = 0; |
| 186 pOp->p4type = P4_NOTUSED; | 180 pOp->p4type = P4_NOTUSED; |
| 187 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | 181 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 188 pOp->zComment = 0; | 182 pOp->zComment = 0; |
| 189 #endif | 183 #endif |
| 190 #ifdef SQLITE_DEBUG | 184 #ifdef SQLITE_DEBUG |
| 191 if( p->db->flags & SQLITE_VdbeAddopTrace ){ | 185 if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
| 192 int jj, kk; | 186 int jj, kk; |
| 193 Parse *pParse = p->pParse; | 187 Parse *pParse = p->pParse; |
| 194 for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){ | 188 for(jj=kk=0; jj<pParse->nColCache; jj++){ |
| 195 struct yColCache *x = pParse->aColCache + jj; | 189 struct yColCache *x = pParse->aColCache + jj; |
| 196 if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue; | |
| 197 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); | 190 printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn); |
| 198 kk++; | 191 kk++; |
| 199 } | 192 } |
| 200 if( kk ) printf("\n"); | 193 if( kk ) printf("\n"); |
| 201 sqlite3VdbePrintOp(0, i, &p->aOp[i]); | 194 sqlite3VdbePrintOp(0, i, &p->aOp[i]); |
| 202 test_addop_breakpoint(); | 195 test_addop_breakpoint(); |
| 203 } | 196 } |
| 204 #endif | 197 #endif |
| 205 #ifdef VDBE_PROFILE | 198 #ifdef VDBE_PROFILE |
| 206 pOp->cycles = 0; | 199 pOp->cycles = 0; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 ** in zTypes[], the register is initialized to an integer. | 236 ** in zTypes[], the register is initialized to an integer. |
| 244 */ | 237 */ |
| 245 void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ | 238 void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ |
| 246 va_list ap; | 239 va_list ap; |
| 247 int i; | 240 int i; |
| 248 char c; | 241 char c; |
| 249 va_start(ap, zTypes); | 242 va_start(ap, zTypes); |
| 250 for(i=0; (c = zTypes[i])!=0; i++){ | 243 for(i=0; (c = zTypes[i])!=0; i++){ |
| 251 if( c=='s' ){ | 244 if( c=='s' ){ |
| 252 const char *z = va_arg(ap, const char*); | 245 const char *z = va_arg(ap, const char*); |
| 253 int addr = sqlite3VdbeAddOp2(p, z==0 ? OP_Null : OP_String8, 0, iDest++); | 246 sqlite3VdbeAddOp4(p, z==0 ? OP_Null : OP_String8, 0, iDest++, 0, z, 0); |
| 254 if( z ) sqlite3VdbeChangeP4(p, addr, z, 0); | |
| 255 }else{ | 247 }else{ |
| 256 assert( c=='i' ); | 248 assert( c=='i' ); |
| 257 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++); | 249 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++); |
| 258 } | 250 } |
| 259 } | 251 } |
| 260 va_end(ap); | 252 va_end(ap); |
| 261 } | 253 } |
| 262 | 254 |
| 263 /* | 255 /* |
| 264 ** Add an opcode that includes the p4 value as a pointer. | 256 ** Add an opcode that includes the p4 value as a pointer. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 283 */ | 275 */ |
| 284 int sqlite3VdbeAddOp4Dup8( | 276 int sqlite3VdbeAddOp4Dup8( |
| 285 Vdbe *p, /* Add the opcode to this VM */ | 277 Vdbe *p, /* Add the opcode to this VM */ |
| 286 int op, /* The new opcode */ | 278 int op, /* The new opcode */ |
| 287 int p1, /* The P1 operand */ | 279 int p1, /* The P1 operand */ |
| 288 int p2, /* The P2 operand */ | 280 int p2, /* The P2 operand */ |
| 289 int p3, /* The P3 operand */ | 281 int p3, /* The P3 operand */ |
| 290 const u8 *zP4, /* The P4 operand */ | 282 const u8 *zP4, /* The P4 operand */ |
| 291 int p4type /* P4 operand type */ | 283 int p4type /* P4 operand type */ |
| 292 ){ | 284 ){ |
| 293 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8); | 285 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8); |
| 294 if( p4copy ) memcpy(p4copy, zP4, 8); | 286 if( p4copy ) memcpy(p4copy, zP4, 8); |
| 295 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); | 287 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); |
| 296 } | 288 } |
| 297 | 289 |
| 298 /* | 290 /* |
| 299 ** Add an OP_ParseSchema opcode. This routine is broken out from | 291 ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 300 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees | 292 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
| 301 ** as having been used. | 293 ** as having been used. |
| 302 ** | 294 ** |
| 303 ** The zWhere string must have been obtained from sqlite3_malloc(). | 295 ** The zWhere string must have been obtained from sqlite3_malloc(). |
| 304 ** This routine will take ownership of the allocated memory. | 296 ** This routine will take ownership of the allocated memory. |
| 305 */ | 297 */ |
| 306 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ | 298 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ |
| 307 int j; | 299 int j; |
| 308 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); | 300 sqlite3VdbeAddOp4(p, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); |
| 309 sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC); | |
| 310 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); | 301 for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j); |
| 311 } | 302 } |
| 312 | 303 |
| 313 /* | 304 /* |
| 314 ** Add an opcode that includes the p4 value as an integer. | 305 ** Add an opcode that includes the p4 value as an integer. |
| 315 */ | 306 */ |
| 316 int sqlite3VdbeAddOp4Int( | 307 int sqlite3VdbeAddOp4Int( |
| 317 Vdbe *p, /* Add the opcode to this VM */ | 308 Vdbe *p, /* Add the opcode to this VM */ |
| 318 int op, /* The new opcode */ | 309 int op, /* The new opcode */ |
| 319 int p1, /* The P1 operand */ | 310 int p1, /* The P1 operand */ |
| 320 int p2, /* The P2 operand */ | 311 int p2, /* The P2 operand */ |
| 321 int p3, /* The P3 operand */ | 312 int p3, /* The P3 operand */ |
| 322 int p4 /* The P4 operand as an integer */ | 313 int p4 /* The P4 operand as an integer */ |
| 323 ){ | 314 ){ |
| 324 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | 315 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 325 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32); | 316 if( p->db->mallocFailed==0 ){ |
| 317 VdbeOp *pOp = &p->aOp[addr]; |
| 318 pOp->p4type = P4_INT32; |
| 319 pOp->p4.i = p4; |
| 320 } |
| 326 return addr; | 321 return addr; |
| 327 } | 322 } |
| 328 | 323 |
| 324 /* Insert the end of a co-routine |
| 325 */ |
| 326 void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){ |
| 327 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); |
| 328 |
| 329 /* Clear the temporary register cache, thereby ensuring that each |
| 330 ** co-routine has its own independent set of registers, because co-routines |
| 331 ** might expect their registers to be preserved across an OP_Yield, and |
| 332 ** that could cause problems if two or more co-routines are using the same |
| 333 ** temporary register. |
| 334 */ |
| 335 v->pParse->nTempReg = 0; |
| 336 v->pParse->nRangeReg = 0; |
| 337 } |
| 338 |
| 329 /* | 339 /* |
| 330 ** Create a new symbolic label for an instruction that has yet to be | 340 ** Create a new symbolic label for an instruction that has yet to be |
| 331 ** coded. The symbolic label is really just a negative number. The | 341 ** coded. The symbolic label is really just a negative number. The |
| 332 ** label can be used as the P2 value of an operation. Later, when | 342 ** label can be used as the P2 value of an operation. Later, when |
| 333 ** the label is resolved to a specific address, the VDBE will scan | 343 ** the label is resolved to a specific address, the VDBE will scan |
| 334 ** through its operation list and change all values of P2 which match | 344 ** through its operation list and change all values of P2 which match |
| 335 ** the label into the resolved address. | 345 ** the label into the resolved address. |
| 336 ** | 346 ** |
| 337 ** The VDBE knows that a P2 value is a label because labels are | 347 ** The VDBE knows that a P2 value is a label because labels are |
| 338 ** always negative and P2 values are suppose to be non-negative. | 348 ** always negative and P2 values are suppose to be non-negative. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 361 */ | 371 */ |
| 362 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ | 372 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 363 Parse *p = v->pParse; | 373 Parse *p = v->pParse; |
| 364 int j = ADDR(x); | 374 int j = ADDR(x); |
| 365 assert( v->magic==VDBE_MAGIC_INIT ); | 375 assert( v->magic==VDBE_MAGIC_INIT ); |
| 366 assert( j<p->nLabel ); | 376 assert( j<p->nLabel ); |
| 367 assert( j>=0 ); | 377 assert( j>=0 ); |
| 368 if( p->aLabel ){ | 378 if( p->aLabel ){ |
| 369 p->aLabel[j] = v->nOp; | 379 p->aLabel[j] = v->nOp; |
| 370 } | 380 } |
| 371 p->iFixedOp = v->nOp - 1; | |
| 372 } | 381 } |
| 373 | 382 |
| 374 /* | 383 /* |
| 375 ** Mark the VDBE as one that can only be run one time. | 384 ** Mark the VDBE as one that can only be run one time. |
| 376 */ | 385 */ |
| 377 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ | 386 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ |
| 378 p->runOnlyOnce = 1; | 387 p->runOnlyOnce = 1; |
| 379 } | 388 } |
| 380 | 389 |
| 390 /* |
| 391 ** Mark the VDBE as one that can only be run multiple times. |
| 392 */ |
| 393 void sqlite3VdbeReusable(Vdbe *p){ |
| 394 p->runOnlyOnce = 0; |
| 395 } |
| 396 |
| 381 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ | 397 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */ |
| 382 | 398 |
| 383 /* | 399 /* |
| 384 ** The following type and function are used to iterate through all opcodes | 400 ** The following type and function are used to iterate through all opcodes |
| 385 ** in a Vdbe main program and each of the sub-programs (triggers) it may | 401 ** in a Vdbe main program and each of the sub-programs (triggers) it may |
| 386 ** invoke directly or indirectly. It should be used as follows: | 402 ** invoke directly or indirectly. It should be used as follows: |
| 387 ** | 403 ** |
| 388 ** Op *pOp; | 404 ** Op *pOp; |
| 389 ** VdbeOpIter sIter; | 405 ** VdbeOpIter sIter; |
| 390 ** | 406 ** |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 ** | 533 ** |
| 518 ** (2) Compute the maximum number of arguments used by any SQL function | 534 ** (2) Compute the maximum number of arguments used by any SQL function |
| 519 ** and store that value in *pMaxFuncArgs. | 535 ** and store that value in *pMaxFuncArgs. |
| 520 ** | 536 ** |
| 521 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately | 537 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately |
| 522 ** indicate what the prepared statement actually does. | 538 ** indicate what the prepared statement actually does. |
| 523 ** | 539 ** |
| 524 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it. | 540 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it. |
| 525 ** | 541 ** |
| 526 ** (5) Reclaim the memory allocated for storing labels. | 542 ** (5) Reclaim the memory allocated for storing labels. |
| 543 ** |
| 544 ** This routine will only function correctly if the mkopcodeh.tcl generator |
| 545 ** script numbers the opcodes correctly. Changes to this routine must be |
| 546 ** coordinated with changes to mkopcodeh.tcl. |
| 527 */ | 547 */ |
| 528 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ | 548 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
| 529 int i; | |
| 530 int nMaxArgs = *pMaxFuncArgs; | 549 int nMaxArgs = *pMaxFuncArgs; |
| 531 Op *pOp; | 550 Op *pOp; |
| 532 Parse *pParse = p->pParse; | 551 Parse *pParse = p->pParse; |
| 533 int *aLabel = pParse->aLabel; | 552 int *aLabel = pParse->aLabel; |
| 534 p->readOnly = 1; | 553 p->readOnly = 1; |
| 535 p->bIsReader = 0; | 554 p->bIsReader = 0; |
| 536 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ | 555 pOp = &p->aOp[p->nOp-1]; |
| 537 u8 opcode = pOp->opcode; | 556 while(1){ |
| 538 | 557 |
| 539 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing | 558 /* Only JUMP opcodes and the short list of special opcodes in the switch |
| 540 ** cases from this switch! */ | 559 ** below need to be considered. The mkopcodeh.tcl generator script groups |
| 541 switch( opcode ){ | 560 ** all these opcodes together near the front of the opcode list. Skip |
| 542 case OP_Transaction: { | 561 ** any opcode that does not need processing by virtual of the fact that |
| 543 if( pOp->p2!=0 ) p->readOnly = 0; | 562 ** it is larger than SQLITE_MX_JUMP_OPCODE, as a performance optimization. |
| 544 /* fall thru */ | 563 */ |
| 564 if( pOp->opcode<=SQLITE_MX_JUMP_OPCODE ){ |
| 565 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing |
| 566 ** cases from this switch! */ |
| 567 switch( pOp->opcode ){ |
| 568 case OP_Transaction: { |
| 569 if( pOp->p2!=0 ) p->readOnly = 0; |
| 570 /* fall thru */ |
| 571 } |
| 572 case OP_AutoCommit: |
| 573 case OP_Savepoint: { |
| 574 p->bIsReader = 1; |
| 575 break; |
| 576 } |
| 577 #ifndef SQLITE_OMIT_WAL |
| 578 case OP_Checkpoint: |
| 579 #endif |
| 580 case OP_Vacuum: |
| 581 case OP_JournalMode: { |
| 582 p->readOnly = 0; |
| 583 p->bIsReader = 1; |
| 584 break; |
| 585 } |
| 586 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 587 case OP_VUpdate: { |
| 588 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; |
| 589 break; |
| 590 } |
| 591 case OP_VFilter: { |
| 592 int n; |
| 593 assert( (pOp - p->aOp) >= 3 ); |
| 594 assert( pOp[-1].opcode==OP_Integer ); |
| 595 n = pOp[-1].p1; |
| 596 if( n>nMaxArgs ) nMaxArgs = n; |
| 597 break; |
| 598 } |
| 599 #endif |
| 600 case OP_Next: |
| 601 case OP_NextIfOpen: |
| 602 case OP_SorterNext: { |
| 603 pOp->p4.xAdvance = sqlite3BtreeNext; |
| 604 pOp->p4type = P4_ADVANCE; |
| 605 break; |
| 606 } |
| 607 case OP_Prev: |
| 608 case OP_PrevIfOpen: { |
| 609 pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 610 pOp->p4type = P4_ADVANCE; |
| 611 break; |
| 612 } |
| 545 } | 613 } |
| 546 case OP_AutoCommit: | 614 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 547 case OP_Savepoint: { | 615 assert( ADDR(pOp->p2)<pParse->nLabel ); |
| 548 p->bIsReader = 1; | 616 pOp->p2 = aLabel[ADDR(pOp->p2)]; |
| 549 break; | |
| 550 } | |
| 551 #ifndef SQLITE_OMIT_WAL | |
| 552 case OP_Checkpoint: | |
| 553 #endif | |
| 554 case OP_Vacuum: | |
| 555 case OP_JournalMode: { | |
| 556 p->readOnly = 0; | |
| 557 p->bIsReader = 1; | |
| 558 break; | |
| 559 } | |
| 560 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 561 case OP_VUpdate: { | |
| 562 if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2; | |
| 563 break; | |
| 564 } | |
| 565 case OP_VFilter: { | |
| 566 int n; | |
| 567 assert( p->nOp - i >= 3 ); | |
| 568 assert( pOp[-1].opcode==OP_Integer ); | |
| 569 n = pOp[-1].p1; | |
| 570 if( n>nMaxArgs ) nMaxArgs = n; | |
| 571 break; | |
| 572 } | |
| 573 #endif | |
| 574 case OP_Next: | |
| 575 case OP_NextIfOpen: | |
| 576 case OP_SorterNext: { | |
| 577 pOp->p4.xAdvance = sqlite3BtreeNext; | |
| 578 pOp->p4type = P4_ADVANCE; | |
| 579 break; | |
| 580 } | |
| 581 case OP_Prev: | |
| 582 case OP_PrevIfOpen: { | |
| 583 pOp->p4.xAdvance = sqlite3BtreePrevious; | |
| 584 pOp->p4type = P4_ADVANCE; | |
| 585 break; | |
| 586 } | 617 } |
| 587 } | 618 } |
| 588 | 619 if( pOp==p->aOp ) break; |
| 589 pOp->opflags = sqlite3OpcodeProperty[opcode]; | 620 pOp--; |
| 590 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ | |
| 591 assert( ADDR(pOp->p2)<pParse->nLabel ); | |
| 592 pOp->p2 = aLabel[ADDR(pOp->p2)]; | |
| 593 } | |
| 594 } | 621 } |
| 595 sqlite3DbFree(p->db, pParse->aLabel); | 622 sqlite3DbFree(p->db, pParse->aLabel); |
| 596 pParse->aLabel = 0; | 623 pParse->aLabel = 0; |
| 597 pParse->nLabel = 0; | 624 pParse->nLabel = 0; |
| 598 *pMaxFuncArgs = nMaxArgs; | 625 *pMaxFuncArgs = nMaxArgs; |
| 599 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); | 626 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
| 600 } | 627 } |
| 601 | 628 |
| 602 /* | 629 /* |
| 603 ** Return the address of the next instruction to be inserted. | 630 ** Return the address of the next instruction to be inserted. |
| 604 */ | 631 */ |
| 605 int sqlite3VdbeCurrentAddr(Vdbe *p){ | 632 int sqlite3VdbeCurrentAddr(Vdbe *p){ |
| 606 assert( p->magic==VDBE_MAGIC_INIT ); | 633 assert( p->magic==VDBE_MAGIC_INIT ); |
| 607 return p->nOp; | 634 return p->nOp; |
| 608 } | 635 } |
| 609 | 636 |
| 610 /* | 637 /* |
| 638 ** Verify that at least N opcode slots are available in p without |
| 639 ** having to malloc for more space (except when compiled using |
| 640 ** SQLITE_TEST_REALLOC_STRESS). This interface is used during testing |
| 641 ** to verify that certain calls to sqlite3VdbeAddOpList() can never |
| 642 ** fail due to a OOM fault and hence that the return value from |
| 643 ** sqlite3VdbeAddOpList() will always be non-NULL. |
| 644 */ |
| 645 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) |
| 646 void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N){ |
| 647 assert( p->nOp + N <= p->pParse->nOpAlloc ); |
| 648 } |
| 649 #endif |
| 650 |
| 651 /* |
| 652 ** Verify that the VM passed as the only argument does not contain |
| 653 ** an OP_ResultRow opcode. Fail an assert() if it does. This is used |
| 654 ** by code in pragma.c to ensure that the implementation of certain |
| 655 ** pragmas comports with the flags specified in the mkpragmatab.tcl |
| 656 ** script. |
| 657 */ |
| 658 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS) |
| 659 void sqlite3VdbeVerifyNoResultRow(Vdbe *p){ |
| 660 int i; |
| 661 for(i=0; i<p->nOp; i++){ |
| 662 assert( p->aOp[i].opcode!=OP_ResultRow ); |
| 663 } |
| 664 } |
| 665 #endif |
| 666 |
| 667 /* |
| 611 ** This function returns a pointer to the array of opcodes associated with | 668 ** This function returns a pointer to the array of opcodes associated with |
| 612 ** the Vdbe passed as the first argument. It is the callers responsibility | 669 ** the Vdbe passed as the first argument. It is the callers responsibility |
| 613 ** to arrange for the returned array to be eventually freed using the | 670 ** to arrange for the returned array to be eventually freed using the |
| 614 ** vdbeFreeOpArray() function. | 671 ** vdbeFreeOpArray() function. |
| 615 ** | 672 ** |
| 616 ** Before returning, *pnOp is set to the number of entries in the returned | 673 ** Before returning, *pnOp is set to the number of entries in the returned |
| 617 ** array. Also, *pnMaxArg is set to the larger of its current value and | 674 ** array. Also, *pnMaxArg is set to the larger of its current value and |
| 618 ** the number of entries in the Vdbe.apArg[] array required to execute the | 675 ** the number of entries in the Vdbe.apArg[] array required to execute the |
| 619 ** returned program. | 676 ** returned program. |
| 620 */ | 677 */ |
| 621 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ | 678 VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){ |
| 622 VdbeOp *aOp = p->aOp; | 679 VdbeOp *aOp = p->aOp; |
| 623 assert( aOp && !p->db->mallocFailed ); | 680 assert( aOp && !p->db->mallocFailed ); |
| 624 | 681 |
| 625 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ | 682 /* Check that sqlite3VdbeUsesBtree() was not called on this VM */ |
| 626 assert( DbMaskAllZero(p->btreeMask) ); | 683 assert( DbMaskAllZero(p->btreeMask) ); |
| 627 | 684 |
| 628 resolveP2Values(p, pnMaxArg); | 685 resolveP2Values(p, pnMaxArg); |
| 629 *pnOp = p->nOp; | 686 *pnOp = p->nOp; |
| 630 p->aOp = 0; | 687 p->aOp = 0; |
| 631 return aOp; | 688 return aOp; |
| 632 } | 689 } |
| 633 | 690 |
| 634 /* | 691 /* |
| 635 ** Add a whole list of operations to the operation stack. Return the | 692 ** Add a whole list of operations to the operation stack. Return a |
| 636 ** address of the first operation added. | 693 ** pointer to the first operation inserted. |
| 694 ** |
| 695 ** Non-zero P2 arguments to jump instructions are automatically adjusted |
| 696 ** so that the jump target is relative to the first operation inserted. |
| 637 */ | 697 */ |
| 638 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ | 698 VdbeOp *sqlite3VdbeAddOpList( |
| 639 int addr, i; | 699 Vdbe *p, /* Add opcodes to the prepared statement */ |
| 640 VdbeOp *pOut; | 700 int nOp, /* Number of opcodes to add */ |
| 701 VdbeOpList const *aOp, /* The opcodes to be added */ |
| 702 int iLineno /* Source-file line number of first opcode */ |
| 703 ){ |
| 704 int i; |
| 705 VdbeOp *pOut, *pFirst; |
| 641 assert( nOp>0 ); | 706 assert( nOp>0 ); |
| 642 assert( p->magic==VDBE_MAGIC_INIT ); | 707 assert( p->magic==VDBE_MAGIC_INIT ); |
| 643 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ | 708 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ |
| 644 return 0; | 709 return 0; |
| 645 } | 710 } |
| 646 addr = p->nOp; | 711 pFirst = pOut = &p->aOp[p->nOp]; |
| 647 pOut = &p->aOp[addr]; | |
| 648 for(i=0; i<nOp; i++, aOp++, pOut++){ | 712 for(i=0; i<nOp; i++, aOp++, pOut++){ |
| 649 pOut->opcode = aOp->opcode; | 713 pOut->opcode = aOp->opcode; |
| 650 pOut->p1 = aOp->p1; | 714 pOut->p1 = aOp->p1; |
| 651 pOut->p2 = aOp->p2; | 715 pOut->p2 = aOp->p2; |
| 652 assert( aOp->p2>=0 ); | 716 assert( aOp->p2>=0 ); |
| 717 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){ |
| 718 pOut->p2 += p->nOp; |
| 719 } |
| 653 pOut->p3 = aOp->p3; | 720 pOut->p3 = aOp->p3; |
| 654 pOut->p4type = P4_NOTUSED; | 721 pOut->p4type = P4_NOTUSED; |
| 655 pOut->p4.p = 0; | 722 pOut->p4.p = 0; |
| 656 pOut->p5 = 0; | 723 pOut->p5 = 0; |
| 657 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | 724 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 658 pOut->zComment = 0; | 725 pOut->zComment = 0; |
| 659 #endif | 726 #endif |
| 660 #ifdef SQLITE_VDBE_COVERAGE | 727 #ifdef SQLITE_VDBE_COVERAGE |
| 661 pOut->iSrcLine = iLineno+i; | 728 pOut->iSrcLine = iLineno+i; |
| 662 #else | 729 #else |
| 663 (void)iLineno; | 730 (void)iLineno; |
| 664 #endif | 731 #endif |
| 665 #ifdef SQLITE_DEBUG | 732 #ifdef SQLITE_DEBUG |
| 666 if( p->db->flags & SQLITE_VdbeAddopTrace ){ | 733 if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
| 667 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); | 734 sqlite3VdbePrintOp(0, i+p->nOp, &p->aOp[i+p->nOp]); |
| 668 } | 735 } |
| 669 #endif | 736 #endif |
| 670 } | 737 } |
| 671 p->nOp += nOp; | 738 p->nOp += nOp; |
| 672 return addr; | 739 return pFirst; |
| 673 } | 740 } |
| 674 | 741 |
| 675 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) | 742 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 676 /* | 743 /* |
| 677 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). | 744 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). |
| 678 */ | 745 */ |
| 679 void sqlite3VdbeScanStatus( | 746 void sqlite3VdbeScanStatus( |
| 680 Vdbe *p, /* VM to add scanstatus() to */ | 747 Vdbe *p, /* VM to add scanstatus() to */ |
| 681 int addrExplain, /* Address of OP_Explain (or 0) */ | 748 int addrExplain, /* Address of OP_Explain (or 0) */ |
| 682 int addrLoop, /* Address of loop counter */ | 749 int addrLoop, /* Address of loop counter */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 709 } | 776 } |
| 710 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ | 777 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
| 711 sqlite3VdbeGetOp(p,addr)->p1 = val; | 778 sqlite3VdbeGetOp(p,addr)->p1 = val; |
| 712 } | 779 } |
| 713 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ | 780 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
| 714 sqlite3VdbeGetOp(p,addr)->p2 = val; | 781 sqlite3VdbeGetOp(p,addr)->p2 = val; |
| 715 } | 782 } |
| 716 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ | 783 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
| 717 sqlite3VdbeGetOp(p,addr)->p3 = val; | 784 sqlite3VdbeGetOp(p,addr)->p3 = val; |
| 718 } | 785 } |
| 719 void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ | 786 void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){ |
| 720 sqlite3VdbeGetOp(p,-1)->p5 = p5; | 787 assert( p->nOp>0 || p->db->mallocFailed ); |
| 788 if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5; |
| 721 } | 789 } |
| 722 | 790 |
| 723 /* | 791 /* |
| 724 ** Change the P2 operand of instruction addr so that it points to | 792 ** Change the P2 operand of instruction addr so that it points to |
| 725 ** the address of the next instruction to be coded. | 793 ** the address of the next instruction to be coded. |
| 726 */ | 794 */ |
| 727 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ | 795 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 728 p->pParse->iFixedOp = p->nOp - 1; | |
| 729 sqlite3VdbeChangeP2(p, addr, p->nOp); | 796 sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 730 } | 797 } |
| 731 | 798 |
| 732 | 799 |
| 733 /* | 800 /* |
| 734 ** If the input FuncDef structure is ephemeral, then free it. If | 801 ** If the input FuncDef structure is ephemeral, then free it. If |
| 735 ** the FuncDef is not ephermal, then do nothing. | 802 ** the FuncDef is not ephermal, then do nothing. |
| 736 */ | 803 */ |
| 737 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ | 804 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
| 738 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ | 805 if( (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ |
| 739 sqlite3DbFree(db, pDef); | 806 sqlite3DbFree(db, pDef); |
| 740 } | 807 } |
| 741 } | 808 } |
| 742 | 809 |
| 743 static void vdbeFreeOpArray(sqlite3 *, Op *, int); | 810 static void vdbeFreeOpArray(sqlite3 *, Op *, int); |
| 744 | 811 |
| 745 /* | 812 /* |
| 746 ** Delete a P4 value if necessary. | 813 ** Delete a P4 value if necessary. |
| 747 */ | 814 */ |
| 815 static SQLITE_NOINLINE void freeP4Mem(sqlite3 *db, Mem *p){ |
| 816 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
| 817 sqlite3DbFree(db, p); |
| 818 } |
| 819 static SQLITE_NOINLINE void freeP4FuncCtx(sqlite3 *db, sqlite3_context *p){ |
| 820 freeEphemeralFunction(db, p->pFunc); |
| 821 sqlite3DbFree(db, p); |
| 822 } |
| 748 static void freeP4(sqlite3 *db, int p4type, void *p4){ | 823 static void freeP4(sqlite3 *db, int p4type, void *p4){ |
| 749 if( p4 ){ | 824 assert( db ); |
| 750 assert( db ); | 825 switch( p4type ){ |
| 751 switch( p4type ){ | 826 case P4_FUNCCTX: { |
| 752 case P4_FUNCCTX: { | 827 freeP4FuncCtx(db, (sqlite3_context*)p4); |
| 753 freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc); | 828 break; |
| 754 /* Fall through into the next case */ | 829 } |
| 830 case P4_REAL: |
| 831 case P4_INT64: |
| 832 case P4_DYNAMIC: |
| 833 case P4_INTARRAY: { |
| 834 sqlite3DbFree(db, p4); |
| 835 break; |
| 836 } |
| 837 case P4_KEYINFO: { |
| 838 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); |
| 839 break; |
| 840 } |
| 841 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 842 case P4_EXPR: { |
| 843 sqlite3ExprDelete(db, (Expr*)p4); |
| 844 break; |
| 845 } |
| 846 #endif |
| 847 case P4_FUNCDEF: { |
| 848 freeEphemeralFunction(db, (FuncDef*)p4); |
| 849 break; |
| 850 } |
| 851 case P4_MEM: { |
| 852 if( db->pnBytesFreed==0 ){ |
| 853 sqlite3ValueFree((sqlite3_value*)p4); |
| 854 }else{ |
| 855 freeP4Mem(db, (Mem*)p4); |
| 755 } | 856 } |
| 756 case P4_REAL: | 857 break; |
| 757 case P4_INT64: | 858 } |
| 758 case P4_DYNAMIC: | 859 case P4_VTAB : { |
| 759 case P4_INTARRAY: { | 860 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); |
| 760 sqlite3DbFree(db, p4); | 861 break; |
| 761 break; | |
| 762 } | |
| 763 case P4_KEYINFO: { | |
| 764 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); | |
| 765 break; | |
| 766 } | |
| 767 #ifdef SQLITE_ENABLE_CURSOR_HINTS | |
| 768 case P4_EXPR: { | |
| 769 sqlite3ExprDelete(db, (Expr*)p4); | |
| 770 break; | |
| 771 } | |
| 772 #endif | |
| 773 case P4_MPRINTF: { | |
| 774 if( db->pnBytesFreed==0 ) sqlite3_free(p4); | |
| 775 break; | |
| 776 } | |
| 777 case P4_FUNCDEF: { | |
| 778 freeEphemeralFunction(db, (FuncDef*)p4); | |
| 779 break; | |
| 780 } | |
| 781 case P4_MEM: { | |
| 782 if( db->pnBytesFreed==0 ){ | |
| 783 sqlite3ValueFree((sqlite3_value*)p4); | |
| 784 }else{ | |
| 785 Mem *p = (Mem*)p4; | |
| 786 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); | |
| 787 sqlite3DbFree(db, p); | |
| 788 } | |
| 789 break; | |
| 790 } | |
| 791 case P4_VTAB : { | |
| 792 if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4); | |
| 793 break; | |
| 794 } | |
| 795 } | 862 } |
| 796 } | 863 } |
| 797 } | 864 } |
| 798 | 865 |
| 799 /* | 866 /* |
| 800 ** Free the space allocated for aOp and any p4 values allocated for the | 867 ** Free the space allocated for aOp and any p4 values allocated for the |
| 801 ** opcodes contained within. If aOp is not NULL it is assumed to contain | 868 ** opcodes contained within. If aOp is not NULL it is assumed to contain |
| 802 ** nOp entries. | 869 ** nOp entries. |
| 803 */ | 870 */ |
| 804 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ | 871 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){ |
| 805 if( aOp ){ | 872 if( aOp ){ |
| 806 Op *pOp; | 873 Op *pOp; |
| 807 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ | 874 for(pOp=aOp; pOp<&aOp[nOp]; pOp++){ |
| 808 freeP4(db, pOp->p4type, pOp->p4.p); | 875 if( pOp->p4type ) freeP4(db, pOp->p4type, pOp->p4.p); |
| 809 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | 876 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 810 sqlite3DbFree(db, pOp->zComment); | 877 sqlite3DbFree(db, pOp->zComment); |
| 811 #endif | 878 #endif |
| 812 } | 879 } |
| 813 } | 880 } |
| 814 sqlite3DbFree(db, aOp); | 881 sqlite3DbFree(db, aOp); |
| 815 } | 882 } |
| 816 | 883 |
| 817 /* | 884 /* |
| 818 ** Link the SubProgram object passed as the second argument into the linked | 885 ** Link the SubProgram object passed as the second argument into the linked |
| 819 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program | 886 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program |
| 820 ** objects when the VM is no longer required. | 887 ** objects when the VM is no longer required. |
| 821 */ | 888 */ |
| 822 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ | 889 void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){ |
| 823 p->pNext = pVdbe->pProgram; | 890 p->pNext = pVdbe->pProgram; |
| 824 pVdbe->pProgram = p; | 891 pVdbe->pProgram = p; |
| 825 } | 892 } |
| 826 | 893 |
| 827 /* | 894 /* |
| 828 ** Change the opcode at addr into OP_Noop | 895 ** Change the opcode at addr into OP_Noop |
| 829 */ | 896 */ |
| 830 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ | 897 int sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ |
| 831 if( addr<p->nOp ){ | 898 VdbeOp *pOp; |
| 832 VdbeOp *pOp = &p->aOp[addr]; | 899 if( p->db->mallocFailed ) return 0; |
| 833 sqlite3 *db = p->db; | 900 assert( addr>=0 && addr<p->nOp ); |
| 834 freeP4(db, pOp->p4type, pOp->p4.p); | 901 pOp = &p->aOp[addr]; |
| 835 memset(pOp, 0, sizeof(pOp[0])); | 902 freeP4(p->db, pOp->p4type, pOp->p4.p); |
| 836 pOp->opcode = OP_Noop; | 903 pOp->p4type = P4_NOTUSED; |
| 837 } | 904 pOp->p4.z = 0; |
| 905 pOp->opcode = OP_Noop; |
| 906 return 1; |
| 838 } | 907 } |
| 839 | 908 |
| 840 /* | 909 /* |
| 841 ** If the last opcode is "op" and it is not a jump destination, | 910 ** If the last opcode is "op" and it is not a jump destination, |
| 842 ** then remove it. Return true if and only if an opcode was removed. | 911 ** then remove it. Return true if and only if an opcode was removed. |
| 843 */ | 912 */ |
| 844 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ | 913 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
| 845 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ | 914 if( p->nOp>0 && p->aOp[p->nOp-1].opcode==op ){ |
| 846 sqlite3VdbeChangeToNoop(p, p->nOp-1); | 915 return sqlite3VdbeChangeToNoop(p, p->nOp-1); |
| 847 return 1; | |
| 848 }else{ | 916 }else{ |
| 849 return 0; | 917 return 0; |
| 850 } | 918 } |
| 851 } | 919 } |
| 852 | 920 |
| 853 /* | 921 /* |
| 854 ** Change the value of the P4 operand for a specific instruction. | 922 ** Change the value of the P4 operand for a specific instruction. |
| 855 ** This routine is useful when a large program is loaded from a | 923 ** This routine is useful when a large program is loaded from a |
| 856 ** static array using sqlite3VdbeAddOpList but we want to make a | 924 ** static array using sqlite3VdbeAddOpList but we want to make a |
| 857 ** few minor changes to the program. | 925 ** few minor changes to the program. |
| 858 ** | 926 ** |
| 859 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of | 927 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of |
| 860 ** the string is made into memory obtained from sqlite3_malloc(). | 928 ** the string is made into memory obtained from sqlite3_malloc(). |
| 861 ** A value of n==0 means copy bytes of zP4 up to and including the | 929 ** A value of n==0 means copy bytes of zP4 up to and including the |
| 862 ** first null byte. If n>0 then copy n+1 bytes of zP4. | 930 ** first null byte. If n>0 then copy n+1 bytes of zP4. |
| 863 ** | 931 ** |
| 864 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points | 932 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points |
| 865 ** to a string or structure that is guaranteed to exist for the lifetime of | 933 ** to a string or structure that is guaranteed to exist for the lifetime of |
| 866 ** the Vdbe. In these cases we can just copy the pointer. | 934 ** the Vdbe. In these cases we can just copy the pointer. |
| 867 ** | 935 ** |
| 868 ** If addr<0 then change P4 on the most recently inserted instruction. | 936 ** If addr<0 then change P4 on the most recently inserted instruction. |
| 869 */ | 937 */ |
| 938 static void SQLITE_NOINLINE vdbeChangeP4Full( |
| 939 Vdbe *p, |
| 940 Op *pOp, |
| 941 const char *zP4, |
| 942 int n |
| 943 ){ |
| 944 if( pOp->p4type ){ |
| 945 freeP4(p->db, pOp->p4type, pOp->p4.p); |
| 946 pOp->p4type = 0; |
| 947 pOp->p4.p = 0; |
| 948 } |
| 949 if( n<0 ){ |
| 950 sqlite3VdbeChangeP4(p, (int)(pOp - p->aOp), zP4, n); |
| 951 }else{ |
| 952 if( n==0 ) n = sqlite3Strlen30(zP4); |
| 953 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); |
| 954 pOp->p4type = P4_DYNAMIC; |
| 955 } |
| 956 } |
| 870 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ | 957 void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){ |
| 871 Op *pOp; | 958 Op *pOp; |
| 872 sqlite3 *db; | 959 sqlite3 *db; |
| 873 assert( p!=0 ); | 960 assert( p!=0 ); |
| 874 db = p->db; | 961 db = p->db; |
| 875 assert( p->magic==VDBE_MAGIC_INIT ); | 962 assert( p->magic==VDBE_MAGIC_INIT ); |
| 876 if( p->aOp==0 || db->mallocFailed ){ | 963 assert( p->aOp!=0 || db->mallocFailed ); |
| 877 if( n!=P4_VTAB ){ | 964 if( db->mallocFailed ){ |
| 878 freeP4(db, n, (void*)*(char**)&zP4); | 965 if( n!=P4_VTAB ) freeP4(db, n, (void*)*(char**)&zP4); |
| 879 } | |
| 880 return; | 966 return; |
| 881 } | 967 } |
| 882 assert( p->nOp>0 ); | 968 assert( p->nOp>0 ); |
| 883 assert( addr<p->nOp ); | 969 assert( addr<p->nOp ); |
| 884 if( addr<0 ){ | 970 if( addr<0 ){ |
| 885 addr = p->nOp - 1; | 971 addr = p->nOp - 1; |
| 886 } | 972 } |
| 887 pOp = &p->aOp[addr]; | 973 pOp = &p->aOp[addr]; |
| 888 assert( pOp->p4type==P4_NOTUSED | 974 if( n>=0 || pOp->p4type ){ |
| 889 || pOp->p4type==P4_INT32 | 975 vdbeChangeP4Full(p, pOp, zP4, n); |
| 890 || pOp->p4type==P4_KEYINFO ); | 976 return; |
| 891 freeP4(db, pOp->p4type, pOp->p4.p); | 977 } |
| 892 pOp->p4.p = 0; | |
| 893 if( n==P4_INT32 ){ | 978 if( n==P4_INT32 ){ |
| 894 /* Note: this cast is safe, because the origin data point was an int | 979 /* Note: this cast is safe, because the origin data point was an int |
| 895 ** that was cast to a (const char *). */ | 980 ** that was cast to a (const char *). */ |
| 896 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); | 981 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); |
| 897 pOp->p4type = P4_INT32; | 982 pOp->p4type = P4_INT32; |
| 898 }else if( zP4==0 ){ | 983 }else if( zP4!=0 ){ |
| 899 pOp->p4.p = 0; | 984 assert( n<0 ); |
| 900 pOp->p4type = P4_NOTUSED; | |
| 901 }else if( n==P4_KEYINFO ){ | |
| 902 pOp->p4.p = (void*)zP4; | |
| 903 pOp->p4type = P4_KEYINFO; | |
| 904 #ifdef SQLITE_ENABLE_CURSOR_HINTS | |
| 905 }else if( n==P4_EXPR ){ | |
| 906 /* Responsibility for deleting the Expr tree is handed over to the | |
| 907 ** VDBE by this operation. The caller should have already invoked | |
| 908 ** sqlite3ExprDup() or whatever other routine is needed to make a | |
| 909 ** private copy of the tree. */ | |
| 910 pOp->p4.pExpr = (Expr*)zP4; | |
| 911 pOp->p4type = P4_EXPR; | |
| 912 #endif | |
| 913 }else if( n==P4_VTAB ){ | |
| 914 pOp->p4.p = (void*)zP4; | |
| 915 pOp->p4type = P4_VTAB; | |
| 916 sqlite3VtabLock((VTable *)zP4); | |
| 917 assert( ((VTable *)zP4)->db==p->db ); | |
| 918 }else if( n<0 ){ | |
| 919 pOp->p4.p = (void*)zP4; | 985 pOp->p4.p = (void*)zP4; |
| 920 pOp->p4type = (signed char)n; | 986 pOp->p4type = (signed char)n; |
| 921 }else{ | 987 if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4); |
| 922 if( n==0 ) n = sqlite3Strlen30(zP4); | |
| 923 pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n); | |
| 924 pOp->p4type = P4_DYNAMIC; | |
| 925 } | 988 } |
| 926 } | 989 } |
| 927 | 990 |
| 991 /* |
| 992 ** Change the P4 operand of the most recently coded instruction |
| 993 ** to the value defined by the arguments. This is a high-speed |
| 994 ** version of sqlite3VdbeChangeP4(). |
| 995 ** |
| 996 ** The P4 operand must not have been previously defined. And the new |
| 997 ** P4 must not be P4_INT32. Use sqlite3VdbeChangeP4() in either of |
| 998 ** those cases. |
| 999 */ |
| 1000 void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){ |
| 1001 VdbeOp *pOp; |
| 1002 assert( n!=P4_INT32 && n!=P4_VTAB ); |
| 1003 assert( n<=0 ); |
| 1004 if( p->db->mallocFailed ){ |
| 1005 freeP4(p->db, n, pP4); |
| 1006 }else{ |
| 1007 assert( pP4!=0 ); |
| 1008 assert( p->nOp>0 ); |
| 1009 pOp = &p->aOp[p->nOp-1]; |
| 1010 assert( pOp->p4type==P4_NOTUSED ); |
| 1011 pOp->p4type = n; |
| 1012 pOp->p4.p = pP4; |
| 1013 } |
| 1014 } |
| 1015 |
| 928 /* | 1016 /* |
| 929 ** Set the P4 on the most recently added opcode to the KeyInfo for the | 1017 ** Set the P4 on the most recently added opcode to the KeyInfo for the |
| 930 ** index given. | 1018 ** index given. |
| 931 */ | 1019 */ |
| 932 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ | 1020 void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){ |
| 933 Vdbe *v = pParse->pVdbe; | 1021 Vdbe *v = pParse->pVdbe; |
| 1022 KeyInfo *pKeyInfo; |
| 934 assert( v!=0 ); | 1023 assert( v!=0 ); |
| 935 assert( pIdx!=0 ); | 1024 assert( pIdx!=0 ); |
| 936 sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx), | 1025 pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx); |
| 937 P4_KEYINFO); | 1026 if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO); |
| 938 } | 1027 } |
| 939 | 1028 |
| 940 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | 1029 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 941 /* | 1030 /* |
| 942 ** Change the comment on the most recently coded instruction. Or | 1031 ** Change the comment on the most recently coded instruction. Or |
| 943 ** insert a No-op and add the comment to that new instruction. This | 1032 ** insert a No-op and add the comment to that new instruction. This |
| 944 ** makes the code easier to read during debugging. None of this happens | 1033 ** makes the code easier to read during debugging. None of this happens |
| 945 ** in a production build. | 1034 ** in a production build. |
| 946 */ | 1035 */ |
| 947 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ | 1036 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){ |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 static int displayComment( | 1128 static int displayComment( |
| 1040 const Op *pOp, /* The opcode to be commented */ | 1129 const Op *pOp, /* The opcode to be commented */ |
| 1041 const char *zP4, /* Previously obtained value for P4 */ | 1130 const char *zP4, /* Previously obtained value for P4 */ |
| 1042 char *zTemp, /* Write result here */ | 1131 char *zTemp, /* Write result here */ |
| 1043 int nTemp /* Space available in zTemp[] */ | 1132 int nTemp /* Space available in zTemp[] */ |
| 1044 ){ | 1133 ){ |
| 1045 const char *zOpName; | 1134 const char *zOpName; |
| 1046 const char *zSynopsis; | 1135 const char *zSynopsis; |
| 1047 int nOpName; | 1136 int nOpName; |
| 1048 int ii, jj; | 1137 int ii, jj; |
| 1138 char zAlt[50]; |
| 1049 zOpName = sqlite3OpcodeName(pOp->opcode); | 1139 zOpName = sqlite3OpcodeName(pOp->opcode); |
| 1050 nOpName = sqlite3Strlen30(zOpName); | 1140 nOpName = sqlite3Strlen30(zOpName); |
| 1051 if( zOpName[nOpName+1] ){ | 1141 if( zOpName[nOpName+1] ){ |
| 1052 int seenCom = 0; | 1142 int seenCom = 0; |
| 1053 char c; | 1143 char c; |
| 1054 zSynopsis = zOpName += nOpName + 1; | 1144 zSynopsis = zOpName += nOpName + 1; |
| 1145 if( strncmp(zSynopsis,"IF ",3)==0 ){ |
| 1146 if( pOp->p5 & SQLITE_STOREP2 ){ |
| 1147 sqlite3_snprintf(sizeof(zAlt), zAlt, "r[P2] = (%s)", zSynopsis+3); |
| 1148 }else{ |
| 1149 sqlite3_snprintf(sizeof(zAlt), zAlt, "if %s goto P2", zSynopsis+3); |
| 1150 } |
| 1151 zSynopsis = zAlt; |
| 1152 } |
| 1055 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ | 1153 for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){ |
| 1056 if( c=='P' ){ | 1154 if( c=='P' ){ |
| 1057 c = zSynopsis[++ii]; | 1155 c = zSynopsis[++ii]; |
| 1058 if( c=='4' ){ | 1156 if( c=='4' ){ |
| 1059 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); | 1157 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4); |
| 1060 }else if( c=='X' ){ | 1158 }else if( c=='X' ){ |
| 1061 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); | 1159 sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment); |
| 1062 seenCom = 1; | 1160 seenCom = 1; |
| 1063 }else{ | 1161 }else{ |
| 1064 int v1 = translateP(c, pOp); | 1162 int v1 = translateP(c, pOp); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1098 } | 1196 } |
| 1099 return jj; | 1197 return jj; |
| 1100 } | 1198 } |
| 1101 #endif /* SQLITE_DEBUG */ | 1199 #endif /* SQLITE_DEBUG */ |
| 1102 | 1200 |
| 1103 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) | 1201 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) |
| 1104 /* | 1202 /* |
| 1105 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text | 1203 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text |
| 1106 ** that can be displayed in the P4 column of EXPLAIN output. | 1204 ** that can be displayed in the P4 column of EXPLAIN output. |
| 1107 */ | 1205 */ |
| 1108 static int displayP4Expr(int nTemp, char *zTemp, Expr *pExpr){ | 1206 static void displayP4Expr(StrAccum *p, Expr *pExpr){ |
| 1109 const char *zOp = 0; | 1207 const char *zOp = 0; |
| 1110 int n; | |
| 1111 switch( pExpr->op ){ | 1208 switch( pExpr->op ){ |
| 1112 case TK_STRING: | 1209 case TK_STRING: |
| 1113 sqlite3_snprintf(nTemp, zTemp, "%Q", pExpr->u.zToken); | 1210 sqlite3XPrintf(p, "%Q", pExpr->u.zToken); |
| 1114 break; | 1211 break; |
| 1115 case TK_INTEGER: | 1212 case TK_INTEGER: |
| 1116 sqlite3_snprintf(nTemp, zTemp, "%d", pExpr->u.iValue); | 1213 sqlite3XPrintf(p, "%d", pExpr->u.iValue); |
| 1117 break; | 1214 break; |
| 1118 case TK_NULL: | 1215 case TK_NULL: |
| 1119 sqlite3_snprintf(nTemp, zTemp, "NULL"); | 1216 sqlite3XPrintf(p, "NULL"); |
| 1120 break; | 1217 break; |
| 1121 case TK_REGISTER: { | 1218 case TK_REGISTER: { |
| 1122 sqlite3_snprintf(nTemp, zTemp, "r[%d]", pExpr->iTable); | 1219 sqlite3XPrintf(p, "r[%d]", pExpr->iTable); |
| 1123 break; | 1220 break; |
| 1124 } | 1221 } |
| 1125 case TK_COLUMN: { | 1222 case TK_COLUMN: { |
| 1126 if( pExpr->iColumn<0 ){ | 1223 if( pExpr->iColumn<0 ){ |
| 1127 sqlite3_snprintf(nTemp, zTemp, "rowid"); | 1224 sqlite3XPrintf(p, "rowid"); |
| 1128 }else{ | 1225 }else{ |
| 1129 sqlite3_snprintf(nTemp, zTemp, "c%d", (int)pExpr->iColumn); | 1226 sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn); |
| 1130 } | 1227 } |
| 1131 break; | 1228 break; |
| 1132 } | 1229 } |
| 1133 case TK_LT: zOp = "LT"; break; | 1230 case TK_LT: zOp = "LT"; break; |
| 1134 case TK_LE: zOp = "LE"; break; | 1231 case TK_LE: zOp = "LE"; break; |
| 1135 case TK_GT: zOp = "GT"; break; | 1232 case TK_GT: zOp = "GT"; break; |
| 1136 case TK_GE: zOp = "GE"; break; | 1233 case TK_GE: zOp = "GE"; break; |
| 1137 case TK_NE: zOp = "NE"; break; | 1234 case TK_NE: zOp = "NE"; break; |
| 1138 case TK_EQ: zOp = "EQ"; break; | 1235 case TK_EQ: zOp = "EQ"; break; |
| 1139 case TK_IS: zOp = "IS"; break; | 1236 case TK_IS: zOp = "IS"; break; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1151 case TK_RSHIFT: zOp = "RSHIFT"; break; | 1248 case TK_RSHIFT: zOp = "RSHIFT"; break; |
| 1152 case TK_CONCAT: zOp = "CONCAT"; break; | 1249 case TK_CONCAT: zOp = "CONCAT"; break; |
| 1153 case TK_UMINUS: zOp = "MINUS"; break; | 1250 case TK_UMINUS: zOp = "MINUS"; break; |
| 1154 case TK_UPLUS: zOp = "PLUS"; break; | 1251 case TK_UPLUS: zOp = "PLUS"; break; |
| 1155 case TK_BITNOT: zOp = "BITNOT"; break; | 1252 case TK_BITNOT: zOp = "BITNOT"; break; |
| 1156 case TK_NOT: zOp = "NOT"; break; | 1253 case TK_NOT: zOp = "NOT"; break; |
| 1157 case TK_ISNULL: zOp = "ISNULL"; break; | 1254 case TK_ISNULL: zOp = "ISNULL"; break; |
| 1158 case TK_NOTNULL: zOp = "NOTNULL"; break; | 1255 case TK_NOTNULL: zOp = "NOTNULL"; break; |
| 1159 | 1256 |
| 1160 default: | 1257 default: |
| 1161 sqlite3_snprintf(nTemp, zTemp, "%s", "expr"); | 1258 sqlite3XPrintf(p, "%s", "expr"); |
| 1162 break; | 1259 break; |
| 1163 } | 1260 } |
| 1164 | 1261 |
| 1165 if( zOp ){ | 1262 if( zOp ){ |
| 1166 sqlite3_snprintf(nTemp, zTemp, "%s(", zOp); | 1263 sqlite3XPrintf(p, "%s(", zOp); |
| 1167 n = sqlite3Strlen30(zTemp); | 1264 displayP4Expr(p, pExpr->pLeft); |
| 1168 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pLeft); | 1265 if( pExpr->pRight ){ |
| 1169 if( n<nTemp-1 && pExpr->pRight ){ | 1266 sqlite3StrAccumAppend(p, ",", 1); |
| 1170 zTemp[n++] = ','; | 1267 displayP4Expr(p, pExpr->pRight); |
| 1171 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pRight); | |
| 1172 } | 1268 } |
| 1173 sqlite3_snprintf(nTemp-n, zTemp+n, ")"); | 1269 sqlite3StrAccumAppend(p, ")", 1); |
| 1174 } | 1270 } |
| 1175 return sqlite3Strlen30(zTemp); | |
| 1176 } | 1271 } |
| 1177 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ | 1272 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ |
| 1178 | 1273 |
| 1179 | 1274 |
| 1180 #if VDBE_DISPLAY_P4 | 1275 #if VDBE_DISPLAY_P4 |
| 1181 /* | 1276 /* |
| 1182 ** Compute a string that describes the P4 parameter for an opcode. | 1277 ** Compute a string that describes the P4 parameter for an opcode. |
| 1183 ** Use zTemp for any required temporary buffer space. | 1278 ** Use zTemp for any required temporary buffer space. |
| 1184 */ | 1279 */ |
| 1185 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ | 1280 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 1186 char *zP4 = zTemp; | 1281 char *zP4 = zTemp; |
| 1282 StrAccum x; |
| 1187 assert( nTemp>=20 ); | 1283 assert( nTemp>=20 ); |
| 1284 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0); |
| 1188 switch( pOp->p4type ){ | 1285 switch( pOp->p4type ){ |
| 1189 case P4_KEYINFO: { | 1286 case P4_KEYINFO: { |
| 1190 int i, j; | 1287 int j; |
| 1191 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; | 1288 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 1192 assert( pKeyInfo->aSortOrder!=0 ); | 1289 assert( pKeyInfo->aSortOrder!=0 ); |
| 1193 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); | 1290 sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField); |
| 1194 i = sqlite3Strlen30(zTemp); | |
| 1195 for(j=0; j<pKeyInfo->nField; j++){ | 1291 for(j=0; j<pKeyInfo->nField; j++){ |
| 1196 CollSeq *pColl = pKeyInfo->aColl[j]; | 1292 CollSeq *pColl = pKeyInfo->aColl[j]; |
| 1197 const char *zColl = pColl ? pColl->zName : "nil"; | 1293 const char *zColl = pColl ? pColl->zName : ""; |
| 1198 int n = sqlite3Strlen30(zColl); | 1294 if( strcmp(zColl, "BINARY")==0 ) zColl = "B"; |
| 1199 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ | 1295 sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl); |
| 1200 zColl = "B"; | |
| 1201 n = 1; | |
| 1202 } | |
| 1203 if( i+n>nTemp-7 ){ | |
| 1204 memcpy(&zTemp[i],",...",4); | |
| 1205 i += 4; | |
| 1206 break; | |
| 1207 } | |
| 1208 zTemp[i++] = ','; | |
| 1209 if( pKeyInfo->aSortOrder[j] ){ | |
| 1210 zTemp[i++] = '-'; | |
| 1211 } | |
| 1212 memcpy(&zTemp[i], zColl, n+1); | |
| 1213 i += n; | |
| 1214 } | 1296 } |
| 1215 zTemp[i++] = ')'; | 1297 sqlite3StrAccumAppend(&x, ")", 1); |
| 1216 zTemp[i] = 0; | |
| 1217 assert( i<nTemp ); | |
| 1218 break; | 1298 break; |
| 1219 } | 1299 } |
| 1220 #ifdef SQLITE_ENABLE_CURSOR_HINTS | 1300 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 1221 case P4_EXPR: { | 1301 case P4_EXPR: { |
| 1222 displayP4Expr(nTemp, zTemp, pOp->p4.pExpr); | 1302 displayP4Expr(&x, pOp->p4.pExpr); |
| 1223 break; | 1303 break; |
| 1224 } | 1304 } |
| 1225 #endif | 1305 #endif |
| 1226 case P4_COLLSEQ: { | 1306 case P4_COLLSEQ: { |
| 1227 CollSeq *pColl = pOp->p4.pColl; | 1307 CollSeq *pColl = pOp->p4.pColl; |
| 1228 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); | 1308 sqlite3XPrintf(&x, "(%.20s)", pColl->zName); |
| 1229 break; | 1309 break; |
| 1230 } | 1310 } |
| 1231 case P4_FUNCDEF: { | 1311 case P4_FUNCDEF: { |
| 1232 FuncDef *pDef = pOp->p4.pFunc; | 1312 FuncDef *pDef = pOp->p4.pFunc; |
| 1233 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | 1313 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); |
| 1234 break; | 1314 break; |
| 1235 } | 1315 } |
| 1236 #ifdef SQLITE_DEBUG | 1316 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 1237 case P4_FUNCCTX: { | 1317 case P4_FUNCCTX: { |
| 1238 FuncDef *pDef = pOp->p4.pCtx->pFunc; | 1318 FuncDef *pDef = pOp->p4.pCtx->pFunc; |
| 1239 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | 1319 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg); |
| 1240 break; | 1320 break; |
| 1241 } | 1321 } |
| 1242 #endif | 1322 #endif |
| 1243 case P4_INT64: { | 1323 case P4_INT64: { |
| 1244 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); | 1324 sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64); |
| 1245 break; | 1325 break; |
| 1246 } | 1326 } |
| 1247 case P4_INT32: { | 1327 case P4_INT32: { |
| 1248 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); | 1328 sqlite3XPrintf(&x, "%d", pOp->p4.i); |
| 1249 break; | 1329 break; |
| 1250 } | 1330 } |
| 1251 case P4_REAL: { | 1331 case P4_REAL: { |
| 1252 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); | 1332 sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal); |
| 1253 break; | 1333 break; |
| 1254 } | 1334 } |
| 1255 case P4_MEM: { | 1335 case P4_MEM: { |
| 1256 Mem *pMem = pOp->p4.pMem; | 1336 Mem *pMem = pOp->p4.pMem; |
| 1257 if( pMem->flags & MEM_Str ){ | 1337 if( pMem->flags & MEM_Str ){ |
| 1258 zP4 = pMem->z; | 1338 zP4 = pMem->z; |
| 1259 }else if( pMem->flags & MEM_Int ){ | 1339 }else if( pMem->flags & MEM_Int ){ |
| 1260 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i); | 1340 sqlite3XPrintf(&x, "%lld", pMem->u.i); |
| 1261 }else if( pMem->flags & MEM_Real ){ | 1341 }else if( pMem->flags & MEM_Real ){ |
| 1262 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r); | 1342 sqlite3XPrintf(&x, "%.16g", pMem->u.r); |
| 1263 }else if( pMem->flags & MEM_Null ){ | 1343 }else if( pMem->flags & MEM_Null ){ |
| 1264 sqlite3_snprintf(nTemp, zTemp, "NULL"); | 1344 zP4 = "NULL"; |
| 1265 }else{ | 1345 }else{ |
| 1266 assert( pMem->flags & MEM_Blob ); | 1346 assert( pMem->flags & MEM_Blob ); |
| 1267 zP4 = "(blob)"; | 1347 zP4 = "(blob)"; |
| 1268 } | 1348 } |
| 1269 break; | 1349 break; |
| 1270 } | 1350 } |
| 1271 #ifndef SQLITE_OMIT_VIRTUALTABLE | 1351 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1272 case P4_VTAB: { | 1352 case P4_VTAB: { |
| 1273 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; | 1353 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
| 1274 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab); | 1354 sqlite3XPrintf(&x, "vtab:%p", pVtab); |
| 1275 break; | 1355 break; |
| 1276 } | 1356 } |
| 1277 #endif | 1357 #endif |
| 1278 case P4_INTARRAY: { | 1358 case P4_INTARRAY: { |
| 1279 sqlite3_snprintf(nTemp, zTemp, "intarray"); | 1359 int i; |
| 1360 int *ai = pOp->p4.ai; |
| 1361 int n = ai[0]; /* The first element of an INTARRAY is always the |
| 1362 ** count of the number of elements to follow */ |
| 1363 for(i=1; i<n; i++){ |
| 1364 sqlite3XPrintf(&x, ",%d", ai[i]); |
| 1365 } |
| 1366 zTemp[0] = '['; |
| 1367 sqlite3StrAccumAppend(&x, "]", 1); |
| 1280 break; | 1368 break; |
| 1281 } | 1369 } |
| 1282 case P4_SUBPROGRAM: { | 1370 case P4_SUBPROGRAM: { |
| 1283 sqlite3_snprintf(nTemp, zTemp, "program"); | 1371 sqlite3XPrintf(&x, "program"); |
| 1284 break; | 1372 break; |
| 1285 } | 1373 } |
| 1286 case P4_ADVANCE: { | 1374 case P4_ADVANCE: { |
| 1287 zTemp[0] = 0; | 1375 zTemp[0] = 0; |
| 1288 break; | 1376 break; |
| 1289 } | 1377 } |
| 1378 case P4_TABLE: { |
| 1379 sqlite3XPrintf(&x, "%s", pOp->p4.pTab->zName); |
| 1380 break; |
| 1381 } |
| 1290 default: { | 1382 default: { |
| 1291 zP4 = pOp->p4.z; | 1383 zP4 = pOp->p4.z; |
| 1292 if( zP4==0 ){ | 1384 if( zP4==0 ){ |
| 1293 zP4 = zTemp; | 1385 zP4 = zTemp; |
| 1294 zTemp[0] = 0; | 1386 zTemp[0] = 0; |
| 1295 } | 1387 } |
| 1296 } | 1388 } |
| 1297 } | 1389 } |
| 1390 sqlite3StrAccumFinish(&x); |
| 1298 assert( zP4!=0 ); | 1391 assert( zP4!=0 ); |
| 1299 return zP4; | 1392 return zP4; |
| 1300 } | 1393 } |
| 1301 #endif /* VDBE_DISPLAY_P4 */ | 1394 #endif /* VDBE_DISPLAY_P4 */ |
| 1302 | 1395 |
| 1303 /* | 1396 /* |
| 1304 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. | 1397 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
| 1305 ** | 1398 ** |
| 1306 ** The prepared statements need to know in advance the complete set of | 1399 ** The prepared statements need to know in advance the complete set of |
| 1307 ** attached databases that will be use. A mask of these databases | 1400 ** attached databases that will be use. A mask of these databases |
| 1308 ** is maintained in p->btreeMask. The p->lockMask value is the subset of | 1401 ** is maintained in p->btreeMask. The p->lockMask value is the subset of |
| 1309 ** p->btreeMask of databases that will require a lock. | 1402 ** p->btreeMask of databases that will require a lock. |
| 1310 */ | 1403 */ |
| 1311 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ | 1404 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
| 1312 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); | 1405 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 ); |
| 1313 assert( i<(int)sizeof(p->btreeMask)*8 ); | 1406 assert( i<(int)sizeof(p->btreeMask)*8 ); |
| 1314 DbMaskSet(p->btreeMask, i); | 1407 DbMaskSet(p->btreeMask, i); |
| 1315 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ | 1408 if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){ |
| 1316 DbMaskSet(p->lockMask, i); | 1409 DbMaskSet(p->lockMask, i); |
| 1317 } | 1410 } |
| 1318 } | 1411 } |
| 1319 | 1412 |
| 1320 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | 1413 #if !defined(SQLITE_OMIT_SHARED_CACHE) |
| 1321 /* | 1414 /* |
| 1322 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, | 1415 ** If SQLite is compiled to support shared-cache mode and to be threadsafe, |
| 1323 ** this routine obtains the mutex associated with each BtShared structure | 1416 ** this routine obtains the mutex associated with each BtShared structure |
| 1324 ** that may be accessed by the VM passed as an argument. In doing so it also | 1417 ** that may be accessed by the VM passed as an argument. In doing so it also |
| 1325 ** sets the BtShared.db member of each of the BtShared structures, ensuring | 1418 ** sets the BtShared.db member of each of the BtShared structures, ensuring |
| 1326 ** that the correct busy-handler callback is invoked if required. | 1419 ** that the correct busy-handler callback is invoked if required. |
| 1327 ** | 1420 ** |
| 1328 ** If SQLite is not threadsafe but does support shared-cache mode, then | 1421 ** If SQLite is not threadsafe but does support shared-cache mode, then |
| 1329 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables | 1422 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables |
| 1330 ** of all of BtShared structures accessible via the database handle | 1423 ** of all of BtShared structures accessible via the database handle |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1401 ** information from the vdbe.c source text */ | 1494 ** information from the vdbe.c source text */ |
| 1402 fprintf(pOut, zFormat1, pc, | 1495 fprintf(pOut, zFormat1, pc, |
| 1403 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, | 1496 sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5, |
| 1404 zCom | 1497 zCom |
| 1405 ); | 1498 ); |
| 1406 fflush(pOut); | 1499 fflush(pOut); |
| 1407 } | 1500 } |
| 1408 #endif | 1501 #endif |
| 1409 | 1502 |
| 1410 /* | 1503 /* |
| 1504 ** Initialize an array of N Mem element. |
| 1505 */ |
| 1506 static void initMemArray(Mem *p, int N, sqlite3 *db, u16 flags){ |
| 1507 while( (N--)>0 ){ |
| 1508 p->db = db; |
| 1509 p->flags = flags; |
| 1510 p->szMalloc = 0; |
| 1511 #ifdef SQLITE_DEBUG |
| 1512 p->pScopyFrom = 0; |
| 1513 #endif |
| 1514 p++; |
| 1515 } |
| 1516 } |
| 1517 |
| 1518 /* |
| 1411 ** Release an array of N Mem elements | 1519 ** Release an array of N Mem elements |
| 1412 */ | 1520 */ |
| 1413 static void releaseMemArray(Mem *p, int N){ | 1521 static void releaseMemArray(Mem *p, int N){ |
| 1414 if( p && N ){ | 1522 if( p && N ){ |
| 1415 Mem *pEnd = &p[N]; | 1523 Mem *pEnd = &p[N]; |
| 1416 sqlite3 *db = p->db; | 1524 sqlite3 *db = p->db; |
| 1417 u8 malloc_failed = db->mallocFailed; | |
| 1418 if( db->pnBytesFreed ){ | 1525 if( db->pnBytesFreed ){ |
| 1419 do{ | 1526 do{ |
| 1420 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); | 1527 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc); |
| 1421 }while( (++p)<pEnd ); | 1528 }while( (++p)<pEnd ); |
| 1422 return; | 1529 return; |
| 1423 } | 1530 } |
| 1424 do{ | 1531 do{ |
| 1425 assert( (&p[1])==pEnd || p[0].db==p[1].db ); | 1532 assert( (&p[1])==pEnd || p[0].db==p[1].db ); |
| 1426 assert( sqlite3VdbeCheckMemInvariants(p) ); | 1533 assert( sqlite3VdbeCheckMemInvariants(p) ); |
| 1427 | 1534 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1443 testcase( p->flags & MEM_RowSet ); | 1550 testcase( p->flags & MEM_RowSet ); |
| 1444 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ | 1551 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){ |
| 1445 sqlite3VdbeMemRelease(p); | 1552 sqlite3VdbeMemRelease(p); |
| 1446 }else if( p->szMalloc ){ | 1553 }else if( p->szMalloc ){ |
| 1447 sqlite3DbFree(db, p->zMalloc); | 1554 sqlite3DbFree(db, p->zMalloc); |
| 1448 p->szMalloc = 0; | 1555 p->szMalloc = 0; |
| 1449 } | 1556 } |
| 1450 | 1557 |
| 1451 p->flags = MEM_Undefined; | 1558 p->flags = MEM_Undefined; |
| 1452 }while( (++p)<pEnd ); | 1559 }while( (++p)<pEnd ); |
| 1453 db->mallocFailed = malloc_failed; | |
| 1454 } | 1560 } |
| 1455 } | 1561 } |
| 1456 | 1562 |
| 1457 /* | 1563 /* |
| 1458 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are | 1564 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are |
| 1459 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). | 1565 ** allocated by the OP_Program opcode in sqlite3VdbeExec(). |
| 1460 */ | 1566 */ |
| 1461 void sqlite3VdbeFrameDelete(VdbeFrame *p){ | 1567 void sqlite3VdbeFrameDelete(VdbeFrame *p){ |
| 1462 int i; | 1568 int i; |
| 1463 Mem *aMem = VdbeFrameMem(p); | 1569 Mem *aMem = VdbeFrameMem(p); |
| 1464 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; | 1570 VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem]; |
| 1465 for(i=0; i<p->nChildCsr; i++){ | 1571 for(i=0; i<p->nChildCsr; i++){ |
| 1466 sqlite3VdbeFreeCursor(p->v, apCsr[i]); | 1572 sqlite3VdbeFreeCursor(p->v, apCsr[i]); |
| 1467 } | 1573 } |
| 1468 releaseMemArray(aMem, p->nChildMem); | 1574 releaseMemArray(aMem, p->nChildMem); |
| 1575 sqlite3VdbeDeleteAuxData(p->v->db, &p->pAuxData, -1, 0); |
| 1469 sqlite3DbFree(p->v->db, p); | 1576 sqlite3DbFree(p->v->db, p); |
| 1470 } | 1577 } |
| 1471 | 1578 |
| 1472 #ifndef SQLITE_OMIT_EXPLAIN | 1579 #ifndef SQLITE_OMIT_EXPLAIN |
| 1473 /* | 1580 /* |
| 1474 ** Give a listing of the program in the virtual machine. | 1581 ** Give a listing of the program in the virtual machine. |
| 1475 ** | 1582 ** |
| 1476 ** The interface is the same as sqlite3VdbeExec(). But instead of | 1583 ** The interface is the same as sqlite3VdbeExec(). But instead of |
| 1477 ** running the code, it invokes the callback once for each instruction. | 1584 ** running the code, it invokes the callback once for each instruction. |
| 1478 ** This feature is used to implement "EXPLAIN". | 1585 ** This feature is used to implement "EXPLAIN". |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1501 assert( p->magic==VDBE_MAGIC_RUN ); | 1608 assert( p->magic==VDBE_MAGIC_RUN ); |
| 1502 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); | 1609 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM ); |
| 1503 | 1610 |
| 1504 /* Even though this opcode does not use dynamic strings for | 1611 /* Even though this opcode does not use dynamic strings for |
| 1505 ** the result, result columns may become dynamic if the user calls | 1612 ** the result, result columns may become dynamic if the user calls |
| 1506 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. | 1613 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding. |
| 1507 */ | 1614 */ |
| 1508 releaseMemArray(pMem, 8); | 1615 releaseMemArray(pMem, 8); |
| 1509 p->pResultSet = 0; | 1616 p->pResultSet = 0; |
| 1510 | 1617 |
| 1511 if( p->rc==SQLITE_NOMEM ){ | 1618 if( p->rc==SQLITE_NOMEM_BKPT ){ |
| 1512 /* This happens if a malloc() inside a call to sqlite3_column_text() or | 1619 /* This happens if a malloc() inside a call to sqlite3_column_text() or |
| 1513 ** sqlite3_column_text16() failed. */ | 1620 ** sqlite3_column_text16() failed. */ |
| 1514 db->mallocFailed = 1; | 1621 sqlite3OomFault(db); |
| 1515 return SQLITE_ERROR; | 1622 return SQLITE_ERROR; |
| 1516 } | 1623 } |
| 1517 | 1624 |
| 1518 /* When the number of output rows reaches nRow, that means the | 1625 /* When the number of output rows reaches nRow, that means the |
| 1519 ** listing has finished and sqlite3_step() should return SQLITE_DONE. | 1626 ** listing has finished and sqlite3_step() should return SQLITE_DONE. |
| 1520 ** nRow is the sum of the number of rows in the main program, plus | 1627 ** nRow is the sum of the number of rows in the main program, plus |
| 1521 ** the sum of the number of rows in all trigger subprograms encountered | 1628 ** the sum of the number of rows in all trigger subprograms encountered |
| 1522 ** so far. The nRow value will increase as new trigger subprograms are | 1629 ** so far. The nRow value will increase as new trigger subprograms are |
| 1523 ** encountered, but p->pc will eventually catch up to nRow. | 1630 ** encountered, but p->pc will eventually catch up to nRow. |
| 1524 */ | 1631 */ |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1612 pMem->u.i = pOp->p3; /* P3 */ | 1719 pMem->u.i = pOp->p3; /* P3 */ |
| 1613 pMem++; | 1720 pMem++; |
| 1614 | 1721 |
| 1615 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ | 1722 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ |
| 1616 assert( p->db->mallocFailed ); | 1723 assert( p->db->mallocFailed ); |
| 1617 return SQLITE_ERROR; | 1724 return SQLITE_ERROR; |
| 1618 } | 1725 } |
| 1619 pMem->flags = MEM_Str|MEM_Term; | 1726 pMem->flags = MEM_Str|MEM_Term; |
| 1620 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); | 1727 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); |
| 1621 if( zP4!=pMem->z ){ | 1728 if( zP4!=pMem->z ){ |
| 1729 pMem->n = 0; |
| 1622 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); | 1730 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
| 1623 }else{ | 1731 }else{ |
| 1624 assert( pMem->z!=0 ); | 1732 assert( pMem->z!=0 ); |
| 1625 pMem->n = sqlite3Strlen30(pMem->z); | 1733 pMem->n = sqlite3Strlen30(pMem->z); |
| 1626 pMem->enc = SQLITE_UTF8; | 1734 pMem->enc = SQLITE_UTF8; |
| 1627 } | 1735 } |
| 1628 pMem++; | 1736 pMem++; |
| 1629 | 1737 |
| 1630 if( p->explain==1 ){ | 1738 if( p->explain==1 ){ |
| 1631 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ | 1739 if( sqlite3VdbeMemClearAndResize(pMem, 4) ){ |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1702 }else{ | 1810 }else{ |
| 1703 z[j++] = z[i]; | 1811 z[j++] = z[i]; |
| 1704 } | 1812 } |
| 1705 } | 1813 } |
| 1706 z[j] = 0; | 1814 z[j] = 0; |
| 1707 sqlite3IoTrace("SQL %s\n", z); | 1815 sqlite3IoTrace("SQL %s\n", z); |
| 1708 } | 1816 } |
| 1709 } | 1817 } |
| 1710 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ | 1818 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */ |
| 1711 | 1819 |
| 1712 /* | 1820 /* An instance of this object describes bulk memory available for use |
| 1713 ** Allocate space from a fixed size buffer and return a pointer to | 1821 ** by subcomponents of a prepared statement. Space is allocated out |
| 1714 ** that space. If insufficient space is available, return NULL. | 1822 ** of a ReusableSpace object by the allocSpace() routine below. |
| 1823 */ |
| 1824 struct ReusableSpace { |
| 1825 u8 *pSpace; /* Available memory */ |
| 1826 int nFree; /* Bytes of available memory */ |
| 1827 int nNeeded; /* Total bytes that could not be allocated */ |
| 1828 }; |
| 1829 |
| 1830 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf |
| 1831 ** from the ReusableSpace object. Return a pointer to the allocated |
| 1832 ** memory on success. If insufficient memory is available in the |
| 1833 ** ReusableSpace object, increase the ReusableSpace.nNeeded |
| 1834 ** value by the amount needed and return NULL. |
| 1715 ** | 1835 ** |
| 1716 ** The pBuf parameter is the initial value of a pointer which will | 1836 ** If pBuf is not initially NULL, that means that the memory has already |
| 1717 ** receive the new memory. pBuf is normally NULL. If pBuf is not | 1837 ** been allocated by a prior call to this routine, so just return a copy |
| 1718 ** NULL, it means that memory space has already been allocated and that | 1838 ** of pBuf and leave ReusableSpace unchanged. |
| 1719 ** this routine should not allocate any new memory. When pBuf is not | |
| 1720 ** NULL simply return pBuf. Only allocate new memory space when pBuf | |
| 1721 ** is NULL. | |
| 1722 ** | 1839 ** |
| 1723 ** nByte is the number of bytes of space needed. | 1840 ** This allocator is employed to repurpose unused slots at the end of the |
| 1724 ** | 1841 ** opcode array of prepared state for other memory needs of the prepared |
| 1725 ** pFrom points to *pnFrom bytes of available space. New space is allocated | 1842 ** statement. |
| 1726 ** from the end of the pFrom buffer and *pnFrom is decremented. | |
| 1727 ** | |
| 1728 ** *pnNeeded is a counter of the number of bytes of space that have failed | |
| 1729 ** to allocate. If there is insufficient space in pFrom to satisfy the | |
| 1730 ** request, then increment *pnNeeded by the amount of the request. | |
| 1731 */ | 1843 */ |
| 1732 static void *allocSpace( | 1844 static void *allocSpace( |
| 1733 void *pBuf, /* Where return pointer will be stored */ | 1845 struct ReusableSpace *p, /* Bulk memory available for allocation */ |
| 1734 int nByte, /* Number of bytes to allocate */ | 1846 void *pBuf, /* Pointer to a prior allocation */ |
| 1735 u8 *pFrom, /* Memory available for allocation */ | 1847 int nByte /* Bytes of memory needed */ |
| 1736 int *pnFrom, /* IN/OUT: Space available at pFrom */ | |
| 1737 int *pnNeeded /* If allocation cannot be made, increment *pnByte */ | |
| 1738 ){ | 1848 ){ |
| 1739 assert( EIGHT_BYTE_ALIGNMENT(pFrom) ); | 1849 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) ); |
| 1740 if( pBuf==0 ){ | 1850 if( pBuf==0 ){ |
| 1741 nByte = ROUND8(nByte); | 1851 nByte = ROUND8(nByte); |
| 1742 if( nByte <= *pnFrom ){ | 1852 if( nByte <= p->nFree ){ |
| 1743 *pnFrom -= nByte; | 1853 p->nFree -= nByte; |
| 1744 pBuf = &pFrom[*pnFrom]; | 1854 pBuf = &p->pSpace[p->nFree]; |
| 1745 }else{ | 1855 }else{ |
| 1746 *pnNeeded += nByte; | 1856 p->nNeeded += nByte; |
| 1747 } | 1857 } |
| 1748 } | 1858 } |
| 1749 assert( EIGHT_BYTE_ALIGNMENT(pBuf) ); | 1859 assert( EIGHT_BYTE_ALIGNMENT(pBuf) ); |
| 1750 return pBuf; | 1860 return pBuf; |
| 1751 } | 1861 } |
| 1752 | 1862 |
| 1753 /* | 1863 /* |
| 1754 ** Rewind the VDBE back to the beginning in preparation for | 1864 ** Rewind the VDBE back to the beginning in preparation for |
| 1755 ** running it. | 1865 ** running it. |
| 1756 */ | 1866 */ |
| 1757 void sqlite3VdbeRewind(Vdbe *p){ | 1867 void sqlite3VdbeRewind(Vdbe *p){ |
| 1758 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) | 1868 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 1759 int i; | 1869 int i; |
| 1760 #endif | 1870 #endif |
| 1761 assert( p!=0 ); | 1871 assert( p!=0 ); |
| 1762 assert( p->magic==VDBE_MAGIC_INIT ); | 1872 assert( p->magic==VDBE_MAGIC_INIT || p->magic==VDBE_MAGIC_RESET ); |
| 1763 | 1873 |
| 1764 /* There should be at least one opcode. | 1874 /* There should be at least one opcode. |
| 1765 */ | 1875 */ |
| 1766 assert( p->nOp>0 ); | 1876 assert( p->nOp>0 ); |
| 1767 | 1877 |
| 1768 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ | 1878 /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */ |
| 1769 p->magic = VDBE_MAGIC_RUN; | 1879 p->magic = VDBE_MAGIC_RUN; |
| 1770 | 1880 |
| 1771 #ifdef SQLITE_DEBUG | 1881 #ifdef SQLITE_DEBUG |
| 1772 for(i=1; i<p->nMem; i++){ | 1882 for(i=0; i<p->nMem; i++){ |
| 1773 assert( p->aMem[i].db==p->db ); | 1883 assert( p->aMem[i].db==p->db ); |
| 1774 } | 1884 } |
| 1775 #endif | 1885 #endif |
| 1776 p->pc = -1; | 1886 p->pc = -1; |
| 1777 p->rc = SQLITE_OK; | 1887 p->rc = SQLITE_OK; |
| 1778 p->errorAction = OE_Abort; | 1888 p->errorAction = OE_Abort; |
| 1779 p->magic = VDBE_MAGIC_RUN; | |
| 1780 p->nChange = 0; | 1889 p->nChange = 0; |
| 1781 p->cacheCtr = 1; | 1890 p->cacheCtr = 1; |
| 1782 p->minWriteFileFormat = 255; | 1891 p->minWriteFileFormat = 255; |
| 1783 p->iStatement = 0; | 1892 p->iStatement = 0; |
| 1784 p->nFkConstraint = 0; | 1893 p->nFkConstraint = 0; |
| 1785 #ifdef VDBE_PROFILE | 1894 #ifdef VDBE_PROFILE |
| 1786 for(i=0; i<p->nOp; i++){ | 1895 for(i=0; i<p->nOp; i++){ |
| 1787 p->aOp[i].cnt = 0; | 1896 p->aOp[i].cnt = 0; |
| 1788 p->aOp[i].cycles = 0; | 1897 p->aOp[i].cycles = 0; |
| 1789 } | 1898 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1810 */ | 1919 */ |
| 1811 void sqlite3VdbeMakeReady( | 1920 void sqlite3VdbeMakeReady( |
| 1812 Vdbe *p, /* The VDBE */ | 1921 Vdbe *p, /* The VDBE */ |
| 1813 Parse *pParse /* Parsing context */ | 1922 Parse *pParse /* Parsing context */ |
| 1814 ){ | 1923 ){ |
| 1815 sqlite3 *db; /* The database connection */ | 1924 sqlite3 *db; /* The database connection */ |
| 1816 int nVar; /* Number of parameters */ | 1925 int nVar; /* Number of parameters */ |
| 1817 int nMem; /* Number of VM memory registers */ | 1926 int nMem; /* Number of VM memory registers */ |
| 1818 int nCursor; /* Number of cursors required */ | 1927 int nCursor; /* Number of cursors required */ |
| 1819 int nArg; /* Number of arguments in subprograms */ | 1928 int nArg; /* Number of arguments in subprograms */ |
| 1820 int nOnce; /* Number of OP_Once instructions */ | |
| 1821 int n; /* Loop counter */ | 1929 int n; /* Loop counter */ |
| 1822 int nFree; /* Available free space */ | 1930 struct ReusableSpace x; /* Reusable bulk memory */ |
| 1823 u8 *zCsr; /* Memory available for allocation */ | |
| 1824 int nByte; /* How much extra memory is needed */ | |
| 1825 | 1931 |
| 1826 assert( p!=0 ); | 1932 assert( p!=0 ); |
| 1827 assert( p->nOp>0 ); | 1933 assert( p->nOp>0 ); |
| 1828 assert( pParse!=0 ); | 1934 assert( pParse!=0 ); |
| 1829 assert( p->magic==VDBE_MAGIC_INIT ); | 1935 assert( p->magic==VDBE_MAGIC_INIT ); |
| 1830 assert( pParse==p->pParse ); | 1936 assert( pParse==p->pParse ); |
| 1831 db = p->db; | 1937 db = p->db; |
| 1832 assert( db->mallocFailed==0 ); | 1938 assert( db->mallocFailed==0 ); |
| 1833 nVar = pParse->nVar; | 1939 nVar = pParse->nVar; |
| 1834 nMem = pParse->nMem; | 1940 nMem = pParse->nMem; |
| 1835 nCursor = pParse->nTab; | 1941 nCursor = pParse->nTab; |
| 1836 nArg = pParse->nMaxArg; | 1942 nArg = pParse->nMaxArg; |
| 1837 nOnce = pParse->nOnce; | |
| 1838 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ | |
| 1839 | 1943 |
| 1840 /* For each cursor required, also allocate a memory cell. Memory | 1944 /* Each cursor uses a memory cell. The first cursor (cursor 0) can |
| 1841 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by | 1945 ** use aMem[0] which is not otherwise used by the VDBE program. Allocate |
| 1842 ** the vdbe program. Instead they are used to allocate space for | 1946 ** space at the end of aMem[] for cursors 1 and greater. |
| 1843 ** VdbeCursor/BtCursor structures. The blob of memory associated with | |
| 1844 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) | |
| 1845 ** stores the blob of memory associated with cursor 1, etc. | |
| 1846 ** | |
| 1847 ** See also: allocateCursor(). | 1947 ** See also: allocateCursor(). |
| 1848 */ | 1948 */ |
| 1849 nMem += nCursor; | 1949 nMem += nCursor; |
| 1950 if( nCursor==0 && nMem>0 ) nMem++; /* Space for aMem[0] even if not used */ |
| 1850 | 1951 |
| 1851 /* zCsr will initially point to nFree bytes of unused space at the | 1952 /* Figure out how much reusable memory is available at the end of the |
| 1852 ** end of the opcode array, p->aOp. The computation of nFree is | 1953 ** opcode array. This extra memory will be reallocated for other elements |
| 1853 ** conservative - it might be smaller than the true number of free | 1954 ** of the prepared statement. |
| 1854 ** bytes, but never larger. nFree must be a multiple of 8 - it is | |
| 1855 ** rounded down if is not. | |
| 1856 */ | 1955 */ |
| 1857 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode space used */ | 1956 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */ |
| 1858 zCsr = &((u8*)p->aOp)[n]; /* Unused opcode space */ | 1957 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */ |
| 1859 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | 1958 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) ); |
| 1860 nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused space */ | 1959 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */ |
| 1861 assert( nFree>=0 ); | 1960 assert( x.nFree>=0 ); |
| 1862 if( nFree>0 ){ | 1961 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) ); |
| 1863 memset(zCsr, 0, nFree); | |
| 1864 assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) ); | |
| 1865 } | |
| 1866 | 1962 |
| 1867 resolveP2Values(p, &nArg); | 1963 resolveP2Values(p, &nArg); |
| 1868 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); | 1964 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 1869 if( pParse->explain && nMem<10 ){ | 1965 if( pParse->explain && nMem<10 ){ |
| 1870 nMem = 10; | 1966 nMem = 10; |
| 1871 } | 1967 } |
| 1872 p->expired = 0; | 1968 p->expired = 0; |
| 1873 | 1969 |
| 1874 /* Memory for registers, parameters, cursor, etc, is allocated in two | 1970 /* Memory for registers, parameters, cursor, etc, is allocated in one or two |
| 1875 ** passes. On the first pass, we try to reuse unused space at the | 1971 ** passes. On the first pass, we try to reuse unused memory at the |
| 1876 ** end of the opcode array. If we are unable to satisfy all memory | 1972 ** end of the opcode array. If we are unable to satisfy all memory |
| 1877 ** requirements by reusing the opcode array tail, then the second | 1973 ** requirements by reusing the opcode array tail, then the second |
| 1878 ** pass will fill in the rest using a fresh allocation. | 1974 ** pass will fill in the remainder using a fresh memory allocation. |
| 1879 ** | 1975 ** |
| 1880 ** This two-pass approach that reuses as much memory as possible from | 1976 ** This two-pass approach that reuses as much memory as possible from |
| 1881 ** the leftover space at the end of the opcode array can significantly | 1977 ** the leftover memory at the end of the opcode array. This can significantly |
| 1882 ** reduce the amount of memory held by a prepared statement. | 1978 ** reduce the amount of memory held by a prepared statement. |
| 1883 */ | 1979 */ |
| 1884 do { | 1980 do { |
| 1885 nByte = 0; | 1981 x.nNeeded = 0; |
| 1886 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte); | 1982 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem)); |
| 1887 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte); | 1983 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem)); |
| 1888 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte); | 1984 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*)); |
| 1889 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), zCsr, &nFree, &nByte); | 1985 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*)); |
| 1890 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), | |
| 1891 zCsr, &nFree, &nByte); | |
| 1892 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte); | |
| 1893 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS | 1986 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1894 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte); | 1987 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64)); |
| 1895 #endif | 1988 #endif |
| 1896 if( nByte ){ | 1989 if( x.nNeeded==0 ) break; |
| 1897 p->pFree = sqlite3DbMallocZero(db, nByte); | 1990 x.pSpace = p->pFree = sqlite3DbMallocRawNN(db, x.nNeeded); |
| 1898 } | 1991 x.nFree = x.nNeeded; |
| 1899 zCsr = p->pFree; | 1992 }while( !db->mallocFailed ); |
| 1900 nFree = nByte; | |
| 1901 }while( nByte && !db->mallocFailed ); | |
| 1902 | 1993 |
| 1903 p->nCursor = nCursor; | 1994 p->pVList = pParse->pVList; |
| 1904 p->nOnceFlag = nOnce; | 1995 pParse->pVList = 0; |
| 1905 if( p->aVar ){ | 1996 p->explain = pParse->explain; |
| 1997 if( db->mallocFailed ){ |
| 1998 p->nVar = 0; |
| 1999 p->nCursor = 0; |
| 2000 p->nMem = 0; |
| 2001 }else{ |
| 2002 p->nCursor = nCursor; |
| 1906 p->nVar = (ynVar)nVar; | 2003 p->nVar = (ynVar)nVar; |
| 1907 for(n=0; n<nVar; n++){ | 2004 initMemArray(p->aVar, nVar, db, MEM_Null); |
| 1908 p->aVar[n].flags = MEM_Null; | 2005 p->nMem = nMem; |
| 1909 p->aVar[n].db = db; | 2006 initMemArray(p->aMem, nMem, db, MEM_Undefined); |
| 1910 } | 2007 memset(p->apCsr, 0, nCursor*sizeof(VdbeCursor*)); |
| 2008 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2009 memset(p->anExec, 0, p->nOp*sizeof(i64)); |
| 2010 #endif |
| 1911 } | 2011 } |
| 1912 if( p->azVar && pParse->nzVar>0 ){ | |
| 1913 p->nzVar = pParse->nzVar; | |
| 1914 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); | |
| 1915 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); | |
| 1916 } | |
| 1917 if( p->aMem ){ | |
| 1918 p->aMem--; /* aMem[] goes from 1..nMem */ | |
| 1919 p->nMem = nMem; /* not from 0..nMem-1 */ | |
| 1920 for(n=1; n<=nMem; n++){ | |
| 1921 p->aMem[n].flags = MEM_Undefined; | |
| 1922 p->aMem[n].db = db; | |
| 1923 } | |
| 1924 } | |
| 1925 p->explain = pParse->explain; | |
| 1926 sqlite3VdbeRewind(p); | 2012 sqlite3VdbeRewind(p); |
| 1927 } | 2013 } |
| 1928 | 2014 |
| 1929 /* | 2015 /* |
| 1930 ** Close a VDBE cursor and release all the resources that cursor | 2016 ** Close a VDBE cursor and release all the resources that cursor |
| 1931 ** happens to hold. | 2017 ** happens to hold. |
| 1932 */ | 2018 */ |
| 1933 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ | 2019 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
| 1934 if( pCx==0 ){ | 2020 if( pCx==0 ){ |
| 1935 return; | 2021 return; |
| 1936 } | 2022 } |
| 1937 assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE ); | 2023 assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE ); |
| 1938 switch( pCx->eCurType ){ | 2024 switch( pCx->eCurType ){ |
| 1939 case CURTYPE_SORTER: { | 2025 case CURTYPE_SORTER: { |
| 1940 sqlite3VdbeSorterClose(p->db, pCx); | 2026 sqlite3VdbeSorterClose(p->db, pCx); |
| 1941 break; | 2027 break; |
| 1942 } | 2028 } |
| 1943 case CURTYPE_BTREE: { | 2029 case CURTYPE_BTREE: { |
| 1944 if( pCx->pBt ){ | 2030 if( pCx->pBtx ){ |
| 1945 sqlite3BtreeClose(pCx->pBt); | 2031 sqlite3BtreeClose(pCx->pBtx); |
| 1946 /* The pCx->pCursor will be close automatically, if it exists, by | 2032 /* The pCx->pCursor will be close automatically, if it exists, by |
| 1947 ** the call above. */ | 2033 ** the call above. */ |
| 1948 }else{ | 2034 }else{ |
| 1949 assert( pCx->uc.pCursor!=0 ); | 2035 assert( pCx->uc.pCursor!=0 ); |
| 1950 sqlite3BtreeCloseCursor(pCx->uc.pCursor); | 2036 sqlite3BtreeCloseCursor(pCx->uc.pCursor); |
| 1951 } | 2037 } |
| 1952 break; | 2038 break; |
| 1953 } | 2039 } |
| 1954 #ifndef SQLITE_OMIT_VIRTUALTABLE | 2040 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1955 case CURTYPE_VTAB: { | 2041 case CURTYPE_VTAB: { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1984 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This | 2070 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This |
| 1985 ** is used, for example, when a trigger sub-program is halted to restore | 2071 ** is used, for example, when a trigger sub-program is halted to restore |
| 1986 ** control to the main program. | 2072 ** control to the main program. |
| 1987 */ | 2073 */ |
| 1988 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ | 2074 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 1989 Vdbe *v = pFrame->v; | 2075 Vdbe *v = pFrame->v; |
| 1990 closeCursorsInFrame(v); | 2076 closeCursorsInFrame(v); |
| 1991 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS | 2077 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1992 v->anExec = pFrame->anExec; | 2078 v->anExec = pFrame->anExec; |
| 1993 #endif | 2079 #endif |
| 1994 v->aOnceFlag = pFrame->aOnceFlag; | |
| 1995 v->nOnceFlag = pFrame->nOnceFlag; | |
| 1996 v->aOp = pFrame->aOp; | 2080 v->aOp = pFrame->aOp; |
| 1997 v->nOp = pFrame->nOp; | 2081 v->nOp = pFrame->nOp; |
| 1998 v->aMem = pFrame->aMem; | 2082 v->aMem = pFrame->aMem; |
| 1999 v->nMem = pFrame->nMem; | 2083 v->nMem = pFrame->nMem; |
| 2000 v->apCsr = pFrame->apCsr; | 2084 v->apCsr = pFrame->apCsr; |
| 2001 v->nCursor = pFrame->nCursor; | 2085 v->nCursor = pFrame->nCursor; |
| 2002 v->db->lastRowid = pFrame->lastRowid; | 2086 v->db->lastRowid = pFrame->lastRowid; |
| 2003 v->nChange = pFrame->nChange; | 2087 v->nChange = pFrame->nChange; |
| 2004 v->db->nChange = pFrame->nDbChange; | 2088 v->db->nChange = pFrame->nDbChange; |
| 2089 sqlite3VdbeDeleteAuxData(v->db, &v->pAuxData, -1, 0); |
| 2090 v->pAuxData = pFrame->pAuxData; |
| 2091 pFrame->pAuxData = 0; |
| 2005 return pFrame->pc; | 2092 return pFrame->pc; |
| 2006 } | 2093 } |
| 2007 | 2094 |
| 2008 /* | 2095 /* |
| 2009 ** Close all cursors. | 2096 ** Close all cursors. |
| 2010 ** | 2097 ** |
| 2011 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | 2098 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory |
| 2012 ** cell array. This is necessary as the memory cell array may contain | 2099 ** cell array. This is necessary as the memory cell array may contain |
| 2013 ** pointers to VdbeFrame objects, which may in turn contain pointers to | 2100 ** pointers to VdbeFrame objects, which may in turn contain pointers to |
| 2014 ** open cursors. | 2101 ** open cursors. |
| 2015 */ | 2102 */ |
| 2016 static void closeAllCursors(Vdbe *p){ | 2103 static void closeAllCursors(Vdbe *p){ |
| 2017 if( p->pFrame ){ | 2104 if( p->pFrame ){ |
| 2018 VdbeFrame *pFrame; | 2105 VdbeFrame *pFrame; |
| 2019 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | 2106 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 2020 sqlite3VdbeFrameRestore(pFrame); | 2107 sqlite3VdbeFrameRestore(pFrame); |
| 2021 p->pFrame = 0; | 2108 p->pFrame = 0; |
| 2022 p->nFrame = 0; | 2109 p->nFrame = 0; |
| 2023 } | 2110 } |
| 2024 assert( p->nFrame==0 ); | 2111 assert( p->nFrame==0 ); |
| 2025 closeCursorsInFrame(p); | 2112 closeCursorsInFrame(p); |
| 2026 if( p->aMem ){ | 2113 if( p->aMem ){ |
| 2027 releaseMemArray(&p->aMem[1], p->nMem); | 2114 releaseMemArray(p->aMem, p->nMem); |
| 2028 } | 2115 } |
| 2029 while( p->pDelFrame ){ | 2116 while( p->pDelFrame ){ |
| 2030 VdbeFrame *pDel = p->pDelFrame; | 2117 VdbeFrame *pDel = p->pDelFrame; |
| 2031 p->pDelFrame = pDel->pParent; | 2118 p->pDelFrame = pDel->pParent; |
| 2032 sqlite3VdbeFrameDelete(pDel); | 2119 sqlite3VdbeFrameDelete(pDel); |
| 2033 } | 2120 } |
| 2034 | 2121 |
| 2035 /* Delete any auxdata allocations made by the VM */ | 2122 /* Delete any auxdata allocations made by the VM */ |
| 2036 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p, -1, 0); | 2123 if( p->pAuxData ) sqlite3VdbeDeleteAuxData(p->db, &p->pAuxData, -1, 0); |
| 2037 assert( p->pAuxData==0 ); | 2124 assert( p->pAuxData==0 ); |
| 2038 } | 2125 } |
| 2039 | 2126 |
| 2040 /* | 2127 /* |
| 2041 ** Clean up the VM after a single run. | 2128 ** Clean up the VM after a single run. |
| 2042 */ | 2129 */ |
| 2043 static void Cleanup(Vdbe *p){ | 2130 static void Cleanup(Vdbe *p){ |
| 2044 sqlite3 *db = p->db; | 2131 sqlite3 *db = p->db; |
| 2045 | 2132 |
| 2046 #ifdef SQLITE_DEBUG | 2133 #ifdef SQLITE_DEBUG |
| 2047 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and | 2134 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and |
| 2048 ** Vdbe.aMem[] arrays have already been cleaned up. */ | 2135 ** Vdbe.aMem[] arrays have already been cleaned up. */ |
| 2049 int i; | 2136 int i; |
| 2050 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); | 2137 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 ); |
| 2051 if( p->aMem ){ | 2138 if( p->aMem ){ |
| 2052 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); | 2139 for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined ); |
| 2053 } | 2140 } |
| 2054 #endif | 2141 #endif |
| 2055 | 2142 |
| 2056 sqlite3DbFree(db, p->zErrMsg); | 2143 sqlite3DbFree(db, p->zErrMsg); |
| 2057 p->zErrMsg = 0; | 2144 p->zErrMsg = 0; |
| 2058 p->pResultSet = 0; | 2145 p->pResultSet = 0; |
| 2059 } | 2146 } |
| 2060 | 2147 |
| 2061 /* | 2148 /* |
| 2062 ** Set the number of result columns that will be returned by this SQL | 2149 ** Set the number of result columns that will be returned by this SQL |
| 2063 ** statement. This is now set at compile time, rather than during | 2150 ** statement. This is now set at compile time, rather than during |
| 2064 ** execution of the vdbe program so that sqlite3_column_count() can | 2151 ** execution of the vdbe program so that sqlite3_column_count() can |
| 2065 ** be called on an SQL statement before sqlite3_step(). | 2152 ** be called on an SQL statement before sqlite3_step(). |
| 2066 */ | 2153 */ |
| 2067 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ | 2154 void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){ |
| 2068 Mem *pColName; | 2155 Mem *pColName; |
| 2069 int n; | 2156 int n; |
| 2070 sqlite3 *db = p->db; | 2157 sqlite3 *db = p->db; |
| 2071 | 2158 |
| 2072 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | 2159 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 2073 sqlite3DbFree(db, p->aColName); | 2160 sqlite3DbFree(db, p->aColName); |
| 2074 n = nResColumn*COLNAME_N; | 2161 n = nResColumn*COLNAME_N; |
| 2075 p->nResColumn = (u16)nResColumn; | 2162 p->nResColumn = (u16)nResColumn; |
| 2076 p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n ); | 2163 p->aColName = pColName = (Mem*)sqlite3DbMallocRawNN(db, sizeof(Mem)*n ); |
| 2077 if( p->aColName==0 ) return; | 2164 if( p->aColName==0 ) return; |
| 2078 while( n-- > 0 ){ | 2165 initMemArray(p->aColName, n, p->db, MEM_Null); |
| 2079 pColName->flags = MEM_Null; | |
| 2080 pColName->db = p->db; | |
| 2081 pColName++; | |
| 2082 } | |
| 2083 } | 2166 } |
| 2084 | 2167 |
| 2085 /* | 2168 /* |
| 2086 ** Set the name of the idx'th column to be returned by the SQL statement. | 2169 ** Set the name of the idx'th column to be returned by the SQL statement. |
| 2087 ** zName must be a pointer to a nul terminated string. | 2170 ** zName must be a pointer to a nul terminated string. |
| 2088 ** | 2171 ** |
| 2089 ** This call must be made after a call to sqlite3VdbeSetNumCols(). | 2172 ** This call must be made after a call to sqlite3VdbeSetNumCols(). |
| 2090 ** | 2173 ** |
| 2091 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC | 2174 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC |
| 2092 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed | 2175 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed |
| 2093 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. | 2176 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed. |
| 2094 */ | 2177 */ |
| 2095 int sqlite3VdbeSetColName( | 2178 int sqlite3VdbeSetColName( |
| 2096 Vdbe *p, /* Vdbe being configured */ | 2179 Vdbe *p, /* Vdbe being configured */ |
| 2097 int idx, /* Index of column zName applies to */ | 2180 int idx, /* Index of column zName applies to */ |
| 2098 int var, /* One of the COLNAME_* constants */ | 2181 int var, /* One of the COLNAME_* constants */ |
| 2099 const char *zName, /* Pointer to buffer containing name */ | 2182 const char *zName, /* Pointer to buffer containing name */ |
| 2100 void (*xDel)(void*) /* Memory management strategy for zName */ | 2183 void (*xDel)(void*) /* Memory management strategy for zName */ |
| 2101 ){ | 2184 ){ |
| 2102 int rc; | 2185 int rc; |
| 2103 Mem *pColName; | 2186 Mem *pColName; |
| 2104 assert( idx<p->nResColumn ); | 2187 assert( idx<p->nResColumn ); |
| 2105 assert( var<COLNAME_N ); | 2188 assert( var<COLNAME_N ); |
| 2106 if( p->db->mallocFailed ){ | 2189 if( p->db->mallocFailed ){ |
| 2107 assert( !zName || xDel!=SQLITE_DYNAMIC ); | 2190 assert( !zName || xDel!=SQLITE_DYNAMIC ); |
| 2108 return SQLITE_NOMEM; | 2191 return SQLITE_NOMEM_BKPT; |
| 2109 } | 2192 } |
| 2110 assert( p->aColName!=0 ); | 2193 assert( p->aColName!=0 ); |
| 2111 pColName = &(p->aColName[idx+var*p->nResColumn]); | 2194 pColName = &(p->aColName[idx+var*p->nResColumn]); |
| 2112 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); | 2195 rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel); |
| 2113 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); | 2196 assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 ); |
| 2114 return rc; | 2197 return rc; |
| 2115 } | 2198 } |
| 2116 | 2199 |
| 2117 /* | 2200 /* |
| 2118 ** A read or write transaction may or may not be active on database handle | 2201 ** A read or write transaction may or may not be active on database handle |
| 2119 ** db. If a transaction is active, commit it. If there is a | 2202 ** db. If a transaction is active, commit it. If there is a |
| 2120 ** write-transaction spanning more than one database file, this routine | 2203 ** write-transaction spanning more than one database file, this routine |
| 2121 ** takes care of the master journal trickery. | 2204 ** takes care of the master journal trickery. |
| 2122 */ | 2205 */ |
| 2123 static int vdbeCommit(sqlite3 *db, Vdbe *p){ | 2206 static int vdbeCommit(sqlite3 *db, Vdbe *p){ |
| 2124 int i; | 2207 int i; |
| 2125 int nTrans = 0; /* Number of databases with an active write-transaction */ | 2208 int nTrans = 0; /* Number of databases with an active write-transaction |
| 2209 ** that are candidates for a two-phase commit using a |
| 2210 ** master-journal */ |
| 2126 int rc = SQLITE_OK; | 2211 int rc = SQLITE_OK; |
| 2127 int needXcommit = 0; | 2212 int needXcommit = 0; |
| 2128 | 2213 |
| 2129 #ifdef SQLITE_OMIT_VIRTUALTABLE | 2214 #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 2130 /* With this option, sqlite3VtabSync() is defined to be simply | 2215 /* With this option, sqlite3VtabSync() is defined to be simply |
| 2131 ** SQLITE_OK so p is not used. | 2216 ** SQLITE_OK so p is not used. |
| 2132 */ | 2217 */ |
| 2133 UNUSED_PARAMETER(p); | 2218 UNUSED_PARAMETER(p); |
| 2134 #endif | 2219 #endif |
| 2135 | 2220 |
| 2136 /* Before doing anything else, call the xSync() callback for any | 2221 /* Before doing anything else, call the xSync() callback for any |
| 2137 ** virtual module tables written in this transaction. This has to | 2222 ** virtual module tables written in this transaction. This has to |
| 2138 ** be done before determining whether a master journal file is | 2223 ** be done before determining whether a master journal file is |
| 2139 ** required, as an xSync() callback may add an attached database | 2224 ** required, as an xSync() callback may add an attached database |
| 2140 ** to the transaction. | 2225 ** to the transaction. |
| 2141 */ | 2226 */ |
| 2142 rc = sqlite3VtabSync(db, p); | 2227 rc = sqlite3VtabSync(db, p); |
| 2143 | 2228 |
| 2144 /* This loop determines (a) if the commit hook should be invoked and | 2229 /* This loop determines (a) if the commit hook should be invoked and |
| 2145 ** (b) how many database files have open write transactions, not | 2230 ** (b) how many database files have open write transactions, not |
| 2146 ** including the temp database. (b) is important because if more than | 2231 ** including the temp database. (b) is important because if more than |
| 2147 ** one database file has an open write transaction, a master journal | 2232 ** one database file has an open write transaction, a master journal |
| 2148 ** file is required for an atomic commit. | 2233 ** file is required for an atomic commit. |
| 2149 */ | 2234 */ |
| 2150 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ | 2235 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
| 2151 Btree *pBt = db->aDb[i].pBt; | 2236 Btree *pBt = db->aDb[i].pBt; |
| 2152 if( sqlite3BtreeIsInTrans(pBt) ){ | 2237 if( sqlite3BtreeIsInTrans(pBt) ){ |
| 2238 /* Whether or not a database might need a master journal depends upon |
| 2239 ** its journal mode (among other things). This matrix determines which |
| 2240 ** journal modes use a master journal and which do not */ |
| 2241 static const u8 aMJNeeded[] = { |
| 2242 /* DELETE */ 1, |
| 2243 /* PERSIST */ 1, |
| 2244 /* OFF */ 0, |
| 2245 /* TRUNCATE */ 1, |
| 2246 /* MEMORY */ 0, |
| 2247 /* WAL */ 0 |
| 2248 }; |
| 2249 Pager *pPager; /* Pager associated with pBt */ |
| 2153 needXcommit = 1; | 2250 needXcommit = 1; |
| 2154 if( i!=1 ) nTrans++; | |
| 2155 sqlite3BtreeEnter(pBt); | 2251 sqlite3BtreeEnter(pBt); |
| 2156 rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt)); | 2252 pPager = sqlite3BtreePager(pBt); |
| 2253 if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF |
| 2254 && aMJNeeded[sqlite3PagerGetJournalMode(pPager)] |
| 2255 ){ |
| 2256 assert( i!=1 ); |
| 2257 nTrans++; |
| 2258 } |
| 2259 rc = sqlite3PagerExclusiveLock(pPager); |
| 2157 sqlite3BtreeLeave(pBt); | 2260 sqlite3BtreeLeave(pBt); |
| 2158 } | 2261 } |
| 2159 } | 2262 } |
| 2160 if( rc!=SQLITE_OK ){ | 2263 if( rc!=SQLITE_OK ){ |
| 2161 return rc; | 2264 return rc; |
| 2162 } | 2265 } |
| 2163 | 2266 |
| 2164 /* If there are any write-transactions at all, invoke the commit hook */ | 2267 /* If there are any write-transactions at all, invoke the commit hook */ |
| 2165 if( needXcommit && db->xCommitCallback ){ | 2268 if( needXcommit && db->xCommitCallback ){ |
| 2166 rc = db->xCommitCallback(db->pCommitArg); | 2269 rc = db->xCommitCallback(db->pCommitArg); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2204 } | 2307 } |
| 2205 } | 2308 } |
| 2206 | 2309 |
| 2207 /* The complex case - There is a multi-file write-transaction active. | 2310 /* The complex case - There is a multi-file write-transaction active. |
| 2208 ** This requires a master journal file to ensure the transaction is | 2311 ** This requires a master journal file to ensure the transaction is |
| 2209 ** committed atomically. | 2312 ** committed atomically. |
| 2210 */ | 2313 */ |
| 2211 #ifndef SQLITE_OMIT_DISKIO | 2314 #ifndef SQLITE_OMIT_DISKIO |
| 2212 else{ | 2315 else{ |
| 2213 sqlite3_vfs *pVfs = db->pVfs; | 2316 sqlite3_vfs *pVfs = db->pVfs; |
| 2214 int needSync = 0; | |
| 2215 char *zMaster = 0; /* File-name for the master journal */ | 2317 char *zMaster = 0; /* File-name for the master journal */ |
| 2216 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); | 2318 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt); |
| 2217 sqlite3_file *pMaster = 0; | 2319 sqlite3_file *pMaster = 0; |
| 2218 i64 offset = 0; | 2320 i64 offset = 0; |
| 2219 int res; | 2321 int res; |
| 2220 int retryCount = 0; | 2322 int retryCount = 0; |
| 2221 int nMainFile; | 2323 int nMainFile; |
| 2222 | 2324 |
| 2223 /* Select a master journal file name */ | 2325 /* Select a master journal file name */ |
| 2224 nMainFile = sqlite3Strlen30(zMainFile); | 2326 nMainFile = sqlite3Strlen30(zMainFile); |
| 2225 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); | 2327 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile); |
| 2226 if( zMaster==0 ) return SQLITE_NOMEM; | 2328 if( zMaster==0 ) return SQLITE_NOMEM_BKPT; |
| 2227 do { | 2329 do { |
| 2228 u32 iRandom; | 2330 u32 iRandom; |
| 2229 if( retryCount ){ | 2331 if( retryCount ){ |
| 2230 if( retryCount>100 ){ | 2332 if( retryCount>100 ){ |
| 2231 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); | 2333 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster); |
| 2232 sqlite3OsDelete(pVfs, zMaster, 0); | 2334 sqlite3OsDelete(pVfs, zMaster, 0); |
| 2233 break; | 2335 break; |
| 2234 }else if( retryCount==1 ){ | 2336 }else if( retryCount==1 ){ |
| 2235 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); | 2337 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster); |
| 2236 } | 2338 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 2264 ** back independently if a failure occurs. | 2366 ** back independently if a failure occurs. |
| 2265 */ | 2367 */ |
| 2266 for(i=0; i<db->nDb; i++){ | 2368 for(i=0; i<db->nDb; i++){ |
| 2267 Btree *pBt = db->aDb[i].pBt; | 2369 Btree *pBt = db->aDb[i].pBt; |
| 2268 if( sqlite3BtreeIsInTrans(pBt) ){ | 2370 if( sqlite3BtreeIsInTrans(pBt) ){ |
| 2269 char const *zFile = sqlite3BtreeGetJournalname(pBt); | 2371 char const *zFile = sqlite3BtreeGetJournalname(pBt); |
| 2270 if( zFile==0 ){ | 2372 if( zFile==0 ){ |
| 2271 continue; /* Ignore TEMP and :memory: databases */ | 2373 continue; /* Ignore TEMP and :memory: databases */ |
| 2272 } | 2374 } |
| 2273 assert( zFile[0]!=0 ); | 2375 assert( zFile[0]!=0 ); |
| 2274 if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){ | |
| 2275 needSync = 1; | |
| 2276 } | |
| 2277 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); | 2376 rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset); |
| 2278 offset += sqlite3Strlen30(zFile)+1; | 2377 offset += sqlite3Strlen30(zFile)+1; |
| 2279 if( rc!=SQLITE_OK ){ | 2378 if( rc!=SQLITE_OK ){ |
| 2280 sqlite3OsCloseFree(pMaster); | 2379 sqlite3OsCloseFree(pMaster); |
| 2281 sqlite3OsDelete(pVfs, zMaster, 0); | 2380 sqlite3OsDelete(pVfs, zMaster, 0); |
| 2282 sqlite3DbFree(db, zMaster); | 2381 sqlite3DbFree(db, zMaster); |
| 2283 return rc; | 2382 return rc; |
| 2284 } | 2383 } |
| 2285 } | 2384 } |
| 2286 } | 2385 } |
| 2287 | 2386 |
| 2288 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device | 2387 /* Sync the master journal file. If the IOCAP_SEQUENTIAL device |
| 2289 ** flag is set this is not required. | 2388 ** flag is set this is not required. |
| 2290 */ | 2389 */ |
| 2291 if( needSync | 2390 if( 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) |
| 2292 && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL) | |
| 2293 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) | 2391 && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL)) |
| 2294 ){ | 2392 ){ |
| 2295 sqlite3OsCloseFree(pMaster); | 2393 sqlite3OsCloseFree(pMaster); |
| 2296 sqlite3OsDelete(pVfs, zMaster, 0); | 2394 sqlite3OsDelete(pVfs, zMaster, 0); |
| 2297 sqlite3DbFree(db, zMaster); | 2395 sqlite3DbFree(db, zMaster); |
| 2298 return rc; | 2396 return rc; |
| 2299 } | 2397 } |
| 2300 | 2398 |
| 2301 /* Sync all the db files involved in the transaction. The same call | 2399 /* Sync all the db files involved in the transaction. The same call |
| 2302 ** sets the master journal pointer in each individual journal. If | 2400 ** sets the master journal pointer in each individual journal. If |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2318 assert( rc!=SQLITE_BUSY ); | 2416 assert( rc!=SQLITE_BUSY ); |
| 2319 if( rc!=SQLITE_OK ){ | 2417 if( rc!=SQLITE_OK ){ |
| 2320 sqlite3DbFree(db, zMaster); | 2418 sqlite3DbFree(db, zMaster); |
| 2321 return rc; | 2419 return rc; |
| 2322 } | 2420 } |
| 2323 | 2421 |
| 2324 /* Delete the master journal file. This commits the transaction. After | 2422 /* Delete the master journal file. This commits the transaction. After |
| 2325 ** doing this the directory is synced again before any individual | 2423 ** doing this the directory is synced again before any individual |
| 2326 ** transaction files are deleted. | 2424 ** transaction files are deleted. |
| 2327 */ | 2425 */ |
| 2328 rc = sqlite3OsDelete(pVfs, zMaster, needSync); | 2426 rc = sqlite3OsDelete(pVfs, zMaster, 1); |
| 2329 sqlite3DbFree(db, zMaster); | 2427 sqlite3DbFree(db, zMaster); |
| 2330 zMaster = 0; | 2428 zMaster = 0; |
| 2331 if( rc ){ | 2429 if( rc ){ |
| 2332 return rc; | 2430 return rc; |
| 2333 } | 2431 } |
| 2334 | 2432 |
| 2335 /* All files and directories have already been synced, so the following | 2433 /* All files and directories have already been synced, so the following |
| 2336 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and | 2434 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and |
| 2337 ** deleting or truncating journals. If something goes wrong while | 2435 ** deleting or truncating journals. If something goes wrong while |
| 2338 ** this is happening we don't really care. The integrity of the | 2436 ** this is happening we don't really care. The integrity of the |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2392 /* | 2490 /* |
| 2393 ** If the Vdbe passed as the first argument opened a statement-transaction, | 2491 ** If the Vdbe passed as the first argument opened a statement-transaction, |
| 2394 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or | 2492 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or |
| 2395 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement | 2493 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement |
| 2396 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the | 2494 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the |
| 2397 ** statement transaction is committed. | 2495 ** statement transaction is committed. |
| 2398 ** | 2496 ** |
| 2399 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. | 2497 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned. |
| 2400 ** Otherwise SQLITE_OK. | 2498 ** Otherwise SQLITE_OK. |
| 2401 */ | 2499 */ |
| 2402 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ | 2500 static SQLITE_NOINLINE int vdbeCloseStatement(Vdbe *p, int eOp){ |
| 2403 sqlite3 *const db = p->db; | 2501 sqlite3 *const db = p->db; |
| 2404 int rc = SQLITE_OK; | 2502 int rc = SQLITE_OK; |
| 2503 int i; |
| 2504 const int iSavepoint = p->iStatement-1; |
| 2405 | 2505 |
| 2406 /* If p->iStatement is greater than zero, then this Vdbe opened a | 2506 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); |
| 2407 ** statement transaction that should be closed here. The only exception | 2507 assert( db->nStatement>0 ); |
| 2408 ** is that an IO error may have occurred, causing an emergency rollback. | 2508 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); |
| 2409 ** In this case (db->nStatement==0), and there is nothing to do. | |
| 2410 */ | |
| 2411 if( db->nStatement && p->iStatement ){ | |
| 2412 int i; | |
| 2413 const int iSavepoint = p->iStatement-1; | |
| 2414 | 2509 |
| 2415 assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE); | 2510 for(i=0; i<db->nDb; i++){ |
| 2416 assert( db->nStatement>0 ); | 2511 int rc2 = SQLITE_OK; |
| 2417 assert( p->iStatement==(db->nStatement+db->nSavepoint) ); | 2512 Btree *pBt = db->aDb[i].pBt; |
| 2418 | 2513 if( pBt ){ |
| 2419 for(i=0; i<db->nDb; i++){ | 2514 if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2420 int rc2 = SQLITE_OK; | 2515 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); |
| 2421 Btree *pBt = db->aDb[i].pBt; | 2516 } |
| 2422 if( pBt ){ | 2517 if( rc2==SQLITE_OK ){ |
| 2423 if( eOp==SAVEPOINT_ROLLBACK ){ | 2518 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); |
| 2424 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint); | 2519 } |
| 2425 } | 2520 if( rc==SQLITE_OK ){ |
| 2426 if( rc2==SQLITE_OK ){ | 2521 rc = rc2; |
| 2427 rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint); | |
| 2428 } | |
| 2429 if( rc==SQLITE_OK ){ | |
| 2430 rc = rc2; | |
| 2431 } | |
| 2432 } | 2522 } |
| 2433 } | 2523 } |
| 2434 db->nStatement--; | 2524 } |
| 2435 p->iStatement = 0; | 2525 db->nStatement--; |
| 2526 p->iStatement = 0; |
| 2436 | 2527 |
| 2528 if( rc==SQLITE_OK ){ |
| 2529 if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2530 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); |
| 2531 } |
| 2437 if( rc==SQLITE_OK ){ | 2532 if( rc==SQLITE_OK ){ |
| 2438 if( eOp==SAVEPOINT_ROLLBACK ){ | 2533 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); |
| 2439 rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint); | |
| 2440 } | |
| 2441 if( rc==SQLITE_OK ){ | |
| 2442 rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint); | |
| 2443 } | |
| 2444 } | 2534 } |
| 2535 } |
| 2445 | 2536 |
| 2446 /* If the statement transaction is being rolled back, also restore the | 2537 /* If the statement transaction is being rolled back, also restore the |
| 2447 ** database handles deferred constraint counter to the value it had when | 2538 ** database handles deferred constraint counter to the value it had when |
| 2448 ** the statement transaction was opened. */ | 2539 ** the statement transaction was opened. */ |
| 2449 if( eOp==SAVEPOINT_ROLLBACK ){ | 2540 if( eOp==SAVEPOINT_ROLLBACK ){ |
| 2450 db->nDeferredCons = p->nStmtDefCons; | 2541 db->nDeferredCons = p->nStmtDefCons; |
| 2451 db->nDeferredImmCons = p->nStmtDefImmCons; | 2542 db->nDeferredImmCons = p->nStmtDefImmCons; |
| 2452 } | |
| 2453 } | 2543 } |
| 2454 return rc; | 2544 return rc; |
| 2455 } | 2545 } |
| 2546 int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){ |
| 2547 if( p->db->nStatement && p->iStatement ){ |
| 2548 return vdbeCloseStatement(p, eOp); |
| 2549 } |
| 2550 return SQLITE_OK; |
| 2551 } |
| 2552 |
| 2456 | 2553 |
| 2457 /* | 2554 /* |
| 2458 ** This function is called when a transaction opened by the database | 2555 ** This function is called when a transaction opened by the database |
| 2459 ** handle associated with the VM passed as an argument is about to be | 2556 ** handle associated with the VM passed as an argument is about to be |
| 2460 ** committed. If there are outstanding deferred foreign key constraint | 2557 ** committed. If there are outstanding deferred foreign key constraint |
| 2461 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. | 2558 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK. |
| 2462 ** | 2559 ** |
| 2463 ** If there are outstanding FK violations and this function returns | 2560 ** If there are outstanding FK violations and this function returns |
| 2464 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY | 2561 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY |
| 2465 ** and write an error message to it. Then return SQLITE_ERROR. | 2562 ** and write an error message to it. Then return SQLITE_ERROR. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2505 ** SQLITE_NOMEM | 2602 ** SQLITE_NOMEM |
| 2506 ** SQLITE_IOERR | 2603 ** SQLITE_IOERR |
| 2507 ** SQLITE_FULL | 2604 ** SQLITE_FULL |
| 2508 ** SQLITE_INTERRUPT | 2605 ** SQLITE_INTERRUPT |
| 2509 ** | 2606 ** |
| 2510 ** Then the internal cache might have been left in an inconsistent | 2607 ** Then the internal cache might have been left in an inconsistent |
| 2511 ** state. We need to rollback the statement transaction, if there is | 2608 ** state. We need to rollback the statement transaction, if there is |
| 2512 ** one, or the complete transaction if there is no statement transaction. | 2609 ** one, or the complete transaction if there is no statement transaction. |
| 2513 */ | 2610 */ |
| 2514 | 2611 |
| 2515 if( p->db->mallocFailed ){ | 2612 if( db->mallocFailed ){ |
| 2516 p->rc = SQLITE_NOMEM; | 2613 p->rc = SQLITE_NOMEM_BKPT; |
| 2517 } | 2614 } |
| 2518 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag); | |
| 2519 closeAllCursors(p); | 2615 closeAllCursors(p); |
| 2520 if( p->magic!=VDBE_MAGIC_RUN ){ | 2616 if( p->magic!=VDBE_MAGIC_RUN ){ |
| 2521 return SQLITE_OK; | 2617 return SQLITE_OK; |
| 2522 } | 2618 } |
| 2523 checkActiveVdbeCnt(db); | 2619 checkActiveVdbeCnt(db); |
| 2524 | 2620 |
| 2525 /* No commit or rollback needed if the program never started or if the | 2621 /* No commit or rollback needed if the program never started or if the |
| 2526 ** SQL statement does not read or write a database file. */ | 2622 ** SQL statement does not read or write a database file. */ |
| 2527 if( p->pc>=0 && p->bIsReader ){ | 2623 if( p->pc>=0 && p->bIsReader ){ |
| 2528 int mrc; /* Primary error code from p->rc */ | 2624 int mrc; /* Primary error code from p->rc */ |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2666 if( p->pc>=0 ){ | 2762 if( p->pc>=0 ){ |
| 2667 db->nVdbeActive--; | 2763 db->nVdbeActive--; |
| 2668 if( !p->readOnly ) db->nVdbeWrite--; | 2764 if( !p->readOnly ) db->nVdbeWrite--; |
| 2669 if( p->bIsReader ) db->nVdbeRead--; | 2765 if( p->bIsReader ) db->nVdbeRead--; |
| 2670 assert( db->nVdbeActive>=db->nVdbeRead ); | 2766 assert( db->nVdbeActive>=db->nVdbeRead ); |
| 2671 assert( db->nVdbeRead>=db->nVdbeWrite ); | 2767 assert( db->nVdbeRead>=db->nVdbeWrite ); |
| 2672 assert( db->nVdbeWrite>=0 ); | 2768 assert( db->nVdbeWrite>=0 ); |
| 2673 } | 2769 } |
| 2674 p->magic = VDBE_MAGIC_HALT; | 2770 p->magic = VDBE_MAGIC_HALT; |
| 2675 checkActiveVdbeCnt(db); | 2771 checkActiveVdbeCnt(db); |
| 2676 if( p->db->mallocFailed ){ | 2772 if( db->mallocFailed ){ |
| 2677 p->rc = SQLITE_NOMEM; | 2773 p->rc = SQLITE_NOMEM_BKPT; |
| 2678 } | 2774 } |
| 2679 | 2775 |
| 2680 /* If the auto-commit flag is set to true, then any locks that were held | 2776 /* If the auto-commit flag is set to true, then any locks that were held |
| 2681 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() | 2777 ** by connection db have now been released. Call sqlite3ConnectionUnlocked() |
| 2682 ** to invoke any required unlock-notify callbacks. | 2778 ** to invoke any required unlock-notify callbacks. |
| 2683 */ | 2779 */ |
| 2684 if( db->autoCommit ){ | 2780 if( db->autoCommit ){ |
| 2685 sqlite3ConnectionUnlocked(db); | 2781 sqlite3ConnectionUnlocked(db); |
| 2686 } | 2782 } |
| 2687 | 2783 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 2703 ** as the first argument to its database handle (so that they will be | 2799 ** as the first argument to its database handle (so that they will be |
| 2704 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). | 2800 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()). |
| 2705 ** | 2801 ** |
| 2706 ** This function does not clear the VDBE error code or message, just | 2802 ** This function does not clear the VDBE error code or message, just |
| 2707 ** copies them to the database handle. | 2803 ** copies them to the database handle. |
| 2708 */ | 2804 */ |
| 2709 int sqlite3VdbeTransferError(Vdbe *p){ | 2805 int sqlite3VdbeTransferError(Vdbe *p){ |
| 2710 sqlite3 *db = p->db; | 2806 sqlite3 *db = p->db; |
| 2711 int rc = p->rc; | 2807 int rc = p->rc; |
| 2712 if( p->zErrMsg ){ | 2808 if( p->zErrMsg ){ |
| 2713 u8 mallocFailed = db->mallocFailed; | 2809 db->bBenignMalloc++; |
| 2714 sqlite3BeginBenignMalloc(); | 2810 sqlite3BeginBenignMalloc(); |
| 2715 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); | 2811 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db); |
| 2716 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); | 2812 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT); |
| 2717 sqlite3EndBenignMalloc(); | 2813 sqlite3EndBenignMalloc(); |
| 2718 db->mallocFailed = mallocFailed; | 2814 db->bBenignMalloc--; |
| 2719 db->errCode = rc; | 2815 db->errCode = rc; |
| 2720 }else{ | 2816 }else{ |
| 2721 sqlite3Error(db, rc); | 2817 sqlite3Error(db, rc); |
| 2722 } | 2818 } |
| 2723 return rc; | 2819 return rc; |
| 2724 } | 2820 } |
| 2725 | 2821 |
| 2726 #ifdef SQLITE_ENABLE_SQLLOG | 2822 #ifdef SQLITE_ENABLE_SQLLOG |
| 2727 /* | 2823 /* |
| 2728 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, | 2824 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run, |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2820 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 | 2916 p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0 |
| 2821 ); | 2917 ); |
| 2822 fprintf(out, "%s", zHdr); | 2918 fprintf(out, "%s", zHdr); |
| 2823 sqlite3VdbePrintOp(out, i, &p->aOp[i]); | 2919 sqlite3VdbePrintOp(out, i, &p->aOp[i]); |
| 2824 } | 2920 } |
| 2825 fclose(out); | 2921 fclose(out); |
| 2826 } | 2922 } |
| 2827 } | 2923 } |
| 2828 #endif | 2924 #endif |
| 2829 p->iCurrentTime = 0; | 2925 p->iCurrentTime = 0; |
| 2830 p->magic = VDBE_MAGIC_INIT; | 2926 p->magic = VDBE_MAGIC_RESET; |
| 2831 return p->rc & db->errMask; | 2927 return p->rc & db->errMask; |
| 2832 } | 2928 } |
| 2833 | 2929 |
| 2834 /* | 2930 /* |
| 2835 ** Clean up and delete a VDBE after execution. Return an integer which is | 2931 ** Clean up and delete a VDBE after execution. Return an integer which is |
| 2836 ** the result code. Write any error message text into *pzErrMsg. | 2932 ** the result code. Write any error message text into *pzErrMsg. |
| 2837 */ | 2933 */ |
| 2838 int sqlite3VdbeFinalize(Vdbe *p){ | 2934 int sqlite3VdbeFinalize(Vdbe *p){ |
| 2839 int rc = SQLITE_OK; | 2935 int rc = SQLITE_OK; |
| 2840 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ | 2936 if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 2854 ** only invoked for those auxiliary data pointers created by the user | 2950 ** only invoked for those auxiliary data pointers created by the user |
| 2855 ** function invoked by the OP_Function opcode at instruction iOp of | 2951 ** function invoked by the OP_Function opcode at instruction iOp of |
| 2856 ** VM pVdbe, and only then if: | 2952 ** VM pVdbe, and only then if: |
| 2857 ** | 2953 ** |
| 2858 ** * the associated function parameter is the 32nd or later (counting | 2954 ** * the associated function parameter is the 32nd or later (counting |
| 2859 ** from left to right), or | 2955 ** from left to right), or |
| 2860 ** | 2956 ** |
| 2861 ** * the corresponding bit in argument mask is clear (where the first | 2957 ** * the corresponding bit in argument mask is clear (where the first |
| 2862 ** function parameter corresponds to bit 0 etc.). | 2958 ** function parameter corresponds to bit 0 etc.). |
| 2863 */ | 2959 */ |
| 2864 void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ | 2960 void sqlite3VdbeDeleteAuxData(sqlite3 *db, AuxData **pp, int iOp, int mask){ |
| 2865 AuxData **pp = &pVdbe->pAuxData; | |
| 2866 while( *pp ){ | 2961 while( *pp ){ |
| 2867 AuxData *pAux = *pp; | 2962 AuxData *pAux = *pp; |
| 2868 if( (iOp<0) | 2963 if( (iOp<0) |
| 2869 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) | 2964 || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) |
| 2870 ){ | 2965 ){ |
| 2871 testcase( pAux->iArg==31 ); | 2966 testcase( pAux->iArg==31 ); |
| 2872 if( pAux->xDelete ){ | 2967 if( pAux->xDelete ){ |
| 2873 pAux->xDelete(pAux->pAux); | 2968 pAux->xDelete(pAux->pAux); |
| 2874 } | 2969 } |
| 2875 *pp = pAux->pNext; | 2970 *pp = pAux->pNext; |
| 2876 sqlite3DbFree(pVdbe->db, pAux); | 2971 sqlite3DbFree(db, pAux); |
| 2877 }else{ | 2972 }else{ |
| 2878 pp= &pAux->pNext; | 2973 pp= &pAux->pNext; |
| 2879 } | 2974 } |
| 2880 } | 2975 } |
| 2881 } | 2976 } |
| 2882 | 2977 |
| 2883 /* | 2978 /* |
| 2884 ** Free all memory associated with the Vdbe passed as the second argument, | 2979 ** Free all memory associated with the Vdbe passed as the second argument, |
| 2885 ** except for object itself, which is preserved. | 2980 ** except for object itself, which is preserved. |
| 2886 ** | 2981 ** |
| 2887 ** The difference between this function and sqlite3VdbeDelete() is that | 2982 ** The difference between this function and sqlite3VdbeDelete() is that |
| 2888 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with | 2983 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with |
| 2889 ** the database connection and frees the object itself. | 2984 ** the database connection and frees the object itself. |
| 2890 */ | 2985 */ |
| 2891 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ | 2986 void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){ |
| 2892 SubProgram *pSub, *pNext; | 2987 SubProgram *pSub, *pNext; |
| 2893 int i; | |
| 2894 assert( p->db==0 || p->db==db ); | 2988 assert( p->db==0 || p->db==db ); |
| 2895 releaseMemArray(p->aVar, p->nVar); | |
| 2896 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); | 2989 releaseMemArray(p->aColName, p->nResColumn*COLNAME_N); |
| 2897 for(pSub=p->pProgram; pSub; pSub=pNext){ | 2990 for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 2898 pNext = pSub->pNext; | 2991 pNext = pSub->pNext; |
| 2899 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); | 2992 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 2900 sqlite3DbFree(db, pSub); | 2993 sqlite3DbFree(db, pSub); |
| 2901 } | 2994 } |
| 2902 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); | 2995 if( p->magic!=VDBE_MAGIC_INIT ){ |
| 2996 releaseMemArray(p->aVar, p->nVar); |
| 2997 sqlite3DbFree(db, p->pVList); |
| 2998 sqlite3DbFree(db, p->pFree); |
| 2999 } |
| 2903 vdbeFreeOpArray(db, p->aOp, p->nOp); | 3000 vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 2904 sqlite3DbFree(db, p->aColName); | 3001 sqlite3DbFree(db, p->aColName); |
| 2905 sqlite3DbFree(db, p->zSql); | 3002 sqlite3DbFree(db, p->zSql); |
| 2906 sqlite3DbFree(db, p->pFree); | |
| 2907 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS | 3003 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2908 for(i=0; i<p->nScan; i++){ | 3004 { |
| 2909 sqlite3DbFree(db, p->aScan[i].zName); | 3005 int i; |
| 3006 for(i=0; i<p->nScan; i++){ |
| 3007 sqlite3DbFree(db, p->aScan[i].zName); |
| 3008 } |
| 3009 sqlite3DbFree(db, p->aScan); |
| 2910 } | 3010 } |
| 2911 sqlite3DbFree(db, p->aScan); | |
| 2912 #endif | 3011 #endif |
| 2913 } | 3012 } |
| 2914 | 3013 |
| 2915 /* | 3014 /* |
| 2916 ** Delete an entire VDBE. | 3015 ** Delete an entire VDBE. |
| 2917 */ | 3016 */ |
| 2918 void sqlite3VdbeDelete(Vdbe *p){ | 3017 void sqlite3VdbeDelete(Vdbe *p){ |
| 2919 sqlite3 *db; | 3018 sqlite3 *db; |
| 2920 | 3019 |
| 2921 if( NEVER(p==0) ) return; | 3020 if( NEVER(p==0) ) return; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2996 ** prevents us from positioning the cursor to its correct position. | 3095 ** prevents us from positioning the cursor to its correct position. |
| 2997 ** | 3096 ** |
| 2998 ** If a MoveTo operation is pending on the given cursor, then do that | 3097 ** If a MoveTo operation is pending on the given cursor, then do that |
| 2999 ** MoveTo now. If no move is pending, check to see if the row has been | 3098 ** MoveTo now. If no move is pending, check to see if the row has been |
| 3000 ** deleted out from under the cursor and if it has, mark the row as | 3099 ** deleted out from under the cursor and if it has, mark the row as |
| 3001 ** a NULL row. | 3100 ** a NULL row. |
| 3002 ** | 3101 ** |
| 3003 ** If the cursor is already pointing to the correct row and that row has | 3102 ** If the cursor is already pointing to the correct row and that row has |
| 3004 ** not been deleted out from under the cursor, then this routine is a no-op. | 3103 ** not been deleted out from under the cursor, then this routine is a no-op. |
| 3005 */ | 3104 */ |
| 3006 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ | 3105 int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){ |
| 3106 VdbeCursor *p = *pp; |
| 3007 if( p->eCurType==CURTYPE_BTREE ){ | 3107 if( p->eCurType==CURTYPE_BTREE ){ |
| 3008 if( p->deferredMoveto ){ | 3108 if( p->deferredMoveto ){ |
| 3109 int iMap; |
| 3110 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){ |
| 3111 *pp = p->pAltCursor; |
| 3112 *piCol = iMap - 1; |
| 3113 return SQLITE_OK; |
| 3114 } |
| 3009 return handleDeferredMoveto(p); | 3115 return handleDeferredMoveto(p); |
| 3010 } | 3116 } |
| 3011 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ | 3117 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ |
| 3012 return handleMovedCursor(p); | 3118 return handleMovedCursor(p); |
| 3013 } | 3119 } |
| 3014 } | 3120 } |
| 3015 return SQLITE_OK; | 3121 return SQLITE_OK; |
| 3016 } | 3122 } |
| 3017 | 3123 |
| 3018 /* | 3124 /* |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3395 ** The space is either allocated using sqlite3DbMallocRaw() or from within | 3501 ** The space is either allocated using sqlite3DbMallocRaw() or from within |
| 3396 ** the unaligned buffer passed via the second and third arguments (presumably | 3502 ** the unaligned buffer passed via the second and third arguments (presumably |
| 3397 ** stack space). If the former, then *ppFree is set to a pointer that should | 3503 ** stack space). If the former, then *ppFree is set to a pointer that should |
| 3398 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the | 3504 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the |
| 3399 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL | 3505 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL |
| 3400 ** before returning. | 3506 ** before returning. |
| 3401 ** | 3507 ** |
| 3402 ** If an OOM error occurs, NULL is returned. | 3508 ** If an OOM error occurs, NULL is returned. |
| 3403 */ | 3509 */ |
| 3404 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( | 3510 UnpackedRecord *sqlite3VdbeAllocUnpackedRecord( |
| 3405 KeyInfo *pKeyInfo, /* Description of the record */ | 3511 KeyInfo *pKeyInfo /* Description of the record */ |
| 3406 char *pSpace, /* Unaligned space available */ | |
| 3407 int szSpace, /* Size of pSpace[] in bytes */ | |
| 3408 char **ppFree /* OUT: Caller should free this pointer */ | |
| 3409 ){ | 3512 ){ |
| 3410 UnpackedRecord *p; /* Unpacked record to return */ | 3513 UnpackedRecord *p; /* Unpacked record to return */ |
| 3411 int nOff; /* Increment pSpace by nOff to align it */ | |
| 3412 int nByte; /* Number of bytes required for *p */ | 3514 int nByte; /* Number of bytes required for *p */ |
| 3413 | |
| 3414 /* We want to shift the pointer pSpace up such that it is 8-byte aligned. | |
| 3415 ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift | |
| 3416 ** it by. If pSpace is already 8-byte aligned, nOff should be zero. | |
| 3417 */ | |
| 3418 nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7; | |
| 3419 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); | 3515 nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1); |
| 3420 if( nByte>szSpace+nOff ){ | 3516 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); |
| 3421 p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte); | 3517 if( !p ) return 0; |
| 3422 *ppFree = (char *)p; | |
| 3423 if( !p ) return 0; | |
| 3424 }else{ | |
| 3425 p = (UnpackedRecord*)&pSpace[nOff]; | |
| 3426 *ppFree = 0; | |
| 3427 } | |
| 3428 | |
| 3429 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; | 3518 p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))]; |
| 3430 assert( pKeyInfo->aSortOrder!=0 ); | 3519 assert( pKeyInfo->aSortOrder!=0 ); |
| 3431 p->pKeyInfo = pKeyInfo; | 3520 p->pKeyInfo = pKeyInfo; |
| 3432 p->nField = pKeyInfo->nField + 1; | 3521 p->nField = pKeyInfo->nField + 1; |
| 3433 return p; | 3522 return p; |
| 3434 } | 3523 } |
| 3435 | 3524 |
| 3436 /* | 3525 /* |
| 3437 ** Given the nKey-byte encoding of a record in pKey[], populate the | 3526 ** Given the nKey-byte encoding of a record in pKey[], populate the |
| 3438 ** UnpackedRecord structure indicated by the fourth argument with the | 3527 ** UnpackedRecord structure indicated by the fourth argument with the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3457 d = szHdr; | 3546 d = szHdr; |
| 3458 u = 0; | 3547 u = 0; |
| 3459 while( idx<szHdr && d<=nKey ){ | 3548 while( idx<szHdr && d<=nKey ){ |
| 3460 u32 serial_type; | 3549 u32 serial_type; |
| 3461 | 3550 |
| 3462 idx += getVarint32(&aKey[idx], serial_type); | 3551 idx += getVarint32(&aKey[idx], serial_type); |
| 3463 pMem->enc = pKeyInfo->enc; | 3552 pMem->enc = pKeyInfo->enc; |
| 3464 pMem->db = pKeyInfo->db; | 3553 pMem->db = pKeyInfo->db; |
| 3465 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ | 3554 /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */ |
| 3466 pMem->szMalloc = 0; | 3555 pMem->szMalloc = 0; |
| 3556 pMem->z = 0; |
| 3467 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); | 3557 d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem); |
| 3468 pMem++; | 3558 pMem++; |
| 3469 if( (++u)>=p->nField ) break; | 3559 if( (++u)>=p->nField ) break; |
| 3470 } | 3560 } |
| 3471 assert( u<=pKeyInfo->nField + 1 ); | 3561 assert( u<=pKeyInfo->nField + 1 ); |
| 3472 p->nField = u; | 3562 p->nField = u; |
| 3473 } | 3563 } |
| 3474 | 3564 |
| 3475 #if SQLITE_DEBUG | 3565 #if SQLITE_DEBUG |
| 3476 /* | 3566 /* |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3637 Mem c2; | 3727 Mem c2; |
| 3638 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); | 3728 sqlite3VdbeMemInit(&c1, pMem1->db, MEM_Null); |
| 3639 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); | 3729 sqlite3VdbeMemInit(&c2, pMem1->db, MEM_Null); |
| 3640 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); | 3730 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); |
| 3641 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); | 3731 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); |
| 3642 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); | 3732 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); |
| 3643 n1 = v1==0 ? 0 : c1.n; | 3733 n1 = v1==0 ? 0 : c1.n; |
| 3644 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); | 3734 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); |
| 3645 n2 = v2==0 ? 0 : c2.n; | 3735 n2 = v2==0 ? 0 : c2.n; |
| 3646 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); | 3736 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); |
| 3737 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM_BKPT; |
| 3647 sqlite3VdbeMemRelease(&c1); | 3738 sqlite3VdbeMemRelease(&c1); |
| 3648 sqlite3VdbeMemRelease(&c2); | 3739 sqlite3VdbeMemRelease(&c2); |
| 3649 if( (v1==0 || v2==0) && prcErr ) *prcErr = SQLITE_NOMEM; | |
| 3650 return rc; | 3740 return rc; |
| 3651 } | 3741 } |
| 3652 } | 3742 } |
| 3653 | 3743 |
| 3654 /* | 3744 /* |
| 3745 ** The input pBlob is guaranteed to be a Blob that is not marked |
| 3746 ** with MEM_Zero. Return true if it could be a zero-blob. |
| 3747 */ |
| 3748 static int isAllZero(const char *z, int n){ |
| 3749 int i; |
| 3750 for(i=0; i<n; i++){ |
| 3751 if( z[i] ) return 0; |
| 3752 } |
| 3753 return 1; |
| 3754 } |
| 3755 |
| 3756 /* |
| 3655 ** Compare two blobs. Return negative, zero, or positive if the first | 3757 ** Compare two blobs. Return negative, zero, or positive if the first |
| 3656 ** is less than, equal to, or greater than the second, respectively. | 3758 ** is less than, equal to, or greater than the second, respectively. |
| 3657 ** If one blob is a prefix of the other, then the shorter is the lessor. | 3759 ** If one blob is a prefix of the other, then the shorter is the lessor. |
| 3658 */ | 3760 */ |
| 3659 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ | 3761 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ |
| 3660 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); | 3762 int c; |
| 3763 int n1 = pB1->n; |
| 3764 int n2 = pB2->n; |
| 3765 |
| 3766 /* It is possible to have a Blob value that has some non-zero content |
| 3767 ** followed by zero content. But that only comes up for Blobs formed |
| 3768 ** by the OP_MakeRecord opcode, and such Blobs never get passed into |
| 3769 ** sqlite3MemCompare(). */ |
| 3770 assert( (pB1->flags & MEM_Zero)==0 || n1==0 ); |
| 3771 assert( (pB2->flags & MEM_Zero)==0 || n2==0 ); |
| 3772 |
| 3773 if( (pB1->flags|pB2->flags) & MEM_Zero ){ |
| 3774 if( pB1->flags & pB2->flags & MEM_Zero ){ |
| 3775 return pB1->u.nZero - pB2->u.nZero; |
| 3776 }else if( pB1->flags & MEM_Zero ){ |
| 3777 if( !isAllZero(pB2->z, pB2->n) ) return -1; |
| 3778 return pB1->u.nZero - n2; |
| 3779 }else{ |
| 3780 if( !isAllZero(pB1->z, pB1->n) ) return +1; |
| 3781 return n1 - pB2->u.nZero; |
| 3782 } |
| 3783 } |
| 3784 c = memcmp(pB1->z, pB2->z, n1>n2 ? n2 : n1); |
| 3661 if( c ) return c; | 3785 if( c ) return c; |
| 3662 return pB1->n - pB2->n; | 3786 return n1 - n2; |
| 3663 } | 3787 } |
| 3664 | 3788 |
| 3665 /* | 3789 /* |
| 3666 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point | 3790 ** Do a comparison between a 64-bit signed integer and a 64-bit floating-point |
| 3667 ** number. Return negative, zero, or positive if the first (i64) is less than, | 3791 ** number. Return negative, zero, or positive if the first (i64) is less than, |
| 3668 ** equal to, or greater than the second (double). | 3792 ** equal to, or greater than the second (double). |
| 3669 */ | 3793 */ |
| 3670 static int sqlite3IntFloatCompare(i64 i, double r){ | 3794 static int sqlite3IntFloatCompare(i64 i, double r){ |
| 3671 if( sizeof(LONGDOUBLE_TYPE)>8 ){ | 3795 if( sizeof(LONGDOUBLE_TYPE)>8 ){ |
| 3672 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; | 3796 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3958 }else{ | 4082 }else{ |
| 3959 int nCmp = MIN(mem1.n, pRhs->n); | 4083 int nCmp = MIN(mem1.n, pRhs->n); |
| 3960 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); | 4084 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 3961 if( rc==0 ) rc = mem1.n - pRhs->n; | 4085 if( rc==0 ) rc = mem1.n - pRhs->n; |
| 3962 } | 4086 } |
| 3963 } | 4087 } |
| 3964 } | 4088 } |
| 3965 | 4089 |
| 3966 /* RHS is a blob */ | 4090 /* RHS is a blob */ |
| 3967 else if( pRhs->flags & MEM_Blob ){ | 4091 else if( pRhs->flags & MEM_Blob ){ |
| 4092 assert( (pRhs->flags & MEM_Zero)==0 || pRhs->n==0 ); |
| 3968 getVarint32(&aKey1[idx1], serial_type); | 4093 getVarint32(&aKey1[idx1], serial_type); |
| 3969 testcase( serial_type==12 ); | 4094 testcase( serial_type==12 ); |
| 3970 if( serial_type<12 || (serial_type & 0x01) ){ | 4095 if( serial_type<12 || (serial_type & 0x01) ){ |
| 3971 rc = -1; | 4096 rc = -1; |
| 3972 }else{ | 4097 }else{ |
| 3973 int nStr = (serial_type - 12) / 2; | 4098 int nStr = (serial_type - 12) / 2; |
| 3974 testcase( (d1+nStr)==(unsigned)nKey1 ); | 4099 testcase( (d1+nStr)==(unsigned)nKey1 ); |
| 3975 testcase( (d1+nStr+1)==(unsigned)nKey1 ); | 4100 testcase( (d1+nStr+1)==(unsigned)nKey1 ); |
| 3976 if( (d1+nStr) > (unsigned)nKey1 ){ | 4101 if( (d1+nStr) > (unsigned)nKey1 ){ |
| 3977 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | 4102 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3978 return 0; /* Corruption */ | 4103 return 0; /* Corruption */ |
| 4104 }else if( pRhs->flags & MEM_Zero ){ |
| 4105 if( !isAllZero((const char*)&aKey1[d1],nStr) ){ |
| 4106 rc = 1; |
| 4107 }else{ |
| 4108 rc = nStr - pRhs->u.nZero; |
| 4109 } |
| 3979 }else{ | 4110 }else{ |
| 3980 int nCmp = MIN(nStr, pRhs->n); | 4111 int nCmp = MIN(nStr, pRhs->n); |
| 3981 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); | 4112 rc = memcmp(&aKey1[d1], pRhs->z, nCmp); |
| 3982 if( rc==0 ) rc = nStr - pRhs->n; | 4113 if( rc==0 ) rc = nStr - pRhs->n; |
| 3983 } | 4114 } |
| 3984 } | 4115 } |
| 3985 } | 4116 } |
| 3986 | 4117 |
| 3987 /* RHS is null */ | 4118 /* RHS is null */ |
| 3988 else{ | 4119 else{ |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4039 */ | 4170 */ |
| 4040 static int vdbeRecordCompareInt( | 4171 static int vdbeRecordCompareInt( |
| 4041 int nKey1, const void *pKey1, /* Left key */ | 4172 int nKey1, const void *pKey1, /* Left key */ |
| 4042 UnpackedRecord *pPKey2 /* Right key */ | 4173 UnpackedRecord *pPKey2 /* Right key */ |
| 4043 ){ | 4174 ){ |
| 4044 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; | 4175 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
| 4045 int serial_type = ((const u8*)pKey1)[1]; | 4176 int serial_type = ((const u8*)pKey1)[1]; |
| 4046 int res; | 4177 int res; |
| 4047 u32 y; | 4178 u32 y; |
| 4048 u64 x; | 4179 u64 x; |
| 4049 i64 v = pPKey2->aMem[0].u.i; | 4180 i64 v; |
| 4050 i64 lhs; | 4181 i64 lhs; |
| 4051 | 4182 |
| 4052 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); | 4183 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); |
| 4053 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); | 4184 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); |
| 4054 switch( serial_type ){ | 4185 switch( serial_type ){ |
| 4055 case 1: { /* 1-byte signed integer */ | 4186 case 1: { /* 1-byte signed integer */ |
| 4056 lhs = ONE_BYTE_INT(aKey); | 4187 lhs = ONE_BYTE_INT(aKey); |
| 4057 testcase( lhs<0 ); | 4188 testcase( lhs<0 ); |
| 4058 break; | 4189 break; |
| 4059 } | 4190 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4098 ** is contiguous) but does not cause any duplicate code to be generated | 4229 ** is contiguous) but does not cause any duplicate code to be generated |
| 4099 ** (as gcc is clever enough to combine the two like cases). Other | 4230 ** (as gcc is clever enough to combine the two like cases). Other |
| 4100 ** compilers might be similar. */ | 4231 ** compilers might be similar. */ |
| 4101 case 0: case 7: | 4232 case 0: case 7: |
| 4102 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); | 4233 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 4103 | 4234 |
| 4104 default: | 4235 default: |
| 4105 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); | 4236 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 4106 } | 4237 } |
| 4107 | 4238 |
| 4239 v = pPKey2->aMem[0].u.i; |
| 4108 if( v>lhs ){ | 4240 if( v>lhs ){ |
| 4109 res = pPKey2->r1; | 4241 res = pPKey2->r1; |
| 4110 }else if( v<lhs ){ | 4242 }else if( v<lhs ){ |
| 4111 res = pPKey2->r2; | 4243 res = pPKey2->r2; |
| 4112 }else if( pPKey2->nField>1 ){ | 4244 }else if( pPKey2->nField>1 ){ |
| 4113 /* The first fields of the two keys are equal. Compare the trailing | 4245 /* The first fields of the two keys are equal. Compare the trailing |
| 4114 ** fields. */ | 4246 ** fields. */ |
| 4115 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); | 4247 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 4116 }else{ | 4248 }else{ |
| 4117 /* The first fields of the two keys are equal and there are no trailing | 4249 /* The first fields of the two keys are equal and there are no trailing |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4244 u32 typeRowid; /* Serial type of the rowid */ | 4376 u32 typeRowid; /* Serial type of the rowid */ |
| 4245 u32 lenRowid; /* Size of the rowid */ | 4377 u32 lenRowid; /* Size of the rowid */ |
| 4246 Mem m, v; | 4378 Mem m, v; |
| 4247 | 4379 |
| 4248 /* Get the size of the index entry. Only indices entries of less | 4380 /* Get the size of the index entry. Only indices entries of less |
| 4249 ** than 2GiB are support - anything large must be database corruption. | 4381 ** than 2GiB are support - anything large must be database corruption. |
| 4250 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so | 4382 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so |
| 4251 ** this code can safely assume that nCellKey is 32-bits | 4383 ** this code can safely assume that nCellKey is 32-bits |
| 4252 */ | 4384 */ |
| 4253 assert( sqlite3BtreeCursorIsValid(pCur) ); | 4385 assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 4254 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); | 4386 nCellKey = sqlite3BtreePayloadSize(pCur); |
| 4255 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
| 4256 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); | 4387 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey ); |
| 4257 | 4388 |
| 4258 /* Read in the complete content of the index entry */ | 4389 /* Read in the complete content of the index entry */ |
| 4259 sqlite3VdbeMemInit(&m, db, 0); | 4390 sqlite3VdbeMemInit(&m, db, 0); |
| 4260 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); | 4391 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m); |
| 4261 if( rc ){ | 4392 if( rc ){ |
| 4262 return rc; | 4393 return rc; |
| 4263 } | 4394 } |
| 4264 | 4395 |
| 4265 /* The index entry must begin with a header size */ | 4396 /* The index entry must begin with a header size */ |
| 4266 (void)getVarint32((u8*)m.z, szHdr); | 4397 (void)getVarint32((u8*)m.z, szHdr); |
| 4267 testcase( szHdr==3 ); | 4398 testcase( szHdr==3 ); |
| 4268 testcase( szHdr==m.n ); | 4399 testcase( szHdr==m.n ); |
| 4269 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ | 4400 if( unlikely(szHdr<3 || (int)szHdr>m.n) ){ |
| 4270 goto idx_rowid_corruption; | 4401 goto idx_rowid_corruption; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4322 int *res /* Write the comparison result here */ | 4453 int *res /* Write the comparison result here */ |
| 4323 ){ | 4454 ){ |
| 4324 i64 nCellKey = 0; | 4455 i64 nCellKey = 0; |
| 4325 int rc; | 4456 int rc; |
| 4326 BtCursor *pCur; | 4457 BtCursor *pCur; |
| 4327 Mem m; | 4458 Mem m; |
| 4328 | 4459 |
| 4329 assert( pC->eCurType==CURTYPE_BTREE ); | 4460 assert( pC->eCurType==CURTYPE_BTREE ); |
| 4330 pCur = pC->uc.pCursor; | 4461 pCur = pC->uc.pCursor; |
| 4331 assert( sqlite3BtreeCursorIsValid(pCur) ); | 4462 assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 4332 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); | 4463 nCellKey = sqlite3BtreePayloadSize(pCur); |
| 4333 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | |
| 4334 /* nCellKey will always be between 0 and 0xffffffff because of the way | 4464 /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 4335 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ | 4465 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 4336 if( nCellKey<=0 || nCellKey>0x7fffffff ){ | 4466 if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 4337 *res = 0; | 4467 *res = 0; |
| 4338 return SQLITE_CORRUPT_BKPT; | 4468 return SQLITE_CORRUPT_BKPT; |
| 4339 } | 4469 } |
| 4340 sqlite3VdbeMemInit(&m, db, 0); | 4470 sqlite3VdbeMemInit(&m, db, 0); |
| 4341 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); | 4471 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, &m); |
| 4342 if( rc ){ | 4472 if( rc ){ |
| 4343 return rc; | 4473 return rc; |
| 4344 } | 4474 } |
| 4345 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); | 4475 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
| 4346 sqlite3VdbeMemRelease(&m); | 4476 sqlite3VdbeMemRelease(&m); |
| 4347 return SQLITE_OK; | 4477 return SQLITE_OK; |
| 4348 } | 4478 } |
| 4349 | 4479 |
| 4350 /* | 4480 /* |
| 4351 ** This routine sets the value to be returned by subsequent calls to | 4481 ** This routine sets the value to be returned by subsequent calls to |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4427 } | 4557 } |
| 4428 } | 4558 } |
| 4429 | 4559 |
| 4430 #ifndef SQLITE_OMIT_VIRTUALTABLE | 4560 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 4431 /* | 4561 /* |
| 4432 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored | 4562 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored |
| 4433 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored | 4563 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored |
| 4434 ** in memory obtained from sqlite3DbMalloc). | 4564 ** in memory obtained from sqlite3DbMalloc). |
| 4435 */ | 4565 */ |
| 4436 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ | 4566 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ |
| 4437 sqlite3 *db = p->db; | 4567 if( pVtab->zErrMsg ){ |
| 4438 sqlite3DbFree(db, p->zErrMsg); | 4568 sqlite3 *db = p->db; |
| 4439 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); | 4569 sqlite3DbFree(db, p->zErrMsg); |
| 4440 sqlite3_free(pVtab->zErrMsg); | 4570 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 4441 pVtab->zErrMsg = 0; | 4571 sqlite3_free(pVtab->zErrMsg); |
| 4572 pVtab->zErrMsg = 0; |
| 4573 } |
| 4442 } | 4574 } |
| 4443 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 4575 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 4576 |
| 4577 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 4578 |
| 4579 /* |
| 4580 ** If the second argument is not NULL, release any allocations associated |
| 4581 ** with the memory cells in the p->aMem[] array. Also free the UnpackedRecord |
| 4582 ** structure itself, using sqlite3DbFree(). |
| 4583 ** |
| 4584 ** This function is used to free UnpackedRecord structures allocated by |
| 4585 ** the vdbeUnpackRecord() function found in vdbeapi.c. |
| 4586 */ |
| 4587 static void vdbeFreeUnpacked(sqlite3 *db, int nField, UnpackedRecord *p){ |
| 4588 if( p ){ |
| 4589 int i; |
| 4590 for(i=0; i<nField; i++){ |
| 4591 Mem *pMem = &p->aMem[i]; |
| 4592 if( pMem->zMalloc ) sqlite3VdbeMemRelease(pMem); |
| 4593 } |
| 4594 sqlite3DbFree(db, p); |
| 4595 } |
| 4596 } |
| 4597 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| 4598 |
| 4599 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK |
| 4600 /* |
| 4601 ** Invoke the pre-update hook. If this is an UPDATE or DELETE pre-update call, |
| 4602 ** then cursor passed as the second argument should point to the row about |
| 4603 ** to be update or deleted. If the application calls sqlite3_preupdate_old(), |
| 4604 ** the required value will be read from the row the cursor points to. |
| 4605 */ |
| 4606 void sqlite3VdbePreUpdateHook( |
| 4607 Vdbe *v, /* Vdbe pre-update hook is invoked by */ |
| 4608 VdbeCursor *pCsr, /* Cursor to grab old.* values from */ |
| 4609 int op, /* SQLITE_INSERT, UPDATE or DELETE */ |
| 4610 const char *zDb, /* Database name */ |
| 4611 Table *pTab, /* Modified table */ |
| 4612 i64 iKey1, /* Initial key value */ |
| 4613 int iReg /* Register for new.* record */ |
| 4614 ){ |
| 4615 sqlite3 *db = v->db; |
| 4616 i64 iKey2; |
| 4617 PreUpdate preupdate; |
| 4618 const char *zTbl = pTab->zName; |
| 4619 static const u8 fakeSortOrder = 0; |
| 4620 |
| 4621 assert( db->pPreUpdate==0 ); |
| 4622 memset(&preupdate, 0, sizeof(PreUpdate)); |
| 4623 if( HasRowid(pTab)==0 ){ |
| 4624 iKey1 = iKey2 = 0; |
| 4625 preupdate.pPk = sqlite3PrimaryKeyIndex(pTab); |
| 4626 }else{ |
| 4627 if( op==SQLITE_UPDATE ){ |
| 4628 iKey2 = v->aMem[iReg].u.i; |
| 4629 }else{ |
| 4630 iKey2 = iKey1; |
| 4631 } |
| 4632 } |
| 4633 |
| 4634 assert( pCsr->nField==pTab->nCol |
| 4635 || (pCsr->nField==pTab->nCol+1 && op==SQLITE_DELETE && iReg==-1) |
| 4636 ); |
| 4637 |
| 4638 preupdate.v = v; |
| 4639 preupdate.pCsr = pCsr; |
| 4640 preupdate.op = op; |
| 4641 preupdate.iNewReg = iReg; |
| 4642 preupdate.keyinfo.db = db; |
| 4643 preupdate.keyinfo.enc = ENC(db); |
| 4644 preupdate.keyinfo.nField = pTab->nCol; |
| 4645 preupdate.keyinfo.aSortOrder = (u8*)&fakeSortOrder; |
| 4646 preupdate.iKey1 = iKey1; |
| 4647 preupdate.iKey2 = iKey2; |
| 4648 preupdate.pTab = pTab; |
| 4649 |
| 4650 db->pPreUpdate = &preupdate; |
| 4651 db->xPreUpdateCallback(db->pPreUpdateArg, db, op, zDb, zTbl, iKey1, iKey2); |
| 4652 db->pPreUpdate = 0; |
| 4653 sqlite3DbFree(db, preupdate.aRecord); |
| 4654 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pUnpacked); |
| 4655 vdbeFreeUnpacked(db, preupdate.keyinfo.nField+1, preupdate.pNewUnpacked); |
| 4656 if( preupdate.aNew ){ |
| 4657 int i; |
| 4658 for(i=0; i<pCsr->nField; i++){ |
| 4659 sqlite3VdbeMemRelease(&preupdate.aNew[i]); |
| 4660 } |
| 4661 sqlite3DbFree(db, preupdate.aNew); |
| 4662 } |
| 4663 } |
| 4664 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ |
| OLD | NEW |