14f26d6c4Sdrh /* 24f26d6c4Sdrh ** 2004 May 26 34f26d6c4Sdrh ** 44f26d6c4Sdrh ** The author disclaims copyright to this source code. In place of 54f26d6c4Sdrh ** a legal notice, here is a blessing: 64f26d6c4Sdrh ** 74f26d6c4Sdrh ** May you do good and not evil. 84f26d6c4Sdrh ** May you find forgiveness for yourself and forgive others. 94f26d6c4Sdrh ** May you share freely, never taking more than you give. 104f26d6c4Sdrh ** 114f26d6c4Sdrh ************************************************************************* 124f26d6c4Sdrh ** 134f26d6c4Sdrh ** This file contains code use to implement APIs that are part of the 144f26d6c4Sdrh ** VDBE. 154f26d6c4Sdrh */ 164f26d6c4Sdrh #include "sqliteInt.h" 174f26d6c4Sdrh #include "vdbeInt.h" 184f26d6c4Sdrh 19eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 20d89bd007Sdrh /* 21d89bd007Sdrh ** Return TRUE (non-zero) of the statement supplied as an argument needs 22d89bd007Sdrh ** to be recompiled. A statement needs to be recompiled whenever the 23d89bd007Sdrh ** execution environment changes in a way that would alter the program 24d89bd007Sdrh ** that sqlite3_prepare() generates. For example, if new functions or 25d89bd007Sdrh ** collating sequences are registered or if an authorizer function is 26d89bd007Sdrh ** added or changed. 27d89bd007Sdrh */ 28d89bd007Sdrh int sqlite3_expired(sqlite3_stmt *pStmt){ 29d89bd007Sdrh Vdbe *p = (Vdbe*)pStmt; 30d89bd007Sdrh return p==0 || p->expired; 31d89bd007Sdrh } 32eec556d3Sshane #endif 33d89bd007Sdrh 34e30f4426Sdrh /* 35413c3d36Sdrh ** Check on a Vdbe to make sure it has not been finalized. Log 36413c3d36Sdrh ** an error and return true if it has been finalized (or is otherwise 37413c3d36Sdrh ** invalid). Return false if it is ok. 38413c3d36Sdrh */ 39413c3d36Sdrh static int vdbeSafety(Vdbe *p){ 40413c3d36Sdrh if( p->db==0 ){ 41413c3d36Sdrh sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement"); 42413c3d36Sdrh return 1; 43413c3d36Sdrh }else{ 44413c3d36Sdrh return 0; 45413c3d36Sdrh } 46413c3d36Sdrh } 47413c3d36Sdrh static int vdbeSafetyNotNull(Vdbe *p){ 48413c3d36Sdrh if( p==0 ){ 49413c3d36Sdrh sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement"); 50413c3d36Sdrh return 1; 51413c3d36Sdrh }else{ 52413c3d36Sdrh return vdbeSafety(p); 53413c3d36Sdrh } 54413c3d36Sdrh } 55413c3d36Sdrh 56201e0c68Sdrh #ifndef SQLITE_OMIT_TRACE 57201e0c68Sdrh /* 58201e0c68Sdrh ** Invoke the profile callback. This routine is only called if we already 59201e0c68Sdrh ** know that the profile callback is defined and needs to be invoked. 60201e0c68Sdrh */ 61201e0c68Sdrh static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){ 62201e0c68Sdrh sqlite3_int64 iNow; 633d2a529dSdrh sqlite3_int64 iElapse; 64201e0c68Sdrh assert( p->startTime>0 ); 653d2a529dSdrh assert( db->xProfile!=0 || (db->mTrace & SQLITE_TRACE_PROFILE)!=0 ); 66201e0c68Sdrh assert( db->init.busy==0 ); 67201e0c68Sdrh assert( p->zSql!=0 ); 68201e0c68Sdrh sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 693d2a529dSdrh iElapse = (iNow - p->startTime)*1000000; 703d2a529dSdrh if( db->xProfile ){ 713d2a529dSdrh db->xProfile(db->pProfileArg, p->zSql, iElapse); 723d2a529dSdrh } 733d2a529dSdrh if( db->mTrace & SQLITE_TRACE_PROFILE ){ 74*fca760c8Sdrh db->xTrace(SQLITE_TRACE_PROFILE, db->pTraceArg, p, (void*)&iElapse); 753d2a529dSdrh } 76201e0c68Sdrh p->startTime = 0; 77201e0c68Sdrh } 78201e0c68Sdrh /* 79201e0c68Sdrh ** The checkProfileCallback(DB,P) macro checks to see if a profile callback 80201e0c68Sdrh ** is needed, and it invokes the callback if it is needed. 81201e0c68Sdrh */ 82201e0c68Sdrh # define checkProfileCallback(DB,P) \ 83201e0c68Sdrh if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); } 84201e0c68Sdrh #else 85201e0c68Sdrh # define checkProfileCallback(DB,P) /*no-op*/ 86201e0c68Sdrh #endif 87201e0c68Sdrh 88413c3d36Sdrh /* 89e30f4426Sdrh ** The following routine destroys a virtual machine that is created by 90e30f4426Sdrh ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 91e30f4426Sdrh ** success/failure code that describes the result of executing the virtual 92e30f4426Sdrh ** machine. 93e30f4426Sdrh ** 94e30f4426Sdrh ** This routine sets the error code and string returned by 95e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 96e30f4426Sdrh */ 97e30f4426Sdrh int sqlite3_finalize(sqlite3_stmt *pStmt){ 98e30f4426Sdrh int rc; 99e30f4426Sdrh if( pStmt==0 ){ 10065bafa65Sdrh /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL 10165bafa65Sdrh ** pointer is a harmless no-op. */ 102e30f4426Sdrh rc = SQLITE_OK; 103e30f4426Sdrh }else{ 104e30f4426Sdrh Vdbe *v = (Vdbe*)pStmt; 105238746a6Sdanielk1977 sqlite3 *db = v->db; 106413c3d36Sdrh if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 1074245c405Sdrh sqlite3_mutex_enter(db->mutex); 108201e0c68Sdrh checkProfileCallback(db, v); 109e30f4426Sdrh rc = sqlite3VdbeFinalize(v); 110238746a6Sdanielk1977 rc = sqlite3ApiExit(db, rc); 1114245c405Sdrh sqlite3LeaveMutexAndCloseZombie(db); 112e30f4426Sdrh } 113e30f4426Sdrh return rc; 114e30f4426Sdrh } 115e30f4426Sdrh 116e30f4426Sdrh /* 117e30f4426Sdrh ** Terminate the current execution of an SQL statement and reset it 118e30f4426Sdrh ** back to its starting state so that it can be reused. A success code from 119e30f4426Sdrh ** the prior execution is returned. 120e30f4426Sdrh ** 121e30f4426Sdrh ** This routine sets the error code and string returned by 122e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 123e30f4426Sdrh */ 124e30f4426Sdrh int sqlite3_reset(sqlite3_stmt *pStmt){ 125e30f4426Sdrh int rc; 126e30f4426Sdrh if( pStmt==0 ){ 127e30f4426Sdrh rc = SQLITE_OK; 128e30f4426Sdrh }else{ 129e30f4426Sdrh Vdbe *v = (Vdbe*)pStmt; 130201e0c68Sdrh sqlite3 *db = v->db; 131201e0c68Sdrh sqlite3_mutex_enter(db->mutex); 132201e0c68Sdrh checkProfileCallback(db, v); 133c890fec3Sdrh rc = sqlite3VdbeReset(v); 134124c0b49Sdrh sqlite3VdbeRewind(v); 135201e0c68Sdrh assert( (rc & (db->errMask))==rc ); 136201e0c68Sdrh rc = sqlite3ApiExit(db, rc); 137201e0c68Sdrh sqlite3_mutex_leave(db->mutex); 138e30f4426Sdrh } 139e30f4426Sdrh return rc; 140e30f4426Sdrh } 141e30f4426Sdrh 142e30f4426Sdrh /* 143e30f4426Sdrh ** Set all the parameters in the compiled SQL statement to NULL. 144e30f4426Sdrh */ 145e30f4426Sdrh int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 146e30f4426Sdrh int i; 147e30f4426Sdrh int rc = SQLITE_OK; 1487297d1f0Sdrh Vdbe *p = (Vdbe*)pStmt; 14918472fa7Sdrh #if SQLITE_THREADSAFE 1507e8b848aSdrh sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 1517e8b848aSdrh #endif 1527e8b848aSdrh sqlite3_mutex_enter(mutex); 1537297d1f0Sdrh for(i=0; i<p->nVar; i++){ 1547297d1f0Sdrh sqlite3VdbeMemRelease(&p->aVar[i]); 1557297d1f0Sdrh p->aVar[i].flags = MEM_Null; 156e30f4426Sdrh } 157c94b8595Sdan if( p->isPrepareV2 && p->expmask ){ 158c94b8595Sdan p->expired = 1; 159c94b8595Sdan } 1607e8b848aSdrh sqlite3_mutex_leave(mutex); 161e30f4426Sdrh return rc; 162e30f4426Sdrh } 163e30f4426Sdrh 164e30f4426Sdrh 1654f26d6c4Sdrh /**************************** sqlite3_value_ ******************************* 1664f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value 1674f26d6c4Sdrh ** structure. 1684f26d6c4Sdrh */ 1694f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){ 1704f26d6c4Sdrh Mem *p = (Mem*)pVal; 1714f26d6c4Sdrh if( p->flags & (MEM_Blob|MEM_Str) ){ 172a4d5ae8fSdan if( sqlite3VdbeMemExpandBlob(p)!=SQLITE_OK ){ 173a4d5ae8fSdan assert( p->flags==MEM_Null && p->z==0 ); 174a4d5ae8fSdan return 0; 175a4d5ae8fSdan } 1761f0feef8Sdrh p->flags |= MEM_Blob; 17742262536Sdrh return p->n ? p->z : 0; 1784f26d6c4Sdrh }else{ 1794f26d6c4Sdrh return sqlite3_value_text(pVal); 1804f26d6c4Sdrh } 1814f26d6c4Sdrh } 1824f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 183b21c8cd4Sdrh return sqlite3ValueBytes(pVal, SQLITE_UTF8); 1844f26d6c4Sdrh } 1854f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 186b21c8cd4Sdrh return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 1874f26d6c4Sdrh } 1884f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 1896a6124e2Sdrh return sqlite3VdbeRealValue((Mem*)pVal); 1904f26d6c4Sdrh } 1914f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 192b27b7f5dSdrh return (int)sqlite3VdbeIntValue((Mem*)pVal); 1934f26d6c4Sdrh } 194efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 1956a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 1964f26d6c4Sdrh } 197bcdf78a6Sdrh unsigned int sqlite3_value_subtype(sqlite3_value *pVal){ 1985b6c8e4eSdan Mem *pMem = (Mem*)pVal; 1995b6c8e4eSdan return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0); 200bcdf78a6Sdrh } 2014f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 202b21c8cd4Sdrh return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 2034f26d6c4Sdrh } 2046c62608fSdrh #ifndef SQLITE_OMIT_UTF16 2054f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 206b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 2074f26d6c4Sdrh } 208d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 209b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16BE); 210d8123366Sdanielk1977 } 211d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 212b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16LE); 213d8123366Sdanielk1977 } 2146c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 21522ec1346Sdrh /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five 21622ec1346Sdrh ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating 21722ec1346Sdrh ** point number string BLOB NULL 21822ec1346Sdrh */ 2194f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 2201b27b8c0Sdrh static const u8 aType[] = { 2211b27b8c0Sdrh SQLITE_BLOB, /* 0x00 */ 2221b27b8c0Sdrh SQLITE_NULL, /* 0x01 */ 2231b27b8c0Sdrh SQLITE_TEXT, /* 0x02 */ 2241b27b8c0Sdrh SQLITE_NULL, /* 0x03 */ 2251b27b8c0Sdrh SQLITE_INTEGER, /* 0x04 */ 2261b27b8c0Sdrh SQLITE_NULL, /* 0x05 */ 2271b27b8c0Sdrh SQLITE_INTEGER, /* 0x06 */ 2281b27b8c0Sdrh SQLITE_NULL, /* 0x07 */ 2291b27b8c0Sdrh SQLITE_FLOAT, /* 0x08 */ 2301b27b8c0Sdrh SQLITE_NULL, /* 0x09 */ 2311b27b8c0Sdrh SQLITE_FLOAT, /* 0x0a */ 2321b27b8c0Sdrh SQLITE_NULL, /* 0x0b */ 2331b27b8c0Sdrh SQLITE_INTEGER, /* 0x0c */ 2341b27b8c0Sdrh SQLITE_NULL, /* 0x0d */ 2351b27b8c0Sdrh SQLITE_INTEGER, /* 0x0e */ 2361b27b8c0Sdrh SQLITE_NULL, /* 0x0f */ 2371b27b8c0Sdrh SQLITE_BLOB, /* 0x10 */ 2381b27b8c0Sdrh SQLITE_NULL, /* 0x11 */ 2391b27b8c0Sdrh SQLITE_TEXT, /* 0x12 */ 2401b27b8c0Sdrh SQLITE_NULL, /* 0x13 */ 2411b27b8c0Sdrh SQLITE_INTEGER, /* 0x14 */ 2421b27b8c0Sdrh SQLITE_NULL, /* 0x15 */ 2431b27b8c0Sdrh SQLITE_INTEGER, /* 0x16 */ 2441b27b8c0Sdrh SQLITE_NULL, /* 0x17 */ 2451b27b8c0Sdrh SQLITE_FLOAT, /* 0x18 */ 2461b27b8c0Sdrh SQLITE_NULL, /* 0x19 */ 2471b27b8c0Sdrh SQLITE_FLOAT, /* 0x1a */ 2481b27b8c0Sdrh SQLITE_NULL, /* 0x1b */ 2491b27b8c0Sdrh SQLITE_INTEGER, /* 0x1c */ 2501b27b8c0Sdrh SQLITE_NULL, /* 0x1d */ 2511b27b8c0Sdrh SQLITE_INTEGER, /* 0x1e */ 2521b27b8c0Sdrh SQLITE_NULL, /* 0x1f */ 2531b27b8c0Sdrh }; 254afc14f72Smistachkin return aType[pVal->flags&MEM_AffMask]; 2554f26d6c4Sdrh } 2564f26d6c4Sdrh 2574f03f413Sdrh /* Make a copy of an sqlite3_value object 2584f03f413Sdrh */ 2594f03f413Sdrh sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){ 2604f03f413Sdrh sqlite3_value *pNew; 2614f03f413Sdrh if( pOrig==0 ) return 0; 2624f03f413Sdrh pNew = sqlite3_malloc( sizeof(*pNew) ); 2634f03f413Sdrh if( pNew==0 ) return 0; 2644f03f413Sdrh memset(pNew, 0, sizeof(*pNew)); 2654f03f413Sdrh memcpy(pNew, pOrig, MEMCELLSIZE); 2664f03f413Sdrh pNew->flags &= ~MEM_Dyn; 2674f03f413Sdrh pNew->db = 0; 2684f03f413Sdrh if( pNew->flags&(MEM_Str|MEM_Blob) ){ 26910ca5b48Sdrh pNew->flags &= ~(MEM_Static|MEM_Dyn); 2704f03f413Sdrh pNew->flags |= MEM_Ephem; 2719dfedc82Sdrh if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){ 2729dfedc82Sdrh sqlite3ValueFree(pNew); 2739dfedc82Sdrh pNew = 0; 2749dfedc82Sdrh } 2754f03f413Sdrh } 2764f03f413Sdrh return pNew; 2774f03f413Sdrh } 2784f03f413Sdrh 2794f03f413Sdrh /* Destroy an sqlite3_value object previously obtained from 2804f03f413Sdrh ** sqlite3_value_dup(). 2814f03f413Sdrh */ 2824f03f413Sdrh void sqlite3_value_free(sqlite3_value *pOld){ 2834f03f413Sdrh sqlite3ValueFree(pOld); 2844f03f413Sdrh } 2854f03f413Sdrh 2864f03f413Sdrh 2874f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 2884f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 2894f26d6c4Sdrh ** the function result. 2904c8555fdSdrh ** 29160ec914cSpeter.d.reid ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the 2924c8555fdSdrh ** result as a string or blob but if the string or blob is too large, it 2934c8555fdSdrh ** then sets the error code to SQLITE_TOOBIG 294da4ca9d1Sdrh ** 295da4ca9d1Sdrh ** The invokeValueDestructor(P,X) routine invokes destructor function X() 296da4ca9d1Sdrh ** on value P is not going to be used and need to be destroyed. 2974f26d6c4Sdrh */ 2984c8555fdSdrh static void setResultStrOrError( 2994c8555fdSdrh sqlite3_context *pCtx, /* Function context */ 3004c8555fdSdrh const char *z, /* String pointer */ 3014c8555fdSdrh int n, /* Bytes in string, or negative */ 3024c8555fdSdrh u8 enc, /* Encoding of z. 0 for BLOBs */ 3034c8555fdSdrh void (*xDel)(void*) /* Destructor function */ 3044c8555fdSdrh ){ 3059bd038f1Sdrh if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){ 3064c8555fdSdrh sqlite3_result_error_toobig(pCtx); 3074c8555fdSdrh } 3084c8555fdSdrh } 309da4ca9d1Sdrh static int invokeValueDestructor( 310da4ca9d1Sdrh const void *p, /* Value to destroy */ 311da4ca9d1Sdrh void (*xDel)(void*), /* The destructor */ 312da4ca9d1Sdrh sqlite3_context *pCtx /* Set a SQLITE_TOOBIG error if no NULL */ 313da4ca9d1Sdrh ){ 314fc59a954Sdrh assert( xDel!=SQLITE_DYNAMIC ); 315da4ca9d1Sdrh if( xDel==0 ){ 316da4ca9d1Sdrh /* noop */ 317da4ca9d1Sdrh }else if( xDel==SQLITE_TRANSIENT ){ 318da4ca9d1Sdrh /* noop */ 319da4ca9d1Sdrh }else{ 320da4ca9d1Sdrh xDel((void*)p); 321da4ca9d1Sdrh } 322da4ca9d1Sdrh if( pCtx ) sqlite3_result_error_toobig(pCtx); 323da4ca9d1Sdrh return SQLITE_TOOBIG; 324da4ca9d1Sdrh } 3254f26d6c4Sdrh void sqlite3_result_blob( 3264f26d6c4Sdrh sqlite3_context *pCtx, 3274f26d6c4Sdrh const void *z, 3284f26d6c4Sdrh int n, 329d8123366Sdanielk1977 void (*xDel)(void *) 3304f26d6c4Sdrh ){ 3315708d2deSdrh assert( n>=0 ); 3329bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3334c8555fdSdrh setResultStrOrError(pCtx, z, n, 0, xDel); 3344f26d6c4Sdrh } 335da4ca9d1Sdrh void sqlite3_result_blob64( 336da4ca9d1Sdrh sqlite3_context *pCtx, 337da4ca9d1Sdrh const void *z, 338da4ca9d1Sdrh sqlite3_uint64 n, 339da4ca9d1Sdrh void (*xDel)(void *) 340da4ca9d1Sdrh ){ 341da4ca9d1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 342fc59a954Sdrh assert( xDel!=SQLITE_DYNAMIC ); 343da4ca9d1Sdrh if( n>0x7fffffff ){ 344da4ca9d1Sdrh (void)invokeValueDestructor(z, xDel, pCtx); 345da4ca9d1Sdrh }else{ 346da4ca9d1Sdrh setResultStrOrError(pCtx, z, (int)n, 0, xDel); 347da4ca9d1Sdrh } 348da4ca9d1Sdrh } 3494f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 3509bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3519bd038f1Sdrh sqlite3VdbeMemSetDouble(pCtx->pOut, rVal); 3524f26d6c4Sdrh } 3534f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 3549bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 35569544ec9Sdrh pCtx->isError = SQLITE_ERROR; 3569b47ee3fSdrh pCtx->fErrorOrAux = 1; 3579bd038f1Sdrh sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 3584f26d6c4Sdrh } 359a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16 3604f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 3619bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 36269544ec9Sdrh pCtx->isError = SQLITE_ERROR; 3639b47ee3fSdrh pCtx->fErrorOrAux = 1; 3649bd038f1Sdrh sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 3654f26d6c4Sdrh } 366a1686c9aSdanielk1977 #endif 367f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 3689bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3699bd038f1Sdrh sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal); 3704f26d6c4Sdrh } 3714f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 3729bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3739bd038f1Sdrh sqlite3VdbeMemSetInt64(pCtx->pOut, iVal); 3744f26d6c4Sdrh } 3754f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 3769bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3779bd038f1Sdrh sqlite3VdbeMemSetNull(pCtx->pOut); 3784f26d6c4Sdrh } 379bcdf78a6Sdrh void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){ 3805b6c8e4eSdan Mem *pOut = pCtx->pOut; 3815b6c8e4eSdan assert( sqlite3_mutex_held(pOut->db->mutex) ); 3825b6c8e4eSdan pOut->eSubtype = eSubtype & 0xff; 3835b6c8e4eSdan pOut->flags |= MEM_Subtype; 384bcdf78a6Sdrh } 3854f26d6c4Sdrh void sqlite3_result_text( 3864f26d6c4Sdrh sqlite3_context *pCtx, 3874f26d6c4Sdrh const char *z, 3884f26d6c4Sdrh int n, 389d8123366Sdanielk1977 void (*xDel)(void *) 3904f26d6c4Sdrh ){ 3919bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 3924c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 3934f26d6c4Sdrh } 394bbf483f8Sdrh void sqlite3_result_text64( 395da4ca9d1Sdrh sqlite3_context *pCtx, 396da4ca9d1Sdrh const char *z, 397da4ca9d1Sdrh sqlite3_uint64 n, 398da4ca9d1Sdrh void (*xDel)(void *), 399da4ca9d1Sdrh unsigned char enc 400da4ca9d1Sdrh ){ 401da4ca9d1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 402fc59a954Sdrh assert( xDel!=SQLITE_DYNAMIC ); 40379f7af9aSdrh if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 404da4ca9d1Sdrh if( n>0x7fffffff ){ 405da4ca9d1Sdrh (void)invokeValueDestructor(z, xDel, pCtx); 406da4ca9d1Sdrh }else{ 407da4ca9d1Sdrh setResultStrOrError(pCtx, z, (int)n, enc, xDel); 408da4ca9d1Sdrh } 409da4ca9d1Sdrh } 4106c62608fSdrh #ifndef SQLITE_OMIT_UTF16 4114f26d6c4Sdrh void sqlite3_result_text16( 4124f26d6c4Sdrh sqlite3_context *pCtx, 4134f26d6c4Sdrh const void *z, 4144f26d6c4Sdrh int n, 415d8123366Sdanielk1977 void (*xDel)(void *) 4164f26d6c4Sdrh ){ 4179bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4184c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 419d8123366Sdanielk1977 } 420d8123366Sdanielk1977 void sqlite3_result_text16be( 421d8123366Sdanielk1977 sqlite3_context *pCtx, 422d8123366Sdanielk1977 const void *z, 423d8123366Sdanielk1977 int n, 424d8123366Sdanielk1977 void (*xDel)(void *) 425d8123366Sdanielk1977 ){ 4269bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4274c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 428d8123366Sdanielk1977 } 429d8123366Sdanielk1977 void sqlite3_result_text16le( 430d8123366Sdanielk1977 sqlite3_context *pCtx, 431d8123366Sdanielk1977 const void *z, 432d8123366Sdanielk1977 int n, 433d8123366Sdanielk1977 void (*xDel)(void *) 434d8123366Sdanielk1977 ){ 4359bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4364c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 4374f26d6c4Sdrh } 4386c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 4394f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 4409bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4419bd038f1Sdrh sqlite3VdbeMemCopy(pCtx->pOut, pValue); 4424f26d6c4Sdrh } 443b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 4449bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4459bd038f1Sdrh sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n); 446b026e05eSdrh } 447a4d5ae8fSdan int sqlite3_result_zeroblob64(sqlite3_context *pCtx, u64 n){ 448a4d5ae8fSdan Mem *pOut = pCtx->pOut; 449a4d5ae8fSdan assert( sqlite3_mutex_held(pOut->db->mutex) ); 4506bacdc21Sdrh if( n>(u64)pOut->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 451a4d5ae8fSdan return SQLITE_TOOBIG; 452a4d5ae8fSdan } 453a4d5ae8fSdan sqlite3VdbeMemSetZeroBlob(pCtx->pOut, (int)n); 454a4d5ae8fSdan return SQLITE_OK; 455a4d5ae8fSdan } 45669544ec9Sdrh void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 45769544ec9Sdrh pCtx->isError = errCode; 4589b47ee3fSdrh pCtx->fErrorOrAux = 1; 459983b5ee7Sdrh #ifdef SQLITE_DEBUG 46096f4ad20Sdrh if( pCtx->pVdbe ) pCtx->pVdbe->rcApp = errCode; 461983b5ee7Sdrh #endif 4629bd038f1Sdrh if( pCtx->pOut->flags & MEM_Null ){ 4639bd038f1Sdrh sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 46451c7d864Sdrh SQLITE_UTF8, SQLITE_STATIC); 46551c7d864Sdrh } 46669544ec9Sdrh } 4674f26d6c4Sdrh 468a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */ 469a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 4709bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 471bb4957f8Sdrh pCtx->isError = SQLITE_TOOBIG; 4729b47ee3fSdrh pCtx->fErrorOrAux = 1; 4739bd038f1Sdrh sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 474bb4957f8Sdrh SQLITE_UTF8, SQLITE_STATIC); 475a0206bc8Sdrh } 476a0206bc8Sdrh 477a1644fd8Sdanielk1977 /* An SQLITE_NOMEM error. */ 478a1644fd8Sdanielk1977 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 4799bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 4809bd038f1Sdrh sqlite3VdbeMemSetNull(pCtx->pOut); 481fad3039cSmistachkin pCtx->isError = SQLITE_NOMEM_BKPT; 4829b47ee3fSdrh pCtx->fErrorOrAux = 1; 4834a642b60Sdrh sqlite3OomFault(pCtx->pOut->db); 484a1644fd8Sdanielk1977 } 4854f26d6c4Sdrh 4865cf53537Sdan /* 4875cf53537Sdan ** This function is called after a transaction has been committed. It 4885cf53537Sdan ** invokes callbacks registered with sqlite3_wal_hook() as required. 4895cf53537Sdan */ 4907ed91f23Sdrh static int doWalCallbacks(sqlite3 *db){ 4918d22a174Sdan int rc = SQLITE_OK; 4925cf53537Sdan #ifndef SQLITE_OMIT_WAL 4935cf53537Sdan int i; 4948d22a174Sdan for(i=0; i<db->nDb; i++){ 4958d22a174Sdan Btree *pBt = db->aDb[i].pBt; 4968d22a174Sdan if( pBt ){ 497ef15c6e9Sdrh int nEntry; 498ef15c6e9Sdrh sqlite3BtreeEnter(pBt); 499ef15c6e9Sdrh nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 500ef15c6e9Sdrh sqlite3BtreeLeave(pBt); 5015def0843Sdrh if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 5025def0843Sdrh rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); 5038d22a174Sdan } 5048d22a174Sdan } 5058d22a174Sdan } 5065cf53537Sdan #endif 5078d22a174Sdan return rc; 5088d22a174Sdan } 5098d22a174Sdan 510201e0c68Sdrh 5114f26d6c4Sdrh /* 5124f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 5134f26d6c4Sdrh ** statement is completely executed or an error occurs. 514b900aaf3Sdrh ** 515b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step() 516b900aaf3Sdrh ** API. The only thing omitted is the automatic recompile if a 517b900aaf3Sdrh ** schema change has occurred. That detail is handled by the 518b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure. 5194f26d6c4Sdrh */ 520b900aaf3Sdrh static int sqlite3Step(Vdbe *p){ 5219bb575fdSdrh sqlite3 *db; 5224f26d6c4Sdrh int rc; 5234f26d6c4Sdrh 524a185ddbfSdanielk1977 assert(p); 525a185ddbfSdanielk1977 if( p->magic!=VDBE_MAGIC_RUN ){ 5263674bfd1Sdrh /* We used to require that sqlite3_reset() be called before retrying 527602acb48Sdrh ** sqlite3_step() after any error or after SQLITE_DONE. But beginning 528602acb48Sdrh ** with version 3.7.0, we changed this so that sqlite3_reset() would 529602acb48Sdrh ** be called automatically instead of throwing the SQLITE_MISUSE error. 530602acb48Sdrh ** This "automatic-reset" change is not technically an incompatibility, 531602acb48Sdrh ** since any application that receives an SQLITE_MISUSE is broken by 532602acb48Sdrh ** definition. 533602acb48Sdrh ** 534602acb48Sdrh ** Nevertheless, some published applications that were originally written 535602acb48Sdrh ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE 536b8a45bbdSdrh ** returns, and those were broken by the automatic-reset change. As a 537602acb48Sdrh ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the 538602acb48Sdrh ** legacy behavior of returning SQLITE_MISUSE for cases where the 539602acb48Sdrh ** previous sqlite3_step() returned something other than a SQLITE_LOCKED 540602acb48Sdrh ** or SQLITE_BUSY error. 5413674bfd1Sdrh */ 542602acb48Sdrh #ifdef SQLITE_OMIT_AUTORESET 543983b5ee7Sdrh if( (rc = p->rc&0xff)==SQLITE_BUSY || rc==SQLITE_LOCKED ){ 5443674bfd1Sdrh sqlite3_reset((sqlite3_stmt*)p); 545602acb48Sdrh }else{ 546602acb48Sdrh return SQLITE_MISUSE_BKPT; 547602acb48Sdrh } 548602acb48Sdrh #else 549602acb48Sdrh sqlite3_reset((sqlite3_stmt*)p); 550602acb48Sdrh #endif 551f1bfe9a8Sdrh } 552f1bfe9a8Sdrh 55314d4cc43Sdan /* Check that malloc() has not failed. If it has, return early. */ 55417435752Sdrh db = p->db; 555c456e57aSdrh if( db->mallocFailed ){ 55614d4cc43Sdan p->rc = SQLITE_NOMEM; 557fad3039cSmistachkin return SQLITE_NOMEM_BKPT; 558c456e57aSdrh } 559261919ccSdanielk1977 560a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 561a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 562b900aaf3Sdrh rc = SQLITE_ERROR; 563b900aaf3Sdrh goto end_of_step; 564a21c6b6fSdanielk1977 } 5651d850a72Sdanielk1977 if( p->pc<0 ){ 56615ca1df1Sdrh /* If there are no other statements currently running, then 56715ca1df1Sdrh ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 56815ca1df1Sdrh ** from interrupting a statement that has not yet started. 56915ca1df1Sdrh */ 5704f7d3a5fSdrh if( db->nVdbeActive==0 ){ 57115ca1df1Sdrh db->u1.isInterrupted = 0; 57215ca1df1Sdrh } 57315ca1df1Sdrh 574888e16e7Sdrh assert( db->nVdbeWrite>0 || db->autoCommit==0 575cb3e4b79Sdan || (db->nDeferredCons==0 && db->nDeferredImmCons==0) 576cb3e4b79Sdan ); 5771da40a38Sdan 57819e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 5793d2a529dSdrh if( (db->xProfile || (db->mTrace & SQLITE_TRACE_PROFILE)!=0) 5803d2a529dSdrh && !db->init.busy && p->zSql ){ 581b7e8ea20Sdrh sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 582201e0c68Sdrh }else{ 583201e0c68Sdrh assert( p->startTime==0 ); 58419e2d37fSdrh } 58519e2d37fSdrh #endif 586c16a03b5Sdrh 5874f7d3a5fSdrh db->nVdbeActive++; 5884f7d3a5fSdrh if( p->readOnly==0 ) db->nVdbeWrite++; 5891713afb0Sdrh if( p->bIsReader ) db->nVdbeRead++; 5901d850a72Sdanielk1977 p->pc = 0; 5911d850a72Sdanielk1977 } 592983b5ee7Sdrh #ifdef SQLITE_DEBUG 593983b5ee7Sdrh p->rcApp = SQLITE_OK; 594983b5ee7Sdrh #endif 595b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 5964f26d6c4Sdrh if( p->explain ){ 5974f26d6c4Sdrh rc = sqlite3VdbeList(p); 598b7f9164eSdrh }else 599b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 600b7f9164eSdrh { 6014f7d3a5fSdrh db->nVdbeExec++; 6024f26d6c4Sdrh rc = sqlite3VdbeExec(p); 6034f7d3a5fSdrh db->nVdbeExec--; 6044f26d6c4Sdrh } 6054f26d6c4Sdrh 60619e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 607201e0c68Sdrh /* If the statement completed successfully, invoke the profile callback */ 608201e0c68Sdrh if( rc!=SQLITE_ROW ) checkProfileCallback(db, p); 60919e2d37fSdrh #endif 61019e2d37fSdrh 6118d22a174Sdan if( rc==SQLITE_DONE ){ 6128d22a174Sdan assert( p->rc==SQLITE_OK ); 6137ed91f23Sdrh p->rc = doWalCallbacks(db); 6148d22a174Sdan if( p->rc!=SQLITE_OK ){ 6158d22a174Sdan rc = SQLITE_ERROR; 6168d22a174Sdan } 6178d22a174Sdan } 6188d22a174Sdan 61980cc85b3Sdrh db->errCode = rc; 620357864ecSdanielk1977 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 621fad3039cSmistachkin p->rc = SQLITE_NOMEM_BKPT; 6224f26d6c4Sdrh } 623357864ecSdanielk1977 end_of_step: 624357864ecSdanielk1977 /* At this point local variable rc holds the value that should be 625357864ecSdanielk1977 ** returned if this statement was compiled using the legacy 626357864ecSdanielk1977 ** sqlite3_prepare() interface. According to the docs, this can only 627357864ecSdanielk1977 ** be one of the values in the first assert() below. Variable p->rc 628357864ecSdanielk1977 ** contains the value that would be returned if sqlite3_finalize() 629357864ecSdanielk1977 ** were called on statement p. 630357864ecSdanielk1977 */ 631357864ecSdanielk1977 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 632cbd8db35Sdrh || (rc&0xff)==SQLITE_BUSY || rc==SQLITE_MISUSE 633357864ecSdanielk1977 ); 634983b5ee7Sdrh assert( (p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE) || p->rc==p->rcApp ); 635357864ecSdanielk1977 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 636357864ecSdanielk1977 /* If this statement was prepared using sqlite3_prepare_v2(), and an 63748864df9Smistachkin ** error has occurred, then return the error code in p->rc to the 638357864ecSdanielk1977 ** caller. Set the error code in the database handle to the same value. 639357864ecSdanielk1977 */ 640029ead64Sdan rc = sqlite3VdbeTransferError(p); 641357864ecSdanielk1977 } 642357864ecSdanielk1977 return (rc&db->errMask); 643b900aaf3Sdrh } 644b900aaf3Sdrh 645b900aaf3Sdrh /* 646b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step(). Call 647b900aaf3Sdrh ** sqlite3Step() to do most of the work. If a schema error occurs, 648b900aaf3Sdrh ** call sqlite3Reprepare() and try again. 649b900aaf3Sdrh */ 650b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 651a6129fa7Sdrh int rc = SQLITE_OK; /* Result from sqlite3Step() */ 652a6129fa7Sdrh int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ 653a6129fa7Sdrh Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 654a6129fa7Sdrh int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 655a6129fa7Sdrh sqlite3 *db; /* The database connection */ 656a6129fa7Sdrh 657413c3d36Sdrh if( vdbeSafetyNotNull(v) ){ 658413c3d36Sdrh return SQLITE_MISUSE_BKPT; 659413c3d36Sdrh } 660413c3d36Sdrh db = v->db; 6618e556520Sdanielk1977 sqlite3_mutex_enter(db->mutex); 66237f58e99Sdrh v->doingRerun = 0; 663b900aaf3Sdrh while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 6642c7946a4Sdrh && cnt++ < SQLITE_MAX_SCHEMA_RETRY ){ 6652c7946a4Sdrh int savedPc = v->pc; 6662c7946a4Sdrh rc2 = rc = sqlite3Reprepare(v); 6672c7946a4Sdrh if( rc!=SQLITE_OK) break; 668b900aaf3Sdrh sqlite3_reset(pStmt); 6695f58ae75Sdrh if( savedPc>=0 ) v->doingRerun = 1; 67008f5f4c5Sdan assert( v->expired==0 ); 671b900aaf3Sdrh } 672a3cc007dSdrh if( rc2!=SQLITE_OK ){ 6738e556520Sdanielk1977 /* This case occurs after failing to recompile an sql statement. 6748e556520Sdanielk1977 ** The error message from the SQL compiler has already been loaded 6758e556520Sdanielk1977 ** into the database handle. This block copies the error message 6768e556520Sdanielk1977 ** from the database handle into the statement and sets the statement 6778e556520Sdanielk1977 ** program counter to 0 to ensure that when the statement is 6788e556520Sdanielk1977 ** finalized or reset the parser error message is available via 6798e556520Sdanielk1977 ** sqlite3_errmsg() and sqlite3_errcode(). 6808e556520Sdanielk1977 */ 6818e556520Sdanielk1977 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 682633e6d57Sdrh sqlite3DbFree(db, v->zErrMsg); 6838e556520Sdanielk1977 if( !db->mallocFailed ){ 6848e556520Sdanielk1977 v->zErrMsg = sqlite3DbStrDup(db, zErr); 685a6129fa7Sdrh v->rc = rc2; 6868e556520Sdanielk1977 } else { 6878e556520Sdanielk1977 v->zErrMsg = 0; 688fad3039cSmistachkin v->rc = rc = SQLITE_NOMEM_BKPT; 6898e556520Sdanielk1977 } 6908e556520Sdanielk1977 } 6918e556520Sdanielk1977 rc = sqlite3ApiExit(db, rc); 6928e556520Sdanielk1977 sqlite3_mutex_leave(db->mutex); 693b900aaf3Sdrh return rc; 694b900aaf3Sdrh } 6954f26d6c4Sdrh 69695a7b3e3Sdrh 6974f26d6c4Sdrh /* 698eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 699eb2e176aSdrh ** pointer to it. 700eb2e176aSdrh */ 701eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 702eb2e176aSdrh assert( p && p->pFunc ); 703eb2e176aSdrh return p->pFunc->pUserData; 704eb2e176aSdrh } 705eb2e176aSdrh 706eb2e176aSdrh /* 707fa4a4b91Sdrh ** Extract the user data from a sqlite3_context structure and return a 708fa4a4b91Sdrh ** pointer to it. 7099f129f46Sdrh ** 7109f129f46Sdrh ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface 7119f129f46Sdrh ** returns a copy of the pointer to the database connection (the 1st 7129f129f46Sdrh ** parameter) of the sqlite3_create_function() and 7139f129f46Sdrh ** sqlite3_create_function16() routines that originally registered the 7149f129f46Sdrh ** application defined function. 715fa4a4b91Sdrh */ 716fa4a4b91Sdrh sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 717a46a4a63Sdrh assert( p && p->pOut ); 7189bd038f1Sdrh return p->pOut->db; 719fa4a4b91Sdrh } 720fa4a4b91Sdrh 721fa4a4b91Sdrh /* 722a9e03b1bSdrh ** Return the current time for a statement. If the current time 723a9e03b1bSdrh ** is requested more than once within the same run of a single prepared 724a9e03b1bSdrh ** statement, the exact same time is returned for each invocation regardless 725a9e03b1bSdrh ** of the amount of time that elapses between invocations. In other words, 726a9e03b1bSdrh ** the time returned is always the time of the first call. 72795a7b3e3Sdrh */ 72895a7b3e3Sdrh sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ 72995a7b3e3Sdrh int rc; 730a9e03b1bSdrh #ifndef SQLITE_ENABLE_STAT3_OR_STAT4 7313df79a9aSdrh sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime; 732a9e03b1bSdrh assert( p->pVdbe!=0 ); 733a9e03b1bSdrh #else 7343df79a9aSdrh sqlite3_int64 iTime = 0; 735a9e03b1bSdrh sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime; 736a9e03b1bSdrh #endif 7373df79a9aSdrh if( *piTime==0 ){ 738a9e03b1bSdrh rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime); 739a9e03b1bSdrh if( rc ) *piTime = 0; 74095a7b3e3Sdrh } 741a9e03b1bSdrh return *piTime; 74295a7b3e3Sdrh } 74395a7b3e3Sdrh 74495a7b3e3Sdrh /* 745b7481e70Sdrh ** The following is the implementation of an SQL function that always 746b7481e70Sdrh ** fails with an error message stating that the function is used in the 747b7481e70Sdrh ** wrong context. The sqlite3_overload_function() API might construct 748b7481e70Sdrh ** SQL function that use this routine so that the functions will exist 749b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction 750b7481e70Sdrh ** method of virtual tables. 751b7481e70Sdrh */ 752b7481e70Sdrh void sqlite3InvalidFunction( 753b7481e70Sdrh sqlite3_context *context, /* The function calling context */ 75462c14b34Sdanielk1977 int NotUsed, /* Number of arguments to the function */ 75562c14b34Sdanielk1977 sqlite3_value **NotUsed2 /* Value of each argument */ 756b7481e70Sdrh ){ 757b7481e70Sdrh const char *zName = context->pFunc->zName; 758b7481e70Sdrh char *zErr; 75962c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 760bc6160b0Sdrh zErr = sqlite3_mprintf( 761b7481e70Sdrh "unable to use function %s in the requested context", zName); 762b7481e70Sdrh sqlite3_result_error(context, zErr, -1); 7631e536953Sdanielk1977 sqlite3_free(zErr); 764b7481e70Sdrh } 765b7481e70Sdrh 766b7481e70Sdrh /* 7679de4a171Sdrh ** Create a new aggregate context for p and return a pointer to 7689de4a171Sdrh ** its pMem->z element. 769eb2e176aSdrh */ 7709de4a171Sdrh static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){ 7719de4a171Sdrh Mem *pMem = p->pMem; 7729de4a171Sdrh assert( (pMem->flags & MEM_Agg)==0 ); 773d68eee04Sdrh if( nByte<=0 ){ 7740725cabeSdrh sqlite3VdbeMemSetNull(pMem); 775a10a34b8Sdrh pMem->z = 0; 776a10a34b8Sdrh }else{ 777322f2852Sdrh sqlite3VdbeMemClearAndResize(pMem, nByte); 778abfcea25Sdrh pMem->flags = MEM_Agg; 7793c024d69Sdrh pMem->u.pDef = p->pFunc; 7805f096135Sdanielk1977 if( pMem->z ){ 7815f096135Sdanielk1977 memset(pMem->z, 0, nByte); 7825f096135Sdanielk1977 } 783eb2e176aSdrh } 784abfcea25Sdrh return (void*)pMem->z; 785eb2e176aSdrh } 786eb2e176aSdrh 787eb2e176aSdrh /* 7889de4a171Sdrh ** Allocate or return the aggregate context for a user function. A new 7899de4a171Sdrh ** context is allocated on the first call. Subsequent calls return the 7909de4a171Sdrh ** same context that was returned on prior calls. 7919de4a171Sdrh */ 7929de4a171Sdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 7932d80151fSdrh assert( p && p->pFunc && p->pFunc->xFinalize ); 7949bd038f1Sdrh assert( sqlite3_mutex_held(p->pOut->db->mutex) ); 7959de4a171Sdrh testcase( nByte<0 ); 7969de4a171Sdrh if( (p->pMem->flags & MEM_Agg)==0 ){ 7979de4a171Sdrh return createAggContext(p, nByte); 7989de4a171Sdrh }else{ 7999de4a171Sdrh return (void*)p->pMem->z; 8009de4a171Sdrh } 8019de4a171Sdrh } 8029de4a171Sdrh 8039de4a171Sdrh /* 80460ec914cSpeter.d.reid ** Return the auxiliary data pointer, if any, for the iArg'th argument to 805682f68b0Sdanielk1977 ** the user-function defined by pCtx. 806682f68b0Sdanielk1977 */ 807682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 8080c547799Sdan AuxData *pAuxData; 809b21c8cd4Sdrh 8109bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 811a9e03b1bSdrh #if SQLITE_ENABLE_STAT3_OR_STAT4 81218bf8076Sdan if( pCtx->pVdbe==0 ) return 0; 813a9e03b1bSdrh #else 814a9e03b1bSdrh assert( pCtx->pVdbe!=0 ); 815a9e03b1bSdrh #endif 8160c547799Sdan for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 8170c547799Sdan if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 818682f68b0Sdanielk1977 } 8190c547799Sdan 8200c547799Sdan return (pAuxData ? pAuxData->pAux : 0); 821682f68b0Sdanielk1977 } 822682f68b0Sdanielk1977 823682f68b0Sdanielk1977 /* 82460ec914cSpeter.d.reid ** Set the auxiliary data pointer and delete function, for the iArg'th 825682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 826682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 827682f68b0Sdanielk1977 */ 828682f68b0Sdanielk1977 void sqlite3_set_auxdata( 829682f68b0Sdanielk1977 sqlite3_context *pCtx, 830682f68b0Sdanielk1977 int iArg, 831682f68b0Sdanielk1977 void *pAux, 832682f68b0Sdanielk1977 void (*xDelete)(void*) 833682f68b0Sdanielk1977 ){ 8340c547799Sdan AuxData *pAuxData; 8350c547799Sdan Vdbe *pVdbe = pCtx->pVdbe; 836682f68b0Sdanielk1977 8379bd038f1Sdrh assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) ); 8380c547799Sdan if( iArg<0 ) goto failed; 839a9e03b1bSdrh #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 84018bf8076Sdan if( pVdbe==0 ) goto failed; 841a9e03b1bSdrh #else 842a9e03b1bSdrh assert( pVdbe!=0 ); 843a9e03b1bSdrh #endif 844682f68b0Sdanielk1977 8450c547799Sdan for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){ 8460c547799Sdan if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break; 8470c547799Sdan } 8480c547799Sdan if( pAuxData==0 ){ 8490c547799Sdan pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData)); 8500c547799Sdan if( !pAuxData ) goto failed; 8510c547799Sdan pAuxData->iOp = pCtx->iOp; 8520c547799Sdan pAuxData->iArg = iArg; 8530c547799Sdan pAuxData->pNext = pVdbe->pAuxData; 8540c547799Sdan pVdbe->pAuxData = pAuxData; 8559b47ee3fSdrh if( pCtx->fErrorOrAux==0 ){ 8569b47ee3fSdrh pCtx->isError = 0; 8579b47ee3fSdrh pCtx->fErrorOrAux = 1; 8589b47ee3fSdrh } 8590c547799Sdan }else if( pAuxData->xDelete ){ 860682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 861682f68b0Sdanielk1977 } 8620c547799Sdan 863682f68b0Sdanielk1977 pAuxData->pAux = pAux; 864682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 865e0fc5261Sdanielk1977 return; 866e0fc5261Sdanielk1977 867e0fc5261Sdanielk1977 failed: 868e0fc5261Sdanielk1977 if( xDelete ){ 869e0fc5261Sdanielk1977 xDelete(pAux); 870e0fc5261Sdanielk1977 } 871682f68b0Sdanielk1977 } 872682f68b0Sdanielk1977 873eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 874682f68b0Sdanielk1977 /* 87560ec914cSpeter.d.reid ** Return the number of times the Step function of an aggregate has been 876eb2e176aSdrh ** called. 877eb2e176aSdrh ** 878cf85a51cSdrh ** This function is deprecated. Do not use it for new code. It is 879cf85a51cSdrh ** provide only to avoid breaking legacy code. New aggregate function 880cf85a51cSdrh ** implementations should keep their own counts within their aggregate 881cf85a51cSdrh ** context. 882eb2e176aSdrh */ 883eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 8842d80151fSdrh assert( p && p->pMem && p->pFunc && p->pFunc->xFinalize ); 885abfcea25Sdrh return p->pMem->n; 886eb2e176aSdrh } 887eec556d3Sshane #endif 888eb2e176aSdrh 889eb2e176aSdrh /* 8904f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 8914f26d6c4Sdrh */ 8924f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 8934f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 8941bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 8954f26d6c4Sdrh } 8964f26d6c4Sdrh 8974f26d6c4Sdrh /* 8984f26d6c4Sdrh ** Return the number of values available from the current row of the 8994f26d6c4Sdrh ** currently executing statement pStmt. 9004f26d6c4Sdrh */ 9014f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 9024f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 903d4e70ebdSdrh if( pVm==0 || pVm->pResultSet==0 ) return 0; 9044f26d6c4Sdrh return pVm->nResColumn; 9054f26d6c4Sdrh } 9064f26d6c4Sdrh 90746c47d46Sdan /* 90846c47d46Sdan ** Return a pointer to static memory containing an SQL NULL value. 90946c47d46Sdan */ 91046c47d46Sdan static const Mem *columnNullValue(void){ 91146c47d46Sdan /* Even though the Mem structure contains an element 912b8a45bbdSdrh ** of type i64, on certain architectures (x86) with certain compiler 91346c47d46Sdan ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 91446c47d46Sdan ** instead of an 8-byte one. This all works fine, except that when 91546c47d46Sdan ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 91646c47d46Sdan ** that a Mem structure is located on an 8-byte boundary. To prevent 917b8a45bbdSdrh ** these assert()s from failing, when building with SQLITE_DEBUG defined 918b8a45bbdSdrh ** using gcc, we force nullMem to be 8-byte aligned using the magical 91946c47d46Sdan ** __attribute__((aligned(8))) macro. */ 92046c47d46Sdan static const Mem nullMem 92146c47d46Sdan #if defined(SQLITE_DEBUG) && defined(__GNUC__) 92246c47d46Sdan __attribute__((aligned(8))) 92346c47d46Sdan #endif 924035e563bSdrh = { 9253329a63aSdrh /* .u = */ {0}, 9268faee877Sdrh /* .flags = */ (u16)MEM_Null, 9278faee877Sdrh /* .enc = */ (u8)0, 9288faee877Sdrh /* .eSubtype = */ (u8)0, 9298faee877Sdrh /* .n = */ (int)0, 9308faee877Sdrh /* .z = */ (char*)0, 9318faee877Sdrh /* .zMalloc = */ (char*)0, 9328faee877Sdrh /* .szMalloc = */ (int)0, 9338faee877Sdrh /* .uTemp = */ (u32)0, 9348faee877Sdrh /* .db = */ (sqlite3*)0, 9358faee877Sdrh /* .xDel = */ (void(*)(void*))0, 936fcd71b60Sdrh #ifdef SQLITE_DEBUG 9378faee877Sdrh /* .pScopyFrom = */ (Mem*)0, 9388faee877Sdrh /* .pFiller = */ (void*)0, 939fcd71b60Sdrh #endif 940035e563bSdrh }; 94146c47d46Sdan return &nullMem; 94246c47d46Sdan } 9434f26d6c4Sdrh 9444f26d6c4Sdrh /* 9454f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 9464f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 9474f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 9484f26d6c4Sdrh ** of NULL. 9494f26d6c4Sdrh */ 9504f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 95132bc3f6eSdrh Vdbe *pVm; 95232bc3f6eSdrh Mem *pOut; 95332bc3f6eSdrh 95432bc3f6eSdrh pVm = (Vdbe *)pStmt; 955d4e70ebdSdrh if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 95632bc3f6eSdrh sqlite3_mutex_enter(pVm->db->mutex); 957d4e70ebdSdrh pOut = &pVm->pResultSet[i]; 95832bc3f6eSdrh }else{ 9599b4ea4a5Sdrh if( pVm && ALWAYS(pVm->db) ){ 96027641703Sdrh sqlite3_mutex_enter(pVm->db->mutex); 96113f40da3Sdrh sqlite3Error(pVm->db, SQLITE_RANGE); 96227641703Sdrh } 96346c47d46Sdan pOut = (Mem*)columnNullValue(); 9644f26d6c4Sdrh } 96532bc3f6eSdrh return pOut; 9664f26d6c4Sdrh } 9674f26d6c4Sdrh 9682e588c75Sdanielk1977 /* 9692e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 9702e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 9712e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 9722e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 9732e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 9742e588c75Sdanielk1977 ** 97532bc3f6eSdrh ** Specifically, this is called from within: 9762e588c75Sdanielk1977 ** 9772e588c75Sdanielk1977 ** sqlite3_column_int() 9782e588c75Sdanielk1977 ** sqlite3_column_int64() 9792e588c75Sdanielk1977 ** sqlite3_column_text() 9802e588c75Sdanielk1977 ** sqlite3_column_text16() 9812e588c75Sdanielk1977 ** sqlite3_column_real() 9822e588c75Sdanielk1977 ** sqlite3_column_bytes() 9832e588c75Sdanielk1977 ** sqlite3_column_bytes16() 98442262536Sdrh ** sqiite3_column_blob() 9852e588c75Sdanielk1977 */ 9862e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 9872e588c75Sdanielk1977 { 9882e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 9892e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 9902e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 9912e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 9922e588c75Sdanielk1977 */ 99354f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 99432bc3f6eSdrh if( p ){ 99532bc3f6eSdrh p->rc = sqlite3ApiExit(p->db, p->rc); 99632bc3f6eSdrh sqlite3_mutex_leave(p->db->mutex); 99732bc3f6eSdrh } 9982e588c75Sdanielk1977 } 9992e588c75Sdanielk1977 10004f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 10014f26d6c4Sdrh ** The following routines are used to access elements of the current row 10024f26d6c4Sdrh ** in the result set. 10034f26d6c4Sdrh */ 1004c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 10052e588c75Sdanielk1977 const void *val; 10062e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 1007c9cf901dSdanielk1977 /* Even though there is no encoding conversion, value_blob() might 1008c9cf901dSdanielk1977 ** need to call malloc() to expand the result of a zeroblob() 1009c9cf901dSdanielk1977 ** expression. 1010c9cf901dSdanielk1977 */ 1011c9cf901dSdanielk1977 columnMallocFailure(pStmt); 10122e588c75Sdanielk1977 return val; 1013c572ef7fSdanielk1977 } 10144f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 10152e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 10162e588c75Sdanielk1977 columnMallocFailure(pStmt); 10172e588c75Sdanielk1977 return val; 10184f26d6c4Sdrh } 10194f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 10202e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 10212e588c75Sdanielk1977 columnMallocFailure(pStmt); 10222e588c75Sdanielk1977 return val; 10234f26d6c4Sdrh } 10244f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 10252e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 10262e588c75Sdanielk1977 columnMallocFailure(pStmt); 10272e588c75Sdanielk1977 return val; 10284f26d6c4Sdrh } 10294f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 10302e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 10312e588c75Sdanielk1977 columnMallocFailure(pStmt); 10322e588c75Sdanielk1977 return val; 10334f26d6c4Sdrh } 1034efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 10352e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 10362e588c75Sdanielk1977 columnMallocFailure(pStmt); 10372e588c75Sdanielk1977 return val; 10384f26d6c4Sdrh } 10394f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 10402e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 10412e588c75Sdanielk1977 columnMallocFailure(pStmt); 10422e588c75Sdanielk1977 return val; 10434f26d6c4Sdrh } 1044d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 1045d0ffa1e8Sdanielk1977 Mem *pOut = columnMem(pStmt, i); 1046d0ffa1e8Sdanielk1977 if( pOut->flags&MEM_Static ){ 1047d0ffa1e8Sdanielk1977 pOut->flags &= ~MEM_Static; 1048d0ffa1e8Sdanielk1977 pOut->flags |= MEM_Ephem; 1049d0ffa1e8Sdanielk1977 } 105032bc3f6eSdrh columnMallocFailure(pStmt); 1051d0ffa1e8Sdanielk1977 return (sqlite3_value *)pOut; 1052d1e4733dSdrh } 10536c62608fSdrh #ifndef SQLITE_OMIT_UTF16 10544f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 10552e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 10562e588c75Sdanielk1977 columnMallocFailure(pStmt); 10572e588c75Sdanielk1977 return val; 10584f26d6c4Sdrh } 10596c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 10604f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 106132bc3f6eSdrh int iType = sqlite3_value_type( columnMem(pStmt,i) ); 106232bc3f6eSdrh columnMallocFailure(pStmt); 106332bc3f6eSdrh return iType; 10644f26d6c4Sdrh } 10654f26d6c4Sdrh 10665e2517e1Sdrh /* 10675e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 10685e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 1069e590fbdeSdrh ** 1070e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 1071e590fbdeSdrh ** name is returned. Here are the names: 1072e590fbdeSdrh ** 1073e590fbdeSdrh ** 0 The column name as it should be displayed for output 1074e590fbdeSdrh ** 1 The datatype name for the column 1075e590fbdeSdrh ** 2 The name of the database that the column derives from 1076e590fbdeSdrh ** 3 The name of the table that the column derives from 1077e590fbdeSdrh ** 4 The name of the table column that the result column derives from 1078e590fbdeSdrh ** 1079e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 1080e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 10815e2517e1Sdrh */ 10825e2517e1Sdrh static const void *columnName( 10835e2517e1Sdrh sqlite3_stmt *pStmt, 10845e2517e1Sdrh int N, 10855e2517e1Sdrh const void *(*xFunc)(Mem*), 10865e2517e1Sdrh int useType 10875e2517e1Sdrh ){ 10889ca95730Sdrh const void *ret; 10899ca95730Sdrh Vdbe *p; 109032bc3f6eSdrh int n; 10919ca95730Sdrh sqlite3 *db; 10929ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 10939ca95730Sdrh if( pStmt==0 ){ 10949ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 10959ca95730Sdrh return 0; 10969ca95730Sdrh } 10979ca95730Sdrh #endif 10989ca95730Sdrh ret = 0; 10999ca95730Sdrh p = (Vdbe *)pStmt; 11009ca95730Sdrh db = p->db; 1101c6c7fd51Sdrh assert( db!=0 ); 110232bc3f6eSdrh n = sqlite3_column_count(pStmt); 110332bc3f6eSdrh if( N<n && N>=0 ){ 1104e590fbdeSdrh N += useType*n; 1105c6c7fd51Sdrh sqlite3_mutex_enter(db->mutex); 1106c6c7fd51Sdrh assert( db->mallocFailed==0 ); 110700fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 110832bc3f6eSdrh /* A malloc may have failed inside of the xFunc() call. If this 110932bc3f6eSdrh ** is the case, clear the mallocFailed flag and return NULL. 111000fd957bSdanielk1977 */ 1111c6c7fd51Sdrh if( db->mallocFailed ){ 11124a642b60Sdrh sqlite3OomClear(db); 111332bc3f6eSdrh ret = 0; 111432bc3f6eSdrh } 1115c6c7fd51Sdrh sqlite3_mutex_leave(db->mutex); 111632bc3f6eSdrh } 111700fd957bSdanielk1977 return ret; 11185e2517e1Sdrh } 11195e2517e1Sdrh 11204f26d6c4Sdrh /* 11214f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 11224f26d6c4Sdrh ** statement pStmt. 11234f26d6c4Sdrh */ 11244f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 1125955de52cSdanielk1977 return columnName( 1126955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 11274f26d6c4Sdrh } 1128e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 1129e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 1130955de52cSdanielk1977 return columnName( 1131955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 1132e590fbdeSdrh } 1133e590fbdeSdrh #endif 11344f26d6c4Sdrh 11354f26d6c4Sdrh /* 11363f913576Sdrh ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 11373f913576Sdrh ** not define OMIT_DECLTYPE. 11383f913576Sdrh */ 11393f913576Sdrh #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 11403f913576Sdrh # error "Must not define both SQLITE_OMIT_DECLTYPE \ 11413f913576Sdrh and SQLITE_ENABLE_COLUMN_METADATA" 11423f913576Sdrh #endif 11433f913576Sdrh 11443f913576Sdrh #ifndef SQLITE_OMIT_DECLTYPE 11453f913576Sdrh /* 11466c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 1147e590fbdeSdrh ** of the result set of SQL statement pStmt. 11486c62608fSdrh */ 11496c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 1150955de52cSdanielk1977 return columnName( 1151955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 11526c62608fSdrh } 11536c62608fSdrh #ifndef SQLITE_OMIT_UTF16 115476d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 1155955de52cSdanielk1977 return columnName( 1156955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 11574f26d6c4Sdrh } 11586c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 11593f913576Sdrh #endif /* SQLITE_OMIT_DECLTYPE */ 11604f26d6c4Sdrh 11614b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA 1162e590fbdeSdrh /* 1163e590fbdeSdrh ** Return the name of the database from which a result column derives. 1164e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 116560ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column. 1166e590fbdeSdrh */ 1167e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 1168955de52cSdanielk1977 return columnName( 1169955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 1170e590fbdeSdrh } 1171e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 1172e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 1173955de52cSdanielk1977 return columnName( 1174955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 1175e590fbdeSdrh } 1176e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 1177e590fbdeSdrh 1178e590fbdeSdrh /* 1179e590fbdeSdrh ** Return the name of the table from which a result column derives. 1180e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 118160ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column. 1182e590fbdeSdrh */ 1183e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 1184955de52cSdanielk1977 return columnName( 1185955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 1186e590fbdeSdrh } 1187e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 1188e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 1189955de52cSdanielk1977 return columnName( 1190955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 1191e590fbdeSdrh } 1192e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 1193e590fbdeSdrh 1194e590fbdeSdrh /* 1195e590fbdeSdrh ** Return the name of the table column from which a result column derives. 1196e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 119760ec914cSpeter.d.reid ** anything else which is not an unambiguous reference to a database column. 1198e590fbdeSdrh */ 1199e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 1200955de52cSdanielk1977 return columnName( 1201955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 1202e590fbdeSdrh } 1203e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 1204e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 1205955de52cSdanielk1977 return columnName( 1206955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 1207e590fbdeSdrh } 1208e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 12094b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 1210e590fbdeSdrh 1211e590fbdeSdrh 12124f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 12134f26d6c4Sdrh ** 12144f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 12154f26d6c4Sdrh */ 12164f26d6c4Sdrh /* 12174f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 12184f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 12194f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 12204f26d6c4Sdrh ** 1221488e7b63Sdrh ** A successful evaluation of this routine acquires the mutex on p. 1222488e7b63Sdrh ** the mutex is released if any kind of error occurs. 1223488e7b63Sdrh ** 12244f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 12254f26d6c4Sdrh ** value in any case. 12264f26d6c4Sdrh */ 12274f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 12284f26d6c4Sdrh Mem *pVar; 1229413c3d36Sdrh if( vdbeSafetyNotNull(p) ){ 1230413c3d36Sdrh return SQLITE_MISUSE_BKPT; 1231413c3d36Sdrh } 1232488e7b63Sdrh sqlite3_mutex_enter(p->db->mutex); 1233488e7b63Sdrh if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 123413f40da3Sdrh sqlite3Error(p->db, SQLITE_MISUSE); 1235488e7b63Sdrh sqlite3_mutex_leave(p->db->mutex); 1236413c3d36Sdrh sqlite3_log(SQLITE_MISUSE, 1237413c3d36Sdrh "bind on a busy prepared statement: [%s]", p->zSql); 1238413c3d36Sdrh return SQLITE_MISUSE_BKPT; 12394f26d6c4Sdrh } 12404f26d6c4Sdrh if( i<1 || i>p->nVar ){ 124113f40da3Sdrh sqlite3Error(p->db, SQLITE_RANGE); 1242488e7b63Sdrh sqlite3_mutex_leave(p->db->mutex); 12434f26d6c4Sdrh return SQLITE_RANGE; 12444f26d6c4Sdrh } 12454f26d6c4Sdrh i--; 1246290c1948Sdrh pVar = &p->aVar[i]; 1247d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 12484f26d6c4Sdrh pVar->flags = MEM_Null; 124913f40da3Sdrh sqlite3Error(p->db, SQLITE_OK); 1250937d0deaSdan 12511d2ce4f8Sdan /* If the bit corresponding to this variable in Vdbe.expmask is set, then 12521d2ce4f8Sdan ** binding a new value to this variable invalidates the current query plan. 1253a7044007Sdrh ** 1254a7044007Sdrh ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host 1255a7044007Sdrh ** parameter in the WHERE clause might influence the choice of query plan 1256a7044007Sdrh ** for a statement, then the statement will be automatically recompiled, 1257a7044007Sdrh ** as if there had been a schema change, on the first sqlite3_step() call 1258a7044007Sdrh ** following any change to the bindings of that parameter. 12591d2ce4f8Sdan */ 1260823e09abSdrh if( p->isPrepareV2 && 1261823e09abSdrh ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) 1262823e09abSdrh ){ 1263937d0deaSdan p->expired = 1; 1264937d0deaSdan } 12654f26d6c4Sdrh return SQLITE_OK; 12664f26d6c4Sdrh } 12674f26d6c4Sdrh 12684f26d6c4Sdrh /* 12695e2517e1Sdrh ** Bind a text or BLOB value. 12704f26d6c4Sdrh */ 12715e2517e1Sdrh static int bindText( 1272605264d2Sdrh sqlite3_stmt *pStmt, /* The statement to bind against */ 1273605264d2Sdrh int i, /* Index of the parameter to bind */ 1274605264d2Sdrh const void *zData, /* Pointer to the data to be bound */ 1275605264d2Sdrh int nData, /* Number of bytes of data to be bound */ 1276605264d2Sdrh void (*xDel)(void*), /* Destructor for the data */ 1277b27b7f5dSdrh u8 encoding /* Encoding for the data */ 12784f26d6c4Sdrh ){ 12794f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 12804f26d6c4Sdrh Mem *pVar; 12814f26d6c4Sdrh int rc; 12824f26d6c4Sdrh 12834f26d6c4Sdrh rc = vdbeUnbind(p, i); 1284488e7b63Sdrh if( rc==SQLITE_OK ){ 1285488e7b63Sdrh if( zData!=0 ){ 1286290c1948Sdrh pVar = &p->aVar[i-1]; 1287b21c8cd4Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 12885e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 1289b21c8cd4Sdrh rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 12905e2517e1Sdrh } 129113f40da3Sdrh sqlite3Error(p->db, rc); 1292605264d2Sdrh rc = sqlite3ApiExit(p->db, rc); 129327641703Sdrh } 1294605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 129594bb2ba6Sdrh }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){ 12966fec9ee3Sdrh xDel((void*)zData); 1297488e7b63Sdrh } 1298605264d2Sdrh return rc; 12995e2517e1Sdrh } 13005e2517e1Sdrh 13015e2517e1Sdrh 13025e2517e1Sdrh /* 13035e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 13045e2517e1Sdrh */ 13055e2517e1Sdrh int sqlite3_bind_blob( 13065e2517e1Sdrh sqlite3_stmt *pStmt, 13075e2517e1Sdrh int i, 13085e2517e1Sdrh const void *zData, 13095e2517e1Sdrh int nData, 13105e2517e1Sdrh void (*xDel)(void*) 13115e2517e1Sdrh ){ 13125b081d8aSdrh #ifdef SQLITE_ENABLE_API_ARMOR 13135b081d8aSdrh if( nData<0 ) return SQLITE_MISUSE_BKPT; 13145b081d8aSdrh #endif 13155e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 13165e2517e1Sdrh } 1317da4ca9d1Sdrh int sqlite3_bind_blob64( 1318da4ca9d1Sdrh sqlite3_stmt *pStmt, 1319da4ca9d1Sdrh int i, 1320da4ca9d1Sdrh const void *zData, 1321da4ca9d1Sdrh sqlite3_uint64 nData, 1322da4ca9d1Sdrh void (*xDel)(void*) 1323da4ca9d1Sdrh ){ 1324fc59a954Sdrh assert( xDel!=SQLITE_DYNAMIC ); 1325da4ca9d1Sdrh if( nData>0x7fffffff ){ 1326da4ca9d1Sdrh return invokeValueDestructor(zData, xDel, 0); 1327da4ca9d1Sdrh }else{ 13283329a63aSdrh return bindText(pStmt, i, zData, (int)nData, xDel, 0); 1329da4ca9d1Sdrh } 1330da4ca9d1Sdrh } 13314f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 13324f26d6c4Sdrh int rc; 13334f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 13344f26d6c4Sdrh rc = vdbeUnbind(p, i); 13354f26d6c4Sdrh if( rc==SQLITE_OK ){ 1336290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1337605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1338488e7b63Sdrh } 1339f4618891Sdanielk1977 return rc; 13404f26d6c4Sdrh } 13414f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1342efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 13434f26d6c4Sdrh } 1344efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 13454f26d6c4Sdrh int rc; 13464f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 13474f26d6c4Sdrh rc = vdbeUnbind(p, i); 13484f26d6c4Sdrh if( rc==SQLITE_OK ){ 1349290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1350605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1351488e7b63Sdrh } 13524f26d6c4Sdrh return rc; 13534f26d6c4Sdrh } 1354605264d2Sdrh int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1355605264d2Sdrh int rc; 1356605264d2Sdrh Vdbe *p = (Vdbe*)pStmt; 1357605264d2Sdrh rc = vdbeUnbind(p, i); 1358488e7b63Sdrh if( rc==SQLITE_OK ){ 1359605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1360488e7b63Sdrh } 1361605264d2Sdrh return rc; 13624f26d6c4Sdrh } 13634f26d6c4Sdrh int sqlite3_bind_text( 13644f26d6c4Sdrh sqlite3_stmt *pStmt, 13654f26d6c4Sdrh int i, 13664f26d6c4Sdrh const char *zData, 13674f26d6c4Sdrh int nData, 1368d8123366Sdanielk1977 void (*xDel)(void*) 13694f26d6c4Sdrh ){ 13705e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 13714f26d6c4Sdrh } 1372bbf483f8Sdrh int sqlite3_bind_text64( 1373da4ca9d1Sdrh sqlite3_stmt *pStmt, 1374da4ca9d1Sdrh int i, 1375da4ca9d1Sdrh const char *zData, 1376da4ca9d1Sdrh sqlite3_uint64 nData, 1377da4ca9d1Sdrh void (*xDel)(void*), 1378da4ca9d1Sdrh unsigned char enc 1379da4ca9d1Sdrh ){ 1380fc59a954Sdrh assert( xDel!=SQLITE_DYNAMIC ); 1381da4ca9d1Sdrh if( nData>0x7fffffff ){ 1382da4ca9d1Sdrh return invokeValueDestructor(zData, xDel, 0); 1383da4ca9d1Sdrh }else{ 1384fc59a954Sdrh if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE; 13853329a63aSdrh return bindText(pStmt, i, zData, (int)nData, xDel, enc); 1386da4ca9d1Sdrh } 1387da4ca9d1Sdrh } 13886c62608fSdrh #ifndef SQLITE_OMIT_UTF16 13894f26d6c4Sdrh int sqlite3_bind_text16( 13904f26d6c4Sdrh sqlite3_stmt *pStmt, 13914f26d6c4Sdrh int i, 13924f26d6c4Sdrh const void *zData, 13934f26d6c4Sdrh int nData, 1394d8123366Sdanielk1977 void (*xDel)(void*) 13954f26d6c4Sdrh ){ 13965e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 13974f26d6c4Sdrh } 13986c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 139926e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 140026e4144dSdanielk1977 int rc; 14011b27b8c0Sdrh switch( sqlite3_value_type((sqlite3_value*)pValue) ){ 140229def560Sdrh case SQLITE_INTEGER: { 140329def560Sdrh rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 140429def560Sdrh break; 140529def560Sdrh } 140629def560Sdrh case SQLITE_FLOAT: { 140774eaba4dSdrh rc = sqlite3_bind_double(pStmt, i, pValue->u.r); 140829def560Sdrh break; 140929def560Sdrh } 141029def560Sdrh case SQLITE_BLOB: { 141129def560Sdrh if( pValue->flags & MEM_Zero ){ 141229def560Sdrh rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 141329def560Sdrh }else{ 141429def560Sdrh rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 141529def560Sdrh } 141629def560Sdrh break; 141729def560Sdrh } 141829def560Sdrh case SQLITE_TEXT: { 1419ac80db78Sdrh rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1420ac80db78Sdrh pValue->enc); 1421ac80db78Sdrh break; 1422ac80db78Sdrh } 1423ac80db78Sdrh default: { 1424ac80db78Sdrh rc = sqlite3_bind_null(pStmt, i); 142529def560Sdrh break; 142629def560Sdrh } 14270f5ea0b3Sdrh } 142826e4144dSdanielk1977 return rc; 142926e4144dSdanielk1977 } 1430b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1431b026e05eSdrh int rc; 1432b026e05eSdrh Vdbe *p = (Vdbe *)pStmt; 1433b026e05eSdrh rc = vdbeUnbind(p, i); 1434b026e05eSdrh if( rc==SQLITE_OK ){ 1435b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1436605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1437488e7b63Sdrh } 1438b026e05eSdrh return rc; 1439b026e05eSdrh } 144080c03022Sdan int sqlite3_bind_zeroblob64(sqlite3_stmt *pStmt, int i, sqlite3_uint64 n){ 144180c03022Sdan int rc; 144280c03022Sdan Vdbe *p = (Vdbe *)pStmt; 144380c03022Sdan sqlite3_mutex_enter(p->db->mutex); 144480c03022Sdan if( n>(u64)p->db->aLimit[SQLITE_LIMIT_LENGTH] ){ 144580c03022Sdan rc = SQLITE_TOOBIG; 144680c03022Sdan }else{ 144780c03022Sdan assert( (n & 0x7FFFFFFF)==n ); 144880c03022Sdan rc = sqlite3_bind_zeroblob(pStmt, i, n); 144980c03022Sdan } 145080c03022Sdan rc = sqlite3ApiExit(p->db, rc); 145180c03022Sdan sqlite3_mutex_leave(p->db->mutex); 145280c03022Sdan return rc; 145380c03022Sdan } 145475f6a032Sdrh 145575f6a032Sdrh /* 145675f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 145775f6a032Sdrh ** This routine is added to support DBD::SQLite. 145875f6a032Sdrh */ 145975f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 146092febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 146192febd92Sdrh return p ? p->nVar : 0; 146275f6a032Sdrh } 1463895d7472Sdrh 1464895d7472Sdrh /* 1465fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 1466fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 1467fa6bc000Sdrh ** 1468fa6bc000Sdrh ** The result is always UTF-8. 1469fa6bc000Sdrh */ 1470fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1471fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 1472124c0b49Sdrh if( p==0 || i<1 || i>p->nzVar ){ 1473fa6bc000Sdrh return 0; 1474fa6bc000Sdrh } 1475895d7472Sdrh return p->azVar[i-1]; 1476895d7472Sdrh } 1477fa6bc000Sdrh 1478fa6bc000Sdrh /* 1479fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 1480fa6bc000Sdrh ** with that name. If there is no variable with the given name, 1481fa6bc000Sdrh ** return 0. 1482fa6bc000Sdrh */ 14835f18a221Sdrh int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1484fa6bc000Sdrh int i; 1485fa6bc000Sdrh if( p==0 ){ 1486fa6bc000Sdrh return 0; 1487fa6bc000Sdrh } 14886a8903c3Sdrh if( zName ){ 1489124c0b49Sdrh for(i=0; i<p->nzVar; i++){ 1490971a7c87Sdrh const char *z = p->azVar[i]; 1491503a686eSdrh if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){ 1492fa6bc000Sdrh return i+1; 1493fa6bc000Sdrh } 1494fa6bc000Sdrh } 14956a8903c3Sdrh } 1496fa6bc000Sdrh return 0; 1497fa6bc000Sdrh } 14985f18a221Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 14995f18a221Sdrh return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 15005f18a221Sdrh } 1501f8db1bc0Sdrh 150251942bc3Sdrh /* 1503f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 1504f8db1bc0Sdrh */ 1505145834a4Sshane int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1506f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 1507f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 15080f5ea0b3Sdrh int i; 15090f5ea0b3Sdrh assert( pTo->db==pFrom->db ); 15100f5ea0b3Sdrh assert( pTo->nVar==pFrom->nVar ); 151127641703Sdrh sqlite3_mutex_enter(pTo->db->mutex); 15120f5ea0b3Sdrh for(i=0; i<pFrom->nVar; i++){ 1513643167ffSdrh sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1514f8db1bc0Sdrh } 151527641703Sdrh sqlite3_mutex_leave(pTo->db->mutex); 15160f5ea0b3Sdrh return SQLITE_OK; 1517f8db1bc0Sdrh } 151851942bc3Sdrh 1519eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 152051942bc3Sdrh /* 1521145834a4Sshane ** Deprecated external interface. Internal/core SQLite code 1522145834a4Sshane ** should call sqlite3TransferBindings. 15230f5ea0b3Sdrh ** 152460ec914cSpeter.d.reid ** It is misuse to call this routine with statements from different 15250f5ea0b3Sdrh ** database connections. But as this is a deprecated interface, we 15260f5ea0b3Sdrh ** will not bother to check for that condition. 15270f5ea0b3Sdrh ** 15280f5ea0b3Sdrh ** If the two statements contain a different number of bindings, then 15290f5ea0b3Sdrh ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 15300f5ea0b3Sdrh ** SQLITE_OK is returned. 1531145834a4Sshane */ 1532145834a4Sshane int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 15330f5ea0b3Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 15340f5ea0b3Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 15350f5ea0b3Sdrh if( pFrom->nVar!=pTo->nVar ){ 15360f5ea0b3Sdrh return SQLITE_ERROR; 15370f5ea0b3Sdrh } 1538c94b8595Sdan if( pTo->isPrepareV2 && pTo->expmask ){ 1539c94b8595Sdan pTo->expired = 1; 1540c94b8595Sdan } 1541c94b8595Sdan if( pFrom->isPrepareV2 && pFrom->expmask ){ 1542c94b8595Sdan pFrom->expired = 1; 1543c94b8595Sdan } 1544145834a4Sshane return sqlite3TransferBindings(pFromStmt, pToStmt); 1545145834a4Sshane } 1546eec556d3Sshane #endif 1547145834a4Sshane 1548145834a4Sshane /* 154951942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 155051942bc3Sdrh ** in the argument belongs. This is the same database handle that was 155151942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 155251942bc3Sdrh ** the statement in the first place. 155351942bc3Sdrh */ 155451942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 155551942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 155651942bc3Sdrh } 1557bb5a9c3eSdrh 1558bb5a9c3eSdrh /* 1559f03d9cccSdrh ** Return true if the prepared statement is guaranteed to not modify the 1560f03d9cccSdrh ** database. 1561f03d9cccSdrh */ 1562f03d9cccSdrh int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){ 1563f03d9cccSdrh return pStmt ? ((Vdbe*)pStmt)->readOnly : 1; 1564f03d9cccSdrh } 1565f03d9cccSdrh 1566f03d9cccSdrh /* 15672fb6693eSdrh ** Return true if the prepared statement is in need of being reset. 15682fb6693eSdrh */ 15692fb6693eSdrh int sqlite3_stmt_busy(sqlite3_stmt *pStmt){ 15702fb6693eSdrh Vdbe *v = (Vdbe*)pStmt; 1571857745c0Sdan return v!=0 && v->pc>=0 && v->magic==VDBE_MAGIC_RUN; 15722fb6693eSdrh } 15732fb6693eSdrh 15742fb6693eSdrh /* 1575bb5a9c3eSdrh ** Return a pointer to the next prepared statement after pStmt associated 1576bb5a9c3eSdrh ** with database connection pDb. If pStmt is NULL, return the first 1577bb5a9c3eSdrh ** prepared statement for the database connection. Return NULL if there 1578bb5a9c3eSdrh ** are no more. 1579bb5a9c3eSdrh */ 1580bb5a9c3eSdrh sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1581bb5a9c3eSdrh sqlite3_stmt *pNext; 15829ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 15839ca95730Sdrh if( !sqlite3SafetyCheckOk(pDb) ){ 15849ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 15859ca95730Sdrh return 0; 15869ca95730Sdrh } 15879ca95730Sdrh #endif 1588bb5a9c3eSdrh sqlite3_mutex_enter(pDb->mutex); 1589bb5a9c3eSdrh if( pStmt==0 ){ 1590bb5a9c3eSdrh pNext = (sqlite3_stmt*)pDb->pVdbe; 1591bb5a9c3eSdrh }else{ 1592bb5a9c3eSdrh pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1593bb5a9c3eSdrh } 1594bb5a9c3eSdrh sqlite3_mutex_leave(pDb->mutex); 1595bb5a9c3eSdrh return pNext; 1596bb5a9c3eSdrh } 1597d1d38488Sdrh 1598d1d38488Sdrh /* 1599d1d38488Sdrh ** Return the value of a status counter for a prepared statement 1600d1d38488Sdrh */ 1601d1d38488Sdrh int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1602d1d38488Sdrh Vdbe *pVdbe = (Vdbe*)pStmt; 16039ca95730Sdrh u32 v; 16049ca95730Sdrh #ifdef SQLITE_ENABLE_API_ARMOR 16059ca95730Sdrh if( !pStmt ){ 16069ca95730Sdrh (void)SQLITE_MISUSE_BKPT; 16079ca95730Sdrh return 0; 16089ca95730Sdrh } 16099ca95730Sdrh #endif 16109ca95730Sdrh v = pVdbe->aCounter[op]; 16119b47ee3fSdrh if( resetFlag ) pVdbe->aCounter[op] = 0; 16129b47ee3fSdrh return (int)v; 1613d1d38488Sdrh } 161446c47d46Sdan 16159b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 161637db03bfSdan /* 161793bca695Sdan ** Allocate and populate an UnpackedRecord structure based on the serialized 161893bca695Sdan ** record in nKey/pKey. Return a pointer to the new UnpackedRecord structure 161993bca695Sdan ** if successful, or a NULL pointer if an OOM error is encountered. 162093bca695Sdan */ 162193bca695Sdan static UnpackedRecord *vdbeUnpackRecord( 162293bca695Sdan KeyInfo *pKeyInfo, 162393bca695Sdan int nKey, 162493bca695Sdan const void *pKey 162593bca695Sdan ){ 162693bca695Sdan char *dummy; /* Dummy argument for AllocUnpackedRecord() */ 162793bca695Sdan UnpackedRecord *pRet; /* Return value */ 162893bca695Sdan 162993bca695Sdan pRet = sqlite3VdbeAllocUnpackedRecord(pKeyInfo, 0, 0, &dummy); 163093bca695Sdan if( pRet ){ 1631c8d985e0Sdrh memset(pRet->aMem, 0, sizeof(Mem)*(pKeyInfo->nField+1)); 163293bca695Sdan sqlite3VdbeRecordUnpack(pKeyInfo, nKey, pKey, pRet); 163393bca695Sdan } 163493bca695Sdan return pRet; 163593bca695Sdan } 163693bca695Sdan 163793bca695Sdan /* 163837db03bfSdan ** This function is called from within a pre-update callback to retrieve 163937db03bfSdan ** a field of the row currently being updated or deleted. 164037db03bfSdan */ 164146c47d46Sdan int sqlite3_preupdate_old(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 164246c47d46Sdan PreUpdate *p = db->pPreUpdate; 164346c47d46Sdan int rc = SQLITE_OK; 164446c47d46Sdan 164537db03bfSdan /* Test that this call is being made from within an SQLITE_DELETE or 164637db03bfSdan ** SQLITE_UPDATE pre-update callback, and that iIdx is within range. */ 164746c47d46Sdan if( !p || p->op==SQLITE_INSERT ){ 164846c47d46Sdan rc = SQLITE_MISUSE_BKPT; 164946c47d46Sdan goto preupdate_old_out; 165046c47d46Sdan } 165146c47d46Sdan if( iIdx>=p->pCsr->nField || iIdx<0 ){ 165246c47d46Sdan rc = SQLITE_RANGE; 165346c47d46Sdan goto preupdate_old_out; 165446c47d46Sdan } 165546c47d46Sdan 165637db03bfSdan /* If the old.* record has not yet been loaded into memory, do so now. */ 165746c47d46Sdan if( p->pUnpacked==0 ){ 165812ca0b56Sdan u32 nRec; 165912ca0b56Sdan u8 *aRec; 166046c47d46Sdan 1661a7c90c42Sdrh nRec = sqlite3BtreePayloadSize(p->pCsr->uc.pCursor); 166212ca0b56Sdan aRec = sqlite3DbMallocRaw(db, nRec); 166312ca0b56Sdan if( !aRec ) goto preupdate_old_out; 16641bb15fc9Sdrh rc = sqlite3BtreeData(p->pCsr->uc.pCursor, 0, nRec, aRec); 166512ca0b56Sdan if( rc==SQLITE_OK ){ 166693bca695Sdan p->pUnpacked = vdbeUnpackRecord(&p->keyinfo, nRec, aRec); 166712ca0b56Sdan if( !p->pUnpacked ) rc = SQLITE_NOMEM; 166812ca0b56Sdan } 166946c47d46Sdan if( rc!=SQLITE_OK ){ 167012ca0b56Sdan sqlite3DbFree(db, aRec); 167146c47d46Sdan goto preupdate_old_out; 167246c47d46Sdan } 167312ca0b56Sdan p->aRecord = aRec; 167446c47d46Sdan } 167546c47d46Sdan 167646c47d46Sdan if( iIdx>=p->pUnpacked->nField ){ 167746c47d46Sdan *ppValue = (sqlite3_value *)columnNullValue(); 167846c47d46Sdan }else{ 167946c47d46Sdan *ppValue = &p->pUnpacked->aMem[iIdx]; 1680319eeb7bSdan if( iIdx==p->iPKey ){ 1681319eeb7bSdan sqlite3VdbeMemSetInt64(*ppValue, p->iKey1); 1682319eeb7bSdan } 168346c47d46Sdan } 168446c47d46Sdan 168546c47d46Sdan preupdate_old_out: 1686e1ed0b0eSdrh sqlite3Error(db, rc); 168746c47d46Sdan return sqlite3ApiExit(db, rc); 168846c47d46Sdan } 16899b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 169046c47d46Sdan 16919b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 169237db03bfSdan /* 169337db03bfSdan ** This function is called from within a pre-update callback to retrieve 169437db03bfSdan ** the number of columns in the row being updated, deleted or inserted. 169537db03bfSdan */ 169646c47d46Sdan int sqlite3_preupdate_count(sqlite3 *db){ 169746c47d46Sdan PreUpdate *p = db->pPreUpdate; 1698e437ca5eSdan return (p ? p->keyinfo.nField : 0); 169946c47d46Sdan } 17009b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 170146c47d46Sdan 17029b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 170337db03bfSdan /* 17041e7a2d43Sdan ** This function is designed to be called from within a pre-update callback 17051e7a2d43Sdan ** only. It returns zero if the change that caused the callback was made 17061e7a2d43Sdan ** immediately by a user SQL statement. Or, if the change was made by a 17071e7a2d43Sdan ** trigger program, it returns the number of trigger programs currently 17081e7a2d43Sdan ** on the stack (1 for a top-level trigger, 2 for a trigger fired by a 17091e7a2d43Sdan ** top-level trigger etc.). 17101e7a2d43Sdan ** 17111e7a2d43Sdan ** For the purposes of the previous paragraph, a foreign key CASCADE, SET NULL 17121e7a2d43Sdan ** or SET DEFAULT action is considered a trigger. 17131e7a2d43Sdan */ 17141e7a2d43Sdan int sqlite3_preupdate_depth(sqlite3 *db){ 17151e7a2d43Sdan PreUpdate *p = db->pPreUpdate; 17161e7a2d43Sdan return (p ? p->v->nFrame : 0); 17171e7a2d43Sdan } 17189b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 17191e7a2d43Sdan 17209b1c62d4Sdrh #ifdef SQLITE_ENABLE_PREUPDATE_HOOK 17211e7a2d43Sdan /* 172237db03bfSdan ** This function is called from within a pre-update callback to retrieve 172337db03bfSdan ** a field of the row currently being updated or inserted. 172437db03bfSdan */ 172537db03bfSdan int sqlite3_preupdate_new(sqlite3 *db, int iIdx, sqlite3_value **ppValue){ 172646c47d46Sdan PreUpdate *p = db->pPreUpdate; 172746c47d46Sdan int rc = SQLITE_OK; 172837db03bfSdan Mem *pMem; 172946c47d46Sdan 173037db03bfSdan if( !p || p->op==SQLITE_DELETE ){ 173146c47d46Sdan rc = SQLITE_MISUSE_BKPT; 173237db03bfSdan goto preupdate_new_out; 173346c47d46Sdan } 173446c47d46Sdan if( iIdx>=p->pCsr->nField || iIdx<0 ){ 173546c47d46Sdan rc = SQLITE_RANGE; 173637db03bfSdan goto preupdate_new_out; 173746c47d46Sdan } 173846c47d46Sdan 173937db03bfSdan if( p->op==SQLITE_INSERT ){ 174037db03bfSdan /* For an INSERT, memory cell p->iNewReg contains the serialized record 174137db03bfSdan ** that is being inserted. Deserialize it. */ 174237db03bfSdan UnpackedRecord *pUnpack = p->pNewUnpacked; 174337db03bfSdan if( !pUnpack ){ 174437db03bfSdan Mem *pData = &p->v->aMem[p->iNewReg]; 174537db03bfSdan rc = sqlite3VdbeMemExpandBlob(pData); 174637db03bfSdan if( rc!=SQLITE_OK ) goto preupdate_new_out; 174793bca695Sdan pUnpack = vdbeUnpackRecord(&p->keyinfo, pData->n, pData->z); 174837db03bfSdan if( !pUnpack ){ 174937db03bfSdan rc = SQLITE_NOMEM; 175037db03bfSdan goto preupdate_new_out; 175137db03bfSdan } 175237db03bfSdan p->pNewUnpacked = pUnpack; 175337db03bfSdan } 175437db03bfSdan if( iIdx>=pUnpack->nField ){ 175537db03bfSdan pMem = (sqlite3_value *)columnNullValue(); 175637db03bfSdan }else{ 175737db03bfSdan pMem = &pUnpack->aMem[iIdx]; 1758319eeb7bSdan if( iIdx==p->iPKey ){ 1759319eeb7bSdan sqlite3VdbeMemSetInt64(pMem, p->iKey2); 1760319eeb7bSdan } 176137db03bfSdan } 176237db03bfSdan }else{ 176337db03bfSdan /* For an UPDATE, memory cell (p->iNewReg+1+iIdx) contains the required 176437db03bfSdan ** value. Make a copy of the cell contents and return a pointer to it. 176537db03bfSdan ** It is not safe to return a pointer to the memory cell itself as the 176637db03bfSdan ** caller may modify the value text encoding. 176737db03bfSdan */ 176837db03bfSdan assert( p->op==SQLITE_UPDATE ); 176937db03bfSdan if( !p->aNew ){ 177037db03bfSdan p->aNew = (Mem *)sqlite3DbMallocZero(db, sizeof(Mem) * p->pCsr->nField); 177137db03bfSdan if( !p->aNew ){ 177237db03bfSdan rc = SQLITE_NOMEM; 177337db03bfSdan goto preupdate_new_out; 177437db03bfSdan } 177537db03bfSdan } 1776304637c0Sdrh assert( iIdx>=0 && iIdx<p->pCsr->nField ); 177737db03bfSdan pMem = &p->aNew[iIdx]; 177837db03bfSdan if( pMem->flags==0 ){ 1779319eeb7bSdan if( iIdx==p->iPKey ){ 1780319eeb7bSdan sqlite3VdbeMemSetInt64(pMem, p->iKey2); 1781319eeb7bSdan }else{ 178237db03bfSdan rc = sqlite3VdbeMemCopy(pMem, &p->v->aMem[p->iNewReg+1+iIdx]); 178337db03bfSdan if( rc!=SQLITE_OK ) goto preupdate_new_out; 1784319eeb7bSdan } 178537db03bfSdan } 178637db03bfSdan } 178737db03bfSdan *ppValue = pMem; 178837db03bfSdan 178937db03bfSdan preupdate_new_out: 1790e1ed0b0eSdrh sqlite3Error(db, rc); 179146c47d46Sdan return sqlite3ApiExit(db, rc); 179246c47d46Sdan } 17939b1c62d4Sdrh #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */ 179404e8a586Sdrh 17956f9702edSdan #ifdef SQLITE_ENABLE_STMT_SCANSTATUS 179604489b6dSdan /* 179704489b6dSdan ** Return status data for a single loop within query pStmt. 179804489b6dSdan */ 179904489b6dSdan int sqlite3_stmt_scanstatus( 1800d1a1c234Sdrh sqlite3_stmt *pStmt, /* Prepared statement being queried */ 180104489b6dSdan int idx, /* Index of loop to report on */ 1802d1a1c234Sdrh int iScanStatusOp, /* Which metric to return */ 1803d1a1c234Sdrh void *pOut /* OUT: Write the answer here */ 180404489b6dSdan ){ 180504489b6dSdan Vdbe *p = (Vdbe*)pStmt; 1806037b5324Sdan ScanStatus *pScan; 18076f9702edSdan if( idx<0 || idx>=p->nScan ) return 1; 18086f9702edSdan pScan = &p->aScan[idx]; 1809d1a1c234Sdrh switch( iScanStatusOp ){ 1810d1a1c234Sdrh case SQLITE_SCANSTAT_NLOOP: { 1811d1a1c234Sdrh *(sqlite3_int64*)pOut = p->anExec[pScan->addrLoop]; 1812d1a1c234Sdrh break; 1813d1a1c234Sdrh } 1814d1a1c234Sdrh case SQLITE_SCANSTAT_NVISIT: { 1815d1a1c234Sdrh *(sqlite3_int64*)pOut = p->anExec[pScan->addrVisit]; 1816d1a1c234Sdrh break; 1817d1a1c234Sdrh } 1818d1a1c234Sdrh case SQLITE_SCANSTAT_EST: { 1819518140edSdrh double r = 1.0; 1820518140edSdrh LogEst x = pScan->nEst; 1821518140edSdrh while( x<100 ){ 1822518140edSdrh x += 10; 1823518140edSdrh r *= 0.5; 1824518140edSdrh } 1825518140edSdrh *(double*)pOut = r*sqlite3LogEstToInt(x); 1826d1a1c234Sdrh break; 1827d1a1c234Sdrh } 1828d1a1c234Sdrh case SQLITE_SCANSTAT_NAME: { 1829d72219daSdan *(const char**)pOut = pScan->zName; 1830d1a1c234Sdrh break; 1831d1a1c234Sdrh } 1832d1a1c234Sdrh case SQLITE_SCANSTAT_EXPLAIN: { 18336f9702edSdan if( pScan->addrExplain ){ 1834d1a1c234Sdrh *(const char**)pOut = p->aOp[ pScan->addrExplain ].p4.z; 18356f9702edSdan }else{ 1836d1a1c234Sdrh *(const char**)pOut = 0; 1837d1a1c234Sdrh } 1838d1a1c234Sdrh break; 1839d1a1c234Sdrh } 1840c6652b1eSdrh case SQLITE_SCANSTAT_SELECTID: { 1841c6652b1eSdrh if( pScan->addrExplain ){ 1842c6652b1eSdrh *(int*)pOut = p->aOp[ pScan->addrExplain ].p1; 1843c6652b1eSdrh }else{ 1844c6652b1eSdrh *(int*)pOut = -1; 1845c6652b1eSdrh } 1846c6652b1eSdrh break; 1847c6652b1eSdrh } 1848d1a1c234Sdrh default: { 1849d1a1c234Sdrh return 1; 18506f9702edSdan } 18516f9702edSdan } 185204489b6dSdan return 0; 185304489b6dSdan } 185404489b6dSdan 185504489b6dSdan /* 185604489b6dSdan ** Zero all counters associated with the sqlite3_stmt_scanstatus() data. 185704489b6dSdan */ 185804489b6dSdan void sqlite3_stmt_scanstatus_reset(sqlite3_stmt *pStmt){ 185904489b6dSdan Vdbe *p = (Vdbe*)pStmt; 18606f9702edSdan memset(p->anExec, 0, p->nOp * sizeof(i64)); 186104489b6dSdan } 18626f9702edSdan #endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ 1863