000001 /* 000002 ** 2004 May 26 000003 ** 000004 ** The author disclaims copyright to this source code. In place of 000005 ** a legal notice, here is a blessing: 000006 ** 000007 ** May you do good and not evil. 000008 ** May you find forgiveness for yourself and forgive others. 000009 ** May you share freely, never taking more than you give. 000010 ** 000011 ************************************************************************* 000012 ** 000013 ** This file contains code use to implement APIs that are part of the 000014 ** VDBE. 000015 */ 000016 #include "sqliteInt.h" 000017 #include "vdbeInt.h" 000018 #include "opcodes.h" 000019 000020 #ifndef SQLITE_OMIT_DEPRECATED 000021 /* 000022 ** Return TRUE (non-zero) of the statement supplied as an argument needs 000023 ** to be recompiled. A statement needs to be recompiled whenever the 000024 ** execution environment changes in a way that would alter the program 000025 ** that sqlite3_prepare() generates. For example, if new functions or 000026 ** collating sequences are registered or if an authorizer function is 000027 ** added or changed. 000028 */ 000029 int sqlite3_expired(sqlite3_stmt *pStmt){ 000030 Vdbe *p = (Vdbe*)pStmt; 000031 return p==0 || p->expired; 000032 } 000033 #endif 000034 000035 /* 000036 ** Check on a Vdbe to make sure it has not been finalized. Log 000037 ** an error and return true if it has been finalized (or is otherwise 000038 ** invalid). Return false if it is ok. 000039 */ 000040 static int vdbeSafety(Vdbe *p){ 000041 if( p->db==0 ){ 000042 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); 000043 return 1; 000044 }else{ 000045 return 0; 000046 } 000047 } 000048 static int vdbeSafetyNotNull(Vdbe *p){ 000049 if( p==0 ){ 000050 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 000051 return 1; 000052 }else{ 000053 return vdbeSafety(p); 000054 } 000055 } 000056 000057 #ifndef SQLITE_OMIT_TRACE 000058 /* 000059 ** Invoke the profile callback. This routine is only called if we already 000060 ** know that the profile callback is defined and needs to be invoked. 000061 */ 000062 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 000063 sqlite3_int64 iNow; 000064 sqlite3_int64 iElapse; 000065 assert( p->startTime>0 ); 000066 assert( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 ); 000067 assert( db->init.busy==0 ); 000068 assert( p->zSql!=0 ); 000069 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 000070 iElapse = (iNow - p->startTime)*1000000; 000071 #ifndef SQLITE_OMIT_DEPRECATED 000072 if( db->xProfile ){ 000073 db->xProfile(db->pProfileArg, p->zSql, iElapse); 000074 } 000075 #endif 000076 if( db->mTrace & SQLITE_TRACE_PROFILE ){ 000077 db->trace.xV2(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 000078 } 000079 p->startTime = 0; 000080 } 000081 /* 000082 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 000083 ** is needed, and it invokes the callback if it is needed. 000084 */ 000085 # define checkProfileCallback(DB,P) \ 000086 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 000087 #else 000088 # define checkProfileCallback(DB,P) /*no-op*/ 000089 #endif 000090 000091 /* 000092 ** The following routine destroys a virtual machine that is created by 000093 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 000094 ** success/failure code that describes the result of executing the virtual 000095 ** machine. 000096 ** 000097 ** This routine sets the error code and string returned by 000098 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 000099 */ 000100 int sqlite3_finalize(sqlite3_stmt *pStmt){ 000101 int rc; 000102 if( pStmt==0 ){ 000103 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 000104 ** pointer is a harmless no-op. */ 000105 rc = SQLITE_OK; 000106 }else{ 000107 Vdbe *v = (Vdbe*)pStmt; 000108 sqlite3 *db = v->db; 000109 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 000110 sqlite3_mutex_enter(db->mutex); 000111 checkProfileCallback(db, v); 000112 assert( v->eVdbeState>=VDBE_READY_STATE ); 000113 rc = sqlite3VdbeReset(v); 000114 sqlite3VdbeDelete(v); 000115 rc = sqlite3ApiExit(db, rc); 000116 sqlite3LeaveMutexAndCloseZombie(db); 000117 } 000118 return rc; 000119 } 000120 000121 /* 000122 ** Terminate the current execution of an SQL statement and reset it 000123 ** back to its starting state so that it can be reused. A success code from 000124 ** the prior execution is returned. 000125 ** 000126 ** This routine sets the error code and string returned by 000127 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 000128 */ 000129 int sqlite3_reset(sqlite3_stmt *pStmt){ 000130 int rc; 000131 if( pStmt==0 ){ 000132 rc = SQLITE_OK; 000133 }else{ 000134 Vdbe *v = (Vdbe*)pStmt; 000135 sqlite3 *db = v->db; 000136 sqlite3_mutex_enter(db->mutex); 000137 checkProfileCallback(db, v); 000138 rc = sqlite3VdbeReset(v); 000139 sqlite3VdbeRewind(v); 000140 assert( (rc & (db->errMask))==rc ); 000141 rc = sqlite3ApiExit(db, rc); 000142 sqlite3_mutex_leave(db->mutex); 000143 } 000144 return rc; 000145 } 000146 000147 /* 000148 ** Set all the parameters in the compiled SQL statement to NULL. 000149 */ 000150 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 000151 int i; 000152 int rc = SQLITE_OK; 000153 Vdbe *p = (Vdbe*)pStmt; 000154 #if SQLITE_THREADSAFE 000155 sqlite3_mutex *mutex; 000156 #endif 000157 #ifdef SQLITE_ENABLE_API_ARMOR 000158 if( pStmt==0 ){ 000159 return SQLITE_MISUSE_BKPT; 000160 } 000161 #endif 000162 #if SQLITE_THREADSAFE 000163 mutex = p->db->mutex; 000164 #endif 000165 sqlite3_mutex_enter(mutex); 000166 for(i=0; i<p->nVar; i++){ 000167 sqlite3VdbeMemRelease(&p->aVar[i]); 000168 p->aVar[i].flags = MEM_Null; 000169 } 000170 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 000171 if( p->expmask ){ 000172 p->expired = 1; 000173 } 000174 sqlite3_mutex_leave(mutex); 000175 return rc; 000176 } 000177 000178 000179 /**************************** sqlite3_value_ ******************************* 000180 ** The following routines extract information from a Mem or sqlite3_value 000181 ** structure. 000182 */ 000183 const void *sqlite3_value_blob(sqlite3_value *pVal){ 000184 Mem *p = (Mem*)pVal; 000185 if( p->flags & (MEM_Blob|MEM_Str) ){ 000186 if( ExpandBlob(p)!=SQLITE_OK ){ 000187 assert( p->flags==MEM_Null && p->z==0 ); 000188 return 0; 000189 } 000190 p->flags |= MEM_Blob; 000191 return p->n ? p->z : 0; 000192 }else{ 000193 return sqlite3_value_text(pVal); 000194 } 000195 } 000196 int sqlite3_value_bytes(sqlite3_value *pVal){ 000197 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 000198 } 000199 int sqlite3_value_bytes16(sqlite3_value *pVal){ 000200 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 000201 } 000202 double sqlite3_value_double(sqlite3_value *pVal){ 000203 return sqlite3VdbeRealValue((Mem*)pVal); 000204 } 000205 int sqlite3_value_int(sqlite3_value *pVal){ 000206 return (int)sqlite3VdbeIntValue((Mem*)pVal); 000207 } 000208 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 000209 return sqlite3VdbeIntValue((Mem*)pVal); 000210 } 000211 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 000212 Mem *pMem = (Mem*)pVal; 000213 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 000214 } 000215 void *sqlite3_value_pointer(sqlite3_value *pVal, const char *zPType){ 000216 Mem *p = (Mem*)pVal; 000217 if( (p->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 000218 (MEM_Null|MEM_Term|MEM_Subtype) 000219 && zPType!=0 000220 && p->eSubtype=='p' 000221 && strcmp(p->u.zPType, zPType)==0 000222 ){ 000223 return (void*)p->z; 000224 }else{ 000225 return 0; 000226 } 000227 } 000228 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 000229 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 000230 } 000231 #ifndef SQLITE_OMIT_UTF16 000232 const void *sqlite3_value_text16(sqlite3_value* pVal){ 000233 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 000234 } 000235 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 000236 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 000237 } 000238 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 000239 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 000240 } 000241 #endif /* SQLITE_OMIT_UTF16 */ 000242 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 000243 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 000244 ** point number string BLOB NULL 000245 */ 000246 int sqlite3_value_type(sqlite3_value* pVal){ 000247 static const u8 aType[] = { 000248 SQLITE_BLOB, /* 0x00 (not possible) */ 000249 SQLITE_NULL, /* 0x01 NULL */ 000250 SQLITE_TEXT, /* 0x02 TEXT */ 000251 SQLITE_NULL, /* 0x03 (not possible) */ 000252 SQLITE_INTEGER, /* 0x04 INTEGER */ 000253 SQLITE_NULL, /* 0x05 (not possible) */ 000254 SQLITE_INTEGER, /* 0x06 INTEGER + TEXT */ 000255 SQLITE_NULL, /* 0x07 (not possible) */ 000256 SQLITE_FLOAT, /* 0x08 FLOAT */ 000257 SQLITE_NULL, /* 0x09 (not possible) */ 000258 SQLITE_FLOAT, /* 0x0a FLOAT + TEXT */ 000259 SQLITE_NULL, /* 0x0b (not possible) */ 000260 SQLITE_INTEGER, /* 0x0c (not possible) */ 000261 SQLITE_NULL, /* 0x0d (not possible) */ 000262 SQLITE_INTEGER, /* 0x0e (not possible) */ 000263 SQLITE_NULL, /* 0x0f (not possible) */ 000264 SQLITE_BLOB, /* 0x10 BLOB */ 000265 SQLITE_NULL, /* 0x11 (not possible) */ 000266 SQLITE_TEXT, /* 0x12 (not possible) */ 000267 SQLITE_NULL, /* 0x13 (not possible) */ 000268 SQLITE_INTEGER, /* 0x14 INTEGER + BLOB */ 000269 SQLITE_NULL, /* 0x15 (not possible) */ 000270 SQLITE_INTEGER, /* 0x16 (not possible) */ 000271 SQLITE_NULL, /* 0x17 (not possible) */ 000272 SQLITE_FLOAT, /* 0x18 FLOAT + BLOB */ 000273 SQLITE_NULL, /* 0x19 (not possible) */ 000274 SQLITE_FLOAT, /* 0x1a (not possible) */ 000275 SQLITE_NULL, /* 0x1b (not possible) */ 000276 SQLITE_INTEGER, /* 0x1c (not possible) */ 000277 SQLITE_NULL, /* 0x1d (not possible) */ 000278 SQLITE_INTEGER, /* 0x1e (not possible) */ 000279 SQLITE_NULL, /* 0x1f (not possible) */ 000280 SQLITE_FLOAT, /* 0x20 INTREAL */ 000281 SQLITE_NULL, /* 0x21 (not possible) */ 000282 SQLITE_FLOAT, /* 0x22 INTREAL + TEXT */ 000283 SQLITE_NULL, /* 0x23 (not possible) */ 000284 SQLITE_FLOAT, /* 0x24 (not possible) */ 000285 SQLITE_NULL, /* 0x25 (not possible) */ 000286 SQLITE_FLOAT, /* 0x26 (not possible) */ 000287 SQLITE_NULL, /* 0x27 (not possible) */ 000288 SQLITE_FLOAT, /* 0x28 (not possible) */ 000289 SQLITE_NULL, /* 0x29 (not possible) */ 000290 SQLITE_FLOAT, /* 0x2a (not possible) */ 000291 SQLITE_NULL, /* 0x2b (not possible) */ 000292 SQLITE_FLOAT, /* 0x2c (not possible) */ 000293 SQLITE_NULL, /* 0x2d (not possible) */ 000294 SQLITE_FLOAT, /* 0x2e (not possible) */ 000295 SQLITE_NULL, /* 0x2f (not possible) */ 000296 SQLITE_BLOB, /* 0x30 (not possible) */ 000297 SQLITE_NULL, /* 0x31 (not possible) */ 000298 SQLITE_TEXT, /* 0x32 (not possible) */ 000299 SQLITE_NULL, /* 0x33 (not possible) */ 000300 SQLITE_FLOAT, /* 0x34 (not possible) */ 000301 SQLITE_NULL, /* 0x35 (not possible) */ 000302 SQLITE_FLOAT, /* 0x36 (not possible) */ 000303 SQLITE_NULL, /* 0x37 (not possible) */ 000304 SQLITE_FLOAT, /* 0x38 (not possible) */ 000305 SQLITE_NULL, /* 0x39 (not possible) */ 000306 SQLITE_FLOAT, /* 0x3a (not possible) */ 000307 SQLITE_NULL, /* 0x3b (not possible) */ 000308 SQLITE_FLOAT, /* 0x3c (not possible) */ 000309 SQLITE_NULL, /* 0x3d (not possible) */ 000310 SQLITE_FLOAT, /* 0x3e (not possible) */ 000311 SQLITE_NULL, /* 0x3f (not possible) */ 000312 }; 000313 #ifdef SQLITE_DEBUG 000314 { 000315 int eType = SQLITE_BLOB; 000316 if( pVal->flags & MEM_Null ){ 000317 eType = SQLITE_NULL; 000318 }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){ 000319 eType = SQLITE_FLOAT; 000320 }else if( pVal->flags & MEM_Int ){ 000321 eType = SQLITE_INTEGER; 000322 }else if( pVal->flags & MEM_Str ){ 000323 eType = SQLITE_TEXT; 000324 } 000325 assert( eType == aType[pVal->flags&MEM_AffMask] ); 000326 } 000327 #endif 000328 return aType[pVal->flags&MEM_AffMask]; 000329 } 000330 int sqlite3_value_encoding(sqlite3_value *pVal){ 000331 return pVal->enc; 000332 } 000333 000334 /* Return true if a parameter to xUpdate represents an unchanged column */ 000335 int sqlite3_value_nochange(sqlite3_value *pVal){ 000336 return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero); 000337 } 000338 000339 /* Return true if a parameter value originated from an sqlite3_bind() */ 000340 int sqlite3_value_frombind(sqlite3_value *pVal){ 000341 return (pVal->flags&MEM_FromBind)!=0; 000342 } 000343 000344 /* Make a copy of an sqlite3_value object 000345 */ 000346 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 000347 sqlite3_value *pNew; 000348 if( pOrig==0 ) return 0; 000349 pNew = sqlite3_malloc( sizeof(*pNew) ); 000350 if( pNew==0 ) return 0; 000351 memset(pNew, 0, sizeof(*pNew)); 000352 memcpy(pNew, pOrig, MEMCELLSIZE); 000353 pNew->flags &= ~MEM_Dyn; 000354 pNew->db = 0; 000355 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 000356 pNew->flags &= ~(MEM_Static|MEM_Dyn); 000357 pNew->flags |= MEM_Ephem; 000358 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 000359 sqlite3ValueFree(pNew); 000360 pNew = 0; 000361 } 000362 }else if( pNew->flags & MEM_Null ){ 000363 /* Do not duplicate pointer values */ 000364 pNew->flags &= ~(MEM_Term|MEM_Subtype); 000365 } 000366 return pNew; 000367 } 000368 000369 /* Destroy an sqlite3_value object previously obtained from 000370 ** sqlite3_value_dup(). 000371 */ 000372 void sqlite3_value_free(sqlite3_value *pOld){ 000373 sqlite3ValueFree(pOld); 000374 } 000375 000376 000377 /**************************** sqlite3_result_ ******************************* 000378 ** The following routines are used by user-defined functions to specify 000379 ** the function result. 000380 ** 000381 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 000382 ** result as a string or blob. Appropriate errors are set if the string/blob 000383 ** is too big or if an OOM occurs. 000384 ** 000385 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 000386 ** on value P if P is not going to be used and need to be destroyed. 000387 */ 000388 static void setResultStrOrError( 000389 sqlite3_context *pCtx, /* Function context */ 000390 const char *z, /* String pointer */ 000391 int n, /* Bytes in string, or negative */ 000392 u8 enc, /* Encoding of z. 0 for BLOBs */ 000393 void (*xDel)(void*) /* Destructor function */ 000394 ){ 000395 Mem *pOut = pCtx->pOut; 000396 int rc = sqlite3VdbeMemSetStr(pOut, z, n, enc, xDel); 000397 if( rc ){ 000398 if( rc==SQLITE_TOOBIG ){ 000399 sqlite3_result_error_toobig(pCtx); 000400 }else{ 000401 /* The only errors possible from sqlite3VdbeMemSetStr are 000402 ** SQLITE_TOOBIG and SQLITE_NOMEM */ 000403 assert( rc==SQLITE_NOMEM ); 000404 sqlite3_result_error_nomem(pCtx); 000405 } 000406 return; 000407 } 000408 sqlite3VdbeChangeEncoding(pOut, pCtx->enc); 000409 if( sqlite3VdbeMemTooBig(pOut) ){ 000410 sqlite3_result_error_toobig(pCtx); 000411 } 000412 } 000413 static int invokeValueDestructor( 000414 const void *p, /* Value to destroy */ 000415 void (*xDel)(void*), /* The destructor */ 000416 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if not NULL */ 000417 ){ 000418 assert( xDel!=SQLITE_DYNAMIC ); 000419 if( xDel==0 ){ 000420 /* noop */ 000421 }else if( xDel==SQLITE_TRANSIENT ){ 000422 /* noop */ 000423 }else{ 000424 xDel((void*)p); 000425 } 000426 #ifdef SQLITE_ENABLE_API_ARMOR 000427 if( pCtx!=0 ){ 000428 sqlite3_result_error_toobig(pCtx); 000429 } 000430 #else 000431 assert( pCtx!=0 ); 000432 sqlite3_result_error_toobig(pCtx); 000433 #endif 000434 return SQLITE_TOOBIG; 000435 } 000436 void sqlite3_result_blob( 000437 sqlite3_context *pCtx, 000438 const void *z, 000439 int n, 000440 void (*xDel)(void *) 000441 ){ 000442 #ifdef SQLITE_ENABLE_API_ARMOR 000443 if( pCtx==0 || n<0 ){ 000444 invokeValueDestructor(z, xDel, pCtx); 000445 return; 000446 } 000447 #endif 000448 assert( n>=0 ); 000449 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000450 setResultStrOrError(pCtx, z, n, 0, xDel); 000451 } 000452 void sqlite3_result_blob64( 000453 sqlite3_context *pCtx, 000454 const void *z, 000455 sqlite3_uint64 n, 000456 void (*xDel)(void *) 000457 ){ 000458 assert( xDel!=SQLITE_DYNAMIC ); 000459 #ifdef SQLITE_ENABLE_API_ARMOR 000460 if( pCtx==0 ){ 000461 invokeValueDestructor(z, xDel, 0); 000462 return; 000463 } 000464 #endif 000465 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000466 if( n>0x7fffffff ){ 000467 (void)invokeValueDestructor(z, xDel, pCtx); 000468 }else{ 000469 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 000470 } 000471 } 000472 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 000473 #ifdef SQLITE_ENABLE_API_ARMOR 000474 if( pCtx==0 ) return; 000475 #endif 000476 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000477 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 000478 } 000479 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 000480 #ifdef SQLITE_ENABLE_API_ARMOR 000481 if( pCtx==0 ) return; 000482 #endif 000483 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000484 pCtx->isError = SQLITE_ERROR; 000485 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 000486 } 000487 #ifndef SQLITE_OMIT_UTF16 000488 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 000489 #ifdef SQLITE_ENABLE_API_ARMOR 000490 if( pCtx==0 ) return; 000491 #endif 000492 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000493 pCtx->isError = SQLITE_ERROR; 000494 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 000495 } 000496 #endif 000497 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 000498 #ifdef SQLITE_ENABLE_API_ARMOR 000499 if( pCtx==0 ) return; 000500 #endif 000501 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000502 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 000503 } 000504 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 000505 #ifdef SQLITE_ENABLE_API_ARMOR 000506 if( pCtx==0 ) return; 000507 #endif 000508 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000509 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 000510 } 000511 void sqlite3_result_null(sqlite3_context *pCtx){ 000512 #ifdef SQLITE_ENABLE_API_ARMOR 000513 if( pCtx==0 ) return; 000514 #endif 000515 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000516 sqlite3VdbeMemSetNull(pCtx->pOut); 000517 } 000518 void sqlite3_result_pointer( 000519 sqlite3_context *pCtx, 000520 void *pPtr, 000521 const char *zPType, 000522 void (*xDestructor)(void*) 000523 ){ 000524 Mem *pOut; 000525 #ifdef SQLITE_ENABLE_API_ARMOR 000526 if( pCtx==0 ){ 000527 invokeValueDestructor(pPtr, xDestructor, 0); 000528 return; 000529 } 000530 #endif 000531 pOut = pCtx->pOut; 000532 assert( sqlite3_mutex_held(pOut->db->mutex) ); 000533 sqlite3VdbeMemRelease(pOut); 000534 pOut->flags = MEM_Null; 000535 sqlite3VdbeMemSetPointer(pOut, pPtr, zPType, xDestructor); 000536 } 000537 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 000538 Mem *pOut; 000539 #ifdef SQLITE_ENABLE_API_ARMOR 000540 if( pCtx==0 ) return; 000541 #endif 000542 #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0 000543 if( pCtx->pFunc!=0 000544 && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0 000545 ){ 000546 char zErr[200]; 000547 sqlite3_snprintf(sizeof(zErr), zErr, 000548 "misuse of sqlite3_result_subtype() by %s()", 000549 pCtx->pFunc->zName); 000550 sqlite3_result_error(pCtx, zErr, -1); 000551 return; 000552 } 000553 #endif /* SQLITE_STRICT_SUBTYPE */ 000554 pOut = pCtx->pOut; 000555 assert( sqlite3_mutex_held(pOut->db->mutex) ); 000556 pOut->eSubtype = eSubtype & 0xff; 000557 pOut->flags |= MEM_Subtype; 000558 } 000559 void sqlite3_result_text( 000560 sqlite3_context *pCtx, 000561 const char *z, 000562 int n, 000563 void (*xDel)(void *) 000564 ){ 000565 #ifdef SQLITE_ENABLE_API_ARMOR 000566 if( pCtx==0 ){ 000567 invokeValueDestructor(z, xDel, 0); 000568 return; 000569 } 000570 #endif 000571 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000572 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 000573 } 000574 void sqlite3_result_text64( 000575 sqlite3_context *pCtx, 000576 const char *z, 000577 sqlite3_uint64 n, 000578 void (*xDel)(void *), 000579 unsigned char enc 000580 ){ 000581 #ifdef SQLITE_ENABLE_API_ARMOR 000582 if( pCtx==0 ){ 000583 invokeValueDestructor(z, xDel, 0); 000584 return; 000585 } 000586 #endif 000587 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000588 assert( xDel!=SQLITE_DYNAMIC ); 000589 if( enc!=SQLITE_UTF8 ){ 000590 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 000591 n &= ~(u64)1; 000592 } 000593 if( n>0x7fffffff ){ 000594 (void)invokeValueDestructor(z, xDel, pCtx); 000595 }else{ 000596 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 000597 sqlite3VdbeMemZeroTerminateIfAble(pCtx->pOut); 000598 } 000599 } 000600 #ifndef SQLITE_OMIT_UTF16 000601 void sqlite3_result_text16( 000602 sqlite3_context *pCtx, 000603 const void *z, 000604 int n, 000605 void (*xDel)(void *) 000606 ){ 000607 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000608 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16NATIVE, xDel); 000609 } 000610 void sqlite3_result_text16be( 000611 sqlite3_context *pCtx, 000612 const void *z, 000613 int n, 000614 void (*xDel)(void *) 000615 ){ 000616 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000617 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16BE, xDel); 000618 } 000619 void sqlite3_result_text16le( 000620 sqlite3_context *pCtx, 000621 const void *z, 000622 int n, 000623 void (*xDel)(void *) 000624 ){ 000625 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000626 setResultStrOrError(pCtx, z, n & ~(u64)1, SQLITE_UTF16LE, xDel); 000627 } 000628 #endif /* SQLITE_OMIT_UTF16 */ 000629 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 000630 Mem *pOut; 000631 000632 #ifdef SQLITE_ENABLE_API_ARMOR 000633 if( pCtx==0 ) return; 000634 if( pValue==0 ){ 000635 sqlite3_result_null(pCtx); 000636 return; 000637 } 000638 #endif 000639 pOut = pCtx->pOut; 000640 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000641 sqlite3VdbeMemCopy(pOut, pValue); 000642 sqlite3VdbeChangeEncoding(pOut, pCtx->enc); 000643 if( sqlite3VdbeMemTooBig(pOut) ){ 000644 sqlite3_result_error_toobig(pCtx); 000645 } 000646 } 000647 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 000648 sqlite3_result_zeroblob64(pCtx, n>0 ? n : 0); 000649 } 000650 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 000651 Mem *pOut; 000652 000653 #ifdef SQLITE_ENABLE_API_ARMOR 000654 if( pCtx==0 ) return SQLITE_MISUSE_BKPT; 000655 #endif 000656 pOut = pCtx->pOut; 000657 assert( sqlite3_mutex_held(pOut->db->mutex) ); 000658 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 000659 sqlite3_result_error_toobig(pCtx); 000660 return SQLITE_TOOBIG; 000661 } 000662 #ifndef SQLITE_OMIT_INCRBLOB 000663 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 000664 return SQLITE_OK; 000665 #else 000666 return sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 000667 #endif 000668 } 000669 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 000670 #ifdef SQLITE_ENABLE_API_ARMOR 000671 if( pCtx==0 ) return; 000672 #endif 000673 pCtx->isError = errCode ? errCode : -1; 000674 #ifdef SQLITE_DEBUG 000675 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 000676 #endif 000677 if( pCtx->pOut->flags & MEM_Null ){ 000678 setResultStrOrError(pCtx, sqlite3ErrStr(errCode), -1, SQLITE_UTF8, 000679 SQLITE_STATIC); 000680 } 000681 } 000682 000683 /* Force an SQLITE_TOOBIG error. */ 000684 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 000685 #ifdef SQLITE_ENABLE_API_ARMOR 000686 if( pCtx==0 ) return; 000687 #endif 000688 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000689 pCtx->isError = SQLITE_TOOBIG; 000690 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 000691 SQLITE_UTF8, SQLITE_STATIC); 000692 } 000693 000694 /* An SQLITE_NOMEM error. */ 000695 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 000696 #ifdef SQLITE_ENABLE_API_ARMOR 000697 if( pCtx==0 ) return; 000698 #endif 000699 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000700 sqlite3VdbeMemSetNull(pCtx->pOut); 000701 pCtx->isError = SQLITE_NOMEM_BKPT; 000702 sqlite3OomFault(pCtx->pOut->db); 000703 } 000704 000705 #ifndef SQLITE_UNTESTABLE 000706 /* Force the INT64 value currently stored as the result to be 000707 ** a MEM_IntReal value. See the SQLITE_TESTCTRL_RESULT_INTREAL 000708 ** test-control. 000709 */ 000710 void sqlite3ResultIntReal(sqlite3_context *pCtx){ 000711 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000712 if( pCtx->pOut->flags & MEM_Int ){ 000713 pCtx->pOut->flags &= ~MEM_Int; 000714 pCtx->pOut->flags |= MEM_IntReal; 000715 } 000716 } 000717 #endif 000718 000719 000720 /* 000721 ** This function is called after a transaction has been committed. It 000722 ** invokes callbacks registered with sqlite3_wal_hook() as required. 000723 */ 000724 static int doWalCallbacks(sqlite3 *db){ 000725 int rc = SQLITE_OK; 000726 #ifndef SQLITE_OMIT_WAL 000727 int i; 000728 for(i=0; i<db->nDb; i++){ 000729 Btree *pBt = db->aDb[i].pBt; 000730 if( pBt ){ 000731 int nEntry; 000732 sqlite3BtreeEnter(pBt); 000733 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 000734 sqlite3BtreeLeave(pBt); 000735 if( nEntry>0 && db->xWalCallback && rc==SQLITE_OK ){ 000736 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); 000737 } 000738 } 000739 } 000740 #endif 000741 return rc; 000742 } 000743 000744 000745 /* 000746 ** Execute the statement pStmt, either until a row of data is ready, the 000747 ** statement is completely executed or an error occurs. 000748 ** 000749 ** This routine implements the bulk of the logic behind the sqlite_step() 000750 ** API. The only thing omitted is the automatic recompile if a 000751 ** schema change has occurred. That detail is handled by the 000752 ** outer sqlite3_step() wrapper procedure. 000753 */ 000754 static int sqlite3Step(Vdbe *p){ 000755 sqlite3 *db; 000756 int rc; 000757 000758 assert(p); 000759 db = p->db; 000760 if( p->eVdbeState!=VDBE_RUN_STATE ){ 000761 restart_step: 000762 if( p->eVdbeState==VDBE_READY_STATE ){ 000763 if( p->expired ){ 000764 p->rc = SQLITE_SCHEMA; 000765 rc = SQLITE_ERROR; 000766 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 000767 /* If this statement was prepared using saved SQL and an 000768 ** error has occurred, then return the error code in p->rc to the 000769 ** caller. Set the error code in the database handle to the same 000770 ** value. 000771 */ 000772 rc = sqlite3VdbeTransferError(p); 000773 } 000774 goto end_of_step; 000775 } 000776 000777 /* If there are no other statements currently running, then 000778 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 000779 ** from interrupting a statement that has not yet started. 000780 */ 000781 if( db->nVdbeActive==0 ){ 000782 AtomicStore(&db->u1.isInterrupted, 0); 000783 } 000784 000785 assert( db->nVdbeWrite>0 || db->autoCommit==0 000786 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 000787 ); 000788 000789 #ifndef SQLITE_OMIT_TRACE 000790 if( (db->mTrace & (SQLITE_TRACE_PROFILE|SQLITE_TRACE_XPROFILE))!=0 000791 && !db->init.busy && p->zSql ){ 000792 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 000793 }else{ 000794 assert( p->startTime==0 ); 000795 } 000796 #endif 000797 000798 db->nVdbeActive++; 000799 if( p->readOnly==0 ) db->nVdbeWrite++; 000800 if( p->bIsReader ) db->nVdbeRead++; 000801 p->pc = 0; 000802 p->eVdbeState = VDBE_RUN_STATE; 000803 }else 000804 000805 if( ALWAYS(p->eVdbeState==VDBE_HALT_STATE) ){ 000806 /* We used to require that sqlite3_reset() be called before retrying 000807 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 000808 ** with version 3.7.0, we changed this so that sqlite3_reset() would 000809 ** be called automatically instead of throwing the SQLITE_MISUSE error. 000810 ** This "automatic-reset" change is not technically an incompatibility, 000811 ** since any application that receives an SQLITE_MISUSE is broken by 000812 ** definition. 000813 ** 000814 ** Nevertheless, some published applications that were originally written 000815 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 000816 ** returns, and those were broken by the automatic-reset change. As a 000817 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 000818 ** legacy behavior of returning SQLITE_MISUSE for cases where the 000819 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 000820 ** or SQLITE_BUSY error. 000821 */ 000822 #ifdef SQLITE_OMIT_AUTORESET 000823 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 000824 sqlite3_reset((sqlite3_stmt*)p); 000825 }else{ 000826 return SQLITE_MISUSE_BKPT; 000827 } 000828 #else 000829 sqlite3_reset((sqlite3_stmt*)p); 000830 #endif 000831 assert( p->eVdbeState==VDBE_READY_STATE ); 000832 goto restart_step; 000833 } 000834 } 000835 000836 #ifdef SQLITE_DEBUG 000837 p->rcApp = SQLITE_OK; 000838 #endif 000839 #ifndef SQLITE_OMIT_EXPLAIN 000840 if( p->explain ){ 000841 rc = sqlite3VdbeList(p); 000842 }else 000843 #endif /* SQLITE_OMIT_EXPLAIN */ 000844 { 000845 db->nVdbeExec++; 000846 rc = sqlite3VdbeExec(p); 000847 db->nVdbeExec--; 000848 } 000849 000850 if( rc==SQLITE_ROW ){ 000851 assert( p->rc==SQLITE_OK ); 000852 assert( db->mallocFailed==0 ); 000853 db->errCode = SQLITE_ROW; 000854 return SQLITE_ROW; 000855 }else{ 000856 #ifndef SQLITE_OMIT_TRACE 000857 /* If the statement completed successfully, invoke the profile callback */ 000858 checkProfileCallback(db, p); 000859 #endif 000860 p->pResultRow = 0; 000861 if( rc==SQLITE_DONE && db->autoCommit ){ 000862 assert( p->rc==SQLITE_OK ); 000863 p->rc = doWalCallbacks(db); 000864 if( p->rc!=SQLITE_OK ){ 000865 rc = SQLITE_ERROR; 000866 } 000867 }else if( rc!=SQLITE_DONE && (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ){ 000868 /* If this statement was prepared using saved SQL and an 000869 ** error has occurred, then return the error code in p->rc to the 000870 ** caller. Set the error code in the database handle to the same value. 000871 */ 000872 rc = sqlite3VdbeTransferError(p); 000873 } 000874 } 000875 000876 db->errCode = rc; 000877 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 000878 p->rc = SQLITE_NOMEM_BKPT; 000879 if( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 ) rc = p->rc; 000880 } 000881 end_of_step: 000882 /* There are only a limited number of result codes allowed from the 000883 ** statements prepared using the legacy sqlite3_prepare() interface */ 000884 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 000885 || rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 000886 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 000887 ); 000888 return (rc&db->errMask); 000889 } 000890 000891 /* 000892 ** This is the top-level implementation of sqlite3_step(). Call 000893 ** sqlite3Step() to do most of the work. If a schema error occurs, 000894 ** call sqlite3Reprepare() and try again. 000895 */ 000896 int sqlite3_step(sqlite3_stmt *pStmt){ 000897 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 000898 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 000899 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 000900 sqlite3 *db; /* The database connection */ 000901 000902 if( vdbeSafetyNotNull(v) ){ 000903 return SQLITE_MISUSE_BKPT; 000904 } 000905 db = v->db; 000906 sqlite3_mutex_enter(db->mutex); 000907 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 000908 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 000909 int savedPc = v->pc; 000910 rc = sqlite3Reprepare(v); 000911 if( rc!=SQLITE_OK ){ 000912 /* This case occurs after failing to recompile an sql statement. 000913 ** The error message from the SQL compiler has already been loaded 000914 ** into the database handle. This block copies the error message 000915 ** from the database handle into the statement and sets the statement 000916 ** program counter to 0 to ensure that when the statement is 000917 ** finalized or reset the parser error message is available via 000918 ** sqlite3_errmsg() and sqlite3_errcode(). 000919 */ 000920 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 000921 sqlite3DbFree(db, v->zErrMsg); 000922 if( !db->mallocFailed ){ 000923 v->zErrMsg = sqlite3DbStrDup(db, zErr); 000924 v->rc = rc = sqlite3ApiExit(db, rc); 000925 } else { 000926 v->zErrMsg = 0; 000927 v->rc = rc = SQLITE_NOMEM_BKPT; 000928 } 000929 break; 000930 } 000931 sqlite3_reset(pStmt); 000932 if( savedPc>=0 ){ 000933 /* Setting minWriteFileFormat to 254 is a signal to the OP_Init and 000934 ** OP_Trace opcodes to *not* perform SQLITE_TRACE_STMT because it has 000935 ** already been done once on a prior invocation that failed due to 000936 ** SQLITE_SCHEMA. tag-20220401a */ 000937 v->minWriteFileFormat = 254; 000938 } 000939 assert( v->expired==0 ); 000940 } 000941 sqlite3_mutex_leave(db->mutex); 000942 return rc; 000943 } 000944 000945 000946 /* 000947 ** Extract the user data from a sqlite3_context structure and return a 000948 ** pointer to it. 000949 */ 000950 void *sqlite3_user_data(sqlite3_context *p){ 000951 #ifdef SQLITE_ENABLE_API_ARMOR 000952 if( p==0 ) return 0; 000953 #endif 000954 assert( p && p->pFunc ); 000955 return p->pFunc->pUserData; 000956 } 000957 000958 /* 000959 ** Extract the user data from a sqlite3_context structure and return a 000960 ** pointer to it. 000961 ** 000962 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 000963 ** returns a copy of the pointer to the database connection (the 1st 000964 ** parameter) of the sqlite3_create_function() and 000965 ** sqlite3_create_function16() routines that originally registered the 000966 ** application defined function. 000967 */ 000968 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 000969 #ifdef SQLITE_ENABLE_API_ARMOR 000970 if( p==0 ) return 0; 000971 #else 000972 assert( p && p->pOut ); 000973 #endif 000974 return p->pOut->db; 000975 } 000976 000977 /* 000978 ** If this routine is invoked from within an xColumn method of a virtual 000979 ** table, then it returns true if and only if the the call is during an 000980 ** UPDATE operation and the value of the column will not be modified 000981 ** by the UPDATE. 000982 ** 000983 ** If this routine is called from any context other than within the 000984 ** xColumn method of a virtual table, then the return value is meaningless 000985 ** and arbitrary. 000986 ** 000987 ** Virtual table implements might use this routine to optimize their 000988 ** performance by substituting a NULL result, or some other light-weight 000989 ** value, as a signal to the xUpdate routine that the column is unchanged. 000990 */ 000991 int sqlite3_vtab_nochange(sqlite3_context *p){ 000992 #ifdef SQLITE_ENABLE_API_ARMOR 000993 if( p==0 ) return 0; 000994 #else 000995 assert( p ); 000996 #endif 000997 return sqlite3_value_nochange(p->pOut); 000998 } 000999 001000 /* 001001 ** The destructor function for a ValueList object. This needs to be 001002 ** a separate function, unknowable to the application, to ensure that 001003 ** calls to sqlite3_vtab_in_first()/sqlite3_vtab_in_next() that are not 001004 ** preceded by activation of IN processing via sqlite3_vtab_int() do not 001005 ** try to access a fake ValueList object inserted by a hostile extension. 001006 */ 001007 void sqlite3VdbeValueListFree(void *pToDelete){ 001008 sqlite3_free(pToDelete); 001009 } 001010 001011 /* 001012 ** Implementation of sqlite3_vtab_in_first() (if bNext==0) and 001013 ** sqlite3_vtab_in_next() (if bNext!=0). 001014 */ 001015 static int valueFromValueList( 001016 sqlite3_value *pVal, /* Pointer to the ValueList object */ 001017 sqlite3_value **ppOut, /* Store the next value from the list here */ 001018 int bNext /* 1 for _next(). 0 for _first() */ 001019 ){ 001020 int rc; 001021 ValueList *pRhs; 001022 001023 *ppOut = 0; 001024 if( pVal==0 ) return SQLITE_MISUSE_BKPT; 001025 if( (pVal->flags & MEM_Dyn)==0 || pVal->xDel!=sqlite3VdbeValueListFree ){ 001026 return SQLITE_ERROR; 001027 }else{ 001028 assert( (pVal->flags&(MEM_TypeMask|MEM_Term|MEM_Subtype)) == 001029 (MEM_Null|MEM_Term|MEM_Subtype) ); 001030 assert( pVal->eSubtype=='p' ); 001031 assert( pVal->u.zPType!=0 && strcmp(pVal->u.zPType,"ValueList")==0 ); 001032 pRhs = (ValueList*)pVal->z; 001033 } 001034 if( bNext ){ 001035 rc = sqlite3BtreeNext(pRhs->pCsr, 0); 001036 }else{ 001037 int dummy = 0; 001038 rc = sqlite3BtreeFirst(pRhs->pCsr, &dummy); 001039 assert( rc==SQLITE_OK || sqlite3BtreeEof(pRhs->pCsr) ); 001040 if( sqlite3BtreeEof(pRhs->pCsr) ) rc = SQLITE_DONE; 001041 } 001042 if( rc==SQLITE_OK ){ 001043 u32 sz; /* Size of current row in bytes */ 001044 Mem sMem; /* Raw content of current row */ 001045 memset(&sMem, 0, sizeof(sMem)); 001046 sz = sqlite3BtreePayloadSize(pRhs->pCsr); 001047 rc = sqlite3VdbeMemFromBtreeZeroOffset(pRhs->pCsr,(int)sz,&sMem); 001048 if( rc==SQLITE_OK ){ 001049 u8 *zBuf = (u8*)sMem.z; 001050 u32 iSerial; 001051 sqlite3_value *pOut = pRhs->pOut; 001052 int iOff = 1 + getVarint32(&zBuf[1], iSerial); 001053 sqlite3VdbeSerialGet(&zBuf[iOff], iSerial, pOut); 001054 pOut->enc = ENC(pOut->db); 001055 if( (pOut->flags & MEM_Ephem)!=0 && sqlite3VdbeMemMakeWriteable(pOut) ){ 001056 rc = SQLITE_NOMEM; 001057 }else{ 001058 *ppOut = pOut; 001059 } 001060 } 001061 sqlite3VdbeMemRelease(&sMem); 001062 } 001063 return rc; 001064 } 001065 001066 /* 001067 ** Set the iterator value pVal to point to the first value in the set. 001068 ** Set (*ppOut) to point to this value before returning. 001069 */ 001070 int sqlite3_vtab_in_first(sqlite3_value *pVal, sqlite3_value **ppOut){ 001071 return valueFromValueList(pVal, ppOut, 0); 001072 } 001073 001074 /* 001075 ** Set the iterator value pVal to point to the next value in the set. 001076 ** Set (*ppOut) to point to this value before returning. 001077 */ 001078 int sqlite3_vtab_in_next(sqlite3_value *pVal, sqlite3_value **ppOut){ 001079 return valueFromValueList(pVal, ppOut, 1); 001080 } 001081 001082 /* 001083 ** Return the current time for a statement. If the current time 001084 ** is requested more than once within the same run of a single prepared 001085 ** statement, the exact same time is returned for each invocation regardless 001086 ** of the amount of time that elapses between invocations. In other words, 001087 ** the time returned is always the time of the first call. 001088 */ 001089 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 001090 int rc; 001091 #ifndef SQLITE_ENABLE_STAT4 001092 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 001093 assert( p->pVdbe!=0 ); 001094 #else 001095 sqlite3_int64 iTime = 0; 001096 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 001097 #endif 001098 if( *piTime==0 ){ 001099 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 001100 if( rc ) *piTime = 0; 001101 } 001102 return *piTime; 001103 } 001104 001105 /* 001106 ** Create a new aggregate context for p and return a pointer to 001107 ** its pMem->z element. 001108 */ 001109 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 001110 Mem *pMem = p->pMem; 001111 assert( (pMem->flags & MEM_Agg)==0 ); 001112 if( nByte<=0 ){ 001113 sqlite3VdbeMemSetNull(pMem); 001114 pMem->z = 0; 001115 }else{ 001116 sqlite3VdbeMemClearAndResize(pMem, nByte); 001117 pMem->flags = MEM_Agg; 001118 pMem->u.pDef = p->pFunc; 001119 if( pMem->z ){ 001120 memset(pMem->z, 0, nByte); 001121 } 001122 } 001123 return (void*)pMem->z; 001124 } 001125 001126 /* 001127 ** Allocate or return the aggregate context for a user function. A new 001128 ** context is allocated on the first call. Subsequent calls return the 001129 ** same context that was returned on prior calls. 001130 */ 001131 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 001132 assert( p && p->pFunc && p->pFunc->xFinalize ); 001133 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 001134 testcase( nByte<0 ); 001135 if( (p->pMem->flags & MEM_Agg)==0 ){ 001136 return createAggContext(p, nByte); 001137 }else{ 001138 return (void*)p->pMem->z; 001139 } 001140 } 001141 001142 /* 001143 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 001144 ** the user-function defined by pCtx. 001145 ** 001146 ** The left-most argument is 0. 001147 ** 001148 ** Undocumented behavior: If iArg is negative then access a cache of 001149 ** auxiliary data pointers that is available to all functions within a 001150 ** single prepared statement. The iArg values must match. 001151 */ 001152 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 001153 AuxData *pAuxData; 001154 001155 #ifdef SQLITE_ENABLE_API_ARMOR 001156 if( pCtx==0 ) return 0; 001157 #endif 001158 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 001159 #if SQLITE_ENABLE_STAT4 001160 if( pCtx->pVdbe==0 ) return 0; 001161 #else 001162 assert( pCtx->pVdbe!=0 ); 001163 #endif 001164 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 001165 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 001166 return pAuxData->pAux; 001167 } 001168 } 001169 return 0; 001170 } 001171 001172 /* 001173 ** Set the auxiliary data pointer and delete function, for the iArg'th 001174 ** argument to the user-function defined by pCtx. Any previous value is 001175 ** deleted by calling the delete function specified when it was set. 001176 ** 001177 ** The left-most argument is 0. 001178 ** 001179 ** Undocumented behavior: If iArg is negative then make the data available 001180 ** to all functions within the current prepared statement using iArg as an 001181 ** access code. 001182 */ 001183 void sqlite3_set_auxdata( 001184 sqlite3_context *pCtx, 001185 int iArg, 001186 void *pAux, 001187 void (*xDelete)(void*) 001188 ){ 001189 AuxData *pAuxData; 001190 Vdbe *pVdbe; 001191 001192 #ifdef SQLITE_ENABLE_API_ARMOR 001193 if( pCtx==0 ) return; 001194 #endif 001195 pVdbe= pCtx->pVdbe; 001196 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 001197 #ifdef SQLITE_ENABLE_STAT4 001198 if( pVdbe==0 ) goto failed; 001199 #else 001200 assert( pVdbe!=0 ); 001201 #endif 001202 001203 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){ 001204 if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){ 001205 break; 001206 } 001207 } 001208 if( pAuxData==0 ){ 001209 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 001210 if( !pAuxData ) goto failed; 001211 pAuxData->iAuxOp = pCtx->iOp; 001212 pAuxData->iAuxArg = iArg; 001213 pAuxData->pNextAux = pVdbe->pAuxData; 001214 pVdbe->pAuxData = pAuxData; 001215 if( pCtx->isError==0 ) pCtx->isError = -1; 001216 }else if( pAuxData->xDeleteAux ){ 001217 pAuxData->xDeleteAux(pAuxData->pAux); 001218 } 001219 001220 pAuxData->pAux = pAux; 001221 pAuxData->xDeleteAux = xDelete; 001222 return; 001223 001224 failed: 001225 if( xDelete ){ 001226 xDelete(pAux); 001227 } 001228 } 001229 001230 #ifndef SQLITE_OMIT_DEPRECATED 001231 /* 001232 ** Return the number of times the Step function of an aggregate has been 001233 ** called. 001234 ** 001235 ** This function is deprecated. Do not use it for new code. It is 001236 ** provide only to avoid breaking legacy code. New aggregate function 001237 ** implementations should keep their own counts within their aggregate 001238 ** context. 001239 */ 001240 int sqlite3_aggregate_count(sqlite3_context *p){ 001241 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 001242 return p->pMem->n; 001243 } 001244 #endif 001245 001246 /* 001247 ** Return the number of columns in the result set for the statement pStmt. 001248 */ 001249 int sqlite3_column_count(sqlite3_stmt *pStmt){ 001250 Vdbe *pVm = (Vdbe *)pStmt; 001251 if( pVm==0 ) return 0; 001252 return pVm->nResColumn; 001253 } 001254 001255 /* 001256 ** Return the number of values available from the current row of the 001257 ** currently executing statement pStmt. 001258 */ 001259 int sqlite3_data_count(sqlite3_stmt *pStmt){ 001260 Vdbe *pVm = (Vdbe *)pStmt; 001261 if( pVm==0 || pVm->pResultRow==0 ) return 0; 001262 return pVm->nResColumn; 001263 } 001264 001265 /* 001266 ** Return a pointer to static memory containing an SQL NULL value. 001267 */ 001268 static const Mem *columnNullValue(void){ 001269 /* Even though the Mem structure contains an element 001270 ** of type i64, on certain architectures (x86) with certain compiler 001271 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 001272 ** instead of an 8-byte one. This all works fine, except that when 001273 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 001274 ** that a Mem structure is located on an 8-byte boundary. To prevent 001275 ** these assert()s from failing, when building with SQLITE_DEBUG defined 001276 ** using gcc, we force nullMem to be 8-byte aligned using the magical 001277 ** __attribute__((aligned(8))) macro. */ 001278 static const Mem nullMem 001279 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 001280 __attribute__((aligned(8))) 001281 #endif 001282 = { 001283 /* .u = */ {0}, 001284 /* .z = */ (char*)0, 001285 /* .n = */ (int)0, 001286 /* .flags = */ (u16)MEM_Null, 001287 /* .enc = */ (u8)0, 001288 /* .eSubtype = */ (u8)0, 001289 /* .db = */ (sqlite3*)0, 001290 /* .szMalloc = */ (int)0, 001291 /* .uTemp = */ (u32)0, 001292 /* .zMalloc = */ (char*)0, 001293 /* .xDel = */ (void(*)(void*))0, 001294 #ifdef SQLITE_DEBUG 001295 /* .pScopyFrom = */ (Mem*)0, 001296 /* .mScopyFlags= */ 0, 001297 #endif 001298 }; 001299 return &nullMem; 001300 } 001301 001302 /* 001303 ** Check to see if column iCol of the given statement is valid. If 001304 ** it is, return a pointer to the Mem for the value of that column. 001305 ** If iCol is not valid, return a pointer to a Mem which has a value 001306 ** of NULL. 001307 */ 001308 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 001309 Vdbe *pVm; 001310 Mem *pOut; 001311 001312 pVm = (Vdbe *)pStmt; 001313 if( pVm==0 ) return (Mem*)columnNullValue(); 001314 assert( pVm->db ); 001315 sqlite3_mutex_enter(pVm->db->mutex); 001316 if( pVm->pResultRow!=0 && i<pVm->nResColumn && i>=0 ){ 001317 pOut = &pVm->pResultRow[i]; 001318 }else{ 001319 sqlite3Error(pVm->db, SQLITE_RANGE); 001320 pOut = (Mem*)columnNullValue(); 001321 } 001322 return pOut; 001323 } 001324 001325 /* 001326 ** This function is called after invoking an sqlite3_value_XXX function on a 001327 ** column value (i.e. a value returned by evaluating an SQL expression in the 001328 ** select list of a SELECT statement) that may cause a malloc() failure. If 001329 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 001330 ** code of statement pStmt set to SQLITE_NOMEM. 001331 ** 001332 ** Specifically, this is called from within: 001333 ** 001334 ** sqlite3_column_int() 001335 ** sqlite3_column_int64() 001336 ** sqlite3_column_text() 001337 ** sqlite3_column_text16() 001338 ** sqlite3_column_real() 001339 ** sqlite3_column_bytes() 001340 ** sqlite3_column_bytes16() 001341 ** sqlite3_column_blob() 001342 */ 001343 static void columnMallocFailure(sqlite3_stmt *pStmt) 001344 { 001345 /* If malloc() failed during an encoding conversion within an 001346 ** sqlite3_column_XXX API, then set the return code of the statement to 001347 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 001348 ** and _finalize() will return NOMEM. 001349 */ 001350 Vdbe *p = (Vdbe *)pStmt; 001351 if( p ){ 001352 assert( p->db!=0 ); 001353 assert( sqlite3_mutex_held(p->db->mutex) ); 001354 p->rc = sqlite3ApiExit(p->db, p->rc); 001355 sqlite3_mutex_leave(p->db->mutex); 001356 } 001357 } 001358 001359 /**************************** sqlite3_column_ ******************************* 001360 ** The following routines are used to access elements of the current row 001361 ** in the result set. 001362 */ 001363 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 001364 const void *val; 001365 val = sqlite3_value_blob( columnMem(pStmt,i) ); 001366 /* Even though there is no encoding conversion, value_blob() might 001367 ** need to call malloc() to expand the result of a zeroblob() 001368 ** expression. 001369 */ 001370 columnMallocFailure(pStmt); 001371 return val; 001372 } 001373 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 001374 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 001375 columnMallocFailure(pStmt); 001376 return val; 001377 } 001378 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 001379 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 001380 columnMallocFailure(pStmt); 001381 return val; 001382 } 001383 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 001384 double val = sqlite3_value_double( columnMem(pStmt,i) ); 001385 columnMallocFailure(pStmt); 001386 return val; 001387 } 001388 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 001389 int val = sqlite3_value_int( columnMem(pStmt,i) ); 001390 columnMallocFailure(pStmt); 001391 return val; 001392 } 001393 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 001394 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 001395 columnMallocFailure(pStmt); 001396 return val; 001397 } 001398 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 001399 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 001400 columnMallocFailure(pStmt); 001401 return val; 001402 } 001403 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 001404 Mem *pOut = columnMem(pStmt, i); 001405 if( pOut->flags&MEM_Static ){ 001406 pOut->flags &= ~MEM_Static; 001407 pOut->flags |= MEM_Ephem; 001408 } 001409 columnMallocFailure(pStmt); 001410 return (sqlite3_value *)pOut; 001411 } 001412 #ifndef SQLITE_OMIT_UTF16 001413 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 001414 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 001415 columnMallocFailure(pStmt); 001416 return val; 001417 } 001418 #endif /* SQLITE_OMIT_UTF16 */ 001419 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 001420 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 001421 columnMallocFailure(pStmt); 001422 return iType; 001423 } 001424 001425 /* 001426 ** Column names appropriate for EXPLAIN or EXPLAIN QUERY PLAN. 001427 */ 001428 static const char * const azExplainColNames8[] = { 001429 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", /* EXPLAIN */ 001430 "id", "parent", "notused", "detail" /* EQP */ 001431 }; 001432 static const u16 azExplainColNames16data[] = { 001433 /* 0 */ 'a', 'd', 'd', 'r', 0, 001434 /* 5 */ 'o', 'p', 'c', 'o', 'd', 'e', 0, 001435 /* 12 */ 'p', '1', 0, 001436 /* 15 */ 'p', '2', 0, 001437 /* 18 */ 'p', '3', 0, 001438 /* 21 */ 'p', '4', 0, 001439 /* 24 */ 'p', '5', 0, 001440 /* 27 */ 'c', 'o', 'm', 'm', 'e', 'n', 't', 0, 001441 /* 35 */ 'i', 'd', 0, 001442 /* 38 */ 'p', 'a', 'r', 'e', 'n', 't', 0, 001443 /* 45 */ 'n', 'o', 't', 'u', 's', 'e', 'd', 0, 001444 /* 53 */ 'd', 'e', 't', 'a', 'i', 'l', 0 001445 }; 001446 static const u8 iExplainColNames16[] = { 001447 0, 5, 12, 15, 18, 21, 24, 27, 001448 35, 38, 45, 53 001449 }; 001450 001451 /* 001452 ** Convert the N-th element of pStmt->pColName[] into a string using 001453 ** xFunc() then return that string. If N is out of range, return 0. 001454 ** 001455 ** There are up to 5 names for each column. useType determines which 001456 ** name is returned. Here are the names: 001457 ** 001458 ** 0 The column name as it should be displayed for output 001459 ** 1 The datatype name for the column 001460 ** 2 The name of the database that the column derives from 001461 ** 3 The name of the table that the column derives from 001462 ** 4 The name of the table column that the result column derives from 001463 ** 001464 ** If the result is not a simple column reference (if it is an expression 001465 ** or a constant) then useTypes 2, 3, and 4 return NULL. 001466 */ 001467 static const void *columnName( 001468 sqlite3_stmt *pStmt, /* The statement */ 001469 int N, /* Which column to get the name for */ 001470 int useUtf16, /* True to return the name as UTF16 */ 001471 int useType /* What type of name */ 001472 ){ 001473 const void *ret; 001474 Vdbe *p; 001475 int n; 001476 sqlite3 *db; 001477 #ifdef SQLITE_ENABLE_API_ARMOR 001478 if( pStmt==0 ){ 001479 (void)SQLITE_MISUSE_BKPT; 001480 return 0; 001481 } 001482 #endif 001483 if( N<0 ) return 0; 001484 ret = 0; 001485 p = (Vdbe *)pStmt; 001486 db = p->db; 001487 assert( db!=0 ); 001488 sqlite3_mutex_enter(db->mutex); 001489 001490 if( p->explain ){ 001491 if( useType>0 ) goto columnName_end; 001492 n = p->explain==1 ? 8 : 4; 001493 if( N>=n ) goto columnName_end; 001494 if( useUtf16 ){ 001495 int i = iExplainColNames16[N + 8*p->explain - 8]; 001496 ret = (void*)&azExplainColNames16data[i]; 001497 }else{ 001498 ret = (void*)azExplainColNames8[N + 8*p->explain - 8]; 001499 } 001500 goto columnName_end; 001501 } 001502 n = p->nResColumn; 001503 if( N<n ){ 001504 u8 prior_mallocFailed = db->mallocFailed; 001505 N += useType*n; 001506 #ifndef SQLITE_OMIT_UTF16 001507 if( useUtf16 ){ 001508 ret = sqlite3_value_text16((sqlite3_value*)&p->aColName[N]); 001509 }else 001510 #endif 001511 { 001512 ret = sqlite3_value_text((sqlite3_value*)&p->aColName[N]); 001513 } 001514 /* A malloc may have failed inside of the _text() call. If this 001515 ** is the case, clear the mallocFailed flag and return NULL. 001516 */ 001517 assert( db->mallocFailed==0 || db->mallocFailed==1 ); 001518 if( db->mallocFailed > prior_mallocFailed ){ 001519 sqlite3OomClear(db); 001520 ret = 0; 001521 } 001522 } 001523 columnName_end: 001524 sqlite3_mutex_leave(db->mutex); 001525 return ret; 001526 } 001527 001528 /* 001529 ** Return the name of the Nth column of the result set returned by SQL 001530 ** statement pStmt. 001531 */ 001532 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 001533 return columnName(pStmt, N, 0, COLNAME_NAME); 001534 } 001535 #ifndef SQLITE_OMIT_UTF16 001536 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 001537 return columnName(pStmt, N, 1, COLNAME_NAME); 001538 } 001539 #endif 001540 001541 /* 001542 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 001543 ** not define OMIT_DECLTYPE. 001544 */ 001545 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 001546 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 001547 and SQLITE_ENABLE_COLUMN_METADATA" 001548 #endif 001549 001550 #ifndef SQLITE_OMIT_DECLTYPE 001551 /* 001552 ** Return the column declaration type (if applicable) of the 'i'th column 001553 ** of the result set of SQL statement pStmt. 001554 */ 001555 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 001556 return columnName(pStmt, N, 0, COLNAME_DECLTYPE); 001557 } 001558 #ifndef SQLITE_OMIT_UTF16 001559 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 001560 return columnName(pStmt, N, 1, COLNAME_DECLTYPE); 001561 } 001562 #endif /* SQLITE_OMIT_UTF16 */ 001563 #endif /* SQLITE_OMIT_DECLTYPE */ 001564 001565 #ifdef SQLITE_ENABLE_COLUMN_METADATA 001566 /* 001567 ** Return the name of the database from which a result column derives. 001568 ** NULL is returned if the result column is an expression or constant or 001569 ** anything else which is not an unambiguous reference to a database column. 001570 */ 001571 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 001572 return columnName(pStmt, N, 0, COLNAME_DATABASE); 001573 } 001574 #ifndef SQLITE_OMIT_UTF16 001575 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 001576 return columnName(pStmt, N, 1, COLNAME_DATABASE); 001577 } 001578 #endif /* SQLITE_OMIT_UTF16 */ 001579 001580 /* 001581 ** Return the name of the table from which a result column derives. 001582 ** NULL is returned if the result column is an expression or constant or 001583 ** anything else which is not an unambiguous reference to a database column. 001584 */ 001585 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 001586 return columnName(pStmt, N, 0, COLNAME_TABLE); 001587 } 001588 #ifndef SQLITE_OMIT_UTF16 001589 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 001590 return columnName(pStmt, N, 1, COLNAME_TABLE); 001591 } 001592 #endif /* SQLITE_OMIT_UTF16 */ 001593 001594 /* 001595 ** Return the name of the table column from which a result column derives. 001596 ** NULL is returned if the result column is an expression or constant or 001597 ** anything else which is not an unambiguous reference to a database column. 001598 */ 001599 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 001600 return columnName(pStmt, N, 0, COLNAME_COLUMN); 001601 } 001602 #ifndef SQLITE_OMIT_UTF16 001603 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 001604 return columnName(pStmt, N, 1, COLNAME_COLUMN); 001605 } 001606 #endif /* SQLITE_OMIT_UTF16 */ 001607 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 001608 001609 001610 /******************************* sqlite3_bind_ *************************** 001611 ** 001612 ** Routines used to attach values to wildcards in a compiled SQL statement. 001613 */ 001614 /* 001615 ** Unbind the value bound to variable i in virtual machine p. This is the 001616 ** the same as binding a NULL value to the column. If the "i" parameter is 001617 ** out of range, then SQLITE_RANGE is returned. Otherwise SQLITE_OK. 001618 ** 001619 ** A successful evaluation of this routine acquires the mutex on p. 001620 ** the mutex is released if any kind of error occurs. 001621 ** 001622 ** The error code stored in database p->db is overwritten with the return 001623 ** value in any case. 001624 ** 001625 ** (tag-20240917-01) If vdbeUnbind(p,(u32)(i-1)) returns SQLITE_OK, 001626 ** that means all of the the following will be true: 001627 ** 001628 ** p!=0 001629 ** p->pVar!=0 001630 ** i>0 001631 ** i<=p->nVar 001632 ** 001633 ** An assert() is normally added after vdbeUnbind() to help static analyzers 001634 ** realize this. 001635 */ 001636 static int vdbeUnbind(Vdbe *p, unsigned int i){ 001637 Mem *pVar; 001638 if( vdbeSafetyNotNull(p) ){ 001639 return SQLITE_MISUSE_BKPT; 001640 } 001641 sqlite3_mutex_enter(p->db->mutex); 001642 if( p->eVdbeState!=VDBE_READY_STATE ){ 001643 sqlite3Error(p->db, SQLITE_MISUSE_BKPT); 001644 sqlite3_mutex_leave(p->db->mutex); 001645 sqlite3_log(SQLITE_MISUSE, 001646 "bind on a busy prepared statement: [%s]", p->zSql); 001647 return SQLITE_MISUSE_BKPT; 001648 } 001649 if( i>=(unsigned int)p->nVar ){ 001650 sqlite3Error(p->db, SQLITE_RANGE); 001651 sqlite3_mutex_leave(p->db->mutex); 001652 return SQLITE_RANGE; 001653 } 001654 pVar = &p->aVar[i]; 001655 sqlite3VdbeMemRelease(pVar); 001656 pVar->flags = MEM_Null; 001657 p->db->errCode = SQLITE_OK; 001658 001659 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 001660 ** binding a new value to this variable invalidates the current query plan. 001661 ** 001662 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host 001663 ** parameter in the WHERE clause might influence the choice of query plan 001664 ** for a statement, then the statement will be automatically recompiled, 001665 ** as if there had been a schema change, on the first sqlite3_step() call 001666 ** following any change to the bindings of that parameter. 001667 */ 001668 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 001669 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ 001670 p->expired = 1; 001671 } 001672 return SQLITE_OK; 001673 } 001674 001675 /* 001676 ** Bind a text or BLOB value. 001677 */ 001678 static int bindText( 001679 sqlite3_stmt *pStmt, /* The statement to bind against */ 001680 int i, /* Index of the parameter to bind */ 001681 const void *zData, /* Pointer to the data to be bound */ 001682 i64 nData, /* Number of bytes of data to be bound */ 001683 void (*xDel)(void*), /* Destructor for the data */ 001684 u8 encoding /* Encoding for the data */ 001685 ){ 001686 Vdbe *p = (Vdbe *)pStmt; 001687 Mem *pVar; 001688 int rc; 001689 001690 rc = vdbeUnbind(p, (u32)(i-1)); 001691 if( rc==SQLITE_OK ){ 001692 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001693 if( zData!=0 ){ 001694 pVar = &p->aVar[i-1]; 001695 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 001696 if( rc==SQLITE_OK && encoding!=0 ){ 001697 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 001698 } 001699 if( rc ){ 001700 sqlite3Error(p->db, rc); 001701 rc = sqlite3ApiExit(p->db, rc); 001702 } 001703 } 001704 sqlite3_mutex_leave(p->db->mutex); 001705 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 001706 xDel((void*)zData); 001707 } 001708 return rc; 001709 } 001710 001711 001712 /* 001713 ** Bind a blob value to an SQL statement variable. 001714 */ 001715 int sqlite3_bind_blob( 001716 sqlite3_stmt *pStmt, 001717 int i, 001718 const void *zData, 001719 int nData, 001720 void (*xDel)(void*) 001721 ){ 001722 #ifdef SQLITE_ENABLE_API_ARMOR 001723 if( nData<0 ) return SQLITE_MISUSE_BKPT; 001724 #endif 001725 return bindText(pStmt, i, zData, nData, xDel, 0); 001726 } 001727 int sqlite3_bind_blob64( 001728 sqlite3_stmt *pStmt, 001729 int i, 001730 const void *zData, 001731 sqlite3_uint64 nData, 001732 void (*xDel)(void*) 001733 ){ 001734 assert( xDel!=SQLITE_DYNAMIC ); 001735 return bindText(pStmt, i, zData, nData, xDel, 0); 001736 } 001737 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 001738 int rc; 001739 Vdbe *p = (Vdbe *)pStmt; 001740 rc = vdbeUnbind(p, (u32)(i-1)); 001741 if( rc==SQLITE_OK ){ 001742 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001743 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 001744 sqlite3_mutex_leave(p->db->mutex); 001745 } 001746 return rc; 001747 } 001748 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 001749 return sqlite3_bind_int64(p, i, (i64)iValue); 001750 } 001751 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 001752 int rc; 001753 Vdbe *p = (Vdbe *)pStmt; 001754 rc = vdbeUnbind(p, (u32)(i-1)); 001755 if( rc==SQLITE_OK ){ 001756 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001757 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 001758 sqlite3_mutex_leave(p->db->mutex); 001759 } 001760 return rc; 001761 } 001762 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 001763 int rc; 001764 Vdbe *p = (Vdbe*)pStmt; 001765 rc = vdbeUnbind(p, (u32)(i-1)); 001766 if( rc==SQLITE_OK ){ 001767 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001768 sqlite3_mutex_leave(p->db->mutex); 001769 } 001770 return rc; 001771 } 001772 int sqlite3_bind_pointer( 001773 sqlite3_stmt *pStmt, 001774 int i, 001775 void *pPtr, 001776 const char *zPTtype, 001777 void (*xDestructor)(void*) 001778 ){ 001779 int rc; 001780 Vdbe *p = (Vdbe*)pStmt; 001781 rc = vdbeUnbind(p, (u32)(i-1)); 001782 if( rc==SQLITE_OK ){ 001783 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001784 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); 001785 sqlite3_mutex_leave(p->db->mutex); 001786 }else if( xDestructor ){ 001787 xDestructor(pPtr); 001788 } 001789 return rc; 001790 } 001791 int sqlite3_bind_text( 001792 sqlite3_stmt *pStmt, 001793 int i, 001794 const char *zData, 001795 int nData, 001796 void (*xDel)(void*) 001797 ){ 001798 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 001799 } 001800 int sqlite3_bind_text64( 001801 sqlite3_stmt *pStmt, 001802 int i, 001803 const char *zData, 001804 sqlite3_uint64 nData, 001805 void (*xDel)(void*), 001806 unsigned char enc 001807 ){ 001808 assert( xDel!=SQLITE_DYNAMIC ); 001809 if( enc!=SQLITE_UTF8 ){ 001810 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 001811 nData &= ~(u16)1; 001812 } 001813 return bindText(pStmt, i, zData, nData, xDel, enc); 001814 } 001815 #ifndef SQLITE_OMIT_UTF16 001816 int sqlite3_bind_text16( 001817 sqlite3_stmt *pStmt, 001818 int i, 001819 const void *zData, 001820 int n, 001821 void (*xDel)(void*) 001822 ){ 001823 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE); 001824 } 001825 #endif /* SQLITE_OMIT_UTF16 */ 001826 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 001827 int rc; 001828 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 001829 case SQLITE_INTEGER: { 001830 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 001831 break; 001832 } 001833 case SQLITE_FLOAT: { 001834 assert( pValue->flags & (MEM_Real|MEM_IntReal) ); 001835 rc = sqlite3_bind_double(pStmt, i, 001836 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i 001837 ); 001838 break; 001839 } 001840 case SQLITE_BLOB: { 001841 if( pValue->flags & MEM_Zero ){ 001842 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 001843 }else{ 001844 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 001845 } 001846 break; 001847 } 001848 case SQLITE_TEXT: { 001849 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 001850 pValue->enc); 001851 break; 001852 } 001853 default: { 001854 rc = sqlite3_bind_null(pStmt, i); 001855 break; 001856 } 001857 } 001858 return rc; 001859 } 001860 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 001861 int rc; 001862 Vdbe *p = (Vdbe *)pStmt; 001863 rc = vdbeUnbind(p, (u32)(i-1)); 001864 if( rc==SQLITE_OK ){ 001865 assert( p!=0 && p->aVar!=0 && i>0 && i<=p->nVar ); /* tag-20240917-01 */ 001866 #ifndef SQLITE_OMIT_INCRBLOB 001867 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 001868 #else 001869 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 001870 #endif 001871 sqlite3_mutex_leave(p->db->mutex); 001872 } 001873 return rc; 001874 } 001875 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 001876 int rc; 001877 Vdbe *p = (Vdbe *)pStmt; 001878 #ifdef SQLITE_ENABLE_API_ARMOR 001879 if( p==0 ) return SQLITE_MISUSE_BKPT; 001880 #endif 001881 sqlite3_mutex_enter(p->db->mutex); 001882 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001883 rc = SQLITE_TOOBIG; 001884 }else{ 001885 assert( (n & 0x7FFFFFFF)==n ); 001886 rc = sqlite3_bind_zeroblob(pStmt, i, n); 001887 } 001888 rc = sqlite3ApiExit(p->db, rc); 001889 sqlite3_mutex_leave(p->db->mutex); 001890 return rc; 001891 } 001892 001893 /* 001894 ** Return the number of wildcards that can be potentially bound to. 001895 ** This routine is added to support DBD::SQLite. 001896 */ 001897 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 001898 Vdbe *p = (Vdbe*)pStmt; 001899 return p ? p->nVar : 0; 001900 } 001901 001902 /* 001903 ** Return the name of a wildcard parameter. Return NULL if the index 001904 ** is out of range or if the wildcard is unnamed. 001905 ** 001906 ** The result is always UTF-8. 001907 */ 001908 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 001909 Vdbe *p = (Vdbe*)pStmt; 001910 if( p==0 ) return 0; 001911 return sqlite3VListNumToName(p->pVList, i); 001912 } 001913 001914 /* 001915 ** Given a wildcard parameter name, return the index of the variable 001916 ** with that name. If there is no variable with the given name, 001917 ** return 0. 001918 */ 001919 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 001920 if( p==0 || zName==0 ) return 0; 001921 return sqlite3VListNameToNum(p->pVList, zName, nName); 001922 } 001923 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 001924 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 001925 } 001926 001927 /* 001928 ** Transfer all bindings from the first statement over to the second. 001929 */ 001930 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001931 Vdbe *pFrom = (Vdbe*)pFromStmt; 001932 Vdbe *pTo = (Vdbe*)pToStmt; 001933 int i; 001934 assert( pTo->db==pFrom->db ); 001935 assert( pTo->nVar==pFrom->nVar ); 001936 sqlite3_mutex_enter(pTo->db->mutex); 001937 for(i=0; i<pFrom->nVar; i++){ 001938 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 001939 } 001940 sqlite3_mutex_leave(pTo->db->mutex); 001941 return SQLITE_OK; 001942 } 001943 001944 #ifndef SQLITE_OMIT_DEPRECATED 001945 /* 001946 ** Deprecated external interface. Internal/core SQLite code 001947 ** should call sqlite3TransferBindings. 001948 ** 001949 ** It is misuse to call this routine with statements from different 001950 ** database connections. But as this is a deprecated interface, we 001951 ** will not bother to check for that condition. 001952 ** 001953 ** If the two statements contain a different number of bindings, then 001954 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 001955 ** SQLITE_OK is returned. 001956 */ 001957 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001958 Vdbe *pFrom = (Vdbe*)pFromStmt; 001959 Vdbe *pTo = (Vdbe*)pToStmt; 001960 if( pFrom->nVar!=pTo->nVar ){ 001961 return SQLITE_ERROR; 001962 } 001963 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); 001964 if( pTo->expmask ){ 001965 pTo->expired = 1; 001966 } 001967 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); 001968 if( pFrom->expmask ){ 001969 pFrom->expired = 1; 001970 } 001971 return sqlite3TransferBindings(pFromStmt, pToStmt); 001972 } 001973 #endif 001974 001975 /* 001976 ** Return the sqlite3* database handle to which the prepared statement given 001977 ** in the argument belongs. This is the same database handle that was 001978 ** the first argument to the sqlite3_prepare() that was used to create 001979 ** the statement in the first place. 001980 */ 001981 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 001982 return pStmt ? ((Vdbe*)pStmt)->db : 0; 001983 } 001984 001985 /* 001986 ** Return true if the prepared statement is guaranteed to not modify the 001987 ** database. 001988 */ 001989 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 001990 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 001991 } 001992 001993 /* 001994 ** Return 1 if the statement is an EXPLAIN and return 2 if the 001995 ** statement is an EXPLAIN QUERY PLAN 001996 */ 001997 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ 001998 return pStmt ? ((Vdbe*)pStmt)->explain : 0; 001999 } 002000 002001 /* 002002 ** Set the explain mode for a statement. 002003 */ 002004 int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){ 002005 Vdbe *v = (Vdbe*)pStmt; 002006 int rc; 002007 #ifdef SQLITE_ENABLE_API_ARMOR 002008 if( pStmt==0 ) return SQLITE_MISUSE_BKPT; 002009 #endif 002010 sqlite3_mutex_enter(v->db->mutex); 002011 if( ((int)v->explain)==eMode ){ 002012 rc = SQLITE_OK; 002013 }else if( eMode<0 || eMode>2 ){ 002014 rc = SQLITE_ERROR; 002015 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ 002016 rc = SQLITE_ERROR; 002017 }else if( v->eVdbeState!=VDBE_READY_STATE ){ 002018 rc = SQLITE_BUSY; 002019 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ 002020 /* No reprepare necessary */ 002021 v->explain = eMode; 002022 rc = SQLITE_OK; 002023 }else{ 002024 v->explain = eMode; 002025 rc = sqlite3Reprepare(v); 002026 v->haveEqpOps = eMode==2; 002027 } 002028 if( v->explain ){ 002029 v->nResColumn = 12 - 4*v->explain; 002030 }else{ 002031 v->nResColumn = v->nResAlloc; 002032 } 002033 sqlite3_mutex_leave(v->db->mutex); 002034 return rc; 002035 } 002036 002037 /* 002038 ** Return true if the prepared statement is in need of being reset. 002039 */ 002040 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 002041 Vdbe *v = (Vdbe*)pStmt; 002042 return v!=0 && v->eVdbeState==VDBE_RUN_STATE; 002043 } 002044 002045 /* 002046 ** Return a pointer to the next prepared statement after pStmt associated 002047 ** with database connection pDb. If pStmt is NULL, return the first 002048 ** prepared statement for the database connection. Return NULL if there 002049 ** are no more. 002050 */ 002051 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 002052 sqlite3_stmt *pNext; 002053 #ifdef SQLITE_ENABLE_API_ARMOR 002054 if( !sqlite3SafetyCheckOk(pDb) ){ 002055 (void)SQLITE_MISUSE_BKPT; 002056 return 0; 002057 } 002058 #endif 002059 sqlite3_mutex_enter(pDb->mutex); 002060 if( pStmt==0 ){ 002061 pNext = (sqlite3_stmt*)pDb->pVdbe; 002062 }else{ 002063 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; 002064 } 002065 sqlite3_mutex_leave(pDb->mutex); 002066 return pNext; 002067 } 002068 002069 /* 002070 ** Return the value of a status counter for a prepared statement 002071 */ 002072 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 002073 Vdbe *pVdbe = (Vdbe*)pStmt; 002074 u32 v; 002075 #ifdef SQLITE_ENABLE_API_ARMOR 002076 if( !pStmt 002077 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) 002078 ){ 002079 (void)SQLITE_MISUSE_BKPT; 002080 return 0; 002081 } 002082 #endif 002083 if( op==SQLITE_STMTSTATUS_MEMUSED ){ 002084 sqlite3 *db = pVdbe->db; 002085 sqlite3_mutex_enter(db->mutex); 002086 v = 0; 002087 db->pnBytesFreed = (int*)&v; 002088 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); 002089 db->lookaside.pEnd = db->lookaside.pStart; 002090 sqlite3VdbeDelete(pVdbe); 002091 db->pnBytesFreed = 0; 002092 db->lookaside.pEnd = db->lookaside.pTrueEnd; 002093 sqlite3_mutex_leave(db->mutex); 002094 }else{ 002095 v = pVdbe->aCounter[op]; 002096 if( resetFlag ) pVdbe->aCounter[op] = 0; 002097 } 002098 return (int)v; 002099 } 002100 002101 /* 002102 ** Return the SQL associated with a prepared statement 002103 */ 002104 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 002105 Vdbe *p = (Vdbe *)pStmt; 002106 return p ? p->zSql : 0; 002107 } 002108 002109 /* 002110 ** Return the SQL associated with a prepared statement with 002111 ** bound parameters expanded. Space to hold the returned string is 002112 ** obtained from sqlite3_malloc(). The caller is responsible for 002113 ** freeing the returned string by passing it to sqlite3_free(). 002114 ** 002115 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 002116 ** expanded bound parameters. 002117 */ 002118 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 002119 #ifdef SQLITE_OMIT_TRACE 002120 return 0; 002121 #else 002122 char *z = 0; 002123 const char *zSql = sqlite3_sql(pStmt); 002124 if( zSql ){ 002125 Vdbe *p = (Vdbe *)pStmt; 002126 sqlite3_mutex_enter(p->db->mutex); 002127 z = sqlite3VdbeExpandSql(p, zSql); 002128 sqlite3_mutex_leave(p->db->mutex); 002129 } 002130 return z; 002131 #endif 002132 } 002133 002134 #ifdef SQLITE_ENABLE_NORMALIZE 002135 /* 002136 ** Return the normalized SQL associated with a prepared statement. 002137 */ 002138 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ 002139 Vdbe *p = (Vdbe *)pStmt; 002140 if( p==0 ) return 0; 002141 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ 002142 sqlite3_mutex_enter(p->db->mutex); 002143 p->zNormSql = sqlite3Normalize(p, p->zSql); 002144 sqlite3_mutex_leave(p->db->mutex); 002145 } 002146 return p->zNormSql; 002147 } 002148 #endif /* SQLITE_ENABLE_NORMALIZE */ 002149 002150 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002151 /* 002152 ** Allocate and populate an UnpackedRecord structure based on the serialized 002153 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 002154 ** if successful, or a NULL pointer if an OOM error is encountered. 002155 */ 002156 static UnpackedRecord *vdbeUnpackRecord( 002157 KeyInfo *pKeyInfo, 002158 int nKey, 002159 const void *pKey 002160 ){ 002161 UnpackedRecord *pRet; /* Return value */ 002162 002163 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 002164 if( pRet ){ 002165 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); 002166 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 002167 } 002168 return pRet; 002169 } 002170 002171 /* 002172 ** This function is called from within a pre-update callback to retrieve 002173 ** a field of the row currently being updated or deleted. 002174 */ 002175 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 002176 PreUpdate *p; 002177 Mem *pMem; 002178 int rc = SQLITE_OK; 002179 002180 #ifdef SQLITE_ENABLE_API_ARMOR 002181 if( db==0 || ppValue==0 ){ 002182 return SQLITE_MISUSE_BKPT; 002183 } 002184 #endif 002185 p = db->pPreUpdate; 002186 /* Test that this call is being made from within an SQLITE_DELETE or 002187 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 002188 if( !p || p->op==SQLITE_INSERT ){ 002189 rc = SQLITE_MISUSE_BKPT; 002190 goto preupdate_old_out; 002191 } 002192 if( p->pPk ){ 002193 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 002194 } 002195 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 002196 rc = SQLITE_RANGE; 002197 goto preupdate_old_out; 002198 } 002199 002200 /* If the old.* record has not yet been loaded into memory, do so now. */ 002201 if( p->pUnpacked==0 ){ 002202 u32 nRec; 002203 u8 *aRec; 002204 002205 assert( p->pCsr->eCurType==CURTYPE_BTREE ); 002206 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 002207 aRec = sqlite3DbMallocRaw(db, nRec); 002208 if( !aRec ) goto preupdate_old_out; 002209 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 002210 if( rc==SQLITE_OK ){ 002211 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 002212 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 002213 } 002214 if( rc!=SQLITE_OK ){ 002215 sqlite3DbFree(db, aRec); 002216 goto preupdate_old_out; 002217 } 002218 p->aRecord = aRec; 002219 } 002220 002221 pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; 002222 if( iIdx==p->pTab->iPKey ){ 002223 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 002224 }else if( iIdx>=p->pUnpacked->nField ){ 002225 /* This occurs when the table has been extended using ALTER TABLE 002226 ** ADD COLUMN. The value to return is the default value of the column. */ 002227 Column *pCol = &p->pTab->aCol[iIdx]; 002228 if( pCol->iDflt>0 ){ 002229 if( p->apDflt==0 ){ 002230 int nByte = sizeof(sqlite3_value*)*p->pTab->nCol; 002231 p->apDflt = (sqlite3_value**)sqlite3DbMallocZero(db, nByte); 002232 if( p->apDflt==0 ) goto preupdate_old_out; 002233 } 002234 if( p->apDflt[iIdx]==0 ){ 002235 sqlite3_value *pVal = 0; 002236 Expr *pDflt; 002237 assert( p->pTab!=0 && IsOrdinaryTable(p->pTab) ); 002238 pDflt = p->pTab->u.tab.pDfltList->a[pCol->iDflt-1].pExpr; 002239 rc = sqlite3ValueFromExpr(db, pDflt, ENC(db), pCol->affinity, &pVal); 002240 if( rc==SQLITE_OK && pVal==0 ){ 002241 rc = SQLITE_CORRUPT_BKPT; 002242 } 002243 p->apDflt[iIdx] = pVal; 002244 } 002245 *ppValue = p->apDflt[iIdx]; 002246 }else{ 002247 *ppValue = (sqlite3_value *)columnNullValue(); 002248 } 002249 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 002250 if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 002251 testcase( pMem->flags & MEM_Int ); 002252 testcase( pMem->flags & MEM_IntReal ); 002253 sqlite3VdbeMemRealify(pMem); 002254 } 002255 } 002256 002257 preupdate_old_out: 002258 sqlite3Error(db, rc); 002259 return sqlite3ApiExit(db, rc); 002260 } 002261 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002262 002263 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002264 /* 002265 ** This function is called from within a pre-update callback to retrieve 002266 ** the number of columns in the row being updated, deleted or inserted. 002267 */ 002268 int sqlite3_preupdate_count(sqlite3 *db){ 002269 PreUpdate *p; 002270 #ifdef SQLITE_ENABLE_API_ARMOR 002271 p = db!=0 ? db->pPreUpdate : 0; 002272 #else 002273 p = db->pPreUpdate; 002274 #endif 002275 return (p ? p->keyinfo.nKeyField : 0); 002276 } 002277 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002278 002279 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002280 /* 002281 ** This function is designed to be called from within a pre-update callback 002282 ** only. It returns zero if the change that caused the callback was made 002283 ** immediately by a user SQL statement. Or, if the change was made by a 002284 ** trigger program, it returns the number of trigger programs currently 002285 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 002286 ** top-level trigger etc.). 002287 ** 002288 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 002289 ** or SET DEFAULT action is considered a trigger. 002290 */ 002291 int sqlite3_preupdate_depth(sqlite3 *db){ 002292 PreUpdate *p; 002293 #ifdef SQLITE_ENABLE_API_ARMOR 002294 p = db!=0 ? db->pPreUpdate : 0; 002295 #else 002296 p = db->pPreUpdate; 002297 #endif 002298 return (p ? p->v->nFrame : 0); 002299 } 002300 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002301 002302 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002303 /* 002304 ** This function is designed to be called from within a pre-update callback 002305 ** only. 002306 */ 002307 int sqlite3_preupdate_blobwrite(sqlite3 *db){ 002308 PreUpdate *p; 002309 #ifdef SQLITE_ENABLE_API_ARMOR 002310 p = db!=0 ? db->pPreUpdate : 0; 002311 #else 002312 p = db->pPreUpdate; 002313 #endif 002314 return (p ? p->iBlobWrite : -1); 002315 } 002316 #endif 002317 002318 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002319 /* 002320 ** This function is called from within a pre-update callback to retrieve 002321 ** a field of the row currently being updated or inserted. 002322 */ 002323 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 002324 PreUpdate *p; 002325 int rc = SQLITE_OK; 002326 Mem *pMem; 002327 002328 #ifdef SQLITE_ENABLE_API_ARMOR 002329 if( db==0 || ppValue==0 ){ 002330 return SQLITE_MISUSE_BKPT; 002331 } 002332 #endif 002333 p = db->pPreUpdate; 002334 if( !p || p->op==SQLITE_DELETE ){ 002335 rc = SQLITE_MISUSE_BKPT; 002336 goto preupdate_new_out; 002337 } 002338 if( p->pPk && p->op!=SQLITE_UPDATE ){ 002339 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 002340 } 002341 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 002342 rc = SQLITE_RANGE; 002343 goto preupdate_new_out; 002344 } 002345 002346 if( p->op==SQLITE_INSERT ){ 002347 /* For an INSERT, memory cell p->iNewReg contains the serialized record 002348 ** that is being inserted. Deserialize it. */ 002349 UnpackedRecord *pUnpack = p->pNewUnpacked; 002350 if( !pUnpack ){ 002351 Mem *pData = &p->v->aMem[p->iNewReg]; 002352 rc = ExpandBlob(pData); 002353 if( rc!=SQLITE_OK ) goto preupdate_new_out; 002354 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 002355 if( !pUnpack ){ 002356 rc = SQLITE_NOMEM; 002357 goto preupdate_new_out; 002358 } 002359 p->pNewUnpacked = pUnpack; 002360 } 002361 pMem = &pUnpack->aMem[iIdx]; 002362 if( iIdx==p->pTab->iPKey ){ 002363 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 002364 }else if( iIdx>=pUnpack->nField ){ 002365 pMem = (sqlite3_value *)columnNullValue(); 002366 } 002367 }else{ 002368 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 002369 ** value. Make a copy of the cell contents and return a pointer to it. 002370 ** It is not safe to return a pointer to the memory cell itself as the 002371 ** caller may modify the value text encoding. 002372 */ 002373 assert( p->op==SQLITE_UPDATE ); 002374 if( !p->aNew ){ 002375 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 002376 if( !p->aNew ){ 002377 rc = SQLITE_NOMEM; 002378 goto preupdate_new_out; 002379 } 002380 } 002381 assert( iIdx>=0 && iIdx<p->pCsr->nField ); 002382 pMem = &p->aNew[iIdx]; 002383 if( pMem->flags==0 ){ 002384 if( iIdx==p->pTab->iPKey ){ 002385 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 002386 }else{ 002387 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 002388 if( rc!=SQLITE_OK ) goto preupdate_new_out; 002389 } 002390 } 002391 } 002392 *ppValue = pMem; 002393 002394 preupdate_new_out: 002395 sqlite3Error(db, rc); 002396 return sqlite3ApiExit(db, rc); 002397 } 002398 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002399 002400 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002401 /* 002402 ** Return status data for a single loop within query pStmt. 002403 */ 002404 int sqlite3_stmt_scanstatus_v2( 002405 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 002406 int iScan, /* Index of loop to report on */ 002407 int iScanStatusOp, /* Which metric to return */ 002408 int flags, 002409 void *pOut /* OUT: Write the answer here */ 002410 ){ 002411 Vdbe *p = (Vdbe*)pStmt; 002412 VdbeOp *aOp; 002413 int nOp; 002414 ScanStatus *pScan = 0; 002415 int idx; 002416 002417 #ifdef SQLITE_ENABLE_API_ARMOR 002418 if( p==0 || pOut==0 002419 || iScanStatusOp<SQLITE_SCANSTAT_NLOOP 002420 || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE ){ 002421 return 1; 002422 } 002423 #endif 002424 aOp = p->aOp; 002425 nOp = p->nOp; 002426 if( p->pFrame ){ 002427 VdbeFrame *pFrame; 002428 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 002429 aOp = pFrame->aOp; 002430 nOp = pFrame->nOp; 002431 } 002432 002433 if( iScan<0 ){ 002434 int ii; 002435 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ 002436 i64 res = 0; 002437 for(ii=0; ii<nOp; ii++){ 002438 res += aOp[ii].nCycle; 002439 } 002440 *(i64*)pOut = res; 002441 return 0; 002442 } 002443 return 1; 002444 } 002445 if( flags & SQLITE_SCANSTAT_COMPLEX ){ 002446 idx = iScan; 002447 }else{ 002448 /* If the COMPLEX flag is clear, then this function must ignore any 002449 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */ 002450 for(idx=0; idx<p->nScan; idx++){ 002451 pScan = &p->aScan[idx]; 002452 if( pScan->zName ){ 002453 iScan--; 002454 if( iScan<0 ) break; 002455 } 002456 } 002457 } 002458 if( idx>=p->nScan ) return 1; 002459 assert( pScan==0 || pScan==&p->aScan[idx] ); 002460 pScan = &p->aScan[idx]; 002461 002462 switch( iScanStatusOp ){ 002463 case SQLITE_SCANSTAT_NLOOP: { 002464 if( pScan->addrLoop>0 ){ 002465 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; 002466 }else{ 002467 *(sqlite3_int64*)pOut = -1; 002468 } 002469 break; 002470 } 002471 case SQLITE_SCANSTAT_NVISIT: { 002472 if( pScan->addrVisit>0 ){ 002473 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; 002474 }else{ 002475 *(sqlite3_int64*)pOut = -1; 002476 } 002477 break; 002478 } 002479 case SQLITE_SCANSTAT_EST: { 002480 double r = 1.0; 002481 LogEst x = pScan->nEst; 002482 while( x<100 ){ 002483 x += 10; 002484 r *= 0.5; 002485 } 002486 *(double*)pOut = r*sqlite3LogEstToInt(x); 002487 break; 002488 } 002489 case SQLITE_SCANSTAT_NAME: { 002490 *(const char**)pOut = pScan->zName; 002491 break; 002492 } 002493 case SQLITE_SCANSTAT_EXPLAIN: { 002494 if( pScan->addrExplain ){ 002495 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; 002496 }else{ 002497 *(const char**)pOut = 0; 002498 } 002499 break; 002500 } 002501 case SQLITE_SCANSTAT_SELECTID: { 002502 if( pScan->addrExplain ){ 002503 *(int*)pOut = aOp[ pScan->addrExplain ].p1; 002504 }else{ 002505 *(int*)pOut = -1; 002506 } 002507 break; 002508 } 002509 case SQLITE_SCANSTAT_PARENTID: { 002510 if( pScan->addrExplain ){ 002511 *(int*)pOut = aOp[ pScan->addrExplain ].p2; 002512 }else{ 002513 *(int*)pOut = -1; 002514 } 002515 break; 002516 } 002517 case SQLITE_SCANSTAT_NCYCLE: { 002518 i64 res = 0; 002519 if( pScan->aAddrRange[0]==0 ){ 002520 res = -1; 002521 }else{ 002522 int ii; 002523 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ 002524 int iIns = pScan->aAddrRange[ii]; 002525 int iEnd = pScan->aAddrRange[ii+1]; 002526 if( iIns==0 ) break; 002527 if( iIns>0 ){ 002528 while( iIns<=iEnd ){ 002529 res += aOp[iIns].nCycle; 002530 iIns++; 002531 } 002532 }else{ 002533 int iOp; 002534 for(iOp=0; iOp<nOp; iOp++){ 002535 Op *pOp = &aOp[iOp]; 002536 if( pOp->p1!=iEnd ) continue; 002537 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ 002538 continue; 002539 } 002540 res += aOp[iOp].nCycle; 002541 } 002542 } 002543 } 002544 } 002545 *(i64*)pOut = res; 002546 break; 002547 } 002548 default: { 002549 return 1; 002550 } 002551 } 002552 return 0; 002553 } 002554 002555 /* 002556 ** Return status data for a single loop within query pStmt. 002557 */ 002558 int sqlite3_stmt_scanstatus( 002559 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 002560 int iScan, /* Index of loop to report on */ 002561 int iScanStatusOp, /* Which metric to return */ 002562 void *pOut /* OUT: Write the answer here */ 002563 ){ 002564 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut); 002565 } 002566 002567 /* 002568 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 002569 */ 002570 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 002571 Vdbe *p = (Vdbe*)pStmt; 002572 int ii; 002573 for(ii=0; p!=0 && ii<p->nOp; ii++){ 002574 Op *pOp = &p->aOp[ii]; 002575 pOp->nExec = 0; 002576 pOp->nCycle = 0; 002577 } 002578 } 002579 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */