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 assert( p->startTime>0 ); 64 assert( db->xProfile!=0 ); 65 assert( db->init.busy==0 ); 66 assert( p->zSql!=0 ); 67 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 68 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); 69 p->startTime = 0; 70 } 71 /* 72 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 73 ** is needed, and it invokes the callback if it is needed. 74 */ 75 # define checkProfileCallback(DB,P) \ 76 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 77 #else 78 # define checkProfileCallback(DB,P) /*no-op*/ 79 #endif 80 81 /* 82 ** The following routine destroys a virtual machine that is created by 83 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 84 ** success/failure code that describes the result of executing the virtual 85 ** machine. 86 ** 87 ** This routine sets the error code and string returned by 88 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 89 */ 90 int sqlite3_finalize(sqlite3_stmt *pStmt){ 91 int rc; 92 if( pStmt==0 ){ 93 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 94 ** pointer is a harmless no-op. */ 95 rc = SQLITE_OK; 96 }else{ 97 Vdbe *v = (Vdbe*)pStmt; 98 sqlite3 *db = v->db; 99 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 100 sqlite3_mutex_enter(db->mutex); 101 checkProfileCallback(db, v); 102 rc = sqlite3VdbeFinalize(v); 103 rc = sqlite3ApiExit(db, rc); 104 sqlite3LeaveMutexAndCloseZombie(db); 105 } 106 return rc; 107 } 108 109 /* 110 ** Terminate the current execution of an SQL statement and reset it 111 ** back to its starting state so that it can be reused. A success code from 112 ** the prior execution is returned. 113 ** 114 ** This routine sets the error code and string returned by 115 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 116 */ 117 int sqlite3_reset(sqlite3_stmt *pStmt){ 118 int rc; 119 if( pStmt==0 ){ 120 rc = SQLITE_OK; 121 }else{ 122 Vdbe *v = (Vdbe*)pStmt; 123 sqlite3 *db = v->db; 124 sqlite3_mutex_enter(db->mutex); 125 checkProfileCallback(db, v); 126 rc = sqlite3VdbeReset(v); 127 sqlite3VdbeRewind(v); 128 assert( (rc & (db->errMask))==rc ); 129 rc = sqlite3ApiExit(db, rc); 130 sqlite3_mutex_leave(db->mutex); 131 } 132 return rc; 133 } 134 135 /* 136 ** Set all the parameters in the compiled SQL statement to NULL. 137 */ 138 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 139 int i; 140 int rc = SQLITE_OK; 141 Vdbe *p = (Vdbe*)pStmt; 142 #if SQLITE_THREADSAFE 143 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 144 #endif 145 sqlite3_mutex_enter(mutex); 146 for(i=0; i<p->nVar; i++){ 147 sqlite3VdbeMemRelease(&p->aVar[i]); 148 p->aVar[i].flags = MEM_Null; 149 } 150 if( p->isPrepareV2 && p->expmask ){ 151 p->expired = 1; 152 } 153 sqlite3_mutex_leave(mutex); 154 return rc; 155 } 156 157 158 /**************************** sqlite3_value_ ******************************* 159 ** The following routines extract information from a Mem or sqlite3_value 160 ** structure. 161 */ 162 const void *sqlite3_value_blob(sqlite3_value *pVal){ 163 Mem *p = (Mem*)pVal; 164 if( p->flags & (MEM_Blob|MEM_Str) ){ 165 sqlite3VdbeMemExpandBlob(p); 166 p->flags |= MEM_Blob; 167 return p->n ? p->z : 0; 168 }else{ 169 return sqlite3_value_text(pVal); 170 } 171 } 172 int sqlite3_value_bytes(sqlite3_value *pVal){ 173 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 174 } 175 int sqlite3_value_bytes16(sqlite3_value *pVal){ 176 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 177 } 178 double sqlite3_value_double(sqlite3_value *pVal){ 179 return sqlite3VdbeRealValue((Mem*)pVal); 180 } 181 int sqlite3_value_int(sqlite3_value *pVal){ 182 return (int)sqlite3VdbeIntValue((Mem*)pVal); 183 } 184 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 185 return sqlite3VdbeIntValue((Mem*)pVal); 186 } 187 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 188 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 189 } 190 #ifndef SQLITE_OMIT_UTF16 191 const void *sqlite3_value_text16(sqlite3_value* pVal){ 192 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 193 } 194 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 195 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 196 } 197 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 198 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 199 } 200 #endif /* SQLITE_OMIT_UTF16 */ 201 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 202 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 203 ** point number string BLOB NULL 204 */ 205 int sqlite3_value_type(sqlite3_value* pVal){ 206 static const u8 aType[] = { 207 SQLITE_BLOB, /* 0x00 */ 208 SQLITE_NULL, /* 0x01 */ 209 SQLITE_TEXT, /* 0x02 */ 210 SQLITE_NULL, /* 0x03 */ 211 SQLITE_INTEGER, /* 0x04 */ 212 SQLITE_NULL, /* 0x05 */ 213 SQLITE_INTEGER, /* 0x06 */ 214 SQLITE_NULL, /* 0x07 */ 215 SQLITE_FLOAT, /* 0x08 */ 216 SQLITE_NULL, /* 0x09 */ 217 SQLITE_FLOAT, /* 0x0a */ 218 SQLITE_NULL, /* 0x0b */ 219 SQLITE_INTEGER, /* 0x0c */ 220 SQLITE_NULL, /* 0x0d */ 221 SQLITE_INTEGER, /* 0x0e */ 222 SQLITE_NULL, /* 0x0f */ 223 SQLITE_BLOB, /* 0x10 */ 224 SQLITE_NULL, /* 0x11 */ 225 SQLITE_TEXT, /* 0x12 */ 226 SQLITE_NULL, /* 0x13 */ 227 SQLITE_INTEGER, /* 0x14 */ 228 SQLITE_NULL, /* 0x15 */ 229 SQLITE_INTEGER, /* 0x16 */ 230 SQLITE_NULL, /* 0x17 */ 231 SQLITE_FLOAT, /* 0x18 */ 232 SQLITE_NULL, /* 0x19 */ 233 SQLITE_FLOAT, /* 0x1a */ 234 SQLITE_NULL, /* 0x1b */ 235 SQLITE_INTEGER, /* 0x1c */ 236 SQLITE_NULL, /* 0x1d */ 237 SQLITE_INTEGER, /* 0x1e */ 238 SQLITE_NULL, /* 0x1f */ 239 }; 240 return aType[pVal->flags&MEM_AffMask]; 241 } 242 243 /* Make a copy of an sqlite3_value object 244 */ 245 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 246 sqlite3_value *pNew; 247 if( pOrig==0 ) return 0; 248 pNew = sqlite3_malloc( sizeof(*pNew) ); 249 if( pNew==0 ) return 0; 250 memset(pNew, 0, sizeof(*pNew)); 251 memcpy(pNew, pOrig, MEMCELLSIZE); 252 pNew->flags &= ~MEM_Dyn; 253 pNew->db = 0; 254 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 255 pNew->flags &= ~(MEM_Static|MEM_Dyn); 256 pNew->flags |= MEM_Ephem; 257 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 258 sqlite3ValueFree(pNew); 259 pNew = 0; 260 } 261 } 262 return pNew; 263 } 264 265 /* Destroy an sqlite3_value object previously obtained from 266 ** sqlite3_value_dup(). 267 */ 268 void sqlite3_value_free(sqlite3_value *pOld){ 269 sqlite3ValueFree(pOld); 270 } 271 272 273 /**************************** sqlite3_result_ ******************************* 274 ** The following routines are used by user-defined functions to specify 275 ** the function result. 276 ** 277 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 278 ** result as a string or blob but if the string or blob is too large, it 279 ** then sets the error code to SQLITE_TOOBIG 280 ** 281 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 282 ** on value P is not going to be used and need to be destroyed. 283 */ 284 static void setResultStrOrError( 285 sqlite3_context *pCtx, /* Function context */ 286 const char *z, /* String pointer */ 287 int n, /* Bytes in string, or negative */ 288 u8 enc, /* Encoding of z. 0 for BLOBs */ 289 void (*xDel)(void*) /* Destructor function */ 290 ){ 291 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ 292 sqlite3_result_error_toobig(pCtx); 293 } 294 } 295 static int invokeValueDestructor( 296 const void *p, /* Value to destroy */ 297 void (*xDel)(void*), /* The destructor */ 298 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ 299 ){ 300 assert( xDel!=SQLITE_DYNAMIC ); 301 if( xDel==0 ){ 302 /* noop */ 303 }else if( xDel==SQLITE_TRANSIENT ){ 304 /* noop */ 305 }else{ 306 xDel((void*)p); 307 } 308 if( pCtx ) sqlite3_result_error_toobig(pCtx); 309 return SQLITE_TOOBIG; 310 } 311 void sqlite3_result_blob( 312 sqlite3_context *pCtx, 313 const void *z, 314 int n, 315 void (*xDel)(void *) 316 ){ 317 assert( n>=0 ); 318 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 319 setResultStrOrError(pCtx, z, n, 0, xDel); 320 } 321 void sqlite3_result_blob64( 322 sqlite3_context *pCtx, 323 const void *z, 324 sqlite3_uint64 n, 325 void (*xDel)(void *) 326 ){ 327 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 328 assert( xDel!=SQLITE_DYNAMIC ); 329 if( n>0x7fffffff ){ 330 (void)invokeValueDestructor(z, xDel, pCtx); 331 }else{ 332 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 333 } 334 } 335 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 336 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 337 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 338 } 339 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 340 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 341 pCtx->isError = SQLITE_ERROR; 342 pCtx->fErrorOrAux = 1; 343 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 344 } 345 #ifndef SQLITE_OMIT_UTF16 346 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 347 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 348 pCtx->isError = SQLITE_ERROR; 349 pCtx->fErrorOrAux = 1; 350 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 351 } 352 #endif 353 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 354 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 355 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 356 } 357 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 358 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 359 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 360 } 361 void sqlite3_result_null(sqlite3_context *pCtx){ 362 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 363 sqlite3VdbeMemSetNull(pCtx->pOut); 364 } 365 void sqlite3_result_text( 366 sqlite3_context *pCtx, 367 const char *z, 368 int n, 369 void (*xDel)(void *) 370 ){ 371 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 372 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 373 } 374 void sqlite3_result_text64( 375 sqlite3_context *pCtx, 376 const char *z, 377 sqlite3_uint64 n, 378 void (*xDel)(void *), 379 unsigned char enc 380 ){ 381 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 382 assert( xDel!=SQLITE_DYNAMIC ); 383 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 384 if( n>0x7fffffff ){ 385 (void)invokeValueDestructor(z, xDel, pCtx); 386 }else{ 387 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 388 } 389 } 390 #ifndef SQLITE_OMIT_UTF16 391 void sqlite3_result_text16( 392 sqlite3_context *pCtx, 393 const void *z, 394 int n, 395 void (*xDel)(void *) 396 ){ 397 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 398 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 399 } 400 void sqlite3_result_text16be( 401 sqlite3_context *pCtx, 402 const void *z, 403 int n, 404 void (*xDel)(void *) 405 ){ 406 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 407 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 408 } 409 void sqlite3_result_text16le( 410 sqlite3_context *pCtx, 411 const void *z, 412 int n, 413 void (*xDel)(void *) 414 ){ 415 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 416 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 417 } 418 #endif /* SQLITE_OMIT_UTF16 */ 419 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 420 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 421 sqlite3VdbeMemCopy(pCtx->pOut, pValue); 422 } 423 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 424 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 425 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); 426 } 427 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 428 pCtx->isError = errCode; 429 pCtx->fErrorOrAux = 1; 430 #ifdef SQLITE_DEBUG 431 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 432 #endif 433 if( pCtx->pOut->flags & MEM_Null ){ 434 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 435 SQLITE_UTF8, SQLITE_STATIC); 436 } 437 } 438 439 /* Force an SQLITE_TOOBIG error. */ 440 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 441 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 442 pCtx->isError = SQLITE_TOOBIG; 443 pCtx->fErrorOrAux = 1; 444 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 445 SQLITE_UTF8, SQLITE_STATIC); 446 } 447 448 /* An SQLITE_NOMEM error. */ 449 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 450 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 451 sqlite3VdbeMemSetNull(pCtx->pOut); 452 pCtx->isError = SQLITE_NOMEM; 453 pCtx->fErrorOrAux = 1; 454 pCtx->pOut->db->mallocFailed = 1; 455 } 456 457 /* 458 ** This function is called after a transaction has been committed. It 459 ** invokes callbacks registered with sqlite3_wal_hook() as required. 460 */ 461 static int doWalCallbacks(sqlite3 *db){ 462 int rc = SQLITE_OK; 463 #ifndef SQLITE_OMIT_WAL 464 int i; 465 for(i=0; i<db->nDb; i++){ 466 Btree *pBt = db->aDb[i].pBt; 467 if( pBt ){ 468 int nEntry; 469 sqlite3BtreeEnter(pBt); 470 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 471 sqlite3BtreeLeave(pBt); 472 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 473 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); 474 } 475 } 476 } 477 #endif 478 return rc; 479 } 480 481 482 /* 483 ** Execute the statement pStmt, either until a row of data is ready, the 484 ** statement is completely executed or an error occurs. 485 ** 486 ** This routine implements the bulk of the logic behind the sqlite_step() 487 ** API. The only thing omitted is the automatic recompile if a 488 ** schema change has occurred. That detail is handled by the 489 ** outer sqlite3_step() wrapper procedure. 490 */ 491 static int sqlite3Step(Vdbe *p){ 492 sqlite3 *db; 493 int rc; 494 495 assert(p); 496 if( p->magic!=VDBE_MAGIC_RUN ){ 497 /* We used to require that sqlite3_reset() be called before retrying 498 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 499 ** with version 3.7.0, we changed this so that sqlite3_reset() would 500 ** be called automatically instead of throwing the SQLITE_MISUSE error. 501 ** This "automatic-reset" change is not technically an incompatibility, 502 ** since any application that receives an SQLITE_MISUSE is broken by 503 ** definition. 504 ** 505 ** Nevertheless, some published applications that were originally written 506 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 507 ** returns, and those were broken by the automatic-reset change. As a 508 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 509 ** legacy behavior of returning SQLITE_MISUSE for cases where the 510 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 511 ** or SQLITE_BUSY error. 512 */ 513 #ifdef SQLITE_OMIT_AUTORESET 514 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 515 sqlite3_reset((sqlite3_stmt*)p); 516 }else{ 517 return SQLITE_MISUSE_BKPT; 518 } 519 #else 520 sqlite3_reset((sqlite3_stmt*)p); 521 #endif 522 } 523 524 /* Check that malloc() has not failed. If it has, return early. */ 525 db = p->db; 526 if( db->mallocFailed ){ 527 p->rc = SQLITE_NOMEM; 528 return SQLITE_NOMEM; 529 } 530 531 if( p->pc<=0 && p->expired ){ 532 p->rc = SQLITE_SCHEMA; 533 rc = SQLITE_ERROR; 534 goto end_of_step; 535 } 536 if( p->pc<0 ){ 537 /* If there are no other statements currently running, then 538 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 539 ** from interrupting a statement that has not yet started. 540 */ 541 if( db->nVdbeActive==0 ){ 542 db->u1.isInterrupted = 0; 543 } 544 545 assert( db->nVdbeWrite>0 || db->autoCommit==0 546 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 547 ); 548 549 #ifndef SQLITE_OMIT_TRACE 550 if( db->xProfile && !db->init.busy && p->zSql ){ 551 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 552 }else{ 553 assert( p->startTime==0 ); 554 } 555 #endif 556 557 db->nVdbeActive++; 558 if( p->readOnly==0 ) db->nVdbeWrite++; 559 if( p->bIsReader ) db->nVdbeRead++; 560 p->pc = 0; 561 } 562 #ifdef SQLITE_DEBUG 563 p->rcApp = SQLITE_OK; 564 #endif 565 #ifndef SQLITE_OMIT_EXPLAIN 566 if( p->explain ){ 567 rc = sqlite3VdbeList(p); 568 }else 569 #endif /* SQLITE_OMIT_EXPLAIN */ 570 { 571 db->nVdbeExec++; 572 rc = sqlite3VdbeExec(p); 573 db->nVdbeExec--; 574 } 575 576 #ifndef SQLITE_OMIT_TRACE 577 /* If the statement completed successfully, invoke the profile callback */ 578 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); 579 #endif 580 581 if( rc==SQLITE_DONE ){ 582 assert( p->rc==SQLITE_OK ); 583 p->rc = doWalCallbacks(db); 584 if( p->rc!=SQLITE_OK ){ 585 rc = SQLITE_ERROR; 586 } 587 } 588 589 db->errCode = rc; 590 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 591 p->rc = SQLITE_NOMEM; 592 } 593 end_of_step: 594 /* At this point local variable rc holds the value that should be 595 ** returned if this statement was compiled using the legacy 596 ** sqlite3_prepare() interface. According to the docs, this can only 597 ** be one of the values in the first assert() below. Variable p->rc 598 ** contains the value that would be returned if sqlite3_finalize() 599 ** were called on statement p. 600 */ 601 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 602 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 603 ); 604 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); 605 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 606 /* If this statement was prepared using sqlite3_prepare_v2(), and an 607 ** error has occurred, then return the error code in p->rc to the 608 ** caller. Set the error code in the database handle to the same value. 609 */ 610 rc = sqlite3VdbeTransferError(p); 611 } 612 return (rc&db->errMask); 613 } 614 615 /* 616 ** This is the top-level implementation of sqlite3_step(). Call 617 ** sqlite3Step() to do most of the work. If a schema error occurs, 618 ** call sqlite3Reprepare() and try again. 619 */ 620 int sqlite3_step(sqlite3_stmt *pStmt){ 621 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 622 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ 623 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 624 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 625 sqlite3 *db; /* The database connection */ 626 627 if( vdbeSafetyNotNull(v) ){ 628 return SQLITE_MISUSE_BKPT; 629 } 630 db = v->db; 631 sqlite3_mutex_enter(db->mutex); 632 v->doingRerun = 0; 633 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 634 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 635 int savedPc = v->pc; 636 rc2 = rc = sqlite3Reprepare(v); 637 if( rc!=SQLITE_OK) break; 638 sqlite3_reset(pStmt); 639 if( savedPc>=0 ) v->doingRerun = 1; 640 assert( v->expired==0 ); 641 } 642 if( rc2!=SQLITE_OK ){ 643 /* This case occurs after failing to recompile an sql statement. 644 ** The error message from the SQL compiler has already been loaded 645 ** into the database handle. This block copies the error message 646 ** from the database handle into the statement and sets the statement 647 ** program counter to 0 to ensure that when the statement is 648 ** finalized or reset the parser error message is available via 649 ** sqlite3_errmsg() and sqlite3_errcode(). 650 */ 651 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 652 sqlite3DbFree(db, v->zErrMsg); 653 if( !db->mallocFailed ){ 654 v->zErrMsg = sqlite3DbStrDup(db, zErr); 655 v->rc = rc2; 656 } else { 657 v->zErrMsg = 0; 658 v->rc = rc = SQLITE_NOMEM; 659 } 660 } 661 rc = sqlite3ApiExit(db, rc); 662 sqlite3_mutex_leave(db->mutex); 663 return rc; 664 } 665 666 667 /* 668 ** Extract the user data from a sqlite3_context structure and return a 669 ** pointer to it. 670 */ 671 void *sqlite3_user_data(sqlite3_context *p){ 672 assert( p && p->pFunc ); 673 return p->pFunc->pUserData; 674 } 675 676 /* 677 ** Extract the user data from a sqlite3_context structure and return a 678 ** pointer to it. 679 ** 680 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 681 ** returns a copy of the pointer to the database connection (the 1st 682 ** parameter) of the sqlite3_create_function() and 683 ** sqlite3_create_function16() routines that originally registered the 684 ** application defined function. 685 */ 686 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 687 assert( p && p->pFunc ); 688 return p->pOut->db; 689 } 690 691 /* 692 ** Return the current time for a statement. If the current time 693 ** is requested more than once within the same run of a single prepared 694 ** statement, the exact same time is returned for each invocation regardless 695 ** of the amount of time that elapses between invocations. In other words, 696 ** the time returned is always the time of the first call. 697 */ 698 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 699 int rc; 700 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 701 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 702 assert( p->pVdbe!=0 ); 703 #else 704 sqlite3_int64 iTime = 0; 705 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 706 #endif 707 if( *piTime==0 ){ 708 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 709 if( rc ) *piTime = 0; 710 } 711 return *piTime; 712 } 713 714 /* 715 ** The following is the implementation of an SQL function that always 716 ** fails with an error message stating that the function is used in the 717 ** wrong context. The sqlite3_overload_function() API might construct 718 ** SQL function that use this routine so that the functions will exist 719 ** for name resolution but are actually overloaded by the xFindFunction 720 ** method of virtual tables. 721 */ 722 void sqlite3InvalidFunction( 723 sqlite3_context *context, /* The function calling context */ 724 int NotUsed, /* Number of arguments to the function */ 725 sqlite3_value **NotUsed2 /* Value of each argument */ 726 ){ 727 const char *zName = context->pFunc->zName; 728 char *zErr; 729 UNUSED_PARAMETER2(NotUsed, NotUsed2); 730 zErr = sqlite3_mprintf( 731 "unable to use function %s in the requested context", zName); 732 sqlite3_result_error(context, zErr, -1); 733 sqlite3_free(zErr); 734 } 735 736 /* 737 ** Create a new aggregate context for p and return a pointer to 738 ** its pMem->z element. 739 */ 740 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 741 Mem *pMem = p->pMem; 742 assert( (pMem->flags & MEM_Agg)==0 ); 743 if( nByte<=0 ){ 744 sqlite3VdbeMemSetNull(pMem); 745 pMem->z = 0; 746 }else{ 747 sqlite3VdbeMemClearAndResize(pMem, nByte); 748 pMem->flags = MEM_Agg; 749 pMem->u.pDef = p->pFunc; 750 if( pMem->z ){ 751 memset(pMem->z, 0, nByte); 752 } 753 } 754 return (void*)pMem->z; 755 } 756 757 /* 758 ** Allocate or return the aggregate context for a user function. A new 759 ** context is allocated on the first call. Subsequent calls return the 760 ** same context that was returned on prior calls. 761 */ 762 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 763 assert( p && p->pFunc && p->pFunc->xStep ); 764 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 765 testcase( nByte<0 ); 766 if( (p->pMem->flags & MEM_Agg)==0 ){ 767 return createAggContext(p, nByte); 768 }else{ 769 return (void*)p->pMem->z; 770 } 771 } 772 773 /* 774 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 775 ** the user-function defined by pCtx. 776 */ 777 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 778 AuxData *pAuxData; 779 780 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 781 #if SQLITE_ENABLE_STAT3_OR_STAT4 782 if( pCtx->pVdbe==0 ) return 0; 783 #else 784 assert( pCtx->pVdbe!=0 ); 785 #endif 786 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 787 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 788 } 789 790 return (pAuxData ? pAuxData->pAux : 0); 791 } 792 793 /* 794 ** Set the auxiliary data pointer and delete function, for the iArg'th 795 ** argument to the user-function defined by pCtx. Any previous value is 796 ** deleted by calling the delete function specified when it was set. 797 */ 798 void sqlite3_set_auxdata( 799 sqlite3_context *pCtx, 800 int iArg, 801 void *pAux, 802 void (*xDelete)(void*) 803 ){ 804 AuxData *pAuxData; 805 Vdbe *pVdbe = pCtx->pVdbe; 806 807 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 808 if( iArg<0 ) goto failed; 809 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 810 if( pVdbe==0 ) goto failed; 811 #else 812 assert( pVdbe!=0 ); 813 #endif 814 815 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 816 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 817 } 818 if( pAuxData==0 ){ 819 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 820 if( !pAuxData ) goto failed; 821 pAuxData->iOp = pCtx->iOp; 822 pAuxData->iArg = iArg; 823 pAuxData->pNext = pVdbe->pAuxData; 824 pVdbe->pAuxData = pAuxData; 825 if( pCtx->fErrorOrAux==0 ){ 826 pCtx->isError = 0; 827 pCtx->fErrorOrAux = 1; 828 } 829 }else if( pAuxData->xDelete ){ 830 pAuxData->xDelete(pAuxData->pAux); 831 } 832 833 pAuxData->pAux = pAux; 834 pAuxData->xDelete = xDelete; 835 return; 836 837 failed: 838 if( xDelete ){ 839 xDelete(pAux); 840 } 841 } 842 843 #ifndef SQLITE_OMIT_DEPRECATED 844 /* 845 ** Return the number of times the Step function of an aggregate has been 846 ** called. 847 ** 848 ** This function is deprecated. Do not use it for new code. It is 849 ** provide only to avoid breaking legacy code. New aggregate function 850 ** implementations should keep their own counts within their aggregate 851 ** context. 852 */ 853 int sqlite3_aggregate_count(sqlite3_context *p){ 854 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 855 return p->pMem->n; 856 } 857 #endif 858 859 /* 860 ** Return the number of columns in the result set for the statement pStmt. 861 */ 862 int sqlite3_column_count(sqlite3_stmt *pStmt){ 863 Vdbe *pVm = (Vdbe *)pStmt; 864 return pVm ? pVm->nResColumn : 0; 865 } 866 867 /* 868 ** Return the number of values available from the current row of the 869 ** currently executing statement pStmt. 870 */ 871 int sqlite3_data_count(sqlite3_stmt *pStmt){ 872 Vdbe *pVm = (Vdbe *)pStmt; 873 if( pVm==0 || pVm->pResultSet==0 ) return 0; 874 return pVm->nResColumn; 875 } 876 877 /* 878 ** Return a pointer to static memory containing an SQL NULL value. 879 */ 880 static const Mem *columnNullValue(void){ 881 /* Even though the Mem structure contains an element 882 ** of type i64, on certain architectures (x86) with certain compiler 883 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 884 ** instead of an 8-byte one. This all works fine, except that when 885 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 886 ** that a Mem structure is located on an 8-byte boundary. To prevent 887 ** these assert()s from failing, when building with SQLITE_DEBUG defined 888 ** using gcc, we force nullMem to be 8-byte aligned using the magical 889 ** __attribute__((aligned(8))) macro. */ 890 static const Mem nullMem 891 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 892 __attribute__((aligned(8))) 893 #endif 894 = { 895 /* .u = */ {0}, 896 /* .flags = */ MEM_Null, 897 /* .enc = */ 0, 898 /* .n = */ 0, 899 /* .z = */ 0, 900 /* .zMalloc = */ 0, 901 /* .szMalloc = */ 0, 902 /* .iPadding1 = */ 0, 903 /* .db = */ 0, 904 /* .xDel = */ 0, 905 #ifdef SQLITE_DEBUG 906 /* .pScopyFrom = */ 0, 907 /* .pFiller = */ 0, 908 #endif 909 }; 910 return &nullMem; 911 } 912 913 /* 914 ** Check to see if column iCol of the given statement is valid. If 915 ** it is, return a pointer to the Mem for the value of that column. 916 ** If iCol is not valid, return a pointer to a Mem which has a value 917 ** of NULL. 918 */ 919 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 920 Vdbe *pVm; 921 Mem *pOut; 922 923 pVm = (Vdbe *)pStmt; 924 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 925 sqlite3_mutex_enter(pVm->db->mutex); 926 pOut = &pVm->pResultSet[i]; 927 }else{ 928 if( pVm && ALWAYS(pVm->db) ){ 929 sqlite3_mutex_enter(pVm->db->mutex); 930 sqlite3Error(pVm->db, SQLITE_RANGE); 931 } 932 pOut = (Mem*)columnNullValue(); 933 } 934 return pOut; 935 } 936 937 /* 938 ** This function is called after invoking an sqlite3_value_XXX function on a 939 ** column value (i.e. a value returned by evaluating an SQL expression in the 940 ** select list of a SELECT statement) that may cause a malloc() failure. If 941 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 942 ** code of statement pStmt set to SQLITE_NOMEM. 943 ** 944 ** Specifically, this is called from within: 945 ** 946 ** sqlite3_column_int() 947 ** sqlite3_column_int64() 948 ** sqlite3_column_text() 949 ** sqlite3_column_text16() 950 ** sqlite3_column_real() 951 ** sqlite3_column_bytes() 952 ** sqlite3_column_bytes16() 953 ** sqiite3_column_blob() 954 */ 955 static void columnMallocFailure(sqlite3_stmt *pStmt) 956 { 957 /* If malloc() failed during an encoding conversion within an 958 ** sqlite3_column_XXX API, then set the return code of the statement to 959 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 960 ** and _finalize() will return NOMEM. 961 */ 962 Vdbe *p = (Vdbe *)pStmt; 963 if( p ){ 964 p->rc = sqlite3ApiExit(p->db, p->rc); 965 sqlite3_mutex_leave(p->db->mutex); 966 } 967 } 968 969 /**************************** sqlite3_column_ ******************************* 970 ** The following routines are used to access elements of the current row 971 ** in the result set. 972 */ 973 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 974 const void *val; 975 val = sqlite3_value_blob( columnMem(pStmt,i) ); 976 /* Even though there is no encoding conversion, value_blob() might 977 ** need to call malloc() to expand the result of a zeroblob() 978 ** expression. 979 */ 980 columnMallocFailure(pStmt); 981 return val; 982 } 983 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 984 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 985 columnMallocFailure(pStmt); 986 return val; 987 } 988 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 989 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 990 columnMallocFailure(pStmt); 991 return val; 992 } 993 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 994 double val = sqlite3_value_double( columnMem(pStmt,i) ); 995 columnMallocFailure(pStmt); 996 return val; 997 } 998 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 999 int val = sqlite3_value_int( columnMem(pStmt,i) ); 1000 columnMallocFailure(pStmt); 1001 return val; 1002 } 1003 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 1004 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 1005 columnMallocFailure(pStmt); 1006 return val; 1007 } 1008 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 1009 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 1010 columnMallocFailure(pStmt); 1011 return val; 1012 } 1013 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 1014 Mem *pOut = columnMem(pStmt, i); 1015 if( pOut->flags&MEM_Static ){ 1016 pOut->flags &= ~MEM_Static; 1017 pOut->flags |= MEM_Ephem; 1018 } 1019 columnMallocFailure(pStmt); 1020 return (sqlite3_value *)pOut; 1021 } 1022 #ifndef SQLITE_OMIT_UTF16 1023 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 1024 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 1025 columnMallocFailure(pStmt); 1026 return val; 1027 } 1028 #endif /* SQLITE_OMIT_UTF16 */ 1029 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 1030 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 1031 columnMallocFailure(pStmt); 1032 return iType; 1033 } 1034 1035 /* 1036 ** Convert the N-th element of pStmt->pColName[] into a string using 1037 ** xFunc() then return that string. If N is out of range, return 0. 1038 ** 1039 ** There are up to 5 names for each column. useType determines which 1040 ** name is returned. Here are the names: 1041 ** 1042 ** 0 The column name as it should be displayed for output 1043 ** 1 The datatype name for the column 1044 ** 2 The name of the database that the column derives from 1045 ** 3 The name of the table that the column derives from 1046 ** 4 The name of the table column that the result column derives from 1047 ** 1048 ** If the result is not a simple column reference (if it is an expression 1049 ** or a constant) then useTypes 2, 3, and 4 return NULL. 1050 */ 1051 static const void *columnName( 1052 sqlite3_stmt *pStmt, 1053 int N, 1054 const void *(*xFunc)(Mem*), 1055 int useType 1056 ){ 1057 const void *ret; 1058 Vdbe *p; 1059 int n; 1060 sqlite3 *db; 1061 #ifdef SQLITE_ENABLE_API_ARMOR 1062 if( pStmt==0 ){ 1063 (void)SQLITE_MISUSE_BKPT; 1064 return 0; 1065 } 1066 #endif 1067 ret = 0; 1068 p = (Vdbe *)pStmt; 1069 db = p->db; 1070 assert( db!=0 ); 1071 n = sqlite3_column_count(pStmt); 1072 if( N<n && N>=0 ){ 1073 N += useType*n; 1074 sqlite3_mutex_enter(db->mutex); 1075 assert( db->mallocFailed==0 ); 1076 ret = xFunc(&p->aColName[N]); 1077 /* A malloc may have failed inside of the xFunc() call. If this 1078 ** is the case, clear the mallocFailed flag and return NULL. 1079 */ 1080 if( db->mallocFailed ){ 1081 db->mallocFailed = 0; 1082 ret = 0; 1083 } 1084 sqlite3_mutex_leave(db->mutex); 1085 } 1086 return ret; 1087 } 1088 1089 /* 1090 ** Return the name of the Nth column of the result set returned by SQL 1091 ** statement pStmt. 1092 */ 1093 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 1094 return columnName( 1095 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 1096 } 1097 #ifndef SQLITE_OMIT_UTF16 1098 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 1099 return columnName( 1100 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 1101 } 1102 #endif 1103 1104 /* 1105 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 1106 ** not define OMIT_DECLTYPE. 1107 */ 1108 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 1109 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 1110 and SQLITE_ENABLE_COLUMN_METADATA" 1111 #endif 1112 1113 #ifndef SQLITE_OMIT_DECLTYPE 1114 /* 1115 ** Return the column declaration type (if applicable) of the 'i'th column 1116 ** of the result set of SQL statement pStmt. 1117 */ 1118 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 1119 return columnName( 1120 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 1121 } 1122 #ifndef SQLITE_OMIT_UTF16 1123 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 1124 return columnName( 1125 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 1126 } 1127 #endif /* SQLITE_OMIT_UTF16 */ 1128 #endif /* SQLITE_OMIT_DECLTYPE */ 1129 1130 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1131 /* 1132 ** Return the name of the database from which a result column derives. 1133 ** NULL is returned if the result column is an expression or constant or 1134 ** anything else which is not an unambiguous reference to a database column. 1135 */ 1136 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1137 return columnName( 1138 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 1139 } 1140 #ifndef SQLITE_OMIT_UTF16 1141 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1142 return columnName( 1143 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 1144 } 1145 #endif /* SQLITE_OMIT_UTF16 */ 1146 1147 /* 1148 ** Return the name of the table from which a result column derives. 1149 ** NULL is returned if the result column is an expression or constant or 1150 ** anything else which is not an unambiguous reference to a database column. 1151 */ 1152 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1153 return columnName( 1154 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 1155 } 1156 #ifndef SQLITE_OMIT_UTF16 1157 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1158 return columnName( 1159 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 1160 } 1161 #endif /* SQLITE_OMIT_UTF16 */ 1162 1163 /* 1164 ** Return the name of the table column from which a result column derives. 1165 ** NULL is returned if the result column is an expression or constant or 1166 ** anything else which is not an unambiguous reference to a database column. 1167 */ 1168 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1169 return columnName( 1170 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 1171 } 1172 #ifndef SQLITE_OMIT_UTF16 1173 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1174 return columnName( 1175 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 1176 } 1177 #endif /* SQLITE_OMIT_UTF16 */ 1178 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1179 1180 1181 /******************************* sqlite3_bind_ *************************** 1182 ** 1183 ** Routines used to attach values to wildcards in a compiled SQL statement. 1184 */ 1185 /* 1186 ** Unbind the value bound to variable i in virtual machine p. This is the 1187 ** the same as binding a NULL value to the column. If the "i" parameter is 1188 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 1189 ** 1190 ** A successful evaluation of this routine acquires the mutex on p. 1191 ** the mutex is released if any kind of error occurs. 1192 ** 1193 ** The error code stored in database p->db is overwritten with the return 1194 ** value in any case. 1195 */ 1196 static int vdbeUnbind(Vdbe *p, int i){ 1197 Mem *pVar; 1198 if( vdbeSafetyNotNull(p) ){ 1199 return SQLITE_MISUSE_BKPT; 1200 } 1201 sqlite3_mutex_enter(p->db->mutex); 1202 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 1203 sqlite3Error(p->db, SQLITE_MISUSE); 1204 sqlite3_mutex_leave(p->db->mutex); 1205 sqlite3_log(SQLITE_MISUSE, 1206 "bind on a busy prepared statement: [%s]", p->zSql); 1207 return SQLITE_MISUSE_BKPT; 1208 } 1209 if( i<1 || i>p->nVar ){ 1210 sqlite3Error(p->db, SQLITE_RANGE); 1211 sqlite3_mutex_leave(p->db->mutex); 1212 return SQLITE_RANGE; 1213 } 1214 i--; 1215 pVar = &p->aVar[i]; 1216 sqlite3VdbeMemRelease(pVar); 1217 pVar->flags = MEM_Null; 1218 sqlite3Error(p->db, SQLITE_OK); 1219 1220 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 1221 ** binding a new value to this variable invalidates the current query plan. 1222 ** 1223 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host 1224 ** parameter in the WHERE clause might influence the choice of query plan 1225 ** for a statement, then the statement will be automatically recompiled, 1226 ** as if there had been a schema change, on the first sqlite3_step() call 1227 ** following any change to the bindings of that parameter. 1228 */ 1229 if( p->isPrepareV2 && 1230 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) 1231 ){ 1232 p->expired = 1; 1233 } 1234 return SQLITE_OK; 1235 } 1236 1237 /* 1238 ** Bind a text or BLOB value. 1239 */ 1240 static int bindText( 1241 sqlite3_stmt *pStmt, /* The statement to bind against */ 1242 int i, /* Index of the parameter to bind */ 1243 const void *zData, /* Pointer to the data to be bound */ 1244 int nData, /* Number of bytes of data to be bound */ 1245 void (*xDel)(void*), /* Destructor for the data */ 1246 u8 encoding /* Encoding for the data */ 1247 ){ 1248 Vdbe *p = (Vdbe *)pStmt; 1249 Mem *pVar; 1250 int rc; 1251 1252 rc = vdbeUnbind(p, i); 1253 if( rc==SQLITE_OK ){ 1254 if( zData!=0 ){ 1255 pVar = &p->aVar[i-1]; 1256 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1257 if( rc==SQLITE_OK && encoding!=0 ){ 1258 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1259 } 1260 sqlite3Error(p->db, rc); 1261 rc = sqlite3ApiExit(p->db, rc); 1262 } 1263 sqlite3_mutex_leave(p->db->mutex); 1264 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 1265 xDel((void*)zData); 1266 } 1267 return rc; 1268 } 1269 1270 1271 /* 1272 ** Bind a blob value to an SQL statement variable. 1273 */ 1274 int sqlite3_bind_blob( 1275 sqlite3_stmt *pStmt, 1276 int i, 1277 const void *zData, 1278 int nData, 1279 void (*xDel)(void*) 1280 ){ 1281 return bindText(pStmt, i, zData, nData, xDel, 0); 1282 } 1283 int sqlite3_bind_blob64( 1284 sqlite3_stmt *pStmt, 1285 int i, 1286 const void *zData, 1287 sqlite3_uint64 nData, 1288 void (*xDel)(void*) 1289 ){ 1290 assert( xDel!=SQLITE_DYNAMIC ); 1291 if( nData>0x7fffffff ){ 1292 return invokeValueDestructor(zData, xDel, 0); 1293 }else{ 1294 return bindText(pStmt, i, zData, (int)nData, xDel, 0); 1295 } 1296 } 1297 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1298 int rc; 1299 Vdbe *p = (Vdbe *)pStmt; 1300 rc = vdbeUnbind(p, i); 1301 if( rc==SQLITE_OK ){ 1302 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1303 sqlite3_mutex_leave(p->db->mutex); 1304 } 1305 return rc; 1306 } 1307 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1308 return sqlite3_bind_int64(p, i, (i64)iValue); 1309 } 1310 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 1311 int rc; 1312 Vdbe *p = (Vdbe *)pStmt; 1313 rc = vdbeUnbind(p, i); 1314 if( rc==SQLITE_OK ){ 1315 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1316 sqlite3_mutex_leave(p->db->mutex); 1317 } 1318 return rc; 1319 } 1320 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1321 int rc; 1322 Vdbe *p = (Vdbe*)pStmt; 1323 rc = vdbeUnbind(p, i); 1324 if( rc==SQLITE_OK ){ 1325 sqlite3_mutex_leave(p->db->mutex); 1326 } 1327 return rc; 1328 } 1329 int sqlite3_bind_text( 1330 sqlite3_stmt *pStmt, 1331 int i, 1332 const char *zData, 1333 int nData, 1334 void (*xDel)(void*) 1335 ){ 1336 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1337 } 1338 int sqlite3_bind_text64( 1339 sqlite3_stmt *pStmt, 1340 int i, 1341 const char *zData, 1342 sqlite3_uint64 nData, 1343 void (*xDel)(void*), 1344 unsigned char enc 1345 ){ 1346 assert( xDel!=SQLITE_DYNAMIC ); 1347 if( nData>0x7fffffff ){ 1348 return invokeValueDestructor(zData, xDel, 0); 1349 }else{ 1350 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 1351 return bindText(pStmt, i, zData, (int)nData, xDel, enc); 1352 } 1353 } 1354 #ifndef SQLITE_OMIT_UTF16 1355 int sqlite3_bind_text16( 1356 sqlite3_stmt *pStmt, 1357 int i, 1358 const void *zData, 1359 int nData, 1360 void (*xDel)(void*) 1361 ){ 1362 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1363 } 1364 #endif /* SQLITE_OMIT_UTF16 */ 1365 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1366 int rc; 1367 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 1368 case SQLITE_INTEGER: { 1369 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1370 break; 1371 } 1372 case SQLITE_FLOAT: { 1373 rc = sqlite3_bind_double(pStmt, i, pValue->u.r); 1374 break; 1375 } 1376 case SQLITE_BLOB: { 1377 if( pValue->flags & MEM_Zero ){ 1378 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1379 }else{ 1380 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1381 } 1382 break; 1383 } 1384 case SQLITE_TEXT: { 1385 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1386 pValue->enc); 1387 break; 1388 } 1389 default: { 1390 rc = sqlite3_bind_null(pStmt, i); 1391 break; 1392 } 1393 } 1394 return rc; 1395 } 1396 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1397 int rc; 1398 Vdbe *p = (Vdbe *)pStmt; 1399 rc = vdbeUnbind(p, i); 1400 if( rc==SQLITE_OK ){ 1401 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1402 sqlite3_mutex_leave(p->db->mutex); 1403 } 1404 return rc; 1405 } 1406 1407 /* 1408 ** Return the number of wildcards that can be potentially bound to. 1409 ** This routine is added to support DBD::SQLite. 1410 */ 1411 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1412 Vdbe *p = (Vdbe*)pStmt; 1413 return p ? p->nVar : 0; 1414 } 1415 1416 /* 1417 ** Return the name of a wildcard parameter. Return NULL if the index 1418 ** is out of range or if the wildcard is unnamed. 1419 ** 1420 ** The result is always UTF-8. 1421 */ 1422 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1423 Vdbe *p = (Vdbe*)pStmt; 1424 if( p==0 || i<1 || i>p->nzVar ){ 1425 return 0; 1426 } 1427 return p->azVar[i-1]; 1428 } 1429 1430 /* 1431 ** Given a wildcard parameter name, return the index of the variable 1432 ** with that name. If there is no variable with the given name, 1433 ** return 0. 1434 */ 1435 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1436 int i; 1437 if( p==0 ){ 1438 return 0; 1439 } 1440 if( zName ){ 1441 for(i=0; i<p->nzVar; i++){ 1442 const char *z = p->azVar[i]; 1443 if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ 1444 return i+1; 1445 } 1446 } 1447 } 1448 return 0; 1449 } 1450 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1451 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 1452 } 1453 1454 /* 1455 ** Transfer all bindings from the first statement over to the second. 1456 */ 1457 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1458 Vdbe *pFrom = (Vdbe*)pFromStmt; 1459 Vdbe *pTo = (Vdbe*)pToStmt; 1460 int i; 1461 assert( pTo->db==pFrom->db ); 1462 assert( pTo->nVar==pFrom->nVar ); 1463 sqlite3_mutex_enter(pTo->db->mutex); 1464 for(i=0; i<pFrom->nVar; i++){ 1465 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1466 } 1467 sqlite3_mutex_leave(pTo->db->mutex); 1468 return SQLITE_OK; 1469 } 1470 1471 #ifndef SQLITE_OMIT_DEPRECATED 1472 /* 1473 ** Deprecated external interface. Internal/core SQLite code 1474 ** should call sqlite3TransferBindings. 1475 ** 1476 ** It is misuse to call this routine with statements from different 1477 ** database connections. But as this is a deprecated interface, we 1478 ** will not bother to check for that condition. 1479 ** 1480 ** If the two statements contain a different number of bindings, then 1481 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1482 ** SQLITE_OK is returned. 1483 */ 1484 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1485 Vdbe *pFrom = (Vdbe*)pFromStmt; 1486 Vdbe *pTo = (Vdbe*)pToStmt; 1487 if( pFrom->nVar!=pTo->nVar ){ 1488 return SQLITE_ERROR; 1489 } 1490 if( pTo->isPrepareV2 && pTo->expmask ){ 1491 pTo->expired = 1; 1492 } 1493 if( pFrom->isPrepareV2 && pFrom->expmask ){ 1494 pFrom->expired = 1; 1495 } 1496 return sqlite3TransferBindings(pFromStmt, pToStmt); 1497 } 1498 #endif 1499 1500 /* 1501 ** Return the sqlite3* database handle to which the prepared statement given 1502 ** in the argument belongs. This is the same database handle that was 1503 ** the first argument to the sqlite3_prepare() that was used to create 1504 ** the statement in the first place. 1505 */ 1506 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 1507 return pStmt ? ((Vdbe*)pStmt)->db : 0; 1508 } 1509 1510 /* 1511 ** Return true if the prepared statement is guaranteed to not modify the 1512 ** database. 1513 */ 1514 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1515 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1516 } 1517 1518 /* 1519 ** Return true if the prepared statement is in need of being reset. 1520 */ 1521 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 1522 Vdbe *v = (Vdbe*)pStmt; 1523 return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN; 1524 } 1525 1526 /* 1527 ** Return a pointer to the next prepared statement after pStmt associated 1528 ** with database connection pDb. If pStmt is NULL, return the first 1529 ** prepared statement for the database connection. Return NULL if there 1530 ** are no more. 1531 */ 1532 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1533 sqlite3_stmt *pNext; 1534 #ifdef SQLITE_ENABLE_API_ARMOR 1535 if( !sqlite3SafetyCheckOk(pDb) ){ 1536 (void)SQLITE_MISUSE_BKPT; 1537 return 0; 1538 } 1539 #endif 1540 sqlite3_mutex_enter(pDb->mutex); 1541 if( pStmt==0 ){ 1542 pNext = (sqlite3_stmt*)pDb->pVdbe; 1543 }else{ 1544 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1545 } 1546 sqlite3_mutex_leave(pDb->mutex); 1547 return pNext; 1548 } 1549 1550 /* 1551 ** Return the value of a status counter for a prepared statement 1552 */ 1553 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1554 Vdbe *pVdbe = (Vdbe*)pStmt; 1555 u32 v; 1556 #ifdef SQLITE_ENABLE_API_ARMOR 1557 if( !pStmt ){ 1558 (void)SQLITE_MISUSE_BKPT; 1559 return 0; 1560 } 1561 #endif 1562 v = pVdbe->aCounter[op]; 1563 if( resetFlag ) pVdbe->aCounter[op] = 0; 1564 return (int)v; 1565 } 1566 1567 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 1568 /* 1569 ** Return status data for a single loop within query pStmt. 1570 */ 1571 int sqlite3_stmt_scanstatus( 1572 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 1573 int idx, /* Index of loop to report on */ 1574 int iScanStatusOp, /* Which metric to return */ 1575 void *pOut /* OUT: Write the answer here */ 1576 ){ 1577 Vdbe *p = (Vdbe*)pStmt; 1578 ScanStatus *pScan; 1579 if( idx<0 || idx>=p->nScan ) return 1; 1580 pScan = &p->aScan[idx]; 1581 switch( iScanStatusOp ){ 1582 case SQLITE_SCANSTAT_NLOOP: { 1583 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; 1584 break; 1585 } 1586 case SQLITE_SCANSTAT_NVISIT: { 1587 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; 1588 break; 1589 } 1590 case SQLITE_SCANSTAT_EST: { 1591 double r = 1.0; 1592 LogEst x = pScan->nEst; 1593 while( x<100 ){ 1594 x += 10; 1595 r *= 0.5; 1596 } 1597 *(double*)pOut = r*sqlite3LogEstToInt(x); 1598 break; 1599 } 1600 case SQLITE_SCANSTAT_NAME: { 1601 *(const char**)pOut = pScan->zName; 1602 break; 1603 } 1604 case SQLITE_SCANSTAT_EXPLAIN: { 1605 if( pScan->addrExplain ){ 1606 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; 1607 }else{ 1608 *(const char**)pOut = 0; 1609 } 1610 break; 1611 } 1612 case SQLITE_SCANSTAT_SELECTID: { 1613 if( pScan->addrExplain ){ 1614 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; 1615 }else{ 1616 *(int*)pOut = -1; 1617 } 1618 break; 1619 } 1620 default: { 1621 return 1; 1622 } 1623 } 1624 return 0; 1625 } 1626 1627 /* 1628 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 1629 */ 1630 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 1631 Vdbe *p = (Vdbe*)pStmt; 1632 memset(p->anExec, 0, p->nOp * sizeof(i64)); 1633 } 1634 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ 1635