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 56413c3d36Sdrh /* 57e30f4426Sdrh ** The following routine destroys a virtual machine that is created by 58e30f4426Sdrh ** the sqlite3_compile() routine. The integer returned is an SQLITE_ 59e30f4426Sdrh ** success/failure code that describes the result of executing the virtual 60e30f4426Sdrh ** machine. 61e30f4426Sdrh ** 62e30f4426Sdrh ** This routine sets the error code and string returned by 63e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 64e30f4426Sdrh */ 65e30f4426Sdrh int sqlite3_finalize(sqlite3_stmt *pStmt){ 66e30f4426Sdrh int rc; 67e30f4426Sdrh if( pStmt==0 ){ 68e30f4426Sdrh rc = SQLITE_OK; 69e30f4426Sdrh }else{ 70e30f4426Sdrh Vdbe *v = (Vdbe*)pStmt; 71238746a6Sdanielk1977 sqlite3 *db = v->db; 720ae0bfd4Sdrh #if SQLITE_THREADSAFE 730ae0bfd4Sdrh sqlite3_mutex *mutex; 740ae0bfd4Sdrh #endif 75413c3d36Sdrh if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT; 7618472fa7Sdrh #if SQLITE_THREADSAFE 770ae0bfd4Sdrh mutex = v->db->mutex; 787e8b848aSdrh #endif 7986f8c197Sdrh sqlite3_mutex_enter(mutex); 80e30f4426Sdrh rc = sqlite3VdbeFinalize(v); 81238746a6Sdanielk1977 rc = sqlite3ApiExit(db, rc); 8286f8c197Sdrh sqlite3_mutex_leave(mutex); 83e30f4426Sdrh } 84e30f4426Sdrh return rc; 85e30f4426Sdrh } 86e30f4426Sdrh 87e30f4426Sdrh /* 88e30f4426Sdrh ** Terminate the current execution of an SQL statement and reset it 89e30f4426Sdrh ** back to its starting state so that it can be reused. A success code from 90e30f4426Sdrh ** the prior execution is returned. 91e30f4426Sdrh ** 92e30f4426Sdrh ** This routine sets the error code and string returned by 93e30f4426Sdrh ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). 94e30f4426Sdrh */ 95e30f4426Sdrh int sqlite3_reset(sqlite3_stmt *pStmt){ 96e30f4426Sdrh int rc; 97e30f4426Sdrh if( pStmt==0 ){ 98e30f4426Sdrh rc = SQLITE_OK; 99e30f4426Sdrh }else{ 100e30f4426Sdrh Vdbe *v = (Vdbe*)pStmt; 101e30f4426Sdrh sqlite3_mutex_enter(v->db->mutex); 102c890fec3Sdrh rc = sqlite3VdbeReset(v); 103e0af83acSdan sqlite3VdbeMakeReady(v, -1, 0, 0, 0, 0, 0); 104e30f4426Sdrh assert( (rc & (v->db->errMask))==rc ); 105238746a6Sdanielk1977 rc = sqlite3ApiExit(v->db, rc); 106e30f4426Sdrh sqlite3_mutex_leave(v->db->mutex); 107e30f4426Sdrh } 108e30f4426Sdrh return rc; 109e30f4426Sdrh } 110e30f4426Sdrh 111e30f4426Sdrh /* 112e30f4426Sdrh ** Set all the parameters in the compiled SQL statement to NULL. 113e30f4426Sdrh */ 114e30f4426Sdrh int sqlite3_clear_bindings(sqlite3_stmt *pStmt){ 115e30f4426Sdrh int i; 116e30f4426Sdrh int rc = SQLITE_OK; 1177297d1f0Sdrh Vdbe *p = (Vdbe*)pStmt; 11818472fa7Sdrh #if SQLITE_THREADSAFE 1197e8b848aSdrh sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex; 1207e8b848aSdrh #endif 1217e8b848aSdrh sqlite3_mutex_enter(mutex); 1227297d1f0Sdrh for(i=0; i<p->nVar; i++){ 1237297d1f0Sdrh sqlite3VdbeMemRelease(&p->aVar[i]); 1247297d1f0Sdrh p->aVar[i].flags = MEM_Null; 125e30f4426Sdrh } 126c94b8595Sdan if( p->isPrepareV2 && p->expmask ){ 127c94b8595Sdan p->expired = 1; 128c94b8595Sdan } 1297e8b848aSdrh sqlite3_mutex_leave(mutex); 130e30f4426Sdrh return rc; 131e30f4426Sdrh } 132e30f4426Sdrh 133e30f4426Sdrh 1344f26d6c4Sdrh /**************************** sqlite3_value_ ******************************* 1354f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value 1364f26d6c4Sdrh ** structure. 1374f26d6c4Sdrh */ 1384f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){ 1394f26d6c4Sdrh Mem *p = (Mem*)pVal; 1404f26d6c4Sdrh if( p->flags & (MEM_Blob|MEM_Str) ){ 141b21c8cd4Sdrh sqlite3VdbeMemExpandBlob(p); 1429310ef23Sdrh p->flags &= ~MEM_Str; 1431f0feef8Sdrh p->flags |= MEM_Blob; 1444f26d6c4Sdrh return p->z; 1454f26d6c4Sdrh }else{ 1464f26d6c4Sdrh return sqlite3_value_text(pVal); 1474f26d6c4Sdrh } 1484f26d6c4Sdrh } 1494f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 150b21c8cd4Sdrh return sqlite3ValueBytes(pVal, SQLITE_UTF8); 1514f26d6c4Sdrh } 1524f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 153b21c8cd4Sdrh return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 1544f26d6c4Sdrh } 1554f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 1566a6124e2Sdrh return sqlite3VdbeRealValue((Mem*)pVal); 1574f26d6c4Sdrh } 1584f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 159b27b7f5dSdrh return (int)sqlite3VdbeIntValue((Mem*)pVal); 1604f26d6c4Sdrh } 161efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 1626a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 1634f26d6c4Sdrh } 1644f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 165b21c8cd4Sdrh return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 1664f26d6c4Sdrh } 1676c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1684f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 169b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 1704f26d6c4Sdrh } 171d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 172b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16BE); 173d8123366Sdanielk1977 } 174d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 175b21c8cd4Sdrh return sqlite3ValueText(pVal, SQLITE_UTF16LE); 176d8123366Sdanielk1977 } 1776c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1784f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 179f4479501Sdrh return pVal->type; 1804f26d6c4Sdrh } 1814f26d6c4Sdrh 1824f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 1834f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 1844f26d6c4Sdrh ** the function result. 1854c8555fdSdrh ** 1864c8555fdSdrh ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the 1874c8555fdSdrh ** result as a string or blob but if the string or blob is too large, it 1884c8555fdSdrh ** then sets the error code to SQLITE_TOOBIG 1894f26d6c4Sdrh */ 1904c8555fdSdrh static void setResultStrOrError( 1914c8555fdSdrh sqlite3_context *pCtx, /* Function context */ 1924c8555fdSdrh const char *z, /* String pointer */ 1934c8555fdSdrh int n, /* Bytes in string, or negative */ 1944c8555fdSdrh u8 enc, /* Encoding of z. 0 for BLOBs */ 1954c8555fdSdrh void (*xDel)(void*) /* Destructor function */ 1964c8555fdSdrh ){ 1974c8555fdSdrh if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){ 1984c8555fdSdrh sqlite3_result_error_toobig(pCtx); 1994c8555fdSdrh } 2004c8555fdSdrh } 2014f26d6c4Sdrh void sqlite3_result_blob( 2024f26d6c4Sdrh sqlite3_context *pCtx, 2034f26d6c4Sdrh const void *z, 2044f26d6c4Sdrh int n, 205d8123366Sdanielk1977 void (*xDel)(void *) 2064f26d6c4Sdrh ){ 2075708d2deSdrh assert( n>=0 ); 20832bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2094c8555fdSdrh setResultStrOrError(pCtx, z, n, 0, xDel); 2104f26d6c4Sdrh } 2114f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 21232bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2134f26d6c4Sdrh sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 2144f26d6c4Sdrh } 2154f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 21632bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 21769544ec9Sdrh pCtx->isError = SQLITE_ERROR; 218b21c8cd4Sdrh sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 2194f26d6c4Sdrh } 220a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16 2214f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 22232bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 22369544ec9Sdrh pCtx->isError = SQLITE_ERROR; 224b21c8cd4Sdrh sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 2254f26d6c4Sdrh } 226a1686c9aSdanielk1977 #endif 227f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 22832bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2294f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 2304f26d6c4Sdrh } 2314f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 23232bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2334f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 2344f26d6c4Sdrh } 2354f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 23632bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 237f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 2384f26d6c4Sdrh } 2394f26d6c4Sdrh void sqlite3_result_text( 2404f26d6c4Sdrh sqlite3_context *pCtx, 2414f26d6c4Sdrh const char *z, 2424f26d6c4Sdrh int n, 243d8123366Sdanielk1977 void (*xDel)(void *) 2444f26d6c4Sdrh ){ 24532bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2464c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel); 2474f26d6c4Sdrh } 2486c62608fSdrh #ifndef SQLITE_OMIT_UTF16 2494f26d6c4Sdrh void sqlite3_result_text16( 2504f26d6c4Sdrh sqlite3_context *pCtx, 2514f26d6c4Sdrh const void *z, 2524f26d6c4Sdrh int n, 253d8123366Sdanielk1977 void (*xDel)(void *) 2544f26d6c4Sdrh ){ 25532bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2564c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel); 257d8123366Sdanielk1977 } 258d8123366Sdanielk1977 void sqlite3_result_text16be( 259d8123366Sdanielk1977 sqlite3_context *pCtx, 260d8123366Sdanielk1977 const void *z, 261d8123366Sdanielk1977 int n, 262d8123366Sdanielk1977 void (*xDel)(void *) 263d8123366Sdanielk1977 ){ 26432bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2654c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel); 266d8123366Sdanielk1977 } 267d8123366Sdanielk1977 void sqlite3_result_text16le( 268d8123366Sdanielk1977 sqlite3_context *pCtx, 269d8123366Sdanielk1977 const void *z, 270d8123366Sdanielk1977 int n, 271d8123366Sdanielk1977 void (*xDel)(void *) 272d8123366Sdanielk1977 ){ 27332bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 2744c8555fdSdrh setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel); 2754f26d6c4Sdrh } 2766c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 2774f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 27832bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 279b21c8cd4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 2804f26d6c4Sdrh } 281b026e05eSdrh void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){ 28232bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 283b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&pCtx->s, n); 284b026e05eSdrh } 28569544ec9Sdrh void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){ 28669544ec9Sdrh pCtx->isError = errCode; 28751c7d864Sdrh if( pCtx->s.flags & MEM_Null ){ 28851c7d864Sdrh sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 28951c7d864Sdrh SQLITE_UTF8, SQLITE_STATIC); 29051c7d864Sdrh } 29169544ec9Sdrh } 2924f26d6c4Sdrh 293a0206bc8Sdrh /* Force an SQLITE_TOOBIG error. */ 294a0206bc8Sdrh void sqlite3_result_error_toobig(sqlite3_context *pCtx){ 29532bc3f6eSdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 296bb4957f8Sdrh pCtx->isError = SQLITE_TOOBIG; 297bb4957f8Sdrh sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 298bb4957f8Sdrh SQLITE_UTF8, SQLITE_STATIC); 299a0206bc8Sdrh } 300a0206bc8Sdrh 301a1644fd8Sdanielk1977 /* An SQLITE_NOMEM error. */ 302a1644fd8Sdanielk1977 void sqlite3_result_error_nomem(sqlite3_context *pCtx){ 303a1644fd8Sdanielk1977 assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 304a1644fd8Sdanielk1977 sqlite3VdbeMemSetNull(&pCtx->s); 30569544ec9Sdrh pCtx->isError = SQLITE_NOMEM; 306a1644fd8Sdanielk1977 pCtx->s.db->mallocFailed = 1; 307a1644fd8Sdanielk1977 } 3084f26d6c4Sdrh 3095cf53537Sdan /* 3105cf53537Sdan ** This function is called after a transaction has been committed. It 3115cf53537Sdan ** invokes callbacks registered with sqlite3_wal_hook() as required. 3125cf53537Sdan */ 3137ed91f23Sdrh static int doWalCallbacks(sqlite3 *db){ 3148d22a174Sdan int rc = SQLITE_OK; 3155cf53537Sdan #ifndef SQLITE_OMIT_WAL 3165cf53537Sdan int i; 3178d22a174Sdan for(i=0; i<db->nDb; i++){ 3188d22a174Sdan Btree *pBt = db->aDb[i].pBt; 3198d22a174Sdan if( pBt ){ 3207ed91f23Sdrh int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt)); 3215def0843Sdrh if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){ 3225def0843Sdrh rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry); 3238d22a174Sdan } 3248d22a174Sdan } 3258d22a174Sdan } 3265cf53537Sdan #endif 3278d22a174Sdan return rc; 3288d22a174Sdan } 3298d22a174Sdan 3304f26d6c4Sdrh /* 3314f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 3324f26d6c4Sdrh ** statement is completely executed or an error occurs. 333b900aaf3Sdrh ** 334b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step() 335b900aaf3Sdrh ** API. The only thing omitted is the automatic recompile if a 336b900aaf3Sdrh ** schema change has occurred. That detail is handled by the 337b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure. 3384f26d6c4Sdrh */ 339b900aaf3Sdrh static int sqlite3Step(Vdbe *p){ 3409bb575fdSdrh sqlite3 *db; 3414f26d6c4Sdrh int rc; 3424f26d6c4Sdrh 343a185ddbfSdanielk1977 assert(p); 344a185ddbfSdanielk1977 if( p->magic!=VDBE_MAGIC_RUN ){ 3453674bfd1Sdrh /* We used to require that sqlite3_reset() be called before retrying 3463674bfd1Sdrh ** sqlite3_step() after any error. But after 3.6.23, we changed this 3473674bfd1Sdrh ** so that sqlite3_reset() would be called automatically instead of 3483674bfd1Sdrh ** throwing the error. 3493674bfd1Sdrh */ 3503674bfd1Sdrh sqlite3_reset((sqlite3_stmt*)p); 351f1bfe9a8Sdrh } 352f1bfe9a8Sdrh 35314d4cc43Sdan /* Check that malloc() has not failed. If it has, return early. */ 35417435752Sdrh db = p->db; 355c456e57aSdrh if( db->mallocFailed ){ 35614d4cc43Sdan p->rc = SQLITE_NOMEM; 357c456e57aSdrh return SQLITE_NOMEM; 358c456e57aSdrh } 359261919ccSdanielk1977 360a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 361a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 362b900aaf3Sdrh rc = SQLITE_ERROR; 363b900aaf3Sdrh goto end_of_step; 364a21c6b6fSdanielk1977 } 3651d850a72Sdanielk1977 if( p->pc<0 ){ 36615ca1df1Sdrh /* If there are no other statements currently running, then 36715ca1df1Sdrh ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 36815ca1df1Sdrh ** from interrupting a statement that has not yet started. 36915ca1df1Sdrh */ 37015ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 37115ca1df1Sdrh db->u1.isInterrupted = 0; 37215ca1df1Sdrh } 37315ca1df1Sdrh 3741da40a38Sdan assert( db->writeVdbeCnt>0 || db->autoCommit==0 || db->nDeferredCons==0 ); 3751da40a38Sdan 37619e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 37719e2d37fSdrh if( db->xProfile && !db->init.busy ){ 378b7e8ea20Sdrh sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime); 37919e2d37fSdrh } 38019e2d37fSdrh #endif 381c16a03b5Sdrh 3821d850a72Sdanielk1977 db->activeVdbeCnt++; 383ad4a4b80Sdrh if( p->readOnly==0 ) db->writeVdbeCnt++; 3841d850a72Sdanielk1977 p->pc = 0; 3851d850a72Sdanielk1977 } 386b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 3874f26d6c4Sdrh if( p->explain ){ 3884f26d6c4Sdrh rc = sqlite3VdbeList(p); 389b7f9164eSdrh }else 390b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 391b7f9164eSdrh { 3924f26d6c4Sdrh rc = sqlite3VdbeExec(p); 3934f26d6c4Sdrh } 3944f26d6c4Sdrh 39519e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 39619e2d37fSdrh /* Invoke the profile callback if there is one 39719e2d37fSdrh */ 3986ab3a2ecSdanielk1977 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){ 399b7e8ea20Sdrh sqlite3_int64 iNow; 400b7e8ea20Sdrh sqlite3OsCurrentTimeInt64(db->pVfs, &iNow); 401*df0db0feSdrh db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000); 40219e2d37fSdrh } 40319e2d37fSdrh #endif 40419e2d37fSdrh 4058d22a174Sdan if( rc==SQLITE_DONE ){ 4068d22a174Sdan assert( p->rc==SQLITE_OK ); 4077ed91f23Sdrh p->rc = doWalCallbacks(db); 4088d22a174Sdan if( p->rc!=SQLITE_OK ){ 4098d22a174Sdan rc = SQLITE_ERROR; 4108d22a174Sdan } 4118d22a174Sdan } 4128d22a174Sdan 41380cc85b3Sdrh db->errCode = rc; 414357864ecSdanielk1977 if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){ 415357864ecSdanielk1977 p->rc = SQLITE_NOMEM; 4164f26d6c4Sdrh } 417357864ecSdanielk1977 end_of_step: 418357864ecSdanielk1977 /* At this point local variable rc holds the value that should be 419357864ecSdanielk1977 ** returned if this statement was compiled using the legacy 420357864ecSdanielk1977 ** sqlite3_prepare() interface. According to the docs, this can only 421357864ecSdanielk1977 ** be one of the values in the first assert() below. Variable p->rc 422357864ecSdanielk1977 ** contains the value that would be returned if sqlite3_finalize() 423357864ecSdanielk1977 ** were called on statement p. 424357864ecSdanielk1977 */ 425357864ecSdanielk1977 assert( rc==SQLITE_ROW || rc==SQLITE_DONE || rc==SQLITE_ERROR 426357864ecSdanielk1977 || rc==SQLITE_BUSY || rc==SQLITE_MISUSE 427357864ecSdanielk1977 ); 428357864ecSdanielk1977 assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE ); 429357864ecSdanielk1977 if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ 430357864ecSdanielk1977 /* If this statement was prepared using sqlite3_prepare_v2(), and an 431357864ecSdanielk1977 ** error has occured, then return the error code in p->rc to the 432357864ecSdanielk1977 ** caller. Set the error code in the database handle to the same value. 433357864ecSdanielk1977 */ 434357864ecSdanielk1977 rc = db->errCode = p->rc; 435357864ecSdanielk1977 } 436357864ecSdanielk1977 return (rc&db->errMask); 437b900aaf3Sdrh } 438b900aaf3Sdrh 439b900aaf3Sdrh /* 440b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step(). Call 441b900aaf3Sdrh ** sqlite3Step() to do most of the work. If a schema error occurs, 442b900aaf3Sdrh ** call sqlite3Reprepare() and try again. 443b900aaf3Sdrh */ 444b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 445a6129fa7Sdrh int rc = SQLITE_OK; /* Result from sqlite3Step() */ 446a6129fa7Sdrh int rc2 = SQLITE_OK; /* Result from sqlite3Reprepare() */ 447a6129fa7Sdrh Vdbe *v = (Vdbe*)pStmt; /* the prepared statement */ 448a6129fa7Sdrh int cnt = 0; /* Counter to prevent infinite loop of reprepares */ 449a6129fa7Sdrh sqlite3 *db; /* The database connection */ 450a6129fa7Sdrh 451413c3d36Sdrh if( vdbeSafetyNotNull(v) ){ 452413c3d36Sdrh return SQLITE_MISUSE_BKPT; 453413c3d36Sdrh } 454413c3d36Sdrh db = v->db; 4558e556520Sdanielk1977 sqlite3_mutex_enter(db->mutex); 456b900aaf3Sdrh while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 457b900aaf3Sdrh && cnt++ < 5 458a6129fa7Sdrh && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){ 459b900aaf3Sdrh sqlite3_reset(pStmt); 460b900aaf3Sdrh v->expired = 0; 461b900aaf3Sdrh } 462bee8065eSdrh if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){ 4638e556520Sdanielk1977 /* This case occurs after failing to recompile an sql statement. 4648e556520Sdanielk1977 ** The error message from the SQL compiler has already been loaded 4658e556520Sdanielk1977 ** into the database handle. This block copies the error message 4668e556520Sdanielk1977 ** from the database handle into the statement and sets the statement 4678e556520Sdanielk1977 ** program counter to 0 to ensure that when the statement is 4688e556520Sdanielk1977 ** finalized or reset the parser error message is available via 4698e556520Sdanielk1977 ** sqlite3_errmsg() and sqlite3_errcode(). 4708e556520Sdanielk1977 */ 4718e556520Sdanielk1977 const char *zErr = (const char *)sqlite3_value_text(db->pErr); 472633e6d57Sdrh sqlite3DbFree(db, v->zErrMsg); 4738e556520Sdanielk1977 if( !db->mallocFailed ){ 4748e556520Sdanielk1977 v->zErrMsg = sqlite3DbStrDup(db, zErr); 475a6129fa7Sdrh v->rc = rc2; 4768e556520Sdanielk1977 } else { 4778e556520Sdanielk1977 v->zErrMsg = 0; 478a6129fa7Sdrh v->rc = rc = SQLITE_NOMEM; 4798e556520Sdanielk1977 } 4808e556520Sdanielk1977 } 4818e556520Sdanielk1977 rc = sqlite3ApiExit(db, rc); 4828e556520Sdanielk1977 sqlite3_mutex_leave(db->mutex); 483b900aaf3Sdrh return rc; 484b900aaf3Sdrh } 4854f26d6c4Sdrh 4864f26d6c4Sdrh /* 487eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 488eb2e176aSdrh ** pointer to it. 489eb2e176aSdrh */ 490eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 491eb2e176aSdrh assert( p && p->pFunc ); 492eb2e176aSdrh return p->pFunc->pUserData; 493eb2e176aSdrh } 494eb2e176aSdrh 495eb2e176aSdrh /* 496fa4a4b91Sdrh ** Extract the user data from a sqlite3_context structure and return a 497fa4a4b91Sdrh ** pointer to it. 498fa4a4b91Sdrh */ 499fa4a4b91Sdrh sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ 500fa4a4b91Sdrh assert( p && p->pFunc ); 501fa4a4b91Sdrh return p->s.db; 502fa4a4b91Sdrh } 503fa4a4b91Sdrh 504fa4a4b91Sdrh /* 505b7481e70Sdrh ** The following is the implementation of an SQL function that always 506b7481e70Sdrh ** fails with an error message stating that the function is used in the 507b7481e70Sdrh ** wrong context. The sqlite3_overload_function() API might construct 508b7481e70Sdrh ** SQL function that use this routine so that the functions will exist 509b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction 510b7481e70Sdrh ** method of virtual tables. 511b7481e70Sdrh */ 512b7481e70Sdrh void sqlite3InvalidFunction( 513b7481e70Sdrh sqlite3_context *context, /* The function calling context */ 51462c14b34Sdanielk1977 int NotUsed, /* Number of arguments to the function */ 51562c14b34Sdanielk1977 sqlite3_value **NotUsed2 /* Value of each argument */ 516b7481e70Sdrh ){ 517b7481e70Sdrh const char *zName = context->pFunc->zName; 518b7481e70Sdrh char *zErr; 51962c14b34Sdanielk1977 UNUSED_PARAMETER2(NotUsed, NotUsed2); 520bc6160b0Sdrh zErr = sqlite3_mprintf( 521b7481e70Sdrh "unable to use function %s in the requested context", zName); 522b7481e70Sdrh sqlite3_result_error(context, zErr, -1); 5231e536953Sdanielk1977 sqlite3_free(zErr); 524b7481e70Sdrh } 525b7481e70Sdrh 526b7481e70Sdrh /* 527eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 528eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 529eb2e176aSdrh ** same context that was returned on prior calls. 530eb2e176aSdrh */ 531eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 532b21c8cd4Sdrh Mem *pMem; 533825c662eSdrh assert( p && p->pFunc && p->pFunc->xStep ); 534b21c8cd4Sdrh assert( sqlite3_mutex_held(p->s.db->mutex) ); 535b21c8cd4Sdrh pMem = p->pMem; 536d68eee04Sdrh testcase( nByte<0 ); 537a10a34b8Sdrh if( (pMem->flags & MEM_Agg)==0 ){ 538d68eee04Sdrh if( nByte<=0 ){ 5395f096135Sdanielk1977 sqlite3VdbeMemReleaseExternal(pMem); 5405f096135Sdanielk1977 pMem->flags = MEM_Null; 541a10a34b8Sdrh pMem->z = 0; 542a10a34b8Sdrh }else{ 5435f096135Sdanielk1977 sqlite3VdbeMemGrow(pMem, nByte, 0); 544abfcea25Sdrh pMem->flags = MEM_Agg; 5453c024d69Sdrh pMem->u.pDef = p->pFunc; 5465f096135Sdanielk1977 if( pMem->z ){ 5475f096135Sdanielk1977 memset(pMem->z, 0, nByte); 5485f096135Sdanielk1977 } 549eb2e176aSdrh } 550eb2e176aSdrh } 551abfcea25Sdrh return (void*)pMem->z; 552eb2e176aSdrh } 553eb2e176aSdrh 554eb2e176aSdrh /* 555682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 556682f68b0Sdanielk1977 ** the user-function defined by pCtx. 557682f68b0Sdanielk1977 */ 558682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 559b21c8cd4Sdrh VdbeFunc *pVdbeFunc; 560b21c8cd4Sdrh 561b21c8cd4Sdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 562b21c8cd4Sdrh pVdbeFunc = pCtx->pVdbeFunc; 563682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 564682f68b0Sdanielk1977 return 0; 565682f68b0Sdanielk1977 } 566f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 567682f68b0Sdanielk1977 } 568682f68b0Sdanielk1977 569682f68b0Sdanielk1977 /* 570682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 571682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 572682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 573682f68b0Sdanielk1977 */ 574682f68b0Sdanielk1977 void sqlite3_set_auxdata( 575682f68b0Sdanielk1977 sqlite3_context *pCtx, 576682f68b0Sdanielk1977 int iArg, 577682f68b0Sdanielk1977 void *pAux, 578682f68b0Sdanielk1977 void (*xDelete)(void*) 579682f68b0Sdanielk1977 ){ 580682f68b0Sdanielk1977 struct AuxData *pAuxData; 581f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 582e0fc5261Sdanielk1977 if( iArg<0 ) goto failed; 583682f68b0Sdanielk1977 584b21c8cd4Sdrh assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); 585f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 586f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 58731dad9daSdanielk1977 int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0); 588998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 58926783a58Sdanielk1977 pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc); 59017435752Sdrh if( !pVdbeFunc ){ 59117435752Sdrh goto failed; 59217435752Sdrh } 59353f733c7Sdrh pCtx->pVdbeFunc = pVdbeFunc; 59431dad9daSdanielk1977 memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux)); 595998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 596998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 597682f68b0Sdanielk1977 } 598682f68b0Sdanielk1977 599f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 600682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 601682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 602682f68b0Sdanielk1977 } 603682f68b0Sdanielk1977 pAuxData->pAux = pAux; 604682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 605e0fc5261Sdanielk1977 return; 606e0fc5261Sdanielk1977 607e0fc5261Sdanielk1977 failed: 608e0fc5261Sdanielk1977 if( xDelete ){ 609e0fc5261Sdanielk1977 xDelete(pAux); 610e0fc5261Sdanielk1977 } 611682f68b0Sdanielk1977 } 612682f68b0Sdanielk1977 613eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 614682f68b0Sdanielk1977 /* 615eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 616eb2e176aSdrh ** called. 617eb2e176aSdrh ** 618cf85a51cSdrh ** This function is deprecated. Do not use it for new code. It is 619cf85a51cSdrh ** provide only to avoid breaking legacy code. New aggregate function 620cf85a51cSdrh ** implementations should keep their own counts within their aggregate 621cf85a51cSdrh ** context. 622eb2e176aSdrh */ 623eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 624e34c647eSshane assert( p && p->pMem && p->pFunc && p->pFunc->xStep ); 625abfcea25Sdrh return p->pMem->n; 626eb2e176aSdrh } 627eec556d3Sshane #endif 628eb2e176aSdrh 629eb2e176aSdrh /* 6304f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 6314f26d6c4Sdrh */ 6324f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 6334f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 6341bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 6354f26d6c4Sdrh } 6364f26d6c4Sdrh 6374f26d6c4Sdrh /* 6384f26d6c4Sdrh ** Return the number of values available from the current row of the 6394f26d6c4Sdrh ** currently executing statement pStmt. 6404f26d6c4Sdrh */ 6414f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 6424f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 643d4e70ebdSdrh if( pVm==0 || pVm->pResultSet==0 ) return 0; 6444f26d6c4Sdrh return pVm->nResColumn; 6454f26d6c4Sdrh } 6464f26d6c4Sdrh 6474f26d6c4Sdrh 6484f26d6c4Sdrh /* 6494f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 6504f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 6514f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 6524f26d6c4Sdrh ** of NULL. 6534f26d6c4Sdrh */ 6544f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 65532bc3f6eSdrh Vdbe *pVm; 65632bc3f6eSdrh int vals; 65732bc3f6eSdrh Mem *pOut; 65832bc3f6eSdrh 65932bc3f6eSdrh pVm = (Vdbe *)pStmt; 660d4e70ebdSdrh if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){ 66132bc3f6eSdrh sqlite3_mutex_enter(pVm->db->mutex); 66232bc3f6eSdrh vals = sqlite3_data_count(pStmt); 663d4e70ebdSdrh pOut = &pVm->pResultSet[i]; 66432bc3f6eSdrh }else{ 66541f5b04cSdanielk1977 /* If the value passed as the second argument is out of range, return 66641f5b04cSdanielk1977 ** a pointer to the following static Mem object which contains the 66741f5b04cSdanielk1977 ** value SQL NULL. Even though the Mem structure contains an element 66841f5b04cSdanielk1977 ** of type i64, on certain architecture (x86) with certain compiler 66941f5b04cSdanielk1977 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary 67041f5b04cSdanielk1977 ** instead of an 8-byte one. This all works fine, except that when 67141f5b04cSdanielk1977 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s 67241f5b04cSdanielk1977 ** that a Mem structure is located on an 8-byte boundary. To prevent 67341f5b04cSdanielk1977 ** this assert() from failing, when building with SQLITE_DEBUG defined 67441f5b04cSdanielk1977 ** using gcc, force nullMem to be 8-byte aligned using the magical 67541f5b04cSdanielk1977 ** __attribute__((aligned(8))) macro. */ 67641f5b04cSdanielk1977 static const Mem nullMem 67741f5b04cSdanielk1977 #if defined(SQLITE_DEBUG) && defined(__GNUC__) 67841f5b04cSdanielk1977 __attribute__((aligned(8))) 67941f5b04cSdanielk1977 #endif 68041f5b04cSdanielk1977 = {{0}, (double)0, 0, "", 0, MEM_Null, SQLITE_NULL, 0, 0, 0 }; 68141f5b04cSdanielk1977 6829b4ea4a5Sdrh if( pVm && ALWAYS(pVm->db) ){ 68327641703Sdrh sqlite3_mutex_enter(pVm->db->mutex); 6844f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 68527641703Sdrh } 68632bc3f6eSdrh pOut = (Mem*)&nullMem; 6874f26d6c4Sdrh } 68832bc3f6eSdrh return pOut; 6894f26d6c4Sdrh } 6904f26d6c4Sdrh 6912e588c75Sdanielk1977 /* 6922e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 6932e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 6942e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 6952e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 6962e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 6972e588c75Sdanielk1977 ** 69832bc3f6eSdrh ** Specifically, this is called from within: 6992e588c75Sdanielk1977 ** 7002e588c75Sdanielk1977 ** sqlite3_column_int() 7012e588c75Sdanielk1977 ** sqlite3_column_int64() 7022e588c75Sdanielk1977 ** sqlite3_column_text() 7032e588c75Sdanielk1977 ** sqlite3_column_text16() 7042e588c75Sdanielk1977 ** sqlite3_column_real() 7052e588c75Sdanielk1977 ** sqlite3_column_bytes() 7062e588c75Sdanielk1977 ** sqlite3_column_bytes16() 7072e588c75Sdanielk1977 ** 7082e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc(). 7092e588c75Sdanielk1977 */ 7102e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 7112e588c75Sdanielk1977 { 7122e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 7132e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 7142e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 7152e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 7162e588c75Sdanielk1977 */ 71754f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 71832bc3f6eSdrh if( p ){ 71932bc3f6eSdrh p->rc = sqlite3ApiExit(p->db, p->rc); 72032bc3f6eSdrh sqlite3_mutex_leave(p->db->mutex); 72132bc3f6eSdrh } 7222e588c75Sdanielk1977 } 7232e588c75Sdanielk1977 7244f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 7254f26d6c4Sdrh ** The following routines are used to access elements of the current row 7264f26d6c4Sdrh ** in the result set. 7274f26d6c4Sdrh */ 728c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 7292e588c75Sdanielk1977 const void *val; 7302e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 731c9cf901dSdanielk1977 /* Even though there is no encoding conversion, value_blob() might 732c9cf901dSdanielk1977 ** need to call malloc() to expand the result of a zeroblob() 733c9cf901dSdanielk1977 ** expression. 734c9cf901dSdanielk1977 */ 735c9cf901dSdanielk1977 columnMallocFailure(pStmt); 7362e588c75Sdanielk1977 return val; 737c572ef7fSdanielk1977 } 7384f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 7392e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 7402e588c75Sdanielk1977 columnMallocFailure(pStmt); 7412e588c75Sdanielk1977 return val; 7424f26d6c4Sdrh } 7434f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 7442e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 7452e588c75Sdanielk1977 columnMallocFailure(pStmt); 7462e588c75Sdanielk1977 return val; 7474f26d6c4Sdrh } 7484f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 7492e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 7502e588c75Sdanielk1977 columnMallocFailure(pStmt); 7512e588c75Sdanielk1977 return val; 7524f26d6c4Sdrh } 7534f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 7542e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 7552e588c75Sdanielk1977 columnMallocFailure(pStmt); 7562e588c75Sdanielk1977 return val; 7574f26d6c4Sdrh } 758efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 7592e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 7602e588c75Sdanielk1977 columnMallocFailure(pStmt); 7612e588c75Sdanielk1977 return val; 7624f26d6c4Sdrh } 7634f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 7642e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 7652e588c75Sdanielk1977 columnMallocFailure(pStmt); 7662e588c75Sdanielk1977 return val; 7674f26d6c4Sdrh } 768d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 769d0ffa1e8Sdanielk1977 Mem *pOut = columnMem(pStmt, i); 770d0ffa1e8Sdanielk1977 if( pOut->flags&MEM_Static ){ 771d0ffa1e8Sdanielk1977 pOut->flags &= ~MEM_Static; 772d0ffa1e8Sdanielk1977 pOut->flags |= MEM_Ephem; 773d0ffa1e8Sdanielk1977 } 77432bc3f6eSdrh columnMallocFailure(pStmt); 775d0ffa1e8Sdanielk1977 return (sqlite3_value *)pOut; 776d1e4733dSdrh } 7776c62608fSdrh #ifndef SQLITE_OMIT_UTF16 7784f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 7792e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 7802e588c75Sdanielk1977 columnMallocFailure(pStmt); 7812e588c75Sdanielk1977 return val; 7824f26d6c4Sdrh } 7836c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 7844f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 78532bc3f6eSdrh int iType = sqlite3_value_type( columnMem(pStmt,i) ); 78632bc3f6eSdrh columnMallocFailure(pStmt); 78732bc3f6eSdrh return iType; 7884f26d6c4Sdrh } 7894f26d6c4Sdrh 79029d72108Sdrh /* The following function is experimental and subject to change or 79129d72108Sdrh ** removal */ 79229d72108Sdrh /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 79329d72108Sdrh ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 79429d72108Sdrh **} 79529d72108Sdrh */ 79629d72108Sdrh 7975e2517e1Sdrh /* 7985e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 7995e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 800e590fbdeSdrh ** 801e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 802e590fbdeSdrh ** name is returned. Here are the names: 803e590fbdeSdrh ** 804e590fbdeSdrh ** 0 The column name as it should be displayed for output 805e590fbdeSdrh ** 1 The datatype name for the column 806e590fbdeSdrh ** 2 The name of the database that the column derives from 807e590fbdeSdrh ** 3 The name of the table that the column derives from 808e590fbdeSdrh ** 4 The name of the table column that the result column derives from 809e590fbdeSdrh ** 810e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 811e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 8125e2517e1Sdrh */ 8135e2517e1Sdrh static const void *columnName( 8145e2517e1Sdrh sqlite3_stmt *pStmt, 8155e2517e1Sdrh int N, 8165e2517e1Sdrh const void *(*xFunc)(Mem*), 8175e2517e1Sdrh int useType 8185e2517e1Sdrh ){ 81932bc3f6eSdrh const void *ret = 0; 8205e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 82132bc3f6eSdrh int n; 822c6c7fd51Sdrh sqlite3 *db = p->db; 8235e2517e1Sdrh 824c6c7fd51Sdrh assert( db!=0 ); 82532bc3f6eSdrh n = sqlite3_column_count(pStmt); 82632bc3f6eSdrh if( N<n && N>=0 ){ 827e590fbdeSdrh N += useType*n; 828c6c7fd51Sdrh sqlite3_mutex_enter(db->mutex); 829c6c7fd51Sdrh assert( db->mallocFailed==0 ); 83000fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 83132bc3f6eSdrh /* A malloc may have failed inside of the xFunc() call. If this 83232bc3f6eSdrh ** is the case, clear the mallocFailed flag and return NULL. 83300fd957bSdanielk1977 */ 834c6c7fd51Sdrh if( db->mallocFailed ){ 835c6c7fd51Sdrh db->mallocFailed = 0; 83632bc3f6eSdrh ret = 0; 83732bc3f6eSdrh } 838c6c7fd51Sdrh sqlite3_mutex_leave(db->mutex); 83932bc3f6eSdrh } 84000fd957bSdanielk1977 return ret; 8415e2517e1Sdrh } 8425e2517e1Sdrh 8434f26d6c4Sdrh /* 8444f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 8454f26d6c4Sdrh ** statement pStmt. 8464f26d6c4Sdrh */ 8474f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 848955de52cSdanielk1977 return columnName( 849955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 8504f26d6c4Sdrh } 851e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 852e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 853955de52cSdanielk1977 return columnName( 854955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 855e590fbdeSdrh } 856e590fbdeSdrh #endif 8574f26d6c4Sdrh 8584f26d6c4Sdrh /* 8593f913576Sdrh ** Constraint: If you have ENABLE_COLUMN_METADATA then you must 8603f913576Sdrh ** not define OMIT_DECLTYPE. 8613f913576Sdrh */ 8623f913576Sdrh #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA) 8633f913576Sdrh # error "Must not define both SQLITE_OMIT_DECLTYPE \ 8643f913576Sdrh and SQLITE_ENABLE_COLUMN_METADATA" 8653f913576Sdrh #endif 8663f913576Sdrh 8673f913576Sdrh #ifndef SQLITE_OMIT_DECLTYPE 8683f913576Sdrh /* 8696c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 870e590fbdeSdrh ** of the result set of SQL statement pStmt. 8716c62608fSdrh */ 8726c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 873955de52cSdanielk1977 return columnName( 874955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 8756c62608fSdrh } 8766c62608fSdrh #ifndef SQLITE_OMIT_UTF16 87776d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 878955de52cSdanielk1977 return columnName( 879955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 8804f26d6c4Sdrh } 8816c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 8823f913576Sdrh #endif /* SQLITE_OMIT_DECLTYPE */ 8834f26d6c4Sdrh 8844b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA 885e590fbdeSdrh /* 886e590fbdeSdrh ** Return the name of the database from which a result column derives. 887e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 888e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 889e590fbdeSdrh */ 890e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 891955de52cSdanielk1977 return columnName( 892955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 893e590fbdeSdrh } 894e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 895e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 896955de52cSdanielk1977 return columnName( 897955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 898e590fbdeSdrh } 899e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 900e590fbdeSdrh 901e590fbdeSdrh /* 902e590fbdeSdrh ** Return the name of the table from which a result column derives. 903e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 904e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 905e590fbdeSdrh */ 906e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 907955de52cSdanielk1977 return columnName( 908955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 909e590fbdeSdrh } 910e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 911e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 912955de52cSdanielk1977 return columnName( 913955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 914e590fbdeSdrh } 915e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 916e590fbdeSdrh 917e590fbdeSdrh /* 918e590fbdeSdrh ** Return the name of the table column from which a result column derives. 919e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 920e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 921e590fbdeSdrh */ 922e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 923955de52cSdanielk1977 return columnName( 924955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 925e590fbdeSdrh } 926e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 927e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 928955de52cSdanielk1977 return columnName( 929955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 930e590fbdeSdrh } 931e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 9324b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 933e590fbdeSdrh 934e590fbdeSdrh 9354f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 9364f26d6c4Sdrh ** 9374f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 9384f26d6c4Sdrh */ 9394f26d6c4Sdrh /* 9404f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 9414f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 9424f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 9434f26d6c4Sdrh ** 944488e7b63Sdrh ** A successful evaluation of this routine acquires the mutex on p. 945488e7b63Sdrh ** the mutex is released if any kind of error occurs. 946488e7b63Sdrh ** 9474f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 9484f26d6c4Sdrh ** value in any case. 9494f26d6c4Sdrh */ 9504f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 9514f26d6c4Sdrh Mem *pVar; 952413c3d36Sdrh if( vdbeSafetyNotNull(p) ){ 953413c3d36Sdrh return SQLITE_MISUSE_BKPT; 954413c3d36Sdrh } 955488e7b63Sdrh sqlite3_mutex_enter(p->db->mutex); 956488e7b63Sdrh if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 957488e7b63Sdrh sqlite3Error(p->db, SQLITE_MISUSE, 0); 958488e7b63Sdrh sqlite3_mutex_leave(p->db->mutex); 959413c3d36Sdrh sqlite3_log(SQLITE_MISUSE, 960413c3d36Sdrh "bind on a busy prepared statement: [%s]", p->zSql); 961413c3d36Sdrh return SQLITE_MISUSE_BKPT; 9624f26d6c4Sdrh } 9634f26d6c4Sdrh if( i<1 || i>p->nVar ){ 9644f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 965488e7b63Sdrh sqlite3_mutex_leave(p->db->mutex); 9664f26d6c4Sdrh return SQLITE_RANGE; 9674f26d6c4Sdrh } 9684f26d6c4Sdrh i--; 969290c1948Sdrh pVar = &p->aVar[i]; 970d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 9714f26d6c4Sdrh pVar->flags = MEM_Null; 9724f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 973937d0deaSdan 9741d2ce4f8Sdan /* If the bit corresponding to this variable in Vdbe.expmask is set, then 9751d2ce4f8Sdan ** binding a new value to this variable invalidates the current query plan. 9761d2ce4f8Sdan */ 977823e09abSdrh if( p->isPrepareV2 && 978823e09abSdrh ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff) 979823e09abSdrh ){ 980937d0deaSdan p->expired = 1; 981937d0deaSdan } 9824f26d6c4Sdrh return SQLITE_OK; 9834f26d6c4Sdrh } 9844f26d6c4Sdrh 9854f26d6c4Sdrh /* 9865e2517e1Sdrh ** Bind a text or BLOB value. 9874f26d6c4Sdrh */ 9885e2517e1Sdrh static int bindText( 989605264d2Sdrh sqlite3_stmt *pStmt, /* The statement to bind against */ 990605264d2Sdrh int i, /* Index of the parameter to bind */ 991605264d2Sdrh const void *zData, /* Pointer to the data to be bound */ 992605264d2Sdrh int nData, /* Number of bytes of data to be bound */ 993605264d2Sdrh void (*xDel)(void*), /* Destructor for the data */ 994b27b7f5dSdrh u8 encoding /* Encoding for the data */ 9954f26d6c4Sdrh ){ 9964f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 9974f26d6c4Sdrh Mem *pVar; 9984f26d6c4Sdrh int rc; 9994f26d6c4Sdrh 10004f26d6c4Sdrh rc = vdbeUnbind(p, i); 1001488e7b63Sdrh if( rc==SQLITE_OK ){ 1002488e7b63Sdrh if( zData!=0 ){ 1003290c1948Sdrh pVar = &p->aVar[i-1]; 1004b21c8cd4Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 10055e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 1006b21c8cd4Sdrh rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 10075e2517e1Sdrh } 10081e536953Sdanielk1977 sqlite3Error(p->db, rc, 0); 1009605264d2Sdrh rc = sqlite3ApiExit(p->db, rc); 101027641703Sdrh } 1011605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1012488e7b63Sdrh } 1013605264d2Sdrh return rc; 10145e2517e1Sdrh } 10155e2517e1Sdrh 10165e2517e1Sdrh 10175e2517e1Sdrh /* 10185e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 10195e2517e1Sdrh */ 10205e2517e1Sdrh int sqlite3_bind_blob( 10215e2517e1Sdrh sqlite3_stmt *pStmt, 10225e2517e1Sdrh int i, 10235e2517e1Sdrh const void *zData, 10245e2517e1Sdrh int nData, 10255e2517e1Sdrh void (*xDel)(void*) 10265e2517e1Sdrh ){ 10275e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 10285e2517e1Sdrh } 10294f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 10304f26d6c4Sdrh int rc; 10314f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 10324f26d6c4Sdrh rc = vdbeUnbind(p, i); 10334f26d6c4Sdrh if( rc==SQLITE_OK ){ 1034290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 1035605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1036488e7b63Sdrh } 1037f4618891Sdanielk1977 return rc; 10384f26d6c4Sdrh } 10394f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 1040efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 10414f26d6c4Sdrh } 1042efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 10434f26d6c4Sdrh int rc; 10444f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 10454f26d6c4Sdrh rc = vdbeUnbind(p, i); 10464f26d6c4Sdrh if( rc==SQLITE_OK ){ 1047290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 1048605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1049488e7b63Sdrh } 10504f26d6c4Sdrh return rc; 10514f26d6c4Sdrh } 1052605264d2Sdrh int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){ 1053605264d2Sdrh int rc; 1054605264d2Sdrh Vdbe *p = (Vdbe*)pStmt; 1055605264d2Sdrh rc = vdbeUnbind(p, i); 1056488e7b63Sdrh if( rc==SQLITE_OK ){ 1057605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1058488e7b63Sdrh } 1059605264d2Sdrh return rc; 10604f26d6c4Sdrh } 10614f26d6c4Sdrh int sqlite3_bind_text( 10624f26d6c4Sdrh sqlite3_stmt *pStmt, 10634f26d6c4Sdrh int i, 10644f26d6c4Sdrh const char *zData, 10654f26d6c4Sdrh int nData, 1066d8123366Sdanielk1977 void (*xDel)(void*) 10674f26d6c4Sdrh ){ 10685e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 10694f26d6c4Sdrh } 10706c62608fSdrh #ifndef SQLITE_OMIT_UTF16 10714f26d6c4Sdrh int sqlite3_bind_text16( 10724f26d6c4Sdrh sqlite3_stmt *pStmt, 10734f26d6c4Sdrh int i, 10744f26d6c4Sdrh const void *zData, 10754f26d6c4Sdrh int nData, 1076d8123366Sdanielk1977 void (*xDel)(void*) 10774f26d6c4Sdrh ){ 10785e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 10794f26d6c4Sdrh } 10806c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 108126e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 108226e4144dSdanielk1977 int rc; 108329def560Sdrh switch( pValue->type ){ 108429def560Sdrh case SQLITE_INTEGER: { 108529def560Sdrh rc = sqlite3_bind_int64(pStmt, i, pValue->u.i); 108629def560Sdrh break; 108729def560Sdrh } 108829def560Sdrh case SQLITE_FLOAT: { 108929def560Sdrh rc = sqlite3_bind_double(pStmt, i, pValue->r); 109029def560Sdrh break; 109129def560Sdrh } 109229def560Sdrh case SQLITE_BLOB: { 109329def560Sdrh if( pValue->flags & MEM_Zero ){ 109429def560Sdrh rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero); 109529def560Sdrh }else{ 109629def560Sdrh rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT); 109729def560Sdrh } 109829def560Sdrh break; 109929def560Sdrh } 110029def560Sdrh case SQLITE_TEXT: { 1101ac80db78Sdrh rc = bindText(pStmt,i, pValue->z, pValue->n, SQLITE_TRANSIENT, 1102ac80db78Sdrh pValue->enc); 1103ac80db78Sdrh break; 1104ac80db78Sdrh } 1105ac80db78Sdrh default: { 1106ac80db78Sdrh rc = sqlite3_bind_null(pStmt, i); 110729def560Sdrh break; 110829def560Sdrh } 11090f5ea0b3Sdrh } 111026e4144dSdanielk1977 return rc; 111126e4144dSdanielk1977 } 1112b026e05eSdrh int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){ 1113b026e05eSdrh int rc; 1114b026e05eSdrh Vdbe *p = (Vdbe *)pStmt; 1115b026e05eSdrh rc = vdbeUnbind(p, i); 1116b026e05eSdrh if( rc==SQLITE_OK ){ 1117b026e05eSdrh sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n); 1118605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1119488e7b63Sdrh } 1120b026e05eSdrh return rc; 1121b026e05eSdrh } 112275f6a032Sdrh 112375f6a032Sdrh /* 112475f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 112575f6a032Sdrh ** This routine is added to support DBD::SQLite. 112675f6a032Sdrh */ 112775f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 112892febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 112992febd92Sdrh return p ? p->nVar : 0; 113075f6a032Sdrh } 1131895d7472Sdrh 1132895d7472Sdrh /* 1133fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 1134fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 1135fa6bc000Sdrh ** exist. 1136895d7472Sdrh */ 1137fa6bc000Sdrh static void createVarMap(Vdbe *p){ 1138895d7472Sdrh if( !p->okVar ){ 1139895d7472Sdrh int j; 1140895d7472Sdrh Op *pOp; 11410f5ea0b3Sdrh sqlite3_mutex_enter(p->db->mutex); 11420f5ea0b3Sdrh /* The race condition here is harmless. If two threads call this 11430f5ea0b3Sdrh ** routine on the same Vdbe at the same time, they both might end 11440f5ea0b3Sdrh ** up initializing the Vdbe.azVar[] array. That is a little extra 11450f5ea0b3Sdrh ** work but it results in the same answer. 11460f5ea0b3Sdrh */ 1147895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 1148895d7472Sdrh if( pOp->opcode==OP_Variable ){ 1149895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 11502dca4ac1Sdanielk1977 p->azVar[pOp->p1-1] = pOp->p4.z; 1151895d7472Sdrh } 1152895d7472Sdrh } 1153895d7472Sdrh p->okVar = 1; 1154605264d2Sdrh sqlite3_mutex_leave(p->db->mutex); 1155605264d2Sdrh } 1156fa6bc000Sdrh } 1157fa6bc000Sdrh 1158fa6bc000Sdrh /* 1159fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 1160fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 1161fa6bc000Sdrh ** 1162fa6bc000Sdrh ** The result is always UTF-8. 1163fa6bc000Sdrh */ 1164fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 1165fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 1166fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 1167fa6bc000Sdrh return 0; 1168fa6bc000Sdrh } 1169fa6bc000Sdrh createVarMap(p); 1170895d7472Sdrh return p->azVar[i-1]; 1171895d7472Sdrh } 1172fa6bc000Sdrh 1173fa6bc000Sdrh /* 1174fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 1175fa6bc000Sdrh ** with that name. If there is no variable with the given name, 1176fa6bc000Sdrh ** return 0. 1177fa6bc000Sdrh */ 11785f18a221Sdrh int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){ 1179fa6bc000Sdrh int i; 1180fa6bc000Sdrh if( p==0 ){ 1181fa6bc000Sdrh return 0; 1182fa6bc000Sdrh } 1183fa6bc000Sdrh createVarMap(p); 11846a8903c3Sdrh if( zName ){ 1185fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 1186971a7c87Sdrh const char *z = p->azVar[i]; 11875f18a221Sdrh if( z && memcmp(z,zName,nName)==0 && z[nName]==0 ){ 1188fa6bc000Sdrh return i+1; 1189fa6bc000Sdrh } 1190fa6bc000Sdrh } 11916a8903c3Sdrh } 1192fa6bc000Sdrh return 0; 1193fa6bc000Sdrh } 11945f18a221Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 11955f18a221Sdrh return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName)); 11965f18a221Sdrh } 1197f8db1bc0Sdrh 119851942bc3Sdrh /* 1199f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 1200f8db1bc0Sdrh */ 1201145834a4Sshane int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 1202f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 1203f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 12040f5ea0b3Sdrh int i; 12050f5ea0b3Sdrh assert( pTo->db==pFrom->db ); 12060f5ea0b3Sdrh assert( pTo->nVar==pFrom->nVar ); 120727641703Sdrh sqlite3_mutex_enter(pTo->db->mutex); 12080f5ea0b3Sdrh for(i=0; i<pFrom->nVar; i++){ 1209643167ffSdrh sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 1210f8db1bc0Sdrh } 121127641703Sdrh sqlite3_mutex_leave(pTo->db->mutex); 12120f5ea0b3Sdrh return SQLITE_OK; 1213f8db1bc0Sdrh } 121451942bc3Sdrh 1215eec556d3Sshane #ifndef SQLITE_OMIT_DEPRECATED 121651942bc3Sdrh /* 1217145834a4Sshane ** Deprecated external interface. Internal/core SQLite code 1218145834a4Sshane ** should call sqlite3TransferBindings. 12190f5ea0b3Sdrh ** 12200f5ea0b3Sdrh ** Is is misuse to call this routine with statements from different 12210f5ea0b3Sdrh ** database connections. But as this is a deprecated interface, we 12220f5ea0b3Sdrh ** will not bother to check for that condition. 12230f5ea0b3Sdrh ** 12240f5ea0b3Sdrh ** If the two statements contain a different number of bindings, then 12250f5ea0b3Sdrh ** an SQLITE_ERROR is returned. Nothing else can go wrong, so otherwise 12260f5ea0b3Sdrh ** SQLITE_OK is returned. 1227145834a4Sshane */ 1228145834a4Sshane int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 12290f5ea0b3Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 12300f5ea0b3Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 12310f5ea0b3Sdrh if( pFrom->nVar!=pTo->nVar ){ 12320f5ea0b3Sdrh return SQLITE_ERROR; 12330f5ea0b3Sdrh } 1234c94b8595Sdan if( pTo->isPrepareV2 && pTo->expmask ){ 1235c94b8595Sdan pTo->expired = 1; 1236c94b8595Sdan } 1237c94b8595Sdan if( pFrom->isPrepareV2 && pFrom->expmask ){ 1238c94b8595Sdan pFrom->expired = 1; 1239c94b8595Sdan } 1240145834a4Sshane return sqlite3TransferBindings(pFromStmt, pToStmt); 1241145834a4Sshane } 1242eec556d3Sshane #endif 1243145834a4Sshane 1244145834a4Sshane /* 124551942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 124651942bc3Sdrh ** in the argument belongs. This is the same database handle that was 124751942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 124851942bc3Sdrh ** the statement in the first place. 124951942bc3Sdrh */ 125051942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 125151942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 125251942bc3Sdrh } 1253bb5a9c3eSdrh 1254bb5a9c3eSdrh /* 1255bb5a9c3eSdrh ** Return a pointer to the next prepared statement after pStmt associated 1256bb5a9c3eSdrh ** with database connection pDb. If pStmt is NULL, return the first 1257bb5a9c3eSdrh ** prepared statement for the database connection. Return NULL if there 1258bb5a9c3eSdrh ** are no more. 1259bb5a9c3eSdrh */ 1260bb5a9c3eSdrh sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){ 1261bb5a9c3eSdrh sqlite3_stmt *pNext; 1262bb5a9c3eSdrh sqlite3_mutex_enter(pDb->mutex); 1263bb5a9c3eSdrh if( pStmt==0 ){ 1264bb5a9c3eSdrh pNext = (sqlite3_stmt*)pDb->pVdbe; 1265bb5a9c3eSdrh }else{ 1266bb5a9c3eSdrh pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext; 1267bb5a9c3eSdrh } 1268bb5a9c3eSdrh sqlite3_mutex_leave(pDb->mutex); 1269bb5a9c3eSdrh return pNext; 1270bb5a9c3eSdrh } 1271d1d38488Sdrh 1272d1d38488Sdrh /* 1273d1d38488Sdrh ** Return the value of a status counter for a prepared statement 1274d1d38488Sdrh */ 1275d1d38488Sdrh int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){ 1276d1d38488Sdrh Vdbe *pVdbe = (Vdbe*)pStmt; 1277d1d38488Sdrh int v = pVdbe->aCounter[op-1]; 1278d1d38488Sdrh if( resetFlag ) pVdbe->aCounter[op-1] = 0; 1279d1d38488Sdrh return v; 1280d1d38488Sdrh } 1281