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.164 2009/04/27 18:46:06 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 if( pCtx->s.flags & MEM_Null ){ 404 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 405 SQLITE_UTF8, SQLITE_STATIC); 406 } 407 } 408 409 /* Force an SQLITE_TOOBIG error. */ 410 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 411 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 412 pCtx->isError = SQLITE_TOOBIG; 413 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 414 SQLITE_UTF8, SQLITE_STATIC); 415 } 416 417 /* An SQLITE_NOMEM error. */ 418 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 419 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 420 sqlite3VdbeMemSetNull(&pCtx->s); 421 pCtx->isError = SQLITE_NOMEM; 422 pCtx->s.db->mallocFailed = 1; 423 } 424 425 /* 426 ** Execute the statement pStmt, either until a row of data is ready, the 427 ** statement is completely executed or an error occurs. 428 ** 429 ** This routine implements the bulk of the logic behind the sqlite_step() 430 ** API. The only thing omitted is the automatic recompile if a 431 ** schema change has occurred. That detail is handled by the 432 ** outer sqlite3_step() wrapper procedure. 433 */ 434 static int sqlite3Step(Vdbe *p){ 435 sqlite3 *db; 436 int rc; 437 438 assert(p); 439 if( p->magic!=VDBE_MAGIC_RUN ){ 440 return SQLITE_MISUSE; 441 } 442 443 /* Assert that malloc() has not failed */ 444 db = p->db; 445 if( db->mallocFailed ){ 446 return SQLITE_NOMEM; 447 } 448 449 if( p->pc<=0 && p->expired ){ 450 if( ALWAYS(p->rc==SQLITE_OK) ){ 451 p->rc = SQLITE_SCHEMA; 452 } 453 rc = SQLITE_ERROR; 454 goto end_of_step; 455 } 456 if( sqlite3SafetyOn(db) ){ 457 p->rc = SQLITE_MISUSE; 458 return SQLITE_MISUSE; 459 } 460 if( p->pc<0 ){ 461 /* If there are no other statements currently running, then 462 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 463 ** from interrupting a statement that has not yet started. 464 */ 465 if( db->activeVdbeCnt==0 ){ 466 db->u1.isInterrupted = 0; 467 } 468 469 #ifndef SQLITE_OMIT_TRACE 470 if( db->xProfile && !db->init.busy ){ 471 double rNow; 472 sqlite3OsCurrentTime(db->pVfs, &rNow); 473 p->startTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); 474 } 475 #endif 476 477 db->activeVdbeCnt++; 478 if( p->readOnly==0 ) db->writeVdbeCnt++; 479 p->pc = 0; 480 stmtLruRemove(p); 481 } 482 #ifndef SQLITE_OMIT_EXPLAIN 483 if( p->explain ){ 484 rc = sqlite3VdbeList(p); 485 }else 486 #endif /* SQLITE_OMIT_EXPLAIN */ 487 { 488 rc = sqlite3VdbeExec(p); 489 } 490 491 if( sqlite3SafetyOff(db) ){ 492 rc = SQLITE_MISUSE; 493 } 494 495 #ifndef SQLITE_OMIT_TRACE 496 /* Invoke the profile callback if there is one 497 */ 498 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ 499 double rNow; 500 u64 elapseTime; 501 502 sqlite3OsCurrentTime(db->pVfs, &rNow); 503 elapseTime = (u64)((rNow - (int)rNow)*3600.0*24.0*1000000000.0); 504 elapseTime -= p->startTime; 505 db->xProfile(db->pProfileArg, p->zSql, elapseTime); 506 } 507 #endif 508 509 db->errCode = rc; 510 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 511 p->rc = SQLITE_NOMEM; 512 } 513 end_of_step: 514 /* At this point local variable rc holds the value that should be 515 ** returned if this statement was compiled using the legacy 516 ** sqlite3_prepare() interface. According to the docs, this can only 517 ** be one of the values in the first assert() below. Variable p->rc 518 ** contains the value that would be returned if sqlite3_finalize() 519 ** were called on statement p. 520 */ 521 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 522 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 523 ); 524 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); 525 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 526 /* If this statement was prepared using sqlite3_prepare_v2(), and an 527 ** error has occured, then return the error code in p->rc to the 528 ** caller. Set the error code in the database handle to the same value. 529 */ 530 rc = db->errCode = p->rc; 531 } 532 return (rc&db->errMask); 533 } 534 535 /* 536 ** This is the top-level implementation of sqlite3_step(). Call 537 ** sqlite3Step() to do most of the work. If a schema error occurs, 538 ** call sqlite3Reprepare() and try again. 539 */ 540 #ifdef SQLITE_OMIT_PARSER 541 int sqlite3_step(sqlite3_stmt *pStmt){ 542 int rc = SQLITE_MISUSE; 543 if( pStmt ){ 544 Vdbe *v; 545 v = (Vdbe*)pStmt; 546 sqlite3_mutex_enter(v->db->mutex); 547 rc = sqlite3Step(v); 548 sqlite3_mutex_leave(v->db->mutex); 549 } 550 return rc; 551 } 552 #else 553 int sqlite3_step(sqlite3_stmt *pStmt){ 554 int rc = SQLITE_MISUSE; 555 if( pStmt ){ 556 int cnt = 0; 557 Vdbe *v = (Vdbe*)pStmt; 558 sqlite3 *db = v->db; 559 sqlite3_mutex_enter(db->mutex); 560 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 561 && cnt++ < 5 562 && (rc = vdbeReprepare(v))==SQLITE_OK ){ 563 sqlite3_reset(pStmt); 564 v->expired = 0; 565 } 566 if( rc==SQLITE_SCHEMA && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){ 567 /* This case occurs after failing to recompile an sql statement. 568 ** The error message from the SQL compiler has already been loaded 569 ** into the database handle. This block copies the error message 570 ** from the database handle into the statement and sets the statement 571 ** program counter to 0 to ensure that when the statement is 572 ** finalized or reset the parser error message is available via 573 ** sqlite3_errmsg() and sqlite3_errcode(). 574 */ 575 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 576 sqlite3DbFree(db, v->zErrMsg); 577 if( !db->mallocFailed ){ 578 v->zErrMsg = sqlite3DbStrDup(db, zErr); 579 } else { 580 v->zErrMsg = 0; 581 v->rc = SQLITE_NOMEM; 582 } 583 } 584 rc = sqlite3ApiExit(db, rc); 585 sqlite3_mutex_leave(db->mutex); 586 } 587 return rc; 588 } 589 #endif 590 591 /* 592 ** Extract the user data from a sqlite3_context structure and return a 593 ** pointer to it. 594 */ 595 void *sqlite3_user_data(sqlite3_context *p){ 596 assert( p && p->pFunc ); 597 return p->pFunc->pUserData; 598 } 599 600 /* 601 ** Extract the user data from a sqlite3_context structure and return a 602 ** pointer to it. 603 */ 604 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 605 assert( p && p->pFunc ); 606 return p->s.db; 607 } 608 609 /* 610 ** The following is the implementation of an SQL function that always 611 ** fails with an error message stating that the function is used in the 612 ** wrong context. The sqlite3_overload_function() API might construct 613 ** SQL function that use this routine so that the functions will exist 614 ** for name resolution but are actually overloaded by the xFindFunction 615 ** method of virtual tables. 616 */ 617 void sqlite3InvalidFunction( 618 sqlite3_context *context, /* The function calling context */ 619 int NotUsed, /* Number of arguments to the function */ 620 sqlite3_value **NotUsed2 /* Value of each argument */ 621 ){ 622 const char *zName = context->pFunc->zName; 623 char *zErr; 624 UNUSED_PARAMETER2(NotUsed, NotUsed2); 625 zErr = sqlite3_mprintf( 626 "unable to use function %s in the requested context", zName); 627 sqlite3_result_error(context, zErr, -1); 628 sqlite3_free(zErr); 629 } 630 631 /* 632 ** Allocate or return the aggregate context for a user function. A new 633 ** context is allocated on the first call. Subsequent calls return the 634 ** same context that was returned on prior calls. 635 */ 636 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 637 Mem *pMem; 638 assert( p && p->pFunc && p->pFunc->xStep ); 639 assert( sqlite3_mutex_held(p->s.db->mutex) ); 640 pMem = p->pMem; 641 if( (pMem->flags & MEM_Agg)==0 ){ 642 if( nByte==0 ){ 643 sqlite3VdbeMemReleaseExternal(pMem); 644 pMem->flags = MEM_Null; 645 pMem->z = 0; 646 }else{ 647 sqlite3VdbeMemGrow(pMem, nByte, 0); 648 pMem->flags = MEM_Agg; 649 pMem->u.pDef = p->pFunc; 650 if( pMem->z ){ 651 memset(pMem->z, 0, nByte); 652 } 653 } 654 } 655 return (void*)pMem->z; 656 } 657 658 /* 659 ** Return the auxilary data pointer, if any, for the iArg'th argument to 660 ** the user-function defined by pCtx. 661 */ 662 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 663 VdbeFunc *pVdbeFunc; 664 665 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 666 pVdbeFunc = pCtx->pVdbeFunc; 667 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 668 return 0; 669 } 670 return pVdbeFunc->apAux[iArg].pAux; 671 } 672 673 /* 674 ** Set the auxilary data pointer and delete function, for the iArg'th 675 ** argument to the user-function defined by pCtx. Any previous value is 676 ** deleted by calling the delete function specified when it was set. 677 */ 678 void sqlite3_set_auxdata( 679 sqlite3_context *pCtx, 680 int iArg, 681 void *pAux, 682 void (*xDelete)(void*) 683 ){ 684 struct AuxData *pAuxData; 685 VdbeFunc *pVdbeFunc; 686 if( iArg<0 ) goto failed; 687 688 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 689 pVdbeFunc = pCtx->pVdbeFunc; 690 if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 691 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); 692 int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 693 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); 694 if( !pVdbeFunc ){ 695 goto failed; 696 } 697 pCtx->pVdbeFunc = pVdbeFunc; 698 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); 699 pVdbeFunc->nAux = iArg+1; 700 pVdbeFunc->pFunc = pCtx->pFunc; 701 } 702 703 pAuxData = &pVdbeFunc->apAux[iArg]; 704 if( pAuxData->pAux && pAuxData->xDelete ){ 705 pAuxData->xDelete(pAuxData->pAux); 706 } 707 pAuxData->pAux = pAux; 708 pAuxData->xDelete = xDelete; 709 return; 710 711 failed: 712 if( xDelete ){ 713 xDelete(pAux); 714 } 715 } 716 717 #ifndef SQLITE_OMIT_DEPRECATED 718 /* 719 ** Return the number of times the Step function of a aggregate has been 720 ** called. 721 ** 722 ** This function is deprecated. Do not use it for new code. It is 723 ** provide only to avoid breaking legacy code. New aggregate function 724 ** implementations should keep their own counts within their aggregate 725 ** context. 726 */ 727 int sqlite3_aggregate_count(sqlite3_context *p){ 728 assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 729 return p->pMem->n; 730 } 731 #endif 732 733 /* 734 ** Return the number of columns in the result set for the statement pStmt. 735 */ 736 int sqlite3_column_count(sqlite3_stmt *pStmt){ 737 Vdbe *pVm = (Vdbe *)pStmt; 738 return pVm ? pVm->nResColumn : 0; 739 } 740 741 /* 742 ** Return the number of values available from the current row of the 743 ** currently executing statement pStmt. 744 */ 745 int sqlite3_data_count(sqlite3_stmt *pStmt){ 746 Vdbe *pVm = (Vdbe *)pStmt; 747 if( pVm==0 || pVm->pResultSet==0 ) return 0; 748 return pVm->nResColumn; 749 } 750 751 752 /* 753 ** Check to see if column iCol of the given statement is valid. If 754 ** it is, return a pointer to the Mem for the value of that column. 755 ** If iCol is not valid, return a pointer to a Mem which has a value 756 ** of NULL. 757 */ 758 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 759 Vdbe *pVm; 760 int vals; 761 Mem *pOut; 762 763 pVm = (Vdbe *)pStmt; 764 if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 765 sqlite3_mutex_enter(pVm->db->mutex); 766 vals = sqlite3_data_count(pStmt); 767 pOut = &pVm->pResultSet[i]; 768 }else{ 769 /* ((double)0) In case of SQLITE_OMIT_FLOATING_POINT... */ 770 static const Mem nullMem = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 }; 771 if( pVm && ALWAYS(pVm->db) ){ 772 sqlite3_mutex_enter(pVm->db->mutex); 773 sqlite3Error(pVm->db, SQLITE_RANGE, 0); 774 } 775 pOut = (Mem*)&nullMem; 776 } 777 return pOut; 778 } 779 780 /* 781 ** This function is called after invoking an sqlite3_value_XXX function on a 782 ** column value (i.e. a value returned by evaluating an SQL expression in the 783 ** select list of a SELECT statement) that may cause a malloc() failure. If 784 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 785 ** code of statement pStmt set to SQLITE_NOMEM. 786 ** 787 ** Specifically, this is called from within: 788 ** 789 ** sqlite3_column_int() 790 ** sqlite3_column_int64() 791 ** sqlite3_column_text() 792 ** sqlite3_column_text16() 793 ** sqlite3_column_real() 794 ** sqlite3_column_bytes() 795 ** sqlite3_column_bytes16() 796 ** 797 ** But not for sqlite3_column_blob(), which never calls malloc(). 798 */ 799 static void columnMallocFailure(sqlite3_stmt *pStmt) 800 { 801 /* If malloc() failed during an encoding conversion within an 802 ** sqlite3_column_XXX API, then set the return code of the statement to 803 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 804 ** and _finalize() will return NOMEM. 805 */ 806 Vdbe *p = (Vdbe *)pStmt; 807 if( p ){ 808 p->rc = sqlite3ApiExit(p->db, p->rc); 809 sqlite3_mutex_leave(p->db->mutex); 810 } 811 } 812 813 /**************************** sqlite3_column_ ******************************* 814 ** The following routines are used to access elements of the current row 815 ** in the result set. 816 */ 817 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 818 const void *val; 819 val = sqlite3_value_blob( columnMem(pStmt,i) ); 820 /* Even though there is no encoding conversion, value_blob() might 821 ** need to call malloc() to expand the result of a zeroblob() 822 ** expression. 823 */ 824 columnMallocFailure(pStmt); 825 return val; 826 } 827 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 828 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 829 columnMallocFailure(pStmt); 830 return val; 831 } 832 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 833 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 834 columnMallocFailure(pStmt); 835 return val; 836 } 837 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 838 double val = sqlite3_value_double( columnMem(pStmt,i) ); 839 columnMallocFailure(pStmt); 840 return val; 841 } 842 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 843 int val = sqlite3_value_int( columnMem(pStmt,i) ); 844 columnMallocFailure(pStmt); 845 return val; 846 } 847 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 848 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 849 columnMallocFailure(pStmt); 850 return val; 851 } 852 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 853 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 854 columnMallocFailure(pStmt); 855 return val; 856 } 857 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 858 Mem *pOut = columnMem(pStmt, i); 859 if( pOut->flags&MEM_Static ){ 860 pOut->flags &= ~MEM_Static; 861 pOut->flags |= MEM_Ephem; 862 } 863 columnMallocFailure(pStmt); 864 return (sqlite3_value *)pOut; 865 } 866 #ifndef SQLITE_OMIT_UTF16 867 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 868 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 869 columnMallocFailure(pStmt); 870 return val; 871 } 872 #endif /* SQLITE_OMIT_UTF16 */ 873 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 874 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 875 columnMallocFailure(pStmt); 876 return iType; 877 } 878 879 /* The following function is experimental and subject to change or 880 ** removal */ 881 /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 882 ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 883 **} 884 */ 885 886 /* 887 ** Convert the N-th element of pStmt->pColName[] into a string using 888 ** xFunc() then return that string. If N is out of range, return 0. 889 ** 890 ** There are up to 5 names for each column. useType determines which 891 ** name is returned. Here are the names: 892 ** 893 ** 0 The column name as it should be displayed for output 894 ** 1 The datatype name for the column 895 ** 2 The name of the database that the column derives from 896 ** 3 The name of the table that the column derives from 897 ** 4 The name of the table column that the result column derives from 898 ** 899 ** If the result is not a simple column reference (if it is an expression 900 ** or a constant) then useTypes 2, 3, and 4 return NULL. 901 */ 902 static const void *columnName( 903 sqlite3_stmt *pStmt, 904 int N, 905 const void *(*xFunc)(Mem*), 906 int useType 907 ){ 908 const void *ret = 0; 909 Vdbe *p = (Vdbe *)pStmt; 910 int n; 911 sqlite3 *db = p->db; 912 913 assert( db!=0 ); 914 n = sqlite3_column_count(pStmt); 915 if( N<n && N>=0 ){ 916 N += useType*n; 917 sqlite3_mutex_enter(db->mutex); 918 assert( db->mallocFailed==0 ); 919 ret = xFunc(&p->aColName[N]); 920 /* A malloc may have failed inside of the xFunc() call. If this 921 ** is the case, clear the mallocFailed flag and return NULL. 922 */ 923 if( db->mallocFailed ){ 924 db->mallocFailed = 0; 925 ret = 0; 926 } 927 sqlite3_mutex_leave(db->mutex); 928 } 929 return ret; 930 } 931 932 /* 933 ** Return the name of the Nth column of the result set returned by SQL 934 ** statement pStmt. 935 */ 936 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 937 return columnName( 938 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 939 } 940 #ifndef SQLITE_OMIT_UTF16 941 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 942 return columnName( 943 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 944 } 945 #endif 946 947 /* 948 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 949 ** not define OMIT_DECLTYPE. 950 */ 951 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 952 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 953 and SQLITE_ENABLE_COLUMN_METADATA" 954 #endif 955 956 #ifndef SQLITE_OMIT_DECLTYPE 957 /* 958 ** Return the column declaration type (if applicable) of the 'i'th column 959 ** of the result set of SQL statement pStmt. 960 */ 961 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 962 return columnName( 963 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 964 } 965 #ifndef SQLITE_OMIT_UTF16 966 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 967 return columnName( 968 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 969 } 970 #endif /* SQLITE_OMIT_UTF16 */ 971 #endif /* SQLITE_OMIT_DECLTYPE */ 972 973 #ifdef SQLITE_ENABLE_COLUMN_METADATA 974 /* 975 ** Return the name of the database from which a result column derives. 976 ** NULL is returned if the result column is an expression or constant or 977 ** anything else which is not an unabiguous reference to a database column. 978 */ 979 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 980 return columnName( 981 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 982 } 983 #ifndef SQLITE_OMIT_UTF16 984 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 985 return columnName( 986 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 987 } 988 #endif /* SQLITE_OMIT_UTF16 */ 989 990 /* 991 ** Return the name of the table from which a result column derives. 992 ** NULL is returned if the result column is an expression or constant or 993 ** anything else which is not an unabiguous reference to a database column. 994 */ 995 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 996 return columnName( 997 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 998 } 999 #ifndef SQLITE_OMIT_UTF16 1000 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1001 return columnName( 1002 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 1003 } 1004 #endif /* SQLITE_OMIT_UTF16 */ 1005 1006 /* 1007 ** Return the name of the table column from which a result column derives. 1008 ** NULL is returned if the result column is an expression or constant or 1009 ** anything else which is not an unabiguous reference to a database column. 1010 */ 1011 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1012 return columnName( 1013 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 1014 } 1015 #ifndef SQLITE_OMIT_UTF16 1016 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1017 return columnName( 1018 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 1019 } 1020 #endif /* SQLITE_OMIT_UTF16 */ 1021 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1022 1023 1024 /******************************* sqlite3_bind_ *************************** 1025 ** 1026 ** Routines used to attach values to wildcards in a compiled SQL statement. 1027 */ 1028 /* 1029 ** Unbind the value bound to variable i in virtual machine p. This is the 1030 ** the same as binding a NULL value to the column. If the "i" parameter is 1031 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 1032 ** 1033 ** A successful evaluation of this routine acquires the mutex on p. 1034 ** the mutex is released if any kind of error occurs. 1035 ** 1036 ** The error code stored in database p->db is overwritten with the return 1037 ** value in any case. 1038 */ 1039 static int vdbeUnbind(Vdbe *p, int i){ 1040 Mem *pVar; 1041 if( p==0 ) return SQLITE_MISUSE; 1042 sqlite3_mutex_enter(p->db->mutex); 1043 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 1044 sqlite3Error(p->db, SQLITE_MISUSE, 0); 1045 sqlite3_mutex_leave(p->db->mutex); 1046 return SQLITE_MISUSE; 1047 } 1048 if( i<1 || i>p->nVar ){ 1049 sqlite3Error(p->db, SQLITE_RANGE, 0); 1050 sqlite3_mutex_leave(p->db->mutex); 1051 return SQLITE_RANGE; 1052 } 1053 i--; 1054 pVar = &p->aVar[i]; 1055 sqlite3VdbeMemRelease(pVar); 1056 pVar->flags = MEM_Null; 1057 sqlite3Error(p->db, SQLITE_OK, 0); 1058 return SQLITE_OK; 1059 } 1060 1061 /* 1062 ** Bind a text or BLOB value. 1063 */ 1064 static int bindText( 1065 sqlite3_stmt *pStmt, /* The statement to bind against */ 1066 int i, /* Index of the parameter to bind */ 1067 const void *zData, /* Pointer to the data to be bound */ 1068 int nData, /* Number of bytes of data to be bound */ 1069 void (*xDel)(void*), /* Destructor for the data */ 1070 u8 encoding /* Encoding for the data */ 1071 ){ 1072 Vdbe *p = (Vdbe *)pStmt; 1073 Mem *pVar; 1074 int rc; 1075 1076 rc = vdbeUnbind(p, i); 1077 if( rc==SQLITE_OK ){ 1078 if( zData!=0 ){ 1079 pVar = &p->aVar[i-1]; 1080 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1081 if( rc==SQLITE_OK && encoding!=0 ){ 1082 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1083 } 1084 sqlite3Error(p->db, rc, 0); 1085 rc = sqlite3ApiExit(p->db, rc); 1086 } 1087 sqlite3_mutex_leave(p->db->mutex); 1088 } 1089 return rc; 1090 } 1091 1092 1093 /* 1094 ** Bind a blob value to an SQL statement variable. 1095 */ 1096 int sqlite3_bind_blob( 1097 sqlite3_stmt *pStmt, 1098 int i, 1099 const void *zData, 1100 int nData, 1101 void (*xDel)(void*) 1102 ){ 1103 return bindText(pStmt, i, zData, nData, xDel, 0); 1104 } 1105 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1106 int rc; 1107 Vdbe *p = (Vdbe *)pStmt; 1108 rc = vdbeUnbind(p, i); 1109 if( rc==SQLITE_OK ){ 1110 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1111 sqlite3_mutex_leave(p->db->mutex); 1112 } 1113 return rc; 1114 } 1115 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1116 return sqlite3_bind_int64(p, i, (i64)iValue); 1117 } 1118 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 1119 int rc; 1120 Vdbe *p = (Vdbe *)pStmt; 1121 rc = vdbeUnbind(p, i); 1122 if( rc==SQLITE_OK ){ 1123 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1124 sqlite3_mutex_leave(p->db->mutex); 1125 } 1126 return rc; 1127 } 1128 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1129 int rc; 1130 Vdbe *p = (Vdbe*)pStmt; 1131 rc = vdbeUnbind(p, i); 1132 if( rc==SQLITE_OK ){ 1133 sqlite3_mutex_leave(p->db->mutex); 1134 } 1135 return rc; 1136 } 1137 int sqlite3_bind_text( 1138 sqlite3_stmt *pStmt, 1139 int i, 1140 const char *zData, 1141 int nData, 1142 void (*xDel)(void*) 1143 ){ 1144 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1145 } 1146 #ifndef SQLITE_OMIT_UTF16 1147 int sqlite3_bind_text16( 1148 sqlite3_stmt *pStmt, 1149 int i, 1150 const void *zData, 1151 int nData, 1152 void (*xDel)(void*) 1153 ){ 1154 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1155 } 1156 #endif /* SQLITE_OMIT_UTF16 */ 1157 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1158 int rc; 1159 switch( pValue->type ){ 1160 case SQLITE_INTEGER: { 1161 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1162 break; 1163 } 1164 case SQLITE_FLOAT: { 1165 rc = sqlite3_bind_double(pStmt, i, pValue->r); 1166 break; 1167 } 1168 case SQLITE_BLOB: { 1169 if( pValue->flags & MEM_Zero ){ 1170 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1171 }else{ 1172 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1173 } 1174 break; 1175 } 1176 case SQLITE_TEXT: { 1177 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1178 pValue->enc); 1179 break; 1180 } 1181 default: { 1182 rc = sqlite3_bind_null(pStmt, i); 1183 break; 1184 } 1185 } 1186 return rc; 1187 } 1188 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1189 int rc; 1190 Vdbe *p = (Vdbe *)pStmt; 1191 rc = vdbeUnbind(p, i); 1192 if( rc==SQLITE_OK ){ 1193 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1194 sqlite3_mutex_leave(p->db->mutex); 1195 } 1196 return rc; 1197 } 1198 1199 /* 1200 ** Return the number of wildcards that can be potentially bound to. 1201 ** This routine is added to support DBD::SQLite. 1202 */ 1203 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1204 Vdbe *p = (Vdbe*)pStmt; 1205 return p ? p->nVar : 0; 1206 } 1207 1208 /* 1209 ** Create a mapping from variable numbers to variable names 1210 ** in the Vdbe.azVar[] array, if such a mapping does not already 1211 ** exist. 1212 */ 1213 static void createVarMap(Vdbe *p){ 1214 if( !p->okVar ){ 1215 int j; 1216 Op *pOp; 1217 sqlite3_mutex_enter(p->db->mutex); 1218 /* The race condition here is harmless. If two threads call this 1219 ** routine on the same Vdbe at the same time, they both might end 1220 ** up initializing the Vdbe.azVar[] array. That is a little extra 1221 ** work but it results in the same answer. 1222 */ 1223 for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 1224 if( pOp->opcode==OP_Variable ){ 1225 assert( pOp->p1>0 && pOp->p1<=p->nVar ); 1226 p->azVar[pOp->p1-1] = pOp->p4.z; 1227 } 1228 } 1229 p->okVar = 1; 1230 sqlite3_mutex_leave(p->db->mutex); 1231 } 1232 } 1233 1234 /* 1235 ** Return the name of a wildcard parameter. Return NULL if the index 1236 ** is out of range or if the wildcard is unnamed. 1237 ** 1238 ** The result is always UTF-8. 1239 */ 1240 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1241 Vdbe *p = (Vdbe*)pStmt; 1242 if( p==0 || i<1 || i>p->nVar ){ 1243 return 0; 1244 } 1245 createVarMap(p); 1246 return p->azVar[i-1]; 1247 } 1248 1249 /* 1250 ** Given a wildcard parameter name, return the index of the variable 1251 ** with that name. If there is no variable with the given name, 1252 ** return 0. 1253 */ 1254 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1255 Vdbe *p = (Vdbe*)pStmt; 1256 int i; 1257 if( p==0 ){ 1258 return 0; 1259 } 1260 createVarMap(p); 1261 if( zName ){ 1262 for(i=0; i<p->nVar; i++){ 1263 const char *z = p->azVar[i]; 1264 if( z && strcmp(z,zName)==0 ){ 1265 return i+1; 1266 } 1267 } 1268 } 1269 return 0; 1270 } 1271 1272 /* 1273 ** Transfer all bindings from the first statement over to the second. 1274 */ 1275 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1276 Vdbe *pFrom = (Vdbe*)pFromStmt; 1277 Vdbe *pTo = (Vdbe*)pToStmt; 1278 int i; 1279 assert( pTo->db==pFrom->db ); 1280 assert( pTo->nVar==pFrom->nVar ); 1281 sqlite3_mutex_enter(pTo->db->mutex); 1282 for(i=0; i<pFrom->nVar; i++){ 1283 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1284 } 1285 sqlite3_mutex_leave(pTo->db->mutex); 1286 return SQLITE_OK; 1287 } 1288 1289 #ifndef SQLITE_OMIT_DEPRECATED 1290 /* 1291 ** Deprecated external interface. Internal/core SQLite code 1292 ** should call sqlite3TransferBindings. 1293 ** 1294 ** Is is misuse to call this routine with statements from different 1295 ** database connections. But as this is a deprecated interface, we 1296 ** will not bother to check for that condition. 1297 ** 1298 ** If the two statements contain a different number of bindings, then 1299 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1300 ** SQLITE_OK is returned. 1301 */ 1302 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1303 Vdbe *pFrom = (Vdbe*)pFromStmt; 1304 Vdbe *pTo = (Vdbe*)pToStmt; 1305 if( pFrom->nVar!=pTo->nVar ){ 1306 return SQLITE_ERROR; 1307 } 1308 return sqlite3TransferBindings(pFromStmt, pToStmt); 1309 } 1310 #endif 1311 1312 /* 1313 ** Return the sqlite3* database handle to which the prepared statement given 1314 ** in the argument belongs. This is the same database handle that was 1315 ** the first argument to the sqlite3_prepare() that was used to create 1316 ** the statement in the first place. 1317 */ 1318 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 1319 return pStmt ? ((Vdbe*)pStmt)->db : 0; 1320 } 1321 1322 /* 1323 ** Return a pointer to the next prepared statement after pStmt associated 1324 ** with database connection pDb. If pStmt is NULL, return the first 1325 ** prepared statement for the database connection. Return NULL if there 1326 ** are no more. 1327 */ 1328 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1329 sqlite3_stmt *pNext; 1330 sqlite3_mutex_enter(pDb->mutex); 1331 if( pStmt==0 ){ 1332 pNext = (sqlite3_stmt*)pDb->pVdbe; 1333 }else{ 1334 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1335 } 1336 sqlite3_mutex_leave(pDb->mutex); 1337 return pNext; 1338 } 1339 1340 /* 1341 ** Return the value of a status counter for a prepared statement 1342 */ 1343 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1344 Vdbe *pVdbe = (Vdbe*)pStmt; 1345 int v = pVdbe->aCounter[op-1]; 1346 if( resetFlag ) pVdbe->aCounter[op-1] = 0; 1347 return v; 1348 } 1349