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