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 ** $Id: vdbeapi.c,v 1.167 2009/06/25 01:47:12 drh Exp $ 17 */ 18 #include "sqliteInt.h" 19 #include "vdbeInt.h" 20 21 #ifndef SQLITE_OMIT_DEPRECATED 22 /* 23 ** Return TRUE (non-zero) of the statement supplied as an argument needs 24 ** to be recompiled. A statement needs to be recompiled whenever the 25 ** execution environment changes in a way that would alter the program 26 ** that sqlite3_prepare() generates. For example, if new functions or 27 ** collating sequences are registered or if an authorizer function is 28 ** added or changed. 29 */ 30 int sqlite3_expired(sqlite3_stmt *pStmt){ 31 Vdbe *p = (Vdbe*)pStmt; 32 return p==0 || p->expired; 33 } 34 #endif 35 36 /* 37 ** The following routine destroys a virtual machine that is created by 38 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 39 ** success/failure code that describes the result of executing the virtual 40 ** machine. 41 ** 42 ** This routine sets the error code and string returned by 43 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 44 */ 45 int sqlite3_finalize(sqlite3_stmt *pStmt){ 46 int rc; 47 if( pStmt==0 ){ 48 rc = SQLITE_OK; 49 }else{ 50 Vdbe *v = (Vdbe*)pStmt; 51 sqlite3 *db = v->db; 52 #if SQLITE_THREADSAFE 53 sqlite3_mutex *mutex = v->db->mutex; 54 #endif 55 sqlite3_mutex_enter(mutex); 56 rc = sqlite3VdbeFinalize(v); 57 rc = sqlite3ApiExit(db, rc); 58 sqlite3_mutex_leave(mutex); 59 } 60 return rc; 61 } 62 63 /* 64 ** Terminate the current execution of an SQL statement and reset it 65 ** back to its starting state so that it can be reused. A success code from 66 ** the prior execution is returned. 67 ** 68 ** This routine sets the error code and string returned by 69 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 70 */ 71 int sqlite3_reset(sqlite3_stmt *pStmt){ 72 int rc; 73 if( pStmt==0 ){ 74 rc = SQLITE_OK; 75 }else{ 76 Vdbe *v = (Vdbe*)pStmt; 77 sqlite3_mutex_enter(v->db->mutex); 78 rc = sqlite3VdbeReset(v); 79 sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0); 80 assert( (rc & (v->db->errMask))==rc ); 81 rc = sqlite3ApiExit(v->db, rc); 82 sqlite3_mutex_leave(v->db->mutex); 83 } 84 return rc; 85 } 86 87 /* 88 ** Set all the parameters in the compiled SQL statement to NULL. 89 */ 90 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 91 int i; 92 int rc = SQLITE_OK; 93 Vdbe *p = (Vdbe*)pStmt; 94 #if SQLITE_THREADSAFE 95 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 96 #endif 97 sqlite3_mutex_enter(mutex); 98 for(i=0; i<p->nVar; i++){ 99 sqlite3VdbeMemRelease(&p->aVar[i]); 100 p->aVar[i].flags = MEM_Null; 101 } 102 sqlite3_mutex_leave(mutex); 103 return rc; 104 } 105 106 107 /**************************** sqlite3_value_ ******************************* 108 ** The following routines extract information from a Mem or sqlite3_value 109 ** structure. 110 */ 111 const void *sqlite3_value_blob(sqlite3_value *pVal){ 112 Mem *p = (Mem*)pVal; 113 if( p->flags & (MEM_Blob|MEM_Str) ){ 114 sqlite3VdbeMemExpandBlob(p); 115 p->flags &= ~MEM_Str; 116 p->flags |= MEM_Blob; 117 return p->z; 118 }else{ 119 return sqlite3_value_text(pVal); 120 } 121 } 122 int sqlite3_value_bytes(sqlite3_value *pVal){ 123 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 124 } 125 int sqlite3_value_bytes16(sqlite3_value *pVal){ 126 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 127 } 128 double sqlite3_value_double(sqlite3_value *pVal){ 129 return sqlite3VdbeRealValue((Mem*)pVal); 130 } 131 int sqlite3_value_int(sqlite3_value *pVal){ 132 return (int)sqlite3VdbeIntValue((Mem*)pVal); 133 } 134 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 135 return sqlite3VdbeIntValue((Mem*)pVal); 136 } 137 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 138 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 139 } 140 #ifndef SQLITE_OMIT_UTF16 141 const void *sqlite3_value_text16(sqlite3_value* pVal){ 142 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 143 } 144 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 145 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 146 } 147 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 148 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 149 } 150 #endif /* SQLITE_OMIT_UTF16 */ 151 int sqlite3_value_type(sqlite3_value* pVal){ 152 return pVal->type; 153 } 154 155 /**************************** sqlite3_result_ ******************************* 156 ** The following routines are used by user-defined functions to specify 157 ** the function result. 158 ** 159 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the 160 ** result as a string or blob but if the string or blob is too large, it 161 ** then sets the error code to SQLITE_TOOBIG 162 */ 163 static void setResultStrOrError( 164 sqlite3_context *pCtx, /* Function context */ 165 const char *z, /* String pointer */ 166 int n, /* Bytes in string, or negative */ 167 u8 enc, /* Encoding of z. 0 for BLOBs */ 168 void (*xDel)(void*) /* Destructor function */ 169 ){ 170 if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){ 171 sqlite3_result_error_toobig(pCtx); 172 } 173 } 174 void sqlite3_result_blob( 175 sqlite3_context *pCtx, 176 const void *z, 177 int n, 178 void (*xDel)(void *) 179 ){ 180 assert( n>=0 ); 181 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 182 setResultStrOrError(pCtx, z, n, 0, xDel); 183 } 184 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 185 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 186 sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 187 } 188 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 189 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 190 pCtx->isError = SQLITE_ERROR; 191 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 192 } 193 #ifndef SQLITE_OMIT_UTF16 194 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 195 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 196 pCtx->isError = SQLITE_ERROR; 197 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 198 } 199 #endif 200 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 201 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 202 sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 203 } 204 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 205 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 206 sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 207 } 208 void sqlite3_result_null(sqlite3_context *pCtx){ 209 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 210 sqlite3VdbeMemSetNull(&pCtx->s); 211 } 212 void sqlite3_result_text( 213 sqlite3_context *pCtx, 214 const char *z, 215 int n, 216 void (*xDel)(void *) 217 ){ 218 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 219 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 220 } 221 #ifndef SQLITE_OMIT_UTF16 222 void sqlite3_result_text16( 223 sqlite3_context *pCtx, 224 const void *z, 225 int n, 226 void (*xDel)(void *) 227 ){ 228 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 229 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 230 } 231 void sqlite3_result_text16be( 232 sqlite3_context *pCtx, 233 const void *z, 234 int n, 235 void (*xDel)(void *) 236 ){ 237 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 238 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 239 } 240 void sqlite3_result_text16le( 241 sqlite3_context *pCtx, 242 const void *z, 243 int n, 244 void (*xDel)(void *) 245 ){ 246 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 247 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 248 } 249 #endif /* SQLITE_OMIT_UTF16 */ 250 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 251 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 252 sqlite3VdbeMemCopy(&pCtx->s, pValue); 253 } 254 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 255 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 256 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); 257 } 258 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 259 pCtx->isError = errCode; 260 if( pCtx->s.flags & MEM_Null ){ 261 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 262 SQLITE_UTF8, SQLITE_STATIC); 263 } 264 } 265 266 /* Force an SQLITE_TOOBIG error. */ 267 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 268 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 269 pCtx->isError = SQLITE_TOOBIG; 270 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 271 SQLITE_UTF8, SQLITE_STATIC); 272 } 273 274 /* An SQLITE_NOMEM error. */ 275 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 276 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 277 sqlite3VdbeMemSetNull(&pCtx->s); 278 pCtx->isError = SQLITE_NOMEM; 279 pCtx->s.db->mallocFailed = 1; 280 } 281 282 /* 283 ** Execute the statement pStmt, either until a row of data is ready, the 284 ** statement is completely executed or an error occurs. 285 ** 286 ** This routine implements the bulk of the logic behind the sqlite_step() 287 ** API. The only thing omitted is the automatic recompile if a 288 ** schema change has occurred. That detail is handled by the 289 ** outer sqlite3_step() wrapper procedure. 290 */ 291 static int sqlite3Step(Vdbe *p){ 292 sqlite3 *db; 293 int rc; 294 295 assert(p); 296 if( p->magic!=VDBE_MAGIC_RUN ){ 297 return SQLITE_MISUSE; 298 } 299 300 /* Assert that malloc() has not failed */ 301 db = p->db; 302 if( db->mallocFailed ){ 303 return SQLITE_NOMEM; 304 } 305 306 if( p->pc<=0 && p->expired ){ 307 if( ALWAYS(p->rc==SQLITE_OK) ){ 308 p->rc = SQLITE_SCHEMA; 309 } 310 rc = SQLITE_ERROR; 311 goto end_of_step; 312 } 313 if( sqlite3SafetyOn(db) ){ 314 p->rc = SQLITE_MISUSE; 315 return SQLITE_MISUSE; 316 } 317 if( p->pc<0 ){ 318 /* If there are no other statements currently running, then 319 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 320 ** from interrupting a statement that has not yet started. 321 */ 322 if( db->activeVdbeCnt==0 ){ 323 db->u1.isInterrupted = 0; 324 } 325 326 assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 ); 327 328 #ifndef SQLITE_OMIT_TRACE 329 if( db->xProfile && !db->init.busy ){ 330 double rNow; 331 sqlite3OsCurrentTime(db->pVfs, &rNow); 332 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); 333 } 334 #endif 335 336 db->activeVdbeCnt++; 337 if( p->readOnly==0 ) db->writeVdbeCnt++; 338 p->pc = 0; 339 } 340 #ifndef SQLITE_OMIT_EXPLAIN 341 if( p->explain ){ 342 rc = sqlite3VdbeList(p); 343 }else 344 #endif /* SQLITE_OMIT_EXPLAIN */ 345 { 346 rc = sqlite3VdbeExec(p); 347 } 348 349 if( sqlite3SafetyOff(db) ){ 350 rc = SQLITE_MISUSE; 351 } 352 353 #ifndef SQLITE_OMIT_TRACE 354 /* Invoke the profile callback if there is one 355 */ 356 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ 357 double rNow; 358 u64 elapseTime; 359 360 sqlite3OsCurrentTime(db->pVfs, &rNow); 361 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); 362 elapseTime -= p->startTime; 363 db->xProfile(db->pProfileArg, p->zSql, elapseTime); 364 } 365 #endif 366 367 db->errCode = rc; 368 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 369 p->rc = SQLITE_NOMEM; 370 } 371 end_of_step: 372 /* At this point local variable rc holds the value that should be 373 ** returned if this statement was compiled using the legacy 374 ** sqlite3_prepare() interface. According to the docs, this can only 375 ** be one of the values in the first assert() below. Variable p->rc 376 ** contains the value that would be returned if sqlite3_finalize() 377 ** were called on statement p. 378 */ 379 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 380 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 381 ); 382 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); 383 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 384 /* If this statement was prepared using sqlite3_prepare_v2(), and an 385 ** error has occured, then return the error code in p->rc to the 386 ** caller. Set the error code in the database handle to the same value. 387 */ 388 rc = db->errCode = p->rc; 389 } 390 return (rc&db->errMask); 391 } 392 393 /* 394 ** This is the top-level implementation of sqlite3_step(). Call 395 ** sqlite3Step() to do most of the work. If a schema error occurs, 396 ** call sqlite3Reprepare() and try again. 397 */ 398 int sqlite3_step(sqlite3_stmt *pStmt){ 399 int rc = SQLITE_MISUSE; 400 if( pStmt ){ 401 int cnt = 0; 402 Vdbe *v = (Vdbe*)pStmt; 403 sqlite3 *db = v->db; 404 sqlite3_mutex_enter(db->mutex); 405 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 406 && cnt++ < 5 407 && (rc = sqlite3Reprepare(v))==SQLITE_OK ){ 408 sqlite3_reset(pStmt); 409 v->expired = 0; 410 } 411 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){ 412 /* This case occurs after failing to recompile an sql statement. 413 ** The error message from the SQL compiler has already been loaded 414 ** into the database handle. This block copies the error message 415 ** from the database handle into the statement and sets the statement 416 ** program counter to 0 to ensure that when the statement is 417 ** finalized or reset the parser error message is available via 418 ** sqlite3_errmsg() and sqlite3_errcode(). 419 */ 420 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 421 sqlite3DbFree(db, v->zErrMsg); 422 if( !db->mallocFailed ){ 423 v->zErrMsg = sqlite3DbStrDup(db, zErr); 424 } else { 425 v->zErrMsg = 0; 426 v->rc = SQLITE_NOMEM; 427 } 428 } 429 rc = sqlite3ApiExit(db, rc); 430 sqlite3_mutex_leave(db->mutex); 431 } 432 return rc; 433 } 434 435 /* 436 ** Extract the user data from a sqlite3_context structure and return a 437 ** pointer to it. 438 */ 439 void *sqlite3_user_data(sqlite3_context *p){ 440 assert( p && p->pFunc ); 441 return p->pFunc->pUserData; 442 } 443 444 /* 445 ** Extract the user data from a sqlite3_context structure and return a 446 ** pointer to it. 447 */ 448 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 449 assert( p && p->pFunc ); 450 return p->s.db; 451 } 452 453 /* 454 ** The following is the implementation of an SQL function that always 455 ** fails with an error message stating that the function is used in the 456 ** wrong context. The sqlite3_overload_function() API might construct 457 ** SQL function that use this routine so that the functions will exist 458 ** for name resolution but are actually overloaded by the xFindFunction 459 ** method of virtual tables. 460 */ 461 void sqlite3InvalidFunction( 462 sqlite3_context *context, /* The function calling context */ 463 int NotUsed, /* Number of arguments to the function */ 464 sqlite3_value **NotUsed2 /* Value of each argument */ 465 ){ 466 const char *zName = context->pFunc->zName; 467 char *zErr; 468 UNUSED_PARAMETER2(NotUsed, NotUsed2); 469 zErr = sqlite3_mprintf( 470 "unable to use function %s in the requested context", zName); 471 sqlite3_result_error(context, zErr, -1); 472 sqlite3_free(zErr); 473 } 474 475 /* 476 ** Allocate or return the aggregate context for a user function. A new 477 ** context is allocated on the first call. Subsequent calls return the 478 ** same context that was returned on prior calls. 479 */ 480 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 481 Mem *pMem; 482 assert( p && p->pFunc && p->pFunc->xStep ); 483 assert( sqlite3_mutex_held(p->s.db->mutex) ); 484 pMem = p->pMem; 485 if( (pMem->flags & MEM_Agg)==0 ){ 486 if( nByte==0 ){ 487 sqlite3VdbeMemReleaseExternal(pMem); 488 pMem->flags = MEM_Null; 489 pMem->z = 0; 490 }else{ 491 sqlite3VdbeMemGrow(pMem, nByte, 0); 492 pMem->flags = MEM_Agg; 493 pMem->u.pDef = p->pFunc; 494 if( pMem->z ){ 495 memset(pMem->z, 0, nByte); 496 } 497 } 498 } 499 return (void*)pMem->z; 500 } 501 502 /* 503 ** Return the auxilary data pointer, if any, for the iArg'th argument to 504 ** the user-function defined by pCtx. 505 */ 506 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 507 VdbeFunc *pVdbeFunc; 508 509 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 510 pVdbeFunc = pCtx->pVdbeFunc; 511 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 512 return 0; 513 } 514 return pVdbeFunc->apAux[iArg].pAux; 515 } 516 517 /* 518 ** Set the auxilary data pointer and delete function, for the iArg'th 519 ** argument to the user-function defined by pCtx. Any previous value is 520 ** deleted by calling the delete function specified when it was set. 521 */ 522 void sqlite3_set_auxdata( 523 sqlite3_context *pCtx, 524 int iArg, 525 void *pAux, 526 void (*xDelete)(void*) 527 ){ 528 struct AuxData *pAuxData; 529 VdbeFunc *pVdbeFunc; 530 if( iArg<0 ) goto failed; 531 532 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 533 pVdbeFunc = pCtx->pVdbeFunc; 534 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 535 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); 536 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 537 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); 538 if( !pVdbeFunc ){ 539 goto failed; 540 } 541 pCtx->pVdbeFunc = pVdbeFunc; 542 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); 543 pVdbeFunc->nAux = iArg+1; 544 pVdbeFunc->pFunc = pCtx->pFunc; 545 } 546 547 pAuxData = &pVdbeFunc->apAux[iArg]; 548 if( pAuxData->pAux && pAuxData->xDelete ){ 549 pAuxData->xDelete(pAuxData->pAux); 550 } 551 pAuxData->pAux = pAux; 552 pAuxData->xDelete = xDelete; 553 return; 554 555 failed: 556 if( xDelete ){ 557 xDelete(pAux); 558 } 559 } 560 561 #ifndef SQLITE_OMIT_DEPRECATED 562 /* 563 ** Return the number of times the Step function of a aggregate has been 564 ** called. 565 ** 566 ** This function is deprecated. Do not use it for new code. It is 567 ** provide only to avoid breaking legacy code. New aggregate function 568 ** implementations should keep their own counts within their aggregate 569 ** context. 570 */ 571 int sqlite3_aggregate_count(sqlite3_context *p){ 572 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 573 return p->pMem->n; 574 } 575 #endif 576 577 /* 578 ** Return the number of columns in the result set for the statement pStmt. 579 */ 580 int sqlite3_column_count(sqlite3_stmt *pStmt){ 581 Vdbe *pVm = (Vdbe *)pStmt; 582 return pVm ? pVm->nResColumn : 0; 583 } 584 585 /* 586 ** Return the number of values available from the current row of the 587 ** currently executing statement pStmt. 588 */ 589 int sqlite3_data_count(sqlite3_stmt *pStmt){ 590 Vdbe *pVm = (Vdbe *)pStmt; 591 if( pVm==0 || pVm->pResultSet==0 ) return 0; 592 return pVm->nResColumn; 593 } 594 595 596 /* 597 ** Check to see if column iCol of the given statement is valid. If 598 ** it is, return a pointer to the Mem for the value of that column. 599 ** If iCol is not valid, return a pointer to a Mem which has a value 600 ** of NULL. 601 */ 602 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 603 Vdbe *pVm; 604 int vals; 605 Mem *pOut; 606 607 pVm = (Vdbe *)pStmt; 608 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 609 sqlite3_mutex_enter(pVm->db->mutex); 610 vals = sqlite3_data_count(pStmt); 611 pOut = &pVm->pResultSet[i]; 612 }else{ 613 /* If the value passed as the second argument is out of range, return 614 ** a pointer to the following static Mem object which contains the 615 ** value SQL NULL. Even though the Mem structure contains an element 616 ** of type i64, on certain architecture (x86) with certain compiler 617 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 618 ** instead of an 8-byte one. This all works fine, except that when 619 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 620 ** that a Mem structure is located on an 8-byte boundary. To prevent 621 ** this assert() from failing, when building with SQLITE_DEBUG defined 622 ** using gcc, force nullMem to be 8-byte aligned using the magical 623 ** __attribute__((aligned(8))) macro. */ 624 static const Mem nullMem 625 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 626 __attribute__((aligned(8))) 627 #endif 628 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 }; 629 630 if( pVm && ALWAYS(pVm->db) ){ 631 sqlite3_mutex_enter(pVm->db->mutex); 632 sqlite3Error(pVm->db, SQLITE_RANGE, 0); 633 } 634 pOut = (Mem*)&nullMem; 635 } 636 return pOut; 637 } 638 639 /* 640 ** This function is called after invoking an sqlite3_value_XXX function on a 641 ** column value (i.e. a value returned by evaluating an SQL expression in the 642 ** select list of a SELECT statement) that may cause a malloc() failure. If 643 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 644 ** code of statement pStmt set to SQLITE_NOMEM. 645 ** 646 ** Specifically, this is called from within: 647 ** 648 ** sqlite3_column_int() 649 ** sqlite3_column_int64() 650 ** sqlite3_column_text() 651 ** sqlite3_column_text16() 652 ** sqlite3_column_real() 653 ** sqlite3_column_bytes() 654 ** sqlite3_column_bytes16() 655 ** 656 ** But not for sqlite3_column_blob(), which never calls malloc(). 657 */ 658 static void columnMallocFailure(sqlite3_stmt *pStmt) 659 { 660 /* If malloc() failed during an encoding conversion within an 661 ** sqlite3_column_XXX API, then set the return code of the statement to 662 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 663 ** and _finalize() will return NOMEM. 664 */ 665 Vdbe *p = (Vdbe *)pStmt; 666 if( p ){ 667 p->rc = sqlite3ApiExit(p->db, p->rc); 668 sqlite3_mutex_leave(p->db->mutex); 669 } 670 } 671 672 /**************************** sqlite3_column_ ******************************* 673 ** The following routines are used to access elements of the current row 674 ** in the result set. 675 */ 676 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 677 const void *val; 678 val = sqlite3_value_blob( columnMem(pStmt,i) ); 679 /* Even though there is no encoding conversion, value_blob() might 680 ** need to call malloc() to expand the result of a zeroblob() 681 ** expression. 682 */ 683 columnMallocFailure(pStmt); 684 return val; 685 } 686 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 687 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 688 columnMallocFailure(pStmt); 689 return val; 690 } 691 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 692 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 693 columnMallocFailure(pStmt); 694 return val; 695 } 696 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 697 double val = sqlite3_value_double( columnMem(pStmt,i) ); 698 columnMallocFailure(pStmt); 699 return val; 700 } 701 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 702 int val = sqlite3_value_int( columnMem(pStmt,i) ); 703 columnMallocFailure(pStmt); 704 return val; 705 } 706 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 707 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 708 columnMallocFailure(pStmt); 709 return val; 710 } 711 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 712 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 713 columnMallocFailure(pStmt); 714 return val; 715 } 716 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 717 Mem *pOut = columnMem(pStmt, i); 718 if( pOut->flags&MEM_Static ){ 719 pOut->flags &= ~MEM_Static; 720 pOut->flags |= MEM_Ephem; 721 } 722 columnMallocFailure(pStmt); 723 return (sqlite3_value *)pOut; 724 } 725 #ifndef SQLITE_OMIT_UTF16 726 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 727 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 728 columnMallocFailure(pStmt); 729 return val; 730 } 731 #endif /* SQLITE_OMIT_UTF16 */ 732 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 733 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 734 columnMallocFailure(pStmt); 735 return iType; 736 } 737 738 /* The following function is experimental and subject to change or 739 ** removal */ 740 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 741 ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 742 **} 743 */ 744 745 /* 746 ** Convert the N-th element of pStmt->pColName[] into a string using 747 ** xFunc() then return that string. If N is out of range, return 0. 748 ** 749 ** There are up to 5 names for each column. useType determines which 750 ** name is returned. Here are the names: 751 ** 752 ** 0 The column name as it should be displayed for output 753 ** 1 The datatype name for the column 754 ** 2 The name of the database that the column derives from 755 ** 3 The name of the table that the column derives from 756 ** 4 The name of the table column that the result column derives from 757 ** 758 ** If the result is not a simple column reference (if it is an expression 759 ** or a constant) then useTypes 2, 3, and 4 return NULL. 760 */ 761 static const void *columnName( 762 sqlite3_stmt *pStmt, 763 int N, 764 const void *(*xFunc)(Mem*), 765 int useType 766 ){ 767 const void *ret = 0; 768 Vdbe *p = (Vdbe *)pStmt; 769 int n; 770 sqlite3 *db = p->db; 771 772 assert( db!=0 ); 773 n = sqlite3_column_count(pStmt); 774 if( N<n && N>=0 ){ 775 N += useType*n; 776 sqlite3_mutex_enter(db->mutex); 777 assert( db->mallocFailed==0 ); 778 ret = xFunc(&p->aColName[N]); 779 /* A malloc may have failed inside of the xFunc() call. If this 780 ** is the case, clear the mallocFailed flag and return NULL. 781 */ 782 if( db->mallocFailed ){ 783 db->mallocFailed = 0; 784 ret = 0; 785 } 786 sqlite3_mutex_leave(db->mutex); 787 } 788 return ret; 789 } 790 791 /* 792 ** Return the name of the Nth column of the result set returned by SQL 793 ** statement pStmt. 794 */ 795 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 796 return columnName( 797 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 798 } 799 #ifndef SQLITE_OMIT_UTF16 800 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 801 return columnName( 802 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 803 } 804 #endif 805 806 /* 807 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 808 ** not define OMIT_DECLTYPE. 809 */ 810 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 811 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 812 and SQLITE_ENABLE_COLUMN_METADATA" 813 #endif 814 815 #ifndef SQLITE_OMIT_DECLTYPE 816 /* 817 ** Return the column declaration type (if applicable) of the 'i'th column 818 ** of the result set of SQL statement pStmt. 819 */ 820 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 821 return columnName( 822 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 823 } 824 #ifndef SQLITE_OMIT_UTF16 825 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 826 return columnName( 827 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 828 } 829 #endif /* SQLITE_OMIT_UTF16 */ 830 #endif /* SQLITE_OMIT_DECLTYPE */ 831 832 #ifdef SQLITE_ENABLE_COLUMN_METADATA 833 /* 834 ** Return the name of the database from which a result column derives. 835 ** NULL is returned if the result column is an expression or constant or 836 ** anything else which is not an unabiguous reference to a database column. 837 */ 838 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 839 return columnName( 840 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 841 } 842 #ifndef SQLITE_OMIT_UTF16 843 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 844 return columnName( 845 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 846 } 847 #endif /* SQLITE_OMIT_UTF16 */ 848 849 /* 850 ** Return the name of the table from which a result column derives. 851 ** NULL is returned if the result column is an expression or constant or 852 ** anything else which is not an unabiguous reference to a database column. 853 */ 854 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 855 return columnName( 856 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 857 } 858 #ifndef SQLITE_OMIT_UTF16 859 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 860 return columnName( 861 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 862 } 863 #endif /* SQLITE_OMIT_UTF16 */ 864 865 /* 866 ** Return the name of the table column from which a result column derives. 867 ** NULL is returned if the result column is an expression or constant or 868 ** anything else which is not an unabiguous reference to a database column. 869 */ 870 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 871 return columnName( 872 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 873 } 874 #ifndef SQLITE_OMIT_UTF16 875 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 876 return columnName( 877 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 878 } 879 #endif /* SQLITE_OMIT_UTF16 */ 880 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 881 882 883 /******************************* sqlite3_bind_ *************************** 884 ** 885 ** Routines used to attach values to wildcards in a compiled SQL statement. 886 */ 887 /* 888 ** Unbind the value bound to variable i in virtual machine p. This is the 889 ** the same as binding a NULL value to the column. If the "i" parameter is 890 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 891 ** 892 ** A successful evaluation of this routine acquires the mutex on p. 893 ** the mutex is released if any kind of error occurs. 894 ** 895 ** The error code stored in database p->db is overwritten with the return 896 ** value in any case. 897 */ 898 static int vdbeUnbind(Vdbe *p, int i){ 899 Mem *pVar; 900 if( p==0 ) return SQLITE_MISUSE; 901 sqlite3_mutex_enter(p->db->mutex); 902 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 903 sqlite3Error(p->db, SQLITE_MISUSE, 0); 904 sqlite3_mutex_leave(p->db->mutex); 905 return SQLITE_MISUSE; 906 } 907 if( i<1 || i>p->nVar ){ 908 sqlite3Error(p->db, SQLITE_RANGE, 0); 909 sqlite3_mutex_leave(p->db->mutex); 910 return SQLITE_RANGE; 911 } 912 i--; 913 pVar = &p->aVar[i]; 914 sqlite3VdbeMemRelease(pVar); 915 pVar->flags = MEM_Null; 916 sqlite3Error(p->db, SQLITE_OK, 0); 917 918 /* If the bit corresponding to this variable is set in Vdbe.opmask, set 919 ** the optimizable flag before returning. This tells the sqlite3_reoptimize() 920 ** function that the VM program may benefit from recompilation. 921 ** 922 ** If the bit in Vdbe.expmask is set, then binding a new value to this 923 ** variable invalidates the current query plan. This comes about when the 924 ** variable is the RHS of a LIKE or GLOB operator and the LIKE/GLOB is 925 ** able to use an index. */ 926 if( (i<32 && p->optmask & ((u32)1 << i)) || p->optmask==0xffffffff ){ 927 p->optimizable = 1; 928 } 929 if( (i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff ){ 930 p->expired = 1; 931 } 932 return SQLITE_OK; 933 } 934 935 /* 936 ** Bind a text or BLOB value. 937 */ 938 static int bindText( 939 sqlite3_stmt *pStmt, /* The statement to bind against */ 940 int i, /* Index of the parameter to bind */ 941 const void *zData, /* Pointer to the data to be bound */ 942 int nData, /* Number of bytes of data to be bound */ 943 void (*xDel)(void*), /* Destructor for the data */ 944 u8 encoding /* Encoding for the data */ 945 ){ 946 Vdbe *p = (Vdbe *)pStmt; 947 Mem *pVar; 948 int rc; 949 950 rc = vdbeUnbind(p, i); 951 if( rc==SQLITE_OK ){ 952 if( zData!=0 ){ 953 pVar = &p->aVar[i-1]; 954 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 955 if( rc==SQLITE_OK && encoding!=0 ){ 956 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 957 } 958 sqlite3Error(p->db, rc, 0); 959 rc = sqlite3ApiExit(p->db, rc); 960 } 961 sqlite3_mutex_leave(p->db->mutex); 962 } 963 return rc; 964 } 965 966 967 /* 968 ** Bind a blob value to an SQL statement variable. 969 */ 970 int sqlite3_bind_blob( 971 sqlite3_stmt *pStmt, 972 int i, 973 const void *zData, 974 int nData, 975 void (*xDel)(void*) 976 ){ 977 return bindText(pStmt, i, zData, nData, xDel, 0); 978 } 979 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 980 int rc; 981 Vdbe *p = (Vdbe *)pStmt; 982 rc = vdbeUnbind(p, i); 983 if( rc==SQLITE_OK ){ 984 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 985 sqlite3_mutex_leave(p->db->mutex); 986 } 987 return rc; 988 } 989 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 990 return sqlite3_bind_int64(p, i, (i64)iValue); 991 } 992 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 993 int rc; 994 Vdbe *p = (Vdbe *)pStmt; 995 rc = vdbeUnbind(p, i); 996 if( rc==SQLITE_OK ){ 997 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 998 sqlite3_mutex_leave(p->db->mutex); 999 } 1000 return rc; 1001 } 1002 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1003 int rc; 1004 Vdbe *p = (Vdbe*)pStmt; 1005 rc = vdbeUnbind(p, i); 1006 if( rc==SQLITE_OK ){ 1007 sqlite3_mutex_leave(p->db->mutex); 1008 } 1009 return rc; 1010 } 1011 int sqlite3_bind_text( 1012 sqlite3_stmt *pStmt, 1013 int i, 1014 const char *zData, 1015 int nData, 1016 void (*xDel)(void*) 1017 ){ 1018 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1019 } 1020 #ifndef SQLITE_OMIT_UTF16 1021 int sqlite3_bind_text16( 1022 sqlite3_stmt *pStmt, 1023 int i, 1024 const void *zData, 1025 int nData, 1026 void (*xDel)(void*) 1027 ){ 1028 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1029 } 1030 #endif /* SQLITE_OMIT_UTF16 */ 1031 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1032 int rc; 1033 switch( pValue->type ){ 1034 case SQLITE_INTEGER: { 1035 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1036 break; 1037 } 1038 case SQLITE_FLOAT: { 1039 rc = sqlite3_bind_double(pStmt, i, pValue->r); 1040 break; 1041 } 1042 case SQLITE_BLOB: { 1043 if( pValue->flags & MEM_Zero ){ 1044 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1045 }else{ 1046 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1047 } 1048 break; 1049 } 1050 case SQLITE_TEXT: { 1051 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1052 pValue->enc); 1053 break; 1054 } 1055 default: { 1056 rc = sqlite3_bind_null(pStmt, i); 1057 break; 1058 } 1059 } 1060 return rc; 1061 } 1062 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1063 int rc; 1064 Vdbe *p = (Vdbe *)pStmt; 1065 rc = vdbeUnbind(p, i); 1066 if( rc==SQLITE_OK ){ 1067 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1068 sqlite3_mutex_leave(p->db->mutex); 1069 } 1070 return rc; 1071 } 1072 1073 /* 1074 ** Return the number of wildcards that can be potentially bound to. 1075 ** This routine is added to support DBD::SQLite. 1076 */ 1077 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1078 Vdbe *p = (Vdbe*)pStmt; 1079 return p ? p->nVar : 0; 1080 } 1081 1082 /* 1083 ** Create a mapping from variable numbers to variable names 1084 ** in the Vdbe.azVar[] array, if such a mapping does not already 1085 ** exist. 1086 */ 1087 static void createVarMap(Vdbe *p){ 1088 if( !p->okVar ){ 1089 int j; 1090 Op *pOp; 1091 sqlite3_mutex_enter(p->db->mutex); 1092 /* The race condition here is harmless. If two threads call this 1093 ** routine on the same Vdbe at the same time, they both might end 1094 ** up initializing the Vdbe.azVar[] array. That is a little extra 1095 ** work but it results in the same answer. 1096 */ 1097 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 1098 if( pOp->opcode==OP_Variable ){ 1099 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 1100 p->azVar[pOp->p1-1] = pOp->p4.z; 1101 } 1102 } 1103 p->okVar = 1; 1104 sqlite3_mutex_leave(p->db->mutex); 1105 } 1106 } 1107 1108 /* 1109 ** Return the name of a wildcard parameter. Return NULL if the index 1110 ** is out of range or if the wildcard is unnamed. 1111 ** 1112 ** The result is always UTF-8. 1113 */ 1114 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1115 Vdbe *p = (Vdbe*)pStmt; 1116 if( p==0 || i<1 || i>p->nVar ){ 1117 return 0; 1118 } 1119 createVarMap(p); 1120 return p->azVar[i-1]; 1121 } 1122 1123 /* 1124 ** Given a wildcard parameter name, return the index of the variable 1125 ** with that name. If there is no variable with the given name, 1126 ** return 0. 1127 */ 1128 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1129 Vdbe *p = (Vdbe*)pStmt; 1130 int i; 1131 if( p==0 ){ 1132 return 0; 1133 } 1134 createVarMap(p); 1135 if( zName ){ 1136 for(i=0; i<p->nVar; i++){ 1137 const char *z = p->azVar[i]; 1138 if( z && strcmp(z,zName)==0 ){ 1139 return i+1; 1140 } 1141 } 1142 } 1143 return 0; 1144 } 1145 1146 /* 1147 ** Transfer all bindings from the first statement over to the second. 1148 */ 1149 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1150 Vdbe *pFrom = (Vdbe*)pFromStmt; 1151 Vdbe *pTo = (Vdbe*)pToStmt; 1152 int i; 1153 assert( pTo->db==pFrom->db ); 1154 assert( pTo->nVar==pFrom->nVar ); 1155 sqlite3_mutex_enter(pTo->db->mutex); 1156 for(i=0; i<pFrom->nVar; i++){ 1157 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1158 } 1159 sqlite3_mutex_leave(pTo->db->mutex); 1160 return SQLITE_OK; 1161 } 1162 1163 #ifndef SQLITE_OMIT_DEPRECATED 1164 /* 1165 ** Deprecated external interface. Internal/core SQLite code 1166 ** should call sqlite3TransferBindings. 1167 ** 1168 ** Is is misuse to call this routine with statements from different 1169 ** database connections. But as this is a deprecated interface, we 1170 ** will not bother to check for that condition. 1171 ** 1172 ** If the two statements contain a different number of bindings, then 1173 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1174 ** SQLITE_OK is returned. 1175 */ 1176 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1177 Vdbe *pFrom = (Vdbe*)pFromStmt; 1178 Vdbe *pTo = (Vdbe*)pToStmt; 1179 if( pFrom->nVar!=pTo->nVar ){ 1180 return SQLITE_ERROR; 1181 } 1182 return sqlite3TransferBindings(pFromStmt, pToStmt); 1183 } 1184 #endif 1185 1186 /* 1187 ** Return the sqlite3* database handle to which the prepared statement given 1188 ** in the argument belongs. This is the same database handle that was 1189 ** the first argument to the sqlite3_prepare() that was used to create 1190 ** the statement in the first place. 1191 */ 1192 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 1193 return pStmt ? ((Vdbe*)pStmt)->db : 0; 1194 } 1195 1196 /* 1197 ** Return a pointer to the next prepared statement after pStmt associated 1198 ** with database connection pDb. If pStmt is NULL, return the first 1199 ** prepared statement for the database connection. Return NULL if there 1200 ** are no more. 1201 */ 1202 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1203 sqlite3_stmt *pNext; 1204 sqlite3_mutex_enter(pDb->mutex); 1205 if( pStmt==0 ){ 1206 pNext = (sqlite3_stmt*)pDb->pVdbe; 1207 }else{ 1208 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1209 } 1210 sqlite3_mutex_leave(pDb->mutex); 1211 return pNext; 1212 } 1213 1214 /* 1215 ** Return the value of a status counter for a prepared statement 1216 */ 1217 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1218 Vdbe *pVdbe = (Vdbe*)pStmt; 1219 int v = pVdbe->aCounter[op-1]; 1220 if( resetFlag ) pVdbe->aCounter[op-1] = 0; 1221 return v; 1222 } 1223 1224 /* 1225 ** If possible, optimize the statement for the current bindings. 1226 */ 1227 int sqlite3_reoptimize(sqlite3_stmt *pStmt){ 1228 int rc = SQLITE_OK; 1229 Vdbe *v = (Vdbe *)pStmt; 1230 sqlite3 *db = v->db; 1231 1232 sqlite3_mutex_enter(db->mutex); 1233 if( v->isPrepareV2==0 || v->pc>0 ){ 1234 rc = SQLITE_MISUSE; 1235 }else if( v->optimizable ){ 1236 rc = sqlite3Reprepare(v); 1237 rc = sqlite3ApiExit(db, rc); 1238 } 1239 assert( rc!=SQLITE_OK || v->optimizable==0 ); 1240 sqlite3_mutex_leave(db->mutex); 1241 1242 return rc; 1243 } 1244 1245