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 static int vdbeUnbind(Vdbe *p, unsigned int i){ 001626 Mem *pVar; 001627 if( vdbeSafetyNotNull(p) ){ 001628 return SQLITE_MISUSE_BKPT; 001629 } 001630 sqlite3_mutex_enter(p->db->mutex); 001631 if( p->eVdbeState!=VDBE_READY_STATE ){ 001632 sqlite3Error(p->db, SQLITE_MISUSE_BKPT); 001633 sqlite3_mutex_leave(p->db->mutex); 001634 sqlite3_log(SQLITE_MISUSE, 001635 "bind on a busy prepared statement: [%s]", p->zSql); 001636 return SQLITE_MISUSE_BKPT; 001637 } 001638 if( i>=(unsigned int)p->nVar ){ 001639 sqlite3Error(p->db, SQLITE_RANGE); 001640 sqlite3_mutex_leave(p->db->mutex); 001641 return SQLITE_RANGE; 001642 } 001643 pVar = &p->aVar[i]; 001644 sqlite3VdbeMemRelease(pVar); 001645 pVar->flags = MEM_Null; 001646 p->db->errCode = SQLITE_OK; 001647 001648 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 001649 ** binding a new value to this variable invalidates the current query plan. 001650 ** 001651 ** IMPLEMENTATION-OF: R-57496-20354 If the specific value bound to a host 001652 ** parameter in the WHERE clause might influence the choice of query plan 001653 ** for a statement, then the statement will be automatically recompiled, 001654 ** as if there had been a schema change, on the first sqlite3_step() call 001655 ** following any change to the bindings of that parameter. 001656 */ 001657 assert( (p->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || p->expmask==0 ); 001658 if( p->expmask!=0 && (p->expmask & (i>=31 ? 0x80000000 : (u32)1<<i))!=0 ){ 001659 p->expired = 1; 001660 } 001661 return SQLITE_OK; 001662 } 001663 001664 /* 001665 ** Bind a text or BLOB value. 001666 */ 001667 static int bindText( 001668 sqlite3_stmt *pStmt, /* The statement to bind against */ 001669 int i, /* Index of the parameter to bind */ 001670 const void *zData, /* Pointer to the data to be bound */ 001671 i64 nData, /* Number of bytes of data to be bound */ 001672 void (*xDel)(void*), /* Destructor for the data */ 001673 u8 encoding /* Encoding for the data */ 001674 ){ 001675 Vdbe *p = (Vdbe *)pStmt; 001676 Mem *pVar; 001677 int rc; 001678 001679 rc = vdbeUnbind(p, (u32)(i-1)); 001680 if( rc==SQLITE_OK ){ 001681 if( zData!=0 ){ 001682 pVar = &p->aVar[i-1]; 001683 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 001684 if( rc==SQLITE_OK && encoding!=0 ){ 001685 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 001686 } 001687 if( rc ){ 001688 sqlite3Error(p->db, rc); 001689 rc = sqlite3ApiExit(p->db, rc); 001690 } 001691 } 001692 sqlite3_mutex_leave(p->db->mutex); 001693 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 001694 xDel((void*)zData); 001695 } 001696 return rc; 001697 } 001698 001699 001700 /* 001701 ** Bind a blob value to an SQL statement variable. 001702 */ 001703 int sqlite3_bind_blob( 001704 sqlite3_stmt *pStmt, 001705 int i, 001706 const void *zData, 001707 int nData, 001708 void (*xDel)(void*) 001709 ){ 001710 #ifdef SQLITE_ENABLE_API_ARMOR 001711 if( nData<0 ) return SQLITE_MISUSE_BKPT; 001712 #endif 001713 return bindText(pStmt, i, zData, nData, xDel, 0); 001714 } 001715 int sqlite3_bind_blob64( 001716 sqlite3_stmt *pStmt, 001717 int i, 001718 const void *zData, 001719 sqlite3_uint64 nData, 001720 void (*xDel)(void*) 001721 ){ 001722 assert( xDel!=SQLITE_DYNAMIC ); 001723 return bindText(pStmt, i, zData, nData, xDel, 0); 001724 } 001725 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 001726 int rc; 001727 Vdbe *p = (Vdbe *)pStmt; 001728 rc = vdbeUnbind(p, (u32)(i-1)); 001729 if( rc==SQLITE_OK ){ 001730 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 001731 sqlite3_mutex_leave(p->db->mutex); 001732 } 001733 return rc; 001734 } 001735 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 001736 return sqlite3_bind_int64(p, i, (i64)iValue); 001737 } 001738 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 001739 int rc; 001740 Vdbe *p = (Vdbe *)pStmt; 001741 rc = vdbeUnbind(p, (u32)(i-1)); 001742 if( rc==SQLITE_OK ){ 001743 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 001744 sqlite3_mutex_leave(p->db->mutex); 001745 } 001746 return rc; 001747 } 001748 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 001749 int rc; 001750 Vdbe *p = (Vdbe*)pStmt; 001751 rc = vdbeUnbind(p, (u32)(i-1)); 001752 if( rc==SQLITE_OK ){ 001753 sqlite3_mutex_leave(p->db->mutex); 001754 } 001755 return rc; 001756 } 001757 int sqlite3_bind_pointer( 001758 sqlite3_stmt *pStmt, 001759 int i, 001760 void *pPtr, 001761 const char *zPTtype, 001762 void (*xDestructor)(void*) 001763 ){ 001764 int rc; 001765 Vdbe *p = (Vdbe*)pStmt; 001766 rc = vdbeUnbind(p, (u32)(i-1)); 001767 if( rc==SQLITE_OK ){ 001768 sqlite3VdbeMemSetPointer(&p->aVar[i-1], pPtr, zPTtype, xDestructor); 001769 sqlite3_mutex_leave(p->db->mutex); 001770 }else if( xDestructor ){ 001771 xDestructor(pPtr); 001772 } 001773 return rc; 001774 } 001775 int sqlite3_bind_text( 001776 sqlite3_stmt *pStmt, 001777 int i, 001778 const char *zData, 001779 int nData, 001780 void (*xDel)(void*) 001781 ){ 001782 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 001783 } 001784 int sqlite3_bind_text64( 001785 sqlite3_stmt *pStmt, 001786 int i, 001787 const char *zData, 001788 sqlite3_uint64 nData, 001789 void (*xDel)(void*), 001790 unsigned char enc 001791 ){ 001792 assert( xDel!=SQLITE_DYNAMIC ); 001793 if( enc!=SQLITE_UTF8 ){ 001794 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 001795 nData &= ~(u16)1; 001796 } 001797 return bindText(pStmt, i, zData, nData, xDel, enc); 001798 } 001799 #ifndef SQLITE_OMIT_UTF16 001800 int sqlite3_bind_text16( 001801 sqlite3_stmt *pStmt, 001802 int i, 001803 const void *zData, 001804 int n, 001805 void (*xDel)(void*) 001806 ){ 001807 return bindText(pStmt, i, zData, n & ~(u64)1, xDel, SQLITE_UTF16NATIVE); 001808 } 001809 #endif /* SQLITE_OMIT_UTF16 */ 001810 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 001811 int rc; 001812 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 001813 case SQLITE_INTEGER: { 001814 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 001815 break; 001816 } 001817 case SQLITE_FLOAT: { 001818 assert( pValue->flags & (MEM_Real|MEM_IntReal) ); 001819 rc = sqlite3_bind_double(pStmt, i, 001820 (pValue->flags & MEM_Real) ? pValue->u.r : (double)pValue->u.i 001821 ); 001822 break; 001823 } 001824 case SQLITE_BLOB: { 001825 if( pValue->flags & MEM_Zero ){ 001826 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 001827 }else{ 001828 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 001829 } 001830 break; 001831 } 001832 case SQLITE_TEXT: { 001833 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 001834 pValue->enc); 001835 break; 001836 } 001837 default: { 001838 rc = sqlite3_bind_null(pStmt, i); 001839 break; 001840 } 001841 } 001842 return rc; 001843 } 001844 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 001845 int rc; 001846 Vdbe *p = (Vdbe *)pStmt; 001847 rc = vdbeUnbind(p, (u32)(i-1)); 001848 if( rc==SQLITE_OK ){ 001849 #ifndef SQLITE_OMIT_INCRBLOB 001850 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 001851 #else 001852 rc = sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 001853 #endif 001854 sqlite3_mutex_leave(p->db->mutex); 001855 } 001856 return rc; 001857 } 001858 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 001859 int rc; 001860 Vdbe *p = (Vdbe *)pStmt; 001861 #ifdef SQLITE_ENABLE_API_ARMOR 001862 if( p==0 ) return SQLITE_MISUSE_BKPT; 001863 #endif 001864 sqlite3_mutex_enter(p->db->mutex); 001865 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001866 rc = SQLITE_TOOBIG; 001867 }else{ 001868 assert( (n & 0x7FFFFFFF)==n ); 001869 rc = sqlite3_bind_zeroblob(pStmt, i, n); 001870 } 001871 rc = sqlite3ApiExit(p->db, rc); 001872 sqlite3_mutex_leave(p->db->mutex); 001873 return rc; 001874 } 001875 001876 /* 001877 ** Return the number of wildcards that can be potentially bound to. 001878 ** This routine is added to support DBD::SQLite. 001879 */ 001880 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 001881 Vdbe *p = (Vdbe*)pStmt; 001882 return p ? p->nVar : 0; 001883 } 001884 001885 /* 001886 ** Return the name of a wildcard parameter. Return NULL if the index 001887 ** is out of range or if the wildcard is unnamed. 001888 ** 001889 ** The result is always UTF-8. 001890 */ 001891 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 001892 Vdbe *p = (Vdbe*)pStmt; 001893 if( p==0 ) return 0; 001894 return sqlite3VListNumToName(p->pVList, i); 001895 } 001896 001897 /* 001898 ** Given a wildcard parameter name, return the index of the variable 001899 ** with that name. If there is no variable with the given name, 001900 ** return 0. 001901 */ 001902 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 001903 if( p==0 || zName==0 ) return 0; 001904 return sqlite3VListNameToNum(p->pVList, zName, nName); 001905 } 001906 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 001907 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 001908 } 001909 001910 /* 001911 ** Transfer all bindings from the first statement over to the second. 001912 */ 001913 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001914 Vdbe *pFrom = (Vdbe*)pFromStmt; 001915 Vdbe *pTo = (Vdbe*)pToStmt; 001916 int i; 001917 assert( pTo->db==pFrom->db ); 001918 assert( pTo->nVar==pFrom->nVar ); 001919 sqlite3_mutex_enter(pTo->db->mutex); 001920 for(i=0; i<pFrom->nVar; i++){ 001921 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 001922 } 001923 sqlite3_mutex_leave(pTo->db->mutex); 001924 return SQLITE_OK; 001925 } 001926 001927 #ifndef SQLITE_OMIT_DEPRECATED 001928 /* 001929 ** Deprecated external interface. Internal/core SQLite code 001930 ** should call sqlite3TransferBindings. 001931 ** 001932 ** It is misuse to call this routine with statements from different 001933 ** database connections. But as this is a deprecated interface, we 001934 ** will not bother to check for that condition. 001935 ** 001936 ** If the two statements contain a different number of bindings, then 001937 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 001938 ** SQLITE_OK is returned. 001939 */ 001940 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001941 Vdbe *pFrom = (Vdbe*)pFromStmt; 001942 Vdbe *pTo = (Vdbe*)pToStmt; 001943 if( pFrom->nVar!=pTo->nVar ){ 001944 return SQLITE_ERROR; 001945 } 001946 assert( (pTo->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pTo->expmask==0 ); 001947 if( pTo->expmask ){ 001948 pTo->expired = 1; 001949 } 001950 assert( (pFrom->prepFlags & SQLITE_PREPARE_SAVESQL)!=0 || pFrom->expmask==0 ); 001951 if( pFrom->expmask ){ 001952 pFrom->expired = 1; 001953 } 001954 return sqlite3TransferBindings(pFromStmt, pToStmt); 001955 } 001956 #endif 001957 001958 /* 001959 ** Return the sqlite3* database handle to which the prepared statement given 001960 ** in the argument belongs. This is the same database handle that was 001961 ** the first argument to the sqlite3_prepare() that was used to create 001962 ** the statement in the first place. 001963 */ 001964 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 001965 return pStmt ? ((Vdbe*)pStmt)->db : 0; 001966 } 001967 001968 /* 001969 ** Return true if the prepared statement is guaranteed to not modify the 001970 ** database. 001971 */ 001972 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 001973 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 001974 } 001975 001976 /* 001977 ** Return 1 if the statement is an EXPLAIN and return 2 if the 001978 ** statement is an EXPLAIN QUERY PLAN 001979 */ 001980 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt){ 001981 return pStmt ? ((Vdbe*)pStmt)->explain : 0; 001982 } 001983 001984 /* 001985 ** Set the explain mode for a statement. 001986 */ 001987 int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode){ 001988 Vdbe *v = (Vdbe*)pStmt; 001989 int rc; 001990 #ifdef SQLITE_ENABLE_API_ARMOR 001991 if( pStmt==0 ) return SQLITE_MISUSE_BKPT; 001992 #endif 001993 sqlite3_mutex_enter(v->db->mutex); 001994 if( ((int)v->explain)==eMode ){ 001995 rc = SQLITE_OK; 001996 }else if( eMode<0 || eMode>2 ){ 001997 rc = SQLITE_ERROR; 001998 }else if( (v->prepFlags & SQLITE_PREPARE_SAVESQL)==0 ){ 001999 rc = SQLITE_ERROR; 002000 }else if( v->eVdbeState!=VDBE_READY_STATE ){ 002001 rc = SQLITE_BUSY; 002002 }else if( v->nMem>=10 && (eMode!=2 || v->haveEqpOps) ){ 002003 /* No reprepare necessary */ 002004 v->explain = eMode; 002005 rc = SQLITE_OK; 002006 }else{ 002007 v->explain = eMode; 002008 rc = sqlite3Reprepare(v); 002009 v->haveEqpOps = eMode==2; 002010 } 002011 if( v->explain ){ 002012 v->nResColumn = 12 - 4*v->explain; 002013 }else{ 002014 v->nResColumn = v->nResAlloc; 002015 } 002016 sqlite3_mutex_leave(v->db->mutex); 002017 return rc; 002018 } 002019 002020 /* 002021 ** Return true if the prepared statement is in need of being reset. 002022 */ 002023 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 002024 Vdbe *v = (Vdbe*)pStmt; 002025 return v!=0 && v->eVdbeState==VDBE_RUN_STATE; 002026 } 002027 002028 /* 002029 ** Return a pointer to the next prepared statement after pStmt associated 002030 ** with database connection pDb. If pStmt is NULL, return the first 002031 ** prepared statement for the database connection. Return NULL if there 002032 ** are no more. 002033 */ 002034 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 002035 sqlite3_stmt *pNext; 002036 #ifdef SQLITE_ENABLE_API_ARMOR 002037 if( !sqlite3SafetyCheckOk(pDb) ){ 002038 (void)SQLITE_MISUSE_BKPT; 002039 return 0; 002040 } 002041 #endif 002042 sqlite3_mutex_enter(pDb->mutex); 002043 if( pStmt==0 ){ 002044 pNext = (sqlite3_stmt*)pDb->pVdbe; 002045 }else{ 002046 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pVNext; 002047 } 002048 sqlite3_mutex_leave(pDb->mutex); 002049 return pNext; 002050 } 002051 002052 /* 002053 ** Return the value of a status counter for a prepared statement 002054 */ 002055 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 002056 Vdbe *pVdbe = (Vdbe*)pStmt; 002057 u32 v; 002058 #ifdef SQLITE_ENABLE_API_ARMOR 002059 if( !pStmt 002060 || (op!=SQLITE_STMTSTATUS_MEMUSED && (op<0||op>=ArraySize(pVdbe->aCounter))) 002061 ){ 002062 (void)SQLITE_MISUSE_BKPT; 002063 return 0; 002064 } 002065 #endif 002066 if( op==SQLITE_STMTSTATUS_MEMUSED ){ 002067 sqlite3 *db = pVdbe->db; 002068 sqlite3_mutex_enter(db->mutex); 002069 v = 0; 002070 db->pnBytesFreed = (int*)&v; 002071 assert( db->lookaside.pEnd==db->lookaside.pTrueEnd ); 002072 db->lookaside.pEnd = db->lookaside.pStart; 002073 sqlite3VdbeDelete(pVdbe); 002074 db->pnBytesFreed = 0; 002075 db->lookaside.pEnd = db->lookaside.pTrueEnd; 002076 sqlite3_mutex_leave(db->mutex); 002077 }else{ 002078 v = pVdbe->aCounter[op]; 002079 if( resetFlag ) pVdbe->aCounter[op] = 0; 002080 } 002081 return (int)v; 002082 } 002083 002084 /* 002085 ** Return the SQL associated with a prepared statement 002086 */ 002087 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 002088 Vdbe *p = (Vdbe *)pStmt; 002089 return p ? p->zSql : 0; 002090 } 002091 002092 /* 002093 ** Return the SQL associated with a prepared statement with 002094 ** bound parameters expanded. Space to hold the returned string is 002095 ** obtained from sqlite3_malloc(). The caller is responsible for 002096 ** freeing the returned string by passing it to sqlite3_free(). 002097 ** 002098 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 002099 ** expanded bound parameters. 002100 */ 002101 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 002102 #ifdef SQLITE_OMIT_TRACE 002103 return 0; 002104 #else 002105 char *z = 0; 002106 const char *zSql = sqlite3_sql(pStmt); 002107 if( zSql ){ 002108 Vdbe *p = (Vdbe *)pStmt; 002109 sqlite3_mutex_enter(p->db->mutex); 002110 z = sqlite3VdbeExpandSql(p, zSql); 002111 sqlite3_mutex_leave(p->db->mutex); 002112 } 002113 return z; 002114 #endif 002115 } 002116 002117 #ifdef SQLITE_ENABLE_NORMALIZE 002118 /* 002119 ** Return the normalized SQL associated with a prepared statement. 002120 */ 002121 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt){ 002122 Vdbe *p = (Vdbe *)pStmt; 002123 if( p==0 ) return 0; 002124 if( p->zNormSql==0 && ALWAYS(p->zSql!=0) ){ 002125 sqlite3_mutex_enter(p->db->mutex); 002126 p->zNormSql = sqlite3Normalize(p, p->zSql); 002127 sqlite3_mutex_leave(p->db->mutex); 002128 } 002129 return p->zNormSql; 002130 } 002131 #endif /* SQLITE_ENABLE_NORMALIZE */ 002132 002133 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002134 /* 002135 ** Allocate and populate an UnpackedRecord structure based on the serialized 002136 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 002137 ** if successful, or a NULL pointer if an OOM error is encountered. 002138 */ 002139 static UnpackedRecord *vdbeUnpackRecord( 002140 KeyInfo *pKeyInfo, 002141 int nKey, 002142 const void *pKey 002143 ){ 002144 UnpackedRecord *pRet; /* Return value */ 002145 002146 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 002147 if( pRet ){ 002148 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nKeyField+1)); 002149 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 002150 } 002151 return pRet; 002152 } 002153 002154 /* 002155 ** This function is called from within a pre-update callback to retrieve 002156 ** a field of the row currently being updated or deleted. 002157 */ 002158 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 002159 PreUpdate *p; 002160 Mem *pMem; 002161 int rc = SQLITE_OK; 002162 002163 #ifdef SQLITE_ENABLE_API_ARMOR 002164 if( db==0 || ppValue==0 ){ 002165 return SQLITE_MISUSE_BKPT; 002166 } 002167 #endif 002168 p = db->pPreUpdate; 002169 /* Test that this call is being made from within an SQLITE_DELETE or 002170 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 002171 if( !p || p->op==SQLITE_INSERT ){ 002172 rc = SQLITE_MISUSE_BKPT; 002173 goto preupdate_old_out; 002174 } 002175 if( p->pPk ){ 002176 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 002177 } 002178 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 002179 rc = SQLITE_RANGE; 002180 goto preupdate_old_out; 002181 } 002182 002183 /* If the old.* record has not yet been loaded into memory, do so now. */ 002184 if( p->pUnpacked==0 ){ 002185 u32 nRec; 002186 u8 *aRec; 002187 002188 assert( p->pCsr->eCurType==CURTYPE_BTREE ); 002189 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 002190 aRec = sqlite3DbMallocRaw(db, nRec); 002191 if( !aRec ) goto preupdate_old_out; 002192 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 002193 if( rc==SQLITE_OK ){ 002194 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 002195 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 002196 } 002197 if( rc!=SQLITE_OK ){ 002198 sqlite3DbFree(db, aRec); 002199 goto preupdate_old_out; 002200 } 002201 p->aRecord = aRec; 002202 } 002203 002204 pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; 002205 if( iIdx==p->pTab->iPKey ){ 002206 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 002207 }else if( iIdx>=p->pUnpacked->nField ){ 002208 *ppValue = (sqlite3_value *)columnNullValue(); 002209 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 002210 if( pMem->flags & (MEM_Int|MEM_IntReal) ){ 002211 testcase( pMem->flags & MEM_Int ); 002212 testcase( pMem->flags & MEM_IntReal ); 002213 sqlite3VdbeMemRealify(pMem); 002214 } 002215 } 002216 002217 preupdate_old_out: 002218 sqlite3Error(db, rc); 002219 return sqlite3ApiExit(db, rc); 002220 } 002221 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002222 002223 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002224 /* 002225 ** This function is called from within a pre-update callback to retrieve 002226 ** the number of columns in the row being updated, deleted or inserted. 002227 */ 002228 int sqlite3_preupdate_count(sqlite3 *db){ 002229 PreUpdate *p; 002230 #ifdef SQLITE_ENABLE_API_ARMOR 002231 p = db!=0 ? db->pPreUpdate : 0; 002232 #else 002233 p = db->pPreUpdate; 002234 #endif 002235 return (p ? p->keyinfo.nKeyField : 0); 002236 } 002237 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002238 002239 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002240 /* 002241 ** This function is designed to be called from within a pre-update callback 002242 ** only. It returns zero if the change that caused the callback was made 002243 ** immediately by a user SQL statement. Or, if the change was made by a 002244 ** trigger program, it returns the number of trigger programs currently 002245 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 002246 ** top-level trigger etc.). 002247 ** 002248 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 002249 ** or SET DEFAULT action is considered a trigger. 002250 */ 002251 int sqlite3_preupdate_depth(sqlite3 *db){ 002252 PreUpdate *p; 002253 #ifdef SQLITE_ENABLE_API_ARMOR 002254 p = db!=0 ? db->pPreUpdate : 0; 002255 #else 002256 p = db->pPreUpdate; 002257 #endif 002258 return (p ? p->v->nFrame : 0); 002259 } 002260 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002261 002262 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002263 /* 002264 ** This function is designed to be called from within a pre-update callback 002265 ** only. 002266 */ 002267 int sqlite3_preupdate_blobwrite(sqlite3 *db){ 002268 PreUpdate *p; 002269 #ifdef SQLITE_ENABLE_API_ARMOR 002270 p = db!=0 ? db->pPreUpdate : 0; 002271 #else 002272 p = db->pPreUpdate; 002273 #endif 002274 return (p ? p->iBlobWrite : -1); 002275 } 002276 #endif 002277 002278 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 002279 /* 002280 ** This function is called from within a pre-update callback to retrieve 002281 ** a field of the row currently being updated or inserted. 002282 */ 002283 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 002284 PreUpdate *p; 002285 int rc = SQLITE_OK; 002286 Mem *pMem; 002287 002288 #ifdef SQLITE_ENABLE_API_ARMOR 002289 if( db==0 || ppValue==0 ){ 002290 return SQLITE_MISUSE_BKPT; 002291 } 002292 #endif 002293 p = db->pPreUpdate; 002294 if( !p || p->op==SQLITE_DELETE ){ 002295 rc = SQLITE_MISUSE_BKPT; 002296 goto preupdate_new_out; 002297 } 002298 if( p->pPk && p->op!=SQLITE_UPDATE ){ 002299 iIdx = sqlite3TableColumnToIndex(p->pPk, iIdx); 002300 } 002301 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 002302 rc = SQLITE_RANGE; 002303 goto preupdate_new_out; 002304 } 002305 002306 if( p->op==SQLITE_INSERT ){ 002307 /* For an INSERT, memory cell p->iNewReg contains the serialized record 002308 ** that is being inserted. Deserialize it. */ 002309 UnpackedRecord *pUnpack = p->pNewUnpacked; 002310 if( !pUnpack ){ 002311 Mem *pData = &p->v->aMem[p->iNewReg]; 002312 rc = ExpandBlob(pData); 002313 if( rc!=SQLITE_OK ) goto preupdate_new_out; 002314 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 002315 if( !pUnpack ){ 002316 rc = SQLITE_NOMEM; 002317 goto preupdate_new_out; 002318 } 002319 p->pNewUnpacked = pUnpack; 002320 } 002321 pMem = &pUnpack->aMem[iIdx]; 002322 if( iIdx==p->pTab->iPKey ){ 002323 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 002324 }else if( iIdx>=pUnpack->nField ){ 002325 pMem = (sqlite3_value *)columnNullValue(); 002326 } 002327 }else{ 002328 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 002329 ** value. Make a copy of the cell contents and return a pointer to it. 002330 ** It is not safe to return a pointer to the memory cell itself as the 002331 ** caller may modify the value text encoding. 002332 */ 002333 assert( p->op==SQLITE_UPDATE ); 002334 if( !p->aNew ){ 002335 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 002336 if( !p->aNew ){ 002337 rc = SQLITE_NOMEM; 002338 goto preupdate_new_out; 002339 } 002340 } 002341 assert( iIdx>=0 && iIdx<p->pCsr->nField ); 002342 pMem = &p->aNew[iIdx]; 002343 if( pMem->flags==0 ){ 002344 if( iIdx==p->pTab->iPKey ){ 002345 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 002346 }else{ 002347 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 002348 if( rc!=SQLITE_OK ) goto preupdate_new_out; 002349 } 002350 } 002351 } 002352 *ppValue = pMem; 002353 002354 preupdate_new_out: 002355 sqlite3Error(db, rc); 002356 return sqlite3ApiExit(db, rc); 002357 } 002358 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 002359 002360 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 002361 /* 002362 ** Return status data for a single loop within query pStmt. 002363 */ 002364 int sqlite3_stmt_scanstatus_v2( 002365 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 002366 int iScan, /* Index of loop to report on */ 002367 int iScanStatusOp, /* Which metric to return */ 002368 int flags, 002369 void *pOut /* OUT: Write the answer here */ 002370 ){ 002371 Vdbe *p = (Vdbe*)pStmt; 002372 VdbeOp *aOp; 002373 int nOp; 002374 ScanStatus *pScan = 0; 002375 int idx; 002376 002377 #ifdef SQLITE_ENABLE_API_ARMOR 002378 if( p==0 || pOut==0 002379 || iScanStatusOp<SQLITE_SCANSTAT_NLOOP 002380 || iScanStatusOp>SQLITE_SCANSTAT_NCYCLE ){ 002381 return 1; 002382 } 002383 #endif 002384 aOp = p->aOp; 002385 nOp = p->nOp; 002386 if( p->pFrame ){ 002387 VdbeFrame *pFrame; 002388 for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent); 002389 aOp = pFrame->aOp; 002390 nOp = pFrame->nOp; 002391 } 002392 002393 if( iScan<0 ){ 002394 int ii; 002395 if( iScanStatusOp==SQLITE_SCANSTAT_NCYCLE ){ 002396 i64 res = 0; 002397 for(ii=0; ii<nOp; ii++){ 002398 res += aOp[ii].nCycle; 002399 } 002400 *(i64*)pOut = res; 002401 return 0; 002402 } 002403 return 1; 002404 } 002405 if( flags & SQLITE_SCANSTAT_COMPLEX ){ 002406 idx = iScan; 002407 }else{ 002408 /* If the COMPLEX flag is clear, then this function must ignore any 002409 ** ScanStatus structures with ScanStatus.addrLoop set to 0. */ 002410 for(idx=0; idx<p->nScan; idx++){ 002411 pScan = &p->aScan[idx]; 002412 if( pScan->zName ){ 002413 iScan--; 002414 if( iScan<0 ) break; 002415 } 002416 } 002417 } 002418 if( idx>=p->nScan ) return 1; 002419 assert( pScan==0 || pScan==&p->aScan[idx] ); 002420 pScan = &p->aScan[idx]; 002421 002422 switch( iScanStatusOp ){ 002423 case SQLITE_SCANSTAT_NLOOP: { 002424 if( pScan->addrLoop>0 ){ 002425 *(sqlite3_int64*)pOut = aOp[pScan->addrLoop].nExec; 002426 }else{ 002427 *(sqlite3_int64*)pOut = -1; 002428 } 002429 break; 002430 } 002431 case SQLITE_SCANSTAT_NVISIT: { 002432 if( pScan->addrVisit>0 ){ 002433 *(sqlite3_int64*)pOut = aOp[pScan->addrVisit].nExec; 002434 }else{ 002435 *(sqlite3_int64*)pOut = -1; 002436 } 002437 break; 002438 } 002439 case SQLITE_SCANSTAT_EST: { 002440 double r = 1.0; 002441 LogEst x = pScan->nEst; 002442 while( x<100 ){ 002443 x += 10; 002444 r *= 0.5; 002445 } 002446 *(double*)pOut = r*sqlite3LogEstToInt(x); 002447 break; 002448 } 002449 case SQLITE_SCANSTAT_NAME: { 002450 *(const char**)pOut = pScan->zName; 002451 break; 002452 } 002453 case SQLITE_SCANSTAT_EXPLAIN: { 002454 if( pScan->addrExplain ){ 002455 *(const char**)pOut = aOp[ pScan->addrExplain ].p4.z; 002456 }else{ 002457 *(const char**)pOut = 0; 002458 } 002459 break; 002460 } 002461 case SQLITE_SCANSTAT_SELECTID: { 002462 if( pScan->addrExplain ){ 002463 *(int*)pOut = aOp[ pScan->addrExplain ].p1; 002464 }else{ 002465 *(int*)pOut = -1; 002466 } 002467 break; 002468 } 002469 case SQLITE_SCANSTAT_PARENTID: { 002470 if( pScan->addrExplain ){ 002471 *(int*)pOut = aOp[ pScan->addrExplain ].p2; 002472 }else{ 002473 *(int*)pOut = -1; 002474 } 002475 break; 002476 } 002477 case SQLITE_SCANSTAT_NCYCLE: { 002478 i64 res = 0; 002479 if( pScan->aAddrRange[0]==0 ){ 002480 res = -1; 002481 }else{ 002482 int ii; 002483 for(ii=0; ii<ArraySize(pScan->aAddrRange); ii+=2){ 002484 int iIns = pScan->aAddrRange[ii]; 002485 int iEnd = pScan->aAddrRange[ii+1]; 002486 if( iIns==0 ) break; 002487 if( iIns>0 ){ 002488 while( iIns<=iEnd ){ 002489 res += aOp[iIns].nCycle; 002490 iIns++; 002491 } 002492 }else{ 002493 int iOp; 002494 for(iOp=0; iOp<nOp; iOp++){ 002495 Op *pOp = &aOp[iOp]; 002496 if( pOp->p1!=iEnd ) continue; 002497 if( (sqlite3OpcodeProperty[pOp->opcode] & OPFLG_NCYCLE)==0 ){ 002498 continue; 002499 } 002500 res += aOp[iOp].nCycle; 002501 } 002502 } 002503 } 002504 } 002505 *(i64*)pOut = res; 002506 break; 002507 } 002508 default: { 002509 return 1; 002510 } 002511 } 002512 return 0; 002513 } 002514 002515 /* 002516 ** Return status data for a single loop within query pStmt. 002517 */ 002518 int sqlite3_stmt_scanstatus( 002519 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 002520 int iScan, /* Index of loop to report on */ 002521 int iScanStatusOp, /* Which metric to return */ 002522 void *pOut /* OUT: Write the answer here */ 002523 ){ 002524 return sqlite3_stmt_scanstatus_v2(pStmt, iScan, iScanStatusOp, 0, pOut); 002525 } 002526 002527 /* 002528 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 002529 */ 002530 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 002531 Vdbe *p = (Vdbe*)pStmt; 002532 int ii; 002533 for(ii=0; p!=0 && ii<p->nOp; ii++){ 002534 Op *pOp = &p->aOp[ii]; 002535 pOp->nExec = 0; 002536 pOp->nCycle = 0; 002537 } 002538 } 002539 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */