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