Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/src/vdbeapi.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / src / vdbeapi.c` 2765 lines ยท 84488 bytes Raw Blame History 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 #include "opcodes.h" 19 20 #ifndef SQLITE_OMIT_DEPRECATED 21 /* 22 ** Return TRUE (non-zero) of the statement supplied as an argument needs 23 ** to be recompiled. A statement needs to be recompiled whenever the 24 ** execution environment changes in a way that would alter the program 25 ** that sqlite3_prepare() generates. For example, if new functions or 26 ** collating sequences are registered or if an authorizer function is 27 ** added or changed. 28 */ 29 int sqlite3_expired(sqlite3_stmt *pStmt){ 30 int iRet = 1; 31 if( pStmt ){ 32 Vdbe *p = (Vdbe*)pStmt; 33 sqlite3_mutex_enter(p->db->mutex); 34 iRet = p->expired; 35 sqlite3_mutex_leave(p->db->mutex); 36 } 37 return iRet; 38 } 39 #endif 40 41 /* 42 ** Check on a Vdbe to make sure it has not been finalized. Log 43 ** an error and return true if it has been finalized (or is otherwise 44 ** invalid). Return false if it is ok. 45 */ 46 static int vdbeSafety(Vdbe *p){ 47 if( p->db==0 ){ 48 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); 49 return 1; 50 }else{ 51 return 0; 52 } 53 } 54 static int vdbeSafetyNotNull(Vdbe *p){ 55 if( p==0 ){ 56 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 57 return 1; 58 }else{ 59 return vdbeSafety(p); 60 } 61 } 62 63 #ifndef SQLITE_OMIT_TRACE 64 /* 65 ** Invoke the profile callback. This routine is only called if we already 66 ** know that the profile callback is defined and needs to be invoked. 67 */ 68 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 69 sqlite3_int64 iNow; 70 sqlite3_int64 iElapse; 71 assert( p->startTime>0 ); 72 assert( db->init.busy==0 ); 73 assert( p->zSql!=0 ); 74 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 75 iElapse = (iNow - p->startTime)*`1000000`; 76 #ifndef SQLITE_OMIT_DEPRECATED 77 if( db->xProfile ){ 78 db->xProfile(db->pProfileArg, p->zSql, iElapse); 79 } 80 #endif 81 if( db->mTrace & SQLITE_TRACE_PROFILE ){ 82 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 83 } 84 p->startTime = 0; 85 } 86 /* 87 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 88 ** is needed, and it invokes the callback if it is needed. 89 */ 90 # define checkProfileCallback(DB,P) \ 91 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 92 #else 93 # define checkProfileCallback(DB,P) /*no-op*/ 94 #endif 95 96 /* 97 ** The following routine destroys a virtual machine that is created by 98 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 99 ** success/failure code that describes the result of executing the virtual 100 ** machine. 101 ** 102 ** This routine sets the error code and string returned by 103 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 104 */ 105 int sqlite3_finalize(sqlite3_stmt *pStmt){ 106 int rc; 107 if( pStmt==0 ){ 108 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 109 ** pointer is a harmless no-op. */ 110 rc = SQLITE_OK; 111 }else{ 112 Vdbe *v = (Vdbe*)pStmt; 113 sqlite3 *db = v->db; 114 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 115 #if defined(SQLITE_DISPOSAL_NOTIFIER) 116 SQLITE_DISPOSAL_NOTIFIER(2,pStmt); 117 #endif 118 sqlite3_mutex_enter(db->mutex); 119 checkProfileCallback(db, v); 120 assert( v->eVdbeState>=VDBE_READY_STATE ); 121 rc = sqlite3VdbeReset(v); 122 sqlite3VdbeDelete(v); 123 rc = sqlite3ApiExit(db, rc); 124 sqlite3LeaveMutexAndCloseZombie(db); 125 } 126 return rc; 127 } 128 129 /* 130 ** Terminate the current execution of an SQL statement and reset it 131 ** back to its starting state so that it can be reused. A success code from 132 ** the prior execution is returned. 133 ** 134 ** This routine sets the error code and string returned by 135 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 136 */ 137 int sqlite3_reset(sqlite3_stmt *pStmt){ 138 int rc; 139 if( pStmt==0 ){ 140 rc = SQLITE_OK; 141 }else{ 142 Vdbe *v = (Vdbe*)pStmt; 143 sqlite3 *db = v->db; 144 sqlite3_mutex_enter(db->mutex); 145 checkProfileCallback(db, v); 146 rc = sqlite3VdbeReset(v); 147 sqlite3VdbeRewind(v); 148 assert( (rc & (db->errMask))==rc ); 149 rc = sqlite3ApiExit(db, rc); 150 sqlite3_mutex_leave(db->mutex); 151 } 152 return rc; 153 } 154 155 /* 156 ** Set all the parameters in the compiled SQL statement to NULL. 157 */ 158 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 159 int i; 160 int rc = SQLITE_OK; 161 Vdbe *p = (Vdbe*)pStmt; 162 u32 maskSmallInt = 0; /* Bitmask of bindings with value 0 or 1 */ 163 #if SQLITE_THREADSAFE 164 sqlite3_mutex *mutex; 165 #endif 166 #ifdef SQLITE_ENABLE_API_ARMOR 167 if( pStmt==0 ){ 168 return SQLITE_MISUSE_BKPT; 169 } 170 #endif 171 #if SQLITE_THREADSAFE 172 mutex = p->db->mutex; 173 #endif 174 175 sqlite3_mutex_enter(mutex); 176 for(i=0; inVar; i++){ 177 Mem *pVar = &p->aVar[i]; 178 if( (pVar->flags & MEM_Int)!=0 && pVar->u.i>=0 && pVar->u.i<=1 ){ 179 maskSmallInt |= i>=31 ? 0x80000000 : ((u32)1)<flags = MEM_Null; 183 } 184 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 185 if( (p->expmask&~p->smimask)!=0 || (p->smimask&maskSmallInt)!=0 ){ 186 p->expired = 1; 187 } 188 sqlite3_mutex_leave(mutex); 189 return rc; 190 } 191 192 193 /**************************** sqlite3_value_ ******************************* 194 ** The following routines extract information from a Mem or sqlite3_value 195 ** structure. 196 */ 197 const void *sqlite3_value_blob(sqlite3_value *pVal){ 198 Mem *p = (Mem*)pVal; 199 if( p->flags & (MEM_Blob|MEM_Str) ){ 200 if( ExpandBlob(p)!=SQLITE_OK ){ 201 assert( p->flags==MEM_Null && p->z==0 ); 202 return 0; 203 } 204 p->flags |= MEM_Blob; 205 return p->n ? p->z : 0; 206 }else{ 207 return sqlite3_value_text(pVal); 208 } 209 } 210 int sqlite3_value_bytes(sqlite3_value *pVal){ 211 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 212 } 213 int sqlite3_value_bytes16(sqlite3_value *pVal){ 214 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 215 } 216 double sqlite3_value_double(sqlite3_value *pVal){ 217 return sqlite3VdbeRealValue((Mem*)pVal); 218 } 219 int sqlite3_value_int(sqlite3_value *pVal){ 220 return (int)sqlite3VdbeIntValue((Mem*)pVal); 221 } 222 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 223 return sqlite3VdbeIntValue((Mem*)pVal); 224 } 225 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 226 Mem *pMem = (Mem*)pVal; 227 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 228 } 229 void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ 230 Mem *p = (Mem*)pVal; 231 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 232 (MEM_Null|MEM_Term|MEM_Subtype) 233 && zPType!=0 234 && p->eSubtype=='p' 235 && strcmp(p->u.zPType, zPType)==0 236 ){ 237 return (void*)p->z; 238 }else{ 239 return 0; 240 } 241 } 242 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 243 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 244 } 245 #ifndef SQLITE_OMIT_UTF16 246 const void *sqlite3_value_text16(sqlite3_value* pVal){ 247 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 248 } 249 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 250 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 251 } 252 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 253 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 254 } 255 #endif /* SQLITE_OMIT_UTF16 */ 256 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 257 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 258 ** point number string BLOB NULL 259 */ 260 int sqlite3_value_type(sqlite3_value* pVal){ 261 static const u8 aType[] = { 262 SQLITE_BLOB, /* 0x00 (not possible) */ 263 SQLITE_NULL, /* 0x01 NULL */ 264 SQLITE_TEXT, /* 0x02 TEXT */ 265 SQLITE_NULL, /* 0x03 (not possible) */ 266 SQLITE_INTEGER, /* 0x04 INTEGER */ 267 SQLITE_NULL, /* 0x05 (not possible) */ 268 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ 269 SQLITE_NULL, /* 0x07 (not possible) */ 270 SQLITE_FLOAT, /* 0x08 FLOAT */ 271 SQLITE_NULL, /* 0x09 (not possible) */ 272 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ 273 SQLITE_NULL, /* 0x0b (not possible) */ 274 SQLITE_INTEGER, /* 0x0c (not possible) */ 275 SQLITE_NULL, /* 0x0d (not possible) */ 276 SQLITE_INTEGER, /* 0x0e (not possible) */ 277 SQLITE_NULL, /* 0x0f (not possible) */ 278 SQLITE_BLOB, /* 0x10 BLOB */ 279 SQLITE_NULL, /* 0x11 (not possible) */ 280 SQLITE_TEXT, /* 0x12 (not possible) */ 281 SQLITE_NULL, /* 0x13 (not possible) */ 282 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ 283 SQLITE_NULL, /* 0x15 (not possible) */ 284 SQLITE_INTEGER, /* 0x16 (not possible) */ 285 SQLITE_NULL, /* 0x17 (not possible) */ 286 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ 287 SQLITE_NULL, /* 0x19 (not possible) */ 288 SQLITE_FLOAT, /* 0x1a (not possible) */ 289 SQLITE_NULL, /* 0x1b (not possible) */ 290 SQLITE_INTEGER, /* 0x1c (not possible) */ 291 SQLITE_NULL, /* 0x1d (not possible) */ 292 SQLITE_INTEGER, /* 0x1e (not possible) */ 293 SQLITE_NULL, /* 0x1f (not possible) */ 294 SQLITE_FLOAT, /* 0x20 INTREAL */ 295 SQLITE_NULL, /* 0x21 (not possible) */ 296 SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */ 297 SQLITE_NULL, /* 0x23 (not possible) */ 298 SQLITE_FLOAT, /* 0x24 (not possible) */ 299 SQLITE_NULL, /* 0x25 (not possible) */ 300 SQLITE_FLOAT, /* 0x26 (not possible) */ 301 SQLITE_NULL, /* 0x27 (not possible) */ 302 SQLITE_FLOAT, /* 0x28 (not possible) */ 303 SQLITE_NULL, /* 0x29 (not possible) */ 304 SQLITE_FLOAT, /* 0x2a (not possible) */ 305 SQLITE_NULL, /* 0x2b (not possible) */ 306 SQLITE_FLOAT, /* 0x2c (not possible) */ 307 SQLITE_NULL, /* 0x2d (not possible) */ 308 SQLITE_FLOAT, /* 0x2e (not possible) */ 309 SQLITE_NULL, /* 0x2f (not possible) */ 310 SQLITE_BLOB, /* 0x30 (not possible) */ 311 SQLITE_NULL, /* 0x31 (not possible) */ 312 SQLITE_TEXT, /* 0x32 (not possible) */ 313 SQLITE_NULL, /* 0x33 (not possible) */ 314 SQLITE_FLOAT, /* 0x34 (not possible) */ 315 SQLITE_NULL, /* 0x35 (not possible) */ 316 SQLITE_FLOAT, /* 0x36 (not possible) */ 317 SQLITE_NULL, /* 0x37 (not possible) */ 318 SQLITE_FLOAT, /* 0x38 (not possible) */ 319 SQLITE_NULL, /* 0x39 (not possible) */ 320 SQLITE_FLOAT, /* 0x3a (not possible) */ 321 SQLITE_NULL, /* 0x3b (not possible) */ 322 SQLITE_FLOAT, /* 0x3c (not possible) */ 323 SQLITE_NULL, /* 0x3d (not possible) */ 324 SQLITE_FLOAT, /* 0x3e (not possible) */ 325 SQLITE_NULL, /* 0x3f (not possible) */ 326 }; 327 #ifdef SQLITE_DEBUG 328 { 329 int eType = SQLITE_BLOB; 330 if( pVal->flags & MEM_Null ){ 331 eType = SQLITE_NULL; 332 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ 333 eType = SQLITE_FLOAT; 334 }else if( pVal->flags & MEM_Int ){ 335 eType = SQLITE_INTEGER; 336 }else if( pVal->flags & MEM_Str ){ 337 eType = SQLITE_TEXT; 338 } 339 assert( eType == aType[pVal->flags&MEM_AffMask] ); 340 } 341 #endif 342 return aType[pVal->flags&MEM_AffMask]; 343 } 344 int sqlite3_value_encoding(sqlite3_value *pVal){ 345 return pVal->enc; 346 } 347 348 /* Return true if a parameter to xUpdate represents an unchanged column */ 349 int sqlite3_value_nochange(sqlite3_value *pVal){ 350 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); 351 } 352 353 /* Return true if a parameter value originated from an sqlite3_bind() */ 354 int sqlite3_value_frombind(sqlite3_value *pVal){ 355 return (pVal->flags&MEM_FromBind)!=0; 356 } 357 358 /* Make a copy of an sqlite3_value object 359 */ 360 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 361 sqlite3_value *pNew; 362 if( pOrig==0 ) return 0; 363 pNew = sqlite3_malloc( sizeof(*pNew) ); 364 if( pNew==0 ) return 0; 365 memset(pNew, 0, sizeof(*pNew)); 366 memcpy(pNew, pOrig, MEMCELLSIZE); 367 pNew->flags &= ~MEM_Dyn; 368 pNew->db = 0; 369 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 370 pNew->flags &= ~(MEM_Static|MEM_Dyn); 371 pNew->flags |= MEM_Ephem; 372 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 373 sqlite3ValueFree(pNew); 374 pNew = 0; 375 } 376 }else if( pNew->flags & MEM_Null ){ 377 /* Do not duplicate pointer values */ 378 pNew->flags &= ~(MEM_Term|MEM_Subtype); 379 } 380 return pNew; 381 } 382 383 /* Destroy an sqlite3_value object previously obtained from 384 ** sqlite3_value_dup(). 385 */ 386 void sqlite3_value_free(sqlite3_value *pOld){ 387 sqlite3ValueFree(pOld); 388 } 389 390 391 /**************************** sqlite3_result_ ******************************* 392 ** The following routines are used by application-defined SQL functions to 393 ** specify the function return value. There are many variations on 394 ** sqlite3_result_xxxx() for different types of return values. 395 ** 396 ** The setStrOrError() function is a helper function that invokes 397 ** sqlite3VdbeMemSetStr() to store the result as a string or blob. 398 ** Appropriate errors are set if the string/blob is too big or if 399 ** an OOM occurs. 400 ** 401 ** The invokeValueDestructor(P,X) helper function invokes the destructor 402 ** function X() on value P if P is not going to be used and need to 403 ** be destroyed. 404 */ 405 static void setResultStrOrError( 406 sqlite3_context *pCtx, /* Function context */ 407 const char *z, /* String pointer */ 408 int n, /* Bytes in string, or negative */ 409 u8 enc, /* Encoding of z. 0 for BLOBs */ 410 void (*xDel)(void*) /* Destructor function */ 411 ){ 412 Mem *pOut = pCtx->pOut; 413 int rc; 414 if( enc==SQLITE_UTF8 ){ 415 rc = sqlite3VdbeMemSetText(pOut, z, n, xDel); 416 }else if( enc==SQLITE_UTF8_ZT ){ 417 /* It is usually considered improper to assert() on an input. However, 418 ** the following assert() is checking for inputs that are documented 419 ** to result in undefined behavior. */ 420 assert( z==0 421 || n<0 422 || n>pOut->db->aLimit[SQLITE_LIMIT_LENGTH] 423 || z[n]==0 424 ); 425 rc = sqlite3VdbeMemSetText(pOut, z, n, xDel); 426 pOut->flags |= MEM_Term; 427 }else{ 428 rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); 429 } 430 if( rc ){ 431 if( rc==SQLITE_TOOBIG ){ 432 sqlite3_result_error_toobig(pCtx); 433 }else{ 434 /* The only errors possible from sqlite3VdbeMemSetStr are 435 ** SQLITE_TOOBIG and SQLITE_NOMEM */ 436 assert( rc==SQLITE_NOMEM ); 437 sqlite3_result_error_nomem(pCtx); 438 } 439 return; 440 } 441 sqlite3VdbeChangeEncoding(pOut, pCtx->enc); 442 if( sqlite3VdbeMemTooBig(pOut) ){ 443 sqlite3_result_error_toobig(pCtx); 444 } 445 } 446 static int invokeValueDestructor( 447 const void *p, /* Value to destroy */ 448 void (*xDel)(void*), /* The destructor */ 449 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */ 450 ){ 451 assert( xDel!=SQLITE_DYNAMIC ); 452 if( xDel==0 ){ 453 /* noop */ 454 }else if( xDel==SQLITE_TRANSIENT ){ 455 /* noop */ 456 }else{ 457 xDel((void*)p); 458 } 459 #ifdef SQLITE_ENABLE_API_ARMOR 460 if( pCtx!=0 ){ 461 sqlite3_result_error_toobig(pCtx); 462 } 463 #else 464 assert( pCtx!=0 ); 465 sqlite3_result_error_toobig(pCtx); 466 #endif 467 return SQLITE_TOOBIG; 468 } 469 void sqlite3_result_blob( 470 sqlite3_context *pCtx, 471 const void *z, 472 int n, 473 void (*xDel)(void *) 474 ){ 475 #ifdef SQLITE_ENABLE_API_ARMOR 476 if( pCtx==0 || n<0 ){ 477 invokeValueDestructor(z, xDel, pCtx); 478 return; 479 } 480 #endif 481 assert( n>=0 ); 482 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 483 setResultStrOrError(pCtx, z, n, 0, xDel); 484 } 485 void sqlite3_result_blob64( 486 sqlite3_context *pCtx, 487 const void *z, 488 sqlite3_uint64 n, 489 void (*xDel)(void *) 490 ){ 491 assert( xDel!=SQLITE_DYNAMIC ); 492 #ifdef SQLITE_ENABLE_API_ARMOR 493 if( pCtx==0 ){ 494 invokeValueDestructor(z, xDel, 0); 495 return; 496 } 497 #endif 498 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 499 if( n>0x7fffffff ){ 500 (void)invokeValueDestructor(z, xDel, pCtx); 501 }else{ 502 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 503 } 504 } 505 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 506 #ifdef SQLITE_ENABLE_API_ARMOR 507 if( pCtx==0 ) return; 508 #endif 509 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 510 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 511 } 512 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 513 #ifdef SQLITE_ENABLE_API_ARMOR 514 if( pCtx==0 ) return; 515 #endif 516 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 517 pCtx->isError = SQLITE_ERROR; 518 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 519 } 520 #ifndef SQLITE_OMIT_UTF16 521 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 522 #ifdef SQLITE_ENABLE_API_ARMOR 523 if( pCtx==0 ) return; 524 #endif 525 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 526 pCtx->isError = SQLITE_ERROR; 527 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 528 } 529 #endif 530 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 531 #ifdef SQLITE_ENABLE_API_ARMOR 532 if( pCtx==0 ) return; 533 #endif 534 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 535 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 536 } 537 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 538 #ifdef SQLITE_ENABLE_API_ARMOR 539 if( pCtx==0 ) return; 540 #endif 541 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 542 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 543 } 544 void sqlite3_result_null(sqlite3_context *pCtx){ 545 #ifdef SQLITE_ENABLE_API_ARMOR 546 if( pCtx==0 ) return; 547 #endif 548 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 549 sqlite3VdbeMemSetNull(pCtx->pOut); 550 } 551 void sqlite3_result_pointer( 552 sqlite3_context *pCtx, 553 void *pPtr, 554 const char *zPType, 555 void (*xDestructor)(void*) 556 ){ 557 Mem *pOut; 558 #ifdef SQLITE_ENABLE_API_ARMOR 559 if( pCtx==0 ){ 560 invokeValueDestructor(pPtr, xDestructor, 0); 561 return; 562 } 563 #endif 564 pOut = pCtx->pOut; 565 assert( sqlite3_mutex_held(pOut->db->mutex) ); 566 sqlite3VdbeMemRelease(pOut); 567 pOut->flags = MEM_Null; 568 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); 569 } 570 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 571 Mem *pOut; 572 #ifdef SQLITE_ENABLE_API_ARMOR 573 if( pCtx==0 ) return; 574 #endif 575 #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0 576 if( pCtx->pFunc!=0 577 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0 578 ){ 579 char zErr[200]; 580 sqlite3_snprintf(sizeof(zErr), zErr, 581 "misuse of sqlite3_result_subtype() by %s()", 582 pCtx->pFunc->zName); 583 sqlite3_result_error(pCtx, zErr, -1); 584 return; 585 } 586 #endif /* SQLITE_STRICT_SUBTYPE */ 587 pOut = pCtx->pOut; 588 assert( sqlite3_mutex_held(pOut->db->mutex) ); 589 pOut->eSubtype = eSubtype & 0xff; 590 pOut->flags |= MEM_Subtype; 591 } 592 void sqlite3_result_text( 593 sqlite3_context *pCtx, 594 const char *z, 595 int n, 596 void (*xDel)(void *) 597 ){ 598 #ifdef SQLITE_ENABLE_API_ARMOR 599 if( pCtx==0 ){ 600 invokeValueDestructor(z, xDel, 0); 601 return; 602 } 603 #endif 604 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 605 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 606 } 607 void sqlite3_result_text64( 608 sqlite3_context *pCtx, 609 const char *z, 610 sqlite3_uint64 n, 611 void (*xDel)(void *), 612 unsigned char enc 613 ){ 614 #ifdef SQLITE_ENABLE_API_ARMOR 615 if( pCtx==0 ){ 616 invokeValueDestructor(z, xDel, 0); 617 return; 618 } 619 #endif 620 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 621 assert( xDel!=SQLITE_DYNAMIC ); 622 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){ 623 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 624 n &= ~(u64)1; 625 } 626 if( n>0x7fffffff ){ 627 (void)invokeValueDestructor(z, xDel, pCtx); 628 }else{ 629 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 630 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut); 631 } 632 } 633 #ifndef SQLITE_OMIT_UTF16 634 void sqlite3_result_text16( 635 sqlite3_context *pCtx, 636 const void *z, 637 int n, 638 void (*xDel)(void *) 639 ){ 640 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 641 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel); 642 } 643 void sqlite3_result_text16be( 644 sqlite3_context *pCtx, 645 const void *z, 646 int n, 647 void (*xDel)(void *) 648 ){ 649 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 650 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel); 651 } 652 void sqlite3_result_text16le( 653 sqlite3_context *pCtx, 654 const void *z, 655 int n, 656 void (*xDel)(void *) 657 ){ 658 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 659 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel); 660 } 661 #endif /* SQLITE_OMIT_UTF16 */ 662 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 663 Mem *pOut; 664 665 #ifdef SQLITE_ENABLE_API_ARMOR 666 if( pCtx==0 ) return; 667 if( pValue==0 ){ 668 sqlite3_result_null(pCtx); 669 return; 670 } 671 #endif 672 pOut = pCtx->pOut; 673 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 674 sqlite3VdbeMemCopy(pOut, pValue); 675 sqlite3VdbeChangeEncoding(pOut, pCtx->enc); 676 if( sqlite3VdbeMemTooBig(pOut) ){ 677 sqlite3_result_error_toobig(pCtx); 678 } 679 } 680 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 681 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); 682 } 683 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 684 Mem *pOut; 685 686 #ifdef SQLITE_ENABLE_API_ARMOR 687 if( pCtx==0 ) return SQLITE_MISUSE_BKPT; 688 #endif 689 pOut = pCtx->pOut; 690 assert( sqlite3_mutex_held(pOut->db->mutex) ); 691 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 692 sqlite3_result_error_toobig(pCtx); 693 return SQLITE_TOOBIG; 694 } 695 #ifndef SQLITE_OMIT_INCRBLOB 696 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 697 pOut->enc = pCtx->enc; 698 return SQLITE_OK; 699 #else 700 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 701 #endif 702 } 703 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 704 #ifdef SQLITE_ENABLE_API_ARMOR 705 if( pCtx==0 ) return; 706 #endif 707 pCtx->isError = errCode ? errCode : -1; 708 #ifdef SQLITE_DEBUG 709 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 710 #endif 711 if( pCtx->pOut->flags & MEM_Null ){ 712 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, 713 SQLITE_STATIC); 714 } 715 } 716 717 /* Cause the SQL function to raise an SQLITE_TOOBIG error. */ 718 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 719 #ifdef SQLITE_ENABLE_API_ARMOR 720 if( pCtx==0 ) return; 721 #endif 722 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 723 pCtx->isError = SQLITE_TOOBIG; 724 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 725 SQLITE_UTF8, SQLITE_STATIC); 726 } 727 728 /* Cause the SQL function to raise an SQLITE_NOMEM error. */ 729 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 730 #ifdef SQLITE_ENABLE_API_ARMOR 731 if( pCtx==0 ) return; 732 #endif 733 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 734 sqlite3VdbeMemSetNull(pCtx->pOut); 735 pCtx->isError = SQLITE_NOMEM_BKPT; 736 sqlite3OomFault(pCtx->pOut->db); 737 } 738 739 /* Make the return value of the SQL function or virtual table pCtx 740 ** be the content of the sqlite3_str object pStr. The eOwn flag 741 ** determines ownership of the sqlite3_str object and its content. 742 ** 743 ** eOwn Ownership transfer 744 ** ------------- ------------------------------------------------ 745 ** 746 ** SQLITE_COPY The SQL function returns a copy the sqlite3_str 747 ** content and leaves the sqlite3_str object itself 748 ** unchanged. 749 ** 750 ** SQLITE_XFER The content of the sqlite3_str is transferred to 751 ** the SQL function and the SQL function takes 752 ** responsibility for freeing that content when it is 753 ** no longer needed. The sqlite3_str object is reset 754 ** to an empty string. 755 ** 756 ** SQLITE_FINISH Like SQLITE_XFER except that the pStr is also 757 ** freed using sqlite3_str_free(). 758 */ 759 void sqlite3_result_str(sqlite3_context *pCtx, sqlite3_str *pStr, int eOwn){ 760 #ifdef SQLITE_ENABLE_API_ARMOR 761 if( pCtx==0 ) return; 762 if( pStr==0 ) return; 763 #endif 764 testcase( pStr==(sqlite3_str*)&sqlite3OomStr ); 765 if( pStr->accError==0 ){ 766 if( pStr->nChar==0 ){ 767 setResultStrOrError(pCtx, "", 0, SQLITE_UTF8_ZT, SQLITE_STATIC); 768 sqlite3_str_reset(pStr); 769 }else{ 770 const char *zText = sqlite3_str_value(pStr); 771 /* Only internal code has the ability to capture a pointer to 772 ** an sqlite3_str object that uses static buffer. And none of 773 ** those internal use cases every invoke the sqlite3_result_str() 774 ** interface on a static-buffer sqlite3_str. Should this change 775 ** in the future, the following assert() will let us know. */ 776 assert( isMalloced(pStr) ); 777 if( eOwn==SQLITE_COPY ){ 778 setResultStrOrError(pCtx, zText, pStr->nChar, 779 SQLITE_UTF8, SQLITE_TRANSIENT); 780 }else{ 781 setResultStrOrError(pCtx, zText, pStr->nChar, 782 SQLITE_UTF8_ZT, SQLITE_DYNAMIC); 783 sqlite3StrAccumInit(pStr, pStr->db, 0, 0, pStr->mxAlloc); 784 } 785 } 786 }else if( pStr->accError==SQLITE_NOMEM ){ 787 sqlite3_result_error_nomem(pCtx); 788 }else{ 789 assert( pStr->accError==SQLITE_TOOBIG ); 790 sqlite3_result_error_toobig(pCtx); 791 } 792 if( eOwn==SQLITE_FINISH ){ 793 sqlite3_str_free(pStr); 794 } 795 } 796 797 #ifndef SQLITE_UNTESTABLE 798 /* Force the INT64 value currently stored as the result to be 799 ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL 800 ** test-control. 801 */ 802 void sqlite3ResultIntReal(sqlite3_context *pCtx){ 803 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 804 if( pCtx->pOut->flags & MEM_Int ){ 805 pCtx->pOut->flags &= ~MEM_Int; 806 pCtx->pOut->flags |= MEM_IntReal; 807 } 808 } 809 #endif 810 811 812 /* 813 ** This function is called after a transaction has been committed. It 814 ** invokes callbacks registered with sqlite3_wal_hook() as required. 815 */ 816 static int doWalCallbacks(sqlite3 *db){ 817 int rc = SQLITE_OK; 818 #ifndef SQLITE_OMIT_WAL 819 int i; 820 for(i=0; inDb; i++){ 821 Btree *pBt = db->aDb[i].pBt; 822 if( pBt ){ 823 int nEntry; 824 sqlite3BtreeEnter(pBt); 825 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 826 sqlite3BtreeLeave(pBt); 827 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ 828 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); 829 } 830 } 831 } 832 #else 833 UNUSED_PARAMETER(db); 834 #endif 835 return rc; 836 } 837 838 839 /* 840 ** Execute the statement pStmt, either until a row of data is ready, the 841 ** statement is completely executed or an error occurs. 842 ** 843 ** This routine implements the bulk of the logic behind the sqlite_step() 844 ** API. The only thing omitted is the automatic recompile if a 845 ** schema change has occurred. That detail is handled by the 846 ** outer sqlite3_step() wrapper procedure. 847 */ 848 static int sqlite3Step(Vdbe *p){ 849 sqlite3 *db; 850 int rc; 851 852 assert(p); 853 db = p->db; 854 if( p->eVdbeState!=VDBE_RUN_STATE ){ 855 restart_step: 856 if( p->eVdbeState==VDBE_READY_STATE ){ 857 if( p->expired ){ 858 p->rc = SQLITE_SCHEMA; 859 rc = SQLITE_ERROR; 860 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 861 /* If this statement was prepared using saved SQL and an 862 ** error has occurred, then return the error code in p->rc to the 863 ** caller. Set the error code in the database handle to the same 864 ** value. 865 */ 866 rc = sqlite3VdbeTransferError(p); 867 } 868 goto end_of_step; 869 } 870 871 /* If there are no other statements currently running, then 872 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 873 ** from interrupting a statement that has not yet started. 874 */ 875 if( db->nVdbeActive==0 ){ 876 AtomicStore(&db->u1.isInterrupted, 0); 877 } 878 879 assert( db->nVdbeWrite>0 || db->autoCommit==0 880 || ((db->nDeferredCons + db->nDeferredImmCons)==0) 881 ); 882 883 #ifndef SQLITE_OMIT_TRACE 884 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 885 && !db->init.busy && p->zSql ){ 886 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 887 }else{ 888 assert( p->startTime==0 ); 889 } 890 #endif 891 892 db->nVdbeActive++; 893 if( p->readOnly==0 ) db->nVdbeWrite++; 894 if( p->bIsReader ) db->nVdbeRead++; 895 p->pc = 0; 896 p->eVdbeState = VDBE_RUN_STATE; 897 }else 898 899 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){ 900 /* We used to require that sqlite3_reset() be called before retrying 901 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 902 ** with version 3.7.0, we changed this so that sqlite3_reset() would 903 ** be called automatically instead of throwing the SQLITE_MISUSE error. 904 ** This "automatic-reset" change is not technically an incompatibility, 905 ** since any application that receives an SQLITE_MISUSE is broken by 906 ** definition. 907 ** 908 ** Nevertheless, some published applications that were originally written 909 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 910 ** returns, and those were broken by the automatic-reset change. As a 911 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 912 ** legacy behavior of returning SQLITE_MISUSE for cases where the 913 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 914 ** or SQLITE_BUSY error. 915 */ 916 #ifdef SQLITE_OMIT_AUTORESET 917 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 918 sqlite3_reset((sqlite3_stmt*)p); 919 }else{ 920 return SQLITE_MISUSE_BKPT; 921 } 922 #else 923 sqlite3_reset((sqlite3_stmt*)p); 924 #endif 925 assert( p->eVdbeState==VDBE_READY_STATE ); 926 goto restart_step; 927 } 928 } 929 930 #ifdef SQLITE_DEBUG 931 p->rcApp = SQLITE_OK; 932 #endif 933 #ifndef SQLITE_OMIT_EXPLAIN 934 if( p->explain ){ 935 rc = sqlite3VdbeList(p); 936 }else 937 #endif /* SQLITE_OMIT_EXPLAIN */ 938 { 939 db->nVdbeExec++; 940 rc = sqlite3VdbeExec(p); 941 db->nVdbeExec--; 942 } 943 944 if( rc==SQLITE_ROW ){ 945 assert( p->rc==SQLITE_OK ); 946 assert( db->mallocFailed==0 ); 947 db->errCode = SQLITE_ROW; 948 return SQLITE_ROW; 949 }else{ 950 #ifndef SQLITE_OMIT_TRACE 951 /* If the statement completed successfully, invoke the profile callback */ 952 checkProfileCallback(db, p); 953 #endif 954 p->pResultRow = 0; 955 if( rc==SQLITE_DONE && db->autoCommit ){ 956 assert( p->rc==SQLITE_OK ); 957 p->rc = doWalCallbacks(db); 958 if( p->rc!=SQLITE_OK ){ 959 rc = SQLITE_ERROR; 960 } 961 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 962 /* If this statement was prepared using saved SQL and an 963 ** error has occurred, then return the error code in p->rc to the 964 ** caller. Set the error code in the database handle to the same value. 965 */ 966 rc = sqlite3VdbeTransferError(p); 967 } 968 } 969 970 db->errCode = rc; 971 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 972 p->rc = SQLITE_NOMEM_BKPT; 973 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; 974 } 975 end_of_step: 976 /* There are only a limited number of result codes allowed from the 977 ** statements prepared using the legacy sqlite3_prepare() interface */ 978 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 979 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 980 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 981 ); 982 return (rc&db->errMask); 983 } 984 985 /* 986 ** This is the top-level implementation of sqlite3_step(). Call 987 ** sqlite3Step() to do most of the work. If a schema error occurs, 988 ** call sqlite3Reprepare() and try again. 989 */ 990 int sqlite3_step(sqlite3_stmt *pStmt){ 991 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 992 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 993 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 994 sqlite3 *db; /* The database connection */ 995 996 if( vdbeSafetyNotNull(v) ){ 997 return SQLITE_MISUSE_BKPT; 998 } 999 db = v->db; 1000 sqlite3_mutex_enter(db->mutex); 1001 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 1002 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 1003 int savedPc = v->pc; 1004 rc = sqlite3Reprepare(v); 1005 if( rc!=SQLITE_OK ){ 1006 /* This case occurs after failing to recompile an sql statement. 1007 ** The error message from the SQL compiler has already been loaded 1008 ** into the database handle. This block copies the error message 1009 ** from the database handle into the statement and sets the statement 1010 ** program counter to 0 to ensure that when the statement is 1011 ** finalized or reset the parser error message is available via 1012 ** sqlite3_errmsg() and sqlite3_errcode(). 1013 */ 1014 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 1015 sqlite3DbFree(db, v->zErrMsg); 1016 if( !db->mallocFailed ){ 1017 v->zErrMsg = sqlite3DbStrDup(db, zErr); 1018 v->rc = rc = sqlite3ApiExit(db, rc); 1019 } else { 1020 v->zErrMsg = 0; 1021 v->rc = rc = SQLITE_NOMEM_BKPT; 1022 } 1023 break; 1024 } 1025 sqlite3_reset(pStmt); 1026 if( savedPc>=0 ){ 1027 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and 1028 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has 1029 ** already been done once on a prior invocation that failed due to 1030 ** SQLITE_SCHEMA. tag-`20220401a` */ 1031 v->minWriteFileFormat = 254; 1032 } 1033 assert( v->expired==0 ); 1034 } 1035 sqlite3_mutex_leave(db->mutex); 1036 return rc; 1037 } 1038 1039 1040 /* 1041 ** Extract the user data from a sqlite3_context structure and return a 1042 ** pointer to it. 1043 */ 1044 void *sqlite3_user_data(sqlite3_context *p){ 1045 #ifdef SQLITE_ENABLE_API_ARMOR 1046 if( p==0 ) return 0; 1047 #endif 1048 assert( p && p->pFunc ); 1049 return p->pFunc->pUserData; 1050 } 1051 1052 /* 1053 ** Extract the user data from a sqlite3_context structure and return a 1054 ** pointer to it. 1055 ** 1056 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 1057 ** returns a copy of the pointer to the database connection (the 1st 1058 ** parameter) of the sqlite3_create_function() and 1059 ** sqlite3_create_function16() routines that originally registered the 1060 ** application defined function. 1061 */ 1062 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 1063 #ifdef SQLITE_ENABLE_API_ARMOR 1064 if( p==0 ) return 0; 1065 #else 1066 assert( p && p->pOut ); 1067 #endif 1068 return p->pOut->db; 1069 } 1070 1071 /* 1072 ** If this routine is invoked from within an xColumn method of a virtual 1073 ** table, then it returns true if and only if the the call is during an 1074 ** UPDATE operation and the value of the column will not be modified 1075 ** by the UPDATE. 1076 ** 1077 ** If this routine is called from any context other than within the 1078 ** xColumn method of a virtual table, then the return value is meaningless 1079 ** and arbitrary. 1080 ** 1081 ** Virtual table implements might use this routine to optimize their 1082 ** performance by substituting a NULL result, or some other light-weight 1083 ** value, as a signal to the xUpdate routine that the column is unchanged. 1084 */ 1085 int sqlite3_vtab_nochange(sqlite3_context *p){ 1086 #ifdef SQLITE_ENABLE_API_ARMOR 1087 if( p==0 ) return 0; 1088 #else 1089 assert( p ); 1090 #endif 1091 return sqlite3_value_nochange(p->pOut); 1092 } 1093 1094 /* 1095 ** The destructor function for a ValueList object. This needs to be 1096 ** a separate function, unknowable to the application, to ensure that 1097 ** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not 1098 ** preceded by activation of IN processing via sqlite3_vtab_int() do not 1099 ** try to access a fake ValueList object inserted by a hostile extension. 1100 */ 1101 void sqlite3VdbeValueListFree(void *pToDelete){ 1102 sqlite3_free(pToDelete); 1103 } 1104 1105 /* 1106 ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and 1107 ** sqlite3_vtab_in_next() (if bNext!=0). 1108 */ 1109 static int valueFromValueList( 1110 sqlite3_value *pVal, /* Pointer to the ValueList object */ 1111 sqlite3_value **ppOut, /* Store the next value from the list here */ 1112 int bNext /* 1 for _next(). 0 for _first() */ 1113 ){ 1114 int rc; 1115 ValueList *pRhs; 1116 1117 *ppOut = 0; 1118 if( pVal==0 ) return SQLITE_MISUSE_BKPT; 1119 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){ 1120 return SQLITE_ERROR; 1121 }else{ 1122 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 1123 (MEM_Null|MEM_Term|MEM_Subtype) ); 1124 assert( pVal->eSubtype=='p' ); 1125 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 ); 1126 pRhs = (ValueList*)pVal->z; 1127 } 1128 if( bNext ){ 1129 rc = sqlite3BtreeNext(pRhs->pCsr, 0); 1130 }else{ 1131 int dummy = 0; 1132 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); 1133 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); 1134 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; 1135 } 1136 if( rc==SQLITE_OK ){ 1137 u32 sz; /* Size of current row in bytes */ 1138 Mem sMem; /* Raw content of current row */ 1139 memset(&sMem, 0, sizeof(sMem)); 1140 sz = sqlite3BtreePayloadSize(pRhs->pCsr); 1141 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,sz,&sMem); 1142 if( rc==SQLITE_OK ){ 1143 u8 *zBuf = (u8*)sMem.z; 1144 u32 iSerial; 1145 sqlite3_value *pOut = pRhs->pOut; 1146 int iOff = 1 + getVarint32(&zBuf[1], iSerial); 1147 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); 1148 pOut->enc = ENC(pOut->db); 1149 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ 1150 rc = SQLITE_NOMEM; 1151 }else{ 1152 *ppOut = pOut; 1153 } 1154 } 1155 sqlite3VdbeMemRelease(&sMem); 1156 } 1157 return rc; 1158 } 1159 1160 /* 1161 ** Set the iterator value pVal to point to the first value in the set. 1162 ** Set (*ppOut) to point to this value before returning. 1163 */ 1164 int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ 1165 return valueFromValueList(pVal, ppOut, 0); 1166 } 1167 1168 /* 1169 ** Set the iterator value pVal to point to the next value in the set. 1170 ** Set (*ppOut) to point to this value before returning. 1171 */ 1172 int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ 1173 return valueFromValueList(pVal, ppOut, 1); 1174 } 1175 1176 /* 1177 ** Return the current time for a statement. If the current time 1178 ** is requested more than once within the same run of a single prepared 1179 ** statement, the exact same time is returned for each invocation regardless 1180 ** of the amount of time that elapses between invocations. In other words, 1181 ** the time returned is always the time of the first call. 1182 */ 1183 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 1184 int rc; 1185 #ifndef SQLITE_ENABLE_STAT4 1186 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 1187 assert( p->pVdbe!=0 ); 1188 #else 1189 sqlite3_int64 iTime = 0; 1190 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 1191 #endif 1192 if( *piTime==0 ){ 1193 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 1194 if( rc ) *piTime = 0; 1195 } 1196 return *piTime; 1197 } 1198 1199 /* 1200 ** Create a new aggregate context for p and return a pointer to 1201 ** its pMem->z element. 1202 */ 1203 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 1204 Mem *pMem = p->pMem; 1205 assert( (pMem->flags & MEM_Agg)==0 ); 1206 if( nByte<=0 ){ 1207 sqlite3VdbeMemSetNull(pMem); 1208 pMem->z = 0; 1209 }else{ 1210 sqlite3VdbeMemClearAndResize(pMem, nByte); 1211 pMem->flags = MEM_Agg; 1212 pMem->u.pDef = p->pFunc; 1213 if( pMem->z ){ 1214 memset(pMem->z, 0, nByte); 1215 } 1216 } 1217 return (void*)pMem->z; 1218 } 1219 1220 /* 1221 ** Allocate or return the aggregate context for a user function. A new 1222 ** context is allocated on the first call. Subsequent calls return the 1223 ** same context that was returned on prior calls. 1224 */ 1225 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 1226 assert( p && p->pFunc && p->pFunc->xFinalize ); 1227 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 1228 testcase( nByte<0 ); 1229 if( (p->pMem->flags & MEM_Agg)==0 ){ 1230 return createAggContext(p, nByte); 1231 }else{ 1232 return (void*)p->pMem->z; 1233 } 1234 } 1235 1236 /* 1237 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 1238 ** the user-function defined by pCtx. 1239 ** 1240 ** The left-most argument is 0. 1241 ** 1242 ** Undocumented behavior: If iArg is negative then access a cache of 1243 ** auxiliary data pointers that is available to all functions within a 1244 ** single prepared statement. The iArg values must match. 1245 */ 1246 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 1247 AuxData *pAuxData; 1248 1249 #ifdef SQLITE_ENABLE_API_ARMOR 1250 if( pCtx==0 ) return 0; 1251 #endif 1252 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 1253 #if SQLITE_ENABLE_STAT4 1254 if( pCtx->pVdbe==0 ) return 0; 1255 #else 1256 assert( pCtx->pVdbe!=0 ); 1257 #endif 1258 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 1259 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 1260 return pAuxData->pAux; 1261 } 1262 } 1263 return 0; 1264 } 1265 1266 /* 1267 ** Set the auxiliary data pointer and delete function, for the iArg'th 1268 ** argument to the user-function defined by pCtx. Any previous value is 1269 ** deleted by calling the delete function specified when it was set. 1270 ** 1271 ** The left-most argument is 0. 1272 ** 1273 ** Undocumented behavior: If iArg is negative then make the data available 1274 ** to all functions within the current prepared statement using iArg as an 1275 ** access code. 1276 */ 1277 void sqlite3_set_auxdata( 1278 sqlite3_context *pCtx, 1279 int iArg, 1280 void *pAux, 1281 void (*xDelete)(void*) 1282 ){ 1283 AuxData *pAuxData; 1284 Vdbe *pVdbe; 1285 1286 #ifdef SQLITE_ENABLE_API_ARMOR 1287 if( pCtx==0 ) return; 1288 #endif 1289 pVdbe= pCtx->pVdbe; 1290 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 1291 #ifdef SQLITE_ENABLE_STAT4 1292 if( pVdbe==0 ) goto failed; 1293 #else 1294 assert( pVdbe!=0 ); 1295 #endif 1296 1297 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 1298 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 1299 break; 1300 } 1301 } 1302 if( pAuxData==0 ){ 1303 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 1304 if( !pAuxData ) goto failed; 1305 pAuxData->iAuxOp = pCtx->iOp; 1306 pAuxData->iAuxArg = iArg; 1307 pAuxData->pNextAux = pVdbe->pAuxData; 1308 pVdbe->pAuxData = pAuxData; 1309 if( pCtx->isError==0 ) pCtx->isError = -1; 1310 }else if( pAuxData->xDeleteAux ){ 1311 pAuxData->xDeleteAux(pAuxData->pAux); 1312 } 1313 1314 pAuxData->pAux = pAux; 1315 pAuxData->xDeleteAux = xDelete; 1316 return; 1317 1318 failed: 1319 if( xDelete ){ 1320 xDelete(pAux); 1321 } 1322 } 1323 1324 #ifndef SQLITE_OMIT_DEPRECATED 1325 /* 1326 ** Return the number of times the Step function of an aggregate has been 1327 ** called. 1328 ** 1329 ** This function is deprecated. Do not use it for new code. It is 1330 ** provide only to avoid breaking legacy code. New aggregate function 1331 ** implementations should keep their own counts within their aggregate 1332 ** context. 1333 */ 1334 int sqlite3_aggregate_count(sqlite3_context *p){ 1335 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 1336 return p->pMem->n; 1337 } 1338 #endif 1339 1340 /* 1341 ** Return the number of columns in the result set for the statement pStmt. 1342 */ 1343 int sqlite3_column_count(sqlite3_stmt *pStmt){ 1344 Vdbe *pVm = (Vdbe *)pStmt; 1345 if( pVm==0 ) return 0; 1346 return pVm->nResColumn; 1347 } 1348 1349 /* 1350 ** Return the number of values available from the current row of the 1351 ** currently executing statement pStmt. 1352 */ 1353 int sqlite3_data_count(sqlite3_stmt *pStmt){ 1354 Vdbe *pVm = (Vdbe *)pStmt; 1355 if( pVm==0 || pVm->pResultRow==0 ) return 0; 1356 return pVm->nResColumn; 1357 } 1358 1359 /* 1360 ** Return a pointer to static memory containing an SQL NULL value. 1361 */ 1362 static const Mem *columnNullValue(void){ 1363 /* Even though the Mem structure contains an element 1364 ** of type i64, on certain architectures (x86) with certain compiler 1365 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 1366 ** instead of an 8-byte one. This all works fine, except that when 1367 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 1368 ** that a Mem structure is located on an 8-byte boundary. To prevent 1369 ** these assert()s from failing, when building with SQLITE_DEBUG defined 1370 ** using gcc, we force nullMem to be 8-byte aligned using the magical 1371 ** __attribute__((aligned(8))) macro. */ 1372 static const Mem nullMem 1373 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 1374 __attribute__((aligned(8))) 1375 #endif 1376 = { 1377 /* .u = */ {0}, 1378 /* .z = */ (char*)0, 1379 /* .n = */ (int)0, 1380 /* .flags = */ (u16)MEM_Null, 1381 /* .enc = */ (u8)0, 1382 /* .eSubtype = */ (u8)0, 1383 /* .db = */ (sqlite3*)0, 1384 /* .szMalloc = */ (int)0, 1385 /* .uTemp = */ (u32)0, 1386 /* .zMalloc = */ (char*)0, 1387 /* .xDel = */ (void(*)(void*))0, 1388 #ifdef SQLITE_DEBUG 1389 /* .pScopyFrom = */ (Mem*)0, 1390 /* .mScopyFlags= */ 0, 1391 /* .bScopy = */ 0, 1392 #endif 1393 }; 1394 return &nullMem; 1395 } 1396 1397 /* 1398 ** Check to see if column iCol of the given statement is valid. If 1399 ** it is, return a pointer to the Mem for the value of that column. 1400 ** If iCol is not valid, return a pointer to a Mem which has a value 1401 ** of NULL. 1402 */ 1403 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 1404 Vdbe *pVm; 1405 Mem *pOut; 1406 1407 pVm = (Vdbe *)pStmt; 1408 if( pVm==0 ) return (Mem*)columnNullValue(); 1409 assert( pVm->db ); 1410 sqlite3_mutex_enter(pVm->db->mutex); 1411 if( pVm->pResultRow!=0 && inResColumn && i>=0 ){ 1412 pOut = &pVm->pResultRow[i]; 1413 }else{ 1414 sqlite3Error(pVm->db, SQLITE_RANGE); 1415 pOut = (Mem*)columnNullValue(); 1416 } 1417 return pOut; 1418 } 1419 1420 /* 1421 ** This function is called after invoking an sqlite3_value_XXX function on a 1422 ** column value (i.e. a value returned by evaluating an SQL expression in the 1423 ** select list of a SELECT statement) that may cause a malloc() failure. If 1424 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 1425 ** code of statement pStmt set to SQLITE_NOMEM. 1426 ** 1427 ** Specifically, this is called from within: 1428 ** 1429 ** sqlite3_column_int() 1430 ** sqlite3_column_int64() 1431 ** sqlite3_column_text() 1432 ** sqlite3_column_text16() 1433 ** sqlite3_column_double() 1434 ** sqlite3_column_bytes() 1435 ** sqlite3_column_bytes16() 1436 ** sqlite3_column_blob() 1437 */ 1438 static void columnMallocFailure(sqlite3_stmt *pStmt) 1439 { 1440 /* If malloc() failed during an encoding conversion within an 1441 ** sqlite3_column_XXX API, then set the return code of the statement to 1442 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 1443 ** and _finalize() will return NOMEM. 1444 */ 1445 Vdbe *p = (Vdbe *)pStmt; 1446 if( p ){ 1447 assert( p->db!=0 ); 1448 assert( sqlite3_mutex_held(p->db->mutex) ); 1449 p->rc = sqlite3ApiExit(p->db, p->rc); 1450 sqlite3_mutex_leave(p->db->mutex); 1451 } 1452 } 1453 1454 /**************************** sqlite3_column_ ******************************* 1455 ** The following routines are used to access elements of the current row 1456 ** in the result set. 1457 */ 1458 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 1459 const void *val; 1460 val = sqlite3_value_blob( columnMem(pStmt,i) ); 1461 /* Even though there is no encoding conversion, value_blob() might 1462 ** need to call malloc() to expand the result of a zeroblob() 1463 ** expression. 1464 */ 1465 columnMallocFailure(pStmt); 1466 return val; 1467 } 1468 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 1469 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 1470 columnMallocFailure(pStmt); 1471 return val; 1472 } 1473 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 1474 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 1475 columnMallocFailure(pStmt); 1476 return val; 1477 } 1478 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 1479 double val = sqlite3_value_double( columnMem(pStmt,i) ); 1480 columnMallocFailure(pStmt); 1481 return val; 1482 } 1483 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 1484 int val = sqlite3_value_int( columnMem(pStmt,i) ); 1485 columnMallocFailure(pStmt); 1486 return val; 1487 } 1488 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 1489 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 1490 columnMallocFailure(pStmt); 1491 return val; 1492 } 1493 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 1494 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 1495 columnMallocFailure(pStmt); 1496 return val; 1497 } 1498 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 1499 Mem *pOut = columnMem(pStmt, i); 1500 if( pOut->flags&MEM_Static ){ 1501 pOut->flags &= ~MEM_Static; 1502 pOut->flags |= MEM_Ephem; 1503 } 1504 columnMallocFailure(pStmt); 1505 return (sqlite3_value *)pOut; 1506 } 1507 #ifndef SQLITE_OMIT_UTF16 1508 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 1509 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 1510 columnMallocFailure(pStmt); 1511 return val; 1512 } 1513 #endif /* SQLITE_OMIT_UTF16 */ 1514 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 1515 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 1516 columnMallocFailure(pStmt); 1517 return iType; 1518 } 1519 1520 /* 1521 ** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN. 1522 */ 1523 static const char * const azExplainColNames8[] = { 1524 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */ 1525 "id", "parent", "notused", "detail" /* EQP */ 1526 }; 1527 static const u16 azExplainColNames16data[] = { 1528 /* 0 */ 'a', 'd', 'd', 'r', 0, 1529 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0, 1530 /* 12 */ 'p', '1', 0, 1531 /* 15 */ 'p', '2', 0, 1532 /* 18 */ 'p', '3', 0, 1533 /* 21 */ 'p', '4', 0, 1534 /* 24 */ 'p', '5', 0, 1535 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, 1536 /* 35 */ 'i', 'd', 0, 1537 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0, 1538 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0, 1539 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0 1540 }; 1541 static const u8 iExplainColNames16[] = { 1542 0, 5, 12, 15, 18, 21, 24, 27, 1543 35, 38, 45, 53 1544 }; 1545 1546 /* 1547 ** Convert the N-th element of pStmt->pColName[] into a string using 1548 ** xFunc() then return that string. If N is out of range, return 0. 1549 ** 1550 ** There are up to 5 names for each column. useType determines which 1551 ** name is returned. Here are the names: 1552 ** 1553 ** 0 The column name as it should be displayed for output 1554 ** 1 The datatype name for the column 1555 ** 2 The name of the database that the column derives from 1556 ** 3 The name of the table that the column derives from 1557 ** 4 The name of the table column that the result column derives from 1558 ** 1559 ** If the result is not a simple column reference (if it is an expression 1560 ** or a constant) then useTypes 2, 3, and 4 return NULL. 1561 */ 1562 static const void *columnName( 1563 sqlite3_stmt *pStmt, /* The statement */ 1564 int N, /* Which column to get the name for */ 1565 int useUtf16, /* True to return the name as UTF16 */ 1566 int useType /* What type of name */ 1567 ){ 1568 const void *ret; 1569 Vdbe *p; 1570 int n; 1571 sqlite3 *db; 1572 #ifdef SQLITE_ENABLE_API_ARMOR 1573 if( pStmt==0 ){ 1574 (void)SQLITE_MISUSE_BKPT; 1575 return 0; 1576 } 1577 #endif 1578 if( N<0 ) return 0; 1579 ret = 0; 1580 p = (Vdbe *)pStmt; 1581 db = p->db; 1582 assert( db!=0 ); 1583 sqlite3_mutex_enter(db->mutex); 1584 1585 if( p->explain ){ 1586 if( useType>0 ) goto columnName_end; 1587 n = p->explain==1 ? 8 : 4; 1588 if( N>=n ) goto columnName_end; 1589 if( useUtf16 ){ 1590 int i = iExplainColNames16[N + 8*p->explain - 8]; 1591 ret = (void*)&azExplainColNames16data[i]; 1592 }else{ 1593 ret = (void*)azExplainColNames8[N + 8*p->explain - 8]; 1594 } 1595 goto columnName_end; 1596 } 1597 n = p->nResColumn; 1598 if( NmallocFailed; 1600 N += useType*n; 1601 #ifndef SQLITE_OMIT_UTF16 1602 if( useUtf16 ){ 1603 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); 1604 }else 1605 #endif 1606 { 1607 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); 1608 } 1609 /* A malloc may have failed inside of the _text() call. If this 1610 ** is the case, clear the mallocFailed flag and return NULL. 1611 */ 1612 assert( db->mallocFailed==0 || db->mallocFailed==1 ); 1613 if( db->mallocFailed > prior_mallocFailed ){ 1614 sqlite3OomClear(db); 1615 ret = 0; 1616 } 1617 } 1618 columnName_end: 1619 sqlite3_mutex_leave(db->mutex); 1620 return ret; 1621 } 1622 1623 /* 1624 ** Return the name of the Nth column of the result set returned by SQL 1625 ** statement pStmt. 1626 */ 1627 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 1628 return columnName(pStmt, N, 0, COLNAME_NAME); 1629 } 1630 #ifndef SQLITE_OMIT_UTF16 1631 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 1632 return columnName(pStmt, N, 1, COLNAME_NAME); 1633 } 1634 #endif 1635 1636 /* 1637 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 1638 ** not define OMIT_DECLTYPE. 1639 */ 1640 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 1641 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 1642 and SQLITE_ENABLE_COLUMN_METADATA" 1643 #endif 1644 1645 #ifndef SQLITE_OMIT_DECLTYPE 1646 /* 1647 ** Return the column declaration type (if applicable) of the 'i'th column 1648 ** of the result set of SQL statement pStmt. 1649 */ 1650 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 1651 return columnName(pStmt, N, 0, COLNAME_DECLTYPE); 1652 } 1653 #ifndef SQLITE_OMIT_UTF16 1654 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 1655 return columnName(pStmt, N, 1, COLNAME_DECLTYPE); 1656 } 1657 #endif /* SQLITE_OMIT_UTF16 */ 1658 #endif /* SQLITE_OMIT_DECLTYPE */ 1659 1660 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1661 /* 1662 ** Return the name of the database from which a result column derives. 1663 ** NULL is returned if the result column is an expression or constant or 1664 ** anything else which is not an unambiguous reference to a database column. 1665 */ 1666 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1667 return columnName(pStmt, N, 0, COLNAME_DATABASE); 1668 } 1669 #ifndef SQLITE_OMIT_UTF16 1670 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1671 return columnName(pStmt, N, 1, COLNAME_DATABASE); 1672 } 1673 #endif /* SQLITE_OMIT_UTF16 */ 1674 1675 /* 1676 ** Return the name of the table from which a result column derives. 1677 ** NULL is returned if the result column is an expression or constant or 1678 ** anything else which is not an unambiguous reference to a database column. 1679 */ 1680 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1681 return columnName(pStmt, N, 0, COLNAME_TABLE); 1682 } 1683 #ifndef SQLITE_OMIT_UTF16 1684 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1685 return columnName(pStmt, N, 1, COLNAME_TABLE); 1686 } 1687 #endif /* SQLITE_OMIT_UTF16 */ 1688 1689 /* 1690 ** Return the name of the table column from which a result column derives. 1691 ** NULL is returned if the result column is an expression or constant or 1692 ** anything else which is not an unambiguous reference to a database column. 1693 */ 1694 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1695 return columnName(pStmt, N, 0, COLNAME_COLUMN); 1696 } 1697 #ifndef SQLITE_OMIT_UTF16 1698 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1699 return columnName(pStmt, N, 1, COLNAME_COLUMN); 1700 } 1701 #endif /* SQLITE_OMIT_UTF16 */ 1702 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1703 1704 1705 /******************************* sqlite3_bind_ *************************** 1706 ** 1707 ** Routines used to attach values to wildcards in a compiled SQL statement. 1708 */ 1709 /* 1710 ** Unbind the value bound to variable i in virtual machine p. This is the 1711 ** the same as binding a NULL value to the column. If the "i" parameter is 1712 ** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK. 1713 ** 1714 ** A successful evaluation of this routine acquires the mutex on p. 1715 ** the mutex is released if any kind of error occurs. 1716 ** 1717 ** The error code stored in database p->db is overwritten with the return 1718 ** value in any case. 1719 ** 1720 ** (tag-`20240917`-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK, 1721 ** that means all of the the following will be true: 1722 ** 1723 ** p!=0 1724 ** p->pVar!=0 1725 ** i>0 1726 ** i<=p->nVar 1727 ** 1728 ** An assert() is normally added after vdbeUnbind() to help static analyzers 1729 ** realize this. 1730 */ 1731 static int vdbeUnbind(Vdbe *p, unsigned int i){ 1732 Mem *pVar; 1733 if( vdbeSafetyNotNull(p) ){ 1734 return SQLITE_MISUSE_BKPT; 1735 } 1736 sqlite3_mutex_enter(p->db->mutex); 1737 if( p->eVdbeState!=VDBE_READY_STATE ){ 1738 sqlite3Error(p->db, SQLITE_MISUSE_BKPT); 1739 sqlite3_mutex_leave(p->db->mutex); 1740 sqlite3_log(SQLITE_MISUSE, 1741 "bind on a busy prepared statement: [%s]", p->zSql); 1742 return SQLITE_MISUSE_BKPT; 1743 } 1744 if( i>=(unsigned int)p->nVar ){ 1745 sqlite3Error(p->db, SQLITE_RANGE); 1746 sqlite3_mutex_leave(p->db->mutex); 1747 return SQLITE_RANGE; 1748 } 1749 1750 pVar = &p->aVar[i]; 1751 1752 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 1753 ** binding a new value to this variable invalidates the current query plan. 1754 ** 1755 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host 1756 ** parameter in the WHERE clause might influence the choice of query plan 1757 ** for a statement, then the statement will be automatically recompiled, 1758 ** as if there had been a schema change, on the first sqlite3_step() call 1759 ** following any change to the bindings of that parameter. 1760 */ 1761 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 1762 assert( (p->expmask & p->smimask)==p->smimask ); 1763 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<smimask bit is also set, then only expire the statement if 1765 ** the value being clobbered was integer value 0 or 1. */ 1766 if( (p->smimask==0) 1767 || (p->smimask & (i>=31 ? 0x80000000 : (u32)1<flags & MEM_Int)!=0 && pVar->u.i>=0 && pVar->u.i<=1) 1769 ){ 1770 p->expired = 1; 1771 } 1772 } 1773 1774 sqlite3VdbeMemRelease(pVar); 1775 pVar->flags = MEM_Null; 1776 p->db->errCode = SQLITE_OK; 1777 1778 return SQLITE_OK; 1779 } 1780 1781 /* 1782 ** Bind a text or BLOB value. 1783 */ 1784 static int bindText( 1785 sqlite3_stmt *pStmt, /* The statement to bind against */ 1786 int i, /* Index of the parameter to bind */ 1787 const void *zData, /* Pointer to the data to be bound */ 1788 i64 nData, /* Number of bytes of data to be bound */ 1789 void (*xDel)(void*), /* Destructor for the data */ 1790 u8 encoding /* Encoding for the data + optional ENC_KEEPBOM */ 1791 ){ 1792 Vdbe *p = (Vdbe *)pStmt; 1793 Mem *pVar; 1794 int rc; 1795 1796 assert( (encoding&ENC_KEEPBOM)==0 || (encoding&ENC_MASK)>SQLITE_UTF8 ); 1797 rc = vdbeUnbind(p, (u32)(i-1)); 1798 if( rc==SQLITE_OK ){ 1799 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-`20240917`-01 */ 1800 if( zData!=0 ){ 1801 pVar = &p->aVar[i-1]; 1802 if( encoding==SQLITE_UTF8 ){ 1803 rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel); 1804 }else if( encoding==SQLITE_UTF8_ZT ){ 1805 /* It is usually consider improper to assert() on an input. 1806 ** However, the following assert() is checking for inputs 1807 ** that are documented to result in undefined behavior. */ 1808 assert( zData==0 1809 || nData<0 1810 || nData>pVar->db->aLimit[SQLITE_LIMIT_LENGTH] 1811 || ((u8*)zData)[nData]==0 1812 ); 1813 rc = sqlite3VdbeMemSetText(pVar, zData, nData, xDel); 1814 pVar->flags |= MEM_Term; 1815 }else{ 1816 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 1817 if( encoding==0 ) pVar->enc = ENC(p->db); 1818 } 1819 if( rc==SQLITE_OK && encoding!=0 ){ 1820 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 1821 } 1822 if( rc ){ 1823 sqlite3Error(p->db, rc); 1824 rc = sqlite3ApiExit(p->db, rc); 1825 } 1826 } 1827 sqlite3_mutex_leave(p->db->mutex); 1828 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 1829 xDel((void*)zData); 1830 } 1831 return rc; 1832 } 1833 1834 1835 /* 1836 ** Bind a blob value to an SQL statement variable. 1837 */ 1838 int sqlite3_bind_blob( 1839 sqlite3_stmt *pStmt, 1840 int i, 1841 const void *zData, 1842 int nData, 1843 void (*xDel)(void*) 1844 ){ 1845 #ifdef SQLITE_ENABLE_API_ARMOR 1846 if( nData<0 ) return SQLITE_MISUSE_BKPT; 1847 #endif 1848 return bindText(pStmt, i, zData, nData, xDel, 0); 1849 } 1850 int sqlite3_bind_blob64( 1851 sqlite3_stmt *pStmt, 1852 int i, 1853 const void *zData, 1854 sqlite3_uint64 nData, 1855 void (*xDel)(void*) 1856 ){ 1857 assert( xDel!=SQLITE_DYNAMIC ); 1858 return bindText(pStmt, i, zData, nData, xDel, 0); 1859 } 1860 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 1861 int rc; 1862 Vdbe *p = (Vdbe *)pStmt; 1863 rc = vdbeUnbind(p, (u32)(i-1)); 1864 if( rc==SQLITE_OK ){ 1865 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-`20240917`-01 */ 1866 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1867 sqlite3_mutex_leave(p->db->mutex); 1868 } 1869 return rc; 1870 } 1871 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1872 return sqlite3_bind_int64(p, i, (i64)iValue); 1873 } 1874 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 1875 Mem *pVar; 1876 Vdbe *p = (Vdbe*)pStmt; 1877 if( vdbeSafetyNotNull(p) ){ 1878 return SQLITE_MISUSE_BKPT; 1879 } 1880 sqlite3_mutex_enter(p->db->mutex); 1881 if( p->eVdbeState!=VDBE_READY_STATE ){ 1882 sqlite3Error(p->db, SQLITE_MISUSE_BKPT); 1883 sqlite3_mutex_leave(p->db->mutex); 1884 sqlite3_log(SQLITE_MISUSE, 1885 "bind on a busy prepared statement: [%s]", p->zSql); 1886 return SQLITE_MISUSE_BKPT; 1887 } 1888 if( i<=0 || (--i)>=p->nVar ){ 1889 sqlite3Error(p->db, SQLITE_RANGE); 1890 sqlite3_mutex_leave(p->db->mutex); 1891 return SQLITE_RANGE; 1892 } 1893 pVar = &p->aVar[i]; 1894 if( p->expmask!=0 ){ 1895 u32 expireMask = i>=32 ? 0x80000000 : (u32)1 << i; 1896 assert( (p->expmask & p->smimask)==p->smimask ); 1897 if( (p->expmask & expireMask)!=0 ){ 1898 if( (p->smimask & expireMask)!=0 ){ 1899 /* If the smimask bit is set, only expire the prepared statement 1900 ** if the value is changing to or from (0,1) and something else */ 1901 int sm1 = (pVar->flags & MEM_Int)!=0 && pVar->u.i>=0 && pVar->u.i<=1; 1902 int sm2 = iValue>=0 && iValue<=1; 1903 if( sm1!=sm2 ){ 1904 p->expired = 1; 1905 } 1906 }else if( (pVar->flags & MEM_Int)==0 || iValue!=pVar->u.i ){ 1907 /* Always expire if the smimask bit is clear */ 1908 p->expired = 1; 1909 } 1910 } 1911 } 1912 sqlite3VdbeMemSetInt64(pVar, iValue); 1913 sqlite3_mutex_leave(p->db->mutex); 1914 return SQLITE_OK; 1915 } 1916 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1917 int rc; 1918 Vdbe *p = (Vdbe*)pStmt; 1919 rc = vdbeUnbind(p, (u32)(i-1)); 1920 if( rc==SQLITE_OK ){ 1921 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-`20240917`-01 */ 1922 sqlite3_mutex_leave(p->db->mutex); 1923 } 1924 return rc; 1925 } 1926 int sqlite3_bind_pointer( 1927 sqlite3_stmt *pStmt, 1928 int i, 1929 void *pPtr, 1930 const char *zPTtype, 1931 void (*xDestructor)(void*) 1932 ){ 1933 int rc; 1934 Vdbe *p = (Vdbe*)pStmt; 1935 rc = vdbeUnbind(p, (u32)(i-1)); 1936 if( rc==SQLITE_OK ){ 1937 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-`20240917`-01 */ 1938 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); 1939 sqlite3_mutex_leave(p->db->mutex); 1940 }else if( xDestructor ){ 1941 xDestructor(pPtr); 1942 } 1943 return rc; 1944 } 1945 int sqlite3_bind_text( 1946 sqlite3_stmt *pStmt, 1947 int i, 1948 const char *zData, 1949 int nData, 1950 void (*xDel)(void*) 1951 ){ 1952 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 1953 } 1954 int sqlite3_bind_text64( 1955 sqlite3_stmt *pStmt, 1956 int i, 1957 const char *zData, 1958 sqlite3_uint64 nData, 1959 void (*xDel)(void*), 1960 unsigned char enc 1961 ){ 1962 assert( xDel!=SQLITE_DYNAMIC ); 1963 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF8_ZT ){ 1964 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 1965 nData &= ~(u64)1; 1966 } 1967 return bindText(pStmt, i, zData, nData, xDel, enc); 1968 } 1969 #ifndef SQLITE_OMIT_UTF16 1970 int sqlite3_bind_text16( 1971 sqlite3_stmt *pStmt, 1972 int i, 1973 const void *zData, 1974 int n, 1975 void (*xDel)(void*) 1976 ){ 1977 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE); 1978 } 1979 #endif /* SQLITE_OMIT_UTF16 */ 1980 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 1981 int rc; 1982 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 1983 case SQLITE_INTEGER: { 1984 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 1985 break; 1986 } 1987 case SQLITE_FLOAT: { 1988 assert( pValue->flags & (MEM_Real|MEM_IntReal) ); 1989 rc = sqlite3_bind_double(pStmt, i, 1990 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i 1991 ); 1992 break; 1993 } 1994 case SQLITE_BLOB: { 1995 if( pValue->flags & MEM_Zero ){ 1996 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 1997 }else{ 1998 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 1999 } 2000 break; 2001 } 2002 case SQLITE_TEXT: { 2003 assert( pValue->enc!=0 ); 2004 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 2005 pValue->enc<=SQLITE_UTF8 ? pValue->enc : pValue->enc|ENC_KEEPBOM); 2006 break; 2007 } 2008 default: { 2009 rc = sqlite3_bind_null(pStmt, i); 2010 break; 2011 } 2012 } 2013 return rc; 2014 } 2015 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 2016 int rc; 2017 Vdbe *p = (Vdbe *)pStmt; 2018 rc = vdbeUnbind(p, (u32)(i-1)); 2019 if( rc==SQLITE_OK ){ 2020 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-`20240917`-01 */ 2021 #ifndef SQLITE_OMIT_INCRBLOB 2022 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 2023 #else 2024 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 2025 #endif 2026 p->aVar[i-1].enc = ENC(p->db); 2027 sqlite3_mutex_leave(p->db->mutex); 2028 } 2029 return rc; 2030 } 2031 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 2032 int rc; 2033 Vdbe *p = (Vdbe *)pStmt; 2034 #ifdef SQLITE_ENABLE_API_ARMOR 2035 if( p==0 ) return SQLITE_MISUSE_BKPT; 2036 #endif 2037 sqlite3_mutex_enter(p->db->mutex); 2038 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 2039 rc = SQLITE_TOOBIG; 2040 }else{ 2041 assert( (n & 0x7FFFFFFF)==n ); 2042 rc = sqlite3_bind_zeroblob(pStmt, i, n); 2043 } 2044 rc = sqlite3ApiExit(p->db, rc); 2045 sqlite3_mutex_leave(p->db->mutex); 2046 return rc; 2047 } 2048 2049 /* 2050 ** Return the number of wildcards that can be potentially bound to. 2051 ** This routine is added to support DBD::SQLite. 2052 */ 2053 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 2054 Vdbe *p = (Vdbe*)pStmt; 2055 return p ? p->nVar : 0; 2056 } 2057 2058 /* 2059 ** Return the name of a wildcard parameter. Return NULL if the index 2060 ** is out of range or if the wildcard is unnamed. 2061 ** 2062 ** The result is always UTF-8. 2063 */ 2064 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 2065 Vdbe *p = (Vdbe*)pStmt; 2066 if( p==0 ) return 0; 2067 return sqlite3VListNumToName(p->pVList, i); 2068 } 2069 2070 /* 2071 ** Given a wildcard parameter name, return the index of the variable 2072 ** with that name. If there is no variable with the given name, 2073 ** return 0. 2074 */ 2075 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 2076 if( p==0 || zName==0 ) return 0; 2077 return sqlite3VListNameToNum(p->pVList, zName, nName); 2078 } 2079 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 2080 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 2081 } 2082 2083 /* 2084 ** Transfer all bindings from the first statement over to the second. 2085 */ 2086 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 2087 Vdbe *pFrom = (Vdbe*)pFromStmt; 2088 Vdbe *pTo = (Vdbe*)pToStmt; 2089 int i; 2090 assert( pTo->db==pFrom->db ); 2091 assert( pTo->nVar==pFrom->nVar ); 2092 sqlite3_mutex_enter(pTo->db->mutex); 2093 for(i=0; inVar; i++){ 2094 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 2095 } 2096 sqlite3_mutex_leave(pTo->db->mutex); 2097 return SQLITE_OK; 2098 } 2099 2100 #ifndef SQLITE_OMIT_DEPRECATED 2101 /* 2102 ** Deprecated external interface. Internal/core SQLite code 2103 ** should call sqlite3TransferBindings. 2104 ** 2105 ** It is misuse to call this routine with statements from different 2106 ** database connections. But as this is a deprecated interface, we 2107 ** will not bother to check for that condition. 2108 ** 2109 ** If the two statements contain a different number of bindings, then 2110 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 2111 ** SQLITE_OK is returned. 2112 */ 2113 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 2114 Vdbe *pFrom = (Vdbe*)pFromStmt; 2115 Vdbe *pTo = (Vdbe*)pToStmt; 2116 if( pFrom->nVar!=pTo->nVar ){ 2117 return SQLITE_ERROR; 2118 } 2119 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); 2120 if( pTo->expmask ){ 2121 pTo->expired = 1; 2122 } 2123 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); 2124 if( pFrom->expmask ){ 2125 pFrom->expired = 1; 2126 } 2127 return sqlite3TransferBindings(pFromStmt, pToStmt); 2128 } 2129 #endif 2130 2131 /* 2132 ** Return the sqlite3* database handle to which the prepared statement given 2133 ** in the argument belongs. This is the same database handle that was 2134 ** the first argument to the sqlite3_prepare() that was used to create 2135 ** the statement in the first place. 2136 */ 2137 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 2138 return pStmt ? ((Vdbe*)pStmt)->db : 0; 2139 } 2140 2141 /* 2142 ** Return true if the prepared statement is guaranteed to not modify the 2143 ** database. 2144 */ 2145 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 2146 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 2147 } 2148 2149 /* 2150 ** Return 1 if the statement is an EXPLAIN and return 2 if the 2151 ** statement is an EXPLAIN QUERY PLAN 2152 */ 2153 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ 2154 return pStmt ? ((Vdbe*)pStmt)->explain : 0; 2155 } 2156 2157 /* 2158 ** Set the explain mode for a statement. 2159 */ 2160 int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){ 2161 Vdbe *v = (Vdbe*)pStmt; 2162 int rc; 2163 #ifdef SQLITE_ENABLE_API_ARMOR 2164 if( pStmt==0 ) return SQLITE_MISUSE_BKPT; 2165 #endif 2166 sqlite3_mutex_enter(v->db->mutex); 2167 if( ((int)v->explain)==eMode ){ 2168 rc = SQLITE_OK; 2169 }else if( eMode<0 || eMode>2 ){ 2170 rc = SQLITE_ERROR; 2171 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ 2172 rc = SQLITE_ERROR; 2173 }else if( v->eVdbeState!=VDBE_READY_STATE ){ 2174 rc = SQLITE_BUSY; 2175 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ 2176 /* No reprepare necessary */ 2177 v->explain = eMode; 2178 rc = SQLITE_OK; 2179 }else{ 2180 int savedMode = v->explain; 2181 v->explain = eMode; 2182 rc = sqlite3Reprepare(v); 2183 if( rc ){ 2184 v->explain = savedMode; 2185 }else{ 2186 v->haveEqpOps = eMode==2; 2187 } 2188 } 2189 if( v->explain ){ 2190 v->nResColumn = 12 - 4*v->explain; 2191 }else{ 2192 v->nResColumn = v->nResAlloc; 2193 } 2194 sqlite3_mutex_leave(v->db->mutex); 2195 return rc; 2196 } 2197 2198 /* 2199 ** Return true if the prepared statement is in need of being reset. 2200 */ 2201 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 2202 Vdbe *v = (Vdbe*)pStmt; 2203 return v!=0 && v->eVdbeState==VDBE_RUN_STATE; 2204 } 2205 2206 /* 2207 ** Return a pointer to the next prepared statement after pStmt associated 2208 ** with database connection pDb. If pStmt is NULL, return the first 2209 ** prepared statement for the database connection. Return NULL if there 2210 ** are no more. 2211 */ 2212 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 2213 sqlite3_stmt *pNext; 2214 #ifdef SQLITE_ENABLE_API_ARMOR 2215 if( !sqlite3SafetyCheckOk(pDb) ){ 2216 (void)SQLITE_MISUSE_BKPT; 2217 return 0; 2218 } 2219 #endif 2220 sqlite3_mutex_enter(pDb->mutex); 2221 if( pStmt==0 ){ 2222 pNext = (sqlite3_stmt*)pDb->pVdbe; 2223 }else{ 2224 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; 2225 } 2226 sqlite3_mutex_leave(pDb->mutex); 2227 return pNext; 2228 } 2229 2230 /* 2231 ** Return the value of a status counter for a prepared statement 2232 */ 2233 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 2234 Vdbe *pVdbe = (Vdbe*)pStmt; 2235 u32 v; 2236 #ifdef SQLITE_ENABLE_API_ARMOR 2237 if( !pStmt 2238 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) 2239 ){ 2240 (void)SQLITE_MISUSE_BKPT; 2241 return 0; 2242 } 2243 #endif 2244 if( op==SQLITE_STMTSTATUS_MEMUSED ){ 2245 sqlite3 *db = pVdbe->db; 2246 sqlite3_mutex_enter(db->mutex); 2247 v = 0; 2248 db->pnBytesFreed = (int*)&v; 2249 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); 2250 db->lookaside.pEnd = db->lookaside.pStart; 2251 sqlite3VdbeDelete(pVdbe); 2252 db->pnBytesFreed = 0; 2253 db->lookaside.pEnd = db->lookaside.pTrueEnd; 2254 sqlite3_mutex_leave(db->mutex); 2255 }else{ 2256 v = pVdbe->aCounter[op]; 2257 if( resetFlag ) pVdbe->aCounter[op] = 0; 2258 } 2259 return (int)v; 2260 } 2261 2262 /* 2263 ** Return the SQL associated with a prepared statement 2264 */ 2265 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 2266 Vdbe *p = (Vdbe *)pStmt; 2267 return p ? p->zSql : 0; 2268 } 2269 2270 /* 2271 ** Return the SQL associated with a prepared statement with 2272 ** bound parameters expanded. Space to hold the returned string is 2273 ** obtained from sqlite3_malloc(). The caller is responsible for 2274 ** freeing the returned string by passing it to sqlite3_free(). 2275 ** 2276 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 2277 ** expanded bound parameters. 2278 */ 2279 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 2280 #ifdef SQLITE_OMIT_TRACE 2281 return 0; 2282 #else 2283 char *z = 0; 2284 const char *zSql = sqlite3_sql(pStmt); 2285 if( zSql ){ 2286 Vdbe *p = (Vdbe *)pStmt; 2287 sqlite3_mutex_enter(p->db->mutex); 2288 z = sqlite3VdbeExpandSql(p, zSql); 2289 sqlite3_mutex_leave(p->db->mutex); 2290 } 2291 return z; 2292 #endif 2293 } 2294 2295 #ifdef SQLITE_ENABLE_NORMALIZE 2296 /* 2297 ** Return the normalized SQL associated with a prepared statement. 2298 */ 2299 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ 2300 Vdbe *p = (Vdbe *)pStmt; 2301 if( p==0 ) return 0; 2302 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ 2303 sqlite3_mutex_enter(p->db->mutex); 2304 p->zNormSql = sqlite3Normalize(p, p->zSql); 2305 sqlite3_mutex_leave(p->db->mutex); 2306 } 2307 return p->zNormSql; 2308 } 2309 #endif /* SQLITE_ENABLE_NORMALIZE */ 2310 2311 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2312 /* 2313 ** Allocate and populate an UnpackedRecord structure based on the serialized 2314 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 2315 ** if successful, or a NULL pointer if an OOM error is encountered. 2316 */ 2317 static UnpackedRecord *vdbeUnpackRecord( 2318 KeyInfo *pKeyInfo, 2319 int nKey, 2320 const void *pKey 2321 ){ 2322 UnpackedRecord *pRet; /* Return value */ 2323 2324 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 2325 if( pRet ){ 2326 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); 2327 sqlite3VdbeRecordUnpack(nKey, pKey, pRet); 2328 } 2329 return pRet; 2330 } 2331 2332 /* 2333 ** This function is called from within a pre-update callback to retrieve 2334 ** a field of the row currently being updated or deleted. 2335 */ 2336 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 2337 PreUpdate *p; 2338 Mem *pMem; 2339 int rc = SQLITE_OK; 2340 int iStore = 0; 2341 2342 #ifdef SQLITE_ENABLE_API_ARMOR 2343 if( db==0 || ppValue==0 ){ 2344 return SQLITE_MISUSE_BKPT; 2345 } 2346 #endif 2347 p = db->pPreUpdate; 2348 /* Test that this call is being made from within an SQLITE_DELETE or 2349 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 2350 if( !p || p->op==SQLITE_INSERT ){ 2351 rc = SQLITE_MISUSE_BKPT; 2352 goto preupdate_old_out; 2353 } 2354 if( p->pPk ){ 2355 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); 2356 }else if( iIdx >= p->pTab->nCol ){ 2357 rc = SQLITE_MISUSE_BKPT; 2358 goto preupdate_old_out; 2359 }else{ 2360 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); 2361 } 2362 if( iStore>=p->pCsr->nField || iStore<0 ){ 2363 rc = SQLITE_RANGE; 2364 goto preupdate_old_out; 2365 } 2366 2367 if( iIdx==p->pTab->iPKey ){ 2368 *ppValue = pMem = &p->oldipk; 2369 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 2370 }else{ 2371 2372 /* If the old.* record has not yet been loaded into memory, do so now. */ 2373 if( p->pUnpacked==0 ){ 2374 u32 nRec; 2375 u8 *aRec; 2376 2377 assert( p->pCsr->eCurType==CURTYPE_BTREE ); 2378 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 2379 aRec = sqlite3DbMallocRaw(db, (u64)nRec+8); 2380 if( !aRec ) goto preupdate_old_out; 2381 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 2382 if( rc==SQLITE_OK ){ 2383 memset(aRec+nRec, 0, 8); 2384 p->pUnpacked = vdbeUnpackRecord(p->pKeyinfo, nRec, aRec); 2385 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 2386 } 2387 if( rc!=SQLITE_OK ){ 2388 sqlite3DbFree(db, aRec); 2389 goto preupdate_old_out; 2390 } 2391 p->aRecord = aRec; 2392 } 2393 2394 pMem = *ppValue = &p->pUnpacked->aMem[iStore]; 2395 if( iStore>=p->pUnpacked->nField ){ 2396 /* This occurs when the table has been extended using ALTER TABLE 2397 ** ADD COLUMN. The value to return is the default value of the column. */ 2398 Column *pCol = &p->pTab->aCol[iIdx]; 2399 if( pCol->iDflt>0 ){ 2400 if( p->apDflt==0 ){ 2401 int nByte; 2402 assert( sizeof(sqlite3_value*)*UMXV(p->pTab->nCol) < 0x7fffffff ); 2403 nByte = sizeof(sqlite3_value*)*p->pTab->nCol; 2404 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); 2405 if( p->apDflt==0 ) goto preupdate_old_out; 2406 } 2407 if( p->apDflt[iIdx]==0 ){ 2408 sqlite3_value *pVal = 0; 2409 Expr *pDflt; 2410 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); 2411 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; 2412 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); 2413 if( rc==SQLITE_OK && pVal==0 ){ 2414 rc = SQLITE_CORRUPT_BKPT; 2415 } 2416 p->apDflt[iIdx] = pVal; 2417 } 2418 *ppValue = p->apDflt[iIdx]; 2419 }else{ 2420 *ppValue = (sqlite3_value *)columnNullValue(); 2421 } 2422 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 2423 if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 2424 testcase( pMem->flags & MEM_Int ); 2425 testcase( pMem->flags & MEM_IntReal ); 2426 sqlite3VdbeMemRealify(pMem); 2427 } 2428 } 2429 } 2430 2431 preupdate_old_out: 2432 sqlite3Error(db, rc); 2433 return sqlite3ApiExit(db, rc); 2434 } 2435 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2436 2437 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2438 /* 2439 ** This function is called from within a pre-update callback to retrieve 2440 ** the number of columns in the row being updated, deleted or inserted. 2441 */ 2442 int sqlite3_preupdate_count(sqlite3 *db){ 2443 PreUpdate *p; 2444 #ifdef SQLITE_ENABLE_API_ARMOR 2445 p = db!=0 ? db->pPreUpdate : 0; 2446 #else 2447 p = db->pPreUpdate; 2448 #endif 2449 return (p ? p->pKeyinfo->nKeyField : 0); 2450 } 2451 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2452 2453 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2454 /* 2455 ** This function is designed to be called from within a pre-update callback 2456 ** only. It returns zero if the change that caused the callback was made 2457 ** immediately by a user SQL statement. Or, if the change was made by a 2458 ** trigger program, it returns the number of trigger programs currently 2459 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 2460 ** top-level trigger etc.). 2461 ** 2462 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 2463 ** or SET DEFAULT action is considered a trigger. 2464 */ 2465 int sqlite3_preupdate_depth(sqlite3 *db){ 2466 PreUpdate *p; 2467 #ifdef SQLITE_ENABLE_API_ARMOR 2468 p = db!=0 ? db->pPreUpdate : 0; 2469 #else 2470 p = db->pPreUpdate; 2471 #endif 2472 return (p ? p->v->nFrame : 0); 2473 } 2474 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2475 2476 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2477 /* 2478 ** This function is designed to be called from within a pre-update callback 2479 ** only. 2480 */ 2481 int sqlite3_preupdate_blobwrite(sqlite3 *db){ 2482 PreUpdate *p; 2483 #ifdef SQLITE_ENABLE_API_ARMOR 2484 p = db!=0 ? db->pPreUpdate : 0; 2485 #else 2486 p = db->pPreUpdate; 2487 #endif 2488 return (p ? p->iBlobWrite : -1); 2489 } 2490 #endif 2491 2492 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 2493 /* 2494 ** This function is called from within a pre-update callback to retrieve 2495 ** a field of the row currently being updated or inserted. 2496 */ 2497 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 2498 PreUpdate *p; 2499 int rc = SQLITE_OK; 2500 Mem *pMem; 2501 int iStore = 0; 2502 2503 #ifdef SQLITE_ENABLE_API_ARMOR 2504 if( db==0 || ppValue==0 ){ 2505 return SQLITE_MISUSE_BKPT; 2506 } 2507 #endif 2508 p = db->pPreUpdate; 2509 if( !p || p->op==SQLITE_DELETE ){ 2510 rc = SQLITE_MISUSE_BKPT; 2511 goto preupdate_new_out; 2512 } 2513 if( p->pPk && p->op!=SQLITE_UPDATE ){ 2514 iStore = sqlite3TableColumnToIndex(p->pPk, iIdx); 2515 }else if( iIdx >= p->pTab->nCol ){ 2516 return SQLITE_MISUSE_BKPT; 2517 }else{ 2518 iStore = sqlite3TableColumnToStorage(p->pTab, iIdx); 2519 } 2520 2521 if( iStore>=p->pCsr->nField || iStore<0 ){ 2522 rc = SQLITE_RANGE; 2523 goto preupdate_new_out; 2524 } 2525 2526 if( p->op==SQLITE_INSERT ){ 2527 /* For an INSERT, memory cell p->iNewReg contains the serialized record 2528 ** that is being inserted. Deserialize it. */ 2529 UnpackedRecord *pUnpack = p->pNewUnpacked; 2530 if( !pUnpack ){ 2531 Mem *pData = &p->v->aMem[p->iNewReg]; 2532 rc = ExpandBlob(pData); 2533 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2534 pUnpack = vdbeUnpackRecord(p->pKeyinfo, pData->n, pData->z); 2535 if( !pUnpack ){ 2536 rc = SQLITE_NOMEM; 2537 goto preupdate_new_out; 2538 } 2539 p->pNewUnpacked = pUnpack; 2540 } 2541 pMem = &pUnpack->aMem[iStore]; 2542 if( iIdx==p->pTab->iPKey ){ 2543 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2544 }else if( iStore>=pUnpack->nField ){ 2545 pMem = (sqlite3_value *)columnNullValue(); 2546 }else if( (p->pTab->aCol) 2547 && (p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL) 2548 && (pMem->flags & (MEM_Int|MEM_IntReal)) 2549 ){ 2550 sqlite3VdbeMemRealify(pMem); 2551 } 2552 }else{ 2553 /* For an UPDATE, memory cell (p->iNewReg+1+iStore) contains the required 2554 ** value. Make a copy of the cell contents and return a pointer to it. 2555 ** It is not safe to return a pointer to the memory cell itself as the 2556 ** caller may modify the value text encoding. 2557 */ 2558 assert( p->op==SQLITE_UPDATE ); 2559 if( !p->aNew ){ 2560 assert( sizeof(Mem)*UMXV(p->pCsr->nField) < 0x7fffffff ); 2561 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem)*p->pCsr->nField); 2562 if( !p->aNew ){ 2563 rc = SQLITE_NOMEM; 2564 goto preupdate_new_out; 2565 } 2566 } 2567 assert( iStore>=0 && iStorepCsr->nField ); 2568 pMem = &p->aNew[iStore]; 2569 if( pMem->flags==0 ){ 2570 if( iIdx==p->pTab->iPKey ){ 2571 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 2572 }else{ 2573 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iStore]); 2574 if( rc!=SQLITE_OK ) goto preupdate_new_out; 2575 } 2576 } 2577 } 2578 *ppValue = pMem; 2579 2580 preupdate_new_out: 2581 sqlite3Error(db, rc); 2582 return sqlite3ApiExit(db, rc); 2583 } 2584 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 2585 2586 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 2587 /* 2588 ** Return status data for a single loop within query pStmt. 2589 */ 2590 int sqlite3_stmt_scanstatus_v2( 2591 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 2592 int iScan, /* Index of loop to report on */ 2593 int iScanStatusOp, /* Which metric to return */ 2594 int flags, 2595 void *pOut /* OUT: Write the answer here */ 2596 ){ 2597 Vdbe *p = (Vdbe*)pStmt; 2598 VdbeOp *aOp; 2599 int nOp; 2600 ScanStatus *pScan = 0; 2601 int idx; 2602 2603 #ifdef SQLITE_ENABLE_API_ARMOR 2604 if( p==0 || pOut==0 2605 || iScanStatusOpSQLITE_SCANSTAT_NCYCLE ){ 2607 return 1; 2608 } 2609 #endif 2610 aOp = p->aOp; 2611 nOp = p->nOp; 2612 if( p->pFrame ){ 2613 VdbeFrame *pFrame; 2614 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 2615 aOp = pFrame->aOp; 2616 nOp = pFrame->nOp; 2617 } 2618 2619 if( iScan<0 ){ 2620 int ii; 2621 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ 2622 i64 res = 0; 2623 for(ii=0; iinScan; idx++){ 2637 pScan = &p->aScan[idx]; 2638 if( pScan->zName ){ 2639 iScan--; 2640 if( iScan<0 ) break; 2641 } 2642 } 2643 } 2644 if( idx>=p->nScan ) return 1; 2645 assert( pScan==0 || pScan==&p->aScan[idx] ); 2646 pScan = &p->aScan[idx]; 2647 2648 switch( iScanStatusOp ){ 2649 case SQLITE_SCANSTAT_NLOOP: { 2650 if( pScan->addrLoop>0 ){ 2651 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; 2652 }else{ 2653 *(sqlite3_int64*)pOut = -1; 2654 } 2655 break; 2656 } 2657 case SQLITE_SCANSTAT_NVISIT: { 2658 if( pScan->addrVisit>0 ){ 2659 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; 2660 }else{ 2661 *(sqlite3_int64*)pOut = -1; 2662 } 2663 break; 2664 } 2665 case SQLITE_SCANSTAT_EST: { 2666 double r = 1.0; 2667 LogEst x = pScan->nEst; 2668 while( x<100 ){ 2669 x += 10; 2670 r *= 0.5; 2671 } 2672 *(double*)pOut = r*sqlite3LogEstToInt(x); 2673 break; 2674 } 2675 case SQLITE_SCANSTAT_NAME: { 2676 *(const char**)pOut = pScan->zName; 2677 break; 2678 } 2679 case SQLITE_SCANSTAT_EXPLAIN: { 2680 if( pScan->addrExplain ){ 2681 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; 2682 }else{ 2683 *(const char**)pOut = 0; 2684 } 2685 break; 2686 } 2687 case SQLITE_SCANSTAT_SELECTID: { 2688 if( pScan->addrExplain ){ 2689 *(int*)pOut = aOp[ pScan->addrExplain ].p1; 2690 }else{ 2691 *(int*)pOut = -1; 2692 } 2693 break; 2694 } 2695 case SQLITE_SCANSTAT_PARENTID: { 2696 if( pScan->addrExplain ){ 2697 *(int*)pOut = aOp[ pScan->addrExplain ].p2; 2698 }else{ 2699 *(int*)pOut = -1; 2700 } 2701 break; 2702 } 2703 case SQLITE_SCANSTAT_NCYCLE: { 2704 i64 res = 0; 2705 if( pScan->aAddrRange[0]==0 ){ 2706 res = -1; 2707 }else{ 2708 int ii; 2709 for(ii=0; iiaAddrRange); ii+=2){ 2710 int iIns = pScan->aAddrRange[ii]; 2711 int iEnd = pScan->aAddrRange[ii+1]; 2712 if( iIns==0 ) break; 2713 if( iIns>0 ){ 2714 while( iIns<=iEnd ){ 2715 res += aOp[iIns].nCycle; 2716 iIns++; 2717 } 2718 }else{ 2719 int iOp; 2720 for(iOp=0; iOpp1!=iEnd ) continue; 2723 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ 2724 continue; 2725 } 2726 res += aOp[iOp].nCycle; 2727 } 2728 } 2729 } 2730 } 2731 *(i64*)pOut = res; 2732 break; 2733 } 2734 default: { 2735 return 1; 2736 } 2737 } 2738 return 0; 2739 } 2740 2741 /* 2742 ** Return status data for a single loop within query pStmt. 2743 */ 2744 int sqlite3_stmt_scanstatus( 2745 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 2746 int iScan, /* Index of loop to report on */ 2747 int iScanStatusOp, /* Which metric to return */ 2748 void *pOut /* OUT: Write the answer here */ 2749 ){ 2750 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut); 2751 } 2752 2753 /* 2754 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 2755 */ 2756 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 2757 Vdbe *p = (Vdbe*)pStmt; 2758 int ii; 2759 for(ii=0; p!=0 && iinOp; ii++){ 2760 Op *pOp = &p->aOp[ii]; 2761 pOp->nExec = 0; 2762 pOp->nCycle = 0; 2763 } 2764 } 2765 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */