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 } 7729d72108Sdrh /* sqlite3_value_numeric_type() defined in vdbe.c */ 784f26d6c4Sdrh 794f26d6c4Sdrh /**************************** sqlite3_result_ ******************************* 804f26d6c4Sdrh ** The following routines are used by user-defined functions to specify 814f26d6c4Sdrh ** the function result. 824f26d6c4Sdrh */ 834f26d6c4Sdrh void sqlite3_result_blob( 844f26d6c4Sdrh sqlite3_context *pCtx, 854f26d6c4Sdrh const void *z, 864f26d6c4Sdrh int n, 87d8123366Sdanielk1977 void (*xDel)(void *) 884f26d6c4Sdrh ){ 895708d2deSdrh assert( n>=0 ); 90d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, 0, xDel); 914f26d6c4Sdrh } 924f26d6c4Sdrh void sqlite3_result_double(sqlite3_context *pCtx, double rVal){ 934f26d6c4Sdrh sqlite3VdbeMemSetDouble(&pCtx->s, rVal); 944f26d6c4Sdrh } 954f26d6c4Sdrh void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){ 964f26d6c4Sdrh pCtx->isError = 1; 97d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT); 984f26d6c4Sdrh } 99a1686c9aSdanielk1977 #ifndef SQLITE_OMIT_UTF16 1004f26d6c4Sdrh void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){ 1014f26d6c4Sdrh pCtx->isError = 1; 102d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT); 1034f26d6c4Sdrh } 104a1686c9aSdanielk1977 #endif 105f4479501Sdrh void sqlite3_result_int(sqlite3_context *pCtx, int iVal){ 1064f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal); 1074f26d6c4Sdrh } 1084f26d6c4Sdrh void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){ 1094f26d6c4Sdrh sqlite3VdbeMemSetInt64(&pCtx->s, iVal); 1104f26d6c4Sdrh } 1114f26d6c4Sdrh void sqlite3_result_null(sqlite3_context *pCtx){ 112f4479501Sdrh sqlite3VdbeMemSetNull(&pCtx->s); 1134f26d6c4Sdrh } 1144f26d6c4Sdrh void sqlite3_result_text( 1154f26d6c4Sdrh sqlite3_context *pCtx, 1164f26d6c4Sdrh const char *z, 1174f26d6c4Sdrh int n, 118d8123366Sdanielk1977 void (*xDel)(void *) 1194f26d6c4Sdrh ){ 120d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, xDel); 1214f26d6c4Sdrh } 1226c62608fSdrh #ifndef SQLITE_OMIT_UTF16 1234f26d6c4Sdrh void sqlite3_result_text16( 1244f26d6c4Sdrh sqlite3_context *pCtx, 1254f26d6c4Sdrh const void *z, 1264f26d6c4Sdrh int n, 127d8123366Sdanielk1977 void (*xDel)(void *) 1284f26d6c4Sdrh ){ 129d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, xDel); 130d8123366Sdanielk1977 } 131d8123366Sdanielk1977 void sqlite3_result_text16be( 132d8123366Sdanielk1977 sqlite3_context *pCtx, 133d8123366Sdanielk1977 const void *z, 134d8123366Sdanielk1977 int n, 135d8123366Sdanielk1977 void (*xDel)(void *) 136d8123366Sdanielk1977 ){ 137d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16BE, xDel); 138d8123366Sdanielk1977 } 139d8123366Sdanielk1977 void sqlite3_result_text16le( 140d8123366Sdanielk1977 sqlite3_context *pCtx, 141d8123366Sdanielk1977 const void *z, 142d8123366Sdanielk1977 int n, 143d8123366Sdanielk1977 void (*xDel)(void *) 144d8123366Sdanielk1977 ){ 145d8123366Sdanielk1977 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16LE, xDel); 1464f26d6c4Sdrh } 1476c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 1484f26d6c4Sdrh void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){ 1494f26d6c4Sdrh sqlite3VdbeMemCopy(&pCtx->s, pValue); 1504f26d6c4Sdrh } 1514f26d6c4Sdrh 1524f26d6c4Sdrh 1534f26d6c4Sdrh /* 1544f26d6c4Sdrh ** Execute the statement pStmt, either until a row of data is ready, the 1554f26d6c4Sdrh ** statement is completely executed or an error occurs. 156b900aaf3Sdrh ** 157b900aaf3Sdrh ** This routine implements the bulk of the logic behind the sqlite_step() 158b900aaf3Sdrh ** API. The only thing omitted is the automatic recompile if a 159b900aaf3Sdrh ** schema change has occurred. That detail is handled by the 160b900aaf3Sdrh ** outer sqlite3_step() wrapper procedure. 1614f26d6c4Sdrh */ 162b900aaf3Sdrh static int sqlite3Step(Vdbe *p){ 1639bb575fdSdrh sqlite3 *db; 1644f26d6c4Sdrh int rc; 1654f26d6c4Sdrh 166efaaf579Sdanielk1977 /* Assert that malloc() has not failed */ 1679e12800dSdanielk1977 assert( !sqlite3MallocFailed() ); 168261919ccSdanielk1977 1691bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN ){ 1704f26d6c4Sdrh return SQLITE_MISUSE; 1714f26d6c4Sdrh } 17291b48aa1Sdrh if( p->aborted ){ 17391b48aa1Sdrh return SQLITE_ABORT; 17491b48aa1Sdrh } 175a21c6b6fSdanielk1977 if( p->pc<=0 && p->expired ){ 176a21c6b6fSdanielk1977 if( p->rc==SQLITE_OK ){ 177a21c6b6fSdanielk1977 p->rc = SQLITE_SCHEMA; 178a21c6b6fSdanielk1977 } 179b900aaf3Sdrh rc = SQLITE_ERROR; 180b900aaf3Sdrh goto end_of_step; 181a21c6b6fSdanielk1977 } 1824f26d6c4Sdrh db = p->db; 1834f26d6c4Sdrh if( sqlite3SafetyOn(db) ){ 1844f26d6c4Sdrh p->rc = SQLITE_MISUSE; 1854f26d6c4Sdrh return SQLITE_MISUSE; 1864f26d6c4Sdrh } 1871d850a72Sdanielk1977 if( p->pc<0 ){ 18815ca1df1Sdrh /* If there are no other statements currently running, then 18915ca1df1Sdrh ** reset the interrupt flag. This prevents a call to sqlite3_interrupt 19015ca1df1Sdrh ** from interrupting a statement that has not yet started. 19115ca1df1Sdrh */ 19215ca1df1Sdrh if( db->activeVdbeCnt==0 ){ 19315ca1df1Sdrh db->u1.isInterrupted = 0; 19415ca1df1Sdrh } 19515ca1df1Sdrh 19619e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 197c16a03b5Sdrh /* Invoke the trace callback if there is one 198c16a03b5Sdrh */ 19919e2d37fSdrh if( db->xTrace && !db->init.busy ){ 200c16a03b5Sdrh assert( p->nOp>0 ); 201c16a03b5Sdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 202c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3!=0 ); 203c16a03b5Sdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 204c16a03b5Sdrh sqlite3SafetyOff(db); 205c16a03b5Sdrh db->xTrace(db->pTraceArg, p->aOp[p->nOp-1].p3); 206c16a03b5Sdrh if( sqlite3SafetyOn(db) ){ 207c16a03b5Sdrh p->rc = SQLITE_MISUSE; 208c16a03b5Sdrh return SQLITE_MISUSE; 209c16a03b5Sdrh } 210c16a03b5Sdrh } 21119e2d37fSdrh if( db->xProfile && !db->init.busy ){ 21219e2d37fSdrh double rNow; 21366560adaSdrh sqlite3OsCurrentTime(&rNow); 21419e2d37fSdrh p->startTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0; 21519e2d37fSdrh } 21619e2d37fSdrh #endif 217c16a03b5Sdrh 218c16a03b5Sdrh /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned 219c16a03b5Sdrh ** on in debugging mode. 220c16a03b5Sdrh */ 221c16a03b5Sdrh #ifdef SQLITE_DEBUG 222c16a03b5Sdrh if( (db->flags & SQLITE_SqlTrace)!=0 ){ 223c16a03b5Sdrh sqlite3DebugPrintf("SQL-trace: %s\n", p->aOp[p->nOp-1].p3); 224c16a03b5Sdrh } 225c16a03b5Sdrh #endif /* SQLITE_DEBUG */ 226c16a03b5Sdrh 2271d850a72Sdanielk1977 db->activeVdbeCnt++; 2281d850a72Sdanielk1977 p->pc = 0; 2291d850a72Sdanielk1977 } 230b7f9164eSdrh #ifndef SQLITE_OMIT_EXPLAIN 2314f26d6c4Sdrh if( p->explain ){ 2324f26d6c4Sdrh rc = sqlite3VdbeList(p); 233b7f9164eSdrh }else 234b7f9164eSdrh #endif /* SQLITE_OMIT_EXPLAIN */ 235b7f9164eSdrh { 2364f26d6c4Sdrh rc = sqlite3VdbeExec(p); 2374f26d6c4Sdrh } 2384f26d6c4Sdrh 2394f26d6c4Sdrh if( sqlite3SafetyOff(db) ){ 2404f26d6c4Sdrh rc = SQLITE_MISUSE; 2414f26d6c4Sdrh } 2424f26d6c4Sdrh 24319e2d37fSdrh #ifndef SQLITE_OMIT_TRACE 24419e2d37fSdrh /* Invoke the profile callback if there is one 24519e2d37fSdrh */ 24619e2d37fSdrh if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy ){ 24719e2d37fSdrh double rNow; 24819e2d37fSdrh u64 elapseTime; 24919e2d37fSdrh 25066560adaSdrh sqlite3OsCurrentTime(&rNow); 25119e2d37fSdrh elapseTime = (rNow - (int)rNow)*3600.0*24.0*1000000000.0 - p->startTime; 25219e2d37fSdrh assert( p->nOp>0 ); 25319e2d37fSdrh assert( p->aOp[p->nOp-1].opcode==OP_Noop ); 25419e2d37fSdrh assert( p->aOp[p->nOp-1].p3!=0 ); 25519e2d37fSdrh assert( p->aOp[p->nOp-1].p3type==P3_DYNAMIC ); 25619e2d37fSdrh db->xProfile(db->pProfileArg, p->aOp[p->nOp-1].p3, elapseTime); 25719e2d37fSdrh } 25819e2d37fSdrh #endif 25919e2d37fSdrh 26097a227c9Sdanielk1977 sqlite3Error(p->db, rc, 0); 26176e8d1a2Sdanielk1977 p->rc = sqlite3ApiExit(p->db, p->rc); 262b900aaf3Sdrh end_of_step: 2634ac285a1Sdrh assert( (rc&0xff)==rc ); 264b900aaf3Sdrh if( p->zSql && (rc&0xff)<SQLITE_ROW ){ 265b900aaf3Sdrh /* This behavior occurs if sqlite3_prepare_v2() was used to build 266b900aaf3Sdrh ** the prepared statement. Return error codes directly */ 267b900aaf3Sdrh return p->rc; 268b900aaf3Sdrh }else{ 269b900aaf3Sdrh /* This is for legacy sqlite3_prepare() builds and when the code 270b900aaf3Sdrh ** is SQLITE_ROW or SQLITE_DONE */ 27176e8d1a2Sdanielk1977 return rc; 2724f26d6c4Sdrh } 273b900aaf3Sdrh } 274b900aaf3Sdrh 275b900aaf3Sdrh /* 276b900aaf3Sdrh ** This is the top-level implementation of sqlite3_step(). Call 277b900aaf3Sdrh ** sqlite3Step() to do most of the work. If a schema error occurs, 278b900aaf3Sdrh ** call sqlite3Reprepare() and try again. 279b900aaf3Sdrh */ 280b900aaf3Sdrh #ifdef SQLITE_OMIT_PARSER 281b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 282b900aaf3Sdrh return sqlite3Step((Vdbe*)pStmt); 283b900aaf3Sdrh } 284b900aaf3Sdrh #else 285b900aaf3Sdrh int sqlite3_step(sqlite3_stmt *pStmt){ 286b900aaf3Sdrh int cnt = 0; 287b900aaf3Sdrh int rc; 288b900aaf3Sdrh Vdbe *v = (Vdbe*)pStmt; 289b900aaf3Sdrh while( (rc = sqlite3Step(v))==SQLITE_SCHEMA 290b900aaf3Sdrh && cnt++ < 5 291b900aaf3Sdrh && sqlite3Reprepare(v) ){ 292b900aaf3Sdrh sqlite3_reset(pStmt); 293b900aaf3Sdrh v->expired = 0; 294b900aaf3Sdrh } 295b900aaf3Sdrh return rc; 296b900aaf3Sdrh } 297b900aaf3Sdrh #endif 2984f26d6c4Sdrh 2994f26d6c4Sdrh /* 300eb2e176aSdrh ** Extract the user data from a sqlite3_context structure and return a 301eb2e176aSdrh ** pointer to it. 302eb2e176aSdrh */ 303eb2e176aSdrh void *sqlite3_user_data(sqlite3_context *p){ 304eb2e176aSdrh assert( p && p->pFunc ); 305eb2e176aSdrh return p->pFunc->pUserData; 306eb2e176aSdrh } 307eb2e176aSdrh 308eb2e176aSdrh /* 309b7481e70Sdrh ** The following is the implementation of an SQL function that always 310b7481e70Sdrh ** fails with an error message stating that the function is used in the 311b7481e70Sdrh ** wrong context. The sqlite3_overload_function() API might construct 312b7481e70Sdrh ** SQL function that use this routine so that the functions will exist 313b7481e70Sdrh ** for name resolution but are actually overloaded by the xFindFunction 314b7481e70Sdrh ** method of virtual tables. 315b7481e70Sdrh */ 316b7481e70Sdrh void sqlite3InvalidFunction( 317b7481e70Sdrh sqlite3_context *context, /* The function calling context */ 318b7481e70Sdrh int argc, /* Number of arguments to the function */ 319b7481e70Sdrh sqlite3_value **argv /* Value of each argument */ 320b7481e70Sdrh ){ 321b7481e70Sdrh const char *zName = context->pFunc->zName; 322b7481e70Sdrh char *zErr; 323b7481e70Sdrh zErr = sqlite3MPrintf( 324b7481e70Sdrh "unable to use function %s in the requested context", zName); 325b7481e70Sdrh sqlite3_result_error(context, zErr, -1); 326b7481e70Sdrh sqliteFree(zErr); 327b7481e70Sdrh } 328b7481e70Sdrh 329b7481e70Sdrh /* 330eb2e176aSdrh ** Allocate or return the aggregate context for a user function. A new 331eb2e176aSdrh ** context is allocated on the first call. Subsequent calls return the 332eb2e176aSdrh ** same context that was returned on prior calls. 333eb2e176aSdrh */ 334eb2e176aSdrh void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){ 335abfcea25Sdrh Mem *pMem = p->pMem; 336825c662eSdrh assert( p && p->pFunc && p->pFunc->xStep ); 337a10a34b8Sdrh if( (pMem->flags & MEM_Agg)==0 ){ 338a10a34b8Sdrh if( nByte==0 ){ 339a10a34b8Sdrh assert( pMem->flags==MEM_Null ); 340a10a34b8Sdrh pMem->z = 0; 341a10a34b8Sdrh }else{ 342abfcea25Sdrh pMem->flags = MEM_Agg; 3435360ad34Sdrh pMem->xDel = sqlite3FreeX; 3443c024d69Sdrh pMem->u.pDef = p->pFunc; 345eb2e176aSdrh if( nByte<=NBFS ){ 346abfcea25Sdrh pMem->z = pMem->zShort; 347abfcea25Sdrh memset(pMem->z, 0, nByte); 348eb2e176aSdrh }else{ 349abfcea25Sdrh pMem->z = sqliteMalloc( nByte ); 350eb2e176aSdrh } 351eb2e176aSdrh } 352a10a34b8Sdrh } 353abfcea25Sdrh return (void*)pMem->z; 354eb2e176aSdrh } 355eb2e176aSdrh 356eb2e176aSdrh /* 357682f68b0Sdanielk1977 ** Return the auxilary data pointer, if any, for the iArg'th argument to 358682f68b0Sdanielk1977 ** the user-function defined by pCtx. 359682f68b0Sdanielk1977 */ 360682f68b0Sdanielk1977 void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){ 361682f68b0Sdanielk1977 VdbeFunc *pVdbeFunc = pCtx->pVdbeFunc; 362682f68b0Sdanielk1977 if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){ 363682f68b0Sdanielk1977 return 0; 364682f68b0Sdanielk1977 } 365f92c7ff7Sdrh return pVdbeFunc->apAux[iArg].pAux; 366682f68b0Sdanielk1977 } 367682f68b0Sdanielk1977 368682f68b0Sdanielk1977 /* 369682f68b0Sdanielk1977 ** Set the auxilary data pointer and delete function, for the iArg'th 370682f68b0Sdanielk1977 ** argument to the user-function defined by pCtx. Any previous value is 371682f68b0Sdanielk1977 ** deleted by calling the delete function specified when it was set. 372682f68b0Sdanielk1977 */ 373682f68b0Sdanielk1977 void sqlite3_set_auxdata( 374682f68b0Sdanielk1977 sqlite3_context *pCtx, 375682f68b0Sdanielk1977 int iArg, 376682f68b0Sdanielk1977 void *pAux, 377682f68b0Sdanielk1977 void (*xDelete)(void*) 378682f68b0Sdanielk1977 ){ 379682f68b0Sdanielk1977 struct AuxData *pAuxData; 380f92c7ff7Sdrh VdbeFunc *pVdbeFunc; 381682f68b0Sdanielk1977 if( iArg<0 ) return; 382682f68b0Sdanielk1977 383f92c7ff7Sdrh pVdbeFunc = pCtx->pVdbeFunc; 384f92c7ff7Sdrh if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){ 385998da3a2Sdrh int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg; 38653f733c7Sdrh pVdbeFunc = sqliteRealloc(pVdbeFunc, nMalloc); 387998da3a2Sdrh if( !pVdbeFunc ) return; 38853f733c7Sdrh pCtx->pVdbeFunc = pVdbeFunc; 3890e3d7476Sdrh memset(&pVdbeFunc->apAux[pVdbeFunc->nAux], 0, 3900e3d7476Sdrh sizeof(struct AuxData)*(iArg+1-pVdbeFunc->nAux)); 391998da3a2Sdrh pVdbeFunc->nAux = iArg+1; 392998da3a2Sdrh pVdbeFunc->pFunc = pCtx->pFunc; 393682f68b0Sdanielk1977 } 394682f68b0Sdanielk1977 395f92c7ff7Sdrh pAuxData = &pVdbeFunc->apAux[iArg]; 396682f68b0Sdanielk1977 if( pAuxData->pAux && pAuxData->xDelete ){ 397682f68b0Sdanielk1977 pAuxData->xDelete(pAuxData->pAux); 398682f68b0Sdanielk1977 } 399682f68b0Sdanielk1977 pAuxData->pAux = pAux; 400682f68b0Sdanielk1977 pAuxData->xDelete = xDelete; 401682f68b0Sdanielk1977 } 402682f68b0Sdanielk1977 403682f68b0Sdanielk1977 /* 404eb2e176aSdrh ** Return the number of times the Step function of a aggregate has been 405eb2e176aSdrh ** called. 406eb2e176aSdrh ** 407cf85a51cSdrh ** This function is deprecated. Do not use it for new code. It is 408cf85a51cSdrh ** provide only to avoid breaking legacy code. New aggregate function 409cf85a51cSdrh ** implementations should keep their own counts within their aggregate 410cf85a51cSdrh ** context. 411eb2e176aSdrh */ 412eb2e176aSdrh int sqlite3_aggregate_count(sqlite3_context *p){ 413eb2e176aSdrh assert( p && p->pFunc && p->pFunc->xStep ); 414abfcea25Sdrh return p->pMem->n; 415eb2e176aSdrh } 416eb2e176aSdrh 417eb2e176aSdrh /* 4184f26d6c4Sdrh ** Return the number of columns in the result set for the statement pStmt. 4194f26d6c4Sdrh */ 4204f26d6c4Sdrh int sqlite3_column_count(sqlite3_stmt *pStmt){ 4214f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4221bcdb0c0Sdrh return pVm ? pVm->nResColumn : 0; 4234f26d6c4Sdrh } 4244f26d6c4Sdrh 4254f26d6c4Sdrh /* 4264f26d6c4Sdrh ** Return the number of values available from the current row of the 4274f26d6c4Sdrh ** currently executing statement pStmt. 4284f26d6c4Sdrh */ 4294f26d6c4Sdrh int sqlite3_data_count(sqlite3_stmt *pStmt){ 4304f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4311bcdb0c0Sdrh if( pVm==0 || !pVm->resOnStack ) return 0; 4324f26d6c4Sdrh return pVm->nResColumn; 4334f26d6c4Sdrh } 4344f26d6c4Sdrh 4354f26d6c4Sdrh 4364f26d6c4Sdrh /* 4374f26d6c4Sdrh ** Check to see if column iCol of the given statement is valid. If 4384f26d6c4Sdrh ** it is, return a pointer to the Mem for the value of that column. 4394f26d6c4Sdrh ** If iCol is not valid, return a pointer to a Mem which has a value 4404f26d6c4Sdrh ** of NULL. 4414f26d6c4Sdrh */ 4424f26d6c4Sdrh static Mem *columnMem(sqlite3_stmt *pStmt, int i){ 4434f26d6c4Sdrh Vdbe *pVm = (Vdbe *)pStmt; 4444f26d6c4Sdrh int vals = sqlite3_data_count(pStmt); 4454f26d6c4Sdrh if( i>=vals || i<0 ){ 446*7a521cfbSdrh static const Mem nullMem = {{0}, 0.0, "", 0, MEM_Null, SQLITE_NULL }; 4474f26d6c4Sdrh sqlite3Error(pVm->db, SQLITE_RANGE, 0); 4480f7eb611Sdrh return (Mem*)&nullMem; 4494f26d6c4Sdrh } 4504f26d6c4Sdrh return &pVm->pTos[(1-vals)+i]; 4514f26d6c4Sdrh } 4524f26d6c4Sdrh 4532e588c75Sdanielk1977 /* 4542e588c75Sdanielk1977 ** This function is called after invoking an sqlite3_value_XXX function on a 4552e588c75Sdanielk1977 ** column value (i.e. a value returned by evaluating an SQL expression in the 4562e588c75Sdanielk1977 ** select list of a SELECT statement) that may cause a malloc() failure. If 4572e588c75Sdanielk1977 ** malloc() has failed, the threads mallocFailed flag is cleared and the result 4582e588c75Sdanielk1977 ** code of statement pStmt set to SQLITE_NOMEM. 4592e588c75Sdanielk1977 ** 4602e588c75Sdanielk1977 ** Specificly, this is called from within: 4612e588c75Sdanielk1977 ** 4622e588c75Sdanielk1977 ** sqlite3_column_int() 4632e588c75Sdanielk1977 ** sqlite3_column_int64() 4642e588c75Sdanielk1977 ** sqlite3_column_text() 4652e588c75Sdanielk1977 ** sqlite3_column_text16() 4662e588c75Sdanielk1977 ** sqlite3_column_real() 4672e588c75Sdanielk1977 ** sqlite3_column_bytes() 4682e588c75Sdanielk1977 ** sqlite3_column_bytes16() 4692e588c75Sdanielk1977 ** 4702e588c75Sdanielk1977 ** But not for sqlite3_column_blob(), which never calls malloc(). 4712e588c75Sdanielk1977 */ 4722e588c75Sdanielk1977 static void columnMallocFailure(sqlite3_stmt *pStmt) 4732e588c75Sdanielk1977 { 4742e588c75Sdanielk1977 /* If malloc() failed during an encoding conversion within an 4752e588c75Sdanielk1977 ** sqlite3_column_XXX API, then set the return code of the statement to 4762e588c75Sdanielk1977 ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR 4772e588c75Sdanielk1977 ** and _finalize() will return NOMEM. 4782e588c75Sdanielk1977 */ 47954f0198eSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 48054f0198eSdanielk1977 p->rc = sqlite3ApiExit(0, p->rc); 4812e588c75Sdanielk1977 } 4822e588c75Sdanielk1977 4834f26d6c4Sdrh /**************************** sqlite3_column_ ******************************* 4844f26d6c4Sdrh ** The following routines are used to access elements of the current row 4854f26d6c4Sdrh ** in the result set. 4864f26d6c4Sdrh */ 487c572ef7fSdanielk1977 const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){ 4882e588c75Sdanielk1977 const void *val; 4892e588c75Sdanielk1977 sqlite3MallocDisallow(); 4902e588c75Sdanielk1977 val = sqlite3_value_blob( columnMem(pStmt,i) ); 4912e588c75Sdanielk1977 sqlite3MallocAllow(); 4922e588c75Sdanielk1977 return val; 493c572ef7fSdanielk1977 } 4944f26d6c4Sdrh int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){ 4952e588c75Sdanielk1977 int val = sqlite3_value_bytes( columnMem(pStmt,i) ); 4962e588c75Sdanielk1977 columnMallocFailure(pStmt); 4972e588c75Sdanielk1977 return val; 4984f26d6c4Sdrh } 4994f26d6c4Sdrh int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){ 5002e588c75Sdanielk1977 int val = sqlite3_value_bytes16( columnMem(pStmt,i) ); 5012e588c75Sdanielk1977 columnMallocFailure(pStmt); 5022e588c75Sdanielk1977 return val; 5034f26d6c4Sdrh } 5044f26d6c4Sdrh double sqlite3_column_double(sqlite3_stmt *pStmt, int i){ 5052e588c75Sdanielk1977 double val = sqlite3_value_double( columnMem(pStmt,i) ); 5062e588c75Sdanielk1977 columnMallocFailure(pStmt); 5072e588c75Sdanielk1977 return val; 5084f26d6c4Sdrh } 5094f26d6c4Sdrh int sqlite3_column_int(sqlite3_stmt *pStmt, int i){ 5102e588c75Sdanielk1977 int val = sqlite3_value_int( columnMem(pStmt,i) ); 5112e588c75Sdanielk1977 columnMallocFailure(pStmt); 5122e588c75Sdanielk1977 return val; 5134f26d6c4Sdrh } 514efad9995Sdrh sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){ 5152e588c75Sdanielk1977 sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) ); 5162e588c75Sdanielk1977 columnMallocFailure(pStmt); 5172e588c75Sdanielk1977 return val; 5184f26d6c4Sdrh } 5194f26d6c4Sdrh const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){ 5202e588c75Sdanielk1977 const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) ); 5212e588c75Sdanielk1977 columnMallocFailure(pStmt); 5222e588c75Sdanielk1977 return val; 5234f26d6c4Sdrh } 524d1e4733dSdrh sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){ 525d1e4733dSdrh return columnMem(pStmt, i); 526d1e4733dSdrh } 5276c62608fSdrh #ifndef SQLITE_OMIT_UTF16 5284f26d6c4Sdrh const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){ 5292e588c75Sdanielk1977 const void *val = sqlite3_value_text16( columnMem(pStmt,i) ); 5302e588c75Sdanielk1977 columnMallocFailure(pStmt); 5312e588c75Sdanielk1977 return val; 5324f26d6c4Sdrh } 5336c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 5344f26d6c4Sdrh int sqlite3_column_type(sqlite3_stmt *pStmt, int i){ 5354f26d6c4Sdrh return sqlite3_value_type( columnMem(pStmt,i) ); 5364f26d6c4Sdrh } 5374f26d6c4Sdrh 53829d72108Sdrh /* The following function is experimental and subject to change or 53929d72108Sdrh ** removal */ 54029d72108Sdrh /*int sqlite3_column_numeric_type(sqlite3_stmt *pStmt, int i){ 54129d72108Sdrh ** return sqlite3_value_numeric_type( columnMem(pStmt,i) ); 54229d72108Sdrh **} 54329d72108Sdrh */ 54429d72108Sdrh 5455e2517e1Sdrh /* 5465e2517e1Sdrh ** Convert the N-th element of pStmt->pColName[] into a string using 5475e2517e1Sdrh ** xFunc() then return that string. If N is out of range, return 0. 548e590fbdeSdrh ** 549e590fbdeSdrh ** There are up to 5 names for each column. useType determines which 550e590fbdeSdrh ** name is returned. Here are the names: 551e590fbdeSdrh ** 552e590fbdeSdrh ** 0 The column name as it should be displayed for output 553e590fbdeSdrh ** 1 The datatype name for the column 554e590fbdeSdrh ** 2 The name of the database that the column derives from 555e590fbdeSdrh ** 3 The name of the table that the column derives from 556e590fbdeSdrh ** 4 The name of the table column that the result column derives from 557e590fbdeSdrh ** 558e590fbdeSdrh ** If the result is not a simple column reference (if it is an expression 559e590fbdeSdrh ** or a constant) then useTypes 2, 3, and 4 return NULL. 5605e2517e1Sdrh */ 5615e2517e1Sdrh static const void *columnName( 5625e2517e1Sdrh sqlite3_stmt *pStmt, 5635e2517e1Sdrh int N, 5645e2517e1Sdrh const void *(*xFunc)(Mem*), 5655e2517e1Sdrh int useType 5665e2517e1Sdrh ){ 56700fd957bSdanielk1977 const void *ret; 5685e2517e1Sdrh Vdbe *p = (Vdbe *)pStmt; 5695e2517e1Sdrh int n = sqlite3_column_count(pStmt); 5705e2517e1Sdrh 5715e2517e1Sdrh if( p==0 || N>=n || N<0 ){ 5725e2517e1Sdrh return 0; 5735e2517e1Sdrh } 574e590fbdeSdrh N += useType*n; 57500fd957bSdanielk1977 ret = xFunc(&p->aColName[N]); 57600fd957bSdanielk1977 57700fd957bSdanielk1977 /* A malloc may have failed inside of the xFunc() call. If this is the case, 57800fd957bSdanielk1977 ** clear the mallocFailed flag and return NULL. 57900fd957bSdanielk1977 */ 58054f0198eSdanielk1977 sqlite3ApiExit(0, 0); 58100fd957bSdanielk1977 return ret; 5825e2517e1Sdrh } 5835e2517e1Sdrh 5844f26d6c4Sdrh /* 5854f26d6c4Sdrh ** Return the name of the Nth column of the result set returned by SQL 5864f26d6c4Sdrh ** statement pStmt. 5874f26d6c4Sdrh */ 5884f26d6c4Sdrh const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){ 589955de52cSdanielk1977 return columnName( 590955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME); 5914f26d6c4Sdrh } 592e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 593e590fbdeSdrh const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){ 594955de52cSdanielk1977 return columnName( 595955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME); 596e590fbdeSdrh } 597e590fbdeSdrh #endif 5984f26d6c4Sdrh 5994f26d6c4Sdrh /* 6006c62608fSdrh ** Return the column declaration type (if applicable) of the 'i'th column 601e590fbdeSdrh ** of the result set of SQL statement pStmt. 6026c62608fSdrh */ 6036c62608fSdrh const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){ 604955de52cSdanielk1977 return columnName( 605955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE); 6066c62608fSdrh } 6076c62608fSdrh #ifndef SQLITE_OMIT_UTF16 60876d505baSdanielk1977 const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){ 609955de52cSdanielk1977 return columnName( 610955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE); 6114f26d6c4Sdrh } 6126c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 6134f26d6c4Sdrh 6144b1ae99dSdanielk1977 #ifdef SQLITE_ENABLE_COLUMN_METADATA 615e590fbdeSdrh /* 616e590fbdeSdrh ** Return the name of the database from which a result column derives. 617e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 618e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 619e590fbdeSdrh */ 620e590fbdeSdrh const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){ 621955de52cSdanielk1977 return columnName( 622955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE); 623e590fbdeSdrh } 624e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 625e590fbdeSdrh const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){ 626955de52cSdanielk1977 return columnName( 627955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE); 628e590fbdeSdrh } 629e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 630e590fbdeSdrh 631e590fbdeSdrh /* 632e590fbdeSdrh ** Return the name of the table from which a result column derives. 633e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 634e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 635e590fbdeSdrh */ 636e590fbdeSdrh const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){ 637955de52cSdanielk1977 return columnName( 638955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE); 639e590fbdeSdrh } 640e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 641e590fbdeSdrh const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){ 642955de52cSdanielk1977 return columnName( 643955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE); 644e590fbdeSdrh } 645e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 646e590fbdeSdrh 647e590fbdeSdrh /* 648e590fbdeSdrh ** Return the name of the table column from which a result column derives. 649e590fbdeSdrh ** NULL is returned if the result column is an expression or constant or 650e590fbdeSdrh ** anything else which is not an unabiguous reference to a database column. 651e590fbdeSdrh */ 652e590fbdeSdrh const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){ 653955de52cSdanielk1977 return columnName( 654955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN); 655e590fbdeSdrh } 656e590fbdeSdrh #ifndef SQLITE_OMIT_UTF16 657e590fbdeSdrh const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){ 658955de52cSdanielk1977 return columnName( 659955de52cSdanielk1977 pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN); 660e590fbdeSdrh } 661e590fbdeSdrh #endif /* SQLITE_OMIT_UTF16 */ 6624b1ae99dSdanielk1977 #endif /* SQLITE_ENABLE_COLUMN_METADATA */ 663e590fbdeSdrh 664e590fbdeSdrh 6654f26d6c4Sdrh /******************************* sqlite3_bind_ *************************** 6664f26d6c4Sdrh ** 6674f26d6c4Sdrh ** Routines used to attach values to wildcards in a compiled SQL statement. 6684f26d6c4Sdrh */ 6694f26d6c4Sdrh /* 6704f26d6c4Sdrh ** Unbind the value bound to variable i in virtual machine p. This is the 6714f26d6c4Sdrh ** the same as binding a NULL value to the column. If the "i" parameter is 6724f26d6c4Sdrh ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK. 6734f26d6c4Sdrh ** 6744f26d6c4Sdrh ** The error code stored in database p->db is overwritten with the return 6754f26d6c4Sdrh ** value in any case. 6764f26d6c4Sdrh */ 6774f26d6c4Sdrh static int vdbeUnbind(Vdbe *p, int i){ 6784f26d6c4Sdrh Mem *pVar; 6791bcdb0c0Sdrh if( p==0 || p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){ 680473d179dSdrh if( p ) sqlite3Error(p->db, SQLITE_MISUSE, 0); 6814f26d6c4Sdrh return SQLITE_MISUSE; 6824f26d6c4Sdrh } 6834f26d6c4Sdrh if( i<1 || i>p->nVar ){ 6844f26d6c4Sdrh sqlite3Error(p->db, SQLITE_RANGE, 0); 6854f26d6c4Sdrh return SQLITE_RANGE; 6864f26d6c4Sdrh } 6874f26d6c4Sdrh i--; 688290c1948Sdrh pVar = &p->aVar[i]; 689d8123366Sdanielk1977 sqlite3VdbeMemRelease(pVar); 6904f26d6c4Sdrh pVar->flags = MEM_Null; 6914f26d6c4Sdrh sqlite3Error(p->db, SQLITE_OK, 0); 6924f26d6c4Sdrh return SQLITE_OK; 6934f26d6c4Sdrh } 6944f26d6c4Sdrh 6954f26d6c4Sdrh /* 6965e2517e1Sdrh ** Bind a text or BLOB value. 6974f26d6c4Sdrh */ 6985e2517e1Sdrh static int bindText( 699f4479501Sdrh sqlite3_stmt *pStmt, 7004f26d6c4Sdrh int i, 7014f26d6c4Sdrh const void *zData, 7024f26d6c4Sdrh int nData, 7035e2517e1Sdrh void (*xDel)(void*), 7045e2517e1Sdrh int encoding 7054f26d6c4Sdrh ){ 7064f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7074f26d6c4Sdrh Mem *pVar; 7084f26d6c4Sdrh int rc; 7094f26d6c4Sdrh 7104f26d6c4Sdrh rc = vdbeUnbind(p, i); 711feac5f8dSdrh if( rc || zData==0 ){ 7124f26d6c4Sdrh return rc; 7134f26d6c4Sdrh } 714290c1948Sdrh pVar = &p->aVar[i-1]; 7155e2517e1Sdrh rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel); 7165e2517e1Sdrh if( rc==SQLITE_OK && encoding!=0 ){ 71714db2665Sdanielk1977 rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db)); 7185e2517e1Sdrh } 71954f0198eSdanielk1977 720771151b6Sdanielk1977 sqlite3Error(((Vdbe *)pStmt)->db, rc, 0); 72154f0198eSdanielk1977 return sqlite3ApiExit(((Vdbe *)pStmt)->db, rc); 7225e2517e1Sdrh } 7235e2517e1Sdrh 7245e2517e1Sdrh 7255e2517e1Sdrh /* 7265e2517e1Sdrh ** Bind a blob value to an SQL statement variable. 7275e2517e1Sdrh */ 7285e2517e1Sdrh int sqlite3_bind_blob( 7295e2517e1Sdrh sqlite3_stmt *pStmt, 7305e2517e1Sdrh int i, 7315e2517e1Sdrh const void *zData, 7325e2517e1Sdrh int nData, 7335e2517e1Sdrh void (*xDel)(void*) 7345e2517e1Sdrh ){ 7355e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, 0); 7365e2517e1Sdrh } 7374f26d6c4Sdrh int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){ 7384f26d6c4Sdrh int rc; 7394f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7404f26d6c4Sdrh rc = vdbeUnbind(p, i); 7414f26d6c4Sdrh if( rc==SQLITE_OK ){ 742290c1948Sdrh sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue); 7434f26d6c4Sdrh } 744f4618891Sdanielk1977 return rc; 7454f26d6c4Sdrh } 7464f26d6c4Sdrh int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){ 747efad9995Sdrh return sqlite3_bind_int64(p, i, (i64)iValue); 7484f26d6c4Sdrh } 749efad9995Sdrh int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){ 7504f26d6c4Sdrh int rc; 7514f26d6c4Sdrh Vdbe *p = (Vdbe *)pStmt; 7524f26d6c4Sdrh rc = vdbeUnbind(p, i); 7534f26d6c4Sdrh if( rc==SQLITE_OK ){ 754290c1948Sdrh sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue); 7554f26d6c4Sdrh } 7564f26d6c4Sdrh return rc; 7574f26d6c4Sdrh } 7584f26d6c4Sdrh int sqlite3_bind_null(sqlite3_stmt* p, int i){ 7594f26d6c4Sdrh return vdbeUnbind((Vdbe *)p, i); 7604f26d6c4Sdrh } 7614f26d6c4Sdrh int sqlite3_bind_text( 7624f26d6c4Sdrh sqlite3_stmt *pStmt, 7634f26d6c4Sdrh int i, 7644f26d6c4Sdrh const char *zData, 7654f26d6c4Sdrh int nData, 766d8123366Sdanielk1977 void (*xDel)(void*) 7674f26d6c4Sdrh ){ 7685e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8); 7694f26d6c4Sdrh } 7706c62608fSdrh #ifndef SQLITE_OMIT_UTF16 7714f26d6c4Sdrh int sqlite3_bind_text16( 7724f26d6c4Sdrh sqlite3_stmt *pStmt, 7734f26d6c4Sdrh int i, 7744f26d6c4Sdrh const void *zData, 7754f26d6c4Sdrh int nData, 776d8123366Sdanielk1977 void (*xDel)(void*) 7774f26d6c4Sdrh ){ 7785e2517e1Sdrh return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE); 7794f26d6c4Sdrh } 7806c62608fSdrh #endif /* SQLITE_OMIT_UTF16 */ 78126e4144dSdanielk1977 int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){ 78226e4144dSdanielk1977 int rc; 78326e4144dSdanielk1977 Vdbe *p = (Vdbe *)pStmt; 78426e4144dSdanielk1977 rc = vdbeUnbind(p, i); 78526e4144dSdanielk1977 if( rc==SQLITE_OK ){ 78626e4144dSdanielk1977 sqlite3VdbeMemCopy(&p->aVar[i-1], pValue); 78726e4144dSdanielk1977 } 78826e4144dSdanielk1977 return rc; 78926e4144dSdanielk1977 } 79075f6a032Sdrh 79175f6a032Sdrh /* 79275f6a032Sdrh ** Return the number of wildcards that can be potentially bound to. 79375f6a032Sdrh ** This routine is added to support DBD::SQLite. 79475f6a032Sdrh */ 79575f6a032Sdrh int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){ 79692febd92Sdrh Vdbe *p = (Vdbe*)pStmt; 79792febd92Sdrh return p ? p->nVar : 0; 79875f6a032Sdrh } 799895d7472Sdrh 800895d7472Sdrh /* 801fa6bc000Sdrh ** Create a mapping from variable numbers to variable names 802fa6bc000Sdrh ** in the Vdbe.azVar[] array, if such a mapping does not already 803fa6bc000Sdrh ** exist. 804895d7472Sdrh */ 805fa6bc000Sdrh static void createVarMap(Vdbe *p){ 806895d7472Sdrh if( !p->okVar ){ 807895d7472Sdrh int j; 808895d7472Sdrh Op *pOp; 809895d7472Sdrh for(j=0, pOp=p->aOp; j<p->nOp; j++, pOp++){ 810895d7472Sdrh if( pOp->opcode==OP_Variable ){ 811895d7472Sdrh assert( pOp->p1>0 && pOp->p1<=p->nVar ); 812895d7472Sdrh p->azVar[pOp->p1-1] = pOp->p3; 813895d7472Sdrh } 814895d7472Sdrh } 815895d7472Sdrh p->okVar = 1; 816895d7472Sdrh } 817fa6bc000Sdrh } 818fa6bc000Sdrh 819fa6bc000Sdrh /* 820fa6bc000Sdrh ** Return the name of a wildcard parameter. Return NULL if the index 821fa6bc000Sdrh ** is out of range or if the wildcard is unnamed. 822fa6bc000Sdrh ** 823fa6bc000Sdrh ** The result is always UTF-8. 824fa6bc000Sdrh */ 825fa6bc000Sdrh const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){ 826fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 827fa6bc000Sdrh if( p==0 || i<1 || i>p->nVar ){ 828fa6bc000Sdrh return 0; 829fa6bc000Sdrh } 830fa6bc000Sdrh createVarMap(p); 831895d7472Sdrh return p->azVar[i-1]; 832895d7472Sdrh } 833fa6bc000Sdrh 834fa6bc000Sdrh /* 835fa6bc000Sdrh ** Given a wildcard parameter name, return the index of the variable 836fa6bc000Sdrh ** with that name. If there is no variable with the given name, 837fa6bc000Sdrh ** return 0. 838fa6bc000Sdrh */ 839fa6bc000Sdrh int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){ 840fa6bc000Sdrh Vdbe *p = (Vdbe*)pStmt; 841fa6bc000Sdrh int i; 842fa6bc000Sdrh if( p==0 ){ 843fa6bc000Sdrh return 0; 844fa6bc000Sdrh } 845fa6bc000Sdrh createVarMap(p); 8466a8903c3Sdrh if( zName ){ 847fa6bc000Sdrh for(i=0; i<p->nVar; i++){ 848971a7c87Sdrh const char *z = p->azVar[i]; 8496a8903c3Sdrh if( z && strcmp(z,zName)==0 ){ 850fa6bc000Sdrh return i+1; 851fa6bc000Sdrh } 852fa6bc000Sdrh } 8536a8903c3Sdrh } 854fa6bc000Sdrh return 0; 855fa6bc000Sdrh } 856f8db1bc0Sdrh 85751942bc3Sdrh /* 858f8db1bc0Sdrh ** Transfer all bindings from the first statement over to the second. 859f8db1bc0Sdrh ** If the two statements contain a different number of bindings, then 860f8db1bc0Sdrh ** an SQLITE_ERROR is returned. 861f8db1bc0Sdrh */ 862f8db1bc0Sdrh int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){ 863f8db1bc0Sdrh Vdbe *pFrom = (Vdbe*)pFromStmt; 864f8db1bc0Sdrh Vdbe *pTo = (Vdbe*)pToStmt; 865f8db1bc0Sdrh int i, rc = SQLITE_OK; 866f8db1bc0Sdrh if( (pFrom->magic!=VDBE_MAGIC_RUN && pFrom->magic!=VDBE_MAGIC_HALT) 867f8db1bc0Sdrh || (pTo->magic!=VDBE_MAGIC_RUN && pTo->magic!=VDBE_MAGIC_HALT) ){ 868f8db1bc0Sdrh return SQLITE_MISUSE; 869f8db1bc0Sdrh } 870f8db1bc0Sdrh if( pFrom->nVar!=pTo->nVar ){ 871f8db1bc0Sdrh return SQLITE_ERROR; 872f8db1bc0Sdrh } 873f8db1bc0Sdrh for(i=0; rc==SQLITE_OK && i<pFrom->nVar; i++){ 874950f054cSdanielk1977 sqlite3MallocDisallow(); 875f8db1bc0Sdrh rc = sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]); 876950f054cSdanielk1977 sqlite3MallocAllow(); 877f8db1bc0Sdrh } 8784ac285a1Sdrh assert( rc==SQLITE_OK || rc==SQLITE_NOMEM ); 879f8db1bc0Sdrh return rc; 880f8db1bc0Sdrh } 88151942bc3Sdrh 88251942bc3Sdrh /* 88351942bc3Sdrh ** Return the sqlite3* database handle to which the prepared statement given 88451942bc3Sdrh ** in the argument belongs. This is the same database handle that was 88551942bc3Sdrh ** the first argument to the sqlite3_prepare() that was used to create 88651942bc3Sdrh ** the statement in the first place. 88751942bc3Sdrh */ 88851942bc3Sdrh sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){ 88951942bc3Sdrh return pStmt ? ((Vdbe*)pStmt)->db : 0; 89051942bc3Sdrh } 891