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 159efaaf579Sdanielk1977 /* Assert that malloc() has not failed */ 160*9e12800dSdanielk1977 assert( !sqlite3MallocFailed() ); 161261919ccSdanielk1977 1621bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ 1634f26d6c4Sdrh return SQLITE_MISUSE; 1644f26d6c4Sdrh } 16591b48aa1Sdrh if( p->aborted ){ 16691b48aa1Sdrh return SQLITE_ABORT; 16791b48aa1Sdrh } 168a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 169a21c6b6fSdanielk1977 if( p->rc==SQLITE_OK ){ 170a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 171a21c6b6fSdanielk1977 } 172a21c6b6fSdanielk1977 return SQLITE_ERROR; 173a21c6b6fSdanielk1977 } 1744f26d6c4Sdrh db = p->db; 1754f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1764f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1774f26d6c4Sdrh return SQLITE_MISUSE; 1784f26d6c4Sdrh } 1791d850a72Sdanielk1977 if( p->pc<0 ){ 18019e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 181c16a03b5Sdrh /* Invoke the trace callback if there is one 182c16a03b5Sdrh */ 18319e2d37fSdrh if( db->xTrace && !db->init.busy ){ 184c16a03b5Sdrh assert( p->nOp>0 ); 185c16a03b5Sdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 186c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3!=0 ); 187c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 188c16a03b5Sdrh sqlite3SafetyOff(db); 189c16a03b5Sdrh db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); 190c16a03b5Sdrh if( sqlite3SafetyOn(db) ){ 191c16a03b5Sdrh p->rc = SQLITE_MISUSE; 192c16a03b5Sdrh return SQLITE_MISUSE; 193c16a03b5Sdrh } 194c16a03b5Sdrh } 19519e2d37fSdrh if( db->xProfile && !db->init.busy ){ 19619e2d37fSdrh double rNow; 19766560adaSdrh sqlite3OsCurrentTime(&rNow); 19819e2d37fSdrh p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; 19919e2d37fSdrh } 20019e2d37fSdrh #endif 201c16a03b5Sdrh 202c16a03b5Sdrh /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned 203c16a03b5Sdrh ** on in debugging mode. 204c16a03b5Sdrh */ 205c16a03b5Sdrh #ifdef SQLITE_DEBUG 206c16a03b5Sdrh if( (db->flags & SQLITE_SqlTrace)!=0 ){ 207c16a03b5Sdrh sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); 208c16a03b5Sdrh } 209c16a03b5Sdrh #endif /* SQLITE_DEBUG */ 210c16a03b5Sdrh 2111d850a72Sdanielk1977 db->activeVdbeCnt++; 2121d850a72Sdanielk1977 p->pc = 0; 2131d850a72Sdanielk1977 } 214b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 2154f26d6c4Sdrh if( p->explain ){ 2164f26d6c4Sdrh rc = sqlite3VdbeList(p); 217b7f9164eSdrh }else 218b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 219b7f9164eSdrh { 2204f26d6c4Sdrh rc = sqlite3VdbeExec(p); 2214f26d6c4Sdrh } 2224f26d6c4Sdrh 2234f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 2244f26d6c4Sdrh rc = SQLITE_MISUSE; 2254f26d6c4Sdrh } 2264f26d6c4Sdrh 22719e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 22819e2d37fSdrh /* Invoke the profile callback if there is one 22919e2d37fSdrh */ 23019e2d37fSdrh if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ 23119e2d37fSdrh double rNow; 23219e2d37fSdrh u64 elapseTime; 23319e2d37fSdrh 23466560adaSdrh sqlite3OsCurrentTime(&rNow); 23519e2d37fSdrh elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; 23619e2d37fSdrh assert( p->nOp>0 ); 23719e2d37fSdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 23819e2d37fSdrh assert( p->aOp[p->nOp-1].p3!=0 ); 23919e2d37fSdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 24019e2d37fSdrh db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); 24119e2d37fSdrh } 24219e2d37fSdrh #endif 24319e2d37fSdrh 244130b9f40Sdrh sqlite3Error(p->db, rc, p->zErrMsg ? "%s" : 0, p->zErrMsg); 24554f0198eSdanielk1977 return sqlite3ApiExit(p->db, 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 */ 40754f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 40854f0198eSdanielk1977 p->rc = sqlite3ApiExit(0, p->rc); 4092e588c75Sdanielk1977 } 4102e588c75Sdanielk1977 4114f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 4124f26d6c4Sdrh ** The following routines are used to access elements of the current row 4134f26d6c4Sdrh ** in the result set. 4144f26d6c4Sdrh */ 415c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 4162e588c75Sdanielk1977 const void *val; 4172e588c75Sdanielk1977 sqlite3MallocDisallow(); 4182e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 4192e588c75Sdanielk1977 sqlite3MallocAllow(); 4202e588c75Sdanielk1977 return val; 421c572ef7fSdanielk1977 } 4224f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 4232e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 4242e588c75Sdanielk1977 columnMallocFailure(pStmt); 4252e588c75Sdanielk1977 return val; 4264f26d6c4Sdrh } 4274f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 4282e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 4292e588c75Sdanielk1977 columnMallocFailure(pStmt); 4302e588c75Sdanielk1977 return val; 4314f26d6c4Sdrh } 4324f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 4332e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 4342e588c75Sdanielk1977 columnMallocFailure(pStmt); 4352e588c75Sdanielk1977 return val; 4364f26d6c4Sdrh } 4374f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 4382e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 4392e588c75Sdanielk1977 columnMallocFailure(pStmt); 4402e588c75Sdanielk1977 return val; 4414f26d6c4Sdrh } 442efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 4432e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 4442e588c75Sdanielk1977 columnMallocFailure(pStmt); 4452e588c75Sdanielk1977 return val; 4464f26d6c4Sdrh } 4474f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 4482e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 4492e588c75Sdanielk1977 columnMallocFailure(pStmt); 4502e588c75Sdanielk1977 return val; 4514f26d6c4Sdrh } 452d1e4733dSdrh #if 0 453d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 454d1e4733dSdrh return columnMem(pStmt, i); 455d1e4733dSdrh } 456d1e4733dSdrh #endif 4576c62608fSdrh #ifndef SQLITE_OMIT_UTF16 4584f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 4592e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 4602e588c75Sdanielk1977 columnMallocFailure(pStmt); 4612e588c75Sdanielk1977 return val; 4624f26d6c4Sdrh } 4636c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 4644f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 4654f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 4664f26d6c4Sdrh } 4674f26d6c4Sdrh 4685e2517e1Sdrh /* 4695e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 4705e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 471e590fbdeSdrh ** 472e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 473e590fbdeSdrh ** name is returned. Here are the names: 474e590fbdeSdrh ** 475e590fbdeSdrh ** 0 The column name as it should be displayed for output 476e590fbdeSdrh ** 1 The datatype name for the column 477e590fbdeSdrh ** 2 The name of the database that the column derives from 478e590fbdeSdrh ** 3 The name of the table that the column derives from 479e590fbdeSdrh ** 4 The name of the table column that the result column derives from 480e590fbdeSdrh ** 481e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 482e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 4835e2517e1Sdrh */ 4845e2517e1Sdrh static const void *columnName( 4855e2517e1Sdrh sqlite3_stmt *pStmt, 4865e2517e1Sdrh int N, 4875e2517e1Sdrh const void *(*xFunc)(Mem*), 4885e2517e1Sdrh int useType 4895e2517e1Sdrh ){ 49000fd957bSdanielk1977 const void *ret; 4915e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 4925e2517e1Sdrh int n = sqlite3_column_count(pStmt); 4935e2517e1Sdrh 4945e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 4955e2517e1Sdrh return 0; 4965e2517e1Sdrh } 497e590fbdeSdrh N += useType*n; 49800fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 49900fd957bSdanielk1977 50000fd957bSdanielk1977 /* A malloc may have failed inside of the xFunc() call. If this is the case, 50100fd957bSdanielk1977 ** clear the mallocFailed flag and return NULL. 50200fd957bSdanielk1977 */ 50354f0198eSdanielk1977 sqlite3ApiExit(0, 0); 50400fd957bSdanielk1977 return ret; 5055e2517e1Sdrh } 5065e2517e1Sdrh 5074f26d6c4Sdrh /* 5084f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 5094f26d6c4Sdrh ** statement pStmt. 5104f26d6c4Sdrh */ 5114f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 5125e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 0); 5134f26d6c4Sdrh } 514e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 515e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 516e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 0); 517e590fbdeSdrh } 518e590fbdeSdrh #endif 5194f26d6c4Sdrh 5204f26d6c4Sdrh /* 5216c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 522e590fbdeSdrh ** of the result set of SQL statement pStmt. 5236c62608fSdrh */ 5246c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 5256c62608fSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 1); 5266c62608fSdrh } 5276c62608fSdrh #ifndef SQLITE_OMIT_UTF16 52876d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 5295e2517e1Sdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 1); 5304f26d6c4Sdrh } 5316c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 5324f26d6c4Sdrh 533e590fbdeSdrh #if !defined(SQLITE_OMIT_ORIGIN_NAMES) && 0 534e590fbdeSdrh /* 535e590fbdeSdrh ** Return the name of the database from which a result column derives. 536e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 537e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 538e590fbdeSdrh */ 539e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 540e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 2); 541e590fbdeSdrh } 542e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 543e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 544e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 2); 545e590fbdeSdrh } 546e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 547e590fbdeSdrh 548e590fbdeSdrh /* 549e590fbdeSdrh ** Return the name of the table from which a result column derives. 550e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 551e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 552e590fbdeSdrh */ 553e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 554e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 3); 555e590fbdeSdrh } 556e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 557e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 558e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 3); 559e590fbdeSdrh } 560e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 561e590fbdeSdrh 562e590fbdeSdrh /* 563e590fbdeSdrh ** Return the name of the table column from which a result column derives. 564e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 565e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 566e590fbdeSdrh */ 567e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 568e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, 4); 569e590fbdeSdrh } 570e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 571e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 572e590fbdeSdrh return columnName(pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, 4); 573e590fbdeSdrh } 574e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 575e590fbdeSdrh #endif /* SQLITE_OMIT_ORIGIN_NAMES */ 576e590fbdeSdrh 577e590fbdeSdrh 578e590fbdeSdrh 579e590fbdeSdrh 5804f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 5814f26d6c4Sdrh ** 5824f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 5834f26d6c4Sdrh */ 5844f26d6c4Sdrh /* 5854f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 5864f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 5874f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 5884f26d6c4Sdrh ** 5894f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 5904f26d6c4Sdrh ** value in any case. 5914f26d6c4Sdrh */ 5924f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 5934f26d6c4Sdrh Mem *pVar; 5941bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 595473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 5964f26d6c4Sdrh return SQLITE_MISUSE; 5974f26d6c4Sdrh } 5984f26d6c4Sdrh if( i<1 || i>p->nVar ){ 5994f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 6004f26d6c4Sdrh return SQLITE_RANGE; 6014f26d6c4Sdrh } 6024f26d6c4Sdrh i--; 603290c1948Sdrh pVar = &p->aVar[i]; 604d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 6054f26d6c4Sdrh pVar->flags = MEM_Null; 6064f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 6074f26d6c4Sdrh return SQLITE_OK; 6084f26d6c4Sdrh } 6094f26d6c4Sdrh 6104f26d6c4Sdrh /* 6115e2517e1Sdrh ** Bind a text or BLOB value. 6124f26d6c4Sdrh */ 6135e2517e1Sdrh static int bindText( 614f4479501Sdrh sqlite3_stmt *pStmt, 6154f26d6c4Sdrh int i, 6164f26d6c4Sdrh const void *zData, 6174f26d6c4Sdrh int nData, 6185e2517e1Sdrh void (*xDel)(void*), 6195e2517e1Sdrh int encoding 6204f26d6c4Sdrh ){ 6214f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6224f26d6c4Sdrh Mem *pVar; 6234f26d6c4Sdrh int rc; 6244f26d6c4Sdrh 6254f26d6c4Sdrh rc = vdbeUnbind(p, i); 626feac5f8dSdrh if( rc || zData==0 ){ 6274f26d6c4Sdrh return rc; 6284f26d6c4Sdrh } 629290c1948Sdrh pVar = &p->aVar[i-1]; 6305e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 6315e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 63214db2665Sdanielk1977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 6335e2517e1Sdrh } 63454f0198eSdanielk1977 635771151b6Sdanielk1977 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); 63654f0198eSdanielk1977 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); 6375e2517e1Sdrh } 6385e2517e1Sdrh 6395e2517e1Sdrh 6405e2517e1Sdrh /* 6415e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 6425e2517e1Sdrh */ 6435e2517e1Sdrh int sqlite3_bind_blob( 6445e2517e1Sdrh sqlite3_stmt *pStmt, 6455e2517e1Sdrh int i, 6465e2517e1Sdrh const void *zData, 6475e2517e1Sdrh int nData, 6485e2517e1Sdrh void (*xDel)(void*) 6495e2517e1Sdrh ){ 6505e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 6515e2517e1Sdrh } 6524f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 6534f26d6c4Sdrh int rc; 6544f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6554f26d6c4Sdrh rc = vdbeUnbind(p, i); 6564f26d6c4Sdrh if( rc==SQLITE_OK ){ 657290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 6584f26d6c4Sdrh } 659f4618891Sdanielk1977 return rc; 6604f26d6c4Sdrh } 6614f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 662efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 6634f26d6c4Sdrh } 664efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 6654f26d6c4Sdrh int rc; 6664f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 6674f26d6c4Sdrh rc = vdbeUnbind(p, i); 6684f26d6c4Sdrh if( rc==SQLITE_OK ){ 669290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 6704f26d6c4Sdrh } 6714f26d6c4Sdrh return rc; 6724f26d6c4Sdrh } 6734f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 6744f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 6754f26d6c4Sdrh } 6764f26d6c4Sdrh int sqlite3_bind_text( 6774f26d6c4Sdrh sqlite3_stmt *pStmt, 6784f26d6c4Sdrh int i, 6794f26d6c4Sdrh const char *zData, 6804f26d6c4Sdrh int nData, 681d8123366Sdanielk1977 void (*xDel)(void*) 6824f26d6c4Sdrh ){ 6835e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 6844f26d6c4Sdrh } 6856c62608fSdrh #ifndef SQLITE_OMIT_UTF16 6864f26d6c4Sdrh int sqlite3_bind_text16( 6874f26d6c4Sdrh sqlite3_stmt *pStmt, 6884f26d6c4Sdrh int i, 6894f26d6c4Sdrh const void *zData, 6904f26d6c4Sdrh int nData, 691d8123366Sdanielk1977 void (*xDel)(void*) 6924f26d6c4Sdrh ){ 6935e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 6944f26d6c4Sdrh } 6956c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 69675f6a032Sdrh 69775f6a032Sdrh /* 69875f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 69975f6a032Sdrh ** This routine is added to support DBD::SQLite. 70075f6a032Sdrh */ 70175f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 70292febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 70392febd92Sdrh return p ? p->nVar : 0; 70475f6a032Sdrh } 705895d7472Sdrh 706895d7472Sdrh /* 707fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 708fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 709fa6bc000Sdrh ** exist. 710895d7472Sdrh */ 711fa6bc000Sdrh static void createVarMap(Vdbe *p){ 712895d7472Sdrh if( !p->okVar ){ 713895d7472Sdrh int j; 714895d7472Sdrh Op *pOp; 715895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 716895d7472Sdrh if( pOp->opcode==OP_Variable ){ 717895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 718895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 719895d7472Sdrh } 720895d7472Sdrh } 721895d7472Sdrh p->okVar = 1; 722895d7472Sdrh } 723fa6bc000Sdrh } 724fa6bc000Sdrh 725fa6bc000Sdrh /* 726fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 727fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 728fa6bc000Sdrh ** 729fa6bc000Sdrh ** The result is always UTF-8. 730fa6bc000Sdrh */ 731fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 732fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 733fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 734fa6bc000Sdrh return 0; 735fa6bc000Sdrh } 736fa6bc000Sdrh createVarMap(p); 737895d7472Sdrh return p->azVar[i-1]; 738895d7472Sdrh } 739fa6bc000Sdrh 740fa6bc000Sdrh /* 741fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 742fa6bc000Sdrh ** with that name. If there is no variable with the given name, 743fa6bc000Sdrh ** return 0. 744fa6bc000Sdrh */ 745fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 746fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 747fa6bc000Sdrh int i; 748fa6bc000Sdrh if( p==0 ){ 749fa6bc000Sdrh return 0; 750fa6bc000Sdrh } 751fa6bc000Sdrh createVarMap(p); 7526a8903c3Sdrh if( zName ){ 753fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 754971a7c87Sdrh const char *z = p->azVar[i]; 7556a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 756fa6bc000Sdrh return i+1; 757fa6bc000Sdrh } 758fa6bc000Sdrh } 7596a8903c3Sdrh } 760fa6bc000Sdrh return 0; 761fa6bc000Sdrh } 762f8db1bc0Sdrh 76351942bc3Sdrh /* 764f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 765f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 766f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 767f8db1bc0Sdrh */ 768f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 769f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 770f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 771f8db1bc0Sdrh int i, rc = SQLITE_OK; 772f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 773f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 774f8db1bc0Sdrh return SQLITE_MISUSE; 775f8db1bc0Sdrh } 776f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 777f8db1bc0Sdrh return SQLITE_ERROR; 778f8db1bc0Sdrh } 779f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 780950f054cSdanielk1977 sqlite3MallocDisallow(); 781f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 782950f054cSdanielk1977 sqlite3MallocAllow(); 783f8db1bc0Sdrh } 784f8db1bc0Sdrh return rc; 785f8db1bc0Sdrh } 78651942bc3Sdrh 78751942bc3Sdrh /* 78851942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 78951942bc3Sdrh ** in the argument belongs. This is the same database handle that was 79051942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 79151942bc3Sdrh ** the statement in the first place. 79251942bc3Sdrh */ 79351942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 79451942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 79551942bc3Sdrh } 796