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" 1819e2d37fSdrh #include "os.h" 194f26d6c4Sdrh 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 } 32d89bd007Sdrh 334f26d6c4Sdrh /**************************** sqlite3_value_ ******************************* 344f26d6c4Sdrh ** The following routines extract information from a Mem or sqlite3_value 354f26d6c4Sdrh ** structure. 364f26d6c4Sdrh */ 374f26d6c4Sdrh const void *sqlite3_value_blob(sqlite3_value *pVal){ 384f26d6c4Sdrh Mem *p = (Mem*)pVal; 394f26d6c4Sdrh if( p->flags & (MEM_Blob|MEM_Str) ){ 404f26d6c4Sdrh return p->z; 414f26d6c4Sdrh }else{ 424f26d6c4Sdrh return sqlite3_value_text(pVal); 434f26d6c4Sdrh } 444f26d6c4Sdrh } 454f26d6c4Sdrh int sqlite3_value_bytes(sqlite3_value *pVal){ 4613073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF8); 474f26d6c4Sdrh } 484f26d6c4Sdrh int sqlite3_value_bytes16(sqlite3_value *pVal){ 4913073931Sdanielk1977 return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE); 504f26d6c4Sdrh } 514f26d6c4Sdrh double sqlite3_value_double(sqlite3_value *pVal){ 526a6124e2Sdrh return sqlite3VdbeRealValue((Mem*)pVal); 534f26d6c4Sdrh } 544f26d6c4Sdrh int sqlite3_value_int(sqlite3_value *pVal){ 556a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 564f26d6c4Sdrh } 57efad9995Sdrh sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){ 586a6124e2Sdrh return sqlite3VdbeIntValue((Mem*)pVal); 594f26d6c4Sdrh } 604f26d6c4Sdrh const unsigned char *sqlite3_value_text(sqlite3_value *pVal){ 6100fd957bSdanielk1977 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8); 624f26d6c4Sdrh } 636c62608fSdrh #ifndef SQLITE_OMIT_UTF16 644f26d6c4Sdrh const void *sqlite3_value_text16(sqlite3_value* pVal){ 65dc8453fdSdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE); 664f26d6c4Sdrh } 67d8123366Sdanielk1977 const void *sqlite3_value_text16be(sqlite3_value *pVal){ 68d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16BE); 69d8123366Sdanielk1977 } 70d8123366Sdanielk1977 const void *sqlite3_value_text16le(sqlite3_value *pVal){ 71d8123366Sdanielk1977 return sqlite3ValueText(pVal, SQLITE_UTF16LE); 72d8123366Sdanielk1977 } 736c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 744f26d6c4Sdrh int sqlite3_value_type(sqlite3_value* pVal){ 75f4479501Sdrh return pVal->type; 764f26d6c4Sdrh } 774f26d6c4Sdrh 784f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 794f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 804f26d6c4Sdrh ** the function result. 814f26d6c4Sdrh */ 824f26d6c4Sdrh void sqlite3_result_blob( 834f26d6c4Sdrh sqlite3_context *pCtx, 844f26d6c4Sdrh const void *z, 854f26d6c4Sdrh int n, 86d8123366Sdanielk1977 void (*xDel)(void *) 874f26d6c4Sdrh ){ 885708d2deSdrh assert( n>=0 ); 89d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); 904f26d6c4Sdrh } 914f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 924f26d6c4Sdrh sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 934f26d6c4Sdrh } 944f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 954f26d6c4Sdrh pCtx->isError = 1; 96d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 974f26d6c4Sdrh } 984f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 994f26d6c4Sdrh pCtx->isError = 1; 100d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 1014f26d6c4Sdrh } 102f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 1034f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 1044f26d6c4Sdrh } 1054f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 1064f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 1074f26d6c4Sdrh } 1084f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 109f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 1104f26d6c4Sdrh } 1114f26d6c4Sdrh void sqlite3_result_text( 1124f26d6c4Sdrh sqlite3_context *pCtx, 1134f26d6c4Sdrh const char *z, 1144f26d6c4Sdrh int n, 115d8123366Sdanielk1977 void (*xDel)(void *) 1164f26d6c4Sdrh ){ 117d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); 1184f26d6c4Sdrh } 1196c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1204f26d6c4Sdrh void sqlite3_result_text16( 1214f26d6c4Sdrh sqlite3_context *pCtx, 1224f26d6c4Sdrh const void *z, 1234f26d6c4Sdrh int n, 124d8123366Sdanielk1977 void (*xDel)(void *) 1254f26d6c4Sdrh ){ 126d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); 127d8123366Sdanielk1977 } 128d8123366Sdanielk1977 void sqlite3_result_text16be( 129d8123366Sdanielk1977 sqlite3_context *pCtx, 130d8123366Sdanielk1977 const void *z, 131d8123366Sdanielk1977 int n, 132d8123366Sdanielk1977 void (*xDel)(void *) 133d8123366Sdanielk1977 ){ 134d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); 135d8123366Sdanielk1977 } 136d8123366Sdanielk1977 void sqlite3_result_text16le( 137d8123366Sdanielk1977 sqlite3_context *pCtx, 138d8123366Sdanielk1977 const void *z, 139d8123366Sdanielk1977 int n, 140d8123366Sdanielk1977 void (*xDel)(void *) 141d8123366Sdanielk1977 ){ 142d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); 1434f26d6c4Sdrh } 1446c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1454f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 1464f26d6c4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 1474f26d6c4Sdrh } 1484f26d6c4Sdrh 1494f26d6c4Sdrh 1504f26d6c4Sdrh /* 1514f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 1524f26d6c4Sdrh ** statement is completely executed or an error occurs. 1534f26d6c4Sdrh */ 1544f26d6c4Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 1554f26d6c4Sdrh Vdbe *p = (Vdbe*)pStmt; 1569bb575fdSdrh sqlite3 *db; 1574f26d6c4Sdrh int rc; 1584f26d6c4Sdrh 159e501b89aSdanielk1977 assert(!sqlite3ThreadData()->mallocFailed); 160261919ccSdanielk1977 1611bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ 1624f26d6c4Sdrh return SQLITE_MISUSE; 1634f26d6c4Sdrh } 16491b48aa1Sdrh if( p->aborted ){ 16591b48aa1Sdrh return SQLITE_ABORT; 16691b48aa1Sdrh } 167a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 168a21c6b6fSdanielk1977 if( p->rc==SQLITE_OK ){ 169a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 170a21c6b6fSdanielk1977 } 171a21c6b6fSdanielk1977 return SQLITE_ERROR; 172a21c6b6fSdanielk1977 } 1734f26d6c4Sdrh db = p->db; 1744f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1754f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1764f26d6c4Sdrh return SQLITE_MISUSE; 1774f26d6c4Sdrh } 1781d850a72Sdanielk1977 if( p->pc<0 ){ 17919e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 180c16a03b5Sdrh /* Invoke the trace callback if there is one 181c16a03b5Sdrh */ 18219e2d37fSdrh if( db->xTrace && !db->init.busy ){ 183c16a03b5Sdrh assert( p->nOp>0 ); 184c16a03b5Sdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 185c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3!=0 ); 186c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 187c16a03b5Sdrh sqlite3SafetyOff(db); 188c16a03b5Sdrh db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); 189c16a03b5Sdrh if( sqlite3SafetyOn(db) ){ 190c16a03b5Sdrh p->rc = SQLITE_MISUSE; 191c16a03b5Sdrh return SQLITE_MISUSE; 192c16a03b5Sdrh } 193c16a03b5Sdrh } 19419e2d37fSdrh if( db->xProfile && !db->init.busy ){ 19519e2d37fSdrh double rNow; 19666560adaSdrh sqlite3OsCurrentTime(&rNow); 19719e2d37fSdrh p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; 19819e2d37fSdrh } 19919e2d37fSdrh #endif 200c16a03b5Sdrh 201c16a03b5Sdrh /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned 202c16a03b5Sdrh ** on in debugging mode. 203c16a03b5Sdrh */ 204c16a03b5Sdrh #ifdef SQLITE_DEBUG 205c16a03b5Sdrh if( (db->flags & SQLITE_SqlTrace)!=0 ){ 206c16a03b5Sdrh sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); 207c16a03b5Sdrh } 208c16a03b5Sdrh #endif /* SQLITE_DEBUG */ 209c16a03b5Sdrh 2101d850a72Sdanielk1977 db->activeVdbeCnt++; 2111d850a72Sdanielk1977 p->pc = 0; 2121d850a72Sdanielk1977 } 213b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 2144f26d6c4Sdrh if( p->explain ){ 2154f26d6c4Sdrh rc = sqlite3VdbeList(p); 216b7f9164eSdrh }else 217b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 218b7f9164eSdrh { 2194f26d6c4Sdrh rc = sqlite3VdbeExec(p); 2204f26d6c4Sdrh } 2214f26d6c4Sdrh 2224f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 2234f26d6c4Sdrh rc = SQLITE_MISUSE; 2244f26d6c4Sdrh } 2254f26d6c4Sdrh 22619e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 22719e2d37fSdrh /* Invoke the profile callback if there is one 22819e2d37fSdrh */ 22919e2d37fSdrh if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ 23019e2d37fSdrh double rNow; 23119e2d37fSdrh u64 elapseTime; 23219e2d37fSdrh 23366560adaSdrh sqlite3OsCurrentTime(&rNow); 23419e2d37fSdrh elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; 23519e2d37fSdrh assert( p->nOp>0 ); 23619e2d37fSdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 23719e2d37fSdrh assert( p->aOp[p->nOp-1].p3!=0 ); 23819e2d37fSdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 23919e2d37fSdrh db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); 24019e2d37fSdrh } 24119e2d37fSdrh #endif 24219e2d37fSdrh 243130b9f40Sdrh sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); 2442e588c75Sdanielk1977 sqlite3MallocClearFailed(); 2454f26d6c4Sdrh return rc; 2464f26d6c4Sdrh } 2474f26d6c4Sdrh 2484f26d6c4Sdrh /* 249eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 250eb2e176aSdrh ** pointer to it. 251eb2e176aSdrh */ 252eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 253eb2e176aSdrh assert( p && p->pFunc ); 254eb2e176aSdrh return p->pFunc->pUserData; 255eb2e176aSdrh } 256eb2e176aSdrh 257eb2e176aSdrh /* 258eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 259eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 260eb2e176aSdrh ** same context that was returned on prior calls. 261eb2e176aSdrh */ 262eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 263abfcea25Sdrh Mem *pMem = p->pMem; 264825c662eSdrh assert( p && p->pFunc && p->pFunc->xStep ); 265a10a34b8Sdrh if( (pMem->flags & MEM_Agg)==0 ){ 266a10a34b8Sdrh if( nByte==0 ){ 267a10a34b8Sdrh assert( pMem->flags==MEM_Null ); 268a10a34b8Sdrh pMem->z = 0; 269a10a34b8Sdrh }else{ 270abfcea25Sdrh pMem->flags = MEM_Agg; 2715360ad34Sdrh pMem->xDel = sqlite3FreeX; 272abfcea25Sdrh *(FuncDef**)&pMem->i = p->pFunc; 273eb2e176aSdrh if( nByte<=NBFS ){ 274abfcea25Sdrh pMem->z = pMem->zShort; 275abfcea25Sdrh memset(pMem->z, 0, nByte); 276eb2e176aSdrh }else{ 277abfcea25Sdrh pMem->z = sqliteMalloc( nByte ); 278eb2e176aSdrh } 279eb2e176aSdrh } 280a10a34b8Sdrh } 281abfcea25Sdrh return (void*)pMem->z; 282eb2e176aSdrh } 283eb2e176aSdrh 284eb2e176aSdrh /* 285682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 286682f68b0Sdanielk1977 ** the user-function defined by pCtx. 287682f68b0Sdanielk1977 */ 288682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 289682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 290682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 291682f68b0Sdanielk1977 return 0; 292682f68b0Sdanielk1977 } 293f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 294682f68b0Sdanielk1977 } 295682f68b0Sdanielk1977 296682f68b0Sdanielk1977 /* 297682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 298682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 299682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 300682f68b0Sdanielk1977 */ 301682f68b0Sdanielk1977 void sqlite3_set_auxdata( 302682f68b0Sdanielk1977 sqlite3_context *pCtx, 303682f68b0Sdanielk1977 int iArg, 304682f68b0Sdanielk1977 void *pAux, 305682f68b0Sdanielk1977 void (*xDelete)(void*) 306682f68b0Sdanielk1977 ){ 307682f68b0Sdanielk1977 struct AuxData *pAuxData; 308f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 309682f68b0Sdanielk1977 if( iArg<0 ) return; 310682f68b0Sdanielk1977 311f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 312f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 313998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 31453f733c7Sdrh pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); 315998da3a2Sdrh if( !pVdbeFunc ) return; 31653f733c7Sdrh pCtx->pVdbeFunc = pVdbeFunc; 3170e3d7476Sdrh memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 3180e3d7476Sdrh sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); 319998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 320998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 321682f68b0Sdanielk1977 } 322682f68b0Sdanielk1977 323f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 324682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 325682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 326682f68b0Sdanielk1977 } 327682f68b0Sdanielk1977 pAuxData->pAux = pAux; 328682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 329682f68b0Sdanielk1977 } 330682f68b0Sdanielk1977 331682f68b0Sdanielk1977 /* 332eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 333eb2e176aSdrh ** called. 334eb2e176aSdrh ** 335eb2e176aSdrh ** This routine is defined here in vdbe.c because it depends on knowing 336eb2e176aSdrh ** the internals of the sqlite3_context structure which is only defined in 337eb2e176aSdrh ** this source file. 338eb2e176aSdrh */ 339eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 340eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 341abfcea25Sdrh return p->pMem->n; 342eb2e176aSdrh } 343eb2e176aSdrh 344eb2e176aSdrh /* 3454f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 3464f26d6c4Sdrh */ 3474f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 3484f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3491bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 3504f26d6c4Sdrh } 3514f26d6c4Sdrh 3524f26d6c4Sdrh /* 3534f26d6c4Sdrh ** Return the number of values available from the current row of the 3544f26d6c4Sdrh ** currently executing statement pStmt. 3554f26d6c4Sdrh */ 3564f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 3574f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3581bcdb0c0Sdrh if( pVm==0 || !pVm->resOnStack ) return 0; 3594f26d6c4Sdrh return pVm->nResColumn; 3604f26d6c4Sdrh } 3614f26d6c4Sdrh 3624f26d6c4Sdrh 3634f26d6c4Sdrh /* 3644f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 3654f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 3664f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 3674f26d6c4Sdrh ** of NULL. 3684f26d6c4Sdrh */ 3694f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 3704f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 3714f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 3724f26d6c4Sdrh if( i>=vals || i<0 ){ 3734f26d6c4Sdrh static Mem nullMem; 3744f26d6c4Sdrh if( nullMem.flags==0 ){ nullMem.flags = MEM_Null; } 3754f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 3764f26d6c4Sdrh return &nullMem; 3774f26d6c4Sdrh } 3784f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 3794f26d6c4Sdrh } 3804f26d6c4Sdrh 3812e588c75Sdanielk1977 /* 3822e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 3832e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 3842e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 3852e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 3862e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 3872e588c75Sdanielk1977 ** 3882e588c75Sdanielk1977 ** Specificly, this is called from within: 3892e588c75Sdanielk1977 ** 3902e588c75Sdanielk1977 ** sqlite3_column_int() 3912e588c75Sdanielk1977 ** sqlite3_column_int64() 3922e588c75Sdanielk1977 ** sqlite3_column_text() 3932e588c75Sdanielk1977 ** sqlite3_column_text16() 3942e588c75Sdanielk1977 ** sqlite3_column_real() 3952e588c75Sdanielk1977 ** sqlite3_column_bytes() 3962e588c75Sdanielk1977 ** sqlite3_column_bytes16() 3972e588c75Sdanielk1977 ** 3982e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc(). 3992e588c75Sdanielk1977 */ 4002e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 4012e588c75Sdanielk1977 { 4022e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 4032e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 4042e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 4052e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 4062e588c75Sdanielk1977 */ 407e501b89aSdanielk1977 if( sqlite3ThreadData()->mallocFailed ){ 4082e588c75Sdanielk1977 ((Vdbe *)pStmt)->rc = SQLITE_NOMEM; 4092e588c75Sdanielk1977 sqlite3MallocClearFailed(); 4102e588c75Sdanielk1977 } 4112e588c75Sdanielk1977 } 4122e588c75Sdanielk1977 4134f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 4144f26d6c4Sdrh ** The following routines are used to access elements of the current row 4154f26d6c4Sdrh ** in the result set. 4164f26d6c4Sdrh */ 417c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 4182e588c75Sdanielk1977 const void *val; 4192e588c75Sdanielk1977 sqlite3MallocDisallow(); 4202e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 4212e588c75Sdanielk1977 sqlite3MallocAllow(); 4222e588c75Sdanielk1977 return val; 423c572ef7fSdanielk1977 } 4244f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 4252e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 4262e588c75Sdanielk1977 columnMallocFailure(pStmt); 4272e588c75Sdanielk1977 return val; 4284f26d6c4Sdrh } 4294f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 4302e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 4312e588c75Sdanielk1977 columnMallocFailure(pStmt); 4322e588c75Sdanielk1977 return val; 4334f26d6c4Sdrh } 4344f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 4352e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 4362e588c75Sdanielk1977 columnMallocFailure(pStmt); 4372e588c75Sdanielk1977 return val; 4384f26d6c4Sdrh } 4394f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 4402e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 4412e588c75Sdanielk1977 columnMallocFailure(pStmt); 4422e588c75Sdanielk1977 return val; 4434f26d6c4Sdrh } 444efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 4452e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 4462e588c75Sdanielk1977 columnMallocFailure(pStmt); 4472e588c75Sdanielk1977 return val; 4484f26d6c4Sdrh } 4494f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 4502e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 4512e588c75Sdanielk1977 columnMallocFailure(pStmt); 4522e588c75Sdanielk1977 return val; 4534f26d6c4Sdrh } 454d1e4733dSdrh #if 0 455d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 456d1e4733dSdrh return columnMem(pStmt, i); 457d1e4733dSdrh } 458d1e4733dSdrh #endif 4596c62608fSdrh #ifndef SQLITE_OMIT_UTF16 4604f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 4612e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 4622e588c75Sdanielk1977 columnMallocFailure(pStmt); 4632e588c75Sdanielk1977 return val; 4644f26d6c4Sdrh } 4656c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 4664f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 4674f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 4684f26d6c4Sdrh } 4694f26d6c4Sdrh 4705e2517e1Sdrh /* 4715e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 4725e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 473e590fbdeSdrh ** 474e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 475e590fbdeSdrh ** name is returned. Here are the names: 476e590fbdeSdrh ** 477e590fbdeSdrh ** 0 The column name as it should be displayed for output 478e590fbdeSdrh ** 1 The datatype name for the column 479e590fbdeSdrh ** 2 The name of the database that the column derives from 480e590fbdeSdrh ** 3 The name of the table that the column derives from 481e590fbdeSdrh ** 4 The name of the table column that the result column derives from 482e590fbdeSdrh ** 483e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 484e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 4855e2517e1Sdrh */ 4865e2517e1Sdrh static const void *columnName( 4875e2517e1Sdrh sqlite3_stmt *pStmt, 4885e2517e1Sdrh int N, 4895e2517e1Sdrh const void *(*xFunc)(Mem*), 4905e2517e1Sdrh int useType 4915e2517e1Sdrh ){ 49200fd957bSdanielk1977 const void *ret; 4935e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 4945e2517e1Sdrh int n = sqlite3_column_count(pStmt); 4955e2517e1Sdrh 4965e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 4975e2517e1Sdrh return 0; 4985e2517e1Sdrh } 499e590fbdeSdrh N += useType*n; 50000fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 50100fd957bSdanielk1977 50200fd957bSdanielk1977 /* A malloc may have failed inside of the xFunc() call. If this is the case, 50300fd957bSdanielk1977 ** clear the mallocFailed flag and return NULL. 50400fd957bSdanielk1977 */ 5052e588c75Sdanielk1977 sqlite3MallocClearFailed(); 50600fd957bSdanielk1977 return ret; 5075e2517e1Sdrh } 5085e2517e1Sdrh 5094f26d6c4Sdrh /* 5104f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 5114f26d6c4Sdrh ** statement pStmt. 5124f26d6c4Sdrh */ 5134f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 5145e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0); 5154f26d6c4Sdrh } 516e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 517e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 518e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0); 519e590fbdeSdrh } 520e590fbdeSdrh #endif 5214f26d6c4Sdrh 5224f26d6c4Sdrh /* 5236c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 524e590fbdeSdrh ** of the result set of SQL statement pStmt. 5256c62608fSdrh */ 5266c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 5276c62608fSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1); 5286c62608fSdrh } 5296c62608fSdrh #ifndef SQLITE_OMIT_UTF16 53076d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 5315e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1); 5324f26d6c4Sdrh } 5336c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 5344f26d6c4Sdrh 535e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0 536e590fbdeSdrh /* 537e590fbdeSdrh ** Return the name of the database from which a result column derives. 538e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 539e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 540e590fbdeSdrh */ 541e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 542e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2); 543e590fbdeSdrh } 544e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 545e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 546e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2); 547e590fbdeSdrh } 548e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 549e590fbdeSdrh 550e590fbdeSdrh /* 551e590fbdeSdrh ** Return the name of the table from which a result column derives. 552e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 553e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 554e590fbdeSdrh */ 555e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 556e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3); 557e590fbdeSdrh } 558e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 559e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 560e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3); 561e590fbdeSdrh } 562e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 563e590fbdeSdrh 564e590fbdeSdrh /* 565e590fbdeSdrh ** Return the name of the table column from which a result column derives. 566e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 567e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 568e590fbdeSdrh */ 569e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 570e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4); 571e590fbdeSdrh } 572e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 573e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 574e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4); 575e590fbdeSdrh } 576e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 577e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */ 578e590fbdeSdrh 579e590fbdeSdrh 580e590fbdeSdrh 581e590fbdeSdrh 5824f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 5834f26d6c4Sdrh ** 5844f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 5854f26d6c4Sdrh */ 5864f26d6c4Sdrh /* 5874f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 5884f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 5894f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 5904f26d6c4Sdrh ** 5914f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 5924f26d6c4Sdrh ** value in any case. 5934f26d6c4Sdrh */ 5944f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 5954f26d6c4Sdrh Mem *pVar; 5961bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 597473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 5984f26d6c4Sdrh return SQLITE_MISUSE; 5994f26d6c4Sdrh } 6004f26d6c4Sdrh if( i<1 || i>p->nVar ){ 6014f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 6024f26d6c4Sdrh return SQLITE_RANGE; 6034f26d6c4Sdrh } 6044f26d6c4Sdrh i--; 605290c1948Sdrh pVar = &p->aVar[i]; 606d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 6074f26d6c4Sdrh pVar->flags = MEM_Null; 6084f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 6094f26d6c4Sdrh return SQLITE_OK; 6104f26d6c4Sdrh } 6114f26d6c4Sdrh 6124f26d6c4Sdrh /* 6135e2517e1Sdrh ** Bind a text or BLOB value. 6144f26d6c4Sdrh */ 6155e2517e1Sdrh static int bindText( 616f4479501Sdrh sqlite3_stmt *pStmt, 6174f26d6c4Sdrh int i, 6184f26d6c4Sdrh const void *zData, 6194f26d6c4Sdrh int nData, 6205e2517e1Sdrh void (*xDel)(void*), 6215e2517e1Sdrh int encoding 6224f26d6c4Sdrh ){ 6234f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6244f26d6c4Sdrh Mem *pVar; 6254f26d6c4Sdrh int rc; 6264f26d6c4Sdrh 6274f26d6c4Sdrh rc = vdbeUnbind(p, i); 628feac5f8dSdrh if( rc || zData==0 ){ 6294f26d6c4Sdrh return rc; 6304f26d6c4Sdrh } 631290c1948Sdrh pVar = &p->aVar[i-1]; 6325e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 6335e2517e1Sdrh if( rc ){ 6344f26d6c4Sdrh return rc; 6354f26d6c4Sdrh } 6365e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 637*14db2665Sdanielk1977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 6385e2517e1Sdrh } 6395e2517e1Sdrh return rc; 6405e2517e1Sdrh } 6415e2517e1Sdrh 6425e2517e1Sdrh 6435e2517e1Sdrh /* 6445e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 6455e2517e1Sdrh */ 6465e2517e1Sdrh int sqlite3_bind_blob( 6475e2517e1Sdrh sqlite3_stmt *pStmt, 6485e2517e1Sdrh int i, 6495e2517e1Sdrh const void *zData, 6505e2517e1Sdrh int nData, 6515e2517e1Sdrh void (*xDel)(void*) 6525e2517e1Sdrh ){ 6535e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 6545e2517e1Sdrh } 6554f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 6564f26d6c4Sdrh int rc; 6574f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6584f26d6c4Sdrh rc = vdbeUnbind(p, i); 6594f26d6c4Sdrh if( rc==SQLITE_OK ){ 660290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 6614f26d6c4Sdrh } 662f4618891Sdanielk1977 return rc; 6634f26d6c4Sdrh } 6644f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 665efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 6664f26d6c4Sdrh } 667efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 6684f26d6c4Sdrh int rc; 6694f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6704f26d6c4Sdrh rc = vdbeUnbind(p, i); 6714f26d6c4Sdrh if( rc==SQLITE_OK ){ 672290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 6734f26d6c4Sdrh } 6744f26d6c4Sdrh return rc; 6754f26d6c4Sdrh } 6764f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 6774f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 6784f26d6c4Sdrh } 6794f26d6c4Sdrh int sqlite3_bind_text( 6804f26d6c4Sdrh sqlite3_stmt *pStmt, 6814f26d6c4Sdrh int i, 6824f26d6c4Sdrh const char *zData, 6834f26d6c4Sdrh int nData, 684d8123366Sdanielk1977 void (*xDel)(void*) 6854f26d6c4Sdrh ){ 6865e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 6874f26d6c4Sdrh } 6886c62608fSdrh #ifndef SQLITE_OMIT_UTF16 6894f26d6c4Sdrh int sqlite3_bind_text16( 6904f26d6c4Sdrh sqlite3_stmt *pStmt, 6914f26d6c4Sdrh int i, 6924f26d6c4Sdrh const void *zData, 6934f26d6c4Sdrh int nData, 694d8123366Sdanielk1977 void (*xDel)(void*) 6954f26d6c4Sdrh ){ 6965e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 6974f26d6c4Sdrh } 6986c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 69975f6a032Sdrh 70075f6a032Sdrh /* 70175f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 70275f6a032Sdrh ** This routine is added to support DBD::SQLite. 70375f6a032Sdrh */ 70475f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 70592febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 70692febd92Sdrh return p ? p->nVar : 0; 70775f6a032Sdrh } 708895d7472Sdrh 709895d7472Sdrh /* 710fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 711fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 712fa6bc000Sdrh ** exist. 713895d7472Sdrh */ 714fa6bc000Sdrh static void createVarMap(Vdbe *p){ 715895d7472Sdrh if( !p->okVar ){ 716895d7472Sdrh int j; 717895d7472Sdrh Op *pOp; 718895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 719895d7472Sdrh if( pOp->opcode==OP_Variable ){ 720895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 721895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 722895d7472Sdrh } 723895d7472Sdrh } 724895d7472Sdrh p->okVar = 1; 725895d7472Sdrh } 726fa6bc000Sdrh } 727fa6bc000Sdrh 728fa6bc000Sdrh /* 729fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 730fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 731fa6bc000Sdrh ** 732fa6bc000Sdrh ** The result is always UTF-8. 733fa6bc000Sdrh */ 734fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 735fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 736fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 737fa6bc000Sdrh return 0; 738fa6bc000Sdrh } 739fa6bc000Sdrh createVarMap(p); 740895d7472Sdrh return p->azVar[i-1]; 741895d7472Sdrh } 742fa6bc000Sdrh 743fa6bc000Sdrh /* 744fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 745fa6bc000Sdrh ** with that name. If there is no variable with the given name, 746fa6bc000Sdrh ** return 0. 747fa6bc000Sdrh */ 748fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 749fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 750fa6bc000Sdrh int i; 751fa6bc000Sdrh if( p==0 ){ 752fa6bc000Sdrh return 0; 753fa6bc000Sdrh } 754fa6bc000Sdrh createVarMap(p); 7556a8903c3Sdrh if( zName ){ 756fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 757971a7c87Sdrh const char *z = p->azVar[i]; 7586a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 759fa6bc000Sdrh return i+1; 760fa6bc000Sdrh } 761fa6bc000Sdrh } 7626a8903c3Sdrh } 763fa6bc000Sdrh return 0; 764fa6bc000Sdrh } 765f8db1bc0Sdrh 76651942bc3Sdrh /* 767f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 768f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 769f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 770f8db1bc0Sdrh */ 771f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 772f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 773f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 774f8db1bc0Sdrh int i, rc = SQLITE_OK; 775f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 776f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 777f8db1bc0Sdrh return SQLITE_MISUSE; 778f8db1bc0Sdrh } 779f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 780f8db1bc0Sdrh return SQLITE_ERROR; 781f8db1bc0Sdrh } 782f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 783f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 784f8db1bc0Sdrh } 785f8db1bc0Sdrh return rc; 786f8db1bc0Sdrh } 78751942bc3Sdrh 78851942bc3Sdrh /* 78951942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 79051942bc3Sdrh ** in the argument belongs. This is the same database handle that was 79151942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 79251942bc3Sdrh ** the statement in the first place. 79351942bc3Sdrh */ 79451942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 79551942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 79651942bc3Sdrh } 797