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 implement APIs that are part of the 14 ** VDBE. 15 */ 16 #include "sqliteInt.h" 17 #include "vdbeInt.h" 18 19 #ifndef SQLITE_OMIT_DEPRECATED 20 /* 21 ** Return TRUE (non-zero) of the statement supplied as an argument needs 22 ** to be recompiled. A statement needs to be recompiled whenever the 23 ** execution environment changes in a way that would alter the program 24 ** that sqlite3_prepare() generates. For example, if new functions or 25 ** collating sequences are registered or if an authorizer function is 26 ** added or changed. 27 */ 28 int sqlite3_expired(sqlite3_stmt *pStmt){ 29 Vdbe *p = (Vdbe*)pStmt; 30 return p==0 || p->expired; 31 } 32 #endif 33 34 /* 35 ** Check on a Vdbe to make sure it has not been finalized. Log 36 ** an error and return true if it has been finalized (or is otherwise 37 ** invalid). Return false if it is ok. 38 */ 39 static int vdbeSafety(Vdbe *p){ 40 if( p->db==0 ){ 41 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); 42 return 1; 43 }else{ 44 return 0; 45 } 46 } 47 static int vdbeSafetyNotNull(Vdbe *p){ 48 if( p==0 ){ 49 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 50 return 1; 51 }else{ 52 return vdbeSafety(p); 53 } 54 } 55 56 #ifndef SQLITE_OMIT_TRACE 57 /* 58 ** Invoke the profile callback. This routine is only called if we already 59 ** know that the profile callback is defined and needs to be invoked. 60 */ 61 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 62 sqlite3_int64 iNow; 63 sqlite3_int64 iElapse; 64 assert( p->startTime>0 ); 65 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 ); 66 assert( db->init.busy==0 ); 67 assert( p->zSql!=0 ); 68 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 69 iElapse = (iNow - p->startTime)*1000000; 70 #ifndef SQLITE_OMIT_DEPRECATED 71 if( db->xProfile ){ 72 db->xProfile(db->pProfileArg, p->zSql, iElapse); 73 } 74 #endif 75 if( db->mTrace & SQLITE_TRACE_PROFILE ){ 76 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 77 } 78 p->startTime = 0; 79 } 80 /* 81 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 82 ** is needed, and it invokes the callback if it is needed. 83 */ 84 # define checkProfileCallback(DB,P) \ 85 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 86 #else 87 # define checkProfileCallback(DB,P) /*no-op*/ 88 #endif 89 90 /* 91 ** The following routine destroys a virtual machine that is created by 92 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 93 ** success/failure code that describes the result of executing the virtual 94 ** machine. 95 ** 96 ** This routine sets the error code and string returned by 97 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 98 */ 99 int sqlite3_finalize(sqlite3_stmt *pStmt){ 100 int rc; 101 if( pStmt==0 ){ 102 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 103 ** pointer is a harmless no-op. */ 104 rc = SQLITE_OK; 105 }else{ 106 Vdbe *v = (Vdbe*)pStmt; 107 sqlite3 *db = v->db; 108 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 109 sqlite3_mutex_enter(db->mutex); 110 checkProfileCallback(db, v); 111 rc = sqlite3VdbeFinalize(v); 112 rc = sqlite3ApiExit(db, rc); 113 sqlite3LeaveMutexAndCloseZombie(db); 114 } 115 return rc; 116 } 117 118 /* 119 ** Terminate the current execution of an SQL statement and reset it 120 ** back to its starting state so that it can be reused. A success code from 121 ** the prior execution is returned. 122 ** 123 ** This routine sets the error code and string returned by 124 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 125 */ 126 int sqlite3_reset(sqlite3_stmt *pStmt){ 127 int rc; 128 if( pStmt==0 ){ 129 rc = SQLITE_OK; 130 }else{ 131 Vdbe *v = (Vdbe*)pStmt; 132 sqlite3 *db = v->db; 133 sqlite3_mutex_enter(db->mutex); 134 checkProfileCallback(db, v); 135 rc = sqlite3VdbeReset(v); 136 sqlite3VdbeRewind(v); 137 assert( (rc & (db->errMask))==rc ); 138 rc = sqlite3ApiExit(db, rc); 139 sqlite3_mutex_leave(db->mutex); 140 } 141 return rc; 142 } 143 144 /* 145 ** Set all the parameters in the compiled SQL statement to NULL. 146 */ 147 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 148 int i; 149 int rc = SQLITE_OK; 150 Vdbe *p = (Vdbe*)pStmt; 151 #if SQLITE_THREADSAFE 152 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 153 #endif 154 sqlite3_mutex_enter(mutex); 155 for(i=0; i<p->nVar; i++){ 156 sqlite3VdbeMemRelease(&p->aVar[i]); 157 p->aVar[i].flags = MEM_Null; 158 } 159 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 160 if( p->expmask ){ 161 p->expired = 1; 162 } 163 sqlite3_mutex_leave(mutex); 164 return rc; 165 } 166 167 168 /**************************** sqlite3_value_ ******************************* 169 ** The following routines extract information from a Mem or sqlite3_value 170 ** structure. 171 */ 172 const void *sqlite3_value_blob(sqlite3_value *pVal){ 173 Mem *p = (Mem*)pVal; 174 if( p->flags & (MEM_Blob|MEM_Str) ){ 175 if( ExpandBlob(p)!=SQLITE_OK ){ 176 assert( p->flags==MEM_Null && p->z==0 ); 177 return 0; 178 } 179 p->flags |= MEM_Blob; 180 return p->n ? p->z : 0; 181 }else{ 182 return sqlite3_value_text(pVal); 183 } 184 } 185 int sqlite3_value_bytes(sqlite3_value *pVal){ 186 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 187 } 188 int sqlite3_value_bytes16(sqlite3_value *pVal){ 189 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 190 } 191 double sqlite3_value_double(sqlite3_value *pVal){ 192 return sqlite3VdbeRealValue((Mem*)pVal); 193 } 194 int sqlite3_value_int(sqlite3_value *pVal){ 195 return (int)sqlite3VdbeIntValue((Mem*)pVal); 196 } 197 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 198 return sqlite3VdbeIntValue((Mem*)pVal); 199 } 200 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 201 Mem *pMem = (Mem*)pVal; 202 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 203 } 204 void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ 205 Mem *p = (Mem*)pVal; 206 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 207 (MEM_Null|MEM_Term|MEM_Subtype) 208 && zPType!=0 209 && p->eSubtype=='p' 210 && strcmp(p->u.zPType, zPType)==0 211 ){ 212 return (void*)p->z; 213 }else{ 214 return 0; 215 } 216 } 217 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 218 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 219 } 220 #ifndef SQLITE_OMIT_UTF16 221 const void *sqlite3_value_text16(sqlite3_value* pVal){ 222 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 223 } 224 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 225 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 226 } 227 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 228 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 229 } 230 #endif /* SQLITE_OMIT_UTF16 */ 231 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 232 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 233 ** point number string BLOB NULL 234 */ 235 int sqlite3_value_type(sqlite3_value* pVal){ 236 static const u8 aType[] = { 237 SQLITE_BLOB, /* 0x00 (not possible) */ 238 SQLITE_NULL, /* 0x01 NULL */ 239 SQLITE_TEXT, /* 0x02 TEXT */ 240 SQLITE_NULL, /* 0x03 (not possible) */ 241 SQLITE_INTEGER, /* 0x04 INTEGER */ 242 SQLITE_NULL, /* 0x05 (not possible) */ 243 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ 244 SQLITE_NULL, /* 0x07 (not possible) */ 245 SQLITE_FLOAT, /* 0x08 FLOAT */ 246 SQLITE_NULL, /* 0x09 (not possible) */ 247 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ 248 SQLITE_NULL, /* 0x0b (not possible) */ 249 SQLITE_INTEGER, /* 0x0c (not possible) */ 250 SQLITE_NULL, /* 0x0d (not possible) */ 251 SQLITE_INTEGER, /* 0x0e (not possible) */ 252 SQLITE_NULL, /* 0x0f (not possible) */ 253 SQLITE_BLOB, /* 0x10 BLOB */ 254 SQLITE_NULL, /* 0x11 (not possible) */ 255 SQLITE_TEXT, /* 0x12 (not possible) */ 256 SQLITE_NULL, /* 0x13 (not possible) */ 257 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ 258 SQLITE_NULL, /* 0x15 (not possible) */ 259 SQLITE_INTEGER, /* 0x16 (not possible) */ 260 SQLITE_NULL, /* 0x17 (not possible) */ 261 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ 262 SQLITE_NULL, /* 0x19 (not possible) */ 263 SQLITE_FLOAT, /* 0x1a (not possible) */ 264 SQLITE_NULL, /* 0x1b (not possible) */ 265 SQLITE_INTEGER, /* 0x1c (not possible) */ 266 SQLITE_NULL, /* 0x1d (not possible) */ 267 SQLITE_INTEGER, /* 0x1e (not possible) */ 268 SQLITE_NULL, /* 0x1f (not possible) */ 269 SQLITE_FLOAT, /* 0x20 INTREAL */ 270 SQLITE_NULL, /* 0x21 (not possible) */ 271 SQLITE_TEXT, /* 0x22 INTREAL + TEXT */ 272 SQLITE_NULL, /* 0x23 (not possible) */ 273 SQLITE_FLOAT, /* 0x24 (not possible) */ 274 SQLITE_NULL, /* 0x25 (not possible) */ 275 SQLITE_FLOAT, /* 0x26 (not possible) */ 276 SQLITE_NULL, /* 0x27 (not possible) */ 277 SQLITE_FLOAT, /* 0x28 (not possible) */ 278 SQLITE_NULL, /* 0x29 (not possible) */ 279 SQLITE_FLOAT, /* 0x2a (not possible) */ 280 SQLITE_NULL, /* 0x2b (not possible) */ 281 SQLITE_FLOAT, /* 0x2c (not possible) */ 282 SQLITE_NULL, /* 0x2d (not possible) */ 283 SQLITE_FLOAT, /* 0x2e (not possible) */ 284 SQLITE_NULL, /* 0x2f (not possible) */ 285 SQLITE_BLOB, /* 0x30 (not possible) */ 286 SQLITE_NULL, /* 0x31 (not possible) */ 287 SQLITE_TEXT, /* 0x32 (not possible) */ 288 SQLITE_NULL, /* 0x33 (not possible) */ 289 SQLITE_FLOAT, /* 0x34 (not possible) */ 290 SQLITE_NULL, /* 0x35 (not possible) */ 291 SQLITE_FLOAT, /* 0x36 (not possible) */ 292 SQLITE_NULL, /* 0x37 (not possible) */ 293 SQLITE_FLOAT, /* 0x38 (not possible) */ 294 SQLITE_NULL, /* 0x39 (not possible) */ 295 SQLITE_FLOAT, /* 0x3a (not possible) */ 296 SQLITE_NULL, /* 0x3b (not possible) */ 297 SQLITE_FLOAT, /* 0x3c (not possible) */ 298 SQLITE_NULL, /* 0x3d (not possible) */ 299 SQLITE_FLOAT, /* 0x3e (not possible) */ 300 SQLITE_NULL, /* 0x3f (not possible) */ 301 }; 302 #ifdef SQLITE_DEBUG 303 { 304 int eType = SQLITE_BLOB; 305 if( pVal->flags & MEM_Null ){ 306 eType = SQLITE_NULL; 307 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ 308 eType = SQLITE_FLOAT; 309 }else if( pVal->flags & MEM_Int ){ 310 eType = SQLITE_INTEGER; 311 }else if( pVal->flags & MEM_Str ){ 312 eType = SQLITE_TEXT; 313 } 314 assert( eType == aType[pVal->flags&MEM_AffMask] ); 315 } 316 #endif 317 return aType[pVal->flags&MEM_AffMask]; 318 } 319 320 /* Return true if a parameter to xUpdate represents an unchanged column */ 321 int sqlite3_value_nochange(sqlite3_value *pVal){ 322 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); 323 } 324 325 /* Return true if a parameter value originated from an sqlite3_bind() */ 326 int sqlite3_value_frombind(sqlite3_value *pVal){ 327 return (pVal->flags&MEM_FromBind)!=0; 328 } 329 330 /* Make a copy of an sqlite3_value object 331 */ 332 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 333 sqlite3_value *pNew; 334 if( pOrig==0 ) return 0; 335 pNew = sqlite3_malloc( sizeof(*pNew) ); 336 if( pNew==0 ) return 0; 337 memset(pNew, 0, sizeof(*pNew)); 338 memcpy(pNew, pOrig, MEMCELLSIZE); 339 pNew->flags &= ~MEM_Dyn; 340 pNew->db = 0; 341 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 342 pNew->flags &= ~(MEM_Static|MEM_Dyn); 343 pNew->flags |= MEM_Ephem; 344 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 345 sqlite3ValueFree(pNew); 346 pNew = 0; 347 } 348 }else if( pNew->flags & MEM_Null ){ 349 /* Do not duplicate pointer values */ 350 pNew->flags &= ~(MEM_Term|MEM_Subtype); 351 } 352 return pNew; 353 } 354 355 /* Destroy an sqlite3_value object previously obtained from 356 ** sqlite3_value_dup(). 357 */ 358 void sqlite3_value_free(sqlite3_value *pOld){ 359 sqlite3ValueFree(pOld); 360 } 361 362 363 /**************************** sqlite3_result_ ******************************* 364 ** The following routines are used by user-defined functions to specify 365 ** the function result. 366 ** 367 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 368 ** result as a string or blob. Appropriate errors are set if the string/blob 369 ** is too big or if an OOM occurs. 370 ** 371 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 372 ** on value P is not going to be used and need to be destroyed. 373 */ 374 static void setResultStrOrError( 375 sqlite3_context *pCtx, /* Function context */ 376 const char *z, /* String pointer */ 377 int n, /* Bytes in string, or negative */ 378 u8 enc, /* Encoding of z. 0 for BLOBs */ 379 void (*xDel)(void*) /* Destructor function */ 380 ){ 381 int rc = sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel); 382 if( rc ){ 383 if( rc==SQLITE_TOOBIG ){ 384 sqlite3_result_error_toobig(pCtx); 385 }else{ 386 /* The only errors possible from sqlite3VdbeMemSetStr are 387 ** SQLITE_TOOBIG and SQLITE_NOMEM */ 388 assert( rc==SQLITE_NOMEM ); 389 sqlite3_result_error_nomem(pCtx); 390 } 391 } 392 } 393 static int invokeValueDestructor( 394 const void *p, /* Value to destroy */ 395 void (*xDel)(void*), /* The destructor */ 396 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ 397 ){ 398 assert( xDel!=SQLITE_DYNAMIC ); 399 if( xDel==0 ){ 400 /* noop */ 401 }else if( xDel==SQLITE_TRANSIENT ){ 402 /* noop */ 403 }else{ 404 xDel((void*)p); 405 } 406 sqlite3_result_error_toobig(pCtx); 407 return SQLITE_TOOBIG; 408 } 409 void sqlite3_result_blob( 410 sqlite3_context *pCtx, 411 const void *z, 412 int n, 413 void (*xDel)(void *) 414 ){ 415 assert( n>=0 ); 416 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 417 setResultStrOrError(pCtx, z, n, 0, xDel); 418 } 419 void sqlite3_result_blob64( 420 sqlite3_context *pCtx, 421 const void *z, 422 sqlite3_uint64 n, 423 void (*xDel)(void *) 424 ){ 425 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 426 assert( xDel!=SQLITE_DYNAMIC ); 427 if( n>0x7fffffff ){ 428 (void)invokeValueDestructor(z, xDel, pCtx); 429 }else{ 430 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 431 } 432 } 433 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 434 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 435 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 436 } 437 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 438 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 439 pCtx->isError = SQLITE_ERROR; 440 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 441 } 442 #ifndef SQLITE_OMIT_UTF16 443 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 444 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 445 pCtx->isError = SQLITE_ERROR; 446 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 447 } 448 #endif 449 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 450 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 451 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 452 } 453 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 454 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 455 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 456 } 457 void sqlite3_result_null(sqlite3_context *pCtx){ 458 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 459 sqlite3VdbeMemSetNull(pCtx->pOut); 460 } 461 void sqlite3_result_pointer( 462 sqlite3_context *pCtx, 463 void *pPtr, 464 const char *zPType, 465 void (*xDestructor)(void*) 466 ){ 467 Mem *pOut = pCtx->pOut; 468 assert( sqlite3_mutex_held(pOut->db->mutex) ); 469 sqlite3VdbeMemRelease(pOut); 470 pOut->flags = MEM_Null; 471 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); 472 } 473 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 474 Mem *pOut = pCtx->pOut; 475 assert( sqlite3_mutex_held(pOut->db->mutex) ); 476 pOut->eSubtype = eSubtype & 0xff; 477 pOut->flags |= MEM_Subtype; 478 } 479 void sqlite3_result_text( 480 sqlite3_context *pCtx, 481 const char *z, 482 int n, 483 void (*xDel)(void *) 484 ){ 485 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 486 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 487 } 488 void sqlite3_result_text64( 489 sqlite3_context *pCtx, 490 const char *z, 491 sqlite3_uint64 n, 492 void (*xDel)(void *), 493 unsigned char enc 494 ){ 495 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 496 assert( xDel!=SQLITE_DYNAMIC ); 497 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 498 if( n>0x7fffffff ){ 499 (void)invokeValueDestructor(z, xDel, pCtx); 500 }else{ 501 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 502 } 503 } 504 #ifndef SQLITE_OMIT_UTF16 505 void sqlite3_result_text16( 506 sqlite3_context *pCtx, 507 const void *z, 508 int n, 509 void (*xDel)(void *) 510 ){ 511 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 512 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 513 } 514 void sqlite3_result_text16be( 515 sqlite3_context *pCtx, 516 const void *z, 517 int n, 518 void (*xDel)(void *) 519 ){ 520 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 521 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 522 } 523 void sqlite3_result_text16le( 524 sqlite3_context *pCtx, 525 const void *z, 526 int n, 527 void (*xDel)(void *) 528 ){ 529 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 530 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 531 } 532 #endif /* SQLITE_OMIT_UTF16 */ 533 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 534 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 535 sqlite3VdbeMemCopy(pCtx->pOut, pValue); 536 } 537 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 538 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 539 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); 540 } 541 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 542 Mem *pOut = pCtx->pOut; 543 assert( sqlite3_mutex_held(pOut->db->mutex) ); 544 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 545 return SQLITE_TOOBIG; 546 } 547 #ifndef SQLITE_OMIT_INCRBLOB 548 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 549 return SQLITE_OK; 550 #else 551 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 552 #endif 553 } 554 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 555 pCtx->isError = errCode ? errCode : -1; 556 #ifdef SQLITE_DEBUG 557 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 558 #endif 559 if( pCtx->pOut->flags & MEM_Null ){ 560 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 561 SQLITE_UTF8, SQLITE_STATIC); 562 } 563 } 564 565 /* Force an SQLITE_TOOBIG error. */ 566 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 567 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 568 pCtx->isError = SQLITE_TOOBIG; 569 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 570 SQLITE_UTF8, SQLITE_STATIC); 571 } 572 573 /* An SQLITE_NOMEM error. */ 574 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 575 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 576 sqlite3VdbeMemSetNull(pCtx->pOut); 577 pCtx->isError = SQLITE_NOMEM_BKPT; 578 sqlite3OomFault(pCtx->pOut->db); 579 } 580 581 #ifndef SQLITE_UNTESTABLE 582 /* Force the INT64 value currently stored as the result to be 583 ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL 584 ** test-control. 585 */ 586 void sqlite3ResultIntReal(sqlite3_context *pCtx){ 587 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 588 if( pCtx->pOut->flags & MEM_Int ){ 589 pCtx->pOut->flags &= ~MEM_Int; 590 pCtx->pOut->flags |= MEM_IntReal; 591 } 592 } 593 #endif 594 595 596 /* 597 ** This function is called after a transaction has been committed. It 598 ** invokes callbacks registered with sqlite3_wal_hook() as required. 599 */ 600 static int doWalCallbacks(sqlite3 *db){ 601 int rc = SQLITE_OK; 602 #ifndef SQLITE_OMIT_WAL 603 int i; 604 for(i=0; i<db->nDb; i++){ 605 Btree *pBt = db->aDb[i].pBt; 606 if( pBt ){ 607 int nEntry; 608 sqlite3BtreeEnter(pBt); 609 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 610 sqlite3BtreeLeave(pBt); 611 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ 612 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); 613 } 614 } 615 } 616 #endif 617 return rc; 618 } 619 620 621 /* 622 ** Execute the statement pStmt, either until a row of data is ready, the 623 ** statement is completely executed or an error occurs. 624 ** 625 ** This routine implements the bulk of the logic behind the sqlite_step() 626 ** API. The only thing omitted is the automatic recompile if a 627 ** schema change has occurred. That detail is handled by the 628 ** outer sqlite3_step() wrapper procedure. 629 */ 630 static int sqlite3Step(Vdbe *p){ 631 sqlite3 *db; 632 int rc; 633 634 assert(p); 635 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){ 636 /* We used to require that sqlite3_reset() be called before retrying 637 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 638 ** with version 3.7.0, we changed this so that sqlite3_reset() would 639 ** be called automatically instead of throwing the SQLITE_MISUSE error. 640 ** This "automatic-reset" change is not technically an incompatibility, 641 ** since any application that receives an SQLITE_MISUSE is broken by 642 ** definition. 643 ** 644 ** Nevertheless, some published applications that were originally written 645 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 646 ** returns, and those were broken by the automatic-reset change. As a 647 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 648 ** legacy behavior of returning SQLITE_MISUSE for cases where the 649 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 650 ** or SQLITE_BUSY error. 651 */ 652 #ifdef SQLITE_OMIT_AUTORESET 653 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 654 sqlite3_reset((sqlite3_stmt*)p); 655 }else{ 656 return SQLITE_MISUSE_BKPT; 657 } 658 #else 659 sqlite3_reset((sqlite3_stmt*)p); 660 #endif 661 } 662 663 /* Check that malloc() has not failed. If it has, return early. */ 664 db = p->db; 665 if( db->mallocFailed ){ 666 p->rc = SQLITE_NOMEM; 667 return SQLITE_NOMEM_BKPT; 668 } 669 670 if( p->pc<0 && p->expired ){ 671 p->rc = SQLITE_SCHEMA; 672 rc = SQLITE_ERROR; 673 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 674 /* If this statement was prepared using saved SQL and an 675 ** error has occurred, then return the error code in p->rc to the 676 ** caller. Set the error code in the database handle to the same value. 677 */ 678 rc = sqlite3VdbeTransferError(p); 679 } 680 goto end_of_step; 681 } 682 if( p->pc<0 ){ 683 /* If there are no other statements currently running, then 684 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 685 ** from interrupting a statement that has not yet started. 686 */ 687 if( db->nVdbeActive==0 ){ 688 AtomicStore(&db->u1.isInterrupted, 0); 689 } 690 691 assert( db->nVdbeWrite>0 || db->autoCommit==0 692 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 693 ); 694 695 #ifndef SQLITE_OMIT_TRACE 696 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 697 && !db->init.busy && p->zSql ){ 698 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 699 }else{ 700 assert( p->startTime==0 ); 701 } 702 #endif 703 704 db->nVdbeActive++; 705 if( p->readOnly==0 ) db->nVdbeWrite++; 706 if( p->bIsReader ) db->nVdbeRead++; 707 p->pc = 0; 708 } 709 #ifdef SQLITE_DEBUG 710 p->rcApp = SQLITE_OK; 711 #endif 712 #ifndef SQLITE_OMIT_EXPLAIN 713 if( p->explain ){ 714 rc = sqlite3VdbeList(p); 715 }else 716 #endif /* SQLITE_OMIT_EXPLAIN */ 717 { 718 db->nVdbeExec++; 719 rc = sqlite3VdbeExec(p); 720 db->nVdbeExec--; 721 } 722 723 if( rc!=SQLITE_ROW ){ 724 #ifndef SQLITE_OMIT_TRACE 725 /* If the statement completed successfully, invoke the profile callback */ 726 checkProfileCallback(db, p); 727 #endif 728 729 if( rc==SQLITE_DONE && db->autoCommit ){ 730 assert( p->rc==SQLITE_OK ); 731 p->rc = doWalCallbacks(db); 732 if( p->rc!=SQLITE_OK ){ 733 rc = SQLITE_ERROR; 734 } 735 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 736 /* If this statement was prepared using saved SQL and an 737 ** error has occurred, then return the error code in p->rc to the 738 ** caller. Set the error code in the database handle to the same value. 739 */ 740 rc = sqlite3VdbeTransferError(p); 741 } 742 } 743 744 db->errCode = rc; 745 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 746 p->rc = SQLITE_NOMEM_BKPT; 747 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; 748 } 749 end_of_step: 750 /* There are only a limited number of result codes allowed from the 751 ** statements prepared using the legacy sqlite3_prepare() interface */ 752 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 753 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 754 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 755 ); 756 return (rc&db->errMask); 757 } 758 759 /* 760 ** This is the top-level implementation of sqlite3_step(). Call 761 ** sqlite3Step() to do most of the work. If a schema error occurs, 762 ** call sqlite3Reprepare() and try again. 763 */ 764 int sqlite3_step(sqlite3_stmt *pStmt){ 765 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 766 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 767 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 768 sqlite3 *db; /* The database connection */ 769 770 if( vdbeSafetyNotNull(v) ){ 771 return SQLITE_MISUSE_BKPT; 772 } 773 db = v->db; 774 sqlite3_mutex_enter(db->mutex); 775 v->doingRerun = 0; 776 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 777 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 778 int savedPc = v->pc; 779 rc = sqlite3Reprepare(v); 780 if( rc!=SQLITE_OK ){ 781 /* This case occurs after failing to recompile an sql statement. 782 ** The error message from the SQL compiler has already been loaded 783 ** into the database handle. This block copies the error message 784 ** from the database handle into the statement and sets the statement 785 ** program counter to 0 to ensure that when the statement is 786 ** finalized or reset the parser error message is available via 787 ** sqlite3_errmsg() and sqlite3_errcode(). 788 */ 789 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 790 sqlite3DbFree(db, v->zErrMsg); 791 if( !db->mallocFailed ){ 792 v->zErrMsg = sqlite3DbStrDup(db, zErr); 793 v->rc = rc = sqlite3ApiExit(db, rc); 794 } else { 795 v->zErrMsg = 0; 796 v->rc = rc = SQLITE_NOMEM_BKPT; 797 } 798 break; 799 } 800 sqlite3_reset(pStmt); 801 if( savedPc>=0 ) v->doingRerun = 1; 802 assert( v->expired==0 ); 803 } 804 sqlite3_mutex_leave(db->mutex); 805 return rc; 806 } 807 808 809 /* 810 ** Extract the user data from a sqlite3_context structure and return a 811 ** pointer to it. 812 */ 813 void *sqlite3_user_data(sqlite3_context *p){ 814 assert( p && p->pFunc ); 815 return p->pFunc->pUserData; 816 } 817 818 /* 819 ** Extract the user data from a sqlite3_context structure and return a 820 ** pointer to it. 821 ** 822 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 823 ** returns a copy of the pointer to the database connection (the 1st 824 ** parameter) of the sqlite3_create_function() and 825 ** sqlite3_create_function16() routines that originally registered the 826 ** application defined function. 827 */ 828 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 829 assert( p && p->pOut ); 830 return p->pOut->db; 831 } 832 833 /* 834 ** If this routine is invoked from within an xColumn method of a virtual 835 ** table, then it returns true if and only if the the call is during an 836 ** UPDATE operation and the value of the column will not be modified 837 ** by the UPDATE. 838 ** 839 ** If this routine is called from any context other than within the 840 ** xColumn method of a virtual table, then the return value is meaningless 841 ** and arbitrary. 842 ** 843 ** Virtual table implements might use this routine to optimize their 844 ** performance by substituting a NULL result, or some other light-weight 845 ** value, as a signal to the xUpdate routine that the column is unchanged. 846 */ 847 int sqlite3_vtab_nochange(sqlite3_context *p){ 848 assert( p ); 849 return sqlite3_value_nochange(p->pOut); 850 } 851 852 /* 853 ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and 854 ** sqlite3_vtab_in_next() (if bNext!=0). 855 */ 856 static int valueFromValueList( 857 sqlite3_value *pVal, /* Pointer to the ValueList object */ 858 sqlite3_value **ppOut, /* Store the next value from the list here */ 859 int bNext /* 1 for _next(). 0 for _first() */ 860 ){ 861 int rc; 862 ValueList *pRhs; 863 864 *ppOut = 0; 865 if( pVal==0 ) return SQLITE_MISUSE; 866 pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList"); 867 if( pRhs==0 ) return SQLITE_MISUSE; 868 if( bNext ){ 869 rc = sqlite3BtreeNext(pRhs->pCsr, 0); 870 }else{ 871 int dummy = 0; 872 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); 873 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); 874 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; 875 } 876 if( rc==SQLITE_OK ){ 877 u32 sz; /* Size of current row in bytes */ 878 Mem sMem; /* Raw content of current row */ 879 memset(&sMem, 0, sizeof(sMem)); 880 sz = sqlite3BtreePayloadSize(pRhs->pCsr); 881 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); 882 if( rc==SQLITE_OK ){ 883 u8 *zBuf = (u8*)sMem.z; 884 u32 iSerial; 885 sqlite3_value *pOut = pRhs->pOut; 886 int iOff = 1 + getVarint32(&zBuf[1], iSerial); 887 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); 888 pOut->enc = ENC(pOut->db); 889 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ 890 rc = SQLITE_NOMEM; 891 }else{ 892 *ppOut = pOut; 893 } 894 } 895 sqlite3VdbeMemRelease(&sMem); 896 } 897 return rc; 898 } 899 900 /* 901 ** Set the iterator value pVal to point to the first value in the set. 902 ** Set (*ppOut) to point to this value before returning. 903 */ 904 int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ 905 return valueFromValueList(pVal, ppOut, 0); 906 } 907 908 /* 909 ** Set the iterator value pVal to point to the next value in the set. 910 ** Set (*ppOut) to point to this value before returning. 911 */ 912 int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ 913 return valueFromValueList(pVal, ppOut, 1); 914 } 915 916 /* 917 ** Return the current time for a statement. If the current time 918 ** is requested more than once within the same run of a single prepared 919 ** statement, the exact same time is returned for each invocation regardless 920 ** of the amount of time that elapses between invocations. In other words, 921 ** the time returned is always the time of the first call. 922 */ 923 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 924 int rc; 925 #ifndef SQLITE_ENABLE_STAT4 926 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 927 assert( p->pVdbe!=0 ); 928 #else 929 sqlite3_int64 iTime = 0; 930 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 931 #endif 932 if( *piTime==0 ){ 933 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 934 if( rc ) *piTime = 0; 935 } 936 return *piTime; 937 } 938 939 /* 940 ** Create a new aggregate context for p and return a pointer to 941 ** its pMem->z element. 942 */ 943 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 944 Mem *pMem = p->pMem; 945 assert( (pMem->flags & MEM_Agg)==0 ); 946 if( nByte<=0 ){ 947 sqlite3VdbeMemSetNull(pMem); 948 pMem->z = 0; 949 }else{ 950 sqlite3VdbeMemClearAndResize(pMem, nByte); 951 pMem->flags = MEM_Agg; 952 pMem->u.pDef = p->pFunc; 953 if( pMem->z ){ 954 memset(pMem->z, 0, nByte); 955 } 956 } 957 return (void*)pMem->z; 958 } 959 960 /* 961 ** Allocate or return the aggregate context for a user function. A new 962 ** context is allocated on the first call. Subsequent calls return the 963 ** same context that was returned on prior calls. 964 */ 965 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 966 assert( p && p->pFunc && p->pFunc->xFinalize ); 967 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 968 testcase( nByte<0 ); 969 if( (p->pMem->flags & MEM_Agg)==0 ){ 970 return createAggContext(p, nByte); 971 }else{ 972 return (void*)p->pMem->z; 973 } 974 } 975 976 /* 977 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 978 ** the user-function defined by pCtx. 979 ** 980 ** The left-most argument is 0. 981 ** 982 ** Undocumented behavior: If iArg is negative then access a cache of 983 ** auxiliary data pointers that is available to all functions within a 984 ** single prepared statement. The iArg values must match. 985 */ 986 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 987 AuxData *pAuxData; 988 989 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 990 #if SQLITE_ENABLE_STAT4 991 if( pCtx->pVdbe==0 ) return 0; 992 #else 993 assert( pCtx->pVdbe!=0 ); 994 #endif 995 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 996 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 997 return pAuxData->pAux; 998 } 999 } 1000 return 0; 1001 } 1002 1003 /* 1004 ** Set the auxiliary data pointer and delete function, for the iArg'th 1005 ** argument to the user-function defined by pCtx. Any previous value is 1006 ** deleted by calling the delete function specified when it was set. 1007 ** 1008 ** The left-most argument is 0. 1009 ** 1010 ** Undocumented behavior: If iArg is negative then make the data available 1011 ** to all functions within the current prepared statement using iArg as an 1012 ** access code. 1013 */ 1014 void sqlite3_set_auxdata( 1015 sqlite3_context *pCtx, 1016 int iArg, 1017 void *pAux, 1018 void (*xDelete)(void*) 1019 ){ 1020 AuxData *pAuxData; 1021 Vdbe *pVdbe = pCtx->pVdbe; 1022 1023 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 1024 #ifdef SQLITE_ENABLE_STAT4 1025 if( pVdbe==0 ) goto failed; 1026 #else 1027 assert( pVdbe!=0 ); 1028 #endif 1029 1030 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 1031 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 1032 break; 1033 } 1034 } 1035 if( pAuxData==0 ){ 1036 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 1037 if( !pAuxData ) goto failed; 1038 pAuxData->iAuxOp = pCtx->iOp; 1039 pAuxData->iAuxArg = iArg; 1040 pAuxData->pNextAux = pVdbe->pAuxData; 1041 pVdbe->pAuxData = pAuxData; 1042 if( pCtx->isError==0 ) pCtx->isError = -1; 1043 }else if( pAuxData->xDeleteAux ){ 1044 pAuxData->xDeleteAux(pAuxData->pAux); 1045 } 1046 1047 pAuxData->pAux = pAux; 1048 pAuxData->xDeleteAux = xDelete; 1049 return; 1050 1051 failed: 1052 if( xDelete ){ 1053 xDelete(pAux); 1054 } 1055 } 1056 1057 #ifndef SQLITE_OMIT_DEPRECATED 1058 /* 1059 ** Return the number of times the Step function of an aggregate has been 1060 ** called. 1061 ** 1062 ** This function is deprecated. Do not use it for new code. It is 1063 ** provide only to avoid breaking legacy code. New aggregate function 1064 ** implementations should keep their own counts within their aggregate 1065 ** context. 1066 */ 1067 int sqlite3_aggregate_count(sqlite3_context *p){ 1068 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 1069 return p->pMem->n; 1070 } 1071 #endif 1072 1073 /* 1074 ** Return the number of columns in the result set for the statement pStmt. 1075 */ 1076 int sqlite3_column_count(sqlite3_stmt *pStmt){ 1077 Vdbe *pVm = (Vdbe *)pStmt; 1078 return pVm ? pVm->nResColumn : 0; 1079 } 1080 1081 /* 1082 ** Return the number of values available from the current row of the 1083 ** currently executing statement pStmt. 1084 */ 1085 int sqlite3_data_count(sqlite3_stmt *pStmt){ 1086 Vdbe *pVm = (Vdbe *)pStmt; 1087 if( pVm==0 || pVm->pResultSet==0 ) return 0; 1088 return pVm->nResColumn; 1089 } 1090 1091 /* 1092 ** Return a pointer to static memory containing an SQL NULL value. 1093 */ 1094 static const Mem *columnNullValue(void){ 1095 /* Even though the Mem structure contains an element 1096 ** of type i64, on certain architectures (x86) with certain compiler 1097 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 1098 ** instead of an 8-byte one. This all works fine, except that when 1099 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 1100 ** that a Mem structure is located on an 8-byte boundary. To prevent 1101 ** these assert()s from failing, when building with SQLITE_DEBUG defined 1102 ** using gcc, we force nullMem to be 8-byte aligned using the magical 1103 ** __attribute__((aligned(8))) macro. */ 1104 static const Mem nullMem 1105 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 1106 __attribute__((aligned(8))) 1107 #endif 1108 = { 1109 /* .u = */ {0}, 1110 /* .z = */ (char*)0, 1111 /* .n = */ (int)0, 1112 /* .flags = */ (u16)MEM_Null, 1113 /* .enc = */ (u8)0, 1114 /* .eSubtype = */ (u8)0, 1115 /* .db = */ (sqlite3*)0, 1116 /* .szMalloc = */ (int)0, 1117 /* .uTemp = */ (u32)0, 1118 /* .zMalloc = */ (char*)0, 1119 /* .xDel = */ (void(*)(void*))0, 1120 #ifdef SQLITE_DEBUG 1121 /* .pScopyFrom = */ (Mem*)0, 1122 /* .mScopyFlags= */ 0, 1123 #endif 1124 }; 1125 return &nullMem; 1126 } 1127 1128 /* 1129 ** Check to see if column iCol of the given statement is valid. If 1130 ** it is, return a pointer to the Mem for the value of that column. 1131 ** If iCol is not valid, return a pointer to a Mem which has a value 1132 ** of NULL. 1133 */ 1134 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 1135 Vdbe *pVm; 1136 Mem *pOut; 1137 1138 pVm = (Vdbe *)pStmt; 1139 if( pVm==0 ) return (Mem*)columnNullValue(); 1140 assert( pVm->db ); 1141 sqlite3_mutex_enter(pVm->db->mutex); 1142 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 1143 pOut = &pVm->pResultSet[i]; 1144 }else{ 1145 sqlite3Error(pVm->db, SQLITE_RANGE); 1146 pOut = (Mem*)columnNullValue(); 1147 } 1148 return pOut; 1149 } 1150 1151 /* 1152 ** This function is called after invoking an sqlite3_value_XXX function on a 1153 ** column value (i.e. a value returned by evaluating an SQL expression in the 1154 ** select list of a SELECT statement) that may cause a malloc() failure. If 1155 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 1156 ** code of statement pStmt set to SQLITE_NOMEM. 1157 ** 1158 ** Specifically, this is called from within: 1159 ** 1160 ** sqlite3_column_int() 1161 ** sqlite3_column_int64() 1162 ** sqlite3_column_text() 1163 ** sqlite3_column_text16() 1164 ** sqlite3_column_real() 1165 ** sqlite3_column_bytes() 1166 ** sqlite3_column_bytes16() 1167 ** sqiite3_column_blob() 1168 */ 1169 static void columnMallocFailure(sqlite3_stmt *pStmt) 1170 { 1171 /* If malloc() failed during an encoding conversion within an 1172 ** sqlite3_column_XXX API, then set the return code of the statement to 1173 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 1174 ** and _finalize() will return NOMEM. 1175 */ 1176 Vdbe *p = (Vdbe *)pStmt; 1177 if( p ){ 1178 assert( p->db!=0 ); 1179 assert( sqlite3_mutex_held(p->db->mutex) ); 1180 p->rc = sqlite3ApiExit(p->db, p->rc); 1181 sqlite3_mutex_leave(p->db->mutex); 1182 } 1183 } 1184 1185 /**************************** sqlite3_column_ ******************************* 1186 ** The following routines are used to access elements of the current row 1187 ** in the result set. 1188 */ 1189 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 1190 const void *val; 1191 val = sqlite3_value_blob( columnMem(pStmt,i) ); 1192 /* Even though there is no encoding conversion, value_blob() might 1193 ** need to call malloc() to expand the result of a zeroblob() 1194 ** expression. 1195 */ 1196 columnMallocFailure(pStmt); 1197 return val; 1198 } 1199 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 1200 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 1201 columnMallocFailure(pStmt); 1202 return val; 1203 } 1204 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 1205 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 1206 columnMallocFailure(pStmt); 1207 return val; 1208 } 1209 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 1210 double val = sqlite3_value_double( columnMem(pStmt,i) ); 1211 columnMallocFailure(pStmt); 1212 return val; 1213 } 1214 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 1215 int val = sqlite3_value_int( columnMem(pStmt,i) ); 1216 columnMallocFailure(pStmt); 1217 return val; 1218 } 1219 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 1220 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 1221 columnMallocFailure(pStmt); 1222 return val; 1223 } 1224 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 1225 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 1226 columnMallocFailure(pStmt); 1227 return val; 1228 } 1229 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 1230 Mem *pOut = columnMem(pStmt, i); 1231 if( pOut->flags&MEM_Static ){ 1232 pOut->flags &= ~MEM_Static; 1233 pOut->flags |= MEM_Ephem; 1234 } 1235 columnMallocFailure(pStmt); 1236 return (sqlite3_value *)pOut; 1237 } 1238 #ifndef SQLITE_OMIT_UTF16 1239 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 1240 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 1241 columnMallocFailure(pStmt); 1242 return val; 1243 } 1244 #endif /* SQLITE_OMIT_UTF16 */ 1245 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 1246 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 1247 columnMallocFailure(pStmt); 1248 return iType; 1249 } 1250 1251 /* 1252 ** Convert the N-th element of pStmt->pColName[] into a string using 1253 ** xFunc() then return that string. If N is out of range, return 0. 1254 ** 1255 ** There are up to 5 names for each column. useType determines which 1256 ** name is returned. Here are the names: 1257 ** 1258 ** 0 The column name as it should be displayed for output 1259 ** 1 The datatype name for the column 1260 ** 2 The name of the database that the column derives from 1261 ** 3 The name of the table that the column derives from 1262 ** 4 The name of the table column that the result column derives from 1263 ** 1264 ** If the result is not a simple column reference (if it is an expression 1265 ** or a constant) then useTypes 2, 3, and 4 return NULL. 1266 */ 1267 static const void *columnName( 1268 sqlite3_stmt *pStmt, /* The statement */ 1269 int N, /* Which column to get the name for */ 1270 int useUtf16, /* True to return the name as UTF16 */ 1271 int useType /* What type of name */ 1272 ){ 1273 const void *ret; 1274 Vdbe *p; 1275 int n; 1276 sqlite3 *db; 1277 #ifdef SQLITE_ENABLE_API_ARMOR 1278 if( pStmt==0 ){ 1279 (void)SQLITE_MISUSE_BKPT; 1280 return 0; 1281 } 1282 #endif 1283 ret = 0; 1284 p = (Vdbe *)pStmt; 1285 db = p->db; 1286 assert( db!=0 ); 1287 n = sqlite3_column_count(pStmt); 1288 if( N<n && N>=0 ){ 1289 N += useType*n; 1290 sqlite3_mutex_enter(db->mutex); 1291 assert( db->mallocFailed==0 ); 1292 #ifndef SQLITE_OMIT_UTF16 1293 if( useUtf16 ){ 1294 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); 1295 }else 1296 #endif 1297 { 1298 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); 1299 } 1300 /* A malloc may have failed inside of the _text() call. If this 1301 ** is the case, clear the mallocFailed flag and return NULL. 1302 */ 1303 if( db->mallocFailed ){ 1304 sqlite3OomClear(db); 1305 ret = 0; 1306 } 1307 sqlite3_mutex_leave(db->mutex); 1308 } 1309 return ret; 1310 } 1311 1312 /* 1313 ** Return the name of the Nth column of the result set returned by SQL 1314 ** statement pStmt. 1315 */ 1316 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 1317 return columnName(pStmt, N, 0, COLNAME_NAME); 1318 } 1319 #ifndef SQLITE_OMIT_UTF16 1320 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 1321 return columnName(pStmt, N, 1, COLNAME_NAME); 1322 } 1323 #endif 1324 1325 /* 1326 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 1327 ** not define OMIT_DECLTYPE. 1328 */ 1329 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 1330 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 1331 and SQLITE_ENABLE_COLUMN_METADATA" 1332 #endif 1333 1334 #ifndef SQLITE_OMIT_DECLTYPE 1335 /* 1336 ** Return the column declaration type (if applicable) of the 'i'th column 1337 ** of the result set of SQL statement pStmt. 1338 */ 1339 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 1340 return columnName(pStmt, N, 0, COLNAME_DECLTYPE); 1341 } 1342 #ifndef SQLITE_OMIT_UTF16 1343 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 1344 return columnName(pStmt, N, 1, COLNAME_DECLTYPE); 1345 } 1346 #endif /* SQLITE_OMIT_UTF16 */ 1347 #endif /* SQLITE_OMIT_DECLTYPE */ 1348 1349 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1350 /* 1351 ** Return the name of the database from which a result column derives. 1352 ** NULL is returned if the result column is an expression or constant or 1353 ** anything else which is not an unambiguous reference to a database column. 1354 */ 1355 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1356 return columnName(pStmt, N, 0, COLNAME_DATABASE); 1357 } 1358 #ifndef SQLITE_OMIT_UTF16 1359 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1360 return columnName(pStmt, N, 1, COLNAME_DATABASE); 1361 } 1362 #endif /* SQLITE_OMIT_UTF16 */ 1363 1364 /* 1365 ** Return the name of the table from which a result column derives. 1366 ** NULL is returned if the result column is an expression or constant or 1367 ** anything else which is not an unambiguous reference to a database column. 1368 */ 1369 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1370 return columnName(pStmt, N, 0, COLNAME_TABLE); 1371 } 1372 #ifndef SQLITE_OMIT_UTF16 1373 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1374 return columnName(pStmt, N, 1, COLNAME_TABLE); 1375 } 1376 #endif /* SQLITE_OMIT_UTF16 */ 1377 1378 /* 1379 ** Return the name of the table column from which a result column derives. 1380 ** NULL is returned if the result column is an expression or constant or 1381 ** anything else which is not an unambiguous reference to a database column. 1382 */ 1383 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1384 return columnName(pStmt, N, 0, COLNAME_COLUMN); 1385 } 1386 #ifndef SQLITE_OMIT_UTF16 1387 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1388 return columnName(pStmt, N, 1, COLNAME_COLUMN); 1389 } 1390 #endif /* SQLITE_OMIT_UTF16 */ 1391 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1392 1393 1394 /******************************* sqlite3_bind_ *************************** 1395 ** 1396 ** Routines used to attach values to wildcards in a compiled SQL statement. 1397 */ 1398 /* 1399 ** Unbind the value bound to variable i in virtual machine p. This is the 1400 ** the same as binding a NULL value to the column. If the "i" parameter is 1401 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 1402 ** 1403 ** A successful evaluation of this routine acquires the mutex on p. 1404 ** the mutex is released if any kind of error occurs. 1405 ** 1406 ** The error code stored in database p->db is overwritten with the return 1407 ** value in any case. 1408 */ 1409 static int vdbeUnbind(Vdbe *p, int i){ 1410 Mem *pVar; 1411 if( vdbeSafetyNotNull(p) ){ 1412 return SQLITE_MISUSE_BKPT; 1413 } 1414 sqlite3_mutex_enter(p->db->mutex); 1415 if( p->iVdbeMagic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 1416 sqlite3Error(p->db, SQLITE_MISUSE); 1417 sqlite3_mutex_leave(p->db->mutex); 1418 sqlite3_log(SQLITE_MISUSE, 1419 "bind on a busy prepared statement: [%s]", p->zSql); 1420 return SQLITE_MISUSE_BKPT; 1421 } 1422 if( i<1 || i>p->nVar ){ 1423 sqlite3Error(p->db, SQLITE_RANGE); 1424 sqlite3_mutex_leave(p->db->mutex); 1425 return SQLITE_RANGE; 1426 } 1427 i--; 1428 pVar = &p->aVar[i]; 1429 sqlite3VdbeMemRelease(pVar); 1430 pVar->flags = MEM_Null; 1431 p->db->errCode = SQLITE_OK; 1432 1433 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 1434 ** binding a new value to this variable invalidates the current query plan. 1435 ** 1436 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host 1437 ** parameter in the WHERE clause might influence the choice of query plan 1438 ** for a statement, then the statement will be automatically recompiled, 1439 ** as if there had been a schema change, on the first sqlite3_step() call 1440 ** following any change to the bindings of that parameter. 1441 */ 1442 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 1443 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ 1444 p->expired = 1; 1445 } 1446 return SQLITE_OK; 1447 } 1448 1449 /* 1450 ** Bind a text or BLOB value. 1451 */ 1452 static int bindText( 1453 sqlite3_stmt *pStmt, /* The statement to bind against */ 1454 int i, /* Index of the parameter to bind */ 1455 const void *zData, /* Pointer to the data to be bound */ 1456 i64 nData, /* Number of bytes of data to be bound */ 1457 void (*xDel)(void*), /* Destructor for the data */ 1458 u8 encoding /* Encoding for the data */ 1459 ){ 1460 Vdbe *p = (Vdbe *)pStmt; 1461 Mem *pVar; 1462 int rc; 1463 1464 rc = vdbeUnbind(p, i); 1465 if( rc==SQLITE_OK ){ 1466 if( zData!=0 ){ 1467 pVar = &p->aVar[i-1]; 1468 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1469 if( rc==SQLITE_OK && encoding!=0 ){ 1470 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1471 } 1472 if( rc ){ 1473 sqlite3Error(p->db, rc); 1474 rc = sqlite3ApiExit(p->db, rc); 1475 } 1476 } 1477 sqlite3_mutex_leave(p->db->mutex); 1478 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 1479 xDel((void*)zData); 1480 } 1481 return rc; 1482 } 1483 1484 1485 /* 1486 ** Bind a blob value to an SQL statement variable. 1487 */ 1488 int sqlite3_bind_blob( 1489 sqlite3_stmt *pStmt, 1490 int i, 1491 const void *zData, 1492 int nData, 1493 void (*xDel)(void*) 1494 ){ 1495 #ifdef SQLITE_ENABLE_API_ARMOR 1496 if( nData<0 ) return SQLITE_MISUSE_BKPT; 1497 #endif 1498 return bindText(pStmt, i, zData, nData, xDel, 0); 1499 } 1500 int sqlite3_bind_blob64( 1501 sqlite3_stmt *pStmt, 1502 int i, 1503 const void *zData, 1504 sqlite3_uint64 nData, 1505 void (*xDel)(void*) 1506 ){ 1507 assert( xDel!=SQLITE_DYNAMIC ); 1508 return bindText(pStmt, i, zData, nData, xDel, 0); 1509 } 1510 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1511 int rc; 1512 Vdbe *p = (Vdbe *)pStmt; 1513 rc = vdbeUnbind(p, i); 1514 if( rc==SQLITE_OK ){ 1515 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1516 sqlite3_mutex_leave(p->db->mutex); 1517 } 1518 return rc; 1519 } 1520 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1521 return sqlite3_bind_int64(p, i, (i64)iValue); 1522 } 1523 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 1524 int rc; 1525 Vdbe *p = (Vdbe *)pStmt; 1526 rc = vdbeUnbind(p, i); 1527 if( rc==SQLITE_OK ){ 1528 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1529 sqlite3_mutex_leave(p->db->mutex); 1530 } 1531 return rc; 1532 } 1533 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1534 int rc; 1535 Vdbe *p = (Vdbe*)pStmt; 1536 rc = vdbeUnbind(p, i); 1537 if( rc==SQLITE_OK ){ 1538 sqlite3_mutex_leave(p->db->mutex); 1539 } 1540 return rc; 1541 } 1542 int sqlite3_bind_pointer( 1543 sqlite3_stmt *pStmt, 1544 int i, 1545 void *pPtr, 1546 const char *zPTtype, 1547 void (*xDestructor)(void*) 1548 ){ 1549 int rc; 1550 Vdbe *p = (Vdbe*)pStmt; 1551 rc = vdbeUnbind(p, i); 1552 if( rc==SQLITE_OK ){ 1553 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); 1554 sqlite3_mutex_leave(p->db->mutex); 1555 }else if( xDestructor ){ 1556 xDestructor(pPtr); 1557 } 1558 return rc; 1559 } 1560 int sqlite3_bind_text( 1561 sqlite3_stmt *pStmt, 1562 int i, 1563 const char *zData, 1564 int nData, 1565 void (*xDel)(void*) 1566 ){ 1567 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1568 } 1569 int sqlite3_bind_text64( 1570 sqlite3_stmt *pStmt, 1571 int i, 1572 const char *zData, 1573 sqlite3_uint64 nData, 1574 void (*xDel)(void*), 1575 unsigned char enc 1576 ){ 1577 assert( xDel!=SQLITE_DYNAMIC ); 1578 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 1579 return bindText(pStmt, i, zData, nData, xDel, enc); 1580 } 1581 #ifndef SQLITE_OMIT_UTF16 1582 int sqlite3_bind_text16( 1583 sqlite3_stmt *pStmt, 1584 int i, 1585 const void *zData, 1586 int nData, 1587 void (*xDel)(void*) 1588 ){ 1589 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1590 } 1591 #endif /* SQLITE_OMIT_UTF16 */ 1592 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1593 int rc; 1594 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 1595 case SQLITE_INTEGER: { 1596 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1597 break; 1598 } 1599 case SQLITE_FLOAT: { 1600 assert( pValue->flags & (MEM_Real|MEM_IntReal) ); 1601 rc = sqlite3_bind_double(pStmt, i, 1602 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i 1603 ); 1604 break; 1605 } 1606 case SQLITE_BLOB: { 1607 if( pValue->flags & MEM_Zero ){ 1608 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1609 }else{ 1610 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1611 } 1612 break; 1613 } 1614 case SQLITE_TEXT: { 1615 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1616 pValue->enc); 1617 break; 1618 } 1619 default: { 1620 rc = sqlite3_bind_null(pStmt, i); 1621 break; 1622 } 1623 } 1624 return rc; 1625 } 1626 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1627 int rc; 1628 Vdbe *p = (Vdbe *)pStmt; 1629 rc = vdbeUnbind(p, i); 1630 if( rc==SQLITE_OK ){ 1631 #ifndef SQLITE_OMIT_INCRBLOB 1632 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1633 #else 1634 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1635 #endif 1636 sqlite3_mutex_leave(p->db->mutex); 1637 } 1638 return rc; 1639 } 1640 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 1641 int rc; 1642 Vdbe *p = (Vdbe *)pStmt; 1643 sqlite3_mutex_enter(p->db->mutex); 1644 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1645 rc = SQLITE_TOOBIG; 1646 }else{ 1647 assert( (n & 0x7FFFFFFF)==n ); 1648 rc = sqlite3_bind_zeroblob(pStmt, i, n); 1649 } 1650 rc = sqlite3ApiExit(p->db, rc); 1651 sqlite3_mutex_leave(p->db->mutex); 1652 return rc; 1653 } 1654 1655 /* 1656 ** Return the number of wildcards that can be potentially bound to. 1657 ** This routine is added to support DBD::SQLite. 1658 */ 1659 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1660 Vdbe *p = (Vdbe*)pStmt; 1661 return p ? p->nVar : 0; 1662 } 1663 1664 /* 1665 ** Return the name of a wildcard parameter. Return NULL if the index 1666 ** is out of range or if the wildcard is unnamed. 1667 ** 1668 ** The result is always UTF-8. 1669 */ 1670 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1671 Vdbe *p = (Vdbe*)pStmt; 1672 if( p==0 ) return 0; 1673 return sqlite3VListNumToName(p->pVList, i); 1674 } 1675 1676 /* 1677 ** Given a wildcard parameter name, return the index of the variable 1678 ** with that name. If there is no variable with the given name, 1679 ** return 0. 1680 */ 1681 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1682 if( p==0 || zName==0 ) return 0; 1683 return sqlite3VListNameToNum(p->pVList, zName, nName); 1684 } 1685 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1686 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 1687 } 1688 1689 /* 1690 ** Transfer all bindings from the first statement over to the second. 1691 */ 1692 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1693 Vdbe *pFrom = (Vdbe*)pFromStmt; 1694 Vdbe *pTo = (Vdbe*)pToStmt; 1695 int i; 1696 assert( pTo->db==pFrom->db ); 1697 assert( pTo->nVar==pFrom->nVar ); 1698 sqlite3_mutex_enter(pTo->db->mutex); 1699 for(i=0; i<pFrom->nVar; i++){ 1700 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1701 } 1702 sqlite3_mutex_leave(pTo->db->mutex); 1703 return SQLITE_OK; 1704 } 1705 1706 #ifndef SQLITE_OMIT_DEPRECATED 1707 /* 1708 ** Deprecated external interface. Internal/core SQLite code 1709 ** should call sqlite3TransferBindings. 1710 ** 1711 ** It is misuse to call this routine with statements from different 1712 ** database connections. But as this is a deprecated interface, we 1713 ** will not bother to check for that condition. 1714 ** 1715 ** If the two statements contain a different number of bindings, then 1716 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1717 ** SQLITE_OK is returned. 1718 */ 1719 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1720 Vdbe *pFrom = (Vdbe*)pFromStmt; 1721 Vdbe *pTo = (Vdbe*)pToStmt; 1722 if( pFrom->nVar!=pTo->nVar ){ 1723 return SQLITE_ERROR; 1724 } 1725 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); 1726 if( pTo->expmask ){ 1727 pTo->expired = 1; 1728 } 1729 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); 1730 if( pFrom->expmask ){ 1731 pFrom->expired = 1; 1732 } 1733 return sqlite3TransferBindings(pFromStmt, pToStmt); 1734 } 1735 #endif 1736 1737 /* 1738 ** Return the sqlite3* database handle to which the prepared statement given 1739 ** in the argument belongs. This is the same database handle that was 1740 ** the first argument to the sqlite3_prepare() that was used to create 1741 ** the statement in the first place. 1742 */ 1743 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 1744 return pStmt ? ((Vdbe*)pStmt)->db : 0; 1745 } 1746 1747 /* 1748 ** Return true if the prepared statement is guaranteed to not modify the 1749 ** database. 1750 */ 1751 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1752 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1753 } 1754 1755 /* 1756 ** Return 1 if the statement is an EXPLAIN and return 2 if the 1757 ** statement is an EXPLAIN QUERY PLAN 1758 */ 1759 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ 1760 return pStmt ? ((Vdbe*)pStmt)->explain : 0; 1761 } 1762 1763 /* 1764 ** Return true if the prepared statement is in need of being reset. 1765 */ 1766 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 1767 Vdbe *v = (Vdbe*)pStmt; 1768 return v!=0 && v->iVdbeMagic==VDBE_MAGIC_RUN && v->pc>=0; 1769 } 1770 1771 /* 1772 ** Return a pointer to the next prepared statement after pStmt associated 1773 ** with database connection pDb. If pStmt is NULL, return the first 1774 ** prepared statement for the database connection. Return NULL if there 1775 ** are no more. 1776 */ 1777 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1778 sqlite3_stmt *pNext; 1779 #ifdef SQLITE_ENABLE_API_ARMOR 1780 if( !sqlite3SafetyCheckOk(pDb) ){ 1781 (void)SQLITE_MISUSE_BKPT; 1782 return 0; 1783 } 1784 #endif 1785 sqlite3_mutex_enter(pDb->mutex); 1786 if( pStmt==0 ){ 1787 pNext = (sqlite3_stmt*)pDb->pVdbe; 1788 }else{ 1789 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1790 } 1791 sqlite3_mutex_leave(pDb->mutex); 1792 return pNext; 1793 } 1794 1795 /* 1796 ** Return the value of a status counter for a prepared statement 1797 */ 1798 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1799 Vdbe *pVdbe = (Vdbe*)pStmt; 1800 u32 v; 1801 #ifdef SQLITE_ENABLE_API_ARMOR 1802 if( !pStmt 1803 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) 1804 ){ 1805 (void)SQLITE_MISUSE_BKPT; 1806 return 0; 1807 } 1808 #endif 1809 if( op==SQLITE_STMTSTATUS_MEMUSED ){ 1810 sqlite3 *db = pVdbe->db; 1811 sqlite3_mutex_enter(db->mutex); 1812 v = 0; 1813 db->pnBytesFreed = (int*)&v; 1814 sqlite3VdbeClearObject(db, pVdbe); 1815 sqlite3DbFree(db, pVdbe); 1816 db->pnBytesFreed = 0; 1817 sqlite3_mutex_leave(db->mutex); 1818 }else{ 1819 v = pVdbe->aCounter[op]; 1820 if( resetFlag ) pVdbe->aCounter[op] = 0; 1821 } 1822 return (int)v; 1823 } 1824 1825 /* 1826 ** Return the SQL associated with a prepared statement 1827 */ 1828 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 1829 Vdbe *p = (Vdbe *)pStmt; 1830 return p ? p->zSql : 0; 1831 } 1832 1833 /* 1834 ** Return the SQL associated with a prepared statement with 1835 ** bound parameters expanded. Space to hold the returned string is 1836 ** obtained from sqlite3_malloc(). The caller is responsible for 1837 ** freeing the returned string by passing it to sqlite3_free(). 1838 ** 1839 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 1840 ** expanded bound parameters. 1841 */ 1842 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 1843 #ifdef SQLITE_OMIT_TRACE 1844 return 0; 1845 #else 1846 char *z = 0; 1847 const char *zSql = sqlite3_sql(pStmt); 1848 if( zSql ){ 1849 Vdbe *p = (Vdbe *)pStmt; 1850 sqlite3_mutex_enter(p->db->mutex); 1851 z = sqlite3VdbeExpandSql(p, zSql); 1852 sqlite3_mutex_leave(p->db->mutex); 1853 } 1854 return z; 1855 #endif 1856 } 1857 1858 #ifdef SQLITE_ENABLE_NORMALIZE 1859 /* 1860 ** Return the normalized SQL associated with a prepared statement. 1861 */ 1862 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ 1863 Vdbe *p = (Vdbe *)pStmt; 1864 if( p==0 ) return 0; 1865 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ 1866 sqlite3_mutex_enter(p->db->mutex); 1867 p->zNormSql = sqlite3Normalize(p, p->zSql); 1868 sqlite3_mutex_leave(p->db->mutex); 1869 } 1870 return p->zNormSql; 1871 } 1872 #endif /* SQLITE_ENABLE_NORMALIZE */ 1873 1874 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1875 /* 1876 ** Allocate and populate an UnpackedRecord structure based on the serialized 1877 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 1878 ** if successful, or a NULL pointer if an OOM error is encountered. 1879 */ 1880 static UnpackedRecord *vdbeUnpackRecord( 1881 KeyInfo *pKeyInfo, 1882 int nKey, 1883 const void *pKey 1884 ){ 1885 UnpackedRecord *pRet; /* Return value */ 1886 1887 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 1888 if( pRet ){ 1889 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); 1890 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 1891 } 1892 return pRet; 1893 } 1894 1895 /* 1896 ** This function is called from within a pre-update callback to retrieve 1897 ** a field of the row currently being updated or deleted. 1898 */ 1899 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 1900 PreUpdate *p = db->pPreUpdate; 1901 Mem *pMem; 1902 int rc = SQLITE_OK; 1903 1904 /* Test that this call is being made from within an SQLITE_DELETE or 1905 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 1906 if( !p || p->op==SQLITE_INSERT ){ 1907 rc = SQLITE_MISUSE_BKPT; 1908 goto preupdate_old_out; 1909 } 1910 if( p->pPk ){ 1911 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 1912 } 1913 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 1914 rc = SQLITE_RANGE; 1915 goto preupdate_old_out; 1916 } 1917 1918 /* If the old.* record has not yet been loaded into memory, do so now. */ 1919 if( p->pUnpacked==0 ){ 1920 u32 nRec; 1921 u8 *aRec; 1922 1923 assert( p->pCsr->eCurType==CURTYPE_BTREE ); 1924 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 1925 aRec = sqlite3DbMallocRaw(db, nRec); 1926 if( !aRec ) goto preupdate_old_out; 1927 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 1928 if( rc==SQLITE_OK ){ 1929 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 1930 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 1931 } 1932 if( rc!=SQLITE_OK ){ 1933 sqlite3DbFree(db, aRec); 1934 goto preupdate_old_out; 1935 } 1936 p->aRecord = aRec; 1937 } 1938 1939 pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; 1940 if( iIdx==p->pTab->iPKey ){ 1941 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 1942 }else if( iIdx>=p->pUnpacked->nField ){ 1943 *ppValue = (sqlite3_value *)columnNullValue(); 1944 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 1945 if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 1946 testcase( pMem->flags & MEM_Int ); 1947 testcase( pMem->flags & MEM_IntReal ); 1948 sqlite3VdbeMemRealify(pMem); 1949 } 1950 } 1951 1952 preupdate_old_out: 1953 sqlite3Error(db, rc); 1954 return sqlite3ApiExit(db, rc); 1955 } 1956 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1957 1958 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1959 /* 1960 ** This function is called from within a pre-update callback to retrieve 1961 ** the number of columns in the row being updated, deleted or inserted. 1962 */ 1963 int sqlite3_preupdate_count(sqlite3 *db){ 1964 PreUpdate *p = db->pPreUpdate; 1965 return (p ? p->keyinfo.nKeyField : 0); 1966 } 1967 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1968 1969 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1970 /* 1971 ** This function is designed to be called from within a pre-update callback 1972 ** only. It returns zero if the change that caused the callback was made 1973 ** immediately by a user SQL statement. Or, if the change was made by a 1974 ** trigger program, it returns the number of trigger programs currently 1975 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 1976 ** top-level trigger etc.). 1977 ** 1978 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 1979 ** or SET DEFAULT action is considered a trigger. 1980 */ 1981 int sqlite3_preupdate_depth(sqlite3 *db){ 1982 PreUpdate *p = db->pPreUpdate; 1983 return (p ? p->v->nFrame : 0); 1984 } 1985 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1986 1987 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1988 /* 1989 ** This function is designed to be called from within a pre-update callback 1990 ** only. 1991 */ 1992 int sqlite3_preupdate_blobwrite(sqlite3 *db){ 1993 PreUpdate *p = db->pPreUpdate; 1994 return (p ? p->iBlobWrite : -1); 1995 } 1996 #endif 1997 1998 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1999 /* 2000 ** This function is called from within a pre-update callback to retrieve 2001 ** a field of the row currently being updated or inserted. 2002 */ 2003 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 2004 PreUpdate *p = db->pPreUpdate; 2005 int rc = SQLITE_OK; 2006 Mem *pMem; 2007 2008 if( !p || p->op==SQLITE_DELETE ){ 2009 rc = SQLITE_MISUSE_BKPT; 2010 goto preupdate_new_out; 2011 } 2012 if( p->pPk && p->op!=SQLITE_UPDATE ){ 2013 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 2014 } 2015 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 2016 rc = SQLITE_RANGE; 2017 goto preupdate_new_out; 2018 } 2019 2020 if( p->op==SQLITE_INSERT ){ 2021 /* For an INSERT, memory cell p->iNewReg contains the serialized record 2022 ** that is being inserted. Deserialize it. */ 2023 UnpackedRecord *pUnpack = p->pNewUnpacked; 2024 if( !pUnpack ){ 2025 Mem *pData = &p->v->aMem[p->iNewReg]; 2026 rc = ExpandBlob(pData); 2027 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2028 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 2029 if( !pUnpack ){ 2030 rc = SQLITE_NOMEM; 2031 goto preupdate_new_out; 2032 } 2033 p->pNewUnpacked = pUnpack; 2034 } 2035 pMem = &pUnpack->aMem[iIdx]; 2036 if( iIdx==p->pTab->iPKey ){ 2037 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2038 }else if( iIdx>=pUnpack->nField ){ 2039 pMem = (sqlite3_value *)columnNullValue(); 2040 } 2041 }else{ 2042 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 2043 ** value. Make a copy of the cell contents and return a pointer to it. 2044 ** It is not safe to return a pointer to the memory cell itself as the 2045 ** caller may modify the value text encoding. 2046 */ 2047 assert( p->op==SQLITE_UPDATE ); 2048 if( !p->aNew ){ 2049 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 2050 if( !p->aNew ){ 2051 rc = SQLITE_NOMEM; 2052 goto preupdate_new_out; 2053 } 2054 } 2055 assert( iIdx>=0 && iIdx<p->pCsr->nField ); 2056 pMem = &p->aNew[iIdx]; 2057 if( pMem->flags==0 ){ 2058 if( iIdx==p->pTab->iPKey ){ 2059 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2060 }else{ 2061 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 2062 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2063 } 2064 } 2065 } 2066 *ppValue = pMem; 2067 2068 preupdate_new_out: 2069 sqlite3Error(db, rc); 2070 return sqlite3ApiExit(db, rc); 2071 } 2072 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2073 2074 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 2075 /* 2076 ** Return status data for a single loop within query pStmt. 2077 */ 2078 int sqlite3_stmt_scanstatus( 2079 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 2080 int idx, /* Index of loop to report on */ 2081 int iScanStatusOp, /* Which metric to return */ 2082 void *pOut /* OUT: Write the answer here */ 2083 ){ 2084 Vdbe *p = (Vdbe*)pStmt; 2085 ScanStatus *pScan; 2086 if( idx<0 || idx>=p->nScan ) return 1; 2087 pScan = &p->aScan[idx]; 2088 switch( iScanStatusOp ){ 2089 case SQLITE_SCANSTAT_NLOOP: { 2090 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; 2091 break; 2092 } 2093 case SQLITE_SCANSTAT_NVISIT: { 2094 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; 2095 break; 2096 } 2097 case SQLITE_SCANSTAT_EST: { 2098 double r = 1.0; 2099 LogEst x = pScan->nEst; 2100 while( x<100 ){ 2101 x += 10; 2102 r *= 0.5; 2103 } 2104 *(double*)pOut = r*sqlite3LogEstToInt(x); 2105 break; 2106 } 2107 case SQLITE_SCANSTAT_NAME: { 2108 *(const char**)pOut = pScan->zName; 2109 break; 2110 } 2111 case SQLITE_SCANSTAT_EXPLAIN: { 2112 if( pScan->addrExplain ){ 2113 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; 2114 }else{ 2115 *(const char**)pOut = 0; 2116 } 2117 break; 2118 } 2119 case SQLITE_SCANSTAT_SELECTID: { 2120 if( pScan->addrExplain ){ 2121 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; 2122 }else{ 2123 *(int*)pOut = -1; 2124 } 2125 break; 2126 } 2127 default: { 2128 return 1; 2129 } 2130 } 2131 return 0; 2132 } 2133 2134 /* 2135 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 2136 */ 2137 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 2138 Vdbe *p = (Vdbe*)pStmt; 2139 memset(p->anExec, 0, p->nOp * sizeof(i64)); 2140 } 2141 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ 2142