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 #ifndef SQLITE_OMIT_TRACE 57 /* 58 ** Invoke the profile callback. This routine is only called if we already 59 ** know that the profile callback is defined and needs to be invoked. 60 */ 61 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 62 sqlite3_int64 iNow; 63 sqlite3_int64 iElapse; 64 assert( p->startTime>0 ); 65 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 ); 66 assert( db->init.busy==0 ); 67 assert( p->zSql!=0 ); 68 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 69 iElapse = (iNow - p->startTime)*1000000; 70 #ifndef SQLITE_OMIT_DEPRECATED 71 if( db->xProfile ){ 72 db->xProfile(db->pProfileArg, p->zSql, iElapse); 73 } 74 #endif 75 if( db->mTrace & SQLITE_TRACE_PROFILE ){ 76 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 77 } 78 p->startTime = 0; 79 } 80 /* 81 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 82 ** is needed, and it invokes the callback if it is needed. 83 */ 84 # define checkProfileCallback(DB,P) \ 85 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 86 #else 87 # define checkProfileCallback(DB,P) /*no-op*/ 88 #endif 89 90 /* 91 ** The following routine destroys a virtual machine that is created by 92 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 93 ** success/failure code that describes the result of executing the virtual 94 ** machine. 95 ** 96 ** This routine sets the error code and string returned by 97 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 98 */ 99 int sqlite3_finalize(sqlite3_stmt *pStmt){ 100 int rc; 101 if( pStmt==0 ){ 102 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 103 ** pointer is a harmless no-op. */ 104 rc = SQLITE_OK; 105 }else{ 106 Vdbe *v = (Vdbe*)pStmt; 107 sqlite3 *db = v->db; 108 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 109 sqlite3_mutex_enter(db->mutex); 110 checkProfileCallback(db, v); 111 rc = sqlite3VdbeFinalize(v); 112 rc = sqlite3ApiExit(db, rc); 113 sqlite3LeaveMutexAndCloseZombie(db); 114 } 115 return rc; 116 } 117 118 /* 119 ** Terminate the current execution of an SQL statement and reset it 120 ** back to its starting state so that it can be reused. A success code from 121 ** the prior execution is returned. 122 ** 123 ** This routine sets the error code and string returned by 124 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 125 */ 126 int sqlite3_reset(sqlite3_stmt *pStmt){ 127 int rc; 128 if( pStmt==0 ){ 129 rc = SQLITE_OK; 130 }else{ 131 Vdbe *v = (Vdbe*)pStmt; 132 sqlite3 *db = v->db; 133 sqlite3_mutex_enter(db->mutex); 134 checkProfileCallback(db, v); 135 rc = sqlite3VdbeReset(v); 136 sqlite3VdbeRewind(v); 137 assert( (rc & (db->errMask))==rc ); 138 rc = sqlite3ApiExit(db, rc); 139 sqlite3_mutex_leave(db->mutex); 140 } 141 return rc; 142 } 143 144 /* 145 ** Set all the parameters in the compiled SQL statement to NULL. 146 */ 147 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 148 int i; 149 int rc = SQLITE_OK; 150 Vdbe *p = (Vdbe*)pStmt; 151 #if SQLITE_THREADSAFE 152 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 153 #endif 154 sqlite3_mutex_enter(mutex); 155 for(i=0; i<p->nVar; i++){ 156 sqlite3VdbeMemRelease(&p->aVar[i]); 157 p->aVar[i].flags = MEM_Null; 158 } 159 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 160 if( p->expmask ){ 161 p->expired = 1; 162 } 163 sqlite3_mutex_leave(mutex); 164 return rc; 165 } 166 167 168 /**************************** sqlite3_value_ ******************************* 169 ** The following routines extract information from a Mem or sqlite3_value 170 ** structure. 171 */ 172 const void *sqlite3_value_blob(sqlite3_value *pVal){ 173 Mem *p = (Mem*)pVal; 174 if( p->flags & (MEM_Blob|MEM_Str) ){ 175 if( ExpandBlob(p)!=SQLITE_OK ){ 176 assert( p->flags==MEM_Null && p->z==0 ); 177 return 0; 178 } 179 p->flags |= MEM_Blob; 180 return p->n ? p->z : 0; 181 }else{ 182 return sqlite3_value_text(pVal); 183 } 184 } 185 int sqlite3_value_bytes(sqlite3_value *pVal){ 186 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 187 } 188 int sqlite3_value_bytes16(sqlite3_value *pVal){ 189 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 190 } 191 double sqlite3_value_double(sqlite3_value *pVal){ 192 return sqlite3VdbeRealValue((Mem*)pVal); 193 } 194 int sqlite3_value_int(sqlite3_value *pVal){ 195 return (int)sqlite3VdbeIntValue((Mem*)pVal); 196 } 197 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 198 return sqlite3VdbeIntValue((Mem*)pVal); 199 } 200 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 201 Mem *pMem = (Mem*)pVal; 202 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 203 } 204 void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ 205 Mem *p = (Mem*)pVal; 206 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 207 (MEM_Null|MEM_Term|MEM_Subtype) 208 && zPType!=0 209 && p->eSubtype=='p' 210 && strcmp(p->u.zPType, zPType)==0 211 ){ 212 return (void*)p->z; 213 }else{ 214 return 0; 215 } 216 } 217 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 218 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 219 } 220 #ifndef SQLITE_OMIT_UTF16 221 const void *sqlite3_value_text16(sqlite3_value* pVal){ 222 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 223 } 224 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 225 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 226 } 227 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 228 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 229 } 230 #endif /* SQLITE_OMIT_UTF16 */ 231 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 232 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 233 ** point number string BLOB NULL 234 */ 235 int sqlite3_value_type(sqlite3_value* pVal){ 236 static const u8 aType[] = { 237 SQLITE_BLOB, /* 0x00 (not possible) */ 238 SQLITE_NULL, /* 0x01 NULL */ 239 SQLITE_TEXT, /* 0x02 TEXT */ 240 SQLITE_NULL, /* 0x03 (not possible) */ 241 SQLITE_INTEGER, /* 0x04 INTEGER */ 242 SQLITE_NULL, /* 0x05 (not possible) */ 243 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ 244 SQLITE_NULL, /* 0x07 (not possible) */ 245 SQLITE_FLOAT, /* 0x08 FLOAT */ 246 SQLITE_NULL, /* 0x09 (not possible) */ 247 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ 248 SQLITE_NULL, /* 0x0b (not possible) */ 249 SQLITE_INTEGER, /* 0x0c (not possible) */ 250 SQLITE_NULL, /* 0x0d (not possible) */ 251 SQLITE_INTEGER, /* 0x0e (not possible) */ 252 SQLITE_NULL, /* 0x0f (not possible) */ 253 SQLITE_BLOB, /* 0x10 BLOB */ 254 SQLITE_NULL, /* 0x11 (not possible) */ 255 SQLITE_TEXT, /* 0x12 (not possible) */ 256 SQLITE_NULL, /* 0x13 (not possible) */ 257 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ 258 SQLITE_NULL, /* 0x15 (not possible) */ 259 SQLITE_INTEGER, /* 0x16 (not possible) */ 260 SQLITE_NULL, /* 0x17 (not possible) */ 261 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ 262 SQLITE_NULL, /* 0x19 (not possible) */ 263 SQLITE_FLOAT, /* 0x1a (not possible) */ 264 SQLITE_NULL, /* 0x1b (not possible) */ 265 SQLITE_INTEGER, /* 0x1c (not possible) */ 266 SQLITE_NULL, /* 0x1d (not possible) */ 267 SQLITE_INTEGER, /* 0x1e (not possible) */ 268 SQLITE_NULL, /* 0x1f (not possible) */ 269 SQLITE_FLOAT, /* 0x20 INTREAL */ 270 SQLITE_NULL, /* 0x21 (not possible) */ 271 SQLITE_TEXT, /* 0x22 INTREAL + TEXT */ 272 SQLITE_NULL, /* 0x23 (not possible) */ 273 SQLITE_FLOAT, /* 0x24 (not possible) */ 274 SQLITE_NULL, /* 0x25 (not possible) */ 275 SQLITE_FLOAT, /* 0x26 (not possible) */ 276 SQLITE_NULL, /* 0x27 (not possible) */ 277 SQLITE_FLOAT, /* 0x28 (not possible) */ 278 SQLITE_NULL, /* 0x29 (not possible) */ 279 SQLITE_FLOAT, /* 0x2a (not possible) */ 280 SQLITE_NULL, /* 0x2b (not possible) */ 281 SQLITE_FLOAT, /* 0x2c (not possible) */ 282 SQLITE_NULL, /* 0x2d (not possible) */ 283 SQLITE_FLOAT, /* 0x2e (not possible) */ 284 SQLITE_NULL, /* 0x2f (not possible) */ 285 SQLITE_BLOB, /* 0x30 (not possible) */ 286 SQLITE_NULL, /* 0x31 (not possible) */ 287 SQLITE_TEXT, /* 0x32 (not possible) */ 288 SQLITE_NULL, /* 0x33 (not possible) */ 289 SQLITE_FLOAT, /* 0x34 (not possible) */ 290 SQLITE_NULL, /* 0x35 (not possible) */ 291 SQLITE_FLOAT, /* 0x36 (not possible) */ 292 SQLITE_NULL, /* 0x37 (not possible) */ 293 SQLITE_FLOAT, /* 0x38 (not possible) */ 294 SQLITE_NULL, /* 0x39 (not possible) */ 295 SQLITE_FLOAT, /* 0x3a (not possible) */ 296 SQLITE_NULL, /* 0x3b (not possible) */ 297 SQLITE_FLOAT, /* 0x3c (not possible) */ 298 SQLITE_NULL, /* 0x3d (not possible) */ 299 SQLITE_FLOAT, /* 0x3e (not possible) */ 300 SQLITE_NULL, /* 0x3f (not possible) */ 301 }; 302 #ifdef SQLITE_DEBUG 303 { 304 int eType = SQLITE_BLOB; 305 if( pVal->flags & MEM_Null ){ 306 eType = SQLITE_NULL; 307 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ 308 eType = SQLITE_FLOAT; 309 }else if( pVal->flags & MEM_Int ){ 310 eType = SQLITE_INTEGER; 311 }else if( pVal->flags & MEM_Str ){ 312 eType = SQLITE_TEXT; 313 } 314 assert( eType == aType[pVal->flags&MEM_AffMask] ); 315 } 316 #endif 317 return aType[pVal->flags&MEM_AffMask]; 318 } 319 320 /* Return true if a parameter to xUpdate represents an unchanged column */ 321 int sqlite3_value_nochange(sqlite3_value *pVal){ 322 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); 323 } 324 325 /* Return true if a parameter value originated from an sqlite3_bind() */ 326 int sqlite3_value_frombind(sqlite3_value *pVal){ 327 return (pVal->flags&MEM_FromBind)!=0; 328 } 329 330 /* Make a copy of an sqlite3_value object 331 */ 332 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 333 sqlite3_value *pNew; 334 if( pOrig==0 ) return 0; 335 pNew = sqlite3_malloc( sizeof(*pNew) ); 336 if( pNew==0 ) return 0; 337 memset(pNew, 0, sizeof(*pNew)); 338 memcpy(pNew, pOrig, MEMCELLSIZE); 339 pNew->flags &= ~MEM_Dyn; 340 pNew->db = 0; 341 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 342 pNew->flags &= ~(MEM_Static|MEM_Dyn); 343 pNew->flags |= MEM_Ephem; 344 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 345 sqlite3ValueFree(pNew); 346 pNew = 0; 347 } 348 }else if( pNew->flags & MEM_Null ){ 349 /* Do not duplicate pointer values */ 350 pNew->flags &= ~(MEM_Term|MEM_Subtype); 351 } 352 return pNew; 353 } 354 355 /* Destroy an sqlite3_value object previously obtained from 356 ** sqlite3_value_dup(). 357 */ 358 void sqlite3_value_free(sqlite3_value *pOld){ 359 sqlite3ValueFree(pOld); 360 } 361 362 363 /**************************** sqlite3_result_ ******************************* 364 ** The following routines are used by user-defined functions to specify 365 ** the function result. 366 ** 367 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 368 ** result as a string or blob. Appropriate errors are set if the string/blob 369 ** is too big or if an OOM occurs. 370 ** 371 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 372 ** on value P is not going to be used and need to be destroyed. 373 */ 374 static void setResultStrOrError( 375 sqlite3_context *pCtx, /* Function context */ 376 const char *z, /* String pointer */ 377 int n, /* Bytes in string, or negative */ 378 u8 enc, /* Encoding of z. 0 for BLOBs */ 379 void (*xDel)(void*) /* Destructor function */ 380 ){ 381 Mem *pOut = pCtx->pOut; 382 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); 383 if( rc ){ 384 if( rc==SQLITE_TOOBIG ){ 385 sqlite3_result_error_toobig(pCtx); 386 }else{ 387 /* The only errors possible from sqlite3VdbeMemSetStr are 388 ** SQLITE_TOOBIG and SQLITE_NOMEM */ 389 assert( rc==SQLITE_NOMEM ); 390 sqlite3_result_error_nomem(pCtx); 391 } 392 return; 393 } 394 if( pOut->enc!=ENC(pOut->db) ){ 395 sqlite3VdbeChangeEncoding(pOut, ENC(pOut->db)); 396 if( sqlite3VdbeMemTooBig(pOut) ){ 397 sqlite3_result_error_toobig(pCtx); 398 } 399 } 400 } 401 static int invokeValueDestructor( 402 const void *p, /* Value to destroy */ 403 void (*xDel)(void*), /* The destructor */ 404 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ 405 ){ 406 assert( xDel!=SQLITE_DYNAMIC ); 407 if( xDel==0 ){ 408 /* noop */ 409 }else if( xDel==SQLITE_TRANSIENT ){ 410 /* noop */ 411 }else{ 412 xDel((void*)p); 413 } 414 sqlite3_result_error_toobig(pCtx); 415 return SQLITE_TOOBIG; 416 } 417 void sqlite3_result_blob( 418 sqlite3_context *pCtx, 419 const void *z, 420 int n, 421 void (*xDel)(void *) 422 ){ 423 assert( n>=0 ); 424 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 425 setResultStrOrError(pCtx, z, n, 0, xDel); 426 } 427 void sqlite3_result_blob64( 428 sqlite3_context *pCtx, 429 const void *z, 430 sqlite3_uint64 n, 431 void (*xDel)(void *) 432 ){ 433 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 434 assert( xDel!=SQLITE_DYNAMIC ); 435 if( n>0x7fffffff ){ 436 (void)invokeValueDestructor(z, xDel, pCtx); 437 }else{ 438 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 439 } 440 } 441 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 442 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 443 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 444 } 445 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 446 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 447 pCtx->isError = SQLITE_ERROR; 448 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 449 } 450 #ifndef SQLITE_OMIT_UTF16 451 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 452 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 453 pCtx->isError = SQLITE_ERROR; 454 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 455 } 456 #endif 457 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 458 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 459 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 460 } 461 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 462 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 463 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 464 } 465 void sqlite3_result_null(sqlite3_context *pCtx){ 466 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 467 sqlite3VdbeMemSetNull(pCtx->pOut); 468 } 469 void sqlite3_result_pointer( 470 sqlite3_context *pCtx, 471 void *pPtr, 472 const char *zPType, 473 void (*xDestructor)(void*) 474 ){ 475 Mem *pOut = pCtx->pOut; 476 assert( sqlite3_mutex_held(pOut->db->mutex) ); 477 sqlite3VdbeMemRelease(pOut); 478 pOut->flags = MEM_Null; 479 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); 480 } 481 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 482 Mem *pOut = pCtx->pOut; 483 assert( sqlite3_mutex_held(pOut->db->mutex) ); 484 pOut->eSubtype = eSubtype & 0xff; 485 pOut->flags |= MEM_Subtype; 486 } 487 void sqlite3_result_text( 488 sqlite3_context *pCtx, 489 const char *z, 490 int n, 491 void (*xDel)(void *) 492 ){ 493 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 494 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 495 } 496 void sqlite3_result_text64( 497 sqlite3_context *pCtx, 498 const char *z, 499 sqlite3_uint64 n, 500 void (*xDel)(void *), 501 unsigned char enc 502 ){ 503 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 504 assert( xDel!=SQLITE_DYNAMIC ); 505 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 506 if( n>0x7fffffff ){ 507 (void)invokeValueDestructor(z, xDel, pCtx); 508 }else{ 509 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 510 } 511 } 512 #ifndef SQLITE_OMIT_UTF16 513 void sqlite3_result_text16( 514 sqlite3_context *pCtx, 515 const void *z, 516 int n, 517 void (*xDel)(void *) 518 ){ 519 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 520 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 521 } 522 void sqlite3_result_text16be( 523 sqlite3_context *pCtx, 524 const void *z, 525 int n, 526 void (*xDel)(void *) 527 ){ 528 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 529 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 530 } 531 void sqlite3_result_text16le( 532 sqlite3_context *pCtx, 533 const void *z, 534 int n, 535 void (*xDel)(void *) 536 ){ 537 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 538 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 539 } 540 #endif /* SQLITE_OMIT_UTF16 */ 541 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 542 Mem *pOut = pCtx->pOut; 543 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 544 sqlite3VdbeMemCopy(pOut, pValue); 545 sqlite3VdbeChangeEncoding(pOut, ENC(pOut->db)); 546 if( sqlite3VdbeMemTooBig(pOut) ){ 547 sqlite3_result_error_toobig(pCtx); 548 } 549 } 550 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 551 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); 552 } 553 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 554 Mem *pOut = pCtx->pOut; 555 assert( sqlite3_mutex_held(pOut->db->mutex) ); 556 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 557 sqlite3_result_error_toobig(pCtx); 558 return SQLITE_TOOBIG; 559 } 560 #ifndef SQLITE_OMIT_INCRBLOB 561 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 562 return SQLITE_OK; 563 #else 564 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 565 #endif 566 } 567 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 568 pCtx->isError = errCode ? errCode : -1; 569 #ifdef SQLITE_DEBUG 570 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 571 #endif 572 if( pCtx->pOut->flags & MEM_Null ){ 573 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, 574 SQLITE_STATIC); 575 } 576 } 577 578 /* Force an SQLITE_TOOBIG error. */ 579 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 580 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 581 pCtx->isError = SQLITE_TOOBIG; 582 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 583 SQLITE_UTF8, SQLITE_STATIC); 584 } 585 586 /* An SQLITE_NOMEM error. */ 587 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 588 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 589 sqlite3VdbeMemSetNull(pCtx->pOut); 590 pCtx->isError = SQLITE_NOMEM_BKPT; 591 sqlite3OomFault(pCtx->pOut->db); 592 } 593 594 #ifndef SQLITE_UNTESTABLE 595 /* Force the INT64 value currently stored as the result to be 596 ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL 597 ** test-control. 598 */ 599 void sqlite3ResultIntReal(sqlite3_context *pCtx){ 600 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 601 if( pCtx->pOut->flags & MEM_Int ){ 602 pCtx->pOut->flags &= ~MEM_Int; 603 pCtx->pOut->flags |= MEM_IntReal; 604 } 605 } 606 #endif 607 608 609 /* 610 ** This function is called after a transaction has been committed. It 611 ** invokes callbacks registered with sqlite3_wal_hook() as required. 612 */ 613 static int doWalCallbacks(sqlite3 *db){ 614 int rc = SQLITE_OK; 615 #ifndef SQLITE_OMIT_WAL 616 int i; 617 for(i=0; i<db->nDb; i++){ 618 Btree *pBt = db->aDb[i].pBt; 619 if( pBt ){ 620 int nEntry; 621 sqlite3BtreeEnter(pBt); 622 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 623 sqlite3BtreeLeave(pBt); 624 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ 625 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); 626 } 627 } 628 } 629 #endif 630 return rc; 631 } 632 633 634 /* 635 ** Execute the statement pStmt, either until a row of data is ready, the 636 ** statement is completely executed or an error occurs. 637 ** 638 ** This routine implements the bulk of the logic behind the sqlite_step() 639 ** API. The only thing omitted is the automatic recompile if a 640 ** schema change has occurred. That detail is handled by the 641 ** outer sqlite3_step() wrapper procedure. 642 */ 643 static int sqlite3Step(Vdbe *p){ 644 sqlite3 *db; 645 int rc; 646 647 assert(p); 648 if( p->iVdbeMagic!=VDBE_MAGIC_RUN ){ 649 /* We used to require that sqlite3_reset() be called before retrying 650 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 651 ** with version 3.7.0, we changed this so that sqlite3_reset() would 652 ** be called automatically instead of throwing the SQLITE_MISUSE error. 653 ** This "automatic-reset" change is not technically an incompatibility, 654 ** since any application that receives an SQLITE_MISUSE is broken by 655 ** definition. 656 ** 657 ** Nevertheless, some published applications that were originally written 658 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 659 ** returns, and those were broken by the automatic-reset change. As a 660 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 661 ** legacy behavior of returning SQLITE_MISUSE for cases where the 662 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 663 ** or SQLITE_BUSY error. 664 */ 665 #ifdef SQLITE_OMIT_AUTORESET 666 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 667 sqlite3_reset((sqlite3_stmt*)p); 668 }else{ 669 return SQLITE_MISUSE_BKPT; 670 } 671 #else 672 sqlite3_reset((sqlite3_stmt*)p); 673 #endif 674 } 675 676 /* Check that malloc() has not failed. If it has, return early. */ 677 db = p->db; 678 if( db->mallocFailed ){ 679 p->rc = SQLITE_NOMEM; 680 return SQLITE_NOMEM_BKPT; 681 } 682 683 if( p->pc<0 && p->expired ){ 684 p->rc = SQLITE_SCHEMA; 685 rc = SQLITE_ERROR; 686 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 687 /* If this statement was prepared using saved SQL and an 688 ** error has occurred, then return the error code in p->rc to the 689 ** caller. Set the error code in the database handle to the same value. 690 */ 691 rc = sqlite3VdbeTransferError(p); 692 } 693 goto end_of_step; 694 } 695 if( p->pc<0 ){ 696 /* If there are no other statements currently running, then 697 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 698 ** from interrupting a statement that has not yet started. 699 */ 700 if( db->nVdbeActive==0 ){ 701 AtomicStore(&db->u1.isInterrupted, 0); 702 } 703 704 assert( db->nVdbeWrite>0 || db->autoCommit==0 705 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 706 ); 707 708 #ifndef SQLITE_OMIT_TRACE 709 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 710 && !db->init.busy && p->zSql ){ 711 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 712 }else{ 713 assert( p->startTime==0 ); 714 } 715 #endif 716 717 db->nVdbeActive++; 718 if( p->readOnly==0 ) db->nVdbeWrite++; 719 if( p->bIsReader ) db->nVdbeRead++; 720 p->pc = 0; 721 } 722 #ifdef SQLITE_DEBUG 723 p->rcApp = SQLITE_OK; 724 #endif 725 #ifndef SQLITE_OMIT_EXPLAIN 726 if( p->explain ){ 727 rc = sqlite3VdbeList(p); 728 }else 729 #endif /* SQLITE_OMIT_EXPLAIN */ 730 { 731 db->nVdbeExec++; 732 rc = sqlite3VdbeExec(p); 733 db->nVdbeExec--; 734 } 735 736 if( rc!=SQLITE_ROW ){ 737 #ifndef SQLITE_OMIT_TRACE 738 /* If the statement completed successfully, invoke the profile callback */ 739 checkProfileCallback(db, p); 740 #endif 741 742 if( rc==SQLITE_DONE && db->autoCommit ){ 743 assert( p->rc==SQLITE_OK ); 744 p->rc = doWalCallbacks(db); 745 if( p->rc!=SQLITE_OK ){ 746 rc = SQLITE_ERROR; 747 } 748 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 749 /* If this statement was prepared using saved SQL and an 750 ** error has occurred, then return the error code in p->rc to the 751 ** caller. Set the error code in the database handle to the same value. 752 */ 753 rc = sqlite3VdbeTransferError(p); 754 } 755 } 756 757 db->errCode = rc; 758 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 759 p->rc = SQLITE_NOMEM_BKPT; 760 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; 761 } 762 end_of_step: 763 /* There are only a limited number of result codes allowed from the 764 ** statements prepared using the legacy sqlite3_prepare() interface */ 765 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 766 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 767 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 768 ); 769 return (rc&db->errMask); 770 } 771 772 /* 773 ** This is the top-level implementation of sqlite3_step(). Call 774 ** sqlite3Step() to do most of the work. If a schema error occurs, 775 ** call sqlite3Reprepare() and try again. 776 */ 777 int sqlite3_step(sqlite3_stmt *pStmt){ 778 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 779 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 780 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 781 sqlite3 *db; /* The database connection */ 782 783 if( vdbeSafetyNotNull(v) ){ 784 return SQLITE_MISUSE_BKPT; 785 } 786 db = v->db; 787 sqlite3_mutex_enter(db->mutex); 788 v->doingRerun = 0; 789 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 790 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 791 int savedPc = v->pc; 792 rc = sqlite3Reprepare(v); 793 if( rc!=SQLITE_OK ){ 794 /* This case occurs after failing to recompile an sql statement. 795 ** The error message from the SQL compiler has already been loaded 796 ** into the database handle. This block copies the error message 797 ** from the database handle into the statement and sets the statement 798 ** program counter to 0 to ensure that when the statement is 799 ** finalized or reset the parser error message is available via 800 ** sqlite3_errmsg() and sqlite3_errcode(). 801 */ 802 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 803 sqlite3DbFree(db, v->zErrMsg); 804 if( !db->mallocFailed ){ 805 v->zErrMsg = sqlite3DbStrDup(db, zErr); 806 v->rc = rc = sqlite3ApiExit(db, rc); 807 } else { 808 v->zErrMsg = 0; 809 v->rc = rc = SQLITE_NOMEM_BKPT; 810 } 811 break; 812 } 813 sqlite3_reset(pStmt); 814 if( savedPc>=0 ) v->doingRerun = 1; 815 assert( v->expired==0 ); 816 } 817 sqlite3_mutex_leave(db->mutex); 818 return rc; 819 } 820 821 822 /* 823 ** Extract the user data from a sqlite3_context structure and return a 824 ** pointer to it. 825 */ 826 void *sqlite3_user_data(sqlite3_context *p){ 827 assert( p && p->pFunc ); 828 return p->pFunc->pUserData; 829 } 830 831 /* 832 ** Extract the user data from a sqlite3_context structure and return a 833 ** pointer to it. 834 ** 835 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 836 ** returns a copy of the pointer to the database connection (the 1st 837 ** parameter) of the sqlite3_create_function() and 838 ** sqlite3_create_function16() routines that originally registered the 839 ** application defined function. 840 */ 841 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 842 assert( p && p->pOut ); 843 return p->pOut->db; 844 } 845 846 /* 847 ** If this routine is invoked from within an xColumn method of a virtual 848 ** table, then it returns true if and only if the the call is during an 849 ** UPDATE operation and the value of the column will not be modified 850 ** by the UPDATE. 851 ** 852 ** If this routine is called from any context other than within the 853 ** xColumn method of a virtual table, then the return value is meaningless 854 ** and arbitrary. 855 ** 856 ** Virtual table implements might use this routine to optimize their 857 ** performance by substituting a NULL result, or some other light-weight 858 ** value, as a signal to the xUpdate routine that the column is unchanged. 859 */ 860 int sqlite3_vtab_nochange(sqlite3_context *p){ 861 assert( p ); 862 return sqlite3_value_nochange(p->pOut); 863 } 864 865 /* 866 ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and 867 ** sqlite3_vtab_in_next() (if bNext!=0). 868 */ 869 static int valueFromValueList( 870 sqlite3_value *pVal, /* Pointer to the ValueList object */ 871 sqlite3_value **ppOut, /* Store the next value from the list here */ 872 int bNext /* 1 for _next(). 0 for _first() */ 873 ){ 874 int rc; 875 ValueList *pRhs; 876 877 *ppOut = 0; 878 if( pVal==0 ) return SQLITE_MISUSE; 879 pRhs = (ValueList*)sqlite3_value_pointer(pVal, "ValueList"); 880 if( pRhs==0 ) return SQLITE_MISUSE; 881 if( bNext ){ 882 rc = sqlite3BtreeNext(pRhs->pCsr, 0); 883 }else{ 884 int dummy = 0; 885 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); 886 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); 887 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; 888 } 889 if( rc==SQLITE_OK ){ 890 u32 sz; /* Size of current row in bytes */ 891 Mem sMem; /* Raw content of current row */ 892 memset(&sMem, 0, sizeof(sMem)); 893 sz = sqlite3BtreePayloadSize(pRhs->pCsr); 894 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); 895 if( rc==SQLITE_OK ){ 896 u8 *zBuf = (u8*)sMem.z; 897 u32 iSerial; 898 sqlite3_value *pOut = pRhs->pOut; 899 int iOff = 1 + getVarint32(&zBuf[1], iSerial); 900 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); 901 pOut->enc = ENC(pOut->db); 902 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ 903 rc = SQLITE_NOMEM; 904 }else{ 905 *ppOut = pOut; 906 } 907 } 908 sqlite3VdbeMemRelease(&sMem); 909 } 910 return rc; 911 } 912 913 /* 914 ** Set the iterator value pVal to point to the first value in the set. 915 ** Set (*ppOut) to point to this value before returning. 916 */ 917 int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ 918 return valueFromValueList(pVal, ppOut, 0); 919 } 920 921 /* 922 ** Set the iterator value pVal to point to the next value in the set. 923 ** Set (*ppOut) to point to this value before returning. 924 */ 925 int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ 926 return valueFromValueList(pVal, ppOut, 1); 927 } 928 929 /* 930 ** Return the current time for a statement. If the current time 931 ** is requested more than once within the same run of a single prepared 932 ** statement, the exact same time is returned for each invocation regardless 933 ** of the amount of time that elapses between invocations. In other words, 934 ** the time returned is always the time of the first call. 935 */ 936 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 937 int rc; 938 #ifndef SQLITE_ENABLE_STAT4 939 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 940 assert( p->pVdbe!=0 ); 941 #else 942 sqlite3_int64 iTime = 0; 943 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 944 #endif 945 if( *piTime==0 ){ 946 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 947 if( rc ) *piTime = 0; 948 } 949 return *piTime; 950 } 951 952 /* 953 ** Create a new aggregate context for p and return a pointer to 954 ** its pMem->z element. 955 */ 956 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 957 Mem *pMem = p->pMem; 958 assert( (pMem->flags & MEM_Agg)==0 ); 959 if( nByte<=0 ){ 960 sqlite3VdbeMemSetNull(pMem); 961 pMem->z = 0; 962 }else{ 963 sqlite3VdbeMemClearAndResize(pMem, nByte); 964 pMem->flags = MEM_Agg; 965 pMem->u.pDef = p->pFunc; 966 if( pMem->z ){ 967 memset(pMem->z, 0, nByte); 968 } 969 } 970 return (void*)pMem->z; 971 } 972 973 /* 974 ** Allocate or return the aggregate context for a user function. A new 975 ** context is allocated on the first call. Subsequent calls return the 976 ** same context that was returned on prior calls. 977 */ 978 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 979 assert( p && p->pFunc && p->pFunc->xFinalize ); 980 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 981 testcase( nByte<0 ); 982 if( (p->pMem->flags & MEM_Agg)==0 ){ 983 return createAggContext(p, nByte); 984 }else{ 985 return (void*)p->pMem->z; 986 } 987 } 988 989 /* 990 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 991 ** the user-function defined by pCtx. 992 ** 993 ** The left-most argument is 0. 994 ** 995 ** Undocumented behavior: If iArg is negative then access a cache of 996 ** auxiliary data pointers that is available to all functions within a 997 ** single prepared statement. The iArg values must match. 998 */ 999 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 1000 AuxData *pAuxData; 1001 1002 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 1003 #if SQLITE_ENABLE_STAT4 1004 if( pCtx->pVdbe==0 ) return 0; 1005 #else 1006 assert( pCtx->pVdbe!=0 ); 1007 #endif 1008 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 1009 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 1010 return pAuxData->pAux; 1011 } 1012 } 1013 return 0; 1014 } 1015 1016 /* 1017 ** Set the auxiliary data pointer and delete function, for the iArg'th 1018 ** argument to the user-function defined by pCtx. Any previous value is 1019 ** deleted by calling the delete function specified when it was set. 1020 ** 1021 ** The left-most argument is 0. 1022 ** 1023 ** Undocumented behavior: If iArg is negative then make the data available 1024 ** to all functions within the current prepared statement using iArg as an 1025 ** access code. 1026 */ 1027 void sqlite3_set_auxdata( 1028 sqlite3_context *pCtx, 1029 int iArg, 1030 void *pAux, 1031 void (*xDelete)(void*) 1032 ){ 1033 AuxData *pAuxData; 1034 Vdbe *pVdbe = pCtx->pVdbe; 1035 1036 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 1037 #ifdef SQLITE_ENABLE_STAT4 1038 if( pVdbe==0 ) goto failed; 1039 #else 1040 assert( pVdbe!=0 ); 1041 #endif 1042 1043 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 1044 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 1045 break; 1046 } 1047 } 1048 if( pAuxData==0 ){ 1049 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 1050 if( !pAuxData ) goto failed; 1051 pAuxData->iAuxOp = pCtx->iOp; 1052 pAuxData->iAuxArg = iArg; 1053 pAuxData->pNextAux = pVdbe->pAuxData; 1054 pVdbe->pAuxData = pAuxData; 1055 if( pCtx->isError==0 ) pCtx->isError = -1; 1056 }else if( pAuxData->xDeleteAux ){ 1057 pAuxData->xDeleteAux(pAuxData->pAux); 1058 } 1059 1060 pAuxData->pAux = pAux; 1061 pAuxData->xDeleteAux = xDelete; 1062 return; 1063 1064 failed: 1065 if( xDelete ){ 1066 xDelete(pAux); 1067 } 1068 } 1069 1070 #ifndef SQLITE_OMIT_DEPRECATED 1071 /* 1072 ** Return the number of times the Step function of an aggregate has been 1073 ** called. 1074 ** 1075 ** This function is deprecated. Do not use it for new code. It is 1076 ** provide only to avoid breaking legacy code. New aggregate function 1077 ** implementations should keep their own counts within their aggregate 1078 ** context. 1079 */ 1080 int sqlite3_aggregate_count(sqlite3_context *p){ 1081 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 1082 return p->pMem->n; 1083 } 1084 #endif 1085 1086 /* 1087 ** Return the number of columns in the result set for the statement pStmt. 1088 */ 1089 int sqlite3_column_count(sqlite3_stmt *pStmt){ 1090 Vdbe *pVm = (Vdbe *)pStmt; 1091 return pVm ? pVm->nResColumn : 0; 1092 } 1093 1094 /* 1095 ** Return the number of values available from the current row of the 1096 ** currently executing statement pStmt. 1097 */ 1098 int sqlite3_data_count(sqlite3_stmt *pStmt){ 1099 Vdbe *pVm = (Vdbe *)pStmt; 1100 if( pVm==0 || pVm->pResultSet==0 ) return 0; 1101 return pVm->nResColumn; 1102 } 1103 1104 /* 1105 ** Return a pointer to static memory containing an SQL NULL value. 1106 */ 1107 static const Mem *columnNullValue(void){ 1108 /* Even though the Mem structure contains an element 1109 ** of type i64, on certain architectures (x86) with certain compiler 1110 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 1111 ** instead of an 8-byte one. This all works fine, except that when 1112 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 1113 ** that a Mem structure is located on an 8-byte boundary. To prevent 1114 ** these assert()s from failing, when building with SQLITE_DEBUG defined 1115 ** using gcc, we force nullMem to be 8-byte aligned using the magical 1116 ** __attribute__((aligned(8))) macro. */ 1117 static const Mem nullMem 1118 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 1119 __attribute__((aligned(8))) 1120 #endif 1121 = { 1122 /* .u = */ {0}, 1123 /* .z = */ (char*)0, 1124 /* .n = */ (int)0, 1125 /* .flags = */ (u16)MEM_Null, 1126 /* .enc = */ (u8)0, 1127 /* .eSubtype = */ (u8)0, 1128 /* .db = */ (sqlite3*)0, 1129 /* .szMalloc = */ (int)0, 1130 /* .uTemp = */ (u32)0, 1131 /* .zMalloc = */ (char*)0, 1132 /* .xDel = */ (void(*)(void*))0, 1133 #ifdef SQLITE_DEBUG 1134 /* .pScopyFrom = */ (Mem*)0, 1135 /* .mScopyFlags= */ 0, 1136 #endif 1137 }; 1138 return &nullMem; 1139 } 1140 1141 /* 1142 ** Check to see if column iCol of the given statement is valid. If 1143 ** it is, return a pointer to the Mem for the value of that column. 1144 ** If iCol is not valid, return a pointer to a Mem which has a value 1145 ** of NULL. 1146 */ 1147 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 1148 Vdbe *pVm; 1149 Mem *pOut; 1150 1151 pVm = (Vdbe *)pStmt; 1152 if( pVm==0 ) return (Mem*)columnNullValue(); 1153 assert( pVm->db ); 1154 sqlite3_mutex_enter(pVm->db->mutex); 1155 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 1156 pOut = &pVm->pResultSet[i]; 1157 }else{ 1158 sqlite3Error(pVm->db, SQLITE_RANGE); 1159 pOut = (Mem*)columnNullValue(); 1160 } 1161 return pOut; 1162 } 1163 1164 /* 1165 ** This function is called after invoking an sqlite3_value_XXX function on a 1166 ** column value (i.e. a value returned by evaluating an SQL expression in the 1167 ** select list of a SELECT statement) that may cause a malloc() failure. If 1168 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 1169 ** code of statement pStmt set to SQLITE_NOMEM. 1170 ** 1171 ** Specifically, this is called from within: 1172 ** 1173 ** sqlite3_column_int() 1174 ** sqlite3_column_int64() 1175 ** sqlite3_column_text() 1176 ** sqlite3_column_text16() 1177 ** sqlite3_column_real() 1178 ** sqlite3_column_bytes() 1179 ** sqlite3_column_bytes16() 1180 ** sqiite3_column_blob() 1181 */ 1182 static void columnMallocFailure(sqlite3_stmt *pStmt) 1183 { 1184 /* If malloc() failed during an encoding conversion within an 1185 ** sqlite3_column_XXX API, then set the return code of the statement to 1186 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 1187 ** and _finalize() will return NOMEM. 1188 */ 1189 Vdbe *p = (Vdbe *)pStmt; 1190 if( p ){ 1191 assert( p->db!=0 ); 1192 assert( sqlite3_mutex_held(p->db->mutex) ); 1193 p->rc = sqlite3ApiExit(p->db, p->rc); 1194 sqlite3_mutex_leave(p->db->mutex); 1195 } 1196 } 1197 1198 /**************************** sqlite3_column_ ******************************* 1199 ** The following routines are used to access elements of the current row 1200 ** in the result set. 1201 */ 1202 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 1203 const void *val; 1204 val = sqlite3_value_blob( columnMem(pStmt,i) ); 1205 /* Even though there is no encoding conversion, value_blob() might 1206 ** need to call malloc() to expand the result of a zeroblob() 1207 ** expression. 1208 */ 1209 columnMallocFailure(pStmt); 1210 return val; 1211 } 1212 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 1213 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 1214 columnMallocFailure(pStmt); 1215 return val; 1216 } 1217 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 1218 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 1219 columnMallocFailure(pStmt); 1220 return val; 1221 } 1222 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 1223 double val = sqlite3_value_double( columnMem(pStmt,i) ); 1224 columnMallocFailure(pStmt); 1225 return val; 1226 } 1227 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 1228 int val = sqlite3_value_int( columnMem(pStmt,i) ); 1229 columnMallocFailure(pStmt); 1230 return val; 1231 } 1232 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 1233 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 1234 columnMallocFailure(pStmt); 1235 return val; 1236 } 1237 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 1238 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 1239 columnMallocFailure(pStmt); 1240 return val; 1241 } 1242 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 1243 Mem *pOut = columnMem(pStmt, i); 1244 if( pOut->flags&MEM_Static ){ 1245 pOut->flags &= ~MEM_Static; 1246 pOut->flags |= MEM_Ephem; 1247 } 1248 columnMallocFailure(pStmt); 1249 return (sqlite3_value *)pOut; 1250 } 1251 #ifndef SQLITE_OMIT_UTF16 1252 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 1253 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 1254 columnMallocFailure(pStmt); 1255 return val; 1256 } 1257 #endif /* SQLITE_OMIT_UTF16 */ 1258 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 1259 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 1260 columnMallocFailure(pStmt); 1261 return iType; 1262 } 1263 1264 /* 1265 ** Convert the N-th element of pStmt->pColName[] into a string using 1266 ** xFunc() then return that string. If N is out of range, return 0. 1267 ** 1268 ** There are up to 5 names for each column. useType determines which 1269 ** name is returned. Here are the names: 1270 ** 1271 ** 0 The column name as it should be displayed for output 1272 ** 1 The datatype name for the column 1273 ** 2 The name of the database that the column derives from 1274 ** 3 The name of the table that the column derives from 1275 ** 4 The name of the table column that the result column derives from 1276 ** 1277 ** If the result is not a simple column reference (if it is an expression 1278 ** or a constant) then useTypes 2, 3, and 4 return NULL. 1279 */ 1280 static const void *columnName( 1281 sqlite3_stmt *pStmt, /* The statement */ 1282 int N, /* Which column to get the name for */ 1283 int useUtf16, /* True to return the name as UTF16 */ 1284 int useType /* What type of name */ 1285 ){ 1286 const void *ret; 1287 Vdbe *p; 1288 int n; 1289 sqlite3 *db; 1290 #ifdef SQLITE_ENABLE_API_ARMOR 1291 if( pStmt==0 ){ 1292 (void)SQLITE_MISUSE_BKPT; 1293 return 0; 1294 } 1295 #endif 1296 ret = 0; 1297 p = (Vdbe *)pStmt; 1298 db = p->db; 1299 assert( db!=0 ); 1300 n = sqlite3_column_count(pStmt); 1301 if( N<n && N>=0 ){ 1302 N += useType*n; 1303 sqlite3_mutex_enter(db->mutex); 1304 assert( db->mallocFailed==0 ); 1305 #ifndef SQLITE_OMIT_UTF16 1306 if( useUtf16 ){ 1307 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); 1308 }else 1309 #endif 1310 { 1311 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); 1312 } 1313 /* A malloc may have failed inside of the _text() call. If this 1314 ** is the case, clear the mallocFailed flag and return NULL. 1315 */ 1316 if( db->mallocFailed ){ 1317 sqlite3OomClear(db); 1318 ret = 0; 1319 } 1320 sqlite3_mutex_leave(db->mutex); 1321 } 1322 return ret; 1323 } 1324 1325 /* 1326 ** Return the name of the Nth column of the result set returned by SQL 1327 ** statement pStmt. 1328 */ 1329 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 1330 return columnName(pStmt, N, 0, COLNAME_NAME); 1331 } 1332 #ifndef SQLITE_OMIT_UTF16 1333 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 1334 return columnName(pStmt, N, 1, COLNAME_NAME); 1335 } 1336 #endif 1337 1338 /* 1339 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 1340 ** not define OMIT_DECLTYPE. 1341 */ 1342 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 1343 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 1344 and SQLITE_ENABLE_COLUMN_METADATA" 1345 #endif 1346 1347 #ifndef SQLITE_OMIT_DECLTYPE 1348 /* 1349 ** Return the column declaration type (if applicable) of the 'i'th column 1350 ** of the result set of SQL statement pStmt. 1351 */ 1352 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 1353 return columnName(pStmt, N, 0, COLNAME_DECLTYPE); 1354 } 1355 #ifndef SQLITE_OMIT_UTF16 1356 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 1357 return columnName(pStmt, N, 1, COLNAME_DECLTYPE); 1358 } 1359 #endif /* SQLITE_OMIT_UTF16 */ 1360 #endif /* SQLITE_OMIT_DECLTYPE */ 1361 1362 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1363 /* 1364 ** Return the name of the database from which a result column derives. 1365 ** NULL is returned if the result column is an expression or constant or 1366 ** anything else which is not an unambiguous reference to a database column. 1367 */ 1368 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1369 return columnName(pStmt, N, 0, COLNAME_DATABASE); 1370 } 1371 #ifndef SQLITE_OMIT_UTF16 1372 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1373 return columnName(pStmt, N, 1, COLNAME_DATABASE); 1374 } 1375 #endif /* SQLITE_OMIT_UTF16 */ 1376 1377 /* 1378 ** Return the name of the table from which a result column derives. 1379 ** NULL is returned if the result column is an expression or constant or 1380 ** anything else which is not an unambiguous reference to a database column. 1381 */ 1382 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1383 return columnName(pStmt, N, 0, COLNAME_TABLE); 1384 } 1385 #ifndef SQLITE_OMIT_UTF16 1386 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1387 return columnName(pStmt, N, 1, COLNAME_TABLE); 1388 } 1389 #endif /* SQLITE_OMIT_UTF16 */ 1390 1391 /* 1392 ** Return the name of the table column from which a result column derives. 1393 ** NULL is returned if the result column is an expression or constant or 1394 ** anything else which is not an unambiguous reference to a database column. 1395 */ 1396 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1397 return columnName(pStmt, N, 0, COLNAME_COLUMN); 1398 } 1399 #ifndef SQLITE_OMIT_UTF16 1400 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1401 return columnName(pStmt, N, 1, COLNAME_COLUMN); 1402 } 1403 #endif /* SQLITE_OMIT_UTF16 */ 1404 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1405 1406 1407 /******************************* sqlite3_bind_ *************************** 1408 ** 1409 ** Routines used to attach values to wildcards in a compiled SQL statement. 1410 */ 1411 /* 1412 ** Unbind the value bound to variable i in virtual machine p. This is the 1413 ** the same as binding a NULL value to the column. If the "i" parameter is 1414 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 1415 ** 1416 ** A successful evaluation of this routine acquires the mutex on p. 1417 ** the mutex is released if any kind of error occurs. 1418 ** 1419 ** The error code stored in database p->db is overwritten with the return 1420 ** value in any case. 1421 */ 1422 static int vdbeUnbind(Vdbe *p, int i){ 1423 Mem *pVar; 1424 if( vdbeSafetyNotNull(p) ){ 1425 return SQLITE_MISUSE_BKPT; 1426 } 1427 sqlite3_mutex_enter(p->db->mutex); 1428 if( p->iVdbeMagic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 1429 sqlite3Error(p->db, SQLITE_MISUSE); 1430 sqlite3_mutex_leave(p->db->mutex); 1431 sqlite3_log(SQLITE_MISUSE, 1432 "bind on a busy prepared statement: [%s]", p->zSql); 1433 return SQLITE_MISUSE_BKPT; 1434 } 1435 if( i<1 || i>p->nVar ){ 1436 sqlite3Error(p->db, SQLITE_RANGE); 1437 sqlite3_mutex_leave(p->db->mutex); 1438 return SQLITE_RANGE; 1439 } 1440 i--; 1441 pVar = &p->aVar[i]; 1442 sqlite3VdbeMemRelease(pVar); 1443 pVar->flags = MEM_Null; 1444 p->db->errCode = SQLITE_OK; 1445 1446 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 1447 ** binding a new value to this variable invalidates the current query plan. 1448 ** 1449 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host 1450 ** parameter in the WHERE clause might influence the choice of query plan 1451 ** for a statement, then the statement will be automatically recompiled, 1452 ** as if there had been a schema change, on the first sqlite3_step() call 1453 ** following any change to the bindings of that parameter. 1454 */ 1455 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 1456 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ 1457 p->expired = 1; 1458 } 1459 return SQLITE_OK; 1460 } 1461 1462 /* 1463 ** Bind a text or BLOB value. 1464 */ 1465 static int bindText( 1466 sqlite3_stmt *pStmt, /* The statement to bind against */ 1467 int i, /* Index of the parameter to bind */ 1468 const void *zData, /* Pointer to the data to be bound */ 1469 i64 nData, /* Number of bytes of data to be bound */ 1470 void (*xDel)(void*), /* Destructor for the data */ 1471 u8 encoding /* Encoding for the data */ 1472 ){ 1473 Vdbe *p = (Vdbe *)pStmt; 1474 Mem *pVar; 1475 int rc; 1476 1477 rc = vdbeUnbind(p, i); 1478 if( rc==SQLITE_OK ){ 1479 if( zData!=0 ){ 1480 pVar = &p->aVar[i-1]; 1481 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1482 if( rc==SQLITE_OK && encoding!=0 ){ 1483 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1484 } 1485 if( rc ){ 1486 sqlite3Error(p->db, rc); 1487 rc = sqlite3ApiExit(p->db, rc); 1488 } 1489 } 1490 sqlite3_mutex_leave(p->db->mutex); 1491 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 1492 xDel((void*)zData); 1493 } 1494 return rc; 1495 } 1496 1497 1498 /* 1499 ** Bind a blob value to an SQL statement variable. 1500 */ 1501 int sqlite3_bind_blob( 1502 sqlite3_stmt *pStmt, 1503 int i, 1504 const void *zData, 1505 int nData, 1506 void (*xDel)(void*) 1507 ){ 1508 #ifdef SQLITE_ENABLE_API_ARMOR 1509 if( nData<0 ) return SQLITE_MISUSE_BKPT; 1510 #endif 1511 return bindText(pStmt, i, zData, nData, xDel, 0); 1512 } 1513 int sqlite3_bind_blob64( 1514 sqlite3_stmt *pStmt, 1515 int i, 1516 const void *zData, 1517 sqlite3_uint64 nData, 1518 void (*xDel)(void*) 1519 ){ 1520 assert( xDel!=SQLITE_DYNAMIC ); 1521 return bindText(pStmt, i, zData, nData, xDel, 0); 1522 } 1523 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1524 int rc; 1525 Vdbe *p = (Vdbe *)pStmt; 1526 rc = vdbeUnbind(p, i); 1527 if( rc==SQLITE_OK ){ 1528 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1529 sqlite3_mutex_leave(p->db->mutex); 1530 } 1531 return rc; 1532 } 1533 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1534 return sqlite3_bind_int64(p, i, (i64)iValue); 1535 } 1536 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 1537 int rc; 1538 Vdbe *p = (Vdbe *)pStmt; 1539 rc = vdbeUnbind(p, i); 1540 if( rc==SQLITE_OK ){ 1541 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1542 sqlite3_mutex_leave(p->db->mutex); 1543 } 1544 return rc; 1545 } 1546 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1547 int rc; 1548 Vdbe *p = (Vdbe*)pStmt; 1549 rc = vdbeUnbind(p, i); 1550 if( rc==SQLITE_OK ){ 1551 sqlite3_mutex_leave(p->db->mutex); 1552 } 1553 return rc; 1554 } 1555 int sqlite3_bind_pointer( 1556 sqlite3_stmt *pStmt, 1557 int i, 1558 void *pPtr, 1559 const char *zPTtype, 1560 void (*xDestructor)(void*) 1561 ){ 1562 int rc; 1563 Vdbe *p = (Vdbe*)pStmt; 1564 rc = vdbeUnbind(p, i); 1565 if( rc==SQLITE_OK ){ 1566 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); 1567 sqlite3_mutex_leave(p->db->mutex); 1568 }else if( xDestructor ){ 1569 xDestructor(pPtr); 1570 } 1571 return rc; 1572 } 1573 int sqlite3_bind_text( 1574 sqlite3_stmt *pStmt, 1575 int i, 1576 const char *zData, 1577 int nData, 1578 void (*xDel)(void*) 1579 ){ 1580 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1581 } 1582 int sqlite3_bind_text64( 1583 sqlite3_stmt *pStmt, 1584 int i, 1585 const char *zData, 1586 sqlite3_uint64 nData, 1587 void (*xDel)(void*), 1588 unsigned char enc 1589 ){ 1590 assert( xDel!=SQLITE_DYNAMIC ); 1591 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 1592 return bindText(pStmt, i, zData, nData, xDel, enc); 1593 } 1594 #ifndef SQLITE_OMIT_UTF16 1595 int sqlite3_bind_text16( 1596 sqlite3_stmt *pStmt, 1597 int i, 1598 const void *zData, 1599 int nData, 1600 void (*xDel)(void*) 1601 ){ 1602 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 1603 } 1604 #endif /* SQLITE_OMIT_UTF16 */ 1605 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1606 int rc; 1607 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 1608 case SQLITE_INTEGER: { 1609 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1610 break; 1611 } 1612 case SQLITE_FLOAT: { 1613 assert( pValue->flags & (MEM_Real|MEM_IntReal) ); 1614 rc = sqlite3_bind_double(pStmt, i, 1615 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i 1616 ); 1617 break; 1618 } 1619 case SQLITE_BLOB: { 1620 if( pValue->flags & MEM_Zero ){ 1621 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1622 }else{ 1623 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1624 } 1625 break; 1626 } 1627 case SQLITE_TEXT: { 1628 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1629 pValue->enc); 1630 break; 1631 } 1632 default: { 1633 rc = sqlite3_bind_null(pStmt, i); 1634 break; 1635 } 1636 } 1637 return rc; 1638 } 1639 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1640 int rc; 1641 Vdbe *p = (Vdbe *)pStmt; 1642 rc = vdbeUnbind(p, i); 1643 if( rc==SQLITE_OK ){ 1644 #ifndef SQLITE_OMIT_INCRBLOB 1645 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1646 #else 1647 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1648 #endif 1649 sqlite3_mutex_leave(p->db->mutex); 1650 } 1651 return rc; 1652 } 1653 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 1654 int rc; 1655 Vdbe *p = (Vdbe *)pStmt; 1656 sqlite3_mutex_enter(p->db->mutex); 1657 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 1658 rc = SQLITE_TOOBIG; 1659 }else{ 1660 assert( (n & 0x7FFFFFFF)==n ); 1661 rc = sqlite3_bind_zeroblob(pStmt, i, n); 1662 } 1663 rc = sqlite3ApiExit(p->db, rc); 1664 sqlite3_mutex_leave(p->db->mutex); 1665 return rc; 1666 } 1667 1668 /* 1669 ** Return the number of wildcards that can be potentially bound to. 1670 ** This routine is added to support DBD::SQLite. 1671 */ 1672 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 1673 Vdbe *p = (Vdbe*)pStmt; 1674 return p ? p->nVar : 0; 1675 } 1676 1677 /* 1678 ** Return the name of a wildcard parameter. Return NULL if the index 1679 ** is out of range or if the wildcard is unnamed. 1680 ** 1681 ** The result is always UTF-8. 1682 */ 1683 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1684 Vdbe *p = (Vdbe*)pStmt; 1685 if( p==0 ) return 0; 1686 return sqlite3VListNumToName(p->pVList, i); 1687 } 1688 1689 /* 1690 ** Given a wildcard parameter name, return the index of the variable 1691 ** with that name. If there is no variable with the given name, 1692 ** return 0. 1693 */ 1694 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1695 if( p==0 || zName==0 ) return 0; 1696 return sqlite3VListNameToNum(p->pVList, zName, nName); 1697 } 1698 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 1699 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 1700 } 1701 1702 /* 1703 ** Transfer all bindings from the first statement over to the second. 1704 */ 1705 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1706 Vdbe *pFrom = (Vdbe*)pFromStmt; 1707 Vdbe *pTo = (Vdbe*)pToStmt; 1708 int i; 1709 assert( pTo->db==pFrom->db ); 1710 assert( pTo->nVar==pFrom->nVar ); 1711 sqlite3_mutex_enter(pTo->db->mutex); 1712 for(i=0; i<pFrom->nVar; i++){ 1713 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1714 } 1715 sqlite3_mutex_leave(pTo->db->mutex); 1716 return SQLITE_OK; 1717 } 1718 1719 #ifndef SQLITE_OMIT_DEPRECATED 1720 /* 1721 ** Deprecated external interface. Internal/core SQLite code 1722 ** should call sqlite3TransferBindings. 1723 ** 1724 ** It is misuse to call this routine with statements from different 1725 ** database connections. But as this is a deprecated interface, we 1726 ** will not bother to check for that condition. 1727 ** 1728 ** If the two statements contain a different number of bindings, then 1729 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 1730 ** SQLITE_OK is returned. 1731 */ 1732 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1733 Vdbe *pFrom = (Vdbe*)pFromStmt; 1734 Vdbe *pTo = (Vdbe*)pToStmt; 1735 if( pFrom->nVar!=pTo->nVar ){ 1736 return SQLITE_ERROR; 1737 } 1738 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); 1739 if( pTo->expmask ){ 1740 pTo->expired = 1; 1741 } 1742 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); 1743 if( pFrom->expmask ){ 1744 pFrom->expired = 1; 1745 } 1746 return sqlite3TransferBindings(pFromStmt, pToStmt); 1747 } 1748 #endif 1749 1750 /* 1751 ** Return the sqlite3* database handle to which the prepared statement given 1752 ** in the argument belongs. This is the same database handle that was 1753 ** the first argument to the sqlite3_prepare() that was used to create 1754 ** the statement in the first place. 1755 */ 1756 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 1757 return pStmt ? ((Vdbe*)pStmt)->db : 0; 1758 } 1759 1760 /* 1761 ** Return true if the prepared statement is guaranteed to not modify the 1762 ** database. 1763 */ 1764 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1765 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1766 } 1767 1768 /* 1769 ** Return 1 if the statement is an EXPLAIN and return 2 if the 1770 ** statement is an EXPLAIN QUERY PLAN 1771 */ 1772 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ 1773 return pStmt ? ((Vdbe*)pStmt)->explain : 0; 1774 } 1775 1776 /* 1777 ** Return true if the prepared statement is in need of being reset. 1778 */ 1779 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 1780 Vdbe *v = (Vdbe*)pStmt; 1781 return v!=0 && v->iVdbeMagic==VDBE_MAGIC_RUN && v->pc>=0; 1782 } 1783 1784 /* 1785 ** Return a pointer to the next prepared statement after pStmt associated 1786 ** with database connection pDb. If pStmt is NULL, return the first 1787 ** prepared statement for the database connection. Return NULL if there 1788 ** are no more. 1789 */ 1790 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1791 sqlite3_stmt *pNext; 1792 #ifdef SQLITE_ENABLE_API_ARMOR 1793 if( !sqlite3SafetyCheckOk(pDb) ){ 1794 (void)SQLITE_MISUSE_BKPT; 1795 return 0; 1796 } 1797 #endif 1798 sqlite3_mutex_enter(pDb->mutex); 1799 if( pStmt==0 ){ 1800 pNext = (sqlite3_stmt*)pDb->pVdbe; 1801 }else{ 1802 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1803 } 1804 sqlite3_mutex_leave(pDb->mutex); 1805 return pNext; 1806 } 1807 1808 /* 1809 ** Return the value of a status counter for a prepared statement 1810 */ 1811 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1812 Vdbe *pVdbe = (Vdbe*)pStmt; 1813 u32 v; 1814 #ifdef SQLITE_ENABLE_API_ARMOR 1815 if( !pStmt 1816 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) 1817 ){ 1818 (void)SQLITE_MISUSE_BKPT; 1819 return 0; 1820 } 1821 #endif 1822 if( op==SQLITE_STMTSTATUS_MEMUSED ){ 1823 sqlite3 *db = pVdbe->db; 1824 sqlite3_mutex_enter(db->mutex); 1825 v = 0; 1826 db->pnBytesFreed = (int*)&v; 1827 sqlite3VdbeClearObject(db, pVdbe); 1828 sqlite3DbFree(db, pVdbe); 1829 db->pnBytesFreed = 0; 1830 sqlite3_mutex_leave(db->mutex); 1831 }else{ 1832 v = pVdbe->aCounter[op]; 1833 if( resetFlag ) pVdbe->aCounter[op] = 0; 1834 } 1835 return (int)v; 1836 } 1837 1838 /* 1839 ** Return the SQL associated with a prepared statement 1840 */ 1841 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 1842 Vdbe *p = (Vdbe *)pStmt; 1843 return p ? p->zSql : 0; 1844 } 1845 1846 /* 1847 ** Return the SQL associated with a prepared statement with 1848 ** bound parameters expanded. Space to hold the returned string is 1849 ** obtained from sqlite3_malloc(). The caller is responsible for 1850 ** freeing the returned string by passing it to sqlite3_free(). 1851 ** 1852 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 1853 ** expanded bound parameters. 1854 */ 1855 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 1856 #ifdef SQLITE_OMIT_TRACE 1857 return 0; 1858 #else 1859 char *z = 0; 1860 const char *zSql = sqlite3_sql(pStmt); 1861 if( zSql ){ 1862 Vdbe *p = (Vdbe *)pStmt; 1863 sqlite3_mutex_enter(p->db->mutex); 1864 z = sqlite3VdbeExpandSql(p, zSql); 1865 sqlite3_mutex_leave(p->db->mutex); 1866 } 1867 return z; 1868 #endif 1869 } 1870 1871 #ifdef SQLITE_ENABLE_NORMALIZE 1872 /* 1873 ** Return the normalized SQL associated with a prepared statement. 1874 */ 1875 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ 1876 Vdbe *p = (Vdbe *)pStmt; 1877 if( p==0 ) return 0; 1878 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ 1879 sqlite3_mutex_enter(p->db->mutex); 1880 p->zNormSql = sqlite3Normalize(p, p->zSql); 1881 sqlite3_mutex_leave(p->db->mutex); 1882 } 1883 return p->zNormSql; 1884 } 1885 #endif /* SQLITE_ENABLE_NORMALIZE */ 1886 1887 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1888 /* 1889 ** Allocate and populate an UnpackedRecord structure based on the serialized 1890 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 1891 ** if successful, or a NULL pointer if an OOM error is encountered. 1892 */ 1893 static UnpackedRecord *vdbeUnpackRecord( 1894 KeyInfo *pKeyInfo, 1895 int nKey, 1896 const void *pKey 1897 ){ 1898 UnpackedRecord *pRet; /* Return value */ 1899 1900 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 1901 if( pRet ){ 1902 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); 1903 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 1904 } 1905 return pRet; 1906 } 1907 1908 /* 1909 ** This function is called from within a pre-update callback to retrieve 1910 ** a field of the row currently being updated or deleted. 1911 */ 1912 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 1913 PreUpdate *p = db->pPreUpdate; 1914 Mem *pMem; 1915 int rc = SQLITE_OK; 1916 1917 /* Test that this call is being made from within an SQLITE_DELETE or 1918 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 1919 if( !p || p->op==SQLITE_INSERT ){ 1920 rc = SQLITE_MISUSE_BKPT; 1921 goto preupdate_old_out; 1922 } 1923 if( p->pPk ){ 1924 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 1925 } 1926 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 1927 rc = SQLITE_RANGE; 1928 goto preupdate_old_out; 1929 } 1930 1931 /* If the old.* record has not yet been loaded into memory, do so now. */ 1932 if( p->pUnpacked==0 ){ 1933 u32 nRec; 1934 u8 *aRec; 1935 1936 assert( p->pCsr->eCurType==CURTYPE_BTREE ); 1937 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 1938 aRec = sqlite3DbMallocRaw(db, nRec); 1939 if( !aRec ) goto preupdate_old_out; 1940 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 1941 if( rc==SQLITE_OK ){ 1942 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 1943 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 1944 } 1945 if( rc!=SQLITE_OK ){ 1946 sqlite3DbFree(db, aRec); 1947 goto preupdate_old_out; 1948 } 1949 p->aRecord = aRec; 1950 } 1951 1952 pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; 1953 if( iIdx==p->pTab->iPKey ){ 1954 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 1955 }else if( iIdx>=p->pUnpacked->nField ){ 1956 *ppValue = (sqlite3_value *)columnNullValue(); 1957 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 1958 if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 1959 testcase( pMem->flags & MEM_Int ); 1960 testcase( pMem->flags & MEM_IntReal ); 1961 sqlite3VdbeMemRealify(pMem); 1962 } 1963 } 1964 1965 preupdate_old_out: 1966 sqlite3Error(db, rc); 1967 return sqlite3ApiExit(db, rc); 1968 } 1969 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1970 1971 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1972 /* 1973 ** This function is called from within a pre-update callback to retrieve 1974 ** the number of columns in the row being updated, deleted or inserted. 1975 */ 1976 int sqlite3_preupdate_count(sqlite3 *db){ 1977 PreUpdate *p = db->pPreUpdate; 1978 return (p ? p->keyinfo.nKeyField : 0); 1979 } 1980 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1981 1982 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 1983 /* 1984 ** This function is designed to be called from within a pre-update callback 1985 ** only. It returns zero if the change that caused the callback was made 1986 ** immediately by a user SQL statement. Or, if the change was made by a 1987 ** trigger program, it returns the number of trigger programs currently 1988 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 1989 ** top-level trigger etc.). 1990 ** 1991 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 1992 ** or SET DEFAULT action is considered a trigger. 1993 */ 1994 int sqlite3_preupdate_depth(sqlite3 *db){ 1995 PreUpdate *p = db->pPreUpdate; 1996 return (p ? p->v->nFrame : 0); 1997 } 1998 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 1999 2000 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2001 /* 2002 ** This function is designed to be called from within a pre-update callback 2003 ** only. 2004 */ 2005 int sqlite3_preupdate_blobwrite(sqlite3 *db){ 2006 PreUpdate *p = db->pPreUpdate; 2007 return (p ? p->iBlobWrite : -1); 2008 } 2009 #endif 2010 2011 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2012 /* 2013 ** This function is called from within a pre-update callback to retrieve 2014 ** a field of the row currently being updated or inserted. 2015 */ 2016 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 2017 PreUpdate *p = db->pPreUpdate; 2018 int rc = SQLITE_OK; 2019 Mem *pMem; 2020 2021 if( !p || p->op==SQLITE_DELETE ){ 2022 rc = SQLITE_MISUSE_BKPT; 2023 goto preupdate_new_out; 2024 } 2025 if( p->pPk && p->op!=SQLITE_UPDATE ){ 2026 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 2027 } 2028 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 2029 rc = SQLITE_RANGE; 2030 goto preupdate_new_out; 2031 } 2032 2033 if( p->op==SQLITE_INSERT ){ 2034 /* For an INSERT, memory cell p->iNewReg contains the serialized record 2035 ** that is being inserted. Deserialize it. */ 2036 UnpackedRecord *pUnpack = p->pNewUnpacked; 2037 if( !pUnpack ){ 2038 Mem *pData = &p->v->aMem[p->iNewReg]; 2039 rc = ExpandBlob(pData); 2040 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2041 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 2042 if( !pUnpack ){ 2043 rc = SQLITE_NOMEM; 2044 goto preupdate_new_out; 2045 } 2046 p->pNewUnpacked = pUnpack; 2047 } 2048 pMem = &pUnpack->aMem[iIdx]; 2049 if( iIdx==p->pTab->iPKey ){ 2050 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2051 }else if( iIdx>=pUnpack->nField ){ 2052 pMem = (sqlite3_value *)columnNullValue(); 2053 } 2054 }else{ 2055 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 2056 ** value. Make a copy of the cell contents and return a pointer to it. 2057 ** It is not safe to return a pointer to the memory cell itself as the 2058 ** caller may modify the value text encoding. 2059 */ 2060 assert( p->op==SQLITE_UPDATE ); 2061 if( !p->aNew ){ 2062 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 2063 if( !p->aNew ){ 2064 rc = SQLITE_NOMEM; 2065 goto preupdate_new_out; 2066 } 2067 } 2068 assert( iIdx>=0 && iIdx<p->pCsr->nField ); 2069 pMem = &p->aNew[iIdx]; 2070 if( pMem->flags==0 ){ 2071 if( iIdx==p->pTab->iPKey ){ 2072 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2073 }else{ 2074 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 2075 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2076 } 2077 } 2078 } 2079 *ppValue = pMem; 2080 2081 preupdate_new_out: 2082 sqlite3Error(db, rc); 2083 return sqlite3ApiExit(db, rc); 2084 } 2085 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2086 2087 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 2088 /* 2089 ** Return status data for a single loop within query pStmt. 2090 */ 2091 int sqlite3_stmt_scanstatus( 2092 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 2093 int idx, /* Index of loop to report on */ 2094 int iScanStatusOp, /* Which metric to return */ 2095 void *pOut /* OUT: Write the answer here */ 2096 ){ 2097 Vdbe *p = (Vdbe*)pStmt; 2098 ScanStatus *pScan; 2099 if( idx<0 || idx>=p->nScan ) return 1; 2100 pScan = &p->aScan[idx]; 2101 switch( iScanStatusOp ){ 2102 case SQLITE_SCANSTAT_NLOOP: { 2103 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; 2104 break; 2105 } 2106 case SQLITE_SCANSTAT_NVISIT: { 2107 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; 2108 break; 2109 } 2110 case SQLITE_SCANSTAT_EST: { 2111 double r = 1.0; 2112 LogEst x = pScan->nEst; 2113 while( x<100 ){ 2114 x += 10; 2115 r *= 0.5; 2116 } 2117 *(double*)pOut = r*sqlite3LogEstToInt(x); 2118 break; 2119 } 2120 case SQLITE_SCANSTAT_NAME: { 2121 *(const char**)pOut = pScan->zName; 2122 break; 2123 } 2124 case SQLITE_SCANSTAT_EXPLAIN: { 2125 if( pScan->addrExplain ){ 2126 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; 2127 }else{ 2128 *(const char**)pOut = 0; 2129 } 2130 break; 2131 } 2132 case SQLITE_SCANSTAT_SELECTID: { 2133 if( pScan->addrExplain ){ 2134 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; 2135 }else{ 2136 *(int*)pOut = -1; 2137 } 2138 break; 2139 } 2140 default: { 2141 return 1; 2142 } 2143 } 2144 return 0; 2145 } 2146 2147 /* 2148 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 2149 */ 2150 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 2151 Vdbe *p = (Vdbe*)pStmt; 2152 memset(p->anExec, 0, p->nOp * sizeof(i64)); 2153 } 2154 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ 2155