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