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 000019 #ifndef SQLITE_OMIT_DEPRECATED 000020 /* 000021 ** Return TRUE (non-zero) of the statement supplied as an argument needs 000022 ** to be recompiled. A statement needs to be recompiled whenever the 000023 ** execution environment changes in a way that would alter the program 000024 ** that sqlite3_prepare() generates. For example, if new functions or 000025 ** collating sequences are registered or if an authorizer function is 000026 ** added or changed. 000027 */ 000028 int sqlite3_expired(sqlite3_stmt *pStmt){ 000029 Vdbe *p = (Vdbe*)pStmt; 000030 return p==0 || p->expired; 000031 } 000032 #endif 000033 000034 /* 000035 ** Check on a Vdbe to make sure it has not been finalized. Log 000036 ** an error and return true if it has been finalized (or is otherwise 000037 ** invalid). Return false if it is ok. 000038 */ 000039 static int vdbeSafety(Vdbe *p){ 000040 if( p->db==0 ){ 000041 sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); 000042 return 1; 000043 }else{ 000044 return 0; 000045 } 000046 } 000047 static int vdbeSafetyNotNull(Vdbe *p){ 000048 if( p==0 ){ 000049 sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 000050 return 1; 000051 }else{ 000052 return vdbeSafety(p); 000053 } 000054 } 000055 000056 #ifndef SQLITE_OMIT_TRACE 000057 /* 000058 ** Invoke the profile callback. This routine is only called if we already 000059 ** know that the profile callback is defined and needs to be invoked. 000060 */ 000061 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 000062 sqlite3_int64 iNow; 000063 sqlite3_int64 iElapse; 000064 assert( p->startTime>0 ); 000065 assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 ); 000066 assert( db->init.busy==0 ); 000067 assert( p->zSql!=0 ); 000068 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 000069 iElapse = (iNow - p->startTime)*1000000; 000070 if( db->xProfile ){ 000071 db->xProfile(db->pProfileArg, p->zSql, iElapse); 000072 } 000073 if( db->mTrace & SQLITE_TRACE_PROFILE ){ 000074 db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 000075 } 000076 p->startTime = 0; 000077 } 000078 /* 000079 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 000080 ** is needed, and it invokes the callback if it is needed. 000081 */ 000082 # define checkProfileCallback(DB,P) \ 000083 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 000084 #else 000085 # define checkProfileCallback(DB,P) /*no-op*/ 000086 #endif 000087 000088 /* 000089 ** The following routine destroys a virtual machine that is created by 000090 ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 000091 ** success/failure code that describes the result of executing the virtual 000092 ** machine. 000093 ** 000094 ** This routine sets the error code and string returned by 000095 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 000096 */ 000097 int sqlite3_finalize(sqlite3_stmt *pStmt){ 000098 int rc; 000099 if( pStmt==0 ){ 000100 /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 000101 ** pointer is a harmless no-op. */ 000102 rc = SQLITE_OK; 000103 }else{ 000104 Vdbe *v = (Vdbe*)pStmt; 000105 sqlite3 *db = v->db; 000106 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 000107 sqlite3_mutex_enter(db->mutex); 000108 checkProfileCallback(db, v); 000109 rc = sqlite3VdbeFinalize(v); 000110 rc = sqlite3ApiExit(db, rc); 000111 sqlite3LeaveMutexAndCloseZombie(db); 000112 } 000113 return rc; 000114 } 000115 000116 /* 000117 ** Terminate the current execution of an SQL statement and reset it 000118 ** back to its starting state so that it can be reused. A success code from 000119 ** the prior execution is returned. 000120 ** 000121 ** This routine sets the error code and string returned by 000122 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 000123 */ 000124 int sqlite3_reset(sqlite3_stmt *pStmt){ 000125 int rc; 000126 if( pStmt==0 ){ 000127 rc = SQLITE_OK; 000128 }else{ 000129 Vdbe *v = (Vdbe*)pStmt; 000130 sqlite3 *db = v->db; 000131 sqlite3_mutex_enter(db->mutex); 000132 checkProfileCallback(db, v); 000133 rc = sqlite3VdbeReset(v); 000134 sqlite3VdbeRewind(v); 000135 assert( (rc & (db->errMask))==rc ); 000136 rc = sqlite3ApiExit(db, rc); 000137 sqlite3_mutex_leave(db->mutex); 000138 } 000139 return rc; 000140 } 000141 000142 /* 000143 ** Set all the parameters in the compiled SQL statement to NULL. 000144 */ 000145 int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 000146 int i; 000147 int rc = SQLITE_OK; 000148 Vdbe *p = (Vdbe*)pStmt; 000149 #if SQLITE_THREADSAFE 000150 sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 000151 #endif 000152 sqlite3_mutex_enter(mutex); 000153 for(i=0; i<p->nVar; i++){ 000154 sqlite3VdbeMemRelease(&p->aVar[i]); 000155 p->aVar[i].flags = MEM_Null; 000156 } 000157 if( p->isPrepareV2 && p->expmask ){ 000158 p->expired = 1; 000159 } 000160 sqlite3_mutex_leave(mutex); 000161 return rc; 000162 } 000163 000164 000165 /**************************** sqlite3_value_ ******************************* 000166 ** The following routines extract information from a Mem or sqlite3_value 000167 ** structure. 000168 */ 000169 const void *sqlite3_value_blob(sqlite3_value *pVal){ 000170 Mem *p = (Mem*)pVal; 000171 if( p->flags & (MEM_Blob|MEM_Str) ){ 000172 if( ExpandBlob(p)!=SQLITE_OK ){ 000173 assert( p->flags==MEM_Null && p->z==0 ); 000174 return 0; 000175 } 000176 p->flags |= MEM_Blob; 000177 return p->n ? p->z : 0; 000178 }else{ 000179 return sqlite3_value_text(pVal); 000180 } 000181 } 000182 int sqlite3_value_bytes(sqlite3_value *pVal){ 000183 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 000184 } 000185 int sqlite3_value_bytes16(sqlite3_value *pVal){ 000186 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 000187 } 000188 double sqlite3_value_double(sqlite3_value *pVal){ 000189 return sqlite3VdbeRealValue((Mem*)pVal); 000190 } 000191 int sqlite3_value_int(sqlite3_value *pVal){ 000192 return (int)sqlite3VdbeIntValue((Mem*)pVal); 000193 } 000194 sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 000195 return sqlite3VdbeIntValue((Mem*)pVal); 000196 } 000197 unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 000198 Mem *pMem = (Mem*)pVal; 000199 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 000200 } 000201 const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 000202 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 000203 } 000204 #ifndef SQLITE_OMIT_UTF16 000205 const void *sqlite3_value_text16(sqlite3_value* pVal){ 000206 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 000207 } 000208 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 000209 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 000210 } 000211 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 000212 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 000213 } 000214 #endif /* SQLITE_OMIT_UTF16 */ 000215 /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 000216 ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 000217 ** point number string BLOB NULL 000218 */ 000219 int sqlite3_value_type(sqlite3_value* pVal){ 000220 static const u8 aType[] = { 000221 SQLITE_BLOB, /* 0x00 */ 000222 SQLITE_NULL, /* 0x01 */ 000223 SQLITE_TEXT, /* 0x02 */ 000224 SQLITE_NULL, /* 0x03 */ 000225 SQLITE_INTEGER, /* 0x04 */ 000226 SQLITE_NULL, /* 0x05 */ 000227 SQLITE_INTEGER, /* 0x06 */ 000228 SQLITE_NULL, /* 0x07 */ 000229 SQLITE_FLOAT, /* 0x08 */ 000230 SQLITE_NULL, /* 0x09 */ 000231 SQLITE_FLOAT, /* 0x0a */ 000232 SQLITE_NULL, /* 0x0b */ 000233 SQLITE_INTEGER, /* 0x0c */ 000234 SQLITE_NULL, /* 0x0d */ 000235 SQLITE_INTEGER, /* 0x0e */ 000236 SQLITE_NULL, /* 0x0f */ 000237 SQLITE_BLOB, /* 0x10 */ 000238 SQLITE_NULL, /* 0x11 */ 000239 SQLITE_TEXT, /* 0x12 */ 000240 SQLITE_NULL, /* 0x13 */ 000241 SQLITE_INTEGER, /* 0x14 */ 000242 SQLITE_NULL, /* 0x15 */ 000243 SQLITE_INTEGER, /* 0x16 */ 000244 SQLITE_NULL, /* 0x17 */ 000245 SQLITE_FLOAT, /* 0x18 */ 000246 SQLITE_NULL, /* 0x19 */ 000247 SQLITE_FLOAT, /* 0x1a */ 000248 SQLITE_NULL, /* 0x1b */ 000249 SQLITE_INTEGER, /* 0x1c */ 000250 SQLITE_NULL, /* 0x1d */ 000251 SQLITE_INTEGER, /* 0x1e */ 000252 SQLITE_NULL, /* 0x1f */ 000253 }; 000254 return aType[pVal->flags&MEM_AffMask]; 000255 } 000256 000257 /* Make a copy of an sqlite3_value object 000258 */ 000259 sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 000260 sqlite3_value *pNew; 000261 if( pOrig==0 ) return 0; 000262 pNew = sqlite3_malloc( sizeof(*pNew) ); 000263 if( pNew==0 ) return 0; 000264 memset(pNew, 0, sizeof(*pNew)); 000265 memcpy(pNew, pOrig, MEMCELLSIZE); 000266 pNew->flags &= ~MEM_Dyn; 000267 pNew->db = 0; 000268 if( pNew->flags&(MEM_Str|MEM_Blob) ){ 000269 pNew->flags &= ~(MEM_Static|MEM_Dyn); 000270 pNew->flags |= MEM_Ephem; 000271 if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 000272 sqlite3ValueFree(pNew); 000273 pNew = 0; 000274 } 000275 } 000276 return pNew; 000277 } 000278 000279 /* Destroy an sqlite3_value object previously obtained from 000280 ** sqlite3_value_dup(). 000281 */ 000282 void sqlite3_value_free(sqlite3_value *pOld){ 000283 sqlite3ValueFree(pOld); 000284 } 000285 000286 000287 /**************************** sqlite3_result_ ******************************* 000288 ** The following routines are used by user-defined functions to specify 000289 ** the function result. 000290 ** 000291 ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 000292 ** result as a string or blob but if the string or blob is too large, it 000293 ** then sets the error code to SQLITE_TOOBIG 000294 ** 000295 ** The invokeValueDestructor(P,X) routine invokes destructor function X() 000296 ** on value P is not going to be used and need to be destroyed. 000297 */ 000298 static void setResultStrOrError( 000299 sqlite3_context *pCtx, /* Function context */ 000300 const char *z, /* String pointer */ 000301 int n, /* Bytes in string, or negative */ 000302 u8 enc, /* Encoding of z. 0 for BLOBs */ 000303 void (*xDel)(void*) /* Destructor function */ 000304 ){ 000305 if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ 000306 sqlite3_result_error_toobig(pCtx); 000307 } 000308 } 000309 static int invokeValueDestructor( 000310 const void *p, /* Value to destroy */ 000311 void (*xDel)(void*), /* The destructor */ 000312 sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ 000313 ){ 000314 assert( xDel!=SQLITE_DYNAMIC ); 000315 if( xDel==0 ){ 000316 /* noop */ 000317 }else if( xDel==SQLITE_TRANSIENT ){ 000318 /* noop */ 000319 }else{ 000320 xDel((void*)p); 000321 } 000322 if( pCtx ) sqlite3_result_error_toobig(pCtx); 000323 return SQLITE_TOOBIG; 000324 } 000325 void sqlite3_result_blob( 000326 sqlite3_context *pCtx, 000327 const void *z, 000328 int n, 000329 void (*xDel)(void *) 000330 ){ 000331 assert( n>=0 ); 000332 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000333 setResultStrOrError(pCtx, z, n, 0, xDel); 000334 } 000335 void sqlite3_result_blob64( 000336 sqlite3_context *pCtx, 000337 const void *z, 000338 sqlite3_uint64 n, 000339 void (*xDel)(void *) 000340 ){ 000341 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000342 assert( xDel!=SQLITE_DYNAMIC ); 000343 if( n>0x7fffffff ){ 000344 (void)invokeValueDestructor(z, xDel, pCtx); 000345 }else{ 000346 setResultStrOrError(pCtx, z, (int)n, 0, xDel); 000347 } 000348 } 000349 void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 000350 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000351 sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 000352 } 000353 void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 000354 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000355 pCtx->isError = SQLITE_ERROR; 000356 pCtx->fErrorOrAux = 1; 000357 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 000358 } 000359 #ifndef SQLITE_OMIT_UTF16 000360 void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 000361 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000362 pCtx->isError = SQLITE_ERROR; 000363 pCtx->fErrorOrAux = 1; 000364 sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 000365 } 000366 #endif 000367 void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 000368 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000369 sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 000370 } 000371 void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 000372 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000373 sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 000374 } 000375 void sqlite3_result_null(sqlite3_context *pCtx){ 000376 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000377 sqlite3VdbeMemSetNull(pCtx->pOut); 000378 } 000379 void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 000380 Mem *pOut = pCtx->pOut; 000381 assert( sqlite3_mutex_held(pOut->db->mutex) ); 000382 pOut->eSubtype = eSubtype & 0xff; 000383 pOut->flags |= MEM_Subtype; 000384 } 000385 void sqlite3_result_text( 000386 sqlite3_context *pCtx, 000387 const char *z, 000388 int n, 000389 void (*xDel)(void *) 000390 ){ 000391 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000392 setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 000393 } 000394 void sqlite3_result_text64( 000395 sqlite3_context *pCtx, 000396 const char *z, 000397 sqlite3_uint64 n, 000398 void (*xDel)(void *), 000399 unsigned char enc 000400 ){ 000401 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000402 assert( xDel!=SQLITE_DYNAMIC ); 000403 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 000404 if( n>0x7fffffff ){ 000405 (void)invokeValueDestructor(z, xDel, pCtx); 000406 }else{ 000407 setResultStrOrError(pCtx, z, (int)n, enc, xDel); 000408 } 000409 } 000410 #ifndef SQLITE_OMIT_UTF16 000411 void sqlite3_result_text16( 000412 sqlite3_context *pCtx, 000413 const void *z, 000414 int n, 000415 void (*xDel)(void *) 000416 ){ 000417 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000418 setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 000419 } 000420 void sqlite3_result_text16be( 000421 sqlite3_context *pCtx, 000422 const void *z, 000423 int n, 000424 void (*xDel)(void *) 000425 ){ 000426 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000427 setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 000428 } 000429 void sqlite3_result_text16le( 000430 sqlite3_context *pCtx, 000431 const void *z, 000432 int n, 000433 void (*xDel)(void *) 000434 ){ 000435 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000436 setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 000437 } 000438 #endif /* SQLITE_OMIT_UTF16 */ 000439 void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 000440 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000441 sqlite3VdbeMemCopy(pCtx->pOut, pValue); 000442 } 000443 void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 000444 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000445 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); 000446 } 000447 int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 000448 Mem *pOut = pCtx->pOut; 000449 assert( sqlite3_mutex_held(pOut->db->mutex) ); 000450 if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 000451 return SQLITE_TOOBIG; 000452 } 000453 sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 000454 return SQLITE_OK; 000455 } 000456 void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 000457 pCtx->isError = errCode; 000458 pCtx->fErrorOrAux = 1; 000459 #ifdef SQLITE_DEBUG 000460 if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 000461 #endif 000462 if( pCtx->pOut->flags & MEM_Null ){ 000463 sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 000464 SQLITE_UTF8, SQLITE_STATIC); 000465 } 000466 } 000467 000468 /* Force an SQLITE_TOOBIG error. */ 000469 void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 000470 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000471 pCtx->isError = SQLITE_TOOBIG; 000472 pCtx->fErrorOrAux = 1; 000473 sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 000474 SQLITE_UTF8, SQLITE_STATIC); 000475 } 000476 000477 /* An SQLITE_NOMEM error. */ 000478 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 000479 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000480 sqlite3VdbeMemSetNull(pCtx->pOut); 000481 pCtx->isError = SQLITE_NOMEM_BKPT; 000482 pCtx->fErrorOrAux = 1; 000483 sqlite3OomFault(pCtx->pOut->db); 000484 } 000485 000486 /* 000487 ** This function is called after a transaction has been committed. It 000488 ** invokes callbacks registered with sqlite3_wal_hook() as required. 000489 */ 000490 static int doWalCallbacks(sqlite3 *db){ 000491 int rc = SQLITE_OK; 000492 #ifndef SQLITE_OMIT_WAL 000493 int i; 000494 for(i=0; i<db->nDb; i++){ 000495 Btree *pBt = db->aDb[i].pBt; 000496 if( pBt ){ 000497 int nEntry; 000498 sqlite3BtreeEnter(pBt); 000499 nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 000500 sqlite3BtreeLeave(pBt); 000501 if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 000502 rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zDbSName, nEntry); 000503 } 000504 } 000505 } 000506 #endif 000507 return rc; 000508 } 000509 000510 000511 /* 000512 ** Execute the statement pStmt, either until a row of data is ready, the 000513 ** statement is completely executed or an error occurs. 000514 ** 000515 ** This routine implements the bulk of the logic behind the sqlite_step() 000516 ** API. The only thing omitted is the automatic recompile if a 000517 ** schema change has occurred. That detail is handled by the 000518 ** outer sqlite3_step() wrapper procedure. 000519 */ 000520 static int sqlite3Step(Vdbe *p){ 000521 sqlite3 *db; 000522 int rc; 000523 000524 assert(p); 000525 if( p->magic!=VDBE_MAGIC_RUN ){ 000526 /* We used to require that sqlite3_reset() be called before retrying 000527 ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 000528 ** with version 3.7.0, we changed this so that sqlite3_reset() would 000529 ** be called automatically instead of throwing the SQLITE_MISUSE error. 000530 ** This "automatic-reset" change is not technically an incompatibility, 000531 ** since any application that receives an SQLITE_MISUSE is broken by 000532 ** definition. 000533 ** 000534 ** Nevertheless, some published applications that were originally written 000535 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 000536 ** returns, and those were broken by the automatic-reset change. As a 000537 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 000538 ** legacy behavior of returning SQLITE_MISUSE for cases where the 000539 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 000540 ** or SQLITE_BUSY error. 000541 */ 000542 #ifdef SQLITE_OMIT_AUTORESET 000543 if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 000544 sqlite3_reset((sqlite3_stmt*)p); 000545 }else{ 000546 return SQLITE_MISUSE_BKPT; 000547 } 000548 #else 000549 sqlite3_reset((sqlite3_stmt*)p); 000550 #endif 000551 } 000552 000553 /* Check that malloc() has not failed. If it has, return early. */ 000554 db = p->db; 000555 if( db->mallocFailed ){ 000556 p->rc = SQLITE_NOMEM; 000557 return SQLITE_NOMEM_BKPT; 000558 } 000559 000560 if( p->pc<=0 && p->expired ){ 000561 p->rc = SQLITE_SCHEMA; 000562 rc = SQLITE_ERROR; 000563 goto end_of_step; 000564 } 000565 if( p->pc<0 ){ 000566 /* If there are no other statements currently running, then 000567 ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 000568 ** from interrupting a statement that has not yet started. 000569 */ 000570 if( db->nVdbeActive==0 ){ 000571 db->u1.isInterrupted = 0; 000572 } 000573 000574 assert( db->nVdbeWrite>0 || db->autoCommit==0 000575 || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 000576 ); 000577 000578 #ifndef SQLITE_OMIT_TRACE 000579 if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0) 000580 && !db->init.busy && p->zSql ){ 000581 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 000582 }else{ 000583 assert( p->startTime==0 ); 000584 } 000585 #endif 000586 000587 db->nVdbeActive++; 000588 if( p->readOnly==0 ) db->nVdbeWrite++; 000589 if( p->bIsReader ) db->nVdbeRead++; 000590 p->pc = 0; 000591 } 000592 #ifdef SQLITE_DEBUG 000593 p->rcApp = SQLITE_OK; 000594 #endif 000595 #ifndef SQLITE_OMIT_EXPLAIN 000596 if( p->explain ){ 000597 rc = sqlite3VdbeList(p); 000598 }else 000599 #endif /* SQLITE_OMIT_EXPLAIN */ 000600 { 000601 db->nVdbeExec++; 000602 rc = sqlite3VdbeExec(p); 000603 db->nVdbeExec--; 000604 } 000605 000606 #ifndef SQLITE_OMIT_TRACE 000607 /* If the statement completed successfully, invoke the profile callback */ 000608 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); 000609 #endif 000610 000611 if( rc==SQLITE_DONE ){ 000612 assert( p->rc==SQLITE_OK ); 000613 p->rc = doWalCallbacks(db); 000614 if( p->rc!=SQLITE_OK ){ 000615 rc = SQLITE_ERROR; 000616 } 000617 } 000618 000619 db->errCode = rc; 000620 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 000621 p->rc = SQLITE_NOMEM_BKPT; 000622 } 000623 end_of_step: 000624 /* At this point local variable rc holds the value that should be 000625 ** returned if this statement was compiled using the legacy 000626 ** sqlite3_prepare() interface. According to the docs, this can only 000627 ** be one of the values in the first assert() below. Variable p->rc 000628 ** contains the value that would be returned if sqlite3_finalize() 000629 ** were called on statement p. 000630 */ 000631 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 000632 || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 000633 ); 000634 assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); 000635 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 000636 /* If this statement was prepared using sqlite3_prepare_v2(), and an 000637 ** error has occurred, then return the error code in p->rc to the 000638 ** caller. Set the error code in the database handle to the same value. 000639 */ 000640 rc = sqlite3VdbeTransferError(p); 000641 } 000642 return (rc&db->errMask); 000643 } 000644 000645 /* 000646 ** This is the top-level implementation of sqlite3_step(). Call 000647 ** sqlite3Step() to do most of the work. If a schema error occurs, 000648 ** call sqlite3Reprepare() and try again. 000649 */ 000650 int sqlite3_step(sqlite3_stmt *pStmt){ 000651 int rc = SQLITE_OK; /* Result from sqlite3Step() */ 000652 int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ 000653 Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 000654 int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 000655 sqlite3 *db; /* The database connection */ 000656 000657 if( vdbeSafetyNotNull(v) ){ 000658 return SQLITE_MISUSE_BKPT; 000659 } 000660 db = v->db; 000661 sqlite3_mutex_enter(db->mutex); 000662 v->doingRerun = 0; 000663 while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 000664 && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 000665 int savedPc = v->pc; 000666 rc2 = rc = sqlite3Reprepare(v); 000667 if( rc!=SQLITE_OK) break; 000668 sqlite3_reset(pStmt); 000669 if( savedPc>=0 ) v->doingRerun = 1; 000670 assert( v->expired==0 ); 000671 } 000672 if( rc2!=SQLITE_OK ){ 000673 /* This case occurs after failing to recompile an sql statement. 000674 ** The error message from the SQL compiler has already been loaded 000675 ** into the database handle. This block copies the error message 000676 ** from the database handle into the statement and sets the statement 000677 ** program counter to 0 to ensure that when the statement is 000678 ** finalized or reset the parser error message is available via 000679 ** sqlite3_errmsg() and sqlite3_errcode(). 000680 */ 000681 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 000682 sqlite3DbFree(db, v->zErrMsg); 000683 if( !db->mallocFailed ){ 000684 v->zErrMsg = sqlite3DbStrDup(db, zErr); 000685 v->rc = rc2; 000686 } else { 000687 v->zErrMsg = 0; 000688 v->rc = rc = SQLITE_NOMEM_BKPT; 000689 } 000690 } 000691 rc = sqlite3ApiExit(db, rc); 000692 sqlite3_mutex_leave(db->mutex); 000693 return rc; 000694 } 000695 000696 000697 /* 000698 ** Extract the user data from a sqlite3_context structure and return a 000699 ** pointer to it. 000700 */ 000701 void *sqlite3_user_data(sqlite3_context *p){ 000702 assert( p && p->pFunc ); 000703 return p->pFunc->pUserData; 000704 } 000705 000706 /* 000707 ** Extract the user data from a sqlite3_context structure and return a 000708 ** pointer to it. 000709 ** 000710 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 000711 ** returns a copy of the pointer to the database connection (the 1st 000712 ** parameter) of the sqlite3_create_function() and 000713 ** sqlite3_create_function16() routines that originally registered the 000714 ** application defined function. 000715 */ 000716 sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 000717 assert( p && p->pOut ); 000718 return p->pOut->db; 000719 } 000720 000721 /* 000722 ** Return the current time for a statement. If the current time 000723 ** is requested more than once within the same run of a single prepared 000724 ** statement, the exact same time is returned for each invocation regardless 000725 ** of the amount of time that elapses between invocations. In other words, 000726 ** the time returned is always the time of the first call. 000727 */ 000728 sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 000729 int rc; 000730 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 000731 sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 000732 assert( p->pVdbe!=0 ); 000733 #else 000734 sqlite3_int64 iTime = 0; 000735 sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 000736 #endif 000737 if( *piTime==0 ){ 000738 rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 000739 if( rc ) *piTime = 0; 000740 } 000741 return *piTime; 000742 } 000743 000744 /* 000745 ** The following is the implementation of an SQL function that always 000746 ** fails with an error message stating that the function is used in the 000747 ** wrong context. The sqlite3_overload_function() API might construct 000748 ** SQL function that use this routine so that the functions will exist 000749 ** for name resolution but are actually overloaded by the xFindFunction 000750 ** method of virtual tables. 000751 */ 000752 void sqlite3InvalidFunction( 000753 sqlite3_context *context, /* The function calling context */ 000754 int NotUsed, /* Number of arguments to the function */ 000755 sqlite3_value **NotUsed2 /* Value of each argument */ 000756 ){ 000757 const char *zName = context->pFunc->zName; 000758 char *zErr; 000759 UNUSED_PARAMETER2(NotUsed, NotUsed2); 000760 zErr = sqlite3_mprintf( 000761 "unable to use function %s in the requested context", zName); 000762 sqlite3_result_error(context, zErr, -1); 000763 sqlite3_free(zErr); 000764 } 000765 000766 /* 000767 ** Create a new aggregate context for p and return a pointer to 000768 ** its pMem->z element. 000769 */ 000770 static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 000771 Mem *pMem = p->pMem; 000772 assert( (pMem->flags & MEM_Agg)==0 ); 000773 if( nByte<=0 ){ 000774 sqlite3VdbeMemSetNull(pMem); 000775 pMem->z = 0; 000776 }else{ 000777 sqlite3VdbeMemClearAndResize(pMem, nByte); 000778 pMem->flags = MEM_Agg; 000779 pMem->u.pDef = p->pFunc; 000780 if( pMem->z ){ 000781 memset(pMem->z, 0, nByte); 000782 } 000783 } 000784 return (void*)pMem->z; 000785 } 000786 000787 /* 000788 ** Allocate or return the aggregate context for a user function. A new 000789 ** context is allocated on the first call. Subsequent calls return the 000790 ** same context that was returned on prior calls. 000791 */ 000792 void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 000793 assert( p && p->pFunc && p->pFunc->xFinalize ); 000794 assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 000795 testcase( nByte<0 ); 000796 if( (p->pMem->flags & MEM_Agg)==0 ){ 000797 return createAggContext(p, nByte); 000798 }else{ 000799 return (void*)p->pMem->z; 000800 } 000801 } 000802 000803 /* 000804 ** Return the auxiliary data pointer, if any, for the iArg'th argument to 000805 ** the user-function defined by pCtx. 000806 */ 000807 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 000808 AuxData *pAuxData; 000809 000810 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000811 #if SQLITE_ENABLE_STAT3_OR_STAT4 000812 if( pCtx->pVdbe==0 ) return 0; 000813 #else 000814 assert( pCtx->pVdbe!=0 ); 000815 #endif 000816 for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 000817 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 000818 } 000819 000820 return (pAuxData ? pAuxData->pAux : 0); 000821 } 000822 000823 /* 000824 ** Set the auxiliary data pointer and delete function, for the iArg'th 000825 ** argument to the user-function defined by pCtx. Any previous value is 000826 ** deleted by calling the delete function specified when it was set. 000827 */ 000828 void sqlite3_set_auxdata( 000829 sqlite3_context *pCtx, 000830 int iArg, 000831 void *pAux, 000832 void (*xDelete)(void*) 000833 ){ 000834 AuxData *pAuxData; 000835 Vdbe *pVdbe = pCtx->pVdbe; 000836 000837 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 000838 if( iArg<0 ) goto failed; 000839 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 000840 if( pVdbe==0 ) goto failed; 000841 #else 000842 assert( pVdbe!=0 ); 000843 #endif 000844 000845 for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 000846 if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 000847 } 000848 if( pAuxData==0 ){ 000849 pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 000850 if( !pAuxData ) goto failed; 000851 pAuxData->iOp = pCtx->iOp; 000852 pAuxData->iArg = iArg; 000853 pAuxData->pNext = pVdbe->pAuxData; 000854 pVdbe->pAuxData = pAuxData; 000855 if( pCtx->fErrorOrAux==0 ){ 000856 pCtx->isError = 0; 000857 pCtx->fErrorOrAux = 1; 000858 } 000859 }else if( pAuxData->xDelete ){ 000860 pAuxData->xDelete(pAuxData->pAux); 000861 } 000862 000863 pAuxData->pAux = pAux; 000864 pAuxData->xDelete = xDelete; 000865 return; 000866 000867 failed: 000868 if( xDelete ){ 000869 xDelete(pAux); 000870 } 000871 } 000872 000873 #ifndef SQLITE_OMIT_DEPRECATED 000874 /* 000875 ** Return the number of times the Step function of an aggregate has been 000876 ** called. 000877 ** 000878 ** This function is deprecated. Do not use it for new code. It is 000879 ** provide only to avoid breaking legacy code. New aggregate function 000880 ** implementations should keep their own counts within their aggregate 000881 ** context. 000882 */ 000883 int sqlite3_aggregate_count(sqlite3_context *p){ 000884 assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 000885 return p->pMem->n; 000886 } 000887 #endif 000888 000889 /* 000890 ** Return the number of columns in the result set for the statement pStmt. 000891 */ 000892 int sqlite3_column_count(sqlite3_stmt *pStmt){ 000893 Vdbe *pVm = (Vdbe *)pStmt; 000894 return pVm ? pVm->nResColumn : 0; 000895 } 000896 000897 /* 000898 ** Return the number of values available from the current row of the 000899 ** currently executing statement pStmt. 000900 */ 000901 int sqlite3_data_count(sqlite3_stmt *pStmt){ 000902 Vdbe *pVm = (Vdbe *)pStmt; 000903 if( pVm==0 || pVm->pResultSet==0 ) return 0; 000904 return pVm->nResColumn; 000905 } 000906 000907 /* 000908 ** Return a pointer to static memory containing an SQL NULL value. 000909 */ 000910 static const Mem *columnNullValue(void){ 000911 /* Even though the Mem structure contains an element 000912 ** of type i64, on certain architectures (x86) with certain compiler 000913 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 000914 ** instead of an 8-byte one. This all works fine, except that when 000915 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 000916 ** that a Mem structure is located on an 8-byte boundary. To prevent 000917 ** these assert()s from failing, when building with SQLITE_DEBUG defined 000918 ** using gcc, we force nullMem to be 8-byte aligned using the magical 000919 ** __attribute__((aligned(8))) macro. */ 000920 static const Mem nullMem 000921 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 000922 __attribute__((aligned(8))) 000923 #endif 000924 = { 000925 /* .u = */ {0}, 000926 /* .flags = */ (u16)MEM_Null, 000927 /* .enc = */ (u8)0, 000928 /* .eSubtype = */ (u8)0, 000929 /* .n = */ (int)0, 000930 /* .z = */ (char*)0, 000931 /* .zMalloc = */ (char*)0, 000932 /* .szMalloc = */ (int)0, 000933 /* .uTemp = */ (u32)0, 000934 /* .db = */ (sqlite3*)0, 000935 /* .xDel = */ (void(*)(void*))0, 000936 #ifdef SQLITE_DEBUG 000937 /* .pScopyFrom = */ (Mem*)0, 000938 /* .pFiller = */ (void*)0, 000939 #endif 000940 }; 000941 return &nullMem; 000942 } 000943 000944 /* 000945 ** Check to see if column iCol of the given statement is valid. If 000946 ** it is, return a pointer to the Mem for the value of that column. 000947 ** If iCol is not valid, return a pointer to a Mem which has a value 000948 ** of NULL. 000949 */ 000950 static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 000951 Vdbe *pVm; 000952 Mem *pOut; 000953 000954 pVm = (Vdbe *)pStmt; 000955 if( pVm==0 ) return (Mem*)columnNullValue(); 000956 assert( pVm->db ); 000957 sqlite3_mutex_enter(pVm->db->mutex); 000958 if( pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 000959 pOut = &pVm->pResultSet[i]; 000960 }else{ 000961 sqlite3Error(pVm->db, SQLITE_RANGE); 000962 pOut = (Mem*)columnNullValue(); 000963 } 000964 return pOut; 000965 } 000966 000967 /* 000968 ** This function is called after invoking an sqlite3_value_XXX function on a 000969 ** column value (i.e. a value returned by evaluating an SQL expression in the 000970 ** select list of a SELECT statement) that may cause a malloc() failure. If 000971 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 000972 ** code of statement pStmt set to SQLITE_NOMEM. 000973 ** 000974 ** Specifically, this is called from within: 000975 ** 000976 ** sqlite3_column_int() 000977 ** sqlite3_column_int64() 000978 ** sqlite3_column_text() 000979 ** sqlite3_column_text16() 000980 ** sqlite3_column_real() 000981 ** sqlite3_column_bytes() 000982 ** sqlite3_column_bytes16() 000983 ** sqiite3_column_blob() 000984 */ 000985 static void columnMallocFailure(sqlite3_stmt *pStmt) 000986 { 000987 /* If malloc() failed during an encoding conversion within an 000988 ** sqlite3_column_XXX API, then set the return code of the statement to 000989 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 000990 ** and _finalize() will return NOMEM. 000991 */ 000992 Vdbe *p = (Vdbe *)pStmt; 000993 if( p ){ 000994 assert( p->db!=0 ); 000995 assert( sqlite3_mutex_held(p->db->mutex) ); 000996 p->rc = sqlite3ApiExit(p->db, p->rc); 000997 sqlite3_mutex_leave(p->db->mutex); 000998 } 000999 } 001000 001001 /**************************** sqlite3_column_ ******************************* 001002 ** The following routines are used to access elements of the current row 001003 ** in the result set. 001004 */ 001005 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 001006 const void *val; 001007 val = sqlite3_value_blob( columnMem(pStmt,i) ); 001008 /* Even though there is no encoding conversion, value_blob() might 001009 ** need to call malloc() to expand the result of a zeroblob() 001010 ** expression. 001011 */ 001012 columnMallocFailure(pStmt); 001013 return val; 001014 } 001015 int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 001016 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 001017 columnMallocFailure(pStmt); 001018 return val; 001019 } 001020 int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 001021 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 001022 columnMallocFailure(pStmt); 001023 return val; 001024 } 001025 double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 001026 double val = sqlite3_value_double( columnMem(pStmt,i) ); 001027 columnMallocFailure(pStmt); 001028 return val; 001029 } 001030 int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 001031 int val = sqlite3_value_int( columnMem(pStmt,i) ); 001032 columnMallocFailure(pStmt); 001033 return val; 001034 } 001035 sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 001036 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 001037 columnMallocFailure(pStmt); 001038 return val; 001039 } 001040 const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 001041 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 001042 columnMallocFailure(pStmt); 001043 return val; 001044 } 001045 sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 001046 Mem *pOut = columnMem(pStmt, i); 001047 if( pOut->flags&MEM_Static ){ 001048 pOut->flags &= ~MEM_Static; 001049 pOut->flags |= MEM_Ephem; 001050 } 001051 columnMallocFailure(pStmt); 001052 return (sqlite3_value *)pOut; 001053 } 001054 #ifndef SQLITE_OMIT_UTF16 001055 const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 001056 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 001057 columnMallocFailure(pStmt); 001058 return val; 001059 } 001060 #endif /* SQLITE_OMIT_UTF16 */ 001061 int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 001062 int iType = sqlite3_value_type( columnMem(pStmt,i) ); 001063 columnMallocFailure(pStmt); 001064 return iType; 001065 } 001066 001067 /* 001068 ** Convert the N-th element of pStmt->pColName[] into a string using 001069 ** xFunc() then return that string. If N is out of range, return 0. 001070 ** 001071 ** There are up to 5 names for each column. useType determines which 001072 ** name is returned. Here are the names: 001073 ** 001074 ** 0 The column name as it should be displayed for output 001075 ** 1 The datatype name for the column 001076 ** 2 The name of the database that the column derives from 001077 ** 3 The name of the table that the column derives from 001078 ** 4 The name of the table column that the result column derives from 001079 ** 001080 ** If the result is not a simple column reference (if it is an expression 001081 ** or a constant) then useTypes 2, 3, and 4 return NULL. 001082 */ 001083 static const void *columnName( 001084 sqlite3_stmt *pStmt, 001085 int N, 001086 const void *(*xFunc)(Mem*), 001087 int useType 001088 ){ 001089 const void *ret; 001090 Vdbe *p; 001091 int n; 001092 sqlite3 *db; 001093 #ifdef SQLITE_ENABLE_API_ARMOR 001094 if( pStmt==0 ){ 001095 (void)SQLITE_MISUSE_BKPT; 001096 return 0; 001097 } 001098 #endif 001099 ret = 0; 001100 p = (Vdbe *)pStmt; 001101 db = p->db; 001102 assert( db!=0 ); 001103 n = sqlite3_column_count(pStmt); 001104 if( N<n && N>=0 ){ 001105 N += useType*n; 001106 sqlite3_mutex_enter(db->mutex); 001107 assert( db->mallocFailed==0 ); 001108 ret = xFunc(&p->aColName[N]); 001109 /* A malloc may have failed inside of the xFunc() call. If this 001110 ** is the case, clear the mallocFailed flag and return NULL. 001111 */ 001112 if( db->mallocFailed ){ 001113 sqlite3OomClear(db); 001114 ret = 0; 001115 } 001116 sqlite3_mutex_leave(db->mutex); 001117 } 001118 return ret; 001119 } 001120 001121 /* 001122 ** Return the name of the Nth column of the result set returned by SQL 001123 ** statement pStmt. 001124 */ 001125 const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 001126 return columnName( 001127 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 001128 } 001129 #ifndef SQLITE_OMIT_UTF16 001130 const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 001131 return columnName( 001132 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 001133 } 001134 #endif 001135 001136 /* 001137 ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 001138 ** not define OMIT_DECLTYPE. 001139 */ 001140 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 001141 # error "Must not define both SQLITE_OMIT_DECLTYPE \ 001142 and SQLITE_ENABLE_COLUMN_METADATA" 001143 #endif 001144 001145 #ifndef SQLITE_OMIT_DECLTYPE 001146 /* 001147 ** Return the column declaration type (if applicable) of the 'i'th column 001148 ** of the result set of SQL statement pStmt. 001149 */ 001150 const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 001151 return columnName( 001152 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 001153 } 001154 #ifndef SQLITE_OMIT_UTF16 001155 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 001156 return columnName( 001157 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 001158 } 001159 #endif /* SQLITE_OMIT_UTF16 */ 001160 #endif /* SQLITE_OMIT_DECLTYPE */ 001161 001162 #ifdef SQLITE_ENABLE_COLUMN_METADATA 001163 /* 001164 ** Return the name of the database from which a result column derives. 001165 ** NULL is returned if the result column is an expression or constant or 001166 ** anything else which is not an unambiguous reference to a database column. 001167 */ 001168 const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 001169 return columnName( 001170 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 001171 } 001172 #ifndef SQLITE_OMIT_UTF16 001173 const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 001174 return columnName( 001175 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 001176 } 001177 #endif /* SQLITE_OMIT_UTF16 */ 001178 001179 /* 001180 ** Return the name of the table from which a result column derives. 001181 ** NULL is returned if the result column is an expression or constant or 001182 ** anything else which is not an unambiguous reference to a database column. 001183 */ 001184 const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 001185 return columnName( 001186 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 001187 } 001188 #ifndef SQLITE_OMIT_UTF16 001189 const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 001190 return columnName( 001191 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 001192 } 001193 #endif /* SQLITE_OMIT_UTF16 */ 001194 001195 /* 001196 ** Return the name of the table column from which a result column derives. 001197 ** NULL is returned if the result column is an expression or constant or 001198 ** anything else which is not an unambiguous reference to a database column. 001199 */ 001200 const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 001201 return columnName( 001202 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 001203 } 001204 #ifndef SQLITE_OMIT_UTF16 001205 const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 001206 return columnName( 001207 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 001208 } 001209 #endif /* SQLITE_OMIT_UTF16 */ 001210 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 001211 001212 001213 /******************************* sqlite3_bind_ *************************** 001214 ** 001215 ** Routines used to attach values to wildcards in a compiled SQL statement. 001216 */ 001217 /* 001218 ** Unbind the value bound to variable i in virtual machine p. This is the 001219 ** the same as binding a NULL value to the column. If the "i" parameter is 001220 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 001221 ** 001222 ** A successful evaluation of this routine acquires the mutex on p. 001223 ** the mutex is released if any kind of error occurs. 001224 ** 001225 ** The error code stored in database p->db is overwritten with the return 001226 ** value in any case. 001227 */ 001228 static int vdbeUnbind(Vdbe *p, int i){ 001229 Mem *pVar; 001230 if( vdbeSafetyNotNull(p) ){ 001231 return SQLITE_MISUSE_BKPT; 001232 } 001233 sqlite3_mutex_enter(p->db->mutex); 001234 if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 001235 sqlite3Error(p->db, SQLITE_MISUSE); 001236 sqlite3_mutex_leave(p->db->mutex); 001237 sqlite3_log(SQLITE_MISUSE, 001238 "bind on a busy prepared statement: [%s]", p->zSql); 001239 return SQLITE_MISUSE_BKPT; 001240 } 001241 if( i<1 || i>p->nVar ){ 001242 sqlite3Error(p->db, SQLITE_RANGE); 001243 sqlite3_mutex_leave(p->db->mutex); 001244 return SQLITE_RANGE; 001245 } 001246 i--; 001247 pVar = &p->aVar[i]; 001248 sqlite3VdbeMemRelease(pVar); 001249 pVar->flags = MEM_Null; 001250 sqlite3Error(p->db, SQLITE_OK); 001251 001252 /* If the bit corresponding to this variable in Vdbe.expmask is set, then 001253 ** binding a new value to this variable invalidates the current query plan. 001254 ** 001255 ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host 001256 ** parameter in the WHERE clause might influence the choice of query plan 001257 ** for a statement, then the statement will be automatically recompiled, 001258 ** as if there had been a schema change, on the first sqlite3_step() call 001259 ** following any change to the bindings of that parameter. 001260 */ 001261 if( p->isPrepareV2 && 001262 ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) 001263 ){ 001264 p->expired = 1; 001265 } 001266 return SQLITE_OK; 001267 } 001268 001269 /* 001270 ** Bind a text or BLOB value. 001271 */ 001272 static int bindText( 001273 sqlite3_stmt *pStmt, /* The statement to bind against */ 001274 int i, /* Index of the parameter to bind */ 001275 const void *zData, /* Pointer to the data to be bound */ 001276 int nData, /* Number of bytes of data to be bound */ 001277 void (*xDel)(void*), /* Destructor for the data */ 001278 u8 encoding /* Encoding for the data */ 001279 ){ 001280 Vdbe *p = (Vdbe *)pStmt; 001281 Mem *pVar; 001282 int rc; 001283 001284 rc = vdbeUnbind(p, i); 001285 if( rc==SQLITE_OK ){ 001286 if( zData!=0 ){ 001287 pVar = &p->aVar[i-1]; 001288 rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 001289 if( rc==SQLITE_OK && encoding!=0 ){ 001290 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 001291 } 001292 sqlite3Error(p->db, rc); 001293 rc = sqlite3ApiExit(p->db, rc); 001294 } 001295 sqlite3_mutex_leave(p->db->mutex); 001296 }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 001297 xDel((void*)zData); 001298 } 001299 return rc; 001300 } 001301 001302 001303 /* 001304 ** Bind a blob value to an SQL statement variable. 001305 */ 001306 int sqlite3_bind_blob( 001307 sqlite3_stmt *pStmt, 001308 int i, 001309 const void *zData, 001310 int nData, 001311 void (*xDel)(void*) 001312 ){ 001313 #ifdef SQLITE_ENABLE_API_ARMOR 001314 if( nData<0 ) return SQLITE_MISUSE_BKPT; 001315 #endif 001316 return bindText(pStmt, i, zData, nData, xDel, 0); 001317 } 001318 int sqlite3_bind_blob64( 001319 sqlite3_stmt *pStmt, 001320 int i, 001321 const void *zData, 001322 sqlite3_uint64 nData, 001323 void (*xDel)(void*) 001324 ){ 001325 assert( xDel!=SQLITE_DYNAMIC ); 001326 if( nData>0x7fffffff ){ 001327 return invokeValueDestructor(zData, xDel, 0); 001328 }else{ 001329 return bindText(pStmt, i, zData, (int)nData, xDel, 0); 001330 } 001331 } 001332 int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 001333 int rc; 001334 Vdbe *p = (Vdbe *)pStmt; 001335 rc = vdbeUnbind(p, i); 001336 if( rc==SQLITE_OK ){ 001337 sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 001338 sqlite3_mutex_leave(p->db->mutex); 001339 } 001340 return rc; 001341 } 001342 int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 001343 return sqlite3_bind_int64(p, i, (i64)iValue); 001344 } 001345 int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 001346 int rc; 001347 Vdbe *p = (Vdbe *)pStmt; 001348 rc = vdbeUnbind(p, i); 001349 if( rc==SQLITE_OK ){ 001350 sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 001351 sqlite3_mutex_leave(p->db->mutex); 001352 } 001353 return rc; 001354 } 001355 int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 001356 int rc; 001357 Vdbe *p = (Vdbe*)pStmt; 001358 rc = vdbeUnbind(p, i); 001359 if( rc==SQLITE_OK ){ 001360 sqlite3_mutex_leave(p->db->mutex); 001361 } 001362 return rc; 001363 } 001364 int sqlite3_bind_text( 001365 sqlite3_stmt *pStmt, 001366 int i, 001367 const char *zData, 001368 int nData, 001369 void (*xDel)(void*) 001370 ){ 001371 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 001372 } 001373 int sqlite3_bind_text64( 001374 sqlite3_stmt *pStmt, 001375 int i, 001376 const char *zData, 001377 sqlite3_uint64 nData, 001378 void (*xDel)(void*), 001379 unsigned char enc 001380 ){ 001381 assert( xDel!=SQLITE_DYNAMIC ); 001382 if( nData>0x7fffffff ){ 001383 return invokeValueDestructor(zData, xDel, 0); 001384 }else{ 001385 if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 001386 return bindText(pStmt, i, zData, (int)nData, xDel, enc); 001387 } 001388 } 001389 #ifndef SQLITE_OMIT_UTF16 001390 int sqlite3_bind_text16( 001391 sqlite3_stmt *pStmt, 001392 int i, 001393 const void *zData, 001394 int nData, 001395 void (*xDel)(void*) 001396 ){ 001397 return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 001398 } 001399 #endif /* SQLITE_OMIT_UTF16 */ 001400 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 001401 int rc; 001402 switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 001403 case SQLITE_INTEGER: { 001404 rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 001405 break; 001406 } 001407 case SQLITE_FLOAT: { 001408 rc = sqlite3_bind_double(pStmt, i, pValue->u.r); 001409 break; 001410 } 001411 case SQLITE_BLOB: { 001412 if( pValue->flags & MEM_Zero ){ 001413 rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 001414 }else{ 001415 rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 001416 } 001417 break; 001418 } 001419 case SQLITE_TEXT: { 001420 rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 001421 pValue->enc); 001422 break; 001423 } 001424 default: { 001425 rc = sqlite3_bind_null(pStmt, i); 001426 break; 001427 } 001428 } 001429 return rc; 001430 } 001431 int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 001432 int rc; 001433 Vdbe *p = (Vdbe *)pStmt; 001434 rc = vdbeUnbind(p, i); 001435 if( rc==SQLITE_OK ){ 001436 sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 001437 sqlite3_mutex_leave(p->db->mutex); 001438 } 001439 return rc; 001440 } 001441 int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 001442 int rc; 001443 Vdbe *p = (Vdbe *)pStmt; 001444 sqlite3_mutex_enter(p->db->mutex); 001445 if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 001446 rc = SQLITE_TOOBIG; 001447 }else{ 001448 assert( (n & 0x7FFFFFFF)==n ); 001449 rc = sqlite3_bind_zeroblob(pStmt, i, n); 001450 } 001451 rc = sqlite3ApiExit(p->db, rc); 001452 sqlite3_mutex_leave(p->db->mutex); 001453 return rc; 001454 } 001455 001456 /* 001457 ** Return the number of wildcards that can be potentially bound to. 001458 ** This routine is added to support DBD::SQLite. 001459 */ 001460 int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 001461 Vdbe *p = (Vdbe*)pStmt; 001462 return p ? p->nVar : 0; 001463 } 001464 001465 /* 001466 ** Return the name of a wildcard parameter. Return NULL if the index 001467 ** is out of range or if the wildcard is unnamed. 001468 ** 001469 ** The result is always UTF-8. 001470 */ 001471 const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 001472 Vdbe *p = (Vdbe*)pStmt; 001473 if( p==0 ) return 0; 001474 return sqlite3VListNumToName(p->pVList, i); 001475 } 001476 001477 /* 001478 ** Given a wildcard parameter name, return the index of the variable 001479 ** with that name. If there is no variable with the given name, 001480 ** return 0. 001481 */ 001482 int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 001483 if( p==0 || zName==0 ) return 0; 001484 return sqlite3VListNameToNum(p->pVList, zName, nName); 001485 } 001486 int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 001487 return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 001488 } 001489 001490 /* 001491 ** Transfer all bindings from the first statement over to the second. 001492 */ 001493 int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001494 Vdbe *pFrom = (Vdbe*)pFromStmt; 001495 Vdbe *pTo = (Vdbe*)pToStmt; 001496 int i; 001497 assert( pTo->db==pFrom->db ); 001498 assert( pTo->nVar==pFrom->nVar ); 001499 sqlite3_mutex_enter(pTo->db->mutex); 001500 for(i=0; i<pFrom->nVar; i++){ 001501 sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 001502 } 001503 sqlite3_mutex_leave(pTo->db->mutex); 001504 return SQLITE_OK; 001505 } 001506 001507 #ifndef SQLITE_OMIT_DEPRECATED 001508 /* 001509 ** Deprecated external interface. Internal/core SQLite code 001510 ** should call sqlite3TransferBindings. 001511 ** 001512 ** It is misuse to call this routine with statements from different 001513 ** database connections. But as this is a deprecated interface, we 001514 ** will not bother to check for that condition. 001515 ** 001516 ** If the two statements contain a different number of bindings, then 001517 ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 001518 ** SQLITE_OK is returned. 001519 */ 001520 int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 001521 Vdbe *pFrom = (Vdbe*)pFromStmt; 001522 Vdbe *pTo = (Vdbe*)pToStmt; 001523 if( pFrom->nVar!=pTo->nVar ){ 001524 return SQLITE_ERROR; 001525 } 001526 if( pTo->isPrepareV2 && pTo->expmask ){ 001527 pTo->expired = 1; 001528 } 001529 if( pFrom->isPrepareV2 && pFrom->expmask ){ 001530 pFrom->expired = 1; 001531 } 001532 return sqlite3TransferBindings(pFromStmt, pToStmt); 001533 } 001534 #endif 001535 001536 /* 001537 ** Return the sqlite3* database handle to which the prepared statement given 001538 ** in the argument belongs. This is the same database handle that was 001539 ** the first argument to the sqlite3_prepare() that was used to create 001540 ** the statement in the first place. 001541 */ 001542 sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 001543 return pStmt ? ((Vdbe*)pStmt)->db : 0; 001544 } 001545 001546 /* 001547 ** Return true if the prepared statement is guaranteed to not modify the 001548 ** database. 001549 */ 001550 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 001551 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 001552 } 001553 001554 /* 001555 ** Return true if the prepared statement is in need of being reset. 001556 */ 001557 int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 001558 Vdbe *v = (Vdbe*)pStmt; 001559 return v!=0 && v->magic==VDBE_MAGIC_RUN && v->pc>=0; 001560 } 001561 001562 /* 001563 ** Return a pointer to the next prepared statement after pStmt associated 001564 ** with database connection pDb. If pStmt is NULL, return the first 001565 ** prepared statement for the database connection. Return NULL if there 001566 ** are no more. 001567 */ 001568 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 001569 sqlite3_stmt *pNext; 001570 #ifdef SQLITE_ENABLE_API_ARMOR 001571 if( !sqlite3SafetyCheckOk(pDb) ){ 001572 (void)SQLITE_MISUSE_BKPT; 001573 return 0; 001574 } 001575 #endif 001576 sqlite3_mutex_enter(pDb->mutex); 001577 if( pStmt==0 ){ 001578 pNext = (sqlite3_stmt*)pDb->pVdbe; 001579 }else{ 001580 pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 001581 } 001582 sqlite3_mutex_leave(pDb->mutex); 001583 return pNext; 001584 } 001585 001586 /* 001587 ** Return the value of a status counter for a prepared statement 001588 */ 001589 int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 001590 Vdbe *pVdbe = (Vdbe*)pStmt; 001591 u32 v; 001592 #ifdef SQLITE_ENABLE_API_ARMOR 001593 if( !pStmt ){ 001594 (void)SQLITE_MISUSE_BKPT; 001595 return 0; 001596 } 001597 #endif 001598 v = pVdbe->aCounter[op]; 001599 if( resetFlag ) pVdbe->aCounter[op] = 0; 001600 return (int)v; 001601 } 001602 001603 /* 001604 ** Return the SQL associated with a prepared statement 001605 */ 001606 const char *sqlite3_sql(sqlite3_stmt *pStmt){ 001607 Vdbe *p = (Vdbe *)pStmt; 001608 return p ? p->zSql : 0; 001609 } 001610 001611 /* 001612 ** Return the SQL associated with a prepared statement with 001613 ** bound parameters expanded. Space to hold the returned string is 001614 ** obtained from sqlite3_malloc(). The caller is responsible for 001615 ** freeing the returned string by passing it to sqlite3_free(). 001616 ** 001617 ** The SQLITE_TRACE_SIZE_LIMIT puts an upper bound on the size of 001618 ** expanded bound parameters. 001619 */ 001620 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt){ 001621 #ifdef SQLITE_OMIT_TRACE 001622 return 0; 001623 #else 001624 char *z = 0; 001625 const char *zSql = sqlite3_sql(pStmt); 001626 if( zSql ){ 001627 Vdbe *p = (Vdbe *)pStmt; 001628 sqlite3_mutex_enter(p->db->mutex); 001629 z = sqlite3VdbeExpandSql(p, zSql); 001630 sqlite3_mutex_leave(p->db->mutex); 001631 } 001632 return z; 001633 #endif 001634 } 001635 001636 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 001637 /* 001638 ** Allocate and populate an UnpackedRecord structure based on the serialized 001639 ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 001640 ** if successful, or a NULL pointer if an OOM error is encountered. 001641 */ 001642 static UnpackedRecord *vdbeUnpackRecord( 001643 KeyInfo *pKeyInfo, 001644 int nKey, 001645 const void *pKey 001646 ){ 001647 UnpackedRecord *pRet; /* Return value */ 001648 001649 pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 001650 if( pRet ){ 001651 memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1)); 001652 sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 001653 } 001654 return pRet; 001655 } 001656 001657 /* 001658 ** This function is called from within a pre-update callback to retrieve 001659 ** a field of the row currently being updated or deleted. 001660 */ 001661 int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 001662 PreUpdate *p = db->pPreUpdate; 001663 int rc = SQLITE_OK; 001664 001665 /* Test that this call is being made from within an SQLITE_DELETE or 001666 ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 001667 if( !p || p->op==SQLITE_INSERT ){ 001668 rc = SQLITE_MISUSE_BKPT; 001669 goto preupdate_old_out; 001670 } 001671 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 001672 rc = SQLITE_RANGE; 001673 goto preupdate_old_out; 001674 } 001675 001676 /* If the old.* record has not yet been loaded into memory, do so now. */ 001677 if( p->pUnpacked==0 ){ 001678 u32 nRec; 001679 u8 *aRec; 001680 001681 nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 001682 aRec = sqlite3DbMallocRaw(db, nRec); 001683 if( !aRec ) goto preupdate_old_out; 001684 rc = sqlite3BtreePayload(p->pCsr->uc.pCursor, 0, nRec, aRec); 001685 if( rc==SQLITE_OK ){ 001686 p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 001687 if( !p->pUnpacked ) rc = SQLITE_NOMEM; 001688 } 001689 if( rc!=SQLITE_OK ){ 001690 sqlite3DbFree(db, aRec); 001691 goto preupdate_old_out; 001692 } 001693 p->aRecord = aRec; 001694 } 001695 001696 if( iIdx>=p->pUnpacked->nField ){ 001697 *ppValue = (sqlite3_value *)columnNullValue(); 001698 }else{ 001699 Mem *pMem = *ppValue = &p->pUnpacked->aMem[iIdx]; 001700 *ppValue = &p->pUnpacked->aMem[iIdx]; 001701 if( iIdx==p->pTab->iPKey ){ 001702 sqlite3VdbeMemSetInt64(pMem, p->iKey1); 001703 }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){ 001704 if( pMem->flags & MEM_Int ){ 001705 sqlite3VdbeMemRealify(pMem); 001706 } 001707 } 001708 } 001709 001710 preupdate_old_out: 001711 sqlite3Error(db, rc); 001712 return sqlite3ApiExit(db, rc); 001713 } 001714 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 001715 001716 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 001717 /* 001718 ** This function is called from within a pre-update callback to retrieve 001719 ** the number of columns in the row being updated, deleted or inserted. 001720 */ 001721 int sqlite3_preupdate_count(sqlite3 *db){ 001722 PreUpdate *p = db->pPreUpdate; 001723 return (p ? p->keyinfo.nField : 0); 001724 } 001725 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 001726 001727 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 001728 /* 001729 ** This function is designed to be called from within a pre-update callback 001730 ** only. It returns zero if the change that caused the callback was made 001731 ** immediately by a user SQL statement. Or, if the change was made by a 001732 ** trigger program, it returns the number of trigger programs currently 001733 ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 001734 ** top-level trigger etc.). 001735 ** 001736 ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 001737 ** or SET DEFAULT action is considered a trigger. 001738 */ 001739 int sqlite3_preupdate_depth(sqlite3 *db){ 001740 PreUpdate *p = db->pPreUpdate; 001741 return (p ? p->v->nFrame : 0); 001742 } 001743 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 001744 001745 #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 001746 /* 001747 ** This function is called from within a pre-update callback to retrieve 001748 ** a field of the row currently being updated or inserted. 001749 */ 001750 int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 001751 PreUpdate *p = db->pPreUpdate; 001752 int rc = SQLITE_OK; 001753 Mem *pMem; 001754 001755 if( !p || p->op==SQLITE_DELETE ){ 001756 rc = SQLITE_MISUSE_BKPT; 001757 goto preupdate_new_out; 001758 } 001759 if( iIdx>=p->pCsr->nField || iIdx<0 ){ 001760 rc = SQLITE_RANGE; 001761 goto preupdate_new_out; 001762 } 001763 001764 if( p->op==SQLITE_INSERT ){ 001765 /* For an INSERT, memory cell p->iNewReg contains the serialized record 001766 ** that is being inserted. Deserialize it. */ 001767 UnpackedRecord *pUnpack = p->pNewUnpacked; 001768 if( !pUnpack ){ 001769 Mem *pData = &p->v->aMem[p->iNewReg]; 001770 rc = ExpandBlob(pData); 001771 if( rc!=SQLITE_OK ) goto preupdate_new_out; 001772 pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 001773 if( !pUnpack ){ 001774 rc = SQLITE_NOMEM; 001775 goto preupdate_new_out; 001776 } 001777 p->pNewUnpacked = pUnpack; 001778 } 001779 if( iIdx>=pUnpack->nField ){ 001780 pMem = (sqlite3_value *)columnNullValue(); 001781 }else{ 001782 pMem = &pUnpack->aMem[iIdx]; 001783 if( iIdx==p->pTab->iPKey ){ 001784 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 001785 } 001786 } 001787 }else{ 001788 /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 001789 ** value. Make a copy of the cell contents and return a pointer to it. 001790 ** It is not safe to return a pointer to the memory cell itself as the 001791 ** caller may modify the value text encoding. 001792 */ 001793 assert( p->op==SQLITE_UPDATE ); 001794 if( !p->aNew ){ 001795 p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 001796 if( !p->aNew ){ 001797 rc = SQLITE_NOMEM; 001798 goto preupdate_new_out; 001799 } 001800 } 001801 assert( iIdx>=0 && iIdx<p->pCsr->nField ); 001802 pMem = &p->aNew[iIdx]; 001803 if( pMem->flags==0 ){ 001804 if( iIdx==p->pTab->iPKey ){ 001805 sqlite3VdbeMemSetInt64(pMem, p->iKey2); 001806 }else{ 001807 rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 001808 if( rc!=SQLITE_OK ) goto preupdate_new_out; 001809 } 001810 } 001811 } 001812 *ppValue = pMem; 001813 001814 preupdate_new_out: 001815 sqlite3Error(db, rc); 001816 return sqlite3ApiExit(db, rc); 001817 } 001818 #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 001819 001820 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 001821 /* 001822 ** Return status data for a single loop within query pStmt. 001823 */ 001824 int sqlite3_stmt_scanstatus( 001825 sqlite3_stmt *pStmt, /* Prepared statement being queried */ 001826 int idx, /* Index of loop to report on */ 001827 int iScanStatusOp, /* Which metric to return */ 001828 void *pOut /* OUT: Write the answer here */ 001829 ){ 001830 Vdbe *p = (Vdbe*)pStmt; 001831 ScanStatus *pScan; 001832 if( idx<0 || idx>=p->nScan ) return 1; 001833 pScan = &p->aScan[idx]; 001834 switch( iScanStatusOp ){ 001835 case SQLITE_SCANSTAT_NLOOP: { 001836 *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; 001837 break; 001838 } 001839 case SQLITE_SCANSTAT_NVISIT: { 001840 *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; 001841 break; 001842 } 001843 case SQLITE_SCANSTAT_EST: { 001844 double r = 1.0; 001845 LogEst x = pScan->nEst; 001846 while( x<100 ){ 001847 x += 10; 001848 r *= 0.5; 001849 } 001850 *(double*)pOut = r*sqlite3LogEstToInt(x); 001851 break; 001852 } 001853 case SQLITE_SCANSTAT_NAME: { 001854 *(const char**)pOut = pScan->zName; 001855 break; 001856 } 001857 case SQLITE_SCANSTAT_EXPLAIN: { 001858 if( pScan->addrExplain ){ 001859 *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; 001860 }else{ 001861 *(const char**)pOut = 0; 001862 } 001863 break; 001864 } 001865 case SQLITE_SCANSTAT_SELECTID: { 001866 if( pScan->addrExplain ){ 001867 *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; 001868 }else{ 001869 *(int*)pOut = -1; 001870 } 001871 break; 001872 } 001873 default: { 001874 return 1; 001875 } 001876 } 001877 return 0; 001878 } 001879 001880 /* 001881 ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 001882 */ 001883 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 001884 Vdbe *p = (Vdbe*)pStmt; 001885 memset(p->anExec, 0, p->nOp * sizeof(i64)); 001886 } 001887 #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */