1 /* 2 ** 2004 May 26 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** 13 ** This file contains code use to manipulate "Mem" structure. A "Mem" 14 ** stores a single value in the VDBE. Mem is an opaque structure visible 15 ** only within the VDBE. Interface routines refer to a Mem using the 16 ** name sqlite_value 17 */ 18 #include "sqliteInt.h" 19 #include "vdbeInt.h" 20 21 #ifdef SQLITE_DEBUG 22 /* 23 ** Check invariants on a Mem object. 24 ** 25 ** This routine is intended for use inside of assert() statements, like 26 ** this: assert( sqlite3VdbeCheckMemInvariants(pMem) ); 27 */ 28 int sqlite3VdbeCheckMemInvariants(Mem *p){ 29 /* If MEM_Dyn is set then Mem.xDel!=0. 30 ** Mem.xDel is might not be initialized if MEM_Dyn is clear. 31 */ 32 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 ); 33 34 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we 35 ** ensure that if Mem.szMalloc>0 then it is safe to do 36 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn. 37 ** That saves a few cycles in inner loops. */ 38 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 ); 39 40 /* Cannot be both MEM_Int and MEM_Real at the same time */ 41 assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) ); 42 43 /* Cannot be both MEM_Null and some other type */ 44 assert( (p->flags & MEM_Null)==0 || 45 (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob))==0 ); 46 47 /* The szMalloc field holds the correct memory allocation size */ 48 assert( p->szMalloc==0 49 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) ); 50 51 /* If p holds a string or blob, the Mem.z must point to exactly 52 ** one of the following: 53 ** 54 ** (1) Memory in Mem.zMalloc and managed by the Mem object 55 ** (2) Memory to be freed using Mem.xDel 56 ** (3) An ephemeral string or blob 57 ** (4) A static string or blob 58 */ 59 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){ 60 assert( 61 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) + 62 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) + 63 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) + 64 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1 65 ); 66 } 67 return 1; 68 } 69 #endif 70 71 72 /* 73 ** If pMem is an object with a valid string representation, this routine 74 ** ensures the internal encoding for the string representation is 75 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. 76 ** 77 ** If pMem is not a string object, or the encoding of the string 78 ** representation is already stored using the requested encoding, then this 79 ** routine is a no-op. 80 ** 81 ** SQLITE_OK is returned if the conversion is successful (or not required). 82 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion 83 ** between formats. 84 */ 85 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ 86 #ifndef SQLITE_OMIT_UTF16 87 int rc; 88 #endif 89 assert( (pMem->flags&MEM_RowSet)==0 ); 90 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE 91 || desiredEnc==SQLITE_UTF16BE ); 92 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ 93 return SQLITE_OK; 94 } 95 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 96 #ifdef SQLITE_OMIT_UTF16 97 return SQLITE_ERROR; 98 #else 99 100 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, 101 ** then the encoding of the value may not have changed. 102 */ 103 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc); 104 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); 105 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); 106 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); 107 return rc; 108 #endif 109 } 110 111 /* 112 ** Make sure pMem->z points to a writable allocation of at least 113 ** min(n,32) bytes. 114 ** 115 ** If the bPreserve argument is true, then copy of the content of 116 ** pMem->z into the new allocation. pMem must be either a string or 117 ** blob if bPreserve is true. If bPreserve is false, any prior content 118 ** in pMem->z is discarded. 119 */ 120 SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){ 121 assert( sqlite3VdbeCheckMemInvariants(pMem) ); 122 assert( (pMem->flags&MEM_RowSet)==0 ); 123 testcase( pMem->db==0 ); 124 125 /* If the bPreserve flag is set to true, then the memory cell must already 126 ** contain a valid string or blob value. */ 127 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) ); 128 testcase( bPreserve && pMem->z==0 ); 129 130 assert( pMem->szMalloc==0 131 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) ); 132 if( n<32 ) n = 32; 133 if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){ 134 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); 135 bPreserve = 0; 136 }else{ 137 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); 138 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); 139 } 140 if( pMem->zMalloc==0 ){ 141 sqlite3VdbeMemSetNull(pMem); 142 pMem->z = 0; 143 pMem->szMalloc = 0; 144 return SQLITE_NOMEM_BKPT; 145 }else{ 146 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); 147 } 148 149 if( bPreserve && pMem->z && ALWAYS(pMem->z!=pMem->zMalloc) ){ 150 memcpy(pMem->zMalloc, pMem->z, pMem->n); 151 } 152 if( (pMem->flags&MEM_Dyn)!=0 ){ 153 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC ); 154 pMem->xDel((void *)(pMem->z)); 155 } 156 157 pMem->z = pMem->zMalloc; 158 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static); 159 return SQLITE_OK; 160 } 161 162 /* 163 ** Change the pMem->zMalloc allocation to be at least szNew bytes. 164 ** If pMem->zMalloc already meets or exceeds the requested size, this 165 ** routine is a no-op. 166 ** 167 ** Any prior string or blob content in the pMem object may be discarded. 168 ** The pMem->xDel destructor is called, if it exists. Though MEM_Str 169 ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null 170 ** values are preserved. 171 ** 172 ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM) 173 ** if unable to complete the resizing. 174 */ 175 int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){ 176 assert( szNew>0 ); 177 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 ); 178 if( pMem->szMalloc<szNew ){ 179 return sqlite3VdbeMemGrow(pMem, szNew, 0); 180 } 181 assert( (pMem->flags & MEM_Dyn)==0 ); 182 pMem->z = pMem->zMalloc; 183 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real); 184 return SQLITE_OK; 185 } 186 187 /* 188 ** Change pMem so that its MEM_Str or MEM_Blob value is stored in 189 ** MEM.zMalloc, where it can be safely written. 190 ** 191 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. 192 */ 193 int sqlite3VdbeMemMakeWriteable(Mem *pMem){ 194 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 195 assert( (pMem->flags&MEM_RowSet)==0 ); 196 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){ 197 if( ExpandBlob(pMem) ) return SQLITE_NOMEM; 198 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){ 199 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ 200 return SQLITE_NOMEM_BKPT; 201 } 202 pMem->z[pMem->n] = 0; 203 pMem->z[pMem->n+1] = 0; 204 pMem->flags |= MEM_Term; 205 } 206 } 207 pMem->flags &= ~MEM_Ephem; 208 #ifdef SQLITE_DEBUG 209 pMem->pScopyFrom = 0; 210 #endif 211 212 return SQLITE_OK; 213 } 214 215 /* 216 ** If the given Mem* has a zero-filled tail, turn it into an ordinary 217 ** blob stored in dynamically allocated space. 218 */ 219 #ifndef SQLITE_OMIT_INCRBLOB 220 int sqlite3VdbeMemExpandBlob(Mem *pMem){ 221 int nByte; 222 assert( pMem->flags & MEM_Zero ); 223 assert( pMem->flags&MEM_Blob ); 224 assert( (pMem->flags&MEM_RowSet)==0 ); 225 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 226 227 /* Set nByte to the number of bytes required to store the expanded blob. */ 228 nByte = pMem->n + pMem->u.nZero; 229 if( nByte<=0 ){ 230 nByte = 1; 231 } 232 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ 233 return SQLITE_NOMEM_BKPT; 234 } 235 236 memset(&pMem->z[pMem->n], 0, pMem->u.nZero); 237 pMem->n += pMem->u.nZero; 238 pMem->flags &= ~(MEM_Zero|MEM_Term); 239 return SQLITE_OK; 240 } 241 #endif 242 243 /* 244 ** It is already known that pMem contains an unterminated string. 245 ** Add the zero terminator. 246 */ 247 static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){ 248 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ 249 return SQLITE_NOMEM_BKPT; 250 } 251 pMem->z[pMem->n] = 0; 252 pMem->z[pMem->n+1] = 0; 253 pMem->flags |= MEM_Term; 254 return SQLITE_OK; 255 } 256 257 /* 258 ** Make sure the given Mem is \u0000 terminated. 259 */ 260 int sqlite3VdbeMemNulTerminate(Mem *pMem){ 261 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 262 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) ); 263 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 ); 264 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){ 265 return SQLITE_OK; /* Nothing to do */ 266 }else{ 267 return vdbeMemAddTerminator(pMem); 268 } 269 } 270 271 /* 272 ** Add MEM_Str to the set of representations for the given Mem. Numbers 273 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string 274 ** is a no-op. 275 ** 276 ** Existing representations MEM_Int and MEM_Real are invalidated if 277 ** bForce is true but are retained if bForce is false. 278 ** 279 ** A MEM_Null value will never be passed to this function. This function is 280 ** used for converting values to text for returning to the user (i.e. via 281 ** sqlite3_value_text()), or for ensuring that values to be used as btree 282 ** keys are strings. In the former case a NULL pointer is returned the 283 ** user and the latter is an internal programming error. 284 */ 285 int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){ 286 int fg = pMem->flags; 287 const int nByte = 32; 288 289 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 290 assert( !(fg&MEM_Zero) ); 291 assert( !(fg&(MEM_Str|MEM_Blob)) ); 292 assert( fg&(MEM_Int|MEM_Real) ); 293 assert( (pMem->flags&MEM_RowSet)==0 ); 294 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 295 296 297 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){ 298 pMem->enc = 0; 299 return SQLITE_NOMEM_BKPT; 300 } 301 302 /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8 303 ** string representation of the value. Then, if the required encoding 304 ** is UTF-16le or UTF-16be do a translation. 305 ** 306 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. 307 */ 308 if( fg & MEM_Int ){ 309 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); 310 }else{ 311 assert( fg & MEM_Real ); 312 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r); 313 } 314 pMem->n = sqlite3Strlen30(pMem->z); 315 pMem->enc = SQLITE_UTF8; 316 pMem->flags |= MEM_Str|MEM_Term; 317 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real); 318 sqlite3VdbeChangeEncoding(pMem, enc); 319 return SQLITE_OK; 320 } 321 322 /* 323 ** Memory cell pMem contains the context of an aggregate function. 324 ** This routine calls the finalize method for that function. The 325 ** result of the aggregate is stored back into pMem. 326 ** 327 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK 328 ** otherwise. 329 */ 330 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ 331 int rc = SQLITE_OK; 332 if( ALWAYS(pFunc && pFunc->xFinalize) ){ 333 sqlite3_context ctx; 334 Mem t; 335 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); 336 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 337 memset(&ctx, 0, sizeof(ctx)); 338 memset(&t, 0, sizeof(t)); 339 t.flags = MEM_Null; 340 t.db = pMem->db; 341 ctx.pOut = &t; 342 ctx.pMem = pMem; 343 ctx.pFunc = pFunc; 344 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */ 345 assert( (pMem->flags & MEM_Dyn)==0 ); 346 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc); 347 memcpy(pMem, &t, sizeof(t)); 348 rc = ctx.isError; 349 } 350 return rc; 351 } 352 353 /* 354 ** If the memory cell contains a value that must be freed by 355 ** invoking the external callback in Mem.xDel, then this routine 356 ** will free that value. It also sets Mem.flags to MEM_Null. 357 ** 358 ** This is a helper routine for sqlite3VdbeMemSetNull() and 359 ** for sqlite3VdbeMemRelease(). Use those other routines as the 360 ** entry point for releasing Mem resources. 361 */ 362 static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){ 363 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); 364 assert( VdbeMemDynamic(p) ); 365 if( p->flags&MEM_Agg ){ 366 sqlite3VdbeMemFinalize(p, p->u.pDef); 367 assert( (p->flags & MEM_Agg)==0 ); 368 testcase( p->flags & MEM_Dyn ); 369 } 370 if( p->flags&MEM_Dyn ){ 371 assert( (p->flags&MEM_RowSet)==0 ); 372 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 ); 373 p->xDel((void *)p->z); 374 }else if( p->flags&MEM_RowSet ){ 375 sqlite3RowSetClear(p->u.pRowSet); 376 }else if( p->flags&MEM_Frame ){ 377 VdbeFrame *pFrame = p->u.pFrame; 378 pFrame->pParent = pFrame->v->pDelFrame; 379 pFrame->v->pDelFrame = pFrame; 380 } 381 p->flags = MEM_Null; 382 } 383 384 /* 385 ** Release memory held by the Mem p, both external memory cleared 386 ** by p->xDel and memory in p->zMalloc. 387 ** 388 ** This is a helper routine invoked by sqlite3VdbeMemRelease() in 389 ** the unusual case where there really is memory in p that needs 390 ** to be freed. 391 */ 392 static SQLITE_NOINLINE void vdbeMemClear(Mem *p){ 393 if( VdbeMemDynamic(p) ){ 394 vdbeMemClearExternAndSetNull(p); 395 } 396 if( p->szMalloc ){ 397 sqlite3DbFreeNN(p->db, p->zMalloc); 398 p->szMalloc = 0; 399 } 400 p->z = 0; 401 } 402 403 /* 404 ** Release any memory resources held by the Mem. Both the memory that is 405 ** free by Mem.xDel and the Mem.zMalloc allocation are freed. 406 ** 407 ** Use this routine prior to clean up prior to abandoning a Mem, or to 408 ** reset a Mem back to its minimum memory utilization. 409 ** 410 ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space 411 ** prior to inserting new content into the Mem. 412 */ 413 void sqlite3VdbeMemRelease(Mem *p){ 414 assert( sqlite3VdbeCheckMemInvariants(p) ); 415 if( VdbeMemDynamic(p) || p->szMalloc ){ 416 vdbeMemClear(p); 417 } 418 } 419 420 /* 421 ** Convert a 64-bit IEEE double into a 64-bit signed integer. 422 ** If the double is out of range of a 64-bit signed integer then 423 ** return the closest available 64-bit signed integer. 424 */ 425 static SQLITE_NOINLINE i64 doubleToInt64(double r){ 426 #ifdef SQLITE_OMIT_FLOATING_POINT 427 /* When floating-point is omitted, double and int64 are the same thing */ 428 return r; 429 #else 430 /* 431 ** Many compilers we encounter do not define constants for the 432 ** minimum and maximum 64-bit integers, or they define them 433 ** inconsistently. And many do not understand the "LL" notation. 434 ** So we define our own static constants here using nothing 435 ** larger than a 32-bit integer constant. 436 */ 437 static const i64 maxInt = LARGEST_INT64; 438 static const i64 minInt = SMALLEST_INT64; 439 440 if( r<=(double)minInt ){ 441 return minInt; 442 }else if( r>=(double)maxInt ){ 443 return maxInt; 444 }else{ 445 return (i64)r; 446 } 447 #endif 448 } 449 450 /* 451 ** Return some kind of integer value which is the best we can do 452 ** at representing the value that *pMem describes as an integer. 453 ** If pMem is an integer, then the value is exact. If pMem is 454 ** a floating-point then the value returned is the integer part. 455 ** If pMem is a string or blob, then we make an attempt to convert 456 ** it into an integer and return that. If pMem represents an 457 ** an SQL-NULL value, return 0. 458 ** 459 ** If pMem represents a string value, its encoding might be changed. 460 */ 461 static SQLITE_NOINLINE i64 memIntValue(Mem *pMem){ 462 i64 value = 0; 463 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc); 464 return value; 465 } 466 i64 sqlite3VdbeIntValue(Mem *pMem){ 467 int flags; 468 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 469 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 470 flags = pMem->flags; 471 if( flags & MEM_Int ){ 472 return pMem->u.i; 473 }else if( flags & MEM_Real ){ 474 return doubleToInt64(pMem->u.r); 475 }else if( flags & (MEM_Str|MEM_Blob) ){ 476 assert( pMem->z || pMem->n==0 ); 477 return memIntValue(pMem); 478 }else{ 479 return 0; 480 } 481 } 482 483 /* 484 ** Return the best representation of pMem that we can get into a 485 ** double. If pMem is already a double or an integer, return its 486 ** value. If it is a string or blob, try to convert it to a double. 487 ** If it is a NULL, return 0.0. 488 */ 489 static SQLITE_NOINLINE double memRealValue(Mem *pMem){ 490 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 491 double val = (double)0; 492 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc); 493 return val; 494 } 495 double sqlite3VdbeRealValue(Mem *pMem){ 496 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 497 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 498 if( pMem->flags & MEM_Real ){ 499 return pMem->u.r; 500 }else if( pMem->flags & MEM_Int ){ 501 return (double)pMem->u.i; 502 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ 503 return memRealValue(pMem); 504 }else{ 505 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ 506 return (double)0; 507 } 508 } 509 510 /* 511 ** The MEM structure is already a MEM_Real. Try to also make it a 512 ** MEM_Int if we can. 513 */ 514 void sqlite3VdbeIntegerAffinity(Mem *pMem){ 515 i64 ix; 516 assert( pMem->flags & MEM_Real ); 517 assert( (pMem->flags & MEM_RowSet)==0 ); 518 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 519 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 520 521 ix = doubleToInt64(pMem->u.r); 522 523 /* Only mark the value as an integer if 524 ** 525 ** (1) the round-trip conversion real->int->real is a no-op, and 526 ** (2) The integer is neither the largest nor the smallest 527 ** possible integer (ticket #3922) 528 ** 529 ** The second and third terms in the following conditional enforces 530 ** the second condition under the assumption that addition overflow causes 531 ** values to wrap around. 532 */ 533 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){ 534 pMem->u.i = ix; 535 MemSetTypeFlag(pMem, MEM_Int); 536 } 537 } 538 539 /* 540 ** Convert pMem to type integer. Invalidate any prior representations. 541 */ 542 int sqlite3VdbeMemIntegerify(Mem *pMem){ 543 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 544 assert( (pMem->flags & MEM_RowSet)==0 ); 545 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 546 547 pMem->u.i = sqlite3VdbeIntValue(pMem); 548 MemSetTypeFlag(pMem, MEM_Int); 549 return SQLITE_OK; 550 } 551 552 /* 553 ** Convert pMem so that it is of type MEM_Real. 554 ** Invalidate any prior representations. 555 */ 556 int sqlite3VdbeMemRealify(Mem *pMem){ 557 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 558 assert( EIGHT_BYTE_ALIGNMENT(pMem) ); 559 560 pMem->u.r = sqlite3VdbeRealValue(pMem); 561 MemSetTypeFlag(pMem, MEM_Real); 562 return SQLITE_OK; 563 } 564 565 /* 566 ** Convert pMem so that it has types MEM_Real or MEM_Int or both. 567 ** Invalidate any prior representations. 568 ** 569 ** Every effort is made to force the conversion, even if the input 570 ** is a string that does not look completely like a number. Convert 571 ** as much of the string as we can and ignore the rest. 572 */ 573 int sqlite3VdbeMemNumerify(Mem *pMem){ 574 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){ 575 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); 576 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 577 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){ 578 MemSetTypeFlag(pMem, MEM_Int); 579 }else{ 580 pMem->u.r = sqlite3VdbeRealValue(pMem); 581 MemSetTypeFlag(pMem, MEM_Real); 582 sqlite3VdbeIntegerAffinity(pMem); 583 } 584 } 585 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 ); 586 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero); 587 return SQLITE_OK; 588 } 589 590 /* 591 ** Cast the datatype of the value in pMem according to the affinity 592 ** "aff". Casting is different from applying affinity in that a cast 593 ** is forced. In other words, the value is converted into the desired 594 ** affinity even if that results in loss of data. This routine is 595 ** used (for example) to implement the SQL "cast()" operator. 596 */ 597 void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){ 598 if( pMem->flags & MEM_Null ) return; 599 switch( aff ){ 600 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */ 601 if( (pMem->flags & MEM_Blob)==0 ){ 602 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); 603 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); 604 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob); 605 }else{ 606 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob); 607 } 608 break; 609 } 610 case SQLITE_AFF_NUMERIC: { 611 sqlite3VdbeMemNumerify(pMem); 612 break; 613 } 614 case SQLITE_AFF_INTEGER: { 615 sqlite3VdbeMemIntegerify(pMem); 616 break; 617 } 618 case SQLITE_AFF_REAL: { 619 sqlite3VdbeMemRealify(pMem); 620 break; 621 } 622 default: { 623 assert( aff==SQLITE_AFF_TEXT ); 624 assert( MEM_Str==(MEM_Blob>>3) ); 625 pMem->flags |= (pMem->flags&MEM_Blob)>>3; 626 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding); 627 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed ); 628 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero); 629 break; 630 } 631 } 632 } 633 634 /* 635 ** Initialize bulk memory to be a consistent Mem object. 636 ** 637 ** The minimum amount of initialization feasible is performed. 638 */ 639 void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){ 640 assert( (flags & ~MEM_TypeMask)==0 ); 641 pMem->flags = flags; 642 pMem->db = db; 643 pMem->szMalloc = 0; 644 } 645 646 647 /* 648 ** Delete any previous value and set the value stored in *pMem to NULL. 649 ** 650 ** This routine calls the Mem.xDel destructor to dispose of values that 651 ** require the destructor. But it preserves the Mem.zMalloc memory allocation. 652 ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this 653 ** routine to invoke the destructor and deallocates Mem.zMalloc. 654 ** 655 ** Use this routine to reset the Mem prior to insert a new value. 656 ** 657 ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it. 658 */ 659 void sqlite3VdbeMemSetNull(Mem *pMem){ 660 if( VdbeMemDynamic(pMem) ){ 661 vdbeMemClearExternAndSetNull(pMem); 662 }else{ 663 pMem->flags = MEM_Null; 664 } 665 } 666 void sqlite3ValueSetNull(sqlite3_value *p){ 667 sqlite3VdbeMemSetNull((Mem*)p); 668 } 669 670 /* 671 ** Delete any previous value and set the value to be a BLOB of length 672 ** n containing all zeros. 673 */ 674 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ 675 sqlite3VdbeMemRelease(pMem); 676 pMem->flags = MEM_Blob|MEM_Zero; 677 pMem->n = 0; 678 if( n<0 ) n = 0; 679 pMem->u.nZero = n; 680 pMem->enc = SQLITE_UTF8; 681 pMem->z = 0; 682 } 683 684 /* 685 ** The pMem is known to contain content that needs to be destroyed prior 686 ** to a value change. So invoke the destructor, then set the value to 687 ** a 64-bit integer. 688 */ 689 static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){ 690 sqlite3VdbeMemSetNull(pMem); 691 pMem->u.i = val; 692 pMem->flags = MEM_Int; 693 } 694 695 /* 696 ** Delete any previous value and set the value stored in *pMem to val, 697 ** manifest type INTEGER. 698 */ 699 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ 700 if( VdbeMemDynamic(pMem) ){ 701 vdbeReleaseAndSetInt64(pMem, val); 702 }else{ 703 pMem->u.i = val; 704 pMem->flags = MEM_Int; 705 } 706 } 707 708 #ifndef SQLITE_OMIT_FLOATING_POINT 709 /* 710 ** Delete any previous value and set the value stored in *pMem to val, 711 ** manifest type REAL. 712 */ 713 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ 714 sqlite3VdbeMemSetNull(pMem); 715 if( !sqlite3IsNaN(val) ){ 716 pMem->u.r = val; 717 pMem->flags = MEM_Real; 718 } 719 } 720 #endif 721 722 /* 723 ** Delete any previous value and set the value of pMem to be an 724 ** empty boolean index. 725 */ 726 void sqlite3VdbeMemSetRowSet(Mem *pMem){ 727 sqlite3 *db = pMem->db; 728 assert( db!=0 ); 729 assert( (pMem->flags & MEM_RowSet)==0 ); 730 sqlite3VdbeMemRelease(pMem); 731 pMem->zMalloc = sqlite3DbMallocRawNN(db, 64); 732 if( db->mallocFailed ){ 733 pMem->flags = MEM_Null; 734 pMem->szMalloc = 0; 735 }else{ 736 assert( pMem->zMalloc ); 737 pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc); 738 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc); 739 assert( pMem->u.pRowSet!=0 ); 740 pMem->flags = MEM_RowSet; 741 } 742 } 743 744 /* 745 ** Return true if the Mem object contains a TEXT or BLOB that is 746 ** too large - whose size exceeds SQLITE_MAX_LENGTH. 747 */ 748 int sqlite3VdbeMemTooBig(Mem *p){ 749 assert( p->db!=0 ); 750 if( p->flags & (MEM_Str|MEM_Blob) ){ 751 int n = p->n; 752 if( p->flags & MEM_Zero ){ 753 n += p->u.nZero; 754 } 755 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; 756 } 757 return 0; 758 } 759 760 #ifdef SQLITE_DEBUG 761 /* 762 ** This routine prepares a memory cell for modification by breaking 763 ** its link to a shallow copy and by marking any current shallow 764 ** copies of this cell as invalid. 765 ** 766 ** This is used for testing and debugging only - to make sure shallow 767 ** copies are not misused. 768 */ 769 void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){ 770 int i; 771 Mem *pX; 772 for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){ 773 if( pX->pScopyFrom==pMem ){ 774 pX->flags |= MEM_Undefined; 775 pX->pScopyFrom = 0; 776 } 777 } 778 pMem->pScopyFrom = 0; 779 } 780 #endif /* SQLITE_DEBUG */ 781 782 783 /* 784 ** Make an shallow copy of pFrom into pTo. Prior contents of 785 ** pTo are freed. The pFrom->z field is not duplicated. If 786 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z 787 ** and flags gets srcType (either MEM_Ephem or MEM_Static). 788 */ 789 static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){ 790 vdbeMemClearExternAndSetNull(pTo); 791 assert( !VdbeMemDynamic(pTo) ); 792 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType); 793 } 794 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ 795 assert( (pFrom->flags & MEM_RowSet)==0 ); 796 assert( pTo->db==pFrom->db ); 797 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; } 798 memcpy(pTo, pFrom, MEMCELLSIZE); 799 if( (pFrom->flags&MEM_Static)==0 ){ 800 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); 801 assert( srcType==MEM_Ephem || srcType==MEM_Static ); 802 pTo->flags |= srcType; 803 } 804 } 805 806 /* 807 ** Make a full copy of pFrom into pTo. Prior contents of pTo are 808 ** freed before the copy is made. 809 */ 810 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ 811 int rc = SQLITE_OK; 812 813 assert( (pFrom->flags & MEM_RowSet)==0 ); 814 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo); 815 memcpy(pTo, pFrom, MEMCELLSIZE); 816 pTo->flags &= ~MEM_Dyn; 817 if( pTo->flags&(MEM_Str|MEM_Blob) ){ 818 if( 0==(pFrom->flags&MEM_Static) ){ 819 pTo->flags |= MEM_Ephem; 820 rc = sqlite3VdbeMemMakeWriteable(pTo); 821 } 822 } 823 824 return rc; 825 } 826 827 /* 828 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is 829 ** freed. If pFrom contains ephemeral data, a copy is made. 830 ** 831 ** pFrom contains an SQL NULL when this routine returns. 832 */ 833 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ 834 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); 835 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); 836 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); 837 838 sqlite3VdbeMemRelease(pTo); 839 memcpy(pTo, pFrom, sizeof(Mem)); 840 pFrom->flags = MEM_Null; 841 pFrom->szMalloc = 0; 842 } 843 844 /* 845 ** Change the value of a Mem to be a string or a BLOB. 846 ** 847 ** The memory management strategy depends on the value of the xDel 848 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 849 ** string is copied into a (possibly existing) buffer managed by the 850 ** Mem structure. Otherwise, any existing buffer is freed and the 851 ** pointer copied. 852 ** 853 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH 854 ** size limit) then no memory allocation occurs. If the string can be 855 ** stored without allocating memory, then it is. If a memory allocation 856 ** is required to store the string, then value of pMem is unchanged. In 857 ** either case, SQLITE_TOOBIG is returned. 858 */ 859 int sqlite3VdbeMemSetStr( 860 Mem *pMem, /* Memory cell to set to string value */ 861 const char *z, /* String pointer */ 862 int n, /* Bytes in string, or negative */ 863 u8 enc, /* Encoding of z. 0 for BLOBs */ 864 void (*xDel)(void*) /* Destructor function */ 865 ){ 866 int nByte = n; /* New value for pMem->n */ 867 int iLimit; /* Maximum allowed string or blob size */ 868 u16 flags = 0; /* New value for pMem->flags */ 869 870 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 871 assert( (pMem->flags & MEM_RowSet)==0 ); 872 873 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ 874 if( !z ){ 875 sqlite3VdbeMemSetNull(pMem); 876 return SQLITE_OK; 877 } 878 879 if( pMem->db ){ 880 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; 881 }else{ 882 iLimit = SQLITE_MAX_LENGTH; 883 } 884 flags = (enc==0?MEM_Blob:MEM_Str); 885 if( nByte<0 ){ 886 assert( enc!=0 ); 887 if( enc==SQLITE_UTF8 ){ 888 nByte = sqlite3Strlen30(z); 889 if( nByte>iLimit ) nByte = iLimit+1; 890 }else{ 891 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} 892 } 893 flags |= MEM_Term; 894 } 895 896 /* The following block sets the new values of Mem.z and Mem.xDel. It 897 ** also sets a flag in local variable "flags" to indicate the memory 898 ** management (one of MEM_Dyn or MEM_Static). 899 */ 900 if( xDel==SQLITE_TRANSIENT ){ 901 int nAlloc = nByte; 902 if( flags&MEM_Term ){ 903 nAlloc += (enc==SQLITE_UTF8?1:2); 904 } 905 if( nByte>iLimit ){ 906 return SQLITE_TOOBIG; 907 } 908 testcase( nAlloc==0 ); 909 testcase( nAlloc==31 ); 910 testcase( nAlloc==32 ); 911 if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){ 912 return SQLITE_NOMEM_BKPT; 913 } 914 memcpy(pMem->z, z, nAlloc); 915 }else if( xDel==SQLITE_DYNAMIC ){ 916 sqlite3VdbeMemRelease(pMem); 917 pMem->zMalloc = pMem->z = (char *)z; 918 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc); 919 }else{ 920 sqlite3VdbeMemRelease(pMem); 921 pMem->z = (char *)z; 922 pMem->xDel = xDel; 923 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); 924 } 925 926 pMem->n = nByte; 927 pMem->flags = flags; 928 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); 929 930 #ifndef SQLITE_OMIT_UTF16 931 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ 932 return SQLITE_NOMEM_BKPT; 933 } 934 #endif 935 936 if( nByte>iLimit ){ 937 return SQLITE_TOOBIG; 938 } 939 940 return SQLITE_OK; 941 } 942 943 /* 944 ** Move data out of a btree key or data field and into a Mem structure. 945 ** The data is payload from the entry that pCur is currently pointing 946 ** to. offset and amt determine what portion of the data or key to retrieve. 947 ** The result is written into the pMem element. 948 ** 949 ** The pMem object must have been initialized. This routine will use 950 ** pMem->zMalloc to hold the content from the btree, if possible. New 951 ** pMem->zMalloc space will be allocated if necessary. The calling routine 952 ** is responsible for making sure that the pMem object is eventually 953 ** destroyed. 954 ** 955 ** If this routine fails for any reason (malloc returns NULL or unable 956 ** to read from the disk) then the pMem is left in an inconsistent state. 957 */ 958 static SQLITE_NOINLINE int vdbeMemFromBtreeResize( 959 BtCursor *pCur, /* Cursor pointing at record to retrieve. */ 960 u32 offset, /* Offset from the start of data to return bytes from. */ 961 u32 amt, /* Number of bytes to return. */ 962 Mem *pMem /* OUT: Return data in this Mem structure. */ 963 ){ 964 int rc; 965 pMem->flags = MEM_Null; 966 if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){ 967 rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z); 968 if( rc==SQLITE_OK ){ 969 pMem->z[amt] = 0; 970 pMem->z[amt+1] = 0; 971 pMem->flags = MEM_Blob|MEM_Term; 972 pMem->n = (int)amt; 973 }else{ 974 sqlite3VdbeMemRelease(pMem); 975 } 976 } 977 return rc; 978 } 979 int sqlite3VdbeMemFromBtree( 980 BtCursor *pCur, /* Cursor pointing at record to retrieve. */ 981 u32 offset, /* Offset from the start of data to return bytes from. */ 982 u32 amt, /* Number of bytes to return. */ 983 Mem *pMem /* OUT: Return data in this Mem structure. */ 984 ){ 985 char *zData; /* Data from the btree layer */ 986 u32 available = 0; /* Number of bytes available on the local btree page */ 987 int rc = SQLITE_OK; /* Return code */ 988 989 assert( sqlite3BtreeCursorIsValid(pCur) ); 990 assert( !VdbeMemDynamic(pMem) ); 991 992 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 993 ** that both the BtShared and database handle mutexes are held. */ 994 assert( (pMem->flags & MEM_RowSet)==0 ); 995 zData = (char *)sqlite3BtreePayloadFetch(pCur, &available); 996 assert( zData!=0 ); 997 998 if( offset+amt<=available ){ 999 pMem->z = &zData[offset]; 1000 pMem->flags = MEM_Blob|MEM_Ephem; 1001 pMem->n = (int)amt; 1002 }else{ 1003 rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem); 1004 } 1005 1006 return rc; 1007 } 1008 1009 /* 1010 ** The pVal argument is known to be a value other than NULL. 1011 ** Convert it into a string with encoding enc and return a pointer 1012 ** to a zero-terminated version of that string. 1013 */ 1014 static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){ 1015 assert( pVal!=0 ); 1016 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); 1017 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); 1018 assert( (pVal->flags & MEM_RowSet)==0 ); 1019 assert( (pVal->flags & (MEM_Null))==0 ); 1020 if( pVal->flags & (MEM_Blob|MEM_Str) ){ 1021 if( ExpandBlob(pVal) ) return 0; 1022 pVal->flags |= MEM_Str; 1023 if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){ 1024 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); 1025 } 1026 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){ 1027 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); 1028 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ 1029 return 0; 1030 } 1031 } 1032 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */ 1033 }else{ 1034 sqlite3VdbeMemStringify(pVal, enc, 0); 1035 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) ); 1036 } 1037 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 1038 || pVal->db->mallocFailed ); 1039 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ 1040 return pVal->z; 1041 }else{ 1042 return 0; 1043 } 1044 } 1045 1046 /* This function is only available internally, it is not part of the 1047 ** external API. It works in a similar way to sqlite3_value_text(), 1048 ** except the data returned is in the encoding specified by the second 1049 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or 1050 ** SQLITE_UTF8. 1051 ** 1052 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. 1053 ** If that is the case, then the result must be aligned on an even byte 1054 ** boundary. 1055 */ 1056 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ 1057 if( !pVal ) return 0; 1058 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); 1059 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); 1060 assert( (pVal->flags & MEM_RowSet)==0 ); 1061 if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){ 1062 return pVal->z; 1063 } 1064 if( pVal->flags&MEM_Null ){ 1065 return 0; 1066 } 1067 return valueToText(pVal, enc); 1068 } 1069 1070 /* 1071 ** Create a new sqlite3_value object. 1072 */ 1073 sqlite3_value *sqlite3ValueNew(sqlite3 *db){ 1074 Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); 1075 if( p ){ 1076 p->flags = MEM_Null; 1077 p->db = db; 1078 } 1079 return p; 1080 } 1081 1082 /* 1083 ** Context object passed by sqlite3Stat4ProbeSetValue() through to 1084 ** valueNew(). See comments above valueNew() for details. 1085 */ 1086 struct ValueNewStat4Ctx { 1087 Parse *pParse; 1088 Index *pIdx; 1089 UnpackedRecord **ppRec; 1090 int iVal; 1091 }; 1092 1093 /* 1094 ** Allocate and return a pointer to a new sqlite3_value object. If 1095 ** the second argument to this function is NULL, the object is allocated 1096 ** by calling sqlite3ValueNew(). 1097 ** 1098 ** Otherwise, if the second argument is non-zero, then this function is 1099 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not 1100 ** already been allocated, allocate the UnpackedRecord structure that 1101 ** that function will return to its caller here. Then return a pointer to 1102 ** an sqlite3_value within the UnpackedRecord.a[] array. 1103 */ 1104 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){ 1105 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1106 if( p ){ 1107 UnpackedRecord *pRec = p->ppRec[0]; 1108 1109 if( pRec==0 ){ 1110 Index *pIdx = p->pIdx; /* Index being probed */ 1111 int nByte; /* Bytes of space to allocate */ 1112 int i; /* Counter variable */ 1113 int nCol = pIdx->nColumn; /* Number of index columns including rowid */ 1114 1115 nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord)); 1116 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte); 1117 if( pRec ){ 1118 pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx); 1119 if( pRec->pKeyInfo ){ 1120 assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol ); 1121 assert( pRec->pKeyInfo->enc==ENC(db) ); 1122 pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord))); 1123 for(i=0; i<nCol; i++){ 1124 pRec->aMem[i].flags = MEM_Null; 1125 pRec->aMem[i].db = db; 1126 } 1127 }else{ 1128 sqlite3DbFreeNN(db, pRec); 1129 pRec = 0; 1130 } 1131 } 1132 if( pRec==0 ) return 0; 1133 p->ppRec[0] = pRec; 1134 } 1135 1136 pRec->nField = p->iVal+1; 1137 return &pRec->aMem[p->iVal]; 1138 } 1139 #else 1140 UNUSED_PARAMETER(p); 1141 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ 1142 return sqlite3ValueNew(db); 1143 } 1144 1145 /* 1146 ** The expression object indicated by the second argument is guaranteed 1147 ** to be a scalar SQL function. If 1148 ** 1149 ** * all function arguments are SQL literals, 1150 ** * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and 1151 ** * the SQLITE_FUNC_NEEDCOLL function flag is not set, 1152 ** 1153 ** then this routine attempts to invoke the SQL function. Assuming no 1154 ** error occurs, output parameter (*ppVal) is set to point to a value 1155 ** object containing the result before returning SQLITE_OK. 1156 ** 1157 ** Affinity aff is applied to the result of the function before returning. 1158 ** If the result is a text value, the sqlite3_value object uses encoding 1159 ** enc. 1160 ** 1161 ** If the conditions above are not met, this function returns SQLITE_OK 1162 ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to 1163 ** NULL and an SQLite error code returned. 1164 */ 1165 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1166 static int valueFromFunction( 1167 sqlite3 *db, /* The database connection */ 1168 Expr *p, /* The expression to evaluate */ 1169 u8 enc, /* Encoding to use */ 1170 u8 aff, /* Affinity to use */ 1171 sqlite3_value **ppVal, /* Write the new value here */ 1172 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ 1173 ){ 1174 sqlite3_context ctx; /* Context object for function invocation */ 1175 sqlite3_value **apVal = 0; /* Function arguments */ 1176 int nVal = 0; /* Size of apVal[] array */ 1177 FuncDef *pFunc = 0; /* Function definition */ 1178 sqlite3_value *pVal = 0; /* New value */ 1179 int rc = SQLITE_OK; /* Return code */ 1180 ExprList *pList = 0; /* Function arguments */ 1181 int i; /* Iterator variable */ 1182 1183 assert( pCtx!=0 ); 1184 assert( (p->flags & EP_TokenOnly)==0 ); 1185 pList = p->x.pList; 1186 if( pList ) nVal = pList->nExpr; 1187 pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0); 1188 assert( pFunc ); 1189 if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 1190 || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) 1191 ){ 1192 return SQLITE_OK; 1193 } 1194 1195 if( pList ){ 1196 apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal); 1197 if( apVal==0 ){ 1198 rc = SQLITE_NOMEM_BKPT; 1199 goto value_from_function_out; 1200 } 1201 for(i=0; i<nVal; i++){ 1202 rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]); 1203 if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out; 1204 } 1205 } 1206 1207 pVal = valueNew(db, pCtx); 1208 if( pVal==0 ){ 1209 rc = SQLITE_NOMEM_BKPT; 1210 goto value_from_function_out; 1211 } 1212 1213 assert( pCtx->pParse->rc==SQLITE_OK ); 1214 memset(&ctx, 0, sizeof(ctx)); 1215 ctx.pOut = pVal; 1216 ctx.pFunc = pFunc; 1217 pFunc->xSFunc(&ctx, nVal, apVal); 1218 if( ctx.isError ){ 1219 rc = ctx.isError; 1220 sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal)); 1221 }else{ 1222 sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8); 1223 assert( rc==SQLITE_OK ); 1224 rc = sqlite3VdbeChangeEncoding(pVal, enc); 1225 if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){ 1226 rc = SQLITE_TOOBIG; 1227 pCtx->pParse->nErr++; 1228 } 1229 } 1230 pCtx->pParse->rc = rc; 1231 1232 value_from_function_out: 1233 if( rc!=SQLITE_OK ){ 1234 pVal = 0; 1235 } 1236 if( apVal ){ 1237 for(i=0; i<nVal; i++){ 1238 sqlite3ValueFree(apVal[i]); 1239 } 1240 sqlite3DbFreeNN(db, apVal); 1241 } 1242 1243 *ppVal = pVal; 1244 return rc; 1245 } 1246 #else 1247 # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK 1248 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */ 1249 1250 /* 1251 ** Extract a value from the supplied expression in the manner described 1252 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object 1253 ** using valueNew(). 1254 ** 1255 ** If pCtx is NULL and an error occurs after the sqlite3_value object 1256 ** has been allocated, it is freed before returning. Or, if pCtx is not 1257 ** NULL, it is assumed that the caller will free any allocated object 1258 ** in all cases. 1259 */ 1260 static int valueFromExpr( 1261 sqlite3 *db, /* The database connection */ 1262 Expr *pExpr, /* The expression to evaluate */ 1263 u8 enc, /* Encoding to use */ 1264 u8 affinity, /* Affinity to use */ 1265 sqlite3_value **ppVal, /* Write the new value here */ 1266 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */ 1267 ){ 1268 int op; 1269 char *zVal = 0; 1270 sqlite3_value *pVal = 0; 1271 int negInt = 1; 1272 const char *zNeg = ""; 1273 int rc = SQLITE_OK; 1274 1275 assert( pExpr!=0 ); 1276 while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft; 1277 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2; 1278 1279 /* Compressed expressions only appear when parsing the DEFAULT clause 1280 ** on a table column definition, and hence only when pCtx==0. This 1281 ** check ensures that an EP_TokenOnly expression is never passed down 1282 ** into valueFromFunction(). */ 1283 assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 ); 1284 1285 if( op==TK_CAST ){ 1286 u8 aff = sqlite3AffinityType(pExpr->u.zToken,0); 1287 rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx); 1288 testcase( rc!=SQLITE_OK ); 1289 if( *ppVal ){ 1290 sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8); 1291 sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8); 1292 } 1293 return rc; 1294 } 1295 1296 /* Handle negative integers in a single step. This is needed in the 1297 ** case when the value is -9223372036854775808. 1298 */ 1299 if( op==TK_UMINUS 1300 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){ 1301 pExpr = pExpr->pLeft; 1302 op = pExpr->op; 1303 negInt = -1; 1304 zNeg = "-"; 1305 } 1306 1307 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ 1308 pVal = valueNew(db, pCtx); 1309 if( pVal==0 ) goto no_mem; 1310 if( ExprHasProperty(pExpr, EP_IntValue) ){ 1311 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt); 1312 }else{ 1313 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken); 1314 if( zVal==0 ) goto no_mem; 1315 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); 1316 } 1317 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){ 1318 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8); 1319 }else{ 1320 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8); 1321 } 1322 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str; 1323 if( enc!=SQLITE_UTF8 ){ 1324 rc = sqlite3VdbeChangeEncoding(pVal, enc); 1325 } 1326 }else if( op==TK_UMINUS ) { 1327 /* This branch happens for multiple negative signs. Ex: -(-5) */ 1328 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 1329 && pVal!=0 1330 ){ 1331 sqlite3VdbeMemNumerify(pVal); 1332 if( pVal->flags & MEM_Real ){ 1333 pVal->u.r = -pVal->u.r; 1334 }else if( pVal->u.i==SMALLEST_INT64 ){ 1335 pVal->u.r = -(double)SMALLEST_INT64; 1336 MemSetTypeFlag(pVal, MEM_Real); 1337 }else{ 1338 pVal->u.i = -pVal->u.i; 1339 } 1340 sqlite3ValueApplyAffinity(pVal, affinity, enc); 1341 } 1342 }else if( op==TK_NULL ){ 1343 pVal = valueNew(db, pCtx); 1344 if( pVal==0 ) goto no_mem; 1345 sqlite3VdbeMemNumerify(pVal); 1346 } 1347 #ifndef SQLITE_OMIT_BLOB_LITERAL 1348 else if( op==TK_BLOB ){ 1349 int nVal; 1350 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 1351 assert( pExpr->u.zToken[1]=='\'' ); 1352 pVal = valueNew(db, pCtx); 1353 if( !pVal ) goto no_mem; 1354 zVal = &pExpr->u.zToken[2]; 1355 nVal = sqlite3Strlen30(zVal)-1; 1356 assert( zVal[nVal]=='\'' ); 1357 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, 1358 0, SQLITE_DYNAMIC); 1359 } 1360 #endif 1361 1362 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1363 else if( op==TK_FUNCTION && pCtx!=0 ){ 1364 rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx); 1365 } 1366 #endif 1367 1368 *ppVal = pVal; 1369 return rc; 1370 1371 no_mem: 1372 sqlite3OomFault(db); 1373 sqlite3DbFree(db, zVal); 1374 assert( *ppVal==0 ); 1375 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1376 if( pCtx==0 ) sqlite3ValueFree(pVal); 1377 #else 1378 assert( pCtx==0 ); sqlite3ValueFree(pVal); 1379 #endif 1380 return SQLITE_NOMEM_BKPT; 1381 } 1382 1383 /* 1384 ** Create a new sqlite3_value object, containing the value of pExpr. 1385 ** 1386 ** This only works for very simple expressions that consist of one constant 1387 ** token (i.e. "5", "5.1", "'a string'"). If the expression can 1388 ** be converted directly into a value, then the value is allocated and 1389 ** a pointer written to *ppVal. The caller is responsible for deallocating 1390 ** the value by passing it to sqlite3ValueFree() later on. If the expression 1391 ** cannot be converted to a value, then *ppVal is set to NULL. 1392 */ 1393 int sqlite3ValueFromExpr( 1394 sqlite3 *db, /* The database connection */ 1395 Expr *pExpr, /* The expression to evaluate */ 1396 u8 enc, /* Encoding to use */ 1397 u8 affinity, /* Affinity to use */ 1398 sqlite3_value **ppVal /* Write the new value here */ 1399 ){ 1400 return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0; 1401 } 1402 1403 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 1404 /* 1405 ** The implementation of the sqlite_record() function. This function accepts 1406 ** a single argument of any type. The return value is a formatted database 1407 ** record (a blob) containing the argument value. 1408 ** 1409 ** This is used to convert the value stored in the 'sample' column of the 1410 ** sqlite_stat3 table to the record format SQLite uses internally. 1411 */ 1412 static void recordFunc( 1413 sqlite3_context *context, 1414 int argc, 1415 sqlite3_value **argv 1416 ){ 1417 const int file_format = 1; 1418 u32 iSerial; /* Serial type */ 1419 int nSerial; /* Bytes of space for iSerial as varint */ 1420 u32 nVal; /* Bytes of space required for argv[0] */ 1421 int nRet; 1422 sqlite3 *db; 1423 u8 *aRet; 1424 1425 UNUSED_PARAMETER( argc ); 1426 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal); 1427 nSerial = sqlite3VarintLen(iSerial); 1428 db = sqlite3_context_db_handle(context); 1429 1430 nRet = 1 + nSerial + nVal; 1431 aRet = sqlite3DbMallocRawNN(db, nRet); 1432 if( aRet==0 ){ 1433 sqlite3_result_error_nomem(context); 1434 }else{ 1435 aRet[0] = nSerial+1; 1436 putVarint32(&aRet[1], iSerial); 1437 sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial); 1438 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT); 1439 sqlite3DbFreeNN(db, aRet); 1440 } 1441 } 1442 1443 /* 1444 ** Register built-in functions used to help read ANALYZE data. 1445 */ 1446 void sqlite3AnalyzeFunctions(void){ 1447 static FuncDef aAnalyzeTableFuncs[] = { 1448 FUNCTION(sqlite_record, 1, 0, 0, recordFunc), 1449 }; 1450 sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs)); 1451 } 1452 1453 /* 1454 ** Attempt to extract a value from pExpr and use it to construct *ppVal. 1455 ** 1456 ** If pAlloc is not NULL, then an UnpackedRecord object is created for 1457 ** pAlloc if one does not exist and the new value is added to the 1458 ** UnpackedRecord object. 1459 ** 1460 ** A value is extracted in the following cases: 1461 ** 1462 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL, 1463 ** 1464 ** * The expression is a bound variable, and this is a reprepare, or 1465 ** 1466 ** * The expression is a literal value. 1467 ** 1468 ** On success, *ppVal is made to point to the extracted value. The caller 1469 ** is responsible for ensuring that the value is eventually freed. 1470 */ 1471 static int stat4ValueFromExpr( 1472 Parse *pParse, /* Parse context */ 1473 Expr *pExpr, /* The expression to extract a value from */ 1474 u8 affinity, /* Affinity to use */ 1475 struct ValueNewStat4Ctx *pAlloc,/* How to allocate space. Or NULL */ 1476 sqlite3_value **ppVal /* OUT: New value object (or NULL) */ 1477 ){ 1478 int rc = SQLITE_OK; 1479 sqlite3_value *pVal = 0; 1480 sqlite3 *db = pParse->db; 1481 1482 /* Skip over any TK_COLLATE nodes */ 1483 pExpr = sqlite3ExprSkipCollate(pExpr); 1484 1485 if( !pExpr ){ 1486 pVal = valueNew(db, pAlloc); 1487 if( pVal ){ 1488 sqlite3VdbeMemSetNull((Mem*)pVal); 1489 } 1490 }else if( pExpr->op==TK_VARIABLE 1491 || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) 1492 ){ 1493 Vdbe *v; 1494 int iBindVar = pExpr->iColumn; 1495 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar); 1496 if( (v = pParse->pReprepare)!=0 ){ 1497 pVal = valueNew(db, pAlloc); 1498 if( pVal ){ 1499 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]); 1500 if( rc==SQLITE_OK ){ 1501 sqlite3ValueApplyAffinity(pVal, affinity, ENC(db)); 1502 } 1503 pVal->db = pParse->db; 1504 } 1505 } 1506 }else{ 1507 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc); 1508 } 1509 1510 assert( pVal==0 || pVal->db==db ); 1511 *ppVal = pVal; 1512 return rc; 1513 } 1514 1515 /* 1516 ** This function is used to allocate and populate UnpackedRecord 1517 ** structures intended to be compared against sample index keys stored 1518 ** in the sqlite_stat4 table. 1519 ** 1520 ** A single call to this function populates zero or more fields of the 1521 ** record starting with field iVal (fields are numbered from left to 1522 ** right starting with 0). A single field is populated if: 1523 ** 1524 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL, 1525 ** 1526 ** * The expression is a bound variable, and this is a reprepare, or 1527 ** 1528 ** * The sqlite3ValueFromExpr() function is able to extract a value 1529 ** from the expression (i.e. the expression is a literal value). 1530 ** 1531 ** Or, if pExpr is a TK_VECTOR, one field is populated for each of the 1532 ** vector components that match either of the two latter criteria listed 1533 ** above. 1534 ** 1535 ** Before any value is appended to the record, the affinity of the 1536 ** corresponding column within index pIdx is applied to it. Before 1537 ** this function returns, output parameter *pnExtract is set to the 1538 ** number of values appended to the record. 1539 ** 1540 ** When this function is called, *ppRec must either point to an object 1541 ** allocated by an earlier call to this function, or must be NULL. If it 1542 ** is NULL and a value can be successfully extracted, a new UnpackedRecord 1543 ** is allocated (and *ppRec set to point to it) before returning. 1544 ** 1545 ** Unless an error is encountered, SQLITE_OK is returned. It is not an 1546 ** error if a value cannot be extracted from pExpr. If an error does 1547 ** occur, an SQLite error code is returned. 1548 */ 1549 int sqlite3Stat4ProbeSetValue( 1550 Parse *pParse, /* Parse context */ 1551 Index *pIdx, /* Index being probed */ 1552 UnpackedRecord **ppRec, /* IN/OUT: Probe record */ 1553 Expr *pExpr, /* The expression to extract a value from */ 1554 int nElem, /* Maximum number of values to append */ 1555 int iVal, /* Array element to populate */ 1556 int *pnExtract /* OUT: Values appended to the record */ 1557 ){ 1558 int rc = SQLITE_OK; 1559 int nExtract = 0; 1560 1561 if( pExpr==0 || pExpr->op!=TK_SELECT ){ 1562 int i; 1563 struct ValueNewStat4Ctx alloc; 1564 1565 alloc.pParse = pParse; 1566 alloc.pIdx = pIdx; 1567 alloc.ppRec = ppRec; 1568 1569 for(i=0; i<nElem; i++){ 1570 sqlite3_value *pVal = 0; 1571 Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0); 1572 u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i); 1573 alloc.iVal = iVal+i; 1574 rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal); 1575 if( !pVal ) break; 1576 nExtract++; 1577 } 1578 } 1579 1580 *pnExtract = nExtract; 1581 return rc; 1582 } 1583 1584 /* 1585 ** Attempt to extract a value from expression pExpr using the methods 1586 ** as described for sqlite3Stat4ProbeSetValue() above. 1587 ** 1588 ** If successful, set *ppVal to point to a new value object and return 1589 ** SQLITE_OK. If no value can be extracted, but no other error occurs 1590 ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error 1591 ** does occur, return an SQLite error code. The final value of *ppVal 1592 ** is undefined in this case. 1593 */ 1594 int sqlite3Stat4ValueFromExpr( 1595 Parse *pParse, /* Parse context */ 1596 Expr *pExpr, /* The expression to extract a value from */ 1597 u8 affinity, /* Affinity to use */ 1598 sqlite3_value **ppVal /* OUT: New value object (or NULL) */ 1599 ){ 1600 return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal); 1601 } 1602 1603 /* 1604 ** Extract the iCol-th column from the nRec-byte record in pRec. Write 1605 ** the column value into *ppVal. If *ppVal is initially NULL then a new 1606 ** sqlite3_value object is allocated. 1607 ** 1608 ** If *ppVal is initially NULL then the caller is responsible for 1609 ** ensuring that the value written into *ppVal is eventually freed. 1610 */ 1611 int sqlite3Stat4Column( 1612 sqlite3 *db, /* Database handle */ 1613 const void *pRec, /* Pointer to buffer containing record */ 1614 int nRec, /* Size of buffer pRec in bytes */ 1615 int iCol, /* Column to extract */ 1616 sqlite3_value **ppVal /* OUT: Extracted value */ 1617 ){ 1618 u32 t; /* a column type code */ 1619 int nHdr; /* Size of the header in the record */ 1620 int iHdr; /* Next unread header byte */ 1621 int iField; /* Next unread data byte */ 1622 int szField; /* Size of the current data field */ 1623 int i; /* Column index */ 1624 u8 *a = (u8*)pRec; /* Typecast byte array */ 1625 Mem *pMem = *ppVal; /* Write result into this Mem object */ 1626 1627 assert( iCol>0 ); 1628 iHdr = getVarint32(a, nHdr); 1629 if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT; 1630 iField = nHdr; 1631 for(i=0; i<=iCol; i++){ 1632 iHdr += getVarint32(&a[iHdr], t); 1633 testcase( iHdr==nHdr ); 1634 testcase( iHdr==nHdr+1 ); 1635 if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT; 1636 szField = sqlite3VdbeSerialTypeLen(t); 1637 iField += szField; 1638 } 1639 testcase( iField==nRec ); 1640 testcase( iField==nRec+1 ); 1641 if( iField>nRec ) return SQLITE_CORRUPT_BKPT; 1642 if( pMem==0 ){ 1643 pMem = *ppVal = sqlite3ValueNew(db); 1644 if( pMem==0 ) return SQLITE_NOMEM_BKPT; 1645 } 1646 sqlite3VdbeSerialGet(&a[iField-szField], t, pMem); 1647 pMem->enc = ENC(db); 1648 return SQLITE_OK; 1649 } 1650 1651 /* 1652 ** Unless it is NULL, the argument must be an UnpackedRecord object returned 1653 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes 1654 ** the object. 1655 */ 1656 void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){ 1657 if( pRec ){ 1658 int i; 1659 int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField; 1660 Mem *aMem = pRec->aMem; 1661 sqlite3 *db = aMem[0].db; 1662 for(i=0; i<nCol; i++){ 1663 sqlite3VdbeMemRelease(&aMem[i]); 1664 } 1665 sqlite3KeyInfoUnref(pRec->pKeyInfo); 1666 sqlite3DbFreeNN(db, pRec); 1667 } 1668 } 1669 #endif /* ifdef SQLITE_ENABLE_STAT4 */ 1670 1671 /* 1672 ** Change the string value of an sqlite3_value object 1673 */ 1674 void sqlite3ValueSetStr( 1675 sqlite3_value *v, /* Value to be set */ 1676 int n, /* Length of string z */ 1677 const void *z, /* Text of the new string */ 1678 u8 enc, /* Encoding to use */ 1679 void (*xDel)(void*) /* Destructor for the string */ 1680 ){ 1681 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); 1682 } 1683 1684 /* 1685 ** Free an sqlite3_value object 1686 */ 1687 void sqlite3ValueFree(sqlite3_value *v){ 1688 if( !v ) return; 1689 sqlite3VdbeMemRelease((Mem *)v); 1690 sqlite3DbFreeNN(((Mem*)v)->db, v); 1691 } 1692 1693 /* 1694 ** The sqlite3ValueBytes() routine returns the number of bytes in the 1695 ** sqlite3_value object assuming that it uses the encoding "enc". 1696 ** The valueBytes() routine is a helper function. 1697 */ 1698 static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){ 1699 return valueToText(pVal, enc)!=0 ? pVal->n : 0; 1700 } 1701 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ 1702 Mem *p = (Mem*)pVal; 1703 assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 ); 1704 if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){ 1705 return p->n; 1706 } 1707 if( (p->flags & MEM_Blob)!=0 ){ 1708 if( p->flags & MEM_Zero ){ 1709 return p->n + p->u.nZero; 1710 }else{ 1711 return p->n; 1712 } 1713 } 1714 if( p->flags & MEM_Null ) return 0; 1715 return valueBytes(pVal, enc); 1716 } 1717