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