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