| 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 ** |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 db->pVdbe->pPrev = p; | 28 db->pVdbe->pPrev = p; |
| 29 } | 29 } |
| 30 p->pNext = db->pVdbe; | 30 p->pNext = db->pVdbe; |
| 31 p->pPrev = 0; | 31 p->pPrev = 0; |
| 32 db->pVdbe = p; | 32 db->pVdbe = p; |
| 33 p->magic = VDBE_MAGIC_INIT; | 33 p->magic = VDBE_MAGIC_INIT; |
| 34 p->pParse = pParse; | 34 p->pParse = pParse; |
| 35 assert( pParse->aLabel==0 ); | 35 assert( pParse->aLabel==0 ); |
| 36 assert( pParse->nLabel==0 ); | 36 assert( pParse->nLabel==0 ); |
| 37 assert( pParse->nOpAlloc==0 ); | 37 assert( pParse->nOpAlloc==0 ); |
| 38 assert( pParse->szOpAlloc==0 ); |
| 38 return p; | 39 return p; |
| 39 } | 40 } |
| 40 | 41 |
| 41 /* | 42 /* |
| 43 ** Change the error string stored in Vdbe.zErrMsg |
| 44 */ |
| 45 void sqlite3VdbeError(Vdbe *p, const char *zFormat, ...){ |
| 46 va_list ap; |
| 47 sqlite3DbFree(p->db, p->zErrMsg); |
| 48 va_start(ap, zFormat); |
| 49 p->zErrMsg = sqlite3VMPrintf(p->db, zFormat, ap); |
| 50 va_end(ap); |
| 51 } |
| 52 |
| 53 /* |
| 42 ** Remember the SQL string for a prepared statement. | 54 ** Remember the SQL string for a prepared statement. |
| 43 */ | 55 */ |
| 44 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ | 56 void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){ |
| 45 assert( isPrepareV2==1 || isPrepareV2==0 ); | 57 assert( isPrepareV2==1 || isPrepareV2==0 ); |
| 46 if( p==0 ) return; | 58 if( p==0 ) return; |
| 47 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) | 59 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG) |
| 48 if( !isPrepareV2 ) return; | 60 if( !isPrepareV2 ) return; |
| 49 #endif | 61 #endif |
| 50 assert( p->zSql==0 ); | 62 assert( p->zSql==0 ); |
| 51 p->zSql = sqlite3DbStrNDup(p->db, z, n); | 63 p->zSql = sqlite3DbStrNDup(p->db, z, n); |
| 52 p->isPrepareV2 = (u8)isPrepareV2; | 64 p->isPrepareV2 = (u8)isPrepareV2; |
| 53 } | 65 } |
| 54 | 66 |
| 55 /* | 67 /* |
| 56 ** Return the SQL associated with a prepared statement | 68 ** Return the SQL associated with a prepared statement |
| 57 */ | 69 */ |
| 58 const char *sqlite3_sql(sqlite3_stmt *pStmt){ | 70 const char *sqlite3_sql(sqlite3_stmt *pStmt){ |
| 59 Vdbe *p = (Vdbe *)pStmt; | 71 Vdbe *p = (Vdbe *)pStmt; |
| 60 return (p && p->isPrepareV2) ? p->zSql : 0; | 72 return p ? p->zSql : 0; |
| 61 } | 73 } |
| 62 | 74 |
| 63 /* | 75 /* |
| 64 ** Swap all content between two VDBE structures. | 76 ** Swap all content between two VDBE structures. |
| 65 */ | 77 */ |
| 66 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ | 78 void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){ |
| 67 Vdbe tmp, *pTmp; | 79 Vdbe tmp, *pTmp; |
| 68 char *zTmp; | 80 char *zTmp; |
| 69 tmp = *pA; | 81 tmp = *pA; |
| 70 *pA = *pB; | 82 *pA = *pB; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); | 118 int nNew = (p->nOpAlloc>=512 ? p->nOpAlloc*2 : p->nOpAlloc+nOp); |
| 107 #else | 119 #else |
| 108 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); | 120 int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op))); |
| 109 UNUSED_PARAMETER(nOp); | 121 UNUSED_PARAMETER(nOp); |
| 110 #endif | 122 #endif |
| 111 | 123 |
| 112 assert( nOp<=(1024/sizeof(Op)) ); | 124 assert( nOp<=(1024/sizeof(Op)) ); |
| 113 assert( nNew>=(p->nOpAlloc+nOp) ); | 125 assert( nNew>=(p->nOpAlloc+nOp) ); |
| 114 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); | 126 pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op)); |
| 115 if( pNew ){ | 127 if( pNew ){ |
| 116 p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op); | 128 p->szOpAlloc = sqlite3DbMallocSize(p->db, pNew); |
| 129 p->nOpAlloc = p->szOpAlloc/sizeof(Op); |
| 117 v->aOp = pNew; | 130 v->aOp = pNew; |
| 118 } | 131 } |
| 119 return (pNew ? SQLITE_OK : SQLITE_NOMEM); | 132 return (pNew ? SQLITE_OK : SQLITE_NOMEM); |
| 120 } | 133 } |
| 121 | 134 |
| 122 #ifdef SQLITE_DEBUG | 135 #ifdef SQLITE_DEBUG |
| 123 /* This routine is just a convenient place to set a breakpoint that will | 136 /* This routine is just a convenient place to set a breakpoint that will |
| 124 ** fire after each opcode is inserted and displayed using | 137 ** fire after each opcode is inserted and displayed using |
| 125 ** "PRAGMA vdbe_addoptrace=on". | 138 ** "PRAGMA vdbe_addoptrace=on". |
| 126 */ | 139 */ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 139 ** p Pointer to the VDBE | 152 ** p Pointer to the VDBE |
| 140 ** | 153 ** |
| 141 ** op The opcode for this instruction | 154 ** op The opcode for this instruction |
| 142 ** | 155 ** |
| 143 ** p1, p2, p3 Operands | 156 ** p1, p2, p3 Operands |
| 144 ** | 157 ** |
| 145 ** Use the sqlite3VdbeResolveLabel() function to fix an address and | 158 ** Use the sqlite3VdbeResolveLabel() function to fix an address and |
| 146 ** the sqlite3VdbeChangeP4() function to change the value of the P4 | 159 ** the sqlite3VdbeChangeP4() function to change the value of the P4 |
| 147 ** operand. | 160 ** operand. |
| 148 */ | 161 */ |
| 162 static SQLITE_NOINLINE int growOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
| 163 assert( p->pParse->nOpAlloc<=p->nOp ); |
| 164 if( growOpArray(p, 1) ) return 1; |
| 165 assert( p->pParse->nOpAlloc>p->nOp ); |
| 166 return sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 167 } |
| 149 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ | 168 int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){ |
| 150 int i; | 169 int i; |
| 151 VdbeOp *pOp; | 170 VdbeOp *pOp; |
| 152 | 171 |
| 153 i = p->nOp; | 172 i = p->nOp; |
| 154 assert( p->magic==VDBE_MAGIC_INIT ); | 173 assert( p->magic==VDBE_MAGIC_INIT ); |
| 155 assert( op>0 && op<0xff ); | 174 assert( op>0 && op<0xff ); |
| 156 if( p->pParse->nOpAlloc<=i ){ | 175 if( p->pParse->nOpAlloc<=i ){ |
| 157 if( growOpArray(p, 1) ){ | 176 return growOp3(p, op, p1, p2, p3); |
| 158 return 1; | |
| 159 } | |
| 160 } | 177 } |
| 161 p->nOp++; | 178 p->nOp++; |
| 162 pOp = &p->aOp[i]; | 179 pOp = &p->aOp[i]; |
| 163 pOp->opcode = (u8)op; | 180 pOp->opcode = (u8)op; |
| 164 pOp->p5 = 0; | 181 pOp->p5 = 0; |
| 165 pOp->p1 = p1; | 182 pOp->p1 = p1; |
| 166 pOp->p2 = p2; | 183 pOp->p2 = p2; |
| 167 pOp->p3 = p3; | 184 pOp->p3 = p3; |
| 168 pOp->p4.p = 0; | 185 pOp->p4.p = 0; |
| 169 pOp->p4type = P4_NOTUSED; | 186 pOp->p4type = P4_NOTUSED; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 197 int sqlite3VdbeAddOp0(Vdbe *p, int op){ | 214 int sqlite3VdbeAddOp0(Vdbe *p, int op){ |
| 198 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); | 215 return sqlite3VdbeAddOp3(p, op, 0, 0, 0); |
| 199 } | 216 } |
| 200 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ | 217 int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){ |
| 201 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); | 218 return sqlite3VdbeAddOp3(p, op, p1, 0, 0); |
| 202 } | 219 } |
| 203 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ | 220 int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){ |
| 204 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); | 221 return sqlite3VdbeAddOp3(p, op, p1, p2, 0); |
| 205 } | 222 } |
| 206 | 223 |
| 224 /* Generate code for an unconditional jump to instruction iDest |
| 225 */ |
| 226 int sqlite3VdbeGoto(Vdbe *p, int iDest){ |
| 227 return sqlite3VdbeAddOp3(p, OP_Goto, 0, iDest, 0); |
| 228 } |
| 229 |
| 230 /* Generate code to cause the string zStr to be loaded into |
| 231 ** register iDest |
| 232 */ |
| 233 int sqlite3VdbeLoadString(Vdbe *p, int iDest, const char *zStr){ |
| 234 return sqlite3VdbeAddOp4(p, OP_String8, 0, iDest, 0, zStr, 0); |
| 235 } |
| 236 |
| 237 /* |
| 238 ** Generate code that initializes multiple registers to string or integer |
| 239 ** constants. The registers begin with iDest and increase consecutively. |
| 240 ** One register is initialized for each characgter in zTypes[]. For each |
| 241 ** "s" character in zTypes[], the register is a string if the argument is |
| 242 ** not NULL, or OP_Null if the value is a null pointer. For each "i" character |
| 243 ** in zTypes[], the register is initialized to an integer. |
| 244 */ |
| 245 void sqlite3VdbeMultiLoad(Vdbe *p, int iDest, const char *zTypes, ...){ |
| 246 va_list ap; |
| 247 int i; |
| 248 char c; |
| 249 va_start(ap, zTypes); |
| 250 for(i=0; (c = zTypes[i])!=0; i++){ |
| 251 if( c=='s' ){ |
| 252 const char *z = va_arg(ap, const char*); |
| 253 int addr = sqlite3VdbeAddOp2(p, z==0 ? OP_Null : OP_String8, 0, iDest++); |
| 254 if( z ) sqlite3VdbeChangeP4(p, addr, z, 0); |
| 255 }else{ |
| 256 assert( c=='i' ); |
| 257 sqlite3VdbeAddOp2(p, OP_Integer, va_arg(ap, int), iDest++); |
| 258 } |
| 259 } |
| 260 va_end(ap); |
| 261 } |
| 207 | 262 |
| 208 /* | 263 /* |
| 209 ** Add an opcode that includes the p4 value as a pointer. | 264 ** Add an opcode that includes the p4 value as a pointer. |
| 210 */ | 265 */ |
| 211 int sqlite3VdbeAddOp4( | 266 int sqlite3VdbeAddOp4( |
| 212 Vdbe *p, /* Add the opcode to this VM */ | 267 Vdbe *p, /* Add the opcode to this VM */ |
| 213 int op, /* The new opcode */ | 268 int op, /* The new opcode */ |
| 214 int p1, /* The P1 operand */ | 269 int p1, /* The P1 operand */ |
| 215 int p2, /* The P2 operand */ | 270 int p2, /* The P2 operand */ |
| 216 int p3, /* The P3 operand */ | 271 int p3, /* The P3 operand */ |
| 217 const char *zP4, /* The P4 operand */ | 272 const char *zP4, /* The P4 operand */ |
| 218 int p4type /* P4 operand type */ | 273 int p4type /* P4 operand type */ |
| 219 ){ | 274 ){ |
| 220 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); | 275 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3); |
| 221 sqlite3VdbeChangeP4(p, addr, zP4, p4type); | 276 sqlite3VdbeChangeP4(p, addr, zP4, p4type); |
| 222 return addr; | 277 return addr; |
| 223 } | 278 } |
| 224 | 279 |
| 225 /* | 280 /* |
| 281 ** Add an opcode that includes the p4 value with a P4_INT64 or |
| 282 ** P4_REAL type. |
| 283 */ |
| 284 int sqlite3VdbeAddOp4Dup8( |
| 285 Vdbe *p, /* Add the opcode to this VM */ |
| 286 int op, /* The new opcode */ |
| 287 int p1, /* The P1 operand */ |
| 288 int p2, /* The P2 operand */ |
| 289 int p3, /* The P3 operand */ |
| 290 const u8 *zP4, /* The P4 operand */ |
| 291 int p4type /* P4 operand type */ |
| 292 ){ |
| 293 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8); |
| 294 if( p4copy ) memcpy(p4copy, zP4, 8); |
| 295 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type); |
| 296 } |
| 297 |
| 298 /* |
| 226 ** Add an OP_ParseSchema opcode. This routine is broken out from | 299 ** Add an OP_ParseSchema opcode. This routine is broken out from |
| 227 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees | 300 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees |
| 228 ** as having been used. | 301 ** as having been used. |
| 229 ** | 302 ** |
| 230 ** The zWhere string must have been obtained from sqlite3_malloc(). | 303 ** The zWhere string must have been obtained from sqlite3_malloc(). |
| 231 ** This routine will take ownership of the allocated memory. | 304 ** This routine will take ownership of the allocated memory. |
| 232 */ | 305 */ |
| 233 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ | 306 void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){ |
| 234 int j; | 307 int j; |
| 235 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); | 308 int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 Parse *p = v->pParse; | 344 Parse *p = v->pParse; |
| 272 int i = p->nLabel++; | 345 int i = p->nLabel++; |
| 273 assert( v->magic==VDBE_MAGIC_INIT ); | 346 assert( v->magic==VDBE_MAGIC_INIT ); |
| 274 if( (i & (i-1))==0 ){ | 347 if( (i & (i-1))==0 ){ |
| 275 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, | 348 p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel, |
| 276 (i*2+1)*sizeof(p->aLabel[0])); | 349 (i*2+1)*sizeof(p->aLabel[0])); |
| 277 } | 350 } |
| 278 if( p->aLabel ){ | 351 if( p->aLabel ){ |
| 279 p->aLabel[i] = -1; | 352 p->aLabel[i] = -1; |
| 280 } | 353 } |
| 281 return -1-i; | 354 return ADDR(i); |
| 282 } | 355 } |
| 283 | 356 |
| 284 /* | 357 /* |
| 285 ** Resolve label "x" to be the address of the next instruction to | 358 ** Resolve label "x" to be the address of the next instruction to |
| 286 ** be inserted. The parameter "x" must have been obtained from | 359 ** be inserted. The parameter "x" must have been obtained from |
| 287 ** a prior call to sqlite3VdbeMakeLabel(). | 360 ** a prior call to sqlite3VdbeMakeLabel(). |
| 288 */ | 361 */ |
| 289 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ | 362 void sqlite3VdbeResolveLabel(Vdbe *v, int x){ |
| 290 Parse *p = v->pParse; | 363 Parse *p = v->pParse; |
| 291 int j = -1-x; | 364 int j = ADDR(x); |
| 292 assert( v->magic==VDBE_MAGIC_INIT ); | 365 assert( v->magic==VDBE_MAGIC_INIT ); |
| 293 assert( j<p->nLabel ); | 366 assert( j<p->nLabel ); |
| 294 if( ALWAYS(j>=0) && p->aLabel ){ | 367 assert( j>=0 ); |
| 368 if( p->aLabel ){ |
| 295 p->aLabel[j] = v->nOp; | 369 p->aLabel[j] = v->nOp; |
| 296 } | 370 } |
| 297 p->iFixedOp = v->nOp - 1; | 371 p->iFixedOp = v->nOp - 1; |
| 298 } | 372 } |
| 299 | 373 |
| 300 /* | 374 /* |
| 301 ** Mark the VDBE as one that can only be run one time. | 375 ** Mark the VDBE as one that can only be run one time. |
| 302 */ | 376 */ |
| 303 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ | 377 void sqlite3VdbeRunOnlyOnce(Vdbe *p){ |
| 304 p->runOnlyOnce = 1; | 378 p->runOnlyOnce = 1; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 ** throw an ABORT exception (causing the statement, but not entire transaction | 453 ** throw an ABORT exception (causing the statement, but not entire transaction |
| 380 ** to be rolled back). This condition is true if the main program or any | 454 ** to be rolled back). This condition is true if the main program or any |
| 381 ** sub-programs contains any of the following: | 455 ** sub-programs contains any of the following: |
| 382 ** | 456 ** |
| 383 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | 457 ** * OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 384 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. | 458 ** * OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort. |
| 385 ** * OP_Destroy | 459 ** * OP_Destroy |
| 386 ** * OP_VUpdate | 460 ** * OP_VUpdate |
| 387 ** * OP_VRename | 461 ** * OP_VRename |
| 388 ** * OP_FkCounter with P2==0 (immediate foreign key constraint) | 462 ** * OP_FkCounter with P2==0 (immediate foreign key constraint) |
| 463 ** * OP_CreateTable and OP_InitCoroutine (for CREATE TABLE AS SELECT ...) |
| 389 ** | 464 ** |
| 390 ** Then check that the value of Parse.mayAbort is true if an | 465 ** Then check that the value of Parse.mayAbort is true if an |
| 391 ** ABORT may be thrown, or false otherwise. Return true if it does | 466 ** ABORT may be thrown, or false otherwise. Return true if it does |
| 392 ** match, or false otherwise. This function is intended to be used as | 467 ** match, or false otherwise. This function is intended to be used as |
| 393 ** part of an assert statement in the compiler. Similar to: | 468 ** part of an assert statement in the compiler. Similar to: |
| 394 ** | 469 ** |
| 395 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); | 470 ** assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) ); |
| 396 */ | 471 */ |
| 397 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ | 472 int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){ |
| 398 int hasAbort = 0; | 473 int hasAbort = 0; |
| 474 int hasFkCounter = 0; |
| 475 int hasCreateTable = 0; |
| 476 int hasInitCoroutine = 0; |
| 399 Op *pOp; | 477 Op *pOp; |
| 400 VdbeOpIter sIter; | 478 VdbeOpIter sIter; |
| 401 memset(&sIter, 0, sizeof(sIter)); | 479 memset(&sIter, 0, sizeof(sIter)); |
| 402 sIter.v = v; | 480 sIter.v = v; |
| 403 | 481 |
| 404 while( (pOp = opIterNext(&sIter))!=0 ){ | 482 while( (pOp = opIterNext(&sIter))!=0 ){ |
| 405 int opcode = pOp->opcode; | 483 int opcode = pOp->opcode; |
| 406 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename | 484 if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename |
| 407 #ifndef SQLITE_OMIT_FOREIGN_KEY | |
| 408 || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1) | |
| 409 #endif | |
| 410 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) | 485 || ((opcode==OP_Halt || opcode==OP_HaltIfNull) |
| 411 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) | 486 && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort)) |
| 412 ){ | 487 ){ |
| 413 hasAbort = 1; | 488 hasAbort = 1; |
| 414 break; | 489 break; |
| 415 } | 490 } |
| 491 if( opcode==OP_CreateTable ) hasCreateTable = 1; |
| 492 if( opcode==OP_InitCoroutine ) hasInitCoroutine = 1; |
| 493 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 494 if( opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1 ){ |
| 495 hasFkCounter = 1; |
| 496 } |
| 497 #endif |
| 416 } | 498 } |
| 417 sqlite3DbFree(v->db, sIter.apSub); | 499 sqlite3DbFree(v->db, sIter.apSub); |
| 418 | 500 |
| 419 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. | 501 /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred. |
| 420 ** If malloc failed, then the while() loop above may not have iterated | 502 ** If malloc failed, then the while() loop above may not have iterated |
| 421 ** through all opcodes and hasAbort may be set incorrectly. Return | 503 ** through all opcodes and hasAbort may be set incorrectly. Return |
| 422 ** true for this case to prevent the assert() in the callers frame | 504 ** true for this case to prevent the assert() in the callers frame |
| 423 ** from failing. */ | 505 ** from failing. */ |
| 424 return ( v->db->mallocFailed || hasAbort==mayAbort ); | 506 return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter |
| 507 || (hasCreateTable && hasInitCoroutine) ); |
| 425 } | 508 } |
| 426 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ | 509 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */ |
| 427 | 510 |
| 428 /* | 511 /* |
| 429 ** Loop through the program looking for P2 values that are negative | 512 ** This routine is called after all opcodes have been inserted. It loops |
| 430 ** on jump instructions. Each such value is a label. Resolve the | 513 ** through all the opcodes and fixes up some details. |
| 431 ** label by setting the P2 value to its correct non-zero value. | |
| 432 ** | 514 ** |
| 433 ** This routine is called once after all opcodes have been inserted. | 515 ** (1) For each jump instruction with a negative P2 value (a label) |
| 516 ** resolve the P2 value to an actual address. |
| 434 ** | 517 ** |
| 435 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument | 518 ** (2) Compute the maximum number of arguments used by any SQL function |
| 436 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by | 519 ** and store that value in *pMaxFuncArgs. |
| 437 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array. | |
| 438 ** | 520 ** |
| 439 ** The Op.opflags field is set on all opcodes. | 521 ** (3) Update the Vdbe.readOnly and Vdbe.bIsReader flags to accurately |
| 522 ** indicate what the prepared statement actually does. |
| 523 ** |
| 524 ** (4) Initialize the p4.xAdvance pointer on opcodes that use it. |
| 525 ** |
| 526 ** (5) Reclaim the memory allocated for storing labels. |
| 440 */ | 527 */ |
| 441 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ | 528 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){ |
| 442 int i; | 529 int i; |
| 443 int nMaxArgs = *pMaxFuncArgs; | 530 int nMaxArgs = *pMaxFuncArgs; |
| 444 Op *pOp; | 531 Op *pOp; |
| 445 Parse *pParse = p->pParse; | 532 Parse *pParse = p->pParse; |
| 446 int *aLabel = pParse->aLabel; | 533 int *aLabel = pParse->aLabel; |
| 447 p->readOnly = 1; | 534 p->readOnly = 1; |
| 448 p->bIsReader = 0; | 535 p->bIsReader = 0; |
| 449 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ | 536 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){ |
| 450 u8 opcode = pOp->opcode; | 537 u8 opcode = pOp->opcode; |
| 451 | 538 |
| 452 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing | 539 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing |
| 453 ** cases from this switch! */ | 540 ** cases from this switch! */ |
| 454 switch( opcode ){ | 541 switch( opcode ){ |
| 455 case OP_Function: | |
| 456 case OP_AggStep: { | |
| 457 if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5; | |
| 458 break; | |
| 459 } | |
| 460 case OP_Transaction: { | 542 case OP_Transaction: { |
| 461 if( pOp->p2!=0 ) p->readOnly = 0; | 543 if( pOp->p2!=0 ) p->readOnly = 0; |
| 462 /* fall thru */ | 544 /* fall thru */ |
| 463 } | 545 } |
| 464 case OP_AutoCommit: | 546 case OP_AutoCommit: |
| 465 case OP_Savepoint: { | 547 case OP_Savepoint: { |
| 466 p->bIsReader = 1; | 548 p->bIsReader = 1; |
| 467 break; | 549 break; |
| 468 } | 550 } |
| 469 #ifndef SQLITE_OMIT_WAL | 551 #ifndef SQLITE_OMIT_WAL |
| (...skipping 29 matching lines...) Expand all Loading... |
| 499 case OP_Prev: | 581 case OP_Prev: |
| 500 case OP_PrevIfOpen: { | 582 case OP_PrevIfOpen: { |
| 501 pOp->p4.xAdvance = sqlite3BtreePrevious; | 583 pOp->p4.xAdvance = sqlite3BtreePrevious; |
| 502 pOp->p4type = P4_ADVANCE; | 584 pOp->p4type = P4_ADVANCE; |
| 503 break; | 585 break; |
| 504 } | 586 } |
| 505 } | 587 } |
| 506 | 588 |
| 507 pOp->opflags = sqlite3OpcodeProperty[opcode]; | 589 pOp->opflags = sqlite3OpcodeProperty[opcode]; |
| 508 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ | 590 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){ |
| 509 assert( -1-pOp->p2<pParse->nLabel ); | 591 assert( ADDR(pOp->p2)<pParse->nLabel ); |
| 510 pOp->p2 = aLabel[-1-pOp->p2]; | 592 pOp->p2 = aLabel[ADDR(pOp->p2)]; |
| 511 } | 593 } |
| 512 } | 594 } |
| 513 sqlite3DbFree(p->db, pParse->aLabel); | 595 sqlite3DbFree(p->db, pParse->aLabel); |
| 514 pParse->aLabel = 0; | 596 pParse->aLabel = 0; |
| 515 pParse->nLabel = 0; | 597 pParse->nLabel = 0; |
| 516 *pMaxFuncArgs = nMaxArgs; | 598 *pMaxFuncArgs = nMaxArgs; |
| 517 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); | 599 assert( p->bIsReader!=0 || DbMaskAllZero(p->btreeMask) ); |
| 518 } | 600 } |
| 519 | 601 |
| 520 /* | 602 /* |
| (...skipping 26 matching lines...) Expand all Loading... |
| 547 *pnOp = p->nOp; | 629 *pnOp = p->nOp; |
| 548 p->aOp = 0; | 630 p->aOp = 0; |
| 549 return aOp; | 631 return aOp; |
| 550 } | 632 } |
| 551 | 633 |
| 552 /* | 634 /* |
| 553 ** Add a whole list of operations to the operation stack. Return the | 635 ** Add a whole list of operations to the operation stack. Return the |
| 554 ** address of the first operation added. | 636 ** address of the first operation added. |
| 555 */ | 637 */ |
| 556 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ | 638 int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){ |
| 557 int addr; | 639 int addr, i; |
| 640 VdbeOp *pOut; |
| 641 assert( nOp>0 ); |
| 558 assert( p->magic==VDBE_MAGIC_INIT ); | 642 assert( p->magic==VDBE_MAGIC_INIT ); |
| 559 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ | 643 if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p, nOp) ){ |
| 560 return 0; | 644 return 0; |
| 561 } | 645 } |
| 562 addr = p->nOp; | 646 addr = p->nOp; |
| 563 if( ALWAYS(nOp>0) ){ | 647 pOut = &p->aOp[addr]; |
| 564 int i; | 648 for(i=0; i<nOp; i++, aOp++, pOut++){ |
| 565 VdbeOpList const *pIn = aOp; | 649 pOut->opcode = aOp->opcode; |
| 566 for(i=0; i<nOp; i++, pIn++){ | 650 pOut->p1 = aOp->p1; |
| 567 int p2 = pIn->p2; | 651 pOut->p2 = aOp->p2; |
| 568 VdbeOp *pOut = &p->aOp[i+addr]; | 652 assert( aOp->p2>=0 ); |
| 569 pOut->opcode = pIn->opcode; | 653 pOut->p3 = aOp->p3; |
| 570 pOut->p1 = pIn->p1; | 654 pOut->p4type = P4_NOTUSED; |
| 571 if( p2<0 ){ | 655 pOut->p4.p = 0; |
| 572 assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP ); | 656 pOut->p5 = 0; |
| 573 pOut->p2 = addr + ADDR(p2); | |
| 574 }else{ | |
| 575 pOut->p2 = p2; | |
| 576 } | |
| 577 pOut->p3 = pIn->p3; | |
| 578 pOut->p4type = P4_NOTUSED; | |
| 579 pOut->p4.p = 0; | |
| 580 pOut->p5 = 0; | |
| 581 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS | 657 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 582 pOut->zComment = 0; | 658 pOut->zComment = 0; |
| 583 #endif | 659 #endif |
| 584 #ifdef SQLITE_VDBE_COVERAGE | 660 #ifdef SQLITE_VDBE_COVERAGE |
| 585 pOut->iSrcLine = iLineno+i; | 661 pOut->iSrcLine = iLineno+i; |
| 586 #else | 662 #else |
| 587 (void)iLineno; | 663 (void)iLineno; |
| 588 #endif | 664 #endif |
| 589 #ifdef SQLITE_DEBUG | 665 #ifdef SQLITE_DEBUG |
| 590 if( p->db->flags & SQLITE_VdbeAddopTrace ){ | 666 if( p->db->flags & SQLITE_VdbeAddopTrace ){ |
| 591 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); | 667 sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]); |
| 592 } | 668 } |
| 593 #endif | 669 #endif |
| 594 } | |
| 595 p->nOp += nOp; | |
| 596 } | 670 } |
| 671 p->nOp += nOp; |
| 597 return addr; | 672 return addr; |
| 598 } | 673 } |
| 599 | 674 |
| 675 #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
| 600 /* | 676 /* |
| 601 ** Change the value of the P1 operand for a specific instruction. | 677 ** Add an entry to the array of counters managed by sqlite3_stmt_scanstatus(). |
| 602 ** This routine is useful when a large program is loaded from a | |
| 603 ** static array using sqlite3VdbeAddOpList but we want to make a | |
| 604 ** few minor changes to the program. | |
| 605 */ | 678 */ |
| 679 void sqlite3VdbeScanStatus( |
| 680 Vdbe *p, /* VM to add scanstatus() to */ |
| 681 int addrExplain, /* Address of OP_Explain (or 0) */ |
| 682 int addrLoop, /* Address of loop counter */ |
| 683 int addrVisit, /* Address of rows visited counter */ |
| 684 LogEst nEst, /* Estimated number of output rows */ |
| 685 const char *zName /* Name of table or index being scanned */ |
| 686 ){ |
| 687 int nByte = (p->nScan+1) * sizeof(ScanStatus); |
| 688 ScanStatus *aNew; |
| 689 aNew = (ScanStatus*)sqlite3DbRealloc(p->db, p->aScan, nByte); |
| 690 if( aNew ){ |
| 691 ScanStatus *pNew = &aNew[p->nScan++]; |
| 692 pNew->addrExplain = addrExplain; |
| 693 pNew->addrLoop = addrLoop; |
| 694 pNew->addrVisit = addrVisit; |
| 695 pNew->nEst = nEst; |
| 696 pNew->zName = sqlite3DbStrDup(p->db, zName); |
| 697 p->aScan = aNew; |
| 698 } |
| 699 } |
| 700 #endif |
| 701 |
| 702 |
| 703 /* |
| 704 ** Change the value of the opcode, or P1, P2, P3, or P5 operands |
| 705 ** for a specific instruction. |
| 706 */ |
| 707 void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){ |
| 708 sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode; |
| 709 } |
| 606 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ | 710 void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){ |
| 607 assert( p!=0 ); | 711 sqlite3VdbeGetOp(p,addr)->p1 = val; |
| 608 if( ((u32)p->nOp)>addr ){ | 712 } |
| 609 p->aOp[addr].p1 = val; | 713 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ |
| 610 } | 714 sqlite3VdbeGetOp(p,addr)->p2 = val; |
| 715 } |
| 716 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ |
| 717 sqlite3VdbeGetOp(p,addr)->p3 = val; |
| 718 } |
| 719 void sqlite3VdbeChangeP5(Vdbe *p, u8 p5){ |
| 720 sqlite3VdbeGetOp(p,-1)->p5 = p5; |
| 611 } | 721 } |
| 612 | 722 |
| 613 /* | 723 /* |
| 614 ** Change the value of the P2 operand for a specific instruction. | |
| 615 ** This routine is useful for setting a jump destination. | |
| 616 */ | |
| 617 void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){ | |
| 618 assert( p!=0 ); | |
| 619 if( ((u32)p->nOp)>addr ){ | |
| 620 p->aOp[addr].p2 = val; | |
| 621 } | |
| 622 } | |
| 623 | |
| 624 /* | |
| 625 ** Change the value of the P3 operand for a specific instruction. | |
| 626 */ | |
| 627 void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){ | |
| 628 assert( p!=0 ); | |
| 629 if( ((u32)p->nOp)>addr ){ | |
| 630 p->aOp[addr].p3 = val; | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 /* | |
| 635 ** Change the value of the P5 operand for the most recently | |
| 636 ** added operation. | |
| 637 */ | |
| 638 void sqlite3VdbeChangeP5(Vdbe *p, u8 val){ | |
| 639 assert( p!=0 ); | |
| 640 if( p->aOp ){ | |
| 641 assert( p->nOp>0 ); | |
| 642 p->aOp[p->nOp-1].p5 = val; | |
| 643 } | |
| 644 } | |
| 645 | |
| 646 /* | |
| 647 ** Change the P2 operand of instruction addr so that it points to | 724 ** Change the P2 operand of instruction addr so that it points to |
| 648 ** the address of the next instruction to be coded. | 725 ** the address of the next instruction to be coded. |
| 649 */ | 726 */ |
| 650 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ | 727 void sqlite3VdbeJumpHere(Vdbe *p, int addr){ |
| 728 p->pParse->iFixedOp = p->nOp - 1; |
| 651 sqlite3VdbeChangeP2(p, addr, p->nOp); | 729 sqlite3VdbeChangeP2(p, addr, p->nOp); |
| 652 p->pParse->iFixedOp = p->nOp - 1; | |
| 653 } | 730 } |
| 654 | 731 |
| 655 | 732 |
| 656 /* | 733 /* |
| 657 ** If the input FuncDef structure is ephemeral, then free it. If | 734 ** If the input FuncDef structure is ephemeral, then free it. If |
| 658 ** the FuncDef is not ephermal, then do nothing. | 735 ** the FuncDef is not ephermal, then do nothing. |
| 659 */ | 736 */ |
| 660 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ | 737 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){ |
| 661 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ | 738 if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){ |
| 662 sqlite3DbFree(db, pDef); | 739 sqlite3DbFree(db, pDef); |
| 663 } | 740 } |
| 664 } | 741 } |
| 665 | 742 |
| 666 static void vdbeFreeOpArray(sqlite3 *, Op *, int); | 743 static void vdbeFreeOpArray(sqlite3 *, Op *, int); |
| 667 | 744 |
| 668 /* | 745 /* |
| 669 ** Delete a P4 value if necessary. | 746 ** Delete a P4 value if necessary. |
| 670 */ | 747 */ |
| 671 static void freeP4(sqlite3 *db, int p4type, void *p4){ | 748 static void freeP4(sqlite3 *db, int p4type, void *p4){ |
| 672 if( p4 ){ | 749 if( p4 ){ |
| 673 assert( db ); | 750 assert( db ); |
| 674 switch( p4type ){ | 751 switch( p4type ){ |
| 752 case P4_FUNCCTX: { |
| 753 freeEphemeralFunction(db, ((sqlite3_context*)p4)->pFunc); |
| 754 /* Fall through into the next case */ |
| 755 } |
| 675 case P4_REAL: | 756 case P4_REAL: |
| 676 case P4_INT64: | 757 case P4_INT64: |
| 677 case P4_DYNAMIC: | 758 case P4_DYNAMIC: |
| 678 case P4_INTARRAY: { | 759 case P4_INTARRAY: { |
| 679 sqlite3DbFree(db, p4); | 760 sqlite3DbFree(db, p4); |
| 680 break; | 761 break; |
| 681 } | 762 } |
| 682 case P4_KEYINFO: { | 763 case P4_KEYINFO: { |
| 683 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); | 764 if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4); |
| 684 break; | 765 break; |
| 685 } | 766 } |
| 767 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 768 case P4_EXPR: { |
| 769 sqlite3ExprDelete(db, (Expr*)p4); |
| 770 break; |
| 771 } |
| 772 #endif |
| 686 case P4_MPRINTF: { | 773 case P4_MPRINTF: { |
| 687 if( db->pnBytesFreed==0 ) sqlite3_free(p4); | 774 if( db->pnBytesFreed==0 ) sqlite3_free(p4); |
| 688 break; | 775 break; |
| 689 } | 776 } |
| 690 case P4_FUNCDEF: { | 777 case P4_FUNCDEF: { |
| 691 freeEphemeralFunction(db, (FuncDef*)p4); | 778 freeEphemeralFunction(db, (FuncDef*)p4); |
| 692 break; | 779 break; |
| 693 } | 780 } |
| 694 case P4_MEM: { | 781 case P4_MEM: { |
| 695 if( db->pnBytesFreed==0 ){ | 782 if( db->pnBytesFreed==0 ){ |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 /* | 827 /* |
| 741 ** Change the opcode at addr into OP_Noop | 828 ** Change the opcode at addr into OP_Noop |
| 742 */ | 829 */ |
| 743 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ | 830 void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){ |
| 744 if( addr<p->nOp ){ | 831 if( addr<p->nOp ){ |
| 745 VdbeOp *pOp = &p->aOp[addr]; | 832 VdbeOp *pOp = &p->aOp[addr]; |
| 746 sqlite3 *db = p->db; | 833 sqlite3 *db = p->db; |
| 747 freeP4(db, pOp->p4type, pOp->p4.p); | 834 freeP4(db, pOp->p4type, pOp->p4.p); |
| 748 memset(pOp, 0, sizeof(pOp[0])); | 835 memset(pOp, 0, sizeof(pOp[0])); |
| 749 pOp->opcode = OP_Noop; | 836 pOp->opcode = OP_Noop; |
| 750 if( addr==p->nOp-1 ) p->nOp--; | |
| 751 } | 837 } |
| 752 } | 838 } |
| 753 | 839 |
| 754 /* | 840 /* |
| 755 ** If the last opcode is "op" and it is not a jump destination, | 841 ** If the last opcode is "op" and it is not a jump destination, |
| 756 ** then remove it. Return true if and only if an opcode was removed. | 842 ** then remove it. Return true if and only if an opcode was removed. |
| 757 */ | 843 */ |
| 758 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ | 844 int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){ |
| 759 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ | 845 if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){ |
| 760 sqlite3VdbeChangeToNoop(p, p->nOp-1); | 846 sqlite3VdbeChangeToNoop(p, p->nOp-1); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 /* Note: this cast is safe, because the origin data point was an int | 894 /* Note: this cast is safe, because the origin data point was an int |
| 809 ** that was cast to a (const char *). */ | 895 ** that was cast to a (const char *). */ |
| 810 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); | 896 pOp->p4.i = SQLITE_PTR_TO_INT(zP4); |
| 811 pOp->p4type = P4_INT32; | 897 pOp->p4type = P4_INT32; |
| 812 }else if( zP4==0 ){ | 898 }else if( zP4==0 ){ |
| 813 pOp->p4.p = 0; | 899 pOp->p4.p = 0; |
| 814 pOp->p4type = P4_NOTUSED; | 900 pOp->p4type = P4_NOTUSED; |
| 815 }else if( n==P4_KEYINFO ){ | 901 }else if( n==P4_KEYINFO ){ |
| 816 pOp->p4.p = (void*)zP4; | 902 pOp->p4.p = (void*)zP4; |
| 817 pOp->p4type = P4_KEYINFO; | 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 |
| 818 }else if( n==P4_VTAB ){ | 913 }else if( n==P4_VTAB ){ |
| 819 pOp->p4.p = (void*)zP4; | 914 pOp->p4.p = (void*)zP4; |
| 820 pOp->p4type = P4_VTAB; | 915 pOp->p4type = P4_VTAB; |
| 821 sqlite3VtabLock((VTable *)zP4); | 916 sqlite3VtabLock((VTable *)zP4); |
| 822 assert( ((VTable *)zP4)->db==p->db ); | 917 assert( ((VTable *)zP4)->db==p->db ); |
| 823 }else if( n<0 ){ | 918 }else if( n<0 ){ |
| 824 pOp->p4.p = (void*)zP4; | 919 pOp->p4.p = (void*)zP4; |
| 825 pOp->p4type = (signed char)n; | 920 pOp->p4type = (signed char)n; |
| 826 }else{ | 921 }else{ |
| 827 if( n==0 ) n = sqlite3Strlen30(zP4); | 922 if( n==0 ) n = sqlite3Strlen30(zP4); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); | 1093 sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment); |
| 999 jj = sqlite3Strlen30(zTemp); | 1094 jj = sqlite3Strlen30(zTemp); |
| 1000 }else{ | 1095 }else{ |
| 1001 zTemp[0] = 0; | 1096 zTemp[0] = 0; |
| 1002 jj = 0; | 1097 jj = 0; |
| 1003 } | 1098 } |
| 1004 return jj; | 1099 return jj; |
| 1005 } | 1100 } |
| 1006 #endif /* SQLITE_DEBUG */ | 1101 #endif /* SQLITE_DEBUG */ |
| 1007 | 1102 |
| 1103 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) |
| 1104 /* |
| 1105 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text |
| 1106 ** that can be displayed in the P4 column of EXPLAIN output. |
| 1107 */ |
| 1108 static int displayP4Expr(int nTemp, char *zTemp, Expr *pExpr){ |
| 1109 const char *zOp = 0; |
| 1110 int n; |
| 1111 switch( pExpr->op ){ |
| 1112 case TK_STRING: |
| 1113 sqlite3_snprintf(nTemp, zTemp, "%Q", pExpr->u.zToken); |
| 1114 break; |
| 1115 case TK_INTEGER: |
| 1116 sqlite3_snprintf(nTemp, zTemp, "%d", pExpr->u.iValue); |
| 1117 break; |
| 1118 case TK_NULL: |
| 1119 sqlite3_snprintf(nTemp, zTemp, "NULL"); |
| 1120 break; |
| 1121 case TK_REGISTER: { |
| 1122 sqlite3_snprintf(nTemp, zTemp, "r[%d]", pExpr->iTable); |
| 1123 break; |
| 1124 } |
| 1125 case TK_COLUMN: { |
| 1126 if( pExpr->iColumn<0 ){ |
| 1127 sqlite3_snprintf(nTemp, zTemp, "rowid"); |
| 1128 }else{ |
| 1129 sqlite3_snprintf(nTemp, zTemp, "c%d", (int)pExpr->iColumn); |
| 1130 } |
| 1131 break; |
| 1132 } |
| 1133 case TK_LT: zOp = "LT"; break; |
| 1134 case TK_LE: zOp = "LE"; break; |
| 1135 case TK_GT: zOp = "GT"; break; |
| 1136 case TK_GE: zOp = "GE"; break; |
| 1137 case TK_NE: zOp = "NE"; break; |
| 1138 case TK_EQ: zOp = "EQ"; break; |
| 1139 case TK_IS: zOp = "IS"; break; |
| 1140 case TK_ISNOT: zOp = "ISNOT"; break; |
| 1141 case TK_AND: zOp = "AND"; break; |
| 1142 case TK_OR: zOp = "OR"; break; |
| 1143 case TK_PLUS: zOp = "ADD"; break; |
| 1144 case TK_STAR: zOp = "MUL"; break; |
| 1145 case TK_MINUS: zOp = "SUB"; break; |
| 1146 case TK_REM: zOp = "REM"; break; |
| 1147 case TK_BITAND: zOp = "BITAND"; break; |
| 1148 case TK_BITOR: zOp = "BITOR"; break; |
| 1149 case TK_SLASH: zOp = "DIV"; break; |
| 1150 case TK_LSHIFT: zOp = "LSHIFT"; break; |
| 1151 case TK_RSHIFT: zOp = "RSHIFT"; break; |
| 1152 case TK_CONCAT: zOp = "CONCAT"; break; |
| 1153 case TK_UMINUS: zOp = "MINUS"; break; |
| 1154 case TK_UPLUS: zOp = "PLUS"; break; |
| 1155 case TK_BITNOT: zOp = "BITNOT"; break; |
| 1156 case TK_NOT: zOp = "NOT"; break; |
| 1157 case TK_ISNULL: zOp = "ISNULL"; break; |
| 1158 case TK_NOTNULL: zOp = "NOTNULL"; break; |
| 1008 | 1159 |
| 1009 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \ | 1160 default: |
| 1010 || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | 1161 sqlite3_snprintf(nTemp, zTemp, "%s", "expr"); |
| 1162 break; |
| 1163 } |
| 1164 |
| 1165 if( zOp ){ |
| 1166 sqlite3_snprintf(nTemp, zTemp, "%s(", zOp); |
| 1167 n = sqlite3Strlen30(zTemp); |
| 1168 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pLeft); |
| 1169 if( n<nTemp-1 && pExpr->pRight ){ |
| 1170 zTemp[n++] = ','; |
| 1171 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pRight); |
| 1172 } |
| 1173 sqlite3_snprintf(nTemp-n, zTemp+n, ")"); |
| 1174 } |
| 1175 return sqlite3Strlen30(zTemp); |
| 1176 } |
| 1177 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */ |
| 1178 |
| 1179 |
| 1180 #if VDBE_DISPLAY_P4 |
| 1011 /* | 1181 /* |
| 1012 ** Compute a string that describes the P4 parameter for an opcode. | 1182 ** Compute a string that describes the P4 parameter for an opcode. |
| 1013 ** Use zTemp for any required temporary buffer space. | 1183 ** Use zTemp for any required temporary buffer space. |
| 1014 */ | 1184 */ |
| 1015 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ | 1185 static char *displayP4(Op *pOp, char *zTemp, int nTemp){ |
| 1016 char *zP4 = zTemp; | 1186 char *zP4 = zTemp; |
| 1017 assert( nTemp>=20 ); | 1187 assert( nTemp>=20 ); |
| 1018 switch( pOp->p4type ){ | 1188 switch( pOp->p4type ){ |
| 1019 case P4_KEYINFO: { | 1189 case P4_KEYINFO: { |
| 1020 int i, j; | 1190 int i, j; |
| 1021 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; | 1191 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo; |
| 1022 assert( pKeyInfo->aSortOrder!=0 ); | 1192 assert( pKeyInfo->aSortOrder!=0 ); |
| 1023 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); | 1193 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField); |
| 1024 i = sqlite3Strlen30(zTemp); | 1194 i = sqlite3Strlen30(zTemp); |
| 1025 for(j=0; j<pKeyInfo->nField; j++){ | 1195 for(j=0; j<pKeyInfo->nField; j++){ |
| 1026 CollSeq *pColl = pKeyInfo->aColl[j]; | 1196 CollSeq *pColl = pKeyInfo->aColl[j]; |
| 1027 const char *zColl = pColl ? pColl->zName : "nil"; | 1197 const char *zColl = pColl ? pColl->zName : "nil"; |
| 1028 int n = sqlite3Strlen30(zColl); | 1198 int n = sqlite3Strlen30(zColl); |
| 1029 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ | 1199 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){ |
| 1030 zColl = "B"; | 1200 zColl = "B"; |
| 1031 n = 1; | 1201 n = 1; |
| 1032 } | 1202 } |
| 1033 if( i+n>nTemp-6 ){ | 1203 if( i+n>nTemp-7 ){ |
| 1034 memcpy(&zTemp[i],",...",4); | 1204 memcpy(&zTemp[i],",...",4); |
| 1205 i += 4; |
| 1035 break; | 1206 break; |
| 1036 } | 1207 } |
| 1037 zTemp[i++] = ','; | 1208 zTemp[i++] = ','; |
| 1038 if( pKeyInfo->aSortOrder[j] ){ | 1209 if( pKeyInfo->aSortOrder[j] ){ |
| 1039 zTemp[i++] = '-'; | 1210 zTemp[i++] = '-'; |
| 1040 } | 1211 } |
| 1041 memcpy(&zTemp[i], zColl, n+1); | 1212 memcpy(&zTemp[i], zColl, n+1); |
| 1042 i += n; | 1213 i += n; |
| 1043 } | 1214 } |
| 1044 zTemp[i++] = ')'; | 1215 zTemp[i++] = ')'; |
| 1045 zTemp[i] = 0; | 1216 zTemp[i] = 0; |
| 1046 assert( i<nTemp ); | 1217 assert( i<nTemp ); |
| 1047 break; | 1218 break; |
| 1048 } | 1219 } |
| 1220 #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 1221 case P4_EXPR: { |
| 1222 displayP4Expr(nTemp, zTemp, pOp->p4.pExpr); |
| 1223 break; |
| 1224 } |
| 1225 #endif |
| 1049 case P4_COLLSEQ: { | 1226 case P4_COLLSEQ: { |
| 1050 CollSeq *pColl = pOp->p4.pColl; | 1227 CollSeq *pColl = pOp->p4.pColl; |
| 1051 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); | 1228 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName); |
| 1052 break; | 1229 break; |
| 1053 } | 1230 } |
| 1054 case P4_FUNCDEF: { | 1231 case P4_FUNCDEF: { |
| 1055 FuncDef *pDef = pOp->p4.pFunc; | 1232 FuncDef *pDef = pOp->p4.pFunc; |
| 1056 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); | 1233 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
| 1057 break; | 1234 break; |
| 1058 } | 1235 } |
| 1236 #ifdef SQLITE_DEBUG |
| 1237 case P4_FUNCCTX: { |
| 1238 FuncDef *pDef = pOp->p4.pCtx->pFunc; |
| 1239 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg); |
| 1240 break; |
| 1241 } |
| 1242 #endif |
| 1059 case P4_INT64: { | 1243 case P4_INT64: { |
| 1060 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); | 1244 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64); |
| 1061 break; | 1245 break; |
| 1062 } | 1246 } |
| 1063 case P4_INT32: { | 1247 case P4_INT32: { |
| 1064 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); | 1248 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i); |
| 1065 break; | 1249 break; |
| 1066 } | 1250 } |
| 1067 case P4_REAL: { | 1251 case P4_REAL: { |
| 1068 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); | 1252 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1080 sqlite3_snprintf(nTemp, zTemp, "NULL"); | 1264 sqlite3_snprintf(nTemp, zTemp, "NULL"); |
| 1081 }else{ | 1265 }else{ |
| 1082 assert( pMem->flags & MEM_Blob ); | 1266 assert( pMem->flags & MEM_Blob ); |
| 1083 zP4 = "(blob)"; | 1267 zP4 = "(blob)"; |
| 1084 } | 1268 } |
| 1085 break; | 1269 break; |
| 1086 } | 1270 } |
| 1087 #ifndef SQLITE_OMIT_VIRTUALTABLE | 1271 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1088 case P4_VTAB: { | 1272 case P4_VTAB: { |
| 1089 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; | 1273 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab; |
| 1090 sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule); | 1274 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab); |
| 1091 break; | 1275 break; |
| 1092 } | 1276 } |
| 1093 #endif | 1277 #endif |
| 1094 case P4_INTARRAY: { | 1278 case P4_INTARRAY: { |
| 1095 sqlite3_snprintf(nTemp, zTemp, "intarray"); | 1279 sqlite3_snprintf(nTemp, zTemp, "intarray"); |
| 1096 break; | 1280 break; |
| 1097 } | 1281 } |
| 1098 case P4_SUBPROGRAM: { | 1282 case P4_SUBPROGRAM: { |
| 1099 sqlite3_snprintf(nTemp, zTemp, "program"); | 1283 sqlite3_snprintf(nTemp, zTemp, "program"); |
| 1100 break; | 1284 break; |
| 1101 } | 1285 } |
| 1102 case P4_ADVANCE: { | 1286 case P4_ADVANCE: { |
| 1103 zTemp[0] = 0; | 1287 zTemp[0] = 0; |
| 1104 break; | 1288 break; |
| 1105 } | 1289 } |
| 1106 default: { | 1290 default: { |
| 1107 zP4 = pOp->p4.z; | 1291 zP4 = pOp->p4.z; |
| 1108 if( zP4==0 ){ | 1292 if( zP4==0 ){ |
| 1109 zP4 = zTemp; | 1293 zP4 = zTemp; |
| 1110 zTemp[0] = 0; | 1294 zTemp[0] = 0; |
| 1111 } | 1295 } |
| 1112 } | 1296 } |
| 1113 } | 1297 } |
| 1114 assert( zP4!=0 ); | 1298 assert( zP4!=0 ); |
| 1115 return zP4; | 1299 return zP4; |
| 1116 } | 1300 } |
| 1117 #endif | 1301 #endif /* VDBE_DISPLAY_P4 */ |
| 1118 | 1302 |
| 1119 /* | 1303 /* |
| 1120 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. | 1304 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used. |
| 1121 ** | 1305 ** |
| 1122 ** The prepared statements need to know in advance the complete set of | 1306 ** The prepared statements need to know in advance the complete set of |
| 1123 ** attached databases that will be use. A mask of these databases | 1307 ** attached databases that will be use. A mask of these databases |
| 1124 ** is maintained in p->btreeMask. The p->lockMask value is the subset of | 1308 ** is maintained in p->btreeMask. The p->lockMask value is the subset of |
| 1125 ** p->btreeMask of databases that will require a lock. | 1309 ** p->btreeMask of databases that will require a lock. |
| 1126 */ | 1310 */ |
| 1127 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ | 1311 void sqlite3VdbeUsesBtree(Vdbe *p, int i){ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 sqlite3BtreeEnter(aDb[i].pBt); | 1353 sqlite3BtreeEnter(aDb[i].pBt); |
| 1170 } | 1354 } |
| 1171 } | 1355 } |
| 1172 } | 1356 } |
| 1173 #endif | 1357 #endif |
| 1174 | 1358 |
| 1175 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 | 1359 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0 |
| 1176 /* | 1360 /* |
| 1177 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). | 1361 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter(). |
| 1178 */ | 1362 */ |
| 1179 void sqlite3VdbeLeave(Vdbe *p){ | 1363 static SQLITE_NOINLINE void vdbeLeave(Vdbe *p){ |
| 1180 int i; | 1364 int i; |
| 1181 sqlite3 *db; | 1365 sqlite3 *db; |
| 1182 Db *aDb; | 1366 Db *aDb; |
| 1183 int nDb; | 1367 int nDb; |
| 1184 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ | |
| 1185 db = p->db; | 1368 db = p->db; |
| 1186 aDb = db->aDb; | 1369 aDb = db->aDb; |
| 1187 nDb = db->nDb; | 1370 nDb = db->nDb; |
| 1188 for(i=0; i<nDb; i++){ | 1371 for(i=0; i<nDb; i++){ |
| 1189 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ | 1372 if( i!=1 && DbMaskTest(p->lockMask,i) && ALWAYS(aDb[i].pBt!=0) ){ |
| 1190 sqlite3BtreeLeave(aDb[i].pBt); | 1373 sqlite3BtreeLeave(aDb[i].pBt); |
| 1191 } | 1374 } |
| 1192 } | 1375 } |
| 1193 } | 1376 } |
| 1377 void sqlite3VdbeLeave(Vdbe *p){ |
| 1378 if( DbMaskAllZero(p->lockMask) ) return; /* The common case */ |
| 1379 vdbeLeave(p); |
| 1380 } |
| 1194 #endif | 1381 #endif |
| 1195 | 1382 |
| 1196 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) | 1383 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG) |
| 1197 /* | 1384 /* |
| 1198 ** Print a single opcode. This routine is used for debugging only. | 1385 ** Print a single opcode. This routine is used for debugging only. |
| 1199 */ | 1386 */ |
| 1200 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ | 1387 void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){ |
| 1201 char *zP4; | 1388 char *zP4; |
| 1202 char zPtr[50]; | 1389 char zPtr[50]; |
| 1203 char zCom[100]; | 1390 char zCom[100]; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1356 | 1543 |
| 1357 do{ | 1544 do{ |
| 1358 i = p->pc++; | 1545 i = p->pc++; |
| 1359 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); | 1546 }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain ); |
| 1360 if( i>=nRow ){ | 1547 if( i>=nRow ){ |
| 1361 p->rc = SQLITE_OK; | 1548 p->rc = SQLITE_OK; |
| 1362 rc = SQLITE_DONE; | 1549 rc = SQLITE_DONE; |
| 1363 }else if( db->u1.isInterrupted ){ | 1550 }else if( db->u1.isInterrupted ){ |
| 1364 p->rc = SQLITE_INTERRUPT; | 1551 p->rc = SQLITE_INTERRUPT; |
| 1365 rc = SQLITE_ERROR; | 1552 rc = SQLITE_ERROR; |
| 1366 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc)); | 1553 sqlite3VdbeError(p, sqlite3ErrStr(p->rc)); |
| 1367 }else{ | 1554 }else{ |
| 1368 char *zP4; | 1555 char *zP4; |
| 1369 Op *pOp; | 1556 Op *pOp; |
| 1370 if( i<p->nOp ){ | 1557 if( i<p->nOp ){ |
| 1371 /* The output line number is small enough that we are still in the | 1558 /* The output line number is small enough that we are still in the |
| 1372 ** main program. */ | 1559 ** main program. */ |
| 1373 pOp = &p->aOp[i]; | 1560 pOp = &p->aOp[i]; |
| 1374 }else{ | 1561 }else{ |
| 1375 /* We are currently listing subprograms. Figure out which one and | 1562 /* We are currently listing subprograms. Figure out which one and |
| 1376 ** pick up the appropriate opcode. */ | 1563 ** pick up the appropriate opcode. */ |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1418 pMem++; | 1605 pMem++; |
| 1419 | 1606 |
| 1420 pMem->flags = MEM_Int; | 1607 pMem->flags = MEM_Int; |
| 1421 pMem->u.i = pOp->p2; /* P2 */ | 1608 pMem->u.i = pOp->p2; /* P2 */ |
| 1422 pMem++; | 1609 pMem++; |
| 1423 | 1610 |
| 1424 pMem->flags = MEM_Int; | 1611 pMem->flags = MEM_Int; |
| 1425 pMem->u.i = pOp->p3; /* P3 */ | 1612 pMem->u.i = pOp->p3; /* P3 */ |
| 1426 pMem++; | 1613 pMem++; |
| 1427 | 1614 |
| 1428 if( sqlite3VdbeMemClearAndResize(pMem, 32) ){ /* P4 */ | 1615 if( sqlite3VdbeMemClearAndResize(pMem, 100) ){ /* P4 */ |
| 1429 assert( p->db->mallocFailed ); | 1616 assert( p->db->mallocFailed ); |
| 1430 return SQLITE_ERROR; | 1617 return SQLITE_ERROR; |
| 1431 } | 1618 } |
| 1432 pMem->flags = MEM_Str|MEM_Term; | 1619 pMem->flags = MEM_Str|MEM_Term; |
| 1433 zP4 = displayP4(pOp, pMem->z, 32); | 1620 zP4 = displayP4(pOp, pMem->z, pMem->szMalloc); |
| 1434 if( zP4!=pMem->z ){ | 1621 if( zP4!=pMem->z ){ |
| 1435 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); | 1622 sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0); |
| 1436 }else{ | 1623 }else{ |
| 1437 assert( pMem->z!=0 ); | 1624 assert( pMem->z!=0 ); |
| 1438 pMem->n = sqlite3Strlen30(pMem->z); | 1625 pMem->n = sqlite3Strlen30(pMem->z); |
| 1439 pMem->enc = SQLITE_UTF8; | 1626 pMem->enc = SQLITE_UTF8; |
| 1440 } | 1627 } |
| 1441 pMem++; | 1628 pMem++; |
| 1442 | 1629 |
| 1443 if( p->explain==1 ){ | 1630 if( p->explain==1 ){ |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1528 ** | 1715 ** |
| 1529 ** The pBuf parameter is the initial value of a pointer which will | 1716 ** The pBuf parameter is the initial value of a pointer which will |
| 1530 ** receive the new memory. pBuf is normally NULL. If pBuf is not | 1717 ** receive the new memory. pBuf is normally NULL. If pBuf is not |
| 1531 ** NULL, it means that memory space has already been allocated and that | 1718 ** NULL, it means that memory space has already been allocated and that |
| 1532 ** this routine should not allocate any new memory. When pBuf is not | 1719 ** this routine should not allocate any new memory. When pBuf is not |
| 1533 ** NULL simply return pBuf. Only allocate new memory space when pBuf | 1720 ** NULL simply return pBuf. Only allocate new memory space when pBuf |
| 1534 ** is NULL. | 1721 ** is NULL. |
| 1535 ** | 1722 ** |
| 1536 ** nByte is the number of bytes of space needed. | 1723 ** nByte is the number of bytes of space needed. |
| 1537 ** | 1724 ** |
| 1538 ** *ppFrom points to available space and pEnd points to the end of the | 1725 ** pFrom points to *pnFrom bytes of available space. New space is allocated |
| 1539 ** available space. When space is allocated, *ppFrom is advanced past | 1726 ** from the end of the pFrom buffer and *pnFrom is decremented. |
| 1540 ** the end of the allocated space. | |
| 1541 ** | 1727 ** |
| 1542 ** *pnByte is a counter of the number of bytes of space that have failed | 1728 ** *pnNeeded is a counter of the number of bytes of space that have failed |
| 1543 ** to allocate. If there is insufficient space in *ppFrom to satisfy the | 1729 ** to allocate. If there is insufficient space in pFrom to satisfy the |
| 1544 ** request, then increment *pnByte by the amount of the request. | 1730 ** request, then increment *pnNeeded by the amount of the request. |
| 1545 */ | 1731 */ |
| 1546 static void *allocSpace( | 1732 static void *allocSpace( |
| 1547 void *pBuf, /* Where return pointer will be stored */ | 1733 void *pBuf, /* Where return pointer will be stored */ |
| 1548 int nByte, /* Number of bytes to allocate */ | 1734 int nByte, /* Number of bytes to allocate */ |
| 1549 u8 **ppFrom, /* IN/OUT: Allocate from *ppFrom */ | 1735 u8 *pFrom, /* Memory available for allocation */ |
| 1550 u8 *pEnd, /* Pointer to 1 byte past the end of *ppFrom buffer */ | 1736 int *pnFrom, /* IN/OUT: Space available at pFrom */ |
| 1551 int *pnByte /* If allocation cannot be made, increment *pnByte */ | 1737 int *pnNeeded /* If allocation cannot be made, increment *pnByte */ |
| 1552 ){ | 1738 ){ |
| 1553 assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) ); | 1739 assert( EIGHT_BYTE_ALIGNMENT(pFrom) ); |
| 1554 if( pBuf ) return pBuf; | 1740 if( pBuf==0 ){ |
| 1555 nByte = ROUND8(nByte); | 1741 nByte = ROUND8(nByte); |
| 1556 if( &(*ppFrom)[nByte] <= pEnd ){ | 1742 if( nByte <= *pnFrom ){ |
| 1557 pBuf = (void*)*ppFrom; | 1743 *pnFrom -= nByte; |
| 1558 *ppFrom += nByte; | 1744 pBuf = &pFrom[*pnFrom]; |
| 1559 }else{ | 1745 }else{ |
| 1560 *pnByte += nByte; | 1746 *pnNeeded += nByte; |
| 1747 } |
| 1561 } | 1748 } |
| 1749 assert( EIGHT_BYTE_ALIGNMENT(pBuf) ); |
| 1562 return pBuf; | 1750 return pBuf; |
| 1563 } | 1751 } |
| 1564 | 1752 |
| 1565 /* | 1753 /* |
| 1566 ** Rewind the VDBE back to the beginning in preparation for | 1754 ** Rewind the VDBE back to the beginning in preparation for |
| 1567 ** running it. | 1755 ** running it. |
| 1568 */ | 1756 */ |
| 1569 void sqlite3VdbeRewind(Vdbe *p){ | 1757 void sqlite3VdbeRewind(Vdbe *p){ |
| 1570 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) | 1758 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 1571 int i; | 1759 int i; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1624 Vdbe *p, /* The VDBE */ | 1812 Vdbe *p, /* The VDBE */ |
| 1625 Parse *pParse /* Parsing context */ | 1813 Parse *pParse /* Parsing context */ |
| 1626 ){ | 1814 ){ |
| 1627 sqlite3 *db; /* The database connection */ | 1815 sqlite3 *db; /* The database connection */ |
| 1628 int nVar; /* Number of parameters */ | 1816 int nVar; /* Number of parameters */ |
| 1629 int nMem; /* Number of VM memory registers */ | 1817 int nMem; /* Number of VM memory registers */ |
| 1630 int nCursor; /* Number of cursors required */ | 1818 int nCursor; /* Number of cursors required */ |
| 1631 int nArg; /* Number of arguments in subprograms */ | 1819 int nArg; /* Number of arguments in subprograms */ |
| 1632 int nOnce; /* Number of OP_Once instructions */ | 1820 int nOnce; /* Number of OP_Once instructions */ |
| 1633 int n; /* Loop counter */ | 1821 int n; /* Loop counter */ |
| 1822 int nFree; /* Available free space */ |
| 1634 u8 *zCsr; /* Memory available for allocation */ | 1823 u8 *zCsr; /* Memory available for allocation */ |
| 1635 u8 *zEnd; /* First byte past allocated memory */ | |
| 1636 int nByte; /* How much extra memory is needed */ | 1824 int nByte; /* How much extra memory is needed */ |
| 1637 | 1825 |
| 1638 assert( p!=0 ); | 1826 assert( p!=0 ); |
| 1639 assert( p->nOp>0 ); | 1827 assert( p->nOp>0 ); |
| 1640 assert( pParse!=0 ); | 1828 assert( pParse!=0 ); |
| 1641 assert( p->magic==VDBE_MAGIC_INIT ); | 1829 assert( p->magic==VDBE_MAGIC_INIT ); |
| 1642 assert( pParse==p->pParse ); | 1830 assert( pParse==p->pParse ); |
| 1643 db = p->db; | 1831 db = p->db; |
| 1644 assert( db->mallocFailed==0 ); | 1832 assert( db->mallocFailed==0 ); |
| 1645 nVar = pParse->nVar; | 1833 nVar = pParse->nVar; |
| 1646 nMem = pParse->nMem; | 1834 nMem = pParse->nMem; |
| 1647 nCursor = pParse->nTab; | 1835 nCursor = pParse->nTab; |
| 1648 nArg = pParse->nMaxArg; | 1836 nArg = pParse->nMaxArg; |
| 1649 nOnce = pParse->nOnce; | 1837 nOnce = pParse->nOnce; |
| 1650 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ | 1838 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */ |
| 1651 | 1839 |
| 1652 /* For each cursor required, also allocate a memory cell. Memory | 1840 /* For each cursor required, also allocate a memory cell. Memory |
| 1653 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by | 1841 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by |
| 1654 ** the vdbe program. Instead they are used to allocate space for | 1842 ** the vdbe program. Instead they are used to allocate space for |
| 1655 ** VdbeCursor/BtCursor structures. The blob of memory associated with | 1843 ** VdbeCursor/BtCursor structures. The blob of memory associated with |
| 1656 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) | 1844 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1) |
| 1657 ** stores the blob of memory associated with cursor 1, etc. | 1845 ** stores the blob of memory associated with cursor 1, etc. |
| 1658 ** | 1846 ** |
| 1659 ** See also: allocateCursor(). | 1847 ** See also: allocateCursor(). |
| 1660 */ | 1848 */ |
| 1661 nMem += nCursor; | 1849 nMem += nCursor; |
| 1662 | 1850 |
| 1663 /* Allocate space for memory registers, SQL variables, VDBE cursors and | 1851 /* zCsr will initially point to nFree bytes of unused space at the |
| 1664 ** an array to marshal SQL function arguments in. | 1852 ** end of the opcode array, p->aOp. The computation of nFree is |
| 1853 ** conservative - it might be smaller than the true number of free |
| 1854 ** bytes, but never larger. nFree must be a multiple of 8 - it is |
| 1855 ** rounded down if is not. |
| 1665 */ | 1856 */ |
| 1666 zCsr = (u8*)&p->aOp[p->nOp]; /* Memory avaliable for allocation */ | 1857 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode space used */ |
| 1667 zEnd = (u8*)&p->aOp[pParse->nOpAlloc]; /* First byte past end of zCsr[] */ | 1858 zCsr = &((u8*)p->aOp)[n]; /* Unused opcode space */ |
| 1859 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); |
| 1860 nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused space */ |
| 1861 assert( nFree>=0 ); |
| 1862 if( nFree>0 ){ |
| 1863 memset(zCsr, 0, nFree); |
| 1864 assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) ); |
| 1865 } |
| 1668 | 1866 |
| 1669 resolveP2Values(p, &nArg); | 1867 resolveP2Values(p, &nArg); |
| 1670 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); | 1868 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort); |
| 1671 if( pParse->explain && nMem<10 ){ | 1869 if( pParse->explain && nMem<10 ){ |
| 1672 nMem = 10; | 1870 nMem = 10; |
| 1673 } | 1871 } |
| 1674 memset(zCsr, 0, zEnd-zCsr); | |
| 1675 zCsr += (zCsr - (u8*)0)&7; | |
| 1676 assert( EIGHT_BYTE_ALIGNMENT(zCsr) ); | |
| 1677 p->expired = 0; | 1872 p->expired = 0; |
| 1678 | 1873 |
| 1679 /* Memory for registers, parameters, cursor, etc, is allocated in two | 1874 /* Memory for registers, parameters, cursor, etc, is allocated in two |
| 1680 ** passes. On the first pass, we try to reuse unused space at the | 1875 ** passes. On the first pass, we try to reuse unused space at the |
| 1681 ** end of the opcode array. If we are unable to satisfy all memory | 1876 ** end of the opcode array. If we are unable to satisfy all memory |
| 1682 ** requirements by reusing the opcode array tail, then the second | 1877 ** requirements by reusing the opcode array tail, then the second |
| 1683 ** pass will fill in the rest using a fresh allocation. | 1878 ** pass will fill in the rest using a fresh allocation. |
| 1684 ** | 1879 ** |
| 1685 ** This two-pass approach that reuses as much memory as possible from | 1880 ** This two-pass approach that reuses as much memory as possible from |
| 1686 ** the leftover space at the end of the opcode array can significantly | 1881 ** the leftover space at the end of the opcode array can significantly |
| 1687 ** reduce the amount of memory held by a prepared statement. | 1882 ** reduce the amount of memory held by a prepared statement. |
| 1688 */ | 1883 */ |
| 1689 do { | 1884 do { |
| 1690 nByte = 0; | 1885 nByte = 0; |
| 1691 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte); | 1886 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte); |
| 1692 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte); | 1887 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte); |
| 1693 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte); | 1888 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte); |
| 1694 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte); | 1889 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), zCsr, &nFree, &nByte); |
| 1695 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), | 1890 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*), |
| 1696 &zCsr, zEnd, &nByte); | 1891 zCsr, &nFree, &nByte); |
| 1697 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte); | 1892 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte); |
| 1893 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1894 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte); |
| 1895 #endif |
| 1698 if( nByte ){ | 1896 if( nByte ){ |
| 1699 p->pFree = sqlite3DbMallocZero(db, nByte); | 1897 p->pFree = sqlite3DbMallocZero(db, nByte); |
| 1700 } | 1898 } |
| 1701 zCsr = p->pFree; | 1899 zCsr = p->pFree; |
| 1702 zEnd = &zCsr[nByte]; | 1900 nFree = nByte; |
| 1703 }while( nByte && !db->mallocFailed ); | 1901 }while( nByte && !db->mallocFailed ); |
| 1704 | 1902 |
| 1705 p->nCursor = nCursor; | 1903 p->nCursor = nCursor; |
| 1706 p->nOnceFlag = nOnce; | 1904 p->nOnceFlag = nOnce; |
| 1707 if( p->aVar ){ | 1905 if( p->aVar ){ |
| 1708 p->nVar = (ynVar)nVar; | 1906 p->nVar = (ynVar)nVar; |
| 1709 for(n=0; n<nVar; n++){ | 1907 for(n=0; n<nVar; n++){ |
| 1710 p->aVar[n].flags = MEM_Null; | 1908 p->aVar[n].flags = MEM_Null; |
| 1711 p->aVar[n].db = db; | 1909 p->aVar[n].db = db; |
| 1712 } | 1910 } |
| 1713 } | 1911 } |
| 1714 if( p->azVar ){ | 1912 if( p->azVar && pParse->nzVar>0 ){ |
| 1715 p->nzVar = pParse->nzVar; | 1913 p->nzVar = pParse->nzVar; |
| 1716 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); | 1914 memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0])); |
| 1717 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); | 1915 memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0])); |
| 1718 } | 1916 } |
| 1719 if( p->aMem ){ | 1917 if( p->aMem ){ |
| 1720 p->aMem--; /* aMem[] goes from 1..nMem */ | 1918 p->aMem--; /* aMem[] goes from 1..nMem */ |
| 1721 p->nMem = nMem; /* not from 0..nMem-1 */ | 1919 p->nMem = nMem; /* not from 0..nMem-1 */ |
| 1722 for(n=1; n<=nMem; n++){ | 1920 for(n=1; n<=nMem; n++){ |
| 1723 p->aMem[n].flags = MEM_Undefined; | 1921 p->aMem[n].flags = MEM_Undefined; |
| 1724 p->aMem[n].db = db; | 1922 p->aMem[n].db = db; |
| 1725 } | 1923 } |
| 1726 } | 1924 } |
| 1727 p->explain = pParse->explain; | 1925 p->explain = pParse->explain; |
| 1728 sqlite3VdbeRewind(p); | 1926 sqlite3VdbeRewind(p); |
| 1729 } | 1927 } |
| 1730 | 1928 |
| 1731 /* | 1929 /* |
| 1732 ** Close a VDBE cursor and release all the resources that cursor | 1930 ** Close a VDBE cursor and release all the resources that cursor |
| 1733 ** happens to hold. | 1931 ** happens to hold. |
| 1734 */ | 1932 */ |
| 1735 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ | 1933 void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ |
| 1736 if( pCx==0 ){ | 1934 if( pCx==0 ){ |
| 1737 return; | 1935 return; |
| 1738 } | 1936 } |
| 1739 sqlite3VdbeSorterClose(p->db, pCx); | 1937 assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE ); |
| 1740 if( pCx->pBt ){ | 1938 switch( pCx->eCurType ){ |
| 1741 sqlite3BtreeClose(pCx->pBt); | 1939 case CURTYPE_SORTER: { |
| 1742 /* The pCx->pCursor will be close automatically, if it exists, by | 1940 sqlite3VdbeSorterClose(p->db, pCx); |
| 1743 ** the call above. */ | 1941 break; |
| 1744 }else if( pCx->pCursor ){ | 1942 } |
| 1745 sqlite3BtreeCloseCursor(pCx->pCursor); | 1943 case CURTYPE_BTREE: { |
| 1944 if( pCx->pBt ){ |
| 1945 sqlite3BtreeClose(pCx->pBt); |
| 1946 /* The pCx->pCursor will be close automatically, if it exists, by |
| 1947 ** the call above. */ |
| 1948 }else{ |
| 1949 assert( pCx->uc.pCursor!=0 ); |
| 1950 sqlite3BtreeCloseCursor(pCx->uc.pCursor); |
| 1951 } |
| 1952 break; |
| 1953 } |
| 1954 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1955 case CURTYPE_VTAB: { |
| 1956 sqlite3_vtab_cursor *pVCur = pCx->uc.pVCur; |
| 1957 const sqlite3_module *pModule = pVCur->pVtab->pModule; |
| 1958 assert( pVCur->pVtab->nRef>0 ); |
| 1959 pVCur->pVtab->nRef--; |
| 1960 pModule->xClose(pVCur); |
| 1961 break; |
| 1962 } |
| 1963 #endif |
| 1746 } | 1964 } |
| 1747 #ifndef SQLITE_OMIT_VIRTUALTABLE | |
| 1748 else if( pCx->pVtabCursor ){ | |
| 1749 sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor; | |
| 1750 const sqlite3_module *pModule = pVtabCursor->pVtab->pModule; | |
| 1751 p->inVtabMethod = 1; | |
| 1752 pModule->xClose(pVtabCursor); | |
| 1753 p->inVtabMethod = 0; | |
| 1754 } | |
| 1755 #endif | |
| 1756 } | 1965 } |
| 1757 | 1966 |
| 1758 /* | 1967 /* |
| 1968 ** Close all cursors in the current frame. |
| 1969 */ |
| 1970 static void closeCursorsInFrame(Vdbe *p){ |
| 1971 if( p->apCsr ){ |
| 1972 int i; |
| 1973 for(i=0; i<p->nCursor; i++){ |
| 1974 VdbeCursor *pC = p->apCsr[i]; |
| 1975 if( pC ){ |
| 1976 sqlite3VdbeFreeCursor(p, pC); |
| 1977 p->apCsr[i] = 0; |
| 1978 } |
| 1979 } |
| 1980 } |
| 1981 } |
| 1982 |
| 1983 /* |
| 1759 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This | 1984 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This |
| 1760 ** is used, for example, when a trigger sub-program is halted to restore | 1985 ** is used, for example, when a trigger sub-program is halted to restore |
| 1761 ** control to the main program. | 1986 ** control to the main program. |
| 1762 */ | 1987 */ |
| 1763 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ | 1988 int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 1764 Vdbe *v = pFrame->v; | 1989 Vdbe *v = pFrame->v; |
| 1990 closeCursorsInFrame(v); |
| 1991 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 1992 v->anExec = pFrame->anExec; |
| 1993 #endif |
| 1765 v->aOnceFlag = pFrame->aOnceFlag; | 1994 v->aOnceFlag = pFrame->aOnceFlag; |
| 1766 v->nOnceFlag = pFrame->nOnceFlag; | 1995 v->nOnceFlag = pFrame->nOnceFlag; |
| 1767 v->aOp = pFrame->aOp; | 1996 v->aOp = pFrame->aOp; |
| 1768 v->nOp = pFrame->nOp; | 1997 v->nOp = pFrame->nOp; |
| 1769 v->aMem = pFrame->aMem; | 1998 v->aMem = pFrame->aMem; |
| 1770 v->nMem = pFrame->nMem; | 1999 v->nMem = pFrame->nMem; |
| 1771 v->apCsr = pFrame->apCsr; | 2000 v->apCsr = pFrame->apCsr; |
| 1772 v->nCursor = pFrame->nCursor; | 2001 v->nCursor = pFrame->nCursor; |
| 1773 v->db->lastRowid = pFrame->lastRowid; | 2002 v->db->lastRowid = pFrame->lastRowid; |
| 1774 v->nChange = pFrame->nChange; | 2003 v->nChange = pFrame->nChange; |
| 2004 v->db->nChange = pFrame->nDbChange; |
| 1775 return pFrame->pc; | 2005 return pFrame->pc; |
| 1776 } | 2006 } |
| 1777 | 2007 |
| 1778 /* | 2008 /* |
| 1779 ** Close all cursors. | 2009 ** Close all cursors. |
| 1780 ** | 2010 ** |
| 1781 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory | 2011 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory |
| 1782 ** cell array. This is necessary as the memory cell array may contain | 2012 ** cell array. This is necessary as the memory cell array may contain |
| 1783 ** pointers to VdbeFrame objects, which may in turn contain pointers to | 2013 ** pointers to VdbeFrame objects, which may in turn contain pointers to |
| 1784 ** open cursors. | 2014 ** open cursors. |
| 1785 */ | 2015 */ |
| 1786 static void closeAllCursors(Vdbe *p){ | 2016 static void closeAllCursors(Vdbe *p){ |
| 1787 if( p->pFrame ){ | 2017 if( p->pFrame ){ |
| 1788 VdbeFrame *pFrame; | 2018 VdbeFrame *pFrame; |
| 1789 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); | 2019 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); |
| 1790 sqlite3VdbeFrameRestore(pFrame); | 2020 sqlite3VdbeFrameRestore(pFrame); |
| 1791 p->pFrame = 0; | 2021 p->pFrame = 0; |
| 1792 p->nFrame = 0; | 2022 p->nFrame = 0; |
| 1793 } | 2023 } |
| 1794 assert( p->nFrame==0 ); | 2024 assert( p->nFrame==0 ); |
| 1795 | 2025 closeCursorsInFrame(p); |
| 1796 if( p->apCsr ){ | |
| 1797 int i; | |
| 1798 for(i=0; i<p->nCursor; i++){ | |
| 1799 VdbeCursor *pC = p->apCsr[i]; | |
| 1800 if( pC ){ | |
| 1801 sqlite3VdbeFreeCursor(p, pC); | |
| 1802 p->apCsr[i] = 0; | |
| 1803 } | |
| 1804 } | |
| 1805 } | |
| 1806 if( p->aMem ){ | 2026 if( p->aMem ){ |
| 1807 releaseMemArray(&p->aMem[1], p->nMem); | 2027 releaseMemArray(&p->aMem[1], p->nMem); |
| 1808 } | 2028 } |
| 1809 while( p->pDelFrame ){ | 2029 while( p->pDelFrame ){ |
| 1810 VdbeFrame *pDel = p->pDelFrame; | 2030 VdbeFrame *pDel = p->pDelFrame; |
| 1811 p->pDelFrame = pDel->pParent; | 2031 p->pDelFrame = pDel->pParent; |
| 1812 sqlite3VdbeFrameDelete(pDel); | 2032 sqlite3VdbeFrameDelete(pDel); |
| 1813 } | 2033 } |
| 1814 | 2034 |
| 1815 /* Delete any auxdata allocations made by the VM */ | 2035 /* Delete any auxdata allocations made by the VM */ |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2098 assert( rc!=SQLITE_BUSY ); | 2318 assert( rc!=SQLITE_BUSY ); |
| 2099 if( rc!=SQLITE_OK ){ | 2319 if( rc!=SQLITE_OK ){ |
| 2100 sqlite3DbFree(db, zMaster); | 2320 sqlite3DbFree(db, zMaster); |
| 2101 return rc; | 2321 return rc; |
| 2102 } | 2322 } |
| 2103 | 2323 |
| 2104 /* Delete the master journal file. This commits the transaction. After | 2324 /* Delete the master journal file. This commits the transaction. After |
| 2105 ** doing this the directory is synced again before any individual | 2325 ** doing this the directory is synced again before any individual |
| 2106 ** transaction files are deleted. | 2326 ** transaction files are deleted. |
| 2107 */ | 2327 */ |
| 2108 rc = sqlite3OsDelete(pVfs, zMaster, 1); | 2328 rc = sqlite3OsDelete(pVfs, zMaster, needSync); |
| 2109 sqlite3DbFree(db, zMaster); | 2329 sqlite3DbFree(db, zMaster); |
| 2110 zMaster = 0; | 2330 zMaster = 0; |
| 2111 if( rc ){ | 2331 if( rc ){ |
| 2112 return rc; | 2332 return rc; |
| 2113 } | 2333 } |
| 2114 | 2334 |
| 2115 /* All files and directories have already been synced, so the following | 2335 /* All files and directories have already been synced, so the following |
| 2116 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and | 2336 ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and |
| 2117 ** deleting or truncating journals. If something goes wrong while | 2337 ** deleting or truncating journals. If something goes wrong while |
| 2118 ** this is happening we don't really care. The integrity of the | 2338 ** this is happening we don't really care. The integrity of the |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2245 ** and write an error message to it. Then return SQLITE_ERROR. | 2465 ** and write an error message to it. Then return SQLITE_ERROR. |
| 2246 */ | 2466 */ |
| 2247 #ifndef SQLITE_OMIT_FOREIGN_KEY | 2467 #ifndef SQLITE_OMIT_FOREIGN_KEY |
| 2248 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ | 2468 int sqlite3VdbeCheckFk(Vdbe *p, int deferred){ |
| 2249 sqlite3 *db = p->db; | 2469 sqlite3 *db = p->db; |
| 2250 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) | 2470 if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0) |
| 2251 || (!deferred && p->nFkConstraint>0) | 2471 || (!deferred && p->nFkConstraint>0) |
| 2252 ){ | 2472 ){ |
| 2253 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; | 2473 p->rc = SQLITE_CONSTRAINT_FOREIGNKEY; |
| 2254 p->errorAction = OE_Abort; | 2474 p->errorAction = OE_Abort; |
| 2255 sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed"); | 2475 sqlite3VdbeError(p, "FOREIGN KEY constraint failed"); |
| 2256 return SQLITE_ERROR; | 2476 return SQLITE_ERROR; |
| 2257 } | 2477 } |
| 2258 return SQLITE_OK; | 2478 return SQLITE_OK; |
| 2259 } | 2479 } |
| 2260 #endif | 2480 #endif |
| 2261 | 2481 |
| 2262 /* | 2482 /* |
| 2263 ** This routine is called the when a VDBE tries to halt. If the VDBE | 2483 ** This routine is called the when a VDBE tries to halt. If the VDBE |
| 2264 ** has made changes and is in autocommit mode, then commit those | 2484 ** has made changes and is in autocommit mode, then commit those |
| 2265 ** changes. If a rollback is needed, then do the rollback. | 2485 ** changes. If a rollback is needed, then do the rollback. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2332 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ | 2552 if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){ |
| 2333 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ | 2553 if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){ |
| 2334 eStatementOp = SAVEPOINT_ROLLBACK; | 2554 eStatementOp = SAVEPOINT_ROLLBACK; |
| 2335 }else{ | 2555 }else{ |
| 2336 /* We are forced to roll back the active transaction. Before doing | 2556 /* We are forced to roll back the active transaction. Before doing |
| 2337 ** so, abort any other statements this handle currently has active. | 2557 ** so, abort any other statements this handle currently has active. |
| 2338 */ | 2558 */ |
| 2339 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | 2559 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 2340 sqlite3CloseSavepoints(db); | 2560 sqlite3CloseSavepoints(db); |
| 2341 db->autoCommit = 1; | 2561 db->autoCommit = 1; |
| 2562 p->nChange = 0; |
| 2342 } | 2563 } |
| 2343 } | 2564 } |
| 2344 } | 2565 } |
| 2345 | 2566 |
| 2346 /* Check for immediate foreign key violations. */ | 2567 /* Check for immediate foreign key violations. */ |
| 2347 if( p->rc==SQLITE_OK ){ | 2568 if( p->rc==SQLITE_OK ){ |
| 2348 sqlite3VdbeCheckFk(p, 0); | 2569 sqlite3VdbeCheckFk(p, 0); |
| 2349 } | 2570 } |
| 2350 | 2571 |
| 2351 /* If the auto-commit flag is set and this is the only active writer | 2572 /* If the auto-commit flag is set and this is the only active writer |
| (...skipping 20 matching lines...) Expand all Loading... |
| 2372 ** key constraints to hold up the transaction. This means a commit | 2593 ** key constraints to hold up the transaction. This means a commit |
| 2373 ** is required. */ | 2594 ** is required. */ |
| 2374 rc = vdbeCommit(db, p); | 2595 rc = vdbeCommit(db, p); |
| 2375 } | 2596 } |
| 2376 if( rc==SQLITE_BUSY && p->readOnly ){ | 2597 if( rc==SQLITE_BUSY && p->readOnly ){ |
| 2377 sqlite3VdbeLeave(p); | 2598 sqlite3VdbeLeave(p); |
| 2378 return SQLITE_BUSY; | 2599 return SQLITE_BUSY; |
| 2379 }else if( rc!=SQLITE_OK ){ | 2600 }else if( rc!=SQLITE_OK ){ |
| 2380 p->rc = rc; | 2601 p->rc = rc; |
| 2381 sqlite3RollbackAll(db, SQLITE_OK); | 2602 sqlite3RollbackAll(db, SQLITE_OK); |
| 2603 p->nChange = 0; |
| 2382 }else{ | 2604 }else{ |
| 2383 db->nDeferredCons = 0; | 2605 db->nDeferredCons = 0; |
| 2384 db->nDeferredImmCons = 0; | 2606 db->nDeferredImmCons = 0; |
| 2385 db->flags &= ~SQLITE_DeferFKs; | 2607 db->flags &= ~SQLITE_DeferFKs; |
| 2386 sqlite3CommitInternalChanges(db); | 2608 sqlite3CommitInternalChanges(db); |
| 2387 } | 2609 } |
| 2388 }else{ | 2610 }else{ |
| 2389 sqlite3RollbackAll(db, SQLITE_OK); | 2611 sqlite3RollbackAll(db, SQLITE_OK); |
| 2612 p->nChange = 0; |
| 2390 } | 2613 } |
| 2391 db->nStatement = 0; | 2614 db->nStatement = 0; |
| 2392 }else if( eStatementOp==0 ){ | 2615 }else if( eStatementOp==0 ){ |
| 2393 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ | 2616 if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){ |
| 2394 eStatementOp = SAVEPOINT_RELEASE; | 2617 eStatementOp = SAVEPOINT_RELEASE; |
| 2395 }else if( p->errorAction==OE_Abort ){ | 2618 }else if( p->errorAction==OE_Abort ){ |
| 2396 eStatementOp = SAVEPOINT_ROLLBACK; | 2619 eStatementOp = SAVEPOINT_ROLLBACK; |
| 2397 }else{ | 2620 }else{ |
| 2398 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | 2621 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 2399 sqlite3CloseSavepoints(db); | 2622 sqlite3CloseSavepoints(db); |
| 2400 db->autoCommit = 1; | 2623 db->autoCommit = 1; |
| 2624 p->nChange = 0; |
| 2401 } | 2625 } |
| 2402 } | 2626 } |
| 2403 | 2627 |
| 2404 /* If eStatementOp is non-zero, then a statement transaction needs to | 2628 /* If eStatementOp is non-zero, then a statement transaction needs to |
| 2405 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to | 2629 ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to |
| 2406 ** do so. If this operation returns an error, and the current statement | 2630 ** do so. If this operation returns an error, and the current statement |
| 2407 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the | 2631 ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the |
| 2408 ** current statement error code. | 2632 ** current statement error code. |
| 2409 */ | 2633 */ |
| 2410 if( eStatementOp ){ | 2634 if( eStatementOp ){ |
| 2411 rc = sqlite3VdbeCloseStatement(p, eStatementOp); | 2635 rc = sqlite3VdbeCloseStatement(p, eStatementOp); |
| 2412 if( rc ){ | 2636 if( rc ){ |
| 2413 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ | 2637 if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){ |
| 2414 p->rc = rc; | 2638 p->rc = rc; |
| 2415 sqlite3DbFree(db, p->zErrMsg); | 2639 sqlite3DbFree(db, p->zErrMsg); |
| 2416 p->zErrMsg = 0; | 2640 p->zErrMsg = 0; |
| 2417 } | 2641 } |
| 2418 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); | 2642 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK); |
| 2419 sqlite3CloseSavepoints(db); | 2643 sqlite3CloseSavepoints(db); |
| 2420 db->autoCommit = 1; | 2644 db->autoCommit = 1; |
| 2645 p->nChange = 0; |
| 2421 } | 2646 } |
| 2422 } | 2647 } |
| 2423 | 2648 |
| 2424 /* If this was an INSERT, UPDATE or DELETE and no statement transaction | 2649 /* If this was an INSERT, UPDATE or DELETE and no statement transaction |
| 2425 ** has been rolled back, update the database connection change-counter. | 2650 ** has been rolled back, update the database connection change-counter. |
| 2426 */ | 2651 */ |
| 2427 if( p->changeCntOn ){ | 2652 if( p->changeCntOn ){ |
| 2428 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ | 2653 if( eStatementOp!=SAVEPOINT_ROLLBACK ){ |
| 2429 sqlite3VdbeSetChanges(db, p->nChange); | 2654 sqlite3VdbeSetChanges(db, p->nChange); |
| 2430 }else{ | 2655 }else{ |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2672 for(pSub=p->pProgram; pSub; pSub=pNext){ | 2897 for(pSub=p->pProgram; pSub; pSub=pNext){ |
| 2673 pNext = pSub->pNext; | 2898 pNext = pSub->pNext; |
| 2674 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); | 2899 vdbeFreeOpArray(db, pSub->aOp, pSub->nOp); |
| 2675 sqlite3DbFree(db, pSub); | 2900 sqlite3DbFree(db, pSub); |
| 2676 } | 2901 } |
| 2677 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); | 2902 for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]); |
| 2678 vdbeFreeOpArray(db, p->aOp, p->nOp); | 2903 vdbeFreeOpArray(db, p->aOp, p->nOp); |
| 2679 sqlite3DbFree(db, p->aColName); | 2904 sqlite3DbFree(db, p->aColName); |
| 2680 sqlite3DbFree(db, p->zSql); | 2905 sqlite3DbFree(db, p->zSql); |
| 2681 sqlite3DbFree(db, p->pFree); | 2906 sqlite3DbFree(db, p->pFree); |
| 2907 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2908 for(i=0; i<p->nScan; i++){ |
| 2909 sqlite3DbFree(db, p->aScan[i].zName); |
| 2910 } |
| 2911 sqlite3DbFree(db, p->aScan); |
| 2912 #endif |
| 2682 } | 2913 } |
| 2683 | 2914 |
| 2684 /* | 2915 /* |
| 2685 ** Delete an entire VDBE. | 2916 ** Delete an entire VDBE. |
| 2686 */ | 2917 */ |
| 2687 void sqlite3VdbeDelete(Vdbe *p){ | 2918 void sqlite3VdbeDelete(Vdbe *p){ |
| 2688 sqlite3 *db; | 2919 sqlite3 *db; |
| 2689 | 2920 |
| 2690 if( NEVER(p==0) ) return; | 2921 if( NEVER(p==0) ) return; |
| 2691 db = p->db; | 2922 db = p->db; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 2710 ** carried out. Seek the cursor now. If an error occurs, return | 2941 ** carried out. Seek the cursor now. If an error occurs, return |
| 2711 ** the appropriate error code. | 2942 ** the appropriate error code. |
| 2712 */ | 2943 */ |
| 2713 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ | 2944 static int SQLITE_NOINLINE handleDeferredMoveto(VdbeCursor *p){ |
| 2714 int res, rc; | 2945 int res, rc; |
| 2715 #ifdef SQLITE_TEST | 2946 #ifdef SQLITE_TEST |
| 2716 extern int sqlite3_search_count; | 2947 extern int sqlite3_search_count; |
| 2717 #endif | 2948 #endif |
| 2718 assert( p->deferredMoveto ); | 2949 assert( p->deferredMoveto ); |
| 2719 assert( p->isTable ); | 2950 assert( p->isTable ); |
| 2720 rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res); | 2951 assert( p->eCurType==CURTYPE_BTREE ); |
| 2952 rc = sqlite3BtreeMovetoUnpacked(p->uc.pCursor, 0, p->movetoTarget, 0, &res); |
| 2721 if( rc ) return rc; | 2953 if( rc ) return rc; |
| 2722 if( res!=0 ) return SQLITE_CORRUPT_BKPT; | 2954 if( res!=0 ) return SQLITE_CORRUPT_BKPT; |
| 2723 #ifdef SQLITE_TEST | 2955 #ifdef SQLITE_TEST |
| 2724 sqlite3_search_count++; | 2956 sqlite3_search_count++; |
| 2725 #endif | 2957 #endif |
| 2726 p->deferredMoveto = 0; | 2958 p->deferredMoveto = 0; |
| 2727 p->cacheStatus = CACHE_STALE; | 2959 p->cacheStatus = CACHE_STALE; |
| 2728 return SQLITE_OK; | 2960 return SQLITE_OK; |
| 2729 } | 2961 } |
| 2730 | 2962 |
| 2731 /* | 2963 /* |
| 2732 ** Something has moved cursor "p" out of place. Maybe the row it was | 2964 ** Something has moved cursor "p" out of place. Maybe the row it was |
| 2733 ** pointed to was deleted out from under it. Or maybe the btree was | 2965 ** pointed to was deleted out from under it. Or maybe the btree was |
| 2734 ** rebalanced. Whatever the cause, try to restore "p" to the place it | 2966 ** rebalanced. Whatever the cause, try to restore "p" to the place it |
| 2735 ** is supposed to be pointing. If the row was deleted out from under the | 2967 ** is supposed to be pointing. If the row was deleted out from under the |
| 2736 ** cursor, set the cursor to point to a NULL row. | 2968 ** cursor, set the cursor to point to a NULL row. |
| 2737 */ | 2969 */ |
| 2738 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ | 2970 static int SQLITE_NOINLINE handleMovedCursor(VdbeCursor *p){ |
| 2739 int isDifferentRow, rc; | 2971 int isDifferentRow, rc; |
| 2740 assert( p->pCursor!=0 ); | 2972 assert( p->eCurType==CURTYPE_BTREE ); |
| 2741 assert( sqlite3BtreeCursorHasMoved(p->pCursor) ); | 2973 assert( p->uc.pCursor!=0 ); |
| 2742 rc = sqlite3BtreeCursorRestore(p->pCursor, &isDifferentRow); | 2974 assert( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ); |
| 2975 rc = sqlite3BtreeCursorRestore(p->uc.pCursor, &isDifferentRow); |
| 2743 p->cacheStatus = CACHE_STALE; | 2976 p->cacheStatus = CACHE_STALE; |
| 2744 if( isDifferentRow ) p->nullRow = 1; | 2977 if( isDifferentRow ) p->nullRow = 1; |
| 2745 return rc; | 2978 return rc; |
| 2746 } | 2979 } |
| 2747 | 2980 |
| 2748 /* | 2981 /* |
| 2749 ** Check to ensure that the cursor is valid. Restore the cursor | 2982 ** Check to ensure that the cursor is valid. Restore the cursor |
| 2750 ** if need be. Return any I/O error from the restore operation. | 2983 ** if need be. Return any I/O error from the restore operation. |
| 2751 */ | 2984 */ |
| 2752 int sqlite3VdbeCursorRestore(VdbeCursor *p){ | 2985 int sqlite3VdbeCursorRestore(VdbeCursor *p){ |
| 2753 if( sqlite3BtreeCursorHasMoved(p->pCursor) ){ | 2986 assert( p->eCurType==CURTYPE_BTREE ); |
| 2987 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ |
| 2754 return handleMovedCursor(p); | 2988 return handleMovedCursor(p); |
| 2755 } | 2989 } |
| 2756 return SQLITE_OK; | 2990 return SQLITE_OK; |
| 2757 } | 2991 } |
| 2758 | 2992 |
| 2759 /* | 2993 /* |
| 2760 ** Make sure the cursor p is ready to read or write the row to which it | 2994 ** Make sure the cursor p is ready to read or write the row to which it |
| 2761 ** was last positioned. Return an error code if an OOM fault or I/O error | 2995 ** was last positioned. Return an error code if an OOM fault or I/O error |
| 2762 ** prevents us from positioning the cursor to its correct position. | 2996 ** prevents us from positioning the cursor to its correct position. |
| 2763 ** | 2997 ** |
| 2764 ** If a MoveTo operation is pending on the given cursor, then do that | 2998 ** If a MoveTo operation is pending on the given cursor, then do that |
| 2765 ** MoveTo now. If no move is pending, check to see if the row has been | 2999 ** MoveTo now. If no move is pending, check to see if the row has been |
| 2766 ** deleted out from under the cursor and if it has, mark the row as | 3000 ** deleted out from under the cursor and if it has, mark the row as |
| 2767 ** a NULL row. | 3001 ** a NULL row. |
| 2768 ** | 3002 ** |
| 2769 ** If the cursor is already pointing to the correct row and that row has | 3003 ** If the cursor is already pointing to the correct row and that row has |
| 2770 ** not been deleted out from under the cursor, then this routine is a no-op. | 3004 ** not been deleted out from under the cursor, then this routine is a no-op. |
| 2771 */ | 3005 */ |
| 2772 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ | 3006 int sqlite3VdbeCursorMoveto(VdbeCursor *p){ |
| 2773 if( p->deferredMoveto ){ | 3007 if( p->eCurType==CURTYPE_BTREE ){ |
| 2774 return handleDeferredMoveto(p); | 3008 if( p->deferredMoveto ){ |
| 2775 } | 3009 return handleDeferredMoveto(p); |
| 2776 if( p->pCursor && sqlite3BtreeCursorHasMoved(p->pCursor) ){ | 3010 } |
| 2777 return handleMovedCursor(p); | 3011 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){ |
| 3012 return handleMovedCursor(p); |
| 3013 } |
| 2778 } | 3014 } |
| 2779 return SQLITE_OK; | 3015 return SQLITE_OK; |
| 2780 } | 3016 } |
| 2781 | 3017 |
| 2782 /* | 3018 /* |
| 2783 ** The following functions: | 3019 ** The following functions: |
| 2784 ** | 3020 ** |
| 2785 ** sqlite3VdbeSerialType() | 3021 ** sqlite3VdbeSerialType() |
| 2786 ** sqlite3VdbeSerialTypeLen() | 3022 ** sqlite3VdbeSerialTypeLen() |
| 2787 ** sqlite3VdbeSerialLen() | 3023 ** sqlite3VdbeSerialLen() |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2817 ** N>=12 and even (N-12)/2 BLOB | 3053 ** N>=12 and even (N-12)/2 BLOB |
| 2818 ** N>=13 and odd (N-13)/2 text | 3054 ** N>=13 and odd (N-13)/2 text |
| 2819 ** | 3055 ** |
| 2820 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions | 3056 ** The 8 and 9 types were added in 3.3.0, file format 4. Prior versions |
| 2821 ** of SQLite will not understand those serial types. | 3057 ** of SQLite will not understand those serial types. |
| 2822 */ | 3058 */ |
| 2823 | 3059 |
| 2824 /* | 3060 /* |
| 2825 ** Return the serial-type for the value stored in pMem. | 3061 ** Return the serial-type for the value stored in pMem. |
| 2826 */ | 3062 */ |
| 2827 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){ | 3063 u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){ |
| 2828 int flags = pMem->flags; | 3064 int flags = pMem->flags; |
| 2829 u32 n; | 3065 u32 n; |
| 2830 | 3066 |
| 3067 assert( pLen!=0 ); |
| 2831 if( flags&MEM_Null ){ | 3068 if( flags&MEM_Null ){ |
| 3069 *pLen = 0; |
| 2832 return 0; | 3070 return 0; |
| 2833 } | 3071 } |
| 2834 if( flags&MEM_Int ){ | 3072 if( flags&MEM_Int ){ |
| 2835 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ | 3073 /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */ |
| 2836 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) | 3074 # define MAX_6BYTE ((((i64)0x00008000)<<32)-1) |
| 2837 i64 i = pMem->u.i; | 3075 i64 i = pMem->u.i; |
| 2838 u64 u; | 3076 u64 u; |
| 2839 if( i<0 ){ | 3077 if( i<0 ){ |
| 2840 if( i<(-MAX_6BYTE) ) return 6; | 3078 u = ~i; |
| 2841 /* Previous test prevents: u = -(-9223372036854775808) */ | |
| 2842 u = -i; | |
| 2843 }else{ | 3079 }else{ |
| 2844 u = i; | 3080 u = i; |
| 2845 } | 3081 } |
| 2846 if( u<=127 ){ | 3082 if( u<=127 ){ |
| 2847 return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1; | 3083 if( (i&1)==i && file_format>=4 ){ |
| 3084 *pLen = 0; |
| 3085 return 8+(u32)u; |
| 3086 }else{ |
| 3087 *pLen = 1; |
| 3088 return 1; |
| 3089 } |
| 2848 } | 3090 } |
| 2849 if( u<=32767 ) return 2; | 3091 if( u<=32767 ){ *pLen = 2; return 2; } |
| 2850 if( u<=8388607 ) return 3; | 3092 if( u<=8388607 ){ *pLen = 3; return 3; } |
| 2851 if( u<=2147483647 ) return 4; | 3093 if( u<=2147483647 ){ *pLen = 4; return 4; } |
| 2852 if( u<=MAX_6BYTE ) return 5; | 3094 if( u<=MAX_6BYTE ){ *pLen = 6; return 5; } |
| 3095 *pLen = 8; |
| 2853 return 6; | 3096 return 6; |
| 2854 } | 3097 } |
| 2855 if( flags&MEM_Real ){ | 3098 if( flags&MEM_Real ){ |
| 3099 *pLen = 8; |
| 2856 return 7; | 3100 return 7; |
| 2857 } | 3101 } |
| 2858 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); | 3102 assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) ); |
| 2859 assert( pMem->n>=0 ); | 3103 assert( pMem->n>=0 ); |
| 2860 n = (u32)pMem->n; | 3104 n = (u32)pMem->n; |
| 2861 if( flags & MEM_Zero ){ | 3105 if( flags & MEM_Zero ){ |
| 2862 n += pMem->u.nZero; | 3106 n += pMem->u.nZero; |
| 2863 } | 3107 } |
| 3108 *pLen = n; |
| 2864 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); | 3109 return ((n*2) + 12 + ((flags&MEM_Str)!=0)); |
| 2865 } | 3110 } |
| 2866 | 3111 |
| 2867 /* | 3112 /* |
| 3113 ** The sizes for serial types less than 128 |
| 3114 */ |
| 3115 static const u8 sqlite3SmallTypeSizes[] = { |
| 3116 /* 0 1 2 3 4 5 6 7 8 9 */ |
| 3117 /* 0 */ 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, |
| 3118 /* 10 */ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, |
| 3119 /* 20 */ 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, |
| 3120 /* 30 */ 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, |
| 3121 /* 40 */ 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, |
| 3122 /* 50 */ 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, |
| 3123 /* 60 */ 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, |
| 3124 /* 70 */ 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, |
| 3125 /* 80 */ 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, |
| 3126 /* 90 */ 39, 39, 40, 40, 41, 41, 42, 42, 43, 43, |
| 3127 /* 100 */ 44, 44, 45, 45, 46, 46, 47, 47, 48, 48, |
| 3128 /* 110 */ 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, |
| 3129 /* 120 */ 54, 54, 55, 55, 56, 56, 57, 57 |
| 3130 }; |
| 3131 |
| 3132 /* |
| 2868 ** Return the length of the data corresponding to the supplied serial-type. | 3133 ** Return the length of the data corresponding to the supplied serial-type. |
| 2869 */ | 3134 */ |
| 2870 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ | 3135 u32 sqlite3VdbeSerialTypeLen(u32 serial_type){ |
| 2871 if( serial_type>=12 ){ | 3136 if( serial_type>=128 ){ |
| 2872 return (serial_type-12)/2; | 3137 return (serial_type-12)/2; |
| 2873 }else{ | 3138 }else{ |
| 2874 static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 }; | 3139 assert( serial_type<12 |
| 2875 return aSize[serial_type]; | 3140 || sqlite3SmallTypeSizes[serial_type]==(serial_type - 12)/2 ); |
| 3141 return sqlite3SmallTypeSizes[serial_type]; |
| 2876 } | 3142 } |
| 2877 } | 3143 } |
| 3144 u8 sqlite3VdbeOneByteSerialTypeLen(u8 serial_type){ |
| 3145 assert( serial_type<128 ); |
| 3146 return sqlite3SmallTypeSizes[serial_type]; |
| 3147 } |
| 2878 | 3148 |
| 2879 /* | 3149 /* |
| 2880 ** If we are on an architecture with mixed-endian floating | 3150 ** If we are on an architecture with mixed-endian floating |
| 2881 ** points (ex: ARM7) then swap the lower 4 bytes with the | 3151 ** points (ex: ARM7) then swap the lower 4 bytes with the |
| 2882 ** upper 4 bytes. Return the result. | 3152 ** upper 4 bytes. Return the result. |
| 2883 ** | 3153 ** |
| 2884 ** For most architectures, this is a no-op. | 3154 ** For most architectures, this is a no-op. |
| 2885 ** | 3155 ** |
| 2886 ** (later): It is reported to me that the mixed-endian problem | 3156 ** (later): It is reported to me that the mixed-endian problem |
| 2887 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems | 3157 ** on ARM7 is an issue with GCC, not with the ARM7 chip. It seems |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2949 if( serial_type<=7 && serial_type>0 ){ | 3219 if( serial_type<=7 && serial_type>0 ){ |
| 2950 u64 v; | 3220 u64 v; |
| 2951 u32 i; | 3221 u32 i; |
| 2952 if( serial_type==7 ){ | 3222 if( serial_type==7 ){ |
| 2953 assert( sizeof(v)==sizeof(pMem->u.r) ); | 3223 assert( sizeof(v)==sizeof(pMem->u.r) ); |
| 2954 memcpy(&v, &pMem->u.r, sizeof(v)); | 3224 memcpy(&v, &pMem->u.r, sizeof(v)); |
| 2955 swapMixedEndianFloat(v); | 3225 swapMixedEndianFloat(v); |
| 2956 }else{ | 3226 }else{ |
| 2957 v = pMem->u.i; | 3227 v = pMem->u.i; |
| 2958 } | 3228 } |
| 2959 len = i = sqlite3VdbeSerialTypeLen(serial_type); | 3229 len = i = sqlite3SmallTypeSizes[serial_type]; |
| 2960 assert( i>0 ); | 3230 assert( i>0 ); |
| 2961 do{ | 3231 do{ |
| 2962 buf[--i] = (u8)(v&0xFF); | 3232 buf[--i] = (u8)(v&0xFF); |
| 2963 v >>= 8; | 3233 v >>= 8; |
| 2964 }while( i ); | 3234 }while( i ); |
| 2965 return len; | 3235 return len; |
| 2966 } | 3236 } |
| 2967 | 3237 |
| 2968 /* String or blob */ | 3238 /* String or blob */ |
| 2969 if( serial_type>=12 ){ | 3239 if( serial_type>=12 ){ |
| 2970 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) | 3240 assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0) |
| 2971 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); | 3241 == (int)sqlite3VdbeSerialTypeLen(serial_type) ); |
| 2972 len = pMem->n; | 3242 len = pMem->n; |
| 2973 memcpy(buf, pMem->z, len); | 3243 if( len>0 ) memcpy(buf, pMem->z, len); |
| 2974 return len; | 3244 return len; |
| 2975 } | 3245 } |
| 2976 | 3246 |
| 2977 /* NULL or constants 0 or 1 */ | 3247 /* NULL or constants 0 or 1 */ |
| 2978 return 0; | 3248 return 0; |
| 2979 } | 3249 } |
| 2980 | 3250 |
| 2981 /* Input "x" is a sequence of unsigned characters that represent a | 3251 /* Input "x" is a sequence of unsigned characters that represent a |
| 2982 ** big-endian integer. Return the equivalent native integer | 3252 ** big-endian integer. Return the equivalent native integer |
| 2983 */ | 3253 */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 2998 */ | 3268 */ |
| 2999 static u32 SQLITE_NOINLINE serialGet( | 3269 static u32 SQLITE_NOINLINE serialGet( |
| 3000 const unsigned char *buf, /* Buffer to deserialize from */ | 3270 const unsigned char *buf, /* Buffer to deserialize from */ |
| 3001 u32 serial_type, /* Serial type to deserialize */ | 3271 u32 serial_type, /* Serial type to deserialize */ |
| 3002 Mem *pMem /* Memory cell to write value into */ | 3272 Mem *pMem /* Memory cell to write value into */ |
| 3003 ){ | 3273 ){ |
| 3004 u64 x = FOUR_BYTE_UINT(buf); | 3274 u64 x = FOUR_BYTE_UINT(buf); |
| 3005 u32 y = FOUR_BYTE_UINT(buf+4); | 3275 u32 y = FOUR_BYTE_UINT(buf+4); |
| 3006 x = (x<<32) + y; | 3276 x = (x<<32) + y; |
| 3007 if( serial_type==6 ){ | 3277 if( serial_type==6 ){ |
| 3278 /* EVIDENCE-OF: R-29851-52272 Value is a big-endian 64-bit |
| 3279 ** twos-complement integer. */ |
| 3008 pMem->u.i = *(i64*)&x; | 3280 pMem->u.i = *(i64*)&x; |
| 3009 pMem->flags = MEM_Int; | 3281 pMem->flags = MEM_Int; |
| 3010 testcase( pMem->u.i<0 ); | 3282 testcase( pMem->u.i<0 ); |
| 3011 }else{ | 3283 }else{ |
| 3284 /* EVIDENCE-OF: R-57343-49114 Value is a big-endian IEEE 754-2008 64-bit |
| 3285 ** floating point number. */ |
| 3012 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) | 3286 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) |
| 3013 /* Verify that integers and floating point values use the same | 3287 /* Verify that integers and floating point values use the same |
| 3014 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is | 3288 ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is |
| 3015 ** defined that 64-bit floating point values really are mixed | 3289 ** defined that 64-bit floating point values really are mixed |
| 3016 ** endian. | 3290 ** endian. |
| 3017 */ | 3291 */ |
| 3018 static const u64 t1 = ((u64)0x3ff00000)<<32; | 3292 static const u64 t1 = ((u64)0x3ff00000)<<32; |
| 3019 static const double r1 = 1.0; | 3293 static const double r1 = 1.0; |
| 3020 u64 t2 = t1; | 3294 u64 t2 = t1; |
| 3021 swapMixedEndianFloat(t2); | 3295 swapMixedEndianFloat(t2); |
| 3022 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); | 3296 assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); |
| 3023 #endif | 3297 #endif |
| 3024 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); | 3298 assert( sizeof(x)==8 && sizeof(pMem->u.r)==8 ); |
| 3025 swapMixedEndianFloat(x); | 3299 swapMixedEndianFloat(x); |
| 3026 memcpy(&pMem->u.r, &x, sizeof(x)); | 3300 memcpy(&pMem->u.r, &x, sizeof(x)); |
| 3027 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; | 3301 pMem->flags = sqlite3IsNaN(pMem->u.r) ? MEM_Null : MEM_Real; |
| 3028 } | 3302 } |
| 3029 return 8; | 3303 return 8; |
| 3030 } | 3304 } |
| 3031 u32 sqlite3VdbeSerialGet( | 3305 u32 sqlite3VdbeSerialGet( |
| 3032 const unsigned char *buf, /* Buffer to deserialize from */ | 3306 const unsigned char *buf, /* Buffer to deserialize from */ |
| 3033 u32 serial_type, /* Serial type to deserialize */ | 3307 u32 serial_type, /* Serial type to deserialize */ |
| 3034 Mem *pMem /* Memory cell to write value into */ | 3308 Mem *pMem /* Memory cell to write value into */ |
| 3035 ){ | 3309 ){ |
| 3036 switch( serial_type ){ | 3310 switch( serial_type ){ |
| 3037 case 10: /* Reserved for future use */ | 3311 case 10: /* Reserved for future use */ |
| 3038 case 11: /* Reserved for future use */ | 3312 case 11: /* Reserved for future use */ |
| 3039 case 0: { /* NULL */ | 3313 case 0: { /* Null */ |
| 3314 /* EVIDENCE-OF: R-24078-09375 Value is a NULL. */ |
| 3040 pMem->flags = MEM_Null; | 3315 pMem->flags = MEM_Null; |
| 3041 break; | 3316 break; |
| 3042 } | 3317 } |
| 3043 case 1: { /* 1-byte signed integer */ | 3318 case 1: { |
| 3319 /* EVIDENCE-OF: R-44885-25196 Value is an 8-bit twos-complement |
| 3320 ** integer. */ |
| 3044 pMem->u.i = ONE_BYTE_INT(buf); | 3321 pMem->u.i = ONE_BYTE_INT(buf); |
| 3045 pMem->flags = MEM_Int; | 3322 pMem->flags = MEM_Int; |
| 3046 testcase( pMem->u.i<0 ); | 3323 testcase( pMem->u.i<0 ); |
| 3047 return 1; | 3324 return 1; |
| 3048 } | 3325 } |
| 3049 case 2: { /* 2-byte signed integer */ | 3326 case 2: { /* 2-byte signed integer */ |
| 3327 /* EVIDENCE-OF: R-49794-35026 Value is a big-endian 16-bit |
| 3328 ** twos-complement integer. */ |
| 3050 pMem->u.i = TWO_BYTE_INT(buf); | 3329 pMem->u.i = TWO_BYTE_INT(buf); |
| 3051 pMem->flags = MEM_Int; | 3330 pMem->flags = MEM_Int; |
| 3052 testcase( pMem->u.i<0 ); | 3331 testcase( pMem->u.i<0 ); |
| 3053 return 2; | 3332 return 2; |
| 3054 } | 3333 } |
| 3055 case 3: { /* 3-byte signed integer */ | 3334 case 3: { /* 3-byte signed integer */ |
| 3335 /* EVIDENCE-OF: R-37839-54301 Value is a big-endian 24-bit |
| 3336 ** twos-complement integer. */ |
| 3056 pMem->u.i = THREE_BYTE_INT(buf); | 3337 pMem->u.i = THREE_BYTE_INT(buf); |
| 3057 pMem->flags = MEM_Int; | 3338 pMem->flags = MEM_Int; |
| 3058 testcase( pMem->u.i<0 ); | 3339 testcase( pMem->u.i<0 ); |
| 3059 return 3; | 3340 return 3; |
| 3060 } | 3341 } |
| 3061 case 4: { /* 4-byte signed integer */ | 3342 case 4: { /* 4-byte signed integer */ |
| 3343 /* EVIDENCE-OF: R-01849-26079 Value is a big-endian 32-bit |
| 3344 ** twos-complement integer. */ |
| 3062 pMem->u.i = FOUR_BYTE_INT(buf); | 3345 pMem->u.i = FOUR_BYTE_INT(buf); |
| 3346 #ifdef __HP_cc |
| 3347 /* Work around a sign-extension bug in the HP compiler for HP/UX */ |
| 3348 if( buf[0]&0x80 ) pMem->u.i |= 0xffffffff80000000LL; |
| 3349 #endif |
| 3063 pMem->flags = MEM_Int; | 3350 pMem->flags = MEM_Int; |
| 3064 testcase( pMem->u.i<0 ); | 3351 testcase( pMem->u.i<0 ); |
| 3065 return 4; | 3352 return 4; |
| 3066 } | 3353 } |
| 3067 case 5: { /* 6-byte signed integer */ | 3354 case 5: { /* 6-byte signed integer */ |
| 3355 /* EVIDENCE-OF: R-50385-09674 Value is a big-endian 48-bit |
| 3356 ** twos-complement integer. */ |
| 3068 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); | 3357 pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); |
| 3069 pMem->flags = MEM_Int; | 3358 pMem->flags = MEM_Int; |
| 3070 testcase( pMem->u.i<0 ); | 3359 testcase( pMem->u.i<0 ); |
| 3071 return 6; | 3360 return 6; |
| 3072 } | 3361 } |
| 3073 case 6: /* 8-byte signed integer */ | 3362 case 6: /* 8-byte signed integer */ |
| 3074 case 7: { /* IEEE floating point */ | 3363 case 7: { /* IEEE floating point */ |
| 3075 /* These use local variables, so do them in a separate routine | 3364 /* These use local variables, so do them in a separate routine |
| 3076 ** to avoid having to move the frame pointer in the common case */ | 3365 ** to avoid having to move the frame pointer in the common case */ |
| 3077 return serialGet(buf,serial_type,pMem); | 3366 return serialGet(buf,serial_type,pMem); |
| 3078 } | 3367 } |
| 3079 case 8: /* Integer 0 */ | 3368 case 8: /* Integer 0 */ |
| 3080 case 9: { /* Integer 1 */ | 3369 case 9: { /* Integer 1 */ |
| 3370 /* EVIDENCE-OF: R-12976-22893 Value is the integer 0. */ |
| 3371 /* EVIDENCE-OF: R-18143-12121 Value is the integer 1. */ |
| 3081 pMem->u.i = serial_type-8; | 3372 pMem->u.i = serial_type-8; |
| 3082 pMem->flags = MEM_Int; | 3373 pMem->flags = MEM_Int; |
| 3083 return 0; | 3374 return 0; |
| 3084 } | 3375 } |
| 3085 default: { | 3376 default: { |
| 3377 /* EVIDENCE-OF: R-14606-31564 Value is a BLOB that is (N-12)/2 bytes in |
| 3378 ** length. |
| 3379 ** EVIDENCE-OF: R-28401-00140 Value is a string in the text encoding and |
| 3380 ** (N-13)/2 bytes in length. */ |
| 3086 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; | 3381 static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem }; |
| 3087 pMem->z = (char *)buf; | 3382 pMem->z = (char *)buf; |
| 3088 pMem->n = (serial_type-12)/2; | 3383 pMem->n = (serial_type-12)/2; |
| 3089 pMem->flags = aFlag[serial_type&1]; | 3384 pMem->flags = aFlag[serial_type&1]; |
| 3090 return pMem->n; | 3385 return pMem->n; |
| 3091 } | 3386 } |
| 3092 } | 3387 } |
| 3093 return 0; | 3388 return 0; |
| 3094 } | 3389 } |
| 3095 /* | 3390 /* |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3213 /* Compilers may complain that mem1.u.i is potentially uninitialized. | 3508 /* Compilers may complain that mem1.u.i is potentially uninitialized. |
| 3214 ** We could initialize it, as shown here, to silence those complaints. | 3509 ** We could initialize it, as shown here, to silence those complaints. |
| 3215 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing | 3510 ** But in fact, mem1.u.i will never actually be used uninitialized, and doing |
| 3216 ** the unnecessary initialization has a measurable negative performance | 3511 ** the unnecessary initialization has a measurable negative performance |
| 3217 ** impact, since this routine is a very high runner. And so, we choose | 3512 ** impact, since this routine is a very high runner. And so, we choose |
| 3218 ** to ignore the compiler warnings and leave this variable uninitialized. | 3513 ** to ignore the compiler warnings and leave this variable uninitialized. |
| 3219 */ | 3514 */ |
| 3220 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ | 3515 /* mem1.u.i = 0; // not needed, here to silence compiler warning */ |
| 3221 | 3516 |
| 3222 idx1 = getVarint32(aKey1, szHdr1); | 3517 idx1 = getVarint32(aKey1, szHdr1); |
| 3518 if( szHdr1>98307 ) return SQLITE_CORRUPT; |
| 3223 d1 = szHdr1; | 3519 d1 = szHdr1; |
| 3224 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); | 3520 assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB ); |
| 3225 assert( pKeyInfo->aSortOrder!=0 ); | 3521 assert( pKeyInfo->aSortOrder!=0 ); |
| 3226 assert( pKeyInfo->nField>0 ); | 3522 assert( pKeyInfo->nField>0 ); |
| 3227 assert( idx1<=szHdr1 || CORRUPT_DB ); | 3523 assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 3228 do{ | 3524 do{ |
| 3229 u32 serial_type1; | 3525 u32 serial_type1; |
| 3230 | 3526 |
| 3231 /* Read the serial types for the next element in each key. */ | 3527 /* Read the serial types for the next element in each key. */ |
| 3232 idx1 += getVarint32( aKey1+idx1, serial_type1 ); | 3528 idx1 += getVarint32( aKey1+idx1, serial_type1 ); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3274 debugCompareEnd: | 3570 debugCompareEnd: |
| 3275 if( desiredResult==0 && rc==0 ) return 1; | 3571 if( desiredResult==0 && rc==0 ) return 1; |
| 3276 if( desiredResult<0 && rc<0 ) return 1; | 3572 if( desiredResult<0 && rc<0 ) return 1; |
| 3277 if( desiredResult>0 && rc>0 ) return 1; | 3573 if( desiredResult>0 && rc>0 ) return 1; |
| 3278 if( CORRUPT_DB ) return 1; | 3574 if( CORRUPT_DB ) return 1; |
| 3279 if( pKeyInfo->db->mallocFailed ) return 1; | 3575 if( pKeyInfo->db->mallocFailed ) return 1; |
| 3280 return 0; | 3576 return 0; |
| 3281 } | 3577 } |
| 3282 #endif | 3578 #endif |
| 3283 | 3579 |
| 3580 #if SQLITE_DEBUG |
| 3581 /* |
| 3582 ** Count the number of fields (a.k.a. columns) in the record given by |
| 3583 ** pKey,nKey. The verify that this count is less than or equal to the |
| 3584 ** limit given by pKeyInfo->nField + pKeyInfo->nXField. |
| 3585 ** |
| 3586 ** If this constraint is not satisfied, it means that the high-speed |
| 3587 ** vdbeRecordCompareInt() and vdbeRecordCompareString() routines will |
| 3588 ** not work correctly. If this assert() ever fires, it probably means |
| 3589 ** that the KeyInfo.nField or KeyInfo.nXField values were computed |
| 3590 ** incorrectly. |
| 3591 */ |
| 3592 static void vdbeAssertFieldCountWithinLimits( |
| 3593 int nKey, const void *pKey, /* The record to verify */ |
| 3594 const KeyInfo *pKeyInfo /* Compare size with this KeyInfo */ |
| 3595 ){ |
| 3596 int nField = 0; |
| 3597 u32 szHdr; |
| 3598 u32 idx; |
| 3599 u32 notUsed; |
| 3600 const unsigned char *aKey = (const unsigned char*)pKey; |
| 3601 |
| 3602 if( CORRUPT_DB ) return; |
| 3603 idx = getVarint32(aKey, szHdr); |
| 3604 assert( nKey>=0 ); |
| 3605 assert( szHdr<=(u32)nKey ); |
| 3606 while( idx<szHdr ){ |
| 3607 idx += getVarint32(aKey+idx, notUsed); |
| 3608 nField++; |
| 3609 } |
| 3610 assert( nField <= pKeyInfo->nField+pKeyInfo->nXField ); |
| 3611 } |
| 3612 #else |
| 3613 # define vdbeAssertFieldCountWithinLimits(A,B,C) |
| 3614 #endif |
| 3615 |
| 3284 /* | 3616 /* |
| 3285 ** Both *pMem1 and *pMem2 contain string values. Compare the two values | 3617 ** Both *pMem1 and *pMem2 contain string values. Compare the two values |
| 3286 ** using the collation sequence pColl. As usual, return a negative , zero | 3618 ** using the collation sequence pColl. As usual, return a negative , zero |
| 3287 ** or positive value if *pMem1 is less than, equal to or greater than | 3619 ** or positive value if *pMem1 is less than, equal to or greater than |
| 3288 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". | 3620 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);". |
| 3289 */ | 3621 */ |
| 3290 static int vdbeCompareMemString( | 3622 static int vdbeCompareMemString( |
| 3291 const Mem *pMem1, | 3623 const Mem *pMem1, |
| 3292 const Mem *pMem2, | 3624 const Mem *pMem2, |
| 3293 const CollSeq *pColl, | 3625 const CollSeq *pColl, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 3323 ** Compare two blobs. Return negative, zero, or positive if the first | 3655 ** Compare two blobs. Return negative, zero, or positive if the first |
| 3324 ** is less than, equal to, or greater than the second, respectively. | 3656 ** is less than, equal to, or greater than the second, respectively. |
| 3325 ** If one blob is a prefix of the other, then the shorter is the lessor. | 3657 ** If one blob is a prefix of the other, then the shorter is the lessor. |
| 3326 */ | 3658 */ |
| 3327 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ | 3659 static SQLITE_NOINLINE int sqlite3BlobCompare(const Mem *pB1, const Mem *pB2){ |
| 3328 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); | 3660 int c = memcmp(pB1->z, pB2->z, pB1->n>pB2->n ? pB2->n : pB1->n); |
| 3329 if( c ) return c; | 3661 if( c ) return c; |
| 3330 return pB1->n - pB2->n; | 3662 return pB1->n - pB2->n; |
| 3331 } | 3663 } |
| 3332 | 3664 |
| 3665 /* |
| 3666 ** 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, |
| 3668 ** equal to, or greater than the second (double). |
| 3669 */ |
| 3670 static int sqlite3IntFloatCompare(i64 i, double r){ |
| 3671 if( sizeof(LONGDOUBLE_TYPE)>8 ){ |
| 3672 LONGDOUBLE_TYPE x = (LONGDOUBLE_TYPE)i; |
| 3673 if( x<r ) return -1; |
| 3674 if( x>r ) return +1; |
| 3675 return 0; |
| 3676 }else{ |
| 3677 i64 y; |
| 3678 double s; |
| 3679 if( r<-9223372036854775808.0 ) return +1; |
| 3680 if( r>9223372036854775807.0 ) return -1; |
| 3681 y = (i64)r; |
| 3682 if( i<y ) return -1; |
| 3683 if( i>y ){ |
| 3684 if( y==SMALLEST_INT64 && r>0.0 ) return -1; |
| 3685 return +1; |
| 3686 } |
| 3687 s = (double)i; |
| 3688 if( s<r ) return -1; |
| 3689 if( s>r ) return +1; |
| 3690 return 0; |
| 3691 } |
| 3692 } |
| 3333 | 3693 |
| 3334 /* | 3694 /* |
| 3335 ** Compare the values contained by the two memory cells, returning | 3695 ** Compare the values contained by the two memory cells, returning |
| 3336 ** negative, zero or positive if pMem1 is less than, equal to, or greater | 3696 ** negative, zero or positive if pMem1 is less than, equal to, or greater |
| 3337 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers | 3697 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers |
| 3338 ** and reals) sorted numerically, followed by text ordered by the collating | 3698 ** and reals) sorted numerically, followed by text ordered by the collating |
| 3339 ** sequence pColl and finally blob's ordered by memcmp(). | 3699 ** sequence pColl and finally blob's ordered by memcmp(). |
| 3340 ** | 3700 ** |
| 3341 ** Two NULL values are considered equal by this function. | 3701 ** Two NULL values are considered equal by this function. |
| 3342 */ | 3702 */ |
| 3343 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ | 3703 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ |
| 3344 int f1, f2; | 3704 int f1, f2; |
| 3345 int combined_flags; | 3705 int combined_flags; |
| 3346 | 3706 |
| 3347 f1 = pMem1->flags; | 3707 f1 = pMem1->flags; |
| 3348 f2 = pMem2->flags; | 3708 f2 = pMem2->flags; |
| 3349 combined_flags = f1|f2; | 3709 combined_flags = f1|f2; |
| 3350 assert( (combined_flags & MEM_RowSet)==0 ); | 3710 assert( (combined_flags & MEM_RowSet)==0 ); |
| 3351 | 3711 |
| 3352 /* If one value is NULL, it is less than the other. If both values | 3712 /* If one value is NULL, it is less than the other. If both values |
| 3353 ** are NULL, return 0. | 3713 ** are NULL, return 0. |
| 3354 */ | 3714 */ |
| 3355 if( combined_flags&MEM_Null ){ | 3715 if( combined_flags&MEM_Null ){ |
| 3356 return (f2&MEM_Null) - (f1&MEM_Null); | 3716 return (f2&MEM_Null) - (f1&MEM_Null); |
| 3357 } | 3717 } |
| 3358 | 3718 |
| 3359 /* If one value is a number and the other is not, the number is less. | 3719 /* At least one of the two values is a number |
| 3360 ** If both are numbers, compare as reals if one is a real, or as integers | |
| 3361 ** if both values are integers. | |
| 3362 */ | 3720 */ |
| 3363 if( combined_flags&(MEM_Int|MEM_Real) ){ | 3721 if( combined_flags&(MEM_Int|MEM_Real) ){ |
| 3364 double r1, r2; | |
| 3365 if( (f1 & f2 & MEM_Int)!=0 ){ | 3722 if( (f1 & f2 & MEM_Int)!=0 ){ |
| 3366 if( pMem1->u.i < pMem2->u.i ) return -1; | 3723 if( pMem1->u.i < pMem2->u.i ) return -1; |
| 3367 if( pMem1->u.i > pMem2->u.i ) return 1; | 3724 if( pMem1->u.i > pMem2->u.i ) return +1; |
| 3368 return 0; | 3725 return 0; |
| 3369 } | 3726 } |
| 3727 if( (f1 & f2 & MEM_Real)!=0 ){ |
| 3728 if( pMem1->u.r < pMem2->u.r ) return -1; |
| 3729 if( pMem1->u.r > pMem2->u.r ) return +1; |
| 3730 return 0; |
| 3731 } |
| 3732 if( (f1&MEM_Int)!=0 ){ |
| 3733 if( (f2&MEM_Real)!=0 ){ |
| 3734 return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r); |
| 3735 }else{ |
| 3736 return -1; |
| 3737 } |
| 3738 } |
| 3370 if( (f1&MEM_Real)!=0 ){ | 3739 if( (f1&MEM_Real)!=0 ){ |
| 3371 r1 = pMem1->u.r; | 3740 if( (f2&MEM_Int)!=0 ){ |
| 3372 }else if( (f1&MEM_Int)!=0 ){ | 3741 return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r); |
| 3373 r1 = (double)pMem1->u.i; | 3742 }else{ |
| 3374 }else{ | 3743 return -1; |
| 3375 return 1; | 3744 } |
| 3376 } | 3745 } |
| 3377 if( (f2&MEM_Real)!=0 ){ | 3746 return +1; |
| 3378 r2 = pMem2->u.r; | |
| 3379 }else if( (f2&MEM_Int)!=0 ){ | |
| 3380 r2 = (double)pMem2->u.i; | |
| 3381 }else{ | |
| 3382 return -1; | |
| 3383 } | |
| 3384 if( r1<r2 ) return -1; | |
| 3385 if( r1>r2 ) return 1; | |
| 3386 return 0; | |
| 3387 } | 3747 } |
| 3388 | 3748 |
| 3389 /* If one value is a string and the other is a blob, the string is less. | 3749 /* If one value is a string and the other is a blob, the string is less. |
| 3390 ** If both are strings, compare using the collating functions. | 3750 ** If both are strings, compare using the collating functions. |
| 3391 */ | 3751 */ |
| 3392 if( combined_flags&MEM_Str ){ | 3752 if( combined_flags&MEM_Str ){ |
| 3393 if( (f1 & MEM_Str)==0 ){ | 3753 if( (f1 & MEM_Str)==0 ){ |
| 3394 return 1; | 3754 return 1; |
| 3395 } | 3755 } |
| 3396 if( (f2 & MEM_Str)==0 ){ | 3756 if( (f2 & MEM_Str)==0 ){ |
| 3397 return -1; | 3757 return -1; |
| 3398 } | 3758 } |
| 3399 | 3759 |
| 3400 assert( pMem1->enc==pMem2->enc ); | 3760 assert( pMem1->enc==pMem2->enc || pMem1->db->mallocFailed ); |
| 3401 assert( pMem1->enc==SQLITE_UTF8 || | 3761 assert( pMem1->enc==SQLITE_UTF8 || |
| 3402 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); | 3762 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); |
| 3403 | 3763 |
| 3404 /* The collation sequence must be defined at this point, even if | 3764 /* The collation sequence must be defined at this point, even if |
| 3405 ** the user deletes the collation sequence after the vdbe program is | 3765 ** the user deletes the collation sequence after the vdbe program is |
| 3406 ** compiled (this was not always the case). | 3766 ** compiled (this was not always the case). |
| 3407 */ | 3767 */ |
| 3408 assert( !pColl || pColl->xCmp ); | 3768 assert( !pColl || pColl->xCmp ); |
| 3409 | 3769 |
| 3410 if( pColl ){ | 3770 if( pColl ){ |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3474 ** | 3834 ** |
| 3475 ** Key1 and Key2 do not have to contain the same number of fields. If all | 3835 ** Key1 and Key2 do not have to contain the same number of fields. If all |
| 3476 ** fields that appear in both keys are equal, then pPKey2->default_rc is | 3836 ** fields that appear in both keys are equal, then pPKey2->default_rc is |
| 3477 ** returned. | 3837 ** returned. |
| 3478 ** | 3838 ** |
| 3479 ** If database corruption is discovered, set pPKey2->errCode to | 3839 ** If database corruption is discovered, set pPKey2->errCode to |
| 3480 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, | 3840 ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, |
| 3481 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the | 3841 ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the |
| 3482 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). | 3842 ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). |
| 3483 */ | 3843 */ |
| 3484 static int vdbeRecordCompareWithSkip( | 3844 int sqlite3VdbeRecordCompareWithSkip( |
| 3485 int nKey1, const void *pKey1, /* Left key */ | 3845 int nKey1, const void *pKey1, /* Left key */ |
| 3486 UnpackedRecord *pPKey2, /* Right key */ | 3846 UnpackedRecord *pPKey2, /* Right key */ |
| 3487 int bSkip /* If true, skip the first field */ | 3847 int bSkip /* If true, skip the first field */ |
| 3488 ){ | 3848 ){ |
| 3489 u32 d1; /* Offset into aKey[] of next data element */ | 3849 u32 d1; /* Offset into aKey[] of next data element */ |
| 3490 int i; /* Index of next field to compare */ | 3850 int i; /* Index of next field to compare */ |
| 3491 u32 szHdr1; /* Size of record header in bytes */ | 3851 u32 szHdr1; /* Size of record header in bytes */ |
| 3492 u32 idx1; /* Offset of first type in header */ | 3852 u32 idx1; /* Offset of first type in header */ |
| 3493 int rc = 0; /* Return value */ | 3853 int rc = 0; /* Return value */ |
| 3494 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ | 3854 Mem *pRhs = pPKey2->aMem; /* Next field of pPKey2 to compare */ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 3522 assert( pPKey2->pKeyInfo->aSortOrder!=0 ); | 3882 assert( pPKey2->pKeyInfo->aSortOrder!=0 ); |
| 3523 assert( pPKey2->pKeyInfo->nField>0 ); | 3883 assert( pPKey2->pKeyInfo->nField>0 ); |
| 3524 assert( idx1<=szHdr1 || CORRUPT_DB ); | 3884 assert( idx1<=szHdr1 || CORRUPT_DB ); |
| 3525 do{ | 3885 do{ |
| 3526 u32 serial_type; | 3886 u32 serial_type; |
| 3527 | 3887 |
| 3528 /* RHS is an integer */ | 3888 /* RHS is an integer */ |
| 3529 if( pRhs->flags & MEM_Int ){ | 3889 if( pRhs->flags & MEM_Int ){ |
| 3530 serial_type = aKey1[idx1]; | 3890 serial_type = aKey1[idx1]; |
| 3531 testcase( serial_type==12 ); | 3891 testcase( serial_type==12 ); |
| 3532 if( serial_type>=12 ){ | 3892 if( serial_type>=10 ){ |
| 3533 rc = +1; | 3893 rc = +1; |
| 3534 }else if( serial_type==0 ){ | 3894 }else if( serial_type==0 ){ |
| 3535 rc = -1; | 3895 rc = -1; |
| 3536 }else if( serial_type==7 ){ | 3896 }else if( serial_type==7 ){ |
| 3537 double rhs = (double)pRhs->u.i; | |
| 3538 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); | 3897 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 3539 if( mem1.u.r<rhs ){ | 3898 rc = -sqlite3IntFloatCompare(pRhs->u.i, mem1.u.r); |
| 3540 rc = -1; | |
| 3541 }else if( mem1.u.r>rhs ){ | |
| 3542 rc = +1; | |
| 3543 } | |
| 3544 }else{ | 3899 }else{ |
| 3545 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); | 3900 i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]); |
| 3546 i64 rhs = pRhs->u.i; | 3901 i64 rhs = pRhs->u.i; |
| 3547 if( lhs<rhs ){ | 3902 if( lhs<rhs ){ |
| 3548 rc = -1; | 3903 rc = -1; |
| 3549 }else if( lhs>rhs ){ | 3904 }else if( lhs>rhs ){ |
| 3550 rc = +1; | 3905 rc = +1; |
| 3551 } | 3906 } |
| 3552 } | 3907 } |
| 3553 } | 3908 } |
| 3554 | 3909 |
| 3555 /* RHS is real */ | 3910 /* RHS is real */ |
| 3556 else if( pRhs->flags & MEM_Real ){ | 3911 else if( pRhs->flags & MEM_Real ){ |
| 3557 serial_type = aKey1[idx1]; | 3912 serial_type = aKey1[idx1]; |
| 3558 if( serial_type>=12 ){ | 3913 if( serial_type>=10 ){ |
| 3914 /* Serial types 12 or greater are strings and blobs (greater than |
| 3915 ** numbers). Types 10 and 11 are currently "reserved for future |
| 3916 ** use", so it doesn't really matter what the results of comparing |
| 3917 ** them to numberic values are. */ |
| 3559 rc = +1; | 3918 rc = +1; |
| 3560 }else if( serial_type==0 ){ | 3919 }else if( serial_type==0 ){ |
| 3561 rc = -1; | 3920 rc = -1; |
| 3562 }else{ | 3921 }else{ |
| 3563 double rhs = pRhs->u.r; | |
| 3564 double lhs; | |
| 3565 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); | 3922 sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1); |
| 3566 if( serial_type==7 ){ | 3923 if( serial_type==7 ){ |
| 3567 lhs = mem1.u.r; | 3924 if( mem1.u.r<pRhs->u.r ){ |
| 3925 rc = -1; |
| 3926 }else if( mem1.u.r>pRhs->u.r ){ |
| 3927 rc = +1; |
| 3928 } |
| 3568 }else{ | 3929 }else{ |
| 3569 lhs = (double)mem1.u.i; | 3930 rc = sqlite3IntFloatCompare(mem1.u.i, pRhs->u.r); |
| 3570 } | |
| 3571 if( lhs<rhs ){ | |
| 3572 rc = -1; | |
| 3573 }else if( lhs>rhs ){ | |
| 3574 rc = +1; | |
| 3575 } | 3931 } |
| 3576 } | 3932 } |
| 3577 } | 3933 } |
| 3578 | 3934 |
| 3579 /* RHS is a string */ | 3935 /* RHS is a string */ |
| 3580 else if( pRhs->flags & MEM_Str ){ | 3936 else if( pRhs->flags & MEM_Str ){ |
| 3581 getVarint32(&aKey1[idx1], serial_type); | 3937 getVarint32(&aKey1[idx1], serial_type); |
| 3582 testcase( serial_type==12 ); | 3938 testcase( serial_type==12 ); |
| 3583 if( serial_type<12 ){ | 3939 if( serial_type<12 ){ |
| 3584 rc = -1; | 3940 rc = -1; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3654 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ | 4010 ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1). */ |
| 3655 assert( mem1.szMalloc==0 ); | 4011 assert( mem1.szMalloc==0 ); |
| 3656 | 4012 |
| 3657 /* rc==0 here means that one or both of the keys ran out of fields and | 4013 /* rc==0 here means that one or both of the keys ran out of fields and |
| 3658 ** all the fields up to that point were equal. Return the default_rc | 4014 ** all the fields up to that point were equal. Return the default_rc |
| 3659 ** value. */ | 4015 ** value. */ |
| 3660 assert( CORRUPT_DB | 4016 assert( CORRUPT_DB |
| 3661 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) | 4017 || vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, pPKey2->default_rc) |
| 3662 || pKeyInfo->db->mallocFailed | 4018 || pKeyInfo->db->mallocFailed |
| 3663 ); | 4019 ); |
| 4020 pPKey2->eqSeen = 1; |
| 3664 return pPKey2->default_rc; | 4021 return pPKey2->default_rc; |
| 3665 } | 4022 } |
| 3666 int sqlite3VdbeRecordCompare( | 4023 int sqlite3VdbeRecordCompare( |
| 3667 int nKey1, const void *pKey1, /* Left key */ | 4024 int nKey1, const void *pKey1, /* Left key */ |
| 3668 UnpackedRecord *pPKey2 /* Right key */ | 4025 UnpackedRecord *pPKey2 /* Right key */ |
| 3669 ){ | 4026 ){ |
| 3670 return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); | 4027 return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); |
| 3671 } | 4028 } |
| 3672 | 4029 |
| 3673 | 4030 |
| 3674 /* | 4031 /* |
| 3675 ** This function is an optimized version of sqlite3VdbeRecordCompare() | 4032 ** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 3676 ** that (a) the first field of pPKey2 is an integer, and (b) the | 4033 ** that (a) the first field of pPKey2 is an integer, and (b) the |
| 3677 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single | 4034 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single |
| 3678 ** byte (i.e. is less than 128). | 4035 ** byte (i.e. is less than 128). |
| 3679 ** | 4036 ** |
| 3680 ** To avoid concerns about buffer overreads, this routine is only used | 4037 ** To avoid concerns about buffer overreads, this routine is only used |
| 3681 ** on schemas where the maximum valid header size is 63 bytes or less. | 4038 ** on schemas where the maximum valid header size is 63 bytes or less. |
| 3682 */ | 4039 */ |
| 3683 static int vdbeRecordCompareInt( | 4040 static int vdbeRecordCompareInt( |
| 3684 int nKey1, const void *pKey1, /* Left key */ | 4041 int nKey1, const void *pKey1, /* Left key */ |
| 3685 UnpackedRecord *pPKey2 /* Right key */ | 4042 UnpackedRecord *pPKey2 /* Right key */ |
| 3686 ){ | 4043 ){ |
| 3687 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; | 4044 const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; |
| 3688 int serial_type = ((const u8*)pKey1)[1]; | 4045 int serial_type = ((const u8*)pKey1)[1]; |
| 3689 int res; | 4046 int res; |
| 3690 u32 y; | 4047 u32 y; |
| 3691 u64 x; | 4048 u64 x; |
| 3692 i64 v = pPKey2->aMem[0].u.i; | 4049 i64 v = pPKey2->aMem[0].u.i; |
| 3693 i64 lhs; | 4050 i64 lhs; |
| 3694 | 4051 |
| 4052 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); |
| 3695 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); | 4053 assert( (*(u8*)pKey1)<=0x3F || CORRUPT_DB ); |
| 3696 switch( serial_type ){ | 4054 switch( serial_type ){ |
| 3697 case 1: { /* 1-byte signed integer */ | 4055 case 1: { /* 1-byte signed integer */ |
| 3698 lhs = ONE_BYTE_INT(aKey); | 4056 lhs = ONE_BYTE_INT(aKey); |
| 3699 testcase( lhs<0 ); | 4057 testcase( lhs<0 ); |
| 3700 break; | 4058 break; |
| 3701 } | 4059 } |
| 3702 case 2: { /* 2-byte signed integer */ | 4060 case 2: { /* 2-byte signed integer */ |
| 3703 lhs = TWO_BYTE_INT(aKey); | 4061 lhs = TWO_BYTE_INT(aKey); |
| 3704 testcase( lhs<0 ); | 4062 testcase( lhs<0 ); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3747 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); | 4105 return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2); |
| 3748 } | 4106 } |
| 3749 | 4107 |
| 3750 if( v>lhs ){ | 4108 if( v>lhs ){ |
| 3751 res = pPKey2->r1; | 4109 res = pPKey2->r1; |
| 3752 }else if( v<lhs ){ | 4110 }else if( v<lhs ){ |
| 3753 res = pPKey2->r2; | 4111 res = pPKey2->r2; |
| 3754 }else if( pPKey2->nField>1 ){ | 4112 }else if( pPKey2->nField>1 ){ |
| 3755 /* The first fields of the two keys are equal. Compare the trailing | 4113 /* The first fields of the two keys are equal. Compare the trailing |
| 3756 ** fields. */ | 4114 ** fields. */ |
| 3757 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); | 4115 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 3758 }else{ | 4116 }else{ |
| 3759 /* The first fields of the two keys are equal and there are no trailing | 4117 /* The first fields of the two keys are equal and there are no trailing |
| 3760 ** fields. Return pPKey2->default_rc in this case. */ | 4118 ** fields. Return pPKey2->default_rc in this case. */ |
| 3761 res = pPKey2->default_rc; | 4119 res = pPKey2->default_rc; |
| 4120 pPKey2->eqSeen = 1; |
| 3762 } | 4121 } |
| 3763 | 4122 |
| 3764 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); | 4123 assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res) ); |
| 3765 return res; | 4124 return res; |
| 3766 } | 4125 } |
| 3767 | 4126 |
| 3768 /* | 4127 /* |
| 3769 ** This function is an optimized version of sqlite3VdbeRecordCompare() | 4128 ** This function is an optimized version of sqlite3VdbeRecordCompare() |
| 3770 ** that (a) the first field of pPKey2 is a string, that (b) the first field | 4129 ** that (a) the first field of pPKey2 is a string, that (b) the first field |
| 3771 ** uses the collation sequence BINARY and (c) that the size-of-header varint | 4130 ** uses the collation sequence BINARY and (c) that the size-of-header varint |
| 3772 ** at the start of (pKey1/nKey1) fits in a single byte. | 4131 ** at the start of (pKey1/nKey1) fits in a single byte. |
| 3773 */ | 4132 */ |
| 3774 static int vdbeRecordCompareString( | 4133 static int vdbeRecordCompareString( |
| 3775 int nKey1, const void *pKey1, /* Left key */ | 4134 int nKey1, const void *pKey1, /* Left key */ |
| 3776 UnpackedRecord *pPKey2 /* Right key */ | 4135 UnpackedRecord *pPKey2 /* Right key */ |
| 3777 ){ | 4136 ){ |
| 3778 const u8 *aKey1 = (const u8*)pKey1; | 4137 const u8 *aKey1 = (const u8*)pKey1; |
| 3779 int serial_type; | 4138 int serial_type; |
| 3780 int res; | 4139 int res; |
| 3781 | 4140 |
| 4141 assert( pPKey2->aMem[0].flags & MEM_Str ); |
| 4142 vdbeAssertFieldCountWithinLimits(nKey1, pKey1, pPKey2->pKeyInfo); |
| 3782 getVarint32(&aKey1[1], serial_type); | 4143 getVarint32(&aKey1[1], serial_type); |
| 3783 if( serial_type<12 ){ | 4144 if( serial_type<12 ){ |
| 3784 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ | 4145 res = pPKey2->r1; /* (pKey1/nKey1) is a number or a null */ |
| 3785 }else if( !(serial_type & 0x01) ){ | 4146 }else if( !(serial_type & 0x01) ){ |
| 3786 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ | 4147 res = pPKey2->r2; /* (pKey1/nKey1) is a blob */ |
| 3787 }else{ | 4148 }else{ |
| 3788 int nCmp; | 4149 int nCmp; |
| 3789 int nStr; | 4150 int nStr; |
| 3790 int szHdr = aKey1[0]; | 4151 int szHdr = aKey1[0]; |
| 3791 | 4152 |
| 3792 nStr = (serial_type-12) / 2; | 4153 nStr = (serial_type-12) / 2; |
| 3793 if( (szHdr + nStr) > nKey1 ){ | 4154 if( (szHdr + nStr) > nKey1 ){ |
| 3794 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; | 4155 pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT; |
| 3795 return 0; /* Corruption */ | 4156 return 0; /* Corruption */ |
| 3796 } | 4157 } |
| 3797 nCmp = MIN( pPKey2->aMem[0].n, nStr ); | 4158 nCmp = MIN( pPKey2->aMem[0].n, nStr ); |
| 3798 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); | 4159 res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp); |
| 3799 | 4160 |
| 3800 if( res==0 ){ | 4161 if( res==0 ){ |
| 3801 res = nStr - pPKey2->aMem[0].n; | 4162 res = nStr - pPKey2->aMem[0].n; |
| 3802 if( res==0 ){ | 4163 if( res==0 ){ |
| 3803 if( pPKey2->nField>1 ){ | 4164 if( pPKey2->nField>1 ){ |
| 3804 res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); | 4165 res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 3805 }else{ | 4166 }else{ |
| 3806 res = pPKey2->default_rc; | 4167 res = pPKey2->default_rc; |
| 4168 pPKey2->eqSeen = 1; |
| 3807 } | 4169 } |
| 3808 }else if( res>0 ){ | 4170 }else if( res>0 ){ |
| 3809 res = pPKey2->r2; | 4171 res = pPKey2->r2; |
| 3810 }else{ | 4172 }else{ |
| 3811 res = pPKey2->r1; | 4173 res = pPKey2->r1; |
| 3812 } | 4174 } |
| 3813 }else if( res>0 ){ | 4175 }else if( res>0 ){ |
| 3814 res = pPKey2->r2; | 4176 res = pPKey2->r2; |
| 3815 }else{ | 4177 }else{ |
| 3816 res = pPKey2->r1; | 4178 res = pPKey2->r1; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3915 testcase( typeRowid==2 ); | 4277 testcase( typeRowid==2 ); |
| 3916 testcase( typeRowid==3 ); | 4278 testcase( typeRowid==3 ); |
| 3917 testcase( typeRowid==4 ); | 4279 testcase( typeRowid==4 ); |
| 3918 testcase( typeRowid==5 ); | 4280 testcase( typeRowid==5 ); |
| 3919 testcase( typeRowid==6 ); | 4281 testcase( typeRowid==6 ); |
| 3920 testcase( typeRowid==8 ); | 4282 testcase( typeRowid==8 ); |
| 3921 testcase( typeRowid==9 ); | 4283 testcase( typeRowid==9 ); |
| 3922 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ | 4284 if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){ |
| 3923 goto idx_rowid_corruption; | 4285 goto idx_rowid_corruption; |
| 3924 } | 4286 } |
| 3925 lenRowid = sqlite3VdbeSerialTypeLen(typeRowid); | 4287 lenRowid = sqlite3SmallTypeSizes[typeRowid]; |
| 3926 testcase( (u32)m.n==szHdr+lenRowid ); | 4288 testcase( (u32)m.n==szHdr+lenRowid ); |
| 3927 if( unlikely((u32)m.n<szHdr+lenRowid) ){ | 4289 if( unlikely((u32)m.n<szHdr+lenRowid) ){ |
| 3928 goto idx_rowid_corruption; | 4290 goto idx_rowid_corruption; |
| 3929 } | 4291 } |
| 3930 | 4292 |
| 3931 /* Fetch the integer off the end of the index record */ | 4293 /* Fetch the integer off the end of the index record */ |
| 3932 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); | 4294 sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v); |
| 3933 *rowid = v.u.i; | 4295 *rowid = v.u.i; |
| 3934 sqlite3VdbeMemRelease(&m); | 4296 sqlite3VdbeMemRelease(&m); |
| 3935 return SQLITE_OK; | 4297 return SQLITE_OK; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 3954 ** of the keys prior to the final rowid, not the entire key. | 4316 ** of the keys prior to the final rowid, not the entire key. |
| 3955 */ | 4317 */ |
| 3956 int sqlite3VdbeIdxKeyCompare( | 4318 int sqlite3VdbeIdxKeyCompare( |
| 3957 sqlite3 *db, /* Database connection */ | 4319 sqlite3 *db, /* Database connection */ |
| 3958 VdbeCursor *pC, /* The cursor to compare against */ | 4320 VdbeCursor *pC, /* The cursor to compare against */ |
| 3959 UnpackedRecord *pUnpacked, /* Unpacked version of key */ | 4321 UnpackedRecord *pUnpacked, /* Unpacked version of key */ |
| 3960 int *res /* Write the comparison result here */ | 4322 int *res /* Write the comparison result here */ |
| 3961 ){ | 4323 ){ |
| 3962 i64 nCellKey = 0; | 4324 i64 nCellKey = 0; |
| 3963 int rc; | 4325 int rc; |
| 3964 BtCursor *pCur = pC->pCursor; | 4326 BtCursor *pCur; |
| 3965 Mem m; | 4327 Mem m; |
| 3966 | 4328 |
| 4329 assert( pC->eCurType==CURTYPE_BTREE ); |
| 4330 pCur = pC->uc.pCursor; |
| 3967 assert( sqlite3BtreeCursorIsValid(pCur) ); | 4331 assert( sqlite3BtreeCursorIsValid(pCur) ); |
| 3968 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); | 4332 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey); |
| 3969 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ | 4333 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */ |
| 3970 /* nCellKey will always be between 0 and 0xffffffff because of the way | 4334 /* nCellKey will always be between 0 and 0xffffffff because of the way |
| 3971 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ | 4335 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */ |
| 3972 if( nCellKey<=0 || nCellKey>0x7fffffff ){ | 4336 if( nCellKey<=0 || nCellKey>0x7fffffff ){ |
| 3973 *res = 0; | 4337 *res = 0; |
| 3974 return SQLITE_CORRUPT_BKPT; | 4338 return SQLITE_CORRUPT_BKPT; |
| 3975 } | 4339 } |
| 3976 sqlite3VdbeMemInit(&m, db, 0); | 4340 sqlite3VdbeMemInit(&m, db, 0); |
| 3977 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m); | 4341 rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m); |
| 3978 if( rc ){ | 4342 if( rc ){ |
| 3979 return rc; | 4343 return rc; |
| 3980 } | 4344 } |
| 3981 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); | 4345 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked); |
| 3982 sqlite3VdbeMemRelease(&m); | 4346 sqlite3VdbeMemRelease(&m); |
| 3983 return SQLITE_OK; | 4347 return SQLITE_OK; |
| 3984 } | 4348 } |
| 3985 | 4349 |
| 3986 /* | 4350 /* |
| 3987 ** This routine sets the value to be returned by subsequent calls to | 4351 ** This routine sets the value to be returned by subsequent calls to |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4070 ** in memory obtained from sqlite3DbMalloc). | 4434 ** in memory obtained from sqlite3DbMalloc). |
| 4071 */ | 4435 */ |
| 4072 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ | 4436 void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){ |
| 4073 sqlite3 *db = p->db; | 4437 sqlite3 *db = p->db; |
| 4074 sqlite3DbFree(db, p->zErrMsg); | 4438 sqlite3DbFree(db, p->zErrMsg); |
| 4075 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); | 4439 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg); |
| 4076 sqlite3_free(pVtab->zErrMsg); | 4440 sqlite3_free(pVtab->zErrMsg); |
| 4077 pVtab->zErrMsg = 0; | 4441 pVtab->zErrMsg = 0; |
| 4078 } | 4442 } |
| 4079 #endif /* SQLITE_OMIT_VIRTUALTABLE */ | 4443 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| OLD | NEW |